Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * NAND Flash Controller Device Driver |
| 3 | * Copyright © 2009-2010, Intel Corporation and its suppliers. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/wait.h> |
| 23 | #include <linux/mutex.h> |
David Miller | b8664b3 | 2010-08-04 22:57:51 -0700 | [diff] [blame] | 24 | #include <linux/slab.h> |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 25 | #include <linux/pci.h> |
| 26 | #include <linux/mtd/mtd.h> |
| 27 | #include <linux/module.h> |
| 28 | |
| 29 | #include "denali.h" |
| 30 | |
| 31 | MODULE_LICENSE("GPL"); |
| 32 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 33 | /* We define a module parameter that allows the user to override |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 34 | * the hardware and decide what timing mode should be used. |
| 35 | */ |
| 36 | #define NAND_DEFAULT_TIMINGS -1 |
| 37 | |
| 38 | static int onfi_timing_mode = NAND_DEFAULT_TIMINGS; |
| 39 | module_param(onfi_timing_mode, int, S_IRUGO); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 40 | MODULE_PARM_DESC(onfi_timing_mode, "Overrides default ONFI setting." |
| 41 | " -1 indicates use default timings"); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 42 | |
| 43 | #define DENALI_NAND_NAME "denali-nand" |
| 44 | |
| 45 | /* We define a macro here that combines all interrupts this driver uses into |
| 46 | * a single constant value, for convenience. */ |
| 47 | #define DENALI_IRQ_ALL (INTR_STATUS0__DMA_CMD_COMP | \ |
| 48 | INTR_STATUS0__ECC_TRANSACTION_DONE | \ |
| 49 | INTR_STATUS0__ECC_ERR | \ |
| 50 | INTR_STATUS0__PROGRAM_FAIL | \ |
| 51 | INTR_STATUS0__LOAD_COMP | \ |
| 52 | INTR_STATUS0__PROGRAM_COMP | \ |
| 53 | INTR_STATUS0__TIME_OUT | \ |
| 54 | INTR_STATUS0__ERASE_FAIL | \ |
| 55 | INTR_STATUS0__RST_COMP | \ |
| 56 | INTR_STATUS0__ERASE_COMP) |
| 57 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 58 | /* indicates whether or not the internal value for the flash bank is |
Chuanxiao Dong | b292c34 | 2010-08-11 17:46:00 +0800 | [diff] [blame] | 59 | * valid or not */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 60 | #define CHIP_SELECT_INVALID -1 |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 61 | |
| 62 | #define SUPPORT_8BITECC 1 |
| 63 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 64 | /* This macro divides two integers and rounds fractional values up |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 65 | * to the nearest integer value. */ |
| 66 | #define CEIL_DIV(X, Y) (((X)%(Y)) ? ((X)/(Y)+1) : ((X)/(Y))) |
| 67 | |
| 68 | /* this macro allows us to convert from an MTD structure to our own |
| 69 | * device context (denali) structure. |
| 70 | */ |
| 71 | #define mtd_to_denali(m) container_of(m, struct denali_nand_info, mtd) |
| 72 | |
| 73 | /* These constants are defined by the driver to enable common driver |
Chuanxiao Dong | b292c34 | 2010-08-11 17:46:00 +0800 | [diff] [blame] | 74 | * configuration options. */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 75 | #define SPARE_ACCESS 0x41 |
| 76 | #define MAIN_ACCESS 0x42 |
| 77 | #define MAIN_SPARE_ACCESS 0x43 |
| 78 | |
| 79 | #define DENALI_READ 0 |
| 80 | #define DENALI_WRITE 0x100 |
| 81 | |
| 82 | /* types of device accesses. We can issue commands and get status */ |
| 83 | #define COMMAND_CYCLE 0 |
| 84 | #define ADDR_CYCLE 1 |
| 85 | #define STATUS_CYCLE 2 |
| 86 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 87 | /* this is a helper macro that allows us to |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 88 | * format the bank into the proper bits for the controller */ |
| 89 | #define BANK(x) ((x) << 24) |
| 90 | |
| 91 | /* List of platforms this NAND controller has be integrated into */ |
| 92 | static const struct pci_device_id denali_pci_ids[] = { |
| 93 | { PCI_VDEVICE(INTEL, 0x0701), INTEL_CE4100 }, |
| 94 | { PCI_VDEVICE(INTEL, 0x0809), INTEL_MRST }, |
| 95 | { /* end: all zeroes */ } |
| 96 | }; |
| 97 | |
| 98 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 99 | /* these are static lookup tables that give us easy access to |
Chuanxiao Dong | b292c34 | 2010-08-11 17:46:00 +0800 | [diff] [blame] | 100 | * registers in the NAND controller. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 101 | */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 102 | static const uint32_t intr_status_addresses[4] = {INTR_STATUS0, |
| 103 | INTR_STATUS1, |
| 104 | INTR_STATUS2, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 105 | INTR_STATUS3}; |
| 106 | |
| 107 | static const uint32_t device_reset_banks[4] = {DEVICE_RESET__BANK0, |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 108 | DEVICE_RESET__BANK1, |
| 109 | DEVICE_RESET__BANK2, |
| 110 | DEVICE_RESET__BANK3}; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 111 | |
| 112 | static const uint32_t operation_timeout[4] = {INTR_STATUS0__TIME_OUT, |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 113 | INTR_STATUS1__TIME_OUT, |
| 114 | INTR_STATUS2__TIME_OUT, |
| 115 | INTR_STATUS3__TIME_OUT}; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 116 | |
| 117 | static const uint32_t reset_complete[4] = {INTR_STATUS0__RST_COMP, |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 118 | INTR_STATUS1__RST_COMP, |
| 119 | INTR_STATUS2__RST_COMP, |
| 120 | INTR_STATUS3__RST_COMP}; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 121 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 122 | /* forward declarations */ |
| 123 | static void clear_interrupts(struct denali_nand_info *denali); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 124 | static uint32_t wait_for_irq(struct denali_nand_info *denali, |
| 125 | uint32_t irq_mask); |
| 126 | static void denali_irq_enable(struct denali_nand_info *denali, |
| 127 | uint32_t int_mask); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 128 | static uint32_t read_interrupt_status(struct denali_nand_info *denali); |
| 129 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 130 | /* Certain operations for the denali NAND controller use |
| 131 | * an indexed mode to read/write data. The operation is |
| 132 | * performed by writing the address value of the command |
| 133 | * to the device memory followed by the data. This function |
| 134 | * abstracts this common operation. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 135 | */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 136 | static void index_addr(struct denali_nand_info *denali, |
| 137 | uint32_t address, uint32_t data) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 138 | { |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 139 | iowrite32(address, denali->flash_mem); |
| 140 | iowrite32(data, denali->flash_mem + 0x10); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* Perform an indexed read of the device */ |
| 144 | static void index_addr_read_data(struct denali_nand_info *denali, |
| 145 | uint32_t address, uint32_t *pdata) |
| 146 | { |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 147 | iowrite32(address, denali->flash_mem); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 148 | *pdata = ioread32(denali->flash_mem + 0x10); |
| 149 | } |
| 150 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 151 | /* We need to buffer some data for some of the NAND core routines. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 152 | * The operations manage buffering that data. */ |
| 153 | static void reset_buf(struct denali_nand_info *denali) |
| 154 | { |
| 155 | denali->buf.head = denali->buf.tail = 0; |
| 156 | } |
| 157 | |
| 158 | static void write_byte_to_buf(struct denali_nand_info *denali, uint8_t byte) |
| 159 | { |
| 160 | BUG_ON(denali->buf.tail >= sizeof(denali->buf.buf)); |
| 161 | denali->buf.buf[denali->buf.tail++] = byte; |
| 162 | } |
| 163 | |
| 164 | /* reads the status of the device */ |
| 165 | static void read_status(struct denali_nand_info *denali) |
| 166 | { |
| 167 | uint32_t cmd = 0x0; |
| 168 | |
| 169 | /* initialize the data buffer to store status */ |
| 170 | reset_buf(denali); |
| 171 | |
Chuanxiao Dong | f0bc0c7 | 2010-08-11 17:14:59 +0800 | [diff] [blame] | 172 | cmd = ioread32(denali->flash_reg + WRITE_PROTECT); |
| 173 | if (cmd) |
| 174 | write_byte_to_buf(denali, NAND_STATUS_WP); |
| 175 | else |
| 176 | write_byte_to_buf(denali, 0); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* resets a specific device connected to the core */ |
| 180 | static void reset_bank(struct denali_nand_info *denali) |
| 181 | { |
| 182 | uint32_t irq_status = 0; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 183 | uint32_t irq_mask = reset_complete[denali->flash_bank] | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 184 | operation_timeout[denali->flash_bank]; |
| 185 | int bank = 0; |
| 186 | |
| 187 | clear_interrupts(denali); |
| 188 | |
| 189 | bank = device_reset_banks[denali->flash_bank]; |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 190 | iowrite32(bank, denali->flash_reg + DEVICE_RESET); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 191 | |
| 192 | irq_status = wait_for_irq(denali, irq_mask); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 193 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 194 | if (irq_status & operation_timeout[denali->flash_bank]) |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 195 | dev_err(&denali->dev->dev, "reset bank failed.\n"); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* Reset the flash controller */ |
Chuanxiao Dong | eda936e | 2010-07-27 14:17:37 +0800 | [diff] [blame] | 199 | static uint16_t denali_nand_reset(struct denali_nand_info *denali) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 200 | { |
| 201 | uint32_t i; |
| 202 | |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 203 | dev_dbg(&denali->dev->dev, "%s, Line %d, Function: %s\n", |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 204 | __FILE__, __LINE__, __func__); |
| 205 | |
| 206 | for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++) |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 207 | iowrite32(reset_complete[i] | operation_timeout[i], |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 208 | denali->flash_reg + intr_status_addresses[i]); |
| 209 | |
| 210 | for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++) { |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 211 | iowrite32(device_reset_banks[i], |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 212 | denali->flash_reg + DEVICE_RESET); |
| 213 | while (!(ioread32(denali->flash_reg + |
Chuanxiao Dong | 628bfd41 | 2010-08-11 17:53:29 +0800 | [diff] [blame] | 214 | intr_status_addresses[i]) & |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 215 | (reset_complete[i] | operation_timeout[i]))) |
Chuanxiao Dong | 628bfd41 | 2010-08-11 17:53:29 +0800 | [diff] [blame] | 216 | cpu_relax(); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 217 | if (ioread32(denali->flash_reg + intr_status_addresses[i]) & |
| 218 | operation_timeout[i]) |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 219 | dev_dbg(&denali->dev->dev, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 220 | "NAND Reset operation timed out on bank %d\n", i); |
| 221 | } |
| 222 | |
| 223 | for (i = 0; i < LLD_MAX_FLASH_BANKS; i++) |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 224 | iowrite32(reset_complete[i] | operation_timeout[i], |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 225 | denali->flash_reg + intr_status_addresses[i]); |
| 226 | |
| 227 | return PASS; |
| 228 | } |
| 229 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 230 | /* this routine calculates the ONFI timing values for a given mode and |
| 231 | * programs the clocking register accordingly. The mode is determined by |
| 232 | * the get_onfi_nand_para routine. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 233 | */ |
Chuanxiao Dong | eda936e | 2010-07-27 14:17:37 +0800 | [diff] [blame] | 234 | static void nand_onfi_timing_set(struct denali_nand_info *denali, |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 235 | uint16_t mode) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 236 | { |
| 237 | uint16_t Trea[6] = {40, 30, 25, 20, 20, 16}; |
| 238 | uint16_t Trp[6] = {50, 25, 17, 15, 12, 10}; |
| 239 | uint16_t Treh[6] = {30, 15, 15, 10, 10, 7}; |
| 240 | uint16_t Trc[6] = {100, 50, 35, 30, 25, 20}; |
| 241 | uint16_t Trhoh[6] = {0, 15, 15, 15, 15, 15}; |
| 242 | uint16_t Trloh[6] = {0, 0, 0, 0, 5, 5}; |
| 243 | uint16_t Tcea[6] = {100, 45, 30, 25, 25, 25}; |
| 244 | uint16_t Tadl[6] = {200, 100, 100, 100, 70, 70}; |
| 245 | uint16_t Trhw[6] = {200, 100, 100, 100, 100, 100}; |
| 246 | uint16_t Trhz[6] = {200, 100, 100, 100, 100, 100}; |
| 247 | uint16_t Twhr[6] = {120, 80, 80, 60, 60, 60}; |
| 248 | uint16_t Tcs[6] = {70, 35, 25, 25, 20, 15}; |
| 249 | |
| 250 | uint16_t TclsRising = 1; |
| 251 | uint16_t data_invalid_rhoh, data_invalid_rloh, data_invalid; |
| 252 | uint16_t dv_window = 0; |
| 253 | uint16_t en_lo, en_hi; |
| 254 | uint16_t acc_clks; |
| 255 | uint16_t addr_2_data, re_2_we, re_2_re, we_2_re, cs_cnt; |
| 256 | |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 257 | dev_dbg(&denali->dev->dev, "%s, Line %d, Function: %s\n", |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 258 | __FILE__, __LINE__, __func__); |
| 259 | |
| 260 | en_lo = CEIL_DIV(Trp[mode], CLK_X); |
| 261 | en_hi = CEIL_DIV(Treh[mode], CLK_X); |
| 262 | #if ONFI_BLOOM_TIME |
| 263 | if ((en_hi * CLK_X) < (Treh[mode] + 2)) |
| 264 | en_hi++; |
| 265 | #endif |
| 266 | |
| 267 | if ((en_lo + en_hi) * CLK_X < Trc[mode]) |
| 268 | en_lo += CEIL_DIV((Trc[mode] - (en_lo + en_hi) * CLK_X), CLK_X); |
| 269 | |
| 270 | if ((en_lo + en_hi) < CLK_MULTI) |
| 271 | en_lo += CLK_MULTI - en_lo - en_hi; |
| 272 | |
| 273 | while (dv_window < 8) { |
| 274 | data_invalid_rhoh = en_lo * CLK_X + Trhoh[mode]; |
| 275 | |
| 276 | data_invalid_rloh = (en_lo + en_hi) * CLK_X + Trloh[mode]; |
| 277 | |
| 278 | data_invalid = |
| 279 | data_invalid_rhoh < |
| 280 | data_invalid_rloh ? data_invalid_rhoh : data_invalid_rloh; |
| 281 | |
| 282 | dv_window = data_invalid - Trea[mode]; |
| 283 | |
| 284 | if (dv_window < 8) |
| 285 | en_lo++; |
| 286 | } |
| 287 | |
| 288 | acc_clks = CEIL_DIV(Trea[mode], CLK_X); |
| 289 | |
| 290 | while (((acc_clks * CLK_X) - Trea[mode]) < 3) |
| 291 | acc_clks++; |
| 292 | |
| 293 | if ((data_invalid - acc_clks * CLK_X) < 2) |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 294 | dev_warn(&denali->dev->dev, "%s, Line %d: Warning!\n", |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 295 | __FILE__, __LINE__); |
| 296 | |
| 297 | addr_2_data = CEIL_DIV(Tadl[mode], CLK_X); |
| 298 | re_2_we = CEIL_DIV(Trhw[mode], CLK_X); |
| 299 | re_2_re = CEIL_DIV(Trhz[mode], CLK_X); |
| 300 | we_2_re = CEIL_DIV(Twhr[mode], CLK_X); |
| 301 | cs_cnt = CEIL_DIV((Tcs[mode] - Trp[mode]), CLK_X); |
| 302 | if (!TclsRising) |
| 303 | cs_cnt = CEIL_DIV(Tcs[mode], CLK_X); |
| 304 | if (cs_cnt == 0) |
| 305 | cs_cnt = 1; |
| 306 | |
| 307 | if (Tcea[mode]) { |
| 308 | while (((cs_cnt * CLK_X) + Trea[mode]) < Tcea[mode]) |
| 309 | cs_cnt++; |
| 310 | } |
| 311 | |
| 312 | #if MODE5_WORKAROUND |
| 313 | if (mode == 5) |
| 314 | acc_clks = 5; |
| 315 | #endif |
| 316 | |
| 317 | /* Sighting 3462430: Temporary hack for MT29F128G08CJABAWP:B */ |
| 318 | if ((ioread32(denali->flash_reg + MANUFACTURER_ID) == 0) && |
| 319 | (ioread32(denali->flash_reg + DEVICE_ID) == 0x88)) |
| 320 | acc_clks = 6; |
| 321 | |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 322 | iowrite32(acc_clks, denali->flash_reg + ACC_CLKS); |
| 323 | iowrite32(re_2_we, denali->flash_reg + RE_2_WE); |
| 324 | iowrite32(re_2_re, denali->flash_reg + RE_2_RE); |
| 325 | iowrite32(we_2_re, denali->flash_reg + WE_2_RE); |
| 326 | iowrite32(addr_2_data, denali->flash_reg + ADDR_2_DATA); |
| 327 | iowrite32(en_lo, denali->flash_reg + RDWR_EN_LO_CNT); |
| 328 | iowrite32(en_hi, denali->flash_reg + RDWR_EN_HI_CNT); |
| 329 | iowrite32(cs_cnt, denali->flash_reg + CS_SETUP_CNT); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 332 | /* queries the NAND device to see what ONFI modes it supports. */ |
| 333 | static uint16_t get_onfi_nand_para(struct denali_nand_info *denali) |
| 334 | { |
| 335 | int i; |
Chuanxiao Dong | 4c03bbd | 2010-08-06 15:45:19 +0800 | [diff] [blame] | 336 | /* we needn't to do a reset here because driver has already |
| 337 | * reset all the banks before |
| 338 | * */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 339 | if (!(ioread32(denali->flash_reg + ONFI_TIMING_MODE) & |
| 340 | ONFI_TIMING_MODE__VALUE)) |
| 341 | return FAIL; |
| 342 | |
| 343 | for (i = 5; i > 0; i--) { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 344 | if (ioread32(denali->flash_reg + ONFI_TIMING_MODE) & |
| 345 | (0x01 << i)) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 346 | break; |
| 347 | } |
| 348 | |
Chuanxiao Dong | eda936e | 2010-07-27 14:17:37 +0800 | [diff] [blame] | 349 | nand_onfi_timing_set(denali, i); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 350 | |
| 351 | /* By now, all the ONFI devices we know support the page cache */ |
| 352 | /* rw feature. So here we enable the pipeline_rw_ahead feature */ |
| 353 | /* iowrite32(1, denali->flash_reg + CACHE_WRITE_ENABLE); */ |
| 354 | /* iowrite32(1, denali->flash_reg + CACHE_READ_ENABLE); */ |
| 355 | |
| 356 | return PASS; |
| 357 | } |
| 358 | |
Chuanxiao Dong | 4c03bbd | 2010-08-06 15:45:19 +0800 | [diff] [blame] | 359 | static void get_samsung_nand_para(struct denali_nand_info *denali, |
| 360 | uint8_t device_id) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 361 | { |
Chuanxiao Dong | 4c03bbd | 2010-08-06 15:45:19 +0800 | [diff] [blame] | 362 | if (device_id == 0xd3) { /* Samsung K9WAG08U1A */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 363 | /* Set timing register values according to datasheet */ |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 364 | iowrite32(5, denali->flash_reg + ACC_CLKS); |
| 365 | iowrite32(20, denali->flash_reg + RE_2_WE); |
| 366 | iowrite32(12, denali->flash_reg + WE_2_RE); |
| 367 | iowrite32(14, denali->flash_reg + ADDR_2_DATA); |
| 368 | iowrite32(3, denali->flash_reg + RDWR_EN_LO_CNT); |
| 369 | iowrite32(2, denali->flash_reg + RDWR_EN_HI_CNT); |
| 370 | iowrite32(2, denali->flash_reg + CS_SETUP_CNT); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 371 | } |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static void get_toshiba_nand_para(struct denali_nand_info *denali) |
| 375 | { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 376 | uint32_t tmp; |
| 377 | |
| 378 | /* Workaround to fix a controller bug which reports a wrong */ |
| 379 | /* spare area size for some kind of Toshiba NAND device */ |
| 380 | if ((ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) == 4096) && |
| 381 | (ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) == 64)) { |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 382 | iowrite32(216, denali->flash_reg + DEVICE_SPARE_AREA_SIZE); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 383 | tmp = ioread32(denali->flash_reg + DEVICES_CONNECTED) * |
| 384 | ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE); |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 385 | iowrite32(tmp, |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 386 | denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 387 | #if SUPPORT_15BITECC |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 388 | iowrite32(15, denali->flash_reg + ECC_CORRECTION); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 389 | #elif SUPPORT_8BITECC |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 390 | iowrite32(8, denali->flash_reg + ECC_CORRECTION); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 391 | #endif |
| 392 | } |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Chuanxiao Dong | ef41e1b | 2010-08-06 00:48:49 +0800 | [diff] [blame] | 395 | static void get_hynix_nand_para(struct denali_nand_info *denali, |
| 396 | uint8_t device_id) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 397 | { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 398 | uint32_t main_size, spare_size; |
| 399 | |
Chuanxiao Dong | ef41e1b | 2010-08-06 00:48:49 +0800 | [diff] [blame] | 400 | switch (device_id) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 401 | case 0xD5: /* Hynix H27UAG8T2A, H27UBG8U5A or H27UCG8VFA */ |
| 402 | case 0xD7: /* Hynix H27UDG8VEM, H27UCG8UDM or H27UCG8V5A */ |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 403 | iowrite32(128, denali->flash_reg + PAGES_PER_BLOCK); |
| 404 | iowrite32(4096, denali->flash_reg + DEVICE_MAIN_AREA_SIZE); |
| 405 | iowrite32(224, denali->flash_reg + DEVICE_SPARE_AREA_SIZE); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 406 | main_size = 4096 * |
| 407 | ioread32(denali->flash_reg + DEVICES_CONNECTED); |
| 408 | spare_size = 224 * |
| 409 | ioread32(denali->flash_reg + DEVICES_CONNECTED); |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 410 | iowrite32(main_size, |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 411 | denali->flash_reg + LOGICAL_PAGE_DATA_SIZE); |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 412 | iowrite32(spare_size, |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 413 | denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE); |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 414 | iowrite32(0, denali->flash_reg + DEVICE_WIDTH); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 415 | #if SUPPORT_15BITECC |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 416 | iowrite32(15, denali->flash_reg + ECC_CORRECTION); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 417 | #elif SUPPORT_8BITECC |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 418 | iowrite32(8, denali->flash_reg + ECC_CORRECTION); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 419 | #endif |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 420 | break; |
| 421 | default: |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 422 | dev_warn(&denali->dev->dev, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 423 | "Spectra: Unknown Hynix NAND (Device ID: 0x%x)." |
| 424 | "Will use default parameter values instead.\n", |
Chuanxiao.Dong | 6640652 | 2010-08-06 18:48:21 +0800 | [diff] [blame] | 425 | device_id); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | /* determines how many NAND chips are connected to the controller. Note for |
Chuanxiao Dong | b292c34 | 2010-08-11 17:46:00 +0800 | [diff] [blame] | 430 | * Intel CE4100 devices we don't support more than one device. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 431 | */ |
| 432 | static void find_valid_banks(struct denali_nand_info *denali) |
| 433 | { |
| 434 | uint32_t id[LLD_MAX_FLASH_BANKS]; |
| 435 | int i; |
| 436 | |
| 437 | denali->total_used_banks = 1; |
| 438 | for (i = 0; i < LLD_MAX_FLASH_BANKS; i++) { |
| 439 | index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 0), 0x90); |
| 440 | index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 1), 0); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 441 | index_addr_read_data(denali, |
| 442 | (uint32_t)(MODE_11 | (i << 24) | 2), &id[i]); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 443 | |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 444 | dev_dbg(&denali->dev->dev, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 445 | "Return 1st ID for bank[%d]: %x\n", i, id[i]); |
| 446 | |
| 447 | if (i == 0) { |
| 448 | if (!(id[i] & 0x0ff)) |
| 449 | break; /* WTF? */ |
| 450 | } else { |
| 451 | if ((id[i] & 0x0ff) == (id[0] & 0x0ff)) |
| 452 | denali->total_used_banks++; |
| 453 | else |
| 454 | break; |
| 455 | } |
| 456 | } |
| 457 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 458 | if (denali->platform == INTEL_CE4100) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 459 | /* Platform limitations of the CE4100 device limit |
| 460 | * users to a single chip solution for NAND. |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 461 | * Multichip support is not enabled. |
| 462 | */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 463 | if (denali->total_used_banks != 1) { |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 464 | dev_err(&denali->dev->dev, |
| 465 | "Sorry, Intel CE4100 only supports " |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 466 | "a single NAND device.\n"); |
| 467 | BUG(); |
| 468 | } |
| 469 | } |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 470 | dev_dbg(&denali->dev->dev, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 471 | "denali->total_used_banks: %d\n", denali->total_used_banks); |
| 472 | } |
| 473 | |
| 474 | static void detect_partition_feature(struct denali_nand_info *denali) |
| 475 | { |
Chuanxiao.Dong | 6640652 | 2010-08-06 18:48:21 +0800 | [diff] [blame] | 476 | /* For MRST platform, denali->fwblks represent the |
| 477 | * number of blocks firmware is taken, |
| 478 | * FW is in protect partition and MTD driver has no |
| 479 | * permission to access it. So let driver know how many |
| 480 | * blocks it can't touch. |
| 481 | * */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 482 | if (ioread32(denali->flash_reg + FEATURES) & FEATURES__PARTITION) { |
| 483 | if ((ioread32(denali->flash_reg + PERM_SRC_ID_1) & |
| 484 | PERM_SRC_ID_1__SRCID) == SPECTRA_PARTITION_ID) { |
Chuanxiao.Dong | 6640652 | 2010-08-06 18:48:21 +0800 | [diff] [blame] | 485 | denali->fwblks = |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 486 | ((ioread32(denali->flash_reg + MIN_MAX_BANK_1) & |
| 487 | MIN_MAX_BANK_1__MIN_VALUE) * |
Chuanxiao.Dong | 6640652 | 2010-08-06 18:48:21 +0800 | [diff] [blame] | 488 | denali->blksperchip) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 489 | + |
| 490 | (ioread32(denali->flash_reg + MIN_BLK_ADDR_1) & |
| 491 | MIN_BLK_ADDR_1__VALUE); |
Chuanxiao.Dong | 6640652 | 2010-08-06 18:48:21 +0800 | [diff] [blame] | 492 | } else |
| 493 | denali->fwblks = SPECTRA_START_BLOCK; |
| 494 | } else |
| 495 | denali->fwblks = SPECTRA_START_BLOCK; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Chuanxiao Dong | eda936e | 2010-07-27 14:17:37 +0800 | [diff] [blame] | 498 | static uint16_t denali_nand_timing_set(struct denali_nand_info *denali) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 499 | { |
| 500 | uint16_t status = PASS; |
Chuanxiao Dong | ef41e1b | 2010-08-06 00:48:49 +0800 | [diff] [blame] | 501 | uint32_t id_bytes[5], addr; |
| 502 | uint8_t i, maf_id, device_id; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 503 | |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 504 | dev_dbg(&denali->dev->dev, |
| 505 | "%s, Line %d, Function: %s\n", |
| 506 | __FILE__, __LINE__, __func__); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 507 | |
Chuanxiao Dong | ef41e1b | 2010-08-06 00:48:49 +0800 | [diff] [blame] | 508 | /* Use read id method to get device ID and other |
| 509 | * params. For some NAND chips, controller can't |
| 510 | * report the correct device ID by reading from |
| 511 | * DEVICE_ID register |
| 512 | * */ |
| 513 | addr = (uint32_t)MODE_11 | BANK(denali->flash_bank); |
| 514 | index_addr(denali, (uint32_t)addr | 0, 0x90); |
| 515 | index_addr(denali, (uint32_t)addr | 1, 0); |
| 516 | for (i = 0; i < 5; i++) |
| 517 | index_addr_read_data(denali, addr | 2, &id_bytes[i]); |
| 518 | maf_id = id_bytes[0]; |
| 519 | device_id = id_bytes[1]; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 520 | |
| 521 | if (ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) & |
| 522 | ONFI_DEVICE_NO_OF_LUNS__ONFI_DEVICE) { /* ONFI 1.0 NAND */ |
| 523 | if (FAIL == get_onfi_nand_para(denali)) |
| 524 | return FAIL; |
Chuanxiao Dong | ef41e1b | 2010-08-06 00:48:49 +0800 | [diff] [blame] | 525 | } else if (maf_id == 0xEC) { /* Samsung NAND */ |
Chuanxiao Dong | 4c03bbd | 2010-08-06 15:45:19 +0800 | [diff] [blame] | 526 | get_samsung_nand_para(denali, device_id); |
Chuanxiao Dong | ef41e1b | 2010-08-06 00:48:49 +0800 | [diff] [blame] | 527 | } else if (maf_id == 0x98) { /* Toshiba NAND */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 528 | get_toshiba_nand_para(denali); |
Chuanxiao Dong | ef41e1b | 2010-08-06 00:48:49 +0800 | [diff] [blame] | 529 | } else if (maf_id == 0xAD) { /* Hynix NAND */ |
| 530 | get_hynix_nand_para(denali, device_id); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 533 | dev_info(&denali->dev->dev, |
| 534 | "Dump timing register values:" |
| 535 | "acc_clks: %d, re_2_we: %d, re_2_re: %d\n" |
| 536 | "we_2_re: %d, addr_2_data: %d, rdwr_en_lo_cnt: %d\n" |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 537 | "rdwr_en_hi_cnt: %d, cs_setup_cnt: %d\n", |
| 538 | ioread32(denali->flash_reg + ACC_CLKS), |
| 539 | ioread32(denali->flash_reg + RE_2_WE), |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 540 | ioread32(denali->flash_reg + RE_2_RE), |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 541 | ioread32(denali->flash_reg + WE_2_RE), |
| 542 | ioread32(denali->flash_reg + ADDR_2_DATA), |
| 543 | ioread32(denali->flash_reg + RDWR_EN_LO_CNT), |
| 544 | ioread32(denali->flash_reg + RDWR_EN_HI_CNT), |
| 545 | ioread32(denali->flash_reg + CS_SETUP_CNT)); |
| 546 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 547 | find_valid_banks(denali); |
| 548 | |
| 549 | detect_partition_feature(denali); |
| 550 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 551 | /* If the user specified to override the default timings |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 552 | * with a specific ONFI mode, we apply those changes here. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 553 | */ |
| 554 | if (onfi_timing_mode != NAND_DEFAULT_TIMINGS) |
Chuanxiao Dong | eda936e | 2010-07-27 14:17:37 +0800 | [diff] [blame] | 555 | nand_onfi_timing_set(denali, onfi_timing_mode); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 556 | |
| 557 | return status; |
| 558 | } |
| 559 | |
Chuanxiao Dong | eda936e | 2010-07-27 14:17:37 +0800 | [diff] [blame] | 560 | static void denali_set_intr_modes(struct denali_nand_info *denali, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 561 | uint16_t INT_ENABLE) |
| 562 | { |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 563 | dev_dbg(&denali->dev->dev, "%s, Line %d, Function: %s\n", |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 564 | __FILE__, __LINE__, __func__); |
| 565 | |
| 566 | if (INT_ENABLE) |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 567 | iowrite32(1, denali->flash_reg + GLOBAL_INT_ENABLE); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 568 | else |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 569 | iowrite32(0, denali->flash_reg + GLOBAL_INT_ENABLE); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /* validation function to verify that the controlling software is making |
Chuanxiao Dong | b292c34 | 2010-08-11 17:46:00 +0800 | [diff] [blame] | 573 | * a valid request |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 574 | */ |
| 575 | static inline bool is_flash_bank_valid(int flash_bank) |
| 576 | { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 577 | return (flash_bank >= 0 && flash_bank < 4); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static void denali_irq_init(struct denali_nand_info *denali) |
| 581 | { |
| 582 | uint32_t int_mask = 0; |
| 583 | |
| 584 | /* Disable global interrupts */ |
Chuanxiao Dong | eda936e | 2010-07-27 14:17:37 +0800 | [diff] [blame] | 585 | denali_set_intr_modes(denali, false); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 586 | |
| 587 | int_mask = DENALI_IRQ_ALL; |
| 588 | |
| 589 | /* Clear all status bits */ |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 590 | iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS0); |
| 591 | iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS1); |
| 592 | iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS2); |
| 593 | iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS3); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 594 | |
| 595 | denali_irq_enable(denali, int_mask); |
| 596 | } |
| 597 | |
| 598 | static void denali_irq_cleanup(int irqnum, struct denali_nand_info *denali) |
| 599 | { |
Chuanxiao Dong | eda936e | 2010-07-27 14:17:37 +0800 | [diff] [blame] | 600 | denali_set_intr_modes(denali, false); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 601 | free_irq(irqnum, denali); |
| 602 | } |
| 603 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 604 | static void denali_irq_enable(struct denali_nand_info *denali, |
| 605 | uint32_t int_mask) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 606 | { |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 607 | iowrite32(int_mask, denali->flash_reg + INTR_EN0); |
| 608 | iowrite32(int_mask, denali->flash_reg + INTR_EN1); |
| 609 | iowrite32(int_mask, denali->flash_reg + INTR_EN2); |
| 610 | iowrite32(int_mask, denali->flash_reg + INTR_EN3); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* This function only returns when an interrupt that this driver cares about |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 614 | * occurs. This is to reduce the overhead of servicing interrupts |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 615 | */ |
| 616 | static inline uint32_t denali_irq_detected(struct denali_nand_info *denali) |
| 617 | { |
Chuanxiao Dong | a99d179 | 2010-07-27 11:32:21 +0800 | [diff] [blame] | 618 | return read_interrupt_status(denali) & DENALI_IRQ_ALL; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* Interrupts are cleared by writing a 1 to the appropriate status bit */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 622 | static inline void clear_interrupt(struct denali_nand_info *denali, |
| 623 | uint32_t irq_mask) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 624 | { |
| 625 | uint32_t intr_status_reg = 0; |
| 626 | |
| 627 | intr_status_reg = intr_status_addresses[denali->flash_bank]; |
| 628 | |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 629 | iowrite32(irq_mask, denali->flash_reg + intr_status_reg); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static void clear_interrupts(struct denali_nand_info *denali) |
| 633 | { |
| 634 | uint32_t status = 0x0; |
| 635 | spin_lock_irq(&denali->irq_lock); |
| 636 | |
| 637 | status = read_interrupt_status(denali); |
Chuanxiao Dong | 8ae61eb | 2010-08-10 00:07:01 +0800 | [diff] [blame] | 638 | clear_interrupt(denali, status); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 639 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 640 | denali->irq_status = 0x0; |
| 641 | spin_unlock_irq(&denali->irq_lock); |
| 642 | } |
| 643 | |
| 644 | static uint32_t read_interrupt_status(struct denali_nand_info *denali) |
| 645 | { |
| 646 | uint32_t intr_status_reg = 0; |
| 647 | |
| 648 | intr_status_reg = intr_status_addresses[denali->flash_bank]; |
| 649 | |
| 650 | return ioread32(denali->flash_reg + intr_status_reg); |
| 651 | } |
| 652 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 653 | /* This is the interrupt service routine. It handles all interrupts |
| 654 | * sent to this device. Note that on CE4100, this is a shared |
| 655 | * interrupt. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 656 | */ |
| 657 | static irqreturn_t denali_isr(int irq, void *dev_id) |
| 658 | { |
| 659 | struct denali_nand_info *denali = dev_id; |
| 660 | uint32_t irq_status = 0x0; |
| 661 | irqreturn_t result = IRQ_NONE; |
| 662 | |
| 663 | spin_lock(&denali->irq_lock); |
| 664 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 665 | /* check to see if a valid NAND chip has |
| 666 | * been selected. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 667 | */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 668 | if (is_flash_bank_valid(denali->flash_bank)) { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 669 | /* check to see if controller generated |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 670 | * the interrupt, since this is a shared interrupt */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 671 | irq_status = denali_irq_detected(denali); |
| 672 | if (irq_status != 0) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 673 | /* handle interrupt */ |
| 674 | /* first acknowledge it */ |
| 675 | clear_interrupt(denali, irq_status); |
| 676 | /* store the status in the device context for someone |
| 677 | to read */ |
| 678 | denali->irq_status |= irq_status; |
| 679 | /* notify anyone who cares that it happened */ |
| 680 | complete(&denali->complete); |
| 681 | /* tell the OS that we've handled this */ |
| 682 | result = IRQ_HANDLED; |
| 683 | } |
| 684 | } |
| 685 | spin_unlock(&denali->irq_lock); |
| 686 | return result; |
| 687 | } |
| 688 | #define BANK(x) ((x) << 24) |
| 689 | |
| 690 | static uint32_t wait_for_irq(struct denali_nand_info *denali, uint32_t irq_mask) |
| 691 | { |
| 692 | unsigned long comp_res = 0; |
| 693 | uint32_t intr_status = 0; |
| 694 | bool retry = false; |
| 695 | unsigned long timeout = msecs_to_jiffies(1000); |
| 696 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 697 | do { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 698 | comp_res = |
| 699 | wait_for_completion_timeout(&denali->complete, timeout); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 700 | spin_lock_irq(&denali->irq_lock); |
| 701 | intr_status = denali->irq_status; |
| 702 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 703 | if (intr_status & irq_mask) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 704 | denali->irq_status &= ~irq_mask; |
| 705 | spin_unlock_irq(&denali->irq_lock); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 706 | /* our interrupt was detected */ |
| 707 | break; |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 708 | } else { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 709 | /* these are not the interrupts you are looking for - |
| 710 | * need to wait again */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 711 | spin_unlock_irq(&denali->irq_lock); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 712 | retry = true; |
| 713 | } |
| 714 | } while (comp_res != 0); |
| 715 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 716 | if (comp_res == 0) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 717 | /* timeout */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 718 | printk(KERN_ERR "timeout occurred, status = 0x%x, mask = 0x%x\n", |
| 719 | intr_status, irq_mask); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 720 | |
| 721 | intr_status = 0; |
| 722 | } |
| 723 | return intr_status; |
| 724 | } |
| 725 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 726 | /* This helper function setups the registers for ECC and whether or not |
Chuanxiao Dong | b292c34 | 2010-08-11 17:46:00 +0800 | [diff] [blame] | 727 | * the spare area will be transfered. */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 728 | static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 729 | bool transfer_spare) |
| 730 | { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 731 | int ecc_en_flag = 0, transfer_spare_flag = 0; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 732 | |
| 733 | /* set ECC, transfer spare bits if needed */ |
| 734 | ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0; |
| 735 | transfer_spare_flag = transfer_spare ? TRANSFER_SPARE_REG__FLAG : 0; |
| 736 | |
| 737 | /* Enable spare area/ECC per user's request. */ |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 738 | iowrite32(ecc_en_flag, denali->flash_reg + ECC_ENABLE); |
| 739 | iowrite32(transfer_spare_flag, |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 740 | denali->flash_reg + TRANSFER_SPARE_REG); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 743 | /* sends a pipeline command operation to the controller. See the Denali NAND |
Chuanxiao Dong | b292c34 | 2010-08-11 17:46:00 +0800 | [diff] [blame] | 744 | * controller's user guide for more information (section 4.2.3.6). |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 745 | */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 746 | static int denali_send_pipeline_cmd(struct denali_nand_info *denali, |
| 747 | bool ecc_en, |
| 748 | bool transfer_spare, |
| 749 | int access_type, |
| 750 | int op) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 751 | { |
| 752 | int status = PASS; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 753 | uint32_t addr = 0x0, cmd = 0x0, page_count = 1, irq_status = 0, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 754 | irq_mask = 0; |
| 755 | |
Chuanxiao Dong | a99d179 | 2010-07-27 11:32:21 +0800 | [diff] [blame] | 756 | if (op == DENALI_READ) |
| 757 | irq_mask = INTR_STATUS0__LOAD_COMP; |
| 758 | else if (op == DENALI_WRITE) |
| 759 | irq_mask = 0; |
| 760 | else |
| 761 | BUG(); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 762 | |
| 763 | setup_ecc_for_xfer(denali, ecc_en, transfer_spare); |
| 764 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 765 | /* clear interrupts */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 766 | clear_interrupts(denali); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 767 | |
| 768 | addr = BANK(denali->flash_bank) | denali->page; |
| 769 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 770 | if (op == DENALI_WRITE && access_type != SPARE_ACCESS) { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 771 | cmd = MODE_01 | addr; |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 772 | iowrite32(cmd, denali->flash_mem); |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 773 | } else if (op == DENALI_WRITE && access_type == SPARE_ACCESS) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 774 | /* read spare area */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 775 | cmd = MODE_10 | addr; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 776 | index_addr(denali, (uint32_t)cmd, access_type); |
| 777 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 778 | cmd = MODE_01 | addr; |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 779 | iowrite32(cmd, denali->flash_mem); |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 780 | } else if (op == DENALI_READ) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 781 | /* setup page read request for access type */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 782 | cmd = MODE_10 | addr; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 783 | index_addr(denali, (uint32_t)cmd, access_type); |
| 784 | |
| 785 | /* page 33 of the NAND controller spec indicates we should not |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 786 | use the pipeline commands in Spare area only mode. So we |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 787 | don't. |
| 788 | */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 789 | if (access_type == SPARE_ACCESS) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 790 | cmd = MODE_01 | addr; |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 791 | iowrite32(cmd, denali->flash_mem); |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 792 | } else { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 793 | index_addr(denali, (uint32_t)cmd, |
| 794 | 0x2000 | op | page_count); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 795 | |
| 796 | /* wait for command to be accepted |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 797 | * can always use status0 bit as the |
| 798 | * mask is identical for each |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 799 | * bank. */ |
| 800 | irq_status = wait_for_irq(denali, irq_mask); |
| 801 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 802 | if (irq_status == 0) { |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 803 | dev_err(&denali->dev->dev, |
| 804 | "cmd, page, addr on timeout " |
| 805 | "(0x%x, 0x%x, 0x%x)\n", |
| 806 | cmd, denali->page, addr); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 807 | status = FAIL; |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 808 | } else { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 809 | cmd = MODE_01 | addr; |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 810 | iowrite32(cmd, denali->flash_mem); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | } |
| 814 | return status; |
| 815 | } |
| 816 | |
| 817 | /* helper function that simply writes a buffer to the flash */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 818 | static int write_data_to_flash_mem(struct denali_nand_info *denali, |
| 819 | const uint8_t *buf, |
| 820 | int len) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 821 | { |
| 822 | uint32_t i = 0, *buf32; |
| 823 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 824 | /* verify that the len is a multiple of 4. see comment in |
| 825 | * read_data_from_flash_mem() */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 826 | BUG_ON((len % 4) != 0); |
| 827 | |
| 828 | /* write the data to the flash memory */ |
| 829 | buf32 = (uint32_t *)buf; |
| 830 | for (i = 0; i < len / 4; i++) |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 831 | iowrite32(*buf32++, denali->flash_mem + 0x10); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 832 | return i*4; /* intent is to return the number of bytes read */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /* helper function that simply reads a buffer from the flash */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 836 | static int read_data_from_flash_mem(struct denali_nand_info *denali, |
| 837 | uint8_t *buf, |
| 838 | int len) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 839 | { |
| 840 | uint32_t i = 0, *buf32; |
| 841 | |
| 842 | /* we assume that len will be a multiple of 4, if not |
| 843 | * it would be nice to know about it ASAP rather than |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 844 | * have random failures... |
| 845 | * This assumption is based on the fact that this |
| 846 | * function is designed to be used to read flash pages, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 847 | * which are typically multiples of 4... |
| 848 | */ |
| 849 | |
| 850 | BUG_ON((len % 4) != 0); |
| 851 | |
| 852 | /* transfer the data from the flash */ |
| 853 | buf32 = (uint32_t *)buf; |
| 854 | for (i = 0; i < len / 4; i++) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 855 | *buf32++ = ioread32(denali->flash_mem + 0x10); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 856 | return i*4; /* intent is to return the number of bytes read */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /* writes OOB data to the device */ |
| 860 | static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page) |
| 861 | { |
| 862 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 863 | uint32_t irq_status = 0; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 864 | uint32_t irq_mask = INTR_STATUS0__PROGRAM_COMP | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 865 | INTR_STATUS0__PROGRAM_FAIL; |
| 866 | int status = 0; |
| 867 | |
| 868 | denali->page = page; |
| 869 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 870 | if (denali_send_pipeline_cmd(denali, false, false, SPARE_ACCESS, |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 871 | DENALI_WRITE) == PASS) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 872 | write_data_to_flash_mem(denali, buf, mtd->oobsize); |
| 873 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 874 | /* wait for operation to complete */ |
| 875 | irq_status = wait_for_irq(denali, irq_mask); |
| 876 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 877 | if (irq_status == 0) { |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 878 | dev_err(&denali->dev->dev, "OOB write failed\n"); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 879 | status = -EIO; |
| 880 | } |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 881 | } else { |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 882 | dev_err(&denali->dev->dev, "unable to send pipeline command\n"); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 883 | status = -EIO; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 884 | } |
| 885 | return status; |
| 886 | } |
| 887 | |
| 888 | /* reads OOB data from the device */ |
| 889 | static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page) |
| 890 | { |
| 891 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 892 | uint32_t irq_mask = INTR_STATUS0__LOAD_COMP, |
| 893 | irq_status = 0, addr = 0x0, cmd = 0x0; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 894 | |
| 895 | denali->page = page; |
| 896 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 897 | if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS, |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 898 | DENALI_READ) == PASS) { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 899 | read_data_from_flash_mem(denali, buf, mtd->oobsize); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 900 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 901 | /* wait for command to be accepted |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 902 | * can always use status0 bit as the mask is identical for each |
| 903 | * bank. */ |
| 904 | irq_status = wait_for_irq(denali, irq_mask); |
| 905 | |
| 906 | if (irq_status == 0) |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 907 | dev_err(&denali->dev->dev, "page on OOB timeout %d\n", |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 908 | denali->page); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 909 | |
| 910 | /* We set the device back to MAIN_ACCESS here as I observed |
| 911 | * instability with the controller if you do a block erase |
| 912 | * and the last transaction was a SPARE_ACCESS. Block erase |
| 913 | * is reliable (according to the MTD test infrastructure) |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 914 | * if you are in MAIN_ACCESS. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 915 | */ |
| 916 | addr = BANK(denali->flash_bank) | denali->page; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 917 | cmd = MODE_10 | addr; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 918 | index_addr(denali, (uint32_t)cmd, MAIN_ACCESS); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 922 | /* this function examines buffers to see if they contain data that |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 923 | * indicate that the buffer is part of an erased region of flash. |
| 924 | */ |
| 925 | bool is_erased(uint8_t *buf, int len) |
| 926 | { |
| 927 | int i = 0; |
| 928 | for (i = 0; i < len; i++) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 929 | if (buf[i] != 0xFF) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 930 | return false; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 931 | return true; |
| 932 | } |
| 933 | #define ECC_SECTOR_SIZE 512 |
| 934 | |
| 935 | #define ECC_SECTOR(x) (((x) & ECC_ERROR_ADDRESS__SECTOR_NR) >> 12) |
| 936 | #define ECC_BYTE(x) (((x) & ECC_ERROR_ADDRESS__OFFSET)) |
| 937 | #define ECC_CORRECTION_VALUE(x) ((x) & ERR_CORRECTION_INFO__BYTEMASK) |
Chuanxiao Dong | 8ae61eb | 2010-08-10 00:07:01 +0800 | [diff] [blame] | 938 | #define ECC_ERROR_CORRECTABLE(x) (!((x) & ERR_CORRECTION_INFO__ERROR_TYPE)) |
| 939 | #define ECC_ERR_DEVICE(x) (((x) & ERR_CORRECTION_INFO__DEVICE_NR) >> 8) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 940 | #define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO) |
| 941 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 942 | static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf, |
Chuanxiao Dong | 8ae61eb | 2010-08-10 00:07:01 +0800 | [diff] [blame] | 943 | uint32_t irq_status) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 944 | { |
| 945 | bool check_erased_page = false; |
| 946 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 947 | if (irq_status & INTR_STATUS0__ECC_ERR) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 948 | /* read the ECC errors. we'll ignore them for now */ |
| 949 | uint32_t err_address = 0, err_correction_info = 0; |
| 950 | uint32_t err_byte = 0, err_sector = 0, err_device = 0; |
| 951 | uint32_t err_correction_value = 0; |
Chuanxiao Dong | 8ae61eb | 2010-08-10 00:07:01 +0800 | [diff] [blame] | 952 | denali_set_intr_modes(denali, false); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 953 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 954 | do { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 955 | err_address = ioread32(denali->flash_reg + |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 956 | ECC_ERROR_ADDRESS); |
| 957 | err_sector = ECC_SECTOR(err_address); |
| 958 | err_byte = ECC_BYTE(err_address); |
| 959 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 960 | err_correction_info = ioread32(denali->flash_reg + |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 961 | ERR_CORRECTION_INFO); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 962 | err_correction_value = |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 963 | ECC_CORRECTION_VALUE(err_correction_info); |
| 964 | err_device = ECC_ERR_DEVICE(err_correction_info); |
| 965 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 966 | if (ECC_ERROR_CORRECTABLE(err_correction_info)) { |
Chuanxiao Dong | 8ae61eb | 2010-08-10 00:07:01 +0800 | [diff] [blame] | 967 | /* If err_byte is larger than ECC_SECTOR_SIZE, |
| 968 | * means error happend in OOB, so we ignore |
| 969 | * it. It's no need for us to correct it |
| 970 | * err_device is represented the NAND error |
| 971 | * bits are happened in if there are more |
| 972 | * than one NAND connected. |
| 973 | * */ |
| 974 | if (err_byte < ECC_SECTOR_SIZE) { |
| 975 | int offset; |
| 976 | offset = (err_sector * |
| 977 | ECC_SECTOR_SIZE + |
| 978 | err_byte) * |
| 979 | denali->devnum + |
| 980 | err_device; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 981 | /* correct the ECC error */ |
| 982 | buf[offset] ^= err_correction_value; |
| 983 | denali->mtd.ecc_stats.corrected++; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 984 | } |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 985 | } else { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 986 | /* if the error is not correctable, need to |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 987 | * look at the page to see if it is an erased |
| 988 | * page. if so, then it's not a real ECC error |
| 989 | * */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 990 | check_erased_page = true; |
| 991 | } |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 992 | } while (!ECC_LAST_ERR(err_correction_info)); |
Chuanxiao Dong | 8ae61eb | 2010-08-10 00:07:01 +0800 | [diff] [blame] | 993 | /* Once handle all ecc errors, controller will triger |
| 994 | * a ECC_TRANSACTION_DONE interrupt, so here just wait |
| 995 | * for a while for this interrupt |
| 996 | * */ |
| 997 | while (!(read_interrupt_status(denali) & |
| 998 | INTR_STATUS0__ECC_TRANSACTION_DONE)) |
| 999 | cpu_relax(); |
| 1000 | clear_interrupts(denali); |
| 1001 | denali_set_intr_modes(denali, true); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1002 | } |
| 1003 | return check_erased_page; |
| 1004 | } |
| 1005 | |
| 1006 | /* programs the controller to either enable/disable DMA transfers */ |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1007 | static void denali_enable_dma(struct denali_nand_info *denali, bool en) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1008 | { |
| 1009 | uint32_t reg_val = 0x0; |
| 1010 | |
Chuanxiao Dong | a99d179 | 2010-07-27 11:32:21 +0800 | [diff] [blame] | 1011 | if (en) |
| 1012 | reg_val = DMA_ENABLE__FLAG; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1013 | |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 1014 | iowrite32(reg_val, denali->flash_reg + DMA_ENABLE); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1015 | ioread32(denali->flash_reg + DMA_ENABLE); |
| 1016 | } |
| 1017 | |
| 1018 | /* setups the HW to perform the data DMA */ |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1019 | static void denali_setup_dma(struct denali_nand_info *denali, int op) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1020 | { |
| 1021 | uint32_t mode = 0x0; |
| 1022 | const int page_count = 1; |
| 1023 | dma_addr_t addr = denali->buf.dma_buf; |
| 1024 | |
| 1025 | mode = MODE_10 | BANK(denali->flash_bank); |
| 1026 | |
| 1027 | /* DMA is a four step process */ |
| 1028 | |
| 1029 | /* 1. setup transfer type and # of pages */ |
| 1030 | index_addr(denali, mode | denali->page, 0x2000 | op | page_count); |
| 1031 | |
| 1032 | /* 2. set memory high address bits 23:8 */ |
| 1033 | index_addr(denali, mode | ((uint16_t)(addr >> 16) << 8), 0x2200); |
| 1034 | |
| 1035 | /* 3. set memory low address bits 23:8 */ |
| 1036 | index_addr(denali, mode | ((uint16_t)addr << 8), 0x2300); |
| 1037 | |
| 1038 | /* 4. interrupt when complete, burst len = 64 bytes*/ |
| 1039 | index_addr(denali, mode | 0x14000, 0x2400); |
| 1040 | } |
| 1041 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1042 | /* writes a page. user specifies type, and this function handles the |
Chuanxiao Dong | b292c34 | 2010-08-11 17:46:00 +0800 | [diff] [blame] | 1043 | * configuration details. */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1044 | static void write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1045 | const uint8_t *buf, bool raw_xfer) |
| 1046 | { |
| 1047 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1048 | struct pci_dev *pci_dev = denali->dev; |
| 1049 | |
| 1050 | dma_addr_t addr = denali->buf.dma_buf; |
| 1051 | size_t size = denali->mtd.writesize + denali->mtd.oobsize; |
| 1052 | |
| 1053 | uint32_t irq_status = 0; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1054 | uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1055 | INTR_STATUS0__PROGRAM_FAIL; |
| 1056 | |
| 1057 | /* if it is a raw xfer, we want to disable ecc, and send |
| 1058 | * the spare area. |
| 1059 | * !raw_xfer - enable ecc |
| 1060 | * raw_xfer - transfer spare |
| 1061 | */ |
| 1062 | setup_ecc_for_xfer(denali, !raw_xfer, raw_xfer); |
| 1063 | |
| 1064 | /* copy buffer into DMA buffer */ |
| 1065 | memcpy(denali->buf.buf, buf, mtd->writesize); |
| 1066 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1067 | if (raw_xfer) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1068 | /* transfer the data to the spare area */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1069 | memcpy(denali->buf.buf + mtd->writesize, |
| 1070 | chip->oob_poi, |
| 1071 | mtd->oobsize); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_TODEVICE); |
| 1075 | |
| 1076 | clear_interrupts(denali); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1077 | denali_enable_dma(denali, true); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1078 | |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1079 | denali_setup_dma(denali, DENALI_WRITE); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1080 | |
| 1081 | /* wait for operation to complete */ |
| 1082 | irq_status = wait_for_irq(denali, irq_mask); |
| 1083 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1084 | if (irq_status == 0) { |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 1085 | dev_err(&denali->dev->dev, |
| 1086 | "timeout on write_page (type = %d)\n", |
| 1087 | raw_xfer); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1088 | denali->status = |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 1089 | (irq_status & INTR_STATUS0__PROGRAM_FAIL) ? |
| 1090 | NAND_STATUS_FAIL : PASS; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1091 | } |
| 1092 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1093 | denali_enable_dma(denali, false); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1094 | pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_TODEVICE); |
| 1095 | } |
| 1096 | |
| 1097 | /* NAND core entry points */ |
| 1098 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1099 | /* this is the callback that the NAND core calls to write a page. Since |
Chuanxiao Dong | b292c34 | 2010-08-11 17:46:00 +0800 | [diff] [blame] | 1100 | * writing a page with ECC or without is similar, all the work is done |
| 1101 | * by write_page above. |
| 1102 | * */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1103 | static void denali_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1104 | const uint8_t *buf) |
| 1105 | { |
| 1106 | /* for regular page writes, we let HW handle all the ECC |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1107 | * data written to the device. */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1108 | write_page(mtd, chip, buf, false); |
| 1109 | } |
| 1110 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1111 | /* This is the callback that the NAND core calls to write a page without ECC. |
Chuanxiao Dong | b292c34 | 2010-08-11 17:46:00 +0800 | [diff] [blame] | 1112 | * raw access is similiar to ECC page writes, so all the work is done in the |
| 1113 | * write_page() function above. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1114 | */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1115 | static void denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1116 | const uint8_t *buf) |
| 1117 | { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1118 | /* for raw page writes, we want to disable ECC and simply write |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1119 | whatever data is in the buffer. */ |
| 1120 | write_page(mtd, chip, buf, true); |
| 1121 | } |
| 1122 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1123 | static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1124 | int page) |
| 1125 | { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1126 | return write_oob_data(mtd, chip->oob_poi, page); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1127 | } |
| 1128 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1129 | static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1130 | int page, int sndcmd) |
| 1131 | { |
| 1132 | read_oob_data(mtd, chip->oob_poi, page); |
| 1133 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1134 | return 0; /* notify NAND core to send command to |
| 1135 | NAND device. */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 1139 | uint8_t *buf, int page) |
| 1140 | { |
| 1141 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1142 | struct pci_dev *pci_dev = denali->dev; |
| 1143 | |
| 1144 | dma_addr_t addr = denali->buf.dma_buf; |
| 1145 | size_t size = denali->mtd.writesize + denali->mtd.oobsize; |
| 1146 | |
| 1147 | uint32_t irq_status = 0; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1148 | uint32_t irq_mask = INTR_STATUS0__ECC_TRANSACTION_DONE | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1149 | INTR_STATUS0__ECC_ERR; |
| 1150 | bool check_erased_page = false; |
| 1151 | |
Chuanxiao Dong | 7d8a26f | 2010-08-11 18:19:23 +0800 | [diff] [blame^] | 1152 | if (page != denali->page) { |
| 1153 | dev_err(&denali->dev->dev, "IN %s: page %d is not" |
| 1154 | " equal to denali->page %d, investigate!!", |
| 1155 | __func__, page, denali->page); |
| 1156 | BUG(); |
| 1157 | } |
| 1158 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1159 | setup_ecc_for_xfer(denali, true, false); |
| 1160 | |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1161 | denali_enable_dma(denali, true); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1162 | pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_FROMDEVICE); |
| 1163 | |
| 1164 | clear_interrupts(denali); |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1165 | denali_setup_dma(denali, DENALI_READ); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1166 | |
| 1167 | /* wait for operation to complete */ |
| 1168 | irq_status = wait_for_irq(denali, irq_mask); |
| 1169 | |
| 1170 | pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_FROMDEVICE); |
| 1171 | |
| 1172 | memcpy(buf, denali->buf.buf, mtd->writesize); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1173 | |
Chuanxiao Dong | 8ae61eb | 2010-08-10 00:07:01 +0800 | [diff] [blame] | 1174 | check_erased_page = handle_ecc(denali, buf, irq_status); |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1175 | denali_enable_dma(denali, false); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1176 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1177 | if (check_erased_page) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1178 | read_oob_data(&denali->mtd, chip->oob_poi, denali->page); |
| 1179 | |
| 1180 | /* check ECC failures that may have occurred on erased pages */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1181 | if (check_erased_page) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1182 | if (!is_erased(buf, denali->mtd.writesize)) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1183 | denali->mtd.ecc_stats.failed++; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1184 | if (!is_erased(buf, denali->mtd.oobsize)) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1185 | denali->mtd.ecc_stats.failed++; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1186 | } |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1187 | } |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 1192 | uint8_t *buf, int page) |
| 1193 | { |
| 1194 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1195 | struct pci_dev *pci_dev = denali->dev; |
| 1196 | |
| 1197 | dma_addr_t addr = denali->buf.dma_buf; |
| 1198 | size_t size = denali->mtd.writesize + denali->mtd.oobsize; |
| 1199 | |
| 1200 | uint32_t irq_status = 0; |
| 1201 | uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1202 | |
Chuanxiao Dong | 7d8a26f | 2010-08-11 18:19:23 +0800 | [diff] [blame^] | 1203 | if (page != denali->page) { |
| 1204 | dev_err(&denali->dev->dev, "IN %s: page %d is not" |
| 1205 | " equal to denali->page %d, investigate!!", |
| 1206 | __func__, page, denali->page); |
| 1207 | BUG(); |
| 1208 | } |
| 1209 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1210 | setup_ecc_for_xfer(denali, false, true); |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1211 | denali_enable_dma(denali, true); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1212 | |
| 1213 | pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_FROMDEVICE); |
| 1214 | |
| 1215 | clear_interrupts(denali); |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1216 | denali_setup_dma(denali, DENALI_READ); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1217 | |
| 1218 | /* wait for operation to complete */ |
| 1219 | irq_status = wait_for_irq(denali, irq_mask); |
| 1220 | |
| 1221 | pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_FROMDEVICE); |
| 1222 | |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1223 | denali_enable_dma(denali, false); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1224 | |
| 1225 | memcpy(buf, denali->buf.buf, mtd->writesize); |
| 1226 | memcpy(chip->oob_poi, denali->buf.buf + mtd->writesize, mtd->oobsize); |
| 1227 | |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | static uint8_t denali_read_byte(struct mtd_info *mtd) |
| 1232 | { |
| 1233 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1234 | uint8_t result = 0xff; |
| 1235 | |
| 1236 | if (denali->buf.head < denali->buf.tail) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1237 | result = denali->buf.buf[denali->buf.head++]; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1238 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1239 | return result; |
| 1240 | } |
| 1241 | |
| 1242 | static void denali_select_chip(struct mtd_info *mtd, int chip) |
| 1243 | { |
| 1244 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 1245 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1246 | spin_lock_irq(&denali->irq_lock); |
| 1247 | denali->flash_bank = chip; |
| 1248 | spin_unlock_irq(&denali->irq_lock); |
| 1249 | } |
| 1250 | |
| 1251 | static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip) |
| 1252 | { |
| 1253 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1254 | int status = denali->status; |
| 1255 | denali->status = 0; |
| 1256 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1257 | return status; |
| 1258 | } |
| 1259 | |
| 1260 | static void denali_erase(struct mtd_info *mtd, int page) |
| 1261 | { |
| 1262 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1263 | |
| 1264 | uint32_t cmd = 0x0, irq_status = 0; |
| 1265 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1266 | /* clear interrupts */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1267 | clear_interrupts(denali); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1268 | |
| 1269 | /* setup page read request for access type */ |
| 1270 | cmd = MODE_10 | BANK(denali->flash_bank) | page; |
| 1271 | index_addr(denali, (uint32_t)cmd, 0x1); |
| 1272 | |
| 1273 | /* wait for erase to complete or failure to occur */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1274 | irq_status = wait_for_irq(denali, INTR_STATUS0__ERASE_COMP | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1275 | INTR_STATUS0__ERASE_FAIL); |
| 1276 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 1277 | denali->status = (irq_status & INTR_STATUS0__ERASE_FAIL) ? |
| 1278 | NAND_STATUS_FAIL : PASS; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1279 | } |
| 1280 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1281 | static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1282 | int page) |
| 1283 | { |
| 1284 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Chuanxiao Dong | ef41e1b | 2010-08-06 00:48:49 +0800 | [diff] [blame] | 1285 | uint32_t addr, id; |
| 1286 | int i; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1287 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1288 | switch (cmd) { |
Chuanxiao Dong | a99d179 | 2010-07-27 11:32:21 +0800 | [diff] [blame] | 1289 | case NAND_CMD_PAGEPROG: |
| 1290 | break; |
| 1291 | case NAND_CMD_STATUS: |
| 1292 | read_status(denali); |
| 1293 | break; |
| 1294 | case NAND_CMD_READID: |
| 1295 | reset_buf(denali); |
Chuanxiao Dong | ef41e1b | 2010-08-06 00:48:49 +0800 | [diff] [blame] | 1296 | /*sometimes ManufactureId read from register is not right |
| 1297 | * e.g. some of Micron MT29F32G08QAA MLC NAND chips |
| 1298 | * So here we send READID cmd to NAND insteand |
| 1299 | * */ |
| 1300 | addr = (uint32_t)MODE_11 | BANK(denali->flash_bank); |
| 1301 | index_addr(denali, (uint32_t)addr | 0, 0x90); |
| 1302 | index_addr(denali, (uint32_t)addr | 1, 0); |
| 1303 | for (i = 0; i < 5; i++) { |
| 1304 | index_addr_read_data(denali, |
| 1305 | (uint32_t)addr | 2, |
| 1306 | &id); |
| 1307 | write_byte_to_buf(denali, id); |
Chuanxiao Dong | a99d179 | 2010-07-27 11:32:21 +0800 | [diff] [blame] | 1308 | } |
| 1309 | break; |
| 1310 | case NAND_CMD_READ0: |
| 1311 | case NAND_CMD_SEQIN: |
| 1312 | denali->page = page; |
| 1313 | break; |
| 1314 | case NAND_CMD_RESET: |
| 1315 | reset_bank(denali); |
| 1316 | break; |
| 1317 | case NAND_CMD_READOOB: |
| 1318 | /* TODO: Read OOB data */ |
| 1319 | break; |
| 1320 | default: |
| 1321 | printk(KERN_ERR ": unsupported command" |
| 1322 | " received 0x%x\n", cmd); |
| 1323 | break; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | /* stubs for ECC functions not used by the NAND core */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1328 | static int denali_ecc_calculate(struct mtd_info *mtd, const uint8_t *data, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1329 | uint8_t *ecc_code) |
| 1330 | { |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 1331 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1332 | dev_err(&denali->dev->dev, |
| 1333 | "denali_ecc_calculate called unexpectedly\n"); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1334 | BUG(); |
| 1335 | return -EIO; |
| 1336 | } |
| 1337 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1338 | static int denali_ecc_correct(struct mtd_info *mtd, uint8_t *data, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1339 | uint8_t *read_ecc, uint8_t *calc_ecc) |
| 1340 | { |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 1341 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1342 | dev_err(&denali->dev->dev, |
| 1343 | "denali_ecc_correct called unexpectedly\n"); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1344 | BUG(); |
| 1345 | return -EIO; |
| 1346 | } |
| 1347 | |
| 1348 | static void denali_ecc_hwctl(struct mtd_info *mtd, int mode) |
| 1349 | { |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 1350 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1351 | dev_err(&denali->dev->dev, |
| 1352 | "denali_ecc_hwctl called unexpectedly\n"); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1353 | BUG(); |
| 1354 | } |
| 1355 | /* end NAND core entry points */ |
| 1356 | |
| 1357 | /* Initialization code to bring the device up to a known good state */ |
| 1358 | static void denali_hw_init(struct denali_nand_info *denali) |
| 1359 | { |
Chuanxiao Dong | db9a321 | 2010-08-06 18:02:03 +0800 | [diff] [blame] | 1360 | /* tell driver how many bit controller will skip before |
| 1361 | * writing ECC code in OOB, this register may be already |
| 1362 | * set by firmware. So we read this value out. |
| 1363 | * if this value is 0, just let it be. |
| 1364 | * */ |
| 1365 | denali->bbtskipbytes = ioread32(denali->flash_reg + |
| 1366 | SPARE_AREA_SKIP_BYTES); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1367 | denali_irq_init(denali); |
Chuanxiao Dong | eda936e | 2010-07-27 14:17:37 +0800 | [diff] [blame] | 1368 | denali_nand_reset(denali); |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 1369 | iowrite32(0x0F, denali->flash_reg + RB_PIN_ENABLED); |
| 1370 | iowrite32(CHIP_EN_DONT_CARE__FLAG, |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 1371 | denali->flash_reg + CHIP_ENABLE_DONT_CARE); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1372 | |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 1373 | iowrite32(0x0, denali->flash_reg + SPARE_AREA_SKIP_BYTES); |
| 1374 | iowrite32(0xffff, denali->flash_reg + SPARE_AREA_MARKER); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1375 | |
| 1376 | /* Should set value for these registers when init */ |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 1377 | iowrite32(0, denali->flash_reg + TWO_ROW_ADDR_CYCLES); |
| 1378 | iowrite32(1, denali->flash_reg + ECC_ENABLE); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1379 | } |
| 1380 | |
Chuanxiao Dong | db9a321 | 2010-08-06 18:02:03 +0800 | [diff] [blame] | 1381 | /* Althogh controller spec said SLC ECC is forceb to be 4bit, |
| 1382 | * but denali controller in MRST only support 15bit and 8bit ECC |
| 1383 | * correction |
| 1384 | * */ |
| 1385 | #define ECC_8BITS 14 |
| 1386 | static struct nand_ecclayout nand_8bit_oob = { |
| 1387 | .eccbytes = 14, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1388 | }; |
| 1389 | |
Chuanxiao Dong | db9a321 | 2010-08-06 18:02:03 +0800 | [diff] [blame] | 1390 | #define ECC_15BITS 26 |
| 1391 | static struct nand_ecclayout nand_15bit_oob = { |
| 1392 | .eccbytes = 26, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1393 | }; |
| 1394 | |
| 1395 | static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' }; |
| 1396 | static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' }; |
| 1397 | |
| 1398 | static struct nand_bbt_descr bbt_main_descr = { |
| 1399 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 1400 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 1401 | .offs = 8, |
| 1402 | .len = 4, |
| 1403 | .veroffs = 12, |
| 1404 | .maxblocks = 4, |
| 1405 | .pattern = bbt_pattern, |
| 1406 | }; |
| 1407 | |
| 1408 | static struct nand_bbt_descr bbt_mirror_descr = { |
| 1409 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 1410 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 1411 | .offs = 8, |
| 1412 | .len = 4, |
| 1413 | .veroffs = 12, |
| 1414 | .maxblocks = 4, |
| 1415 | .pattern = mirror_pattern, |
| 1416 | }; |
| 1417 | |
Uwe Kleine-König | 421f91d | 2010-06-11 12:17:00 +0200 | [diff] [blame] | 1418 | /* initialize driver data structures */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1419 | void denali_drv_init(struct denali_nand_info *denali) |
| 1420 | { |
| 1421 | denali->idx = 0; |
| 1422 | |
| 1423 | /* setup interrupt handler */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1424 | /* the completion object will be used to notify |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1425 | * the callee that the interrupt is done */ |
| 1426 | init_completion(&denali->complete); |
| 1427 | |
| 1428 | /* the spinlock will be used to synchronize the ISR |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1429 | * with any element that might be access shared |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1430 | * data (interrupt status) */ |
| 1431 | spin_lock_init(&denali->irq_lock); |
| 1432 | |
| 1433 | /* indicate that MTD has not selected a valid bank yet */ |
| 1434 | denali->flash_bank = CHIP_SELECT_INVALID; |
| 1435 | |
| 1436 | /* initialize our irq_status variable to indicate no interrupts */ |
| 1437 | denali->irq_status = 0; |
| 1438 | } |
| 1439 | |
| 1440 | /* driver entry point */ |
| 1441 | static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) |
| 1442 | { |
| 1443 | int ret = -ENODEV; |
| 1444 | resource_size_t csr_base, mem_base; |
| 1445 | unsigned long csr_len, mem_len; |
| 1446 | struct denali_nand_info *denali; |
| 1447 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1448 | denali = kzalloc(sizeof(*denali), GFP_KERNEL); |
| 1449 | if (!denali) |
| 1450 | return -ENOMEM; |
| 1451 | |
| 1452 | ret = pci_enable_device(dev); |
| 1453 | if (ret) { |
| 1454 | printk(KERN_ERR "Spectra: pci_enable_device failed.\n"); |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1455 | goto failed_alloc_memery; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | if (id->driver_data == INTEL_CE4100) { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1459 | /* Due to a silicon limitation, we can only support |
| 1460 | * ONFI timing mode 1 and below. |
| 1461 | */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1462 | if (onfi_timing_mode < -1 || onfi_timing_mode > 1) { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 1463 | printk(KERN_ERR "Intel CE4100 only supports" |
| 1464 | " ONFI timing mode 1 or below\n"); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1465 | ret = -EINVAL; |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1466 | goto failed_enable_dev; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1467 | } |
| 1468 | denali->platform = INTEL_CE4100; |
| 1469 | mem_base = pci_resource_start(dev, 0); |
| 1470 | mem_len = pci_resource_len(dev, 1); |
| 1471 | csr_base = pci_resource_start(dev, 1); |
| 1472 | csr_len = pci_resource_len(dev, 1); |
| 1473 | } else { |
| 1474 | denali->platform = INTEL_MRST; |
| 1475 | csr_base = pci_resource_start(dev, 0); |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1476 | csr_len = pci_resource_len(dev, 0); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1477 | mem_base = pci_resource_start(dev, 1); |
| 1478 | mem_len = pci_resource_len(dev, 1); |
| 1479 | if (!mem_len) { |
| 1480 | mem_base = csr_base + csr_len; |
| 1481 | mem_len = csr_len; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | /* Is 32-bit DMA supported? */ |
| 1486 | ret = pci_set_dma_mask(dev, DMA_BIT_MASK(32)); |
| 1487 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1488 | if (ret) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1489 | printk(KERN_ERR "Spectra: no usable DMA configuration\n"); |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1490 | goto failed_enable_dev; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1491 | } |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 1492 | denali->buf.dma_buf = |
| 1493 | pci_map_single(dev, denali->buf.buf, |
| 1494 | DENALI_BUF_SIZE, |
| 1495 | PCI_DMA_BIDIRECTIONAL); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1496 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1497 | if (pci_dma_mapping_error(dev, denali->buf.dma_buf)) { |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 1498 | dev_err(&dev->dev, "Spectra: failed to map DMA buffer\n"); |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1499 | goto failed_enable_dev; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | pci_set_master(dev); |
| 1503 | denali->dev = dev; |
| 1504 | |
| 1505 | ret = pci_request_regions(dev, DENALI_NAND_NAME); |
| 1506 | if (ret) { |
| 1507 | printk(KERN_ERR "Spectra: Unable to request memory regions\n"); |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1508 | goto failed_dma_map; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | denali->flash_reg = ioremap_nocache(csr_base, csr_len); |
| 1512 | if (!denali->flash_reg) { |
| 1513 | printk(KERN_ERR "Spectra: Unable to remap memory region\n"); |
| 1514 | ret = -ENOMEM; |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1515 | goto failed_req_regions; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1516 | } |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1517 | |
| 1518 | denali->flash_mem = ioremap_nocache(mem_base, mem_len); |
| 1519 | if (!denali->flash_mem) { |
| 1520 | printk(KERN_ERR "Spectra: ioremap_nocache failed!"); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1521 | ret = -ENOMEM; |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1522 | goto failed_remap_reg; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1523 | } |
| 1524 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1525 | denali_hw_init(denali); |
| 1526 | denali_drv_init(denali); |
| 1527 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1528 | if (request_irq(dev->irq, denali_isr, IRQF_SHARED, |
| 1529 | DENALI_NAND_NAME, denali)) { |
| 1530 | printk(KERN_ERR "Spectra: Unable to allocate IRQ\n"); |
| 1531 | ret = -ENODEV; |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1532 | goto failed_remap_mem; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | /* now that our ISR is registered, we can enable interrupts */ |
Chuanxiao Dong | eda936e | 2010-07-27 14:17:37 +0800 | [diff] [blame] | 1536 | denali_set_intr_modes(denali, true); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1537 | |
| 1538 | pci_set_drvdata(dev, denali); |
| 1539 | |
Chuanxiao Dong | eda936e | 2010-07-27 14:17:37 +0800 | [diff] [blame] | 1540 | denali_nand_timing_set(denali); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1541 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1542 | denali->mtd.name = "Denali NAND"; |
| 1543 | denali->mtd.owner = THIS_MODULE; |
| 1544 | denali->mtd.priv = &denali->nand; |
| 1545 | |
| 1546 | /* register the driver with the NAND core subsystem */ |
| 1547 | denali->nand.select_chip = denali_select_chip; |
| 1548 | denali->nand.cmdfunc = denali_cmdfunc; |
| 1549 | denali->nand.read_byte = denali_read_byte; |
| 1550 | denali->nand.waitfunc = denali_waitfunc; |
| 1551 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1552 | /* scan for NAND devices attached to the controller |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1553 | * this is the first stage in a two step process to register |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1554 | * with the nand subsystem */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1555 | if (nand_scan_ident(&denali->mtd, LLD_MAX_FLASH_BANKS, NULL)) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1556 | ret = -ENXIO; |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1557 | goto failed_req_irq; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1558 | } |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1559 | |
Chuanxiao.Dong | 6640652 | 2010-08-06 18:48:21 +0800 | [diff] [blame] | 1560 | /* MTD supported page sizes vary by kernel. We validate our |
| 1561 | * kernel supports the device here. |
| 1562 | */ |
| 1563 | if (denali->mtd.writesize > NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE) { |
| 1564 | ret = -ENODEV; |
| 1565 | printk(KERN_ERR "Spectra: device size not supported by this " |
| 1566 | "version of MTD."); |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1567 | goto failed_req_irq; |
Chuanxiao.Dong | 6640652 | 2010-08-06 18:48:21 +0800 | [diff] [blame] | 1568 | } |
| 1569 | |
Chuanxiao Dong | 08b9ab9 | 2010-08-06 18:19:09 +0800 | [diff] [blame] | 1570 | /* support for multi nand |
| 1571 | * MTD known nothing about multi nand, |
| 1572 | * so we should tell it the real pagesize |
| 1573 | * and anything necessery |
| 1574 | */ |
| 1575 | denali->devnum = ioread32(denali->flash_reg + DEVICES_CONNECTED); |
| 1576 | denali->nand.chipsize <<= (denali->devnum - 1); |
| 1577 | denali->nand.page_shift += (denali->devnum - 1); |
| 1578 | denali->nand.pagemask = (denali->nand.chipsize >> |
| 1579 | denali->nand.page_shift) - 1; |
| 1580 | denali->nand.bbt_erase_shift += (denali->devnum - 1); |
| 1581 | denali->nand.phys_erase_shift = denali->nand.bbt_erase_shift; |
| 1582 | denali->nand.chip_shift += (denali->devnum - 1); |
| 1583 | denali->mtd.writesize <<= (denali->devnum - 1); |
| 1584 | denali->mtd.oobsize <<= (denali->devnum - 1); |
| 1585 | denali->mtd.erasesize <<= (denali->devnum - 1); |
| 1586 | denali->mtd.size = denali->nand.numchips * denali->nand.chipsize; |
| 1587 | denali->bbtskipbytes *= denali->devnum; |
| 1588 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1589 | /* second stage of the NAND scan |
| 1590 | * this stage requires information regarding ECC and |
| 1591 | * bad block management. */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1592 | |
| 1593 | /* Bad block management */ |
| 1594 | denali->nand.bbt_td = &bbt_main_descr; |
| 1595 | denali->nand.bbt_md = &bbt_mirror_descr; |
| 1596 | |
| 1597 | /* skip the scan for now until we have OOB read and write support */ |
| 1598 | denali->nand.options |= NAND_USE_FLASH_BBT | NAND_SKIP_BBTSCAN; |
| 1599 | denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME; |
| 1600 | |
Chuanxiao Dong | db9a321 | 2010-08-06 18:02:03 +0800 | [diff] [blame] | 1601 | /* Denali Controller only support 15bit and 8bit ECC in MRST, |
| 1602 | * so just let controller do 15bit ECC for MLC and 8bit ECC for |
| 1603 | * SLC if possible. |
| 1604 | * */ |
| 1605 | if (denali->nand.cellinfo & 0xc && |
| 1606 | (denali->mtd.oobsize > (denali->bbtskipbytes + |
| 1607 | ECC_15BITS * (denali->mtd.writesize / |
| 1608 | ECC_SECTOR_SIZE)))) { |
| 1609 | /* if MLC OOB size is large enough, use 15bit ECC*/ |
| 1610 | denali->nand.ecc.layout = &nand_15bit_oob; |
| 1611 | denali->nand.ecc.bytes = ECC_15BITS; |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 1612 | iowrite32(15, denali->flash_reg + ECC_CORRECTION); |
Chuanxiao Dong | db9a321 | 2010-08-06 18:02:03 +0800 | [diff] [blame] | 1613 | } else if (denali->mtd.oobsize < (denali->bbtskipbytes + |
| 1614 | ECC_8BITS * (denali->mtd.writesize / |
| 1615 | ECC_SECTOR_SIZE))) { |
| 1616 | printk(KERN_ERR "Your NAND chip OOB is not large enough to" |
| 1617 | " contain 8bit ECC correction codes"); |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1618 | goto failed_req_irq; |
Chuanxiao Dong | db9a321 | 2010-08-06 18:02:03 +0800 | [diff] [blame] | 1619 | } else { |
| 1620 | denali->nand.ecc.layout = &nand_8bit_oob; |
| 1621 | denali->nand.ecc.bytes = ECC_8BITS; |
Chuanxiao Dong | 24c3fa3 | 2010-08-09 23:59:23 +0800 | [diff] [blame] | 1622 | iowrite32(8, denali->flash_reg + ECC_CORRECTION); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1623 | } |
| 1624 | |
Chuanxiao Dong | 08b9ab9 | 2010-08-06 18:19:09 +0800 | [diff] [blame] | 1625 | denali->nand.ecc.bytes *= denali->devnum; |
Chuanxiao Dong | db9a321 | 2010-08-06 18:02:03 +0800 | [diff] [blame] | 1626 | denali->nand.ecc.layout->eccbytes *= |
| 1627 | denali->mtd.writesize / ECC_SECTOR_SIZE; |
| 1628 | denali->nand.ecc.layout->oobfree[0].offset = |
| 1629 | denali->bbtskipbytes + denali->nand.ecc.layout->eccbytes; |
| 1630 | denali->nand.ecc.layout->oobfree[0].length = |
| 1631 | denali->mtd.oobsize - denali->nand.ecc.layout->eccbytes - |
| 1632 | denali->bbtskipbytes; |
| 1633 | |
Chuanxiao.Dong | 6640652 | 2010-08-06 18:48:21 +0800 | [diff] [blame] | 1634 | /* Let driver know the total blocks number and |
| 1635 | * how many blocks contained by each nand chip. |
| 1636 | * blksperchip will help driver to know how many |
| 1637 | * blocks is taken by FW. |
| 1638 | * */ |
| 1639 | denali->totalblks = denali->mtd.size >> |
| 1640 | denali->nand.phys_erase_shift; |
| 1641 | denali->blksperchip = denali->totalblks / denali->nand.numchips; |
| 1642 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1643 | /* These functions are required by the NAND core framework, otherwise, |
| 1644 | * the NAND core will assert. However, we don't need them, so we'll stub |
| 1645 | * them out. */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1646 | denali->nand.ecc.calculate = denali_ecc_calculate; |
| 1647 | denali->nand.ecc.correct = denali_ecc_correct; |
| 1648 | denali->nand.ecc.hwctl = denali_ecc_hwctl; |
| 1649 | |
| 1650 | /* override the default read operations */ |
Chuanxiao Dong | 08b9ab9 | 2010-08-06 18:19:09 +0800 | [diff] [blame] | 1651 | denali->nand.ecc.size = ECC_SECTOR_SIZE * denali->devnum; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1652 | denali->nand.ecc.read_page = denali_read_page; |
| 1653 | denali->nand.ecc.read_page_raw = denali_read_page_raw; |
| 1654 | denali->nand.ecc.write_page = denali_write_page; |
| 1655 | denali->nand.ecc.write_page_raw = denali_write_page_raw; |
| 1656 | denali->nand.ecc.read_oob = denali_read_oob; |
| 1657 | denali->nand.ecc.write_oob = denali_write_oob; |
| 1658 | denali->nand.erase_cmd = denali_erase; |
| 1659 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1660 | if (nand_scan_tail(&denali->mtd)) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1661 | ret = -ENXIO; |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1662 | goto failed_req_irq; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | ret = add_mtd_device(&denali->mtd); |
| 1666 | if (ret) { |
Chuanxiao Dong | 7cfffac | 2010-08-10 00:16:51 +0800 | [diff] [blame] | 1667 | dev_err(&dev->dev, "Spectra: Failed to register MTD: %d\n", |
| 1668 | ret); |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1669 | goto failed_req_irq; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1670 | } |
| 1671 | return 0; |
| 1672 | |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1673 | failed_req_irq: |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1674 | denali_irq_cleanup(dev->irq, denali); |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1675 | failed_remap_mem: |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1676 | iounmap(denali->flash_mem); |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1677 | failed_remap_reg: |
| 1678 | iounmap(denali->flash_reg); |
| 1679 | failed_req_regions: |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1680 | pci_release_regions(dev); |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1681 | failed_dma_map: |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1682 | pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1683 | PCI_DMA_BIDIRECTIONAL); |
Chuanxiao Dong | 5c0eb90 | 2010-08-09 18:37:00 +0800 | [diff] [blame] | 1684 | failed_enable_dev: |
| 1685 | pci_disable_device(dev); |
| 1686 | failed_alloc_memery: |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1687 | kfree(denali); |
| 1688 | return ret; |
| 1689 | } |
| 1690 | |
| 1691 | /* driver exit point */ |
| 1692 | static void denali_pci_remove(struct pci_dev *dev) |
| 1693 | { |
| 1694 | struct denali_nand_info *denali = pci_get_drvdata(dev); |
| 1695 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1696 | nand_release(&denali->mtd); |
| 1697 | del_mtd_device(&denali->mtd); |
| 1698 | |
| 1699 | denali_irq_cleanup(dev->irq, denali); |
| 1700 | |
| 1701 | iounmap(denali->flash_reg); |
| 1702 | iounmap(denali->flash_mem); |
| 1703 | pci_release_regions(dev); |
| 1704 | pci_disable_device(dev); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1705 | pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1706 | PCI_DMA_BIDIRECTIONAL); |
| 1707 | pci_set_drvdata(dev, NULL); |
| 1708 | kfree(denali); |
| 1709 | } |
| 1710 | |
| 1711 | MODULE_DEVICE_TABLE(pci, denali_pci_ids); |
| 1712 | |
| 1713 | static struct pci_driver denali_pci_driver = { |
| 1714 | .name = DENALI_NAND_NAME, |
| 1715 | .id_table = denali_pci_ids, |
| 1716 | .probe = denali_pci_probe, |
| 1717 | .remove = denali_pci_remove, |
| 1718 | }; |
| 1719 | |
| 1720 | static int __devinit denali_init(void) |
| 1721 | { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame] | 1722 | printk(KERN_INFO "Spectra MTD driver built on %s @ %s\n", |
| 1723 | __DATE__, __TIME__); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1724 | return pci_register_driver(&denali_pci_driver); |
| 1725 | } |
| 1726 | |
| 1727 | /* Free memory */ |
| 1728 | static void __devexit denali_exit(void) |
| 1729 | { |
| 1730 | pci_unregister_driver(&denali_pci_driver); |
| 1731 | } |
| 1732 | |
| 1733 | module_init(denali_init); |
| 1734 | module_exit(denali_exit); |