blob: e4462c0740b2b726a7ecc3c65063d211c7538a95 [file] [log] [blame]
Jason Robertsce082592010-05-13 15:57:33 +01001/*
2 * NAND Flash Controller Device Driver
3 * Copyright © 2009-2010, Intel Corporation and its suppliers.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19
20#include <linux/interrupt.h>
21#include <linux/delay.h>
22#include <linux/wait.h>
23#include <linux/mutex.h>
David Millerb8664b32010-08-04 22:57:51 -070024#include <linux/slab.h>
Jason Robertsce082592010-05-13 15:57:33 +010025#include <linux/pci.h>
26#include <linux/mtd/mtd.h>
27#include <linux/module.h>
28
29#include "denali.h"
30
31MODULE_LICENSE("GPL");
32
Chuanxiao5bac3ac2010-08-05 23:06:04 +080033/* We define a module parameter that allows the user to override
Jason Robertsce082592010-05-13 15:57:33 +010034 * the hardware and decide what timing mode should be used.
35 */
36#define NAND_DEFAULT_TIMINGS -1
37
38static int onfi_timing_mode = NAND_DEFAULT_TIMINGS;
39module_param(onfi_timing_mode, int, S_IRUGO);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +080040MODULE_PARM_DESC(onfi_timing_mode, "Overrides default ONFI setting."
41 " -1 indicates use default timings");
Jason Robertsce082592010-05-13 15:57:33 +010042
43#define DENALI_NAND_NAME "denali-nand"
44
45/* We define a macro here that combines all interrupts this driver uses into
46 * a single constant value, for convenience. */
47#define DENALI_IRQ_ALL (INTR_STATUS0__DMA_CMD_COMP | \
48 INTR_STATUS0__ECC_TRANSACTION_DONE | \
49 INTR_STATUS0__ECC_ERR | \
50 INTR_STATUS0__PROGRAM_FAIL | \
51 INTR_STATUS0__LOAD_COMP | \
52 INTR_STATUS0__PROGRAM_COMP | \
53 INTR_STATUS0__TIME_OUT | \
54 INTR_STATUS0__ERASE_FAIL | \
55 INTR_STATUS0__RST_COMP | \
56 INTR_STATUS0__ERASE_COMP)
57
Chuanxiao5bac3ac2010-08-05 23:06:04 +080058/* indicates whether or not the internal value for the flash bank is
Jason Robertsce082592010-05-13 15:57:33 +010059 valid or not */
Chuanxiao5bac3ac2010-08-05 23:06:04 +080060#define CHIP_SELECT_INVALID -1
Jason Robertsce082592010-05-13 15:57:33 +010061
62#define SUPPORT_8BITECC 1
63
Chuanxiao5bac3ac2010-08-05 23:06:04 +080064/* This macro divides two integers and rounds fractional values up
Jason Robertsce082592010-05-13 15:57:33 +010065 * to the nearest integer value. */
66#define CEIL_DIV(X, Y) (((X)%(Y)) ? ((X)/(Y)+1) : ((X)/(Y)))
67
68/* this macro allows us to convert from an MTD structure to our own
69 * device context (denali) structure.
70 */
71#define mtd_to_denali(m) container_of(m, struct denali_nand_info, mtd)
72
73/* These constants are defined by the driver to enable common driver
74 configuration options. */
75#define SPARE_ACCESS 0x41
76#define MAIN_ACCESS 0x42
77#define MAIN_SPARE_ACCESS 0x43
78
79#define DENALI_READ 0
80#define DENALI_WRITE 0x100
81
82/* types of device accesses. We can issue commands and get status */
83#define COMMAND_CYCLE 0
84#define ADDR_CYCLE 1
85#define STATUS_CYCLE 2
86
Chuanxiao5bac3ac2010-08-05 23:06:04 +080087/* this is a helper macro that allows us to
Jason Robertsce082592010-05-13 15:57:33 +010088 * format the bank into the proper bits for the controller */
89#define BANK(x) ((x) << 24)
90
91/* List of platforms this NAND controller has be integrated into */
92static const struct pci_device_id denali_pci_ids[] = {
93 { PCI_VDEVICE(INTEL, 0x0701), INTEL_CE4100 },
94 { PCI_VDEVICE(INTEL, 0x0809), INTEL_MRST },
95 { /* end: all zeroes */ }
96};
97
98
Chuanxiao5bac3ac2010-08-05 23:06:04 +080099/* these are static lookup tables that give us easy access to
100 registers in the NAND controller.
Jason Robertsce082592010-05-13 15:57:33 +0100101 */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800102static const uint32_t intr_status_addresses[4] = {INTR_STATUS0,
103 INTR_STATUS1,
104 INTR_STATUS2,
Jason Robertsce082592010-05-13 15:57:33 +0100105 INTR_STATUS3};
106
107static const uint32_t device_reset_banks[4] = {DEVICE_RESET__BANK0,
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800108 DEVICE_RESET__BANK1,
109 DEVICE_RESET__BANK2,
110 DEVICE_RESET__BANK3};
Jason Robertsce082592010-05-13 15:57:33 +0100111
112static const uint32_t operation_timeout[4] = {INTR_STATUS0__TIME_OUT,
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800113 INTR_STATUS1__TIME_OUT,
114 INTR_STATUS2__TIME_OUT,
115 INTR_STATUS3__TIME_OUT};
Jason Robertsce082592010-05-13 15:57:33 +0100116
117static const uint32_t reset_complete[4] = {INTR_STATUS0__RST_COMP,
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800118 INTR_STATUS1__RST_COMP,
119 INTR_STATUS2__RST_COMP,
120 INTR_STATUS3__RST_COMP};
Jason Robertsce082592010-05-13 15:57:33 +0100121
122/* specifies the debug level of the driver */
Chuanxiao Donga99d1792010-07-27 11:32:21 +0800123static int nand_debug_level;
Jason Robertsce082592010-05-13 15:57:33 +0100124
125/* forward declarations */
126static void clear_interrupts(struct denali_nand_info *denali);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800127static uint32_t wait_for_irq(struct denali_nand_info *denali,
128 uint32_t irq_mask);
129static void denali_irq_enable(struct denali_nand_info *denali,
130 uint32_t int_mask);
Jason Robertsce082592010-05-13 15:57:33 +0100131static uint32_t read_interrupt_status(struct denali_nand_info *denali);
132
133#define DEBUG_DENALI 0
134
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800135/* Certain operations for the denali NAND controller use
136 * an indexed mode to read/write data. The operation is
137 * performed by writing the address value of the command
138 * to the device memory followed by the data. This function
139 * abstracts this common operation.
Jason Robertsce082592010-05-13 15:57:33 +0100140*/
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800141static void index_addr(struct denali_nand_info *denali,
142 uint32_t address, uint32_t data)
Jason Robertsce082592010-05-13 15:57:33 +0100143{
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800144 iowrite32(address, denali->flash_mem);
145 iowrite32(data, denali->flash_mem + 0x10);
Jason Robertsce082592010-05-13 15:57:33 +0100146}
147
148/* Perform an indexed read of the device */
149static void index_addr_read_data(struct denali_nand_info *denali,
150 uint32_t address, uint32_t *pdata)
151{
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800152 iowrite32(address, denali->flash_mem);
Jason Robertsce082592010-05-13 15:57:33 +0100153 *pdata = ioread32(denali->flash_mem + 0x10);
154}
155
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800156/* We need to buffer some data for some of the NAND core routines.
Jason Robertsce082592010-05-13 15:57:33 +0100157 * The operations manage buffering that data. */
158static void reset_buf(struct denali_nand_info *denali)
159{
160 denali->buf.head = denali->buf.tail = 0;
161}
162
163static void write_byte_to_buf(struct denali_nand_info *denali, uint8_t byte)
164{
165 BUG_ON(denali->buf.tail >= sizeof(denali->buf.buf));
166 denali->buf.buf[denali->buf.tail++] = byte;
167}
168
169/* reads the status of the device */
170static void read_status(struct denali_nand_info *denali)
171{
172 uint32_t cmd = 0x0;
173
174 /* initialize the data buffer to store status */
175 reset_buf(denali);
176
177 /* initiate a device status read */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800178 cmd = MODE_11 | BANK(denali->flash_bank);
Jason Robertsce082592010-05-13 15:57:33 +0100179 index_addr(denali, cmd | COMMAND_CYCLE, 0x70);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800180 iowrite32(cmd | STATUS_CYCLE, denali->flash_mem);
Jason Robertsce082592010-05-13 15:57:33 +0100181
182 /* update buffer with status value */
183 write_byte_to_buf(denali, ioread32(denali->flash_mem + 0x10));
184
185#if DEBUG_DENALI
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800186 printk(KERN_INFO "device reporting status value of 0x%2x\n",
187 denali->buf.buf[0]);
Jason Robertsce082592010-05-13 15:57:33 +0100188#endif
189}
190
191/* resets a specific device connected to the core */
192static void reset_bank(struct denali_nand_info *denali)
193{
194 uint32_t irq_status = 0;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800195 uint32_t irq_mask = reset_complete[denali->flash_bank] |
Jason Robertsce082592010-05-13 15:57:33 +0100196 operation_timeout[denali->flash_bank];
197 int bank = 0;
198
199 clear_interrupts(denali);
200
201 bank = device_reset_banks[denali->flash_bank];
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800202 iowrite32(bank, denali->flash_reg + DEVICE_RESET);
Jason Robertsce082592010-05-13 15:57:33 +0100203
204 irq_status = wait_for_irq(denali, irq_mask);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800205
Jason Robertsce082592010-05-13 15:57:33 +0100206 if (irq_status & operation_timeout[denali->flash_bank])
Jason Robertsce082592010-05-13 15:57:33 +0100207 printk(KERN_ERR "reset bank failed.\n");
Jason Robertsce082592010-05-13 15:57:33 +0100208}
209
210/* Reset the flash controller */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800211static uint16_t denali_nand_reset(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +0100212{
213 uint32_t i;
214
215 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
216 __FILE__, __LINE__, __func__);
217
218 for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800219 iowrite32(reset_complete[i] | operation_timeout[i],
Jason Robertsce082592010-05-13 15:57:33 +0100220 denali->flash_reg + intr_status_addresses[i]);
221
222 for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++) {
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800223 iowrite32(device_reset_banks[i],
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800224 denali->flash_reg + DEVICE_RESET);
225 while (!(ioread32(denali->flash_reg +
226 intr_status_addresses[i]) &
Jason Robertsce082592010-05-13 15:57:33 +0100227 (reset_complete[i] | operation_timeout[i])))
228 ;
229 if (ioread32(denali->flash_reg + intr_status_addresses[i]) &
230 operation_timeout[i])
231 nand_dbg_print(NAND_DBG_WARN,
232 "NAND Reset operation timed out on bank %d\n", i);
233 }
234
235 for (i = 0; i < LLD_MAX_FLASH_BANKS; i++)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800236 iowrite32(reset_complete[i] | operation_timeout[i],
Jason Robertsce082592010-05-13 15:57:33 +0100237 denali->flash_reg + intr_status_addresses[i]);
238
239 return PASS;
240}
241
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800242/* this routine calculates the ONFI timing values for a given mode and
243 * programs the clocking register accordingly. The mode is determined by
244 * the get_onfi_nand_para routine.
Jason Robertsce082592010-05-13 15:57:33 +0100245 */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800246static void nand_onfi_timing_set(struct denali_nand_info *denali,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800247 uint16_t mode)
Jason Robertsce082592010-05-13 15:57:33 +0100248{
249 uint16_t Trea[6] = {40, 30, 25, 20, 20, 16};
250 uint16_t Trp[6] = {50, 25, 17, 15, 12, 10};
251 uint16_t Treh[6] = {30, 15, 15, 10, 10, 7};
252 uint16_t Trc[6] = {100, 50, 35, 30, 25, 20};
253 uint16_t Trhoh[6] = {0, 15, 15, 15, 15, 15};
254 uint16_t Trloh[6] = {0, 0, 0, 0, 5, 5};
255 uint16_t Tcea[6] = {100, 45, 30, 25, 25, 25};
256 uint16_t Tadl[6] = {200, 100, 100, 100, 70, 70};
257 uint16_t Trhw[6] = {200, 100, 100, 100, 100, 100};
258 uint16_t Trhz[6] = {200, 100, 100, 100, 100, 100};
259 uint16_t Twhr[6] = {120, 80, 80, 60, 60, 60};
260 uint16_t Tcs[6] = {70, 35, 25, 25, 20, 15};
261
262 uint16_t TclsRising = 1;
263 uint16_t data_invalid_rhoh, data_invalid_rloh, data_invalid;
264 uint16_t dv_window = 0;
265 uint16_t en_lo, en_hi;
266 uint16_t acc_clks;
267 uint16_t addr_2_data, re_2_we, re_2_re, we_2_re, cs_cnt;
268
269 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
270 __FILE__, __LINE__, __func__);
271
272 en_lo = CEIL_DIV(Trp[mode], CLK_X);
273 en_hi = CEIL_DIV(Treh[mode], CLK_X);
274#if ONFI_BLOOM_TIME
275 if ((en_hi * CLK_X) < (Treh[mode] + 2))
276 en_hi++;
277#endif
278
279 if ((en_lo + en_hi) * CLK_X < Trc[mode])
280 en_lo += CEIL_DIV((Trc[mode] - (en_lo + en_hi) * CLK_X), CLK_X);
281
282 if ((en_lo + en_hi) < CLK_MULTI)
283 en_lo += CLK_MULTI - en_lo - en_hi;
284
285 while (dv_window < 8) {
286 data_invalid_rhoh = en_lo * CLK_X + Trhoh[mode];
287
288 data_invalid_rloh = (en_lo + en_hi) * CLK_X + Trloh[mode];
289
290 data_invalid =
291 data_invalid_rhoh <
292 data_invalid_rloh ? data_invalid_rhoh : data_invalid_rloh;
293
294 dv_window = data_invalid - Trea[mode];
295
296 if (dv_window < 8)
297 en_lo++;
298 }
299
300 acc_clks = CEIL_DIV(Trea[mode], CLK_X);
301
302 while (((acc_clks * CLK_X) - Trea[mode]) < 3)
303 acc_clks++;
304
305 if ((data_invalid - acc_clks * CLK_X) < 2)
306 nand_dbg_print(NAND_DBG_WARN, "%s, Line %d: Warning!\n",
307 __FILE__, __LINE__);
308
309 addr_2_data = CEIL_DIV(Tadl[mode], CLK_X);
310 re_2_we = CEIL_DIV(Trhw[mode], CLK_X);
311 re_2_re = CEIL_DIV(Trhz[mode], CLK_X);
312 we_2_re = CEIL_DIV(Twhr[mode], CLK_X);
313 cs_cnt = CEIL_DIV((Tcs[mode] - Trp[mode]), CLK_X);
314 if (!TclsRising)
315 cs_cnt = CEIL_DIV(Tcs[mode], CLK_X);
316 if (cs_cnt == 0)
317 cs_cnt = 1;
318
319 if (Tcea[mode]) {
320 while (((cs_cnt * CLK_X) + Trea[mode]) < Tcea[mode])
321 cs_cnt++;
322 }
323
324#if MODE5_WORKAROUND
325 if (mode == 5)
326 acc_clks = 5;
327#endif
328
329 /* Sighting 3462430: Temporary hack for MT29F128G08CJABAWP:B */
330 if ((ioread32(denali->flash_reg + MANUFACTURER_ID) == 0) &&
331 (ioread32(denali->flash_reg + DEVICE_ID) == 0x88))
332 acc_clks = 6;
333
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800334 iowrite32(acc_clks, denali->flash_reg + ACC_CLKS);
335 iowrite32(re_2_we, denali->flash_reg + RE_2_WE);
336 iowrite32(re_2_re, denali->flash_reg + RE_2_RE);
337 iowrite32(we_2_re, denali->flash_reg + WE_2_RE);
338 iowrite32(addr_2_data, denali->flash_reg + ADDR_2_DATA);
339 iowrite32(en_lo, denali->flash_reg + RDWR_EN_LO_CNT);
340 iowrite32(en_hi, denali->flash_reg + RDWR_EN_HI_CNT);
341 iowrite32(cs_cnt, denali->flash_reg + CS_SETUP_CNT);
Jason Robertsce082592010-05-13 15:57:33 +0100342}
343
Jason Robertsce082592010-05-13 15:57:33 +0100344/* queries the NAND device to see what ONFI modes it supports. */
345static uint16_t get_onfi_nand_para(struct denali_nand_info *denali)
346{
347 int i;
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800348 /* we needn't to do a reset here because driver has already
349 * reset all the banks before
350 * */
Jason Robertsce082592010-05-13 15:57:33 +0100351 if (!(ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
352 ONFI_TIMING_MODE__VALUE))
353 return FAIL;
354
355 for (i = 5; i > 0; i--) {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800356 if (ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
357 (0x01 << i))
Jason Robertsce082592010-05-13 15:57:33 +0100358 break;
359 }
360
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800361 nand_onfi_timing_set(denali, i);
Jason Robertsce082592010-05-13 15:57:33 +0100362
363 /* By now, all the ONFI devices we know support the page cache */
364 /* rw feature. So here we enable the pipeline_rw_ahead feature */
365 /* iowrite32(1, denali->flash_reg + CACHE_WRITE_ENABLE); */
366 /* iowrite32(1, denali->flash_reg + CACHE_READ_ENABLE); */
367
368 return PASS;
369}
370
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800371static void get_samsung_nand_para(struct denali_nand_info *denali,
372 uint8_t device_id)
Jason Robertsce082592010-05-13 15:57:33 +0100373{
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800374 if (device_id == 0xd3) { /* Samsung K9WAG08U1A */
Jason Robertsce082592010-05-13 15:57:33 +0100375 /* Set timing register values according to datasheet */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800376 iowrite32(5, denali->flash_reg + ACC_CLKS);
377 iowrite32(20, denali->flash_reg + RE_2_WE);
378 iowrite32(12, denali->flash_reg + WE_2_RE);
379 iowrite32(14, denali->flash_reg + ADDR_2_DATA);
380 iowrite32(3, denali->flash_reg + RDWR_EN_LO_CNT);
381 iowrite32(2, denali->flash_reg + RDWR_EN_HI_CNT);
382 iowrite32(2, denali->flash_reg + CS_SETUP_CNT);
Jason Robertsce082592010-05-13 15:57:33 +0100383 }
Jason Robertsce082592010-05-13 15:57:33 +0100384}
385
386static void get_toshiba_nand_para(struct denali_nand_info *denali)
387{
Jason Robertsce082592010-05-13 15:57:33 +0100388 uint32_t tmp;
389
390 /* Workaround to fix a controller bug which reports a wrong */
391 /* spare area size for some kind of Toshiba NAND device */
392 if ((ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) == 4096) &&
393 (ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) == 64)) {
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800394 iowrite32(216, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
Jason Robertsce082592010-05-13 15:57:33 +0100395 tmp = ioread32(denali->flash_reg + DEVICES_CONNECTED) *
396 ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800397 iowrite32(tmp,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800398 denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
Jason Robertsce082592010-05-13 15:57:33 +0100399#if SUPPORT_15BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800400 iowrite32(15, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100401#elif SUPPORT_8BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800402 iowrite32(8, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100403#endif
404 }
Jason Robertsce082592010-05-13 15:57:33 +0100405}
406
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800407static void get_hynix_nand_para(struct denali_nand_info *denali,
408 uint8_t device_id)
Jason Robertsce082592010-05-13 15:57:33 +0100409{
Jason Robertsce082592010-05-13 15:57:33 +0100410 uint32_t main_size, spare_size;
411
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800412 switch (device_id) {
Jason Robertsce082592010-05-13 15:57:33 +0100413 case 0xD5: /* Hynix H27UAG8T2A, H27UBG8U5A or H27UCG8VFA */
414 case 0xD7: /* Hynix H27UDG8VEM, H27UCG8UDM or H27UCG8V5A */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800415 iowrite32(128, denali->flash_reg + PAGES_PER_BLOCK);
416 iowrite32(4096, denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
417 iowrite32(224, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800418 main_size = 4096 *
419 ioread32(denali->flash_reg + DEVICES_CONNECTED);
420 spare_size = 224 *
421 ioread32(denali->flash_reg + DEVICES_CONNECTED);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800422 iowrite32(main_size,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800423 denali->flash_reg + LOGICAL_PAGE_DATA_SIZE);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800424 iowrite32(spare_size,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800425 denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800426 iowrite32(0, denali->flash_reg + DEVICE_WIDTH);
Jason Robertsce082592010-05-13 15:57:33 +0100427#if SUPPORT_15BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800428 iowrite32(15, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100429#elif SUPPORT_8BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800430 iowrite32(8, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100431#endif
Jason Robertsce082592010-05-13 15:57:33 +0100432 break;
433 default:
434 nand_dbg_print(NAND_DBG_WARN,
435 "Spectra: Unknown Hynix NAND (Device ID: 0x%x)."
436 "Will use default parameter values instead.\n",
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800437 device_id);
Jason Robertsce082592010-05-13 15:57:33 +0100438 }
439}
440
441/* determines how many NAND chips are connected to the controller. Note for
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800442 Intel CE4100 devices we don't support more than one device.
Jason Robertsce082592010-05-13 15:57:33 +0100443 */
444static void find_valid_banks(struct denali_nand_info *denali)
445{
446 uint32_t id[LLD_MAX_FLASH_BANKS];
447 int i;
448
449 denali->total_used_banks = 1;
450 for (i = 0; i < LLD_MAX_FLASH_BANKS; i++) {
451 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 0), 0x90);
452 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 1), 0);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800453 index_addr_read_data(denali,
454 (uint32_t)(MODE_11 | (i << 24) | 2), &id[i]);
Jason Robertsce082592010-05-13 15:57:33 +0100455
456 nand_dbg_print(NAND_DBG_DEBUG,
457 "Return 1st ID for bank[%d]: %x\n", i, id[i]);
458
459 if (i == 0) {
460 if (!(id[i] & 0x0ff))
461 break; /* WTF? */
462 } else {
463 if ((id[i] & 0x0ff) == (id[0] & 0x0ff))
464 denali->total_used_banks++;
465 else
466 break;
467 }
468 }
469
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800470 if (denali->platform == INTEL_CE4100) {
Jason Robertsce082592010-05-13 15:57:33 +0100471 /* Platform limitations of the CE4100 device limit
472 * users to a single chip solution for NAND.
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800473 * Multichip support is not enabled.
474 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800475 if (denali->total_used_banks != 1) {
Jason Robertsce082592010-05-13 15:57:33 +0100476 printk(KERN_ERR "Sorry, Intel CE4100 only supports "
477 "a single NAND device.\n");
478 BUG();
479 }
480 }
481 nand_dbg_print(NAND_DBG_DEBUG,
482 "denali->total_used_banks: %d\n", denali->total_used_banks);
483}
484
485static void detect_partition_feature(struct denali_nand_info *denali)
486{
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800487 /* For MRST platform, denali->fwblks represent the
488 * number of blocks firmware is taken,
489 * FW is in protect partition and MTD driver has no
490 * permission to access it. So let driver know how many
491 * blocks it can't touch.
492 * */
Jason Robertsce082592010-05-13 15:57:33 +0100493 if (ioread32(denali->flash_reg + FEATURES) & FEATURES__PARTITION) {
494 if ((ioread32(denali->flash_reg + PERM_SRC_ID_1) &
495 PERM_SRC_ID_1__SRCID) == SPECTRA_PARTITION_ID) {
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800496 denali->fwblks =
Jason Robertsce082592010-05-13 15:57:33 +0100497 ((ioread32(denali->flash_reg + MIN_MAX_BANK_1) &
498 MIN_MAX_BANK_1__MIN_VALUE) *
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800499 denali->blksperchip)
Jason Robertsce082592010-05-13 15:57:33 +0100500 +
501 (ioread32(denali->flash_reg + MIN_BLK_ADDR_1) &
502 MIN_BLK_ADDR_1__VALUE);
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800503 } else
504 denali->fwblks = SPECTRA_START_BLOCK;
505 } else
506 denali->fwblks = SPECTRA_START_BLOCK;
Jason Robertsce082592010-05-13 15:57:33 +0100507}
508
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800509static uint16_t denali_nand_timing_set(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +0100510{
511 uint16_t status = PASS;
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800512 uint32_t id_bytes[5], addr;
513 uint8_t i, maf_id, device_id;
Jason Robertsce082592010-05-13 15:57:33 +0100514
515 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
516 __FILE__, __LINE__, __func__);
517
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800518 /* Use read id method to get device ID and other
519 * params. For some NAND chips, controller can't
520 * report the correct device ID by reading from
521 * DEVICE_ID register
522 * */
523 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
524 index_addr(denali, (uint32_t)addr | 0, 0x90);
525 index_addr(denali, (uint32_t)addr | 1, 0);
526 for (i = 0; i < 5; i++)
527 index_addr_read_data(denali, addr | 2, &id_bytes[i]);
528 maf_id = id_bytes[0];
529 device_id = id_bytes[1];
Jason Robertsce082592010-05-13 15:57:33 +0100530
531 if (ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) &
532 ONFI_DEVICE_NO_OF_LUNS__ONFI_DEVICE) { /* ONFI 1.0 NAND */
533 if (FAIL == get_onfi_nand_para(denali))
534 return FAIL;
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800535 } else if (maf_id == 0xEC) { /* Samsung NAND */
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800536 get_samsung_nand_para(denali, device_id);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800537 } else if (maf_id == 0x98) { /* Toshiba NAND */
Jason Robertsce082592010-05-13 15:57:33 +0100538 get_toshiba_nand_para(denali);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800539 } else if (maf_id == 0xAD) { /* Hynix NAND */
540 get_hynix_nand_para(denali, device_id);
Jason Robertsce082592010-05-13 15:57:33 +0100541 }
542
543 nand_dbg_print(NAND_DBG_DEBUG, "Dump timing register values:"
544 "acc_clks: %d, re_2_we: %d, we_2_re: %d,"
545 "addr_2_data: %d, rdwr_en_lo_cnt: %d, "
546 "rdwr_en_hi_cnt: %d, cs_setup_cnt: %d\n",
547 ioread32(denali->flash_reg + ACC_CLKS),
548 ioread32(denali->flash_reg + RE_2_WE),
549 ioread32(denali->flash_reg + WE_2_RE),
550 ioread32(denali->flash_reg + ADDR_2_DATA),
551 ioread32(denali->flash_reg + RDWR_EN_LO_CNT),
552 ioread32(denali->flash_reg + RDWR_EN_HI_CNT),
553 ioread32(denali->flash_reg + CS_SETUP_CNT));
554
Jason Robertsce082592010-05-13 15:57:33 +0100555 find_valid_banks(denali);
556
557 detect_partition_feature(denali);
558
Jason Robertsce082592010-05-13 15:57:33 +0100559 /* If the user specified to override the default timings
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800560 * with a specific ONFI mode, we apply those changes here.
Jason Robertsce082592010-05-13 15:57:33 +0100561 */
562 if (onfi_timing_mode != NAND_DEFAULT_TIMINGS)
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800563 nand_onfi_timing_set(denali, onfi_timing_mode);
Jason Robertsce082592010-05-13 15:57:33 +0100564
565 return status;
566}
567
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800568static void denali_set_intr_modes(struct denali_nand_info *denali,
Jason Robertsce082592010-05-13 15:57:33 +0100569 uint16_t INT_ENABLE)
570{
571 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
572 __FILE__, __LINE__, __func__);
573
574 if (INT_ENABLE)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800575 iowrite32(1, denali->flash_reg + GLOBAL_INT_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +0100576 else
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800577 iowrite32(0, denali->flash_reg + GLOBAL_INT_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +0100578}
579
580/* validation function to verify that the controlling software is making
581 a valid request
582 */
583static inline bool is_flash_bank_valid(int flash_bank)
584{
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800585 return (flash_bank >= 0 && flash_bank < 4);
Jason Robertsce082592010-05-13 15:57:33 +0100586}
587
588static void denali_irq_init(struct denali_nand_info *denali)
589{
590 uint32_t int_mask = 0;
591
592 /* Disable global interrupts */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800593 denali_set_intr_modes(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +0100594
595 int_mask = DENALI_IRQ_ALL;
596
597 /* Clear all status bits */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800598 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS0);
599 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS1);
600 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS2);
601 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS3);
Jason Robertsce082592010-05-13 15:57:33 +0100602
603 denali_irq_enable(denali, int_mask);
604}
605
606static void denali_irq_cleanup(int irqnum, struct denali_nand_info *denali)
607{
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800608 denali_set_intr_modes(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +0100609 free_irq(irqnum, denali);
610}
611
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800612static void denali_irq_enable(struct denali_nand_info *denali,
613 uint32_t int_mask)
Jason Robertsce082592010-05-13 15:57:33 +0100614{
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800615 iowrite32(int_mask, denali->flash_reg + INTR_EN0);
616 iowrite32(int_mask, denali->flash_reg + INTR_EN1);
617 iowrite32(int_mask, denali->flash_reg + INTR_EN2);
618 iowrite32(int_mask, denali->flash_reg + INTR_EN3);
Jason Robertsce082592010-05-13 15:57:33 +0100619}
620
621/* This function only returns when an interrupt that this driver cares about
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800622 * occurs. This is to reduce the overhead of servicing interrupts
Jason Robertsce082592010-05-13 15:57:33 +0100623 */
624static inline uint32_t denali_irq_detected(struct denali_nand_info *denali)
625{
Chuanxiao Donga99d1792010-07-27 11:32:21 +0800626 return read_interrupt_status(denali) & DENALI_IRQ_ALL;
Jason Robertsce082592010-05-13 15:57:33 +0100627}
628
629/* Interrupts are cleared by writing a 1 to the appropriate status bit */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800630static inline void clear_interrupt(struct denali_nand_info *denali,
631 uint32_t irq_mask)
Jason Robertsce082592010-05-13 15:57:33 +0100632{
633 uint32_t intr_status_reg = 0;
634
635 intr_status_reg = intr_status_addresses[denali->flash_bank];
636
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800637 iowrite32(irq_mask, denali->flash_reg + intr_status_reg);
Jason Robertsce082592010-05-13 15:57:33 +0100638}
639
640static void clear_interrupts(struct denali_nand_info *denali)
641{
642 uint32_t status = 0x0;
643 spin_lock_irq(&denali->irq_lock);
644
645 status = read_interrupt_status(denali);
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800646 clear_interrupt(denali, status);
Jason Robertsce082592010-05-13 15:57:33 +0100647
648#if DEBUG_DENALI
649 denali->irq_debug_array[denali->idx++] = 0x30000000 | status;
650 denali->idx %= 32;
651#endif
652
653 denali->irq_status = 0x0;
654 spin_unlock_irq(&denali->irq_lock);
655}
656
657static uint32_t read_interrupt_status(struct denali_nand_info *denali)
658{
659 uint32_t intr_status_reg = 0;
660
661 intr_status_reg = intr_status_addresses[denali->flash_bank];
662
663 return ioread32(denali->flash_reg + intr_status_reg);
664}
665
666#if DEBUG_DENALI
667static void print_irq_log(struct denali_nand_info *denali)
668{
669 int i = 0;
670
Chuanxiao Dongbf1806d2010-07-27 10:48:34 +0800671 printk(KERN_INFO "ISR debug log index = %X\n", denali->idx);
Jason Robertsce082592010-05-13 15:57:33 +0100672 for (i = 0; i < 32; i++)
Chuanxiao Dongbf1806d2010-07-27 10:48:34 +0800673 printk(KERN_INFO "%08X: %08X\n", i, denali->irq_debug_array[i]);
Jason Robertsce082592010-05-13 15:57:33 +0100674}
675#endif
676
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800677/* This is the interrupt service routine. It handles all interrupts
678 * sent to this device. Note that on CE4100, this is a shared
679 * interrupt.
Jason Robertsce082592010-05-13 15:57:33 +0100680 */
681static irqreturn_t denali_isr(int irq, void *dev_id)
682{
683 struct denali_nand_info *denali = dev_id;
684 uint32_t irq_status = 0x0;
685 irqreturn_t result = IRQ_NONE;
686
687 spin_lock(&denali->irq_lock);
688
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800689 /* check to see if a valid NAND chip has
690 * been selected.
Jason Robertsce082592010-05-13 15:57:33 +0100691 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800692 if (is_flash_bank_valid(denali->flash_bank)) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800693 /* check to see if controller generated
Jason Robertsce082592010-05-13 15:57:33 +0100694 * the interrupt, since this is a shared interrupt */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800695 irq_status = denali_irq_detected(denali);
696 if (irq_status != 0) {
Jason Robertsce082592010-05-13 15:57:33 +0100697#if DEBUG_DENALI
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800698 denali->irq_debug_array[denali->idx++] =
699 0x10000000 | irq_status;
Jason Robertsce082592010-05-13 15:57:33 +0100700 denali->idx %= 32;
701
Chuanxiao Dongbf1806d2010-07-27 10:48:34 +0800702 printk(KERN_INFO "IRQ status = 0x%04x\n", irq_status);
Jason Robertsce082592010-05-13 15:57:33 +0100703#endif
704 /* handle interrupt */
705 /* first acknowledge it */
706 clear_interrupt(denali, irq_status);
707 /* store the status in the device context for someone
708 to read */
709 denali->irq_status |= irq_status;
710 /* notify anyone who cares that it happened */
711 complete(&denali->complete);
712 /* tell the OS that we've handled this */
713 result = IRQ_HANDLED;
714 }
715 }
716 spin_unlock(&denali->irq_lock);
717 return result;
718}
719#define BANK(x) ((x) << 24)
720
721static uint32_t wait_for_irq(struct denali_nand_info *denali, uint32_t irq_mask)
722{
723 unsigned long comp_res = 0;
724 uint32_t intr_status = 0;
725 bool retry = false;
726 unsigned long timeout = msecs_to_jiffies(1000);
727
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800728 do {
Jason Robertsce082592010-05-13 15:57:33 +0100729#if DEBUG_DENALI
Chuanxiao Dongbf1806d2010-07-27 10:48:34 +0800730 printk(KERN_INFO "waiting for 0x%x\n", irq_mask);
Jason Robertsce082592010-05-13 15:57:33 +0100731#endif
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800732 comp_res =
733 wait_for_completion_timeout(&denali->complete, timeout);
Jason Robertsce082592010-05-13 15:57:33 +0100734 spin_lock_irq(&denali->irq_lock);
735 intr_status = denali->irq_status;
736
737#if DEBUG_DENALI
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800738 denali->irq_debug_array[denali->idx++] =
739 0x20000000 | (irq_mask << 16) | intr_status;
Jason Robertsce082592010-05-13 15:57:33 +0100740 denali->idx %= 32;
741#endif
742
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800743 if (intr_status & irq_mask) {
Jason Robertsce082592010-05-13 15:57:33 +0100744 denali->irq_status &= ~irq_mask;
745 spin_unlock_irq(&denali->irq_lock);
746#if DEBUG_DENALI
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800747 if (retry)
748 printk(KERN_INFO "status on retry = 0x%x\n",
749 intr_status);
Jason Robertsce082592010-05-13 15:57:33 +0100750#endif
751 /* our interrupt was detected */
752 break;
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800753 } else {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800754 /* these are not the interrupts you are looking for -
755 * need to wait again */
Jason Robertsce082592010-05-13 15:57:33 +0100756 spin_unlock_irq(&denali->irq_lock);
757#if DEBUG_DENALI
758 print_irq_log(denali);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800759 printk(KERN_INFO "received irq nobody cared:"
760 " irq_status = 0x%x, irq_mask = 0x%x,"
761 " timeout = %ld\n", intr_status,
762 irq_mask, comp_res);
Jason Robertsce082592010-05-13 15:57:33 +0100763#endif
764 retry = true;
765 }
766 } while (comp_res != 0);
767
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800768 if (comp_res == 0) {
Jason Robertsce082592010-05-13 15:57:33 +0100769 /* timeout */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800770 printk(KERN_ERR "timeout occurred, status = 0x%x, mask = 0x%x\n",
771 intr_status, irq_mask);
Jason Robertsce082592010-05-13 15:57:33 +0100772
773 intr_status = 0;
774 }
775 return intr_status;
776}
777
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800778/* This helper function setups the registers for ECC and whether or not
Jason Robertsce082592010-05-13 15:57:33 +0100779 the spare area will be transfered. */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800780static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en,
Jason Robertsce082592010-05-13 15:57:33 +0100781 bool transfer_spare)
782{
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800783 int ecc_en_flag = 0, transfer_spare_flag = 0;
Jason Robertsce082592010-05-13 15:57:33 +0100784
785 /* set ECC, transfer spare bits if needed */
786 ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0;
787 transfer_spare_flag = transfer_spare ? TRANSFER_SPARE_REG__FLAG : 0;
788
789 /* Enable spare area/ECC per user's request. */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800790 iowrite32(ecc_en_flag, denali->flash_reg + ECC_ENABLE);
791 iowrite32(transfer_spare_flag,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800792 denali->flash_reg + TRANSFER_SPARE_REG);
Jason Robertsce082592010-05-13 15:57:33 +0100793}
794
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800795/* sends a pipeline command operation to the controller. See the Denali NAND
796 controller's user guide for more information (section 4.2.3.6).
Jason Robertsce082592010-05-13 15:57:33 +0100797 */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800798static int denali_send_pipeline_cmd(struct denali_nand_info *denali,
799 bool ecc_en,
800 bool transfer_spare,
801 int access_type,
802 int op)
Jason Robertsce082592010-05-13 15:57:33 +0100803{
804 int status = PASS;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800805 uint32_t addr = 0x0, cmd = 0x0, page_count = 1, irq_status = 0,
Jason Robertsce082592010-05-13 15:57:33 +0100806 irq_mask = 0;
807
Chuanxiao Donga99d1792010-07-27 11:32:21 +0800808 if (op == DENALI_READ)
809 irq_mask = INTR_STATUS0__LOAD_COMP;
810 else if (op == DENALI_WRITE)
811 irq_mask = 0;
812 else
813 BUG();
Jason Robertsce082592010-05-13 15:57:33 +0100814
815 setup_ecc_for_xfer(denali, ecc_en, transfer_spare);
816
817#if DEBUG_DENALI
818 spin_lock_irq(&denali->irq_lock);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800819 denali->irq_debug_array[denali->idx++] =
820 0x40000000 | ioread32(denali->flash_reg + ECC_ENABLE) |
821 (access_type << 4);
Jason Robertsce082592010-05-13 15:57:33 +0100822 denali->idx %= 32;
823 spin_unlock_irq(&denali->irq_lock);
824#endif
825
826
827 /* clear interrupts */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800828 clear_interrupts(denali);
Jason Robertsce082592010-05-13 15:57:33 +0100829
830 addr = BANK(denali->flash_bank) | denali->page;
831
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800832 if (op == DENALI_WRITE && access_type != SPARE_ACCESS) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800833 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800834 iowrite32(cmd, denali->flash_mem);
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800835 } else if (op == DENALI_WRITE && access_type == SPARE_ACCESS) {
Jason Robertsce082592010-05-13 15:57:33 +0100836 /* read spare area */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800837 cmd = MODE_10 | addr;
Jason Robertsce082592010-05-13 15:57:33 +0100838 index_addr(denali, (uint32_t)cmd, access_type);
839
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800840 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800841 iowrite32(cmd, denali->flash_mem);
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800842 } else if (op == DENALI_READ) {
Jason Robertsce082592010-05-13 15:57:33 +0100843 /* setup page read request for access type */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800844 cmd = MODE_10 | addr;
Jason Robertsce082592010-05-13 15:57:33 +0100845 index_addr(denali, (uint32_t)cmd, access_type);
846
847 /* page 33 of the NAND controller spec indicates we should not
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800848 use the pipeline commands in Spare area only mode. So we
Jason Robertsce082592010-05-13 15:57:33 +0100849 don't.
850 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800851 if (access_type == SPARE_ACCESS) {
Jason Robertsce082592010-05-13 15:57:33 +0100852 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800853 iowrite32(cmd, denali->flash_mem);
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800854 } else {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800855 index_addr(denali, (uint32_t)cmd,
856 0x2000 | op | page_count);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800857
858 /* wait for command to be accepted
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800859 * can always use status0 bit as the
860 * mask is identical for each
Jason Robertsce082592010-05-13 15:57:33 +0100861 * bank. */
862 irq_status = wait_for_irq(denali, irq_mask);
863
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800864 if (irq_status == 0) {
Jason Robertsce082592010-05-13 15:57:33 +0100865 printk(KERN_ERR "cmd, page, addr on timeout "
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800866 "(0x%x, 0x%x, 0x%x)\n", cmd,
867 denali->page, addr);
Jason Robertsce082592010-05-13 15:57:33 +0100868 status = FAIL;
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800869 } else {
Jason Robertsce082592010-05-13 15:57:33 +0100870 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800871 iowrite32(cmd, denali->flash_mem);
Jason Robertsce082592010-05-13 15:57:33 +0100872 }
873 }
874 }
875 return status;
876}
877
878/* helper function that simply writes a buffer to the flash */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800879static int write_data_to_flash_mem(struct denali_nand_info *denali,
880 const uint8_t *buf,
881 int len)
Jason Robertsce082592010-05-13 15:57:33 +0100882{
883 uint32_t i = 0, *buf32;
884
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800885 /* verify that the len is a multiple of 4. see comment in
886 * read_data_from_flash_mem() */
Jason Robertsce082592010-05-13 15:57:33 +0100887 BUG_ON((len % 4) != 0);
888
889 /* write the data to the flash memory */
890 buf32 = (uint32_t *)buf;
891 for (i = 0; i < len / 4; i++)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800892 iowrite32(*buf32++, denali->flash_mem + 0x10);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800893 return i*4; /* intent is to return the number of bytes read */
Jason Robertsce082592010-05-13 15:57:33 +0100894}
895
896/* helper function that simply reads a buffer from the flash */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800897static int read_data_from_flash_mem(struct denali_nand_info *denali,
898 uint8_t *buf,
899 int len)
Jason Robertsce082592010-05-13 15:57:33 +0100900{
901 uint32_t i = 0, *buf32;
902
903 /* we assume that len will be a multiple of 4, if not
904 * it would be nice to know about it ASAP rather than
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800905 * have random failures...
906 * This assumption is based on the fact that this
907 * function is designed to be used to read flash pages,
Jason Robertsce082592010-05-13 15:57:33 +0100908 * which are typically multiples of 4...
909 */
910
911 BUG_ON((len % 4) != 0);
912
913 /* transfer the data from the flash */
914 buf32 = (uint32_t *)buf;
915 for (i = 0; i < len / 4; i++)
Jason Robertsce082592010-05-13 15:57:33 +0100916 *buf32++ = ioread32(denali->flash_mem + 0x10);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800917 return i*4; /* intent is to return the number of bytes read */
Jason Robertsce082592010-05-13 15:57:33 +0100918}
919
920/* writes OOB data to the device */
921static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
922{
923 struct denali_nand_info *denali = mtd_to_denali(mtd);
924 uint32_t irq_status = 0;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800925 uint32_t irq_mask = INTR_STATUS0__PROGRAM_COMP |
Jason Robertsce082592010-05-13 15:57:33 +0100926 INTR_STATUS0__PROGRAM_FAIL;
927 int status = 0;
928
929 denali->page = page;
930
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800931 if (denali_send_pipeline_cmd(denali, false, false, SPARE_ACCESS,
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800932 DENALI_WRITE) == PASS) {
Jason Robertsce082592010-05-13 15:57:33 +0100933 write_data_to_flash_mem(denali, buf, mtd->oobsize);
934
935#if DEBUG_DENALI
936 spin_lock_irq(&denali->irq_lock);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800937 denali->irq_debug_array[denali->idx++] =
938 0x80000000 | mtd->oobsize;
Jason Robertsce082592010-05-13 15:57:33 +0100939 denali->idx %= 32;
940 spin_unlock_irq(&denali->irq_lock);
941#endif
942
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800943
Jason Robertsce082592010-05-13 15:57:33 +0100944 /* wait for operation to complete */
945 irq_status = wait_for_irq(denali, irq_mask);
946
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800947 if (irq_status == 0) {
Jason Robertsce082592010-05-13 15:57:33 +0100948 printk(KERN_ERR "OOB write failed\n");
949 status = -EIO;
950 }
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800951 } else {
Jason Robertsce082592010-05-13 15:57:33 +0100952 printk(KERN_ERR "unable to send pipeline command\n");
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800953 status = -EIO;
Jason Robertsce082592010-05-13 15:57:33 +0100954 }
955 return status;
956}
957
958/* reads OOB data from the device */
959static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
960{
961 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800962 uint32_t irq_mask = INTR_STATUS0__LOAD_COMP,
963 irq_status = 0, addr = 0x0, cmd = 0x0;
Jason Robertsce082592010-05-13 15:57:33 +0100964
965 denali->page = page;
966
967#if DEBUG_DENALI
Chuanxiao Dongbf1806d2010-07-27 10:48:34 +0800968 printk(KERN_INFO "read_oob %d\n", page);
Jason Robertsce082592010-05-13 15:57:33 +0100969#endif
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800970 if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800971 DENALI_READ) == PASS) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800972 read_data_from_flash_mem(denali, buf, mtd->oobsize);
Jason Robertsce082592010-05-13 15:57:33 +0100973
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800974 /* wait for command to be accepted
Jason Robertsce082592010-05-13 15:57:33 +0100975 * can always use status0 bit as the mask is identical for each
976 * bank. */
977 irq_status = wait_for_irq(denali, irq_mask);
978
979 if (irq_status == 0)
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800980 printk(KERN_ERR "page on OOB timeout %d\n",
981 denali->page);
Jason Robertsce082592010-05-13 15:57:33 +0100982
983 /* We set the device back to MAIN_ACCESS here as I observed
984 * instability with the controller if you do a block erase
985 * and the last transaction was a SPARE_ACCESS. Block erase
986 * is reliable (according to the MTD test infrastructure)
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800987 * if you are in MAIN_ACCESS.
Jason Robertsce082592010-05-13 15:57:33 +0100988 */
989 addr = BANK(denali->flash_bank) | denali->page;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800990 cmd = MODE_10 | addr;
Jason Robertsce082592010-05-13 15:57:33 +0100991 index_addr(denali, (uint32_t)cmd, MAIN_ACCESS);
992
993#if DEBUG_DENALI
994 spin_lock_irq(&denali->irq_lock);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800995 denali->irq_debug_array[denali->idx++] =
996 0x60000000 | mtd->oobsize;
Jason Robertsce082592010-05-13 15:57:33 +0100997 denali->idx %= 32;
998 spin_unlock_irq(&denali->irq_lock);
999#endif
1000 }
1001}
1002
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001003/* this function examines buffers to see if they contain data that
Jason Robertsce082592010-05-13 15:57:33 +01001004 * indicate that the buffer is part of an erased region of flash.
1005 */
1006bool is_erased(uint8_t *buf, int len)
1007{
1008 int i = 0;
1009 for (i = 0; i < len; i++)
Jason Robertsce082592010-05-13 15:57:33 +01001010 if (buf[i] != 0xFF)
Jason Robertsce082592010-05-13 15:57:33 +01001011 return false;
Jason Robertsce082592010-05-13 15:57:33 +01001012 return true;
1013}
1014#define ECC_SECTOR_SIZE 512
1015
1016#define ECC_SECTOR(x) (((x) & ECC_ERROR_ADDRESS__SECTOR_NR) >> 12)
1017#define ECC_BYTE(x) (((x) & ECC_ERROR_ADDRESS__OFFSET))
1018#define ECC_CORRECTION_VALUE(x) ((x) & ERR_CORRECTION_INFO__BYTEMASK)
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +08001019#define ECC_ERROR_CORRECTABLE(x) (!((x) & ERR_CORRECTION_INFO__ERROR_TYPE))
1020#define ECC_ERR_DEVICE(x) (((x) & ERR_CORRECTION_INFO__DEVICE_NR) >> 8)
Jason Robertsce082592010-05-13 15:57:33 +01001021#define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO)
1022
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001023static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf,
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +08001024 uint32_t irq_status)
Jason Robertsce082592010-05-13 15:57:33 +01001025{
1026 bool check_erased_page = false;
1027
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001028 if (irq_status & INTR_STATUS0__ECC_ERR) {
Jason Robertsce082592010-05-13 15:57:33 +01001029 /* read the ECC errors. we'll ignore them for now */
1030 uint32_t err_address = 0, err_correction_info = 0;
1031 uint32_t err_byte = 0, err_sector = 0, err_device = 0;
1032 uint32_t err_correction_value = 0;
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +08001033 denali_set_intr_modes(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +01001034
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001035 do {
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001036 err_address = ioread32(denali->flash_reg +
Jason Robertsce082592010-05-13 15:57:33 +01001037 ECC_ERROR_ADDRESS);
1038 err_sector = ECC_SECTOR(err_address);
1039 err_byte = ECC_BYTE(err_address);
1040
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001041 err_correction_info = ioread32(denali->flash_reg +
Jason Robertsce082592010-05-13 15:57:33 +01001042 ERR_CORRECTION_INFO);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001043 err_correction_value =
Jason Robertsce082592010-05-13 15:57:33 +01001044 ECC_CORRECTION_VALUE(err_correction_info);
1045 err_device = ECC_ERR_DEVICE(err_correction_info);
1046
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001047 if (ECC_ERROR_CORRECTABLE(err_correction_info)) {
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +08001048 /* If err_byte is larger than ECC_SECTOR_SIZE,
1049 * means error happend in OOB, so we ignore
1050 * it. It's no need for us to correct it
1051 * err_device is represented the NAND error
1052 * bits are happened in if there are more
1053 * than one NAND connected.
1054 * */
1055 if (err_byte < ECC_SECTOR_SIZE) {
1056 int offset;
1057 offset = (err_sector *
1058 ECC_SECTOR_SIZE +
1059 err_byte) *
1060 denali->devnum +
1061 err_device;
Jason Robertsce082592010-05-13 15:57:33 +01001062 /* correct the ECC error */
1063 buf[offset] ^= err_correction_value;
1064 denali->mtd.ecc_stats.corrected++;
Jason Robertsce082592010-05-13 15:57:33 +01001065 }
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001066 } else {
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001067 /* if the error is not correctable, need to
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001068 * look at the page to see if it is an erased
1069 * page. if so, then it's not a real ECC error
1070 * */
Jason Robertsce082592010-05-13 15:57:33 +01001071 check_erased_page = true;
1072 }
1073
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001074#if DEBUG_DENALI
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001075 printk(KERN_INFO "Detected ECC error in page %d:"
1076 " err_addr = 0x%08x, info to fix is"
1077 " 0x%08x\n", denali->page, err_address,
1078 err_correction_info);
Jason Robertsce082592010-05-13 15:57:33 +01001079#endif
1080 } while (!ECC_LAST_ERR(err_correction_info));
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +08001081 /* Once handle all ecc errors, controller will triger
1082 * a ECC_TRANSACTION_DONE interrupt, so here just wait
1083 * for a while for this interrupt
1084 * */
1085 while (!(read_interrupt_status(denali) &
1086 INTR_STATUS0__ECC_TRANSACTION_DONE))
1087 cpu_relax();
1088 clear_interrupts(denali);
1089 denali_set_intr_modes(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001090 }
1091 return check_erased_page;
1092}
1093
1094/* programs the controller to either enable/disable DMA transfers */
David Woodhouseaadff492010-05-13 16:12:43 +01001095static void denali_enable_dma(struct denali_nand_info *denali, bool en)
Jason Robertsce082592010-05-13 15:57:33 +01001096{
1097 uint32_t reg_val = 0x0;
1098
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001099 if (en)
1100 reg_val = DMA_ENABLE__FLAG;
Jason Robertsce082592010-05-13 15:57:33 +01001101
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001102 iowrite32(reg_val, denali->flash_reg + DMA_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +01001103 ioread32(denali->flash_reg + DMA_ENABLE);
1104}
1105
1106/* setups the HW to perform the data DMA */
David Woodhouseaadff492010-05-13 16:12:43 +01001107static void denali_setup_dma(struct denali_nand_info *denali, int op)
Jason Robertsce082592010-05-13 15:57:33 +01001108{
1109 uint32_t mode = 0x0;
1110 const int page_count = 1;
1111 dma_addr_t addr = denali->buf.dma_buf;
1112
1113 mode = MODE_10 | BANK(denali->flash_bank);
1114
1115 /* DMA is a four step process */
1116
1117 /* 1. setup transfer type and # of pages */
1118 index_addr(denali, mode | denali->page, 0x2000 | op | page_count);
1119
1120 /* 2. set memory high address bits 23:8 */
1121 index_addr(denali, mode | ((uint16_t)(addr >> 16) << 8), 0x2200);
1122
1123 /* 3. set memory low address bits 23:8 */
1124 index_addr(denali, mode | ((uint16_t)addr << 8), 0x2300);
1125
1126 /* 4. interrupt when complete, burst len = 64 bytes*/
1127 index_addr(denali, mode | 0x14000, 0x2400);
1128}
1129
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001130/* writes a page. user specifies type, and this function handles the
Jason Robertsce082592010-05-13 15:57:33 +01001131 configuration details. */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001132static void write_page(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001133 const uint8_t *buf, bool raw_xfer)
1134{
1135 struct denali_nand_info *denali = mtd_to_denali(mtd);
1136 struct pci_dev *pci_dev = denali->dev;
1137
1138 dma_addr_t addr = denali->buf.dma_buf;
1139 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1140
1141 uint32_t irq_status = 0;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001142 uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP |
Jason Robertsce082592010-05-13 15:57:33 +01001143 INTR_STATUS0__PROGRAM_FAIL;
1144
1145 /* if it is a raw xfer, we want to disable ecc, and send
1146 * the spare area.
1147 * !raw_xfer - enable ecc
1148 * raw_xfer - transfer spare
1149 */
1150 setup_ecc_for_xfer(denali, !raw_xfer, raw_xfer);
1151
1152 /* copy buffer into DMA buffer */
1153 memcpy(denali->buf.buf, buf, mtd->writesize);
1154
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001155 if (raw_xfer) {
Jason Robertsce082592010-05-13 15:57:33 +01001156 /* transfer the data to the spare area */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001157 memcpy(denali->buf.buf + mtd->writesize,
1158 chip->oob_poi,
1159 mtd->oobsize);
Jason Robertsce082592010-05-13 15:57:33 +01001160 }
1161
1162 pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_TODEVICE);
1163
1164 clear_interrupts(denali);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001165 denali_enable_dma(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001166
David Woodhouseaadff492010-05-13 16:12:43 +01001167 denali_setup_dma(denali, DENALI_WRITE);
Jason Robertsce082592010-05-13 15:57:33 +01001168
1169 /* wait for operation to complete */
1170 irq_status = wait_for_irq(denali, irq_mask);
1171
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001172 if (irq_status == 0) {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001173 printk(KERN_ERR "timeout on write_page"
1174 " (type = %d)\n", raw_xfer);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001175 denali->status =
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001176 (irq_status & INTR_STATUS0__PROGRAM_FAIL) ?
1177 NAND_STATUS_FAIL : PASS;
Jason Robertsce082592010-05-13 15:57:33 +01001178 }
1179
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001180 denali_enable_dma(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +01001181 pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_TODEVICE);
1182}
1183
1184/* NAND core entry points */
1185
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001186/* this is the callback that the NAND core calls to write a page. Since
1187 writing a page with ECC or without is similar, all the work is done
Jason Robertsce082592010-05-13 15:57:33 +01001188 by write_page above. */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001189static void denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001190 const uint8_t *buf)
1191{
1192 /* for regular page writes, we let HW handle all the ECC
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001193 * data written to the device. */
Jason Robertsce082592010-05-13 15:57:33 +01001194 write_page(mtd, chip, buf, false);
1195}
1196
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001197/* This is the callback that the NAND core calls to write a page without ECC.
Jason Robertsce082592010-05-13 15:57:33 +01001198 raw access is similiar to ECC page writes, so all the work is done in the
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001199 write_page() function above.
Jason Robertsce082592010-05-13 15:57:33 +01001200 */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001201static void denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001202 const uint8_t *buf)
1203{
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001204 /* for raw page writes, we want to disable ECC and simply write
Jason Robertsce082592010-05-13 15:57:33 +01001205 whatever data is in the buffer. */
1206 write_page(mtd, chip, buf, true);
1207}
1208
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001209static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001210 int page)
1211{
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001212 return write_oob_data(mtd, chip->oob_poi, page);
Jason Robertsce082592010-05-13 15:57:33 +01001213}
1214
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001215static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001216 int page, int sndcmd)
1217{
1218 read_oob_data(mtd, chip->oob_poi, page);
1219
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001220 return 0; /* notify NAND core to send command to
1221 NAND device. */
Jason Robertsce082592010-05-13 15:57:33 +01001222}
1223
1224static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1225 uint8_t *buf, int page)
1226{
1227 struct denali_nand_info *denali = mtd_to_denali(mtd);
1228 struct pci_dev *pci_dev = denali->dev;
1229
1230 dma_addr_t addr = denali->buf.dma_buf;
1231 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1232
1233 uint32_t irq_status = 0;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001234 uint32_t irq_mask = INTR_STATUS0__ECC_TRANSACTION_DONE |
Jason Robertsce082592010-05-13 15:57:33 +01001235 INTR_STATUS0__ECC_ERR;
1236 bool check_erased_page = false;
1237
1238 setup_ecc_for_xfer(denali, true, false);
1239
David Woodhouseaadff492010-05-13 16:12:43 +01001240 denali_enable_dma(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001241 pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
1242
1243 clear_interrupts(denali);
David Woodhouseaadff492010-05-13 16:12:43 +01001244 denali_setup_dma(denali, DENALI_READ);
Jason Robertsce082592010-05-13 15:57:33 +01001245
1246 /* wait for operation to complete */
1247 irq_status = wait_for_irq(denali, irq_mask);
1248
1249 pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
1250
1251 memcpy(buf, denali->buf.buf, mtd->writesize);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001252
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +08001253 check_erased_page = handle_ecc(denali, buf, irq_status);
David Woodhouseaadff492010-05-13 16:12:43 +01001254 denali_enable_dma(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +01001255
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001256 if (check_erased_page) {
Jason Robertsce082592010-05-13 15:57:33 +01001257 read_oob_data(&denali->mtd, chip->oob_poi, denali->page);
1258
1259 /* check ECC failures that may have occurred on erased pages */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001260 if (check_erased_page) {
Jason Robertsce082592010-05-13 15:57:33 +01001261 if (!is_erased(buf, denali->mtd.writesize))
Jason Robertsce082592010-05-13 15:57:33 +01001262 denali->mtd.ecc_stats.failed++;
Jason Robertsce082592010-05-13 15:57:33 +01001263 if (!is_erased(buf, denali->mtd.oobsize))
Jason Robertsce082592010-05-13 15:57:33 +01001264 denali->mtd.ecc_stats.failed++;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001265 }
Jason Robertsce082592010-05-13 15:57:33 +01001266 }
1267 return 0;
1268}
1269
1270static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1271 uint8_t *buf, int page)
1272{
1273 struct denali_nand_info *denali = mtd_to_denali(mtd);
1274 struct pci_dev *pci_dev = denali->dev;
1275
1276 dma_addr_t addr = denali->buf.dma_buf;
1277 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1278
1279 uint32_t irq_status = 0;
1280 uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001281
Jason Robertsce082592010-05-13 15:57:33 +01001282 setup_ecc_for_xfer(denali, false, true);
David Woodhouseaadff492010-05-13 16:12:43 +01001283 denali_enable_dma(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001284
1285 pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
1286
1287 clear_interrupts(denali);
David Woodhouseaadff492010-05-13 16:12:43 +01001288 denali_setup_dma(denali, DENALI_READ);
Jason Robertsce082592010-05-13 15:57:33 +01001289
1290 /* wait for operation to complete */
1291 irq_status = wait_for_irq(denali, irq_mask);
1292
1293 pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
1294
David Woodhouseaadff492010-05-13 16:12:43 +01001295 denali_enable_dma(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +01001296
1297 memcpy(buf, denali->buf.buf, mtd->writesize);
1298 memcpy(chip->oob_poi, denali->buf.buf + mtd->writesize, mtd->oobsize);
1299
1300 return 0;
1301}
1302
1303static uint8_t denali_read_byte(struct mtd_info *mtd)
1304{
1305 struct denali_nand_info *denali = mtd_to_denali(mtd);
1306 uint8_t result = 0xff;
1307
1308 if (denali->buf.head < denali->buf.tail)
Jason Robertsce082592010-05-13 15:57:33 +01001309 result = denali->buf.buf[denali->buf.head++];
Jason Robertsce082592010-05-13 15:57:33 +01001310
1311#if DEBUG_DENALI
Chuanxiao Dongbf1806d2010-07-27 10:48:34 +08001312 printk(KERN_INFO "read byte -> 0x%02x\n", result);
Jason Robertsce082592010-05-13 15:57:33 +01001313#endif
1314 return result;
1315}
1316
1317static void denali_select_chip(struct mtd_info *mtd, int chip)
1318{
1319 struct denali_nand_info *denali = mtd_to_denali(mtd);
1320#if DEBUG_DENALI
Chuanxiao Dongbf1806d2010-07-27 10:48:34 +08001321 printk(KERN_INFO "denali select chip %d\n", chip);
Jason Robertsce082592010-05-13 15:57:33 +01001322#endif
1323 spin_lock_irq(&denali->irq_lock);
1324 denali->flash_bank = chip;
1325 spin_unlock_irq(&denali->irq_lock);
1326}
1327
1328static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
1329{
1330 struct denali_nand_info *denali = mtd_to_denali(mtd);
1331 int status = denali->status;
1332 denali->status = 0;
1333
1334#if DEBUG_DENALI
Chuanxiao Dongbf1806d2010-07-27 10:48:34 +08001335 printk(KERN_INFO "waitfunc %d\n", status);
Jason Robertsce082592010-05-13 15:57:33 +01001336#endif
1337 return status;
1338}
1339
1340static void denali_erase(struct mtd_info *mtd, int page)
1341{
1342 struct denali_nand_info *denali = mtd_to_denali(mtd);
1343
1344 uint32_t cmd = 0x0, irq_status = 0;
1345
1346#if DEBUG_DENALI
Chuanxiao Dongbf1806d2010-07-27 10:48:34 +08001347 printk(KERN_INFO "erase page: %d\n", page);
Jason Robertsce082592010-05-13 15:57:33 +01001348#endif
1349 /* clear interrupts */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001350 clear_interrupts(denali);
Jason Robertsce082592010-05-13 15:57:33 +01001351
1352 /* setup page read request for access type */
1353 cmd = MODE_10 | BANK(denali->flash_bank) | page;
1354 index_addr(denali, (uint32_t)cmd, 0x1);
1355
1356 /* wait for erase to complete or failure to occur */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001357 irq_status = wait_for_irq(denali, INTR_STATUS0__ERASE_COMP |
Jason Robertsce082592010-05-13 15:57:33 +01001358 INTR_STATUS0__ERASE_FAIL);
1359
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001360 denali->status = (irq_status & INTR_STATUS0__ERASE_FAIL) ?
1361 NAND_STATUS_FAIL : PASS;
Jason Robertsce082592010-05-13 15:57:33 +01001362}
1363
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001364static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col,
Jason Robertsce082592010-05-13 15:57:33 +01001365 int page)
1366{
1367 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +08001368 uint32_t addr, id;
1369 int i;
Jason Robertsce082592010-05-13 15:57:33 +01001370
1371#if DEBUG_DENALI
Chuanxiao Dongbf1806d2010-07-27 10:48:34 +08001372 printk(KERN_INFO "cmdfunc: 0x%x %d %d\n", cmd, col, page);
Jason Robertsce082592010-05-13 15:57:33 +01001373#endif
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001374 switch (cmd) {
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001375 case NAND_CMD_PAGEPROG:
1376 break;
1377 case NAND_CMD_STATUS:
1378 read_status(denali);
1379 break;
1380 case NAND_CMD_READID:
1381 reset_buf(denali);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +08001382 /*sometimes ManufactureId read from register is not right
1383 * e.g. some of Micron MT29F32G08QAA MLC NAND chips
1384 * So here we send READID cmd to NAND insteand
1385 * */
1386 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
1387 index_addr(denali, (uint32_t)addr | 0, 0x90);
1388 index_addr(denali, (uint32_t)addr | 1, 0);
1389 for (i = 0; i < 5; i++) {
1390 index_addr_read_data(denali,
1391 (uint32_t)addr | 2,
1392 &id);
1393 write_byte_to_buf(denali, id);
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001394 }
1395 break;
1396 case NAND_CMD_READ0:
1397 case NAND_CMD_SEQIN:
1398 denali->page = page;
1399 break;
1400 case NAND_CMD_RESET:
1401 reset_bank(denali);
1402 break;
1403 case NAND_CMD_READOOB:
1404 /* TODO: Read OOB data */
1405 break;
1406 default:
1407 printk(KERN_ERR ": unsupported command"
1408 " received 0x%x\n", cmd);
1409 break;
Jason Robertsce082592010-05-13 15:57:33 +01001410 }
1411}
1412
1413/* stubs for ECC functions not used by the NAND core */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001414static int denali_ecc_calculate(struct mtd_info *mtd, const uint8_t *data,
Jason Robertsce082592010-05-13 15:57:33 +01001415 uint8_t *ecc_code)
1416{
1417 printk(KERN_ERR "denali_ecc_calculate called unexpectedly\n");
1418 BUG();
1419 return -EIO;
1420}
1421
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001422static int denali_ecc_correct(struct mtd_info *mtd, uint8_t *data,
Jason Robertsce082592010-05-13 15:57:33 +01001423 uint8_t *read_ecc, uint8_t *calc_ecc)
1424{
1425 printk(KERN_ERR "denali_ecc_correct called unexpectedly\n");
1426 BUG();
1427 return -EIO;
1428}
1429
1430static void denali_ecc_hwctl(struct mtd_info *mtd, int mode)
1431{
1432 printk(KERN_ERR "denali_ecc_hwctl called unexpectedly\n");
1433 BUG();
1434}
1435/* end NAND core entry points */
1436
1437/* Initialization code to bring the device up to a known good state */
1438static void denali_hw_init(struct denali_nand_info *denali)
1439{
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001440 /* tell driver how many bit controller will skip before
1441 * writing ECC code in OOB, this register may be already
1442 * set by firmware. So we read this value out.
1443 * if this value is 0, just let it be.
1444 * */
1445 denali->bbtskipbytes = ioread32(denali->flash_reg +
1446 SPARE_AREA_SKIP_BYTES);
Jason Robertsce082592010-05-13 15:57:33 +01001447 denali_irq_init(denali);
Chuanxiao Dongeda936e2010-07-27 14:17:37 +08001448 denali_nand_reset(denali);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001449 iowrite32(0x0F, denali->flash_reg + RB_PIN_ENABLED);
1450 iowrite32(CHIP_EN_DONT_CARE__FLAG,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001451 denali->flash_reg + CHIP_ENABLE_DONT_CARE);
Jason Robertsce082592010-05-13 15:57:33 +01001452
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001453 iowrite32(0x0, denali->flash_reg + SPARE_AREA_SKIP_BYTES);
1454 iowrite32(0xffff, denali->flash_reg + SPARE_AREA_MARKER);
Jason Robertsce082592010-05-13 15:57:33 +01001455
1456 /* Should set value for these registers when init */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001457 iowrite32(0, denali->flash_reg + TWO_ROW_ADDR_CYCLES);
1458 iowrite32(1, denali->flash_reg + ECC_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +01001459}
1460
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001461/* Althogh controller spec said SLC ECC is forceb to be 4bit,
1462 * but denali controller in MRST only support 15bit and 8bit ECC
1463 * correction
1464 * */
1465#define ECC_8BITS 14
1466static struct nand_ecclayout nand_8bit_oob = {
1467 .eccbytes = 14,
Jason Robertsce082592010-05-13 15:57:33 +01001468};
1469
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001470#define ECC_15BITS 26
1471static struct nand_ecclayout nand_15bit_oob = {
1472 .eccbytes = 26,
Jason Robertsce082592010-05-13 15:57:33 +01001473};
1474
1475static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1476static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1477
1478static struct nand_bbt_descr bbt_main_descr = {
1479 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1480 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1481 .offs = 8,
1482 .len = 4,
1483 .veroffs = 12,
1484 .maxblocks = 4,
1485 .pattern = bbt_pattern,
1486};
1487
1488static struct nand_bbt_descr bbt_mirror_descr = {
1489 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1490 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1491 .offs = 8,
1492 .len = 4,
1493 .veroffs = 12,
1494 .maxblocks = 4,
1495 .pattern = mirror_pattern,
1496};
1497
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001498/* initialize driver data structures */
Jason Robertsce082592010-05-13 15:57:33 +01001499void denali_drv_init(struct denali_nand_info *denali)
1500{
1501 denali->idx = 0;
1502
1503 /* setup interrupt handler */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001504 /* the completion object will be used to notify
Jason Robertsce082592010-05-13 15:57:33 +01001505 * the callee that the interrupt is done */
1506 init_completion(&denali->complete);
1507
1508 /* the spinlock will be used to synchronize the ISR
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001509 * with any element that might be access shared
Jason Robertsce082592010-05-13 15:57:33 +01001510 * data (interrupt status) */
1511 spin_lock_init(&denali->irq_lock);
1512
1513 /* indicate that MTD has not selected a valid bank yet */
1514 denali->flash_bank = CHIP_SELECT_INVALID;
1515
1516 /* initialize our irq_status variable to indicate no interrupts */
1517 denali->irq_status = 0;
1518}
1519
1520/* driver entry point */
1521static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
1522{
1523 int ret = -ENODEV;
1524 resource_size_t csr_base, mem_base;
1525 unsigned long csr_len, mem_len;
1526 struct denali_nand_info *denali;
1527
1528 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1529 __FILE__, __LINE__, __func__);
1530
1531 denali = kzalloc(sizeof(*denali), GFP_KERNEL);
1532 if (!denali)
1533 return -ENOMEM;
1534
1535 ret = pci_enable_device(dev);
1536 if (ret) {
1537 printk(KERN_ERR "Spectra: pci_enable_device failed.\n");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001538 goto failed_alloc_memery;
Jason Robertsce082592010-05-13 15:57:33 +01001539 }
1540
1541 if (id->driver_data == INTEL_CE4100) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001542 /* Due to a silicon limitation, we can only support
1543 * ONFI timing mode 1 and below.
1544 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001545 if (onfi_timing_mode < -1 || onfi_timing_mode > 1) {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001546 printk(KERN_ERR "Intel CE4100 only supports"
1547 " ONFI timing mode 1 or below\n");
Jason Robertsce082592010-05-13 15:57:33 +01001548 ret = -EINVAL;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001549 goto failed_enable_dev;
Jason Robertsce082592010-05-13 15:57:33 +01001550 }
1551 denali->platform = INTEL_CE4100;
1552 mem_base = pci_resource_start(dev, 0);
1553 mem_len = pci_resource_len(dev, 1);
1554 csr_base = pci_resource_start(dev, 1);
1555 csr_len = pci_resource_len(dev, 1);
1556 } else {
1557 denali->platform = INTEL_MRST;
1558 csr_base = pci_resource_start(dev, 0);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001559 csr_len = pci_resource_len(dev, 0);
Jason Robertsce082592010-05-13 15:57:33 +01001560 mem_base = pci_resource_start(dev, 1);
1561 mem_len = pci_resource_len(dev, 1);
1562 if (!mem_len) {
1563 mem_base = csr_base + csr_len;
1564 mem_len = csr_len;
1565 nand_dbg_print(NAND_DBG_WARN,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001566 "Spectra: No second"
1567 " BAR for PCI device;"
1568 " assuming %08Lx\n",
Jason Robertsce082592010-05-13 15:57:33 +01001569 (uint64_t)csr_base);
1570 }
1571 }
1572
1573 /* Is 32-bit DMA supported? */
1574 ret = pci_set_dma_mask(dev, DMA_BIT_MASK(32));
1575
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001576 if (ret) {
Jason Robertsce082592010-05-13 15:57:33 +01001577 printk(KERN_ERR "Spectra: no usable DMA configuration\n");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001578 goto failed_enable_dev;
Jason Robertsce082592010-05-13 15:57:33 +01001579 }
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001580 denali->buf.dma_buf =
1581 pci_map_single(dev, denali->buf.buf,
1582 DENALI_BUF_SIZE,
1583 PCI_DMA_BIDIRECTIONAL);
Jason Robertsce082592010-05-13 15:57:33 +01001584
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001585 if (pci_dma_mapping_error(dev, denali->buf.dma_buf)) {
Jason Robertsce082592010-05-13 15:57:33 +01001586 printk(KERN_ERR "Spectra: failed to map DMA buffer\n");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001587 goto failed_enable_dev;
Jason Robertsce082592010-05-13 15:57:33 +01001588 }
1589
1590 pci_set_master(dev);
1591 denali->dev = dev;
1592
1593 ret = pci_request_regions(dev, DENALI_NAND_NAME);
1594 if (ret) {
1595 printk(KERN_ERR "Spectra: Unable to request memory regions\n");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001596 goto failed_dma_map;
Jason Robertsce082592010-05-13 15:57:33 +01001597 }
1598
1599 denali->flash_reg = ioremap_nocache(csr_base, csr_len);
1600 if (!denali->flash_reg) {
1601 printk(KERN_ERR "Spectra: Unable to remap memory region\n");
1602 ret = -ENOMEM;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001603 goto failed_req_regions;
Jason Robertsce082592010-05-13 15:57:33 +01001604 }
1605 nand_dbg_print(NAND_DBG_DEBUG, "Spectra: CSR 0x%08Lx -> 0x%p (0x%lx)\n",
1606 (uint64_t)csr_base, denali->flash_reg, csr_len);
1607
1608 denali->flash_mem = ioremap_nocache(mem_base, mem_len);
1609 if (!denali->flash_mem) {
1610 printk(KERN_ERR "Spectra: ioremap_nocache failed!");
Jason Robertsce082592010-05-13 15:57:33 +01001611 ret = -ENOMEM;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001612 goto failed_remap_reg;
Jason Robertsce082592010-05-13 15:57:33 +01001613 }
1614
1615 nand_dbg_print(NAND_DBG_WARN,
1616 "Spectra: Remapped flash base address: "
1617 "0x%p, len: %ld\n",
1618 denali->flash_mem, csr_len);
1619
1620 denali_hw_init(denali);
1621 denali_drv_init(denali);
1622
1623 nand_dbg_print(NAND_DBG_DEBUG, "Spectra: IRQ %d\n", dev->irq);
1624 if (request_irq(dev->irq, denali_isr, IRQF_SHARED,
1625 DENALI_NAND_NAME, denali)) {
1626 printk(KERN_ERR "Spectra: Unable to allocate IRQ\n");
1627 ret = -ENODEV;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001628 goto failed_remap_mem;
Jason Robertsce082592010-05-13 15:57:33 +01001629 }
1630
1631 /* now that our ISR is registered, we can enable interrupts */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +08001632 denali_set_intr_modes(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001633
1634 pci_set_drvdata(dev, denali);
1635
Chuanxiao Dongeda936e2010-07-27 14:17:37 +08001636 denali_nand_timing_set(denali);
Jason Robertsce082592010-05-13 15:57:33 +01001637
1638 nand_dbg_print(NAND_DBG_DEBUG, "Dump timing register values:"
1639 "acc_clks: %d, re_2_we: %d, we_2_re: %d,"
1640 "addr_2_data: %d, rdwr_en_lo_cnt: %d, "
1641 "rdwr_en_hi_cnt: %d, cs_setup_cnt: %d\n",
1642 ioread32(denali->flash_reg + ACC_CLKS),
1643 ioread32(denali->flash_reg + RE_2_WE),
1644 ioread32(denali->flash_reg + WE_2_RE),
1645 ioread32(denali->flash_reg + ADDR_2_DATA),
1646 ioread32(denali->flash_reg + RDWR_EN_LO_CNT),
1647 ioread32(denali->flash_reg + RDWR_EN_HI_CNT),
1648 ioread32(denali->flash_reg + CS_SETUP_CNT));
1649
1650 denali->mtd.name = "Denali NAND";
1651 denali->mtd.owner = THIS_MODULE;
1652 denali->mtd.priv = &denali->nand;
1653
1654 /* register the driver with the NAND core subsystem */
1655 denali->nand.select_chip = denali_select_chip;
1656 denali->nand.cmdfunc = denali_cmdfunc;
1657 denali->nand.read_byte = denali_read_byte;
1658 denali->nand.waitfunc = denali_waitfunc;
1659
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001660 /* scan for NAND devices attached to the controller
Jason Robertsce082592010-05-13 15:57:33 +01001661 * this is the first stage in a two step process to register
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001662 * with the nand subsystem */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001663 if (nand_scan_ident(&denali->mtd, LLD_MAX_FLASH_BANKS, NULL)) {
Jason Robertsce082592010-05-13 15:57:33 +01001664 ret = -ENXIO;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001665 goto failed_req_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001666 }
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001667
Chuanxiao.Dong66406522010-08-06 18:48:21 +08001668 /* MTD supported page sizes vary by kernel. We validate our
1669 * kernel supports the device here.
1670 */
1671 if (denali->mtd.writesize > NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE) {
1672 ret = -ENODEV;
1673 printk(KERN_ERR "Spectra: device size not supported by this "
1674 "version of MTD.");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001675 goto failed_req_irq;
Chuanxiao.Dong66406522010-08-06 18:48:21 +08001676 }
1677
Chuanxiao Dong08b9ab92010-08-06 18:19:09 +08001678 /* support for multi nand
1679 * MTD known nothing about multi nand,
1680 * so we should tell it the real pagesize
1681 * and anything necessery
1682 */
1683 denali->devnum = ioread32(denali->flash_reg + DEVICES_CONNECTED);
1684 denali->nand.chipsize <<= (denali->devnum - 1);
1685 denali->nand.page_shift += (denali->devnum - 1);
1686 denali->nand.pagemask = (denali->nand.chipsize >>
1687 denali->nand.page_shift) - 1;
1688 denali->nand.bbt_erase_shift += (denali->devnum - 1);
1689 denali->nand.phys_erase_shift = denali->nand.bbt_erase_shift;
1690 denali->nand.chip_shift += (denali->devnum - 1);
1691 denali->mtd.writesize <<= (denali->devnum - 1);
1692 denali->mtd.oobsize <<= (denali->devnum - 1);
1693 denali->mtd.erasesize <<= (denali->devnum - 1);
1694 denali->mtd.size = denali->nand.numchips * denali->nand.chipsize;
1695 denali->bbtskipbytes *= denali->devnum;
1696
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001697 /* second stage of the NAND scan
1698 * this stage requires information regarding ECC and
1699 * bad block management. */
Jason Robertsce082592010-05-13 15:57:33 +01001700
1701 /* Bad block management */
1702 denali->nand.bbt_td = &bbt_main_descr;
1703 denali->nand.bbt_md = &bbt_mirror_descr;
1704
1705 /* skip the scan for now until we have OOB read and write support */
1706 denali->nand.options |= NAND_USE_FLASH_BBT | NAND_SKIP_BBTSCAN;
1707 denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
1708
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001709 /* Denali Controller only support 15bit and 8bit ECC in MRST,
1710 * so just let controller do 15bit ECC for MLC and 8bit ECC for
1711 * SLC if possible.
1712 * */
1713 if (denali->nand.cellinfo & 0xc &&
1714 (denali->mtd.oobsize > (denali->bbtskipbytes +
1715 ECC_15BITS * (denali->mtd.writesize /
1716 ECC_SECTOR_SIZE)))) {
1717 /* if MLC OOB size is large enough, use 15bit ECC*/
1718 denali->nand.ecc.layout = &nand_15bit_oob;
1719 denali->nand.ecc.bytes = ECC_15BITS;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001720 iowrite32(15, denali->flash_reg + ECC_CORRECTION);
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001721 } else if (denali->mtd.oobsize < (denali->bbtskipbytes +
1722 ECC_8BITS * (denali->mtd.writesize /
1723 ECC_SECTOR_SIZE))) {
1724 printk(KERN_ERR "Your NAND chip OOB is not large enough to"
1725 " contain 8bit ECC correction codes");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001726 goto failed_req_irq;
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001727 } else {
1728 denali->nand.ecc.layout = &nand_8bit_oob;
1729 denali->nand.ecc.bytes = ECC_8BITS;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001730 iowrite32(8, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +01001731 }
1732
Chuanxiao Dong08b9ab92010-08-06 18:19:09 +08001733 denali->nand.ecc.bytes *= denali->devnum;
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001734 denali->nand.ecc.layout->eccbytes *=
1735 denali->mtd.writesize / ECC_SECTOR_SIZE;
1736 denali->nand.ecc.layout->oobfree[0].offset =
1737 denali->bbtskipbytes + denali->nand.ecc.layout->eccbytes;
1738 denali->nand.ecc.layout->oobfree[0].length =
1739 denali->mtd.oobsize - denali->nand.ecc.layout->eccbytes -
1740 denali->bbtskipbytes;
1741
Chuanxiao.Dong66406522010-08-06 18:48:21 +08001742 /* Let driver know the total blocks number and
1743 * how many blocks contained by each nand chip.
1744 * blksperchip will help driver to know how many
1745 * blocks is taken by FW.
1746 * */
1747 denali->totalblks = denali->mtd.size >>
1748 denali->nand.phys_erase_shift;
1749 denali->blksperchip = denali->totalblks / denali->nand.numchips;
1750
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001751 /* These functions are required by the NAND core framework, otherwise,
1752 * the NAND core will assert. However, we don't need them, so we'll stub
1753 * them out. */
Jason Robertsce082592010-05-13 15:57:33 +01001754 denali->nand.ecc.calculate = denali_ecc_calculate;
1755 denali->nand.ecc.correct = denali_ecc_correct;
1756 denali->nand.ecc.hwctl = denali_ecc_hwctl;
1757
1758 /* override the default read operations */
Chuanxiao Dong08b9ab92010-08-06 18:19:09 +08001759 denali->nand.ecc.size = ECC_SECTOR_SIZE * denali->devnum;
Jason Robertsce082592010-05-13 15:57:33 +01001760 denali->nand.ecc.read_page = denali_read_page;
1761 denali->nand.ecc.read_page_raw = denali_read_page_raw;
1762 denali->nand.ecc.write_page = denali_write_page;
1763 denali->nand.ecc.write_page_raw = denali_write_page_raw;
1764 denali->nand.ecc.read_oob = denali_read_oob;
1765 denali->nand.ecc.write_oob = denali_write_oob;
1766 denali->nand.erase_cmd = denali_erase;
1767
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001768 if (nand_scan_tail(&denali->mtd)) {
Jason Robertsce082592010-05-13 15:57:33 +01001769 ret = -ENXIO;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001770 goto failed_req_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001771 }
1772
1773 ret = add_mtd_device(&denali->mtd);
1774 if (ret) {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001775 printk(KERN_ERR "Spectra: Failed to register"
1776 " MTD device: %d\n", ret);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001777 goto failed_req_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001778 }
1779 return 0;
1780
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001781failed_req_irq:
Jason Robertsce082592010-05-13 15:57:33 +01001782 denali_irq_cleanup(dev->irq, denali);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001783failed_remap_mem:
Jason Robertsce082592010-05-13 15:57:33 +01001784 iounmap(denali->flash_mem);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001785failed_remap_reg:
1786 iounmap(denali->flash_reg);
1787failed_req_regions:
Jason Robertsce082592010-05-13 15:57:33 +01001788 pci_release_regions(dev);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001789failed_dma_map:
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001790 pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE,
Jason Robertsce082592010-05-13 15:57:33 +01001791 PCI_DMA_BIDIRECTIONAL);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001792failed_enable_dev:
1793 pci_disable_device(dev);
1794failed_alloc_memery:
Jason Robertsce082592010-05-13 15:57:33 +01001795 kfree(denali);
1796 return ret;
1797}
1798
1799/* driver exit point */
1800static void denali_pci_remove(struct pci_dev *dev)
1801{
1802 struct denali_nand_info *denali = pci_get_drvdata(dev);
1803
1804 nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
1805 __FILE__, __LINE__, __func__);
1806
1807 nand_release(&denali->mtd);
1808 del_mtd_device(&denali->mtd);
1809
1810 denali_irq_cleanup(dev->irq, denali);
1811
1812 iounmap(denali->flash_reg);
1813 iounmap(denali->flash_mem);
1814 pci_release_regions(dev);
1815 pci_disable_device(dev);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001816 pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE,
Jason Robertsce082592010-05-13 15:57:33 +01001817 PCI_DMA_BIDIRECTIONAL);
1818 pci_set_drvdata(dev, NULL);
1819 kfree(denali);
1820}
1821
1822MODULE_DEVICE_TABLE(pci, denali_pci_ids);
1823
1824static struct pci_driver denali_pci_driver = {
1825 .name = DENALI_NAND_NAME,
1826 .id_table = denali_pci_ids,
1827 .probe = denali_pci_probe,
1828 .remove = denali_pci_remove,
1829};
1830
1831static int __devinit denali_init(void)
1832{
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001833 printk(KERN_INFO "Spectra MTD driver built on %s @ %s\n",
1834 __DATE__, __TIME__);
Jason Robertsce082592010-05-13 15:57:33 +01001835 return pci_register_driver(&denali_pci_driver);
1836}
1837
1838/* Free memory */
1839static void __devexit denali_exit(void)
1840{
1841 pci_unregister_driver(&denali_pci_driver);
1842}
1843
1844module_init(denali_init);
1845module_exit(denali_exit);