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