blob: a43adce34ae5fa2d829a494f782e9655bed546c3 [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 Polettef4fa697c2009-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 Polettef4fa697c2009-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
361static void pmecc_data_free(struct atmel_nand_host *host)
362{
363 kfree(host->pmecc_partial_syn);
364 kfree(host->pmecc_si);
365 kfree(host->pmecc_lmu);
366 kfree(host->pmecc_smu);
367 kfree(host->pmecc_mu);
368 kfree(host->pmecc_dmu);
369 kfree(host->pmecc_delta);
370}
371
Bill Pemberton06f25512012-11-19 13:23:07 -0500372static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800373{
374 const int cap = host->pmecc_corr_cap;
375
376 host->pmecc_partial_syn = kzalloc((2 * cap + 1) * sizeof(int16_t),
377 GFP_KERNEL);
378 host->pmecc_si = kzalloc((2 * cap + 1) * sizeof(int16_t), GFP_KERNEL);
379 host->pmecc_lmu = kzalloc((cap + 1) * sizeof(int16_t), GFP_KERNEL);
380 host->pmecc_smu = kzalloc((cap + 2) * (2 * cap + 1) * sizeof(int16_t),
381 GFP_KERNEL);
382 host->pmecc_mu = kzalloc((cap + 1) * sizeof(int), GFP_KERNEL);
383 host->pmecc_dmu = kzalloc((cap + 1) * sizeof(int), GFP_KERNEL);
384 host->pmecc_delta = kzalloc((cap + 1) * sizeof(int), GFP_KERNEL);
385
386 if (host->pmecc_partial_syn &&
387 host->pmecc_si &&
388 host->pmecc_lmu &&
389 host->pmecc_smu &&
390 host->pmecc_mu &&
391 host->pmecc_dmu &&
392 host->pmecc_delta)
393 return 0;
394
395 /* error happened */
396 pmecc_data_free(host);
397 return -ENOMEM;
398}
399
400static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
401{
402 struct nand_chip *nand_chip = mtd->priv;
403 struct atmel_nand_host *host = nand_chip->priv;
404 int i;
405 uint32_t value;
406
407 /* Fill odd syndromes */
408 for (i = 0; i < host->pmecc_corr_cap; i++) {
409 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
410 if (i & 1)
411 value >>= 16;
412 value &= 0xffff;
413 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
414 }
415}
416
417static void pmecc_substitute(struct mtd_info *mtd)
418{
419 struct nand_chip *nand_chip = mtd->priv;
420 struct atmel_nand_host *host = nand_chip->priv;
421 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
422 int16_t __iomem *index_of = host->pmecc_index_of;
423 int16_t *partial_syn = host->pmecc_partial_syn;
424 const int cap = host->pmecc_corr_cap;
425 int16_t *si;
426 int i, j;
427
428 /* si[] is a table that holds the current syndrome value,
429 * an element of that table belongs to the field
430 */
431 si = host->pmecc_si;
432
433 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
434
435 /* Computation 2t syndromes based on S(x) */
436 /* Odd syndromes */
437 for (i = 1; i < 2 * cap; i += 2) {
438 for (j = 0; j < host->pmecc_degree; j++) {
439 if (partial_syn[i] & ((unsigned short)0x1 << j))
440 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
441 }
442 }
443 /* Even syndrome = (Odd syndrome) ** 2 */
444 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
445 if (si[j] == 0) {
446 si[i] = 0;
447 } else {
448 int16_t tmp;
449
450 tmp = readw_relaxed(index_of + si[j]);
451 tmp = (tmp * 2) % host->pmecc_cw_len;
452 si[i] = readw_relaxed(alpha_to + tmp);
453 }
454 }
455
456 return;
457}
458
459static void pmecc_get_sigma(struct mtd_info *mtd)
460{
461 struct nand_chip *nand_chip = mtd->priv;
462 struct atmel_nand_host *host = nand_chip->priv;
463
464 int16_t *lmu = host->pmecc_lmu;
465 int16_t *si = host->pmecc_si;
466 int *mu = host->pmecc_mu;
467 int *dmu = host->pmecc_dmu; /* Discrepancy */
468 int *delta = host->pmecc_delta; /* Delta order */
469 int cw_len = host->pmecc_cw_len;
470 const int16_t cap = host->pmecc_corr_cap;
471 const int num = 2 * cap + 1;
472 int16_t __iomem *index_of = host->pmecc_index_of;
473 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
474 int i, j, k;
475 uint32_t dmu_0_count, tmp;
476 int16_t *smu = host->pmecc_smu;
477
478 /* index of largest delta */
479 int ro;
480 int largest;
481 int diff;
482
483 dmu_0_count = 0;
484
485 /* First Row */
486
487 /* Mu */
488 mu[0] = -1;
489
490 memset(smu, 0, sizeof(int16_t) * num);
491 smu[0] = 1;
492
493 /* discrepancy set to 1 */
494 dmu[0] = 1;
495 /* polynom order set to 0 */
496 lmu[0] = 0;
497 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
498
499 /* Second Row */
500
501 /* Mu */
502 mu[1] = 0;
503 /* Sigma(x) set to 1 */
504 memset(&smu[num], 0, sizeof(int16_t) * num);
505 smu[num] = 1;
506
507 /* discrepancy set to S1 */
508 dmu[1] = si[1];
509
510 /* polynom order set to 0 */
511 lmu[1] = 0;
512
513 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
514
515 /* Init the Sigma(x) last row */
516 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
517
518 for (i = 1; i <= cap; i++) {
519 mu[i + 1] = i << 1;
520 /* Begin Computing Sigma (Mu+1) and L(mu) */
521 /* check if discrepancy is set to 0 */
522 if (dmu[i] == 0) {
523 dmu_0_count++;
524
525 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
526 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
527 tmp += 2;
528 else
529 tmp += 1;
530
531 if (dmu_0_count == tmp) {
532 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
533 smu[(cap + 1) * num + j] =
534 smu[i * num + j];
535
536 lmu[cap + 1] = lmu[i];
537 return;
538 }
539
540 /* copy polynom */
541 for (j = 0; j <= lmu[i] >> 1; j++)
542 smu[(i + 1) * num + j] = smu[i * num + j];
543
544 /* copy previous polynom order to the next */
545 lmu[i + 1] = lmu[i];
546 } else {
547 ro = 0;
548 largest = -1;
549 /* find largest delta with dmu != 0 */
550 for (j = 0; j < i; j++) {
551 if ((dmu[j]) && (delta[j] > largest)) {
552 largest = delta[j];
553 ro = j;
554 }
555 }
556
557 /* compute difference */
558 diff = (mu[i] - mu[ro]);
559
560 /* Compute degree of the new smu polynomial */
561 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
562 lmu[i + 1] = lmu[i];
563 else
564 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
565
566 /* Init smu[i+1] with 0 */
567 for (k = 0; k < num; k++)
568 smu[(i + 1) * num + k] = 0;
569
570 /* Compute smu[i+1] */
571 for (k = 0; k <= lmu[ro] >> 1; k++) {
572 int16_t a, b, c;
573
574 if (!(smu[ro * num + k] && dmu[i]))
575 continue;
576 a = readw_relaxed(index_of + dmu[i]);
577 b = readw_relaxed(index_of + dmu[ro]);
578 c = readw_relaxed(index_of + smu[ro * num + k]);
579 tmp = a + (cw_len - b) + c;
580 a = readw_relaxed(alpha_to + tmp % cw_len);
581 smu[(i + 1) * num + (k + diff)] = a;
582 }
583
584 for (k = 0; k <= lmu[i] >> 1; k++)
585 smu[(i + 1) * num + k] ^= smu[i * num + k];
586 }
587
588 /* End Computing Sigma (Mu+1) and L(mu) */
589 /* In either case compute delta */
590 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
591
592 /* Do not compute discrepancy for the last iteration */
593 if (i >= cap)
594 continue;
595
596 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
597 tmp = 2 * (i - 1);
598 if (k == 0) {
599 dmu[i + 1] = si[tmp + 3];
600 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
601 int16_t a, b, c;
602 a = readw_relaxed(index_of +
603 smu[(i + 1) * num + k]);
604 b = si[2 * (i - 1) + 3 - k];
605 c = readw_relaxed(index_of + b);
606 tmp = a + c;
607 tmp %= cw_len;
608 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
609 dmu[i + 1];
610 }
611 }
612 }
613
614 return;
615}
616
617static int pmecc_err_location(struct mtd_info *mtd)
618{
619 struct nand_chip *nand_chip = mtd->priv;
620 struct atmel_nand_host *host = nand_chip->priv;
621 unsigned long end_time;
622 const int cap = host->pmecc_corr_cap;
623 const int num = 2 * cap + 1;
624 int sector_size = host->pmecc_sector_size;
625 int err_nbr = 0; /* number of error */
626 int roots_nbr; /* number of roots */
627 int i;
628 uint32_t val;
629 int16_t *smu = host->pmecc_smu;
630
631 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
632
633 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
634 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
635 smu[(cap + 1) * num + i]);
636 err_nbr++;
637 }
638
639 val = (err_nbr - 1) << 16;
640 if (sector_size == 1024)
641 val |= 1;
642
643 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
644 pmerrloc_writel(host->pmerrloc_base, ELEN,
645 sector_size * 8 + host->pmecc_degree * cap);
646
647 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
648 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
649 & PMERRLOC_CALC_DONE)) {
650 if (unlikely(time_after(jiffies, end_time))) {
651 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
652 return -1;
653 }
654 cpu_relax();
655 }
656
657 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
658 & PMERRLOC_ERR_NUM_MASK) >> 8;
659 /* Number of roots == degree of smu hence <= cap */
660 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
661 return err_nbr - 1;
662
663 /* Number of roots does not match the degree of smu
664 * unable to correct error */
665 return -1;
666}
667
668static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
669 int sector_num, int extra_bytes, int err_nbr)
670{
671 struct nand_chip *nand_chip = mtd->priv;
672 struct atmel_nand_host *host = nand_chip->priv;
673 int i = 0;
674 int byte_pos, bit_pos, sector_size, pos;
675 uint32_t tmp;
676 uint8_t err_byte;
677
678 sector_size = host->pmecc_sector_size;
679
680 while (err_nbr) {
681 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
682 byte_pos = tmp / 8;
683 bit_pos = tmp % 8;
684
685 if (byte_pos >= (sector_size + extra_bytes))
686 BUG(); /* should never happen */
687
688 if (byte_pos < sector_size) {
689 err_byte = *(buf + byte_pos);
690 *(buf + byte_pos) ^= (1 << bit_pos);
691
692 pos = sector_num * host->pmecc_sector_size + byte_pos;
693 dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
694 pos, bit_pos, err_byte, *(buf + byte_pos));
695 } else {
696 /* Bit flip in OOB area */
697 tmp = sector_num * host->pmecc_bytes_per_sector
698 + (byte_pos - sector_size);
699 err_byte = ecc[tmp];
700 ecc[tmp] ^= (1 << bit_pos);
701
702 pos = tmp + nand_chip->ecc.layout->eccpos[0];
703 dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
704 pos, bit_pos, err_byte, ecc[tmp]);
705 }
706
707 i++;
708 err_nbr--;
709 }
710
711 return;
712}
713
714static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
715 u8 *ecc)
716{
717 struct nand_chip *nand_chip = mtd->priv;
718 struct atmel_nand_host *host = nand_chip->priv;
719 int i, err_nbr, eccbytes;
720 uint8_t *buf_pos;
Josh Wuc0c70d92012-11-27 18:50:31 +0800721 int total_err = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800722
723 eccbytes = nand_chip->ecc.bytes;
724 for (i = 0; i < eccbytes; i++)
725 if (ecc[i] != 0xff)
726 goto normal_check;
727 /* Erased page, return OK */
728 return 0;
729
730normal_check:
731 for (i = 0; i < host->pmecc_sector_number; i++) {
732 err_nbr = 0;
733 if (pmecc_stat & 0x1) {
734 buf_pos = buf + i * host->pmecc_sector_size;
735
736 pmecc_gen_syndrome(mtd, i);
737 pmecc_substitute(mtd);
738 pmecc_get_sigma(mtd);
739
740 err_nbr = pmecc_err_location(mtd);
741 if (err_nbr == -1) {
742 dev_err(host->dev, "PMECC: Too many errors\n");
743 mtd->ecc_stats.failed++;
744 return -EIO;
745 } else {
746 pmecc_correct_data(mtd, buf_pos, ecc, i,
747 host->pmecc_bytes_per_sector, err_nbr);
748 mtd->ecc_stats.corrected += err_nbr;
Josh Wuc0c70d92012-11-27 18:50:31 +0800749 total_err += err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800750 }
751 }
752 pmecc_stat >>= 1;
753 }
754
Josh Wuc0c70d92012-11-27 18:50:31 +0800755 return total_err;
Josh Wu1c7b8742012-06-29 17:47:55 +0800756}
757
758static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
759 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
760{
761 struct atmel_nand_host *host = chip->priv;
762 int eccsize = chip->ecc.size;
763 uint8_t *oob = chip->oob_poi;
764 uint32_t *eccpos = chip->ecc.layout->eccpos;
765 uint32_t stat;
766 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800767 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800768
769 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
770 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
771 pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG)
772 & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE);
773
774 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
775 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
776
777 chip->read_buf(mtd, buf, eccsize);
778 chip->read_buf(mtd, oob, mtd->oobsize);
779
780 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
781 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
782 if (unlikely(time_after(jiffies, end_time))) {
783 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
784 return -EIO;
785 }
786 cpu_relax();
787 }
788
789 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800790 if (stat != 0) {
791 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
792 if (bitflips < 0)
793 /* uncorrectable errors */
794 return 0;
795 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800796
Josh Wuc0c70d92012-11-27 18:50:31 +0800797 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800798}
799
800static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
801 struct nand_chip *chip, const uint8_t *buf, int oob_required)
802{
803 struct atmel_nand_host *host = chip->priv;
804 uint32_t *eccpos = chip->ecc.layout->eccpos;
805 int i, j;
806 unsigned long end_time;
807
808 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
809 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
810
811 pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG) |
812 PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
813
814 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
815 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
816
817 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
818
819 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
820 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
821 if (unlikely(time_after(jiffies, end_time))) {
822 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
823 return -EIO;
824 }
825 cpu_relax();
826 }
827
828 for (i = 0; i < host->pmecc_sector_number; i++) {
829 for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
830 int pos;
831
832 pos = i * host->pmecc_bytes_per_sector + j;
833 chip->oob_poi[eccpos[pos]] =
834 pmecc_readb_ecc_relaxed(host->ecc, i, j);
835 }
836 }
837 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
838
839 return 0;
840}
841
842static void atmel_pmecc_core_init(struct mtd_info *mtd)
843{
844 struct nand_chip *nand_chip = mtd->priv;
845 struct atmel_nand_host *host = nand_chip->priv;
846 uint32_t val = 0;
847 struct nand_ecclayout *ecc_layout;
848
849 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
850 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
851
852 switch (host->pmecc_corr_cap) {
853 case 2:
854 val = PMECC_CFG_BCH_ERR2;
855 break;
856 case 4:
857 val = PMECC_CFG_BCH_ERR4;
858 break;
859 case 8:
860 val = PMECC_CFG_BCH_ERR8;
861 break;
862 case 12:
863 val = PMECC_CFG_BCH_ERR12;
864 break;
865 case 24:
866 val = PMECC_CFG_BCH_ERR24;
867 break;
868 }
869
870 if (host->pmecc_sector_size == 512)
871 val |= PMECC_CFG_SECTOR512;
872 else if (host->pmecc_sector_size == 1024)
873 val |= PMECC_CFG_SECTOR1024;
874
875 switch (host->pmecc_sector_number) {
876 case 1:
877 val |= PMECC_CFG_PAGE_1SECTOR;
878 break;
879 case 2:
880 val |= PMECC_CFG_PAGE_2SECTORS;
881 break;
882 case 4:
883 val |= PMECC_CFG_PAGE_4SECTORS;
884 break;
885 case 8:
886 val |= PMECC_CFG_PAGE_8SECTORS;
887 break;
888 }
889
890 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
891 | PMECC_CFG_AUTO_DISABLE);
892 pmecc_writel(host->ecc, CFG, val);
893
894 ecc_layout = nand_chip->ecc.layout;
895 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
896 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
897 pmecc_writel(host->ecc, EADDR,
898 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
899 /* See datasheet about PMECC Clock Control Register */
900 pmecc_writel(host->ecc, CLK, 2);
901 pmecc_writel(host->ecc, IDR, 0xff);
902 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
903}
904
Josh Wu84cfbbb2013-01-23 20:47:12 +0800905/*
906 * Get ECC requirement in ONFI parameters, returns -1 if ONFI
907 * parameters is not supported.
908 * return 0 if success to get the ECC requirement.
909 */
910static int get_onfi_ecc_param(struct nand_chip *chip,
911 int *ecc_bits, int *sector_size)
912{
913 *ecc_bits = *sector_size = 0;
914
915 if (chip->onfi_params.ecc_bits == 0xff)
916 /* TODO: the sector_size and ecc_bits need to be find in
917 * extended ecc parameter, currently we don't support it.
918 */
919 return -1;
920
921 *ecc_bits = chip->onfi_params.ecc_bits;
922
923 /* The default sector size (ecc codeword size) is 512 */
924 *sector_size = 512;
925
926 return 0;
927}
928
929/*
930 * Get ecc requirement from ONFI parameters ecc requirement.
931 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
932 * will set them according to ONFI ecc requirement. Otherwise, use the
933 * value in DTS file.
934 * return 0 if success. otherwise return error code.
935 */
936static int pmecc_choose_ecc(struct atmel_nand_host *host,
937 int *cap, int *sector_size)
938{
939 /* Get ECC requirement from ONFI parameters */
940 *cap = *sector_size = 0;
941 if (host->nand_chip.onfi_version) {
942 if (!get_onfi_ecc_param(&host->nand_chip, cap, sector_size))
943 dev_info(host->dev, "ONFI params, minimum required ECC: %d bits in %d bytes\n",
944 *cap, *sector_size);
945 else
946 dev_info(host->dev, "NAND chip ECC reqirement is in Extended ONFI parameter, we don't support yet.\n");
947 } else {
948 dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes");
949 }
950 if (*cap == 0 && *sector_size == 0) {
951 *cap = 2;
952 *sector_size = 512;
953 }
954
955 /* If dts file doesn't specify then use the one in ONFI parameters */
956 if (host->pmecc_corr_cap == 0) {
957 /* use the most fitable ecc bits (the near bigger one ) */
958 if (*cap <= 2)
959 host->pmecc_corr_cap = 2;
960 else if (*cap <= 4)
961 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +0800962 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +0800963 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +0800964 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +0800965 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +0800966 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +0800967 host->pmecc_corr_cap = 24;
968 else
969 return -EINVAL;
970 }
971 if (host->pmecc_sector_size == 0) {
972 /* use the most fitable sector size (the near smaller one ) */
973 if (*sector_size >= 1024)
974 host->pmecc_sector_size = 1024;
975 else if (*sector_size >= 512)
976 host->pmecc_sector_size = 512;
977 else
978 return -EINVAL;
979 }
980 return 0;
981}
982
Josh Wu1c7b8742012-06-29 17:47:55 +0800983static int __init atmel_pmecc_nand_init_params(struct platform_device *pdev,
984 struct atmel_nand_host *host)
985{
986 struct mtd_info *mtd = &host->mtd;
987 struct nand_chip *nand_chip = &host->nand_chip;
988 struct resource *regs, *regs_pmerr, *regs_rom;
989 int cap, sector_size, err_no;
990
Josh Wu84cfbbb2013-01-23 20:47:12 +0800991 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
992 if (err_no) {
993 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
994 return err_no;
995 }
996
Richard Genoudf666d642013-07-30 17:17:29 +0200997 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +0800998 sector_size != host->pmecc_sector_size)
999 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001000
Josh Wu1c7b8742012-06-29 17:47:55 +08001001 cap = host->pmecc_corr_cap;
1002 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001003 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1004 host->pmecc_lookup_table_offset_512 :
1005 host->pmecc_lookup_table_offset_1024;
1006
Josh Wu1c7b8742012-06-29 17:47:55 +08001007 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1008 cap, sector_size);
1009
1010 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1011 if (!regs) {
1012 dev_warn(host->dev,
1013 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1014 nand_chip->ecc.mode = NAND_ECC_SOFT;
1015 return 0;
1016 }
1017
1018 host->ecc = ioremap(regs->start, resource_size(regs));
1019 if (host->ecc == NULL) {
1020 dev_err(host->dev, "ioremap failed\n");
1021 err_no = -EIO;
1022 goto err_pmecc_ioremap;
1023 }
1024
1025 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1026 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1027 if (regs_pmerr && regs_rom) {
1028 host->pmerrloc_base = ioremap(regs_pmerr->start,
1029 resource_size(regs_pmerr));
1030 host->pmecc_rom_base = ioremap(regs_rom->start,
1031 resource_size(regs_rom));
1032 }
1033
1034 if (!host->pmerrloc_base || !host->pmecc_rom_base) {
1035 dev_err(host->dev,
1036 "Can not get I/O resource for PMECC ERRLOC controller or ROM!\n");
1037 err_no = -EIO;
1038 goto err_pmloc_ioremap;
1039 }
1040
1041 /* ECC is calculated for the whole page (1 step) */
1042 nand_chip->ecc.size = mtd->writesize;
1043
1044 /* set ECC page size and oob layout */
1045 switch (mtd->writesize) {
1046 case 2048:
1047 host->pmecc_degree = PMECC_GF_DIMENSION_13;
1048 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
1049 host->pmecc_sector_number = mtd->writesize / sector_size;
1050 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
1051 cap, sector_size);
1052 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1053 host->pmecc_index_of = host->pmecc_rom_base +
1054 host->pmecc_lookup_table_offset;
1055
1056 nand_chip->ecc.steps = 1;
1057 nand_chip->ecc.strength = cap;
1058 nand_chip->ecc.bytes = host->pmecc_bytes_per_sector *
1059 host->pmecc_sector_number;
1060 if (nand_chip->ecc.bytes > mtd->oobsize - 2) {
1061 dev_err(host->dev, "No room for ECC bytes\n");
1062 err_no = -EINVAL;
1063 goto err_no_ecc_room;
1064 }
1065 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1066 mtd->oobsize,
1067 nand_chip->ecc.bytes);
1068 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1069 break;
1070 case 512:
1071 case 1024:
1072 case 4096:
1073 /* TODO */
1074 dev_warn(host->dev,
1075 "Unsupported page size for PMECC, use Software ECC\n");
1076 default:
1077 /* page size not handled by HW ECC */
1078 /* switching back to soft ECC */
1079 nand_chip->ecc.mode = NAND_ECC_SOFT;
1080 return 0;
1081 }
1082
1083 /* Allocate data for PMECC computation */
1084 err_no = pmecc_data_alloc(host);
1085 if (err_no) {
1086 dev_err(host->dev,
1087 "Cannot allocate memory for PMECC computation!\n");
1088 goto err_pmecc_data_alloc;
1089 }
1090
1091 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1092 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1093
1094 atmel_pmecc_core_init(mtd);
1095
1096 return 0;
1097
1098err_pmecc_data_alloc:
1099err_no_ecc_room:
1100err_pmloc_ioremap:
1101 iounmap(host->ecc);
1102 if (host->pmerrloc_base)
1103 iounmap(host->pmerrloc_base);
1104 if (host->pmecc_rom_base)
1105 iounmap(host->pmecc_rom_base);
1106err_pmecc_ioremap:
1107 return err_no;
1108}
1109
1110/*
Richard Genoud77f54922008-04-23 19:51:14 +02001111 * Calculate HW ECC
1112 *
1113 * function called after a write
1114 *
1115 * mtd: MTD block structure
1116 * dat: raw data (unused)
1117 * ecc_code: buffer for ECC
1118 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001119static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001120 const u_char *dat, unsigned char *ecc_code)
1121{
1122 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001123 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001124 unsigned int ecc_value;
1125
1126 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001127 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001128
Richard Genoud3fc23892008-10-12 08:42:28 +02001129 ecc_code[0] = ecc_value & 0xFF;
1130 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001131
1132 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001133 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001134
Richard Genoud3fc23892008-10-12 08:42:28 +02001135 ecc_code[2] = ecc_value & 0xFF;
1136 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001137
1138 return 0;
1139}
1140
1141/*
1142 * HW ECC read page function
1143 *
1144 * mtd: mtd info structure
1145 * chip: nand chip info structure
1146 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001147 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001148 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001149static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1150 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001151{
1152 int eccsize = chip->ecc.size;
1153 int eccbytes = chip->ecc.bytes;
1154 uint32_t *eccpos = chip->ecc.layout->eccpos;
1155 uint8_t *p = buf;
1156 uint8_t *oob = chip->oob_poi;
1157 uint8_t *ecc_pos;
1158 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001159 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001160
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001161 /*
1162 * Errata: ALE is incorrectly wired up to the ECC controller
1163 * on the AP7000, so it will include the address cycles in the
1164 * ECC calculation.
1165 *
1166 * Workaround: Reset the parity registers before reading the
1167 * actual data.
1168 */
Josh Wu71b94e22013-05-09 15:34:54 +08001169 struct atmel_nand_host *host = chip->priv;
1170 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001171 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001172
Richard Genoud77f54922008-04-23 19:51:14 +02001173 /* read the page */
1174 chip->read_buf(mtd, p, eccsize);
1175
1176 /* move to ECC position if needed */
1177 if (eccpos[0] != 0) {
1178 /* This only works on large pages
1179 * because the ECC controller waits for
1180 * NAND_CMD_RNDOUTSTART after the
1181 * NAND_CMD_RNDOUT.
1182 * anyway, for small pages, the eccpos[0] == 0
1183 */
1184 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1185 mtd->writesize + eccpos[0], -1);
1186 }
1187
1188 /* the ECC controller needs to read the ECC just after the data */
1189 ecc_pos = oob + eccpos[0];
1190 chip->read_buf(mtd, ecc_pos, eccbytes);
1191
1192 /* check if there's an error */
1193 stat = chip->ecc.correct(mtd, p, oob, NULL);
1194
Mike Dunn3f91e942012-04-25 12:06:09 -07001195 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001196 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001197 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001198 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001199 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1200 }
Richard Genoud77f54922008-04-23 19:51:14 +02001201
1202 /* get back to oob start (end of page) */
1203 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1204
1205 /* read the oob */
1206 chip->read_buf(mtd, oob, mtd->oobsize);
1207
Mike Dunn3f91e942012-04-25 12:06:09 -07001208 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001209}
1210
1211/*
1212 * HW ECC Correction
1213 *
1214 * function called after a read
1215 *
1216 * mtd: MTD block structure
1217 * dat: raw data read from the chip
1218 * read_ecc: ECC from the chip (unused)
1219 * isnull: unused
1220 *
1221 * Detect and correct a 1 bit error for a page
1222 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001223static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001224 u_char *read_ecc, u_char *isnull)
1225{
1226 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001227 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001228 unsigned int ecc_status;
1229 unsigned int ecc_word, ecc_bit;
1230
1231 /* get the status from the Status Register */
1232 ecc_status = ecc_readl(host->ecc, SR);
1233
1234 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001235 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001236 return 0;
1237
1238 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001239 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001240 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001241 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001242 ecc_word >>= 4;
1243
1244 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001245 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001246 /* check if it is a freshly erased block
1247 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001248 if ((ecc_bit == ATMEL_ECC_BITADDR)
1249 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001250 /* the block has just been erased, return OK */
1251 return 0;
1252 }
1253 /* it doesn't seems to be a freshly
1254 * erased block.
1255 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001256 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001257 " Unable to correct.\n");
1258 return -EIO;
1259 }
1260
1261 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001262 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001263 /* there's nothing much to do here.
1264 * the bit error is on the ECC itself.
1265 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001266 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001267 " Nothing to correct\n");
1268 return 0;
1269 }
1270
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001271 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001272 " (word offset in the page :"
1273 " 0x%x bit offset : 0x%x)\n",
1274 ecc_word, ecc_bit);
1275 /* correct the error */
1276 if (nand_chip->options & NAND_BUSWIDTH_16) {
1277 /* 16 bits words */
1278 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1279 } else {
1280 /* 8 bits words */
1281 dat[ecc_word] ^= (1 << ecc_bit);
1282 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001283 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001284 return 1;
1285}
1286
1287/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001288 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001289 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001290static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1291{
Josh Wu71b94e22013-05-09 15:34:54 +08001292 struct nand_chip *nand_chip = mtd->priv;
1293 struct atmel_nand_host *host = nand_chip->priv;
1294
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
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001299#if defined(CONFIG_OF)
Bill Pemberton06f25512012-11-19 13:23:07 -05001300static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001301 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001302{
Josh Wuc0cf7872013-01-23 20:47:08 +08001303 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001304 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001305 int ecc_mode;
1306 struct atmel_nand_data *board = &host->board;
1307 enum of_gpio_flags flags;
1308
1309 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1310 if (val >= 32) {
1311 dev_err(host->dev, "invalid addr-offset %u\n", val);
1312 return -EINVAL;
1313 }
1314 board->ale = val;
1315 }
1316
1317 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1318 if (val >= 32) {
1319 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1320 return -EINVAL;
1321 }
1322 board->cle = val;
1323 }
1324
1325 ecc_mode = of_get_nand_ecc_mode(np);
1326
1327 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1328
1329 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1330
Josh Wu1b719262013-05-09 15:34:55 +08001331 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1332
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001333 if (of_get_nand_bus_width(np) == 16)
1334 board->bus_width_16 = 1;
1335
1336 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1337 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1338
1339 board->enable_pin = of_get_gpio(np, 1);
1340 board->det_pin = of_get_gpio(np, 2);
1341
Josh Wua41b51a2012-06-29 17:47:54 +08001342 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1343
1344 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1345 return 0; /* Not using PMECC */
1346
1347 /* use PMECC, get correction capability, sector size and lookup
1348 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001349 * If correction bits and sector size are not specified, then find
1350 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001351 */
Josh Wue66b4312013-01-23 20:47:11 +08001352 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1353 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1354 (val != 24)) {
1355 dev_err(host->dev,
1356 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1357 val);
1358 return -EINVAL;
1359 }
1360 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001361 }
Josh Wua41b51a2012-06-29 17:47:54 +08001362
Josh Wue66b4312013-01-23 20:47:11 +08001363 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1364 if ((val != 512) && (val != 1024)) {
1365 dev_err(host->dev,
1366 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1367 val);
1368 return -EINVAL;
1369 }
1370 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001371 }
Josh Wua41b51a2012-06-29 17:47:54 +08001372
1373 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1374 offset, 2) != 0) {
1375 dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
1376 return -EINVAL;
1377 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001378 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001379 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1380 return -EINVAL;
1381 }
Josh Wue66b4312013-01-23 20:47:11 +08001382 host->pmecc_lookup_table_offset_512 = offset[0];
1383 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001384
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001385 return 0;
1386}
1387#else
Bill Pemberton06f25512012-11-19 13:23:07 -05001388static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001389 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001390{
1391 return -EINVAL;
1392}
1393#endif
1394
Josh Wu3dfe41a2012-06-25 18:07:43 +08001395static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
1396 struct atmel_nand_host *host)
1397{
1398 struct mtd_info *mtd = &host->mtd;
1399 struct nand_chip *nand_chip = &host->nand_chip;
1400 struct resource *regs;
1401
1402 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1403 if (!regs) {
1404 dev_err(host->dev,
1405 "Can't get I/O resource regs, use software ECC\n");
1406 nand_chip->ecc.mode = NAND_ECC_SOFT;
1407 return 0;
1408 }
1409
1410 host->ecc = ioremap(regs->start, resource_size(regs));
1411 if (host->ecc == NULL) {
1412 dev_err(host->dev, "ioremap failed\n");
1413 return -EIO;
1414 }
1415
1416 /* ECC is calculated for the whole page (1 step) */
1417 nand_chip->ecc.size = mtd->writesize;
1418
1419 /* set ECC page size and oob layout */
1420 switch (mtd->writesize) {
1421 case 512:
1422 nand_chip->ecc.layout = &atmel_oobinfo_small;
1423 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1424 break;
1425 case 1024:
1426 nand_chip->ecc.layout = &atmel_oobinfo_large;
1427 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1428 break;
1429 case 2048:
1430 nand_chip->ecc.layout = &atmel_oobinfo_large;
1431 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1432 break;
1433 case 4096:
1434 nand_chip->ecc.layout = &atmel_oobinfo_large;
1435 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1436 break;
1437 default:
1438 /* page size not handled by HW ECC */
1439 /* switching back to soft ECC */
1440 nand_chip->ecc.mode = NAND_ECC_SOFT;
1441 return 0;
1442 }
1443
1444 /* set up for HW ECC */
1445 nand_chip->ecc.calculate = atmel_nand_calculate;
1446 nand_chip->ecc.correct = atmel_nand_correct;
1447 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1448 nand_chip->ecc.read_page = atmel_nand_read_page;
1449 nand_chip->ecc.bytes = 4;
1450 nand_chip->ecc.strength = 1;
1451
1452 return 0;
1453}
1454
Andrew Victor42cb1402006-10-19 18:24:35 +02001455/*
1456 * Probe for the NAND device.
1457 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001458static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02001459{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001460 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02001461 struct mtd_info *mtd;
1462 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001463 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001464 struct mtd_part_parser_data ppdata = {};
Andrew Victor42cb1402006-10-19 18:24:35 +02001465 int res;
Andrew Victor42cb1402006-10-19 18:24:35 +02001466
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001467 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1468 if (!mem) {
1469 printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
1470 return -ENXIO;
1471 }
1472
Andrew Victor42cb1402006-10-19 18:24:35 +02001473 /* Allocate memory for the device structure (and zero it) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001474 host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +02001475 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001476 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +02001477 return -ENOMEM;
1478 }
1479
Hong Xucbc6c5e2011-01-18 14:36:05 +08001480 host->io_phys = (dma_addr_t)mem->start;
1481
Joe Perches28f65c112011-06-09 09:13:32 -07001482 host->io_base = ioremap(mem->start, resource_size(mem));
Andrew Victor42cb1402006-10-19 18:24:35 +02001483 if (host->io_base == NULL) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001484 printk(KERN_ERR "atmel_nand: ioremap failed\n");
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001485 res = -EIO;
1486 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02001487 }
1488
1489 mtd = &host->mtd;
1490 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001491 host->dev = &pdev->dev;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001492 if (pdev->dev.of_node) {
1493 res = atmel_of_init_port(host, pdev->dev.of_node);
1494 if (res)
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001495 goto err_ecc_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001496 } else {
1497 memcpy(&host->board, pdev->dev.platform_data,
1498 sizeof(struct atmel_nand_data));
1499 }
Andrew Victor42cb1402006-10-19 18:24:35 +02001500
1501 nand_chip->priv = host; /* link the private data structures */
1502 mtd->priv = nand_chip;
1503 mtd->owner = THIS_MODULE;
1504
1505 /* Set address of NAND IO lines */
1506 nand_chip->IO_ADDR_R = host->io_base;
1507 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001508 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +03001509
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001510 if (gpio_is_valid(host->board.rdy_pin)) {
1511 res = gpio_request(host->board.rdy_pin, "nand_rdy");
1512 if (res < 0) {
1513 dev_err(&pdev->dev,
1514 "can't request rdy gpio %d\n",
1515 host->board.rdy_pin);
1516 goto err_ecc_ioremap;
1517 }
1518
1519 res = gpio_direction_input(host->board.rdy_pin);
1520 if (res < 0) {
1521 dev_err(&pdev->dev,
1522 "can't request input direction rdy gpio %d\n",
1523 host->board.rdy_pin);
1524 goto err_ecc_ioremap;
1525 }
1526
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001527 nand_chip->dev_ready = atmel_nand_device_ready;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001528 }
1529
1530 if (gpio_is_valid(host->board.enable_pin)) {
1531 res = gpio_request(host->board.enable_pin, "nand_enable");
1532 if (res < 0) {
1533 dev_err(&pdev->dev,
1534 "can't request enable gpio %d\n",
1535 host->board.enable_pin);
1536 goto err_ecc_ioremap;
1537 }
1538
1539 res = gpio_direction_output(host->board.enable_pin, 1);
1540 if (res < 0) {
1541 dev_err(&pdev->dev,
1542 "can't request output direction enable gpio %d\n",
1543 host->board.enable_pin);
1544 goto err_ecc_ioremap;
1545 }
1546 }
Ivan Kutena4265f82007-05-24 14:35:58 +03001547
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001548 nand_chip->ecc.mode = host->board.ecc_mode;
Andrew Victor42cb1402006-10-19 18:24:35 +02001549 nand_chip->chip_delay = 20; /* 20us command delay time */
1550
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001551 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02001552 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08001553
1554 nand_chip->read_buf = atmel_read_buf;
1555 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02001556
Andrew Victor42cb1402006-10-19 18:24:35 +02001557 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001558 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02001559
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001560 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001561 res = gpio_request(host->board.det_pin, "nand_det");
1562 if (res < 0) {
1563 dev_err(&pdev->dev,
1564 "can't request det gpio %d\n",
1565 host->board.det_pin);
1566 goto err_no_card;
1567 }
1568
1569 res = gpio_direction_input(host->board.det_pin);
1570 if (res < 0) {
1571 dev_err(&pdev->dev,
1572 "can't request input direction det gpio %d\n",
1573 host->board.det_pin);
1574 goto err_no_card;
1575 }
1576
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001577 if (gpio_get_value(host->board.det_pin)) {
Simon Polettef4fa697c2009-05-27 18:19:39 +03001578 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01001579 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001580 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02001581 }
1582 }
1583
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001584 if (host->board.on_flash_bbt || on_flash_bbt) {
Simon Polettef4fa697c2009-05-27 18:19:39 +03001585 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001586 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa697c2009-05-27 18:19:39 +03001587 }
1588
Josh Wu1b719262013-05-09 15:34:55 +08001589 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08001590 use_dma = 0;
1591
1592 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08001593 dma_cap_mask_t mask;
1594
1595 dma_cap_zero(mask);
1596 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02001597 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08001598 if (!host->dma_chan) {
1599 dev_err(host->dev, "Failed to request DMA channel\n");
1600 use_dma = 0;
1601 }
1602 }
1603 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08001604 dev_info(host->dev, "Using %s for DMA transfers.\n",
1605 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08001606 else
1607 dev_info(host->dev, "No DMA support for NAND access.\n");
1608
Richard Genoud77f54922008-04-23 19:51:14 +02001609 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00001610 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02001611 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001612 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02001613 }
1614
Richard Genoud3fc23892008-10-12 08:42:28 +02001615 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001616 if (host->has_pmecc)
1617 res = atmel_pmecc_nand_init_params(pdev, host);
1618 else
1619 res = atmel_hw_nand_init_params(pdev, host);
1620
Josh Wu3dfe41a2012-06-25 18:07:43 +08001621 if (res != 0)
1622 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02001623 }
1624
1625 /* second phase scan */
1626 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02001627 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001628 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02001629 }
1630
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001631 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001632 ppdata.of_node = pdev->dev.of_node;
1633 res = mtd_device_parse_register(mtd, NULL, &ppdata,
1634 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02001635 if (!res)
1636 return res;
1637
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001638err_scan_tail:
Josh Wu1c7b8742012-06-29 17:47:55 +08001639 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
1640 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1641 pmecc_data_free(host);
1642 }
Josh Wu3dfe41a2012-06-25 18:07:43 +08001643 if (host->ecc)
1644 iounmap(host->ecc);
Josh Wu1c7b8742012-06-29 17:47:55 +08001645 if (host->pmerrloc_base)
1646 iounmap(host->pmerrloc_base);
1647 if (host->pmecc_rom_base)
1648 iounmap(host->pmecc_rom_base);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001649err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001650err_scan_ident:
1651err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001652 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08001653 if (host->dma_chan)
1654 dma_release_channel(host->dma_chan);
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001655err_ecc_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02001656 iounmap(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001657err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02001658 kfree(host);
1659 return res;
1660}
1661
1662/*
1663 * Remove a NAND device.
1664 */
David Brownell23a346c2008-07-03 23:40:16 -07001665static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02001666{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001667 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +02001668 struct mtd_info *mtd = &host->mtd;
1669
1670 nand_release(mtd);
1671
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001672 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02001673
Josh Wu1c7b8742012-06-29 17:47:55 +08001674 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
1675 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1676 pmerrloc_writel(host->pmerrloc_base, ELDIS,
1677 PMERRLOC_DISABLE);
1678 pmecc_data_free(host);
1679 }
1680
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001681 if (gpio_is_valid(host->board.det_pin))
1682 gpio_free(host->board.det_pin);
1683
1684 if (gpio_is_valid(host->board.enable_pin))
1685 gpio_free(host->board.enable_pin);
1686
1687 if (gpio_is_valid(host->board.rdy_pin))
1688 gpio_free(host->board.rdy_pin);
1689
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001690 if (host->ecc)
1691 iounmap(host->ecc);
Josh Wu1c7b8742012-06-29 17:47:55 +08001692 if (host->pmecc_rom_base)
1693 iounmap(host->pmecc_rom_base);
1694 if (host->pmerrloc_base)
1695 iounmap(host->pmerrloc_base);
Hong Xucbc6c5e2011-01-18 14:36:05 +08001696
1697 if (host->dma_chan)
1698 dma_release_channel(host->dma_chan);
1699
Andrew Victor42cb1402006-10-19 18:24:35 +02001700 iounmap(host->io_base);
1701 kfree(host);
1702
1703 return 0;
1704}
1705
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001706#if defined(CONFIG_OF)
1707static const struct of_device_id atmel_nand_dt_ids[] = {
1708 { .compatible = "atmel,at91rm9200-nand" },
1709 { /* sentinel */ }
1710};
1711
1712MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
1713#endif
1714
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001715static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -07001716 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +02001717 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001718 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +02001719 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001720 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02001721 },
1722};
1723
Jingoo Hanc5345ed2013-03-05 13:30:04 +09001724module_platform_driver_probe(atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +02001725
1726MODULE_LICENSE("GPL");
1727MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02001728MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001729MODULE_ALIAS("platform:atmel_nand");