Xilinx: ARM: NAND: Removed ClearNAND support
[linux-2.6-xlnx.git] / drivers / mtd / nand / xilinx_nandpss.c
bloba07ce8424ba605d0312d0c6ecdf354f761df01b6
1 /*
2 * Xilinx PSS NAND Flash Controller Driver
4 * Copyright (C) 2009 Xilinx, Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License version 2 as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 * You should have received a copy of the GNU General Public License along with
12 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
13 * Place, Suite 330, Boston, MA 02111-1307 USA
17 * This driver is based on plat_nand.c and mxc_nand.c drivers
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/ioport.h>
24 #include <linux/platform_device.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/io.h>
28 #include <linux/slab.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/nand.h>
31 #include <linux/mtd/partitions.h>
32 #include <linux/delay.h>
33 #include <linux/mtd/nand_ecc.h>
34 #include <mach/smc.h>
35 #include <mach/nand.h>
37 #ifdef CONFIG_OF
38 #include <linux/of_address.h>
39 #include <linux/of_device.h>
40 #include <linux/of_platform.h>
41 #endif
43 #define XNANDPSS_DRIVER_NAME "Xilinx_PSS_NAND"
46 * The NAND flash driver defines
49 #define XNANDPSS_CMD_PHASE 1 /* End command valid in command phase */
50 #define XNANDPSS_DATA_PHASE 2 /* End command valid in data phase */
51 #define XNANDPSS_ECC_SIZE 512 /* Size of data for ECC operation */
54 * Register values for using NAND interface of NAND controller
55 * The SET_CYCLES_REG register value depends on the flash device. Look in to the
56 * device datasheet and change its value, This value is for 2Gb Numonyx flash.
59 /* Flash memory controller operating parameters */
60 #define XNANDPSS_CLR_CONFIG ((0x1 << 1) | /* Disable interrupt */ \
61 (0x1 << 4) | /* Clear interrupt */ \
62 (0x1 << 6)) /* Disable ECC interrupt */
64 /* Assuming 50MHz clock (20ns cycle time) and 3V operation */
65 #define XNANDPSS_SET_CYCLES ((0x4 << 20) | /* t_rr from nand_cycles */ \
66 (0x2 << 17) | /* t_ar from nand_cycles */ \
67 (0x2 << 14) | /* t_clr from nand_cycles */ \
68 (0x2 << 11) | /* t_wp from nand_cycles */ \
69 (0x1 << 8) | /* t_rea from nand_cycles */ \
70 (0x4 << 4) | /* t_wc from nand_cycles */ \
71 (0x4 << 0)) /* t_rc from nand_cycles */
73 #define XNANDPSS_SET_OPMODE 0x0
75 #define XNANDPSS_DIRECT_CMD ((0x4 << 23) | /* Chip 0 from interface 1 */ \
76 (0x2 << 21)) /* UpdateRegs operation */
78 #define XNANDPSS_ECC_CONFIG ((0x1 << 2) | /* ECC available on APB */ \
79 (0x1 << 4) | /* ECC read at end of page */ \
80 (0x0 << 5)) /* No Jumping */
82 #define XNANDPSS_ECC_CMD1 ((0x80) | /* Write command */ \
83 (0x00 << 8) | /* Read command */ \
84 (0x30 << 16) | /* Read End command */ \
85 (0x1 << 24)) /* Read End command calid */
87 #define XNANDPSS_ECC_CMD2 ((0x85) | /* Write col change cmd */ \
88 (0x05 << 8) | /* Read col change cmd */ \
89 (0xE0 << 16) | /* Read col change end cmd */ \
90 (0x1 << 24)) /* Read col change
91 end cmd valid */
93 * AXI Address definitions
95 #define START_CMD_SHIFT 3
96 #define END_CMD_SHIFT 11
97 #define END_CMD_VALID_SHIFT 20
98 #define ADDR_CYCLES_SHIFT 21
99 #define CLEAR_CS_SHIFT 21
100 #define ECC_LAST_SHIFT 10
101 #define COMMAND_PHASE (0 << 19)
102 #define DATA_PHASE (1 << 19)
104 #define XNANDPSS_ECC_LAST (1 << ECC_LAST_SHIFT) /* Set ECC_Last */
105 #define XNANDPSS_CLEAR_CS (1 << CLEAR_CS_SHIFT) /* Clear chip select */
108 * ECC block registers bit position and bit mask
110 #define XNANDPSS_ECC_BUSY (1 << 6) /* ECC block is busy */
111 #define XNANDPSS_ECC_MASK 0x00FFFFFF /* ECC value mask */
114 * ONFI Get/Set features command
116 #define NAND_CMD_GET_FEATURES 0xEE
117 #define NAND_CMD_SET_FEATURES 0xEF
119 #define ONDIE_ECC_FEATURE_ADDR 0x90
122 * Macros for the NAND controller register read/write
124 #define xnandpss_read32(addr) __raw_readl(addr)
125 #define xnandpss_write32(addr, val) __raw_writel((val), (addr))
129 * struct xnandpss_command_format - Defines NAND flash command format
130 * @start_cmd: First cycle command (Start command)
131 * @end_cmd: Second cycle command (Last command)
132 * @addr_cycles: Number of address cycles required to send the address
133 * @end_cmd_valid: The second cycle command is valid for cmd or data phase
135 struct xnandpss_command_format {
136 int start_cmd;
137 int end_cmd;
138 u8 addr_cycles;
139 u8 end_cmd_valid;
143 * struct xnandpss_info - Defines the NAND flash driver instance
144 * @chip: NAND chip information structure
145 * @mtd: MTD information structure
146 * @parts: Pointer to the mtd_partition structure
147 * @nand_base: Virtual address of the NAND flash device
148 * @smc_regs: Virtual address of the NAND controller registers
149 * @end_cmd_pending: End command is pending
150 * @end_cmd: End command
152 struct xnandpss_info {
153 struct nand_chip chip;
154 struct mtd_info mtd;
155 struct mtd_partition *parts;
156 struct platform_device *pdev;
158 void __iomem *nand_base;
159 void __iomem *smc_regs;
160 unsigned long end_cmd_pending;
161 unsigned long end_cmd;
165 * The NAND flash operations command format
167 static struct xnandpss_command_format xnandpss_commands[] __devinitdata = {
168 {NAND_CMD_READ0, NAND_CMD_READSTART, 5, XNANDPSS_CMD_PHASE},
169 {NAND_CMD_RNDOUT, NAND_CMD_RNDOUTSTART, 2, XNANDPSS_CMD_PHASE},
170 {NAND_CMD_READID, NAND_CMD_NONE, 1, NAND_CMD_NONE},
171 {NAND_CMD_STATUS, NAND_CMD_NONE, 0, NAND_CMD_NONE},
172 {NAND_CMD_SEQIN, NAND_CMD_PAGEPROG, 5, XNANDPSS_DATA_PHASE},
173 {NAND_CMD_RNDIN, NAND_CMD_NONE, 2, NAND_CMD_NONE},
174 {NAND_CMD_ERASE1, NAND_CMD_ERASE2, 3, XNANDPSS_CMD_PHASE},
175 {NAND_CMD_RESET, NAND_CMD_NONE, 0, NAND_CMD_NONE},
176 {NAND_CMD_PARAM, NAND_CMD_NONE, 1, NAND_CMD_NONE},
177 {NAND_CMD_GET_FEATURES, NAND_CMD_NONE, 1, NAND_CMD_NONE},
178 {NAND_CMD_SET_FEATURES, NAND_CMD_NONE, 1, NAND_CMD_NONE},
179 {NAND_CMD_NONE, NAND_CMD_NONE, 0, 0},
180 /* Add all the flash commands supported by the flash device and Linux */
181 /* The cache program command is not supported by driver because driver
182 * cant differentiate between page program and cached page program from
183 * start command, these commands can be differentiated through end
184 * command, which doesn't fit in to the driver design. The cache program
185 * command is not supported by NAND subsystem also, look at 1612 line
186 * number (in nand_write_page function) of nand_base.c file.
187 * {NAND_CMD_SEQIN, NAND_CMD_CACHEDPROG, 5, XNANDPSS_YES}, */
190 /* Define default oob placement schemes for large and small page devices */
191 static struct nand_ecclayout nand_oob_16 = {
192 .eccbytes = 3,
193 .eccpos = {0, 1, 2},
194 .oobfree = {
195 {.offset = 8,
196 . length = 8} }
199 static struct nand_ecclayout nand_oob_64 = {
200 .eccbytes = 12,
201 .eccpos = {
202 52, 53, 54, 55, 56, 57,
203 58, 59, 60, 61, 62, 63},
204 .oobfree = {
205 {.offset = 2,
206 .length = 50} }
209 static struct nand_ecclayout ondie_nand_oob_64 = {
210 .eccbytes = 32,
212 .eccpos = {
213 8, 9, 10, 11, 12, 13, 14, 15,
214 24, 25, 26, 27, 28, 29, 30, 31,
215 40, 41, 42, 43, 44, 45, 46, 47,
216 56, 57, 58, 59, 60, 61, 62, 63
219 .oobfree = {
220 { .offset = 4, .length = 4 },
221 { .offset = 20, .length = 4 },
222 { .offset = 36, .length = 4 },
223 { .offset = 52, .length = 4 }
227 /* Generic flash bbt decriptors
229 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
230 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
232 static struct nand_bbt_descr bbt_main_descr = {
233 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
234 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
235 .offs = 4,
236 .len = 4,
237 .veroffs = 20,
238 .maxblocks = 4,
239 .pattern = bbt_pattern
242 static struct nand_bbt_descr bbt_mirror_descr = {
243 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
244 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
245 .offs = 4,
246 .len = 4,
247 .veroffs = 20,
248 .maxblocks = 4,
249 .pattern = mirror_pattern
253 * xnandpss_init_nand_flash - Initialize NAND controller
254 * @smc_regs: Virtual address of the NAND controller registers
255 * @option: Device property flags
257 * This function initializes the NAND flash interface on the NAND controller.
259 static void xnandpss_init_nand_flash(void __iomem *smc_regs, int option)
261 /* disable interrupts */
262 xnandpss_write32(smc_regs + XSMCPSS_MC_CLR_CONFIG, XNANDPSS_CLR_CONFIG);
263 /* Initialize the NAND interface by setting cycles and operation mode */
264 xnandpss_write32(smc_regs + XSMCPSS_MC_SET_CYCLES, XNANDPSS_SET_CYCLES);
265 if (option & NAND_BUSWIDTH_16)
266 xnandpss_write32(smc_regs + XSMCPSS_MC_SET_OPMODE,
267 (XNANDPSS_SET_OPMODE | 0x1));
268 else
269 xnandpss_write32(smc_regs + XSMCPSS_MC_SET_OPMODE,
270 XNANDPSS_SET_OPMODE);
271 xnandpss_write32(smc_regs + XSMCPSS_MC_DIRECT_CMD, XNANDPSS_DIRECT_CMD);
273 /* Wait till the ECC operation is complete */
274 while ( (xnandpss_read32(smc_regs + XSMCPSS_ECC_STATUS_OFFSET(
275 XSMCPSS_ECC_IF1_OFFSET))) & XNANDPSS_ECC_BUSY)
277 /* Set the command1 and command2 register */
278 xnandpss_write32(smc_regs +
279 (XSMCPSS_ECC_MEMCMD1_OFFSET(XSMCPSS_ECC_IF1_OFFSET)),
280 XNANDPSS_ECC_CMD1);
281 xnandpss_write32(smc_regs +
282 (XSMCPSS_ECC_MEMCMD2_OFFSET(XSMCPSS_ECC_IF1_OFFSET)),
283 XNANDPSS_ECC_CMD2);
287 * xnandpss_calculate_hwecc - Calculate Hardware ECC
288 * @mtd: Pointer to the mtd_info structure
289 * @data: Pointer to the page data
290 * @ecc_code: Pointer to the ECC buffer where ECC data needs to be stored
292 * This function retrieves the Hardware ECC data from the controller and returns
293 * ECC data back to the MTD subsystem.
295 * returns: 0 on success or error value on failure
297 static int
298 xnandpss_calculate_hwecc(struct mtd_info *mtd, const u8 *data, u8 *ecc_code)
300 struct xnandpss_info *xnand =
301 container_of(mtd, struct xnandpss_info, mtd);
302 u32 ecc_value = 0;
303 u8 ecc_reg, ecc_byte;
304 u32 ecc_status;
306 /* Wait till the ECC operation is complete */
307 do {
308 ecc_status = xnandpss_read32(xnand->smc_regs +
309 XSMCPSS_ECC_STATUS_OFFSET(XSMCPSS_ECC_IF1_OFFSET));
310 } while (ecc_status & XNANDPSS_ECC_BUSY);
312 for (ecc_reg = 0; ecc_reg < 4; ecc_reg++) {
313 /* Read ECC value for each block */
314 ecc_value = (xnandpss_read32(xnand->smc_regs +
315 (XSMCPSS_ECC_VALUE0_OFFSET(XSMCPSS_ECC_IF1_OFFSET) +
316 (ecc_reg*4))));
317 ecc_status = (ecc_value >> 24) & 0xFF;
318 /* ECC value valid */
319 if (ecc_status & 0x40) {
320 for (ecc_byte = 0; ecc_byte < 3; ecc_byte++) {
321 /* Copy ECC bytes to MTD buffer */
322 *ecc_code = ecc_value & 0xFF;
323 ecc_value = ecc_value >> 8;
324 ecc_code++;
326 } else {
327 /* TO DO */
328 /* dev_warn(&pdev->dev, "pl350: ecc status failed\n");
329 * */
332 return 0;
336 * onehot - onehot function
337 * @value: value to check for onehot
339 * This function checks whether a value is onehot or not.
340 * onehot is if and only if onebit is set.
343 int onehot(unsigned short value)
345 return ((value & (value-1)) == 0);
349 * xnandpss_correct_data - ECC correction function
350 * @mtd: Pointer to the mtd_info structure
351 * @buf: Pointer to the page data
352 * @read_ecc: Pointer to the ECC value read from spare data area
353 * @calc_ecc: Pointer to the calculated ECC value
355 * This function corrects the ECC single bit errors & detects 2-bit errors.
357 * returns: 0 if no ECC errors found
358 * 1 if single bit error found and corrected.
359 * -1 if multiple ECC errors found.
361 int xnandpss_correct_data(struct mtd_info *mtd, unsigned char *buf,
362 unsigned char *read_ecc, unsigned char *calc_ecc)
364 unsigned char bit_addr;
365 unsigned int byte_addr;
366 unsigned short ecc_odd, ecc_even;
367 unsigned short read_ecc_lower, read_ecc_upper;
368 unsigned short calc_ecc_lower, calc_ecc_upper;
370 read_ecc_lower = (read_ecc[0] | (read_ecc[1] << 8)) & 0xfff;
371 read_ecc_upper = ((read_ecc[1] >> 4) | (read_ecc[2] << 4)) & 0xfff;
373 calc_ecc_lower = (calc_ecc[0] | (calc_ecc[1] << 8)) & 0xfff;
374 calc_ecc_upper = ((calc_ecc[1] >> 4) | (calc_ecc[2] << 4)) & 0xfff;
376 ecc_odd = read_ecc_lower ^ calc_ecc_lower;
377 ecc_even = read_ecc_upper ^ calc_ecc_upper;
379 if ((ecc_odd == 0) && (ecc_even == 0))
380 return 0; /* no error */
381 else if (ecc_odd == (~ecc_even & 0xfff)) {
382 /* bits [11:3] of error code is byte offset */
383 byte_addr = (ecc_odd >> 3) & 0x1ff;
384 /* bits [2:0] of error code is bit offset */
385 bit_addr = ecc_odd & 0x7;
386 /* Toggling error bit */
387 buf[byte_addr] ^= (1 << bit_addr);
388 return 1;
389 } else if (onehot(ecc_odd | ecc_even) == 1) {
390 return 1; /* one error in parity */
391 } else {
392 return -1; /* Uncorrectable error */
397 * xnandpss_read_oob - [REPLACABLE] the most common OOB data read function
398 * @mtd: mtd info structure
399 * @chip: nand chip info structure
400 * @page: page number to read
402 static int xnandpss_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
403 int page)
405 unsigned long data_width = 4;
406 unsigned long data_phase_addr = 0;
407 uint8_t *p;
409 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
411 p = chip->oob_poi;
412 chip->read_buf(mtd, p, (mtd->oobsize - data_width));
413 p += (mtd->oobsize - data_width);
415 data_phase_addr = (unsigned long __force)chip->IO_ADDR_R;
416 data_phase_addr |= XNANDPSS_CLEAR_CS;
417 chip->IO_ADDR_R = (void __iomem *__force)data_phase_addr;
418 chip->read_buf(mtd, p, data_width);
420 return 0;
424 * xnandpss_write_oob - [REPLACABLE] the most common OOB data write function
425 * @mtd: mtd info structure
426 * @chip: nand chip info structure
427 * @page: page number to write
429 static int xnandpss_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
430 int page)
432 int status = 0;
433 const uint8_t *buf = chip->oob_poi;
434 unsigned long data_width = 4;
435 unsigned long data_phase_addr = 0;
437 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
439 chip->write_buf(mtd, buf, (mtd->oobsize - data_width));
440 buf += (mtd->oobsize - data_width);
442 data_phase_addr = (unsigned long __force)chip->IO_ADDR_W;
443 data_phase_addr |= XNANDPSS_CLEAR_CS;
444 data_phase_addr |= (1 << END_CMD_VALID_SHIFT);
445 chip->IO_ADDR_W = (void __iomem *__force)data_phase_addr;
446 chip->write_buf(mtd, buf, data_width);
448 /* Send command to program the OOB data */
449 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
450 status = chip->waitfunc(mtd, chip);
452 return status & NAND_STATUS_FAIL ? -EIO : 0;
456 * xnandpss_read_page_raw - [Intern] read raw page data without ecc
457 * @mtd: mtd info structure
458 * @chip: nand chip info structure
459 * @buf: buffer to store read data
460 * @page: page number to read
463 static int xnandpss_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
464 uint8_t *buf, int oob_required, int page)
466 unsigned long data_width = 4;
467 unsigned long data_phase_addr = 0;
468 uint8_t *p;
470 chip->read_buf(mtd, buf, mtd->writesize);
472 p = chip->oob_poi;
473 chip->read_buf(mtd, p, (mtd->oobsize - data_width));
474 p += (mtd->oobsize - data_width);
476 data_phase_addr = (unsigned long __force)chip->IO_ADDR_R;
477 data_phase_addr |= XNANDPSS_CLEAR_CS;
478 chip->IO_ADDR_R = (void __iomem *__force)data_phase_addr;
480 chip->read_buf(mtd, p, data_width);
481 return 0;
485 * xnandpss_write_page_raw - [Intern] raw page write function
486 * @mtd: mtd info structure
487 * @chip: nand chip info structure
488 * @buf: data buffer
491 static void xnandpss_write_page_raw(struct mtd_info *mtd,
492 struct nand_chip *chip, const uint8_t *buf, int oob_required)
494 unsigned long data_width = 4;
495 unsigned long data_phase_addr = 0;
496 uint8_t *p;
498 chip->write_buf(mtd, buf, mtd->writesize);
500 p = chip->oob_poi;
501 chip->write_buf(mtd, p, (mtd->oobsize - data_width));
502 p += (mtd->oobsize - data_width);
504 data_phase_addr = (unsigned long __force)chip->IO_ADDR_W;
505 data_phase_addr |= XNANDPSS_CLEAR_CS;
506 data_phase_addr |= (1 << END_CMD_VALID_SHIFT);
507 chip->IO_ADDR_W = (void __iomem *__force)data_phase_addr;
509 chip->write_buf(mtd, p, data_width);
513 * nand_write_page_hwecc - Hardware ECC based page write function
514 * @mtd: Pointer to the mtd info structure
515 * @chip: Pointer to the NAND chip info structure
516 * @buf: Pointer to the data buffer
518 * This functions writes data and hardware generated ECC values in to the page.
520 void xnandpss_write_page_hwecc(struct mtd_info *mtd,
521 struct nand_chip *chip, const uint8_t *buf, int oob_required)
523 int i, eccsize = chip->ecc.size;
524 int eccsteps = chip->ecc.steps;
525 uint8_t *ecc_calc = chip->buffers->ecccalc;
526 const uint8_t *p = buf;
527 uint32_t *eccpos = chip->ecc.layout->eccpos;
528 unsigned long data_phase_addr = 0;
529 unsigned long data_width = 4;
530 uint8_t *oob_ptr;
532 for ( ; (eccsteps - 1); eccsteps--) {
533 chip->write_buf(mtd, p, eccsize);
534 p += eccsize;
536 chip->write_buf(mtd, p, (eccsize - data_width));
537 p += (eccsize - data_width);
539 /* Set ECC Last bit to 1 */
540 data_phase_addr = (unsigned long __force)chip->IO_ADDR_W;
541 data_phase_addr |= XNANDPSS_ECC_LAST;
542 chip->IO_ADDR_W = (void __iomem *__force)data_phase_addr;
543 chip->write_buf(mtd, p, data_width);
545 /* Wait for ECC to be calculated and read the error values */
546 p = buf;
547 chip->ecc.calculate(mtd, p, &ecc_calc[0]);
549 for (i = 0; i < chip->ecc.total; i++)
550 chip->oob_poi[eccpos[i]] = ~(ecc_calc[i]);
552 /* Clear ECC last bit */
553 data_phase_addr = (unsigned long __force)chip->IO_ADDR_W;
554 data_phase_addr &= ~XNANDPSS_ECC_LAST;
555 chip->IO_ADDR_W = (void __iomem *__force)data_phase_addr;
557 /* Write the spare area with ECC bytes */
558 oob_ptr = chip->oob_poi;
559 chip->write_buf(mtd, oob_ptr, (mtd->oobsize - data_width));
561 data_phase_addr = (unsigned long __force)chip->IO_ADDR_W;
562 data_phase_addr |= XNANDPSS_CLEAR_CS;
563 data_phase_addr |= (1 << END_CMD_VALID_SHIFT);
564 chip->IO_ADDR_W = (void __iomem *__force)data_phase_addr;
565 oob_ptr += (mtd->oobsize - data_width);
566 chip->write_buf(mtd, oob_ptr, data_width);
570 * xnandpss_write_page_swecc - [REPLACABLE] software ecc based page write function
571 * @mtd: mtd info structure
572 * @chip: nand chip info structure
573 * @buf: data buffer
575 static void xnandpss_write_page_swecc(struct mtd_info *mtd,
576 struct nand_chip *chip, const uint8_t *buf, int oob_required)
578 int i, eccsize = chip->ecc.size;
579 int eccbytes = chip->ecc.bytes;
580 int eccsteps = chip->ecc.steps;
581 uint8_t *ecc_calc = chip->buffers->ecccalc;
582 const uint8_t *p = buf;
583 uint32_t *eccpos = chip->ecc.layout->eccpos;
585 /* Software ecc calculation */
586 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
587 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
589 for (i = 0; i < chip->ecc.total; i++)
590 chip->oob_poi[eccpos[i]] = ecc_calc[i];
592 chip->ecc.write_page_raw(mtd, chip, buf, 1);
596 * nand_read_page_hwecc - Hardware ECC based page read function
597 * @mtd: Pointer to the mtd info structure
598 * @chip: Pointer to the NAND chip info structure
599 * @buf: Pointer to the buffer to store read data
600 * @page: page number to read
602 * This functions reads data and checks the data integrity by comparing hardware
603 * generated ECC values and read ECC values from spare area.
605 * returns: 0 always and updates ECC operation status in to MTD structure
607 int xnandpss_read_page_hwecc(struct mtd_info *mtd,
608 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
610 int i, stat, eccsize = chip->ecc.size;
611 int eccbytes = chip->ecc.bytes;
612 int eccsteps = chip->ecc.steps;
613 uint8_t *p = buf;
614 uint8_t *ecc_calc = chip->buffers->ecccalc;
615 uint8_t *ecc_code = chip->buffers->ecccode;
616 uint32_t *eccpos = chip->ecc.layout->eccpos;
617 unsigned long data_phase_addr = 0;
618 unsigned long data_width = 4;
619 uint8_t *oob_ptr;
621 for ( ; (eccsteps - 1); eccsteps--) {
622 chip->read_buf(mtd, p, eccsize);
623 p += eccsize;
625 chip->read_buf(mtd, p, (eccsize - data_width));
626 p += (eccsize - data_width);
628 /* Set ECC Last bit to 1 */
629 data_phase_addr = (unsigned long __force)chip->IO_ADDR_R;
630 data_phase_addr |= XNANDPSS_ECC_LAST;
631 chip->IO_ADDR_R = (void __iomem *__force)data_phase_addr;
632 chip->read_buf(mtd, p, data_width);
634 /* Read the calculated ECC value */
635 p = buf;
636 chip->ecc.calculate(mtd, p, &ecc_calc[0]);
638 /* Clear ECC last bit */
639 data_phase_addr = (unsigned long __force)chip->IO_ADDR_R;
640 data_phase_addr &= ~XNANDPSS_ECC_LAST;
641 chip->IO_ADDR_R = (void __iomem *__force)data_phase_addr;
643 /* Read the stored ECC value */
644 oob_ptr = chip->oob_poi;
645 chip->read_buf(mtd, oob_ptr, (mtd->oobsize - data_width));
647 /* de-assert chip select */
648 data_phase_addr = (unsigned long __force)chip->IO_ADDR_R;
649 data_phase_addr |= XNANDPSS_CLEAR_CS;
650 chip->IO_ADDR_R = (void __iomem *__force)data_phase_addr;
652 oob_ptr += (mtd->oobsize - data_width);
653 chip->read_buf(mtd, oob_ptr, data_width);
655 for (i = 0; i < chip->ecc.total; i++)
656 ecc_code[i] = ~(chip->oob_poi[eccpos[i]]);
658 eccsteps = chip->ecc.steps;
659 p = buf;
661 /* Check ECC error for all blocks and correct if it is correctable */
662 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
663 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
664 if (stat < 0)
665 mtd->ecc_stats.failed++;
666 else
667 mtd->ecc_stats.corrected += stat;
669 return 0;
673 * xnandpss_read_page_swecc - [REPLACABLE] software ecc based page read function
674 * @mtd: mtd info structure
675 * @chip: nand chip info structure
676 * @buf: buffer to store read data
677 * @page: page number to read
679 static int xnandpss_read_page_swecc(struct mtd_info *mtd,
680 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
682 int i, eccsize = chip->ecc.size;
683 int eccbytes = chip->ecc.bytes;
684 int eccsteps = chip->ecc.steps;
685 uint8_t *p = buf;
686 uint8_t *ecc_calc = chip->buffers->ecccalc;
687 uint8_t *ecc_code = chip->buffers->ecccode;
688 uint32_t *eccpos = chip->ecc.layout->eccpos;
690 chip->ecc.read_page_raw(mtd, chip, buf, page, 1);
692 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
693 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
695 for (i = 0; i < chip->ecc.total; i++)
696 ecc_code[i] = chip->oob_poi[eccpos[i]];
698 eccsteps = chip->ecc.steps;
699 p = buf;
701 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
702 int stat;
704 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
705 if (stat < 0)
706 mtd->ecc_stats.failed++;
707 else
708 mtd->ecc_stats.corrected += stat;
710 return 0;
714 * xnandpss_select_chip - Select the flash device
715 * @mtd: Pointer to the mtd_info structure
716 * @chip: Chip number to be selected
718 * This function is empty as the NAND controller handles chip select line
719 * internally based on the chip address passed in command and data phase.
721 static void xnandpss_select_chip(struct mtd_info *mtd, int chip)
723 return;
727 * xnandpss_cmd_function - Send command to NAND device
728 * @mtd: Pointer to the mtd_info structure
729 * @command: The command to be sent to the flash device
730 * @column: The column address for this command, -1 if none
731 * @page_addr: The page address for this command, -1 if none
733 static void xnandpss_cmd_function(struct mtd_info *mtd, unsigned int command,
734 int column, int page_addr)
736 struct nand_chip *chip = mtd->priv;
737 struct xnandpss_command_format *curr_cmd = NULL;
738 struct xnandpss_info *xnand =
739 container_of(mtd, struct xnandpss_info, mtd);
740 void __iomem *cmd_addr;
741 unsigned long cmd_data = 0;
742 unsigned long cmd_phase_addr = 0;
743 unsigned long data_phase_addr = 0;
744 unsigned long end_cmd = 0;
745 unsigned long end_cmd_valid = 0;
746 unsigned long i;
748 if (xnand->end_cmd_pending) {
749 /* Check for end command if this command request is same as the
750 * pending command then return */
751 if (xnand->end_cmd == command) {
752 xnand->end_cmd = 0;
753 xnand->end_cmd_pending = 0;
754 return;
758 /* Emulate NAND_CMD_READOOB for large page device */
759 if ((mtd->writesize > XNANDPSS_ECC_SIZE) &&
760 (command == NAND_CMD_READOOB)) {
761 column += mtd->writesize;
762 command = NAND_CMD_READ0;
765 /* Get the command format */
766 for (i = 0; (xnandpss_commands[i].start_cmd != NAND_CMD_NONE ||
767 xnandpss_commands[i].end_cmd != NAND_CMD_NONE); i++) {
768 if (command == xnandpss_commands[i].start_cmd)
769 curr_cmd = &xnandpss_commands[i];
772 if (curr_cmd == NULL)
773 return;
775 /* Clear interrupt */
776 xnandpss_write32((xnand->smc_regs + XSMCPSS_MC_CLR_CONFIG), (1 << 4));
778 /* Get the command phase address */
779 if (curr_cmd->end_cmd_valid == XNANDPSS_CMD_PHASE)
780 end_cmd_valid = 1;
782 if (curr_cmd->end_cmd == NAND_CMD_NONE)
783 end_cmd = 0x0;
784 else
785 end_cmd = curr_cmd->end_cmd;
787 cmd_phase_addr = (unsigned long __force)xnand->nand_base |
788 (curr_cmd->addr_cycles << ADDR_CYCLES_SHIFT) |
789 (end_cmd_valid << END_CMD_VALID_SHIFT) |
790 (COMMAND_PHASE) |
791 (end_cmd << END_CMD_SHIFT) |
792 (curr_cmd->start_cmd << START_CMD_SHIFT);
794 cmd_addr = (void __iomem * __force)cmd_phase_addr;
796 /* Get the data phase address */
797 end_cmd_valid = 0;
799 data_phase_addr = (unsigned long __force)xnand->nand_base |
800 (0x0 << CLEAR_CS_SHIFT) |
801 (end_cmd_valid << END_CMD_VALID_SHIFT) |
802 (DATA_PHASE) |
803 (end_cmd << END_CMD_SHIFT) |
804 (0x0 << ECC_LAST_SHIFT);
806 chip->IO_ADDR_R = (void __iomem * __force)data_phase_addr;
807 chip->IO_ADDR_W = chip->IO_ADDR_R;
809 /* Command phase AXI write */
810 /* Read & Write */
811 if (column != -1 && page_addr != -1) {
812 /* Adjust columns for 16 bit bus width */
813 if (chip->options & NAND_BUSWIDTH_16)
814 column >>= 1;
815 cmd_data = column;
816 if (mtd->writesize > XNANDPSS_ECC_SIZE) {
817 cmd_data |= page_addr << 16;
818 /* Another address cycle for devices > 128MiB */
819 if (chip->chipsize > (128 << 20)) {
820 xnandpss_write32(cmd_addr, cmd_data);
821 cmd_data = (page_addr >> 16);
823 } else
824 cmd_data |= page_addr << 8;
826 /* Erase */
827 else if (page_addr != -1)
828 cmd_data = page_addr;
829 /* Change read/write column, read id etc */
830 else if (column != -1) {
831 /* Adjust columns for 16 bit bus width */
832 if ((chip->options & NAND_BUSWIDTH_16) &&
833 ((command == NAND_CMD_READ0) ||
834 (command == NAND_CMD_SEQIN) ||
835 (command == NAND_CMD_RNDOUT) ||
836 (command == NAND_CMD_RNDIN)))
837 column >>= 1;
838 cmd_data = column;
839 } else
842 xnandpss_write32(cmd_addr, cmd_data);
844 if (curr_cmd->end_cmd_valid) {
845 xnand->end_cmd = curr_cmd->end_cmd;
846 xnand->end_cmd_pending = 1;
849 ndelay(100);
851 if ((command == NAND_CMD_READ0) ||
852 (command == NAND_CMD_ERASE1) ||
853 (command == NAND_CMD_RESET) ||
854 (command == NAND_CMD_PARAM) ||
855 (command == NAND_CMD_GET_FEATURES)) {
857 while (!chip->dev_ready(mtd))
859 return;
864 * xnandpss_read_buf - read chip data into buffer
865 * @mtd: MTD device structure
866 * @buf: buffer to store date
867 * @len: number of bytes to read
870 void xnandpss_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
872 int i;
873 struct nand_chip *chip = mtd->priv;
874 unsigned long *ptr = (unsigned long *)buf;
876 len >>= 2;
877 for (i = 0; i < len; i++)
878 ptr[i] = readl(chip->IO_ADDR_R);
882 * xnandpss_write_buf - write buffer to chip
883 * @mtd: MTD device structure
884 * @buf: data buffer
885 * @len: number of bytes to write
888 void xnandpss_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
890 int i;
891 struct nand_chip *chip = mtd->priv;
892 unsigned long *ptr = (unsigned long *)buf;
893 len >>= 2;
895 for (i = 0; i < len; i++)
896 writel(ptr[i], chip->IO_ADDR_W);
900 * xnandpss_verify_buf - Verify chip data against buffer
901 * @mtd: MTD device structure
902 * @buf: buffer containing the data to compare
903 * @len: number of bytes to compare
906 static int xnandpss_verify_buf(struct mtd_info *mtd, const uint8_t *buf,
907 int len)
909 int i;
910 struct nand_chip *chip = mtd->priv;
911 unsigned long *ptr = (unsigned long *)buf;
912 unsigned long addr;
914 len >>= 2;
915 for (i = 0; i < (len - 1); i++) {
916 if (ptr[i] != readl(chip->IO_ADDR_R))
917 return -EFAULT;
919 addr = (unsigned long __force)chip->IO_ADDR_R;
920 addr |= XNANDPSS_CLEAR_CS;
921 chip->IO_ADDR_R = (void __iomem *__force)addr;
922 if (ptr[i] != readl(chip->IO_ADDR_R))
923 return -EFAULT;
924 return 0;
928 * xnandpss_device_ready - Check device ready/busy line
929 * @mtd: Pointer to the mtd_info structure
931 * returns: 0 on busy or 1 on ready state
933 static int xnandpss_device_ready(struct mtd_info *mtd)
935 struct xnandpss_info *xnand =
936 container_of(mtd, struct xnandpss_info, mtd);
937 unsigned long status;
939 /* Check the raw_int_status1 bit */
940 status = xnandpss_read32(xnand->smc_regs + XSMCPSS_MC_STATUS) & 0x40;
941 /* Clear the interrupt condition */
942 if (status)
943 xnandpss_write32((xnand->smc_regs + XSMCPSS_MC_CLR_CONFIG),
944 (1<<4));
945 return status ? 1 : 0;
948 #ifdef CONFIG_OF
949 static const struct of_device_id __devinitconst xnandpss_of_match[];
950 #endif
952 * xnandpss_probe - Probe method for the NAND driver
953 * @pdev: Pointer to the platform_device structure
955 * This function initializes the driver data structures and the hardware.
957 * returns: 0 on success or error value on failure
959 static int __devinit xnandpss_probe(struct platform_device *pdev)
961 struct xnandpss_info *xnand;
962 struct mtd_info *mtd;
963 struct nand_chip *nand_chip;
964 struct resource *nand_res, *smc_res;
965 unsigned long ecc_page_size;
966 int err = 0;
967 u8 maf_id, dev_id, i;
968 u8 get_feature;
969 u8 set_feature[4] = {0x08, 0x00, 0x00, 0x00};
970 int ondie_ecc_enabled = 0;
971 unsigned long ecc_cfg;
972 struct xnand_platform_data *pdata = NULL;
973 struct mtd_part_parser_data ppdata;
974 #ifdef CONFIG_OF
975 const struct of_device_id *match;
976 const unsigned int *prop;
977 #endif
979 #ifdef CONFIG_OF
980 match = of_match_device(xnandpss_of_match, &pdev->dev);
981 if (match)
982 pdata = match->data;
983 #else
984 pdata = pdev->dev.platform_data;
985 #endif
986 if (pdata == NULL) {
987 dev_err(&pdev->dev, "platform data missing\n");
988 return -ENODEV;
991 xnand = kzalloc(sizeof(struct xnandpss_info), GFP_KERNEL);
992 if (!xnand) {
993 dev_err(&pdev->dev, "failed to allocate device structure.\n");
994 return -ENOMEM;
997 /* Map physical address of NAND flash */
998 nand_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
999 if (nand_res == NULL) {
1000 err = -ENODEV;
1001 dev_err(&pdev->dev, "platform_get_resource for NAND failed\n");
1002 goto out_free_data;
1004 nand_res = request_mem_region(nand_res->start, resource_size(nand_res),
1005 pdev->name);
1006 if (nand_res == NULL) {
1007 err = -ENOMEM;
1008 dev_err(&pdev->dev, "request_mem_region for cont failed\n");
1009 goto out_free_data;
1012 xnand->nand_base = ioremap(nand_res->start, resource_size(nand_res));
1013 if (xnand->nand_base == NULL) {
1014 err = -EIO;
1015 dev_err(&pdev->dev, "ioremap for NAND failed\n");
1016 goto out_release_nand_mem_region;
1018 /* Get the NAND controller virtual address */
1019 smc_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1020 if (smc_res == NULL) {
1021 err = -ENODEV;
1022 dev_err(&pdev->dev, "platform_get_resource for cont failed\n");
1023 goto out_nand_iounmap;
1025 smc_res = request_mem_region(smc_res->start, resource_size(smc_res),
1026 pdev->name);
1027 if (smc_res == NULL) {
1028 err = -ENOMEM;
1029 dev_err(&pdev->dev, "request_mem_region for cont failed\n");
1030 goto out_nand_iounmap;
1033 xnand->smc_regs = ioremap(smc_res->start, resource_size(smc_res));
1034 if (!xnand->smc_regs) {
1035 err = -EIO;
1036 dev_err(&pdev->dev, "ioremap for cont failed\n");
1037 goto out_release_smc_mem_region;
1039 /* Get x8 or x16 mode from device tree */
1040 #ifdef CONFIG_OF
1041 prop = of_get_property(pdev->dev.of_node, "xlnx,nand-width", NULL);
1042 if (prop) {
1043 if (be32_to_cpup(prop) == 16) {
1044 pdata->options |= NAND_BUSWIDTH_16;
1045 } else if (be32_to_cpup(prop) == 8) {
1046 pdata->options &= ~NAND_BUSWIDTH_16;
1047 } else {
1048 dev_info(&pdev->dev, "xlnx,nand-width not valid, using 8");
1049 pdata->options &= ~NAND_BUSWIDTH_16;
1051 } else {
1052 dev_info(&pdev->dev, "xlnx,nand-width not in device tree, using 8");
1053 pdata->options &= ~NAND_BUSWIDTH_16;
1055 #endif
1056 xnand->pdev = pdev;
1057 /* Link the private data with the MTD structure */
1058 mtd = &xnand->mtd;
1059 nand_chip = &xnand->chip;
1061 nand_chip->priv = xnand;
1062 mtd->priv = nand_chip;
1063 mtd->owner = THIS_MODULE;
1064 mtd->name = "xilinx_nand";
1066 /* Set address of NAND IO lines */
1067 nand_chip->IO_ADDR_R = xnand->nand_base;
1068 nand_chip->IO_ADDR_W = xnand->nand_base;
1070 /* Set the driver entry points for MTD */
1071 nand_chip->cmdfunc = xnandpss_cmd_function;
1072 nand_chip->dev_ready = xnandpss_device_ready;
1073 nand_chip->select_chip = xnandpss_select_chip;
1075 /* If we don't set this delay driver sets 20us by default */
1076 nand_chip->chip_delay = 30;
1078 /* Buffer read/write routines */
1079 nand_chip->read_buf = xnandpss_read_buf;
1080 nand_chip->write_buf = xnandpss_write_buf;
1081 nand_chip->verify_buf = xnandpss_verify_buf;
1083 /* Set the device option and flash width */
1084 nand_chip->options = pdata->options;
1085 nand_chip->bbt_options = NAND_BBT_USE_FLASH;
1087 platform_set_drvdata(pdev, xnand);
1089 /* Initialize the NAND flash interface on NAND controller */
1090 xnandpss_init_nand_flash(xnand->smc_regs, nand_chip->options);
1092 /* first scan to find the device and get the page size */
1093 if (nand_scan_ident(mtd, 1, NULL)) {
1094 err = -ENXIO;
1095 dev_err(&pdev->dev, "nand_scan_ident for NAND failed\n");
1096 goto out_unmap_all_mem;
1099 /* Check if On-Die ECC flash */
1100 nand_chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1101 nand_chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
1103 /* Read manufacturer and device IDs */
1104 maf_id = nand_chip->read_byte(mtd);
1105 dev_id = nand_chip->read_byte(mtd);
1107 if ((maf_id == 0x2c) &&
1108 ((dev_id == 0xf1) || (dev_id == 0xa1) ||
1109 (dev_id == 0xb1) ||
1110 (dev_id == 0xaa) || (dev_id == 0xba) ||
1111 (dev_id == 0xda) || (dev_id == 0xca) ||
1112 (dev_id == 0xac) || (dev_id == 0xbc) ||
1113 (dev_id == 0xdc) || (dev_id == 0xcc) ||
1114 (dev_id == 0xa3) || (dev_id == 0xb3) ||
1115 (dev_id == 0xd3) || (dev_id == 0xc3))) {
1117 nand_chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES,
1118 ONDIE_ECC_FEATURE_ADDR, -1);
1119 get_feature = nand_chip->read_byte(mtd);
1121 if (get_feature & 0x08) {
1122 ondie_ecc_enabled = 1;
1123 } else {
1124 nand_chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES,
1125 ONDIE_ECC_FEATURE_ADDR, -1);
1126 for (i = 0; i < 4; i++)
1127 writeb(set_feature[i], nand_chip->IO_ADDR_W);
1129 ndelay(1000);
1131 nand_chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES,
1132 ONDIE_ECC_FEATURE_ADDR, -1);
1133 get_feature = nand_chip->read_byte(mtd);
1135 if (get_feature & 0x08)
1136 ondie_ecc_enabled = 1;
1140 if (ondie_ecc_enabled) {
1141 /* bypass the controller ECC block */
1142 ecc_cfg = xnandpss_read32(xnand->smc_regs +
1143 XSMCPSS_ECC_MEMCFG_OFFSET(XSMCPSS_ECC_IF1_OFFSET));
1144 ecc_cfg &= ~0xc;
1145 xnandpss_write32(xnand->smc_regs +
1146 (XSMCPSS_ECC_MEMCFG_OFFSET(XSMCPSS_ECC_IF1_OFFSET)),
1147 ecc_cfg);
1149 /* The software ECC routines won't work with the
1150 SMC controller */
1151 nand_chip->ecc.mode = NAND_ECC_HW;
1152 nand_chip->ecc.read_page = xnandpss_read_page_raw;
1153 nand_chip->ecc.write_page = xnandpss_write_page_raw;
1154 nand_chip->ecc.read_page_raw = xnandpss_read_page_raw;
1155 nand_chip->ecc.write_page_raw = xnandpss_write_page_raw;
1156 nand_chip->ecc.read_oob = xnandpss_read_oob;
1157 nand_chip->ecc.write_oob = xnandpss_write_oob;
1158 nand_chip->ecc.size = mtd->writesize;
1159 nand_chip->ecc.bytes = 0;
1160 nand_chip->ecc.strength = 1;
1161 /* On-Die ECC spare bytes offset 8 is used for ECC codes */
1162 nand_chip->ecc.layout = &ondie_nand_oob_64;
1163 /* Use the BBT pattern descriptors */
1164 nand_chip->bbt_td = &bbt_main_descr;
1165 nand_chip->bbt_md = &bbt_mirror_descr;
1166 } else {
1167 /* Hardware ECC generates 3 bytes ECC code for each 512 bytes */
1168 nand_chip->ecc.mode = NAND_ECC_HW;
1169 nand_chip->ecc.size = XNANDPSS_ECC_SIZE;
1170 nand_chip->ecc.bytes = 3;
1171 nand_chip->ecc.calculate = xnandpss_calculate_hwecc;
1172 nand_chip->ecc.correct = xnandpss_correct_data;
1173 nand_chip->ecc.hwctl = NULL;
1174 nand_chip->ecc.read_page = xnandpss_read_page_hwecc;
1175 nand_chip->ecc.write_page = xnandpss_write_page_hwecc;
1176 nand_chip->ecc.read_page_raw = xnandpss_read_page_raw;
1177 nand_chip->ecc.write_page_raw = xnandpss_write_page_raw;
1178 nand_chip->ecc.read_oob = xnandpss_read_oob;
1179 nand_chip->ecc.write_oob = xnandpss_write_oob;
1180 nand_chip->ecc.strength = 1;
1182 switch (mtd->writesize) {
1183 case 512:
1184 ecc_page_size = 0x1;
1185 /* Set the ECC memory config register */
1186 xnandpss_write32(xnand->smc_regs +
1187 (XSMCPSS_ECC_MEMCFG_OFFSET(XSMCPSS_ECC_IF1_OFFSET)),
1188 (XNANDPSS_ECC_CONFIG | ecc_page_size));
1189 break;
1190 case 1024:
1191 ecc_page_size = 0x2;
1192 /* Set the ECC memory config register */
1193 xnandpss_write32(xnand->smc_regs +
1194 (XSMCPSS_ECC_MEMCFG_OFFSET(XSMCPSS_ECC_IF1_OFFSET)),
1195 (XNANDPSS_ECC_CONFIG | ecc_page_size));
1196 break;
1197 case 2048:
1198 ecc_page_size = 0x3;
1199 /* Set the ECC memory config register */
1200 xnandpss_write32(xnand->smc_regs +
1201 (XSMCPSS_ECC_MEMCFG_OFFSET(XSMCPSS_ECC_IF1_OFFSET)),
1202 (XNANDPSS_ECC_CONFIG | ecc_page_size));
1203 break;
1204 default:
1205 /* The software ECC routines won't work with the
1206 SMC controller */
1207 nand_chip->ecc.mode = NAND_ECC_HW;
1208 nand_chip->ecc.calculate = nand_calculate_ecc;
1209 nand_chip->ecc.correct = nand_correct_data;
1210 nand_chip->ecc.read_page = xnandpss_read_page_swecc;
1211 /* nand_chip->ecc.read_subpage = nand_read_subpage; */
1212 nand_chip->ecc.write_page = xnandpss_write_page_swecc;
1213 nand_chip->ecc.read_page_raw = xnandpss_read_page_raw;
1214 nand_chip->ecc.write_page_raw = xnandpss_write_page_raw;
1215 nand_chip->ecc.read_oob = xnandpss_read_oob;
1216 nand_chip->ecc.write_oob = xnandpss_write_oob;
1217 nand_chip->ecc.size = 256;
1218 nand_chip->ecc.bytes = 3;
1219 break;
1222 if (mtd->oobsize == 16)
1223 nand_chip->ecc.layout = &nand_oob_16;
1224 else if (mtd->oobsize == 64)
1225 nand_chip->ecc.layout = &nand_oob_64;
1226 else
1230 /* second phase scan */
1231 if (nand_scan_tail(mtd)) {
1232 err = -ENXIO;
1233 dev_err(&pdev->dev, "nand_scan_tail for NAND failed\n");
1234 goto out_unmap_all_mem;
1237 #ifdef CONFIG_OF
1238 ppdata.of_node = pdev->dev.of_node;
1239 #endif
1240 mtd_device_parse_register(&xnand->mtd, NULL, &ppdata,
1241 NULL, 0);
1243 if (!err) {
1244 dev_info(&pdev->dev, "at 0x%08X mapped to 0x%08X\n",
1245 smc_res->start, (u32 __force) xnand->nand_base);
1246 return 0;
1250 out_unmap_all_mem:
1251 platform_set_drvdata(pdev, NULL);
1252 iounmap(xnand->smc_regs);
1253 out_release_smc_mem_region:
1254 release_mem_region(smc_res->start, resource_size(smc_res));
1255 out_nand_iounmap:
1256 iounmap(xnand->nand_base);
1257 out_release_nand_mem_region:
1258 release_mem_region(nand_res->start, resource_size(nand_res));
1259 out_free_data:
1260 kfree(xnand);
1261 return err;
1265 * xnandpss_remove - Remove method for the NAND driver
1266 * @pdev: Pointer to the platform_device structure
1268 * This function is called if the driver module is being unloaded. It frees all
1269 * resources allocated to the device.
1271 * returns: 0 on success or error value on failure
1273 static int __devexit xnandpss_remove(struct platform_device *pdev)
1275 struct xnandpss_info *xnand = platform_get_drvdata(pdev);
1276 struct resource *nand_res, *smc_res;
1278 /* Release resources, unregister device */
1279 nand_release(&xnand->mtd);
1280 /* kfree(NULL) is safe */
1281 kfree(xnand->parts);
1283 platform_set_drvdata(pdev, NULL);
1284 /* Unmap and release physical address */
1285 iounmap(xnand->smc_regs);
1286 smc_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1287 release_mem_region(smc_res->start, resource_size(smc_res));
1289 iounmap(xnand->nand_base);
1290 nand_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1291 release_mem_region(nand_res->start, resource_size(nand_res));
1292 /* Free the MTD device structure */
1293 kfree(xnand);
1294 return 0;
1297 #ifdef CONFIG_OF
1298 static struct xnand_platform_data xnandpss_config;
1300 /* Match table for device tree binding */
1301 static const struct of_device_id __devinitconst xnandpss_of_match[] = {
1302 { .compatible = "xlnx,ps7-nand-1.00.a", .data = &xnandpss_config},
1305 MODULE_DEVICE_TABLE(of, xnandpss_of_match);
1306 #else
1307 #define xnandpss_of_match NULL
1308 #endif
1311 * xnandpss_driver - This structure defines the NAND subsystem platform driver
1313 static struct platform_driver xnandpss_driver = {
1314 .probe = xnandpss_probe,
1315 .remove = __devexit_p(xnandpss_remove),
1316 .suspend = NULL,
1317 .resume = NULL,
1318 .driver = {
1319 .name = XNANDPSS_DRIVER_NAME,
1320 .owner = THIS_MODULE,
1321 #ifdef CONFIG_OF
1322 .of_match_table = xnandpss_of_match,
1323 #endif
1328 * xnandpss_init - NAND driver module initialization function
1330 * returns: 0 on success and error value on failure
1332 static int __init xnandpss_init(void)
1334 return platform_driver_register(&xnandpss_driver);
1338 * xnandpss_exit - NAND driver module exit function
1340 static void __exit xnandpss_exit(void)
1342 platform_driver_unregister(&xnandpss_driver);
1345 module_init(xnandpss_init);
1346 module_exit(xnandpss_exit);
1348 MODULE_AUTHOR("Xilinx, Inc.");
1349 MODULE_ALIAS("platform:" XNANDPSS_DRIVER_NAME);
1350 MODULE_DESCRIPTION("Xilinx PSS NAND Flash Driver");
1351 MODULE_LICENSE("GPL");