blob: b132c8fd3701affcc92f87920dcd1ebec5b2532f [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, Josh515857782015-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, Josh515857782015-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
Richard Genoud77f54922008-04-23 19:51:14 +020074/* oob layout for large page size
75 * bad block info is on bytes 0 and 1
76 * the bytes have to be consecutives to avoid
77 * several NAND_CMD_RNDOUT during read
78 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020079static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020080 .eccbytes = 4,
81 .eccpos = {60, 61, 62, 63},
82 .oobfree = {
83 {2, 58}
84 },
85};
86
87/* oob layout for small page size
88 * bad block info is on bytes 4 and 5
89 * the bytes have to be consecutives to avoid
90 * several NAND_CMD_RNDOUT during read
91 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020092static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020093 .eccbytes = 4,
94 .eccpos = {0, 1, 2, 3},
95 .oobfree = {
96 {6, 10}
97 },
98};
99
Josh Wu7dc37de2013-08-05 19:14:35 +0800100struct atmel_nfc {
101 void __iomem *base_cmd_regs;
102 void __iomem *hsmc_regs;
Wu, Josh068b44b2014-11-07 15:26:09 +0800103 void *sram_bank0;
Josh Wu7dc37de2013-08-05 19:14:35 +0800104 dma_addr_t sram_bank0_phys;
Josh Wu1ae9c092013-08-05 19:14:36 +0800105 bool use_nfc_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +0800106 bool write_by_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +0800107
Boris BREZILLON2d405ec2014-09-13 01:23:59 +0200108 struct clk *clk;
109
Josh Wu7dc37de2013-08-05 19:14:35 +0800110 bool is_initialized;
Josh Wue4e06932014-06-10 17:50:11 +0800111 struct completion comp_ready;
112 struct completion comp_cmd_done;
113 struct completion comp_xfer_done;
Josh Wu1ae9c092013-08-05 19:14:36 +0800114
115 /* Point to the sram bank which include readed data via NFC */
Wu, Josh068b44b2014-11-07 15:26:09 +0800116 void *data_in_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +0800117 bool will_write_sram;
Romain Izard5ddc7bd2016-02-10 10:56:23 +0100118 const struct atmel_nand_nfc_caps *caps;
Josh Wu7dc37de2013-08-05 19:14:35 +0800119};
120static struct atmel_nfc nand_nfc;
121
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200122struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +0200123 struct nand_chip nand_chip;
Andrew Victor42cb1402006-10-19 18:24:35 +0200124 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800125 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800126 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +0200127 struct device *dev;
128 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800129
130 struct completion comp;
131 struct dma_chan *dma_chan;
Josh Wua41b51a2012-06-29 17:47:54 +0800132
Josh Wu7dc37de2013-08-05 19:14:35 +0800133 struct atmel_nfc *nfc;
134
LABBE Corentin72eaec22015-11-20 08:45:16 +0100135 const struct atmel_nand_caps *caps;
Josh Wua41b51a2012-06-29 17:47:54 +0800136 bool has_pmecc;
137 u8 pmecc_corr_cap;
138 u16 pmecc_sector_size;
Josh Wuabb1cd02014-10-11 18:01:50 +0800139 bool has_no_lookup_table;
Josh Wua41b51a2012-06-29 17:47:54 +0800140 u32 pmecc_lookup_table_offset;
Josh Wue66b4312013-01-23 20:47:11 +0800141 u32 pmecc_lookup_table_offset_512;
142 u32 pmecc_lookup_table_offset_1024;
Josh Wu1c7b8742012-06-29 17:47:55 +0800143
Josh Wu1c7b8742012-06-29 17:47:55 +0800144 int pmecc_degree; /* Degree of remainders */
145 int pmecc_cw_len; /* Length of codeword */
146
147 void __iomem *pmerrloc_base;
Romain Izard55750752016-02-10 10:56:25 +0100148 void __iomem *pmerrloc_el_base;
Josh Wu1c7b8742012-06-29 17:47:55 +0800149 void __iomem *pmecc_rom_base;
150
151 /* lookup table for alpha_to and index_of */
152 void __iomem *pmecc_alpha_to;
153 void __iomem *pmecc_index_of;
154
155 /* data for pmecc computation */
156 int16_t *pmecc_partial_syn;
157 int16_t *pmecc_si;
158 int16_t *pmecc_smu; /* Sigma table */
159 int16_t *pmecc_lmu; /* polynomal order */
160 int *pmecc_mu;
161 int *pmecc_dmu;
162 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200163};
164
Josh Wu1c7b8742012-06-29 17:47:55 +0800165static struct nand_ecclayout atmel_pmecc_oobinfo;
166
Andrew Victor42cb1402006-10-19 18:24:35 +0200167/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900168 * Enable NAND.
169 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200170static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900171{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800172 if (gpio_is_valid(host->board.enable_pin))
173 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900174}
175
176/*
177 * Disable NAND.
178 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200179static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900180{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800181 if (gpio_is_valid(host->board.enable_pin))
182 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900183}
184
185/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200186 * Hardware specific access to control-lines
187 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200188static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200189{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100190 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100191 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +0200192
Atsushi Nemoto81365082008-04-27 01:51:12 +0900193 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900194 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200195 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900196 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200197 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900198 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200199 if (cmd == NAND_CMD_NONE)
200 return;
201
202 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800203 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200204 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800205 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200206}
207
208/*
209 * Read the Device Ready pin.
210 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200211static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200212{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100213 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100214 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +0200215
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800216 return gpio_get_value(host->board.rdy_pin) ^
217 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200218}
219
Josh Wu7dc37de2013-08-05 19:14:35 +0800220/* Set up for hardware ready pin and enable pin. */
221static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
222{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100223 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100224 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu7dc37de2013-08-05 19:14:35 +0800225 int res = 0;
226
227 if (gpio_is_valid(host->board.rdy_pin)) {
228 res = devm_gpio_request(host->dev,
229 host->board.rdy_pin, "nand_rdy");
230 if (res < 0) {
231 dev_err(host->dev,
232 "can't request rdy gpio %d\n",
233 host->board.rdy_pin);
234 return res;
235 }
236
237 res = gpio_direction_input(host->board.rdy_pin);
238 if (res < 0) {
239 dev_err(host->dev,
240 "can't request input direction rdy gpio %d\n",
241 host->board.rdy_pin);
242 return res;
243 }
244
245 chip->dev_ready = atmel_nand_device_ready;
246 }
247
248 if (gpio_is_valid(host->board.enable_pin)) {
249 res = devm_gpio_request(host->dev,
250 host->board.enable_pin, "nand_enable");
251 if (res < 0) {
252 dev_err(host->dev,
253 "can't request enable gpio %d\n",
254 host->board.enable_pin);
255 return res;
256 }
257
258 res = gpio_direction_output(host->board.enable_pin, 1);
259 if (res < 0) {
260 dev_err(host->dev,
261 "can't request output direction enable gpio %d\n",
262 host->board.enable_pin);
263 return res;
264 }
265 }
266
267 return res;
268}
269
Artem Bityutskiy50082312012-02-02 13:54:25 +0200270/*
271 * Minimal-overhead PIO for data access.
272 */
273static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
274{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100275 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100276 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200277
Josh Wu1ae9c092013-08-05 19:14:36 +0800278 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +0800279 memcpy(buf, host->nfc->data_in_sram, len);
Josh Wu1ae9c092013-08-05 19:14:36 +0800280 host->nfc->data_in_sram += len;
281 } else {
282 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
283 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200284}
285
286static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
287{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100288 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100289 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200290
Josh Wu1ae9c092013-08-05 19:14:36 +0800291 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +0800292 memcpy(buf, host->nfc->data_in_sram, len);
Josh Wu1ae9c092013-08-05 19:14:36 +0800293 host->nfc->data_in_sram += len;
294 } else {
295 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
296 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200297}
298
299static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
300{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100301 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200302
303 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
304}
305
306static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
307{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100308 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200309
310 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
311}
312
Hong Xucbc6c5e2011-01-18 14:36:05 +0800313static void dma_complete_func(void *completion)
314{
315 complete(completion);
316}
317
Josh Wu1ae9c092013-08-05 19:14:36 +0800318static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
319{
320 /* NFC only has two banks. Must be 0 or 1 */
321 if (bank > 1)
322 return -EINVAL;
323
324 if (bank) {
Boris BREZILLONac01efe2015-12-10 08:59:50 +0100325 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
326
Josh Wu1ae9c092013-08-05 19:14:36 +0800327 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
Boris BREZILLONac01efe2015-12-10 08:59:50 +0100328 if (mtd->writesize > 2048)
Josh Wu1ae9c092013-08-05 19:14:36 +0800329 return -EINVAL;
330 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
331 } else {
332 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
333 }
334
335 return 0;
336}
337
338static uint nfc_get_sram_off(struct atmel_nand_host *host)
339{
340 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
341 return NFC_SRAM_BANK1_OFFSET;
342 else
343 return 0;
344}
345
346static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
347{
348 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
349 return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
350 else
351 return host->nfc->sram_bank0_phys;
352}
353
Hong Xucbc6c5e2011-01-18 14:36:05 +0800354static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
355 int is_read)
356{
357 struct dma_device *dma_dev;
358 enum dma_ctrl_flags flags;
359 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
360 struct dma_async_tx_descriptor *tx = NULL;
361 dma_cookie_t cookie;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100362 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100363 struct atmel_nand_host *host = nand_get_controller_data(chip);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800364 void *p = buf;
365 int err = -EIO;
366 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Josh Wu1ae9c092013-08-05 19:14:36 +0800367 struct atmel_nfc *nfc = host->nfc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800368
Hong Xu80b4f812011-03-31 18:33:15 +0800369 if (buf >= high_memory)
370 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800371
372 dma_dev = host->dma_chan->device;
373
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200374 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800375
376 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
377 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
378 dev_err(host->dev, "Failed to dma_map_single\n");
379 goto err_buf;
380 }
381
382 if (is_read) {
Josh Wu1ae9c092013-08-05 19:14:36 +0800383 if (nfc && nfc->data_in_sram)
384 dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
385 - (nfc->sram_bank0 + nfc_get_sram_off(host)));
386 else
387 dma_src_addr = host->io_phys;
388
Hong Xucbc6c5e2011-01-18 14:36:05 +0800389 dma_dst_addr = phys_addr;
390 } else {
391 dma_src_addr = phys_addr;
Josh Wu6054d4d2013-08-05 19:14:37 +0800392
393 if (nfc && nfc->write_by_sram)
394 dma_dst_addr = nfc_sram_phys(host);
395 else
396 dma_dst_addr = host->io_phys;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800397 }
398
399 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
400 dma_src_addr, len, flags);
401 if (!tx) {
402 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
403 goto err_dma;
404 }
405
406 init_completion(&host->comp);
407 tx->callback = dma_complete_func;
408 tx->callback_param = &host->comp;
409
410 cookie = tx->tx_submit(tx);
411 if (dma_submit_error(cookie)) {
412 dev_err(host->dev, "Failed to do DMA tx_submit\n");
413 goto err_dma;
414 }
415
416 dma_async_issue_pending(host->dma_chan);
417 wait_for_completion(&host->comp);
418
Josh Wu1ae9c092013-08-05 19:14:36 +0800419 if (is_read && nfc && nfc->data_in_sram)
420 /* After read data from SRAM, need to increase the position */
421 nfc->data_in_sram += len;
422
Hong Xucbc6c5e2011-01-18 14:36:05 +0800423 err = 0;
424
425err_dma:
426 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
427err_buf:
428 if (err != 0)
Nicolas Ferre74414a942014-02-12 12:26:54 +0100429 dev_dbg(host->dev, "Fall back to CPU I/O\n");
Hong Xucbc6c5e2011-01-18 14:36:05 +0800430 return err;
431}
432
433static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
434{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100435 struct nand_chip *chip = mtd_to_nand(mtd);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800436
Nicolas Ferre9d515672011-04-01 16:40:44 +0200437 if (use_dma && len > mtd->oobsize)
438 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800439 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
440 return;
441
Boris Brezillon4f3cab92016-04-01 14:54:22 +0200442 if (chip->options & NAND_BUSWIDTH_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200443 atmel_read_buf16(mtd, buf, len);
444 else
445 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800446}
447
448static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
449{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100450 struct nand_chip *chip = mtd_to_nand(mtd);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800451
Nicolas Ferre9d515672011-04-01 16:40:44 +0200452 if (use_dma && len > mtd->oobsize)
453 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800454 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
455 return;
456
Boris Brezillon4f3cab92016-04-01 14:54:22 +0200457 if (chip->options & NAND_BUSWIDTH_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200458 atmel_write_buf16(mtd, buf, len);
459 else
460 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800461}
462
David Brownell23a346c2008-07-03 23:40:16 -0700463/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800464 * Return number of ecc bytes per sector according to sector size and
465 * correction capability
466 *
467 * Following table shows what at91 PMECC supported:
468 * Correction Capability Sector_512_bytes Sector_1024_bytes
469 * ===================== ================ =================
470 * 2-bits 4-bytes 4-bytes
471 * 4-bits 7-bytes 7-bytes
472 * 8-bits 13-bytes 14-bytes
473 * 12-bits 20-bytes 21-bytes
474 * 24-bits 39-bytes 42-bytes
Romain Izard94248462016-02-10 10:56:26 +0100475 * 32-bits 52-bytes 56-bytes
Josh Wu1c7b8742012-06-29 17:47:55 +0800476 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500477static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800478{
479 int m = 12 + sector_size / 512;
480 return (m * cap + 7) / 8;
481}
482
Bill Pemberton06f25512012-11-19 13:23:07 -0500483static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800484 int oobsize, int ecc_len)
Josh Wu1c7b8742012-06-29 17:47:55 +0800485{
486 int i;
487
488 layout->eccbytes = ecc_len;
489
490 /* ECC will occupy the last ecc_len bytes continuously */
491 for (i = 0; i < ecc_len; i++)
492 layout->eccpos[i] = oobsize - ecc_len + i;
493
Josh Wu477478a2015-04-02 14:12:33 +0800494 layout->oobfree[0].offset = PMECC_OOB_RESERVED_BYTES;
Josh Wu1c7b8742012-06-29 17:47:55 +0800495 layout->oobfree[0].length =
496 oobsize - ecc_len - layout->oobfree[0].offset;
497}
498
Bill Pemberton06f25512012-11-19 13:23:07 -0500499static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800500{
501 int table_size;
502
503 table_size = host->pmecc_sector_size == 512 ?
504 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
505
506 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
507 table_size * sizeof(int16_t);
508}
509
Bill Pemberton06f25512012-11-19 13:23:07 -0500510static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800511{
512 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800513 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800514
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800515 size = (2 * cap + 1) * sizeof(int16_t);
516 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
517 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
518 host->pmecc_lmu = devm_kzalloc(host->dev,
519 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
520 host->pmecc_smu = devm_kzalloc(host->dev,
521 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800522
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800523 size = (cap + 1) * sizeof(int);
524 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
525 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
526 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800527
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800528 if (!host->pmecc_partial_syn ||
529 !host->pmecc_si ||
530 !host->pmecc_lmu ||
531 !host->pmecc_smu ||
532 !host->pmecc_mu ||
533 !host->pmecc_dmu ||
534 !host->pmecc_delta)
535 return -ENOMEM;
536
537 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800538}
539
540static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
541{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100542 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100543 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800544 int i;
545 uint32_t value;
546
547 /* Fill odd syndromes */
548 for (i = 0; i < host->pmecc_corr_cap; i++) {
549 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
550 if (i & 1)
551 value >>= 16;
552 value &= 0xffff;
553 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
554 }
555}
556
557static void pmecc_substitute(struct mtd_info *mtd)
558{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100559 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100560 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800561 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
562 int16_t __iomem *index_of = host->pmecc_index_of;
563 int16_t *partial_syn = host->pmecc_partial_syn;
564 const int cap = host->pmecc_corr_cap;
565 int16_t *si;
566 int i, j;
567
568 /* si[] is a table that holds the current syndrome value,
569 * an element of that table belongs to the field
570 */
571 si = host->pmecc_si;
572
573 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
574
575 /* Computation 2t syndromes based on S(x) */
576 /* Odd syndromes */
577 for (i = 1; i < 2 * cap; i += 2) {
578 for (j = 0; j < host->pmecc_degree; j++) {
579 if (partial_syn[i] & ((unsigned short)0x1 << j))
580 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
581 }
582 }
583 /* Even syndrome = (Odd syndrome) ** 2 */
584 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
585 if (si[j] == 0) {
586 si[i] = 0;
587 } else {
588 int16_t tmp;
589
590 tmp = readw_relaxed(index_of + si[j]);
591 tmp = (tmp * 2) % host->pmecc_cw_len;
592 si[i] = readw_relaxed(alpha_to + tmp);
593 }
594 }
595
596 return;
597}
598
599static void pmecc_get_sigma(struct mtd_info *mtd)
600{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100601 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100602 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800603
604 int16_t *lmu = host->pmecc_lmu;
605 int16_t *si = host->pmecc_si;
606 int *mu = host->pmecc_mu;
607 int *dmu = host->pmecc_dmu; /* Discrepancy */
608 int *delta = host->pmecc_delta; /* Delta order */
609 int cw_len = host->pmecc_cw_len;
610 const int16_t cap = host->pmecc_corr_cap;
611 const int num = 2 * cap + 1;
612 int16_t __iomem *index_of = host->pmecc_index_of;
613 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
614 int i, j, k;
615 uint32_t dmu_0_count, tmp;
616 int16_t *smu = host->pmecc_smu;
617
618 /* index of largest delta */
619 int ro;
620 int largest;
621 int diff;
622
623 dmu_0_count = 0;
624
625 /* First Row */
626
627 /* Mu */
628 mu[0] = -1;
629
630 memset(smu, 0, sizeof(int16_t) * num);
631 smu[0] = 1;
632
633 /* discrepancy set to 1 */
634 dmu[0] = 1;
635 /* polynom order set to 0 */
636 lmu[0] = 0;
637 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
638
639 /* Second Row */
640
641 /* Mu */
642 mu[1] = 0;
643 /* Sigma(x) set to 1 */
644 memset(&smu[num], 0, sizeof(int16_t) * num);
645 smu[num] = 1;
646
647 /* discrepancy set to S1 */
648 dmu[1] = si[1];
649
650 /* polynom order set to 0 */
651 lmu[1] = 0;
652
653 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
654
655 /* Init the Sigma(x) last row */
656 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
657
658 for (i = 1; i <= cap; i++) {
659 mu[i + 1] = i << 1;
660 /* Begin Computing Sigma (Mu+1) and L(mu) */
661 /* check if discrepancy is set to 0 */
662 if (dmu[i] == 0) {
663 dmu_0_count++;
664
665 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
666 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
667 tmp += 2;
668 else
669 tmp += 1;
670
671 if (dmu_0_count == tmp) {
672 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
673 smu[(cap + 1) * num + j] =
674 smu[i * num + j];
675
676 lmu[cap + 1] = lmu[i];
677 return;
678 }
679
680 /* copy polynom */
681 for (j = 0; j <= lmu[i] >> 1; j++)
682 smu[(i + 1) * num + j] = smu[i * num + j];
683
684 /* copy previous polynom order to the next */
685 lmu[i + 1] = lmu[i];
686 } else {
687 ro = 0;
688 largest = -1;
689 /* find largest delta with dmu != 0 */
690 for (j = 0; j < i; j++) {
691 if ((dmu[j]) && (delta[j] > largest)) {
692 largest = delta[j];
693 ro = j;
694 }
695 }
696
697 /* compute difference */
698 diff = (mu[i] - mu[ro]);
699
700 /* Compute degree of the new smu polynomial */
701 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
702 lmu[i + 1] = lmu[i];
703 else
704 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
705
706 /* Init smu[i+1] with 0 */
707 for (k = 0; k < num; k++)
708 smu[(i + 1) * num + k] = 0;
709
710 /* Compute smu[i+1] */
711 for (k = 0; k <= lmu[ro] >> 1; k++) {
712 int16_t a, b, c;
713
714 if (!(smu[ro * num + k] && dmu[i]))
715 continue;
716 a = readw_relaxed(index_of + dmu[i]);
717 b = readw_relaxed(index_of + dmu[ro]);
718 c = readw_relaxed(index_of + smu[ro * num + k]);
719 tmp = a + (cw_len - b) + c;
720 a = readw_relaxed(alpha_to + tmp % cw_len);
721 smu[(i + 1) * num + (k + diff)] = a;
722 }
723
724 for (k = 0; k <= lmu[i] >> 1; k++)
725 smu[(i + 1) * num + k] ^= smu[i * num + k];
726 }
727
728 /* End Computing Sigma (Mu+1) and L(mu) */
729 /* In either case compute delta */
730 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
731
732 /* Do not compute discrepancy for the last iteration */
733 if (i >= cap)
734 continue;
735
736 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
737 tmp = 2 * (i - 1);
738 if (k == 0) {
739 dmu[i + 1] = si[tmp + 3];
740 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
741 int16_t a, b, c;
742 a = readw_relaxed(index_of +
743 smu[(i + 1) * num + k]);
744 b = si[2 * (i - 1) + 3 - k];
745 c = readw_relaxed(index_of + b);
746 tmp = a + c;
747 tmp %= cw_len;
748 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
749 dmu[i + 1];
750 }
751 }
752 }
753
754 return;
755}
756
757static int pmecc_err_location(struct mtd_info *mtd)
758{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100759 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100760 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800761 unsigned long end_time;
762 const int cap = host->pmecc_corr_cap;
763 const int num = 2 * cap + 1;
764 int sector_size = host->pmecc_sector_size;
765 int err_nbr = 0; /* number of error */
766 int roots_nbr; /* number of roots */
767 int i;
768 uint32_t val;
769 int16_t *smu = host->pmecc_smu;
770
771 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
772
773 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
774 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
775 smu[(cap + 1) * num + i]);
776 err_nbr++;
777 }
778
779 val = (err_nbr - 1) << 16;
780 if (sector_size == 1024)
781 val |= 1;
782
783 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
784 pmerrloc_writel(host->pmerrloc_base, ELEN,
785 sector_size * 8 + host->pmecc_degree * cap);
786
787 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
788 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
789 & PMERRLOC_CALC_DONE)) {
790 if (unlikely(time_after(jiffies, end_time))) {
791 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
792 return -1;
793 }
794 cpu_relax();
795 }
796
797 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
798 & PMERRLOC_ERR_NUM_MASK) >> 8;
799 /* Number of roots == degree of smu hence <= cap */
800 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
801 return err_nbr - 1;
802
803 /* Number of roots does not match the degree of smu
804 * unable to correct error */
805 return -1;
806}
807
808static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
809 int sector_num, int extra_bytes, int err_nbr)
810{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100811 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100812 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800813 int i = 0;
814 int byte_pos, bit_pos, sector_size, pos;
815 uint32_t tmp;
816 uint8_t err_byte;
817
818 sector_size = host->pmecc_sector_size;
819
820 while (err_nbr) {
Romain Izard55750752016-02-10 10:56:25 +0100821 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_el_base, i) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +0800822 byte_pos = tmp / 8;
823 bit_pos = tmp % 8;
824
825 if (byte_pos >= (sector_size + extra_bytes))
826 BUG(); /* should never happen */
827
828 if (byte_pos < sector_size) {
829 err_byte = *(buf + byte_pos);
830 *(buf + byte_pos) ^= (1 << bit_pos);
831
832 pos = sector_num * host->pmecc_sector_size + byte_pos;
Romain Izard12197bf2016-01-13 17:34:13 +0100833 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 +0800834 pos, bit_pos, err_byte, *(buf + byte_pos));
835 } else {
Boris Brezillon78d28e82016-02-03 20:11:14 +0100836 struct mtd_oob_region oobregion;
837
Josh Wu1c7b8742012-06-29 17:47:55 +0800838 /* Bit flip in OOB area */
Wu, Josh022a4782014-08-08 17:12:35 +0800839 tmp = sector_num * nand_chip->ecc.bytes
Josh Wu1c7b8742012-06-29 17:47:55 +0800840 + (byte_pos - sector_size);
841 err_byte = ecc[tmp];
842 ecc[tmp] ^= (1 << bit_pos);
843
Boris Brezillon78d28e82016-02-03 20:11:14 +0100844 mtd_ooblayout_ecc(mtd, 0, &oobregion);
845 pos = tmp + oobregion.offset;
Romain Izard12197bf2016-01-13 17:34:13 +0100846 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 +0800847 pos, bit_pos, err_byte, ecc[tmp]);
848 }
849
850 i++;
851 err_nbr--;
852 }
853
854 return;
855}
856
857static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
858 u8 *ecc)
859{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100860 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100861 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Bo Shenb3857662014-06-12 15:58:45 +0800862 int i, err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800863 uint8_t *buf_pos;
Wu, Josh267d46e2015-01-14 11:50:46 +0800864 int max_bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800865
Wu, Joshc9447ff2014-08-08 17:12:34 +0800866 for (i = 0; i < nand_chip->ecc.steps; i++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800867 err_nbr = 0;
868 if (pmecc_stat & 0x1) {
869 buf_pos = buf + i * host->pmecc_sector_size;
870
871 pmecc_gen_syndrome(mtd, i);
872 pmecc_substitute(mtd);
873 pmecc_get_sigma(mtd);
874
875 err_nbr = pmecc_err_location(mtd);
Boris Brezillonff6ee102016-03-01 14:11:52 +0100876 if (err_nbr >= 0) {
877 pmecc_correct_data(mtd, buf_pos, ecc, i,
878 nand_chip->ecc.bytes,
879 err_nbr);
880 } else if (!host->caps->pmecc_correct_erase_page) {
881 u8 *ecc_pos = ecc + (i * nand_chip->ecc.bytes);
882
883 /* Try to detect erased pages */
884 err_nbr = nand_check_erased_ecc_chunk(buf_pos,
885 host->pmecc_sector_size,
886 ecc_pos,
887 nand_chip->ecc.bytes,
888 NULL, 0,
889 nand_chip->ecc.strength);
890 }
891
892 if (err_nbr < 0) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800893 dev_err(host->dev, "PMECC: Too many errors\n");
894 mtd->ecc_stats.failed++;
895 return -EIO;
Josh Wu1c7b8742012-06-29 17:47:55 +0800896 }
Boris Brezillonff6ee102016-03-01 14:11:52 +0100897
898 mtd->ecc_stats.corrected += err_nbr;
899 max_bitflips = max_t(int, max_bitflips, err_nbr);
Josh Wu1c7b8742012-06-29 17:47:55 +0800900 }
901 pmecc_stat >>= 1;
902 }
903
Wu, Josh267d46e2015-01-14 11:50:46 +0800904 return max_bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800905}
906
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800907static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
908{
909 u32 val;
910
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800911 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
912 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
913 return;
914 }
915
Josh Wu1fad0e82013-08-07 17:58:11 +0800916 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
917 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
918 val = pmecc_readl_relaxed(host->ecc, CFG);
919
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800920 if (ecc_op == NAND_ECC_READ)
921 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
922 | PMECC_CFG_AUTO_ENABLE);
923 else
924 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
925 & ~PMECC_CFG_AUTO_ENABLE);
926
927 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
928 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
929}
930
Josh Wu1c7b8742012-06-29 17:47:55 +0800931static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
932 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
933{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100934 struct atmel_nand_host *host = nand_get_controller_data(chip);
Bo Shenb3857662014-06-12 15:58:45 +0800935 int eccsize = chip->ecc.size * chip->ecc.steps;
Josh Wu1c7b8742012-06-29 17:47:55 +0800936 uint8_t *oob = chip->oob_poi;
Josh Wu1c7b8742012-06-29 17:47:55 +0800937 uint32_t stat;
938 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800939 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800940
Josh Wu1ae9c092013-08-05 19:14:36 +0800941 if (!host->nfc || !host->nfc->use_nfc_sram)
942 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800943
944 chip->read_buf(mtd, buf, eccsize);
945 chip->read_buf(mtd, oob, mtd->oobsize);
946
947 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
948 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
949 if (unlikely(time_after(jiffies, end_time))) {
950 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
951 return -EIO;
952 }
953 cpu_relax();
954 }
955
956 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800957 if (stat != 0) {
Boris Brezillon78d28e82016-02-03 20:11:14 +0100958 struct mtd_oob_region oobregion;
959
960 mtd_ooblayout_ecc(mtd, 0, &oobregion);
961 bitflips = pmecc_correction(mtd, stat, buf,
962 &oob[oobregion.offset]);
Josh Wuc0c70d92012-11-27 18:50:31 +0800963 if (bitflips < 0)
964 /* uncorrectable errors */
965 return 0;
966 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800967
Josh Wuc0c70d92012-11-27 18:50:31 +0800968 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800969}
970
971static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +0200972 struct nand_chip *chip, const uint8_t *buf, int oob_required,
973 int page)
Josh Wu1c7b8742012-06-29 17:47:55 +0800974{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100975 struct atmel_nand_host *host = nand_get_controller_data(chip);
Boris Brezillon78d28e82016-02-03 20:11:14 +0100976 struct mtd_oob_region oobregion = { };
977 int i, j, section = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800978 unsigned long end_time;
979
Josh Wu6054d4d2013-08-05 19:14:37 +0800980 if (!host->nfc || !host->nfc->write_by_sram) {
981 pmecc_enable(host, NAND_ECC_WRITE);
982 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
983 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800984
985 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
986 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
987 if (unlikely(time_after(jiffies, end_time))) {
988 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
989 return -EIO;
990 }
991 cpu_relax();
992 }
993
Wu, Joshc9447ff2014-08-08 17:12:34 +0800994 for (i = 0; i < chip->ecc.steps; i++) {
Wu, Josh022a4782014-08-08 17:12:35 +0800995 for (j = 0; j < chip->ecc.bytes; j++) {
Boris Brezillon78d28e82016-02-03 20:11:14 +0100996 if (!oobregion.length)
997 mtd_ooblayout_ecc(mtd, section, &oobregion);
Josh Wu1c7b8742012-06-29 17:47:55 +0800998
Boris Brezillon78d28e82016-02-03 20:11:14 +0100999 chip->oob_poi[oobregion.offset] =
Josh Wu1c7b8742012-06-29 17:47:55 +08001000 pmecc_readb_ecc_relaxed(host->ecc, i, j);
Boris Brezillon78d28e82016-02-03 20:11:14 +01001001 oobregion.length--;
1002 oobregion.offset++;
1003 section++;
Josh Wu1c7b8742012-06-29 17:47:55 +08001004 }
1005 }
1006 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1007
1008 return 0;
1009}
1010
1011static void atmel_pmecc_core_init(struct mtd_info *mtd)
1012{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001013 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001014 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +08001015 uint32_t val = 0;
1016 struct nand_ecclayout *ecc_layout;
Boris Brezillon78d28e82016-02-03 20:11:14 +01001017 struct mtd_oob_region oobregion;
Josh Wu1c7b8742012-06-29 17:47:55 +08001018
1019 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
1020 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1021
1022 switch (host->pmecc_corr_cap) {
1023 case 2:
1024 val = PMECC_CFG_BCH_ERR2;
1025 break;
1026 case 4:
1027 val = PMECC_CFG_BCH_ERR4;
1028 break;
1029 case 8:
1030 val = PMECC_CFG_BCH_ERR8;
1031 break;
1032 case 12:
1033 val = PMECC_CFG_BCH_ERR12;
1034 break;
1035 case 24:
1036 val = PMECC_CFG_BCH_ERR24;
1037 break;
Romain Izard94248462016-02-10 10:56:26 +01001038 case 32:
1039 val = PMECC_CFG_BCH_ERR32;
1040 break;
Josh Wu1c7b8742012-06-29 17:47:55 +08001041 }
1042
1043 if (host->pmecc_sector_size == 512)
1044 val |= PMECC_CFG_SECTOR512;
1045 else if (host->pmecc_sector_size == 1024)
1046 val |= PMECC_CFG_SECTOR1024;
1047
Wu, Joshc9447ff2014-08-08 17:12:34 +08001048 switch (nand_chip->ecc.steps) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001049 case 1:
1050 val |= PMECC_CFG_PAGE_1SECTOR;
1051 break;
1052 case 2:
1053 val |= PMECC_CFG_PAGE_2SECTORS;
1054 break;
1055 case 4:
1056 val |= PMECC_CFG_PAGE_4SECTORS;
1057 break;
1058 case 8:
1059 val |= PMECC_CFG_PAGE_8SECTORS;
1060 break;
1061 }
1062
1063 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
1064 | PMECC_CFG_AUTO_DISABLE);
1065 pmecc_writel(host->ecc, CFG, val);
1066
1067 ecc_layout = nand_chip->ecc.layout;
1068 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
Boris Brezillon78d28e82016-02-03 20:11:14 +01001069 mtd_ooblayout_ecc(mtd, 0, &oobregion);
1070 pmecc_writel(host->ecc, SADDR, oobregion.offset);
Josh Wu1c7b8742012-06-29 17:47:55 +08001071 pmecc_writel(host->ecc, EADDR,
Boris Brezillon78d28e82016-02-03 20:11:14 +01001072 oobregion.offset + ecc_layout->eccbytes - 1);
Josh Wu1c7b8742012-06-29 17:47:55 +08001073 /* See datasheet about PMECC Clock Control Register */
1074 pmecc_writel(host->ecc, CLK, 2);
1075 pmecc_writel(host->ecc, IDR, 0xff);
1076 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
1077}
1078
Josh Wu84cfbbb2013-01-23 20:47:12 +08001079/*
Josh Wu2a3d9332013-09-18 13:58:48 +08001080 * Get minimum ecc requirements from NAND.
Josh Wu84cfbbb2013-01-23 20:47:12 +08001081 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
Josh Wu2a3d9332013-09-18 13:58:48 +08001082 * will set them according to minimum ecc requirement. Otherwise, use the
Josh Wu84cfbbb2013-01-23 20:47:12 +08001083 * value in DTS file.
1084 * return 0 if success. otherwise return error code.
1085 */
1086static int pmecc_choose_ecc(struct atmel_nand_host *host,
1087 int *cap, int *sector_size)
1088{
Josh Wu2a3d9332013-09-18 13:58:48 +08001089 /* Get minimum ECC requirements */
1090 if (host->nand_chip.ecc_strength_ds) {
1091 *cap = host->nand_chip.ecc_strength_ds;
1092 *sector_size = host->nand_chip.ecc_step_ds;
1093 dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n",
Josh Wu84cfbbb2013-01-23 20:47:12 +08001094 *cap, *sector_size);
Josh Wu84cfbbb2013-01-23 20:47:12 +08001095 } else {
Josh Wu84cfbbb2013-01-23 20:47:12 +08001096 *cap = 2;
1097 *sector_size = 512;
Josh Wu2a3d9332013-09-18 13:58:48 +08001098 dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
Josh Wu84cfbbb2013-01-23 20:47:12 +08001099 }
1100
Josh Wu2a3d9332013-09-18 13:58:48 +08001101 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
Josh Wu84cfbbb2013-01-23 20:47:12 +08001102 if (host->pmecc_corr_cap == 0) {
Romain Izard94248462016-02-10 10:56:26 +01001103 if (*cap > host->caps->pmecc_max_correction)
1104 return -EINVAL;
1105
Josh Wu84cfbbb2013-01-23 20:47:12 +08001106 /* use the most fitable ecc bits (the near bigger one ) */
1107 if (*cap <= 2)
1108 host->pmecc_corr_cap = 2;
1109 else if (*cap <= 4)
1110 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +08001111 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001112 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +08001113 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001114 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +08001115 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001116 host->pmecc_corr_cap = 24;
Romain Izard94248462016-02-10 10:56:26 +01001117 else if (*cap <= 32)
1118 host->pmecc_corr_cap = 32;
Josh Wu84cfbbb2013-01-23 20:47:12 +08001119 else
1120 return -EINVAL;
1121 }
1122 if (host->pmecc_sector_size == 0) {
1123 /* use the most fitable sector size (the near smaller one ) */
1124 if (*sector_size >= 1024)
1125 host->pmecc_sector_size = 1024;
1126 else if (*sector_size >= 512)
1127 host->pmecc_sector_size = 512;
1128 else
1129 return -EINVAL;
1130 }
1131 return 0;
1132}
1133
Josh Wuabb1cd02014-10-11 18:01:50 +08001134static inline int deg(unsigned int poly)
1135{
1136 /* polynomial degree is the most-significant bit index */
1137 return fls(poly) - 1;
1138}
1139
1140static int build_gf_tables(int mm, unsigned int poly,
1141 int16_t *index_of, int16_t *alpha_to)
1142{
1143 unsigned int i, x = 1;
1144 const unsigned int k = 1 << deg(poly);
1145 unsigned int nn = (1 << mm) - 1;
1146
1147 /* primitive polynomial must be of degree m */
1148 if (k != (1u << mm))
1149 return -EINVAL;
1150
1151 for (i = 0; i < nn; i++) {
1152 alpha_to[i] = x;
1153 index_of[x] = i;
1154 if (i && (x == 1))
1155 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
1156 return -EINVAL;
1157 x <<= 1;
1158 if (x & k)
1159 x ^= poly;
1160 }
1161 alpha_to[nn] = 1;
1162 index_of[0] = 0;
1163
1164 return 0;
1165}
1166
1167static uint16_t *create_lookup_table(struct device *dev, int sector_size)
1168{
1169 int degree = (sector_size == 512) ?
1170 PMECC_GF_DIMENSION_13 :
1171 PMECC_GF_DIMENSION_14;
1172 unsigned int poly = (sector_size == 512) ?
1173 PMECC_GF_13_PRIMITIVE_POLY :
1174 PMECC_GF_14_PRIMITIVE_POLY;
1175 int table_size = (sector_size == 512) ?
1176 PMECC_LOOKUP_TABLE_SIZE_512 :
1177 PMECC_LOOKUP_TABLE_SIZE_1024;
1178
1179 int16_t *addr = devm_kzalloc(dev, 2 * table_size * sizeof(uint16_t),
1180 GFP_KERNEL);
1181 if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
1182 return NULL;
1183
1184 return addr;
1185}
1186
Johan Hovold2c2b9282013-09-23 16:27:28 +02001187static int atmel_pmecc_nand_init_params(struct platform_device *pdev,
Josh Wu1c7b8742012-06-29 17:47:55 +08001188 struct atmel_nand_host *host)
1189{
Josh Wu1c7b8742012-06-29 17:47:55 +08001190 struct nand_chip *nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01001191 struct mtd_info *mtd = nand_to_mtd(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +08001192 struct resource *regs, *regs_pmerr, *regs_rom;
Josh Wuabb1cd02014-10-11 18:01:50 +08001193 uint16_t *galois_table;
Josh Wu1c7b8742012-06-29 17:47:55 +08001194 int cap, sector_size, err_no;
1195
Josh Wu84cfbbb2013-01-23 20:47:12 +08001196 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
1197 if (err_no) {
1198 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1199 return err_no;
1200 }
1201
Richard Genoudf666d642013-07-30 17:17:29 +02001202 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001203 sector_size != host->pmecc_sector_size)
1204 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001205
Josh Wu1c7b8742012-06-29 17:47:55 +08001206 cap = host->pmecc_corr_cap;
1207 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001208 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1209 host->pmecc_lookup_table_offset_512 :
1210 host->pmecc_lookup_table_offset_1024;
1211
Josh Wu1c7b8742012-06-29 17:47:55 +08001212 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1213 cap, sector_size);
1214
1215 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1216 if (!regs) {
1217 dev_warn(host->dev,
1218 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1219 nand_chip->ecc.mode = NAND_ECC_SOFT;
1220 return 0;
1221 }
1222
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001223 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1224 if (IS_ERR(host->ecc)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001225 err_no = PTR_ERR(host->ecc);
1226 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001227 }
1228
1229 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001230 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1231 if (IS_ERR(host->pmerrloc_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001232 err_no = PTR_ERR(host->pmerrloc_base);
1233 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001234 }
Romain Izard55750752016-02-10 10:56:25 +01001235 host->pmerrloc_el_base = host->pmerrloc_base + ATMEL_PMERRLOC_SIGMAx +
1236 (host->caps->pmecc_max_correction + 1) * 4;
Josh Wu1c7b8742012-06-29 17:47:55 +08001237
Wu, Josh41c75402015-04-02 14:13:47 +08001238 if (!host->has_no_lookup_table) {
1239 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1240 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev,
1241 regs_rom);
1242 if (IS_ERR(host->pmecc_rom_base)) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001243 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 +08001244 host->has_no_lookup_table = true;
1245 }
Josh Wuabb1cd02014-10-11 18:01:50 +08001246 }
1247
1248 if (host->has_no_lookup_table) {
1249 /* Build the look-up table in runtime */
1250 galois_table = create_lookup_table(host->dev, sector_size);
1251 if (!galois_table) {
1252 dev_err(host->dev, "Failed to build a lookup table in runtime!\n");
1253 err_no = -EINVAL;
1254 goto err;
1255 }
1256
1257 host->pmecc_rom_base = (void __iomem *)galois_table;
1258 host->pmecc_lookup_table_offset = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +08001259 }
1260
Bo Shenb3857662014-06-12 15:58:45 +08001261 nand_chip->ecc.size = sector_size;
Josh Wu1c7b8742012-06-29 17:47:55 +08001262
1263 /* set ECC page size and oob layout */
1264 switch (mtd->writesize) {
Wu, Josha3557102014-07-22 17:24:18 +08001265 case 512:
1266 case 1024:
Josh Wu1c7b8742012-06-29 17:47:55 +08001267 case 2048:
Wu, Josha3557102014-07-22 17:24:18 +08001268 case 4096:
1269 case 8192:
1270 if (sector_size > mtd->writesize) {
1271 dev_err(host->dev, "pmecc sector size is bigger than the page size!\n");
1272 err_no = -EINVAL;
1273 goto err;
1274 }
1275
Josh Wu2fa831f2013-08-19 18:05:44 +08001276 host->pmecc_degree = (sector_size == 512) ?
1277 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Josh Wu1c7b8742012-06-29 17:47:55 +08001278 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +08001279 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1280 host->pmecc_index_of = host->pmecc_rom_base +
1281 host->pmecc_lookup_table_offset;
1282
Josh Wu1c7b8742012-06-29 17:47:55 +08001283 nand_chip->ecc.strength = cap;
Wu, Josh022a4782014-08-08 17:12:35 +08001284 nand_chip->ecc.bytes = pmecc_get_ecc_bytes(cap, sector_size);
Wu, Joshc9447ff2014-08-08 17:12:34 +08001285 nand_chip->ecc.steps = mtd->writesize / sector_size;
1286 nand_chip->ecc.total = nand_chip->ecc.bytes *
1287 nand_chip->ecc.steps;
Josh Wu477478a2015-04-02 14:12:33 +08001288 if (nand_chip->ecc.total >
1289 mtd->oobsize - PMECC_OOB_RESERVED_BYTES) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001290 dev_err(host->dev, "No room for ECC bytes\n");
1291 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001292 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001293 }
1294 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1295 mtd->oobsize,
Bo Shenb3857662014-06-12 15:58:45 +08001296 nand_chip->ecc.total);
1297
Josh Wu1c7b8742012-06-29 17:47:55 +08001298 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1299 break;
Wu, Josha3557102014-07-22 17:24:18 +08001300 default:
Josh Wu1c7b8742012-06-29 17:47:55 +08001301 dev_warn(host->dev,
1302 "Unsupported page size for PMECC, use Software ECC\n");
Josh Wu1c7b8742012-06-29 17:47:55 +08001303 /* page size not handled by HW ECC */
1304 /* switching back to soft ECC */
1305 nand_chip->ecc.mode = NAND_ECC_SOFT;
1306 return 0;
1307 }
1308
1309 /* Allocate data for PMECC computation */
1310 err_no = pmecc_data_alloc(host);
1311 if (err_no) {
1312 dev_err(host->dev,
1313 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001314 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001315 }
1316
Herve Codina90445ff2014-03-03 12:15:29 +01001317 nand_chip->options |= NAND_NO_SUBPAGE_WRITE;
Josh Wu1c7b8742012-06-29 17:47:55 +08001318 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1319 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1320
1321 atmel_pmecc_core_init(mtd);
1322
1323 return 0;
1324
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001325err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001326 return err_no;
1327}
1328
1329/*
Richard Genoud77f54922008-04-23 19:51:14 +02001330 * Calculate HW ECC
1331 *
1332 * function called after a write
1333 *
1334 * mtd: MTD block structure
1335 * dat: raw data (unused)
1336 * ecc_code: buffer for ECC
1337 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001338static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001339 const u_char *dat, unsigned char *ecc_code)
1340{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001341 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001342 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02001343 unsigned int ecc_value;
1344
1345 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001346 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001347
Richard Genoud3fc23892008-10-12 08:42:28 +02001348 ecc_code[0] = ecc_value & 0xFF;
1349 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001350
1351 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001352 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001353
Richard Genoud3fc23892008-10-12 08:42:28 +02001354 ecc_code[2] = ecc_value & 0xFF;
1355 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001356
1357 return 0;
1358}
1359
1360/*
1361 * HW ECC read page function
1362 *
1363 * mtd: mtd info structure
1364 * chip: nand chip info structure
1365 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001366 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001367 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001368static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1369 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001370{
1371 int eccsize = chip->ecc.size;
1372 int eccbytes = chip->ecc.bytes;
Richard Genoud77f54922008-04-23 19:51:14 +02001373 uint8_t *p = buf;
1374 uint8_t *oob = chip->oob_poi;
1375 uint8_t *ecc_pos;
1376 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001377 unsigned int max_bitflips = 0;
Boris Brezillon78d28e82016-02-03 20:11:14 +01001378 struct mtd_oob_region oobregion = {};
Richard Genoud77f54922008-04-23 19:51:14 +02001379
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001380 /*
1381 * Errata: ALE is incorrectly wired up to the ECC controller
1382 * on the AP7000, so it will include the address cycles in the
1383 * ECC calculation.
1384 *
1385 * Workaround: Reset the parity registers before reading the
1386 * actual data.
1387 */
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001388 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu71b94e22013-05-09 15:34:54 +08001389 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001390 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001391
Richard Genoud77f54922008-04-23 19:51:14 +02001392 /* read the page */
1393 chip->read_buf(mtd, p, eccsize);
1394
1395 /* move to ECC position if needed */
Boris Brezillon78d28e82016-02-03 20:11:14 +01001396 mtd_ooblayout_ecc(mtd, 0, &oobregion);
1397 if (oobregion.offset != 0) {
1398 /*
1399 * This only works on large pages because the ECC controller
1400 * waits for NAND_CMD_RNDOUTSTART after the NAND_CMD_RNDOUT.
1401 * Anyway, for small pages, the first ECC byte is at offset
1402 * 0 in the OOB area.
Richard Genoud77f54922008-04-23 19:51:14 +02001403 */
1404 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon78d28e82016-02-03 20:11:14 +01001405 mtd->writesize + oobregion.offset, -1);
Richard Genoud77f54922008-04-23 19:51:14 +02001406 }
1407
1408 /* the ECC controller needs to read the ECC just after the data */
Boris Brezillon78d28e82016-02-03 20:11:14 +01001409 ecc_pos = oob + oobregion.offset;
Richard Genoud77f54922008-04-23 19:51:14 +02001410 chip->read_buf(mtd, ecc_pos, eccbytes);
1411
1412 /* check if there's an error */
1413 stat = chip->ecc.correct(mtd, p, oob, NULL);
1414
Mike Dunn3f91e942012-04-25 12:06:09 -07001415 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001416 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001417 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001418 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001419 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1420 }
Richard Genoud77f54922008-04-23 19:51:14 +02001421
1422 /* get back to oob start (end of page) */
1423 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1424
1425 /* read the oob */
1426 chip->read_buf(mtd, oob, mtd->oobsize);
1427
Mike Dunn3f91e942012-04-25 12:06:09 -07001428 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001429}
1430
1431/*
1432 * HW ECC Correction
1433 *
1434 * function called after a read
1435 *
1436 * mtd: MTD block structure
1437 * dat: raw data read from the chip
1438 * read_ecc: ECC from the chip (unused)
1439 * isnull: unused
1440 *
1441 * Detect and correct a 1 bit error for a page
1442 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001443static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001444 u_char *read_ecc, u_char *isnull)
1445{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001446 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001447 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02001448 unsigned int ecc_status;
1449 unsigned int ecc_word, ecc_bit;
1450
1451 /* get the status from the Status Register */
1452 ecc_status = ecc_readl(host->ecc, SR);
1453
1454 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001455 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001456 return 0;
1457
1458 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001459 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001460 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001461 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001462 ecc_word >>= 4;
1463
1464 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001465 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001466 /* check if it is a freshly erased block
1467 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001468 if ((ecc_bit == ATMEL_ECC_BITADDR)
1469 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001470 /* the block has just been erased, return OK */
1471 return 0;
1472 }
1473 /* it doesn't seems to be a freshly
1474 * erased block.
1475 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001476 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001477 " Unable to correct.\n");
Boris BREZILLON6e941192015-12-30 20:32:03 +01001478 return -EBADMSG;
Richard Genoud77f54922008-04-23 19:51:14 +02001479 }
1480
1481 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001482 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001483 /* there's nothing much to do here.
1484 * the bit error is on the ECC itself.
1485 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001486 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001487 " Nothing to correct\n");
1488 return 0;
1489 }
1490
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001491 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001492 " (word offset in the page :"
1493 " 0x%x bit offset : 0x%x)\n",
1494 ecc_word, ecc_bit);
1495 /* correct the error */
1496 if (nand_chip->options & NAND_BUSWIDTH_16) {
1497 /* 16 bits words */
1498 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1499 } else {
1500 /* 8 bits words */
1501 dat[ecc_word] ^= (1 << ecc_bit);
1502 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001503 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001504 return 1;
1505}
1506
1507/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001508 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001509 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001510static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1511{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001512 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001513 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu71b94e22013-05-09 15:34:54 +08001514
1515 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001516 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001517}
Richard Genoud77f54922008-04-23 19:51:14 +02001518
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001519static int atmel_of_init_ecc(struct atmel_nand_host *host,
1520 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001521{
Josh Wua41b51a2012-06-29 17:47:54 +08001522 u32 offset[2];
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001523 u32 val;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001524
Josh Wua41b51a2012-06-29 17:47:54 +08001525 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1526
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001527 /* Not using PMECC */
1528 if (!(host->nand_chip.ecc.mode == NAND_ECC_HW) || !host->has_pmecc)
1529 return 0;
Josh Wua41b51a2012-06-29 17:47:54 +08001530
1531 /* use PMECC, get correction capability, sector size and lookup
1532 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001533 * If correction bits and sector size are not specified, then find
1534 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001535 */
Josh Wue66b4312013-01-23 20:47:11 +08001536 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
Romain Izard94248462016-02-10 10:56:26 +01001537 if (val > host->caps->pmecc_max_correction) {
1538 dev_err(host->dev,
1539 "Required ECC strength too high: %u max %u\n",
1540 val, host->caps->pmecc_max_correction);
1541 return -EINVAL;
1542 }
1543 if ((val != 2) && (val != 4) && (val != 8) &&
1544 (val != 12) && (val != 24) && (val != 32)) {
Josh Wue66b4312013-01-23 20:47:11 +08001545 dev_err(host->dev,
Romain Izarde88b7f72016-01-15 11:34:56 +01001546 "Required ECC strength not supported: %u\n",
Josh Wue66b4312013-01-23 20:47:11 +08001547 val);
1548 return -EINVAL;
1549 }
1550 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001551 }
Josh Wua41b51a2012-06-29 17:47:54 +08001552
Josh Wue66b4312013-01-23 20:47:11 +08001553 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1554 if ((val != 512) && (val != 1024)) {
1555 dev_err(host->dev,
Romain Izarde88b7f72016-01-15 11:34:56 +01001556 "Required ECC sector size not supported: %u\n",
Josh Wue66b4312013-01-23 20:47:11 +08001557 val);
1558 return -EINVAL;
1559 }
1560 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001561 }
Josh Wua41b51a2012-06-29 17:47:54 +08001562
1563 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1564 offset, 2) != 0) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001565 dev_err(host->dev, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n");
1566 host->has_no_lookup_table = true;
1567 /* Will build a lookup table and initialize the offset later */
1568 return 0;
Josh Wua41b51a2012-06-29 17:47:54 +08001569 }
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001570
Josh Wuc0cf7872013-01-23 20:47:08 +08001571 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001572 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1573 return -EINVAL;
1574 }
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001575
Josh Wue66b4312013-01-23 20:47:11 +08001576 host->pmecc_lookup_table_offset_512 = offset[0];
1577 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001578
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001579 return 0;
1580}
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001581
Boris Brezillon4f3cab92016-04-01 14:54:22 +02001582static int atmel_of_init_port(struct atmel_nand_host *host,
1583 struct device_node *np)
1584{
1585 u32 val;
1586 struct atmel_nand_data *board = &host->board;
1587 enum of_gpio_flags flags = 0;
1588
1589 host->caps = (struct atmel_nand_caps *)
1590 of_device_get_match_data(host->dev);
1591
1592 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1593 if (val >= 32) {
1594 dev_err(host->dev, "invalid addr-offset %u\n", val);
1595 return -EINVAL;
1596 }
1597 board->ale = val;
1598 }
1599
1600 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1601 if (val >= 32) {
1602 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1603 return -EINVAL;
1604 }
1605 board->cle = val;
1606 }
1607
1608 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1609
1610 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1611 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1612
1613 board->enable_pin = of_get_gpio(np, 1);
1614 board->det_pin = of_get_gpio(np, 2);
1615
1616 /* load the nfc driver if there is */
1617 of_platform_populate(np, NULL, NULL, host->dev);
1618
1619 /*
1620 * Initialize ECC mode to NAND_ECC_SOFT so that we have a correct value
1621 * even if the nand-ecc-mode property is not defined.
1622 */
1623 host->nand_chip.ecc.mode = NAND_ECC_SOFT;
1624
1625 return 0;
1626}
1627
Johan Hovold2c2b9282013-09-23 16:27:28 +02001628static int atmel_hw_nand_init_params(struct platform_device *pdev,
Josh Wu3dfe41a2012-06-25 18:07:43 +08001629 struct atmel_nand_host *host)
1630{
Josh Wu3dfe41a2012-06-25 18:07:43 +08001631 struct nand_chip *nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01001632 struct mtd_info *mtd = nand_to_mtd(nand_chip);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001633 struct resource *regs;
1634
1635 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1636 if (!regs) {
1637 dev_err(host->dev,
1638 "Can't get I/O resource regs, use software ECC\n");
1639 nand_chip->ecc.mode = NAND_ECC_SOFT;
1640 return 0;
1641 }
1642
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001643 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun8fb7b932014-07-28 21:19:55 +08001644 if (IS_ERR(host->ecc))
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001645 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001646
1647 /* ECC is calculated for the whole page (1 step) */
1648 nand_chip->ecc.size = mtd->writesize;
1649
1650 /* set ECC page size and oob layout */
1651 switch (mtd->writesize) {
1652 case 512:
1653 nand_chip->ecc.layout = &atmel_oobinfo_small;
1654 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1655 break;
1656 case 1024:
1657 nand_chip->ecc.layout = &atmel_oobinfo_large;
1658 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1659 break;
1660 case 2048:
1661 nand_chip->ecc.layout = &atmel_oobinfo_large;
1662 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1663 break;
1664 case 4096:
1665 nand_chip->ecc.layout = &atmel_oobinfo_large;
1666 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1667 break;
1668 default:
1669 /* page size not handled by HW ECC */
1670 /* switching back to soft ECC */
1671 nand_chip->ecc.mode = NAND_ECC_SOFT;
1672 return 0;
1673 }
1674
1675 /* set up for HW ECC */
1676 nand_chip->ecc.calculate = atmel_nand_calculate;
1677 nand_chip->ecc.correct = atmel_nand_correct;
1678 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1679 nand_chip->ecc.read_page = atmel_nand_read_page;
1680 nand_chip->ecc.bytes = 4;
1681 nand_chip->ecc.strength = 1;
1682
1683 return 0;
1684}
1685
Wu, Josh50e04e22014-06-10 17:50:09 +08001686static inline u32 nfc_read_status(struct atmel_nand_host *host)
1687{
1688 u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
1689 u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
1690
1691 if (unlikely(nfc_status & err_flags)) {
1692 if (nfc_status & NFC_SR_DTOE)
1693 dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
1694 else if (nfc_status & NFC_SR_UNDEF)
1695 dev_err(host->dev, "NFC: Access Undefined Area Error\n");
1696 else if (nfc_status & NFC_SR_AWB)
1697 dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
1698 else if (nfc_status & NFC_SR_ASE)
1699 dev_err(host->dev, "NFC: Access memory Size Error\n");
1700 }
1701
1702 return nfc_status;
1703}
1704
Josh Wu7dc37de2013-08-05 19:14:35 +08001705/* SMC interrupt service routine */
1706static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
1707{
1708 struct atmel_nand_host *host = dev_id;
1709 u32 status, mask, pending;
Josh Wue4e06932014-06-10 17:50:11 +08001710 irqreturn_t ret = IRQ_NONE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001711
Wu, Josh50e04e22014-06-10 17:50:09 +08001712 status = nfc_read_status(host);
Josh Wu7dc37de2013-08-05 19:14:35 +08001713 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1714 pending = status & mask;
1715
1716 if (pending & NFC_SR_XFR_DONE) {
Josh Wue4e06932014-06-10 17:50:11 +08001717 complete(&host->nfc->comp_xfer_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001718 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001719 ret = IRQ_HANDLED;
1720 }
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001721 if (pending & host->nfc->caps->rb_mask) {
Josh Wue4e06932014-06-10 17:50:11 +08001722 complete(&host->nfc->comp_ready);
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001723 nfc_writel(host->nfc->hsmc_regs, IDR, host->nfc->caps->rb_mask);
Josh Wue4e06932014-06-10 17:50:11 +08001724 ret = IRQ_HANDLED;
1725 }
1726 if (pending & NFC_SR_CMD_DONE) {
1727 complete(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001728 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001729 ret = IRQ_HANDLED;
Josh Wu7dc37de2013-08-05 19:14:35 +08001730 }
1731
1732 return ret;
1733}
1734
1735/* NFC(Nand Flash Controller) related functions */
Josh Wue4e06932014-06-10 17:50:11 +08001736static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag)
Josh Wu7dc37de2013-08-05 19:14:35 +08001737{
Josh Wue4e06932014-06-10 17:50:11 +08001738 if (flag & NFC_SR_XFR_DONE)
1739 init_completion(&host->nfc->comp_xfer_done);
1740
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001741 if (flag & host->nfc->caps->rb_mask)
Josh Wue4e06932014-06-10 17:50:11 +08001742 init_completion(&host->nfc->comp_ready);
1743
1744 if (flag & NFC_SR_CMD_DONE)
1745 init_completion(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001746
1747 /* Enable interrupt that need to wait for */
1748 nfc_writel(host->nfc->hsmc_regs, IER, flag);
Josh Wue4e06932014-06-10 17:50:11 +08001749}
Josh Wu7dc37de2013-08-05 19:14:35 +08001750
Josh Wue4e06932014-06-10 17:50:11 +08001751static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
1752{
1753 int i, index = 0;
1754 struct completion *comp[3]; /* Support 3 interrupt completion */
Josh Wu7dc37de2013-08-05 19:14:35 +08001755
Josh Wue4e06932014-06-10 17:50:11 +08001756 if (flag & NFC_SR_XFR_DONE)
1757 comp[index++] = &host->nfc->comp_xfer_done;
1758
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001759 if (flag & host->nfc->caps->rb_mask)
Josh Wue4e06932014-06-10 17:50:11 +08001760 comp[index++] = &host->nfc->comp_ready;
1761
1762 if (flag & NFC_SR_CMD_DONE)
1763 comp[index++] = &host->nfc->comp_cmd_done;
1764
1765 if (index == 0) {
Colin Ian King393d23c2015-02-28 20:27:56 +00001766 dev_err(host->dev, "Unknown interrupt flag: 0x%08x\n", flag);
Josh Wue4e06932014-06-10 17:50:11 +08001767 return -EINVAL;
1768 }
1769
1770 for (i = 0; i < index; i++) {
1771 if (wait_for_completion_timeout(comp[i],
1772 msecs_to_jiffies(NFC_TIME_OUT_MS)))
1773 continue; /* wait for next completion */
1774 else
1775 goto err_timeout;
1776 }
1777
1778 return 0;
1779
1780err_timeout:
Josh Wu7dc37de2013-08-05 19:14:35 +08001781 dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
Josh Wue4e06932014-06-10 17:50:11 +08001782 /* Disable the interrupt as it is not handled by interrupt handler */
1783 nfc_writel(host->nfc->hsmc_regs, IDR, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001784 return -ETIMEDOUT;
1785}
1786
1787static int nfc_send_command(struct atmel_nand_host *host,
1788 unsigned int cmd, unsigned int addr, unsigned char cycle0)
1789{
1790 unsigned long timeout;
Josh Wue4e06932014-06-10 17:50:11 +08001791 u32 flag = NFC_SR_CMD_DONE;
1792 flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0;
1793
Josh Wu7dc37de2013-08-05 19:14:35 +08001794 dev_dbg(host->dev,
1795 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1796 cmd, addr, cycle0);
1797
1798 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
Boris Brezillon111573cc2015-01-22 18:21:30 +08001799 while (nfc_readl(host->nfc->hsmc_regs, SR) & NFC_SR_BUSY) {
Josh Wu7dc37de2013-08-05 19:14:35 +08001800 if (time_after(jiffies, timeout)) {
1801 dev_err(host->dev,
Boris Brezillon111573cc2015-01-22 18:21:30 +08001802 "Time out to wait for NFC ready!\n");
Josh Wu7dc37de2013-08-05 19:14:35 +08001803 return -ETIMEDOUT;
1804 }
1805 }
Josh Wue4e06932014-06-10 17:50:11 +08001806
1807 nfc_prepare_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001808 nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
1809 nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
Josh Wue4e06932014-06-10 17:50:11 +08001810 return nfc_wait_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001811}
1812
1813static int nfc_device_ready(struct mtd_info *mtd)
1814{
Wu, Josh72a78e32014-06-10 17:50:10 +08001815 u32 status, mask;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001816 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001817 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Wu, Josh72a78e32014-06-10 17:50:10 +08001818
1819 status = nfc_read_status(host);
1820 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1821
1822 /* The mask should be 0. If not we may lost interrupts */
1823 if (unlikely(mask & status))
1824 dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n",
1825 mask & status);
1826
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001827 return status & host->nfc->caps->rb_mask;
Josh Wu7dc37de2013-08-05 19:14:35 +08001828}
1829
1830static void nfc_select_chip(struct mtd_info *mtd, int chip)
1831{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001832 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001833 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu7dc37de2013-08-05 19:14:35 +08001834
1835 if (chip == -1)
1836 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
1837 else
1838 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
1839}
1840
Brian Norris3dad2342014-01-29 14:08:12 -08001841static int nfc_make_addr(struct mtd_info *mtd, int command, int column,
1842 int page_addr, unsigned int *addr1234, unsigned int *cycle0)
Josh Wu7dc37de2013-08-05 19:14:35 +08001843{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001844 struct nand_chip *chip = mtd_to_nand(mtd);
Josh Wu7dc37de2013-08-05 19:14:35 +08001845
1846 int acycle = 0;
1847 unsigned char addr_bytes[8];
1848 int index = 0, bit_shift;
1849
1850 BUG_ON(addr1234 == NULL || cycle0 == NULL);
1851
1852 *cycle0 = 0;
1853 *addr1234 = 0;
1854
1855 if (column != -1) {
Brian Norris3dad2342014-01-29 14:08:12 -08001856 if (chip->options & NAND_BUSWIDTH_16 &&
1857 !nand_opcode_8bits(command))
Josh Wu7dc37de2013-08-05 19:14:35 +08001858 column >>= 1;
1859 addr_bytes[acycle++] = column & 0xff;
1860 if (mtd->writesize > 512)
1861 addr_bytes[acycle++] = (column >> 8) & 0xff;
1862 }
1863
1864 if (page_addr != -1) {
1865 addr_bytes[acycle++] = page_addr & 0xff;
1866 addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
1867 if (chip->chipsize > (128 << 20))
1868 addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
1869 }
1870
1871 if (acycle > 4)
1872 *cycle0 = addr_bytes[index++];
1873
1874 for (bit_shift = 0; index < acycle; bit_shift += 8)
1875 *addr1234 += addr_bytes[index++] << bit_shift;
1876
1877 /* return acycle in cmd register */
1878 return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
1879}
1880
1881static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
1882 int column, int page_addr)
1883{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001884 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001885 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu7dc37de2013-08-05 19:14:35 +08001886 unsigned long timeout;
1887 unsigned int nfc_addr_cmd = 0;
1888
1889 unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1890
1891 /* Set default settings: no cmd2, no addr cycle. read from nand */
1892 unsigned int cmd2 = 0;
1893 unsigned int vcmd2 = 0;
1894 int acycle = NFCADDR_CMD_ACYCLE_NONE;
1895 int csid = NFCADDR_CMD_CSID_3;
1896 int dataen = NFCADDR_CMD_DATADIS;
1897 int nfcwr = NFCADDR_CMD_NFCRD;
1898 unsigned int addr1234 = 0;
1899 unsigned int cycle0 = 0;
1900 bool do_addr = true;
Josh Wu1ae9c092013-08-05 19:14:36 +08001901 host->nfc->data_in_sram = NULL;
Josh Wu7dc37de2013-08-05 19:14:35 +08001902
1903 dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1904 __func__, command, column, page_addr);
1905
1906 switch (command) {
1907 case NAND_CMD_RESET:
1908 nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
1909 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1910 udelay(chip->chip_delay);
1911
1912 nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
1913 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1914 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
1915 if (time_after(jiffies, timeout)) {
1916 dev_err(host->dev,
1917 "Time out to wait status ready!\n");
1918 break;
1919 }
1920 }
1921 return;
1922 case NAND_CMD_STATUS:
1923 do_addr = false;
1924 break;
1925 case NAND_CMD_PARAM:
1926 case NAND_CMD_READID:
1927 do_addr = false;
1928 acycle = NFCADDR_CMD_ACYCLE_1;
1929 if (column != -1)
1930 addr1234 = column;
1931 break;
1932 case NAND_CMD_RNDOUT:
1933 cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
1934 vcmd2 = NFCADDR_CMD_VCMD2;
1935 break;
1936 case NAND_CMD_READ0:
1937 case NAND_CMD_READOOB:
1938 if (command == NAND_CMD_READOOB) {
1939 column += mtd->writesize;
1940 command = NAND_CMD_READ0; /* only READ0 is valid */
1941 cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1942 }
Josh Wu1ae9c092013-08-05 19:14:36 +08001943 if (host->nfc->use_nfc_sram) {
1944 /* Enable Data transfer to sram */
1945 dataen = NFCADDR_CMD_DATAEN;
1946
1947 /* Need enable PMECC now, since NFC will transfer
1948 * data in bus after sending nfc read command.
1949 */
1950 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1951 pmecc_enable(host, NAND_ECC_READ);
1952 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001953
1954 cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
1955 vcmd2 = NFCADDR_CMD_VCMD2;
1956 break;
1957 /* For prgramming command, the cmd need set to write enable */
1958 case NAND_CMD_PAGEPROG:
1959 case NAND_CMD_SEQIN:
1960 case NAND_CMD_RNDIN:
1961 nfcwr = NFCADDR_CMD_NFCWR;
Josh Wu6054d4d2013-08-05 19:14:37 +08001962 if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
1963 dataen = NFCADDR_CMD_DATAEN;
Josh Wu7dc37de2013-08-05 19:14:35 +08001964 break;
1965 default:
1966 break;
1967 }
1968
1969 if (do_addr)
Brian Norris3dad2342014-01-29 14:08:12 -08001970 acycle = nfc_make_addr(mtd, command, column, page_addr,
1971 &addr1234, &cycle0);
Josh Wu7dc37de2013-08-05 19:14:35 +08001972
1973 nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
1974 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1975
1976 /*
1977 * Program and erase have their own busy handlers status, sequential
1978 * in, and deplete1 need no delay.
1979 */
1980 switch (command) {
1981 case NAND_CMD_CACHEDPROG:
1982 case NAND_CMD_PAGEPROG:
1983 case NAND_CMD_ERASE1:
1984 case NAND_CMD_ERASE2:
1985 case NAND_CMD_RNDIN:
1986 case NAND_CMD_STATUS:
1987 case NAND_CMD_RNDOUT:
1988 case NAND_CMD_SEQIN:
1989 case NAND_CMD_READID:
1990 return;
1991
1992 case NAND_CMD_READ0:
Josh Wu1ae9c092013-08-05 19:14:36 +08001993 if (dataen == NFCADDR_CMD_DATAEN) {
1994 host->nfc->data_in_sram = host->nfc->sram_bank0 +
1995 nfc_get_sram_off(host);
1996 return;
1997 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001998 /* fall through */
1999 default:
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002000 nfc_prepare_interrupt(host, host->nfc->caps->rb_mask);
2001 nfc_wait_interrupt(host, host->nfc->caps->rb_mask);
Josh Wu7dc37de2013-08-05 19:14:35 +08002002 }
2003}
2004
Josh Wu6054d4d2013-08-05 19:14:37 +08002005static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
2006 uint32_t offset, int data_len, const uint8_t *buf,
2007 int oob_required, int page, int cached, int raw)
2008{
2009 int cfg, len;
2010 int status = 0;
Boris BREZILLONd699ed22015-12-10 09:00:41 +01002011 struct atmel_nand_host *host = nand_get_controller_data(chip);
Wu, Josh068b44b2014-11-07 15:26:09 +08002012 void *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
Josh Wu6054d4d2013-08-05 19:14:37 +08002013
2014 /* Subpage write is not supported */
2015 if (offset || (data_len < mtd->writesize))
2016 return -EINVAL;
2017
Josh Wu6054d4d2013-08-05 19:14:37 +08002018 len = mtd->writesize;
Josh Wu6054d4d2013-08-05 19:14:37 +08002019 /* Copy page data to sram that will write to nand via NFC */
2020 if (use_dma) {
2021 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
2022 /* Fall back to use cpu copy */
Wu, Josh068b44b2014-11-07 15:26:09 +08002023 memcpy(sram, buf, len);
Josh Wu6054d4d2013-08-05 19:14:37 +08002024 } else {
Wu, Josh068b44b2014-11-07 15:26:09 +08002025 memcpy(sram, buf, len);
Josh Wu6054d4d2013-08-05 19:14:37 +08002026 }
2027
Wu, Joshff0a2152014-08-05 18:38:52 +08002028 cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
2029 if (unlikely(raw) && oob_required) {
Wu, Josh068b44b2014-11-07 15:26:09 +08002030 memcpy(sram + len, chip->oob_poi, mtd->oobsize);
Wu, Joshff0a2152014-08-05 18:38:52 +08002031 len += mtd->oobsize;
2032 nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
2033 } else {
2034 nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
2035 }
2036
Josh Wu6054d4d2013-08-05 19:14:37 +08002037 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
2038 /*
2039 * When use NFC sram, need set up PMECC before send
2040 * NAND_CMD_SEQIN command. Since when the nand command
2041 * is sent, nfc will do transfer from sram and nand.
2042 */
2043 pmecc_enable(host, NAND_ECC_WRITE);
2044
2045 host->nfc->will_write_sram = true;
2046 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2047 host->nfc->will_write_sram = false;
2048
2049 if (likely(!raw))
2050 /* Need to write ecc into oob */
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002051 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2052 page);
Josh Wu6054d4d2013-08-05 19:14:37 +08002053
2054 if (status < 0)
2055 return status;
2056
2057 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2058 status = chip->waitfunc(mtd, chip);
2059
2060 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2061 status = chip->errstat(mtd, chip, FL_WRITING, status, page);
2062
2063 if (status & NAND_STATUS_FAIL)
2064 return -EIO;
2065
2066 return 0;
2067}
2068
Josh Wu1ae9c092013-08-05 19:14:36 +08002069static int nfc_sram_init(struct mtd_info *mtd)
2070{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01002071 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01002072 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu1ae9c092013-08-05 19:14:36 +08002073 int res = 0;
2074
2075 /* Initialize the NFC CFG register */
2076 unsigned int cfg_nfc = 0;
2077
2078 /* set page size and oob layout */
2079 switch (mtd->writesize) {
2080 case 512:
2081 cfg_nfc = NFC_CFG_PAGESIZE_512;
2082 break;
2083 case 1024:
2084 cfg_nfc = NFC_CFG_PAGESIZE_1024;
2085 break;
2086 case 2048:
2087 cfg_nfc = NFC_CFG_PAGESIZE_2048;
2088 break;
2089 case 4096:
2090 cfg_nfc = NFC_CFG_PAGESIZE_4096;
2091 break;
2092 case 8192:
2093 cfg_nfc = NFC_CFG_PAGESIZE_8192;
2094 break;
2095 default:
2096 dev_err(host->dev, "Unsupported page size for NFC.\n");
2097 res = -ENXIO;
2098 return res;
2099 }
2100
2101 /* oob bytes size = (NFCSPARESIZE + 1) * 4
2102 * Max support spare size is 512 bytes. */
2103 cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
2104 & NFC_CFG_NFC_SPARESIZE);
2105 /* default set a max timeout */
2106 cfg_nfc |= NFC_CFG_RSPARE |
2107 NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
2108
2109 nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
2110
Josh Wu6054d4d2013-08-05 19:14:37 +08002111 host->nfc->will_write_sram = false;
Josh Wu1ae9c092013-08-05 19:14:36 +08002112 nfc_set_sram_bank(host, 0);
2113
Josh Wu6054d4d2013-08-05 19:14:37 +08002114 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
2115 if (host->nfc->write_by_sram) {
2116 if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
2117 chip->ecc.mode == NAND_ECC_NONE)
2118 chip->write_page = nfc_sram_write_page;
2119 else
2120 host->nfc->write_by_sram = false;
2121 }
Josh Wu1ae9c092013-08-05 19:14:36 +08002122
Josh Wu6054d4d2013-08-05 19:14:37 +08002123 dev_info(host->dev, "Using NFC Sram read %s\n",
2124 host->nfc->write_by_sram ? "and write" : "");
Josh Wu1ae9c092013-08-05 19:14:36 +08002125 return 0;
2126}
2127
Josh Wu7dc37de2013-08-05 19:14:35 +08002128static struct platform_driver atmel_nand_nfc_driver;
Andrew Victor42cb1402006-10-19 18:24:35 +02002129/*
2130 * Probe for the NAND device.
2131 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002132static int atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002133{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002134 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02002135 struct mtd_info *mtd;
2136 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002137 struct resource *mem;
Josh Wu7dc37de2013-08-05 19:14:35 +08002138 int res, irq;
Andrew Victor42cb1402006-10-19 18:24:35 +02002139
2140 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002141 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Han9e3677a2013-12-26 12:00:16 +09002142 if (!host)
Andrew Victor42cb1402006-10-19 18:24:35 +02002143 return -ENOMEM;
Andrew Victor42cb1402006-10-19 18:24:35 +02002144
Josh Wu7dc37de2013-08-05 19:14:35 +08002145 res = platform_driver_register(&atmel_nand_nfc_driver);
2146 if (res)
2147 dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
2148
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002149 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2150 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
2151 if (IS_ERR(host->io_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002152 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002153 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02002154 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002155 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02002156
Andrew Victor42cb1402006-10-19 18:24:35 +02002157 nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01002158 mtd = nand_to_mtd(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02002159 host->dev = &pdev->dev;
Josh Wue9d8da82013-09-18 11:31:19 +08002160 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
Brian Norrisa61ae812015-10-30 20:33:25 -07002161 nand_set_flash_node(nand_chip, pdev->dev.of_node);
Josh Wue9d8da82013-09-18 11:31:19 +08002162 /* Only when CONFIG_OF is enabled of_node can be parsed */
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002163 res = atmel_of_init_port(host, pdev->dev.of_node);
2164 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002165 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002166 } else {
Jingoo Han453810b2013-07-30 17:18:33 +09002167 memcpy(&host->board, dev_get_platdata(&pdev->dev),
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002168 sizeof(struct atmel_nand_data));
Boris Brezillon4f3cab92016-04-01 14:54:22 +02002169 nand_chip->ecc.mode = host->board.ecc_mode;
2170
2171 /* 16-bit bus width */
2172 if (host->board.bus_width_16)
2173 nand_chip->options |= NAND_BUSWIDTH_16;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002174 }
Andrew Victor42cb1402006-10-19 18:24:35 +02002175
Boris BREZILLONd699ed22015-12-10 09:00:41 +01002176 /* link the private data structures */
2177 nand_set_controller_data(nand_chip, host);
Frans Klaver03c287d2015-06-10 22:38:36 +02002178 mtd->dev.parent = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +02002179
2180 /* Set address of NAND IO lines */
2181 nand_chip->IO_ADDR_R = host->io_base;
2182 nand_chip->IO_ADDR_W = host->io_base;
Ivan Kutena4265f82007-05-24 14:35:58 +03002183
Josh Wu7dc37de2013-08-05 19:14:35 +08002184 if (nand_nfc.is_initialized) {
2185 /* NFC driver is probed and initialized */
2186 host->nfc = &nand_nfc;
2187
2188 nand_chip->select_chip = nfc_select_chip;
2189 nand_chip->dev_ready = nfc_device_ready;
2190 nand_chip->cmdfunc = nfc_nand_command;
2191
2192 /* Initialize the interrupt for NFC */
2193 irq = platform_get_irq(pdev, 0);
2194 if (irq < 0) {
2195 dev_err(host->dev, "Cannot get HSMC irq!\n");
Wei Yongjunff52c672013-08-23 10:50:36 +08002196 res = irq;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002197 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002198 }
2199
Josh Wu7dc37de2013-08-05 19:14:35 +08002200 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
2201 0, "hsmc", host);
2202 if (res) {
2203 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
2204 irq);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002205 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002206 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002207 } else {
2208 res = atmel_nand_set_enable_ready_pins(mtd);
2209 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002210 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002211
Josh Wu7dc37de2013-08-05 19:14:35 +08002212 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002213 }
Ivan Kutena4265f82007-05-24 14:35:58 +03002214
Raphaël Poggi796fe362014-07-29 15:27:27 +02002215 nand_chip->chip_delay = 40; /* 40us command delay time */
Andrew Victor42cb1402006-10-19 18:24:35 +02002216
Hong Xucbc6c5e2011-01-18 14:36:05 +08002217
2218 nand_chip->read_buf = atmel_read_buf;
2219 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02002220
Andrew Victor42cb1402006-10-19 18:24:35 +02002221 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002222 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002223
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002224 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002225 res = devm_gpio_request(&pdev->dev,
2226 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002227 if (res < 0) {
2228 dev_err(&pdev->dev,
2229 "can't request det gpio %d\n",
2230 host->board.det_pin);
2231 goto err_no_card;
2232 }
2233
2234 res = gpio_direction_input(host->board.det_pin);
2235 if (res < 0) {
2236 dev_err(&pdev->dev,
2237 "can't request input direction det gpio %d\n",
2238 host->board.det_pin);
2239 goto err_no_card;
2240 }
2241
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002242 if (gpio_get_value(host->board.det_pin)) {
Jingoo Han1295f972013-12-26 12:30:58 +09002243 dev_info(&pdev->dev, "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01002244 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002245 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02002246 }
2247 }
2248
Josh Wu1b719262013-05-09 15:34:55 +08002249 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08002250 use_dma = 0;
2251
2252 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08002253 dma_cap_mask_t mask;
2254
2255 dma_cap_zero(mask);
2256 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02002257 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002258 if (!host->dma_chan) {
2259 dev_err(host->dev, "Failed to request DMA channel\n");
2260 use_dma = 0;
2261 }
2262 }
2263 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08002264 dev_info(host->dev, "Using %s for DMA transfers.\n",
2265 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08002266 else
2267 dev_info(host->dev, "No DMA support for NAND access.\n");
2268
Richard Genoud77f54922008-04-23 19:51:14 +02002269 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00002270 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02002271 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002272 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02002273 }
2274
Boris Brezillon4f3cab92016-04-01 14:54:22 +02002275 if (host->board.on_flash_bbt || on_flash_bbt)
2276 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
2277
2278 if (nand_chip->bbt_options & NAND_BBT_USE_FLASH)
2279 dev_info(&pdev->dev, "Use On Flash BBT\n");
2280
2281 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
2282 res = atmel_of_init_ecc(host, pdev->dev.of_node);
2283 if (res)
2284 goto err_hw_ecc;
2285 }
2286
Richard Genoud3fc23892008-10-12 08:42:28 +02002287 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08002288 if (host->has_pmecc)
2289 res = atmel_pmecc_nand_init_params(pdev, host);
2290 else
2291 res = atmel_hw_nand_init_params(pdev, host);
2292
Josh Wu3dfe41a2012-06-25 18:07:43 +08002293 if (res != 0)
2294 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02002295 }
2296
Josh Wu1ae9c092013-08-05 19:14:36 +08002297 /* initialize the nfc configuration register */
2298 if (host->nfc && host->nfc->use_nfc_sram) {
2299 res = nfc_sram_init(mtd);
2300 if (res) {
2301 host->nfc->use_nfc_sram = false;
2302 dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
2303 }
2304 }
2305
Richard Genoud77f54922008-04-23 19:51:14 +02002306 /* second phase scan */
2307 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02002308 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002309 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02002310 }
2311
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002312 mtd->name = "atmel_nand";
Brian Norrisa61ae812015-10-30 20:33:25 -07002313 res = mtd_device_register(mtd, host->board.parts,
2314 host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02002315 if (!res)
2316 return res;
2317
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002318err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002319 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08002320 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08002321err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002322err_scan_ident:
2323err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002324 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002325 if (host->dma_chan)
2326 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002327err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02002328 return res;
2329}
2330
2331/*
2332 * Remove a NAND device.
2333 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002334static int atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002335{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002336 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Boris BREZILLONac01efe2015-12-10 08:59:50 +01002337 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +02002338
2339 nand_release(mtd);
2340
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002341 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002342
Josh Wu1c7b8742012-06-29 17:47:55 +08002343 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
2344 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2345 pmerrloc_writel(host->pmerrloc_base, ELDIS,
2346 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08002347 }
2348
Hong Xucbc6c5e2011-01-18 14:36:05 +08002349 if (host->dma_chan)
2350 dma_release_channel(host->dma_chan);
2351
Josh Wu7dc37de2013-08-05 19:14:35 +08002352 platform_driver_unregister(&atmel_nand_nfc_driver);
2353
Andrew Victor42cb1402006-10-19 18:24:35 +02002354 return 0;
2355}
2356
Romain Izard55750752016-02-10 10:56:25 +01002357/*
2358 * AT91RM9200 does not have PMECC or PMECC Errloc peripherals for
2359 * BCH ECC. Combined with the "atmel,has-pmecc", it is used to describe
2360 * devices from the SAM9 family that have those.
2361 */
LABBE Corentin72eaec22015-11-20 08:45:16 +01002362static const struct atmel_nand_caps at91rm9200_caps = {
Wu, Josh515857782015-01-19 16:33:06 +08002363 .pmecc_correct_erase_page = false,
Romain Izard55750752016-02-10 10:56:25 +01002364 .pmecc_max_correction = 24,
Wu, Josh515857782015-01-19 16:33:06 +08002365};
2366
LABBE Corentin72eaec22015-11-20 08:45:16 +01002367static const struct atmel_nand_caps sama5d4_caps = {
Wu, Josh515857782015-01-19 16:33:06 +08002368 .pmecc_correct_erase_page = true,
Romain Izard55750752016-02-10 10:56:25 +01002369 .pmecc_max_correction = 24,
2370};
2371
2372/*
2373 * The PMECC Errloc controller starting in SAMA5D2 is not compatible,
2374 * as the increased correction strength requires more registers.
2375 */
2376static const struct atmel_nand_caps sama5d2_caps = {
2377 .pmecc_correct_erase_page = true,
2378 .pmecc_max_correction = 32,
Wu, Josh515857782015-01-19 16:33:06 +08002379};
2380
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002381static const struct of_device_id atmel_nand_dt_ids[] = {
Wu, Josh515857782015-01-19 16:33:06 +08002382 { .compatible = "atmel,at91rm9200-nand", .data = &at91rm9200_caps },
2383 { .compatible = "atmel,sama5d4-nand", .data = &sama5d4_caps },
Romain Izard55750752016-02-10 10:56:25 +01002384 { .compatible = "atmel,sama5d2-nand", .data = &sama5d2_caps },
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002385 { /* sentinel */ }
2386};
2387
2388MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002389
Josh Wu7dc37de2013-08-05 19:14:35 +08002390static int atmel_nand_nfc_probe(struct platform_device *pdev)
2391{
2392 struct atmel_nfc *nfc = &nand_nfc;
2393 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002394 int ret;
Josh Wu7dc37de2013-08-05 19:14:35 +08002395
2396 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2397 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
2398 if (IS_ERR(nfc->base_cmd_regs))
2399 return PTR_ERR(nfc->base_cmd_regs);
2400
2401 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2402 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
2403 if (IS_ERR(nfc->hsmc_regs))
2404 return PTR_ERR(nfc->hsmc_regs);
2405
2406 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2407 if (nfc_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +08002408 nfc->sram_bank0 = (void * __force)
2409 devm_ioremap_resource(&pdev->dev, nfc_sram);
Josh Wu1ae9c092013-08-05 19:14:36 +08002410 if (IS_ERR(nfc->sram_bank0)) {
Josh Wu7dc37de2013-08-05 19:14:35 +08002411 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2412 PTR_ERR(nfc->sram_bank0));
Josh Wu1ae9c092013-08-05 19:14:36 +08002413 } else {
2414 nfc->use_nfc_sram = true;
Josh Wu7dc37de2013-08-05 19:14:35 +08002415 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
Josh Wu6054d4d2013-08-05 19:14:37 +08002416
2417 if (pdev->dev.of_node)
2418 nfc->write_by_sram = of_property_read_bool(
2419 pdev->dev.of_node,
2420 "atmel,write-by-sram");
Josh Wu1ae9c092013-08-05 19:14:36 +08002421 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002422 }
2423
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002424 nfc->caps = (const struct atmel_nand_nfc_caps *)
2425 of_device_get_match_data(&pdev->dev);
2426 if (!nfc->caps)
2427 return -ENODEV;
2428
Wu, Josh50e04e22014-06-10 17:50:09 +08002429 nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
2430 nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */
2431
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002432 nfc->clk = devm_clk_get(&pdev->dev, NULL);
2433 if (!IS_ERR(nfc->clk)) {
2434 ret = clk_prepare_enable(nfc->clk);
2435 if (ret)
2436 return ret;
2437 } else {
2438 dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree");
2439 }
2440
Josh Wu7dc37de2013-08-05 19:14:35 +08002441 nfc->is_initialized = true;
2442 dev_info(&pdev->dev, "NFC is probed.\n");
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002443
2444 return 0;
2445}
2446
2447static int atmel_nand_nfc_remove(struct platform_device *pdev)
2448{
2449 struct atmel_nfc *nfc = &nand_nfc;
2450
2451 if (!IS_ERR(nfc->clk))
2452 clk_disable_unprepare(nfc->clk);
2453
Josh Wu7dc37de2013-08-05 19:14:35 +08002454 return 0;
2455}
2456
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002457static const struct atmel_nand_nfc_caps sama5d3_nfc_caps = {
2458 .rb_mask = NFC_SR_RB_EDGE0,
2459};
2460
2461static const struct atmel_nand_nfc_caps sama5d4_nfc_caps = {
2462 .rb_mask = NFC_SR_RB_EDGE3,
2463};
2464
Josh Wu81f29b42013-09-18 11:31:20 +08002465static const struct of_device_id atmel_nand_nfc_match[] = {
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002466 { .compatible = "atmel,sama5d3-nfc", .data = &sama5d3_nfc_caps },
2467 { .compatible = "atmel,sama5d4-nfc", .data = &sama5d4_nfc_caps },
Josh Wu7dc37de2013-08-05 19:14:35 +08002468 { /* sentinel */ }
2469};
Josh Wu81f29b42013-09-18 11:31:20 +08002470MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
Josh Wu7dc37de2013-08-05 19:14:35 +08002471
2472static struct platform_driver atmel_nand_nfc_driver = {
2473 .driver = {
2474 .name = "atmel_nand_nfc",
Josh Wu7dc37de2013-08-05 19:14:35 +08002475 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2476 },
2477 .probe = atmel_nand_nfc_probe,
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002478 .remove = atmel_nand_nfc_remove,
Josh Wu7dc37de2013-08-05 19:14:35 +08002479};
2480
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002481static struct platform_driver atmel_nand_driver = {
Johan Hovold2c2b9282013-09-23 16:27:28 +02002482 .probe = atmel_nand_probe,
2483 .remove = atmel_nand_remove,
Andrew Victor42cb1402006-10-19 18:24:35 +02002484 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002485 .name = "atmel_nand",
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002486 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02002487 },
2488};
2489
Johan Hovold2c2b9282013-09-23 16:27:28 +02002490module_platform_driver(atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02002491
2492MODULE_LICENSE("GPL");
2493MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02002494MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002495MODULE_ALIAS("platform:atmel_nand");