blob: d1e502f8dbd0b647b6506c66d45887d7a493b09c [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;
95 void __iomem *sram_bank0;
96 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 */
108 void __iomem *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;
130 u32 pmecc_lookup_table_offset;
Josh Wue66b4312013-01-23 20:47:11 +0800131 u32 pmecc_lookup_table_offset_512;
132 u32 pmecc_lookup_table_offset_1024;
Josh Wu1c7b8742012-06-29 17:47:55 +0800133
134 int pmecc_bytes_per_sector;
135 int pmecc_sector_number;
136 int pmecc_degree; /* Degree of remainders */
137 int pmecc_cw_len; /* Length of codeword */
138
139 void __iomem *pmerrloc_base;
140 void __iomem *pmecc_rom_base;
141
142 /* lookup table for alpha_to and index_of */
143 void __iomem *pmecc_alpha_to;
144 void __iomem *pmecc_index_of;
145
146 /* data for pmecc computation */
147 int16_t *pmecc_partial_syn;
148 int16_t *pmecc_si;
149 int16_t *pmecc_smu; /* Sigma table */
150 int16_t *pmecc_lmu; /* polynomal order */
151 int *pmecc_mu;
152 int *pmecc_dmu;
153 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200154};
155
Josh Wu1c7b8742012-06-29 17:47:55 +0800156static struct nand_ecclayout atmel_pmecc_oobinfo;
157
Andrew Victor42cb1402006-10-19 18:24:35 +0200158/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900159 * Enable NAND.
160 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200161static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900162{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800163 if (gpio_is_valid(host->board.enable_pin))
164 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900165}
166
167/*
168 * Disable NAND.
169 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200170static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900171{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800172 if (gpio_is_valid(host->board.enable_pin))
173 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900174}
175
176/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200177 * Hardware specific access to control-lines
178 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200179static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200180{
181 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200182 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200183
Atsushi Nemoto81365082008-04-27 01:51:12 +0900184 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900185 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200186 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900187 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200188 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900189 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200190 if (cmd == NAND_CMD_NONE)
191 return;
192
193 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800194 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200195 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800196 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200197}
198
199/*
200 * Read the Device Ready pin.
201 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200202static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200203{
204 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200205 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200206
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800207 return gpio_get_value(host->board.rdy_pin) ^
208 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200209}
210
Josh Wu7dc37de2013-08-05 19:14:35 +0800211/* Set up for hardware ready pin and enable pin. */
212static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
213{
214 struct nand_chip *chip = mtd->priv;
215 struct atmel_nand_host *host = chip->priv;
216 int res = 0;
217
218 if (gpio_is_valid(host->board.rdy_pin)) {
219 res = devm_gpio_request(host->dev,
220 host->board.rdy_pin, "nand_rdy");
221 if (res < 0) {
222 dev_err(host->dev,
223 "can't request rdy gpio %d\n",
224 host->board.rdy_pin);
225 return res;
226 }
227
228 res = gpio_direction_input(host->board.rdy_pin);
229 if (res < 0) {
230 dev_err(host->dev,
231 "can't request input direction rdy gpio %d\n",
232 host->board.rdy_pin);
233 return res;
234 }
235
236 chip->dev_ready = atmel_nand_device_ready;
237 }
238
239 if (gpio_is_valid(host->board.enable_pin)) {
240 res = devm_gpio_request(host->dev,
241 host->board.enable_pin, "nand_enable");
242 if (res < 0) {
243 dev_err(host->dev,
244 "can't request enable gpio %d\n",
245 host->board.enable_pin);
246 return res;
247 }
248
249 res = gpio_direction_output(host->board.enable_pin, 1);
250 if (res < 0) {
251 dev_err(host->dev,
252 "can't request output direction enable gpio %d\n",
253 host->board.enable_pin);
254 return res;
255 }
256 }
257
258 return res;
259}
260
Josh Wu1ae9c092013-08-05 19:14:36 +0800261static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size)
262{
263 int i;
264 u32 *t = trg;
265 const __iomem u32 *s = src;
266
267 for (i = 0; i < (size >> 2); i++)
268 *t++ = readl_relaxed(s++);
269}
270
Josh Wu6054d4d2013-08-05 19:14:37 +0800271static void memcpy32_toio(void __iomem *trg, const void *src, int size)
272{
273 int i;
274 u32 __iomem *t = trg;
275 const u32 *s = src;
276
277 for (i = 0; i < (size >> 2); i++)
278 writel_relaxed(*s++, t++);
279}
280
Artem Bityutskiy50082312012-02-02 13:54:25 +0200281/*
282 * Minimal-overhead PIO for data access.
283 */
284static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
285{
286 struct nand_chip *nand_chip = mtd->priv;
Josh Wu1ae9c092013-08-05 19:14:36 +0800287 struct atmel_nand_host *host = nand_chip->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200288
Josh Wu1ae9c092013-08-05 19:14:36 +0800289 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
290 memcpy32_fromio(buf, host->nfc->data_in_sram, len);
291 host->nfc->data_in_sram += len;
292 } else {
293 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
294 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200295}
296
297static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
298{
299 struct nand_chip *nand_chip = mtd->priv;
Josh Wu1ae9c092013-08-05 19:14:36 +0800300 struct atmel_nand_host *host = nand_chip->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200301
Josh Wu1ae9c092013-08-05 19:14:36 +0800302 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
303 memcpy32_fromio(buf, host->nfc->data_in_sram, len);
304 host->nfc->data_in_sram += len;
305 } else {
306 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
307 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200308}
309
310static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
311{
312 struct nand_chip *nand_chip = mtd->priv;
313
314 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
315}
316
317static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
318{
319 struct nand_chip *nand_chip = mtd->priv;
320
321 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
322}
323
Hong Xucbc6c5e2011-01-18 14:36:05 +0800324static void dma_complete_func(void *completion)
325{
326 complete(completion);
327}
328
Josh Wu1ae9c092013-08-05 19:14:36 +0800329static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
330{
331 /* NFC only has two banks. Must be 0 or 1 */
332 if (bank > 1)
333 return -EINVAL;
334
335 if (bank) {
336 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
337 if (host->mtd.writesize > 2048)
338 return -EINVAL;
339 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
340 } else {
341 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
342 }
343
344 return 0;
345}
346
347static uint nfc_get_sram_off(struct atmel_nand_host *host)
348{
349 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
350 return NFC_SRAM_BANK1_OFFSET;
351 else
352 return 0;
353}
354
355static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
356{
357 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
358 return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
359 else
360 return host->nfc->sram_bank0_phys;
361}
362
Hong Xucbc6c5e2011-01-18 14:36:05 +0800363static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
364 int is_read)
365{
366 struct dma_device *dma_dev;
367 enum dma_ctrl_flags flags;
368 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
369 struct dma_async_tx_descriptor *tx = NULL;
370 dma_cookie_t cookie;
371 struct nand_chip *chip = mtd->priv;
372 struct atmel_nand_host *host = chip->priv;
373 void *p = buf;
374 int err = -EIO;
375 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Josh Wu1ae9c092013-08-05 19:14:36 +0800376 struct atmel_nfc *nfc = host->nfc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800377
Hong Xu80b4f812011-03-31 18:33:15 +0800378 if (buf >= high_memory)
379 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800380
381 dma_dev = host->dma_chan->device;
382
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200383 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800384
385 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
386 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
387 dev_err(host->dev, "Failed to dma_map_single\n");
388 goto err_buf;
389 }
390
391 if (is_read) {
Josh Wu1ae9c092013-08-05 19:14:36 +0800392 if (nfc && nfc->data_in_sram)
393 dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
394 - (nfc->sram_bank0 + nfc_get_sram_off(host)));
395 else
396 dma_src_addr = host->io_phys;
397
Hong Xucbc6c5e2011-01-18 14:36:05 +0800398 dma_dst_addr = phys_addr;
399 } else {
400 dma_src_addr = phys_addr;
Josh Wu6054d4d2013-08-05 19:14:37 +0800401
402 if (nfc && nfc->write_by_sram)
403 dma_dst_addr = nfc_sram_phys(host);
404 else
405 dma_dst_addr = host->io_phys;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800406 }
407
408 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
409 dma_src_addr, len, flags);
410 if (!tx) {
411 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
412 goto err_dma;
413 }
414
415 init_completion(&host->comp);
416 tx->callback = dma_complete_func;
417 tx->callback_param = &host->comp;
418
419 cookie = tx->tx_submit(tx);
420 if (dma_submit_error(cookie)) {
421 dev_err(host->dev, "Failed to do DMA tx_submit\n");
422 goto err_dma;
423 }
424
425 dma_async_issue_pending(host->dma_chan);
426 wait_for_completion(&host->comp);
427
Josh Wu1ae9c092013-08-05 19:14:36 +0800428 if (is_read && nfc && nfc->data_in_sram)
429 /* After read data from SRAM, need to increase the position */
430 nfc->data_in_sram += len;
431
Hong Xucbc6c5e2011-01-18 14:36:05 +0800432 err = 0;
433
434err_dma:
435 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
436err_buf:
437 if (err != 0)
Nicolas Ferre74414a942014-02-12 12:26:54 +0100438 dev_dbg(host->dev, "Fall back to CPU I/O\n");
Hong Xucbc6c5e2011-01-18 14:36:05 +0800439 return err;
440}
441
442static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
443{
444 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200445 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800446
Nicolas Ferre9d515672011-04-01 16:40:44 +0200447 if (use_dma && len > mtd->oobsize)
448 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800449 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
450 return;
451
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800452 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200453 atmel_read_buf16(mtd, buf, len);
454 else
455 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800456}
457
458static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
459{
460 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200461 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800462
Nicolas Ferre9d515672011-04-01 16:40:44 +0200463 if (use_dma && len > mtd->oobsize)
464 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800465 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
466 return;
467
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800468 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200469 atmel_write_buf16(mtd, buf, len);
470 else
471 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800472}
473
David Brownell23a346c2008-07-03 23:40:16 -0700474/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800475 * Return number of ecc bytes per sector according to sector size and
476 * correction capability
477 *
478 * Following table shows what at91 PMECC supported:
479 * Correction Capability Sector_512_bytes Sector_1024_bytes
480 * ===================== ================ =================
481 * 2-bits 4-bytes 4-bytes
482 * 4-bits 7-bytes 7-bytes
483 * 8-bits 13-bytes 14-bytes
484 * 12-bits 20-bytes 21-bytes
485 * 24-bits 39-bytes 42-bytes
486 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500487static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800488{
489 int m = 12 + sector_size / 512;
490 return (m * cap + 7) / 8;
491}
492
Bill Pemberton06f25512012-11-19 13:23:07 -0500493static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800494 int oobsize, int ecc_len)
Josh Wu1c7b8742012-06-29 17:47:55 +0800495{
496 int i;
497
498 layout->eccbytes = ecc_len;
499
500 /* ECC will occupy the last ecc_len bytes continuously */
501 for (i = 0; i < ecc_len; i++)
502 layout->eccpos[i] = oobsize - ecc_len + i;
503
504 layout->oobfree[0].offset = 2;
505 layout->oobfree[0].length =
506 oobsize - ecc_len - layout->oobfree[0].offset;
507}
508
Bill Pemberton06f25512012-11-19 13:23:07 -0500509static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800510{
511 int table_size;
512
513 table_size = host->pmecc_sector_size == 512 ?
514 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
515
516 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
517 table_size * sizeof(int16_t);
518}
519
Bill Pemberton06f25512012-11-19 13:23:07 -0500520static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800521{
522 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800523 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800524
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800525 size = (2 * cap + 1) * sizeof(int16_t);
526 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
527 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
528 host->pmecc_lmu = devm_kzalloc(host->dev,
529 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
530 host->pmecc_smu = devm_kzalloc(host->dev,
531 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800532
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800533 size = (cap + 1) * sizeof(int);
534 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
535 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
536 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800537
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800538 if (!host->pmecc_partial_syn ||
539 !host->pmecc_si ||
540 !host->pmecc_lmu ||
541 !host->pmecc_smu ||
542 !host->pmecc_mu ||
543 !host->pmecc_dmu ||
544 !host->pmecc_delta)
545 return -ENOMEM;
546
547 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800548}
549
550static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
551{
552 struct nand_chip *nand_chip = mtd->priv;
553 struct atmel_nand_host *host = nand_chip->priv;
554 int i;
555 uint32_t value;
556
557 /* Fill odd syndromes */
558 for (i = 0; i < host->pmecc_corr_cap; i++) {
559 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
560 if (i & 1)
561 value >>= 16;
562 value &= 0xffff;
563 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
564 }
565}
566
567static void pmecc_substitute(struct mtd_info *mtd)
568{
569 struct nand_chip *nand_chip = mtd->priv;
570 struct atmel_nand_host *host = nand_chip->priv;
571 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
572 int16_t __iomem *index_of = host->pmecc_index_of;
573 int16_t *partial_syn = host->pmecc_partial_syn;
574 const int cap = host->pmecc_corr_cap;
575 int16_t *si;
576 int i, j;
577
578 /* si[] is a table that holds the current syndrome value,
579 * an element of that table belongs to the field
580 */
581 si = host->pmecc_si;
582
583 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
584
585 /* Computation 2t syndromes based on S(x) */
586 /* Odd syndromes */
587 for (i = 1; i < 2 * cap; i += 2) {
588 for (j = 0; j < host->pmecc_degree; j++) {
589 if (partial_syn[i] & ((unsigned short)0x1 << j))
590 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
591 }
592 }
593 /* Even syndrome = (Odd syndrome) ** 2 */
594 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
595 if (si[j] == 0) {
596 si[i] = 0;
597 } else {
598 int16_t tmp;
599
600 tmp = readw_relaxed(index_of + si[j]);
601 tmp = (tmp * 2) % host->pmecc_cw_len;
602 si[i] = readw_relaxed(alpha_to + tmp);
603 }
604 }
605
606 return;
607}
608
609static void pmecc_get_sigma(struct mtd_info *mtd)
610{
611 struct nand_chip *nand_chip = mtd->priv;
612 struct atmel_nand_host *host = nand_chip->priv;
613
614 int16_t *lmu = host->pmecc_lmu;
615 int16_t *si = host->pmecc_si;
616 int *mu = host->pmecc_mu;
617 int *dmu = host->pmecc_dmu; /* Discrepancy */
618 int *delta = host->pmecc_delta; /* Delta order */
619 int cw_len = host->pmecc_cw_len;
620 const int16_t cap = host->pmecc_corr_cap;
621 const int num = 2 * cap + 1;
622 int16_t __iomem *index_of = host->pmecc_index_of;
623 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
624 int i, j, k;
625 uint32_t dmu_0_count, tmp;
626 int16_t *smu = host->pmecc_smu;
627
628 /* index of largest delta */
629 int ro;
630 int largest;
631 int diff;
632
633 dmu_0_count = 0;
634
635 /* First Row */
636
637 /* Mu */
638 mu[0] = -1;
639
640 memset(smu, 0, sizeof(int16_t) * num);
641 smu[0] = 1;
642
643 /* discrepancy set to 1 */
644 dmu[0] = 1;
645 /* polynom order set to 0 */
646 lmu[0] = 0;
647 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
648
649 /* Second Row */
650
651 /* Mu */
652 mu[1] = 0;
653 /* Sigma(x) set to 1 */
654 memset(&smu[num], 0, sizeof(int16_t) * num);
655 smu[num] = 1;
656
657 /* discrepancy set to S1 */
658 dmu[1] = si[1];
659
660 /* polynom order set to 0 */
661 lmu[1] = 0;
662
663 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
664
665 /* Init the Sigma(x) last row */
666 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
667
668 for (i = 1; i <= cap; i++) {
669 mu[i + 1] = i << 1;
670 /* Begin Computing Sigma (Mu+1) and L(mu) */
671 /* check if discrepancy is set to 0 */
672 if (dmu[i] == 0) {
673 dmu_0_count++;
674
675 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
676 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
677 tmp += 2;
678 else
679 tmp += 1;
680
681 if (dmu_0_count == tmp) {
682 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
683 smu[(cap + 1) * num + j] =
684 smu[i * num + j];
685
686 lmu[cap + 1] = lmu[i];
687 return;
688 }
689
690 /* copy polynom */
691 for (j = 0; j <= lmu[i] >> 1; j++)
692 smu[(i + 1) * num + j] = smu[i * num + j];
693
694 /* copy previous polynom order to the next */
695 lmu[i + 1] = lmu[i];
696 } else {
697 ro = 0;
698 largest = -1;
699 /* find largest delta with dmu != 0 */
700 for (j = 0; j < i; j++) {
701 if ((dmu[j]) && (delta[j] > largest)) {
702 largest = delta[j];
703 ro = j;
704 }
705 }
706
707 /* compute difference */
708 diff = (mu[i] - mu[ro]);
709
710 /* Compute degree of the new smu polynomial */
711 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
712 lmu[i + 1] = lmu[i];
713 else
714 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
715
716 /* Init smu[i+1] with 0 */
717 for (k = 0; k < num; k++)
718 smu[(i + 1) * num + k] = 0;
719
720 /* Compute smu[i+1] */
721 for (k = 0; k <= lmu[ro] >> 1; k++) {
722 int16_t a, b, c;
723
724 if (!(smu[ro * num + k] && dmu[i]))
725 continue;
726 a = readw_relaxed(index_of + dmu[i]);
727 b = readw_relaxed(index_of + dmu[ro]);
728 c = readw_relaxed(index_of + smu[ro * num + k]);
729 tmp = a + (cw_len - b) + c;
730 a = readw_relaxed(alpha_to + tmp % cw_len);
731 smu[(i + 1) * num + (k + diff)] = a;
732 }
733
734 for (k = 0; k <= lmu[i] >> 1; k++)
735 smu[(i + 1) * num + k] ^= smu[i * num + k];
736 }
737
738 /* End Computing Sigma (Mu+1) and L(mu) */
739 /* In either case compute delta */
740 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
741
742 /* Do not compute discrepancy for the last iteration */
743 if (i >= cap)
744 continue;
745
746 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
747 tmp = 2 * (i - 1);
748 if (k == 0) {
749 dmu[i + 1] = si[tmp + 3];
750 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
751 int16_t a, b, c;
752 a = readw_relaxed(index_of +
753 smu[(i + 1) * num + k]);
754 b = si[2 * (i - 1) + 3 - k];
755 c = readw_relaxed(index_of + b);
756 tmp = a + c;
757 tmp %= cw_len;
758 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
759 dmu[i + 1];
760 }
761 }
762 }
763
764 return;
765}
766
767static int pmecc_err_location(struct mtd_info *mtd)
768{
769 struct nand_chip *nand_chip = mtd->priv;
770 struct atmel_nand_host *host = nand_chip->priv;
771 unsigned long end_time;
772 const int cap = host->pmecc_corr_cap;
773 const int num = 2 * cap + 1;
774 int sector_size = host->pmecc_sector_size;
775 int err_nbr = 0; /* number of error */
776 int roots_nbr; /* number of roots */
777 int i;
778 uint32_t val;
779 int16_t *smu = host->pmecc_smu;
780
781 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
782
783 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
784 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
785 smu[(cap + 1) * num + i]);
786 err_nbr++;
787 }
788
789 val = (err_nbr - 1) << 16;
790 if (sector_size == 1024)
791 val |= 1;
792
793 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
794 pmerrloc_writel(host->pmerrloc_base, ELEN,
795 sector_size * 8 + host->pmecc_degree * cap);
796
797 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
798 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
799 & PMERRLOC_CALC_DONE)) {
800 if (unlikely(time_after(jiffies, end_time))) {
801 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
802 return -1;
803 }
804 cpu_relax();
805 }
806
807 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
808 & PMERRLOC_ERR_NUM_MASK) >> 8;
809 /* Number of roots == degree of smu hence <= cap */
810 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
811 return err_nbr - 1;
812
813 /* Number of roots does not match the degree of smu
814 * unable to correct error */
815 return -1;
816}
817
818static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
819 int sector_num, int extra_bytes, int err_nbr)
820{
821 struct nand_chip *nand_chip = mtd->priv;
822 struct atmel_nand_host *host = nand_chip->priv;
823 int i = 0;
824 int byte_pos, bit_pos, sector_size, pos;
825 uint32_t tmp;
826 uint8_t err_byte;
827
828 sector_size = host->pmecc_sector_size;
829
830 while (err_nbr) {
831 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
832 byte_pos = tmp / 8;
833 bit_pos = tmp % 8;
834
835 if (byte_pos >= (sector_size + extra_bytes))
836 BUG(); /* should never happen */
837
838 if (byte_pos < sector_size) {
839 err_byte = *(buf + byte_pos);
840 *(buf + byte_pos) ^= (1 << bit_pos);
841
842 pos = sector_num * host->pmecc_sector_size + byte_pos;
843 dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
844 pos, bit_pos, err_byte, *(buf + byte_pos));
845 } else {
846 /* Bit flip in OOB area */
847 tmp = sector_num * host->pmecc_bytes_per_sector
848 + (byte_pos - sector_size);
849 err_byte = ecc[tmp];
850 ecc[tmp] ^= (1 << bit_pos);
851
852 pos = tmp + nand_chip->ecc.layout->eccpos[0];
853 dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
854 pos, bit_pos, err_byte, ecc[tmp]);
855 }
856
857 i++;
858 err_nbr--;
859 }
860
861 return;
862}
863
864static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
865 u8 *ecc)
866{
867 struct nand_chip *nand_chip = mtd->priv;
868 struct atmel_nand_host *host = nand_chip->priv;
Bo Shenb3857662014-06-12 15:58:45 +0800869 int i, err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800870 uint8_t *buf_pos;
Josh Wuc0c70d92012-11-27 18:50:31 +0800871 int total_err = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800872
Bo Shenb3857662014-06-12 15:58:45 +0800873 for (i = 0; i < nand_chip->ecc.total; i++)
Josh Wu1c7b8742012-06-29 17:47:55 +0800874 if (ecc[i] != 0xff)
875 goto normal_check;
876 /* Erased page, return OK */
877 return 0;
878
879normal_check:
880 for (i = 0; i < host->pmecc_sector_number; i++) {
881 err_nbr = 0;
882 if (pmecc_stat & 0x1) {
883 buf_pos = buf + i * host->pmecc_sector_size;
884
885 pmecc_gen_syndrome(mtd, i);
886 pmecc_substitute(mtd);
887 pmecc_get_sigma(mtd);
888
889 err_nbr = pmecc_err_location(mtd);
890 if (err_nbr == -1) {
891 dev_err(host->dev, "PMECC: Too many errors\n");
892 mtd->ecc_stats.failed++;
893 return -EIO;
894 } else {
895 pmecc_correct_data(mtd, buf_pos, ecc, i,
896 host->pmecc_bytes_per_sector, err_nbr);
897 mtd->ecc_stats.corrected += err_nbr;
Josh Wuc0c70d92012-11-27 18:50:31 +0800898 total_err += err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800899 }
900 }
901 pmecc_stat >>= 1;
902 }
903
Josh Wuc0c70d92012-11-27 18:50:31 +0800904 return total_err;
Josh Wu1c7b8742012-06-29 17:47:55 +0800905}
906
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800907static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
908{
909 u32 val;
910
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800911 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
912 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
913 return;
914 }
915
Josh Wu1fad0e82013-08-07 17:58:11 +0800916 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
917 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
918 val = pmecc_readl_relaxed(host->ecc, CFG);
919
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800920 if (ecc_op == NAND_ECC_READ)
921 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
922 | PMECC_CFG_AUTO_ENABLE);
923 else
924 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
925 & ~PMECC_CFG_AUTO_ENABLE);
926
927 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
928 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
929}
930
Josh Wu1c7b8742012-06-29 17:47:55 +0800931static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
932 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
933{
934 struct atmel_nand_host *host = chip->priv;
Bo Shenb3857662014-06-12 15:58:45 +0800935 int eccsize = chip->ecc.size * chip->ecc.steps;
Josh Wu1c7b8742012-06-29 17:47:55 +0800936 uint8_t *oob = chip->oob_poi;
937 uint32_t *eccpos = chip->ecc.layout->eccpos;
938 uint32_t stat;
939 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800940 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800941
Josh Wu1ae9c092013-08-05 19:14:36 +0800942 if (!host->nfc || !host->nfc->use_nfc_sram)
943 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800944
945 chip->read_buf(mtd, buf, eccsize);
946 chip->read_buf(mtd, oob, mtd->oobsize);
947
948 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
949 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
950 if (unlikely(time_after(jiffies, end_time))) {
951 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
952 return -EIO;
953 }
954 cpu_relax();
955 }
956
957 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800958 if (stat != 0) {
959 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
960 if (bitflips < 0)
961 /* uncorrectable errors */
962 return 0;
963 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800964
Josh Wuc0c70d92012-11-27 18:50:31 +0800965 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800966}
967
968static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
969 struct nand_chip *chip, const uint8_t *buf, int oob_required)
970{
971 struct atmel_nand_host *host = chip->priv;
972 uint32_t *eccpos = chip->ecc.layout->eccpos;
973 int i, j;
974 unsigned long end_time;
975
Josh Wu6054d4d2013-08-05 19:14:37 +0800976 if (!host->nfc || !host->nfc->write_by_sram) {
977 pmecc_enable(host, NAND_ECC_WRITE);
978 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
979 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800980
981 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
982 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
983 if (unlikely(time_after(jiffies, end_time))) {
984 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
985 return -EIO;
986 }
987 cpu_relax();
988 }
989
990 for (i = 0; i < host->pmecc_sector_number; i++) {
991 for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
992 int pos;
993
994 pos = i * host->pmecc_bytes_per_sector + j;
995 chip->oob_poi[eccpos[pos]] =
996 pmecc_readb_ecc_relaxed(host->ecc, i, j);
997 }
998 }
999 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1000
1001 return 0;
1002}
1003
1004static void atmel_pmecc_core_init(struct mtd_info *mtd)
1005{
1006 struct nand_chip *nand_chip = mtd->priv;
1007 struct atmel_nand_host *host = nand_chip->priv;
1008 uint32_t val = 0;
1009 struct nand_ecclayout *ecc_layout;
1010
1011 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
1012 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1013
1014 switch (host->pmecc_corr_cap) {
1015 case 2:
1016 val = PMECC_CFG_BCH_ERR2;
1017 break;
1018 case 4:
1019 val = PMECC_CFG_BCH_ERR4;
1020 break;
1021 case 8:
1022 val = PMECC_CFG_BCH_ERR8;
1023 break;
1024 case 12:
1025 val = PMECC_CFG_BCH_ERR12;
1026 break;
1027 case 24:
1028 val = PMECC_CFG_BCH_ERR24;
1029 break;
1030 }
1031
1032 if (host->pmecc_sector_size == 512)
1033 val |= PMECC_CFG_SECTOR512;
1034 else if (host->pmecc_sector_size == 1024)
1035 val |= PMECC_CFG_SECTOR1024;
1036
1037 switch (host->pmecc_sector_number) {
1038 case 1:
1039 val |= PMECC_CFG_PAGE_1SECTOR;
1040 break;
1041 case 2:
1042 val |= PMECC_CFG_PAGE_2SECTORS;
1043 break;
1044 case 4:
1045 val |= PMECC_CFG_PAGE_4SECTORS;
1046 break;
1047 case 8:
1048 val |= PMECC_CFG_PAGE_8SECTORS;
1049 break;
1050 }
1051
1052 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
1053 | PMECC_CFG_AUTO_DISABLE);
1054 pmecc_writel(host->ecc, CFG, val);
1055
1056 ecc_layout = nand_chip->ecc.layout;
1057 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
1058 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
1059 pmecc_writel(host->ecc, EADDR,
1060 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
1061 /* See datasheet about PMECC Clock Control Register */
1062 pmecc_writel(host->ecc, CLK, 2);
1063 pmecc_writel(host->ecc, IDR, 0xff);
1064 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
1065}
1066
Josh Wu84cfbbb2013-01-23 20:47:12 +08001067/*
Josh Wu2a3d9332013-09-18 13:58:48 +08001068 * Get minimum ecc requirements from NAND.
Josh Wu84cfbbb2013-01-23 20:47:12 +08001069 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
Josh Wu2a3d9332013-09-18 13:58:48 +08001070 * will set them according to minimum ecc requirement. Otherwise, use the
Josh Wu84cfbbb2013-01-23 20:47:12 +08001071 * value in DTS file.
1072 * return 0 if success. otherwise return error code.
1073 */
1074static int pmecc_choose_ecc(struct atmel_nand_host *host,
1075 int *cap, int *sector_size)
1076{
Josh Wu2a3d9332013-09-18 13:58:48 +08001077 /* Get minimum ECC requirements */
1078 if (host->nand_chip.ecc_strength_ds) {
1079 *cap = host->nand_chip.ecc_strength_ds;
1080 *sector_size = host->nand_chip.ecc_step_ds;
1081 dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n",
Josh Wu84cfbbb2013-01-23 20:47:12 +08001082 *cap, *sector_size);
Josh Wu84cfbbb2013-01-23 20:47:12 +08001083 } else {
Josh Wu84cfbbb2013-01-23 20:47:12 +08001084 *cap = 2;
1085 *sector_size = 512;
Josh Wu2a3d9332013-09-18 13:58:48 +08001086 dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
Josh Wu84cfbbb2013-01-23 20:47:12 +08001087 }
1088
Josh Wu2a3d9332013-09-18 13:58:48 +08001089 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
Josh Wu84cfbbb2013-01-23 20:47:12 +08001090 if (host->pmecc_corr_cap == 0) {
1091 /* use the most fitable ecc bits (the near bigger one ) */
1092 if (*cap <= 2)
1093 host->pmecc_corr_cap = 2;
1094 else if (*cap <= 4)
1095 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +08001096 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001097 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +08001098 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001099 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +08001100 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001101 host->pmecc_corr_cap = 24;
1102 else
1103 return -EINVAL;
1104 }
1105 if (host->pmecc_sector_size == 0) {
1106 /* use the most fitable sector size (the near smaller one ) */
1107 if (*sector_size >= 1024)
1108 host->pmecc_sector_size = 1024;
1109 else if (*sector_size >= 512)
1110 host->pmecc_sector_size = 512;
1111 else
1112 return -EINVAL;
1113 }
1114 return 0;
1115}
1116
Johan Hovold2c2b9282013-09-23 16:27:28 +02001117static int atmel_pmecc_nand_init_params(struct platform_device *pdev,
Josh Wu1c7b8742012-06-29 17:47:55 +08001118 struct atmel_nand_host *host)
1119{
1120 struct mtd_info *mtd = &host->mtd;
1121 struct nand_chip *nand_chip = &host->nand_chip;
1122 struct resource *regs, *regs_pmerr, *regs_rom;
1123 int cap, sector_size, err_no;
1124
Josh Wu84cfbbb2013-01-23 20:47:12 +08001125 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
1126 if (err_no) {
1127 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1128 return err_no;
1129 }
1130
Richard Genoudf666d642013-07-30 17:17:29 +02001131 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001132 sector_size != host->pmecc_sector_size)
1133 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001134
Josh Wu1c7b8742012-06-29 17:47:55 +08001135 cap = host->pmecc_corr_cap;
1136 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001137 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1138 host->pmecc_lookup_table_offset_512 :
1139 host->pmecc_lookup_table_offset_1024;
1140
Josh Wu1c7b8742012-06-29 17:47:55 +08001141 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1142 cap, sector_size);
1143
1144 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1145 if (!regs) {
1146 dev_warn(host->dev,
1147 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1148 nand_chip->ecc.mode = NAND_ECC_SOFT;
1149 return 0;
1150 }
1151
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001152 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1153 if (IS_ERR(host->ecc)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001154 err_no = PTR_ERR(host->ecc);
1155 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001156 }
1157
1158 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001159 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1160 if (IS_ERR(host->pmerrloc_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001161 err_no = PTR_ERR(host->pmerrloc_base);
1162 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001163 }
1164
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001165 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1166 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, regs_rom);
1167 if (IS_ERR(host->pmecc_rom_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001168 err_no = PTR_ERR(host->pmecc_rom_base);
1169 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001170 }
1171
Bo Shenb3857662014-06-12 15:58:45 +08001172 nand_chip->ecc.size = sector_size;
Josh Wu1c7b8742012-06-29 17:47:55 +08001173
1174 /* set ECC page size and oob layout */
1175 switch (mtd->writesize) {
Wu, Josha3557102014-07-22 17:24:18 +08001176 case 512:
1177 case 1024:
Josh Wu1c7b8742012-06-29 17:47:55 +08001178 case 2048:
Wu, Josha3557102014-07-22 17:24:18 +08001179 case 4096:
1180 case 8192:
1181 if (sector_size > mtd->writesize) {
1182 dev_err(host->dev, "pmecc sector size is bigger than the page size!\n");
1183 err_no = -EINVAL;
1184 goto err;
1185 }
1186
Josh Wu2fa831f2013-08-19 18:05:44 +08001187 host->pmecc_degree = (sector_size == 512) ?
1188 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Josh Wu1c7b8742012-06-29 17:47:55 +08001189 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
1190 host->pmecc_sector_number = mtd->writesize / sector_size;
1191 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
1192 cap, sector_size);
1193 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1194 host->pmecc_index_of = host->pmecc_rom_base +
1195 host->pmecc_lookup_table_offset;
1196
Bo Shenb3857662014-06-12 15:58:45 +08001197 nand_chip->ecc.steps = host->pmecc_sector_number;
Josh Wu1c7b8742012-06-29 17:47:55 +08001198 nand_chip->ecc.strength = cap;
Bo Shenb3857662014-06-12 15:58:45 +08001199 nand_chip->ecc.bytes = host->pmecc_bytes_per_sector;
1200 nand_chip->ecc.total = host->pmecc_bytes_per_sector *
Josh Wu1c7b8742012-06-29 17:47:55 +08001201 host->pmecc_sector_number;
Bo Shenb3857662014-06-12 15:58:45 +08001202 if (nand_chip->ecc.total > mtd->oobsize - 2) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001203 dev_err(host->dev, "No room for ECC bytes\n");
1204 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001205 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001206 }
1207 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1208 mtd->oobsize,
Bo Shenb3857662014-06-12 15:58:45 +08001209 nand_chip->ecc.total);
1210
Josh Wu1c7b8742012-06-29 17:47:55 +08001211 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1212 break;
Wu, Josha3557102014-07-22 17:24:18 +08001213 default:
Josh Wu1c7b8742012-06-29 17:47:55 +08001214 dev_warn(host->dev,
1215 "Unsupported page size for PMECC, use Software ECC\n");
Josh Wu1c7b8742012-06-29 17:47:55 +08001216 /* page size not handled by HW ECC */
1217 /* switching back to soft ECC */
1218 nand_chip->ecc.mode = NAND_ECC_SOFT;
1219 return 0;
1220 }
1221
1222 /* Allocate data for PMECC computation */
1223 err_no = pmecc_data_alloc(host);
1224 if (err_no) {
1225 dev_err(host->dev,
1226 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001227 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001228 }
1229
Herve Codina90445ff2014-03-03 12:15:29 +01001230 nand_chip->options |= NAND_NO_SUBPAGE_WRITE;
Josh Wu1c7b8742012-06-29 17:47:55 +08001231 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1232 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1233
1234 atmel_pmecc_core_init(mtd);
1235
1236 return 0;
1237
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001238err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001239 return err_no;
1240}
1241
1242/*
Richard Genoud77f54922008-04-23 19:51:14 +02001243 * Calculate HW ECC
1244 *
1245 * function called after a write
1246 *
1247 * mtd: MTD block structure
1248 * dat: raw data (unused)
1249 * ecc_code: buffer for ECC
1250 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001251static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001252 const u_char *dat, unsigned char *ecc_code)
1253{
1254 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001255 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001256 unsigned int ecc_value;
1257
1258 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001259 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001260
Richard Genoud3fc23892008-10-12 08:42:28 +02001261 ecc_code[0] = ecc_value & 0xFF;
1262 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001263
1264 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001265 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001266
Richard Genoud3fc23892008-10-12 08:42:28 +02001267 ecc_code[2] = ecc_value & 0xFF;
1268 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001269
1270 return 0;
1271}
1272
1273/*
1274 * HW ECC read page function
1275 *
1276 * mtd: mtd info structure
1277 * chip: nand chip info structure
1278 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001279 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001280 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001281static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1282 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001283{
1284 int eccsize = chip->ecc.size;
1285 int eccbytes = chip->ecc.bytes;
1286 uint32_t *eccpos = chip->ecc.layout->eccpos;
1287 uint8_t *p = buf;
1288 uint8_t *oob = chip->oob_poi;
1289 uint8_t *ecc_pos;
1290 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001291 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001292
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001293 /*
1294 * Errata: ALE is incorrectly wired up to the ECC controller
1295 * on the AP7000, so it will include the address cycles in the
1296 * ECC calculation.
1297 *
1298 * Workaround: Reset the parity registers before reading the
1299 * actual data.
1300 */
Josh Wu71b94e22013-05-09 15:34:54 +08001301 struct atmel_nand_host *host = chip->priv;
1302 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001303 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001304
Richard Genoud77f54922008-04-23 19:51:14 +02001305 /* read the page */
1306 chip->read_buf(mtd, p, eccsize);
1307
1308 /* move to ECC position if needed */
1309 if (eccpos[0] != 0) {
1310 /* This only works on large pages
1311 * because the ECC controller waits for
1312 * NAND_CMD_RNDOUTSTART after the
1313 * NAND_CMD_RNDOUT.
1314 * anyway, for small pages, the eccpos[0] == 0
1315 */
1316 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1317 mtd->writesize + eccpos[0], -1);
1318 }
1319
1320 /* the ECC controller needs to read the ECC just after the data */
1321 ecc_pos = oob + eccpos[0];
1322 chip->read_buf(mtd, ecc_pos, eccbytes);
1323
1324 /* check if there's an error */
1325 stat = chip->ecc.correct(mtd, p, oob, NULL);
1326
Mike Dunn3f91e942012-04-25 12:06:09 -07001327 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001328 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001329 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001330 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001331 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1332 }
Richard Genoud77f54922008-04-23 19:51:14 +02001333
1334 /* get back to oob start (end of page) */
1335 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1336
1337 /* read the oob */
1338 chip->read_buf(mtd, oob, mtd->oobsize);
1339
Mike Dunn3f91e942012-04-25 12:06:09 -07001340 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001341}
1342
1343/*
1344 * HW ECC Correction
1345 *
1346 * function called after a read
1347 *
1348 * mtd: MTD block structure
1349 * dat: raw data read from the chip
1350 * read_ecc: ECC from the chip (unused)
1351 * isnull: unused
1352 *
1353 * Detect and correct a 1 bit error for a page
1354 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001355static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001356 u_char *read_ecc, u_char *isnull)
1357{
1358 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001359 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001360 unsigned int ecc_status;
1361 unsigned int ecc_word, ecc_bit;
1362
1363 /* get the status from the Status Register */
1364 ecc_status = ecc_readl(host->ecc, SR);
1365
1366 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001367 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001368 return 0;
1369
1370 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001371 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001372 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001373 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001374 ecc_word >>= 4;
1375
1376 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001377 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001378 /* check if it is a freshly erased block
1379 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001380 if ((ecc_bit == ATMEL_ECC_BITADDR)
1381 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001382 /* the block has just been erased, return OK */
1383 return 0;
1384 }
1385 /* it doesn't seems to be a freshly
1386 * erased block.
1387 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001388 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001389 " Unable to correct.\n");
1390 return -EIO;
1391 }
1392
1393 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001394 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001395 /* there's nothing much to do here.
1396 * the bit error is on the ECC itself.
1397 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001398 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001399 " Nothing to correct\n");
1400 return 0;
1401 }
1402
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001403 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001404 " (word offset in the page :"
1405 " 0x%x bit offset : 0x%x)\n",
1406 ecc_word, ecc_bit);
1407 /* correct the error */
1408 if (nand_chip->options & NAND_BUSWIDTH_16) {
1409 /* 16 bits words */
1410 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1411 } else {
1412 /* 8 bits words */
1413 dat[ecc_word] ^= (1 << ecc_bit);
1414 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001415 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001416 return 1;
1417}
1418
1419/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001420 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001421 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001422static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1423{
Josh Wu71b94e22013-05-09 15:34:54 +08001424 struct nand_chip *nand_chip = mtd->priv;
1425 struct atmel_nand_host *host = nand_chip->priv;
1426
1427 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001428 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001429}
Richard Genoud77f54922008-04-23 19:51:14 +02001430
Bill Pemberton06f25512012-11-19 13:23:07 -05001431static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001432 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001433{
Josh Wuc0cf7872013-01-23 20:47:08 +08001434 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001435 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001436 int ecc_mode;
1437 struct atmel_nand_data *board = &host->board;
Josh Wue9d8da82013-09-18 11:31:19 +08001438 enum of_gpio_flags flags = 0;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001439
1440 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1441 if (val >= 32) {
1442 dev_err(host->dev, "invalid addr-offset %u\n", val);
1443 return -EINVAL;
1444 }
1445 board->ale = val;
1446 }
1447
1448 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1449 if (val >= 32) {
1450 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1451 return -EINVAL;
1452 }
1453 board->cle = val;
1454 }
1455
1456 ecc_mode = of_get_nand_ecc_mode(np);
1457
1458 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1459
1460 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1461
Josh Wu1b719262013-05-09 15:34:55 +08001462 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1463
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001464 if (of_get_nand_bus_width(np) == 16)
1465 board->bus_width_16 = 1;
1466
1467 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1468 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1469
1470 board->enable_pin = of_get_gpio(np, 1);
1471 board->det_pin = of_get_gpio(np, 2);
1472
Josh Wua41b51a2012-06-29 17:47:54 +08001473 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1474
Josh Wu7dc37de2013-08-05 19:14:35 +08001475 /* load the nfc driver if there is */
1476 of_platform_populate(np, NULL, NULL, host->dev);
1477
Josh Wua41b51a2012-06-29 17:47:54 +08001478 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1479 return 0; /* Not using PMECC */
1480
1481 /* use PMECC, get correction capability, sector size and lookup
1482 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001483 * If correction bits and sector size are not specified, then find
1484 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001485 */
Josh Wue66b4312013-01-23 20:47:11 +08001486 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1487 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1488 (val != 24)) {
1489 dev_err(host->dev,
1490 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1491 val);
1492 return -EINVAL;
1493 }
1494 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001495 }
Josh Wua41b51a2012-06-29 17:47:54 +08001496
Josh Wue66b4312013-01-23 20:47:11 +08001497 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1498 if ((val != 512) && (val != 1024)) {
1499 dev_err(host->dev,
1500 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1501 val);
1502 return -EINVAL;
1503 }
1504 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001505 }
Josh Wua41b51a2012-06-29 17:47:54 +08001506
1507 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1508 offset, 2) != 0) {
1509 dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
1510 return -EINVAL;
1511 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001512 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001513 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1514 return -EINVAL;
1515 }
Josh Wue66b4312013-01-23 20:47:11 +08001516 host->pmecc_lookup_table_offset_512 = offset[0];
1517 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001518
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001519 return 0;
1520}
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001521
Johan Hovold2c2b9282013-09-23 16:27:28 +02001522static int atmel_hw_nand_init_params(struct platform_device *pdev,
Josh Wu3dfe41a2012-06-25 18:07:43 +08001523 struct atmel_nand_host *host)
1524{
1525 struct mtd_info *mtd = &host->mtd;
1526 struct nand_chip *nand_chip = &host->nand_chip;
1527 struct resource *regs;
1528
1529 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1530 if (!regs) {
1531 dev_err(host->dev,
1532 "Can't get I/O resource regs, use software ECC\n");
1533 nand_chip->ecc.mode = NAND_ECC_SOFT;
1534 return 0;
1535 }
1536
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001537 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun8fb7b932014-07-28 21:19:55 +08001538 if (IS_ERR(host->ecc))
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001539 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001540
1541 /* ECC is calculated for the whole page (1 step) */
1542 nand_chip->ecc.size = mtd->writesize;
1543
1544 /* set ECC page size and oob layout */
1545 switch (mtd->writesize) {
1546 case 512:
1547 nand_chip->ecc.layout = &atmel_oobinfo_small;
1548 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1549 break;
1550 case 1024:
1551 nand_chip->ecc.layout = &atmel_oobinfo_large;
1552 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1553 break;
1554 case 2048:
1555 nand_chip->ecc.layout = &atmel_oobinfo_large;
1556 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1557 break;
1558 case 4096:
1559 nand_chip->ecc.layout = &atmel_oobinfo_large;
1560 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1561 break;
1562 default:
1563 /* page size not handled by HW ECC */
1564 /* switching back to soft ECC */
1565 nand_chip->ecc.mode = NAND_ECC_SOFT;
1566 return 0;
1567 }
1568
1569 /* set up for HW ECC */
1570 nand_chip->ecc.calculate = atmel_nand_calculate;
1571 nand_chip->ecc.correct = atmel_nand_correct;
1572 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1573 nand_chip->ecc.read_page = atmel_nand_read_page;
1574 nand_chip->ecc.bytes = 4;
1575 nand_chip->ecc.strength = 1;
1576
1577 return 0;
1578}
1579
Wu, Josh50e04e22014-06-10 17:50:09 +08001580static inline u32 nfc_read_status(struct atmel_nand_host *host)
1581{
1582 u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
1583 u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
1584
1585 if (unlikely(nfc_status & err_flags)) {
1586 if (nfc_status & NFC_SR_DTOE)
1587 dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
1588 else if (nfc_status & NFC_SR_UNDEF)
1589 dev_err(host->dev, "NFC: Access Undefined Area Error\n");
1590 else if (nfc_status & NFC_SR_AWB)
1591 dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
1592 else if (nfc_status & NFC_SR_ASE)
1593 dev_err(host->dev, "NFC: Access memory Size Error\n");
1594 }
1595
1596 return nfc_status;
1597}
1598
Josh Wu7dc37de2013-08-05 19:14:35 +08001599/* SMC interrupt service routine */
1600static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
1601{
1602 struct atmel_nand_host *host = dev_id;
1603 u32 status, mask, pending;
Josh Wue4e06932014-06-10 17:50:11 +08001604 irqreturn_t ret = IRQ_NONE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001605
Wu, Josh50e04e22014-06-10 17:50:09 +08001606 status = nfc_read_status(host);
Josh Wu7dc37de2013-08-05 19:14:35 +08001607 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1608 pending = status & mask;
1609
1610 if (pending & NFC_SR_XFR_DONE) {
Josh Wue4e06932014-06-10 17:50:11 +08001611 complete(&host->nfc->comp_xfer_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001612 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001613 ret = IRQ_HANDLED;
1614 }
1615 if (pending & NFC_SR_RB_EDGE) {
1616 complete(&host->nfc->comp_ready);
Josh Wu7dc37de2013-08-05 19:14:35 +08001617 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_RB_EDGE);
Josh Wue4e06932014-06-10 17:50:11 +08001618 ret = IRQ_HANDLED;
1619 }
1620 if (pending & NFC_SR_CMD_DONE) {
1621 complete(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001622 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001623 ret = IRQ_HANDLED;
Josh Wu7dc37de2013-08-05 19:14:35 +08001624 }
1625
1626 return ret;
1627}
1628
1629/* NFC(Nand Flash Controller) related functions */
Josh Wue4e06932014-06-10 17:50:11 +08001630static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag)
Josh Wu7dc37de2013-08-05 19:14:35 +08001631{
Josh Wue4e06932014-06-10 17:50:11 +08001632 if (flag & NFC_SR_XFR_DONE)
1633 init_completion(&host->nfc->comp_xfer_done);
1634
1635 if (flag & NFC_SR_RB_EDGE)
1636 init_completion(&host->nfc->comp_ready);
1637
1638 if (flag & NFC_SR_CMD_DONE)
1639 init_completion(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001640
1641 /* Enable interrupt that need to wait for */
1642 nfc_writel(host->nfc->hsmc_regs, IER, flag);
Josh Wue4e06932014-06-10 17:50:11 +08001643}
Josh Wu7dc37de2013-08-05 19:14:35 +08001644
Josh Wue4e06932014-06-10 17:50:11 +08001645static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
1646{
1647 int i, index = 0;
1648 struct completion *comp[3]; /* Support 3 interrupt completion */
Josh Wu7dc37de2013-08-05 19:14:35 +08001649
Josh Wue4e06932014-06-10 17:50:11 +08001650 if (flag & NFC_SR_XFR_DONE)
1651 comp[index++] = &host->nfc->comp_xfer_done;
1652
1653 if (flag & NFC_SR_RB_EDGE)
1654 comp[index++] = &host->nfc->comp_ready;
1655
1656 if (flag & NFC_SR_CMD_DONE)
1657 comp[index++] = &host->nfc->comp_cmd_done;
1658
1659 if (index == 0) {
1660 dev_err(host->dev, "Unkown interrupt flag: 0x%08x\n", flag);
1661 return -EINVAL;
1662 }
1663
1664 for (i = 0; i < index; i++) {
1665 if (wait_for_completion_timeout(comp[i],
1666 msecs_to_jiffies(NFC_TIME_OUT_MS)))
1667 continue; /* wait for next completion */
1668 else
1669 goto err_timeout;
1670 }
1671
1672 return 0;
1673
1674err_timeout:
Josh Wu7dc37de2013-08-05 19:14:35 +08001675 dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
Josh Wue4e06932014-06-10 17:50:11 +08001676 /* Disable the interrupt as it is not handled by interrupt handler */
1677 nfc_writel(host->nfc->hsmc_regs, IDR, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001678 return -ETIMEDOUT;
1679}
1680
1681static int nfc_send_command(struct atmel_nand_host *host,
1682 unsigned int cmd, unsigned int addr, unsigned char cycle0)
1683{
1684 unsigned long timeout;
Josh Wue4e06932014-06-10 17:50:11 +08001685 u32 flag = NFC_SR_CMD_DONE;
1686 flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0;
1687
Josh Wu7dc37de2013-08-05 19:14:35 +08001688 dev_dbg(host->dev,
1689 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1690 cmd, addr, cycle0);
1691
1692 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1693 while (nfc_cmd_readl(NFCADDR_CMD_NFCBUSY, host->nfc->base_cmd_regs)
1694 & NFCADDR_CMD_NFCBUSY) {
1695 if (time_after(jiffies, timeout)) {
1696 dev_err(host->dev,
1697 "Time out to wait CMD_NFCBUSY ready!\n");
1698 return -ETIMEDOUT;
1699 }
1700 }
Josh Wue4e06932014-06-10 17:50:11 +08001701
1702 nfc_prepare_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001703 nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
1704 nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
Josh Wue4e06932014-06-10 17:50:11 +08001705 return nfc_wait_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001706}
1707
1708static int nfc_device_ready(struct mtd_info *mtd)
1709{
Wu, Josh72a78e32014-06-10 17:50:10 +08001710 u32 status, mask;
Josh Wu7dc37de2013-08-05 19:14:35 +08001711 struct nand_chip *nand_chip = mtd->priv;
1712 struct atmel_nand_host *host = nand_chip->priv;
Wu, Josh72a78e32014-06-10 17:50:10 +08001713
1714 status = nfc_read_status(host);
1715 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1716
1717 /* The mask should be 0. If not we may lost interrupts */
1718 if (unlikely(mask & status))
1719 dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n",
1720 mask & status);
1721
1722 return status & NFC_SR_RB_EDGE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001723}
1724
1725static void nfc_select_chip(struct mtd_info *mtd, int chip)
1726{
1727 struct nand_chip *nand_chip = mtd->priv;
1728 struct atmel_nand_host *host = nand_chip->priv;
1729
1730 if (chip == -1)
1731 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
1732 else
1733 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
1734}
1735
Brian Norris3dad2342014-01-29 14:08:12 -08001736static int nfc_make_addr(struct mtd_info *mtd, int command, int column,
1737 int page_addr, unsigned int *addr1234, unsigned int *cycle0)
Josh Wu7dc37de2013-08-05 19:14:35 +08001738{
1739 struct nand_chip *chip = mtd->priv;
1740
1741 int acycle = 0;
1742 unsigned char addr_bytes[8];
1743 int index = 0, bit_shift;
1744
1745 BUG_ON(addr1234 == NULL || cycle0 == NULL);
1746
1747 *cycle0 = 0;
1748 *addr1234 = 0;
1749
1750 if (column != -1) {
Brian Norris3dad2342014-01-29 14:08:12 -08001751 if (chip->options & NAND_BUSWIDTH_16 &&
1752 !nand_opcode_8bits(command))
Josh Wu7dc37de2013-08-05 19:14:35 +08001753 column >>= 1;
1754 addr_bytes[acycle++] = column & 0xff;
1755 if (mtd->writesize > 512)
1756 addr_bytes[acycle++] = (column >> 8) & 0xff;
1757 }
1758
1759 if (page_addr != -1) {
1760 addr_bytes[acycle++] = page_addr & 0xff;
1761 addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
1762 if (chip->chipsize > (128 << 20))
1763 addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
1764 }
1765
1766 if (acycle > 4)
1767 *cycle0 = addr_bytes[index++];
1768
1769 for (bit_shift = 0; index < acycle; bit_shift += 8)
1770 *addr1234 += addr_bytes[index++] << bit_shift;
1771
1772 /* return acycle in cmd register */
1773 return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
1774}
1775
1776static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
1777 int column, int page_addr)
1778{
1779 struct nand_chip *chip = mtd->priv;
1780 struct atmel_nand_host *host = chip->priv;
1781 unsigned long timeout;
1782 unsigned int nfc_addr_cmd = 0;
1783
1784 unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1785
1786 /* Set default settings: no cmd2, no addr cycle. read from nand */
1787 unsigned int cmd2 = 0;
1788 unsigned int vcmd2 = 0;
1789 int acycle = NFCADDR_CMD_ACYCLE_NONE;
1790 int csid = NFCADDR_CMD_CSID_3;
1791 int dataen = NFCADDR_CMD_DATADIS;
1792 int nfcwr = NFCADDR_CMD_NFCRD;
1793 unsigned int addr1234 = 0;
1794 unsigned int cycle0 = 0;
1795 bool do_addr = true;
Josh Wu1ae9c092013-08-05 19:14:36 +08001796 host->nfc->data_in_sram = NULL;
Josh Wu7dc37de2013-08-05 19:14:35 +08001797
1798 dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1799 __func__, command, column, page_addr);
1800
1801 switch (command) {
1802 case NAND_CMD_RESET:
1803 nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
1804 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1805 udelay(chip->chip_delay);
1806
1807 nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
1808 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1809 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
1810 if (time_after(jiffies, timeout)) {
1811 dev_err(host->dev,
1812 "Time out to wait status ready!\n");
1813 break;
1814 }
1815 }
1816 return;
1817 case NAND_CMD_STATUS:
1818 do_addr = false;
1819 break;
1820 case NAND_CMD_PARAM:
1821 case NAND_CMD_READID:
1822 do_addr = false;
1823 acycle = NFCADDR_CMD_ACYCLE_1;
1824 if (column != -1)
1825 addr1234 = column;
1826 break;
1827 case NAND_CMD_RNDOUT:
1828 cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
1829 vcmd2 = NFCADDR_CMD_VCMD2;
1830 break;
1831 case NAND_CMD_READ0:
1832 case NAND_CMD_READOOB:
1833 if (command == NAND_CMD_READOOB) {
1834 column += mtd->writesize;
1835 command = NAND_CMD_READ0; /* only READ0 is valid */
1836 cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1837 }
Josh Wu1ae9c092013-08-05 19:14:36 +08001838 if (host->nfc->use_nfc_sram) {
1839 /* Enable Data transfer to sram */
1840 dataen = NFCADDR_CMD_DATAEN;
1841
1842 /* Need enable PMECC now, since NFC will transfer
1843 * data in bus after sending nfc read command.
1844 */
1845 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1846 pmecc_enable(host, NAND_ECC_READ);
1847 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001848
1849 cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
1850 vcmd2 = NFCADDR_CMD_VCMD2;
1851 break;
1852 /* For prgramming command, the cmd need set to write enable */
1853 case NAND_CMD_PAGEPROG:
1854 case NAND_CMD_SEQIN:
1855 case NAND_CMD_RNDIN:
1856 nfcwr = NFCADDR_CMD_NFCWR;
Josh Wu6054d4d2013-08-05 19:14:37 +08001857 if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
1858 dataen = NFCADDR_CMD_DATAEN;
Josh Wu7dc37de2013-08-05 19:14:35 +08001859 break;
1860 default:
1861 break;
1862 }
1863
1864 if (do_addr)
Brian Norris3dad2342014-01-29 14:08:12 -08001865 acycle = nfc_make_addr(mtd, command, column, page_addr,
1866 &addr1234, &cycle0);
Josh Wu7dc37de2013-08-05 19:14:35 +08001867
1868 nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
1869 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1870
1871 /*
1872 * Program and erase have their own busy handlers status, sequential
1873 * in, and deplete1 need no delay.
1874 */
1875 switch (command) {
1876 case NAND_CMD_CACHEDPROG:
1877 case NAND_CMD_PAGEPROG:
1878 case NAND_CMD_ERASE1:
1879 case NAND_CMD_ERASE2:
1880 case NAND_CMD_RNDIN:
1881 case NAND_CMD_STATUS:
1882 case NAND_CMD_RNDOUT:
1883 case NAND_CMD_SEQIN:
1884 case NAND_CMD_READID:
1885 return;
1886
1887 case NAND_CMD_READ0:
Josh Wu1ae9c092013-08-05 19:14:36 +08001888 if (dataen == NFCADDR_CMD_DATAEN) {
1889 host->nfc->data_in_sram = host->nfc->sram_bank0 +
1890 nfc_get_sram_off(host);
1891 return;
1892 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001893 /* fall through */
1894 default:
Josh Wue4e06932014-06-10 17:50:11 +08001895 nfc_prepare_interrupt(host, NFC_SR_RB_EDGE);
Josh Wu7dc37de2013-08-05 19:14:35 +08001896 nfc_wait_interrupt(host, NFC_SR_RB_EDGE);
1897 }
1898}
1899
Josh Wu6054d4d2013-08-05 19:14:37 +08001900static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1901 uint32_t offset, int data_len, const uint8_t *buf,
1902 int oob_required, int page, int cached, int raw)
1903{
1904 int cfg, len;
1905 int status = 0;
1906 struct atmel_nand_host *host = chip->priv;
1907 void __iomem *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
1908
1909 /* Subpage write is not supported */
1910 if (offset || (data_len < mtd->writesize))
1911 return -EINVAL;
1912
Josh Wu6054d4d2013-08-05 19:14:37 +08001913 len = mtd->writesize;
Josh Wu6054d4d2013-08-05 19:14:37 +08001914 /* Copy page data to sram that will write to nand via NFC */
1915 if (use_dma) {
1916 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
1917 /* Fall back to use cpu copy */
1918 memcpy32_toio(sram, buf, len);
1919 } else {
1920 memcpy32_toio(sram, buf, len);
1921 }
1922
Wu, Joshff0a2152014-08-05 18:38:52 +08001923 cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
1924 if (unlikely(raw) && oob_required) {
1925 memcpy32_toio(sram + len, chip->oob_poi, mtd->oobsize);
1926 len += mtd->oobsize;
1927 nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
1928 } else {
1929 nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
1930 }
1931
Josh Wu6054d4d2013-08-05 19:14:37 +08001932 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1933 /*
1934 * When use NFC sram, need set up PMECC before send
1935 * NAND_CMD_SEQIN command. Since when the nand command
1936 * is sent, nfc will do transfer from sram and nand.
1937 */
1938 pmecc_enable(host, NAND_ECC_WRITE);
1939
1940 host->nfc->will_write_sram = true;
1941 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1942 host->nfc->will_write_sram = false;
1943
1944 if (likely(!raw))
1945 /* Need to write ecc into oob */
1946 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
1947
1948 if (status < 0)
1949 return status;
1950
1951 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1952 status = chip->waitfunc(mtd, chip);
1953
1954 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1955 status = chip->errstat(mtd, chip, FL_WRITING, status, page);
1956
1957 if (status & NAND_STATUS_FAIL)
1958 return -EIO;
1959
1960 return 0;
1961}
1962
Josh Wu1ae9c092013-08-05 19:14:36 +08001963static int nfc_sram_init(struct mtd_info *mtd)
1964{
1965 struct nand_chip *chip = mtd->priv;
1966 struct atmel_nand_host *host = chip->priv;
1967 int res = 0;
1968
1969 /* Initialize the NFC CFG register */
1970 unsigned int cfg_nfc = 0;
1971
1972 /* set page size and oob layout */
1973 switch (mtd->writesize) {
1974 case 512:
1975 cfg_nfc = NFC_CFG_PAGESIZE_512;
1976 break;
1977 case 1024:
1978 cfg_nfc = NFC_CFG_PAGESIZE_1024;
1979 break;
1980 case 2048:
1981 cfg_nfc = NFC_CFG_PAGESIZE_2048;
1982 break;
1983 case 4096:
1984 cfg_nfc = NFC_CFG_PAGESIZE_4096;
1985 break;
1986 case 8192:
1987 cfg_nfc = NFC_CFG_PAGESIZE_8192;
1988 break;
1989 default:
1990 dev_err(host->dev, "Unsupported page size for NFC.\n");
1991 res = -ENXIO;
1992 return res;
1993 }
1994
1995 /* oob bytes size = (NFCSPARESIZE + 1) * 4
1996 * Max support spare size is 512 bytes. */
1997 cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
1998 & NFC_CFG_NFC_SPARESIZE);
1999 /* default set a max timeout */
2000 cfg_nfc |= NFC_CFG_RSPARE |
2001 NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
2002
2003 nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
2004
Josh Wu6054d4d2013-08-05 19:14:37 +08002005 host->nfc->will_write_sram = false;
Josh Wu1ae9c092013-08-05 19:14:36 +08002006 nfc_set_sram_bank(host, 0);
2007
Josh Wu6054d4d2013-08-05 19:14:37 +08002008 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
2009 if (host->nfc->write_by_sram) {
2010 if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
2011 chip->ecc.mode == NAND_ECC_NONE)
2012 chip->write_page = nfc_sram_write_page;
2013 else
2014 host->nfc->write_by_sram = false;
2015 }
Josh Wu1ae9c092013-08-05 19:14:36 +08002016
Josh Wu6054d4d2013-08-05 19:14:37 +08002017 dev_info(host->dev, "Using NFC Sram read %s\n",
2018 host->nfc->write_by_sram ? "and write" : "");
Josh Wu1ae9c092013-08-05 19:14:36 +08002019 return 0;
2020}
2021
Josh Wu7dc37de2013-08-05 19:14:35 +08002022static struct platform_driver atmel_nand_nfc_driver;
Andrew Victor42cb1402006-10-19 18:24:35 +02002023/*
2024 * Probe for the NAND device.
2025 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002026static int atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002027{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002028 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02002029 struct mtd_info *mtd;
2030 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002031 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002032 struct mtd_part_parser_data ppdata = {};
Josh Wu7dc37de2013-08-05 19:14:35 +08002033 int res, irq;
Andrew Victor42cb1402006-10-19 18:24:35 +02002034
2035 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002036 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Han9e3677a2013-12-26 12:00:16 +09002037 if (!host)
Andrew Victor42cb1402006-10-19 18:24:35 +02002038 return -ENOMEM;
Andrew Victor42cb1402006-10-19 18:24:35 +02002039
Josh Wu7dc37de2013-08-05 19:14:35 +08002040 res = platform_driver_register(&atmel_nand_nfc_driver);
2041 if (res)
2042 dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
2043
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002044 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2045 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
2046 if (IS_ERR(host->io_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002047 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002048 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02002049 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002050 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02002051
2052 mtd = &host->mtd;
2053 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002054 host->dev = &pdev->dev;
Josh Wue9d8da82013-09-18 11:31:19 +08002055 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
2056 /* Only when CONFIG_OF is enabled of_node can be parsed */
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002057 res = atmel_of_init_port(host, pdev->dev.of_node);
2058 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002059 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002060 } else {
Jingoo Han453810b2013-07-30 17:18:33 +09002061 memcpy(&host->board, dev_get_platdata(&pdev->dev),
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002062 sizeof(struct atmel_nand_data));
2063 }
Andrew Victor42cb1402006-10-19 18:24:35 +02002064
2065 nand_chip->priv = host; /* link the private data structures */
2066 mtd->priv = nand_chip;
2067 mtd->owner = THIS_MODULE;
2068
2069 /* Set address of NAND IO lines */
2070 nand_chip->IO_ADDR_R = host->io_base;
2071 nand_chip->IO_ADDR_W = host->io_base;
Ivan Kutena4265f82007-05-24 14:35:58 +03002072
Josh Wu7dc37de2013-08-05 19:14:35 +08002073 if (nand_nfc.is_initialized) {
2074 /* NFC driver is probed and initialized */
2075 host->nfc = &nand_nfc;
2076
2077 nand_chip->select_chip = nfc_select_chip;
2078 nand_chip->dev_ready = nfc_device_ready;
2079 nand_chip->cmdfunc = nfc_nand_command;
2080
2081 /* Initialize the interrupt for NFC */
2082 irq = platform_get_irq(pdev, 0);
2083 if (irq < 0) {
2084 dev_err(host->dev, "Cannot get HSMC irq!\n");
Wei Yongjunff52c672013-08-23 10:50:36 +08002085 res = irq;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002086 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002087 }
2088
Josh Wu7dc37de2013-08-05 19:14:35 +08002089 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
2090 0, "hsmc", host);
2091 if (res) {
2092 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
2093 irq);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002094 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002095 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002096 } else {
2097 res = atmel_nand_set_enable_ready_pins(mtd);
2098 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002099 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002100
Josh Wu7dc37de2013-08-05 19:14:35 +08002101 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002102 }
Ivan Kutena4265f82007-05-24 14:35:58 +03002103
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002104 nand_chip->ecc.mode = host->board.ecc_mode;
Raphaël Poggi796fe362014-07-29 15:27:27 +02002105 nand_chip->chip_delay = 40; /* 40us command delay time */
Andrew Victor42cb1402006-10-19 18:24:35 +02002106
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002107 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02002108 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08002109
2110 nand_chip->read_buf = atmel_read_buf;
2111 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02002112
Andrew Victor42cb1402006-10-19 18:24:35 +02002113 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002114 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002115
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002116 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002117 res = devm_gpio_request(&pdev->dev,
2118 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002119 if (res < 0) {
2120 dev_err(&pdev->dev,
2121 "can't request det gpio %d\n",
2122 host->board.det_pin);
2123 goto err_no_card;
2124 }
2125
2126 res = gpio_direction_input(host->board.det_pin);
2127 if (res < 0) {
2128 dev_err(&pdev->dev,
2129 "can't request input direction det gpio %d\n",
2130 host->board.det_pin);
2131 goto err_no_card;
2132 }
2133
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002134 if (gpio_get_value(host->board.det_pin)) {
Jingoo Han1295f972013-12-26 12:30:58 +09002135 dev_info(&pdev->dev, "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01002136 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002137 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02002138 }
2139 }
2140
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002141 if (host->board.on_flash_bbt || on_flash_bbt) {
Jingoo Han1295f972013-12-26 12:30:58 +09002142 dev_info(&pdev->dev, "Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -07002143 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa6972009-05-27 18:19:39 +03002144 }
2145
Josh Wu1b719262013-05-09 15:34:55 +08002146 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08002147 use_dma = 0;
2148
2149 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08002150 dma_cap_mask_t mask;
2151
2152 dma_cap_zero(mask);
2153 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02002154 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002155 if (!host->dma_chan) {
2156 dev_err(host->dev, "Failed to request DMA channel\n");
2157 use_dma = 0;
2158 }
2159 }
2160 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08002161 dev_info(host->dev, "Using %s for DMA transfers.\n",
2162 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08002163 else
2164 dev_info(host->dev, "No DMA support for NAND access.\n");
2165
Richard Genoud77f54922008-04-23 19:51:14 +02002166 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00002167 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02002168 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002169 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02002170 }
2171
Richard Genoud3fc23892008-10-12 08:42:28 +02002172 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08002173 if (host->has_pmecc)
2174 res = atmel_pmecc_nand_init_params(pdev, host);
2175 else
2176 res = atmel_hw_nand_init_params(pdev, host);
2177
Josh Wu3dfe41a2012-06-25 18:07:43 +08002178 if (res != 0)
2179 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02002180 }
2181
Josh Wu1ae9c092013-08-05 19:14:36 +08002182 /* initialize the nfc configuration register */
2183 if (host->nfc && host->nfc->use_nfc_sram) {
2184 res = nfc_sram_init(mtd);
2185 if (res) {
2186 host->nfc->use_nfc_sram = false;
2187 dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
2188 }
2189 }
2190
Richard Genoud77f54922008-04-23 19:51:14 +02002191 /* second phase scan */
2192 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02002193 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002194 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02002195 }
2196
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002197 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002198 ppdata.of_node = pdev->dev.of_node;
2199 res = mtd_device_parse_register(mtd, NULL, &ppdata,
2200 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02002201 if (!res)
2202 return res;
2203
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002204err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002205 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08002206 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08002207err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002208err_scan_ident:
2209err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002210 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002211 if (host->dma_chan)
2212 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002213err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02002214 return res;
2215}
2216
2217/*
2218 * Remove a NAND device.
2219 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002220static int atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002221{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002222 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +02002223 struct mtd_info *mtd = &host->mtd;
2224
2225 nand_release(mtd);
2226
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002227 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002228
Josh Wu1c7b8742012-06-29 17:47:55 +08002229 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
2230 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2231 pmerrloc_writel(host->pmerrloc_base, ELDIS,
2232 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08002233 }
2234
Hong Xucbc6c5e2011-01-18 14:36:05 +08002235 if (host->dma_chan)
2236 dma_release_channel(host->dma_chan);
2237
Josh Wu7dc37de2013-08-05 19:14:35 +08002238 platform_driver_unregister(&atmel_nand_nfc_driver);
2239
Andrew Victor42cb1402006-10-19 18:24:35 +02002240 return 0;
2241}
2242
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002243static const struct of_device_id atmel_nand_dt_ids[] = {
2244 { .compatible = "atmel,at91rm9200-nand" },
2245 { /* sentinel */ }
2246};
2247
2248MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002249
Josh Wu7dc37de2013-08-05 19:14:35 +08002250static int atmel_nand_nfc_probe(struct platform_device *pdev)
2251{
2252 struct atmel_nfc *nfc = &nand_nfc;
2253 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002254 int ret;
Josh Wu7dc37de2013-08-05 19:14:35 +08002255
2256 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2257 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
2258 if (IS_ERR(nfc->base_cmd_regs))
2259 return PTR_ERR(nfc->base_cmd_regs);
2260
2261 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2262 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
2263 if (IS_ERR(nfc->hsmc_regs))
2264 return PTR_ERR(nfc->hsmc_regs);
2265
2266 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2267 if (nfc_sram) {
2268 nfc->sram_bank0 = devm_ioremap_resource(&pdev->dev, nfc_sram);
Josh Wu1ae9c092013-08-05 19:14:36 +08002269 if (IS_ERR(nfc->sram_bank0)) {
Josh Wu7dc37de2013-08-05 19:14:35 +08002270 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2271 PTR_ERR(nfc->sram_bank0));
Josh Wu1ae9c092013-08-05 19:14:36 +08002272 } else {
2273 nfc->use_nfc_sram = true;
Josh Wu7dc37de2013-08-05 19:14:35 +08002274 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
Josh Wu6054d4d2013-08-05 19:14:37 +08002275
2276 if (pdev->dev.of_node)
2277 nfc->write_by_sram = of_property_read_bool(
2278 pdev->dev.of_node,
2279 "atmel,write-by-sram");
Josh Wu1ae9c092013-08-05 19:14:36 +08002280 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002281 }
2282
Wu, Josh50e04e22014-06-10 17:50:09 +08002283 nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
2284 nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */
2285
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002286 nfc->clk = devm_clk_get(&pdev->dev, NULL);
2287 if (!IS_ERR(nfc->clk)) {
2288 ret = clk_prepare_enable(nfc->clk);
2289 if (ret)
2290 return ret;
2291 } else {
2292 dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree");
2293 }
2294
Josh Wu7dc37de2013-08-05 19:14:35 +08002295 nfc->is_initialized = true;
2296 dev_info(&pdev->dev, "NFC is probed.\n");
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002297
2298 return 0;
2299}
2300
2301static int atmel_nand_nfc_remove(struct platform_device *pdev)
2302{
2303 struct atmel_nfc *nfc = &nand_nfc;
2304
2305 if (!IS_ERR(nfc->clk))
2306 clk_disable_unprepare(nfc->clk);
2307
Josh Wu7dc37de2013-08-05 19:14:35 +08002308 return 0;
2309}
2310
Josh Wu81f29b42013-09-18 11:31:20 +08002311static const struct of_device_id atmel_nand_nfc_match[] = {
Josh Wu7dc37de2013-08-05 19:14:35 +08002312 { .compatible = "atmel,sama5d3-nfc" },
2313 { /* sentinel */ }
2314};
Josh Wu81f29b42013-09-18 11:31:20 +08002315MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
Josh Wu7dc37de2013-08-05 19:14:35 +08002316
2317static struct platform_driver atmel_nand_nfc_driver = {
2318 .driver = {
2319 .name = "atmel_nand_nfc",
2320 .owner = THIS_MODULE,
2321 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2322 },
2323 .probe = atmel_nand_nfc_probe,
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002324 .remove = atmel_nand_nfc_remove,
Josh Wu7dc37de2013-08-05 19:14:35 +08002325};
2326
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002327static struct platform_driver atmel_nand_driver = {
Johan Hovold2c2b9282013-09-23 16:27:28 +02002328 .probe = atmel_nand_probe,
2329 .remove = atmel_nand_remove,
Andrew Victor42cb1402006-10-19 18:24:35 +02002330 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002331 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +02002332 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002333 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02002334 },
2335};
2336
Johan Hovold2c2b9282013-09-23 16:27:28 +02002337module_platform_driver(atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02002338
2339MODULE_LICENSE("GPL");
2340MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02002341MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002342MODULE_ALIAS("platform:atmel_nand");