blob: f59dd03f36d410939c2d5542d23d3d017cd9e15f [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
Jason Robertsce082592010-05-13 15:57:33 +0100122/* forward declarations */
123static void clear_interrupts(struct denali_nand_info *denali);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800124static uint32_t wait_for_irq(struct denali_nand_info *denali,
125 uint32_t irq_mask);
126static void denali_irq_enable(struct denali_nand_info *denali,
127 uint32_t int_mask);
Jason Robertsce082592010-05-13 15:57:33 +0100128static uint32_t read_interrupt_status(struct denali_nand_info *denali);
129
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800130/* Certain operations for the denali NAND controller use
131 * an indexed mode to read/write data. The operation is
132 * performed by writing the address value of the command
133 * to the device memory followed by the data. This function
134 * abstracts this common operation.
Jason Robertsce082592010-05-13 15:57:33 +0100135*/
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800136static void index_addr(struct denali_nand_info *denali,
137 uint32_t address, uint32_t data)
Jason Robertsce082592010-05-13 15:57:33 +0100138{
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800139 iowrite32(address, denali->flash_mem);
140 iowrite32(data, denali->flash_mem + 0x10);
Jason Robertsce082592010-05-13 15:57:33 +0100141}
142
143/* Perform an indexed read of the device */
144static void index_addr_read_data(struct denali_nand_info *denali,
145 uint32_t address, uint32_t *pdata)
146{
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800147 iowrite32(address, denali->flash_mem);
Jason Robertsce082592010-05-13 15:57:33 +0100148 *pdata = ioread32(denali->flash_mem + 0x10);
149}
150
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800151/* We need to buffer some data for some of the NAND core routines.
Jason Robertsce082592010-05-13 15:57:33 +0100152 * The operations manage buffering that data. */
153static void reset_buf(struct denali_nand_info *denali)
154{
155 denali->buf.head = denali->buf.tail = 0;
156}
157
158static void write_byte_to_buf(struct denali_nand_info *denali, uint8_t byte)
159{
160 BUG_ON(denali->buf.tail >= sizeof(denali->buf.buf));
161 denali->buf.buf[denali->buf.tail++] = byte;
162}
163
164/* reads the status of the device */
165static void read_status(struct denali_nand_info *denali)
166{
167 uint32_t cmd = 0x0;
168
169 /* initialize the data buffer to store status */
170 reset_buf(denali);
171
172 /* initiate a device status read */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800173 cmd = MODE_11 | BANK(denali->flash_bank);
Jason Robertsce082592010-05-13 15:57:33 +0100174 index_addr(denali, cmd | COMMAND_CYCLE, 0x70);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800175 iowrite32(cmd | STATUS_CYCLE, denali->flash_mem);
Jason Robertsce082592010-05-13 15:57:33 +0100176
177 /* update buffer with status value */
178 write_byte_to_buf(denali, ioread32(denali->flash_mem + 0x10));
Jason Robertsce082592010-05-13 15:57:33 +0100179}
180
181/* resets a specific device connected to the core */
182static void reset_bank(struct denali_nand_info *denali)
183{
184 uint32_t irq_status = 0;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800185 uint32_t irq_mask = reset_complete[denali->flash_bank] |
Jason Robertsce082592010-05-13 15:57:33 +0100186 operation_timeout[denali->flash_bank];
187 int bank = 0;
188
189 clear_interrupts(denali);
190
191 bank = device_reset_banks[denali->flash_bank];
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800192 iowrite32(bank, denali->flash_reg + DEVICE_RESET);
Jason Robertsce082592010-05-13 15:57:33 +0100193
194 irq_status = wait_for_irq(denali, irq_mask);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800195
Jason Robertsce082592010-05-13 15:57:33 +0100196 if (irq_status & operation_timeout[denali->flash_bank])
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800197 dev_err(&denali->dev->dev, "reset bank failed.\n");
Jason Robertsce082592010-05-13 15:57:33 +0100198}
199
200/* Reset the flash controller */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800201static uint16_t denali_nand_reset(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +0100202{
203 uint32_t i;
204
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800205 dev_dbg(&denali->dev->dev, "%s, Line %d, Function: %s\n",
Jason Robertsce082592010-05-13 15:57:33 +0100206 __FILE__, __LINE__, __func__);
207
208 for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800209 iowrite32(reset_complete[i] | operation_timeout[i],
Jason Robertsce082592010-05-13 15:57:33 +0100210 denali->flash_reg + intr_status_addresses[i]);
211
212 for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++) {
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800213 iowrite32(device_reset_banks[i],
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800214 denali->flash_reg + DEVICE_RESET);
215 while (!(ioread32(denali->flash_reg +
216 intr_status_addresses[i]) &
Jason Robertsce082592010-05-13 15:57:33 +0100217 (reset_complete[i] | operation_timeout[i])))
218 ;
219 if (ioread32(denali->flash_reg + intr_status_addresses[i]) &
220 operation_timeout[i])
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800221 dev_dbg(&denali->dev->dev,
Jason Robertsce082592010-05-13 15:57:33 +0100222 "NAND Reset operation timed out on bank %d\n", i);
223 }
224
225 for (i = 0; i < LLD_MAX_FLASH_BANKS; i++)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800226 iowrite32(reset_complete[i] | operation_timeout[i],
Jason Robertsce082592010-05-13 15:57:33 +0100227 denali->flash_reg + intr_status_addresses[i]);
228
229 return PASS;
230}
231
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800232/* this routine calculates the ONFI timing values for a given mode and
233 * programs the clocking register accordingly. The mode is determined by
234 * the get_onfi_nand_para routine.
Jason Robertsce082592010-05-13 15:57:33 +0100235 */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800236static void nand_onfi_timing_set(struct denali_nand_info *denali,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800237 uint16_t mode)
Jason Robertsce082592010-05-13 15:57:33 +0100238{
239 uint16_t Trea[6] = {40, 30, 25, 20, 20, 16};
240 uint16_t Trp[6] = {50, 25, 17, 15, 12, 10};
241 uint16_t Treh[6] = {30, 15, 15, 10, 10, 7};
242 uint16_t Trc[6] = {100, 50, 35, 30, 25, 20};
243 uint16_t Trhoh[6] = {0, 15, 15, 15, 15, 15};
244 uint16_t Trloh[6] = {0, 0, 0, 0, 5, 5};
245 uint16_t Tcea[6] = {100, 45, 30, 25, 25, 25};
246 uint16_t Tadl[6] = {200, 100, 100, 100, 70, 70};
247 uint16_t Trhw[6] = {200, 100, 100, 100, 100, 100};
248 uint16_t Trhz[6] = {200, 100, 100, 100, 100, 100};
249 uint16_t Twhr[6] = {120, 80, 80, 60, 60, 60};
250 uint16_t Tcs[6] = {70, 35, 25, 25, 20, 15};
251
252 uint16_t TclsRising = 1;
253 uint16_t data_invalid_rhoh, data_invalid_rloh, data_invalid;
254 uint16_t dv_window = 0;
255 uint16_t en_lo, en_hi;
256 uint16_t acc_clks;
257 uint16_t addr_2_data, re_2_we, re_2_re, we_2_re, cs_cnt;
258
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800259 dev_dbg(&denali->dev->dev, "%s, Line %d, Function: %s\n",
Jason Robertsce082592010-05-13 15:57:33 +0100260 __FILE__, __LINE__, __func__);
261
262 en_lo = CEIL_DIV(Trp[mode], CLK_X);
263 en_hi = CEIL_DIV(Treh[mode], CLK_X);
264#if ONFI_BLOOM_TIME
265 if ((en_hi * CLK_X) < (Treh[mode] + 2))
266 en_hi++;
267#endif
268
269 if ((en_lo + en_hi) * CLK_X < Trc[mode])
270 en_lo += CEIL_DIV((Trc[mode] - (en_lo + en_hi) * CLK_X), CLK_X);
271
272 if ((en_lo + en_hi) < CLK_MULTI)
273 en_lo += CLK_MULTI - en_lo - en_hi;
274
275 while (dv_window < 8) {
276 data_invalid_rhoh = en_lo * CLK_X + Trhoh[mode];
277
278 data_invalid_rloh = (en_lo + en_hi) * CLK_X + Trloh[mode];
279
280 data_invalid =
281 data_invalid_rhoh <
282 data_invalid_rloh ? data_invalid_rhoh : data_invalid_rloh;
283
284 dv_window = data_invalid - Trea[mode];
285
286 if (dv_window < 8)
287 en_lo++;
288 }
289
290 acc_clks = CEIL_DIV(Trea[mode], CLK_X);
291
292 while (((acc_clks * CLK_X) - Trea[mode]) < 3)
293 acc_clks++;
294
295 if ((data_invalid - acc_clks * CLK_X) < 2)
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800296 dev_warn(&denali->dev->dev, "%s, Line %d: Warning!\n",
Jason Robertsce082592010-05-13 15:57:33 +0100297 __FILE__, __LINE__);
298
299 addr_2_data = CEIL_DIV(Tadl[mode], CLK_X);
300 re_2_we = CEIL_DIV(Trhw[mode], CLK_X);
301 re_2_re = CEIL_DIV(Trhz[mode], CLK_X);
302 we_2_re = CEIL_DIV(Twhr[mode], CLK_X);
303 cs_cnt = CEIL_DIV((Tcs[mode] - Trp[mode]), CLK_X);
304 if (!TclsRising)
305 cs_cnt = CEIL_DIV(Tcs[mode], CLK_X);
306 if (cs_cnt == 0)
307 cs_cnt = 1;
308
309 if (Tcea[mode]) {
310 while (((cs_cnt * CLK_X) + Trea[mode]) < Tcea[mode])
311 cs_cnt++;
312 }
313
314#if MODE5_WORKAROUND
315 if (mode == 5)
316 acc_clks = 5;
317#endif
318
319 /* Sighting 3462430: Temporary hack for MT29F128G08CJABAWP:B */
320 if ((ioread32(denali->flash_reg + MANUFACTURER_ID) == 0) &&
321 (ioread32(denali->flash_reg + DEVICE_ID) == 0x88))
322 acc_clks = 6;
323
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800324 iowrite32(acc_clks, denali->flash_reg + ACC_CLKS);
325 iowrite32(re_2_we, denali->flash_reg + RE_2_WE);
326 iowrite32(re_2_re, denali->flash_reg + RE_2_RE);
327 iowrite32(we_2_re, denali->flash_reg + WE_2_RE);
328 iowrite32(addr_2_data, denali->flash_reg + ADDR_2_DATA);
329 iowrite32(en_lo, denali->flash_reg + RDWR_EN_LO_CNT);
330 iowrite32(en_hi, denali->flash_reg + RDWR_EN_HI_CNT);
331 iowrite32(cs_cnt, denali->flash_reg + CS_SETUP_CNT);
Jason Robertsce082592010-05-13 15:57:33 +0100332}
333
Jason Robertsce082592010-05-13 15:57:33 +0100334/* queries the NAND device to see what ONFI modes it supports. */
335static uint16_t get_onfi_nand_para(struct denali_nand_info *denali)
336{
337 int i;
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800338 /* we needn't to do a reset here because driver has already
339 * reset all the banks before
340 * */
Jason Robertsce082592010-05-13 15:57:33 +0100341 if (!(ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
342 ONFI_TIMING_MODE__VALUE))
343 return FAIL;
344
345 for (i = 5; i > 0; i--) {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800346 if (ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
347 (0x01 << i))
Jason Robertsce082592010-05-13 15:57:33 +0100348 break;
349 }
350
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800351 nand_onfi_timing_set(denali, i);
Jason Robertsce082592010-05-13 15:57:33 +0100352
353 /* By now, all the ONFI devices we know support the page cache */
354 /* rw feature. So here we enable the pipeline_rw_ahead feature */
355 /* iowrite32(1, denali->flash_reg + CACHE_WRITE_ENABLE); */
356 /* iowrite32(1, denali->flash_reg + CACHE_READ_ENABLE); */
357
358 return PASS;
359}
360
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800361static void get_samsung_nand_para(struct denali_nand_info *denali,
362 uint8_t device_id)
Jason Robertsce082592010-05-13 15:57:33 +0100363{
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800364 if (device_id == 0xd3) { /* Samsung K9WAG08U1A */
Jason Robertsce082592010-05-13 15:57:33 +0100365 /* Set timing register values according to datasheet */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800366 iowrite32(5, denali->flash_reg + ACC_CLKS);
367 iowrite32(20, denali->flash_reg + RE_2_WE);
368 iowrite32(12, denali->flash_reg + WE_2_RE);
369 iowrite32(14, denali->flash_reg + ADDR_2_DATA);
370 iowrite32(3, denali->flash_reg + RDWR_EN_LO_CNT);
371 iowrite32(2, denali->flash_reg + RDWR_EN_HI_CNT);
372 iowrite32(2, denali->flash_reg + CS_SETUP_CNT);
Jason Robertsce082592010-05-13 15:57:33 +0100373 }
Jason Robertsce082592010-05-13 15:57:33 +0100374}
375
376static void get_toshiba_nand_para(struct denali_nand_info *denali)
377{
Jason Robertsce082592010-05-13 15:57:33 +0100378 uint32_t tmp;
379
380 /* Workaround to fix a controller bug which reports a wrong */
381 /* spare area size for some kind of Toshiba NAND device */
382 if ((ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) == 4096) &&
383 (ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) == 64)) {
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800384 iowrite32(216, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
Jason Robertsce082592010-05-13 15:57:33 +0100385 tmp = ioread32(denali->flash_reg + DEVICES_CONNECTED) *
386 ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800387 iowrite32(tmp,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800388 denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
Jason Robertsce082592010-05-13 15:57:33 +0100389#if SUPPORT_15BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800390 iowrite32(15, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100391#elif SUPPORT_8BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800392 iowrite32(8, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100393#endif
394 }
Jason Robertsce082592010-05-13 15:57:33 +0100395}
396
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800397static void get_hynix_nand_para(struct denali_nand_info *denali,
398 uint8_t device_id)
Jason Robertsce082592010-05-13 15:57:33 +0100399{
Jason Robertsce082592010-05-13 15:57:33 +0100400 uint32_t main_size, spare_size;
401
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800402 switch (device_id) {
Jason Robertsce082592010-05-13 15:57:33 +0100403 case 0xD5: /* Hynix H27UAG8T2A, H27UBG8U5A or H27UCG8VFA */
404 case 0xD7: /* Hynix H27UDG8VEM, H27UCG8UDM or H27UCG8V5A */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800405 iowrite32(128, denali->flash_reg + PAGES_PER_BLOCK);
406 iowrite32(4096, denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
407 iowrite32(224, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800408 main_size = 4096 *
409 ioread32(denali->flash_reg + DEVICES_CONNECTED);
410 spare_size = 224 *
411 ioread32(denali->flash_reg + DEVICES_CONNECTED);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800412 iowrite32(main_size,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800413 denali->flash_reg + LOGICAL_PAGE_DATA_SIZE);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800414 iowrite32(spare_size,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800415 denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800416 iowrite32(0, denali->flash_reg + DEVICE_WIDTH);
Jason Robertsce082592010-05-13 15:57:33 +0100417#if SUPPORT_15BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800418 iowrite32(15, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100419#elif SUPPORT_8BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800420 iowrite32(8, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100421#endif
Jason Robertsce082592010-05-13 15:57:33 +0100422 break;
423 default:
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800424 dev_warn(&denali->dev->dev,
Jason Robertsce082592010-05-13 15:57:33 +0100425 "Spectra: Unknown Hynix NAND (Device ID: 0x%x)."
426 "Will use default parameter values instead.\n",
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800427 device_id);
Jason Robertsce082592010-05-13 15:57:33 +0100428 }
429}
430
431/* determines how many NAND chips are connected to the controller. Note for
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800432 Intel CE4100 devices we don't support more than one device.
Jason Robertsce082592010-05-13 15:57:33 +0100433 */
434static void find_valid_banks(struct denali_nand_info *denali)
435{
436 uint32_t id[LLD_MAX_FLASH_BANKS];
437 int i;
438
439 denali->total_used_banks = 1;
440 for (i = 0; i < LLD_MAX_FLASH_BANKS; i++) {
441 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 0), 0x90);
442 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 1), 0);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800443 index_addr_read_data(denali,
444 (uint32_t)(MODE_11 | (i << 24) | 2), &id[i]);
Jason Robertsce082592010-05-13 15:57:33 +0100445
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800446 dev_dbg(&denali->dev->dev,
Jason Robertsce082592010-05-13 15:57:33 +0100447 "Return 1st ID for bank[%d]: %x\n", i, id[i]);
448
449 if (i == 0) {
450 if (!(id[i] & 0x0ff))
451 break; /* WTF? */
452 } else {
453 if ((id[i] & 0x0ff) == (id[0] & 0x0ff))
454 denali->total_used_banks++;
455 else
456 break;
457 }
458 }
459
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800460 if (denali->platform == INTEL_CE4100) {
Jason Robertsce082592010-05-13 15:57:33 +0100461 /* Platform limitations of the CE4100 device limit
462 * users to a single chip solution for NAND.
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800463 * Multichip support is not enabled.
464 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800465 if (denali->total_used_banks != 1) {
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800466 dev_err(&denali->dev->dev,
467 "Sorry, Intel CE4100 only supports "
Jason Robertsce082592010-05-13 15:57:33 +0100468 "a single NAND device.\n");
469 BUG();
470 }
471 }
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800472 dev_dbg(&denali->dev->dev,
Jason Robertsce082592010-05-13 15:57:33 +0100473 "denali->total_used_banks: %d\n", denali->total_used_banks);
474}
475
476static void detect_partition_feature(struct denali_nand_info *denali)
477{
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800478 /* For MRST platform, denali->fwblks represent the
479 * number of blocks firmware is taken,
480 * FW is in protect partition and MTD driver has no
481 * permission to access it. So let driver know how many
482 * blocks it can't touch.
483 * */
Jason Robertsce082592010-05-13 15:57:33 +0100484 if (ioread32(denali->flash_reg + FEATURES) & FEATURES__PARTITION) {
485 if ((ioread32(denali->flash_reg + PERM_SRC_ID_1) &
486 PERM_SRC_ID_1__SRCID) == SPECTRA_PARTITION_ID) {
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800487 denali->fwblks =
Jason Robertsce082592010-05-13 15:57:33 +0100488 ((ioread32(denali->flash_reg + MIN_MAX_BANK_1) &
489 MIN_MAX_BANK_1__MIN_VALUE) *
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800490 denali->blksperchip)
Jason Robertsce082592010-05-13 15:57:33 +0100491 +
492 (ioread32(denali->flash_reg + MIN_BLK_ADDR_1) &
493 MIN_BLK_ADDR_1__VALUE);
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800494 } else
495 denali->fwblks = SPECTRA_START_BLOCK;
496 } else
497 denali->fwblks = SPECTRA_START_BLOCK;
Jason Robertsce082592010-05-13 15:57:33 +0100498}
499
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800500static uint16_t denali_nand_timing_set(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +0100501{
502 uint16_t status = PASS;
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800503 uint32_t id_bytes[5], addr;
504 uint8_t i, maf_id, device_id;
Jason Robertsce082592010-05-13 15:57:33 +0100505
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800506 dev_dbg(&denali->dev->dev,
507 "%s, Line %d, Function: %s\n",
508 __FILE__, __LINE__, __func__);
Jason Robertsce082592010-05-13 15:57:33 +0100509
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800510 /* Use read id method to get device ID and other
511 * params. For some NAND chips, controller can't
512 * report the correct device ID by reading from
513 * DEVICE_ID register
514 * */
515 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
516 index_addr(denali, (uint32_t)addr | 0, 0x90);
517 index_addr(denali, (uint32_t)addr | 1, 0);
518 for (i = 0; i < 5; i++)
519 index_addr_read_data(denali, addr | 2, &id_bytes[i]);
520 maf_id = id_bytes[0];
521 device_id = id_bytes[1];
Jason Robertsce082592010-05-13 15:57:33 +0100522
523 if (ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) &
524 ONFI_DEVICE_NO_OF_LUNS__ONFI_DEVICE) { /* ONFI 1.0 NAND */
525 if (FAIL == get_onfi_nand_para(denali))
526 return FAIL;
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800527 } else if (maf_id == 0xEC) { /* Samsung NAND */
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800528 get_samsung_nand_para(denali, device_id);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800529 } else if (maf_id == 0x98) { /* Toshiba NAND */
Jason Robertsce082592010-05-13 15:57:33 +0100530 get_toshiba_nand_para(denali);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800531 } else if (maf_id == 0xAD) { /* Hynix NAND */
532 get_hynix_nand_para(denali, device_id);
Jason Robertsce082592010-05-13 15:57:33 +0100533 }
534
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800535 dev_info(&denali->dev->dev,
536 "Dump timing register values:"
537 "acc_clks: %d, re_2_we: %d, re_2_re: %d\n"
538 "we_2_re: %d, addr_2_data: %d, rdwr_en_lo_cnt: %d\n"
Jason Robertsce082592010-05-13 15:57:33 +0100539 "rdwr_en_hi_cnt: %d, cs_setup_cnt: %d\n",
540 ioread32(denali->flash_reg + ACC_CLKS),
541 ioread32(denali->flash_reg + RE_2_WE),
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800542 ioread32(denali->flash_reg + RE_2_RE),
Jason Robertsce082592010-05-13 15:57:33 +0100543 ioread32(denali->flash_reg + WE_2_RE),
544 ioread32(denali->flash_reg + ADDR_2_DATA),
545 ioread32(denali->flash_reg + RDWR_EN_LO_CNT),
546 ioread32(denali->flash_reg + RDWR_EN_HI_CNT),
547 ioread32(denali->flash_reg + CS_SETUP_CNT));
548
Jason Robertsce082592010-05-13 15:57:33 +0100549 find_valid_banks(denali);
550
551 detect_partition_feature(denali);
552
Jason Robertsce082592010-05-13 15:57:33 +0100553 /* If the user specified to override the default timings
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800554 * with a specific ONFI mode, we apply those changes here.
Jason Robertsce082592010-05-13 15:57:33 +0100555 */
556 if (onfi_timing_mode != NAND_DEFAULT_TIMINGS)
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800557 nand_onfi_timing_set(denali, onfi_timing_mode);
Jason Robertsce082592010-05-13 15:57:33 +0100558
559 return status;
560}
561
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800562static void denali_set_intr_modes(struct denali_nand_info *denali,
Jason Robertsce082592010-05-13 15:57:33 +0100563 uint16_t INT_ENABLE)
564{
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800565 dev_dbg(&denali->dev->dev, "%s, Line %d, Function: %s\n",
Jason Robertsce082592010-05-13 15:57:33 +0100566 __FILE__, __LINE__, __func__);
567
568 if (INT_ENABLE)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800569 iowrite32(1, denali->flash_reg + GLOBAL_INT_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +0100570 else
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800571 iowrite32(0, denali->flash_reg + GLOBAL_INT_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +0100572}
573
574/* validation function to verify that the controlling software is making
575 a valid request
576 */
577static inline bool is_flash_bank_valid(int flash_bank)
578{
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800579 return (flash_bank >= 0 && flash_bank < 4);
Jason Robertsce082592010-05-13 15:57:33 +0100580}
581
582static void denali_irq_init(struct denali_nand_info *denali)
583{
584 uint32_t int_mask = 0;
585
586 /* Disable global interrupts */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800587 denali_set_intr_modes(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +0100588
589 int_mask = DENALI_IRQ_ALL;
590
591 /* Clear all status bits */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800592 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS0);
593 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS1);
594 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS2);
595 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS3);
Jason Robertsce082592010-05-13 15:57:33 +0100596
597 denali_irq_enable(denali, int_mask);
598}
599
600static void denali_irq_cleanup(int irqnum, struct denali_nand_info *denali)
601{
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800602 denali_set_intr_modes(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +0100603 free_irq(irqnum, denali);
604}
605
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800606static void denali_irq_enable(struct denali_nand_info *denali,
607 uint32_t int_mask)
Jason Robertsce082592010-05-13 15:57:33 +0100608{
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800609 iowrite32(int_mask, denali->flash_reg + INTR_EN0);
610 iowrite32(int_mask, denali->flash_reg + INTR_EN1);
611 iowrite32(int_mask, denali->flash_reg + INTR_EN2);
612 iowrite32(int_mask, denali->flash_reg + INTR_EN3);
Jason Robertsce082592010-05-13 15:57:33 +0100613}
614
615/* This function only returns when an interrupt that this driver cares about
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800616 * occurs. This is to reduce the overhead of servicing interrupts
Jason Robertsce082592010-05-13 15:57:33 +0100617 */
618static inline uint32_t denali_irq_detected(struct denali_nand_info *denali)
619{
Chuanxiao Donga99d1792010-07-27 11:32:21 +0800620 return read_interrupt_status(denali) & DENALI_IRQ_ALL;
Jason Robertsce082592010-05-13 15:57:33 +0100621}
622
623/* Interrupts are cleared by writing a 1 to the appropriate status bit */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800624static inline void clear_interrupt(struct denali_nand_info *denali,
625 uint32_t irq_mask)
Jason Robertsce082592010-05-13 15:57:33 +0100626{
627 uint32_t intr_status_reg = 0;
628
629 intr_status_reg = intr_status_addresses[denali->flash_bank];
630
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800631 iowrite32(irq_mask, denali->flash_reg + intr_status_reg);
Jason Robertsce082592010-05-13 15:57:33 +0100632}
633
634static void clear_interrupts(struct denali_nand_info *denali)
635{
636 uint32_t status = 0x0;
637 spin_lock_irq(&denali->irq_lock);
638
639 status = read_interrupt_status(denali);
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800640 clear_interrupt(denali, status);
Jason Robertsce082592010-05-13 15:57:33 +0100641
Jason Robertsce082592010-05-13 15:57:33 +0100642 denali->irq_status = 0x0;
643 spin_unlock_irq(&denali->irq_lock);
644}
645
646static uint32_t read_interrupt_status(struct denali_nand_info *denali)
647{
648 uint32_t intr_status_reg = 0;
649
650 intr_status_reg = intr_status_addresses[denali->flash_bank];
651
652 return ioread32(denali->flash_reg + intr_status_reg);
653}
654
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800655/* This is the interrupt service routine. It handles all interrupts
656 * sent to this device. Note that on CE4100, this is a shared
657 * interrupt.
Jason Robertsce082592010-05-13 15:57:33 +0100658 */
659static irqreturn_t denali_isr(int irq, void *dev_id)
660{
661 struct denali_nand_info *denali = dev_id;
662 uint32_t irq_status = 0x0;
663 irqreturn_t result = IRQ_NONE;
664
665 spin_lock(&denali->irq_lock);
666
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800667 /* check to see if a valid NAND chip has
668 * been selected.
Jason Robertsce082592010-05-13 15:57:33 +0100669 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800670 if (is_flash_bank_valid(denali->flash_bank)) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800671 /* check to see if controller generated
Jason Robertsce082592010-05-13 15:57:33 +0100672 * the interrupt, since this is a shared interrupt */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800673 irq_status = denali_irq_detected(denali);
674 if (irq_status != 0) {
Jason Robertsce082592010-05-13 15:57:33 +0100675 /* handle interrupt */
676 /* first acknowledge it */
677 clear_interrupt(denali, irq_status);
678 /* store the status in the device context for someone
679 to read */
680 denali->irq_status |= irq_status;
681 /* notify anyone who cares that it happened */
682 complete(&denali->complete);
683 /* tell the OS that we've handled this */
684 result = IRQ_HANDLED;
685 }
686 }
687 spin_unlock(&denali->irq_lock);
688 return result;
689}
690#define BANK(x) ((x) << 24)
691
692static uint32_t wait_for_irq(struct denali_nand_info *denali, uint32_t irq_mask)
693{
694 unsigned long comp_res = 0;
695 uint32_t intr_status = 0;
696 bool retry = false;
697 unsigned long timeout = msecs_to_jiffies(1000);
698
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800699 do {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800700 comp_res =
701 wait_for_completion_timeout(&denali->complete, timeout);
Jason Robertsce082592010-05-13 15:57:33 +0100702 spin_lock_irq(&denali->irq_lock);
703 intr_status = denali->irq_status;
704
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800705 if (intr_status & irq_mask) {
Jason Robertsce082592010-05-13 15:57:33 +0100706 denali->irq_status &= ~irq_mask;
707 spin_unlock_irq(&denali->irq_lock);
Jason Robertsce082592010-05-13 15:57:33 +0100708 /* our interrupt was detected */
709 break;
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800710 } else {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800711 /* these are not the interrupts you are looking for -
712 * need to wait again */
Jason Robertsce082592010-05-13 15:57:33 +0100713 spin_unlock_irq(&denali->irq_lock);
Jason Robertsce082592010-05-13 15:57:33 +0100714 retry = true;
715 }
716 } while (comp_res != 0);
717
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800718 if (comp_res == 0) {
Jason Robertsce082592010-05-13 15:57:33 +0100719 /* timeout */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800720 printk(KERN_ERR "timeout occurred, status = 0x%x, mask = 0x%x\n",
721 intr_status, irq_mask);
Jason Robertsce082592010-05-13 15:57:33 +0100722
723 intr_status = 0;
724 }
725 return intr_status;
726}
727
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800728/* This helper function setups the registers for ECC and whether or not
Jason Robertsce082592010-05-13 15:57:33 +0100729 the spare area will be transfered. */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800730static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en,
Jason Robertsce082592010-05-13 15:57:33 +0100731 bool transfer_spare)
732{
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800733 int ecc_en_flag = 0, transfer_spare_flag = 0;
Jason Robertsce082592010-05-13 15:57:33 +0100734
735 /* set ECC, transfer spare bits if needed */
736 ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0;
737 transfer_spare_flag = transfer_spare ? TRANSFER_SPARE_REG__FLAG : 0;
738
739 /* Enable spare area/ECC per user's request. */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800740 iowrite32(ecc_en_flag, denali->flash_reg + ECC_ENABLE);
741 iowrite32(transfer_spare_flag,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800742 denali->flash_reg + TRANSFER_SPARE_REG);
Jason Robertsce082592010-05-13 15:57:33 +0100743}
744
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800745/* sends a pipeline command operation to the controller. See the Denali NAND
746 controller's user guide for more information (section 4.2.3.6).
Jason Robertsce082592010-05-13 15:57:33 +0100747 */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800748static int denali_send_pipeline_cmd(struct denali_nand_info *denali,
749 bool ecc_en,
750 bool transfer_spare,
751 int access_type,
752 int op)
Jason Robertsce082592010-05-13 15:57:33 +0100753{
754 int status = PASS;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800755 uint32_t addr = 0x0, cmd = 0x0, page_count = 1, irq_status = 0,
Jason Robertsce082592010-05-13 15:57:33 +0100756 irq_mask = 0;
757
Chuanxiao Donga99d1792010-07-27 11:32:21 +0800758 if (op == DENALI_READ)
759 irq_mask = INTR_STATUS0__LOAD_COMP;
760 else if (op == DENALI_WRITE)
761 irq_mask = 0;
762 else
763 BUG();
Jason Robertsce082592010-05-13 15:57:33 +0100764
765 setup_ecc_for_xfer(denali, ecc_en, transfer_spare);
766
Jason Robertsce082592010-05-13 15:57:33 +0100767 /* clear interrupts */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800768 clear_interrupts(denali);
Jason Robertsce082592010-05-13 15:57:33 +0100769
770 addr = BANK(denali->flash_bank) | denali->page;
771
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800772 if (op == DENALI_WRITE && access_type != SPARE_ACCESS) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800773 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800774 iowrite32(cmd, denali->flash_mem);
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800775 } else if (op == DENALI_WRITE && access_type == SPARE_ACCESS) {
Jason Robertsce082592010-05-13 15:57:33 +0100776 /* read spare area */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800777 cmd = MODE_10 | addr;
Jason Robertsce082592010-05-13 15:57:33 +0100778 index_addr(denali, (uint32_t)cmd, access_type);
779
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800780 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800781 iowrite32(cmd, denali->flash_mem);
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800782 } else if (op == DENALI_READ) {
Jason Robertsce082592010-05-13 15:57:33 +0100783 /* setup page read request for access type */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800784 cmd = MODE_10 | addr;
Jason Robertsce082592010-05-13 15:57:33 +0100785 index_addr(denali, (uint32_t)cmd, access_type);
786
787 /* page 33 of the NAND controller spec indicates we should not
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800788 use the pipeline commands in Spare area only mode. So we
Jason Robertsce082592010-05-13 15:57:33 +0100789 don't.
790 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800791 if (access_type == SPARE_ACCESS) {
Jason Robertsce082592010-05-13 15:57:33 +0100792 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800793 iowrite32(cmd, denali->flash_mem);
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800794 } else {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800795 index_addr(denali, (uint32_t)cmd,
796 0x2000 | op | page_count);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800797
798 /* wait for command to be accepted
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800799 * can always use status0 bit as the
800 * mask is identical for each
Jason Robertsce082592010-05-13 15:57:33 +0100801 * bank. */
802 irq_status = wait_for_irq(denali, irq_mask);
803
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800804 if (irq_status == 0) {
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800805 dev_err(&denali->dev->dev,
806 "cmd, page, addr on timeout "
807 "(0x%x, 0x%x, 0x%x)\n",
808 cmd, denali->page, addr);
Jason Robertsce082592010-05-13 15:57:33 +0100809 status = FAIL;
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800810 } else {
Jason Robertsce082592010-05-13 15:57:33 +0100811 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800812 iowrite32(cmd, denali->flash_mem);
Jason Robertsce082592010-05-13 15:57:33 +0100813 }
814 }
815 }
816 return status;
817}
818
819/* helper function that simply writes a buffer to the flash */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800820static int write_data_to_flash_mem(struct denali_nand_info *denali,
821 const uint8_t *buf,
822 int len)
Jason Robertsce082592010-05-13 15:57:33 +0100823{
824 uint32_t i = 0, *buf32;
825
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800826 /* verify that the len is a multiple of 4. see comment in
827 * read_data_from_flash_mem() */
Jason Robertsce082592010-05-13 15:57:33 +0100828 BUG_ON((len % 4) != 0);
829
830 /* write the data to the flash memory */
831 buf32 = (uint32_t *)buf;
832 for (i = 0; i < len / 4; i++)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800833 iowrite32(*buf32++, denali->flash_mem + 0x10);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800834 return i*4; /* intent is to return the number of bytes read */
Jason Robertsce082592010-05-13 15:57:33 +0100835}
836
837/* helper function that simply reads a buffer from the flash */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800838static int read_data_from_flash_mem(struct denali_nand_info *denali,
839 uint8_t *buf,
840 int len)
Jason Robertsce082592010-05-13 15:57:33 +0100841{
842 uint32_t i = 0, *buf32;
843
844 /* we assume that len will be a multiple of 4, if not
845 * it would be nice to know about it ASAP rather than
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800846 * have random failures...
847 * This assumption is based on the fact that this
848 * function is designed to be used to read flash pages,
Jason Robertsce082592010-05-13 15:57:33 +0100849 * which are typically multiples of 4...
850 */
851
852 BUG_ON((len % 4) != 0);
853
854 /* transfer the data from the flash */
855 buf32 = (uint32_t *)buf;
856 for (i = 0; i < len / 4; i++)
Jason Robertsce082592010-05-13 15:57:33 +0100857 *buf32++ = ioread32(denali->flash_mem + 0x10);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800858 return i*4; /* intent is to return the number of bytes read */
Jason Robertsce082592010-05-13 15:57:33 +0100859}
860
861/* writes OOB data to the device */
862static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
863{
864 struct denali_nand_info *denali = mtd_to_denali(mtd);
865 uint32_t irq_status = 0;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800866 uint32_t irq_mask = INTR_STATUS0__PROGRAM_COMP |
Jason Robertsce082592010-05-13 15:57:33 +0100867 INTR_STATUS0__PROGRAM_FAIL;
868 int status = 0;
869
870 denali->page = page;
871
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800872 if (denali_send_pipeline_cmd(denali, false, false, SPARE_ACCESS,
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800873 DENALI_WRITE) == PASS) {
Jason Robertsce082592010-05-13 15:57:33 +0100874 write_data_to_flash_mem(denali, buf, mtd->oobsize);
875
Jason Robertsce082592010-05-13 15:57:33 +0100876 /* wait for operation to complete */
877 irq_status = wait_for_irq(denali, irq_mask);
878
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800879 if (irq_status == 0) {
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800880 dev_err(&denali->dev->dev, "OOB write failed\n");
Jason Robertsce082592010-05-13 15:57:33 +0100881 status = -EIO;
882 }
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800883 } else {
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800884 dev_err(&denali->dev->dev, "unable to send pipeline command\n");
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800885 status = -EIO;
Jason Robertsce082592010-05-13 15:57:33 +0100886 }
887 return status;
888}
889
890/* reads OOB data from the device */
891static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
892{
893 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800894 uint32_t irq_mask = INTR_STATUS0__LOAD_COMP,
895 irq_status = 0, addr = 0x0, cmd = 0x0;
Jason Robertsce082592010-05-13 15:57:33 +0100896
897 denali->page = page;
898
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800899 if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800900 DENALI_READ) == PASS) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800901 read_data_from_flash_mem(denali, buf, mtd->oobsize);
Jason Robertsce082592010-05-13 15:57:33 +0100902
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800903 /* wait for command to be accepted
Jason Robertsce082592010-05-13 15:57:33 +0100904 * can always use status0 bit as the mask is identical for each
905 * bank. */
906 irq_status = wait_for_irq(denali, irq_mask);
907
908 if (irq_status == 0)
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800909 dev_err(&denali->dev->dev, "page on OOB timeout %d\n",
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800910 denali->page);
Jason Robertsce082592010-05-13 15:57:33 +0100911
912 /* We set the device back to MAIN_ACCESS here as I observed
913 * instability with the controller if you do a block erase
914 * and the last transaction was a SPARE_ACCESS. Block erase
915 * is reliable (according to the MTD test infrastructure)
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800916 * if you are in MAIN_ACCESS.
Jason Robertsce082592010-05-13 15:57:33 +0100917 */
918 addr = BANK(denali->flash_bank) | denali->page;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800919 cmd = MODE_10 | addr;
Jason Robertsce082592010-05-13 15:57:33 +0100920 index_addr(denali, (uint32_t)cmd, MAIN_ACCESS);
Jason Robertsce082592010-05-13 15:57:33 +0100921 }
922}
923
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800924/* this function examines buffers to see if they contain data that
Jason Robertsce082592010-05-13 15:57:33 +0100925 * indicate that the buffer is part of an erased region of flash.
926 */
927bool is_erased(uint8_t *buf, int len)
928{
929 int i = 0;
930 for (i = 0; i < len; i++)
Jason Robertsce082592010-05-13 15:57:33 +0100931 if (buf[i] != 0xFF)
Jason Robertsce082592010-05-13 15:57:33 +0100932 return false;
Jason Robertsce082592010-05-13 15:57:33 +0100933 return true;
934}
935#define ECC_SECTOR_SIZE 512
936
937#define ECC_SECTOR(x) (((x) & ECC_ERROR_ADDRESS__SECTOR_NR) >> 12)
938#define ECC_BYTE(x) (((x) & ECC_ERROR_ADDRESS__OFFSET))
939#define ECC_CORRECTION_VALUE(x) ((x) & ERR_CORRECTION_INFO__BYTEMASK)
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800940#define ECC_ERROR_CORRECTABLE(x) (!((x) & ERR_CORRECTION_INFO__ERROR_TYPE))
941#define ECC_ERR_DEVICE(x) (((x) & ERR_CORRECTION_INFO__DEVICE_NR) >> 8)
Jason Robertsce082592010-05-13 15:57:33 +0100942#define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO)
943
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800944static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf,
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800945 uint32_t irq_status)
Jason Robertsce082592010-05-13 15:57:33 +0100946{
947 bool check_erased_page = false;
948
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800949 if (irq_status & INTR_STATUS0__ECC_ERR) {
Jason Robertsce082592010-05-13 15:57:33 +0100950 /* read the ECC errors. we'll ignore them for now */
951 uint32_t err_address = 0, err_correction_info = 0;
952 uint32_t err_byte = 0, err_sector = 0, err_device = 0;
953 uint32_t err_correction_value = 0;
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800954 denali_set_intr_modes(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +0100955
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800956 do {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800957 err_address = ioread32(denali->flash_reg +
Jason Robertsce082592010-05-13 15:57:33 +0100958 ECC_ERROR_ADDRESS);
959 err_sector = ECC_SECTOR(err_address);
960 err_byte = ECC_BYTE(err_address);
961
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800962 err_correction_info = ioread32(denali->flash_reg +
Jason Robertsce082592010-05-13 15:57:33 +0100963 ERR_CORRECTION_INFO);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800964 err_correction_value =
Jason Robertsce082592010-05-13 15:57:33 +0100965 ECC_CORRECTION_VALUE(err_correction_info);
966 err_device = ECC_ERR_DEVICE(err_correction_info);
967
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800968 if (ECC_ERROR_CORRECTABLE(err_correction_info)) {
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800969 /* If err_byte is larger than ECC_SECTOR_SIZE,
970 * means error happend in OOB, so we ignore
971 * it. It's no need for us to correct it
972 * err_device is represented the NAND error
973 * bits are happened in if there are more
974 * than one NAND connected.
975 * */
976 if (err_byte < ECC_SECTOR_SIZE) {
977 int offset;
978 offset = (err_sector *
979 ECC_SECTOR_SIZE +
980 err_byte) *
981 denali->devnum +
982 err_device;
Jason Robertsce082592010-05-13 15:57:33 +0100983 /* correct the ECC error */
984 buf[offset] ^= err_correction_value;
985 denali->mtd.ecc_stats.corrected++;
Jason Robertsce082592010-05-13 15:57:33 +0100986 }
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800987 } else {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800988 /* if the error is not correctable, need to
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800989 * look at the page to see if it is an erased
990 * page. if so, then it's not a real ECC error
991 * */
Jason Robertsce082592010-05-13 15:57:33 +0100992 check_erased_page = true;
993 }
Jason Robertsce082592010-05-13 15:57:33 +0100994 } while (!ECC_LAST_ERR(err_correction_info));
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800995 /* Once handle all ecc errors, controller will triger
996 * a ECC_TRANSACTION_DONE interrupt, so here just wait
997 * for a while for this interrupt
998 * */
999 while (!(read_interrupt_status(denali) &
1000 INTR_STATUS0__ECC_TRANSACTION_DONE))
1001 cpu_relax();
1002 clear_interrupts(denali);
1003 denali_set_intr_modes(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001004 }
1005 return check_erased_page;
1006}
1007
1008/* programs the controller to either enable/disable DMA transfers */
David Woodhouseaadff492010-05-13 16:12:43 +01001009static void denali_enable_dma(struct denali_nand_info *denali, bool en)
Jason Robertsce082592010-05-13 15:57:33 +01001010{
1011 uint32_t reg_val = 0x0;
1012
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001013 if (en)
1014 reg_val = DMA_ENABLE__FLAG;
Jason Robertsce082592010-05-13 15:57:33 +01001015
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001016 iowrite32(reg_val, denali->flash_reg + DMA_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +01001017 ioread32(denali->flash_reg + DMA_ENABLE);
1018}
1019
1020/* setups the HW to perform the data DMA */
David Woodhouseaadff492010-05-13 16:12:43 +01001021static void denali_setup_dma(struct denali_nand_info *denali, int op)
Jason Robertsce082592010-05-13 15:57:33 +01001022{
1023 uint32_t mode = 0x0;
1024 const int page_count = 1;
1025 dma_addr_t addr = denali->buf.dma_buf;
1026
1027 mode = MODE_10 | BANK(denali->flash_bank);
1028
1029 /* DMA is a four step process */
1030
1031 /* 1. setup transfer type and # of pages */
1032 index_addr(denali, mode | denali->page, 0x2000 | op | page_count);
1033
1034 /* 2. set memory high address bits 23:8 */
1035 index_addr(denali, mode | ((uint16_t)(addr >> 16) << 8), 0x2200);
1036
1037 /* 3. set memory low address bits 23:8 */
1038 index_addr(denali, mode | ((uint16_t)addr << 8), 0x2300);
1039
1040 /* 4. interrupt when complete, burst len = 64 bytes*/
1041 index_addr(denali, mode | 0x14000, 0x2400);
1042}
1043
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001044/* writes a page. user specifies type, and this function handles the
Jason Robertsce082592010-05-13 15:57:33 +01001045 configuration details. */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001046static void write_page(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001047 const uint8_t *buf, bool raw_xfer)
1048{
1049 struct denali_nand_info *denali = mtd_to_denali(mtd);
1050 struct pci_dev *pci_dev = denali->dev;
1051
1052 dma_addr_t addr = denali->buf.dma_buf;
1053 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1054
1055 uint32_t irq_status = 0;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001056 uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP |
Jason Robertsce082592010-05-13 15:57:33 +01001057 INTR_STATUS0__PROGRAM_FAIL;
1058
1059 /* if it is a raw xfer, we want to disable ecc, and send
1060 * the spare area.
1061 * !raw_xfer - enable ecc
1062 * raw_xfer - transfer spare
1063 */
1064 setup_ecc_for_xfer(denali, !raw_xfer, raw_xfer);
1065
1066 /* copy buffer into DMA buffer */
1067 memcpy(denali->buf.buf, buf, mtd->writesize);
1068
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001069 if (raw_xfer) {
Jason Robertsce082592010-05-13 15:57:33 +01001070 /* transfer the data to the spare area */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001071 memcpy(denali->buf.buf + mtd->writesize,
1072 chip->oob_poi,
1073 mtd->oobsize);
Jason Robertsce082592010-05-13 15:57:33 +01001074 }
1075
1076 pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_TODEVICE);
1077
1078 clear_interrupts(denali);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001079 denali_enable_dma(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001080
David Woodhouseaadff492010-05-13 16:12:43 +01001081 denali_setup_dma(denali, DENALI_WRITE);
Jason Robertsce082592010-05-13 15:57:33 +01001082
1083 /* wait for operation to complete */
1084 irq_status = wait_for_irq(denali, irq_mask);
1085
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001086 if (irq_status == 0) {
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001087 dev_err(&denali->dev->dev,
1088 "timeout on write_page (type = %d)\n",
1089 raw_xfer);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001090 denali->status =
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001091 (irq_status & INTR_STATUS0__PROGRAM_FAIL) ?
1092 NAND_STATUS_FAIL : PASS;
Jason Robertsce082592010-05-13 15:57:33 +01001093 }
1094
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001095 denali_enable_dma(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +01001096 pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_TODEVICE);
1097}
1098
1099/* NAND core entry points */
1100
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001101/* this is the callback that the NAND core calls to write a page. Since
1102 writing a page with ECC or without is similar, all the work is done
Jason Robertsce082592010-05-13 15:57:33 +01001103 by write_page above. */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001104static void denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001105 const uint8_t *buf)
1106{
1107 /* for regular page writes, we let HW handle all the ECC
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001108 * data written to the device. */
Jason Robertsce082592010-05-13 15:57:33 +01001109 write_page(mtd, chip, buf, false);
1110}
1111
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001112/* This is the callback that the NAND core calls to write a page without ECC.
Jason Robertsce082592010-05-13 15:57:33 +01001113 raw access is similiar to ECC page writes, so all the work is done in the
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001114 write_page() function above.
Jason Robertsce082592010-05-13 15:57:33 +01001115 */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001116static void denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001117 const uint8_t *buf)
1118{
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001119 /* for raw page writes, we want to disable ECC and simply write
Jason Robertsce082592010-05-13 15:57:33 +01001120 whatever data is in the buffer. */
1121 write_page(mtd, chip, buf, true);
1122}
1123
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001124static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001125 int page)
1126{
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001127 return write_oob_data(mtd, chip->oob_poi, page);
Jason Robertsce082592010-05-13 15:57:33 +01001128}
1129
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001130static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001131 int page, int sndcmd)
1132{
1133 read_oob_data(mtd, chip->oob_poi, page);
1134
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001135 return 0; /* notify NAND core to send command to
1136 NAND device. */
Jason Robertsce082592010-05-13 15:57:33 +01001137}
1138
1139static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1140 uint8_t *buf, int page)
1141{
1142 struct denali_nand_info *denali = mtd_to_denali(mtd);
1143 struct pci_dev *pci_dev = denali->dev;
1144
1145 dma_addr_t addr = denali->buf.dma_buf;
1146 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1147
1148 uint32_t irq_status = 0;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001149 uint32_t irq_mask = INTR_STATUS0__ECC_TRANSACTION_DONE |
Jason Robertsce082592010-05-13 15:57:33 +01001150 INTR_STATUS0__ECC_ERR;
1151 bool check_erased_page = false;
1152
1153 setup_ecc_for_xfer(denali, true, false);
1154
David Woodhouseaadff492010-05-13 16:12:43 +01001155 denali_enable_dma(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001156 pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
1157
1158 clear_interrupts(denali);
David Woodhouseaadff492010-05-13 16:12:43 +01001159 denali_setup_dma(denali, DENALI_READ);
Jason Robertsce082592010-05-13 15:57:33 +01001160
1161 /* wait for operation to complete */
1162 irq_status = wait_for_irq(denali, irq_mask);
1163
1164 pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
1165
1166 memcpy(buf, denali->buf.buf, mtd->writesize);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001167
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +08001168 check_erased_page = handle_ecc(denali, buf, irq_status);
David Woodhouseaadff492010-05-13 16:12:43 +01001169 denali_enable_dma(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +01001170
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001171 if (check_erased_page) {
Jason Robertsce082592010-05-13 15:57:33 +01001172 read_oob_data(&denali->mtd, chip->oob_poi, denali->page);
1173
1174 /* check ECC failures that may have occurred on erased pages */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001175 if (check_erased_page) {
Jason Robertsce082592010-05-13 15:57:33 +01001176 if (!is_erased(buf, denali->mtd.writesize))
Jason Robertsce082592010-05-13 15:57:33 +01001177 denali->mtd.ecc_stats.failed++;
Jason Robertsce082592010-05-13 15:57:33 +01001178 if (!is_erased(buf, denali->mtd.oobsize))
Jason Robertsce082592010-05-13 15:57:33 +01001179 denali->mtd.ecc_stats.failed++;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001180 }
Jason Robertsce082592010-05-13 15:57:33 +01001181 }
1182 return 0;
1183}
1184
1185static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1186 uint8_t *buf, int page)
1187{
1188 struct denali_nand_info *denali = mtd_to_denali(mtd);
1189 struct pci_dev *pci_dev = denali->dev;
1190
1191 dma_addr_t addr = denali->buf.dma_buf;
1192 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1193
1194 uint32_t irq_status = 0;
1195 uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001196
Jason Robertsce082592010-05-13 15:57:33 +01001197 setup_ecc_for_xfer(denali, false, true);
David Woodhouseaadff492010-05-13 16:12:43 +01001198 denali_enable_dma(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001199
1200 pci_dma_sync_single_for_device(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
1201
1202 clear_interrupts(denali);
David Woodhouseaadff492010-05-13 16:12:43 +01001203 denali_setup_dma(denali, DENALI_READ);
Jason Robertsce082592010-05-13 15:57:33 +01001204
1205 /* wait for operation to complete */
1206 irq_status = wait_for_irq(denali, irq_mask);
1207
1208 pci_dma_sync_single_for_cpu(pci_dev, addr, size, PCI_DMA_FROMDEVICE);
1209
David Woodhouseaadff492010-05-13 16:12:43 +01001210 denali_enable_dma(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +01001211
1212 memcpy(buf, denali->buf.buf, mtd->writesize);
1213 memcpy(chip->oob_poi, denali->buf.buf + mtd->writesize, mtd->oobsize);
1214
1215 return 0;
1216}
1217
1218static uint8_t denali_read_byte(struct mtd_info *mtd)
1219{
1220 struct denali_nand_info *denali = mtd_to_denali(mtd);
1221 uint8_t result = 0xff;
1222
1223 if (denali->buf.head < denali->buf.tail)
Jason Robertsce082592010-05-13 15:57:33 +01001224 result = denali->buf.buf[denali->buf.head++];
Jason Robertsce082592010-05-13 15:57:33 +01001225
Jason Robertsce082592010-05-13 15:57:33 +01001226 return result;
1227}
1228
1229static void denali_select_chip(struct mtd_info *mtd, int chip)
1230{
1231 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001232
Jason Robertsce082592010-05-13 15:57:33 +01001233 spin_lock_irq(&denali->irq_lock);
1234 denali->flash_bank = chip;
1235 spin_unlock_irq(&denali->irq_lock);
1236}
1237
1238static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
1239{
1240 struct denali_nand_info *denali = mtd_to_denali(mtd);
1241 int status = denali->status;
1242 denali->status = 0;
1243
Jason Robertsce082592010-05-13 15:57:33 +01001244 return status;
1245}
1246
1247static void denali_erase(struct mtd_info *mtd, int page)
1248{
1249 struct denali_nand_info *denali = mtd_to_denali(mtd);
1250
1251 uint32_t cmd = 0x0, irq_status = 0;
1252
Jason Robertsce082592010-05-13 15:57:33 +01001253 /* clear interrupts */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001254 clear_interrupts(denali);
Jason Robertsce082592010-05-13 15:57:33 +01001255
1256 /* setup page read request for access type */
1257 cmd = MODE_10 | BANK(denali->flash_bank) | page;
1258 index_addr(denali, (uint32_t)cmd, 0x1);
1259
1260 /* wait for erase to complete or failure to occur */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001261 irq_status = wait_for_irq(denali, INTR_STATUS0__ERASE_COMP |
Jason Robertsce082592010-05-13 15:57:33 +01001262 INTR_STATUS0__ERASE_FAIL);
1263
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001264 denali->status = (irq_status & INTR_STATUS0__ERASE_FAIL) ?
1265 NAND_STATUS_FAIL : PASS;
Jason Robertsce082592010-05-13 15:57:33 +01001266}
1267
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001268static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col,
Jason Robertsce082592010-05-13 15:57:33 +01001269 int page)
1270{
1271 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +08001272 uint32_t addr, id;
1273 int i;
Jason Robertsce082592010-05-13 15:57:33 +01001274
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001275 switch (cmd) {
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001276 case NAND_CMD_PAGEPROG:
1277 break;
1278 case NAND_CMD_STATUS:
1279 read_status(denali);
1280 break;
1281 case NAND_CMD_READID:
1282 reset_buf(denali);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +08001283 /*sometimes ManufactureId read from register is not right
1284 * e.g. some of Micron MT29F32G08QAA MLC NAND chips
1285 * So here we send READID cmd to NAND insteand
1286 * */
1287 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
1288 index_addr(denali, (uint32_t)addr | 0, 0x90);
1289 index_addr(denali, (uint32_t)addr | 1, 0);
1290 for (i = 0; i < 5; i++) {
1291 index_addr_read_data(denali,
1292 (uint32_t)addr | 2,
1293 &id);
1294 write_byte_to_buf(denali, id);
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001295 }
1296 break;
1297 case NAND_CMD_READ0:
1298 case NAND_CMD_SEQIN:
1299 denali->page = page;
1300 break;
1301 case NAND_CMD_RESET:
1302 reset_bank(denali);
1303 break;
1304 case NAND_CMD_READOOB:
1305 /* TODO: Read OOB data */
1306 break;
1307 default:
1308 printk(KERN_ERR ": unsupported command"
1309 " received 0x%x\n", cmd);
1310 break;
Jason Robertsce082592010-05-13 15:57:33 +01001311 }
1312}
1313
1314/* stubs for ECC functions not used by the NAND core */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001315static int denali_ecc_calculate(struct mtd_info *mtd, const uint8_t *data,
Jason Robertsce082592010-05-13 15:57:33 +01001316 uint8_t *ecc_code)
1317{
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001318 struct denali_nand_info *denali = mtd_to_denali(mtd);
1319 dev_err(&denali->dev->dev,
1320 "denali_ecc_calculate called unexpectedly\n");
Jason Robertsce082592010-05-13 15:57:33 +01001321 BUG();
1322 return -EIO;
1323}
1324
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001325static int denali_ecc_correct(struct mtd_info *mtd, uint8_t *data,
Jason Robertsce082592010-05-13 15:57:33 +01001326 uint8_t *read_ecc, uint8_t *calc_ecc)
1327{
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001328 struct denali_nand_info *denali = mtd_to_denali(mtd);
1329 dev_err(&denali->dev->dev,
1330 "denali_ecc_correct called unexpectedly\n");
Jason Robertsce082592010-05-13 15:57:33 +01001331 BUG();
1332 return -EIO;
1333}
1334
1335static void denali_ecc_hwctl(struct mtd_info *mtd, int mode)
1336{
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001337 struct denali_nand_info *denali = mtd_to_denali(mtd);
1338 dev_err(&denali->dev->dev,
1339 "denali_ecc_hwctl called unexpectedly\n");
Jason Robertsce082592010-05-13 15:57:33 +01001340 BUG();
1341}
1342/* end NAND core entry points */
1343
1344/* Initialization code to bring the device up to a known good state */
1345static void denali_hw_init(struct denali_nand_info *denali)
1346{
Chuanxiao Dongdb9a32102010-08-06 18:02:03 +08001347 /* tell driver how many bit controller will skip before
1348 * writing ECC code in OOB, this register may be already
1349 * set by firmware. So we read this value out.
1350 * if this value is 0, just let it be.
1351 * */
1352 denali->bbtskipbytes = ioread32(denali->flash_reg +
1353 SPARE_AREA_SKIP_BYTES);
Jason Robertsce082592010-05-13 15:57:33 +01001354 denali_irq_init(denali);
Chuanxiao Dongeda936e2010-07-27 14:17:37 +08001355 denali_nand_reset(denali);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001356 iowrite32(0x0F, denali->flash_reg + RB_PIN_ENABLED);
1357 iowrite32(CHIP_EN_DONT_CARE__FLAG,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001358 denali->flash_reg + CHIP_ENABLE_DONT_CARE);
Jason Robertsce082592010-05-13 15:57:33 +01001359
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001360 iowrite32(0x0, denali->flash_reg + SPARE_AREA_SKIP_BYTES);
1361 iowrite32(0xffff, denali->flash_reg + SPARE_AREA_MARKER);
Jason Robertsce082592010-05-13 15:57:33 +01001362
1363 /* Should set value for these registers when init */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001364 iowrite32(0, denali->flash_reg + TWO_ROW_ADDR_CYCLES);
1365 iowrite32(1, denali->flash_reg + ECC_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +01001366}
1367
Chuanxiao Dongdb9a32102010-08-06 18:02:03 +08001368/* Althogh controller spec said SLC ECC is forceb to be 4bit,
1369 * but denali controller in MRST only support 15bit and 8bit ECC
1370 * correction
1371 * */
1372#define ECC_8BITS 14
1373static struct nand_ecclayout nand_8bit_oob = {
1374 .eccbytes = 14,
Jason Robertsce082592010-05-13 15:57:33 +01001375};
1376
Chuanxiao Dongdb9a32102010-08-06 18:02:03 +08001377#define ECC_15BITS 26
1378static struct nand_ecclayout nand_15bit_oob = {
1379 .eccbytes = 26,
Jason Robertsce082592010-05-13 15:57:33 +01001380};
1381
1382static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1383static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1384
1385static struct nand_bbt_descr bbt_main_descr = {
1386 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1387 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1388 .offs = 8,
1389 .len = 4,
1390 .veroffs = 12,
1391 .maxblocks = 4,
1392 .pattern = bbt_pattern,
1393};
1394
1395static struct nand_bbt_descr bbt_mirror_descr = {
1396 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1397 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1398 .offs = 8,
1399 .len = 4,
1400 .veroffs = 12,
1401 .maxblocks = 4,
1402 .pattern = mirror_pattern,
1403};
1404
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001405/* initialize driver data structures */
Jason Robertsce082592010-05-13 15:57:33 +01001406void denali_drv_init(struct denali_nand_info *denali)
1407{
1408 denali->idx = 0;
1409
1410 /* setup interrupt handler */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001411 /* the completion object will be used to notify
Jason Robertsce082592010-05-13 15:57:33 +01001412 * the callee that the interrupt is done */
1413 init_completion(&denali->complete);
1414
1415 /* the spinlock will be used to synchronize the ISR
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001416 * with any element that might be access shared
Jason Robertsce082592010-05-13 15:57:33 +01001417 * data (interrupt status) */
1418 spin_lock_init(&denali->irq_lock);
1419
1420 /* indicate that MTD has not selected a valid bank yet */
1421 denali->flash_bank = CHIP_SELECT_INVALID;
1422
1423 /* initialize our irq_status variable to indicate no interrupts */
1424 denali->irq_status = 0;
1425}
1426
1427/* driver entry point */
1428static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
1429{
1430 int ret = -ENODEV;
1431 resource_size_t csr_base, mem_base;
1432 unsigned long csr_len, mem_len;
1433 struct denali_nand_info *denali;
1434
Jason Robertsce082592010-05-13 15:57:33 +01001435 denali = kzalloc(sizeof(*denali), GFP_KERNEL);
1436 if (!denali)
1437 return -ENOMEM;
1438
1439 ret = pci_enable_device(dev);
1440 if (ret) {
1441 printk(KERN_ERR "Spectra: pci_enable_device failed.\n");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001442 goto failed_alloc_memery;
Jason Robertsce082592010-05-13 15:57:33 +01001443 }
1444
1445 if (id->driver_data == INTEL_CE4100) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001446 /* Due to a silicon limitation, we can only support
1447 * ONFI timing mode 1 and below.
1448 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001449 if (onfi_timing_mode < -1 || onfi_timing_mode > 1) {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001450 printk(KERN_ERR "Intel CE4100 only supports"
1451 " ONFI timing mode 1 or below\n");
Jason Robertsce082592010-05-13 15:57:33 +01001452 ret = -EINVAL;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001453 goto failed_enable_dev;
Jason Robertsce082592010-05-13 15:57:33 +01001454 }
1455 denali->platform = INTEL_CE4100;
1456 mem_base = pci_resource_start(dev, 0);
1457 mem_len = pci_resource_len(dev, 1);
1458 csr_base = pci_resource_start(dev, 1);
1459 csr_len = pci_resource_len(dev, 1);
1460 } else {
1461 denali->platform = INTEL_MRST;
1462 csr_base = pci_resource_start(dev, 0);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001463 csr_len = pci_resource_len(dev, 0);
Jason Robertsce082592010-05-13 15:57:33 +01001464 mem_base = pci_resource_start(dev, 1);
1465 mem_len = pci_resource_len(dev, 1);
1466 if (!mem_len) {
1467 mem_base = csr_base + csr_len;
1468 mem_len = csr_len;
Jason Robertsce082592010-05-13 15:57:33 +01001469 }
1470 }
1471
1472 /* Is 32-bit DMA supported? */
1473 ret = pci_set_dma_mask(dev, DMA_BIT_MASK(32));
1474
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001475 if (ret) {
Jason Robertsce082592010-05-13 15:57:33 +01001476 printk(KERN_ERR "Spectra: no usable DMA configuration\n");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001477 goto failed_enable_dev;
Jason Robertsce082592010-05-13 15:57:33 +01001478 }
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001479 denali->buf.dma_buf =
1480 pci_map_single(dev, denali->buf.buf,
1481 DENALI_BUF_SIZE,
1482 PCI_DMA_BIDIRECTIONAL);
Jason Robertsce082592010-05-13 15:57:33 +01001483
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001484 if (pci_dma_mapping_error(dev, denali->buf.dma_buf)) {
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001485 dev_err(&dev->dev, "Spectra: failed to map DMA buffer\n");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001486 goto failed_enable_dev;
Jason Robertsce082592010-05-13 15:57:33 +01001487 }
1488
1489 pci_set_master(dev);
1490 denali->dev = dev;
1491
1492 ret = pci_request_regions(dev, DENALI_NAND_NAME);
1493 if (ret) {
1494 printk(KERN_ERR "Spectra: Unable to request memory regions\n");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001495 goto failed_dma_map;
Jason Robertsce082592010-05-13 15:57:33 +01001496 }
1497
1498 denali->flash_reg = ioremap_nocache(csr_base, csr_len);
1499 if (!denali->flash_reg) {
1500 printk(KERN_ERR "Spectra: Unable to remap memory region\n");
1501 ret = -ENOMEM;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001502 goto failed_req_regions;
Jason Robertsce082592010-05-13 15:57:33 +01001503 }
Jason Robertsce082592010-05-13 15:57:33 +01001504
1505 denali->flash_mem = ioremap_nocache(mem_base, mem_len);
1506 if (!denali->flash_mem) {
1507 printk(KERN_ERR "Spectra: ioremap_nocache failed!");
Jason Robertsce082592010-05-13 15:57:33 +01001508 ret = -ENOMEM;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001509 goto failed_remap_reg;
Jason Robertsce082592010-05-13 15:57:33 +01001510 }
1511
Jason Robertsce082592010-05-13 15:57:33 +01001512 denali_hw_init(denali);
1513 denali_drv_init(denali);
1514
Jason Robertsce082592010-05-13 15:57:33 +01001515 if (request_irq(dev->irq, denali_isr, IRQF_SHARED,
1516 DENALI_NAND_NAME, denali)) {
1517 printk(KERN_ERR "Spectra: Unable to allocate IRQ\n");
1518 ret = -ENODEV;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001519 goto failed_remap_mem;
Jason Robertsce082592010-05-13 15:57:33 +01001520 }
1521
1522 /* now that our ISR is registered, we can enable interrupts */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +08001523 denali_set_intr_modes(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001524
1525 pci_set_drvdata(dev, denali);
1526
Chuanxiao Dongeda936e2010-07-27 14:17:37 +08001527 denali_nand_timing_set(denali);
Jason Robertsce082592010-05-13 15:57:33 +01001528
Jason Robertsce082592010-05-13 15:57:33 +01001529 denali->mtd.name = "Denali NAND";
1530 denali->mtd.owner = THIS_MODULE;
1531 denali->mtd.priv = &denali->nand;
1532
1533 /* register the driver with the NAND core subsystem */
1534 denali->nand.select_chip = denali_select_chip;
1535 denali->nand.cmdfunc = denali_cmdfunc;
1536 denali->nand.read_byte = denali_read_byte;
1537 denali->nand.waitfunc = denali_waitfunc;
1538
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001539 /* scan for NAND devices attached to the controller
Jason Robertsce082592010-05-13 15:57:33 +01001540 * this is the first stage in a two step process to register
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001541 * with the nand subsystem */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001542 if (nand_scan_ident(&denali->mtd, LLD_MAX_FLASH_BANKS, NULL)) {
Jason Robertsce082592010-05-13 15:57:33 +01001543 ret = -ENXIO;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001544 goto failed_req_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001545 }
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001546
Chuanxiao.Dong66406522010-08-06 18:48:21 +08001547 /* MTD supported page sizes vary by kernel. We validate our
1548 * kernel supports the device here.
1549 */
1550 if (denali->mtd.writesize > NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE) {
1551 ret = -ENODEV;
1552 printk(KERN_ERR "Spectra: device size not supported by this "
1553 "version of MTD.");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001554 goto failed_req_irq;
Chuanxiao.Dong66406522010-08-06 18:48:21 +08001555 }
1556
Chuanxiao Dong08b9ab92010-08-06 18:19:09 +08001557 /* support for multi nand
1558 * MTD known nothing about multi nand,
1559 * so we should tell it the real pagesize
1560 * and anything necessery
1561 */
1562 denali->devnum = ioread32(denali->flash_reg + DEVICES_CONNECTED);
1563 denali->nand.chipsize <<= (denali->devnum - 1);
1564 denali->nand.page_shift += (denali->devnum - 1);
1565 denali->nand.pagemask = (denali->nand.chipsize >>
1566 denali->nand.page_shift) - 1;
1567 denali->nand.bbt_erase_shift += (denali->devnum - 1);
1568 denali->nand.phys_erase_shift = denali->nand.bbt_erase_shift;
1569 denali->nand.chip_shift += (denali->devnum - 1);
1570 denali->mtd.writesize <<= (denali->devnum - 1);
1571 denali->mtd.oobsize <<= (denali->devnum - 1);
1572 denali->mtd.erasesize <<= (denali->devnum - 1);
1573 denali->mtd.size = denali->nand.numchips * denali->nand.chipsize;
1574 denali->bbtskipbytes *= denali->devnum;
1575
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001576 /* second stage of the NAND scan
1577 * this stage requires information regarding ECC and
1578 * bad block management. */
Jason Robertsce082592010-05-13 15:57:33 +01001579
1580 /* Bad block management */
1581 denali->nand.bbt_td = &bbt_main_descr;
1582 denali->nand.bbt_md = &bbt_mirror_descr;
1583
1584 /* skip the scan for now until we have OOB read and write support */
1585 denali->nand.options |= NAND_USE_FLASH_BBT | NAND_SKIP_BBTSCAN;
1586 denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
1587
Chuanxiao Dongdb9a32102010-08-06 18:02:03 +08001588 /* Denali Controller only support 15bit and 8bit ECC in MRST,
1589 * so just let controller do 15bit ECC for MLC and 8bit ECC for
1590 * SLC if possible.
1591 * */
1592 if (denali->nand.cellinfo & 0xc &&
1593 (denali->mtd.oobsize > (denali->bbtskipbytes +
1594 ECC_15BITS * (denali->mtd.writesize /
1595 ECC_SECTOR_SIZE)))) {
1596 /* if MLC OOB size is large enough, use 15bit ECC*/
1597 denali->nand.ecc.layout = &nand_15bit_oob;
1598 denali->nand.ecc.bytes = ECC_15BITS;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001599 iowrite32(15, denali->flash_reg + ECC_CORRECTION);
Chuanxiao Dongdb9a32102010-08-06 18:02:03 +08001600 } else if (denali->mtd.oobsize < (denali->bbtskipbytes +
1601 ECC_8BITS * (denali->mtd.writesize /
1602 ECC_SECTOR_SIZE))) {
1603 printk(KERN_ERR "Your NAND chip OOB is not large enough to"
1604 " contain 8bit ECC correction codes");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001605 goto failed_req_irq;
Chuanxiao Dongdb9a32102010-08-06 18:02:03 +08001606 } else {
1607 denali->nand.ecc.layout = &nand_8bit_oob;
1608 denali->nand.ecc.bytes = ECC_8BITS;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001609 iowrite32(8, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +01001610 }
1611
Chuanxiao Dong08b9ab92010-08-06 18:19:09 +08001612 denali->nand.ecc.bytes *= denali->devnum;
Chuanxiao Dongdb9a32102010-08-06 18:02:03 +08001613 denali->nand.ecc.layout->eccbytes *=
1614 denali->mtd.writesize / ECC_SECTOR_SIZE;
1615 denali->nand.ecc.layout->oobfree[0].offset =
1616 denali->bbtskipbytes + denali->nand.ecc.layout->eccbytes;
1617 denali->nand.ecc.layout->oobfree[0].length =
1618 denali->mtd.oobsize - denali->nand.ecc.layout->eccbytes -
1619 denali->bbtskipbytes;
1620
Chuanxiao.Dong66406522010-08-06 18:48:21 +08001621 /* Let driver know the total blocks number and
1622 * how many blocks contained by each nand chip.
1623 * blksperchip will help driver to know how many
1624 * blocks is taken by FW.
1625 * */
1626 denali->totalblks = denali->mtd.size >>
1627 denali->nand.phys_erase_shift;
1628 denali->blksperchip = denali->totalblks / denali->nand.numchips;
1629
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001630 /* These functions are required by the NAND core framework, otherwise,
1631 * the NAND core will assert. However, we don't need them, so we'll stub
1632 * them out. */
Jason Robertsce082592010-05-13 15:57:33 +01001633 denali->nand.ecc.calculate = denali_ecc_calculate;
1634 denali->nand.ecc.correct = denali_ecc_correct;
1635 denali->nand.ecc.hwctl = denali_ecc_hwctl;
1636
1637 /* override the default read operations */
Chuanxiao Dong08b9ab92010-08-06 18:19:09 +08001638 denali->nand.ecc.size = ECC_SECTOR_SIZE * denali->devnum;
Jason Robertsce082592010-05-13 15:57:33 +01001639 denali->nand.ecc.read_page = denali_read_page;
1640 denali->nand.ecc.read_page_raw = denali_read_page_raw;
1641 denali->nand.ecc.write_page = denali_write_page;
1642 denali->nand.ecc.write_page_raw = denali_write_page_raw;
1643 denali->nand.ecc.read_oob = denali_read_oob;
1644 denali->nand.ecc.write_oob = denali_write_oob;
1645 denali->nand.erase_cmd = denali_erase;
1646
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001647 if (nand_scan_tail(&denali->mtd)) {
Jason Robertsce082592010-05-13 15:57:33 +01001648 ret = -ENXIO;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001649 goto failed_req_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001650 }
1651
1652 ret = add_mtd_device(&denali->mtd);
1653 if (ret) {
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001654 dev_err(&dev->dev, "Spectra: Failed to register MTD: %d\n",
1655 ret);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001656 goto failed_req_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001657 }
1658 return 0;
1659
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001660failed_req_irq:
Jason Robertsce082592010-05-13 15:57:33 +01001661 denali_irq_cleanup(dev->irq, denali);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001662failed_remap_mem:
Jason Robertsce082592010-05-13 15:57:33 +01001663 iounmap(denali->flash_mem);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001664failed_remap_reg:
1665 iounmap(denali->flash_reg);
1666failed_req_regions:
Jason Robertsce082592010-05-13 15:57:33 +01001667 pci_release_regions(dev);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001668failed_dma_map:
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001669 pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE,
Jason Robertsce082592010-05-13 15:57:33 +01001670 PCI_DMA_BIDIRECTIONAL);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001671failed_enable_dev:
1672 pci_disable_device(dev);
1673failed_alloc_memery:
Jason Robertsce082592010-05-13 15:57:33 +01001674 kfree(denali);
1675 return ret;
1676}
1677
1678/* driver exit point */
1679static void denali_pci_remove(struct pci_dev *dev)
1680{
1681 struct denali_nand_info *denali = pci_get_drvdata(dev);
1682
Jason Robertsce082592010-05-13 15:57:33 +01001683 nand_release(&denali->mtd);
1684 del_mtd_device(&denali->mtd);
1685
1686 denali_irq_cleanup(dev->irq, denali);
1687
1688 iounmap(denali->flash_reg);
1689 iounmap(denali->flash_mem);
1690 pci_release_regions(dev);
1691 pci_disable_device(dev);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001692 pci_unmap_single(dev, denali->buf.dma_buf, DENALI_BUF_SIZE,
Jason Robertsce082592010-05-13 15:57:33 +01001693 PCI_DMA_BIDIRECTIONAL);
1694 pci_set_drvdata(dev, NULL);
1695 kfree(denali);
1696}
1697
1698MODULE_DEVICE_TABLE(pci, denali_pci_ids);
1699
1700static struct pci_driver denali_pci_driver = {
1701 .name = DENALI_NAND_NAME,
1702 .id_table = denali_pci_ids,
1703 .probe = denali_pci_probe,
1704 .remove = denali_pci_remove,
1705};
1706
1707static int __devinit denali_init(void)
1708{
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001709 printk(KERN_INFO "Spectra MTD driver built on %s @ %s\n",
1710 __DATE__, __TIME__);
Jason Robertsce082592010-05-13 15:57:33 +01001711 return pci_register_driver(&denali_pci_driver);
1712}
1713
1714/* Free memory */
1715static void __devexit denali_exit(void)
1716{
1717 pci_unregister_driver(&denali_pci_driver);
1718}
1719
1720module_init(denali_init);
1721module_exit(denali_exit);