blob: 663e3314bb83d89937fcb7c5f49a280dad6e3994 [file] [log] [blame]
Boris BREZILLON1fef62c2014-10-21 15:08:41 +02001/*
2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon.dev@gmail.com>
3 *
4 * Derived from:
5 * https://github.com/yuq/sunxi-nfc-mtd
6 * Copyright (C) 2013 Qiang Yu <yuq825@gmail.com>
7 *
8 * https://github.com/hno/Allwinner-Info
9 * Copyright (C) 2013 Henrik Nordström <Henrik Nordström>
10 *
11 * Copyright (C) 2013 Dmitriy B. <rzk333@gmail.com>
12 * Copyright (C) 2013 Sergey Lapin <slapin@ossfans.org>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 */
24
25#include <linux/dma-mapping.h>
26#include <linux/slab.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/platform_device.h>
30#include <linux/of.h>
31#include <linux/of_device.h>
32#include <linux/of_gpio.h>
33#include <linux/of_mtd.h>
34#include <linux/mtd/mtd.h>
35#include <linux/mtd/nand.h>
36#include <linux/mtd/partitions.h>
37#include <linux/clk.h>
38#include <linux/delay.h>
39#include <linux/dmaengine.h>
40#include <linux/gpio.h>
41#include <linux/interrupt.h>
42#include <linux/io.h>
43
44#define NFC_REG_CTL 0x0000
45#define NFC_REG_ST 0x0004
46#define NFC_REG_INT 0x0008
47#define NFC_REG_TIMING_CTL 0x000C
48#define NFC_REG_TIMING_CFG 0x0010
49#define NFC_REG_ADDR_LOW 0x0014
50#define NFC_REG_ADDR_HIGH 0x0018
51#define NFC_REG_SECTOR_NUM 0x001C
52#define NFC_REG_CNT 0x0020
53#define NFC_REG_CMD 0x0024
54#define NFC_REG_RCMD_SET 0x0028
55#define NFC_REG_WCMD_SET 0x002C
56#define NFC_REG_IO_DATA 0x0030
57#define NFC_REG_ECC_CTL 0x0034
58#define NFC_REG_ECC_ST 0x0038
59#define NFC_REG_DEBUG 0x003C
60#define NFC_REG_ECC_CNT0 0x0040
61#define NFC_REG_ECC_CNT1 0x0044
62#define NFC_REG_ECC_CNT2 0x0048
63#define NFC_REG_ECC_CNT3 0x004c
64#define NFC_REG_USER_DATA_BASE 0x0050
65#define NFC_REG_SPARE_AREA 0x00A0
66#define NFC_RAM0_BASE 0x0400
67#define NFC_RAM1_BASE 0x0800
68
69/* define bit use in NFC_CTL */
70#define NFC_EN BIT(0)
71#define NFC_RESET BIT(1)
72#define NFC_BUS_WIDYH BIT(2)
73#define NFC_RB_SEL BIT(3)
74#define NFC_CE_SEL GENMASK(26, 24)
75#define NFC_CE_CTL BIT(6)
76#define NFC_CE_CTL1 BIT(7)
77#define NFC_PAGE_SIZE GENMASK(11, 8)
78#define NFC_SAM BIT(12)
79#define NFC_RAM_METHOD BIT(14)
80#define NFC_DEBUG_CTL BIT(31)
81
82/* define bit use in NFC_ST */
83#define NFC_RB_B2R BIT(0)
84#define NFC_CMD_INT_FLAG BIT(1)
85#define NFC_DMA_INT_FLAG BIT(2)
86#define NFC_CMD_FIFO_STATUS BIT(3)
87#define NFC_STA BIT(4)
88#define NFC_NATCH_INT_FLAG BIT(5)
89#define NFC_RB_STATE0 BIT(8)
90#define NFC_RB_STATE1 BIT(9)
91#define NFC_RB_STATE2 BIT(10)
92#define NFC_RB_STATE3 BIT(11)
93
94/* define bit use in NFC_INT */
95#define NFC_B2R_INT_ENABLE BIT(0)
96#define NFC_CMD_INT_ENABLE BIT(1)
97#define NFC_DMA_INT_ENABLE BIT(2)
98#define NFC_INT_MASK (NFC_B2R_INT_ENABLE | \
99 NFC_CMD_INT_ENABLE | \
100 NFC_DMA_INT_ENABLE)
101
Roy Spliet9c618292015-06-26 11:00:10 +0200102/* define NFC_TIMING_CFG register layout */
103#define NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD) \
104 (((tWB) & 0x3) | (((tADL) & 0x3) << 2) | \
105 (((tWHR) & 0x3) << 4) | (((tRHW) & 0x3) << 6) | \
106 (((tCAD) & 0x7) << 8))
107
Boris BREZILLON1fef62c2014-10-21 15:08:41 +0200108/* define bit use in NFC_CMD */
109#define NFC_CMD_LOW_BYTE GENMASK(7, 0)
110#define NFC_CMD_HIGH_BYTE GENMASK(15, 8)
111#define NFC_ADR_NUM GENMASK(18, 16)
112#define NFC_SEND_ADR BIT(19)
113#define NFC_ACCESS_DIR BIT(20)
114#define NFC_DATA_TRANS BIT(21)
115#define NFC_SEND_CMD1 BIT(22)
116#define NFC_WAIT_FLAG BIT(23)
117#define NFC_SEND_CMD2 BIT(24)
118#define NFC_SEQ BIT(25)
119#define NFC_DATA_SWAP_METHOD BIT(26)
120#define NFC_ROW_AUTO_INC BIT(27)
121#define NFC_SEND_CMD3 BIT(28)
122#define NFC_SEND_CMD4 BIT(29)
123#define NFC_CMD_TYPE GENMASK(31, 30)
124
125/* define bit use in NFC_RCMD_SET */
126#define NFC_READ_CMD GENMASK(7, 0)
127#define NFC_RANDOM_READ_CMD0 GENMASK(15, 8)
128#define NFC_RANDOM_READ_CMD1 GENMASK(23, 16)
129
130/* define bit use in NFC_WCMD_SET */
131#define NFC_PROGRAM_CMD GENMASK(7, 0)
132#define NFC_RANDOM_WRITE_CMD GENMASK(15, 8)
133#define NFC_READ_CMD0 GENMASK(23, 16)
134#define NFC_READ_CMD1 GENMASK(31, 24)
135
136/* define bit use in NFC_ECC_CTL */
137#define NFC_ECC_EN BIT(0)
138#define NFC_ECC_PIPELINE BIT(3)
139#define NFC_ECC_EXCEPTION BIT(4)
140#define NFC_ECC_BLOCK_SIZE BIT(5)
141#define NFC_RANDOM_EN BIT(9)
142#define NFC_RANDOM_DIRECTION BIT(10)
143#define NFC_ECC_MODE_SHIFT 12
144#define NFC_ECC_MODE GENMASK(15, 12)
145#define NFC_RANDOM_SEED GENMASK(30, 16)
146
147#define NFC_DEFAULT_TIMEOUT_MS 1000
148
149#define NFC_SRAM_SIZE 1024
150
151#define NFC_MAX_CS 7
152
153/*
154 * Ready/Busy detection type: describes the Ready/Busy detection modes
155 *
156 * @RB_NONE: no external detection available, rely on STATUS command
157 * and software timeouts
158 * @RB_NATIVE: use sunxi NAND controller Ready/Busy support. The Ready/Busy
159 * pin of the NAND flash chip must be connected to one of the
160 * native NAND R/B pins (those which can be muxed to the NAND
161 * Controller)
162 * @RB_GPIO: use a simple GPIO to handle Ready/Busy status. The Ready/Busy
163 * pin of the NAND flash chip must be connected to a GPIO capable
164 * pin.
165 */
166enum sunxi_nand_rb_type {
167 RB_NONE,
168 RB_NATIVE,
169 RB_GPIO,
170};
171
172/*
173 * Ready/Busy structure: stores information related to Ready/Busy detection
174 *
175 * @type: the Ready/Busy detection mode
176 * @info: information related to the R/B detection mode. Either a gpio
177 * id or a native R/B id (those supported by the NAND controller).
178 */
179struct sunxi_nand_rb {
180 enum sunxi_nand_rb_type type;
181 union {
182 int gpio;
183 int nativeid;
184 } info;
185};
186
187/*
188 * Chip Select structure: stores information related to NAND Chip Select
189 *
190 * @cs: the NAND CS id used to communicate with a NAND Chip
191 * @rb: the Ready/Busy description
192 */
193struct sunxi_nand_chip_sel {
194 u8 cs;
195 struct sunxi_nand_rb rb;
196};
197
198/*
199 * sunxi HW ECC infos: stores information related to HW ECC support
200 *
201 * @mode: the sunxi ECC mode field deduced from ECC requirements
202 * @layout: the OOB layout depending on the ECC requirements and the
203 * selected ECC mode
204 */
205struct sunxi_nand_hw_ecc {
206 int mode;
207 struct nand_ecclayout layout;
208};
209
210/*
211 * NAND chip structure: stores NAND chip device related information
212 *
213 * @node: used to store NAND chips into a list
214 * @nand: base NAND chip structure
215 * @mtd: base MTD structure
216 * @clk_rate: clk_rate required for this NAND chip
Roy Spliet9c618292015-06-26 11:00:10 +0200217 * @timing_cfg TIMING_CFG register value for this NAND chip
Boris BREZILLON1fef62c2014-10-21 15:08:41 +0200218 * @selected: current active CS
219 * @nsels: number of CS lines required by the NAND chip
220 * @sels: array of CS lines descriptions
221 */
222struct sunxi_nand_chip {
223 struct list_head node;
224 struct nand_chip nand;
225 struct mtd_info mtd;
226 unsigned long clk_rate;
Roy Spliet9c618292015-06-26 11:00:10 +0200227 u32 timing_cfg;
Boris BREZILLON1fef62c2014-10-21 15:08:41 +0200228 int selected;
229 int nsels;
230 struct sunxi_nand_chip_sel sels[0];
231};
232
233static inline struct sunxi_nand_chip *to_sunxi_nand(struct nand_chip *nand)
234{
235 return container_of(nand, struct sunxi_nand_chip, nand);
236}
237
238/*
239 * NAND Controller structure: stores sunxi NAND controller information
240 *
241 * @controller: base controller structure
242 * @dev: parent device (used to print error messages)
243 * @regs: NAND controller registers
244 * @ahb_clk: NAND Controller AHB clock
245 * @mod_clk: NAND Controller mod clock
246 * @assigned_cs: bitmask describing already assigned CS lines
247 * @clk_rate: NAND controller current clock rate
248 * @chips: a list containing all the NAND chips attached to
249 * this NAND controller
250 * @complete: a completion object used to wait for NAND
251 * controller events
252 */
253struct sunxi_nfc {
254 struct nand_hw_control controller;
255 struct device *dev;
256 void __iomem *regs;
257 struct clk *ahb_clk;
258 struct clk *mod_clk;
259 unsigned long assigned_cs;
260 unsigned long clk_rate;
261 struct list_head chips;
262 struct completion complete;
263};
264
265static inline struct sunxi_nfc *to_sunxi_nfc(struct nand_hw_control *ctrl)
266{
267 return container_of(ctrl, struct sunxi_nfc, controller);
268}
269
270static irqreturn_t sunxi_nfc_interrupt(int irq, void *dev_id)
271{
272 struct sunxi_nfc *nfc = dev_id;
273 u32 st = readl(nfc->regs + NFC_REG_ST);
274 u32 ien = readl(nfc->regs + NFC_REG_INT);
275
276 if (!(ien & st))
277 return IRQ_NONE;
278
279 if ((ien & st) == ien)
280 complete(&nfc->complete);
281
282 writel(st & NFC_INT_MASK, nfc->regs + NFC_REG_ST);
283 writel(~st & ien & NFC_INT_MASK, nfc->regs + NFC_REG_INT);
284
285 return IRQ_HANDLED;
286}
287
288static int sunxi_nfc_wait_int(struct sunxi_nfc *nfc, u32 flags,
289 unsigned int timeout_ms)
290{
291 init_completion(&nfc->complete);
292
293 writel(flags, nfc->regs + NFC_REG_INT);
294
295 if (!timeout_ms)
296 timeout_ms = NFC_DEFAULT_TIMEOUT_MS;
297
298 if (!wait_for_completion_timeout(&nfc->complete,
299 msecs_to_jiffies(timeout_ms))) {
300 dev_err(nfc->dev, "wait interrupt timedout\n");
301 return -ETIMEDOUT;
302 }
303
304 return 0;
305}
306
307static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc *nfc)
308{
309 unsigned long timeout = jiffies +
310 msecs_to_jiffies(NFC_DEFAULT_TIMEOUT_MS);
311
312 do {
313 if (!(readl(nfc->regs + NFC_REG_ST) & NFC_CMD_FIFO_STATUS))
314 return 0;
315 } while (time_before(jiffies, timeout));
316
317 dev_err(nfc->dev, "wait for empty cmd FIFO timedout\n");
318 return -ETIMEDOUT;
319}
320
321static int sunxi_nfc_rst(struct sunxi_nfc *nfc)
322{
323 unsigned long timeout = jiffies +
324 msecs_to_jiffies(NFC_DEFAULT_TIMEOUT_MS);
325
326 writel(0, nfc->regs + NFC_REG_ECC_CTL);
327 writel(NFC_RESET, nfc->regs + NFC_REG_CTL);
328
329 do {
330 if (!(readl(nfc->regs + NFC_REG_CTL) & NFC_RESET))
331 return 0;
332 } while (time_before(jiffies, timeout));
333
334 dev_err(nfc->dev, "wait for NAND controller reset timedout\n");
335 return -ETIMEDOUT;
336}
337
338static int sunxi_nfc_dev_ready(struct mtd_info *mtd)
339{
340 struct nand_chip *nand = mtd->priv;
341 struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
342 struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
343 struct sunxi_nand_rb *rb;
344 unsigned long timeo = (sunxi_nand->nand.state == FL_ERASING ? 400 : 20);
345 int ret;
346
347 if (sunxi_nand->selected < 0)
348 return 0;
349
350 rb = &sunxi_nand->sels[sunxi_nand->selected].rb;
351
352 switch (rb->type) {
353 case RB_NATIVE:
354 ret = !!(readl(nfc->regs + NFC_REG_ST) &
355 (NFC_RB_STATE0 << rb->info.nativeid));
356 if (ret)
357 break;
358
359 sunxi_nfc_wait_int(nfc, NFC_RB_B2R, timeo);
360 ret = !!(readl(nfc->regs + NFC_REG_ST) &
361 (NFC_RB_STATE0 << rb->info.nativeid));
362 break;
363 case RB_GPIO:
364 ret = gpio_get_value(rb->info.gpio);
365 break;
366 case RB_NONE:
367 default:
368 ret = 0;
369 dev_err(nfc->dev, "cannot check R/B NAND status!\n");
370 break;
371 }
372
373 return ret;
374}
375
376static void sunxi_nfc_select_chip(struct mtd_info *mtd, int chip)
377{
378 struct nand_chip *nand = mtd->priv;
379 struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
380 struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
381 struct sunxi_nand_chip_sel *sel;
382 u32 ctl;
383
384 if (chip > 0 && chip >= sunxi_nand->nsels)
385 return;
386
387 if (chip == sunxi_nand->selected)
388 return;
389
390 ctl = readl(nfc->regs + NFC_REG_CTL) &
391 ~(NFC_CE_SEL | NFC_RB_SEL | NFC_EN);
392
393 if (chip >= 0) {
394 sel = &sunxi_nand->sels[chip];
395
396 ctl |= (sel->cs << 24) | NFC_EN |
397 (((nand->page_shift - 10) & 0xf) << 8);
398 if (sel->rb.type == RB_NONE) {
399 nand->dev_ready = NULL;
400 } else {
401 nand->dev_ready = sunxi_nfc_dev_ready;
402 if (sel->rb.type == RB_NATIVE)
403 ctl |= (sel->rb.info.nativeid << 3);
404 }
405
406 writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA);
407
408 if (nfc->clk_rate != sunxi_nand->clk_rate) {
409 clk_set_rate(nfc->mod_clk, sunxi_nand->clk_rate);
410 nfc->clk_rate = sunxi_nand->clk_rate;
411 }
412 }
413
Roy Spliet9c618292015-06-26 11:00:10 +0200414 writel(sunxi_nand->timing_cfg, nfc->regs + NFC_REG_TIMING_CFG);
Boris BREZILLON1fef62c2014-10-21 15:08:41 +0200415 writel(ctl, nfc->regs + NFC_REG_CTL);
416
417 sunxi_nand->selected = chip;
418}
419
420static void sunxi_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
421{
422 struct nand_chip *nand = mtd->priv;
423 struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
424 struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
425 int ret;
426 int cnt;
427 int offs = 0;
428 u32 tmp;
429
430 while (len > offs) {
431 cnt = min(len - offs, NFC_SRAM_SIZE);
432
433 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
434 if (ret)
435 break;
436
437 writel(cnt, nfc->regs + NFC_REG_CNT);
438 tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD;
439 writel(tmp, nfc->regs + NFC_REG_CMD);
440
441 ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
442 if (ret)
443 break;
444
445 if (buf)
446 memcpy_fromio(buf + offs, nfc->regs + NFC_RAM0_BASE,
447 cnt);
448 offs += cnt;
449 }
450}
451
452static void sunxi_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
453 int len)
454{
455 struct nand_chip *nand = mtd->priv;
456 struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
457 struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
458 int ret;
459 int cnt;
460 int offs = 0;
461 u32 tmp;
462
463 while (len > offs) {
464 cnt = min(len - offs, NFC_SRAM_SIZE);
465
466 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
467 if (ret)
468 break;
469
470 writel(cnt, nfc->regs + NFC_REG_CNT);
471 memcpy_toio(nfc->regs + NFC_RAM0_BASE, buf + offs, cnt);
472 tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
473 NFC_ACCESS_DIR;
474 writel(tmp, nfc->regs + NFC_REG_CMD);
475
476 ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
477 if (ret)
478 break;
479
480 offs += cnt;
481 }
482}
483
484static uint8_t sunxi_nfc_read_byte(struct mtd_info *mtd)
485{
486 uint8_t ret;
487
488 sunxi_nfc_read_buf(mtd, &ret, 1);
489
490 return ret;
491}
492
493static void sunxi_nfc_cmd_ctrl(struct mtd_info *mtd, int dat,
494 unsigned int ctrl)
495{
496 struct nand_chip *nand = mtd->priv;
497 struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
498 struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
499 int ret;
500 u32 tmp;
501
502 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
503 if (ret)
504 return;
505
506 if (ctrl & NAND_CTRL_CHANGE) {
507 tmp = readl(nfc->regs + NFC_REG_CTL);
508 if (ctrl & NAND_NCE)
509 tmp |= NFC_CE_CTL;
510 else
511 tmp &= ~NFC_CE_CTL;
512 writel(tmp, nfc->regs + NFC_REG_CTL);
513 }
514
515 if (dat == NAND_CMD_NONE)
516 return;
517
518 if (ctrl & NAND_CLE) {
519 writel(NFC_SEND_CMD1 | dat, nfc->regs + NFC_REG_CMD);
520 } else {
521 writel(dat, nfc->regs + NFC_REG_ADDR_LOW);
522 writel(NFC_SEND_ADR, nfc->regs + NFC_REG_CMD);
523 }
524
525 sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
526}
527
528static int sunxi_nfc_hw_ecc_read_page(struct mtd_info *mtd,
529 struct nand_chip *chip, uint8_t *buf,
530 int oob_required, int page)
531{
532 struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
533 struct nand_ecc_ctrl *ecc = &chip->ecc;
534 struct nand_ecclayout *layout = ecc->layout;
535 struct sunxi_nand_hw_ecc *data = ecc->priv;
536 unsigned int max_bitflips = 0;
537 int offset;
538 int ret;
539 u32 tmp;
540 int i;
541 int cnt;
542
543 tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
544 tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE);
545 tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT) |
546 NFC_ECC_EXCEPTION;
547
548 writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
549
550 for (i = 0; i < ecc->steps; i++) {
551 if (i)
552 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, i * ecc->size, -1);
553
554 offset = mtd->writesize + layout->eccpos[i * ecc->bytes] - 4;
555
556 chip->read_buf(mtd, NULL, ecc->size);
557
558 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
559
560 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
561 if (ret)
562 return ret;
563
564 tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | (1 << 30);
565 writel(tmp, nfc->regs + NFC_REG_CMD);
566
567 ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
568 if (ret)
569 return ret;
570
571 memcpy_fromio(buf + (i * ecc->size),
572 nfc->regs + NFC_RAM0_BASE, ecc->size);
573
574 if (readl(nfc->regs + NFC_REG_ECC_ST) & 0x1) {
575 mtd->ecc_stats.failed++;
576 } else {
577 tmp = readl(nfc->regs + NFC_REG_ECC_CNT0) & 0xff;
578 mtd->ecc_stats.corrected += tmp;
579 max_bitflips = max_t(unsigned int, max_bitflips, tmp);
580 }
581
582 if (oob_required) {
583 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
584
585 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
586 if (ret)
587 return ret;
588
589 offset -= mtd->writesize;
590 chip->read_buf(mtd, chip->oob_poi + offset,
591 ecc->bytes + 4);
592 }
593 }
594
595 if (oob_required) {
596 cnt = ecc->layout->oobfree[ecc->steps].length;
597 if (cnt > 0) {
598 offset = mtd->writesize +
599 ecc->layout->oobfree[ecc->steps].offset;
600 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
601 offset -= mtd->writesize;
602 chip->read_buf(mtd, chip->oob_poi + offset, cnt);
603 }
604 }
605
606 tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
607 tmp &= ~NFC_ECC_EN;
608
609 writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
610
611 return max_bitflips;
612}
613
614static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd,
615 struct nand_chip *chip,
616 const uint8_t *buf, int oob_required)
617{
618 struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
619 struct nand_ecc_ctrl *ecc = &chip->ecc;
620 struct nand_ecclayout *layout = ecc->layout;
621 struct sunxi_nand_hw_ecc *data = ecc->priv;
622 int offset;
623 int ret;
624 u32 tmp;
625 int i;
626 int cnt;
627
628 tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
629 tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE);
630 tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT) |
631 NFC_ECC_EXCEPTION;
632
633 writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
634
635 for (i = 0; i < ecc->steps; i++) {
636 if (i)
637 chip->cmdfunc(mtd, NAND_CMD_RNDIN, i * ecc->size, -1);
638
639 chip->write_buf(mtd, buf + (i * ecc->size), ecc->size);
640
641 offset = layout->eccpos[i * ecc->bytes] - 4 + mtd->writesize;
642
643 /* Fill OOB data in */
644 if (oob_required) {
645 tmp = 0xffffffff;
646 memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
647 4);
648 } else {
649 memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE,
650 chip->oob_poi + offset - mtd->writesize,
651 4);
652 }
653
654 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset, -1);
655
656 ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
657 if (ret)
658 return ret;
659
660 tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ACCESS_DIR |
661 (1 << 30);
662 writel(tmp, nfc->regs + NFC_REG_CMD);
663 ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
664 if (ret)
665 return ret;
666 }
667
668 if (oob_required) {
669 cnt = ecc->layout->oobfree[i].length;
670 if (cnt > 0) {
671 offset = mtd->writesize +
672 ecc->layout->oobfree[i].offset;
673 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset, -1);
674 offset -= mtd->writesize;
675 chip->write_buf(mtd, chip->oob_poi + offset, cnt);
676 }
677 }
678
679 tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
680 tmp &= ~NFC_ECC_EN;
681
682 writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
683
684 return 0;
685}
686
687static int sunxi_nfc_hw_syndrome_ecc_read_page(struct mtd_info *mtd,
688 struct nand_chip *chip,
689 uint8_t *buf, int oob_required,
690 int page)
691{
692 struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
693 struct nand_ecc_ctrl *ecc = &chip->ecc;
694 struct sunxi_nand_hw_ecc *data = ecc->priv;
695 unsigned int max_bitflips = 0;
696 uint8_t *oob = chip->oob_poi;
697 int offset = 0;
698 int ret;
699 int cnt;
700 u32 tmp;
701 int i;
702
703 tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
704 tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE);
705 tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT) |
706 NFC_ECC_EXCEPTION;
707
708 writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
709
710 for (i = 0; i < ecc->steps; i++) {
711 chip->read_buf(mtd, NULL, ecc->size);
712
713 tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | (1 << 30);
714 writel(tmp, nfc->regs + NFC_REG_CMD);
715
716 ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
717 if (ret)
718 return ret;
719
720 memcpy_fromio(buf, nfc->regs + NFC_RAM0_BASE, ecc->size);
721 buf += ecc->size;
722 offset += ecc->size;
723
724 if (readl(nfc->regs + NFC_REG_ECC_ST) & 0x1) {
725 mtd->ecc_stats.failed++;
726 } else {
727 tmp = readl(nfc->regs + NFC_REG_ECC_CNT0) & 0xff;
728 mtd->ecc_stats.corrected += tmp;
729 max_bitflips = max_t(unsigned int, max_bitflips, tmp);
730 }
731
732 if (oob_required) {
733 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
734 chip->read_buf(mtd, oob, ecc->bytes + ecc->prepad);
735 oob += ecc->bytes + ecc->prepad;
736 }
737
738 offset += ecc->bytes + ecc->prepad;
739 }
740
741 if (oob_required) {
742 cnt = mtd->oobsize - (oob - chip->oob_poi);
743 if (cnt > 0) {
744 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
745 chip->read_buf(mtd, oob, cnt);
746 }
747 }
748
749 writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_ECC_EN,
750 nfc->regs + NFC_REG_ECC_CTL);
751
752 return max_bitflips;
753}
754
755static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info *mtd,
756 struct nand_chip *chip,
757 const uint8_t *buf,
758 int oob_required)
759{
760 struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
761 struct nand_ecc_ctrl *ecc = &chip->ecc;
762 struct sunxi_nand_hw_ecc *data = ecc->priv;
763 uint8_t *oob = chip->oob_poi;
764 int offset = 0;
765 int ret;
766 int cnt;
767 u32 tmp;
768 int i;
769
770 tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
771 tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE);
772 tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT) |
773 NFC_ECC_EXCEPTION;
774
775 writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
776
777 for (i = 0; i < ecc->steps; i++) {
778 chip->write_buf(mtd, buf + (i * ecc->size), ecc->size);
779 offset += ecc->size;
780
781 /* Fill OOB data in */
782 if (oob_required) {
783 tmp = 0xffffffff;
784 memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
785 4);
786 } else {
787 memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, oob,
788 4);
789 }
790
791 tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ACCESS_DIR |
792 (1 << 30);
793 writel(tmp, nfc->regs + NFC_REG_CMD);
794
795 ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
796 if (ret)
797 return ret;
798
799 offset += ecc->bytes + ecc->prepad;
800 oob += ecc->bytes + ecc->prepad;
801 }
802
803 if (oob_required) {
804 cnt = mtd->oobsize - (oob - chip->oob_poi);
805 if (cnt > 0) {
806 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset, -1);
807 chip->write_buf(mtd, oob, cnt);
808 }
809 }
810
811 tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
812 tmp &= ~NFC_ECC_EN;
813
814 writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
815
816 return 0;
817}
818
Roy Spliet9c618292015-06-26 11:00:10 +0200819static const s32 tWB_lut[] = {6, 12, 16, 20};
820static const s32 tRHW_lut[] = {4, 8, 12, 20};
821
822static int _sunxi_nand_lookup_timing(const s32 *lut, int lut_size, u32 duration,
823 u32 clk_period)
824{
825 u32 clk_cycles = DIV_ROUND_UP(duration, clk_period);
826 int i;
827
828 for (i = 0; i < lut_size; i++) {
829 if (clk_cycles <= lut[i])
830 return i;
831 }
832
833 /* Doesn't fit */
834 return -EINVAL;
835}
836
837#define sunxi_nand_lookup_timing(l, p, c) \
838 _sunxi_nand_lookup_timing(l, ARRAY_SIZE(l), p, c)
839
Boris BREZILLON1fef62c2014-10-21 15:08:41 +0200840static int sunxi_nand_chip_set_timings(struct sunxi_nand_chip *chip,
841 const struct nand_sdr_timings *timings)
842{
Roy Spliet9c618292015-06-26 11:00:10 +0200843 struct sunxi_nfc *nfc = to_sunxi_nfc(chip->nand.controller);
Boris BREZILLON1fef62c2014-10-21 15:08:41 +0200844 u32 min_clk_period = 0;
Roy Spliet9c618292015-06-26 11:00:10 +0200845 s32 tWB, tADL, tWHR, tRHW, tCAD;
Boris BREZILLON1fef62c2014-10-21 15:08:41 +0200846
847 /* T1 <=> tCLS */
848 if (timings->tCLS_min > min_clk_period)
849 min_clk_period = timings->tCLS_min;
850
851 /* T2 <=> tCLH */
852 if (timings->tCLH_min > min_clk_period)
853 min_clk_period = timings->tCLH_min;
854
855 /* T3 <=> tCS */
856 if (timings->tCS_min > min_clk_period)
857 min_clk_period = timings->tCS_min;
858
859 /* T4 <=> tCH */
860 if (timings->tCH_min > min_clk_period)
861 min_clk_period = timings->tCH_min;
862
863 /* T5 <=> tWP */
864 if (timings->tWP_min > min_clk_period)
865 min_clk_period = timings->tWP_min;
866
867 /* T6 <=> tWH */
868 if (timings->tWH_min > min_clk_period)
869 min_clk_period = timings->tWH_min;
870
871 /* T7 <=> tALS */
872 if (timings->tALS_min > min_clk_period)
873 min_clk_period = timings->tALS_min;
874
875 /* T8 <=> tDS */
876 if (timings->tDS_min > min_clk_period)
877 min_clk_period = timings->tDS_min;
878
879 /* T9 <=> tDH */
880 if (timings->tDH_min > min_clk_period)
881 min_clk_period = timings->tDH_min;
882
883 /* T10 <=> tRR */
884 if (timings->tRR_min > (min_clk_period * 3))
885 min_clk_period = DIV_ROUND_UP(timings->tRR_min, 3);
886
887 /* T11 <=> tALH */
888 if (timings->tALH_min > min_clk_period)
889 min_clk_period = timings->tALH_min;
890
891 /* T12 <=> tRP */
892 if (timings->tRP_min > min_clk_period)
893 min_clk_period = timings->tRP_min;
894
895 /* T13 <=> tREH */
896 if (timings->tREH_min > min_clk_period)
897 min_clk_period = timings->tREH_min;
898
899 /* T14 <=> tRC */
900 if (timings->tRC_min > (min_clk_period * 2))
901 min_clk_period = DIV_ROUND_UP(timings->tRC_min, 2);
902
903 /* T15 <=> tWC */
904 if (timings->tWC_min > (min_clk_period * 2))
905 min_clk_period = DIV_ROUND_UP(timings->tWC_min, 2);
906
Roy Spliet9c618292015-06-26 11:00:10 +0200907 /* T16 - T19 + tCAD */
908 tWB = sunxi_nand_lookup_timing(tWB_lut, timings->tWB_max,
909 min_clk_period);
910 if (tWB < 0) {
911 dev_err(nfc->dev, "unsupported tWB\n");
912 return tWB;
913 }
914
915 tADL = DIV_ROUND_UP(timings->tADL_min, min_clk_period) >> 3;
916 if (tADL > 3) {
917 dev_err(nfc->dev, "unsupported tADL\n");
918 return -EINVAL;
919 }
920
921 tWHR = DIV_ROUND_UP(timings->tWHR_min, min_clk_period) >> 3;
922 if (tWHR > 3) {
923 dev_err(nfc->dev, "unsupported tWHR\n");
924 return -EINVAL;
925 }
926
927 tRHW = sunxi_nand_lookup_timing(tRHW_lut, timings->tRHW_min,
928 min_clk_period);
929 if (tRHW < 0) {
930 dev_err(nfc->dev, "unsupported tRHW\n");
931 return tRHW;
932 }
933
934 /*
935 * TODO: according to ONFI specs this value only applies for DDR NAND,
936 * but Allwinner seems to set this to 0x7. Mimic them for now.
937 */
938 tCAD = 0x7;
939
940 /* TODO: A83 has some more bits for CDQSS, CS, CLHZ, CCS, WC */
941 chip->timing_cfg = NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD);
Boris BREZILLON1fef62c2014-10-21 15:08:41 +0200942
943 /* Convert min_clk_period from picoseconds to nanoseconds */
944 min_clk_period = DIV_ROUND_UP(min_clk_period, 1000);
945
946 /*
947 * Convert min_clk_period into a clk frequency, then get the
948 * appropriate rate for the NAND controller IP given this formula
949 * (specified in the datasheet):
950 * nand clk_rate = 2 * min_clk_rate
951 */
952 chip->clk_rate = (2 * NSEC_PER_SEC) / min_clk_period;
953
Boris BREZILLON1fef62c2014-10-21 15:08:41 +0200954 return 0;
955}
956
957static int sunxi_nand_chip_init_timings(struct sunxi_nand_chip *chip,
958 struct device_node *np)
959{
960 const struct nand_sdr_timings *timings;
961 int ret;
962 int mode;
963
964 mode = onfi_get_async_timing_mode(&chip->nand);
965 if (mode == ONFI_TIMING_MODE_UNKNOWN) {
966 mode = chip->nand.onfi_timing_mode_default;
967 } else {
968 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {};
969
970 mode = fls(mode) - 1;
971 if (mode < 0)
972 mode = 0;
973
974 feature[0] = mode;
975 ret = chip->nand.onfi_set_features(&chip->mtd, &chip->nand,
976 ONFI_FEATURE_ADDR_TIMING_MODE,
977 feature);
978 if (ret)
979 return ret;
980 }
981
982 timings = onfi_async_timing_mode_to_sdr_timings(mode);
983 if (IS_ERR(timings))
984 return PTR_ERR(timings);
985
986 return sunxi_nand_chip_set_timings(chip, timings);
987}
988
989static int sunxi_nand_hw_common_ecc_ctrl_init(struct mtd_info *mtd,
990 struct nand_ecc_ctrl *ecc,
991 struct device_node *np)
992{
993 static const u8 strengths[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 };
994 struct nand_chip *nand = mtd->priv;
995 struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
996 struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
997 struct sunxi_nand_hw_ecc *data;
998 struct nand_ecclayout *layout;
999 int nsectors;
1000 int ret;
1001 int i;
1002
1003 data = kzalloc(sizeof(*data), GFP_KERNEL);
1004 if (!data)
1005 return -ENOMEM;
1006
1007 /* Add ECC info retrieval from DT */
1008 for (i = 0; i < ARRAY_SIZE(strengths); i++) {
1009 if (ecc->strength <= strengths[i])
1010 break;
1011 }
1012
1013 if (i >= ARRAY_SIZE(strengths)) {
1014 dev_err(nfc->dev, "unsupported strength\n");
1015 ret = -ENOTSUPP;
1016 goto err;
1017 }
1018
1019 data->mode = i;
1020
1021 /* HW ECC always request ECC bytes for 1024 bytes blocks */
1022 ecc->bytes = DIV_ROUND_UP(ecc->strength * fls(8 * 1024), 8);
1023
1024 /* HW ECC always work with even numbers of ECC bytes */
1025 ecc->bytes = ALIGN(ecc->bytes, 2);
1026
1027 layout = &data->layout;
1028 nsectors = mtd->writesize / ecc->size;
1029
1030 if (mtd->oobsize < ((ecc->bytes + 4) * nsectors)) {
1031 ret = -EINVAL;
1032 goto err;
1033 }
1034
1035 layout->eccbytes = (ecc->bytes * nsectors);
1036
1037 ecc->layout = layout;
1038 ecc->priv = data;
1039
1040 return 0;
1041
1042err:
1043 kfree(data);
1044
1045 return ret;
1046}
1047
1048static void sunxi_nand_hw_common_ecc_ctrl_cleanup(struct nand_ecc_ctrl *ecc)
1049{
1050 kfree(ecc->priv);
1051}
1052
1053static int sunxi_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
1054 struct nand_ecc_ctrl *ecc,
1055 struct device_node *np)
1056{
1057 struct nand_ecclayout *layout;
1058 int nsectors;
1059 int i, j;
1060 int ret;
1061
1062 ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc, np);
1063 if (ret)
1064 return ret;
1065
1066 ecc->read_page = sunxi_nfc_hw_ecc_read_page;
1067 ecc->write_page = sunxi_nfc_hw_ecc_write_page;
1068 layout = ecc->layout;
1069 nsectors = mtd->writesize / ecc->size;
1070
1071 for (i = 0; i < nsectors; i++) {
1072 if (i) {
1073 layout->oobfree[i].offset =
1074 layout->oobfree[i - 1].offset +
1075 layout->oobfree[i - 1].length +
1076 ecc->bytes;
1077 layout->oobfree[i].length = 4;
1078 } else {
1079 /*
1080 * The first 2 bytes are used for BB markers, hence we
1081 * only have 2 bytes available in the first user data
1082 * section.
1083 */
1084 layout->oobfree[i].length = 2;
1085 layout->oobfree[i].offset = 2;
1086 }
1087
1088 for (j = 0; j < ecc->bytes; j++)
1089 layout->eccpos[(ecc->bytes * i) + j] =
1090 layout->oobfree[i].offset +
1091 layout->oobfree[i].length + j;
1092 }
1093
1094 if (mtd->oobsize > (ecc->bytes + 4) * nsectors) {
1095 layout->oobfree[nsectors].offset =
1096 layout->oobfree[nsectors - 1].offset +
1097 layout->oobfree[nsectors - 1].length +
1098 ecc->bytes;
1099 layout->oobfree[nsectors].length = mtd->oobsize -
1100 ((ecc->bytes + 4) * nsectors);
1101 }
1102
1103 return 0;
1104}
1105
1106static int sunxi_nand_hw_syndrome_ecc_ctrl_init(struct mtd_info *mtd,
1107 struct nand_ecc_ctrl *ecc,
1108 struct device_node *np)
1109{
1110 struct nand_ecclayout *layout;
1111 int nsectors;
1112 int i;
1113 int ret;
1114
1115 ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc, np);
1116 if (ret)
1117 return ret;
1118
1119 ecc->prepad = 4;
1120 ecc->read_page = sunxi_nfc_hw_syndrome_ecc_read_page;
1121 ecc->write_page = sunxi_nfc_hw_syndrome_ecc_write_page;
1122
1123 layout = ecc->layout;
1124 nsectors = mtd->writesize / ecc->size;
1125
1126 for (i = 0; i < (ecc->bytes * nsectors); i++)
1127 layout->eccpos[i] = i;
1128
1129 layout->oobfree[0].length = mtd->oobsize - i;
1130 layout->oobfree[0].offset = i;
1131
1132 return 0;
1133}
1134
1135static void sunxi_nand_ecc_cleanup(struct nand_ecc_ctrl *ecc)
1136{
1137 switch (ecc->mode) {
1138 case NAND_ECC_HW:
1139 case NAND_ECC_HW_SYNDROME:
1140 sunxi_nand_hw_common_ecc_ctrl_cleanup(ecc);
1141 break;
1142 case NAND_ECC_NONE:
1143 kfree(ecc->layout);
1144 default:
1145 break;
1146 }
1147}
1148
1149static int sunxi_nand_ecc_init(struct mtd_info *mtd, struct nand_ecc_ctrl *ecc,
1150 struct device_node *np)
1151{
1152 struct nand_chip *nand = mtd->priv;
1153 int strength;
1154 int blk_size;
1155 int ret;
1156
1157 blk_size = of_get_nand_ecc_step_size(np);
1158 strength = of_get_nand_ecc_strength(np);
1159 if (blk_size > 0 && strength > 0) {
1160 ecc->size = blk_size;
1161 ecc->strength = strength;
1162 } else {
1163 ecc->size = nand->ecc_step_ds;
1164 ecc->strength = nand->ecc_strength_ds;
1165 }
1166
1167 if (!ecc->size || !ecc->strength)
1168 return -EINVAL;
1169
1170 ecc->mode = NAND_ECC_HW;
1171
1172 ret = of_get_nand_ecc_mode(np);
1173 if (ret >= 0)
1174 ecc->mode = ret;
1175
1176 switch (ecc->mode) {
1177 case NAND_ECC_SOFT_BCH:
Boris BREZILLON1fef62c2014-10-21 15:08:41 +02001178 break;
1179 case NAND_ECC_HW:
1180 ret = sunxi_nand_hw_ecc_ctrl_init(mtd, ecc, np);
1181 if (ret)
1182 return ret;
1183 break;
1184 case NAND_ECC_HW_SYNDROME:
1185 ret = sunxi_nand_hw_syndrome_ecc_ctrl_init(mtd, ecc, np);
1186 if (ret)
1187 return ret;
1188 break;
1189 case NAND_ECC_NONE:
1190 ecc->layout = kzalloc(sizeof(*ecc->layout), GFP_KERNEL);
1191 if (!ecc->layout)
1192 return -ENOMEM;
1193 ecc->layout->oobfree[0].length = mtd->oobsize;
1194 case NAND_ECC_SOFT:
1195 break;
1196 default:
1197 return -EINVAL;
1198 }
1199
1200 return 0;
1201}
1202
1203static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc,
1204 struct device_node *np)
1205{
1206 const struct nand_sdr_timings *timings;
1207 struct sunxi_nand_chip *chip;
1208 struct mtd_part_parser_data ppdata;
1209 struct mtd_info *mtd;
1210 struct nand_chip *nand;
1211 int nsels;
1212 int ret;
1213 int i;
1214 u32 tmp;
1215
1216 if (!of_get_property(np, "reg", &nsels))
1217 return -EINVAL;
1218
1219 nsels /= sizeof(u32);
1220 if (!nsels) {
1221 dev_err(dev, "invalid reg property size\n");
1222 return -EINVAL;
1223 }
1224
1225 chip = devm_kzalloc(dev,
1226 sizeof(*chip) +
1227 (nsels * sizeof(struct sunxi_nand_chip_sel)),
1228 GFP_KERNEL);
1229 if (!chip) {
1230 dev_err(dev, "could not allocate chip\n");
1231 return -ENOMEM;
1232 }
1233
1234 chip->nsels = nsels;
1235 chip->selected = -1;
1236
1237 for (i = 0; i < nsels; i++) {
1238 ret = of_property_read_u32_index(np, "reg", i, &tmp);
1239 if (ret) {
1240 dev_err(dev, "could not retrieve reg property: %d\n",
1241 ret);
1242 return ret;
1243 }
1244
1245 if (tmp > NFC_MAX_CS) {
1246 dev_err(dev,
1247 "invalid reg value: %u (max CS = 7)\n",
1248 tmp);
1249 return -EINVAL;
1250 }
1251
1252 if (test_and_set_bit(tmp, &nfc->assigned_cs)) {
1253 dev_err(dev, "CS %d already assigned\n", tmp);
1254 return -EINVAL;
1255 }
1256
1257 chip->sels[i].cs = tmp;
1258
1259 if (!of_property_read_u32_index(np, "allwinner,rb", i, &tmp) &&
1260 tmp < 2) {
1261 chip->sels[i].rb.type = RB_NATIVE;
1262 chip->sels[i].rb.info.nativeid = tmp;
1263 } else {
1264 ret = of_get_named_gpio(np, "rb-gpios", i);
1265 if (ret >= 0) {
1266 tmp = ret;
1267 chip->sels[i].rb.type = RB_GPIO;
1268 chip->sels[i].rb.info.gpio = tmp;
1269 ret = devm_gpio_request(dev, tmp, "nand-rb");
1270 if (ret)
1271 return ret;
1272
1273 ret = gpio_direction_input(tmp);
1274 if (ret)
1275 return ret;
1276 } else {
1277 chip->sels[i].rb.type = RB_NONE;
1278 }
1279 }
1280 }
1281
1282 timings = onfi_async_timing_mode_to_sdr_timings(0);
1283 if (IS_ERR(timings)) {
1284 ret = PTR_ERR(timings);
1285 dev_err(dev,
1286 "could not retrieve timings for ONFI mode 0: %d\n",
1287 ret);
1288 return ret;
1289 }
1290
1291 ret = sunxi_nand_chip_set_timings(chip, timings);
1292 if (ret) {
1293 dev_err(dev, "could not configure chip timings: %d\n", ret);
1294 return ret;
1295 }
1296
1297 nand = &chip->nand;
1298 /* Default tR value specified in the ONFI spec (chapter 4.15.1) */
1299 nand->chip_delay = 200;
1300 nand->controller = &nfc->controller;
1301 nand->select_chip = sunxi_nfc_select_chip;
1302 nand->cmd_ctrl = sunxi_nfc_cmd_ctrl;
1303 nand->read_buf = sunxi_nfc_read_buf;
1304 nand->write_buf = sunxi_nfc_write_buf;
1305 nand->read_byte = sunxi_nfc_read_byte;
1306
1307 if (of_get_nand_on_flash_bbt(np))
1308 nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1309
1310 mtd = &chip->mtd;
1311 mtd->dev.parent = dev;
1312 mtd->priv = nand;
1313 mtd->owner = THIS_MODULE;
1314
1315 ret = nand_scan_ident(mtd, nsels, NULL);
1316 if (ret)
1317 return ret;
1318
1319 ret = sunxi_nand_chip_init_timings(chip, np);
1320 if (ret) {
1321 dev_err(dev, "could not configure chip timings: %d\n", ret);
1322 return ret;
1323 }
1324
1325 ret = sunxi_nand_ecc_init(mtd, &nand->ecc, np);
1326 if (ret) {
1327 dev_err(dev, "ECC init failed: %d\n", ret);
1328 return ret;
1329 }
1330
1331 ret = nand_scan_tail(mtd);
1332 if (ret) {
1333 dev_err(dev, "nand_scan_tail failed: %d\n", ret);
1334 return ret;
1335 }
1336
1337 ppdata.of_node = np;
1338 ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
1339 if (ret) {
1340 dev_err(dev, "failed to register mtd device: %d\n", ret);
1341 nand_release(mtd);
1342 return ret;
1343 }
1344
1345 list_add_tail(&chip->node, &nfc->chips);
1346
1347 return 0;
1348}
1349
1350static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc)
1351{
1352 struct device_node *np = dev->of_node;
1353 struct device_node *nand_np;
1354 int nchips = of_get_child_count(np);
1355 int ret;
1356
1357 if (nchips > 8) {
1358 dev_err(dev, "too many NAND chips: %d (max = 8)\n", nchips);
1359 return -EINVAL;
1360 }
1361
1362 for_each_child_of_node(np, nand_np) {
1363 ret = sunxi_nand_chip_init(dev, nfc, nand_np);
1364 if (ret)
1365 return ret;
1366 }
1367
1368 return 0;
1369}
1370
1371static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc)
1372{
1373 struct sunxi_nand_chip *chip;
1374
1375 while (!list_empty(&nfc->chips)) {
1376 chip = list_first_entry(&nfc->chips, struct sunxi_nand_chip,
1377 node);
1378 nand_release(&chip->mtd);
1379 sunxi_nand_ecc_cleanup(&chip->nand.ecc);
1380 }
1381}
1382
1383static int sunxi_nfc_probe(struct platform_device *pdev)
1384{
1385 struct device *dev = &pdev->dev;
1386 struct resource *r;
1387 struct sunxi_nfc *nfc;
1388 int irq;
1389 int ret;
1390
1391 nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL);
1392 if (!nfc)
1393 return -ENOMEM;
1394
1395 nfc->dev = dev;
1396 spin_lock_init(&nfc->controller.lock);
1397 init_waitqueue_head(&nfc->controller.wq);
1398 INIT_LIST_HEAD(&nfc->chips);
1399
1400 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1401 nfc->regs = devm_ioremap_resource(dev, r);
1402 if (IS_ERR(nfc->regs))
1403 return PTR_ERR(nfc->regs);
1404
1405 irq = platform_get_irq(pdev, 0);
1406 if (irq < 0) {
1407 dev_err(dev, "failed to retrieve irq\n");
1408 return irq;
1409 }
1410
1411 nfc->ahb_clk = devm_clk_get(dev, "ahb");
1412 if (IS_ERR(nfc->ahb_clk)) {
1413 dev_err(dev, "failed to retrieve ahb clk\n");
1414 return PTR_ERR(nfc->ahb_clk);
1415 }
1416
1417 ret = clk_prepare_enable(nfc->ahb_clk);
1418 if (ret)
1419 return ret;
1420
1421 nfc->mod_clk = devm_clk_get(dev, "mod");
1422 if (IS_ERR(nfc->mod_clk)) {
1423 dev_err(dev, "failed to retrieve mod clk\n");
1424 ret = PTR_ERR(nfc->mod_clk);
1425 goto out_ahb_clk_unprepare;
1426 }
1427
1428 ret = clk_prepare_enable(nfc->mod_clk);
1429 if (ret)
1430 goto out_ahb_clk_unprepare;
1431
1432 ret = sunxi_nfc_rst(nfc);
1433 if (ret)
1434 goto out_mod_clk_unprepare;
1435
1436 writel(0, nfc->regs + NFC_REG_INT);
1437 ret = devm_request_irq(dev, irq, sunxi_nfc_interrupt,
1438 0, "sunxi-nand", nfc);
1439 if (ret)
1440 goto out_mod_clk_unprepare;
1441
1442 platform_set_drvdata(pdev, nfc);
1443
1444 /*
Roy Spliet9c618292015-06-26 11:00:10 +02001445 * TODO: replace this magic value with EDO flag
Boris BREZILLON1fef62c2014-10-21 15:08:41 +02001446 */
1447 writel(0x100, nfc->regs + NFC_REG_TIMING_CTL);
Boris BREZILLON1fef62c2014-10-21 15:08:41 +02001448
1449 ret = sunxi_nand_chips_init(dev, nfc);
1450 if (ret) {
1451 dev_err(dev, "failed to init nand chips\n");
1452 goto out_mod_clk_unprepare;
1453 }
1454
1455 return 0;
1456
1457out_mod_clk_unprepare:
1458 clk_disable_unprepare(nfc->mod_clk);
1459out_ahb_clk_unprepare:
1460 clk_disable_unprepare(nfc->ahb_clk);
1461
1462 return ret;
1463}
1464
1465static int sunxi_nfc_remove(struct platform_device *pdev)
1466{
1467 struct sunxi_nfc *nfc = platform_get_drvdata(pdev);
1468
1469 sunxi_nand_chips_cleanup(nfc);
1470
1471 return 0;
1472}
1473
1474static const struct of_device_id sunxi_nfc_ids[] = {
1475 { .compatible = "allwinner,sun4i-a10-nand" },
1476 { /* sentinel */ }
1477};
1478MODULE_DEVICE_TABLE(of, sunxi_nfc_ids);
1479
1480static struct platform_driver sunxi_nfc_driver = {
1481 .driver = {
1482 .name = "sunxi_nand",
1483 .of_match_table = sunxi_nfc_ids,
1484 },
1485 .probe = sunxi_nfc_probe,
1486 .remove = sunxi_nfc_remove,
1487};
1488module_platform_driver(sunxi_nfc_driver);
1489
1490MODULE_LICENSE("GPL v2");
1491MODULE_AUTHOR("Boris BREZILLON");
1492MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver");
1493MODULE_ALIAS("platform:sunxi_nand");