blob: 84ddede5ede4b639442e604f937cf925d60139c2 [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
Boris Brezillon7e534322018-09-06 14:05:22 +0200413static u8 atmel_nand_read_byte(struct nand_chip *chip)
Boris Brezillonf88fc122017-03-16 09:02:40 +0100414{
Boris Brezillonf88fc122017-03-16 09:02:40 +0100415 struct atmel_nand *nand = to_atmel_nand(chip);
416
417 return ioread8(nand->activecs->io.virt);
418}
419
Boris Brezillonc0739d82018-09-06 14:05:23 +0200420static void atmel_nand_write_byte(struct nand_chip *chip, u8 byte)
Boris Brezillonf88fc122017-03-16 09:02:40 +0100421{
Boris Brezillonf88fc122017-03-16 09:02:40 +0100422 struct atmel_nand *nand = to_atmel_nand(chip);
423
424 if (chip->options & NAND_BUSWIDTH_16)
425 iowrite16(byte | (byte << 8), nand->activecs->io.virt);
426 else
427 iowrite8(byte, nand->activecs->io.virt);
428}
429
Boris Brezillon7e534322018-09-06 14:05:22 +0200430static void atmel_nand_read_buf(struct nand_chip *chip, u8 *buf, int len)
Boris Brezillonf88fc122017-03-16 09:02:40 +0100431{
Boris Brezillonf88fc122017-03-16 09:02:40 +0100432 struct atmel_nand *nand = to_atmel_nand(chip);
433 struct atmel_nand_controller *nc;
434
435 nc = to_nand_controller(chip->controller);
436
437 /*
438 * If the controller supports DMA, the buffer address is DMA-able and
439 * len is long enough to make DMA transfers profitable, let's trigger
440 * a DMA transfer. If it fails, fallback to PIO mode.
441 */
442 if (nc->dmac && virt_addr_valid(buf) &&
443 len >= MIN_DMA_LEN &&
444 !atmel_nand_dma_transfer(nc, buf, nand->activecs->io.dma, len,
445 DMA_FROM_DEVICE))
446 return;
447
448 if (chip->options & NAND_BUSWIDTH_16)
449 ioread16_rep(nand->activecs->io.virt, buf, len / 2);
450 else
451 ioread8_rep(nand->activecs->io.virt, buf, len);
452}
453
Boris Brezillonc0739d82018-09-06 14:05:23 +0200454static void atmel_nand_write_buf(struct nand_chip *chip, const u8 *buf, int len)
Boris Brezillonf88fc122017-03-16 09:02:40 +0100455{
Boris Brezillonf88fc122017-03-16 09:02:40 +0100456 struct atmel_nand *nand = to_atmel_nand(chip);
457 struct atmel_nand_controller *nc;
458
459 nc = to_nand_controller(chip->controller);
460
461 /*
462 * If the controller supports DMA, the buffer address is DMA-able and
463 * len is long enough to make DMA transfers profitable, let's trigger
464 * a DMA transfer. If it fails, fallback to PIO mode.
465 */
466 if (nc->dmac && virt_addr_valid(buf) &&
467 len >= MIN_DMA_LEN &&
468 !atmel_nand_dma_transfer(nc, (void *)buf, nand->activecs->io.dma,
469 len, DMA_TO_DEVICE))
470 return;
471
472 if (chip->options & NAND_BUSWIDTH_16)
473 iowrite16_rep(nand->activecs->io.virt, buf, len / 2);
474 else
475 iowrite8_rep(nand->activecs->io.virt, buf, len);
476}
477
478static int atmel_nand_dev_ready(struct mtd_info *mtd)
479{
480 struct nand_chip *chip = mtd_to_nand(mtd);
481 struct atmel_nand *nand = to_atmel_nand(chip);
482
483 return gpiod_get_value(nand->activecs->rb.gpio);
484}
485
486static void atmel_nand_select_chip(struct mtd_info *mtd, int cs)
487{
488 struct nand_chip *chip = mtd_to_nand(mtd);
489 struct atmel_nand *nand = to_atmel_nand(chip);
490
491 if (cs < 0 || cs >= nand->numcs) {
492 nand->activecs = NULL;
493 chip->dev_ready = NULL;
494 return;
495 }
496
497 nand->activecs = &nand->cs[cs];
498
499 if (nand->activecs->rb.type == ATMEL_NAND_GPIO_RB)
500 chip->dev_ready = atmel_nand_dev_ready;
501}
502
503static int atmel_hsmc_nand_dev_ready(struct mtd_info *mtd)
504{
505 struct nand_chip *chip = mtd_to_nand(mtd);
506 struct atmel_nand *nand = to_atmel_nand(chip);
507 struct atmel_hsmc_nand_controller *nc;
508 u32 status;
509
510 nc = to_hsmc_nand_controller(chip->controller);
511
512 regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &status);
513
514 return status & ATMEL_HSMC_NFC_SR_RBEDGE(nand->activecs->rb.id);
515}
516
517static void atmel_hsmc_nand_select_chip(struct mtd_info *mtd, int cs)
518{
519 struct nand_chip *chip = mtd_to_nand(mtd);
520 struct atmel_nand *nand = to_atmel_nand(chip);
521 struct atmel_hsmc_nand_controller *nc;
522
523 nc = to_hsmc_nand_controller(chip->controller);
524
525 atmel_nand_select_chip(mtd, cs);
526
527 if (!nand->activecs) {
528 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CTRL,
529 ATMEL_HSMC_NFC_CTRL_DIS);
530 return;
531 }
532
533 if (nand->activecs->rb.type == ATMEL_NAND_NATIVE_RB)
534 chip->dev_ready = atmel_hsmc_nand_dev_ready;
535
536 regmap_update_bits(nc->base.smc, ATMEL_HSMC_NFC_CFG,
537 ATMEL_HSMC_NFC_CFG_PAGESIZE_MASK |
538 ATMEL_HSMC_NFC_CFG_SPARESIZE_MASK |
539 ATMEL_HSMC_NFC_CFG_RSPARE |
540 ATMEL_HSMC_NFC_CFG_WSPARE,
541 ATMEL_HSMC_NFC_CFG_PAGESIZE(mtd->writesize) |
542 ATMEL_HSMC_NFC_CFG_SPARESIZE(mtd->oobsize) |
543 ATMEL_HSMC_NFC_CFG_RSPARE);
544 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CTRL,
545 ATMEL_HSMC_NFC_CTRL_EN);
546}
547
548static int atmel_nfc_exec_op(struct atmel_hsmc_nand_controller *nc, bool poll)
549{
550 u8 *addrs = nc->op.addrs;
551 unsigned int op = 0;
552 u32 addr, val;
553 int i, ret;
554
555 nc->op.wait = ATMEL_HSMC_NFC_SR_CMDDONE;
556
557 for (i = 0; i < nc->op.ncmds; i++)
558 op |= ATMEL_NFC_CMD(i, nc->op.cmds[i]);
559
560 if (nc->op.naddrs == ATMEL_NFC_MAX_ADDR_CYCLES)
561 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_ADDR, *addrs++);
562
563 op |= ATMEL_NFC_CSID(nc->op.cs) |
564 ATMEL_NFC_ACYCLE(nc->op.naddrs);
565
566 if (nc->op.ncmds > 1)
567 op |= ATMEL_NFC_VCMD2;
568
569 addr = addrs[0] | (addrs[1] << 8) | (addrs[2] << 16) |
570 (addrs[3] << 24);
571
572 if (nc->op.data != ATMEL_NFC_NO_DATA) {
573 op |= ATMEL_NFC_DATAEN;
574 nc->op.wait |= ATMEL_HSMC_NFC_SR_XFRDONE;
575
576 if (nc->op.data == ATMEL_NFC_WRITE_DATA)
577 op |= ATMEL_NFC_NFCWR;
578 }
579
580 /* Clear all flags. */
581 regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &val);
582
583 /* Send the command. */
584 regmap_write(nc->io, op, addr);
585
586 ret = atmel_nfc_wait(nc, poll, 0);
587 if (ret)
588 dev_err(nc->base.dev,
589 "Failed to send NAND command (err = %d)!",
590 ret);
591
592 /* Reset the op state. */
593 memset(&nc->op, 0, sizeof(nc->op));
594
595 return ret;
596}
597
598static void atmel_hsmc_nand_cmd_ctrl(struct mtd_info *mtd, int dat,
599 unsigned int ctrl)
600{
601 struct nand_chip *chip = mtd_to_nand(mtd);
602 struct atmel_nand *nand = to_atmel_nand(chip);
603 struct atmel_hsmc_nand_controller *nc;
604
605 nc = to_hsmc_nand_controller(chip->controller);
606
607 if (ctrl & NAND_ALE) {
608 if (nc->op.naddrs == ATMEL_NFC_MAX_ADDR_CYCLES)
609 return;
610
611 nc->op.addrs[nc->op.naddrs++] = dat;
612 } else if (ctrl & NAND_CLE) {
613 if (nc->op.ncmds > 1)
614 return;
615
616 nc->op.cmds[nc->op.ncmds++] = dat;
617 }
618
619 if (dat == NAND_CMD_NONE) {
620 nc->op.cs = nand->activecs->id;
621 atmel_nfc_exec_op(nc, true);
622 }
623}
624
625static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
626 unsigned int ctrl)
627{
628 struct nand_chip *chip = mtd_to_nand(mtd);
629 struct atmel_nand *nand = to_atmel_nand(chip);
630 struct atmel_nand_controller *nc;
631
632 nc = to_nand_controller(chip->controller);
633
634 if ((ctrl & NAND_CTRL_CHANGE) && nand->activecs->csgpio) {
635 if (ctrl & NAND_NCE)
636 gpiod_set_value(nand->activecs->csgpio, 0);
637 else
638 gpiod_set_value(nand->activecs->csgpio, 1);
639 }
640
641 if (ctrl & NAND_ALE)
642 writeb(cmd, nand->activecs->io.virt + nc->caps->ale_offs);
643 else if (ctrl & NAND_CLE)
644 writeb(cmd, nand->activecs->io.virt + nc->caps->cle_offs);
645}
646
647static void atmel_nfc_copy_to_sram(struct nand_chip *chip, const u8 *buf,
648 bool oob_required)
649{
650 struct mtd_info *mtd = nand_to_mtd(chip);
651 struct atmel_hsmc_nand_controller *nc;
652 int ret = -EIO;
653
654 nc = to_hsmc_nand_controller(chip->controller);
655
656 if (nc->base.dmac)
657 ret = atmel_nand_dma_transfer(&nc->base, (void *)buf,
658 nc->sram.dma, mtd->writesize,
659 DMA_TO_DEVICE);
660
661 /* Falling back to CPU copy. */
662 if (ret)
663 memcpy_toio(nc->sram.virt, buf, mtd->writesize);
664
665 if (oob_required)
666 memcpy_toio(nc->sram.virt + mtd->writesize, chip->oob_poi,
667 mtd->oobsize);
668}
669
670static void atmel_nfc_copy_from_sram(struct nand_chip *chip, u8 *buf,
671 bool oob_required)
672{
673 struct mtd_info *mtd = nand_to_mtd(chip);
674 struct atmel_hsmc_nand_controller *nc;
675 int ret = -EIO;
676
677 nc = to_hsmc_nand_controller(chip->controller);
678
679 if (nc->base.dmac)
680 ret = atmel_nand_dma_transfer(&nc->base, buf, nc->sram.dma,
681 mtd->writesize, DMA_FROM_DEVICE);
682
683 /* Falling back to CPU copy. */
684 if (ret)
685 memcpy_fromio(buf, nc->sram.virt, mtd->writesize);
686
687 if (oob_required)
688 memcpy_fromio(chip->oob_poi, nc->sram.virt + mtd->writesize,
689 mtd->oobsize);
690}
691
692static void atmel_nfc_set_op_addr(struct nand_chip *chip, int page, int column)
693{
694 struct mtd_info *mtd = nand_to_mtd(chip);
695 struct atmel_hsmc_nand_controller *nc;
696
697 nc = to_hsmc_nand_controller(chip->controller);
698
699 if (column >= 0) {
700 nc->op.addrs[nc->op.naddrs++] = column;
701
702 /*
703 * 2 address cycles for the column offset on large page NANDs.
704 */
705 if (mtd->writesize > 512)
706 nc->op.addrs[nc->op.naddrs++] = column >> 8;
707 }
708
709 if (page >= 0) {
710 nc->op.addrs[nc->op.naddrs++] = page;
711 nc->op.addrs[nc->op.naddrs++] = page >> 8;
712
Masahiro Yamada14157f82017-09-13 11:05:50 +0900713 if (chip->options & NAND_ROW_ADDR_3)
Boris Brezillonf88fc122017-03-16 09:02:40 +0100714 nc->op.addrs[nc->op.naddrs++] = page >> 16;
715 }
716}
717
718static int atmel_nand_pmecc_enable(struct nand_chip *chip, int op, bool raw)
719{
720 struct atmel_nand *nand = to_atmel_nand(chip);
721 struct atmel_nand_controller *nc;
722 int ret;
723
724 nc = to_nand_controller(chip->controller);
725
726 if (raw)
727 return 0;
728
729 ret = atmel_pmecc_enable(nand->pmecc, op);
730 if (ret)
731 dev_err(nc->dev,
732 "Failed to enable ECC engine (err = %d)\n", ret);
733
734 return ret;
735}
736
737static void atmel_nand_pmecc_disable(struct nand_chip *chip, bool raw)
738{
739 struct atmel_nand *nand = to_atmel_nand(chip);
740
741 if (!raw)
742 atmel_pmecc_disable(nand->pmecc);
743}
744
745static int atmel_nand_pmecc_generate_eccbytes(struct nand_chip *chip, bool raw)
746{
747 struct atmel_nand *nand = to_atmel_nand(chip);
748 struct mtd_info *mtd = nand_to_mtd(chip);
749 struct atmel_nand_controller *nc;
750 struct mtd_oob_region oobregion;
751 void *eccbuf;
752 int ret, i;
753
754 nc = to_nand_controller(chip->controller);
755
756 if (raw)
757 return 0;
758
759 ret = atmel_pmecc_wait_rdy(nand->pmecc);
760 if (ret) {
761 dev_err(nc->dev,
762 "Failed to transfer NAND page data (err = %d)\n",
763 ret);
764 return ret;
765 }
766
767 mtd_ooblayout_ecc(mtd, 0, &oobregion);
768 eccbuf = chip->oob_poi + oobregion.offset;
769
770 for (i = 0; i < chip->ecc.steps; i++) {
771 atmel_pmecc_get_generated_eccbytes(nand->pmecc, i,
772 eccbuf);
773 eccbuf += chip->ecc.bytes;
774 }
775
776 return 0;
777}
778
779static int atmel_nand_pmecc_correct_data(struct nand_chip *chip, void *buf,
780 bool raw)
781{
782 struct atmel_nand *nand = to_atmel_nand(chip);
783 struct mtd_info *mtd = nand_to_mtd(chip);
784 struct atmel_nand_controller *nc;
785 struct mtd_oob_region oobregion;
786 int ret, i, max_bitflips = 0;
787 void *databuf, *eccbuf;
788
789 nc = to_nand_controller(chip->controller);
790
791 if (raw)
792 return 0;
793
794 ret = atmel_pmecc_wait_rdy(nand->pmecc);
795 if (ret) {
796 dev_err(nc->dev,
797 "Failed to read NAND page data (err = %d)\n",
798 ret);
799 return ret;
800 }
801
802 mtd_ooblayout_ecc(mtd, 0, &oobregion);
803 eccbuf = chip->oob_poi + oobregion.offset;
804 databuf = buf;
805
806 for (i = 0; i < chip->ecc.steps; i++) {
807 ret = atmel_pmecc_correct_sector(nand->pmecc, i, databuf,
808 eccbuf);
809 if (ret < 0 && !atmel_pmecc_correct_erased_chunks(nand->pmecc))
810 ret = nand_check_erased_ecc_chunk(databuf,
811 chip->ecc.size,
812 eccbuf,
813 chip->ecc.bytes,
814 NULL, 0,
815 chip->ecc.strength);
816
817 if (ret >= 0)
818 max_bitflips = max(ret, max_bitflips);
819 else
820 mtd->ecc_stats.failed++;
821
822 databuf += chip->ecc.size;
823 eccbuf += chip->ecc.bytes;
824 }
825
826 return max_bitflips;
827}
828
829static int atmel_nand_pmecc_write_pg(struct nand_chip *chip, const u8 *buf,
830 bool oob_required, int page, bool raw)
831{
832 struct mtd_info *mtd = nand_to_mtd(chip);
833 struct atmel_nand *nand = to_atmel_nand(chip);
834 int ret;
835
Boris Brezillon25f815f2017-11-30 18:01:30 +0100836 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
837
Boris Brezillonf88fc122017-03-16 09:02:40 +0100838 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw);
839 if (ret)
840 return ret;
841
Boris Brezillonc0739d82018-09-06 14:05:23 +0200842 atmel_nand_write_buf(chip, buf, mtd->writesize);
Boris Brezillonf88fc122017-03-16 09:02:40 +0100843
844 ret = atmel_nand_pmecc_generate_eccbytes(chip, raw);
845 if (ret) {
846 atmel_pmecc_disable(nand->pmecc);
847 return ret;
848 }
849
850 atmel_nand_pmecc_disable(chip, raw);
851
Boris Brezillonc0739d82018-09-06 14:05:23 +0200852 atmel_nand_write_buf(chip, chip->oob_poi, mtd->oobsize);
Boris Brezillonf88fc122017-03-16 09:02:40 +0100853
Boris Brezillon25f815f2017-11-30 18:01:30 +0100854 return nand_prog_page_end_op(chip);
Boris Brezillonf88fc122017-03-16 09:02:40 +0100855}
856
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200857static int atmel_nand_pmecc_write_page(struct nand_chip *chip, const u8 *buf,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100858 int oob_required, int page)
859{
860 return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, false);
861}
862
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200863static int atmel_nand_pmecc_write_page_raw(struct nand_chip *chip,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100864 const u8 *buf, int oob_required,
865 int page)
866{
867 return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, true);
868}
869
870static int atmel_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf,
871 bool oob_required, int page, bool raw)
872{
873 struct mtd_info *mtd = nand_to_mtd(chip);
874 int ret;
875
Boris Brezillon25f815f2017-11-30 18:01:30 +0100876 nand_read_page_op(chip, page, 0, NULL, 0);
877
Boris Brezillonf88fc122017-03-16 09:02:40 +0100878 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw);
879 if (ret)
880 return ret;
881
Boris Brezillon7e534322018-09-06 14:05:22 +0200882 atmel_nand_read_buf(chip, buf, mtd->writesize);
883 atmel_nand_read_buf(chip, chip->oob_poi, mtd->oobsize);
Boris Brezillonf88fc122017-03-16 09:02:40 +0100884
885 ret = atmel_nand_pmecc_correct_data(chip, buf, raw);
886
887 atmel_nand_pmecc_disable(chip, raw);
888
889 return ret;
890}
891
Boris Brezillonb9761682018-09-06 14:05:20 +0200892static int atmel_nand_pmecc_read_page(struct nand_chip *chip, u8 *buf,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100893 int oob_required, int page)
894{
895 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, false);
896}
897
Boris Brezillonb9761682018-09-06 14:05:20 +0200898static int atmel_nand_pmecc_read_page_raw(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, true);
902}
903
904static int atmel_hsmc_nand_pmecc_write_pg(struct nand_chip *chip,
905 const u8 *buf, bool oob_required,
906 int page, bool raw)
907{
908 struct mtd_info *mtd = nand_to_mtd(chip);
909 struct atmel_nand *nand = to_atmel_nand(chip);
910 struct atmel_hsmc_nand_controller *nc;
Boris Brezillon41145642017-05-16 18:27:49 +0200911 int ret, status;
Boris Brezillonf88fc122017-03-16 09:02:40 +0100912
913 nc = to_hsmc_nand_controller(chip->controller);
914
915 atmel_nfc_copy_to_sram(chip, buf, false);
916
917 nc->op.cmds[0] = NAND_CMD_SEQIN;
918 nc->op.ncmds = 1;
919 atmel_nfc_set_op_addr(chip, page, 0x0);
920 nc->op.cs = nand->activecs->id;
921 nc->op.data = ATMEL_NFC_WRITE_DATA;
922
923 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw);
924 if (ret)
925 return ret;
926
927 ret = atmel_nfc_exec_op(nc, false);
928 if (ret) {
929 atmel_nand_pmecc_disable(chip, raw);
930 dev_err(nc->base.dev,
931 "Failed to transfer NAND page data (err = %d)\n",
932 ret);
933 return ret;
934 }
935
936 ret = atmel_nand_pmecc_generate_eccbytes(chip, raw);
937
938 atmel_nand_pmecc_disable(chip, raw);
939
940 if (ret)
941 return ret;
942
Boris Brezillonc0739d82018-09-06 14:05:23 +0200943 atmel_nand_write_buf(chip, chip->oob_poi, mtd->oobsize);
Boris Brezillonf88fc122017-03-16 09:02:40 +0100944
945 nc->op.cmds[0] = NAND_CMD_PAGEPROG;
946 nc->op.ncmds = 1;
947 nc->op.cs = nand->activecs->id;
948 ret = atmel_nfc_exec_op(nc, false);
949 if (ret)
950 dev_err(nc->base.dev, "Failed to program NAND page (err = %d)\n",
951 ret);
952
Boris Brezillon41145642017-05-16 18:27:49 +0200953 status = chip->waitfunc(mtd, chip);
954 if (status & NAND_STATUS_FAIL)
955 return -EIO;
956
Boris Brezillonf88fc122017-03-16 09:02:40 +0100957 return ret;
958}
959
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200960static int atmel_hsmc_nand_pmecc_write_page(struct nand_chip *chip,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100961 const u8 *buf, int oob_required,
962 int page)
963{
964 return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page,
965 false);
966}
967
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200968static int atmel_hsmc_nand_pmecc_write_page_raw(struct nand_chip *chip,
Boris Brezillonf88fc122017-03-16 09:02:40 +0100969 const u8 *buf,
970 int oob_required, int page)
971{
972 return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page,
973 true);
974}
975
976static int atmel_hsmc_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf,
977 bool oob_required, int page,
978 bool raw)
979{
980 struct mtd_info *mtd = nand_to_mtd(chip);
981 struct atmel_nand *nand = to_atmel_nand(chip);
982 struct atmel_hsmc_nand_controller *nc;
983 int ret;
984
985 nc = to_hsmc_nand_controller(chip->controller);
986
987 /*
988 * Optimized read page accessors only work when the NAND R/B pin is
989 * connected to a native SoC R/B pin. If that's not the case, fallback
990 * to the non-optimized one.
991 */
992 if (nand->activecs->rb.type != ATMEL_NAND_NATIVE_RB) {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100993 nand_read_page_op(chip, page, 0, NULL, 0);
Boris Brezillonf88fc122017-03-16 09:02:40 +0100994
995 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page,
996 raw);
997 }
998
999 nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READ0;
1000
1001 if (mtd->writesize > 512)
1002 nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READSTART;
1003
1004 atmel_nfc_set_op_addr(chip, page, 0x0);
1005 nc->op.cs = nand->activecs->id;
1006 nc->op.data = ATMEL_NFC_READ_DATA;
1007
1008 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw);
1009 if (ret)
1010 return ret;
1011
1012 ret = atmel_nfc_exec_op(nc, false);
1013 if (ret) {
1014 atmel_nand_pmecc_disable(chip, raw);
1015 dev_err(nc->base.dev,
1016 "Failed to load NAND page data (err = %d)\n",
1017 ret);
1018 return ret;
1019 }
1020
1021 atmel_nfc_copy_from_sram(chip, buf, true);
1022
1023 ret = atmel_nand_pmecc_correct_data(chip, buf, raw);
1024
1025 atmel_nand_pmecc_disable(chip, raw);
1026
1027 return ret;
1028}
1029
Boris Brezillonb9761682018-09-06 14:05:20 +02001030static int atmel_hsmc_nand_pmecc_read_page(struct nand_chip *chip, u8 *buf,
Boris Brezillonf88fc122017-03-16 09:02:40 +01001031 int oob_required, int page)
1032{
1033 return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page,
1034 false);
1035}
1036
Boris Brezillonb9761682018-09-06 14:05:20 +02001037static int atmel_hsmc_nand_pmecc_read_page_raw(struct nand_chip *chip,
Boris Brezillonf88fc122017-03-16 09:02:40 +01001038 u8 *buf, int oob_required,
1039 int page)
1040{
1041 return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page,
1042 true);
1043}
1044
1045static int atmel_nand_pmecc_init(struct nand_chip *chip)
1046{
1047 struct mtd_info *mtd = nand_to_mtd(chip);
1048 struct atmel_nand *nand = to_atmel_nand(chip);
1049 struct atmel_nand_controller *nc;
1050 struct atmel_pmecc_user_req req;
1051
1052 nc = to_nand_controller(chip->controller);
1053
1054 if (!nc->pmecc) {
1055 dev_err(nc->dev, "HW ECC not supported\n");
1056 return -ENOTSUPP;
1057 }
1058
1059 if (nc->caps->legacy_of_bindings) {
1060 u32 val;
1061
1062 if (!of_property_read_u32(nc->dev->of_node, "atmel,pmecc-cap",
1063 &val))
1064 chip->ecc.strength = val;
1065
1066 if (!of_property_read_u32(nc->dev->of_node,
1067 "atmel,pmecc-sector-size",
1068 &val))
1069 chip->ecc.size = val;
1070 }
1071
1072 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
1073 req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH;
1074 else if (chip->ecc.strength)
1075 req.ecc.strength = chip->ecc.strength;
1076 else if (chip->ecc_strength_ds)
1077 req.ecc.strength = chip->ecc_strength_ds;
1078 else
1079 req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH;
1080
1081 if (chip->ecc.size)
1082 req.ecc.sectorsize = chip->ecc.size;
1083 else if (chip->ecc_step_ds)
1084 req.ecc.sectorsize = chip->ecc_step_ds;
1085 else
1086 req.ecc.sectorsize = ATMEL_PMECC_SECTOR_SIZE_AUTO;
1087
1088 req.pagesize = mtd->writesize;
1089 req.oobsize = mtd->oobsize;
1090
1091 if (mtd->writesize <= 512) {
1092 req.ecc.bytes = 4;
1093 req.ecc.ooboffset = 0;
1094 } else {
1095 req.ecc.bytes = mtd->oobsize - 2;
1096 req.ecc.ooboffset = ATMEL_PMECC_OOBOFFSET_AUTO;
1097 }
1098
1099 nand->pmecc = atmel_pmecc_create_user(nc->pmecc, &req);
1100 if (IS_ERR(nand->pmecc))
1101 return PTR_ERR(nand->pmecc);
1102
1103 chip->ecc.algo = NAND_ECC_BCH;
1104 chip->ecc.size = req.ecc.sectorsize;
1105 chip->ecc.bytes = req.ecc.bytes / req.ecc.nsectors;
1106 chip->ecc.strength = req.ecc.strength;
1107
1108 chip->options |= NAND_NO_SUBPAGE_WRITE;
1109
1110 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
1111
1112 return 0;
1113}
1114
Miquel Raynal577e0102018-07-25 15:31:41 +02001115static int atmel_nand_ecc_init(struct nand_chip *chip)
Boris Brezillonf88fc122017-03-16 09:02:40 +01001116{
Boris Brezillonf88fc122017-03-16 09:02:40 +01001117 struct atmel_nand_controller *nc;
1118 int ret;
1119
1120 nc = to_nand_controller(chip->controller);
1121
1122 switch (chip->ecc.mode) {
1123 case NAND_ECC_NONE:
1124 case NAND_ECC_SOFT:
1125 /*
1126 * Nothing to do, the core will initialize everything for us.
1127 */
1128 break;
1129
1130 case NAND_ECC_HW:
1131 ret = atmel_nand_pmecc_init(chip);
1132 if (ret)
1133 return ret;
1134
1135 chip->ecc.read_page = atmel_nand_pmecc_read_page;
1136 chip->ecc.write_page = atmel_nand_pmecc_write_page;
1137 chip->ecc.read_page_raw = atmel_nand_pmecc_read_page_raw;
1138 chip->ecc.write_page_raw = atmel_nand_pmecc_write_page_raw;
1139 break;
1140
1141 default:
1142 /* Other modes are not supported. */
1143 dev_err(nc->dev, "Unsupported ECC mode: %d\n",
1144 chip->ecc.mode);
1145 return -ENOTSUPP;
1146 }
1147
1148 return 0;
1149}
1150
Miquel Raynal577e0102018-07-25 15:31:41 +02001151static int atmel_hsmc_nand_ecc_init(struct nand_chip *chip)
Boris Brezillonf88fc122017-03-16 09:02:40 +01001152{
Boris Brezillonf88fc122017-03-16 09:02:40 +01001153 int ret;
1154
Miquel Raynal577e0102018-07-25 15:31:41 +02001155 ret = atmel_nand_ecc_init(chip);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001156 if (ret)
1157 return ret;
1158
1159 if (chip->ecc.mode != NAND_ECC_HW)
1160 return 0;
1161
1162 /* Adjust the ECC operations for the HSMC IP. */
1163 chip->ecc.read_page = atmel_hsmc_nand_pmecc_read_page;
1164 chip->ecc.write_page = atmel_hsmc_nand_pmecc_write_page;
1165 chip->ecc.read_page_raw = atmel_hsmc_nand_pmecc_read_page_raw;
1166 chip->ecc.write_page_raw = atmel_hsmc_nand_pmecc_write_page_raw;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001167
1168 return 0;
1169}
1170
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001171static int atmel_smc_nand_prepare_smcconf(struct atmel_nand *nand,
1172 const struct nand_data_interface *conf,
1173 struct atmel_smc_cs_conf *smcconf)
1174{
1175 u32 ncycles, totalcycles, timeps, mckperiodps;
1176 struct atmel_nand_controller *nc;
1177 int ret;
1178
1179 nc = to_nand_controller(nand->base.controller);
1180
1181 /* DDR interface not supported. */
1182 if (conf->type != NAND_SDR_IFACE)
1183 return -ENOTSUPP;
1184
1185 /*
1186 * tRC < 30ns implies EDO mode. This controller does not support this
1187 * mode.
1188 */
Boris Brezillonee02f732017-07-31 10:32:21 +02001189 if (conf->timings.sdr.tRC_min < 30000)
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001190 return -ENOTSUPP;
1191
1192 atmel_smc_cs_conf_init(smcconf);
1193
1194 mckperiodps = NSEC_PER_SEC / clk_get_rate(nc->mck);
1195 mckperiodps *= 1000;
1196
1197 /*
1198 * Set write pulse timing. This one is easy to extract:
1199 *
1200 * NWE_PULSE = tWP
1201 */
1202 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWP_min, mckperiodps);
1203 totalcycles = ncycles;
1204 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NWE_SHIFT,
1205 ncycles);
1206 if (ret)
1207 return ret;
1208
1209 /*
1210 * The write setup timing depends on the operation done on the NAND.
1211 * All operations goes through the same data bus, but the operation
1212 * type depends on the address we are writing to (ALE/CLE address
1213 * lines).
1214 * Since we have no way to differentiate the different operations at
1215 * the SMC level, we must consider the worst case (the biggest setup
1216 * time among all operation types):
1217 *
1218 * NWE_SETUP = max(tCLS, tCS, tALS, tDS) - NWE_PULSE
1219 */
1220 timeps = max3(conf->timings.sdr.tCLS_min, conf->timings.sdr.tCS_min,
1221 conf->timings.sdr.tALS_min);
1222 timeps = max(timeps, conf->timings.sdr.tDS_min);
1223 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1224 ncycles = ncycles > totalcycles ? ncycles - totalcycles : 0;
1225 totalcycles += ncycles;
1226 ret = atmel_smc_cs_conf_set_setup(smcconf, ATMEL_SMC_NWE_SHIFT,
1227 ncycles);
1228 if (ret)
1229 return ret;
1230
1231 /*
1232 * As for the write setup timing, the write hold timing depends on the
1233 * operation done on the NAND:
1234 *
1235 * NWE_HOLD = max(tCLH, tCH, tALH, tDH, tWH)
1236 */
1237 timeps = max3(conf->timings.sdr.tCLH_min, conf->timings.sdr.tCH_min,
1238 conf->timings.sdr.tALH_min);
1239 timeps = max3(timeps, conf->timings.sdr.tDH_min,
1240 conf->timings.sdr.tWH_min);
1241 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1242 totalcycles += ncycles;
1243
1244 /*
1245 * The write cycle timing is directly matching tWC, but is also
1246 * dependent on the other timings on the setup and hold timings we
1247 * calculated earlier, which gives:
1248 *
1249 * NWE_CYCLE = max(tWC, NWE_SETUP + NWE_PULSE + NWE_HOLD)
1250 */
1251 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWC_min, mckperiodps);
1252 ncycles = max(totalcycles, ncycles);
1253 ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NWE_SHIFT,
1254 ncycles);
1255 if (ret)
1256 return ret;
1257
1258 /*
1259 * We don't want the CS line to be toggled between each byte/word
1260 * transfer to the NAND. The only way to guarantee that is to have the
1261 * NCS_{WR,RD}_{SETUP,HOLD} timings set to 0, which in turn means:
1262 *
1263 * NCS_WR_PULSE = NWE_CYCLE
1264 */
1265 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_WR_SHIFT,
1266 ncycles);
1267 if (ret)
1268 return ret;
1269
1270 /*
1271 * As for the write setup timing, the read hold timing depends on the
1272 * operation done on the NAND:
1273 *
1274 * NRD_HOLD = max(tREH, tRHOH)
1275 */
1276 timeps = max(conf->timings.sdr.tREH_min, conf->timings.sdr.tRHOH_min);
1277 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1278 totalcycles = ncycles;
1279
1280 /*
1281 * TDF = tRHZ - NRD_HOLD
1282 */
1283 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRHZ_max, mckperiodps);
1284 ncycles -= totalcycles;
1285
1286 /*
1287 * In ONFI 4.0 specs, tRHZ has been increased to support EDO NANDs and
1288 * we might end up with a config that does not fit in the TDF field.
1289 * Just take the max value in this case and hope that the NAND is more
1290 * tolerant than advertised.
1291 */
1292 if (ncycles > ATMEL_SMC_MODE_TDF_MAX)
1293 ncycles = ATMEL_SMC_MODE_TDF_MAX;
1294 else if (ncycles < ATMEL_SMC_MODE_TDF_MIN)
1295 ncycles = ATMEL_SMC_MODE_TDF_MIN;
1296
1297 smcconf->mode |= ATMEL_SMC_MODE_TDF(ncycles) |
1298 ATMEL_SMC_MODE_TDFMODE_OPTIMIZED;
1299
1300 /*
1301 * Read pulse timing directly matches tRP:
1302 *
1303 * NRD_PULSE = tRP
1304 */
1305 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRP_min, mckperiodps);
1306 totalcycles += ncycles;
1307 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NRD_SHIFT,
1308 ncycles);
1309 if (ret)
1310 return ret;
1311
1312 /*
1313 * The write cycle timing is directly matching tWC, but is also
1314 * dependent on the setup and hold timings we calculated earlier,
1315 * which gives:
1316 *
1317 * NRD_CYCLE = max(tRC, NRD_PULSE + NRD_HOLD)
1318 *
1319 * NRD_SETUP is always 0.
1320 */
1321 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRC_min, mckperiodps);
1322 ncycles = max(totalcycles, ncycles);
1323 ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NRD_SHIFT,
1324 ncycles);
1325 if (ret)
1326 return ret;
1327
1328 /*
1329 * We don't want the CS line to be toggled between each byte/word
1330 * transfer from the NAND. The only way to guarantee that is to have
1331 * the NCS_{WR,RD}_{SETUP,HOLD} timings set to 0, which in turn means:
1332 *
1333 * NCS_RD_PULSE = NRD_CYCLE
1334 */
1335 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_RD_SHIFT,
1336 ncycles);
1337 if (ret)
1338 return ret;
1339
1340 /* Txxx timings are directly matching tXXX ones. */
1341 ncycles = DIV_ROUND_UP(conf->timings.sdr.tCLR_min, mckperiodps);
1342 ret = atmel_smc_cs_conf_set_timing(smcconf,
1343 ATMEL_HSMC_TIMINGS_TCLR_SHIFT,
1344 ncycles);
1345 if (ret)
1346 return ret;
1347
1348 ncycles = DIV_ROUND_UP(conf->timings.sdr.tADL_min, mckperiodps);
1349 ret = atmel_smc_cs_conf_set_timing(smcconf,
1350 ATMEL_HSMC_TIMINGS_TADL_SHIFT,
1351 ncycles);
Boris Brezillonbe3e83e2017-08-23 20:45:01 +02001352 /*
1353 * Version 4 of the ONFI spec mandates that tADL be at least 400
1354 * nanoseconds, but, depending on the master clock rate, 400 ns may not
1355 * fit in the tADL field of the SMC reg. We need to relax the check and
1356 * accept the -ERANGE return code.
1357 *
1358 * Note that previous versions of the ONFI spec had a lower tADL_min
1359 * (100 or 200 ns). It's not clear why this timing constraint got
1360 * increased but it seems most NANDs are fine with values lower than
1361 * 400ns, so we should be safe.
1362 */
1363 if (ret && ret != -ERANGE)
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001364 return ret;
1365
1366 ncycles = DIV_ROUND_UP(conf->timings.sdr.tAR_min, mckperiodps);
1367 ret = atmel_smc_cs_conf_set_timing(smcconf,
1368 ATMEL_HSMC_TIMINGS_TAR_SHIFT,
1369 ncycles);
1370 if (ret)
1371 return ret;
1372
1373 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRR_min, mckperiodps);
1374 ret = atmel_smc_cs_conf_set_timing(smcconf,
1375 ATMEL_HSMC_TIMINGS_TRR_SHIFT,
1376 ncycles);
1377 if (ret)
1378 return ret;
1379
1380 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWB_max, mckperiodps);
1381 ret = atmel_smc_cs_conf_set_timing(smcconf,
1382 ATMEL_HSMC_TIMINGS_TWB_SHIFT,
1383 ncycles);
1384 if (ret)
1385 return ret;
1386
1387 /* Attach the CS line to the NFC logic. */
1388 smcconf->timings |= ATMEL_HSMC_TIMINGS_NFSEL;
1389
1390 /* Set the appropriate data bus width. */
1391 if (nand->base.options & NAND_BUSWIDTH_16)
1392 smcconf->mode |= ATMEL_SMC_MODE_DBW_16;
1393
1394 /* Operate in NRD/NWE READ/WRITEMODE. */
1395 smcconf->mode |= ATMEL_SMC_MODE_READMODE_NRD |
1396 ATMEL_SMC_MODE_WRITEMODE_NWE;
1397
1398 return 0;
1399}
1400
1401static int atmel_smc_nand_setup_data_interface(struct atmel_nand *nand,
1402 int csline,
1403 const struct nand_data_interface *conf)
1404{
1405 struct atmel_nand_controller *nc;
1406 struct atmel_smc_cs_conf smcconf;
1407 struct atmel_nand_cs *cs;
1408 int ret;
1409
1410 nc = to_nand_controller(nand->base.controller);
1411
1412 ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf);
1413 if (ret)
1414 return ret;
1415
1416 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1417 return 0;
1418
1419 cs = &nand->cs[csline];
1420 cs->smcconf = smcconf;
1421 atmel_smc_cs_conf_apply(nc->smc, cs->id, &cs->smcconf);
1422
1423 return 0;
1424}
1425
1426static int atmel_hsmc_nand_setup_data_interface(struct atmel_nand *nand,
1427 int csline,
1428 const struct nand_data_interface *conf)
1429{
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02001430 struct atmel_hsmc_nand_controller *nc;
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001431 struct atmel_smc_cs_conf smcconf;
1432 struct atmel_nand_cs *cs;
1433 int ret;
1434
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02001435 nc = to_hsmc_nand_controller(nand->base.controller);
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001436
1437 ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf);
1438 if (ret)
1439 return ret;
1440
1441 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1442 return 0;
1443
1444 cs = &nand->cs[csline];
1445 cs->smcconf = smcconf;
1446
1447 if (cs->rb.type == ATMEL_NAND_NATIVE_RB)
1448 cs->smcconf.timings |= ATMEL_HSMC_TIMINGS_RBNSEL(cs->rb.id);
1449
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02001450 atmel_hsmc_cs_conf_apply(nc->base.smc, nc->hsmc_layout, cs->id,
1451 &cs->smcconf);
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001452
1453 return 0;
1454}
1455
1456static int atmel_nand_setup_data_interface(struct mtd_info *mtd, int csline,
1457 const struct nand_data_interface *conf)
1458{
1459 struct nand_chip *chip = mtd_to_nand(mtd);
1460 struct atmel_nand *nand = to_atmel_nand(chip);
1461 struct atmel_nand_controller *nc;
1462
1463 nc = to_nand_controller(nand->base.controller);
1464
1465 if (csline >= nand->numcs ||
1466 (csline < 0 && csline != NAND_DATA_IFACE_CHECK_ONLY))
1467 return -EINVAL;
1468
1469 return nc->caps->ops->setup_data_interface(nand, csline, conf);
1470}
1471
Boris Brezillonf88fc122017-03-16 09:02:40 +01001472static void atmel_nand_init(struct atmel_nand_controller *nc,
1473 struct atmel_nand *nand)
1474{
1475 struct nand_chip *chip = &nand->base;
1476 struct mtd_info *mtd = nand_to_mtd(chip);
1477
1478 mtd->dev.parent = nc->dev;
1479 nand->base.controller = &nc->base;
1480
1481 chip->cmd_ctrl = atmel_nand_cmd_ctrl;
1482 chip->read_byte = atmel_nand_read_byte;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001483 chip->write_byte = atmel_nand_write_byte;
1484 chip->read_buf = atmel_nand_read_buf;
1485 chip->write_buf = atmel_nand_write_buf;
1486 chip->select_chip = atmel_nand_select_chip;
1487
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001488 if (nc->mck && nc->caps->ops->setup_data_interface)
1489 chip->setup_data_interface = atmel_nand_setup_data_interface;
1490
Boris Brezillonf88fc122017-03-16 09:02:40 +01001491 /* Some NANDs require a longer delay than the default one (20us). */
1492 chip->chip_delay = 40;
1493
1494 /*
1495 * Use a bounce buffer when the buffer passed by the MTD user is not
1496 * suitable for DMA.
1497 */
1498 if (nc->dmac)
1499 chip->options |= NAND_USE_BOUNCE_BUFFER;
1500
1501 /* Default to HW ECC if pmecc is available. */
1502 if (nc->pmecc)
1503 chip->ecc.mode = NAND_ECC_HW;
1504}
1505
1506static void atmel_smc_nand_init(struct atmel_nand_controller *nc,
1507 struct atmel_nand *nand)
1508{
1509 struct nand_chip *chip = &nand->base;
1510 struct atmel_smc_nand_controller *smc_nc;
1511 int i;
1512
1513 atmel_nand_init(nc, nand);
1514
1515 smc_nc = to_smc_nand_controller(chip->controller);
1516 if (!smc_nc->matrix)
1517 return;
1518
1519 /* Attach the CS to the NAND Flash logic. */
1520 for (i = 0; i < nand->numcs; i++)
1521 regmap_update_bits(smc_nc->matrix, smc_nc->ebi_csa_offs,
1522 BIT(nand->cs[i].id), BIT(nand->cs[i].id));
1523}
1524
1525static void atmel_hsmc_nand_init(struct atmel_nand_controller *nc,
1526 struct atmel_nand *nand)
1527{
1528 struct nand_chip *chip = &nand->base;
1529
1530 atmel_nand_init(nc, nand);
1531
1532 /* Overload some methods for the HSMC controller. */
1533 chip->cmd_ctrl = atmel_hsmc_nand_cmd_ctrl;
1534 chip->select_chip = atmel_hsmc_nand_select_chip;
1535}
1536
Miquel Raynal79282252018-07-25 15:31:40 +02001537static int atmel_nand_controller_remove_nand(struct atmel_nand *nand)
Boris Brezillonf88fc122017-03-16 09:02:40 +01001538{
1539 struct nand_chip *chip = &nand->base;
1540 struct mtd_info *mtd = nand_to_mtd(chip);
1541 int ret;
1542
1543 ret = mtd_device_unregister(mtd);
1544 if (ret)
1545 return ret;
1546
1547 nand_cleanup(chip);
1548 list_del(&nand->node);
1549
1550 return 0;
1551}
1552
Boris Brezillonf88fc122017-03-16 09:02:40 +01001553static struct atmel_nand *atmel_nand_create(struct atmel_nand_controller *nc,
1554 struct device_node *np,
1555 int reg_cells)
1556{
1557 struct atmel_nand *nand;
1558 struct gpio_desc *gpio;
1559 int numcs, ret, i;
1560
1561 numcs = of_property_count_elems_of_size(np, "reg",
1562 reg_cells * sizeof(u32));
1563 if (numcs < 1) {
1564 dev_err(nc->dev, "Missing or invalid reg property\n");
1565 return ERR_PTR(-EINVAL);
1566 }
1567
Gustavo A. R. Silva2f91eb62018-08-23 20:09:38 -05001568 nand = devm_kzalloc(nc->dev, struct_size(nand, cs, numcs), GFP_KERNEL);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001569 if (!nand) {
1570 dev_err(nc->dev, "Failed to allocate NAND object\n");
1571 return ERR_PTR(-ENOMEM);
1572 }
1573
1574 nand->numcs = numcs;
1575
1576 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, "det", 0,
1577 &np->fwnode, GPIOD_IN,
1578 "nand-det");
1579 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1580 dev_err(nc->dev,
1581 "Failed to get detect gpio (err = %ld)\n",
1582 PTR_ERR(gpio));
1583 return ERR_CAST(gpio);
1584 }
1585
1586 if (!IS_ERR(gpio))
1587 nand->cdgpio = gpio;
1588
1589 for (i = 0; i < numcs; i++) {
1590 struct resource res;
1591 u32 val;
1592
1593 ret = of_address_to_resource(np, 0, &res);
1594 if (ret) {
1595 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1596 ret);
1597 return ERR_PTR(ret);
1598 }
1599
1600 ret = of_property_read_u32_index(np, "reg", i * reg_cells,
1601 &val);
1602 if (ret) {
1603 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1604 ret);
1605 return ERR_PTR(ret);
1606 }
1607
1608 nand->cs[i].id = val;
1609
1610 nand->cs[i].io.dma = res.start;
1611 nand->cs[i].io.virt = devm_ioremap_resource(nc->dev, &res);
1612 if (IS_ERR(nand->cs[i].io.virt))
1613 return ERR_CAST(nand->cs[i].io.virt);
1614
1615 if (!of_property_read_u32(np, "atmel,rb", &val)) {
1616 if (val > ATMEL_NFC_MAX_RB_ID)
1617 return ERR_PTR(-EINVAL);
1618
1619 nand->cs[i].rb.type = ATMEL_NAND_NATIVE_RB;
1620 nand->cs[i].rb.id = val;
1621 } else {
1622 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev,
1623 "rb", i, &np->fwnode,
1624 GPIOD_IN, "nand-rb");
1625 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1626 dev_err(nc->dev,
1627 "Failed to get R/B gpio (err = %ld)\n",
1628 PTR_ERR(gpio));
1629 return ERR_CAST(gpio);
1630 }
1631
1632 if (!IS_ERR(gpio)) {
1633 nand->cs[i].rb.type = ATMEL_NAND_GPIO_RB;
1634 nand->cs[i].rb.gpio = gpio;
1635 }
1636 }
1637
1638 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, "cs",
1639 i, &np->fwnode,
1640 GPIOD_OUT_HIGH,
1641 "nand-cs");
1642 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1643 dev_err(nc->dev,
1644 "Failed to get CS gpio (err = %ld)\n",
1645 PTR_ERR(gpio));
1646 return ERR_CAST(gpio);
1647 }
1648
1649 if (!IS_ERR(gpio))
1650 nand->cs[i].csgpio = gpio;
1651 }
1652
1653 nand_set_flash_node(&nand->base, np);
1654
1655 return nand;
1656}
1657
1658static int
1659atmel_nand_controller_add_nand(struct atmel_nand_controller *nc,
1660 struct atmel_nand *nand)
1661{
Miquel Raynal577e0102018-07-25 15:31:41 +02001662 struct nand_chip *chip = &nand->base;
1663 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001664 int ret;
1665
1666 /* No card inserted, skip this NAND. */
1667 if (nand->cdgpio && gpiod_get_value(nand->cdgpio)) {
1668 dev_info(nc->dev, "No SmartMedia card inserted.\n");
1669 return 0;
1670 }
1671
1672 nc->caps->ops->nand_init(nc, nand);
1673
Boris Brezillon00ad3782018-09-06 14:05:14 +02001674 ret = nand_scan(chip, nand->numcs);
Miquel Raynal79282252018-07-25 15:31:40 +02001675 if (ret) {
Miquel Raynal577e0102018-07-25 15:31:41 +02001676 dev_err(nc->dev, "NAND scan failed: %d\n", ret);
Miquel Raynal79282252018-07-25 15:31:40 +02001677 return ret;
1678 }
1679
1680 ret = mtd_device_register(mtd, NULL, 0);
1681 if (ret) {
1682 dev_err(nc->dev, "Failed to register mtd device: %d\n", ret);
1683 nand_cleanup(chip);
1684 return ret;
1685 }
1686
1687 list_add_tail(&nand->node, &nc->chips);
1688
1689 return 0;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001690}
1691
1692static int
1693atmel_nand_controller_remove_nands(struct atmel_nand_controller *nc)
1694{
1695 struct atmel_nand *nand, *tmp;
1696 int ret;
1697
1698 list_for_each_entry_safe(nand, tmp, &nc->chips, node) {
Miquel Raynal79282252018-07-25 15:31:40 +02001699 ret = atmel_nand_controller_remove_nand(nand);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001700 if (ret)
1701 return ret;
1702 }
1703
1704 return 0;
1705}
1706
1707static int
1708atmel_nand_controller_legacy_add_nands(struct atmel_nand_controller *nc)
1709{
1710 struct device *dev = nc->dev;
1711 struct platform_device *pdev = to_platform_device(dev);
1712 struct atmel_nand *nand;
1713 struct gpio_desc *gpio;
1714 struct resource *res;
1715
1716 /*
1717 * Legacy bindings only allow connecting a single NAND with a unique CS
1718 * line to the controller.
1719 */
1720 nand = devm_kzalloc(nc->dev, sizeof(*nand) + sizeof(*nand->cs),
1721 GFP_KERNEL);
1722 if (!nand)
1723 return -ENOMEM;
1724
1725 nand->numcs = 1;
1726
1727 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1728 nand->cs[0].io.virt = devm_ioremap_resource(dev, res);
1729 if (IS_ERR(nand->cs[0].io.virt))
1730 return PTR_ERR(nand->cs[0].io.virt);
1731
1732 nand->cs[0].io.dma = res->start;
1733
1734 /*
1735 * The old driver was hardcoding the CS id to 3 for all sama5
1736 * controllers. Since this id is only meaningful for the sama5
1737 * controller we can safely assign this id to 3 no matter the
1738 * controller.
1739 * If one wants to connect a NAND to a different CS line, he will
1740 * have to use the new bindings.
1741 */
1742 nand->cs[0].id = 3;
1743
1744 /* R/B GPIO. */
1745 gpio = devm_gpiod_get_index_optional(dev, NULL, 0, GPIOD_IN);
1746 if (IS_ERR(gpio)) {
1747 dev_err(dev, "Failed to get R/B gpio (err = %ld)\n",
1748 PTR_ERR(gpio));
1749 return PTR_ERR(gpio);
1750 }
1751
1752 if (gpio) {
1753 nand->cs[0].rb.type = ATMEL_NAND_GPIO_RB;
1754 nand->cs[0].rb.gpio = gpio;
1755 }
1756
1757 /* CS GPIO. */
1758 gpio = devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_HIGH);
1759 if (IS_ERR(gpio)) {
1760 dev_err(dev, "Failed to get CS gpio (err = %ld)\n",
1761 PTR_ERR(gpio));
1762 return PTR_ERR(gpio);
1763 }
1764
1765 nand->cs[0].csgpio = gpio;
1766
1767 /* Card detect GPIO. */
1768 gpio = devm_gpiod_get_index_optional(nc->dev, NULL, 2, GPIOD_IN);
1769 if (IS_ERR(gpio)) {
1770 dev_err(dev,
1771 "Failed to get detect gpio (err = %ld)\n",
1772 PTR_ERR(gpio));
1773 return PTR_ERR(gpio);
1774 }
1775
1776 nand->cdgpio = gpio;
1777
1778 nand_set_flash_node(&nand->base, nc->dev->of_node);
1779
1780 return atmel_nand_controller_add_nand(nc, nand);
1781}
1782
1783static int atmel_nand_controller_add_nands(struct atmel_nand_controller *nc)
1784{
1785 struct device_node *np, *nand_np;
1786 struct device *dev = nc->dev;
1787 int ret, reg_cells;
1788 u32 val;
1789
1790 /* We do not retrieve the SMC syscon when parsing old DTs. */
1791 if (nc->caps->legacy_of_bindings)
1792 return atmel_nand_controller_legacy_add_nands(nc);
1793
1794 np = dev->of_node;
1795
1796 ret = of_property_read_u32(np, "#address-cells", &val);
1797 if (ret) {
1798 dev_err(dev, "missing #address-cells property\n");
1799 return ret;
1800 }
1801
1802 reg_cells = val;
1803
1804 ret = of_property_read_u32(np, "#size-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 for_each_child_of_node(np, nand_np) {
1813 struct atmel_nand *nand;
1814
1815 nand = atmel_nand_create(nc, nand_np, reg_cells);
1816 if (IS_ERR(nand)) {
1817 ret = PTR_ERR(nand);
1818 goto err;
1819 }
1820
1821 ret = atmel_nand_controller_add_nand(nc, nand);
1822 if (ret)
1823 goto err;
1824 }
1825
1826 return 0;
1827
1828err:
1829 atmel_nand_controller_remove_nands(nc);
1830
1831 return ret;
1832}
1833
1834static void atmel_nand_controller_cleanup(struct atmel_nand_controller *nc)
1835{
1836 if (nc->dmac)
1837 dma_release_channel(nc->dmac);
1838
1839 clk_put(nc->mck);
1840}
1841
1842static const struct of_device_id atmel_matrix_of_ids[] = {
1843 {
1844 .compatible = "atmel,at91sam9260-matrix",
1845 .data = (void *)AT91SAM9260_MATRIX_EBICSA,
1846 },
1847 {
1848 .compatible = "atmel,at91sam9261-matrix",
1849 .data = (void *)AT91SAM9261_MATRIX_EBICSA,
1850 },
1851 {
1852 .compatible = "atmel,at91sam9263-matrix",
1853 .data = (void *)AT91SAM9263_MATRIX_EBI0CSA,
1854 },
1855 {
1856 .compatible = "atmel,at91sam9rl-matrix",
1857 .data = (void *)AT91SAM9RL_MATRIX_EBICSA,
1858 },
1859 {
1860 .compatible = "atmel,at91sam9g45-matrix",
1861 .data = (void *)AT91SAM9G45_MATRIX_EBICSA,
1862 },
1863 {
1864 .compatible = "atmel,at91sam9n12-matrix",
1865 .data = (void *)AT91SAM9N12_MATRIX_EBICSA,
1866 },
1867 {
1868 .compatible = "atmel,at91sam9x5-matrix",
1869 .data = (void *)AT91SAM9X5_MATRIX_EBICSA,
1870 },
Christophe Jaillet038e8ad6e2017-04-11 07:22:52 +02001871 { /* sentinel */ },
Boris Brezillonf88fc122017-03-16 09:02:40 +01001872};
1873
Miquel Raynal577e0102018-07-25 15:31:41 +02001874static int atmel_nand_attach_chip(struct nand_chip *chip)
1875{
1876 struct atmel_nand_controller *nc = to_nand_controller(chip->controller);
1877 struct atmel_nand *nand = to_atmel_nand(chip);
1878 struct mtd_info *mtd = nand_to_mtd(chip);
1879 int ret;
1880
1881 ret = nc->caps->ops->ecc_init(chip);
1882 if (ret)
1883 return ret;
1884
1885 if (nc->caps->legacy_of_bindings || !nc->dev->of_node) {
1886 /*
1887 * We keep the MTD name unchanged to avoid breaking platforms
1888 * where the MTD cmdline parser is used and the bootloader
1889 * has not been updated to use the new naming scheme.
1890 */
1891 mtd->name = "atmel_nand";
1892 } else if (!mtd->name) {
1893 /*
1894 * If the new bindings are used and the bootloader has not been
1895 * updated to pass a new mtdparts parameter on the cmdline, you
1896 * should define the following property in your nand node:
1897 *
1898 * label = "atmel_nand";
1899 *
1900 * This way, mtd->name will be set by the core when
1901 * nand_set_flash_node() is called.
1902 */
1903 mtd->name = devm_kasprintf(nc->dev, GFP_KERNEL,
1904 "%s:nand.%d", dev_name(nc->dev),
1905 nand->cs[0].id);
1906 if (!mtd->name) {
1907 dev_err(nc->dev, "Failed to allocate mtd->name\n");
1908 return -ENOMEM;
1909 }
1910 }
1911
1912 return 0;
1913}
1914
1915static const struct nand_controller_ops atmel_nand_controller_ops = {
1916 .attach_chip = atmel_nand_attach_chip,
1917};
1918
Boris Brezillonf88fc122017-03-16 09:02:40 +01001919static int atmel_nand_controller_init(struct atmel_nand_controller *nc,
1920 struct platform_device *pdev,
1921 const struct atmel_nand_controller_caps *caps)
1922{
1923 struct device *dev = &pdev->dev;
1924 struct device_node *np = dev->of_node;
1925 int ret;
1926
Miquel Raynal7da45132018-07-17 09:08:02 +02001927 nand_controller_init(&nc->base);
Miquel Raynal577e0102018-07-25 15:31:41 +02001928 nc->base.ops = &atmel_nand_controller_ops;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001929 INIT_LIST_HEAD(&nc->chips);
1930 nc->dev = dev;
1931 nc->caps = caps;
1932
1933 platform_set_drvdata(pdev, nc);
1934
1935 nc->pmecc = devm_atmel_pmecc_get(dev);
1936 if (IS_ERR(nc->pmecc)) {
1937 ret = PTR_ERR(nc->pmecc);
1938 if (ret != -EPROBE_DEFER)
1939 dev_err(dev, "Could not get PMECC object (err = %d)\n",
1940 ret);
1941 return ret;
1942 }
1943
Peter Rosinefc63622018-03-29 15:10:54 +02001944 if (nc->caps->has_dma && !atmel_nand_avoid_dma) {
Boris Brezillonf88fc122017-03-16 09:02:40 +01001945 dma_cap_mask_t mask;
1946
1947 dma_cap_zero(mask);
1948 dma_cap_set(DMA_MEMCPY, mask);
1949
1950 nc->dmac = dma_request_channel(mask, NULL, NULL);
1951 if (!nc->dmac)
1952 dev_err(nc->dev, "Failed to request DMA channel\n");
1953 }
1954
1955 /* We do not retrieve the SMC syscon when parsing old DTs. */
1956 if (nc->caps->legacy_of_bindings)
1957 return 0;
1958
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001959 nc->mck = of_clk_get(dev->parent->of_node, 0);
1960 if (IS_ERR(nc->mck)) {
1961 dev_err(dev, "Failed to retrieve MCK clk\n");
1962 return PTR_ERR(nc->mck);
1963 }
1964
Boris Brezillonf88fc122017-03-16 09:02:40 +01001965 np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
1966 if (!np) {
1967 dev_err(dev, "Missing or invalid atmel,smc property\n");
1968 return -EINVAL;
1969 }
1970
1971 nc->smc = syscon_node_to_regmap(np);
1972 of_node_put(np);
1973 if (IS_ERR(nc->smc)) {
Dan Carpenter70106dd2017-04-04 11:15:46 +03001974 ret = PTR_ERR(nc->smc);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001975 dev_err(dev, "Could not get SMC regmap (err = %d)\n", ret);
1976 return ret;
1977 }
1978
1979 return 0;
1980}
1981
1982static int
1983atmel_smc_nand_controller_init(struct atmel_smc_nand_controller *nc)
1984{
1985 struct device *dev = nc->base.dev;
1986 const struct of_device_id *match;
1987 struct device_node *np;
1988 int ret;
1989
1990 /* We do not retrieve the matrix syscon when parsing old DTs. */
1991 if (nc->base.caps->legacy_of_bindings)
1992 return 0;
1993
1994 np = of_parse_phandle(dev->parent->of_node, "atmel,matrix", 0);
1995 if (!np)
1996 return 0;
1997
1998 match = of_match_node(atmel_matrix_of_ids, np);
1999 if (!match) {
2000 of_node_put(np);
2001 return 0;
2002 }
2003
2004 nc->matrix = syscon_node_to_regmap(np);
2005 of_node_put(np);
2006 if (IS_ERR(nc->matrix)) {
Dan Carpenter70106dd2017-04-04 11:15:46 +03002007 ret = PTR_ERR(nc->matrix);
Boris Brezillonf88fc122017-03-16 09:02:40 +01002008 dev_err(dev, "Could not get Matrix regmap (err = %d)\n", ret);
2009 return ret;
2010 }
2011
Boris Brezillone6848512018-07-09 22:09:22 +02002012 nc->ebi_csa_offs = (uintptr_t)match->data;
Boris Brezillonf88fc122017-03-16 09:02:40 +01002013
2014 /*
2015 * The at91sam9263 has 2 EBIs, if the NAND controller is under EBI1
2016 * add 4 to ->ebi_csa_offs.
2017 */
2018 if (of_device_is_compatible(dev->parent->of_node,
2019 "atmel,at91sam9263-ebi1"))
2020 nc->ebi_csa_offs += 4;
2021
2022 return 0;
2023}
2024
2025static int
2026atmel_hsmc_nand_controller_legacy_init(struct atmel_hsmc_nand_controller *nc)
2027{
2028 struct regmap_config regmap_conf = {
2029 .reg_bits = 32,
2030 .val_bits = 32,
2031 .reg_stride = 4,
2032 };
2033
2034 struct device *dev = nc->base.dev;
2035 struct device_node *nand_np, *nfc_np;
2036 void __iomem *iomem;
2037 struct resource res;
2038 int ret;
2039
2040 nand_np = dev->of_node;
2041 nfc_np = of_find_compatible_node(dev->of_node, NULL,
2042 "atmel,sama5d3-nfc");
2043
2044 nc->clk = of_clk_get(nfc_np, 0);
2045 if (IS_ERR(nc->clk)) {
2046 ret = PTR_ERR(nc->clk);
2047 dev_err(dev, "Failed to retrieve HSMC clock (err = %d)\n",
2048 ret);
2049 goto out;
2050 }
2051
2052 ret = clk_prepare_enable(nc->clk);
2053 if (ret) {
2054 dev_err(dev, "Failed to enable the HSMC clock (err = %d)\n",
2055 ret);
2056 goto out;
2057 }
2058
2059 nc->irq = of_irq_get(nand_np, 0);
Sergei Shtylyov892dd182017-08-06 00:14:28 +03002060 if (nc->irq <= 0) {
2061 ret = nc->irq ?: -ENXIO;
Boris Brezillonf88fc122017-03-16 09:02:40 +01002062 if (ret != -EPROBE_DEFER)
2063 dev_err(dev, "Failed to get IRQ number (err = %d)\n",
2064 ret);
2065 goto out;
2066 }
2067
2068 ret = of_address_to_resource(nfc_np, 0, &res);
2069 if (ret) {
2070 dev_err(dev, "Invalid or missing NFC IO resource (err = %d)\n",
2071 ret);
2072 goto out;
2073 }
2074
2075 iomem = devm_ioremap_resource(dev, &res);
2076 if (IS_ERR(iomem)) {
2077 ret = PTR_ERR(iomem);
2078 goto out;
2079 }
2080
2081 regmap_conf.name = "nfc-io";
2082 regmap_conf.max_register = resource_size(&res) - 4;
2083 nc->io = devm_regmap_init_mmio(dev, iomem, &regmap_conf);
2084 if (IS_ERR(nc->io)) {
2085 ret = PTR_ERR(nc->io);
2086 dev_err(dev, "Could not create NFC IO regmap (err = %d)\n",
2087 ret);
2088 goto out;
2089 }
2090
2091 ret = of_address_to_resource(nfc_np, 1, &res);
2092 if (ret) {
2093 dev_err(dev, "Invalid or missing HSMC resource (err = %d)\n",
2094 ret);
2095 goto out;
2096 }
2097
2098 iomem = devm_ioremap_resource(dev, &res);
2099 if (IS_ERR(iomem)) {
2100 ret = PTR_ERR(iomem);
2101 goto out;
2102 }
2103
2104 regmap_conf.name = "smc";
2105 regmap_conf.max_register = resource_size(&res) - 4;
2106 nc->base.smc = devm_regmap_init_mmio(dev, iomem, &regmap_conf);
2107 if (IS_ERR(nc->base.smc)) {
2108 ret = PTR_ERR(nc->base.smc);
2109 dev_err(dev, "Could not create NFC IO regmap (err = %d)\n",
2110 ret);
2111 goto out;
2112 }
2113
2114 ret = of_address_to_resource(nfc_np, 2, &res);
2115 if (ret) {
2116 dev_err(dev, "Invalid or missing SRAM resource (err = %d)\n",
2117 ret);
2118 goto out;
2119 }
2120
2121 nc->sram.virt = devm_ioremap_resource(dev, &res);
2122 if (IS_ERR(nc->sram.virt)) {
2123 ret = PTR_ERR(nc->sram.virt);
2124 goto out;
2125 }
2126
2127 nc->sram.dma = res.start;
2128
2129out:
2130 of_node_put(nfc_np);
2131
2132 return ret;
2133}
2134
2135static int
2136atmel_hsmc_nand_controller_init(struct atmel_hsmc_nand_controller *nc)
2137{
2138 struct device *dev = nc->base.dev;
2139 struct device_node *np;
2140 int ret;
2141
2142 np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
2143 if (!np) {
2144 dev_err(dev, "Missing or invalid atmel,smc property\n");
2145 return -EINVAL;
2146 }
2147
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02002148 nc->hsmc_layout = atmel_hsmc_get_reg_layout(np);
2149
Boris Brezillonf88fc122017-03-16 09:02:40 +01002150 nc->irq = of_irq_get(np, 0);
2151 of_node_put(np);
Sergei Shtylyov892dd182017-08-06 00:14:28 +03002152 if (nc->irq <= 0) {
2153 ret = nc->irq ?: -ENXIO;
2154 if (ret != -EPROBE_DEFER)
Boris Brezillonf88fc122017-03-16 09:02:40 +01002155 dev_err(dev, "Failed to get IRQ number (err = %d)\n",
Sergei Shtylyov892dd182017-08-06 00:14:28 +03002156 ret);
2157 return ret;
Boris Brezillonf88fc122017-03-16 09:02:40 +01002158 }
2159
2160 np = of_parse_phandle(dev->of_node, "atmel,nfc-io", 0);
2161 if (!np) {
2162 dev_err(dev, "Missing or invalid atmel,nfc-io property\n");
2163 return -EINVAL;
2164 }
2165
2166 nc->io = syscon_node_to_regmap(np);
2167 of_node_put(np);
2168 if (IS_ERR(nc->io)) {
2169 ret = PTR_ERR(nc->io);
2170 dev_err(dev, "Could not get NFC IO regmap (err = %d)\n", ret);
2171 return ret;
2172 }
2173
2174 nc->sram.pool = of_gen_pool_get(nc->base.dev->of_node,
2175 "atmel,nfc-sram", 0);
2176 if (!nc->sram.pool) {
2177 dev_err(nc->base.dev, "Missing SRAM\n");
2178 return -ENOMEM;
2179 }
2180
Boris Brezillond28395c2018-07-09 22:09:23 +02002181 nc->sram.virt = (void __iomem *)gen_pool_dma_alloc(nc->sram.pool,
2182 ATMEL_NFC_SRAM_SIZE,
2183 &nc->sram.dma);
Boris Brezillonf88fc122017-03-16 09:02:40 +01002184 if (!nc->sram.virt) {
2185 dev_err(nc->base.dev,
2186 "Could not allocate memory from the NFC SRAM pool\n");
2187 return -ENOMEM;
2188 }
2189
2190 return 0;
2191}
2192
2193static int
2194atmel_hsmc_nand_controller_remove(struct atmel_nand_controller *nc)
2195{
2196 struct atmel_hsmc_nand_controller *hsmc_nc;
2197 int ret;
2198
2199 ret = atmel_nand_controller_remove_nands(nc);
2200 if (ret)
2201 return ret;
2202
2203 hsmc_nc = container_of(nc, struct atmel_hsmc_nand_controller, base);
2204 if (hsmc_nc->sram.pool)
2205 gen_pool_free(hsmc_nc->sram.pool,
2206 (unsigned long)hsmc_nc->sram.virt,
2207 ATMEL_NFC_SRAM_SIZE);
2208
2209 if (hsmc_nc->clk) {
2210 clk_disable_unprepare(hsmc_nc->clk);
2211 clk_put(hsmc_nc->clk);
2212 }
2213
2214 atmel_nand_controller_cleanup(nc);
2215
2216 return 0;
2217}
2218
2219static int atmel_hsmc_nand_controller_probe(struct platform_device *pdev,
2220 const struct atmel_nand_controller_caps *caps)
2221{
2222 struct device *dev = &pdev->dev;
2223 struct atmel_hsmc_nand_controller *nc;
2224 int ret;
2225
2226 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2227 if (!nc)
2228 return -ENOMEM;
2229
2230 ret = atmel_nand_controller_init(&nc->base, pdev, caps);
2231 if (ret)
2232 return ret;
2233
2234 if (caps->legacy_of_bindings)
2235 ret = atmel_hsmc_nand_controller_legacy_init(nc);
2236 else
2237 ret = atmel_hsmc_nand_controller_init(nc);
2238
2239 if (ret)
2240 return ret;
2241
2242 /* Make sure all irqs are masked before registering our IRQ handler. */
2243 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, 0xffffffff);
2244 ret = devm_request_irq(dev, nc->irq, atmel_nfc_interrupt,
2245 IRQF_SHARED, "nfc", nc);
2246 if (ret) {
2247 dev_err(dev,
2248 "Could not get register NFC interrupt handler (err = %d)\n",
2249 ret);
2250 goto err;
2251 }
2252
2253 /* Initial NFC configuration. */
2254 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CFG,
2255 ATMEL_HSMC_NFC_CFG_DTO_MAX);
2256
2257 ret = atmel_nand_controller_add_nands(&nc->base);
2258 if (ret)
2259 goto err;
2260
2261 return 0;
2262
2263err:
2264 atmel_hsmc_nand_controller_remove(&nc->base);
2265
2266 return ret;
2267}
2268
2269static const struct atmel_nand_controller_ops atmel_hsmc_nc_ops = {
2270 .probe = atmel_hsmc_nand_controller_probe,
2271 .remove = atmel_hsmc_nand_controller_remove,
2272 .ecc_init = atmel_hsmc_nand_ecc_init,
2273 .nand_init = atmel_hsmc_nand_init,
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002274 .setup_data_interface = atmel_hsmc_nand_setup_data_interface,
Boris Brezillonf88fc122017-03-16 09:02:40 +01002275};
2276
2277static const struct atmel_nand_controller_caps atmel_sama5_nc_caps = {
2278 .has_dma = true,
2279 .ale_offs = BIT(21),
2280 .cle_offs = BIT(22),
2281 .ops = &atmel_hsmc_nc_ops,
2282};
2283
2284/* Only used to parse old bindings. */
2285static const struct atmel_nand_controller_caps atmel_sama5_nand_caps = {
2286 .has_dma = true,
2287 .ale_offs = BIT(21),
2288 .cle_offs = BIT(22),
2289 .ops = &atmel_hsmc_nc_ops,
2290 .legacy_of_bindings = true,
2291};
2292
2293static int atmel_smc_nand_controller_probe(struct platform_device *pdev,
2294 const struct atmel_nand_controller_caps *caps)
2295{
2296 struct device *dev = &pdev->dev;
2297 struct atmel_smc_nand_controller *nc;
2298 int ret;
2299
2300 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2301 if (!nc)
2302 return -ENOMEM;
2303
2304 ret = atmel_nand_controller_init(&nc->base, pdev, caps);
2305 if (ret)
2306 return ret;
2307
2308 ret = atmel_smc_nand_controller_init(nc);
2309 if (ret)
2310 return ret;
2311
2312 return atmel_nand_controller_add_nands(&nc->base);
2313}
2314
2315static int
2316atmel_smc_nand_controller_remove(struct atmel_nand_controller *nc)
2317{
2318 int ret;
2319
2320 ret = atmel_nand_controller_remove_nands(nc);
2321 if (ret)
2322 return ret;
2323
2324 atmel_nand_controller_cleanup(nc);
2325
2326 return 0;
2327}
2328
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002329/*
2330 * The SMC reg layout of at91rm9200 is completely different which prevents us
2331 * from re-using atmel_smc_nand_setup_data_interface() for the
2332 * ->setup_data_interface() hook.
2333 * At this point, there's no support for the at91rm9200 SMC IP, so we leave
2334 * ->setup_data_interface() unassigned.
2335 */
2336static const struct atmel_nand_controller_ops at91rm9200_nc_ops = {
Boris Brezillonf88fc122017-03-16 09:02:40 +01002337 .probe = atmel_smc_nand_controller_probe,
2338 .remove = atmel_smc_nand_controller_remove,
2339 .ecc_init = atmel_nand_ecc_init,
2340 .nand_init = atmel_smc_nand_init,
2341};
2342
2343static const struct atmel_nand_controller_caps atmel_rm9200_nc_caps = {
2344 .ale_offs = BIT(21),
2345 .cle_offs = BIT(22),
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002346 .ops = &at91rm9200_nc_ops,
2347};
2348
2349static const struct atmel_nand_controller_ops atmel_smc_nc_ops = {
2350 .probe = atmel_smc_nand_controller_probe,
2351 .remove = atmel_smc_nand_controller_remove,
2352 .ecc_init = atmel_nand_ecc_init,
2353 .nand_init = atmel_smc_nand_init,
2354 .setup_data_interface = atmel_smc_nand_setup_data_interface,
2355};
2356
2357static const struct atmel_nand_controller_caps atmel_sam9260_nc_caps = {
2358 .ale_offs = BIT(21),
2359 .cle_offs = BIT(22),
Boris Brezillonf88fc122017-03-16 09:02:40 +01002360 .ops = &atmel_smc_nc_ops,
2361};
2362
2363static const struct atmel_nand_controller_caps atmel_sam9261_nc_caps = {
2364 .ale_offs = BIT(22),
2365 .cle_offs = BIT(21),
2366 .ops = &atmel_smc_nc_ops,
2367};
2368
2369static const struct atmel_nand_controller_caps atmel_sam9g45_nc_caps = {
2370 .has_dma = true,
2371 .ale_offs = BIT(21),
2372 .cle_offs = BIT(22),
2373 .ops = &atmel_smc_nc_ops,
2374};
2375
2376/* Only used to parse old bindings. */
2377static const struct atmel_nand_controller_caps atmel_rm9200_nand_caps = {
2378 .ale_offs = BIT(21),
2379 .cle_offs = BIT(22),
2380 .ops = &atmel_smc_nc_ops,
2381 .legacy_of_bindings = true,
2382};
2383
2384static const struct atmel_nand_controller_caps atmel_sam9261_nand_caps = {
2385 .ale_offs = BIT(22),
2386 .cle_offs = BIT(21),
2387 .ops = &atmel_smc_nc_ops,
2388 .legacy_of_bindings = true,
2389};
2390
2391static const struct atmel_nand_controller_caps atmel_sam9g45_nand_caps = {
2392 .has_dma = true,
2393 .ale_offs = BIT(21),
2394 .cle_offs = BIT(22),
2395 .ops = &atmel_smc_nc_ops,
2396 .legacy_of_bindings = true,
2397};
2398
2399static const struct of_device_id atmel_nand_controller_of_ids[] = {
2400 {
2401 .compatible = "atmel,at91rm9200-nand-controller",
2402 .data = &atmel_rm9200_nc_caps,
2403 },
2404 {
2405 .compatible = "atmel,at91sam9260-nand-controller",
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002406 .data = &atmel_sam9260_nc_caps,
Boris Brezillonf88fc122017-03-16 09:02:40 +01002407 },
2408 {
2409 .compatible = "atmel,at91sam9261-nand-controller",
2410 .data = &atmel_sam9261_nc_caps,
2411 },
2412 {
2413 .compatible = "atmel,at91sam9g45-nand-controller",
2414 .data = &atmel_sam9g45_nc_caps,
2415 },
2416 {
2417 .compatible = "atmel,sama5d3-nand-controller",
2418 .data = &atmel_sama5_nc_caps,
2419 },
2420 /* Support for old/deprecated bindings: */
2421 {
2422 .compatible = "atmel,at91rm9200-nand",
2423 .data = &atmel_rm9200_nand_caps,
2424 },
2425 {
2426 .compatible = "atmel,sama5d4-nand",
2427 .data = &atmel_rm9200_nand_caps,
2428 },
2429 {
2430 .compatible = "atmel,sama5d2-nand",
2431 .data = &atmel_rm9200_nand_caps,
2432 },
2433 { /* sentinel */ },
2434};
2435MODULE_DEVICE_TABLE(of, atmel_nand_controller_of_ids);
2436
2437static int atmel_nand_controller_probe(struct platform_device *pdev)
2438{
2439 const struct atmel_nand_controller_caps *caps;
2440
2441 if (pdev->id_entry)
2442 caps = (void *)pdev->id_entry->driver_data;
2443 else
2444 caps = of_device_get_match_data(&pdev->dev);
2445
2446 if (!caps) {
2447 dev_err(&pdev->dev, "Could not retrieve NFC caps\n");
2448 return -EINVAL;
2449 }
2450
2451 if (caps->legacy_of_bindings) {
2452 u32 ale_offs = 21;
2453
2454 /*
2455 * If we are parsing legacy DT props and the DT contains a
2456 * valid NFC node, forward the request to the sama5 logic.
2457 */
2458 if (of_find_compatible_node(pdev->dev.of_node, NULL,
2459 "atmel,sama5d3-nfc"))
2460 caps = &atmel_sama5_nand_caps;
2461
2462 /*
2463 * Even if the compatible says we are dealing with an
2464 * at91rm9200 controller, the atmel,nand-has-dma specify that
2465 * this controller supports DMA, which means we are in fact
2466 * dealing with an at91sam9g45+ controller.
2467 */
2468 if (!caps->has_dma &&
2469 of_property_read_bool(pdev->dev.of_node,
2470 "atmel,nand-has-dma"))
2471 caps = &atmel_sam9g45_nand_caps;
2472
2473 /*
2474 * All SoCs except the at91sam9261 are assigning ALE to A21 and
2475 * CLE to A22. If atmel,nand-addr-offset != 21 this means we're
2476 * actually dealing with an at91sam9261 controller.
2477 */
2478 of_property_read_u32(pdev->dev.of_node,
2479 "atmel,nand-addr-offset", &ale_offs);
2480 if (ale_offs != 21)
2481 caps = &atmel_sam9261_nand_caps;
2482 }
2483
2484 return caps->ops->probe(pdev, caps);
2485}
2486
2487static int atmel_nand_controller_remove(struct platform_device *pdev)
2488{
2489 struct atmel_nand_controller *nc = platform_get_drvdata(pdev);
2490
2491 return nc->caps->ops->remove(nc);
2492}
2493
Arnd Bergmann05b6c232017-05-31 10:19:26 +02002494static __maybe_unused int atmel_nand_controller_resume(struct device *dev)
Boris Brezillon6e532af2017-03-16 09:36:00 +01002495{
2496 struct atmel_nand_controller *nc = dev_get_drvdata(dev);
2497 struct atmel_nand *nand;
2498
Romain Izard143b0ab2017-09-28 11:46:23 +02002499 if (nc->pmecc)
2500 atmel_pmecc_reset(nc->pmecc);
2501
Boris Brezillon6e532af2017-03-16 09:36:00 +01002502 list_for_each_entry(nand, &nc->chips, node) {
2503 int i;
2504
2505 for (i = 0; i < nand->numcs; i++)
2506 nand_reset(&nand->base, i);
2507 }
2508
2509 return 0;
2510}
2511
2512static SIMPLE_DEV_PM_OPS(atmel_nand_controller_pm_ops, NULL,
2513 atmel_nand_controller_resume);
2514
Boris Brezillonf88fc122017-03-16 09:02:40 +01002515static struct platform_driver atmel_nand_controller_driver = {
2516 .driver = {
2517 .name = "atmel-nand-controller",
2518 .of_match_table = of_match_ptr(atmel_nand_controller_of_ids),
Boris Brezillon1533bfa2017-10-05 18:57:24 +02002519 .pm = &atmel_nand_controller_pm_ops,
Boris Brezillonf88fc122017-03-16 09:02:40 +01002520 },
2521 .probe = atmel_nand_controller_probe,
2522 .remove = atmel_nand_controller_remove,
2523};
2524module_platform_driver(atmel_nand_controller_driver);
2525
2526MODULE_LICENSE("GPL");
2527MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
2528MODULE_DESCRIPTION("NAND Flash Controller driver for Atmel SoCs");
2529MODULE_ALIAS("platform:atmel-nand-controller");