blob: 45061b591346f04af9eb108f2f80b9d36039db20 [file] [log] [blame]
Boris Brezillonf88fc122017-03-16 09:02:40 +01001/*
2 * Copyright 2017 ATMEL
3 * Copyright 2017 Free Electrons
4 *
5 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
6 *
7 * Derived from the atmel_nand.c driver which contained the following
8 * copyrights:
9 *
10 * Copyright 2003 Rick Bronson
11 *
Boris Brezillon187c54482018-02-05 23:02:02 +010012 * Derived from drivers/mtd/nand/autcpu12.c (removed in v3.8)
Boris Brezillonf88fc122017-03-16 09:02:40 +010013 * Copyright 2001 Thomas Gleixner (gleixner@autronix.de)
14 *
Boris Brezillon187c54482018-02-05 23:02:02 +010015 * Derived from drivers/mtd/spia.c (removed in v3.8)
Boris Brezillonf88fc122017-03-16 09:02:40 +010016 * Copyright 2000 Steven J. Hill (sjhill@cotw.com)
17 *
18 *
19 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
20 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright 2007
21 *
22 * Derived from Das U-Boot source code
23 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
24 * Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
25 *
26 * Add Programmable Multibit ECC support for various AT91 SoC
27 * Copyright 2012 ATMEL, Hong Xu
28 *
29 * Add Nand Flash Controller support for SAMA5 SoC
30 * Copyright 2013 ATMEL, Josh Wu (josh.wu@atmel.com)
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License version 2 as
34 * published by the Free Software Foundation.
35 *
36 * A few words about the naming convention in this file. This convention
37 * applies to structure and function names.
38 *
39 * Prefixes:
40 *
41 * - atmel_nand_: all generic structures/functions
42 * - atmel_smc_nand_: all structures/functions specific to the SMC interface
43 * (at91sam9 and avr32 SoCs)
44 * - atmel_hsmc_nand_: all structures/functions specific to the HSMC interface
45 * (sama5 SoCs and later)
46 * - atmel_nfc_: all structures/functions used to manipulate the NFC sub-block
47 * that is available in the HSMC block
48 * - <soc>_nand_: all SoC specific structures/functions
49 */
50
51#include <linux/clk.h>
52#include <linux/dma-mapping.h>
53#include <linux/dmaengine.h>
54#include <linux/genalloc.h>
Boris Brezillonf88fc122017-03-16 09:02:40 +010055#include <linux/gpio/consumer.h>
56#include <linux/interrupt.h>
57#include <linux/mfd/syscon.h>
58#include <linux/mfd/syscon/atmel-matrix.h>
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +010059#include <linux/mfd/syscon/atmel-smc.h>
Boris Brezillonf88fc122017-03-16 09:02:40 +010060#include <linux/module.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020061#include <linux/mtd/rawnand.h>
Boris Brezillonf88fc122017-03-16 09:02:40 +010062#include <linux/of_address.h>
63#include <linux/of_irq.h>
64#include <linux/of_platform.h>
65#include <linux/iopoll.h>
66#include <linux/platform_device.h>
Boris Brezillonf88fc122017-03-16 09:02:40 +010067#include <linux/regmap.h>
68
69#include "pmecc.h"
70
71#define ATMEL_HSMC_NFC_CFG 0x0
72#define ATMEL_HSMC_NFC_CFG_SPARESIZE(x) (((x) / 4) << 24)
73#define ATMEL_HSMC_NFC_CFG_SPARESIZE_MASK GENMASK(30, 24)
74#define ATMEL_HSMC_NFC_CFG_DTO(cyc, mul) (((cyc) << 16) | ((mul) << 20))
75#define ATMEL_HSMC_NFC_CFG_DTO_MAX GENMASK(22, 16)
76#define ATMEL_HSMC_NFC_CFG_RBEDGE BIT(13)
77#define ATMEL_HSMC_NFC_CFG_FALLING_EDGE BIT(12)
78#define ATMEL_HSMC_NFC_CFG_RSPARE BIT(9)
79#define ATMEL_HSMC_NFC_CFG_WSPARE BIT(8)
80#define ATMEL_HSMC_NFC_CFG_PAGESIZE_MASK GENMASK(2, 0)
81#define ATMEL_HSMC_NFC_CFG_PAGESIZE(x) (fls((x) / 512) - 1)
82
83#define ATMEL_HSMC_NFC_CTRL 0x4
84#define ATMEL_HSMC_NFC_CTRL_EN BIT(0)
85#define ATMEL_HSMC_NFC_CTRL_DIS BIT(1)
86
87#define ATMEL_HSMC_NFC_SR 0x8
88#define ATMEL_HSMC_NFC_IER 0xc
89#define ATMEL_HSMC_NFC_IDR 0x10
90#define ATMEL_HSMC_NFC_IMR 0x14
91#define ATMEL_HSMC_NFC_SR_ENABLED BIT(1)
92#define ATMEL_HSMC_NFC_SR_RB_RISE BIT(4)
93#define ATMEL_HSMC_NFC_SR_RB_FALL BIT(5)
94#define ATMEL_HSMC_NFC_SR_BUSY BIT(8)
95#define ATMEL_HSMC_NFC_SR_WR BIT(11)
96#define ATMEL_HSMC_NFC_SR_CSID GENMASK(14, 12)
97#define ATMEL_HSMC_NFC_SR_XFRDONE BIT(16)
98#define ATMEL_HSMC_NFC_SR_CMDDONE BIT(17)
99#define ATMEL_HSMC_NFC_SR_DTOE BIT(20)
100#define ATMEL_HSMC_NFC_SR_UNDEF BIT(21)
101#define ATMEL_HSMC_NFC_SR_AWB BIT(22)
102#define ATMEL_HSMC_NFC_SR_NFCASE BIT(23)
103#define ATMEL_HSMC_NFC_SR_ERRORS (ATMEL_HSMC_NFC_SR_DTOE | \
104 ATMEL_HSMC_NFC_SR_UNDEF | \
105 ATMEL_HSMC_NFC_SR_AWB | \
106 ATMEL_HSMC_NFC_SR_NFCASE)
107#define ATMEL_HSMC_NFC_SR_RBEDGE(x) BIT((x) + 24)
108
109#define ATMEL_HSMC_NFC_ADDR 0x18
110#define ATMEL_HSMC_NFC_BANK 0x1c
111
112#define ATMEL_NFC_MAX_RB_ID 7
113
114#define ATMEL_NFC_SRAM_SIZE 0x2400
115
116#define ATMEL_NFC_CMD(pos, cmd) ((cmd) << (((pos) * 8) + 2))
117#define ATMEL_NFC_VCMD2 BIT(18)
118#define ATMEL_NFC_ACYCLE(naddrs) ((naddrs) << 19)
119#define ATMEL_NFC_CSID(cs) ((cs) << 22)
120#define ATMEL_NFC_DATAEN BIT(25)
121#define ATMEL_NFC_NFCWR BIT(26)
122
123#define ATMEL_NFC_MAX_ADDR_CYCLES 5
124
125#define ATMEL_NAND_ALE_OFFSET BIT(21)
126#define ATMEL_NAND_CLE_OFFSET BIT(22)
127
128#define DEFAULT_TIMEOUT_MS 1000
129#define MIN_DMA_LEN 128
130
Peter Rosinefc63622018-03-29 15:10:54 +0200131static bool atmel_nand_avoid_dma __read_mostly;
132
133MODULE_PARM_DESC(avoiddma, "Avoid using DMA");
134module_param_named(avoiddma, atmel_nand_avoid_dma, bool, 0400);
135
Boris Brezillonf88fc122017-03-16 09:02:40 +0100136enum atmel_nand_rb_type {
137 ATMEL_NAND_NO_RB,
138 ATMEL_NAND_NATIVE_RB,
139 ATMEL_NAND_GPIO_RB,
140};
141
142struct atmel_nand_rb {
143 enum atmel_nand_rb_type type;
144 union {
145 struct gpio_desc *gpio;
146 int id;
147 };
148};
149
150struct atmel_nand_cs {
151 int id;
152 struct atmel_nand_rb rb;
153 struct gpio_desc *csgpio;
154 struct {
155 void __iomem *virt;
156 dma_addr_t dma;
157 } io;
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +0100158
159 struct atmel_smc_cs_conf smcconf;
Boris Brezillonf88fc122017-03-16 09:02:40 +0100160};
161
162struct atmel_nand {
163 struct list_head node;
164 struct device *dev;
165 struct nand_chip base;
166 struct atmel_nand_cs *activecs;
167 struct atmel_pmecc_user *pmecc;
168 struct gpio_desc *cdgpio;
169 int numcs;
170 struct atmel_nand_cs cs[];
171};
172
173static inline struct atmel_nand *to_atmel_nand(struct nand_chip *chip)
174{
175 return container_of(chip, struct atmel_nand, base);
176}
177
178enum atmel_nfc_data_xfer {
179 ATMEL_NFC_NO_DATA,
180 ATMEL_NFC_READ_DATA,
181 ATMEL_NFC_WRITE_DATA,
182};
183
184struct atmel_nfc_op {
185 u8 cs;
186 u8 ncmds;
187 u8 cmds[2];
188 u8 naddrs;
189 u8 addrs[5];
190 enum atmel_nfc_data_xfer data;
191 u32 wait;
192 u32 errors;
193};
194
195struct atmel_nand_controller;
196struct atmel_nand_controller_caps;
197
198struct atmel_nand_controller_ops {
199 int (*probe)(struct platform_device *pdev,
200 const struct atmel_nand_controller_caps *caps);
201 int (*remove)(struct atmel_nand_controller *nc);
202 void (*nand_init)(struct atmel_nand_controller *nc,
203 struct atmel_nand *nand);
Miquel Raynal577e0102018-07-25 15:31:41 +0200204 int (*ecc_init)(struct nand_chip *chip);
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +0100205 int (*setup_data_interface)(struct atmel_nand *nand, int csline,
206 const struct nand_data_interface *conf);
Boris Brezillonf88fc122017-03-16 09:02:40 +0100207};
208
209struct atmel_nand_controller_caps {
210 bool has_dma;
211 bool legacy_of_bindings;
212 u32 ale_offs;
213 u32 cle_offs;
214 const struct atmel_nand_controller_ops *ops;
215};
216
217struct atmel_nand_controller {
Miquel Raynal7da45132018-07-17 09:08:02 +0200218 struct nand_controller base;
Boris Brezillonf88fc122017-03-16 09:02:40 +0100219 const struct atmel_nand_controller_caps *caps;
220 struct device *dev;
221 struct regmap *smc;
222 struct dma_chan *dmac;
223 struct atmel_pmecc *pmecc;
224 struct list_head chips;
225 struct clk *mck;
226};
227
228static inline struct atmel_nand_controller *
Miquel Raynal7da45132018-07-17 09:08:02 +0200229to_nand_controller(struct nand_controller *ctl)
Boris Brezillonf88fc122017-03-16 09:02:40 +0100230{
231 return container_of(ctl, struct atmel_nand_controller, base);
232}
233
234struct atmel_smc_nand_controller {
235 struct atmel_nand_controller base;
236 struct regmap *matrix;
237 unsigned int ebi_csa_offs;
238};
239
240static inline struct atmel_smc_nand_controller *
Miquel Raynal7da45132018-07-17 09:08:02 +0200241to_smc_nand_controller(struct nand_controller *ctl)
Boris Brezillonf88fc122017-03-16 09:02:40 +0100242{
243 return container_of(to_nand_controller(ctl),
244 struct atmel_smc_nand_controller, base);
245}
246
247struct atmel_hsmc_nand_controller {
248 struct atmel_nand_controller base;
249 struct {
250 struct gen_pool *pool;
251 void __iomem *virt;
252 dma_addr_t dma;
253 } sram;
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +0200254 const struct atmel_hsmc_reg_layout *hsmc_layout;
Boris Brezillonf88fc122017-03-16 09:02:40 +0100255 struct regmap *io;
256 struct atmel_nfc_op op;
257 struct completion complete;
258 int irq;
259
260 /* Only used when instantiating from legacy DT bindings. */
261 struct clk *clk;
262};
263
264static inline struct atmel_hsmc_nand_controller *
Miquel Raynal7da45132018-07-17 09:08:02 +0200265to_hsmc_nand_controller(struct nand_controller *ctl)
Boris Brezillonf88fc122017-03-16 09:02:40 +0100266{
267 return container_of(to_nand_controller(ctl),
268 struct atmel_hsmc_nand_controller, base);
269}
270
271static bool atmel_nfc_op_done(struct atmel_nfc_op *op, u32 status)
272{
273 op->errors |= status & ATMEL_HSMC_NFC_SR_ERRORS;
274 op->wait ^= status & op->wait;
275
276 return !op->wait || op->errors;
277}
278
279static irqreturn_t atmel_nfc_interrupt(int irq, void *data)
280{
281 struct atmel_hsmc_nand_controller *nc = data;
282 u32 sr, rcvd;
283 bool done;
284
285 regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &sr);
286
287 rcvd = sr & (nc->op.wait | ATMEL_HSMC_NFC_SR_ERRORS);
288 done = atmel_nfc_op_done(&nc->op, sr);
289
290 if (rcvd)
291 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, rcvd);
292
293 if (done)
294 complete(&nc->complete);
295
296 return rcvd ? IRQ_HANDLED : IRQ_NONE;
297}
298
299static int atmel_nfc_wait(struct atmel_hsmc_nand_controller *nc, bool poll,
300 unsigned int timeout_ms)
301{
302 int ret;
303
304 if (!timeout_ms)
305 timeout_ms = DEFAULT_TIMEOUT_MS;
306
307 if (poll) {
308 u32 status;
309
310 ret = regmap_read_poll_timeout(nc->base.smc,
311 ATMEL_HSMC_NFC_SR, status,
312 atmel_nfc_op_done(&nc->op,
313 status),
314 0, timeout_ms * 1000);
315 } else {
316 init_completion(&nc->complete);
317 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IER,
318 nc->op.wait | ATMEL_HSMC_NFC_SR_ERRORS);
319 ret = wait_for_completion_timeout(&nc->complete,
320 msecs_to_jiffies(timeout_ms));
321 if (!ret)
322 ret = -ETIMEDOUT;
323 else
324 ret = 0;
325
326 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, 0xffffffff);
327 }
328
329 if (nc->op.errors & ATMEL_HSMC_NFC_SR_DTOE) {
330 dev_err(nc->base.dev, "Waiting NAND R/B Timeout\n");
331 ret = -ETIMEDOUT;
332 }
333
334 if (nc->op.errors & ATMEL_HSMC_NFC_SR_UNDEF) {
335 dev_err(nc->base.dev, "Access to an undefined area\n");
336 ret = -EIO;
337 }
338
339 if (nc->op.errors & ATMEL_HSMC_NFC_SR_AWB) {
340 dev_err(nc->base.dev, "Access while busy\n");
341 ret = -EIO;
342 }
343
344 if (nc->op.errors & ATMEL_HSMC_NFC_SR_NFCASE) {
345 dev_err(nc->base.dev, "Wrong access size\n");
346 ret = -EIO;
347 }
348
349 return ret;
350}
351
352static void atmel_nand_dma_transfer_finished(void *data)
353{
354 struct completion *finished = data;
355
356 complete(finished);
357}
358
359static int atmel_nand_dma_transfer(struct atmel_nand_controller *nc,
360 void *buf, dma_addr_t dev_dma, size_t len,
361 enum dma_data_direction dir)
362{
363 DECLARE_COMPLETION_ONSTACK(finished);
364 dma_addr_t src_dma, dst_dma, buf_dma;
365 struct dma_async_tx_descriptor *tx;
366 dma_cookie_t cookie;
367
368 buf_dma = dma_map_single(nc->dev, buf, len, dir);
369 if (dma_mapping_error(nc->dev, dev_dma)) {
370 dev_err(nc->dev,
371 "Failed to prepare a buffer for DMA access\n");
372 goto err;
373 }
374
375 if (dir == DMA_FROM_DEVICE) {
376 src_dma = dev_dma;
377 dst_dma = buf_dma;
378 } else {
379 src_dma = buf_dma;
380 dst_dma = dev_dma;
381 }
382
383 tx = dmaengine_prep_dma_memcpy(nc->dmac, dst_dma, src_dma, len,
384 DMA_CTRL_ACK | DMA_PREP_INTERRUPT);
385 if (!tx) {
386 dev_err(nc->dev, "Failed to prepare DMA memcpy\n");
387 goto err_unmap;
388 }
389
390 tx->callback = atmel_nand_dma_transfer_finished;
391 tx->callback_param = &finished;
392
393 cookie = dmaengine_submit(tx);
394 if (dma_submit_error(cookie)) {
395 dev_err(nc->dev, "Failed to do DMA tx_submit\n");
396 goto err_unmap;
397 }
398
399 dma_async_issue_pending(nc->dmac);
400 wait_for_completion(&finished);
401
402 return 0;
403
404err_unmap:
405 dma_unmap_single(nc->dev, buf_dma, len, dir);
406
407err:
408 dev_dbg(nc->dev, "Fall back to CPU I/O\n");
409
410 return -EIO;
411}
412
413static u8 atmel_nand_read_byte(struct mtd_info *mtd)
414{
415 struct nand_chip *chip = mtd_to_nand(mtd);
416 struct atmel_nand *nand = to_atmel_nand(chip);
417
418 return ioread8(nand->activecs->io.virt);
419}
420
Boris Brezillonf88fc122017-03-16 09:02:40 +0100421static void atmel_nand_write_byte(struct mtd_info *mtd, u8 byte)
422{
423 struct nand_chip *chip = mtd_to_nand(mtd);
424 struct atmel_nand *nand = to_atmel_nand(chip);
425
426 if (chip->options & NAND_BUSWIDTH_16)
427 iowrite16(byte | (byte << 8), nand->activecs->io.virt);
428 else
429 iowrite8(byte, nand->activecs->io.virt);
430}
431
432static void atmel_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
433{
434 struct nand_chip *chip = mtd_to_nand(mtd);
435 struct atmel_nand *nand = to_atmel_nand(chip);
436 struct atmel_nand_controller *nc;
437
438 nc = to_nand_controller(chip->controller);
439
440 /*
441 * If the controller supports DMA, the buffer address is DMA-able and
442 * len is long enough to make DMA transfers profitable, let's trigger
443 * a DMA transfer. If it fails, fallback to PIO mode.
444 */
445 if (nc->dmac && virt_addr_valid(buf) &&
446 len >= MIN_DMA_LEN &&
447 !atmel_nand_dma_transfer(nc, buf, nand->activecs->io.dma, len,
448 DMA_FROM_DEVICE))
449 return;
450
451 if (chip->options & NAND_BUSWIDTH_16)
452 ioread16_rep(nand->activecs->io.virt, buf, len / 2);
453 else
454 ioread8_rep(nand->activecs->io.virt, buf, len);
455}
456
457static void atmel_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
458{
459 struct nand_chip *chip = mtd_to_nand(mtd);
460 struct atmel_nand *nand = to_atmel_nand(chip);
461 struct atmel_nand_controller *nc;
462
463 nc = to_nand_controller(chip->controller);
464
465 /*
466 * If the controller supports DMA, the buffer address is DMA-able and
467 * len is long enough to make DMA transfers profitable, let's trigger
468 * a DMA transfer. If it fails, fallback to PIO mode.
469 */
470 if (nc->dmac && virt_addr_valid(buf) &&
471 len >= MIN_DMA_LEN &&
472 !atmel_nand_dma_transfer(nc, (void *)buf, nand->activecs->io.dma,
473 len, DMA_TO_DEVICE))
474 return;
475
476 if (chip->options & NAND_BUSWIDTH_16)
477 iowrite16_rep(nand->activecs->io.virt, buf, len / 2);
478 else
479 iowrite8_rep(nand->activecs->io.virt, buf, len);
480}
481
482static int atmel_nand_dev_ready(struct mtd_info *mtd)
483{
484 struct nand_chip *chip = mtd_to_nand(mtd);
485 struct atmel_nand *nand = to_atmel_nand(chip);
486
487 return gpiod_get_value(nand->activecs->rb.gpio);
488}
489
490static void atmel_nand_select_chip(struct mtd_info *mtd, int cs)
491{
492 struct nand_chip *chip = mtd_to_nand(mtd);
493 struct atmel_nand *nand = to_atmel_nand(chip);
494
495 if (cs < 0 || cs >= nand->numcs) {
496 nand->activecs = NULL;
497 chip->dev_ready = NULL;
498 return;
499 }
500
501 nand->activecs = &nand->cs[cs];
502
503 if (nand->activecs->rb.type == ATMEL_NAND_GPIO_RB)
504 chip->dev_ready = atmel_nand_dev_ready;
505}
506
507static int atmel_hsmc_nand_dev_ready(struct mtd_info *mtd)
508{
509 struct nand_chip *chip = mtd_to_nand(mtd);
510 struct atmel_nand *nand = to_atmel_nand(chip);
511 struct atmel_hsmc_nand_controller *nc;
512 u32 status;
513
514 nc = to_hsmc_nand_controller(chip->controller);
515
516 regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &status);
517
518 return status & ATMEL_HSMC_NFC_SR_RBEDGE(nand->activecs->rb.id);
519}
520
521static void atmel_hsmc_nand_select_chip(struct mtd_info *mtd, int cs)
522{
523 struct nand_chip *chip = mtd_to_nand(mtd);
524 struct atmel_nand *nand = to_atmel_nand(chip);
525 struct atmel_hsmc_nand_controller *nc;
526
527 nc = to_hsmc_nand_controller(chip->controller);
528
529 atmel_nand_select_chip(mtd, cs);
530
531 if (!nand->activecs) {
532 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CTRL,
533 ATMEL_HSMC_NFC_CTRL_DIS);
534 return;
535 }
536
537 if (nand->activecs->rb.type == ATMEL_NAND_NATIVE_RB)
538 chip->dev_ready = atmel_hsmc_nand_dev_ready;
539
540 regmap_update_bits(nc->base.smc, ATMEL_HSMC_NFC_CFG,
541 ATMEL_HSMC_NFC_CFG_PAGESIZE_MASK |
542 ATMEL_HSMC_NFC_CFG_SPARESIZE_MASK |
543 ATMEL_HSMC_NFC_CFG_RSPARE |
544 ATMEL_HSMC_NFC_CFG_WSPARE,
545 ATMEL_HSMC_NFC_CFG_PAGESIZE(mtd->writesize) |
546 ATMEL_HSMC_NFC_CFG_SPARESIZE(mtd->oobsize) |
547 ATMEL_HSMC_NFC_CFG_RSPARE);
548 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CTRL,
549 ATMEL_HSMC_NFC_CTRL_EN);
550}
551
552static int atmel_nfc_exec_op(struct atmel_hsmc_nand_controller *nc, bool poll)
553{
554 u8 *addrs = nc->op.addrs;
555 unsigned int op = 0;
556 u32 addr, val;
557 int i, ret;
558
559 nc->op.wait = ATMEL_HSMC_NFC_SR_CMDDONE;
560
561 for (i = 0; i < nc->op.ncmds; i++)
562 op |= ATMEL_NFC_CMD(i, nc->op.cmds[i]);
563
564 if (nc->op.naddrs == ATMEL_NFC_MAX_ADDR_CYCLES)
565 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_ADDR, *addrs++);
566
567 op |= ATMEL_NFC_CSID(nc->op.cs) |
568 ATMEL_NFC_ACYCLE(nc->op.naddrs);
569
570 if (nc->op.ncmds > 1)
571 op |= ATMEL_NFC_VCMD2;
572
573 addr = addrs[0] | (addrs[1] << 8) | (addrs[2] << 16) |
574 (addrs[3] << 24);
575
576 if (nc->op.data != ATMEL_NFC_NO_DATA) {
577 op |= ATMEL_NFC_DATAEN;
578 nc->op.wait |= ATMEL_HSMC_NFC_SR_XFRDONE;
579
580 if (nc->op.data == ATMEL_NFC_WRITE_DATA)
581 op |= ATMEL_NFC_NFCWR;
582 }
583
584 /* Clear all flags. */
585 regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &val);
586
587 /* Send the command. */
588 regmap_write(nc->io, op, addr);
589
590 ret = atmel_nfc_wait(nc, poll, 0);
591 if (ret)
592 dev_err(nc->base.dev,
593 "Failed to send NAND command (err = %d)!",
594 ret);
595
596 /* Reset the op state. */
597 memset(&nc->op, 0, sizeof(nc->op));
598
599 return ret;
600}
601
602static void atmel_hsmc_nand_cmd_ctrl(struct mtd_info *mtd, int dat,
603 unsigned int ctrl)
604{
605 struct nand_chip *chip = mtd_to_nand(mtd);
606 struct atmel_nand *nand = to_atmel_nand(chip);
607 struct atmel_hsmc_nand_controller *nc;
608
609 nc = to_hsmc_nand_controller(chip->controller);
610
611 if (ctrl & NAND_ALE) {
612 if (nc->op.naddrs == ATMEL_NFC_MAX_ADDR_CYCLES)
613 return;
614
615 nc->op.addrs[nc->op.naddrs++] = dat;
616 } else if (ctrl & NAND_CLE) {
617 if (nc->op.ncmds > 1)
618 return;
619
620 nc->op.cmds[nc->op.ncmds++] = dat;
621 }
622
623 if (dat == NAND_CMD_NONE) {
624 nc->op.cs = nand->activecs->id;
625 atmel_nfc_exec_op(nc, true);
626 }
627}
628
629static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
630 unsigned int ctrl)
631{
632 struct nand_chip *chip = mtd_to_nand(mtd);
633 struct atmel_nand *nand = to_atmel_nand(chip);
634 struct atmel_nand_controller *nc;
635
636 nc = to_nand_controller(chip->controller);
637
638 if ((ctrl & NAND_CTRL_CHANGE) && nand->activecs->csgpio) {
639 if (ctrl & NAND_NCE)
640 gpiod_set_value(nand->activecs->csgpio, 0);
641 else
642 gpiod_set_value(nand->activecs->csgpio, 1);
643 }
644
645 if (ctrl & NAND_ALE)
646 writeb(cmd, nand->activecs->io.virt + nc->caps->ale_offs);
647 else if (ctrl & NAND_CLE)
648 writeb(cmd, nand->activecs->io.virt + nc->caps->cle_offs);
649}
650
651static void atmel_nfc_copy_to_sram(struct nand_chip *chip, const u8 *buf,
652 bool oob_required)
653{
654 struct mtd_info *mtd = nand_to_mtd(chip);
655 struct atmel_hsmc_nand_controller *nc;
656 int ret = -EIO;
657
658 nc = to_hsmc_nand_controller(chip->controller);
659
660 if (nc->base.dmac)
661 ret = atmel_nand_dma_transfer(&nc->base, (void *)buf,
662 nc->sram.dma, mtd->writesize,
663 DMA_TO_DEVICE);
664
665 /* Falling back to CPU copy. */
666 if (ret)
667 memcpy_toio(nc->sram.virt, buf, mtd->writesize);
668
669 if (oob_required)
670 memcpy_toio(nc->sram.virt + mtd->writesize, chip->oob_poi,
671 mtd->oobsize);
672}
673
674static void atmel_nfc_copy_from_sram(struct nand_chip *chip, u8 *buf,
675 bool oob_required)
676{
677 struct mtd_info *mtd = nand_to_mtd(chip);
678 struct atmel_hsmc_nand_controller *nc;
679 int ret = -EIO;
680
681 nc = to_hsmc_nand_controller(chip->controller);
682
683 if (nc->base.dmac)
684 ret = atmel_nand_dma_transfer(&nc->base, buf, nc->sram.dma,
685 mtd->writesize, DMA_FROM_DEVICE);
686
687 /* Falling back to CPU copy. */
688 if (ret)
689 memcpy_fromio(buf, nc->sram.virt, mtd->writesize);
690
691 if (oob_required)
692 memcpy_fromio(chip->oob_poi, nc->sram.virt + mtd->writesize,
693 mtd->oobsize);
694}
695
696static void atmel_nfc_set_op_addr(struct nand_chip *chip, int page, int column)
697{
698 struct mtd_info *mtd = nand_to_mtd(chip);
699 struct atmel_hsmc_nand_controller *nc;
700
701 nc = to_hsmc_nand_controller(chip->controller);
702
703 if (column >= 0) {
704 nc->op.addrs[nc->op.naddrs++] = column;
705
706 /*
707 * 2 address cycles for the column offset on large page NANDs.
708 */
709 if (mtd->writesize > 512)
710 nc->op.addrs[nc->op.naddrs++] = column >> 8;
711 }
712
713 if (page >= 0) {
714 nc->op.addrs[nc->op.naddrs++] = page;
715 nc->op.addrs[nc->op.naddrs++] = page >> 8;
716
Masahiro Yamada14157f82017-09-13 11:05:50 +0900717 if (chip->options & NAND_ROW_ADDR_3)
Boris Brezillonf88fc122017-03-16 09:02:40 +0100718 nc->op.addrs[nc->op.naddrs++] = page >> 16;
719 }
720}
721
722static int atmel_nand_pmecc_enable(struct nand_chip *chip, int op, bool raw)
723{
724 struct atmel_nand *nand = to_atmel_nand(chip);
725 struct atmel_nand_controller *nc;
726 int ret;
727
728 nc = to_nand_controller(chip->controller);
729
730 if (raw)
731 return 0;
732
733 ret = atmel_pmecc_enable(nand->pmecc, op);
734 if (ret)
735 dev_err(nc->dev,
736 "Failed to enable ECC engine (err = %d)\n", ret);
737
738 return ret;
739}
740
741static void atmel_nand_pmecc_disable(struct nand_chip *chip, bool raw)
742{
743 struct atmel_nand *nand = to_atmel_nand(chip);
744
745 if (!raw)
746 atmel_pmecc_disable(nand->pmecc);
747}
748
749static int atmel_nand_pmecc_generate_eccbytes(struct nand_chip *chip, bool raw)
750{
751 struct atmel_nand *nand = to_atmel_nand(chip);
752 struct mtd_info *mtd = nand_to_mtd(chip);
753 struct atmel_nand_controller *nc;
754 struct mtd_oob_region oobregion;
755 void *eccbuf;
756 int ret, i;
757
758 nc = to_nand_controller(chip->controller);
759
760 if (raw)
761 return 0;
762
763 ret = atmel_pmecc_wait_rdy(nand->pmecc);
764 if (ret) {
765 dev_err(nc->dev,
766 "Failed to transfer NAND page data (err = %d)\n",
767 ret);
768 return ret;
769 }
770
771 mtd_ooblayout_ecc(mtd, 0, &oobregion);
772 eccbuf = chip->oob_poi + oobregion.offset;
773
774 for (i = 0; i < chip->ecc.steps; i++) {
775 atmel_pmecc_get_generated_eccbytes(nand->pmecc, i,
776 eccbuf);
777 eccbuf += chip->ecc.bytes;
778 }
779
780 return 0;
781}
782
783static int atmel_nand_pmecc_correct_data(struct nand_chip *chip, void *buf,
784 bool raw)
785{
786 struct atmel_nand *nand = to_atmel_nand(chip);
787 struct mtd_info *mtd = nand_to_mtd(chip);
788 struct atmel_nand_controller *nc;
789 struct mtd_oob_region oobregion;
790 int ret, i, max_bitflips = 0;
791 void *databuf, *eccbuf;
792
793 nc = to_nand_controller(chip->controller);
794
795 if (raw)
796 return 0;
797
798 ret = atmel_pmecc_wait_rdy(nand->pmecc);
799 if (ret) {
800 dev_err(nc->dev,
801 "Failed to read NAND page data (err = %d)\n",
802 ret);
803 return ret;
804 }
805
806 mtd_ooblayout_ecc(mtd, 0, &oobregion);
807 eccbuf = chip->oob_poi + oobregion.offset;
808 databuf = buf;
809
810 for (i = 0; i < chip->ecc.steps; i++) {
811 ret = atmel_pmecc_correct_sector(nand->pmecc, i, databuf,
812 eccbuf);
813 if (ret < 0 && !atmel_pmecc_correct_erased_chunks(nand->pmecc))
814 ret = nand_check_erased_ecc_chunk(databuf,
815 chip->ecc.size,
816 eccbuf,
817 chip->ecc.bytes,
818 NULL, 0,
819 chip->ecc.strength);
820
821 if (ret >= 0)
822 max_bitflips = max(ret, max_bitflips);
823 else
824 mtd->ecc_stats.failed++;
825
826 databuf += chip->ecc.size;
827 eccbuf += chip->ecc.bytes;
828 }
829
830 return max_bitflips;
831}
832
833static int atmel_nand_pmecc_write_pg(struct nand_chip *chip, const u8 *buf,
834 bool oob_required, int page, bool raw)
835{
836 struct mtd_info *mtd = nand_to_mtd(chip);
837 struct atmel_nand *nand = to_atmel_nand(chip);
838 int ret;
839
Boris Brezillon25f815f2017-11-30 18:01:30 +0100840 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
841
Boris Brezillonf88fc122017-03-16 09:02:40 +0100842 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw);
843 if (ret)
844 return ret;
845
846 atmel_nand_write_buf(mtd, buf, mtd->writesize);
847
848 ret = atmel_nand_pmecc_generate_eccbytes(chip, raw);
849 if (ret) {
850 atmel_pmecc_disable(nand->pmecc);
851 return ret;
852 }
853
854 atmel_nand_pmecc_disable(chip, raw);
855
856 atmel_nand_write_buf(mtd, chip->oob_poi, mtd->oobsize);
857
Boris Brezillon25f815f2017-11-30 18:01:30 +0100858 return nand_prog_page_end_op(chip);
Boris Brezillonf88fc122017-03-16 09:02:40 +0100859}
860
861static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
862 struct nand_chip *chip, const u8 *buf,
863 int oob_required, int page)
864{
865 return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, false);
866}
867
868static int atmel_nand_pmecc_write_page_raw(struct mtd_info *mtd,
869 struct nand_chip *chip,
870 const u8 *buf, int oob_required,
871 int page)
872{
873 return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, true);
874}
875
876static int atmel_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf,
877 bool oob_required, int page, bool raw)
878{
879 struct mtd_info *mtd = nand_to_mtd(chip);
880 int ret;
881
Boris Brezillon25f815f2017-11-30 18:01:30 +0100882 nand_read_page_op(chip, page, 0, NULL, 0);
883
Boris Brezillonf88fc122017-03-16 09:02:40 +0100884 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw);
885 if (ret)
886 return ret;
887
888 atmel_nand_read_buf(mtd, buf, mtd->writesize);
889 atmel_nand_read_buf(mtd, chip->oob_poi, mtd->oobsize);
890
891 ret = atmel_nand_pmecc_correct_data(chip, buf, raw);
892
893 atmel_nand_pmecc_disable(chip, raw);
894
895 return ret;
896}
897
Boris Brezillonb9761682018-09-06 14:05:20 +0200898static int atmel_nand_pmecc_read_page(struct nand_chip *chip, u8 *buf,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100899 int oob_required, int page)
900{
901 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, false);
902}
903
Boris Brezillonb9761682018-09-06 14:05:20 +0200904static int atmel_nand_pmecc_read_page_raw(struct nand_chip *chip, u8 *buf,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100905 int oob_required, int page)
906{
907 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, true);
908}
909
910static int atmel_hsmc_nand_pmecc_write_pg(struct nand_chip *chip,
911 const u8 *buf, bool oob_required,
912 int page, bool raw)
913{
914 struct mtd_info *mtd = nand_to_mtd(chip);
915 struct atmel_nand *nand = to_atmel_nand(chip);
916 struct atmel_hsmc_nand_controller *nc;
Boris Brezillon41145642017-05-16 18:27:49 +0200917 int ret, status;
Boris Brezillonf88fc122017-03-16 09:02:40 +0100918
919 nc = to_hsmc_nand_controller(chip->controller);
920
921 atmel_nfc_copy_to_sram(chip, buf, false);
922
923 nc->op.cmds[0] = NAND_CMD_SEQIN;
924 nc->op.ncmds = 1;
925 atmel_nfc_set_op_addr(chip, page, 0x0);
926 nc->op.cs = nand->activecs->id;
927 nc->op.data = ATMEL_NFC_WRITE_DATA;
928
929 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw);
930 if (ret)
931 return ret;
932
933 ret = atmel_nfc_exec_op(nc, false);
934 if (ret) {
935 atmel_nand_pmecc_disable(chip, raw);
936 dev_err(nc->base.dev,
937 "Failed to transfer NAND page data (err = %d)\n",
938 ret);
939 return ret;
940 }
941
942 ret = atmel_nand_pmecc_generate_eccbytes(chip, raw);
943
944 atmel_nand_pmecc_disable(chip, raw);
945
946 if (ret)
947 return ret;
948
949 atmel_nand_write_buf(mtd, chip->oob_poi, mtd->oobsize);
950
951 nc->op.cmds[0] = NAND_CMD_PAGEPROG;
952 nc->op.ncmds = 1;
953 nc->op.cs = nand->activecs->id;
954 ret = atmel_nfc_exec_op(nc, false);
955 if (ret)
956 dev_err(nc->base.dev, "Failed to program NAND page (err = %d)\n",
957 ret);
958
Boris Brezillon41145642017-05-16 18:27:49 +0200959 status = chip->waitfunc(mtd, chip);
960 if (status & NAND_STATUS_FAIL)
961 return -EIO;
962
Boris Brezillonf88fc122017-03-16 09:02:40 +0100963 return ret;
964}
965
966static int atmel_hsmc_nand_pmecc_write_page(struct mtd_info *mtd,
967 struct nand_chip *chip,
968 const u8 *buf, int oob_required,
969 int page)
970{
971 return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page,
972 false);
973}
974
975static int atmel_hsmc_nand_pmecc_write_page_raw(struct mtd_info *mtd,
976 struct nand_chip *chip,
977 const u8 *buf,
978 int oob_required, int page)
979{
980 return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page,
981 true);
982}
983
984static int atmel_hsmc_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf,
985 bool oob_required, int page,
986 bool raw)
987{
988 struct mtd_info *mtd = nand_to_mtd(chip);
989 struct atmel_nand *nand = to_atmel_nand(chip);
990 struct atmel_hsmc_nand_controller *nc;
991 int ret;
992
993 nc = to_hsmc_nand_controller(chip->controller);
994
995 /*
996 * Optimized read page accessors only work when the NAND R/B pin is
997 * connected to a native SoC R/B pin. If that's not the case, fallback
998 * to the non-optimized one.
999 */
1000 if (nand->activecs->rb.type != ATMEL_NAND_NATIVE_RB) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001001 nand_read_page_op(chip, page, 0, NULL, 0);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001002
1003 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page,
1004 raw);
1005 }
1006
1007 nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READ0;
1008
1009 if (mtd->writesize > 512)
1010 nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READSTART;
1011
1012 atmel_nfc_set_op_addr(chip, page, 0x0);
1013 nc->op.cs = nand->activecs->id;
1014 nc->op.data = ATMEL_NFC_READ_DATA;
1015
1016 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw);
1017 if (ret)
1018 return ret;
1019
1020 ret = atmel_nfc_exec_op(nc, false);
1021 if (ret) {
1022 atmel_nand_pmecc_disable(chip, raw);
1023 dev_err(nc->base.dev,
1024 "Failed to load NAND page data (err = %d)\n",
1025 ret);
1026 return ret;
1027 }
1028
1029 atmel_nfc_copy_from_sram(chip, buf, true);
1030
1031 ret = atmel_nand_pmecc_correct_data(chip, buf, raw);
1032
1033 atmel_nand_pmecc_disable(chip, raw);
1034
1035 return ret;
1036}
1037
Boris Brezillonb9761682018-09-06 14:05:20 +02001038static int atmel_hsmc_nand_pmecc_read_page(struct nand_chip *chip, u8 *buf,
Boris Brezillonf88fc122017-03-16 09:02:40 +01001039 int oob_required, int page)
1040{
1041 return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page,
1042 false);
1043}
1044
Boris Brezillonb9761682018-09-06 14:05:20 +02001045static int atmel_hsmc_nand_pmecc_read_page_raw(struct nand_chip *chip,
Boris Brezillonf88fc122017-03-16 09:02:40 +01001046 u8 *buf, int oob_required,
1047 int page)
1048{
1049 return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page,
1050 true);
1051}
1052
1053static int atmel_nand_pmecc_init(struct nand_chip *chip)
1054{
1055 struct mtd_info *mtd = nand_to_mtd(chip);
1056 struct atmel_nand *nand = to_atmel_nand(chip);
1057 struct atmel_nand_controller *nc;
1058 struct atmel_pmecc_user_req req;
1059
1060 nc = to_nand_controller(chip->controller);
1061
1062 if (!nc->pmecc) {
1063 dev_err(nc->dev, "HW ECC not supported\n");
1064 return -ENOTSUPP;
1065 }
1066
1067 if (nc->caps->legacy_of_bindings) {
1068 u32 val;
1069
1070 if (!of_property_read_u32(nc->dev->of_node, "atmel,pmecc-cap",
1071 &val))
1072 chip->ecc.strength = val;
1073
1074 if (!of_property_read_u32(nc->dev->of_node,
1075 "atmel,pmecc-sector-size",
1076 &val))
1077 chip->ecc.size = val;
1078 }
1079
1080 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
1081 req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH;
1082 else if (chip->ecc.strength)
1083 req.ecc.strength = chip->ecc.strength;
1084 else if (chip->ecc_strength_ds)
1085 req.ecc.strength = chip->ecc_strength_ds;
1086 else
1087 req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH;
1088
1089 if (chip->ecc.size)
1090 req.ecc.sectorsize = chip->ecc.size;
1091 else if (chip->ecc_step_ds)
1092 req.ecc.sectorsize = chip->ecc_step_ds;
1093 else
1094 req.ecc.sectorsize = ATMEL_PMECC_SECTOR_SIZE_AUTO;
1095
1096 req.pagesize = mtd->writesize;
1097 req.oobsize = mtd->oobsize;
1098
1099 if (mtd->writesize <= 512) {
1100 req.ecc.bytes = 4;
1101 req.ecc.ooboffset = 0;
1102 } else {
1103 req.ecc.bytes = mtd->oobsize - 2;
1104 req.ecc.ooboffset = ATMEL_PMECC_OOBOFFSET_AUTO;
1105 }
1106
1107 nand->pmecc = atmel_pmecc_create_user(nc->pmecc, &req);
1108 if (IS_ERR(nand->pmecc))
1109 return PTR_ERR(nand->pmecc);
1110
1111 chip->ecc.algo = NAND_ECC_BCH;
1112 chip->ecc.size = req.ecc.sectorsize;
1113 chip->ecc.bytes = req.ecc.bytes / req.ecc.nsectors;
1114 chip->ecc.strength = req.ecc.strength;
1115
1116 chip->options |= NAND_NO_SUBPAGE_WRITE;
1117
1118 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
1119
1120 return 0;
1121}
1122
Miquel Raynal577e0102018-07-25 15:31:41 +02001123static int atmel_nand_ecc_init(struct nand_chip *chip)
Boris Brezillonf88fc122017-03-16 09:02:40 +01001124{
Boris Brezillonf88fc122017-03-16 09:02:40 +01001125 struct atmel_nand_controller *nc;
1126 int ret;
1127
1128 nc = to_nand_controller(chip->controller);
1129
1130 switch (chip->ecc.mode) {
1131 case NAND_ECC_NONE:
1132 case NAND_ECC_SOFT:
1133 /*
1134 * Nothing to do, the core will initialize everything for us.
1135 */
1136 break;
1137
1138 case NAND_ECC_HW:
1139 ret = atmel_nand_pmecc_init(chip);
1140 if (ret)
1141 return ret;
1142
1143 chip->ecc.read_page = atmel_nand_pmecc_read_page;
1144 chip->ecc.write_page = atmel_nand_pmecc_write_page;
1145 chip->ecc.read_page_raw = atmel_nand_pmecc_read_page_raw;
1146 chip->ecc.write_page_raw = atmel_nand_pmecc_write_page_raw;
1147 break;
1148
1149 default:
1150 /* Other modes are not supported. */
1151 dev_err(nc->dev, "Unsupported ECC mode: %d\n",
1152 chip->ecc.mode);
1153 return -ENOTSUPP;
1154 }
1155
1156 return 0;
1157}
1158
Miquel Raynal577e0102018-07-25 15:31:41 +02001159static int atmel_hsmc_nand_ecc_init(struct nand_chip *chip)
Boris Brezillonf88fc122017-03-16 09:02:40 +01001160{
Boris Brezillonf88fc122017-03-16 09:02:40 +01001161 int ret;
1162
Miquel Raynal577e0102018-07-25 15:31:41 +02001163 ret = atmel_nand_ecc_init(chip);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001164 if (ret)
1165 return ret;
1166
1167 if (chip->ecc.mode != NAND_ECC_HW)
1168 return 0;
1169
1170 /* Adjust the ECC operations for the HSMC IP. */
1171 chip->ecc.read_page = atmel_hsmc_nand_pmecc_read_page;
1172 chip->ecc.write_page = atmel_hsmc_nand_pmecc_write_page;
1173 chip->ecc.read_page_raw = atmel_hsmc_nand_pmecc_read_page_raw;
1174 chip->ecc.write_page_raw = atmel_hsmc_nand_pmecc_write_page_raw;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001175
1176 return 0;
1177}
1178
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001179static int atmel_smc_nand_prepare_smcconf(struct atmel_nand *nand,
1180 const struct nand_data_interface *conf,
1181 struct atmel_smc_cs_conf *smcconf)
1182{
1183 u32 ncycles, totalcycles, timeps, mckperiodps;
1184 struct atmel_nand_controller *nc;
1185 int ret;
1186
1187 nc = to_nand_controller(nand->base.controller);
1188
1189 /* DDR interface not supported. */
1190 if (conf->type != NAND_SDR_IFACE)
1191 return -ENOTSUPP;
1192
1193 /*
1194 * tRC < 30ns implies EDO mode. This controller does not support this
1195 * mode.
1196 */
Boris Brezillonee02f732017-07-31 10:32:21 +02001197 if (conf->timings.sdr.tRC_min < 30000)
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001198 return -ENOTSUPP;
1199
1200 atmel_smc_cs_conf_init(smcconf);
1201
1202 mckperiodps = NSEC_PER_SEC / clk_get_rate(nc->mck);
1203 mckperiodps *= 1000;
1204
1205 /*
1206 * Set write pulse timing. This one is easy to extract:
1207 *
1208 * NWE_PULSE = tWP
1209 */
1210 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWP_min, mckperiodps);
1211 totalcycles = ncycles;
1212 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NWE_SHIFT,
1213 ncycles);
1214 if (ret)
1215 return ret;
1216
1217 /*
1218 * The write setup timing depends on the operation done on the NAND.
1219 * All operations goes through the same data bus, but the operation
1220 * type depends on the address we are writing to (ALE/CLE address
1221 * lines).
1222 * Since we have no way to differentiate the different operations at
1223 * the SMC level, we must consider the worst case (the biggest setup
1224 * time among all operation types):
1225 *
1226 * NWE_SETUP = max(tCLS, tCS, tALS, tDS) - NWE_PULSE
1227 */
1228 timeps = max3(conf->timings.sdr.tCLS_min, conf->timings.sdr.tCS_min,
1229 conf->timings.sdr.tALS_min);
1230 timeps = max(timeps, conf->timings.sdr.tDS_min);
1231 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1232 ncycles = ncycles > totalcycles ? ncycles - totalcycles : 0;
1233 totalcycles += ncycles;
1234 ret = atmel_smc_cs_conf_set_setup(smcconf, ATMEL_SMC_NWE_SHIFT,
1235 ncycles);
1236 if (ret)
1237 return ret;
1238
1239 /*
1240 * As for the write setup timing, the write hold timing depends on the
1241 * operation done on the NAND:
1242 *
1243 * NWE_HOLD = max(tCLH, tCH, tALH, tDH, tWH)
1244 */
1245 timeps = max3(conf->timings.sdr.tCLH_min, conf->timings.sdr.tCH_min,
1246 conf->timings.sdr.tALH_min);
1247 timeps = max3(timeps, conf->timings.sdr.tDH_min,
1248 conf->timings.sdr.tWH_min);
1249 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1250 totalcycles += ncycles;
1251
1252 /*
1253 * The write cycle timing is directly matching tWC, but is also
1254 * dependent on the other timings on the setup and hold timings we
1255 * calculated earlier, which gives:
1256 *
1257 * NWE_CYCLE = max(tWC, NWE_SETUP + NWE_PULSE + NWE_HOLD)
1258 */
1259 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWC_min, mckperiodps);
1260 ncycles = max(totalcycles, ncycles);
1261 ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NWE_SHIFT,
1262 ncycles);
1263 if (ret)
1264 return ret;
1265
1266 /*
1267 * We don't want the CS line to be toggled between each byte/word
1268 * transfer to the NAND. The only way to guarantee that is to have the
1269 * NCS_{WR,RD}_{SETUP,HOLD} timings set to 0, which in turn means:
1270 *
1271 * NCS_WR_PULSE = NWE_CYCLE
1272 */
1273 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_WR_SHIFT,
1274 ncycles);
1275 if (ret)
1276 return ret;
1277
1278 /*
1279 * As for the write setup timing, the read hold timing depends on the
1280 * operation done on the NAND:
1281 *
1282 * NRD_HOLD = max(tREH, tRHOH)
1283 */
1284 timeps = max(conf->timings.sdr.tREH_min, conf->timings.sdr.tRHOH_min);
1285 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1286 totalcycles = ncycles;
1287
1288 /*
1289 * TDF = tRHZ - NRD_HOLD
1290 */
1291 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRHZ_max, mckperiodps);
1292 ncycles -= totalcycles;
1293
1294 /*
1295 * In ONFI 4.0 specs, tRHZ has been increased to support EDO NANDs and
1296 * we might end up with a config that does not fit in the TDF field.
1297 * Just take the max value in this case and hope that the NAND is more
1298 * tolerant than advertised.
1299 */
1300 if (ncycles > ATMEL_SMC_MODE_TDF_MAX)
1301 ncycles = ATMEL_SMC_MODE_TDF_MAX;
1302 else if (ncycles < ATMEL_SMC_MODE_TDF_MIN)
1303 ncycles = ATMEL_SMC_MODE_TDF_MIN;
1304
1305 smcconf->mode |= ATMEL_SMC_MODE_TDF(ncycles) |
1306 ATMEL_SMC_MODE_TDFMODE_OPTIMIZED;
1307
1308 /*
1309 * Read pulse timing directly matches tRP:
1310 *
1311 * NRD_PULSE = tRP
1312 */
1313 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRP_min, mckperiodps);
1314 totalcycles += ncycles;
1315 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NRD_SHIFT,
1316 ncycles);
1317 if (ret)
1318 return ret;
1319
1320 /*
1321 * The write cycle timing is directly matching tWC, but is also
1322 * dependent on the setup and hold timings we calculated earlier,
1323 * which gives:
1324 *
1325 * NRD_CYCLE = max(tRC, NRD_PULSE + NRD_HOLD)
1326 *
1327 * NRD_SETUP is always 0.
1328 */
1329 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRC_min, mckperiodps);
1330 ncycles = max(totalcycles, ncycles);
1331 ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NRD_SHIFT,
1332 ncycles);
1333 if (ret)
1334 return ret;
1335
1336 /*
1337 * We don't want the CS line to be toggled between each byte/word
1338 * transfer from the NAND. The only way to guarantee that is to have
1339 * the NCS_{WR,RD}_{SETUP,HOLD} timings set to 0, which in turn means:
1340 *
1341 * NCS_RD_PULSE = NRD_CYCLE
1342 */
1343 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_RD_SHIFT,
1344 ncycles);
1345 if (ret)
1346 return ret;
1347
1348 /* Txxx timings are directly matching tXXX ones. */
1349 ncycles = DIV_ROUND_UP(conf->timings.sdr.tCLR_min, mckperiodps);
1350 ret = atmel_smc_cs_conf_set_timing(smcconf,
1351 ATMEL_HSMC_TIMINGS_TCLR_SHIFT,
1352 ncycles);
1353 if (ret)
1354 return ret;
1355
1356 ncycles = DIV_ROUND_UP(conf->timings.sdr.tADL_min, mckperiodps);
1357 ret = atmel_smc_cs_conf_set_timing(smcconf,
1358 ATMEL_HSMC_TIMINGS_TADL_SHIFT,
1359 ncycles);
Boris Brezillonbe3e83e2017-08-23 20:45:01 +02001360 /*
1361 * Version 4 of the ONFI spec mandates that tADL be at least 400
1362 * nanoseconds, but, depending on the master clock rate, 400 ns may not
1363 * fit in the tADL field of the SMC reg. We need to relax the check and
1364 * accept the -ERANGE return code.
1365 *
1366 * Note that previous versions of the ONFI spec had a lower tADL_min
1367 * (100 or 200 ns). It's not clear why this timing constraint got
1368 * increased but it seems most NANDs are fine with values lower than
1369 * 400ns, so we should be safe.
1370 */
1371 if (ret && ret != -ERANGE)
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001372 return ret;
1373
1374 ncycles = DIV_ROUND_UP(conf->timings.sdr.tAR_min, mckperiodps);
1375 ret = atmel_smc_cs_conf_set_timing(smcconf,
1376 ATMEL_HSMC_TIMINGS_TAR_SHIFT,
1377 ncycles);
1378 if (ret)
1379 return ret;
1380
1381 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRR_min, mckperiodps);
1382 ret = atmel_smc_cs_conf_set_timing(smcconf,
1383 ATMEL_HSMC_TIMINGS_TRR_SHIFT,
1384 ncycles);
1385 if (ret)
1386 return ret;
1387
1388 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWB_max, mckperiodps);
1389 ret = atmel_smc_cs_conf_set_timing(smcconf,
1390 ATMEL_HSMC_TIMINGS_TWB_SHIFT,
1391 ncycles);
1392 if (ret)
1393 return ret;
1394
1395 /* Attach the CS line to the NFC logic. */
1396 smcconf->timings |= ATMEL_HSMC_TIMINGS_NFSEL;
1397
1398 /* Set the appropriate data bus width. */
1399 if (nand->base.options & NAND_BUSWIDTH_16)
1400 smcconf->mode |= ATMEL_SMC_MODE_DBW_16;
1401
1402 /* Operate in NRD/NWE READ/WRITEMODE. */
1403 smcconf->mode |= ATMEL_SMC_MODE_READMODE_NRD |
1404 ATMEL_SMC_MODE_WRITEMODE_NWE;
1405
1406 return 0;
1407}
1408
1409static int atmel_smc_nand_setup_data_interface(struct atmel_nand *nand,
1410 int csline,
1411 const struct nand_data_interface *conf)
1412{
1413 struct atmel_nand_controller *nc;
1414 struct atmel_smc_cs_conf smcconf;
1415 struct atmel_nand_cs *cs;
1416 int ret;
1417
1418 nc = to_nand_controller(nand->base.controller);
1419
1420 ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf);
1421 if (ret)
1422 return ret;
1423
1424 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1425 return 0;
1426
1427 cs = &nand->cs[csline];
1428 cs->smcconf = smcconf;
1429 atmel_smc_cs_conf_apply(nc->smc, cs->id, &cs->smcconf);
1430
1431 return 0;
1432}
1433
1434static int atmel_hsmc_nand_setup_data_interface(struct atmel_nand *nand,
1435 int csline,
1436 const struct nand_data_interface *conf)
1437{
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02001438 struct atmel_hsmc_nand_controller *nc;
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001439 struct atmel_smc_cs_conf smcconf;
1440 struct atmel_nand_cs *cs;
1441 int ret;
1442
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02001443 nc = to_hsmc_nand_controller(nand->base.controller);
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001444
1445 ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf);
1446 if (ret)
1447 return ret;
1448
1449 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1450 return 0;
1451
1452 cs = &nand->cs[csline];
1453 cs->smcconf = smcconf;
1454
1455 if (cs->rb.type == ATMEL_NAND_NATIVE_RB)
1456 cs->smcconf.timings |= ATMEL_HSMC_TIMINGS_RBNSEL(cs->rb.id);
1457
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02001458 atmel_hsmc_cs_conf_apply(nc->base.smc, nc->hsmc_layout, cs->id,
1459 &cs->smcconf);
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001460
1461 return 0;
1462}
1463
1464static int atmel_nand_setup_data_interface(struct mtd_info *mtd, int csline,
1465 const struct nand_data_interface *conf)
1466{
1467 struct nand_chip *chip = mtd_to_nand(mtd);
1468 struct atmel_nand *nand = to_atmel_nand(chip);
1469 struct atmel_nand_controller *nc;
1470
1471 nc = to_nand_controller(nand->base.controller);
1472
1473 if (csline >= nand->numcs ||
1474 (csline < 0 && csline != NAND_DATA_IFACE_CHECK_ONLY))
1475 return -EINVAL;
1476
1477 return nc->caps->ops->setup_data_interface(nand, csline, conf);
1478}
1479
Boris Brezillonf88fc122017-03-16 09:02:40 +01001480static void atmel_nand_init(struct atmel_nand_controller *nc,
1481 struct atmel_nand *nand)
1482{
1483 struct nand_chip *chip = &nand->base;
1484 struct mtd_info *mtd = nand_to_mtd(chip);
1485
1486 mtd->dev.parent = nc->dev;
1487 nand->base.controller = &nc->base;
1488
1489 chip->cmd_ctrl = atmel_nand_cmd_ctrl;
1490 chip->read_byte = atmel_nand_read_byte;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001491 chip->write_byte = atmel_nand_write_byte;
1492 chip->read_buf = atmel_nand_read_buf;
1493 chip->write_buf = atmel_nand_write_buf;
1494 chip->select_chip = atmel_nand_select_chip;
1495
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001496 if (nc->mck && nc->caps->ops->setup_data_interface)
1497 chip->setup_data_interface = atmel_nand_setup_data_interface;
1498
Boris Brezillonf88fc122017-03-16 09:02:40 +01001499 /* Some NANDs require a longer delay than the default one (20us). */
1500 chip->chip_delay = 40;
1501
1502 /*
1503 * Use a bounce buffer when the buffer passed by the MTD user is not
1504 * suitable for DMA.
1505 */
1506 if (nc->dmac)
1507 chip->options |= NAND_USE_BOUNCE_BUFFER;
1508
1509 /* Default to HW ECC if pmecc is available. */
1510 if (nc->pmecc)
1511 chip->ecc.mode = NAND_ECC_HW;
1512}
1513
1514static void atmel_smc_nand_init(struct atmel_nand_controller *nc,
1515 struct atmel_nand *nand)
1516{
1517 struct nand_chip *chip = &nand->base;
1518 struct atmel_smc_nand_controller *smc_nc;
1519 int i;
1520
1521 atmel_nand_init(nc, nand);
1522
1523 smc_nc = to_smc_nand_controller(chip->controller);
1524 if (!smc_nc->matrix)
1525 return;
1526
1527 /* Attach the CS to the NAND Flash logic. */
1528 for (i = 0; i < nand->numcs; i++)
1529 regmap_update_bits(smc_nc->matrix, smc_nc->ebi_csa_offs,
1530 BIT(nand->cs[i].id), BIT(nand->cs[i].id));
1531}
1532
1533static void atmel_hsmc_nand_init(struct atmel_nand_controller *nc,
1534 struct atmel_nand *nand)
1535{
1536 struct nand_chip *chip = &nand->base;
1537
1538 atmel_nand_init(nc, nand);
1539
1540 /* Overload some methods for the HSMC controller. */
1541 chip->cmd_ctrl = atmel_hsmc_nand_cmd_ctrl;
1542 chip->select_chip = atmel_hsmc_nand_select_chip;
1543}
1544
Miquel Raynal79282252018-07-25 15:31:40 +02001545static int atmel_nand_controller_remove_nand(struct atmel_nand *nand)
Boris Brezillonf88fc122017-03-16 09:02:40 +01001546{
1547 struct nand_chip *chip = &nand->base;
1548 struct mtd_info *mtd = nand_to_mtd(chip);
1549 int ret;
1550
1551 ret = mtd_device_unregister(mtd);
1552 if (ret)
1553 return ret;
1554
1555 nand_cleanup(chip);
1556 list_del(&nand->node);
1557
1558 return 0;
1559}
1560
Boris Brezillonf88fc122017-03-16 09:02:40 +01001561static struct atmel_nand *atmel_nand_create(struct atmel_nand_controller *nc,
1562 struct device_node *np,
1563 int reg_cells)
1564{
1565 struct atmel_nand *nand;
1566 struct gpio_desc *gpio;
1567 int numcs, ret, i;
1568
1569 numcs = of_property_count_elems_of_size(np, "reg",
1570 reg_cells * sizeof(u32));
1571 if (numcs < 1) {
1572 dev_err(nc->dev, "Missing or invalid reg property\n");
1573 return ERR_PTR(-EINVAL);
1574 }
1575
Gustavo A. R. Silva2f91eb62018-08-23 20:09:38 -05001576 nand = devm_kzalloc(nc->dev, struct_size(nand, cs, numcs), GFP_KERNEL);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001577 if (!nand) {
1578 dev_err(nc->dev, "Failed to allocate NAND object\n");
1579 return ERR_PTR(-ENOMEM);
1580 }
1581
1582 nand->numcs = numcs;
1583
1584 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, "det", 0,
1585 &np->fwnode, GPIOD_IN,
1586 "nand-det");
1587 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1588 dev_err(nc->dev,
1589 "Failed to get detect gpio (err = %ld)\n",
1590 PTR_ERR(gpio));
1591 return ERR_CAST(gpio);
1592 }
1593
1594 if (!IS_ERR(gpio))
1595 nand->cdgpio = gpio;
1596
1597 for (i = 0; i < numcs; i++) {
1598 struct resource res;
1599 u32 val;
1600
1601 ret = of_address_to_resource(np, 0, &res);
1602 if (ret) {
1603 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1604 ret);
1605 return ERR_PTR(ret);
1606 }
1607
1608 ret = of_property_read_u32_index(np, "reg", i * reg_cells,
1609 &val);
1610 if (ret) {
1611 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1612 ret);
1613 return ERR_PTR(ret);
1614 }
1615
1616 nand->cs[i].id = val;
1617
1618 nand->cs[i].io.dma = res.start;
1619 nand->cs[i].io.virt = devm_ioremap_resource(nc->dev, &res);
1620 if (IS_ERR(nand->cs[i].io.virt))
1621 return ERR_CAST(nand->cs[i].io.virt);
1622
1623 if (!of_property_read_u32(np, "atmel,rb", &val)) {
1624 if (val > ATMEL_NFC_MAX_RB_ID)
1625 return ERR_PTR(-EINVAL);
1626
1627 nand->cs[i].rb.type = ATMEL_NAND_NATIVE_RB;
1628 nand->cs[i].rb.id = val;
1629 } else {
1630 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev,
1631 "rb", i, &np->fwnode,
1632 GPIOD_IN, "nand-rb");
1633 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1634 dev_err(nc->dev,
1635 "Failed to get R/B gpio (err = %ld)\n",
1636 PTR_ERR(gpio));
1637 return ERR_CAST(gpio);
1638 }
1639
1640 if (!IS_ERR(gpio)) {
1641 nand->cs[i].rb.type = ATMEL_NAND_GPIO_RB;
1642 nand->cs[i].rb.gpio = gpio;
1643 }
1644 }
1645
1646 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, "cs",
1647 i, &np->fwnode,
1648 GPIOD_OUT_HIGH,
1649 "nand-cs");
1650 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1651 dev_err(nc->dev,
1652 "Failed to get CS gpio (err = %ld)\n",
1653 PTR_ERR(gpio));
1654 return ERR_CAST(gpio);
1655 }
1656
1657 if (!IS_ERR(gpio))
1658 nand->cs[i].csgpio = gpio;
1659 }
1660
1661 nand_set_flash_node(&nand->base, np);
1662
1663 return nand;
1664}
1665
1666static int
1667atmel_nand_controller_add_nand(struct atmel_nand_controller *nc,
1668 struct atmel_nand *nand)
1669{
Miquel Raynal577e0102018-07-25 15:31:41 +02001670 struct nand_chip *chip = &nand->base;
1671 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001672 int ret;
1673
1674 /* No card inserted, skip this NAND. */
1675 if (nand->cdgpio && gpiod_get_value(nand->cdgpio)) {
1676 dev_info(nc->dev, "No SmartMedia card inserted.\n");
1677 return 0;
1678 }
1679
1680 nc->caps->ops->nand_init(nc, nand);
1681
Boris Brezillon00ad3782018-09-06 14:05:14 +02001682 ret = nand_scan(chip, nand->numcs);
Miquel Raynal79282252018-07-25 15:31:40 +02001683 if (ret) {
Miquel Raynal577e0102018-07-25 15:31:41 +02001684 dev_err(nc->dev, "NAND scan failed: %d\n", ret);
Miquel Raynal79282252018-07-25 15:31:40 +02001685 return ret;
1686 }
1687
1688 ret = mtd_device_register(mtd, NULL, 0);
1689 if (ret) {
1690 dev_err(nc->dev, "Failed to register mtd device: %d\n", ret);
1691 nand_cleanup(chip);
1692 return ret;
1693 }
1694
1695 list_add_tail(&nand->node, &nc->chips);
1696
1697 return 0;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001698}
1699
1700static int
1701atmel_nand_controller_remove_nands(struct atmel_nand_controller *nc)
1702{
1703 struct atmel_nand *nand, *tmp;
1704 int ret;
1705
1706 list_for_each_entry_safe(nand, tmp, &nc->chips, node) {
Miquel Raynal79282252018-07-25 15:31:40 +02001707 ret = atmel_nand_controller_remove_nand(nand);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001708 if (ret)
1709 return ret;
1710 }
1711
1712 return 0;
1713}
1714
1715static int
1716atmel_nand_controller_legacy_add_nands(struct atmel_nand_controller *nc)
1717{
1718 struct device *dev = nc->dev;
1719 struct platform_device *pdev = to_platform_device(dev);
1720 struct atmel_nand *nand;
1721 struct gpio_desc *gpio;
1722 struct resource *res;
1723
1724 /*
1725 * Legacy bindings only allow connecting a single NAND with a unique CS
1726 * line to the controller.
1727 */
1728 nand = devm_kzalloc(nc->dev, sizeof(*nand) + sizeof(*nand->cs),
1729 GFP_KERNEL);
1730 if (!nand)
1731 return -ENOMEM;
1732
1733 nand->numcs = 1;
1734
1735 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1736 nand->cs[0].io.virt = devm_ioremap_resource(dev, res);
1737 if (IS_ERR(nand->cs[0].io.virt))
1738 return PTR_ERR(nand->cs[0].io.virt);
1739
1740 nand->cs[0].io.dma = res->start;
1741
1742 /*
1743 * The old driver was hardcoding the CS id to 3 for all sama5
1744 * controllers. Since this id is only meaningful for the sama5
1745 * controller we can safely assign this id to 3 no matter the
1746 * controller.
1747 * If one wants to connect a NAND to a different CS line, he will
1748 * have to use the new bindings.
1749 */
1750 nand->cs[0].id = 3;
1751
1752 /* R/B GPIO. */
1753 gpio = devm_gpiod_get_index_optional(dev, NULL, 0, GPIOD_IN);
1754 if (IS_ERR(gpio)) {
1755 dev_err(dev, "Failed to get R/B gpio (err = %ld)\n",
1756 PTR_ERR(gpio));
1757 return PTR_ERR(gpio);
1758 }
1759
1760 if (gpio) {
1761 nand->cs[0].rb.type = ATMEL_NAND_GPIO_RB;
1762 nand->cs[0].rb.gpio = gpio;
1763 }
1764
1765 /* CS GPIO. */
1766 gpio = devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_HIGH);
1767 if (IS_ERR(gpio)) {
1768 dev_err(dev, "Failed to get CS gpio (err = %ld)\n",
1769 PTR_ERR(gpio));
1770 return PTR_ERR(gpio);
1771 }
1772
1773 nand->cs[0].csgpio = gpio;
1774
1775 /* Card detect GPIO. */
1776 gpio = devm_gpiod_get_index_optional(nc->dev, NULL, 2, GPIOD_IN);
1777 if (IS_ERR(gpio)) {
1778 dev_err(dev,
1779 "Failed to get detect gpio (err = %ld)\n",
1780 PTR_ERR(gpio));
1781 return PTR_ERR(gpio);
1782 }
1783
1784 nand->cdgpio = gpio;
1785
1786 nand_set_flash_node(&nand->base, nc->dev->of_node);
1787
1788 return atmel_nand_controller_add_nand(nc, nand);
1789}
1790
1791static int atmel_nand_controller_add_nands(struct atmel_nand_controller *nc)
1792{
1793 struct device_node *np, *nand_np;
1794 struct device *dev = nc->dev;
1795 int ret, reg_cells;
1796 u32 val;
1797
1798 /* We do not retrieve the SMC syscon when parsing old DTs. */
1799 if (nc->caps->legacy_of_bindings)
1800 return atmel_nand_controller_legacy_add_nands(nc);
1801
1802 np = dev->of_node;
1803
1804 ret = of_property_read_u32(np, "#address-cells", &val);
1805 if (ret) {
1806 dev_err(dev, "missing #address-cells property\n");
1807 return ret;
1808 }
1809
1810 reg_cells = val;
1811
1812 ret = of_property_read_u32(np, "#size-cells", &val);
1813 if (ret) {
1814 dev_err(dev, "missing #address-cells property\n");
1815 return ret;
1816 }
1817
1818 reg_cells += val;
1819
1820 for_each_child_of_node(np, nand_np) {
1821 struct atmel_nand *nand;
1822
1823 nand = atmel_nand_create(nc, nand_np, reg_cells);
1824 if (IS_ERR(nand)) {
1825 ret = PTR_ERR(nand);
1826 goto err;
1827 }
1828
1829 ret = atmel_nand_controller_add_nand(nc, nand);
1830 if (ret)
1831 goto err;
1832 }
1833
1834 return 0;
1835
1836err:
1837 atmel_nand_controller_remove_nands(nc);
1838
1839 return ret;
1840}
1841
1842static void atmel_nand_controller_cleanup(struct atmel_nand_controller *nc)
1843{
1844 if (nc->dmac)
1845 dma_release_channel(nc->dmac);
1846
1847 clk_put(nc->mck);
1848}
1849
1850static const struct of_device_id atmel_matrix_of_ids[] = {
1851 {
1852 .compatible = "atmel,at91sam9260-matrix",
1853 .data = (void *)AT91SAM9260_MATRIX_EBICSA,
1854 },
1855 {
1856 .compatible = "atmel,at91sam9261-matrix",
1857 .data = (void *)AT91SAM9261_MATRIX_EBICSA,
1858 },
1859 {
1860 .compatible = "atmel,at91sam9263-matrix",
1861 .data = (void *)AT91SAM9263_MATRIX_EBI0CSA,
1862 },
1863 {
1864 .compatible = "atmel,at91sam9rl-matrix",
1865 .data = (void *)AT91SAM9RL_MATRIX_EBICSA,
1866 },
1867 {
1868 .compatible = "atmel,at91sam9g45-matrix",
1869 .data = (void *)AT91SAM9G45_MATRIX_EBICSA,
1870 },
1871 {
1872 .compatible = "atmel,at91sam9n12-matrix",
1873 .data = (void *)AT91SAM9N12_MATRIX_EBICSA,
1874 },
1875 {
1876 .compatible = "atmel,at91sam9x5-matrix",
1877 .data = (void *)AT91SAM9X5_MATRIX_EBICSA,
1878 },
Christophe Jaillet038e8ad6e2017-04-11 07:22:52 +02001879 { /* sentinel */ },
Boris Brezillonf88fc122017-03-16 09:02:40 +01001880};
1881
Miquel Raynal577e0102018-07-25 15:31:41 +02001882static int atmel_nand_attach_chip(struct nand_chip *chip)
1883{
1884 struct atmel_nand_controller *nc = to_nand_controller(chip->controller);
1885 struct atmel_nand *nand = to_atmel_nand(chip);
1886 struct mtd_info *mtd = nand_to_mtd(chip);
1887 int ret;
1888
1889 ret = nc->caps->ops->ecc_init(chip);
1890 if (ret)
1891 return ret;
1892
1893 if (nc->caps->legacy_of_bindings || !nc->dev->of_node) {
1894 /*
1895 * We keep the MTD name unchanged to avoid breaking platforms
1896 * where the MTD cmdline parser is used and the bootloader
1897 * has not been updated to use the new naming scheme.
1898 */
1899 mtd->name = "atmel_nand";
1900 } else if (!mtd->name) {
1901 /*
1902 * If the new bindings are used and the bootloader has not been
1903 * updated to pass a new mtdparts parameter on the cmdline, you
1904 * should define the following property in your nand node:
1905 *
1906 * label = "atmel_nand";
1907 *
1908 * This way, mtd->name will be set by the core when
1909 * nand_set_flash_node() is called.
1910 */
1911 mtd->name = devm_kasprintf(nc->dev, GFP_KERNEL,
1912 "%s:nand.%d", dev_name(nc->dev),
1913 nand->cs[0].id);
1914 if (!mtd->name) {
1915 dev_err(nc->dev, "Failed to allocate mtd->name\n");
1916 return -ENOMEM;
1917 }
1918 }
1919
1920 return 0;
1921}
1922
1923static const struct nand_controller_ops atmel_nand_controller_ops = {
1924 .attach_chip = atmel_nand_attach_chip,
1925};
1926
Boris Brezillonf88fc122017-03-16 09:02:40 +01001927static int atmel_nand_controller_init(struct atmel_nand_controller *nc,
1928 struct platform_device *pdev,
1929 const struct atmel_nand_controller_caps *caps)
1930{
1931 struct device *dev = &pdev->dev;
1932 struct device_node *np = dev->of_node;
1933 int ret;
1934
Miquel Raynal7da45132018-07-17 09:08:02 +02001935 nand_controller_init(&nc->base);
Miquel Raynal577e0102018-07-25 15:31:41 +02001936 nc->base.ops = &atmel_nand_controller_ops;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001937 INIT_LIST_HEAD(&nc->chips);
1938 nc->dev = dev;
1939 nc->caps = caps;
1940
1941 platform_set_drvdata(pdev, nc);
1942
1943 nc->pmecc = devm_atmel_pmecc_get(dev);
1944 if (IS_ERR(nc->pmecc)) {
1945 ret = PTR_ERR(nc->pmecc);
1946 if (ret != -EPROBE_DEFER)
1947 dev_err(dev, "Could not get PMECC object (err = %d)\n",
1948 ret);
1949 return ret;
1950 }
1951
Peter Rosinefc63622018-03-29 15:10:54 +02001952 if (nc->caps->has_dma && !atmel_nand_avoid_dma) {
Boris Brezillonf88fc122017-03-16 09:02:40 +01001953 dma_cap_mask_t mask;
1954
1955 dma_cap_zero(mask);
1956 dma_cap_set(DMA_MEMCPY, mask);
1957
1958 nc->dmac = dma_request_channel(mask, NULL, NULL);
1959 if (!nc->dmac)
1960 dev_err(nc->dev, "Failed to request DMA channel\n");
1961 }
1962
1963 /* We do not retrieve the SMC syscon when parsing old DTs. */
1964 if (nc->caps->legacy_of_bindings)
1965 return 0;
1966
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001967 nc->mck = of_clk_get(dev->parent->of_node, 0);
1968 if (IS_ERR(nc->mck)) {
1969 dev_err(dev, "Failed to retrieve MCK clk\n");
1970 return PTR_ERR(nc->mck);
1971 }
1972
Boris Brezillonf88fc122017-03-16 09:02:40 +01001973 np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
1974 if (!np) {
1975 dev_err(dev, "Missing or invalid atmel,smc property\n");
1976 return -EINVAL;
1977 }
1978
1979 nc->smc = syscon_node_to_regmap(np);
1980 of_node_put(np);
1981 if (IS_ERR(nc->smc)) {
Dan Carpenter70106dd2017-04-04 11:15:46 +03001982 ret = PTR_ERR(nc->smc);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001983 dev_err(dev, "Could not get SMC regmap (err = %d)\n", ret);
1984 return ret;
1985 }
1986
1987 return 0;
1988}
1989
1990static int
1991atmel_smc_nand_controller_init(struct atmel_smc_nand_controller *nc)
1992{
1993 struct device *dev = nc->base.dev;
1994 const struct of_device_id *match;
1995 struct device_node *np;
1996 int ret;
1997
1998 /* We do not retrieve the matrix syscon when parsing old DTs. */
1999 if (nc->base.caps->legacy_of_bindings)
2000 return 0;
2001
2002 np = of_parse_phandle(dev->parent->of_node, "atmel,matrix", 0);
2003 if (!np)
2004 return 0;
2005
2006 match = of_match_node(atmel_matrix_of_ids, np);
2007 if (!match) {
2008 of_node_put(np);
2009 return 0;
2010 }
2011
2012 nc->matrix = syscon_node_to_regmap(np);
2013 of_node_put(np);
2014 if (IS_ERR(nc->matrix)) {
Dan Carpenter70106dd2017-04-04 11:15:46 +03002015 ret = PTR_ERR(nc->matrix);
Boris Brezillonf88fc122017-03-16 09:02:40 +01002016 dev_err(dev, "Could not get Matrix regmap (err = %d)\n", ret);
2017 return ret;
2018 }
2019
Boris Brezillone6848512018-07-09 22:09:22 +02002020 nc->ebi_csa_offs = (uintptr_t)match->data;
Boris Brezillonf88fc122017-03-16 09:02:40 +01002021
2022 /*
2023 * The at91sam9263 has 2 EBIs, if the NAND controller is under EBI1
2024 * add 4 to ->ebi_csa_offs.
2025 */
2026 if (of_device_is_compatible(dev->parent->of_node,
2027 "atmel,at91sam9263-ebi1"))
2028 nc->ebi_csa_offs += 4;
2029
2030 return 0;
2031}
2032
2033static int
2034atmel_hsmc_nand_controller_legacy_init(struct atmel_hsmc_nand_controller *nc)
2035{
2036 struct regmap_config regmap_conf = {
2037 .reg_bits = 32,
2038 .val_bits = 32,
2039 .reg_stride = 4,
2040 };
2041
2042 struct device *dev = nc->base.dev;
2043 struct device_node *nand_np, *nfc_np;
2044 void __iomem *iomem;
2045 struct resource res;
2046 int ret;
2047
2048 nand_np = dev->of_node;
2049 nfc_np = of_find_compatible_node(dev->of_node, NULL,
2050 "atmel,sama5d3-nfc");
2051
2052 nc->clk = of_clk_get(nfc_np, 0);
2053 if (IS_ERR(nc->clk)) {
2054 ret = PTR_ERR(nc->clk);
2055 dev_err(dev, "Failed to retrieve HSMC clock (err = %d)\n",
2056 ret);
2057 goto out;
2058 }
2059
2060 ret = clk_prepare_enable(nc->clk);
2061 if (ret) {
2062 dev_err(dev, "Failed to enable the HSMC clock (err = %d)\n",
2063 ret);
2064 goto out;
2065 }
2066
2067 nc->irq = of_irq_get(nand_np, 0);
Sergei Shtylyov892dd182017-08-06 00:14:28 +03002068 if (nc->irq <= 0) {
2069 ret = nc->irq ?: -ENXIO;
Boris Brezillonf88fc122017-03-16 09:02:40 +01002070 if (ret != -EPROBE_DEFER)
2071 dev_err(dev, "Failed to get IRQ number (err = %d)\n",
2072 ret);
2073 goto out;
2074 }
2075
2076 ret = of_address_to_resource(nfc_np, 0, &res);
2077 if (ret) {
2078 dev_err(dev, "Invalid or missing NFC IO resource (err = %d)\n",
2079 ret);
2080 goto out;
2081 }
2082
2083 iomem = devm_ioremap_resource(dev, &res);
2084 if (IS_ERR(iomem)) {
2085 ret = PTR_ERR(iomem);
2086 goto out;
2087 }
2088
2089 regmap_conf.name = "nfc-io";
2090 regmap_conf.max_register = resource_size(&res) - 4;
2091 nc->io = devm_regmap_init_mmio(dev, iomem, &regmap_conf);
2092 if (IS_ERR(nc->io)) {
2093 ret = PTR_ERR(nc->io);
2094 dev_err(dev, "Could not create NFC IO regmap (err = %d)\n",
2095 ret);
2096 goto out;
2097 }
2098
2099 ret = of_address_to_resource(nfc_np, 1, &res);
2100 if (ret) {
2101 dev_err(dev, "Invalid or missing HSMC resource (err = %d)\n",
2102 ret);
2103 goto out;
2104 }
2105
2106 iomem = devm_ioremap_resource(dev, &res);
2107 if (IS_ERR(iomem)) {
2108 ret = PTR_ERR(iomem);
2109 goto out;
2110 }
2111
2112 regmap_conf.name = "smc";
2113 regmap_conf.max_register = resource_size(&res) - 4;
2114 nc->base.smc = devm_regmap_init_mmio(dev, iomem, &regmap_conf);
2115 if (IS_ERR(nc->base.smc)) {
2116 ret = PTR_ERR(nc->base.smc);
2117 dev_err(dev, "Could not create NFC IO regmap (err = %d)\n",
2118 ret);
2119 goto out;
2120 }
2121
2122 ret = of_address_to_resource(nfc_np, 2, &res);
2123 if (ret) {
2124 dev_err(dev, "Invalid or missing SRAM resource (err = %d)\n",
2125 ret);
2126 goto out;
2127 }
2128
2129 nc->sram.virt = devm_ioremap_resource(dev, &res);
2130 if (IS_ERR(nc->sram.virt)) {
2131 ret = PTR_ERR(nc->sram.virt);
2132 goto out;
2133 }
2134
2135 nc->sram.dma = res.start;
2136
2137out:
2138 of_node_put(nfc_np);
2139
2140 return ret;
2141}
2142
2143static int
2144atmel_hsmc_nand_controller_init(struct atmel_hsmc_nand_controller *nc)
2145{
2146 struct device *dev = nc->base.dev;
2147 struct device_node *np;
2148 int ret;
2149
2150 np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
2151 if (!np) {
2152 dev_err(dev, "Missing or invalid atmel,smc property\n");
2153 return -EINVAL;
2154 }
2155
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02002156 nc->hsmc_layout = atmel_hsmc_get_reg_layout(np);
2157
Boris Brezillonf88fc122017-03-16 09:02:40 +01002158 nc->irq = of_irq_get(np, 0);
2159 of_node_put(np);
Sergei Shtylyov892dd182017-08-06 00:14:28 +03002160 if (nc->irq <= 0) {
2161 ret = nc->irq ?: -ENXIO;
2162 if (ret != -EPROBE_DEFER)
Boris Brezillonf88fc122017-03-16 09:02:40 +01002163 dev_err(dev, "Failed to get IRQ number (err = %d)\n",
Sergei Shtylyov892dd182017-08-06 00:14:28 +03002164 ret);
2165 return ret;
Boris Brezillonf88fc122017-03-16 09:02:40 +01002166 }
2167
2168 np = of_parse_phandle(dev->of_node, "atmel,nfc-io", 0);
2169 if (!np) {
2170 dev_err(dev, "Missing or invalid atmel,nfc-io property\n");
2171 return -EINVAL;
2172 }
2173
2174 nc->io = syscon_node_to_regmap(np);
2175 of_node_put(np);
2176 if (IS_ERR(nc->io)) {
2177 ret = PTR_ERR(nc->io);
2178 dev_err(dev, "Could not get NFC IO regmap (err = %d)\n", ret);
2179 return ret;
2180 }
2181
2182 nc->sram.pool = of_gen_pool_get(nc->base.dev->of_node,
2183 "atmel,nfc-sram", 0);
2184 if (!nc->sram.pool) {
2185 dev_err(nc->base.dev, "Missing SRAM\n");
2186 return -ENOMEM;
2187 }
2188
Boris Brezillond28395c2018-07-09 22:09:23 +02002189 nc->sram.virt = (void __iomem *)gen_pool_dma_alloc(nc->sram.pool,
2190 ATMEL_NFC_SRAM_SIZE,
2191 &nc->sram.dma);
Boris Brezillonf88fc122017-03-16 09:02:40 +01002192 if (!nc->sram.virt) {
2193 dev_err(nc->base.dev,
2194 "Could not allocate memory from the NFC SRAM pool\n");
2195 return -ENOMEM;
2196 }
2197
2198 return 0;
2199}
2200
2201static int
2202atmel_hsmc_nand_controller_remove(struct atmel_nand_controller *nc)
2203{
2204 struct atmel_hsmc_nand_controller *hsmc_nc;
2205 int ret;
2206
2207 ret = atmel_nand_controller_remove_nands(nc);
2208 if (ret)
2209 return ret;
2210
2211 hsmc_nc = container_of(nc, struct atmel_hsmc_nand_controller, base);
2212 if (hsmc_nc->sram.pool)
2213 gen_pool_free(hsmc_nc->sram.pool,
2214 (unsigned long)hsmc_nc->sram.virt,
2215 ATMEL_NFC_SRAM_SIZE);
2216
2217 if (hsmc_nc->clk) {
2218 clk_disable_unprepare(hsmc_nc->clk);
2219 clk_put(hsmc_nc->clk);
2220 }
2221
2222 atmel_nand_controller_cleanup(nc);
2223
2224 return 0;
2225}
2226
2227static int atmel_hsmc_nand_controller_probe(struct platform_device *pdev,
2228 const struct atmel_nand_controller_caps *caps)
2229{
2230 struct device *dev = &pdev->dev;
2231 struct atmel_hsmc_nand_controller *nc;
2232 int ret;
2233
2234 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2235 if (!nc)
2236 return -ENOMEM;
2237
2238 ret = atmel_nand_controller_init(&nc->base, pdev, caps);
2239 if (ret)
2240 return ret;
2241
2242 if (caps->legacy_of_bindings)
2243 ret = atmel_hsmc_nand_controller_legacy_init(nc);
2244 else
2245 ret = atmel_hsmc_nand_controller_init(nc);
2246
2247 if (ret)
2248 return ret;
2249
2250 /* Make sure all irqs are masked before registering our IRQ handler. */
2251 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, 0xffffffff);
2252 ret = devm_request_irq(dev, nc->irq, atmel_nfc_interrupt,
2253 IRQF_SHARED, "nfc", nc);
2254 if (ret) {
2255 dev_err(dev,
2256 "Could not get register NFC interrupt handler (err = %d)\n",
2257 ret);
2258 goto err;
2259 }
2260
2261 /* Initial NFC configuration. */
2262 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CFG,
2263 ATMEL_HSMC_NFC_CFG_DTO_MAX);
2264
2265 ret = atmel_nand_controller_add_nands(&nc->base);
2266 if (ret)
2267 goto err;
2268
2269 return 0;
2270
2271err:
2272 atmel_hsmc_nand_controller_remove(&nc->base);
2273
2274 return ret;
2275}
2276
2277static const struct atmel_nand_controller_ops atmel_hsmc_nc_ops = {
2278 .probe = atmel_hsmc_nand_controller_probe,
2279 .remove = atmel_hsmc_nand_controller_remove,
2280 .ecc_init = atmel_hsmc_nand_ecc_init,
2281 .nand_init = atmel_hsmc_nand_init,
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002282 .setup_data_interface = atmel_hsmc_nand_setup_data_interface,
Boris Brezillonf88fc122017-03-16 09:02:40 +01002283};
2284
2285static const struct atmel_nand_controller_caps atmel_sama5_nc_caps = {
2286 .has_dma = true,
2287 .ale_offs = BIT(21),
2288 .cle_offs = BIT(22),
2289 .ops = &atmel_hsmc_nc_ops,
2290};
2291
2292/* Only used to parse old bindings. */
2293static const struct atmel_nand_controller_caps atmel_sama5_nand_caps = {
2294 .has_dma = true,
2295 .ale_offs = BIT(21),
2296 .cle_offs = BIT(22),
2297 .ops = &atmel_hsmc_nc_ops,
2298 .legacy_of_bindings = true,
2299};
2300
2301static int atmel_smc_nand_controller_probe(struct platform_device *pdev,
2302 const struct atmel_nand_controller_caps *caps)
2303{
2304 struct device *dev = &pdev->dev;
2305 struct atmel_smc_nand_controller *nc;
2306 int ret;
2307
2308 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2309 if (!nc)
2310 return -ENOMEM;
2311
2312 ret = atmel_nand_controller_init(&nc->base, pdev, caps);
2313 if (ret)
2314 return ret;
2315
2316 ret = atmel_smc_nand_controller_init(nc);
2317 if (ret)
2318 return ret;
2319
2320 return atmel_nand_controller_add_nands(&nc->base);
2321}
2322
2323static int
2324atmel_smc_nand_controller_remove(struct atmel_nand_controller *nc)
2325{
2326 int ret;
2327
2328 ret = atmel_nand_controller_remove_nands(nc);
2329 if (ret)
2330 return ret;
2331
2332 atmel_nand_controller_cleanup(nc);
2333
2334 return 0;
2335}
2336
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002337/*
2338 * The SMC reg layout of at91rm9200 is completely different which prevents us
2339 * from re-using atmel_smc_nand_setup_data_interface() for the
2340 * ->setup_data_interface() hook.
2341 * At this point, there's no support for the at91rm9200 SMC IP, so we leave
2342 * ->setup_data_interface() unassigned.
2343 */
2344static const struct atmel_nand_controller_ops at91rm9200_nc_ops = {
Boris Brezillonf88fc122017-03-16 09:02:40 +01002345 .probe = atmel_smc_nand_controller_probe,
2346 .remove = atmel_smc_nand_controller_remove,
2347 .ecc_init = atmel_nand_ecc_init,
2348 .nand_init = atmel_smc_nand_init,
2349};
2350
2351static const struct atmel_nand_controller_caps atmel_rm9200_nc_caps = {
2352 .ale_offs = BIT(21),
2353 .cle_offs = BIT(22),
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002354 .ops = &at91rm9200_nc_ops,
2355};
2356
2357static const struct atmel_nand_controller_ops atmel_smc_nc_ops = {
2358 .probe = atmel_smc_nand_controller_probe,
2359 .remove = atmel_smc_nand_controller_remove,
2360 .ecc_init = atmel_nand_ecc_init,
2361 .nand_init = atmel_smc_nand_init,
2362 .setup_data_interface = atmel_smc_nand_setup_data_interface,
2363};
2364
2365static const struct atmel_nand_controller_caps atmel_sam9260_nc_caps = {
2366 .ale_offs = BIT(21),
2367 .cle_offs = BIT(22),
Boris Brezillonf88fc122017-03-16 09:02:40 +01002368 .ops = &atmel_smc_nc_ops,
2369};
2370
2371static const struct atmel_nand_controller_caps atmel_sam9261_nc_caps = {
2372 .ale_offs = BIT(22),
2373 .cle_offs = BIT(21),
2374 .ops = &atmel_smc_nc_ops,
2375};
2376
2377static const struct atmel_nand_controller_caps atmel_sam9g45_nc_caps = {
2378 .has_dma = true,
2379 .ale_offs = BIT(21),
2380 .cle_offs = BIT(22),
2381 .ops = &atmel_smc_nc_ops,
2382};
2383
2384/* Only used to parse old bindings. */
2385static const struct atmel_nand_controller_caps atmel_rm9200_nand_caps = {
2386 .ale_offs = BIT(21),
2387 .cle_offs = BIT(22),
2388 .ops = &atmel_smc_nc_ops,
2389 .legacy_of_bindings = true,
2390};
2391
2392static const struct atmel_nand_controller_caps atmel_sam9261_nand_caps = {
2393 .ale_offs = BIT(22),
2394 .cle_offs = BIT(21),
2395 .ops = &atmel_smc_nc_ops,
2396 .legacy_of_bindings = true,
2397};
2398
2399static const struct atmel_nand_controller_caps atmel_sam9g45_nand_caps = {
2400 .has_dma = true,
2401 .ale_offs = BIT(21),
2402 .cle_offs = BIT(22),
2403 .ops = &atmel_smc_nc_ops,
2404 .legacy_of_bindings = true,
2405};
2406
2407static const struct of_device_id atmel_nand_controller_of_ids[] = {
2408 {
2409 .compatible = "atmel,at91rm9200-nand-controller",
2410 .data = &atmel_rm9200_nc_caps,
2411 },
2412 {
2413 .compatible = "atmel,at91sam9260-nand-controller",
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002414 .data = &atmel_sam9260_nc_caps,
Boris Brezillonf88fc122017-03-16 09:02:40 +01002415 },
2416 {
2417 .compatible = "atmel,at91sam9261-nand-controller",
2418 .data = &atmel_sam9261_nc_caps,
2419 },
2420 {
2421 .compatible = "atmel,at91sam9g45-nand-controller",
2422 .data = &atmel_sam9g45_nc_caps,
2423 },
2424 {
2425 .compatible = "atmel,sama5d3-nand-controller",
2426 .data = &atmel_sama5_nc_caps,
2427 },
2428 /* Support for old/deprecated bindings: */
2429 {
2430 .compatible = "atmel,at91rm9200-nand",
2431 .data = &atmel_rm9200_nand_caps,
2432 },
2433 {
2434 .compatible = "atmel,sama5d4-nand",
2435 .data = &atmel_rm9200_nand_caps,
2436 },
2437 {
2438 .compatible = "atmel,sama5d2-nand",
2439 .data = &atmel_rm9200_nand_caps,
2440 },
2441 { /* sentinel */ },
2442};
2443MODULE_DEVICE_TABLE(of, atmel_nand_controller_of_ids);
2444
2445static int atmel_nand_controller_probe(struct platform_device *pdev)
2446{
2447 const struct atmel_nand_controller_caps *caps;
2448
2449 if (pdev->id_entry)
2450 caps = (void *)pdev->id_entry->driver_data;
2451 else
2452 caps = of_device_get_match_data(&pdev->dev);
2453
2454 if (!caps) {
2455 dev_err(&pdev->dev, "Could not retrieve NFC caps\n");
2456 return -EINVAL;
2457 }
2458
2459 if (caps->legacy_of_bindings) {
2460 u32 ale_offs = 21;
2461
2462 /*
2463 * If we are parsing legacy DT props and the DT contains a
2464 * valid NFC node, forward the request to the sama5 logic.
2465 */
2466 if (of_find_compatible_node(pdev->dev.of_node, NULL,
2467 "atmel,sama5d3-nfc"))
2468 caps = &atmel_sama5_nand_caps;
2469
2470 /*
2471 * Even if the compatible says we are dealing with an
2472 * at91rm9200 controller, the atmel,nand-has-dma specify that
2473 * this controller supports DMA, which means we are in fact
2474 * dealing with an at91sam9g45+ controller.
2475 */
2476 if (!caps->has_dma &&
2477 of_property_read_bool(pdev->dev.of_node,
2478 "atmel,nand-has-dma"))
2479 caps = &atmel_sam9g45_nand_caps;
2480
2481 /*
2482 * All SoCs except the at91sam9261 are assigning ALE to A21 and
2483 * CLE to A22. If atmel,nand-addr-offset != 21 this means we're
2484 * actually dealing with an at91sam9261 controller.
2485 */
2486 of_property_read_u32(pdev->dev.of_node,
2487 "atmel,nand-addr-offset", &ale_offs);
2488 if (ale_offs != 21)
2489 caps = &atmel_sam9261_nand_caps;
2490 }
2491
2492 return caps->ops->probe(pdev, caps);
2493}
2494
2495static int atmel_nand_controller_remove(struct platform_device *pdev)
2496{
2497 struct atmel_nand_controller *nc = platform_get_drvdata(pdev);
2498
2499 return nc->caps->ops->remove(nc);
2500}
2501
Arnd Bergmann05b6c232017-05-31 10:19:26 +02002502static __maybe_unused int atmel_nand_controller_resume(struct device *dev)
Boris Brezillon6e532af2017-03-16 09:36:00 +01002503{
2504 struct atmel_nand_controller *nc = dev_get_drvdata(dev);
2505 struct atmel_nand *nand;
2506
Romain Izard143b0ab2017-09-28 11:46:23 +02002507 if (nc->pmecc)
2508 atmel_pmecc_reset(nc->pmecc);
2509
Boris Brezillon6e532af2017-03-16 09:36:00 +01002510 list_for_each_entry(nand, &nc->chips, node) {
2511 int i;
2512
2513 for (i = 0; i < nand->numcs; i++)
2514 nand_reset(&nand->base, i);
2515 }
2516
2517 return 0;
2518}
2519
2520static SIMPLE_DEV_PM_OPS(atmel_nand_controller_pm_ops, NULL,
2521 atmel_nand_controller_resume);
2522
Boris Brezillonf88fc122017-03-16 09:02:40 +01002523static struct platform_driver atmel_nand_controller_driver = {
2524 .driver = {
2525 .name = "atmel-nand-controller",
2526 .of_match_table = of_match_ptr(atmel_nand_controller_of_ids),
Boris Brezillon1533bfa2017-10-05 18:57:24 +02002527 .pm = &atmel_nand_controller_pm_ops,
Boris Brezillonf88fc122017-03-16 09:02:40 +01002528 },
2529 .probe = atmel_nand_controller_probe,
2530 .remove = atmel_nand_controller_remove,
2531};
2532module_platform_driver(atmel_nand_controller_driver);
2533
2534MODULE_LICENSE("GPL");
2535MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
2536MODULE_DESCRIPTION("NAND Flash Controller driver for Atmel SoCs");
2537MODULE_ALIAS("platform:atmel-nand-controller");