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