2 * Driver for the built-in NAND controller of the Atheros AR934x SoCs
4 * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/nand.h>
17 #include <linux/mtd/partitions.h>
18 #include <linux/platform_device.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
22 #include <linux/platform/ar934x_nfc.h>
24 #define AR934X_NFC_REG_CMD 0x00
25 #define AR934X_NFC_REG_CTRL 0x04
26 #define AR934X_NFC_REG_STATUS 0x08
27 #define AR934X_NFC_REG_INT_MASK 0x0c
28 #define AR934X_NFC_REG_INT_STATUS 0x10
29 #define AR934X_NFC_REG_ECC_CTRL 0x14
30 #define AR934X_NFC_REG_ECC_OFFSET 0x18
31 #define AR934X_NFC_REG_ADDR0_0 0x1c
32 #define AR934X_NFC_REG_ADDR0_1 0x24
33 #define AR934X_NFC_REG_ADDR1_0 0x20
34 #define AR934X_NFC_REG_ADDR1_1 0x28
35 #define AR934X_NFC_REG_SPARE_SIZE 0x30
36 #define AR934X_NFC_REG_PROTECT 0x38
37 #define AR934X_NFC_REG_LOOKUP_EN 0x40
38 #define AR934X_NFC_REG_LOOKUP(_x) (0x44 + (_i) * 4)
39 #define AR934X_NFC_REG_DMA_ADDR 0x64
40 #define AR934X_NFC_REG_DMA_COUNT 0x68
41 #define AR934X_NFC_REG_DMA_CTRL 0x6c
42 #define AR934X_NFC_REG_MEM_CTRL 0x80
43 #define AR934X_NFC_REG_DATA_SIZE 0x84
44 #define AR934X_NFC_REG_READ_STATUS 0x88
45 #define AR934X_NFC_REG_TIME_SEQ 0x8c
46 #define AR934X_NFC_REG_TIMINGS_ASYN 0x90
47 #define AR934X_NFC_REG_TIMINGS_SYN 0x94
48 #define AR934X_NFC_REG_FIFO_DATA 0x98
49 #define AR934X_NFC_REG_TIME_MODE 0x9c
50 #define AR934X_NFC_REG_DMA_ADDR_OFFS 0xa0
51 #define AR934X_NFC_REG_FIFO_INIT 0xb0
52 #define AR934X_NFC_REG_GEN_SEQ_CTRL 0xb4
54 #define AR934X_NFC_CMD_CMD_SEQ_S 0
55 #define AR934X_NFC_CMD_CMD_SEQ_M 0x3f
56 #define AR934X_NFC_CMD_SEQ_1C 0x00
57 #define AR934X_NFC_CMD_SEQ_ERASE 0x0e
58 #define AR934X_NFC_CMD_SEQ_12 0x0c
59 #define AR934X_NFC_CMD_SEQ_1C1AXR 0x21
60 #define AR934X_NFC_CMD_SEQ_S 0x24
61 #define AR934X_NFC_CMD_SEQ_1C3AXR 0x27
62 #define AR934X_NFC_CMD_SEQ_1C5A1CXR 0x2a
63 #define AR934X_NFC_CMD_SEQ_18 0x32
64 #define AR934X_NFC_CMD_INPUT_SEL_SIU 0
65 #define AR934X_NFC_CMD_INPUT_SEL_DMA BIT(6)
66 #define AR934X_NFC_CMD_ADDR_SEL_0 0
67 #define AR934X_NFC_CMD_ADDR_SEL_1 BIT(7)
68 #define AR934X_NFC_CMD_CMD0_S 8
69 #define AR934X_NFC_CMD_CMD0_M 0xff
70 #define AR934X_NFC_CMD_CMD1_S 16
71 #define AR934X_NFC_CMD_CMD1_M 0xff
72 #define AR934X_NFC_CMD_CMD2_S 24
73 #define AR934X_NFC_CMD_CMD2_M 0xff
75 #define AR934X_NFC_CTRL_ADDR_CYCLE0_M 0x7
76 #define AR934X_NFC_CTRL_ADDR_CYCLE0_S 0
77 #define AR934X_NFC_CTRL_SPARE_EN BIT(3)
78 #define AR934X_NFC_CTRL_INT_EN BIT(4)
79 #define AR934X_NFC_CTRL_ECC_EN BIT(5)
80 #define AR934X_NFC_CTRL_BLOCK_SIZE_S 6
81 #define AR934X_NFC_CTRL_BLOCK_SIZE_M 0x3
82 #define AR934X_NFC_CTRL_BLOCK_SIZE_32 0
83 #define AR934X_NFC_CTRL_BLOCK_SIZE_64 1
84 #define AR934X_NFC_CTRL_BLOCK_SIZE_128 2
85 #define AR934X_NFC_CTRL_BLOCK_SIZE_256 3
86 #define AR934X_NFC_CTRL_PAGE_SIZE_S 8
87 #define AR934X_NFC_CTRL_PAGE_SIZE_M 0x7
88 #define AR934X_NFC_CTRL_PAGE_SIZE_256 0
89 #define AR934X_NFC_CTRL_PAGE_SIZE_512 1
90 #define AR934X_NFC_CTRL_PAGE_SIZE_1024 2
91 #define AR934X_NFC_CTRL_PAGE_SIZE_2048 3
92 #define AR934X_NFC_CTRL_PAGE_SIZE_4096 4
93 #define AR934X_NFC_CTRL_PAGE_SIZE_8192 5
94 #define AR934X_NFC_CTRL_PAGE_SIZE_16384 6
95 #define AR934X_NFC_CTRL_CUSTOM_SIZE_EN BIT(11)
96 #define AR934X_NFC_CTRL_IO_WIDTH_8BITS 0
97 #define AR934X_NFC_CTRL_IO_WIDTH_16BITS BIT(12)
98 #define AR934X_NFC_CTRL_LOOKUP_EN BIT(13)
99 #define AR934X_NFC_CTRL_PROT_EN BIT(14)
100 #define AR934X_NFC_CTRL_WORK_MODE_ASYNC 0
101 #define AR934X_NFC_CTRL_WORK_MODE_SYNC BIT(15)
102 #define AR934X_NFC_CTRL_ADDR0_AUTO_INC BIT(16)
103 #define AR934X_NFC_CTRL_ADDR1_AUTO_INC BIT(17)
104 #define AR934X_NFC_CTRL_ADDR_CYCLE1_M 0x7
105 #define AR934X_NFC_CTRL_ADDR_CYCLE1_S 18
106 #define AR934X_NFC_CTRL_SMALL_PAGE BIT(21)
108 #define AR934X_NFC_DMA_CTRL_DMA_START BIT(7)
109 #define AR934X_NFC_DMA_CTRL_DMA_DIR_WRITE 0
110 #define AR934X_NFC_DMA_CTRL_DMA_DIR_READ BIT(6)
111 #define AR934X_NFC_DMA_CTRL_DMA_MODE_SG BIT(5)
112 #define AR934X_NFC_DMA_CTRL_DMA_BURST_S 2
113 #define AR934X_NFC_DMA_CTRL_DMA_BURST_0 0
114 #define AR934X_NFC_DMA_CTRL_DMA_BURST_1 1
115 #define AR934X_NFC_DMA_CTRL_DMA_BURST_2 2
116 #define AR934X_NFC_DMA_CTRL_DMA_BURST_3 3
117 #define AR934X_NFC_DMA_CTRL_DMA_BURST_4 4
118 #define AR934X_NFC_DMA_CTRL_DMA_BURST_5 5
119 #define AR934X_NFC_DMA_CTRL_ERR_FLAG BIT(1)
120 #define AR934X_NFC_DMA_CTRL_DMA_READY BIT(0)
122 #define AR934X_NFC_INT_DEV_RDY(_x) BIT(4 + (_x))
123 #define AR934X_NFC_INT_CMD_END BIT(1)
125 /* default timing values */
126 #define AR934X_NFC_TIME_SEQ_DEFAULT 0x7fff
127 #define AR934X_NFC_TIMINGS_ASYN_DEFAULT 0x22
128 #define AR934X_NFC_TIMINGS_SYN_DEFAULT 0xf
130 #define AR934X_NFC_ID_BUF_SIZE 8
131 #define AR934X_NFC_DEV_READY_TIMEOUT 25 /* msecs */
132 #define AR934X_NFC_DMA_READY_TIMEOUT 25 /* msecs */
133 #define AR934X_NFC_DONE_TIMEOUT 1000
134 #define AR934X_NFC_DMA_RETRIES 20
136 #define AR934X_NFC_USE_IRQ true
137 #define AR934X_NFC_IRQ_MASK AR934X_NFC_INT_DEV_RDY(0)
139 #define AR934X_NFC_GENSEQ_SMALL_PAGE_READ 0x30043
141 #undef AR934X_NFC_DEBUG_DATA
142 #undef AR934X_NFC_DEBUG
146 static inline __attribute__ ((format (printf, 2, 3)))
147 void _nfc_dbg(struct ar934x_nfc *nfc, const char *fmt, ...)
151 #ifdef AR934X_NFC_DEBUG
152 #define nfc_dbg(_nfc, fmt, ...) \
153 dev_info((_nfc)->parent, fmt, ##__VA_ARGS__)
155 #define nfc_dbg(_nfc, fmt, ...) \
156 _nfc_dbg((_nfc), fmt, ##__VA_ARGS__)
157 #endif /* AR934X_NFC_DEBUG */
159 #ifdef AR934X_NFC_DEBUG_DATA
161 nfc_debug_data(const char *label, void *data, int len)
163 print_hex_dump(KERN_WARNING, label, DUMP_PREFIX_OFFSET, 16, 1,
168 nfc_debug_data(const char *label, void *data, int len) {}
169 #endif /* AR934X_NFC_DEBUG_DATA */
173 struct nand_chip nand_chip;
174 struct device *parent;
176 void (*select_chip)(int chip_no);
178 wait_queue_head_t irq_waitq;
180 bool spurious_irq_expected;
185 unsigned int addr_count0;
186 unsigned int addr_count1;
190 unsigned int buf_size;
193 int erase1_page_addr;
195 int rndout_page_addr;
203 static void ar934x_nfc_restart(struct ar934x_nfc *nfc);
206 ar934x_nfc_wr(struct ar934x_nfc *nfc, unsigned reg, u32 val)
208 __raw_writel(val, nfc->base + reg);
212 ar934x_nfc_rr(struct ar934x_nfc *nfc, unsigned reg)
214 return __raw_readl(nfc->base + reg);
217 static inline struct ar934x_nfc_platform_data *
218 ar934x_nfc_get_platform_data(struct ar934x_nfc *nfc)
220 return nfc->parent->platform_data;
224 ar934x_nfc *mtd_to_ar934x_nfc(struct mtd_info *mtd)
226 return container_of(mtd, struct ar934x_nfc, mtd);
229 static inline bool ar934x_nfc_use_irq(struct ar934x_nfc *nfc)
231 return AR934X_NFC_USE_IRQ;
234 static inline void ar934x_nfc_write_cmd_reg(struct ar934x_nfc *nfc, u32 cmd_reg)
238 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CMD, cmd_reg);
240 ar934x_nfc_rr(nfc, AR934X_NFC_REG_CMD);
244 __ar934x_nfc_dev_ready(struct ar934x_nfc *nfc)
248 status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_STATUS);
249 return (status & 0xff) == 0xff;
253 __ar934x_nfc_is_dma_ready(struct ar934x_nfc *nfc)
257 status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_DMA_CTRL);
258 return (status & AR934X_NFC_DMA_CTRL_DMA_READY) != 0;
262 ar934x_nfc_wait_dev_ready(struct ar934x_nfc *nfc)
264 unsigned long timeout;
266 timeout = jiffies + msecs_to_jiffies(AR934X_NFC_DEV_READY_TIMEOUT);
268 if (__ar934x_nfc_dev_ready(nfc))
270 } while time_before(jiffies, timeout);
272 nfc_dbg(nfc, "timeout waiting for device ready, status:%08x int:%08x\n",
273 ar934x_nfc_rr(nfc, AR934X_NFC_REG_STATUS),
274 ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS));
279 ar934x_nfc_wait_dma_ready(struct ar934x_nfc *nfc)
281 unsigned long timeout;
283 timeout = jiffies + msecs_to_jiffies(AR934X_NFC_DMA_READY_TIMEOUT);
285 if (__ar934x_nfc_is_dma_ready(nfc))
287 } while time_before(jiffies, timeout);
289 nfc_dbg(nfc, "timeout waiting for DMA ready, dma_ctrl:%08x\n",
290 ar934x_nfc_rr(nfc, AR934X_NFC_REG_DMA_CTRL));
295 ar934x_nfc_wait_irq(struct ar934x_nfc *nfc)
300 timeout = wait_event_timeout(nfc->irq_waitq,
301 (nfc->irq_status & AR934X_NFC_IRQ_MASK) != 0,
302 msecs_to_jiffies(AR934X_NFC_DEV_READY_TIMEOUT));
306 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_MASK, 0);
307 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
309 ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS);
312 "timeout waiting for interrupt, status:%08x\n",
322 ar934x_nfc_wait_done(struct ar934x_nfc *nfc)
326 if (ar934x_nfc_use_irq(nfc))
327 ret = ar934x_nfc_wait_irq(nfc);
329 ret = ar934x_nfc_wait_dev_ready(nfc);
334 return ar934x_nfc_wait_dma_ready(nfc);
338 ar934x_nfc_alloc_buf(struct ar934x_nfc *nfc, unsigned size)
340 nfc->buf = dma_alloc_coherent(nfc->parent, size,
341 &nfc->buf_dma, GFP_KERNEL);
342 if (nfc->buf == NULL) {
343 dev_err(nfc->parent, "no memory for DMA buffer\n");
347 nfc->buf_size = size;
348 nfc_dbg(nfc, "buf:%p size:%u\n", nfc->buf, nfc->buf_size);
354 ar934x_nfc_free_buf(struct ar934x_nfc *nfc)
356 dma_free_coherent(nfc->parent, nfc->buf_size, nfc->buf, nfc->buf_dma);
360 ar934x_nfc_get_addr(struct ar934x_nfc *nfc, int column, int page_addr,
361 u32 *addr0, u32 *addr1)
370 a0 = (page_addr & 0xffff) << 16;
371 a1 = (page_addr >> 16) & 0xf;
372 } else if (page_addr != -1) {
373 /* SEQIN, READ0, etc.. */
375 /* TODO: handle 16bit bus width */
376 if (nfc->small_page) {
378 a0 |= (page_addr & 0xff) << 8;
379 a0 |= ((page_addr >> 8) & 0xff) << 16;
380 a0 |= ((page_addr >> 16) & 0xff) << 24;
382 a0 = column & 0x0FFF;
383 a0 |= (page_addr & 0xffff) << 16;
385 if (nfc->addr_count0 > 4)
386 a1 = (page_addr >> 16) & 0xf;
395 ar934x_nfc_send_cmd(struct ar934x_nfc *nfc, unsigned command)
399 cmd_reg = AR934X_NFC_CMD_INPUT_SEL_SIU | AR934X_NFC_CMD_ADDR_SEL_0 |
400 AR934X_NFC_CMD_SEQ_1C;
401 cmd_reg |= (command & AR934X_NFC_CMD_CMD0_M) << AR934X_NFC_CMD_CMD0_S;
403 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
404 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
406 ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
407 ar934x_nfc_wait_dev_ready(nfc);
411 ar934x_nfc_do_rw_command(struct ar934x_nfc *nfc, int column, int page_addr,
412 int len, u32 cmd_reg, u32 ctrl_reg, bool write)
422 if (WARN_ON(len > nfc->buf_size))
423 dev_err(nfc->parent, "len=%d > buf_size=%d", len, nfc->buf_size);
426 dma_ctrl = AR934X_NFC_DMA_CTRL_DMA_DIR_WRITE;
429 dma_ctrl = AR934X_NFC_DMA_CTRL_DMA_DIR_READ;
430 dir = DMA_FROM_DEVICE;
433 ar934x_nfc_get_addr(nfc, column, page_addr, &addr0, &addr1);
435 dma_ctrl |= AR934X_NFC_DMA_CTRL_DMA_START |
436 (AR934X_NFC_DMA_CTRL_DMA_BURST_3 <<
437 AR934X_NFC_DMA_CTRL_DMA_BURST_S);
439 cmd_reg |= AR934X_NFC_CMD_INPUT_SEL_DMA | AR934X_NFC_CMD_ADDR_SEL_0;
440 ctrl_reg |= AR934X_NFC_CTRL_INT_EN;
442 nfc_dbg(nfc, "%s a0:%08x a1:%08x len:%x cmd:%08x dma:%08x ctrl:%08x\n",
443 (write) ? "write" : "read",
444 addr0, addr1, len, cmd_reg, dma_ctrl, ctrl_reg);
447 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
448 ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_0, addr0);
449 ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_1, addr1);
450 ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_ADDR, nfc->buf_dma);
451 ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_COUNT, len);
452 ar934x_nfc_wr(nfc, AR934X_NFC_REG_DATA_SIZE, len);
453 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, ctrl_reg);
454 ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_CTRL, dma_ctrl);
456 if (ar934x_nfc_use_irq(nfc)) {
457 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_MASK, AR934X_NFC_IRQ_MASK);
459 ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_MASK);
462 ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
463 err = ar934x_nfc_wait_done(nfc);
465 dev_dbg(nfc->parent, "%s operation stuck at page %d\n",
466 (write) ? "write" : "read", page_addr);
468 ar934x_nfc_restart(nfc);
469 if (retries++ < AR934X_NFC_DMA_RETRIES)
472 dev_err(nfc->parent, "%s operation failed on page %d\n",
473 (write) ? "write" : "read", page_addr);
478 ar934x_nfc_send_readid(struct ar934x_nfc *nfc, unsigned command)
482 nfc_dbg(nfc, "readid, cmd:%02x\n", command);
484 cmd_reg = AR934X_NFC_CMD_SEQ_1C1AXR;
485 cmd_reg |= (command & AR934X_NFC_CMD_CMD0_M) << AR934X_NFC_CMD_CMD0_S;
487 ar934x_nfc_do_rw_command(nfc, -1, -1, AR934X_NFC_ID_BUF_SIZE, cmd_reg,
488 nfc->ctrl_reg, false);
490 nfc_debug_data("[id] ", nfc->buf, AR934X_NFC_ID_BUF_SIZE);
494 ar934x_nfc_send_read(struct ar934x_nfc *nfc, unsigned command, int column,
495 int page_addr, int len)
499 nfc_dbg(nfc, "read, column=%d page=%d len=%d\n",
500 column, page_addr, len);
502 cmd_reg = (command & AR934X_NFC_CMD_CMD0_M) << AR934X_NFC_CMD_CMD0_S;
504 if (nfc->small_page) {
505 cmd_reg |= AR934X_NFC_CMD_SEQ_18;
507 cmd_reg |= NAND_CMD_READSTART << AR934X_NFC_CMD_CMD1_S;
508 cmd_reg |= AR934X_NFC_CMD_SEQ_1C5A1CXR;
511 ar934x_nfc_do_rw_command(nfc, column, page_addr, len,
512 cmd_reg, nfc->ctrl_reg, false);
514 nfc_debug_data("[data] ", nfc->buf, len);
518 ar934x_nfc_send_erase(struct ar934x_nfc *nfc, unsigned command, int column,
525 ar934x_nfc_get_addr(nfc, column, page_addr, &addr0, &addr1);
527 ctrl_reg = nfc->ctrl_reg;
528 if (nfc->small_page) {
529 /* override number of address cycles for the erase command */
530 ctrl_reg &= ~(AR934X_NFC_CTRL_ADDR_CYCLE0_M <<
531 AR934X_NFC_CTRL_ADDR_CYCLE0_S);
532 ctrl_reg &= ~(AR934X_NFC_CTRL_ADDR_CYCLE1_M <<
533 AR934X_NFC_CTRL_ADDR_CYCLE1_S);
534 ctrl_reg &= ~(AR934X_NFC_CTRL_SMALL_PAGE);
535 ctrl_reg |= (nfc->addr_count0 + 1) <<
536 AR934X_NFC_CTRL_ADDR_CYCLE0_S;
539 cmd_reg = NAND_CMD_ERASE1 << AR934X_NFC_CMD_CMD0_S;
540 cmd_reg |= command << AR934X_NFC_CMD_CMD1_S;
541 cmd_reg |= AR934X_NFC_CMD_SEQ_ERASE;
543 nfc_dbg(nfc, "erase page %d, a0:%08x a1:%08x cmd:%08x ctrl:%08x\n",
544 page_addr, addr0, addr1, cmd_reg, ctrl_reg);
546 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
547 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, ctrl_reg);
548 ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_0, addr0);
549 ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_1, addr1);
551 ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
552 ar934x_nfc_wait_dev_ready(nfc);
556 ar934x_nfc_send_write(struct ar934x_nfc *nfc, unsigned command, int column,
557 int page_addr, int len)
561 nfc_dbg(nfc, "write, column=%d page=%d len=%d\n",
562 column, page_addr, len);
564 nfc_debug_data("[data] ", nfc->buf, len);
566 cmd_reg = NAND_CMD_SEQIN << AR934X_NFC_CMD_CMD0_S;
567 cmd_reg |= command << AR934X_NFC_CMD_CMD1_S;
568 cmd_reg |= AR934X_NFC_CMD_SEQ_12;
570 ar934x_nfc_do_rw_command(nfc, column, page_addr, len,
571 cmd_reg, nfc->ctrl_reg, true);
575 ar934x_nfc_read_status(struct ar934x_nfc *nfc)
580 cmd_reg = NAND_CMD_STATUS << AR934X_NFC_CMD_CMD0_S;
581 cmd_reg |= AR934X_NFC_CMD_SEQ_S;
583 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
584 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
586 ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
587 ar934x_nfc_wait_dev_ready(nfc);
589 status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_READ_STATUS);
591 nfc_dbg(nfc, "read status, cmd:%08x status:%02x\n",
592 cmd_reg, (status & 0xff));
594 nfc->buf[0 ^ 3] = status;
598 ar934x_nfc_cmdfunc(struct mtd_info *mtd, unsigned int command, int column,
601 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
603 if (command != NAND_CMD_PAGEPROG)
608 ar934x_nfc_send_cmd(nfc, command);
611 case NAND_CMD_READID:
612 ar934x_nfc_send_readid(nfc, command);
617 if (nfc->small_page) {
618 ar934x_nfc_send_read(nfc, command, column, page_addr,
619 mtd->writesize + mtd->oobsize);
621 ar934x_nfc_send_read(nfc, command, 0, page_addr,
622 mtd->writesize + mtd->oobsize);
623 nfc->buf_index = column;
624 nfc->rndout_page_addr = page_addr;
625 nfc->rndout_read_cmd = command;
629 case NAND_CMD_READOOB:
631 ar934x_nfc_send_read(nfc, NAND_CMD_READOOB,
635 ar934x_nfc_send_read(nfc, NAND_CMD_READ0,
636 mtd->writesize, page_addr,
640 case NAND_CMD_RNDOUT:
641 if (WARN_ON(nfc->small_page))
644 /* emulate subpage read */
645 ar934x_nfc_send_read(nfc, nfc->rndout_read_cmd, 0,
646 nfc->rndout_page_addr,
647 mtd->writesize + mtd->oobsize);
648 nfc->buf_index = column;
651 case NAND_CMD_ERASE1:
652 nfc->erase1_page_addr = page_addr;
655 case NAND_CMD_ERASE2:
656 ar934x_nfc_send_erase(nfc, command, -1, nfc->erase1_page_addr);
659 case NAND_CMD_STATUS:
660 ar934x_nfc_read_status(nfc);
664 if (nfc->small_page) {
665 /* output read command */
666 if (column >= mtd->writesize) {
667 column -= mtd->writesize;
668 nfc->seqin_read_cmd = NAND_CMD_READOOB;
669 } else if (column < 256) {
670 nfc->seqin_read_cmd = NAND_CMD_READ0;
673 nfc->seqin_read_cmd = NAND_CMD_READ1;
676 nfc->seqin_read_cmd = NAND_CMD_READ0;
678 nfc->seqin_column = column;
679 nfc->seqin_page_addr = page_addr;
682 case NAND_CMD_PAGEPROG:
684 ar934x_nfc_send_cmd(nfc, nfc->seqin_read_cmd);
686 ar934x_nfc_send_write(nfc, command, nfc->seqin_column,
687 nfc->seqin_page_addr,
693 "unsupported command: %x, column:%d page_addr=%d\n",
694 command, column, page_addr);
700 ar934x_nfc_dev_ready(struct mtd_info *mtd)
702 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
704 return __ar934x_nfc_dev_ready(nfc);
708 ar934x_nfc_select_chip(struct mtd_info *mtd, int chip_no)
710 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
712 if (nfc->select_chip)
713 nfc->select_chip(chip_no);
717 ar934x_nfc_read_byte(struct mtd_info *mtd)
719 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
720 unsigned int buf_index;
723 WARN_ON(nfc->buf_index >= nfc->buf_size);
725 buf_index = nfc->buf_index ^ 3;
726 data = nfc->buf[buf_index];
733 ar934x_nfc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
735 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
738 WARN_ON(nfc->buf_index + len > nfc->buf_size);
740 for (i = 0; i < len; i++) {
741 nfc->buf[nfc->buf_index ^ 3] = buf[i];
747 ar934x_nfc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
749 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
753 WARN_ON(nfc->buf_index + len > nfc->buf_size);
755 buf_index = nfc->buf_index;
757 for (i = 0; i < len; i++) {
758 buf[i] = nfc->buf[buf_index ^ 3];
762 nfc->buf_index = buf_index;
766 ar934x_nfc_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
770 for (i = 0; i < len; i++)
771 if (buf[i] != ar934x_nfc_read_byte(mtd))
778 ar934x_nfc_hw_init(struct ar934x_nfc *nfc)
780 struct ar934x_nfc_platform_data *pdata;
782 pdata = ar934x_nfc_get_platform_data(nfc);
783 if (pdata->hw_reset) {
784 pdata->hw_reset(true);
785 pdata->hw_reset(false);
790 * TODO: make it configurable via platform data
792 ar934x_nfc_wr(nfc, AR934X_NFC_REG_TIME_SEQ,
793 AR934X_NFC_TIME_SEQ_DEFAULT);
794 ar934x_nfc_wr(nfc, AR934X_NFC_REG_TIMINGS_ASYN,
795 AR934X_NFC_TIMINGS_ASYN_DEFAULT);
796 ar934x_nfc_wr(nfc, AR934X_NFC_REG_TIMINGS_SYN,
797 AR934X_NFC_TIMINGS_SYN_DEFAULT);
799 /* disable WP on all chips, and select chip 0 */
800 ar934x_nfc_wr(nfc, AR934X_NFC_REG_MEM_CTRL, 0xff00);
802 ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_ADDR_OFFS, 0);
804 /* initialize Control register */
805 nfc->ctrl_reg = AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
806 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
808 if (nfc->small_page) {
809 /* Setup generic sequence register for small page reads. */
810 ar934x_nfc_wr(nfc, AR934X_NFC_REG_GEN_SEQ_CTRL,
811 AR934X_NFC_GENSEQ_SMALL_PAGE_READ);
816 ar934x_nfc_restart(struct ar934x_nfc *nfc)
820 if (nfc->select_chip)
821 nfc->select_chip(-1);
823 ctrl_reg = nfc->ctrl_reg;
824 ar934x_nfc_hw_init(nfc);
825 nfc->ctrl_reg = ctrl_reg;
827 if (nfc->select_chip)
830 ar934x_nfc_send_cmd(nfc, NAND_CMD_RESET);
834 ar934x_nfc_irq_handler(int irq, void *data)
836 struct ar934x_nfc *nfc = data;
839 status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS);
841 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
843 ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS);
845 status &= ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_MASK);
847 nfc_dbg(nfc, "got IRQ, status:%08x\n", status);
849 nfc->irq_status = status;
850 nfc->spurious_irq_expected = true;
851 wake_up(&nfc->irq_waitq);
853 if (nfc->spurious_irq_expected) {
854 nfc->spurious_irq_expected = false;
856 dev_warn(nfc->parent, "spurious interrupt\n");
864 ar934x_nfc_init_tail(struct mtd_info *mtd)
866 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
867 struct nand_chip *chip = &nfc->nand_chip;
872 switch (mtd->oobsize) {
876 ar934x_nfc_wr(nfc, AR934X_NFC_REG_SPARE_SIZE, mtd->oobsize);
880 dev_err(nfc->parent, "unsupported OOB size: %d bytes\n",
885 ctrl = AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
887 switch (mtd->erasesize / mtd->writesize) {
889 t = AR934X_NFC_CTRL_BLOCK_SIZE_32;
893 t = AR934X_NFC_CTRL_BLOCK_SIZE_64;
897 t = AR934X_NFC_CTRL_BLOCK_SIZE_128;
901 t = AR934X_NFC_CTRL_BLOCK_SIZE_256;
905 dev_err(nfc->parent, "unsupported block size: %u\n",
906 mtd->erasesize / mtd->writesize);
910 ctrl |= t << AR934X_NFC_CTRL_BLOCK_SIZE_S;
912 switch (mtd->writesize) {
915 t = AR934X_NFC_CTRL_PAGE_SIZE_256;
920 t = AR934X_NFC_CTRL_PAGE_SIZE_512;
924 t = AR934X_NFC_CTRL_PAGE_SIZE_1024;
928 t = AR934X_NFC_CTRL_PAGE_SIZE_2048;
932 t = AR934X_NFC_CTRL_PAGE_SIZE_4096;
936 t = AR934X_NFC_CTRL_PAGE_SIZE_8192;
940 t = AR934X_NFC_CTRL_PAGE_SIZE_16384;
944 dev_err(nfc->parent, "unsupported write size: %d bytes\n",
949 ctrl |= t << AR934X_NFC_CTRL_PAGE_SIZE_S;
951 if (nfc->small_page) {
952 ctrl |= AR934X_NFC_CTRL_SMALL_PAGE;
954 if (chip->chipsize > (32 << 20)) {
955 nfc->addr_count0 = 4;
956 nfc->addr_count1 = 3;
957 } else if (chip->chipsize > (2 << 16)) {
958 nfc->addr_count0 = 3;
959 nfc->addr_count1 = 2;
961 nfc->addr_count0 = 2;
962 nfc->addr_count1 = 1;
965 if (chip->chipsize > (128 << 20)) {
966 nfc->addr_count0 = 5;
967 nfc->addr_count1 = 3;
968 } else if (chip->chipsize > (8 << 16)) {
969 nfc->addr_count0 = 4;
970 nfc->addr_count1 = 2;
972 nfc->addr_count0 = 3;
973 nfc->addr_count1 = 1;
977 ctrl |= nfc->addr_count0 << AR934X_NFC_CTRL_ADDR_CYCLE0_S;
978 ctrl |= nfc->addr_count1 << AR934X_NFC_CTRL_ADDR_CYCLE1_S;
980 nfc->ctrl_reg = ctrl;
981 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
983 ar934x_nfc_free_buf(nfc);
984 err = ar934x_nfc_alloc_buf(nfc, mtd->writesize + mtd->oobsize);
990 ar934x_nfc_probe(struct platform_device *pdev)
992 static const char *part_probes[] = { "cmdlinepart", NULL, };
993 struct ar934x_nfc_platform_data *pdata;
994 struct ar934x_nfc *nfc;
995 struct resource *res;
996 struct mtd_info *mtd;
997 struct nand_chip *nand;
998 struct mtd_part_parser_data ppdata;
1001 pdata = pdev->dev.platform_data;
1002 if (pdata == NULL) {
1003 dev_err(&pdev->dev, "no platform data defined\n");
1007 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1009 dev_err(&pdev->dev, "failed to get I/O memory\n");
1013 nfc = kzalloc(sizeof(struct ar934x_nfc), GFP_KERNEL);
1015 dev_err(&pdev->dev, "failed to allocate driver data\n");
1019 nfc->base = ioremap(res->start, resource_size(res));
1020 if (nfc->base == NULL) {
1021 dev_err(&pdev->dev, "failed to remap I/O memory\n");
1026 nfc->irq = platform_get_irq(pdev, 0);
1028 dev_err(&pdev->dev, "no IRQ resource specified\n");
1033 init_waitqueue_head(&nfc->irq_waitq);
1034 ret = request_irq(nfc->irq, ar934x_nfc_irq_handler, IRQF_DISABLED,
1035 dev_name(&pdev->dev), nfc);
1037 dev_err(&pdev->dev, "requast_irq failed, err:%d\n", ret);
1041 nfc->parent = &pdev->dev;
1042 nfc->select_chip = pdata->select_chip;
1044 nand = &nfc->nand_chip;
1048 mtd->owner = THIS_MODULE;
1050 mtd->name = pdata->name;
1052 mtd->name = dev_name(&pdev->dev);
1054 nand->chip_delay = 25;
1055 nand->ecc.mode = NAND_ECC_SOFT;
1057 nand->dev_ready = ar934x_nfc_dev_ready;
1058 nand->cmdfunc = ar934x_nfc_cmdfunc;
1059 nand->read_byte = ar934x_nfc_read_byte;
1060 nand->write_buf = ar934x_nfc_write_buf;
1061 nand->read_buf = ar934x_nfc_read_buf;
1062 nand->verify_buf = ar934x_nfc_verify_buf;
1063 nand->select_chip = ar934x_nfc_select_chip;
1065 ret = ar934x_nfc_alloc_buf(nfc, AR934X_NFC_ID_BUF_SIZE);
1069 platform_set_drvdata(pdev, nfc);
1071 ar934x_nfc_hw_init(nfc);
1073 ret = nand_scan_ident(mtd, 1, NULL);
1075 dev_err(&pdev->dev, "nand_scan_ident failed, err:%d\n", ret);
1079 ret = ar934x_nfc_init_tail(mtd);
1081 dev_err(&pdev->dev, "init tail failed, err:%d\n", ret);
1085 if (pdata->scan_fixup) {
1086 ret = pdata->scan_fixup(mtd);
1091 ret = nand_scan_tail(mtd);
1093 dev_err(&pdev->dev, "scan tail failed, err:%d\n", ret);
1097 memset(&ppdata, '\0', sizeof(ppdata));
1098 ret = mtd_device_parse_register(mtd, part_probes, &ppdata,
1099 pdata->parts, pdata->nr_parts);
1101 dev_err(&pdev->dev, "unable to register mtd, err:%d\n", ret);
1108 ar934x_nfc_free_buf(nfc);
1110 free_irq(nfc->irq, nfc);
1115 platform_set_drvdata(pdev, NULL);
1119 static int __devexit
1120 ar934x_nfc_remove(struct platform_device *pdev)
1122 struct ar934x_nfc *nfc;
1124 nfc = platform_get_drvdata(pdev);
1126 nand_release(&nfc->mtd);
1127 ar934x_nfc_free_buf(nfc);
1128 free_irq(nfc->irq, nfc);
1136 static struct platform_driver ar934x_nfc_driver = {
1137 .probe = ar934x_nfc_probe,
1138 .remove = __devexit_p(ar934x_nfc_remove),
1140 .name = AR934X_NFC_DRIVER_NAME,
1141 .owner = THIS_MODULE,
1145 module_platform_driver(ar934x_nfc_driver);
1147 MODULE_LICENSE("GPL v2");
1148 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1149 MODULE_DESCRIPTION("Atheros AR934x NAND Flash Controller driver");
1150 MODULE_ALIAS("platform:" AR934X_NFC_DRIVER_NAME);