blob: 5c76704f4080c0db1ec09c58ffb6947ef76f2153 [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;
Josh Wu1c7b8742012-06-29 17:47:55 +0800135 int pmecc_degree; /* Degree of remainders */
136 int pmecc_cw_len; /* Length of codeword */
137
138 void __iomem *pmerrloc_base;
139 void __iomem *pmecc_rom_base;
140
141 /* lookup table for alpha_to and index_of */
142 void __iomem *pmecc_alpha_to;
143 void __iomem *pmecc_index_of;
144
145 /* data for pmecc computation */
146 int16_t *pmecc_partial_syn;
147 int16_t *pmecc_si;
148 int16_t *pmecc_smu; /* Sigma table */
149 int16_t *pmecc_lmu; /* polynomal order */
150 int *pmecc_mu;
151 int *pmecc_dmu;
152 int *pmecc_delta;
Andrew Victor42cb1402006-10-19 18:24:35 +0200153};
154
Josh Wu1c7b8742012-06-29 17:47:55 +0800155static struct nand_ecclayout atmel_pmecc_oobinfo;
156
Andrew Victor42cb1402006-10-19 18:24:35 +0200157/*
Atsushi Nemoto81365082008-04-27 01:51:12 +0900158 * Enable NAND.
159 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200160static void atmel_nand_enable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900161{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800162 if (gpio_is_valid(host->board.enable_pin))
163 gpio_set_value(host->board.enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900164}
165
166/*
167 * Disable NAND.
168 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200169static void atmel_nand_disable(struct atmel_nand_host *host)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900170{
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800171 if (gpio_is_valid(host->board.enable_pin))
172 gpio_set_value(host->board.enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900173}
174
175/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200176 * Hardware specific access to control-lines
177 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200178static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Andrew Victor42cb1402006-10-19 18:24:35 +0200179{
180 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200181 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200182
Atsushi Nemoto81365082008-04-27 01:51:12 +0900183 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900184 if (ctrl & NAND_NCE)
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200185 atmel_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900186 else
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200187 atmel_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900188 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200189 if (cmd == NAND_CMD_NONE)
190 return;
191
192 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800193 writeb(cmd, host->io_base + (1 << host->board.cle));
Andrew Victor42cb1402006-10-19 18:24:35 +0200194 else
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800195 writeb(cmd, host->io_base + (1 << host->board.ale));
Andrew Victor42cb1402006-10-19 18:24:35 +0200196}
197
198/*
199 * Read the Device Ready pin.
200 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200201static int atmel_nand_device_ready(struct mtd_info *mtd)
Andrew Victor42cb1402006-10-19 18:24:35 +0200202{
203 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +0200204 struct atmel_nand_host *host = nand_chip->priv;
Andrew Victor42cb1402006-10-19 18:24:35 +0200205
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800206 return gpio_get_value(host->board.rdy_pin) ^
207 !!host->board.rdy_pin_active_low;
Andrew Victor42cb1402006-10-19 18:24:35 +0200208}
209
Josh Wu7dc37de2013-08-05 19:14:35 +0800210/* Set up for hardware ready pin and enable pin. */
211static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
212{
213 struct nand_chip *chip = mtd->priv;
214 struct atmel_nand_host *host = chip->priv;
215 int res = 0;
216
217 if (gpio_is_valid(host->board.rdy_pin)) {
218 res = devm_gpio_request(host->dev,
219 host->board.rdy_pin, "nand_rdy");
220 if (res < 0) {
221 dev_err(host->dev,
222 "can't request rdy gpio %d\n",
223 host->board.rdy_pin);
224 return res;
225 }
226
227 res = gpio_direction_input(host->board.rdy_pin);
228 if (res < 0) {
229 dev_err(host->dev,
230 "can't request input direction rdy gpio %d\n",
231 host->board.rdy_pin);
232 return res;
233 }
234
235 chip->dev_ready = atmel_nand_device_ready;
236 }
237
238 if (gpio_is_valid(host->board.enable_pin)) {
239 res = devm_gpio_request(host->dev,
240 host->board.enable_pin, "nand_enable");
241 if (res < 0) {
242 dev_err(host->dev,
243 "can't request enable gpio %d\n",
244 host->board.enable_pin);
245 return res;
246 }
247
248 res = gpio_direction_output(host->board.enable_pin, 1);
249 if (res < 0) {
250 dev_err(host->dev,
251 "can't request output direction enable gpio %d\n",
252 host->board.enable_pin);
253 return res;
254 }
255 }
256
257 return res;
258}
259
Josh Wu1ae9c092013-08-05 19:14:36 +0800260static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size)
261{
262 int i;
263 u32 *t = trg;
264 const __iomem u32 *s = src;
265
266 for (i = 0; i < (size >> 2); i++)
267 *t++ = readl_relaxed(s++);
268}
269
Josh Wu6054d4d2013-08-05 19:14:37 +0800270static void memcpy32_toio(void __iomem *trg, const void *src, int size)
271{
272 int i;
273 u32 __iomem *t = trg;
274 const u32 *s = src;
275
276 for (i = 0; i < (size >> 2); i++)
277 writel_relaxed(*s++, t++);
278}
279
Artem Bityutskiy50082312012-02-02 13:54:25 +0200280/*
281 * Minimal-overhead PIO for data access.
282 */
283static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
284{
285 struct nand_chip *nand_chip = mtd->priv;
Josh Wu1ae9c092013-08-05 19:14:36 +0800286 struct atmel_nand_host *host = nand_chip->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200287
Josh Wu1ae9c092013-08-05 19:14:36 +0800288 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
289 memcpy32_fromio(buf, host->nfc->data_in_sram, len);
290 host->nfc->data_in_sram += len;
291 } else {
292 __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
293 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200294}
295
296static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
297{
298 struct nand_chip *nand_chip = mtd->priv;
Josh Wu1ae9c092013-08-05 19:14:36 +0800299 struct atmel_nand_host *host = nand_chip->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200300
Josh Wu1ae9c092013-08-05 19:14:36 +0800301 if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
302 memcpy32_fromio(buf, host->nfc->data_in_sram, len);
303 host->nfc->data_in_sram += len;
304 } else {
305 __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
306 }
Artem Bityutskiy50082312012-02-02 13:54:25 +0200307}
308
309static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
310{
311 struct nand_chip *nand_chip = mtd->priv;
312
313 __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
314}
315
316static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
317{
318 struct nand_chip *nand_chip = mtd->priv;
319
320 __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
321}
322
Hong Xucbc6c5e2011-01-18 14:36:05 +0800323static void dma_complete_func(void *completion)
324{
325 complete(completion);
326}
327
Josh Wu1ae9c092013-08-05 19:14:36 +0800328static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
329{
330 /* NFC only has two banks. Must be 0 or 1 */
331 if (bank > 1)
332 return -EINVAL;
333
334 if (bank) {
335 /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
336 if (host->mtd.writesize > 2048)
337 return -EINVAL;
338 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
339 } else {
340 nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
341 }
342
343 return 0;
344}
345
346static uint nfc_get_sram_off(struct atmel_nand_host *host)
347{
348 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
349 return NFC_SRAM_BANK1_OFFSET;
350 else
351 return 0;
352}
353
354static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
355{
356 if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
357 return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
358 else
359 return host->nfc->sram_bank0_phys;
360}
361
Hong Xucbc6c5e2011-01-18 14:36:05 +0800362static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
363 int is_read)
364{
365 struct dma_device *dma_dev;
366 enum dma_ctrl_flags flags;
367 dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
368 struct dma_async_tx_descriptor *tx = NULL;
369 dma_cookie_t cookie;
370 struct nand_chip *chip = mtd->priv;
371 struct atmel_nand_host *host = chip->priv;
372 void *p = buf;
373 int err = -EIO;
374 enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Josh Wu1ae9c092013-08-05 19:14:36 +0800375 struct atmel_nfc *nfc = host->nfc;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800376
Hong Xu80b4f812011-03-31 18:33:15 +0800377 if (buf >= high_memory)
378 goto err_buf;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800379
380 dma_dev = host->dma_chan->device;
381
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200382 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800383
384 phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
385 if (dma_mapping_error(dma_dev->dev, phys_addr)) {
386 dev_err(host->dev, "Failed to dma_map_single\n");
387 goto err_buf;
388 }
389
390 if (is_read) {
Josh Wu1ae9c092013-08-05 19:14:36 +0800391 if (nfc && nfc->data_in_sram)
392 dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
393 - (nfc->sram_bank0 + nfc_get_sram_off(host)));
394 else
395 dma_src_addr = host->io_phys;
396
Hong Xucbc6c5e2011-01-18 14:36:05 +0800397 dma_dst_addr = phys_addr;
398 } else {
399 dma_src_addr = phys_addr;
Josh Wu6054d4d2013-08-05 19:14:37 +0800400
401 if (nfc && nfc->write_by_sram)
402 dma_dst_addr = nfc_sram_phys(host);
403 else
404 dma_dst_addr = host->io_phys;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800405 }
406
407 tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
408 dma_src_addr, len, flags);
409 if (!tx) {
410 dev_err(host->dev, "Failed to prepare DMA memcpy\n");
411 goto err_dma;
412 }
413
414 init_completion(&host->comp);
415 tx->callback = dma_complete_func;
416 tx->callback_param = &host->comp;
417
418 cookie = tx->tx_submit(tx);
419 if (dma_submit_error(cookie)) {
420 dev_err(host->dev, "Failed to do DMA tx_submit\n");
421 goto err_dma;
422 }
423
424 dma_async_issue_pending(host->dma_chan);
425 wait_for_completion(&host->comp);
426
Josh Wu1ae9c092013-08-05 19:14:36 +0800427 if (is_read && nfc && nfc->data_in_sram)
428 /* After read data from SRAM, need to increase the position */
429 nfc->data_in_sram += len;
430
Hong Xucbc6c5e2011-01-18 14:36:05 +0800431 err = 0;
432
433err_dma:
434 dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
435err_buf:
436 if (err != 0)
Nicolas Ferre74414a942014-02-12 12:26:54 +0100437 dev_dbg(host->dev, "Fall back to CPU I/O\n");
Hong Xucbc6c5e2011-01-18 14:36:05 +0800438 return err;
439}
440
441static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
442{
443 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200444 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800445
Nicolas Ferre9d515672011-04-01 16:40:44 +0200446 if (use_dma && len > mtd->oobsize)
447 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800448 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
449 return;
450
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800451 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200452 atmel_read_buf16(mtd, buf, len);
453 else
454 atmel_read_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800455}
456
457static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
458{
459 struct nand_chip *chip = mtd->priv;
Artem Bityutskiy50082312012-02-02 13:54:25 +0200460 struct atmel_nand_host *host = chip->priv;
Hong Xucbc6c5e2011-01-18 14:36:05 +0800461
Nicolas Ferre9d515672011-04-01 16:40:44 +0200462 if (use_dma && len > mtd->oobsize)
463 /* only use DMA for bigger than oob size: better performances */
Hong Xucbc6c5e2011-01-18 14:36:05 +0800464 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
465 return;
466
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +0800467 if (host->board.bus_width_16)
Artem Bityutskiy50082312012-02-02 13:54:25 +0200468 atmel_write_buf16(mtd, buf, len);
469 else
470 atmel_write_buf8(mtd, buf, len);
Hong Xucbc6c5e2011-01-18 14:36:05 +0800471}
472
David Brownell23a346c2008-07-03 23:40:16 -0700473/*
Josh Wu1c7b8742012-06-29 17:47:55 +0800474 * Return number of ecc bytes per sector according to sector size and
475 * correction capability
476 *
477 * Following table shows what at91 PMECC supported:
478 * Correction Capability Sector_512_bytes Sector_1024_bytes
479 * ===================== ================ =================
480 * 2-bits 4-bytes 4-bytes
481 * 4-bits 7-bytes 7-bytes
482 * 8-bits 13-bytes 14-bytes
483 * 12-bits 20-bytes 21-bytes
484 * 24-bits 39-bytes 42-bytes
485 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500486static int pmecc_get_ecc_bytes(int cap, int sector_size)
Josh Wu1c7b8742012-06-29 17:47:55 +0800487{
488 int m = 12 + sector_size / 512;
489 return (m * cap + 7) / 8;
490}
491
Bill Pemberton06f25512012-11-19 13:23:07 -0500492static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800493 int oobsize, int ecc_len)
Josh Wu1c7b8742012-06-29 17:47:55 +0800494{
495 int i;
496
497 layout->eccbytes = ecc_len;
498
499 /* ECC will occupy the last ecc_len bytes continuously */
500 for (i = 0; i < ecc_len; i++)
501 layout->eccpos[i] = oobsize - ecc_len + i;
502
503 layout->oobfree[0].offset = 2;
504 layout->oobfree[0].length =
505 oobsize - ecc_len - layout->oobfree[0].offset;
506}
507
Bill Pemberton06f25512012-11-19 13:23:07 -0500508static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800509{
510 int table_size;
511
512 table_size = host->pmecc_sector_size == 512 ?
513 PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
514
515 return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
516 table_size * sizeof(int16_t);
517}
518
Bill Pemberton06f25512012-11-19 13:23:07 -0500519static int pmecc_data_alloc(struct atmel_nand_host *host)
Josh Wu1c7b8742012-06-29 17:47:55 +0800520{
521 const int cap = host->pmecc_corr_cap;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800522 int size;
Josh Wu1c7b8742012-06-29 17:47:55 +0800523
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800524 size = (2 * cap + 1) * sizeof(int16_t);
525 host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
526 host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
527 host->pmecc_lmu = devm_kzalloc(host->dev,
528 (cap + 1) * sizeof(int16_t), GFP_KERNEL);
529 host->pmecc_smu = devm_kzalloc(host->dev,
530 (cap + 2) * size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800531
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800532 size = (cap + 1) * sizeof(int);
533 host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
534 host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
535 host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
Josh Wu1c7b8742012-06-29 17:47:55 +0800536
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +0800537 if (!host->pmecc_partial_syn ||
538 !host->pmecc_si ||
539 !host->pmecc_lmu ||
540 !host->pmecc_smu ||
541 !host->pmecc_mu ||
542 !host->pmecc_dmu ||
543 !host->pmecc_delta)
544 return -ENOMEM;
545
546 return 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800547}
548
549static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
550{
551 struct nand_chip *nand_chip = mtd->priv;
552 struct atmel_nand_host *host = nand_chip->priv;
553 int i;
554 uint32_t value;
555
556 /* Fill odd syndromes */
557 for (i = 0; i < host->pmecc_corr_cap; i++) {
558 value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
559 if (i & 1)
560 value >>= 16;
561 value &= 0xffff;
562 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
563 }
564}
565
566static void pmecc_substitute(struct mtd_info *mtd)
567{
568 struct nand_chip *nand_chip = mtd->priv;
569 struct atmel_nand_host *host = nand_chip->priv;
570 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
571 int16_t __iomem *index_of = host->pmecc_index_of;
572 int16_t *partial_syn = host->pmecc_partial_syn;
573 const int cap = host->pmecc_corr_cap;
574 int16_t *si;
575 int i, j;
576
577 /* si[] is a table that holds the current syndrome value,
578 * an element of that table belongs to the field
579 */
580 si = host->pmecc_si;
581
582 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
583
584 /* Computation 2t syndromes based on S(x) */
585 /* Odd syndromes */
586 for (i = 1; i < 2 * cap; i += 2) {
587 for (j = 0; j < host->pmecc_degree; j++) {
588 if (partial_syn[i] & ((unsigned short)0x1 << j))
589 si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
590 }
591 }
592 /* Even syndrome = (Odd syndrome) ** 2 */
593 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
594 if (si[j] == 0) {
595 si[i] = 0;
596 } else {
597 int16_t tmp;
598
599 tmp = readw_relaxed(index_of + si[j]);
600 tmp = (tmp * 2) % host->pmecc_cw_len;
601 si[i] = readw_relaxed(alpha_to + tmp);
602 }
603 }
604
605 return;
606}
607
608static void pmecc_get_sigma(struct mtd_info *mtd)
609{
610 struct nand_chip *nand_chip = mtd->priv;
611 struct atmel_nand_host *host = nand_chip->priv;
612
613 int16_t *lmu = host->pmecc_lmu;
614 int16_t *si = host->pmecc_si;
615 int *mu = host->pmecc_mu;
616 int *dmu = host->pmecc_dmu; /* Discrepancy */
617 int *delta = host->pmecc_delta; /* Delta order */
618 int cw_len = host->pmecc_cw_len;
619 const int16_t cap = host->pmecc_corr_cap;
620 const int num = 2 * cap + 1;
621 int16_t __iomem *index_of = host->pmecc_index_of;
622 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
623 int i, j, k;
624 uint32_t dmu_0_count, tmp;
625 int16_t *smu = host->pmecc_smu;
626
627 /* index of largest delta */
628 int ro;
629 int largest;
630 int diff;
631
632 dmu_0_count = 0;
633
634 /* First Row */
635
636 /* Mu */
637 mu[0] = -1;
638
639 memset(smu, 0, sizeof(int16_t) * num);
640 smu[0] = 1;
641
642 /* discrepancy set to 1 */
643 dmu[0] = 1;
644 /* polynom order set to 0 */
645 lmu[0] = 0;
646 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
647
648 /* Second Row */
649
650 /* Mu */
651 mu[1] = 0;
652 /* Sigma(x) set to 1 */
653 memset(&smu[num], 0, sizeof(int16_t) * num);
654 smu[num] = 1;
655
656 /* discrepancy set to S1 */
657 dmu[1] = si[1];
658
659 /* polynom order set to 0 */
660 lmu[1] = 0;
661
662 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
663
664 /* Init the Sigma(x) last row */
665 memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
666
667 for (i = 1; i <= cap; i++) {
668 mu[i + 1] = i << 1;
669 /* Begin Computing Sigma (Mu+1) and L(mu) */
670 /* check if discrepancy is set to 0 */
671 if (dmu[i] == 0) {
672 dmu_0_count++;
673
674 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
675 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
676 tmp += 2;
677 else
678 tmp += 1;
679
680 if (dmu_0_count == tmp) {
681 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
682 smu[(cap + 1) * num + j] =
683 smu[i * num + j];
684
685 lmu[cap + 1] = lmu[i];
686 return;
687 }
688
689 /* copy polynom */
690 for (j = 0; j <= lmu[i] >> 1; j++)
691 smu[(i + 1) * num + j] = smu[i * num + j];
692
693 /* copy previous polynom order to the next */
694 lmu[i + 1] = lmu[i];
695 } else {
696 ro = 0;
697 largest = -1;
698 /* find largest delta with dmu != 0 */
699 for (j = 0; j < i; j++) {
700 if ((dmu[j]) && (delta[j] > largest)) {
701 largest = delta[j];
702 ro = j;
703 }
704 }
705
706 /* compute difference */
707 diff = (mu[i] - mu[ro]);
708
709 /* Compute degree of the new smu polynomial */
710 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
711 lmu[i + 1] = lmu[i];
712 else
713 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
714
715 /* Init smu[i+1] with 0 */
716 for (k = 0; k < num; k++)
717 smu[(i + 1) * num + k] = 0;
718
719 /* Compute smu[i+1] */
720 for (k = 0; k <= lmu[ro] >> 1; k++) {
721 int16_t a, b, c;
722
723 if (!(smu[ro * num + k] && dmu[i]))
724 continue;
725 a = readw_relaxed(index_of + dmu[i]);
726 b = readw_relaxed(index_of + dmu[ro]);
727 c = readw_relaxed(index_of + smu[ro * num + k]);
728 tmp = a + (cw_len - b) + c;
729 a = readw_relaxed(alpha_to + tmp % cw_len);
730 smu[(i + 1) * num + (k + diff)] = a;
731 }
732
733 for (k = 0; k <= lmu[i] >> 1; k++)
734 smu[(i + 1) * num + k] ^= smu[i * num + k];
735 }
736
737 /* End Computing Sigma (Mu+1) and L(mu) */
738 /* In either case compute delta */
739 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
740
741 /* Do not compute discrepancy for the last iteration */
742 if (i >= cap)
743 continue;
744
745 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
746 tmp = 2 * (i - 1);
747 if (k == 0) {
748 dmu[i + 1] = si[tmp + 3];
749 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
750 int16_t a, b, c;
751 a = readw_relaxed(index_of +
752 smu[(i + 1) * num + k]);
753 b = si[2 * (i - 1) + 3 - k];
754 c = readw_relaxed(index_of + b);
755 tmp = a + c;
756 tmp %= cw_len;
757 dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
758 dmu[i + 1];
759 }
760 }
761 }
762
763 return;
764}
765
766static int pmecc_err_location(struct mtd_info *mtd)
767{
768 struct nand_chip *nand_chip = mtd->priv;
769 struct atmel_nand_host *host = nand_chip->priv;
770 unsigned long end_time;
771 const int cap = host->pmecc_corr_cap;
772 const int num = 2 * cap + 1;
773 int sector_size = host->pmecc_sector_size;
774 int err_nbr = 0; /* number of error */
775 int roots_nbr; /* number of roots */
776 int i;
777 uint32_t val;
778 int16_t *smu = host->pmecc_smu;
779
780 pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
781
782 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
783 pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
784 smu[(cap + 1) * num + i]);
785 err_nbr++;
786 }
787
788 val = (err_nbr - 1) << 16;
789 if (sector_size == 1024)
790 val |= 1;
791
792 pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
793 pmerrloc_writel(host->pmerrloc_base, ELEN,
794 sector_size * 8 + host->pmecc_degree * cap);
795
796 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
797 while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
798 & PMERRLOC_CALC_DONE)) {
799 if (unlikely(time_after(jiffies, end_time))) {
800 dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
801 return -1;
802 }
803 cpu_relax();
804 }
805
806 roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
807 & PMERRLOC_ERR_NUM_MASK) >> 8;
808 /* Number of roots == degree of smu hence <= cap */
809 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
810 return err_nbr - 1;
811
812 /* Number of roots does not match the degree of smu
813 * unable to correct error */
814 return -1;
815}
816
817static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
818 int sector_num, int extra_bytes, int err_nbr)
819{
820 struct nand_chip *nand_chip = mtd->priv;
821 struct atmel_nand_host *host = nand_chip->priv;
822 int i = 0;
823 int byte_pos, bit_pos, sector_size, pos;
824 uint32_t tmp;
825 uint8_t err_byte;
826
827 sector_size = host->pmecc_sector_size;
828
829 while (err_nbr) {
830 tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
831 byte_pos = tmp / 8;
832 bit_pos = tmp % 8;
833
834 if (byte_pos >= (sector_size + extra_bytes))
835 BUG(); /* should never happen */
836
837 if (byte_pos < sector_size) {
838 err_byte = *(buf + byte_pos);
839 *(buf + byte_pos) ^= (1 << bit_pos);
840
841 pos = sector_num * host->pmecc_sector_size + byte_pos;
842 dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
843 pos, bit_pos, err_byte, *(buf + byte_pos));
844 } else {
845 /* Bit flip in OOB area */
846 tmp = sector_num * host->pmecc_bytes_per_sector
847 + (byte_pos - sector_size);
848 err_byte = ecc[tmp];
849 ecc[tmp] ^= (1 << bit_pos);
850
851 pos = tmp + nand_chip->ecc.layout->eccpos[0];
852 dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
853 pos, bit_pos, err_byte, ecc[tmp]);
854 }
855
856 i++;
857 err_nbr--;
858 }
859
860 return;
861}
862
863static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
864 u8 *ecc)
865{
866 struct nand_chip *nand_chip = mtd->priv;
867 struct atmel_nand_host *host = nand_chip->priv;
Bo Shenb3857662014-06-12 15:58:45 +0800868 int i, err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800869 uint8_t *buf_pos;
Josh Wuc0c70d92012-11-27 18:50:31 +0800870 int total_err = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800871
Bo Shenb3857662014-06-12 15:58:45 +0800872 for (i = 0; i < nand_chip->ecc.total; i++)
Josh Wu1c7b8742012-06-29 17:47:55 +0800873 if (ecc[i] != 0xff)
874 goto normal_check;
875 /* Erased page, return OK */
876 return 0;
877
878normal_check:
Wu, Joshc9447ff2014-08-08 17:12:34 +0800879 for (i = 0; i < nand_chip->ecc.steps; i++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800880 err_nbr = 0;
881 if (pmecc_stat & 0x1) {
882 buf_pos = buf + i * host->pmecc_sector_size;
883
884 pmecc_gen_syndrome(mtd, i);
885 pmecc_substitute(mtd);
886 pmecc_get_sigma(mtd);
887
888 err_nbr = pmecc_err_location(mtd);
889 if (err_nbr == -1) {
890 dev_err(host->dev, "PMECC: Too many errors\n");
891 mtd->ecc_stats.failed++;
892 return -EIO;
893 } else {
894 pmecc_correct_data(mtd, buf_pos, ecc, i,
895 host->pmecc_bytes_per_sector, err_nbr);
896 mtd->ecc_stats.corrected += err_nbr;
Josh Wuc0c70d92012-11-27 18:50:31 +0800897 total_err += err_nbr;
Josh Wu1c7b8742012-06-29 17:47:55 +0800898 }
899 }
900 pmecc_stat >>= 1;
901 }
902
Josh Wuc0c70d92012-11-27 18:50:31 +0800903 return total_err;
Josh Wu1c7b8742012-06-29 17:47:55 +0800904}
905
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800906static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
907{
908 u32 val;
909
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800910 if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
911 dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
912 return;
913 }
914
Josh Wu1fad0e82013-08-07 17:58:11 +0800915 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
916 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
917 val = pmecc_readl_relaxed(host->ecc, CFG);
918
Josh Wu5ee3d9d2013-08-05 19:14:34 +0800919 if (ecc_op == NAND_ECC_READ)
920 pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
921 | PMECC_CFG_AUTO_ENABLE);
922 else
923 pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
924 & ~PMECC_CFG_AUTO_ENABLE);
925
926 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
927 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
928}
929
Josh Wu1c7b8742012-06-29 17:47:55 +0800930static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
931 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
932{
933 struct atmel_nand_host *host = chip->priv;
Bo Shenb3857662014-06-12 15:58:45 +0800934 int eccsize = chip->ecc.size * chip->ecc.steps;
Josh Wu1c7b8742012-06-29 17:47:55 +0800935 uint8_t *oob = chip->oob_poi;
936 uint32_t *eccpos = chip->ecc.layout->eccpos;
937 uint32_t stat;
938 unsigned long end_time;
Josh Wuc0c70d92012-11-27 18:50:31 +0800939 int bitflips = 0;
Josh Wu1c7b8742012-06-29 17:47:55 +0800940
Josh Wu1ae9c092013-08-05 19:14:36 +0800941 if (!host->nfc || !host->nfc->use_nfc_sram)
942 pmecc_enable(host, NAND_ECC_READ);
Josh Wu1c7b8742012-06-29 17:47:55 +0800943
944 chip->read_buf(mtd, buf, eccsize);
945 chip->read_buf(mtd, oob, mtd->oobsize);
946
947 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
948 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
949 if (unlikely(time_after(jiffies, end_time))) {
950 dev_err(host->dev, "PMECC: Timeout to get error status.\n");
951 return -EIO;
952 }
953 cpu_relax();
954 }
955
956 stat = pmecc_readl_relaxed(host->ecc, ISR);
Josh Wuc0c70d92012-11-27 18:50:31 +0800957 if (stat != 0) {
958 bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
959 if (bitflips < 0)
960 /* uncorrectable errors */
961 return 0;
962 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800963
Josh Wuc0c70d92012-11-27 18:50:31 +0800964 return bitflips;
Josh Wu1c7b8742012-06-29 17:47:55 +0800965}
966
967static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
968 struct nand_chip *chip, const uint8_t *buf, int oob_required)
969{
970 struct atmel_nand_host *host = chip->priv;
971 uint32_t *eccpos = chip->ecc.layout->eccpos;
972 int i, j;
973 unsigned long end_time;
974
Josh Wu6054d4d2013-08-05 19:14:37 +0800975 if (!host->nfc || !host->nfc->write_by_sram) {
976 pmecc_enable(host, NAND_ECC_WRITE);
977 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
978 }
Josh Wu1c7b8742012-06-29 17:47:55 +0800979
980 end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
981 while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
982 if (unlikely(time_after(jiffies, end_time))) {
983 dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
984 return -EIO;
985 }
986 cpu_relax();
987 }
988
Wu, Joshc9447ff2014-08-08 17:12:34 +0800989 for (i = 0; i < chip->ecc.steps; i++) {
Josh Wu1c7b8742012-06-29 17:47:55 +0800990 for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
991 int pos;
992
993 pos = i * host->pmecc_bytes_per_sector + j;
994 chip->oob_poi[eccpos[pos]] =
995 pmecc_readb_ecc_relaxed(host->ecc, i, j);
996 }
997 }
998 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
999
1000 return 0;
1001}
1002
1003static void atmel_pmecc_core_init(struct mtd_info *mtd)
1004{
1005 struct nand_chip *nand_chip = mtd->priv;
1006 struct atmel_nand_host *host = nand_chip->priv;
1007 uint32_t val = 0;
1008 struct nand_ecclayout *ecc_layout;
1009
1010 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
1011 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
1012
1013 switch (host->pmecc_corr_cap) {
1014 case 2:
1015 val = PMECC_CFG_BCH_ERR2;
1016 break;
1017 case 4:
1018 val = PMECC_CFG_BCH_ERR4;
1019 break;
1020 case 8:
1021 val = PMECC_CFG_BCH_ERR8;
1022 break;
1023 case 12:
1024 val = PMECC_CFG_BCH_ERR12;
1025 break;
1026 case 24:
1027 val = PMECC_CFG_BCH_ERR24;
1028 break;
1029 }
1030
1031 if (host->pmecc_sector_size == 512)
1032 val |= PMECC_CFG_SECTOR512;
1033 else if (host->pmecc_sector_size == 1024)
1034 val |= PMECC_CFG_SECTOR1024;
1035
Wu, Joshc9447ff2014-08-08 17:12:34 +08001036 switch (nand_chip->ecc.steps) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001037 case 1:
1038 val |= PMECC_CFG_PAGE_1SECTOR;
1039 break;
1040 case 2:
1041 val |= PMECC_CFG_PAGE_2SECTORS;
1042 break;
1043 case 4:
1044 val |= PMECC_CFG_PAGE_4SECTORS;
1045 break;
1046 case 8:
1047 val |= PMECC_CFG_PAGE_8SECTORS;
1048 break;
1049 }
1050
1051 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
1052 | PMECC_CFG_AUTO_DISABLE);
1053 pmecc_writel(host->ecc, CFG, val);
1054
1055 ecc_layout = nand_chip->ecc.layout;
1056 pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
1057 pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
1058 pmecc_writel(host->ecc, EADDR,
1059 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
1060 /* See datasheet about PMECC Clock Control Register */
1061 pmecc_writel(host->ecc, CLK, 2);
1062 pmecc_writel(host->ecc, IDR, 0xff);
1063 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
1064}
1065
Josh Wu84cfbbb2013-01-23 20:47:12 +08001066/*
Josh Wu2a3d9332013-09-18 13:58:48 +08001067 * Get minimum ecc requirements from NAND.
Josh Wu84cfbbb2013-01-23 20:47:12 +08001068 * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
Josh Wu2a3d9332013-09-18 13:58:48 +08001069 * will set them according to minimum ecc requirement. Otherwise, use the
Josh Wu84cfbbb2013-01-23 20:47:12 +08001070 * value in DTS file.
1071 * return 0 if success. otherwise return error code.
1072 */
1073static int pmecc_choose_ecc(struct atmel_nand_host *host,
1074 int *cap, int *sector_size)
1075{
Josh Wu2a3d9332013-09-18 13:58:48 +08001076 /* Get minimum ECC requirements */
1077 if (host->nand_chip.ecc_strength_ds) {
1078 *cap = host->nand_chip.ecc_strength_ds;
1079 *sector_size = host->nand_chip.ecc_step_ds;
1080 dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n",
Josh Wu84cfbbb2013-01-23 20:47:12 +08001081 *cap, *sector_size);
Josh Wu84cfbbb2013-01-23 20:47:12 +08001082 } else {
Josh Wu84cfbbb2013-01-23 20:47:12 +08001083 *cap = 2;
1084 *sector_size = 512;
Josh Wu2a3d9332013-09-18 13:58:48 +08001085 dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
Josh Wu84cfbbb2013-01-23 20:47:12 +08001086 }
1087
Josh Wu2a3d9332013-09-18 13:58:48 +08001088 /* If device tree doesn't specify, use NAND's minimum ECC parameters */
Josh Wu84cfbbb2013-01-23 20:47:12 +08001089 if (host->pmecc_corr_cap == 0) {
1090 /* use the most fitable ecc bits (the near bigger one ) */
1091 if (*cap <= 2)
1092 host->pmecc_corr_cap = 2;
1093 else if (*cap <= 4)
1094 host->pmecc_corr_cap = 4;
Josh Wuedc9cba2013-07-03 17:56:19 +08001095 else if (*cap <= 8)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001096 host->pmecc_corr_cap = 8;
Josh Wuedc9cba2013-07-03 17:56:19 +08001097 else if (*cap <= 12)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001098 host->pmecc_corr_cap = 12;
Josh Wuedc9cba2013-07-03 17:56:19 +08001099 else if (*cap <= 24)
Josh Wu84cfbbb2013-01-23 20:47:12 +08001100 host->pmecc_corr_cap = 24;
1101 else
1102 return -EINVAL;
1103 }
1104 if (host->pmecc_sector_size == 0) {
1105 /* use the most fitable sector size (the near smaller one ) */
1106 if (*sector_size >= 1024)
1107 host->pmecc_sector_size = 1024;
1108 else if (*sector_size >= 512)
1109 host->pmecc_sector_size = 512;
1110 else
1111 return -EINVAL;
1112 }
1113 return 0;
1114}
1115
Johan Hovold2c2b9282013-09-23 16:27:28 +02001116static int atmel_pmecc_nand_init_params(struct platform_device *pdev,
Josh Wu1c7b8742012-06-29 17:47:55 +08001117 struct atmel_nand_host *host)
1118{
1119 struct mtd_info *mtd = &host->mtd;
1120 struct nand_chip *nand_chip = &host->nand_chip;
1121 struct resource *regs, *regs_pmerr, *regs_rom;
1122 int cap, sector_size, err_no;
1123
Josh Wu84cfbbb2013-01-23 20:47:12 +08001124 err_no = pmecc_choose_ecc(host, &cap, &sector_size);
1125 if (err_no) {
1126 dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
1127 return err_no;
1128 }
1129
Richard Genoudf666d642013-07-30 17:17:29 +02001130 if (cap > host->pmecc_corr_cap ||
Josh Wu84cfbbb2013-01-23 20:47:12 +08001131 sector_size != host->pmecc_sector_size)
1132 dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
Josh Wue66b4312013-01-23 20:47:11 +08001133
Josh Wu1c7b8742012-06-29 17:47:55 +08001134 cap = host->pmecc_corr_cap;
1135 sector_size = host->pmecc_sector_size;
Josh Wue66b4312013-01-23 20:47:11 +08001136 host->pmecc_lookup_table_offset = (sector_size == 512) ?
1137 host->pmecc_lookup_table_offset_512 :
1138 host->pmecc_lookup_table_offset_1024;
1139
Josh Wu1c7b8742012-06-29 17:47:55 +08001140 dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
1141 cap, sector_size);
1142
1143 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1144 if (!regs) {
1145 dev_warn(host->dev,
1146 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
1147 nand_chip->ecc.mode = NAND_ECC_SOFT;
1148 return 0;
1149 }
1150
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001151 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
1152 if (IS_ERR(host->ecc)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001153 err_no = PTR_ERR(host->ecc);
1154 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001155 }
1156
1157 regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001158 host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
1159 if (IS_ERR(host->pmerrloc_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001160 err_no = PTR_ERR(host->pmerrloc_base);
1161 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001162 }
1163
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001164 regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
1165 host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, regs_rom);
1166 if (IS_ERR(host->pmecc_rom_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001167 err_no = PTR_ERR(host->pmecc_rom_base);
1168 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001169 }
1170
Bo Shenb3857662014-06-12 15:58:45 +08001171 nand_chip->ecc.size = sector_size;
Josh Wu1c7b8742012-06-29 17:47:55 +08001172
1173 /* set ECC page size and oob layout */
1174 switch (mtd->writesize) {
Wu, Josha3557102014-07-22 17:24:18 +08001175 case 512:
1176 case 1024:
Josh Wu1c7b8742012-06-29 17:47:55 +08001177 case 2048:
Wu, Josha3557102014-07-22 17:24:18 +08001178 case 4096:
1179 case 8192:
1180 if (sector_size > mtd->writesize) {
1181 dev_err(host->dev, "pmecc sector size is bigger than the page size!\n");
1182 err_no = -EINVAL;
1183 goto err;
1184 }
1185
Josh Wu2fa831f2013-08-19 18:05:44 +08001186 host->pmecc_degree = (sector_size == 512) ?
1187 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Josh Wu1c7b8742012-06-29 17:47:55 +08001188 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
Josh Wu1c7b8742012-06-29 17:47:55 +08001189 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
1190 cap, sector_size);
1191 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
1192 host->pmecc_index_of = host->pmecc_rom_base +
1193 host->pmecc_lookup_table_offset;
1194
Josh Wu1c7b8742012-06-29 17:47:55 +08001195 nand_chip->ecc.strength = cap;
Bo Shenb3857662014-06-12 15:58:45 +08001196 nand_chip->ecc.bytes = host->pmecc_bytes_per_sector;
Wu, Joshc9447ff2014-08-08 17:12:34 +08001197 nand_chip->ecc.steps = mtd->writesize / sector_size;
1198 nand_chip->ecc.total = nand_chip->ecc.bytes *
1199 nand_chip->ecc.steps;
Bo Shenb3857662014-06-12 15:58:45 +08001200 if (nand_chip->ecc.total > mtd->oobsize - 2) {
Josh Wu1c7b8742012-06-29 17:47:55 +08001201 dev_err(host->dev, "No room for ECC bytes\n");
1202 err_no = -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001203 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001204 }
1205 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
1206 mtd->oobsize,
Bo Shenb3857662014-06-12 15:58:45 +08001207 nand_chip->ecc.total);
1208
Josh Wu1c7b8742012-06-29 17:47:55 +08001209 nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
1210 break;
Wu, Josha3557102014-07-22 17:24:18 +08001211 default:
Josh Wu1c7b8742012-06-29 17:47:55 +08001212 dev_warn(host->dev,
1213 "Unsupported page size for PMECC, use Software ECC\n");
Josh Wu1c7b8742012-06-29 17:47:55 +08001214 /* page size not handled by HW ECC */
1215 /* switching back to soft ECC */
1216 nand_chip->ecc.mode = NAND_ECC_SOFT;
1217 return 0;
1218 }
1219
1220 /* Allocate data for PMECC computation */
1221 err_no = pmecc_data_alloc(host);
1222 if (err_no) {
1223 dev_err(host->dev,
1224 "Cannot allocate memory for PMECC computation!\n");
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001225 goto err;
Josh Wu1c7b8742012-06-29 17:47:55 +08001226 }
1227
Herve Codina90445ff2014-03-03 12:15:29 +01001228 nand_chip->options |= NAND_NO_SUBPAGE_WRITE;
Josh Wu1c7b8742012-06-29 17:47:55 +08001229 nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
1230 nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
1231
1232 atmel_pmecc_core_init(mtd);
1233
1234 return 0;
1235
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001236err:
Josh Wu1c7b8742012-06-29 17:47:55 +08001237 return err_no;
1238}
1239
1240/*
Richard Genoud77f54922008-04-23 19:51:14 +02001241 * Calculate HW ECC
1242 *
1243 * function called after a write
1244 *
1245 * mtd: MTD block structure
1246 * dat: raw data (unused)
1247 * ecc_code: buffer for ECC
1248 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001249static int atmel_nand_calculate(struct mtd_info *mtd,
Richard Genoud77f54922008-04-23 19:51:14 +02001250 const u_char *dat, unsigned char *ecc_code)
1251{
1252 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001253 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001254 unsigned int ecc_value;
1255
1256 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +02001257 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +02001258
Richard Genoud3fc23892008-10-12 08:42:28 +02001259 ecc_code[0] = ecc_value & 0xFF;
1260 ecc_code[1] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001261
1262 /* get the last 2 ECC bytes */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001263 ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
Richard Genoud77f54922008-04-23 19:51:14 +02001264
Richard Genoud3fc23892008-10-12 08:42:28 +02001265 ecc_code[2] = ecc_value & 0xFF;
1266 ecc_code[3] = (ecc_value >> 8) & 0xFF;
Richard Genoud77f54922008-04-23 19:51:14 +02001267
1268 return 0;
1269}
1270
1271/*
1272 * HW ECC read page function
1273 *
1274 * mtd: mtd info structure
1275 * chip: nand chip info structure
1276 * buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001277 * oob_required: caller expects OOB data read to chip->oob_poi
Richard Genoud77f54922008-04-23 19:51:14 +02001278 */
Brian Norris1fbb9382012-05-02 10:14:55 -07001279static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1280 uint8_t *buf, int oob_required, int page)
Richard Genoud77f54922008-04-23 19:51:14 +02001281{
1282 int eccsize = chip->ecc.size;
1283 int eccbytes = chip->ecc.bytes;
1284 uint32_t *eccpos = chip->ecc.layout->eccpos;
1285 uint8_t *p = buf;
1286 uint8_t *oob = chip->oob_poi;
1287 uint8_t *ecc_pos;
1288 int stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001289 unsigned int max_bitflips = 0;
Richard Genoud77f54922008-04-23 19:51:14 +02001290
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001291 /*
1292 * Errata: ALE is incorrectly wired up to the ECC controller
1293 * on the AP7000, so it will include the address cycles in the
1294 * ECC calculation.
1295 *
1296 * Workaround: Reset the parity registers before reading the
1297 * actual data.
1298 */
Josh Wu71b94e22013-05-09 15:34:54 +08001299 struct atmel_nand_host *host = chip->priv;
1300 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001301 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001302
Richard Genoud77f54922008-04-23 19:51:14 +02001303 /* read the page */
1304 chip->read_buf(mtd, p, eccsize);
1305
1306 /* move to ECC position if needed */
1307 if (eccpos[0] != 0) {
1308 /* This only works on large pages
1309 * because the ECC controller waits for
1310 * NAND_CMD_RNDOUTSTART after the
1311 * NAND_CMD_RNDOUT.
1312 * anyway, for small pages, the eccpos[0] == 0
1313 */
1314 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1315 mtd->writesize + eccpos[0], -1);
1316 }
1317
1318 /* the ECC controller needs to read the ECC just after the data */
1319 ecc_pos = oob + eccpos[0];
1320 chip->read_buf(mtd, ecc_pos, eccbytes);
1321
1322 /* check if there's an error */
1323 stat = chip->ecc.correct(mtd, p, oob, NULL);
1324
Mike Dunn3f91e942012-04-25 12:06:09 -07001325 if (stat < 0) {
Richard Genoud77f54922008-04-23 19:51:14 +02001326 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001327 } else {
Richard Genoud77f54922008-04-23 19:51:14 +02001328 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001329 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1330 }
Richard Genoud77f54922008-04-23 19:51:14 +02001331
1332 /* get back to oob start (end of page) */
1333 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1334
1335 /* read the oob */
1336 chip->read_buf(mtd, oob, mtd->oobsize);
1337
Mike Dunn3f91e942012-04-25 12:06:09 -07001338 return max_bitflips;
Richard Genoud77f54922008-04-23 19:51:14 +02001339}
1340
1341/*
1342 * HW ECC Correction
1343 *
1344 * function called after a read
1345 *
1346 * mtd: MTD block structure
1347 * dat: raw data read from the chip
1348 * read_ecc: ECC from the chip (unused)
1349 * isnull: unused
1350 *
1351 * Detect and correct a 1 bit error for a page
1352 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001353static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
Richard Genoud77f54922008-04-23 19:51:14 +02001354 u_char *read_ecc, u_char *isnull)
1355{
1356 struct nand_chip *nand_chip = mtd->priv;
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001357 struct atmel_nand_host *host = nand_chip->priv;
Richard Genoud77f54922008-04-23 19:51:14 +02001358 unsigned int ecc_status;
1359 unsigned int ecc_word, ecc_bit;
1360
1361 /* get the status from the Status Register */
1362 ecc_status = ecc_readl(host->ecc, SR);
1363
1364 /* if there's no error */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001365 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
Richard Genoud77f54922008-04-23 19:51:14 +02001366 return 0;
1367
1368 /* get error bit offset (4 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001369 ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001370 /* get word address (12 bits) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001371 ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
Richard Genoud77f54922008-04-23 19:51:14 +02001372 ecc_word >>= 4;
1373
1374 /* if there are multiple errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001375 if (ecc_status & ATMEL_ECC_MULERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001376 /* check if it is a freshly erased block
1377 * (filled with 0xff) */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001378 if ((ecc_bit == ATMEL_ECC_BITADDR)
1379 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
Richard Genoud77f54922008-04-23 19:51:14 +02001380 /* the block has just been erased, return OK */
1381 return 0;
1382 }
1383 /* it doesn't seems to be a freshly
1384 * erased block.
1385 * We can't correct so many errors */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001386 dev_dbg(host->dev, "atmel_nand : multiple errors detected."
Richard Genoud77f54922008-04-23 19:51:14 +02001387 " Unable to correct.\n");
1388 return -EIO;
1389 }
1390
1391 /* if there's a single bit error : we can correct it */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001392 if (ecc_status & ATMEL_ECC_ECCERR) {
Richard Genoud77f54922008-04-23 19:51:14 +02001393 /* there's nothing much to do here.
1394 * the bit error is on the ECC itself.
1395 */
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001396 dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
Richard Genoud77f54922008-04-23 19:51:14 +02001397 " Nothing to correct\n");
1398 return 0;
1399 }
1400
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001401 dev_dbg(host->dev, "atmel_nand : one bit error on data."
Richard Genoud77f54922008-04-23 19:51:14 +02001402 " (word offset in the page :"
1403 " 0x%x bit offset : 0x%x)\n",
1404 ecc_word, ecc_bit);
1405 /* correct the error */
1406 if (nand_chip->options & NAND_BUSWIDTH_16) {
1407 /* 16 bits words */
1408 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1409 } else {
1410 /* 8 bits words */
1411 dat[ecc_word] ^= (1 << ecc_bit);
1412 }
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02001413 dev_dbg(host->dev, "atmel_nand : error corrected\n");
Richard Genoud77f54922008-04-23 19:51:14 +02001414 return 1;
1415}
1416
1417/*
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001418 * Enable HW ECC : unused on most chips
Richard Genoud77f54922008-04-23 19:51:14 +02001419 */
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001420static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1421{
Josh Wu71b94e22013-05-09 15:34:54 +08001422 struct nand_chip *nand_chip = mtd->priv;
1423 struct atmel_nand_host *host = nand_chip->priv;
1424
1425 if (host->board.need_reset_workaround)
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001426 ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
Haavard Skinnemoend6248fd2008-07-03 23:40:18 -07001427}
Richard Genoud77f54922008-04-23 19:51:14 +02001428
Bill Pemberton06f25512012-11-19 13:23:07 -05001429static int atmel_of_init_port(struct atmel_nand_host *host,
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -08001430 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001431{
Josh Wuc0cf7872013-01-23 20:47:08 +08001432 u32 val;
Josh Wua41b51a2012-06-29 17:47:54 +08001433 u32 offset[2];
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001434 int ecc_mode;
1435 struct atmel_nand_data *board = &host->board;
Josh Wue9d8da82013-09-18 11:31:19 +08001436 enum of_gpio_flags flags = 0;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001437
1438 if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
1439 if (val >= 32) {
1440 dev_err(host->dev, "invalid addr-offset %u\n", val);
1441 return -EINVAL;
1442 }
1443 board->ale = val;
1444 }
1445
1446 if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
1447 if (val >= 32) {
1448 dev_err(host->dev, "invalid cmd-offset %u\n", val);
1449 return -EINVAL;
1450 }
1451 board->cle = val;
1452 }
1453
1454 ecc_mode = of_get_nand_ecc_mode(np);
1455
1456 board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
1457
1458 board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
1459
Josh Wu1b719262013-05-09 15:34:55 +08001460 board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
1461
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001462 if (of_get_nand_bus_width(np) == 16)
1463 board->bus_width_16 = 1;
1464
1465 board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
1466 board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
1467
1468 board->enable_pin = of_get_gpio(np, 1);
1469 board->det_pin = of_get_gpio(np, 2);
1470
Josh Wua41b51a2012-06-29 17:47:54 +08001471 host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
1472
Josh Wu7dc37de2013-08-05 19:14:35 +08001473 /* load the nfc driver if there is */
1474 of_platform_populate(np, NULL, NULL, host->dev);
1475
Josh Wua41b51a2012-06-29 17:47:54 +08001476 if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
1477 return 0; /* Not using PMECC */
1478
1479 /* use PMECC, get correction capability, sector size and lookup
1480 * table offset.
Josh Wue66b4312013-01-23 20:47:11 +08001481 * If correction bits and sector size are not specified, then find
1482 * them from NAND ONFI parameters.
Josh Wua41b51a2012-06-29 17:47:54 +08001483 */
Josh Wue66b4312013-01-23 20:47:11 +08001484 if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
1485 if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1486 (val != 24)) {
1487 dev_err(host->dev,
1488 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1489 val);
1490 return -EINVAL;
1491 }
1492 host->pmecc_corr_cap = (u8)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001493 }
Josh Wua41b51a2012-06-29 17:47:54 +08001494
Josh Wue66b4312013-01-23 20:47:11 +08001495 if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
1496 if ((val != 512) && (val != 1024)) {
1497 dev_err(host->dev,
1498 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1499 val);
1500 return -EINVAL;
1501 }
1502 host->pmecc_sector_size = (u16)val;
Josh Wua41b51a2012-06-29 17:47:54 +08001503 }
Josh Wua41b51a2012-06-29 17:47:54 +08001504
1505 if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
1506 offset, 2) != 0) {
1507 dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
1508 return -EINVAL;
1509 }
Josh Wuc0cf7872013-01-23 20:47:08 +08001510 if (!offset[0] && !offset[1]) {
Josh Wua41b51a2012-06-29 17:47:54 +08001511 dev_err(host->dev, "Invalid PMECC lookup table offset\n");
1512 return -EINVAL;
1513 }
Josh Wue66b4312013-01-23 20:47:11 +08001514 host->pmecc_lookup_table_offset_512 = offset[0];
1515 host->pmecc_lookup_table_offset_1024 = offset[1];
Josh Wua41b51a2012-06-29 17:47:54 +08001516
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001517 return 0;
1518}
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08001519
Johan Hovold2c2b9282013-09-23 16:27:28 +02001520static int atmel_hw_nand_init_params(struct platform_device *pdev,
Josh Wu3dfe41a2012-06-25 18:07:43 +08001521 struct atmel_nand_host *host)
1522{
1523 struct mtd_info *mtd = &host->mtd;
1524 struct nand_chip *nand_chip = &host->nand_chip;
1525 struct resource *regs;
1526
1527 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1528 if (!regs) {
1529 dev_err(host->dev,
1530 "Can't get I/O resource regs, use software ECC\n");
1531 nand_chip->ecc.mode = NAND_ECC_SOFT;
1532 return 0;
1533 }
1534
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001535 host->ecc = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun8fb7b932014-07-28 21:19:55 +08001536 if (IS_ERR(host->ecc))
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08001537 return PTR_ERR(host->ecc);
Josh Wu3dfe41a2012-06-25 18:07:43 +08001538
1539 /* ECC is calculated for the whole page (1 step) */
1540 nand_chip->ecc.size = mtd->writesize;
1541
1542 /* set ECC page size and oob layout */
1543 switch (mtd->writesize) {
1544 case 512:
1545 nand_chip->ecc.layout = &atmel_oobinfo_small;
1546 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
1547 break;
1548 case 1024:
1549 nand_chip->ecc.layout = &atmel_oobinfo_large;
1550 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
1551 break;
1552 case 2048:
1553 nand_chip->ecc.layout = &atmel_oobinfo_large;
1554 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
1555 break;
1556 case 4096:
1557 nand_chip->ecc.layout = &atmel_oobinfo_large;
1558 ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
1559 break;
1560 default:
1561 /* page size not handled by HW ECC */
1562 /* switching back to soft ECC */
1563 nand_chip->ecc.mode = NAND_ECC_SOFT;
1564 return 0;
1565 }
1566
1567 /* set up for HW ECC */
1568 nand_chip->ecc.calculate = atmel_nand_calculate;
1569 nand_chip->ecc.correct = atmel_nand_correct;
1570 nand_chip->ecc.hwctl = atmel_nand_hwctl;
1571 nand_chip->ecc.read_page = atmel_nand_read_page;
1572 nand_chip->ecc.bytes = 4;
1573 nand_chip->ecc.strength = 1;
1574
1575 return 0;
1576}
1577
Wu, Josh50e04e22014-06-10 17:50:09 +08001578static inline u32 nfc_read_status(struct atmel_nand_host *host)
1579{
1580 u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
1581 u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
1582
1583 if (unlikely(nfc_status & err_flags)) {
1584 if (nfc_status & NFC_SR_DTOE)
1585 dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
1586 else if (nfc_status & NFC_SR_UNDEF)
1587 dev_err(host->dev, "NFC: Access Undefined Area Error\n");
1588 else if (nfc_status & NFC_SR_AWB)
1589 dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
1590 else if (nfc_status & NFC_SR_ASE)
1591 dev_err(host->dev, "NFC: Access memory Size Error\n");
1592 }
1593
1594 return nfc_status;
1595}
1596
Josh Wu7dc37de2013-08-05 19:14:35 +08001597/* SMC interrupt service routine */
1598static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
1599{
1600 struct atmel_nand_host *host = dev_id;
1601 u32 status, mask, pending;
Josh Wue4e06932014-06-10 17:50:11 +08001602 irqreturn_t ret = IRQ_NONE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001603
Wu, Josh50e04e22014-06-10 17:50:09 +08001604 status = nfc_read_status(host);
Josh Wu7dc37de2013-08-05 19:14:35 +08001605 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1606 pending = status & mask;
1607
1608 if (pending & NFC_SR_XFR_DONE) {
Josh Wue4e06932014-06-10 17:50:11 +08001609 complete(&host->nfc->comp_xfer_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001610 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001611 ret = IRQ_HANDLED;
1612 }
1613 if (pending & NFC_SR_RB_EDGE) {
1614 complete(&host->nfc->comp_ready);
Josh Wu7dc37de2013-08-05 19:14:35 +08001615 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_RB_EDGE);
Josh Wue4e06932014-06-10 17:50:11 +08001616 ret = IRQ_HANDLED;
1617 }
1618 if (pending & NFC_SR_CMD_DONE) {
1619 complete(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001620 nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
Josh Wue4e06932014-06-10 17:50:11 +08001621 ret = IRQ_HANDLED;
Josh Wu7dc37de2013-08-05 19:14:35 +08001622 }
1623
1624 return ret;
1625}
1626
1627/* NFC(Nand Flash Controller) related functions */
Josh Wue4e06932014-06-10 17:50:11 +08001628static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag)
Josh Wu7dc37de2013-08-05 19:14:35 +08001629{
Josh Wue4e06932014-06-10 17:50:11 +08001630 if (flag & NFC_SR_XFR_DONE)
1631 init_completion(&host->nfc->comp_xfer_done);
1632
1633 if (flag & NFC_SR_RB_EDGE)
1634 init_completion(&host->nfc->comp_ready);
1635
1636 if (flag & NFC_SR_CMD_DONE)
1637 init_completion(&host->nfc->comp_cmd_done);
Josh Wu7dc37de2013-08-05 19:14:35 +08001638
1639 /* Enable interrupt that need to wait for */
1640 nfc_writel(host->nfc->hsmc_regs, IER, flag);
Josh Wue4e06932014-06-10 17:50:11 +08001641}
Josh Wu7dc37de2013-08-05 19:14:35 +08001642
Josh Wue4e06932014-06-10 17:50:11 +08001643static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
1644{
1645 int i, index = 0;
1646 struct completion *comp[3]; /* Support 3 interrupt completion */
Josh Wu7dc37de2013-08-05 19:14:35 +08001647
Josh Wue4e06932014-06-10 17:50:11 +08001648 if (flag & NFC_SR_XFR_DONE)
1649 comp[index++] = &host->nfc->comp_xfer_done;
1650
1651 if (flag & NFC_SR_RB_EDGE)
1652 comp[index++] = &host->nfc->comp_ready;
1653
1654 if (flag & NFC_SR_CMD_DONE)
1655 comp[index++] = &host->nfc->comp_cmd_done;
1656
1657 if (index == 0) {
1658 dev_err(host->dev, "Unkown interrupt flag: 0x%08x\n", flag);
1659 return -EINVAL;
1660 }
1661
1662 for (i = 0; i < index; i++) {
1663 if (wait_for_completion_timeout(comp[i],
1664 msecs_to_jiffies(NFC_TIME_OUT_MS)))
1665 continue; /* wait for next completion */
1666 else
1667 goto err_timeout;
1668 }
1669
1670 return 0;
1671
1672err_timeout:
Josh Wu7dc37de2013-08-05 19:14:35 +08001673 dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
Josh Wue4e06932014-06-10 17:50:11 +08001674 /* Disable the interrupt as it is not handled by interrupt handler */
1675 nfc_writel(host->nfc->hsmc_regs, IDR, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001676 return -ETIMEDOUT;
1677}
1678
1679static int nfc_send_command(struct atmel_nand_host *host,
1680 unsigned int cmd, unsigned int addr, unsigned char cycle0)
1681{
1682 unsigned long timeout;
Josh Wue4e06932014-06-10 17:50:11 +08001683 u32 flag = NFC_SR_CMD_DONE;
1684 flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0;
1685
Josh Wu7dc37de2013-08-05 19:14:35 +08001686 dev_dbg(host->dev,
1687 "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
1688 cmd, addr, cycle0);
1689
1690 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1691 while (nfc_cmd_readl(NFCADDR_CMD_NFCBUSY, host->nfc->base_cmd_regs)
1692 & NFCADDR_CMD_NFCBUSY) {
1693 if (time_after(jiffies, timeout)) {
1694 dev_err(host->dev,
1695 "Time out to wait CMD_NFCBUSY ready!\n");
1696 return -ETIMEDOUT;
1697 }
1698 }
Josh Wue4e06932014-06-10 17:50:11 +08001699
1700 nfc_prepare_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001701 nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
1702 nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
Josh Wue4e06932014-06-10 17:50:11 +08001703 return nfc_wait_interrupt(host, flag);
Josh Wu7dc37de2013-08-05 19:14:35 +08001704}
1705
1706static int nfc_device_ready(struct mtd_info *mtd)
1707{
Wu, Josh72a78e32014-06-10 17:50:10 +08001708 u32 status, mask;
Josh Wu7dc37de2013-08-05 19:14:35 +08001709 struct nand_chip *nand_chip = mtd->priv;
1710 struct atmel_nand_host *host = nand_chip->priv;
Wu, Josh72a78e32014-06-10 17:50:10 +08001711
1712 status = nfc_read_status(host);
1713 mask = nfc_readl(host->nfc->hsmc_regs, IMR);
1714
1715 /* The mask should be 0. If not we may lost interrupts */
1716 if (unlikely(mask & status))
1717 dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n",
1718 mask & status);
1719
1720 return status & NFC_SR_RB_EDGE;
Josh Wu7dc37de2013-08-05 19:14:35 +08001721}
1722
1723static void nfc_select_chip(struct mtd_info *mtd, int chip)
1724{
1725 struct nand_chip *nand_chip = mtd->priv;
1726 struct atmel_nand_host *host = nand_chip->priv;
1727
1728 if (chip == -1)
1729 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
1730 else
1731 nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
1732}
1733
Brian Norris3dad2342014-01-29 14:08:12 -08001734static int nfc_make_addr(struct mtd_info *mtd, int command, int column,
1735 int page_addr, unsigned int *addr1234, unsigned int *cycle0)
Josh Wu7dc37de2013-08-05 19:14:35 +08001736{
1737 struct nand_chip *chip = mtd->priv;
1738
1739 int acycle = 0;
1740 unsigned char addr_bytes[8];
1741 int index = 0, bit_shift;
1742
1743 BUG_ON(addr1234 == NULL || cycle0 == NULL);
1744
1745 *cycle0 = 0;
1746 *addr1234 = 0;
1747
1748 if (column != -1) {
Brian Norris3dad2342014-01-29 14:08:12 -08001749 if (chip->options & NAND_BUSWIDTH_16 &&
1750 !nand_opcode_8bits(command))
Josh Wu7dc37de2013-08-05 19:14:35 +08001751 column >>= 1;
1752 addr_bytes[acycle++] = column & 0xff;
1753 if (mtd->writesize > 512)
1754 addr_bytes[acycle++] = (column >> 8) & 0xff;
1755 }
1756
1757 if (page_addr != -1) {
1758 addr_bytes[acycle++] = page_addr & 0xff;
1759 addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
1760 if (chip->chipsize > (128 << 20))
1761 addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
1762 }
1763
1764 if (acycle > 4)
1765 *cycle0 = addr_bytes[index++];
1766
1767 for (bit_shift = 0; index < acycle; bit_shift += 8)
1768 *addr1234 += addr_bytes[index++] << bit_shift;
1769
1770 /* return acycle in cmd register */
1771 return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
1772}
1773
1774static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
1775 int column, int page_addr)
1776{
1777 struct nand_chip *chip = mtd->priv;
1778 struct atmel_nand_host *host = chip->priv;
1779 unsigned long timeout;
1780 unsigned int nfc_addr_cmd = 0;
1781
1782 unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1783
1784 /* Set default settings: no cmd2, no addr cycle. read from nand */
1785 unsigned int cmd2 = 0;
1786 unsigned int vcmd2 = 0;
1787 int acycle = NFCADDR_CMD_ACYCLE_NONE;
1788 int csid = NFCADDR_CMD_CSID_3;
1789 int dataen = NFCADDR_CMD_DATADIS;
1790 int nfcwr = NFCADDR_CMD_NFCRD;
1791 unsigned int addr1234 = 0;
1792 unsigned int cycle0 = 0;
1793 bool do_addr = true;
Josh Wu1ae9c092013-08-05 19:14:36 +08001794 host->nfc->data_in_sram = NULL;
Josh Wu7dc37de2013-08-05 19:14:35 +08001795
1796 dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
1797 __func__, command, column, page_addr);
1798
1799 switch (command) {
1800 case NAND_CMD_RESET:
1801 nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
1802 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1803 udelay(chip->chip_delay);
1804
1805 nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
1806 timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
1807 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
1808 if (time_after(jiffies, timeout)) {
1809 dev_err(host->dev,
1810 "Time out to wait status ready!\n");
1811 break;
1812 }
1813 }
1814 return;
1815 case NAND_CMD_STATUS:
1816 do_addr = false;
1817 break;
1818 case NAND_CMD_PARAM:
1819 case NAND_CMD_READID:
1820 do_addr = false;
1821 acycle = NFCADDR_CMD_ACYCLE_1;
1822 if (column != -1)
1823 addr1234 = column;
1824 break;
1825 case NAND_CMD_RNDOUT:
1826 cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
1827 vcmd2 = NFCADDR_CMD_VCMD2;
1828 break;
1829 case NAND_CMD_READ0:
1830 case NAND_CMD_READOOB:
1831 if (command == NAND_CMD_READOOB) {
1832 column += mtd->writesize;
1833 command = NAND_CMD_READ0; /* only READ0 is valid */
1834 cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
1835 }
Josh Wu1ae9c092013-08-05 19:14:36 +08001836 if (host->nfc->use_nfc_sram) {
1837 /* Enable Data transfer to sram */
1838 dataen = NFCADDR_CMD_DATAEN;
1839
1840 /* Need enable PMECC now, since NFC will transfer
1841 * data in bus after sending nfc read command.
1842 */
1843 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1844 pmecc_enable(host, NAND_ECC_READ);
1845 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001846
1847 cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
1848 vcmd2 = NFCADDR_CMD_VCMD2;
1849 break;
1850 /* For prgramming command, the cmd need set to write enable */
1851 case NAND_CMD_PAGEPROG:
1852 case NAND_CMD_SEQIN:
1853 case NAND_CMD_RNDIN:
1854 nfcwr = NFCADDR_CMD_NFCWR;
Josh Wu6054d4d2013-08-05 19:14:37 +08001855 if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
1856 dataen = NFCADDR_CMD_DATAEN;
Josh Wu7dc37de2013-08-05 19:14:35 +08001857 break;
1858 default:
1859 break;
1860 }
1861
1862 if (do_addr)
Brian Norris3dad2342014-01-29 14:08:12 -08001863 acycle = nfc_make_addr(mtd, command, column, page_addr,
1864 &addr1234, &cycle0);
Josh Wu7dc37de2013-08-05 19:14:35 +08001865
1866 nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
1867 nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
1868
1869 /*
1870 * Program and erase have their own busy handlers status, sequential
1871 * in, and deplete1 need no delay.
1872 */
1873 switch (command) {
1874 case NAND_CMD_CACHEDPROG:
1875 case NAND_CMD_PAGEPROG:
1876 case NAND_CMD_ERASE1:
1877 case NAND_CMD_ERASE2:
1878 case NAND_CMD_RNDIN:
1879 case NAND_CMD_STATUS:
1880 case NAND_CMD_RNDOUT:
1881 case NAND_CMD_SEQIN:
1882 case NAND_CMD_READID:
1883 return;
1884
1885 case NAND_CMD_READ0:
Josh Wu1ae9c092013-08-05 19:14:36 +08001886 if (dataen == NFCADDR_CMD_DATAEN) {
1887 host->nfc->data_in_sram = host->nfc->sram_bank0 +
1888 nfc_get_sram_off(host);
1889 return;
1890 }
Josh Wu7dc37de2013-08-05 19:14:35 +08001891 /* fall through */
1892 default:
Josh Wue4e06932014-06-10 17:50:11 +08001893 nfc_prepare_interrupt(host, NFC_SR_RB_EDGE);
Josh Wu7dc37de2013-08-05 19:14:35 +08001894 nfc_wait_interrupt(host, NFC_SR_RB_EDGE);
1895 }
1896}
1897
Josh Wu6054d4d2013-08-05 19:14:37 +08001898static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1899 uint32_t offset, int data_len, const uint8_t *buf,
1900 int oob_required, int page, int cached, int raw)
1901{
1902 int cfg, len;
1903 int status = 0;
1904 struct atmel_nand_host *host = chip->priv;
1905 void __iomem *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
1906
1907 /* Subpage write is not supported */
1908 if (offset || (data_len < mtd->writesize))
1909 return -EINVAL;
1910
Josh Wu6054d4d2013-08-05 19:14:37 +08001911 len = mtd->writesize;
Josh Wu6054d4d2013-08-05 19:14:37 +08001912 /* Copy page data to sram that will write to nand via NFC */
1913 if (use_dma) {
1914 if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
1915 /* Fall back to use cpu copy */
1916 memcpy32_toio(sram, buf, len);
1917 } else {
1918 memcpy32_toio(sram, buf, len);
1919 }
1920
Wu, Joshff0a2152014-08-05 18:38:52 +08001921 cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
1922 if (unlikely(raw) && oob_required) {
1923 memcpy32_toio(sram + len, chip->oob_poi, mtd->oobsize);
1924 len += mtd->oobsize;
1925 nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
1926 } else {
1927 nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
1928 }
1929
Josh Wu6054d4d2013-08-05 19:14:37 +08001930 if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
1931 /*
1932 * When use NFC sram, need set up PMECC before send
1933 * NAND_CMD_SEQIN command. Since when the nand command
1934 * is sent, nfc will do transfer from sram and nand.
1935 */
1936 pmecc_enable(host, NAND_ECC_WRITE);
1937
1938 host->nfc->will_write_sram = true;
1939 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1940 host->nfc->will_write_sram = false;
1941
1942 if (likely(!raw))
1943 /* Need to write ecc into oob */
1944 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
1945
1946 if (status < 0)
1947 return status;
1948
1949 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1950 status = chip->waitfunc(mtd, chip);
1951
1952 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1953 status = chip->errstat(mtd, chip, FL_WRITING, status, page);
1954
1955 if (status & NAND_STATUS_FAIL)
1956 return -EIO;
1957
1958 return 0;
1959}
1960
Josh Wu1ae9c092013-08-05 19:14:36 +08001961static int nfc_sram_init(struct mtd_info *mtd)
1962{
1963 struct nand_chip *chip = mtd->priv;
1964 struct atmel_nand_host *host = chip->priv;
1965 int res = 0;
1966
1967 /* Initialize the NFC CFG register */
1968 unsigned int cfg_nfc = 0;
1969
1970 /* set page size and oob layout */
1971 switch (mtd->writesize) {
1972 case 512:
1973 cfg_nfc = NFC_CFG_PAGESIZE_512;
1974 break;
1975 case 1024:
1976 cfg_nfc = NFC_CFG_PAGESIZE_1024;
1977 break;
1978 case 2048:
1979 cfg_nfc = NFC_CFG_PAGESIZE_2048;
1980 break;
1981 case 4096:
1982 cfg_nfc = NFC_CFG_PAGESIZE_4096;
1983 break;
1984 case 8192:
1985 cfg_nfc = NFC_CFG_PAGESIZE_8192;
1986 break;
1987 default:
1988 dev_err(host->dev, "Unsupported page size for NFC.\n");
1989 res = -ENXIO;
1990 return res;
1991 }
1992
1993 /* oob bytes size = (NFCSPARESIZE + 1) * 4
1994 * Max support spare size is 512 bytes. */
1995 cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
1996 & NFC_CFG_NFC_SPARESIZE);
1997 /* default set a max timeout */
1998 cfg_nfc |= NFC_CFG_RSPARE |
1999 NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
2000
2001 nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
2002
Josh Wu6054d4d2013-08-05 19:14:37 +08002003 host->nfc->will_write_sram = false;
Josh Wu1ae9c092013-08-05 19:14:36 +08002004 nfc_set_sram_bank(host, 0);
2005
Josh Wu6054d4d2013-08-05 19:14:37 +08002006 /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
2007 if (host->nfc->write_by_sram) {
2008 if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
2009 chip->ecc.mode == NAND_ECC_NONE)
2010 chip->write_page = nfc_sram_write_page;
2011 else
2012 host->nfc->write_by_sram = false;
2013 }
Josh Wu1ae9c092013-08-05 19:14:36 +08002014
Josh Wu6054d4d2013-08-05 19:14:37 +08002015 dev_info(host->dev, "Using NFC Sram read %s\n",
2016 host->nfc->write_by_sram ? "and write" : "");
Josh Wu1ae9c092013-08-05 19:14:36 +08002017 return 0;
2018}
2019
Josh Wu7dc37de2013-08-05 19:14:35 +08002020static struct platform_driver atmel_nand_nfc_driver;
Andrew Victor42cb1402006-10-19 18:24:35 +02002021/*
2022 * Probe for the NAND device.
2023 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002024static int atmel_nand_probe(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002025{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002026 struct atmel_nand_host *host;
Andrew Victor42cb1402006-10-19 18:24:35 +02002027 struct mtd_info *mtd;
2028 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002029 struct resource *mem;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002030 struct mtd_part_parser_data ppdata = {};
Josh Wu7dc37de2013-08-05 19:14:35 +08002031 int res, irq;
Andrew Victor42cb1402006-10-19 18:24:35 +02002032
2033 /* Allocate memory for the device structure (and zero it) */
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002034 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Jingoo Han9e3677a2013-12-26 12:00:16 +09002035 if (!host)
Andrew Victor42cb1402006-10-19 18:24:35 +02002036 return -ENOMEM;
Andrew Victor42cb1402006-10-19 18:24:35 +02002037
Josh Wu7dc37de2013-08-05 19:14:35 +08002038 res = platform_driver_register(&atmel_nand_nfc_driver);
2039 if (res)
2040 dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
2041
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002042 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2043 host->io_base = devm_ioremap_resource(&pdev->dev, mem);
2044 if (IS_ERR(host->io_base)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002045 res = PTR_ERR(host->io_base);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002046 goto err_nand_ioremap;
Andrew Victor42cb1402006-10-19 18:24:35 +02002047 }
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002048 host->io_phys = (dma_addr_t)mem->start;
Andrew Victor42cb1402006-10-19 18:24:35 +02002049
2050 mtd = &host->mtd;
2051 nand_chip = &host->nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +02002052 host->dev = &pdev->dev;
Josh Wue9d8da82013-09-18 11:31:19 +08002053 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
2054 /* Only when CONFIG_OF is enabled of_node can be parsed */
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002055 res = atmel_of_init_port(host, pdev->dev.of_node);
2056 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002057 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002058 } else {
Jingoo Han453810b2013-07-30 17:18:33 +09002059 memcpy(&host->board, dev_get_platdata(&pdev->dev),
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002060 sizeof(struct atmel_nand_data));
2061 }
Andrew Victor42cb1402006-10-19 18:24:35 +02002062
2063 nand_chip->priv = host; /* link the private data structures */
2064 mtd->priv = nand_chip;
2065 mtd->owner = THIS_MODULE;
2066
2067 /* Set address of NAND IO lines */
2068 nand_chip->IO_ADDR_R = host->io_base;
2069 nand_chip->IO_ADDR_W = host->io_base;
Ivan Kutena4265f82007-05-24 14:35:58 +03002070
Josh Wu7dc37de2013-08-05 19:14:35 +08002071 if (nand_nfc.is_initialized) {
2072 /* NFC driver is probed and initialized */
2073 host->nfc = &nand_nfc;
2074
2075 nand_chip->select_chip = nfc_select_chip;
2076 nand_chip->dev_ready = nfc_device_ready;
2077 nand_chip->cmdfunc = nfc_nand_command;
2078
2079 /* Initialize the interrupt for NFC */
2080 irq = platform_get_irq(pdev, 0);
2081 if (irq < 0) {
2082 dev_err(host->dev, "Cannot get HSMC irq!\n");
Wei Yongjunff52c672013-08-23 10:50:36 +08002083 res = irq;
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002084 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002085 }
2086
Josh Wu7dc37de2013-08-05 19:14:35 +08002087 res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
2088 0, "hsmc", host);
2089 if (res) {
2090 dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
2091 irq);
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002092 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002093 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002094 } else {
2095 res = atmel_nand_set_enable_ready_pins(mtd);
2096 if (res)
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002097 goto err_nand_ioremap;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002098
Josh Wu7dc37de2013-08-05 19:14:35 +08002099 nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002100 }
Ivan Kutena4265f82007-05-24 14:35:58 +03002101
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002102 nand_chip->ecc.mode = host->board.ecc_mode;
Raphaël Poggi796fe362014-07-29 15:27:27 +02002103 nand_chip->chip_delay = 40; /* 40us command delay time */
Andrew Victor42cb1402006-10-19 18:24:35 +02002104
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002105 if (host->board.bus_width_16) /* 16-bit bus width */
Andrew Victordd11b8c2006-12-08 13:49:42 +02002106 nand_chip->options |= NAND_BUSWIDTH_16;
Hong Xucbc6c5e2011-01-18 14:36:05 +08002107
2108 nand_chip->read_buf = atmel_read_buf;
2109 nand_chip->write_buf = atmel_write_buf;
Andrew Victordd11b8c2006-12-08 13:49:42 +02002110
Andrew Victor42cb1402006-10-19 18:24:35 +02002111 platform_set_drvdata(pdev, host);
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002112 atmel_nand_enable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002113
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002114 if (gpio_is_valid(host->board.det_pin)) {
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002115 res = devm_gpio_request(&pdev->dev,
2116 host->board.det_pin, "nand_det");
Jean-Christophe PLAGNIOL-VILLARD28446ac2012-07-12 10:31:08 +02002117 if (res < 0) {
2118 dev_err(&pdev->dev,
2119 "can't request det gpio %d\n",
2120 host->board.det_pin);
2121 goto err_no_card;
2122 }
2123
2124 res = gpio_direction_input(host->board.det_pin);
2125 if (res < 0) {
2126 dev_err(&pdev->dev,
2127 "can't request input direction det gpio %d\n",
2128 host->board.det_pin);
2129 goto err_no_card;
2130 }
2131
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002132 if (gpio_get_value(host->board.det_pin)) {
Jingoo Han1295f972013-12-26 12:30:58 +09002133 dev_info(&pdev->dev, "No SmartMedia card inserted.\n");
Roel Kluin895fb492009-11-11 21:47:06 +01002134 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002135 goto err_no_card;
Andrew Victor42cb1402006-10-19 18:24:35 +02002136 }
2137 }
2138
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002139 if (host->board.on_flash_bbt || on_flash_bbt) {
Jingoo Han1295f972013-12-26 12:30:58 +09002140 dev_info(&pdev->dev, "Use On Flash BBT\n");
Brian Norrisbb9ebd42011-05-31 16:31:23 -07002141 nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
Simon Polettef4fa6972009-05-27 18:19:39 +03002142 }
2143
Josh Wu1b719262013-05-09 15:34:55 +08002144 if (!host->board.has_dma)
Hong Xucb457a42011-03-30 16:26:41 +08002145 use_dma = 0;
2146
2147 if (use_dma) {
Hong Xucbc6c5e2011-01-18 14:36:05 +08002148 dma_cap_mask_t mask;
2149
2150 dma_cap_zero(mask);
2151 dma_cap_set(DMA_MEMCPY, mask);
Nicolas Ferre201ab532011-06-29 18:41:16 +02002152 host->dma_chan = dma_request_channel(mask, NULL, NULL);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002153 if (!host->dma_chan) {
2154 dev_err(host->dev, "Failed to request DMA channel\n");
2155 use_dma = 0;
2156 }
2157 }
2158 if (use_dma)
Nicolas Ferre042bc9c2011-03-30 16:26:40 +08002159 dev_info(host->dev, "Using %s for DMA transfers.\n",
2160 dma_chan_name(host->dma_chan));
Hong Xucbc6c5e2011-01-18 14:36:05 +08002161 else
2162 dev_info(host->dev, "No DMA support for NAND access.\n");
2163
Richard Genoud77f54922008-04-23 19:51:14 +02002164 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00002165 if (nand_scan_ident(mtd, 1, NULL)) {
Richard Genoud77f54922008-04-23 19:51:14 +02002166 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002167 goto err_scan_ident;
Richard Genoud77f54922008-04-23 19:51:14 +02002168 }
2169
Richard Genoud3fc23892008-10-12 08:42:28 +02002170 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Josh Wu1c7b8742012-06-29 17:47:55 +08002171 if (host->has_pmecc)
2172 res = atmel_pmecc_nand_init_params(pdev, host);
2173 else
2174 res = atmel_hw_nand_init_params(pdev, host);
2175
Josh Wu3dfe41a2012-06-25 18:07:43 +08002176 if (res != 0)
2177 goto err_hw_ecc;
Richard Genoud77f54922008-04-23 19:51:14 +02002178 }
2179
Josh Wu1ae9c092013-08-05 19:14:36 +08002180 /* initialize the nfc configuration register */
2181 if (host->nfc && host->nfc->use_nfc_sram) {
2182 res = nfc_sram_init(mtd);
2183 if (res) {
2184 host->nfc->use_nfc_sram = false;
2185 dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
2186 }
2187 }
2188
Richard Genoud77f54922008-04-23 19:51:14 +02002189 /* second phase scan */
2190 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +02002191 res = -ENXIO;
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002192 goto err_scan_tail;
Andrew Victor42cb1402006-10-19 18:24:35 +02002193 }
2194
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002195 mtd->name = "atmel_nand";
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002196 ppdata.of_node = pdev->dev.of_node;
2197 res = mtd_device_parse_register(mtd, NULL, &ppdata,
2198 host->board.parts, host->board.num_parts);
Andrew Victor42cb1402006-10-19 18:24:35 +02002199 if (!res)
2200 return res;
2201
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002202err_scan_tail:
Jean-Christophe PLAGNIOL-VILLARD0d637482013-08-05 19:14:33 +08002203 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
Josh Wu1c7b8742012-06-29 17:47:55 +08002204 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
Josh Wu3dfe41a2012-06-25 18:07:43 +08002205err_hw_ecc:
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002206err_scan_ident:
2207err_no_card:
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002208 atmel_nand_disable(host);
Hong Xucbc6c5e2011-01-18 14:36:05 +08002209 if (host->dma_chan)
2210 dma_release_channel(host->dma_chan);
Håvard Skinnemoencc0c72e2008-06-06 18:04:54 +02002211err_nand_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +02002212 return res;
2213}
2214
2215/*
2216 * Remove a NAND device.
2217 */
Johan Hovold2c2b9282013-09-23 16:27:28 +02002218static int atmel_nand_remove(struct platform_device *pdev)
Andrew Victor42cb1402006-10-19 18:24:35 +02002219{
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002220 struct atmel_nand_host *host = platform_get_drvdata(pdev);
Andrew Victor42cb1402006-10-19 18:24:35 +02002221 struct mtd_info *mtd = &host->mtd;
2222
2223 nand_release(mtd);
2224
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002225 atmel_nand_disable(host);
Andrew Victor42cb1402006-10-19 18:24:35 +02002226
Josh Wu1c7b8742012-06-29 17:47:55 +08002227 if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
2228 pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
2229 pmerrloc_writel(host->pmerrloc_base, ELDIS,
2230 PMERRLOC_DISABLE);
Josh Wu1c7b8742012-06-29 17:47:55 +08002231 }
2232
Hong Xucbc6c5e2011-01-18 14:36:05 +08002233 if (host->dma_chan)
2234 dma_release_channel(host->dma_chan);
2235
Josh Wu7dc37de2013-08-05 19:14:35 +08002236 platform_driver_unregister(&atmel_nand_nfc_driver);
2237
Andrew Victor42cb1402006-10-19 18:24:35 +02002238 return 0;
2239}
2240
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002241static const struct of_device_id atmel_nand_dt_ids[] = {
2242 { .compatible = "atmel,at91rm9200-nand" },
2243 { /* sentinel */ }
2244};
2245
2246MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002247
Josh Wu7dc37de2013-08-05 19:14:35 +08002248static int atmel_nand_nfc_probe(struct platform_device *pdev)
2249{
2250 struct atmel_nfc *nfc = &nand_nfc;
2251 struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002252 int ret;
Josh Wu7dc37de2013-08-05 19:14:35 +08002253
2254 nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2255 nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
2256 if (IS_ERR(nfc->base_cmd_regs))
2257 return PTR_ERR(nfc->base_cmd_regs);
2258
2259 nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2260 nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
2261 if (IS_ERR(nfc->hsmc_regs))
2262 return PTR_ERR(nfc->hsmc_regs);
2263
2264 nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2265 if (nfc_sram) {
2266 nfc->sram_bank0 = devm_ioremap_resource(&pdev->dev, nfc_sram);
Josh Wu1ae9c092013-08-05 19:14:36 +08002267 if (IS_ERR(nfc->sram_bank0)) {
Josh Wu7dc37de2013-08-05 19:14:35 +08002268 dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
2269 PTR_ERR(nfc->sram_bank0));
Josh Wu1ae9c092013-08-05 19:14:36 +08002270 } else {
2271 nfc->use_nfc_sram = true;
Josh Wu7dc37de2013-08-05 19:14:35 +08002272 nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
Josh Wu6054d4d2013-08-05 19:14:37 +08002273
2274 if (pdev->dev.of_node)
2275 nfc->write_by_sram = of_property_read_bool(
2276 pdev->dev.of_node,
2277 "atmel,write-by-sram");
Josh Wu1ae9c092013-08-05 19:14:36 +08002278 }
Josh Wu7dc37de2013-08-05 19:14:35 +08002279 }
2280
Wu, Josh50e04e22014-06-10 17:50:09 +08002281 nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
2282 nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */
2283
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002284 nfc->clk = devm_clk_get(&pdev->dev, NULL);
2285 if (!IS_ERR(nfc->clk)) {
2286 ret = clk_prepare_enable(nfc->clk);
2287 if (ret)
2288 return ret;
2289 } else {
2290 dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree");
2291 }
2292
Josh Wu7dc37de2013-08-05 19:14:35 +08002293 nfc->is_initialized = true;
2294 dev_info(&pdev->dev, "NFC is probed.\n");
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002295
2296 return 0;
2297}
2298
2299static int atmel_nand_nfc_remove(struct platform_device *pdev)
2300{
2301 struct atmel_nfc *nfc = &nand_nfc;
2302
2303 if (!IS_ERR(nfc->clk))
2304 clk_disable_unprepare(nfc->clk);
2305
Josh Wu7dc37de2013-08-05 19:14:35 +08002306 return 0;
2307}
2308
Josh Wu81f29b42013-09-18 11:31:20 +08002309static const struct of_device_id atmel_nand_nfc_match[] = {
Josh Wu7dc37de2013-08-05 19:14:35 +08002310 { .compatible = "atmel,sama5d3-nfc" },
2311 { /* sentinel */ }
2312};
Josh Wu81f29b42013-09-18 11:31:20 +08002313MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
Josh Wu7dc37de2013-08-05 19:14:35 +08002314
2315static struct platform_driver atmel_nand_nfc_driver = {
2316 .driver = {
2317 .name = "atmel_nand_nfc",
2318 .owner = THIS_MODULE,
2319 .of_match_table = of_match_ptr(atmel_nand_nfc_match),
2320 },
2321 .probe = atmel_nand_nfc_probe,
Boris BREZILLON2d405ec2014-09-13 01:23:59 +02002322 .remove = atmel_nand_nfc_remove,
Josh Wu7dc37de2013-08-05 19:14:35 +08002323};
2324
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002325static struct platform_driver atmel_nand_driver = {
Johan Hovold2c2b9282013-09-23 16:27:28 +02002326 .probe = atmel_nand_probe,
2327 .remove = atmel_nand_remove,
Andrew Victor42cb1402006-10-19 18:24:35 +02002328 .driver = {
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002329 .name = "atmel_nand",
Andrew Victor42cb1402006-10-19 18:24:35 +02002330 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDd6a01662012-01-26 02:11:06 +08002331 .of_match_table = of_match_ptr(atmel_nand_dt_ids),
Andrew Victor42cb1402006-10-19 18:24:35 +02002332 },
2333};
2334
Johan Hovold2c2b9282013-09-23 16:27:28 +02002335module_platform_driver(atmel_nand_driver);
Andrew Victor42cb1402006-10-19 18:24:35 +02002336
2337MODULE_LICENSE("GPL");
2338MODULE_AUTHOR("Rick Bronson");
Håvard Skinnemoend4f4c0a2008-06-06 18:04:52 +02002339MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
Håvard Skinnemoen3c3796c2008-06-06 18:04:53 +02002340MODULE_ALIAS("platform:atmel_nand");