blob: 28d159a7706f0e639e0a37d8d35f8cfe60bf8cc7 [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
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800749static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
750{
751 u32 val;
752
753 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
754 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
755 val = pmecc_readl_relaxed(host->ecc, CFG);
756
757 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
758 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
759 return;
760 }
761
762 if (ecc_op == NAND_ECC_READ)
763 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
764 | PMECC_CFG_AUTO_ENABLE);
765 else
766 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
767 & ~PMECC_CFG_AUTO_ENABLE);
768
769 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
770 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
771}
772
Josh Wu1c7b8742012-06-29 17:47:55 +0800773static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
774 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
775{
776 struct atmel_nand_host *host = chip->priv;
777 int eccsize = chip->ecc.size;
778 uint8_t *oob = chip->oob_poi;
779 uint32_t *eccpos = chip->ecc.layout->eccpos;
780 uint32_t stat;
781 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800782 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800783
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800784 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800785
786 chip->read_buf(mtd, buf, eccsize);
787 chip->read_buf(mtd, oob, mtd->oobsize);
788
789 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
790 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
791 if (unlikely(time_after(jiffies, end_time))) {
792 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
793 return -EIO;
794 }
795 cpu_relax();
796 }
797
798 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800799 if (stat != 0) {
800 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
801 if (bitflips < 0)
802 /* uncorrectable errors */
803 return 0;
804 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800805
Josh Wuc0c70d92012-11-27 18:50:31 +0800806 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800807}
808
809static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
810 struct nand_chip *chip, const uint8_t *buf, int oob_required)
811{
812 struct atmel_nand_host *host = chip->priv;
813 uint32_t *eccpos = chip->ecc.layout->eccpos;
814 int i, j;
815 unsigned long end_time;
816
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800817 pmecc_enable(host, NAND_ECC_WRITE);
Josh Wu1c7b8742012-06-29 17:47:55 +0800818
819 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
820
821 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
822 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
823 if (unlikely(time_after(jiffies, end_time))) {
824 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
825 return -EIO;
826 }
827 cpu_relax();
828 }
829
830 for (i = 0; i < host->pmecc_sector_number; i++) {
831 for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
832 int pos;
833
834 pos = i * host->pmecc_bytes_per_sector + j;
835 chip->oob_poi[eccpos[pos]] =
836 pmecc_readb_ecc_relaxed(host->ecc, i, j);
837 }
838 }
839 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
840
841 return 0;
842}
843
844static void atmel_pmecc_core_init(struct mtd_info *mtd)
845{
846 struct nand_chip *nand_chip = mtd->priv;
847 struct atmel_nand_host *host = nand_chip->priv;
848 uint32_t val = 0;
849 struct nand_ecclayout *ecc_layout;
850
851 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
852 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
853
854 switch (host->pmecc_corr_cap) {
855 case 2:
856 val = PMECC_CFG_BCH_ERR2;
857 break;
858 case 4:
859 val = PMECC_CFG_BCH_ERR4;
860 break;
861 case 8:
862 val = PMECC_CFG_BCH_ERR8;
863 break;
864 case 12:
865 val = PMECC_CFG_BCH_ERR12;
866 break;
867 case 24:
868 val = PMECC_CFG_BCH_ERR24;
869 break;
870 }
871
872 if (host->pmecc_sector_size == 512)
873 val |= PMECC_CFG_SECTOR512;
874 else if (host->pmecc_sector_size == 1024)
875 val |= PMECC_CFG_SECTOR1024;
876
877 switch (host->pmecc_sector_number) {
878 case 1:
879 val |= PMECC_CFG_PAGE_1SECTOR;
880 break;
881 case 2:
882 val |= PMECC_CFG_PAGE_2SECTORS;
883 break;
884 case 4:
885 val |= PMECC_CFG_PAGE_4SECTORS;
886 break;
887 case 8:
888 val |= PMECC_CFG_PAGE_8SECTORS;
889 break;
890 }
891
892 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
893 | PMECC_CFG_AUTO_DISABLE);
894 pmecc_writel(host->ecc, CFG, val);
895
896 ecc_layout = nand_chip->ecc.layout;
897 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
898 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
899 pmecc_writel(host->ecc, EADDR,
900 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
901 /* See datasheet about PMECC Clock Control Register */
902 pmecc_writel(host->ecc, CLK, 2);
903 pmecc_writel(host->ecc, IDR, 0xff);
904 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
905}
906
Josh Wu84cfbbb2013-01-23 20:47:12 +0800907/*
908 * Get ECC requirement in ONFI parameters, returns -1 if ONFI
909 * parameters is not supported.
910 * return 0 if success to get the ECC requirement.
911 */
912static int get_onfi_ecc_param(struct nand_chip *chip,
913 int *ecc_bits, int *sector_size)
914{
915 *ecc_bits = *sector_size = 0;
916
917 if (chip->onfi_params.ecc_bits == 0xff)
918 /* TODO: the sector_size and ecc_bits need to be find in
919 * extended ecc parameter, currently we don't support it.
920 */
921 return -1;
922
923 *ecc_bits = chip->onfi_params.ecc_bits;
924
925 /* The default sector size (ecc codeword size) is 512 */
926 *sector_size = 512;
927
928 return 0;
929}
930
931/*
932 * Get ecc requirement from ONFI parameters ecc requirement.
933 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
934 * will set them according to ONFI ecc requirement. Otherwise, use the
935 * value in DTS file.
936 * return 0 if success. otherwise return error code.
937 */
938static int pmecc_choose_ecc(struct atmel_nand_host *host,
939 int *cap, int *sector_size)
940{
941 /* Get ECC requirement from ONFI parameters */
942 *cap = *sector_size = 0;
943 if (host->nand_chip.onfi_version) {
944 if (!get_onfi_ecc_param(&host->nand_chip, cap, sector_size))
945 dev_info(host->dev, "ONFI params, minimum required ECC: %d bits in %d bytes\n",
946 *cap, *sector_size);
947 else
948 dev_info(host->dev, "NAND chip ECC reqirement is in Extended ONFI parameter, we don't support yet.\n");
949 } else {
950 dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes");
951 }
952 if (*cap == 0 && *sector_size == 0) {
953 *cap = 2;
954 *sector_size = 512;
955 }
956
957 /* If dts file doesn't specify then use the one in ONFI parameters */
958 if (host->pmecc_corr_cap == 0) {
959 /* use the most fitable ecc bits (the near bigger one ) */
960 if (*cap <= 2)
961 host->pmecc_corr_cap = 2;
962 else if (*cap <= 4)
963 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +0800964 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +0800965 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +0800966 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +0800967 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +0800968 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +0800969 host->pmecc_corr_cap = 24;
970 else
971 return -EINVAL;
972 }
973 if (host->pmecc_sector_size == 0) {
974 /* use the most fitable sector size (the near smaller one ) */
975 if (*sector_size >= 1024)
976 host->pmecc_sector_size = 1024;
977 else if (*sector_size >= 512)
978 host->pmecc_sector_size = 512;
979 else
980 return -EINVAL;
981 }
982 return 0;
983}
984
Josh Wu1c7b8742012-06-29 17:47:55 +0800985static int __init atmel_pmecc_nand_init_params(struct platform_device *pdev,
986 struct atmel_nand_host *host)
987{
988 struct mtd_info *mtd = &host->mtd;
989 struct nand_chip *nand_chip = &host->nand_chip;
990 struct resource *regs, *regs_pmerr, *regs_rom;
991 int cap, sector_size, err_no;
992
Josh Wu84cfbbb2013-01-23 20:47:12 +0800993 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
994 if (err_no) {
995 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
996 return err_no;
997 }
998
Richard Genoudf666d642013-07-30 17:17:29 +0200999 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001000 sector_size != host->pmecc_sector_size)
1001 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001002
Josh Wu1c7b8742012-06-29 17:47:55 +08001003 cap = host->pmecc_corr_cap;
1004 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001005 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1006 host->pmecc_lookup_table_offset_512 :
1007 host->pmecc_lookup_table_offset_1024;
1008
Josh Wu1c7b8742012-06-29 17:47:55 +08001009 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1010 cap, sector_size);
1011
1012 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1013 if (!regs) {
1014 dev_warn(host->dev,
1015 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1016 nand_chip->ecc.mode = NAND_ECC_SOFT;
1017 return 0;
1018 }
1019
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001020 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1021 if (IS_ERR(host->ecc)) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001022 dev_err(host->dev, "ioremap failed\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001023 err_no = PTR_ERR(host->ecc);
1024 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001025 }
1026
1027 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001028 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1029 if (IS_ERR(host->pmerrloc_base)) {
1030 dev_err(host->dev,
1031 "Can not get I/O resource for PMECC ERRLOC controller!\n");
1032 err_no = PTR_ERR(host->pmerrloc_base);
1033 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001034 }
1035
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001036 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1037 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, regs_rom);
1038 if (IS_ERR(host->pmecc_rom_base)) {
1039 dev_err(host->dev, "Can not get I/O resource for ROM!\n");
1040 err_no = PTR_ERR(host->pmecc_rom_base);
1041 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001042 }
1043
1044 /* ECC is calculated for the whole page (1 step) */
1045 nand_chip->ecc.size = mtd->writesize;
1046
1047 /* set ECC page size and oob layout */
1048 switch (mtd->writesize) {
1049 case 2048:
1050 host->pmecc_degree = PMECC_GF_DIMENSION_13;
1051 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
1052 host->pmecc_sector_number = mtd->writesize / sector_size;
1053 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
1054 cap, sector_size);
1055 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1056 host->pmecc_index_of = host->pmecc_rom_base +
1057 host->pmecc_lookup_table_offset;
1058
1059 nand_chip->ecc.steps = 1;
1060 nand_chip->ecc.strength = cap;
1061 nand_chip->ecc.bytes = host->pmecc_bytes_per_sector *
1062 host->pmecc_sector_number;
1063 if (nand_chip->ecc.bytes > mtd->oobsize - 2) {
1064 dev_err(host->dev, "No room for ECC bytes\n");
1065 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001066 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001067 }
1068 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1069 mtd->oobsize,
1070 nand_chip->ecc.bytes);
1071 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1072 break;
1073 case 512:
1074 case 1024:
1075 case 4096:
1076 /* TODO */
1077 dev_warn(host->dev,
1078 "Unsupported page size for PMECC, use Software ECC\n");
1079 default:
1080 /* page size not handled by HW ECC */
1081 /* switching back to soft ECC */
1082 nand_chip->ecc.mode = NAND_ECC_SOFT;
1083 return 0;
1084 }
1085
1086 /* Allocate data for PMECC computation */
1087 err_no = pmecc_data_alloc(host);
1088 if (err_no) {
1089 dev_err(host->dev,
1090 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001091 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001092 }
1093
1094 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1095 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1096
1097 atmel_pmecc_core_init(mtd);
1098
1099 return 0;
1100
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001101err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001102 return err_no;
1103}
1104
1105/*
Richard Genoud77f54922008-04-23 19:51:14 +02001106 * Calculate HW ECC
1107 *
1108 * function called after a write
1109 *
1110 * mtd: MTD block structure
1111 * dat: raw data (unused)
1112 * ecc_code: buffer for ECC
1113 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001114static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001115 const u_char *dat, unsigned char *ecc_code)
1116{
1117 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001118 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001119 unsigned int ecc_value;
1120
1121 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001122 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001123
Richard Genoud3fc23892008-10-12 08:42:28 +02001124 ecc_code[0] = ecc_value & 0xFF;
1125 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001126
1127 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001128 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001129
Richard Genoud3fc23892008-10-12 08:42:28 +02001130 ecc_code[2] = ecc_value & 0xFF;
1131 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001132
1133 return 0;
1134}
1135
1136/*
1137 * HW ECC read page function
1138 *
1139 * mtd: mtd info structure
1140 * chip: nand chip info structure
1141 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001142 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001143 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001144static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1145 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001146{
1147 int eccsize = chip->ecc.size;
1148 int eccbytes = chip->ecc.bytes;
1149 uint32_t *eccpos = chip->ecc.layout->eccpos;
1150 uint8_t *p = buf;
1151 uint8_t *oob = chip->oob_poi;
1152 uint8_t *ecc_pos;
1153 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001154 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001155
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001156 /*
1157 * Errata: ALE is incorrectly wired up to the ECC controller
1158 * on the AP7000, so it will include the address cycles in the
1159 * ECC calculation.
1160 *
1161 * Workaround: Reset the parity registers before reading the
1162 * actual data.
1163 */
Josh Wu71b94e22013-05-09 15:34:54 +08001164 struct atmel_nand_host *host = chip->priv;
1165 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001166 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001167
Richard Genoud77f54922008-04-23 19:51:14 +02001168 /* read the page */
1169 chip->read_buf(mtd, p, eccsize);
1170
1171 /* move to ECC position if needed */
1172 if (eccpos[0] != 0) {
1173 /* This only works on large pages
1174 * because the ECC controller waits for
1175 * NAND_CMD_RNDOUTSTART after the
1176 * NAND_CMD_RNDOUT.
1177 * anyway, for small pages, the eccpos[0] == 0
1178 */
1179 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1180 mtd->writesize + eccpos[0], -1);
1181 }
1182
1183 /* the ECC controller needs to read the ECC just after the data */
1184 ecc_pos = oob + eccpos[0];
1185 chip->read_buf(mtd, ecc_pos, eccbytes);
1186
1187 /* check if there's an error */
1188 stat = chip->ecc.correct(mtd, p, oob, NULL);
1189
Mike Dunn3f91e942012-04-25 12:06:09 -07001190 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001191 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001192 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001193 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001194 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1195 }
Richard Genoud77f54922008-04-23 19:51:14 +02001196
1197 /* get back to oob start (end of page) */
1198 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1199
1200 /* read the oob */
1201 chip->read_buf(mtd, oob, mtd->oobsize);
1202
Mike Dunn3f91e942012-04-25 12:06:09 -07001203 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001204}
1205
1206/*
1207 * HW ECC Correction
1208 *
1209 * function called after a read
1210 *
1211 * mtd: MTD block structure
1212 * dat: raw data read from the chip
1213 * read_ecc: ECC from the chip (unused)
1214 * isnull: unused
1215 *
1216 * Detect and correct a 1 bit error for a page
1217 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001218static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001219 u_char *read_ecc, u_char *isnull)
1220{
1221 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001222 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001223 unsigned int ecc_status;
1224 unsigned int ecc_word, ecc_bit;
1225
1226 /* get the status from the Status Register */
1227 ecc_status = ecc_readl(host->ecc, SR);
1228
1229 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001230 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001231 return 0;
1232
1233 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001234 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001235 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001236 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001237 ecc_word >>= 4;
1238
1239 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001240 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001241 /* check if it is a freshly erased block
1242 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001243 if ((ecc_bit == ATMEL_ECC_BITADDR)
1244 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001245 /* the block has just been erased, return OK */
1246 return 0;
1247 }
1248 /* it doesn't seems to be a freshly
1249 * erased block.
1250 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001251 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001252 " Unable to correct.\n");
1253 return -EIO;
1254 }
1255
1256 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001257 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001258 /* there's nothing much to do here.
1259 * the bit error is on the ECC itself.
1260 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001261 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001262 " Nothing to correct\n");
1263 return 0;
1264 }
1265
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001266 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001267 " (word offset in the page :"
1268 " 0x%x bit offset : 0x%x)\n",
1269 ecc_word, ecc_bit);
1270 /* correct the error */
1271 if (nand_chip->options & NAND_BUSWIDTH_16) {
1272 /* 16 bits words */
1273 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1274 } else {
1275 /* 8 bits words */
1276 dat[ecc_word] ^= (1 << ecc_bit);
1277 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001278 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001279 return 1;
1280}
1281
1282/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001283 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001284 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001285static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1286{
Josh Wu71b94e22013-05-09 15:34:54 +08001287 struct nand_chip *nand_chip = mtd->priv;
1288 struct atmel_nand_host *host = nand_chip->priv;
1289
1290 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001291 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001292}
Richard Genoud77f54922008-04-23 19:51:14 +02001293
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001294#if defined(CONFIG_OF)
Bill Pemberton06f25512012-11-19 13:23:07 -05001295static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001296 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001297{
Josh Wuc0cf7872013-01-23 20:47:08 +08001298 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001299 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001300 int ecc_mode;
1301 struct atmel_nand_data *board = &host->board;
1302 enum of_gpio_flags flags;
1303
1304 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1305 if (val >= 32) {
1306 dev_err(host->dev, "invalid addr-offset %u\n", val);
1307 return -EINVAL;
1308 }
1309 board->ale = val;
1310 }
1311
1312 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1313 if (val >= 32) {
1314 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1315 return -EINVAL;
1316 }
1317 board->cle = val;
1318 }
1319
1320 ecc_mode = of_get_nand_ecc_mode(np);
1321
1322 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1323
1324 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1325
Josh Wu1b719262013-05-09 15:34:55 +08001326 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1327
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001328 if (of_get_nand_bus_width(np) == 16)
1329 board->bus_width_16 = 1;
1330
1331 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1332 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1333
1334 board->enable_pin = of_get_gpio(np, 1);
1335 board->det_pin = of_get_gpio(np, 2);
1336
Josh Wua41b51a2012-06-29 17:47:54 +08001337 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1338
1339 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1340 return 0; /* Not using PMECC */
1341
1342 /* use PMECC, get correction capability, sector size and lookup
1343 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001344 * If correction bits and sector size are not specified, then find
1345 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001346 */
Josh Wue66b4312013-01-23 20:47:11 +08001347 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1348 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1349 (val != 24)) {
1350 dev_err(host->dev,
1351 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1352 val);
1353 return -EINVAL;
1354 }
1355 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001356 }
Josh Wua41b51a2012-06-29 17:47:54 +08001357
Josh Wue66b4312013-01-23 20:47:11 +08001358 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1359 if ((val != 512) && (val != 1024)) {
1360 dev_err(host->dev,
1361 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1362 val);
1363 return -EINVAL;
1364 }
1365 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001366 }
Josh Wua41b51a2012-06-29 17:47:54 +08001367
1368 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1369 offset, 2) != 0) {
1370 dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
1371 return -EINVAL;
1372 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001373 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001374 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1375 return -EINVAL;
1376 }
Josh Wue66b4312013-01-23 20:47:11 +08001377 host->pmecc_lookup_table_offset_512 = offset[0];
1378 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001379
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001380 return 0;
1381}
1382#else
Bill Pemberton06f25512012-11-19 13:23:07 -05001383static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001384 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001385{
1386 return -EINVAL;
1387}
1388#endif
1389
Josh Wu3dfe41a2012-06-25 18:07:43 +08001390static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
1391 struct atmel_nand_host *host)
1392{
1393 struct mtd_info *mtd = &host->mtd;
1394 struct nand_chip *nand_chip = &host->nand_chip;
1395 struct resource *regs;
1396
1397 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1398 if (!regs) {
1399 dev_err(host->dev,
1400 "Can't get I/O resource regs, use software ECC\n");
1401 nand_chip->ecc.mode = NAND_ECC_SOFT;
1402 return 0;
1403 }
1404
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001405 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1406 if (IS_ERR(host->ecc)) {
Josh Wu3dfe41a2012-06-25 18:07:43 +08001407 dev_err(host->dev, "ioremap failed\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001408 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001409 }
1410
1411 /* ECC is calculated for the whole page (1 step) */
1412 nand_chip->ecc.size = mtd->writesize;
1413
1414 /* set ECC page size and oob layout */
1415 switch (mtd->writesize) {
1416 case 512:
1417 nand_chip->ecc.layout = &atmel_oobinfo_small;
1418 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1419 break;
1420 case 1024:
1421 nand_chip->ecc.layout = &atmel_oobinfo_large;
1422 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1423 break;
1424 case 2048:
1425 nand_chip->ecc.layout = &atmel_oobinfo_large;
1426 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1427 break;
1428 case 4096:
1429 nand_chip->ecc.layout = &atmel_oobinfo_large;
1430 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1431 break;
1432 default:
1433 /* page size not handled by HW ECC */
1434 /* switching back to soft ECC */
1435 nand_chip->ecc.mode = NAND_ECC_SOFT;
1436 return 0;
1437 }
1438
1439 /* set up for HW ECC */
1440 nand_chip->ecc.calculate = atmel_nand_calculate;
1441 nand_chip->ecc.correct = atmel_nand_correct;
1442 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1443 nand_chip->ecc.read_page = atmel_nand_read_page;
1444 nand_chip->ecc.bytes = 4;
1445 nand_chip->ecc.strength = 1;
1446
1447 return 0;
1448}
1449
Andrew Victor42cb1402006-10-19 18:24:35 +02001450/*
1451 * Probe for the NAND device.
1452 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001453static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02001454{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001455 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02001456 struct mtd_info *mtd;
1457 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001458 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001459 struct mtd_part_parser_data ppdata = {};
Andrew Victor42cb1402006-10-19 18:24:35 +02001460 int res;
Andrew Victor42cb1402006-10-19 18:24:35 +02001461
1462 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001463 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +02001464 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001465 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +02001466 return -ENOMEM;
1467 }
1468
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001469 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1470 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
1471 if (IS_ERR(host->io_base)) {
1472 dev_err(&pdev->dev, "atmel_nand: ioremap resource failed\n");
1473 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001474 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02001475 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001476 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02001477
1478 mtd = &host->mtd;
1479 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001480 host->dev = &pdev->dev;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001481 if (pdev->dev.of_node) {
1482 res = atmel_of_init_port(host, pdev->dev.of_node);
1483 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001484 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001485 } else {
1486 memcpy(&host->board, pdev->dev.platform_data,
1487 sizeof(struct atmel_nand_data));
1488 }
Andrew Victor42cb1402006-10-19 18:24:35 +02001489
1490 nand_chip->priv = host; /* link the private data structures */
1491 mtd->priv = nand_chip;
1492 mtd->owner = THIS_MODULE;
1493
1494 /* Set address of NAND IO lines */
1495 nand_chip->IO_ADDR_R = host->io_base;
1496 nand_chip->IO_ADDR_W = host->io_base;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001497 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +03001498
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001499 if (gpio_is_valid(host->board.rdy_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001500 res = devm_gpio_request(&pdev->dev,
1501 host->board.rdy_pin, "nand_rdy");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001502 if (res < 0) {
1503 dev_err(&pdev->dev,
1504 "can't request rdy gpio %d\n",
1505 host->board.rdy_pin);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001506 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001507 }
1508
1509 res = gpio_direction_input(host->board.rdy_pin);
1510 if (res < 0) {
1511 dev_err(&pdev->dev,
1512 "can't request input direction rdy gpio %d\n",
1513 host->board.rdy_pin);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001514 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001515 }
1516
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001517 nand_chip->dev_ready = atmel_nand_device_ready;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001518 }
1519
1520 if (gpio_is_valid(host->board.enable_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001521 res = devm_gpio_request(&pdev->dev,
1522 host->board.enable_pin, "nand_enable");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001523 if (res < 0) {
1524 dev_err(&pdev->dev,
1525 "can't request enable gpio %d\n",
1526 host->board.enable_pin);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001527 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001528 }
1529
1530 res = gpio_direction_output(host->board.enable_pin, 1);
1531 if (res < 0) {
1532 dev_err(&pdev->dev,
1533 "can't request output direction enable gpio %d\n",
1534 host->board.enable_pin);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001535 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001536 }
1537 }
Ivan Kutena4265f82007-05-24 14:35:58 +03001538
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001539 nand_chip->ecc.mode = host->board.ecc_mode;
Andrew Victor42cb1402006-10-19 18:24:35 +02001540 nand_chip->chip_delay = 20; /* 20us command delay time */
1541
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001542 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02001543 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08001544
1545 nand_chip->read_buf = atmel_read_buf;
1546 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02001547
Andrew Victor42cb1402006-10-19 18:24:35 +02001548 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001549 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02001550
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001551 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001552 res = devm_gpio_request(&pdev->dev,
1553 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001554 if (res < 0) {
1555 dev_err(&pdev->dev,
1556 "can't request det gpio %d\n",
1557 host->board.det_pin);
1558 goto err_no_card;
1559 }
1560
1561 res = gpio_direction_input(host->board.det_pin);
1562 if (res < 0) {
1563 dev_err(&pdev->dev,
1564 "can't request input direction det gpio %d\n",
1565 host->board.det_pin);
1566 goto err_no_card;
1567 }
1568
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001569 if (gpio_get_value(host->board.det_pin)) {
Simon Polettef4fa6972009-05-27 18:19:39 +03001570 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01001571 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001572 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02001573 }
1574 }
1575
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001576 if (host->board.on_flash_bbt || on_flash_bbt) {
Simon Polettef4fa6972009-05-27 18:19:39 +03001577 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001578 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa6972009-05-27 18:19:39 +03001579 }
1580
Josh Wu1b719262013-05-09 15:34:55 +08001581 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08001582 use_dma = 0;
1583
1584 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08001585 dma_cap_mask_t mask;
1586
1587 dma_cap_zero(mask);
1588 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02001589 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08001590 if (!host->dma_chan) {
1591 dev_err(host->dev, "Failed to request DMA channel\n");
1592 use_dma = 0;
1593 }
1594 }
1595 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08001596 dev_info(host->dev, "Using %s for DMA transfers.\n",
1597 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08001598 else
1599 dev_info(host->dev, "No DMA support for NAND access.\n");
1600
Richard Genoud77f54922008-04-23 19:51:14 +02001601 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00001602 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02001603 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001604 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02001605 }
1606
Richard Genoud3fc23892008-10-12 08:42:28 +02001607 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001608 if (host->has_pmecc)
1609 res = atmel_pmecc_nand_init_params(pdev, host);
1610 else
1611 res = atmel_hw_nand_init_params(pdev, host);
1612
Josh Wu3dfe41a2012-06-25 18:07:43 +08001613 if (res != 0)
1614 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02001615 }
1616
1617 /* second phase scan */
1618 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02001619 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001620 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02001621 }
1622
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001623 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001624 ppdata.of_node = pdev->dev.of_node;
1625 res = mtd_device_parse_register(mtd, NULL, &ppdata,
1626 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02001627 if (!res)
1628 return res;
1629
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001630err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001631 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08001632 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001633err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001634err_scan_ident:
1635err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001636 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08001637 if (host->dma_chan)
1638 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001639err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02001640 return res;
1641}
1642
1643/*
1644 * Remove a NAND device.
1645 */
David Brownell23a346c2008-07-03 23:40:16 -07001646static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02001647{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001648 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +02001649 struct mtd_info *mtd = &host->mtd;
1650
1651 nand_release(mtd);
1652
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001653 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02001654
Josh Wu1c7b8742012-06-29 17:47:55 +08001655 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
1656 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1657 pmerrloc_writel(host->pmerrloc_base, ELDIS,
1658 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08001659 }
1660
Hong Xucbc6c5e2011-01-18 14:36:05 +08001661 if (host->dma_chan)
1662 dma_release_channel(host->dma_chan);
1663
Andrew Victor42cb1402006-10-19 18:24:35 +02001664 return 0;
1665}
1666
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001667#if defined(CONFIG_OF)
1668static const struct of_device_id atmel_nand_dt_ids[] = {
1669 { .compatible = "atmel,at91rm9200-nand" },
1670 { /* sentinel */ }
1671};
1672
1673MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
1674#endif
1675
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001676static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -07001677 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +02001678 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001679 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +02001680 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001681 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02001682 },
1683};
1684
Jingoo Hanc5345ed2013-03-05 13:30:04 +09001685module_platform_driver_probe(atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +02001686
1687MODULE_LICENSE("GPL");
1688MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02001689MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001690MODULE_ALIAS("platform:atmel_nand");