blob: eec8ca739cae8f686ae0b4a84be3e9e7da1b3b49 [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
Romain Izard5ddc7bd2016-02-10 10:56:23 +010070struct atmel_nand_nfc_caps {
71 uint32_t rb_mask;
72};
73
Boris Brezilloncc003832016-02-03 19:59:11 +010074/*
75 * oob layout for large page size
Richard Genoud77f54922008-04-23 19:51:14 +020076 * bad block info is on bytes 0 and 1
77 * the bytes have to be consecutives to avoid
78 * several NAND_CMD_RNDOUT during read
Boris Brezilloncc003832016-02-03 19:59:11 +010079 *
80 * oob layout for small page size
Richard Genoud77f54922008-04-23 19:51:14 +020081 * bad block info is on bytes 4 and 5
82 * the bytes have to be consecutives to avoid
83 * several NAND_CMD_RNDOUT during read
84 */
Boris Brezilloncc003832016-02-03 19:59:11 +010085static int atmel_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
86 struct mtd_oob_region *oobregion)
87{
88 if (section)
89 return -ERANGE;
90
91 oobregion->length = 4;
92 oobregion->offset = 0;
93
94 return 0;
95}
96
97static int atmel_ooblayout_free_sp(struct mtd_info *mtd, int section,
98 struct mtd_oob_region *oobregion)
99{
100 if (section)
101 return -ERANGE;
102
103 oobregion->offset = 6;
104 oobregion->length = mtd->oobsize - oobregion->offset;
105
106 return 0;
107}
108
109static const struct mtd_ooblayout_ops atmel_ooblayout_sp_ops = {
110 .ecc = atmel_ooblayout_ecc_sp,
111 .free = atmel_ooblayout_free_sp,
Richard Genoud77f54922008-04-23 19:51:14 +0200112};
113
Josh Wu7dc37de2013-08-05 19:14:35 +0800114struct atmel_nfc {
115 void __iomem *base_cmd_regs;
116 void __iomem *hsmc_regs;
Wu, Josh068b44b2014-11-07 15:26:09 +0800117 void *sram_bank0;
Josh Wu7dc37de2013-08-05 19:14:35 +0800118 dma_addr_t sram_bank0_phys;
Josh Wu1ae9c092013-08-05 19:14:36 +0800119 bool use_nfc_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +0800120 bool write_by_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +0800121
Boris BREZILLON2d405ec2014-09-13 01:23:59 +0200122 struct clk *clk;
123
Josh Wu7dc37de2013-08-05 19:14:35 +0800124 bool is_initialized;
Josh Wue4e06932014-06-10 17:50:11 +0800125 struct completion comp_ready;
126 struct completion comp_cmd_done;
127 struct completion comp_xfer_done;
Josh Wu1ae9c092013-08-05 19:14:36 +0800128
129 /* Point to the sram bank which include readed data via NFC */
Wu, Josh068b44b2014-11-07 15:26:09 +0800130 void *data_in_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +0800131 bool will_write_sram;
Romain Izard5ddc7bd2016-02-10 10:56:23 +0100132 const struct atmel_nand_nfc_caps *caps;
Josh Wu7dc37de2013-08-05 19:14:35 +0800133};
134static struct atmel_nfc nand_nfc;
135
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200136struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +0200137 struct nand_chip nand_chip;
Andrew Victor42cb1402006-10-19 18:24:35 +0200138 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800139 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800140 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +0200141 struct device *dev;
142 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800143
144 struct completion comp;
145 struct dma_chan *dma_chan;
Josh Wua41b51a2012-06-29 17:47:54 +0800146
Josh Wu7dc37de2013-08-05 19:14:35 +0800147 struct atmel_nfc *nfc;
148
LABBE Corentin72eaec22015-11-20 08:45:16 +0100149 const struct atmel_nand_caps *caps;
Josh Wua41b51a2012-06-29 17:47:54 +0800150 bool has_pmecc;
151 u8 pmecc_corr_cap;
152 u16 pmecc_sector_size;
Josh Wuabb1cd02014-10-11 18:01:50 +0800153 bool has_no_lookup_table;
Josh Wua41b51a2012-06-29 17:47:54 +0800154 u32 pmecc_lookup_table_offset;
Josh Wue66b4312013-01-23 20:47:11 +0800155 u32 pmecc_lookup_table_offset_512;
156 u32 pmecc_lookup_table_offset_1024;
Josh Wu1c7b8742012-06-29 17:47:55 +0800157
Josh Wu1c7b8742012-06-29 17:47:55 +0800158 int pmecc_degree; /* Degree of remainders */
159 int pmecc_cw_len; /* Length of codeword */
160
161 void __iomem *pmerrloc_base;
Romain Izard55750752016-02-10 10:56:25 +0100162 void __iomem *pmerrloc_el_base;
Josh Wu1c7b8742012-06-29 17:47:55 +0800163 void __iomem *pmecc_rom_base;
164
165 /* lookup table for alpha_to and index_of */
166 void __iomem *pmecc_alpha_to;
167 void __iomem *pmecc_index_of;
168
169 /* data for pmecc computation */
170 int16_t *pmecc_partial_syn;
171 int16_t *pmecc_si;
172 int16_t *pmecc_smu; /* Sigma table */
173 int16_t *pmecc_lmu; /* polynomal order */
174 int *pmecc_mu;
175 int *pmecc_dmu;
176 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200177};
178
179/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900180 * Enable NAND.
181 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200182static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900183{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800184 if (gpio_is_valid(host->board.enable_pin))
185 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900186}
187
188/*
189 * Disable NAND.
190 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200191static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900192{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800193 if (gpio_is_valid(host->board.enable_pin))
194 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900195}
196
197/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200198 * Hardware specific access to control-lines
199 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200200static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200201{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100202 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100203 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +0200204
Atsushi Nemoto81365082008-04-27 01:51:12 +0900205 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900206 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200207 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900208 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200209 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900210 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200211 if (cmd == NAND_CMD_NONE)
212 return;
213
214 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800215 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200216 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800217 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200218}
219
220/*
221 * Read the Device Ready pin.
222 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200223static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200224{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100225 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100226 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +0200227
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800228 return gpio_get_value(host->board.rdy_pin) ^
229 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200230}
231
Josh Wu7dc37de2013-08-05 19:14:35 +0800232/* Set up for hardware ready pin and enable pin. */
233static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
234{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100235 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100236 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu7dc37de2013-08-05 19:14:35 +0800237 int res = 0;
238
239 if (gpio_is_valid(host->board.rdy_pin)) {
240 res = devm_gpio_request(host->dev,
241 host->board.rdy_pin, "nand_rdy");
242 if (res < 0) {
243 dev_err(host->dev,
244 "can't request rdy gpio %d\n",
245 host->board.rdy_pin);
246 return res;
247 }
248
249 res = gpio_direction_input(host->board.rdy_pin);
250 if (res < 0) {
251 dev_err(host->dev,
252 "can't request input direction rdy gpio %d\n",
253 host->board.rdy_pin);
254 return res;
255 }
256
257 chip->dev_ready = atmel_nand_device_ready;
258 }
259
260 if (gpio_is_valid(host->board.enable_pin)) {
261 res = devm_gpio_request(host->dev,
262 host->board.enable_pin, "nand_enable");
263 if (res < 0) {
264 dev_err(host->dev,
265 "can't request enable gpio %d\n",
266 host->board.enable_pin);
267 return res;
268 }
269
270 res = gpio_direction_output(host->board.enable_pin, 1);
271 if (res < 0) {
272 dev_err(host->dev,
273 "can't request output direction enable gpio %d\n",
274 host->board.enable_pin);
275 return res;
276 }
277 }
278
279 return res;
280}
281
Artem Bityutskiy50082312012-02-02 13:54:25 +0200282/*
283 * Minimal-overhead PIO for data access.
284 */
285static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
286{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100287 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100288 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200289
Josh Wu1ae9c092013-08-05 19:14:36 +0800290 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +0800291 memcpy(buf, host->nfc->data_in_sram, len);
Josh Wu1ae9c092013-08-05 19:14:36 +0800292 host->nfc->data_in_sram += len;
293 } else {
294 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
295 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200296}
297
298static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
299{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100300 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100301 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200302
Josh Wu1ae9c092013-08-05 19:14:36 +0800303 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +0800304 memcpy(buf, host->nfc->data_in_sram, len);
Josh Wu1ae9c092013-08-05 19:14:36 +0800305 host->nfc->data_in_sram += len;
306 } else {
307 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
308 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200309}
310
311static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
312{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100313 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200314
315 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
316}
317
318static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
319{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100320 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200321
322 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
323}
324
Hong Xucbc6c5e2011-01-18 14:36:05 +0800325static void dma_complete_func(void *completion)
326{
327 complete(completion);
328}
329
Josh Wu1ae9c092013-08-05 19:14:36 +0800330static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
331{
332 /* NFC only has two banks. Must be 0 or 1 */
333 if (bank > 1)
334 return -EINVAL;
335
336 if (bank) {
Boris BREZILLONac01efe2015-12-10 08:59:50 +0100337 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
338
Josh Wu1ae9c092013-08-05 19:14:36 +0800339 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
Boris BREZILLONac01efe2015-12-10 08:59:50 +0100340 if (mtd->writesize > 2048)
Josh Wu1ae9c092013-08-05 19:14:36 +0800341 return -EINVAL;
342 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
343 } else {
344 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
345 }
346
347 return 0;
348}
349
350static uint nfc_get_sram_off(struct atmel_nand_host *host)
351{
352 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
353 return NFC_SRAM_BANK1_OFFSET;
354 else
355 return 0;
356}
357
358static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
359{
360 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
361 return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
362 else
363 return host->nfc->sram_bank0_phys;
364}
365
Hong Xucbc6c5e2011-01-18 14:36:05 +0800366static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
367 int is_read)
368{
369 struct dma_device *dma_dev;
370 enum dma_ctrl_flags flags;
371 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
372 struct dma_async_tx_descriptor *tx = NULL;
373 dma_cookie_t cookie;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100374 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100375 struct atmel_nand_host *host = nand_get_controller_data(chip);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800376 void *p = buf;
377 int err = -EIO;
378 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Josh Wu1ae9c092013-08-05 19:14:36 +0800379 struct atmel_nfc *nfc = host->nfc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800380
Hong Xu80b4f812011-03-31 18:33:15 +0800381 if (buf >= high_memory)
382 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800383
384 dma_dev = host->dma_chan->device;
385
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200386 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800387
388 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
389 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
390 dev_err(host->dev, "Failed to dma_map_single\n");
391 goto err_buf;
392 }
393
394 if (is_read) {
Josh Wu1ae9c092013-08-05 19:14:36 +0800395 if (nfc && nfc->data_in_sram)
396 dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
397 - (nfc->sram_bank0 + nfc_get_sram_off(host)));
398 else
399 dma_src_addr = host->io_phys;
400
Hong Xucbc6c5e2011-01-18 14:36:05 +0800401 dma_dst_addr = phys_addr;
402 } else {
403 dma_src_addr = phys_addr;
Josh Wu6054d4d2013-08-05 19:14:37 +0800404
405 if (nfc && nfc->write_by_sram)
406 dma_dst_addr = nfc_sram_phys(host);
407 else
408 dma_dst_addr = host->io_phys;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800409 }
410
411 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
412 dma_src_addr, len, flags);
413 if (!tx) {
414 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
415 goto err_dma;
416 }
417
418 init_completion(&host->comp);
419 tx->callback = dma_complete_func;
420 tx->callback_param = &host->comp;
421
422 cookie = tx->tx_submit(tx);
423 if (dma_submit_error(cookie)) {
424 dev_err(host->dev, "Failed to do DMA tx_submit\n");
425 goto err_dma;
426 }
427
428 dma_async_issue_pending(host->dma_chan);
429 wait_for_completion(&host->comp);
430
Josh Wu1ae9c092013-08-05 19:14:36 +0800431 if (is_read && nfc && nfc->data_in_sram)
432 /* After read data from SRAM, need to increase the position */
433 nfc->data_in_sram += len;
434
Hong Xucbc6c5e2011-01-18 14:36:05 +0800435 err = 0;
436
437err_dma:
438 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
439err_buf:
440 if (err != 0)
Nicolas Ferre74414a942014-02-12 12:26:54 +0100441 dev_dbg(host->dev, "Fall back to CPU I/O\n");
Hong Xucbc6c5e2011-01-18 14:36:05 +0800442 return err;
443}
444
445static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
446{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100447 struct nand_chip *chip = mtd_to_nand(mtd);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800448
Nicolas Ferre9d515672011-04-01 16:40:44 +0200449 if (use_dma && len > mtd->oobsize)
450 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800451 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
452 return;
453
Boris Brezillon4f3cab92016-04-01 14:54:22 +0200454 if (chip->options & NAND_BUSWIDTH_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200455 atmel_read_buf16(mtd, buf, len);
456 else
457 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800458}
459
460static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
461{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100462 struct nand_chip *chip = mtd_to_nand(mtd);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800463
Nicolas Ferre9d515672011-04-01 16:40:44 +0200464 if (use_dma && len > mtd->oobsize)
465 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800466 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
467 return;
468
Boris Brezillon4f3cab92016-04-01 14:54:22 +0200469 if (chip->options & NAND_BUSWIDTH_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200470 atmel_write_buf16(mtd, buf, len);
471 else
472 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800473}
474
David Brownell23a346c2008-07-03 23:40:16 -0700475/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800476 * Return number of ecc bytes per sector according to sector size and
477 * correction capability
478 *
479 * Following table shows what at91 PMECC supported:
480 * Correction Capability Sector_512_bytes Sector_1024_bytes
481 * ===================== ================ =================
482 * 2-bits 4-bytes 4-bytes
483 * 4-bits 7-bytes 7-bytes
484 * 8-bits 13-bytes 14-bytes
485 * 12-bits 20-bytes 21-bytes
486 * 24-bits 39-bytes 42-bytes
Romain Izard94248462016-02-10 10:56:26 +0100487 * 32-bits 52-bytes 56-bytes
Josh Wu1c7b8742012-06-29 17:47:55 +0800488 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500489static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800490{
491 int m = 12 + sector_size / 512;
492 return (m * cap + 7) / 8;
493}
494
Bill Pemberton06f25512012-11-19 13:23:07 -0500495static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800496{
497 int table_size;
498
499 table_size = host->pmecc_sector_size == 512 ?
500 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
501
502 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
503 table_size * sizeof(int16_t);
504}
505
Bill Pemberton06f25512012-11-19 13:23:07 -0500506static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800507{
508 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800509 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800510
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800511 size = (2 * cap + 1) * sizeof(int16_t);
512 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
513 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
514 host->pmecc_lmu = devm_kzalloc(host->dev,
515 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
516 host->pmecc_smu = devm_kzalloc(host->dev,
517 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800518
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800519 size = (cap + 1) * sizeof(int);
520 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
521 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
522 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800523
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800524 if (!host->pmecc_partial_syn ||
525 !host->pmecc_si ||
526 !host->pmecc_lmu ||
527 !host->pmecc_smu ||
528 !host->pmecc_mu ||
529 !host->pmecc_dmu ||
530 !host->pmecc_delta)
531 return -ENOMEM;
532
533 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800534}
535
536static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
537{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100538 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100539 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800540 int i;
541 uint32_t value;
542
543 /* Fill odd syndromes */
544 for (i = 0; i < host->pmecc_corr_cap; i++) {
545 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
546 if (i & 1)
547 value >>= 16;
548 value &= 0xffff;
549 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
550 }
551}
552
553static void pmecc_substitute(struct mtd_info *mtd)
554{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100555 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100556 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800557 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
558 int16_t __iomem *index_of = host->pmecc_index_of;
559 int16_t *partial_syn = host->pmecc_partial_syn;
560 const int cap = host->pmecc_corr_cap;
561 int16_t *si;
562 int i, j;
563
564 /* si[] is a table that holds the current syndrome value,
565 * an element of that table belongs to the field
566 */
567 si = host->pmecc_si;
568
569 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
570
571 /* Computation 2t syndromes based on S(x) */
572 /* Odd syndromes */
573 for (i = 1; i < 2 * cap; i += 2) {
574 for (j = 0; j < host->pmecc_degree; j++) {
575 if (partial_syn[i] & ((unsigned short)0x1 << j))
576 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
577 }
578 }
579 /* Even syndrome = (Odd syndrome) ** 2 */
580 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
581 if (si[j] == 0) {
582 si[i] = 0;
583 } else {
584 int16_t tmp;
585
586 tmp = readw_relaxed(index_of + si[j]);
587 tmp = (tmp * 2) % host->pmecc_cw_len;
588 si[i] = readw_relaxed(alpha_to + tmp);
589 }
590 }
591
592 return;
593}
594
595static void pmecc_get_sigma(struct mtd_info *mtd)
596{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100597 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100598 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800599
600 int16_t *lmu = host->pmecc_lmu;
601 int16_t *si = host->pmecc_si;
602 int *mu = host->pmecc_mu;
603 int *dmu = host->pmecc_dmu; /* Discrepancy */
604 int *delta = host->pmecc_delta; /* Delta order */
605 int cw_len = host->pmecc_cw_len;
606 const int16_t cap = host->pmecc_corr_cap;
607 const int num = 2 * cap + 1;
608 int16_t __iomem *index_of = host->pmecc_index_of;
609 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
610 int i, j, k;
611 uint32_t dmu_0_count, tmp;
612 int16_t *smu = host->pmecc_smu;
613
614 /* index of largest delta */
615 int ro;
616 int largest;
617 int diff;
618
619 dmu_0_count = 0;
620
621 /* First Row */
622
623 /* Mu */
624 mu[0] = -1;
625
626 memset(smu, 0, sizeof(int16_t) * num);
627 smu[0] = 1;
628
629 /* discrepancy set to 1 */
630 dmu[0] = 1;
631 /* polynom order set to 0 */
632 lmu[0] = 0;
633 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
634
635 /* Second Row */
636
637 /* Mu */
638 mu[1] = 0;
639 /* Sigma(x) set to 1 */
640 memset(&smu[num], 0, sizeof(int16_t) * num);
641 smu[num] = 1;
642
643 /* discrepancy set to S1 */
644 dmu[1] = si[1];
645
646 /* polynom order set to 0 */
647 lmu[1] = 0;
648
649 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
650
651 /* Init the Sigma(x) last row */
652 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
653
654 for (i = 1; i <= cap; i++) {
655 mu[i + 1] = i << 1;
656 /* Begin Computing Sigma (Mu+1) and L(mu) */
657 /* check if discrepancy is set to 0 */
658 if (dmu[i] == 0) {
659 dmu_0_count++;
660
661 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
662 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
663 tmp += 2;
664 else
665 tmp += 1;
666
667 if (dmu_0_count == tmp) {
668 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
669 smu[(cap + 1) * num + j] =
670 smu[i * num + j];
671
672 lmu[cap + 1] = lmu[i];
673 return;
674 }
675
676 /* copy polynom */
677 for (j = 0; j <= lmu[i] >> 1; j++)
678 smu[(i + 1) * num + j] = smu[i * num + j];
679
680 /* copy previous polynom order to the next */
681 lmu[i + 1] = lmu[i];
682 } else {
683 ro = 0;
684 largest = -1;
685 /* find largest delta with dmu != 0 */
686 for (j = 0; j < i; j++) {
687 if ((dmu[j]) && (delta[j] > largest)) {
688 largest = delta[j];
689 ro = j;
690 }
691 }
692
693 /* compute difference */
694 diff = (mu[i] - mu[ro]);
695
696 /* Compute degree of the new smu polynomial */
697 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
698 lmu[i + 1] = lmu[i];
699 else
700 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
701
702 /* Init smu[i+1] with 0 */
703 for (k = 0; k < num; k++)
704 smu[(i + 1) * num + k] = 0;
705
706 /* Compute smu[i+1] */
707 for (k = 0; k <= lmu[ro] >> 1; k++) {
708 int16_t a, b, c;
709
710 if (!(smu[ro * num + k] && dmu[i]))
711 continue;
712 a = readw_relaxed(index_of + dmu[i]);
713 b = readw_relaxed(index_of + dmu[ro]);
714 c = readw_relaxed(index_of + smu[ro * num + k]);
715 tmp = a + (cw_len - b) + c;
716 a = readw_relaxed(alpha_to + tmp % cw_len);
717 smu[(i + 1) * num + (k + diff)] = a;
718 }
719
720 for (k = 0; k <= lmu[i] >> 1; k++)
721 smu[(i + 1) * num + k] ^= smu[i * num + k];
722 }
723
724 /* End Computing Sigma (Mu+1) and L(mu) */
725 /* In either case compute delta */
726 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
727
728 /* Do not compute discrepancy for the last iteration */
729 if (i >= cap)
730 continue;
731
732 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
733 tmp = 2 * (i - 1);
734 if (k == 0) {
735 dmu[i + 1] = si[tmp + 3];
736 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
737 int16_t a, b, c;
738 a = readw_relaxed(index_of +
739 smu[(i + 1) * num + k]);
740 b = si[2 * (i - 1) + 3 - k];
741 c = readw_relaxed(index_of + b);
742 tmp = a + c;
743 tmp %= cw_len;
744 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
745 dmu[i + 1];
746 }
747 }
748 }
749
750 return;
751}
752
753static int pmecc_err_location(struct mtd_info *mtd)
754{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100755 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100756 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800757 unsigned long end_time;
758 const int cap = host->pmecc_corr_cap;
759 const int num = 2 * cap + 1;
760 int sector_size = host->pmecc_sector_size;
761 int err_nbr = 0; /* number of error */
762 int roots_nbr; /* number of roots */
763 int i;
764 uint32_t val;
765 int16_t *smu = host->pmecc_smu;
766
767 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
768
769 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
770 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
771 smu[(cap + 1) * num + i]);
772 err_nbr++;
773 }
774
775 val = (err_nbr - 1) << 16;
776 if (sector_size == 1024)
777 val |= 1;
778
779 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
780 pmerrloc_writel(host->pmerrloc_base, ELEN,
781 sector_size * 8 + host->pmecc_degree * cap);
782
783 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
784 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
785 & PMERRLOC_CALC_DONE)) {
786 if (unlikely(time_after(jiffies, end_time))) {
787 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
788 return -1;
789 }
790 cpu_relax();
791 }
792
793 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
794 & PMERRLOC_ERR_NUM_MASK) >> 8;
795 /* Number of roots == degree of smu hence <= cap */
796 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
797 return err_nbr - 1;
798
799 /* Number of roots does not match the degree of smu
800 * unable to correct error */
801 return -1;
802}
803
804static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
805 int sector_num, int extra_bytes, int err_nbr)
806{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100807 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100808 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800809 int i = 0;
810 int byte_pos, bit_pos, sector_size, pos;
811 uint32_t tmp;
812 uint8_t err_byte;
813
814 sector_size = host->pmecc_sector_size;
815
816 while (err_nbr) {
Romain Izard55750752016-02-10 10:56:25 +0100817 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_el_base, i) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +0800818 byte_pos = tmp / 8;
819 bit_pos = tmp % 8;
820
821 if (byte_pos >= (sector_size + extra_bytes))
822 BUG(); /* should never happen */
823
824 if (byte_pos < sector_size) {
825 err_byte = *(buf + byte_pos);
826 *(buf + byte_pos) ^= (1 << bit_pos);
827
828 pos = sector_num * host->pmecc_sector_size + byte_pos;
Romain Izard12197bf2016-01-13 17:34:13 +0100829 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 +0800830 pos, bit_pos, err_byte, *(buf + byte_pos));
831 } else {
Boris Brezillon78d28e82016-02-03 20:11:14 +0100832 struct mtd_oob_region oobregion;
833
Josh Wu1c7b8742012-06-29 17:47:55 +0800834 /* Bit flip in OOB area */
Wu, Josh022a4782014-08-08 17:12:35 +0800835 tmp = sector_num * nand_chip->ecc.bytes
Josh Wu1c7b8742012-06-29 17:47:55 +0800836 + (byte_pos - sector_size);
837 err_byte = ecc[tmp];
838 ecc[tmp] ^= (1 << bit_pos);
839
Boris Brezillon78d28e82016-02-03 20:11:14 +0100840 mtd_ooblayout_ecc(mtd, 0, &oobregion);
841 pos = tmp + oobregion.offset;
Romain Izard12197bf2016-01-13 17:34:13 +0100842 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 +0800843 pos, bit_pos, err_byte, ecc[tmp]);
844 }
845
846 i++;
847 err_nbr--;
848 }
849
850 return;
851}
852
853static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
854 u8 *ecc)
855{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100856 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100857 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Bo Shenb3857662014-06-12 15:58:45 +0800858 int i, err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800859 uint8_t *buf_pos;
Wu, Josh267d46e2015-01-14 11:50:46 +0800860 int max_bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800861
Wu, Joshc9447ff2014-08-08 17:12:34 +0800862 for (i = 0; i < nand_chip->ecc.steps; i++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800863 err_nbr = 0;
864 if (pmecc_stat & 0x1) {
865 buf_pos = buf + i * host->pmecc_sector_size;
866
867 pmecc_gen_syndrome(mtd, i);
868 pmecc_substitute(mtd);
869 pmecc_get_sigma(mtd);
870
871 err_nbr = pmecc_err_location(mtd);
Boris Brezillonff6ee102016-03-01 14:11:52 +0100872 if (err_nbr >= 0) {
873 pmecc_correct_data(mtd, buf_pos, ecc, i,
874 nand_chip->ecc.bytes,
875 err_nbr);
876 } else if (!host->caps->pmecc_correct_erase_page) {
877 u8 *ecc_pos = ecc + (i * nand_chip->ecc.bytes);
878
879 /* Try to detect erased pages */
880 err_nbr = nand_check_erased_ecc_chunk(buf_pos,
881 host->pmecc_sector_size,
882 ecc_pos,
883 nand_chip->ecc.bytes,
884 NULL, 0,
885 nand_chip->ecc.strength);
886 }
887
888 if (err_nbr < 0) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800889 dev_err(host->dev, "PMECC: Too many errors\n");
890 mtd->ecc_stats.failed++;
891 return -EIO;
Josh Wu1c7b8742012-06-29 17:47:55 +0800892 }
Boris Brezillonff6ee102016-03-01 14:11:52 +0100893
894 mtd->ecc_stats.corrected += err_nbr;
895 max_bitflips = max_t(int, max_bitflips, err_nbr);
Josh Wu1c7b8742012-06-29 17:47:55 +0800896 }
897 pmecc_stat >>= 1;
898 }
899
Wu, Josh267d46e2015-01-14 11:50:46 +0800900 return max_bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800901}
902
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800903static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
904{
905 u32 val;
906
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800907 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
908 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
909 return;
910 }
911
Josh Wu1fad0e82013-08-07 17:58:11 +0800912 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
913 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
914 val = pmecc_readl_relaxed(host->ecc, CFG);
915
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800916 if (ecc_op == NAND_ECC_READ)
917 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
918 | PMECC_CFG_AUTO_ENABLE);
919 else
920 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
921 & ~PMECC_CFG_AUTO_ENABLE);
922
923 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
924 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
925}
926
Josh Wu1c7b8742012-06-29 17:47:55 +0800927static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
928 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
929{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100930 struct atmel_nand_host *host = nand_get_controller_data(chip);
Bo Shenb3857662014-06-12 15:58:45 +0800931 int eccsize = chip->ecc.size * chip->ecc.steps;
Josh Wu1c7b8742012-06-29 17:47:55 +0800932 uint8_t *oob = chip->oob_poi;
Josh Wu1c7b8742012-06-29 17:47:55 +0800933 uint32_t stat;
934 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800935 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800936
Josh Wu1ae9c092013-08-05 19:14:36 +0800937 if (!host->nfc || !host->nfc->use_nfc_sram)
938 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800939
940 chip->read_buf(mtd, buf, eccsize);
941 chip->read_buf(mtd, oob, mtd->oobsize);
942
943 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
944 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
945 if (unlikely(time_after(jiffies, end_time))) {
946 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
947 return -EIO;
948 }
949 cpu_relax();
950 }
951
952 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800953 if (stat != 0) {
Boris Brezillon78d28e82016-02-03 20:11:14 +0100954 struct mtd_oob_region oobregion;
955
956 mtd_ooblayout_ecc(mtd, 0, &oobregion);
957 bitflips = pmecc_correction(mtd, stat, buf,
958 &oob[oobregion.offset]);
Josh Wuc0c70d92012-11-27 18:50:31 +0800959 if (bitflips < 0)
960 /* uncorrectable errors */
961 return 0;
962 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800963
Josh Wuc0c70d92012-11-27 18:50:31 +0800964 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800965}
966
967static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +0200968 struct nand_chip *chip, const uint8_t *buf, int oob_required,
969 int page)
Josh Wu1c7b8742012-06-29 17:47:55 +0800970{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100971 struct atmel_nand_host *host = nand_get_controller_data(chip);
Boris Brezillon78d28e82016-02-03 20:11:14 +0100972 struct mtd_oob_region oobregion = { };
973 int i, j, section = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800974 unsigned long end_time;
975
Josh Wu6054d4d2013-08-05 19:14:37 +0800976 if (!host->nfc || !host->nfc->write_by_sram) {
977 pmecc_enable(host, NAND_ECC_WRITE);
978 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
979 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800980
981 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
982 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
983 if (unlikely(time_after(jiffies, end_time))) {
984 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
985 return -EIO;
986 }
987 cpu_relax();
988 }
989
Wu, Joshc9447ff2014-08-08 17:12:34 +0800990 for (i = 0; i < chip->ecc.steps; i++) {
Wu, Josh022a4782014-08-08 17:12:35 +0800991 for (j = 0; j < chip->ecc.bytes; j++) {
Boris Brezillon78d28e82016-02-03 20:11:14 +0100992 if (!oobregion.length)
993 mtd_ooblayout_ecc(mtd, section, &oobregion);
Josh Wu1c7b8742012-06-29 17:47:55 +0800994
Boris Brezillon78d28e82016-02-03 20:11:14 +0100995 chip->oob_poi[oobregion.offset] =
Josh Wu1c7b8742012-06-29 17:47:55 +0800996 pmecc_readb_ecc_relaxed(host->ecc, i, j);
Boris Brezillon78d28e82016-02-03 20:11:14 +0100997 oobregion.length--;
998 oobregion.offset++;
999 section++;
Josh Wu1c7b8742012-06-29 17:47:55 +08001000 }
1001 }
1002 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1003
1004 return 0;
1005}
1006
1007static void atmel_pmecc_core_init(struct mtd_info *mtd)
1008{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001009 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001010 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Boris Brezilloncc003832016-02-03 19:59:11 +01001011 int eccbytes = mtd_ooblayout_count_eccbytes(mtd);
Josh Wu1c7b8742012-06-29 17:47:55 +08001012 uint32_t val = 0;
Boris Brezillon78d28e82016-02-03 20:11:14 +01001013 struct mtd_oob_region oobregion;
Josh Wu1c7b8742012-06-29 17:47:55 +08001014
1015 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
1016 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1017
1018 switch (host->pmecc_corr_cap) {
1019 case 2:
1020 val = PMECC_CFG_BCH_ERR2;
1021 break;
1022 case 4:
1023 val = PMECC_CFG_BCH_ERR4;
1024 break;
1025 case 8:
1026 val = PMECC_CFG_BCH_ERR8;
1027 break;
1028 case 12:
1029 val = PMECC_CFG_BCH_ERR12;
1030 break;
1031 case 24:
1032 val = PMECC_CFG_BCH_ERR24;
1033 break;
Romain Izard94248462016-02-10 10:56:26 +01001034 case 32:
1035 val = PMECC_CFG_BCH_ERR32;
1036 break;
Josh Wu1c7b8742012-06-29 17:47:55 +08001037 }
1038
1039 if (host->pmecc_sector_size == 512)
1040 val |= PMECC_CFG_SECTOR512;
1041 else if (host->pmecc_sector_size == 1024)
1042 val |= PMECC_CFG_SECTOR1024;
1043
Wu, Joshc9447ff2014-08-08 17:12:34 +08001044 switch (nand_chip->ecc.steps) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001045 case 1:
1046 val |= PMECC_CFG_PAGE_1SECTOR;
1047 break;
1048 case 2:
1049 val |= PMECC_CFG_PAGE_2SECTORS;
1050 break;
1051 case 4:
1052 val |= PMECC_CFG_PAGE_4SECTORS;
1053 break;
1054 case 8:
1055 val |= PMECC_CFG_PAGE_8SECTORS;
1056 break;
1057 }
1058
1059 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
1060 | PMECC_CFG_AUTO_DISABLE);
1061 pmecc_writel(host->ecc, CFG, val);
1062
Josh Wu1c7b8742012-06-29 17:47:55 +08001063 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
Boris Brezillon78d28e82016-02-03 20:11:14 +01001064 mtd_ooblayout_ecc(mtd, 0, &oobregion);
1065 pmecc_writel(host->ecc, SADDR, oobregion.offset);
Josh Wu1c7b8742012-06-29 17:47:55 +08001066 pmecc_writel(host->ecc, EADDR,
Boris Brezilloncc003832016-02-03 19:59:11 +01001067 oobregion.offset + eccbytes - 1);
Josh Wu1c7b8742012-06-29 17:47:55 +08001068 /* See datasheet about PMECC Clock Control Register */
1069 pmecc_writel(host->ecc, CLK, 2);
1070 pmecc_writel(host->ecc, IDR, 0xff);
1071 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
1072}
1073
Josh Wu84cfbbb2013-01-23 20:47:12 +08001074/*
Josh Wu2a3d9332013-09-18 13:58:48 +08001075 * Get minimum ecc requirements from NAND.
Josh Wu84cfbbb2013-01-23 20:47:12 +08001076 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
Josh Wu2a3d9332013-09-18 13:58:48 +08001077 * will set them according to minimum ecc requirement. Otherwise, use the
Josh Wu84cfbbb2013-01-23 20:47:12 +08001078 * value in DTS file.
1079 * return 0 if success. otherwise return error code.
1080 */
1081static int pmecc_choose_ecc(struct atmel_nand_host *host,
1082 int *cap, int *sector_size)
1083{
Josh Wu2a3d9332013-09-18 13:58:48 +08001084 /* Get minimum ECC requirements */
1085 if (host->nand_chip.ecc_strength_ds) {
1086 *cap = host->nand_chip.ecc_strength_ds;
1087 *sector_size = host->nand_chip.ecc_step_ds;
1088 dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n",
Josh Wu84cfbbb2013-01-23 20:47:12 +08001089 *cap, *sector_size);
Josh Wu84cfbbb2013-01-23 20:47:12 +08001090 } else {
Josh Wu84cfbbb2013-01-23 20:47:12 +08001091 *cap = 2;
1092 *sector_size = 512;
Josh Wu2a3d9332013-09-18 13:58:48 +08001093 dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
Josh Wu84cfbbb2013-01-23 20:47:12 +08001094 }
1095
Josh Wu2a3d9332013-09-18 13:58:48 +08001096 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
Josh Wu84cfbbb2013-01-23 20:47:12 +08001097 if (host->pmecc_corr_cap == 0) {
Romain Izard94248462016-02-10 10:56:26 +01001098 if (*cap > host->caps->pmecc_max_correction)
1099 return -EINVAL;
1100
Josh Wu84cfbbb2013-01-23 20:47:12 +08001101 /* use the most fitable ecc bits (the near bigger one ) */
1102 if (*cap <= 2)
1103 host->pmecc_corr_cap = 2;
1104 else if (*cap <= 4)
1105 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +08001106 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001107 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +08001108 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001109 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +08001110 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001111 host->pmecc_corr_cap = 24;
Romain Izard94248462016-02-10 10:56:26 +01001112 else if (*cap <= 32)
1113 host->pmecc_corr_cap = 32;
Josh Wu84cfbbb2013-01-23 20:47:12 +08001114 else
1115 return -EINVAL;
1116 }
1117 if (host->pmecc_sector_size == 0) {
1118 /* use the most fitable sector size (the near smaller one ) */
1119 if (*sector_size >= 1024)
1120 host->pmecc_sector_size = 1024;
1121 else if (*sector_size >= 512)
1122 host->pmecc_sector_size = 512;
1123 else
1124 return -EINVAL;
1125 }
1126 return 0;
1127}
1128
Josh Wuabb1cd02014-10-11 18:01:50 +08001129static inline int deg(unsigned int poly)
1130{
1131 /* polynomial degree is the most-significant bit index */
1132 return fls(poly) - 1;
1133}
1134
1135static int build_gf_tables(int mm, unsigned int poly,
1136 int16_t *index_of, int16_t *alpha_to)
1137{
1138 unsigned int i, x = 1;
1139 const unsigned int k = 1 << deg(poly);
1140 unsigned int nn = (1 << mm) - 1;
1141
1142 /* primitive polynomial must be of degree m */
1143 if (k != (1u << mm))
1144 return -EINVAL;
1145
1146 for (i = 0; i < nn; i++) {
1147 alpha_to[i] = x;
1148 index_of[x] = i;
1149 if (i && (x == 1))
1150 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
1151 return -EINVAL;
1152 x <<= 1;
1153 if (x & k)
1154 x ^= poly;
1155 }
1156 alpha_to[nn] = 1;
1157 index_of[0] = 0;
1158
1159 return 0;
1160}
1161
1162static uint16_t *create_lookup_table(struct device *dev, int sector_size)
1163{
1164 int degree = (sector_size == 512) ?
1165 PMECC_GF_DIMENSION_13 :
1166 PMECC_GF_DIMENSION_14;
1167 unsigned int poly = (sector_size == 512) ?
1168 PMECC_GF_13_PRIMITIVE_POLY :
1169 PMECC_GF_14_PRIMITIVE_POLY;
1170 int table_size = (sector_size == 512) ?
1171 PMECC_LOOKUP_TABLE_SIZE_512 :
1172 PMECC_LOOKUP_TABLE_SIZE_1024;
1173
1174 int16_t *addr = devm_kzalloc(dev, 2 * table_size * sizeof(uint16_t),
1175 GFP_KERNEL);
1176 if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
1177 return NULL;
1178
1179 return addr;
1180}
1181
Johan Hovold2c2b9282013-09-23 16:27:28 +02001182static int atmel_pmecc_nand_init_params(struct platform_device *pdev,
Josh Wu1c7b8742012-06-29 17:47:55 +08001183 struct atmel_nand_host *host)
1184{
Josh Wu1c7b8742012-06-29 17:47:55 +08001185 struct nand_chip *nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01001186 struct mtd_info *mtd = nand_to_mtd(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +08001187 struct resource *regs, *regs_pmerr, *regs_rom;
Josh Wuabb1cd02014-10-11 18:01:50 +08001188 uint16_t *galois_table;
Josh Wu1c7b8742012-06-29 17:47:55 +08001189 int cap, sector_size, err_no;
1190
Josh Wu84cfbbb2013-01-23 20:47:12 +08001191 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
1192 if (err_no) {
1193 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1194 return err_no;
1195 }
1196
Richard Genoudf666d642013-07-30 17:17:29 +02001197 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001198 sector_size != host->pmecc_sector_size)
1199 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001200
Josh Wu1c7b8742012-06-29 17:47:55 +08001201 cap = host->pmecc_corr_cap;
1202 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001203 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1204 host->pmecc_lookup_table_offset_512 :
1205 host->pmecc_lookup_table_offset_1024;
1206
Josh Wu1c7b8742012-06-29 17:47:55 +08001207 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1208 cap, sector_size);
1209
1210 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1211 if (!regs) {
1212 dev_warn(host->dev,
1213 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1214 nand_chip->ecc.mode = NAND_ECC_SOFT;
1215 return 0;
1216 }
1217
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001218 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1219 if (IS_ERR(host->ecc)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001220 err_no = PTR_ERR(host->ecc);
1221 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001222 }
1223
1224 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001225 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1226 if (IS_ERR(host->pmerrloc_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001227 err_no = PTR_ERR(host->pmerrloc_base);
1228 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001229 }
Romain Izard55750752016-02-10 10:56:25 +01001230 host->pmerrloc_el_base = host->pmerrloc_base + ATMEL_PMERRLOC_SIGMAx +
1231 (host->caps->pmecc_max_correction + 1) * 4;
Josh Wu1c7b8742012-06-29 17:47:55 +08001232
Wu, Josh41c75402015-04-02 14:13:47 +08001233 if (!host->has_no_lookup_table) {
1234 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1235 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev,
1236 regs_rom);
1237 if (IS_ERR(host->pmecc_rom_base)) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001238 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 +08001239 host->has_no_lookup_table = true;
1240 }
Josh Wuabb1cd02014-10-11 18:01:50 +08001241 }
1242
1243 if (host->has_no_lookup_table) {
1244 /* Build the look-up table in runtime */
1245 galois_table = create_lookup_table(host->dev, sector_size);
1246 if (!galois_table) {
1247 dev_err(host->dev, "Failed to build a lookup table in runtime!\n");
1248 err_no = -EINVAL;
1249 goto err;
1250 }
1251
1252 host->pmecc_rom_base = (void __iomem *)galois_table;
1253 host->pmecc_lookup_table_offset = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +08001254 }
1255
Bo Shenb3857662014-06-12 15:58:45 +08001256 nand_chip->ecc.size = sector_size;
Josh Wu1c7b8742012-06-29 17:47:55 +08001257
1258 /* set ECC page size and oob layout */
1259 switch (mtd->writesize) {
Wu, Josha3557102014-07-22 17:24:18 +08001260 case 512:
1261 case 1024:
Josh Wu1c7b8742012-06-29 17:47:55 +08001262 case 2048:
Wu, Josha3557102014-07-22 17:24:18 +08001263 case 4096:
1264 case 8192:
1265 if (sector_size > mtd->writesize) {
1266 dev_err(host->dev, "pmecc sector size is bigger than the page size!\n");
1267 err_no = -EINVAL;
1268 goto err;
1269 }
1270
Josh Wu2fa831f2013-08-19 18:05:44 +08001271 host->pmecc_degree = (sector_size == 512) ?
1272 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Josh Wu1c7b8742012-06-29 17:47:55 +08001273 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +08001274 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1275 host->pmecc_index_of = host->pmecc_rom_base +
1276 host->pmecc_lookup_table_offset;
1277
Josh Wu1c7b8742012-06-29 17:47:55 +08001278 nand_chip->ecc.strength = cap;
Wu, Josh022a4782014-08-08 17:12:35 +08001279 nand_chip->ecc.bytes = pmecc_get_ecc_bytes(cap, sector_size);
Wu, Joshc9447ff2014-08-08 17:12:34 +08001280 nand_chip->ecc.steps = mtd->writesize / sector_size;
1281 nand_chip->ecc.total = nand_chip->ecc.bytes *
1282 nand_chip->ecc.steps;
Josh Wu477478a2015-04-02 14:12:33 +08001283 if (nand_chip->ecc.total >
1284 mtd->oobsize - PMECC_OOB_RESERVED_BYTES) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001285 dev_err(host->dev, "No room for ECC bytes\n");
1286 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001287 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001288 }
Bo Shenb3857662014-06-12 15:58:45 +08001289
Boris Brezilloncc003832016-02-03 19:59:11 +01001290 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Josh Wu1c7b8742012-06-29 17:47:55 +08001291 break;
Wu, Josha3557102014-07-22 17:24:18 +08001292 default:
Josh Wu1c7b8742012-06-29 17:47:55 +08001293 dev_warn(host->dev,
1294 "Unsupported page size for PMECC, use Software ECC\n");
Josh Wu1c7b8742012-06-29 17:47:55 +08001295 /* page size not handled by HW ECC */
1296 /* switching back to soft ECC */
1297 nand_chip->ecc.mode = NAND_ECC_SOFT;
1298 return 0;
1299 }
1300
1301 /* Allocate data for PMECC computation */
1302 err_no = pmecc_data_alloc(host);
1303 if (err_no) {
1304 dev_err(host->dev,
1305 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001306 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001307 }
1308
Herve Codina90445ff2014-03-03 12:15:29 +01001309 nand_chip->options |= NAND_NO_SUBPAGE_WRITE;
Josh Wu1c7b8742012-06-29 17:47:55 +08001310 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1311 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1312
1313 atmel_pmecc_core_init(mtd);
1314
1315 return 0;
1316
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001317err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001318 return err_no;
1319}
1320
1321/*
Richard Genoud77f54922008-04-23 19:51:14 +02001322 * Calculate HW ECC
1323 *
1324 * function called after a write
1325 *
1326 * mtd: MTD block structure
1327 * dat: raw data (unused)
1328 * ecc_code: buffer for ECC
1329 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001330static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001331 const u_char *dat, unsigned char *ecc_code)
1332{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001333 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001334 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02001335 unsigned int ecc_value;
1336
1337 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001338 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001339
Richard Genoud3fc23892008-10-12 08:42:28 +02001340 ecc_code[0] = ecc_value & 0xFF;
1341 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001342
1343 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001344 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001345
Richard Genoud3fc23892008-10-12 08:42:28 +02001346 ecc_code[2] = ecc_value & 0xFF;
1347 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001348
1349 return 0;
1350}
1351
1352/*
1353 * HW ECC read page function
1354 *
1355 * mtd: mtd info structure
1356 * chip: nand chip info structure
1357 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001358 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001359 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001360static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1361 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001362{
1363 int eccsize = chip->ecc.size;
1364 int eccbytes = chip->ecc.bytes;
Richard Genoud77f54922008-04-23 19:51:14 +02001365 uint8_t *p = buf;
1366 uint8_t *oob = chip->oob_poi;
1367 uint8_t *ecc_pos;
1368 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001369 unsigned int max_bitflips = 0;
Boris Brezillon78d28e82016-02-03 20:11:14 +01001370 struct mtd_oob_region oobregion = {};
Richard Genoud77f54922008-04-23 19:51:14 +02001371
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001372 /*
1373 * Errata: ALE is incorrectly wired up to the ECC controller
1374 * on the AP7000, so it will include the address cycles in the
1375 * ECC calculation.
1376 *
1377 * Workaround: Reset the parity registers before reading the
1378 * actual data.
1379 */
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001380 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu71b94e22013-05-09 15:34:54 +08001381 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001382 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001383
Richard Genoud77f54922008-04-23 19:51:14 +02001384 /* read the page */
1385 chip->read_buf(mtd, p, eccsize);
1386
1387 /* move to ECC position if needed */
Boris Brezillon78d28e82016-02-03 20:11:14 +01001388 mtd_ooblayout_ecc(mtd, 0, &oobregion);
1389 if (oobregion.offset != 0) {
1390 /*
1391 * This only works on large pages because the ECC controller
1392 * waits for NAND_CMD_RNDOUTSTART after the NAND_CMD_RNDOUT.
1393 * Anyway, for small pages, the first ECC byte is at offset
1394 * 0 in the OOB area.
Richard Genoud77f54922008-04-23 19:51:14 +02001395 */
1396 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon78d28e82016-02-03 20:11:14 +01001397 mtd->writesize + oobregion.offset, -1);
Richard Genoud77f54922008-04-23 19:51:14 +02001398 }
1399
1400 /* the ECC controller needs to read the ECC just after the data */
Boris Brezillon78d28e82016-02-03 20:11:14 +01001401 ecc_pos = oob + oobregion.offset;
Richard Genoud77f54922008-04-23 19:51:14 +02001402 chip->read_buf(mtd, ecc_pos, eccbytes);
1403
1404 /* check if there's an error */
1405 stat = chip->ecc.correct(mtd, p, oob, NULL);
1406
Mike Dunn3f91e942012-04-25 12:06:09 -07001407 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001408 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001409 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001410 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001411 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1412 }
Richard Genoud77f54922008-04-23 19:51:14 +02001413
1414 /* get back to oob start (end of page) */
1415 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1416
1417 /* read the oob */
1418 chip->read_buf(mtd, oob, mtd->oobsize);
1419
Mike Dunn3f91e942012-04-25 12:06:09 -07001420 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001421}
1422
1423/*
1424 * HW ECC Correction
1425 *
1426 * function called after a read
1427 *
1428 * mtd: MTD block structure
1429 * dat: raw data read from the chip
1430 * read_ecc: ECC from the chip (unused)
1431 * isnull: unused
1432 *
1433 * Detect and correct a 1 bit error for a page
1434 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001435static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001436 u_char *read_ecc, u_char *isnull)
1437{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001438 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001439 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02001440 unsigned int ecc_status;
1441 unsigned int ecc_word, ecc_bit;
1442
1443 /* get the status from the Status Register */
1444 ecc_status = ecc_readl(host->ecc, SR);
1445
1446 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001447 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001448 return 0;
1449
1450 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001451 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001452 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001453 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001454 ecc_word >>= 4;
1455
1456 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001457 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001458 /* check if it is a freshly erased block
1459 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001460 if ((ecc_bit == ATMEL_ECC_BITADDR)
1461 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001462 /* the block has just been erased, return OK */
1463 return 0;
1464 }
1465 /* it doesn't seems to be a freshly
1466 * erased block.
1467 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001468 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001469 " Unable to correct.\n");
Boris BREZILLON6e941192015-12-30 20:32:03 +01001470 return -EBADMSG;
Richard Genoud77f54922008-04-23 19:51:14 +02001471 }
1472
1473 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001474 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001475 /* there's nothing much to do here.
1476 * the bit error is on the ECC itself.
1477 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001478 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001479 " Nothing to correct\n");
1480 return 0;
1481 }
1482
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001483 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001484 " (word offset in the page :"
1485 " 0x%x bit offset : 0x%x)\n",
1486 ecc_word, ecc_bit);
1487 /* correct the error */
1488 if (nand_chip->options & NAND_BUSWIDTH_16) {
1489 /* 16 bits words */
1490 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1491 } else {
1492 /* 8 bits words */
1493 dat[ecc_word] ^= (1 << ecc_bit);
1494 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001495 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001496 return 1;
1497}
1498
1499/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001500 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001501 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001502static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1503{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001504 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001505 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu71b94e22013-05-09 15:34:54 +08001506
1507 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001508 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001509}
Richard Genoud77f54922008-04-23 19:51:14 +02001510
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001511static int atmel_of_init_ecc(struct atmel_nand_host *host,
1512 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001513{
Josh Wua41b51a2012-06-29 17:47:54 +08001514 u32 offset[2];
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001515 u32 val;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001516
Josh Wua41b51a2012-06-29 17:47:54 +08001517 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1518
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001519 /* Not using PMECC */
1520 if (!(host->nand_chip.ecc.mode == NAND_ECC_HW) || !host->has_pmecc)
1521 return 0;
Josh Wua41b51a2012-06-29 17:47:54 +08001522
1523 /* use PMECC, get correction capability, sector size and lookup
1524 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001525 * If correction bits and sector size are not specified, then find
1526 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001527 */
Josh Wue66b4312013-01-23 20:47:11 +08001528 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
Romain Izard94248462016-02-10 10:56:26 +01001529 if (val > host->caps->pmecc_max_correction) {
1530 dev_err(host->dev,
1531 "Required ECC strength too high: %u max %u\n",
1532 val, host->caps->pmecc_max_correction);
1533 return -EINVAL;
1534 }
1535 if ((val != 2) && (val != 4) && (val != 8) &&
1536 (val != 12) && (val != 24) && (val != 32)) {
Josh Wue66b4312013-01-23 20:47:11 +08001537 dev_err(host->dev,
Romain Izarde88b7f72016-01-15 11:34:56 +01001538 "Required ECC strength not supported: %u\n",
Josh Wue66b4312013-01-23 20:47:11 +08001539 val);
1540 return -EINVAL;
1541 }
1542 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001543 }
Josh Wua41b51a2012-06-29 17:47:54 +08001544
Josh Wue66b4312013-01-23 20:47:11 +08001545 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1546 if ((val != 512) && (val != 1024)) {
1547 dev_err(host->dev,
Romain Izarde88b7f72016-01-15 11:34:56 +01001548 "Required ECC sector size not supported: %u\n",
Josh Wue66b4312013-01-23 20:47:11 +08001549 val);
1550 return -EINVAL;
1551 }
1552 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001553 }
Josh Wua41b51a2012-06-29 17:47:54 +08001554
1555 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1556 offset, 2) != 0) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001557 dev_err(host->dev, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n");
1558 host->has_no_lookup_table = true;
1559 /* Will build a lookup table and initialize the offset later */
1560 return 0;
Josh Wua41b51a2012-06-29 17:47:54 +08001561 }
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001562
Josh Wuc0cf7872013-01-23 20:47:08 +08001563 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001564 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1565 return -EINVAL;
1566 }
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001567
Josh Wue66b4312013-01-23 20:47:11 +08001568 host->pmecc_lookup_table_offset_512 = offset[0];
1569 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001570
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001571 return 0;
1572}
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001573
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001574static int atmel_of_init_port(struct atmel_nand_host *host,
1575 struct device_node *np)
1576{
1577 u32 val;
1578 struct atmel_nand_data *board = &host->board;
1579 enum of_gpio_flags flags = 0;
1580
1581 host->caps = (struct atmel_nand_caps *)
1582 of_device_get_match_data(host->dev);
1583
1584 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1585 if (val >= 32) {
1586 dev_err(host->dev, "invalid addr-offset %u\n", val);
1587 return -EINVAL;
1588 }
1589 board->ale = val;
1590 }
1591
1592 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1593 if (val >= 32) {
1594 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1595 return -EINVAL;
1596 }
1597 board->cle = val;
1598 }
1599
1600 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1601
1602 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1603 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1604
1605 board->enable_pin = of_get_gpio(np, 1);
1606 board->det_pin = of_get_gpio(np, 2);
1607
1608 /* load the nfc driver if there is */
1609 of_platform_populate(np, NULL, NULL, host->dev);
1610
1611 /*
1612 * Initialize ECC mode to NAND_ECC_SOFT so that we have a correct value
1613 * even if the nand-ecc-mode property is not defined.
1614 */
1615 host->nand_chip.ecc.mode = NAND_ECC_SOFT;
1616
1617 return 0;
1618}
1619
Johan Hovold2c2b9282013-09-23 16:27:28 +02001620static int atmel_hw_nand_init_params(struct platform_device *pdev,
Josh Wu3dfe41a2012-06-25 18:07:43 +08001621 struct atmel_nand_host *host)
1622{
Josh Wu3dfe41a2012-06-25 18:07:43 +08001623 struct nand_chip *nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01001624 struct mtd_info *mtd = nand_to_mtd(nand_chip);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001625 struct resource *regs;
1626
1627 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1628 if (!regs) {
1629 dev_err(host->dev,
1630 "Can't get I/O resource regs, use software ECC\n");
1631 nand_chip->ecc.mode = NAND_ECC_SOFT;
1632 return 0;
1633 }
1634
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001635 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun8fb7b932014-07-28 21:19:55 +08001636 if (IS_ERR(host->ecc))
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001637 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001638
1639 /* ECC is calculated for the whole page (1 step) */
1640 nand_chip->ecc.size = mtd->writesize;
1641
1642 /* set ECC page size and oob layout */
1643 switch (mtd->writesize) {
1644 case 512:
Boris Brezilloncc003832016-02-03 19:59:11 +01001645 mtd_set_ooblayout(mtd, &atmel_ooblayout_sp_ops);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001646 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1647 break;
1648 case 1024:
Boris Brezilloncc003832016-02-03 19:59:11 +01001649 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001650 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1651 break;
1652 case 2048:
Boris Brezilloncc003832016-02-03 19:59:11 +01001653 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001654 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1655 break;
1656 case 4096:
Boris Brezilloncc003832016-02-03 19:59:11 +01001657 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001658 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1659 break;
1660 default:
1661 /* page size not handled by HW ECC */
1662 /* switching back to soft ECC */
1663 nand_chip->ecc.mode = NAND_ECC_SOFT;
1664 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 }
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001713 if (pending & host->nfc->caps->rb_mask) {
Josh Wue4e06932014-06-10 17:50:11 +08001714 complete(&host->nfc->comp_ready);
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001715 nfc_writel(host->nfc->hsmc_regs, IDR, host->nfc->caps->rb_mask);
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
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001733 if (flag & host->nfc->caps->rb_mask)
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
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001751 if (flag & host->nfc->caps->rb_mask)
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
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001819 return status & host->nfc->caps->rb_mask;
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:
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001992 nfc_prepare_interrupt(host, host->nfc->caps->rb_mask);
1993 nfc_wait_interrupt(host, host->nfc->caps->rb_mask);
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
2163 /* 16-bit bus width */
2164 if (host->board.bus_width_16)
2165 nand_chip->options |= NAND_BUSWIDTH_16;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002166 }
Andrew Victor42cb1402006-10-19 18:24:35 +02002167
Boris BREZILLONd699ed22015-12-10 09:00:41 +01002168 /* link the private data structures */
2169 nand_set_controller_data(nand_chip, host);
Frans Klaver03c287d2015-06-10 22:38:36 +02002170 mtd->dev.parent = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +02002171
2172 /* Set address of NAND IO lines */
2173 nand_chip->IO_ADDR_R = host->io_base;
2174 nand_chip->IO_ADDR_W = host->io_base;
Ivan Kutena4265f82007-05-24 14:35:58 +03002175
Josh Wu7dc37de2013-08-05 19:14:35 +08002176 if (nand_nfc.is_initialized) {
2177 /* NFC driver is probed and initialized */
2178 host->nfc = &nand_nfc;
2179
2180 nand_chip->select_chip = nfc_select_chip;
2181 nand_chip->dev_ready = nfc_device_ready;
2182 nand_chip->cmdfunc = nfc_nand_command;
2183
2184 /* Initialize the interrupt for NFC */
2185 irq = platform_get_irq(pdev, 0);
2186 if (irq < 0) {
2187 dev_err(host->dev, "Cannot get HSMC irq!\n");
Wei Yongjunff52c672013-08-23 10:50:36 +08002188 res = irq;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002189 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002190 }
2191
Josh Wu7dc37de2013-08-05 19:14:35 +08002192 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
2193 0, "hsmc", host);
2194 if (res) {
2195 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
2196 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 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002199 } else {
2200 res = atmel_nand_set_enable_ready_pins(mtd);
2201 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002202 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002203
Josh Wu7dc37de2013-08-05 19:14:35 +08002204 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002205 }
Ivan Kutena4265f82007-05-24 14:35:58 +03002206
Raphaël Poggi796fe362014-07-29 15:27:27 +02002207 nand_chip->chip_delay = 40; /* 40us command delay time */
Andrew Victor42cb1402006-10-19 18:24:35 +02002208
Hong Xucbc6c5e2011-01-18 14:36:05 +08002209
2210 nand_chip->read_buf = atmel_read_buf;
2211 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02002212
Andrew Victor42cb1402006-10-19 18:24:35 +02002213 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002214 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002215
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002216 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002217 res = devm_gpio_request(&pdev->dev,
2218 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002219 if (res < 0) {
2220 dev_err(&pdev->dev,
2221 "can't request det gpio %d\n",
2222 host->board.det_pin);
2223 goto err_no_card;
2224 }
2225
2226 res = gpio_direction_input(host->board.det_pin);
2227 if (res < 0) {
2228 dev_err(&pdev->dev,
2229 "can't request input direction det gpio %d\n",
2230 host->board.det_pin);
2231 goto err_no_card;
2232 }
2233
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002234 if (gpio_get_value(host->board.det_pin)) {
Jingoo Han1295f972013-12-26 12:30:58 +09002235 dev_info(&pdev->dev, "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01002236 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002237 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02002238 }
2239 }
2240
Josh Wu1b719262013-05-09 15:34:55 +08002241 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08002242 use_dma = 0;
2243
2244 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08002245 dma_cap_mask_t mask;
2246
2247 dma_cap_zero(mask);
2248 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02002249 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002250 if (!host->dma_chan) {
2251 dev_err(host->dev, "Failed to request DMA channel\n");
2252 use_dma = 0;
2253 }
2254 }
2255 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08002256 dev_info(host->dev, "Using %s for DMA transfers.\n",
2257 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08002258 else
2259 dev_info(host->dev, "No DMA support for NAND access.\n");
2260
Richard Genoud77f54922008-04-23 19:51:14 +02002261 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00002262 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02002263 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002264 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02002265 }
2266
Boris Brezillon4f3cab92016-04-01 14:54:22 +02002267 if (host->board.on_flash_bbt || on_flash_bbt)
2268 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
2269
2270 if (nand_chip->bbt_options & NAND_BBT_USE_FLASH)
2271 dev_info(&pdev->dev, "Use On Flash BBT\n");
2272
2273 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
2274 res = atmel_of_init_ecc(host, pdev->dev.of_node);
2275 if (res)
2276 goto err_hw_ecc;
2277 }
2278
Richard Genoud3fc23892008-10-12 08:42:28 +02002279 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08002280 if (host->has_pmecc)
2281 res = atmel_pmecc_nand_init_params(pdev, host);
2282 else
2283 res = atmel_hw_nand_init_params(pdev, host);
2284
Josh Wu3dfe41a2012-06-25 18:07:43 +08002285 if (res != 0)
2286 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02002287 }
2288
Josh Wu1ae9c092013-08-05 19:14:36 +08002289 /* initialize the nfc configuration register */
2290 if (host->nfc && host->nfc->use_nfc_sram) {
2291 res = nfc_sram_init(mtd);
2292 if (res) {
2293 host->nfc->use_nfc_sram = false;
2294 dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
2295 }
2296 }
2297
Richard Genoud77f54922008-04-23 19:51:14 +02002298 /* second phase scan */
2299 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02002300 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002301 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02002302 }
2303
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002304 mtd->name = "atmel_nand";
Brian Norrisa61ae812015-10-30 20:33:25 -07002305 res = mtd_device_register(mtd, host->board.parts,
2306 host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02002307 if (!res)
2308 return res;
2309
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002310err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002311 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08002312 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08002313err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002314err_scan_ident:
2315err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002316 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002317 if (host->dma_chan)
2318 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002319err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02002320 return res;
2321}
2322
2323/*
2324 * Remove a NAND device.
2325 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002326static int atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002327{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002328 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Boris BREZILLONac01efe2015-12-10 08:59:50 +01002329 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +02002330
2331 nand_release(mtd);
2332
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002333 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002334
Josh Wu1c7b8742012-06-29 17:47:55 +08002335 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
2336 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2337 pmerrloc_writel(host->pmerrloc_base, ELDIS,
2338 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08002339 }
2340
Hong Xucbc6c5e2011-01-18 14:36:05 +08002341 if (host->dma_chan)
2342 dma_release_channel(host->dma_chan);
2343
Josh Wu7dc37de2013-08-05 19:14:35 +08002344 platform_driver_unregister(&atmel_nand_nfc_driver);
2345
Andrew Victor42cb1402006-10-19 18:24:35 +02002346 return 0;
2347}
2348
Romain Izard55750752016-02-10 10:56:25 +01002349/*
2350 * AT91RM9200 does not have PMECC or PMECC Errloc peripherals for
2351 * BCH ECC. Combined with the "atmel,has-pmecc", it is used to describe
2352 * devices from the SAM9 family that have those.
2353 */
LABBE Corentin72eaec22015-11-20 08:45:16 +01002354static const struct atmel_nand_caps at91rm9200_caps = {
Wu, Josh51585772015-01-19 16:33:06 +08002355 .pmecc_correct_erase_page = false,
Romain Izard55750752016-02-10 10:56:25 +01002356 .pmecc_max_correction = 24,
Wu, Josh51585772015-01-19 16:33:06 +08002357};
2358
LABBE Corentin72eaec22015-11-20 08:45:16 +01002359static const struct atmel_nand_caps sama5d4_caps = {
Wu, Josh51585772015-01-19 16:33:06 +08002360 .pmecc_correct_erase_page = true,
Romain Izard55750752016-02-10 10:56:25 +01002361 .pmecc_max_correction = 24,
2362};
2363
2364/*
2365 * The PMECC Errloc controller starting in SAMA5D2 is not compatible,
2366 * as the increased correction strength requires more registers.
2367 */
2368static const struct atmel_nand_caps sama5d2_caps = {
2369 .pmecc_correct_erase_page = true,
2370 .pmecc_max_correction = 32,
Wu, Josh51585772015-01-19 16:33:06 +08002371};
2372
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002373static const struct of_device_id atmel_nand_dt_ids[] = {
Wu, Josh51585772015-01-19 16:33:06 +08002374 { .compatible = "atmel,at91rm9200-nand", .data = &at91rm9200_caps },
2375 { .compatible = "atmel,sama5d4-nand", .data = &sama5d4_caps },
Romain Izard55750752016-02-10 10:56:25 +01002376 { .compatible = "atmel,sama5d2-nand", .data = &sama5d2_caps },
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002377 { /* sentinel */ }
2378};
2379
2380MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002381
Josh Wu7dc37de2013-08-05 19:14:35 +08002382static int atmel_nand_nfc_probe(struct platform_device *pdev)
2383{
2384 struct atmel_nfc *nfc = &nand_nfc;
2385 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002386 int ret;
Josh Wu7dc37de2013-08-05 19:14:35 +08002387
2388 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2389 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
2390 if (IS_ERR(nfc->base_cmd_regs))
2391 return PTR_ERR(nfc->base_cmd_regs);
2392
2393 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2394 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
2395 if (IS_ERR(nfc->hsmc_regs))
2396 return PTR_ERR(nfc->hsmc_regs);
2397
2398 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2399 if (nfc_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +08002400 nfc->sram_bank0 = (void * __force)
2401 devm_ioremap_resource(&pdev->dev, nfc_sram);
Josh Wu1ae9c092013-08-05 19:14:36 +08002402 if (IS_ERR(nfc->sram_bank0)) {
Josh Wu7dc37de2013-08-05 19:14:35 +08002403 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2404 PTR_ERR(nfc->sram_bank0));
Josh Wu1ae9c092013-08-05 19:14:36 +08002405 } else {
2406 nfc->use_nfc_sram = true;
Josh Wu7dc37de2013-08-05 19:14:35 +08002407 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
Josh Wu6054d4d2013-08-05 19:14:37 +08002408
2409 if (pdev->dev.of_node)
2410 nfc->write_by_sram = of_property_read_bool(
2411 pdev->dev.of_node,
2412 "atmel,write-by-sram");
Josh Wu1ae9c092013-08-05 19:14:36 +08002413 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002414 }
2415
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002416 nfc->caps = (const struct atmel_nand_nfc_caps *)
2417 of_device_get_match_data(&pdev->dev);
2418 if (!nfc->caps)
2419 return -ENODEV;
2420
Wu, Josh50e04e22014-06-10 17:50:09 +08002421 nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
2422 nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */
2423
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002424 nfc->clk = devm_clk_get(&pdev->dev, NULL);
2425 if (!IS_ERR(nfc->clk)) {
2426 ret = clk_prepare_enable(nfc->clk);
2427 if (ret)
2428 return ret;
2429 } else {
2430 dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree");
2431 }
2432
Josh Wu7dc37de2013-08-05 19:14:35 +08002433 nfc->is_initialized = true;
2434 dev_info(&pdev->dev, "NFC is probed.\n");
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002435
2436 return 0;
2437}
2438
2439static int atmel_nand_nfc_remove(struct platform_device *pdev)
2440{
2441 struct atmel_nfc *nfc = &nand_nfc;
2442
2443 if (!IS_ERR(nfc->clk))
2444 clk_disable_unprepare(nfc->clk);
2445
Josh Wu7dc37de2013-08-05 19:14:35 +08002446 return 0;
2447}
2448
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002449static const struct atmel_nand_nfc_caps sama5d3_nfc_caps = {
2450 .rb_mask = NFC_SR_RB_EDGE0,
2451};
2452
2453static const struct atmel_nand_nfc_caps sama5d4_nfc_caps = {
2454 .rb_mask = NFC_SR_RB_EDGE3,
2455};
2456
Josh Wu81f29b42013-09-18 11:31:20 +08002457static const struct of_device_id atmel_nand_nfc_match[] = {
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002458 { .compatible = "atmel,sama5d3-nfc", .data = &sama5d3_nfc_caps },
2459 { .compatible = "atmel,sama5d4-nfc", .data = &sama5d4_nfc_caps },
Josh Wu7dc37de2013-08-05 19:14:35 +08002460 { /* sentinel */ }
2461};
Josh Wu81f29b42013-09-18 11:31:20 +08002462MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
Josh Wu7dc37de2013-08-05 19:14:35 +08002463
2464static struct platform_driver atmel_nand_nfc_driver = {
2465 .driver = {
2466 .name = "atmel_nand_nfc",
Josh Wu7dc37de2013-08-05 19:14:35 +08002467 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2468 },
2469 .probe = atmel_nand_nfc_probe,
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002470 .remove = atmel_nand_nfc_remove,
Josh Wu7dc37de2013-08-05 19:14:35 +08002471};
2472
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002473static struct platform_driver atmel_nand_driver = {
Johan Hovold2c2b9282013-09-23 16:27:28 +02002474 .probe = atmel_nand_probe,
2475 .remove = atmel_nand_remove,
Andrew Victor42cb1402006-10-19 18:24:35 +02002476 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002477 .name = "atmel_nand",
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002478 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02002479 },
2480};
2481
Johan Hovold2c2b9282013-09-23 16:27:28 +02002482module_platform_driver(atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02002483
2484MODULE_LICENSE("GPL");
2485MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02002486MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002487MODULE_ALIAS("platform:atmel_nand");