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> |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 18 | #include <linux/dmaengine.h> |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 19 | #include <linux/dma-mapping.h> |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 20 | #include <linux/dma/pxa-dma.h> |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 21 | #include <linux/delay.h> |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/mtd/mtd.h> |
| 24 | #include <linux/mtd/nand.h> |
| 25 | #include <linux/mtd/partitions.h> |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 26 | #include <linux/io.h> |
Maxime Ripard | afca11e | 2015-04-07 15:32:45 +0200 | [diff] [blame] | 27 | #include <linux/iopoll.h> |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 28 | #include <linux/irq.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 30 | #include <linux/of.h> |
| 31 | #include <linux/of_device.h> |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 32 | #include <linux/of_mtd.h> |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 33 | |
Rob Herring | ce914e6 | 2015-04-30 15:17:47 -0500 | [diff] [blame] | 34 | #if defined(CONFIG_ARM) && (defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)) |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 35 | #define ARCH_HAS_DMA |
| 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 | |
Nicholas Mc Guire | e5860c1 | 2015-02-01 11:55:37 -0500 | [diff] [blame] | 40 | #define CHIP_DELAY_TIMEOUT msecs_to_jiffies(200) |
| 41 | #define NAND_STOP_DELAY msecs_to_jiffies(40) |
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: |
Ezequiel Garcia | c163409 | 2015-08-03 11:31:26 -0300 | [diff] [blame] | 46 | * STATUS, READID and PARAM. |
| 47 | * ONFI param page is 256 bytes, and there are three redundant copies |
| 48 | * to be read. JEDEC param page is 512 bytes, and there are also three |
| 49 | * redundant copies to be read. |
| 50 | * Hence this buffer should be at least 512 x 3. Let's pick 2048. |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 51 | */ |
Ezequiel Garcia | c163409 | 2015-08-03 11:31:26 -0300 | [diff] [blame] | 52 | #define INIT_BUFFER_SIZE 2048 |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 53 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 54 | /* registers and bit definitions */ |
| 55 | #define NDCR (0x00) /* Control register */ |
| 56 | #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */ |
| 57 | #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */ |
| 58 | #define NDSR (0x14) /* Status Register */ |
| 59 | #define NDPCR (0x18) /* Page Count Register */ |
| 60 | #define NDBDR0 (0x1C) /* Bad Block Register 0 */ |
| 61 | #define NDBDR1 (0x20) /* Bad Block Register 1 */ |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 62 | #define NDECCCTRL (0x28) /* ECC control */ |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 63 | #define NDDB (0x40) /* Data Buffer */ |
| 64 | #define NDCB0 (0x48) /* Command Buffer0 */ |
| 65 | #define NDCB1 (0x4C) /* Command Buffer1 */ |
| 66 | #define NDCB2 (0x50) /* Command Buffer2 */ |
| 67 | |
| 68 | #define NDCR_SPARE_EN (0x1 << 31) |
| 69 | #define NDCR_ECC_EN (0x1 << 30) |
| 70 | #define NDCR_DMA_EN (0x1 << 29) |
| 71 | #define NDCR_ND_RUN (0x1 << 28) |
| 72 | #define NDCR_DWIDTH_C (0x1 << 27) |
| 73 | #define NDCR_DWIDTH_M (0x1 << 26) |
| 74 | #define NDCR_PAGE_SZ (0x1 << 24) |
| 75 | #define NDCR_NCSX (0x1 << 23) |
| 76 | #define NDCR_ND_MODE (0x3 << 21) |
| 77 | #define NDCR_NAND_MODE (0x0) |
| 78 | #define NDCR_CLR_PG_CNT (0x1 << 20) |
Robert Jarzmik | e971aff | 2015-09-28 22:56:51 +0200 | [diff] [blame] | 79 | #define NFCV1_NDCR_ARB_CNTL (0x1 << 19) |
| 80 | #define NFCV2_NDCR_STOP_ON_UNCOR (0x1 << 19) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 81 | #define NDCR_RD_ID_CNT_MASK (0x7 << 16) |
| 82 | #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK) |
| 83 | |
| 84 | #define NDCR_RA_START (0x1 << 15) |
| 85 | #define NDCR_PG_PER_BLK (0x1 << 14) |
| 86 | #define NDCR_ND_ARB_EN (0x1 << 12) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 87 | #define NDCR_INT_MASK (0xFFF) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 88 | |
| 89 | #define NDSR_MASK (0xfff) |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 90 | #define NDSR_ERR_CNT_OFF (16) |
| 91 | #define NDSR_ERR_CNT_MASK (0x1f) |
| 92 | #define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 93 | #define NDSR_RDY (0x1 << 12) |
| 94 | #define NDSR_FLASH_RDY (0x1 << 11) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 95 | #define NDSR_CS0_PAGED (0x1 << 10) |
| 96 | #define NDSR_CS1_PAGED (0x1 << 9) |
| 97 | #define NDSR_CS0_CMDD (0x1 << 8) |
| 98 | #define NDSR_CS1_CMDD (0x1 << 7) |
| 99 | #define NDSR_CS0_BBD (0x1 << 6) |
| 100 | #define NDSR_CS1_BBD (0x1 << 5) |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 101 | #define NDSR_UNCORERR (0x1 << 4) |
| 102 | #define NDSR_CORERR (0x1 << 3) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 103 | #define NDSR_WRDREQ (0x1 << 2) |
| 104 | #define NDSR_RDDREQ (0x1 << 1) |
| 105 | #define NDSR_WRCMDREQ (0x1) |
| 106 | |
Ezequiel Garcia | 41a6343 | 2013-08-12 14:14:51 -0300 | [diff] [blame] | 107 | #define NDCB0_LEN_OVRD (0x1 << 28) |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 108 | #define NDCB0_ST_ROW_EN (0x1 << 26) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 109 | #define NDCB0_AUTO_RS (0x1 << 25) |
| 110 | #define NDCB0_CSEL (0x1 << 24) |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 111 | #define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29) |
| 112 | #define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 113 | #define NDCB0_CMD_TYPE_MASK (0x7 << 21) |
| 114 | #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK) |
| 115 | #define NDCB0_NC (0x1 << 20) |
| 116 | #define NDCB0_DBC (0x1 << 19) |
| 117 | #define NDCB0_ADDR_CYC_MASK (0x7 << 16) |
| 118 | #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK) |
| 119 | #define NDCB0_CMD2_MASK (0xff << 8) |
| 120 | #define NDCB0_CMD1_MASK (0xff) |
| 121 | #define NDCB0_ADDR_CYC_SHIFT (16) |
| 122 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 123 | #define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */ |
| 124 | #define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */ |
| 125 | #define EXT_CMD_TYPE_READ 4 /* Read */ |
| 126 | #define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */ |
| 127 | #define EXT_CMD_TYPE_FINAL 3 /* Final command */ |
| 128 | #define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */ |
| 129 | #define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */ |
| 130 | |
Ezequiel García | b226eca | 2015-08-19 19:40:09 -0300 | [diff] [blame] | 131 | /* |
| 132 | * This should be large enough to read 'ONFI' and 'JEDEC'. |
| 133 | * Let's use 7 bytes, which is the maximum ID count supported |
| 134 | * by the controller (see NDCR_RD_ID_CNT_MASK). |
| 135 | */ |
| 136 | #define READ_ID_BYTES 7 |
| 137 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 138 | /* macros for registers read/write */ |
| 139 | #define nand_writel(info, off, val) \ |
Thomas Petazzoni | b7e46062 | 2014-05-22 14:56:52 +0200 | [diff] [blame] | 140 | writel_relaxed((val), (info)->mmio_base + (off)) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 141 | |
| 142 | #define nand_readl(info, off) \ |
Thomas Petazzoni | b7e46062 | 2014-05-22 14:56:52 +0200 | [diff] [blame] | 143 | readl_relaxed((info)->mmio_base + (off)) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 144 | |
| 145 | /* error code and state */ |
| 146 | enum { |
| 147 | ERR_NONE = 0, |
| 148 | ERR_DMABUSERR = -1, |
| 149 | ERR_SENDCMD = -2, |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 150 | ERR_UNCORERR = -3, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 151 | ERR_BBERR = -4, |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 152 | ERR_CORERR = -5, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | enum { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 156 | STATE_IDLE = 0, |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 157 | STATE_PREPARED, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 158 | STATE_CMD_HANDLE, |
| 159 | STATE_DMA_READING, |
| 160 | STATE_DMA_WRITING, |
| 161 | STATE_DMA_DONE, |
| 162 | STATE_PIO_READING, |
| 163 | STATE_PIO_WRITING, |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 164 | STATE_CMD_DONE, |
| 165 | STATE_READY, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 166 | }; |
| 167 | |
Ezequiel Garcia | c0f3b86 | 2013-08-10 16:34:52 -0300 | [diff] [blame] | 168 | enum pxa3xx_nand_variant { |
| 169 | PXA3XX_NAND_VARIANT_PXA, |
| 170 | PXA3XX_NAND_VARIANT_ARMADA370, |
| 171 | }; |
| 172 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 173 | struct pxa3xx_nand_host { |
| 174 | struct nand_chip chip; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 175 | struct mtd_info *mtd; |
| 176 | void *info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 177 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 178 | /* page size of attached chip */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 179 | int use_ecc; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 180 | int cs; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 181 | |
| 182 | /* calculated from pxa3xx_nand_flash data */ |
| 183 | unsigned int col_addr_cycles; |
| 184 | unsigned int row_addr_cycles; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | struct pxa3xx_nand_info { |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 188 | struct nand_hw_control controller; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 189 | struct platform_device *pdev; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 190 | |
| 191 | struct clk *clk; |
| 192 | void __iomem *mmio_base; |
Haojian Zhuang | 8638fac | 2009-09-10 14:11:44 +0800 | [diff] [blame] | 193 | unsigned long mmio_phys; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 194 | struct completion cmd_complete, dev_ready; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 195 | |
| 196 | unsigned int buf_start; |
| 197 | unsigned int buf_count; |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 198 | unsigned int buf_size; |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 199 | unsigned int data_buff_pos; |
| 200 | unsigned int oob_buff_pos; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 201 | |
| 202 | /* DMA information */ |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 203 | struct scatterlist sg; |
| 204 | enum dma_data_direction dma_dir; |
| 205 | struct dma_chan *dma_chan; |
| 206 | dma_cookie_t dma_cookie; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 207 | int drcmr_dat; |
| 208 | int drcmr_cmd; |
| 209 | |
| 210 | unsigned char *data_buff; |
Lei Wen | 18c81b1 | 2010-08-17 17:25:57 +0800 | [diff] [blame] | 211 | unsigned char *oob_buff; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 212 | dma_addr_t data_buff_phys; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 213 | int data_dma_ch; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 214 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 215 | struct pxa3xx_nand_host *host[NUM_CHIP_SELECT]; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 216 | unsigned int state; |
| 217 | |
Ezequiel Garcia | c0f3b86 | 2013-08-10 16:34:52 -0300 | [diff] [blame] | 218 | /* |
| 219 | * This driver supports NFCv1 (as found in PXA SoC) |
| 220 | * and NFCv2 (as found in Armada 370/XP SoC). |
| 221 | */ |
| 222 | enum pxa3xx_nand_variant variant; |
| 223 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 224 | int cs; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 225 | int use_ecc; /* use HW ECC ? */ |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 226 | int ecc_bch; /* using BCH ECC? */ |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 227 | int use_dma; /* use DMA ? */ |
Ezequiel Garcia | 5bb653e | 2013-08-12 14:14:49 -0300 | [diff] [blame] | 228 | int use_spare; /* use spare ? */ |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 229 | int need_wait; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 230 | |
Ezequiel Garcia | 2128b08 | 2013-11-07 12:17:16 -0300 | [diff] [blame] | 231 | unsigned int data_size; /* data to be read from FIFO */ |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 232 | unsigned int chunk_size; /* split commands chunk size */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 233 | unsigned int oob_size; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 234 | unsigned int spare_size; |
| 235 | unsigned int ecc_size; |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 236 | unsigned int ecc_err_cnt; |
| 237 | unsigned int max_bitflips; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 238 | int retcode; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 239 | |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 240 | /* cached register value */ |
| 241 | uint32_t reg_ndcr; |
| 242 | uint32_t ndtr0cs0; |
| 243 | uint32_t ndtr1cs0; |
| 244 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 245 | /* generated NDCBx register values */ |
| 246 | uint32_t ndcb0; |
| 247 | uint32_t ndcb1; |
| 248 | uint32_t ndcb2; |
Ezequiel Garcia | 3a1a344 | 2013-08-12 14:14:50 -0300 | [diff] [blame] | 249 | uint32_t ndcb3; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 250 | }; |
| 251 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 252 | static bool use_dma = 1; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 253 | module_param(use_dma, bool, 0444); |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 254 | 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] | 255 | |
Ezequiel García | a9cadf7 | 2015-08-21 15:47:28 -0300 | [diff] [blame] | 256 | struct pxa3xx_nand_timing { |
| 257 | unsigned int tCH; /* Enable signal hold time */ |
| 258 | unsigned int tCS; /* Enable signal setup time */ |
| 259 | unsigned int tWH; /* ND_nWE high duration */ |
| 260 | unsigned int tWP; /* ND_nWE pulse time */ |
| 261 | unsigned int tRH; /* ND_nRE high duration */ |
| 262 | unsigned int tRP; /* ND_nRE pulse width */ |
| 263 | unsigned int tR; /* ND_nWE high to ND_nRE low for read */ |
| 264 | unsigned int tWHR; /* ND_nWE high to ND_nRE low for status read */ |
| 265 | unsigned int tAR; /* ND_ALE low to ND_nRE low delay */ |
| 266 | }; |
| 267 | |
| 268 | struct pxa3xx_nand_flash { |
| 269 | char *name; |
| 270 | uint32_t chip_id; |
| 271 | unsigned int page_per_block; /* Pages per block (PG_PER_BLK) */ |
| 272 | unsigned int page_size; /* Page size in bytes (PAGE_SZ) */ |
| 273 | unsigned int flash_width; /* Width of Flash memory (DWIDTH_M) */ |
| 274 | unsigned int dfc_width; /* Width of flash controller(DWIDTH_C) */ |
| 275 | unsigned int num_blocks; /* Number of physical blocks in Flash */ |
| 276 | |
| 277 | struct pxa3xx_nand_timing *timing; /* NAND Flash timing */ |
| 278 | }; |
| 279 | |
Lei Wen | c1f8247 | 2010-08-17 13:50:23 +0800 | [diff] [blame] | 280 | static struct pxa3xx_nand_timing timing[] = { |
Lei Wen | 227a886 | 2010-08-18 18:00:03 +0800 | [diff] [blame] | 281 | { 40, 80, 60, 100, 80, 100, 90000, 400, 40, }, |
| 282 | { 10, 0, 20, 40, 30, 40, 11123, 110, 10, }, |
| 283 | { 10, 25, 15, 25, 15, 30, 25000, 60, 10, }, |
| 284 | { 10, 35, 15, 25, 15, 25, 25000, 60, 10, }, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 285 | }; |
| 286 | |
Lei Wen | c1f8247 | 2010-08-17 13:50:23 +0800 | [diff] [blame] | 287 | static struct pxa3xx_nand_flash builtin_flash_types[] = { |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 288 | { "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] }, |
| 289 | { "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] }, |
| 290 | { "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] }, |
| 291 | { "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] }, |
| 292 | { "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] }, |
| 293 | { "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] }, |
| 294 | { "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] }, |
| 295 | { "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] }, |
| 296 | { "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] }, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 297 | }; |
| 298 | |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 299 | static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' }; |
| 300 | static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' }; |
| 301 | |
| 302 | static struct nand_bbt_descr bbt_main_descr = { |
| 303 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 304 | | NAND_BBT_2BIT | NAND_BBT_VERSION, |
| 305 | .offs = 8, |
| 306 | .len = 6, |
| 307 | .veroffs = 14, |
| 308 | .maxblocks = 8, /* Last 8 blocks in each chip */ |
| 309 | .pattern = bbt_pattern |
| 310 | }; |
| 311 | |
| 312 | static struct nand_bbt_descr bbt_mirror_descr = { |
| 313 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 314 | | NAND_BBT_2BIT | NAND_BBT_VERSION, |
| 315 | .offs = 8, |
| 316 | .len = 6, |
| 317 | .veroffs = 14, |
| 318 | .maxblocks = 8, /* Last 8 blocks in each chip */ |
| 319 | .pattern = bbt_mirror_pattern |
| 320 | }; |
| 321 | |
Rodolfo Giometti | 3db227b | 2014-01-13 15:35:38 +0100 | [diff] [blame] | 322 | static struct nand_ecclayout ecc_layout_2KB_bch4bit = { |
| 323 | .eccbytes = 32, |
| 324 | .eccpos = { |
| 325 | 32, 33, 34, 35, 36, 37, 38, 39, |
| 326 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 327 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 328 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 329 | .oobfree = { {2, 30} } |
| 330 | }; |
| 331 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 332 | static struct nand_ecclayout ecc_layout_4KB_bch4bit = { |
| 333 | .eccbytes = 64, |
| 334 | .eccpos = { |
| 335 | 32, 33, 34, 35, 36, 37, 38, 39, |
| 336 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 337 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 338 | 56, 57, 58, 59, 60, 61, 62, 63, |
| 339 | 96, 97, 98, 99, 100, 101, 102, 103, |
| 340 | 104, 105, 106, 107, 108, 109, 110, 111, |
| 341 | 112, 113, 114, 115, 116, 117, 118, 119, |
| 342 | 120, 121, 122, 123, 124, 125, 126, 127}, |
| 343 | /* Bootrom looks in bytes 0 & 5 for bad blocks */ |
| 344 | .oobfree = { {6, 26}, { 64, 32} } |
| 345 | }; |
| 346 | |
| 347 | static struct nand_ecclayout ecc_layout_4KB_bch8bit = { |
| 348 | .eccbytes = 128, |
| 349 | .eccpos = { |
| 350 | 32, 33, 34, 35, 36, 37, 38, 39, |
| 351 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 352 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 353 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 354 | .oobfree = { } |
| 355 | }; |
| 356 | |
Lei Wen | 227a886 | 2010-08-18 18:00:03 +0800 | [diff] [blame] | 357 | /* Define a default flash type setting serve as flash detecting only */ |
| 358 | #define DEFAULT_FLASH_TYPE (&builtin_flash_types[0]) |
| 359 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 360 | #define NDTR0_tCH(c) (min((c), 7) << 19) |
| 361 | #define NDTR0_tCS(c) (min((c), 7) << 16) |
| 362 | #define NDTR0_tWH(c) (min((c), 7) << 11) |
| 363 | #define NDTR0_tWP(c) (min((c), 7) << 8) |
| 364 | #define NDTR0_tRH(c) (min((c), 7) << 3) |
| 365 | #define NDTR0_tRP(c) (min((c), 7) << 0) |
| 366 | |
| 367 | #define NDTR1_tR(c) (min((c), 65535) << 16) |
| 368 | #define NDTR1_tWHR(c) (min((c), 15) << 4) |
| 369 | #define NDTR1_tAR(c) (min((c), 15) << 0) |
| 370 | |
| 371 | /* convert nano-seconds to nand flash controller clock cycles */ |
Axel Lin | 93b352f | 2010-08-16 16:09:09 +0800 | [diff] [blame] | 372 | #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 373 | |
Jingoo Han | 17754ad | 2014-05-07 17:49:13 +0900 | [diff] [blame] | 374 | static const struct of_device_id pxa3xx_nand_dt_ids[] = { |
Ezequiel Garcia | c7e9c7e | 2013-11-07 12:17:14 -0300 | [diff] [blame] | 375 | { |
| 376 | .compatible = "marvell,pxa3xx-nand", |
| 377 | .data = (void *)PXA3XX_NAND_VARIANT_PXA, |
| 378 | }, |
Ezequiel Garcia | 1963ff9 | 2013-12-24 12:40:07 -0300 | [diff] [blame] | 379 | { |
| 380 | .compatible = "marvell,armada370-nand", |
| 381 | .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370, |
| 382 | }, |
Ezequiel Garcia | c7e9c7e | 2013-11-07 12:17:14 -0300 | [diff] [blame] | 383 | {} |
| 384 | }; |
| 385 | MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids); |
| 386 | |
| 387 | static enum pxa3xx_nand_variant |
| 388 | pxa3xx_nand_get_variant(struct platform_device *pdev) |
| 389 | { |
| 390 | const struct of_device_id *of_id = |
| 391 | of_match_device(pxa3xx_nand_dt_ids, &pdev->dev); |
| 392 | if (!of_id) |
| 393 | return PXA3XX_NAND_VARIANT_PXA; |
| 394 | return (enum pxa3xx_nand_variant)of_id->data; |
| 395 | } |
| 396 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 397 | static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host, |
Enrico Scholz | 7dad482 | 2008-08-29 12:59:50 +0200 | [diff] [blame] | 398 | const struct pxa3xx_nand_timing *t) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 399 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 400 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 401 | unsigned long nand_clk = clk_get_rate(info->clk); |
| 402 | uint32_t ndtr0, ndtr1; |
| 403 | |
| 404 | ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) | |
| 405 | NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) | |
| 406 | NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) | |
| 407 | NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) | |
| 408 | NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) | |
| 409 | NDTR0_tRP(ns2cycle(t->tRP, nand_clk)); |
| 410 | |
| 411 | ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) | |
| 412 | NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) | |
| 413 | NDTR1_tAR(ns2cycle(t->tAR, nand_clk)); |
| 414 | |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 415 | info->ndtr0cs0 = ndtr0; |
| 416 | info->ndtr1cs0 = ndtr1; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 417 | nand_writel(info, NDTR0CS0, ndtr0); |
| 418 | nand_writel(info, NDTR1CS0, ndtr1); |
| 419 | } |
| 420 | |
Ezequiel Garcia | 6a3e486 | 2013-11-07 12:17:18 -0300 | [diff] [blame] | 421 | /* |
| 422 | * Set the data and OOB size, depending on the selected |
| 423 | * spare and ECC configuration. |
| 424 | * Only applicable to READ0, READOOB and PAGEPROG commands. |
| 425 | */ |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 426 | static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info, |
| 427 | struct mtd_info *mtd) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 428 | { |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 429 | int oob_enable = info->reg_ndcr & NDCR_SPARE_EN; |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 430 | |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 431 | info->data_size = mtd->writesize; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 432 | if (!oob_enable) |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 433 | return; |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 434 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 435 | info->oob_size = info->spare_size; |
| 436 | if (!info->use_ecc) |
| 437 | info->oob_size += info->ecc_size; |
Lei Wen | 18c81b1 | 2010-08-17 17:25:57 +0800 | [diff] [blame] | 438 | } |
| 439 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 440 | /** |
| 441 | * NOTE: it is a must to set ND_RUN firstly, then write |
| 442 | * command buffer, otherwise, it does not work. |
| 443 | * We enable all the interrupt at the same time, and |
| 444 | * let pxa3xx_nand_irq to handle all logic. |
| 445 | */ |
| 446 | static void pxa3xx_nand_start(struct pxa3xx_nand_info *info) |
| 447 | { |
| 448 | uint32_t ndcr; |
| 449 | |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 450 | ndcr = info->reg_ndcr; |
Ezequiel Garcia | cd9d118 | 2013-08-12 14:14:48 -0300 | [diff] [blame] | 451 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 452 | if (info->use_ecc) { |
Ezequiel Garcia | cd9d118 | 2013-08-12 14:14:48 -0300 | [diff] [blame] | 453 | ndcr |= NDCR_ECC_EN; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 454 | if (info->ecc_bch) |
| 455 | nand_writel(info, NDECCCTRL, 0x1); |
| 456 | } else { |
Ezequiel Garcia | cd9d118 | 2013-08-12 14:14:48 -0300 | [diff] [blame] | 457 | ndcr &= ~NDCR_ECC_EN; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 458 | if (info->ecc_bch) |
| 459 | nand_writel(info, NDECCCTRL, 0x0); |
| 460 | } |
Ezequiel Garcia | cd9d118 | 2013-08-12 14:14:48 -0300 | [diff] [blame] | 461 | |
| 462 | if (info->use_dma) |
| 463 | ndcr |= NDCR_DMA_EN; |
| 464 | else |
| 465 | ndcr &= ~NDCR_DMA_EN; |
| 466 | |
Ezequiel Garcia | 5bb653e | 2013-08-12 14:14:49 -0300 | [diff] [blame] | 467 | if (info->use_spare) |
| 468 | ndcr |= NDCR_SPARE_EN; |
| 469 | else |
| 470 | ndcr &= ~NDCR_SPARE_EN; |
| 471 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 472 | ndcr |= NDCR_ND_RUN; |
| 473 | |
| 474 | /* clear status bits and run */ |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 475 | nand_writel(info, NDSR, NDSR_MASK); |
Robert Jarzmik | 0b14392 | 2015-08-19 20:30:14 +0200 | [diff] [blame] | 476 | nand_writel(info, NDCR, 0); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 477 | nand_writel(info, NDCR, ndcr); |
| 478 | } |
| 479 | |
| 480 | static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info) |
| 481 | { |
| 482 | uint32_t ndcr; |
| 483 | int timeout = NAND_STOP_DELAY; |
| 484 | |
| 485 | /* wait RUN bit in NDCR become 0 */ |
| 486 | ndcr = nand_readl(info, NDCR); |
| 487 | while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) { |
| 488 | ndcr = nand_readl(info, NDCR); |
| 489 | udelay(1); |
| 490 | } |
| 491 | |
| 492 | if (timeout <= 0) { |
| 493 | ndcr &= ~NDCR_ND_RUN; |
| 494 | nand_writel(info, NDCR, ndcr); |
| 495 | } |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 496 | if (info->dma_chan) |
| 497 | dmaengine_terminate_all(info->dma_chan); |
| 498 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 499 | /* clear status bits */ |
| 500 | nand_writel(info, NDSR, NDSR_MASK); |
| 501 | } |
| 502 | |
Ezequiel Garcia | 57ff88f | 2013-08-12 14:14:57 -0300 | [diff] [blame] | 503 | static void __maybe_unused |
| 504 | enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 505 | { |
| 506 | uint32_t ndcr; |
| 507 | |
| 508 | ndcr = nand_readl(info, NDCR); |
| 509 | nand_writel(info, NDCR, ndcr & ~int_mask); |
| 510 | } |
| 511 | |
| 512 | static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask) |
| 513 | { |
| 514 | uint32_t ndcr; |
| 515 | |
| 516 | ndcr = nand_readl(info, NDCR); |
| 517 | nand_writel(info, NDCR, ndcr | int_mask); |
| 518 | } |
| 519 | |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 520 | static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len) |
| 521 | { |
| 522 | if (info->ecc_bch) { |
Maxime Ripard | afca11e | 2015-04-07 15:32:45 +0200 | [diff] [blame] | 523 | u32 val; |
| 524 | int ret; |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 525 | |
| 526 | /* |
| 527 | * According to the datasheet, when reading from NDDB |
| 528 | * with BCH enabled, after each 32 bytes reads, we |
| 529 | * have to make sure that the NDSR.RDDREQ bit is set. |
| 530 | * |
| 531 | * Drain the FIFO 8 32 bits reads at a time, and skip |
| 532 | * the polling on the last read. |
| 533 | */ |
| 534 | while (len > 8) { |
Antoine Ténart | ab53a57 | 2015-10-21 10:29:00 +0200 | [diff] [blame^] | 535 | ioread32_rep(info->mmio_base + NDDB, data, 8); |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 536 | |
Maxime Ripard | afca11e | 2015-04-07 15:32:45 +0200 | [diff] [blame] | 537 | ret = readl_relaxed_poll_timeout(info->mmio_base + NDSR, val, |
| 538 | val & NDSR_RDDREQ, 1000, 5000); |
| 539 | if (ret) { |
| 540 | dev_err(&info->pdev->dev, |
| 541 | "Timeout on RDDREQ while draining the FIFO\n"); |
| 542 | return; |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | data += 32; |
| 546 | len -= 8; |
| 547 | } |
| 548 | } |
| 549 | |
Antoine Ténart | ab53a57 | 2015-10-21 10:29:00 +0200 | [diff] [blame^] | 550 | ioread32_rep(info->mmio_base + NDDB, data, len); |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 551 | } |
| 552 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 553 | static void handle_data_pio(struct pxa3xx_nand_info *info) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 554 | { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 555 | unsigned int do_bytes = min(info->data_size, info->chunk_size); |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 556 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 557 | switch (info->state) { |
| 558 | case STATE_PIO_WRITING: |
Rob Herring | ce914e6 | 2015-04-30 15:17:47 -0500 | [diff] [blame] | 559 | writesl(info->mmio_base + NDDB, |
| 560 | info->data_buff + info->data_buff_pos, |
| 561 | DIV_ROUND_UP(do_bytes, 4)); |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 562 | |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 563 | if (info->oob_size > 0) |
Rob Herring | ce914e6 | 2015-04-30 15:17:47 -0500 | [diff] [blame] | 564 | writesl(info->mmio_base + NDDB, |
| 565 | info->oob_buff + info->oob_buff_pos, |
| 566 | DIV_ROUND_UP(info->oob_size, 4)); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 567 | break; |
| 568 | case STATE_PIO_READING: |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 569 | drain_fifo(info, |
| 570 | info->data_buff + info->data_buff_pos, |
| 571 | DIV_ROUND_UP(do_bytes, 4)); |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 572 | |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 573 | if (info->oob_size > 0) |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 574 | drain_fifo(info, |
| 575 | info->oob_buff + info->oob_buff_pos, |
| 576 | DIV_ROUND_UP(info->oob_size, 4)); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 577 | break; |
| 578 | default: |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 579 | dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 580 | info->state); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 581 | BUG(); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 582 | } |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 583 | |
| 584 | /* Update buffer pointers for multi-page read/write */ |
| 585 | info->data_buff_pos += do_bytes; |
| 586 | info->oob_buff_pos += info->oob_size; |
| 587 | info->data_size -= do_bytes; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 588 | } |
| 589 | |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 590 | static void pxa3xx_nand_data_dma_irq(void *data) |
| 591 | { |
| 592 | struct pxa3xx_nand_info *info = data; |
| 593 | struct dma_tx_state state; |
| 594 | enum dma_status status; |
| 595 | |
| 596 | status = dmaengine_tx_status(info->dma_chan, info->dma_cookie, &state); |
| 597 | if (likely(status == DMA_COMPLETE)) { |
| 598 | info->state = STATE_DMA_DONE; |
| 599 | } else { |
| 600 | dev_err(&info->pdev->dev, "DMA error on data channel\n"); |
| 601 | info->retcode = ERR_DMABUSERR; |
| 602 | } |
| 603 | dma_unmap_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir); |
| 604 | |
| 605 | nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ); |
| 606 | enable_int(info, NDCR_INT_MASK); |
| 607 | } |
| 608 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 609 | static void start_data_dma(struct pxa3xx_nand_info *info) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 610 | { |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 611 | enum dma_transfer_direction direction; |
| 612 | struct dma_async_tx_descriptor *tx; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 613 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 614 | switch (info->state) { |
| 615 | case STATE_DMA_WRITING: |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 616 | info->dma_dir = DMA_TO_DEVICE; |
| 617 | direction = DMA_MEM_TO_DEV; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 618 | break; |
| 619 | case STATE_DMA_READING: |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 620 | info->dma_dir = DMA_FROM_DEVICE; |
| 621 | direction = DMA_DEV_TO_MEM; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 622 | break; |
| 623 | default: |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 624 | dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__, |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 625 | info->state); |
| 626 | BUG(); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 627 | } |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 628 | info->sg.length = info->data_size + |
| 629 | (info->oob_size ? info->spare_size + info->ecc_size : 0); |
| 630 | dma_map_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 631 | |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 632 | tx = dmaengine_prep_slave_sg(info->dma_chan, &info->sg, 1, direction, |
| 633 | DMA_PREP_INTERRUPT); |
| 634 | if (!tx) { |
| 635 | dev_err(&info->pdev->dev, "prep_slave_sg() failed\n"); |
| 636 | return; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 637 | } |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 638 | tx->callback = pxa3xx_nand_data_dma_irq; |
| 639 | tx->callback_param = info; |
| 640 | info->dma_cookie = dmaengine_submit(tx); |
| 641 | dma_async_issue_pending(info->dma_chan); |
| 642 | dev_dbg(&info->pdev->dev, "%s(dir=%d cookie=%x size=%u)\n", |
| 643 | __func__, direction, info->dma_cookie, info->sg.length); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 644 | } |
| 645 | |
Robert Jarzmik | 2454225 | 2015-02-20 19:36:43 +0100 | [diff] [blame] | 646 | static irqreturn_t pxa3xx_nand_irq_thread(int irq, void *data) |
| 647 | { |
| 648 | struct pxa3xx_nand_info *info = data; |
| 649 | |
| 650 | handle_data_pio(info); |
| 651 | |
| 652 | info->state = STATE_CMD_DONE; |
| 653 | nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ); |
| 654 | |
| 655 | return IRQ_HANDLED; |
| 656 | } |
| 657 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 658 | static irqreturn_t pxa3xx_nand_irq(int irq, void *devid) |
| 659 | { |
| 660 | struct pxa3xx_nand_info *info = devid; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 661 | unsigned int status, is_completed = 0, is_ready = 0; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 662 | unsigned int ready, cmd_done; |
Robert Jarzmik | 2454225 | 2015-02-20 19:36:43 +0100 | [diff] [blame] | 663 | irqreturn_t ret = IRQ_HANDLED; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 664 | |
| 665 | if (info->cs == 0) { |
| 666 | ready = NDSR_FLASH_RDY; |
| 667 | cmd_done = NDSR_CS0_CMDD; |
| 668 | } else { |
| 669 | ready = NDSR_RDY; |
| 670 | cmd_done = NDSR_CS1_CMDD; |
| 671 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 672 | |
| 673 | status = nand_readl(info, NDSR); |
| 674 | |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 675 | if (status & NDSR_UNCORERR) |
| 676 | info->retcode = ERR_UNCORERR; |
| 677 | if (status & NDSR_CORERR) { |
| 678 | info->retcode = ERR_CORERR; |
| 679 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 && |
| 680 | info->ecc_bch) |
| 681 | info->ecc_err_cnt = NDSR_ERR_CNT(status); |
| 682 | else |
| 683 | info->ecc_err_cnt = 1; |
| 684 | |
| 685 | /* |
| 686 | * Each chunk composing a page is corrected independently, |
| 687 | * and we need to store maximum number of corrected bitflips |
| 688 | * to return it to the MTD layer in ecc.read_page(). |
| 689 | */ |
| 690 | info->max_bitflips = max_t(unsigned int, |
| 691 | info->max_bitflips, |
| 692 | info->ecc_err_cnt); |
| 693 | } |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 694 | if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) { |
| 695 | /* whether use dma to transfer data */ |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 696 | if (info->use_dma) { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 697 | disable_int(info, NDCR_INT_MASK); |
| 698 | info->state = (status & NDSR_RDDREQ) ? |
| 699 | STATE_DMA_READING : STATE_DMA_WRITING; |
| 700 | start_data_dma(info); |
| 701 | goto NORMAL_IRQ_EXIT; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 702 | } else { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 703 | info->state = (status & NDSR_RDDREQ) ? |
| 704 | STATE_PIO_READING : STATE_PIO_WRITING; |
Robert Jarzmik | 2454225 | 2015-02-20 19:36:43 +0100 | [diff] [blame] | 705 | ret = IRQ_WAKE_THREAD; |
| 706 | goto NORMAL_IRQ_EXIT; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 707 | } |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 708 | } |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 709 | if (status & cmd_done) { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 710 | info->state = STATE_CMD_DONE; |
| 711 | is_completed = 1; |
| 712 | } |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 713 | if (status & ready) { |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 714 | info->state = STATE_READY; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 715 | is_ready = 1; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 716 | } |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 717 | |
Robert Jarzmik | 21fc0ef | 2015-08-19 20:30:15 +0200 | [diff] [blame] | 718 | /* |
| 719 | * Clear all status bit before issuing the next command, which |
| 720 | * can and will alter the status bits and will deserve a new |
| 721 | * interrupt on its own. This lets the controller exit the IRQ |
| 722 | */ |
| 723 | nand_writel(info, NDSR, status); |
| 724 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 725 | if (status & NDSR_WRCMDREQ) { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 726 | status &= ~NDSR_WRCMDREQ; |
| 727 | info->state = STATE_CMD_HANDLE; |
Ezequiel Garcia | 3a1a344 | 2013-08-12 14:14:50 -0300 | [diff] [blame] | 728 | |
| 729 | /* |
| 730 | * Command buffer registers NDCB{0-2} (and optionally NDCB3) |
| 731 | * must be loaded by writing directly either 12 or 16 |
| 732 | * bytes directly to NDCB0, four bytes at a time. |
| 733 | * |
| 734 | * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored |
| 735 | * but each NDCBx register can be read. |
| 736 | */ |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 737 | nand_writel(info, NDCB0, info->ndcb0); |
| 738 | nand_writel(info, NDCB0, info->ndcb1); |
| 739 | nand_writel(info, NDCB0, info->ndcb2); |
Ezequiel Garcia | 3a1a344 | 2013-08-12 14:14:50 -0300 | [diff] [blame] | 740 | |
| 741 | /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */ |
| 742 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) |
| 743 | nand_writel(info, NDCB0, info->ndcb3); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 744 | } |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 745 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 746 | if (is_completed) |
| 747 | complete(&info->cmd_complete); |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 748 | if (is_ready) |
| 749 | complete(&info->dev_ready); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 750 | NORMAL_IRQ_EXIT: |
Robert Jarzmik | 2454225 | 2015-02-20 19:36:43 +0100 | [diff] [blame] | 751 | return ret; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 752 | } |
| 753 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 754 | static inline int is_buf_blank(uint8_t *buf, size_t len) |
| 755 | { |
| 756 | for (; len > 0; len--) |
| 757 | if (*buf++ != 0xff) |
| 758 | return 0; |
| 759 | return 1; |
| 760 | } |
| 761 | |
Ezequiel Garcia | 86beeba | 2013-11-14 18:25:31 -0300 | [diff] [blame] | 762 | static void set_command_address(struct pxa3xx_nand_info *info, |
| 763 | unsigned int page_size, uint16_t column, int page_addr) |
| 764 | { |
| 765 | /* small page addr setting */ |
| 766 | if (page_size < PAGE_CHUNK_SIZE) { |
| 767 | info->ndcb1 = ((page_addr & 0xFFFFFF) << 8) |
| 768 | | (column & 0xFF); |
| 769 | |
| 770 | info->ndcb2 = 0; |
| 771 | } else { |
| 772 | info->ndcb1 = ((page_addr & 0xFFFF) << 16) |
| 773 | | (column & 0xFFFF); |
| 774 | |
| 775 | if (page_addr & 0xFF0000) |
| 776 | info->ndcb2 = (page_addr & 0xFF0000) >> 16; |
| 777 | else |
| 778 | info->ndcb2 = 0; |
| 779 | } |
| 780 | } |
| 781 | |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 782 | static void prepare_start_command(struct pxa3xx_nand_info *info, int command) |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 783 | { |
Ezequiel Garcia | 39f83d1 | 2013-11-14 18:25:34 -0300 | [diff] [blame] | 784 | struct pxa3xx_nand_host *host = info->host[info->cs]; |
| 785 | struct mtd_info *mtd = host->mtd; |
| 786 | |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 787 | /* reset data and oob column point to handle data */ |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 788 | info->buf_start = 0; |
| 789 | info->buf_count = 0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 790 | info->oob_size = 0; |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 791 | info->data_buff_pos = 0; |
| 792 | info->oob_buff_pos = 0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 793 | info->use_ecc = 0; |
Ezequiel Garcia | 5bb653e | 2013-08-12 14:14:49 -0300 | [diff] [blame] | 794 | info->use_spare = 1; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 795 | info->retcode = ERR_NONE; |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 796 | info->ecc_err_cnt = 0; |
Ezequiel Garcia | f0e6a32e | 2013-11-14 18:25:30 -0300 | [diff] [blame] | 797 | info->ndcb3 = 0; |
Ezequiel Garcia | d20d0a6 | 2013-12-18 18:44:08 -0300 | [diff] [blame] | 798 | info->need_wait = 0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 799 | |
| 800 | switch (command) { |
| 801 | case NAND_CMD_READ0: |
| 802 | case NAND_CMD_PAGEPROG: |
| 803 | info->use_ecc = 1; |
| 804 | case NAND_CMD_READOOB: |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 805 | pxa3xx_set_datasize(info, mtd); |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 806 | break; |
Ezequiel Garcia | 41a6343 | 2013-08-12 14:14:51 -0300 | [diff] [blame] | 807 | case NAND_CMD_PARAM: |
| 808 | info->use_spare = 0; |
| 809 | break; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 810 | default: |
| 811 | info->ndcb1 = 0; |
| 812 | info->ndcb2 = 0; |
| 813 | break; |
| 814 | } |
Ezequiel Garcia | 39f83d1 | 2013-11-14 18:25:34 -0300 | [diff] [blame] | 815 | |
| 816 | /* |
| 817 | * If we are about to issue a read command, or about to set |
| 818 | * the write address, then clean the data buffer. |
| 819 | */ |
| 820 | if (command == NAND_CMD_READ0 || |
| 821 | command == NAND_CMD_READOOB || |
| 822 | command == NAND_CMD_SEQIN) { |
| 823 | |
| 824 | info->buf_count = mtd->writesize + mtd->oobsize; |
| 825 | memset(info->data_buff, 0xFF, info->buf_count); |
| 826 | } |
| 827 | |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | static int prepare_set_command(struct pxa3xx_nand_info *info, int command, |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 831 | int ext_cmd_type, uint16_t column, int page_addr) |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 832 | { |
| 833 | int addr_cycle, exec_cmd; |
| 834 | struct pxa3xx_nand_host *host; |
| 835 | struct mtd_info *mtd; |
| 836 | |
| 837 | host = info->host[info->cs]; |
| 838 | mtd = host->mtd; |
| 839 | addr_cycle = 0; |
| 840 | exec_cmd = 1; |
| 841 | |
| 842 | if (info->cs != 0) |
| 843 | info->ndcb0 = NDCB0_CSEL; |
| 844 | else |
| 845 | info->ndcb0 = 0; |
| 846 | |
| 847 | if (command == NAND_CMD_SEQIN) |
| 848 | exec_cmd = 0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 849 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 850 | addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles |
| 851 | + host->col_addr_cycles); |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 852 | |
| 853 | switch (command) { |
| 854 | case NAND_CMD_READOOB: |
| 855 | case NAND_CMD_READ0: |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 856 | info->buf_start = column; |
| 857 | info->ndcb0 |= NDCB0_CMD_TYPE(0) |
| 858 | | addr_cycle |
| 859 | | NAND_CMD_READ0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 860 | |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 861 | if (command == NAND_CMD_READOOB) |
| 862 | info->buf_start += mtd->writesize; |
| 863 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 864 | /* |
| 865 | * Multiple page read needs an 'extended command type' field, |
| 866 | * which is either naked-read or last-read according to the |
| 867 | * state. |
| 868 | */ |
| 869 | if (mtd->writesize == PAGE_CHUNK_SIZE) { |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 870 | info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8); |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 871 | } else if (mtd->writesize > PAGE_CHUNK_SIZE) { |
| 872 | info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8) |
| 873 | | NDCB0_LEN_OVRD |
| 874 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type); |
| 875 | info->ndcb3 = info->chunk_size + |
| 876 | info->oob_size; |
| 877 | } |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 878 | |
Ezequiel Garcia | 01d9947 | 2013-11-14 18:25:32 -0300 | [diff] [blame] | 879 | set_command_address(info, mtd->writesize, column, page_addr); |
Ezequiel Garcia | 01d9947 | 2013-11-14 18:25:32 -0300 | [diff] [blame] | 880 | break; |
| 881 | |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 882 | case NAND_CMD_SEQIN: |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 883 | |
Ezequiel Garcia | e7f9a6a | 2013-11-14 18:25:35 -0300 | [diff] [blame] | 884 | info->buf_start = column; |
| 885 | set_command_address(info, mtd->writesize, 0, page_addr); |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 886 | |
| 887 | /* |
| 888 | * Multiple page programming needs to execute the initial |
| 889 | * SEQIN command that sets the page address. |
| 890 | */ |
| 891 | if (mtd->writesize > PAGE_CHUNK_SIZE) { |
| 892 | info->ndcb0 |= NDCB0_CMD_TYPE(0x1) |
| 893 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type) |
| 894 | | addr_cycle |
| 895 | | command; |
| 896 | /* No data transfer in this case */ |
| 897 | info->data_size = 0; |
| 898 | exec_cmd = 1; |
| 899 | } |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 900 | break; |
| 901 | |
| 902 | case NAND_CMD_PAGEPROG: |
| 903 | if (is_buf_blank(info->data_buff, |
| 904 | (mtd->writesize + mtd->oobsize))) { |
| 905 | exec_cmd = 0; |
| 906 | break; |
| 907 | } |
| 908 | |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 909 | /* Second command setting for large pages */ |
| 910 | if (mtd->writesize > PAGE_CHUNK_SIZE) { |
| 911 | /* |
| 912 | * Multiple page write uses the 'extended command' |
| 913 | * field. This can be used to issue a command dispatch |
| 914 | * or a naked-write depending on the current stage. |
| 915 | */ |
| 916 | info->ndcb0 |= NDCB0_CMD_TYPE(0x1) |
| 917 | | NDCB0_LEN_OVRD |
| 918 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type); |
| 919 | info->ndcb3 = info->chunk_size + |
| 920 | info->oob_size; |
| 921 | |
| 922 | /* |
| 923 | * This is the command dispatch that completes a chunked |
| 924 | * page program operation. |
| 925 | */ |
| 926 | if (info->data_size == 0) { |
| 927 | info->ndcb0 = NDCB0_CMD_TYPE(0x1) |
| 928 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type) |
| 929 | | command; |
| 930 | info->ndcb1 = 0; |
| 931 | info->ndcb2 = 0; |
| 932 | info->ndcb3 = 0; |
| 933 | } |
| 934 | } else { |
| 935 | info->ndcb0 |= NDCB0_CMD_TYPE(0x1) |
| 936 | | NDCB0_AUTO_RS |
| 937 | | NDCB0_ST_ROW_EN |
| 938 | | NDCB0_DBC |
| 939 | | (NAND_CMD_PAGEPROG << 8) |
| 940 | | NAND_CMD_SEQIN |
| 941 | | addr_cycle; |
| 942 | } |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 943 | break; |
| 944 | |
Ezequiel Garcia | ce0268f | 2013-05-14 08:15:25 -0300 | [diff] [blame] | 945 | case NAND_CMD_PARAM: |
Ezequiel Garcia | c163409 | 2015-08-03 11:31:26 -0300 | [diff] [blame] | 946 | info->buf_count = INIT_BUFFER_SIZE; |
Ezequiel Garcia | ce0268f | 2013-05-14 08:15:25 -0300 | [diff] [blame] | 947 | info->ndcb0 |= NDCB0_CMD_TYPE(0) |
| 948 | | NDCB0_ADDR_CYC(1) |
Ezequiel Garcia | 41a6343 | 2013-08-12 14:14:51 -0300 | [diff] [blame] | 949 | | NDCB0_LEN_OVRD |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 950 | | command; |
Ezequiel Garcia | ce0268f | 2013-05-14 08:15:25 -0300 | [diff] [blame] | 951 | info->ndcb1 = (column & 0xFF); |
Ezequiel Garcia | c163409 | 2015-08-03 11:31:26 -0300 | [diff] [blame] | 952 | info->ndcb3 = INIT_BUFFER_SIZE; |
| 953 | info->data_size = INIT_BUFFER_SIZE; |
Ezequiel Garcia | ce0268f | 2013-05-14 08:15:25 -0300 | [diff] [blame] | 954 | break; |
| 955 | |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 956 | case NAND_CMD_READID: |
Ezequiel García | b226eca | 2015-08-19 19:40:09 -0300 | [diff] [blame] | 957 | info->buf_count = READ_ID_BYTES; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 958 | info->ndcb0 |= NDCB0_CMD_TYPE(3) |
| 959 | | NDCB0_ADDR_CYC(1) |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 960 | | command; |
Ezequiel Garcia | d14231f | 2013-05-14 08:15:24 -0300 | [diff] [blame] | 961 | info->ndcb1 = (column & 0xFF); |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 962 | |
| 963 | info->data_size = 8; |
| 964 | break; |
| 965 | case NAND_CMD_STATUS: |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 966 | info->buf_count = 1; |
| 967 | info->ndcb0 |= NDCB0_CMD_TYPE(4) |
| 968 | | NDCB0_ADDR_CYC(1) |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 969 | | command; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 970 | |
| 971 | info->data_size = 8; |
| 972 | break; |
| 973 | |
| 974 | case NAND_CMD_ERASE1: |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 975 | info->ndcb0 |= NDCB0_CMD_TYPE(2) |
| 976 | | NDCB0_AUTO_RS |
| 977 | | NDCB0_ADDR_CYC(3) |
| 978 | | NDCB0_DBC |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 979 | | (NAND_CMD_ERASE2 << 8) |
| 980 | | NAND_CMD_ERASE1; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 981 | info->ndcb1 = page_addr; |
| 982 | info->ndcb2 = 0; |
| 983 | |
| 984 | break; |
| 985 | case NAND_CMD_RESET: |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 986 | info->ndcb0 |= NDCB0_CMD_TYPE(5) |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 987 | | command; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 988 | |
| 989 | break; |
| 990 | |
| 991 | case NAND_CMD_ERASE2: |
| 992 | exec_cmd = 0; |
| 993 | break; |
| 994 | |
| 995 | default: |
| 996 | exec_cmd = 0; |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 997 | dev_err(&info->pdev->dev, "non-supported command %x\n", |
| 998 | command); |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 999 | break; |
| 1000 | } |
| 1001 | |
| 1002 | return exec_cmd; |
| 1003 | } |
| 1004 | |
Ezequiel Garcia | 5cbbdc6 | 2013-12-18 18:44:09 -0300 | [diff] [blame] | 1005 | static void nand_cmdfunc(struct mtd_info *mtd, unsigned command, |
| 1006 | int column, int page_addr) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1007 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1008 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1009 | struct pxa3xx_nand_info *info = host->info_data; |
Nicholas Mc Guire | e5860c1 | 2015-02-01 11:55:37 -0500 | [diff] [blame] | 1010 | int exec_cmd; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1011 | |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1012 | /* |
| 1013 | * if this is a x16 device ,then convert the input |
| 1014 | * "byte" address into a "word" address appropriate |
| 1015 | * for indexing a word-oriented device |
| 1016 | */ |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 1017 | if (info->reg_ndcr & NDCR_DWIDTH_M) |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1018 | column /= 2; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1019 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1020 | /* |
| 1021 | * There may be different NAND chip hooked to |
| 1022 | * different chip select, so check whether |
| 1023 | * chip select has been changed, if yes, reset the timing |
| 1024 | */ |
| 1025 | if (info->cs != host->cs) { |
| 1026 | info->cs = host->cs; |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 1027 | nand_writel(info, NDTR0CS0, info->ndtr0cs0); |
| 1028 | nand_writel(info, NDTR1CS0, info->ndtr1cs0); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 1031 | prepare_start_command(info, command); |
| 1032 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1033 | info->state = STATE_PREPARED; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1034 | exec_cmd = prepare_set_command(info, command, 0, column, page_addr); |
| 1035 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1036 | if (exec_cmd) { |
| 1037 | init_completion(&info->cmd_complete); |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1038 | init_completion(&info->dev_ready); |
| 1039 | info->need_wait = 1; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1040 | pxa3xx_nand_start(info); |
| 1041 | |
Nicholas Mc Guire | e5860c1 | 2015-02-01 11:55:37 -0500 | [diff] [blame] | 1042 | if (!wait_for_completion_timeout(&info->cmd_complete, |
| 1043 | CHIP_DELAY_TIMEOUT)) { |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1044 | dev_err(&info->pdev->dev, "Wait time out!!!\n"); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1045 | /* Stop State Machine for next command cycle */ |
| 1046 | pxa3xx_nand_stop(info); |
| 1047 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1048 | } |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1049 | info->state = STATE_IDLE; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1050 | } |
| 1051 | |
Ezequiel Garcia | 5cbbdc6 | 2013-12-18 18:44:09 -0300 | [diff] [blame] | 1052 | static void nand_cmdfunc_extended(struct mtd_info *mtd, |
| 1053 | const unsigned command, |
| 1054 | int column, int page_addr) |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1055 | { |
| 1056 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1057 | struct pxa3xx_nand_info *info = host->info_data; |
Nicholas Mc Guire | e5860c1 | 2015-02-01 11:55:37 -0500 | [diff] [blame] | 1058 | int exec_cmd, ext_cmd_type; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1059 | |
| 1060 | /* |
| 1061 | * if this is a x16 device then convert the input |
| 1062 | * "byte" address into a "word" address appropriate |
| 1063 | * for indexing a word-oriented device |
| 1064 | */ |
| 1065 | if (info->reg_ndcr & NDCR_DWIDTH_M) |
| 1066 | column /= 2; |
| 1067 | |
| 1068 | /* |
| 1069 | * There may be different NAND chip hooked to |
| 1070 | * different chip select, so check whether |
| 1071 | * chip select has been changed, if yes, reset the timing |
| 1072 | */ |
| 1073 | if (info->cs != host->cs) { |
| 1074 | info->cs = host->cs; |
| 1075 | nand_writel(info, NDTR0CS0, info->ndtr0cs0); |
| 1076 | nand_writel(info, NDTR1CS0, info->ndtr1cs0); |
| 1077 | } |
| 1078 | |
| 1079 | /* Select the extended command for the first command */ |
| 1080 | switch (command) { |
| 1081 | case NAND_CMD_READ0: |
| 1082 | case NAND_CMD_READOOB: |
| 1083 | ext_cmd_type = EXT_CMD_TYPE_MONO; |
| 1084 | break; |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 1085 | case NAND_CMD_SEQIN: |
| 1086 | ext_cmd_type = EXT_CMD_TYPE_DISPATCH; |
| 1087 | break; |
| 1088 | case NAND_CMD_PAGEPROG: |
| 1089 | ext_cmd_type = EXT_CMD_TYPE_NAKED_RW; |
| 1090 | break; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1091 | default: |
| 1092 | ext_cmd_type = 0; |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 1093 | break; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | prepare_start_command(info, command); |
| 1097 | |
| 1098 | /* |
| 1099 | * Prepare the "is ready" completion before starting a command |
| 1100 | * transaction sequence. If the command is not executed the |
| 1101 | * completion will be completed, see below. |
| 1102 | * |
| 1103 | * We can do that inside the loop because the command variable |
| 1104 | * is invariant and thus so is the exec_cmd. |
| 1105 | */ |
| 1106 | info->need_wait = 1; |
| 1107 | init_completion(&info->dev_ready); |
| 1108 | do { |
| 1109 | info->state = STATE_PREPARED; |
| 1110 | exec_cmd = prepare_set_command(info, command, ext_cmd_type, |
| 1111 | column, page_addr); |
| 1112 | if (!exec_cmd) { |
| 1113 | info->need_wait = 0; |
| 1114 | complete(&info->dev_ready); |
| 1115 | break; |
| 1116 | } |
| 1117 | |
| 1118 | init_completion(&info->cmd_complete); |
| 1119 | pxa3xx_nand_start(info); |
| 1120 | |
Nicholas Mc Guire | e5860c1 | 2015-02-01 11:55:37 -0500 | [diff] [blame] | 1121 | if (!wait_for_completion_timeout(&info->cmd_complete, |
| 1122 | CHIP_DELAY_TIMEOUT)) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1123 | dev_err(&info->pdev->dev, "Wait time out!!!\n"); |
| 1124 | /* Stop State Machine for next command cycle */ |
| 1125 | pxa3xx_nand_stop(info); |
| 1126 | break; |
| 1127 | } |
| 1128 | |
| 1129 | /* Check if the sequence is complete */ |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 1130 | if (info->data_size == 0 && command != NAND_CMD_PAGEPROG) |
| 1131 | break; |
| 1132 | |
| 1133 | /* |
| 1134 | * After a splitted program command sequence has issued |
| 1135 | * the command dispatch, the command sequence is complete. |
| 1136 | */ |
| 1137 | if (info->data_size == 0 && |
| 1138 | command == NAND_CMD_PAGEPROG && |
| 1139 | ext_cmd_type == EXT_CMD_TYPE_DISPATCH) |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1140 | break; |
| 1141 | |
| 1142 | if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) { |
| 1143 | /* Last read: issue a 'last naked read' */ |
| 1144 | if (info->data_size == info->chunk_size) |
| 1145 | ext_cmd_type = EXT_CMD_TYPE_LAST_RW; |
| 1146 | else |
| 1147 | ext_cmd_type = EXT_CMD_TYPE_NAKED_RW; |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 1148 | |
| 1149 | /* |
| 1150 | * If a splitted program command has no more data to transfer, |
| 1151 | * the command dispatch must be issued to complete. |
| 1152 | */ |
| 1153 | } else if (command == NAND_CMD_PAGEPROG && |
| 1154 | info->data_size == 0) { |
| 1155 | ext_cmd_type = EXT_CMD_TYPE_DISPATCH; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1156 | } |
| 1157 | } while (1); |
| 1158 | |
| 1159 | info->state = STATE_IDLE; |
| 1160 | } |
| 1161 | |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 1162 | static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 1163 | struct nand_chip *chip, const uint8_t *buf, int oob_required, |
| 1164 | int page) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1165 | { |
| 1166 | chip->write_buf(mtd, buf, mtd->writesize); |
| 1167 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 1168 | |
| 1169 | return 0; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1173 | struct nand_chip *chip, uint8_t *buf, int oob_required, |
| 1174 | int page) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1175 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1176 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1177 | struct pxa3xx_nand_info *info = host->info_data; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1178 | |
| 1179 | chip->read_buf(mtd, buf, mtd->writesize); |
| 1180 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1181 | |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 1182 | if (info->retcode == ERR_CORERR && info->use_ecc) { |
| 1183 | mtd->ecc_stats.corrected += info->ecc_err_cnt; |
| 1184 | |
| 1185 | } else if (info->retcode == ERR_UNCORERR) { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1186 | /* |
| 1187 | * for blank page (all 0xff), HW will calculate its ECC as |
| 1188 | * 0, which is different from the ECC information within |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 1189 | * OOB, ignore such uncorrectable errors |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1190 | */ |
| 1191 | if (is_buf_blank(buf, mtd->writesize)) |
Daniel Mack | 543e32d | 2011-06-07 03:01:07 -0700 | [diff] [blame] | 1192 | info->retcode = ERR_NONE; |
| 1193 | else |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1194 | mtd->ecc_stats.failed++; |
| 1195 | } |
| 1196 | |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 1197 | return info->max_bitflips; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1198 | } |
| 1199 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1200 | static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd) |
| 1201 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1202 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1203 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1204 | char retval = 0xFF; |
| 1205 | |
| 1206 | if (info->buf_start < info->buf_count) |
| 1207 | /* Has just send a new command? */ |
| 1208 | retval = info->data_buff[info->buf_start++]; |
| 1209 | |
| 1210 | return retval; |
| 1211 | } |
| 1212 | |
| 1213 | static u16 pxa3xx_nand_read_word(struct mtd_info *mtd) |
| 1214 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1215 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1216 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1217 | u16 retval = 0xFFFF; |
| 1218 | |
| 1219 | if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) { |
| 1220 | retval = *((u16 *)(info->data_buff+info->buf_start)); |
| 1221 | info->buf_start += 2; |
| 1222 | } |
| 1223 | return retval; |
| 1224 | } |
| 1225 | |
| 1226 | static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 1227 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1228 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1229 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1230 | int real_len = min_t(size_t, len, info->buf_count - info->buf_start); |
| 1231 | |
| 1232 | memcpy(buf, info->data_buff + info->buf_start, real_len); |
| 1233 | info->buf_start += real_len; |
| 1234 | } |
| 1235 | |
| 1236 | static void pxa3xx_nand_write_buf(struct mtd_info *mtd, |
| 1237 | const uint8_t *buf, int len) |
| 1238 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1239 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1240 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1241 | int real_len = min_t(size_t, len, info->buf_count - info->buf_start); |
| 1242 | |
| 1243 | memcpy(info->data_buff + info->buf_start, buf, real_len); |
| 1244 | info->buf_start += real_len; |
| 1245 | } |
| 1246 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1247 | static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip) |
| 1248 | { |
| 1249 | return; |
| 1250 | } |
| 1251 | |
| 1252 | static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this) |
| 1253 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1254 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1255 | struct pxa3xx_nand_info *info = host->info_data; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1256 | |
| 1257 | if (info->need_wait) { |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1258 | info->need_wait = 0; |
Nicholas Mc Guire | e5860c1 | 2015-02-01 11:55:37 -0500 | [diff] [blame] | 1259 | if (!wait_for_completion_timeout(&info->dev_ready, |
| 1260 | CHIP_DELAY_TIMEOUT)) { |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1261 | dev_err(&info->pdev->dev, "Ready time out!!!\n"); |
| 1262 | return NAND_STATUS_FAIL; |
| 1263 | } |
| 1264 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1265 | |
| 1266 | /* pxa3xx_nand_send_command has waited for command complete */ |
| 1267 | if (this->state == FL_WRITING || this->state == FL_ERASING) { |
| 1268 | if (info->retcode == ERR_NONE) |
| 1269 | return 0; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1270 | else |
| 1271 | return NAND_STATUS_FAIL; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1272 | } |
| 1273 | |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1274 | return NAND_STATUS_READY; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1275 | } |
| 1276 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1277 | static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info, |
Enrico Scholz | c8c17c8 | 2008-08-29 12:59:51 +0200 | [diff] [blame] | 1278 | const struct pxa3xx_nand_flash *f) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1279 | { |
| 1280 | struct platform_device *pdev = info->pdev; |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1281 | struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1282 | struct pxa3xx_nand_host *host = info->host[info->cs]; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1283 | uint32_t ndcr = 0x0; /* enable all interrupts */ |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1284 | |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1285 | if (f->page_size != 2048 && f->page_size != 512) { |
| 1286 | dev_err(&pdev->dev, "Current only support 2048 and 512 size\n"); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1287 | return -EINVAL; |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1288 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1289 | |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1290 | if (f->flash_width != 16 && f->flash_width != 8) { |
| 1291 | dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n"); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1292 | return -EINVAL; |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1293 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1294 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1295 | /* calculate addressing information */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1296 | host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1297 | |
| 1298 | if (f->num_blocks * f->page_per_block > 65536) |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1299 | host->row_addr_cycles = 3; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1300 | else |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1301 | host->row_addr_cycles = 2; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1302 | |
| 1303 | ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1304 | ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1305 | ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0; |
| 1306 | ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0; |
| 1307 | ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0; |
| 1308 | ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0; |
| 1309 | |
Ezequiel García | b226eca | 2015-08-19 19:40:09 -0300 | [diff] [blame] | 1310 | ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1311 | ndcr |= NDCR_SPARE_EN; /* enable spare by default */ |
| 1312 | |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 1313 | info->reg_ndcr = ndcr; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1314 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1315 | pxa3xx_nand_set_timing(host, f->timing); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1316 | return 0; |
| 1317 | } |
| 1318 | |
Mike Rapoport | f271049 | 2009-02-17 13:54:47 +0200 | [diff] [blame] | 1319 | static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info) |
| 1320 | { |
| 1321 | uint32_t ndcr = nand_readl(info, NDCR); |
Mike Rapoport | f271049 | 2009-02-17 13:54:47 +0200 | [diff] [blame] | 1322 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1323 | /* Set an initial chunk size */ |
Ezequiel García | b226eca | 2015-08-19 19:40:09 -0300 | [diff] [blame] | 1324 | info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512; |
Robert Jarzmik | e971aff | 2015-09-28 22:56:51 +0200 | [diff] [blame] | 1325 | info->reg_ndcr = ndcr & |
| 1326 | ~(NDCR_INT_MASK | NDCR_ND_ARB_EN | NFCV1_NDCR_ARB_CNTL); |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 1327 | info->ndtr0cs0 = nand_readl(info, NDTR0CS0); |
| 1328 | info->ndtr1cs0 = nand_readl(info, NDTR1CS0); |
Mike Rapoport | f271049 | 2009-02-17 13:54:47 +0200 | [diff] [blame] | 1329 | return 0; |
| 1330 | } |
| 1331 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1332 | static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info) |
| 1333 | { |
| 1334 | struct platform_device *pdev = info->pdev; |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1335 | struct dma_slave_config config; |
| 1336 | dma_cap_mask_t mask; |
| 1337 | struct pxad_param param; |
| 1338 | int ret; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1339 | |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1340 | info->data_buff = kmalloc(info->buf_size, GFP_KERNEL); |
| 1341 | if (info->data_buff == NULL) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1342 | return -ENOMEM; |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1343 | if (use_dma == 0) |
| 1344 | return 0; |
| 1345 | |
| 1346 | ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
| 1347 | if (ret) |
| 1348 | return ret; |
| 1349 | |
| 1350 | sg_init_one(&info->sg, info->data_buff, info->buf_size); |
| 1351 | dma_cap_zero(mask); |
| 1352 | dma_cap_set(DMA_SLAVE, mask); |
| 1353 | param.prio = PXAD_PRIO_LOWEST; |
| 1354 | param.drcmr = info->drcmr_dat; |
| 1355 | info->dma_chan = dma_request_slave_channel_compat(mask, pxad_filter_fn, |
| 1356 | ¶m, &pdev->dev, |
| 1357 | "data"); |
| 1358 | if (!info->dma_chan) { |
| 1359 | dev_err(&pdev->dev, "unable to request data dma channel\n"); |
| 1360 | return -ENODEV; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1361 | } |
| 1362 | |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1363 | memset(&config, 0, sizeof(config)); |
| 1364 | config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1365 | config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1366 | config.src_addr = info->mmio_phys + NDDB; |
| 1367 | config.dst_addr = info->mmio_phys + NDDB; |
| 1368 | config.src_maxburst = 32; |
| 1369 | config.dst_maxburst = 32; |
| 1370 | ret = dmaengine_slave_config(info->dma_chan, &config); |
| 1371 | if (ret < 0) { |
| 1372 | dev_err(&info->pdev->dev, |
| 1373 | "dma channel configuration failed: %d\n", |
| 1374 | ret); |
| 1375 | return ret; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1376 | } |
| 1377 | |
Ezequiel Garcia | 95b2656 | 2013-10-04 15:30:37 -0300 | [diff] [blame] | 1378 | /* |
| 1379 | * Now that DMA buffers are allocated we turn on |
| 1380 | * DMA proper for I/O operations. |
| 1381 | */ |
| 1382 | info->use_dma = 1; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1383 | return 0; |
| 1384 | } |
| 1385 | |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1386 | static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info) |
| 1387 | { |
Ezequiel Garcia | 15b540c | 2013-12-10 09:57:15 -0300 | [diff] [blame] | 1388 | if (info->use_dma) { |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1389 | dmaengine_terminate_all(info->dma_chan); |
| 1390 | dma_release_channel(info->dma_chan); |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1391 | } |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 1392 | kfree(info->data_buff); |
| 1393 | } |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1394 | |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1395 | static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1396 | { |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1397 | struct mtd_info *mtd; |
Ezequiel Garcia | 2d79ab1 | 2013-11-07 12:17:15 -0300 | [diff] [blame] | 1398 | struct nand_chip *chip; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1399 | int ret; |
Ezequiel Garcia | 2d79ab1 | 2013-11-07 12:17:15 -0300 | [diff] [blame] | 1400 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1401 | mtd = info->host[info->cs]->mtd; |
Ezequiel Garcia | 2d79ab1 | 2013-11-07 12:17:15 -0300 | [diff] [blame] | 1402 | chip = mtd->priv; |
| 1403 | |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1404 | /* use the common timing to make a try */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1405 | ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]); |
| 1406 | if (ret) |
| 1407 | return ret; |
| 1408 | |
Ezequiel Garcia | 2d79ab1 | 2013-11-07 12:17:15 -0300 | [diff] [blame] | 1409 | chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0); |
Ezequiel Garcia | 56704d8 | 2013-11-14 18:25:27 -0300 | [diff] [blame] | 1410 | ret = chip->waitfunc(mtd, chip); |
| 1411 | if (ret & NAND_STATUS_FAIL) |
| 1412 | return -ENODEV; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1413 | |
Ezequiel Garcia | 56704d8 | 2013-11-14 18:25:27 -0300 | [diff] [blame] | 1414 | return 0; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1415 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1416 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1417 | static int pxa_ecc_init(struct pxa3xx_nand_info *info, |
| 1418 | struct nand_ecc_ctrl *ecc, |
Ezequiel Garcia | 30b2afc | 2013-12-18 18:44:10 -0300 | [diff] [blame] | 1419 | int strength, int ecc_stepsize, int page_size) |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1420 | { |
Ezequiel Garcia | 30b2afc | 2013-12-18 18:44:10 -0300 | [diff] [blame] | 1421 | if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1422 | info->chunk_size = 2048; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1423 | info->spare_size = 40; |
| 1424 | info->ecc_size = 24; |
| 1425 | ecc->mode = NAND_ECC_HW; |
| 1426 | ecc->size = 512; |
| 1427 | ecc->strength = 1; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1428 | |
Ezequiel Garcia | 30b2afc | 2013-12-18 18:44:10 -0300 | [diff] [blame] | 1429 | } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1430 | info->chunk_size = 512; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1431 | info->spare_size = 8; |
| 1432 | info->ecc_size = 8; |
| 1433 | ecc->mode = NAND_ECC_HW; |
| 1434 | ecc->size = 512; |
| 1435 | ecc->strength = 1; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1436 | |
Brian Norris | 6033a94 | 2013-11-14 14:41:32 -0800 | [diff] [blame] | 1437 | /* |
| 1438 | * Required ECC: 4-bit correction per 512 bytes |
| 1439 | * Select: 16-bit correction per 2048 bytes |
| 1440 | */ |
Rodolfo Giometti | 3db227b | 2014-01-13 15:35:38 +0100 | [diff] [blame] | 1441 | } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) { |
| 1442 | info->ecc_bch = 1; |
| 1443 | info->chunk_size = 2048; |
| 1444 | info->spare_size = 32; |
| 1445 | info->ecc_size = 32; |
| 1446 | ecc->mode = NAND_ECC_HW; |
| 1447 | ecc->size = info->chunk_size; |
| 1448 | ecc->layout = &ecc_layout_2KB_bch4bit; |
| 1449 | ecc->strength = 16; |
Rodolfo Giometti | 3db227b | 2014-01-13 15:35:38 +0100 | [diff] [blame] | 1450 | |
Ezequiel Garcia | 30b2afc | 2013-12-18 18:44:10 -0300 | [diff] [blame] | 1451 | } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1452 | info->ecc_bch = 1; |
| 1453 | info->chunk_size = 2048; |
| 1454 | info->spare_size = 32; |
| 1455 | info->ecc_size = 32; |
| 1456 | ecc->mode = NAND_ECC_HW; |
| 1457 | ecc->size = info->chunk_size; |
| 1458 | ecc->layout = &ecc_layout_4KB_bch4bit; |
| 1459 | ecc->strength = 16; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1460 | |
Brian Norris | 6033a94 | 2013-11-14 14:41:32 -0800 | [diff] [blame] | 1461 | /* |
| 1462 | * Required ECC: 8-bit correction per 512 bytes |
| 1463 | * Select: 16-bit correction per 1024 bytes |
| 1464 | */ |
| 1465 | } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1466 | info->ecc_bch = 1; |
| 1467 | info->chunk_size = 1024; |
| 1468 | info->spare_size = 0; |
| 1469 | info->ecc_size = 32; |
| 1470 | ecc->mode = NAND_ECC_HW; |
| 1471 | ecc->size = info->chunk_size; |
| 1472 | ecc->layout = &ecc_layout_4KB_bch8bit; |
| 1473 | ecc->strength = 16; |
Ezequiel Garcia | eee0166 | 2014-05-14 14:58:07 -0300 | [diff] [blame] | 1474 | } else { |
| 1475 | dev_err(&info->pdev->dev, |
| 1476 | "ECC strength %d at page size %d is not supported\n", |
| 1477 | strength, page_size); |
| 1478 | return -ENODEV; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1479 | } |
Ezequiel Garcia | eee0166 | 2014-05-14 14:58:07 -0300 | [diff] [blame] | 1480 | |
| 1481 | dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n", |
| 1482 | ecc->strength, ecc->size); |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1483 | return 0; |
| 1484 | } |
| 1485 | |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1486 | static int pxa3xx_nand_scan(struct mtd_info *mtd) |
| 1487 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1488 | struct pxa3xx_nand_host *host = mtd->priv; |
| 1489 | struct pxa3xx_nand_info *info = host->info_data; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1490 | struct platform_device *pdev = info->pdev; |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1491 | struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Lei Wen | 0fab028 | 2011-06-07 03:01:06 -0700 | [diff] [blame] | 1492 | struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1493 | const struct pxa3xx_nand_flash *f = NULL; |
| 1494 | struct nand_chip *chip = mtd->priv; |
| 1495 | uint32_t id = -1; |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1496 | uint64_t chipsize; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1497 | int i, ret, num; |
Ezequiel Garcia | 30b2afc | 2013-12-18 18:44:10 -0300 | [diff] [blame] | 1498 | uint16_t ecc_strength, ecc_step; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1499 | |
| 1500 | if (pdata->keep_config && !pxa3xx_nand_detect_config(info)) |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1501 | goto KEEP_CONFIG; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1502 | |
Antoine Ténart | bc3e00f | 2015-08-18 10:59:10 +0200 | [diff] [blame] | 1503 | /* Set a default chunk size */ |
| 1504 | info->chunk_size = 512; |
| 1505 | |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1506 | ret = pxa3xx_nand_sensing(info); |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1507 | if (ret) { |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1508 | dev_info(&info->pdev->dev, "There is no chip on cs %d!\n", |
| 1509 | info->cs); |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1510 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1511 | return ret; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0); |
| 1515 | id = *((uint16_t *)(info->data_buff)); |
| 1516 | if (id != 0) |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1517 | dev_info(&info->pdev->dev, "Detect a flash id %x\n", id); |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1518 | else { |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1519 | dev_warn(&info->pdev->dev, |
| 1520 | "Read out ID 0, potential timing set wrong!!\n"); |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1521 | |
| 1522 | return -EINVAL; |
| 1523 | } |
| 1524 | |
Ezequiel García | a9cadf7 | 2015-08-21 15:47:28 -0300 | [diff] [blame] | 1525 | num = ARRAY_SIZE(builtin_flash_types) - 1; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1526 | for (i = 0; i < num; i++) { |
Ezequiel García | a9cadf7 | 2015-08-21 15:47:28 -0300 | [diff] [blame] | 1527 | f = &builtin_flash_types[i + 1]; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1528 | |
| 1529 | /* find the chip in default list */ |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1530 | if (f->chip_id == id) |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1531 | break; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1532 | } |
| 1533 | |
Ezequiel García | a9cadf7 | 2015-08-21 15:47:28 -0300 | [diff] [blame] | 1534 | if (i >= (ARRAY_SIZE(builtin_flash_types) - 1)) { |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1535 | dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n"); |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1536 | |
| 1537 | return -EINVAL; |
| 1538 | } |
| 1539 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1540 | ret = pxa3xx_nand_config_flash(info, f); |
| 1541 | if (ret) { |
| 1542 | dev_err(&info->pdev->dev, "ERROR! Configure failed\n"); |
| 1543 | return ret; |
| 1544 | } |
| 1545 | |
Antoine Ténart | 7c2f717 | 2015-02-12 15:53:27 +0100 | [diff] [blame] | 1546 | memset(pxa3xx_flash_ids, 0, sizeof(pxa3xx_flash_ids)); |
| 1547 | |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1548 | pxa3xx_flash_ids[0].name = f->name; |
Artem Bityutskiy | 68aa352de | 2013-03-04 16:05:00 +0200 | [diff] [blame] | 1549 | pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff; |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1550 | pxa3xx_flash_ids[0].pagesize = f->page_size; |
| 1551 | chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size; |
| 1552 | pxa3xx_flash_ids[0].chipsize = chipsize >> 20; |
| 1553 | pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block; |
| 1554 | if (f->flash_width == 16) |
| 1555 | pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16; |
Lei Wen | 0fab028 | 2011-06-07 03:01:06 -0700 | [diff] [blame] | 1556 | pxa3xx_flash_ids[1].name = NULL; |
| 1557 | def = pxa3xx_flash_ids; |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1558 | KEEP_CONFIG: |
Robert Jarzmik | e971aff | 2015-09-28 22:56:51 +0200 | [diff] [blame] | 1559 | info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0; |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 1560 | if (info->reg_ndcr & NDCR_DWIDTH_M) |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1561 | chip->options |= NAND_BUSWIDTH_16; |
| 1562 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1563 | /* Device detection must be done with ECC disabled */ |
| 1564 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) |
| 1565 | nand_writel(info, NDECCCTRL, 0x0); |
| 1566 | |
Lei Wen | 0fab028 | 2011-06-07 03:01:06 -0700 | [diff] [blame] | 1567 | if (nand_scan_ident(mtd, 1, def)) |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1568 | return -ENODEV; |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 1569 | |
| 1570 | if (pdata->flash_bbt) { |
| 1571 | /* |
| 1572 | * We'll use a bad block table stored in-flash and don't |
| 1573 | * allow writing the bad block marker to the flash. |
| 1574 | */ |
| 1575 | chip->bbt_options |= NAND_BBT_USE_FLASH | |
| 1576 | NAND_BBT_NO_OOB_BBM; |
| 1577 | chip->bbt_td = &bbt_main_descr; |
| 1578 | chip->bbt_md = &bbt_mirror_descr; |
| 1579 | } |
| 1580 | |
Ezequiel Garcia | 5cbbdc6 | 2013-12-18 18:44:09 -0300 | [diff] [blame] | 1581 | /* |
| 1582 | * If the page size is bigger than the FIFO size, let's check |
| 1583 | * we are given the right variant and then switch to the extended |
| 1584 | * (aka splitted) command handling, |
| 1585 | */ |
| 1586 | if (mtd->writesize > PAGE_CHUNK_SIZE) { |
| 1587 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) { |
| 1588 | chip->cmdfunc = nand_cmdfunc_extended; |
| 1589 | } else { |
| 1590 | dev_err(&info->pdev->dev, |
| 1591 | "unsupported page size on this variant\n"); |
| 1592 | return -ENODEV; |
| 1593 | } |
| 1594 | } |
| 1595 | |
Ezequiel Garcia | 5b3e507 | 2014-05-14 14:58:08 -0300 | [diff] [blame] | 1596 | if (pdata->ecc_strength && pdata->ecc_step_size) { |
| 1597 | ecc_strength = pdata->ecc_strength; |
| 1598 | ecc_step = pdata->ecc_step_size; |
| 1599 | } else { |
| 1600 | ecc_strength = chip->ecc_strength_ds; |
| 1601 | ecc_step = chip->ecc_step_ds; |
| 1602 | } |
Ezequiel Garcia | 30b2afc | 2013-12-18 18:44:10 -0300 | [diff] [blame] | 1603 | |
| 1604 | /* Set default ECC strength requirements on non-ONFI devices */ |
| 1605 | if (ecc_strength < 1 && ecc_step < 1) { |
| 1606 | ecc_strength = 1; |
| 1607 | ecc_step = 512; |
| 1608 | } |
| 1609 | |
| 1610 | ret = pxa_ecc_init(info, &chip->ecc, ecc_strength, |
| 1611 | ecc_step, mtd->writesize); |
Ezequiel Garcia | eee0166 | 2014-05-14 14:58:07 -0300 | [diff] [blame] | 1612 | if (ret) |
| 1613 | return ret; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1614 | |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1615 | /* calculate addressing information */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1616 | if (mtd->writesize >= 2048) |
| 1617 | host->col_addr_cycles = 2; |
| 1618 | else |
| 1619 | host->col_addr_cycles = 1; |
| 1620 | |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1621 | /* release the initial buffer */ |
| 1622 | kfree(info->data_buff); |
| 1623 | |
| 1624 | /* allocate the real data + oob buffer */ |
| 1625 | info->buf_size = mtd->writesize + mtd->oobsize; |
| 1626 | ret = pxa3xx_nand_init_buff(info); |
| 1627 | if (ret) |
| 1628 | return ret; |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1629 | info->oob_buff = info->data_buff + mtd->writesize; |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1630 | |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1631 | if ((mtd->size >> chip->page_shift) > 65536) |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1632 | host->row_addr_cycles = 3; |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1633 | else |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1634 | host->row_addr_cycles = 2; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1635 | return nand_scan_tail(mtd); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1636 | } |
| 1637 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1638 | static int alloc_nand_resource(struct platform_device *pdev) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1639 | { |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1640 | struct pxa3xx_nand_platform_data *pdata; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1641 | struct pxa3xx_nand_info *info; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1642 | struct pxa3xx_nand_host *host; |
Haojian Zhuang | 6e308f8 | 2012-08-20 13:40:31 +0800 | [diff] [blame] | 1643 | struct nand_chip *chip = NULL; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1644 | struct mtd_info *mtd; |
| 1645 | struct resource *r; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1646 | int ret, irq, cs; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1647 | |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1648 | pdata = dev_get_platdata(&pdev->dev); |
Robert Jarzmik | e423c90 | 2015-02-08 21:02:09 +0100 | [diff] [blame] | 1649 | if (pdata->num_cs <= 0) |
| 1650 | return -ENODEV; |
Ezequiel Garcia | 4c073cd | 2013-04-17 13:38:09 -0300 | [diff] [blame] | 1651 | info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) + |
| 1652 | sizeof(*host)) * pdata->num_cs, GFP_KERNEL); |
| 1653 | if (!info) |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1654 | return -ENOMEM; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1655 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1656 | info->pdev = pdev; |
Ezequiel Garcia | c7e9c7e | 2013-11-07 12:17:14 -0300 | [diff] [blame] | 1657 | info->variant = pxa3xx_nand_get_variant(pdev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1658 | for (cs = 0; cs < pdata->num_cs; cs++) { |
Rob Herring | ce914e6 | 2015-04-30 15:17:47 -0500 | [diff] [blame] | 1659 | mtd = (void *)&info[1] + (sizeof(*mtd) + sizeof(*host)) * cs; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1660 | chip = (struct nand_chip *)(&mtd[1]); |
| 1661 | host = (struct pxa3xx_nand_host *)chip; |
| 1662 | info->host[cs] = host; |
| 1663 | host->mtd = mtd; |
| 1664 | host->cs = cs; |
| 1665 | host->info_data = info; |
| 1666 | mtd->priv = host; |
Frans Klaver | 550dab5 | 2015-06-10 22:39:01 +0200 | [diff] [blame] | 1667 | mtd->dev.parent = &pdev->dev; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1668 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1669 | chip->ecc.read_page = pxa3xx_nand_read_page_hwecc; |
| 1670 | chip->ecc.write_page = pxa3xx_nand_write_page_hwecc; |
| 1671 | chip->controller = &info->controller; |
| 1672 | chip->waitfunc = pxa3xx_nand_waitfunc; |
| 1673 | chip->select_chip = pxa3xx_nand_select_chip; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1674 | chip->read_word = pxa3xx_nand_read_word; |
| 1675 | chip->read_byte = pxa3xx_nand_read_byte; |
| 1676 | chip->read_buf = pxa3xx_nand_read_buf; |
| 1677 | chip->write_buf = pxa3xx_nand_write_buf; |
Ezequiel Garcia | 664c7f5 | 2013-11-07 12:17:12 -0300 | [diff] [blame] | 1678 | chip->options |= NAND_NO_SUBPAGE_WRITE; |
Ezequiel Garcia | 5cbbdc6 | 2013-12-18 18:44:09 -0300 | [diff] [blame] | 1679 | chip->cmdfunc = nand_cmdfunc; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1680 | } |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1681 | |
| 1682 | spin_lock_init(&chip->controller->lock); |
| 1683 | init_waitqueue_head(&chip->controller->wq); |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1684 | info->clk = devm_clk_get(&pdev->dev, NULL); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1685 | if (IS_ERR(info->clk)) { |
| 1686 | dev_err(&pdev->dev, "failed to get nand clock\n"); |
Ezequiel Garcia | 4c073cd | 2013-04-17 13:38:09 -0300 | [diff] [blame] | 1687 | return PTR_ERR(info->clk); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1688 | } |
Ezequiel Garcia | 1f8eaff | 2013-04-17 13:38:13 -0300 | [diff] [blame] | 1689 | ret = clk_prepare_enable(info->clk); |
| 1690 | if (ret < 0) |
| 1691 | return ret; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1692 | |
Ezequiel Garcia | 6b45c1e | 2013-08-12 14:14:58 -0300 | [diff] [blame] | 1693 | if (use_dma) { |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1694 | r = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 1695 | if (r == NULL) { |
| 1696 | dev_err(&pdev->dev, |
| 1697 | "no resource defined for data DMA\n"); |
| 1698 | ret = -ENXIO; |
| 1699 | goto fail_disable_clk; |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1700 | } |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1701 | info->drcmr_dat = r->start; |
| 1702 | |
| 1703 | r = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 1704 | if (r == NULL) { |
| 1705 | dev_err(&pdev->dev, |
| 1706 | "no resource defined for cmd DMA\n"); |
| 1707 | ret = -ENXIO; |
| 1708 | goto fail_disable_clk; |
| 1709 | } |
| 1710 | info->drcmr_cmd = r->start; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1711 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1712 | |
| 1713 | irq = platform_get_irq(pdev, 0); |
| 1714 | if (irq < 0) { |
| 1715 | dev_err(&pdev->dev, "no IRQ resource defined\n"); |
| 1716 | ret = -ENXIO; |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1717 | goto fail_disable_clk; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Ezequiel Garcia | 0ddd846 | 2013-04-17 13:38:10 -0300 | [diff] [blame] | 1721 | info->mmio_base = devm_ioremap_resource(&pdev->dev, r); |
| 1722 | if (IS_ERR(info->mmio_base)) { |
| 1723 | ret = PTR_ERR(info->mmio_base); |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1724 | goto fail_disable_clk; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1725 | } |
Haojian Zhuang | 8638fac | 2009-09-10 14:11:44 +0800 | [diff] [blame] | 1726 | info->mmio_phys = r->start; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1727 | |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1728 | /* Allocate a buffer to allow flash detection */ |
| 1729 | info->buf_size = INIT_BUFFER_SIZE; |
| 1730 | info->data_buff = kmalloc(info->buf_size, GFP_KERNEL); |
| 1731 | if (info->data_buff == NULL) { |
| 1732 | ret = -ENOMEM; |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1733 | goto fail_disable_clk; |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1734 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1735 | |
Haojian Zhuang | 346e125 | 2009-09-10 14:27:23 +0800 | [diff] [blame] | 1736 | /* initialize all interrupts to be disabled */ |
| 1737 | disable_int(info, NDSR_MASK); |
| 1738 | |
Robert Jarzmik | 2454225 | 2015-02-20 19:36:43 +0100 | [diff] [blame] | 1739 | ret = request_threaded_irq(irq, pxa3xx_nand_irq, |
| 1740 | pxa3xx_nand_irq_thread, IRQF_ONESHOT, |
| 1741 | pdev->name, info); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1742 | if (ret < 0) { |
| 1743 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
| 1744 | goto fail_free_buf; |
| 1745 | } |
| 1746 | |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1747 | platform_set_drvdata(pdev, info); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1748 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1749 | return 0; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1750 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1751 | fail_free_buf: |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1752 | free_irq(irq, info); |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1753 | kfree(info->data_buff); |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1754 | fail_disable_clk: |
Ezequiel Garcia | fb32061 | 2013-04-17 13:38:12 -0300 | [diff] [blame] | 1755 | clk_disable_unprepare(info->clk); |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1756 | return ret; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | static int pxa3xx_nand_remove(struct platform_device *pdev) |
| 1760 | { |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1761 | struct pxa3xx_nand_info *info = platform_get_drvdata(pdev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1762 | struct pxa3xx_nand_platform_data *pdata; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1763 | int irq, cs; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1764 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1765 | if (!info) |
| 1766 | return 0; |
| 1767 | |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1768 | pdata = dev_get_platdata(&pdev->dev); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1769 | |
Haojian Zhuang | dbf5986 | 2009-09-10 14:22:55 +0800 | [diff] [blame] | 1770 | irq = platform_get_irq(pdev, 0); |
| 1771 | if (irq >= 0) |
| 1772 | free_irq(irq, info); |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1773 | pxa3xx_nand_free_buff(info); |
Mike Rapoport | 82a72d1 | 2009-02-17 13:54:46 +0200 | [diff] [blame] | 1774 | |
Robert Jarzmik | e971aff | 2015-09-28 22:56:51 +0200 | [diff] [blame] | 1775 | /* |
| 1776 | * In the pxa3xx case, the DFI bus is shared between the SMC and NFC. |
| 1777 | * In order to prevent a lockup of the system bus, the DFI bus |
| 1778 | * arbitration is granted to SMC upon driver removal. This is done by |
| 1779 | * setting the x_ARB_CNTL bit, which also prevents the NAND to have |
| 1780 | * access to the bus anymore. |
| 1781 | */ |
| 1782 | nand_writel(info, NDCR, |
| 1783 | (nand_readl(info, NDCR) & ~NDCR_ND_ARB_EN) | |
| 1784 | NFCV1_NDCR_ARB_CNTL); |
Ezequiel Garcia | fb32061 | 2013-04-17 13:38:12 -0300 | [diff] [blame] | 1785 | clk_disable_unprepare(info->clk); |
Mike Rapoport | 82a72d1 | 2009-02-17 13:54:46 +0200 | [diff] [blame] | 1786 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1787 | for (cs = 0; cs < pdata->num_cs; cs++) |
| 1788 | nand_release(info->host[cs]->mtd); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1789 | return 0; |
| 1790 | } |
| 1791 | |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1792 | static int pxa3xx_nand_probe_dt(struct platform_device *pdev) |
| 1793 | { |
| 1794 | struct pxa3xx_nand_platform_data *pdata; |
| 1795 | struct device_node *np = pdev->dev.of_node; |
| 1796 | const struct of_device_id *of_id = |
| 1797 | of_match_device(pxa3xx_nand_dt_ids, &pdev->dev); |
| 1798 | |
| 1799 | if (!of_id) |
| 1800 | return 0; |
| 1801 | |
| 1802 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 1803 | if (!pdata) |
| 1804 | return -ENOMEM; |
| 1805 | |
| 1806 | if (of_get_property(np, "marvell,nand-enable-arbiter", NULL)) |
| 1807 | pdata->enable_arbiter = 1; |
| 1808 | if (of_get_property(np, "marvell,nand-keep-config", NULL)) |
| 1809 | pdata->keep_config = 1; |
| 1810 | of_property_read_u32(np, "num-cs", &pdata->num_cs); |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 1811 | pdata->flash_bbt = of_get_nand_on_flash_bbt(np); |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1812 | |
Ezequiel Garcia | 5b3e507 | 2014-05-14 14:58:08 -0300 | [diff] [blame] | 1813 | pdata->ecc_strength = of_get_nand_ecc_strength(np); |
| 1814 | if (pdata->ecc_strength < 0) |
| 1815 | pdata->ecc_strength = 0; |
| 1816 | |
| 1817 | pdata->ecc_step_size = of_get_nand_ecc_step_size(np); |
| 1818 | if (pdata->ecc_step_size < 0) |
| 1819 | pdata->ecc_step_size = 0; |
| 1820 | |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1821 | pdev->dev.platform_data = pdata; |
| 1822 | |
| 1823 | return 0; |
| 1824 | } |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1825 | |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1826 | static int pxa3xx_nand_probe(struct platform_device *pdev) |
| 1827 | { |
| 1828 | struct pxa3xx_nand_platform_data *pdata; |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1829 | struct mtd_part_parser_data ppdata = {}; |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1830 | struct pxa3xx_nand_info *info; |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1831 | int ret, cs, probe_success, dma_available; |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1832 | |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1833 | dma_available = IS_ENABLED(CONFIG_ARM) && |
| 1834 | (IS_ENABLED(CONFIG_ARCH_PXA) || IS_ENABLED(CONFIG_ARCH_MMP)); |
| 1835 | if (use_dma && !dma_available) { |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 1836 | use_dma = 0; |
| 1837 | dev_warn(&pdev->dev, |
| 1838 | "This platform can't do DMA on this device\n"); |
| 1839 | } |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1840 | |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1841 | ret = pxa3xx_nand_probe_dt(pdev); |
| 1842 | if (ret) |
| 1843 | return ret; |
| 1844 | |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1845 | pdata = dev_get_platdata(&pdev->dev); |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1846 | if (!pdata) { |
| 1847 | dev_err(&pdev->dev, "no platform data defined\n"); |
| 1848 | return -ENODEV; |
| 1849 | } |
| 1850 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1851 | ret = alloc_nand_resource(pdev); |
| 1852 | if (ret) { |
| 1853 | dev_err(&pdev->dev, "alloc nand resource failed\n"); |
| 1854 | return ret; |
| 1855 | } |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1856 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1857 | info = platform_get_drvdata(pdev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1858 | probe_success = 0; |
| 1859 | for (cs = 0; cs < pdata->num_cs; cs++) { |
Ezequiel Garcia | b7655bc | 2013-08-12 14:14:52 -0300 | [diff] [blame] | 1860 | struct mtd_info *mtd = info->host[cs]->mtd; |
Ezequiel Garcia | f455578 | 2013-08-12 14:14:53 -0300 | [diff] [blame] | 1861 | |
Ezequiel Garcia | 18a84e9 | 2013-10-19 18:19:25 -0300 | [diff] [blame] | 1862 | /* |
| 1863 | * The mtd name matches the one used in 'mtdparts' kernel |
| 1864 | * parameter. This name cannot be changed or otherwise |
| 1865 | * user's mtd partitions configuration would get broken. |
| 1866 | */ |
| 1867 | mtd->name = "pxa3xx_nand-0"; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1868 | info->cs = cs; |
Ezequiel Garcia | b7655bc | 2013-08-12 14:14:52 -0300 | [diff] [blame] | 1869 | ret = pxa3xx_nand_scan(mtd); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1870 | if (ret) { |
| 1871 | dev_warn(&pdev->dev, "failed to scan nand at cs %d\n", |
| 1872 | cs); |
| 1873 | continue; |
| 1874 | } |
| 1875 | |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1876 | ppdata.of_node = pdev->dev.of_node; |
Ezequiel Garcia | b7655bc | 2013-08-12 14:14:52 -0300 | [diff] [blame] | 1877 | ret = mtd_device_parse_register(mtd, NULL, |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1878 | &ppdata, pdata->parts[cs], |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 1879 | pdata->nr_parts[cs]); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1880 | if (!ret) |
| 1881 | probe_success = 1; |
| 1882 | } |
| 1883 | |
| 1884 | if (!probe_success) { |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1885 | pxa3xx_nand_remove(pdev); |
| 1886 | return -ENODEV; |
| 1887 | } |
| 1888 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1889 | return 0; |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1890 | } |
| 1891 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1892 | #ifdef CONFIG_PM |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1893 | static int pxa3xx_nand_suspend(struct device *dev) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1894 | { |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1895 | struct pxa3xx_nand_info *info = dev_get_drvdata(dev); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1896 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1897 | if (info->state) { |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1898 | dev_err(dev, "driver busy, state = %d\n", info->state); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1899 | return -EAGAIN; |
| 1900 | } |
| 1901 | |
| 1902 | return 0; |
| 1903 | } |
| 1904 | |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1905 | static int pxa3xx_nand_resume(struct device *dev) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1906 | { |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1907 | struct pxa3xx_nand_info *info = dev_get_drvdata(dev); |
Lei Wen | 051fc41 | 2011-07-14 20:44:30 -0700 | [diff] [blame] | 1908 | |
| 1909 | /* We don't want to handle interrupt without calling mtd routine */ |
| 1910 | disable_int(info, NDCR_INT_MASK); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1911 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1912 | /* |
| 1913 | * Directly set the chip select to a invalid value, |
| 1914 | * then the driver would reset the timing according |
| 1915 | * to current chip select at the beginning of cmdfunc |
| 1916 | */ |
| 1917 | info->cs = 0xff; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1918 | |
Lei Wen | 051fc41 | 2011-07-14 20:44:30 -0700 | [diff] [blame] | 1919 | /* |
| 1920 | * As the spec says, the NDSR would be updated to 0x1800 when |
| 1921 | * doing the nand_clk disable/enable. |
| 1922 | * To prevent it damaging state machine of the driver, clear |
| 1923 | * all status before resume |
| 1924 | */ |
| 1925 | nand_writel(info, NDSR, NDSR_MASK); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1926 | |
Lei Wen | 18c81b1 | 2010-08-17 17:25:57 +0800 | [diff] [blame] | 1927 | return 0; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1928 | } |
| 1929 | #else |
| 1930 | #define pxa3xx_nand_suspend NULL |
| 1931 | #define pxa3xx_nand_resume NULL |
| 1932 | #endif |
| 1933 | |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1934 | static const struct dev_pm_ops pxa3xx_nand_pm_ops = { |
| 1935 | .suspend = pxa3xx_nand_suspend, |
| 1936 | .resume = pxa3xx_nand_resume, |
| 1937 | }; |
| 1938 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1939 | static struct platform_driver pxa3xx_nand_driver = { |
| 1940 | .driver = { |
| 1941 | .name = "pxa3xx-nand", |
Sachin Kamat | 5576bc7 | 2013-09-30 15:10:24 +0530 | [diff] [blame] | 1942 | .of_match_table = pxa3xx_nand_dt_ids, |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1943 | .pm = &pxa3xx_nand_pm_ops, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1944 | }, |
| 1945 | .probe = pxa3xx_nand_probe, |
| 1946 | .remove = pxa3xx_nand_remove, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1947 | }; |
| 1948 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 1949 | module_platform_driver(pxa3xx_nand_driver); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1950 | |
| 1951 | MODULE_LICENSE("GPL"); |
| 1952 | MODULE_DESCRIPTION("PXA3xx NAND controller driver"); |