blob: 68b9160108c9f114f6ea83e1cda5892923d0433d [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>
Andrew Victor42cb1402006-10-19 18:24:35 +020039#include <linux/mtd/mtd.h>
40#include <linux/mtd/nand.h>
41#include <linux/mtd/partitions.h>
42
Josh Wu7dc37de2013-08-05 19:14:35 +080043#include <linux/delay.h>
Hans-Christian Egtvedt5c39c4c2011-04-13 15:55:17 +020044#include <linux/dmaengine.h>
David Woodhouse90574d02008-06-07 08:49:00 +010045#include <linux/gpio.h>
Josh Wu7dc37de2013-08-05 19:14:35 +080046#include <linux/interrupt.h>
David Woodhouse90574d02008-06-07 08:49:00 +010047#include <linux/io.h>
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +080048#include <linux/platform_data/atmel.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020049
Hong Xucbc6c5e2011-01-18 14:36:05 +080050static int use_dma = 1;
51module_param(use_dma, int, 0);
52
Simon Polettef4fa6972009-05-27 18:19:39 +030053static int on_flash_bbt = 0;
54module_param(on_flash_bbt, int, 0);
55
Richard Genoud77f54922008-04-23 19:51:14 +020056/* Register access macros */
57#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020058 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020059#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020060 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020061
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020062#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Josh Wu7dc37de2013-08-05 19:14:35 +080063#include "atmel_nand_nfc.h" /* Nand Flash Controller definition */
Richard Genoud77f54922008-04-23 19:51:14 +020064
Wu, Josh51585772015-01-19 16:33:06 +080065struct atmel_nand_caps {
66 bool pmecc_correct_erase_page;
Romain Izard55750752016-02-10 10:56:25 +010067 uint8_t pmecc_max_correction;
Wu, Josh51585772015-01-19 16:33:06 +080068};
69
Boris Brezilloncc003832016-02-03 19:59:11 +010070/*
71 * oob layout for large page size
Richard Genoud77f54922008-04-23 19:51:14 +020072 * bad block info is on bytes 0 and 1
73 * the bytes have to be consecutives to avoid
74 * several NAND_CMD_RNDOUT during read
Boris Brezilloncc003832016-02-03 19:59:11 +010075 *
76 * oob layout for small page size
Richard Genoud77f54922008-04-23 19:51:14 +020077 * bad block info is on bytes 4 and 5
78 * the bytes have to be consecutives to avoid
79 * several NAND_CMD_RNDOUT during read
80 */
Boris Brezilloncc003832016-02-03 19:59:11 +010081static int atmel_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
82 struct mtd_oob_region *oobregion)
83{
84 if (section)
85 return -ERANGE;
86
87 oobregion->length = 4;
88 oobregion->offset = 0;
89
90 return 0;
91}
92
93static int atmel_ooblayout_free_sp(struct mtd_info *mtd, int section,
94 struct mtd_oob_region *oobregion)
95{
96 if (section)
97 return -ERANGE;
98
99 oobregion->offset = 6;
100 oobregion->length = mtd->oobsize - oobregion->offset;
101
102 return 0;
103}
104
105static const struct mtd_ooblayout_ops atmel_ooblayout_sp_ops = {
106 .ecc = atmel_ooblayout_ecc_sp,
107 .free = atmel_ooblayout_free_sp,
Richard Genoud77f54922008-04-23 19:51:14 +0200108};
109
Josh Wu7dc37de2013-08-05 19:14:35 +0800110struct atmel_nfc {
111 void __iomem *base_cmd_regs;
112 void __iomem *hsmc_regs;
Wu, Josh068b44b2014-11-07 15:26:09 +0800113 void *sram_bank0;
Josh Wu7dc37de2013-08-05 19:14:35 +0800114 dma_addr_t sram_bank0_phys;
Josh Wu1ae9c092013-08-05 19:14:36 +0800115 bool use_nfc_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +0800116 bool write_by_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +0800117
Boris BREZILLON2d405ec2014-09-13 01:23:59 +0200118 struct clk *clk;
119
Josh Wu7dc37de2013-08-05 19:14:35 +0800120 bool is_initialized;
Josh Wue4e06932014-06-10 17:50:11 +0800121 struct completion comp_ready;
122 struct completion comp_cmd_done;
123 struct completion comp_xfer_done;
Josh Wu1ae9c092013-08-05 19:14:36 +0800124
125 /* Point to the sram bank which include readed data via NFC */
Wu, Josh068b44b2014-11-07 15:26:09 +0800126 void *data_in_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +0800127 bool will_write_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +0800128};
129static struct atmel_nfc nand_nfc;
130
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200131struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +0200132 struct nand_chip nand_chip;
Andrew Victor42cb1402006-10-19 18:24:35 +0200133 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800134 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800135 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +0200136 struct device *dev;
137 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800138
139 struct completion comp;
140 struct dma_chan *dma_chan;
Josh Wua41b51a2012-06-29 17:47:54 +0800141
Josh Wu7dc37de2013-08-05 19:14:35 +0800142 struct atmel_nfc *nfc;
143
LABBE Corentin72eaec22015-11-20 08:45:16 +0100144 const struct atmel_nand_caps *caps;
Josh Wua41b51a2012-06-29 17:47:54 +0800145 bool has_pmecc;
146 u8 pmecc_corr_cap;
147 u16 pmecc_sector_size;
Josh Wuabb1cd02014-10-11 18:01:50 +0800148 bool has_no_lookup_table;
Josh Wua41b51a2012-06-29 17:47:54 +0800149 u32 pmecc_lookup_table_offset;
Josh Wue66b4312013-01-23 20:47:11 +0800150 u32 pmecc_lookup_table_offset_512;
151 u32 pmecc_lookup_table_offset_1024;
Josh Wu1c7b8742012-06-29 17:47:55 +0800152
Josh Wu1c7b8742012-06-29 17:47:55 +0800153 int pmecc_degree; /* Degree of remainders */
154 int pmecc_cw_len; /* Length of codeword */
155
156 void __iomem *pmerrloc_base;
Romain Izard55750752016-02-10 10:56:25 +0100157 void __iomem *pmerrloc_el_base;
Josh Wu1c7b8742012-06-29 17:47:55 +0800158 void __iomem *pmecc_rom_base;
159
160 /* lookup table for alpha_to and index_of */
161 void __iomem *pmecc_alpha_to;
162 void __iomem *pmecc_index_of;
163
164 /* data for pmecc computation */
165 int16_t *pmecc_partial_syn;
166 int16_t *pmecc_si;
167 int16_t *pmecc_smu; /* Sigma table */
168 int16_t *pmecc_lmu; /* polynomal order */
169 int *pmecc_mu;
170 int *pmecc_dmu;
171 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200172};
173
174/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900175 * Enable NAND.
176 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200177static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900178{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800179 if (gpio_is_valid(host->board.enable_pin))
180 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900181}
182
183/*
184 * Disable NAND.
185 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200186static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900187{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800188 if (gpio_is_valid(host->board.enable_pin))
189 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900190}
191
192/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200193 * Hardware specific access to control-lines
194 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200195static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200196{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100197 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100198 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +0200199
Atsushi Nemoto81365082008-04-27 01:51:12 +0900200 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900201 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200202 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900203 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200204 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900205 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200206 if (cmd == NAND_CMD_NONE)
207 return;
208
209 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800210 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200211 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800212 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200213}
214
215/*
216 * Read the Device Ready pin.
217 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200218static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200219{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100220 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100221 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +0200222
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800223 return gpio_get_value(host->board.rdy_pin) ^
224 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200225}
226
Josh Wu7dc37de2013-08-05 19:14:35 +0800227/* Set up for hardware ready pin and enable pin. */
228static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
229{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100230 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100231 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu7dc37de2013-08-05 19:14:35 +0800232 int res = 0;
233
234 if (gpio_is_valid(host->board.rdy_pin)) {
235 res = devm_gpio_request(host->dev,
236 host->board.rdy_pin, "nand_rdy");
237 if (res < 0) {
238 dev_err(host->dev,
239 "can't request rdy gpio %d\n",
240 host->board.rdy_pin);
241 return res;
242 }
243
244 res = gpio_direction_input(host->board.rdy_pin);
245 if (res < 0) {
246 dev_err(host->dev,
247 "can't request input direction rdy gpio %d\n",
248 host->board.rdy_pin);
249 return res;
250 }
251
252 chip->dev_ready = atmel_nand_device_ready;
253 }
254
255 if (gpio_is_valid(host->board.enable_pin)) {
256 res = devm_gpio_request(host->dev,
257 host->board.enable_pin, "nand_enable");
258 if (res < 0) {
259 dev_err(host->dev,
260 "can't request enable gpio %d\n",
261 host->board.enable_pin);
262 return res;
263 }
264
265 res = gpio_direction_output(host->board.enable_pin, 1);
266 if (res < 0) {
267 dev_err(host->dev,
268 "can't request output direction enable gpio %d\n",
269 host->board.enable_pin);
270 return res;
271 }
272 }
273
274 return res;
275}
276
Artem Bityutskiy50082312012-02-02 13:54:25 +0200277/*
278 * Minimal-overhead PIO for data access.
279 */
280static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
281{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100282 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100283 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200284
Josh Wu1ae9c092013-08-05 19:14:36 +0800285 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +0800286 memcpy(buf, host->nfc->data_in_sram, len);
Josh Wu1ae9c092013-08-05 19:14:36 +0800287 host->nfc->data_in_sram += len;
288 } else {
289 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
290 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200291}
292
293static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
294{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100295 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100296 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200297
Josh Wu1ae9c092013-08-05 19:14:36 +0800298 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +0800299 memcpy(buf, host->nfc->data_in_sram, len);
Josh Wu1ae9c092013-08-05 19:14:36 +0800300 host->nfc->data_in_sram += len;
301 } else {
302 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
303 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200304}
305
306static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
307{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100308 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200309
310 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
311}
312
313static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
314{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100315 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200316
317 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
318}
319
Hong Xucbc6c5e2011-01-18 14:36:05 +0800320static void dma_complete_func(void *completion)
321{
322 complete(completion);
323}
324
Josh Wu1ae9c092013-08-05 19:14:36 +0800325static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
326{
327 /* NFC only has two banks. Must be 0 or 1 */
328 if (bank > 1)
329 return -EINVAL;
330
331 if (bank) {
Boris BREZILLONac01efe2015-12-10 08:59:50 +0100332 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
333
Josh Wu1ae9c092013-08-05 19:14:36 +0800334 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
Boris BREZILLONac01efe2015-12-10 08:59:50 +0100335 if (mtd->writesize > 2048)
Josh Wu1ae9c092013-08-05 19:14:36 +0800336 return -EINVAL;
337 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
338 } else {
339 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
340 }
341
342 return 0;
343}
344
345static uint nfc_get_sram_off(struct atmel_nand_host *host)
346{
347 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
348 return NFC_SRAM_BANK1_OFFSET;
349 else
350 return 0;
351}
352
353static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
354{
355 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
356 return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
357 else
358 return host->nfc->sram_bank0_phys;
359}
360
Hong Xucbc6c5e2011-01-18 14:36:05 +0800361static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
362 int is_read)
363{
364 struct dma_device *dma_dev;
365 enum dma_ctrl_flags flags;
366 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
367 struct dma_async_tx_descriptor *tx = NULL;
368 dma_cookie_t cookie;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100369 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100370 struct atmel_nand_host *host = nand_get_controller_data(chip);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800371 void *p = buf;
372 int err = -EIO;
373 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Josh Wu1ae9c092013-08-05 19:14:36 +0800374 struct atmel_nfc *nfc = host->nfc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800375
Hong Xu80b4f812011-03-31 18:33:15 +0800376 if (buf >= high_memory)
377 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800378
379 dma_dev = host->dma_chan->device;
380
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200381 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800382
383 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
384 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
385 dev_err(host->dev, "Failed to dma_map_single\n");
386 goto err_buf;
387 }
388
389 if (is_read) {
Josh Wu1ae9c092013-08-05 19:14:36 +0800390 if (nfc && nfc->data_in_sram)
391 dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
392 - (nfc->sram_bank0 + nfc_get_sram_off(host)));
393 else
394 dma_src_addr = host->io_phys;
395
Hong Xucbc6c5e2011-01-18 14:36:05 +0800396 dma_dst_addr = phys_addr;
397 } else {
398 dma_src_addr = phys_addr;
Josh Wu6054d4d2013-08-05 19:14:37 +0800399
400 if (nfc && nfc->write_by_sram)
401 dma_dst_addr = nfc_sram_phys(host);
402 else
403 dma_dst_addr = host->io_phys;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800404 }
405
406 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
407 dma_src_addr, len, flags);
408 if (!tx) {
409 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
410 goto err_dma;
411 }
412
413 init_completion(&host->comp);
414 tx->callback = dma_complete_func;
415 tx->callback_param = &host->comp;
416
417 cookie = tx->tx_submit(tx);
418 if (dma_submit_error(cookie)) {
419 dev_err(host->dev, "Failed to do DMA tx_submit\n");
420 goto err_dma;
421 }
422
423 dma_async_issue_pending(host->dma_chan);
424 wait_for_completion(&host->comp);
425
Josh Wu1ae9c092013-08-05 19:14:36 +0800426 if (is_read && nfc && nfc->data_in_sram)
427 /* After read data from SRAM, need to increase the position */
428 nfc->data_in_sram += len;
429
Hong Xucbc6c5e2011-01-18 14:36:05 +0800430 err = 0;
431
432err_dma:
433 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
434err_buf:
435 if (err != 0)
Nicolas Ferre74414a942014-02-12 12:26:54 +0100436 dev_dbg(host->dev, "Fall back to CPU I/O\n");
Hong Xucbc6c5e2011-01-18 14:36:05 +0800437 return err;
438}
439
440static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
441{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100442 struct nand_chip *chip = mtd_to_nand(mtd);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800443
Nicolas Ferre9d515672011-04-01 16:40:44 +0200444 if (use_dma && len > mtd->oobsize)
445 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800446 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
447 return;
448
Boris Brezillon4f3cab92016-04-01 14:54:22 +0200449 if (chip->options & NAND_BUSWIDTH_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200450 atmel_read_buf16(mtd, buf, len);
451 else
452 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800453}
454
455static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
456{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100457 struct nand_chip *chip = mtd_to_nand(mtd);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800458
Nicolas Ferre9d515672011-04-01 16:40:44 +0200459 if (use_dma && len > mtd->oobsize)
460 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800461 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
462 return;
463
Boris Brezillon4f3cab92016-04-01 14:54:22 +0200464 if (chip->options & NAND_BUSWIDTH_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200465 atmel_write_buf16(mtd, buf, len);
466 else
467 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800468}
469
David Brownell23a346c2008-07-03 23:40:16 -0700470/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800471 * Return number of ecc bytes per sector according to sector size and
472 * correction capability
473 *
474 * Following table shows what at91 PMECC supported:
475 * Correction Capability Sector_512_bytes Sector_1024_bytes
476 * ===================== ================ =================
477 * 2-bits 4-bytes 4-bytes
478 * 4-bits 7-bytes 7-bytes
479 * 8-bits 13-bytes 14-bytes
480 * 12-bits 20-bytes 21-bytes
481 * 24-bits 39-bytes 42-bytes
Romain Izard94248462016-02-10 10:56:26 +0100482 * 32-bits 52-bytes 56-bytes
Josh Wu1c7b8742012-06-29 17:47:55 +0800483 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500484static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800485{
486 int m = 12 + sector_size / 512;
487 return (m * cap + 7) / 8;
488}
489
Bill Pemberton06f25512012-11-19 13:23:07 -0500490static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800491{
492 int table_size;
493
494 table_size = host->pmecc_sector_size == 512 ?
495 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
496
497 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
498 table_size * sizeof(int16_t);
499}
500
Bill Pemberton06f25512012-11-19 13:23:07 -0500501static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800502{
503 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800504 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800505
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800506 size = (2 * cap + 1) * sizeof(int16_t);
507 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
508 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
509 host->pmecc_lmu = devm_kzalloc(host->dev,
510 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
511 host->pmecc_smu = devm_kzalloc(host->dev,
512 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800513
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800514 size = (cap + 1) * sizeof(int);
515 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
516 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
517 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800518
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800519 if (!host->pmecc_partial_syn ||
520 !host->pmecc_si ||
521 !host->pmecc_lmu ||
522 !host->pmecc_smu ||
523 !host->pmecc_mu ||
524 !host->pmecc_dmu ||
525 !host->pmecc_delta)
526 return -ENOMEM;
527
528 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800529}
530
531static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
532{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100533 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100534 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800535 int i;
536 uint32_t value;
537
538 /* Fill odd syndromes */
539 for (i = 0; i < host->pmecc_corr_cap; i++) {
540 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
541 if (i & 1)
542 value >>= 16;
543 value &= 0xffff;
544 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
545 }
546}
547
548static void pmecc_substitute(struct mtd_info *mtd)
549{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100550 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100551 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800552 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
553 int16_t __iomem *index_of = host->pmecc_index_of;
554 int16_t *partial_syn = host->pmecc_partial_syn;
555 const int cap = host->pmecc_corr_cap;
556 int16_t *si;
557 int i, j;
558
559 /* si[] is a table that holds the current syndrome value,
560 * an element of that table belongs to the field
561 */
562 si = host->pmecc_si;
563
564 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
565
566 /* Computation 2t syndromes based on S(x) */
567 /* Odd syndromes */
568 for (i = 1; i < 2 * cap; i += 2) {
569 for (j = 0; j < host->pmecc_degree; j++) {
570 if (partial_syn[i] & ((unsigned short)0x1 << j))
571 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
572 }
573 }
574 /* Even syndrome = (Odd syndrome) ** 2 */
575 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
576 if (si[j] == 0) {
577 si[i] = 0;
578 } else {
579 int16_t tmp;
580
581 tmp = readw_relaxed(index_of + si[j]);
582 tmp = (tmp * 2) % host->pmecc_cw_len;
583 si[i] = readw_relaxed(alpha_to + tmp);
584 }
585 }
586
587 return;
588}
589
590static void pmecc_get_sigma(struct mtd_info *mtd)
591{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100592 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100593 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800594
595 int16_t *lmu = host->pmecc_lmu;
596 int16_t *si = host->pmecc_si;
597 int *mu = host->pmecc_mu;
598 int *dmu = host->pmecc_dmu; /* Discrepancy */
599 int *delta = host->pmecc_delta; /* Delta order */
600 int cw_len = host->pmecc_cw_len;
601 const int16_t cap = host->pmecc_corr_cap;
602 const int num = 2 * cap + 1;
603 int16_t __iomem *index_of = host->pmecc_index_of;
604 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
605 int i, j, k;
606 uint32_t dmu_0_count, tmp;
607 int16_t *smu = host->pmecc_smu;
608
609 /* index of largest delta */
610 int ro;
611 int largest;
612 int diff;
613
614 dmu_0_count = 0;
615
616 /* First Row */
617
618 /* Mu */
619 mu[0] = -1;
620
621 memset(smu, 0, sizeof(int16_t) * num);
622 smu[0] = 1;
623
624 /* discrepancy set to 1 */
625 dmu[0] = 1;
626 /* polynom order set to 0 */
627 lmu[0] = 0;
628 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
629
630 /* Second Row */
631
632 /* Mu */
633 mu[1] = 0;
634 /* Sigma(x) set to 1 */
635 memset(&smu[num], 0, sizeof(int16_t) * num);
636 smu[num] = 1;
637
638 /* discrepancy set to S1 */
639 dmu[1] = si[1];
640
641 /* polynom order set to 0 */
642 lmu[1] = 0;
643
644 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
645
646 /* Init the Sigma(x) last row */
647 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
648
649 for (i = 1; i <= cap; i++) {
650 mu[i + 1] = i << 1;
651 /* Begin Computing Sigma (Mu+1) and L(mu) */
652 /* check if discrepancy is set to 0 */
653 if (dmu[i] == 0) {
654 dmu_0_count++;
655
656 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
657 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
658 tmp += 2;
659 else
660 tmp += 1;
661
662 if (dmu_0_count == tmp) {
663 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
664 smu[(cap + 1) * num + j] =
665 smu[i * num + j];
666
667 lmu[cap + 1] = lmu[i];
668 return;
669 }
670
671 /* copy polynom */
672 for (j = 0; j <= lmu[i] >> 1; j++)
673 smu[(i + 1) * num + j] = smu[i * num + j];
674
675 /* copy previous polynom order to the next */
676 lmu[i + 1] = lmu[i];
677 } else {
678 ro = 0;
679 largest = -1;
680 /* find largest delta with dmu != 0 */
681 for (j = 0; j < i; j++) {
682 if ((dmu[j]) && (delta[j] > largest)) {
683 largest = delta[j];
684 ro = j;
685 }
686 }
687
688 /* compute difference */
689 diff = (mu[i] - mu[ro]);
690
691 /* Compute degree of the new smu polynomial */
692 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
693 lmu[i + 1] = lmu[i];
694 else
695 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
696
697 /* Init smu[i+1] with 0 */
698 for (k = 0; k < num; k++)
699 smu[(i + 1) * num + k] = 0;
700
701 /* Compute smu[i+1] */
702 for (k = 0; k <= lmu[ro] >> 1; k++) {
703 int16_t a, b, c;
704
705 if (!(smu[ro * num + k] && dmu[i]))
706 continue;
707 a = readw_relaxed(index_of + dmu[i]);
708 b = readw_relaxed(index_of + dmu[ro]);
709 c = readw_relaxed(index_of + smu[ro * num + k]);
710 tmp = a + (cw_len - b) + c;
711 a = readw_relaxed(alpha_to + tmp % cw_len);
712 smu[(i + 1) * num + (k + diff)] = a;
713 }
714
715 for (k = 0; k <= lmu[i] >> 1; k++)
716 smu[(i + 1) * num + k] ^= smu[i * num + k];
717 }
718
719 /* End Computing Sigma (Mu+1) and L(mu) */
720 /* In either case compute delta */
721 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
722
723 /* Do not compute discrepancy for the last iteration */
724 if (i >= cap)
725 continue;
726
727 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
728 tmp = 2 * (i - 1);
729 if (k == 0) {
730 dmu[i + 1] = si[tmp + 3];
731 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
732 int16_t a, b, c;
733 a = readw_relaxed(index_of +
734 smu[(i + 1) * num + k]);
735 b = si[2 * (i - 1) + 3 - k];
736 c = readw_relaxed(index_of + b);
737 tmp = a + c;
738 tmp %= cw_len;
739 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
740 dmu[i + 1];
741 }
742 }
743 }
744
745 return;
746}
747
748static int pmecc_err_location(struct mtd_info *mtd)
749{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100750 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100751 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800752 unsigned long end_time;
753 const int cap = host->pmecc_corr_cap;
754 const int num = 2 * cap + 1;
755 int sector_size = host->pmecc_sector_size;
756 int err_nbr = 0; /* number of error */
757 int roots_nbr; /* number of roots */
758 int i;
759 uint32_t val;
760 int16_t *smu = host->pmecc_smu;
761
762 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
763
764 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
765 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
766 smu[(cap + 1) * num + i]);
767 err_nbr++;
768 }
769
770 val = (err_nbr - 1) << 16;
771 if (sector_size == 1024)
772 val |= 1;
773
774 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
775 pmerrloc_writel(host->pmerrloc_base, ELEN,
776 sector_size * 8 + host->pmecc_degree * cap);
777
778 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
779 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
780 & PMERRLOC_CALC_DONE)) {
781 if (unlikely(time_after(jiffies, end_time))) {
782 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
783 return -1;
784 }
785 cpu_relax();
786 }
787
788 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
789 & PMERRLOC_ERR_NUM_MASK) >> 8;
790 /* Number of roots == degree of smu hence <= cap */
791 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
792 return err_nbr - 1;
793
794 /* Number of roots does not match the degree of smu
795 * unable to correct error */
796 return -1;
797}
798
799static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
800 int sector_num, int extra_bytes, int err_nbr)
801{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100802 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100803 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800804 int i = 0;
805 int byte_pos, bit_pos, sector_size, pos;
806 uint32_t tmp;
807 uint8_t err_byte;
808
809 sector_size = host->pmecc_sector_size;
810
811 while (err_nbr) {
Romain Izard55750752016-02-10 10:56:25 +0100812 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_el_base, i) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +0800813 byte_pos = tmp / 8;
814 bit_pos = tmp % 8;
815
816 if (byte_pos >= (sector_size + extra_bytes))
817 BUG(); /* should never happen */
818
819 if (byte_pos < sector_size) {
820 err_byte = *(buf + byte_pos);
821 *(buf + byte_pos) ^= (1 << bit_pos);
822
823 pos = sector_num * host->pmecc_sector_size + byte_pos;
Romain Izard12197bf2016-01-13 17:34:13 +0100824 dev_dbg(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
Josh Wu1c7b8742012-06-29 17:47:55 +0800825 pos, bit_pos, err_byte, *(buf + byte_pos));
826 } else {
Boris Brezillon78d28e82016-02-03 20:11:14 +0100827 struct mtd_oob_region oobregion;
828
Josh Wu1c7b8742012-06-29 17:47:55 +0800829 /* Bit flip in OOB area */
Wu, Josh022a4782014-08-08 17:12:35 +0800830 tmp = sector_num * nand_chip->ecc.bytes
Josh Wu1c7b8742012-06-29 17:47:55 +0800831 + (byte_pos - sector_size);
832 err_byte = ecc[tmp];
833 ecc[tmp] ^= (1 << bit_pos);
834
Boris Brezillon78d28e82016-02-03 20:11:14 +0100835 mtd_ooblayout_ecc(mtd, 0, &oobregion);
836 pos = tmp + oobregion.offset;
Romain Izard12197bf2016-01-13 17:34:13 +0100837 dev_dbg(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
Josh Wu1c7b8742012-06-29 17:47:55 +0800838 pos, bit_pos, err_byte, ecc[tmp]);
839 }
840
841 i++;
842 err_nbr--;
843 }
844
845 return;
846}
847
848static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
849 u8 *ecc)
850{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100851 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100852 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Bo Shenb3857662014-06-12 15:58:45 +0800853 int i, err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800854 uint8_t *buf_pos;
Wu, Josh267d46e2015-01-14 11:50:46 +0800855 int max_bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800856
Wu, Joshc9447ff2014-08-08 17:12:34 +0800857 for (i = 0; i < nand_chip->ecc.steps; i++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800858 err_nbr = 0;
859 if (pmecc_stat & 0x1) {
860 buf_pos = buf + i * host->pmecc_sector_size;
861
862 pmecc_gen_syndrome(mtd, i);
863 pmecc_substitute(mtd);
864 pmecc_get_sigma(mtd);
865
866 err_nbr = pmecc_err_location(mtd);
Boris Brezillonff6ee102016-03-01 14:11:52 +0100867 if (err_nbr >= 0) {
868 pmecc_correct_data(mtd, buf_pos, ecc, i,
869 nand_chip->ecc.bytes,
870 err_nbr);
871 } else if (!host->caps->pmecc_correct_erase_page) {
872 u8 *ecc_pos = ecc + (i * nand_chip->ecc.bytes);
873
874 /* Try to detect erased pages */
875 err_nbr = nand_check_erased_ecc_chunk(buf_pos,
876 host->pmecc_sector_size,
877 ecc_pos,
878 nand_chip->ecc.bytes,
879 NULL, 0,
880 nand_chip->ecc.strength);
881 }
882
883 if (err_nbr < 0) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800884 dev_err(host->dev, "PMECC: Too many errors\n");
885 mtd->ecc_stats.failed++;
886 return -EIO;
Josh Wu1c7b8742012-06-29 17:47:55 +0800887 }
Boris Brezillonff6ee102016-03-01 14:11:52 +0100888
889 mtd->ecc_stats.corrected += err_nbr;
890 max_bitflips = max_t(int, max_bitflips, err_nbr);
Josh Wu1c7b8742012-06-29 17:47:55 +0800891 }
892 pmecc_stat >>= 1;
893 }
894
Wu, Josh267d46e2015-01-14 11:50:46 +0800895 return max_bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800896}
897
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800898static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
899{
900 u32 val;
901
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800902 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
903 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
904 return;
905 }
906
Josh Wu1fad0e82013-08-07 17:58:11 +0800907 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
908 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
909 val = pmecc_readl_relaxed(host->ecc, CFG);
910
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800911 if (ecc_op == NAND_ECC_READ)
912 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
913 | PMECC_CFG_AUTO_ENABLE);
914 else
915 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
916 & ~PMECC_CFG_AUTO_ENABLE);
917
918 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
919 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
920}
921
Josh Wu1c7b8742012-06-29 17:47:55 +0800922static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
923 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
924{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100925 struct atmel_nand_host *host = nand_get_controller_data(chip);
Bo Shenb3857662014-06-12 15:58:45 +0800926 int eccsize = chip->ecc.size * chip->ecc.steps;
Josh Wu1c7b8742012-06-29 17:47:55 +0800927 uint8_t *oob = chip->oob_poi;
Josh Wu1c7b8742012-06-29 17:47:55 +0800928 uint32_t stat;
929 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800930 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800931
Josh Wu1ae9c092013-08-05 19:14:36 +0800932 if (!host->nfc || !host->nfc->use_nfc_sram)
933 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800934
935 chip->read_buf(mtd, buf, eccsize);
936 chip->read_buf(mtd, oob, mtd->oobsize);
937
938 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
939 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
940 if (unlikely(time_after(jiffies, end_time))) {
941 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
942 return -EIO;
943 }
944 cpu_relax();
945 }
946
947 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800948 if (stat != 0) {
Boris Brezillon78d28e82016-02-03 20:11:14 +0100949 struct mtd_oob_region oobregion;
950
951 mtd_ooblayout_ecc(mtd, 0, &oobregion);
952 bitflips = pmecc_correction(mtd, stat, buf,
953 &oob[oobregion.offset]);
Josh Wuc0c70d92012-11-27 18:50:31 +0800954 if (bitflips < 0)
955 /* uncorrectable errors */
956 return 0;
957 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800958
Josh Wuc0c70d92012-11-27 18:50:31 +0800959 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800960}
961
962static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +0200963 struct nand_chip *chip, const uint8_t *buf, int oob_required,
964 int page)
Josh Wu1c7b8742012-06-29 17:47:55 +0800965{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100966 struct atmel_nand_host *host = nand_get_controller_data(chip);
Boris Brezillon78d28e82016-02-03 20:11:14 +0100967 struct mtd_oob_region oobregion = { };
968 int i, j, section = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800969 unsigned long end_time;
970
Josh Wu6054d4d2013-08-05 19:14:37 +0800971 if (!host->nfc || !host->nfc->write_by_sram) {
972 pmecc_enable(host, NAND_ECC_WRITE);
973 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
974 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800975
976 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
977 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
978 if (unlikely(time_after(jiffies, end_time))) {
979 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
980 return -EIO;
981 }
982 cpu_relax();
983 }
984
Wu, Joshc9447ff2014-08-08 17:12:34 +0800985 for (i = 0; i < chip->ecc.steps; i++) {
Wu, Josh022a4782014-08-08 17:12:35 +0800986 for (j = 0; j < chip->ecc.bytes; j++) {
Boris Brezillon78d28e82016-02-03 20:11:14 +0100987 if (!oobregion.length)
988 mtd_ooblayout_ecc(mtd, section, &oobregion);
Josh Wu1c7b8742012-06-29 17:47:55 +0800989
Boris Brezillon78d28e82016-02-03 20:11:14 +0100990 chip->oob_poi[oobregion.offset] =
Josh Wu1c7b8742012-06-29 17:47:55 +0800991 pmecc_readb_ecc_relaxed(host->ecc, i, j);
Boris Brezillon78d28e82016-02-03 20:11:14 +0100992 oobregion.length--;
993 oobregion.offset++;
994 section++;
Josh Wu1c7b8742012-06-29 17:47:55 +0800995 }
996 }
997 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
998
999 return 0;
1000}
1001
1002static void atmel_pmecc_core_init(struct mtd_info *mtd)
1003{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001004 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001005 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Boris Brezilloncc003832016-02-03 19:59:11 +01001006 int eccbytes = mtd_ooblayout_count_eccbytes(mtd);
Josh Wu1c7b8742012-06-29 17:47:55 +08001007 uint32_t val = 0;
Boris Brezillon78d28e82016-02-03 20:11:14 +01001008 struct mtd_oob_region oobregion;
Josh Wu1c7b8742012-06-29 17:47:55 +08001009
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;
Romain Izard94248462016-02-10 10:56:26 +01001029 case 32:
1030 val = PMECC_CFG_BCH_ERR32;
1031 break;
Josh Wu1c7b8742012-06-29 17:47:55 +08001032 }
1033
1034 if (host->pmecc_sector_size == 512)
1035 val |= PMECC_CFG_SECTOR512;
1036 else if (host->pmecc_sector_size == 1024)
1037 val |= PMECC_CFG_SECTOR1024;
1038
Wu, Joshc9447ff2014-08-08 17:12:34 +08001039 switch (nand_chip->ecc.steps) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001040 case 1:
1041 val |= PMECC_CFG_PAGE_1SECTOR;
1042 break;
1043 case 2:
1044 val |= PMECC_CFG_PAGE_2SECTORS;
1045 break;
1046 case 4:
1047 val |= PMECC_CFG_PAGE_4SECTORS;
1048 break;
1049 case 8:
1050 val |= PMECC_CFG_PAGE_8SECTORS;
1051 break;
1052 }
1053
1054 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
1055 | PMECC_CFG_AUTO_DISABLE);
1056 pmecc_writel(host->ecc, CFG, val);
1057
Josh Wu1c7b8742012-06-29 17:47:55 +08001058 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
Boris Brezillon78d28e82016-02-03 20:11:14 +01001059 mtd_ooblayout_ecc(mtd, 0, &oobregion);
1060 pmecc_writel(host->ecc, SADDR, oobregion.offset);
Josh Wu1c7b8742012-06-29 17:47:55 +08001061 pmecc_writel(host->ecc, EADDR,
Boris Brezilloncc003832016-02-03 19:59:11 +01001062 oobregion.offset + eccbytes - 1);
Josh Wu1c7b8742012-06-29 17:47:55 +08001063 /* See datasheet about PMECC Clock Control Register */
1064 pmecc_writel(host->ecc, CLK, 2);
1065 pmecc_writel(host->ecc, IDR, 0xff);
1066 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
1067}
1068
Josh Wu84cfbbb2013-01-23 20:47:12 +08001069/*
Josh Wu2a3d9332013-09-18 13:58:48 +08001070 * Get minimum ecc requirements from NAND.
Josh Wu84cfbbb2013-01-23 20:47:12 +08001071 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
Josh Wu2a3d9332013-09-18 13:58:48 +08001072 * will set them according to minimum ecc requirement. Otherwise, use the
Josh Wu84cfbbb2013-01-23 20:47:12 +08001073 * value in DTS file.
1074 * return 0 if success. otherwise return error code.
1075 */
1076static int pmecc_choose_ecc(struct atmel_nand_host *host,
1077 int *cap, int *sector_size)
1078{
Josh Wu2a3d9332013-09-18 13:58:48 +08001079 /* Get minimum ECC requirements */
1080 if (host->nand_chip.ecc_strength_ds) {
1081 *cap = host->nand_chip.ecc_strength_ds;
1082 *sector_size = host->nand_chip.ecc_step_ds;
1083 dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n",
Josh Wu84cfbbb2013-01-23 20:47:12 +08001084 *cap, *sector_size);
Josh Wu84cfbbb2013-01-23 20:47:12 +08001085 } else {
Josh Wu84cfbbb2013-01-23 20:47:12 +08001086 *cap = 2;
1087 *sector_size = 512;
Josh Wu2a3d9332013-09-18 13:58:48 +08001088 dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
Josh Wu84cfbbb2013-01-23 20:47:12 +08001089 }
1090
Josh Wu2a3d9332013-09-18 13:58:48 +08001091 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
Josh Wu84cfbbb2013-01-23 20:47:12 +08001092 if (host->pmecc_corr_cap == 0) {
Romain Izard94248462016-02-10 10:56:26 +01001093 if (*cap > host->caps->pmecc_max_correction)
1094 return -EINVAL;
1095
Josh Wu84cfbbb2013-01-23 20:47:12 +08001096 /* use the most fitable ecc bits (the near bigger one ) */
1097 if (*cap <= 2)
1098 host->pmecc_corr_cap = 2;
1099 else if (*cap <= 4)
1100 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +08001101 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001102 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +08001103 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001104 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +08001105 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001106 host->pmecc_corr_cap = 24;
Romain Izard94248462016-02-10 10:56:26 +01001107 else if (*cap <= 32)
1108 host->pmecc_corr_cap = 32;
Josh Wu84cfbbb2013-01-23 20:47:12 +08001109 else
1110 return -EINVAL;
1111 }
1112 if (host->pmecc_sector_size == 0) {
1113 /* use the most fitable sector size (the near smaller one ) */
1114 if (*sector_size >= 1024)
1115 host->pmecc_sector_size = 1024;
1116 else if (*sector_size >= 512)
1117 host->pmecc_sector_size = 512;
1118 else
1119 return -EINVAL;
1120 }
1121 return 0;
1122}
1123
Josh Wuabb1cd02014-10-11 18:01:50 +08001124static inline int deg(unsigned int poly)
1125{
1126 /* polynomial degree is the most-significant bit index */
1127 return fls(poly) - 1;
1128}
1129
1130static int build_gf_tables(int mm, unsigned int poly,
1131 int16_t *index_of, int16_t *alpha_to)
1132{
1133 unsigned int i, x = 1;
1134 const unsigned int k = 1 << deg(poly);
1135 unsigned int nn = (1 << mm) - 1;
1136
1137 /* primitive polynomial must be of degree m */
1138 if (k != (1u << mm))
1139 return -EINVAL;
1140
1141 for (i = 0; i < nn; i++) {
1142 alpha_to[i] = x;
1143 index_of[x] = i;
1144 if (i && (x == 1))
1145 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
1146 return -EINVAL;
1147 x <<= 1;
1148 if (x & k)
1149 x ^= poly;
1150 }
1151 alpha_to[nn] = 1;
1152 index_of[0] = 0;
1153
1154 return 0;
1155}
1156
1157static uint16_t *create_lookup_table(struct device *dev, int sector_size)
1158{
1159 int degree = (sector_size == 512) ?
1160 PMECC_GF_DIMENSION_13 :
1161 PMECC_GF_DIMENSION_14;
1162 unsigned int poly = (sector_size == 512) ?
1163 PMECC_GF_13_PRIMITIVE_POLY :
1164 PMECC_GF_14_PRIMITIVE_POLY;
1165 int table_size = (sector_size == 512) ?
1166 PMECC_LOOKUP_TABLE_SIZE_512 :
1167 PMECC_LOOKUP_TABLE_SIZE_1024;
1168
1169 int16_t *addr = devm_kzalloc(dev, 2 * table_size * sizeof(uint16_t),
1170 GFP_KERNEL);
1171 if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
1172 return NULL;
1173
1174 return addr;
1175}
1176
Johan Hovold2c2b9282013-09-23 16:27:28 +02001177static int atmel_pmecc_nand_init_params(struct platform_device *pdev,
Josh Wu1c7b8742012-06-29 17:47:55 +08001178 struct atmel_nand_host *host)
1179{
Josh Wu1c7b8742012-06-29 17:47:55 +08001180 struct nand_chip *nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01001181 struct mtd_info *mtd = nand_to_mtd(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +08001182 struct resource *regs, *regs_pmerr, *regs_rom;
Josh Wuabb1cd02014-10-11 18:01:50 +08001183 uint16_t *galois_table;
Josh Wu1c7b8742012-06-29 17:47:55 +08001184 int cap, sector_size, err_no;
1185
Josh Wu84cfbbb2013-01-23 20:47:12 +08001186 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
1187 if (err_no) {
1188 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1189 return err_no;
1190 }
1191
Richard Genoudf666d642013-07-30 17:17:29 +02001192 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001193 sector_size != host->pmecc_sector_size)
1194 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001195
Josh Wu1c7b8742012-06-29 17:47:55 +08001196 cap = host->pmecc_corr_cap;
1197 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001198 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1199 host->pmecc_lookup_table_offset_512 :
1200 host->pmecc_lookup_table_offset_1024;
1201
Josh Wu1c7b8742012-06-29 17:47:55 +08001202 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1203 cap, sector_size);
1204
1205 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1206 if (!regs) {
1207 dev_warn(host->dev,
1208 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1209 nand_chip->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckid5f304a2016-04-17 22:52:59 +02001210 nand_chip->ecc.algo = NAND_ECC_HAMMING;
Josh Wu1c7b8742012-06-29 17:47:55 +08001211 return 0;
1212 }
1213
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001214 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1215 if (IS_ERR(host->ecc)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001216 err_no = PTR_ERR(host->ecc);
1217 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001218 }
1219
1220 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001221 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1222 if (IS_ERR(host->pmerrloc_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001223 err_no = PTR_ERR(host->pmerrloc_base);
1224 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001225 }
Romain Izard55750752016-02-10 10:56:25 +01001226 host->pmerrloc_el_base = host->pmerrloc_base + ATMEL_PMERRLOC_SIGMAx +
1227 (host->caps->pmecc_max_correction + 1) * 4;
Josh Wu1c7b8742012-06-29 17:47:55 +08001228
Wu, Josh41c75402015-04-02 14:13:47 +08001229 if (!host->has_no_lookup_table) {
1230 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1231 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev,
1232 regs_rom);
1233 if (IS_ERR(host->pmecc_rom_base)) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001234 dev_err(host->dev, "Can not get I/O resource for ROM, will build a lookup table in runtime!\n");
Wu, Josh41c75402015-04-02 14:13:47 +08001235 host->has_no_lookup_table = true;
1236 }
Josh Wuabb1cd02014-10-11 18:01:50 +08001237 }
1238
1239 if (host->has_no_lookup_table) {
1240 /* Build the look-up table in runtime */
1241 galois_table = create_lookup_table(host->dev, sector_size);
1242 if (!galois_table) {
1243 dev_err(host->dev, "Failed to build a lookup table in runtime!\n");
1244 err_no = -EINVAL;
1245 goto err;
1246 }
1247
1248 host->pmecc_rom_base = (void __iomem *)galois_table;
1249 host->pmecc_lookup_table_offset = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +08001250 }
1251
Bo Shenb3857662014-06-12 15:58:45 +08001252 nand_chip->ecc.size = sector_size;
Josh Wu1c7b8742012-06-29 17:47:55 +08001253
1254 /* set ECC page size and oob layout */
1255 switch (mtd->writesize) {
Wu, Josha3557102014-07-22 17:24:18 +08001256 case 512:
1257 case 1024:
Josh Wu1c7b8742012-06-29 17:47:55 +08001258 case 2048:
Wu, Josha3557102014-07-22 17:24:18 +08001259 case 4096:
1260 case 8192:
1261 if (sector_size > mtd->writesize) {
1262 dev_err(host->dev, "pmecc sector size is bigger than the page size!\n");
1263 err_no = -EINVAL;
1264 goto err;
1265 }
1266
Josh Wu2fa831f2013-08-19 18:05:44 +08001267 host->pmecc_degree = (sector_size == 512) ?
1268 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Josh Wu1c7b8742012-06-29 17:47:55 +08001269 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +08001270 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1271 host->pmecc_index_of = host->pmecc_rom_base +
1272 host->pmecc_lookup_table_offset;
1273
Josh Wu1c7b8742012-06-29 17:47:55 +08001274 nand_chip->ecc.strength = cap;
Wu, Josh022a4782014-08-08 17:12:35 +08001275 nand_chip->ecc.bytes = pmecc_get_ecc_bytes(cap, sector_size);
Wu, Joshc9447ff2014-08-08 17:12:34 +08001276 nand_chip->ecc.steps = mtd->writesize / sector_size;
1277 nand_chip->ecc.total = nand_chip->ecc.bytes *
1278 nand_chip->ecc.steps;
Josh Wu477478a2015-04-02 14:12:33 +08001279 if (nand_chip->ecc.total >
1280 mtd->oobsize - PMECC_OOB_RESERVED_BYTES) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001281 dev_err(host->dev, "No room for ECC bytes\n");
1282 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001283 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001284 }
Bo Shenb3857662014-06-12 15:58:45 +08001285
Boris Brezilloncc003832016-02-03 19:59:11 +01001286 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Josh Wu1c7b8742012-06-29 17:47:55 +08001287 break;
Wu, Josha3557102014-07-22 17:24:18 +08001288 default:
Josh Wu1c7b8742012-06-29 17:47:55 +08001289 dev_warn(host->dev,
1290 "Unsupported page size for PMECC, use Software ECC\n");
Josh Wu1c7b8742012-06-29 17:47:55 +08001291 /* page size not handled by HW ECC */
1292 /* switching back to soft ECC */
1293 nand_chip->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckid5f304a2016-04-17 22:52:59 +02001294 nand_chip->ecc.algo = NAND_ECC_HAMMING;
Josh Wu1c7b8742012-06-29 17:47:55 +08001295 return 0;
1296 }
1297
1298 /* Allocate data for PMECC computation */
1299 err_no = pmecc_data_alloc(host);
1300 if (err_no) {
1301 dev_err(host->dev,
1302 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001303 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001304 }
1305
Herve Codina90445ff2014-03-03 12:15:29 +01001306 nand_chip->options |= NAND_NO_SUBPAGE_WRITE;
Josh Wu1c7b8742012-06-29 17:47:55 +08001307 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1308 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1309
1310 atmel_pmecc_core_init(mtd);
1311
1312 return 0;
1313
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001314err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001315 return err_no;
1316}
1317
1318/*
Richard Genoud77f54922008-04-23 19:51:14 +02001319 * Calculate HW ECC
1320 *
1321 * function called after a write
1322 *
1323 * mtd: MTD block structure
1324 * dat: raw data (unused)
1325 * ecc_code: buffer for ECC
1326 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001327static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001328 const u_char *dat, unsigned char *ecc_code)
1329{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001330 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001331 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02001332 unsigned int ecc_value;
1333
1334 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001335 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001336
Richard Genoud3fc23892008-10-12 08:42:28 +02001337 ecc_code[0] = ecc_value & 0xFF;
1338 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001339
1340 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001341 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001342
Richard Genoud3fc23892008-10-12 08:42:28 +02001343 ecc_code[2] = ecc_value & 0xFF;
1344 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001345
1346 return 0;
1347}
1348
1349/*
1350 * HW ECC read page function
1351 *
1352 * mtd: mtd info structure
1353 * chip: nand chip info structure
1354 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001355 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001356 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001357static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1358 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001359{
1360 int eccsize = chip->ecc.size;
1361 int eccbytes = chip->ecc.bytes;
Richard Genoud77f54922008-04-23 19:51:14 +02001362 uint8_t *p = buf;
1363 uint8_t *oob = chip->oob_poi;
1364 uint8_t *ecc_pos;
1365 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001366 unsigned int max_bitflips = 0;
Boris Brezillon78d28e82016-02-03 20:11:14 +01001367 struct mtd_oob_region oobregion = {};
Richard Genoud77f54922008-04-23 19:51:14 +02001368
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001369 /*
1370 * Errata: ALE is incorrectly wired up to the ECC controller
1371 * on the AP7000, so it will include the address cycles in the
1372 * ECC calculation.
1373 *
1374 * Workaround: Reset the parity registers before reading the
1375 * actual data.
1376 */
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001377 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu71b94e22013-05-09 15:34:54 +08001378 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001379 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001380
Richard Genoud77f54922008-04-23 19:51:14 +02001381 /* read the page */
1382 chip->read_buf(mtd, p, eccsize);
1383
1384 /* move to ECC position if needed */
Boris Brezillon78d28e82016-02-03 20:11:14 +01001385 mtd_ooblayout_ecc(mtd, 0, &oobregion);
1386 if (oobregion.offset != 0) {
1387 /*
1388 * This only works on large pages because the ECC controller
1389 * waits for NAND_CMD_RNDOUTSTART after the NAND_CMD_RNDOUT.
1390 * Anyway, for small pages, the first ECC byte is at offset
1391 * 0 in the OOB area.
Richard Genoud77f54922008-04-23 19:51:14 +02001392 */
1393 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon78d28e82016-02-03 20:11:14 +01001394 mtd->writesize + oobregion.offset, -1);
Richard Genoud77f54922008-04-23 19:51:14 +02001395 }
1396
1397 /* the ECC controller needs to read the ECC just after the data */
Boris Brezillon78d28e82016-02-03 20:11:14 +01001398 ecc_pos = oob + oobregion.offset;
Richard Genoud77f54922008-04-23 19:51:14 +02001399 chip->read_buf(mtd, ecc_pos, eccbytes);
1400
1401 /* check if there's an error */
1402 stat = chip->ecc.correct(mtd, p, oob, NULL);
1403
Mike Dunn3f91e942012-04-25 12:06:09 -07001404 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001405 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001406 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001407 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001408 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1409 }
Richard Genoud77f54922008-04-23 19:51:14 +02001410
1411 /* get back to oob start (end of page) */
1412 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1413
1414 /* read the oob */
1415 chip->read_buf(mtd, oob, mtd->oobsize);
1416
Mike Dunn3f91e942012-04-25 12:06:09 -07001417 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001418}
1419
1420/*
1421 * HW ECC Correction
1422 *
1423 * function called after a read
1424 *
1425 * mtd: MTD block structure
1426 * dat: raw data read from the chip
1427 * read_ecc: ECC from the chip (unused)
1428 * isnull: unused
1429 *
1430 * Detect and correct a 1 bit error for a page
1431 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001432static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001433 u_char *read_ecc, u_char *isnull)
1434{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001435 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001436 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02001437 unsigned int ecc_status;
1438 unsigned int ecc_word, ecc_bit;
1439
1440 /* get the status from the Status Register */
1441 ecc_status = ecc_readl(host->ecc, SR);
1442
1443 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001444 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001445 return 0;
1446
1447 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001448 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001449 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001450 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001451 ecc_word >>= 4;
1452
1453 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001454 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001455 /* check if it is a freshly erased block
1456 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001457 if ((ecc_bit == ATMEL_ECC_BITADDR)
1458 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001459 /* the block has just been erased, return OK */
1460 return 0;
1461 }
1462 /* it doesn't seems to be a freshly
1463 * erased block.
1464 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001465 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001466 " Unable to correct.\n");
Boris BREZILLON6e941192015-12-30 20:32:03 +01001467 return -EBADMSG;
Richard Genoud77f54922008-04-23 19:51:14 +02001468 }
1469
1470 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001471 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001472 /* there's nothing much to do here.
1473 * the bit error is on the ECC itself.
1474 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001475 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001476 " Nothing to correct\n");
1477 return 0;
1478 }
1479
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001480 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001481 " (word offset in the page :"
1482 " 0x%x bit offset : 0x%x)\n",
1483 ecc_word, ecc_bit);
1484 /* correct the error */
1485 if (nand_chip->options & NAND_BUSWIDTH_16) {
1486 /* 16 bits words */
1487 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1488 } else {
1489 /* 8 bits words */
1490 dat[ecc_word] ^= (1 << ecc_bit);
1491 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001492 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001493 return 1;
1494}
1495
1496/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001497 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001498 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001499static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1500{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001501 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001502 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu71b94e22013-05-09 15:34:54 +08001503
1504 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001505 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001506}
Richard Genoud77f54922008-04-23 19:51:14 +02001507
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001508static int atmel_of_init_ecc(struct atmel_nand_host *host,
1509 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001510{
Josh Wua41b51a2012-06-29 17:47:54 +08001511 u32 offset[2];
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001512 u32 val;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001513
Josh Wua41b51a2012-06-29 17:47:54 +08001514 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1515
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001516 /* Not using PMECC */
1517 if (!(host->nand_chip.ecc.mode == NAND_ECC_HW) || !host->has_pmecc)
1518 return 0;
Josh Wua41b51a2012-06-29 17:47:54 +08001519
1520 /* use PMECC, get correction capability, sector size and lookup
1521 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001522 * If correction bits and sector size are not specified, then find
1523 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001524 */
Josh Wue66b4312013-01-23 20:47:11 +08001525 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
Romain Izard94248462016-02-10 10:56:26 +01001526 if (val > host->caps->pmecc_max_correction) {
1527 dev_err(host->dev,
1528 "Required ECC strength too high: %u max %u\n",
1529 val, host->caps->pmecc_max_correction);
1530 return -EINVAL;
1531 }
1532 if ((val != 2) && (val != 4) && (val != 8) &&
1533 (val != 12) && (val != 24) && (val != 32)) {
Josh Wue66b4312013-01-23 20:47:11 +08001534 dev_err(host->dev,
Romain Izarde88b7f72016-01-15 11:34:56 +01001535 "Required ECC strength not supported: %u\n",
Josh Wue66b4312013-01-23 20:47:11 +08001536 val);
1537 return -EINVAL;
1538 }
1539 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001540 }
Josh Wua41b51a2012-06-29 17:47:54 +08001541
Josh Wue66b4312013-01-23 20:47:11 +08001542 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1543 if ((val != 512) && (val != 1024)) {
1544 dev_err(host->dev,
Romain Izarde88b7f72016-01-15 11:34:56 +01001545 "Required ECC sector size not supported: %u\n",
Josh Wue66b4312013-01-23 20:47:11 +08001546 val);
1547 return -EINVAL;
1548 }
1549 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001550 }
Josh Wua41b51a2012-06-29 17:47:54 +08001551
1552 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1553 offset, 2) != 0) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001554 dev_err(host->dev, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n");
1555 host->has_no_lookup_table = true;
1556 /* Will build a lookup table and initialize the offset later */
1557 return 0;
Josh Wua41b51a2012-06-29 17:47:54 +08001558 }
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001559
Josh Wuc0cf7872013-01-23 20:47:08 +08001560 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001561 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1562 return -EINVAL;
1563 }
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001564
Josh Wue66b4312013-01-23 20:47:11 +08001565 host->pmecc_lookup_table_offset_512 = offset[0];
1566 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001567
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001568 return 0;
1569}
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001570
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001571static int atmel_of_init_port(struct atmel_nand_host *host,
1572 struct device_node *np)
1573{
1574 u32 val;
1575 struct atmel_nand_data *board = &host->board;
1576 enum of_gpio_flags flags = 0;
1577
1578 host->caps = (struct atmel_nand_caps *)
1579 of_device_get_match_data(host->dev);
1580
1581 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1582 if (val >= 32) {
1583 dev_err(host->dev, "invalid addr-offset %u\n", val);
1584 return -EINVAL;
1585 }
1586 board->ale = val;
1587 }
1588
1589 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1590 if (val >= 32) {
1591 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1592 return -EINVAL;
1593 }
1594 board->cle = val;
1595 }
1596
1597 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1598
1599 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1600 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1601
1602 board->enable_pin = of_get_gpio(np, 1);
1603 board->det_pin = of_get_gpio(np, 2);
1604
1605 /* load the nfc driver if there is */
1606 of_platform_populate(np, NULL, NULL, host->dev);
1607
1608 /*
1609 * Initialize ECC mode to NAND_ECC_SOFT so that we have a correct value
1610 * even if the nand-ecc-mode property is not defined.
1611 */
1612 host->nand_chip.ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckid5f304a2016-04-17 22:52:59 +02001613 host->nand_chip.ecc.algo = NAND_ECC_HAMMING;
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001614
1615 return 0;
1616}
1617
Johan Hovold2c2b9282013-09-23 16:27:28 +02001618static int atmel_hw_nand_init_params(struct platform_device *pdev,
Josh Wu3dfe41a2012-06-25 18:07:43 +08001619 struct atmel_nand_host *host)
1620{
Josh Wu3dfe41a2012-06-25 18:07:43 +08001621 struct nand_chip *nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01001622 struct mtd_info *mtd = nand_to_mtd(nand_chip);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001623 struct resource *regs;
1624
1625 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1626 if (!regs) {
1627 dev_err(host->dev,
1628 "Can't get I/O resource regs, use software ECC\n");
1629 nand_chip->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckid5f304a2016-04-17 22:52:59 +02001630 nand_chip->ecc.algo = NAND_ECC_HAMMING;
Josh Wu3dfe41a2012-06-25 18:07:43 +08001631 return 0;
1632 }
1633
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001634 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun8fb7b932014-07-28 21:19:55 +08001635 if (IS_ERR(host->ecc))
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001636 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001637
1638 /* ECC is calculated for the whole page (1 step) */
1639 nand_chip->ecc.size = mtd->writesize;
1640
1641 /* set ECC page size and oob layout */
1642 switch (mtd->writesize) {
1643 case 512:
Boris Brezilloncc003832016-02-03 19:59:11 +01001644 mtd_set_ooblayout(mtd, &atmel_ooblayout_sp_ops);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001645 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1646 break;
1647 case 1024:
Boris Brezilloncc003832016-02-03 19:59:11 +01001648 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001649 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1650 break;
1651 case 2048:
Boris Brezilloncc003832016-02-03 19:59:11 +01001652 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001653 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1654 break;
1655 case 4096:
Boris Brezilloncc003832016-02-03 19:59:11 +01001656 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001657 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1658 break;
1659 default:
1660 /* page size not handled by HW ECC */
1661 /* switching back to soft ECC */
1662 nand_chip->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckid5f304a2016-04-17 22:52:59 +02001663 nand_chip->ecc.algo = NAND_ECC_HAMMING;
Josh Wu3dfe41a2012-06-25 18:07:43 +08001664 return 0;
1665 }
1666
1667 /* set up for HW ECC */
1668 nand_chip->ecc.calculate = atmel_nand_calculate;
1669 nand_chip->ecc.correct = atmel_nand_correct;
1670 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1671 nand_chip->ecc.read_page = atmel_nand_read_page;
1672 nand_chip->ecc.bytes = 4;
1673 nand_chip->ecc.strength = 1;
1674
1675 return 0;
1676}
1677
Wu, Josh50e04e22014-06-10 17:50:09 +08001678static inline u32 nfc_read_status(struct atmel_nand_host *host)
1679{
1680 u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
1681 u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
1682
1683 if (unlikely(nfc_status & err_flags)) {
1684 if (nfc_status & NFC_SR_DTOE)
1685 dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
1686 else if (nfc_status & NFC_SR_UNDEF)
1687 dev_err(host->dev, "NFC: Access Undefined Area Error\n");
1688 else if (nfc_status & NFC_SR_AWB)
1689 dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
1690 else if (nfc_status & NFC_SR_ASE)
1691 dev_err(host->dev, "NFC: Access memory Size Error\n");
1692 }
1693
1694 return nfc_status;
1695}
1696
Josh Wu7dc37de2013-08-05 19:14:35 +08001697/* SMC interrupt service routine */
1698static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
1699{
1700 struct atmel_nand_host *host = dev_id;
1701 u32 status, mask, pending;
Josh Wue4e06932014-06-10 17:50:11 +08001702 irqreturn_t ret = IRQ_NONE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001703
Wu, Josh50e04e22014-06-10 17:50:09 +08001704 status = nfc_read_status(host);
Josh Wu7dc37de2013-08-05 19:14:35 +08001705 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1706 pending = status & mask;
1707
1708 if (pending & NFC_SR_XFR_DONE) {
Josh Wue4e06932014-06-10 17:50:11 +08001709 complete(&host->nfc->comp_xfer_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001710 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001711 ret = IRQ_HANDLED;
1712 }
Wenyou Yang53b74ed2016-05-09 14:51:18 +08001713 if (pending & NFC_SR_RB_EDGE) {
Josh Wue4e06932014-06-10 17:50:11 +08001714 complete(&host->nfc->comp_ready);
Wenyou Yang53b74ed2016-05-09 14:51:18 +08001715 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_RB_EDGE);
Josh Wue4e06932014-06-10 17:50:11 +08001716 ret = IRQ_HANDLED;
1717 }
1718 if (pending & NFC_SR_CMD_DONE) {
1719 complete(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001720 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001721 ret = IRQ_HANDLED;
Josh Wu7dc37de2013-08-05 19:14:35 +08001722 }
1723
1724 return ret;
1725}
1726
1727/* NFC(Nand Flash Controller) related functions */
Josh Wue4e06932014-06-10 17:50:11 +08001728static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag)
Josh Wu7dc37de2013-08-05 19:14:35 +08001729{
Josh Wue4e06932014-06-10 17:50:11 +08001730 if (flag & NFC_SR_XFR_DONE)
1731 init_completion(&host->nfc->comp_xfer_done);
1732
Wenyou Yang53b74ed2016-05-09 14:51:18 +08001733 if (flag & NFC_SR_RB_EDGE)
Josh Wue4e06932014-06-10 17:50:11 +08001734 init_completion(&host->nfc->comp_ready);
1735
1736 if (flag & NFC_SR_CMD_DONE)
1737 init_completion(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001738
1739 /* Enable interrupt that need to wait for */
1740 nfc_writel(host->nfc->hsmc_regs, IER, flag);
Josh Wue4e06932014-06-10 17:50:11 +08001741}
Josh Wu7dc37de2013-08-05 19:14:35 +08001742
Josh Wue4e06932014-06-10 17:50:11 +08001743static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
1744{
1745 int i, index = 0;
1746 struct completion *comp[3]; /* Support 3 interrupt completion */
Josh Wu7dc37de2013-08-05 19:14:35 +08001747
Josh Wue4e06932014-06-10 17:50:11 +08001748 if (flag & NFC_SR_XFR_DONE)
1749 comp[index++] = &host->nfc->comp_xfer_done;
1750
Wenyou Yang53b74ed2016-05-09 14:51:18 +08001751 if (flag & NFC_SR_RB_EDGE)
Josh Wue4e06932014-06-10 17:50:11 +08001752 comp[index++] = &host->nfc->comp_ready;
1753
1754 if (flag & NFC_SR_CMD_DONE)
1755 comp[index++] = &host->nfc->comp_cmd_done;
1756
1757 if (index == 0) {
Colin Ian King393d23c2015-02-28 20:27:56 +00001758 dev_err(host->dev, "Unknown interrupt flag: 0x%08x\n", flag);
Josh Wue4e06932014-06-10 17:50:11 +08001759 return -EINVAL;
1760 }
1761
1762 for (i = 0; i < index; i++) {
1763 if (wait_for_completion_timeout(comp[i],
1764 msecs_to_jiffies(NFC_TIME_OUT_MS)))
1765 continue; /* wait for next completion */
1766 else
1767 goto err_timeout;
1768 }
1769
1770 return 0;
1771
1772err_timeout:
Josh Wu7dc37de2013-08-05 19:14:35 +08001773 dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
Josh Wue4e06932014-06-10 17:50:11 +08001774 /* Disable the interrupt as it is not handled by interrupt handler */
1775 nfc_writel(host->nfc->hsmc_regs, IDR, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001776 return -ETIMEDOUT;
1777}
1778
1779static int nfc_send_command(struct atmel_nand_host *host,
1780 unsigned int cmd, unsigned int addr, unsigned char cycle0)
1781{
1782 unsigned long timeout;
Josh Wue4e06932014-06-10 17:50:11 +08001783 u32 flag = NFC_SR_CMD_DONE;
1784 flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0;
1785
Josh Wu7dc37de2013-08-05 19:14:35 +08001786 dev_dbg(host->dev,
1787 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1788 cmd, addr, cycle0);
1789
1790 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
Boris Brezillon111573cc2015-01-22 18:21:30 +08001791 while (nfc_readl(host->nfc->hsmc_regs, SR) & NFC_SR_BUSY) {
Josh Wu7dc37de2013-08-05 19:14:35 +08001792 if (time_after(jiffies, timeout)) {
1793 dev_err(host->dev,
Boris Brezillon111573cc2015-01-22 18:21:30 +08001794 "Time out to wait for NFC ready!\n");
Josh Wu7dc37de2013-08-05 19:14:35 +08001795 return -ETIMEDOUT;
1796 }
1797 }
Josh Wue4e06932014-06-10 17:50:11 +08001798
1799 nfc_prepare_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001800 nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
1801 nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
Josh Wue4e06932014-06-10 17:50:11 +08001802 return nfc_wait_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001803}
1804
1805static int nfc_device_ready(struct mtd_info *mtd)
1806{
Wu, Josh72a78e32014-06-10 17:50:10 +08001807 u32 status, mask;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001808 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001809 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Wu, Josh72a78e32014-06-10 17:50:10 +08001810
1811 status = nfc_read_status(host);
1812 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1813
1814 /* The mask should be 0. If not we may lost interrupts */
1815 if (unlikely(mask & status))
1816 dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n",
1817 mask & status);
1818
Wenyou Yang53b74ed2016-05-09 14:51:18 +08001819 return status & NFC_SR_RB_EDGE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001820}
1821
1822static void nfc_select_chip(struct mtd_info *mtd, int chip)
1823{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001824 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001825 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu7dc37de2013-08-05 19:14:35 +08001826
1827 if (chip == -1)
1828 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
1829 else
1830 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
1831}
1832
Brian Norris3dad2342014-01-29 14:08:12 -08001833static int nfc_make_addr(struct mtd_info *mtd, int command, int column,
1834 int page_addr, unsigned int *addr1234, unsigned int *cycle0)
Josh Wu7dc37de2013-08-05 19:14:35 +08001835{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001836 struct nand_chip *chip = mtd_to_nand(mtd);
Josh Wu7dc37de2013-08-05 19:14:35 +08001837
1838 int acycle = 0;
1839 unsigned char addr_bytes[8];
1840 int index = 0, bit_shift;
1841
1842 BUG_ON(addr1234 == NULL || cycle0 == NULL);
1843
1844 *cycle0 = 0;
1845 *addr1234 = 0;
1846
1847 if (column != -1) {
Brian Norris3dad2342014-01-29 14:08:12 -08001848 if (chip->options & NAND_BUSWIDTH_16 &&
1849 !nand_opcode_8bits(command))
Josh Wu7dc37de2013-08-05 19:14:35 +08001850 column >>= 1;
1851 addr_bytes[acycle++] = column & 0xff;
1852 if (mtd->writesize > 512)
1853 addr_bytes[acycle++] = (column >> 8) & 0xff;
1854 }
1855
1856 if (page_addr != -1) {
1857 addr_bytes[acycle++] = page_addr & 0xff;
1858 addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
1859 if (chip->chipsize > (128 << 20))
1860 addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
1861 }
1862
1863 if (acycle > 4)
1864 *cycle0 = addr_bytes[index++];
1865
1866 for (bit_shift = 0; index < acycle; bit_shift += 8)
1867 *addr1234 += addr_bytes[index++] << bit_shift;
1868
1869 /* return acycle in cmd register */
1870 return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
1871}
1872
1873static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
1874 int column, int page_addr)
1875{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001876 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001877 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu7dc37de2013-08-05 19:14:35 +08001878 unsigned long timeout;
1879 unsigned int nfc_addr_cmd = 0;
1880
1881 unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1882
1883 /* Set default settings: no cmd2, no addr cycle. read from nand */
1884 unsigned int cmd2 = 0;
1885 unsigned int vcmd2 = 0;
1886 int acycle = NFCADDR_CMD_ACYCLE_NONE;
1887 int csid = NFCADDR_CMD_CSID_3;
1888 int dataen = NFCADDR_CMD_DATADIS;
1889 int nfcwr = NFCADDR_CMD_NFCRD;
1890 unsigned int addr1234 = 0;
1891 unsigned int cycle0 = 0;
1892 bool do_addr = true;
Josh Wu1ae9c092013-08-05 19:14:36 +08001893 host->nfc->data_in_sram = NULL;
Josh Wu7dc37de2013-08-05 19:14:35 +08001894
1895 dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1896 __func__, command, column, page_addr);
1897
1898 switch (command) {
1899 case NAND_CMD_RESET:
1900 nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
1901 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1902 udelay(chip->chip_delay);
1903
1904 nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
1905 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1906 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
1907 if (time_after(jiffies, timeout)) {
1908 dev_err(host->dev,
1909 "Time out to wait status ready!\n");
1910 break;
1911 }
1912 }
1913 return;
1914 case NAND_CMD_STATUS:
1915 do_addr = false;
1916 break;
1917 case NAND_CMD_PARAM:
1918 case NAND_CMD_READID:
1919 do_addr = false;
1920 acycle = NFCADDR_CMD_ACYCLE_1;
1921 if (column != -1)
1922 addr1234 = column;
1923 break;
1924 case NAND_CMD_RNDOUT:
1925 cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
1926 vcmd2 = NFCADDR_CMD_VCMD2;
1927 break;
1928 case NAND_CMD_READ0:
1929 case NAND_CMD_READOOB:
1930 if (command == NAND_CMD_READOOB) {
1931 column += mtd->writesize;
1932 command = NAND_CMD_READ0; /* only READ0 is valid */
1933 cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1934 }
Josh Wu1ae9c092013-08-05 19:14:36 +08001935 if (host->nfc->use_nfc_sram) {
1936 /* Enable Data transfer to sram */
1937 dataen = NFCADDR_CMD_DATAEN;
1938
1939 /* Need enable PMECC now, since NFC will transfer
1940 * data in bus after sending nfc read command.
1941 */
1942 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1943 pmecc_enable(host, NAND_ECC_READ);
1944 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001945
1946 cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
1947 vcmd2 = NFCADDR_CMD_VCMD2;
1948 break;
1949 /* For prgramming command, the cmd need set to write enable */
1950 case NAND_CMD_PAGEPROG:
1951 case NAND_CMD_SEQIN:
1952 case NAND_CMD_RNDIN:
1953 nfcwr = NFCADDR_CMD_NFCWR;
Josh Wu6054d4d2013-08-05 19:14:37 +08001954 if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
1955 dataen = NFCADDR_CMD_DATAEN;
Josh Wu7dc37de2013-08-05 19:14:35 +08001956 break;
1957 default:
1958 break;
1959 }
1960
1961 if (do_addr)
Brian Norris3dad2342014-01-29 14:08:12 -08001962 acycle = nfc_make_addr(mtd, command, column, page_addr,
1963 &addr1234, &cycle0);
Josh Wu7dc37de2013-08-05 19:14:35 +08001964
1965 nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
1966 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1967
1968 /*
1969 * Program and erase have their own busy handlers status, sequential
1970 * in, and deplete1 need no delay.
1971 */
1972 switch (command) {
1973 case NAND_CMD_CACHEDPROG:
1974 case NAND_CMD_PAGEPROG:
1975 case NAND_CMD_ERASE1:
1976 case NAND_CMD_ERASE2:
1977 case NAND_CMD_RNDIN:
1978 case NAND_CMD_STATUS:
1979 case NAND_CMD_RNDOUT:
1980 case NAND_CMD_SEQIN:
1981 case NAND_CMD_READID:
1982 return;
1983
1984 case NAND_CMD_READ0:
Josh Wu1ae9c092013-08-05 19:14:36 +08001985 if (dataen == NFCADDR_CMD_DATAEN) {
1986 host->nfc->data_in_sram = host->nfc->sram_bank0 +
1987 nfc_get_sram_off(host);
1988 return;
1989 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001990 /* fall through */
1991 default:
Wenyou Yang53b74ed2016-05-09 14:51:18 +08001992 nfc_prepare_interrupt(host, NFC_SR_RB_EDGE);
1993 nfc_wait_interrupt(host, NFC_SR_RB_EDGE);
Josh Wu7dc37de2013-08-05 19:14:35 +08001994 }
1995}
1996
Josh Wu6054d4d2013-08-05 19:14:37 +08001997static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1998 uint32_t offset, int data_len, const uint8_t *buf,
1999 int oob_required, int page, int cached, int raw)
2000{
2001 int cfg, len;
2002 int status = 0;
Boris BREZILLONd699ed22015-12-10 09:00:41 +01002003 struct atmel_nand_host *host = nand_get_controller_data(chip);
Wu, Josh068b44b2014-11-07 15:26:09 +08002004 void *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
Josh Wu6054d4d2013-08-05 19:14:37 +08002005
2006 /* Subpage write is not supported */
2007 if (offset || (data_len < mtd->writesize))
2008 return -EINVAL;
2009
Josh Wu6054d4d2013-08-05 19:14:37 +08002010 len = mtd->writesize;
Josh Wu6054d4d2013-08-05 19:14:37 +08002011 /* Copy page data to sram that will write to nand via NFC */
2012 if (use_dma) {
2013 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
2014 /* Fall back to use cpu copy */
Wu, Josh068b44b2014-11-07 15:26:09 +08002015 memcpy(sram, buf, len);
Josh Wu6054d4d2013-08-05 19:14:37 +08002016 } else {
Wu, Josh068b44b2014-11-07 15:26:09 +08002017 memcpy(sram, buf, len);
Josh Wu6054d4d2013-08-05 19:14:37 +08002018 }
2019
Wu, Joshff0a2152014-08-05 18:38:52 +08002020 cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
2021 if (unlikely(raw) && oob_required) {
Wu, Josh068b44b2014-11-07 15:26:09 +08002022 memcpy(sram + len, chip->oob_poi, mtd->oobsize);
Wu, Joshff0a2152014-08-05 18:38:52 +08002023 len += mtd->oobsize;
2024 nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
2025 } else {
2026 nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
2027 }
2028
Josh Wu6054d4d2013-08-05 19:14:37 +08002029 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
2030 /*
2031 * When use NFC sram, need set up PMECC before send
2032 * NAND_CMD_SEQIN command. Since when the nand command
2033 * is sent, nfc will do transfer from sram and nand.
2034 */
2035 pmecc_enable(host, NAND_ECC_WRITE);
2036
2037 host->nfc->will_write_sram = true;
2038 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2039 host->nfc->will_write_sram = false;
2040
2041 if (likely(!raw))
2042 /* Need to write ecc into oob */
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002043 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2044 page);
Josh Wu6054d4d2013-08-05 19:14:37 +08002045
2046 if (status < 0)
2047 return status;
2048
2049 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2050 status = chip->waitfunc(mtd, chip);
2051
2052 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2053 status = chip->errstat(mtd, chip, FL_WRITING, status, page);
2054
2055 if (status & NAND_STATUS_FAIL)
2056 return -EIO;
2057
2058 return 0;
2059}
2060
Josh Wu1ae9c092013-08-05 19:14:36 +08002061static int nfc_sram_init(struct mtd_info *mtd)
2062{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01002063 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01002064 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu1ae9c092013-08-05 19:14:36 +08002065 int res = 0;
2066
2067 /* Initialize the NFC CFG register */
2068 unsigned int cfg_nfc = 0;
2069
2070 /* set page size and oob layout */
2071 switch (mtd->writesize) {
2072 case 512:
2073 cfg_nfc = NFC_CFG_PAGESIZE_512;
2074 break;
2075 case 1024:
2076 cfg_nfc = NFC_CFG_PAGESIZE_1024;
2077 break;
2078 case 2048:
2079 cfg_nfc = NFC_CFG_PAGESIZE_2048;
2080 break;
2081 case 4096:
2082 cfg_nfc = NFC_CFG_PAGESIZE_4096;
2083 break;
2084 case 8192:
2085 cfg_nfc = NFC_CFG_PAGESIZE_8192;
2086 break;
2087 default:
2088 dev_err(host->dev, "Unsupported page size for NFC.\n");
2089 res = -ENXIO;
2090 return res;
2091 }
2092
2093 /* oob bytes size = (NFCSPARESIZE + 1) * 4
2094 * Max support spare size is 512 bytes. */
2095 cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
2096 & NFC_CFG_NFC_SPARESIZE);
2097 /* default set a max timeout */
2098 cfg_nfc |= NFC_CFG_RSPARE |
2099 NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
2100
2101 nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
2102
Josh Wu6054d4d2013-08-05 19:14:37 +08002103 host->nfc->will_write_sram = false;
Josh Wu1ae9c092013-08-05 19:14:36 +08002104 nfc_set_sram_bank(host, 0);
2105
Josh Wu6054d4d2013-08-05 19:14:37 +08002106 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
2107 if (host->nfc->write_by_sram) {
2108 if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
2109 chip->ecc.mode == NAND_ECC_NONE)
2110 chip->write_page = nfc_sram_write_page;
2111 else
2112 host->nfc->write_by_sram = false;
2113 }
Josh Wu1ae9c092013-08-05 19:14:36 +08002114
Josh Wu6054d4d2013-08-05 19:14:37 +08002115 dev_info(host->dev, "Using NFC Sram read %s\n",
2116 host->nfc->write_by_sram ? "and write" : "");
Josh Wu1ae9c092013-08-05 19:14:36 +08002117 return 0;
2118}
2119
Josh Wu7dc37de2013-08-05 19:14:35 +08002120static struct platform_driver atmel_nand_nfc_driver;
Andrew Victor42cb1402006-10-19 18:24:35 +02002121/*
2122 * Probe for the NAND device.
2123 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002124static int atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002125{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002126 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02002127 struct mtd_info *mtd;
2128 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002129 struct resource *mem;
Josh Wu7dc37de2013-08-05 19:14:35 +08002130 int res, irq;
Andrew Victor42cb1402006-10-19 18:24:35 +02002131
2132 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002133 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Han9e3677a2013-12-26 12:00:16 +09002134 if (!host)
Andrew Victor42cb1402006-10-19 18:24:35 +02002135 return -ENOMEM;
Andrew Victor42cb1402006-10-19 18:24:35 +02002136
Josh Wu7dc37de2013-08-05 19:14:35 +08002137 res = platform_driver_register(&atmel_nand_nfc_driver);
2138 if (res)
2139 dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
2140
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002141 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2142 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
2143 if (IS_ERR(host->io_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002144 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002145 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02002146 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002147 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02002148
Andrew Victor42cb1402006-10-19 18:24:35 +02002149 nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01002150 mtd = nand_to_mtd(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02002151 host->dev = &pdev->dev;
Josh Wue9d8da82013-09-18 11:31:19 +08002152 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
Brian Norrisa61ae812015-10-30 20:33:25 -07002153 nand_set_flash_node(nand_chip, pdev->dev.of_node);
Josh Wue9d8da82013-09-18 11:31:19 +08002154 /* Only when CONFIG_OF is enabled of_node can be parsed */
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002155 res = atmel_of_init_port(host, pdev->dev.of_node);
2156 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002157 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002158 } else {
Jingoo Han453810b2013-07-30 17:18:33 +09002159 memcpy(&host->board, dev_get_platdata(&pdev->dev),
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002160 sizeof(struct atmel_nand_data));
Boris Brezillon4f3cab92016-04-01 14:54:22 +02002161 nand_chip->ecc.mode = host->board.ecc_mode;
2162
Rafał Miłeckid5f304a2016-04-17 22:52:59 +02002163 /*
2164 * When using software ECC every supported avr32 board means
2165 * Hamming algorithm. If that ever changes we'll need to add
2166 * ecc_algo field to the struct atmel_nand_data.
2167 */
2168 if (nand_chip->ecc.mode == NAND_ECC_SOFT)
2169 nand_chip->ecc.algo = NAND_ECC_HAMMING;
2170
Boris Brezillon4f3cab92016-04-01 14:54:22 +02002171 /* 16-bit bus width */
2172 if (host->board.bus_width_16)
2173 nand_chip->options |= NAND_BUSWIDTH_16;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002174 }
Andrew Victor42cb1402006-10-19 18:24:35 +02002175
Boris BREZILLONd699ed22015-12-10 09:00:41 +01002176 /* link the private data structures */
2177 nand_set_controller_data(nand_chip, host);
Frans Klaver03c287d2015-06-10 22:38:36 +02002178 mtd->dev.parent = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +02002179
2180 /* Set address of NAND IO lines */
2181 nand_chip->IO_ADDR_R = host->io_base;
2182 nand_chip->IO_ADDR_W = host->io_base;
Ivan Kutena4265f82007-05-24 14:35:58 +03002183
Josh Wu7dc37de2013-08-05 19:14:35 +08002184 if (nand_nfc.is_initialized) {
2185 /* NFC driver is probed and initialized */
2186 host->nfc = &nand_nfc;
2187
2188 nand_chip->select_chip = nfc_select_chip;
2189 nand_chip->dev_ready = nfc_device_ready;
2190 nand_chip->cmdfunc = nfc_nand_command;
2191
2192 /* Initialize the interrupt for NFC */
2193 irq = platform_get_irq(pdev, 0);
2194 if (irq < 0) {
2195 dev_err(host->dev, "Cannot get HSMC irq!\n");
Wei Yongjunff52c672013-08-23 10:50:36 +08002196 res = irq;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002197 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002198 }
2199
Josh Wu7dc37de2013-08-05 19:14:35 +08002200 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
2201 0, "hsmc", host);
2202 if (res) {
2203 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
2204 irq);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002205 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002206 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002207 } else {
2208 res = atmel_nand_set_enable_ready_pins(mtd);
2209 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002210 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002211
Josh Wu7dc37de2013-08-05 19:14:35 +08002212 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002213 }
Ivan Kutena4265f82007-05-24 14:35:58 +03002214
Raphaël Poggi796fe362014-07-29 15:27:27 +02002215 nand_chip->chip_delay = 40; /* 40us command delay time */
Andrew Victor42cb1402006-10-19 18:24:35 +02002216
Hong Xucbc6c5e2011-01-18 14:36:05 +08002217
2218 nand_chip->read_buf = atmel_read_buf;
2219 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02002220
Andrew Victor42cb1402006-10-19 18:24:35 +02002221 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002222 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002223
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002224 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002225 res = devm_gpio_request(&pdev->dev,
2226 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002227 if (res < 0) {
2228 dev_err(&pdev->dev,
2229 "can't request det gpio %d\n",
2230 host->board.det_pin);
2231 goto err_no_card;
2232 }
2233
2234 res = gpio_direction_input(host->board.det_pin);
2235 if (res < 0) {
2236 dev_err(&pdev->dev,
2237 "can't request input direction det gpio %d\n",
2238 host->board.det_pin);
2239 goto err_no_card;
2240 }
2241
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002242 if (gpio_get_value(host->board.det_pin)) {
Jingoo Han1295f972013-12-26 12:30:58 +09002243 dev_info(&pdev->dev, "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01002244 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002245 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02002246 }
2247 }
2248
Josh Wu1b719262013-05-09 15:34:55 +08002249 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08002250 use_dma = 0;
2251
2252 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08002253 dma_cap_mask_t mask;
2254
2255 dma_cap_zero(mask);
2256 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02002257 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002258 if (!host->dma_chan) {
2259 dev_err(host->dev, "Failed to request DMA channel\n");
2260 use_dma = 0;
2261 }
2262 }
2263 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08002264 dev_info(host->dev, "Using %s for DMA transfers.\n",
2265 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08002266 else
2267 dev_info(host->dev, "No DMA support for NAND access.\n");
2268
Richard Genoud77f54922008-04-23 19:51:14 +02002269 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00002270 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02002271 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002272 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02002273 }
2274
Boris Brezillon4f3cab92016-04-01 14:54:22 +02002275 if (host->board.on_flash_bbt || on_flash_bbt)
2276 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
2277
2278 if (nand_chip->bbt_options & NAND_BBT_USE_FLASH)
2279 dev_info(&pdev->dev, "Use On Flash BBT\n");
2280
2281 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
2282 res = atmel_of_init_ecc(host, pdev->dev.of_node);
2283 if (res)
2284 goto err_hw_ecc;
2285 }
2286
Richard Genoud3fc23892008-10-12 08:42:28 +02002287 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08002288 if (host->has_pmecc)
2289 res = atmel_pmecc_nand_init_params(pdev, host);
2290 else
2291 res = atmel_hw_nand_init_params(pdev, host);
2292
Josh Wu3dfe41a2012-06-25 18:07:43 +08002293 if (res != 0)
2294 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02002295 }
2296
Josh Wu1ae9c092013-08-05 19:14:36 +08002297 /* initialize the nfc configuration register */
2298 if (host->nfc && host->nfc->use_nfc_sram) {
2299 res = nfc_sram_init(mtd);
2300 if (res) {
2301 host->nfc->use_nfc_sram = false;
2302 dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
2303 }
2304 }
2305
Richard Genoud77f54922008-04-23 19:51:14 +02002306 /* second phase scan */
2307 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02002308 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002309 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02002310 }
2311
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002312 mtd->name = "atmel_nand";
Brian Norrisa61ae812015-10-30 20:33:25 -07002313 res = mtd_device_register(mtd, host->board.parts,
2314 host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02002315 if (!res)
2316 return res;
2317
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002318err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002319 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08002320 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08002321err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002322err_scan_ident:
2323err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002324 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002325 if (host->dma_chan)
2326 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002327err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02002328 return res;
2329}
2330
2331/*
2332 * Remove a NAND device.
2333 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002334static int atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002335{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002336 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Boris BREZILLONac01efe2015-12-10 08:59:50 +01002337 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +02002338
2339 nand_release(mtd);
2340
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002341 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002342
Josh Wu1c7b8742012-06-29 17:47:55 +08002343 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
2344 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2345 pmerrloc_writel(host->pmerrloc_base, ELDIS,
2346 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08002347 }
2348
Hong Xucbc6c5e2011-01-18 14:36:05 +08002349 if (host->dma_chan)
2350 dma_release_channel(host->dma_chan);
2351
Josh Wu7dc37de2013-08-05 19:14:35 +08002352 platform_driver_unregister(&atmel_nand_nfc_driver);
2353
Andrew Victor42cb1402006-10-19 18:24:35 +02002354 return 0;
2355}
2356
Romain Izard55750752016-02-10 10:56:25 +01002357/*
2358 * AT91RM9200 does not have PMECC or PMECC Errloc peripherals for
2359 * BCH ECC. Combined with the "atmel,has-pmecc", it is used to describe
2360 * devices from the SAM9 family that have those.
2361 */
LABBE Corentin72eaec22015-11-20 08:45:16 +01002362static const struct atmel_nand_caps at91rm9200_caps = {
Wu, Josh51585772015-01-19 16:33:06 +08002363 .pmecc_correct_erase_page = false,
Romain Izard55750752016-02-10 10:56:25 +01002364 .pmecc_max_correction = 24,
Wu, Josh51585772015-01-19 16:33:06 +08002365};
2366
LABBE Corentin72eaec22015-11-20 08:45:16 +01002367static const struct atmel_nand_caps sama5d4_caps = {
Wu, Josh51585772015-01-19 16:33:06 +08002368 .pmecc_correct_erase_page = true,
Romain Izard55750752016-02-10 10:56:25 +01002369 .pmecc_max_correction = 24,
2370};
2371
2372/*
2373 * The PMECC Errloc controller starting in SAMA5D2 is not compatible,
2374 * as the increased correction strength requires more registers.
2375 */
2376static const struct atmel_nand_caps sama5d2_caps = {
2377 .pmecc_correct_erase_page = true,
2378 .pmecc_max_correction = 32,
Wu, Josh51585772015-01-19 16:33:06 +08002379};
2380
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002381static const struct of_device_id atmel_nand_dt_ids[] = {
Wu, Josh51585772015-01-19 16:33:06 +08002382 { .compatible = "atmel,at91rm9200-nand", .data = &at91rm9200_caps },
2383 { .compatible = "atmel,sama5d4-nand", .data = &sama5d4_caps },
Romain Izard55750752016-02-10 10:56:25 +01002384 { .compatible = "atmel,sama5d2-nand", .data = &sama5d2_caps },
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002385 { /* sentinel */ }
2386};
2387
2388MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002389
Josh Wu7dc37de2013-08-05 19:14:35 +08002390static int atmel_nand_nfc_probe(struct platform_device *pdev)
2391{
2392 struct atmel_nfc *nfc = &nand_nfc;
2393 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002394 int ret;
Josh Wu7dc37de2013-08-05 19:14:35 +08002395
2396 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2397 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
2398 if (IS_ERR(nfc->base_cmd_regs))
2399 return PTR_ERR(nfc->base_cmd_regs);
2400
2401 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2402 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
2403 if (IS_ERR(nfc->hsmc_regs))
2404 return PTR_ERR(nfc->hsmc_regs);
2405
2406 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2407 if (nfc_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +08002408 nfc->sram_bank0 = (void * __force)
2409 devm_ioremap_resource(&pdev->dev, nfc_sram);
Josh Wu1ae9c092013-08-05 19:14:36 +08002410 if (IS_ERR(nfc->sram_bank0)) {
Josh Wu7dc37de2013-08-05 19:14:35 +08002411 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2412 PTR_ERR(nfc->sram_bank0));
Josh Wu1ae9c092013-08-05 19:14:36 +08002413 } else {
2414 nfc->use_nfc_sram = true;
Josh Wu7dc37de2013-08-05 19:14:35 +08002415 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
Josh Wu6054d4d2013-08-05 19:14:37 +08002416
2417 if (pdev->dev.of_node)
2418 nfc->write_by_sram = of_property_read_bool(
2419 pdev->dev.of_node,
2420 "atmel,write-by-sram");
Josh Wu1ae9c092013-08-05 19:14:36 +08002421 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002422 }
2423
Wu, Josh50e04e22014-06-10 17:50:09 +08002424 nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
2425 nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */
2426
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002427 nfc->clk = devm_clk_get(&pdev->dev, NULL);
2428 if (!IS_ERR(nfc->clk)) {
2429 ret = clk_prepare_enable(nfc->clk);
2430 if (ret)
2431 return ret;
2432 } else {
2433 dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree");
2434 }
2435
Josh Wu7dc37de2013-08-05 19:14:35 +08002436 nfc->is_initialized = true;
2437 dev_info(&pdev->dev, "NFC is probed.\n");
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002438
2439 return 0;
2440}
2441
2442static int atmel_nand_nfc_remove(struct platform_device *pdev)
2443{
2444 struct atmel_nfc *nfc = &nand_nfc;
2445
2446 if (!IS_ERR(nfc->clk))
2447 clk_disable_unprepare(nfc->clk);
2448
Josh Wu7dc37de2013-08-05 19:14:35 +08002449 return 0;
2450}
2451
Josh Wu81f29b42013-09-18 11:31:20 +08002452static const struct of_device_id atmel_nand_nfc_match[] = {
Wenyou Yang53b74ed2016-05-09 14:51:18 +08002453 { .compatible = "atmel,sama5d3-nfc" },
Josh Wu7dc37de2013-08-05 19:14:35 +08002454 { /* sentinel */ }
2455};
Josh Wu81f29b42013-09-18 11:31:20 +08002456MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
Josh Wu7dc37de2013-08-05 19:14:35 +08002457
2458static struct platform_driver atmel_nand_nfc_driver = {
2459 .driver = {
2460 .name = "atmel_nand_nfc",
Josh Wu7dc37de2013-08-05 19:14:35 +08002461 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2462 },
2463 .probe = atmel_nand_nfc_probe,
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002464 .remove = atmel_nand_nfc_remove,
Josh Wu7dc37de2013-08-05 19:14:35 +08002465};
2466
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002467static struct platform_driver atmel_nand_driver = {
Johan Hovold2c2b9282013-09-23 16:27:28 +02002468 .probe = atmel_nand_probe,
2469 .remove = atmel_nand_remove,
Andrew Victor42cb1402006-10-19 18:24:35 +02002470 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002471 .name = "atmel_nand",
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002472 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02002473 },
2474};
2475
Johan Hovold2c2b9282013-09-23 16:27:28 +02002476module_platform_driver(atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02002477
2478MODULE_LICENSE("GPL");
2479MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02002480MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002481MODULE_ALIAS("platform:atmel_nand");