blob: 8b9e05ac24ee5c7b9c9add8bb1258a62f0cdde8e [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
Gustavo A. R. Silva2f91eb62018-08-23 20:09:38 -05001580 nand = devm_kzalloc(nc->dev, struct_size(nand, cs, numcs), GFP_KERNEL);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001581 if (!nand) {
1582 dev_err(nc->dev, "Failed to allocate NAND object\n");
1583 return ERR_PTR(-ENOMEM);
1584 }
1585
1586 nand->numcs = numcs;
1587
1588 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, "det", 0,
1589 &np->fwnode, GPIOD_IN,
1590 "nand-det");
1591 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1592 dev_err(nc->dev,
1593 "Failed to get detect gpio (err = %ld)\n",
1594 PTR_ERR(gpio));
1595 return ERR_CAST(gpio);
1596 }
1597
1598 if (!IS_ERR(gpio))
1599 nand->cdgpio = gpio;
1600
1601 for (i = 0; i < numcs; i++) {
1602 struct resource res;
1603 u32 val;
1604
1605 ret = of_address_to_resource(np, 0, &res);
1606 if (ret) {
1607 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1608 ret);
1609 return ERR_PTR(ret);
1610 }
1611
1612 ret = of_property_read_u32_index(np, "reg", i * reg_cells,
1613 &val);
1614 if (ret) {
1615 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1616 ret);
1617 return ERR_PTR(ret);
1618 }
1619
1620 nand->cs[i].id = val;
1621
1622 nand->cs[i].io.dma = res.start;
1623 nand->cs[i].io.virt = devm_ioremap_resource(nc->dev, &res);
1624 if (IS_ERR(nand->cs[i].io.virt))
1625 return ERR_CAST(nand->cs[i].io.virt);
1626
1627 if (!of_property_read_u32(np, "atmel,rb", &val)) {
1628 if (val > ATMEL_NFC_MAX_RB_ID)
1629 return ERR_PTR(-EINVAL);
1630
1631 nand->cs[i].rb.type = ATMEL_NAND_NATIVE_RB;
1632 nand->cs[i].rb.id = val;
1633 } else {
1634 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev,
1635 "rb", i, &np->fwnode,
1636 GPIOD_IN, "nand-rb");
1637 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1638 dev_err(nc->dev,
1639 "Failed to get R/B gpio (err = %ld)\n",
1640 PTR_ERR(gpio));
1641 return ERR_CAST(gpio);
1642 }
1643
1644 if (!IS_ERR(gpio)) {
1645 nand->cs[i].rb.type = ATMEL_NAND_GPIO_RB;
1646 nand->cs[i].rb.gpio = gpio;
1647 }
1648 }
1649
1650 gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, "cs",
1651 i, &np->fwnode,
1652 GPIOD_OUT_HIGH,
1653 "nand-cs");
1654 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1655 dev_err(nc->dev,
1656 "Failed to get CS gpio (err = %ld)\n",
1657 PTR_ERR(gpio));
1658 return ERR_CAST(gpio);
1659 }
1660
1661 if (!IS_ERR(gpio))
1662 nand->cs[i].csgpio = gpio;
1663 }
1664
1665 nand_set_flash_node(&nand->base, np);
1666
1667 return nand;
1668}
1669
1670static int
1671atmel_nand_controller_add_nand(struct atmel_nand_controller *nc,
1672 struct atmel_nand *nand)
1673{
Miquel Raynal577e0102018-07-25 15:31:41 +02001674 struct nand_chip *chip = &nand->base;
1675 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001676 int ret;
1677
1678 /* No card inserted, skip this NAND. */
1679 if (nand->cdgpio && gpiod_get_value(nand->cdgpio)) {
1680 dev_info(nc->dev, "No SmartMedia card inserted.\n");
1681 return 0;
1682 }
1683
1684 nc->caps->ops->nand_init(nc, nand);
1685
Miquel Raynal577e0102018-07-25 15:31:41 +02001686 ret = nand_scan(mtd, nand->numcs);
Miquel Raynal79282252018-07-25 15:31:40 +02001687 if (ret) {
Miquel Raynal577e0102018-07-25 15:31:41 +02001688 dev_err(nc->dev, "NAND scan failed: %d\n", ret);
Miquel Raynal79282252018-07-25 15:31:40 +02001689 return ret;
1690 }
1691
1692 ret = mtd_device_register(mtd, NULL, 0);
1693 if (ret) {
1694 dev_err(nc->dev, "Failed to register mtd device: %d\n", ret);
1695 nand_cleanup(chip);
1696 return ret;
1697 }
1698
1699 list_add_tail(&nand->node, &nc->chips);
1700
1701 return 0;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001702}
1703
1704static int
1705atmel_nand_controller_remove_nands(struct atmel_nand_controller *nc)
1706{
1707 struct atmel_nand *nand, *tmp;
1708 int ret;
1709
1710 list_for_each_entry_safe(nand, tmp, &nc->chips, node) {
Miquel Raynal79282252018-07-25 15:31:40 +02001711 ret = atmel_nand_controller_remove_nand(nand);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001712 if (ret)
1713 return ret;
1714 }
1715
1716 return 0;
1717}
1718
1719static int
1720atmel_nand_controller_legacy_add_nands(struct atmel_nand_controller *nc)
1721{
1722 struct device *dev = nc->dev;
1723 struct platform_device *pdev = to_platform_device(dev);
1724 struct atmel_nand *nand;
1725 struct gpio_desc *gpio;
1726 struct resource *res;
1727
1728 /*
1729 * Legacy bindings only allow connecting a single NAND with a unique CS
1730 * line to the controller.
1731 */
1732 nand = devm_kzalloc(nc->dev, sizeof(*nand) + sizeof(*nand->cs),
1733 GFP_KERNEL);
1734 if (!nand)
1735 return -ENOMEM;
1736
1737 nand->numcs = 1;
1738
1739 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1740 nand->cs[0].io.virt = devm_ioremap_resource(dev, res);
1741 if (IS_ERR(nand->cs[0].io.virt))
1742 return PTR_ERR(nand->cs[0].io.virt);
1743
1744 nand->cs[0].io.dma = res->start;
1745
1746 /*
1747 * The old driver was hardcoding the CS id to 3 for all sama5
1748 * controllers. Since this id is only meaningful for the sama5
1749 * controller we can safely assign this id to 3 no matter the
1750 * controller.
1751 * If one wants to connect a NAND to a different CS line, he will
1752 * have to use the new bindings.
1753 */
1754 nand->cs[0].id = 3;
1755
1756 /* R/B GPIO. */
1757 gpio = devm_gpiod_get_index_optional(dev, NULL, 0, GPIOD_IN);
1758 if (IS_ERR(gpio)) {
1759 dev_err(dev, "Failed to get R/B gpio (err = %ld)\n",
1760 PTR_ERR(gpio));
1761 return PTR_ERR(gpio);
1762 }
1763
1764 if (gpio) {
1765 nand->cs[0].rb.type = ATMEL_NAND_GPIO_RB;
1766 nand->cs[0].rb.gpio = gpio;
1767 }
1768
1769 /* CS GPIO. */
1770 gpio = devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_HIGH);
1771 if (IS_ERR(gpio)) {
1772 dev_err(dev, "Failed to get CS gpio (err = %ld)\n",
1773 PTR_ERR(gpio));
1774 return PTR_ERR(gpio);
1775 }
1776
1777 nand->cs[0].csgpio = gpio;
1778
1779 /* Card detect GPIO. */
1780 gpio = devm_gpiod_get_index_optional(nc->dev, NULL, 2, GPIOD_IN);
1781 if (IS_ERR(gpio)) {
1782 dev_err(dev,
1783 "Failed to get detect gpio (err = %ld)\n",
1784 PTR_ERR(gpio));
1785 return PTR_ERR(gpio);
1786 }
1787
1788 nand->cdgpio = gpio;
1789
1790 nand_set_flash_node(&nand->base, nc->dev->of_node);
1791
1792 return atmel_nand_controller_add_nand(nc, nand);
1793}
1794
1795static int atmel_nand_controller_add_nands(struct atmel_nand_controller *nc)
1796{
1797 struct device_node *np, *nand_np;
1798 struct device *dev = nc->dev;
1799 int ret, reg_cells;
1800 u32 val;
1801
1802 /* We do not retrieve the SMC syscon when parsing old DTs. */
1803 if (nc->caps->legacy_of_bindings)
1804 return atmel_nand_controller_legacy_add_nands(nc);
1805
1806 np = dev->of_node;
1807
1808 ret = of_property_read_u32(np, "#address-cells", &val);
1809 if (ret) {
1810 dev_err(dev, "missing #address-cells property\n");
1811 return ret;
1812 }
1813
1814 reg_cells = val;
1815
1816 ret = of_property_read_u32(np, "#size-cells", &val);
1817 if (ret) {
1818 dev_err(dev, "missing #address-cells property\n");
1819 return ret;
1820 }
1821
1822 reg_cells += val;
1823
1824 for_each_child_of_node(np, nand_np) {
1825 struct atmel_nand *nand;
1826
1827 nand = atmel_nand_create(nc, nand_np, reg_cells);
1828 if (IS_ERR(nand)) {
1829 ret = PTR_ERR(nand);
1830 goto err;
1831 }
1832
1833 ret = atmel_nand_controller_add_nand(nc, nand);
1834 if (ret)
1835 goto err;
1836 }
1837
1838 return 0;
1839
1840err:
1841 atmel_nand_controller_remove_nands(nc);
1842
1843 return ret;
1844}
1845
1846static void atmel_nand_controller_cleanup(struct atmel_nand_controller *nc)
1847{
1848 if (nc->dmac)
1849 dma_release_channel(nc->dmac);
1850
1851 clk_put(nc->mck);
1852}
1853
1854static const struct of_device_id atmel_matrix_of_ids[] = {
1855 {
1856 .compatible = "atmel,at91sam9260-matrix",
1857 .data = (void *)AT91SAM9260_MATRIX_EBICSA,
1858 },
1859 {
1860 .compatible = "atmel,at91sam9261-matrix",
1861 .data = (void *)AT91SAM9261_MATRIX_EBICSA,
1862 },
1863 {
1864 .compatible = "atmel,at91sam9263-matrix",
1865 .data = (void *)AT91SAM9263_MATRIX_EBI0CSA,
1866 },
1867 {
1868 .compatible = "atmel,at91sam9rl-matrix",
1869 .data = (void *)AT91SAM9RL_MATRIX_EBICSA,
1870 },
1871 {
1872 .compatible = "atmel,at91sam9g45-matrix",
1873 .data = (void *)AT91SAM9G45_MATRIX_EBICSA,
1874 },
1875 {
1876 .compatible = "atmel,at91sam9n12-matrix",
1877 .data = (void *)AT91SAM9N12_MATRIX_EBICSA,
1878 },
1879 {
1880 .compatible = "atmel,at91sam9x5-matrix",
1881 .data = (void *)AT91SAM9X5_MATRIX_EBICSA,
1882 },
Christophe Jaillet038e8ad6e2017-04-11 07:22:52 +02001883 { /* sentinel */ },
Boris Brezillonf88fc122017-03-16 09:02:40 +01001884};
1885
Miquel Raynal577e0102018-07-25 15:31:41 +02001886static int atmel_nand_attach_chip(struct nand_chip *chip)
1887{
1888 struct atmel_nand_controller *nc = to_nand_controller(chip->controller);
1889 struct atmel_nand *nand = to_atmel_nand(chip);
1890 struct mtd_info *mtd = nand_to_mtd(chip);
1891 int ret;
1892
1893 ret = nc->caps->ops->ecc_init(chip);
1894 if (ret)
1895 return ret;
1896
1897 if (nc->caps->legacy_of_bindings || !nc->dev->of_node) {
1898 /*
1899 * We keep the MTD name unchanged to avoid breaking platforms
1900 * where the MTD cmdline parser is used and the bootloader
1901 * has not been updated to use the new naming scheme.
1902 */
1903 mtd->name = "atmel_nand";
1904 } else if (!mtd->name) {
1905 /*
1906 * If the new bindings are used and the bootloader has not been
1907 * updated to pass a new mtdparts parameter on the cmdline, you
1908 * should define the following property in your nand node:
1909 *
1910 * label = "atmel_nand";
1911 *
1912 * This way, mtd->name will be set by the core when
1913 * nand_set_flash_node() is called.
1914 */
1915 mtd->name = devm_kasprintf(nc->dev, GFP_KERNEL,
1916 "%s:nand.%d", dev_name(nc->dev),
1917 nand->cs[0].id);
1918 if (!mtd->name) {
1919 dev_err(nc->dev, "Failed to allocate mtd->name\n");
1920 return -ENOMEM;
1921 }
1922 }
1923
1924 return 0;
1925}
1926
1927static const struct nand_controller_ops atmel_nand_controller_ops = {
1928 .attach_chip = atmel_nand_attach_chip,
1929};
1930
Boris Brezillonf88fc122017-03-16 09:02:40 +01001931static int atmel_nand_controller_init(struct atmel_nand_controller *nc,
1932 struct platform_device *pdev,
1933 const struct atmel_nand_controller_caps *caps)
1934{
1935 struct device *dev = &pdev->dev;
1936 struct device_node *np = dev->of_node;
1937 int ret;
1938
Miquel Raynal7da45132018-07-17 09:08:02 +02001939 nand_controller_init(&nc->base);
Miquel Raynal577e0102018-07-25 15:31:41 +02001940 nc->base.ops = &atmel_nand_controller_ops;
Boris Brezillonf88fc122017-03-16 09:02:40 +01001941 INIT_LIST_HEAD(&nc->chips);
1942 nc->dev = dev;
1943 nc->caps = caps;
1944
1945 platform_set_drvdata(pdev, nc);
1946
1947 nc->pmecc = devm_atmel_pmecc_get(dev);
1948 if (IS_ERR(nc->pmecc)) {
1949 ret = PTR_ERR(nc->pmecc);
1950 if (ret != -EPROBE_DEFER)
1951 dev_err(dev, "Could not get PMECC object (err = %d)\n",
1952 ret);
1953 return ret;
1954 }
1955
Peter Rosinefc63622018-03-29 15:10:54 +02001956 if (nc->caps->has_dma && !atmel_nand_avoid_dma) {
Boris Brezillonf88fc122017-03-16 09:02:40 +01001957 dma_cap_mask_t mask;
1958
1959 dma_cap_zero(mask);
1960 dma_cap_set(DMA_MEMCPY, mask);
1961
1962 nc->dmac = dma_request_channel(mask, NULL, NULL);
1963 if (!nc->dmac)
1964 dev_err(nc->dev, "Failed to request DMA channel\n");
1965 }
1966
1967 /* We do not retrieve the SMC syscon when parsing old DTs. */
1968 if (nc->caps->legacy_of_bindings)
1969 return 0;
1970
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01001971 nc->mck = of_clk_get(dev->parent->of_node, 0);
1972 if (IS_ERR(nc->mck)) {
1973 dev_err(dev, "Failed to retrieve MCK clk\n");
1974 return PTR_ERR(nc->mck);
1975 }
1976
Boris Brezillonf88fc122017-03-16 09:02:40 +01001977 np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
1978 if (!np) {
1979 dev_err(dev, "Missing or invalid atmel,smc property\n");
1980 return -EINVAL;
1981 }
1982
1983 nc->smc = syscon_node_to_regmap(np);
1984 of_node_put(np);
1985 if (IS_ERR(nc->smc)) {
Dan Carpenter70106dd2017-04-04 11:15:46 +03001986 ret = PTR_ERR(nc->smc);
Boris Brezillonf88fc122017-03-16 09:02:40 +01001987 dev_err(dev, "Could not get SMC regmap (err = %d)\n", ret);
1988 return ret;
1989 }
1990
1991 return 0;
1992}
1993
1994static int
1995atmel_smc_nand_controller_init(struct atmel_smc_nand_controller *nc)
1996{
1997 struct device *dev = nc->base.dev;
1998 const struct of_device_id *match;
1999 struct device_node *np;
2000 int ret;
2001
2002 /* We do not retrieve the matrix syscon when parsing old DTs. */
2003 if (nc->base.caps->legacy_of_bindings)
2004 return 0;
2005
2006 np = of_parse_phandle(dev->parent->of_node, "atmel,matrix", 0);
2007 if (!np)
2008 return 0;
2009
2010 match = of_match_node(atmel_matrix_of_ids, np);
2011 if (!match) {
2012 of_node_put(np);
2013 return 0;
2014 }
2015
2016 nc->matrix = syscon_node_to_regmap(np);
2017 of_node_put(np);
2018 if (IS_ERR(nc->matrix)) {
Dan Carpenter70106dd2017-04-04 11:15:46 +03002019 ret = PTR_ERR(nc->matrix);
Boris Brezillonf88fc122017-03-16 09:02:40 +01002020 dev_err(dev, "Could not get Matrix regmap (err = %d)\n", ret);
2021 return ret;
2022 }
2023
Boris Brezillone6848512018-07-09 22:09:22 +02002024 nc->ebi_csa_offs = (uintptr_t)match->data;
Boris Brezillonf88fc122017-03-16 09:02:40 +01002025
2026 /*
2027 * The at91sam9263 has 2 EBIs, if the NAND controller is under EBI1
2028 * add 4 to ->ebi_csa_offs.
2029 */
2030 if (of_device_is_compatible(dev->parent->of_node,
2031 "atmel,at91sam9263-ebi1"))
2032 nc->ebi_csa_offs += 4;
2033
2034 return 0;
2035}
2036
2037static int
2038atmel_hsmc_nand_controller_legacy_init(struct atmel_hsmc_nand_controller *nc)
2039{
2040 struct regmap_config regmap_conf = {
2041 .reg_bits = 32,
2042 .val_bits = 32,
2043 .reg_stride = 4,
2044 };
2045
2046 struct device *dev = nc->base.dev;
2047 struct device_node *nand_np, *nfc_np;
2048 void __iomem *iomem;
2049 struct resource res;
2050 int ret;
2051
2052 nand_np = dev->of_node;
2053 nfc_np = of_find_compatible_node(dev->of_node, NULL,
2054 "atmel,sama5d3-nfc");
2055
2056 nc->clk = of_clk_get(nfc_np, 0);
2057 if (IS_ERR(nc->clk)) {
2058 ret = PTR_ERR(nc->clk);
2059 dev_err(dev, "Failed to retrieve HSMC clock (err = %d)\n",
2060 ret);
2061 goto out;
2062 }
2063
2064 ret = clk_prepare_enable(nc->clk);
2065 if (ret) {
2066 dev_err(dev, "Failed to enable the HSMC clock (err = %d)\n",
2067 ret);
2068 goto out;
2069 }
2070
2071 nc->irq = of_irq_get(nand_np, 0);
Sergei Shtylyov892dd182017-08-06 00:14:28 +03002072 if (nc->irq <= 0) {
2073 ret = nc->irq ?: -ENXIO;
Boris Brezillonf88fc122017-03-16 09:02:40 +01002074 if (ret != -EPROBE_DEFER)
2075 dev_err(dev, "Failed to get IRQ number (err = %d)\n",
2076 ret);
2077 goto out;
2078 }
2079
2080 ret = of_address_to_resource(nfc_np, 0, &res);
2081 if (ret) {
2082 dev_err(dev, "Invalid or missing NFC IO resource (err = %d)\n",
2083 ret);
2084 goto out;
2085 }
2086
2087 iomem = devm_ioremap_resource(dev, &res);
2088 if (IS_ERR(iomem)) {
2089 ret = PTR_ERR(iomem);
2090 goto out;
2091 }
2092
2093 regmap_conf.name = "nfc-io";
2094 regmap_conf.max_register = resource_size(&res) - 4;
2095 nc->io = devm_regmap_init_mmio(dev, iomem, &regmap_conf);
2096 if (IS_ERR(nc->io)) {
2097 ret = PTR_ERR(nc->io);
2098 dev_err(dev, "Could not create NFC IO regmap (err = %d)\n",
2099 ret);
2100 goto out;
2101 }
2102
2103 ret = of_address_to_resource(nfc_np, 1, &res);
2104 if (ret) {
2105 dev_err(dev, "Invalid or missing HSMC resource (err = %d)\n",
2106 ret);
2107 goto out;
2108 }
2109
2110 iomem = devm_ioremap_resource(dev, &res);
2111 if (IS_ERR(iomem)) {
2112 ret = PTR_ERR(iomem);
2113 goto out;
2114 }
2115
2116 regmap_conf.name = "smc";
2117 regmap_conf.max_register = resource_size(&res) - 4;
2118 nc->base.smc = devm_regmap_init_mmio(dev, iomem, &regmap_conf);
2119 if (IS_ERR(nc->base.smc)) {
2120 ret = PTR_ERR(nc->base.smc);
2121 dev_err(dev, "Could not create NFC IO regmap (err = %d)\n",
2122 ret);
2123 goto out;
2124 }
2125
2126 ret = of_address_to_resource(nfc_np, 2, &res);
2127 if (ret) {
2128 dev_err(dev, "Invalid or missing SRAM resource (err = %d)\n",
2129 ret);
2130 goto out;
2131 }
2132
2133 nc->sram.virt = devm_ioremap_resource(dev, &res);
2134 if (IS_ERR(nc->sram.virt)) {
2135 ret = PTR_ERR(nc->sram.virt);
2136 goto out;
2137 }
2138
2139 nc->sram.dma = res.start;
2140
2141out:
2142 of_node_put(nfc_np);
2143
2144 return ret;
2145}
2146
2147static int
2148atmel_hsmc_nand_controller_init(struct atmel_hsmc_nand_controller *nc)
2149{
2150 struct device *dev = nc->base.dev;
2151 struct device_node *np;
2152 int ret;
2153
2154 np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
2155 if (!np) {
2156 dev_err(dev, "Missing or invalid atmel,smc property\n");
2157 return -EINVAL;
2158 }
2159
Ludovic Desrochesb0f3ab22017-07-18 15:22:19 +02002160 nc->hsmc_layout = atmel_hsmc_get_reg_layout(np);
2161
Boris Brezillonf88fc122017-03-16 09:02:40 +01002162 nc->irq = of_irq_get(np, 0);
2163 of_node_put(np);
Sergei Shtylyov892dd182017-08-06 00:14:28 +03002164 if (nc->irq <= 0) {
2165 ret = nc->irq ?: -ENXIO;
2166 if (ret != -EPROBE_DEFER)
Boris Brezillonf88fc122017-03-16 09:02:40 +01002167 dev_err(dev, "Failed to get IRQ number (err = %d)\n",
Sergei Shtylyov892dd182017-08-06 00:14:28 +03002168 ret);
2169 return ret;
Boris Brezillonf88fc122017-03-16 09:02:40 +01002170 }
2171
2172 np = of_parse_phandle(dev->of_node, "atmel,nfc-io", 0);
2173 if (!np) {
2174 dev_err(dev, "Missing or invalid atmel,nfc-io property\n");
2175 return -EINVAL;
2176 }
2177
2178 nc->io = syscon_node_to_regmap(np);
2179 of_node_put(np);
2180 if (IS_ERR(nc->io)) {
2181 ret = PTR_ERR(nc->io);
2182 dev_err(dev, "Could not get NFC IO regmap (err = %d)\n", ret);
2183 return ret;
2184 }
2185
2186 nc->sram.pool = of_gen_pool_get(nc->base.dev->of_node,
2187 "atmel,nfc-sram", 0);
2188 if (!nc->sram.pool) {
2189 dev_err(nc->base.dev, "Missing SRAM\n");
2190 return -ENOMEM;
2191 }
2192
Boris Brezillond28395c2018-07-09 22:09:23 +02002193 nc->sram.virt = (void __iomem *)gen_pool_dma_alloc(nc->sram.pool,
2194 ATMEL_NFC_SRAM_SIZE,
2195 &nc->sram.dma);
Boris Brezillonf88fc122017-03-16 09:02:40 +01002196 if (!nc->sram.virt) {
2197 dev_err(nc->base.dev,
2198 "Could not allocate memory from the NFC SRAM pool\n");
2199 return -ENOMEM;
2200 }
2201
2202 return 0;
2203}
2204
2205static int
2206atmel_hsmc_nand_controller_remove(struct atmel_nand_controller *nc)
2207{
2208 struct atmel_hsmc_nand_controller *hsmc_nc;
2209 int ret;
2210
2211 ret = atmel_nand_controller_remove_nands(nc);
2212 if (ret)
2213 return ret;
2214
2215 hsmc_nc = container_of(nc, struct atmel_hsmc_nand_controller, base);
2216 if (hsmc_nc->sram.pool)
2217 gen_pool_free(hsmc_nc->sram.pool,
2218 (unsigned long)hsmc_nc->sram.virt,
2219 ATMEL_NFC_SRAM_SIZE);
2220
2221 if (hsmc_nc->clk) {
2222 clk_disable_unprepare(hsmc_nc->clk);
2223 clk_put(hsmc_nc->clk);
2224 }
2225
2226 atmel_nand_controller_cleanup(nc);
2227
2228 return 0;
2229}
2230
2231static int atmel_hsmc_nand_controller_probe(struct platform_device *pdev,
2232 const struct atmel_nand_controller_caps *caps)
2233{
2234 struct device *dev = &pdev->dev;
2235 struct atmel_hsmc_nand_controller *nc;
2236 int ret;
2237
2238 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2239 if (!nc)
2240 return -ENOMEM;
2241
2242 ret = atmel_nand_controller_init(&nc->base, pdev, caps);
2243 if (ret)
2244 return ret;
2245
2246 if (caps->legacy_of_bindings)
2247 ret = atmel_hsmc_nand_controller_legacy_init(nc);
2248 else
2249 ret = atmel_hsmc_nand_controller_init(nc);
2250
2251 if (ret)
2252 return ret;
2253
2254 /* Make sure all irqs are masked before registering our IRQ handler. */
2255 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, 0xffffffff);
2256 ret = devm_request_irq(dev, nc->irq, atmel_nfc_interrupt,
2257 IRQF_SHARED, "nfc", nc);
2258 if (ret) {
2259 dev_err(dev,
2260 "Could not get register NFC interrupt handler (err = %d)\n",
2261 ret);
2262 goto err;
2263 }
2264
2265 /* Initial NFC configuration. */
2266 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CFG,
2267 ATMEL_HSMC_NFC_CFG_DTO_MAX);
2268
2269 ret = atmel_nand_controller_add_nands(&nc->base);
2270 if (ret)
2271 goto err;
2272
2273 return 0;
2274
2275err:
2276 atmel_hsmc_nand_controller_remove(&nc->base);
2277
2278 return ret;
2279}
2280
2281static const struct atmel_nand_controller_ops atmel_hsmc_nc_ops = {
2282 .probe = atmel_hsmc_nand_controller_probe,
2283 .remove = atmel_hsmc_nand_controller_remove,
2284 .ecc_init = atmel_hsmc_nand_ecc_init,
2285 .nand_init = atmel_hsmc_nand_init,
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002286 .setup_data_interface = atmel_hsmc_nand_setup_data_interface,
Boris Brezillonf88fc122017-03-16 09:02:40 +01002287};
2288
2289static const struct atmel_nand_controller_caps atmel_sama5_nc_caps = {
2290 .has_dma = true,
2291 .ale_offs = BIT(21),
2292 .cle_offs = BIT(22),
2293 .ops = &atmel_hsmc_nc_ops,
2294};
2295
2296/* Only used to parse old bindings. */
2297static const struct atmel_nand_controller_caps atmel_sama5_nand_caps = {
2298 .has_dma = true,
2299 .ale_offs = BIT(21),
2300 .cle_offs = BIT(22),
2301 .ops = &atmel_hsmc_nc_ops,
2302 .legacy_of_bindings = true,
2303};
2304
2305static int atmel_smc_nand_controller_probe(struct platform_device *pdev,
2306 const struct atmel_nand_controller_caps *caps)
2307{
2308 struct device *dev = &pdev->dev;
2309 struct atmel_smc_nand_controller *nc;
2310 int ret;
2311
2312 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2313 if (!nc)
2314 return -ENOMEM;
2315
2316 ret = atmel_nand_controller_init(&nc->base, pdev, caps);
2317 if (ret)
2318 return ret;
2319
2320 ret = atmel_smc_nand_controller_init(nc);
2321 if (ret)
2322 return ret;
2323
2324 return atmel_nand_controller_add_nands(&nc->base);
2325}
2326
2327static int
2328atmel_smc_nand_controller_remove(struct atmel_nand_controller *nc)
2329{
2330 int ret;
2331
2332 ret = atmel_nand_controller_remove_nands(nc);
2333 if (ret)
2334 return ret;
2335
2336 atmel_nand_controller_cleanup(nc);
2337
2338 return 0;
2339}
2340
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002341/*
2342 * The SMC reg layout of at91rm9200 is completely different which prevents us
2343 * from re-using atmel_smc_nand_setup_data_interface() for the
2344 * ->setup_data_interface() hook.
2345 * At this point, there's no support for the at91rm9200 SMC IP, so we leave
2346 * ->setup_data_interface() unassigned.
2347 */
2348static const struct atmel_nand_controller_ops at91rm9200_nc_ops = {
Boris Brezillonf88fc122017-03-16 09:02:40 +01002349 .probe = atmel_smc_nand_controller_probe,
2350 .remove = atmel_smc_nand_controller_remove,
2351 .ecc_init = atmel_nand_ecc_init,
2352 .nand_init = atmel_smc_nand_init,
2353};
2354
2355static const struct atmel_nand_controller_caps atmel_rm9200_nc_caps = {
2356 .ale_offs = BIT(21),
2357 .cle_offs = BIT(22),
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002358 .ops = &at91rm9200_nc_ops,
2359};
2360
2361static const struct atmel_nand_controller_ops atmel_smc_nc_ops = {
2362 .probe = atmel_smc_nand_controller_probe,
2363 .remove = atmel_smc_nand_controller_remove,
2364 .ecc_init = atmel_nand_ecc_init,
2365 .nand_init = atmel_smc_nand_init,
2366 .setup_data_interface = atmel_smc_nand_setup_data_interface,
2367};
2368
2369static const struct atmel_nand_controller_caps atmel_sam9260_nc_caps = {
2370 .ale_offs = BIT(21),
2371 .cle_offs = BIT(22),
Boris Brezillonf88fc122017-03-16 09:02:40 +01002372 .ops = &atmel_smc_nc_ops,
2373};
2374
2375static const struct atmel_nand_controller_caps atmel_sam9261_nc_caps = {
2376 .ale_offs = BIT(22),
2377 .cle_offs = BIT(21),
2378 .ops = &atmel_smc_nc_ops,
2379};
2380
2381static const struct atmel_nand_controller_caps atmel_sam9g45_nc_caps = {
2382 .has_dma = true,
2383 .ale_offs = BIT(21),
2384 .cle_offs = BIT(22),
2385 .ops = &atmel_smc_nc_ops,
2386};
2387
2388/* Only used to parse old bindings. */
2389static const struct atmel_nand_controller_caps atmel_rm9200_nand_caps = {
2390 .ale_offs = BIT(21),
2391 .cle_offs = BIT(22),
2392 .ops = &atmel_smc_nc_ops,
2393 .legacy_of_bindings = true,
2394};
2395
2396static const struct atmel_nand_controller_caps atmel_sam9261_nand_caps = {
2397 .ale_offs = BIT(22),
2398 .cle_offs = BIT(21),
2399 .ops = &atmel_smc_nc_ops,
2400 .legacy_of_bindings = true,
2401};
2402
2403static const struct atmel_nand_controller_caps atmel_sam9g45_nand_caps = {
2404 .has_dma = true,
2405 .ale_offs = BIT(21),
2406 .cle_offs = BIT(22),
2407 .ops = &atmel_smc_nc_ops,
2408 .legacy_of_bindings = true,
2409};
2410
2411static const struct of_device_id atmel_nand_controller_of_ids[] = {
2412 {
2413 .compatible = "atmel,at91rm9200-nand-controller",
2414 .data = &atmel_rm9200_nc_caps,
2415 },
2416 {
2417 .compatible = "atmel,at91sam9260-nand-controller",
Boris Brezillonf9ce2ed2017-03-16 09:35:59 +01002418 .data = &atmel_sam9260_nc_caps,
Boris Brezillonf88fc122017-03-16 09:02:40 +01002419 },
2420 {
2421 .compatible = "atmel,at91sam9261-nand-controller",
2422 .data = &atmel_sam9261_nc_caps,
2423 },
2424 {
2425 .compatible = "atmel,at91sam9g45-nand-controller",
2426 .data = &atmel_sam9g45_nc_caps,
2427 },
2428 {
2429 .compatible = "atmel,sama5d3-nand-controller",
2430 .data = &atmel_sama5_nc_caps,
2431 },
2432 /* Support for old/deprecated bindings: */
2433 {
2434 .compatible = "atmel,at91rm9200-nand",
2435 .data = &atmel_rm9200_nand_caps,
2436 },
2437 {
2438 .compatible = "atmel,sama5d4-nand",
2439 .data = &atmel_rm9200_nand_caps,
2440 },
2441 {
2442 .compatible = "atmel,sama5d2-nand",
2443 .data = &atmel_rm9200_nand_caps,
2444 },
2445 { /* sentinel */ },
2446};
2447MODULE_DEVICE_TABLE(of, atmel_nand_controller_of_ids);
2448
2449static int atmel_nand_controller_probe(struct platform_device *pdev)
2450{
2451 const struct atmel_nand_controller_caps *caps;
2452
2453 if (pdev->id_entry)
2454 caps = (void *)pdev->id_entry->driver_data;
2455 else
2456 caps = of_device_get_match_data(&pdev->dev);
2457
2458 if (!caps) {
2459 dev_err(&pdev->dev, "Could not retrieve NFC caps\n");
2460 return -EINVAL;
2461 }
2462
2463 if (caps->legacy_of_bindings) {
2464 u32 ale_offs = 21;
2465
2466 /*
2467 * If we are parsing legacy DT props and the DT contains a
2468 * valid NFC node, forward the request to the sama5 logic.
2469 */
2470 if (of_find_compatible_node(pdev->dev.of_node, NULL,
2471 "atmel,sama5d3-nfc"))
2472 caps = &atmel_sama5_nand_caps;
2473
2474 /*
2475 * Even if the compatible says we are dealing with an
2476 * at91rm9200 controller, the atmel,nand-has-dma specify that
2477 * this controller supports DMA, which means we are in fact
2478 * dealing with an at91sam9g45+ controller.
2479 */
2480 if (!caps->has_dma &&
2481 of_property_read_bool(pdev->dev.of_node,
2482 "atmel,nand-has-dma"))
2483 caps = &atmel_sam9g45_nand_caps;
2484
2485 /*
2486 * All SoCs except the at91sam9261 are assigning ALE to A21 and
2487 * CLE to A22. If atmel,nand-addr-offset != 21 this means we're
2488 * actually dealing with an at91sam9261 controller.
2489 */
2490 of_property_read_u32(pdev->dev.of_node,
2491 "atmel,nand-addr-offset", &ale_offs);
2492 if (ale_offs != 21)
2493 caps = &atmel_sam9261_nand_caps;
2494 }
2495
2496 return caps->ops->probe(pdev, caps);
2497}
2498
2499static int atmel_nand_controller_remove(struct platform_device *pdev)
2500{
2501 struct atmel_nand_controller *nc = platform_get_drvdata(pdev);
2502
2503 return nc->caps->ops->remove(nc);
2504}
2505
Arnd Bergmann05b6c232017-05-31 10:19:26 +02002506static __maybe_unused int atmel_nand_controller_resume(struct device *dev)
Boris Brezillon6e532af2017-03-16 09:36:00 +01002507{
2508 struct atmel_nand_controller *nc = dev_get_drvdata(dev);
2509 struct atmel_nand *nand;
2510
Romain Izard143b0ab2017-09-28 11:46:23 +02002511 if (nc->pmecc)
2512 atmel_pmecc_reset(nc->pmecc);
2513
Boris Brezillon6e532af2017-03-16 09:36:00 +01002514 list_for_each_entry(nand, &nc->chips, node) {
2515 int i;
2516
2517 for (i = 0; i < nand->numcs; i++)
2518 nand_reset(&nand->base, i);
2519 }
2520
2521 return 0;
2522}
2523
2524static SIMPLE_DEV_PM_OPS(atmel_nand_controller_pm_ops, NULL,
2525 atmel_nand_controller_resume);
2526
Boris Brezillonf88fc122017-03-16 09:02:40 +01002527static struct platform_driver atmel_nand_controller_driver = {
2528 .driver = {
2529 .name = "atmel-nand-controller",
2530 .of_match_table = of_match_ptr(atmel_nand_controller_of_ids),
Boris Brezillon1533bfa2017-10-05 18:57:24 +02002531 .pm = &atmel_nand_controller_pm_ops,
Boris Brezillonf88fc122017-03-16 09:02:40 +01002532 },
2533 .probe = atmel_nand_controller_probe,
2534 .remove = atmel_nand_controller_remove,
2535};
2536module_platform_driver(atmel_nand_controller_driver);
2537
2538MODULE_LICENSE("GPL");
2539MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
2540MODULE_DESCRIPTION("NAND Flash Controller driver for Atmel SoCs");
2541MODULE_ALIAS("platform:atmel-nand-controller");