blob: 3d7db95b27c4e76b344f297816d6aac837ee77ee [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 *
Andrew Victor42cb1402006-10-19 18:24:35 +020021 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License version 2 as
23 * published by the Free Software Foundation.
24 *
25 */
26
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000027#include <linux/dma-mapping.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020028#include <linux/slab.h>
29#include <linux/module.h>
Simon Polettef4fa6972009-05-27 18:19:39 +030030#include <linux/moduleparam.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020031#include <linux/platform_device.h>
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080032#include <linux/of.h>
33#include <linux/of_device.h>
34#include <linux/of_gpio.h>
35#include <linux/of_mtd.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020036#include <linux/mtd/mtd.h>
37#include <linux/mtd/nand.h>
38#include <linux/mtd/partitions.h>
39
Hans-Christian Egtvedt5c39c4c2011-04-13 15:55:17 +020040#include <linux/dmaengine.h>
David Woodhouse90574d02008-06-07 08:49:00 +010041#include <linux/gpio.h>
42#include <linux/io.h>
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +080043#include <linux/platform_data/atmel.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020044
Hong Xucbc6c5e2011-01-18 14:36:05 +080045static int use_dma = 1;
46module_param(use_dma, int, 0);
47
Simon Polettef4fa6972009-05-27 18:19:39 +030048static int on_flash_bbt = 0;
49module_param(on_flash_bbt, int, 0);
50
Richard Genoud77f54922008-04-23 19:51:14 +020051/* Register access macros */
52#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020053 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020054#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020055 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020056
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020057#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Richard Genoud77f54922008-04-23 19:51:14 +020058
59/* oob layout for large page size
60 * bad block info is on bytes 0 and 1
61 * the bytes have to be consecutives to avoid
62 * several NAND_CMD_RNDOUT during read
63 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020064static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020065 .eccbytes = 4,
66 .eccpos = {60, 61, 62, 63},
67 .oobfree = {
68 {2, 58}
69 },
70};
71
72/* oob layout for small page size
73 * bad block info is on bytes 4 and 5
74 * the bytes have to be consecutives to avoid
75 * several NAND_CMD_RNDOUT during read
76 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020077static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020078 .eccbytes = 4,
79 .eccpos = {0, 1, 2, 3},
80 .oobfree = {
81 {6, 10}
82 },
83};
84
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020085struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +020086 struct nand_chip nand_chip;
87 struct mtd_info mtd;
88 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +080089 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080090 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +020091 struct device *dev;
92 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +080093
94 struct completion comp;
95 struct dma_chan *dma_chan;
Josh Wua41b51a2012-06-29 17:47:54 +080096
97 bool has_pmecc;
98 u8 pmecc_corr_cap;
99 u16 pmecc_sector_size;
100 u32 pmecc_lookup_table_offset;
Josh Wue66b4312013-01-23 20:47:11 +0800101 u32 pmecc_lookup_table_offset_512;
102 u32 pmecc_lookup_table_offset_1024;
Josh Wu1c7b8742012-06-29 17:47:55 +0800103
104 int pmecc_bytes_per_sector;
105 int pmecc_sector_number;
106 int pmecc_degree; /* Degree of remainders */
107 int pmecc_cw_len; /* Length of codeword */
108
109 void __iomem *pmerrloc_base;
110 void __iomem *pmecc_rom_base;
111
112 /* lookup table for alpha_to and index_of */
113 void __iomem *pmecc_alpha_to;
114 void __iomem *pmecc_index_of;
115
116 /* data for pmecc computation */
117 int16_t *pmecc_partial_syn;
118 int16_t *pmecc_si;
119 int16_t *pmecc_smu; /* Sigma table */
120 int16_t *pmecc_lmu; /* polynomal order */
121 int *pmecc_mu;
122 int *pmecc_dmu;
123 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200124};
125
Josh Wu1c7b8742012-06-29 17:47:55 +0800126static struct nand_ecclayout atmel_pmecc_oobinfo;
127
Andrew Victor42cb1402006-10-19 18:24:35 +0200128/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900129 * Enable NAND.
130 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200131static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900132{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800133 if (gpio_is_valid(host->board.enable_pin))
134 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900135}
136
137/*
138 * Disable NAND.
139 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200140static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900141{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800142 if (gpio_is_valid(host->board.enable_pin))
143 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900144}
145
146/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200147 * Hardware specific access to control-lines
148 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200149static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200150{
151 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200152 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200153
Atsushi Nemoto81365082008-04-27 01:51:12 +0900154 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900155 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200156 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900157 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200158 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900159 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200160 if (cmd == NAND_CMD_NONE)
161 return;
162
163 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800164 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200165 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800166 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200167}
168
169/*
170 * Read the Device Ready pin.
171 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200172static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200173{
174 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200175 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200176
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800177 return gpio_get_value(host->board.rdy_pin) ^
178 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200179}
180
Artem Bityutskiy50082312012-02-02 13:54:25 +0200181/*
182 * Minimal-overhead PIO for data access.
183 */
184static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
185{
186 struct nand_chip *nand_chip = mtd->priv;
187
188 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
189}
190
191static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
192{
193 struct nand_chip *nand_chip = mtd->priv;
194
195 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
196}
197
198static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
199{
200 struct nand_chip *nand_chip = mtd->priv;
201
202 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
203}
204
205static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
206{
207 struct nand_chip *nand_chip = mtd->priv;
208
209 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
210}
211
Hong Xucbc6c5e2011-01-18 14:36:05 +0800212static void dma_complete_func(void *completion)
213{
214 complete(completion);
215}
216
217static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
218 int is_read)
219{
220 struct dma_device *dma_dev;
221 enum dma_ctrl_flags flags;
222 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
223 struct dma_async_tx_descriptor *tx = NULL;
224 dma_cookie_t cookie;
225 struct nand_chip *chip = mtd->priv;
226 struct atmel_nand_host *host = chip->priv;
227 void *p = buf;
228 int err = -EIO;
229 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
230
Hong Xu80b4f812011-03-31 18:33:15 +0800231 if (buf >= high_memory)
232 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800233
234 dma_dev = host->dma_chan->device;
235
236 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
237 DMA_COMPL_SKIP_DEST_UNMAP;
238
239 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
240 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
241 dev_err(host->dev, "Failed to dma_map_single\n");
242 goto err_buf;
243 }
244
245 if (is_read) {
246 dma_src_addr = host->io_phys;
247 dma_dst_addr = phys_addr;
248 } else {
249 dma_src_addr = phys_addr;
250 dma_dst_addr = host->io_phys;
251 }
252
253 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
254 dma_src_addr, len, flags);
255 if (!tx) {
256 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
257 goto err_dma;
258 }
259
260 init_completion(&host->comp);
261 tx->callback = dma_complete_func;
262 tx->callback_param = &host->comp;
263
264 cookie = tx->tx_submit(tx);
265 if (dma_submit_error(cookie)) {
266 dev_err(host->dev, "Failed to do DMA tx_submit\n");
267 goto err_dma;
268 }
269
270 dma_async_issue_pending(host->dma_chan);
271 wait_for_completion(&host->comp);
272
273 err = 0;
274
275err_dma:
276 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
277err_buf:
278 if (err != 0)
279 dev_warn(host->dev, "Fall back to CPU I/O\n");
280 return err;
281}
282
283static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
284{
285 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200286 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800287
Nicolas Ferre9d515672011-04-01 16:40:44 +0200288 if (use_dma && len > mtd->oobsize)
289 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800290 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
291 return;
292
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800293 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200294 atmel_read_buf16(mtd, buf, len);
295 else
296 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800297}
298
299static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
300{
301 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200302 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800303
Nicolas Ferre9d515672011-04-01 16:40:44 +0200304 if (use_dma && len > mtd->oobsize)
305 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800306 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
307 return;
308
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800309 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200310 atmel_write_buf16(mtd, buf, len);
311 else
312 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800313}
314
David Brownell23a346c2008-07-03 23:40:16 -0700315/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800316 * Return number of ecc bytes per sector according to sector size and
317 * correction capability
318 *
319 * Following table shows what at91 PMECC supported:
320 * Correction Capability Sector_512_bytes Sector_1024_bytes
321 * ===================== ================ =================
322 * 2-bits 4-bytes 4-bytes
323 * 4-bits 7-bytes 7-bytes
324 * 8-bits 13-bytes 14-bytes
325 * 12-bits 20-bytes 21-bytes
326 * 24-bits 39-bytes 42-bytes
327 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500328static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800329{
330 int m = 12 + sector_size / 512;
331 return (m * cap + 7) / 8;
332}
333
Bill Pemberton06f25512012-11-19 13:23:07 -0500334static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800335 int oobsize, int ecc_len)
Josh Wu1c7b8742012-06-29 17:47:55 +0800336{
337 int i;
338
339 layout->eccbytes = ecc_len;
340
341 /* ECC will occupy the last ecc_len bytes continuously */
342 for (i = 0; i < ecc_len; i++)
343 layout->eccpos[i] = oobsize - ecc_len + i;
344
345 layout->oobfree[0].offset = 2;
346 layout->oobfree[0].length =
347 oobsize - ecc_len - layout->oobfree[0].offset;
348}
349
Bill Pemberton06f25512012-11-19 13:23:07 -0500350static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800351{
352 int table_size;
353
354 table_size = host->pmecc_sector_size == 512 ?
355 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
356
357 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
358 table_size * sizeof(int16_t);
359}
360
Bill Pemberton06f25512012-11-19 13:23:07 -0500361static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800362{
363 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800364 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800365
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800366 size = (2 * cap + 1) * sizeof(int16_t);
367 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
368 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
369 host->pmecc_lmu = devm_kzalloc(host->dev,
370 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
371 host->pmecc_smu = devm_kzalloc(host->dev,
372 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800373
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800374 size = (cap + 1) * sizeof(int);
375 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
376 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
377 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800378
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800379 if (!host->pmecc_partial_syn ||
380 !host->pmecc_si ||
381 !host->pmecc_lmu ||
382 !host->pmecc_smu ||
383 !host->pmecc_mu ||
384 !host->pmecc_dmu ||
385 !host->pmecc_delta)
386 return -ENOMEM;
387
388 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800389}
390
391static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
392{
393 struct nand_chip *nand_chip = mtd->priv;
394 struct atmel_nand_host *host = nand_chip->priv;
395 int i;
396 uint32_t value;
397
398 /* Fill odd syndromes */
399 for (i = 0; i < host->pmecc_corr_cap; i++) {
400 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
401 if (i & 1)
402 value >>= 16;
403 value &= 0xffff;
404 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
405 }
406}
407
408static void pmecc_substitute(struct mtd_info *mtd)
409{
410 struct nand_chip *nand_chip = mtd->priv;
411 struct atmel_nand_host *host = nand_chip->priv;
412 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
413 int16_t __iomem *index_of = host->pmecc_index_of;
414 int16_t *partial_syn = host->pmecc_partial_syn;
415 const int cap = host->pmecc_corr_cap;
416 int16_t *si;
417 int i, j;
418
419 /* si[] is a table that holds the current syndrome value,
420 * an element of that table belongs to the field
421 */
422 si = host->pmecc_si;
423
424 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
425
426 /* Computation 2t syndromes based on S(x) */
427 /* Odd syndromes */
428 for (i = 1; i < 2 * cap; i += 2) {
429 for (j = 0; j < host->pmecc_degree; j++) {
430 if (partial_syn[i] & ((unsigned short)0x1 << j))
431 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
432 }
433 }
434 /* Even syndrome = (Odd syndrome) ** 2 */
435 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
436 if (si[j] == 0) {
437 si[i] = 0;
438 } else {
439 int16_t tmp;
440
441 tmp = readw_relaxed(index_of + si[j]);
442 tmp = (tmp * 2) % host->pmecc_cw_len;
443 si[i] = readw_relaxed(alpha_to + tmp);
444 }
445 }
446
447 return;
448}
449
450static void pmecc_get_sigma(struct mtd_info *mtd)
451{
452 struct nand_chip *nand_chip = mtd->priv;
453 struct atmel_nand_host *host = nand_chip->priv;
454
455 int16_t *lmu = host->pmecc_lmu;
456 int16_t *si = host->pmecc_si;
457 int *mu = host->pmecc_mu;
458 int *dmu = host->pmecc_dmu; /* Discrepancy */
459 int *delta = host->pmecc_delta; /* Delta order */
460 int cw_len = host->pmecc_cw_len;
461 const int16_t cap = host->pmecc_corr_cap;
462 const int num = 2 * cap + 1;
463 int16_t __iomem *index_of = host->pmecc_index_of;
464 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
465 int i, j, k;
466 uint32_t dmu_0_count, tmp;
467 int16_t *smu = host->pmecc_smu;
468
469 /* index of largest delta */
470 int ro;
471 int largest;
472 int diff;
473
474 dmu_0_count = 0;
475
476 /* First Row */
477
478 /* Mu */
479 mu[0] = -1;
480
481 memset(smu, 0, sizeof(int16_t) * num);
482 smu[0] = 1;
483
484 /* discrepancy set to 1 */
485 dmu[0] = 1;
486 /* polynom order set to 0 */
487 lmu[0] = 0;
488 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
489
490 /* Second Row */
491
492 /* Mu */
493 mu[1] = 0;
494 /* Sigma(x) set to 1 */
495 memset(&smu[num], 0, sizeof(int16_t) * num);
496 smu[num] = 1;
497
498 /* discrepancy set to S1 */
499 dmu[1] = si[1];
500
501 /* polynom order set to 0 */
502 lmu[1] = 0;
503
504 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
505
506 /* Init the Sigma(x) last row */
507 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
508
509 for (i = 1; i <= cap; i++) {
510 mu[i + 1] = i << 1;
511 /* Begin Computing Sigma (Mu+1) and L(mu) */
512 /* check if discrepancy is set to 0 */
513 if (dmu[i] == 0) {
514 dmu_0_count++;
515
516 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
517 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
518 tmp += 2;
519 else
520 tmp += 1;
521
522 if (dmu_0_count == tmp) {
523 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
524 smu[(cap + 1) * num + j] =
525 smu[i * num + j];
526
527 lmu[cap + 1] = lmu[i];
528 return;
529 }
530
531 /* copy polynom */
532 for (j = 0; j <= lmu[i] >> 1; j++)
533 smu[(i + 1) * num + j] = smu[i * num + j];
534
535 /* copy previous polynom order to the next */
536 lmu[i + 1] = lmu[i];
537 } else {
538 ro = 0;
539 largest = -1;
540 /* find largest delta with dmu != 0 */
541 for (j = 0; j < i; j++) {
542 if ((dmu[j]) && (delta[j] > largest)) {
543 largest = delta[j];
544 ro = j;
545 }
546 }
547
548 /* compute difference */
549 diff = (mu[i] - mu[ro]);
550
551 /* Compute degree of the new smu polynomial */
552 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
553 lmu[i + 1] = lmu[i];
554 else
555 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
556
557 /* Init smu[i+1] with 0 */
558 for (k = 0; k < num; k++)
559 smu[(i + 1) * num + k] = 0;
560
561 /* Compute smu[i+1] */
562 for (k = 0; k <= lmu[ro] >> 1; k++) {
563 int16_t a, b, c;
564
565 if (!(smu[ro * num + k] && dmu[i]))
566 continue;
567 a = readw_relaxed(index_of + dmu[i]);
568 b = readw_relaxed(index_of + dmu[ro]);
569 c = readw_relaxed(index_of + smu[ro * num + k]);
570 tmp = a + (cw_len - b) + c;
571 a = readw_relaxed(alpha_to + tmp % cw_len);
572 smu[(i + 1) * num + (k + diff)] = a;
573 }
574
575 for (k = 0; k <= lmu[i] >> 1; k++)
576 smu[(i + 1) * num + k] ^= smu[i * num + k];
577 }
578
579 /* End Computing Sigma (Mu+1) and L(mu) */
580 /* In either case compute delta */
581 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
582
583 /* Do not compute discrepancy for the last iteration */
584 if (i >= cap)
585 continue;
586
587 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
588 tmp = 2 * (i - 1);
589 if (k == 0) {
590 dmu[i + 1] = si[tmp + 3];
591 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
592 int16_t a, b, c;
593 a = readw_relaxed(index_of +
594 smu[(i + 1) * num + k]);
595 b = si[2 * (i - 1) + 3 - k];
596 c = readw_relaxed(index_of + b);
597 tmp = a + c;
598 tmp %= cw_len;
599 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
600 dmu[i + 1];
601 }
602 }
603 }
604
605 return;
606}
607
608static int pmecc_err_location(struct mtd_info *mtd)
609{
610 struct nand_chip *nand_chip = mtd->priv;
611 struct atmel_nand_host *host = nand_chip->priv;
612 unsigned long end_time;
613 const int cap = host->pmecc_corr_cap;
614 const int num = 2 * cap + 1;
615 int sector_size = host->pmecc_sector_size;
616 int err_nbr = 0; /* number of error */
617 int roots_nbr; /* number of roots */
618 int i;
619 uint32_t val;
620 int16_t *smu = host->pmecc_smu;
621
622 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
623
624 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
625 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
626 smu[(cap + 1) * num + i]);
627 err_nbr++;
628 }
629
630 val = (err_nbr - 1) << 16;
631 if (sector_size == 1024)
632 val |= 1;
633
634 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
635 pmerrloc_writel(host->pmerrloc_base, ELEN,
636 sector_size * 8 + host->pmecc_degree * cap);
637
638 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
639 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
640 & PMERRLOC_CALC_DONE)) {
641 if (unlikely(time_after(jiffies, end_time))) {
642 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
643 return -1;
644 }
645 cpu_relax();
646 }
647
648 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
649 & PMERRLOC_ERR_NUM_MASK) >> 8;
650 /* Number of roots == degree of smu hence <= cap */
651 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
652 return err_nbr - 1;
653
654 /* Number of roots does not match the degree of smu
655 * unable to correct error */
656 return -1;
657}
658
659static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
660 int sector_num, int extra_bytes, int err_nbr)
661{
662 struct nand_chip *nand_chip = mtd->priv;
663 struct atmel_nand_host *host = nand_chip->priv;
664 int i = 0;
665 int byte_pos, bit_pos, sector_size, pos;
666 uint32_t tmp;
667 uint8_t err_byte;
668
669 sector_size = host->pmecc_sector_size;
670
671 while (err_nbr) {
672 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
673 byte_pos = tmp / 8;
674 bit_pos = tmp % 8;
675
676 if (byte_pos >= (sector_size + extra_bytes))
677 BUG(); /* should never happen */
678
679 if (byte_pos < sector_size) {
680 err_byte = *(buf + byte_pos);
681 *(buf + byte_pos) ^= (1 << bit_pos);
682
683 pos = sector_num * host->pmecc_sector_size + byte_pos;
684 dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
685 pos, bit_pos, err_byte, *(buf + byte_pos));
686 } else {
687 /* Bit flip in OOB area */
688 tmp = sector_num * host->pmecc_bytes_per_sector
689 + (byte_pos - sector_size);
690 err_byte = ecc[tmp];
691 ecc[tmp] ^= (1 << bit_pos);
692
693 pos = tmp + nand_chip->ecc.layout->eccpos[0];
694 dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
695 pos, bit_pos, err_byte, ecc[tmp]);
696 }
697
698 i++;
699 err_nbr--;
700 }
701
702 return;
703}
704
705static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
706 u8 *ecc)
707{
708 struct nand_chip *nand_chip = mtd->priv;
709 struct atmel_nand_host *host = nand_chip->priv;
710 int i, err_nbr, eccbytes;
711 uint8_t *buf_pos;
Josh Wuc0c70d92012-11-27 18:50:31 +0800712 int total_err = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800713
714 eccbytes = nand_chip->ecc.bytes;
715 for (i = 0; i < eccbytes; i++)
716 if (ecc[i] != 0xff)
717 goto normal_check;
718 /* Erased page, return OK */
719 return 0;
720
721normal_check:
722 for (i = 0; i < host->pmecc_sector_number; i++) {
723 err_nbr = 0;
724 if (pmecc_stat & 0x1) {
725 buf_pos = buf + i * host->pmecc_sector_size;
726
727 pmecc_gen_syndrome(mtd, i);
728 pmecc_substitute(mtd);
729 pmecc_get_sigma(mtd);
730
731 err_nbr = pmecc_err_location(mtd);
732 if (err_nbr == -1) {
733 dev_err(host->dev, "PMECC: Too many errors\n");
734 mtd->ecc_stats.failed++;
735 return -EIO;
736 } else {
737 pmecc_correct_data(mtd, buf_pos, ecc, i,
738 host->pmecc_bytes_per_sector, err_nbr);
739 mtd->ecc_stats.corrected += err_nbr;
Josh Wuc0c70d92012-11-27 18:50:31 +0800740 total_err += err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800741 }
742 }
743 pmecc_stat >>= 1;
744 }
745
Josh Wuc0c70d92012-11-27 18:50:31 +0800746 return total_err;
Josh Wu1c7b8742012-06-29 17:47:55 +0800747}
748
749static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
750 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
751{
752 struct atmel_nand_host *host = chip->priv;
753 int eccsize = chip->ecc.size;
754 uint8_t *oob = chip->oob_poi;
755 uint32_t *eccpos = chip->ecc.layout->eccpos;
756 uint32_t stat;
757 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800758 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800759
760 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
761 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
762 pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG)
763 & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE);
764
765 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
766 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
767
768 chip->read_buf(mtd, buf, eccsize);
769 chip->read_buf(mtd, oob, mtd->oobsize);
770
771 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
772 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
773 if (unlikely(time_after(jiffies, end_time))) {
774 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
775 return -EIO;
776 }
777 cpu_relax();
778 }
779
780 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800781 if (stat != 0) {
782 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
783 if (bitflips < 0)
784 /* uncorrectable errors */
785 return 0;
786 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800787
Josh Wuc0c70d92012-11-27 18:50:31 +0800788 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800789}
790
791static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
792 struct nand_chip *chip, const uint8_t *buf, int oob_required)
793{
794 struct atmel_nand_host *host = chip->priv;
795 uint32_t *eccpos = chip->ecc.layout->eccpos;
796 int i, j;
797 unsigned long end_time;
798
799 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
800 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
801
802 pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG) |
803 PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
804
805 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
806 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
807
808 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
809
810 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
811 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
812 if (unlikely(time_after(jiffies, end_time))) {
813 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
814 return -EIO;
815 }
816 cpu_relax();
817 }
818
819 for (i = 0; i < host->pmecc_sector_number; i++) {
820 for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
821 int pos;
822
823 pos = i * host->pmecc_bytes_per_sector + j;
824 chip->oob_poi[eccpos[pos]] =
825 pmecc_readb_ecc_relaxed(host->ecc, i, j);
826 }
827 }
828 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
829
830 return 0;
831}
832
833static void atmel_pmecc_core_init(struct mtd_info *mtd)
834{
835 struct nand_chip *nand_chip = mtd->priv;
836 struct atmel_nand_host *host = nand_chip->priv;
837 uint32_t val = 0;
838 struct nand_ecclayout *ecc_layout;
839
840 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
841 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
842
843 switch (host->pmecc_corr_cap) {
844 case 2:
845 val = PMECC_CFG_BCH_ERR2;
846 break;
847 case 4:
848 val = PMECC_CFG_BCH_ERR4;
849 break;
850 case 8:
851 val = PMECC_CFG_BCH_ERR8;
852 break;
853 case 12:
854 val = PMECC_CFG_BCH_ERR12;
855 break;
856 case 24:
857 val = PMECC_CFG_BCH_ERR24;
858 break;
859 }
860
861 if (host->pmecc_sector_size == 512)
862 val |= PMECC_CFG_SECTOR512;
863 else if (host->pmecc_sector_size == 1024)
864 val |= PMECC_CFG_SECTOR1024;
865
866 switch (host->pmecc_sector_number) {
867 case 1:
868 val |= PMECC_CFG_PAGE_1SECTOR;
869 break;
870 case 2:
871 val |= PMECC_CFG_PAGE_2SECTORS;
872 break;
873 case 4:
874 val |= PMECC_CFG_PAGE_4SECTORS;
875 break;
876 case 8:
877 val |= PMECC_CFG_PAGE_8SECTORS;
878 break;
879 }
880
881 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
882 | PMECC_CFG_AUTO_DISABLE);
883 pmecc_writel(host->ecc, CFG, val);
884
885 ecc_layout = nand_chip->ecc.layout;
886 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
887 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
888 pmecc_writel(host->ecc, EADDR,
889 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
890 /* See datasheet about PMECC Clock Control Register */
891 pmecc_writel(host->ecc, CLK, 2);
892 pmecc_writel(host->ecc, IDR, 0xff);
893 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
894}
895
Josh Wu84cfbbb2013-01-23 20:47:12 +0800896/*
897 * Get ECC requirement in ONFI parameters, returns -1 if ONFI
898 * parameters is not supported.
899 * return 0 if success to get the ECC requirement.
900 */
901static int get_onfi_ecc_param(struct nand_chip *chip,
902 int *ecc_bits, int *sector_size)
903{
904 *ecc_bits = *sector_size = 0;
905
906 if (chip->onfi_params.ecc_bits == 0xff)
907 /* TODO: the sector_size and ecc_bits need to be find in
908 * extended ecc parameter, currently we don't support it.
909 */
910 return -1;
911
912 *ecc_bits = chip->onfi_params.ecc_bits;
913
914 /* The default sector size (ecc codeword size) is 512 */
915 *sector_size = 512;
916
917 return 0;
918}
919
920/*
921 * Get ecc requirement from ONFI parameters ecc requirement.
922 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
923 * will set them according to ONFI ecc requirement. Otherwise, use the
924 * value in DTS file.
925 * return 0 if success. otherwise return error code.
926 */
927static int pmecc_choose_ecc(struct atmel_nand_host *host,
928 int *cap, int *sector_size)
929{
930 /* Get ECC requirement from ONFI parameters */
931 *cap = *sector_size = 0;
932 if (host->nand_chip.onfi_version) {
933 if (!get_onfi_ecc_param(&host->nand_chip, cap, sector_size))
934 dev_info(host->dev, "ONFI params, minimum required ECC: %d bits in %d bytes\n",
935 *cap, *sector_size);
936 else
937 dev_info(host->dev, "NAND chip ECC reqirement is in Extended ONFI parameter, we don't support yet.\n");
938 } else {
939 dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes");
940 }
941 if (*cap == 0 && *sector_size == 0) {
942 *cap = 2;
943 *sector_size = 512;
944 }
945
946 /* If dts file doesn't specify then use the one in ONFI parameters */
947 if (host->pmecc_corr_cap == 0) {
948 /* use the most fitable ecc bits (the near bigger one ) */
949 if (*cap <= 2)
950 host->pmecc_corr_cap = 2;
951 else if (*cap <= 4)
952 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +0800953 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +0800954 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +0800955 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +0800956 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +0800957 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +0800958 host->pmecc_corr_cap = 24;
959 else
960 return -EINVAL;
961 }
962 if (host->pmecc_sector_size == 0) {
963 /* use the most fitable sector size (the near smaller one ) */
964 if (*sector_size >= 1024)
965 host->pmecc_sector_size = 1024;
966 else if (*sector_size >= 512)
967 host->pmecc_sector_size = 512;
968 else
969 return -EINVAL;
970 }
971 return 0;
972}
973
Josh Wu1c7b8742012-06-29 17:47:55 +0800974static int __init atmel_pmecc_nand_init_params(struct platform_device *pdev,
975 struct atmel_nand_host *host)
976{
977 struct mtd_info *mtd = &host->mtd;
978 struct nand_chip *nand_chip = &host->nand_chip;
979 struct resource *regs, *regs_pmerr, *regs_rom;
980 int cap, sector_size, err_no;
981
Josh Wu84cfbbb2013-01-23 20:47:12 +0800982 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
983 if (err_no) {
984 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
985 return err_no;
986 }
987
Richard Genoudf666d642013-07-30 17:17:29 +0200988 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +0800989 sector_size != host->pmecc_sector_size)
990 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +0800991
Josh Wu1c7b8742012-06-29 17:47:55 +0800992 cap = host->pmecc_corr_cap;
993 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +0800994 host->pmecc_lookup_table_offset = (sector_size == 512) ?
995 host->pmecc_lookup_table_offset_512 :
996 host->pmecc_lookup_table_offset_1024;
997
Josh Wu1c7b8742012-06-29 17:47:55 +0800998 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
999 cap, sector_size);
1000
1001 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1002 if (!regs) {
1003 dev_warn(host->dev,
1004 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1005 nand_chip->ecc.mode = NAND_ECC_SOFT;
1006 return 0;
1007 }
1008
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001009 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1010 if (IS_ERR(host->ecc)) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001011 dev_err(host->dev, "ioremap failed\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001012 err_no = PTR_ERR(host->ecc);
1013 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001014 }
1015
1016 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001017 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1018 if (IS_ERR(host->pmerrloc_base)) {
1019 dev_err(host->dev,
1020 "Can not get I/O resource for PMECC ERRLOC controller!\n");
1021 err_no = PTR_ERR(host->pmerrloc_base);
1022 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001023 }
1024
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001025 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1026 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, regs_rom);
1027 if (IS_ERR(host->pmecc_rom_base)) {
1028 dev_err(host->dev, "Can not get I/O resource for ROM!\n");
1029 err_no = PTR_ERR(host->pmecc_rom_base);
1030 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001031 }
1032
1033 /* ECC is calculated for the whole page (1 step) */
1034 nand_chip->ecc.size = mtd->writesize;
1035
1036 /* set ECC page size and oob layout */
1037 switch (mtd->writesize) {
1038 case 2048:
1039 host->pmecc_degree = PMECC_GF_DIMENSION_13;
1040 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
1041 host->pmecc_sector_number = mtd->writesize / sector_size;
1042 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
1043 cap, sector_size);
1044 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1045 host->pmecc_index_of = host->pmecc_rom_base +
1046 host->pmecc_lookup_table_offset;
1047
1048 nand_chip->ecc.steps = 1;
1049 nand_chip->ecc.strength = cap;
1050 nand_chip->ecc.bytes = host->pmecc_bytes_per_sector *
1051 host->pmecc_sector_number;
1052 if (nand_chip->ecc.bytes > mtd->oobsize - 2) {
1053 dev_err(host->dev, "No room for ECC bytes\n");
1054 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001055 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001056 }
1057 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1058 mtd->oobsize,
1059 nand_chip->ecc.bytes);
1060 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1061 break;
1062 case 512:
1063 case 1024:
1064 case 4096:
1065 /* TODO */
1066 dev_warn(host->dev,
1067 "Unsupported page size for PMECC, use Software ECC\n");
1068 default:
1069 /* page size not handled by HW ECC */
1070 /* switching back to soft ECC */
1071 nand_chip->ecc.mode = NAND_ECC_SOFT;
1072 return 0;
1073 }
1074
1075 /* Allocate data for PMECC computation */
1076 err_no = pmecc_data_alloc(host);
1077 if (err_no) {
1078 dev_err(host->dev,
1079 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001080 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001081 }
1082
1083 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1084 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1085
1086 atmel_pmecc_core_init(mtd);
1087
1088 return 0;
1089
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001090err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001091 return err_no;
1092}
1093
1094/*
Richard Genoud77f54922008-04-23 19:51:14 +02001095 * Calculate HW ECC
1096 *
1097 * function called after a write
1098 *
1099 * mtd: MTD block structure
1100 * dat: raw data (unused)
1101 * ecc_code: buffer for ECC
1102 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001103static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001104 const u_char *dat, unsigned char *ecc_code)
1105{
1106 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001107 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001108 unsigned int ecc_value;
1109
1110 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001111 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001112
Richard Genoud3fc23892008-10-12 08:42:28 +02001113 ecc_code[0] = ecc_value & 0xFF;
1114 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001115
1116 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001117 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001118
Richard Genoud3fc23892008-10-12 08:42:28 +02001119 ecc_code[2] = ecc_value & 0xFF;
1120 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001121
1122 return 0;
1123}
1124
1125/*
1126 * HW ECC read page function
1127 *
1128 * mtd: mtd info structure
1129 * chip: nand chip info structure
1130 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001131 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001132 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001133static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1134 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001135{
1136 int eccsize = chip->ecc.size;
1137 int eccbytes = chip->ecc.bytes;
1138 uint32_t *eccpos = chip->ecc.layout->eccpos;
1139 uint8_t *p = buf;
1140 uint8_t *oob = chip->oob_poi;
1141 uint8_t *ecc_pos;
1142 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001143 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001144
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001145 /*
1146 * Errata: ALE is incorrectly wired up to the ECC controller
1147 * on the AP7000, so it will include the address cycles in the
1148 * ECC calculation.
1149 *
1150 * Workaround: Reset the parity registers before reading the
1151 * actual data.
1152 */
Josh Wu71b94e22013-05-09 15:34:54 +08001153 struct atmel_nand_host *host = chip->priv;
1154 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001155 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001156
Richard Genoud77f54922008-04-23 19:51:14 +02001157 /* read the page */
1158 chip->read_buf(mtd, p, eccsize);
1159
1160 /* move to ECC position if needed */
1161 if (eccpos[0] != 0) {
1162 /* This only works on large pages
1163 * because the ECC controller waits for
1164 * NAND_CMD_RNDOUTSTART after the
1165 * NAND_CMD_RNDOUT.
1166 * anyway, for small pages, the eccpos[0] == 0
1167 */
1168 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1169 mtd->writesize + eccpos[0], -1);
1170 }
1171
1172 /* the ECC controller needs to read the ECC just after the data */
1173 ecc_pos = oob + eccpos[0];
1174 chip->read_buf(mtd, ecc_pos, eccbytes);
1175
1176 /* check if there's an error */
1177 stat = chip->ecc.correct(mtd, p, oob, NULL);
1178
Mike Dunn3f91e942012-04-25 12:06:09 -07001179 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001180 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001181 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001182 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001183 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1184 }
Richard Genoud77f54922008-04-23 19:51:14 +02001185
1186 /* get back to oob start (end of page) */
1187 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1188
1189 /* read the oob */
1190 chip->read_buf(mtd, oob, mtd->oobsize);
1191
Mike Dunn3f91e942012-04-25 12:06:09 -07001192 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001193}
1194
1195/*
1196 * HW ECC Correction
1197 *
1198 * function called after a read
1199 *
1200 * mtd: MTD block structure
1201 * dat: raw data read from the chip
1202 * read_ecc: ECC from the chip (unused)
1203 * isnull: unused
1204 *
1205 * Detect and correct a 1 bit error for a page
1206 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001207static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001208 u_char *read_ecc, u_char *isnull)
1209{
1210 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001211 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001212 unsigned int ecc_status;
1213 unsigned int ecc_word, ecc_bit;
1214
1215 /* get the status from the Status Register */
1216 ecc_status = ecc_readl(host->ecc, SR);
1217
1218 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001219 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001220 return 0;
1221
1222 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001223 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001224 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001225 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001226 ecc_word >>= 4;
1227
1228 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001229 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001230 /* check if it is a freshly erased block
1231 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001232 if ((ecc_bit == ATMEL_ECC_BITADDR)
1233 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001234 /* the block has just been erased, return OK */
1235 return 0;
1236 }
1237 /* it doesn't seems to be a freshly
1238 * erased block.
1239 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001240 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001241 " Unable to correct.\n");
1242 return -EIO;
1243 }
1244
1245 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001246 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001247 /* there's nothing much to do here.
1248 * the bit error is on the ECC itself.
1249 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001250 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001251 " Nothing to correct\n");
1252 return 0;
1253 }
1254
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001255 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001256 " (word offset in the page :"
1257 " 0x%x bit offset : 0x%x)\n",
1258 ecc_word, ecc_bit);
1259 /* correct the error */
1260 if (nand_chip->options & NAND_BUSWIDTH_16) {
1261 /* 16 bits words */
1262 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1263 } else {
1264 /* 8 bits words */
1265 dat[ecc_word] ^= (1 << ecc_bit);
1266 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001267 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001268 return 1;
1269}
1270
1271/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001272 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001273 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001274static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1275{
Josh Wu71b94e22013-05-09 15:34:54 +08001276 struct nand_chip *nand_chip = mtd->priv;
1277 struct atmel_nand_host *host = nand_chip->priv;
1278
1279 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001280 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001281}
Richard Genoud77f54922008-04-23 19:51:14 +02001282
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001283#if defined(CONFIG_OF)
Bill Pemberton06f25512012-11-19 13:23:07 -05001284static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001285 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001286{
Josh Wuc0cf7872013-01-23 20:47:08 +08001287 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001288 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001289 int ecc_mode;
1290 struct atmel_nand_data *board = &host->board;
1291 enum of_gpio_flags flags;
1292
1293 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1294 if (val >= 32) {
1295 dev_err(host->dev, "invalid addr-offset %u\n", val);
1296 return -EINVAL;
1297 }
1298 board->ale = val;
1299 }
1300
1301 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1302 if (val >= 32) {
1303 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1304 return -EINVAL;
1305 }
1306 board->cle = val;
1307 }
1308
1309 ecc_mode = of_get_nand_ecc_mode(np);
1310
1311 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1312
1313 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1314
Josh Wu1b719262013-05-09 15:34:55 +08001315 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1316
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001317 if (of_get_nand_bus_width(np) == 16)
1318 board->bus_width_16 = 1;
1319
1320 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1321 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1322
1323 board->enable_pin = of_get_gpio(np, 1);
1324 board->det_pin = of_get_gpio(np, 2);
1325
Josh Wua41b51a2012-06-29 17:47:54 +08001326 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1327
1328 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1329 return 0; /* Not using PMECC */
1330
1331 /* use PMECC, get correction capability, sector size and lookup
1332 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001333 * If correction bits and sector size are not specified, then find
1334 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001335 */
Josh Wue66b4312013-01-23 20:47:11 +08001336 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1337 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1338 (val != 24)) {
1339 dev_err(host->dev,
1340 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1341 val);
1342 return -EINVAL;
1343 }
1344 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001345 }
Josh Wua41b51a2012-06-29 17:47:54 +08001346
Josh Wue66b4312013-01-23 20:47:11 +08001347 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1348 if ((val != 512) && (val != 1024)) {
1349 dev_err(host->dev,
1350 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1351 val);
1352 return -EINVAL;
1353 }
1354 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001355 }
Josh Wua41b51a2012-06-29 17:47:54 +08001356
1357 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1358 offset, 2) != 0) {
1359 dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
1360 return -EINVAL;
1361 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001362 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001363 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1364 return -EINVAL;
1365 }
Josh Wue66b4312013-01-23 20:47:11 +08001366 host->pmecc_lookup_table_offset_512 = offset[0];
1367 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001368
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001369 return 0;
1370}
1371#else
Bill Pemberton06f25512012-11-19 13:23:07 -05001372static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001373 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001374{
1375 return -EINVAL;
1376}
1377#endif
1378
Josh Wu3dfe41a2012-06-25 18:07:43 +08001379static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
1380 struct atmel_nand_host *host)
1381{
1382 struct mtd_info *mtd = &host->mtd;
1383 struct nand_chip *nand_chip = &host->nand_chip;
1384 struct resource *regs;
1385
1386 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1387 if (!regs) {
1388 dev_err(host->dev,
1389 "Can't get I/O resource regs, use software ECC\n");
1390 nand_chip->ecc.mode = NAND_ECC_SOFT;
1391 return 0;
1392 }
1393
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001394 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1395 if (IS_ERR(host->ecc)) {
Josh Wu3dfe41a2012-06-25 18:07:43 +08001396 dev_err(host->dev, "ioremap failed\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001397 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001398 }
1399
1400 /* ECC is calculated for the whole page (1 step) */
1401 nand_chip->ecc.size = mtd->writesize;
1402
1403 /* set ECC page size and oob layout */
1404 switch (mtd->writesize) {
1405 case 512:
1406 nand_chip->ecc.layout = &atmel_oobinfo_small;
1407 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1408 break;
1409 case 1024:
1410 nand_chip->ecc.layout = &atmel_oobinfo_large;
1411 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1412 break;
1413 case 2048:
1414 nand_chip->ecc.layout = &atmel_oobinfo_large;
1415 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1416 break;
1417 case 4096:
1418 nand_chip->ecc.layout = &atmel_oobinfo_large;
1419 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1420 break;
1421 default:
1422 /* page size not handled by HW ECC */
1423 /* switching back to soft ECC */
1424 nand_chip->ecc.mode = NAND_ECC_SOFT;
1425 return 0;
1426 }
1427
1428 /* set up for HW ECC */
1429 nand_chip->ecc.calculate = atmel_nand_calculate;
1430 nand_chip->ecc.correct = atmel_nand_correct;
1431 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1432 nand_chip->ecc.read_page = atmel_nand_read_page;
1433 nand_chip->ecc.bytes = 4;
1434 nand_chip->ecc.strength = 1;
1435
1436 return 0;
1437}
1438
Andrew Victor42cb1402006-10-19 18:24:35 +02001439/*
1440 * Probe for the NAND device.
1441 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001442static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02001443{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001444 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02001445 struct mtd_info *mtd;
1446 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001447 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001448 struct mtd_part_parser_data ppdata = {};
Andrew Victor42cb1402006-10-19 18:24:35 +02001449 int res;
Andrew Victor42cb1402006-10-19 18:24:35 +02001450
1451 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001452 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +02001453 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001454 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +02001455 return -ENOMEM;
1456 }
1457
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001458 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1459 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
1460 if (IS_ERR(host->io_base)) {
1461 dev_err(&pdev->dev, "atmel_nand: ioremap resource failed\n");
1462 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001463 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02001464 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001465 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02001466
1467 mtd = &host->mtd;
1468 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001469 host->dev = &pdev->dev;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001470 if (pdev->dev.of_node) {
1471 res = atmel_of_init_port(host, pdev->dev.of_node);
1472 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001473 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001474 } else {
1475 memcpy(&host->board, pdev->dev.platform_data,
1476 sizeof(struct atmel_nand_data));
1477 }
Andrew Victor42cb1402006-10-19 18:24:35 +02001478
1479 nand_chip->priv = host; /* link the private data structures */
1480 mtd->priv = nand_chip;
1481 mtd->owner = THIS_MODULE;
1482
1483 /* Set address of NAND IO lines */
1484 nand_chip->IO_ADDR_R = host->io_base;
1485 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001486 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +03001487
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001488 if (gpio_is_valid(host->board.rdy_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001489 res = devm_gpio_request(&pdev->dev,
1490 host->board.rdy_pin, "nand_rdy");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001491 if (res < 0) {
1492 dev_err(&pdev->dev,
1493 "can't request rdy gpio %d\n",
1494 host->board.rdy_pin);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001495 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001496 }
1497
1498 res = gpio_direction_input(host->board.rdy_pin);
1499 if (res < 0) {
1500 dev_err(&pdev->dev,
1501 "can't request input direction rdy gpio %d\n",
1502 host->board.rdy_pin);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001503 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001504 }
1505
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001506 nand_chip->dev_ready = atmel_nand_device_ready;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001507 }
1508
1509 if (gpio_is_valid(host->board.enable_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001510 res = devm_gpio_request(&pdev->dev,
1511 host->board.enable_pin, "nand_enable");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001512 if (res < 0) {
1513 dev_err(&pdev->dev,
1514 "can't request enable gpio %d\n",
1515 host->board.enable_pin);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001516 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001517 }
1518
1519 res = gpio_direction_output(host->board.enable_pin, 1);
1520 if (res < 0) {
1521 dev_err(&pdev->dev,
1522 "can't request output direction enable gpio %d\n",
1523 host->board.enable_pin);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001524 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001525 }
1526 }
Ivan Kutena4265f82007-05-24 14:35:58 +03001527
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001528 nand_chip->ecc.mode = host->board.ecc_mode;
Andrew Victor42cb1402006-10-19 18:24:35 +02001529 nand_chip->chip_delay = 20; /* 20us command delay time */
1530
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001531 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02001532 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08001533
1534 nand_chip->read_buf = atmel_read_buf;
1535 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02001536
Andrew Victor42cb1402006-10-19 18:24:35 +02001537 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001538 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02001539
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001540 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001541 res = devm_gpio_request(&pdev->dev,
1542 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001543 if (res < 0) {
1544 dev_err(&pdev->dev,
1545 "can't request det gpio %d\n",
1546 host->board.det_pin);
1547 goto err_no_card;
1548 }
1549
1550 res = gpio_direction_input(host->board.det_pin);
1551 if (res < 0) {
1552 dev_err(&pdev->dev,
1553 "can't request input direction det gpio %d\n",
1554 host->board.det_pin);
1555 goto err_no_card;
1556 }
1557
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001558 if (gpio_get_value(host->board.det_pin)) {
Simon Polettef4fa6972009-05-27 18:19:39 +03001559 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01001560 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001561 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02001562 }
1563 }
1564
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001565 if (host->board.on_flash_bbt || on_flash_bbt) {
Simon Polettef4fa6972009-05-27 18:19:39 +03001566 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001567 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa6972009-05-27 18:19:39 +03001568 }
1569
Josh Wu1b719262013-05-09 15:34:55 +08001570 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08001571 use_dma = 0;
1572
1573 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08001574 dma_cap_mask_t mask;
1575
1576 dma_cap_zero(mask);
1577 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02001578 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08001579 if (!host->dma_chan) {
1580 dev_err(host->dev, "Failed to request DMA channel\n");
1581 use_dma = 0;
1582 }
1583 }
1584 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08001585 dev_info(host->dev, "Using %s for DMA transfers.\n",
1586 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08001587 else
1588 dev_info(host->dev, "No DMA support for NAND access.\n");
1589
Richard Genoud77f54922008-04-23 19:51:14 +02001590 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00001591 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02001592 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001593 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02001594 }
1595
Richard Genoud3fc23892008-10-12 08:42:28 +02001596 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001597 if (host->has_pmecc)
1598 res = atmel_pmecc_nand_init_params(pdev, host);
1599 else
1600 res = atmel_hw_nand_init_params(pdev, host);
1601
Josh Wu3dfe41a2012-06-25 18:07:43 +08001602 if (res != 0)
1603 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02001604 }
1605
1606 /* second phase scan */
1607 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02001608 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001609 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02001610 }
1611
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001612 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001613 ppdata.of_node = pdev->dev.of_node;
1614 res = mtd_device_parse_register(mtd, NULL, &ppdata,
1615 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02001616 if (!res)
1617 return res;
1618
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001619err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001620 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08001621 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001622err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001623err_scan_ident:
1624err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001625 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08001626 if (host->dma_chan)
1627 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001628err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02001629 return res;
1630}
1631
1632/*
1633 * Remove a NAND device.
1634 */
David Brownell23a346c2008-07-03 23:40:16 -07001635static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02001636{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001637 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +02001638 struct mtd_info *mtd = &host->mtd;
1639
1640 nand_release(mtd);
1641
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001642 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02001643
Josh Wu1c7b8742012-06-29 17:47:55 +08001644 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
1645 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1646 pmerrloc_writel(host->pmerrloc_base, ELDIS,
1647 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08001648 }
1649
Hong Xucbc6c5e2011-01-18 14:36:05 +08001650 if (host->dma_chan)
1651 dma_release_channel(host->dma_chan);
1652
Andrew Victor42cb1402006-10-19 18:24:35 +02001653 return 0;
1654}
1655
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001656#if defined(CONFIG_OF)
1657static const struct of_device_id atmel_nand_dt_ids[] = {
1658 { .compatible = "atmel,at91rm9200-nand" },
1659 { /* sentinel */ }
1660};
1661
1662MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
1663#endif
1664
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001665static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -07001666 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +02001667 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001668 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +02001669 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001670 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02001671 },
1672};
1673
Jingoo Hanc5345ed2013-03-05 13:30:04 +09001674module_platform_driver_probe(atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +02001675
1676MODULE_LICENSE("GPL");
1677MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02001678MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001679MODULE_ALIAS("platform:atmel_nand");