blob: 0b5da727144021933e458f12a0ef84a3caaa5c48 [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, Josh51585772015-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, Josh51585772015-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
Romain Izard94248462016-02-10 10:56:26 +0100478 * 32-bits 52-bytes 56-bytes
Josh Wu1c7b8742012-06-29 17:47:55 +0800479 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500480static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800481{
482 int m = 12 + sector_size / 512;
483 return (m * cap + 7) / 8;
484}
485
Bill Pemberton06f25512012-11-19 13:23:07 -0500486static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800487 int oobsize, int ecc_len)
Josh Wu1c7b8742012-06-29 17:47:55 +0800488{
489 int i;
490
491 layout->eccbytes = ecc_len;
492
493 /* ECC will occupy the last ecc_len bytes continuously */
494 for (i = 0; i < ecc_len; i++)
495 layout->eccpos[i] = oobsize - ecc_len + i;
496
Josh Wu477478a2015-04-02 14:12:33 +0800497 layout->oobfree[0].offset = PMECC_OOB_RESERVED_BYTES;
Josh Wu1c7b8742012-06-29 17:47:55 +0800498 layout->oobfree[0].length =
499 oobsize - ecc_len - layout->oobfree[0].offset;
500}
501
Bill Pemberton06f25512012-11-19 13:23:07 -0500502static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800503{
504 int table_size;
505
506 table_size = host->pmecc_sector_size == 512 ?
507 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
508
509 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
510 table_size * sizeof(int16_t);
511}
512
Bill Pemberton06f25512012-11-19 13:23:07 -0500513static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800514{
515 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800516 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800517
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800518 size = (2 * cap + 1) * sizeof(int16_t);
519 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
520 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
521 host->pmecc_lmu = devm_kzalloc(host->dev,
522 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
523 host->pmecc_smu = devm_kzalloc(host->dev,
524 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800525
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800526 size = (cap + 1) * sizeof(int);
527 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
528 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
529 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800530
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800531 if (!host->pmecc_partial_syn ||
532 !host->pmecc_si ||
533 !host->pmecc_lmu ||
534 !host->pmecc_smu ||
535 !host->pmecc_mu ||
536 !host->pmecc_dmu ||
537 !host->pmecc_delta)
538 return -ENOMEM;
539
540 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800541}
542
543static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
544{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100545 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100546 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800547 int i;
548 uint32_t value;
549
550 /* Fill odd syndromes */
551 for (i = 0; i < host->pmecc_corr_cap; i++) {
552 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
553 if (i & 1)
554 value >>= 16;
555 value &= 0xffff;
556 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
557 }
558}
559
560static void pmecc_substitute(struct mtd_info *mtd)
561{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100562 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100563 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800564 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
565 int16_t __iomem *index_of = host->pmecc_index_of;
566 int16_t *partial_syn = host->pmecc_partial_syn;
567 const int cap = host->pmecc_corr_cap;
568 int16_t *si;
569 int i, j;
570
571 /* si[] is a table that holds the current syndrome value,
572 * an element of that table belongs to the field
573 */
574 si = host->pmecc_si;
575
576 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
577
578 /* Computation 2t syndromes based on S(x) */
579 /* Odd syndromes */
580 for (i = 1; i < 2 * cap; i += 2) {
581 for (j = 0; j < host->pmecc_degree; j++) {
582 if (partial_syn[i] & ((unsigned short)0x1 << j))
583 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
584 }
585 }
586 /* Even syndrome = (Odd syndrome) ** 2 */
587 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
588 if (si[j] == 0) {
589 si[i] = 0;
590 } else {
591 int16_t tmp;
592
593 tmp = readw_relaxed(index_of + si[j]);
594 tmp = (tmp * 2) % host->pmecc_cw_len;
595 si[i] = readw_relaxed(alpha_to + tmp);
596 }
597 }
598
599 return;
600}
601
602static void pmecc_get_sigma(struct mtd_info *mtd)
603{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100604 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100605 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800606
607 int16_t *lmu = host->pmecc_lmu;
608 int16_t *si = host->pmecc_si;
609 int *mu = host->pmecc_mu;
610 int *dmu = host->pmecc_dmu; /* Discrepancy */
611 int *delta = host->pmecc_delta; /* Delta order */
612 int cw_len = host->pmecc_cw_len;
613 const int16_t cap = host->pmecc_corr_cap;
614 const int num = 2 * cap + 1;
615 int16_t __iomem *index_of = host->pmecc_index_of;
616 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
617 int i, j, k;
618 uint32_t dmu_0_count, tmp;
619 int16_t *smu = host->pmecc_smu;
620
621 /* index of largest delta */
622 int ro;
623 int largest;
624 int diff;
625
626 dmu_0_count = 0;
627
628 /* First Row */
629
630 /* Mu */
631 mu[0] = -1;
632
633 memset(smu, 0, sizeof(int16_t) * num);
634 smu[0] = 1;
635
636 /* discrepancy set to 1 */
637 dmu[0] = 1;
638 /* polynom order set to 0 */
639 lmu[0] = 0;
640 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
641
642 /* Second Row */
643
644 /* Mu */
645 mu[1] = 0;
646 /* Sigma(x) set to 1 */
647 memset(&smu[num], 0, sizeof(int16_t) * num);
648 smu[num] = 1;
649
650 /* discrepancy set to S1 */
651 dmu[1] = si[1];
652
653 /* polynom order set to 0 */
654 lmu[1] = 0;
655
656 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
657
658 /* Init the Sigma(x) last row */
659 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
660
661 for (i = 1; i <= cap; i++) {
662 mu[i + 1] = i << 1;
663 /* Begin Computing Sigma (Mu+1) and L(mu) */
664 /* check if discrepancy is set to 0 */
665 if (dmu[i] == 0) {
666 dmu_0_count++;
667
668 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
669 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
670 tmp += 2;
671 else
672 tmp += 1;
673
674 if (dmu_0_count == tmp) {
675 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
676 smu[(cap + 1) * num + j] =
677 smu[i * num + j];
678
679 lmu[cap + 1] = lmu[i];
680 return;
681 }
682
683 /* copy polynom */
684 for (j = 0; j <= lmu[i] >> 1; j++)
685 smu[(i + 1) * num + j] = smu[i * num + j];
686
687 /* copy previous polynom order to the next */
688 lmu[i + 1] = lmu[i];
689 } else {
690 ro = 0;
691 largest = -1;
692 /* find largest delta with dmu != 0 */
693 for (j = 0; j < i; j++) {
694 if ((dmu[j]) && (delta[j] > largest)) {
695 largest = delta[j];
696 ro = j;
697 }
698 }
699
700 /* compute difference */
701 diff = (mu[i] - mu[ro]);
702
703 /* Compute degree of the new smu polynomial */
704 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
705 lmu[i + 1] = lmu[i];
706 else
707 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
708
709 /* Init smu[i+1] with 0 */
710 for (k = 0; k < num; k++)
711 smu[(i + 1) * num + k] = 0;
712
713 /* Compute smu[i+1] */
714 for (k = 0; k <= lmu[ro] >> 1; k++) {
715 int16_t a, b, c;
716
717 if (!(smu[ro * num + k] && dmu[i]))
718 continue;
719 a = readw_relaxed(index_of + dmu[i]);
720 b = readw_relaxed(index_of + dmu[ro]);
721 c = readw_relaxed(index_of + smu[ro * num + k]);
722 tmp = a + (cw_len - b) + c;
723 a = readw_relaxed(alpha_to + tmp % cw_len);
724 smu[(i + 1) * num + (k + diff)] = a;
725 }
726
727 for (k = 0; k <= lmu[i] >> 1; k++)
728 smu[(i + 1) * num + k] ^= smu[i * num + k];
729 }
730
731 /* End Computing Sigma (Mu+1) and L(mu) */
732 /* In either case compute delta */
733 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
734
735 /* Do not compute discrepancy for the last iteration */
736 if (i >= cap)
737 continue;
738
739 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
740 tmp = 2 * (i - 1);
741 if (k == 0) {
742 dmu[i + 1] = si[tmp + 3];
743 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
744 int16_t a, b, c;
745 a = readw_relaxed(index_of +
746 smu[(i + 1) * num + k]);
747 b = si[2 * (i - 1) + 3 - k];
748 c = readw_relaxed(index_of + b);
749 tmp = a + c;
750 tmp %= cw_len;
751 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
752 dmu[i + 1];
753 }
754 }
755 }
756
757 return;
758}
759
760static int pmecc_err_location(struct mtd_info *mtd)
761{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100762 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100763 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800764 unsigned long end_time;
765 const int cap = host->pmecc_corr_cap;
766 const int num = 2 * cap + 1;
767 int sector_size = host->pmecc_sector_size;
768 int err_nbr = 0; /* number of error */
769 int roots_nbr; /* number of roots */
770 int i;
771 uint32_t val;
772 int16_t *smu = host->pmecc_smu;
773
774 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
775
776 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
777 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
778 smu[(cap + 1) * num + i]);
779 err_nbr++;
780 }
781
782 val = (err_nbr - 1) << 16;
783 if (sector_size == 1024)
784 val |= 1;
785
786 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
787 pmerrloc_writel(host->pmerrloc_base, ELEN,
788 sector_size * 8 + host->pmecc_degree * cap);
789
790 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
791 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
792 & PMERRLOC_CALC_DONE)) {
793 if (unlikely(time_after(jiffies, end_time))) {
794 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
795 return -1;
796 }
797 cpu_relax();
798 }
799
800 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
801 & PMERRLOC_ERR_NUM_MASK) >> 8;
802 /* Number of roots == degree of smu hence <= cap */
803 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
804 return err_nbr - 1;
805
806 /* Number of roots does not match the degree of smu
807 * unable to correct error */
808 return -1;
809}
810
811static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
812 int sector_num, int extra_bytes, int err_nbr)
813{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100814 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100815 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800816 int i = 0;
817 int byte_pos, bit_pos, sector_size, pos;
818 uint32_t tmp;
819 uint8_t err_byte;
820
821 sector_size = host->pmecc_sector_size;
822
823 while (err_nbr) {
Romain Izard55750752016-02-10 10:56:25 +0100824 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_el_base, i) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +0800825 byte_pos = tmp / 8;
826 bit_pos = tmp % 8;
827
828 if (byte_pos >= (sector_size + extra_bytes))
829 BUG(); /* should never happen */
830
831 if (byte_pos < sector_size) {
832 err_byte = *(buf + byte_pos);
833 *(buf + byte_pos) ^= (1 << bit_pos);
834
835 pos = sector_num * host->pmecc_sector_size + byte_pos;
Romain Izard12197bf2016-01-13 17:34:13 +0100836 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 +0800837 pos, bit_pos, err_byte, *(buf + byte_pos));
838 } else {
839 /* Bit flip in OOB area */
Wu, Josh022a4782014-08-08 17:12:35 +0800840 tmp = sector_num * nand_chip->ecc.bytes
Josh Wu1c7b8742012-06-29 17:47:55 +0800841 + (byte_pos - sector_size);
842 err_byte = ecc[tmp];
843 ecc[tmp] ^= (1 << bit_pos);
844
845 pos = tmp + nand_chip->ecc.layout->eccpos[0];
Romain Izard12197bf2016-01-13 17:34:13 +0100846 dev_dbg(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
Josh Wu1c7b8742012-06-29 17:47:55 +0800847 pos, bit_pos, err_byte, ecc[tmp]);
848 }
849
850 i++;
851 err_nbr--;
852 }
853
854 return;
855}
856
857static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
858 u8 *ecc)
859{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100860 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100861 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Bo Shenb3857662014-06-12 15:58:45 +0800862 int i, err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800863 uint8_t *buf_pos;
Wu, Josh267d46e2015-01-14 11:50:46 +0800864 int max_bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800865
Wu, Joshc9447ff2014-08-08 17:12:34 +0800866 for (i = 0; i < nand_chip->ecc.steps; i++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800867 err_nbr = 0;
868 if (pmecc_stat & 0x1) {
869 buf_pos = buf + i * host->pmecc_sector_size;
870
871 pmecc_gen_syndrome(mtd, i);
872 pmecc_substitute(mtd);
873 pmecc_get_sigma(mtd);
874
875 err_nbr = pmecc_err_location(mtd);
Boris Brezillonff6ee102016-03-01 14:11:52 +0100876 if (err_nbr >= 0) {
877 pmecc_correct_data(mtd, buf_pos, ecc, i,
878 nand_chip->ecc.bytes,
879 err_nbr);
880 } else if (!host->caps->pmecc_correct_erase_page) {
881 u8 *ecc_pos = ecc + (i * nand_chip->ecc.bytes);
882
883 /* Try to detect erased pages */
884 err_nbr = nand_check_erased_ecc_chunk(buf_pos,
885 host->pmecc_sector_size,
886 ecc_pos,
887 nand_chip->ecc.bytes,
888 NULL, 0,
889 nand_chip->ecc.strength);
890 }
891
892 if (err_nbr < 0) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800893 dev_err(host->dev, "PMECC: Too many errors\n");
894 mtd->ecc_stats.failed++;
895 return -EIO;
Josh Wu1c7b8742012-06-29 17:47:55 +0800896 }
Boris Brezillonff6ee102016-03-01 14:11:52 +0100897
898 mtd->ecc_stats.corrected += err_nbr;
899 max_bitflips = max_t(int, max_bitflips, err_nbr);
Josh Wu1c7b8742012-06-29 17:47:55 +0800900 }
901 pmecc_stat >>= 1;
902 }
903
Wu, Josh267d46e2015-01-14 11:50:46 +0800904 return max_bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800905}
906
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800907static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
908{
909 u32 val;
910
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800911 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
912 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
913 return;
914 }
915
Josh Wu1fad0e82013-08-07 17:58:11 +0800916 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
917 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
918 val = pmecc_readl_relaxed(host->ecc, CFG);
919
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800920 if (ecc_op == NAND_ECC_READ)
921 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
922 | PMECC_CFG_AUTO_ENABLE);
923 else
924 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
925 & ~PMECC_CFG_AUTO_ENABLE);
926
927 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
928 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
929}
930
Josh Wu1c7b8742012-06-29 17:47:55 +0800931static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
932 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
933{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100934 struct atmel_nand_host *host = nand_get_controller_data(chip);
Bo Shenb3857662014-06-12 15:58:45 +0800935 int eccsize = chip->ecc.size * chip->ecc.steps;
Josh Wu1c7b8742012-06-29 17:47:55 +0800936 uint8_t *oob = chip->oob_poi;
937 uint32_t *eccpos = chip->ecc.layout->eccpos;
938 uint32_t stat;
939 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800940 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800941
Josh Wu1ae9c092013-08-05 19:14:36 +0800942 if (!host->nfc || !host->nfc->use_nfc_sram)
943 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800944
945 chip->read_buf(mtd, buf, eccsize);
946 chip->read_buf(mtd, oob, mtd->oobsize);
947
948 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
949 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
950 if (unlikely(time_after(jiffies, end_time))) {
951 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
952 return -EIO;
953 }
954 cpu_relax();
955 }
956
957 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800958 if (stat != 0) {
959 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
960 if (bitflips < 0)
961 /* uncorrectable errors */
962 return 0;
963 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800964
Josh Wuc0c70d92012-11-27 18:50:31 +0800965 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800966}
967
968static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +0200969 struct nand_chip *chip, const uint8_t *buf, int oob_required,
970 int page)
Josh Wu1c7b8742012-06-29 17:47:55 +0800971{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100972 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu1c7b8742012-06-29 17:47:55 +0800973 uint32_t *eccpos = chip->ecc.layout->eccpos;
974 int i, j;
975 unsigned long end_time;
976
Josh Wu6054d4d2013-08-05 19:14:37 +0800977 if (!host->nfc || !host->nfc->write_by_sram) {
978 pmecc_enable(host, NAND_ECC_WRITE);
979 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
980 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800981
982 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
983 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
984 if (unlikely(time_after(jiffies, end_time))) {
985 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
986 return -EIO;
987 }
988 cpu_relax();
989 }
990
Wu, Joshc9447ff2014-08-08 17:12:34 +0800991 for (i = 0; i < chip->ecc.steps; i++) {
Wu, Josh022a4782014-08-08 17:12:35 +0800992 for (j = 0; j < chip->ecc.bytes; j++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800993 int pos;
994
Wu, Josh022a4782014-08-08 17:12:35 +0800995 pos = i * chip->ecc.bytes + j;
Josh Wu1c7b8742012-06-29 17:47:55 +0800996 chip->oob_poi[eccpos[pos]] =
997 pmecc_readb_ecc_relaxed(host->ecc, i, j);
998 }
999 }
1000 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1001
1002 return 0;
1003}
1004
1005static void atmel_pmecc_core_init(struct mtd_info *mtd)
1006{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001007 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001008 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +08001009 uint32_t val = 0;
1010 struct nand_ecclayout *ecc_layout;
1011
1012 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
1013 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1014
1015 switch (host->pmecc_corr_cap) {
1016 case 2:
1017 val = PMECC_CFG_BCH_ERR2;
1018 break;
1019 case 4:
1020 val = PMECC_CFG_BCH_ERR4;
1021 break;
1022 case 8:
1023 val = PMECC_CFG_BCH_ERR8;
1024 break;
1025 case 12:
1026 val = PMECC_CFG_BCH_ERR12;
1027 break;
1028 case 24:
1029 val = PMECC_CFG_BCH_ERR24;
1030 break;
Romain Izard94248462016-02-10 10:56:26 +01001031 case 32:
1032 val = PMECC_CFG_BCH_ERR32;
1033 break;
Josh Wu1c7b8742012-06-29 17:47:55 +08001034 }
1035
1036 if (host->pmecc_sector_size == 512)
1037 val |= PMECC_CFG_SECTOR512;
1038 else if (host->pmecc_sector_size == 1024)
1039 val |= PMECC_CFG_SECTOR1024;
1040
Wu, Joshc9447ff2014-08-08 17:12:34 +08001041 switch (nand_chip->ecc.steps) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001042 case 1:
1043 val |= PMECC_CFG_PAGE_1SECTOR;
1044 break;
1045 case 2:
1046 val |= PMECC_CFG_PAGE_2SECTORS;
1047 break;
1048 case 4:
1049 val |= PMECC_CFG_PAGE_4SECTORS;
1050 break;
1051 case 8:
1052 val |= PMECC_CFG_PAGE_8SECTORS;
1053 break;
1054 }
1055
1056 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
1057 | PMECC_CFG_AUTO_DISABLE);
1058 pmecc_writel(host->ecc, CFG, val);
1059
1060 ecc_layout = nand_chip->ecc.layout;
1061 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
1062 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
1063 pmecc_writel(host->ecc, EADDR,
1064 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
1065 /* See datasheet about PMECC Clock Control Register */
1066 pmecc_writel(host->ecc, CLK, 2);
1067 pmecc_writel(host->ecc, IDR, 0xff);
1068 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
1069}
1070
Josh Wu84cfbbb2013-01-23 20:47:12 +08001071/*
Josh Wu2a3d9332013-09-18 13:58:48 +08001072 * Get minimum ecc requirements from NAND.
Josh Wu84cfbbb2013-01-23 20:47:12 +08001073 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
Josh Wu2a3d9332013-09-18 13:58:48 +08001074 * will set them according to minimum ecc requirement. Otherwise, use the
Josh Wu84cfbbb2013-01-23 20:47:12 +08001075 * value in DTS file.
1076 * return 0 if success. otherwise return error code.
1077 */
1078static int pmecc_choose_ecc(struct atmel_nand_host *host,
1079 int *cap, int *sector_size)
1080{
Josh Wu2a3d9332013-09-18 13:58:48 +08001081 /* Get minimum ECC requirements */
1082 if (host->nand_chip.ecc_strength_ds) {
1083 *cap = host->nand_chip.ecc_strength_ds;
1084 *sector_size = host->nand_chip.ecc_step_ds;
1085 dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n",
Josh Wu84cfbbb2013-01-23 20:47:12 +08001086 *cap, *sector_size);
Josh Wu84cfbbb2013-01-23 20:47:12 +08001087 } else {
Josh Wu84cfbbb2013-01-23 20:47:12 +08001088 *cap = 2;
1089 *sector_size = 512;
Josh Wu2a3d9332013-09-18 13:58:48 +08001090 dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
Josh Wu84cfbbb2013-01-23 20:47:12 +08001091 }
1092
Josh Wu2a3d9332013-09-18 13:58:48 +08001093 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
Josh Wu84cfbbb2013-01-23 20:47:12 +08001094 if (host->pmecc_corr_cap == 0) {
Romain Izard94248462016-02-10 10:56:26 +01001095 if (*cap > host->caps->pmecc_max_correction)
1096 return -EINVAL;
1097
Josh Wu84cfbbb2013-01-23 20:47:12 +08001098 /* use the most fitable ecc bits (the near bigger one ) */
1099 if (*cap <= 2)
1100 host->pmecc_corr_cap = 2;
1101 else if (*cap <= 4)
1102 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +08001103 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001104 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +08001105 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001106 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +08001107 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001108 host->pmecc_corr_cap = 24;
Romain Izard94248462016-02-10 10:56:26 +01001109 else if (*cap <= 32)
1110 host->pmecc_corr_cap = 32;
Josh Wu84cfbbb2013-01-23 20:47:12 +08001111 else
1112 return -EINVAL;
1113 }
1114 if (host->pmecc_sector_size == 0) {
1115 /* use the most fitable sector size (the near smaller one ) */
1116 if (*sector_size >= 1024)
1117 host->pmecc_sector_size = 1024;
1118 else if (*sector_size >= 512)
1119 host->pmecc_sector_size = 512;
1120 else
1121 return -EINVAL;
1122 }
1123 return 0;
1124}
1125
Josh Wuabb1cd02014-10-11 18:01:50 +08001126static inline int deg(unsigned int poly)
1127{
1128 /* polynomial degree is the most-significant bit index */
1129 return fls(poly) - 1;
1130}
1131
1132static int build_gf_tables(int mm, unsigned int poly,
1133 int16_t *index_of, int16_t *alpha_to)
1134{
1135 unsigned int i, x = 1;
1136 const unsigned int k = 1 << deg(poly);
1137 unsigned int nn = (1 << mm) - 1;
1138
1139 /* primitive polynomial must be of degree m */
1140 if (k != (1u << mm))
1141 return -EINVAL;
1142
1143 for (i = 0; i < nn; i++) {
1144 alpha_to[i] = x;
1145 index_of[x] = i;
1146 if (i && (x == 1))
1147 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
1148 return -EINVAL;
1149 x <<= 1;
1150 if (x & k)
1151 x ^= poly;
1152 }
1153 alpha_to[nn] = 1;
1154 index_of[0] = 0;
1155
1156 return 0;
1157}
1158
1159static uint16_t *create_lookup_table(struct device *dev, int sector_size)
1160{
1161 int degree = (sector_size == 512) ?
1162 PMECC_GF_DIMENSION_13 :
1163 PMECC_GF_DIMENSION_14;
1164 unsigned int poly = (sector_size == 512) ?
1165 PMECC_GF_13_PRIMITIVE_POLY :
1166 PMECC_GF_14_PRIMITIVE_POLY;
1167 int table_size = (sector_size == 512) ?
1168 PMECC_LOOKUP_TABLE_SIZE_512 :
1169 PMECC_LOOKUP_TABLE_SIZE_1024;
1170
1171 int16_t *addr = devm_kzalloc(dev, 2 * table_size * sizeof(uint16_t),
1172 GFP_KERNEL);
1173 if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
1174 return NULL;
1175
1176 return addr;
1177}
1178
Johan Hovold2c2b9282013-09-23 16:27:28 +02001179static int atmel_pmecc_nand_init_params(struct platform_device *pdev,
Josh Wu1c7b8742012-06-29 17:47:55 +08001180 struct atmel_nand_host *host)
1181{
Josh Wu1c7b8742012-06-29 17:47:55 +08001182 struct nand_chip *nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01001183 struct mtd_info *mtd = nand_to_mtd(nand_chip);
Josh Wu1c7b8742012-06-29 17:47:55 +08001184 struct resource *regs, *regs_pmerr, *regs_rom;
Josh Wuabb1cd02014-10-11 18:01:50 +08001185 uint16_t *galois_table;
Josh Wu1c7b8742012-06-29 17:47:55 +08001186 int cap, sector_size, err_no;
1187
Josh Wu84cfbbb2013-01-23 20:47:12 +08001188 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
1189 if (err_no) {
1190 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1191 return err_no;
1192 }
1193
Richard Genoudf666d642013-07-30 17:17:29 +02001194 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001195 sector_size != host->pmecc_sector_size)
1196 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001197
Josh Wu1c7b8742012-06-29 17:47:55 +08001198 cap = host->pmecc_corr_cap;
1199 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001200 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1201 host->pmecc_lookup_table_offset_512 :
1202 host->pmecc_lookup_table_offset_1024;
1203
Josh Wu1c7b8742012-06-29 17:47:55 +08001204 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1205 cap, sector_size);
1206
1207 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1208 if (!regs) {
1209 dev_warn(host->dev,
1210 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1211 nand_chip->ecc.mode = NAND_ECC_SOFT;
1212 return 0;
1213 }
1214
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001215 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1216 if (IS_ERR(host->ecc)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001217 err_no = PTR_ERR(host->ecc);
1218 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001219 }
1220
1221 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001222 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1223 if (IS_ERR(host->pmerrloc_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001224 err_no = PTR_ERR(host->pmerrloc_base);
1225 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001226 }
Romain Izard55750752016-02-10 10:56:25 +01001227 host->pmerrloc_el_base = host->pmerrloc_base + ATMEL_PMERRLOC_SIGMAx +
1228 (host->caps->pmecc_max_correction + 1) * 4;
Josh Wu1c7b8742012-06-29 17:47:55 +08001229
Wu, Josh41c75402015-04-02 14:13:47 +08001230 if (!host->has_no_lookup_table) {
1231 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1232 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev,
1233 regs_rom);
1234 if (IS_ERR(host->pmecc_rom_base)) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001235 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 +08001236 host->has_no_lookup_table = true;
1237 }
Josh Wuabb1cd02014-10-11 18:01:50 +08001238 }
1239
1240 if (host->has_no_lookup_table) {
1241 /* Build the look-up table in runtime */
1242 galois_table = create_lookup_table(host->dev, sector_size);
1243 if (!galois_table) {
1244 dev_err(host->dev, "Failed to build a lookup table in runtime!\n");
1245 err_no = -EINVAL;
1246 goto err;
1247 }
1248
1249 host->pmecc_rom_base = (void __iomem *)galois_table;
1250 host->pmecc_lookup_table_offset = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +08001251 }
1252
Bo Shenb3857662014-06-12 15:58:45 +08001253 nand_chip->ecc.size = sector_size;
Josh Wu1c7b8742012-06-29 17:47:55 +08001254
1255 /* set ECC page size and oob layout */
1256 switch (mtd->writesize) {
Wu, Josha3557102014-07-22 17:24:18 +08001257 case 512:
1258 case 1024:
Josh Wu1c7b8742012-06-29 17:47:55 +08001259 case 2048:
Wu, Josha3557102014-07-22 17:24:18 +08001260 case 4096:
1261 case 8192:
1262 if (sector_size > mtd->writesize) {
1263 dev_err(host->dev, "pmecc sector size is bigger than the page size!\n");
1264 err_no = -EINVAL;
1265 goto err;
1266 }
1267
Josh Wu2fa831f2013-08-19 18:05:44 +08001268 host->pmecc_degree = (sector_size == 512) ?
1269 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Josh Wu1c7b8742012-06-29 17:47:55 +08001270 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +08001271 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1272 host->pmecc_index_of = host->pmecc_rom_base +
1273 host->pmecc_lookup_table_offset;
1274
Josh Wu1c7b8742012-06-29 17:47:55 +08001275 nand_chip->ecc.strength = cap;
Wu, Josh022a4782014-08-08 17:12:35 +08001276 nand_chip->ecc.bytes = pmecc_get_ecc_bytes(cap, sector_size);
Wu, Joshc9447ff2014-08-08 17:12:34 +08001277 nand_chip->ecc.steps = mtd->writesize / sector_size;
1278 nand_chip->ecc.total = nand_chip->ecc.bytes *
1279 nand_chip->ecc.steps;
Josh Wu477478a2015-04-02 14:12:33 +08001280 if (nand_chip->ecc.total >
1281 mtd->oobsize - PMECC_OOB_RESERVED_BYTES) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001282 dev_err(host->dev, "No room for ECC bytes\n");
1283 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001284 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001285 }
1286 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1287 mtd->oobsize,
Bo Shenb3857662014-06-12 15:58:45 +08001288 nand_chip->ecc.total);
1289
Josh Wu1c7b8742012-06-29 17:47:55 +08001290 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1291 break;
Wu, Josha3557102014-07-22 17:24:18 +08001292 default:
Josh Wu1c7b8742012-06-29 17:47:55 +08001293 dev_warn(host->dev,
1294 "Unsupported page size for PMECC, use Software ECC\n");
Josh Wu1c7b8742012-06-29 17:47:55 +08001295 /* page size not handled by HW ECC */
1296 /* switching back to soft ECC */
1297 nand_chip->ecc.mode = NAND_ECC_SOFT;
1298 return 0;
1299 }
1300
1301 /* Allocate data for PMECC computation */
1302 err_no = pmecc_data_alloc(host);
1303 if (err_no) {
1304 dev_err(host->dev,
1305 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001306 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001307 }
1308
Herve Codina90445ff2014-03-03 12:15:29 +01001309 nand_chip->options |= NAND_NO_SUBPAGE_WRITE;
Josh Wu1c7b8742012-06-29 17:47:55 +08001310 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1311 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1312
1313 atmel_pmecc_core_init(mtd);
1314
1315 return 0;
1316
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001317err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001318 return err_no;
1319}
1320
1321/*
Richard Genoud77f54922008-04-23 19:51:14 +02001322 * Calculate HW ECC
1323 *
1324 * function called after a write
1325 *
1326 * mtd: MTD block structure
1327 * dat: raw data (unused)
1328 * ecc_code: buffer for ECC
1329 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001330static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001331 const u_char *dat, unsigned char *ecc_code)
1332{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001333 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001334 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02001335 unsigned int ecc_value;
1336
1337 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001338 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001339
Richard Genoud3fc23892008-10-12 08:42:28 +02001340 ecc_code[0] = ecc_value & 0xFF;
1341 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001342
1343 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001344 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001345
Richard Genoud3fc23892008-10-12 08:42:28 +02001346 ecc_code[2] = ecc_value & 0xFF;
1347 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001348
1349 return 0;
1350}
1351
1352/*
1353 * HW ECC read page function
1354 *
1355 * mtd: mtd info structure
1356 * chip: nand chip info structure
1357 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001358 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001359 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001360static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1361 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001362{
1363 int eccsize = chip->ecc.size;
1364 int eccbytes = chip->ecc.bytes;
1365 uint32_t *eccpos = chip->ecc.layout->eccpos;
1366 uint8_t *p = buf;
1367 uint8_t *oob = chip->oob_poi;
1368 uint8_t *ecc_pos;
1369 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001370 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001371
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001372 /*
1373 * Errata: ALE is incorrectly wired up to the ECC controller
1374 * on the AP7000, so it will include the address cycles in the
1375 * ECC calculation.
1376 *
1377 * Workaround: Reset the parity registers before reading the
1378 * actual data.
1379 */
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001380 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu71b94e22013-05-09 15:34:54 +08001381 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001382 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001383
Richard Genoud77f54922008-04-23 19:51:14 +02001384 /* read the page */
1385 chip->read_buf(mtd, p, eccsize);
1386
1387 /* move to ECC position if needed */
1388 if (eccpos[0] != 0) {
1389 /* This only works on large pages
1390 * because the ECC controller waits for
1391 * NAND_CMD_RNDOUTSTART after the
1392 * NAND_CMD_RNDOUT.
1393 * anyway, for small pages, the eccpos[0] == 0
1394 */
1395 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1396 mtd->writesize + eccpos[0], -1);
1397 }
1398
1399 /* the ECC controller needs to read the ECC just after the data */
1400 ecc_pos = oob + eccpos[0];
1401 chip->read_buf(mtd, ecc_pos, eccbytes);
1402
1403 /* check if there's an error */
1404 stat = chip->ecc.correct(mtd, p, oob, NULL);
1405
Mike Dunn3f91e942012-04-25 12:06:09 -07001406 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001407 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001408 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001409 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001410 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1411 }
Richard Genoud77f54922008-04-23 19:51:14 +02001412
1413 /* get back to oob start (end of page) */
1414 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1415
1416 /* read the oob */
1417 chip->read_buf(mtd, oob, mtd->oobsize);
1418
Mike Dunn3f91e942012-04-25 12:06:09 -07001419 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001420}
1421
1422/*
1423 * HW ECC Correction
1424 *
1425 * function called after a read
1426 *
1427 * mtd: MTD block structure
1428 * dat: raw data read from the chip
1429 * read_ecc: ECC from the chip (unused)
1430 * isnull: unused
1431 *
1432 * Detect and correct a 1 bit error for a page
1433 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001434static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001435 u_char *read_ecc, u_char *isnull)
1436{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001437 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001438 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02001439 unsigned int ecc_status;
1440 unsigned int ecc_word, ecc_bit;
1441
1442 /* get the status from the Status Register */
1443 ecc_status = ecc_readl(host->ecc, SR);
1444
1445 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001446 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001447 return 0;
1448
1449 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001450 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001451 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001452 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001453 ecc_word >>= 4;
1454
1455 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001456 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001457 /* check if it is a freshly erased block
1458 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001459 if ((ecc_bit == ATMEL_ECC_BITADDR)
1460 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001461 /* the block has just been erased, return OK */
1462 return 0;
1463 }
1464 /* it doesn't seems to be a freshly
1465 * erased block.
1466 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001467 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001468 " Unable to correct.\n");
Boris BREZILLON6e941192015-12-30 20:32:03 +01001469 return -EBADMSG;
Richard Genoud77f54922008-04-23 19:51:14 +02001470 }
1471
1472 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001473 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001474 /* there's nothing much to do here.
1475 * the bit error is on the ECC itself.
1476 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001477 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001478 " Nothing to correct\n");
1479 return 0;
1480 }
1481
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001482 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001483 " (word offset in the page :"
1484 " 0x%x bit offset : 0x%x)\n",
1485 ecc_word, ecc_bit);
1486 /* correct the error */
1487 if (nand_chip->options & NAND_BUSWIDTH_16) {
1488 /* 16 bits words */
1489 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1490 } else {
1491 /* 8 bits words */
1492 dat[ecc_word] ^= (1 << ecc_bit);
1493 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001494 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001495 return 1;
1496}
1497
1498/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001499 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001500 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001501static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1502{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001503 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001504 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu71b94e22013-05-09 15:34:54 +08001505
1506 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001507 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001508}
Richard Genoud77f54922008-04-23 19:51:14 +02001509
Bill Pemberton06f25512012-11-19 13:23:07 -05001510static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001511 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001512{
Josh Wuc0cf7872013-01-23 20:47:08 +08001513 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001514 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001515 int ecc_mode;
1516 struct atmel_nand_data *board = &host->board;
Josh Wue9d8da82013-09-18 11:31:19 +08001517 enum of_gpio_flags flags = 0;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001518
Wu, Josh51585772015-01-19 16:33:06 +08001519 host->caps = (struct atmel_nand_caps *)
Romain Izard8ce06d32016-01-15 11:34:57 +01001520 of_device_get_match_data(host->dev);
Wu, Josh51585772015-01-19 16:33:06 +08001521
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001522 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1523 if (val >= 32) {
1524 dev_err(host->dev, "invalid addr-offset %u\n", val);
1525 return -EINVAL;
1526 }
1527 board->ale = val;
1528 }
1529
1530 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1531 if (val >= 32) {
1532 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1533 return -EINVAL;
1534 }
1535 board->cle = val;
1536 }
1537
1538 ecc_mode = of_get_nand_ecc_mode(np);
1539
1540 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1541
1542 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1543
Josh Wu1b719262013-05-09 15:34:55 +08001544 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1545
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001546 if (of_get_nand_bus_width(np) == 16)
1547 board->bus_width_16 = 1;
1548
1549 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1550 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1551
1552 board->enable_pin = of_get_gpio(np, 1);
1553 board->det_pin = of_get_gpio(np, 2);
1554
Josh Wua41b51a2012-06-29 17:47:54 +08001555 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1556
Josh Wu7dc37de2013-08-05 19:14:35 +08001557 /* load the nfc driver if there is */
1558 of_platform_populate(np, NULL, NULL, host->dev);
1559
Josh Wua41b51a2012-06-29 17:47:54 +08001560 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1561 return 0; /* Not using PMECC */
1562
1563 /* use PMECC, get correction capability, sector size and lookup
1564 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001565 * If correction bits and sector size are not specified, then find
1566 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001567 */
Josh Wue66b4312013-01-23 20:47:11 +08001568 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
Romain Izard94248462016-02-10 10:56:26 +01001569 if (val > host->caps->pmecc_max_correction) {
1570 dev_err(host->dev,
1571 "Required ECC strength too high: %u max %u\n",
1572 val, host->caps->pmecc_max_correction);
1573 return -EINVAL;
1574 }
1575 if ((val != 2) && (val != 4) && (val != 8) &&
1576 (val != 12) && (val != 24) && (val != 32)) {
Josh Wue66b4312013-01-23 20:47:11 +08001577 dev_err(host->dev,
Romain Izarde88b7f72016-01-15 11:34:56 +01001578 "Required ECC strength not supported: %u\n",
Josh Wue66b4312013-01-23 20:47:11 +08001579 val);
1580 return -EINVAL;
1581 }
1582 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001583 }
Josh Wua41b51a2012-06-29 17:47:54 +08001584
Josh Wue66b4312013-01-23 20:47:11 +08001585 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1586 if ((val != 512) && (val != 1024)) {
1587 dev_err(host->dev,
Romain Izarde88b7f72016-01-15 11:34:56 +01001588 "Required ECC sector size not supported: %u\n",
Josh Wue66b4312013-01-23 20:47:11 +08001589 val);
1590 return -EINVAL;
1591 }
1592 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001593 }
Josh Wua41b51a2012-06-29 17:47:54 +08001594
1595 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1596 offset, 2) != 0) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001597 dev_err(host->dev, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n");
1598 host->has_no_lookup_table = true;
1599 /* Will build a lookup table and initialize the offset later */
1600 return 0;
Josh Wua41b51a2012-06-29 17:47:54 +08001601 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001602 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001603 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1604 return -EINVAL;
1605 }
Josh Wue66b4312013-01-23 20:47:11 +08001606 host->pmecc_lookup_table_offset_512 = offset[0];
1607 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001608
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001609 return 0;
1610}
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001611
Johan Hovold2c2b9282013-09-23 16:27:28 +02001612static int atmel_hw_nand_init_params(struct platform_device *pdev,
Josh Wu3dfe41a2012-06-25 18:07:43 +08001613 struct atmel_nand_host *host)
1614{
Josh Wu3dfe41a2012-06-25 18:07:43 +08001615 struct nand_chip *nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01001616 struct mtd_info *mtd = nand_to_mtd(nand_chip);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001617 struct resource *regs;
1618
1619 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1620 if (!regs) {
1621 dev_err(host->dev,
1622 "Can't get I/O resource regs, use software ECC\n");
1623 nand_chip->ecc.mode = NAND_ECC_SOFT;
1624 return 0;
1625 }
1626
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001627 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun8fb7b932014-07-28 21:19:55 +08001628 if (IS_ERR(host->ecc))
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001629 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001630
1631 /* ECC is calculated for the whole page (1 step) */
1632 nand_chip->ecc.size = mtd->writesize;
1633
1634 /* set ECC page size and oob layout */
1635 switch (mtd->writesize) {
1636 case 512:
1637 nand_chip->ecc.layout = &atmel_oobinfo_small;
1638 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1639 break;
1640 case 1024:
1641 nand_chip->ecc.layout = &atmel_oobinfo_large;
1642 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1643 break;
1644 case 2048:
1645 nand_chip->ecc.layout = &atmel_oobinfo_large;
1646 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1647 break;
1648 case 4096:
1649 nand_chip->ecc.layout = &atmel_oobinfo_large;
1650 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1651 break;
1652 default:
1653 /* page size not handled by HW ECC */
1654 /* switching back to soft ECC */
1655 nand_chip->ecc.mode = NAND_ECC_SOFT;
1656 return 0;
1657 }
1658
1659 /* set up for HW ECC */
1660 nand_chip->ecc.calculate = atmel_nand_calculate;
1661 nand_chip->ecc.correct = atmel_nand_correct;
1662 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1663 nand_chip->ecc.read_page = atmel_nand_read_page;
1664 nand_chip->ecc.bytes = 4;
1665 nand_chip->ecc.strength = 1;
1666
1667 return 0;
1668}
1669
Wu, Josh50e04e22014-06-10 17:50:09 +08001670static inline u32 nfc_read_status(struct atmel_nand_host *host)
1671{
1672 u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
1673 u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
1674
1675 if (unlikely(nfc_status & err_flags)) {
1676 if (nfc_status & NFC_SR_DTOE)
1677 dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
1678 else if (nfc_status & NFC_SR_UNDEF)
1679 dev_err(host->dev, "NFC: Access Undefined Area Error\n");
1680 else if (nfc_status & NFC_SR_AWB)
1681 dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
1682 else if (nfc_status & NFC_SR_ASE)
1683 dev_err(host->dev, "NFC: Access memory Size Error\n");
1684 }
1685
1686 return nfc_status;
1687}
1688
Josh Wu7dc37de2013-08-05 19:14:35 +08001689/* SMC interrupt service routine */
1690static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
1691{
1692 struct atmel_nand_host *host = dev_id;
1693 u32 status, mask, pending;
Josh Wue4e06932014-06-10 17:50:11 +08001694 irqreturn_t ret = IRQ_NONE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001695
Wu, Josh50e04e22014-06-10 17:50:09 +08001696 status = nfc_read_status(host);
Josh Wu7dc37de2013-08-05 19:14:35 +08001697 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1698 pending = status & mask;
1699
1700 if (pending & NFC_SR_XFR_DONE) {
Josh Wue4e06932014-06-10 17:50:11 +08001701 complete(&host->nfc->comp_xfer_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001702 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001703 ret = IRQ_HANDLED;
1704 }
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001705 if (pending & host->nfc->caps->rb_mask) {
Josh Wue4e06932014-06-10 17:50:11 +08001706 complete(&host->nfc->comp_ready);
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001707 nfc_writel(host->nfc->hsmc_regs, IDR, host->nfc->caps->rb_mask);
Josh Wue4e06932014-06-10 17:50:11 +08001708 ret = IRQ_HANDLED;
1709 }
1710 if (pending & NFC_SR_CMD_DONE) {
1711 complete(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001712 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001713 ret = IRQ_HANDLED;
Josh Wu7dc37de2013-08-05 19:14:35 +08001714 }
1715
1716 return ret;
1717}
1718
1719/* NFC(Nand Flash Controller) related functions */
Josh Wue4e06932014-06-10 17:50:11 +08001720static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag)
Josh Wu7dc37de2013-08-05 19:14:35 +08001721{
Josh Wue4e06932014-06-10 17:50:11 +08001722 if (flag & NFC_SR_XFR_DONE)
1723 init_completion(&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 init_completion(&host->nfc->comp_ready);
1727
1728 if (flag & NFC_SR_CMD_DONE)
1729 init_completion(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001730
1731 /* Enable interrupt that need to wait for */
1732 nfc_writel(host->nfc->hsmc_regs, IER, flag);
Josh Wue4e06932014-06-10 17:50:11 +08001733}
Josh Wu7dc37de2013-08-05 19:14:35 +08001734
Josh Wue4e06932014-06-10 17:50:11 +08001735static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
1736{
1737 int i, index = 0;
1738 struct completion *comp[3]; /* Support 3 interrupt completion */
Josh Wu7dc37de2013-08-05 19:14:35 +08001739
Josh Wue4e06932014-06-10 17:50:11 +08001740 if (flag & NFC_SR_XFR_DONE)
1741 comp[index++] = &host->nfc->comp_xfer_done;
1742
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001743 if (flag & host->nfc->caps->rb_mask)
Josh Wue4e06932014-06-10 17:50:11 +08001744 comp[index++] = &host->nfc->comp_ready;
1745
1746 if (flag & NFC_SR_CMD_DONE)
1747 comp[index++] = &host->nfc->comp_cmd_done;
1748
1749 if (index == 0) {
Colin Ian King393d23c2015-02-28 20:27:56 +00001750 dev_err(host->dev, "Unknown interrupt flag: 0x%08x\n", flag);
Josh Wue4e06932014-06-10 17:50:11 +08001751 return -EINVAL;
1752 }
1753
1754 for (i = 0; i < index; i++) {
1755 if (wait_for_completion_timeout(comp[i],
1756 msecs_to_jiffies(NFC_TIME_OUT_MS)))
1757 continue; /* wait for next completion */
1758 else
1759 goto err_timeout;
1760 }
1761
1762 return 0;
1763
1764err_timeout:
Josh Wu7dc37de2013-08-05 19:14:35 +08001765 dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
Josh Wue4e06932014-06-10 17:50:11 +08001766 /* Disable the interrupt as it is not handled by interrupt handler */
1767 nfc_writel(host->nfc->hsmc_regs, IDR, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001768 return -ETIMEDOUT;
1769}
1770
1771static int nfc_send_command(struct atmel_nand_host *host,
1772 unsigned int cmd, unsigned int addr, unsigned char cycle0)
1773{
1774 unsigned long timeout;
Josh Wue4e06932014-06-10 17:50:11 +08001775 u32 flag = NFC_SR_CMD_DONE;
1776 flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0;
1777
Josh Wu7dc37de2013-08-05 19:14:35 +08001778 dev_dbg(host->dev,
1779 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1780 cmd, addr, cycle0);
1781
1782 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
Boris Brezillon111573cc2015-01-22 18:21:30 +08001783 while (nfc_readl(host->nfc->hsmc_regs, SR) & NFC_SR_BUSY) {
Josh Wu7dc37de2013-08-05 19:14:35 +08001784 if (time_after(jiffies, timeout)) {
1785 dev_err(host->dev,
Boris Brezillon111573cc2015-01-22 18:21:30 +08001786 "Time out to wait for NFC ready!\n");
Josh Wu7dc37de2013-08-05 19:14:35 +08001787 return -ETIMEDOUT;
1788 }
1789 }
Josh Wue4e06932014-06-10 17:50:11 +08001790
1791 nfc_prepare_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001792 nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
1793 nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
Josh Wue4e06932014-06-10 17:50:11 +08001794 return nfc_wait_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001795}
1796
1797static int nfc_device_ready(struct mtd_info *mtd)
1798{
Wu, Josh72a78e32014-06-10 17:50:10 +08001799 u32 status, mask;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001800 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001801 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Wu, Josh72a78e32014-06-10 17:50:10 +08001802
1803 status = nfc_read_status(host);
1804 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1805
1806 /* The mask should be 0. If not we may lost interrupts */
1807 if (unlikely(mask & status))
1808 dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n",
1809 mask & status);
1810
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001811 return status & host->nfc->caps->rb_mask;
Josh Wu7dc37de2013-08-05 19:14:35 +08001812}
1813
1814static void nfc_select_chip(struct mtd_info *mtd, int chip)
1815{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001816 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001817 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Josh Wu7dc37de2013-08-05 19:14:35 +08001818
1819 if (chip == -1)
1820 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
1821 else
1822 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
1823}
1824
Brian Norris3dad2342014-01-29 14:08:12 -08001825static int nfc_make_addr(struct mtd_info *mtd, int command, int column,
1826 int page_addr, unsigned int *addr1234, unsigned int *cycle0)
Josh Wu7dc37de2013-08-05 19:14:35 +08001827{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001828 struct nand_chip *chip = mtd_to_nand(mtd);
Josh Wu7dc37de2013-08-05 19:14:35 +08001829
1830 int acycle = 0;
1831 unsigned char addr_bytes[8];
1832 int index = 0, bit_shift;
1833
1834 BUG_ON(addr1234 == NULL || cycle0 == NULL);
1835
1836 *cycle0 = 0;
1837 *addr1234 = 0;
1838
1839 if (column != -1) {
Brian Norris3dad2342014-01-29 14:08:12 -08001840 if (chip->options & NAND_BUSWIDTH_16 &&
1841 !nand_opcode_8bits(command))
Josh Wu7dc37de2013-08-05 19:14:35 +08001842 column >>= 1;
1843 addr_bytes[acycle++] = column & 0xff;
1844 if (mtd->writesize > 512)
1845 addr_bytes[acycle++] = (column >> 8) & 0xff;
1846 }
1847
1848 if (page_addr != -1) {
1849 addr_bytes[acycle++] = page_addr & 0xff;
1850 addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
1851 if (chip->chipsize > (128 << 20))
1852 addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
1853 }
1854
1855 if (acycle > 4)
1856 *cycle0 = addr_bytes[index++];
1857
1858 for (bit_shift = 0; index < acycle; bit_shift += 8)
1859 *addr1234 += addr_bytes[index++] << bit_shift;
1860
1861 /* return acycle in cmd register */
1862 return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
1863}
1864
1865static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
1866 int column, int page_addr)
1867{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001868 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001869 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu7dc37de2013-08-05 19:14:35 +08001870 unsigned long timeout;
1871 unsigned int nfc_addr_cmd = 0;
1872
1873 unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1874
1875 /* Set default settings: no cmd2, no addr cycle. read from nand */
1876 unsigned int cmd2 = 0;
1877 unsigned int vcmd2 = 0;
1878 int acycle = NFCADDR_CMD_ACYCLE_NONE;
1879 int csid = NFCADDR_CMD_CSID_3;
1880 int dataen = NFCADDR_CMD_DATADIS;
1881 int nfcwr = NFCADDR_CMD_NFCRD;
1882 unsigned int addr1234 = 0;
1883 unsigned int cycle0 = 0;
1884 bool do_addr = true;
Josh Wu1ae9c092013-08-05 19:14:36 +08001885 host->nfc->data_in_sram = NULL;
Josh Wu7dc37de2013-08-05 19:14:35 +08001886
1887 dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1888 __func__, command, column, page_addr);
1889
1890 switch (command) {
1891 case NAND_CMD_RESET:
1892 nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
1893 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1894 udelay(chip->chip_delay);
1895
1896 nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
1897 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1898 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
1899 if (time_after(jiffies, timeout)) {
1900 dev_err(host->dev,
1901 "Time out to wait status ready!\n");
1902 break;
1903 }
1904 }
1905 return;
1906 case NAND_CMD_STATUS:
1907 do_addr = false;
1908 break;
1909 case NAND_CMD_PARAM:
1910 case NAND_CMD_READID:
1911 do_addr = false;
1912 acycle = NFCADDR_CMD_ACYCLE_1;
1913 if (column != -1)
1914 addr1234 = column;
1915 break;
1916 case NAND_CMD_RNDOUT:
1917 cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
1918 vcmd2 = NFCADDR_CMD_VCMD2;
1919 break;
1920 case NAND_CMD_READ0:
1921 case NAND_CMD_READOOB:
1922 if (command == NAND_CMD_READOOB) {
1923 column += mtd->writesize;
1924 command = NAND_CMD_READ0; /* only READ0 is valid */
1925 cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1926 }
Josh Wu1ae9c092013-08-05 19:14:36 +08001927 if (host->nfc->use_nfc_sram) {
1928 /* Enable Data transfer to sram */
1929 dataen = NFCADDR_CMD_DATAEN;
1930
1931 /* Need enable PMECC now, since NFC will transfer
1932 * data in bus after sending nfc read command.
1933 */
1934 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1935 pmecc_enable(host, NAND_ECC_READ);
1936 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001937
1938 cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
1939 vcmd2 = NFCADDR_CMD_VCMD2;
1940 break;
1941 /* For prgramming command, the cmd need set to write enable */
1942 case NAND_CMD_PAGEPROG:
1943 case NAND_CMD_SEQIN:
1944 case NAND_CMD_RNDIN:
1945 nfcwr = NFCADDR_CMD_NFCWR;
Josh Wu6054d4d2013-08-05 19:14:37 +08001946 if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
1947 dataen = NFCADDR_CMD_DATAEN;
Josh Wu7dc37de2013-08-05 19:14:35 +08001948 break;
1949 default:
1950 break;
1951 }
1952
1953 if (do_addr)
Brian Norris3dad2342014-01-29 14:08:12 -08001954 acycle = nfc_make_addr(mtd, command, column, page_addr,
1955 &addr1234, &cycle0);
Josh Wu7dc37de2013-08-05 19:14:35 +08001956
1957 nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
1958 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1959
1960 /*
1961 * Program and erase have their own busy handlers status, sequential
1962 * in, and deplete1 need no delay.
1963 */
1964 switch (command) {
1965 case NAND_CMD_CACHEDPROG:
1966 case NAND_CMD_PAGEPROG:
1967 case NAND_CMD_ERASE1:
1968 case NAND_CMD_ERASE2:
1969 case NAND_CMD_RNDIN:
1970 case NAND_CMD_STATUS:
1971 case NAND_CMD_RNDOUT:
1972 case NAND_CMD_SEQIN:
1973 case NAND_CMD_READID:
1974 return;
1975
1976 case NAND_CMD_READ0:
Josh Wu1ae9c092013-08-05 19:14:36 +08001977 if (dataen == NFCADDR_CMD_DATAEN) {
1978 host->nfc->data_in_sram = host->nfc->sram_bank0 +
1979 nfc_get_sram_off(host);
1980 return;
1981 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001982 /* fall through */
1983 default:
Romain Izard5ddc7bd2016-02-10 10:56:23 +01001984 nfc_prepare_interrupt(host, host->nfc->caps->rb_mask);
1985 nfc_wait_interrupt(host, host->nfc->caps->rb_mask);
Josh Wu7dc37de2013-08-05 19:14:35 +08001986 }
1987}
1988
Josh Wu6054d4d2013-08-05 19:14:37 +08001989static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1990 uint32_t offset, int data_len, const uint8_t *buf,
1991 int oob_required, int page, int cached, int raw)
1992{
1993 int cfg, len;
1994 int status = 0;
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001995 struct atmel_nand_host *host = nand_get_controller_data(chip);
Wu, Josh068b44b2014-11-07 15:26:09 +08001996 void *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
Josh Wu6054d4d2013-08-05 19:14:37 +08001997
1998 /* Subpage write is not supported */
1999 if (offset || (data_len < mtd->writesize))
2000 return -EINVAL;
2001
Josh Wu6054d4d2013-08-05 19:14:37 +08002002 len = mtd->writesize;
Josh Wu6054d4d2013-08-05 19:14:37 +08002003 /* Copy page data to sram that will write to nand via NFC */
2004 if (use_dma) {
2005 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
2006 /* Fall back to use cpu copy */
Wu, Josh068b44b2014-11-07 15:26:09 +08002007 memcpy(sram, buf, len);
Josh Wu6054d4d2013-08-05 19:14:37 +08002008 } else {
Wu, Josh068b44b2014-11-07 15:26:09 +08002009 memcpy(sram, buf, len);
Josh Wu6054d4d2013-08-05 19:14:37 +08002010 }
2011
Wu, Joshff0a2152014-08-05 18:38:52 +08002012 cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
2013 if (unlikely(raw) && oob_required) {
Wu, Josh068b44b2014-11-07 15:26:09 +08002014 memcpy(sram + len, chip->oob_poi, mtd->oobsize);
Wu, Joshff0a2152014-08-05 18:38:52 +08002015 len += mtd->oobsize;
2016 nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
2017 } else {
2018 nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
2019 }
2020
Josh Wu6054d4d2013-08-05 19:14:37 +08002021 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
2022 /*
2023 * When use NFC sram, need set up PMECC before send
2024 * NAND_CMD_SEQIN command. Since when the nand command
2025 * is sent, nfc will do transfer from sram and nand.
2026 */
2027 pmecc_enable(host, NAND_ECC_WRITE);
2028
2029 host->nfc->will_write_sram = true;
2030 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2031 host->nfc->will_write_sram = false;
2032
2033 if (likely(!raw))
2034 /* Need to write ecc into oob */
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002035 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2036 page);
Josh Wu6054d4d2013-08-05 19:14:37 +08002037
2038 if (status < 0)
2039 return status;
2040
2041 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2042 status = chip->waitfunc(mtd, chip);
2043
2044 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2045 status = chip->errstat(mtd, chip, FL_WRITING, status, page);
2046
2047 if (status & NAND_STATUS_FAIL)
2048 return -EIO;
2049
2050 return 0;
2051}
2052
Josh Wu1ae9c092013-08-05 19:14:36 +08002053static int nfc_sram_init(struct mtd_info *mtd)
2054{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01002055 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01002056 struct atmel_nand_host *host = nand_get_controller_data(chip);
Josh Wu1ae9c092013-08-05 19:14:36 +08002057 int res = 0;
2058
2059 /* Initialize the NFC CFG register */
2060 unsigned int cfg_nfc = 0;
2061
2062 /* set page size and oob layout */
2063 switch (mtd->writesize) {
2064 case 512:
2065 cfg_nfc = NFC_CFG_PAGESIZE_512;
2066 break;
2067 case 1024:
2068 cfg_nfc = NFC_CFG_PAGESIZE_1024;
2069 break;
2070 case 2048:
2071 cfg_nfc = NFC_CFG_PAGESIZE_2048;
2072 break;
2073 case 4096:
2074 cfg_nfc = NFC_CFG_PAGESIZE_4096;
2075 break;
2076 case 8192:
2077 cfg_nfc = NFC_CFG_PAGESIZE_8192;
2078 break;
2079 default:
2080 dev_err(host->dev, "Unsupported page size for NFC.\n");
2081 res = -ENXIO;
2082 return res;
2083 }
2084
2085 /* oob bytes size = (NFCSPARESIZE + 1) * 4
2086 * Max support spare size is 512 bytes. */
2087 cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
2088 & NFC_CFG_NFC_SPARESIZE);
2089 /* default set a max timeout */
2090 cfg_nfc |= NFC_CFG_RSPARE |
2091 NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
2092
2093 nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
2094
Josh Wu6054d4d2013-08-05 19:14:37 +08002095 host->nfc->will_write_sram = false;
Josh Wu1ae9c092013-08-05 19:14:36 +08002096 nfc_set_sram_bank(host, 0);
2097
Josh Wu6054d4d2013-08-05 19:14:37 +08002098 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
2099 if (host->nfc->write_by_sram) {
2100 if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
2101 chip->ecc.mode == NAND_ECC_NONE)
2102 chip->write_page = nfc_sram_write_page;
2103 else
2104 host->nfc->write_by_sram = false;
2105 }
Josh Wu1ae9c092013-08-05 19:14:36 +08002106
Josh Wu6054d4d2013-08-05 19:14:37 +08002107 dev_info(host->dev, "Using NFC Sram read %s\n",
2108 host->nfc->write_by_sram ? "and write" : "");
Josh Wu1ae9c092013-08-05 19:14:36 +08002109 return 0;
2110}
2111
Josh Wu7dc37de2013-08-05 19:14:35 +08002112static struct platform_driver atmel_nand_nfc_driver;
Andrew Victor42cb1402006-10-19 18:24:35 +02002113/*
2114 * Probe for the NAND device.
2115 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002116static int atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002117{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002118 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02002119 struct mtd_info *mtd;
2120 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002121 struct resource *mem;
Josh Wu7dc37de2013-08-05 19:14:35 +08002122 int res, irq;
Andrew Victor42cb1402006-10-19 18:24:35 +02002123
2124 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002125 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Han9e3677a2013-12-26 12:00:16 +09002126 if (!host)
Andrew Victor42cb1402006-10-19 18:24:35 +02002127 return -ENOMEM;
Andrew Victor42cb1402006-10-19 18:24:35 +02002128
Josh Wu7dc37de2013-08-05 19:14:35 +08002129 res = platform_driver_register(&atmel_nand_nfc_driver);
2130 if (res)
2131 dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
2132
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002133 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2134 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
2135 if (IS_ERR(host->io_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002136 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002137 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02002138 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002139 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02002140
Andrew Victor42cb1402006-10-19 18:24:35 +02002141 nand_chip = &host->nand_chip;
Boris BREZILLONac01efe2015-12-10 08:59:50 +01002142 mtd = nand_to_mtd(nand_chip);
Richard Genoud77f54922008-04-23 19:51:14 +02002143 host->dev = &pdev->dev;
Josh Wue9d8da82013-09-18 11:31:19 +08002144 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
Brian Norrisa61ae812015-10-30 20:33:25 -07002145 nand_set_flash_node(nand_chip, pdev->dev.of_node);
Josh Wue9d8da82013-09-18 11:31:19 +08002146 /* Only when CONFIG_OF is enabled of_node can be parsed */
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002147 res = atmel_of_init_port(host, pdev->dev.of_node);
2148 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002149 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002150 } else {
Jingoo Han453810b2013-07-30 17:18:33 +09002151 memcpy(&host->board, dev_get_platdata(&pdev->dev),
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002152 sizeof(struct atmel_nand_data));
2153 }
Andrew Victor42cb1402006-10-19 18:24:35 +02002154
Boris BREZILLONd699ed22015-12-10 09:00:41 +01002155 /* link the private data structures */
2156 nand_set_controller_data(nand_chip, host);
Frans Klaver03c287d2015-06-10 22:38:36 +02002157 mtd->dev.parent = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +02002158
2159 /* Set address of NAND IO lines */
2160 nand_chip->IO_ADDR_R = host->io_base;
2161 nand_chip->IO_ADDR_W = host->io_base;
Ivan Kutena4265f82007-05-24 14:35:58 +03002162
Josh Wu7dc37de2013-08-05 19:14:35 +08002163 if (nand_nfc.is_initialized) {
2164 /* NFC driver is probed and initialized */
2165 host->nfc = &nand_nfc;
2166
2167 nand_chip->select_chip = nfc_select_chip;
2168 nand_chip->dev_ready = nfc_device_ready;
2169 nand_chip->cmdfunc = nfc_nand_command;
2170
2171 /* Initialize the interrupt for NFC */
2172 irq = platform_get_irq(pdev, 0);
2173 if (irq < 0) {
2174 dev_err(host->dev, "Cannot get HSMC irq!\n");
Wei Yongjunff52c672013-08-23 10:50:36 +08002175 res = irq;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002176 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002177 }
2178
Josh Wu7dc37de2013-08-05 19:14:35 +08002179 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
2180 0, "hsmc", host);
2181 if (res) {
2182 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
2183 irq);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002184 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002185 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002186 } else {
2187 res = atmel_nand_set_enable_ready_pins(mtd);
2188 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002189 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002190
Josh Wu7dc37de2013-08-05 19:14:35 +08002191 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002192 }
Ivan Kutena4265f82007-05-24 14:35:58 +03002193
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002194 nand_chip->ecc.mode = host->board.ecc_mode;
Raphaël Poggi796fe362014-07-29 15:27:27 +02002195 nand_chip->chip_delay = 40; /* 40us command delay time */
Andrew Victor42cb1402006-10-19 18:24:35 +02002196
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002197 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02002198 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08002199
2200 nand_chip->read_buf = atmel_read_buf;
2201 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02002202
Andrew Victor42cb1402006-10-19 18:24:35 +02002203 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002204 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002205
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002206 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002207 res = devm_gpio_request(&pdev->dev,
2208 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002209 if (res < 0) {
2210 dev_err(&pdev->dev,
2211 "can't request det gpio %d\n",
2212 host->board.det_pin);
2213 goto err_no_card;
2214 }
2215
2216 res = gpio_direction_input(host->board.det_pin);
2217 if (res < 0) {
2218 dev_err(&pdev->dev,
2219 "can't request input direction det gpio %d\n",
2220 host->board.det_pin);
2221 goto err_no_card;
2222 }
2223
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002224 if (gpio_get_value(host->board.det_pin)) {
Jingoo Han1295f972013-12-26 12:30:58 +09002225 dev_info(&pdev->dev, "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01002226 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002227 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02002228 }
2229 }
2230
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002231 if (host->board.on_flash_bbt || on_flash_bbt) {
Jingoo Han1295f972013-12-26 12:30:58 +09002232 dev_info(&pdev->dev, "Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -07002233 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa6972009-05-27 18:19:39 +03002234 }
2235
Josh Wu1b719262013-05-09 15:34:55 +08002236 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08002237 use_dma = 0;
2238
2239 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08002240 dma_cap_mask_t mask;
2241
2242 dma_cap_zero(mask);
2243 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02002244 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002245 if (!host->dma_chan) {
2246 dev_err(host->dev, "Failed to request DMA channel\n");
2247 use_dma = 0;
2248 }
2249 }
2250 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08002251 dev_info(host->dev, "Using %s for DMA transfers.\n",
2252 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08002253 else
2254 dev_info(host->dev, "No DMA support for NAND access.\n");
2255
Richard Genoud77f54922008-04-23 19:51:14 +02002256 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00002257 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02002258 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002259 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02002260 }
2261
Richard Genoud3fc23892008-10-12 08:42:28 +02002262 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08002263 if (host->has_pmecc)
2264 res = atmel_pmecc_nand_init_params(pdev, host);
2265 else
2266 res = atmel_hw_nand_init_params(pdev, host);
2267
Josh Wu3dfe41a2012-06-25 18:07:43 +08002268 if (res != 0)
2269 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02002270 }
2271
Josh Wu1ae9c092013-08-05 19:14:36 +08002272 /* initialize the nfc configuration register */
2273 if (host->nfc && host->nfc->use_nfc_sram) {
2274 res = nfc_sram_init(mtd);
2275 if (res) {
2276 host->nfc->use_nfc_sram = false;
2277 dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
2278 }
2279 }
2280
Richard Genoud77f54922008-04-23 19:51:14 +02002281 /* second phase scan */
2282 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02002283 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002284 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02002285 }
2286
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002287 mtd->name = "atmel_nand";
Brian Norrisa61ae812015-10-30 20:33:25 -07002288 res = mtd_device_register(mtd, host->board.parts,
2289 host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02002290 if (!res)
2291 return res;
2292
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002293err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002294 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08002295 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08002296err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002297err_scan_ident:
2298err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002299 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002300 if (host->dma_chan)
2301 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002302err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02002303 return res;
2304}
2305
2306/*
2307 * Remove a NAND device.
2308 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002309static int atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002310{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002311 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Boris BREZILLONac01efe2015-12-10 08:59:50 +01002312 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
Andrew Victor42cb1402006-10-19 18:24:35 +02002313
2314 nand_release(mtd);
2315
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002316 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002317
Josh Wu1c7b8742012-06-29 17:47:55 +08002318 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
2319 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2320 pmerrloc_writel(host->pmerrloc_base, ELDIS,
2321 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08002322 }
2323
Hong Xucbc6c5e2011-01-18 14:36:05 +08002324 if (host->dma_chan)
2325 dma_release_channel(host->dma_chan);
2326
Josh Wu7dc37de2013-08-05 19:14:35 +08002327 platform_driver_unregister(&atmel_nand_nfc_driver);
2328
Andrew Victor42cb1402006-10-19 18:24:35 +02002329 return 0;
2330}
2331
Romain Izard55750752016-02-10 10:56:25 +01002332/*
2333 * AT91RM9200 does not have PMECC or PMECC Errloc peripherals for
2334 * BCH ECC. Combined with the "atmel,has-pmecc", it is used to describe
2335 * devices from the SAM9 family that have those.
2336 */
LABBE Corentin72eaec22015-11-20 08:45:16 +01002337static const struct atmel_nand_caps at91rm9200_caps = {
Wu, Josh51585772015-01-19 16:33:06 +08002338 .pmecc_correct_erase_page = false,
Romain Izard55750752016-02-10 10:56:25 +01002339 .pmecc_max_correction = 24,
Wu, Josh51585772015-01-19 16:33:06 +08002340};
2341
LABBE Corentin72eaec22015-11-20 08:45:16 +01002342static const struct atmel_nand_caps sama5d4_caps = {
Wu, Josh51585772015-01-19 16:33:06 +08002343 .pmecc_correct_erase_page = true,
Romain Izard55750752016-02-10 10:56:25 +01002344 .pmecc_max_correction = 24,
2345};
2346
2347/*
2348 * The PMECC Errloc controller starting in SAMA5D2 is not compatible,
2349 * as the increased correction strength requires more registers.
2350 */
2351static const struct atmel_nand_caps sama5d2_caps = {
2352 .pmecc_correct_erase_page = true,
2353 .pmecc_max_correction = 32,
Wu, Josh51585772015-01-19 16:33:06 +08002354};
2355
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002356static const struct of_device_id atmel_nand_dt_ids[] = {
Wu, Josh51585772015-01-19 16:33:06 +08002357 { .compatible = "atmel,at91rm9200-nand", .data = &at91rm9200_caps },
2358 { .compatible = "atmel,sama5d4-nand", .data = &sama5d4_caps },
Romain Izard55750752016-02-10 10:56:25 +01002359 { .compatible = "atmel,sama5d2-nand", .data = &sama5d2_caps },
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002360 { /* sentinel */ }
2361};
2362
2363MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002364
Josh Wu7dc37de2013-08-05 19:14:35 +08002365static int atmel_nand_nfc_probe(struct platform_device *pdev)
2366{
2367 struct atmel_nfc *nfc = &nand_nfc;
2368 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002369 int ret;
Josh Wu7dc37de2013-08-05 19:14:35 +08002370
2371 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2372 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
2373 if (IS_ERR(nfc->base_cmd_regs))
2374 return PTR_ERR(nfc->base_cmd_regs);
2375
2376 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2377 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
2378 if (IS_ERR(nfc->hsmc_regs))
2379 return PTR_ERR(nfc->hsmc_regs);
2380
2381 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2382 if (nfc_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +08002383 nfc->sram_bank0 = (void * __force)
2384 devm_ioremap_resource(&pdev->dev, nfc_sram);
Josh Wu1ae9c092013-08-05 19:14:36 +08002385 if (IS_ERR(nfc->sram_bank0)) {
Josh Wu7dc37de2013-08-05 19:14:35 +08002386 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2387 PTR_ERR(nfc->sram_bank0));
Josh Wu1ae9c092013-08-05 19:14:36 +08002388 } else {
2389 nfc->use_nfc_sram = true;
Josh Wu7dc37de2013-08-05 19:14:35 +08002390 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
Josh Wu6054d4d2013-08-05 19:14:37 +08002391
2392 if (pdev->dev.of_node)
2393 nfc->write_by_sram = of_property_read_bool(
2394 pdev->dev.of_node,
2395 "atmel,write-by-sram");
Josh Wu1ae9c092013-08-05 19:14:36 +08002396 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002397 }
2398
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002399 nfc->caps = (const struct atmel_nand_nfc_caps *)
2400 of_device_get_match_data(&pdev->dev);
2401 if (!nfc->caps)
2402 return -ENODEV;
2403
Wu, Josh50e04e22014-06-10 17:50:09 +08002404 nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
2405 nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */
2406
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002407 nfc->clk = devm_clk_get(&pdev->dev, NULL);
2408 if (!IS_ERR(nfc->clk)) {
2409 ret = clk_prepare_enable(nfc->clk);
2410 if (ret)
2411 return ret;
2412 } else {
2413 dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree");
2414 }
2415
Josh Wu7dc37de2013-08-05 19:14:35 +08002416 nfc->is_initialized = true;
2417 dev_info(&pdev->dev, "NFC is probed.\n");
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002418
2419 return 0;
2420}
2421
2422static int atmel_nand_nfc_remove(struct platform_device *pdev)
2423{
2424 struct atmel_nfc *nfc = &nand_nfc;
2425
2426 if (!IS_ERR(nfc->clk))
2427 clk_disable_unprepare(nfc->clk);
2428
Josh Wu7dc37de2013-08-05 19:14:35 +08002429 return 0;
2430}
2431
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002432static const struct atmel_nand_nfc_caps sama5d3_nfc_caps = {
2433 .rb_mask = NFC_SR_RB_EDGE0,
2434};
2435
2436static const struct atmel_nand_nfc_caps sama5d4_nfc_caps = {
2437 .rb_mask = NFC_SR_RB_EDGE3,
2438};
2439
Josh Wu81f29b42013-09-18 11:31:20 +08002440static const struct of_device_id atmel_nand_nfc_match[] = {
Romain Izard5ddc7bd2016-02-10 10:56:23 +01002441 { .compatible = "atmel,sama5d3-nfc", .data = &sama5d3_nfc_caps },
2442 { .compatible = "atmel,sama5d4-nfc", .data = &sama5d4_nfc_caps },
Josh Wu7dc37de2013-08-05 19:14:35 +08002443 { /* sentinel */ }
2444};
Josh Wu81f29b42013-09-18 11:31:20 +08002445MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
Josh Wu7dc37de2013-08-05 19:14:35 +08002446
2447static struct platform_driver atmel_nand_nfc_driver = {
2448 .driver = {
2449 .name = "atmel_nand_nfc",
Josh Wu7dc37de2013-08-05 19:14:35 +08002450 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2451 },
2452 .probe = atmel_nand_nfc_probe,
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002453 .remove = atmel_nand_nfc_remove,
Josh Wu7dc37de2013-08-05 19:14:35 +08002454};
2455
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002456static struct platform_driver atmel_nand_driver = {
Johan Hovold2c2b9282013-09-23 16:27:28 +02002457 .probe = atmel_nand_probe,
2458 .remove = atmel_nand_remove,
Andrew Victor42cb1402006-10-19 18:24:35 +02002459 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002460 .name = "atmel_nand",
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002461 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02002462 },
2463};
2464
Johan Hovold2c2b9282013-09-23 16:27:28 +02002465module_platform_driver(atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02002466
2467MODULE_LICENSE("GPL");
2468MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02002469MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002470MODULE_ALIAS("platform:atmel_nand");