blob: ef9c9f547c01fffe391235e3a6d9dffe95944321 [file] [log] [blame]
Andrew Victor42cb1402006-10-19 18:24:35 +02001/*
Josh Wu1c7b8742012-06-29 17:47:55 +08002 * Copyright © 2003 Rick Bronson
Andrew Victor42cb1402006-10-19 18:24:35 +02003 *
4 * Derived from drivers/mtd/nand/autcpu12.c
Josh Wu1c7b8742012-06-29 17:47:55 +08005 * Copyright © 2001 Thomas Gleixner (gleixner@autronix.de)
Andrew Victor42cb1402006-10-19 18:24:35 +02006 *
7 * Derived from drivers/mtd/spia.c
Josh Wu1c7b8742012-06-29 17:47:55 +08008 * Copyright © 2000 Steven J. Hill (sjhill@cotw.com)
Andrew Victor42cb1402006-10-19 18:24:35 +02009 *
Richard Genoud77f54922008-04-23 19:51:14 +020010 *
11 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
Josh Wu1c7b8742012-06-29 17:47:55 +080012 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright © 2007
Richard Genoud77f54922008-04-23 19:51:14 +020013 *
14 * Derived from Das U-Boot source code
15 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
Josh Wu1c7b8742012-06-29 17:47:55 +080016 * © Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
Richard Genoud77f54922008-04-23 19:51:14 +020017 *
Josh Wu1c7b8742012-06-29 17:47:55 +080018 * Add Programmable Multibit ECC support for various AT91 SoC
19 * © Copyright 2012 ATMEL, Hong Xu
Richard Genoud77f54922008-04-23 19:51:14 +020020 *
Josh Wu7dc37de2013-08-05 19:14:35 +080021 * Add Nand Flash Controller support for SAMA5 SoC
22 * © Copyright 2013 ATMEL, Josh Wu (josh.wu@atmel.com)
23 *
Andrew Victor42cb1402006-10-19 18:24:35 +020024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000030#include <linux/dma-mapping.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020031#include <linux/slab.h>
32#include <linux/module.h>
Simon Polettef4fa6972009-05-27 18:19:39 +030033#include <linux/moduleparam.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020034#include <linux/platform_device.h>
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080035#include <linux/of.h>
36#include <linux/of_device.h>
37#include <linux/of_gpio.h>
38#include <linux/of_mtd.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020039#include <linux/mtd/mtd.h>
40#include <linux/mtd/nand.h>
41#include <linux/mtd/partitions.h>
42
Josh Wu7dc37de2013-08-05 19:14:35 +080043#include <linux/delay.h>
Hans-Christian Egtvedt5c39c4c2011-04-13 15:55:17 +020044#include <linux/dmaengine.h>
David Woodhouse90574d02008-06-07 08:49:00 +010045#include <linux/gpio.h>
Josh Wu7dc37de2013-08-05 19:14:35 +080046#include <linux/interrupt.h>
David Woodhouse90574d02008-06-07 08:49:00 +010047#include <linux/io.h>
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +080048#include <linux/platform_data/atmel.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020049
Hong Xucbc6c5e2011-01-18 14:36:05 +080050static int use_dma = 1;
51module_param(use_dma, int, 0);
52
Simon Polettef4fa6972009-05-27 18:19:39 +030053static int on_flash_bbt = 0;
54module_param(on_flash_bbt, int, 0);
55
Richard Genoud77f54922008-04-23 19:51:14 +020056/* Register access macros */
57#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020058 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020059#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020060 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020061
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020062#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Josh Wu7dc37de2013-08-05 19:14:35 +080063#include "atmel_nand_nfc.h" /* Nand Flash Controller definition */
Richard Genoud77f54922008-04-23 19:51:14 +020064
65/* oob layout for large page size
66 * bad block info is on bytes 0 and 1
67 * the bytes have to be consecutives to avoid
68 * several NAND_CMD_RNDOUT during read
69 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020070static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020071 .eccbytes = 4,
72 .eccpos = {60, 61, 62, 63},
73 .oobfree = {
74 {2, 58}
75 },
76};
77
78/* oob layout for small page size
79 * bad block info is on bytes 4 and 5
80 * the bytes have to be consecutives to avoid
81 * several NAND_CMD_RNDOUT during read
82 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020083static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020084 .eccbytes = 4,
85 .eccpos = {0, 1, 2, 3},
86 .oobfree = {
87 {6, 10}
88 },
89};
90
Josh Wu7dc37de2013-08-05 19:14:35 +080091struct atmel_nfc {
92 void __iomem *base_cmd_regs;
93 void __iomem *hsmc_regs;
94 void __iomem *sram_bank0;
95 dma_addr_t sram_bank0_phys;
Josh Wu1ae9c092013-08-05 19:14:36 +080096 bool use_nfc_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +080097 bool write_by_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +080098
99 bool is_initialized;
100 struct completion comp_nfc;
Josh Wu1ae9c092013-08-05 19:14:36 +0800101
102 /* Point to the sram bank which include readed data via NFC */
103 void __iomem *data_in_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +0800104 bool will_write_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +0800105};
106static struct atmel_nfc nand_nfc;
107
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200108struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +0200109 struct nand_chip nand_chip;
110 struct mtd_info mtd;
111 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800112 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800113 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +0200114 struct device *dev;
115 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800116
117 struct completion comp;
118 struct dma_chan *dma_chan;
Josh Wua41b51a2012-06-29 17:47:54 +0800119
Josh Wu7dc37de2013-08-05 19:14:35 +0800120 struct atmel_nfc *nfc;
121
Josh Wua41b51a2012-06-29 17:47:54 +0800122 bool has_pmecc;
123 u8 pmecc_corr_cap;
124 u16 pmecc_sector_size;
125 u32 pmecc_lookup_table_offset;
Josh Wue66b4312013-01-23 20:47:11 +0800126 u32 pmecc_lookup_table_offset_512;
127 u32 pmecc_lookup_table_offset_1024;
Josh Wu1c7b8742012-06-29 17:47:55 +0800128
129 int pmecc_bytes_per_sector;
130 int pmecc_sector_number;
131 int pmecc_degree; /* Degree of remainders */
132 int pmecc_cw_len; /* Length of codeword */
133
134 void __iomem *pmerrloc_base;
135 void __iomem *pmecc_rom_base;
136
137 /* lookup table for alpha_to and index_of */
138 void __iomem *pmecc_alpha_to;
139 void __iomem *pmecc_index_of;
140
141 /* data for pmecc computation */
142 int16_t *pmecc_partial_syn;
143 int16_t *pmecc_si;
144 int16_t *pmecc_smu; /* Sigma table */
145 int16_t *pmecc_lmu; /* polynomal order */
146 int *pmecc_mu;
147 int *pmecc_dmu;
148 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200149};
150
Josh Wu1c7b8742012-06-29 17:47:55 +0800151static struct nand_ecclayout atmel_pmecc_oobinfo;
152
Andrew Victor42cb1402006-10-19 18:24:35 +0200153/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900154 * Enable NAND.
155 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200156static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900157{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800158 if (gpio_is_valid(host->board.enable_pin))
159 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900160}
161
162/*
163 * Disable NAND.
164 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200165static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900166{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800167 if (gpio_is_valid(host->board.enable_pin))
168 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900169}
170
171/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200172 * Hardware specific access to control-lines
173 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200174static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200175{
176 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200177 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200178
Atsushi Nemoto81365082008-04-27 01:51:12 +0900179 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900180 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200181 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900182 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200183 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900184 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200185 if (cmd == NAND_CMD_NONE)
186 return;
187
188 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800189 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200190 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800191 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200192}
193
194/*
195 * Read the Device Ready pin.
196 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200197static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200198{
199 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200200 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200201
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800202 return gpio_get_value(host->board.rdy_pin) ^
203 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200204}
205
Josh Wu7dc37de2013-08-05 19:14:35 +0800206/* Set up for hardware ready pin and enable pin. */
207static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
208{
209 struct nand_chip *chip = mtd->priv;
210 struct atmel_nand_host *host = chip->priv;
211 int res = 0;
212
213 if (gpio_is_valid(host->board.rdy_pin)) {
214 res = devm_gpio_request(host->dev,
215 host->board.rdy_pin, "nand_rdy");
216 if (res < 0) {
217 dev_err(host->dev,
218 "can't request rdy gpio %d\n",
219 host->board.rdy_pin);
220 return res;
221 }
222
223 res = gpio_direction_input(host->board.rdy_pin);
224 if (res < 0) {
225 dev_err(host->dev,
226 "can't request input direction rdy gpio %d\n",
227 host->board.rdy_pin);
228 return res;
229 }
230
231 chip->dev_ready = atmel_nand_device_ready;
232 }
233
234 if (gpio_is_valid(host->board.enable_pin)) {
235 res = devm_gpio_request(host->dev,
236 host->board.enable_pin, "nand_enable");
237 if (res < 0) {
238 dev_err(host->dev,
239 "can't request enable gpio %d\n",
240 host->board.enable_pin);
241 return res;
242 }
243
244 res = gpio_direction_output(host->board.enable_pin, 1);
245 if (res < 0) {
246 dev_err(host->dev,
247 "can't request output direction enable gpio %d\n",
248 host->board.enable_pin);
249 return res;
250 }
251 }
252
253 return res;
254}
255
Josh Wu1ae9c092013-08-05 19:14:36 +0800256static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size)
257{
258 int i;
259 u32 *t = trg;
260 const __iomem u32 *s = src;
261
262 for (i = 0; i < (size >> 2); i++)
263 *t++ = readl_relaxed(s++);
264}
265
Josh Wu6054d4d2013-08-05 19:14:37 +0800266static void memcpy32_toio(void __iomem *trg, const void *src, int size)
267{
268 int i;
269 u32 __iomem *t = trg;
270 const u32 *s = src;
271
272 for (i = 0; i < (size >> 2); i++)
273 writel_relaxed(*s++, t++);
274}
275
Artem Bityutskiy50082312012-02-02 13:54:25 +0200276/*
277 * Minimal-overhead PIO for data access.
278 */
279static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
280{
281 struct nand_chip *nand_chip = mtd->priv;
Josh Wu1ae9c092013-08-05 19:14:36 +0800282 struct atmel_nand_host *host = nand_chip->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200283
Josh Wu1ae9c092013-08-05 19:14:36 +0800284 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
285 memcpy32_fromio(buf, host->nfc->data_in_sram, len);
286 host->nfc->data_in_sram += len;
287 } else {
288 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
289 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200290}
291
292static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
293{
294 struct nand_chip *nand_chip = mtd->priv;
Josh Wu1ae9c092013-08-05 19:14:36 +0800295 struct atmel_nand_host *host = nand_chip->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200296
Josh Wu1ae9c092013-08-05 19:14:36 +0800297 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
298 memcpy32_fromio(buf, host->nfc->data_in_sram, len);
299 host->nfc->data_in_sram += len;
300 } else {
301 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
302 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200303}
304
305static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
306{
307 struct nand_chip *nand_chip = mtd->priv;
308
309 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
310}
311
312static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
313{
314 struct nand_chip *nand_chip = mtd->priv;
315
316 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
317}
318
Hong Xucbc6c5e2011-01-18 14:36:05 +0800319static void dma_complete_func(void *completion)
320{
321 complete(completion);
322}
323
Josh Wu1ae9c092013-08-05 19:14:36 +0800324static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
325{
326 /* NFC only has two banks. Must be 0 or 1 */
327 if (bank > 1)
328 return -EINVAL;
329
330 if (bank) {
331 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
332 if (host->mtd.writesize > 2048)
333 return -EINVAL;
334 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
335 } else {
336 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
337 }
338
339 return 0;
340}
341
342static uint nfc_get_sram_off(struct atmel_nand_host *host)
343{
344 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
345 return NFC_SRAM_BANK1_OFFSET;
346 else
347 return 0;
348}
349
350static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
351{
352 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
353 return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
354 else
355 return host->nfc->sram_bank0_phys;
356}
357
Hong Xucbc6c5e2011-01-18 14:36:05 +0800358static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
359 int is_read)
360{
361 struct dma_device *dma_dev;
362 enum dma_ctrl_flags flags;
363 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
364 struct dma_async_tx_descriptor *tx = NULL;
365 dma_cookie_t cookie;
366 struct nand_chip *chip = mtd->priv;
367 struct atmel_nand_host *host = chip->priv;
368 void *p = buf;
369 int err = -EIO;
370 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Josh Wu1ae9c092013-08-05 19:14:36 +0800371 struct atmel_nfc *nfc = host->nfc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800372
Hong Xu80b4f812011-03-31 18:33:15 +0800373 if (buf >= high_memory)
374 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800375
376 dma_dev = host->dma_chan->device;
377
378 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
379 DMA_COMPL_SKIP_DEST_UNMAP;
380
381 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
382 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
383 dev_err(host->dev, "Failed to dma_map_single\n");
384 goto err_buf;
385 }
386
387 if (is_read) {
Josh Wu1ae9c092013-08-05 19:14:36 +0800388 if (nfc && nfc->data_in_sram)
389 dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
390 - (nfc->sram_bank0 + nfc_get_sram_off(host)));
391 else
392 dma_src_addr = host->io_phys;
393
Hong Xucbc6c5e2011-01-18 14:36:05 +0800394 dma_dst_addr = phys_addr;
395 } else {
396 dma_src_addr = phys_addr;
Josh Wu6054d4d2013-08-05 19:14:37 +0800397
398 if (nfc && nfc->write_by_sram)
399 dma_dst_addr = nfc_sram_phys(host);
400 else
401 dma_dst_addr = host->io_phys;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800402 }
403
404 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
405 dma_src_addr, len, flags);
406 if (!tx) {
407 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
408 goto err_dma;
409 }
410
411 init_completion(&host->comp);
412 tx->callback = dma_complete_func;
413 tx->callback_param = &host->comp;
414
415 cookie = tx->tx_submit(tx);
416 if (dma_submit_error(cookie)) {
417 dev_err(host->dev, "Failed to do DMA tx_submit\n");
418 goto err_dma;
419 }
420
421 dma_async_issue_pending(host->dma_chan);
422 wait_for_completion(&host->comp);
423
Josh Wu1ae9c092013-08-05 19:14:36 +0800424 if (is_read && nfc && nfc->data_in_sram)
425 /* After read data from SRAM, need to increase the position */
426 nfc->data_in_sram += len;
427
Hong Xucbc6c5e2011-01-18 14:36:05 +0800428 err = 0;
429
430err_dma:
431 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
432err_buf:
433 if (err != 0)
434 dev_warn(host->dev, "Fall back to CPU I/O\n");
435 return err;
436}
437
438static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
439{
440 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200441 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800442
Nicolas Ferre9d515672011-04-01 16:40:44 +0200443 if (use_dma && len > mtd->oobsize)
444 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800445 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
446 return;
447
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800448 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200449 atmel_read_buf16(mtd, buf, len);
450 else
451 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800452}
453
454static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
455{
456 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200457 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800458
Nicolas Ferre9d515672011-04-01 16:40:44 +0200459 if (use_dma && len > mtd->oobsize)
460 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800461 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
462 return;
463
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800464 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200465 atmel_write_buf16(mtd, buf, len);
466 else
467 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800468}
469
David Brownell23a346c2008-07-03 23:40:16 -0700470/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800471 * Return number of ecc bytes per sector according to sector size and
472 * correction capability
473 *
474 * Following table shows what at91 PMECC supported:
475 * Correction Capability Sector_512_bytes Sector_1024_bytes
476 * ===================== ================ =================
477 * 2-bits 4-bytes 4-bytes
478 * 4-bits 7-bytes 7-bytes
479 * 8-bits 13-bytes 14-bytes
480 * 12-bits 20-bytes 21-bytes
481 * 24-bits 39-bytes 42-bytes
482 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500483static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800484{
485 int m = 12 + sector_size / 512;
486 return (m * cap + 7) / 8;
487}
488
Bill Pemberton06f25512012-11-19 13:23:07 -0500489static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800490 int oobsize, int ecc_len)
Josh Wu1c7b8742012-06-29 17:47:55 +0800491{
492 int i;
493
494 layout->eccbytes = ecc_len;
495
496 /* ECC will occupy the last ecc_len bytes continuously */
497 for (i = 0; i < ecc_len; i++)
498 layout->eccpos[i] = oobsize - ecc_len + i;
499
500 layout->oobfree[0].offset = 2;
501 layout->oobfree[0].length =
502 oobsize - ecc_len - layout->oobfree[0].offset;
503}
504
Bill Pemberton06f25512012-11-19 13:23:07 -0500505static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800506{
507 int table_size;
508
509 table_size = host->pmecc_sector_size == 512 ?
510 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
511
512 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
513 table_size * sizeof(int16_t);
514}
515
Bill Pemberton06f25512012-11-19 13:23:07 -0500516static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800517{
518 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800519 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800520
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800521 size = (2 * cap + 1) * sizeof(int16_t);
522 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
523 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
524 host->pmecc_lmu = devm_kzalloc(host->dev,
525 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
526 host->pmecc_smu = devm_kzalloc(host->dev,
527 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800528
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800529 size = (cap + 1) * sizeof(int);
530 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
531 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
532 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800533
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800534 if (!host->pmecc_partial_syn ||
535 !host->pmecc_si ||
536 !host->pmecc_lmu ||
537 !host->pmecc_smu ||
538 !host->pmecc_mu ||
539 !host->pmecc_dmu ||
540 !host->pmecc_delta)
541 return -ENOMEM;
542
543 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800544}
545
546static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
547{
548 struct nand_chip *nand_chip = mtd->priv;
549 struct atmel_nand_host *host = nand_chip->priv;
550 int i;
551 uint32_t value;
552
553 /* Fill odd syndromes */
554 for (i = 0; i < host->pmecc_corr_cap; i++) {
555 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
556 if (i & 1)
557 value >>= 16;
558 value &= 0xffff;
559 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
560 }
561}
562
563static void pmecc_substitute(struct mtd_info *mtd)
564{
565 struct nand_chip *nand_chip = mtd->priv;
566 struct atmel_nand_host *host = nand_chip->priv;
567 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
568 int16_t __iomem *index_of = host->pmecc_index_of;
569 int16_t *partial_syn = host->pmecc_partial_syn;
570 const int cap = host->pmecc_corr_cap;
571 int16_t *si;
572 int i, j;
573
574 /* si[] is a table that holds the current syndrome value,
575 * an element of that table belongs to the field
576 */
577 si = host->pmecc_si;
578
579 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
580
581 /* Computation 2t syndromes based on S(x) */
582 /* Odd syndromes */
583 for (i = 1; i < 2 * cap; i += 2) {
584 for (j = 0; j < host->pmecc_degree; j++) {
585 if (partial_syn[i] & ((unsigned short)0x1 << j))
586 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
587 }
588 }
589 /* Even syndrome = (Odd syndrome) ** 2 */
590 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
591 if (si[j] == 0) {
592 si[i] = 0;
593 } else {
594 int16_t tmp;
595
596 tmp = readw_relaxed(index_of + si[j]);
597 tmp = (tmp * 2) % host->pmecc_cw_len;
598 si[i] = readw_relaxed(alpha_to + tmp);
599 }
600 }
601
602 return;
603}
604
605static void pmecc_get_sigma(struct mtd_info *mtd)
606{
607 struct nand_chip *nand_chip = mtd->priv;
608 struct atmel_nand_host *host = nand_chip->priv;
609
610 int16_t *lmu = host->pmecc_lmu;
611 int16_t *si = host->pmecc_si;
612 int *mu = host->pmecc_mu;
613 int *dmu = host->pmecc_dmu; /* Discrepancy */
614 int *delta = host->pmecc_delta; /* Delta order */
615 int cw_len = host->pmecc_cw_len;
616 const int16_t cap = host->pmecc_corr_cap;
617 const int num = 2 * cap + 1;
618 int16_t __iomem *index_of = host->pmecc_index_of;
619 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
620 int i, j, k;
621 uint32_t dmu_0_count, tmp;
622 int16_t *smu = host->pmecc_smu;
623
624 /* index of largest delta */
625 int ro;
626 int largest;
627 int diff;
628
629 dmu_0_count = 0;
630
631 /* First Row */
632
633 /* Mu */
634 mu[0] = -1;
635
636 memset(smu, 0, sizeof(int16_t) * num);
637 smu[0] = 1;
638
639 /* discrepancy set to 1 */
640 dmu[0] = 1;
641 /* polynom order set to 0 */
642 lmu[0] = 0;
643 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
644
645 /* Second Row */
646
647 /* Mu */
648 mu[1] = 0;
649 /* Sigma(x) set to 1 */
650 memset(&smu[num], 0, sizeof(int16_t) * num);
651 smu[num] = 1;
652
653 /* discrepancy set to S1 */
654 dmu[1] = si[1];
655
656 /* polynom order set to 0 */
657 lmu[1] = 0;
658
659 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
660
661 /* Init the Sigma(x) last row */
662 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
663
664 for (i = 1; i <= cap; i++) {
665 mu[i + 1] = i << 1;
666 /* Begin Computing Sigma (Mu+1) and L(mu) */
667 /* check if discrepancy is set to 0 */
668 if (dmu[i] == 0) {
669 dmu_0_count++;
670
671 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
672 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
673 tmp += 2;
674 else
675 tmp += 1;
676
677 if (dmu_0_count == tmp) {
678 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
679 smu[(cap + 1) * num + j] =
680 smu[i * num + j];
681
682 lmu[cap + 1] = lmu[i];
683 return;
684 }
685
686 /* copy polynom */
687 for (j = 0; j <= lmu[i] >> 1; j++)
688 smu[(i + 1) * num + j] = smu[i * num + j];
689
690 /* copy previous polynom order to the next */
691 lmu[i + 1] = lmu[i];
692 } else {
693 ro = 0;
694 largest = -1;
695 /* find largest delta with dmu != 0 */
696 for (j = 0; j < i; j++) {
697 if ((dmu[j]) && (delta[j] > largest)) {
698 largest = delta[j];
699 ro = j;
700 }
701 }
702
703 /* compute difference */
704 diff = (mu[i] - mu[ro]);
705
706 /* Compute degree of the new smu polynomial */
707 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
708 lmu[i + 1] = lmu[i];
709 else
710 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
711
712 /* Init smu[i+1] with 0 */
713 for (k = 0; k < num; k++)
714 smu[(i + 1) * num + k] = 0;
715
716 /* Compute smu[i+1] */
717 for (k = 0; k <= lmu[ro] >> 1; k++) {
718 int16_t a, b, c;
719
720 if (!(smu[ro * num + k] && dmu[i]))
721 continue;
722 a = readw_relaxed(index_of + dmu[i]);
723 b = readw_relaxed(index_of + dmu[ro]);
724 c = readw_relaxed(index_of + smu[ro * num + k]);
725 tmp = a + (cw_len - b) + c;
726 a = readw_relaxed(alpha_to + tmp % cw_len);
727 smu[(i + 1) * num + (k + diff)] = a;
728 }
729
730 for (k = 0; k <= lmu[i] >> 1; k++)
731 smu[(i + 1) * num + k] ^= smu[i * num + k];
732 }
733
734 /* End Computing Sigma (Mu+1) and L(mu) */
735 /* In either case compute delta */
736 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
737
738 /* Do not compute discrepancy for the last iteration */
739 if (i >= cap)
740 continue;
741
742 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
743 tmp = 2 * (i - 1);
744 if (k == 0) {
745 dmu[i + 1] = si[tmp + 3];
746 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
747 int16_t a, b, c;
748 a = readw_relaxed(index_of +
749 smu[(i + 1) * num + k]);
750 b = si[2 * (i - 1) + 3 - k];
751 c = readw_relaxed(index_of + b);
752 tmp = a + c;
753 tmp %= cw_len;
754 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
755 dmu[i + 1];
756 }
757 }
758 }
759
760 return;
761}
762
763static int pmecc_err_location(struct mtd_info *mtd)
764{
765 struct nand_chip *nand_chip = mtd->priv;
766 struct atmel_nand_host *host = nand_chip->priv;
767 unsigned long end_time;
768 const int cap = host->pmecc_corr_cap;
769 const int num = 2 * cap + 1;
770 int sector_size = host->pmecc_sector_size;
771 int err_nbr = 0; /* number of error */
772 int roots_nbr; /* number of roots */
773 int i;
774 uint32_t val;
775 int16_t *smu = host->pmecc_smu;
776
777 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
778
779 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
780 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
781 smu[(cap + 1) * num + i]);
782 err_nbr++;
783 }
784
785 val = (err_nbr - 1) << 16;
786 if (sector_size == 1024)
787 val |= 1;
788
789 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
790 pmerrloc_writel(host->pmerrloc_base, ELEN,
791 sector_size * 8 + host->pmecc_degree * cap);
792
793 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
794 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
795 & PMERRLOC_CALC_DONE)) {
796 if (unlikely(time_after(jiffies, end_time))) {
797 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
798 return -1;
799 }
800 cpu_relax();
801 }
802
803 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
804 & PMERRLOC_ERR_NUM_MASK) >> 8;
805 /* Number of roots == degree of smu hence <= cap */
806 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
807 return err_nbr - 1;
808
809 /* Number of roots does not match the degree of smu
810 * unable to correct error */
811 return -1;
812}
813
814static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
815 int sector_num, int extra_bytes, int err_nbr)
816{
817 struct nand_chip *nand_chip = mtd->priv;
818 struct atmel_nand_host *host = nand_chip->priv;
819 int i = 0;
820 int byte_pos, bit_pos, sector_size, pos;
821 uint32_t tmp;
822 uint8_t err_byte;
823
824 sector_size = host->pmecc_sector_size;
825
826 while (err_nbr) {
827 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
828 byte_pos = tmp / 8;
829 bit_pos = tmp % 8;
830
831 if (byte_pos >= (sector_size + extra_bytes))
832 BUG(); /* should never happen */
833
834 if (byte_pos < sector_size) {
835 err_byte = *(buf + byte_pos);
836 *(buf + byte_pos) ^= (1 << bit_pos);
837
838 pos = sector_num * host->pmecc_sector_size + byte_pos;
839 dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
840 pos, bit_pos, err_byte, *(buf + byte_pos));
841 } else {
842 /* Bit flip in OOB area */
843 tmp = sector_num * host->pmecc_bytes_per_sector
844 + (byte_pos - sector_size);
845 err_byte = ecc[tmp];
846 ecc[tmp] ^= (1 << bit_pos);
847
848 pos = tmp + nand_chip->ecc.layout->eccpos[0];
849 dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
850 pos, bit_pos, err_byte, ecc[tmp]);
851 }
852
853 i++;
854 err_nbr--;
855 }
856
857 return;
858}
859
860static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
861 u8 *ecc)
862{
863 struct nand_chip *nand_chip = mtd->priv;
864 struct atmel_nand_host *host = nand_chip->priv;
865 int i, err_nbr, eccbytes;
866 uint8_t *buf_pos;
Josh Wuc0c70d92012-11-27 18:50:31 +0800867 int total_err = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800868
869 eccbytes = nand_chip->ecc.bytes;
870 for (i = 0; i < eccbytes; i++)
871 if (ecc[i] != 0xff)
872 goto normal_check;
873 /* Erased page, return OK */
874 return 0;
875
876normal_check:
877 for (i = 0; i < host->pmecc_sector_number; i++) {
878 err_nbr = 0;
879 if (pmecc_stat & 0x1) {
880 buf_pos = buf + i * host->pmecc_sector_size;
881
882 pmecc_gen_syndrome(mtd, i);
883 pmecc_substitute(mtd);
884 pmecc_get_sigma(mtd);
885
886 err_nbr = pmecc_err_location(mtd);
887 if (err_nbr == -1) {
888 dev_err(host->dev, "PMECC: Too many errors\n");
889 mtd->ecc_stats.failed++;
890 return -EIO;
891 } else {
892 pmecc_correct_data(mtd, buf_pos, ecc, i,
893 host->pmecc_bytes_per_sector, err_nbr);
894 mtd->ecc_stats.corrected += err_nbr;
Josh Wuc0c70d92012-11-27 18:50:31 +0800895 total_err += err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800896 }
897 }
898 pmecc_stat >>= 1;
899 }
900
Josh Wuc0c70d92012-11-27 18:50:31 +0800901 return total_err;
Josh Wu1c7b8742012-06-29 17:47:55 +0800902}
903
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800904static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
905{
906 u32 val;
907
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800908 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
909 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
910 return;
911 }
912
Josh Wu1fad0e82013-08-07 17:58:11 +0800913 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
914 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
915 val = pmecc_readl_relaxed(host->ecc, CFG);
916
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800917 if (ecc_op == NAND_ECC_READ)
918 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
919 | PMECC_CFG_AUTO_ENABLE);
920 else
921 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
922 & ~PMECC_CFG_AUTO_ENABLE);
923
924 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
925 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
926}
927
Josh Wu1c7b8742012-06-29 17:47:55 +0800928static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
929 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
930{
931 struct atmel_nand_host *host = chip->priv;
932 int eccsize = chip->ecc.size;
933 uint8_t *oob = chip->oob_poi;
934 uint32_t *eccpos = chip->ecc.layout->eccpos;
935 uint32_t stat;
936 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800937 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800938
Josh Wu1ae9c092013-08-05 19:14:36 +0800939 if (!host->nfc || !host->nfc->use_nfc_sram)
940 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800941
942 chip->read_buf(mtd, buf, eccsize);
943 chip->read_buf(mtd, oob, mtd->oobsize);
944
945 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
946 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
947 if (unlikely(time_after(jiffies, end_time))) {
948 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
949 return -EIO;
950 }
951 cpu_relax();
952 }
953
954 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800955 if (stat != 0) {
956 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
957 if (bitflips < 0)
958 /* uncorrectable errors */
959 return 0;
960 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800961
Josh Wuc0c70d92012-11-27 18:50:31 +0800962 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800963}
964
965static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
966 struct nand_chip *chip, const uint8_t *buf, int oob_required)
967{
968 struct atmel_nand_host *host = chip->priv;
969 uint32_t *eccpos = chip->ecc.layout->eccpos;
970 int i, j;
971 unsigned long end_time;
972
Josh Wu6054d4d2013-08-05 19:14:37 +0800973 if (!host->nfc || !host->nfc->write_by_sram) {
974 pmecc_enable(host, NAND_ECC_WRITE);
975 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
976 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800977
978 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
979 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
980 if (unlikely(time_after(jiffies, end_time))) {
981 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
982 return -EIO;
983 }
984 cpu_relax();
985 }
986
987 for (i = 0; i < host->pmecc_sector_number; i++) {
988 for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
989 int pos;
990
991 pos = i * host->pmecc_bytes_per_sector + j;
992 chip->oob_poi[eccpos[pos]] =
993 pmecc_readb_ecc_relaxed(host->ecc, i, j);
994 }
995 }
996 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
997
998 return 0;
999}
1000
1001static void atmel_pmecc_core_init(struct mtd_info *mtd)
1002{
1003 struct nand_chip *nand_chip = mtd->priv;
1004 struct atmel_nand_host *host = nand_chip->priv;
1005 uint32_t val = 0;
1006 struct nand_ecclayout *ecc_layout;
1007
1008 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
1009 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1010
1011 switch (host->pmecc_corr_cap) {
1012 case 2:
1013 val = PMECC_CFG_BCH_ERR2;
1014 break;
1015 case 4:
1016 val = PMECC_CFG_BCH_ERR4;
1017 break;
1018 case 8:
1019 val = PMECC_CFG_BCH_ERR8;
1020 break;
1021 case 12:
1022 val = PMECC_CFG_BCH_ERR12;
1023 break;
1024 case 24:
1025 val = PMECC_CFG_BCH_ERR24;
1026 break;
1027 }
1028
1029 if (host->pmecc_sector_size == 512)
1030 val |= PMECC_CFG_SECTOR512;
1031 else if (host->pmecc_sector_size == 1024)
1032 val |= PMECC_CFG_SECTOR1024;
1033
1034 switch (host->pmecc_sector_number) {
1035 case 1:
1036 val |= PMECC_CFG_PAGE_1SECTOR;
1037 break;
1038 case 2:
1039 val |= PMECC_CFG_PAGE_2SECTORS;
1040 break;
1041 case 4:
1042 val |= PMECC_CFG_PAGE_4SECTORS;
1043 break;
1044 case 8:
1045 val |= PMECC_CFG_PAGE_8SECTORS;
1046 break;
1047 }
1048
1049 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
1050 | PMECC_CFG_AUTO_DISABLE);
1051 pmecc_writel(host->ecc, CFG, val);
1052
1053 ecc_layout = nand_chip->ecc.layout;
1054 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
1055 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
1056 pmecc_writel(host->ecc, EADDR,
1057 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
1058 /* See datasheet about PMECC Clock Control Register */
1059 pmecc_writel(host->ecc, CLK, 2);
1060 pmecc_writel(host->ecc, IDR, 0xff);
1061 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
1062}
1063
Josh Wu84cfbbb2013-01-23 20:47:12 +08001064/*
Josh Wu2a3d9332013-09-18 13:58:48 +08001065 * Get minimum ecc requirements from NAND.
Josh Wu84cfbbb2013-01-23 20:47:12 +08001066 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
Josh Wu2a3d9332013-09-18 13:58:48 +08001067 * will set them according to minimum ecc requirement. Otherwise, use the
Josh Wu84cfbbb2013-01-23 20:47:12 +08001068 * value in DTS file.
1069 * return 0 if success. otherwise return error code.
1070 */
1071static int pmecc_choose_ecc(struct atmel_nand_host *host,
1072 int *cap, int *sector_size)
1073{
Josh Wu2a3d9332013-09-18 13:58:48 +08001074 /* Get minimum ECC requirements */
1075 if (host->nand_chip.ecc_strength_ds) {
1076 *cap = host->nand_chip.ecc_strength_ds;
1077 *sector_size = host->nand_chip.ecc_step_ds;
1078 dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n",
Josh Wu84cfbbb2013-01-23 20:47:12 +08001079 *cap, *sector_size);
Josh Wu84cfbbb2013-01-23 20:47:12 +08001080 } else {
Josh Wu84cfbbb2013-01-23 20:47:12 +08001081 *cap = 2;
1082 *sector_size = 512;
Josh Wu2a3d9332013-09-18 13:58:48 +08001083 dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
Josh Wu84cfbbb2013-01-23 20:47:12 +08001084 }
1085
Josh Wu2a3d9332013-09-18 13:58:48 +08001086 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
Josh Wu84cfbbb2013-01-23 20:47:12 +08001087 if (host->pmecc_corr_cap == 0) {
1088 /* use the most fitable ecc bits (the near bigger one ) */
1089 if (*cap <= 2)
1090 host->pmecc_corr_cap = 2;
1091 else if (*cap <= 4)
1092 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +08001093 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001094 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +08001095 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001096 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +08001097 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001098 host->pmecc_corr_cap = 24;
1099 else
1100 return -EINVAL;
1101 }
1102 if (host->pmecc_sector_size == 0) {
1103 /* use the most fitable sector size (the near smaller one ) */
1104 if (*sector_size >= 1024)
1105 host->pmecc_sector_size = 1024;
1106 else if (*sector_size >= 512)
1107 host->pmecc_sector_size = 512;
1108 else
1109 return -EINVAL;
1110 }
1111 return 0;
1112}
1113
Josh Wu1c7b8742012-06-29 17:47:55 +08001114static int __init atmel_pmecc_nand_init_params(struct platform_device *pdev,
1115 struct atmel_nand_host *host)
1116{
1117 struct mtd_info *mtd = &host->mtd;
1118 struct nand_chip *nand_chip = &host->nand_chip;
1119 struct resource *regs, *regs_pmerr, *regs_rom;
1120 int cap, sector_size, err_no;
1121
Josh Wu84cfbbb2013-01-23 20:47:12 +08001122 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
1123 if (err_no) {
1124 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1125 return err_no;
1126 }
1127
Richard Genoudf666d642013-07-30 17:17:29 +02001128 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001129 sector_size != host->pmecc_sector_size)
1130 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001131
Josh Wu1c7b8742012-06-29 17:47:55 +08001132 cap = host->pmecc_corr_cap;
1133 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001134 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1135 host->pmecc_lookup_table_offset_512 :
1136 host->pmecc_lookup_table_offset_1024;
1137
Josh Wu1c7b8742012-06-29 17:47:55 +08001138 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1139 cap, sector_size);
1140
1141 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1142 if (!regs) {
1143 dev_warn(host->dev,
1144 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1145 nand_chip->ecc.mode = NAND_ECC_SOFT;
1146 return 0;
1147 }
1148
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001149 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1150 if (IS_ERR(host->ecc)) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001151 dev_err(host->dev, "ioremap failed\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001152 err_no = PTR_ERR(host->ecc);
1153 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001154 }
1155
1156 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001157 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1158 if (IS_ERR(host->pmerrloc_base)) {
1159 dev_err(host->dev,
1160 "Can not get I/O resource for PMECC ERRLOC controller!\n");
1161 err_no = PTR_ERR(host->pmerrloc_base);
1162 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001163 }
1164
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001165 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1166 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, regs_rom);
1167 if (IS_ERR(host->pmecc_rom_base)) {
1168 dev_err(host->dev, "Can not get I/O resource for ROM!\n");
1169 err_no = PTR_ERR(host->pmecc_rom_base);
1170 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001171 }
1172
1173 /* ECC is calculated for the whole page (1 step) */
1174 nand_chip->ecc.size = mtd->writesize;
1175
1176 /* set ECC page size and oob layout */
1177 switch (mtd->writesize) {
1178 case 2048:
Josh Wu2fa831f2013-08-19 18:05:44 +08001179 host->pmecc_degree = (sector_size == 512) ?
1180 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Josh Wu1c7b8742012-06-29 17:47:55 +08001181 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
1182 host->pmecc_sector_number = mtd->writesize / sector_size;
1183 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
1184 cap, sector_size);
1185 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1186 host->pmecc_index_of = host->pmecc_rom_base +
1187 host->pmecc_lookup_table_offset;
1188
1189 nand_chip->ecc.steps = 1;
1190 nand_chip->ecc.strength = cap;
1191 nand_chip->ecc.bytes = host->pmecc_bytes_per_sector *
1192 host->pmecc_sector_number;
1193 if (nand_chip->ecc.bytes > mtd->oobsize - 2) {
1194 dev_err(host->dev, "No room for ECC bytes\n");
1195 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001196 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001197 }
1198 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1199 mtd->oobsize,
1200 nand_chip->ecc.bytes);
1201 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1202 break;
1203 case 512:
1204 case 1024:
1205 case 4096:
1206 /* TODO */
1207 dev_warn(host->dev,
1208 "Unsupported page size for PMECC, use Software ECC\n");
1209 default:
1210 /* page size not handled by HW ECC */
1211 /* switching back to soft ECC */
1212 nand_chip->ecc.mode = NAND_ECC_SOFT;
1213 return 0;
1214 }
1215
1216 /* Allocate data for PMECC computation */
1217 err_no = pmecc_data_alloc(host);
1218 if (err_no) {
1219 dev_err(host->dev,
1220 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001221 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001222 }
1223
1224 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1225 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1226
1227 atmel_pmecc_core_init(mtd);
1228
1229 return 0;
1230
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001231err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001232 return err_no;
1233}
1234
1235/*
Richard Genoud77f54922008-04-23 19:51:14 +02001236 * Calculate HW ECC
1237 *
1238 * function called after a write
1239 *
1240 * mtd: MTD block structure
1241 * dat: raw data (unused)
1242 * ecc_code: buffer for ECC
1243 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001244static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001245 const u_char *dat, unsigned char *ecc_code)
1246{
1247 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001248 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001249 unsigned int ecc_value;
1250
1251 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001252 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001253
Richard Genoud3fc23892008-10-12 08:42:28 +02001254 ecc_code[0] = ecc_value & 0xFF;
1255 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001256
1257 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001258 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001259
Richard Genoud3fc23892008-10-12 08:42:28 +02001260 ecc_code[2] = ecc_value & 0xFF;
1261 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001262
1263 return 0;
1264}
1265
1266/*
1267 * HW ECC read page function
1268 *
1269 * mtd: mtd info structure
1270 * chip: nand chip info structure
1271 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001272 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001273 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001274static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1275 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001276{
1277 int eccsize = chip->ecc.size;
1278 int eccbytes = chip->ecc.bytes;
1279 uint32_t *eccpos = chip->ecc.layout->eccpos;
1280 uint8_t *p = buf;
1281 uint8_t *oob = chip->oob_poi;
1282 uint8_t *ecc_pos;
1283 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001284 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001285
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001286 /*
1287 * Errata: ALE is incorrectly wired up to the ECC controller
1288 * on the AP7000, so it will include the address cycles in the
1289 * ECC calculation.
1290 *
1291 * Workaround: Reset the parity registers before reading the
1292 * actual data.
1293 */
Josh Wu71b94e22013-05-09 15:34:54 +08001294 struct atmel_nand_host *host = chip->priv;
1295 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001296 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001297
Richard Genoud77f54922008-04-23 19:51:14 +02001298 /* read the page */
1299 chip->read_buf(mtd, p, eccsize);
1300
1301 /* move to ECC position if needed */
1302 if (eccpos[0] != 0) {
1303 /* This only works on large pages
1304 * because the ECC controller waits for
1305 * NAND_CMD_RNDOUTSTART after the
1306 * NAND_CMD_RNDOUT.
1307 * anyway, for small pages, the eccpos[0] == 0
1308 */
1309 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1310 mtd->writesize + eccpos[0], -1);
1311 }
1312
1313 /* the ECC controller needs to read the ECC just after the data */
1314 ecc_pos = oob + eccpos[0];
1315 chip->read_buf(mtd, ecc_pos, eccbytes);
1316
1317 /* check if there's an error */
1318 stat = chip->ecc.correct(mtd, p, oob, NULL);
1319
Mike Dunn3f91e942012-04-25 12:06:09 -07001320 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001321 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001322 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001323 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001324 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1325 }
Richard Genoud77f54922008-04-23 19:51:14 +02001326
1327 /* get back to oob start (end of page) */
1328 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1329
1330 /* read the oob */
1331 chip->read_buf(mtd, oob, mtd->oobsize);
1332
Mike Dunn3f91e942012-04-25 12:06:09 -07001333 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001334}
1335
1336/*
1337 * HW ECC Correction
1338 *
1339 * function called after a read
1340 *
1341 * mtd: MTD block structure
1342 * dat: raw data read from the chip
1343 * read_ecc: ECC from the chip (unused)
1344 * isnull: unused
1345 *
1346 * Detect and correct a 1 bit error for a page
1347 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001348static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001349 u_char *read_ecc, u_char *isnull)
1350{
1351 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001352 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001353 unsigned int ecc_status;
1354 unsigned int ecc_word, ecc_bit;
1355
1356 /* get the status from the Status Register */
1357 ecc_status = ecc_readl(host->ecc, SR);
1358
1359 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001360 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001361 return 0;
1362
1363 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001364 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001365 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001366 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001367 ecc_word >>= 4;
1368
1369 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001370 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001371 /* check if it is a freshly erased block
1372 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001373 if ((ecc_bit == ATMEL_ECC_BITADDR)
1374 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001375 /* the block has just been erased, return OK */
1376 return 0;
1377 }
1378 /* it doesn't seems to be a freshly
1379 * erased block.
1380 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001381 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001382 " Unable to correct.\n");
1383 return -EIO;
1384 }
1385
1386 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001387 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001388 /* there's nothing much to do here.
1389 * the bit error is on the ECC itself.
1390 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001391 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001392 " Nothing to correct\n");
1393 return 0;
1394 }
1395
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001396 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001397 " (word offset in the page :"
1398 " 0x%x bit offset : 0x%x)\n",
1399 ecc_word, ecc_bit);
1400 /* correct the error */
1401 if (nand_chip->options & NAND_BUSWIDTH_16) {
1402 /* 16 bits words */
1403 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1404 } else {
1405 /* 8 bits words */
1406 dat[ecc_word] ^= (1 << ecc_bit);
1407 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001408 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001409 return 1;
1410}
1411
1412/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001413 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001414 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001415static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1416{
Josh Wu71b94e22013-05-09 15:34:54 +08001417 struct nand_chip *nand_chip = mtd->priv;
1418 struct atmel_nand_host *host = nand_chip->priv;
1419
1420 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001421 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001422}
Richard Genoud77f54922008-04-23 19:51:14 +02001423
Bill Pemberton06f25512012-11-19 13:23:07 -05001424static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001425 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001426{
Josh Wuc0cf7872013-01-23 20:47:08 +08001427 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001428 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001429 int ecc_mode;
1430 struct atmel_nand_data *board = &host->board;
Josh Wue9d8da82013-09-18 11:31:19 +08001431 enum of_gpio_flags flags = 0;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001432
1433 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1434 if (val >= 32) {
1435 dev_err(host->dev, "invalid addr-offset %u\n", val);
1436 return -EINVAL;
1437 }
1438 board->ale = val;
1439 }
1440
1441 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1442 if (val >= 32) {
1443 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1444 return -EINVAL;
1445 }
1446 board->cle = val;
1447 }
1448
1449 ecc_mode = of_get_nand_ecc_mode(np);
1450
1451 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1452
1453 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1454
Josh Wu1b719262013-05-09 15:34:55 +08001455 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1456
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001457 if (of_get_nand_bus_width(np) == 16)
1458 board->bus_width_16 = 1;
1459
1460 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1461 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1462
1463 board->enable_pin = of_get_gpio(np, 1);
1464 board->det_pin = of_get_gpio(np, 2);
1465
Josh Wua41b51a2012-06-29 17:47:54 +08001466 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1467
Josh Wu7dc37de2013-08-05 19:14:35 +08001468 /* load the nfc driver if there is */
1469 of_platform_populate(np, NULL, NULL, host->dev);
1470
Josh Wua41b51a2012-06-29 17:47:54 +08001471 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1472 return 0; /* Not using PMECC */
1473
1474 /* use PMECC, get correction capability, sector size and lookup
1475 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001476 * If correction bits and sector size are not specified, then find
1477 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001478 */
Josh Wue66b4312013-01-23 20:47:11 +08001479 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1480 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1481 (val != 24)) {
1482 dev_err(host->dev,
1483 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1484 val);
1485 return -EINVAL;
1486 }
1487 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001488 }
Josh Wua41b51a2012-06-29 17:47:54 +08001489
Josh Wue66b4312013-01-23 20:47:11 +08001490 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1491 if ((val != 512) && (val != 1024)) {
1492 dev_err(host->dev,
1493 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1494 val);
1495 return -EINVAL;
1496 }
1497 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001498 }
Josh Wua41b51a2012-06-29 17:47:54 +08001499
1500 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1501 offset, 2) != 0) {
1502 dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
1503 return -EINVAL;
1504 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001505 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001506 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1507 return -EINVAL;
1508 }
Josh Wue66b4312013-01-23 20:47:11 +08001509 host->pmecc_lookup_table_offset_512 = offset[0];
1510 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001511
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001512 return 0;
1513}
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001514
Josh Wu3dfe41a2012-06-25 18:07:43 +08001515static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
1516 struct atmel_nand_host *host)
1517{
1518 struct mtd_info *mtd = &host->mtd;
1519 struct nand_chip *nand_chip = &host->nand_chip;
1520 struct resource *regs;
1521
1522 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1523 if (!regs) {
1524 dev_err(host->dev,
1525 "Can't get I/O resource regs, use software ECC\n");
1526 nand_chip->ecc.mode = NAND_ECC_SOFT;
1527 return 0;
1528 }
1529
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001530 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1531 if (IS_ERR(host->ecc)) {
Josh Wu3dfe41a2012-06-25 18:07:43 +08001532 dev_err(host->dev, "ioremap failed\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001533 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001534 }
1535
1536 /* ECC is calculated for the whole page (1 step) */
1537 nand_chip->ecc.size = mtd->writesize;
1538
1539 /* set ECC page size and oob layout */
1540 switch (mtd->writesize) {
1541 case 512:
1542 nand_chip->ecc.layout = &atmel_oobinfo_small;
1543 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1544 break;
1545 case 1024:
1546 nand_chip->ecc.layout = &atmel_oobinfo_large;
1547 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1548 break;
1549 case 2048:
1550 nand_chip->ecc.layout = &atmel_oobinfo_large;
1551 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1552 break;
1553 case 4096:
1554 nand_chip->ecc.layout = &atmel_oobinfo_large;
1555 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1556 break;
1557 default:
1558 /* page size not handled by HW ECC */
1559 /* switching back to soft ECC */
1560 nand_chip->ecc.mode = NAND_ECC_SOFT;
1561 return 0;
1562 }
1563
1564 /* set up for HW ECC */
1565 nand_chip->ecc.calculate = atmel_nand_calculate;
1566 nand_chip->ecc.correct = atmel_nand_correct;
1567 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1568 nand_chip->ecc.read_page = atmel_nand_read_page;
1569 nand_chip->ecc.bytes = 4;
1570 nand_chip->ecc.strength = 1;
1571
1572 return 0;
1573}
1574
Josh Wu7dc37de2013-08-05 19:14:35 +08001575/* SMC interrupt service routine */
1576static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
1577{
1578 struct atmel_nand_host *host = dev_id;
1579 u32 status, mask, pending;
1580 irqreturn_t ret = IRQ_HANDLED;
1581
1582 status = nfc_readl(host->nfc->hsmc_regs, SR);
1583 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1584 pending = status & mask;
1585
1586 if (pending & NFC_SR_XFR_DONE) {
1587 complete(&host->nfc->comp_nfc);
1588 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
1589 } else if (pending & NFC_SR_RB_EDGE) {
1590 complete(&host->nfc->comp_nfc);
1591 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_RB_EDGE);
1592 } else if (pending & NFC_SR_CMD_DONE) {
1593 complete(&host->nfc->comp_nfc);
1594 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
1595 } else {
1596 ret = IRQ_NONE;
1597 }
1598
1599 return ret;
1600}
1601
1602/* NFC(Nand Flash Controller) related functions */
1603static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
1604{
1605 unsigned long timeout;
1606 init_completion(&host->nfc->comp_nfc);
1607
1608 /* Enable interrupt that need to wait for */
1609 nfc_writel(host->nfc->hsmc_regs, IER, flag);
1610
1611 timeout = wait_for_completion_timeout(&host->nfc->comp_nfc,
1612 msecs_to_jiffies(NFC_TIME_OUT_MS));
1613 if (timeout)
1614 return 0;
1615
1616 /* Time out to wait for the interrupt */
1617 dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
1618 return -ETIMEDOUT;
1619}
1620
1621static int nfc_send_command(struct atmel_nand_host *host,
1622 unsigned int cmd, unsigned int addr, unsigned char cycle0)
1623{
1624 unsigned long timeout;
1625 dev_dbg(host->dev,
1626 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1627 cmd, addr, cycle0);
1628
1629 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1630 while (nfc_cmd_readl(NFCADDR_CMD_NFCBUSY, host->nfc->base_cmd_regs)
1631 & NFCADDR_CMD_NFCBUSY) {
1632 if (time_after(jiffies, timeout)) {
1633 dev_err(host->dev,
1634 "Time out to wait CMD_NFCBUSY ready!\n");
1635 return -ETIMEDOUT;
1636 }
1637 }
1638 nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
1639 nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
1640 return nfc_wait_interrupt(host, NFC_SR_CMD_DONE);
1641}
1642
1643static int nfc_device_ready(struct mtd_info *mtd)
1644{
1645 struct nand_chip *nand_chip = mtd->priv;
1646 struct atmel_nand_host *host = nand_chip->priv;
1647 if (!nfc_wait_interrupt(host, NFC_SR_RB_EDGE))
1648 return 1;
1649 return 0;
1650}
1651
1652static void nfc_select_chip(struct mtd_info *mtd, int chip)
1653{
1654 struct nand_chip *nand_chip = mtd->priv;
1655 struct atmel_nand_host *host = nand_chip->priv;
1656
1657 if (chip == -1)
1658 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
1659 else
1660 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
1661}
1662
1663static int nfc_make_addr(struct mtd_info *mtd, int column, int page_addr,
1664 unsigned int *addr1234, unsigned int *cycle0)
1665{
1666 struct nand_chip *chip = mtd->priv;
1667
1668 int acycle = 0;
1669 unsigned char addr_bytes[8];
1670 int index = 0, bit_shift;
1671
1672 BUG_ON(addr1234 == NULL || cycle0 == NULL);
1673
1674 *cycle0 = 0;
1675 *addr1234 = 0;
1676
1677 if (column != -1) {
1678 if (chip->options & NAND_BUSWIDTH_16)
1679 column >>= 1;
1680 addr_bytes[acycle++] = column & 0xff;
1681 if (mtd->writesize > 512)
1682 addr_bytes[acycle++] = (column >> 8) & 0xff;
1683 }
1684
1685 if (page_addr != -1) {
1686 addr_bytes[acycle++] = page_addr & 0xff;
1687 addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
1688 if (chip->chipsize > (128 << 20))
1689 addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
1690 }
1691
1692 if (acycle > 4)
1693 *cycle0 = addr_bytes[index++];
1694
1695 for (bit_shift = 0; index < acycle; bit_shift += 8)
1696 *addr1234 += addr_bytes[index++] << bit_shift;
1697
1698 /* return acycle in cmd register */
1699 return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
1700}
1701
1702static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
1703 int column, int page_addr)
1704{
1705 struct nand_chip *chip = mtd->priv;
1706 struct atmel_nand_host *host = chip->priv;
1707 unsigned long timeout;
1708 unsigned int nfc_addr_cmd = 0;
1709
1710 unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1711
1712 /* Set default settings: no cmd2, no addr cycle. read from nand */
1713 unsigned int cmd2 = 0;
1714 unsigned int vcmd2 = 0;
1715 int acycle = NFCADDR_CMD_ACYCLE_NONE;
1716 int csid = NFCADDR_CMD_CSID_3;
1717 int dataen = NFCADDR_CMD_DATADIS;
1718 int nfcwr = NFCADDR_CMD_NFCRD;
1719 unsigned int addr1234 = 0;
1720 unsigned int cycle0 = 0;
1721 bool do_addr = true;
Josh Wu1ae9c092013-08-05 19:14:36 +08001722 host->nfc->data_in_sram = NULL;
Josh Wu7dc37de2013-08-05 19:14:35 +08001723
1724 dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1725 __func__, command, column, page_addr);
1726
1727 switch (command) {
1728 case NAND_CMD_RESET:
1729 nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
1730 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1731 udelay(chip->chip_delay);
1732
1733 nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
1734 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1735 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
1736 if (time_after(jiffies, timeout)) {
1737 dev_err(host->dev,
1738 "Time out to wait status ready!\n");
1739 break;
1740 }
1741 }
1742 return;
1743 case NAND_CMD_STATUS:
1744 do_addr = false;
1745 break;
1746 case NAND_CMD_PARAM:
1747 case NAND_CMD_READID:
1748 do_addr = false;
1749 acycle = NFCADDR_CMD_ACYCLE_1;
1750 if (column != -1)
1751 addr1234 = column;
1752 break;
1753 case NAND_CMD_RNDOUT:
1754 cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
1755 vcmd2 = NFCADDR_CMD_VCMD2;
1756 break;
1757 case NAND_CMD_READ0:
1758 case NAND_CMD_READOOB:
1759 if (command == NAND_CMD_READOOB) {
1760 column += mtd->writesize;
1761 command = NAND_CMD_READ0; /* only READ0 is valid */
1762 cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1763 }
Josh Wu1ae9c092013-08-05 19:14:36 +08001764 if (host->nfc->use_nfc_sram) {
1765 /* Enable Data transfer to sram */
1766 dataen = NFCADDR_CMD_DATAEN;
1767
1768 /* Need enable PMECC now, since NFC will transfer
1769 * data in bus after sending nfc read command.
1770 */
1771 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1772 pmecc_enable(host, NAND_ECC_READ);
1773 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001774
1775 cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
1776 vcmd2 = NFCADDR_CMD_VCMD2;
1777 break;
1778 /* For prgramming command, the cmd need set to write enable */
1779 case NAND_CMD_PAGEPROG:
1780 case NAND_CMD_SEQIN:
1781 case NAND_CMD_RNDIN:
1782 nfcwr = NFCADDR_CMD_NFCWR;
Josh Wu6054d4d2013-08-05 19:14:37 +08001783 if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
1784 dataen = NFCADDR_CMD_DATAEN;
Josh Wu7dc37de2013-08-05 19:14:35 +08001785 break;
1786 default:
1787 break;
1788 }
1789
1790 if (do_addr)
1791 acycle = nfc_make_addr(mtd, column, page_addr, &addr1234,
1792 &cycle0);
1793
1794 nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
1795 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1796
Josh Wu1ae9c092013-08-05 19:14:36 +08001797 if (dataen == NFCADDR_CMD_DATAEN)
1798 if (nfc_wait_interrupt(host, NFC_SR_XFR_DONE))
1799 dev_err(host->dev, "something wrong, No XFR_DONE interrupt comes.\n");
1800
Josh Wu7dc37de2013-08-05 19:14:35 +08001801 /*
1802 * Program and erase have their own busy handlers status, sequential
1803 * in, and deplete1 need no delay.
1804 */
1805 switch (command) {
1806 case NAND_CMD_CACHEDPROG:
1807 case NAND_CMD_PAGEPROG:
1808 case NAND_CMD_ERASE1:
1809 case NAND_CMD_ERASE2:
1810 case NAND_CMD_RNDIN:
1811 case NAND_CMD_STATUS:
1812 case NAND_CMD_RNDOUT:
1813 case NAND_CMD_SEQIN:
1814 case NAND_CMD_READID:
1815 return;
1816
1817 case NAND_CMD_READ0:
Josh Wu1ae9c092013-08-05 19:14:36 +08001818 if (dataen == NFCADDR_CMD_DATAEN) {
1819 host->nfc->data_in_sram = host->nfc->sram_bank0 +
1820 nfc_get_sram_off(host);
1821 return;
1822 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001823 /* fall through */
1824 default:
1825 nfc_wait_interrupt(host, NFC_SR_RB_EDGE);
1826 }
1827}
1828
Josh Wu6054d4d2013-08-05 19:14:37 +08001829static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1830 uint32_t offset, int data_len, const uint8_t *buf,
1831 int oob_required, int page, int cached, int raw)
1832{
1833 int cfg, len;
1834 int status = 0;
1835 struct atmel_nand_host *host = chip->priv;
1836 void __iomem *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
1837
1838 /* Subpage write is not supported */
1839 if (offset || (data_len < mtd->writesize))
1840 return -EINVAL;
1841
1842 cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
1843 len = mtd->writesize;
1844
1845 if (unlikely(raw)) {
1846 len += mtd->oobsize;
1847 nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
1848 } else
1849 nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
1850
1851 /* Copy page data to sram that will write to nand via NFC */
1852 if (use_dma) {
1853 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
1854 /* Fall back to use cpu copy */
1855 memcpy32_toio(sram, buf, len);
1856 } else {
1857 memcpy32_toio(sram, buf, len);
1858 }
1859
1860 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1861 /*
1862 * When use NFC sram, need set up PMECC before send
1863 * NAND_CMD_SEQIN command. Since when the nand command
1864 * is sent, nfc will do transfer from sram and nand.
1865 */
1866 pmecc_enable(host, NAND_ECC_WRITE);
1867
1868 host->nfc->will_write_sram = true;
1869 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1870 host->nfc->will_write_sram = false;
1871
1872 if (likely(!raw))
1873 /* Need to write ecc into oob */
1874 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
1875
1876 if (status < 0)
1877 return status;
1878
1879 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1880 status = chip->waitfunc(mtd, chip);
1881
1882 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1883 status = chip->errstat(mtd, chip, FL_WRITING, status, page);
1884
1885 if (status & NAND_STATUS_FAIL)
1886 return -EIO;
1887
1888 return 0;
1889}
1890
Josh Wu1ae9c092013-08-05 19:14:36 +08001891static int nfc_sram_init(struct mtd_info *mtd)
1892{
1893 struct nand_chip *chip = mtd->priv;
1894 struct atmel_nand_host *host = chip->priv;
1895 int res = 0;
1896
1897 /* Initialize the NFC CFG register */
1898 unsigned int cfg_nfc = 0;
1899
1900 /* set page size and oob layout */
1901 switch (mtd->writesize) {
1902 case 512:
1903 cfg_nfc = NFC_CFG_PAGESIZE_512;
1904 break;
1905 case 1024:
1906 cfg_nfc = NFC_CFG_PAGESIZE_1024;
1907 break;
1908 case 2048:
1909 cfg_nfc = NFC_CFG_PAGESIZE_2048;
1910 break;
1911 case 4096:
1912 cfg_nfc = NFC_CFG_PAGESIZE_4096;
1913 break;
1914 case 8192:
1915 cfg_nfc = NFC_CFG_PAGESIZE_8192;
1916 break;
1917 default:
1918 dev_err(host->dev, "Unsupported page size for NFC.\n");
1919 res = -ENXIO;
1920 return res;
1921 }
1922
1923 /* oob bytes size = (NFCSPARESIZE + 1) * 4
1924 * Max support spare size is 512 bytes. */
1925 cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
1926 & NFC_CFG_NFC_SPARESIZE);
1927 /* default set a max timeout */
1928 cfg_nfc |= NFC_CFG_RSPARE |
1929 NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
1930
1931 nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
1932
Josh Wu6054d4d2013-08-05 19:14:37 +08001933 host->nfc->will_write_sram = false;
Josh Wu1ae9c092013-08-05 19:14:36 +08001934 nfc_set_sram_bank(host, 0);
1935
Josh Wu6054d4d2013-08-05 19:14:37 +08001936 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
1937 if (host->nfc->write_by_sram) {
1938 if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
1939 chip->ecc.mode == NAND_ECC_NONE)
1940 chip->write_page = nfc_sram_write_page;
1941 else
1942 host->nfc->write_by_sram = false;
1943 }
Josh Wu1ae9c092013-08-05 19:14:36 +08001944
Josh Wu6054d4d2013-08-05 19:14:37 +08001945 dev_info(host->dev, "Using NFC Sram read %s\n",
1946 host->nfc->write_by_sram ? "and write" : "");
Josh Wu1ae9c092013-08-05 19:14:36 +08001947 return 0;
1948}
1949
Josh Wu7dc37de2013-08-05 19:14:35 +08001950static struct platform_driver atmel_nand_nfc_driver;
Andrew Victor42cb1402006-10-19 18:24:35 +02001951/*
1952 * Probe for the NAND device.
1953 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001954static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02001955{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001956 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02001957 struct mtd_info *mtd;
1958 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001959 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001960 struct mtd_part_parser_data ppdata = {};
Josh Wu7dc37de2013-08-05 19:14:35 +08001961 int res, irq;
Andrew Victor42cb1402006-10-19 18:24:35 +02001962
1963 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001964 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +02001965 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001966 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +02001967 return -ENOMEM;
1968 }
1969
Josh Wu7dc37de2013-08-05 19:14:35 +08001970 res = platform_driver_register(&atmel_nand_nfc_driver);
1971 if (res)
1972 dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
1973
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001974 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1975 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
1976 if (IS_ERR(host->io_base)) {
1977 dev_err(&pdev->dev, "atmel_nand: ioremap resource failed\n");
1978 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001979 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02001980 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001981 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02001982
1983 mtd = &host->mtd;
1984 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001985 host->dev = &pdev->dev;
Josh Wue9d8da82013-09-18 11:31:19 +08001986 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
1987 /* Only when CONFIG_OF is enabled of_node can be parsed */
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001988 res = atmel_of_init_port(host, pdev->dev.of_node);
1989 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001990 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001991 } else {
Jingoo Han453810b2013-07-30 17:18:33 +09001992 memcpy(&host->board, dev_get_platdata(&pdev->dev),
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001993 sizeof(struct atmel_nand_data));
1994 }
Andrew Victor42cb1402006-10-19 18:24:35 +02001995
1996 nand_chip->priv = host; /* link the private data structures */
1997 mtd->priv = nand_chip;
1998 mtd->owner = THIS_MODULE;
1999
2000 /* Set address of NAND IO lines */
2001 nand_chip->IO_ADDR_R = host->io_base;
2002 nand_chip->IO_ADDR_W = host->io_base;
Ivan Kutena4265f82007-05-24 14:35:58 +03002003
Josh Wu7dc37de2013-08-05 19:14:35 +08002004 if (nand_nfc.is_initialized) {
2005 /* NFC driver is probed and initialized */
2006 host->nfc = &nand_nfc;
2007
2008 nand_chip->select_chip = nfc_select_chip;
2009 nand_chip->dev_ready = nfc_device_ready;
2010 nand_chip->cmdfunc = nfc_nand_command;
2011
2012 /* Initialize the interrupt for NFC */
2013 irq = platform_get_irq(pdev, 0);
2014 if (irq < 0) {
2015 dev_err(host->dev, "Cannot get HSMC irq!\n");
Wei Yongjunff52c672013-08-23 10:50:36 +08002016 res = irq;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002017 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002018 }
2019
Josh Wu7dc37de2013-08-05 19:14:35 +08002020 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
2021 0, "hsmc", host);
2022 if (res) {
2023 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
2024 irq);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002025 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002026 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002027 } else {
2028 res = atmel_nand_set_enable_ready_pins(mtd);
2029 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002030 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002031
Josh Wu7dc37de2013-08-05 19:14:35 +08002032 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002033 }
Ivan Kutena4265f82007-05-24 14:35:58 +03002034
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002035 nand_chip->ecc.mode = host->board.ecc_mode;
Andrew Victor42cb1402006-10-19 18:24:35 +02002036 nand_chip->chip_delay = 20; /* 20us command delay time */
2037
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002038 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02002039 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08002040
2041 nand_chip->read_buf = atmel_read_buf;
2042 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02002043
Andrew Victor42cb1402006-10-19 18:24:35 +02002044 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002045 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002046
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002047 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002048 res = devm_gpio_request(&pdev->dev,
2049 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002050 if (res < 0) {
2051 dev_err(&pdev->dev,
2052 "can't request det gpio %d\n",
2053 host->board.det_pin);
2054 goto err_no_card;
2055 }
2056
2057 res = gpio_direction_input(host->board.det_pin);
2058 if (res < 0) {
2059 dev_err(&pdev->dev,
2060 "can't request input direction det gpio %d\n",
2061 host->board.det_pin);
2062 goto err_no_card;
2063 }
2064
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002065 if (gpio_get_value(host->board.det_pin)) {
Simon Polettef4fa6972009-05-27 18:19:39 +03002066 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01002067 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002068 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02002069 }
2070 }
2071
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002072 if (host->board.on_flash_bbt || on_flash_bbt) {
Simon Polettef4fa6972009-05-27 18:19:39 +03002073 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -07002074 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa6972009-05-27 18:19:39 +03002075 }
2076
Josh Wu1b719262013-05-09 15:34:55 +08002077 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08002078 use_dma = 0;
2079
2080 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08002081 dma_cap_mask_t mask;
2082
2083 dma_cap_zero(mask);
2084 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02002085 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002086 if (!host->dma_chan) {
2087 dev_err(host->dev, "Failed to request DMA channel\n");
2088 use_dma = 0;
2089 }
2090 }
2091 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08002092 dev_info(host->dev, "Using %s for DMA transfers.\n",
2093 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08002094 else
2095 dev_info(host->dev, "No DMA support for NAND access.\n");
2096
Richard Genoud77f54922008-04-23 19:51:14 +02002097 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00002098 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02002099 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002100 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02002101 }
2102
Richard Genoud3fc23892008-10-12 08:42:28 +02002103 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08002104 if (host->has_pmecc)
2105 res = atmel_pmecc_nand_init_params(pdev, host);
2106 else
2107 res = atmel_hw_nand_init_params(pdev, host);
2108
Josh Wu3dfe41a2012-06-25 18:07:43 +08002109 if (res != 0)
2110 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02002111 }
2112
Josh Wu1ae9c092013-08-05 19:14:36 +08002113 /* initialize the nfc configuration register */
2114 if (host->nfc && host->nfc->use_nfc_sram) {
2115 res = nfc_sram_init(mtd);
2116 if (res) {
2117 host->nfc->use_nfc_sram = false;
2118 dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
2119 }
2120 }
2121
Richard Genoud77f54922008-04-23 19:51:14 +02002122 /* second phase scan */
2123 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02002124 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002125 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02002126 }
2127
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002128 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002129 ppdata.of_node = pdev->dev.of_node;
2130 res = mtd_device_parse_register(mtd, NULL, &ppdata,
2131 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02002132 if (!res)
2133 return res;
2134
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002135err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002136 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08002137 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08002138err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002139err_scan_ident:
2140err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002141 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002142 if (host->dma_chan)
2143 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002144err_nand_ioremap:
Josh Wu7dc37de2013-08-05 19:14:35 +08002145 platform_driver_unregister(&atmel_nand_nfc_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02002146 return res;
2147}
2148
2149/*
2150 * Remove a NAND device.
2151 */
David Brownell23a346c2008-07-03 23:40:16 -07002152static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002153{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002154 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +02002155 struct mtd_info *mtd = &host->mtd;
2156
2157 nand_release(mtd);
2158
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002159 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002160
Josh Wu1c7b8742012-06-29 17:47:55 +08002161 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
2162 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2163 pmerrloc_writel(host->pmerrloc_base, ELDIS,
2164 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08002165 }
2166
Hong Xucbc6c5e2011-01-18 14:36:05 +08002167 if (host->dma_chan)
2168 dma_release_channel(host->dma_chan);
2169
Josh Wu7dc37de2013-08-05 19:14:35 +08002170 platform_driver_unregister(&atmel_nand_nfc_driver);
2171
Andrew Victor42cb1402006-10-19 18:24:35 +02002172 return 0;
2173}
2174
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002175static const struct of_device_id atmel_nand_dt_ids[] = {
2176 { .compatible = "atmel,at91rm9200-nand" },
2177 { /* sentinel */ }
2178};
2179
2180MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002181
Josh Wu7dc37de2013-08-05 19:14:35 +08002182static int atmel_nand_nfc_probe(struct platform_device *pdev)
2183{
2184 struct atmel_nfc *nfc = &nand_nfc;
2185 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
2186
2187 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2188 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
2189 if (IS_ERR(nfc->base_cmd_regs))
2190 return PTR_ERR(nfc->base_cmd_regs);
2191
2192 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2193 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
2194 if (IS_ERR(nfc->hsmc_regs))
2195 return PTR_ERR(nfc->hsmc_regs);
2196
2197 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2198 if (nfc_sram) {
2199 nfc->sram_bank0 = devm_ioremap_resource(&pdev->dev, nfc_sram);
Josh Wu1ae9c092013-08-05 19:14:36 +08002200 if (IS_ERR(nfc->sram_bank0)) {
Josh Wu7dc37de2013-08-05 19:14:35 +08002201 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2202 PTR_ERR(nfc->sram_bank0));
Josh Wu1ae9c092013-08-05 19:14:36 +08002203 } else {
2204 nfc->use_nfc_sram = true;
Josh Wu7dc37de2013-08-05 19:14:35 +08002205 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
Josh Wu6054d4d2013-08-05 19:14:37 +08002206
2207 if (pdev->dev.of_node)
2208 nfc->write_by_sram = of_property_read_bool(
2209 pdev->dev.of_node,
2210 "atmel,write-by-sram");
Josh Wu1ae9c092013-08-05 19:14:36 +08002211 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002212 }
2213
2214 nfc->is_initialized = true;
2215 dev_info(&pdev->dev, "NFC is probed.\n");
2216 return 0;
2217}
2218
Josh Wu81f29b42013-09-18 11:31:20 +08002219static const struct of_device_id atmel_nand_nfc_match[] = {
Josh Wu7dc37de2013-08-05 19:14:35 +08002220 { .compatible = "atmel,sama5d3-nfc" },
2221 { /* sentinel */ }
2222};
Josh Wu81f29b42013-09-18 11:31:20 +08002223MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
Josh Wu7dc37de2013-08-05 19:14:35 +08002224
2225static struct platform_driver atmel_nand_nfc_driver = {
2226 .driver = {
2227 .name = "atmel_nand_nfc",
2228 .owner = THIS_MODULE,
2229 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2230 },
2231 .probe = atmel_nand_nfc_probe,
2232};
2233
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002234static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -07002235 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +02002236 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002237 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +02002238 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002239 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02002240 },
2241};
2242
Jingoo Hanc5345ed2013-03-05 13:30:04 +09002243module_platform_driver_probe(atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +02002244
2245MODULE_LICENSE("GPL");
2246MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02002247MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002248MODULE_ALIAS("platform:atmel_nand");