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