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