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