blob: 3ebe9b727315a447f28c20bc646eb012e2f9f5be [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
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200861static int atmel_nand_pmecc_write_page(struct nand_chip *chip, const u8 *buf,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100862 int oob_required, int page)
863{
864 return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, false);
865}
866
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200867static int atmel_nand_pmecc_write_page_raw(struct nand_chip *chip,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100868 const u8 *buf, int oob_required,
869 int page)
870{
871 return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, true);
872}
873
874static int atmel_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf,
875 bool oob_required, int page, bool raw)
876{
877 struct mtd_info *mtd = nand_to_mtd(chip);
878 int ret;
879
Boris Brezillon25f815f2017-11-30 18:01:30 +0100880 nand_read_page_op(chip, page, 0, NULL, 0);
881
Boris Brezillonf88fc122017-03-16 09:02:40 +0100882 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw);
883 if (ret)
884 return ret;
885
886 atmel_nand_read_buf(mtd, buf, mtd->writesize);
887 atmel_nand_read_buf(mtd, chip->oob_poi, mtd->oobsize);
888
889 ret = atmel_nand_pmecc_correct_data(chip, buf, raw);
890
891 atmel_nand_pmecc_disable(chip, raw);
892
893 return ret;
894}
895
Boris Brezillonb9761682018-09-06 14:05:20 +0200896static int atmel_nand_pmecc_read_page(struct nand_chip *chip, u8 *buf,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100897 int oob_required, int page)
898{
899 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, false);
900}
901
Boris Brezillonb9761682018-09-06 14:05:20 +0200902static int atmel_nand_pmecc_read_page_raw(struct nand_chip *chip, u8 *buf,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100903 int oob_required, int page)
904{
905 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, true);
906}
907
908static int atmel_hsmc_nand_pmecc_write_pg(struct nand_chip *chip,
909 const u8 *buf, bool oob_required,
910 int page, bool raw)
911{
912 struct mtd_info *mtd = nand_to_mtd(chip);
913 struct atmel_nand *nand = to_atmel_nand(chip);
914 struct atmel_hsmc_nand_controller *nc;
Boris Brezillon41145642017-05-16 18:27:49 +0200915 int ret, status;
Boris Brezillonf88fc122017-03-16 09:02:40 +0100916
917 nc = to_hsmc_nand_controller(chip->controller);
918
919 atmel_nfc_copy_to_sram(chip, buf, false);
920
921 nc->op.cmds[0] = NAND_CMD_SEQIN;
922 nc->op.ncmds = 1;
923 atmel_nfc_set_op_addr(chip, page, 0x0);
924 nc->op.cs = nand->activecs->id;
925 nc->op.data = ATMEL_NFC_WRITE_DATA;
926
927 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw);
928 if (ret)
929 return ret;
930
931 ret = atmel_nfc_exec_op(nc, false);
932 if (ret) {
933 atmel_nand_pmecc_disable(chip, raw);
934 dev_err(nc->base.dev,
935 "Failed to transfer NAND page data (err = %d)\n",
936 ret);
937 return ret;
938 }
939
940 ret = atmel_nand_pmecc_generate_eccbytes(chip, raw);
941
942 atmel_nand_pmecc_disable(chip, raw);
943
944 if (ret)
945 return ret;
946
947 atmel_nand_write_buf(mtd, chip->oob_poi, mtd->oobsize);
948
949 nc->op.cmds[0] = NAND_CMD_PAGEPROG;
950 nc->op.ncmds = 1;
951 nc->op.cs = nand->activecs->id;
952 ret = atmel_nfc_exec_op(nc, false);
953 if (ret)
954 dev_err(nc->base.dev, "Failed to program NAND page (err = %d)\n",
955 ret);
956
Boris Brezillon41145642017-05-16 18:27:49 +0200957 status = chip->waitfunc(mtd, chip);
958 if (status & NAND_STATUS_FAIL)
959 return -EIO;
960
Boris Brezillonf88fc122017-03-16 09:02:40 +0100961 return ret;
962}
963
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200964static int atmel_hsmc_nand_pmecc_write_page(struct nand_chip *chip,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100965 const u8 *buf, int oob_required,
966 int page)
967{
968 return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page,
969 false);
970}
971
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200972static int atmel_hsmc_nand_pmecc_write_page_raw(struct nand_chip *chip,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100973 const u8 *buf,
974 int oob_required, int page)
975{
976 return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page,
977 true);
978}
979
980static int atmel_hsmc_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf,
981 bool oob_required, int page,
982 bool raw)
983{
984 struct mtd_info *mtd = nand_to_mtd(chip);
985 struct atmel_nand *nand = to_atmel_nand(chip);
986 struct atmel_hsmc_nand_controller *nc;
987 int ret;
988
989 nc = to_hsmc_nand_controller(chip->controller);
990
991 /*
992 * Optimized read page accessors only work when the NAND R/B pin is
993 * connected to a native SoC R/B pin. If that's not the case, fallback
994 * to the non-optimized one.
995 */
996 if (nand->activecs->rb.type != ATMEL_NAND_NATIVE_RB) {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100997 nand_read_page_op(chip, page, 0, NULL, 0);
Boris Brezillonf88fc122017-03-16 09:02:40 +0100998
999 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page,
1000 raw);
1001 }
1002
1003 nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READ0;
1004
1005 if (mtd->writesize > 512)
1006 nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READSTART;
1007
1008 atmel_nfc_set_op_addr(chip, page, 0x0);
1009 nc->op.cs = nand->activecs->id;
1010 nc->op.data = ATMEL_NFC_READ_DATA;
1011
1012 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw);
1013 if (ret)
1014 return ret;
1015
1016 ret = atmel_nfc_exec_op(nc, false);
1017 if (ret) {
1018 atmel_nand_pmecc_disable(chip, raw);
1019 dev_err(nc->base.dev,
1020 "Failed to load NAND page data (err = %d)\n",
1021 ret);
1022 return ret;
1023 }
1024
1025 atmel_nfc_copy_from_sram(chip, buf, true);
1026
1027 ret = atmel_nand_pmecc_correct_data(chip, buf, raw);
1028
1029 atmel_nand_pmecc_disable(chip, raw);
1030
1031 return ret;
1032}
1033
Boris Brezillonb9761682018-09-06 14:05:20 +02001034static int atmel_hsmc_nand_pmecc_read_page(struct nand_chip *chip, u8 *buf,
Boris Brezillonf88fc122017-03-16 09:02:40 +01001035 int oob_required, int page)
1036{
1037 return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page,
1038 false);
1039}
1040
Boris Brezillonb9761682018-09-06 14:05:20 +02001041static int atmel_hsmc_nand_pmecc_read_page_raw(struct nand_chip *chip,
Boris Brezillonf88fc122017-03-16 09:02:40 +01001042 u8 *buf, int oob_required,
1043 int page)
1044{
1045 return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page,
1046 true);
1047}
1048
1049static int atmel_nand_pmecc_init(struct nand_chip *chip)
1050{
1051 struct mtd_info *mtd = nand_to_mtd(chip);
1052 struct atmel_nand *nand = to_atmel_nand(chip);
1053 struct atmel_nand_controller *nc;
1054 struct atmel_pmecc_user_req req;
1055
1056 nc = to_nand_controller(chip->controller);
1057
1058 if (!nc->pmecc) {
1059 dev_err(nc->dev, "HW ECC not supported\n");
1060 return -ENOTSUPP;
1061 }
1062
1063 if (nc->caps->legacy_of_bindings) {
1064 u32 val;
1065
1066 if (!of_property_read_u32(nc->dev->of_node, "atmel,pmecc-cap",
1067 &val))
1068 chip->ecc.strength = val;
1069
1070 if (!of_property_read_u32(nc->dev->of_node,
1071 "atmel,pmecc-sector-size",
1072 &val))
1073 chip->ecc.size = val;
1074 }
1075
1076 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
1077 req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH;
1078 else if (chip->ecc.strength)
1079 req.ecc.strength = chip->ecc.strength;
1080 else if (chip->ecc_strength_ds)
1081 req.ecc.strength = chip->ecc_strength_ds;
1082 else
1083 req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH;
1084
1085 if (chip->ecc.size)
1086 req.ecc.sectorsize = chip->ecc.size;
1087 else if (chip->ecc_step_ds)
1088 req.ecc.sectorsize = chip->ecc_step_ds;
1089 else
1090 req.ecc.sectorsize = ATMEL_PMECC_SECTOR_SIZE_AUTO;
1091
1092 req.pagesize = mtd->writesize;
1093 req.oobsize = mtd->oobsize;
1094
1095 if (mtd->writesize <= 512) {
1096 req.ecc.bytes = 4;
1097 req.ecc.ooboffset = 0;
1098 } else {
1099 req.ecc.bytes = mtd->oobsize - 2;
1100 req.ecc.ooboffset = ATMEL_PMECC_OOBOFFSET_AUTO;
1101 }
1102
1103 nand->pmecc = atmel_pmecc_create_user(nc->pmecc, &req);
1104 if (IS_ERR(nand->pmecc))
1105 return PTR_ERR(nand->pmecc);
1106
1107 chip->ecc.algo = NAND_ECC_BCH;
1108 chip->ecc.size = req.ecc.sectorsize;
1109 chip->ecc.bytes = req.ecc.bytes / req.ecc.nsectors;
1110 chip->ecc.strength = req.ecc.strength;
1111
1112 chip->options |= NAND_NO_SUBPAGE_WRITE;
1113
1114 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
1115
1116 return 0;
1117}
1118
Miquel Raynal577e0102018-07-25 15:31:41 +02001119static int atmel_nand_ecc_init(struct nand_chip *chip)
Boris Brezillonf88fc122017-03-16 09:02:40 +01001120{
Boris Brezillonf88fc122017-03-16 09:02:40 +01001121 struct atmel_nand_controller *nc;
1122 int ret;
1123
1124 nc = to_nand_controller(chip->controller);
1125
1126 switch (chip->ecc.mode) {
1127 case NAND_ECC_NONE:
1128 case NAND_ECC_SOFT:
1129 /*
1130 * Nothing to do, the core will initialize everything for us.
1131 */
1132 break;
1133
1134 case NAND_ECC_HW:
1135 ret = atmel_nand_pmecc_init(chip);
1136 if (ret)
1137 return ret;
1138
1139 chip->ecc.read_page = atmel_nand_pmecc_read_page;
1140 chip->ecc.write_page = atmel_nand_pmecc_write_page;
1141 chip->ecc.read_page_raw = atmel_nand_pmecc_read_page_raw;
1142 chip->ecc.write_page_raw = atmel_nand_pmecc_write_page_raw;
1143 break;
1144
1145 default:
1146 /* Other modes are not supported. */
1147 dev_err(nc->dev, "Unsupported ECC mode: %d\n",
1148 chip->ecc.mode);
1149 return -ENOTSUPP;
1150 }
1151
1152 return 0;
1153}
1154
Miquel Raynal577e0102018-07-25 15:31:41 +02001155static int atmel_hsmc_nand_ecc_init(struct nand_chip *chip)
Boris Brezillonf88fc122017-03-16 09:02:40 +01001156{
Boris Brezillonf88fc122017-03-16 09:02:40 +01001157 int ret;
1158
Miquel Raynal577e0102018-07-25 15:31:41 +02001159 ret = atmel_nand_ecc_init(chip);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001160 if (ret)
1161 return ret;
1162
1163 if (chip->ecc.mode != NAND_ECC_HW)
1164 return 0;
1165
1166 /* Adjust the ECC operations for the HSMC IP. */
1167 chip->ecc.read_page = atmel_hsmc_nand_pmecc_read_page;
1168 chip->ecc.write_page = atmel_hsmc_nand_pmecc_write_page;
1169 chip->ecc.read_page_raw = atmel_hsmc_nand_pmecc_read_page_raw;
1170 chip->ecc.write_page_raw = atmel_hsmc_nand_pmecc_write_page_raw;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001171
1172 return 0;
1173}
1174
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001175static int atmel_smc_nand_prepare_smcconf(struct atmel_nand *nand,
1176 const struct nand_data_interface *conf,
1177 struct atmel_smc_cs_conf *smcconf)
1178{
1179 u32 ncycles, totalcycles, timeps, mckperiodps;
1180 struct atmel_nand_controller *nc;
1181 int ret;
1182
1183 nc = to_nand_controller(nand->base.controller);
1184
1185 /* DDR interface not supported. */
1186 if (conf->type != NAND_SDR_IFACE)
1187 return -ENOTSUPP;
1188
1189 /*
1190 * tRC < 30ns implies EDO mode. This controller does not support this
1191 * mode.
1192 */
Boris Brezillonee02f732017-07-31 10:32:21 +02001193 if (conf->timings.sdr.tRC_min < 30000)
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001194 return -ENOTSUPP;
1195
1196 atmel_smc_cs_conf_init(smcconf);
1197
1198 mckperiodps = NSEC_PER_SEC / clk_get_rate(nc->mck);
1199 mckperiodps *= 1000;
1200
1201 /*
1202 * Set write pulse timing. This one is easy to extract:
1203 *
1204 * NWE_PULSE = tWP
1205 */
1206 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWP_min, mckperiodps);
1207 totalcycles = ncycles;
1208 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NWE_SHIFT,
1209 ncycles);
1210 if (ret)
1211 return ret;
1212
1213 /*
1214 * The write setup timing depends on the operation done on the NAND.
1215 * All operations goes through the same data bus, but the operation
1216 * type depends on the address we are writing to (ALE/CLE address
1217 * lines).
1218 * Since we have no way to differentiate the different operations at
1219 * the SMC level, we must consider the worst case (the biggest setup
1220 * time among all operation types):
1221 *
1222 * NWE_SETUP = max(tCLS, tCS, tALS, tDS) - NWE_PULSE
1223 */
1224 timeps = max3(conf->timings.sdr.tCLS_min, conf->timings.sdr.tCS_min,
1225 conf->timings.sdr.tALS_min);
1226 timeps = max(timeps, conf->timings.sdr.tDS_min);
1227 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1228 ncycles = ncycles > totalcycles ? ncycles - totalcycles : 0;
1229 totalcycles += ncycles;
1230 ret = atmel_smc_cs_conf_set_setup(smcconf, ATMEL_SMC_NWE_SHIFT,
1231 ncycles);
1232 if (ret)
1233 return ret;
1234
1235 /*
1236 * As for the write setup timing, the write hold timing depends on the
1237 * operation done on the NAND:
1238 *
1239 * NWE_HOLD = max(tCLH, tCH, tALH, tDH, tWH)
1240 */
1241 timeps = max3(conf->timings.sdr.tCLH_min, conf->timings.sdr.tCH_min,
1242 conf->timings.sdr.tALH_min);
1243 timeps = max3(timeps, conf->timings.sdr.tDH_min,
1244 conf->timings.sdr.tWH_min);
1245 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1246 totalcycles += ncycles;
1247
1248 /*
1249 * The write cycle timing is directly matching tWC, but is also
1250 * dependent on the other timings on the setup and hold timings we
1251 * calculated earlier, which gives:
1252 *
1253 * NWE_CYCLE = max(tWC, NWE_SETUP + NWE_PULSE + NWE_HOLD)
1254 */
1255 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWC_min, mckperiodps);
1256 ncycles = max(totalcycles, ncycles);
1257 ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NWE_SHIFT,
1258 ncycles);
1259 if (ret)
1260 return ret;
1261
1262 /*
1263 * We don't want the CS line to be toggled between each byte/word
1264 * transfer to the NAND. The only way to guarantee that is to have the
1265 * NCS_{WR,RD}_{SETUP,HOLD} timings set to 0, which in turn means:
1266 *
1267 * NCS_WR_PULSE = NWE_CYCLE
1268 */
1269 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_WR_SHIFT,
1270 ncycles);
1271 if (ret)
1272 return ret;
1273
1274 /*
1275 * As for the write setup timing, the read hold timing depends on the
1276 * operation done on the NAND:
1277 *
1278 * NRD_HOLD = max(tREH, tRHOH)
1279 */
1280 timeps = max(conf->timings.sdr.tREH_min, conf->timings.sdr.tRHOH_min);
1281 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1282 totalcycles = ncycles;
1283
1284 /*
1285 * TDF = tRHZ - NRD_HOLD
1286 */
1287 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRHZ_max, mckperiodps);
1288 ncycles -= totalcycles;
1289
1290 /*
1291 * In ONFI 4.0 specs, tRHZ has been increased to support EDO NANDs and
1292 * we might end up with a config that does not fit in the TDF field.
1293 * Just take the max value in this case and hope that the NAND is more
1294 * tolerant than advertised.
1295 */
1296 if (ncycles > ATMEL_SMC_MODE_TDF_MAX)
1297 ncycles = ATMEL_SMC_MODE_TDF_MAX;
1298 else if (ncycles < ATMEL_SMC_MODE_TDF_MIN)
1299 ncycles = ATMEL_SMC_MODE_TDF_MIN;
1300
1301 smcconf->mode |= ATMEL_SMC_MODE_TDF(ncycles) |
1302 ATMEL_SMC_MODE_TDFMODE_OPTIMIZED;
1303
1304 /*
1305 * Read pulse timing directly matches tRP:
1306 *
1307 * NRD_PULSE = tRP
1308 */
1309 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRP_min, mckperiodps);
1310 totalcycles += ncycles;
1311 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NRD_SHIFT,
1312 ncycles);
1313 if (ret)
1314 return ret;
1315
1316 /*
1317 * The write cycle timing is directly matching tWC, but is also
1318 * dependent on the setup and hold timings we calculated earlier,
1319 * which gives:
1320 *
1321 * NRD_CYCLE = max(tRC, NRD_PULSE + NRD_HOLD)
1322 *
1323 * NRD_SETUP is always 0.
1324 */
1325 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRC_min, mckperiodps);
1326 ncycles = max(totalcycles, ncycles);
1327 ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NRD_SHIFT,
1328 ncycles);
1329 if (ret)
1330 return ret;
1331
1332 /*
1333 * We don't want the CS line to be toggled between each byte/word
1334 * transfer from the NAND. The only way to guarantee that is to have
1335 * the NCS_{WR,RD}_{SETUP,HOLD} timings set to 0, which in turn means:
1336 *
1337 * NCS_RD_PULSE = NRD_CYCLE
1338 */
1339 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_RD_SHIFT,
1340 ncycles);
1341 if (ret)
1342 return ret;
1343
1344 /* Txxx timings are directly matching tXXX ones. */
1345 ncycles = DIV_ROUND_UP(conf->timings.sdr.tCLR_min, mckperiodps);
1346 ret = atmel_smc_cs_conf_set_timing(smcconf,
1347 ATMEL_HSMC_TIMINGS_TCLR_SHIFT,
1348 ncycles);
1349 if (ret)
1350 return ret;
1351
1352 ncycles = DIV_ROUND_UP(conf->timings.sdr.tADL_min, mckperiodps);
1353 ret = atmel_smc_cs_conf_set_timing(smcconf,
1354 ATMEL_HSMC_TIMINGS_TADL_SHIFT,
1355 ncycles);
Boris Brezillonbe3e83e2017-08-23 20:45:01 +02001356 /*
1357 * Version 4 of the ONFI spec mandates that tADL be at least 400
1358 * nanoseconds, but, depending on the master clock rate, 400 ns may not
1359 * fit in the tADL field of the SMC reg. We need to relax the check and
1360 * accept the -ERANGE return code.
1361 *
1362 * Note that previous versions of the ONFI spec had a lower tADL_min
1363 * (100 or 200 ns). It's not clear why this timing constraint got
1364 * increased but it seems most NANDs are fine with values lower than
1365 * 400ns, so we should be safe.
1366 */
1367 if (ret && ret != -ERANGE)
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001368 return ret;
1369
1370 ncycles = DIV_ROUND_UP(conf->timings.sdr.tAR_min, mckperiodps);
1371 ret = atmel_smc_cs_conf_set_timing(smcconf,
1372 ATMEL_HSMC_TIMINGS_TAR_SHIFT,
1373 ncycles);
1374 if (ret)
1375 return ret;
1376
1377 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRR_min, mckperiodps);
1378 ret = atmel_smc_cs_conf_set_timing(smcconf,
1379 ATMEL_HSMC_TIMINGS_TRR_SHIFT,
1380 ncycles);
1381 if (ret)
1382 return ret;
1383
1384 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWB_max, mckperiodps);
1385 ret = atmel_smc_cs_conf_set_timing(smcconf,
1386 ATMEL_HSMC_TIMINGS_TWB_SHIFT,
1387 ncycles);
1388 if (ret)
1389 return ret;
1390
1391 /* Attach the CS line to the NFC logic. */
1392 smcconf->timings |= ATMEL_HSMC_TIMINGS_NFSEL;
1393
1394 /* Set the appropriate data bus width. */
1395 if (nand->base.options & NAND_BUSWIDTH_16)
1396 smcconf->mode |= ATMEL_SMC_MODE_DBW_16;
1397
1398 /* Operate in NRD/NWE READ/WRITEMODE. */
1399 smcconf->mode |= ATMEL_SMC_MODE_READMODE_NRD |
1400 ATMEL_SMC_MODE_WRITEMODE_NWE;
1401
1402 return 0;
1403}
1404
1405static int atmel_smc_nand_setup_data_interface(struct atmel_nand *nand,
1406 int csline,
1407 const struct nand_data_interface *conf)
1408{
1409 struct atmel_nand_controller *nc;
1410 struct atmel_smc_cs_conf smcconf;
1411 struct atmel_nand_cs *cs;
1412 int ret;
1413
1414 nc = to_nand_controller(nand->base.controller);
1415
1416 ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf);
1417 if (ret)
1418 return ret;
1419
1420 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1421 return 0;
1422
1423 cs = &nand->cs[csline];
1424 cs->smcconf = smcconf;
1425 atmel_smc_cs_conf_apply(nc->smc, cs->id, &cs->smcconf);
1426
1427 return 0;
1428}
1429
1430static int atmel_hsmc_nand_setup_data_interface(struct atmel_nand *nand,
1431 int csline,
1432 const struct nand_data_interface *conf)
1433{
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02001434 struct atmel_hsmc_nand_controller *nc;
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001435 struct atmel_smc_cs_conf smcconf;
1436 struct atmel_nand_cs *cs;
1437 int ret;
1438
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02001439 nc = to_hsmc_nand_controller(nand->base.controller);
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001440
1441 ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf);
1442 if (ret)
1443 return ret;
1444
1445 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1446 return 0;
1447
1448 cs = &nand->cs[csline];
1449 cs->smcconf = smcconf;
1450
1451 if (cs->rb.type == ATMEL_NAND_NATIVE_RB)
1452 cs->smcconf.timings |= ATMEL_HSMC_TIMINGS_RBNSEL(cs->rb.id);
1453
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02001454 atmel_hsmc_cs_conf_apply(nc->base.smc, nc->hsmc_layout, cs->id,
1455 &cs->smcconf);
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001456
1457 return 0;
1458}
1459
1460static int atmel_nand_setup_data_interface(struct mtd_info *mtd, int csline,
1461 const struct nand_data_interface *conf)
1462{
1463 struct nand_chip *chip = mtd_to_nand(mtd);
1464 struct atmel_nand *nand = to_atmel_nand(chip);
1465 struct atmel_nand_controller *nc;
1466
1467 nc = to_nand_controller(nand->base.controller);
1468
1469 if (csline >= nand->numcs ||
1470 (csline < 0 && csline != NAND_DATA_IFACE_CHECK_ONLY))
1471 return -EINVAL;
1472
1473 return nc->caps->ops->setup_data_interface(nand, csline, conf);
1474}
1475
Boris Brezillonf88fc122017-03-16 09:02:40 +01001476static void atmel_nand_init(struct atmel_nand_controller *nc,
1477 struct atmel_nand *nand)
1478{
1479 struct nand_chip *chip = &nand->base;
1480 struct mtd_info *mtd = nand_to_mtd(chip);
1481
1482 mtd->dev.parent = nc->dev;
1483 nand->base.controller = &nc->base;
1484
1485 chip->cmd_ctrl = atmel_nand_cmd_ctrl;
1486 chip->read_byte = atmel_nand_read_byte;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001487 chip->write_byte = atmel_nand_write_byte;
1488 chip->read_buf = atmel_nand_read_buf;
1489 chip->write_buf = atmel_nand_write_buf;
1490 chip->select_chip = atmel_nand_select_chip;
1491
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001492 if (nc->mck && nc->caps->ops->setup_data_interface)
1493 chip->setup_data_interface = atmel_nand_setup_data_interface;
1494
Boris Brezillonf88fc122017-03-16 09:02:40 +01001495 /* Some NANDs require a longer delay than the default one (20us). */
1496 chip->chip_delay = 40;
1497
1498 /*
1499 * Use a bounce buffer when the buffer passed by the MTD user is not
1500 * suitable for DMA.
1501 */
1502 if (nc->dmac)
1503 chip->options |= NAND_USE_BOUNCE_BUFFER;
1504
1505 /* Default to HW ECC if pmecc is available. */
1506 if (nc->pmecc)
1507 chip->ecc.mode = NAND_ECC_HW;
1508}
1509
1510static void atmel_smc_nand_init(struct atmel_nand_controller *nc,
1511 struct atmel_nand *nand)
1512{
1513 struct nand_chip *chip = &nand->base;
1514 struct atmel_smc_nand_controller *smc_nc;
1515 int i;
1516
1517 atmel_nand_init(nc, nand);
1518
1519 smc_nc = to_smc_nand_controller(chip->controller);
1520 if (!smc_nc->matrix)
1521 return;
1522
1523 /* Attach the CS to the NAND Flash logic. */
1524 for (i = 0; i < nand->numcs; i++)
1525 regmap_update_bits(smc_nc->matrix, smc_nc->ebi_csa_offs,
1526 BIT(nand->cs[i].id), BIT(nand->cs[i].id));
1527}
1528
1529static void atmel_hsmc_nand_init(struct atmel_nand_controller *nc,
1530 struct atmel_nand *nand)
1531{
1532 struct nand_chip *chip = &nand->base;
1533
1534 atmel_nand_init(nc, nand);
1535
1536 /* Overload some methods for the HSMC controller. */
1537 chip->cmd_ctrl = atmel_hsmc_nand_cmd_ctrl;
1538 chip->select_chip = atmel_hsmc_nand_select_chip;
1539}
1540
Miquel Raynal79282252018-07-25 15:31:40 +02001541static int atmel_nand_controller_remove_nand(struct atmel_nand *nand)
Boris Brezillonf88fc122017-03-16 09:02:40 +01001542{
1543 struct nand_chip *chip = &nand->base;
1544 struct mtd_info *mtd = nand_to_mtd(chip);
1545 int ret;
1546
1547 ret = mtd_device_unregister(mtd);
1548 if (ret)
1549 return ret;
1550
1551 nand_cleanup(chip);
1552 list_del(&nand->node);
1553
1554 return 0;
1555}
1556
Boris Brezillonf88fc122017-03-16 09:02:40 +01001557static struct atmel_nand *atmel_nand_create(struct atmel_nand_controller *nc,
1558 struct device_node *np,
1559 int reg_cells)
1560{
1561 struct atmel_nand *nand;
1562 struct gpio_desc *gpio;
1563 int numcs, ret, i;
1564
1565 numcs = of_property_count_elems_of_size(np, "reg",
1566 reg_cells * sizeof(u32));
1567 if (numcs < 1) {
1568 dev_err(nc->dev, "Missing or invalid reg property\n");
1569 return ERR_PTR(-EINVAL);
1570 }
1571
Gustavo A. R. Silva2f91eb62018-08-23 20:09:38 -05001572 nand = devm_kzalloc(nc->dev, struct_size(nand, cs, numcs), GFP_KERNEL);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001573 if (!nand) {
1574 dev_err(nc->dev, "Failed to allocate NAND object\n");
1575 return ERR_PTR(-ENOMEM);
1576 }
1577
1578 nand->numcs = numcs;
1579
1580 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, "det", 0,
1581 &np->fwnode, GPIOD_IN,
1582 "nand-det");
1583 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1584 dev_err(nc->dev,
1585 "Failed to get detect gpio (err = %ld)\n",
1586 PTR_ERR(gpio));
1587 return ERR_CAST(gpio);
1588 }
1589
1590 if (!IS_ERR(gpio))
1591 nand->cdgpio = gpio;
1592
1593 for (i = 0; i < numcs; i++) {
1594 struct resource res;
1595 u32 val;
1596
1597 ret = of_address_to_resource(np, 0, &res);
1598 if (ret) {
1599 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1600 ret);
1601 return ERR_PTR(ret);
1602 }
1603
1604 ret = of_property_read_u32_index(np, "reg", i * reg_cells,
1605 &val);
1606 if (ret) {
1607 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1608 ret);
1609 return ERR_PTR(ret);
1610 }
1611
1612 nand->cs[i].id = val;
1613
1614 nand->cs[i].io.dma = res.start;
1615 nand->cs[i].io.virt = devm_ioremap_resource(nc->dev, &res);
1616 if (IS_ERR(nand->cs[i].io.virt))
1617 return ERR_CAST(nand->cs[i].io.virt);
1618
1619 if (!of_property_read_u32(np, "atmel,rb", &val)) {
1620 if (val > ATMEL_NFC_MAX_RB_ID)
1621 return ERR_PTR(-EINVAL);
1622
1623 nand->cs[i].rb.type = ATMEL_NAND_NATIVE_RB;
1624 nand->cs[i].rb.id = val;
1625 } else {
1626 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev,
1627 "rb", i, &np->fwnode,
1628 GPIOD_IN, "nand-rb");
1629 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1630 dev_err(nc->dev,
1631 "Failed to get R/B gpio (err = %ld)\n",
1632 PTR_ERR(gpio));
1633 return ERR_CAST(gpio);
1634 }
1635
1636 if (!IS_ERR(gpio)) {
1637 nand->cs[i].rb.type = ATMEL_NAND_GPIO_RB;
1638 nand->cs[i].rb.gpio = gpio;
1639 }
1640 }
1641
1642 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, "cs",
1643 i, &np->fwnode,
1644 GPIOD_OUT_HIGH,
1645 "nand-cs");
1646 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1647 dev_err(nc->dev,
1648 "Failed to get CS gpio (err = %ld)\n",
1649 PTR_ERR(gpio));
1650 return ERR_CAST(gpio);
1651 }
1652
1653 if (!IS_ERR(gpio))
1654 nand->cs[i].csgpio = gpio;
1655 }
1656
1657 nand_set_flash_node(&nand->base, np);
1658
1659 return nand;
1660}
1661
1662static int
1663atmel_nand_controller_add_nand(struct atmel_nand_controller *nc,
1664 struct atmel_nand *nand)
1665{
Miquel Raynal577e0102018-07-25 15:31:41 +02001666 struct nand_chip *chip = &nand->base;
1667 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001668 int ret;
1669
1670 /* No card inserted, skip this NAND. */
1671 if (nand->cdgpio && gpiod_get_value(nand->cdgpio)) {
1672 dev_info(nc->dev, "No SmartMedia card inserted.\n");
1673 return 0;
1674 }
1675
1676 nc->caps->ops->nand_init(nc, nand);
1677
Boris Brezillon00ad3782018-09-06 14:05:14 +02001678 ret = nand_scan(chip, nand->numcs);
Miquel Raynal79282252018-07-25 15:31:40 +02001679 if (ret) {
Miquel Raynal577e0102018-07-25 15:31:41 +02001680 dev_err(nc->dev, "NAND scan failed: %d\n", ret);
Miquel Raynal79282252018-07-25 15:31:40 +02001681 return ret;
1682 }
1683
1684 ret = mtd_device_register(mtd, NULL, 0);
1685 if (ret) {
1686 dev_err(nc->dev, "Failed to register mtd device: %d\n", ret);
1687 nand_cleanup(chip);
1688 return ret;
1689 }
1690
1691 list_add_tail(&nand->node, &nc->chips);
1692
1693 return 0;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001694}
1695
1696static int
1697atmel_nand_controller_remove_nands(struct atmel_nand_controller *nc)
1698{
1699 struct atmel_nand *nand, *tmp;
1700 int ret;
1701
1702 list_for_each_entry_safe(nand, tmp, &nc->chips, node) {
Miquel Raynal79282252018-07-25 15:31:40 +02001703 ret = atmel_nand_controller_remove_nand(nand);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001704 if (ret)
1705 return ret;
1706 }
1707
1708 return 0;
1709}
1710
1711static int
1712atmel_nand_controller_legacy_add_nands(struct atmel_nand_controller *nc)
1713{
1714 struct device *dev = nc->dev;
1715 struct platform_device *pdev = to_platform_device(dev);
1716 struct atmel_nand *nand;
1717 struct gpio_desc *gpio;
1718 struct resource *res;
1719
1720 /*
1721 * Legacy bindings only allow connecting a single NAND with a unique CS
1722 * line to the controller.
1723 */
1724 nand = devm_kzalloc(nc->dev, sizeof(*nand) + sizeof(*nand->cs),
1725 GFP_KERNEL);
1726 if (!nand)
1727 return -ENOMEM;
1728
1729 nand->numcs = 1;
1730
1731 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1732 nand->cs[0].io.virt = devm_ioremap_resource(dev, res);
1733 if (IS_ERR(nand->cs[0].io.virt))
1734 return PTR_ERR(nand->cs[0].io.virt);
1735
1736 nand->cs[0].io.dma = res->start;
1737
1738 /*
1739 * The old driver was hardcoding the CS id to 3 for all sama5
1740 * controllers. Since this id is only meaningful for the sama5
1741 * controller we can safely assign this id to 3 no matter the
1742 * controller.
1743 * If one wants to connect a NAND to a different CS line, he will
1744 * have to use the new bindings.
1745 */
1746 nand->cs[0].id = 3;
1747
1748 /* R/B GPIO. */
1749 gpio = devm_gpiod_get_index_optional(dev, NULL, 0, GPIOD_IN);
1750 if (IS_ERR(gpio)) {
1751 dev_err(dev, "Failed to get R/B gpio (err = %ld)\n",
1752 PTR_ERR(gpio));
1753 return PTR_ERR(gpio);
1754 }
1755
1756 if (gpio) {
1757 nand->cs[0].rb.type = ATMEL_NAND_GPIO_RB;
1758 nand->cs[0].rb.gpio = gpio;
1759 }
1760
1761 /* CS GPIO. */
1762 gpio = devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_HIGH);
1763 if (IS_ERR(gpio)) {
1764 dev_err(dev, "Failed to get CS gpio (err = %ld)\n",
1765 PTR_ERR(gpio));
1766 return PTR_ERR(gpio);
1767 }
1768
1769 nand->cs[0].csgpio = gpio;
1770
1771 /* Card detect GPIO. */
1772 gpio = devm_gpiod_get_index_optional(nc->dev, NULL, 2, GPIOD_IN);
1773 if (IS_ERR(gpio)) {
1774 dev_err(dev,
1775 "Failed to get detect gpio (err = %ld)\n",
1776 PTR_ERR(gpio));
1777 return PTR_ERR(gpio);
1778 }
1779
1780 nand->cdgpio = gpio;
1781
1782 nand_set_flash_node(&nand->base, nc->dev->of_node);
1783
1784 return atmel_nand_controller_add_nand(nc, nand);
1785}
1786
1787static int atmel_nand_controller_add_nands(struct atmel_nand_controller *nc)
1788{
1789 struct device_node *np, *nand_np;
1790 struct device *dev = nc->dev;
1791 int ret, reg_cells;
1792 u32 val;
1793
1794 /* We do not retrieve the SMC syscon when parsing old DTs. */
1795 if (nc->caps->legacy_of_bindings)
1796 return atmel_nand_controller_legacy_add_nands(nc);
1797
1798 np = dev->of_node;
1799
1800 ret = of_property_read_u32(np, "#address-cells", &val);
1801 if (ret) {
1802 dev_err(dev, "missing #address-cells property\n");
1803 return ret;
1804 }
1805
1806 reg_cells = val;
1807
1808 ret = of_property_read_u32(np, "#size-cells", &val);
1809 if (ret) {
1810 dev_err(dev, "missing #address-cells property\n");
1811 return ret;
1812 }
1813
1814 reg_cells += val;
1815
1816 for_each_child_of_node(np, nand_np) {
1817 struct atmel_nand *nand;
1818
1819 nand = atmel_nand_create(nc, nand_np, reg_cells);
1820 if (IS_ERR(nand)) {
1821 ret = PTR_ERR(nand);
1822 goto err;
1823 }
1824
1825 ret = atmel_nand_controller_add_nand(nc, nand);
1826 if (ret)
1827 goto err;
1828 }
1829
1830 return 0;
1831
1832err:
1833 atmel_nand_controller_remove_nands(nc);
1834
1835 return ret;
1836}
1837
1838static void atmel_nand_controller_cleanup(struct atmel_nand_controller *nc)
1839{
1840 if (nc->dmac)
1841 dma_release_channel(nc->dmac);
1842
1843 clk_put(nc->mck);
1844}
1845
1846static const struct of_device_id atmel_matrix_of_ids[] = {
1847 {
1848 .compatible = "atmel,at91sam9260-matrix",
1849 .data = (void *)AT91SAM9260_MATRIX_EBICSA,
1850 },
1851 {
1852 .compatible = "atmel,at91sam9261-matrix",
1853 .data = (void *)AT91SAM9261_MATRIX_EBICSA,
1854 },
1855 {
1856 .compatible = "atmel,at91sam9263-matrix",
1857 .data = (void *)AT91SAM9263_MATRIX_EBI0CSA,
1858 },
1859 {
1860 .compatible = "atmel,at91sam9rl-matrix",
1861 .data = (void *)AT91SAM9RL_MATRIX_EBICSA,
1862 },
1863 {
1864 .compatible = "atmel,at91sam9g45-matrix",
1865 .data = (void *)AT91SAM9G45_MATRIX_EBICSA,
1866 },
1867 {
1868 .compatible = "atmel,at91sam9n12-matrix",
1869 .data = (void *)AT91SAM9N12_MATRIX_EBICSA,
1870 },
1871 {
1872 .compatible = "atmel,at91sam9x5-matrix",
1873 .data = (void *)AT91SAM9X5_MATRIX_EBICSA,
1874 },
Christophe Jaillet038e8ad6e2017-04-11 07:22:52 +02001875 { /* sentinel */ },
Boris Brezillonf88fc122017-03-16 09:02:40 +01001876};
1877
Miquel Raynal577e0102018-07-25 15:31:41 +02001878static int atmel_nand_attach_chip(struct nand_chip *chip)
1879{
1880 struct atmel_nand_controller *nc = to_nand_controller(chip->controller);
1881 struct atmel_nand *nand = to_atmel_nand(chip);
1882 struct mtd_info *mtd = nand_to_mtd(chip);
1883 int ret;
1884
1885 ret = nc->caps->ops->ecc_init(chip);
1886 if (ret)
1887 return ret;
1888
1889 if (nc->caps->legacy_of_bindings || !nc->dev->of_node) {
1890 /*
1891 * We keep the MTD name unchanged to avoid breaking platforms
1892 * where the MTD cmdline parser is used and the bootloader
1893 * has not been updated to use the new naming scheme.
1894 */
1895 mtd->name = "atmel_nand";
1896 } else if (!mtd->name) {
1897 /*
1898 * If the new bindings are used and the bootloader has not been
1899 * updated to pass a new mtdparts parameter on the cmdline, you
1900 * should define the following property in your nand node:
1901 *
1902 * label = "atmel_nand";
1903 *
1904 * This way, mtd->name will be set by the core when
1905 * nand_set_flash_node() is called.
1906 */
1907 mtd->name = devm_kasprintf(nc->dev, GFP_KERNEL,
1908 "%s:nand.%d", dev_name(nc->dev),
1909 nand->cs[0].id);
1910 if (!mtd->name) {
1911 dev_err(nc->dev, "Failed to allocate mtd->name\n");
1912 return -ENOMEM;
1913 }
1914 }
1915
1916 return 0;
1917}
1918
1919static const struct nand_controller_ops atmel_nand_controller_ops = {
1920 .attach_chip = atmel_nand_attach_chip,
1921};
1922
Boris Brezillonf88fc122017-03-16 09:02:40 +01001923static int atmel_nand_controller_init(struct atmel_nand_controller *nc,
1924 struct platform_device *pdev,
1925 const struct atmel_nand_controller_caps *caps)
1926{
1927 struct device *dev = &pdev->dev;
1928 struct device_node *np = dev->of_node;
1929 int ret;
1930
Miquel Raynal7da45132018-07-17 09:08:02 +02001931 nand_controller_init(&nc->base);
Miquel Raynal577e0102018-07-25 15:31:41 +02001932 nc->base.ops = &atmel_nand_controller_ops;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001933 INIT_LIST_HEAD(&nc->chips);
1934 nc->dev = dev;
1935 nc->caps = caps;
1936
1937 platform_set_drvdata(pdev, nc);
1938
1939 nc->pmecc = devm_atmel_pmecc_get(dev);
1940 if (IS_ERR(nc->pmecc)) {
1941 ret = PTR_ERR(nc->pmecc);
1942 if (ret != -EPROBE_DEFER)
1943 dev_err(dev, "Could not get PMECC object (err = %d)\n",
1944 ret);
1945 return ret;
1946 }
1947
Peter Rosinefc63622018-03-29 15:10:54 +02001948 if (nc->caps->has_dma && !atmel_nand_avoid_dma) {
Boris Brezillonf88fc122017-03-16 09:02:40 +01001949 dma_cap_mask_t mask;
1950
1951 dma_cap_zero(mask);
1952 dma_cap_set(DMA_MEMCPY, mask);
1953
1954 nc->dmac = dma_request_channel(mask, NULL, NULL);
1955 if (!nc->dmac)
1956 dev_err(nc->dev, "Failed to request DMA channel\n");
1957 }
1958
1959 /* We do not retrieve the SMC syscon when parsing old DTs. */
1960 if (nc->caps->legacy_of_bindings)
1961 return 0;
1962
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001963 nc->mck = of_clk_get(dev->parent->of_node, 0);
1964 if (IS_ERR(nc->mck)) {
1965 dev_err(dev, "Failed to retrieve MCK clk\n");
1966 return PTR_ERR(nc->mck);
1967 }
1968
Boris Brezillonf88fc122017-03-16 09:02:40 +01001969 np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
1970 if (!np) {
1971 dev_err(dev, "Missing or invalid atmel,smc property\n");
1972 return -EINVAL;
1973 }
1974
1975 nc->smc = syscon_node_to_regmap(np);
1976 of_node_put(np);
1977 if (IS_ERR(nc->smc)) {
Dan Carpenter70106dd2017-04-04 11:15:46 +03001978 ret = PTR_ERR(nc->smc);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001979 dev_err(dev, "Could not get SMC regmap (err = %d)\n", ret);
1980 return ret;
1981 }
1982
1983 return 0;
1984}
1985
1986static int
1987atmel_smc_nand_controller_init(struct atmel_smc_nand_controller *nc)
1988{
1989 struct device *dev = nc->base.dev;
1990 const struct of_device_id *match;
1991 struct device_node *np;
1992 int ret;
1993
1994 /* We do not retrieve the matrix syscon when parsing old DTs. */
1995 if (nc->base.caps->legacy_of_bindings)
1996 return 0;
1997
1998 np = of_parse_phandle(dev->parent->of_node, "atmel,matrix", 0);
1999 if (!np)
2000 return 0;
2001
2002 match = of_match_node(atmel_matrix_of_ids, np);
2003 if (!match) {
2004 of_node_put(np);
2005 return 0;
2006 }
2007
2008 nc->matrix = syscon_node_to_regmap(np);
2009 of_node_put(np);
2010 if (IS_ERR(nc->matrix)) {
Dan Carpenter70106dd2017-04-04 11:15:46 +03002011 ret = PTR_ERR(nc->matrix);
Boris Brezillonf88fc122017-03-16 09:02:40 +01002012 dev_err(dev, "Could not get Matrix regmap (err = %d)\n", ret);
2013 return ret;
2014 }
2015
Boris Brezillone6848512018-07-09 22:09:22 +02002016 nc->ebi_csa_offs = (uintptr_t)match->data;
Boris Brezillonf88fc122017-03-16 09:02:40 +01002017
2018 /*
2019 * The at91sam9263 has 2 EBIs, if the NAND controller is under EBI1
2020 * add 4 to ->ebi_csa_offs.
2021 */
2022 if (of_device_is_compatible(dev->parent->of_node,
2023 "atmel,at91sam9263-ebi1"))
2024 nc->ebi_csa_offs += 4;
2025
2026 return 0;
2027}
2028
2029static int
2030atmel_hsmc_nand_controller_legacy_init(struct atmel_hsmc_nand_controller *nc)
2031{
2032 struct regmap_config regmap_conf = {
2033 .reg_bits = 32,
2034 .val_bits = 32,
2035 .reg_stride = 4,
2036 };
2037
2038 struct device *dev = nc->base.dev;
2039 struct device_node *nand_np, *nfc_np;
2040 void __iomem *iomem;
2041 struct resource res;
2042 int ret;
2043
2044 nand_np = dev->of_node;
2045 nfc_np = of_find_compatible_node(dev->of_node, NULL,
2046 "atmel,sama5d3-nfc");
2047
2048 nc->clk = of_clk_get(nfc_np, 0);
2049 if (IS_ERR(nc->clk)) {
2050 ret = PTR_ERR(nc->clk);
2051 dev_err(dev, "Failed to retrieve HSMC clock (err = %d)\n",
2052 ret);
2053 goto out;
2054 }
2055
2056 ret = clk_prepare_enable(nc->clk);
2057 if (ret) {
2058 dev_err(dev, "Failed to enable the HSMC clock (err = %d)\n",
2059 ret);
2060 goto out;
2061 }
2062
2063 nc->irq = of_irq_get(nand_np, 0);
Sergei Shtylyov892dd182017-08-06 00:14:28 +03002064 if (nc->irq <= 0) {
2065 ret = nc->irq ?: -ENXIO;
Boris Brezillonf88fc122017-03-16 09:02:40 +01002066 if (ret != -EPROBE_DEFER)
2067 dev_err(dev, "Failed to get IRQ number (err = %d)\n",
2068 ret);
2069 goto out;
2070 }
2071
2072 ret = of_address_to_resource(nfc_np, 0, &res);
2073 if (ret) {
2074 dev_err(dev, "Invalid or missing NFC IO resource (err = %d)\n",
2075 ret);
2076 goto out;
2077 }
2078
2079 iomem = devm_ioremap_resource(dev, &res);
2080 if (IS_ERR(iomem)) {
2081 ret = PTR_ERR(iomem);
2082 goto out;
2083 }
2084
2085 regmap_conf.name = "nfc-io";
2086 regmap_conf.max_register = resource_size(&res) - 4;
2087 nc->io = devm_regmap_init_mmio(dev, iomem, &regmap_conf);
2088 if (IS_ERR(nc->io)) {
2089 ret = PTR_ERR(nc->io);
2090 dev_err(dev, "Could not create NFC IO regmap (err = %d)\n",
2091 ret);
2092 goto out;
2093 }
2094
2095 ret = of_address_to_resource(nfc_np, 1, &res);
2096 if (ret) {
2097 dev_err(dev, "Invalid or missing HSMC resource (err = %d)\n",
2098 ret);
2099 goto out;
2100 }
2101
2102 iomem = devm_ioremap_resource(dev, &res);
2103 if (IS_ERR(iomem)) {
2104 ret = PTR_ERR(iomem);
2105 goto out;
2106 }
2107
2108 regmap_conf.name = "smc";
2109 regmap_conf.max_register = resource_size(&res) - 4;
2110 nc->base.smc = devm_regmap_init_mmio(dev, iomem, &regmap_conf);
2111 if (IS_ERR(nc->base.smc)) {
2112 ret = PTR_ERR(nc->base.smc);
2113 dev_err(dev, "Could not create NFC IO regmap (err = %d)\n",
2114 ret);
2115 goto out;
2116 }
2117
2118 ret = of_address_to_resource(nfc_np, 2, &res);
2119 if (ret) {
2120 dev_err(dev, "Invalid or missing SRAM resource (err = %d)\n",
2121 ret);
2122 goto out;
2123 }
2124
2125 nc->sram.virt = devm_ioremap_resource(dev, &res);
2126 if (IS_ERR(nc->sram.virt)) {
2127 ret = PTR_ERR(nc->sram.virt);
2128 goto out;
2129 }
2130
2131 nc->sram.dma = res.start;
2132
2133out:
2134 of_node_put(nfc_np);
2135
2136 return ret;
2137}
2138
2139static int
2140atmel_hsmc_nand_controller_init(struct atmel_hsmc_nand_controller *nc)
2141{
2142 struct device *dev = nc->base.dev;
2143 struct device_node *np;
2144 int ret;
2145
2146 np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
2147 if (!np) {
2148 dev_err(dev, "Missing or invalid atmel,smc property\n");
2149 return -EINVAL;
2150 }
2151
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02002152 nc->hsmc_layout = atmel_hsmc_get_reg_layout(np);
2153
Boris Brezillonf88fc122017-03-16 09:02:40 +01002154 nc->irq = of_irq_get(np, 0);
2155 of_node_put(np);
Sergei Shtylyov892dd182017-08-06 00:14:28 +03002156 if (nc->irq <= 0) {
2157 ret = nc->irq ?: -ENXIO;
2158 if (ret != -EPROBE_DEFER)
Boris Brezillonf88fc122017-03-16 09:02:40 +01002159 dev_err(dev, "Failed to get IRQ number (err = %d)\n",
Sergei Shtylyov892dd182017-08-06 00:14:28 +03002160 ret);
2161 return ret;
Boris Brezillonf88fc122017-03-16 09:02:40 +01002162 }
2163
2164 np = of_parse_phandle(dev->of_node, "atmel,nfc-io", 0);
2165 if (!np) {
2166 dev_err(dev, "Missing or invalid atmel,nfc-io property\n");
2167 return -EINVAL;
2168 }
2169
2170 nc->io = syscon_node_to_regmap(np);
2171 of_node_put(np);
2172 if (IS_ERR(nc->io)) {
2173 ret = PTR_ERR(nc->io);
2174 dev_err(dev, "Could not get NFC IO regmap (err = %d)\n", ret);
2175 return ret;
2176 }
2177
2178 nc->sram.pool = of_gen_pool_get(nc->base.dev->of_node,
2179 "atmel,nfc-sram", 0);
2180 if (!nc->sram.pool) {
2181 dev_err(nc->base.dev, "Missing SRAM\n");
2182 return -ENOMEM;
2183 }
2184
Boris Brezillond28395c2018-07-09 22:09:23 +02002185 nc->sram.virt = (void __iomem *)gen_pool_dma_alloc(nc->sram.pool,
2186 ATMEL_NFC_SRAM_SIZE,
2187 &nc->sram.dma);
Boris Brezillonf88fc122017-03-16 09:02:40 +01002188 if (!nc->sram.virt) {
2189 dev_err(nc->base.dev,
2190 "Could not allocate memory from the NFC SRAM pool\n");
2191 return -ENOMEM;
2192 }
2193
2194 return 0;
2195}
2196
2197static int
2198atmel_hsmc_nand_controller_remove(struct atmel_nand_controller *nc)
2199{
2200 struct atmel_hsmc_nand_controller *hsmc_nc;
2201 int ret;
2202
2203 ret = atmel_nand_controller_remove_nands(nc);
2204 if (ret)
2205 return ret;
2206
2207 hsmc_nc = container_of(nc, struct atmel_hsmc_nand_controller, base);
2208 if (hsmc_nc->sram.pool)
2209 gen_pool_free(hsmc_nc->sram.pool,
2210 (unsigned long)hsmc_nc->sram.virt,
2211 ATMEL_NFC_SRAM_SIZE);
2212
2213 if (hsmc_nc->clk) {
2214 clk_disable_unprepare(hsmc_nc->clk);
2215 clk_put(hsmc_nc->clk);
2216 }
2217
2218 atmel_nand_controller_cleanup(nc);
2219
2220 return 0;
2221}
2222
2223static int atmel_hsmc_nand_controller_probe(struct platform_device *pdev,
2224 const struct atmel_nand_controller_caps *caps)
2225{
2226 struct device *dev = &pdev->dev;
2227 struct atmel_hsmc_nand_controller *nc;
2228 int ret;
2229
2230 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2231 if (!nc)
2232 return -ENOMEM;
2233
2234 ret = atmel_nand_controller_init(&nc->base, pdev, caps);
2235 if (ret)
2236 return ret;
2237
2238 if (caps->legacy_of_bindings)
2239 ret = atmel_hsmc_nand_controller_legacy_init(nc);
2240 else
2241 ret = atmel_hsmc_nand_controller_init(nc);
2242
2243 if (ret)
2244 return ret;
2245
2246 /* Make sure all irqs are masked before registering our IRQ handler. */
2247 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, 0xffffffff);
2248 ret = devm_request_irq(dev, nc->irq, atmel_nfc_interrupt,
2249 IRQF_SHARED, "nfc", nc);
2250 if (ret) {
2251 dev_err(dev,
2252 "Could not get register NFC interrupt handler (err = %d)\n",
2253 ret);
2254 goto err;
2255 }
2256
2257 /* Initial NFC configuration. */
2258 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CFG,
2259 ATMEL_HSMC_NFC_CFG_DTO_MAX);
2260
2261 ret = atmel_nand_controller_add_nands(&nc->base);
2262 if (ret)
2263 goto err;
2264
2265 return 0;
2266
2267err:
2268 atmel_hsmc_nand_controller_remove(&nc->base);
2269
2270 return ret;
2271}
2272
2273static const struct atmel_nand_controller_ops atmel_hsmc_nc_ops = {
2274 .probe = atmel_hsmc_nand_controller_probe,
2275 .remove = atmel_hsmc_nand_controller_remove,
2276 .ecc_init = atmel_hsmc_nand_ecc_init,
2277 .nand_init = atmel_hsmc_nand_init,
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002278 .setup_data_interface = atmel_hsmc_nand_setup_data_interface,
Boris Brezillonf88fc122017-03-16 09:02:40 +01002279};
2280
2281static const struct atmel_nand_controller_caps atmel_sama5_nc_caps = {
2282 .has_dma = true,
2283 .ale_offs = BIT(21),
2284 .cle_offs = BIT(22),
2285 .ops = &atmel_hsmc_nc_ops,
2286};
2287
2288/* Only used to parse old bindings. */
2289static const struct atmel_nand_controller_caps atmel_sama5_nand_caps = {
2290 .has_dma = true,
2291 .ale_offs = BIT(21),
2292 .cle_offs = BIT(22),
2293 .ops = &atmel_hsmc_nc_ops,
2294 .legacy_of_bindings = true,
2295};
2296
2297static int atmel_smc_nand_controller_probe(struct platform_device *pdev,
2298 const struct atmel_nand_controller_caps *caps)
2299{
2300 struct device *dev = &pdev->dev;
2301 struct atmel_smc_nand_controller *nc;
2302 int ret;
2303
2304 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2305 if (!nc)
2306 return -ENOMEM;
2307
2308 ret = atmel_nand_controller_init(&nc->base, pdev, caps);
2309 if (ret)
2310 return ret;
2311
2312 ret = atmel_smc_nand_controller_init(nc);
2313 if (ret)
2314 return ret;
2315
2316 return atmel_nand_controller_add_nands(&nc->base);
2317}
2318
2319static int
2320atmel_smc_nand_controller_remove(struct atmel_nand_controller *nc)
2321{
2322 int ret;
2323
2324 ret = atmel_nand_controller_remove_nands(nc);
2325 if (ret)
2326 return ret;
2327
2328 atmel_nand_controller_cleanup(nc);
2329
2330 return 0;
2331}
2332
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002333/*
2334 * The SMC reg layout of at91rm9200 is completely different which prevents us
2335 * from re-using atmel_smc_nand_setup_data_interface() for the
2336 * ->setup_data_interface() hook.
2337 * At this point, there's no support for the at91rm9200 SMC IP, so we leave
2338 * ->setup_data_interface() unassigned.
2339 */
2340static const struct atmel_nand_controller_ops at91rm9200_nc_ops = {
Boris Brezillonf88fc122017-03-16 09:02:40 +01002341 .probe = atmel_smc_nand_controller_probe,
2342 .remove = atmel_smc_nand_controller_remove,
2343 .ecc_init = atmel_nand_ecc_init,
2344 .nand_init = atmel_smc_nand_init,
2345};
2346
2347static const struct atmel_nand_controller_caps atmel_rm9200_nc_caps = {
2348 .ale_offs = BIT(21),
2349 .cle_offs = BIT(22),
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002350 .ops = &at91rm9200_nc_ops,
2351};
2352
2353static const struct atmel_nand_controller_ops atmel_smc_nc_ops = {
2354 .probe = atmel_smc_nand_controller_probe,
2355 .remove = atmel_smc_nand_controller_remove,
2356 .ecc_init = atmel_nand_ecc_init,
2357 .nand_init = atmel_smc_nand_init,
2358 .setup_data_interface = atmel_smc_nand_setup_data_interface,
2359};
2360
2361static const struct atmel_nand_controller_caps atmel_sam9260_nc_caps = {
2362 .ale_offs = BIT(21),
2363 .cle_offs = BIT(22),
Boris Brezillonf88fc122017-03-16 09:02:40 +01002364 .ops = &atmel_smc_nc_ops,
2365};
2366
2367static const struct atmel_nand_controller_caps atmel_sam9261_nc_caps = {
2368 .ale_offs = BIT(22),
2369 .cle_offs = BIT(21),
2370 .ops = &atmel_smc_nc_ops,
2371};
2372
2373static const struct atmel_nand_controller_caps atmel_sam9g45_nc_caps = {
2374 .has_dma = true,
2375 .ale_offs = BIT(21),
2376 .cle_offs = BIT(22),
2377 .ops = &atmel_smc_nc_ops,
2378};
2379
2380/* Only used to parse old bindings. */
2381static const struct atmel_nand_controller_caps atmel_rm9200_nand_caps = {
2382 .ale_offs = BIT(21),
2383 .cle_offs = BIT(22),
2384 .ops = &atmel_smc_nc_ops,
2385 .legacy_of_bindings = true,
2386};
2387
2388static const struct atmel_nand_controller_caps atmel_sam9261_nand_caps = {
2389 .ale_offs = BIT(22),
2390 .cle_offs = BIT(21),
2391 .ops = &atmel_smc_nc_ops,
2392 .legacy_of_bindings = true,
2393};
2394
2395static const struct atmel_nand_controller_caps atmel_sam9g45_nand_caps = {
2396 .has_dma = true,
2397 .ale_offs = BIT(21),
2398 .cle_offs = BIT(22),
2399 .ops = &atmel_smc_nc_ops,
2400 .legacy_of_bindings = true,
2401};
2402
2403static const struct of_device_id atmel_nand_controller_of_ids[] = {
2404 {
2405 .compatible = "atmel,at91rm9200-nand-controller",
2406 .data = &atmel_rm9200_nc_caps,
2407 },
2408 {
2409 .compatible = "atmel,at91sam9260-nand-controller",
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002410 .data = &atmel_sam9260_nc_caps,
Boris Brezillonf88fc122017-03-16 09:02:40 +01002411 },
2412 {
2413 .compatible = "atmel,at91sam9261-nand-controller",
2414 .data = &atmel_sam9261_nc_caps,
2415 },
2416 {
2417 .compatible = "atmel,at91sam9g45-nand-controller",
2418 .data = &atmel_sam9g45_nc_caps,
2419 },
2420 {
2421 .compatible = "atmel,sama5d3-nand-controller",
2422 .data = &atmel_sama5_nc_caps,
2423 },
2424 /* Support for old/deprecated bindings: */
2425 {
2426 .compatible = "atmel,at91rm9200-nand",
2427 .data = &atmel_rm9200_nand_caps,
2428 },
2429 {
2430 .compatible = "atmel,sama5d4-nand",
2431 .data = &atmel_rm9200_nand_caps,
2432 },
2433 {
2434 .compatible = "atmel,sama5d2-nand",
2435 .data = &atmel_rm9200_nand_caps,
2436 },
2437 { /* sentinel */ },
2438};
2439MODULE_DEVICE_TABLE(of, atmel_nand_controller_of_ids);
2440
2441static int atmel_nand_controller_probe(struct platform_device *pdev)
2442{
2443 const struct atmel_nand_controller_caps *caps;
2444
2445 if (pdev->id_entry)
2446 caps = (void *)pdev->id_entry->driver_data;
2447 else
2448 caps = of_device_get_match_data(&pdev->dev);
2449
2450 if (!caps) {
2451 dev_err(&pdev->dev, "Could not retrieve NFC caps\n");
2452 return -EINVAL;
2453 }
2454
2455 if (caps->legacy_of_bindings) {
2456 u32 ale_offs = 21;
2457
2458 /*
2459 * If we are parsing legacy DT props and the DT contains a
2460 * valid NFC node, forward the request to the sama5 logic.
2461 */
2462 if (of_find_compatible_node(pdev->dev.of_node, NULL,
2463 "atmel,sama5d3-nfc"))
2464 caps = &atmel_sama5_nand_caps;
2465
2466 /*
2467 * Even if the compatible says we are dealing with an
2468 * at91rm9200 controller, the atmel,nand-has-dma specify that
2469 * this controller supports DMA, which means we are in fact
2470 * dealing with an at91sam9g45+ controller.
2471 */
2472 if (!caps->has_dma &&
2473 of_property_read_bool(pdev->dev.of_node,
2474 "atmel,nand-has-dma"))
2475 caps = &atmel_sam9g45_nand_caps;
2476
2477 /*
2478 * All SoCs except the at91sam9261 are assigning ALE to A21 and
2479 * CLE to A22. If atmel,nand-addr-offset != 21 this means we're
2480 * actually dealing with an at91sam9261 controller.
2481 */
2482 of_property_read_u32(pdev->dev.of_node,
2483 "atmel,nand-addr-offset", &ale_offs);
2484 if (ale_offs != 21)
2485 caps = &atmel_sam9261_nand_caps;
2486 }
2487
2488 return caps->ops->probe(pdev, caps);
2489}
2490
2491static int atmel_nand_controller_remove(struct platform_device *pdev)
2492{
2493 struct atmel_nand_controller *nc = platform_get_drvdata(pdev);
2494
2495 return nc->caps->ops->remove(nc);
2496}
2497
Arnd Bergmann05b6c232017-05-31 10:19:26 +02002498static __maybe_unused int atmel_nand_controller_resume(struct device *dev)
Boris Brezillon6e532af2017-03-16 09:36:00 +01002499{
2500 struct atmel_nand_controller *nc = dev_get_drvdata(dev);
2501 struct atmel_nand *nand;
2502
Romain Izard143b0ab2017-09-28 11:46:23 +02002503 if (nc->pmecc)
2504 atmel_pmecc_reset(nc->pmecc);
2505
Boris Brezillon6e532af2017-03-16 09:36:00 +01002506 list_for_each_entry(nand, &nc->chips, node) {
2507 int i;
2508
2509 for (i = 0; i < nand->numcs; i++)
2510 nand_reset(&nand->base, i);
2511 }
2512
2513 return 0;
2514}
2515
2516static SIMPLE_DEV_PM_OPS(atmel_nand_controller_pm_ops, NULL,
2517 atmel_nand_controller_resume);
2518
Boris Brezillonf88fc122017-03-16 09:02:40 +01002519static struct platform_driver atmel_nand_controller_driver = {
2520 .driver = {
2521 .name = "atmel-nand-controller",
2522 .of_match_table = of_match_ptr(atmel_nand_controller_of_ids),
Boris Brezillon1533bfa2017-10-05 18:57:24 +02002523 .pm = &atmel_nand_controller_pm_ops,
Boris Brezillonf88fc122017-03-16 09:02:40 +01002524 },
2525 .probe = atmel_nand_controller_probe,
2526 .remove = atmel_nand_controller_remove,
2527};
2528module_platform_driver(atmel_nand_controller_driver);
2529
2530MODULE_LICENSE("GPL");
2531MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
2532MODULE_DESCRIPTION("NAND Flash Controller driver for Atmel SoCs");
2533MODULE_ALIAS("platform:atmel-nand-controller");