1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#include <linux/clk.h>
49#include <linux/dma-mapping.h>
50#include <linux/dmaengine.h>
51#include <linux/genalloc.h>
52#include <linux/gpio/consumer.h>
53#include <linux/interrupt.h>
54#include <linux/mfd/syscon.h>
55#include <linux/mfd/syscon/atmel-matrix.h>
56#include <linux/mfd/syscon/atmel-smc.h>
57#include <linux/module.h>
58#include <linux/mtd/rawnand.h>
59#include <linux/of_address.h>
60#include <linux/of_irq.h>
61#include <linux/of_platform.h>
62#include <linux/iopoll.h>
63#include <linux/platform_device.h>
64#include <linux/regmap.h>
65#include <soc/at91/atmel-sfr.h>
66
67#include "pmecc.h"
68
69#define ATMEL_HSMC_NFC_CFG 0x0
70#define ATMEL_HSMC_NFC_CFG_SPARESIZE(x) (((x) / 4) << 24)
71#define ATMEL_HSMC_NFC_CFG_SPARESIZE_MASK GENMASK(30, 24)
72#define ATMEL_HSMC_NFC_CFG_DTO(cyc, mul) (((cyc) << 16) | ((mul) << 20))
73#define ATMEL_HSMC_NFC_CFG_DTO_MAX GENMASK(22, 16)
74#define ATMEL_HSMC_NFC_CFG_RBEDGE BIT(13)
75#define ATMEL_HSMC_NFC_CFG_FALLING_EDGE BIT(12)
76#define ATMEL_HSMC_NFC_CFG_RSPARE BIT(9)
77#define ATMEL_HSMC_NFC_CFG_WSPARE BIT(8)
78#define ATMEL_HSMC_NFC_CFG_PAGESIZE_MASK GENMASK(2, 0)
79#define ATMEL_HSMC_NFC_CFG_PAGESIZE(x) (fls((x) / 512) - 1)
80
81#define ATMEL_HSMC_NFC_CTRL 0x4
82#define ATMEL_HSMC_NFC_CTRL_EN BIT(0)
83#define ATMEL_HSMC_NFC_CTRL_DIS BIT(1)
84
85#define ATMEL_HSMC_NFC_SR 0x8
86#define ATMEL_HSMC_NFC_IER 0xc
87#define ATMEL_HSMC_NFC_IDR 0x10
88#define ATMEL_HSMC_NFC_IMR 0x14
89#define ATMEL_HSMC_NFC_SR_ENABLED BIT(1)
90#define ATMEL_HSMC_NFC_SR_RB_RISE BIT(4)
91#define ATMEL_HSMC_NFC_SR_RB_FALL BIT(5)
92#define ATMEL_HSMC_NFC_SR_BUSY BIT(8)
93#define ATMEL_HSMC_NFC_SR_WR BIT(11)
94#define ATMEL_HSMC_NFC_SR_CSID GENMASK(14, 12)
95#define ATMEL_HSMC_NFC_SR_XFRDONE BIT(16)
96#define ATMEL_HSMC_NFC_SR_CMDDONE BIT(17)
97#define ATMEL_HSMC_NFC_SR_DTOE BIT(20)
98#define ATMEL_HSMC_NFC_SR_UNDEF BIT(21)
99#define ATMEL_HSMC_NFC_SR_AWB BIT(22)
100#define ATMEL_HSMC_NFC_SR_NFCASE BIT(23)
101#define ATMEL_HSMC_NFC_SR_ERRORS (ATMEL_HSMC_NFC_SR_DTOE | \
102 ATMEL_HSMC_NFC_SR_UNDEF | \
103 ATMEL_HSMC_NFC_SR_AWB | \
104 ATMEL_HSMC_NFC_SR_NFCASE)
105#define ATMEL_HSMC_NFC_SR_RBEDGE(x) BIT((x) + 24)
106
107#define ATMEL_HSMC_NFC_ADDR 0x18
108#define ATMEL_HSMC_NFC_BANK 0x1c
109
110#define ATMEL_NFC_MAX_RB_ID 7
111
112#define ATMEL_NFC_SRAM_SIZE 0x2400
113
114#define ATMEL_NFC_CMD(pos, cmd) ((cmd) << (((pos) * 8) + 2))
115#define ATMEL_NFC_VCMD2 BIT(18)
116#define ATMEL_NFC_ACYCLE(naddrs) ((naddrs) << 19)
117#define ATMEL_NFC_CSID(cs) ((cs) << 22)
118#define ATMEL_NFC_DATAEN BIT(25)
119#define ATMEL_NFC_NFCWR BIT(26)
120
121#define ATMEL_NFC_MAX_ADDR_CYCLES 5
122
123#define ATMEL_NAND_ALE_OFFSET BIT(21)
124#define ATMEL_NAND_CLE_OFFSET BIT(22)
125
126#define DEFAULT_TIMEOUT_MS 1000
127#define MIN_DMA_LEN 128
128
129static bool atmel_nand_avoid_dma __read_mostly;
130
131MODULE_PARM_DESC(avoiddma, "Avoid using DMA");
132module_param_named(avoiddma, atmel_nand_avoid_dma, bool, 0400);
133
134enum atmel_nand_rb_type {
135 ATMEL_NAND_NO_RB,
136 ATMEL_NAND_NATIVE_RB,
137 ATMEL_NAND_GPIO_RB,
138};
139
140struct atmel_nand_rb {
141 enum atmel_nand_rb_type type;
142 union {
143 struct gpio_desc *gpio;
144 int id;
145 };
146};
147
148struct atmel_nand_cs {
149 int id;
150 struct atmel_nand_rb rb;
151 struct gpio_desc *csgpio;
152 struct {
153 void __iomem *virt;
154 dma_addr_t dma;
155 } io;
156
157 struct atmel_smc_cs_conf smcconf;
158};
159
160struct atmel_nand {
161 struct list_head node;
162 struct device *dev;
163 struct nand_chip base;
164 struct atmel_nand_cs *activecs;
165 struct atmel_pmecc_user *pmecc;
166 struct gpio_desc *cdgpio;
167 int numcs;
168 struct atmel_nand_cs cs[];
169};
170
171static inline struct atmel_nand *to_atmel_nand(struct nand_chip *chip)
172{
173 return container_of(chip, struct atmel_nand, base);
174}
175
176enum atmel_nfc_data_xfer {
177 ATMEL_NFC_NO_DATA,
178 ATMEL_NFC_READ_DATA,
179 ATMEL_NFC_WRITE_DATA,
180};
181
182struct atmel_nfc_op {
183 u8 cs;
184 u8 ncmds;
185 u8 cmds[2];
186 u8 naddrs;
187 u8 addrs[5];
188 enum atmel_nfc_data_xfer data;
189 u32 wait;
190 u32 errors;
191};
192
193struct atmel_nand_controller;
194struct atmel_nand_controller_caps;
195
196struct atmel_nand_controller_ops {
197 int (*probe)(struct platform_device *pdev,
198 const struct atmel_nand_controller_caps *caps);
199 int (*remove)(struct atmel_nand_controller *nc);
200 void (*nand_init)(struct atmel_nand_controller *nc,
201 struct atmel_nand *nand);
202 int (*ecc_init)(struct nand_chip *chip);
203 int (*setup_interface)(struct atmel_nand *nand, int csline,
204 const struct nand_interface_config *conf);
205 int (*exec_op)(struct atmel_nand *nand,
206 const struct nand_operation *op, bool check_only);
207};
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 char *ebi_csa_regmap_name;
215 const struct atmel_nand_controller_ops *ops;
216};
217
218struct atmel_nand_controller {
219 struct nand_controller 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_controller *ctl)
231{
232 return container_of(ctl, struct atmel_nand_controller, base);
233}
234
235struct atmel_smc_nand_ebi_csa_cfg {
236 u32 offs;
237 u32 nfd0_on_d16;
238};
239
240struct atmel_smc_nand_controller {
241 struct atmel_nand_controller base;
242 struct regmap *ebi_csa_regmap;
243 struct atmel_smc_nand_ebi_csa_cfg *ebi_csa;
244};
245
246static inline struct atmel_smc_nand_controller *
247to_smc_nand_controller(struct nand_controller *ctl)
248{
249 return container_of(to_nand_controller(ctl),
250 struct atmel_smc_nand_controller, base);
251}
252
253struct atmel_hsmc_nand_controller {
254 struct atmel_nand_controller base;
255 struct {
256 struct gen_pool *pool;
257 void __iomem *virt;
258 dma_addr_t dma;
259 } sram;
260 const struct atmel_hsmc_reg_layout *hsmc_layout;
261 struct regmap *io;
262 struct atmel_nfc_op op;
263 struct completion complete;
264 u32 cfg;
265 int irq;
266
267
268 struct clk *clk;
269};
270
271static inline struct atmel_hsmc_nand_controller *
272to_hsmc_nand_controller(struct nand_controller *ctl)
273{
274 return container_of(to_nand_controller(ctl),
275 struct atmel_hsmc_nand_controller, base);
276}
277
278static bool atmel_nfc_op_done(struct atmel_nfc_op *op, u32 status)
279{
280 op->errors |= status & ATMEL_HSMC_NFC_SR_ERRORS;
281 op->wait ^= status & op->wait;
282
283 return !op->wait || op->errors;
284}
285
286static irqreturn_t atmel_nfc_interrupt(int irq, void *data)
287{
288 struct atmel_hsmc_nand_controller *nc = data;
289 u32 sr, rcvd;
290 bool done;
291
292 regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &sr);
293
294 rcvd = sr & (nc->op.wait | ATMEL_HSMC_NFC_SR_ERRORS);
295 done = atmel_nfc_op_done(&nc->op, sr);
296
297 if (rcvd)
298 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, rcvd);
299
300 if (done)
301 complete(&nc->complete);
302
303 return rcvd ? IRQ_HANDLED : IRQ_NONE;
304}
305
306static int atmel_nfc_wait(struct atmel_hsmc_nand_controller *nc, bool poll,
307 unsigned int timeout_ms)
308{
309 int ret;
310
311 if (!timeout_ms)
312 timeout_ms = DEFAULT_TIMEOUT_MS;
313
314 if (poll) {
315 u32 status;
316
317 ret = regmap_read_poll_timeout(nc->base.smc,
318 ATMEL_HSMC_NFC_SR, status,
319 atmel_nfc_op_done(&nc->op,
320 status),
321 0, timeout_ms * 1000);
322 } else {
323 init_completion(&nc->complete);
324 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IER,
325 nc->op.wait | ATMEL_HSMC_NFC_SR_ERRORS);
326 ret = wait_for_completion_timeout(&nc->complete,
327 msecs_to_jiffies(timeout_ms));
328 if (!ret)
329 ret = -ETIMEDOUT;
330 else
331 ret = 0;
332
333 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, 0xffffffff);
334 }
335
336 if (nc->op.errors & ATMEL_HSMC_NFC_SR_DTOE) {
337 dev_err(nc->base.dev, "Waiting NAND R/B Timeout\n");
338 ret = -ETIMEDOUT;
339 }
340
341 if (nc->op.errors & ATMEL_HSMC_NFC_SR_UNDEF) {
342 dev_err(nc->base.dev, "Access to an undefined area\n");
343 ret = -EIO;
344 }
345
346 if (nc->op.errors & ATMEL_HSMC_NFC_SR_AWB) {
347 dev_err(nc->base.dev, "Access while busy\n");
348 ret = -EIO;
349 }
350
351 if (nc->op.errors & ATMEL_HSMC_NFC_SR_NFCASE) {
352 dev_err(nc->base.dev, "Wrong access size\n");
353 ret = -EIO;
354 }
355
356 return ret;
357}
358
359static void atmel_nand_dma_transfer_finished(void *data)
360{
361 struct completion *finished = data;
362
363 complete(finished);
364}
365
366static int atmel_nand_dma_transfer(struct atmel_nand_controller *nc,
367 void *buf, dma_addr_t dev_dma, size_t len,
368 enum dma_data_direction dir)
369{
370 DECLARE_COMPLETION_ONSTACK(finished);
371 dma_addr_t src_dma, dst_dma, buf_dma;
372 struct dma_async_tx_descriptor *tx;
373 dma_cookie_t cookie;
374
375 buf_dma = dma_map_single(nc->dev, buf, len, dir);
376 if (dma_mapping_error(nc->dev, dev_dma)) {
377 dev_err(nc->dev,
378 "Failed to prepare a buffer for DMA access\n");
379 goto err;
380 }
381
382 if (dir == DMA_FROM_DEVICE) {
383 src_dma = dev_dma;
384 dst_dma = buf_dma;
385 } else {
386 src_dma = buf_dma;
387 dst_dma = dev_dma;
388 }
389
390 tx = dmaengine_prep_dma_memcpy(nc->dmac, dst_dma, src_dma, len,
391 DMA_CTRL_ACK | DMA_PREP_INTERRUPT);
392 if (!tx) {
393 dev_err(nc->dev, "Failed to prepare DMA memcpy\n");
394 goto err_unmap;
395 }
396
397 tx->callback = atmel_nand_dma_transfer_finished;
398 tx->callback_param = &finished;
399
400 cookie = dmaengine_submit(tx);
401 if (dma_submit_error(cookie)) {
402 dev_err(nc->dev, "Failed to do DMA tx_submit\n");
403 goto err_unmap;
404 }
405
406 dma_async_issue_pending(nc->dmac);
407 wait_for_completion(&finished);
408
409 return 0;
410
411err_unmap:
412 dma_unmap_single(nc->dev, buf_dma, len, dir);
413
414err:
415 dev_dbg(nc->dev, "Fall back to CPU I/O\n");
416
417 return -EIO;
418}
419
420static int atmel_nfc_exec_op(struct atmel_hsmc_nand_controller *nc, bool poll)
421{
422 u8 *addrs = nc->op.addrs;
423 unsigned int op = 0;
424 u32 addr, val;
425 int i, ret;
426
427 nc->op.wait = ATMEL_HSMC_NFC_SR_CMDDONE;
428
429 for (i = 0; i < nc->op.ncmds; i++)
430 op |= ATMEL_NFC_CMD(i, nc->op.cmds[i]);
431
432 if (nc->op.naddrs == ATMEL_NFC_MAX_ADDR_CYCLES)
433 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_ADDR, *addrs++);
434
435 op |= ATMEL_NFC_CSID(nc->op.cs) |
436 ATMEL_NFC_ACYCLE(nc->op.naddrs);
437
438 if (nc->op.ncmds > 1)
439 op |= ATMEL_NFC_VCMD2;
440
441 addr = addrs[0] | (addrs[1] << 8) | (addrs[2] << 16) |
442 (addrs[3] << 24);
443
444 if (nc->op.data != ATMEL_NFC_NO_DATA) {
445 op |= ATMEL_NFC_DATAEN;
446 nc->op.wait |= ATMEL_HSMC_NFC_SR_XFRDONE;
447
448 if (nc->op.data == ATMEL_NFC_WRITE_DATA)
449 op |= ATMEL_NFC_NFCWR;
450 }
451
452
453 regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &val);
454
455
456 regmap_write(nc->io, op, addr);
457
458 ret = atmel_nfc_wait(nc, poll, 0);
459 if (ret)
460 dev_err(nc->base.dev,
461 "Failed to send NAND command (err = %d)!",
462 ret);
463
464
465 memset(&nc->op, 0, sizeof(nc->op));
466
467 return ret;
468}
469
470static void atmel_nand_data_in(struct atmel_nand *nand, void *buf,
471 unsigned int len, bool force_8bit)
472{
473 struct atmel_nand_controller *nc;
474
475 nc = to_nand_controller(nand->base.controller);
476
477
478
479
480
481
482 if (nc->dmac && virt_addr_valid(buf) &&
483 len >= MIN_DMA_LEN && !force_8bit &&
484 !atmel_nand_dma_transfer(nc, buf, nand->activecs->io.dma, len,
485 DMA_FROM_DEVICE))
486 return;
487
488 if ((nand->base.options & NAND_BUSWIDTH_16) && !force_8bit)
489 ioread16_rep(nand->activecs->io.virt, buf, len / 2);
490 else
491 ioread8_rep(nand->activecs->io.virt, buf, len);
492}
493
494static void atmel_nand_data_out(struct atmel_nand *nand, const void *buf,
495 unsigned int len, bool force_8bit)
496{
497 struct atmel_nand_controller *nc;
498
499 nc = to_nand_controller(nand->base.controller);
500
501
502
503
504
505
506 if (nc->dmac && virt_addr_valid(buf) &&
507 len >= MIN_DMA_LEN && !force_8bit &&
508 !atmel_nand_dma_transfer(nc, (void *)buf, nand->activecs->io.dma,
509 len, DMA_TO_DEVICE))
510 return;
511
512 if ((nand->base.options & NAND_BUSWIDTH_16) && !force_8bit)
513 iowrite16_rep(nand->activecs->io.virt, buf, len / 2);
514 else
515 iowrite8_rep(nand->activecs->io.virt, buf, len);
516}
517
518static int atmel_nand_waitrdy(struct atmel_nand *nand, unsigned int timeout_ms)
519{
520 if (nand->activecs->rb.type == ATMEL_NAND_NO_RB)
521 return nand_soft_waitrdy(&nand->base, timeout_ms);
522
523 return nand_gpio_waitrdy(&nand->base, nand->activecs->rb.gpio,
524 timeout_ms);
525}
526
527static int atmel_hsmc_nand_waitrdy(struct atmel_nand *nand,
528 unsigned int timeout_ms)
529{
530 struct atmel_hsmc_nand_controller *nc;
531 u32 status, mask;
532
533 if (nand->activecs->rb.type != ATMEL_NAND_NATIVE_RB)
534 return atmel_nand_waitrdy(nand, timeout_ms);
535
536 nc = to_hsmc_nand_controller(nand->base.controller);
537 mask = ATMEL_HSMC_NFC_SR_RBEDGE(nand->activecs->rb.id);
538 return regmap_read_poll_timeout_atomic(nc->base.smc, ATMEL_HSMC_NFC_SR,
539 status, status & mask,
540 10, timeout_ms * 1000);
541}
542
543static void atmel_nand_select_target(struct atmel_nand *nand,
544 unsigned int cs)
545{
546 nand->activecs = &nand->cs[cs];
547}
548
549static void atmel_hsmc_nand_select_target(struct atmel_nand *nand,
550 unsigned int cs)
551{
552 struct mtd_info *mtd = nand_to_mtd(&nand->base);
553 struct atmel_hsmc_nand_controller *nc;
554 u32 cfg = ATMEL_HSMC_NFC_CFG_PAGESIZE(mtd->writesize) |
555 ATMEL_HSMC_NFC_CFG_SPARESIZE(mtd->oobsize) |
556 ATMEL_HSMC_NFC_CFG_RSPARE;
557
558 nand->activecs = &nand->cs[cs];
559 nc = to_hsmc_nand_controller(nand->base.controller);
560 if (nc->cfg == cfg)
561 return;
562
563 regmap_update_bits(nc->base.smc, ATMEL_HSMC_NFC_CFG,
564 ATMEL_HSMC_NFC_CFG_PAGESIZE_MASK |
565 ATMEL_HSMC_NFC_CFG_SPARESIZE_MASK |
566 ATMEL_HSMC_NFC_CFG_RSPARE |
567 ATMEL_HSMC_NFC_CFG_WSPARE,
568 cfg);
569 nc->cfg = cfg;
570}
571
572static int atmel_smc_nand_exec_instr(struct atmel_nand *nand,
573 const struct nand_op_instr *instr)
574{
575 struct atmel_nand_controller *nc;
576 unsigned int i;
577
578 nc = to_nand_controller(nand->base.controller);
579 switch (instr->type) {
580 case NAND_OP_CMD_INSTR:
581 writeb(instr->ctx.cmd.opcode,
582 nand->activecs->io.virt + nc->caps->cle_offs);
583 return 0;
584 case NAND_OP_ADDR_INSTR:
585 for (i = 0; i < instr->ctx.addr.naddrs; i++)
586 writeb(instr->ctx.addr.addrs[i],
587 nand->activecs->io.virt + nc->caps->ale_offs);
588 return 0;
589 case NAND_OP_DATA_IN_INSTR:
590 atmel_nand_data_in(nand, instr->ctx.data.buf.in,
591 instr->ctx.data.len,
592 instr->ctx.data.force_8bit);
593 return 0;
594 case NAND_OP_DATA_OUT_INSTR:
595 atmel_nand_data_out(nand, instr->ctx.data.buf.out,
596 instr->ctx.data.len,
597 instr->ctx.data.force_8bit);
598 return 0;
599 case NAND_OP_WAITRDY_INSTR:
600 return atmel_nand_waitrdy(nand,
601 instr->ctx.waitrdy.timeout_ms);
602 default:
603 break;
604 }
605
606 return -EINVAL;
607}
608
609static int atmel_smc_nand_exec_op(struct atmel_nand *nand,
610 const struct nand_operation *op,
611 bool check_only)
612{
613 unsigned int i;
614 int ret = 0;
615
616 if (check_only)
617 return 0;
618
619 atmel_nand_select_target(nand, op->cs);
620 gpiod_set_value(nand->activecs->csgpio, 0);
621 for (i = 0; i < op->ninstrs; i++) {
622 ret = atmel_smc_nand_exec_instr(nand, &op->instrs[i]);
623 if (ret)
624 break;
625 }
626 gpiod_set_value(nand->activecs->csgpio, 1);
627
628 return ret;
629}
630
631static int atmel_hsmc_exec_cmd_addr(struct nand_chip *chip,
632 const struct nand_subop *subop)
633{
634 struct atmel_nand *nand = to_atmel_nand(chip);
635 struct atmel_hsmc_nand_controller *nc;
636 unsigned int i, j;
637
638 nc = to_hsmc_nand_controller(chip->controller);
639
640 nc->op.cs = nand->activecs->id;
641 for (i = 0; i < subop->ninstrs; i++) {
642 const struct nand_op_instr *instr = &subop->instrs[i];
643
644 if (instr->type == NAND_OP_CMD_INSTR) {
645 nc->op.cmds[nc->op.ncmds++] = instr->ctx.cmd.opcode;
646 continue;
647 }
648
649 for (j = nand_subop_get_addr_start_off(subop, i);
650 j < nand_subop_get_num_addr_cyc(subop, i); j++) {
651 nc->op.addrs[nc->op.naddrs] = instr->ctx.addr.addrs[j];
652 nc->op.naddrs++;
653 }
654 }
655
656 return atmel_nfc_exec_op(nc, true);
657}
658
659static int atmel_hsmc_exec_rw(struct nand_chip *chip,
660 const struct nand_subop *subop)
661{
662 const struct nand_op_instr *instr = subop->instrs;
663 struct atmel_nand *nand = to_atmel_nand(chip);
664
665 if (instr->type == NAND_OP_DATA_IN_INSTR)
666 atmel_nand_data_in(nand, instr->ctx.data.buf.in,
667 instr->ctx.data.len,
668 instr->ctx.data.force_8bit);
669 else
670 atmel_nand_data_out(nand, instr->ctx.data.buf.out,
671 instr->ctx.data.len,
672 instr->ctx.data.force_8bit);
673
674 return 0;
675}
676
677static int atmel_hsmc_exec_waitrdy(struct nand_chip *chip,
678 const struct nand_subop *subop)
679{
680 const struct nand_op_instr *instr = subop->instrs;
681 struct atmel_nand *nand = to_atmel_nand(chip);
682
683 return atmel_hsmc_nand_waitrdy(nand, instr->ctx.waitrdy.timeout_ms);
684}
685
686static const struct nand_op_parser atmel_hsmc_op_parser = NAND_OP_PARSER(
687 NAND_OP_PARSER_PATTERN(atmel_hsmc_exec_cmd_addr,
688 NAND_OP_PARSER_PAT_CMD_ELEM(true),
689 NAND_OP_PARSER_PAT_ADDR_ELEM(true, 5),
690 NAND_OP_PARSER_PAT_CMD_ELEM(true)),
691 NAND_OP_PARSER_PATTERN(atmel_hsmc_exec_rw,
692 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 0)),
693 NAND_OP_PARSER_PATTERN(atmel_hsmc_exec_rw,
694 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, 0)),
695 NAND_OP_PARSER_PATTERN(atmel_hsmc_exec_waitrdy,
696 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
697);
698
699static int atmel_hsmc_nand_exec_op(struct atmel_nand *nand,
700 const struct nand_operation *op,
701 bool check_only)
702{
703 int ret;
704
705 if (check_only)
706 return nand_op_parser_exec_op(&nand->base,
707 &atmel_hsmc_op_parser, op, true);
708
709 atmel_hsmc_nand_select_target(nand, op->cs);
710 ret = nand_op_parser_exec_op(&nand->base, &atmel_hsmc_op_parser, op,
711 false);
712
713 return ret;
714}
715
716static void atmel_nfc_copy_to_sram(struct nand_chip *chip, const u8 *buf,
717 bool oob_required)
718{
719 struct mtd_info *mtd = nand_to_mtd(chip);
720 struct atmel_hsmc_nand_controller *nc;
721 int ret = -EIO;
722
723 nc = to_hsmc_nand_controller(chip->controller);
724
725 if (nc->base.dmac)
726 ret = atmel_nand_dma_transfer(&nc->base, (void *)buf,
727 nc->sram.dma, mtd->writesize,
728 DMA_TO_DEVICE);
729
730
731 if (ret)
732 memcpy_toio(nc->sram.virt, buf, mtd->writesize);
733
734 if (oob_required)
735 memcpy_toio(nc->sram.virt + mtd->writesize, chip->oob_poi,
736 mtd->oobsize);
737}
738
739static void atmel_nfc_copy_from_sram(struct nand_chip *chip, u8 *buf,
740 bool oob_required)
741{
742 struct mtd_info *mtd = nand_to_mtd(chip);
743 struct atmel_hsmc_nand_controller *nc;
744 int ret = -EIO;
745
746 nc = to_hsmc_nand_controller(chip->controller);
747
748 if (nc->base.dmac)
749 ret = atmel_nand_dma_transfer(&nc->base, buf, nc->sram.dma,
750 mtd->writesize, DMA_FROM_DEVICE);
751
752
753 if (ret)
754 memcpy_fromio(buf, nc->sram.virt, mtd->writesize);
755
756 if (oob_required)
757 memcpy_fromio(chip->oob_poi, nc->sram.virt + mtd->writesize,
758 mtd->oobsize);
759}
760
761static void atmel_nfc_set_op_addr(struct nand_chip *chip, int page, int column)
762{
763 struct mtd_info *mtd = nand_to_mtd(chip);
764 struct atmel_hsmc_nand_controller *nc;
765
766 nc = to_hsmc_nand_controller(chip->controller);
767
768 if (column >= 0) {
769 nc->op.addrs[nc->op.naddrs++] = column;
770
771
772
773
774 if (mtd->writesize > 512)
775 nc->op.addrs[nc->op.naddrs++] = column >> 8;
776 }
777
778 if (page >= 0) {
779 nc->op.addrs[nc->op.naddrs++] = page;
780 nc->op.addrs[nc->op.naddrs++] = page >> 8;
781
782 if (chip->options & NAND_ROW_ADDR_3)
783 nc->op.addrs[nc->op.naddrs++] = page >> 16;
784 }
785}
786
787static int atmel_nand_pmecc_enable(struct nand_chip *chip, int op, bool raw)
788{
789 struct atmel_nand *nand = to_atmel_nand(chip);
790 struct atmel_nand_controller *nc;
791 int ret;
792
793 nc = to_nand_controller(chip->controller);
794
795 if (raw)
796 return 0;
797
798 ret = atmel_pmecc_enable(nand->pmecc, op);
799 if (ret)
800 dev_err(nc->dev,
801 "Failed to enable ECC engine (err = %d)\n", ret);
802
803 return ret;
804}
805
806static void atmel_nand_pmecc_disable(struct nand_chip *chip, bool raw)
807{
808 struct atmel_nand *nand = to_atmel_nand(chip);
809
810 if (!raw)
811 atmel_pmecc_disable(nand->pmecc);
812}
813
814static int atmel_nand_pmecc_generate_eccbytes(struct nand_chip *chip, bool raw)
815{
816 struct atmel_nand *nand = to_atmel_nand(chip);
817 struct mtd_info *mtd = nand_to_mtd(chip);
818 struct atmel_nand_controller *nc;
819 struct mtd_oob_region oobregion;
820 void *eccbuf;
821 int ret, i;
822
823 nc = to_nand_controller(chip->controller);
824
825 if (raw)
826 return 0;
827
828 ret = atmel_pmecc_wait_rdy(nand->pmecc);
829 if (ret) {
830 dev_err(nc->dev,
831 "Failed to transfer NAND page data (err = %d)\n",
832 ret);
833 return ret;
834 }
835
836 mtd_ooblayout_ecc(mtd, 0, &oobregion);
837 eccbuf = chip->oob_poi + oobregion.offset;
838
839 for (i = 0; i < chip->ecc.steps; i++) {
840 atmel_pmecc_get_generated_eccbytes(nand->pmecc, i,
841 eccbuf);
842 eccbuf += chip->ecc.bytes;
843 }
844
845 return 0;
846}
847
848static int atmel_nand_pmecc_correct_data(struct nand_chip *chip, void *buf,
849 bool raw)
850{
851 struct atmel_nand *nand = to_atmel_nand(chip);
852 struct mtd_info *mtd = nand_to_mtd(chip);
853 struct atmel_nand_controller *nc;
854 struct mtd_oob_region oobregion;
855 int ret, i, max_bitflips = 0;
856 void *databuf, *eccbuf;
857
858 nc = to_nand_controller(chip->controller);
859
860 if (raw)
861 return 0;
862
863 ret = atmel_pmecc_wait_rdy(nand->pmecc);
864 if (ret) {
865 dev_err(nc->dev,
866 "Failed to read NAND page data (err = %d)\n",
867 ret);
868 return ret;
869 }
870
871 mtd_ooblayout_ecc(mtd, 0, &oobregion);
872 eccbuf = chip->oob_poi + oobregion.offset;
873 databuf = buf;
874
875 for (i = 0; i < chip->ecc.steps; i++) {
876 ret = atmel_pmecc_correct_sector(nand->pmecc, i, databuf,
877 eccbuf);
878 if (ret < 0 && !atmel_pmecc_correct_erased_chunks(nand->pmecc))
879 ret = nand_check_erased_ecc_chunk(databuf,
880 chip->ecc.size,
881 eccbuf,
882 chip->ecc.bytes,
883 NULL, 0,
884 chip->ecc.strength);
885
886 if (ret >= 0)
887 max_bitflips = max(ret, max_bitflips);
888 else
889 mtd->ecc_stats.failed++;
890
891 databuf += chip->ecc.size;
892 eccbuf += chip->ecc.bytes;
893 }
894
895 return max_bitflips;
896}
897
898static int atmel_nand_pmecc_write_pg(struct nand_chip *chip, const u8 *buf,
899 bool oob_required, int page, bool raw)
900{
901 struct mtd_info *mtd = nand_to_mtd(chip);
902 struct atmel_nand *nand = to_atmel_nand(chip);
903 int ret;
904
905 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
906
907 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw);
908 if (ret)
909 return ret;
910
911 nand_write_data_op(chip, buf, mtd->writesize, false);
912
913 ret = atmel_nand_pmecc_generate_eccbytes(chip, raw);
914 if (ret) {
915 atmel_pmecc_disable(nand->pmecc);
916 return ret;
917 }
918
919 atmel_nand_pmecc_disable(chip, raw);
920
921 nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
922
923 return nand_prog_page_end_op(chip);
924}
925
926static int atmel_nand_pmecc_write_page(struct nand_chip *chip, const u8 *buf,
927 int oob_required, int page)
928{
929 return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, false);
930}
931
932static int atmel_nand_pmecc_write_page_raw(struct nand_chip *chip,
933 const u8 *buf, int oob_required,
934 int page)
935{
936 return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, true);
937}
938
939static int atmel_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf,
940 bool oob_required, int page, bool raw)
941{
942 struct mtd_info *mtd = nand_to_mtd(chip);
943 int ret;
944
945 nand_read_page_op(chip, page, 0, NULL, 0);
946
947 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw);
948 if (ret)
949 return ret;
950
951 ret = nand_read_data_op(chip, buf, mtd->writesize, false, false);
952 if (ret)
953 goto out_disable;
954
955 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false, false);
956 if (ret)
957 goto out_disable;
958
959 ret = atmel_nand_pmecc_correct_data(chip, buf, raw);
960
961out_disable:
962 atmel_nand_pmecc_disable(chip, raw);
963
964 return ret;
965}
966
967static int atmel_nand_pmecc_read_page(struct nand_chip *chip, u8 *buf,
968 int oob_required, int page)
969{
970 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, false);
971}
972
973static int atmel_nand_pmecc_read_page_raw(struct nand_chip *chip, u8 *buf,
974 int oob_required, int page)
975{
976 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, true);
977}
978
979static int atmel_hsmc_nand_pmecc_write_pg(struct nand_chip *chip,
980 const u8 *buf, bool oob_required,
981 int page, bool raw)
982{
983 struct mtd_info *mtd = nand_to_mtd(chip);
984 struct atmel_nand *nand = to_atmel_nand(chip);
985 struct atmel_hsmc_nand_controller *nc;
986 int ret;
987
988 atmel_hsmc_nand_select_target(nand, chip->cur_cs);
989 nc = to_hsmc_nand_controller(chip->controller);
990
991 atmel_nfc_copy_to_sram(chip, buf, false);
992
993 nc->op.cmds[0] = NAND_CMD_SEQIN;
994 nc->op.ncmds = 1;
995 atmel_nfc_set_op_addr(chip, page, 0x0);
996 nc->op.cs = nand->activecs->id;
997 nc->op.data = ATMEL_NFC_WRITE_DATA;
998
999 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw);
1000 if (ret)
1001 return ret;
1002
1003 ret = atmel_nfc_exec_op(nc, false);
1004 if (ret) {
1005 atmel_nand_pmecc_disable(chip, raw);
1006 dev_err(nc->base.dev,
1007 "Failed to transfer NAND page data (err = %d)\n",
1008 ret);
1009 return ret;
1010 }
1011
1012 ret = atmel_nand_pmecc_generate_eccbytes(chip, raw);
1013
1014 atmel_nand_pmecc_disable(chip, raw);
1015
1016 if (ret)
1017 return ret;
1018
1019 nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
1020
1021 return nand_prog_page_end_op(chip);
1022}
1023
1024static int atmel_hsmc_nand_pmecc_write_page(struct nand_chip *chip,
1025 const u8 *buf, int oob_required,
1026 int page)
1027{
1028 return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page,
1029 false);
1030}
1031
1032static int atmel_hsmc_nand_pmecc_write_page_raw(struct nand_chip *chip,
1033 const u8 *buf,
1034 int oob_required, int page)
1035{
1036 return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page,
1037 true);
1038}
1039
1040static int atmel_hsmc_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf,
1041 bool oob_required, int page,
1042 bool raw)
1043{
1044 struct mtd_info *mtd = nand_to_mtd(chip);
1045 struct atmel_nand *nand = to_atmel_nand(chip);
1046 struct atmel_hsmc_nand_controller *nc;
1047 int ret;
1048
1049 atmel_hsmc_nand_select_target(nand, chip->cur_cs);
1050 nc = to_hsmc_nand_controller(chip->controller);
1051
1052
1053
1054
1055
1056
1057 if (nand->activecs->rb.type != ATMEL_NAND_NATIVE_RB)
1058 return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page,
1059 raw);
1060
1061 nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READ0;
1062
1063 if (mtd->writesize > 512)
1064 nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READSTART;
1065
1066 atmel_nfc_set_op_addr(chip, page, 0x0);
1067 nc->op.cs = nand->activecs->id;
1068 nc->op.data = ATMEL_NFC_READ_DATA;
1069
1070 ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw);
1071 if (ret)
1072 return ret;
1073
1074 ret = atmel_nfc_exec_op(nc, false);
1075 if (ret) {
1076 atmel_nand_pmecc_disable(chip, raw);
1077 dev_err(nc->base.dev,
1078 "Failed to load NAND page data (err = %d)\n",
1079 ret);
1080 return ret;
1081 }
1082
1083 atmel_nfc_copy_from_sram(chip, buf, true);
1084
1085 ret = atmel_nand_pmecc_correct_data(chip, buf, raw);
1086
1087 atmel_nand_pmecc_disable(chip, raw);
1088
1089 return ret;
1090}
1091
1092static int atmel_hsmc_nand_pmecc_read_page(struct nand_chip *chip, u8 *buf,
1093 int oob_required, int page)
1094{
1095 return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page,
1096 false);
1097}
1098
1099static int atmel_hsmc_nand_pmecc_read_page_raw(struct nand_chip *chip,
1100 u8 *buf, int oob_required,
1101 int page)
1102{
1103 return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page,
1104 true);
1105}
1106
1107static int atmel_nand_pmecc_init(struct nand_chip *chip)
1108{
1109 const struct nand_ecc_props *requirements =
1110 nanddev_get_ecc_requirements(&chip->base);
1111 struct mtd_info *mtd = nand_to_mtd(chip);
1112 struct nand_device *nanddev = mtd_to_nanddev(mtd);
1113 struct atmel_nand *nand = to_atmel_nand(chip);
1114 struct atmel_nand_controller *nc;
1115 struct atmel_pmecc_user_req req;
1116
1117 nc = to_nand_controller(chip->controller);
1118
1119 if (!nc->pmecc) {
1120 dev_err(nc->dev, "HW ECC not supported\n");
1121 return -ENOTSUPP;
1122 }
1123
1124 if (nc->caps->legacy_of_bindings) {
1125 u32 val;
1126
1127 if (!of_property_read_u32(nc->dev->of_node, "atmel,pmecc-cap",
1128 &val))
1129 chip->ecc.strength = val;
1130
1131 if (!of_property_read_u32(nc->dev->of_node,
1132 "atmel,pmecc-sector-size",
1133 &val))
1134 chip->ecc.size = val;
1135 }
1136
1137 if (nanddev->ecc.user_conf.flags & NAND_ECC_MAXIMIZE_STRENGTH)
1138 req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH;
1139 else if (chip->ecc.strength)
1140 req.ecc.strength = chip->ecc.strength;
1141 else if (requirements->strength)
1142 req.ecc.strength = requirements->strength;
1143 else
1144 req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH;
1145
1146 if (chip->ecc.size)
1147 req.ecc.sectorsize = chip->ecc.size;
1148 else if (requirements->step_size)
1149 req.ecc.sectorsize = requirements->step_size;
1150 else
1151 req.ecc.sectorsize = ATMEL_PMECC_SECTOR_SIZE_AUTO;
1152
1153 req.pagesize = mtd->writesize;
1154 req.oobsize = mtd->oobsize;
1155
1156 if (mtd->writesize <= 512) {
1157 req.ecc.bytes = 4;
1158 req.ecc.ooboffset = 0;
1159 } else {
1160 req.ecc.bytes = mtd->oobsize - 2;
1161 req.ecc.ooboffset = ATMEL_PMECC_OOBOFFSET_AUTO;
1162 }
1163
1164 nand->pmecc = atmel_pmecc_create_user(nc->pmecc, &req);
1165 if (IS_ERR(nand->pmecc))
1166 return PTR_ERR(nand->pmecc);
1167
1168 chip->ecc.algo = NAND_ECC_ALGO_BCH;
1169 chip->ecc.size = req.ecc.sectorsize;
1170 chip->ecc.bytes = req.ecc.bytes / req.ecc.nsectors;
1171 chip->ecc.strength = req.ecc.strength;
1172
1173 chip->options |= NAND_NO_SUBPAGE_WRITE;
1174
1175 mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
1176
1177 return 0;
1178}
1179
1180static int atmel_nand_ecc_init(struct nand_chip *chip)
1181{
1182 struct atmel_nand_controller *nc;
1183 int ret;
1184
1185 nc = to_nand_controller(chip->controller);
1186
1187 switch (chip->ecc.engine_type) {
1188 case NAND_ECC_ENGINE_TYPE_NONE:
1189 case NAND_ECC_ENGINE_TYPE_SOFT:
1190
1191
1192
1193 break;
1194
1195 case NAND_ECC_ENGINE_TYPE_ON_HOST:
1196 ret = atmel_nand_pmecc_init(chip);
1197 if (ret)
1198 return ret;
1199
1200 chip->ecc.read_page = atmel_nand_pmecc_read_page;
1201 chip->ecc.write_page = atmel_nand_pmecc_write_page;
1202 chip->ecc.read_page_raw = atmel_nand_pmecc_read_page_raw;
1203 chip->ecc.write_page_raw = atmel_nand_pmecc_write_page_raw;
1204 break;
1205
1206 default:
1207
1208 dev_err(nc->dev, "Unsupported ECC mode: %d\n",
1209 chip->ecc.engine_type);
1210 return -ENOTSUPP;
1211 }
1212
1213 return 0;
1214}
1215
1216static int atmel_hsmc_nand_ecc_init(struct nand_chip *chip)
1217{
1218 int ret;
1219
1220 ret = atmel_nand_ecc_init(chip);
1221 if (ret)
1222 return ret;
1223
1224 if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
1225 return 0;
1226
1227
1228 chip->ecc.read_page = atmel_hsmc_nand_pmecc_read_page;
1229 chip->ecc.write_page = atmel_hsmc_nand_pmecc_write_page;
1230 chip->ecc.read_page_raw = atmel_hsmc_nand_pmecc_read_page_raw;
1231 chip->ecc.write_page_raw = atmel_hsmc_nand_pmecc_write_page_raw;
1232
1233 return 0;
1234}
1235
1236static int atmel_smc_nand_prepare_smcconf(struct atmel_nand *nand,
1237 const struct nand_interface_config *conf,
1238 struct atmel_smc_cs_conf *smcconf)
1239{
1240 u32 ncycles, totalcycles, timeps, mckperiodps;
1241 struct atmel_nand_controller *nc;
1242 int ret;
1243
1244 nc = to_nand_controller(nand->base.controller);
1245
1246
1247 if (!nand_interface_is_sdr(conf))
1248 return -ENOTSUPP;
1249
1250
1251
1252
1253
1254 if (conf->timings.sdr.tRC_min < 30000)
1255 return -ENOTSUPP;
1256
1257 atmel_smc_cs_conf_init(smcconf);
1258
1259 mckperiodps = NSEC_PER_SEC / clk_get_rate(nc->mck);
1260 mckperiodps *= 1000;
1261
1262
1263
1264
1265
1266
1267 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWP_min, mckperiodps);
1268 totalcycles = ncycles;
1269 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NWE_SHIFT,
1270 ncycles);
1271 if (ret)
1272 return ret;
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285 timeps = max3(conf->timings.sdr.tCLS_min, conf->timings.sdr.tCS_min,
1286 conf->timings.sdr.tALS_min);
1287 timeps = max(timeps, conf->timings.sdr.tDS_min);
1288 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1289 ncycles = ncycles > totalcycles ? ncycles - totalcycles : 0;
1290 totalcycles += ncycles;
1291 ret = atmel_smc_cs_conf_set_setup(smcconf, ATMEL_SMC_NWE_SHIFT,
1292 ncycles);
1293 if (ret)
1294 return ret;
1295
1296
1297
1298
1299
1300
1301
1302 timeps = max3(conf->timings.sdr.tCLH_min, conf->timings.sdr.tCH_min,
1303 conf->timings.sdr.tALH_min);
1304 timeps = max3(timeps, conf->timings.sdr.tDH_min,
1305 conf->timings.sdr.tWH_min);
1306 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1307 totalcycles += ncycles;
1308
1309
1310
1311
1312
1313
1314
1315
1316 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWC_min, mckperiodps);
1317 ncycles = max(totalcycles, ncycles);
1318 ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NWE_SHIFT,
1319 ncycles);
1320 if (ret)
1321 return ret;
1322
1323
1324
1325
1326
1327
1328
1329
1330 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_WR_SHIFT,
1331 ncycles);
1332 if (ret)
1333 return ret;
1334
1335
1336
1337
1338
1339
1340
1341 timeps = max(conf->timings.sdr.tREH_min, conf->timings.sdr.tRHOH_min);
1342 ncycles = DIV_ROUND_UP(timeps, mckperiodps);
1343 totalcycles = ncycles;
1344
1345
1346
1347
1348 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRHZ_max, mckperiodps);
1349 ncycles -= totalcycles;
1350
1351
1352
1353
1354
1355
1356
1357 if (ncycles > ATMEL_SMC_MODE_TDF_MAX)
1358 ncycles = ATMEL_SMC_MODE_TDF_MAX;
1359 else if (ncycles < ATMEL_SMC_MODE_TDF_MIN)
1360 ncycles = ATMEL_SMC_MODE_TDF_MIN;
1361
1362 smcconf->mode |= ATMEL_SMC_MODE_TDF(ncycles) |
1363 ATMEL_SMC_MODE_TDFMODE_OPTIMIZED;
1364
1365
1366
1367
1368
1369
1370 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRP_min, mckperiodps);
1371 totalcycles += ncycles;
1372 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NRD_SHIFT,
1373 ncycles);
1374 if (ret)
1375 return ret;
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRC_min, mckperiodps);
1387 ncycles = max(totalcycles, ncycles);
1388 ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NRD_SHIFT,
1389 ncycles);
1390 if (ret)
1391 return ret;
1392
1393
1394
1395
1396
1397
1398
1399
1400 ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_RD_SHIFT,
1401 ncycles);
1402 if (ret)
1403 return ret;
1404
1405
1406 ncycles = DIV_ROUND_UP(conf->timings.sdr.tCLR_min, mckperiodps);
1407 ret = atmel_smc_cs_conf_set_timing(smcconf,
1408 ATMEL_HSMC_TIMINGS_TCLR_SHIFT,
1409 ncycles);
1410 if (ret)
1411 return ret;
1412
1413 ncycles = DIV_ROUND_UP(conf->timings.sdr.tADL_min, mckperiodps);
1414 ret = atmel_smc_cs_conf_set_timing(smcconf,
1415 ATMEL_HSMC_TIMINGS_TADL_SHIFT,
1416 ncycles);
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428 if (ret && ret != -ERANGE)
1429 return ret;
1430
1431 ncycles = DIV_ROUND_UP(conf->timings.sdr.tAR_min, mckperiodps);
1432 ret = atmel_smc_cs_conf_set_timing(smcconf,
1433 ATMEL_HSMC_TIMINGS_TAR_SHIFT,
1434 ncycles);
1435 if (ret)
1436 return ret;
1437
1438 ncycles = DIV_ROUND_UP(conf->timings.sdr.tRR_min, mckperiodps);
1439 ret = atmel_smc_cs_conf_set_timing(smcconf,
1440 ATMEL_HSMC_TIMINGS_TRR_SHIFT,
1441 ncycles);
1442 if (ret)
1443 return ret;
1444
1445 ncycles = DIV_ROUND_UP(conf->timings.sdr.tWB_max, mckperiodps);
1446 ret = atmel_smc_cs_conf_set_timing(smcconf,
1447 ATMEL_HSMC_TIMINGS_TWB_SHIFT,
1448 ncycles);
1449 if (ret)
1450 return ret;
1451
1452
1453 smcconf->timings |= ATMEL_HSMC_TIMINGS_NFSEL;
1454
1455
1456 if (nand->base.options & NAND_BUSWIDTH_16)
1457 smcconf->mode |= ATMEL_SMC_MODE_DBW_16;
1458
1459
1460 smcconf->mode |= ATMEL_SMC_MODE_READMODE_NRD |
1461 ATMEL_SMC_MODE_WRITEMODE_NWE;
1462
1463 return 0;
1464}
1465
1466static int atmel_smc_nand_setup_interface(struct atmel_nand *nand,
1467 int csline,
1468 const struct nand_interface_config *conf)
1469{
1470 struct atmel_nand_controller *nc;
1471 struct atmel_smc_cs_conf smcconf;
1472 struct atmel_nand_cs *cs;
1473 int ret;
1474
1475 nc = to_nand_controller(nand->base.controller);
1476
1477 ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf);
1478 if (ret)
1479 return ret;
1480
1481 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1482 return 0;
1483
1484 cs = &nand->cs[csline];
1485 cs->smcconf = smcconf;
1486 atmel_smc_cs_conf_apply(nc->smc, cs->id, &cs->smcconf);
1487
1488 return 0;
1489}
1490
1491static int atmel_hsmc_nand_setup_interface(struct atmel_nand *nand,
1492 int csline,
1493 const struct nand_interface_config *conf)
1494{
1495 struct atmel_hsmc_nand_controller *nc;
1496 struct atmel_smc_cs_conf smcconf;
1497 struct atmel_nand_cs *cs;
1498 int ret;
1499
1500 nc = to_hsmc_nand_controller(nand->base.controller);
1501
1502 ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf);
1503 if (ret)
1504 return ret;
1505
1506 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1507 return 0;
1508
1509 cs = &nand->cs[csline];
1510 cs->smcconf = smcconf;
1511
1512 if (cs->rb.type == ATMEL_NAND_NATIVE_RB)
1513 cs->smcconf.timings |= ATMEL_HSMC_TIMINGS_RBNSEL(cs->rb.id);
1514
1515 atmel_hsmc_cs_conf_apply(nc->base.smc, nc->hsmc_layout, cs->id,
1516 &cs->smcconf);
1517
1518 return 0;
1519}
1520
1521static int atmel_nand_setup_interface(struct nand_chip *chip, int csline,
1522 const struct nand_interface_config *conf)
1523{
1524 struct atmel_nand *nand = to_atmel_nand(chip);
1525 const struct nand_sdr_timings *sdr;
1526 struct atmel_nand_controller *nc;
1527
1528 sdr = nand_get_sdr_timings(conf);
1529 if (IS_ERR(sdr))
1530 return PTR_ERR(sdr);
1531
1532 nc = to_nand_controller(nand->base.controller);
1533
1534 if (csline >= nand->numcs ||
1535 (csline < 0 && csline != NAND_DATA_IFACE_CHECK_ONLY))
1536 return -EINVAL;
1537
1538 return nc->caps->ops->setup_interface(nand, csline, conf);
1539}
1540
1541static int atmel_nand_exec_op(struct nand_chip *chip,
1542 const struct nand_operation *op,
1543 bool check_only)
1544{
1545 struct atmel_nand *nand = to_atmel_nand(chip);
1546 struct atmel_nand_controller *nc;
1547
1548 nc = to_nand_controller(nand->base.controller);
1549
1550 return nc->caps->ops->exec_op(nand, op, check_only);
1551}
1552
1553static void atmel_nand_init(struct atmel_nand_controller *nc,
1554 struct atmel_nand *nand)
1555{
1556 struct nand_chip *chip = &nand->base;
1557 struct mtd_info *mtd = nand_to_mtd(chip);
1558
1559 mtd->dev.parent = nc->dev;
1560 nand->base.controller = &nc->base;
1561
1562 if (!nc->mck || !nc->caps->ops->setup_interface)
1563 chip->options |= NAND_KEEP_TIMINGS;
1564
1565
1566
1567
1568
1569 if (nc->dmac)
1570 chip->options |= NAND_USES_DMA;
1571
1572
1573 if (nc->pmecc)
1574 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
1575}
1576
1577static void atmel_smc_nand_init(struct atmel_nand_controller *nc,
1578 struct atmel_nand *nand)
1579{
1580 struct nand_chip *chip = &nand->base;
1581 struct atmel_smc_nand_controller *smc_nc;
1582 int i;
1583
1584 atmel_nand_init(nc, nand);
1585
1586 smc_nc = to_smc_nand_controller(chip->controller);
1587 if (!smc_nc->ebi_csa_regmap)
1588 return;
1589
1590
1591 for (i = 0; i < nand->numcs; i++)
1592 regmap_update_bits(smc_nc->ebi_csa_regmap,
1593 smc_nc->ebi_csa->offs,
1594 BIT(nand->cs[i].id), BIT(nand->cs[i].id));
1595
1596 if (smc_nc->ebi_csa->nfd0_on_d16)
1597 regmap_update_bits(smc_nc->ebi_csa_regmap,
1598 smc_nc->ebi_csa->offs,
1599 smc_nc->ebi_csa->nfd0_on_d16,
1600 smc_nc->ebi_csa->nfd0_on_d16);
1601}
1602
1603static int atmel_nand_controller_remove_nand(struct atmel_nand *nand)
1604{
1605 struct nand_chip *chip = &nand->base;
1606 struct mtd_info *mtd = nand_to_mtd(chip);
1607 int ret;
1608
1609 ret = mtd_device_unregister(mtd);
1610 if (ret)
1611 return ret;
1612
1613 nand_cleanup(chip);
1614 list_del(&nand->node);
1615
1616 return 0;
1617}
1618
1619static struct atmel_nand *atmel_nand_create(struct atmel_nand_controller *nc,
1620 struct device_node *np,
1621 int reg_cells)
1622{
1623 struct atmel_nand *nand;
1624 struct gpio_desc *gpio;
1625 int numcs, ret, i;
1626
1627 numcs = of_property_count_elems_of_size(np, "reg",
1628 reg_cells * sizeof(u32));
1629 if (numcs < 1) {
1630 dev_err(nc->dev, "Missing or invalid reg property\n");
1631 return ERR_PTR(-EINVAL);
1632 }
1633
1634 nand = devm_kzalloc(nc->dev, struct_size(nand, cs, numcs), GFP_KERNEL);
1635 if (!nand) {
1636 dev_err(nc->dev, "Failed to allocate NAND object\n");
1637 return ERR_PTR(-ENOMEM);
1638 }
1639
1640 nand->numcs = numcs;
1641
1642 gpio = devm_fwnode_gpiod_get(nc->dev, of_fwnode_handle(np),
1643 "det", GPIOD_IN, "nand-det");
1644 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1645 dev_err(nc->dev,
1646 "Failed to get detect gpio (err = %ld)\n",
1647 PTR_ERR(gpio));
1648 return ERR_CAST(gpio);
1649 }
1650
1651 if (!IS_ERR(gpio))
1652 nand->cdgpio = gpio;
1653
1654 for (i = 0; i < numcs; i++) {
1655 struct resource res;
1656 u32 val;
1657
1658 ret = of_address_to_resource(np, 0, &res);
1659 if (ret) {
1660 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1661 ret);
1662 return ERR_PTR(ret);
1663 }
1664
1665 ret = of_property_read_u32_index(np, "reg", i * reg_cells,
1666 &val);
1667 if (ret) {
1668 dev_err(nc->dev, "Invalid reg property (err = %d)\n",
1669 ret);
1670 return ERR_PTR(ret);
1671 }
1672
1673 nand->cs[i].id = val;
1674
1675 nand->cs[i].io.dma = res.start;
1676 nand->cs[i].io.virt = devm_ioremap_resource(nc->dev, &res);
1677 if (IS_ERR(nand->cs[i].io.virt))
1678 return ERR_CAST(nand->cs[i].io.virt);
1679
1680 if (!of_property_read_u32(np, "atmel,rb", &val)) {
1681 if (val > ATMEL_NFC_MAX_RB_ID)
1682 return ERR_PTR(-EINVAL);
1683
1684 nand->cs[i].rb.type = ATMEL_NAND_NATIVE_RB;
1685 nand->cs[i].rb.id = val;
1686 } else {
1687 gpio = devm_fwnode_gpiod_get_index(nc->dev,
1688 of_fwnode_handle(np),
1689 "rb", i, GPIOD_IN,
1690 "nand-rb");
1691 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1692 dev_err(nc->dev,
1693 "Failed to get R/B gpio (err = %ld)\n",
1694 PTR_ERR(gpio));
1695 return ERR_CAST(gpio);
1696 }
1697
1698 if (!IS_ERR(gpio)) {
1699 nand->cs[i].rb.type = ATMEL_NAND_GPIO_RB;
1700 nand->cs[i].rb.gpio = gpio;
1701 }
1702 }
1703
1704 gpio = devm_fwnode_gpiod_get_index(nc->dev,
1705 of_fwnode_handle(np),
1706 "cs", i, GPIOD_OUT_HIGH,
1707 "nand-cs");
1708 if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) {
1709 dev_err(nc->dev,
1710 "Failed to get CS gpio (err = %ld)\n",
1711 PTR_ERR(gpio));
1712 return ERR_CAST(gpio);
1713 }
1714
1715 if (!IS_ERR(gpio))
1716 nand->cs[i].csgpio = gpio;
1717 }
1718
1719 nand_set_flash_node(&nand->base, np);
1720
1721 return nand;
1722}
1723
1724static int
1725atmel_nand_controller_add_nand(struct atmel_nand_controller *nc,
1726 struct atmel_nand *nand)
1727{
1728 struct nand_chip *chip = &nand->base;
1729 struct mtd_info *mtd = nand_to_mtd(chip);
1730 int ret;
1731
1732
1733 if (nand->cdgpio && gpiod_get_value(nand->cdgpio)) {
1734 dev_info(nc->dev, "No SmartMedia card inserted.\n");
1735 return 0;
1736 }
1737
1738 nc->caps->ops->nand_init(nc, nand);
1739
1740 ret = nand_scan(chip, nand->numcs);
1741 if (ret) {
1742 dev_err(nc->dev, "NAND scan failed: %d\n", ret);
1743 return ret;
1744 }
1745
1746 ret = mtd_device_register(mtd, NULL, 0);
1747 if (ret) {
1748 dev_err(nc->dev, "Failed to register mtd device: %d\n", ret);
1749 nand_cleanup(chip);
1750 return ret;
1751 }
1752
1753 list_add_tail(&nand->node, &nc->chips);
1754
1755 return 0;
1756}
1757
1758static int
1759atmel_nand_controller_remove_nands(struct atmel_nand_controller *nc)
1760{
1761 struct atmel_nand *nand, *tmp;
1762 int ret;
1763
1764 list_for_each_entry_safe(nand, tmp, &nc->chips, node) {
1765 ret = atmel_nand_controller_remove_nand(nand);
1766 if (ret)
1767 return ret;
1768 }
1769
1770 return 0;
1771}
1772
1773static int
1774atmel_nand_controller_legacy_add_nands(struct atmel_nand_controller *nc)
1775{
1776 struct device *dev = nc->dev;
1777 struct platform_device *pdev = to_platform_device(dev);
1778 struct atmel_nand *nand;
1779 struct gpio_desc *gpio;
1780 struct resource *res;
1781
1782
1783
1784
1785
1786 nand = devm_kzalloc(nc->dev, sizeof(*nand) + sizeof(*nand->cs),
1787 GFP_KERNEL);
1788 if (!nand)
1789 return -ENOMEM;
1790
1791 nand->numcs = 1;
1792
1793 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1794 nand->cs[0].io.virt = devm_ioremap_resource(dev, res);
1795 if (IS_ERR(nand->cs[0].io.virt))
1796 return PTR_ERR(nand->cs[0].io.virt);
1797
1798 nand->cs[0].io.dma = res->start;
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808 nand->cs[0].id = 3;
1809
1810
1811 gpio = devm_gpiod_get_index_optional(dev, NULL, 0, GPIOD_IN);
1812 if (IS_ERR(gpio)) {
1813 dev_err(dev, "Failed to get R/B gpio (err = %ld)\n",
1814 PTR_ERR(gpio));
1815 return PTR_ERR(gpio);
1816 }
1817
1818 if (gpio) {
1819 nand->cs[0].rb.type = ATMEL_NAND_GPIO_RB;
1820 nand->cs[0].rb.gpio = gpio;
1821 }
1822
1823
1824 gpio = devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_HIGH);
1825 if (IS_ERR(gpio)) {
1826 dev_err(dev, "Failed to get CS gpio (err = %ld)\n",
1827 PTR_ERR(gpio));
1828 return PTR_ERR(gpio);
1829 }
1830
1831 nand->cs[0].csgpio = gpio;
1832
1833
1834 gpio = devm_gpiod_get_index_optional(nc->dev, NULL, 2, GPIOD_IN);
1835 if (IS_ERR(gpio)) {
1836 dev_err(dev,
1837 "Failed to get detect gpio (err = %ld)\n",
1838 PTR_ERR(gpio));
1839 return PTR_ERR(gpio);
1840 }
1841
1842 nand->cdgpio = gpio;
1843
1844 nand_set_flash_node(&nand->base, nc->dev->of_node);
1845
1846 return atmel_nand_controller_add_nand(nc, nand);
1847}
1848
1849static int atmel_nand_controller_add_nands(struct atmel_nand_controller *nc)
1850{
1851 struct device_node *np, *nand_np;
1852 struct device *dev = nc->dev;
1853 int ret, reg_cells;
1854 u32 val;
1855
1856
1857 if (nc->caps->legacy_of_bindings)
1858 return atmel_nand_controller_legacy_add_nands(nc);
1859
1860 np = dev->of_node;
1861
1862 ret = of_property_read_u32(np, "#address-cells", &val);
1863 if (ret) {
1864 dev_err(dev, "missing #address-cells property\n");
1865 return ret;
1866 }
1867
1868 reg_cells = val;
1869
1870 ret = of_property_read_u32(np, "#size-cells", &val);
1871 if (ret) {
1872 dev_err(dev, "missing #size-cells property\n");
1873 return ret;
1874 }
1875
1876 reg_cells += val;
1877
1878 for_each_child_of_node(np, nand_np) {
1879 struct atmel_nand *nand;
1880
1881 nand = atmel_nand_create(nc, nand_np, reg_cells);
1882 if (IS_ERR(nand)) {
1883 ret = PTR_ERR(nand);
1884 goto err;
1885 }
1886
1887 ret = atmel_nand_controller_add_nand(nc, nand);
1888 if (ret)
1889 goto err;
1890 }
1891
1892 return 0;
1893
1894err:
1895 atmel_nand_controller_remove_nands(nc);
1896
1897 return ret;
1898}
1899
1900static void atmel_nand_controller_cleanup(struct atmel_nand_controller *nc)
1901{
1902 if (nc->dmac)
1903 dma_release_channel(nc->dmac);
1904
1905 clk_put(nc->mck);
1906}
1907
1908static const struct atmel_smc_nand_ebi_csa_cfg at91sam9260_ebi_csa = {
1909 .offs = AT91SAM9260_MATRIX_EBICSA,
1910};
1911
1912static const struct atmel_smc_nand_ebi_csa_cfg at91sam9261_ebi_csa = {
1913 .offs = AT91SAM9261_MATRIX_EBICSA,
1914};
1915
1916static const struct atmel_smc_nand_ebi_csa_cfg at91sam9263_ebi_csa = {
1917 .offs = AT91SAM9263_MATRIX_EBI0CSA,
1918};
1919
1920static const struct atmel_smc_nand_ebi_csa_cfg at91sam9rl_ebi_csa = {
1921 .offs = AT91SAM9RL_MATRIX_EBICSA,
1922};
1923
1924static const struct atmel_smc_nand_ebi_csa_cfg at91sam9g45_ebi_csa = {
1925 .offs = AT91SAM9G45_MATRIX_EBICSA,
1926};
1927
1928static const struct atmel_smc_nand_ebi_csa_cfg at91sam9n12_ebi_csa = {
1929 .offs = AT91SAM9N12_MATRIX_EBICSA,
1930};
1931
1932static const struct atmel_smc_nand_ebi_csa_cfg at91sam9x5_ebi_csa = {
1933 .offs = AT91SAM9X5_MATRIX_EBICSA,
1934};
1935
1936static const struct atmel_smc_nand_ebi_csa_cfg sam9x60_ebi_csa = {
1937 .offs = AT91_SFR_CCFG_EBICSA,
1938 .nfd0_on_d16 = AT91_SFR_CCFG_NFD0_ON_D16,
1939};
1940
1941static const struct of_device_id atmel_ebi_csa_regmap_of_ids[] = {
1942 {
1943 .compatible = "atmel,at91sam9260-matrix",
1944 .data = &at91sam9260_ebi_csa,
1945 },
1946 {
1947 .compatible = "atmel,at91sam9261-matrix",
1948 .data = &at91sam9261_ebi_csa,
1949 },
1950 {
1951 .compatible = "atmel,at91sam9263-matrix",
1952 .data = &at91sam9263_ebi_csa,
1953 },
1954 {
1955 .compatible = "atmel,at91sam9rl-matrix",
1956 .data = &at91sam9rl_ebi_csa,
1957 },
1958 {
1959 .compatible = "atmel,at91sam9g45-matrix",
1960 .data = &at91sam9g45_ebi_csa,
1961 },
1962 {
1963 .compatible = "atmel,at91sam9n12-matrix",
1964 .data = &at91sam9n12_ebi_csa,
1965 },
1966 {
1967 .compatible = "atmel,at91sam9x5-matrix",
1968 .data = &at91sam9x5_ebi_csa,
1969 },
1970 {
1971 .compatible = "microchip,sam9x60-sfr",
1972 .data = &sam9x60_ebi_csa,
1973 },
1974 { },
1975};
1976
1977static int atmel_nand_attach_chip(struct nand_chip *chip)
1978{
1979 struct atmel_nand_controller *nc = to_nand_controller(chip->controller);
1980 struct atmel_nand *nand = to_atmel_nand(chip);
1981 struct mtd_info *mtd = nand_to_mtd(chip);
1982 int ret;
1983
1984 ret = nc->caps->ops->ecc_init(chip);
1985 if (ret)
1986 return ret;
1987
1988 if (nc->caps->legacy_of_bindings || !nc->dev->of_node) {
1989
1990
1991
1992
1993
1994 mtd->name = "atmel_nand";
1995 } else if (!mtd->name) {
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006 mtd->name = devm_kasprintf(nc->dev, GFP_KERNEL,
2007 "%s:nand.%d", dev_name(nc->dev),
2008 nand->cs[0].id);
2009 if (!mtd->name) {
2010 dev_err(nc->dev, "Failed to allocate mtd->name\n");
2011 return -ENOMEM;
2012 }
2013 }
2014
2015 return 0;
2016}
2017
2018static const struct nand_controller_ops atmel_nand_controller_ops = {
2019 .attach_chip = atmel_nand_attach_chip,
2020 .setup_interface = atmel_nand_setup_interface,
2021 .exec_op = atmel_nand_exec_op,
2022};
2023
2024static int atmel_nand_controller_init(struct atmel_nand_controller *nc,
2025 struct platform_device *pdev,
2026 const struct atmel_nand_controller_caps *caps)
2027{
2028 struct device *dev = &pdev->dev;
2029 struct device_node *np = dev->of_node;
2030 int ret;
2031
2032 nand_controller_init(&nc->base);
2033 nc->base.ops = &atmel_nand_controller_ops;
2034 INIT_LIST_HEAD(&nc->chips);
2035 nc->dev = dev;
2036 nc->caps = caps;
2037
2038 platform_set_drvdata(pdev, nc);
2039
2040 nc->pmecc = devm_atmel_pmecc_get(dev);
2041 if (IS_ERR(nc->pmecc))
2042 return dev_err_probe(dev, PTR_ERR(nc->pmecc),
2043 "Could not get PMECC object\n");
2044
2045 if (nc->caps->has_dma && !atmel_nand_avoid_dma) {
2046 dma_cap_mask_t mask;
2047
2048 dma_cap_zero(mask);
2049 dma_cap_set(DMA_MEMCPY, mask);
2050
2051 nc->dmac = dma_request_channel(mask, NULL, NULL);
2052 if (!nc->dmac)
2053 dev_err(nc->dev, "Failed to request DMA channel\n");
2054 }
2055
2056
2057 if (nc->caps->legacy_of_bindings)
2058 return 0;
2059
2060 nc->mck = of_clk_get(dev->parent->of_node, 0);
2061 if (IS_ERR(nc->mck)) {
2062 dev_err(dev, "Failed to retrieve MCK clk\n");
2063 return PTR_ERR(nc->mck);
2064 }
2065
2066 np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
2067 if (!np) {
2068 dev_err(dev, "Missing or invalid atmel,smc property\n");
2069 return -EINVAL;
2070 }
2071
2072 nc->smc = syscon_node_to_regmap(np);
2073 of_node_put(np);
2074 if (IS_ERR(nc->smc)) {
2075 ret = PTR_ERR(nc->smc);
2076 dev_err(dev, "Could not get SMC regmap (err = %d)\n", ret);
2077 return ret;
2078 }
2079
2080 return 0;
2081}
2082
2083static int
2084atmel_smc_nand_controller_init(struct atmel_smc_nand_controller *nc)
2085{
2086 struct device *dev = nc->base.dev;
2087 const struct of_device_id *match;
2088 struct device_node *np;
2089 int ret;
2090
2091
2092 if (nc->base.caps->legacy_of_bindings)
2093 return 0;
2094
2095 np = of_parse_phandle(dev->parent->of_node,
2096 nc->base.caps->ebi_csa_regmap_name, 0);
2097 if (!np)
2098 return 0;
2099
2100 match = of_match_node(atmel_ebi_csa_regmap_of_ids, np);
2101 if (!match) {
2102 of_node_put(np);
2103 return 0;
2104 }
2105
2106 nc->ebi_csa_regmap = syscon_node_to_regmap(np);
2107 of_node_put(np);
2108 if (IS_ERR(nc->ebi_csa_regmap)) {
2109 ret = PTR_ERR(nc->ebi_csa_regmap);
2110 dev_err(dev, "Could not get EBICSA regmap (err = %d)\n", ret);
2111 return ret;
2112 }
2113
2114 nc->ebi_csa = (struct atmel_smc_nand_ebi_csa_cfg *)match->data;
2115
2116
2117
2118
2119
2120 if (of_device_is_compatible(dev->parent->of_node,
2121 "atmel,at91sam9263-ebi1"))
2122 nc->ebi_csa->offs += 4;
2123
2124 return 0;
2125}
2126
2127static int
2128atmel_hsmc_nand_controller_legacy_init(struct atmel_hsmc_nand_controller *nc)
2129{
2130 struct regmap_config regmap_conf = {
2131 .reg_bits = 32,
2132 .val_bits = 32,
2133 .reg_stride = 4,
2134 };
2135
2136 struct device *dev = nc->base.dev;
2137 struct device_node *nand_np, *nfc_np;
2138 void __iomem *iomem;
2139 struct resource res;
2140 int ret;
2141
2142 nand_np = dev->of_node;
2143 nfc_np = of_get_compatible_child(dev->of_node, "atmel,sama5d3-nfc");
2144 if (!nfc_np) {
2145 dev_err(dev, "Could not find device node for sama5d3-nfc\n");
2146 return -ENODEV;
2147 }
2148
2149 nc->clk = of_clk_get(nfc_np, 0);
2150 if (IS_ERR(nc->clk)) {
2151 ret = PTR_ERR(nc->clk);
2152 dev_err(dev, "Failed to retrieve HSMC clock (err = %d)\n",
2153 ret);
2154 goto out;
2155 }
2156
2157 ret = clk_prepare_enable(nc->clk);
2158 if (ret) {
2159 dev_err(dev, "Failed to enable the HSMC clock (err = %d)\n",
2160 ret);
2161 goto out;
2162 }
2163
2164 nc->irq = of_irq_get(nand_np, 0);
2165 if (nc->irq <= 0) {
2166 ret = nc->irq ?: -ENXIO;
2167 if (ret != -EPROBE_DEFER)
2168 dev_err(dev, "Failed to get IRQ number (err = %d)\n",
2169 ret);
2170 goto out;
2171 }
2172
2173 ret = of_address_to_resource(nfc_np, 0, &res);
2174 if (ret) {
2175 dev_err(dev, "Invalid or missing NFC IO resource (err = %d)\n",
2176 ret);
2177 goto out;
2178 }
2179
2180 iomem = devm_ioremap_resource(dev, &res);
2181 if (IS_ERR(iomem)) {
2182 ret = PTR_ERR(iomem);
2183 goto out;
2184 }
2185
2186 regmap_conf.name = "nfc-io";
2187 regmap_conf.max_register = resource_size(&res) - 4;
2188 nc->io = devm_regmap_init_mmio(dev, iomem, ®map_conf);
2189 if (IS_ERR(nc->io)) {
2190 ret = PTR_ERR(nc->io);
2191 dev_err(dev, "Could not create NFC IO regmap (err = %d)\n",
2192 ret);
2193 goto out;
2194 }
2195
2196 ret = of_address_to_resource(nfc_np, 1, &res);
2197 if (ret) {
2198 dev_err(dev, "Invalid or missing HSMC resource (err = %d)\n",
2199 ret);
2200 goto out;
2201 }
2202
2203 iomem = devm_ioremap_resource(dev, &res);
2204 if (IS_ERR(iomem)) {
2205 ret = PTR_ERR(iomem);
2206 goto out;
2207 }
2208
2209 regmap_conf.name = "smc";
2210 regmap_conf.max_register = resource_size(&res) - 4;
2211 nc->base.smc = devm_regmap_init_mmio(dev, iomem, ®map_conf);
2212 if (IS_ERR(nc->base.smc)) {
2213 ret = PTR_ERR(nc->base.smc);
2214 dev_err(dev, "Could not create NFC IO regmap (err = %d)\n",
2215 ret);
2216 goto out;
2217 }
2218
2219 ret = of_address_to_resource(nfc_np, 2, &res);
2220 if (ret) {
2221 dev_err(dev, "Invalid or missing SRAM resource (err = %d)\n",
2222 ret);
2223 goto out;
2224 }
2225
2226 nc->sram.virt = devm_ioremap_resource(dev, &res);
2227 if (IS_ERR(nc->sram.virt)) {
2228 ret = PTR_ERR(nc->sram.virt);
2229 goto out;
2230 }
2231
2232 nc->sram.dma = res.start;
2233
2234out:
2235 of_node_put(nfc_np);
2236
2237 return ret;
2238}
2239
2240static int
2241atmel_hsmc_nand_controller_init(struct atmel_hsmc_nand_controller *nc)
2242{
2243 struct device *dev = nc->base.dev;
2244 struct device_node *np;
2245 int ret;
2246
2247 np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
2248 if (!np) {
2249 dev_err(dev, "Missing or invalid atmel,smc property\n");
2250 return -EINVAL;
2251 }
2252
2253 nc->hsmc_layout = atmel_hsmc_get_reg_layout(np);
2254
2255 nc->irq = of_irq_get(np, 0);
2256 of_node_put(np);
2257 if (nc->irq <= 0) {
2258 ret = nc->irq ?: -ENXIO;
2259 if (ret != -EPROBE_DEFER)
2260 dev_err(dev, "Failed to get IRQ number (err = %d)\n",
2261 ret);
2262 return ret;
2263 }
2264
2265 np = of_parse_phandle(dev->of_node, "atmel,nfc-io", 0);
2266 if (!np) {
2267 dev_err(dev, "Missing or invalid atmel,nfc-io property\n");
2268 return -EINVAL;
2269 }
2270
2271 nc->io = syscon_node_to_regmap(np);
2272 of_node_put(np);
2273 if (IS_ERR(nc->io)) {
2274 ret = PTR_ERR(nc->io);
2275 dev_err(dev, "Could not get NFC IO regmap (err = %d)\n", ret);
2276 return ret;
2277 }
2278
2279 nc->sram.pool = of_gen_pool_get(nc->base.dev->of_node,
2280 "atmel,nfc-sram", 0);
2281 if (!nc->sram.pool) {
2282 dev_err(nc->base.dev, "Missing SRAM\n");
2283 return -ENOMEM;
2284 }
2285
2286 nc->sram.virt = (void __iomem *)gen_pool_dma_alloc(nc->sram.pool,
2287 ATMEL_NFC_SRAM_SIZE,
2288 &nc->sram.dma);
2289 if (!nc->sram.virt) {
2290 dev_err(nc->base.dev,
2291 "Could not allocate memory from the NFC SRAM pool\n");
2292 return -ENOMEM;
2293 }
2294
2295 return 0;
2296}
2297
2298static int
2299atmel_hsmc_nand_controller_remove(struct atmel_nand_controller *nc)
2300{
2301 struct atmel_hsmc_nand_controller *hsmc_nc;
2302 int ret;
2303
2304 ret = atmel_nand_controller_remove_nands(nc);
2305 if (ret)
2306 return ret;
2307
2308 hsmc_nc = container_of(nc, struct atmel_hsmc_nand_controller, base);
2309 regmap_write(hsmc_nc->base.smc, ATMEL_HSMC_NFC_CTRL,
2310 ATMEL_HSMC_NFC_CTRL_DIS);
2311
2312 if (hsmc_nc->sram.pool)
2313 gen_pool_free(hsmc_nc->sram.pool,
2314 (unsigned long)hsmc_nc->sram.virt,
2315 ATMEL_NFC_SRAM_SIZE);
2316
2317 if (hsmc_nc->clk) {
2318 clk_disable_unprepare(hsmc_nc->clk);
2319 clk_put(hsmc_nc->clk);
2320 }
2321
2322 atmel_nand_controller_cleanup(nc);
2323
2324 return 0;
2325}
2326
2327static int atmel_hsmc_nand_controller_probe(struct platform_device *pdev,
2328 const struct atmel_nand_controller_caps *caps)
2329{
2330 struct device *dev = &pdev->dev;
2331 struct atmel_hsmc_nand_controller *nc;
2332 int ret;
2333
2334 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2335 if (!nc)
2336 return -ENOMEM;
2337
2338 ret = atmel_nand_controller_init(&nc->base, pdev, caps);
2339 if (ret)
2340 return ret;
2341
2342 if (caps->legacy_of_bindings)
2343 ret = atmel_hsmc_nand_controller_legacy_init(nc);
2344 else
2345 ret = atmel_hsmc_nand_controller_init(nc);
2346
2347 if (ret)
2348 return ret;
2349
2350
2351 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, 0xffffffff);
2352 ret = devm_request_irq(dev, nc->irq, atmel_nfc_interrupt,
2353 IRQF_SHARED, "nfc", nc);
2354 if (ret) {
2355 dev_err(dev,
2356 "Could not get register NFC interrupt handler (err = %d)\n",
2357 ret);
2358 goto err;
2359 }
2360
2361
2362 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CFG,
2363 ATMEL_HSMC_NFC_CFG_DTO_MAX);
2364 regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CTRL,
2365 ATMEL_HSMC_NFC_CTRL_EN);
2366
2367 ret = atmel_nand_controller_add_nands(&nc->base);
2368 if (ret)
2369 goto err;
2370
2371 return 0;
2372
2373err:
2374 atmel_hsmc_nand_controller_remove(&nc->base);
2375
2376 return ret;
2377}
2378
2379static const struct atmel_nand_controller_ops atmel_hsmc_nc_ops = {
2380 .probe = atmel_hsmc_nand_controller_probe,
2381 .remove = atmel_hsmc_nand_controller_remove,
2382 .ecc_init = atmel_hsmc_nand_ecc_init,
2383 .nand_init = atmel_nand_init,
2384 .setup_interface = atmel_hsmc_nand_setup_interface,
2385 .exec_op = atmel_hsmc_nand_exec_op,
2386};
2387
2388static const struct atmel_nand_controller_caps atmel_sama5_nc_caps = {
2389 .has_dma = true,
2390 .ale_offs = BIT(21),
2391 .cle_offs = BIT(22),
2392 .ops = &atmel_hsmc_nc_ops,
2393};
2394
2395
2396static const struct atmel_nand_controller_caps atmel_sama5_nand_caps = {
2397 .has_dma = true,
2398 .ale_offs = BIT(21),
2399 .cle_offs = BIT(22),
2400 .ops = &atmel_hsmc_nc_ops,
2401 .legacy_of_bindings = true,
2402};
2403
2404static int atmel_smc_nand_controller_probe(struct platform_device *pdev,
2405 const struct atmel_nand_controller_caps *caps)
2406{
2407 struct device *dev = &pdev->dev;
2408 struct atmel_smc_nand_controller *nc;
2409 int ret;
2410
2411 nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL);
2412 if (!nc)
2413 return -ENOMEM;
2414
2415 ret = atmel_nand_controller_init(&nc->base, pdev, caps);
2416 if (ret)
2417 return ret;
2418
2419 ret = atmel_smc_nand_controller_init(nc);
2420 if (ret)
2421 return ret;
2422
2423 return atmel_nand_controller_add_nands(&nc->base);
2424}
2425
2426static int
2427atmel_smc_nand_controller_remove(struct atmel_nand_controller *nc)
2428{
2429 int ret;
2430
2431 ret = atmel_nand_controller_remove_nands(nc);
2432 if (ret)
2433 return ret;
2434
2435 atmel_nand_controller_cleanup(nc);
2436
2437 return 0;
2438}
2439
2440
2441
2442
2443
2444
2445
2446
2447static const struct atmel_nand_controller_ops at91rm9200_nc_ops = {
2448 .probe = atmel_smc_nand_controller_probe,
2449 .remove = atmel_smc_nand_controller_remove,
2450 .ecc_init = atmel_nand_ecc_init,
2451 .nand_init = atmel_smc_nand_init,
2452 .exec_op = atmel_smc_nand_exec_op,
2453};
2454
2455static const struct atmel_nand_controller_caps atmel_rm9200_nc_caps = {
2456 .ale_offs = BIT(21),
2457 .cle_offs = BIT(22),
2458 .ebi_csa_regmap_name = "atmel,matrix",
2459 .ops = &at91rm9200_nc_ops,
2460};
2461
2462static const struct atmel_nand_controller_ops atmel_smc_nc_ops = {
2463 .probe = atmel_smc_nand_controller_probe,
2464 .remove = atmel_smc_nand_controller_remove,
2465 .ecc_init = atmel_nand_ecc_init,
2466 .nand_init = atmel_smc_nand_init,
2467 .setup_interface = atmel_smc_nand_setup_interface,
2468 .exec_op = atmel_smc_nand_exec_op,
2469};
2470
2471static const struct atmel_nand_controller_caps atmel_sam9260_nc_caps = {
2472 .ale_offs = BIT(21),
2473 .cle_offs = BIT(22),
2474 .ebi_csa_regmap_name = "atmel,matrix",
2475 .ops = &atmel_smc_nc_ops,
2476};
2477
2478static const struct atmel_nand_controller_caps atmel_sam9261_nc_caps = {
2479 .ale_offs = BIT(22),
2480 .cle_offs = BIT(21),
2481 .ebi_csa_regmap_name = "atmel,matrix",
2482 .ops = &atmel_smc_nc_ops,
2483};
2484
2485static const struct atmel_nand_controller_caps atmel_sam9g45_nc_caps = {
2486 .has_dma = true,
2487 .ale_offs = BIT(21),
2488 .cle_offs = BIT(22),
2489 .ebi_csa_regmap_name = "atmel,matrix",
2490 .ops = &atmel_smc_nc_ops,
2491};
2492
2493static const struct atmel_nand_controller_caps microchip_sam9x60_nc_caps = {
2494 .has_dma = true,
2495 .ale_offs = BIT(21),
2496 .cle_offs = BIT(22),
2497 .ebi_csa_regmap_name = "microchip,sfr",
2498 .ops = &atmel_smc_nc_ops,
2499};
2500
2501
2502static const struct atmel_nand_controller_caps atmel_rm9200_nand_caps = {
2503 .ale_offs = BIT(21),
2504 .cle_offs = BIT(22),
2505 .ops = &atmel_smc_nc_ops,
2506 .legacy_of_bindings = true,
2507};
2508
2509static const struct atmel_nand_controller_caps atmel_sam9261_nand_caps = {
2510 .ale_offs = BIT(22),
2511 .cle_offs = BIT(21),
2512 .ops = &atmel_smc_nc_ops,
2513 .legacy_of_bindings = true,
2514};
2515
2516static const struct atmel_nand_controller_caps atmel_sam9g45_nand_caps = {
2517 .has_dma = true,
2518 .ale_offs = BIT(21),
2519 .cle_offs = BIT(22),
2520 .ops = &atmel_smc_nc_ops,
2521 .legacy_of_bindings = true,
2522};
2523
2524static const struct of_device_id atmel_nand_controller_of_ids[] = {
2525 {
2526 .compatible = "atmel,at91rm9200-nand-controller",
2527 .data = &atmel_rm9200_nc_caps,
2528 },
2529 {
2530 .compatible = "atmel,at91sam9260-nand-controller",
2531 .data = &atmel_sam9260_nc_caps,
2532 },
2533 {
2534 .compatible = "atmel,at91sam9261-nand-controller",
2535 .data = &atmel_sam9261_nc_caps,
2536 },
2537 {
2538 .compatible = "atmel,at91sam9g45-nand-controller",
2539 .data = &atmel_sam9g45_nc_caps,
2540 },
2541 {
2542 .compatible = "atmel,sama5d3-nand-controller",
2543 .data = &atmel_sama5_nc_caps,
2544 },
2545 {
2546 .compatible = "microchip,sam9x60-nand-controller",
2547 .data = µchip_sam9x60_nc_caps,
2548 },
2549
2550 {
2551 .compatible = "atmel,at91rm9200-nand",
2552 .data = &atmel_rm9200_nand_caps,
2553 },
2554 {
2555 .compatible = "atmel,sama5d4-nand",
2556 .data = &atmel_rm9200_nand_caps,
2557 },
2558 {
2559 .compatible = "atmel,sama5d2-nand",
2560 .data = &atmel_rm9200_nand_caps,
2561 },
2562 { },
2563};
2564MODULE_DEVICE_TABLE(of, atmel_nand_controller_of_ids);
2565
2566static int atmel_nand_controller_probe(struct platform_device *pdev)
2567{
2568 const struct atmel_nand_controller_caps *caps;
2569
2570 if (pdev->id_entry)
2571 caps = (void *)pdev->id_entry->driver_data;
2572 else
2573 caps = of_device_get_match_data(&pdev->dev);
2574
2575 if (!caps) {
2576 dev_err(&pdev->dev, "Could not retrieve NFC caps\n");
2577 return -EINVAL;
2578 }
2579
2580 if (caps->legacy_of_bindings) {
2581 struct device_node *nfc_node;
2582 u32 ale_offs = 21;
2583
2584
2585
2586
2587
2588 nfc_node = of_get_compatible_child(pdev->dev.of_node,
2589 "atmel,sama5d3-nfc");
2590 if (nfc_node) {
2591 caps = &atmel_sama5_nand_caps;
2592 of_node_put(nfc_node);
2593 }
2594
2595
2596
2597
2598
2599
2600
2601 if (!caps->has_dma &&
2602 of_property_read_bool(pdev->dev.of_node,
2603 "atmel,nand-has-dma"))
2604 caps = &atmel_sam9g45_nand_caps;
2605
2606
2607
2608
2609
2610
2611 of_property_read_u32(pdev->dev.of_node,
2612 "atmel,nand-addr-offset", &ale_offs);
2613 if (ale_offs != 21)
2614 caps = &atmel_sam9261_nand_caps;
2615 }
2616
2617 return caps->ops->probe(pdev, caps);
2618}
2619
2620static int atmel_nand_controller_remove(struct platform_device *pdev)
2621{
2622 struct atmel_nand_controller *nc = platform_get_drvdata(pdev);
2623
2624 return nc->caps->ops->remove(nc);
2625}
2626
2627static __maybe_unused int atmel_nand_controller_resume(struct device *dev)
2628{
2629 struct atmel_nand_controller *nc = dev_get_drvdata(dev);
2630 struct atmel_nand *nand;
2631
2632 if (nc->pmecc)
2633 atmel_pmecc_reset(nc->pmecc);
2634
2635 list_for_each_entry(nand, &nc->chips, node) {
2636 int i;
2637
2638 for (i = 0; i < nand->numcs; i++)
2639 nand_reset(&nand->base, i);
2640 }
2641
2642 return 0;
2643}
2644
2645static SIMPLE_DEV_PM_OPS(atmel_nand_controller_pm_ops, NULL,
2646 atmel_nand_controller_resume);
2647
2648static struct platform_driver atmel_nand_controller_driver = {
2649 .driver = {
2650 .name = "atmel-nand-controller",
2651 .of_match_table = of_match_ptr(atmel_nand_controller_of_ids),
2652 .pm = &atmel_nand_controller_pm_ops,
2653 },
2654 .probe = atmel_nand_controller_probe,
2655 .remove = atmel_nand_controller_remove,
2656};
2657module_platform_driver(atmel_nand_controller_driver);
2658
2659MODULE_LICENSE("GPL");
2660MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
2661MODULE_DESCRIPTION("NAND Flash Controller driver for Atmel SoCs");
2662MODULE_ALIAS("platform:atmel-nand-controller");
2663