blob: d883279f58ce1ec0b41a4995cc02ed25709bc119 [file] [log] [blame]
Andrew Victor42cb1402006-10-19 18:24:35 +02001/*
Josh Wu1c7b8742012-06-29 17:47:55 +08002 * Copyright © 2003 Rick Bronson
Andrew Victor42cb1402006-10-19 18:24:35 +02003 *
4 * Derived from drivers/mtd/nand/autcpu12.c
Josh Wu1c7b8742012-06-29 17:47:55 +08005 * Copyright © 2001 Thomas Gleixner (gleixner@autronix.de)
Andrew Victor42cb1402006-10-19 18:24:35 +02006 *
7 * Derived from drivers/mtd/spia.c
Josh Wu1c7b8742012-06-29 17:47:55 +08008 * Copyright © 2000 Steven J. Hill (sjhill@cotw.com)
Andrew Victor42cb1402006-10-19 18:24:35 +02009 *
Richard Genoud77f54922008-04-23 19:51:14 +020010 *
11 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
Josh Wu1c7b8742012-06-29 17:47:55 +080012 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright © 2007
Richard Genoud77f54922008-04-23 19:51:14 +020013 *
14 * Derived from Das U-Boot source code
15 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
Josh Wu1c7b8742012-06-29 17:47:55 +080016 * © Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
Richard Genoud77f54922008-04-23 19:51:14 +020017 *
Josh Wu1c7b8742012-06-29 17:47:55 +080018 * Add Programmable Multibit ECC support for various AT91 SoC
19 * © Copyright 2012 ATMEL, Hong Xu
Richard Genoud77f54922008-04-23 19:51:14 +020020 *
Josh Wu7dc37de2013-08-05 19:14:35 +080021 * Add Nand Flash Controller support for SAMA5 SoC
22 * © Copyright 2013 ATMEL, Josh Wu (josh.wu@atmel.com)
23 *
Andrew Victor42cb1402006-10-19 18:24:35 +020024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Boris BREZILLON2d405ec2014-09-13 01:23:59 +020030#include <linux/clk.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000031#include <linux/dma-mapping.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020032#include <linux/slab.h>
33#include <linux/module.h>
Simon Polettef4fa6972009-05-27 18:19:39 +030034#include <linux/moduleparam.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020035#include <linux/platform_device.h>
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +080036#include <linux/of.h>
37#include <linux/of_device.h>
38#include <linux/of_gpio.h>
39#include <linux/of_mtd.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020040#include <linux/mtd/mtd.h>
41#include <linux/mtd/nand.h>
42#include <linux/mtd/partitions.h>
43
Josh Wu7dc37de2013-08-05 19:14:35 +080044#include <linux/delay.h>
Hans-Christian Egtvedt5c39c4c2011-04-13 15:55:17 +020045#include <linux/dmaengine.h>
David Woodhouse90574d02008-06-07 08:49:00 +010046#include <linux/gpio.h>
Josh Wu7dc37de2013-08-05 19:14:35 +080047#include <linux/interrupt.h>
David Woodhouse90574d02008-06-07 08:49:00 +010048#include <linux/io.h>
Jean-Christophe PLAGNIOL-VILLARDbf4289c2011-12-29 14:43:24 +080049#include <linux/platform_data/atmel.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020050
Hong Xucbc6c5e2011-01-18 14:36:05 +080051static int use_dma = 1;
52module_param(use_dma, int, 0);
53
Simon Polettef4fa6972009-05-27 18:19:39 +030054static int on_flash_bbt = 0;
55module_param(on_flash_bbt, int, 0);
56
Richard Genoud77f54922008-04-23 19:51:14 +020057/* Register access macros */
58#define ecc_readl(add, reg) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020059 __raw_readl(add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020060#define ecc_writel(add, reg, value) \
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020061 __raw_writel((value), add + ATMEL_ECC_##reg)
Richard Genoud77f54922008-04-23 19:51:14 +020062
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +020063#include "atmel_nand_ecc.h" /* Hardware ECC registers */
Josh Wu7dc37de2013-08-05 19:14:35 +080064#include "atmel_nand_nfc.h" /* Nand Flash Controller definition */
Richard Genoud77f54922008-04-23 19:51:14 +020065
Wu, Josh515857782015-01-19 16:33:06 +080066struct atmel_nand_caps {
67 bool pmecc_correct_erase_page;
Romain Izard55750752016-02-10 10:56:25 +010068 uint8_t pmecc_max_correction;
Wu, Josh515857782015-01-19 16:33:06 +080069};
70
Romain Izard5ddc7bd2016-02-10 10:56:23 +010071struct atmel_nand_nfc_caps {
72 uint32_t rb_mask;
73};
74
Richard Genoud77f54922008-04-23 19:51:14 +020075/* oob layout for large page size
76 * bad block info is on bytes 0 and 1
77 * the bytes have to be consecutives to avoid
78 * several NAND_CMD_RNDOUT during read
79 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020080static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020081 .eccbytes = 4,
82 .eccpos = {60, 61, 62, 63},
83 .oobfree = {
84 {2, 58}
85 },
86};
87
88/* oob layout for small page size
89 * bad block info is on bytes 4 and 5
90 * the bytes have to be consecutives to avoid
91 * several NAND_CMD_RNDOUT during read
92 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020093static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020094 .eccbytes = 4,
95 .eccpos = {0, 1, 2, 3},
96 .oobfree = {
97 {6, 10}
98 },
99};
100
Josh Wu7dc37de2013-08-05 19:14:35 +0800101struct atmel_nfc {
102 void __iomem *base_cmd_regs;
103 void __iomem *hsmc_regs;
Wu, Josh068b44b2014-11-07 15:26:09 +0800104 void *sram_bank0;
Josh Wu7dc37de2013-08-05 19:14:35 +0800105 dma_addr_t sram_bank0_phys;
Josh Wu1ae9c092013-08-05 19:14:36 +0800106 bool use_nfc_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +0800107 bool write_by_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +0800108
Boris BREZILLON2d405ec2014-09-13 01:23:59 +0200109 struct clk *clk;
110
Josh Wu7dc37de2013-08-05 19:14:35 +0800111 bool is_initialized;
Josh Wue4e06932014-06-10 17:50:11 +0800112 struct completion comp_ready;
113 struct completion comp_cmd_done;
114 struct completion comp_xfer_done;
Josh Wu1ae9c092013-08-05 19:14:36 +0800115
116 /* Point to the sram bank which include readed data via NFC */
Wu, Josh068b44b2014-11-07 15:26:09 +0800117 void *data_in_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +0800118 bool will_write_sram;
Romain Izard5ddc7bd2016-02-10 10:56:23 +0100119 const struct atmel_nand_nfc_caps *caps;
Josh Wu7dc37de2013-08-05 19:14:35 +0800120};
121static struct atmel_nfc nand_nfc;
122
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200123struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +0200124 struct nand_chip nand_chip;
Andrew Victor42cb1402006-10-19 18:24:35 +0200125 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800126 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800127 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +0200128 struct device *dev;
129 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800130
131 struct completion comp;
132 struct dma_chan *dma_chan;
Josh Wua41b51a2012-06-29 17:47:54 +0800133
Josh Wu7dc37de2013-08-05 19:14:35 +0800134 struct atmel_nfc *nfc;
135
LABBE Corentin72eaec22015-11-20 08:45:16 +0100136 const struct atmel_nand_caps *caps;
Josh Wua41b51a2012-06-29 17:47:54 +0800137 bool has_pmecc;
138 u8 pmecc_corr_cap;
139 u16 pmecc_sector_size;
Josh Wuabb1cd02014-10-11 18:01:50 +0800140 bool has_no_lookup_table;
Josh Wua41b51a2012-06-29 17:47:54 +0800141 u32 pmecc_lookup_table_offset;
Josh Wue66b4312013-01-23 20:47:11 +0800142 u32 pmecc_lookup_table_offset_512;
143 u32 pmecc_lookup_table_offset_1024;
Josh Wu1c7b8742012-06-29 17:47:55 +0800144
Josh Wu1c7b8742012-06-29 17:47:55 +0800145 int pmecc_degree; /* Degree of remainders */
146 int pmecc_cw_len; /* Length of codeword */
147
148 void __iomem *pmerrloc_base;
Romain Izard55750752016-02-10 10:56:25 +0100149 void __iomem *pmerrloc_el_base;
Josh Wu1c7b8742012-06-29 17:47:55 +0800150 void __iomem *pmecc_rom_base;
151
152 /* lookup table for alpha_to and index_of */
153 void __iomem *pmecc_alpha_to;
154 void __iomem *pmecc_index_of;
155
156 /* data for pmecc computation */
157 int16_t *pmecc_partial_syn;
158 int16_t *pmecc_si;
159 int16_t *pmecc_smu; /* Sigma table */
160 int16_t *pmecc_lmu; /* polynomal order */
161 int *pmecc_mu;
162 int *pmecc_dmu;
163 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200164};
165
Josh Wu1c7b8742012-06-29 17:47:55 +0800166static struct nand_ecclayout atmel_pmecc_oobinfo;
167
Andrew Victor42cb1402006-10-19 18:24:35 +0200168/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900169 * Enable NAND.
170 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200171static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900172{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800173 if (gpio_is_valid(host->board.enable_pin))
174 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900175}
176
177/*
178 * Disable NAND.
179 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200180static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900181{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800182 if (gpio_is_valid(host->board.enable_pin))
183 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900184}
185
186/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200187 * Hardware specific access to control-lines
188 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200189static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200190{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100191 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100192 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +0200193
Atsushi Nemoto81365082008-04-27 01:51:12 +0900194 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900195 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200196 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900197 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200198 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900199 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200200 if (cmd == NAND_CMD_NONE)
201 return;
202
203 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800204 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200205 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800206 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200207}
208
209/*
210 * Read the Device Ready pin.
211 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200212static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200213{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100214 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100215 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +0200216
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800217 return gpio_get_value(host->board.rdy_pin) ^
218 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200219}
220
Josh Wu7dc37de2013-08-05 19:14:35 +0800221/* Set up for hardware ready pin and enable pin. */
222static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
223{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100224 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100225 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu7dc37de2013-08-05 19:14:35 +0800226 int res = 0;
227
228 if (gpio_is_valid(host->board.rdy_pin)) {
229 res = devm_gpio_request(host->dev,
230 host->board.rdy_pin, "nand_rdy");
231 if (res < 0) {
232 dev_err(host->dev,
233 "can't request rdy gpio %d\n",
234 host->board.rdy_pin);
235 return res;
236 }
237
238 res = gpio_direction_input(host->board.rdy_pin);
239 if (res < 0) {
240 dev_err(host->dev,
241 "can't request input direction rdy gpio %d\n",
242 host->board.rdy_pin);
243 return res;
244 }
245
246 chip->dev_ready = atmel_nand_device_ready;
247 }
248
249 if (gpio_is_valid(host->board.enable_pin)) {
250 res = devm_gpio_request(host->dev,
251 host->board.enable_pin, "nand_enable");
252 if (res < 0) {
253 dev_err(host->dev,
254 "can't request enable gpio %d\n",
255 host->board.enable_pin);
256 return res;
257 }
258
259 res = gpio_direction_output(host->board.enable_pin, 1);
260 if (res < 0) {
261 dev_err(host->dev,
262 "can't request output direction enable gpio %d\n",
263 host->board.enable_pin);
264 return res;
265 }
266 }
267
268 return res;
269}
270
Artem Bityutskiy50082312012-02-02 13:54:25 +0200271/*
272 * Minimal-overhead PIO for data access.
273 */
274static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
275{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100276 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100277 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200278
Josh Wu1ae9c092013-08-05 19:14:36 +0800279 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +0800280 memcpy(buf, host->nfc->data_in_sram, len);
Josh Wu1ae9c092013-08-05 19:14:36 +0800281 host->nfc->data_in_sram += len;
282 } else {
283 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
284 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200285}
286
287static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
288{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100289 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100290 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200291
Josh Wu1ae9c092013-08-05 19:14:36 +0800292 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +0800293 memcpy(buf, host->nfc->data_in_sram, len);
Josh Wu1ae9c092013-08-05 19:14:36 +0800294 host->nfc->data_in_sram += len;
295 } else {
296 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
297 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200298}
299
300static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
301{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100302 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200303
304 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
305}
306
307static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
308{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100309 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Artem Bityutskiy50082312012-02-02 13:54:25 +0200310
311 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
312}
313
Hong Xucbc6c5e2011-01-18 14:36:05 +0800314static void dma_complete_func(void *completion)
315{
316 complete(completion);
317}
318
Josh Wu1ae9c092013-08-05 19:14:36 +0800319static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
320{
321 /* NFC only has two banks. Must be 0 or 1 */
322 if (bank > 1)
323 return -EINVAL;
324
325 if (bank) {
Boris BREZILLONac01efe2015-12-10 08:59:50 +0100326 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
327
Josh Wu1ae9c092013-08-05 19:14:36 +0800328 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
Boris BREZILLONac01efe2015-12-10 08:59:50 +0100329 if (mtd->writesize > 2048)
Josh Wu1ae9c092013-08-05 19:14:36 +0800330 return -EINVAL;
331 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
332 } else {
333 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
334 }
335
336 return 0;
337}
338
339static uint nfc_get_sram_off(struct atmel_nand_host *host)
340{
341 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
342 return NFC_SRAM_BANK1_OFFSET;
343 else
344 return 0;
345}
346
347static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
348{
349 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
350 return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
351 else
352 return host->nfc->sram_bank0_phys;
353}
354
Hong Xucbc6c5e2011-01-18 14:36:05 +0800355static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
356 int is_read)
357{
358 struct dma_device *dma_dev;
359 enum dma_ctrl_flags flags;
360 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
361 struct dma_async_tx_descriptor *tx = NULL;
362 dma_cookie_t cookie;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100363 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100364 struct atmel_nand_host *host = nand_get_controller_data(chip);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800365 void *p = buf;
366 int err = -EIO;
367 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Josh Wu1ae9c092013-08-05 19:14:36 +0800368 struct atmel_nfc *nfc = host->nfc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800369
Hong Xu80b4f812011-03-31 18:33:15 +0800370 if (buf >= high_memory)
371 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800372
373 dma_dev = host->dma_chan->device;
374
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200375 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800376
377 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
378 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
379 dev_err(host->dev, "Failed to dma_map_single\n");
380 goto err_buf;
381 }
382
383 if (is_read) {
Josh Wu1ae9c092013-08-05 19:14:36 +0800384 if (nfc && nfc->data_in_sram)
385 dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
386 - (nfc->sram_bank0 + nfc_get_sram_off(host)));
387 else
388 dma_src_addr = host->io_phys;
389
Hong Xucbc6c5e2011-01-18 14:36:05 +0800390 dma_dst_addr = phys_addr;
391 } else {
392 dma_src_addr = phys_addr;
Josh Wu6054d4d2013-08-05 19:14:37 +0800393
394 if (nfc && nfc->write_by_sram)
395 dma_dst_addr = nfc_sram_phys(host);
396 else
397 dma_dst_addr = host->io_phys;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800398 }
399
400 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
401 dma_src_addr, len, flags);
402 if (!tx) {
403 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
404 goto err_dma;
405 }
406
407 init_completion(&host->comp);
408 tx->callback = dma_complete_func;
409 tx->callback_param = &host->comp;
410
411 cookie = tx->tx_submit(tx);
412 if (dma_submit_error(cookie)) {
413 dev_err(host->dev, "Failed to do DMA tx_submit\n");
414 goto err_dma;
415 }
416
417 dma_async_issue_pending(host->dma_chan);
418 wait_for_completion(&host->comp);
419
Josh Wu1ae9c092013-08-05 19:14:36 +0800420 if (is_read && nfc && nfc->data_in_sram)
421 /* After read data from SRAM, need to increase the position */
422 nfc->data_in_sram += len;
423
Hong Xucbc6c5e2011-01-18 14:36:05 +0800424 err = 0;
425
426err_dma:
427 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
428err_buf:
429 if (err != 0)
Nicolas Ferre74414a942014-02-12 12:26:54 +0100430 dev_dbg(host->dev, "Fall back to CPU I/O\n");
Hong Xucbc6c5e2011-01-18 14:36:05 +0800431 return err;
432}
433
434static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
435{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100436 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100437 struct atmel_nand_host *host = nand_get_controller_data(chip);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800438
Nicolas Ferre9d515672011-04-01 16:40:44 +0200439 if (use_dma && len > mtd->oobsize)
440 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800441 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
442 return;
443
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800444 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200445 atmel_read_buf16(mtd, buf, len);
446 else
447 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800448}
449
450static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
451{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100452 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100453 struct atmel_nand_host *host = nand_get_controller_data(chip);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800454
Nicolas Ferre9d515672011-04-01 16:40:44 +0200455 if (use_dma && len > mtd->oobsize)
456 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800457 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
458 return;
459
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800460 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200461 atmel_write_buf16(mtd, buf, len);
462 else
463 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800464}
465
David Brownell23a346c2008-07-03 23:40:16 -0700466/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800467 * Return number of ecc bytes per sector according to sector size and
468 * correction capability
469 *
470 * Following table shows what at91 PMECC supported:
471 * Correction Capability Sector_512_bytes Sector_1024_bytes
472 * ===================== ================ =================
473 * 2-bits 4-bytes 4-bytes
474 * 4-bits 7-bytes 7-bytes
475 * 8-bits 13-bytes 14-bytes
476 * 12-bits 20-bytes 21-bytes
477 * 24-bits 39-bytes 42-bytes
478 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500479static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800480{
481 int m = 12 + sector_size / 512;
482 return (m * cap + 7) / 8;
483}
484
Bill Pemberton06f25512012-11-19 13:23:07 -0500485static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800486 int oobsize, int ecc_len)
Josh Wu1c7b8742012-06-29 17:47:55 +0800487{
488 int i;
489
490 layout->eccbytes = ecc_len;
491
492 /* ECC will occupy the last ecc_len bytes continuously */
493 for (i = 0; i < ecc_len; i++)
494 layout->eccpos[i] = oobsize - ecc_len + i;
495
Josh Wu477478a2015-04-02 14:12:33 +0800496 layout->oobfree[0].offset = PMECC_OOB_RESERVED_BYTES;
Josh Wu1c7b8742012-06-29 17:47:55 +0800497 layout->oobfree[0].length =
498 oobsize - ecc_len - layout->oobfree[0].offset;
499}
500
Bill Pemberton06f25512012-11-19 13:23:07 -0500501static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800502{
503 int table_size;
504
505 table_size = host->pmecc_sector_size == 512 ?
506 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
507
508 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
509 table_size * sizeof(int16_t);
510}
511
Bill Pemberton06f25512012-11-19 13:23:07 -0500512static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800513{
514 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800515 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800516
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800517 size = (2 * cap + 1) * sizeof(int16_t);
518 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
519 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
520 host->pmecc_lmu = devm_kzalloc(host->dev,
521 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
522 host->pmecc_smu = devm_kzalloc(host->dev,
523 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800524
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800525 size = (cap + 1) * sizeof(int);
526 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
527 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
528 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800529
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800530 if (!host->pmecc_partial_syn ||
531 !host->pmecc_si ||
532 !host->pmecc_lmu ||
533 !host->pmecc_smu ||
534 !host->pmecc_mu ||
535 !host->pmecc_dmu ||
536 !host->pmecc_delta)
537 return -ENOMEM;
538
539 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800540}
541
542static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
543{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100544 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100545 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800546 int i;
547 uint32_t value;
548
549 /* Fill odd syndromes */
550 for (i = 0; i < host->pmecc_corr_cap; i++) {
551 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
552 if (i & 1)
553 value >>= 16;
554 value &= 0xffff;
555 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
556 }
557}
558
559static void pmecc_substitute(struct mtd_info *mtd)
560{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100561 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100562 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800563 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
564 int16_t __iomem *index_of = host->pmecc_index_of;
565 int16_t *partial_syn = host->pmecc_partial_syn;
566 const int cap = host->pmecc_corr_cap;
567 int16_t *si;
568 int i, j;
569
570 /* si[] is a table that holds the current syndrome value,
571 * an element of that table belongs to the field
572 */
573 si = host->pmecc_si;
574
575 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
576
577 /* Computation 2t syndromes based on S(x) */
578 /* Odd syndromes */
579 for (i = 1; i < 2 * cap; i += 2) {
580 for (j = 0; j < host->pmecc_degree; j++) {
581 if (partial_syn[i] & ((unsigned short)0x1 << j))
582 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
583 }
584 }
585 /* Even syndrome = (Odd syndrome) ** 2 */
586 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
587 if (si[j] == 0) {
588 si[i] = 0;
589 } else {
590 int16_t tmp;
591
592 tmp = readw_relaxed(index_of + si[j]);
593 tmp = (tmp * 2) % host->pmecc_cw_len;
594 si[i] = readw_relaxed(alpha_to + tmp);
595 }
596 }
597
598 return;
599}
600
601static void pmecc_get_sigma(struct mtd_info *mtd)
602{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100603 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100604 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800605
606 int16_t *lmu = host->pmecc_lmu;
607 int16_t *si = host->pmecc_si;
608 int *mu = host->pmecc_mu;
609 int *dmu = host->pmecc_dmu; /* Discrepancy */
610 int *delta = host->pmecc_delta; /* Delta order */
611 int cw_len = host->pmecc_cw_len;
612 const int16_t cap = host->pmecc_corr_cap;
613 const int num = 2 * cap + 1;
614 int16_t __iomem *index_of = host->pmecc_index_of;
615 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
616 int i, j, k;
617 uint32_t dmu_0_count, tmp;
618 int16_t *smu = host->pmecc_smu;
619
620 /* index of largest delta */
621 int ro;
622 int largest;
623 int diff;
624
625 dmu_0_count = 0;
626
627 /* First Row */
628
629 /* Mu */
630 mu[0] = -1;
631
632 memset(smu, 0, sizeof(int16_t) * num);
633 smu[0] = 1;
634
635 /* discrepancy set to 1 */
636 dmu[0] = 1;
637 /* polynom order set to 0 */
638 lmu[0] = 0;
639 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
640
641 /* Second Row */
642
643 /* Mu */
644 mu[1] = 0;
645 /* Sigma(x) set to 1 */
646 memset(&smu[num], 0, sizeof(int16_t) * num);
647 smu[num] = 1;
648
649 /* discrepancy set to S1 */
650 dmu[1] = si[1];
651
652 /* polynom order set to 0 */
653 lmu[1] = 0;
654
655 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
656
657 /* Init the Sigma(x) last row */
658 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
659
660 for (i = 1; i <= cap; i++) {
661 mu[i + 1] = i << 1;
662 /* Begin Computing Sigma (Mu+1) and L(mu) */
663 /* check if discrepancy is set to 0 */
664 if (dmu[i] == 0) {
665 dmu_0_count++;
666
667 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
668 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
669 tmp += 2;
670 else
671 tmp += 1;
672
673 if (dmu_0_count == tmp) {
674 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
675 smu[(cap + 1) * num + j] =
676 smu[i * num + j];
677
678 lmu[cap + 1] = lmu[i];
679 return;
680 }
681
682 /* copy polynom */
683 for (j = 0; j <= lmu[i] >> 1; j++)
684 smu[(i + 1) * num + j] = smu[i * num + j];
685
686 /* copy previous polynom order to the next */
687 lmu[i + 1] = lmu[i];
688 } else {
689 ro = 0;
690 largest = -1;
691 /* find largest delta with dmu != 0 */
692 for (j = 0; j < i; j++) {
693 if ((dmu[j]) && (delta[j] > largest)) {
694 largest = delta[j];
695 ro = j;
696 }
697 }
698
699 /* compute difference */
700 diff = (mu[i] - mu[ro]);
701
702 /* Compute degree of the new smu polynomial */
703 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
704 lmu[i + 1] = lmu[i];
705 else
706 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
707
708 /* Init smu[i+1] with 0 */
709 for (k = 0; k < num; k++)
710 smu[(i + 1) * num + k] = 0;
711
712 /* Compute smu[i+1] */
713 for (k = 0; k <= lmu[ro] >> 1; k++) {
714 int16_t a, b, c;
715
716 if (!(smu[ro * num + k] && dmu[i]))
717 continue;
718 a = readw_relaxed(index_of + dmu[i]);
719 b = readw_relaxed(index_of + dmu[ro]);
720 c = readw_relaxed(index_of + smu[ro * num + k]);
721 tmp = a + (cw_len - b) + c;
722 a = readw_relaxed(alpha_to + tmp % cw_len);
723 smu[(i + 1) * num + (k + diff)] = a;
724 }
725
726 for (k = 0; k <= lmu[i] >> 1; k++)
727 smu[(i + 1) * num + k] ^= smu[i * num + k];
728 }
729
730 /* End Computing Sigma (Mu+1) and L(mu) */
731 /* In either case compute delta */
732 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
733
734 /* Do not compute discrepancy for the last iteration */
735 if (i >= cap)
736 continue;
737
738 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
739 tmp = 2 * (i - 1);
740 if (k == 0) {
741 dmu[i + 1] = si[tmp + 3];
742 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
743 int16_t a, b, c;
744 a = readw_relaxed(index_of +
745 smu[(i + 1) * num + k]);
746 b = si[2 * (i - 1) + 3 - k];
747 c = readw_relaxed(index_of + b);
748 tmp = a + c;
749 tmp %= cw_len;
750 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
751 dmu[i + 1];
752 }
753 }
754 }
755
756 return;
757}
758
759static int pmecc_err_location(struct mtd_info *mtd)
760{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100761 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100762 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800763 unsigned long end_time;
764 const int cap = host->pmecc_corr_cap;
765 const int num = 2 * cap + 1;
766 int sector_size = host->pmecc_sector_size;
767 int err_nbr = 0; /* number of error */
768 int roots_nbr; /* number of roots */
769 int i;
770 uint32_t val;
771 int16_t *smu = host->pmecc_smu;
772
773 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
774
775 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
776 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
777 smu[(cap + 1) * num + i]);
778 err_nbr++;
779 }
780
781 val = (err_nbr - 1) << 16;
782 if (sector_size == 1024)
783 val |= 1;
784
785 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
786 pmerrloc_writel(host->pmerrloc_base, ELEN,
787 sector_size * 8 + host->pmecc_degree * cap);
788
789 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
790 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
791 & PMERRLOC_CALC_DONE)) {
792 if (unlikely(time_after(jiffies, end_time))) {
793 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
794 return -1;
795 }
796 cpu_relax();
797 }
798
799 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
800 & PMERRLOC_ERR_NUM_MASK) >> 8;
801 /* Number of roots == degree of smu hence <= cap */
802 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
803 return err_nbr - 1;
804
805 /* Number of roots does not match the degree of smu
806 * unable to correct error */
807 return -1;
808}
809
810static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
811 int sector_num, int extra_bytes, int err_nbr)
812{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100813 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100814 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800815 int i = 0;
816 int byte_pos, bit_pos, sector_size, pos;
817 uint32_t tmp;
818 uint8_t err_byte;
819
820 sector_size = host->pmecc_sector_size;
821
822 while (err_nbr) {
Romain Izard55750752016-02-10 10:56:25 +0100823 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_el_base, i) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +0800824 byte_pos = tmp / 8;
825 bit_pos = tmp % 8;
826
827 if (byte_pos >= (sector_size + extra_bytes))
828 BUG(); /* should never happen */
829
830 if (byte_pos < sector_size) {
831 err_byte = *(buf + byte_pos);
832 *(buf + byte_pos) ^= (1 << bit_pos);
833
834 pos = sector_num * host->pmecc_sector_size + byte_pos;
Romain Izard12197bf2016-01-13 17:34:13 +0100835 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 +0800836 pos, bit_pos, err_byte, *(buf + byte_pos));
837 } else {
838 /* 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
844 pos = tmp + nand_chip->ecc.layout->eccpos[0];
Romain Izard12197bf2016-01-13 17:34:13 +0100845 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 +0800846 pos, bit_pos, err_byte, ecc[tmp]);
847 }
848
849 i++;
850 err_nbr--;
851 }
852
853 return;
854}
855
856static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
857 u8 *ecc)
858{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100859 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100860 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Bo Shenb3857662014-06-12 15:58:45 +0800861 int i, err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800862 uint8_t *buf_pos;
Wu, Josh267d46e2015-01-14 11:50:46 +0800863 int max_bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800864
Wu, Josh515857782015-01-19 16:33:06 +0800865 /* If can correct bitfilps from erased page, do the normal check */
866 if (host->caps->pmecc_correct_erase_page)
867 goto normal_check;
868
Bo Shenb3857662014-06-12 15:58:45 +0800869 for (i = 0; i < nand_chip->ecc.total; i++)
Josh Wu1c7b8742012-06-29 17:47:55 +0800870 if (ecc[i] != 0xff)
871 goto normal_check;
872 /* Erased page, return OK */
873 return 0;
874
875normal_check:
Wu, Joshc9447ff2014-08-08 17:12:34 +0800876 for (i = 0; i < nand_chip->ecc.steps; i++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800877 err_nbr = 0;
878 if (pmecc_stat & 0x1) {
879 buf_pos = buf + i * host->pmecc_sector_size;
880
881 pmecc_gen_syndrome(mtd, i);
882 pmecc_substitute(mtd);
883 pmecc_get_sigma(mtd);
884
885 err_nbr = pmecc_err_location(mtd);
886 if (err_nbr == -1) {
887 dev_err(host->dev, "PMECC: Too many errors\n");
888 mtd->ecc_stats.failed++;
889 return -EIO;
890 } else {
891 pmecc_correct_data(mtd, buf_pos, ecc, i,
Wu, Josh022a4782014-08-08 17:12:35 +0800892 nand_chip->ecc.bytes, err_nbr);
Josh Wu1c7b8742012-06-29 17:47:55 +0800893 mtd->ecc_stats.corrected += err_nbr;
Wu, Josh267d46e2015-01-14 11:50:46 +0800894 max_bitflips = max_t(int, max_bitflips, err_nbr);
Josh Wu1c7b8742012-06-29 17:47:55 +0800895 }
896 }
897 pmecc_stat >>= 1;
898 }
899
Wu, Josh267d46e2015-01-14 11:50:46 +0800900 return max_bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800901}
902
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800903static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
904{
905 u32 val;
906
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800907 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
908 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
909 return;
910 }
911
Josh Wu1fad0e82013-08-07 17:58:11 +0800912 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
913 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
914 val = pmecc_readl_relaxed(host->ecc, CFG);
915
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800916 if (ecc_op == NAND_ECC_READ)
917 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
918 | PMECC_CFG_AUTO_ENABLE);
919 else
920 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
921 & ~PMECC_CFG_AUTO_ENABLE);
922
923 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
924 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
925}
926
Josh Wu1c7b8742012-06-29 17:47:55 +0800927static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
928 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
929{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100930 struct atmel_nand_host *host = nand_get_controller_data(chip);
Bo Shenb3857662014-06-12 15:58:45 +0800931 int eccsize = chip->ecc.size * chip->ecc.steps;
Josh Wu1c7b8742012-06-29 17:47:55 +0800932 uint8_t *oob = chip->oob_poi;
933 uint32_t *eccpos = chip->ecc.layout->eccpos;
934 uint32_t stat;
935 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800936 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800937
Josh Wu1ae9c092013-08-05 19:14:36 +0800938 if (!host->nfc || !host->nfc->use_nfc_sram)
939 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800940
941 chip->read_buf(mtd, buf, eccsize);
942 chip->read_buf(mtd, oob, mtd->oobsize);
943
944 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
945 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
946 if (unlikely(time_after(jiffies, end_time))) {
947 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
948 return -EIO;
949 }
950 cpu_relax();
951 }
952
953 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800954 if (stat != 0) {
955 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
956 if (bitflips < 0)
957 /* uncorrectable errors */
958 return 0;
959 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800960
Josh Wuc0c70d92012-11-27 18:50:31 +0800961 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800962}
963
964static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +0200965 struct nand_chip *chip, const uint8_t *buf, int oob_required,
966 int page)
Josh Wu1c7b8742012-06-29 17:47:55 +0800967{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100968 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800969 uint32_t *eccpos = chip->ecc.layout->eccpos;
970 int i, j;
971 unsigned long end_time;
972
Josh Wu6054d4d2013-08-05 19:14:37 +0800973 if (!host->nfc || !host->nfc->write_by_sram) {
974 pmecc_enable(host, NAND_ECC_WRITE);
975 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
976 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800977
978 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
979 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
980 if (unlikely(time_after(jiffies, end_time))) {
981 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
982 return -EIO;
983 }
984 cpu_relax();
985 }
986
Wu, Joshc9447ff2014-08-08 17:12:34 +0800987 for (i = 0; i < chip->ecc.steps; i++) {
Wu, Josh022a4782014-08-08 17:12:35 +0800988 for (j = 0; j < chip->ecc.bytes; j++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800989 int pos;
990
Wu, Josh022a4782014-08-08 17:12:35 +0800991 pos = i * chip->ecc.bytes + j;
Josh Wu1c7b8742012-06-29 17:47:55 +0800992 chip->oob_poi[eccpos[pos]] =
993 pmecc_readb_ecc_relaxed(host->ecc, i, j);
994 }
995 }
996 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
997
998 return 0;
999}
1000
1001static void atmel_pmecc_core_init(struct mtd_info *mtd)
1002{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001003 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001004 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +08001005 uint32_t val = 0;
1006 struct nand_ecclayout *ecc_layout;
1007
1008 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
1009 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1010
1011 switch (host->pmecc_corr_cap) {
1012 case 2:
1013 val = PMECC_CFG_BCH_ERR2;
1014 break;
1015 case 4:
1016 val = PMECC_CFG_BCH_ERR4;
1017 break;
1018 case 8:
1019 val = PMECC_CFG_BCH_ERR8;
1020 break;
1021 case 12:
1022 val = PMECC_CFG_BCH_ERR12;
1023 break;
1024 case 24:
1025 val = PMECC_CFG_BCH_ERR24;
1026 break;
1027 }
1028
1029 if (host->pmecc_sector_size == 512)
1030 val |= PMECC_CFG_SECTOR512;
1031 else if (host->pmecc_sector_size == 1024)
1032 val |= PMECC_CFG_SECTOR1024;
1033
Wu, Joshc9447ff2014-08-08 17:12:34 +08001034 switch (nand_chip->ecc.steps) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001035 case 1:
1036 val |= PMECC_CFG_PAGE_1SECTOR;
1037 break;
1038 case 2:
1039 val |= PMECC_CFG_PAGE_2SECTORS;
1040 break;
1041 case 4:
1042 val |= PMECC_CFG_PAGE_4SECTORS;
1043 break;
1044 case 8:
1045 val |= PMECC_CFG_PAGE_8SECTORS;
1046 break;
1047 }
1048
1049 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
1050 | PMECC_CFG_AUTO_DISABLE);
1051 pmecc_writel(host->ecc, CFG, val);
1052
1053 ecc_layout = nand_chip->ecc.layout;
1054 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
1055 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
1056 pmecc_writel(host->ecc, EADDR,
1057 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
1058 /* See datasheet about PMECC Clock Control Register */
1059 pmecc_writel(host->ecc, CLK, 2);
1060 pmecc_writel(host->ecc, IDR, 0xff);
1061 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
1062}
1063
Josh Wu84cfbbb2013-01-23 20:47:12 +08001064/*
Josh Wu2a3d9332013-09-18 13:58:48 +08001065 * Get minimum ecc requirements from NAND.
Josh Wu84cfbbb2013-01-23 20:47:12 +08001066 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
Josh Wu2a3d9332013-09-18 13:58:48 +08001067 * will set them according to minimum ecc requirement. Otherwise, use the
Josh Wu84cfbbb2013-01-23 20:47:12 +08001068 * value in DTS file.
1069 * return 0 if success. otherwise return error code.
1070 */
1071static int pmecc_choose_ecc(struct atmel_nand_host *host,
1072 int *cap, int *sector_size)
1073{
Josh Wu2a3d9332013-09-18 13:58:48 +08001074 /* Get minimum ECC requirements */
1075 if (host->nand_chip.ecc_strength_ds) {
1076 *cap = host->nand_chip.ecc_strength_ds;
1077 *sector_size = host->nand_chip.ecc_step_ds;
1078 dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n",
Josh Wu84cfbbb2013-01-23 20:47:12 +08001079 *cap, *sector_size);
Josh Wu84cfbbb2013-01-23 20:47:12 +08001080 } else {
Josh Wu84cfbbb2013-01-23 20:47:12 +08001081 *cap = 2;
1082 *sector_size = 512;
Josh Wu2a3d9332013-09-18 13:58:48 +08001083 dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
Josh Wu84cfbbb2013-01-23 20:47:12 +08001084 }
1085
Josh Wu2a3d9332013-09-18 13:58:48 +08001086 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
Josh Wu84cfbbb2013-01-23 20:47:12 +08001087 if (host->pmecc_corr_cap == 0) {
1088 /* use the most fitable ecc bits (the near bigger one ) */
1089 if (*cap <= 2)
1090 host->pmecc_corr_cap = 2;
1091 else if (*cap <= 4)
1092 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +08001093 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001094 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +08001095 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001096 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +08001097 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001098 host->pmecc_corr_cap = 24;
1099 else
1100 return -EINVAL;
1101 }
1102 if (host->pmecc_sector_size == 0) {
1103 /* use the most fitable sector size (the near smaller one ) */
1104 if (*sector_size >= 1024)
1105 host->pmecc_sector_size = 1024;
1106 else if (*sector_size >= 512)
1107 host->pmecc_sector_size = 512;
1108 else
1109 return -EINVAL;
1110 }
1111 return 0;
1112}
1113
Josh Wuabb1cd02014-10-11 18:01:50 +08001114static inline int deg(unsigned int poly)
1115{
1116 /* polynomial degree is the most-significant bit index */
1117 return fls(poly) - 1;
1118}
1119
1120static int build_gf_tables(int mm, unsigned int poly,
1121 int16_t *index_of, int16_t *alpha_to)
1122{
1123 unsigned int i, x = 1;
1124 const unsigned int k = 1 << deg(poly);
1125 unsigned int nn = (1 << mm) - 1;
1126
1127 /* primitive polynomial must be of degree m */
1128 if (k != (1u << mm))
1129 return -EINVAL;
1130
1131 for (i = 0; i < nn; i++) {
1132 alpha_to[i] = x;
1133 index_of[x] = i;
1134 if (i && (x == 1))
1135 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
1136 return -EINVAL;
1137 x <<= 1;
1138 if (x & k)
1139 x ^= poly;
1140 }
1141 alpha_to[nn] = 1;
1142 index_of[0] = 0;
1143
1144 return 0;
1145}
1146
1147static uint16_t *create_lookup_table(struct device *dev, int sector_size)
1148{
1149 int degree = (sector_size == 512) ?
1150 PMECC_GF_DIMENSION_13 :
1151 PMECC_GF_DIMENSION_14;
1152 unsigned int poly = (sector_size == 512) ?
1153 PMECC_GF_13_PRIMITIVE_POLY :
1154 PMECC_GF_14_PRIMITIVE_POLY;
1155 int table_size = (sector_size == 512) ?
1156 PMECC_LOOKUP_TABLE_SIZE_512 :
1157 PMECC_LOOKUP_TABLE_SIZE_1024;
1158
1159 int16_t *addr = devm_kzalloc(dev, 2 * table_size * sizeof(uint16_t),
1160 GFP_KERNEL);
1161 if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
1162 return NULL;
1163
1164 return addr;
1165}
1166
Johan Hovold2c2b9282013-09-23 16:27:28 +02001167static int atmel_pmecc_nand_init_params(struct platform_device *pdev,
Josh Wu1c7b8742012-06-29 17:47:55 +08001168 struct atmel_nand_host *host)
1169{
Josh Wu1c7b8742012-06-29 17:47:55 +08001170 struct nand_chip *nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01001171 struct mtd_info *mtd = nand_to_mtd(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +08001172 struct resource *regs, *regs_pmerr, *regs_rom;
Josh Wuabb1cd02014-10-11 18:01:50 +08001173 uint16_t *galois_table;
Josh Wu1c7b8742012-06-29 17:47:55 +08001174 int cap, sector_size, err_no;
1175
Josh Wu84cfbbb2013-01-23 20:47:12 +08001176 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
1177 if (err_no) {
1178 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1179 return err_no;
1180 }
1181
Richard Genoudf666d642013-07-30 17:17:29 +02001182 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001183 sector_size != host->pmecc_sector_size)
1184 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001185
Josh Wu1c7b8742012-06-29 17:47:55 +08001186 cap = host->pmecc_corr_cap;
1187 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001188 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1189 host->pmecc_lookup_table_offset_512 :
1190 host->pmecc_lookup_table_offset_1024;
1191
Josh Wu1c7b8742012-06-29 17:47:55 +08001192 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1193 cap, sector_size);
1194
1195 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1196 if (!regs) {
1197 dev_warn(host->dev,
1198 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1199 nand_chip->ecc.mode = NAND_ECC_SOFT;
1200 return 0;
1201 }
1202
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001203 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1204 if (IS_ERR(host->ecc)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001205 err_no = PTR_ERR(host->ecc);
1206 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001207 }
1208
1209 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001210 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1211 if (IS_ERR(host->pmerrloc_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001212 err_no = PTR_ERR(host->pmerrloc_base);
1213 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001214 }
Romain Izard55750752016-02-10 10:56:25 +01001215 host->pmerrloc_el_base = host->pmerrloc_base + ATMEL_PMERRLOC_SIGMAx +
1216 (host->caps->pmecc_max_correction + 1) * 4;
Josh Wu1c7b8742012-06-29 17:47:55 +08001217
Wu, Josh41c75402015-04-02 14:13:47 +08001218 if (!host->has_no_lookup_table) {
1219 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1220 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev,
1221 regs_rom);
1222 if (IS_ERR(host->pmecc_rom_base)) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001223 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 +08001224 host->has_no_lookup_table = true;
1225 }
Josh Wuabb1cd02014-10-11 18:01:50 +08001226 }
1227
1228 if (host->has_no_lookup_table) {
1229 /* Build the look-up table in runtime */
1230 galois_table = create_lookup_table(host->dev, sector_size);
1231 if (!galois_table) {
1232 dev_err(host->dev, "Failed to build a lookup table in runtime!\n");
1233 err_no = -EINVAL;
1234 goto err;
1235 }
1236
1237 host->pmecc_rom_base = (void __iomem *)galois_table;
1238 host->pmecc_lookup_table_offset = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +08001239 }
1240
Bo Shenb3857662014-06-12 15:58:45 +08001241 nand_chip->ecc.size = sector_size;
Josh Wu1c7b8742012-06-29 17:47:55 +08001242
1243 /* set ECC page size and oob layout */
1244 switch (mtd->writesize) {
Wu, Josha3557102014-07-22 17:24:18 +08001245 case 512:
1246 case 1024:
Josh Wu1c7b8742012-06-29 17:47:55 +08001247 case 2048:
Wu, Josha3557102014-07-22 17:24:18 +08001248 case 4096:
1249 case 8192:
1250 if (sector_size > mtd->writesize) {
1251 dev_err(host->dev, "pmecc sector size is bigger than the page size!\n");
1252 err_no = -EINVAL;
1253 goto err;
1254 }
1255
Josh Wu2fa831f2013-08-19 18:05:44 +08001256 host->pmecc_degree = (sector_size == 512) ?
1257 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Josh Wu1c7b8742012-06-29 17:47:55 +08001258 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +08001259 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1260 host->pmecc_index_of = host->pmecc_rom_base +
1261 host->pmecc_lookup_table_offset;
1262
Josh Wu1c7b8742012-06-29 17:47:55 +08001263 nand_chip->ecc.strength = cap;
Wu, Josh022a4782014-08-08 17:12:35 +08001264 nand_chip->ecc.bytes = pmecc_get_ecc_bytes(cap, sector_size);
Wu, Joshc9447ff2014-08-08 17:12:34 +08001265 nand_chip->ecc.steps = mtd->writesize / sector_size;
1266 nand_chip->ecc.total = nand_chip->ecc.bytes *
1267 nand_chip->ecc.steps;
Josh Wu477478a2015-04-02 14:12:33 +08001268 if (nand_chip->ecc.total >
1269 mtd->oobsize - PMECC_OOB_RESERVED_BYTES) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001270 dev_err(host->dev, "No room for ECC bytes\n");
1271 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001272 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001273 }
1274 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1275 mtd->oobsize,
Bo Shenb3857662014-06-12 15:58:45 +08001276 nand_chip->ecc.total);
1277
Josh Wu1c7b8742012-06-29 17:47:55 +08001278 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1279 break;
Wu, Josha3557102014-07-22 17:24:18 +08001280 default:
Josh Wu1c7b8742012-06-29 17:47:55 +08001281 dev_warn(host->dev,
1282 "Unsupported page size for PMECC, use Software ECC\n");
Josh Wu1c7b8742012-06-29 17:47:55 +08001283 /* page size not handled by HW ECC */
1284 /* switching back to soft ECC */
1285 nand_chip->ecc.mode = NAND_ECC_SOFT;
1286 return 0;
1287 }
1288
1289 /* Allocate data for PMECC computation */
1290 err_no = pmecc_data_alloc(host);
1291 if (err_no) {
1292 dev_err(host->dev,
1293 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001294 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001295 }
1296
Herve Codina90445ff2014-03-03 12:15:29 +01001297 nand_chip->options |= NAND_NO_SUBPAGE_WRITE;
Josh Wu1c7b8742012-06-29 17:47:55 +08001298 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1299 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1300
1301 atmel_pmecc_core_init(mtd);
1302
1303 return 0;
1304
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001305err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001306 return err_no;
1307}
1308
1309/*
Richard Genoud77f54922008-04-23 19:51:14 +02001310 * Calculate HW ECC
1311 *
1312 * function called after a write
1313 *
1314 * mtd: MTD block structure
1315 * dat: raw data (unused)
1316 * ecc_code: buffer for ECC
1317 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001318static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001319 const u_char *dat, unsigned char *ecc_code)
1320{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001321 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001322 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02001323 unsigned int ecc_value;
1324
1325 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001326 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001327
Richard Genoud3fc23892008-10-12 08:42:28 +02001328 ecc_code[0] = ecc_value & 0xFF;
1329 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001330
1331 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001332 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001333
Richard Genoud3fc23892008-10-12 08:42:28 +02001334 ecc_code[2] = ecc_value & 0xFF;
1335 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001336
1337 return 0;
1338}
1339
1340/*
1341 * HW ECC read page function
1342 *
1343 * mtd: mtd info structure
1344 * chip: nand chip info structure
1345 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001346 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001347 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001348static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1349 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001350{
1351 int eccsize = chip->ecc.size;
1352 int eccbytes = chip->ecc.bytes;
1353 uint32_t *eccpos = chip->ecc.layout->eccpos;
1354 uint8_t *p = buf;
1355 uint8_t *oob = chip->oob_poi;
1356 uint8_t *ecc_pos;
1357 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001358 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001359
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001360 /*
1361 * Errata: ALE is incorrectly wired up to the ECC controller
1362 * on the AP7000, so it will include the address cycles in the
1363 * ECC calculation.
1364 *
1365 * Workaround: Reset the parity registers before reading the
1366 * actual data.
1367 */
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001368 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu71b94e22013-05-09 15:34:54 +08001369 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001370 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001371
Richard Genoud77f54922008-04-23 19:51:14 +02001372 /* read the page */
1373 chip->read_buf(mtd, p, eccsize);
1374
1375 /* move to ECC position if needed */
1376 if (eccpos[0] != 0) {
1377 /* This only works on large pages
1378 * because the ECC controller waits for
1379 * NAND_CMD_RNDOUTSTART after the
1380 * NAND_CMD_RNDOUT.
1381 * anyway, for small pages, the eccpos[0] == 0
1382 */
1383 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1384 mtd->writesize + eccpos[0], -1);
1385 }
1386
1387 /* the ECC controller needs to read the ECC just after the data */
1388 ecc_pos = oob + eccpos[0];
1389 chip->read_buf(mtd, ecc_pos, eccbytes);
1390
1391 /* check if there's an error */
1392 stat = chip->ecc.correct(mtd, p, oob, NULL);
1393
Mike Dunn3f91e942012-04-25 12:06:09 -07001394 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001395 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001396 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001397 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001398 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1399 }
Richard Genoud77f54922008-04-23 19:51:14 +02001400
1401 /* get back to oob start (end of page) */
1402 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1403
1404 /* read the oob */
1405 chip->read_buf(mtd, oob, mtd->oobsize);
1406
Mike Dunn3f91e942012-04-25 12:06:09 -07001407 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001408}
1409
1410/*
1411 * HW ECC Correction
1412 *
1413 * function called after a read
1414 *
1415 * mtd: MTD block structure
1416 * dat: raw data read from the chip
1417 * read_ecc: ECC from the chip (unused)
1418 * isnull: unused
1419 *
1420 * Detect and correct a 1 bit error for a page
1421 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001422static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001423 u_char *read_ecc, u_char *isnull)
1424{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001425 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001426 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02001427 unsigned int ecc_status;
1428 unsigned int ecc_word, ecc_bit;
1429
1430 /* get the status from the Status Register */
1431 ecc_status = ecc_readl(host->ecc, SR);
1432
1433 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001434 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001435 return 0;
1436
1437 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001438 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001439 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001440 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001441 ecc_word >>= 4;
1442
1443 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001444 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001445 /* check if it is a freshly erased block
1446 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001447 if ((ecc_bit == ATMEL_ECC_BITADDR)
1448 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001449 /* the block has just been erased, return OK */
1450 return 0;
1451 }
1452 /* it doesn't seems to be a freshly
1453 * erased block.
1454 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001455 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001456 " Unable to correct.\n");
Boris BREZILLON6e941192015-12-30 20:32:03 +01001457 return -EBADMSG;
Richard Genoud77f54922008-04-23 19:51:14 +02001458 }
1459
1460 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001461 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001462 /* there's nothing much to do here.
1463 * the bit error is on the ECC itself.
1464 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001465 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001466 " Nothing to correct\n");
1467 return 0;
1468 }
1469
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001470 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001471 " (word offset in the page :"
1472 " 0x%x bit offset : 0x%x)\n",
1473 ecc_word, ecc_bit);
1474 /* correct the error */
1475 if (nand_chip->options & NAND_BUSWIDTH_16) {
1476 /* 16 bits words */
1477 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1478 } else {
1479 /* 8 bits words */
1480 dat[ecc_word] ^= (1 << ecc_bit);
1481 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001482 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001483 return 1;
1484}
1485
1486/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001487 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001488 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001489static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1490{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001491 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001492 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu71b94e22013-05-09 15:34:54 +08001493
1494 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001495 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001496}
Richard Genoud77f54922008-04-23 19:51:14 +02001497
Bill Pemberton06f25512012-11-19 13:23:07 -05001498static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001499 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001500{
Josh Wuc0cf7872013-01-23 20:47:08 +08001501 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001502 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001503 int ecc_mode;
1504 struct atmel_nand_data *board = &host->board;
Josh Wue9d8da82013-09-18 11:31:19 +08001505 enum of_gpio_flags flags = 0;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001506
Wu, Josh515857782015-01-19 16:33:06 +08001507 host->caps = (struct atmel_nand_caps *)
Romain Izard8ce06d32016-01-15 11:34:57 +01001508 of_device_get_match_data(host->dev);
Wu, Josh515857782015-01-19 16:33:06 +08001509
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001510 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1511 if (val >= 32) {
1512 dev_err(host->dev, "invalid addr-offset %u\n", val);
1513 return -EINVAL;
1514 }
1515 board->ale = val;
1516 }
1517
1518 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1519 if (val >= 32) {
1520 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1521 return -EINVAL;
1522 }
1523 board->cle = val;
1524 }
1525
1526 ecc_mode = of_get_nand_ecc_mode(np);
1527
1528 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1529
1530 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1531
Josh Wu1b719262013-05-09 15:34:55 +08001532 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1533
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001534 if (of_get_nand_bus_width(np) == 16)
1535 board->bus_width_16 = 1;
1536
1537 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1538 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1539
1540 board->enable_pin = of_get_gpio(np, 1);
1541 board->det_pin = of_get_gpio(np, 2);
1542
Josh Wua41b51a2012-06-29 17:47:54 +08001543 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1544
Josh Wu7dc37de2013-08-05 19:14:35 +08001545 /* load the nfc driver if there is */
1546 of_platform_populate(np, NULL, NULL, host->dev);
1547
Josh Wua41b51a2012-06-29 17:47:54 +08001548 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1549 return 0; /* Not using PMECC */
1550
1551 /* use PMECC, get correction capability, sector size and lookup
1552 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001553 * If correction bits and sector size are not specified, then find
1554 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001555 */
Josh Wue66b4312013-01-23 20:47:11 +08001556 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1557 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1558 (val != 24)) {
1559 dev_err(host->dev,
Romain Izarde88b7f72016-01-15 11:34:56 +01001560 "Required ECC strength not supported: %u\n",
Josh Wue66b4312013-01-23 20:47:11 +08001561 val);
1562 return -EINVAL;
1563 }
1564 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001565 }
Josh Wua41b51a2012-06-29 17:47:54 +08001566
Josh Wue66b4312013-01-23 20:47:11 +08001567 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1568 if ((val != 512) && (val != 1024)) {
1569 dev_err(host->dev,
Romain Izarde88b7f72016-01-15 11:34:56 +01001570 "Required ECC sector size not supported: %u\n",
Josh Wue66b4312013-01-23 20:47:11 +08001571 val);
1572 return -EINVAL;
1573 }
1574 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001575 }
Josh Wua41b51a2012-06-29 17:47:54 +08001576
1577 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1578 offset, 2) != 0) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001579 dev_err(host->dev, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n");
1580 host->has_no_lookup_table = true;
1581 /* Will build a lookup table and initialize the offset later */
1582 return 0;
Josh Wua41b51a2012-06-29 17:47:54 +08001583 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001584 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001585 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1586 return -EINVAL;
1587 }
Josh Wue66b4312013-01-23 20:47:11 +08001588 host->pmecc_lookup_table_offset_512 = offset[0];
1589 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001590
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001591 return 0;
1592}
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001593
Johan Hovold2c2b9282013-09-23 16:27:28 +02001594static int atmel_hw_nand_init_params(struct platform_device *pdev,
Josh Wu3dfe41a2012-06-25 18:07:43 +08001595 struct atmel_nand_host *host)
1596{
Josh Wu3dfe41a2012-06-25 18:07:43 +08001597 struct nand_chip *nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01001598 struct mtd_info *mtd = nand_to_mtd(nand_chip);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001599 struct resource *regs;
1600
1601 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1602 if (!regs) {
1603 dev_err(host->dev,
1604 "Can't get I/O resource regs, use software ECC\n");
1605 nand_chip->ecc.mode = NAND_ECC_SOFT;
1606 return 0;
1607 }
1608
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001609 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun8fb7b932014-07-28 21:19:55 +08001610 if (IS_ERR(host->ecc))
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001611 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001612
1613 /* ECC is calculated for the whole page (1 step) */
1614 nand_chip->ecc.size = mtd->writesize;
1615
1616 /* set ECC page size and oob layout */
1617 switch (mtd->writesize) {
1618 case 512:
1619 nand_chip->ecc.layout = &atmel_oobinfo_small;
1620 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1621 break;
1622 case 1024:
1623 nand_chip->ecc.layout = &atmel_oobinfo_large;
1624 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1625 break;
1626 case 2048:
1627 nand_chip->ecc.layout = &atmel_oobinfo_large;
1628 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1629 break;
1630 case 4096:
1631 nand_chip->ecc.layout = &atmel_oobinfo_large;
1632 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1633 break;
1634 default:
1635 /* page size not handled by HW ECC */
1636 /* switching back to soft ECC */
1637 nand_chip->ecc.mode = NAND_ECC_SOFT;
1638 return 0;
1639 }
1640
1641 /* set up for HW ECC */
1642 nand_chip->ecc.calculate = atmel_nand_calculate;
1643 nand_chip->ecc.correct = atmel_nand_correct;
1644 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1645 nand_chip->ecc.read_page = atmel_nand_read_page;
1646 nand_chip->ecc.bytes = 4;
1647 nand_chip->ecc.strength = 1;
1648
1649 return 0;
1650}
1651
Wu, Josh50e04e22014-06-10 17:50:09 +08001652static inline u32 nfc_read_status(struct atmel_nand_host *host)
1653{
1654 u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
1655 u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
1656
1657 if (unlikely(nfc_status & err_flags)) {
1658 if (nfc_status & NFC_SR_DTOE)
1659 dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
1660 else if (nfc_status & NFC_SR_UNDEF)
1661 dev_err(host->dev, "NFC: Access Undefined Area Error\n");
1662 else if (nfc_status & NFC_SR_AWB)
1663 dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
1664 else if (nfc_status & NFC_SR_ASE)
1665 dev_err(host->dev, "NFC: Access memory Size Error\n");
1666 }
1667
1668 return nfc_status;
1669}
1670
Josh Wu7dc37de2013-08-05 19:14:35 +08001671/* SMC interrupt service routine */
1672static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
1673{
1674 struct atmel_nand_host *host = dev_id;
1675 u32 status, mask, pending;
Josh Wue4e06932014-06-10 17:50:11 +08001676 irqreturn_t ret = IRQ_NONE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001677
Wu, Josh50e04e22014-06-10 17:50:09 +08001678 status = nfc_read_status(host);
Josh Wu7dc37de2013-08-05 19:14:35 +08001679 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1680 pending = status & mask;
1681
1682 if (pending & NFC_SR_XFR_DONE) {
Josh Wue4e06932014-06-10 17:50:11 +08001683 complete(&host->nfc->comp_xfer_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001684 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001685 ret = IRQ_HANDLED;
1686 }
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001687 if (pending & host->nfc->caps->rb_mask) {
Josh Wue4e06932014-06-10 17:50:11 +08001688 complete(&host->nfc->comp_ready);
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001689 nfc_writel(host->nfc->hsmc_regs, IDR, host->nfc->caps->rb_mask);
Josh Wue4e06932014-06-10 17:50:11 +08001690 ret = IRQ_HANDLED;
1691 }
1692 if (pending & NFC_SR_CMD_DONE) {
1693 complete(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001694 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001695 ret = IRQ_HANDLED;
Josh Wu7dc37de2013-08-05 19:14:35 +08001696 }
1697
1698 return ret;
1699}
1700
1701/* NFC(Nand Flash Controller) related functions */
Josh Wue4e06932014-06-10 17:50:11 +08001702static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag)
Josh Wu7dc37de2013-08-05 19:14:35 +08001703{
Josh Wue4e06932014-06-10 17:50:11 +08001704 if (flag & NFC_SR_XFR_DONE)
1705 init_completion(&host->nfc->comp_xfer_done);
1706
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001707 if (flag & host->nfc->caps->rb_mask)
Josh Wue4e06932014-06-10 17:50:11 +08001708 init_completion(&host->nfc->comp_ready);
1709
1710 if (flag & NFC_SR_CMD_DONE)
1711 init_completion(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001712
1713 /* Enable interrupt that need to wait for */
1714 nfc_writel(host->nfc->hsmc_regs, IER, flag);
Josh Wue4e06932014-06-10 17:50:11 +08001715}
Josh Wu7dc37de2013-08-05 19:14:35 +08001716
Josh Wue4e06932014-06-10 17:50:11 +08001717static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
1718{
1719 int i, index = 0;
1720 struct completion *comp[3]; /* Support 3 interrupt completion */
Josh Wu7dc37de2013-08-05 19:14:35 +08001721
Josh Wue4e06932014-06-10 17:50:11 +08001722 if (flag & NFC_SR_XFR_DONE)
1723 comp[index++] = &host->nfc->comp_xfer_done;
1724
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001725 if (flag & host->nfc->caps->rb_mask)
Josh Wue4e06932014-06-10 17:50:11 +08001726 comp[index++] = &host->nfc->comp_ready;
1727
1728 if (flag & NFC_SR_CMD_DONE)
1729 comp[index++] = &host->nfc->comp_cmd_done;
1730
1731 if (index == 0) {
Colin Ian King393d23c2015-02-28 20:27:56 +00001732 dev_err(host->dev, "Unknown interrupt flag: 0x%08x\n", flag);
Josh Wue4e06932014-06-10 17:50:11 +08001733 return -EINVAL;
1734 }
1735
1736 for (i = 0; i < index; i++) {
1737 if (wait_for_completion_timeout(comp[i],
1738 msecs_to_jiffies(NFC_TIME_OUT_MS)))
1739 continue; /* wait for next completion */
1740 else
1741 goto err_timeout;
1742 }
1743
1744 return 0;
1745
1746err_timeout:
Josh Wu7dc37de2013-08-05 19:14:35 +08001747 dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
Josh Wue4e06932014-06-10 17:50:11 +08001748 /* Disable the interrupt as it is not handled by interrupt handler */
1749 nfc_writel(host->nfc->hsmc_regs, IDR, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001750 return -ETIMEDOUT;
1751}
1752
1753static int nfc_send_command(struct atmel_nand_host *host,
1754 unsigned int cmd, unsigned int addr, unsigned char cycle0)
1755{
1756 unsigned long timeout;
Josh Wue4e06932014-06-10 17:50:11 +08001757 u32 flag = NFC_SR_CMD_DONE;
1758 flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0;
1759
Josh Wu7dc37de2013-08-05 19:14:35 +08001760 dev_dbg(host->dev,
1761 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1762 cmd, addr, cycle0);
1763
1764 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
Boris Brezillon111573cc2015-01-22 18:21:30 +08001765 while (nfc_readl(host->nfc->hsmc_regs, SR) & NFC_SR_BUSY) {
Josh Wu7dc37de2013-08-05 19:14:35 +08001766 if (time_after(jiffies, timeout)) {
1767 dev_err(host->dev,
Boris Brezillon111573cc2015-01-22 18:21:30 +08001768 "Time out to wait for NFC ready!\n");
Josh Wu7dc37de2013-08-05 19:14:35 +08001769 return -ETIMEDOUT;
1770 }
1771 }
Josh Wue4e06932014-06-10 17:50:11 +08001772
1773 nfc_prepare_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001774 nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
1775 nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
Josh Wue4e06932014-06-10 17:50:11 +08001776 return nfc_wait_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001777}
1778
1779static int nfc_device_ready(struct mtd_info *mtd)
1780{
Wu, Josh72a78e32014-06-10 17:50:10 +08001781 u32 status, mask;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001782 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001783 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Wu, Josh72a78e32014-06-10 17:50:10 +08001784
1785 status = nfc_read_status(host);
1786 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1787
1788 /* The mask should be 0. If not we may lost interrupts */
1789 if (unlikely(mask & status))
1790 dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n",
1791 mask & status);
1792
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001793 return status & host->nfc->caps->rb_mask;
Josh Wu7dc37de2013-08-05 19:14:35 +08001794}
1795
1796static void nfc_select_chip(struct mtd_info *mtd, int chip)
1797{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001798 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001799 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu7dc37de2013-08-05 19:14:35 +08001800
1801 if (chip == -1)
1802 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
1803 else
1804 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
1805}
1806
Brian Norris3dad2342014-01-29 14:08:12 -08001807static int nfc_make_addr(struct mtd_info *mtd, int command, int column,
1808 int page_addr, unsigned int *addr1234, unsigned int *cycle0)
Josh Wu7dc37de2013-08-05 19:14:35 +08001809{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001810 struct nand_chip *chip = mtd_to_nand(mtd);
Josh Wu7dc37de2013-08-05 19:14:35 +08001811
1812 int acycle = 0;
1813 unsigned char addr_bytes[8];
1814 int index = 0, bit_shift;
1815
1816 BUG_ON(addr1234 == NULL || cycle0 == NULL);
1817
1818 *cycle0 = 0;
1819 *addr1234 = 0;
1820
1821 if (column != -1) {
Brian Norris3dad2342014-01-29 14:08:12 -08001822 if (chip->options & NAND_BUSWIDTH_16 &&
1823 !nand_opcode_8bits(command))
Josh Wu7dc37de2013-08-05 19:14:35 +08001824 column >>= 1;
1825 addr_bytes[acycle++] = column & 0xff;
1826 if (mtd->writesize > 512)
1827 addr_bytes[acycle++] = (column >> 8) & 0xff;
1828 }
1829
1830 if (page_addr != -1) {
1831 addr_bytes[acycle++] = page_addr & 0xff;
1832 addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
1833 if (chip->chipsize > (128 << 20))
1834 addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
1835 }
1836
1837 if (acycle > 4)
1838 *cycle0 = addr_bytes[index++];
1839
1840 for (bit_shift = 0; index < acycle; bit_shift += 8)
1841 *addr1234 += addr_bytes[index++] << bit_shift;
1842
1843 /* return acycle in cmd register */
1844 return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
1845}
1846
1847static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
1848 int column, int page_addr)
1849{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001850 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001851 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu7dc37de2013-08-05 19:14:35 +08001852 unsigned long timeout;
1853 unsigned int nfc_addr_cmd = 0;
1854
1855 unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1856
1857 /* Set default settings: no cmd2, no addr cycle. read from nand */
1858 unsigned int cmd2 = 0;
1859 unsigned int vcmd2 = 0;
1860 int acycle = NFCADDR_CMD_ACYCLE_NONE;
1861 int csid = NFCADDR_CMD_CSID_3;
1862 int dataen = NFCADDR_CMD_DATADIS;
1863 int nfcwr = NFCADDR_CMD_NFCRD;
1864 unsigned int addr1234 = 0;
1865 unsigned int cycle0 = 0;
1866 bool do_addr = true;
Josh Wu1ae9c092013-08-05 19:14:36 +08001867 host->nfc->data_in_sram = NULL;
Josh Wu7dc37de2013-08-05 19:14:35 +08001868
1869 dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1870 __func__, command, column, page_addr);
1871
1872 switch (command) {
1873 case NAND_CMD_RESET:
1874 nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
1875 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1876 udelay(chip->chip_delay);
1877
1878 nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
1879 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1880 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
1881 if (time_after(jiffies, timeout)) {
1882 dev_err(host->dev,
1883 "Time out to wait status ready!\n");
1884 break;
1885 }
1886 }
1887 return;
1888 case NAND_CMD_STATUS:
1889 do_addr = false;
1890 break;
1891 case NAND_CMD_PARAM:
1892 case NAND_CMD_READID:
1893 do_addr = false;
1894 acycle = NFCADDR_CMD_ACYCLE_1;
1895 if (column != -1)
1896 addr1234 = column;
1897 break;
1898 case NAND_CMD_RNDOUT:
1899 cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
1900 vcmd2 = NFCADDR_CMD_VCMD2;
1901 break;
1902 case NAND_CMD_READ0:
1903 case NAND_CMD_READOOB:
1904 if (command == NAND_CMD_READOOB) {
1905 column += mtd->writesize;
1906 command = NAND_CMD_READ0; /* only READ0 is valid */
1907 cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1908 }
Josh Wu1ae9c092013-08-05 19:14:36 +08001909 if (host->nfc->use_nfc_sram) {
1910 /* Enable Data transfer to sram */
1911 dataen = NFCADDR_CMD_DATAEN;
1912
1913 /* Need enable PMECC now, since NFC will transfer
1914 * data in bus after sending nfc read command.
1915 */
1916 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1917 pmecc_enable(host, NAND_ECC_READ);
1918 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001919
1920 cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
1921 vcmd2 = NFCADDR_CMD_VCMD2;
1922 break;
1923 /* For prgramming command, the cmd need set to write enable */
1924 case NAND_CMD_PAGEPROG:
1925 case NAND_CMD_SEQIN:
1926 case NAND_CMD_RNDIN:
1927 nfcwr = NFCADDR_CMD_NFCWR;
Josh Wu6054d4d2013-08-05 19:14:37 +08001928 if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
1929 dataen = NFCADDR_CMD_DATAEN;
Josh Wu7dc37de2013-08-05 19:14:35 +08001930 break;
1931 default:
1932 break;
1933 }
1934
1935 if (do_addr)
Brian Norris3dad2342014-01-29 14:08:12 -08001936 acycle = nfc_make_addr(mtd, command, column, page_addr,
1937 &addr1234, &cycle0);
Josh Wu7dc37de2013-08-05 19:14:35 +08001938
1939 nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
1940 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1941
1942 /*
1943 * Program and erase have their own busy handlers status, sequential
1944 * in, and deplete1 need no delay.
1945 */
1946 switch (command) {
1947 case NAND_CMD_CACHEDPROG:
1948 case NAND_CMD_PAGEPROG:
1949 case NAND_CMD_ERASE1:
1950 case NAND_CMD_ERASE2:
1951 case NAND_CMD_RNDIN:
1952 case NAND_CMD_STATUS:
1953 case NAND_CMD_RNDOUT:
1954 case NAND_CMD_SEQIN:
1955 case NAND_CMD_READID:
1956 return;
1957
1958 case NAND_CMD_READ0:
Josh Wu1ae9c092013-08-05 19:14:36 +08001959 if (dataen == NFCADDR_CMD_DATAEN) {
1960 host->nfc->data_in_sram = host->nfc->sram_bank0 +
1961 nfc_get_sram_off(host);
1962 return;
1963 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001964 /* fall through */
1965 default:
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001966 nfc_prepare_interrupt(host, host->nfc->caps->rb_mask);
1967 nfc_wait_interrupt(host, host->nfc->caps->rb_mask);
Josh Wu7dc37de2013-08-05 19:14:35 +08001968 }
1969}
1970
Josh Wu6054d4d2013-08-05 19:14:37 +08001971static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1972 uint32_t offset, int data_len, const uint8_t *buf,
1973 int oob_required, int page, int cached, int raw)
1974{
1975 int cfg, len;
1976 int status = 0;
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001977 struct atmel_nand_host *host = nand_get_controller_data(chip);
Wu, Josh068b44b2014-11-07 15:26:09 +08001978 void *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
Josh Wu6054d4d2013-08-05 19:14:37 +08001979
1980 /* Subpage write is not supported */
1981 if (offset || (data_len < mtd->writesize))
1982 return -EINVAL;
1983
Josh Wu6054d4d2013-08-05 19:14:37 +08001984 len = mtd->writesize;
Josh Wu6054d4d2013-08-05 19:14:37 +08001985 /* Copy page data to sram that will write to nand via NFC */
1986 if (use_dma) {
1987 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
1988 /* Fall back to use cpu copy */
Wu, Josh068b44b2014-11-07 15:26:09 +08001989 memcpy(sram, buf, len);
Josh Wu6054d4d2013-08-05 19:14:37 +08001990 } else {
Wu, Josh068b44b2014-11-07 15:26:09 +08001991 memcpy(sram, buf, len);
Josh Wu6054d4d2013-08-05 19:14:37 +08001992 }
1993
Wu, Joshff0a2152014-08-05 18:38:52 +08001994 cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
1995 if (unlikely(raw) && oob_required) {
Wu, Josh068b44b2014-11-07 15:26:09 +08001996 memcpy(sram + len, chip->oob_poi, mtd->oobsize);
Wu, Joshff0a2152014-08-05 18:38:52 +08001997 len += mtd->oobsize;
1998 nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
1999 } else {
2000 nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
2001 }
2002
Josh Wu6054d4d2013-08-05 19:14:37 +08002003 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
2004 /*
2005 * When use NFC sram, need set up PMECC before send
2006 * NAND_CMD_SEQIN command. Since when the nand command
2007 * is sent, nfc will do transfer from sram and nand.
2008 */
2009 pmecc_enable(host, NAND_ECC_WRITE);
2010
2011 host->nfc->will_write_sram = true;
2012 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2013 host->nfc->will_write_sram = false;
2014
2015 if (likely(!raw))
2016 /* Need to write ecc into oob */
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002017 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2018 page);
Josh Wu6054d4d2013-08-05 19:14:37 +08002019
2020 if (status < 0)
2021 return status;
2022
2023 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2024 status = chip->waitfunc(mtd, chip);
2025
2026 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2027 status = chip->errstat(mtd, chip, FL_WRITING, status, page);
2028
2029 if (status & NAND_STATUS_FAIL)
2030 return -EIO;
2031
2032 return 0;
2033}
2034
Josh Wu1ae9c092013-08-05 19:14:36 +08002035static int nfc_sram_init(struct mtd_info *mtd)
2036{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01002037 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01002038 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu1ae9c092013-08-05 19:14:36 +08002039 int res = 0;
2040
2041 /* Initialize the NFC CFG register */
2042 unsigned int cfg_nfc = 0;
2043
2044 /* set page size and oob layout */
2045 switch (mtd->writesize) {
2046 case 512:
2047 cfg_nfc = NFC_CFG_PAGESIZE_512;
2048 break;
2049 case 1024:
2050 cfg_nfc = NFC_CFG_PAGESIZE_1024;
2051 break;
2052 case 2048:
2053 cfg_nfc = NFC_CFG_PAGESIZE_2048;
2054 break;
2055 case 4096:
2056 cfg_nfc = NFC_CFG_PAGESIZE_4096;
2057 break;
2058 case 8192:
2059 cfg_nfc = NFC_CFG_PAGESIZE_8192;
2060 break;
2061 default:
2062 dev_err(host->dev, "Unsupported page size for NFC.\n");
2063 res = -ENXIO;
2064 return res;
2065 }
2066
2067 /* oob bytes size = (NFCSPARESIZE + 1) * 4
2068 * Max support spare size is 512 bytes. */
2069 cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
2070 & NFC_CFG_NFC_SPARESIZE);
2071 /* default set a max timeout */
2072 cfg_nfc |= NFC_CFG_RSPARE |
2073 NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
2074
2075 nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
2076
Josh Wu6054d4d2013-08-05 19:14:37 +08002077 host->nfc->will_write_sram = false;
Josh Wu1ae9c092013-08-05 19:14:36 +08002078 nfc_set_sram_bank(host, 0);
2079
Josh Wu6054d4d2013-08-05 19:14:37 +08002080 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
2081 if (host->nfc->write_by_sram) {
2082 if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
2083 chip->ecc.mode == NAND_ECC_NONE)
2084 chip->write_page = nfc_sram_write_page;
2085 else
2086 host->nfc->write_by_sram = false;
2087 }
Josh Wu1ae9c092013-08-05 19:14:36 +08002088
Josh Wu6054d4d2013-08-05 19:14:37 +08002089 dev_info(host->dev, "Using NFC Sram read %s\n",
2090 host->nfc->write_by_sram ? "and write" : "");
Josh Wu1ae9c092013-08-05 19:14:36 +08002091 return 0;
2092}
2093
Josh Wu7dc37de2013-08-05 19:14:35 +08002094static struct platform_driver atmel_nand_nfc_driver;
Andrew Victor42cb1402006-10-19 18:24:35 +02002095/*
2096 * Probe for the NAND device.
2097 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002098static int atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002099{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002100 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02002101 struct mtd_info *mtd;
2102 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002103 struct resource *mem;
Josh Wu7dc37de2013-08-05 19:14:35 +08002104 int res, irq;
Andrew Victor42cb1402006-10-19 18:24:35 +02002105
2106 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002107 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Han9e3677a2013-12-26 12:00:16 +09002108 if (!host)
Andrew Victor42cb1402006-10-19 18:24:35 +02002109 return -ENOMEM;
Andrew Victor42cb1402006-10-19 18:24:35 +02002110
Josh Wu7dc37de2013-08-05 19:14:35 +08002111 res = platform_driver_register(&atmel_nand_nfc_driver);
2112 if (res)
2113 dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
2114
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002115 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2116 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
2117 if (IS_ERR(host->io_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002118 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002119 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02002120 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002121 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02002122
Andrew Victor42cb1402006-10-19 18:24:35 +02002123 nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01002124 mtd = nand_to_mtd(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02002125 host->dev = &pdev->dev;
Josh Wue9d8da82013-09-18 11:31:19 +08002126 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
Brian Norrisa61ae812015-10-30 20:33:25 -07002127 nand_set_flash_node(nand_chip, pdev->dev.of_node);
Josh Wue9d8da82013-09-18 11:31:19 +08002128 /* Only when CONFIG_OF is enabled of_node can be parsed */
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002129 res = atmel_of_init_port(host, pdev->dev.of_node);
2130 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002131 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002132 } else {
Jingoo Han453810b2013-07-30 17:18:33 +09002133 memcpy(&host->board, dev_get_platdata(&pdev->dev),
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002134 sizeof(struct atmel_nand_data));
2135 }
Andrew Victor42cb1402006-10-19 18:24:35 +02002136
Boris BREZILLONd699ed22015-12-10 09:00:41 +01002137 /* link the private data structures */
2138 nand_set_controller_data(nand_chip, host);
Frans Klaver03c287d2015-06-10 22:38:36 +02002139 mtd->dev.parent = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +02002140
2141 /* Set address of NAND IO lines */
2142 nand_chip->IO_ADDR_R = host->io_base;
2143 nand_chip->IO_ADDR_W = host->io_base;
Ivan Kutena4265f82007-05-24 14:35:58 +03002144
Josh Wu7dc37de2013-08-05 19:14:35 +08002145 if (nand_nfc.is_initialized) {
2146 /* NFC driver is probed and initialized */
2147 host->nfc = &nand_nfc;
2148
2149 nand_chip->select_chip = nfc_select_chip;
2150 nand_chip->dev_ready = nfc_device_ready;
2151 nand_chip->cmdfunc = nfc_nand_command;
2152
2153 /* Initialize the interrupt for NFC */
2154 irq = platform_get_irq(pdev, 0);
2155 if (irq < 0) {
2156 dev_err(host->dev, "Cannot get HSMC irq!\n");
Wei Yongjunff52c672013-08-23 10:50:36 +08002157 res = irq;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002158 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002159 }
2160
Josh Wu7dc37de2013-08-05 19:14:35 +08002161 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
2162 0, "hsmc", host);
2163 if (res) {
2164 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
2165 irq);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002166 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002167 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002168 } else {
2169 res = atmel_nand_set_enable_ready_pins(mtd);
2170 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002171 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002172
Josh Wu7dc37de2013-08-05 19:14:35 +08002173 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002174 }
Ivan Kutena4265f82007-05-24 14:35:58 +03002175
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002176 nand_chip->ecc.mode = host->board.ecc_mode;
Raphaël Poggi796fe362014-07-29 15:27:27 +02002177 nand_chip->chip_delay = 40; /* 40us command delay time */
Andrew Victor42cb1402006-10-19 18:24:35 +02002178
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002179 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02002180 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08002181
2182 nand_chip->read_buf = atmel_read_buf;
2183 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02002184
Andrew Victor42cb1402006-10-19 18:24:35 +02002185 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002186 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002187
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002188 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002189 res = devm_gpio_request(&pdev->dev,
2190 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002191 if (res < 0) {
2192 dev_err(&pdev->dev,
2193 "can't request det gpio %d\n",
2194 host->board.det_pin);
2195 goto err_no_card;
2196 }
2197
2198 res = gpio_direction_input(host->board.det_pin);
2199 if (res < 0) {
2200 dev_err(&pdev->dev,
2201 "can't request input direction det gpio %d\n",
2202 host->board.det_pin);
2203 goto err_no_card;
2204 }
2205
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002206 if (gpio_get_value(host->board.det_pin)) {
Jingoo Han1295f972013-12-26 12:30:58 +09002207 dev_info(&pdev->dev, "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01002208 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002209 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02002210 }
2211 }
2212
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002213 if (host->board.on_flash_bbt || on_flash_bbt) {
Jingoo Han1295f972013-12-26 12:30:58 +09002214 dev_info(&pdev->dev, "Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -07002215 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa6972009-05-27 18:19:39 +03002216 }
2217
Josh Wu1b719262013-05-09 15:34:55 +08002218 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08002219 use_dma = 0;
2220
2221 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08002222 dma_cap_mask_t mask;
2223
2224 dma_cap_zero(mask);
2225 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02002226 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002227 if (!host->dma_chan) {
2228 dev_err(host->dev, "Failed to request DMA channel\n");
2229 use_dma = 0;
2230 }
2231 }
2232 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08002233 dev_info(host->dev, "Using %s for DMA transfers.\n",
2234 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08002235 else
2236 dev_info(host->dev, "No DMA support for NAND access.\n");
2237
Richard Genoud77f54922008-04-23 19:51:14 +02002238 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00002239 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02002240 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002241 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02002242 }
2243
Richard Genoud3fc23892008-10-12 08:42:28 +02002244 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08002245 if (host->has_pmecc)
2246 res = atmel_pmecc_nand_init_params(pdev, host);
2247 else
2248 res = atmel_hw_nand_init_params(pdev, host);
2249
Josh Wu3dfe41a2012-06-25 18:07:43 +08002250 if (res != 0)
2251 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02002252 }
2253
Josh Wu1ae9c092013-08-05 19:14:36 +08002254 /* initialize the nfc configuration register */
2255 if (host->nfc && host->nfc->use_nfc_sram) {
2256 res = nfc_sram_init(mtd);
2257 if (res) {
2258 host->nfc->use_nfc_sram = false;
2259 dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
2260 }
2261 }
2262
Richard Genoud77f54922008-04-23 19:51:14 +02002263 /* second phase scan */
2264 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02002265 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002266 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02002267 }
2268
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002269 mtd->name = "atmel_nand";
Brian Norrisa61ae812015-10-30 20:33:25 -07002270 res = mtd_device_register(mtd, host->board.parts,
2271 host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02002272 if (!res)
2273 return res;
2274
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002275err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002276 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08002277 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08002278err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002279err_scan_ident:
2280err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002281 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002282 if (host->dma_chan)
2283 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002284err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02002285 return res;
2286}
2287
2288/*
2289 * Remove a NAND device.
2290 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002291static int atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002292{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002293 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Boris BREZILLONac01efe2015-12-10 08:59:50 +01002294 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +02002295
2296 nand_release(mtd);
2297
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002298 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002299
Josh Wu1c7b8742012-06-29 17:47:55 +08002300 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
2301 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2302 pmerrloc_writel(host->pmerrloc_base, ELDIS,
2303 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08002304 }
2305
Hong Xucbc6c5e2011-01-18 14:36:05 +08002306 if (host->dma_chan)
2307 dma_release_channel(host->dma_chan);
2308
Josh Wu7dc37de2013-08-05 19:14:35 +08002309 platform_driver_unregister(&atmel_nand_nfc_driver);
2310
Andrew Victor42cb1402006-10-19 18:24:35 +02002311 return 0;
2312}
2313
Romain Izard55750752016-02-10 10:56:25 +01002314/*
2315 * AT91RM9200 does not have PMECC or PMECC Errloc peripherals for
2316 * BCH ECC. Combined with the "atmel,has-pmecc", it is used to describe
2317 * devices from the SAM9 family that have those.
2318 */
LABBE Corentin72eaec22015-11-20 08:45:16 +01002319static const struct atmel_nand_caps at91rm9200_caps = {
Wu, Josh515857782015-01-19 16:33:06 +08002320 .pmecc_correct_erase_page = false,
Romain Izard55750752016-02-10 10:56:25 +01002321 .pmecc_max_correction = 24,
Wu, Josh515857782015-01-19 16:33:06 +08002322};
2323
LABBE Corentin72eaec22015-11-20 08:45:16 +01002324static const struct atmel_nand_caps sama5d4_caps = {
Wu, Josh515857782015-01-19 16:33:06 +08002325 .pmecc_correct_erase_page = true,
Romain Izard55750752016-02-10 10:56:25 +01002326 .pmecc_max_correction = 24,
2327};
2328
2329/*
2330 * The PMECC Errloc controller starting in SAMA5D2 is not compatible,
2331 * as the increased correction strength requires more registers.
2332 */
2333static const struct atmel_nand_caps sama5d2_caps = {
2334 .pmecc_correct_erase_page = true,
2335 .pmecc_max_correction = 32,
Wu, Josh515857782015-01-19 16:33:06 +08002336};
2337
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002338static const struct of_device_id atmel_nand_dt_ids[] = {
Wu, Josh515857782015-01-19 16:33:06 +08002339 { .compatible = "atmel,at91rm9200-nand", .data = &at91rm9200_caps },
2340 { .compatible = "atmel,sama5d4-nand", .data = &sama5d4_caps },
Romain Izard55750752016-02-10 10:56:25 +01002341 { .compatible = "atmel,sama5d2-nand", .data = &sama5d2_caps },
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002342 { /* sentinel */ }
2343};
2344
2345MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002346
Josh Wu7dc37de2013-08-05 19:14:35 +08002347static int atmel_nand_nfc_probe(struct platform_device *pdev)
2348{
2349 struct atmel_nfc *nfc = &nand_nfc;
2350 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002351 int ret;
Josh Wu7dc37de2013-08-05 19:14:35 +08002352
2353 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2354 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
2355 if (IS_ERR(nfc->base_cmd_regs))
2356 return PTR_ERR(nfc->base_cmd_regs);
2357
2358 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2359 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
2360 if (IS_ERR(nfc->hsmc_regs))
2361 return PTR_ERR(nfc->hsmc_regs);
2362
2363 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2364 if (nfc_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +08002365 nfc->sram_bank0 = (void * __force)
2366 devm_ioremap_resource(&pdev->dev, nfc_sram);
Josh Wu1ae9c092013-08-05 19:14:36 +08002367 if (IS_ERR(nfc->sram_bank0)) {
Josh Wu7dc37de2013-08-05 19:14:35 +08002368 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2369 PTR_ERR(nfc->sram_bank0));
Josh Wu1ae9c092013-08-05 19:14:36 +08002370 } else {
2371 nfc->use_nfc_sram = true;
Josh Wu7dc37de2013-08-05 19:14:35 +08002372 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
Josh Wu6054d4d2013-08-05 19:14:37 +08002373
2374 if (pdev->dev.of_node)
2375 nfc->write_by_sram = of_property_read_bool(
2376 pdev->dev.of_node,
2377 "atmel,write-by-sram");
Josh Wu1ae9c092013-08-05 19:14:36 +08002378 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002379 }
2380
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002381 nfc->caps = (const struct atmel_nand_nfc_caps *)
2382 of_device_get_match_data(&pdev->dev);
2383 if (!nfc->caps)
2384 return -ENODEV;
2385
Wu, Josh50e04e22014-06-10 17:50:09 +08002386 nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
2387 nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */
2388
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002389 nfc->clk = devm_clk_get(&pdev->dev, NULL);
2390 if (!IS_ERR(nfc->clk)) {
2391 ret = clk_prepare_enable(nfc->clk);
2392 if (ret)
2393 return ret;
2394 } else {
2395 dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree");
2396 }
2397
Josh Wu7dc37de2013-08-05 19:14:35 +08002398 nfc->is_initialized = true;
2399 dev_info(&pdev->dev, "NFC is probed.\n");
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002400
2401 return 0;
2402}
2403
2404static int atmel_nand_nfc_remove(struct platform_device *pdev)
2405{
2406 struct atmel_nfc *nfc = &nand_nfc;
2407
2408 if (!IS_ERR(nfc->clk))
2409 clk_disable_unprepare(nfc->clk);
2410
Josh Wu7dc37de2013-08-05 19:14:35 +08002411 return 0;
2412}
2413
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002414static const struct atmel_nand_nfc_caps sama5d3_nfc_caps = {
2415 .rb_mask = NFC_SR_RB_EDGE0,
2416};
2417
2418static const struct atmel_nand_nfc_caps sama5d4_nfc_caps = {
2419 .rb_mask = NFC_SR_RB_EDGE3,
2420};
2421
Josh Wu81f29b42013-09-18 11:31:20 +08002422static const struct of_device_id atmel_nand_nfc_match[] = {
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002423 { .compatible = "atmel,sama5d3-nfc", .data = &sama5d3_nfc_caps },
2424 { .compatible = "atmel,sama5d4-nfc", .data = &sama5d4_nfc_caps },
Josh Wu7dc37de2013-08-05 19:14:35 +08002425 { /* sentinel */ }
2426};
Josh Wu81f29b42013-09-18 11:31:20 +08002427MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
Josh Wu7dc37de2013-08-05 19:14:35 +08002428
2429static struct platform_driver atmel_nand_nfc_driver = {
2430 .driver = {
2431 .name = "atmel_nand_nfc",
Josh Wu7dc37de2013-08-05 19:14:35 +08002432 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2433 },
2434 .probe = atmel_nand_nfc_probe,
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002435 .remove = atmel_nand_nfc_remove,
Josh Wu7dc37de2013-08-05 19:14:35 +08002436};
2437
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002438static struct platform_driver atmel_nand_driver = {
Johan Hovold2c2b9282013-09-23 16:27:28 +02002439 .probe = atmel_nand_probe,
2440 .remove = atmel_nand_remove,
Andrew Victor42cb1402006-10-19 18:24:35 +02002441 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002442 .name = "atmel_nand",
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002443 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02002444 },
2445};
2446
Johan Hovold2c2b9282013-09-23 16:27:28 +02002447module_platform_driver(atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02002448
2449MODULE_LICENSE("GPL");
2450MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02002451MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002452MODULE_ALIAS("platform:atmel_nand");