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> |
| 24 | #include <linux/pci.h> |
| 25 | #include <linux/mtd/mtd.h> |
| 26 | #include <linux/module.h> |
| 27 | |
| 28 | #include "denali.h" |
| 29 | |
| 30 | MODULE_LICENSE("GPL"); |
| 31 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 32 | /* We define a module parameter that allows the user to override |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 33 | * the hardware and decide what timing mode should be used. |
| 34 | */ |
| 35 | #define NAND_DEFAULT_TIMINGS -1 |
| 36 | |
| 37 | static int onfi_timing_mode = NAND_DEFAULT_TIMINGS; |
| 38 | module_param(onfi_timing_mode, int, S_IRUGO); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 39 | MODULE_PARM_DESC(onfi_timing_mode, "Overrides default ONFI setting." |
| 40 | " -1 indicates use default timings"); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 41 | |
| 42 | #define DENALI_NAND_NAME "denali-nand" |
| 43 | |
| 44 | /* We define a macro here that combines all interrupts this driver uses into |
| 45 | * a single constant value, for convenience. */ |
| 46 | #define DENALI_IRQ_ALL (INTR_STATUS0__DMA_CMD_COMP | \ |
| 47 | INTR_STATUS0__ECC_TRANSACTION_DONE | \ |
| 48 | INTR_STATUS0__ECC_ERR | \ |
| 49 | INTR_STATUS0__PROGRAM_FAIL | \ |
| 50 | INTR_STATUS0__LOAD_COMP | \ |
| 51 | INTR_STATUS0__PROGRAM_COMP | \ |
| 52 | INTR_STATUS0__TIME_OUT | \ |
| 53 | INTR_STATUS0__ERASE_FAIL | \ |
| 54 | INTR_STATUS0__RST_COMP | \ |
| 55 | INTR_STATUS0__ERASE_COMP) |
| 56 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 57 | /* indicates whether or not the internal value for the flash bank is |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 58 | valid or not */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 59 | #define CHIP_SELECT_INVALID -1 |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 60 | |
| 61 | #define SUPPORT_8BITECC 1 |
| 62 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 63 | /* This macro divides two integers and rounds fractional values up |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 64 | * to the nearest integer value. */ |
| 65 | #define CEIL_DIV(X, Y) (((X)%(Y)) ? ((X)/(Y)+1) : ((X)/(Y))) |
| 66 | |
| 67 | /* this macro allows us to convert from an MTD structure to our own |
| 68 | * device context (denali) structure. |
| 69 | */ |
| 70 | #define mtd_to_denali(m) container_of(m, struct denali_nand_info, mtd) |
| 71 | |
| 72 | /* These constants are defined by the driver to enable common driver |
| 73 | configuration options. */ |
| 74 | #define SPARE_ACCESS 0x41 |
| 75 | #define MAIN_ACCESS 0x42 |
| 76 | #define MAIN_SPARE_ACCESS 0x43 |
| 77 | |
| 78 | #define DENALI_READ 0 |
| 79 | #define DENALI_WRITE 0x100 |
| 80 | |
| 81 | /* types of device accesses. We can issue commands and get status */ |
| 82 | #define COMMAND_CYCLE 0 |
| 83 | #define ADDR_CYCLE 1 |
| 84 | #define STATUS_CYCLE 2 |
| 85 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 86 | /* this is a helper macro that allows us to |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 87 | * format the bank into the proper bits for the controller */ |
| 88 | #define BANK(x) ((x) << 24) |
| 89 | |
| 90 | /* List of platforms this NAND controller has be integrated into */ |
| 91 | static const struct pci_device_id denali_pci_ids[] = { |
| 92 | { PCI_VDEVICE(INTEL, 0x0701), INTEL_CE4100 }, |
| 93 | { PCI_VDEVICE(INTEL, 0x0809), INTEL_MRST }, |
| 94 | { /* end: all zeroes */ } |
| 95 | }; |
| 96 | |
| 97 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 98 | /* these are static lookup tables that give us easy access to |
| 99 | registers in the NAND controller. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 100 | */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 101 | static const uint32_t intr_status_addresses[4] = {INTR_STATUS0, |
| 102 | INTR_STATUS1, |
| 103 | INTR_STATUS2, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 104 | INTR_STATUS3}; |
| 105 | |
| 106 | static const uint32_t device_reset_banks[4] = {DEVICE_RESET__BANK0, |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 107 | DEVICE_RESET__BANK1, |
| 108 | DEVICE_RESET__BANK2, |
| 109 | DEVICE_RESET__BANK3}; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 110 | |
| 111 | static const uint32_t operation_timeout[4] = {INTR_STATUS0__TIME_OUT, |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 112 | INTR_STATUS1__TIME_OUT, |
| 113 | INTR_STATUS2__TIME_OUT, |
| 114 | INTR_STATUS3__TIME_OUT}; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 115 | |
| 116 | static const uint32_t reset_complete[4] = {INTR_STATUS0__RST_COMP, |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 117 | INTR_STATUS1__RST_COMP, |
| 118 | INTR_STATUS2__RST_COMP, |
| 119 | INTR_STATUS3__RST_COMP}; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 120 | |
| 121 | /* specifies the debug level of the driver */ |
| 122 | static int nand_debug_level = 0; |
| 123 | |
| 124 | /* forward declarations */ |
| 125 | static void clear_interrupts(struct denali_nand_info *denali); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 126 | static uint32_t wait_for_irq(struct denali_nand_info *denali, |
| 127 | uint32_t irq_mask); |
| 128 | static void denali_irq_enable(struct denali_nand_info *denali, |
| 129 | uint32_t int_mask); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 130 | static uint32_t read_interrupt_status(struct denali_nand_info *denali); |
| 131 | |
| 132 | #define DEBUG_DENALI 0 |
| 133 | |
| 134 | /* This is a wrapper for writing to the denali registers. |
| 135 | * this allows us to create debug information so we can |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 136 | * observe how the driver is programming the device. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 137 | * it uses standard linux convention for (val, addr) */ |
| 138 | static void denali_write32(uint32_t value, void *addr) |
| 139 | { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 140 | iowrite32(value, addr); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 141 | |
| 142 | #if DEBUG_DENALI |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 143 | printk(KERN_INFO "wrote: 0x%x -> 0x%x\n", value, |
| 144 | (uint32_t)((uint32_t)addr & 0x1fff)); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 145 | #endif |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 146 | } |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 147 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 148 | /* Certain operations for the denali NAND controller use |
| 149 | * an indexed mode to read/write data. The operation is |
| 150 | * performed by writing the address value of the command |
| 151 | * to the device memory followed by the data. This function |
| 152 | * abstracts this common operation. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 153 | */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 154 | static void index_addr(struct denali_nand_info *denali, |
| 155 | uint32_t address, uint32_t data) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 156 | { |
| 157 | denali_write32(address, denali->flash_mem); |
| 158 | denali_write32(data, denali->flash_mem + 0x10); |
| 159 | } |
| 160 | |
| 161 | /* Perform an indexed read of the device */ |
| 162 | static void index_addr_read_data(struct denali_nand_info *denali, |
| 163 | uint32_t address, uint32_t *pdata) |
| 164 | { |
| 165 | denali_write32(address, denali->flash_mem); |
| 166 | *pdata = ioread32(denali->flash_mem + 0x10); |
| 167 | } |
| 168 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 169 | /* 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] | 170 | * The operations manage buffering that data. */ |
| 171 | static void reset_buf(struct denali_nand_info *denali) |
| 172 | { |
| 173 | denali->buf.head = denali->buf.tail = 0; |
| 174 | } |
| 175 | |
| 176 | static void write_byte_to_buf(struct denali_nand_info *denali, uint8_t byte) |
| 177 | { |
| 178 | BUG_ON(denali->buf.tail >= sizeof(denali->buf.buf)); |
| 179 | denali->buf.buf[denali->buf.tail++] = byte; |
| 180 | } |
| 181 | |
| 182 | /* reads the status of the device */ |
| 183 | static void read_status(struct denali_nand_info *denali) |
| 184 | { |
| 185 | uint32_t cmd = 0x0; |
| 186 | |
| 187 | /* initialize the data buffer to store status */ |
| 188 | reset_buf(denali); |
| 189 | |
| 190 | /* initiate a device status read */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 191 | cmd = MODE_11 | BANK(denali->flash_bank); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 192 | index_addr(denali, cmd | COMMAND_CYCLE, 0x70); |
| 193 | denali_write32(cmd | STATUS_CYCLE, denali->flash_mem); |
| 194 | |
| 195 | /* update buffer with status value */ |
| 196 | write_byte_to_buf(denali, ioread32(denali->flash_mem + 0x10)); |
| 197 | |
| 198 | #if DEBUG_DENALI |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 199 | printk(KERN_INFO "device reporting status value of 0x%2x\n", |
| 200 | denali->buf.buf[0]); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 201 | #endif |
| 202 | } |
| 203 | |
| 204 | /* resets a specific device connected to the core */ |
| 205 | static void reset_bank(struct denali_nand_info *denali) |
| 206 | { |
| 207 | uint32_t irq_status = 0; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 208 | uint32_t irq_mask = reset_complete[denali->flash_bank] | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 209 | operation_timeout[denali->flash_bank]; |
| 210 | int bank = 0; |
| 211 | |
| 212 | clear_interrupts(denali); |
| 213 | |
| 214 | bank = device_reset_banks[denali->flash_bank]; |
| 215 | denali_write32(bank, denali->flash_reg + DEVICE_RESET); |
| 216 | |
| 217 | irq_status = wait_for_irq(denali, irq_mask); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 218 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 219 | if (irq_status & operation_timeout[denali->flash_bank]) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 220 | printk(KERN_ERR "reset bank failed.\n"); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* Reset the flash controller */ |
| 224 | static uint16_t NAND_Flash_Reset(struct denali_nand_info *denali) |
| 225 | { |
| 226 | uint32_t i; |
| 227 | |
| 228 | nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n", |
| 229 | __FILE__, __LINE__, __func__); |
| 230 | |
| 231 | for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++) |
| 232 | denali_write32(reset_complete[i] | operation_timeout[i], |
| 233 | denali->flash_reg + intr_status_addresses[i]); |
| 234 | |
| 235 | for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++) { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 236 | denali_write32(device_reset_banks[i], |
| 237 | denali->flash_reg + DEVICE_RESET); |
| 238 | while (!(ioread32(denali->flash_reg + |
| 239 | intr_status_addresses[i]) & |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 240 | (reset_complete[i] | operation_timeout[i]))) |
| 241 | ; |
| 242 | if (ioread32(denali->flash_reg + intr_status_addresses[i]) & |
| 243 | operation_timeout[i]) |
| 244 | nand_dbg_print(NAND_DBG_WARN, |
| 245 | "NAND Reset operation timed out on bank %d\n", i); |
| 246 | } |
| 247 | |
| 248 | for (i = 0; i < LLD_MAX_FLASH_BANKS; i++) |
| 249 | denali_write32(reset_complete[i] | operation_timeout[i], |
| 250 | denali->flash_reg + intr_status_addresses[i]); |
| 251 | |
| 252 | return PASS; |
| 253 | } |
| 254 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 255 | /* this routine calculates the ONFI timing values for a given mode and |
| 256 | * programs the clocking register accordingly. The mode is determined by |
| 257 | * the get_onfi_nand_para routine. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 258 | */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 259 | static void NAND_ONFi_Timing_Mode(struct denali_nand_info *denali, |
| 260 | uint16_t mode) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 261 | { |
| 262 | uint16_t Trea[6] = {40, 30, 25, 20, 20, 16}; |
| 263 | uint16_t Trp[6] = {50, 25, 17, 15, 12, 10}; |
| 264 | uint16_t Treh[6] = {30, 15, 15, 10, 10, 7}; |
| 265 | uint16_t Trc[6] = {100, 50, 35, 30, 25, 20}; |
| 266 | uint16_t Trhoh[6] = {0, 15, 15, 15, 15, 15}; |
| 267 | uint16_t Trloh[6] = {0, 0, 0, 0, 5, 5}; |
| 268 | uint16_t Tcea[6] = {100, 45, 30, 25, 25, 25}; |
| 269 | uint16_t Tadl[6] = {200, 100, 100, 100, 70, 70}; |
| 270 | uint16_t Trhw[6] = {200, 100, 100, 100, 100, 100}; |
| 271 | uint16_t Trhz[6] = {200, 100, 100, 100, 100, 100}; |
| 272 | uint16_t Twhr[6] = {120, 80, 80, 60, 60, 60}; |
| 273 | uint16_t Tcs[6] = {70, 35, 25, 25, 20, 15}; |
| 274 | |
| 275 | uint16_t TclsRising = 1; |
| 276 | uint16_t data_invalid_rhoh, data_invalid_rloh, data_invalid; |
| 277 | uint16_t dv_window = 0; |
| 278 | uint16_t en_lo, en_hi; |
| 279 | uint16_t acc_clks; |
| 280 | uint16_t addr_2_data, re_2_we, re_2_re, we_2_re, cs_cnt; |
| 281 | |
| 282 | nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n", |
| 283 | __FILE__, __LINE__, __func__); |
| 284 | |
| 285 | en_lo = CEIL_DIV(Trp[mode], CLK_X); |
| 286 | en_hi = CEIL_DIV(Treh[mode], CLK_X); |
| 287 | #if ONFI_BLOOM_TIME |
| 288 | if ((en_hi * CLK_X) < (Treh[mode] + 2)) |
| 289 | en_hi++; |
| 290 | #endif |
| 291 | |
| 292 | if ((en_lo + en_hi) * CLK_X < Trc[mode]) |
| 293 | en_lo += CEIL_DIV((Trc[mode] - (en_lo + en_hi) * CLK_X), CLK_X); |
| 294 | |
| 295 | if ((en_lo + en_hi) < CLK_MULTI) |
| 296 | en_lo += CLK_MULTI - en_lo - en_hi; |
| 297 | |
| 298 | while (dv_window < 8) { |
| 299 | data_invalid_rhoh = en_lo * CLK_X + Trhoh[mode]; |
| 300 | |
| 301 | data_invalid_rloh = (en_lo + en_hi) * CLK_X + Trloh[mode]; |
| 302 | |
| 303 | data_invalid = |
| 304 | data_invalid_rhoh < |
| 305 | data_invalid_rloh ? data_invalid_rhoh : data_invalid_rloh; |
| 306 | |
| 307 | dv_window = data_invalid - Trea[mode]; |
| 308 | |
| 309 | if (dv_window < 8) |
| 310 | en_lo++; |
| 311 | } |
| 312 | |
| 313 | acc_clks = CEIL_DIV(Trea[mode], CLK_X); |
| 314 | |
| 315 | while (((acc_clks * CLK_X) - Trea[mode]) < 3) |
| 316 | acc_clks++; |
| 317 | |
| 318 | if ((data_invalid - acc_clks * CLK_X) < 2) |
| 319 | nand_dbg_print(NAND_DBG_WARN, "%s, Line %d: Warning!\n", |
| 320 | __FILE__, __LINE__); |
| 321 | |
| 322 | addr_2_data = CEIL_DIV(Tadl[mode], CLK_X); |
| 323 | re_2_we = CEIL_DIV(Trhw[mode], CLK_X); |
| 324 | re_2_re = CEIL_DIV(Trhz[mode], CLK_X); |
| 325 | we_2_re = CEIL_DIV(Twhr[mode], CLK_X); |
| 326 | cs_cnt = CEIL_DIV((Tcs[mode] - Trp[mode]), CLK_X); |
| 327 | if (!TclsRising) |
| 328 | cs_cnt = CEIL_DIV(Tcs[mode], CLK_X); |
| 329 | if (cs_cnt == 0) |
| 330 | cs_cnt = 1; |
| 331 | |
| 332 | if (Tcea[mode]) { |
| 333 | while (((cs_cnt * CLK_X) + Trea[mode]) < Tcea[mode]) |
| 334 | cs_cnt++; |
| 335 | } |
| 336 | |
| 337 | #if MODE5_WORKAROUND |
| 338 | if (mode == 5) |
| 339 | acc_clks = 5; |
| 340 | #endif |
| 341 | |
| 342 | /* Sighting 3462430: Temporary hack for MT29F128G08CJABAWP:B */ |
| 343 | if ((ioread32(denali->flash_reg + MANUFACTURER_ID) == 0) && |
| 344 | (ioread32(denali->flash_reg + DEVICE_ID) == 0x88)) |
| 345 | acc_clks = 6; |
| 346 | |
| 347 | denali_write32(acc_clks, denali->flash_reg + ACC_CLKS); |
| 348 | denali_write32(re_2_we, denali->flash_reg + RE_2_WE); |
| 349 | denali_write32(re_2_re, denali->flash_reg + RE_2_RE); |
| 350 | denali_write32(we_2_re, denali->flash_reg + WE_2_RE); |
| 351 | denali_write32(addr_2_data, denali->flash_reg + ADDR_2_DATA); |
| 352 | denali_write32(en_lo, denali->flash_reg + RDWR_EN_LO_CNT); |
| 353 | denali_write32(en_hi, denali->flash_reg + RDWR_EN_HI_CNT); |
| 354 | denali_write32(cs_cnt, denali->flash_reg + CS_SETUP_CNT); |
| 355 | } |
| 356 | |
| 357 | /* configures the initial ECC settings for the controller */ |
| 358 | static void set_ecc_config(struct denali_nand_info *denali) |
| 359 | { |
| 360 | #if SUPPORT_8BITECC |
| 361 | if ((ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) < 4096) || |
| 362 | (ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) <= 128)) |
| 363 | denali_write32(8, denali->flash_reg + ECC_CORRECTION); |
| 364 | #endif |
| 365 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 366 | if ((ioread32(denali->flash_reg + ECC_CORRECTION) & |
| 367 | ECC_CORRECTION__VALUE) == 1) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 368 | denali->dev_info.wECCBytesPerSector = 4; |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 369 | denali->dev_info.wECCBytesPerSector *= |
| 370 | denali->dev_info.wDevicesConnected; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 371 | denali->dev_info.wNumPageSpareFlag = |
| 372 | denali->dev_info.wPageSpareSize - |
| 373 | denali->dev_info.wPageDataSize / |
| 374 | (ECC_SECTOR_SIZE * denali->dev_info.wDevicesConnected) * |
| 375 | denali->dev_info.wECCBytesPerSector |
| 376 | - denali->dev_info.wSpareSkipBytes; |
| 377 | } else { |
| 378 | denali->dev_info.wECCBytesPerSector = |
| 379 | (ioread32(denali->flash_reg + ECC_CORRECTION) & |
| 380 | ECC_CORRECTION__VALUE) * 13 / 8; |
| 381 | if ((denali->dev_info.wECCBytesPerSector) % 2 == 0) |
| 382 | denali->dev_info.wECCBytesPerSector += 2; |
| 383 | else |
| 384 | denali->dev_info.wECCBytesPerSector += 1; |
| 385 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 386 | denali->dev_info.wECCBytesPerSector *= |
| 387 | denali->dev_info.wDevicesConnected; |
| 388 | denali->dev_info.wNumPageSpareFlag = |
| 389 | denali->dev_info.wPageSpareSize - |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 390 | denali->dev_info.wPageDataSize / |
| 391 | (ECC_SECTOR_SIZE * denali->dev_info.wDevicesConnected) * |
| 392 | denali->dev_info.wECCBytesPerSector |
| 393 | - denali->dev_info.wSpareSkipBytes; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /* queries the NAND device to see what ONFI modes it supports. */ |
| 398 | static uint16_t get_onfi_nand_para(struct denali_nand_info *denali) |
| 399 | { |
| 400 | int i; |
| 401 | uint16_t blks_lun_l, blks_lun_h, n_of_luns; |
| 402 | uint32_t blockperlun, id; |
| 403 | |
| 404 | denali_write32(DEVICE_RESET__BANK0, denali->flash_reg + DEVICE_RESET); |
| 405 | |
| 406 | while (!((ioread32(denali->flash_reg + INTR_STATUS0) & |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 407 | INTR_STATUS0__RST_COMP) | |
| 408 | (ioread32(denali->flash_reg + INTR_STATUS0) & |
| 409 | INTR_STATUS0__TIME_OUT))) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 410 | ; |
| 411 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 412 | if (ioread32(denali->flash_reg + INTR_STATUS0) & |
| 413 | INTR_STATUS0__RST_COMP) { |
| 414 | denali_write32(DEVICE_RESET__BANK1, |
| 415 | denali->flash_reg + DEVICE_RESET); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 416 | while (!((ioread32(denali->flash_reg + INTR_STATUS1) & |
| 417 | INTR_STATUS1__RST_COMP) | |
| 418 | (ioread32(denali->flash_reg + INTR_STATUS1) & |
| 419 | INTR_STATUS1__TIME_OUT))) |
| 420 | ; |
| 421 | |
| 422 | if (ioread32(denali->flash_reg + INTR_STATUS1) & |
| 423 | INTR_STATUS1__RST_COMP) { |
| 424 | denali_write32(DEVICE_RESET__BANK2, |
| 425 | denali->flash_reg + DEVICE_RESET); |
| 426 | while (!((ioread32(denali->flash_reg + INTR_STATUS2) & |
| 427 | INTR_STATUS2__RST_COMP) | |
| 428 | (ioread32(denali->flash_reg + INTR_STATUS2) & |
| 429 | INTR_STATUS2__TIME_OUT))) |
| 430 | ; |
| 431 | |
| 432 | if (ioread32(denali->flash_reg + INTR_STATUS2) & |
| 433 | INTR_STATUS2__RST_COMP) { |
| 434 | denali_write32(DEVICE_RESET__BANK3, |
| 435 | denali->flash_reg + DEVICE_RESET); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 436 | while (!((ioread32(denali->flash_reg + |
| 437 | INTR_STATUS3) & |
| 438 | INTR_STATUS3__RST_COMP) | |
| 439 | (ioread32(denali->flash_reg + |
| 440 | INTR_STATUS3) & |
| 441 | INTR_STATUS3__TIME_OUT))) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 442 | ; |
| 443 | } else { |
| 444 | printk(KERN_ERR "Getting a time out for bank 2!\n"); |
| 445 | } |
| 446 | } else { |
| 447 | printk(KERN_ERR "Getting a time out for bank 1!\n"); |
| 448 | } |
| 449 | } |
| 450 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 451 | denali_write32(INTR_STATUS0__TIME_OUT, |
| 452 | denali->flash_reg + INTR_STATUS0); |
| 453 | denali_write32(INTR_STATUS1__TIME_OUT, |
| 454 | denali->flash_reg + INTR_STATUS1); |
| 455 | denali_write32(INTR_STATUS2__TIME_OUT, |
| 456 | denali->flash_reg + INTR_STATUS2); |
| 457 | denali_write32(INTR_STATUS3__TIME_OUT, |
| 458 | denali->flash_reg + INTR_STATUS3); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 459 | |
| 460 | denali->dev_info.wONFIDevFeatures = |
| 461 | ioread32(denali->flash_reg + ONFI_DEVICE_FEATURES); |
| 462 | denali->dev_info.wONFIOptCommands = |
| 463 | ioread32(denali->flash_reg + ONFI_OPTIONAL_COMMANDS); |
| 464 | denali->dev_info.wONFITimingMode = |
| 465 | ioread32(denali->flash_reg + ONFI_TIMING_MODE); |
| 466 | denali->dev_info.wONFIPgmCacheTimingMode = |
| 467 | ioread32(denali->flash_reg + ONFI_PGM_CACHE_TIMING_MODE); |
| 468 | |
| 469 | n_of_luns = ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) & |
| 470 | ONFI_DEVICE_NO_OF_LUNS__NO_OF_LUNS; |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 471 | blks_lun_l = ioread32(denali->flash_reg + |
| 472 | ONFI_DEVICE_NO_OF_BLOCKS_PER_LUN_L); |
| 473 | blks_lun_h = ioread32(denali->flash_reg + |
| 474 | ONFI_DEVICE_NO_OF_BLOCKS_PER_LUN_U); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 475 | |
| 476 | blockperlun = (blks_lun_h << 16) | blks_lun_l; |
| 477 | |
| 478 | denali->dev_info.wTotalBlocks = n_of_luns * blockperlun; |
| 479 | |
| 480 | if (!(ioread32(denali->flash_reg + ONFI_TIMING_MODE) & |
| 481 | ONFI_TIMING_MODE__VALUE)) |
| 482 | return FAIL; |
| 483 | |
| 484 | for (i = 5; i > 0; i--) { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 485 | if (ioread32(denali->flash_reg + ONFI_TIMING_MODE) & |
| 486 | (0x01 << i)) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 487 | break; |
| 488 | } |
| 489 | |
| 490 | NAND_ONFi_Timing_Mode(denali, i); |
| 491 | |
| 492 | index_addr(denali, MODE_11 | 0, 0x90); |
| 493 | index_addr(denali, MODE_11 | 1, 0); |
| 494 | |
| 495 | for (i = 0; i < 3; i++) |
| 496 | index_addr_read_data(denali, MODE_11 | 2, &id); |
| 497 | |
| 498 | nand_dbg_print(NAND_DBG_DEBUG, "3rd ID: 0x%x\n", id); |
| 499 | |
| 500 | denali->dev_info.MLCDevice = id & 0x0C; |
| 501 | |
| 502 | /* By now, all the ONFI devices we know support the page cache */ |
| 503 | /* rw feature. So here we enable the pipeline_rw_ahead feature */ |
| 504 | /* iowrite32(1, denali->flash_reg + CACHE_WRITE_ENABLE); */ |
| 505 | /* iowrite32(1, denali->flash_reg + CACHE_READ_ENABLE); */ |
| 506 | |
| 507 | return PASS; |
| 508 | } |
| 509 | |
| 510 | static void get_samsung_nand_para(struct denali_nand_info *denali) |
| 511 | { |
| 512 | uint8_t no_of_planes; |
| 513 | uint32_t blk_size; |
| 514 | uint64_t plane_size, capacity; |
| 515 | uint32_t id_bytes[5]; |
| 516 | int i; |
| 517 | |
| 518 | index_addr(denali, (uint32_t)(MODE_11 | 0), 0x90); |
| 519 | index_addr(denali, (uint32_t)(MODE_11 | 1), 0); |
| 520 | for (i = 0; i < 5; i++) |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 521 | index_addr_read_data(denali, (uint32_t)(MODE_11 | 2), |
| 522 | &id_bytes[i]); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 523 | |
| 524 | nand_dbg_print(NAND_DBG_DEBUG, |
| 525 | "ID bytes: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", |
| 526 | id_bytes[0], id_bytes[1], id_bytes[2], |
| 527 | id_bytes[3], id_bytes[4]); |
| 528 | |
| 529 | if ((id_bytes[1] & 0xff) == 0xd3) { /* Samsung K9WAG08U1A */ |
| 530 | /* Set timing register values according to datasheet */ |
| 531 | denali_write32(5, denali->flash_reg + ACC_CLKS); |
| 532 | denali_write32(20, denali->flash_reg + RE_2_WE); |
| 533 | denali_write32(12, denali->flash_reg + WE_2_RE); |
| 534 | denali_write32(14, denali->flash_reg + ADDR_2_DATA); |
| 535 | denali_write32(3, denali->flash_reg + RDWR_EN_LO_CNT); |
| 536 | denali_write32(2, denali->flash_reg + RDWR_EN_HI_CNT); |
| 537 | denali_write32(2, denali->flash_reg + CS_SETUP_CNT); |
| 538 | } |
| 539 | |
| 540 | no_of_planes = 1 << ((id_bytes[4] & 0x0c) >> 2); |
| 541 | plane_size = (uint64_t)64 << ((id_bytes[4] & 0x70) >> 4); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 542 | blk_size = 64 << ((ioread32(denali->flash_reg + DEVICE_PARAM_1) & |
| 543 | 0x30) >> 4); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 544 | capacity = (uint64_t)128 * plane_size * no_of_planes; |
| 545 | |
| 546 | do_div(capacity, blk_size); |
| 547 | denali->dev_info.wTotalBlocks = capacity; |
| 548 | } |
| 549 | |
| 550 | static void get_toshiba_nand_para(struct denali_nand_info *denali) |
| 551 | { |
| 552 | void __iomem *scratch_reg; |
| 553 | uint32_t tmp; |
| 554 | |
| 555 | /* Workaround to fix a controller bug which reports a wrong */ |
| 556 | /* spare area size for some kind of Toshiba NAND device */ |
| 557 | if ((ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) == 4096) && |
| 558 | (ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) == 64)) { |
| 559 | denali_write32(216, denali->flash_reg + DEVICE_SPARE_AREA_SIZE); |
| 560 | tmp = ioread32(denali->flash_reg + DEVICES_CONNECTED) * |
| 561 | ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 562 | denali_write32(tmp, |
| 563 | denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 564 | #if SUPPORT_15BITECC |
| 565 | denali_write32(15, denali->flash_reg + ECC_CORRECTION); |
| 566 | #elif SUPPORT_8BITECC |
| 567 | denali_write32(8, denali->flash_reg + ECC_CORRECTION); |
| 568 | #endif |
| 569 | } |
| 570 | |
| 571 | /* As Toshiba NAND can not provide it's block number, */ |
| 572 | /* so here we need user to provide the correct block */ |
| 573 | /* number in a scratch register before the Linux NAND */ |
| 574 | /* driver is loaded. If no valid value found in the scratch */ |
| 575 | /* register, then we use default block number value */ |
| 576 | scratch_reg = ioremap_nocache(SCRATCH_REG_ADDR, SCRATCH_REG_SIZE); |
| 577 | if (!scratch_reg) { |
| 578 | printk(KERN_ERR "Spectra: ioremap failed in %s, Line %d", |
| 579 | __FILE__, __LINE__); |
| 580 | denali->dev_info.wTotalBlocks = GLOB_HWCTL_DEFAULT_BLKS; |
| 581 | } else { |
| 582 | nand_dbg_print(NAND_DBG_WARN, |
| 583 | "Spectra: ioremap reg address: 0x%p\n", scratch_reg); |
| 584 | denali->dev_info.wTotalBlocks = 1 << ioread8(scratch_reg); |
| 585 | if (denali->dev_info.wTotalBlocks < 512) |
| 586 | denali->dev_info.wTotalBlocks = GLOB_HWCTL_DEFAULT_BLKS; |
| 587 | iounmap(scratch_reg); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | static void get_hynix_nand_para(struct denali_nand_info *denali) |
| 592 | { |
| 593 | void __iomem *scratch_reg; |
| 594 | uint32_t main_size, spare_size; |
| 595 | |
| 596 | switch (denali->dev_info.wDeviceID) { |
| 597 | case 0xD5: /* Hynix H27UAG8T2A, H27UBG8U5A or H27UCG8VFA */ |
| 598 | case 0xD7: /* Hynix H27UDG8VEM, H27UCG8UDM or H27UCG8V5A */ |
| 599 | denali_write32(128, denali->flash_reg + PAGES_PER_BLOCK); |
| 600 | denali_write32(4096, denali->flash_reg + DEVICE_MAIN_AREA_SIZE); |
| 601 | denali_write32(224, denali->flash_reg + DEVICE_SPARE_AREA_SIZE); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 602 | main_size = 4096 * |
| 603 | ioread32(denali->flash_reg + DEVICES_CONNECTED); |
| 604 | spare_size = 224 * |
| 605 | ioread32(denali->flash_reg + DEVICES_CONNECTED); |
| 606 | denali_write32(main_size, |
| 607 | denali->flash_reg + LOGICAL_PAGE_DATA_SIZE); |
| 608 | denali_write32(spare_size, |
| 609 | denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 610 | denali_write32(0, denali->flash_reg + DEVICE_WIDTH); |
| 611 | #if SUPPORT_15BITECC |
| 612 | denali_write32(15, denali->flash_reg + ECC_CORRECTION); |
| 613 | #elif SUPPORT_8BITECC |
| 614 | denali_write32(8, denali->flash_reg + ECC_CORRECTION); |
| 615 | #endif |
| 616 | denali->dev_info.MLCDevice = 1; |
| 617 | break; |
| 618 | default: |
| 619 | nand_dbg_print(NAND_DBG_WARN, |
| 620 | "Spectra: Unknown Hynix NAND (Device ID: 0x%x)." |
| 621 | "Will use default parameter values instead.\n", |
| 622 | denali->dev_info.wDeviceID); |
| 623 | } |
| 624 | |
| 625 | scratch_reg = ioremap_nocache(SCRATCH_REG_ADDR, SCRATCH_REG_SIZE); |
| 626 | if (!scratch_reg) { |
| 627 | printk(KERN_ERR "Spectra: ioremap failed in %s, Line %d", |
| 628 | __FILE__, __LINE__); |
| 629 | denali->dev_info.wTotalBlocks = GLOB_HWCTL_DEFAULT_BLKS; |
| 630 | } else { |
| 631 | nand_dbg_print(NAND_DBG_WARN, |
| 632 | "Spectra: ioremap reg address: 0x%p\n", scratch_reg); |
| 633 | denali->dev_info.wTotalBlocks = 1 << ioread8(scratch_reg); |
| 634 | if (denali->dev_info.wTotalBlocks < 512) |
| 635 | denali->dev_info.wTotalBlocks = GLOB_HWCTL_DEFAULT_BLKS; |
| 636 | iounmap(scratch_reg); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /* determines how many NAND chips are connected to the controller. Note for |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 641 | Intel CE4100 devices we don't support more than one device. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 642 | */ |
| 643 | static void find_valid_banks(struct denali_nand_info *denali) |
| 644 | { |
| 645 | uint32_t id[LLD_MAX_FLASH_BANKS]; |
| 646 | int i; |
| 647 | |
| 648 | denali->total_used_banks = 1; |
| 649 | for (i = 0; i < LLD_MAX_FLASH_BANKS; i++) { |
| 650 | index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 0), 0x90); |
| 651 | index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 1), 0); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 652 | index_addr_read_data(denali, |
| 653 | (uint32_t)(MODE_11 | (i << 24) | 2), &id[i]); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 654 | |
| 655 | nand_dbg_print(NAND_DBG_DEBUG, |
| 656 | "Return 1st ID for bank[%d]: %x\n", i, id[i]); |
| 657 | |
| 658 | if (i == 0) { |
| 659 | if (!(id[i] & 0x0ff)) |
| 660 | break; /* WTF? */ |
| 661 | } else { |
| 662 | if ((id[i] & 0x0ff) == (id[0] & 0x0ff)) |
| 663 | denali->total_used_banks++; |
| 664 | else |
| 665 | break; |
| 666 | } |
| 667 | } |
| 668 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 669 | if (denali->platform == INTEL_CE4100) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 670 | /* Platform limitations of the CE4100 device limit |
| 671 | * users to a single chip solution for NAND. |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 672 | * Multichip support is not enabled. |
| 673 | */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 674 | if (denali->total_used_banks != 1) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 675 | printk(KERN_ERR "Sorry, Intel CE4100 only supports " |
| 676 | "a single NAND device.\n"); |
| 677 | BUG(); |
| 678 | } |
| 679 | } |
| 680 | nand_dbg_print(NAND_DBG_DEBUG, |
| 681 | "denali->total_used_banks: %d\n", denali->total_used_banks); |
| 682 | } |
| 683 | |
| 684 | static void detect_partition_feature(struct denali_nand_info *denali) |
| 685 | { |
| 686 | if (ioread32(denali->flash_reg + FEATURES) & FEATURES__PARTITION) { |
| 687 | if ((ioread32(denali->flash_reg + PERM_SRC_ID_1) & |
| 688 | PERM_SRC_ID_1__SRCID) == SPECTRA_PARTITION_ID) { |
| 689 | denali->dev_info.wSpectraStartBlock = |
| 690 | ((ioread32(denali->flash_reg + MIN_MAX_BANK_1) & |
| 691 | MIN_MAX_BANK_1__MIN_VALUE) * |
| 692 | denali->dev_info.wTotalBlocks) |
| 693 | + |
| 694 | (ioread32(denali->flash_reg + MIN_BLK_ADDR_1) & |
| 695 | MIN_BLK_ADDR_1__VALUE); |
| 696 | |
| 697 | denali->dev_info.wSpectraEndBlock = |
| 698 | (((ioread32(denali->flash_reg + MIN_MAX_BANK_1) & |
| 699 | MIN_MAX_BANK_1__MAX_VALUE) >> 2) * |
| 700 | denali->dev_info.wTotalBlocks) |
| 701 | + |
| 702 | (ioread32(denali->flash_reg + MAX_BLK_ADDR_1) & |
| 703 | MAX_BLK_ADDR_1__VALUE); |
| 704 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 705 | denali->dev_info.wTotalBlocks *= |
| 706 | denali->total_used_banks; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 707 | |
| 708 | if (denali->dev_info.wSpectraEndBlock >= |
| 709 | denali->dev_info.wTotalBlocks) { |
| 710 | denali->dev_info.wSpectraEndBlock = |
| 711 | denali->dev_info.wTotalBlocks - 1; |
| 712 | } |
| 713 | |
| 714 | denali->dev_info.wDataBlockNum = |
| 715 | denali->dev_info.wSpectraEndBlock - |
| 716 | denali->dev_info.wSpectraStartBlock + 1; |
| 717 | } else { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 718 | denali->dev_info.wTotalBlocks *= |
| 719 | denali->total_used_banks; |
| 720 | denali->dev_info.wSpectraStartBlock = |
| 721 | SPECTRA_START_BLOCK; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 722 | denali->dev_info.wSpectraEndBlock = |
| 723 | denali->dev_info.wTotalBlocks - 1; |
| 724 | denali->dev_info.wDataBlockNum = |
| 725 | denali->dev_info.wSpectraEndBlock - |
| 726 | denali->dev_info.wSpectraStartBlock + 1; |
| 727 | } |
| 728 | } else { |
| 729 | denali->dev_info.wTotalBlocks *= denali->total_used_banks; |
| 730 | denali->dev_info.wSpectraStartBlock = SPECTRA_START_BLOCK; |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 731 | denali->dev_info.wSpectraEndBlock = |
| 732 | denali->dev_info.wTotalBlocks - 1; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 733 | denali->dev_info.wDataBlockNum = |
| 734 | denali->dev_info.wSpectraEndBlock - |
| 735 | denali->dev_info.wSpectraStartBlock + 1; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | static void dump_device_info(struct denali_nand_info *denali) |
| 740 | { |
| 741 | nand_dbg_print(NAND_DBG_DEBUG, "denali->dev_info:\n"); |
| 742 | nand_dbg_print(NAND_DBG_DEBUG, "DeviceMaker: 0x%x\n", |
| 743 | denali->dev_info.wDeviceMaker); |
| 744 | nand_dbg_print(NAND_DBG_DEBUG, "DeviceID: 0x%x\n", |
| 745 | denali->dev_info.wDeviceID); |
| 746 | nand_dbg_print(NAND_DBG_DEBUG, "DeviceType: 0x%x\n", |
| 747 | denali->dev_info.wDeviceType); |
| 748 | nand_dbg_print(NAND_DBG_DEBUG, "SpectraStartBlock: %d\n", |
| 749 | denali->dev_info.wSpectraStartBlock); |
| 750 | nand_dbg_print(NAND_DBG_DEBUG, "SpectraEndBlock: %d\n", |
| 751 | denali->dev_info.wSpectraEndBlock); |
| 752 | nand_dbg_print(NAND_DBG_DEBUG, "TotalBlocks: %d\n", |
| 753 | denali->dev_info.wTotalBlocks); |
| 754 | nand_dbg_print(NAND_DBG_DEBUG, "PagesPerBlock: %d\n", |
| 755 | denali->dev_info.wPagesPerBlock); |
| 756 | nand_dbg_print(NAND_DBG_DEBUG, "PageSize: %d\n", |
| 757 | denali->dev_info.wPageSize); |
| 758 | nand_dbg_print(NAND_DBG_DEBUG, "PageDataSize: %d\n", |
| 759 | denali->dev_info.wPageDataSize); |
| 760 | nand_dbg_print(NAND_DBG_DEBUG, "PageSpareSize: %d\n", |
| 761 | denali->dev_info.wPageSpareSize); |
| 762 | nand_dbg_print(NAND_DBG_DEBUG, "NumPageSpareFlag: %d\n", |
| 763 | denali->dev_info.wNumPageSpareFlag); |
| 764 | nand_dbg_print(NAND_DBG_DEBUG, "ECCBytesPerSector: %d\n", |
| 765 | denali->dev_info.wECCBytesPerSector); |
| 766 | nand_dbg_print(NAND_DBG_DEBUG, "BlockSize: %d\n", |
| 767 | denali->dev_info.wBlockSize); |
| 768 | nand_dbg_print(NAND_DBG_DEBUG, "BlockDataSize: %d\n", |
| 769 | denali->dev_info.wBlockDataSize); |
| 770 | nand_dbg_print(NAND_DBG_DEBUG, "DataBlockNum: %d\n", |
| 771 | denali->dev_info.wDataBlockNum); |
| 772 | nand_dbg_print(NAND_DBG_DEBUG, "PlaneNum: %d\n", |
| 773 | denali->dev_info.bPlaneNum); |
| 774 | nand_dbg_print(NAND_DBG_DEBUG, "DeviceMainAreaSize: %d\n", |
| 775 | denali->dev_info.wDeviceMainAreaSize); |
| 776 | nand_dbg_print(NAND_DBG_DEBUG, "DeviceSpareAreaSize: %d\n", |
| 777 | denali->dev_info.wDeviceSpareAreaSize); |
| 778 | nand_dbg_print(NAND_DBG_DEBUG, "DevicesConnected: %d\n", |
| 779 | denali->dev_info.wDevicesConnected); |
| 780 | nand_dbg_print(NAND_DBG_DEBUG, "DeviceWidth: %d\n", |
| 781 | denali->dev_info.wDeviceWidth); |
| 782 | nand_dbg_print(NAND_DBG_DEBUG, "HWRevision: 0x%x\n", |
| 783 | denali->dev_info.wHWRevision); |
| 784 | nand_dbg_print(NAND_DBG_DEBUG, "HWFeatures: 0x%x\n", |
| 785 | denali->dev_info.wHWFeatures); |
| 786 | nand_dbg_print(NAND_DBG_DEBUG, "ONFIDevFeatures: 0x%x\n", |
| 787 | denali->dev_info.wONFIDevFeatures); |
| 788 | nand_dbg_print(NAND_DBG_DEBUG, "ONFIOptCommands: 0x%x\n", |
| 789 | denali->dev_info.wONFIOptCommands); |
| 790 | nand_dbg_print(NAND_DBG_DEBUG, "ONFITimingMode: 0x%x\n", |
| 791 | denali->dev_info.wONFITimingMode); |
| 792 | nand_dbg_print(NAND_DBG_DEBUG, "ONFIPgmCacheTimingMode: 0x%x\n", |
| 793 | denali->dev_info.wONFIPgmCacheTimingMode); |
| 794 | nand_dbg_print(NAND_DBG_DEBUG, "MLCDevice: %s\n", |
| 795 | denali->dev_info.MLCDevice ? "Yes" : "No"); |
| 796 | nand_dbg_print(NAND_DBG_DEBUG, "SpareSkipBytes: %d\n", |
| 797 | denali->dev_info.wSpareSkipBytes); |
| 798 | nand_dbg_print(NAND_DBG_DEBUG, "BitsInPageNumber: %d\n", |
| 799 | denali->dev_info.nBitsInPageNumber); |
| 800 | nand_dbg_print(NAND_DBG_DEBUG, "BitsInPageDataSize: %d\n", |
| 801 | denali->dev_info.nBitsInPageDataSize); |
| 802 | nand_dbg_print(NAND_DBG_DEBUG, "BitsInBlockDataSize: %d\n", |
| 803 | denali->dev_info.nBitsInBlockDataSize); |
| 804 | } |
| 805 | |
| 806 | static uint16_t NAND_Read_Device_ID(struct denali_nand_info *denali) |
| 807 | { |
| 808 | uint16_t status = PASS; |
| 809 | uint8_t no_of_planes; |
| 810 | |
| 811 | nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n", |
| 812 | __FILE__, __LINE__, __func__); |
| 813 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 814 | denali->dev_info.wDeviceMaker = |
| 815 | ioread32(denali->flash_reg + MANUFACTURER_ID); |
| 816 | denali->dev_info.wDeviceID = |
| 817 | ioread32(denali->flash_reg + DEVICE_ID); |
| 818 | denali->dev_info.bDeviceParam0 = |
| 819 | ioread32(denali->flash_reg + DEVICE_PARAM_0); |
| 820 | denali->dev_info.bDeviceParam1 = |
| 821 | ioread32(denali->flash_reg + DEVICE_PARAM_1); |
| 822 | denali->dev_info.bDeviceParam2 = |
| 823 | ioread32(denali->flash_reg + DEVICE_PARAM_2); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 824 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 825 | denali->dev_info.MLCDevice = |
| 826 | ioread32(denali->flash_reg + DEVICE_PARAM_0) & 0x0c; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 827 | |
| 828 | if (ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) & |
| 829 | ONFI_DEVICE_NO_OF_LUNS__ONFI_DEVICE) { /* ONFI 1.0 NAND */ |
| 830 | if (FAIL == get_onfi_nand_para(denali)) |
| 831 | return FAIL; |
| 832 | } else if (denali->dev_info.wDeviceMaker == 0xEC) { /* Samsung NAND */ |
| 833 | get_samsung_nand_para(denali); |
| 834 | } else if (denali->dev_info.wDeviceMaker == 0x98) { /* Toshiba NAND */ |
| 835 | get_toshiba_nand_para(denali); |
| 836 | } else if (denali->dev_info.wDeviceMaker == 0xAD) { /* Hynix NAND */ |
| 837 | get_hynix_nand_para(denali); |
| 838 | } else { |
| 839 | denali->dev_info.wTotalBlocks = GLOB_HWCTL_DEFAULT_BLKS; |
| 840 | } |
| 841 | |
| 842 | nand_dbg_print(NAND_DBG_DEBUG, "Dump timing register values:" |
| 843 | "acc_clks: %d, re_2_we: %d, we_2_re: %d," |
| 844 | "addr_2_data: %d, rdwr_en_lo_cnt: %d, " |
| 845 | "rdwr_en_hi_cnt: %d, cs_setup_cnt: %d\n", |
| 846 | ioread32(denali->flash_reg + ACC_CLKS), |
| 847 | ioread32(denali->flash_reg + RE_2_WE), |
| 848 | ioread32(denali->flash_reg + WE_2_RE), |
| 849 | ioread32(denali->flash_reg + ADDR_2_DATA), |
| 850 | ioread32(denali->flash_reg + RDWR_EN_LO_CNT), |
| 851 | ioread32(denali->flash_reg + RDWR_EN_HI_CNT), |
| 852 | ioread32(denali->flash_reg + CS_SETUP_CNT)); |
| 853 | |
| 854 | denali->dev_info.wHWRevision = ioread32(denali->flash_reg + REVISION); |
| 855 | denali->dev_info.wHWFeatures = ioread32(denali->flash_reg + FEATURES); |
| 856 | |
| 857 | denali->dev_info.wDeviceMainAreaSize = |
| 858 | ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE); |
| 859 | denali->dev_info.wDeviceSpareAreaSize = |
| 860 | ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE); |
| 861 | |
| 862 | denali->dev_info.wPageDataSize = |
| 863 | ioread32(denali->flash_reg + LOGICAL_PAGE_DATA_SIZE); |
| 864 | |
| 865 | /* Note: When using the Micon 4K NAND device, the controller will report |
| 866 | * Page Spare Size as 216 bytes. But Micron's Spec say it's 218 bytes. |
| 867 | * And if force set it to 218 bytes, the controller can not work |
| 868 | * correctly. So just let it be. But keep in mind that this bug may |
| 869 | * cause |
| 870 | * other problems in future. - Yunpeng 2008-10-10 |
| 871 | */ |
| 872 | denali->dev_info.wPageSpareSize = |
| 873 | ioread32(denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE); |
| 874 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 875 | denali->dev_info.wPagesPerBlock = |
| 876 | ioread32(denali->flash_reg + PAGES_PER_BLOCK); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 877 | |
| 878 | denali->dev_info.wPageSize = |
| 879 | denali->dev_info.wPageDataSize + denali->dev_info.wPageSpareSize; |
| 880 | denali->dev_info.wBlockSize = |
| 881 | denali->dev_info.wPageSize * denali->dev_info.wPagesPerBlock; |
| 882 | denali->dev_info.wBlockDataSize = |
| 883 | denali->dev_info.wPagesPerBlock * denali->dev_info.wPageDataSize; |
| 884 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 885 | denali->dev_info.wDeviceWidth = |
| 886 | ioread32(denali->flash_reg + DEVICE_WIDTH); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 887 | denali->dev_info.wDeviceType = |
| 888 | ((ioread32(denali->flash_reg + DEVICE_WIDTH) > 0) ? 16 : 8); |
| 889 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 890 | denali->dev_info.wDevicesConnected = |
| 891 | ioread32(denali->flash_reg + DEVICES_CONNECTED); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 892 | |
| 893 | denali->dev_info.wSpareSkipBytes = |
| 894 | ioread32(denali->flash_reg + SPARE_AREA_SKIP_BYTES) * |
| 895 | denali->dev_info.wDevicesConnected; |
| 896 | |
| 897 | denali->dev_info.nBitsInPageNumber = |
| 898 | ilog2(denali->dev_info.wPagesPerBlock); |
| 899 | denali->dev_info.nBitsInPageDataSize = |
| 900 | ilog2(denali->dev_info.wPageDataSize); |
| 901 | denali->dev_info.nBitsInBlockDataSize = |
| 902 | ilog2(denali->dev_info.wBlockDataSize); |
| 903 | |
| 904 | set_ecc_config(denali); |
| 905 | |
| 906 | no_of_planes = ioread32(denali->flash_reg + NUMBER_OF_PLANES) & |
| 907 | NUMBER_OF_PLANES__VALUE; |
| 908 | |
| 909 | switch (no_of_planes) { |
| 910 | case 0: |
| 911 | case 1: |
| 912 | case 3: |
| 913 | case 7: |
| 914 | denali->dev_info.bPlaneNum = no_of_planes + 1; |
| 915 | break; |
| 916 | default: |
| 917 | status = FAIL; |
| 918 | break; |
| 919 | } |
| 920 | |
| 921 | find_valid_banks(denali); |
| 922 | |
| 923 | detect_partition_feature(denali); |
| 924 | |
| 925 | dump_device_info(denali); |
| 926 | |
| 927 | /* If the user specified to override the default timings |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 928 | * with a specific ONFI mode, we apply those changes here. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 929 | */ |
| 930 | if (onfi_timing_mode != NAND_DEFAULT_TIMINGS) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 931 | NAND_ONFi_Timing_Mode(denali, onfi_timing_mode); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 932 | |
| 933 | return status; |
| 934 | } |
| 935 | |
| 936 | static void NAND_LLD_Enable_Disable_Interrupts(struct denali_nand_info *denali, |
| 937 | uint16_t INT_ENABLE) |
| 938 | { |
| 939 | nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n", |
| 940 | __FILE__, __LINE__, __func__); |
| 941 | |
| 942 | if (INT_ENABLE) |
| 943 | denali_write32(1, denali->flash_reg + GLOBAL_INT_ENABLE); |
| 944 | else |
| 945 | denali_write32(0, denali->flash_reg + GLOBAL_INT_ENABLE); |
| 946 | } |
| 947 | |
| 948 | /* validation function to verify that the controlling software is making |
| 949 | a valid request |
| 950 | */ |
| 951 | static inline bool is_flash_bank_valid(int flash_bank) |
| 952 | { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 953 | return (flash_bank >= 0 && flash_bank < 4); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | static void denali_irq_init(struct denali_nand_info *denali) |
| 957 | { |
| 958 | uint32_t int_mask = 0; |
| 959 | |
| 960 | /* Disable global interrupts */ |
| 961 | NAND_LLD_Enable_Disable_Interrupts(denali, false); |
| 962 | |
| 963 | int_mask = DENALI_IRQ_ALL; |
| 964 | |
| 965 | /* Clear all status bits */ |
| 966 | denali_write32(0xFFFF, denali->flash_reg + INTR_STATUS0); |
| 967 | denali_write32(0xFFFF, denali->flash_reg + INTR_STATUS1); |
| 968 | denali_write32(0xFFFF, denali->flash_reg + INTR_STATUS2); |
| 969 | denali_write32(0xFFFF, denali->flash_reg + INTR_STATUS3); |
| 970 | |
| 971 | denali_irq_enable(denali, int_mask); |
| 972 | } |
| 973 | |
| 974 | static void denali_irq_cleanup(int irqnum, struct denali_nand_info *denali) |
| 975 | { |
| 976 | NAND_LLD_Enable_Disable_Interrupts(denali, false); |
| 977 | free_irq(irqnum, denali); |
| 978 | } |
| 979 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 980 | static void denali_irq_enable(struct denali_nand_info *denali, |
| 981 | uint32_t int_mask) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 982 | { |
| 983 | denali_write32(int_mask, denali->flash_reg + INTR_EN0); |
| 984 | denali_write32(int_mask, denali->flash_reg + INTR_EN1); |
| 985 | denali_write32(int_mask, denali->flash_reg + INTR_EN2); |
| 986 | denali_write32(int_mask, denali->flash_reg + INTR_EN3); |
| 987 | } |
| 988 | |
| 989 | /* This function only returns when an interrupt that this driver cares about |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 990 | * occurs. This is to reduce the overhead of servicing interrupts |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 991 | */ |
| 992 | static inline uint32_t denali_irq_detected(struct denali_nand_info *denali) |
| 993 | { |
| 994 | return (read_interrupt_status(denali) & DENALI_IRQ_ALL); |
| 995 | } |
| 996 | |
| 997 | /* Interrupts are cleared by writing a 1 to the appropriate status bit */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 998 | static inline void clear_interrupt(struct denali_nand_info *denali, |
| 999 | uint32_t irq_mask) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1000 | { |
| 1001 | uint32_t intr_status_reg = 0; |
| 1002 | |
| 1003 | intr_status_reg = intr_status_addresses[denali->flash_bank]; |
| 1004 | |
| 1005 | denali_write32(irq_mask, denali->flash_reg + intr_status_reg); |
| 1006 | } |
| 1007 | |
| 1008 | static void clear_interrupts(struct denali_nand_info *denali) |
| 1009 | { |
| 1010 | uint32_t status = 0x0; |
| 1011 | spin_lock_irq(&denali->irq_lock); |
| 1012 | |
| 1013 | status = read_interrupt_status(denali); |
| 1014 | |
| 1015 | #if DEBUG_DENALI |
| 1016 | denali->irq_debug_array[denali->idx++] = 0x30000000 | status; |
| 1017 | denali->idx %= 32; |
| 1018 | #endif |
| 1019 | |
| 1020 | denali->irq_status = 0x0; |
| 1021 | spin_unlock_irq(&denali->irq_lock); |
| 1022 | } |
| 1023 | |
| 1024 | static uint32_t read_interrupt_status(struct denali_nand_info *denali) |
| 1025 | { |
| 1026 | uint32_t intr_status_reg = 0; |
| 1027 | |
| 1028 | intr_status_reg = intr_status_addresses[denali->flash_bank]; |
| 1029 | |
| 1030 | return ioread32(denali->flash_reg + intr_status_reg); |
| 1031 | } |
| 1032 | |
| 1033 | #if DEBUG_DENALI |
| 1034 | static void print_irq_log(struct denali_nand_info *denali) |
| 1035 | { |
| 1036 | int i = 0; |
| 1037 | |
Chuanxiao Dong | bf1806d | 2010-07-27 10:48:34 +0800 | [diff] [blame] | 1038 | printk(KERN_INFO "ISR debug log index = %X\n", denali->idx); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1039 | for (i = 0; i < 32; i++) |
Chuanxiao Dong | bf1806d | 2010-07-27 10:48:34 +0800 | [diff] [blame] | 1040 | printk(KERN_INFO "%08X: %08X\n", i, denali->irq_debug_array[i]); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1041 | } |
| 1042 | #endif |
| 1043 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1044 | /* This is the interrupt service routine. It handles all interrupts |
| 1045 | * sent to this device. Note that on CE4100, this is a shared |
| 1046 | * interrupt. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1047 | */ |
| 1048 | static irqreturn_t denali_isr(int irq, void *dev_id) |
| 1049 | { |
| 1050 | struct denali_nand_info *denali = dev_id; |
| 1051 | uint32_t irq_status = 0x0; |
| 1052 | irqreturn_t result = IRQ_NONE; |
| 1053 | |
| 1054 | spin_lock(&denali->irq_lock); |
| 1055 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1056 | /* check to see if a valid NAND chip has |
| 1057 | * been selected. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1058 | */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1059 | if (is_flash_bank_valid(denali->flash_bank)) { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1060 | /* check to see if controller generated |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1061 | * the interrupt, since this is a shared interrupt */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1062 | irq_status = denali_irq_detected(denali); |
| 1063 | if (irq_status != 0) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1064 | #if DEBUG_DENALI |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1065 | denali->irq_debug_array[denali->idx++] = |
| 1066 | 0x10000000 | irq_status; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1067 | denali->idx %= 32; |
| 1068 | |
Chuanxiao Dong | bf1806d | 2010-07-27 10:48:34 +0800 | [diff] [blame] | 1069 | printk(KERN_INFO "IRQ status = 0x%04x\n", irq_status); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1070 | #endif |
| 1071 | /* handle interrupt */ |
| 1072 | /* first acknowledge it */ |
| 1073 | clear_interrupt(denali, irq_status); |
| 1074 | /* store the status in the device context for someone |
| 1075 | to read */ |
| 1076 | denali->irq_status |= irq_status; |
| 1077 | /* notify anyone who cares that it happened */ |
| 1078 | complete(&denali->complete); |
| 1079 | /* tell the OS that we've handled this */ |
| 1080 | result = IRQ_HANDLED; |
| 1081 | } |
| 1082 | } |
| 1083 | spin_unlock(&denali->irq_lock); |
| 1084 | return result; |
| 1085 | } |
| 1086 | #define BANK(x) ((x) << 24) |
| 1087 | |
| 1088 | static uint32_t wait_for_irq(struct denali_nand_info *denali, uint32_t irq_mask) |
| 1089 | { |
| 1090 | unsigned long comp_res = 0; |
| 1091 | uint32_t intr_status = 0; |
| 1092 | bool retry = false; |
| 1093 | unsigned long timeout = msecs_to_jiffies(1000); |
| 1094 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1095 | do { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1096 | #if DEBUG_DENALI |
Chuanxiao Dong | bf1806d | 2010-07-27 10:48:34 +0800 | [diff] [blame] | 1097 | printk(KERN_INFO "waiting for 0x%x\n", irq_mask); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1098 | #endif |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1099 | comp_res = |
| 1100 | wait_for_completion_timeout(&denali->complete, timeout); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1101 | spin_lock_irq(&denali->irq_lock); |
| 1102 | intr_status = denali->irq_status; |
| 1103 | |
| 1104 | #if DEBUG_DENALI |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1105 | denali->irq_debug_array[denali->idx++] = |
| 1106 | 0x20000000 | (irq_mask << 16) | intr_status; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1107 | denali->idx %= 32; |
| 1108 | #endif |
| 1109 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1110 | if (intr_status & irq_mask) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1111 | denali->irq_status &= ~irq_mask; |
| 1112 | spin_unlock_irq(&denali->irq_lock); |
| 1113 | #if DEBUG_DENALI |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1114 | if (retry) |
| 1115 | printk(KERN_INFO "status on retry = 0x%x\n", |
| 1116 | intr_status); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1117 | #endif |
| 1118 | /* our interrupt was detected */ |
| 1119 | break; |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1120 | } else { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1121 | /* these are not the interrupts you are looking for - |
| 1122 | * need to wait again */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1123 | spin_unlock_irq(&denali->irq_lock); |
| 1124 | #if DEBUG_DENALI |
| 1125 | print_irq_log(denali); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1126 | printk(KERN_INFO "received irq nobody cared:" |
| 1127 | " irq_status = 0x%x, irq_mask = 0x%x," |
| 1128 | " timeout = %ld\n", intr_status, |
| 1129 | irq_mask, comp_res); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1130 | #endif |
| 1131 | retry = true; |
| 1132 | } |
| 1133 | } while (comp_res != 0); |
| 1134 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1135 | if (comp_res == 0) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1136 | /* timeout */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1137 | printk(KERN_ERR "timeout occurred, status = 0x%x, mask = 0x%x\n", |
| 1138 | intr_status, irq_mask); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1139 | |
| 1140 | intr_status = 0; |
| 1141 | } |
| 1142 | return intr_status; |
| 1143 | } |
| 1144 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1145 | /* This helper function setups the registers for ECC and whether or not |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1146 | the spare area will be transfered. */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1147 | 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] | 1148 | bool transfer_spare) |
| 1149 | { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1150 | int ecc_en_flag = 0, transfer_spare_flag = 0; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1151 | |
| 1152 | /* set ECC, transfer spare bits if needed */ |
| 1153 | ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0; |
| 1154 | transfer_spare_flag = transfer_spare ? TRANSFER_SPARE_REG__FLAG : 0; |
| 1155 | |
| 1156 | /* Enable spare area/ECC per user's request. */ |
| 1157 | denali_write32(ecc_en_flag, denali->flash_reg + ECC_ENABLE); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1158 | denali_write32(transfer_spare_flag, |
| 1159 | denali->flash_reg + TRANSFER_SPARE_REG); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1160 | } |
| 1161 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1162 | /* sends a pipeline command operation to the controller. See the Denali NAND |
| 1163 | controller's user guide for more information (section 4.2.3.6). |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1164 | */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1165 | static int denali_send_pipeline_cmd(struct denali_nand_info *denali, |
| 1166 | bool ecc_en, |
| 1167 | bool transfer_spare, |
| 1168 | int access_type, |
| 1169 | int op) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1170 | { |
| 1171 | int status = PASS; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1172 | uint32_t addr = 0x0, cmd = 0x0, page_count = 1, irq_status = 0, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1173 | irq_mask = 0; |
| 1174 | |
| 1175 | if (op == DENALI_READ) irq_mask = INTR_STATUS0__LOAD_COMP; |
| 1176 | else if (op == DENALI_WRITE) irq_mask = 0; |
| 1177 | else BUG(); |
| 1178 | |
| 1179 | setup_ecc_for_xfer(denali, ecc_en, transfer_spare); |
| 1180 | |
| 1181 | #if DEBUG_DENALI |
| 1182 | spin_lock_irq(&denali->irq_lock); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1183 | denali->irq_debug_array[denali->idx++] = |
| 1184 | 0x40000000 | ioread32(denali->flash_reg + ECC_ENABLE) | |
| 1185 | (access_type << 4); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1186 | denali->idx %= 32; |
| 1187 | spin_unlock_irq(&denali->irq_lock); |
| 1188 | #endif |
| 1189 | |
| 1190 | |
| 1191 | /* clear interrupts */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1192 | clear_interrupts(denali); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1193 | |
| 1194 | addr = BANK(denali->flash_bank) | denali->page; |
| 1195 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1196 | if (op == DENALI_WRITE && access_type != SPARE_ACCESS) { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1197 | cmd = MODE_01 | addr; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1198 | denali_write32(cmd, denali->flash_mem); |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1199 | } else if (op == DENALI_WRITE && access_type == SPARE_ACCESS) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1200 | /* read spare area */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1201 | cmd = MODE_10 | addr; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1202 | index_addr(denali, (uint32_t)cmd, access_type); |
| 1203 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1204 | cmd = MODE_01 | addr; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1205 | denali_write32(cmd, denali->flash_mem); |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1206 | } else if (op == DENALI_READ) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1207 | /* setup page read request for access type */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1208 | cmd = MODE_10 | addr; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1209 | index_addr(denali, (uint32_t)cmd, access_type); |
| 1210 | |
| 1211 | /* page 33 of the NAND controller spec indicates we should not |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1212 | use the pipeline commands in Spare area only mode. So we |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1213 | don't. |
| 1214 | */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1215 | if (access_type == SPARE_ACCESS) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1216 | cmd = MODE_01 | addr; |
| 1217 | denali_write32(cmd, denali->flash_mem); |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1218 | } else { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1219 | index_addr(denali, (uint32_t)cmd, |
| 1220 | 0x2000 | op | page_count); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1221 | |
| 1222 | /* wait for command to be accepted |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1223 | * can always use status0 bit as the |
| 1224 | * mask is identical for each |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1225 | * bank. */ |
| 1226 | irq_status = wait_for_irq(denali, irq_mask); |
| 1227 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1228 | if (irq_status == 0) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1229 | printk(KERN_ERR "cmd, page, addr on timeout " |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1230 | "(0x%x, 0x%x, 0x%x)\n", cmd, |
| 1231 | denali->page, addr); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1232 | status = FAIL; |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1233 | } else { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1234 | cmd = MODE_01 | addr; |
| 1235 | denali_write32(cmd, denali->flash_mem); |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | return status; |
| 1240 | } |
| 1241 | |
| 1242 | /* helper function that simply writes a buffer to the flash */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1243 | static int write_data_to_flash_mem(struct denali_nand_info *denali, |
| 1244 | const uint8_t *buf, |
| 1245 | int len) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1246 | { |
| 1247 | uint32_t i = 0, *buf32; |
| 1248 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1249 | /* verify that the len is a multiple of 4. see comment in |
| 1250 | * read_data_from_flash_mem() */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1251 | BUG_ON((len % 4) != 0); |
| 1252 | |
| 1253 | /* write the data to the flash memory */ |
| 1254 | buf32 = (uint32_t *)buf; |
| 1255 | for (i = 0; i < len / 4; i++) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1256 | denali_write32(*buf32++, denali->flash_mem + 0x10); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1257 | return i*4; /* intent is to return the number of bytes read */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | /* helper function that simply reads a buffer from the flash */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1261 | static int read_data_from_flash_mem(struct denali_nand_info *denali, |
| 1262 | uint8_t *buf, |
| 1263 | int len) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1264 | { |
| 1265 | uint32_t i = 0, *buf32; |
| 1266 | |
| 1267 | /* we assume that len will be a multiple of 4, if not |
| 1268 | * it would be nice to know about it ASAP rather than |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1269 | * have random failures... |
| 1270 | * This assumption is based on the fact that this |
| 1271 | * function is designed to be used to read flash pages, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1272 | * which are typically multiples of 4... |
| 1273 | */ |
| 1274 | |
| 1275 | BUG_ON((len % 4) != 0); |
| 1276 | |
| 1277 | /* transfer the data from the flash */ |
| 1278 | buf32 = (uint32_t *)buf; |
| 1279 | for (i = 0; i < len / 4; i++) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1280 | *buf32++ = ioread32(denali->flash_mem + 0x10); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1281 | return i*4; /* intent is to return the number of bytes read */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | /* writes OOB data to the device */ |
| 1285 | static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page) |
| 1286 | { |
| 1287 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1288 | uint32_t irq_status = 0; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1289 | uint32_t irq_mask = INTR_STATUS0__PROGRAM_COMP | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1290 | INTR_STATUS0__PROGRAM_FAIL; |
| 1291 | int status = 0; |
| 1292 | |
| 1293 | denali->page = page; |
| 1294 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1295 | if (denali_send_pipeline_cmd(denali, false, false, SPARE_ACCESS, |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1296 | DENALI_WRITE) == PASS) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1297 | write_data_to_flash_mem(denali, buf, mtd->oobsize); |
| 1298 | |
| 1299 | #if DEBUG_DENALI |
| 1300 | spin_lock_irq(&denali->irq_lock); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1301 | denali->irq_debug_array[denali->idx++] = |
| 1302 | 0x80000000 | mtd->oobsize; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1303 | denali->idx %= 32; |
| 1304 | spin_unlock_irq(&denali->irq_lock); |
| 1305 | #endif |
| 1306 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1307 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1308 | /* wait for operation to complete */ |
| 1309 | irq_status = wait_for_irq(denali, irq_mask); |
| 1310 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1311 | if (irq_status == 0) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1312 | printk(KERN_ERR "OOB write failed\n"); |
| 1313 | status = -EIO; |
| 1314 | } |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1315 | } else { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1316 | printk(KERN_ERR "unable to send pipeline command\n"); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1317 | status = -EIO; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1318 | } |
| 1319 | return status; |
| 1320 | } |
| 1321 | |
| 1322 | /* reads OOB data from the device */ |
| 1323 | static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page) |
| 1324 | { |
| 1325 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1326 | uint32_t irq_mask = INTR_STATUS0__LOAD_COMP, |
| 1327 | irq_status = 0, addr = 0x0, cmd = 0x0; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1328 | |
| 1329 | denali->page = page; |
| 1330 | |
| 1331 | #if DEBUG_DENALI |
Chuanxiao Dong | bf1806d | 2010-07-27 10:48:34 +0800 | [diff] [blame] | 1332 | printk(KERN_INFO "read_oob %d\n", page); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1333 | #endif |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1334 | if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS, |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1335 | DENALI_READ) == PASS) { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1336 | read_data_from_flash_mem(denali, buf, mtd->oobsize); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1337 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1338 | /* wait for command to be accepted |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1339 | * can always use status0 bit as the mask is identical for each |
| 1340 | * bank. */ |
| 1341 | irq_status = wait_for_irq(denali, irq_mask); |
| 1342 | |
| 1343 | if (irq_status == 0) |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1344 | printk(KERN_ERR "page on OOB timeout %d\n", |
| 1345 | denali->page); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1346 | |
| 1347 | /* We set the device back to MAIN_ACCESS here as I observed |
| 1348 | * instability with the controller if you do a block erase |
| 1349 | * and the last transaction was a SPARE_ACCESS. Block erase |
| 1350 | * is reliable (according to the MTD test infrastructure) |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1351 | * if you are in MAIN_ACCESS. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1352 | */ |
| 1353 | addr = BANK(denali->flash_bank) | denali->page; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1354 | cmd = MODE_10 | addr; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1355 | index_addr(denali, (uint32_t)cmd, MAIN_ACCESS); |
| 1356 | |
| 1357 | #if DEBUG_DENALI |
| 1358 | spin_lock_irq(&denali->irq_lock); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1359 | denali->irq_debug_array[denali->idx++] = |
| 1360 | 0x60000000 | mtd->oobsize; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1361 | denali->idx %= 32; |
| 1362 | spin_unlock_irq(&denali->irq_lock); |
| 1363 | #endif |
| 1364 | } |
| 1365 | } |
| 1366 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1367 | /* this function examines buffers to see if they contain data that |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1368 | * indicate that the buffer is part of an erased region of flash. |
| 1369 | */ |
| 1370 | bool is_erased(uint8_t *buf, int len) |
| 1371 | { |
| 1372 | int i = 0; |
| 1373 | for (i = 0; i < len; i++) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1374 | if (buf[i] != 0xFF) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1375 | return false; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1376 | return true; |
| 1377 | } |
| 1378 | #define ECC_SECTOR_SIZE 512 |
| 1379 | |
| 1380 | #define ECC_SECTOR(x) (((x) & ECC_ERROR_ADDRESS__SECTOR_NR) >> 12) |
| 1381 | #define ECC_BYTE(x) (((x) & ECC_ERROR_ADDRESS__OFFSET)) |
| 1382 | #define ECC_CORRECTION_VALUE(x) ((x) & ERR_CORRECTION_INFO__BYTEMASK) |
| 1383 | #define ECC_ERROR_CORRECTABLE(x) (!((x) & ERR_CORRECTION_INFO)) |
| 1384 | #define ECC_ERR_DEVICE(x) ((x) & ERR_CORRECTION_INFO__DEVICE_NR >> 8) |
| 1385 | #define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO) |
| 1386 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1387 | static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1388 | uint8_t *oobbuf, uint32_t irq_status) |
| 1389 | { |
| 1390 | bool check_erased_page = false; |
| 1391 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1392 | if (irq_status & INTR_STATUS0__ECC_ERR) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1393 | /* read the ECC errors. we'll ignore them for now */ |
| 1394 | uint32_t err_address = 0, err_correction_info = 0; |
| 1395 | uint32_t err_byte = 0, err_sector = 0, err_device = 0; |
| 1396 | uint32_t err_correction_value = 0; |
| 1397 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1398 | do { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1399 | err_address = ioread32(denali->flash_reg + |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1400 | ECC_ERROR_ADDRESS); |
| 1401 | err_sector = ECC_SECTOR(err_address); |
| 1402 | err_byte = ECC_BYTE(err_address); |
| 1403 | |
| 1404 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1405 | err_correction_info = ioread32(denali->flash_reg + |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1406 | ERR_CORRECTION_INFO); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1407 | err_correction_value = |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1408 | ECC_CORRECTION_VALUE(err_correction_info); |
| 1409 | err_device = ECC_ERR_DEVICE(err_correction_info); |
| 1410 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1411 | if (ECC_ERROR_CORRECTABLE(err_correction_info)) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1412 | /* offset in our buffer is computed as: |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1413 | sector number * sector size + offset in |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1414 | sector |
| 1415 | */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1416 | int offset = err_sector * ECC_SECTOR_SIZE + |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1417 | err_byte; |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1418 | if (offset < denali->mtd.writesize) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1419 | /* correct the ECC error */ |
| 1420 | buf[offset] ^= err_correction_value; |
| 1421 | denali->mtd.ecc_stats.corrected++; |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1422 | } else { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1423 | /* bummer, couldn't correct the error */ |
| 1424 | printk(KERN_ERR "ECC offset invalid\n"); |
| 1425 | denali->mtd.ecc_stats.failed++; |
| 1426 | } |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1427 | } else { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1428 | /* if the error is not correctable, need to |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1429 | * look at the page to see if it is an erased |
| 1430 | * page. if so, then it's not a real ECC error |
| 1431 | * */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1432 | check_erased_page = true; |
| 1433 | } |
| 1434 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1435 | #if DEBUG_DENALI |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1436 | printk(KERN_INFO "Detected ECC error in page %d:" |
| 1437 | " err_addr = 0x%08x, info to fix is" |
| 1438 | " 0x%08x\n", denali->page, err_address, |
| 1439 | err_correction_info); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1440 | #endif |
| 1441 | } while (!ECC_LAST_ERR(err_correction_info)); |
| 1442 | } |
| 1443 | return check_erased_page; |
| 1444 | } |
| 1445 | |
| 1446 | /* programs the controller to either enable/disable DMA transfers */ |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1447 | static void denali_enable_dma(struct denali_nand_info *denali, bool en) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1448 | { |
| 1449 | uint32_t reg_val = 0x0; |
| 1450 | |
| 1451 | if (en) reg_val = DMA_ENABLE__FLAG; |
| 1452 | |
| 1453 | denali_write32(reg_val, denali->flash_reg + DMA_ENABLE); |
| 1454 | ioread32(denali->flash_reg + DMA_ENABLE); |
| 1455 | } |
| 1456 | |
| 1457 | /* setups the HW to perform the data DMA */ |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1458 | static void denali_setup_dma(struct denali_nand_info *denali, int op) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1459 | { |
| 1460 | uint32_t mode = 0x0; |
| 1461 | const int page_count = 1; |
| 1462 | dma_addr_t addr = denali->buf.dma_buf; |
| 1463 | |
| 1464 | mode = MODE_10 | BANK(denali->flash_bank); |
| 1465 | |
| 1466 | /* DMA is a four step process */ |
| 1467 | |
| 1468 | /* 1. setup transfer type and # of pages */ |
| 1469 | index_addr(denali, mode | denali->page, 0x2000 | op | page_count); |
| 1470 | |
| 1471 | /* 2. set memory high address bits 23:8 */ |
| 1472 | index_addr(denali, mode | ((uint16_t)(addr >> 16) << 8), 0x2200); |
| 1473 | |
| 1474 | /* 3. set memory low address bits 23:8 */ |
| 1475 | index_addr(denali, mode | ((uint16_t)addr << 8), 0x2300); |
| 1476 | |
| 1477 | /* 4. interrupt when complete, burst len = 64 bytes*/ |
| 1478 | index_addr(denali, mode | 0x14000, 0x2400); |
| 1479 | } |
| 1480 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1481 | /* writes a page. user specifies type, and this function handles the |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1482 | configuration details. */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1483 | static void write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1484 | const uint8_t *buf, bool raw_xfer) |
| 1485 | { |
| 1486 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1487 | struct pci_dev *pci_dev = denali->dev; |
| 1488 | |
| 1489 | dma_addr_t addr = denali->buf.dma_buf; |
| 1490 | size_t size = denali->mtd.writesize + denali->mtd.oobsize; |
| 1491 | |
| 1492 | uint32_t irq_status = 0; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1493 | uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1494 | INTR_STATUS0__PROGRAM_FAIL; |
| 1495 | |
| 1496 | /* if it is a raw xfer, we want to disable ecc, and send |
| 1497 | * the spare area. |
| 1498 | * !raw_xfer - enable ecc |
| 1499 | * raw_xfer - transfer spare |
| 1500 | */ |
| 1501 | setup_ecc_for_xfer(denali, !raw_xfer, raw_xfer); |
| 1502 | |
| 1503 | /* copy buffer into DMA buffer */ |
| 1504 | memcpy(denali->buf.buf, buf, mtd->writesize); |
| 1505 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1506 | if (raw_xfer) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1507 | /* transfer the data to the spare area */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1508 | memcpy(denali->buf.buf + mtd->writesize, |
| 1509 | chip->oob_poi, |
| 1510 | mtd->oobsize); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_TODEVICE); |
| 1514 | |
| 1515 | clear_interrupts(denali); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1516 | denali_enable_dma(denali, true); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1517 | |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1518 | denali_setup_dma(denali, DENALI_WRITE); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1519 | |
| 1520 | /* wait for operation to complete */ |
| 1521 | irq_status = wait_for_irq(denali, irq_mask); |
| 1522 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1523 | if (irq_status == 0) { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1524 | printk(KERN_ERR "timeout on write_page" |
| 1525 | " (type = %d)\n", raw_xfer); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1526 | denali->status = |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1527 | (irq_status & INTR_STATUS0__PROGRAM_FAIL) ? |
| 1528 | NAND_STATUS_FAIL : PASS; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1529 | } |
| 1530 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1531 | denali_enable_dma(denali, false); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1532 | pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_TODEVICE); |
| 1533 | } |
| 1534 | |
| 1535 | /* NAND core entry points */ |
| 1536 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1537 | /* this is the callback that the NAND core calls to write a page. Since |
| 1538 | writing a page with ECC or without is similar, all the work is done |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1539 | by write_page above. */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1540 | 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] | 1541 | const uint8_t *buf) |
| 1542 | { |
| 1543 | /* for regular page writes, we let HW handle all the ECC |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1544 | * data written to the device. */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1545 | write_page(mtd, chip, buf, false); |
| 1546 | } |
| 1547 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1548 | /* This is the callback that the NAND core calls to write a page without ECC. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1549 | raw access is similiar to ECC page writes, so all the work is done in the |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1550 | write_page() function above. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1551 | */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1552 | 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] | 1553 | const uint8_t *buf) |
| 1554 | { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1555 | /* for raw page writes, we want to disable ECC and simply write |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1556 | whatever data is in the buffer. */ |
| 1557 | write_page(mtd, chip, buf, true); |
| 1558 | } |
| 1559 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1560 | 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] | 1561 | int page) |
| 1562 | { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1563 | return write_oob_data(mtd, chip->oob_poi, page); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1564 | } |
| 1565 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1566 | 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] | 1567 | int page, int sndcmd) |
| 1568 | { |
| 1569 | read_oob_data(mtd, chip->oob_poi, page); |
| 1570 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1571 | return 0; /* notify NAND core to send command to |
| 1572 | NAND device. */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 1576 | uint8_t *buf, int page) |
| 1577 | { |
| 1578 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1579 | struct pci_dev *pci_dev = denali->dev; |
| 1580 | |
| 1581 | dma_addr_t addr = denali->buf.dma_buf; |
| 1582 | size_t size = denali->mtd.writesize + denali->mtd.oobsize; |
| 1583 | |
| 1584 | uint32_t irq_status = 0; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1585 | uint32_t irq_mask = INTR_STATUS0__ECC_TRANSACTION_DONE | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1586 | INTR_STATUS0__ECC_ERR; |
| 1587 | bool check_erased_page = false; |
| 1588 | |
| 1589 | setup_ecc_for_xfer(denali, true, false); |
| 1590 | |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1591 | denali_enable_dma(denali, true); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1592 | pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_FROMDEVICE); |
| 1593 | |
| 1594 | clear_interrupts(denali); |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1595 | denali_setup_dma(denali, DENALI_READ); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1596 | |
| 1597 | /* wait for operation to complete */ |
| 1598 | irq_status = wait_for_irq(denali, irq_mask); |
| 1599 | |
| 1600 | pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_FROMDEVICE); |
| 1601 | |
| 1602 | memcpy(buf, denali->buf.buf, mtd->writesize); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1603 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1604 | check_erased_page = handle_ecc(denali, buf, chip->oob_poi, irq_status); |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1605 | denali_enable_dma(denali, false); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1606 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1607 | if (check_erased_page) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1608 | read_oob_data(&denali->mtd, chip->oob_poi, denali->page); |
| 1609 | |
| 1610 | /* check ECC failures that may have occurred on erased pages */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1611 | if (check_erased_page) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1612 | if (!is_erased(buf, denali->mtd.writesize)) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1613 | denali->mtd.ecc_stats.failed++; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1614 | if (!is_erased(buf, denali->mtd.oobsize)) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1615 | denali->mtd.ecc_stats.failed++; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1616 | } |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1617 | } |
| 1618 | return 0; |
| 1619 | } |
| 1620 | |
| 1621 | static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 1622 | uint8_t *buf, int page) |
| 1623 | { |
| 1624 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1625 | struct pci_dev *pci_dev = denali->dev; |
| 1626 | |
| 1627 | dma_addr_t addr = denali->buf.dma_buf; |
| 1628 | size_t size = denali->mtd.writesize + denali->mtd.oobsize; |
| 1629 | |
| 1630 | uint32_t irq_status = 0; |
| 1631 | uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1632 | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1633 | setup_ecc_for_xfer(denali, false, true); |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1634 | denali_enable_dma(denali, true); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1635 | |
| 1636 | pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_FROMDEVICE); |
| 1637 | |
| 1638 | clear_interrupts(denali); |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1639 | denali_setup_dma(denali, DENALI_READ); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1640 | |
| 1641 | /* wait for operation to complete */ |
| 1642 | irq_status = wait_for_irq(denali, irq_mask); |
| 1643 | |
| 1644 | pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_FROMDEVICE); |
| 1645 | |
David Woodhouse | aadff49 | 2010-05-13 16:12:43 +0100 | [diff] [blame] | 1646 | denali_enable_dma(denali, false); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1647 | |
| 1648 | memcpy(buf, denali->buf.buf, mtd->writesize); |
| 1649 | memcpy(chip->oob_poi, denali->buf.buf + mtd->writesize, mtd->oobsize); |
| 1650 | |
| 1651 | return 0; |
| 1652 | } |
| 1653 | |
| 1654 | static uint8_t denali_read_byte(struct mtd_info *mtd) |
| 1655 | { |
| 1656 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1657 | uint8_t result = 0xff; |
| 1658 | |
| 1659 | if (denali->buf.head < denali->buf.tail) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1660 | result = denali->buf.buf[denali->buf.head++]; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1661 | |
| 1662 | #if DEBUG_DENALI |
Chuanxiao Dong | bf1806d | 2010-07-27 10:48:34 +0800 | [diff] [blame] | 1663 | printk(KERN_INFO "read byte -> 0x%02x\n", result); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1664 | #endif |
| 1665 | return result; |
| 1666 | } |
| 1667 | |
| 1668 | static void denali_select_chip(struct mtd_info *mtd, int chip) |
| 1669 | { |
| 1670 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1671 | #if DEBUG_DENALI |
Chuanxiao Dong | bf1806d | 2010-07-27 10:48:34 +0800 | [diff] [blame] | 1672 | printk(KERN_INFO "denali select chip %d\n", chip); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1673 | #endif |
| 1674 | spin_lock_irq(&denali->irq_lock); |
| 1675 | denali->flash_bank = chip; |
| 1676 | spin_unlock_irq(&denali->irq_lock); |
| 1677 | } |
| 1678 | |
| 1679 | static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip) |
| 1680 | { |
| 1681 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1682 | int status = denali->status; |
| 1683 | denali->status = 0; |
| 1684 | |
| 1685 | #if DEBUG_DENALI |
Chuanxiao Dong | bf1806d | 2010-07-27 10:48:34 +0800 | [diff] [blame] | 1686 | printk(KERN_INFO "waitfunc %d\n", status); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1687 | #endif |
| 1688 | return status; |
| 1689 | } |
| 1690 | |
| 1691 | static void denali_erase(struct mtd_info *mtd, int page) |
| 1692 | { |
| 1693 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1694 | |
| 1695 | uint32_t cmd = 0x0, irq_status = 0; |
| 1696 | |
| 1697 | #if DEBUG_DENALI |
Chuanxiao Dong | bf1806d | 2010-07-27 10:48:34 +0800 | [diff] [blame] | 1698 | printk(KERN_INFO "erase page: %d\n", page); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1699 | #endif |
| 1700 | /* clear interrupts */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1701 | clear_interrupts(denali); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1702 | |
| 1703 | /* setup page read request for access type */ |
| 1704 | cmd = MODE_10 | BANK(denali->flash_bank) | page; |
| 1705 | index_addr(denali, (uint32_t)cmd, 0x1); |
| 1706 | |
| 1707 | /* wait for erase to complete or failure to occur */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1708 | irq_status = wait_for_irq(denali, INTR_STATUS0__ERASE_COMP | |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1709 | INTR_STATUS0__ERASE_FAIL); |
| 1710 | |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1711 | denali->status = (irq_status & INTR_STATUS0__ERASE_FAIL) ? |
| 1712 | NAND_STATUS_FAIL : PASS; |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1713 | } |
| 1714 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1715 | 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] | 1716 | int page) |
| 1717 | { |
| 1718 | struct denali_nand_info *denali = mtd_to_denali(mtd); |
| 1719 | |
| 1720 | #if DEBUG_DENALI |
Chuanxiao Dong | bf1806d | 2010-07-27 10:48:34 +0800 | [diff] [blame] | 1721 | printk(KERN_INFO "cmdfunc: 0x%x %d %d\n", cmd, col, page); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1722 | #endif |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1723 | switch (cmd) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1724 | case NAND_CMD_PAGEPROG: |
| 1725 | break; |
| 1726 | case NAND_CMD_STATUS: |
| 1727 | read_status(denali); |
| 1728 | break; |
| 1729 | case NAND_CMD_READID: |
| 1730 | reset_buf(denali); |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1731 | if (denali->flash_bank < denali->total_used_banks) { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1732 | /* write manufacturer information into nand |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1733 | buffer for NAND subsystem to fetch. |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1734 | */ |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1735 | write_byte_to_buf(denali, |
| 1736 | denali->dev_info.wDeviceMaker); |
| 1737 | write_byte_to_buf(denali, |
| 1738 | denali->dev_info.wDeviceID); |
| 1739 | write_byte_to_buf(denali, |
| 1740 | denali->dev_info.bDeviceParam0); |
| 1741 | write_byte_to_buf(denali, |
| 1742 | denali->dev_info.bDeviceParam1); |
| 1743 | write_byte_to_buf(denali, |
| 1744 | denali->dev_info.bDeviceParam2); |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1745 | } else { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1746 | int i; |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1747 | for (i = 0; i < 5; i++) |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1748 | write_byte_to_buf(denali, 0xff); |
| 1749 | } |
| 1750 | break; |
| 1751 | case NAND_CMD_READ0: |
| 1752 | case NAND_CMD_SEQIN: |
| 1753 | denali->page = page; |
| 1754 | break; |
| 1755 | case NAND_CMD_RESET: |
| 1756 | reset_bank(denali); |
| 1757 | break; |
| 1758 | case NAND_CMD_READOOB: |
| 1759 | /* TODO: Read OOB data */ |
| 1760 | break; |
| 1761 | default: |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1762 | printk(KERN_ERR ": unsupported command" |
| 1763 | " received 0x%x\n", cmd); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1764 | break; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | /* stubs for ECC functions not used by the NAND core */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1769 | 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] | 1770 | uint8_t *ecc_code) |
| 1771 | { |
| 1772 | printk(KERN_ERR "denali_ecc_calculate called unexpectedly\n"); |
| 1773 | BUG(); |
| 1774 | return -EIO; |
| 1775 | } |
| 1776 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1777 | static int denali_ecc_correct(struct mtd_info *mtd, uint8_t *data, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1778 | uint8_t *read_ecc, uint8_t *calc_ecc) |
| 1779 | { |
| 1780 | printk(KERN_ERR "denali_ecc_correct called unexpectedly\n"); |
| 1781 | BUG(); |
| 1782 | return -EIO; |
| 1783 | } |
| 1784 | |
| 1785 | static void denali_ecc_hwctl(struct mtd_info *mtd, int mode) |
| 1786 | { |
| 1787 | printk(KERN_ERR "denali_ecc_hwctl called unexpectedly\n"); |
| 1788 | BUG(); |
| 1789 | } |
| 1790 | /* end NAND core entry points */ |
| 1791 | |
| 1792 | /* Initialization code to bring the device up to a known good state */ |
| 1793 | static void denali_hw_init(struct denali_nand_info *denali) |
| 1794 | { |
| 1795 | denali_irq_init(denali); |
| 1796 | NAND_Flash_Reset(denali); |
| 1797 | denali_write32(0x0F, denali->flash_reg + RB_PIN_ENABLED); |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1798 | denali_write32(CHIP_EN_DONT_CARE__FLAG, |
| 1799 | denali->flash_reg + CHIP_ENABLE_DONT_CARE); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1800 | |
| 1801 | denali_write32(0x0, denali->flash_reg + SPARE_AREA_SKIP_BYTES); |
| 1802 | denali_write32(0xffff, denali->flash_reg + SPARE_AREA_MARKER); |
| 1803 | |
| 1804 | /* Should set value for these registers when init */ |
| 1805 | denali_write32(0, denali->flash_reg + TWO_ROW_ADDR_CYCLES); |
| 1806 | denali_write32(1, denali->flash_reg + ECC_ENABLE); |
| 1807 | } |
| 1808 | |
| 1809 | /* ECC layout for SLC devices. Denali spec indicates SLC fixed at 4 bytes */ |
| 1810 | #define ECC_BYTES_SLC 4 * (2048 / ECC_SECTOR_SIZE) |
| 1811 | static struct nand_ecclayout nand_oob_slc = { |
| 1812 | .eccbytes = 4, |
| 1813 | .eccpos = { 0, 1, 2, 3 }, /* not used */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1814 | .oobfree = { |
| 1815 | { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1816 | .offset = ECC_BYTES_SLC, |
| 1817 | .length = 64 - ECC_BYTES_SLC |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1818 | } |
| 1819 | } |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1820 | }; |
| 1821 | |
| 1822 | #define ECC_BYTES_MLC 14 * (2048 / ECC_SECTOR_SIZE) |
| 1823 | static struct nand_ecclayout nand_oob_mlc_14bit = { |
| 1824 | .eccbytes = 14, |
| 1825 | .eccpos = { 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13 }, /* not used */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1826 | .oobfree = { |
| 1827 | { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1828 | .offset = ECC_BYTES_MLC, |
| 1829 | .length = 64 - ECC_BYTES_MLC |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1830 | } |
| 1831 | } |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1832 | }; |
| 1833 | |
| 1834 | static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' }; |
| 1835 | static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' }; |
| 1836 | |
| 1837 | static struct nand_bbt_descr bbt_main_descr = { |
| 1838 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 1839 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 1840 | .offs = 8, |
| 1841 | .len = 4, |
| 1842 | .veroffs = 12, |
| 1843 | .maxblocks = 4, |
| 1844 | .pattern = bbt_pattern, |
| 1845 | }; |
| 1846 | |
| 1847 | static struct nand_bbt_descr bbt_mirror_descr = { |
| 1848 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 1849 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 1850 | .offs = 8, |
| 1851 | .len = 4, |
| 1852 | .veroffs = 12, |
| 1853 | .maxblocks = 4, |
| 1854 | .pattern = mirror_pattern, |
| 1855 | }; |
| 1856 | |
| 1857 | /* initalize driver data structures */ |
| 1858 | void denali_drv_init(struct denali_nand_info *denali) |
| 1859 | { |
| 1860 | denali->idx = 0; |
| 1861 | |
| 1862 | /* setup interrupt handler */ |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1863 | /* the completion object will be used to notify |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1864 | * the callee that the interrupt is done */ |
| 1865 | init_completion(&denali->complete); |
| 1866 | |
| 1867 | /* the spinlock will be used to synchronize the ISR |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1868 | * with any element that might be access shared |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1869 | * data (interrupt status) */ |
| 1870 | spin_lock_init(&denali->irq_lock); |
| 1871 | |
| 1872 | /* indicate that MTD has not selected a valid bank yet */ |
| 1873 | denali->flash_bank = CHIP_SELECT_INVALID; |
| 1874 | |
| 1875 | /* initialize our irq_status variable to indicate no interrupts */ |
| 1876 | denali->irq_status = 0; |
| 1877 | } |
| 1878 | |
| 1879 | /* driver entry point */ |
| 1880 | static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) |
| 1881 | { |
| 1882 | int ret = -ENODEV; |
| 1883 | resource_size_t csr_base, mem_base; |
| 1884 | unsigned long csr_len, mem_len; |
| 1885 | struct denali_nand_info *denali; |
| 1886 | |
| 1887 | nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n", |
| 1888 | __FILE__, __LINE__, __func__); |
| 1889 | |
| 1890 | denali = kzalloc(sizeof(*denali), GFP_KERNEL); |
| 1891 | if (!denali) |
| 1892 | return -ENOMEM; |
| 1893 | |
| 1894 | ret = pci_enable_device(dev); |
| 1895 | if (ret) { |
| 1896 | printk(KERN_ERR "Spectra: pci_enable_device failed.\n"); |
| 1897 | goto failed_enable; |
| 1898 | } |
| 1899 | |
| 1900 | if (id->driver_data == INTEL_CE4100) { |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1901 | /* Due to a silicon limitation, we can only support |
| 1902 | * ONFI timing mode 1 and below. |
| 1903 | */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1904 | if (onfi_timing_mode < -1 || onfi_timing_mode > 1) { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1905 | printk(KERN_ERR "Intel CE4100 only supports" |
| 1906 | " ONFI timing mode 1 or below\n"); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1907 | ret = -EINVAL; |
| 1908 | goto failed_enable; |
| 1909 | } |
| 1910 | denali->platform = INTEL_CE4100; |
| 1911 | mem_base = pci_resource_start(dev, 0); |
| 1912 | mem_len = pci_resource_len(dev, 1); |
| 1913 | csr_base = pci_resource_start(dev, 1); |
| 1914 | csr_len = pci_resource_len(dev, 1); |
| 1915 | } else { |
| 1916 | denali->platform = INTEL_MRST; |
| 1917 | csr_base = pci_resource_start(dev, 0); |
| 1918 | csr_len = pci_resource_start(dev, 0); |
| 1919 | mem_base = pci_resource_start(dev, 1); |
| 1920 | mem_len = pci_resource_len(dev, 1); |
| 1921 | if (!mem_len) { |
| 1922 | mem_base = csr_base + csr_len; |
| 1923 | mem_len = csr_len; |
| 1924 | nand_dbg_print(NAND_DBG_WARN, |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1925 | "Spectra: No second" |
| 1926 | " BAR for PCI device;" |
| 1927 | " assuming %08Lx\n", |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1928 | (uint64_t)csr_base); |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | /* Is 32-bit DMA supported? */ |
| 1933 | ret = pci_set_dma_mask(dev, DMA_BIT_MASK(32)); |
| 1934 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1935 | if (ret) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1936 | printk(KERN_ERR "Spectra: no usable DMA configuration\n"); |
| 1937 | goto failed_enable; |
| 1938 | } |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 1939 | denali->buf.dma_buf = |
| 1940 | pci_map_single(dev, denali->buf.buf, |
| 1941 | DENALI_BUF_SIZE, |
| 1942 | PCI_DMA_BIDIRECTIONAL); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1943 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 1944 | if (pci_dma_mapping_error(dev, denali->buf.dma_buf)) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 1945 | printk(KERN_ERR "Spectra: failed to map DMA buffer\n"); |
| 1946 | goto failed_enable; |
| 1947 | } |
| 1948 | |
| 1949 | pci_set_master(dev); |
| 1950 | denali->dev = dev; |
| 1951 | |
| 1952 | ret = pci_request_regions(dev, DENALI_NAND_NAME); |
| 1953 | if (ret) { |
| 1954 | printk(KERN_ERR "Spectra: Unable to request memory regions\n"); |
| 1955 | goto failed_req_csr; |
| 1956 | } |
| 1957 | |
| 1958 | denali->flash_reg = ioremap_nocache(csr_base, csr_len); |
| 1959 | if (!denali->flash_reg) { |
| 1960 | printk(KERN_ERR "Spectra: Unable to remap memory region\n"); |
| 1961 | ret = -ENOMEM; |
| 1962 | goto failed_remap_csr; |
| 1963 | } |
| 1964 | nand_dbg_print(NAND_DBG_DEBUG, "Spectra: CSR 0x%08Lx -> 0x%p (0x%lx)\n", |
| 1965 | (uint64_t)csr_base, denali->flash_reg, csr_len); |
| 1966 | |
| 1967 | denali->flash_mem = ioremap_nocache(mem_base, mem_len); |
| 1968 | if (!denali->flash_mem) { |
| 1969 | printk(KERN_ERR "Spectra: ioremap_nocache failed!"); |
| 1970 | iounmap(denali->flash_reg); |
| 1971 | ret = -ENOMEM; |
| 1972 | goto failed_remap_csr; |
| 1973 | } |
| 1974 | |
| 1975 | nand_dbg_print(NAND_DBG_WARN, |
| 1976 | "Spectra: Remapped flash base address: " |
| 1977 | "0x%p, len: %ld\n", |
| 1978 | denali->flash_mem, csr_len); |
| 1979 | |
| 1980 | denali_hw_init(denali); |
| 1981 | denali_drv_init(denali); |
| 1982 | |
| 1983 | nand_dbg_print(NAND_DBG_DEBUG, "Spectra: IRQ %d\n", dev->irq); |
| 1984 | if (request_irq(dev->irq, denali_isr, IRQF_SHARED, |
| 1985 | DENALI_NAND_NAME, denali)) { |
| 1986 | printk(KERN_ERR "Spectra: Unable to allocate IRQ\n"); |
| 1987 | ret = -ENODEV; |
| 1988 | goto failed_request_irq; |
| 1989 | } |
| 1990 | |
| 1991 | /* now that our ISR is registered, we can enable interrupts */ |
| 1992 | NAND_LLD_Enable_Disable_Interrupts(denali, true); |
| 1993 | |
| 1994 | pci_set_drvdata(dev, denali); |
| 1995 | |
| 1996 | NAND_Read_Device_ID(denali); |
| 1997 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 1998 | /* MTD supported page sizes vary by kernel. We validate our |
| 1999 | * kernel supports the device here. |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2000 | */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 2001 | if (denali->dev_info.wPageSize > NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2002 | ret = -ENODEV; |
| 2003 | printk(KERN_ERR "Spectra: device size not supported by this " |
| 2004 | "version of MTD."); |
| 2005 | goto failed_nand; |
| 2006 | } |
| 2007 | |
| 2008 | nand_dbg_print(NAND_DBG_DEBUG, "Dump timing register values:" |
| 2009 | "acc_clks: %d, re_2_we: %d, we_2_re: %d," |
| 2010 | "addr_2_data: %d, rdwr_en_lo_cnt: %d, " |
| 2011 | "rdwr_en_hi_cnt: %d, cs_setup_cnt: %d\n", |
| 2012 | ioread32(denali->flash_reg + ACC_CLKS), |
| 2013 | ioread32(denali->flash_reg + RE_2_WE), |
| 2014 | ioread32(denali->flash_reg + WE_2_RE), |
| 2015 | ioread32(denali->flash_reg + ADDR_2_DATA), |
| 2016 | ioread32(denali->flash_reg + RDWR_EN_LO_CNT), |
| 2017 | ioread32(denali->flash_reg + RDWR_EN_HI_CNT), |
| 2018 | ioread32(denali->flash_reg + CS_SETUP_CNT)); |
| 2019 | |
| 2020 | denali->mtd.name = "Denali NAND"; |
| 2021 | denali->mtd.owner = THIS_MODULE; |
| 2022 | denali->mtd.priv = &denali->nand; |
| 2023 | |
| 2024 | /* register the driver with the NAND core subsystem */ |
| 2025 | denali->nand.select_chip = denali_select_chip; |
| 2026 | denali->nand.cmdfunc = denali_cmdfunc; |
| 2027 | denali->nand.read_byte = denali_read_byte; |
| 2028 | denali->nand.waitfunc = denali_waitfunc; |
| 2029 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 2030 | /* scan for NAND devices attached to the controller |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2031 | * this is the first stage in a two step process to register |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 2032 | * with the nand subsystem */ |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 2033 | if (nand_scan_ident(&denali->mtd, LLD_MAX_FLASH_BANKS, NULL)) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2034 | ret = -ENXIO; |
| 2035 | goto failed_nand; |
| 2036 | } |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 2037 | |
| 2038 | /* second stage of the NAND scan |
| 2039 | * this stage requires information regarding ECC and |
| 2040 | * bad block management. */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2041 | |
| 2042 | /* Bad block management */ |
| 2043 | denali->nand.bbt_td = &bbt_main_descr; |
| 2044 | denali->nand.bbt_md = &bbt_mirror_descr; |
| 2045 | |
| 2046 | /* skip the scan for now until we have OOB read and write support */ |
| 2047 | denali->nand.options |= NAND_USE_FLASH_BBT | NAND_SKIP_BBTSCAN; |
| 2048 | denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME; |
| 2049 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 2050 | if (denali->dev_info.MLCDevice) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2051 | denali->nand.ecc.layout = &nand_oob_mlc_14bit; |
| 2052 | denali->nand.ecc.bytes = ECC_BYTES_MLC; |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 2053 | } else {/* SLC */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2054 | denali->nand.ecc.layout = &nand_oob_slc; |
| 2055 | denali->nand.ecc.bytes = ECC_BYTES_SLC; |
| 2056 | } |
| 2057 | |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 2058 | /* These functions are required by the NAND core framework, otherwise, |
| 2059 | * the NAND core will assert. However, we don't need them, so we'll stub |
| 2060 | * them out. */ |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2061 | denali->nand.ecc.calculate = denali_ecc_calculate; |
| 2062 | denali->nand.ecc.correct = denali_ecc_correct; |
| 2063 | denali->nand.ecc.hwctl = denali_ecc_hwctl; |
| 2064 | |
| 2065 | /* override the default read operations */ |
| 2066 | denali->nand.ecc.size = denali->mtd.writesize; |
| 2067 | denali->nand.ecc.read_page = denali_read_page; |
| 2068 | denali->nand.ecc.read_page_raw = denali_read_page_raw; |
| 2069 | denali->nand.ecc.write_page = denali_write_page; |
| 2070 | denali->nand.ecc.write_page_raw = denali_write_page_raw; |
| 2071 | denali->nand.ecc.read_oob = denali_read_oob; |
| 2072 | denali->nand.ecc.write_oob = denali_write_oob; |
| 2073 | denali->nand.erase_cmd = denali_erase; |
| 2074 | |
Chuanxiao Dong | 345b1d3 | 2010-07-27 10:41:53 +0800 | [diff] [blame] | 2075 | if (nand_scan_tail(&denali->mtd)) { |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2076 | ret = -ENXIO; |
| 2077 | goto failed_nand; |
| 2078 | } |
| 2079 | |
| 2080 | ret = add_mtd_device(&denali->mtd); |
| 2081 | if (ret) { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 2082 | printk(KERN_ERR "Spectra: Failed to register" |
| 2083 | " MTD device: %d\n", ret); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2084 | goto failed_nand; |
| 2085 | } |
| 2086 | return 0; |
| 2087 | |
| 2088 | failed_nand: |
| 2089 | denali_irq_cleanup(dev->irq, denali); |
| 2090 | failed_request_irq: |
| 2091 | iounmap(denali->flash_reg); |
| 2092 | iounmap(denali->flash_mem); |
| 2093 | failed_remap_csr: |
| 2094 | pci_release_regions(dev); |
| 2095 | failed_req_csr: |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 2096 | pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2097 | PCI_DMA_BIDIRECTIONAL); |
| 2098 | failed_enable: |
| 2099 | kfree(denali); |
| 2100 | return ret; |
| 2101 | } |
| 2102 | |
| 2103 | /* driver exit point */ |
| 2104 | static void denali_pci_remove(struct pci_dev *dev) |
| 2105 | { |
| 2106 | struct denali_nand_info *denali = pci_get_drvdata(dev); |
| 2107 | |
| 2108 | nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n", |
| 2109 | __FILE__, __LINE__, __func__); |
| 2110 | |
| 2111 | nand_release(&denali->mtd); |
| 2112 | del_mtd_device(&denali->mtd); |
| 2113 | |
| 2114 | denali_irq_cleanup(dev->irq, denali); |
| 2115 | |
| 2116 | iounmap(denali->flash_reg); |
| 2117 | iounmap(denali->flash_mem); |
| 2118 | pci_release_regions(dev); |
| 2119 | pci_disable_device(dev); |
Chuanxiao | 5bac3ac | 2010-08-05 23:06:04 +0800 | [diff] [blame] | 2120 | pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE, |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2121 | PCI_DMA_BIDIRECTIONAL); |
| 2122 | pci_set_drvdata(dev, NULL); |
| 2123 | kfree(denali); |
| 2124 | } |
| 2125 | |
| 2126 | MODULE_DEVICE_TABLE(pci, denali_pci_ids); |
| 2127 | |
| 2128 | static struct pci_driver denali_pci_driver = { |
| 2129 | .name = DENALI_NAND_NAME, |
| 2130 | .id_table = denali_pci_ids, |
| 2131 | .probe = denali_pci_probe, |
| 2132 | .remove = denali_pci_remove, |
| 2133 | }; |
| 2134 | |
| 2135 | static int __devinit denali_init(void) |
| 2136 | { |
Chuanxiao Dong | bdca6da | 2010-07-27 11:28:09 +0800 | [diff] [blame^] | 2137 | printk(KERN_INFO "Spectra MTD driver built on %s @ %s\n", |
| 2138 | __DATE__, __TIME__); |
Jason Roberts | ce08259 | 2010-05-13 15:57:33 +0100 | [diff] [blame] | 2139 | return pci_register_driver(&denali_pci_driver); |
| 2140 | } |
| 2141 | |
| 2142 | /* Free memory */ |
| 2143 | static void __devexit denali_exit(void) |
| 2144 | { |
| 2145 | pci_unregister_driver(&denali_pci_driver); |
| 2146 | } |
| 2147 | |
| 2148 | module_init(denali_init); |
| 2149 | module_exit(denali_exit); |