blob: 2a837cb425d76b056a8a14b801a772d76b6746f7 [file] [log] [blame]
Andrew Victor42cb1402006-10-19 18:24:35 +02001/*
Josh Wu1c7b8742012-06-29 17:47:55 +08002 * Copyright © 2003 Rick Bronson
Andrew Victor42cb1402006-10-19 18:24:35 +02003 *
4 * Derived from drivers/mtd/nand/autcpu12.c
Josh Wu1c7b8742012-06-29 17:47:55 +08005 * Copyright © 2001 Thomas Gleixner (gleixner@autronix.de)
Andrew Victor42cb1402006-10-19 18:24:35 +02006 *
7 * Derived from drivers/mtd/spia.c
Josh Wu1c7b8742012-06-29 17:47:55 +08008 * Copyright © 2000 Steven J. Hill (sjhill@cotw.com)
Andrew Victor42cb1402006-10-19 18:24:35 +02009 *
Richard Genoud77f54922008-04-23 19:51:14 +020010 *
11 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
Josh Wu1c7b8742012-06-29 17:47:55 +080012 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright © 2007
Richard Genoud77f54922008-04-23 19:51:14 +020013 *
14 * Derived from Das U-Boot source code
15 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
Josh Wu1c7b8742012-06-29 17:47:55 +080016 * © Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
Richard Genoud77f54922008-04-23 19:51:14 +020017 *
Josh Wu1c7b8742012-06-29 17:47:55 +080018 * Add Programmable Multibit ECC support for various AT91 SoC
19 * © Copyright 2012 ATMEL, Hong Xu
Richard Genoud77f54922008-04-23 19:51:14 +020020 *
Josh Wu7dc37de2013-08-05 19:14:35 +080021 * Add Nand Flash Controller support for SAMA5 SoC
22 * © Copyright 2013 ATMEL, Josh Wu (josh.wu@atmel.com)
23 *
Andrew Victor42cb1402006-10-19 18:24:35 +020024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000030#include <linux/dma-mapping.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020031#include <linux/slab.h>
32#include <linux/module.h>
Simon Polettef4fa6972009-05-27 18:19:39 +030033#include <linux/moduleparam.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020034#include <linux/platform_device.h>
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080035#include <linux/of.h>
36#include <linux/of_device.h>
37#include <linux/of_gpio.h>
38#include <linux/of_mtd.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020039#include <linux/mtd/mtd.h>
40#include <linux/mtd/nand.h>
41#include <linux/mtd/partitions.h>
42
Josh Wu7dc37de2013-08-05 19:14:35 +080043#include <linux/delay.h>
Hans-Christian Egtvedt5c39c4c2011-04-13 15:55:17 +020044#include <linux/dmaengine.h>
David Woodhouse90574d02008-06-07 08:49:00 +010045#include <linux/gpio.h>
Josh Wu7dc37de2013-08-05 19:14:35 +080046#include <linux/interrupt.h>
David Woodhouse90574d02008-06-07 08:49:00 +010047#include <linux/io.h>
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +080048#include <linux/platform_data/atmel.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020049
Hong Xucbc6c5e2011-01-18 14:36:05 +080050static int use_dma = 1;
51module_param(use_dma, int, 0);
52
Simon Polettef4fa6972009-05-27 18:19:39 +030053static int on_flash_bbt = 0;
54module_param(on_flash_bbt, int, 0);
55
Richard Genoud77f54922008-04-23 19:51:14 +020056/* Register access macros */
57#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020058 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020059#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020060 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020061
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020062#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Josh Wu7dc37de2013-08-05 19:14:35 +080063#include "atmel_nand_nfc.h" /* Nand Flash Controller definition */
Richard Genoud77f54922008-04-23 19:51:14 +020064
65/* oob layout for large page size
66 * bad block info is on bytes 0 and 1
67 * the bytes have to be consecutives to avoid
68 * several NAND_CMD_RNDOUT during read
69 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020070static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020071 .eccbytes = 4,
72 .eccpos = {60, 61, 62, 63},
73 .oobfree = {
74 {2, 58}
75 },
76};
77
78/* oob layout for small page size
79 * bad block info is on bytes 4 and 5
80 * the bytes have to be consecutives to avoid
81 * several NAND_CMD_RNDOUT during read
82 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020083static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020084 .eccbytes = 4,
85 .eccpos = {0, 1, 2, 3},
86 .oobfree = {
87 {6, 10}
88 },
89};
90
Josh Wu7dc37de2013-08-05 19:14:35 +080091struct atmel_nfc {
92 void __iomem *base_cmd_regs;
93 void __iomem *hsmc_regs;
94 void __iomem *sram_bank0;
95 dma_addr_t sram_bank0_phys;
Josh Wu1ae9c092013-08-05 19:14:36 +080096 bool use_nfc_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +080097 bool write_by_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +080098
99 bool is_initialized;
100 struct completion comp_nfc;
Josh Wu1ae9c092013-08-05 19:14:36 +0800101
102 /* Point to the sram bank which include readed data via NFC */
103 void __iomem *data_in_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +0800104 bool will_write_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +0800105};
106static struct atmel_nfc nand_nfc;
107
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200108struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +0200109 struct nand_chip nand_chip;
110 struct mtd_info mtd;
111 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800112 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800113 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +0200114 struct device *dev;
115 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800116
117 struct completion comp;
118 struct dma_chan *dma_chan;
Josh Wua41b51a2012-06-29 17:47:54 +0800119
Josh Wu7dc37de2013-08-05 19:14:35 +0800120 struct atmel_nfc *nfc;
121
Josh Wua41b51a2012-06-29 17:47:54 +0800122 bool has_pmecc;
123 u8 pmecc_corr_cap;
124 u16 pmecc_sector_size;
125 u32 pmecc_lookup_table_offset;
Josh Wue66b4312013-01-23 20:47:11 +0800126 u32 pmecc_lookup_table_offset_512;
127 u32 pmecc_lookup_table_offset_1024;
Josh Wu1c7b8742012-06-29 17:47:55 +0800128
129 int pmecc_bytes_per_sector;
130 int pmecc_sector_number;
131 int pmecc_degree; /* Degree of remainders */
132 int pmecc_cw_len; /* Length of codeword */
133
134 void __iomem *pmerrloc_base;
135 void __iomem *pmecc_rom_base;
136
137 /* lookup table for alpha_to and index_of */
138 void __iomem *pmecc_alpha_to;
139 void __iomem *pmecc_index_of;
140
141 /* data for pmecc computation */
142 int16_t *pmecc_partial_syn;
143 int16_t *pmecc_si;
144 int16_t *pmecc_smu; /* Sigma table */
145 int16_t *pmecc_lmu; /* polynomal order */
146 int *pmecc_mu;
147 int *pmecc_dmu;
148 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200149};
150
Josh Wu1c7b8742012-06-29 17:47:55 +0800151static struct nand_ecclayout atmel_pmecc_oobinfo;
152
Andrew Victor42cb1402006-10-19 18:24:35 +0200153/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900154 * Enable NAND.
155 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200156static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900157{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800158 if (gpio_is_valid(host->board.enable_pin))
159 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900160}
161
162/*
163 * Disable NAND.
164 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200165static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900166{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800167 if (gpio_is_valid(host->board.enable_pin))
168 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900169}
170
171/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200172 * Hardware specific access to control-lines
173 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200174static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200175{
176 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200177 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200178
Atsushi Nemoto81365082008-04-27 01:51:12 +0900179 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900180 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200181 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900182 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200183 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900184 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200185 if (cmd == NAND_CMD_NONE)
186 return;
187
188 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800189 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200190 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800191 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200192}
193
194/*
195 * Read the Device Ready pin.
196 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200197static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200198{
199 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200200 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200201
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800202 return gpio_get_value(host->board.rdy_pin) ^
203 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200204}
205
Josh Wu7dc37de2013-08-05 19:14:35 +0800206/* Set up for hardware ready pin and enable pin. */
207static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
208{
209 struct nand_chip *chip = mtd->priv;
210 struct atmel_nand_host *host = chip->priv;
211 int res = 0;
212
213 if (gpio_is_valid(host->board.rdy_pin)) {
214 res = devm_gpio_request(host->dev,
215 host->board.rdy_pin, "nand_rdy");
216 if (res < 0) {
217 dev_err(host->dev,
218 "can't request rdy gpio %d\n",
219 host->board.rdy_pin);
220 return res;
221 }
222
223 res = gpio_direction_input(host->board.rdy_pin);
224 if (res < 0) {
225 dev_err(host->dev,
226 "can't request input direction rdy gpio %d\n",
227 host->board.rdy_pin);
228 return res;
229 }
230
231 chip->dev_ready = atmel_nand_device_ready;
232 }
233
234 if (gpio_is_valid(host->board.enable_pin)) {
235 res = devm_gpio_request(host->dev,
236 host->board.enable_pin, "nand_enable");
237 if (res < 0) {
238 dev_err(host->dev,
239 "can't request enable gpio %d\n",
240 host->board.enable_pin);
241 return res;
242 }
243
244 res = gpio_direction_output(host->board.enable_pin, 1);
245 if (res < 0) {
246 dev_err(host->dev,
247 "can't request output direction enable gpio %d\n",
248 host->board.enable_pin);
249 return res;
250 }
251 }
252
253 return res;
254}
255
Josh Wu1ae9c092013-08-05 19:14:36 +0800256static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size)
257{
258 int i;
259 u32 *t = trg;
260 const __iomem u32 *s = src;
261
262 for (i = 0; i < (size >> 2); i++)
263 *t++ = readl_relaxed(s++);
264}
265
Josh Wu6054d4d2013-08-05 19:14:37 +0800266static void memcpy32_toio(void __iomem *trg, const void *src, int size)
267{
268 int i;
269 u32 __iomem *t = trg;
270 const u32 *s = src;
271
272 for (i = 0; i < (size >> 2); i++)
273 writel_relaxed(*s++, t++);
274}
275
Artem Bityutskiy50082312012-02-02 13:54:25 +0200276/*
277 * Minimal-overhead PIO for data access.
278 */
279static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
280{
281 struct nand_chip *nand_chip = mtd->priv;
Josh Wu1ae9c092013-08-05 19:14:36 +0800282 struct atmel_nand_host *host = nand_chip->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200283
Josh Wu1ae9c092013-08-05 19:14:36 +0800284 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
285 memcpy32_fromio(buf, host->nfc->data_in_sram, len);
286 host->nfc->data_in_sram += len;
287 } else {
288 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
289 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200290}
291
292static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
293{
294 struct nand_chip *nand_chip = mtd->priv;
Josh Wu1ae9c092013-08-05 19:14:36 +0800295 struct atmel_nand_host *host = nand_chip->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200296
Josh Wu1ae9c092013-08-05 19:14:36 +0800297 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
298 memcpy32_fromio(buf, host->nfc->data_in_sram, len);
299 host->nfc->data_in_sram += len;
300 } else {
301 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
302 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200303}
304
305static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
306{
307 struct nand_chip *nand_chip = mtd->priv;
308
309 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
310}
311
312static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
313{
314 struct nand_chip *nand_chip = mtd->priv;
315
316 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
317}
318
Hong Xucbc6c5e2011-01-18 14:36:05 +0800319static void dma_complete_func(void *completion)
320{
321 complete(completion);
322}
323
Josh Wu1ae9c092013-08-05 19:14:36 +0800324static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
325{
326 /* NFC only has two banks. Must be 0 or 1 */
327 if (bank > 1)
328 return -EINVAL;
329
330 if (bank) {
331 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
332 if (host->mtd.writesize > 2048)
333 return -EINVAL;
334 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
335 } else {
336 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
337 }
338
339 return 0;
340}
341
342static uint nfc_get_sram_off(struct atmel_nand_host *host)
343{
344 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
345 return NFC_SRAM_BANK1_OFFSET;
346 else
347 return 0;
348}
349
350static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
351{
352 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
353 return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
354 else
355 return host->nfc->sram_bank0_phys;
356}
357
Hong Xucbc6c5e2011-01-18 14:36:05 +0800358static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
359 int is_read)
360{
361 struct dma_device *dma_dev;
362 enum dma_ctrl_flags flags;
363 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
364 struct dma_async_tx_descriptor *tx = NULL;
365 dma_cookie_t cookie;
366 struct nand_chip *chip = mtd->priv;
367 struct atmel_nand_host *host = chip->priv;
368 void *p = buf;
369 int err = -EIO;
370 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Josh Wu1ae9c092013-08-05 19:14:36 +0800371 struct atmel_nfc *nfc = host->nfc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800372
Hong Xu80b4f812011-03-31 18:33:15 +0800373 if (buf >= high_memory)
374 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800375
376 dma_dev = host->dma_chan->device;
377
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200378 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800379
380 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
381 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
382 dev_err(host->dev, "Failed to dma_map_single\n");
383 goto err_buf;
384 }
385
386 if (is_read) {
Josh Wu1ae9c092013-08-05 19:14:36 +0800387 if (nfc && nfc->data_in_sram)
388 dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
389 - (nfc->sram_bank0 + nfc_get_sram_off(host)));
390 else
391 dma_src_addr = host->io_phys;
392
Hong Xucbc6c5e2011-01-18 14:36:05 +0800393 dma_dst_addr = phys_addr;
394 } else {
395 dma_src_addr = phys_addr;
Josh Wu6054d4d2013-08-05 19:14:37 +0800396
397 if (nfc && nfc->write_by_sram)
398 dma_dst_addr = nfc_sram_phys(host);
399 else
400 dma_dst_addr = host->io_phys;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800401 }
402
403 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
404 dma_src_addr, len, flags);
405 if (!tx) {
406 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
407 goto err_dma;
408 }
409
410 init_completion(&host->comp);
411 tx->callback = dma_complete_func;
412 tx->callback_param = &host->comp;
413
414 cookie = tx->tx_submit(tx);
415 if (dma_submit_error(cookie)) {
416 dev_err(host->dev, "Failed to do DMA tx_submit\n");
417 goto err_dma;
418 }
419
420 dma_async_issue_pending(host->dma_chan);
421 wait_for_completion(&host->comp);
422
Josh Wu1ae9c092013-08-05 19:14:36 +0800423 if (is_read && nfc && nfc->data_in_sram)
424 /* After read data from SRAM, need to increase the position */
425 nfc->data_in_sram += len;
426
Hong Xucbc6c5e2011-01-18 14:36:05 +0800427 err = 0;
428
429err_dma:
430 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
431err_buf:
432 if (err != 0)
433 dev_warn(host->dev, "Fall back to CPU I/O\n");
434 return err;
435}
436
437static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
438{
439 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200440 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800441
Nicolas Ferre9d515672011-04-01 16:40:44 +0200442 if (use_dma && len > mtd->oobsize)
443 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800444 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
445 return;
446
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800447 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200448 atmel_read_buf16(mtd, buf, len);
449 else
450 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800451}
452
453static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
454{
455 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200456 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800457
Nicolas Ferre9d515672011-04-01 16:40:44 +0200458 if (use_dma && len > mtd->oobsize)
459 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800460 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
461 return;
462
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800463 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200464 atmel_write_buf16(mtd, buf, len);
465 else
466 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800467}
468
David Brownell23a346c2008-07-03 23:40:16 -0700469/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800470 * Return number of ecc bytes per sector according to sector size and
471 * correction capability
472 *
473 * Following table shows what at91 PMECC supported:
474 * Correction Capability Sector_512_bytes Sector_1024_bytes
475 * ===================== ================ =================
476 * 2-bits 4-bytes 4-bytes
477 * 4-bits 7-bytes 7-bytes
478 * 8-bits 13-bytes 14-bytes
479 * 12-bits 20-bytes 21-bytes
480 * 24-bits 39-bytes 42-bytes
481 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500482static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800483{
484 int m = 12 + sector_size / 512;
485 return (m * cap + 7) / 8;
486}
487
Bill Pemberton06f25512012-11-19 13:23:07 -0500488static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800489 int oobsize, int ecc_len)
Josh Wu1c7b8742012-06-29 17:47:55 +0800490{
491 int i;
492
493 layout->eccbytes = ecc_len;
494
495 /* ECC will occupy the last ecc_len bytes continuously */
496 for (i = 0; i < ecc_len; i++)
497 layout->eccpos[i] = oobsize - ecc_len + i;
498
499 layout->oobfree[0].offset = 2;
500 layout->oobfree[0].length =
501 oobsize - ecc_len - layout->oobfree[0].offset;
502}
503
Bill Pemberton06f25512012-11-19 13:23:07 -0500504static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800505{
506 int table_size;
507
508 table_size = host->pmecc_sector_size == 512 ?
509 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
510
511 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
512 table_size * sizeof(int16_t);
513}
514
Bill Pemberton06f25512012-11-19 13:23:07 -0500515static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800516{
517 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800518 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800519
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800520 size = (2 * cap + 1) * sizeof(int16_t);
521 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
522 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
523 host->pmecc_lmu = devm_kzalloc(host->dev,
524 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
525 host->pmecc_smu = devm_kzalloc(host->dev,
526 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800527
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800528 size = (cap + 1) * sizeof(int);
529 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
530 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
531 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800532
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800533 if (!host->pmecc_partial_syn ||
534 !host->pmecc_si ||
535 !host->pmecc_lmu ||
536 !host->pmecc_smu ||
537 !host->pmecc_mu ||
538 !host->pmecc_dmu ||
539 !host->pmecc_delta)
540 return -ENOMEM;
541
542 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800543}
544
545static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
546{
547 struct nand_chip *nand_chip = mtd->priv;
548 struct atmel_nand_host *host = nand_chip->priv;
549 int i;
550 uint32_t value;
551
552 /* Fill odd syndromes */
553 for (i = 0; i < host->pmecc_corr_cap; i++) {
554 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
555 if (i & 1)
556 value >>= 16;
557 value &= 0xffff;
558 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
559 }
560}
561
562static void pmecc_substitute(struct mtd_info *mtd)
563{
564 struct nand_chip *nand_chip = mtd->priv;
565 struct atmel_nand_host *host = nand_chip->priv;
566 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
567 int16_t __iomem *index_of = host->pmecc_index_of;
568 int16_t *partial_syn = host->pmecc_partial_syn;
569 const int cap = host->pmecc_corr_cap;
570 int16_t *si;
571 int i, j;
572
573 /* si[] is a table that holds the current syndrome value,
574 * an element of that table belongs to the field
575 */
576 si = host->pmecc_si;
577
578 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
579
580 /* Computation 2t syndromes based on S(x) */
581 /* Odd syndromes */
582 for (i = 1; i < 2 * cap; i += 2) {
583 for (j = 0; j < host->pmecc_degree; j++) {
584 if (partial_syn[i] & ((unsigned short)0x1 << j))
585 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
586 }
587 }
588 /* Even syndrome = (Odd syndrome) ** 2 */
589 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
590 if (si[j] == 0) {
591 si[i] = 0;
592 } else {
593 int16_t tmp;
594
595 tmp = readw_relaxed(index_of + si[j]);
596 tmp = (tmp * 2) % host->pmecc_cw_len;
597 si[i] = readw_relaxed(alpha_to + tmp);
598 }
599 }
600
601 return;
602}
603
604static void pmecc_get_sigma(struct mtd_info *mtd)
605{
606 struct nand_chip *nand_chip = mtd->priv;
607 struct atmel_nand_host *host = nand_chip->priv;
608
609 int16_t *lmu = host->pmecc_lmu;
610 int16_t *si = host->pmecc_si;
611 int *mu = host->pmecc_mu;
612 int *dmu = host->pmecc_dmu; /* Discrepancy */
613 int *delta = host->pmecc_delta; /* Delta order */
614 int cw_len = host->pmecc_cw_len;
615 const int16_t cap = host->pmecc_corr_cap;
616 const int num = 2 * cap + 1;
617 int16_t __iomem *index_of = host->pmecc_index_of;
618 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
619 int i, j, k;
620 uint32_t dmu_0_count, tmp;
621 int16_t *smu = host->pmecc_smu;
622
623 /* index of largest delta */
624 int ro;
625 int largest;
626 int diff;
627
628 dmu_0_count = 0;
629
630 /* First Row */
631
632 /* Mu */
633 mu[0] = -1;
634
635 memset(smu, 0, sizeof(int16_t) * num);
636 smu[0] = 1;
637
638 /* discrepancy set to 1 */
639 dmu[0] = 1;
640 /* polynom order set to 0 */
641 lmu[0] = 0;
642 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
643
644 /* Second Row */
645
646 /* Mu */
647 mu[1] = 0;
648 /* Sigma(x) set to 1 */
649 memset(&smu[num], 0, sizeof(int16_t) * num);
650 smu[num] = 1;
651
652 /* discrepancy set to S1 */
653 dmu[1] = si[1];
654
655 /* polynom order set to 0 */
656 lmu[1] = 0;
657
658 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
659
660 /* Init the Sigma(x) last row */
661 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
662
663 for (i = 1; i <= cap; i++) {
664 mu[i + 1] = i << 1;
665 /* Begin Computing Sigma (Mu+1) and L(mu) */
666 /* check if discrepancy is set to 0 */
667 if (dmu[i] == 0) {
668 dmu_0_count++;
669
670 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
671 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
672 tmp += 2;
673 else
674 tmp += 1;
675
676 if (dmu_0_count == tmp) {
677 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
678 smu[(cap + 1) * num + j] =
679 smu[i * num + j];
680
681 lmu[cap + 1] = lmu[i];
682 return;
683 }
684
685 /* copy polynom */
686 for (j = 0; j <= lmu[i] >> 1; j++)
687 smu[(i + 1) * num + j] = smu[i * num + j];
688
689 /* copy previous polynom order to the next */
690 lmu[i + 1] = lmu[i];
691 } else {
692 ro = 0;
693 largest = -1;
694 /* find largest delta with dmu != 0 */
695 for (j = 0; j < i; j++) {
696 if ((dmu[j]) && (delta[j] > largest)) {
697 largest = delta[j];
698 ro = j;
699 }
700 }
701
702 /* compute difference */
703 diff = (mu[i] - mu[ro]);
704
705 /* Compute degree of the new smu polynomial */
706 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
707 lmu[i + 1] = lmu[i];
708 else
709 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
710
711 /* Init smu[i+1] with 0 */
712 for (k = 0; k < num; k++)
713 smu[(i + 1) * num + k] = 0;
714
715 /* Compute smu[i+1] */
716 for (k = 0; k <= lmu[ro] >> 1; k++) {
717 int16_t a, b, c;
718
719 if (!(smu[ro * num + k] && dmu[i]))
720 continue;
721 a = readw_relaxed(index_of + dmu[i]);
722 b = readw_relaxed(index_of + dmu[ro]);
723 c = readw_relaxed(index_of + smu[ro * num + k]);
724 tmp = a + (cw_len - b) + c;
725 a = readw_relaxed(alpha_to + tmp % cw_len);
726 smu[(i + 1) * num + (k + diff)] = a;
727 }
728
729 for (k = 0; k <= lmu[i] >> 1; k++)
730 smu[(i + 1) * num + k] ^= smu[i * num + k];
731 }
732
733 /* End Computing Sigma (Mu+1) and L(mu) */
734 /* In either case compute delta */
735 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
736
737 /* Do not compute discrepancy for the last iteration */
738 if (i >= cap)
739 continue;
740
741 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
742 tmp = 2 * (i - 1);
743 if (k == 0) {
744 dmu[i + 1] = si[tmp + 3];
745 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
746 int16_t a, b, c;
747 a = readw_relaxed(index_of +
748 smu[(i + 1) * num + k]);
749 b = si[2 * (i - 1) + 3 - k];
750 c = readw_relaxed(index_of + b);
751 tmp = a + c;
752 tmp %= cw_len;
753 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
754 dmu[i + 1];
755 }
756 }
757 }
758
759 return;
760}
761
762static int pmecc_err_location(struct mtd_info *mtd)
763{
764 struct nand_chip *nand_chip = mtd->priv;
765 struct atmel_nand_host *host = nand_chip->priv;
766 unsigned long end_time;
767 const int cap = host->pmecc_corr_cap;
768 const int num = 2 * cap + 1;
769 int sector_size = host->pmecc_sector_size;
770 int err_nbr = 0; /* number of error */
771 int roots_nbr; /* number of roots */
772 int i;
773 uint32_t val;
774 int16_t *smu = host->pmecc_smu;
775
776 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
777
778 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
779 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
780 smu[(cap + 1) * num + i]);
781 err_nbr++;
782 }
783
784 val = (err_nbr - 1) << 16;
785 if (sector_size == 1024)
786 val |= 1;
787
788 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
789 pmerrloc_writel(host->pmerrloc_base, ELEN,
790 sector_size * 8 + host->pmecc_degree * cap);
791
792 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
793 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
794 & PMERRLOC_CALC_DONE)) {
795 if (unlikely(time_after(jiffies, end_time))) {
796 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
797 return -1;
798 }
799 cpu_relax();
800 }
801
802 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
803 & PMERRLOC_ERR_NUM_MASK) >> 8;
804 /* Number of roots == degree of smu hence <= cap */
805 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
806 return err_nbr - 1;
807
808 /* Number of roots does not match the degree of smu
809 * unable to correct error */
810 return -1;
811}
812
813static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
814 int sector_num, int extra_bytes, int err_nbr)
815{
816 struct nand_chip *nand_chip = mtd->priv;
817 struct atmel_nand_host *host = nand_chip->priv;
818 int i = 0;
819 int byte_pos, bit_pos, sector_size, pos;
820 uint32_t tmp;
821 uint8_t err_byte;
822
823 sector_size = host->pmecc_sector_size;
824
825 while (err_nbr) {
826 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
827 byte_pos = tmp / 8;
828 bit_pos = tmp % 8;
829
830 if (byte_pos >= (sector_size + extra_bytes))
831 BUG(); /* should never happen */
832
833 if (byte_pos < sector_size) {
834 err_byte = *(buf + byte_pos);
835 *(buf + byte_pos) ^= (1 << bit_pos);
836
837 pos = sector_num * host->pmecc_sector_size + byte_pos;
838 dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
839 pos, bit_pos, err_byte, *(buf + byte_pos));
840 } else {
841 /* Bit flip in OOB area */
842 tmp = sector_num * host->pmecc_bytes_per_sector
843 + (byte_pos - sector_size);
844 err_byte = ecc[tmp];
845 ecc[tmp] ^= (1 << bit_pos);
846
847 pos = tmp + nand_chip->ecc.layout->eccpos[0];
848 dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
849 pos, bit_pos, err_byte, ecc[tmp]);
850 }
851
852 i++;
853 err_nbr--;
854 }
855
856 return;
857}
858
859static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
860 u8 *ecc)
861{
862 struct nand_chip *nand_chip = mtd->priv;
863 struct atmel_nand_host *host = nand_chip->priv;
864 int i, err_nbr, eccbytes;
865 uint8_t *buf_pos;
Josh Wuc0c70d92012-11-27 18:50:31 +0800866 int total_err = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800867
868 eccbytes = nand_chip->ecc.bytes;
869 for (i = 0; i < eccbytes; i++)
870 if (ecc[i] != 0xff)
871 goto normal_check;
872 /* Erased page, return OK */
873 return 0;
874
875normal_check:
876 for (i = 0; i < host->pmecc_sector_number; i++) {
877 err_nbr = 0;
878 if (pmecc_stat & 0x1) {
879 buf_pos = buf + i * host->pmecc_sector_size;
880
881 pmecc_gen_syndrome(mtd, i);
882 pmecc_substitute(mtd);
883 pmecc_get_sigma(mtd);
884
885 err_nbr = pmecc_err_location(mtd);
886 if (err_nbr == -1) {
887 dev_err(host->dev, "PMECC: Too many errors\n");
888 mtd->ecc_stats.failed++;
889 return -EIO;
890 } else {
891 pmecc_correct_data(mtd, buf_pos, ecc, i,
892 host->pmecc_bytes_per_sector, err_nbr);
893 mtd->ecc_stats.corrected += err_nbr;
Josh Wuc0c70d92012-11-27 18:50:31 +0800894 total_err += err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800895 }
896 }
897 pmecc_stat >>= 1;
898 }
899
Josh Wuc0c70d92012-11-27 18:50:31 +0800900 return total_err;
Josh Wu1c7b8742012-06-29 17:47:55 +0800901}
902
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800903static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
904{
905 u32 val;
906
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800907 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
908 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
909 return;
910 }
911
Josh Wu1fad0e82013-08-07 17:58:11 +0800912 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
913 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
914 val = pmecc_readl_relaxed(host->ecc, CFG);
915
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800916 if (ecc_op == NAND_ECC_READ)
917 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
918 | PMECC_CFG_AUTO_ENABLE);
919 else
920 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
921 & ~PMECC_CFG_AUTO_ENABLE);
922
923 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
924 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
925}
926
Josh Wu1c7b8742012-06-29 17:47:55 +0800927static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
928 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
929{
930 struct atmel_nand_host *host = chip->priv;
931 int eccsize = chip->ecc.size;
932 uint8_t *oob = chip->oob_poi;
933 uint32_t *eccpos = chip->ecc.layout->eccpos;
934 uint32_t stat;
935 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800936 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800937
Josh Wu1ae9c092013-08-05 19:14:36 +0800938 if (!host->nfc || !host->nfc->use_nfc_sram)
939 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800940
941 chip->read_buf(mtd, buf, eccsize);
942 chip->read_buf(mtd, oob, mtd->oobsize);
943
944 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
945 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
946 if (unlikely(time_after(jiffies, end_time))) {
947 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
948 return -EIO;
949 }
950 cpu_relax();
951 }
952
953 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800954 if (stat != 0) {
955 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
956 if (bitflips < 0)
957 /* uncorrectable errors */
958 return 0;
959 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800960
Josh Wuc0c70d92012-11-27 18:50:31 +0800961 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800962}
963
964static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
965 struct nand_chip *chip, const uint8_t *buf, int oob_required)
966{
967 struct atmel_nand_host *host = chip->priv;
968 uint32_t *eccpos = chip->ecc.layout->eccpos;
969 int i, j;
970 unsigned long end_time;
971
Josh Wu6054d4d2013-08-05 19:14:37 +0800972 if (!host->nfc || !host->nfc->write_by_sram) {
973 pmecc_enable(host, NAND_ECC_WRITE);
974 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
975 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800976
977 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
978 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
979 if (unlikely(time_after(jiffies, end_time))) {
980 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
981 return -EIO;
982 }
983 cpu_relax();
984 }
985
986 for (i = 0; i < host->pmecc_sector_number; i++) {
987 for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
988 int pos;
989
990 pos = i * host->pmecc_bytes_per_sector + j;
991 chip->oob_poi[eccpos[pos]] =
992 pmecc_readb_ecc_relaxed(host->ecc, i, j);
993 }
994 }
995 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
996
997 return 0;
998}
999
1000static void atmel_pmecc_core_init(struct mtd_info *mtd)
1001{
1002 struct nand_chip *nand_chip = mtd->priv;
1003 struct atmel_nand_host *host = nand_chip->priv;
1004 uint32_t val = 0;
1005 struct nand_ecclayout *ecc_layout;
1006
1007 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
1008 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1009
1010 switch (host->pmecc_corr_cap) {
1011 case 2:
1012 val = PMECC_CFG_BCH_ERR2;
1013 break;
1014 case 4:
1015 val = PMECC_CFG_BCH_ERR4;
1016 break;
1017 case 8:
1018 val = PMECC_CFG_BCH_ERR8;
1019 break;
1020 case 12:
1021 val = PMECC_CFG_BCH_ERR12;
1022 break;
1023 case 24:
1024 val = PMECC_CFG_BCH_ERR24;
1025 break;
1026 }
1027
1028 if (host->pmecc_sector_size == 512)
1029 val |= PMECC_CFG_SECTOR512;
1030 else if (host->pmecc_sector_size == 1024)
1031 val |= PMECC_CFG_SECTOR1024;
1032
1033 switch (host->pmecc_sector_number) {
1034 case 1:
1035 val |= PMECC_CFG_PAGE_1SECTOR;
1036 break;
1037 case 2:
1038 val |= PMECC_CFG_PAGE_2SECTORS;
1039 break;
1040 case 4:
1041 val |= PMECC_CFG_PAGE_4SECTORS;
1042 break;
1043 case 8:
1044 val |= PMECC_CFG_PAGE_8SECTORS;
1045 break;
1046 }
1047
1048 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
1049 | PMECC_CFG_AUTO_DISABLE);
1050 pmecc_writel(host->ecc, CFG, val);
1051
1052 ecc_layout = nand_chip->ecc.layout;
1053 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
1054 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
1055 pmecc_writel(host->ecc, EADDR,
1056 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
1057 /* See datasheet about PMECC Clock Control Register */
1058 pmecc_writel(host->ecc, CLK, 2);
1059 pmecc_writel(host->ecc, IDR, 0xff);
1060 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
1061}
1062
Josh Wu84cfbbb2013-01-23 20:47:12 +08001063/*
1064 * Get ECC requirement in ONFI parameters, returns -1 if ONFI
1065 * parameters is not supported.
1066 * return 0 if success to get the ECC requirement.
1067 */
1068static int get_onfi_ecc_param(struct nand_chip *chip,
1069 int *ecc_bits, int *sector_size)
1070{
1071 *ecc_bits = *sector_size = 0;
1072
1073 if (chip->onfi_params.ecc_bits == 0xff)
1074 /* TODO: the sector_size and ecc_bits need to be find in
1075 * extended ecc parameter, currently we don't support it.
1076 */
1077 return -1;
1078
1079 *ecc_bits = chip->onfi_params.ecc_bits;
1080
1081 /* The default sector size (ecc codeword size) is 512 */
1082 *sector_size = 512;
1083
1084 return 0;
1085}
1086
1087/*
1088 * Get ecc requirement from ONFI parameters ecc requirement.
1089 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
1090 * will set them according to ONFI ecc requirement. Otherwise, use the
1091 * value in DTS file.
1092 * return 0 if success. otherwise return error code.
1093 */
1094static int pmecc_choose_ecc(struct atmel_nand_host *host,
1095 int *cap, int *sector_size)
1096{
1097 /* Get ECC requirement from ONFI parameters */
1098 *cap = *sector_size = 0;
1099 if (host->nand_chip.onfi_version) {
1100 if (!get_onfi_ecc_param(&host->nand_chip, cap, sector_size))
1101 dev_info(host->dev, "ONFI params, minimum required ECC: %d bits in %d bytes\n",
1102 *cap, *sector_size);
1103 else
1104 dev_info(host->dev, "NAND chip ECC reqirement is in Extended ONFI parameter, we don't support yet.\n");
1105 } else {
1106 dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes");
1107 }
1108 if (*cap == 0 && *sector_size == 0) {
1109 *cap = 2;
1110 *sector_size = 512;
1111 }
1112
1113 /* If dts file doesn't specify then use the one in ONFI parameters */
1114 if (host->pmecc_corr_cap == 0) {
1115 /* use the most fitable ecc bits (the near bigger one ) */
1116 if (*cap <= 2)
1117 host->pmecc_corr_cap = 2;
1118 else if (*cap <= 4)
1119 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +08001120 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001121 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +08001122 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001123 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +08001124 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001125 host->pmecc_corr_cap = 24;
1126 else
1127 return -EINVAL;
1128 }
1129 if (host->pmecc_sector_size == 0) {
1130 /* use the most fitable sector size (the near smaller one ) */
1131 if (*sector_size >= 1024)
1132 host->pmecc_sector_size = 1024;
1133 else if (*sector_size >= 512)
1134 host->pmecc_sector_size = 512;
1135 else
1136 return -EINVAL;
1137 }
1138 return 0;
1139}
1140
Josh Wu1c7b8742012-06-29 17:47:55 +08001141static int __init atmel_pmecc_nand_init_params(struct platform_device *pdev,
1142 struct atmel_nand_host *host)
1143{
1144 struct mtd_info *mtd = &host->mtd;
1145 struct nand_chip *nand_chip = &host->nand_chip;
1146 struct resource *regs, *regs_pmerr, *regs_rom;
1147 int cap, sector_size, err_no;
1148
Josh Wu84cfbbb2013-01-23 20:47:12 +08001149 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
1150 if (err_no) {
1151 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1152 return err_no;
1153 }
1154
Richard Genoudf666d642013-07-30 17:17:29 +02001155 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001156 sector_size != host->pmecc_sector_size)
1157 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001158
Josh Wu1c7b8742012-06-29 17:47:55 +08001159 cap = host->pmecc_corr_cap;
1160 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001161 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1162 host->pmecc_lookup_table_offset_512 :
1163 host->pmecc_lookup_table_offset_1024;
1164
Josh Wu1c7b8742012-06-29 17:47:55 +08001165 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1166 cap, sector_size);
1167
1168 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1169 if (!regs) {
1170 dev_warn(host->dev,
1171 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1172 nand_chip->ecc.mode = NAND_ECC_SOFT;
1173 return 0;
1174 }
1175
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001176 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1177 if (IS_ERR(host->ecc)) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001178 dev_err(host->dev, "ioremap failed\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001179 err_no = PTR_ERR(host->ecc);
1180 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001181 }
1182
1183 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001184 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1185 if (IS_ERR(host->pmerrloc_base)) {
1186 dev_err(host->dev,
1187 "Can not get I/O resource for PMECC ERRLOC controller!\n");
1188 err_no = PTR_ERR(host->pmerrloc_base);
1189 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001190 }
1191
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001192 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1193 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, regs_rom);
1194 if (IS_ERR(host->pmecc_rom_base)) {
1195 dev_err(host->dev, "Can not get I/O resource for ROM!\n");
1196 err_no = PTR_ERR(host->pmecc_rom_base);
1197 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001198 }
1199
1200 /* ECC is calculated for the whole page (1 step) */
1201 nand_chip->ecc.size = mtd->writesize;
1202
1203 /* set ECC page size and oob layout */
1204 switch (mtd->writesize) {
1205 case 2048:
Josh Wu2fa831f2013-08-19 18:05:44 +08001206 host->pmecc_degree = (sector_size == 512) ?
1207 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Josh Wu1c7b8742012-06-29 17:47:55 +08001208 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
1209 host->pmecc_sector_number = mtd->writesize / sector_size;
1210 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
1211 cap, sector_size);
1212 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1213 host->pmecc_index_of = host->pmecc_rom_base +
1214 host->pmecc_lookup_table_offset;
1215
1216 nand_chip->ecc.steps = 1;
1217 nand_chip->ecc.strength = cap;
1218 nand_chip->ecc.bytes = host->pmecc_bytes_per_sector *
1219 host->pmecc_sector_number;
1220 if (nand_chip->ecc.bytes > mtd->oobsize - 2) {
1221 dev_err(host->dev, "No room for ECC bytes\n");
1222 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001223 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001224 }
1225 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1226 mtd->oobsize,
1227 nand_chip->ecc.bytes);
1228 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1229 break;
1230 case 512:
1231 case 1024:
1232 case 4096:
1233 /* TODO */
1234 dev_warn(host->dev,
1235 "Unsupported page size for PMECC, use Software ECC\n");
1236 default:
1237 /* page size not handled by HW ECC */
1238 /* switching back to soft ECC */
1239 nand_chip->ecc.mode = NAND_ECC_SOFT;
1240 return 0;
1241 }
1242
1243 /* Allocate data for PMECC computation */
1244 err_no = pmecc_data_alloc(host);
1245 if (err_no) {
1246 dev_err(host->dev,
1247 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001248 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001249 }
1250
1251 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1252 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1253
1254 atmel_pmecc_core_init(mtd);
1255
1256 return 0;
1257
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001258err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001259 return err_no;
1260}
1261
1262/*
Richard Genoud77f54922008-04-23 19:51:14 +02001263 * Calculate HW ECC
1264 *
1265 * function called after a write
1266 *
1267 * mtd: MTD block structure
1268 * dat: raw data (unused)
1269 * ecc_code: buffer for ECC
1270 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001271static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001272 const u_char *dat, unsigned char *ecc_code)
1273{
1274 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001275 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001276 unsigned int ecc_value;
1277
1278 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001279 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001280
Richard Genoud3fc23892008-10-12 08:42:28 +02001281 ecc_code[0] = ecc_value & 0xFF;
1282 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001283
1284 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001285 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001286
Richard Genoud3fc23892008-10-12 08:42:28 +02001287 ecc_code[2] = ecc_value & 0xFF;
1288 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001289
1290 return 0;
1291}
1292
1293/*
1294 * HW ECC read page function
1295 *
1296 * mtd: mtd info structure
1297 * chip: nand chip info structure
1298 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001299 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001300 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001301static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1302 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001303{
1304 int eccsize = chip->ecc.size;
1305 int eccbytes = chip->ecc.bytes;
1306 uint32_t *eccpos = chip->ecc.layout->eccpos;
1307 uint8_t *p = buf;
1308 uint8_t *oob = chip->oob_poi;
1309 uint8_t *ecc_pos;
1310 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001311 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001312
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001313 /*
1314 * Errata: ALE is incorrectly wired up to the ECC controller
1315 * on the AP7000, so it will include the address cycles in the
1316 * ECC calculation.
1317 *
1318 * Workaround: Reset the parity registers before reading the
1319 * actual data.
1320 */
Josh Wu71b94e22013-05-09 15:34:54 +08001321 struct atmel_nand_host *host = chip->priv;
1322 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001323 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001324
Richard Genoud77f54922008-04-23 19:51:14 +02001325 /* read the page */
1326 chip->read_buf(mtd, p, eccsize);
1327
1328 /* move to ECC position if needed */
1329 if (eccpos[0] != 0) {
1330 /* This only works on large pages
1331 * because the ECC controller waits for
1332 * NAND_CMD_RNDOUTSTART after the
1333 * NAND_CMD_RNDOUT.
1334 * anyway, for small pages, the eccpos[0] == 0
1335 */
1336 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1337 mtd->writesize + eccpos[0], -1);
1338 }
1339
1340 /* the ECC controller needs to read the ECC just after the data */
1341 ecc_pos = oob + eccpos[0];
1342 chip->read_buf(mtd, ecc_pos, eccbytes);
1343
1344 /* check if there's an error */
1345 stat = chip->ecc.correct(mtd, p, oob, NULL);
1346
Mike Dunn3f91e942012-04-25 12:06:09 -07001347 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001348 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001349 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001350 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001351 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1352 }
Richard Genoud77f54922008-04-23 19:51:14 +02001353
1354 /* get back to oob start (end of page) */
1355 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1356
1357 /* read the oob */
1358 chip->read_buf(mtd, oob, mtd->oobsize);
1359
Mike Dunn3f91e942012-04-25 12:06:09 -07001360 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001361}
1362
1363/*
1364 * HW ECC Correction
1365 *
1366 * function called after a read
1367 *
1368 * mtd: MTD block structure
1369 * dat: raw data read from the chip
1370 * read_ecc: ECC from the chip (unused)
1371 * isnull: unused
1372 *
1373 * Detect and correct a 1 bit error for a page
1374 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001375static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001376 u_char *read_ecc, u_char *isnull)
1377{
1378 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001379 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001380 unsigned int ecc_status;
1381 unsigned int ecc_word, ecc_bit;
1382
1383 /* get the status from the Status Register */
1384 ecc_status = ecc_readl(host->ecc, SR);
1385
1386 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001387 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001388 return 0;
1389
1390 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001391 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001392 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001393 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001394 ecc_word >>= 4;
1395
1396 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001397 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001398 /* check if it is a freshly erased block
1399 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001400 if ((ecc_bit == ATMEL_ECC_BITADDR)
1401 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001402 /* the block has just been erased, return OK */
1403 return 0;
1404 }
1405 /* it doesn't seems to be a freshly
1406 * erased block.
1407 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001408 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001409 " Unable to correct.\n");
1410 return -EIO;
1411 }
1412
1413 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001414 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001415 /* there's nothing much to do here.
1416 * the bit error is on the ECC itself.
1417 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001418 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001419 " Nothing to correct\n");
1420 return 0;
1421 }
1422
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001423 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001424 " (word offset in the page :"
1425 " 0x%x bit offset : 0x%x)\n",
1426 ecc_word, ecc_bit);
1427 /* correct the error */
1428 if (nand_chip->options & NAND_BUSWIDTH_16) {
1429 /* 16 bits words */
1430 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1431 } else {
1432 /* 8 bits words */
1433 dat[ecc_word] ^= (1 << ecc_bit);
1434 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001435 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001436 return 1;
1437}
1438
1439/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001440 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001441 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001442static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1443{
Josh Wu71b94e22013-05-09 15:34:54 +08001444 struct nand_chip *nand_chip = mtd->priv;
1445 struct atmel_nand_host *host = nand_chip->priv;
1446
1447 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001448 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001449}
Richard Genoud77f54922008-04-23 19:51:14 +02001450
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001451#if defined(CONFIG_OF)
Bill Pemberton06f25512012-11-19 13:23:07 -05001452static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001453 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001454{
Josh Wuc0cf7872013-01-23 20:47:08 +08001455 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001456 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001457 int ecc_mode;
1458 struct atmel_nand_data *board = &host->board;
1459 enum of_gpio_flags flags;
1460
1461 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1462 if (val >= 32) {
1463 dev_err(host->dev, "invalid addr-offset %u\n", val);
1464 return -EINVAL;
1465 }
1466 board->ale = val;
1467 }
1468
1469 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1470 if (val >= 32) {
1471 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1472 return -EINVAL;
1473 }
1474 board->cle = val;
1475 }
1476
1477 ecc_mode = of_get_nand_ecc_mode(np);
1478
1479 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1480
1481 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1482
Josh Wu1b719262013-05-09 15:34:55 +08001483 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1484
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001485 if (of_get_nand_bus_width(np) == 16)
1486 board->bus_width_16 = 1;
1487
1488 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1489 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1490
1491 board->enable_pin = of_get_gpio(np, 1);
1492 board->det_pin = of_get_gpio(np, 2);
1493
Josh Wua41b51a2012-06-29 17:47:54 +08001494 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1495
Josh Wu7dc37de2013-08-05 19:14:35 +08001496 /* load the nfc driver if there is */
1497 of_platform_populate(np, NULL, NULL, host->dev);
1498
Josh Wua41b51a2012-06-29 17:47:54 +08001499 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1500 return 0; /* Not using PMECC */
1501
1502 /* use PMECC, get correction capability, sector size and lookup
1503 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001504 * If correction bits and sector size are not specified, then find
1505 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001506 */
Josh Wue66b4312013-01-23 20:47:11 +08001507 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1508 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1509 (val != 24)) {
1510 dev_err(host->dev,
1511 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1512 val);
1513 return -EINVAL;
1514 }
1515 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001516 }
Josh Wua41b51a2012-06-29 17:47:54 +08001517
Josh Wue66b4312013-01-23 20:47:11 +08001518 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1519 if ((val != 512) && (val != 1024)) {
1520 dev_err(host->dev,
1521 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1522 val);
1523 return -EINVAL;
1524 }
1525 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001526 }
Josh Wua41b51a2012-06-29 17:47:54 +08001527
1528 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1529 offset, 2) != 0) {
1530 dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
1531 return -EINVAL;
1532 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001533 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001534 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1535 return -EINVAL;
1536 }
Josh Wue66b4312013-01-23 20:47:11 +08001537 host->pmecc_lookup_table_offset_512 = offset[0];
1538 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001539
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001540 return 0;
1541}
1542#else
Bill Pemberton06f25512012-11-19 13:23:07 -05001543static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001544 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001545{
1546 return -EINVAL;
1547}
1548#endif
1549
Josh Wu3dfe41a2012-06-25 18:07:43 +08001550static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
1551 struct atmel_nand_host *host)
1552{
1553 struct mtd_info *mtd = &host->mtd;
1554 struct nand_chip *nand_chip = &host->nand_chip;
1555 struct resource *regs;
1556
1557 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1558 if (!regs) {
1559 dev_err(host->dev,
1560 "Can't get I/O resource regs, use software ECC\n");
1561 nand_chip->ecc.mode = NAND_ECC_SOFT;
1562 return 0;
1563 }
1564
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001565 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1566 if (IS_ERR(host->ecc)) {
Josh Wu3dfe41a2012-06-25 18:07:43 +08001567 dev_err(host->dev, "ioremap failed\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001568 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001569 }
1570
1571 /* ECC is calculated for the whole page (1 step) */
1572 nand_chip->ecc.size = mtd->writesize;
1573
1574 /* set ECC page size and oob layout */
1575 switch (mtd->writesize) {
1576 case 512:
1577 nand_chip->ecc.layout = &atmel_oobinfo_small;
1578 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1579 break;
1580 case 1024:
1581 nand_chip->ecc.layout = &atmel_oobinfo_large;
1582 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1583 break;
1584 case 2048:
1585 nand_chip->ecc.layout = &atmel_oobinfo_large;
1586 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1587 break;
1588 case 4096:
1589 nand_chip->ecc.layout = &atmel_oobinfo_large;
1590 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1591 break;
1592 default:
1593 /* page size not handled by HW ECC */
1594 /* switching back to soft ECC */
1595 nand_chip->ecc.mode = NAND_ECC_SOFT;
1596 return 0;
1597 }
1598
1599 /* set up for HW ECC */
1600 nand_chip->ecc.calculate = atmel_nand_calculate;
1601 nand_chip->ecc.correct = atmel_nand_correct;
1602 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1603 nand_chip->ecc.read_page = atmel_nand_read_page;
1604 nand_chip->ecc.bytes = 4;
1605 nand_chip->ecc.strength = 1;
1606
1607 return 0;
1608}
1609
Josh Wu7dc37de2013-08-05 19:14:35 +08001610/* SMC interrupt service routine */
1611static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
1612{
1613 struct atmel_nand_host *host = dev_id;
1614 u32 status, mask, pending;
1615 irqreturn_t ret = IRQ_HANDLED;
1616
1617 status = nfc_readl(host->nfc->hsmc_regs, SR);
1618 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1619 pending = status & mask;
1620
1621 if (pending & NFC_SR_XFR_DONE) {
1622 complete(&host->nfc->comp_nfc);
1623 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
1624 } else if (pending & NFC_SR_RB_EDGE) {
1625 complete(&host->nfc->comp_nfc);
1626 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_RB_EDGE);
1627 } else if (pending & NFC_SR_CMD_DONE) {
1628 complete(&host->nfc->comp_nfc);
1629 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
1630 } else {
1631 ret = IRQ_NONE;
1632 }
1633
1634 return ret;
1635}
1636
1637/* NFC(Nand Flash Controller) related functions */
1638static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
1639{
1640 unsigned long timeout;
1641 init_completion(&host->nfc->comp_nfc);
1642
1643 /* Enable interrupt that need to wait for */
1644 nfc_writel(host->nfc->hsmc_regs, IER, flag);
1645
1646 timeout = wait_for_completion_timeout(&host->nfc->comp_nfc,
1647 msecs_to_jiffies(NFC_TIME_OUT_MS));
1648 if (timeout)
1649 return 0;
1650
1651 /* Time out to wait for the interrupt */
1652 dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
1653 return -ETIMEDOUT;
1654}
1655
1656static int nfc_send_command(struct atmel_nand_host *host,
1657 unsigned int cmd, unsigned int addr, unsigned char cycle0)
1658{
1659 unsigned long timeout;
1660 dev_dbg(host->dev,
1661 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1662 cmd, addr, cycle0);
1663
1664 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1665 while (nfc_cmd_readl(NFCADDR_CMD_NFCBUSY, host->nfc->base_cmd_regs)
1666 & NFCADDR_CMD_NFCBUSY) {
1667 if (time_after(jiffies, timeout)) {
1668 dev_err(host->dev,
1669 "Time out to wait CMD_NFCBUSY ready!\n");
1670 return -ETIMEDOUT;
1671 }
1672 }
1673 nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
1674 nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
1675 return nfc_wait_interrupt(host, NFC_SR_CMD_DONE);
1676}
1677
1678static int nfc_device_ready(struct mtd_info *mtd)
1679{
1680 struct nand_chip *nand_chip = mtd->priv;
1681 struct atmel_nand_host *host = nand_chip->priv;
1682 if (!nfc_wait_interrupt(host, NFC_SR_RB_EDGE))
1683 return 1;
1684 return 0;
1685}
1686
1687static void nfc_select_chip(struct mtd_info *mtd, int chip)
1688{
1689 struct nand_chip *nand_chip = mtd->priv;
1690 struct atmel_nand_host *host = nand_chip->priv;
1691
1692 if (chip == -1)
1693 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
1694 else
1695 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
1696}
1697
1698static int nfc_make_addr(struct mtd_info *mtd, int column, int page_addr,
1699 unsigned int *addr1234, unsigned int *cycle0)
1700{
1701 struct nand_chip *chip = mtd->priv;
1702
1703 int acycle = 0;
1704 unsigned char addr_bytes[8];
1705 int index = 0, bit_shift;
1706
1707 BUG_ON(addr1234 == NULL || cycle0 == NULL);
1708
1709 *cycle0 = 0;
1710 *addr1234 = 0;
1711
1712 if (column != -1) {
1713 if (chip->options & NAND_BUSWIDTH_16)
1714 column >>= 1;
1715 addr_bytes[acycle++] = column & 0xff;
1716 if (mtd->writesize > 512)
1717 addr_bytes[acycle++] = (column >> 8) & 0xff;
1718 }
1719
1720 if (page_addr != -1) {
1721 addr_bytes[acycle++] = page_addr & 0xff;
1722 addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
1723 if (chip->chipsize > (128 << 20))
1724 addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
1725 }
1726
1727 if (acycle > 4)
1728 *cycle0 = addr_bytes[index++];
1729
1730 for (bit_shift = 0; index < acycle; bit_shift += 8)
1731 *addr1234 += addr_bytes[index++] << bit_shift;
1732
1733 /* return acycle in cmd register */
1734 return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
1735}
1736
1737static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
1738 int column, int page_addr)
1739{
1740 struct nand_chip *chip = mtd->priv;
1741 struct atmel_nand_host *host = chip->priv;
1742 unsigned long timeout;
1743 unsigned int nfc_addr_cmd = 0;
1744
1745 unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1746
1747 /* Set default settings: no cmd2, no addr cycle. read from nand */
1748 unsigned int cmd2 = 0;
1749 unsigned int vcmd2 = 0;
1750 int acycle = NFCADDR_CMD_ACYCLE_NONE;
1751 int csid = NFCADDR_CMD_CSID_3;
1752 int dataen = NFCADDR_CMD_DATADIS;
1753 int nfcwr = NFCADDR_CMD_NFCRD;
1754 unsigned int addr1234 = 0;
1755 unsigned int cycle0 = 0;
1756 bool do_addr = true;
Josh Wu1ae9c092013-08-05 19:14:36 +08001757 host->nfc->data_in_sram = NULL;
Josh Wu7dc37de2013-08-05 19:14:35 +08001758
1759 dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1760 __func__, command, column, page_addr);
1761
1762 switch (command) {
1763 case NAND_CMD_RESET:
1764 nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
1765 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1766 udelay(chip->chip_delay);
1767
1768 nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
1769 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1770 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
1771 if (time_after(jiffies, timeout)) {
1772 dev_err(host->dev,
1773 "Time out to wait status ready!\n");
1774 break;
1775 }
1776 }
1777 return;
1778 case NAND_CMD_STATUS:
1779 do_addr = false;
1780 break;
1781 case NAND_CMD_PARAM:
1782 case NAND_CMD_READID:
1783 do_addr = false;
1784 acycle = NFCADDR_CMD_ACYCLE_1;
1785 if (column != -1)
1786 addr1234 = column;
1787 break;
1788 case NAND_CMD_RNDOUT:
1789 cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
1790 vcmd2 = NFCADDR_CMD_VCMD2;
1791 break;
1792 case NAND_CMD_READ0:
1793 case NAND_CMD_READOOB:
1794 if (command == NAND_CMD_READOOB) {
1795 column += mtd->writesize;
1796 command = NAND_CMD_READ0; /* only READ0 is valid */
1797 cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1798 }
Josh Wu1ae9c092013-08-05 19:14:36 +08001799 if (host->nfc->use_nfc_sram) {
1800 /* Enable Data transfer to sram */
1801 dataen = NFCADDR_CMD_DATAEN;
1802
1803 /* Need enable PMECC now, since NFC will transfer
1804 * data in bus after sending nfc read command.
1805 */
1806 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1807 pmecc_enable(host, NAND_ECC_READ);
1808 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001809
1810 cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
1811 vcmd2 = NFCADDR_CMD_VCMD2;
1812 break;
1813 /* For prgramming command, the cmd need set to write enable */
1814 case NAND_CMD_PAGEPROG:
1815 case NAND_CMD_SEQIN:
1816 case NAND_CMD_RNDIN:
1817 nfcwr = NFCADDR_CMD_NFCWR;
Josh Wu6054d4d2013-08-05 19:14:37 +08001818 if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
1819 dataen = NFCADDR_CMD_DATAEN;
Josh Wu7dc37de2013-08-05 19:14:35 +08001820 break;
1821 default:
1822 break;
1823 }
1824
1825 if (do_addr)
1826 acycle = nfc_make_addr(mtd, column, page_addr, &addr1234,
1827 &cycle0);
1828
1829 nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
1830 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1831
Josh Wu1ae9c092013-08-05 19:14:36 +08001832 if (dataen == NFCADDR_CMD_DATAEN)
1833 if (nfc_wait_interrupt(host, NFC_SR_XFR_DONE))
1834 dev_err(host->dev, "something wrong, No XFR_DONE interrupt comes.\n");
1835
Josh Wu7dc37de2013-08-05 19:14:35 +08001836 /*
1837 * Program and erase have their own busy handlers status, sequential
1838 * in, and deplete1 need no delay.
1839 */
1840 switch (command) {
1841 case NAND_CMD_CACHEDPROG:
1842 case NAND_CMD_PAGEPROG:
1843 case NAND_CMD_ERASE1:
1844 case NAND_CMD_ERASE2:
1845 case NAND_CMD_RNDIN:
1846 case NAND_CMD_STATUS:
1847 case NAND_CMD_RNDOUT:
1848 case NAND_CMD_SEQIN:
1849 case NAND_CMD_READID:
1850 return;
1851
1852 case NAND_CMD_READ0:
Josh Wu1ae9c092013-08-05 19:14:36 +08001853 if (dataen == NFCADDR_CMD_DATAEN) {
1854 host->nfc->data_in_sram = host->nfc->sram_bank0 +
1855 nfc_get_sram_off(host);
1856 return;
1857 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001858 /* fall through */
1859 default:
1860 nfc_wait_interrupt(host, NFC_SR_RB_EDGE);
1861 }
1862}
1863
Josh Wu6054d4d2013-08-05 19:14:37 +08001864static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1865 uint32_t offset, int data_len, const uint8_t *buf,
1866 int oob_required, int page, int cached, int raw)
1867{
1868 int cfg, len;
1869 int status = 0;
1870 struct atmel_nand_host *host = chip->priv;
1871 void __iomem *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
1872
1873 /* Subpage write is not supported */
1874 if (offset || (data_len < mtd->writesize))
1875 return -EINVAL;
1876
1877 cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
1878 len = mtd->writesize;
1879
1880 if (unlikely(raw)) {
1881 len += mtd->oobsize;
1882 nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
1883 } else
1884 nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
1885
1886 /* Copy page data to sram that will write to nand via NFC */
1887 if (use_dma) {
1888 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
1889 /* Fall back to use cpu copy */
1890 memcpy32_toio(sram, buf, len);
1891 } else {
1892 memcpy32_toio(sram, buf, len);
1893 }
1894
1895 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1896 /*
1897 * When use NFC sram, need set up PMECC before send
1898 * NAND_CMD_SEQIN command. Since when the nand command
1899 * is sent, nfc will do transfer from sram and nand.
1900 */
1901 pmecc_enable(host, NAND_ECC_WRITE);
1902
1903 host->nfc->will_write_sram = true;
1904 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1905 host->nfc->will_write_sram = false;
1906
1907 if (likely(!raw))
1908 /* Need to write ecc into oob */
1909 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
1910
1911 if (status < 0)
1912 return status;
1913
1914 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1915 status = chip->waitfunc(mtd, chip);
1916
1917 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1918 status = chip->errstat(mtd, chip, FL_WRITING, status, page);
1919
1920 if (status & NAND_STATUS_FAIL)
1921 return -EIO;
1922
1923 return 0;
1924}
1925
Josh Wu1ae9c092013-08-05 19:14:36 +08001926static int nfc_sram_init(struct mtd_info *mtd)
1927{
1928 struct nand_chip *chip = mtd->priv;
1929 struct atmel_nand_host *host = chip->priv;
1930 int res = 0;
1931
1932 /* Initialize the NFC CFG register */
1933 unsigned int cfg_nfc = 0;
1934
1935 /* set page size and oob layout */
1936 switch (mtd->writesize) {
1937 case 512:
1938 cfg_nfc = NFC_CFG_PAGESIZE_512;
1939 break;
1940 case 1024:
1941 cfg_nfc = NFC_CFG_PAGESIZE_1024;
1942 break;
1943 case 2048:
1944 cfg_nfc = NFC_CFG_PAGESIZE_2048;
1945 break;
1946 case 4096:
1947 cfg_nfc = NFC_CFG_PAGESIZE_4096;
1948 break;
1949 case 8192:
1950 cfg_nfc = NFC_CFG_PAGESIZE_8192;
1951 break;
1952 default:
1953 dev_err(host->dev, "Unsupported page size for NFC.\n");
1954 res = -ENXIO;
1955 return res;
1956 }
1957
1958 /* oob bytes size = (NFCSPARESIZE + 1) * 4
1959 * Max support spare size is 512 bytes. */
1960 cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
1961 & NFC_CFG_NFC_SPARESIZE);
1962 /* default set a max timeout */
1963 cfg_nfc |= NFC_CFG_RSPARE |
1964 NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
1965
1966 nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
1967
Josh Wu6054d4d2013-08-05 19:14:37 +08001968 host->nfc->will_write_sram = false;
Josh Wu1ae9c092013-08-05 19:14:36 +08001969 nfc_set_sram_bank(host, 0);
1970
Josh Wu6054d4d2013-08-05 19:14:37 +08001971 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
1972 if (host->nfc->write_by_sram) {
1973 if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
1974 chip->ecc.mode == NAND_ECC_NONE)
1975 chip->write_page = nfc_sram_write_page;
1976 else
1977 host->nfc->write_by_sram = false;
1978 }
Josh Wu1ae9c092013-08-05 19:14:36 +08001979
Josh Wu6054d4d2013-08-05 19:14:37 +08001980 dev_info(host->dev, "Using NFC Sram read %s\n",
1981 host->nfc->write_by_sram ? "and write" : "");
Josh Wu1ae9c092013-08-05 19:14:36 +08001982 return 0;
1983}
1984
Josh Wu7dc37de2013-08-05 19:14:35 +08001985static struct platform_driver atmel_nand_nfc_driver;
Andrew Victor42cb1402006-10-19 18:24:35 +02001986/*
1987 * Probe for the NAND device.
1988 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001989static int __init atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02001990{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001991 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02001992 struct mtd_info *mtd;
1993 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02001994 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001995 struct mtd_part_parser_data ppdata = {};
Josh Wu7dc37de2013-08-05 19:14:35 +08001996 int res, irq;
Andrew Victor42cb1402006-10-19 18:24:35 +02001997
1998 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001999 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Andrew Victor42cb1402006-10-19 18:24:35 +02002000 if (!host) {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002001 printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
Andrew Victor42cb1402006-10-19 18:24:35 +02002002 return -ENOMEM;
2003 }
2004
Josh Wu7dc37de2013-08-05 19:14:35 +08002005 res = platform_driver_register(&atmel_nand_nfc_driver);
2006 if (res)
2007 dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
2008
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002009 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2010 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
2011 if (IS_ERR(host->io_base)) {
2012 dev_err(&pdev->dev, "atmel_nand: ioremap resource failed\n");
2013 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002014 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02002015 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002016 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02002017
2018 mtd = &host->mtd;
2019 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002020 host->dev = &pdev->dev;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002021 if (pdev->dev.of_node) {
2022 res = atmel_of_init_port(host, pdev->dev.of_node);
2023 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002024 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002025 } else {
Jingoo Han453810b2013-07-30 17:18:33 +09002026 memcpy(&host->board, dev_get_platdata(&pdev->dev),
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002027 sizeof(struct atmel_nand_data));
2028 }
Andrew Victor42cb1402006-10-19 18:24:35 +02002029
2030 nand_chip->priv = host; /* link the private data structures */
2031 mtd->priv = nand_chip;
2032 mtd->owner = THIS_MODULE;
2033
2034 /* Set address of NAND IO lines */
2035 nand_chip->IO_ADDR_R = host->io_base;
2036 nand_chip->IO_ADDR_W = host->io_base;
Ivan Kutena4265f82007-05-24 14:35:58 +03002037
Josh Wu7dc37de2013-08-05 19:14:35 +08002038 if (nand_nfc.is_initialized) {
2039 /* NFC driver is probed and initialized */
2040 host->nfc = &nand_nfc;
2041
2042 nand_chip->select_chip = nfc_select_chip;
2043 nand_chip->dev_ready = nfc_device_ready;
2044 nand_chip->cmdfunc = nfc_nand_command;
2045
2046 /* Initialize the interrupt for NFC */
2047 irq = platform_get_irq(pdev, 0);
2048 if (irq < 0) {
2049 dev_err(host->dev, "Cannot get HSMC irq!\n");
Wei Yongjunff52c672013-08-23 10:50:36 +08002050 res = irq;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002051 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002052 }
2053
Josh Wu7dc37de2013-08-05 19:14:35 +08002054 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
2055 0, "hsmc", host);
2056 if (res) {
2057 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
2058 irq);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002059 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002060 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002061 } else {
2062 res = atmel_nand_set_enable_ready_pins(mtd);
2063 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002064 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002065
Josh Wu7dc37de2013-08-05 19:14:35 +08002066 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002067 }
Ivan Kutena4265f82007-05-24 14:35:58 +03002068
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002069 nand_chip->ecc.mode = host->board.ecc_mode;
Andrew Victor42cb1402006-10-19 18:24:35 +02002070 nand_chip->chip_delay = 20; /* 20us command delay time */
2071
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002072 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02002073 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08002074
2075 nand_chip->read_buf = atmel_read_buf;
2076 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02002077
Andrew Victor42cb1402006-10-19 18:24:35 +02002078 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002079 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002080
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002081 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002082 res = devm_gpio_request(&pdev->dev,
2083 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002084 if (res < 0) {
2085 dev_err(&pdev->dev,
2086 "can't request det gpio %d\n",
2087 host->board.det_pin);
2088 goto err_no_card;
2089 }
2090
2091 res = gpio_direction_input(host->board.det_pin);
2092 if (res < 0) {
2093 dev_err(&pdev->dev,
2094 "can't request input direction det gpio %d\n",
2095 host->board.det_pin);
2096 goto err_no_card;
2097 }
2098
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002099 if (gpio_get_value(host->board.det_pin)) {
Simon Polettef4fa6972009-05-27 18:19:39 +03002100 printk(KERN_INFO "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01002101 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002102 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02002103 }
2104 }
2105
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002106 if (host->board.on_flash_bbt || on_flash_bbt) {
Simon Polettef4fa6972009-05-27 18:19:39 +03002107 printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -07002108 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa6972009-05-27 18:19:39 +03002109 }
2110
Josh Wu1b719262013-05-09 15:34:55 +08002111 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08002112 use_dma = 0;
2113
2114 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08002115 dma_cap_mask_t mask;
2116
2117 dma_cap_zero(mask);
2118 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02002119 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002120 if (!host->dma_chan) {
2121 dev_err(host->dev, "Failed to request DMA channel\n");
2122 use_dma = 0;
2123 }
2124 }
2125 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08002126 dev_info(host->dev, "Using %s for DMA transfers.\n",
2127 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08002128 else
2129 dev_info(host->dev, "No DMA support for NAND access.\n");
2130
Richard Genoud77f54922008-04-23 19:51:14 +02002131 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00002132 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02002133 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002134 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02002135 }
2136
Richard Genoud3fc23892008-10-12 08:42:28 +02002137 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08002138 if (host->has_pmecc)
2139 res = atmel_pmecc_nand_init_params(pdev, host);
2140 else
2141 res = atmel_hw_nand_init_params(pdev, host);
2142
Josh Wu3dfe41a2012-06-25 18:07:43 +08002143 if (res != 0)
2144 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02002145 }
2146
Josh Wu1ae9c092013-08-05 19:14:36 +08002147 /* initialize the nfc configuration register */
2148 if (host->nfc && host->nfc->use_nfc_sram) {
2149 res = nfc_sram_init(mtd);
2150 if (res) {
2151 host->nfc->use_nfc_sram = false;
2152 dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
2153 }
2154 }
2155
Richard Genoud77f54922008-04-23 19:51:14 +02002156 /* second phase scan */
2157 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02002158 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002159 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02002160 }
2161
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002162 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002163 ppdata.of_node = pdev->dev.of_node;
2164 res = mtd_device_parse_register(mtd, NULL, &ppdata,
2165 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02002166 if (!res)
2167 return res;
2168
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002169err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002170 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08002171 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08002172err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002173err_scan_ident:
2174err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002175 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002176 if (host->dma_chan)
2177 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002178err_nand_ioremap:
Josh Wu7dc37de2013-08-05 19:14:35 +08002179 platform_driver_unregister(&atmel_nand_nfc_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02002180 return res;
2181}
2182
2183/*
2184 * Remove a NAND device.
2185 */
David Brownell23a346c2008-07-03 23:40:16 -07002186static int __exit atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002187{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002188 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +02002189 struct mtd_info *mtd = &host->mtd;
2190
2191 nand_release(mtd);
2192
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002193 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002194
Josh Wu1c7b8742012-06-29 17:47:55 +08002195 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
2196 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2197 pmerrloc_writel(host->pmerrloc_base, ELDIS,
2198 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08002199 }
2200
Hong Xucbc6c5e2011-01-18 14:36:05 +08002201 if (host->dma_chan)
2202 dma_release_channel(host->dma_chan);
2203
Josh Wu7dc37de2013-08-05 19:14:35 +08002204 platform_driver_unregister(&atmel_nand_nfc_driver);
2205
Andrew Victor42cb1402006-10-19 18:24:35 +02002206 return 0;
2207}
2208
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002209#if defined(CONFIG_OF)
2210static const struct of_device_id atmel_nand_dt_ids[] = {
2211 { .compatible = "atmel,at91rm9200-nand" },
2212 { /* sentinel */ }
2213};
2214
2215MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
2216#endif
2217
Josh Wu7dc37de2013-08-05 19:14:35 +08002218static int atmel_nand_nfc_probe(struct platform_device *pdev)
2219{
2220 struct atmel_nfc *nfc = &nand_nfc;
2221 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
2222
2223 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2224 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
2225 if (IS_ERR(nfc->base_cmd_regs))
2226 return PTR_ERR(nfc->base_cmd_regs);
2227
2228 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2229 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
2230 if (IS_ERR(nfc->hsmc_regs))
2231 return PTR_ERR(nfc->hsmc_regs);
2232
2233 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2234 if (nfc_sram) {
2235 nfc->sram_bank0 = devm_ioremap_resource(&pdev->dev, nfc_sram);
Josh Wu1ae9c092013-08-05 19:14:36 +08002236 if (IS_ERR(nfc->sram_bank0)) {
Josh Wu7dc37de2013-08-05 19:14:35 +08002237 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2238 PTR_ERR(nfc->sram_bank0));
Josh Wu1ae9c092013-08-05 19:14:36 +08002239 } else {
2240 nfc->use_nfc_sram = true;
Josh Wu7dc37de2013-08-05 19:14:35 +08002241 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
Josh Wu6054d4d2013-08-05 19:14:37 +08002242
2243 if (pdev->dev.of_node)
2244 nfc->write_by_sram = of_property_read_bool(
2245 pdev->dev.of_node,
2246 "atmel,write-by-sram");
Josh Wu1ae9c092013-08-05 19:14:36 +08002247 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002248 }
2249
2250 nfc->is_initialized = true;
2251 dev_info(&pdev->dev, "NFC is probed.\n");
2252 return 0;
2253}
2254
Josh Wu9fe5f522013-08-07 11:36:09 +08002255#if defined(CONFIG_OF)
Josh Wu7dc37de2013-08-05 19:14:35 +08002256static struct of_device_id atmel_nand_nfc_match[] = {
2257 { .compatible = "atmel,sama5d3-nfc" },
2258 { /* sentinel */ }
2259};
Josh Wu9fe5f522013-08-07 11:36:09 +08002260#endif
Josh Wu7dc37de2013-08-05 19:14:35 +08002261
2262static struct platform_driver atmel_nand_nfc_driver = {
2263 .driver = {
2264 .name = "atmel_nand_nfc",
2265 .owner = THIS_MODULE,
2266 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2267 },
2268 .probe = atmel_nand_nfc_probe,
2269};
2270
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002271static struct platform_driver atmel_nand_driver = {
David Brownell23a346c2008-07-03 23:40:16 -07002272 .remove = __exit_p(atmel_nand_remove),
Andrew Victor42cb1402006-10-19 18:24:35 +02002273 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002274 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +02002275 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002276 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02002277 },
2278};
2279
Jingoo Hanc5345ed2013-03-05 13:30:04 +09002280module_platform_driver_probe(atmel_nand_driver, atmel_nand_probe);
Andrew Victor42cb1402006-10-19 18:24:35 +02002281
2282MODULE_LICENSE("GPL");
2283MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02002284MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002285MODULE_ALIAS("platform:atmel_nand");