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