blob: 5c1423a2ffb59d7e22166e7992ef6410db9f2ef6 [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
Boris BREZILLON2d405ec2014-09-13 01:23:59 +020030#include <linux/clk.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000031#include <linux/dma-mapping.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020032#include <linux/slab.h>
33#include <linux/module.h>
Simon Polettef4fa6972009-05-27 18:19:39 +030034#include <linux/moduleparam.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020035#include <linux/platform_device.h>
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080036#include <linux/of.h>
37#include <linux/of_device.h>
38#include <linux/of_gpio.h>
39#include <linux/of_mtd.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020040#include <linux/mtd/mtd.h>
41#include <linux/mtd/nand.h>
42#include <linux/mtd/partitions.h>
43
Josh Wu7dc37de2013-08-05 19:14:35 +080044#include <linux/delay.h>
Hans-Christian Egtvedt5c39c4c2011-04-13 15:55:17 +020045#include <linux/dmaengine.h>
David Woodhouse90574d02008-06-07 08:49:00 +010046#include <linux/gpio.h>
Josh Wu7dc37de2013-08-05 19:14:35 +080047#include <linux/interrupt.h>
David Woodhouse90574d02008-06-07 08:49:00 +010048#include <linux/io.h>
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +080049#include <linux/platform_data/atmel.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020050
Hong Xucbc6c5e2011-01-18 14:36:05 +080051static int use_dma = 1;
52module_param(use_dma, int, 0);
53
Simon Polettef4fa6972009-05-27 18:19:39 +030054static int on_flash_bbt = 0;
55module_param(on_flash_bbt, int, 0);
56
Richard Genoud77f54922008-04-23 19:51:14 +020057/* Register access macros */
58#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020059 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020060#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020061 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020062
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020063#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Josh Wu7dc37de2013-08-05 19:14:35 +080064#include "atmel_nand_nfc.h" /* Nand Flash Controller definition */
Richard Genoud77f54922008-04-23 19:51:14 +020065
66/* oob layout for large page size
67 * bad block info is on bytes 0 and 1
68 * the bytes have to be consecutives to avoid
69 * several NAND_CMD_RNDOUT during read
70 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020071static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020072 .eccbytes = 4,
73 .eccpos = {60, 61, 62, 63},
74 .oobfree = {
75 {2, 58}
76 },
77};
78
79/* oob layout for small page size
80 * bad block info is on bytes 4 and 5
81 * the bytes have to be consecutives to avoid
82 * several NAND_CMD_RNDOUT during read
83 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020084static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020085 .eccbytes = 4,
86 .eccpos = {0, 1, 2, 3},
87 .oobfree = {
88 {6, 10}
89 },
90};
91
Josh Wu7dc37de2013-08-05 19:14:35 +080092struct atmel_nfc {
93 void __iomem *base_cmd_regs;
94 void __iomem *hsmc_regs;
95 void __iomem *sram_bank0;
96 dma_addr_t sram_bank0_phys;
Josh Wu1ae9c092013-08-05 19:14:36 +080097 bool use_nfc_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +080098 bool write_by_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +080099
Boris BREZILLON2d405ec2014-09-13 01:23:59 +0200100 struct clk *clk;
101
Josh Wu7dc37de2013-08-05 19:14:35 +0800102 bool is_initialized;
Josh Wue4e06932014-06-10 17:50:11 +0800103 struct completion comp_ready;
104 struct completion comp_cmd_done;
105 struct completion comp_xfer_done;
Josh Wu1ae9c092013-08-05 19:14:36 +0800106
107 /* Point to the sram bank which include readed data via NFC */
108 void __iomem *data_in_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +0800109 bool will_write_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +0800110};
111static struct atmel_nfc nand_nfc;
112
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200113struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +0200114 struct nand_chip nand_chip;
115 struct mtd_info mtd;
116 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800117 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800118 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +0200119 struct device *dev;
120 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800121
122 struct completion comp;
123 struct dma_chan *dma_chan;
Josh Wua41b51a2012-06-29 17:47:54 +0800124
Josh Wu7dc37de2013-08-05 19:14:35 +0800125 struct atmel_nfc *nfc;
126
Josh Wua41b51a2012-06-29 17:47:54 +0800127 bool has_pmecc;
128 u8 pmecc_corr_cap;
129 u16 pmecc_sector_size;
Josh Wuabb1cd02014-10-11 18:01:50 +0800130 bool has_no_lookup_table;
Josh Wua41b51a2012-06-29 17:47:54 +0800131 u32 pmecc_lookup_table_offset;
Josh Wue66b4312013-01-23 20:47:11 +0800132 u32 pmecc_lookup_table_offset_512;
133 u32 pmecc_lookup_table_offset_1024;
Josh Wu1c7b8742012-06-29 17:47:55 +0800134
Josh Wu1c7b8742012-06-29 17:47:55 +0800135 int pmecc_degree; /* Degree of remainders */
136 int pmecc_cw_len; /* Length of codeword */
137
138 void __iomem *pmerrloc_base;
139 void __iomem *pmecc_rom_base;
140
141 /* lookup table for alpha_to and index_of */
142 void __iomem *pmecc_alpha_to;
143 void __iomem *pmecc_index_of;
144
145 /* data for pmecc computation */
146 int16_t *pmecc_partial_syn;
147 int16_t *pmecc_si;
148 int16_t *pmecc_smu; /* Sigma table */
149 int16_t *pmecc_lmu; /* polynomal order */
150 int *pmecc_mu;
151 int *pmecc_dmu;
152 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200153};
154
Josh Wu1c7b8742012-06-29 17:47:55 +0800155static struct nand_ecclayout atmel_pmecc_oobinfo;
156
Andrew Victor42cb1402006-10-19 18:24:35 +0200157/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900158 * Enable NAND.
159 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200160static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900161{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800162 if (gpio_is_valid(host->board.enable_pin))
163 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900164}
165
166/*
167 * Disable NAND.
168 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200169static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900170{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800171 if (gpio_is_valid(host->board.enable_pin))
172 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900173}
174
175/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200176 * Hardware specific access to control-lines
177 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200178static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200179{
180 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200181 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200182
Atsushi Nemoto81365082008-04-27 01:51:12 +0900183 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900184 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200185 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900186 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200187 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900188 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200189 if (cmd == NAND_CMD_NONE)
190 return;
191
192 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800193 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200194 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800195 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200196}
197
198/*
199 * Read the Device Ready pin.
200 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200201static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200202{
203 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200204 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200205
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800206 return gpio_get_value(host->board.rdy_pin) ^
207 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200208}
209
Josh Wu7dc37de2013-08-05 19:14:35 +0800210/* Set up for hardware ready pin and enable pin. */
211static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
212{
213 struct nand_chip *chip = mtd->priv;
214 struct atmel_nand_host *host = chip->priv;
215 int res = 0;
216
217 if (gpio_is_valid(host->board.rdy_pin)) {
218 res = devm_gpio_request(host->dev,
219 host->board.rdy_pin, "nand_rdy");
220 if (res < 0) {
221 dev_err(host->dev,
222 "can't request rdy gpio %d\n",
223 host->board.rdy_pin);
224 return res;
225 }
226
227 res = gpio_direction_input(host->board.rdy_pin);
228 if (res < 0) {
229 dev_err(host->dev,
230 "can't request input direction rdy gpio %d\n",
231 host->board.rdy_pin);
232 return res;
233 }
234
235 chip->dev_ready = atmel_nand_device_ready;
236 }
237
238 if (gpio_is_valid(host->board.enable_pin)) {
239 res = devm_gpio_request(host->dev,
240 host->board.enable_pin, "nand_enable");
241 if (res < 0) {
242 dev_err(host->dev,
243 "can't request enable gpio %d\n",
244 host->board.enable_pin);
245 return res;
246 }
247
248 res = gpio_direction_output(host->board.enable_pin, 1);
249 if (res < 0) {
250 dev_err(host->dev,
251 "can't request output direction enable gpio %d\n",
252 host->board.enable_pin);
253 return res;
254 }
255 }
256
257 return res;
258}
259
Josh Wu1ae9c092013-08-05 19:14:36 +0800260static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size)
261{
262 int i;
263 u32 *t = trg;
264 const __iomem u32 *s = src;
265
266 for (i = 0; i < (size >> 2); i++)
267 *t++ = readl_relaxed(s++);
268}
269
Josh Wu6054d4d2013-08-05 19:14:37 +0800270static void memcpy32_toio(void __iomem *trg, const void *src, int size)
271{
272 int i;
273 u32 __iomem *t = trg;
274 const u32 *s = src;
275
276 for (i = 0; i < (size >> 2); i++)
277 writel_relaxed(*s++, t++);
278}
279
Artem Bityutskiy50082312012-02-02 13:54:25 +0200280/*
281 * Minimal-overhead PIO for data access.
282 */
283static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
284{
285 struct nand_chip *nand_chip = mtd->priv;
Josh Wu1ae9c092013-08-05 19:14:36 +0800286 struct atmel_nand_host *host = nand_chip->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200287
Josh Wu1ae9c092013-08-05 19:14:36 +0800288 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
289 memcpy32_fromio(buf, host->nfc->data_in_sram, len);
290 host->nfc->data_in_sram += len;
291 } else {
292 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
293 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200294}
295
296static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
297{
298 struct nand_chip *nand_chip = mtd->priv;
Josh Wu1ae9c092013-08-05 19:14:36 +0800299 struct atmel_nand_host *host = nand_chip->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200300
Josh Wu1ae9c092013-08-05 19:14:36 +0800301 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
302 memcpy32_fromio(buf, host->nfc->data_in_sram, len);
303 host->nfc->data_in_sram += len;
304 } else {
305 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
306 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200307}
308
309static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
310{
311 struct nand_chip *nand_chip = mtd->priv;
312
313 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
314}
315
316static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
317{
318 struct nand_chip *nand_chip = mtd->priv;
319
320 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
321}
322
Hong Xucbc6c5e2011-01-18 14:36:05 +0800323static void dma_complete_func(void *completion)
324{
325 complete(completion);
326}
327
Josh Wu1ae9c092013-08-05 19:14:36 +0800328static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
329{
330 /* NFC only has two banks. Must be 0 or 1 */
331 if (bank > 1)
332 return -EINVAL;
333
334 if (bank) {
335 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
336 if (host->mtd.writesize > 2048)
337 return -EINVAL;
338 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
339 } else {
340 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
341 }
342
343 return 0;
344}
345
346static uint nfc_get_sram_off(struct atmel_nand_host *host)
347{
348 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
349 return NFC_SRAM_BANK1_OFFSET;
350 else
351 return 0;
352}
353
354static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
355{
356 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
357 return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
358 else
359 return host->nfc->sram_bank0_phys;
360}
361
Hong Xucbc6c5e2011-01-18 14:36:05 +0800362static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
363 int is_read)
364{
365 struct dma_device *dma_dev;
366 enum dma_ctrl_flags flags;
367 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
368 struct dma_async_tx_descriptor *tx = NULL;
369 dma_cookie_t cookie;
370 struct nand_chip *chip = mtd->priv;
371 struct atmel_nand_host *host = chip->priv;
372 void *p = buf;
373 int err = -EIO;
374 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Josh Wu1ae9c092013-08-05 19:14:36 +0800375 struct atmel_nfc *nfc = host->nfc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800376
Hong Xu80b4f812011-03-31 18:33:15 +0800377 if (buf >= high_memory)
378 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800379
380 dma_dev = host->dma_chan->device;
381
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200382 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800383
384 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
385 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
386 dev_err(host->dev, "Failed to dma_map_single\n");
387 goto err_buf;
388 }
389
390 if (is_read) {
Josh Wu1ae9c092013-08-05 19:14:36 +0800391 if (nfc && nfc->data_in_sram)
392 dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
393 - (nfc->sram_bank0 + nfc_get_sram_off(host)));
394 else
395 dma_src_addr = host->io_phys;
396
Hong Xucbc6c5e2011-01-18 14:36:05 +0800397 dma_dst_addr = phys_addr;
398 } else {
399 dma_src_addr = phys_addr;
Josh Wu6054d4d2013-08-05 19:14:37 +0800400
401 if (nfc && nfc->write_by_sram)
402 dma_dst_addr = nfc_sram_phys(host);
403 else
404 dma_dst_addr = host->io_phys;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800405 }
406
407 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
408 dma_src_addr, len, flags);
409 if (!tx) {
410 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
411 goto err_dma;
412 }
413
414 init_completion(&host->comp);
415 tx->callback = dma_complete_func;
416 tx->callback_param = &host->comp;
417
418 cookie = tx->tx_submit(tx);
419 if (dma_submit_error(cookie)) {
420 dev_err(host->dev, "Failed to do DMA tx_submit\n");
421 goto err_dma;
422 }
423
424 dma_async_issue_pending(host->dma_chan);
425 wait_for_completion(&host->comp);
426
Josh Wu1ae9c092013-08-05 19:14:36 +0800427 if (is_read && nfc && nfc->data_in_sram)
428 /* After read data from SRAM, need to increase the position */
429 nfc->data_in_sram += len;
430
Hong Xucbc6c5e2011-01-18 14:36:05 +0800431 err = 0;
432
433err_dma:
434 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
435err_buf:
436 if (err != 0)
Nicolas Ferre74414a942014-02-12 12:26:54 +0100437 dev_dbg(host->dev, "Fall back to CPU I/O\n");
Hong Xucbc6c5e2011-01-18 14:36:05 +0800438 return err;
439}
440
441static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
442{
443 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200444 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800445
Nicolas Ferre9d515672011-04-01 16:40:44 +0200446 if (use_dma && len > mtd->oobsize)
447 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800448 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
449 return;
450
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800451 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200452 atmel_read_buf16(mtd, buf, len);
453 else
454 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800455}
456
457static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
458{
459 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200460 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800461
Nicolas Ferre9d515672011-04-01 16:40:44 +0200462 if (use_dma && len > mtd->oobsize)
463 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800464 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
465 return;
466
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800467 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200468 atmel_write_buf16(mtd, buf, len);
469 else
470 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800471}
472
David Brownell23a346c2008-07-03 23:40:16 -0700473/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800474 * Return number of ecc bytes per sector according to sector size and
475 * correction capability
476 *
477 * Following table shows what at91 PMECC supported:
478 * Correction Capability Sector_512_bytes Sector_1024_bytes
479 * ===================== ================ =================
480 * 2-bits 4-bytes 4-bytes
481 * 4-bits 7-bytes 7-bytes
482 * 8-bits 13-bytes 14-bytes
483 * 12-bits 20-bytes 21-bytes
484 * 24-bits 39-bytes 42-bytes
485 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500486static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800487{
488 int m = 12 + sector_size / 512;
489 return (m * cap + 7) / 8;
490}
491
Bill Pemberton06f25512012-11-19 13:23:07 -0500492static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800493 int oobsize, int ecc_len)
Josh Wu1c7b8742012-06-29 17:47:55 +0800494{
495 int i;
496
497 layout->eccbytes = ecc_len;
498
499 /* ECC will occupy the last ecc_len bytes continuously */
500 for (i = 0; i < ecc_len; i++)
501 layout->eccpos[i] = oobsize - ecc_len + i;
502
503 layout->oobfree[0].offset = 2;
504 layout->oobfree[0].length =
505 oobsize - ecc_len - layout->oobfree[0].offset;
506}
507
Bill Pemberton06f25512012-11-19 13:23:07 -0500508static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800509{
510 int table_size;
511
512 table_size = host->pmecc_sector_size == 512 ?
513 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
514
515 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
516 table_size * sizeof(int16_t);
517}
518
Bill Pemberton06f25512012-11-19 13:23:07 -0500519static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800520{
521 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800522 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800523
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800524 size = (2 * cap + 1) * sizeof(int16_t);
525 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
526 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
527 host->pmecc_lmu = devm_kzalloc(host->dev,
528 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
529 host->pmecc_smu = devm_kzalloc(host->dev,
530 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800531
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800532 size = (cap + 1) * sizeof(int);
533 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
534 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
535 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800536
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800537 if (!host->pmecc_partial_syn ||
538 !host->pmecc_si ||
539 !host->pmecc_lmu ||
540 !host->pmecc_smu ||
541 !host->pmecc_mu ||
542 !host->pmecc_dmu ||
543 !host->pmecc_delta)
544 return -ENOMEM;
545
546 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800547}
548
549static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
550{
551 struct nand_chip *nand_chip = mtd->priv;
552 struct atmel_nand_host *host = nand_chip->priv;
553 int i;
554 uint32_t value;
555
556 /* Fill odd syndromes */
557 for (i = 0; i < host->pmecc_corr_cap; i++) {
558 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
559 if (i & 1)
560 value >>= 16;
561 value &= 0xffff;
562 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
563 }
564}
565
566static void pmecc_substitute(struct mtd_info *mtd)
567{
568 struct nand_chip *nand_chip = mtd->priv;
569 struct atmel_nand_host *host = nand_chip->priv;
570 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
571 int16_t __iomem *index_of = host->pmecc_index_of;
572 int16_t *partial_syn = host->pmecc_partial_syn;
573 const int cap = host->pmecc_corr_cap;
574 int16_t *si;
575 int i, j;
576
577 /* si[] is a table that holds the current syndrome value,
578 * an element of that table belongs to the field
579 */
580 si = host->pmecc_si;
581
582 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
583
584 /* Computation 2t syndromes based on S(x) */
585 /* Odd syndromes */
586 for (i = 1; i < 2 * cap; i += 2) {
587 for (j = 0; j < host->pmecc_degree; j++) {
588 if (partial_syn[i] & ((unsigned short)0x1 << j))
589 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
590 }
591 }
592 /* Even syndrome = (Odd syndrome) ** 2 */
593 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
594 if (si[j] == 0) {
595 si[i] = 0;
596 } else {
597 int16_t tmp;
598
599 tmp = readw_relaxed(index_of + si[j]);
600 tmp = (tmp * 2) % host->pmecc_cw_len;
601 si[i] = readw_relaxed(alpha_to + tmp);
602 }
603 }
604
605 return;
606}
607
608static void pmecc_get_sigma(struct mtd_info *mtd)
609{
610 struct nand_chip *nand_chip = mtd->priv;
611 struct atmel_nand_host *host = nand_chip->priv;
612
613 int16_t *lmu = host->pmecc_lmu;
614 int16_t *si = host->pmecc_si;
615 int *mu = host->pmecc_mu;
616 int *dmu = host->pmecc_dmu; /* Discrepancy */
617 int *delta = host->pmecc_delta; /* Delta order */
618 int cw_len = host->pmecc_cw_len;
619 const int16_t cap = host->pmecc_corr_cap;
620 const int num = 2 * cap + 1;
621 int16_t __iomem *index_of = host->pmecc_index_of;
622 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
623 int i, j, k;
624 uint32_t dmu_0_count, tmp;
625 int16_t *smu = host->pmecc_smu;
626
627 /* index of largest delta */
628 int ro;
629 int largest;
630 int diff;
631
632 dmu_0_count = 0;
633
634 /* First Row */
635
636 /* Mu */
637 mu[0] = -1;
638
639 memset(smu, 0, sizeof(int16_t) * num);
640 smu[0] = 1;
641
642 /* discrepancy set to 1 */
643 dmu[0] = 1;
644 /* polynom order set to 0 */
645 lmu[0] = 0;
646 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
647
648 /* Second Row */
649
650 /* Mu */
651 mu[1] = 0;
652 /* Sigma(x) set to 1 */
653 memset(&smu[num], 0, sizeof(int16_t) * num);
654 smu[num] = 1;
655
656 /* discrepancy set to S1 */
657 dmu[1] = si[1];
658
659 /* polynom order set to 0 */
660 lmu[1] = 0;
661
662 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
663
664 /* Init the Sigma(x) last row */
665 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
666
667 for (i = 1; i <= cap; i++) {
668 mu[i + 1] = i << 1;
669 /* Begin Computing Sigma (Mu+1) and L(mu) */
670 /* check if discrepancy is set to 0 */
671 if (dmu[i] == 0) {
672 dmu_0_count++;
673
674 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
675 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
676 tmp += 2;
677 else
678 tmp += 1;
679
680 if (dmu_0_count == tmp) {
681 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
682 smu[(cap + 1) * num + j] =
683 smu[i * num + j];
684
685 lmu[cap + 1] = lmu[i];
686 return;
687 }
688
689 /* copy polynom */
690 for (j = 0; j <= lmu[i] >> 1; j++)
691 smu[(i + 1) * num + j] = smu[i * num + j];
692
693 /* copy previous polynom order to the next */
694 lmu[i + 1] = lmu[i];
695 } else {
696 ro = 0;
697 largest = -1;
698 /* find largest delta with dmu != 0 */
699 for (j = 0; j < i; j++) {
700 if ((dmu[j]) && (delta[j] > largest)) {
701 largest = delta[j];
702 ro = j;
703 }
704 }
705
706 /* compute difference */
707 diff = (mu[i] - mu[ro]);
708
709 /* Compute degree of the new smu polynomial */
710 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
711 lmu[i + 1] = lmu[i];
712 else
713 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
714
715 /* Init smu[i+1] with 0 */
716 for (k = 0; k < num; k++)
717 smu[(i + 1) * num + k] = 0;
718
719 /* Compute smu[i+1] */
720 for (k = 0; k <= lmu[ro] >> 1; k++) {
721 int16_t a, b, c;
722
723 if (!(smu[ro * num + k] && dmu[i]))
724 continue;
725 a = readw_relaxed(index_of + dmu[i]);
726 b = readw_relaxed(index_of + dmu[ro]);
727 c = readw_relaxed(index_of + smu[ro * num + k]);
728 tmp = a + (cw_len - b) + c;
729 a = readw_relaxed(alpha_to + tmp % cw_len);
730 smu[(i + 1) * num + (k + diff)] = a;
731 }
732
733 for (k = 0; k <= lmu[i] >> 1; k++)
734 smu[(i + 1) * num + k] ^= smu[i * num + k];
735 }
736
737 /* End Computing Sigma (Mu+1) and L(mu) */
738 /* In either case compute delta */
739 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
740
741 /* Do not compute discrepancy for the last iteration */
742 if (i >= cap)
743 continue;
744
745 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
746 tmp = 2 * (i - 1);
747 if (k == 0) {
748 dmu[i + 1] = si[tmp + 3];
749 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
750 int16_t a, b, c;
751 a = readw_relaxed(index_of +
752 smu[(i + 1) * num + k]);
753 b = si[2 * (i - 1) + 3 - k];
754 c = readw_relaxed(index_of + b);
755 tmp = a + c;
756 tmp %= cw_len;
757 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
758 dmu[i + 1];
759 }
760 }
761 }
762
763 return;
764}
765
766static int pmecc_err_location(struct mtd_info *mtd)
767{
768 struct nand_chip *nand_chip = mtd->priv;
769 struct atmel_nand_host *host = nand_chip->priv;
770 unsigned long end_time;
771 const int cap = host->pmecc_corr_cap;
772 const int num = 2 * cap + 1;
773 int sector_size = host->pmecc_sector_size;
774 int err_nbr = 0; /* number of error */
775 int roots_nbr; /* number of roots */
776 int i;
777 uint32_t val;
778 int16_t *smu = host->pmecc_smu;
779
780 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
781
782 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
783 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
784 smu[(cap + 1) * num + i]);
785 err_nbr++;
786 }
787
788 val = (err_nbr - 1) << 16;
789 if (sector_size == 1024)
790 val |= 1;
791
792 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
793 pmerrloc_writel(host->pmerrloc_base, ELEN,
794 sector_size * 8 + host->pmecc_degree * cap);
795
796 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
797 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
798 & PMERRLOC_CALC_DONE)) {
799 if (unlikely(time_after(jiffies, end_time))) {
800 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
801 return -1;
802 }
803 cpu_relax();
804 }
805
806 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
807 & PMERRLOC_ERR_NUM_MASK) >> 8;
808 /* Number of roots == degree of smu hence <= cap */
809 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
810 return err_nbr - 1;
811
812 /* Number of roots does not match the degree of smu
813 * unable to correct error */
814 return -1;
815}
816
817static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
818 int sector_num, int extra_bytes, int err_nbr)
819{
820 struct nand_chip *nand_chip = mtd->priv;
821 struct atmel_nand_host *host = nand_chip->priv;
822 int i = 0;
823 int byte_pos, bit_pos, sector_size, pos;
824 uint32_t tmp;
825 uint8_t err_byte;
826
827 sector_size = host->pmecc_sector_size;
828
829 while (err_nbr) {
830 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
831 byte_pos = tmp / 8;
832 bit_pos = tmp % 8;
833
834 if (byte_pos >= (sector_size + extra_bytes))
835 BUG(); /* should never happen */
836
837 if (byte_pos < sector_size) {
838 err_byte = *(buf + byte_pos);
839 *(buf + byte_pos) ^= (1 << bit_pos);
840
841 pos = sector_num * host->pmecc_sector_size + byte_pos;
842 dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
843 pos, bit_pos, err_byte, *(buf + byte_pos));
844 } else {
845 /* Bit flip in OOB area */
Wu, Josh022a4782014-08-08 17:12:35 +0800846 tmp = sector_num * nand_chip->ecc.bytes
Josh Wu1c7b8742012-06-29 17:47:55 +0800847 + (byte_pos - sector_size);
848 err_byte = ecc[tmp];
849 ecc[tmp] ^= (1 << bit_pos);
850
851 pos = tmp + nand_chip->ecc.layout->eccpos[0];
852 dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
853 pos, bit_pos, err_byte, ecc[tmp]);
854 }
855
856 i++;
857 err_nbr--;
858 }
859
860 return;
861}
862
863static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
864 u8 *ecc)
865{
866 struct nand_chip *nand_chip = mtd->priv;
867 struct atmel_nand_host *host = nand_chip->priv;
Bo Shenb3857662014-06-12 15:58:45 +0800868 int i, err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800869 uint8_t *buf_pos;
Josh Wuc0c70d92012-11-27 18:50:31 +0800870 int total_err = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800871
Bo Shenb3857662014-06-12 15:58:45 +0800872 for (i = 0; i < nand_chip->ecc.total; i++)
Josh Wu1c7b8742012-06-29 17:47:55 +0800873 if (ecc[i] != 0xff)
874 goto normal_check;
875 /* Erased page, return OK */
876 return 0;
877
878normal_check:
Wu, Joshc9447ff2014-08-08 17:12:34 +0800879 for (i = 0; i < nand_chip->ecc.steps; i++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800880 err_nbr = 0;
881 if (pmecc_stat & 0x1) {
882 buf_pos = buf + i * host->pmecc_sector_size;
883
884 pmecc_gen_syndrome(mtd, i);
885 pmecc_substitute(mtd);
886 pmecc_get_sigma(mtd);
887
888 err_nbr = pmecc_err_location(mtd);
889 if (err_nbr == -1) {
890 dev_err(host->dev, "PMECC: Too many errors\n");
891 mtd->ecc_stats.failed++;
892 return -EIO;
893 } else {
894 pmecc_correct_data(mtd, buf_pos, ecc, i,
Wu, Josh022a4782014-08-08 17:12:35 +0800895 nand_chip->ecc.bytes, err_nbr);
Josh Wu1c7b8742012-06-29 17:47:55 +0800896 mtd->ecc_stats.corrected += err_nbr;
Josh Wuc0c70d92012-11-27 18:50:31 +0800897 total_err += err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800898 }
899 }
900 pmecc_stat >>= 1;
901 }
902
Josh Wuc0c70d92012-11-27 18:50:31 +0800903 return total_err;
Josh Wu1c7b8742012-06-29 17:47:55 +0800904}
905
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800906static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
907{
908 u32 val;
909
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800910 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
911 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
912 return;
913 }
914
Josh Wu1fad0e82013-08-07 17:58:11 +0800915 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
916 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
917 val = pmecc_readl_relaxed(host->ecc, CFG);
918
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800919 if (ecc_op == NAND_ECC_READ)
920 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
921 | PMECC_CFG_AUTO_ENABLE);
922 else
923 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
924 & ~PMECC_CFG_AUTO_ENABLE);
925
926 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
927 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
928}
929
Josh Wu1c7b8742012-06-29 17:47:55 +0800930static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
931 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
932{
933 struct atmel_nand_host *host = chip->priv;
Bo Shenb3857662014-06-12 15:58:45 +0800934 int eccsize = chip->ecc.size * chip->ecc.steps;
Josh Wu1c7b8742012-06-29 17:47:55 +0800935 uint8_t *oob = chip->oob_poi;
936 uint32_t *eccpos = chip->ecc.layout->eccpos;
937 uint32_t stat;
938 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800939 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800940
Josh Wu1ae9c092013-08-05 19:14:36 +0800941 if (!host->nfc || !host->nfc->use_nfc_sram)
942 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800943
944 chip->read_buf(mtd, buf, eccsize);
945 chip->read_buf(mtd, oob, mtd->oobsize);
946
947 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
948 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
949 if (unlikely(time_after(jiffies, end_time))) {
950 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
951 return -EIO;
952 }
953 cpu_relax();
954 }
955
956 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800957 if (stat != 0) {
958 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
959 if (bitflips < 0)
960 /* uncorrectable errors */
961 return 0;
962 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800963
Josh Wuc0c70d92012-11-27 18:50:31 +0800964 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800965}
966
967static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
968 struct nand_chip *chip, const uint8_t *buf, int oob_required)
969{
970 struct atmel_nand_host *host = chip->priv;
971 uint32_t *eccpos = chip->ecc.layout->eccpos;
972 int i, j;
973 unsigned long end_time;
974
Josh Wu6054d4d2013-08-05 19:14:37 +0800975 if (!host->nfc || !host->nfc->write_by_sram) {
976 pmecc_enable(host, NAND_ECC_WRITE);
977 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
978 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800979
980 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
981 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
982 if (unlikely(time_after(jiffies, end_time))) {
983 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
984 return -EIO;
985 }
986 cpu_relax();
987 }
988
Wu, Joshc9447ff2014-08-08 17:12:34 +0800989 for (i = 0; i < chip->ecc.steps; i++) {
Wu, Josh022a4782014-08-08 17:12:35 +0800990 for (j = 0; j < chip->ecc.bytes; j++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800991 int pos;
992
Wu, Josh022a4782014-08-08 17:12:35 +0800993 pos = i * chip->ecc.bytes + j;
Josh Wu1c7b8742012-06-29 17:47:55 +0800994 chip->oob_poi[eccpos[pos]] =
995 pmecc_readb_ecc_relaxed(host->ecc, i, j);
996 }
997 }
998 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
999
1000 return 0;
1001}
1002
1003static void atmel_pmecc_core_init(struct mtd_info *mtd)
1004{
1005 struct nand_chip *nand_chip = mtd->priv;
1006 struct atmel_nand_host *host = nand_chip->priv;
1007 uint32_t val = 0;
1008 struct nand_ecclayout *ecc_layout;
1009
1010 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
1011 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1012
1013 switch (host->pmecc_corr_cap) {
1014 case 2:
1015 val = PMECC_CFG_BCH_ERR2;
1016 break;
1017 case 4:
1018 val = PMECC_CFG_BCH_ERR4;
1019 break;
1020 case 8:
1021 val = PMECC_CFG_BCH_ERR8;
1022 break;
1023 case 12:
1024 val = PMECC_CFG_BCH_ERR12;
1025 break;
1026 case 24:
1027 val = PMECC_CFG_BCH_ERR24;
1028 break;
1029 }
1030
1031 if (host->pmecc_sector_size == 512)
1032 val |= PMECC_CFG_SECTOR512;
1033 else if (host->pmecc_sector_size == 1024)
1034 val |= PMECC_CFG_SECTOR1024;
1035
Wu, Joshc9447ff2014-08-08 17:12:34 +08001036 switch (nand_chip->ecc.steps) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001037 case 1:
1038 val |= PMECC_CFG_PAGE_1SECTOR;
1039 break;
1040 case 2:
1041 val |= PMECC_CFG_PAGE_2SECTORS;
1042 break;
1043 case 4:
1044 val |= PMECC_CFG_PAGE_4SECTORS;
1045 break;
1046 case 8:
1047 val |= PMECC_CFG_PAGE_8SECTORS;
1048 break;
1049 }
1050
1051 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
1052 | PMECC_CFG_AUTO_DISABLE);
1053 pmecc_writel(host->ecc, CFG, val);
1054
1055 ecc_layout = nand_chip->ecc.layout;
1056 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
1057 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
1058 pmecc_writel(host->ecc, EADDR,
1059 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
1060 /* See datasheet about PMECC Clock Control Register */
1061 pmecc_writel(host->ecc, CLK, 2);
1062 pmecc_writel(host->ecc, IDR, 0xff);
1063 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
1064}
1065
Josh Wu84cfbbb2013-01-23 20:47:12 +08001066/*
Josh Wu2a3d9332013-09-18 13:58:48 +08001067 * Get minimum ecc requirements from NAND.
Josh Wu84cfbbb2013-01-23 20:47:12 +08001068 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
Josh Wu2a3d9332013-09-18 13:58:48 +08001069 * will set them according to minimum ecc requirement. Otherwise, use the
Josh Wu84cfbbb2013-01-23 20:47:12 +08001070 * value in DTS file.
1071 * return 0 if success. otherwise return error code.
1072 */
1073static int pmecc_choose_ecc(struct atmel_nand_host *host,
1074 int *cap, int *sector_size)
1075{
Josh Wu2a3d9332013-09-18 13:58:48 +08001076 /* Get minimum ECC requirements */
1077 if (host->nand_chip.ecc_strength_ds) {
1078 *cap = host->nand_chip.ecc_strength_ds;
1079 *sector_size = host->nand_chip.ecc_step_ds;
1080 dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n",
Josh Wu84cfbbb2013-01-23 20:47:12 +08001081 *cap, *sector_size);
Josh Wu84cfbbb2013-01-23 20:47:12 +08001082 } else {
Josh Wu84cfbbb2013-01-23 20:47:12 +08001083 *cap = 2;
1084 *sector_size = 512;
Josh Wu2a3d9332013-09-18 13:58:48 +08001085 dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
Josh Wu84cfbbb2013-01-23 20:47:12 +08001086 }
1087
Josh Wu2a3d9332013-09-18 13:58:48 +08001088 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
Josh Wu84cfbbb2013-01-23 20:47:12 +08001089 if (host->pmecc_corr_cap == 0) {
1090 /* use the most fitable ecc bits (the near bigger one ) */
1091 if (*cap <= 2)
1092 host->pmecc_corr_cap = 2;
1093 else if (*cap <= 4)
1094 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +08001095 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001096 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +08001097 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001098 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +08001099 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001100 host->pmecc_corr_cap = 24;
1101 else
1102 return -EINVAL;
1103 }
1104 if (host->pmecc_sector_size == 0) {
1105 /* use the most fitable sector size (the near smaller one ) */
1106 if (*sector_size >= 1024)
1107 host->pmecc_sector_size = 1024;
1108 else if (*sector_size >= 512)
1109 host->pmecc_sector_size = 512;
1110 else
1111 return -EINVAL;
1112 }
1113 return 0;
1114}
1115
Josh Wuabb1cd02014-10-11 18:01:50 +08001116static inline int deg(unsigned int poly)
1117{
1118 /* polynomial degree is the most-significant bit index */
1119 return fls(poly) - 1;
1120}
1121
1122static int build_gf_tables(int mm, unsigned int poly,
1123 int16_t *index_of, int16_t *alpha_to)
1124{
1125 unsigned int i, x = 1;
1126 const unsigned int k = 1 << deg(poly);
1127 unsigned int nn = (1 << mm) - 1;
1128
1129 /* primitive polynomial must be of degree m */
1130 if (k != (1u << mm))
1131 return -EINVAL;
1132
1133 for (i = 0; i < nn; i++) {
1134 alpha_to[i] = x;
1135 index_of[x] = i;
1136 if (i && (x == 1))
1137 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
1138 return -EINVAL;
1139 x <<= 1;
1140 if (x & k)
1141 x ^= poly;
1142 }
1143 alpha_to[nn] = 1;
1144 index_of[0] = 0;
1145
1146 return 0;
1147}
1148
1149static uint16_t *create_lookup_table(struct device *dev, int sector_size)
1150{
1151 int degree = (sector_size == 512) ?
1152 PMECC_GF_DIMENSION_13 :
1153 PMECC_GF_DIMENSION_14;
1154 unsigned int poly = (sector_size == 512) ?
1155 PMECC_GF_13_PRIMITIVE_POLY :
1156 PMECC_GF_14_PRIMITIVE_POLY;
1157 int table_size = (sector_size == 512) ?
1158 PMECC_LOOKUP_TABLE_SIZE_512 :
1159 PMECC_LOOKUP_TABLE_SIZE_1024;
1160
1161 int16_t *addr = devm_kzalloc(dev, 2 * table_size * sizeof(uint16_t),
1162 GFP_KERNEL);
1163 if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
1164 return NULL;
1165
1166 return addr;
1167}
1168
Johan Hovold2c2b9282013-09-23 16:27:28 +02001169static int atmel_pmecc_nand_init_params(struct platform_device *pdev,
Josh Wu1c7b8742012-06-29 17:47:55 +08001170 struct atmel_nand_host *host)
1171{
1172 struct mtd_info *mtd = &host->mtd;
1173 struct nand_chip *nand_chip = &host->nand_chip;
1174 struct resource *regs, *regs_pmerr, *regs_rom;
Josh Wuabb1cd02014-10-11 18:01:50 +08001175 uint16_t *galois_table;
Josh Wu1c7b8742012-06-29 17:47:55 +08001176 int cap, sector_size, err_no;
1177
Josh Wu84cfbbb2013-01-23 20:47:12 +08001178 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
1179 if (err_no) {
1180 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1181 return err_no;
1182 }
1183
Richard Genoudf666d642013-07-30 17:17:29 +02001184 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001185 sector_size != host->pmecc_sector_size)
1186 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001187
Josh Wu1c7b8742012-06-29 17:47:55 +08001188 cap = host->pmecc_corr_cap;
1189 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001190 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1191 host->pmecc_lookup_table_offset_512 :
1192 host->pmecc_lookup_table_offset_1024;
1193
Josh Wu1c7b8742012-06-29 17:47:55 +08001194 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1195 cap, sector_size);
1196
1197 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1198 if (!regs) {
1199 dev_warn(host->dev,
1200 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1201 nand_chip->ecc.mode = NAND_ECC_SOFT;
1202 return 0;
1203 }
1204
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001205 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1206 if (IS_ERR(host->ecc)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001207 err_no = PTR_ERR(host->ecc);
1208 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001209 }
1210
1211 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001212 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1213 if (IS_ERR(host->pmerrloc_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001214 err_no = PTR_ERR(host->pmerrloc_base);
1215 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001216 }
1217
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001218 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1219 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, regs_rom);
1220 if (IS_ERR(host->pmecc_rom_base)) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001221 if (!host->has_no_lookup_table)
1222 /* Don't display the information again */
1223 dev_err(host->dev, "Can not get I/O resource for ROM, will build a lookup table in runtime!\n");
1224
1225 host->has_no_lookup_table = true;
1226 }
1227
1228 if (host->has_no_lookup_table) {
1229 /* Build the look-up table in runtime */
1230 galois_table = create_lookup_table(host->dev, sector_size);
1231 if (!galois_table) {
1232 dev_err(host->dev, "Failed to build a lookup table in runtime!\n");
1233 err_no = -EINVAL;
1234 goto err;
1235 }
1236
1237 host->pmecc_rom_base = (void __iomem *)galois_table;
1238 host->pmecc_lookup_table_offset = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +08001239 }
1240
Bo Shenb3857662014-06-12 15:58:45 +08001241 nand_chip->ecc.size = sector_size;
Josh Wu1c7b8742012-06-29 17:47:55 +08001242
1243 /* set ECC page size and oob layout */
1244 switch (mtd->writesize) {
Wu, Josha3557102014-07-22 17:24:18 +08001245 case 512:
1246 case 1024:
Josh Wu1c7b8742012-06-29 17:47:55 +08001247 case 2048:
Wu, Josha3557102014-07-22 17:24:18 +08001248 case 4096:
1249 case 8192:
1250 if (sector_size > mtd->writesize) {
1251 dev_err(host->dev, "pmecc sector size is bigger than the page size!\n");
1252 err_no = -EINVAL;
1253 goto err;
1254 }
1255
Josh Wu2fa831f2013-08-19 18:05:44 +08001256 host->pmecc_degree = (sector_size == 512) ?
1257 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Josh Wu1c7b8742012-06-29 17:47:55 +08001258 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +08001259 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1260 host->pmecc_index_of = host->pmecc_rom_base +
1261 host->pmecc_lookup_table_offset;
1262
Josh Wu1c7b8742012-06-29 17:47:55 +08001263 nand_chip->ecc.strength = cap;
Wu, Josh022a4782014-08-08 17:12:35 +08001264 nand_chip->ecc.bytes = pmecc_get_ecc_bytes(cap, sector_size);
Wu, Joshc9447ff2014-08-08 17:12:34 +08001265 nand_chip->ecc.steps = mtd->writesize / sector_size;
1266 nand_chip->ecc.total = nand_chip->ecc.bytes *
1267 nand_chip->ecc.steps;
Bo Shenb3857662014-06-12 15:58:45 +08001268 if (nand_chip->ecc.total > mtd->oobsize - 2) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001269 dev_err(host->dev, "No room for ECC bytes\n");
1270 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001271 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001272 }
1273 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1274 mtd->oobsize,
Bo Shenb3857662014-06-12 15:58:45 +08001275 nand_chip->ecc.total);
1276
Josh Wu1c7b8742012-06-29 17:47:55 +08001277 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1278 break;
Wu, Josha3557102014-07-22 17:24:18 +08001279 default:
Josh Wu1c7b8742012-06-29 17:47:55 +08001280 dev_warn(host->dev,
1281 "Unsupported page size for PMECC, use Software ECC\n");
Josh Wu1c7b8742012-06-29 17:47:55 +08001282 /* page size not handled by HW ECC */
1283 /* switching back to soft ECC */
1284 nand_chip->ecc.mode = NAND_ECC_SOFT;
1285 return 0;
1286 }
1287
1288 /* Allocate data for PMECC computation */
1289 err_no = pmecc_data_alloc(host);
1290 if (err_no) {
1291 dev_err(host->dev,
1292 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001293 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001294 }
1295
Herve Codina90445ff2014-03-03 12:15:29 +01001296 nand_chip->options |= NAND_NO_SUBPAGE_WRITE;
Josh Wu1c7b8742012-06-29 17:47:55 +08001297 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1298 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1299
1300 atmel_pmecc_core_init(mtd);
1301
1302 return 0;
1303
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001304err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001305 return err_no;
1306}
1307
1308/*
Richard Genoud77f54922008-04-23 19:51:14 +02001309 * Calculate HW ECC
1310 *
1311 * function called after a write
1312 *
1313 * mtd: MTD block structure
1314 * dat: raw data (unused)
1315 * ecc_code: buffer for ECC
1316 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001317static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001318 const u_char *dat, unsigned char *ecc_code)
1319{
1320 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001321 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001322 unsigned int ecc_value;
1323
1324 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001325 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001326
Richard Genoud3fc23892008-10-12 08:42:28 +02001327 ecc_code[0] = ecc_value & 0xFF;
1328 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001329
1330 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001331 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001332
Richard Genoud3fc23892008-10-12 08:42:28 +02001333 ecc_code[2] = ecc_value & 0xFF;
1334 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001335
1336 return 0;
1337}
1338
1339/*
1340 * HW ECC read page function
1341 *
1342 * mtd: mtd info structure
1343 * chip: nand chip info structure
1344 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001345 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001346 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001347static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1348 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001349{
1350 int eccsize = chip->ecc.size;
1351 int eccbytes = chip->ecc.bytes;
1352 uint32_t *eccpos = chip->ecc.layout->eccpos;
1353 uint8_t *p = buf;
1354 uint8_t *oob = chip->oob_poi;
1355 uint8_t *ecc_pos;
1356 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001357 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001358
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001359 /*
1360 * Errata: ALE is incorrectly wired up to the ECC controller
1361 * on the AP7000, so it will include the address cycles in the
1362 * ECC calculation.
1363 *
1364 * Workaround: Reset the parity registers before reading the
1365 * actual data.
1366 */
Josh Wu71b94e22013-05-09 15:34:54 +08001367 struct atmel_nand_host *host = chip->priv;
1368 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001369 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001370
Richard Genoud77f54922008-04-23 19:51:14 +02001371 /* read the page */
1372 chip->read_buf(mtd, p, eccsize);
1373
1374 /* move to ECC position if needed */
1375 if (eccpos[0] != 0) {
1376 /* This only works on large pages
1377 * because the ECC controller waits for
1378 * NAND_CMD_RNDOUTSTART after the
1379 * NAND_CMD_RNDOUT.
1380 * anyway, for small pages, the eccpos[0] == 0
1381 */
1382 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1383 mtd->writesize + eccpos[0], -1);
1384 }
1385
1386 /* the ECC controller needs to read the ECC just after the data */
1387 ecc_pos = oob + eccpos[0];
1388 chip->read_buf(mtd, ecc_pos, eccbytes);
1389
1390 /* check if there's an error */
1391 stat = chip->ecc.correct(mtd, p, oob, NULL);
1392
Mike Dunn3f91e942012-04-25 12:06:09 -07001393 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001394 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001395 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001396 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001397 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1398 }
Richard Genoud77f54922008-04-23 19:51:14 +02001399
1400 /* get back to oob start (end of page) */
1401 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1402
1403 /* read the oob */
1404 chip->read_buf(mtd, oob, mtd->oobsize);
1405
Mike Dunn3f91e942012-04-25 12:06:09 -07001406 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001407}
1408
1409/*
1410 * HW ECC Correction
1411 *
1412 * function called after a read
1413 *
1414 * mtd: MTD block structure
1415 * dat: raw data read from the chip
1416 * read_ecc: ECC from the chip (unused)
1417 * isnull: unused
1418 *
1419 * Detect and correct a 1 bit error for a page
1420 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001421static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001422 u_char *read_ecc, u_char *isnull)
1423{
1424 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001425 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001426 unsigned int ecc_status;
1427 unsigned int ecc_word, ecc_bit;
1428
1429 /* get the status from the Status Register */
1430 ecc_status = ecc_readl(host->ecc, SR);
1431
1432 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001433 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001434 return 0;
1435
1436 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001437 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001438 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001439 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001440 ecc_word >>= 4;
1441
1442 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001443 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001444 /* check if it is a freshly erased block
1445 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001446 if ((ecc_bit == ATMEL_ECC_BITADDR)
1447 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001448 /* the block has just been erased, return OK */
1449 return 0;
1450 }
1451 /* it doesn't seems to be a freshly
1452 * erased block.
1453 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001454 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001455 " Unable to correct.\n");
1456 return -EIO;
1457 }
1458
1459 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001460 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001461 /* there's nothing much to do here.
1462 * the bit error is on the ECC itself.
1463 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001464 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001465 " Nothing to correct\n");
1466 return 0;
1467 }
1468
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001469 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001470 " (word offset in the page :"
1471 " 0x%x bit offset : 0x%x)\n",
1472 ecc_word, ecc_bit);
1473 /* correct the error */
1474 if (nand_chip->options & NAND_BUSWIDTH_16) {
1475 /* 16 bits words */
1476 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1477 } else {
1478 /* 8 bits words */
1479 dat[ecc_word] ^= (1 << ecc_bit);
1480 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001481 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001482 return 1;
1483}
1484
1485/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001486 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001487 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001488static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1489{
Josh Wu71b94e22013-05-09 15:34:54 +08001490 struct nand_chip *nand_chip = mtd->priv;
1491 struct atmel_nand_host *host = nand_chip->priv;
1492
1493 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001494 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001495}
Richard Genoud77f54922008-04-23 19:51:14 +02001496
Bill Pemberton06f25512012-11-19 13:23:07 -05001497static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001498 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001499{
Josh Wuc0cf7872013-01-23 20:47:08 +08001500 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001501 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001502 int ecc_mode;
1503 struct atmel_nand_data *board = &host->board;
Josh Wue9d8da82013-09-18 11:31:19 +08001504 enum of_gpio_flags flags = 0;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001505
1506 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1507 if (val >= 32) {
1508 dev_err(host->dev, "invalid addr-offset %u\n", val);
1509 return -EINVAL;
1510 }
1511 board->ale = val;
1512 }
1513
1514 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1515 if (val >= 32) {
1516 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1517 return -EINVAL;
1518 }
1519 board->cle = val;
1520 }
1521
1522 ecc_mode = of_get_nand_ecc_mode(np);
1523
1524 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1525
1526 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1527
Josh Wu1b719262013-05-09 15:34:55 +08001528 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1529
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001530 if (of_get_nand_bus_width(np) == 16)
1531 board->bus_width_16 = 1;
1532
1533 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1534 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1535
1536 board->enable_pin = of_get_gpio(np, 1);
1537 board->det_pin = of_get_gpio(np, 2);
1538
Josh Wua41b51a2012-06-29 17:47:54 +08001539 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1540
Josh Wu7dc37de2013-08-05 19:14:35 +08001541 /* load the nfc driver if there is */
1542 of_platform_populate(np, NULL, NULL, host->dev);
1543
Josh Wua41b51a2012-06-29 17:47:54 +08001544 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1545 return 0; /* Not using PMECC */
1546
1547 /* use PMECC, get correction capability, sector size and lookup
1548 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001549 * If correction bits and sector size are not specified, then find
1550 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001551 */
Josh Wue66b4312013-01-23 20:47:11 +08001552 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1553 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1554 (val != 24)) {
1555 dev_err(host->dev,
1556 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1557 val);
1558 return -EINVAL;
1559 }
1560 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001561 }
Josh Wua41b51a2012-06-29 17:47:54 +08001562
Josh Wue66b4312013-01-23 20:47:11 +08001563 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1564 if ((val != 512) && (val != 1024)) {
1565 dev_err(host->dev,
1566 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1567 val);
1568 return -EINVAL;
1569 }
1570 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001571 }
Josh Wua41b51a2012-06-29 17:47:54 +08001572
1573 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1574 offset, 2) != 0) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001575 dev_err(host->dev, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n");
1576 host->has_no_lookup_table = true;
1577 /* Will build a lookup table and initialize the offset later */
1578 return 0;
Josh Wua41b51a2012-06-29 17:47:54 +08001579 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001580 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001581 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1582 return -EINVAL;
1583 }
Josh Wue66b4312013-01-23 20:47:11 +08001584 host->pmecc_lookup_table_offset_512 = offset[0];
1585 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001586
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001587 return 0;
1588}
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001589
Johan Hovold2c2b9282013-09-23 16:27:28 +02001590static int atmel_hw_nand_init_params(struct platform_device *pdev,
Josh Wu3dfe41a2012-06-25 18:07:43 +08001591 struct atmel_nand_host *host)
1592{
1593 struct mtd_info *mtd = &host->mtd;
1594 struct nand_chip *nand_chip = &host->nand_chip;
1595 struct resource *regs;
1596
1597 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1598 if (!regs) {
1599 dev_err(host->dev,
1600 "Can't get I/O resource regs, use software ECC\n");
1601 nand_chip->ecc.mode = NAND_ECC_SOFT;
1602 return 0;
1603 }
1604
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001605 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun8fb7b932014-07-28 21:19:55 +08001606 if (IS_ERR(host->ecc))
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001607 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001608
1609 /* ECC is calculated for the whole page (1 step) */
1610 nand_chip->ecc.size = mtd->writesize;
1611
1612 /* set ECC page size and oob layout */
1613 switch (mtd->writesize) {
1614 case 512:
1615 nand_chip->ecc.layout = &atmel_oobinfo_small;
1616 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1617 break;
1618 case 1024:
1619 nand_chip->ecc.layout = &atmel_oobinfo_large;
1620 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1621 break;
1622 case 2048:
1623 nand_chip->ecc.layout = &atmel_oobinfo_large;
1624 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1625 break;
1626 case 4096:
1627 nand_chip->ecc.layout = &atmel_oobinfo_large;
1628 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1629 break;
1630 default:
1631 /* page size not handled by HW ECC */
1632 /* switching back to soft ECC */
1633 nand_chip->ecc.mode = NAND_ECC_SOFT;
1634 return 0;
1635 }
1636
1637 /* set up for HW ECC */
1638 nand_chip->ecc.calculate = atmel_nand_calculate;
1639 nand_chip->ecc.correct = atmel_nand_correct;
1640 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1641 nand_chip->ecc.read_page = atmel_nand_read_page;
1642 nand_chip->ecc.bytes = 4;
1643 nand_chip->ecc.strength = 1;
1644
1645 return 0;
1646}
1647
Wu, Josh50e04e22014-06-10 17:50:09 +08001648static inline u32 nfc_read_status(struct atmel_nand_host *host)
1649{
1650 u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
1651 u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
1652
1653 if (unlikely(nfc_status & err_flags)) {
1654 if (nfc_status & NFC_SR_DTOE)
1655 dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
1656 else if (nfc_status & NFC_SR_UNDEF)
1657 dev_err(host->dev, "NFC: Access Undefined Area Error\n");
1658 else if (nfc_status & NFC_SR_AWB)
1659 dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
1660 else if (nfc_status & NFC_SR_ASE)
1661 dev_err(host->dev, "NFC: Access memory Size Error\n");
1662 }
1663
1664 return nfc_status;
1665}
1666
Josh Wu7dc37de2013-08-05 19:14:35 +08001667/* SMC interrupt service routine */
1668static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
1669{
1670 struct atmel_nand_host *host = dev_id;
1671 u32 status, mask, pending;
Josh Wue4e06932014-06-10 17:50:11 +08001672 irqreturn_t ret = IRQ_NONE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001673
Wu, Josh50e04e22014-06-10 17:50:09 +08001674 status = nfc_read_status(host);
Josh Wu7dc37de2013-08-05 19:14:35 +08001675 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1676 pending = status & mask;
1677
1678 if (pending & NFC_SR_XFR_DONE) {
Josh Wue4e06932014-06-10 17:50:11 +08001679 complete(&host->nfc->comp_xfer_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001680 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001681 ret = IRQ_HANDLED;
1682 }
1683 if (pending & NFC_SR_RB_EDGE) {
1684 complete(&host->nfc->comp_ready);
Josh Wu7dc37de2013-08-05 19:14:35 +08001685 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_RB_EDGE);
Josh Wue4e06932014-06-10 17:50:11 +08001686 ret = IRQ_HANDLED;
1687 }
1688 if (pending & NFC_SR_CMD_DONE) {
1689 complete(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001690 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001691 ret = IRQ_HANDLED;
Josh Wu7dc37de2013-08-05 19:14:35 +08001692 }
1693
1694 return ret;
1695}
1696
1697/* NFC(Nand Flash Controller) related functions */
Josh Wue4e06932014-06-10 17:50:11 +08001698static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag)
Josh Wu7dc37de2013-08-05 19:14:35 +08001699{
Josh Wue4e06932014-06-10 17:50:11 +08001700 if (flag & NFC_SR_XFR_DONE)
1701 init_completion(&host->nfc->comp_xfer_done);
1702
1703 if (flag & NFC_SR_RB_EDGE)
1704 init_completion(&host->nfc->comp_ready);
1705
1706 if (flag & NFC_SR_CMD_DONE)
1707 init_completion(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001708
1709 /* Enable interrupt that need to wait for */
1710 nfc_writel(host->nfc->hsmc_regs, IER, flag);
Josh Wue4e06932014-06-10 17:50:11 +08001711}
Josh Wu7dc37de2013-08-05 19:14:35 +08001712
Josh Wue4e06932014-06-10 17:50:11 +08001713static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
1714{
1715 int i, index = 0;
1716 struct completion *comp[3]; /* Support 3 interrupt completion */
Josh Wu7dc37de2013-08-05 19:14:35 +08001717
Josh Wue4e06932014-06-10 17:50:11 +08001718 if (flag & NFC_SR_XFR_DONE)
1719 comp[index++] = &host->nfc->comp_xfer_done;
1720
1721 if (flag & NFC_SR_RB_EDGE)
1722 comp[index++] = &host->nfc->comp_ready;
1723
1724 if (flag & NFC_SR_CMD_DONE)
1725 comp[index++] = &host->nfc->comp_cmd_done;
1726
1727 if (index == 0) {
1728 dev_err(host->dev, "Unkown interrupt flag: 0x%08x\n", flag);
1729 return -EINVAL;
1730 }
1731
1732 for (i = 0; i < index; i++) {
1733 if (wait_for_completion_timeout(comp[i],
1734 msecs_to_jiffies(NFC_TIME_OUT_MS)))
1735 continue; /* wait for next completion */
1736 else
1737 goto err_timeout;
1738 }
1739
1740 return 0;
1741
1742err_timeout:
Josh Wu7dc37de2013-08-05 19:14:35 +08001743 dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
Josh Wue4e06932014-06-10 17:50:11 +08001744 /* Disable the interrupt as it is not handled by interrupt handler */
1745 nfc_writel(host->nfc->hsmc_regs, IDR, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001746 return -ETIMEDOUT;
1747}
1748
1749static int nfc_send_command(struct atmel_nand_host *host,
1750 unsigned int cmd, unsigned int addr, unsigned char cycle0)
1751{
1752 unsigned long timeout;
Josh Wue4e06932014-06-10 17:50:11 +08001753 u32 flag = NFC_SR_CMD_DONE;
1754 flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0;
1755
Josh Wu7dc37de2013-08-05 19:14:35 +08001756 dev_dbg(host->dev,
1757 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1758 cmd, addr, cycle0);
1759
1760 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1761 while (nfc_cmd_readl(NFCADDR_CMD_NFCBUSY, host->nfc->base_cmd_regs)
1762 & NFCADDR_CMD_NFCBUSY) {
1763 if (time_after(jiffies, timeout)) {
1764 dev_err(host->dev,
1765 "Time out to wait CMD_NFCBUSY ready!\n");
1766 return -ETIMEDOUT;
1767 }
1768 }
Josh Wue4e06932014-06-10 17:50:11 +08001769
1770 nfc_prepare_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001771 nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
1772 nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
Josh Wue4e06932014-06-10 17:50:11 +08001773 return nfc_wait_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001774}
1775
1776static int nfc_device_ready(struct mtd_info *mtd)
1777{
Wu, Josh72a78e32014-06-10 17:50:10 +08001778 u32 status, mask;
Josh Wu7dc37de2013-08-05 19:14:35 +08001779 struct nand_chip *nand_chip = mtd->priv;
1780 struct atmel_nand_host *host = nand_chip->priv;
Wu, Josh72a78e32014-06-10 17:50:10 +08001781
1782 status = nfc_read_status(host);
1783 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1784
1785 /* The mask should be 0. If not we may lost interrupts */
1786 if (unlikely(mask & status))
1787 dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n",
1788 mask & status);
1789
1790 return status & NFC_SR_RB_EDGE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001791}
1792
1793static void nfc_select_chip(struct mtd_info *mtd, int chip)
1794{
1795 struct nand_chip *nand_chip = mtd->priv;
1796 struct atmel_nand_host *host = nand_chip->priv;
1797
1798 if (chip == -1)
1799 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
1800 else
1801 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
1802}
1803
Brian Norris3dad2342014-01-29 14:08:12 -08001804static int nfc_make_addr(struct mtd_info *mtd, int command, int column,
1805 int page_addr, unsigned int *addr1234, unsigned int *cycle0)
Josh Wu7dc37de2013-08-05 19:14:35 +08001806{
1807 struct nand_chip *chip = mtd->priv;
1808
1809 int acycle = 0;
1810 unsigned char addr_bytes[8];
1811 int index = 0, bit_shift;
1812
1813 BUG_ON(addr1234 == NULL || cycle0 == NULL);
1814
1815 *cycle0 = 0;
1816 *addr1234 = 0;
1817
1818 if (column != -1) {
Brian Norris3dad2342014-01-29 14:08:12 -08001819 if (chip->options & NAND_BUSWIDTH_16 &&
1820 !nand_opcode_8bits(command))
Josh Wu7dc37de2013-08-05 19:14:35 +08001821 column >>= 1;
1822 addr_bytes[acycle++] = column & 0xff;
1823 if (mtd->writesize > 512)
1824 addr_bytes[acycle++] = (column >> 8) & 0xff;
1825 }
1826
1827 if (page_addr != -1) {
1828 addr_bytes[acycle++] = page_addr & 0xff;
1829 addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
1830 if (chip->chipsize > (128 << 20))
1831 addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
1832 }
1833
1834 if (acycle > 4)
1835 *cycle0 = addr_bytes[index++];
1836
1837 for (bit_shift = 0; index < acycle; bit_shift += 8)
1838 *addr1234 += addr_bytes[index++] << bit_shift;
1839
1840 /* return acycle in cmd register */
1841 return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
1842}
1843
1844static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
1845 int column, int page_addr)
1846{
1847 struct nand_chip *chip = mtd->priv;
1848 struct atmel_nand_host *host = chip->priv;
1849 unsigned long timeout;
1850 unsigned int nfc_addr_cmd = 0;
1851
1852 unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1853
1854 /* Set default settings: no cmd2, no addr cycle. read from nand */
1855 unsigned int cmd2 = 0;
1856 unsigned int vcmd2 = 0;
1857 int acycle = NFCADDR_CMD_ACYCLE_NONE;
1858 int csid = NFCADDR_CMD_CSID_3;
1859 int dataen = NFCADDR_CMD_DATADIS;
1860 int nfcwr = NFCADDR_CMD_NFCRD;
1861 unsigned int addr1234 = 0;
1862 unsigned int cycle0 = 0;
1863 bool do_addr = true;
Josh Wu1ae9c092013-08-05 19:14:36 +08001864 host->nfc->data_in_sram = NULL;
Josh Wu7dc37de2013-08-05 19:14:35 +08001865
1866 dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1867 __func__, command, column, page_addr);
1868
1869 switch (command) {
1870 case NAND_CMD_RESET:
1871 nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
1872 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1873 udelay(chip->chip_delay);
1874
1875 nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
1876 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1877 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
1878 if (time_after(jiffies, timeout)) {
1879 dev_err(host->dev,
1880 "Time out to wait status ready!\n");
1881 break;
1882 }
1883 }
1884 return;
1885 case NAND_CMD_STATUS:
1886 do_addr = false;
1887 break;
1888 case NAND_CMD_PARAM:
1889 case NAND_CMD_READID:
1890 do_addr = false;
1891 acycle = NFCADDR_CMD_ACYCLE_1;
1892 if (column != -1)
1893 addr1234 = column;
1894 break;
1895 case NAND_CMD_RNDOUT:
1896 cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
1897 vcmd2 = NFCADDR_CMD_VCMD2;
1898 break;
1899 case NAND_CMD_READ0:
1900 case NAND_CMD_READOOB:
1901 if (command == NAND_CMD_READOOB) {
1902 column += mtd->writesize;
1903 command = NAND_CMD_READ0; /* only READ0 is valid */
1904 cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1905 }
Josh Wu1ae9c092013-08-05 19:14:36 +08001906 if (host->nfc->use_nfc_sram) {
1907 /* Enable Data transfer to sram */
1908 dataen = NFCADDR_CMD_DATAEN;
1909
1910 /* Need enable PMECC now, since NFC will transfer
1911 * data in bus after sending nfc read command.
1912 */
1913 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1914 pmecc_enable(host, NAND_ECC_READ);
1915 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001916
1917 cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
1918 vcmd2 = NFCADDR_CMD_VCMD2;
1919 break;
1920 /* For prgramming command, the cmd need set to write enable */
1921 case NAND_CMD_PAGEPROG:
1922 case NAND_CMD_SEQIN:
1923 case NAND_CMD_RNDIN:
1924 nfcwr = NFCADDR_CMD_NFCWR;
Josh Wu6054d4d2013-08-05 19:14:37 +08001925 if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
1926 dataen = NFCADDR_CMD_DATAEN;
Josh Wu7dc37de2013-08-05 19:14:35 +08001927 break;
1928 default:
1929 break;
1930 }
1931
1932 if (do_addr)
Brian Norris3dad2342014-01-29 14:08:12 -08001933 acycle = nfc_make_addr(mtd, command, column, page_addr,
1934 &addr1234, &cycle0);
Josh Wu7dc37de2013-08-05 19:14:35 +08001935
1936 nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
1937 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1938
1939 /*
1940 * Program and erase have their own busy handlers status, sequential
1941 * in, and deplete1 need no delay.
1942 */
1943 switch (command) {
1944 case NAND_CMD_CACHEDPROG:
1945 case NAND_CMD_PAGEPROG:
1946 case NAND_CMD_ERASE1:
1947 case NAND_CMD_ERASE2:
1948 case NAND_CMD_RNDIN:
1949 case NAND_CMD_STATUS:
1950 case NAND_CMD_RNDOUT:
1951 case NAND_CMD_SEQIN:
1952 case NAND_CMD_READID:
1953 return;
1954
1955 case NAND_CMD_READ0:
Josh Wu1ae9c092013-08-05 19:14:36 +08001956 if (dataen == NFCADDR_CMD_DATAEN) {
1957 host->nfc->data_in_sram = host->nfc->sram_bank0 +
1958 nfc_get_sram_off(host);
1959 return;
1960 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001961 /* fall through */
1962 default:
Josh Wue4e06932014-06-10 17:50:11 +08001963 nfc_prepare_interrupt(host, NFC_SR_RB_EDGE);
Josh Wu7dc37de2013-08-05 19:14:35 +08001964 nfc_wait_interrupt(host, NFC_SR_RB_EDGE);
1965 }
1966}
1967
Josh Wu6054d4d2013-08-05 19:14:37 +08001968static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1969 uint32_t offset, int data_len, const uint8_t *buf,
1970 int oob_required, int page, int cached, int raw)
1971{
1972 int cfg, len;
1973 int status = 0;
1974 struct atmel_nand_host *host = chip->priv;
1975 void __iomem *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
1976
1977 /* Subpage write is not supported */
1978 if (offset || (data_len < mtd->writesize))
1979 return -EINVAL;
1980
Josh Wu6054d4d2013-08-05 19:14:37 +08001981 len = mtd->writesize;
Josh Wu6054d4d2013-08-05 19:14:37 +08001982 /* Copy page data to sram that will write to nand via NFC */
1983 if (use_dma) {
1984 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
1985 /* Fall back to use cpu copy */
1986 memcpy32_toio(sram, buf, len);
1987 } else {
1988 memcpy32_toio(sram, buf, len);
1989 }
1990
Wu, Joshff0a2152014-08-05 18:38:52 +08001991 cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
1992 if (unlikely(raw) && oob_required) {
1993 memcpy32_toio(sram + len, chip->oob_poi, mtd->oobsize);
1994 len += mtd->oobsize;
1995 nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
1996 } else {
1997 nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
1998 }
1999
Josh Wu6054d4d2013-08-05 19:14:37 +08002000 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
2001 /*
2002 * When use NFC sram, need set up PMECC before send
2003 * NAND_CMD_SEQIN command. Since when the nand command
2004 * is sent, nfc will do transfer from sram and nand.
2005 */
2006 pmecc_enable(host, NAND_ECC_WRITE);
2007
2008 host->nfc->will_write_sram = true;
2009 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2010 host->nfc->will_write_sram = false;
2011
2012 if (likely(!raw))
2013 /* Need to write ecc into oob */
2014 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2015
2016 if (status < 0)
2017 return status;
2018
2019 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2020 status = chip->waitfunc(mtd, chip);
2021
2022 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2023 status = chip->errstat(mtd, chip, FL_WRITING, status, page);
2024
2025 if (status & NAND_STATUS_FAIL)
2026 return -EIO;
2027
2028 return 0;
2029}
2030
Josh Wu1ae9c092013-08-05 19:14:36 +08002031static int nfc_sram_init(struct mtd_info *mtd)
2032{
2033 struct nand_chip *chip = mtd->priv;
2034 struct atmel_nand_host *host = chip->priv;
2035 int res = 0;
2036
2037 /* Initialize the NFC CFG register */
2038 unsigned int cfg_nfc = 0;
2039
2040 /* set page size and oob layout */
2041 switch (mtd->writesize) {
2042 case 512:
2043 cfg_nfc = NFC_CFG_PAGESIZE_512;
2044 break;
2045 case 1024:
2046 cfg_nfc = NFC_CFG_PAGESIZE_1024;
2047 break;
2048 case 2048:
2049 cfg_nfc = NFC_CFG_PAGESIZE_2048;
2050 break;
2051 case 4096:
2052 cfg_nfc = NFC_CFG_PAGESIZE_4096;
2053 break;
2054 case 8192:
2055 cfg_nfc = NFC_CFG_PAGESIZE_8192;
2056 break;
2057 default:
2058 dev_err(host->dev, "Unsupported page size for NFC.\n");
2059 res = -ENXIO;
2060 return res;
2061 }
2062
2063 /* oob bytes size = (NFCSPARESIZE + 1) * 4
2064 * Max support spare size is 512 bytes. */
2065 cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
2066 & NFC_CFG_NFC_SPARESIZE);
2067 /* default set a max timeout */
2068 cfg_nfc |= NFC_CFG_RSPARE |
2069 NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
2070
2071 nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
2072
Josh Wu6054d4d2013-08-05 19:14:37 +08002073 host->nfc->will_write_sram = false;
Josh Wu1ae9c092013-08-05 19:14:36 +08002074 nfc_set_sram_bank(host, 0);
2075
Josh Wu6054d4d2013-08-05 19:14:37 +08002076 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
2077 if (host->nfc->write_by_sram) {
2078 if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
2079 chip->ecc.mode == NAND_ECC_NONE)
2080 chip->write_page = nfc_sram_write_page;
2081 else
2082 host->nfc->write_by_sram = false;
2083 }
Josh Wu1ae9c092013-08-05 19:14:36 +08002084
Josh Wu6054d4d2013-08-05 19:14:37 +08002085 dev_info(host->dev, "Using NFC Sram read %s\n",
2086 host->nfc->write_by_sram ? "and write" : "");
Josh Wu1ae9c092013-08-05 19:14:36 +08002087 return 0;
2088}
2089
Josh Wu7dc37de2013-08-05 19:14:35 +08002090static struct platform_driver atmel_nand_nfc_driver;
Andrew Victor42cb1402006-10-19 18:24:35 +02002091/*
2092 * Probe for the NAND device.
2093 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002094static int atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002095{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002096 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02002097 struct mtd_info *mtd;
2098 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002099 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002100 struct mtd_part_parser_data ppdata = {};
Josh Wu7dc37de2013-08-05 19:14:35 +08002101 int res, irq;
Andrew Victor42cb1402006-10-19 18:24:35 +02002102
2103 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002104 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Han9e3677a2013-12-26 12:00:16 +09002105 if (!host)
Andrew Victor42cb1402006-10-19 18:24:35 +02002106 return -ENOMEM;
Andrew Victor42cb1402006-10-19 18:24:35 +02002107
Josh Wu7dc37de2013-08-05 19:14:35 +08002108 res = platform_driver_register(&atmel_nand_nfc_driver);
2109 if (res)
2110 dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
2111
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002112 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2113 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
2114 if (IS_ERR(host->io_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002115 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002116 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02002117 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002118 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02002119
2120 mtd = &host->mtd;
2121 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002122 host->dev = &pdev->dev;
Josh Wue9d8da82013-09-18 11:31:19 +08002123 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
2124 /* Only when CONFIG_OF is enabled of_node can be parsed */
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002125 res = atmel_of_init_port(host, pdev->dev.of_node);
2126 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002127 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002128 } else {
Jingoo Han453810b2013-07-30 17:18:33 +09002129 memcpy(&host->board, dev_get_platdata(&pdev->dev),
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002130 sizeof(struct atmel_nand_data));
2131 }
Andrew Victor42cb1402006-10-19 18:24:35 +02002132
2133 nand_chip->priv = host; /* link the private data structures */
2134 mtd->priv = nand_chip;
2135 mtd->owner = THIS_MODULE;
2136
2137 /* Set address of NAND IO lines */
2138 nand_chip->IO_ADDR_R = host->io_base;
2139 nand_chip->IO_ADDR_W = host->io_base;
Ivan Kutena4265f82007-05-24 14:35:58 +03002140
Josh Wu7dc37de2013-08-05 19:14:35 +08002141 if (nand_nfc.is_initialized) {
2142 /* NFC driver is probed and initialized */
2143 host->nfc = &nand_nfc;
2144
2145 nand_chip->select_chip = nfc_select_chip;
2146 nand_chip->dev_ready = nfc_device_ready;
2147 nand_chip->cmdfunc = nfc_nand_command;
2148
2149 /* Initialize the interrupt for NFC */
2150 irq = platform_get_irq(pdev, 0);
2151 if (irq < 0) {
2152 dev_err(host->dev, "Cannot get HSMC irq!\n");
Wei Yongjunff52c672013-08-23 10:50:36 +08002153 res = irq;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002154 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002155 }
2156
Josh Wu7dc37de2013-08-05 19:14:35 +08002157 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
2158 0, "hsmc", host);
2159 if (res) {
2160 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
2161 irq);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002162 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002163 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002164 } else {
2165 res = atmel_nand_set_enable_ready_pins(mtd);
2166 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002167 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002168
Josh Wu7dc37de2013-08-05 19:14:35 +08002169 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002170 }
Ivan Kutena4265f82007-05-24 14:35:58 +03002171
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002172 nand_chip->ecc.mode = host->board.ecc_mode;
Raphaël Poggi796fe362014-07-29 15:27:27 +02002173 nand_chip->chip_delay = 40; /* 40us command delay time */
Andrew Victor42cb1402006-10-19 18:24:35 +02002174
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002175 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02002176 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08002177
2178 nand_chip->read_buf = atmel_read_buf;
2179 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02002180
Andrew Victor42cb1402006-10-19 18:24:35 +02002181 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002182 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002183
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002184 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002185 res = devm_gpio_request(&pdev->dev,
2186 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002187 if (res < 0) {
2188 dev_err(&pdev->dev,
2189 "can't request det gpio %d\n",
2190 host->board.det_pin);
2191 goto err_no_card;
2192 }
2193
2194 res = gpio_direction_input(host->board.det_pin);
2195 if (res < 0) {
2196 dev_err(&pdev->dev,
2197 "can't request input direction det gpio %d\n",
2198 host->board.det_pin);
2199 goto err_no_card;
2200 }
2201
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002202 if (gpio_get_value(host->board.det_pin)) {
Jingoo Han1295f972013-12-26 12:30:58 +09002203 dev_info(&pdev->dev, "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01002204 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002205 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02002206 }
2207 }
2208
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002209 if (host->board.on_flash_bbt || on_flash_bbt) {
Jingoo Han1295f972013-12-26 12:30:58 +09002210 dev_info(&pdev->dev, "Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -07002211 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa6972009-05-27 18:19:39 +03002212 }
2213
Josh Wu1b719262013-05-09 15:34:55 +08002214 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08002215 use_dma = 0;
2216
2217 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08002218 dma_cap_mask_t mask;
2219
2220 dma_cap_zero(mask);
2221 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02002222 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002223 if (!host->dma_chan) {
2224 dev_err(host->dev, "Failed to request DMA channel\n");
2225 use_dma = 0;
2226 }
2227 }
2228 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08002229 dev_info(host->dev, "Using %s for DMA transfers.\n",
2230 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08002231 else
2232 dev_info(host->dev, "No DMA support for NAND access.\n");
2233
Richard Genoud77f54922008-04-23 19:51:14 +02002234 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00002235 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02002236 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002237 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02002238 }
2239
Richard Genoud3fc23892008-10-12 08:42:28 +02002240 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08002241 if (host->has_pmecc)
2242 res = atmel_pmecc_nand_init_params(pdev, host);
2243 else
2244 res = atmel_hw_nand_init_params(pdev, host);
2245
Josh Wu3dfe41a2012-06-25 18:07:43 +08002246 if (res != 0)
2247 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02002248 }
2249
Josh Wu1ae9c092013-08-05 19:14:36 +08002250 /* initialize the nfc configuration register */
2251 if (host->nfc && host->nfc->use_nfc_sram) {
2252 res = nfc_sram_init(mtd);
2253 if (res) {
2254 host->nfc->use_nfc_sram = false;
2255 dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
2256 }
2257 }
2258
Richard Genoud77f54922008-04-23 19:51:14 +02002259 /* second phase scan */
2260 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02002261 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002262 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02002263 }
2264
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002265 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002266 ppdata.of_node = pdev->dev.of_node;
2267 res = mtd_device_parse_register(mtd, NULL, &ppdata,
2268 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02002269 if (!res)
2270 return res;
2271
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002272err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002273 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08002274 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08002275err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002276err_scan_ident:
2277err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002278 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002279 if (host->dma_chan)
2280 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002281err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02002282 return res;
2283}
2284
2285/*
2286 * Remove a NAND device.
2287 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002288static int atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002289{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002290 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +02002291 struct mtd_info *mtd = &host->mtd;
2292
2293 nand_release(mtd);
2294
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002295 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002296
Josh Wu1c7b8742012-06-29 17:47:55 +08002297 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
2298 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2299 pmerrloc_writel(host->pmerrloc_base, ELDIS,
2300 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08002301 }
2302
Hong Xucbc6c5e2011-01-18 14:36:05 +08002303 if (host->dma_chan)
2304 dma_release_channel(host->dma_chan);
2305
Josh Wu7dc37de2013-08-05 19:14:35 +08002306 platform_driver_unregister(&atmel_nand_nfc_driver);
2307
Andrew Victor42cb1402006-10-19 18:24:35 +02002308 return 0;
2309}
2310
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002311static const struct of_device_id atmel_nand_dt_ids[] = {
2312 { .compatible = "atmel,at91rm9200-nand" },
2313 { /* sentinel */ }
2314};
2315
2316MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002317
Josh Wu7dc37de2013-08-05 19:14:35 +08002318static int atmel_nand_nfc_probe(struct platform_device *pdev)
2319{
2320 struct atmel_nfc *nfc = &nand_nfc;
2321 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002322 int ret;
Josh Wu7dc37de2013-08-05 19:14:35 +08002323
2324 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2325 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
2326 if (IS_ERR(nfc->base_cmd_regs))
2327 return PTR_ERR(nfc->base_cmd_regs);
2328
2329 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2330 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
2331 if (IS_ERR(nfc->hsmc_regs))
2332 return PTR_ERR(nfc->hsmc_regs);
2333
2334 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2335 if (nfc_sram) {
2336 nfc->sram_bank0 = devm_ioremap_resource(&pdev->dev, nfc_sram);
Josh Wu1ae9c092013-08-05 19:14:36 +08002337 if (IS_ERR(nfc->sram_bank0)) {
Josh Wu7dc37de2013-08-05 19:14:35 +08002338 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2339 PTR_ERR(nfc->sram_bank0));
Josh Wu1ae9c092013-08-05 19:14:36 +08002340 } else {
2341 nfc->use_nfc_sram = true;
Josh Wu7dc37de2013-08-05 19:14:35 +08002342 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
Josh Wu6054d4d2013-08-05 19:14:37 +08002343
2344 if (pdev->dev.of_node)
2345 nfc->write_by_sram = of_property_read_bool(
2346 pdev->dev.of_node,
2347 "atmel,write-by-sram");
Josh Wu1ae9c092013-08-05 19:14:36 +08002348 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002349 }
2350
Wu, Josh50e04e22014-06-10 17:50:09 +08002351 nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
2352 nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */
2353
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002354 nfc->clk = devm_clk_get(&pdev->dev, NULL);
2355 if (!IS_ERR(nfc->clk)) {
2356 ret = clk_prepare_enable(nfc->clk);
2357 if (ret)
2358 return ret;
2359 } else {
2360 dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree");
2361 }
2362
Josh Wu7dc37de2013-08-05 19:14:35 +08002363 nfc->is_initialized = true;
2364 dev_info(&pdev->dev, "NFC is probed.\n");
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002365
2366 return 0;
2367}
2368
2369static int atmel_nand_nfc_remove(struct platform_device *pdev)
2370{
2371 struct atmel_nfc *nfc = &nand_nfc;
2372
2373 if (!IS_ERR(nfc->clk))
2374 clk_disable_unprepare(nfc->clk);
2375
Josh Wu7dc37de2013-08-05 19:14:35 +08002376 return 0;
2377}
2378
Josh Wu81f29b42013-09-18 11:31:20 +08002379static const struct of_device_id atmel_nand_nfc_match[] = {
Josh Wu7dc37de2013-08-05 19:14:35 +08002380 { .compatible = "atmel,sama5d3-nfc" },
2381 { /* sentinel */ }
2382};
Josh Wu81f29b42013-09-18 11:31:20 +08002383MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
Josh Wu7dc37de2013-08-05 19:14:35 +08002384
2385static struct platform_driver atmel_nand_nfc_driver = {
2386 .driver = {
2387 .name = "atmel_nand_nfc",
2388 .owner = THIS_MODULE,
2389 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2390 },
2391 .probe = atmel_nand_nfc_probe,
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002392 .remove = atmel_nand_nfc_remove,
Josh Wu7dc37de2013-08-05 19:14:35 +08002393};
2394
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002395static struct platform_driver atmel_nand_driver = {
Johan Hovold2c2b9282013-09-23 16:27:28 +02002396 .probe = atmel_nand_probe,
2397 .remove = atmel_nand_remove,
Andrew Victor42cb1402006-10-19 18:24:35 +02002398 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002399 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +02002400 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002401 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02002402 },
2403};
2404
Johan Hovold2c2b9282013-09-23 16:27:28 +02002405module_platform_driver(atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02002406
2407MODULE_LICENSE("GPL");
2408MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02002409MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002410MODULE_ALIAS("platform:atmel_nand");