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