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