eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/mtd/nand/pxa3xx_nand.c |
| 3 | * |
| 4 | * Copyright © 2005 Intel Corporation |
| 5 | * Copyright © 2006 Marvell International Ltd. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
Ezequiel Garcia | de484a3 | 2013-11-07 12:17:10 -0300 | [diff] [blame] | 10 | * |
| 11 | * See Documentation/mtd/nand/pxa3xx-nand.txt for more details. |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 12 | */ |
| 13 | |
Haojian Zhuang | a88bdbb | 2009-09-11 19:33:58 +0800 | [diff] [blame] | 14 | #include <linux/kernel.h> |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/dma-mapping.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/clk.h> |
| 21 | #include <linux/mtd/mtd.h> |
| 22 | #include <linux/mtd/nand.h> |
| 23 | #include <linux/mtd/partitions.h> |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 24 | #include <linux/io.h> |
| 25 | #include <linux/irq.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 27 | #include <linux/of.h> |
| 28 | #include <linux/of_device.h> |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 29 | #include <linux/of_mtd.h> |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 30 | |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 31 | #if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP) |
| 32 | #define ARCH_HAS_DMA |
| 33 | #endif |
| 34 | |
| 35 | #ifdef ARCH_HAS_DMA |
Eric Miao | afb5b5c | 2008-12-01 11:43:08 +0800 | [diff] [blame] | 36 | #include <mach/dma.h> |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 37 | #endif |
| 38 | |
Arnd Bergmann | 293b2da | 2012-08-24 15:16:48 +0200 | [diff] [blame] | 39 | #include <linux/platform_data/mtd-nand-pxa3xx.h> |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 40 | |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 41 | #define NAND_DEV_READY_TIMEOUT 50 |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 42 | #define CHIP_DELAY_TIMEOUT (2 * HZ/10) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 43 | #define NAND_STOP_DELAY (2 * HZ/50) |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 44 | #define PAGE_CHUNK_SIZE (2048) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 45 | |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 46 | /* |
| 47 | * Define a buffer size for the initial command that detects the flash device: |
| 48 | * STATUS, READID and PARAM. The largest of these is the PARAM command, |
| 49 | * needing 256 bytes. |
| 50 | */ |
| 51 | #define INIT_BUFFER_SIZE 256 |
| 52 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 53 | /* registers and bit definitions */ |
| 54 | #define NDCR (0x00) /* Control register */ |
| 55 | #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */ |
| 56 | #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */ |
| 57 | #define NDSR (0x14) /* Status Register */ |
| 58 | #define NDPCR (0x18) /* Page Count Register */ |
| 59 | #define NDBDR0 (0x1C) /* Bad Block Register 0 */ |
| 60 | #define NDBDR1 (0x20) /* Bad Block Register 1 */ |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 61 | #define NDECCCTRL (0x28) /* ECC control */ |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 62 | #define NDDB (0x40) /* Data Buffer */ |
| 63 | #define NDCB0 (0x48) /* Command Buffer0 */ |
| 64 | #define NDCB1 (0x4C) /* Command Buffer1 */ |
| 65 | #define NDCB2 (0x50) /* Command Buffer2 */ |
| 66 | |
| 67 | #define NDCR_SPARE_EN (0x1 << 31) |
| 68 | #define NDCR_ECC_EN (0x1 << 30) |
| 69 | #define NDCR_DMA_EN (0x1 << 29) |
| 70 | #define NDCR_ND_RUN (0x1 << 28) |
| 71 | #define NDCR_DWIDTH_C (0x1 << 27) |
| 72 | #define NDCR_DWIDTH_M (0x1 << 26) |
| 73 | #define NDCR_PAGE_SZ (0x1 << 24) |
| 74 | #define NDCR_NCSX (0x1 << 23) |
| 75 | #define NDCR_ND_MODE (0x3 << 21) |
| 76 | #define NDCR_NAND_MODE (0x0) |
| 77 | #define NDCR_CLR_PG_CNT (0x1 << 20) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 78 | #define NDCR_STOP_ON_UNCOR (0x1 << 19) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 79 | #define NDCR_RD_ID_CNT_MASK (0x7 << 16) |
| 80 | #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK) |
| 81 | |
| 82 | #define NDCR_RA_START (0x1 << 15) |
| 83 | #define NDCR_PG_PER_BLK (0x1 << 14) |
| 84 | #define NDCR_ND_ARB_EN (0x1 << 12) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 85 | #define NDCR_INT_MASK (0xFFF) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 86 | |
| 87 | #define NDSR_MASK (0xfff) |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 88 | #define NDSR_ERR_CNT_OFF (16) |
| 89 | #define NDSR_ERR_CNT_MASK (0x1f) |
| 90 | #define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 91 | #define NDSR_RDY (0x1 << 12) |
| 92 | #define NDSR_FLASH_RDY (0x1 << 11) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 93 | #define NDSR_CS0_PAGED (0x1 << 10) |
| 94 | #define NDSR_CS1_PAGED (0x1 << 9) |
| 95 | #define NDSR_CS0_CMDD (0x1 << 8) |
| 96 | #define NDSR_CS1_CMDD (0x1 << 7) |
| 97 | #define NDSR_CS0_BBD (0x1 << 6) |
| 98 | #define NDSR_CS1_BBD (0x1 << 5) |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 99 | #define NDSR_UNCORERR (0x1 << 4) |
| 100 | #define NDSR_CORERR (0x1 << 3) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 101 | #define NDSR_WRDREQ (0x1 << 2) |
| 102 | #define NDSR_RDDREQ (0x1 << 1) |
| 103 | #define NDSR_WRCMDREQ (0x1) |
| 104 | |
Ezequiel Garcia | 41a6343 | 2013-08-12 14:14:51 -0300 | [diff] [blame] | 105 | #define NDCB0_LEN_OVRD (0x1 << 28) |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 106 | #define NDCB0_ST_ROW_EN (0x1 << 26) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 107 | #define NDCB0_AUTO_RS (0x1 << 25) |
| 108 | #define NDCB0_CSEL (0x1 << 24) |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 109 | #define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29) |
| 110 | #define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 111 | #define NDCB0_CMD_TYPE_MASK (0x7 << 21) |
| 112 | #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK) |
| 113 | #define NDCB0_NC (0x1 << 20) |
| 114 | #define NDCB0_DBC (0x1 << 19) |
| 115 | #define NDCB0_ADDR_CYC_MASK (0x7 << 16) |
| 116 | #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK) |
| 117 | #define NDCB0_CMD2_MASK (0xff << 8) |
| 118 | #define NDCB0_CMD1_MASK (0xff) |
| 119 | #define NDCB0_ADDR_CYC_SHIFT (16) |
| 120 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 121 | #define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */ |
| 122 | #define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */ |
| 123 | #define EXT_CMD_TYPE_READ 4 /* Read */ |
| 124 | #define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */ |
| 125 | #define EXT_CMD_TYPE_FINAL 3 /* Final command */ |
| 126 | #define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */ |
| 127 | #define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */ |
| 128 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 129 | /* macros for registers read/write */ |
| 130 | #define nand_writel(info, off, val) \ |
| 131 | __raw_writel((val), (info)->mmio_base + (off)) |
| 132 | |
| 133 | #define nand_readl(info, off) \ |
| 134 | __raw_readl((info)->mmio_base + (off)) |
| 135 | |
| 136 | /* error code and state */ |
| 137 | enum { |
| 138 | ERR_NONE = 0, |
| 139 | ERR_DMABUSERR = -1, |
| 140 | ERR_SENDCMD = -2, |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 141 | ERR_UNCORERR = -3, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 142 | ERR_BBERR = -4, |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 143 | ERR_CORERR = -5, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | enum { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 147 | STATE_IDLE = 0, |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 148 | STATE_PREPARED, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 149 | STATE_CMD_HANDLE, |
| 150 | STATE_DMA_READING, |
| 151 | STATE_DMA_WRITING, |
| 152 | STATE_DMA_DONE, |
| 153 | STATE_PIO_READING, |
| 154 | STATE_PIO_WRITING, |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 155 | STATE_CMD_DONE, |
| 156 | STATE_READY, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 157 | }; |
| 158 | |
Ezequiel Garcia | c0f3b86 | 2013-08-10 16:34:52 -0300 | [diff] [blame] | 159 | enum pxa3xx_nand_variant { |
| 160 | PXA3XX_NAND_VARIANT_PXA, |
| 161 | PXA3XX_NAND_VARIANT_ARMADA370, |
| 162 | }; |
| 163 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 164 | struct pxa3xx_nand_host { |
| 165 | struct nand_chip chip; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 166 | struct mtd_info *mtd; |
| 167 | void *info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 168 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 169 | /* page size of attached chip */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 170 | int use_ecc; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 171 | int cs; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 172 | |
| 173 | /* calculated from pxa3xx_nand_flash data */ |
| 174 | unsigned int col_addr_cycles; |
| 175 | unsigned int row_addr_cycles; |
| 176 | size_t read_id_bytes; |
| 177 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | struct pxa3xx_nand_info { |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 181 | struct nand_hw_control controller; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 182 | struct platform_device *pdev; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 183 | |
| 184 | struct clk *clk; |
| 185 | void __iomem *mmio_base; |
Haojian Zhuang | 8638fac | 2009-09-10 14:11:44 +0800 | [diff] [blame] | 186 | unsigned long mmio_phys; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 187 | struct completion cmd_complete, dev_ready; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 188 | |
| 189 | unsigned int buf_start; |
| 190 | unsigned int buf_count; |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 191 | unsigned int buf_size; |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 192 | unsigned int data_buff_pos; |
| 193 | unsigned int oob_buff_pos; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 194 | |
| 195 | /* DMA information */ |
| 196 | int drcmr_dat; |
| 197 | int drcmr_cmd; |
| 198 | |
| 199 | unsigned char *data_buff; |
Lei Wen | 18c81b1 | 2010-08-17 17:25:57 +0800 | [diff] [blame] | 200 | unsigned char *oob_buff; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 201 | dma_addr_t data_buff_phys; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 202 | int data_dma_ch; |
| 203 | struct pxa_dma_desc *data_desc; |
| 204 | dma_addr_t data_desc_addr; |
| 205 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 206 | struct pxa3xx_nand_host *host[NUM_CHIP_SELECT]; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 207 | unsigned int state; |
| 208 | |
Ezequiel Garcia | c0f3b86 | 2013-08-10 16:34:52 -0300 | [diff] [blame] | 209 | /* |
| 210 | * This driver supports NFCv1 (as found in PXA SoC) |
| 211 | * and NFCv2 (as found in Armada 370/XP SoC). |
| 212 | */ |
| 213 | enum pxa3xx_nand_variant variant; |
| 214 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 215 | int cs; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 216 | int use_ecc; /* use HW ECC ? */ |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 217 | int ecc_bch; /* using BCH ECC? */ |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 218 | int use_dma; /* use DMA ? */ |
Ezequiel Garcia | 5bb653e | 2013-08-12 14:14:49 -0300 | [diff] [blame] | 219 | int use_spare; /* use spare ? */ |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 220 | int need_wait; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 221 | |
Ezequiel Garcia | 2128b08 | 2013-11-07 12:17:16 -0300 | [diff] [blame] | 222 | unsigned int data_size; /* data to be read from FIFO */ |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 223 | unsigned int chunk_size; /* split commands chunk size */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 224 | unsigned int oob_size; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 225 | unsigned int spare_size; |
| 226 | unsigned int ecc_size; |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 227 | unsigned int ecc_err_cnt; |
| 228 | unsigned int max_bitflips; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 229 | int retcode; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 230 | |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 231 | /* cached register value */ |
| 232 | uint32_t reg_ndcr; |
| 233 | uint32_t ndtr0cs0; |
| 234 | uint32_t ndtr1cs0; |
| 235 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 236 | /* generated NDCBx register values */ |
| 237 | uint32_t ndcb0; |
| 238 | uint32_t ndcb1; |
| 239 | uint32_t ndcb2; |
Ezequiel Garcia | 3a1a344 | 2013-08-12 14:14:50 -0300 | [diff] [blame] | 240 | uint32_t ndcb3; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 241 | }; |
| 242 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 243 | static bool use_dma = 1; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 244 | module_param(use_dma, bool, 0444); |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 245 | MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW"); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 246 | |
Lei Wen | c1f8247 | 2010-08-17 13:50:23 +0800 | [diff] [blame] | 247 | static struct pxa3xx_nand_timing timing[] = { |
Lei Wen | 227a886 | 2010-08-18 18:00:03 +0800 | [diff] [blame] | 248 | { 40, 80, 60, 100, 80, 100, 90000, 400, 40, }, |
| 249 | { 10, 0, 20, 40, 30, 40, 11123, 110, 10, }, |
| 250 | { 10, 25, 15, 25, 15, 30, 25000, 60, 10, }, |
| 251 | { 10, 35, 15, 25, 15, 25, 25000, 60, 10, }, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 252 | }; |
| 253 | |
Lei Wen | c1f8247 | 2010-08-17 13:50:23 +0800 | [diff] [blame] | 254 | static struct pxa3xx_nand_flash builtin_flash_types[] = { |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 255 | { "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] }, |
| 256 | { "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] }, |
| 257 | { "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] }, |
| 258 | { "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] }, |
| 259 | { "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] }, |
| 260 | { "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] }, |
| 261 | { "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] }, |
| 262 | { "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] }, |
| 263 | { "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] }, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 264 | }; |
| 265 | |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 266 | static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' }; |
| 267 | static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' }; |
| 268 | |
| 269 | static struct nand_bbt_descr bbt_main_descr = { |
| 270 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 271 | | NAND_BBT_2BIT | NAND_BBT_VERSION, |
| 272 | .offs = 8, |
| 273 | .len = 6, |
| 274 | .veroffs = 14, |
| 275 | .maxblocks = 8, /* Last 8 blocks in each chip */ |
| 276 | .pattern = bbt_pattern |
| 277 | }; |
| 278 | |
| 279 | static struct nand_bbt_descr bbt_mirror_descr = { |
| 280 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 281 | | NAND_BBT_2BIT | NAND_BBT_VERSION, |
| 282 | .offs = 8, |
| 283 | .len = 6, |
| 284 | .veroffs = 14, |
| 285 | .maxblocks = 8, /* Last 8 blocks in each chip */ |
| 286 | .pattern = bbt_mirror_pattern |
| 287 | }; |
| 288 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 289 | static struct nand_ecclayout ecc_layout_4KB_bch4bit = { |
| 290 | .eccbytes = 64, |
| 291 | .eccpos = { |
| 292 | 32, 33, 34, 35, 36, 37, 38, 39, |
| 293 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 294 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 295 | 56, 57, 58, 59, 60, 61, 62, 63, |
| 296 | 96, 97, 98, 99, 100, 101, 102, 103, |
| 297 | 104, 105, 106, 107, 108, 109, 110, 111, |
| 298 | 112, 113, 114, 115, 116, 117, 118, 119, |
| 299 | 120, 121, 122, 123, 124, 125, 126, 127}, |
| 300 | /* Bootrom looks in bytes 0 & 5 for bad blocks */ |
| 301 | .oobfree = { {6, 26}, { 64, 32} } |
| 302 | }; |
| 303 | |
| 304 | static struct nand_ecclayout ecc_layout_4KB_bch8bit = { |
| 305 | .eccbytes = 128, |
| 306 | .eccpos = { |
| 307 | 32, 33, 34, 35, 36, 37, 38, 39, |
| 308 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 309 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 310 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 311 | .oobfree = { } |
| 312 | }; |
| 313 | |
Lei Wen | 227a886 | 2010-08-18 18:00:03 +0800 | [diff] [blame] | 314 | /* Define a default flash type setting serve as flash detecting only */ |
| 315 | #define DEFAULT_FLASH_TYPE (&builtin_flash_types[0]) |
| 316 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 317 | #define NDTR0_tCH(c) (min((c), 7) << 19) |
| 318 | #define NDTR0_tCS(c) (min((c), 7) << 16) |
| 319 | #define NDTR0_tWH(c) (min((c), 7) << 11) |
| 320 | #define NDTR0_tWP(c) (min((c), 7) << 8) |
| 321 | #define NDTR0_tRH(c) (min((c), 7) << 3) |
| 322 | #define NDTR0_tRP(c) (min((c), 7) << 0) |
| 323 | |
| 324 | #define NDTR1_tR(c) (min((c), 65535) << 16) |
| 325 | #define NDTR1_tWHR(c) (min((c), 15) << 4) |
| 326 | #define NDTR1_tAR(c) (min((c), 15) << 0) |
| 327 | |
| 328 | /* convert nano-seconds to nand flash controller clock cycles */ |
Axel Lin | 93b352f | 2010-08-16 16:09:09 +0800 | [diff] [blame] | 329 | #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 330 | |
Ezequiel Garcia | c7e9c7e | 2013-11-07 12:17:14 -0300 | [diff] [blame] | 331 | static struct of_device_id pxa3xx_nand_dt_ids[] = { |
| 332 | { |
| 333 | .compatible = "marvell,pxa3xx-nand", |
| 334 | .data = (void *)PXA3XX_NAND_VARIANT_PXA, |
| 335 | }, |
| 336 | {} |
| 337 | }; |
| 338 | MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids); |
| 339 | |
| 340 | static enum pxa3xx_nand_variant |
| 341 | pxa3xx_nand_get_variant(struct platform_device *pdev) |
| 342 | { |
| 343 | const struct of_device_id *of_id = |
| 344 | of_match_device(pxa3xx_nand_dt_ids, &pdev->dev); |
| 345 | if (!of_id) |
| 346 | return PXA3XX_NAND_VARIANT_PXA; |
| 347 | return (enum pxa3xx_nand_variant)of_id->data; |
| 348 | } |
| 349 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 350 | static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host, |
Enrico Scholz | 7dad482 | 2008-08-29 12:59:50 +0200 | [diff] [blame] | 351 | const struct pxa3xx_nand_timing *t) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 352 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 353 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 354 | unsigned long nand_clk = clk_get_rate(info->clk); |
| 355 | uint32_t ndtr0, ndtr1; |
| 356 | |
| 357 | ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) | |
| 358 | NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) | |
| 359 | NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) | |
| 360 | NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) | |
| 361 | NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) | |
| 362 | NDTR0_tRP(ns2cycle(t->tRP, nand_clk)); |
| 363 | |
| 364 | ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) | |
| 365 | NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) | |
| 366 | NDTR1_tAR(ns2cycle(t->tAR, nand_clk)); |
| 367 | |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 368 | info->ndtr0cs0 = ndtr0; |
| 369 | info->ndtr1cs0 = ndtr1; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 370 | nand_writel(info, NDTR0CS0, ndtr0); |
| 371 | nand_writel(info, NDTR1CS0, ndtr1); |
| 372 | } |
| 373 | |
Ezequiel Garcia | 6a3e486 | 2013-11-07 12:17:18 -0300 | [diff] [blame] | 374 | /* |
| 375 | * Set the data and OOB size, depending on the selected |
| 376 | * spare and ECC configuration. |
| 377 | * Only applicable to READ0, READOOB and PAGEPROG commands. |
| 378 | */ |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 379 | static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info, |
| 380 | struct mtd_info *mtd) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 381 | { |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 382 | int oob_enable = info->reg_ndcr & NDCR_SPARE_EN; |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 383 | |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 384 | info->data_size = mtd->writesize; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 385 | if (!oob_enable) |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 386 | return; |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 387 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 388 | info->oob_size = info->spare_size; |
| 389 | if (!info->use_ecc) |
| 390 | info->oob_size += info->ecc_size; |
Lei Wen | 18c81b1 | 2010-08-17 17:25:57 +0800 | [diff] [blame] | 391 | } |
| 392 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 393 | /** |
| 394 | * NOTE: it is a must to set ND_RUN firstly, then write |
| 395 | * command buffer, otherwise, it does not work. |
| 396 | * We enable all the interrupt at the same time, and |
| 397 | * let pxa3xx_nand_irq to handle all logic. |
| 398 | */ |
| 399 | static void pxa3xx_nand_start(struct pxa3xx_nand_info *info) |
| 400 | { |
| 401 | uint32_t ndcr; |
| 402 | |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 403 | ndcr = info->reg_ndcr; |
Ezequiel Garcia | cd9d118 | 2013-08-12 14:14:48 -0300 | [diff] [blame] | 404 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 405 | if (info->use_ecc) { |
Ezequiel Garcia | cd9d118 | 2013-08-12 14:14:48 -0300 | [diff] [blame] | 406 | ndcr |= NDCR_ECC_EN; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 407 | if (info->ecc_bch) |
| 408 | nand_writel(info, NDECCCTRL, 0x1); |
| 409 | } else { |
Ezequiel Garcia | cd9d118 | 2013-08-12 14:14:48 -0300 | [diff] [blame] | 410 | ndcr &= ~NDCR_ECC_EN; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 411 | if (info->ecc_bch) |
| 412 | nand_writel(info, NDECCCTRL, 0x0); |
| 413 | } |
Ezequiel Garcia | cd9d118 | 2013-08-12 14:14:48 -0300 | [diff] [blame] | 414 | |
| 415 | if (info->use_dma) |
| 416 | ndcr |= NDCR_DMA_EN; |
| 417 | else |
| 418 | ndcr &= ~NDCR_DMA_EN; |
| 419 | |
Ezequiel Garcia | 5bb653e | 2013-08-12 14:14:49 -0300 | [diff] [blame] | 420 | if (info->use_spare) |
| 421 | ndcr |= NDCR_SPARE_EN; |
| 422 | else |
| 423 | ndcr &= ~NDCR_SPARE_EN; |
| 424 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 425 | ndcr |= NDCR_ND_RUN; |
| 426 | |
| 427 | /* clear status bits and run */ |
| 428 | nand_writel(info, NDCR, 0); |
| 429 | nand_writel(info, NDSR, NDSR_MASK); |
| 430 | nand_writel(info, NDCR, ndcr); |
| 431 | } |
| 432 | |
| 433 | static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info) |
| 434 | { |
| 435 | uint32_t ndcr; |
| 436 | int timeout = NAND_STOP_DELAY; |
| 437 | |
| 438 | /* wait RUN bit in NDCR become 0 */ |
| 439 | ndcr = nand_readl(info, NDCR); |
| 440 | while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) { |
| 441 | ndcr = nand_readl(info, NDCR); |
| 442 | udelay(1); |
| 443 | } |
| 444 | |
| 445 | if (timeout <= 0) { |
| 446 | ndcr &= ~NDCR_ND_RUN; |
| 447 | nand_writel(info, NDCR, ndcr); |
| 448 | } |
| 449 | /* clear status bits */ |
| 450 | nand_writel(info, NDSR, NDSR_MASK); |
| 451 | } |
| 452 | |
Ezequiel Garcia | 57ff88f | 2013-08-12 14:14:57 -0300 | [diff] [blame] | 453 | static void __maybe_unused |
| 454 | enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 455 | { |
| 456 | uint32_t ndcr; |
| 457 | |
| 458 | ndcr = nand_readl(info, NDCR); |
| 459 | nand_writel(info, NDCR, ndcr & ~int_mask); |
| 460 | } |
| 461 | |
| 462 | static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask) |
| 463 | { |
| 464 | uint32_t ndcr; |
| 465 | |
| 466 | ndcr = nand_readl(info, NDCR); |
| 467 | nand_writel(info, NDCR, ndcr | int_mask); |
| 468 | } |
| 469 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 470 | static void handle_data_pio(struct pxa3xx_nand_info *info) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 471 | { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 472 | unsigned int do_bytes = min(info->data_size, info->chunk_size); |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 473 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 474 | switch (info->state) { |
| 475 | case STATE_PIO_WRITING: |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 476 | __raw_writesl(info->mmio_base + NDDB, |
| 477 | info->data_buff + info->data_buff_pos, |
| 478 | DIV_ROUND_UP(do_bytes, 4)); |
| 479 | |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 480 | if (info->oob_size > 0) |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 481 | __raw_writesl(info->mmio_base + NDDB, |
| 482 | info->oob_buff + info->oob_buff_pos, |
| 483 | DIV_ROUND_UP(info->oob_size, 4)); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 484 | break; |
| 485 | case STATE_PIO_READING: |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 486 | __raw_readsl(info->mmio_base + NDDB, |
| 487 | info->data_buff + info->data_buff_pos, |
| 488 | DIV_ROUND_UP(do_bytes, 4)); |
| 489 | |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 490 | if (info->oob_size > 0) |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 491 | __raw_readsl(info->mmio_base + NDDB, |
| 492 | info->oob_buff + info->oob_buff_pos, |
| 493 | DIV_ROUND_UP(info->oob_size, 4)); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 494 | break; |
| 495 | default: |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 496 | dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 497 | info->state); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 498 | BUG(); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 499 | } |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 500 | |
| 501 | /* Update buffer pointers for multi-page read/write */ |
| 502 | info->data_buff_pos += do_bytes; |
| 503 | info->oob_buff_pos += info->oob_size; |
| 504 | info->data_size -= do_bytes; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 505 | } |
| 506 | |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 507 | #ifdef ARCH_HAS_DMA |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 508 | static void start_data_dma(struct pxa3xx_nand_info *info) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 509 | { |
| 510 | struct pxa_dma_desc *desc = info->data_desc; |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 511 | int dma_len = ALIGN(info->data_size + info->oob_size, 32); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 512 | |
| 513 | desc->ddadr = DDADR_STOP; |
| 514 | desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len; |
| 515 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 516 | switch (info->state) { |
| 517 | case STATE_DMA_WRITING: |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 518 | desc->dsadr = info->data_buff_phys; |
Haojian Zhuang | 8638fac | 2009-09-10 14:11:44 +0800 | [diff] [blame] | 519 | desc->dtadr = info->mmio_phys + NDDB; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 520 | desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 521 | break; |
| 522 | case STATE_DMA_READING: |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 523 | desc->dtadr = info->data_buff_phys; |
Haojian Zhuang | 8638fac | 2009-09-10 14:11:44 +0800 | [diff] [blame] | 524 | desc->dsadr = info->mmio_phys + NDDB; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 525 | desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 526 | break; |
| 527 | default: |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 528 | dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__, |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 529 | info->state); |
| 530 | BUG(); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch; |
| 534 | DDADR(info->data_dma_ch) = info->data_desc_addr; |
| 535 | DCSR(info->data_dma_ch) |= DCSR_RUN; |
| 536 | } |
| 537 | |
| 538 | static void pxa3xx_nand_data_dma_irq(int channel, void *data) |
| 539 | { |
| 540 | struct pxa3xx_nand_info *info = data; |
| 541 | uint32_t dcsr; |
| 542 | |
| 543 | dcsr = DCSR(channel); |
| 544 | DCSR(channel) = dcsr; |
| 545 | |
| 546 | if (dcsr & DCSR_BUSERR) { |
| 547 | info->retcode = ERR_DMABUSERR; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 548 | } |
| 549 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 550 | info->state = STATE_DMA_DONE; |
| 551 | enable_int(info, NDCR_INT_MASK); |
| 552 | nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 553 | } |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 554 | #else |
| 555 | static void start_data_dma(struct pxa3xx_nand_info *info) |
| 556 | {} |
| 557 | #endif |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 558 | |
| 559 | static irqreturn_t pxa3xx_nand_irq(int irq, void *devid) |
| 560 | { |
| 561 | struct pxa3xx_nand_info *info = devid; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 562 | unsigned int status, is_completed = 0, is_ready = 0; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 563 | unsigned int ready, cmd_done; |
| 564 | |
| 565 | if (info->cs == 0) { |
| 566 | ready = NDSR_FLASH_RDY; |
| 567 | cmd_done = NDSR_CS0_CMDD; |
| 568 | } else { |
| 569 | ready = NDSR_RDY; |
| 570 | cmd_done = NDSR_CS1_CMDD; |
| 571 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 572 | |
| 573 | status = nand_readl(info, NDSR); |
| 574 | |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 575 | if (status & NDSR_UNCORERR) |
| 576 | info->retcode = ERR_UNCORERR; |
| 577 | if (status & NDSR_CORERR) { |
| 578 | info->retcode = ERR_CORERR; |
| 579 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 && |
| 580 | info->ecc_bch) |
| 581 | info->ecc_err_cnt = NDSR_ERR_CNT(status); |
| 582 | else |
| 583 | info->ecc_err_cnt = 1; |
| 584 | |
| 585 | /* |
| 586 | * Each chunk composing a page is corrected independently, |
| 587 | * and we need to store maximum number of corrected bitflips |
| 588 | * to return it to the MTD layer in ecc.read_page(). |
| 589 | */ |
| 590 | info->max_bitflips = max_t(unsigned int, |
| 591 | info->max_bitflips, |
| 592 | info->ecc_err_cnt); |
| 593 | } |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 594 | if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) { |
| 595 | /* whether use dma to transfer data */ |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 596 | if (info->use_dma) { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 597 | disable_int(info, NDCR_INT_MASK); |
| 598 | info->state = (status & NDSR_RDDREQ) ? |
| 599 | STATE_DMA_READING : STATE_DMA_WRITING; |
| 600 | start_data_dma(info); |
| 601 | goto NORMAL_IRQ_EXIT; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 602 | } else { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 603 | info->state = (status & NDSR_RDDREQ) ? |
| 604 | STATE_PIO_READING : STATE_PIO_WRITING; |
| 605 | handle_data_pio(info); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 606 | } |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 607 | } |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 608 | if (status & cmd_done) { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 609 | info->state = STATE_CMD_DONE; |
| 610 | is_completed = 1; |
| 611 | } |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 612 | if (status & ready) { |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 613 | info->state = STATE_READY; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 614 | is_ready = 1; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 615 | } |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 616 | |
| 617 | if (status & NDSR_WRCMDREQ) { |
| 618 | nand_writel(info, NDSR, NDSR_WRCMDREQ); |
| 619 | status &= ~NDSR_WRCMDREQ; |
| 620 | info->state = STATE_CMD_HANDLE; |
Ezequiel Garcia | 3a1a344 | 2013-08-12 14:14:50 -0300 | [diff] [blame] | 621 | |
| 622 | /* |
| 623 | * Command buffer registers NDCB{0-2} (and optionally NDCB3) |
| 624 | * must be loaded by writing directly either 12 or 16 |
| 625 | * bytes directly to NDCB0, four bytes at a time. |
| 626 | * |
| 627 | * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored |
| 628 | * but each NDCBx register can be read. |
| 629 | */ |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 630 | nand_writel(info, NDCB0, info->ndcb0); |
| 631 | nand_writel(info, NDCB0, info->ndcb1); |
| 632 | nand_writel(info, NDCB0, info->ndcb2); |
Ezequiel Garcia | 3a1a344 | 2013-08-12 14:14:50 -0300 | [diff] [blame] | 633 | |
| 634 | /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */ |
| 635 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) |
| 636 | nand_writel(info, NDCB0, info->ndcb3); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 637 | } |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 638 | |
| 639 | /* clear NDSR to let the controller exit the IRQ */ |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 640 | nand_writel(info, NDSR, status); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 641 | if (is_completed) |
| 642 | complete(&info->cmd_complete); |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 643 | if (is_ready) |
| 644 | complete(&info->dev_ready); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 645 | NORMAL_IRQ_EXIT: |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 646 | return IRQ_HANDLED; |
| 647 | } |
| 648 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 649 | static inline int is_buf_blank(uint8_t *buf, size_t len) |
| 650 | { |
| 651 | for (; len > 0; len--) |
| 652 | if (*buf++ != 0xff) |
| 653 | return 0; |
| 654 | return 1; |
| 655 | } |
| 656 | |
Ezequiel Garcia | 86beeba | 2013-11-14 18:25:31 -0300 | [diff] [blame] | 657 | static void set_command_address(struct pxa3xx_nand_info *info, |
| 658 | unsigned int page_size, uint16_t column, int page_addr) |
| 659 | { |
| 660 | /* small page addr setting */ |
| 661 | if (page_size < PAGE_CHUNK_SIZE) { |
| 662 | info->ndcb1 = ((page_addr & 0xFFFFFF) << 8) |
| 663 | | (column & 0xFF); |
| 664 | |
| 665 | info->ndcb2 = 0; |
| 666 | } else { |
| 667 | info->ndcb1 = ((page_addr & 0xFFFF) << 16) |
| 668 | | (column & 0xFFFF); |
| 669 | |
| 670 | if (page_addr & 0xFF0000) |
| 671 | info->ndcb2 = (page_addr & 0xFF0000) >> 16; |
| 672 | else |
| 673 | info->ndcb2 = 0; |
| 674 | } |
| 675 | } |
| 676 | |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 677 | static void prepare_start_command(struct pxa3xx_nand_info *info, int command) |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 678 | { |
Ezequiel Garcia | 39f83d1 | 2013-11-14 18:25:34 -0300 | [diff] [blame] | 679 | struct pxa3xx_nand_host *host = info->host[info->cs]; |
| 680 | struct mtd_info *mtd = host->mtd; |
| 681 | |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 682 | /* reset data and oob column point to handle data */ |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 683 | info->buf_start = 0; |
| 684 | info->buf_count = 0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 685 | info->oob_size = 0; |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 686 | info->data_buff_pos = 0; |
| 687 | info->oob_buff_pos = 0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 688 | info->use_ecc = 0; |
Ezequiel Garcia | 5bb653e | 2013-08-12 14:14:49 -0300 | [diff] [blame] | 689 | info->use_spare = 1; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 690 | info->retcode = ERR_NONE; |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 691 | info->ecc_err_cnt = 0; |
Ezequiel Garcia | f0e6a32e | 2013-11-14 18:25:30 -0300 | [diff] [blame] | 692 | info->ndcb3 = 0; |
Ezequiel Garcia | d20d0a6 | 2013-12-18 18:44:08 -0300 | [diff] [blame^] | 693 | info->need_wait = 0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 694 | |
| 695 | switch (command) { |
| 696 | case NAND_CMD_READ0: |
| 697 | case NAND_CMD_PAGEPROG: |
| 698 | info->use_ecc = 1; |
| 699 | case NAND_CMD_READOOB: |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 700 | pxa3xx_set_datasize(info, mtd); |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 701 | break; |
Ezequiel Garcia | 41a6343 | 2013-08-12 14:14:51 -0300 | [diff] [blame] | 702 | case NAND_CMD_PARAM: |
| 703 | info->use_spare = 0; |
| 704 | break; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 705 | default: |
| 706 | info->ndcb1 = 0; |
| 707 | info->ndcb2 = 0; |
| 708 | break; |
| 709 | } |
Ezequiel Garcia | 39f83d1 | 2013-11-14 18:25:34 -0300 | [diff] [blame] | 710 | |
| 711 | /* |
| 712 | * If we are about to issue a read command, or about to set |
| 713 | * the write address, then clean the data buffer. |
| 714 | */ |
| 715 | if (command == NAND_CMD_READ0 || |
| 716 | command == NAND_CMD_READOOB || |
| 717 | command == NAND_CMD_SEQIN) { |
| 718 | |
| 719 | info->buf_count = mtd->writesize + mtd->oobsize; |
| 720 | memset(info->data_buff, 0xFF, info->buf_count); |
| 721 | } |
| 722 | |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static int prepare_set_command(struct pxa3xx_nand_info *info, int command, |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 726 | int ext_cmd_type, uint16_t column, int page_addr) |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 727 | { |
| 728 | int addr_cycle, exec_cmd; |
| 729 | struct pxa3xx_nand_host *host; |
| 730 | struct mtd_info *mtd; |
| 731 | |
| 732 | host = info->host[info->cs]; |
| 733 | mtd = host->mtd; |
| 734 | addr_cycle = 0; |
| 735 | exec_cmd = 1; |
| 736 | |
| 737 | if (info->cs != 0) |
| 738 | info->ndcb0 = NDCB0_CSEL; |
| 739 | else |
| 740 | info->ndcb0 = 0; |
| 741 | |
| 742 | if (command == NAND_CMD_SEQIN) |
| 743 | exec_cmd = 0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 744 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 745 | addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles |
| 746 | + host->col_addr_cycles); |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 747 | |
| 748 | switch (command) { |
| 749 | case NAND_CMD_READOOB: |
| 750 | case NAND_CMD_READ0: |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 751 | info->buf_start = column; |
| 752 | info->ndcb0 |= NDCB0_CMD_TYPE(0) |
| 753 | | addr_cycle |
| 754 | | NAND_CMD_READ0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 755 | |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 756 | if (command == NAND_CMD_READOOB) |
| 757 | info->buf_start += mtd->writesize; |
| 758 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 759 | /* |
| 760 | * Multiple page read needs an 'extended command type' field, |
| 761 | * which is either naked-read or last-read according to the |
| 762 | * state. |
| 763 | */ |
| 764 | if (mtd->writesize == PAGE_CHUNK_SIZE) { |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 765 | info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8); |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 766 | } else if (mtd->writesize > PAGE_CHUNK_SIZE) { |
| 767 | info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8) |
| 768 | | NDCB0_LEN_OVRD |
| 769 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type); |
| 770 | info->ndcb3 = info->chunk_size + |
| 771 | info->oob_size; |
| 772 | } |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 773 | |
Ezequiel Garcia | 01d9947 | 2013-11-14 18:25:32 -0300 | [diff] [blame] | 774 | set_command_address(info, mtd->writesize, column, page_addr); |
Ezequiel Garcia | 01d9947 | 2013-11-14 18:25:32 -0300 | [diff] [blame] | 775 | break; |
| 776 | |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 777 | case NAND_CMD_SEQIN: |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 778 | |
Ezequiel Garcia | e7f9a6a | 2013-11-14 18:25:35 -0300 | [diff] [blame] | 779 | info->buf_start = column; |
| 780 | set_command_address(info, mtd->writesize, 0, page_addr); |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 781 | |
| 782 | /* |
| 783 | * Multiple page programming needs to execute the initial |
| 784 | * SEQIN command that sets the page address. |
| 785 | */ |
| 786 | if (mtd->writesize > PAGE_CHUNK_SIZE) { |
| 787 | info->ndcb0 |= NDCB0_CMD_TYPE(0x1) |
| 788 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type) |
| 789 | | addr_cycle |
| 790 | | command; |
| 791 | /* No data transfer in this case */ |
| 792 | info->data_size = 0; |
| 793 | exec_cmd = 1; |
| 794 | } |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 795 | break; |
| 796 | |
| 797 | case NAND_CMD_PAGEPROG: |
| 798 | if (is_buf_blank(info->data_buff, |
| 799 | (mtd->writesize + mtd->oobsize))) { |
| 800 | exec_cmd = 0; |
| 801 | break; |
| 802 | } |
| 803 | |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 804 | /* Second command setting for large pages */ |
| 805 | if (mtd->writesize > PAGE_CHUNK_SIZE) { |
| 806 | /* |
| 807 | * Multiple page write uses the 'extended command' |
| 808 | * field. This can be used to issue a command dispatch |
| 809 | * or a naked-write depending on the current stage. |
| 810 | */ |
| 811 | info->ndcb0 |= NDCB0_CMD_TYPE(0x1) |
| 812 | | NDCB0_LEN_OVRD |
| 813 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type); |
| 814 | info->ndcb3 = info->chunk_size + |
| 815 | info->oob_size; |
| 816 | |
| 817 | /* |
| 818 | * This is the command dispatch that completes a chunked |
| 819 | * page program operation. |
| 820 | */ |
| 821 | if (info->data_size == 0) { |
| 822 | info->ndcb0 = NDCB0_CMD_TYPE(0x1) |
| 823 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type) |
| 824 | | command; |
| 825 | info->ndcb1 = 0; |
| 826 | info->ndcb2 = 0; |
| 827 | info->ndcb3 = 0; |
| 828 | } |
| 829 | } else { |
| 830 | info->ndcb0 |= NDCB0_CMD_TYPE(0x1) |
| 831 | | NDCB0_AUTO_RS |
| 832 | | NDCB0_ST_ROW_EN |
| 833 | | NDCB0_DBC |
| 834 | | (NAND_CMD_PAGEPROG << 8) |
| 835 | | NAND_CMD_SEQIN |
| 836 | | addr_cycle; |
| 837 | } |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 838 | break; |
| 839 | |
Ezequiel Garcia | ce0268f | 2013-05-14 08:15:25 -0300 | [diff] [blame] | 840 | case NAND_CMD_PARAM: |
Ezequiel Garcia | ce0268f | 2013-05-14 08:15:25 -0300 | [diff] [blame] | 841 | info->buf_count = 256; |
| 842 | info->ndcb0 |= NDCB0_CMD_TYPE(0) |
| 843 | | NDCB0_ADDR_CYC(1) |
Ezequiel Garcia | 41a6343 | 2013-08-12 14:14:51 -0300 | [diff] [blame] | 844 | | NDCB0_LEN_OVRD |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 845 | | command; |
Ezequiel Garcia | ce0268f | 2013-05-14 08:15:25 -0300 | [diff] [blame] | 846 | info->ndcb1 = (column & 0xFF); |
Ezequiel Garcia | 41a6343 | 2013-08-12 14:14:51 -0300 | [diff] [blame] | 847 | info->ndcb3 = 256; |
Ezequiel Garcia | ce0268f | 2013-05-14 08:15:25 -0300 | [diff] [blame] | 848 | info->data_size = 256; |
| 849 | break; |
| 850 | |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 851 | case NAND_CMD_READID: |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 852 | info->buf_count = host->read_id_bytes; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 853 | info->ndcb0 |= NDCB0_CMD_TYPE(3) |
| 854 | | NDCB0_ADDR_CYC(1) |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 855 | | command; |
Ezequiel Garcia | d14231f | 2013-05-14 08:15:24 -0300 | [diff] [blame] | 856 | info->ndcb1 = (column & 0xFF); |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 857 | |
| 858 | info->data_size = 8; |
| 859 | break; |
| 860 | case NAND_CMD_STATUS: |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 861 | info->buf_count = 1; |
| 862 | info->ndcb0 |= NDCB0_CMD_TYPE(4) |
| 863 | | NDCB0_ADDR_CYC(1) |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 864 | | command; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 865 | |
| 866 | info->data_size = 8; |
| 867 | break; |
| 868 | |
| 869 | case NAND_CMD_ERASE1: |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 870 | info->ndcb0 |= NDCB0_CMD_TYPE(2) |
| 871 | | NDCB0_AUTO_RS |
| 872 | | NDCB0_ADDR_CYC(3) |
| 873 | | NDCB0_DBC |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 874 | | (NAND_CMD_ERASE2 << 8) |
| 875 | | NAND_CMD_ERASE1; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 876 | info->ndcb1 = page_addr; |
| 877 | info->ndcb2 = 0; |
| 878 | |
| 879 | break; |
| 880 | case NAND_CMD_RESET: |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 881 | info->ndcb0 |= NDCB0_CMD_TYPE(5) |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 882 | | command; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 883 | |
| 884 | break; |
| 885 | |
| 886 | case NAND_CMD_ERASE2: |
| 887 | exec_cmd = 0; |
| 888 | break; |
| 889 | |
| 890 | default: |
| 891 | exec_cmd = 0; |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 892 | dev_err(&info->pdev->dev, "non-supported command %x\n", |
| 893 | command); |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 894 | break; |
| 895 | } |
| 896 | |
| 897 | return exec_cmd; |
| 898 | } |
| 899 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 900 | static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command, |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 901 | int column, int page_addr) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 902 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 903 | struct pxa3xx_nand_host *host = mtd->priv; |
| 904 | struct pxa3xx_nand_info *info = host->info_data; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 905 | int ret, exec_cmd; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 906 | |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 907 | /* |
| 908 | * if this is a x16 device ,then convert the input |
| 909 | * "byte" address into a "word" address appropriate |
| 910 | * for indexing a word-oriented device |
| 911 | */ |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 912 | if (info->reg_ndcr & NDCR_DWIDTH_M) |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 913 | column /= 2; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 914 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 915 | /* |
| 916 | * There may be different NAND chip hooked to |
| 917 | * different chip select, so check whether |
| 918 | * chip select has been changed, if yes, reset the timing |
| 919 | */ |
| 920 | if (info->cs != host->cs) { |
| 921 | info->cs = host->cs; |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 922 | nand_writel(info, NDTR0CS0, info->ndtr0cs0); |
| 923 | nand_writel(info, NDTR1CS0, info->ndtr1cs0); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 926 | prepare_start_command(info, command); |
| 927 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 928 | info->state = STATE_PREPARED; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 929 | exec_cmd = prepare_set_command(info, command, 0, column, page_addr); |
| 930 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 931 | if (exec_cmd) { |
| 932 | init_completion(&info->cmd_complete); |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 933 | init_completion(&info->dev_ready); |
| 934 | info->need_wait = 1; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 935 | pxa3xx_nand_start(info); |
| 936 | |
| 937 | ret = wait_for_completion_timeout(&info->cmd_complete, |
| 938 | CHIP_DELAY_TIMEOUT); |
| 939 | if (!ret) { |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 940 | dev_err(&info->pdev->dev, "Wait time out!!!\n"); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 941 | /* Stop State Machine for next command cycle */ |
| 942 | pxa3xx_nand_stop(info); |
| 943 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 944 | } |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 945 | info->state = STATE_IDLE; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 946 | } |
| 947 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 948 | static void armada370_nand_cmdfunc(struct mtd_info *mtd, |
| 949 | const unsigned command, |
| 950 | int column, int page_addr) |
| 951 | { |
| 952 | struct pxa3xx_nand_host *host = mtd->priv; |
| 953 | struct pxa3xx_nand_info *info = host->info_data; |
| 954 | int ret, exec_cmd, ext_cmd_type; |
| 955 | |
| 956 | /* |
| 957 | * if this is a x16 device then convert the input |
| 958 | * "byte" address into a "word" address appropriate |
| 959 | * for indexing a word-oriented device |
| 960 | */ |
| 961 | if (info->reg_ndcr & NDCR_DWIDTH_M) |
| 962 | column /= 2; |
| 963 | |
| 964 | /* |
| 965 | * There may be different NAND chip hooked to |
| 966 | * different chip select, so check whether |
| 967 | * chip select has been changed, if yes, reset the timing |
| 968 | */ |
| 969 | if (info->cs != host->cs) { |
| 970 | info->cs = host->cs; |
| 971 | nand_writel(info, NDTR0CS0, info->ndtr0cs0); |
| 972 | nand_writel(info, NDTR1CS0, info->ndtr1cs0); |
| 973 | } |
| 974 | |
| 975 | /* Select the extended command for the first command */ |
| 976 | switch (command) { |
| 977 | case NAND_CMD_READ0: |
| 978 | case NAND_CMD_READOOB: |
| 979 | ext_cmd_type = EXT_CMD_TYPE_MONO; |
| 980 | break; |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 981 | case NAND_CMD_SEQIN: |
| 982 | ext_cmd_type = EXT_CMD_TYPE_DISPATCH; |
| 983 | break; |
| 984 | case NAND_CMD_PAGEPROG: |
| 985 | ext_cmd_type = EXT_CMD_TYPE_NAKED_RW; |
| 986 | break; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 987 | default: |
| 988 | ext_cmd_type = 0; |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 989 | break; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | prepare_start_command(info, command); |
| 993 | |
| 994 | /* |
| 995 | * Prepare the "is ready" completion before starting a command |
| 996 | * transaction sequence. If the command is not executed the |
| 997 | * completion will be completed, see below. |
| 998 | * |
| 999 | * We can do that inside the loop because the command variable |
| 1000 | * is invariant and thus so is the exec_cmd. |
| 1001 | */ |
| 1002 | info->need_wait = 1; |
| 1003 | init_completion(&info->dev_ready); |
| 1004 | do { |
| 1005 | info->state = STATE_PREPARED; |
| 1006 | exec_cmd = prepare_set_command(info, command, ext_cmd_type, |
| 1007 | column, page_addr); |
| 1008 | if (!exec_cmd) { |
| 1009 | info->need_wait = 0; |
| 1010 | complete(&info->dev_ready); |
| 1011 | break; |
| 1012 | } |
| 1013 | |
| 1014 | init_completion(&info->cmd_complete); |
| 1015 | pxa3xx_nand_start(info); |
| 1016 | |
| 1017 | ret = wait_for_completion_timeout(&info->cmd_complete, |
| 1018 | CHIP_DELAY_TIMEOUT); |
| 1019 | if (!ret) { |
| 1020 | dev_err(&info->pdev->dev, "Wait time out!!!\n"); |
| 1021 | /* Stop State Machine for next command cycle */ |
| 1022 | pxa3xx_nand_stop(info); |
| 1023 | break; |
| 1024 | } |
| 1025 | |
| 1026 | /* Check if the sequence is complete */ |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 1027 | if (info->data_size == 0 && command != NAND_CMD_PAGEPROG) |
| 1028 | break; |
| 1029 | |
| 1030 | /* |
| 1031 | * After a splitted program command sequence has issued |
| 1032 | * the command dispatch, the command sequence is complete. |
| 1033 | */ |
| 1034 | if (info->data_size == 0 && |
| 1035 | command == NAND_CMD_PAGEPROG && |
| 1036 | ext_cmd_type == EXT_CMD_TYPE_DISPATCH) |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1037 | break; |
| 1038 | |
| 1039 | if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) { |
| 1040 | /* Last read: issue a 'last naked read' */ |
| 1041 | if (info->data_size == info->chunk_size) |
| 1042 | ext_cmd_type = EXT_CMD_TYPE_LAST_RW; |
| 1043 | else |
| 1044 | ext_cmd_type = EXT_CMD_TYPE_NAKED_RW; |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 1045 | |
| 1046 | /* |
| 1047 | * If a splitted program command has no more data to transfer, |
| 1048 | * the command dispatch must be issued to complete. |
| 1049 | */ |
| 1050 | } else if (command == NAND_CMD_PAGEPROG && |
| 1051 | info->data_size == 0) { |
| 1052 | ext_cmd_type = EXT_CMD_TYPE_DISPATCH; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1053 | } |
| 1054 | } while (1); |
| 1055 | |
| 1056 | info->state = STATE_IDLE; |
| 1057 | } |
| 1058 | |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 1059 | static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1060 | struct nand_chip *chip, const uint8_t *buf, int oob_required) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1061 | { |
| 1062 | chip->write_buf(mtd, buf, mtd->writesize); |
| 1063 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 1064 | |
| 1065 | return 0; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1069 | struct nand_chip *chip, uint8_t *buf, int oob_required, |
| 1070 | int page) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1071 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1072 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1073 | struct pxa3xx_nand_info *info = host->info_data; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1074 | |
| 1075 | chip->read_buf(mtd, buf, mtd->writesize); |
| 1076 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1077 | |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 1078 | if (info->retcode == ERR_CORERR && info->use_ecc) { |
| 1079 | mtd->ecc_stats.corrected += info->ecc_err_cnt; |
| 1080 | |
| 1081 | } else if (info->retcode == ERR_UNCORERR) { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1082 | /* |
| 1083 | * for blank page (all 0xff), HW will calculate its ECC as |
| 1084 | * 0, which is different from the ECC information within |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 1085 | * OOB, ignore such uncorrectable errors |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1086 | */ |
| 1087 | if (is_buf_blank(buf, mtd->writesize)) |
Daniel Mack | 543e32d | 2011-06-07 03:01:07 -0700 | [diff] [blame] | 1088 | info->retcode = ERR_NONE; |
| 1089 | else |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1090 | mtd->ecc_stats.failed++; |
| 1091 | } |
| 1092 | |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 1093 | return info->max_bitflips; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1094 | } |
| 1095 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1096 | static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd) |
| 1097 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1098 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1099 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1100 | char retval = 0xFF; |
| 1101 | |
| 1102 | if (info->buf_start < info->buf_count) |
| 1103 | /* Has just send a new command? */ |
| 1104 | retval = info->data_buff[info->buf_start++]; |
| 1105 | |
| 1106 | return retval; |
| 1107 | } |
| 1108 | |
| 1109 | static u16 pxa3xx_nand_read_word(struct mtd_info *mtd) |
| 1110 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1111 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1112 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1113 | u16 retval = 0xFFFF; |
| 1114 | |
| 1115 | if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) { |
| 1116 | retval = *((u16 *)(info->data_buff+info->buf_start)); |
| 1117 | info->buf_start += 2; |
| 1118 | } |
| 1119 | return retval; |
| 1120 | } |
| 1121 | |
| 1122 | static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 1123 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1124 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1125 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1126 | int real_len = min_t(size_t, len, info->buf_count - info->buf_start); |
| 1127 | |
| 1128 | memcpy(buf, info->data_buff + info->buf_start, real_len); |
| 1129 | info->buf_start += real_len; |
| 1130 | } |
| 1131 | |
| 1132 | static void pxa3xx_nand_write_buf(struct mtd_info *mtd, |
| 1133 | const uint8_t *buf, int len) |
| 1134 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1135 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1136 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1137 | int real_len = min_t(size_t, len, info->buf_count - info->buf_start); |
| 1138 | |
| 1139 | memcpy(info->data_buff + info->buf_start, buf, real_len); |
| 1140 | info->buf_start += real_len; |
| 1141 | } |
| 1142 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1143 | static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip) |
| 1144 | { |
| 1145 | return; |
| 1146 | } |
| 1147 | |
| 1148 | static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this) |
| 1149 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1150 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1151 | struct pxa3xx_nand_info *info = host->info_data; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1152 | int ret; |
| 1153 | |
| 1154 | if (info->need_wait) { |
| 1155 | ret = wait_for_completion_timeout(&info->dev_ready, |
| 1156 | CHIP_DELAY_TIMEOUT); |
| 1157 | info->need_wait = 0; |
| 1158 | if (!ret) { |
| 1159 | dev_err(&info->pdev->dev, "Ready time out!!!\n"); |
| 1160 | return NAND_STATUS_FAIL; |
| 1161 | } |
| 1162 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1163 | |
| 1164 | /* pxa3xx_nand_send_command has waited for command complete */ |
| 1165 | if (this->state == FL_WRITING || this->state == FL_ERASING) { |
| 1166 | if (info->retcode == ERR_NONE) |
| 1167 | return 0; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1168 | else |
| 1169 | return NAND_STATUS_FAIL; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1172 | return NAND_STATUS_READY; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1173 | } |
| 1174 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1175 | static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info, |
Enrico Scholz | c8c17c8 | 2008-08-29 12:59:51 +0200 | [diff] [blame] | 1176 | const struct pxa3xx_nand_flash *f) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1177 | { |
| 1178 | struct platform_device *pdev = info->pdev; |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1179 | struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1180 | struct pxa3xx_nand_host *host = info->host[info->cs]; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1181 | uint32_t ndcr = 0x0; /* enable all interrupts */ |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1182 | |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1183 | if (f->page_size != 2048 && f->page_size != 512) { |
| 1184 | dev_err(&pdev->dev, "Current only support 2048 and 512 size\n"); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1185 | return -EINVAL; |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1186 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1187 | |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1188 | if (f->flash_width != 16 && f->flash_width != 8) { |
| 1189 | dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n"); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1190 | return -EINVAL; |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1191 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1192 | |
| 1193 | /* calculate flash information */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1194 | host->read_id_bytes = (f->page_size == 2048) ? 4 : 2; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1195 | |
| 1196 | /* calculate addressing information */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1197 | host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1198 | |
| 1199 | if (f->num_blocks * f->page_per_block > 65536) |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1200 | host->row_addr_cycles = 3; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1201 | else |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1202 | host->row_addr_cycles = 2; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1203 | |
| 1204 | ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1205 | ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1206 | ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0; |
| 1207 | ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0; |
| 1208 | ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0; |
| 1209 | ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0; |
| 1210 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1211 | ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1212 | ndcr |= NDCR_SPARE_EN; /* enable spare by default */ |
| 1213 | |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 1214 | info->reg_ndcr = ndcr; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1215 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1216 | pxa3xx_nand_set_timing(host, f->timing); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1217 | return 0; |
| 1218 | } |
| 1219 | |
Mike Rapoport | f271049 | 2009-02-17 13:54:47 +0200 | [diff] [blame] | 1220 | static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info) |
| 1221 | { |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1222 | /* |
| 1223 | * We set 0 by hard coding here, for we don't support keep_config |
| 1224 | * when there is more than one chip attached to the controller |
| 1225 | */ |
| 1226 | struct pxa3xx_nand_host *host = info->host[0]; |
Mike Rapoport | f271049 | 2009-02-17 13:54:47 +0200 | [diff] [blame] | 1227 | uint32_t ndcr = nand_readl(info, NDCR); |
Mike Rapoport | f271049 | 2009-02-17 13:54:47 +0200 | [diff] [blame] | 1228 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1229 | if (ndcr & NDCR_PAGE_SZ) { |
Ezequiel Garcia | 2128b08 | 2013-11-07 12:17:16 -0300 | [diff] [blame] | 1230 | /* Controller's FIFO size */ |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1231 | info->chunk_size = 2048; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1232 | host->read_id_bytes = 4; |
| 1233 | } else { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1234 | info->chunk_size = 512; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1235 | host->read_id_bytes = 2; |
| 1236 | } |
| 1237 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1238 | /* Set an initial chunk size */ |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 1239 | info->reg_ndcr = ndcr & ~NDCR_INT_MASK; |
| 1240 | info->ndtr0cs0 = nand_readl(info, NDTR0CS0); |
| 1241 | info->ndtr1cs0 = nand_readl(info, NDTR1CS0); |
Mike Rapoport | f271049 | 2009-02-17 13:54:47 +0200 | [diff] [blame] | 1242 | return 0; |
| 1243 | } |
| 1244 | |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 1245 | #ifdef ARCH_HAS_DMA |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1246 | static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info) |
| 1247 | { |
| 1248 | struct platform_device *pdev = info->pdev; |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1249 | int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1250 | |
| 1251 | if (use_dma == 0) { |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1252 | info->data_buff = kmalloc(info->buf_size, GFP_KERNEL); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1253 | if (info->data_buff == NULL) |
| 1254 | return -ENOMEM; |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1258 | info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1259 | &info->data_buff_phys, GFP_KERNEL); |
| 1260 | if (info->data_buff == NULL) { |
| 1261 | dev_err(&pdev->dev, "failed to allocate dma buffer\n"); |
| 1262 | return -ENOMEM; |
| 1263 | } |
| 1264 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1265 | info->data_desc = (void *)info->data_buff + data_desc_offset; |
| 1266 | info->data_desc_addr = info->data_buff_phys + data_desc_offset; |
| 1267 | |
| 1268 | info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW, |
| 1269 | pxa3xx_nand_data_dma_irq, info); |
| 1270 | if (info->data_dma_ch < 0) { |
| 1271 | dev_err(&pdev->dev, "failed to request data dma\n"); |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1272 | dma_free_coherent(&pdev->dev, info->buf_size, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1273 | info->data_buff, info->data_buff_phys); |
| 1274 | return info->data_dma_ch; |
| 1275 | } |
| 1276 | |
Ezequiel Garcia | 95b2656 | 2013-10-04 15:30:37 -0300 | [diff] [blame] | 1277 | /* |
| 1278 | * Now that DMA buffers are allocated we turn on |
| 1279 | * DMA proper for I/O operations. |
| 1280 | */ |
| 1281 | info->use_dma = 1; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1282 | return 0; |
| 1283 | } |
| 1284 | |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1285 | static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info) |
| 1286 | { |
| 1287 | struct platform_device *pdev = info->pdev; |
Ezequiel Garcia | 15b540c | 2013-12-10 09:57:15 -0300 | [diff] [blame] | 1288 | if (info->use_dma) { |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1289 | pxa_free_dma(info->data_dma_ch); |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1290 | dma_free_coherent(&pdev->dev, info->buf_size, |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1291 | info->data_buff, info->data_buff_phys); |
| 1292 | } else { |
| 1293 | kfree(info->data_buff); |
| 1294 | } |
| 1295 | } |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 1296 | #else |
| 1297 | static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info) |
| 1298 | { |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1299 | info->data_buff = kmalloc(info->buf_size, GFP_KERNEL); |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 1300 | if (info->data_buff == NULL) |
| 1301 | return -ENOMEM; |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info) |
| 1306 | { |
| 1307 | kfree(info->data_buff); |
| 1308 | } |
| 1309 | #endif |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1310 | |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1311 | static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1312 | { |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1313 | struct mtd_info *mtd; |
Ezequiel Garcia | 2d79ab1 | 2013-11-07 12:17:15 -0300 | [diff] [blame] | 1314 | struct nand_chip *chip; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1315 | int ret; |
Ezequiel Garcia | 2d79ab1 | 2013-11-07 12:17:15 -0300 | [diff] [blame] | 1316 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1317 | mtd = info->host[info->cs]->mtd; |
Ezequiel Garcia | 2d79ab1 | 2013-11-07 12:17:15 -0300 | [diff] [blame] | 1318 | chip = mtd->priv; |
| 1319 | |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1320 | /* use the common timing to make a try */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1321 | ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]); |
| 1322 | if (ret) |
| 1323 | return ret; |
| 1324 | |
Ezequiel Garcia | 2d79ab1 | 2013-11-07 12:17:15 -0300 | [diff] [blame] | 1325 | chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0); |
Ezequiel Garcia | 56704d8 | 2013-11-14 18:25:27 -0300 | [diff] [blame] | 1326 | ret = chip->waitfunc(mtd, chip); |
| 1327 | if (ret & NAND_STATUS_FAIL) |
| 1328 | return -ENODEV; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1329 | |
Ezequiel Garcia | 56704d8 | 2013-11-14 18:25:27 -0300 | [diff] [blame] | 1330 | return 0; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1331 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1332 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1333 | static int pxa_ecc_init(struct pxa3xx_nand_info *info, |
| 1334 | struct nand_ecc_ctrl *ecc, |
| 1335 | int strength, int page_size) |
| 1336 | { |
| 1337 | /* |
| 1338 | * We don't use strength here as the PXA variant |
| 1339 | * is used with non-ONFI compliant devices. |
| 1340 | */ |
| 1341 | if (page_size == 2048) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1342 | info->chunk_size = 2048; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1343 | info->spare_size = 40; |
| 1344 | info->ecc_size = 24; |
| 1345 | ecc->mode = NAND_ECC_HW; |
| 1346 | ecc->size = 512; |
| 1347 | ecc->strength = 1; |
| 1348 | return 1; |
| 1349 | |
| 1350 | } else if (page_size == 512) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1351 | info->chunk_size = 512; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1352 | info->spare_size = 8; |
| 1353 | info->ecc_size = 8; |
| 1354 | ecc->mode = NAND_ECC_HW; |
| 1355 | ecc->size = 512; |
| 1356 | ecc->strength = 1; |
| 1357 | return 1; |
| 1358 | } |
| 1359 | return 0; |
| 1360 | } |
| 1361 | |
| 1362 | static int armada370_ecc_init(struct pxa3xx_nand_info *info, |
| 1363 | struct nand_ecc_ctrl *ecc, |
Brian Norris | 6033a94 | 2013-11-14 14:41:32 -0800 | [diff] [blame] | 1364 | int strength, int ecc_stepsize, int page_size) |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1365 | { |
Brian Norris | 6033a94 | 2013-11-14 14:41:32 -0800 | [diff] [blame] | 1366 | /* |
| 1367 | * Required ECC: 4-bit correction per 512 bytes |
| 1368 | * Select: 16-bit correction per 2048 bytes |
| 1369 | */ |
| 1370 | if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1371 | info->ecc_bch = 1; |
| 1372 | info->chunk_size = 2048; |
| 1373 | info->spare_size = 32; |
| 1374 | info->ecc_size = 32; |
| 1375 | ecc->mode = NAND_ECC_HW; |
| 1376 | ecc->size = info->chunk_size; |
| 1377 | ecc->layout = &ecc_layout_4KB_bch4bit; |
| 1378 | ecc->strength = 16; |
| 1379 | return 1; |
| 1380 | |
Brian Norris | 6033a94 | 2013-11-14 14:41:32 -0800 | [diff] [blame] | 1381 | /* |
| 1382 | * Required ECC: 8-bit correction per 512 bytes |
| 1383 | * Select: 16-bit correction per 1024 bytes |
| 1384 | */ |
| 1385 | } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1386 | info->ecc_bch = 1; |
| 1387 | info->chunk_size = 1024; |
| 1388 | info->spare_size = 0; |
| 1389 | info->ecc_size = 32; |
| 1390 | ecc->mode = NAND_ECC_HW; |
| 1391 | ecc->size = info->chunk_size; |
| 1392 | ecc->layout = &ecc_layout_4KB_bch8bit; |
| 1393 | ecc->strength = 16; |
| 1394 | return 1; |
| 1395 | } |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1396 | return 0; |
| 1397 | } |
| 1398 | |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1399 | static int pxa3xx_nand_scan(struct mtd_info *mtd) |
| 1400 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1401 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1402 | struct pxa3xx_nand_info *info = host->info_data; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1403 | struct platform_device *pdev = info->pdev; |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1404 | struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Lei Wen | 0fab028 | 2011-06-07 03:01:06 -0700 | [diff] [blame] | 1405 | struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1406 | const struct pxa3xx_nand_flash *f = NULL; |
| 1407 | struct nand_chip *chip = mtd->priv; |
| 1408 | uint32_t id = -1; |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1409 | uint64_t chipsize; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1410 | int i, ret, num; |
| 1411 | |
| 1412 | if (pdata->keep_config && !pxa3xx_nand_detect_config(info)) |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1413 | goto KEEP_CONFIG; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1414 | |
| 1415 | ret = pxa3xx_nand_sensing(info); |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1416 | if (ret) { |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1417 | dev_info(&info->pdev->dev, "There is no chip on cs %d!\n", |
| 1418 | info->cs); |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1419 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1420 | return ret; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0); |
| 1424 | id = *((uint16_t *)(info->data_buff)); |
| 1425 | if (id != 0) |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1426 | dev_info(&info->pdev->dev, "Detect a flash id %x\n", id); |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1427 | else { |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1428 | dev_warn(&info->pdev->dev, |
| 1429 | "Read out ID 0, potential timing set wrong!!\n"); |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1430 | |
| 1431 | return -EINVAL; |
| 1432 | } |
| 1433 | |
| 1434 | num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1; |
| 1435 | for (i = 0; i < num; i++) { |
| 1436 | if (i < pdata->num_flash) |
| 1437 | f = pdata->flash + i; |
| 1438 | else |
| 1439 | f = &builtin_flash_types[i - pdata->num_flash + 1]; |
| 1440 | |
| 1441 | /* find the chip in default list */ |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1442 | if (f->chip_id == id) |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1443 | break; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1444 | } |
| 1445 | |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1446 | if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) { |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1447 | dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n"); |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1448 | |
| 1449 | return -EINVAL; |
| 1450 | } |
| 1451 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1452 | ret = pxa3xx_nand_config_flash(info, f); |
| 1453 | if (ret) { |
| 1454 | dev_err(&info->pdev->dev, "ERROR! Configure failed\n"); |
| 1455 | return ret; |
| 1456 | } |
| 1457 | |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1458 | pxa3xx_flash_ids[0].name = f->name; |
Artem Bityutskiy | 68aa352de | 2013-03-04 16:05:00 +0200 | [diff] [blame] | 1459 | pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff; |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1460 | pxa3xx_flash_ids[0].pagesize = f->page_size; |
| 1461 | chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size; |
| 1462 | pxa3xx_flash_ids[0].chipsize = chipsize >> 20; |
| 1463 | pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block; |
| 1464 | if (f->flash_width == 16) |
| 1465 | pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16; |
Lei Wen | 0fab028 | 2011-06-07 03:01:06 -0700 | [diff] [blame] | 1466 | pxa3xx_flash_ids[1].name = NULL; |
| 1467 | def = pxa3xx_flash_ids; |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1468 | KEEP_CONFIG: |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 1469 | if (info->reg_ndcr & NDCR_DWIDTH_M) |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1470 | chip->options |= NAND_BUSWIDTH_16; |
| 1471 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1472 | /* Device detection must be done with ECC disabled */ |
| 1473 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) |
| 1474 | nand_writel(info, NDECCCTRL, 0x0); |
| 1475 | |
Lei Wen | 0fab028 | 2011-06-07 03:01:06 -0700 | [diff] [blame] | 1476 | if (nand_scan_ident(mtd, 1, def)) |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1477 | return -ENODEV; |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 1478 | |
| 1479 | if (pdata->flash_bbt) { |
| 1480 | /* |
| 1481 | * We'll use a bad block table stored in-flash and don't |
| 1482 | * allow writing the bad block marker to the flash. |
| 1483 | */ |
| 1484 | chip->bbt_options |= NAND_BBT_USE_FLASH | |
| 1485 | NAND_BBT_NO_OOB_BBM; |
| 1486 | chip->bbt_td = &bbt_main_descr; |
| 1487 | chip->bbt_md = &bbt_mirror_descr; |
| 1488 | } |
| 1489 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1490 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) |
| 1491 | ret = armada370_ecc_init(info, &chip->ecc, |
| 1492 | chip->ecc_strength_ds, |
Brian Norris | 6033a94 | 2013-11-14 14:41:32 -0800 | [diff] [blame] | 1493 | chip->ecc_step_ds, |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1494 | mtd->writesize); |
| 1495 | else |
| 1496 | ret = pxa_ecc_init(info, &chip->ecc, |
| 1497 | chip->ecc_strength_ds, |
| 1498 | mtd->writesize); |
| 1499 | if (!ret) { |
| 1500 | dev_err(&info->pdev->dev, |
| 1501 | "ECC strength %d at page size %d is not supported\n", |
| 1502 | chip->ecc_strength_ds, mtd->writesize); |
| 1503 | return -ENODEV; |
| 1504 | } |
| 1505 | |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1506 | /* calculate addressing information */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1507 | if (mtd->writesize >= 2048) |
| 1508 | host->col_addr_cycles = 2; |
| 1509 | else |
| 1510 | host->col_addr_cycles = 1; |
| 1511 | |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1512 | /* release the initial buffer */ |
| 1513 | kfree(info->data_buff); |
| 1514 | |
| 1515 | /* allocate the real data + oob buffer */ |
| 1516 | info->buf_size = mtd->writesize + mtd->oobsize; |
| 1517 | ret = pxa3xx_nand_init_buff(info); |
| 1518 | if (ret) |
| 1519 | return ret; |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1520 | info->oob_buff = info->data_buff + mtd->writesize; |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1521 | |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1522 | if ((mtd->size >> chip->page_shift) > 65536) |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1523 | host->row_addr_cycles = 3; |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1524 | else |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1525 | host->row_addr_cycles = 2; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1526 | return nand_scan_tail(mtd); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1527 | } |
| 1528 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1529 | static int alloc_nand_resource(struct platform_device *pdev) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1530 | { |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1531 | struct pxa3xx_nand_platform_data *pdata; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1532 | struct pxa3xx_nand_info *info; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1533 | struct pxa3xx_nand_host *host; |
Haojian Zhuang | 6e308f8 | 2012-08-20 13:40:31 +0800 | [diff] [blame] | 1534 | struct nand_chip *chip = NULL; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1535 | struct mtd_info *mtd; |
| 1536 | struct resource *r; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1537 | int ret, irq, cs; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1538 | |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1539 | pdata = dev_get_platdata(&pdev->dev); |
Ezequiel Garcia | 4c073cd | 2013-04-17 13:38:09 -0300 | [diff] [blame] | 1540 | info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) + |
| 1541 | sizeof(*host)) * pdata->num_cs, GFP_KERNEL); |
| 1542 | if (!info) |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1543 | return -ENOMEM; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1544 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1545 | info->pdev = pdev; |
Ezequiel Garcia | c7e9c7e | 2013-11-07 12:17:14 -0300 | [diff] [blame] | 1546 | info->variant = pxa3xx_nand_get_variant(pdev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1547 | for (cs = 0; cs < pdata->num_cs; cs++) { |
| 1548 | mtd = (struct mtd_info *)((unsigned int)&info[1] + |
| 1549 | (sizeof(*mtd) + sizeof(*host)) * cs); |
| 1550 | chip = (struct nand_chip *)(&mtd[1]); |
| 1551 | host = (struct pxa3xx_nand_host *)chip; |
| 1552 | info->host[cs] = host; |
| 1553 | host->mtd = mtd; |
| 1554 | host->cs = cs; |
| 1555 | host->info_data = info; |
| 1556 | mtd->priv = host; |
| 1557 | mtd->owner = THIS_MODULE; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1558 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1559 | chip->ecc.read_page = pxa3xx_nand_read_page_hwecc; |
| 1560 | chip->ecc.write_page = pxa3xx_nand_write_page_hwecc; |
| 1561 | chip->controller = &info->controller; |
| 1562 | chip->waitfunc = pxa3xx_nand_waitfunc; |
| 1563 | chip->select_chip = pxa3xx_nand_select_chip; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1564 | chip->read_word = pxa3xx_nand_read_word; |
| 1565 | chip->read_byte = pxa3xx_nand_read_byte; |
| 1566 | chip->read_buf = pxa3xx_nand_read_buf; |
| 1567 | chip->write_buf = pxa3xx_nand_write_buf; |
Ezequiel Garcia | 664c7f5 | 2013-11-07 12:17:12 -0300 | [diff] [blame] | 1568 | chip->options |= NAND_NO_SUBPAGE_WRITE; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1569 | |
| 1570 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) |
| 1571 | chip->cmdfunc = armada370_nand_cmdfunc; |
| 1572 | else |
| 1573 | chip->cmdfunc = pxa3xx_nand_cmdfunc; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1574 | } |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1575 | |
| 1576 | spin_lock_init(&chip->controller->lock); |
| 1577 | init_waitqueue_head(&chip->controller->wq); |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1578 | info->clk = devm_clk_get(&pdev->dev, NULL); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1579 | if (IS_ERR(info->clk)) { |
| 1580 | dev_err(&pdev->dev, "failed to get nand clock\n"); |
Ezequiel Garcia | 4c073cd | 2013-04-17 13:38:09 -0300 | [diff] [blame] | 1581 | return PTR_ERR(info->clk); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1582 | } |
Ezequiel Garcia | 1f8eaff | 2013-04-17 13:38:13 -0300 | [diff] [blame] | 1583 | ret = clk_prepare_enable(info->clk); |
| 1584 | if (ret < 0) |
| 1585 | return ret; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1586 | |
Ezequiel Garcia | 6b45c1e | 2013-08-12 14:14:58 -0300 | [diff] [blame] | 1587 | if (use_dma) { |
| 1588 | /* |
| 1589 | * This is a dirty hack to make this driver work from |
| 1590 | * devicetree bindings. It can be removed once we have |
| 1591 | * a prober DMA controller framework for DT. |
| 1592 | */ |
| 1593 | if (pdev->dev.of_node && |
| 1594 | of_machine_is_compatible("marvell,pxa3xx")) { |
| 1595 | info->drcmr_dat = 97; |
| 1596 | info->drcmr_cmd = 99; |
| 1597 | } else { |
| 1598 | r = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 1599 | if (r == NULL) { |
| 1600 | dev_err(&pdev->dev, |
| 1601 | "no resource defined for data DMA\n"); |
| 1602 | ret = -ENXIO; |
| 1603 | goto fail_disable_clk; |
| 1604 | } |
| 1605 | info->drcmr_dat = r->start; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1606 | |
Ezequiel Garcia | 6b45c1e | 2013-08-12 14:14:58 -0300 | [diff] [blame] | 1607 | r = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 1608 | if (r == NULL) { |
| 1609 | dev_err(&pdev->dev, |
| 1610 | "no resource defined for cmd DMA\n"); |
| 1611 | ret = -ENXIO; |
| 1612 | goto fail_disable_clk; |
| 1613 | } |
| 1614 | info->drcmr_cmd = r->start; |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1615 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1616 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1617 | |
| 1618 | irq = platform_get_irq(pdev, 0); |
| 1619 | if (irq < 0) { |
| 1620 | dev_err(&pdev->dev, "no IRQ resource defined\n"); |
| 1621 | ret = -ENXIO; |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1622 | goto fail_disable_clk; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Ezequiel Garcia | 0ddd846 | 2013-04-17 13:38:10 -0300 | [diff] [blame] | 1626 | info->mmio_base = devm_ioremap_resource(&pdev->dev, r); |
| 1627 | if (IS_ERR(info->mmio_base)) { |
| 1628 | ret = PTR_ERR(info->mmio_base); |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1629 | goto fail_disable_clk; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1630 | } |
Haojian Zhuang | 8638fac | 2009-09-10 14:11:44 +0800 | [diff] [blame] | 1631 | info->mmio_phys = r->start; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1632 | |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1633 | /* Allocate a buffer to allow flash detection */ |
| 1634 | info->buf_size = INIT_BUFFER_SIZE; |
| 1635 | info->data_buff = kmalloc(info->buf_size, GFP_KERNEL); |
| 1636 | if (info->data_buff == NULL) { |
| 1637 | ret = -ENOMEM; |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1638 | goto fail_disable_clk; |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1639 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1640 | |
Haojian Zhuang | 346e125 | 2009-09-10 14:27:23 +0800 | [diff] [blame] | 1641 | /* initialize all interrupts to be disabled */ |
| 1642 | disable_int(info, NDSR_MASK); |
| 1643 | |
Michael Opdenacker | b1eb234 | 2013-10-13 08:21:32 +0200 | [diff] [blame] | 1644 | ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1645 | if (ret < 0) { |
| 1646 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
| 1647 | goto fail_free_buf; |
| 1648 | } |
| 1649 | |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1650 | platform_set_drvdata(pdev, info); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1651 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1652 | return 0; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1653 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1654 | fail_free_buf: |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1655 | free_irq(irq, info); |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1656 | kfree(info->data_buff); |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1657 | fail_disable_clk: |
Ezequiel Garcia | fb32061 | 2013-04-17 13:38:12 -0300 | [diff] [blame] | 1658 | clk_disable_unprepare(info->clk); |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1659 | return ret; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | static int pxa3xx_nand_remove(struct platform_device *pdev) |
| 1663 | { |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1664 | struct pxa3xx_nand_info *info = platform_get_drvdata(pdev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1665 | struct pxa3xx_nand_platform_data *pdata; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1666 | int irq, cs; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1667 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1668 | if (!info) |
| 1669 | return 0; |
| 1670 | |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1671 | pdata = dev_get_platdata(&pdev->dev); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1672 | |
Haojian Zhuang | dbf5986 | 2009-09-10 14:22:55 +0800 | [diff] [blame] | 1673 | irq = platform_get_irq(pdev, 0); |
| 1674 | if (irq >= 0) |
| 1675 | free_irq(irq, info); |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1676 | pxa3xx_nand_free_buff(info); |
Mike Rapoport | 82a72d1 | 2009-02-17 13:54:46 +0200 | [diff] [blame] | 1677 | |
Ezequiel Garcia | fb32061 | 2013-04-17 13:38:12 -0300 | [diff] [blame] | 1678 | clk_disable_unprepare(info->clk); |
Mike Rapoport | 82a72d1 | 2009-02-17 13:54:46 +0200 | [diff] [blame] | 1679 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1680 | for (cs = 0; cs < pdata->num_cs; cs++) |
| 1681 | nand_release(info->host[cs]->mtd); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1682 | return 0; |
| 1683 | } |
| 1684 | |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1685 | static int pxa3xx_nand_probe_dt(struct platform_device *pdev) |
| 1686 | { |
| 1687 | struct pxa3xx_nand_platform_data *pdata; |
| 1688 | struct device_node *np = pdev->dev.of_node; |
| 1689 | const struct of_device_id *of_id = |
| 1690 | of_match_device(pxa3xx_nand_dt_ids, &pdev->dev); |
| 1691 | |
| 1692 | if (!of_id) |
| 1693 | return 0; |
| 1694 | |
| 1695 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 1696 | if (!pdata) |
| 1697 | return -ENOMEM; |
| 1698 | |
| 1699 | if (of_get_property(np, "marvell,nand-enable-arbiter", NULL)) |
| 1700 | pdata->enable_arbiter = 1; |
| 1701 | if (of_get_property(np, "marvell,nand-keep-config", NULL)) |
| 1702 | pdata->keep_config = 1; |
| 1703 | of_property_read_u32(np, "num-cs", &pdata->num_cs); |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 1704 | pdata->flash_bbt = of_get_nand_on_flash_bbt(np); |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1705 | |
| 1706 | pdev->dev.platform_data = pdata; |
| 1707 | |
| 1708 | return 0; |
| 1709 | } |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1710 | |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1711 | static int pxa3xx_nand_probe(struct platform_device *pdev) |
| 1712 | { |
| 1713 | struct pxa3xx_nand_platform_data *pdata; |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1714 | struct mtd_part_parser_data ppdata = {}; |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1715 | struct pxa3xx_nand_info *info; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1716 | int ret, cs, probe_success; |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1717 | |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 1718 | #ifndef ARCH_HAS_DMA |
| 1719 | if (use_dma) { |
| 1720 | use_dma = 0; |
| 1721 | dev_warn(&pdev->dev, |
| 1722 | "This platform can't do DMA on this device\n"); |
| 1723 | } |
| 1724 | #endif |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1725 | ret = pxa3xx_nand_probe_dt(pdev); |
| 1726 | if (ret) |
| 1727 | return ret; |
| 1728 | |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1729 | pdata = dev_get_platdata(&pdev->dev); |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1730 | if (!pdata) { |
| 1731 | dev_err(&pdev->dev, "no platform data defined\n"); |
| 1732 | return -ENODEV; |
| 1733 | } |
| 1734 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1735 | ret = alloc_nand_resource(pdev); |
| 1736 | if (ret) { |
| 1737 | dev_err(&pdev->dev, "alloc nand resource failed\n"); |
| 1738 | return ret; |
| 1739 | } |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1740 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1741 | info = platform_get_drvdata(pdev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1742 | probe_success = 0; |
| 1743 | for (cs = 0; cs < pdata->num_cs; cs++) { |
Ezequiel Garcia | b7655bc | 2013-08-12 14:14:52 -0300 | [diff] [blame] | 1744 | struct mtd_info *mtd = info->host[cs]->mtd; |
Ezequiel Garcia | f455578 | 2013-08-12 14:14:53 -0300 | [diff] [blame] | 1745 | |
Ezequiel Garcia | 18a84e9 | 2013-10-19 18:19:25 -0300 | [diff] [blame] | 1746 | /* |
| 1747 | * The mtd name matches the one used in 'mtdparts' kernel |
| 1748 | * parameter. This name cannot be changed or otherwise |
| 1749 | * user's mtd partitions configuration would get broken. |
| 1750 | */ |
| 1751 | mtd->name = "pxa3xx_nand-0"; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1752 | info->cs = cs; |
Ezequiel Garcia | b7655bc | 2013-08-12 14:14:52 -0300 | [diff] [blame] | 1753 | ret = pxa3xx_nand_scan(mtd); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1754 | if (ret) { |
| 1755 | dev_warn(&pdev->dev, "failed to scan nand at cs %d\n", |
| 1756 | cs); |
| 1757 | continue; |
| 1758 | } |
| 1759 | |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1760 | ppdata.of_node = pdev->dev.of_node; |
Ezequiel Garcia | b7655bc | 2013-08-12 14:14:52 -0300 | [diff] [blame] | 1761 | ret = mtd_device_parse_register(mtd, NULL, |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1762 | &ppdata, pdata->parts[cs], |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 1763 | pdata->nr_parts[cs]); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1764 | if (!ret) |
| 1765 | probe_success = 1; |
| 1766 | } |
| 1767 | |
| 1768 | if (!probe_success) { |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1769 | pxa3xx_nand_remove(pdev); |
| 1770 | return -ENODEV; |
| 1771 | } |
| 1772 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1773 | return 0; |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1774 | } |
| 1775 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1776 | #ifdef CONFIG_PM |
| 1777 | static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state) |
| 1778 | { |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1779 | struct pxa3xx_nand_info *info = platform_get_drvdata(pdev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1780 | struct pxa3xx_nand_platform_data *pdata; |
| 1781 | struct mtd_info *mtd; |
| 1782 | int cs; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1783 | |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1784 | pdata = dev_get_platdata(&pdev->dev); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1785 | if (info->state) { |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1786 | dev_err(&pdev->dev, "driver busy, state = %d\n", info->state); |
| 1787 | return -EAGAIN; |
| 1788 | } |
| 1789 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1790 | for (cs = 0; cs < pdata->num_cs; cs++) { |
| 1791 | mtd = info->host[cs]->mtd; |
Artem Bityutskiy | 3fe4bae | 2011-12-23 19:25:16 +0200 | [diff] [blame] | 1792 | mtd_suspend(mtd); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1795 | return 0; |
| 1796 | } |
| 1797 | |
| 1798 | static int pxa3xx_nand_resume(struct platform_device *pdev) |
| 1799 | { |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1800 | struct pxa3xx_nand_info *info = platform_get_drvdata(pdev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1801 | struct pxa3xx_nand_platform_data *pdata; |
| 1802 | struct mtd_info *mtd; |
| 1803 | int cs; |
Lei Wen | 051fc41 | 2011-07-14 20:44:30 -0700 | [diff] [blame] | 1804 | |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1805 | pdata = dev_get_platdata(&pdev->dev); |
Lei Wen | 051fc41 | 2011-07-14 20:44:30 -0700 | [diff] [blame] | 1806 | /* We don't want to handle interrupt without calling mtd routine */ |
| 1807 | disable_int(info, NDCR_INT_MASK); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1808 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1809 | /* |
| 1810 | * Directly set the chip select to a invalid value, |
| 1811 | * then the driver would reset the timing according |
| 1812 | * to current chip select at the beginning of cmdfunc |
| 1813 | */ |
| 1814 | info->cs = 0xff; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1815 | |
Lei Wen | 051fc41 | 2011-07-14 20:44:30 -0700 | [diff] [blame] | 1816 | /* |
| 1817 | * As the spec says, the NDSR would be updated to 0x1800 when |
| 1818 | * doing the nand_clk disable/enable. |
| 1819 | * To prevent it damaging state machine of the driver, clear |
| 1820 | * all status before resume |
| 1821 | */ |
| 1822 | nand_writel(info, NDSR, NDSR_MASK); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1823 | for (cs = 0; cs < pdata->num_cs; cs++) { |
| 1824 | mtd = info->host[cs]->mtd; |
Artem Bityutskiy | ead995f | 2011-12-23 19:31:25 +0200 | [diff] [blame] | 1825 | mtd_resume(mtd); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
Lei Wen | 18c81b1 | 2010-08-17 17:25:57 +0800 | [diff] [blame] | 1828 | return 0; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1829 | } |
| 1830 | #else |
| 1831 | #define pxa3xx_nand_suspend NULL |
| 1832 | #define pxa3xx_nand_resume NULL |
| 1833 | #endif |
| 1834 | |
| 1835 | static struct platform_driver pxa3xx_nand_driver = { |
| 1836 | .driver = { |
| 1837 | .name = "pxa3xx-nand", |
Sachin Kamat | 5576bc7 | 2013-09-30 15:10:24 +0530 | [diff] [blame] | 1838 | .of_match_table = pxa3xx_nand_dt_ids, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1839 | }, |
| 1840 | .probe = pxa3xx_nand_probe, |
| 1841 | .remove = pxa3xx_nand_remove, |
| 1842 | .suspend = pxa3xx_nand_suspend, |
| 1843 | .resume = pxa3xx_nand_resume, |
| 1844 | }; |
| 1845 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 1846 | module_platform_driver(pxa3xx_nand_driver); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1847 | |
| 1848 | MODULE_LICENSE("GPL"); |
| 1849 | MODULE_DESCRIPTION("PXA3xx NAND controller driver"); |