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