58198e446562d96c1a8f3207de7dab81c94a4e7e
[openwrt/staging/rmilecki.git] / target / linux / ath79 / files / drivers / mtd / nand / raw / ar934x_nand.c
1 /*
2 * Driver for the built-in NAND controller of the Atheros AR934x SoCs
3 *
4 * Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
5 *
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.
9 */
10
11 #include <linux/version.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/rawnand.h>
18 #include <linux/mtd/partitions.h>
19 #include <linux/platform_device.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/reset.h>
25
26 #define AR934X_NFC_DRIVER_NAME "ar934x-nand"
27
28 #define AR934X_NFC_REG_CMD 0x00
29 #define AR934X_NFC_REG_CTRL 0x04
30 #define AR934X_NFC_REG_STATUS 0x08
31 #define AR934X_NFC_REG_INT_MASK 0x0c
32 #define AR934X_NFC_REG_INT_STATUS 0x10
33 #define AR934X_NFC_REG_ECC_CTRL 0x14
34 #define AR934X_NFC_REG_ECC_OFFSET 0x18
35 #define AR934X_NFC_REG_ADDR0_0 0x1c
36 #define AR934X_NFC_REG_ADDR0_1 0x24
37 #define AR934X_NFC_REG_ADDR1_0 0x20
38 #define AR934X_NFC_REG_ADDR1_1 0x28
39 #define AR934X_NFC_REG_SPARE_SIZE 0x30
40 #define AR934X_NFC_REG_PROTECT 0x38
41 #define AR934X_NFC_REG_LOOKUP_EN 0x40
42 #define AR934X_NFC_REG_LOOKUP(_x) (0x44 + (_i) * 4)
43 #define AR934X_NFC_REG_DMA_ADDR 0x64
44 #define AR934X_NFC_REG_DMA_COUNT 0x68
45 #define AR934X_NFC_REG_DMA_CTRL 0x6c
46 #define AR934X_NFC_REG_MEM_CTRL 0x80
47 #define AR934X_NFC_REG_DATA_SIZE 0x84
48 #define AR934X_NFC_REG_READ_STATUS 0x88
49 #define AR934X_NFC_REG_TIME_SEQ 0x8c
50 #define AR934X_NFC_REG_TIMINGS_ASYN 0x90
51 #define AR934X_NFC_REG_TIMINGS_SYN 0x94
52 #define AR934X_NFC_REG_FIFO_DATA 0x98
53 #define AR934X_NFC_REG_TIME_MODE 0x9c
54 #define AR934X_NFC_REG_DMA_ADDR_OFFS 0xa0
55 #define AR934X_NFC_REG_FIFO_INIT 0xb0
56 #define AR934X_NFC_REG_GEN_SEQ_CTRL 0xb4
57
58 #define AR934X_NFC_CMD_CMD_SEQ_S 0
59 #define AR934X_NFC_CMD_CMD_SEQ_M 0x3f
60 #define AR934X_NFC_CMD_SEQ_1C 0x00
61 #define AR934X_NFC_CMD_SEQ_ERASE 0x0e
62 #define AR934X_NFC_CMD_SEQ_12 0x0c
63 #define AR934X_NFC_CMD_SEQ_1C1AXR 0x21
64 #define AR934X_NFC_CMD_SEQ_S 0x24
65 #define AR934X_NFC_CMD_SEQ_1C3AXR 0x27
66 #define AR934X_NFC_CMD_SEQ_1C5A1CXR 0x2a
67 #define AR934X_NFC_CMD_SEQ_18 0x32
68 #define AR934X_NFC_CMD_INPUT_SEL_SIU 0
69 #define AR934X_NFC_CMD_INPUT_SEL_DMA BIT(6)
70 #define AR934X_NFC_CMD_ADDR_SEL_0 0
71 #define AR934X_NFC_CMD_ADDR_SEL_1 BIT(7)
72 #define AR934X_NFC_CMD_CMD0_S 8
73 #define AR934X_NFC_CMD_CMD0_M 0xff
74 #define AR934X_NFC_CMD_CMD1_S 16
75 #define AR934X_NFC_CMD_CMD1_M 0xff
76 #define AR934X_NFC_CMD_CMD2_S 24
77 #define AR934X_NFC_CMD_CMD2_M 0xff
78
79 #define AR934X_NFC_CTRL_ADDR_CYCLE0_M 0x7
80 #define AR934X_NFC_CTRL_ADDR_CYCLE0_S 0
81 #define AR934X_NFC_CTRL_SPARE_EN BIT(3)
82 #define AR934X_NFC_CTRL_INT_EN BIT(4)
83 #define AR934X_NFC_CTRL_ECC_EN BIT(5)
84 #define AR934X_NFC_CTRL_BLOCK_SIZE_S 6
85 #define AR934X_NFC_CTRL_BLOCK_SIZE_M 0x3
86 #define AR934X_NFC_CTRL_BLOCK_SIZE_32 0
87 #define AR934X_NFC_CTRL_BLOCK_SIZE_64 1
88 #define AR934X_NFC_CTRL_BLOCK_SIZE_128 2
89 #define AR934X_NFC_CTRL_BLOCK_SIZE_256 3
90 #define AR934X_NFC_CTRL_PAGE_SIZE_S 8
91 #define AR934X_NFC_CTRL_PAGE_SIZE_M 0x7
92 #define AR934X_NFC_CTRL_PAGE_SIZE_256 0
93 #define AR934X_NFC_CTRL_PAGE_SIZE_512 1
94 #define AR934X_NFC_CTRL_PAGE_SIZE_1024 2
95 #define AR934X_NFC_CTRL_PAGE_SIZE_2048 3
96 #define AR934X_NFC_CTRL_PAGE_SIZE_4096 4
97 #define AR934X_NFC_CTRL_PAGE_SIZE_8192 5
98 #define AR934X_NFC_CTRL_PAGE_SIZE_16384 6
99 #define AR934X_NFC_CTRL_CUSTOM_SIZE_EN BIT(11)
100 #define AR934X_NFC_CTRL_IO_WIDTH_8BITS 0
101 #define AR934X_NFC_CTRL_IO_WIDTH_16BITS BIT(12)
102 #define AR934X_NFC_CTRL_LOOKUP_EN BIT(13)
103 #define AR934X_NFC_CTRL_PROT_EN BIT(14)
104 #define AR934X_NFC_CTRL_WORK_MODE_ASYNC 0
105 #define AR934X_NFC_CTRL_WORK_MODE_SYNC BIT(15)
106 #define AR934X_NFC_CTRL_ADDR0_AUTO_INC BIT(16)
107 #define AR934X_NFC_CTRL_ADDR1_AUTO_INC BIT(17)
108 #define AR934X_NFC_CTRL_ADDR_CYCLE1_M 0x7
109 #define AR934X_NFC_CTRL_ADDR_CYCLE1_S 18
110 #define AR934X_NFC_CTRL_SMALL_PAGE BIT(21)
111
112 #define AR934X_NFC_DMA_CTRL_DMA_START BIT(7)
113 #define AR934X_NFC_DMA_CTRL_DMA_DIR_WRITE 0
114 #define AR934X_NFC_DMA_CTRL_DMA_DIR_READ BIT(6)
115 #define AR934X_NFC_DMA_CTRL_DMA_MODE_SG BIT(5)
116 #define AR934X_NFC_DMA_CTRL_DMA_BURST_S 2
117 #define AR934X_NFC_DMA_CTRL_DMA_BURST_0 0
118 #define AR934X_NFC_DMA_CTRL_DMA_BURST_1 1
119 #define AR934X_NFC_DMA_CTRL_DMA_BURST_2 2
120 #define AR934X_NFC_DMA_CTRL_DMA_BURST_3 3
121 #define AR934X_NFC_DMA_CTRL_DMA_BURST_4 4
122 #define AR934X_NFC_DMA_CTRL_DMA_BURST_5 5
123 #define AR934X_NFC_DMA_CTRL_ERR_FLAG BIT(1)
124 #define AR934X_NFC_DMA_CTRL_DMA_READY BIT(0)
125
126 #define AR934X_NFC_INT_DEV_RDY(_x) BIT(4 + (_x))
127 #define AR934X_NFC_INT_CMD_END BIT(1)
128
129 #define AR934X_NFC_ECC_CTRL_ERR_THRES_S 8
130 #define AR934X_NFC_ECC_CTRL_ERR_THRES_M 0x1f
131 #define AR934X_NFC_ECC_CTRL_ECC_CAP_S 5
132 #define AR934X_NFC_ECC_CTRL_ECC_CAP_M 0x7
133 #define AR934X_NFC_ECC_CTRL_ECC_CAP_2 0
134 #define AR934X_NFC_ECC_CTRL_ECC_CAP_4 1
135 #define AR934X_NFC_ECC_CTRL_ECC_CAP_6 2
136 #define AR934X_NFC_ECC_CTRL_ECC_CAP_8 3
137 #define AR934X_NFC_ECC_CTRL_ECC_CAP_10 4
138 #define AR934X_NFC_ECC_CTRL_ECC_CAP_12 5
139 #define AR934X_NFC_ECC_CTRL_ECC_CAP_14 6
140 #define AR934X_NFC_ECC_CTRL_ECC_CAP_16 7
141 #define AR934X_NFC_ECC_CTRL_ERR_OVER BIT(2)
142 #define AR934X_NFC_ECC_CTRL_ERR_UNCORRECT BIT(1)
143 #define AR934X_NFC_ECC_CTRL_ERR_CORRECT BIT(0)
144
145 #define AR934X_NFC_ECC_OFFS_OFSET_M 0xffff
146
147 /* default timing values */
148 #define AR934X_NFC_TIME_SEQ_DEFAULT 0x7fff
149 #define AR934X_NFC_TIMINGS_ASYN_DEFAULT 0x22
150 #define AR934X_NFC_TIMINGS_SYN_DEFAULT 0xf
151
152 #define AR934X_NFC_ID_BUF_SIZE 8
153 #define AR934X_NFC_DEV_READY_TIMEOUT 25 /* msecs */
154 #define AR934X_NFC_DMA_READY_TIMEOUT 25 /* msecs */
155 #define AR934X_NFC_DONE_TIMEOUT 1000
156 #define AR934X_NFC_DMA_RETRIES 20
157
158 #define AR934X_NFC_USE_IRQ true
159 #define AR934X_NFC_IRQ_MASK AR934X_NFC_INT_DEV_RDY(0)
160
161 #define AR934X_NFC_GENSEQ_SMALL_PAGE_READ 0x30043
162
163 #undef AR934X_NFC_DEBUG_DATA
164 #undef AR934X_NFC_DEBUG
165
166 struct mtd_info;
167 struct mtd_partition;
168 struct ar934x_nfc;
169
170 struct ar934x_nfc {
171 struct nand_chip nand_chip;
172 struct device *parent;
173 void __iomem *base;
174 bool swap_dma;
175 int irq;
176 wait_queue_head_t irq_waitq;
177
178 bool spurious_irq_expected;
179 u32 irq_status;
180
181 u32 ctrl_reg;
182 u32 ecc_ctrl_reg;
183 u32 ecc_offset_reg;
184 u32 ecc_thres;
185 u32 ecc_oob_pos;
186
187 bool small_page;
188 unsigned int addr_count0;
189 unsigned int addr_count1;
190
191 u8 *buf;
192 dma_addr_t buf_dma;
193 unsigned int buf_size;
194 int buf_index;
195
196 bool read_id;
197
198 int erase1_page_addr;
199
200 int rndout_page_addr;
201 int rndout_read_cmd;
202
203 int seqin_page_addr;
204 int seqin_column;
205 int seqin_read_cmd;
206
207 struct reset_control *rst;
208 };
209
210 static inline __printf(2, 3)
211 void _nfc_dbg(struct ar934x_nfc *nfc, const char *fmt, ...)
212 {
213 }
214
215 #ifdef AR934X_NFC_DEBUG
216 #define nfc_dbg(_nfc, fmt, ...) \
217 dev_info((_nfc)->parent, fmt, ##__VA_ARGS__)
218 #else
219 #define nfc_dbg(_nfc, fmt, ...) \
220 _nfc_dbg((_nfc), fmt, ##__VA_ARGS__)
221 #endif /* AR934X_NFC_DEBUG */
222
223 #ifdef AR934X_NFC_DEBUG_DATA
224 static void nfc_debug_data(const char *label, void *data, int len)
225 {
226 print_hex_dump(KERN_WARNING, label, DUMP_PREFIX_OFFSET, 16, 1,
227 data, len, 0);
228 }
229 #else
230 static inline void nfc_debug_data(const char *label, void *data, int len) {}
231 #endif /* AR934X_NFC_DEBUG_DATA */
232
233 static void ar934x_nfc_restart(struct ar934x_nfc *nfc);
234
235 static inline bool is_all_ff(u8 *buf, int len)
236 {
237 while (len--)
238 if (buf[len] != 0xff)
239 return false;
240
241 return true;
242 }
243
244 static inline void ar934x_nfc_wr(struct ar934x_nfc *nfc, unsigned reg, u32 val)
245 {
246 __raw_writel(val, nfc->base + reg);
247 }
248
249 static inline u32 ar934x_nfc_rr(struct ar934x_nfc *nfc, unsigned reg)
250 {
251 return __raw_readl(nfc->base + reg);
252 }
253
254 static inline struct ar934x_nfc *mtd_to_ar934x_nfc(struct mtd_info *mtd)
255 {
256 struct nand_chip *chip = mtd_to_nand(mtd);
257
258 return container_of(chip, struct ar934x_nfc, nand_chip);
259 }
260
261 static struct mtd_info *ar934x_nfc_to_mtd(struct ar934x_nfc *nfc)
262 {
263 return nand_to_mtd(&nfc->nand_chip);
264 }
265
266 static inline bool ar934x_nfc_use_irq(struct ar934x_nfc *nfc)
267 {
268 return AR934X_NFC_USE_IRQ;
269 }
270
271 static inline void ar934x_nfc_write_cmd_reg(struct ar934x_nfc *nfc, u32 cmd_reg)
272 {
273 wmb();
274
275 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CMD, cmd_reg);
276 /* flush write */
277 ar934x_nfc_rr(nfc, AR934X_NFC_REG_CMD);
278 }
279
280 static bool __ar934x_nfc_dev_ready(struct ar934x_nfc *nfc)
281 {
282 u32 status;
283
284 status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_STATUS);
285 return (status & 0xff) == 0xff;
286 }
287
288 static inline bool __ar934x_nfc_is_dma_ready(struct ar934x_nfc *nfc)
289 {
290 u32 status;
291
292 status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_DMA_CTRL);
293 return (status & AR934X_NFC_DMA_CTRL_DMA_READY) != 0;
294 }
295
296 static int ar934x_nfc_wait_dev_ready(struct ar934x_nfc *nfc)
297 {
298 unsigned long timeout;
299
300 timeout = jiffies + msecs_to_jiffies(AR934X_NFC_DEV_READY_TIMEOUT);
301 do {
302 if (__ar934x_nfc_dev_ready(nfc))
303 return 0;
304 } while time_before(jiffies, timeout);
305
306 nfc_dbg(nfc, "timeout waiting for device ready, status:%08x int:%08x\n",
307 ar934x_nfc_rr(nfc, AR934X_NFC_REG_STATUS),
308 ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS));
309 return -ETIMEDOUT;
310 }
311
312 static int ar934x_nfc_wait_dma_ready(struct ar934x_nfc *nfc)
313 {
314 unsigned long timeout;
315
316 timeout = jiffies + msecs_to_jiffies(AR934X_NFC_DMA_READY_TIMEOUT);
317 do {
318 if (__ar934x_nfc_is_dma_ready(nfc))
319 return 0;
320 } while time_before(jiffies, timeout);
321
322 nfc_dbg(nfc, "timeout waiting for DMA ready, dma_ctrl:%08x\n",
323 ar934x_nfc_rr(nfc, AR934X_NFC_REG_DMA_CTRL));
324 return -ETIMEDOUT;
325 }
326
327 static int ar934x_nfc_wait_irq(struct ar934x_nfc *nfc)
328 {
329 long timeout;
330 int ret;
331
332 timeout = wait_event_timeout(nfc->irq_waitq,
333 (nfc->irq_status & AR934X_NFC_IRQ_MASK) != 0,
334 msecs_to_jiffies(AR934X_NFC_DEV_READY_TIMEOUT));
335
336 ret = 0;
337 if (!timeout) {
338 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_MASK, 0);
339 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
340 /* flush write */
341 ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS);
342
343 nfc_dbg(nfc,
344 "timeout waiting for interrupt, status:%08x\n",
345 nfc->irq_status);
346 ret = -ETIMEDOUT;
347 }
348
349 nfc->irq_status = 0;
350 return ret;
351 }
352
353 static int ar934x_nfc_wait_done(struct ar934x_nfc *nfc)
354 {
355 int ret;
356
357 if (ar934x_nfc_use_irq(nfc))
358 ret = ar934x_nfc_wait_irq(nfc);
359 else
360 ret = ar934x_nfc_wait_dev_ready(nfc);
361
362 if (ret)
363 return ret;
364
365 return ar934x_nfc_wait_dma_ready(nfc);
366 }
367
368 static int ar934x_nfc_alloc_buf(struct ar934x_nfc *nfc, unsigned size)
369 {
370 nfc->buf = dma_alloc_coherent(nfc->parent, size,
371 &nfc->buf_dma, GFP_KERNEL);
372 if (nfc->buf == NULL) {
373 dev_err(nfc->parent, "no memory for DMA buffer\n");
374 return -ENOMEM;
375 }
376
377 nfc->buf_size = size;
378 nfc_dbg(nfc, "buf:%p size:%u\n", nfc->buf, nfc->buf_size);
379
380 return 0;
381 }
382
383 static void ar934x_nfc_free_buf(struct ar934x_nfc *nfc)
384 {
385 dma_free_coherent(nfc->parent, nfc->buf_size, nfc->buf, nfc->buf_dma);
386 }
387
388 static void ar934x_nfc_get_addr(struct ar934x_nfc *nfc, int column,
389 int page_addr, u32 *addr0, u32 *addr1)
390 {
391 u32 a0, a1;
392
393 a0 = 0;
394 a1 = 0;
395
396 if (column == -1) {
397 /* ERASE1 */
398 a0 = (page_addr & 0xffff) << 16;
399 a1 = (page_addr >> 16) & 0xf;
400 } else if (page_addr != -1) {
401 /* SEQIN, READ0, etc.. */
402
403 /* TODO: handle 16bit bus width */
404 if (nfc->small_page) {
405 a0 = column & 0xff;
406 a0 |= (page_addr & 0xff) << 8;
407 a0 |= ((page_addr >> 8) & 0xff) << 16;
408 a0 |= ((page_addr >> 16) & 0xff) << 24;
409 } else {
410 a0 = column & 0x0FFF;
411 a0 |= (page_addr & 0xffff) << 16;
412
413 if (nfc->addr_count0 > 4)
414 a1 = (page_addr >> 16) & 0xf;
415 }
416 }
417
418 *addr0 = a0;
419 *addr1 = a1;
420 }
421
422 static void ar934x_nfc_send_cmd(struct ar934x_nfc *nfc, unsigned command)
423 {
424 u32 cmd_reg;
425
426 cmd_reg = AR934X_NFC_CMD_INPUT_SEL_SIU | AR934X_NFC_CMD_ADDR_SEL_0 |
427 AR934X_NFC_CMD_SEQ_1C;
428 cmd_reg |= (command & AR934X_NFC_CMD_CMD0_M) << AR934X_NFC_CMD_CMD0_S;
429
430 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
431 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
432
433 ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
434 ar934x_nfc_wait_dev_ready(nfc);
435 }
436
437 static int ar934x_nfc_do_rw_command(struct ar934x_nfc *nfc, int column,
438 int page_addr, int len, u32 cmd_reg,
439 u32 ctrl_reg, bool write)
440 {
441 u32 addr0, addr1;
442 u32 dma_ctrl;
443 int dir;
444 int err;
445 int retries = 0;
446
447 WARN_ON(len & 3);
448
449 if (WARN_ON(len > nfc->buf_size))
450 dev_err(nfc->parent, "len=%d > buf_size=%d", len,
451 nfc->buf_size);
452
453 if (write) {
454 dma_ctrl = AR934X_NFC_DMA_CTRL_DMA_DIR_WRITE;
455 dir = DMA_TO_DEVICE;
456 } else {
457 dma_ctrl = AR934X_NFC_DMA_CTRL_DMA_DIR_READ;
458 dir = DMA_FROM_DEVICE;
459 }
460
461 ar934x_nfc_get_addr(nfc, column, page_addr, &addr0, &addr1);
462
463 dma_ctrl |= AR934X_NFC_DMA_CTRL_DMA_START |
464 (AR934X_NFC_DMA_CTRL_DMA_BURST_3 <<
465 AR934X_NFC_DMA_CTRL_DMA_BURST_S);
466
467 cmd_reg |= AR934X_NFC_CMD_INPUT_SEL_DMA | AR934X_NFC_CMD_ADDR_SEL_0;
468 ctrl_reg |= AR934X_NFC_CTRL_INT_EN;
469
470 nfc_dbg(nfc, "%s a0:%08x a1:%08x len:%x cmd:%08x dma:%08x ctrl:%08x\n",
471 (write) ? "write" : "read",
472 addr0, addr1, len, cmd_reg, dma_ctrl, ctrl_reg);
473
474 retry:
475 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
476 ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_0, addr0);
477 ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_1, addr1);
478 ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_ADDR, nfc->buf_dma);
479 ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_COUNT, len);
480 ar934x_nfc_wr(nfc, AR934X_NFC_REG_DATA_SIZE, len);
481 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, ctrl_reg);
482 ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_CTRL, dma_ctrl);
483 ar934x_nfc_wr(nfc, AR934X_NFC_REG_ECC_CTRL, nfc->ecc_ctrl_reg);
484 ar934x_nfc_wr(nfc, AR934X_NFC_REG_ECC_OFFSET, nfc->ecc_offset_reg);
485
486 if (ar934x_nfc_use_irq(nfc)) {
487 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_MASK,
488 AR934X_NFC_IRQ_MASK);
489 /* flush write */
490 ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_MASK);
491 }
492
493 ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
494 err = ar934x_nfc_wait_done(nfc);
495 if (err) {
496 dev_dbg(nfc->parent, "%s operation stuck at page %d\n",
497 (write) ? "write" : "read", page_addr);
498
499 ar934x_nfc_restart(nfc);
500 if (retries++ < AR934X_NFC_DMA_RETRIES)
501 goto retry;
502
503 dev_err(nfc->parent, "%s operation failed on page %d\n",
504 (write) ? "write" : "read", page_addr);
505 }
506
507 return err;
508 }
509
510 static int ar934x_nfc_send_readid(struct ar934x_nfc *nfc, unsigned command)
511 {
512 u32 cmd_reg;
513 int err;
514
515 nfc_dbg(nfc, "readid, cmd:%02x\n", command);
516
517 cmd_reg = AR934X_NFC_CMD_SEQ_1C1AXR;
518 cmd_reg |= (command & AR934X_NFC_CMD_CMD0_M) << AR934X_NFC_CMD_CMD0_S;
519
520 err = ar934x_nfc_do_rw_command(nfc, -1, -1, AR934X_NFC_ID_BUF_SIZE,
521 cmd_reg, nfc->ctrl_reg, false);
522
523 nfc_debug_data("[id] ", nfc->buf, AR934X_NFC_ID_BUF_SIZE);
524
525 return err;
526 }
527
528 static int ar934x_nfc_send_read(struct ar934x_nfc *nfc, unsigned command,
529 int column, int page_addr, int len)
530 {
531 u32 cmd_reg;
532 int err;
533
534 nfc_dbg(nfc, "read, column=%d page=%d len=%d\n",
535 column, page_addr, len);
536
537 cmd_reg = (command & AR934X_NFC_CMD_CMD0_M) << AR934X_NFC_CMD_CMD0_S;
538
539 if (nfc->small_page) {
540 cmd_reg |= AR934X_NFC_CMD_SEQ_18;
541 } else {
542 cmd_reg |= NAND_CMD_READSTART << AR934X_NFC_CMD_CMD1_S;
543 cmd_reg |= AR934X_NFC_CMD_SEQ_1C5A1CXR;
544 }
545
546 err = ar934x_nfc_do_rw_command(nfc, column, page_addr, len,
547 cmd_reg, nfc->ctrl_reg, false);
548
549 nfc_debug_data("[data] ", nfc->buf, len);
550
551 return err;
552 }
553
554 static void ar934x_nfc_send_erase(struct ar934x_nfc *nfc, unsigned command,
555 int column, int page_addr)
556 {
557 u32 addr0, addr1;
558 u32 ctrl_reg;
559 u32 cmd_reg;
560
561 ar934x_nfc_get_addr(nfc, column, page_addr, &addr0, &addr1);
562
563 ctrl_reg = nfc->ctrl_reg;
564 if (nfc->small_page) {
565 /* override number of address cycles for the erase command */
566 ctrl_reg &= ~(AR934X_NFC_CTRL_ADDR_CYCLE0_M <<
567 AR934X_NFC_CTRL_ADDR_CYCLE0_S);
568 ctrl_reg &= ~(AR934X_NFC_CTRL_ADDR_CYCLE1_M <<
569 AR934X_NFC_CTRL_ADDR_CYCLE1_S);
570 ctrl_reg &= ~(AR934X_NFC_CTRL_SMALL_PAGE);
571 ctrl_reg |= (nfc->addr_count0 + 1) <<
572 AR934X_NFC_CTRL_ADDR_CYCLE0_S;
573 }
574
575 cmd_reg = NAND_CMD_ERASE1 << AR934X_NFC_CMD_CMD0_S;
576 cmd_reg |= command << AR934X_NFC_CMD_CMD1_S;
577 cmd_reg |= AR934X_NFC_CMD_SEQ_ERASE;
578
579 nfc_dbg(nfc, "erase page %d, a0:%08x a1:%08x cmd:%08x ctrl:%08x\n",
580 page_addr, addr0, addr1, cmd_reg, ctrl_reg);
581
582 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
583 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, ctrl_reg);
584 ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_0, addr0);
585 ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_1, addr1);
586
587 ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
588 ar934x_nfc_wait_dev_ready(nfc);
589 }
590
591 static int ar934x_nfc_send_write(struct ar934x_nfc *nfc, unsigned command,
592 int column, int page_addr, int len)
593 {
594 u32 cmd_reg;
595
596 nfc_dbg(nfc, "write, column=%d page=%d len=%d\n",
597 column, page_addr, len);
598
599 nfc_debug_data("[data] ", nfc->buf, len);
600
601 cmd_reg = NAND_CMD_SEQIN << AR934X_NFC_CMD_CMD0_S;
602 cmd_reg |= command << AR934X_NFC_CMD_CMD1_S;
603 cmd_reg |= AR934X_NFC_CMD_SEQ_12;
604
605 return ar934x_nfc_do_rw_command(nfc, column, page_addr, len,
606 cmd_reg, nfc->ctrl_reg, true);
607 }
608
609 static void ar934x_nfc_read_status(struct ar934x_nfc *nfc)
610 {
611 u32 cmd_reg;
612 u32 status;
613
614 cmd_reg = NAND_CMD_STATUS << AR934X_NFC_CMD_CMD0_S;
615 cmd_reg |= AR934X_NFC_CMD_SEQ_S;
616
617 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
618 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
619
620 ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
621 ar934x_nfc_wait_dev_ready(nfc);
622
623 status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_READ_STATUS);
624
625 nfc_dbg(nfc, "read status, cmd:%08x status:%02x\n",
626 cmd_reg, (status & 0xff));
627
628 if (nfc->swap_dma)
629 nfc->buf[0 ^ 3] = status;
630 else
631 nfc->buf[0] = status;
632 }
633
634 static void ar934x_nfc_cmdfunc(struct mtd_info *mtd, unsigned int command,
635 int column, int page_addr)
636 {
637 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
638 struct nand_chip *nand = &nfc->nand_chip;
639
640 nfc->read_id = false;
641 if (command != NAND_CMD_PAGEPROG)
642 nfc->buf_index = 0;
643
644 switch (command) {
645 case NAND_CMD_RESET:
646 ar934x_nfc_send_cmd(nfc, command);
647 break;
648
649 case NAND_CMD_READID:
650 nfc->read_id = true;
651 ar934x_nfc_send_readid(nfc, command);
652 break;
653
654 case NAND_CMD_READ0:
655 case NAND_CMD_READ1:
656 if (nfc->small_page) {
657 ar934x_nfc_send_read(nfc, command, column, page_addr,
658 mtd->writesize + mtd->oobsize);
659 } else {
660 ar934x_nfc_send_read(nfc, command, 0, page_addr,
661 mtd->writesize + mtd->oobsize);
662 nfc->buf_index = column;
663 nfc->rndout_page_addr = page_addr;
664 nfc->rndout_read_cmd = command;
665 }
666 break;
667
668 case NAND_CMD_READOOB:
669 if (nfc->small_page)
670 ar934x_nfc_send_read(nfc, NAND_CMD_READOOB,
671 column, page_addr,
672 mtd->oobsize);
673 else
674 ar934x_nfc_send_read(nfc, NAND_CMD_READ0,
675 mtd->writesize, page_addr,
676 mtd->oobsize);
677 break;
678
679 case NAND_CMD_RNDOUT:
680 if (WARN_ON(nfc->small_page))
681 break;
682
683 /* emulate subpage read */
684 ar934x_nfc_send_read(nfc, nfc->rndout_read_cmd, 0,
685 nfc->rndout_page_addr,
686 mtd->writesize + mtd->oobsize);
687 nfc->buf_index = column;
688 break;
689
690 case NAND_CMD_ERASE1:
691 nfc->erase1_page_addr = page_addr;
692 break;
693
694 case NAND_CMD_ERASE2:
695 ar934x_nfc_send_erase(nfc, command, -1, nfc->erase1_page_addr);
696 break;
697
698 case NAND_CMD_STATUS:
699 ar934x_nfc_read_status(nfc);
700 break;
701
702 case NAND_CMD_SEQIN:
703 if (nfc->small_page) {
704 /* output read command */
705 if (column >= mtd->writesize) {
706 column -= mtd->writesize;
707 nfc->seqin_read_cmd = NAND_CMD_READOOB;
708 } else if (column < 256) {
709 nfc->seqin_read_cmd = NAND_CMD_READ0;
710 } else {
711 column -= 256;
712 nfc->seqin_read_cmd = NAND_CMD_READ1;
713 }
714 } else {
715 nfc->seqin_read_cmd = NAND_CMD_READ0;
716 }
717 nfc->seqin_column = column;
718 nfc->seqin_page_addr = page_addr;
719 break;
720
721 case NAND_CMD_PAGEPROG:
722 if (nand->ecc.mode == NAND_ECC_HW) {
723 /* the data is already written */
724 break;
725 }
726
727 if (nfc->small_page)
728 ar934x_nfc_send_cmd(nfc, nfc->seqin_read_cmd);
729
730 ar934x_nfc_send_write(nfc, command, nfc->seqin_column,
731 nfc->seqin_page_addr,
732 nfc->buf_index);
733 break;
734
735 default:
736 dev_err(nfc->parent,
737 "unsupported command: %x, column:%d page_addr=%d\n",
738 command, column, page_addr);
739 break;
740 }
741 }
742
743 static int ar934x_nfc_dev_ready(struct mtd_info *mtd)
744 {
745 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
746
747 return __ar934x_nfc_dev_ready(nfc);
748 }
749
750 static u8 ar934x_nfc_read_byte(struct mtd_info *mtd)
751 {
752 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
753 u8 data;
754
755 WARN_ON(nfc->buf_index >= nfc->buf_size);
756
757 if (nfc->swap_dma || nfc->read_id)
758 data = nfc->buf[nfc->buf_index ^ 3];
759 else
760 data = nfc->buf[nfc->buf_index];
761
762 nfc->buf_index++;
763
764 return data;
765 }
766
767 static void ar934x_nfc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
768 {
769 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
770 int i;
771
772 WARN_ON(nfc->buf_index + len > nfc->buf_size);
773
774 if (nfc->swap_dma) {
775 for (i = 0; i < len; i++) {
776 nfc->buf[nfc->buf_index ^ 3] = buf[i];
777 nfc->buf_index++;
778 }
779 } else {
780 for (i = 0; i < len; i++) {
781 nfc->buf[nfc->buf_index] = buf[i];
782 nfc->buf_index++;
783 }
784 }
785 }
786
787 static void ar934x_nfc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
788 {
789 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
790 int buf_index;
791 int i;
792
793 WARN_ON(nfc->buf_index + len > nfc->buf_size);
794
795 buf_index = nfc->buf_index;
796
797 if (nfc->swap_dma || nfc->read_id) {
798 for (i = 0; i < len; i++) {
799 buf[i] = nfc->buf[buf_index ^ 3];
800 buf_index++;
801 }
802 } else {
803 for (i = 0; i < len; i++) {
804 buf[i] = nfc->buf[buf_index];
805 buf_index++;
806 }
807 }
808
809 nfc->buf_index = buf_index;
810 }
811
812 static inline void ar934x_nfc_enable_hwecc(struct ar934x_nfc *nfc)
813 {
814 nfc->ctrl_reg |= AR934X_NFC_CTRL_ECC_EN;
815 nfc->ctrl_reg &= ~AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
816 }
817
818 static inline void ar934x_nfc_disable_hwecc(struct ar934x_nfc *nfc)
819 {
820 nfc->ctrl_reg &= ~AR934X_NFC_CTRL_ECC_EN;
821 nfc->ctrl_reg |= AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
822 }
823
824 static int ar934x_nfc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
825 int page)
826 {
827 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
828 int err;
829
830 nfc_dbg(nfc, "read_oob: page:%d\n", page);
831
832 err = ar934x_nfc_send_read(nfc, NAND_CMD_READ0, mtd->writesize, page,
833 mtd->oobsize);
834 if (err)
835 return err;
836
837 memcpy(chip->oob_poi, nfc->buf, mtd->oobsize);
838
839 return 0;
840 }
841
842 static int ar934x_nfc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
843 int page)
844 {
845 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
846
847 nfc_dbg(nfc, "write_oob: page:%d\n", page);
848
849 memcpy(nfc->buf, chip->oob_poi, mtd->oobsize);
850
851 return ar934x_nfc_send_write(nfc, NAND_CMD_PAGEPROG, mtd->writesize,
852 page, mtd->oobsize);
853 }
854
855 static int ar934x_nfc_read_page_raw(struct mtd_info *mtd,
856 struct nand_chip *chip, u8 *buf,
857 int oob_required, int page)
858 {
859 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
860 int len;
861 int err;
862
863 nfc_dbg(nfc, "read_page_raw: page:%d oob:%d\n", page, oob_required);
864
865 len = mtd->writesize;
866 if (oob_required)
867 len += mtd->oobsize;
868
869 err = ar934x_nfc_send_read(nfc, NAND_CMD_READ0, 0, page, len);
870 if (err)
871 return err;
872
873 memcpy(buf, nfc->buf, mtd->writesize);
874
875 if (oob_required)
876 memcpy(chip->oob_poi, &nfc->buf[mtd->writesize], mtd->oobsize);
877
878 return 0;
879 }
880
881 static int ar934x_nfc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
882 u8 *buf, int oob_required, int page)
883 {
884 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
885 u32 ecc_ctrl;
886 int max_bitflips = 0;
887 bool ecc_failed;
888 bool ecc_corrected;
889 int err;
890
891 nfc_dbg(nfc, "read_page: page:%d oob:%d\n", page, oob_required);
892
893 ar934x_nfc_enable_hwecc(nfc);
894 err = ar934x_nfc_send_read(nfc, NAND_CMD_READ0, 0, page,
895 mtd->writesize);
896 ar934x_nfc_disable_hwecc(nfc);
897
898 if (err)
899 return err;
900
901 /* TODO: optimize to avoid memcpy */
902 memcpy(buf, nfc->buf, mtd->writesize);
903
904 /* read the ECC status */
905 ecc_ctrl = ar934x_nfc_rr(nfc, AR934X_NFC_REG_ECC_CTRL);
906 ecc_failed = ecc_ctrl & AR934X_NFC_ECC_CTRL_ERR_UNCORRECT;
907 ecc_corrected = ecc_ctrl & AR934X_NFC_ECC_CTRL_ERR_CORRECT;
908
909 if (oob_required || ecc_failed) {
910 err = ar934x_nfc_send_read(nfc, NAND_CMD_READ0, mtd->writesize,
911 page, mtd->oobsize);
912 if (err)
913 return err;
914
915 if (oob_required)
916 memcpy(chip->oob_poi, nfc->buf, mtd->oobsize);
917 }
918
919 if (ecc_failed) {
920 /*
921 * The hardware ECC engine reports uncorrectable errors
922 * on empty pages. Check the ECC bytes and the data. If
923 * both contains 0xff bytes only, dont report a failure.
924 *
925 * TODO: prebuild a buffer with 0xff bytes and use memcmp
926 * for better performance?
927 */
928 if (!is_all_ff(&nfc->buf[nfc->ecc_oob_pos], chip->ecc.total) ||
929 !is_all_ff(buf, mtd->writesize))
930 mtd->ecc_stats.failed++;
931 } else if (ecc_corrected) {
932 /*
933 * The hardware does not report the exact count of the
934 * corrected bitflips, use assumptions based on the
935 * threshold.
936 */
937 if (ecc_ctrl & AR934X_NFC_ECC_CTRL_ERR_OVER) {
938 /*
939 * The number of corrected bitflips exceeds the
940 * threshold. Assume the maximum.
941 */
942 max_bitflips = chip->ecc.strength * chip->ecc.steps;
943 } else {
944 max_bitflips = nfc->ecc_thres * chip->ecc.steps;
945 }
946
947 mtd->ecc_stats.corrected += max_bitflips;
948 }
949
950 return max_bitflips;
951 }
952
953 static int ar934x_nfc_write_page_raw(struct mtd_info *mtd,
954 struct nand_chip *chip, const u8 *buf,
955 int oob_required, int page)
956 {
957 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
958 int len;
959
960 nfc_dbg(nfc, "write_page_raw: page:%d oob:%d\n", page, oob_required);
961
962 memcpy(nfc->buf, buf, mtd->writesize);
963 len = mtd->writesize;
964
965 if (oob_required) {
966 memcpy(&nfc->buf[mtd->writesize], chip->oob_poi, mtd->oobsize);
967 len += mtd->oobsize;
968 }
969
970 return ar934x_nfc_send_write(nfc, NAND_CMD_PAGEPROG, 0, page, len);
971 }
972
973 static int ar934x_nfc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
974 const u8 *buf, int oob_required, int page)
975 {
976 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
977 int err;
978
979 nfc_dbg(nfc, "write_page: page:%d oob:%d\n", page, oob_required);
980
981 /* write OOB first */
982 if (oob_required &&
983 !is_all_ff(chip->oob_poi, mtd->oobsize)) {
984 err = ar934x_nfc_write_oob(mtd, chip, page);
985 if (err)
986 return err;
987 }
988
989 /* TODO: optimize to avoid memcopy */
990 memcpy(nfc->buf, buf, mtd->writesize);
991
992 ar934x_nfc_enable_hwecc(nfc);
993 err = ar934x_nfc_send_write(nfc, NAND_CMD_PAGEPROG, 0, page,
994 mtd->writesize);
995 ar934x_nfc_disable_hwecc(nfc);
996
997 return err;
998 }
999
1000 static int ar934x_nfc_hw_reset_assert(struct ar934x_nfc *nfc) {
1001 reset_control_assert(nfc->rst);
1002 udelay(250);
1003 }
1004
1005 static int ar934x_nfc_hw_reset_deassert(struct ar934x_nfc *nfc) {
1006 reset_control_deassert(nfc->rst);
1007 udelay(250);
1008 }
1009
1010 static int ar934x_nfc_hw_init(struct ar934x_nfc *nfc)
1011 {
1012 ar934x_nfc_hw_reset_assert(nfc);
1013 ar934x_nfc_hw_reset_deassert(nfc);
1014 /*
1015 * setup timings
1016 * TODO: make it configurable via platform data or DT
1017 */
1018 ar934x_nfc_wr(nfc, AR934X_NFC_REG_TIME_SEQ,
1019 AR934X_NFC_TIME_SEQ_DEFAULT);
1020 ar934x_nfc_wr(nfc, AR934X_NFC_REG_TIMINGS_ASYN,
1021 AR934X_NFC_TIMINGS_ASYN_DEFAULT);
1022 ar934x_nfc_wr(nfc, AR934X_NFC_REG_TIMINGS_SYN,
1023 AR934X_NFC_TIMINGS_SYN_DEFAULT);
1024
1025 /* disable WP on all chips, and select chip 0 */
1026 ar934x_nfc_wr(nfc, AR934X_NFC_REG_MEM_CTRL, 0xff00);
1027
1028 ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_ADDR_OFFS, 0);
1029
1030 /* initialize Control register */
1031 nfc->ctrl_reg = AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
1032 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
1033
1034 if (nfc->small_page) {
1035 /* Setup generic sequence register for small page reads. */
1036 ar934x_nfc_wr(nfc, AR934X_NFC_REG_GEN_SEQ_CTRL,
1037 AR934X_NFC_GENSEQ_SMALL_PAGE_READ);
1038 }
1039
1040 return 0;
1041 }
1042
1043 static void ar934x_nfc_restart(struct ar934x_nfc *nfc)
1044 {
1045 u32 ctrl_reg;
1046
1047 ctrl_reg = nfc->ctrl_reg;
1048 ar934x_nfc_hw_init(nfc);
1049 nfc->ctrl_reg = ctrl_reg;
1050
1051 ar934x_nfc_send_cmd(nfc, NAND_CMD_RESET);
1052 }
1053
1054 static irqreturn_t ar934x_nfc_irq_handler(int irq, void *data)
1055 {
1056 struct ar934x_nfc *nfc = data;
1057 u32 status;
1058
1059 status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS);
1060
1061 ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
1062 /* flush write */
1063 ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS);
1064
1065 status &= ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_MASK);
1066 if (status) {
1067 nfc_dbg(nfc, "got IRQ, status:%08x\n", status);
1068
1069 nfc->irq_status = status;
1070 nfc->spurious_irq_expected = true;
1071 wake_up(&nfc->irq_waitq);
1072 } else {
1073 if (nfc->spurious_irq_expected)
1074 nfc->spurious_irq_expected = false;
1075 else
1076 dev_warn(nfc->parent, "spurious interrupt\n");
1077 }
1078
1079 return IRQ_HANDLED;
1080 }
1081
1082 static int ar934x_nfc_init_tail(struct mtd_info *mtd)
1083 {
1084 struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
1085 struct nand_chip *chip = &nfc->nand_chip;
1086 u32 ctrl;
1087 u32 t;
1088 int err;
1089
1090 switch (mtd->oobsize) {
1091 case 16:
1092 case 64:
1093 case 128:
1094 ar934x_nfc_wr(nfc, AR934X_NFC_REG_SPARE_SIZE, mtd->oobsize);
1095 break;
1096
1097 default:
1098 dev_err(nfc->parent, "unsupported OOB size: %d bytes\n",
1099 mtd->oobsize);
1100 return -ENXIO;
1101 }
1102
1103 ctrl = AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
1104
1105 switch (mtd->erasesize / mtd->writesize) {
1106 case 32:
1107 t = AR934X_NFC_CTRL_BLOCK_SIZE_32;
1108 break;
1109
1110 case 64:
1111 t = AR934X_NFC_CTRL_BLOCK_SIZE_64;
1112 break;
1113
1114 case 128:
1115 t = AR934X_NFC_CTRL_BLOCK_SIZE_128;
1116 break;
1117
1118 case 256:
1119 t = AR934X_NFC_CTRL_BLOCK_SIZE_256;
1120 break;
1121
1122 default:
1123 dev_err(nfc->parent, "unsupported block size: %u\n",
1124 mtd->erasesize / mtd->writesize);
1125 return -ENXIO;
1126 }
1127
1128 ctrl |= t << AR934X_NFC_CTRL_BLOCK_SIZE_S;
1129
1130 switch (mtd->writesize) {
1131 case 256:
1132 nfc->small_page = 1;
1133 t = AR934X_NFC_CTRL_PAGE_SIZE_256;
1134 break;
1135
1136 case 512:
1137 nfc->small_page = 1;
1138 t = AR934X_NFC_CTRL_PAGE_SIZE_512;
1139 break;
1140
1141 case 1024:
1142 t = AR934X_NFC_CTRL_PAGE_SIZE_1024;
1143 break;
1144
1145 case 2048:
1146 t = AR934X_NFC_CTRL_PAGE_SIZE_2048;
1147 break;
1148
1149 case 4096:
1150 t = AR934X_NFC_CTRL_PAGE_SIZE_4096;
1151 break;
1152
1153 case 8192:
1154 t = AR934X_NFC_CTRL_PAGE_SIZE_8192;
1155 break;
1156
1157 case 16384:
1158 t = AR934X_NFC_CTRL_PAGE_SIZE_16384;
1159 break;
1160
1161 default:
1162 dev_err(nfc->parent, "unsupported write size: %d bytes\n",
1163 mtd->writesize);
1164 return -ENXIO;
1165 }
1166
1167 ctrl |= t << AR934X_NFC_CTRL_PAGE_SIZE_S;
1168
1169 if (nfc->small_page) {
1170 ctrl |= AR934X_NFC_CTRL_SMALL_PAGE;
1171
1172 if (chip->chipsize > (32 << 20)) {
1173 nfc->addr_count0 = 4;
1174 nfc->addr_count1 = 3;
1175 } else if (chip->chipsize > (2 << 16)) {
1176 nfc->addr_count0 = 3;
1177 nfc->addr_count1 = 2;
1178 } else {
1179 nfc->addr_count0 = 2;
1180 nfc->addr_count1 = 1;
1181 }
1182 } else {
1183 if (chip->chipsize > (128 << 20)) {
1184 nfc->addr_count0 = 5;
1185 nfc->addr_count1 = 3;
1186 } else if (chip->chipsize > (8 << 16)) {
1187 nfc->addr_count0 = 4;
1188 nfc->addr_count1 = 2;
1189 } else {
1190 nfc->addr_count0 = 3;
1191 nfc->addr_count1 = 1;
1192 }
1193 }
1194
1195 ctrl |= nfc->addr_count0 << AR934X_NFC_CTRL_ADDR_CYCLE0_S;
1196 ctrl |= nfc->addr_count1 << AR934X_NFC_CTRL_ADDR_CYCLE1_S;
1197
1198 nfc->ctrl_reg = ctrl;
1199 ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
1200
1201 ar934x_nfc_free_buf(nfc);
1202 err = ar934x_nfc_alloc_buf(nfc, mtd->writesize + mtd->oobsize);
1203
1204 return err;
1205 }
1206
1207 static int ar934x_nfc_ooblayout_ecc(struct mtd_info *mtd, int section,
1208 struct mtd_oob_region *oobregion)
1209 {
1210 if (section)
1211 return -ERANGE;
1212
1213 oobregion->offset = 20;
1214 oobregion->length = 28;
1215
1216 return 0;
1217 }
1218
1219 static int ar934x_nfc_ooblayout_free(struct mtd_info *mtd, int section,
1220 struct mtd_oob_region *oobregion)
1221 {
1222 switch (section) {
1223 case 0:
1224 oobregion->offset = 4;
1225 oobregion->length = 16;
1226 return 0;
1227 case 1:
1228 oobregion->offset = 48;
1229 oobregion->length = 16;
1230 return 0;
1231 default:
1232 return -ERANGE;
1233 }
1234 }
1235
1236 static const struct mtd_ooblayout_ops ar934x_nfc_ecclayout_ops = {
1237 .ecc = ar934x_nfc_ooblayout_ecc,
1238 .free = ar934x_nfc_ooblayout_free,
1239 };
1240
1241 static int ar934x_nfc_setup_hwecc(struct ar934x_nfc *nfc)
1242 {
1243 struct nand_chip *nand = &nfc->nand_chip;
1244 struct mtd_info *mtd = nand_to_mtd(nand);
1245 u32 ecc_cap;
1246 u32 ecc_thres;
1247 struct mtd_oob_region oobregion;
1248
1249 switch (mtd->writesize) {
1250 case 2048:
1251 /*
1252 * Writing a subpage separately is not supported, because
1253 * the controller only does ECC on full-page accesses.
1254 */
1255 nand->options = NAND_NO_SUBPAGE_WRITE;
1256
1257 nand->ecc.size = 512;
1258 nand->ecc.bytes = 7;
1259 nand->ecc.strength = 4;
1260 mtd_set_ooblayout(mtd, &ar934x_nfc_ecclayout_ops);
1261 break;
1262
1263 default:
1264 dev_err(nfc->parent,
1265 "hardware ECC is not available for %d byte pages\n",
1266 mtd->writesize);
1267 return -EINVAL;
1268 }
1269
1270 BUG_ON(!mtd->ooblayout->ecc);
1271
1272 switch (nand->ecc.strength) {
1273 case 4:
1274 ecc_cap = AR934X_NFC_ECC_CTRL_ECC_CAP_4;
1275 ecc_thres = 4;
1276 break;
1277
1278 default:
1279 dev_err(nfc->parent, "unsupported ECC strength %u\n",
1280 nand->ecc.strength);
1281 return -EINVAL;
1282 }
1283
1284 nfc->ecc_thres = ecc_thres;
1285 mtd->ooblayout->ecc(mtd, 0, &oobregion);
1286 nfc->ecc_oob_pos = oobregion.offset;
1287
1288 nfc->ecc_ctrl_reg = ecc_cap << AR934X_NFC_ECC_CTRL_ECC_CAP_S;
1289 nfc->ecc_ctrl_reg |= ecc_thres << AR934X_NFC_ECC_CTRL_ERR_THRES_S;
1290
1291 nfc->ecc_offset_reg = mtd->writesize + nfc->ecc_oob_pos;
1292
1293 nand->ecc.read_page = ar934x_nfc_read_page;
1294 nand->ecc.read_page_raw = ar934x_nfc_read_page_raw;
1295 nand->ecc.write_page = ar934x_nfc_write_page;
1296 nand->ecc.write_page_raw = ar934x_nfc_write_page_raw;
1297 nand->ecc.read_oob = ar934x_nfc_read_oob;
1298 nand->ecc.write_oob = ar934x_nfc_write_oob;
1299
1300 return 0;
1301 }
1302
1303 static int ar934x_nfc_attach_chip(struct nand_chip *nand)
1304 {
1305 struct mtd_info *mtd = nand_to_mtd(nand);
1306 struct ar934x_nfc *nfc = nand_get_controller_data(nand);
1307 struct device *dev = mtd->dev.parent;
1308 int ret;
1309
1310 ret = ar934x_nfc_init_tail(mtd);
1311 if (ret)
1312 return ret;
1313
1314 if (nand->ecc.mode == NAND_ECC_HW) {
1315 ret = ar934x_nfc_setup_hwecc(nfc);
1316 if (ret)
1317 return ret;
1318 } else if (nand->ecc.mode != NAND_ECC_SOFT) {
1319 dev_err(dev, "unknown ECC mode %d\n", nand->ecc.mode);
1320 return -EINVAL;
1321 } else if ((nand->ecc.algo != NAND_ECC_BCH) &&
1322 (nand->ecc.algo != NAND_ECC_HAMMING)) {
1323 dev_err(dev, "unknown software ECC algo %d\n", nand->ecc.algo);
1324 return -EINVAL;
1325 }
1326
1327 return 0;
1328 }
1329
1330 static u64 ar934x_nfc_dma_mask = DMA_BIT_MASK(32);
1331
1332 static void ar934x_nfc_cmd_ctrl(struct mtd_info *mtd, int dat,
1333 unsigned int ctrl)
1334 {
1335 WARN_ON(dat != NAND_CMD_NONE);
1336 }
1337
1338 static const struct nand_controller_ops ar934x_nfc_controller_ops = {
1339 .attach_chip = ar934x_nfc_attach_chip,
1340 };
1341
1342 static int ar934x_nfc_probe(struct platform_device *pdev)
1343 {
1344 struct ar934x_nfc *nfc;
1345 struct resource *res;
1346 struct mtd_info *mtd;
1347 struct nand_chip *nand;
1348 int ret;
1349
1350 pdev->dev.dma_mask = &ar934x_nfc_dma_mask;
1351 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1352
1353 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1354 if (!res) {
1355 dev_err(&pdev->dev, "failed to get I/O memory\n");
1356 return -EINVAL;
1357 }
1358
1359 nfc = devm_kzalloc(&pdev->dev, sizeof(struct ar934x_nfc), GFP_KERNEL);
1360 if (!nfc) {
1361 dev_err(&pdev->dev, "failed to allocate driver data\n");
1362 return -ENOMEM;
1363 }
1364
1365 nfc->base = devm_ioremap_resource(&pdev->dev, res);
1366 if (IS_ERR(nfc->base)) {
1367 dev_err(&pdev->dev, "failed to remap I/O memory\n");
1368 return PTR_ERR(nfc->base);
1369 }
1370
1371 nfc->irq = platform_get_irq(pdev, 0);
1372 if (nfc->irq < 0) {
1373 dev_err(&pdev->dev, "no IRQ resource specified\n");
1374 return -EINVAL;
1375 }
1376
1377 init_waitqueue_head(&nfc->irq_waitq);
1378 ret = devm_request_irq(&pdev->dev, nfc->irq, ar934x_nfc_irq_handler,
1379 0, AR934X_NFC_DRIVER_NAME, nfc);
1380 if (ret) {
1381 dev_err(&pdev->dev, "devm_request_irq failed, err:%d\n", ret);
1382 return ret;
1383 }
1384
1385 nfc->rst = devm_reset_control_get(&pdev->dev, "nand");
1386 if (IS_ERR(nfc->rst)) {
1387 dev_err(&pdev->dev, "Failed to get reset\n");
1388 return PTR_ERR(nfc->rst);
1389 }
1390
1391 nfc->parent = &pdev->dev;
1392 nfc->swap_dma = of_property_read_bool(pdev->dev.of_node,
1393 "qca,nand-swap-dma");
1394
1395 nand = &nfc->nand_chip;
1396 mtd = nand_to_mtd(nand);
1397
1398 mtd->owner = THIS_MODULE;
1399 mtd->dev.parent = &pdev->dev;
1400 mtd->name = AR934X_NFC_DRIVER_NAME;
1401
1402 nand_set_controller_data(nand, nfc);
1403 nand_set_flash_node(nand, pdev->dev.of_node);
1404 nand->chip_delay = 25;
1405 nand->dev_ready = ar934x_nfc_dev_ready;
1406 nand->cmdfunc = ar934x_nfc_cmdfunc;
1407 nand->cmd_ctrl = ar934x_nfc_cmd_ctrl; /* dummy */
1408 nand->read_byte = ar934x_nfc_read_byte;
1409 nand->write_buf = ar934x_nfc_write_buf;
1410 nand->read_buf = ar934x_nfc_read_buf;
1411 nand->ecc.mode = NAND_ECC_HW; /* default */
1412 platform_set_drvdata(pdev, nfc);
1413
1414 ret = ar934x_nfc_alloc_buf(nfc, AR934X_NFC_ID_BUF_SIZE);
1415 if (ret)
1416 return ret;
1417
1418 ret = ar934x_nfc_hw_init(nfc);
1419 if (ret) {
1420 dev_err(&pdev->dev, "hardware init failed, err:%d\n", ret);
1421 goto err_free_buf;
1422 }
1423
1424 nand->dummy_controller.ops = &ar934x_nfc_controller_ops;
1425 ret = nand_scan(mtd, 1);
1426 if (ret) {
1427 dev_err(&pdev->dev, "nand_scan failed, err:%d\n", ret);
1428 goto err_free_buf;
1429 }
1430
1431 ret = mtd_device_register(mtd, NULL, 0);
1432 if (ret) {
1433 dev_err(&pdev->dev, "unable to register mtd, err:%d\n", ret);
1434 goto err_free_buf;
1435 }
1436
1437 return 0;
1438
1439 err_free_buf:
1440 ar934x_nfc_free_buf(nfc);
1441 return ret;
1442 }
1443
1444 static int ar934x_nfc_remove(struct platform_device *pdev)
1445 {
1446 struct ar934x_nfc *nfc;
1447 struct mtd_info *mtd;
1448
1449 nfc = platform_get_drvdata(pdev);
1450 if (nfc) {
1451 mtd = ar934x_nfc_to_mtd(nfc);
1452 nand_release(mtd);
1453 ar934x_nfc_free_buf(nfc);
1454 }
1455
1456 return 0;
1457 }
1458
1459 static const struct of_device_id ar934x_nfc_match[] = {
1460 { .compatible = "qca," AR934X_NFC_DRIVER_NAME },
1461 {},
1462 };
1463
1464 MODULE_DEVICE_TABLE(of, ar934x_nfc_match);
1465
1466 static struct platform_driver ar934x_nfc_driver = {
1467 .probe = ar934x_nfc_probe,
1468 .remove = ar934x_nfc_remove,
1469 .driver = {
1470 .name = AR934X_NFC_DRIVER_NAME,
1471 .owner = THIS_MODULE,
1472 .of_match_table = ar934x_nfc_match,
1473 },
1474 };
1475
1476 module_platform_driver(ar934x_nfc_driver);
1477
1478 MODULE_LICENSE("GPL v2");
1479 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1480 MODULE_DESCRIPTION("Atheros AR934x NAND Flash Controller driver");
1481 MODULE_ALIAS("platform:" AR934X_NFC_DRIVER_NAME);