blob: 9e0f95d8cd7702b3f9123fd670b6408bc16891c8 [file] [log] [blame]
Andrew Victor42cb1402006-10-19 18:24:35 +02001/*
Josh Wu1c7b8742012-06-29 17:47:55 +08002 * Copyright © 2003 Rick Bronson
Andrew Victor42cb1402006-10-19 18:24:35 +02003 *
4 * Derived from drivers/mtd/nand/autcpu12.c
Josh Wu1c7b8742012-06-29 17:47:55 +08005 * Copyright © 2001 Thomas Gleixner (gleixner@autronix.de)
Andrew Victor42cb1402006-10-19 18:24:35 +02006 *
7 * Derived from drivers/mtd/spia.c
Josh Wu1c7b8742012-06-29 17:47:55 +08008 * Copyright © 2000 Steven J. Hill (sjhill@cotw.com)
Andrew Victor42cb1402006-10-19 18:24:35 +02009 *
Richard Genoud77f54922008-04-23 19:51:14 +020010 *
11 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
Josh Wu1c7b8742012-06-29 17:47:55 +080012 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright © 2007
Richard Genoud77f54922008-04-23 19:51:14 +020013 *
14 * Derived from Das U-Boot source code
15 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
Josh Wu1c7b8742012-06-29 17:47:55 +080016 * © Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
Richard Genoud77f54922008-04-23 19:51:14 +020017 *
Josh Wu1c7b8742012-06-29 17:47:55 +080018 * Add Programmable Multibit ECC support for various AT91 SoC
19 * © Copyright 2012 ATMEL, Hong Xu
Richard Genoud77f54922008-04-23 19:51:14 +020020 *
Josh Wu7dc37de2013-08-05 19:14:35 +080021 * Add Nand Flash Controller support for SAMA5 SoC
22 * © Copyright 2013 ATMEL, Josh Wu (josh.wu@atmel.com)
23 *
Andrew Victor42cb1402006-10-19 18:24:35 +020024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000030#include <linux/dma-mapping.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020031#include <linux/slab.h>
32#include <linux/module.h>
Simon Polettef4fa6972009-05-27 18:19:39 +030033#include <linux/moduleparam.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020034#include <linux/platform_device.h>
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080035#include <linux/of.h>
36#include <linux/of_device.h>
37#include <linux/of_gpio.h>
38#include <linux/of_mtd.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020039#include <linux/mtd/mtd.h>
40#include <linux/mtd/nand.h>
41#include <linux/mtd/partitions.h>
42
Josh Wu7dc37de2013-08-05 19:14:35 +080043#include <linux/delay.h>
Hans-Christian Egtvedt5c39c4c2011-04-13 15:55:17 +020044#include <linux/dmaengine.h>
David Woodhouse90574d02008-06-07 08:49:00 +010045#include <linux/gpio.h>
Josh Wu7dc37de2013-08-05 19:14:35 +080046#include <linux/interrupt.h>
David Woodhouse90574d02008-06-07 08:49:00 +010047#include <linux/io.h>
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +080048#include <linux/platform_data/atmel.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020049
Hong Xucbc6c5e2011-01-18 14:36:05 +080050static int use_dma = 1;
51module_param(use_dma, int, 0);
52
Simon Polettef4fa6972009-05-27 18:19:39 +030053static int on_flash_bbt = 0;
54module_param(on_flash_bbt, int, 0);
55
Richard Genoud77f54922008-04-23 19:51:14 +020056/* Register access macros */
57#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020058 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020059#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020060 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020061
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020062#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Josh Wu7dc37de2013-08-05 19:14:35 +080063#include "atmel_nand_nfc.h" /* Nand Flash Controller definition */
Richard Genoud77f54922008-04-23 19:51:14 +020064
65/* oob layout for large page size
66 * bad block info is on bytes 0 and 1
67 * the bytes have to be consecutives to avoid
68 * several NAND_CMD_RNDOUT during read
69 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020070static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020071 .eccbytes = 4,
72 .eccpos = {60, 61, 62, 63},
73 .oobfree = {
74 {2, 58}
75 },
76};
77
78/* oob layout for small page size
79 * bad block info is on bytes 4 and 5
80 * the bytes have to be consecutives to avoid
81 * several NAND_CMD_RNDOUT during read
82 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020083static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020084 .eccbytes = 4,
85 .eccpos = {0, 1, 2, 3},
86 .oobfree = {
87 {6, 10}
88 },
89};
90
Josh Wu7dc37de2013-08-05 19:14:35 +080091struct atmel_nfc {
92 void __iomem *base_cmd_regs;
93 void __iomem *hsmc_regs;
94 void __iomem *sram_bank0;
95 dma_addr_t sram_bank0_phys;
96
97 bool is_initialized;
98 struct completion comp_nfc;
99};
100static struct atmel_nfc nand_nfc;
101
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200102struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +0200103 struct nand_chip nand_chip;
104 struct mtd_info mtd;
105 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800106 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800107 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +0200108 struct device *dev;
109 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800110
111 struct completion comp;
112 struct dma_chan *dma_chan;
Josh Wua41b51a2012-06-29 17:47:54 +0800113
Josh Wu7dc37de2013-08-05 19:14:35 +0800114 struct atmel_nfc *nfc;
115
Josh Wua41b51a2012-06-29 17:47:54 +0800116 bool has_pmecc;
117 u8 pmecc_corr_cap;
118 u16 pmecc_sector_size;
119 u32 pmecc_lookup_table_offset;
Josh Wue66b4312013-01-23 20:47:11 +0800120 u32 pmecc_lookup_table_offset_512;
121 u32 pmecc_lookup_table_offset_1024;
Josh Wu1c7b8742012-06-29 17:47:55 +0800122
123 int pmecc_bytes_per_sector;
124 int pmecc_sector_number;
125 int pmecc_degree; /* Degree of remainders */
126 int pmecc_cw_len; /* Length of codeword */
127
128 void __iomem *pmerrloc_base;
129 void __iomem *pmecc_rom_base;
130
131 /* lookup table for alpha_to and index_of */
132 void __iomem *pmecc_alpha_to;
133 void __iomem *pmecc_index_of;
134
135 /* data for pmecc computation */
136 int16_t *pmecc_partial_syn;
137 int16_t *pmecc_si;
138 int16_t *pmecc_smu; /* Sigma table */
139 int16_t *pmecc_lmu; /* polynomal order */
140 int *pmecc_mu;
141 int *pmecc_dmu;
142 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200143};
144
Josh Wu1c7b8742012-06-29 17:47:55 +0800145static struct nand_ecclayout atmel_pmecc_oobinfo;
146
Andrew Victor42cb1402006-10-19 18:24:35 +0200147/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900148 * Enable NAND.
149 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200150static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900151{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800152 if (gpio_is_valid(host->board.enable_pin))
153 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900154}
155
156/*
157 * Disable NAND.
158 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200159static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900160{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800161 if (gpio_is_valid(host->board.enable_pin))
162 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900163}
164
165/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200166 * Hardware specific access to control-lines
167 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200168static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200169{
170 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200171 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200172
Atsushi Nemoto81365082008-04-27 01:51:12 +0900173 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900174 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200175 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900176 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200177 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900178 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200179 if (cmd == NAND_CMD_NONE)
180 return;
181
182 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800183 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200184 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800185 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200186}
187
188/*
189 * Read the Device Ready pin.
190 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200191static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200192{
193 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200194 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200195
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800196 return gpio_get_value(host->board.rdy_pin) ^
197 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200198}
199
Josh Wu7dc37de2013-08-05 19:14:35 +0800200/* Set up for hardware ready pin and enable pin. */
201static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
202{
203 struct nand_chip *chip = mtd->priv;
204 struct atmel_nand_host *host = chip->priv;
205 int res = 0;
206
207 if (gpio_is_valid(host->board.rdy_pin)) {
208 res = devm_gpio_request(host->dev,
209 host->board.rdy_pin, "nand_rdy");
210 if (res < 0) {
211 dev_err(host->dev,
212 "can't request rdy gpio %d\n",
213 host->board.rdy_pin);
214 return res;
215 }
216
217 res = gpio_direction_input(host->board.rdy_pin);
218 if (res < 0) {
219 dev_err(host->dev,
220 "can't request input direction rdy gpio %d\n",
221 host->board.rdy_pin);
222 return res;
223 }
224
225 chip->dev_ready = atmel_nand_device_ready;
226 }
227
228 if (gpio_is_valid(host->board.enable_pin)) {
229 res = devm_gpio_request(host->dev,
230 host->board.enable_pin, "nand_enable");
231 if (res < 0) {
232 dev_err(host->dev,
233 "can't request enable gpio %d\n",
234 host->board.enable_pin);
235 return res;
236 }
237
238 res = gpio_direction_output(host->board.enable_pin, 1);
239 if (res < 0) {
240 dev_err(host->dev,
241 "can't request output direction enable gpio %d\n",
242 host->board.enable_pin);
243 return res;
244 }
245 }
246
247 return res;
248}
249
Artem Bityutskiy50082312012-02-02 13:54:25 +0200250/*
251 * Minimal-overhead PIO for data access.
252 */
253static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
254{
255 struct nand_chip *nand_chip = mtd->priv;
256
257 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
258}
259
260static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
261{
262 struct nand_chip *nand_chip = mtd->priv;
263
264 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
265}
266
267static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
268{
269 struct nand_chip *nand_chip = mtd->priv;
270
271 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
272}
273
274static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
275{
276 struct nand_chip *nand_chip = mtd->priv;
277
278 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
279}
280
Hong Xucbc6c5e2011-01-18 14:36:05 +0800281static void dma_complete_func(void *completion)
282{
283 complete(completion);
284}
285
286static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
287 int is_read)
288{
289 struct dma_device *dma_dev;
290 enum dma_ctrl_flags flags;
291 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
292 struct dma_async_tx_descriptor *tx = NULL;
293 dma_cookie_t cookie;
294 struct nand_chip *chip = mtd->priv;
295 struct atmel_nand_host *host = chip->priv;
296 void *p = buf;
297 int err = -EIO;
298 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
299
Hong Xu80b4f812011-03-31 18:33:15 +0800300 if (buf >= high_memory)
301 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800302
303 dma_dev = host->dma_chan->device;
304
305 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
306 DMA_COMPL_SKIP_DEST_UNMAP;
307
308 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
309 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
310 dev_err(host->dev, "Failed to dma_map_single\n");
311 goto err_buf;
312 }
313
314 if (is_read) {
315 dma_src_addr = host->io_phys;
316 dma_dst_addr = phys_addr;
317 } else {
318 dma_src_addr = phys_addr;
319 dma_dst_addr = host->io_phys;
320 }
321
322 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
323 dma_src_addr, len, flags);
324 if (!tx) {
325 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
326 goto err_dma;
327 }
328
329 init_completion(&host->comp);
330 tx->callback = dma_complete_func;
331 tx->callback_param = &host->comp;
332
333 cookie = tx->tx_submit(tx);
334 if (dma_submit_error(cookie)) {
335 dev_err(host->dev, "Failed to do DMA tx_submit\n");
336 goto err_dma;
337 }
338
339 dma_async_issue_pending(host->dma_chan);
340 wait_for_completion(&host->comp);
341
342 err = 0;
343
344err_dma:
345 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
346err_buf:
347 if (err != 0)
348 dev_warn(host->dev, "Fall back to CPU I/O\n");
349 return err;
350}
351
352static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
353{
354 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200355 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800356
Nicolas Ferre9d515672011-04-01 16:40:44 +0200357 if (use_dma && len > mtd->oobsize)
358 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800359 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
360 return;
361
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800362 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200363 atmel_read_buf16(mtd, buf, len);
364 else
365 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800366}
367
368static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
369{
370 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200371 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800372
Nicolas Ferre9d515672011-04-01 16:40:44 +0200373 if (use_dma && len > mtd->oobsize)
374 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800375 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
376 return;
377
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800378 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200379 atmel_write_buf16(mtd, buf, len);
380 else
381 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800382}
383
David Brownell23a346c2008-07-03 23:40:16 -0700384/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800385 * Return number of ecc bytes per sector according to sector size and
386 * correction capability
387 *
388 * Following table shows what at91 PMECC supported:
389 * Correction Capability Sector_512_bytes Sector_1024_bytes
390 * ===================== ================ =================
391 * 2-bits 4-bytes 4-bytes
392 * 4-bits 7-bytes 7-bytes
393 * 8-bits 13-bytes 14-bytes
394 * 12-bits 20-bytes 21-bytes
395 * 24-bits 39-bytes 42-bytes
396 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500397static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800398{
399 int m = 12 + sector_size / 512;
400 return (m * cap + 7) / 8;
401}
402
Bill Pemberton06f25512012-11-19 13:23:07 -0500403static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800404 int oobsize, int ecc_len)
Josh Wu1c7b8742012-06-29 17:47:55 +0800405{
406 int i;
407
408 layout->eccbytes = ecc_len;
409
410 /* ECC will occupy the last ecc_len bytes continuously */
411 for (i = 0; i < ecc_len; i++)
412 layout->eccpos[i] = oobsize - ecc_len + i;
413
414 layout->oobfree[0].offset = 2;
415 layout->oobfree[0].length =
416 oobsize - ecc_len - layout->oobfree[0].offset;
417}
418
Bill Pemberton06f25512012-11-19 13:23:07 -0500419static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800420{
421 int table_size;
422
423 table_size = host->pmecc_sector_size == 512 ?
424 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
425
426 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
427 table_size * sizeof(int16_t);
428}
429
Bill Pemberton06f25512012-11-19 13:23:07 -0500430static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800431{
432 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800433 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800434
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800435 size = (2 * cap + 1) * sizeof(int16_t);
436 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
437 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
438 host->pmecc_lmu = devm_kzalloc(host->dev,
439 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
440 host->pmecc_smu = devm_kzalloc(host->dev,
441 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800442
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800443 size = (cap + 1) * sizeof(int);
444 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
445 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
446 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800447
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800448 if (!host->pmecc_partial_syn ||
449 !host->pmecc_si ||
450 !host->pmecc_lmu ||
451 !host->pmecc_smu ||
452 !host->pmecc_mu ||
453 !host->pmecc_dmu ||
454 !host->pmecc_delta)
455 return -ENOMEM;
456
457 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800458}
459
460static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
461{
462 struct nand_chip *nand_chip = mtd->priv;
463 struct atmel_nand_host *host = nand_chip->priv;
464 int i;
465 uint32_t value;
466
467 /* Fill odd syndromes */
468 for (i = 0; i < host->pmecc_corr_cap; i++) {
469 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
470 if (i & 1)
471 value >>= 16;
472 value &= 0xffff;
473 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
474 }
475}
476
477static void pmecc_substitute(struct mtd_info *mtd)
478{
479 struct nand_chip *nand_chip = mtd->priv;
480 struct atmel_nand_host *host = nand_chip->priv;
481 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
482 int16_t __iomem *index_of = host->pmecc_index_of;
483 int16_t *partial_syn = host->pmecc_partial_syn;
484 const int cap = host->pmecc_corr_cap;
485 int16_t *si;
486 int i, j;
487
488 /* si[] is a table that holds the current syndrome value,
489 * an element of that table belongs to the field
490 */
491 si = host->pmecc_si;
492
493 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
494
495 /* Computation 2t syndromes based on S(x) */
496 /* Odd syndromes */
497 for (i = 1; i < 2 * cap; i += 2) {
498 for (j = 0; j < host->pmecc_degree; j++) {
499 if (partial_syn[i] & ((unsigned short)0x1 << j))
500 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
501 }
502 }
503 /* Even syndrome = (Odd syndrome) ** 2 */
504 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
505 if (si[j] == 0) {
506 si[i] = 0;
507 } else {
508 int16_t tmp;
509
510 tmp = readw_relaxed(index_of + si[j]);
511 tmp = (tmp * 2) % host->pmecc_cw_len;
512 si[i] = readw_relaxed(alpha_to + tmp);
513 }
514 }
515
516 return;
517}
518
519static void pmecc_get_sigma(struct mtd_info *mtd)
520{
521 struct nand_chip *nand_chip = mtd->priv;
522 struct atmel_nand_host *host = nand_chip->priv;
523
524 int16_t *lmu = host->pmecc_lmu;
525 int16_t *si = host->pmecc_si;
526 int *mu = host->pmecc_mu;
527 int *dmu = host->pmecc_dmu; /* Discrepancy */
528 int *delta = host->pmecc_delta; /* Delta order */
529 int cw_len = host->pmecc_cw_len;
530 const int16_t cap = host->pmecc_corr_cap;
531 const int num = 2 * cap + 1;
532 int16_t __iomem *index_of = host->pmecc_index_of;
533 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
534 int i, j, k;
535 uint32_t dmu_0_count, tmp;
536 int16_t *smu = host->pmecc_smu;
537
538 /* index of largest delta */
539 int ro;
540 int largest;
541 int diff;
542
543 dmu_0_count = 0;
544
545 /* First Row */
546
547 /* Mu */
548 mu[0] = -1;
549
550 memset(smu, 0, sizeof(int16_t) * num);
551 smu[0] = 1;
552
553 /* discrepancy set to 1 */
554 dmu[0] = 1;
555 /* polynom order set to 0 */
556 lmu[0] = 0;
557 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
558
559 /* Second Row */
560
561 /* Mu */
562 mu[1] = 0;
563 /* Sigma(x) set to 1 */
564 memset(&smu[num], 0, sizeof(int16_t) * num);
565 smu[num] = 1;
566
567 /* discrepancy set to S1 */
568 dmu[1] = si[1];
569
570 /* polynom order set to 0 */
571 lmu[1] = 0;
572
573 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
574
575 /* Init the Sigma(x) last row */
576 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
577
578 for (i = 1; i <= cap; i++) {
579 mu[i + 1] = i << 1;
580 /* Begin Computing Sigma (Mu+1) and L(mu) */
581 /* check if discrepancy is set to 0 */
582 if (dmu[i] == 0) {
583 dmu_0_count++;
584
585 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
586 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
587 tmp += 2;
588 else
589 tmp += 1;
590
591 if (dmu_0_count == tmp) {
592 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
593 smu[(cap + 1) * num + j] =
594 smu[i * num + j];
595
596 lmu[cap + 1] = lmu[i];
597 return;
598 }
599
600 /* copy polynom */
601 for (j = 0; j <= lmu[i] >> 1; j++)
602 smu[(i + 1) * num + j] = smu[i * num + j];
603
604 /* copy previous polynom order to the next */
605 lmu[i + 1] = lmu[i];
606 } else {
607 ro = 0;
608 largest = -1;
609 /* find largest delta with dmu != 0 */
610 for (j = 0; j < i; j++) {
611 if ((dmu[j]) && (delta[j] > largest)) {
612 largest = delta[j];
613 ro = j;
614 }
615 }
616
617 /* compute difference */
618 diff = (mu[i] - mu[ro]);
619
620 /* Compute degree of the new smu polynomial */
621 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
622 lmu[i + 1] = lmu[i];
623 else
624 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
625
626 /* Init smu[i+1] with 0 */
627 for (k = 0; k < num; k++)
628 smu[(i + 1) * num + k] = 0;
629
630 /* Compute smu[i+1] */
631 for (k = 0; k <= lmu[ro] >> 1; k++) {
632 int16_t a, b, c;
633
634 if (!(smu[ro * num + k] && dmu[i]))
635 continue;
636 a = readw_relaxed(index_of + dmu[i]);
637 b = readw_relaxed(index_of + dmu[ro]);
638 c = readw_relaxed(index_of + smu[ro * num + k]);
639 tmp = a + (cw_len - b) + c;
640 a = readw_relaxed(alpha_to + tmp % cw_len);
641 smu[(i + 1) * num + (k + diff)] = a;
642 }
643
644 for (k = 0; k <= lmu[i] >> 1; k++)
645 smu[(i + 1) * num + k] ^= smu[i * num + k];
646 }
647
648 /* End Computing Sigma (Mu+1) and L(mu) */
649 /* In either case compute delta */
650 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
651
652 /* Do not compute discrepancy for the last iteration */
653 if (i >= cap)
654 continue;
655
656 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
657 tmp = 2 * (i - 1);
658 if (k == 0) {
659 dmu[i + 1] = si[tmp + 3];
660 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
661 int16_t a, b, c;
662 a = readw_relaxed(index_of +
663 smu[(i + 1) * num + k]);
664 b = si[2 * (i - 1) + 3 - k];
665 c = readw_relaxed(index_of + b);
666 tmp = a + c;
667 tmp %= cw_len;
668 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
669 dmu[i + 1];
670 }
671 }
672 }
673
674 return;
675}
676
677static int pmecc_err_location(struct mtd_info *mtd)
678{
679 struct nand_chip *nand_chip = mtd->priv;
680 struct atmel_nand_host *host = nand_chip->priv;
681 unsigned long end_time;
682 const int cap = host->pmecc_corr_cap;
683 const int num = 2 * cap + 1;
684 int sector_size = host->pmecc_sector_size;
685 int err_nbr = 0; /* number of error */
686 int roots_nbr; /* number of roots */
687 int i;
688 uint32_t val;
689 int16_t *smu = host->pmecc_smu;
690
691 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
692
693 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
694 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
695 smu[(cap + 1) * num + i]);
696 err_nbr++;
697 }
698
699 val = (err_nbr - 1) << 16;
700 if (sector_size == 1024)
701 val |= 1;
702
703 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
704 pmerrloc_writel(host->pmerrloc_base, ELEN,
705 sector_size * 8 + host->pmecc_degree * cap);
706
707 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
708 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
709 & PMERRLOC_CALC_DONE)) {
710 if (unlikely(time_after(jiffies, end_time))) {
711 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
712 return -1;
713 }
714 cpu_relax();
715 }
716
717 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
718 & PMERRLOC_ERR_NUM_MASK) >> 8;
719 /* Number of roots == degree of smu hence <= cap */
720 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
721 return err_nbr - 1;
722
723 /* Number of roots does not match the degree of smu
724 * unable to correct error */
725 return -1;
726}
727
728static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
729 int sector_num, int extra_bytes, int err_nbr)
730{
731 struct nand_chip *nand_chip = mtd->priv;
732 struct atmel_nand_host *host = nand_chip->priv;
733 int i = 0;
734 int byte_pos, bit_pos, sector_size, pos;
735 uint32_t tmp;
736 uint8_t err_byte;
737
738 sector_size = host->pmecc_sector_size;
739
740 while (err_nbr) {
741 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
742 byte_pos = tmp / 8;
743 bit_pos = tmp % 8;
744
745 if (byte_pos >= (sector_size + extra_bytes))
746 BUG(); /* should never happen */
747
748 if (byte_pos < sector_size) {
749 err_byte = *(buf + byte_pos);
750 *(buf + byte_pos) ^= (1 << bit_pos);
751
752 pos = sector_num * host->pmecc_sector_size + byte_pos;
753 dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
754 pos, bit_pos, err_byte, *(buf + byte_pos));
755 } else {
756 /* Bit flip in OOB area */
757 tmp = sector_num * host->pmecc_bytes_per_sector
758 + (byte_pos - sector_size);
759 err_byte = ecc[tmp];
760 ecc[tmp] ^= (1 << bit_pos);
761
762 pos = tmp + nand_chip->ecc.layout->eccpos[0];
763 dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
764 pos, bit_pos, err_byte, ecc[tmp]);
765 }
766
767 i++;
768 err_nbr--;
769 }
770
771 return;
772}
773
774static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
775 u8 *ecc)
776{
777 struct nand_chip *nand_chip = mtd->priv;
778 struct atmel_nand_host *host = nand_chip->priv;
779 int i, err_nbr, eccbytes;
780 uint8_t *buf_pos;
Josh Wuc0c70d92012-11-27 18:50:31 +0800781 int total_err = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800782
783 eccbytes = nand_chip->ecc.bytes;
784 for (i = 0; i < eccbytes; i++)
785 if (ecc[i] != 0xff)
786 goto normal_check;
787 /* Erased page, return OK */
788 return 0;
789
790normal_check:
791 for (i = 0; i < host->pmecc_sector_number; i++) {
792 err_nbr = 0;
793 if (pmecc_stat & 0x1) {
794 buf_pos = buf + i * host->pmecc_sector_size;
795
796 pmecc_gen_syndrome(mtd, i);
797 pmecc_substitute(mtd);
798 pmecc_get_sigma(mtd);
799
800 err_nbr = pmecc_err_location(mtd);
801 if (err_nbr == -1) {
802 dev_err(host->dev, "PMECC: Too many errors\n");
803 mtd->ecc_stats.failed++;
804 return -EIO;
805 } else {
806 pmecc_correct_data(mtd, buf_pos, ecc, i,
807 host->pmecc_bytes_per_sector, err_nbr);
808 mtd->ecc_stats.corrected += err_nbr;
Josh Wuc0c70d92012-11-27 18:50:31 +0800809 total_err += err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800810 }
811 }
812 pmecc_stat >>= 1;
813 }
814
Josh Wuc0c70d92012-11-27 18:50:31 +0800815 return total_err;
Josh Wu1c7b8742012-06-29 17:47:55 +0800816}
817
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800818static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
819{
820 u32 val;
821
822 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
823 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
824 val = pmecc_readl_relaxed(host->ecc, CFG);
825
826 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
827 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
828 return;
829 }
830
831 if (ecc_op == NAND_ECC_READ)
832 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
833 | PMECC_CFG_AUTO_ENABLE);
834 else
835 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
836 & ~PMECC_CFG_AUTO_ENABLE);
837
838 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
839 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
840}
841
Josh Wu1c7b8742012-06-29 17:47:55 +0800842static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
843 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
844{
845 struct atmel_nand_host *host = chip->priv;
846 int eccsize = chip->ecc.size;
847 uint8_t *oob = chip->oob_poi;
848 uint32_t *eccpos = chip->ecc.layout->eccpos;
849 uint32_t stat;
850 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800851 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800852
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800853 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800854
855 chip->read_buf(mtd, buf, eccsize);
856 chip->read_buf(mtd, oob, mtd->oobsize);
857
858 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
859 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
860 if (unlikely(time_after(jiffies, end_time))) {
861 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
862 return -EIO;
863 }
864 cpu_relax();
865 }
866
867 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800868 if (stat != 0) {
869 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
870 if (bitflips < 0)
871 /* uncorrectable errors */
872 return 0;
873 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800874
Josh Wuc0c70d92012-11-27 18:50:31 +0800875 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800876}
877
878static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
879 struct nand_chip *chip, const uint8_t *buf, int oob_required)
880{
881 struct atmel_nand_host *host = chip->priv;
882 uint32_t *eccpos = chip->ecc.layout->eccpos;
883 int i, j;
884 unsigned long end_time;
885
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800886 pmecc_enable(host, NAND_ECC_WRITE);
Josh Wu1c7b8742012-06-29 17:47:55 +0800887
888 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
889
890 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
891 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
892 if (unlikely(time_after(jiffies, end_time))) {
893 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
894 return -EIO;
895 }
896 cpu_relax();
897 }
898
899 for (i = 0; i < host->pmecc_sector_number; i++) {
900 for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
901 int pos;
902
903 pos = i * host->pmecc_bytes_per_sector + j;
904 chip->oob_poi[eccpos[pos]] =
905 pmecc_readb_ecc_relaxed(host->ecc, i, j);
906 }
907 }
908 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
909
910 return 0;
911}
912
913static void atmel_pmecc_core_init(struct mtd_info *mtd)
914{
915 struct nand_chip *nand_chip = mtd->priv;
916 struct atmel_nand_host *host = nand_chip->priv;
917 uint32_t val = 0;
918 struct nand_ecclayout *ecc_layout;
919
920 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
921 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
922
923 switch (host->pmecc_corr_cap) {
924 case 2:
925 val = PMECC_CFG_BCH_ERR2;
926 break;
927 case 4:
928 val = PMECC_CFG_BCH_ERR4;
929 break;
930 case 8:
931 val = PMECC_CFG_BCH_ERR8;
932 break;
933 case 12:
934 val = PMECC_CFG_BCH_ERR12;
935 break;
936 case 24:
937 val = PMECC_CFG_BCH_ERR24;
938 break;
939 }
940
941 if (host->pmecc_sector_size == 512)
942 val |= PMECC_CFG_SECTOR512;
943 else if (host->pmecc_sector_size == 1024)
944 val |= PMECC_CFG_SECTOR1024;
945
946 switch (host->pmecc_sector_number) {
947 case 1:
948 val |= PMECC_CFG_PAGE_1SECTOR;
949 break;
950 case 2:
951 val |= PMECC_CFG_PAGE_2SECTORS;
952 break;
953 case 4:
954 val |= PMECC_CFG_PAGE_4SECTORS;
955 break;
956 case 8:
957 val |= PMECC_CFG_PAGE_8SECTORS;
958 break;
959 }
960
961 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
962 | PMECC_CFG_AUTO_DISABLE);
963 pmecc_writel(host->ecc, CFG, val);
964
965 ecc_layout = nand_chip->ecc.layout;
966 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
967 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
968 pmecc_writel(host->ecc, EADDR,
969 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
970 /* See datasheet about PMECC Clock Control Register */
971 pmecc_writel(host->ecc, CLK, 2);
972 pmecc_writel(host->ecc, IDR, 0xff);
973 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
974}
975
Josh Wu84cfbbb2013-01-23 20:47:12 +0800976/*
977 * Get ECC requirement in ONFI parameters, returns -1 if ONFI
978 * parameters is not supported.
979 * return 0 if success to get the ECC requirement.
980 */
981static int get_onfi_ecc_param(struct nand_chip *chip,
982 int *ecc_bits, int *sector_size)
983{
984 *ecc_bits = *sector_size = 0;
985
986 if (chip->onfi_params.ecc_bits == 0xff)
987 /* TODO: the sector_size and ecc_bits need to be find in
988 * extended ecc parameter, currently we don't support it.
989 */
990 return -1;
991
992 *ecc_bits = chip->onfi_params.ecc_bits;
993
994 /* The default sector size (ecc codeword size) is 512 */
995 *sector_size = 512;
996
997 return 0;
998}
999
1000/*
1001 * Get ecc requirement from ONFI parameters ecc requirement.
1002 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
1003 * will set them according to ONFI ecc requirement. Otherwise, use the
1004 * value in DTS file.
1005 * return 0 if success. otherwise return error code.
1006 */
1007static int pmecc_choose_ecc(struct atmel_nand_host *host,
1008 int *cap, int *sector_size)
1009{
1010 /* Get ECC requirement from ONFI parameters */
1011 *cap = *sector_size = 0;
1012 if (host->nand_chip.onfi_version) {
1013 if (!get_onfi_ecc_param(&host->nand_chip, cap, sector_size))
1014 dev_info(host->dev, "ONFI params, minimum required ECC: %d bits in %d bytes\n",
1015 *cap, *sector_size);
1016 else
1017 dev_info(host->dev, "NAND chip ECC reqirement is in Extended ONFI parameter, we don't support yet.\n");
1018 } else {
1019 dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes");
1020 }
1021 if (*cap == 0 && *sector_size == 0) {
1022 *cap = 2;
1023 *sector_size = 512;
1024 }
1025
1026 /* If dts file doesn't specify then use the one in ONFI parameters */
1027 if (host->pmecc_corr_cap == 0) {
1028 /* use the most fitable ecc bits (the near bigger one ) */
1029 if (*cap <= 2)
1030 host->pmecc_corr_cap = 2;
1031 else if (*cap <= 4)
1032 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +08001033 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001034 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +08001035 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001036 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +08001037 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001038 host->pmecc_corr_cap = 24;
1039 else
1040 return -EINVAL;
1041 }
1042 if (host->pmecc_sector_size == 0) {
1043 /* use the most fitable sector size (the near smaller one ) */
1044 if (*sector_size >= 1024)
1045 host->pmecc_sector_size = 1024;
1046 else if (*sector_size >= 512)
1047 host->pmecc_sector_size = 512;
1048 else
1049 return -EINVAL;
1050 }
1051 return 0;
1052}
1053
Josh Wu1c7b8742012-06-29 17:47:55 +08001054static int __init atmel_pmecc_nand_init_params(struct platform_device *pdev,
1055 struct atmel_nand_host *host)
1056{
1057 struct mtd_info *mtd = &host->mtd;
1058 struct nand_chip *nand_chip = &host->nand_chip;
1059 struct resource *regs, *regs_pmerr, *regs_rom;
1060 int cap, sector_size, err_no;
1061
Josh Wu84cfbbb2013-01-23 20:47:12 +08001062 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
1063 if (err_no) {
1064 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1065 return err_no;
1066 }
1067
Richard Genoudf666d642013-07-30 17:17:29 +02001068 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001069 sector_size != host->pmecc_sector_size)
1070 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001071
Josh Wu1c7b8742012-06-29 17:47:55 +08001072 cap = host->pmecc_corr_cap;
1073 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001074 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1075 host->pmecc_lookup_table_offset_512 :
1076 host->pmecc_lookup_table_offset_1024;
1077
Josh Wu1c7b8742012-06-29 17:47:55 +08001078 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1079 cap, sector_size);
1080
1081 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1082 if (!regs) {
1083 dev_warn(host->dev,
1084 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1085 nand_chip->ecc.mode = NAND_ECC_SOFT;
1086 return 0;
1087 }
1088
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001089 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1090 if (IS_ERR(host->ecc)) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001091 dev_err(host->dev, "ioremap failed\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001092 err_no = PTR_ERR(host->ecc);
1093 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001094 }
1095
1096 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001097 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1098 if (IS_ERR(host->pmerrloc_base)) {
1099 dev_err(host->dev,
1100 "Can not get I/O resource for PMECC ERRLOC controller!\n");
1101 err_no = PTR_ERR(host->pmerrloc_base);
1102 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001103 }
1104
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001105 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1106 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, regs_rom);
1107 if (IS_ERR(host->pmecc_rom_base)) {
1108 dev_err(host->dev, "Can not get I/O resource for ROM!\n");
1109 err_no = PTR_ERR(host->pmecc_rom_base);
1110 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001111 }
1112
1113 /* ECC is calculated for the whole page (1 step) */
1114 nand_chip->ecc.size = mtd->writesize;
1115
1116 /* set ECC page size and oob layout */
1117 switch (mtd->writesize) {
1118 case 2048:
1119 host->pmecc_degree = PMECC_GF_DIMENSION_13;
1120 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
1121 host->pmecc_sector_number = mtd->writesize / sector_size;
1122 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
1123 cap, sector_size);
1124 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1125 host->pmecc_index_of = host->pmecc_rom_base +
1126 host->pmecc_lookup_table_offset;
1127
1128 nand_chip->ecc.steps = 1;
1129 nand_chip->ecc.strength = cap;
1130 nand_chip->ecc.bytes = host->pmecc_bytes_per_sector *
1131 host->pmecc_sector_number;
1132 if (nand_chip->ecc.bytes > mtd->oobsize - 2) {
1133 dev_err(host->dev, "No room for ECC bytes\n");
1134 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001135 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001136 }
1137 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1138 mtd->oobsize,
1139 nand_chip->ecc.bytes);
1140 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1141 break;
1142 case 512:
1143 case 1024:
1144 case 4096:
1145 /* TODO */
1146 dev_warn(host->dev,
1147 "Unsupported page size for PMECC, use Software ECC\n");
1148 default:
1149 /* page size not handled by HW ECC */
1150 /* switching back to soft ECC */
1151 nand_chip->ecc.mode = NAND_ECC_SOFT;
1152 return 0;
1153 }
1154
1155 /* Allocate data for PMECC computation */
1156 err_no = pmecc_data_alloc(host);
1157 if (err_no) {
1158 dev_err(host->dev,
1159 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001160 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001161 }
1162
1163 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1164 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1165
1166 atmel_pmecc_core_init(mtd);
1167
1168 return 0;
1169
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001170err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001171 return err_no;
1172}
1173
1174/*
Richard Genoud77f54922008-04-23 19:51:14 +02001175 * Calculate HW ECC
1176 *
1177 * function called after a write
1178 *
1179 * mtd: MTD block structure
1180 * dat: raw data (unused)
1181 * ecc_code: buffer for ECC
1182 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001183static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001184 const u_char *dat, unsigned char *ecc_code)
1185{
1186 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001187 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001188 unsigned int ecc_value;
1189
1190 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001191 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001192
Richard Genoud3fc23892008-10-12 08:42:28 +02001193 ecc_code[0] = ecc_value & 0xFF;
1194 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001195
1196 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001197 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001198
Richard Genoud3fc23892008-10-12 08:42:28 +02001199 ecc_code[2] = ecc_value & 0xFF;
1200 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001201
1202 return 0;
1203}
1204
1205/*
1206 * HW ECC read page function
1207 *
1208 * mtd: mtd info structure
1209 * chip: nand chip info structure
1210 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001211 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001212 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001213static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1214 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001215{
1216 int eccsize = chip->ecc.size;
1217 int eccbytes = chip->ecc.bytes;
1218 uint32_t *eccpos = chip->ecc.layout->eccpos;
1219 uint8_t *p = buf;
1220 uint8_t *oob = chip->oob_poi;
1221 uint8_t *ecc_pos;
1222 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001223 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001224
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001225 /*
1226 * Errata: ALE is incorrectly wired up to the ECC controller
1227 * on the AP7000, so it will include the address cycles in the
1228 * ECC calculation.
1229 *
1230 * Workaround: Reset the parity registers before reading the
1231 * actual data.
1232 */
Josh Wu71b94e22013-05-09 15:34:54 +08001233 struct atmel_nand_host *host = chip->priv;
1234 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001235 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001236
Richard Genoud77f54922008-04-23 19:51:14 +02001237 /* read the page */
1238 chip->read_buf(mtd, p, eccsize);
1239
1240 /* move to ECC position if needed */
1241 if (eccpos[0] != 0) {
1242 /* This only works on large pages
1243 * because the ECC controller waits for
1244 * NAND_CMD_RNDOUTSTART after the
1245 * NAND_CMD_RNDOUT.
1246 * anyway, for small pages, the eccpos[0] == 0
1247 */
1248 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1249 mtd->writesize + eccpos[0], -1);
1250 }
1251
1252 /* the ECC controller needs to read the ECC just after the data */
1253 ecc_pos = oob + eccpos[0];
1254 chip->read_buf(mtd, ecc_pos, eccbytes);
1255
1256 /* check if there's an error */
1257 stat = chip->ecc.correct(mtd, p, oob, NULL);
1258
Mike Dunn3f91e942012-04-25 12:06:09 -07001259 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001260 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001261 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001262 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001263 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1264 }
Richard Genoud77f54922008-04-23 19:51:14 +02001265
1266 /* get back to oob start (end of page) */
1267 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1268
1269 /* read the oob */
1270 chip->read_buf(mtd, oob, mtd->oobsize);
1271
Mike Dunn3f91e942012-04-25 12:06:09 -07001272 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001273}
1274
1275/*
1276 * HW ECC Correction
1277 *
1278 * function called after a read
1279 *
1280 * mtd: MTD block structure
1281 * dat: raw data read from the chip
1282 * read_ecc: ECC from the chip (unused)
1283 * isnull: unused
1284 *
1285 * Detect and correct a 1 bit error for a page
1286 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001287static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001288 u_char *read_ecc, u_char *isnull)
1289{
1290 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001291 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001292 unsigned int ecc_status;
1293 unsigned int ecc_word, ecc_bit;
1294
1295 /* get the status from the Status Register */
1296 ecc_status = ecc_readl(host->ecc, SR);
1297
1298 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001299 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001300 return 0;
1301
1302 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001303 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001304 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001305 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001306 ecc_word >>= 4;
1307
1308 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001309 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001310 /* check if it is a freshly erased block
1311 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001312 if ((ecc_bit == ATMEL_ECC_BITADDR)
1313 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001314 /* the block has just been erased, return OK */
1315 return 0;
1316 }
1317 /* it doesn't seems to be a freshly
1318 * erased block.
1319 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001320 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001321 " Unable to correct.\n");
1322 return -EIO;
1323 }
1324
1325 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001326 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001327 /* there's nothing much to do here.
1328 * the bit error is on the ECC itself.
1329 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001330 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001331 " Nothing to correct\n");
1332 return 0;
1333 }
1334
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001335 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001336 " (word offset in the page :"
1337 " 0x%x bit offset : 0x%x)\n",
1338 ecc_word, ecc_bit);
1339 /* correct the error */
1340 if (nand_chip->options & NAND_BUSWIDTH_16) {
1341 /* 16 bits words */
1342 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1343 } else {
1344 /* 8 bits words */
1345 dat[ecc_word] ^= (1 << ecc_bit);
1346 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001347 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001348 return 1;
1349}
1350
1351/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001352 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001353 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001354static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1355{
Josh Wu71b94e22013-05-09 15:34:54 +08001356 struct nand_chip *nand_chip = mtd->priv;
1357 struct atmel_nand_host *host = nand_chip->priv;
1358
1359 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001360 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001361}
Richard Genoud77f54922008-04-23 19:51:14 +02001362
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001363#if defined(CONFIG_OF)
Bill Pemberton06f25512012-11-19 13:23:07 -05001364static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001365 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001366{
Josh Wuc0cf7872013-01-23 20:47:08 +08001367 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001368 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001369 int ecc_mode;
1370 struct atmel_nand_data *board = &host->board;
1371 enum of_gpio_flags flags;
1372
1373 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1374 if (val >= 32) {
1375 dev_err(host->dev, "invalid addr-offset %u\n", val);
1376 return -EINVAL;
1377 }
1378 board->ale = val;
1379 }
1380
1381 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1382 if (val >= 32) {
1383 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1384 return -EINVAL;
1385 }
1386 board->cle = val;
1387 }
1388
1389 ecc_mode = of_get_nand_ecc_mode(np);
1390
1391 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1392
1393 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1394
Josh Wu1b719262013-05-09 15:34:55 +08001395 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1396
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001397 if (of_get_nand_bus_width(np) == 16)
1398 board->bus_width_16 = 1;
1399
1400 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1401 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1402
1403 board->enable_pin = of_get_gpio(np, 1);
1404 board->det_pin = of_get_gpio(np, 2);
1405
Josh Wua41b51a2012-06-29 17:47:54 +08001406 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1407
Josh Wu7dc37de2013-08-05 19:14:35 +08001408 /* load the nfc driver if there is */
1409 of_platform_populate(np, NULL, NULL, host->dev);
1410
Josh Wua41b51a2012-06-29 17:47:54 +08001411 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1412 return 0; /* Not using PMECC */
1413
1414 /* use PMECC, get correction capability, sector size and lookup
1415 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001416 * If correction bits and sector size are not specified, then find
1417 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001418 */
Josh Wue66b4312013-01-23 20:47:11 +08001419 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1420 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1421 (val != 24)) {
1422 dev_err(host->dev,
1423 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1424 val);
1425 return -EINVAL;
1426 }
1427 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001428 }
Josh Wua41b51a2012-06-29 17:47:54 +08001429
Josh Wue66b4312013-01-23 20:47:11 +08001430 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1431 if ((val != 512) && (val != 1024)) {
1432 dev_err(host->dev,
1433 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1434 val);
1435 return -EINVAL;
1436 }
1437 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001438 }
Josh Wua41b51a2012-06-29 17:47:54 +08001439
1440 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1441 offset, 2) != 0) {
1442 dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
1443 return -EINVAL;
1444 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001445 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001446 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1447 return -EINVAL;
1448 }
Josh Wue66b4312013-01-23 20:47:11 +08001449 host->pmecc_lookup_table_offset_512 = offset[0];
1450 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001451
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001452 return 0;
1453}
1454#else
Bill Pemberton06f25512012-11-19 13:23:07 -05001455static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001456 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001457{
1458 return -EINVAL;
1459}
1460#endif
1461
Josh Wu3dfe41a2012-06-25 18:07:43 +08001462static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
1463 struct atmel_nand_host *host)
1464{
1465 struct mtd_info *mtd = &host->mtd;
1466 struct nand_chip *nand_chip = &host->nand_chip;
1467 struct resource *regs;
1468
1469 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1470 if (!regs) {
1471 dev_err(host->dev,
1472 "Can't get I/O resource regs, use software ECC\n");
1473 nand_chip->ecc.mode = NAND_ECC_SOFT;
1474 return 0;
1475 }
1476
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001477 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1478 if (IS_ERR(host->ecc)) {
Josh Wu3dfe41a2012-06-25 18:07:43 +08001479 dev_err(host->dev, "ioremap failed\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001480 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001481 }
1482
1483 /* ECC is calculated for the whole page (1 step) */
1484 nand_chip->ecc.size = mtd->writesize;
1485
1486 /* set ECC page size and oob layout */
1487 switch (mtd->writesize) {
1488 case 512:
1489 nand_chip->ecc.layout = &atmel_oobinfo_small;
1490 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1491 break;
1492 case 1024:
1493 nand_chip->ecc.layout = &atmel_oobinfo_large;
1494 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1495 break;
1496 case 2048:
1497 nand_chip->ecc.layout = &atmel_oobinfo_large;
1498 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1499 break;
1500 case 4096:
1501 nand_chip->ecc.layout = &atmel_oobinfo_large;
1502 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1503 break;
1504 default:
1505 /* page size not handled by HW ECC */
1506 /* switching back to soft ECC */
1507 nand_chip->ecc.mode = NAND_ECC_SOFT;
1508 return 0;
1509 }
1510
1511 /* set up for HW ECC */
1512 nand_chip->ecc.calculate = atmel_nand_calculate;
1513 nand_chip->ecc.correct = atmel_nand_correct;
1514 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1515 nand_chip->ecc.read_page = atmel_nand_read_page;
1516 nand_chip->ecc.bytes = 4;
1517 nand_chip->ecc.strength = 1;
1518
1519 return 0;
1520}
1521
Josh Wu7dc37de2013-08-05 19:14:35 +08001522/* SMC interrupt service routine */
1523static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
1524{
1525 struct atmel_nand_host *host = dev_id;
1526 u32 status, mask, pending;
1527 irqreturn_t ret = IRQ_HANDLED;
1528
1529 status = nfc_readl(host->nfc->hsmc_regs, SR);
1530 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1531 pending = status & mask;
1532
1533 if (pending & NFC_SR_XFR_DONE) {
1534 complete(&host->nfc->comp_nfc);
1535 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
1536 } else if (pending & NFC_SR_RB_EDGE) {
1537 complete(&host->nfc->comp_nfc);
1538 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_RB_EDGE);
1539 } else if (pending & NFC_SR_CMD_DONE) {
1540 complete(&host->nfc->comp_nfc);
1541 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
1542 } else {
1543 ret = IRQ_NONE;
1544 }
1545
1546 return ret;
1547}
1548
1549/* NFC(Nand Flash Controller) related functions */
1550static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
1551{
1552 unsigned long timeout;
1553 init_completion(&host->nfc->comp_nfc);
1554
1555 /* Enable interrupt that need to wait for */
1556 nfc_writel(host->nfc->hsmc_regs, IER, flag);
1557
1558 timeout = wait_for_completion_timeout(&host->nfc->comp_nfc,
1559 msecs_to_jiffies(NFC_TIME_OUT_MS));
1560 if (timeout)
1561 return 0;
1562
1563 /* Time out to wait for the interrupt */
1564 dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
1565 return -ETIMEDOUT;
1566}
1567
1568static int nfc_send_command(struct atmel_nand_host *host,
1569 unsigned int cmd, unsigned int addr, unsigned char cycle0)
1570{
1571 unsigned long timeout;
1572 dev_dbg(host->dev,
1573 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1574 cmd, addr, cycle0);
1575
1576 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1577 while (nfc_cmd_readl(NFCADDR_CMD_NFCBUSY, host->nfc->base_cmd_regs)
1578 & NFCADDR_CMD_NFCBUSY) {
1579 if (time_after(jiffies, timeout)) {
1580 dev_err(host->dev,
1581 "Time out to wait CMD_NFCBUSY ready!\n");
1582 return -ETIMEDOUT;
1583 }
1584 }
1585 nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
1586 nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
1587 return nfc_wait_interrupt(host, NFC_SR_CMD_DONE);
1588}
1589
1590static int nfc_device_ready(struct mtd_info *mtd)
1591{
1592 struct nand_chip *nand_chip = mtd->priv;
1593 struct atmel_nand_host *host = nand_chip->priv;
1594 if (!nfc_wait_interrupt(host, NFC_SR_RB_EDGE))
1595 return 1;
1596 return 0;
1597}
1598
1599static void nfc_select_chip(struct mtd_info *mtd, int chip)
1600{
1601 struct nand_chip *nand_chip = mtd->priv;
1602 struct atmel_nand_host *host = nand_chip->priv;
1603
1604 if (chip == -1)
1605 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
1606 else
1607 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
1608}
1609
1610static int nfc_make_addr(struct mtd_info *mtd, int column, int page_addr,
1611 unsigned int *addr1234, unsigned int *cycle0)
1612{
1613 struct nand_chip *chip = mtd->priv;
1614
1615 int acycle = 0;
1616 unsigned char addr_bytes[8];
1617 int index = 0, bit_shift;
1618
1619 BUG_ON(addr1234 == NULL || cycle0 == NULL);
1620
1621 *cycle0 = 0;
1622 *addr1234 = 0;
1623
1624 if (column != -1) {
1625 if (chip->options & NAND_BUSWIDTH_16)
1626 column >>= 1;
1627 addr_bytes[acycle++] = column & 0xff;
1628 if (mtd->writesize > 512)
1629 addr_bytes[acycle++] = (column >> 8) & 0xff;
1630 }
1631
1632 if (page_addr != -1) {
1633 addr_bytes[acycle++] = page_addr & 0xff;
1634 addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
1635 if (chip->chipsize > (128 << 20))
1636 addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
1637 }
1638
1639 if (acycle > 4)
1640 *cycle0 = addr_bytes[index++];
1641
1642 for (bit_shift = 0; index < acycle; bit_shift += 8)
1643 *addr1234 += addr_bytes[index++] << bit_shift;
1644
1645 /* return acycle in cmd register */
1646 return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
1647}
1648
1649static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
1650 int column, int page_addr)
1651{
1652 struct nand_chip *chip = mtd->priv;
1653 struct atmel_nand_host *host = chip->priv;
1654 unsigned long timeout;
1655 unsigned int nfc_addr_cmd = 0;
1656
1657 unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1658
1659 /* Set default settings: no cmd2, no addr cycle. read from nand */
1660 unsigned int cmd2 = 0;
1661 unsigned int vcmd2 = 0;
1662 int acycle = NFCADDR_CMD_ACYCLE_NONE;
1663 int csid = NFCADDR_CMD_CSID_3;
1664 int dataen = NFCADDR_CMD_DATADIS;
1665 int nfcwr = NFCADDR_CMD_NFCRD;
1666 unsigned int addr1234 = 0;
1667 unsigned int cycle0 = 0;
1668 bool do_addr = true;
1669
1670 dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1671 __func__, command, column, page_addr);
1672
1673 switch (command) {
1674 case NAND_CMD_RESET:
1675 nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
1676 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1677 udelay(chip->chip_delay);
1678
1679 nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
1680 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1681 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
1682 if (time_after(jiffies, timeout)) {
1683 dev_err(host->dev,
1684 "Time out to wait status ready!\n");
1685 break;
1686 }
1687 }
1688 return;
1689 case NAND_CMD_STATUS:
1690 do_addr = false;
1691 break;
1692 case NAND_CMD_PARAM:
1693 case NAND_CMD_READID:
1694 do_addr = false;
1695 acycle = NFCADDR_CMD_ACYCLE_1;
1696 if (column != -1)
1697 addr1234 = column;
1698 break;
1699 case NAND_CMD_RNDOUT:
1700 cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
1701 vcmd2 = NFCADDR_CMD_VCMD2;
1702 break;
1703 case NAND_CMD_READ0:
1704 case NAND_CMD_READOOB:
1705 if (command == NAND_CMD_READOOB) {
1706 column += mtd->writesize;
1707 command = NAND_CMD_READ0; /* only READ0 is valid */
1708 cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1709 }
1710
1711 cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
1712 vcmd2 = NFCADDR_CMD_VCMD2;
1713 break;
1714 /* For prgramming command, the cmd need set to write enable */
1715 case NAND_CMD_PAGEPROG:
1716 case NAND_CMD_SEQIN:
1717 case NAND_CMD_RNDIN:
1718 nfcwr = NFCADDR_CMD_NFCWR;
1719 break;
1720 default:
1721 break;
1722 }
1723
1724 if (do_addr)
1725 acycle = nfc_make_addr(mtd, column, page_addr, &addr1234,
1726 &cycle0);
1727
1728 nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
1729 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1730
1731 /*
1732 * Program and erase have their own busy handlers status, sequential
1733 * in, and deplete1 need no delay.
1734 */
1735 switch (command) {
1736 case NAND_CMD_CACHEDPROG:
1737 case NAND_CMD_PAGEPROG:
1738 case NAND_CMD_ERASE1:
1739 case NAND_CMD_ERASE2:
1740 case NAND_CMD_RNDIN:
1741 case NAND_CMD_STATUS:
1742 case NAND_CMD_RNDOUT:
1743 case NAND_CMD_SEQIN:
1744 case NAND_CMD_READID:
1745 return;
1746
1747 case NAND_CMD_READ0:
1748 /* fall through */
1749 default:
1750 nfc_wait_interrupt(host, NFC_SR_RB_EDGE);
1751 }
1752}
1753
1754static struct platform_driver atmel_nand_nfc_driver;
Andrew Victor42cb1402006-10-19 18:24:35 +02001755/*
1756 * Probe for the NAND device.
1757 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001758static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02001759{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001760 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02001761 struct mtd_info *mtd;
1762 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001763 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001764 struct mtd_part_parser_data ppdata = {};
Josh Wu7dc37de2013-08-05 19:14:35 +08001765 int res, irq;
Andrew Victor42cb1402006-10-19 18:24:35 +02001766
1767 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001768 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +02001769 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001770 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +02001771 return -ENOMEM;
1772 }
1773
Josh Wu7dc37de2013-08-05 19:14:35 +08001774 res = platform_driver_register(&atmel_nand_nfc_driver);
1775 if (res)
1776 dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
1777
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001778 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1779 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
1780 if (IS_ERR(host->io_base)) {
1781 dev_err(&pdev->dev, "atmel_nand: ioremap resource failed\n");
1782 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001783 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02001784 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001785 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02001786
1787 mtd = &host->mtd;
1788 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001789 host->dev = &pdev->dev;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001790 if (pdev->dev.of_node) {
1791 res = atmel_of_init_port(host, pdev->dev.of_node);
1792 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001793 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001794 } else {
1795 memcpy(&host->board, pdev->dev.platform_data,
1796 sizeof(struct atmel_nand_data));
1797 }
Andrew Victor42cb1402006-10-19 18:24:35 +02001798
1799 nand_chip->priv = host; /* link the private data structures */
1800 mtd->priv = nand_chip;
1801 mtd->owner = THIS_MODULE;
1802
1803 /* Set address of NAND IO lines */
1804 nand_chip->IO_ADDR_R = host->io_base;
1805 nand_chip->IO_ADDR_W = host->io_base;
Ivan Kutena4265f82007-05-24 14:35:58 +03001806
Josh Wu7dc37de2013-08-05 19:14:35 +08001807 if (nand_nfc.is_initialized) {
1808 /* NFC driver is probed and initialized */
1809 host->nfc = &nand_nfc;
1810
1811 nand_chip->select_chip = nfc_select_chip;
1812 nand_chip->dev_ready = nfc_device_ready;
1813 nand_chip->cmdfunc = nfc_nand_command;
1814
1815 /* Initialize the interrupt for NFC */
1816 irq = platform_get_irq(pdev, 0);
1817 if (irq < 0) {
1818 dev_err(host->dev, "Cannot get HSMC irq!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001819 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001820 }
1821
Josh Wu7dc37de2013-08-05 19:14:35 +08001822 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
1823 0, "hsmc", host);
1824 if (res) {
1825 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
1826 irq);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001827 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001828 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001829 } else {
1830 res = atmel_nand_set_enable_ready_pins(mtd);
1831 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001832 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001833
Josh Wu7dc37de2013-08-05 19:14:35 +08001834 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001835 }
Ivan Kutena4265f82007-05-24 14:35:58 +03001836
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001837 nand_chip->ecc.mode = host->board.ecc_mode;
Andrew Victor42cb1402006-10-19 18:24:35 +02001838 nand_chip->chip_delay = 20; /* 20us command delay time */
1839
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001840 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02001841 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08001842
1843 nand_chip->read_buf = atmel_read_buf;
1844 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02001845
Andrew Victor42cb1402006-10-19 18:24:35 +02001846 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001847 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02001848
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001849 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001850 res = devm_gpio_request(&pdev->dev,
1851 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02001852 if (res < 0) {
1853 dev_err(&pdev->dev,
1854 "can't request det gpio %d\n",
1855 host->board.det_pin);
1856 goto err_no_card;
1857 }
1858
1859 res = gpio_direction_input(host->board.det_pin);
1860 if (res < 0) {
1861 dev_err(&pdev->dev,
1862 "can't request input direction det gpio %d\n",
1863 host->board.det_pin);
1864 goto err_no_card;
1865 }
1866
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001867 if (gpio_get_value(host->board.det_pin)) {
Simon Polettef4fa6972009-05-27 18:19:39 +03001868 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01001869 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001870 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02001871 }
1872 }
1873
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001874 if (host->board.on_flash_bbt || on_flash_bbt) {
Simon Polettef4fa6972009-05-27 18:19:39 +03001875 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001876 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa6972009-05-27 18:19:39 +03001877 }
1878
Josh Wu1b719262013-05-09 15:34:55 +08001879 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08001880 use_dma = 0;
1881
1882 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08001883 dma_cap_mask_t mask;
1884
1885 dma_cap_zero(mask);
1886 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02001887 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08001888 if (!host->dma_chan) {
1889 dev_err(host->dev, "Failed to request DMA channel\n");
1890 use_dma = 0;
1891 }
1892 }
1893 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08001894 dev_info(host->dev, "Using %s for DMA transfers.\n",
1895 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08001896 else
1897 dev_info(host->dev, "No DMA support for NAND access.\n");
1898
Richard Genoud77f54922008-04-23 19:51:14 +02001899 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00001900 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02001901 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001902 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02001903 }
1904
Richard Genoud3fc23892008-10-12 08:42:28 +02001905 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001906 if (host->has_pmecc)
1907 res = atmel_pmecc_nand_init_params(pdev, host);
1908 else
1909 res = atmel_hw_nand_init_params(pdev, host);
1910
Josh Wu3dfe41a2012-06-25 18:07:43 +08001911 if (res != 0)
1912 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02001913 }
1914
1915 /* second phase scan */
1916 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02001917 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001918 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02001919 }
1920
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001921 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001922 ppdata.of_node = pdev->dev.of_node;
1923 res = mtd_device_parse_register(mtd, NULL, &ppdata,
1924 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02001925 if (!res)
1926 return res;
1927
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001928err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001929 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08001930 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001931err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001932err_scan_ident:
1933err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001934 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08001935 if (host->dma_chan)
1936 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02001937err_nand_ioremap:
Josh Wu7dc37de2013-08-05 19:14:35 +08001938 platform_driver_unregister(&atmel_nand_nfc_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02001939 return res;
1940}
1941
1942/*
1943 * Remove a NAND device.
1944 */
David Brownell23a346c2008-07-03 23:40:16 -07001945static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02001946{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001947 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +02001948 struct mtd_info *mtd = &host->mtd;
1949
1950 nand_release(mtd);
1951
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001952 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02001953
Josh Wu1c7b8742012-06-29 17:47:55 +08001954 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
1955 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1956 pmerrloc_writel(host->pmerrloc_base, ELDIS,
1957 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08001958 }
1959
Hong Xucbc6c5e2011-01-18 14:36:05 +08001960 if (host->dma_chan)
1961 dma_release_channel(host->dma_chan);
1962
Josh Wu7dc37de2013-08-05 19:14:35 +08001963 platform_driver_unregister(&atmel_nand_nfc_driver);
1964
Andrew Victor42cb1402006-10-19 18:24:35 +02001965 return 0;
1966}
1967
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001968#if defined(CONFIG_OF)
1969static const struct of_device_id atmel_nand_dt_ids[] = {
1970 { .compatible = "atmel,at91rm9200-nand" },
1971 { /* sentinel */ }
1972};
1973
1974MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
1975#endif
1976
Josh Wu7dc37de2013-08-05 19:14:35 +08001977static int atmel_nand_nfc_probe(struct platform_device *pdev)
1978{
1979 struct atmel_nfc *nfc = &nand_nfc;
1980 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
1981
1982 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1983 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
1984 if (IS_ERR(nfc->base_cmd_regs))
1985 return PTR_ERR(nfc->base_cmd_regs);
1986
1987 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1988 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
1989 if (IS_ERR(nfc->hsmc_regs))
1990 return PTR_ERR(nfc->hsmc_regs);
1991
1992 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1993 if (nfc_sram) {
1994 nfc->sram_bank0 = devm_ioremap_resource(&pdev->dev, nfc_sram);
1995 if (IS_ERR(nfc->sram_bank0))
1996 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
1997 PTR_ERR(nfc->sram_bank0));
1998 else
1999 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
2000 }
2001
2002 nfc->is_initialized = true;
2003 dev_info(&pdev->dev, "NFC is probed.\n");
2004 return 0;
2005}
2006
2007static struct of_device_id atmel_nand_nfc_match[] = {
2008 { .compatible = "atmel,sama5d3-nfc" },
2009 { /* sentinel */ }
2010};
2011
2012static struct platform_driver atmel_nand_nfc_driver = {
2013 .driver = {
2014 .name = "atmel_nand_nfc",
2015 .owner = THIS_MODULE,
2016 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2017 },
2018 .probe = atmel_nand_nfc_probe,
2019};
2020
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002021static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -07002022 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +02002023 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002024 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +02002025 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002026 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02002027 },
2028};
2029
Jingoo Hanc5345ed2013-03-05 13:30:04 +09002030module_platform_driver_probe(atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +02002031
2032MODULE_LICENSE("GPL");
2033MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02002034MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002035MODULE_ALIAS("platform:atmel_nand");