blob: 9c28d010dbd9354de25c2566e1d4bb787cc50a10 [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
66/* oob layout for large page size
67 * bad block info is on bytes 0 and 1
68 * the bytes have to be consecutives to avoid
69 * several NAND_CMD_RNDOUT during read
70 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020071static struct nand_ecclayout atmel_oobinfo_large = {
Richard Genoud77f54922008-04-23 19:51:14 +020072 .eccbytes = 4,
73 .eccpos = {60, 61, 62, 63},
74 .oobfree = {
75 {2, 58}
76 },
77};
78
79/* oob layout for small page size
80 * bad block info is on bytes 4 and 5
81 * the bytes have to be consecutives to avoid
82 * several NAND_CMD_RNDOUT during read
83 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +020084static struct nand_ecclayout atmel_oobinfo_small = {
Richard Genoud77f54922008-04-23 19:51:14 +020085 .eccbytes = 4,
86 .eccpos = {0, 1, 2, 3},
87 .oobfree = {
88 {6, 10}
89 },
90};
91
Josh Wu7dc37de2013-08-05 19:14:35 +080092struct atmel_nfc {
93 void __iomem *base_cmd_regs;
94 void __iomem *hsmc_regs;
Wu, Josh068b44b2014-11-07 15:26:09 +080095 void *sram_bank0;
Josh Wu7dc37de2013-08-05 19:14:35 +080096 dma_addr_t sram_bank0_phys;
Josh Wu1ae9c092013-08-05 19:14:36 +080097 bool use_nfc_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +080098 bool write_by_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +080099
Boris BREZILLON2d405ec2014-09-13 01:23:59 +0200100 struct clk *clk;
101
Josh Wu7dc37de2013-08-05 19:14:35 +0800102 bool is_initialized;
Josh Wue4e06932014-06-10 17:50:11 +0800103 struct completion comp_ready;
104 struct completion comp_cmd_done;
105 struct completion comp_xfer_done;
Josh Wu1ae9c092013-08-05 19:14:36 +0800106
107 /* Point to the sram bank which include readed data via NFC */
Wu, Josh068b44b2014-11-07 15:26:09 +0800108 void *data_in_sram;
Josh Wu6054d4d2013-08-05 19:14:37 +0800109 bool will_write_sram;
Josh Wu7dc37de2013-08-05 19:14:35 +0800110};
111static struct atmel_nfc nand_nfc;
112
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200113struct atmel_nand_host {
Andrew Victor42cb1402006-10-19 18:24:35 +0200114 struct nand_chip nand_chip;
115 struct mtd_info mtd;
116 void __iomem *io_base;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800117 dma_addr_t io_phys;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800118 struct atmel_nand_data board;
Richard Genoud77f54922008-04-23 19:51:14 +0200119 struct device *dev;
120 void __iomem *ecc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800121
122 struct completion comp;
123 struct dma_chan *dma_chan;
Josh Wua41b51a2012-06-29 17:47:54 +0800124
Josh Wu7dc37de2013-08-05 19:14:35 +0800125 struct atmel_nfc *nfc;
126
Josh Wua41b51a2012-06-29 17:47:54 +0800127 bool has_pmecc;
128 u8 pmecc_corr_cap;
129 u16 pmecc_sector_size;
Josh Wuabb1cd02014-10-11 18:01:50 +0800130 bool has_no_lookup_table;
Josh Wua41b51a2012-06-29 17:47:54 +0800131 u32 pmecc_lookup_table_offset;
Josh Wue66b4312013-01-23 20:47:11 +0800132 u32 pmecc_lookup_table_offset_512;
133 u32 pmecc_lookup_table_offset_1024;
Josh Wu1c7b8742012-06-29 17:47:55 +0800134
Josh Wu1c7b8742012-06-29 17:47:55 +0800135 int pmecc_degree; /* Degree of remainders */
136 int pmecc_cw_len; /* Length of codeword */
137
138 void __iomem *pmerrloc_base;
139 void __iomem *pmecc_rom_base;
140
141 /* lookup table for alpha_to and index_of */
142 void __iomem *pmecc_alpha_to;
143 void __iomem *pmecc_index_of;
144
145 /* data for pmecc computation */
146 int16_t *pmecc_partial_syn;
147 int16_t *pmecc_si;
148 int16_t *pmecc_smu; /* Sigma table */
149 int16_t *pmecc_lmu; /* polynomal order */
150 int *pmecc_mu;
151 int *pmecc_dmu;
152 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200153};
154
Josh Wu1c7b8742012-06-29 17:47:55 +0800155static struct nand_ecclayout atmel_pmecc_oobinfo;
156
Andrew Victor42cb1402006-10-19 18:24:35 +0200157/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900158 * Enable NAND.
159 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200160static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900161{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800162 if (gpio_is_valid(host->board.enable_pin))
163 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900164}
165
166/*
167 * Disable NAND.
168 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200169static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900170{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800171 if (gpio_is_valid(host->board.enable_pin))
172 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900173}
174
175/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200176 * Hardware specific access to control-lines
177 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200178static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200179{
180 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200181 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200182
Atsushi Nemoto81365082008-04-27 01:51:12 +0900183 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900184 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200185 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900186 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200187 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900188 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200189 if (cmd == NAND_CMD_NONE)
190 return;
191
192 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800193 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200194 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800195 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200196}
197
198/*
199 * Read the Device Ready pin.
200 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200201static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200202{
203 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200204 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200205
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800206 return gpio_get_value(host->board.rdy_pin) ^
207 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200208}
209
Josh Wu7dc37de2013-08-05 19:14:35 +0800210/* Set up for hardware ready pin and enable pin. */
211static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
212{
213 struct nand_chip *chip = mtd->priv;
214 struct atmel_nand_host *host = chip->priv;
215 int res = 0;
216
217 if (gpio_is_valid(host->board.rdy_pin)) {
218 res = devm_gpio_request(host->dev,
219 host->board.rdy_pin, "nand_rdy");
220 if (res < 0) {
221 dev_err(host->dev,
222 "can't request rdy gpio %d\n",
223 host->board.rdy_pin);
224 return res;
225 }
226
227 res = gpio_direction_input(host->board.rdy_pin);
228 if (res < 0) {
229 dev_err(host->dev,
230 "can't request input direction rdy gpio %d\n",
231 host->board.rdy_pin);
232 return res;
233 }
234
235 chip->dev_ready = atmel_nand_device_ready;
236 }
237
238 if (gpio_is_valid(host->board.enable_pin)) {
239 res = devm_gpio_request(host->dev,
240 host->board.enable_pin, "nand_enable");
241 if (res < 0) {
242 dev_err(host->dev,
243 "can't request enable gpio %d\n",
244 host->board.enable_pin);
245 return res;
246 }
247
248 res = gpio_direction_output(host->board.enable_pin, 1);
249 if (res < 0) {
250 dev_err(host->dev,
251 "can't request output direction enable gpio %d\n",
252 host->board.enable_pin);
253 return res;
254 }
255 }
256
257 return res;
258}
259
Artem Bityutskiy50082312012-02-02 13:54:25 +0200260/*
261 * Minimal-overhead PIO for data access.
262 */
263static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
264{
265 struct nand_chip *nand_chip = mtd->priv;
Josh Wu1ae9c092013-08-05 19:14:36 +0800266 struct atmel_nand_host *host = nand_chip->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200267
Josh Wu1ae9c092013-08-05 19:14:36 +0800268 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +0800269 memcpy(buf, host->nfc->data_in_sram, len);
Josh Wu1ae9c092013-08-05 19:14:36 +0800270 host->nfc->data_in_sram += len;
271 } else {
272 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
273 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200274}
275
276static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
277{
278 struct nand_chip *nand_chip = mtd->priv;
Josh Wu1ae9c092013-08-05 19:14:36 +0800279 struct atmel_nand_host *host = nand_chip->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200280
Josh Wu1ae9c092013-08-05 19:14:36 +0800281 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +0800282 memcpy(buf, host->nfc->data_in_sram, len);
Josh Wu1ae9c092013-08-05 19:14:36 +0800283 host->nfc->data_in_sram += len;
284 } else {
285 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
286 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200287}
288
289static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
290{
291 struct nand_chip *nand_chip = mtd->priv;
292
293 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
294}
295
296static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
297{
298 struct nand_chip *nand_chip = mtd->priv;
299
300 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
301}
302
Hong Xucbc6c5e2011-01-18 14:36:05 +0800303static void dma_complete_func(void *completion)
304{
305 complete(completion);
306}
307
Josh Wu1ae9c092013-08-05 19:14:36 +0800308static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
309{
310 /* NFC only has two banks. Must be 0 or 1 */
311 if (bank > 1)
312 return -EINVAL;
313
314 if (bank) {
315 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
316 if (host->mtd.writesize > 2048)
317 return -EINVAL;
318 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
319 } else {
320 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
321 }
322
323 return 0;
324}
325
326static uint nfc_get_sram_off(struct atmel_nand_host *host)
327{
328 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
329 return NFC_SRAM_BANK1_OFFSET;
330 else
331 return 0;
332}
333
334static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
335{
336 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
337 return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
338 else
339 return host->nfc->sram_bank0_phys;
340}
341
Hong Xucbc6c5e2011-01-18 14:36:05 +0800342static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
343 int is_read)
344{
345 struct dma_device *dma_dev;
346 enum dma_ctrl_flags flags;
347 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
348 struct dma_async_tx_descriptor *tx = NULL;
349 dma_cookie_t cookie;
350 struct nand_chip *chip = mtd->priv;
351 struct atmel_nand_host *host = chip->priv;
352 void *p = buf;
353 int err = -EIO;
354 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Josh Wu1ae9c092013-08-05 19:14:36 +0800355 struct atmel_nfc *nfc = host->nfc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800356
Hong Xu80b4f812011-03-31 18:33:15 +0800357 if (buf >= high_memory)
358 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800359
360 dma_dev = host->dma_chan->device;
361
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200362 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800363
364 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
365 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
366 dev_err(host->dev, "Failed to dma_map_single\n");
367 goto err_buf;
368 }
369
370 if (is_read) {
Josh Wu1ae9c092013-08-05 19:14:36 +0800371 if (nfc && nfc->data_in_sram)
372 dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
373 - (nfc->sram_bank0 + nfc_get_sram_off(host)));
374 else
375 dma_src_addr = host->io_phys;
376
Hong Xucbc6c5e2011-01-18 14:36:05 +0800377 dma_dst_addr = phys_addr;
378 } else {
379 dma_src_addr = phys_addr;
Josh Wu6054d4d2013-08-05 19:14:37 +0800380
381 if (nfc && nfc->write_by_sram)
382 dma_dst_addr = nfc_sram_phys(host);
383 else
384 dma_dst_addr = host->io_phys;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800385 }
386
387 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
388 dma_src_addr, len, flags);
389 if (!tx) {
390 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
391 goto err_dma;
392 }
393
394 init_completion(&host->comp);
395 tx->callback = dma_complete_func;
396 tx->callback_param = &host->comp;
397
398 cookie = tx->tx_submit(tx);
399 if (dma_submit_error(cookie)) {
400 dev_err(host->dev, "Failed to do DMA tx_submit\n");
401 goto err_dma;
402 }
403
404 dma_async_issue_pending(host->dma_chan);
405 wait_for_completion(&host->comp);
406
Josh Wu1ae9c092013-08-05 19:14:36 +0800407 if (is_read && nfc && nfc->data_in_sram)
408 /* After read data from SRAM, need to increase the position */
409 nfc->data_in_sram += len;
410
Hong Xucbc6c5e2011-01-18 14:36:05 +0800411 err = 0;
412
413err_dma:
414 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
415err_buf:
416 if (err != 0)
Nicolas Ferre74414a942014-02-12 12:26:54 +0100417 dev_dbg(host->dev, "Fall back to CPU I/O\n");
Hong Xucbc6c5e2011-01-18 14:36:05 +0800418 return err;
419}
420
421static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
422{
423 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200424 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800425
Nicolas Ferre9d515672011-04-01 16:40:44 +0200426 if (use_dma && len > mtd->oobsize)
427 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800428 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
429 return;
430
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800431 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200432 atmel_read_buf16(mtd, buf, len);
433 else
434 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800435}
436
437static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
438{
439 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200440 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800441
Nicolas Ferre9d515672011-04-01 16:40:44 +0200442 if (use_dma && len > mtd->oobsize)
443 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800444 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
445 return;
446
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800447 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200448 atmel_write_buf16(mtd, buf, len);
449 else
450 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800451}
452
David Brownell23a346c2008-07-03 23:40:16 -0700453/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800454 * Return number of ecc bytes per sector according to sector size and
455 * correction capability
456 *
457 * Following table shows what at91 PMECC supported:
458 * Correction Capability Sector_512_bytes Sector_1024_bytes
459 * ===================== ================ =================
460 * 2-bits 4-bytes 4-bytes
461 * 4-bits 7-bytes 7-bytes
462 * 8-bits 13-bytes 14-bytes
463 * 12-bits 20-bytes 21-bytes
464 * 24-bits 39-bytes 42-bytes
465 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500466static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800467{
468 int m = 12 + sector_size / 512;
469 return (m * cap + 7) / 8;
470}
471
Bill Pemberton06f25512012-11-19 13:23:07 -0500472static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800473 int oobsize, int ecc_len)
Josh Wu1c7b8742012-06-29 17:47:55 +0800474{
475 int i;
476
477 layout->eccbytes = ecc_len;
478
479 /* ECC will occupy the last ecc_len bytes continuously */
480 for (i = 0; i < ecc_len; i++)
481 layout->eccpos[i] = oobsize - ecc_len + i;
482
483 layout->oobfree[0].offset = 2;
484 layout->oobfree[0].length =
485 oobsize - ecc_len - layout->oobfree[0].offset;
486}
487
Bill Pemberton06f25512012-11-19 13:23:07 -0500488static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800489{
490 int table_size;
491
492 table_size = host->pmecc_sector_size == 512 ?
493 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
494
495 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
496 table_size * sizeof(int16_t);
497}
498
Bill Pemberton06f25512012-11-19 13:23:07 -0500499static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800500{
501 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800502 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800503
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800504 size = (2 * cap + 1) * sizeof(int16_t);
505 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
506 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
507 host->pmecc_lmu = devm_kzalloc(host->dev,
508 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
509 host->pmecc_smu = devm_kzalloc(host->dev,
510 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800511
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800512 size = (cap + 1) * sizeof(int);
513 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
514 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
515 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800516
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800517 if (!host->pmecc_partial_syn ||
518 !host->pmecc_si ||
519 !host->pmecc_lmu ||
520 !host->pmecc_smu ||
521 !host->pmecc_mu ||
522 !host->pmecc_dmu ||
523 !host->pmecc_delta)
524 return -ENOMEM;
525
526 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800527}
528
529static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
530{
531 struct nand_chip *nand_chip = mtd->priv;
532 struct atmel_nand_host *host = nand_chip->priv;
533 int i;
534 uint32_t value;
535
536 /* Fill odd syndromes */
537 for (i = 0; i < host->pmecc_corr_cap; i++) {
538 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
539 if (i & 1)
540 value >>= 16;
541 value &= 0xffff;
542 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
543 }
544}
545
546static void pmecc_substitute(struct mtd_info *mtd)
547{
548 struct nand_chip *nand_chip = mtd->priv;
549 struct atmel_nand_host *host = nand_chip->priv;
550 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
551 int16_t __iomem *index_of = host->pmecc_index_of;
552 int16_t *partial_syn = host->pmecc_partial_syn;
553 const int cap = host->pmecc_corr_cap;
554 int16_t *si;
555 int i, j;
556
557 /* si[] is a table that holds the current syndrome value,
558 * an element of that table belongs to the field
559 */
560 si = host->pmecc_si;
561
562 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
563
564 /* Computation 2t syndromes based on S(x) */
565 /* Odd syndromes */
566 for (i = 1; i < 2 * cap; i += 2) {
567 for (j = 0; j < host->pmecc_degree; j++) {
568 if (partial_syn[i] & ((unsigned short)0x1 << j))
569 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
570 }
571 }
572 /* Even syndrome = (Odd syndrome) ** 2 */
573 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
574 if (si[j] == 0) {
575 si[i] = 0;
576 } else {
577 int16_t tmp;
578
579 tmp = readw_relaxed(index_of + si[j]);
580 tmp = (tmp * 2) % host->pmecc_cw_len;
581 si[i] = readw_relaxed(alpha_to + tmp);
582 }
583 }
584
585 return;
586}
587
588static void pmecc_get_sigma(struct mtd_info *mtd)
589{
590 struct nand_chip *nand_chip = mtd->priv;
591 struct atmel_nand_host *host = nand_chip->priv;
592
593 int16_t *lmu = host->pmecc_lmu;
594 int16_t *si = host->pmecc_si;
595 int *mu = host->pmecc_mu;
596 int *dmu = host->pmecc_dmu; /* Discrepancy */
597 int *delta = host->pmecc_delta; /* Delta order */
598 int cw_len = host->pmecc_cw_len;
599 const int16_t cap = host->pmecc_corr_cap;
600 const int num = 2 * cap + 1;
601 int16_t __iomem *index_of = host->pmecc_index_of;
602 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
603 int i, j, k;
604 uint32_t dmu_0_count, tmp;
605 int16_t *smu = host->pmecc_smu;
606
607 /* index of largest delta */
608 int ro;
609 int largest;
610 int diff;
611
612 dmu_0_count = 0;
613
614 /* First Row */
615
616 /* Mu */
617 mu[0] = -1;
618
619 memset(smu, 0, sizeof(int16_t) * num);
620 smu[0] = 1;
621
622 /* discrepancy set to 1 */
623 dmu[0] = 1;
624 /* polynom order set to 0 */
625 lmu[0] = 0;
626 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
627
628 /* Second Row */
629
630 /* Mu */
631 mu[1] = 0;
632 /* Sigma(x) set to 1 */
633 memset(&smu[num], 0, sizeof(int16_t) * num);
634 smu[num] = 1;
635
636 /* discrepancy set to S1 */
637 dmu[1] = si[1];
638
639 /* polynom order set to 0 */
640 lmu[1] = 0;
641
642 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
643
644 /* Init the Sigma(x) last row */
645 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
646
647 for (i = 1; i <= cap; i++) {
648 mu[i + 1] = i << 1;
649 /* Begin Computing Sigma (Mu+1) and L(mu) */
650 /* check if discrepancy is set to 0 */
651 if (dmu[i] == 0) {
652 dmu_0_count++;
653
654 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
655 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
656 tmp += 2;
657 else
658 tmp += 1;
659
660 if (dmu_0_count == tmp) {
661 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
662 smu[(cap + 1) * num + j] =
663 smu[i * num + j];
664
665 lmu[cap + 1] = lmu[i];
666 return;
667 }
668
669 /* copy polynom */
670 for (j = 0; j <= lmu[i] >> 1; j++)
671 smu[(i + 1) * num + j] = smu[i * num + j];
672
673 /* copy previous polynom order to the next */
674 lmu[i + 1] = lmu[i];
675 } else {
676 ro = 0;
677 largest = -1;
678 /* find largest delta with dmu != 0 */
679 for (j = 0; j < i; j++) {
680 if ((dmu[j]) && (delta[j] > largest)) {
681 largest = delta[j];
682 ro = j;
683 }
684 }
685
686 /* compute difference */
687 diff = (mu[i] - mu[ro]);
688
689 /* Compute degree of the new smu polynomial */
690 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
691 lmu[i + 1] = lmu[i];
692 else
693 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
694
695 /* Init smu[i+1] with 0 */
696 for (k = 0; k < num; k++)
697 smu[(i + 1) * num + k] = 0;
698
699 /* Compute smu[i+1] */
700 for (k = 0; k <= lmu[ro] >> 1; k++) {
701 int16_t a, b, c;
702
703 if (!(smu[ro * num + k] && dmu[i]))
704 continue;
705 a = readw_relaxed(index_of + dmu[i]);
706 b = readw_relaxed(index_of + dmu[ro]);
707 c = readw_relaxed(index_of + smu[ro * num + k]);
708 tmp = a + (cw_len - b) + c;
709 a = readw_relaxed(alpha_to + tmp % cw_len);
710 smu[(i + 1) * num + (k + diff)] = a;
711 }
712
713 for (k = 0; k <= lmu[i] >> 1; k++)
714 smu[(i + 1) * num + k] ^= smu[i * num + k];
715 }
716
717 /* End Computing Sigma (Mu+1) and L(mu) */
718 /* In either case compute delta */
719 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
720
721 /* Do not compute discrepancy for the last iteration */
722 if (i >= cap)
723 continue;
724
725 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
726 tmp = 2 * (i - 1);
727 if (k == 0) {
728 dmu[i + 1] = si[tmp + 3];
729 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
730 int16_t a, b, c;
731 a = readw_relaxed(index_of +
732 smu[(i + 1) * num + k]);
733 b = si[2 * (i - 1) + 3 - k];
734 c = readw_relaxed(index_of + b);
735 tmp = a + c;
736 tmp %= cw_len;
737 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
738 dmu[i + 1];
739 }
740 }
741 }
742
743 return;
744}
745
746static int pmecc_err_location(struct mtd_info *mtd)
747{
748 struct nand_chip *nand_chip = mtd->priv;
749 struct atmel_nand_host *host = nand_chip->priv;
750 unsigned long end_time;
751 const int cap = host->pmecc_corr_cap;
752 const int num = 2 * cap + 1;
753 int sector_size = host->pmecc_sector_size;
754 int err_nbr = 0; /* number of error */
755 int roots_nbr; /* number of roots */
756 int i;
757 uint32_t val;
758 int16_t *smu = host->pmecc_smu;
759
760 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
761
762 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
763 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
764 smu[(cap + 1) * num + i]);
765 err_nbr++;
766 }
767
768 val = (err_nbr - 1) << 16;
769 if (sector_size == 1024)
770 val |= 1;
771
772 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
773 pmerrloc_writel(host->pmerrloc_base, ELEN,
774 sector_size * 8 + host->pmecc_degree * cap);
775
776 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
777 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
778 & PMERRLOC_CALC_DONE)) {
779 if (unlikely(time_after(jiffies, end_time))) {
780 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
781 return -1;
782 }
783 cpu_relax();
784 }
785
786 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
787 & PMERRLOC_ERR_NUM_MASK) >> 8;
788 /* Number of roots == degree of smu hence <= cap */
789 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
790 return err_nbr - 1;
791
792 /* Number of roots does not match the degree of smu
793 * unable to correct error */
794 return -1;
795}
796
797static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
798 int sector_num, int extra_bytes, int err_nbr)
799{
800 struct nand_chip *nand_chip = mtd->priv;
801 struct atmel_nand_host *host = nand_chip->priv;
802 int i = 0;
803 int byte_pos, bit_pos, sector_size, pos;
804 uint32_t tmp;
805 uint8_t err_byte;
806
807 sector_size = host->pmecc_sector_size;
808
809 while (err_nbr) {
810 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
811 byte_pos = tmp / 8;
812 bit_pos = tmp % 8;
813
814 if (byte_pos >= (sector_size + extra_bytes))
815 BUG(); /* should never happen */
816
817 if (byte_pos < sector_size) {
818 err_byte = *(buf + byte_pos);
819 *(buf + byte_pos) ^= (1 << bit_pos);
820
821 pos = sector_num * host->pmecc_sector_size + byte_pos;
822 dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
823 pos, bit_pos, err_byte, *(buf + byte_pos));
824 } else {
825 /* Bit flip in OOB area */
Wu, Josh022a4782014-08-08 17:12:35 +0800826 tmp = sector_num * nand_chip->ecc.bytes
Josh Wu1c7b8742012-06-29 17:47:55 +0800827 + (byte_pos - sector_size);
828 err_byte = ecc[tmp];
829 ecc[tmp] ^= (1 << bit_pos);
830
831 pos = tmp + nand_chip->ecc.layout->eccpos[0];
832 dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
833 pos, bit_pos, err_byte, ecc[tmp]);
834 }
835
836 i++;
837 err_nbr--;
838 }
839
840 return;
841}
842
843static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
844 u8 *ecc)
845{
846 struct nand_chip *nand_chip = mtd->priv;
847 struct atmel_nand_host *host = nand_chip->priv;
Bo Shenb3857662014-06-12 15:58:45 +0800848 int i, err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800849 uint8_t *buf_pos;
Josh Wuc0c70d92012-11-27 18:50:31 +0800850 int total_err = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800851
Bo Shenb3857662014-06-12 15:58:45 +0800852 for (i = 0; i < nand_chip->ecc.total; i++)
Josh Wu1c7b8742012-06-29 17:47:55 +0800853 if (ecc[i] != 0xff)
854 goto normal_check;
855 /* Erased page, return OK */
856 return 0;
857
858normal_check:
Wu, Joshc9447ff2014-08-08 17:12:34 +0800859 for (i = 0; i < nand_chip->ecc.steps; i++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800860 err_nbr = 0;
861 if (pmecc_stat & 0x1) {
862 buf_pos = buf + i * host->pmecc_sector_size;
863
864 pmecc_gen_syndrome(mtd, i);
865 pmecc_substitute(mtd);
866 pmecc_get_sigma(mtd);
867
868 err_nbr = pmecc_err_location(mtd);
869 if (err_nbr == -1) {
870 dev_err(host->dev, "PMECC: Too many errors\n");
871 mtd->ecc_stats.failed++;
872 return -EIO;
873 } else {
874 pmecc_correct_data(mtd, buf_pos, ecc, i,
Wu, Josh022a4782014-08-08 17:12:35 +0800875 nand_chip->ecc.bytes, err_nbr);
Josh Wu1c7b8742012-06-29 17:47:55 +0800876 mtd->ecc_stats.corrected += err_nbr;
Josh Wuc0c70d92012-11-27 18:50:31 +0800877 total_err += err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800878 }
879 }
880 pmecc_stat >>= 1;
881 }
882
Josh Wuc0c70d92012-11-27 18:50:31 +0800883 return total_err;
Josh Wu1c7b8742012-06-29 17:47:55 +0800884}
885
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800886static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
887{
888 u32 val;
889
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800890 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
891 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
892 return;
893 }
894
Josh Wu1fad0e82013-08-07 17:58:11 +0800895 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
896 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
897 val = pmecc_readl_relaxed(host->ecc, CFG);
898
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800899 if (ecc_op == NAND_ECC_READ)
900 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
901 | PMECC_CFG_AUTO_ENABLE);
902 else
903 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
904 & ~PMECC_CFG_AUTO_ENABLE);
905
906 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
907 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
908}
909
Josh Wu1c7b8742012-06-29 17:47:55 +0800910static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
911 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
912{
913 struct atmel_nand_host *host = chip->priv;
Bo Shenb3857662014-06-12 15:58:45 +0800914 int eccsize = chip->ecc.size * chip->ecc.steps;
Josh Wu1c7b8742012-06-29 17:47:55 +0800915 uint8_t *oob = chip->oob_poi;
916 uint32_t *eccpos = chip->ecc.layout->eccpos;
917 uint32_t stat;
918 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800919 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800920
Josh Wu1ae9c092013-08-05 19:14:36 +0800921 if (!host->nfc || !host->nfc->use_nfc_sram)
922 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800923
924 chip->read_buf(mtd, buf, eccsize);
925 chip->read_buf(mtd, oob, mtd->oobsize);
926
927 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
928 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
929 if (unlikely(time_after(jiffies, end_time))) {
930 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
931 return -EIO;
932 }
933 cpu_relax();
934 }
935
936 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800937 if (stat != 0) {
938 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
939 if (bitflips < 0)
940 /* uncorrectable errors */
941 return 0;
942 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800943
Josh Wuc0c70d92012-11-27 18:50:31 +0800944 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800945}
946
947static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
948 struct nand_chip *chip, const uint8_t *buf, int oob_required)
949{
950 struct atmel_nand_host *host = chip->priv;
951 uint32_t *eccpos = chip->ecc.layout->eccpos;
952 int i, j;
953 unsigned long end_time;
954
Josh Wu6054d4d2013-08-05 19:14:37 +0800955 if (!host->nfc || !host->nfc->write_by_sram) {
956 pmecc_enable(host, NAND_ECC_WRITE);
957 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
958 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800959
960 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
961 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
962 if (unlikely(time_after(jiffies, end_time))) {
963 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
964 return -EIO;
965 }
966 cpu_relax();
967 }
968
Wu, Joshc9447ff2014-08-08 17:12:34 +0800969 for (i = 0; i < chip->ecc.steps; i++) {
Wu, Josh022a4782014-08-08 17:12:35 +0800970 for (j = 0; j < chip->ecc.bytes; j++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800971 int pos;
972
Wu, Josh022a4782014-08-08 17:12:35 +0800973 pos = i * chip->ecc.bytes + j;
Josh Wu1c7b8742012-06-29 17:47:55 +0800974 chip->oob_poi[eccpos[pos]] =
975 pmecc_readb_ecc_relaxed(host->ecc, i, j);
976 }
977 }
978 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
979
980 return 0;
981}
982
983static void atmel_pmecc_core_init(struct mtd_info *mtd)
984{
985 struct nand_chip *nand_chip = mtd->priv;
986 struct atmel_nand_host *host = nand_chip->priv;
987 uint32_t val = 0;
988 struct nand_ecclayout *ecc_layout;
989
990 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
991 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
992
993 switch (host->pmecc_corr_cap) {
994 case 2:
995 val = PMECC_CFG_BCH_ERR2;
996 break;
997 case 4:
998 val = PMECC_CFG_BCH_ERR4;
999 break;
1000 case 8:
1001 val = PMECC_CFG_BCH_ERR8;
1002 break;
1003 case 12:
1004 val = PMECC_CFG_BCH_ERR12;
1005 break;
1006 case 24:
1007 val = PMECC_CFG_BCH_ERR24;
1008 break;
1009 }
1010
1011 if (host->pmecc_sector_size == 512)
1012 val |= PMECC_CFG_SECTOR512;
1013 else if (host->pmecc_sector_size == 1024)
1014 val |= PMECC_CFG_SECTOR1024;
1015
Wu, Joshc9447ff2014-08-08 17:12:34 +08001016 switch (nand_chip->ecc.steps) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001017 case 1:
1018 val |= PMECC_CFG_PAGE_1SECTOR;
1019 break;
1020 case 2:
1021 val |= PMECC_CFG_PAGE_2SECTORS;
1022 break;
1023 case 4:
1024 val |= PMECC_CFG_PAGE_4SECTORS;
1025 break;
1026 case 8:
1027 val |= PMECC_CFG_PAGE_8SECTORS;
1028 break;
1029 }
1030
1031 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
1032 | PMECC_CFG_AUTO_DISABLE);
1033 pmecc_writel(host->ecc, CFG, val);
1034
1035 ecc_layout = nand_chip->ecc.layout;
1036 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
1037 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
1038 pmecc_writel(host->ecc, EADDR,
1039 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
1040 /* See datasheet about PMECC Clock Control Register */
1041 pmecc_writel(host->ecc, CLK, 2);
1042 pmecc_writel(host->ecc, IDR, 0xff);
1043 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
1044}
1045
Josh Wu84cfbbb2013-01-23 20:47:12 +08001046/*
Josh Wu2a3d9332013-09-18 13:58:48 +08001047 * Get minimum ecc requirements from NAND.
Josh Wu84cfbbb2013-01-23 20:47:12 +08001048 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
Josh Wu2a3d9332013-09-18 13:58:48 +08001049 * will set them according to minimum ecc requirement. Otherwise, use the
Josh Wu84cfbbb2013-01-23 20:47:12 +08001050 * value in DTS file.
1051 * return 0 if success. otherwise return error code.
1052 */
1053static int pmecc_choose_ecc(struct atmel_nand_host *host,
1054 int *cap, int *sector_size)
1055{
Josh Wu2a3d9332013-09-18 13:58:48 +08001056 /* Get minimum ECC requirements */
1057 if (host->nand_chip.ecc_strength_ds) {
1058 *cap = host->nand_chip.ecc_strength_ds;
1059 *sector_size = host->nand_chip.ecc_step_ds;
1060 dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n",
Josh Wu84cfbbb2013-01-23 20:47:12 +08001061 *cap, *sector_size);
Josh Wu84cfbbb2013-01-23 20:47:12 +08001062 } else {
Josh Wu84cfbbb2013-01-23 20:47:12 +08001063 *cap = 2;
1064 *sector_size = 512;
Josh Wu2a3d9332013-09-18 13:58:48 +08001065 dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
Josh Wu84cfbbb2013-01-23 20:47:12 +08001066 }
1067
Josh Wu2a3d9332013-09-18 13:58:48 +08001068 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
Josh Wu84cfbbb2013-01-23 20:47:12 +08001069 if (host->pmecc_corr_cap == 0) {
1070 /* use the most fitable ecc bits (the near bigger one ) */
1071 if (*cap <= 2)
1072 host->pmecc_corr_cap = 2;
1073 else if (*cap <= 4)
1074 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +08001075 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001076 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +08001077 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001078 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +08001079 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001080 host->pmecc_corr_cap = 24;
1081 else
1082 return -EINVAL;
1083 }
1084 if (host->pmecc_sector_size == 0) {
1085 /* use the most fitable sector size (the near smaller one ) */
1086 if (*sector_size >= 1024)
1087 host->pmecc_sector_size = 1024;
1088 else if (*sector_size >= 512)
1089 host->pmecc_sector_size = 512;
1090 else
1091 return -EINVAL;
1092 }
1093 return 0;
1094}
1095
Josh Wuabb1cd02014-10-11 18:01:50 +08001096static inline int deg(unsigned int poly)
1097{
1098 /* polynomial degree is the most-significant bit index */
1099 return fls(poly) - 1;
1100}
1101
1102static int build_gf_tables(int mm, unsigned int poly,
1103 int16_t *index_of, int16_t *alpha_to)
1104{
1105 unsigned int i, x = 1;
1106 const unsigned int k = 1 << deg(poly);
1107 unsigned int nn = (1 << mm) - 1;
1108
1109 /* primitive polynomial must be of degree m */
1110 if (k != (1u << mm))
1111 return -EINVAL;
1112
1113 for (i = 0; i < nn; i++) {
1114 alpha_to[i] = x;
1115 index_of[x] = i;
1116 if (i && (x == 1))
1117 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
1118 return -EINVAL;
1119 x <<= 1;
1120 if (x & k)
1121 x ^= poly;
1122 }
1123 alpha_to[nn] = 1;
1124 index_of[0] = 0;
1125
1126 return 0;
1127}
1128
1129static uint16_t *create_lookup_table(struct device *dev, int sector_size)
1130{
1131 int degree = (sector_size == 512) ?
1132 PMECC_GF_DIMENSION_13 :
1133 PMECC_GF_DIMENSION_14;
1134 unsigned int poly = (sector_size == 512) ?
1135 PMECC_GF_13_PRIMITIVE_POLY :
1136 PMECC_GF_14_PRIMITIVE_POLY;
1137 int table_size = (sector_size == 512) ?
1138 PMECC_LOOKUP_TABLE_SIZE_512 :
1139 PMECC_LOOKUP_TABLE_SIZE_1024;
1140
1141 int16_t *addr = devm_kzalloc(dev, 2 * table_size * sizeof(uint16_t),
1142 GFP_KERNEL);
1143 if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
1144 return NULL;
1145
1146 return addr;
1147}
1148
Johan Hovold2c2b9282013-09-23 16:27:28 +02001149static int atmel_pmecc_nand_init_params(struct platform_device *pdev,
Josh Wu1c7b8742012-06-29 17:47:55 +08001150 struct atmel_nand_host *host)
1151{
1152 struct mtd_info *mtd = &host->mtd;
1153 struct nand_chip *nand_chip = &host->nand_chip;
1154 struct resource *regs, *regs_pmerr, *regs_rom;
Josh Wuabb1cd02014-10-11 18:01:50 +08001155 uint16_t *galois_table;
Josh Wu1c7b8742012-06-29 17:47:55 +08001156 int cap, sector_size, err_no;
1157
Josh Wu84cfbbb2013-01-23 20:47:12 +08001158 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
1159 if (err_no) {
1160 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1161 return err_no;
1162 }
1163
Richard Genoudf666d642013-07-30 17:17:29 +02001164 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001165 sector_size != host->pmecc_sector_size)
1166 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001167
Josh Wu1c7b8742012-06-29 17:47:55 +08001168 cap = host->pmecc_corr_cap;
1169 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001170 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1171 host->pmecc_lookup_table_offset_512 :
1172 host->pmecc_lookup_table_offset_1024;
1173
Josh Wu1c7b8742012-06-29 17:47:55 +08001174 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1175 cap, sector_size);
1176
1177 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1178 if (!regs) {
1179 dev_warn(host->dev,
1180 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1181 nand_chip->ecc.mode = NAND_ECC_SOFT;
1182 return 0;
1183 }
1184
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001185 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1186 if (IS_ERR(host->ecc)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001187 err_no = PTR_ERR(host->ecc);
1188 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001189 }
1190
1191 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001192 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1193 if (IS_ERR(host->pmerrloc_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001194 err_no = PTR_ERR(host->pmerrloc_base);
1195 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001196 }
1197
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001198 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1199 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, regs_rom);
1200 if (IS_ERR(host->pmecc_rom_base)) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001201 if (!host->has_no_lookup_table)
1202 /* Don't display the information again */
1203 dev_err(host->dev, "Can not get I/O resource for ROM, will build a lookup table in runtime!\n");
1204
1205 host->has_no_lookup_table = true;
1206 }
1207
1208 if (host->has_no_lookup_table) {
1209 /* Build the look-up table in runtime */
1210 galois_table = create_lookup_table(host->dev, sector_size);
1211 if (!galois_table) {
1212 dev_err(host->dev, "Failed to build a lookup table in runtime!\n");
1213 err_no = -EINVAL;
1214 goto err;
1215 }
1216
1217 host->pmecc_rom_base = (void __iomem *)galois_table;
1218 host->pmecc_lookup_table_offset = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +08001219 }
1220
Bo Shenb3857662014-06-12 15:58:45 +08001221 nand_chip->ecc.size = sector_size;
Josh Wu1c7b8742012-06-29 17:47:55 +08001222
1223 /* set ECC page size and oob layout */
1224 switch (mtd->writesize) {
Wu, Josha3557102014-07-22 17:24:18 +08001225 case 512:
1226 case 1024:
Josh Wu1c7b8742012-06-29 17:47:55 +08001227 case 2048:
Wu, Josha3557102014-07-22 17:24:18 +08001228 case 4096:
1229 case 8192:
1230 if (sector_size > mtd->writesize) {
1231 dev_err(host->dev, "pmecc sector size is bigger than the page size!\n");
1232 err_no = -EINVAL;
1233 goto err;
1234 }
1235
Josh Wu2fa831f2013-08-19 18:05:44 +08001236 host->pmecc_degree = (sector_size == 512) ?
1237 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Josh Wu1c7b8742012-06-29 17:47:55 +08001238 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +08001239 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1240 host->pmecc_index_of = host->pmecc_rom_base +
1241 host->pmecc_lookup_table_offset;
1242
Josh Wu1c7b8742012-06-29 17:47:55 +08001243 nand_chip->ecc.strength = cap;
Wu, Josh022a4782014-08-08 17:12:35 +08001244 nand_chip->ecc.bytes = pmecc_get_ecc_bytes(cap, sector_size);
Wu, Joshc9447ff2014-08-08 17:12:34 +08001245 nand_chip->ecc.steps = mtd->writesize / sector_size;
1246 nand_chip->ecc.total = nand_chip->ecc.bytes *
1247 nand_chip->ecc.steps;
Bo Shenb3857662014-06-12 15:58:45 +08001248 if (nand_chip->ecc.total > mtd->oobsize - 2) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001249 dev_err(host->dev, "No room for ECC bytes\n");
1250 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001251 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001252 }
1253 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1254 mtd->oobsize,
Bo Shenb3857662014-06-12 15:58:45 +08001255 nand_chip->ecc.total);
1256
Josh Wu1c7b8742012-06-29 17:47:55 +08001257 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1258 break;
Wu, Josha3557102014-07-22 17:24:18 +08001259 default:
Josh Wu1c7b8742012-06-29 17:47:55 +08001260 dev_warn(host->dev,
1261 "Unsupported page size for PMECC, use Software ECC\n");
Josh Wu1c7b8742012-06-29 17:47:55 +08001262 /* page size not handled by HW ECC */
1263 /* switching back to soft ECC */
1264 nand_chip->ecc.mode = NAND_ECC_SOFT;
1265 return 0;
1266 }
1267
1268 /* Allocate data for PMECC computation */
1269 err_no = pmecc_data_alloc(host);
1270 if (err_no) {
1271 dev_err(host->dev,
1272 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001273 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001274 }
1275
Herve Codina90445ff2014-03-03 12:15:29 +01001276 nand_chip->options |= NAND_NO_SUBPAGE_WRITE;
Josh Wu1c7b8742012-06-29 17:47:55 +08001277 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1278 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1279
1280 atmel_pmecc_core_init(mtd);
1281
1282 return 0;
1283
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001284err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001285 return err_no;
1286}
1287
1288/*
Richard Genoud77f54922008-04-23 19:51:14 +02001289 * Calculate HW ECC
1290 *
1291 * function called after a write
1292 *
1293 * mtd: MTD block structure
1294 * dat: raw data (unused)
1295 * ecc_code: buffer for ECC
1296 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001297static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001298 const u_char *dat, unsigned char *ecc_code)
1299{
1300 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001301 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001302 unsigned int ecc_value;
1303
1304 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001305 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001306
Richard Genoud3fc23892008-10-12 08:42:28 +02001307 ecc_code[0] = ecc_value & 0xFF;
1308 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001309
1310 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001311 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001312
Richard Genoud3fc23892008-10-12 08:42:28 +02001313 ecc_code[2] = ecc_value & 0xFF;
1314 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001315
1316 return 0;
1317}
1318
1319/*
1320 * HW ECC read page function
1321 *
1322 * mtd: mtd info structure
1323 * chip: nand chip info structure
1324 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001325 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001326 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001327static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1328 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001329{
1330 int eccsize = chip->ecc.size;
1331 int eccbytes = chip->ecc.bytes;
1332 uint32_t *eccpos = chip->ecc.layout->eccpos;
1333 uint8_t *p = buf;
1334 uint8_t *oob = chip->oob_poi;
1335 uint8_t *ecc_pos;
1336 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001337 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001338
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001339 /*
1340 * Errata: ALE is incorrectly wired up to the ECC controller
1341 * on the AP7000, so it will include the address cycles in the
1342 * ECC calculation.
1343 *
1344 * Workaround: Reset the parity registers before reading the
1345 * actual data.
1346 */
Josh Wu71b94e22013-05-09 15:34:54 +08001347 struct atmel_nand_host *host = chip->priv;
1348 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001349 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001350
Richard Genoud77f54922008-04-23 19:51:14 +02001351 /* read the page */
1352 chip->read_buf(mtd, p, eccsize);
1353
1354 /* move to ECC position if needed */
1355 if (eccpos[0] != 0) {
1356 /* This only works on large pages
1357 * because the ECC controller waits for
1358 * NAND_CMD_RNDOUTSTART after the
1359 * NAND_CMD_RNDOUT.
1360 * anyway, for small pages, the eccpos[0] == 0
1361 */
1362 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1363 mtd->writesize + eccpos[0], -1);
1364 }
1365
1366 /* the ECC controller needs to read the ECC just after the data */
1367 ecc_pos = oob + eccpos[0];
1368 chip->read_buf(mtd, ecc_pos, eccbytes);
1369
1370 /* check if there's an error */
1371 stat = chip->ecc.correct(mtd, p, oob, NULL);
1372
Mike Dunn3f91e942012-04-25 12:06:09 -07001373 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001374 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001375 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001376 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001377 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1378 }
Richard Genoud77f54922008-04-23 19:51:14 +02001379
1380 /* get back to oob start (end of page) */
1381 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1382
1383 /* read the oob */
1384 chip->read_buf(mtd, oob, mtd->oobsize);
1385
Mike Dunn3f91e942012-04-25 12:06:09 -07001386 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001387}
1388
1389/*
1390 * HW ECC Correction
1391 *
1392 * function called after a read
1393 *
1394 * mtd: MTD block structure
1395 * dat: raw data read from the chip
1396 * read_ecc: ECC from the chip (unused)
1397 * isnull: unused
1398 *
1399 * Detect and correct a 1 bit error for a page
1400 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001401static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001402 u_char *read_ecc, u_char *isnull)
1403{
1404 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001405 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001406 unsigned int ecc_status;
1407 unsigned int ecc_word, ecc_bit;
1408
1409 /* get the status from the Status Register */
1410 ecc_status = ecc_readl(host->ecc, SR);
1411
1412 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001413 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001414 return 0;
1415
1416 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001417 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001418 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001419 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001420 ecc_word >>= 4;
1421
1422 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001423 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001424 /* check if it is a freshly erased block
1425 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001426 if ((ecc_bit == ATMEL_ECC_BITADDR)
1427 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001428 /* the block has just been erased, return OK */
1429 return 0;
1430 }
1431 /* it doesn't seems to be a freshly
1432 * erased block.
1433 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001434 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001435 " Unable to correct.\n");
1436 return -EIO;
1437 }
1438
1439 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001440 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001441 /* there's nothing much to do here.
1442 * the bit error is on the ECC itself.
1443 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001444 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001445 " Nothing to correct\n");
1446 return 0;
1447 }
1448
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001449 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001450 " (word offset in the page :"
1451 " 0x%x bit offset : 0x%x)\n",
1452 ecc_word, ecc_bit);
1453 /* correct the error */
1454 if (nand_chip->options & NAND_BUSWIDTH_16) {
1455 /* 16 bits words */
1456 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1457 } else {
1458 /* 8 bits words */
1459 dat[ecc_word] ^= (1 << ecc_bit);
1460 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001461 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001462 return 1;
1463}
1464
1465/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001466 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001467 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001468static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1469{
Josh Wu71b94e22013-05-09 15:34:54 +08001470 struct nand_chip *nand_chip = mtd->priv;
1471 struct atmel_nand_host *host = nand_chip->priv;
1472
1473 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001474 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001475}
Richard Genoud77f54922008-04-23 19:51:14 +02001476
Bill Pemberton06f25512012-11-19 13:23:07 -05001477static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001478 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001479{
Josh Wuc0cf7872013-01-23 20:47:08 +08001480 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001481 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001482 int ecc_mode;
1483 struct atmel_nand_data *board = &host->board;
Josh Wue9d8da82013-09-18 11:31:19 +08001484 enum of_gpio_flags flags = 0;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001485
1486 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1487 if (val >= 32) {
1488 dev_err(host->dev, "invalid addr-offset %u\n", val);
1489 return -EINVAL;
1490 }
1491 board->ale = val;
1492 }
1493
1494 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1495 if (val >= 32) {
1496 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1497 return -EINVAL;
1498 }
1499 board->cle = val;
1500 }
1501
1502 ecc_mode = of_get_nand_ecc_mode(np);
1503
1504 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1505
1506 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1507
Josh Wu1b719262013-05-09 15:34:55 +08001508 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1509
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001510 if (of_get_nand_bus_width(np) == 16)
1511 board->bus_width_16 = 1;
1512
1513 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1514 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1515
1516 board->enable_pin = of_get_gpio(np, 1);
1517 board->det_pin = of_get_gpio(np, 2);
1518
Josh Wua41b51a2012-06-29 17:47:54 +08001519 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1520
Josh Wu7dc37de2013-08-05 19:14:35 +08001521 /* load the nfc driver if there is */
1522 of_platform_populate(np, NULL, NULL, host->dev);
1523
Josh Wua41b51a2012-06-29 17:47:54 +08001524 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1525 return 0; /* Not using PMECC */
1526
1527 /* use PMECC, get correction capability, sector size and lookup
1528 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001529 * If correction bits and sector size are not specified, then find
1530 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001531 */
Josh Wue66b4312013-01-23 20:47:11 +08001532 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1533 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1534 (val != 24)) {
1535 dev_err(host->dev,
1536 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1537 val);
1538 return -EINVAL;
1539 }
1540 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001541 }
Josh Wua41b51a2012-06-29 17:47:54 +08001542
Josh Wue66b4312013-01-23 20:47:11 +08001543 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1544 if ((val != 512) && (val != 1024)) {
1545 dev_err(host->dev,
1546 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1547 val);
1548 return -EINVAL;
1549 }
1550 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001551 }
Josh Wua41b51a2012-06-29 17:47:54 +08001552
1553 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1554 offset, 2) != 0) {
Josh Wuabb1cd02014-10-11 18:01:50 +08001555 dev_err(host->dev, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n");
1556 host->has_no_lookup_table = true;
1557 /* Will build a lookup table and initialize the offset later */
1558 return 0;
Josh Wua41b51a2012-06-29 17:47:54 +08001559 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001560 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001561 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1562 return -EINVAL;
1563 }
Josh Wue66b4312013-01-23 20:47:11 +08001564 host->pmecc_lookup_table_offset_512 = offset[0];
1565 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001566
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001567 return 0;
1568}
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001569
Johan Hovold2c2b9282013-09-23 16:27:28 +02001570static int atmel_hw_nand_init_params(struct platform_device *pdev,
Josh Wu3dfe41a2012-06-25 18:07:43 +08001571 struct atmel_nand_host *host)
1572{
1573 struct mtd_info *mtd = &host->mtd;
1574 struct nand_chip *nand_chip = &host->nand_chip;
1575 struct resource *regs;
1576
1577 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1578 if (!regs) {
1579 dev_err(host->dev,
1580 "Can't get I/O resource regs, use software ECC\n");
1581 nand_chip->ecc.mode = NAND_ECC_SOFT;
1582 return 0;
1583 }
1584
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001585 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun8fb7b932014-07-28 21:19:55 +08001586 if (IS_ERR(host->ecc))
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001587 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001588
1589 /* ECC is calculated for the whole page (1 step) */
1590 nand_chip->ecc.size = mtd->writesize;
1591
1592 /* set ECC page size and oob layout */
1593 switch (mtd->writesize) {
1594 case 512:
1595 nand_chip->ecc.layout = &atmel_oobinfo_small;
1596 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1597 break;
1598 case 1024:
1599 nand_chip->ecc.layout = &atmel_oobinfo_large;
1600 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1601 break;
1602 case 2048:
1603 nand_chip->ecc.layout = &atmel_oobinfo_large;
1604 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1605 break;
1606 case 4096:
1607 nand_chip->ecc.layout = &atmel_oobinfo_large;
1608 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1609 break;
1610 default:
1611 /* page size not handled by HW ECC */
1612 /* switching back to soft ECC */
1613 nand_chip->ecc.mode = NAND_ECC_SOFT;
1614 return 0;
1615 }
1616
1617 /* set up for HW ECC */
1618 nand_chip->ecc.calculate = atmel_nand_calculate;
1619 nand_chip->ecc.correct = atmel_nand_correct;
1620 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1621 nand_chip->ecc.read_page = atmel_nand_read_page;
1622 nand_chip->ecc.bytes = 4;
1623 nand_chip->ecc.strength = 1;
1624
1625 return 0;
1626}
1627
Wu, Josh50e04e22014-06-10 17:50:09 +08001628static inline u32 nfc_read_status(struct atmel_nand_host *host)
1629{
1630 u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
1631 u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
1632
1633 if (unlikely(nfc_status & err_flags)) {
1634 if (nfc_status & NFC_SR_DTOE)
1635 dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
1636 else if (nfc_status & NFC_SR_UNDEF)
1637 dev_err(host->dev, "NFC: Access Undefined Area Error\n");
1638 else if (nfc_status & NFC_SR_AWB)
1639 dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
1640 else if (nfc_status & NFC_SR_ASE)
1641 dev_err(host->dev, "NFC: Access memory Size Error\n");
1642 }
1643
1644 return nfc_status;
1645}
1646
Josh Wu7dc37de2013-08-05 19:14:35 +08001647/* SMC interrupt service routine */
1648static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
1649{
1650 struct atmel_nand_host *host = dev_id;
1651 u32 status, mask, pending;
Josh Wue4e06932014-06-10 17:50:11 +08001652 irqreturn_t ret = IRQ_NONE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001653
Wu, Josh50e04e22014-06-10 17:50:09 +08001654 status = nfc_read_status(host);
Josh Wu7dc37de2013-08-05 19:14:35 +08001655 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1656 pending = status & mask;
1657
1658 if (pending & NFC_SR_XFR_DONE) {
Josh Wue4e06932014-06-10 17:50:11 +08001659 complete(&host->nfc->comp_xfer_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001660 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001661 ret = IRQ_HANDLED;
1662 }
1663 if (pending & NFC_SR_RB_EDGE) {
1664 complete(&host->nfc->comp_ready);
Josh Wu7dc37de2013-08-05 19:14:35 +08001665 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_RB_EDGE);
Josh Wue4e06932014-06-10 17:50:11 +08001666 ret = IRQ_HANDLED;
1667 }
1668 if (pending & NFC_SR_CMD_DONE) {
1669 complete(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001670 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001671 ret = IRQ_HANDLED;
Josh Wu7dc37de2013-08-05 19:14:35 +08001672 }
1673
1674 return ret;
1675}
1676
1677/* NFC(Nand Flash Controller) related functions */
Josh Wue4e06932014-06-10 17:50:11 +08001678static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag)
Josh Wu7dc37de2013-08-05 19:14:35 +08001679{
Josh Wue4e06932014-06-10 17:50:11 +08001680 if (flag & NFC_SR_XFR_DONE)
1681 init_completion(&host->nfc->comp_xfer_done);
1682
1683 if (flag & NFC_SR_RB_EDGE)
1684 init_completion(&host->nfc->comp_ready);
1685
1686 if (flag & NFC_SR_CMD_DONE)
1687 init_completion(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001688
1689 /* Enable interrupt that need to wait for */
1690 nfc_writel(host->nfc->hsmc_regs, IER, flag);
Josh Wue4e06932014-06-10 17:50:11 +08001691}
Josh Wu7dc37de2013-08-05 19:14:35 +08001692
Josh Wue4e06932014-06-10 17:50:11 +08001693static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
1694{
1695 int i, index = 0;
1696 struct completion *comp[3]; /* Support 3 interrupt completion */
Josh Wu7dc37de2013-08-05 19:14:35 +08001697
Josh Wue4e06932014-06-10 17:50:11 +08001698 if (flag & NFC_SR_XFR_DONE)
1699 comp[index++] = &host->nfc->comp_xfer_done;
1700
1701 if (flag & NFC_SR_RB_EDGE)
1702 comp[index++] = &host->nfc->comp_ready;
1703
1704 if (flag & NFC_SR_CMD_DONE)
1705 comp[index++] = &host->nfc->comp_cmd_done;
1706
1707 if (index == 0) {
1708 dev_err(host->dev, "Unkown interrupt flag: 0x%08x\n", flag);
1709 return -EINVAL;
1710 }
1711
1712 for (i = 0; i < index; i++) {
1713 if (wait_for_completion_timeout(comp[i],
1714 msecs_to_jiffies(NFC_TIME_OUT_MS)))
1715 continue; /* wait for next completion */
1716 else
1717 goto err_timeout;
1718 }
1719
1720 return 0;
1721
1722err_timeout:
Josh Wu7dc37de2013-08-05 19:14:35 +08001723 dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
Josh Wue4e06932014-06-10 17:50:11 +08001724 /* Disable the interrupt as it is not handled by interrupt handler */
1725 nfc_writel(host->nfc->hsmc_regs, IDR, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001726 return -ETIMEDOUT;
1727}
1728
1729static int nfc_send_command(struct atmel_nand_host *host,
1730 unsigned int cmd, unsigned int addr, unsigned char cycle0)
1731{
1732 unsigned long timeout;
Josh Wue4e06932014-06-10 17:50:11 +08001733 u32 flag = NFC_SR_CMD_DONE;
1734 flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0;
1735
Josh Wu7dc37de2013-08-05 19:14:35 +08001736 dev_dbg(host->dev,
1737 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1738 cmd, addr, cycle0);
1739
1740 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1741 while (nfc_cmd_readl(NFCADDR_CMD_NFCBUSY, host->nfc->base_cmd_regs)
1742 & NFCADDR_CMD_NFCBUSY) {
1743 if (time_after(jiffies, timeout)) {
1744 dev_err(host->dev,
1745 "Time out to wait CMD_NFCBUSY ready!\n");
1746 return -ETIMEDOUT;
1747 }
1748 }
Josh Wue4e06932014-06-10 17:50:11 +08001749
1750 nfc_prepare_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001751 nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
1752 nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
Josh Wue4e06932014-06-10 17:50:11 +08001753 return nfc_wait_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001754}
1755
1756static int nfc_device_ready(struct mtd_info *mtd)
1757{
Wu, Josh72a78e32014-06-10 17:50:10 +08001758 u32 status, mask;
Josh Wu7dc37de2013-08-05 19:14:35 +08001759 struct nand_chip *nand_chip = mtd->priv;
1760 struct atmel_nand_host *host = nand_chip->priv;
Wu, Josh72a78e32014-06-10 17:50:10 +08001761
1762 status = nfc_read_status(host);
1763 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1764
1765 /* The mask should be 0. If not we may lost interrupts */
1766 if (unlikely(mask & status))
1767 dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n",
1768 mask & status);
1769
1770 return status & NFC_SR_RB_EDGE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001771}
1772
1773static void nfc_select_chip(struct mtd_info *mtd, int chip)
1774{
1775 struct nand_chip *nand_chip = mtd->priv;
1776 struct atmel_nand_host *host = nand_chip->priv;
1777
1778 if (chip == -1)
1779 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
1780 else
1781 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
1782}
1783
Brian Norris3dad2342014-01-29 14:08:12 -08001784static int nfc_make_addr(struct mtd_info *mtd, int command, int column,
1785 int page_addr, unsigned int *addr1234, unsigned int *cycle0)
Josh Wu7dc37de2013-08-05 19:14:35 +08001786{
1787 struct nand_chip *chip = mtd->priv;
1788
1789 int acycle = 0;
1790 unsigned char addr_bytes[8];
1791 int index = 0, bit_shift;
1792
1793 BUG_ON(addr1234 == NULL || cycle0 == NULL);
1794
1795 *cycle0 = 0;
1796 *addr1234 = 0;
1797
1798 if (column != -1) {
Brian Norris3dad2342014-01-29 14:08:12 -08001799 if (chip->options & NAND_BUSWIDTH_16 &&
1800 !nand_opcode_8bits(command))
Josh Wu7dc37de2013-08-05 19:14:35 +08001801 column >>= 1;
1802 addr_bytes[acycle++] = column & 0xff;
1803 if (mtd->writesize > 512)
1804 addr_bytes[acycle++] = (column >> 8) & 0xff;
1805 }
1806
1807 if (page_addr != -1) {
1808 addr_bytes[acycle++] = page_addr & 0xff;
1809 addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
1810 if (chip->chipsize > (128 << 20))
1811 addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
1812 }
1813
1814 if (acycle > 4)
1815 *cycle0 = addr_bytes[index++];
1816
1817 for (bit_shift = 0; index < acycle; bit_shift += 8)
1818 *addr1234 += addr_bytes[index++] << bit_shift;
1819
1820 /* return acycle in cmd register */
1821 return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
1822}
1823
1824static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
1825 int column, int page_addr)
1826{
1827 struct nand_chip *chip = mtd->priv;
1828 struct atmel_nand_host *host = chip->priv;
1829 unsigned long timeout;
1830 unsigned int nfc_addr_cmd = 0;
1831
1832 unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1833
1834 /* Set default settings: no cmd2, no addr cycle. read from nand */
1835 unsigned int cmd2 = 0;
1836 unsigned int vcmd2 = 0;
1837 int acycle = NFCADDR_CMD_ACYCLE_NONE;
1838 int csid = NFCADDR_CMD_CSID_3;
1839 int dataen = NFCADDR_CMD_DATADIS;
1840 int nfcwr = NFCADDR_CMD_NFCRD;
1841 unsigned int addr1234 = 0;
1842 unsigned int cycle0 = 0;
1843 bool do_addr = true;
Josh Wu1ae9c092013-08-05 19:14:36 +08001844 host->nfc->data_in_sram = NULL;
Josh Wu7dc37de2013-08-05 19:14:35 +08001845
1846 dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1847 __func__, command, column, page_addr);
1848
1849 switch (command) {
1850 case NAND_CMD_RESET:
1851 nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
1852 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1853 udelay(chip->chip_delay);
1854
1855 nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
1856 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1857 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
1858 if (time_after(jiffies, timeout)) {
1859 dev_err(host->dev,
1860 "Time out to wait status ready!\n");
1861 break;
1862 }
1863 }
1864 return;
1865 case NAND_CMD_STATUS:
1866 do_addr = false;
1867 break;
1868 case NAND_CMD_PARAM:
1869 case NAND_CMD_READID:
1870 do_addr = false;
1871 acycle = NFCADDR_CMD_ACYCLE_1;
1872 if (column != -1)
1873 addr1234 = column;
1874 break;
1875 case NAND_CMD_RNDOUT:
1876 cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
1877 vcmd2 = NFCADDR_CMD_VCMD2;
1878 break;
1879 case NAND_CMD_READ0:
1880 case NAND_CMD_READOOB:
1881 if (command == NAND_CMD_READOOB) {
1882 column += mtd->writesize;
1883 command = NAND_CMD_READ0; /* only READ0 is valid */
1884 cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1885 }
Josh Wu1ae9c092013-08-05 19:14:36 +08001886 if (host->nfc->use_nfc_sram) {
1887 /* Enable Data transfer to sram */
1888 dataen = NFCADDR_CMD_DATAEN;
1889
1890 /* Need enable PMECC now, since NFC will transfer
1891 * data in bus after sending nfc read command.
1892 */
1893 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1894 pmecc_enable(host, NAND_ECC_READ);
1895 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001896
1897 cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
1898 vcmd2 = NFCADDR_CMD_VCMD2;
1899 break;
1900 /* For prgramming command, the cmd need set to write enable */
1901 case NAND_CMD_PAGEPROG:
1902 case NAND_CMD_SEQIN:
1903 case NAND_CMD_RNDIN:
1904 nfcwr = NFCADDR_CMD_NFCWR;
Josh Wu6054d4d2013-08-05 19:14:37 +08001905 if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
1906 dataen = NFCADDR_CMD_DATAEN;
Josh Wu7dc37de2013-08-05 19:14:35 +08001907 break;
1908 default:
1909 break;
1910 }
1911
1912 if (do_addr)
Brian Norris3dad2342014-01-29 14:08:12 -08001913 acycle = nfc_make_addr(mtd, command, column, page_addr,
1914 &addr1234, &cycle0);
Josh Wu7dc37de2013-08-05 19:14:35 +08001915
1916 nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
1917 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1918
1919 /*
1920 * Program and erase have their own busy handlers status, sequential
1921 * in, and deplete1 need no delay.
1922 */
1923 switch (command) {
1924 case NAND_CMD_CACHEDPROG:
1925 case NAND_CMD_PAGEPROG:
1926 case NAND_CMD_ERASE1:
1927 case NAND_CMD_ERASE2:
1928 case NAND_CMD_RNDIN:
1929 case NAND_CMD_STATUS:
1930 case NAND_CMD_RNDOUT:
1931 case NAND_CMD_SEQIN:
1932 case NAND_CMD_READID:
1933 return;
1934
1935 case NAND_CMD_READ0:
Josh Wu1ae9c092013-08-05 19:14:36 +08001936 if (dataen == NFCADDR_CMD_DATAEN) {
1937 host->nfc->data_in_sram = host->nfc->sram_bank0 +
1938 nfc_get_sram_off(host);
1939 return;
1940 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001941 /* fall through */
1942 default:
Josh Wue4e06932014-06-10 17:50:11 +08001943 nfc_prepare_interrupt(host, NFC_SR_RB_EDGE);
Josh Wu7dc37de2013-08-05 19:14:35 +08001944 nfc_wait_interrupt(host, NFC_SR_RB_EDGE);
1945 }
1946}
1947
Josh Wu6054d4d2013-08-05 19:14:37 +08001948static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1949 uint32_t offset, int data_len, const uint8_t *buf,
1950 int oob_required, int page, int cached, int raw)
1951{
1952 int cfg, len;
1953 int status = 0;
1954 struct atmel_nand_host *host = chip->priv;
Wu, Josh068b44b2014-11-07 15:26:09 +08001955 void *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
Josh Wu6054d4d2013-08-05 19:14:37 +08001956
1957 /* Subpage write is not supported */
1958 if (offset || (data_len < mtd->writesize))
1959 return -EINVAL;
1960
Josh Wu6054d4d2013-08-05 19:14:37 +08001961 len = mtd->writesize;
Josh Wu6054d4d2013-08-05 19:14:37 +08001962 /* Copy page data to sram that will write to nand via NFC */
1963 if (use_dma) {
1964 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
1965 /* Fall back to use cpu copy */
Wu, Josh068b44b2014-11-07 15:26:09 +08001966 memcpy(sram, buf, len);
Josh Wu6054d4d2013-08-05 19:14:37 +08001967 } else {
Wu, Josh068b44b2014-11-07 15:26:09 +08001968 memcpy(sram, buf, len);
Josh Wu6054d4d2013-08-05 19:14:37 +08001969 }
1970
Wu, Joshff0a2152014-08-05 18:38:52 +08001971 cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
1972 if (unlikely(raw) && oob_required) {
Wu, Josh068b44b2014-11-07 15:26:09 +08001973 memcpy(sram + len, chip->oob_poi, mtd->oobsize);
Wu, Joshff0a2152014-08-05 18:38:52 +08001974 len += mtd->oobsize;
1975 nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
1976 } else {
1977 nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
1978 }
1979
Josh Wu6054d4d2013-08-05 19:14:37 +08001980 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1981 /*
1982 * When use NFC sram, need set up PMECC before send
1983 * NAND_CMD_SEQIN command. Since when the nand command
1984 * is sent, nfc will do transfer from sram and nand.
1985 */
1986 pmecc_enable(host, NAND_ECC_WRITE);
1987
1988 host->nfc->will_write_sram = true;
1989 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1990 host->nfc->will_write_sram = false;
1991
1992 if (likely(!raw))
1993 /* Need to write ecc into oob */
1994 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
1995
1996 if (status < 0)
1997 return status;
1998
1999 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2000 status = chip->waitfunc(mtd, chip);
2001
2002 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2003 status = chip->errstat(mtd, chip, FL_WRITING, status, page);
2004
2005 if (status & NAND_STATUS_FAIL)
2006 return -EIO;
2007
2008 return 0;
2009}
2010
Josh Wu1ae9c092013-08-05 19:14:36 +08002011static int nfc_sram_init(struct mtd_info *mtd)
2012{
2013 struct nand_chip *chip = mtd->priv;
2014 struct atmel_nand_host *host = chip->priv;
2015 int res = 0;
2016
2017 /* Initialize the NFC CFG register */
2018 unsigned int cfg_nfc = 0;
2019
2020 /* set page size and oob layout */
2021 switch (mtd->writesize) {
2022 case 512:
2023 cfg_nfc = NFC_CFG_PAGESIZE_512;
2024 break;
2025 case 1024:
2026 cfg_nfc = NFC_CFG_PAGESIZE_1024;
2027 break;
2028 case 2048:
2029 cfg_nfc = NFC_CFG_PAGESIZE_2048;
2030 break;
2031 case 4096:
2032 cfg_nfc = NFC_CFG_PAGESIZE_4096;
2033 break;
2034 case 8192:
2035 cfg_nfc = NFC_CFG_PAGESIZE_8192;
2036 break;
2037 default:
2038 dev_err(host->dev, "Unsupported page size for NFC.\n");
2039 res = -ENXIO;
2040 return res;
2041 }
2042
2043 /* oob bytes size = (NFCSPARESIZE + 1) * 4
2044 * Max support spare size is 512 bytes. */
2045 cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
2046 & NFC_CFG_NFC_SPARESIZE);
2047 /* default set a max timeout */
2048 cfg_nfc |= NFC_CFG_RSPARE |
2049 NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
2050
2051 nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
2052
Josh Wu6054d4d2013-08-05 19:14:37 +08002053 host->nfc->will_write_sram = false;
Josh Wu1ae9c092013-08-05 19:14:36 +08002054 nfc_set_sram_bank(host, 0);
2055
Josh Wu6054d4d2013-08-05 19:14:37 +08002056 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
2057 if (host->nfc->write_by_sram) {
2058 if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
2059 chip->ecc.mode == NAND_ECC_NONE)
2060 chip->write_page = nfc_sram_write_page;
2061 else
2062 host->nfc->write_by_sram = false;
2063 }
Josh Wu1ae9c092013-08-05 19:14:36 +08002064
Josh Wu6054d4d2013-08-05 19:14:37 +08002065 dev_info(host->dev, "Using NFC Sram read %s\n",
2066 host->nfc->write_by_sram ? "and write" : "");
Josh Wu1ae9c092013-08-05 19:14:36 +08002067 return 0;
2068}
2069
Josh Wu7dc37de2013-08-05 19:14:35 +08002070static struct platform_driver atmel_nand_nfc_driver;
Andrew Victor42cb1402006-10-19 18:24:35 +02002071/*
2072 * Probe for the NAND device.
2073 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002074static int atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002075{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002076 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02002077 struct mtd_info *mtd;
2078 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002079 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002080 struct mtd_part_parser_data ppdata = {};
Josh Wu7dc37de2013-08-05 19:14:35 +08002081 int res, irq;
Andrew Victor42cb1402006-10-19 18:24:35 +02002082
2083 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002084 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Han9e3677a2013-12-26 12:00:16 +09002085 if (!host)
Andrew Victor42cb1402006-10-19 18:24:35 +02002086 return -ENOMEM;
Andrew Victor42cb1402006-10-19 18:24:35 +02002087
Josh Wu7dc37de2013-08-05 19:14:35 +08002088 res = platform_driver_register(&atmel_nand_nfc_driver);
2089 if (res)
2090 dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
2091
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002092 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2093 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
2094 if (IS_ERR(host->io_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002095 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002096 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02002097 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002098 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02002099
2100 mtd = &host->mtd;
2101 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002102 host->dev = &pdev->dev;
Josh Wue9d8da82013-09-18 11:31:19 +08002103 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
2104 /* Only when CONFIG_OF is enabled of_node can be parsed */
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002105 res = atmel_of_init_port(host, pdev->dev.of_node);
2106 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002107 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002108 } else {
Jingoo Han453810b2013-07-30 17:18:33 +09002109 memcpy(&host->board, dev_get_platdata(&pdev->dev),
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002110 sizeof(struct atmel_nand_data));
2111 }
Andrew Victor42cb1402006-10-19 18:24:35 +02002112
2113 nand_chip->priv = host; /* link the private data structures */
2114 mtd->priv = nand_chip;
2115 mtd->owner = THIS_MODULE;
2116
2117 /* Set address of NAND IO lines */
2118 nand_chip->IO_ADDR_R = host->io_base;
2119 nand_chip->IO_ADDR_W = host->io_base;
Ivan Kutena4265f82007-05-24 14:35:58 +03002120
Josh Wu7dc37de2013-08-05 19:14:35 +08002121 if (nand_nfc.is_initialized) {
2122 /* NFC driver is probed and initialized */
2123 host->nfc = &nand_nfc;
2124
2125 nand_chip->select_chip = nfc_select_chip;
2126 nand_chip->dev_ready = nfc_device_ready;
2127 nand_chip->cmdfunc = nfc_nand_command;
2128
2129 /* Initialize the interrupt for NFC */
2130 irq = platform_get_irq(pdev, 0);
2131 if (irq < 0) {
2132 dev_err(host->dev, "Cannot get HSMC irq!\n");
Wei Yongjunff52c672013-08-23 10:50:36 +08002133 res = irq;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002134 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002135 }
2136
Josh Wu7dc37de2013-08-05 19:14:35 +08002137 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
2138 0, "hsmc", host);
2139 if (res) {
2140 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
2141 irq);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002142 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002143 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002144 } else {
2145 res = atmel_nand_set_enable_ready_pins(mtd);
2146 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002147 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002148
Josh Wu7dc37de2013-08-05 19:14:35 +08002149 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002150 }
Ivan Kutena4265f82007-05-24 14:35:58 +03002151
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002152 nand_chip->ecc.mode = host->board.ecc_mode;
Raphaël Poggi796fe362014-07-29 15:27:27 +02002153 nand_chip->chip_delay = 40; /* 40us command delay time */
Andrew Victor42cb1402006-10-19 18:24:35 +02002154
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002155 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02002156 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08002157
2158 nand_chip->read_buf = atmel_read_buf;
2159 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02002160
Andrew Victor42cb1402006-10-19 18:24:35 +02002161 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002162 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002163
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002164 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002165 res = devm_gpio_request(&pdev->dev,
2166 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002167 if (res < 0) {
2168 dev_err(&pdev->dev,
2169 "can't request det gpio %d\n",
2170 host->board.det_pin);
2171 goto err_no_card;
2172 }
2173
2174 res = gpio_direction_input(host->board.det_pin);
2175 if (res < 0) {
2176 dev_err(&pdev->dev,
2177 "can't request input direction det gpio %d\n",
2178 host->board.det_pin);
2179 goto err_no_card;
2180 }
2181
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002182 if (gpio_get_value(host->board.det_pin)) {
Jingoo Han1295f972013-12-26 12:30:58 +09002183 dev_info(&pdev->dev, "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01002184 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002185 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02002186 }
2187 }
2188
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002189 if (host->board.on_flash_bbt || on_flash_bbt) {
Jingoo Han1295f972013-12-26 12:30:58 +09002190 dev_info(&pdev->dev, "Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -07002191 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa6972009-05-27 18:19:39 +03002192 }
2193
Josh Wu1b719262013-05-09 15:34:55 +08002194 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08002195 use_dma = 0;
2196
2197 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08002198 dma_cap_mask_t mask;
2199
2200 dma_cap_zero(mask);
2201 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02002202 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002203 if (!host->dma_chan) {
2204 dev_err(host->dev, "Failed to request DMA channel\n");
2205 use_dma = 0;
2206 }
2207 }
2208 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08002209 dev_info(host->dev, "Using %s for DMA transfers.\n",
2210 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08002211 else
2212 dev_info(host->dev, "No DMA support for NAND access.\n");
2213
Richard Genoud77f54922008-04-23 19:51:14 +02002214 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00002215 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02002216 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002217 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02002218 }
2219
Richard Genoud3fc23892008-10-12 08:42:28 +02002220 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08002221 if (host->has_pmecc)
2222 res = atmel_pmecc_nand_init_params(pdev, host);
2223 else
2224 res = atmel_hw_nand_init_params(pdev, host);
2225
Josh Wu3dfe41a2012-06-25 18:07:43 +08002226 if (res != 0)
2227 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02002228 }
2229
Josh Wu1ae9c092013-08-05 19:14:36 +08002230 /* initialize the nfc configuration register */
2231 if (host->nfc && host->nfc->use_nfc_sram) {
2232 res = nfc_sram_init(mtd);
2233 if (res) {
2234 host->nfc->use_nfc_sram = false;
2235 dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
2236 }
2237 }
2238
Richard Genoud77f54922008-04-23 19:51:14 +02002239 /* second phase scan */
2240 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02002241 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002242 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02002243 }
2244
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002245 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002246 ppdata.of_node = pdev->dev.of_node;
2247 res = mtd_device_parse_register(mtd, NULL, &ppdata,
2248 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02002249 if (!res)
2250 return res;
2251
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002252err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002253 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08002254 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08002255err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002256err_scan_ident:
2257err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002258 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002259 if (host->dma_chan)
2260 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002261err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02002262 return res;
2263}
2264
2265/*
2266 * Remove a NAND device.
2267 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002268static int atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002269{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002270 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +02002271 struct mtd_info *mtd = &host->mtd;
2272
2273 nand_release(mtd);
2274
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002275 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002276
Josh Wu1c7b8742012-06-29 17:47:55 +08002277 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
2278 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2279 pmerrloc_writel(host->pmerrloc_base, ELDIS,
2280 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08002281 }
2282
Hong Xucbc6c5e2011-01-18 14:36:05 +08002283 if (host->dma_chan)
2284 dma_release_channel(host->dma_chan);
2285
Josh Wu7dc37de2013-08-05 19:14:35 +08002286 platform_driver_unregister(&atmel_nand_nfc_driver);
2287
Andrew Victor42cb1402006-10-19 18:24:35 +02002288 return 0;
2289}
2290
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002291static const struct of_device_id atmel_nand_dt_ids[] = {
2292 { .compatible = "atmel,at91rm9200-nand" },
2293 { /* sentinel */ }
2294};
2295
2296MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002297
Josh Wu7dc37de2013-08-05 19:14:35 +08002298static int atmel_nand_nfc_probe(struct platform_device *pdev)
2299{
2300 struct atmel_nfc *nfc = &nand_nfc;
2301 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002302 int ret;
Josh Wu7dc37de2013-08-05 19:14:35 +08002303
2304 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2305 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
2306 if (IS_ERR(nfc->base_cmd_regs))
2307 return PTR_ERR(nfc->base_cmd_regs);
2308
2309 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2310 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
2311 if (IS_ERR(nfc->hsmc_regs))
2312 return PTR_ERR(nfc->hsmc_regs);
2313
2314 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2315 if (nfc_sram) {
Wu, Josh068b44b2014-11-07 15:26:09 +08002316 nfc->sram_bank0 = (void * __force)
2317 devm_ioremap_resource(&pdev->dev, nfc_sram);
Josh Wu1ae9c092013-08-05 19:14:36 +08002318 if (IS_ERR(nfc->sram_bank0)) {
Josh Wu7dc37de2013-08-05 19:14:35 +08002319 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2320 PTR_ERR(nfc->sram_bank0));
Josh Wu1ae9c092013-08-05 19:14:36 +08002321 } else {
2322 nfc->use_nfc_sram = true;
Josh Wu7dc37de2013-08-05 19:14:35 +08002323 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
Josh Wu6054d4d2013-08-05 19:14:37 +08002324
2325 if (pdev->dev.of_node)
2326 nfc->write_by_sram = of_property_read_bool(
2327 pdev->dev.of_node,
2328 "atmel,write-by-sram");
Josh Wu1ae9c092013-08-05 19:14:36 +08002329 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002330 }
2331
Wu, Josh50e04e22014-06-10 17:50:09 +08002332 nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
2333 nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */
2334
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002335 nfc->clk = devm_clk_get(&pdev->dev, NULL);
2336 if (!IS_ERR(nfc->clk)) {
2337 ret = clk_prepare_enable(nfc->clk);
2338 if (ret)
2339 return ret;
2340 } else {
2341 dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree");
2342 }
2343
Josh Wu7dc37de2013-08-05 19:14:35 +08002344 nfc->is_initialized = true;
2345 dev_info(&pdev->dev, "NFC is probed.\n");
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002346
2347 return 0;
2348}
2349
2350static int atmel_nand_nfc_remove(struct platform_device *pdev)
2351{
2352 struct atmel_nfc *nfc = &nand_nfc;
2353
2354 if (!IS_ERR(nfc->clk))
2355 clk_disable_unprepare(nfc->clk);
2356
Josh Wu7dc37de2013-08-05 19:14:35 +08002357 return 0;
2358}
2359
Josh Wu81f29b42013-09-18 11:31:20 +08002360static const struct of_device_id atmel_nand_nfc_match[] = {
Josh Wu7dc37de2013-08-05 19:14:35 +08002361 { .compatible = "atmel,sama5d3-nfc" },
2362 { /* sentinel */ }
2363};
Josh Wu81f29b42013-09-18 11:31:20 +08002364MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
Josh Wu7dc37de2013-08-05 19:14:35 +08002365
2366static struct platform_driver atmel_nand_nfc_driver = {
2367 .driver = {
2368 .name = "atmel_nand_nfc",
2369 .owner = THIS_MODULE,
2370 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2371 },
2372 .probe = atmel_nand_nfc_probe,
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002373 .remove = atmel_nand_nfc_remove,
Josh Wu7dc37de2013-08-05 19:14:35 +08002374};
2375
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002376static struct platform_driver atmel_nand_driver = {
Johan Hovold2c2b9282013-09-23 16:27:28 +02002377 .probe = atmel_nand_probe,
2378 .remove = atmel_nand_remove,
Andrew Victor42cb1402006-10-19 18:24:35 +02002379 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002380 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +02002381 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002382 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02002383 },
2384};
2385
Johan Hovold2c2b9282013-09-23 16:27:28 +02002386module_platform_driver(atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02002387
2388MODULE_LICENSE("GPL");
2389MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02002390MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002391MODULE_ALIAS("platform:atmel_nand");