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