blob: 3184eb9bdd57b0f5289db1cd5c385e96e61ccac8 [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>
Jamie Iles84457942011-05-06 15:28:55 +010022#include <linux/dma-mapping.h>
Jason Robertsce082592010-05-13 15:57:33 +010023#include <linux/wait.h>
24#include <linux/mutex.h>
David Millerb8664b32010-08-04 22:57:51 -070025#include <linux/slab.h>
Jason Robertsce082592010-05-13 15:57:33 +010026#include <linux/pci.h>
27#include <linux/mtd/mtd.h>
28#include <linux/module.h>
29
30#include "denali.h"
31
32MODULE_LICENSE("GPL");
33
Chuanxiao5bac3ac2010-08-05 23:06:04 +080034/* We define a module parameter that allows the user to override
Jason Robertsce082592010-05-13 15:57:33 +010035 * the hardware and decide what timing mode should be used.
36 */
37#define NAND_DEFAULT_TIMINGS -1
38
39static int onfi_timing_mode = NAND_DEFAULT_TIMINGS;
40module_param(onfi_timing_mode, int, S_IRUGO);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +080041MODULE_PARM_DESC(onfi_timing_mode, "Overrides default ONFI setting."
42 " -1 indicates use default timings");
Jason Robertsce082592010-05-13 15:57:33 +010043
44#define DENALI_NAND_NAME "denali-nand"
45
46/* We define a macro here that combines all interrupts this driver uses into
47 * a single constant value, for convenience. */
48#define DENALI_IRQ_ALL (INTR_STATUS0__DMA_CMD_COMP | \
49 INTR_STATUS0__ECC_TRANSACTION_DONE | \
50 INTR_STATUS0__ECC_ERR | \
51 INTR_STATUS0__PROGRAM_FAIL | \
52 INTR_STATUS0__LOAD_COMP | \
53 INTR_STATUS0__PROGRAM_COMP | \
54 INTR_STATUS0__TIME_OUT | \
55 INTR_STATUS0__ERASE_FAIL | \
56 INTR_STATUS0__RST_COMP | \
57 INTR_STATUS0__ERASE_COMP)
58
Chuanxiao5bac3ac2010-08-05 23:06:04 +080059/* indicates whether or not the internal value for the flash bank is
Chuanxiao Dongb292c342010-08-11 17:46:00 +080060 * valid or not */
Chuanxiao5bac3ac2010-08-05 23:06:04 +080061#define CHIP_SELECT_INVALID -1
Jason Robertsce082592010-05-13 15:57:33 +010062
63#define SUPPORT_8BITECC 1
64
Chuanxiao5bac3ac2010-08-05 23:06:04 +080065/* This macro divides two integers and rounds fractional values up
Jason Robertsce082592010-05-13 15:57:33 +010066 * to the nearest integer value. */
67#define CEIL_DIV(X, Y) (((X)%(Y)) ? ((X)/(Y)+1) : ((X)/(Y)))
68
69/* this macro allows us to convert from an MTD structure to our own
70 * device context (denali) structure.
71 */
72#define mtd_to_denali(m) container_of(m, struct denali_nand_info, mtd)
73
74/* These constants are defined by the driver to enable common driver
Chuanxiao Dongb292c342010-08-11 17:46:00 +080075 * configuration options. */
Jason Robertsce082592010-05-13 15:57:33 +010076#define SPARE_ACCESS 0x41
77#define MAIN_ACCESS 0x42
78#define MAIN_SPARE_ACCESS 0x43
79
80#define DENALI_READ 0
81#define DENALI_WRITE 0x100
82
83/* types of device accesses. We can issue commands and get status */
84#define COMMAND_CYCLE 0
85#define ADDR_CYCLE 1
86#define STATUS_CYCLE 2
87
Chuanxiao5bac3ac2010-08-05 23:06:04 +080088/* this is a helper macro that allows us to
Jason Robertsce082592010-05-13 15:57:33 +010089 * format the bank into the proper bits for the controller */
90#define BANK(x) ((x) << 24)
91
92/* List of platforms this NAND controller has be integrated into */
93static const struct pci_device_id denali_pci_ids[] = {
94 { PCI_VDEVICE(INTEL, 0x0701), INTEL_CE4100 },
95 { PCI_VDEVICE(INTEL, 0x0809), INTEL_MRST },
96 { /* end: all zeroes */ }
97};
98
99
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800100/* these are static lookup tables that give us easy access to
Chuanxiao Dongb292c342010-08-11 17:46:00 +0800101 * registers in the NAND controller.
Jason Robertsce082592010-05-13 15:57:33 +0100102 */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800103static const uint32_t intr_status_addresses[4] = {INTR_STATUS0,
104 INTR_STATUS1,
105 INTR_STATUS2,
Jason Robertsce082592010-05-13 15:57:33 +0100106 INTR_STATUS3};
107
108static const uint32_t device_reset_banks[4] = {DEVICE_RESET__BANK0,
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800109 DEVICE_RESET__BANK1,
110 DEVICE_RESET__BANK2,
111 DEVICE_RESET__BANK3};
Jason Robertsce082592010-05-13 15:57:33 +0100112
113static const uint32_t operation_timeout[4] = {INTR_STATUS0__TIME_OUT,
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800114 INTR_STATUS1__TIME_OUT,
115 INTR_STATUS2__TIME_OUT,
116 INTR_STATUS3__TIME_OUT};
Jason Robertsce082592010-05-13 15:57:33 +0100117
118static const uint32_t reset_complete[4] = {INTR_STATUS0__RST_COMP,
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800119 INTR_STATUS1__RST_COMP,
120 INTR_STATUS2__RST_COMP,
121 INTR_STATUS3__RST_COMP};
Jason Robertsce082592010-05-13 15:57:33 +0100122
Jason Robertsce082592010-05-13 15:57:33 +0100123/* forward declarations */
124static void clear_interrupts(struct denali_nand_info *denali);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800125static uint32_t wait_for_irq(struct denali_nand_info *denali,
126 uint32_t irq_mask);
127static void denali_irq_enable(struct denali_nand_info *denali,
128 uint32_t int_mask);
Jason Robertsce082592010-05-13 15:57:33 +0100129static uint32_t read_interrupt_status(struct denali_nand_info *denali);
130
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800131/* Certain operations for the denali NAND controller use
132 * an indexed mode to read/write data. The operation is
133 * performed by writing the address value of the command
134 * to the device memory followed by the data. This function
135 * abstracts this common operation.
Jason Robertsce082592010-05-13 15:57:33 +0100136*/
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800137static void index_addr(struct denali_nand_info *denali,
138 uint32_t address, uint32_t data)
Jason Robertsce082592010-05-13 15:57:33 +0100139{
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800140 iowrite32(address, denali->flash_mem);
141 iowrite32(data, denali->flash_mem + 0x10);
Jason Robertsce082592010-05-13 15:57:33 +0100142}
143
144/* Perform an indexed read of the device */
145static void index_addr_read_data(struct denali_nand_info *denali,
146 uint32_t address, uint32_t *pdata)
147{
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800148 iowrite32(address, denali->flash_mem);
Jason Robertsce082592010-05-13 15:57:33 +0100149 *pdata = ioread32(denali->flash_mem + 0x10);
150}
151
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800152/* We need to buffer some data for some of the NAND core routines.
Jason Robertsce082592010-05-13 15:57:33 +0100153 * The operations manage buffering that data. */
154static void reset_buf(struct denali_nand_info *denali)
155{
156 denali->buf.head = denali->buf.tail = 0;
157}
158
159static void write_byte_to_buf(struct denali_nand_info *denali, uint8_t byte)
160{
161 BUG_ON(denali->buf.tail >= sizeof(denali->buf.buf));
162 denali->buf.buf[denali->buf.tail++] = byte;
163}
164
165/* reads the status of the device */
166static void read_status(struct denali_nand_info *denali)
167{
168 uint32_t cmd = 0x0;
169
170 /* initialize the data buffer to store status */
171 reset_buf(denali);
172
Chuanxiao Dongf0bc0c72010-08-11 17:14:59 +0800173 cmd = ioread32(denali->flash_reg + WRITE_PROTECT);
174 if (cmd)
175 write_byte_to_buf(denali, NAND_STATUS_WP);
176 else
177 write_byte_to_buf(denali, 0);
Jason Robertsce082592010-05-13 15:57:33 +0100178}
179
180/* resets a specific device connected to the core */
181static void reset_bank(struct denali_nand_info *denali)
182{
183 uint32_t irq_status = 0;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800184 uint32_t irq_mask = reset_complete[denali->flash_bank] |
Jason Robertsce082592010-05-13 15:57:33 +0100185 operation_timeout[denali->flash_bank];
186 int bank = 0;
187
188 clear_interrupts(denali);
189
190 bank = device_reset_banks[denali->flash_bank];
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800191 iowrite32(bank, denali->flash_reg + DEVICE_RESET);
Jason Robertsce082592010-05-13 15:57:33 +0100192
193 irq_status = wait_for_irq(denali, irq_mask);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800194
Jason Robertsce082592010-05-13 15:57:33 +0100195 if (irq_status & operation_timeout[denali->flash_bank])
Jamie Iles84457942011-05-06 15:28:55 +0100196 dev_err(denali->dev, "reset bank failed.\n");
Jason Robertsce082592010-05-13 15:57:33 +0100197}
198
199/* Reset the flash controller */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800200static uint16_t denali_nand_reset(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +0100201{
202 uint32_t i;
203
Jamie Iles84457942011-05-06 15:28:55 +0100204 dev_dbg(denali->dev, "%s, Line %d, Function: %s\n",
Jason Robertsce082592010-05-13 15:57:33 +0100205 __FILE__, __LINE__, __func__);
206
207 for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800208 iowrite32(reset_complete[i] | operation_timeout[i],
Jason Robertsce082592010-05-13 15:57:33 +0100209 denali->flash_reg + intr_status_addresses[i]);
210
211 for (i = 0 ; i < LLD_MAX_FLASH_BANKS; i++) {
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800212 iowrite32(device_reset_banks[i],
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800213 denali->flash_reg + DEVICE_RESET);
214 while (!(ioread32(denali->flash_reg +
Chuanxiao Dong628bfd412010-08-11 17:53:29 +0800215 intr_status_addresses[i]) &
Jason Robertsce082592010-05-13 15:57:33 +0100216 (reset_complete[i] | operation_timeout[i])))
Chuanxiao Dong628bfd412010-08-11 17:53:29 +0800217 cpu_relax();
Jason Robertsce082592010-05-13 15:57:33 +0100218 if (ioread32(denali->flash_reg + intr_status_addresses[i]) &
219 operation_timeout[i])
Jamie Iles84457942011-05-06 15:28:55 +0100220 dev_dbg(denali->dev,
Jason Robertsce082592010-05-13 15:57:33 +0100221 "NAND Reset operation timed out on bank %d\n", i);
222 }
223
224 for (i = 0; i < LLD_MAX_FLASH_BANKS; i++)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800225 iowrite32(reset_complete[i] | operation_timeout[i],
Jason Robertsce082592010-05-13 15:57:33 +0100226 denali->flash_reg + intr_status_addresses[i]);
227
228 return PASS;
229}
230
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800231/* this routine calculates the ONFI timing values for a given mode and
232 * programs the clocking register accordingly. The mode is determined by
233 * the get_onfi_nand_para routine.
Jason Robertsce082592010-05-13 15:57:33 +0100234 */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800235static void nand_onfi_timing_set(struct denali_nand_info *denali,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800236 uint16_t mode)
Jason Robertsce082592010-05-13 15:57:33 +0100237{
238 uint16_t Trea[6] = {40, 30, 25, 20, 20, 16};
239 uint16_t Trp[6] = {50, 25, 17, 15, 12, 10};
240 uint16_t Treh[6] = {30, 15, 15, 10, 10, 7};
241 uint16_t Trc[6] = {100, 50, 35, 30, 25, 20};
242 uint16_t Trhoh[6] = {0, 15, 15, 15, 15, 15};
243 uint16_t Trloh[6] = {0, 0, 0, 0, 5, 5};
244 uint16_t Tcea[6] = {100, 45, 30, 25, 25, 25};
245 uint16_t Tadl[6] = {200, 100, 100, 100, 70, 70};
246 uint16_t Trhw[6] = {200, 100, 100, 100, 100, 100};
247 uint16_t Trhz[6] = {200, 100, 100, 100, 100, 100};
248 uint16_t Twhr[6] = {120, 80, 80, 60, 60, 60};
249 uint16_t Tcs[6] = {70, 35, 25, 25, 20, 15};
250
251 uint16_t TclsRising = 1;
252 uint16_t data_invalid_rhoh, data_invalid_rloh, data_invalid;
253 uint16_t dv_window = 0;
254 uint16_t en_lo, en_hi;
255 uint16_t acc_clks;
256 uint16_t addr_2_data, re_2_we, re_2_re, we_2_re, cs_cnt;
257
Jamie Iles84457942011-05-06 15:28:55 +0100258 dev_dbg(denali->dev, "%s, Line %d, Function: %s\n",
Jason Robertsce082592010-05-13 15:57:33 +0100259 __FILE__, __LINE__, __func__);
260
261 en_lo = CEIL_DIV(Trp[mode], CLK_X);
262 en_hi = CEIL_DIV(Treh[mode], CLK_X);
263#if ONFI_BLOOM_TIME
264 if ((en_hi * CLK_X) < (Treh[mode] + 2))
265 en_hi++;
266#endif
267
268 if ((en_lo + en_hi) * CLK_X < Trc[mode])
269 en_lo += CEIL_DIV((Trc[mode] - (en_lo + en_hi) * CLK_X), CLK_X);
270
271 if ((en_lo + en_hi) < CLK_MULTI)
272 en_lo += CLK_MULTI - en_lo - en_hi;
273
274 while (dv_window < 8) {
275 data_invalid_rhoh = en_lo * CLK_X + Trhoh[mode];
276
277 data_invalid_rloh = (en_lo + en_hi) * CLK_X + Trloh[mode];
278
279 data_invalid =
280 data_invalid_rhoh <
281 data_invalid_rloh ? data_invalid_rhoh : data_invalid_rloh;
282
283 dv_window = data_invalid - Trea[mode];
284
285 if (dv_window < 8)
286 en_lo++;
287 }
288
289 acc_clks = CEIL_DIV(Trea[mode], CLK_X);
290
291 while (((acc_clks * CLK_X) - Trea[mode]) < 3)
292 acc_clks++;
293
294 if ((data_invalid - acc_clks * CLK_X) < 2)
Jamie Iles84457942011-05-06 15:28:55 +0100295 dev_warn(denali->dev, "%s, Line %d: Warning!\n",
Jason Robertsce082592010-05-13 15:57:33 +0100296 __FILE__, __LINE__);
297
298 addr_2_data = CEIL_DIV(Tadl[mode], CLK_X);
299 re_2_we = CEIL_DIV(Trhw[mode], CLK_X);
300 re_2_re = CEIL_DIV(Trhz[mode], CLK_X);
301 we_2_re = CEIL_DIV(Twhr[mode], CLK_X);
302 cs_cnt = CEIL_DIV((Tcs[mode] - Trp[mode]), CLK_X);
303 if (!TclsRising)
304 cs_cnt = CEIL_DIV(Tcs[mode], CLK_X);
305 if (cs_cnt == 0)
306 cs_cnt = 1;
307
308 if (Tcea[mode]) {
309 while (((cs_cnt * CLK_X) + Trea[mode]) < Tcea[mode])
310 cs_cnt++;
311 }
312
313#if MODE5_WORKAROUND
314 if (mode == 5)
315 acc_clks = 5;
316#endif
317
318 /* Sighting 3462430: Temporary hack for MT29F128G08CJABAWP:B */
319 if ((ioread32(denali->flash_reg + MANUFACTURER_ID) == 0) &&
320 (ioread32(denali->flash_reg + DEVICE_ID) == 0x88))
321 acc_clks = 6;
322
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800323 iowrite32(acc_clks, denali->flash_reg + ACC_CLKS);
324 iowrite32(re_2_we, denali->flash_reg + RE_2_WE);
325 iowrite32(re_2_re, denali->flash_reg + RE_2_RE);
326 iowrite32(we_2_re, denali->flash_reg + WE_2_RE);
327 iowrite32(addr_2_data, denali->flash_reg + ADDR_2_DATA);
328 iowrite32(en_lo, denali->flash_reg + RDWR_EN_LO_CNT);
329 iowrite32(en_hi, denali->flash_reg + RDWR_EN_HI_CNT);
330 iowrite32(cs_cnt, denali->flash_reg + CS_SETUP_CNT);
Jason Robertsce082592010-05-13 15:57:33 +0100331}
332
Jason Robertsce082592010-05-13 15:57:33 +0100333/* queries the NAND device to see what ONFI modes it supports. */
334static uint16_t get_onfi_nand_para(struct denali_nand_info *denali)
335{
336 int i;
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800337 /* we needn't to do a reset here because driver has already
338 * reset all the banks before
339 * */
Jason Robertsce082592010-05-13 15:57:33 +0100340 if (!(ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
341 ONFI_TIMING_MODE__VALUE))
342 return FAIL;
343
344 for (i = 5; i > 0; i--) {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800345 if (ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
346 (0x01 << i))
Jason Robertsce082592010-05-13 15:57:33 +0100347 break;
348 }
349
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800350 nand_onfi_timing_set(denali, i);
Jason Robertsce082592010-05-13 15:57:33 +0100351
352 /* By now, all the ONFI devices we know support the page cache */
353 /* rw feature. So here we enable the pipeline_rw_ahead feature */
354 /* iowrite32(1, denali->flash_reg + CACHE_WRITE_ENABLE); */
355 /* iowrite32(1, denali->flash_reg + CACHE_READ_ENABLE); */
356
357 return PASS;
358}
359
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800360static void get_samsung_nand_para(struct denali_nand_info *denali,
361 uint8_t device_id)
Jason Robertsce082592010-05-13 15:57:33 +0100362{
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800363 if (device_id == 0xd3) { /* Samsung K9WAG08U1A */
Jason Robertsce082592010-05-13 15:57:33 +0100364 /* Set timing register values according to datasheet */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800365 iowrite32(5, denali->flash_reg + ACC_CLKS);
366 iowrite32(20, denali->flash_reg + RE_2_WE);
367 iowrite32(12, denali->flash_reg + WE_2_RE);
368 iowrite32(14, denali->flash_reg + ADDR_2_DATA);
369 iowrite32(3, denali->flash_reg + RDWR_EN_LO_CNT);
370 iowrite32(2, denali->flash_reg + RDWR_EN_HI_CNT);
371 iowrite32(2, denali->flash_reg + CS_SETUP_CNT);
Jason Robertsce082592010-05-13 15:57:33 +0100372 }
Jason Robertsce082592010-05-13 15:57:33 +0100373}
374
375static void get_toshiba_nand_para(struct denali_nand_info *denali)
376{
Jason Robertsce082592010-05-13 15:57:33 +0100377 uint32_t tmp;
378
379 /* Workaround to fix a controller bug which reports a wrong */
380 /* spare area size for some kind of Toshiba NAND device */
381 if ((ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) == 4096) &&
382 (ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) == 64)) {
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800383 iowrite32(216, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
Jason Robertsce082592010-05-13 15:57:33 +0100384 tmp = ioread32(denali->flash_reg + DEVICES_CONNECTED) *
385 ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800386 iowrite32(tmp,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800387 denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
Jason Robertsce082592010-05-13 15:57:33 +0100388#if SUPPORT_15BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800389 iowrite32(15, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100390#elif SUPPORT_8BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800391 iowrite32(8, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100392#endif
393 }
Jason Robertsce082592010-05-13 15:57:33 +0100394}
395
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800396static void get_hynix_nand_para(struct denali_nand_info *denali,
397 uint8_t device_id)
Jason Robertsce082592010-05-13 15:57:33 +0100398{
Jason Robertsce082592010-05-13 15:57:33 +0100399 uint32_t main_size, spare_size;
400
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800401 switch (device_id) {
Jason Robertsce082592010-05-13 15:57:33 +0100402 case 0xD5: /* Hynix H27UAG8T2A, H27UBG8U5A or H27UCG8VFA */
403 case 0xD7: /* Hynix H27UDG8VEM, H27UCG8UDM or H27UCG8V5A */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800404 iowrite32(128, denali->flash_reg + PAGES_PER_BLOCK);
405 iowrite32(4096, denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
406 iowrite32(224, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800407 main_size = 4096 *
408 ioread32(denali->flash_reg + DEVICES_CONNECTED);
409 spare_size = 224 *
410 ioread32(denali->flash_reg + DEVICES_CONNECTED);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800411 iowrite32(main_size,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800412 denali->flash_reg + LOGICAL_PAGE_DATA_SIZE);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800413 iowrite32(spare_size,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800414 denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800415 iowrite32(0, denali->flash_reg + DEVICE_WIDTH);
Jason Robertsce082592010-05-13 15:57:33 +0100416#if SUPPORT_15BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800417 iowrite32(15, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100418#elif SUPPORT_8BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800419 iowrite32(8, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100420#endif
Jason Robertsce082592010-05-13 15:57:33 +0100421 break;
422 default:
Jamie Iles84457942011-05-06 15:28:55 +0100423 dev_warn(denali->dev,
Jason Robertsce082592010-05-13 15:57:33 +0100424 "Spectra: Unknown Hynix NAND (Device ID: 0x%x)."
425 "Will use default parameter values instead.\n",
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800426 device_id);
Jason Robertsce082592010-05-13 15:57:33 +0100427 }
428}
429
430/* determines how many NAND chips are connected to the controller. Note for
Chuanxiao Dongb292c342010-08-11 17:46:00 +0800431 * Intel CE4100 devices we don't support more than one device.
Jason Robertsce082592010-05-13 15:57:33 +0100432 */
433static void find_valid_banks(struct denali_nand_info *denali)
434{
435 uint32_t id[LLD_MAX_FLASH_BANKS];
436 int i;
437
438 denali->total_used_banks = 1;
439 for (i = 0; i < LLD_MAX_FLASH_BANKS; i++) {
440 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 0), 0x90);
441 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 1), 0);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800442 index_addr_read_data(denali,
443 (uint32_t)(MODE_11 | (i << 24) | 2), &id[i]);
Jason Robertsce082592010-05-13 15:57:33 +0100444
Jamie Iles84457942011-05-06 15:28:55 +0100445 dev_dbg(denali->dev,
Jason Robertsce082592010-05-13 15:57:33 +0100446 "Return 1st ID for bank[%d]: %x\n", i, id[i]);
447
448 if (i == 0) {
449 if (!(id[i] & 0x0ff))
450 break; /* WTF? */
451 } else {
452 if ((id[i] & 0x0ff) == (id[0] & 0x0ff))
453 denali->total_used_banks++;
454 else
455 break;
456 }
457 }
458
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800459 if (denali->platform == INTEL_CE4100) {
Jason Robertsce082592010-05-13 15:57:33 +0100460 /* Platform limitations of the CE4100 device limit
461 * users to a single chip solution for NAND.
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800462 * Multichip support is not enabled.
463 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800464 if (denali->total_used_banks != 1) {
Jamie Iles84457942011-05-06 15:28:55 +0100465 dev_err(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800466 "Sorry, Intel CE4100 only supports "
Jason Robertsce082592010-05-13 15:57:33 +0100467 "a single NAND device.\n");
468 BUG();
469 }
470 }
Jamie Iles84457942011-05-06 15:28:55 +0100471 dev_dbg(denali->dev,
Jason Robertsce082592010-05-13 15:57:33 +0100472 "denali->total_used_banks: %d\n", denali->total_used_banks);
473}
474
475static void detect_partition_feature(struct denali_nand_info *denali)
476{
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800477 /* For MRST platform, denali->fwblks represent the
478 * number of blocks firmware is taken,
479 * FW is in protect partition and MTD driver has no
480 * permission to access it. So let driver know how many
481 * blocks it can't touch.
482 * */
Jason Robertsce082592010-05-13 15:57:33 +0100483 if (ioread32(denali->flash_reg + FEATURES) & FEATURES__PARTITION) {
484 if ((ioread32(denali->flash_reg + PERM_SRC_ID_1) &
485 PERM_SRC_ID_1__SRCID) == SPECTRA_PARTITION_ID) {
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800486 denali->fwblks =
Jason Robertsce082592010-05-13 15:57:33 +0100487 ((ioread32(denali->flash_reg + MIN_MAX_BANK_1) &
488 MIN_MAX_BANK_1__MIN_VALUE) *
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800489 denali->blksperchip)
Jason Robertsce082592010-05-13 15:57:33 +0100490 +
491 (ioread32(denali->flash_reg + MIN_BLK_ADDR_1) &
492 MIN_BLK_ADDR_1__VALUE);
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800493 } else
494 denali->fwblks = SPECTRA_START_BLOCK;
495 } else
496 denali->fwblks = SPECTRA_START_BLOCK;
Jason Robertsce082592010-05-13 15:57:33 +0100497}
498
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800499static uint16_t denali_nand_timing_set(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +0100500{
501 uint16_t status = PASS;
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800502 uint32_t id_bytes[5], addr;
503 uint8_t i, maf_id, device_id;
Jason Robertsce082592010-05-13 15:57:33 +0100504
Jamie Iles84457942011-05-06 15:28:55 +0100505 dev_dbg(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800506 "%s, Line %d, Function: %s\n",
507 __FILE__, __LINE__, __func__);
Jason Robertsce082592010-05-13 15:57:33 +0100508
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800509 /* Use read id method to get device ID and other
510 * params. For some NAND chips, controller can't
511 * report the correct device ID by reading from
512 * DEVICE_ID register
513 * */
514 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
515 index_addr(denali, (uint32_t)addr | 0, 0x90);
516 index_addr(denali, (uint32_t)addr | 1, 0);
517 for (i = 0; i < 5; i++)
518 index_addr_read_data(denali, addr | 2, &id_bytes[i]);
519 maf_id = id_bytes[0];
520 device_id = id_bytes[1];
Jason Robertsce082592010-05-13 15:57:33 +0100521
522 if (ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) &
523 ONFI_DEVICE_NO_OF_LUNS__ONFI_DEVICE) { /* ONFI 1.0 NAND */
524 if (FAIL == get_onfi_nand_para(denali))
525 return FAIL;
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800526 } else if (maf_id == 0xEC) { /* Samsung NAND */
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800527 get_samsung_nand_para(denali, device_id);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800528 } else if (maf_id == 0x98) { /* Toshiba NAND */
Jason Robertsce082592010-05-13 15:57:33 +0100529 get_toshiba_nand_para(denali);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800530 } else if (maf_id == 0xAD) { /* Hynix NAND */
531 get_hynix_nand_para(denali, device_id);
Jason Robertsce082592010-05-13 15:57:33 +0100532 }
533
Jamie Iles84457942011-05-06 15:28:55 +0100534 dev_info(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800535 "Dump timing register values:"
536 "acc_clks: %d, re_2_we: %d, re_2_re: %d\n"
537 "we_2_re: %d, addr_2_data: %d, rdwr_en_lo_cnt: %d\n"
Jason Robertsce082592010-05-13 15:57:33 +0100538 "rdwr_en_hi_cnt: %d, cs_setup_cnt: %d\n",
539 ioread32(denali->flash_reg + ACC_CLKS),
540 ioread32(denali->flash_reg + RE_2_WE),
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800541 ioread32(denali->flash_reg + RE_2_RE),
Jason Robertsce082592010-05-13 15:57:33 +0100542 ioread32(denali->flash_reg + WE_2_RE),
543 ioread32(denali->flash_reg + ADDR_2_DATA),
544 ioread32(denali->flash_reg + RDWR_EN_LO_CNT),
545 ioread32(denali->flash_reg + RDWR_EN_HI_CNT),
546 ioread32(denali->flash_reg + CS_SETUP_CNT));
547
Jason Robertsce082592010-05-13 15:57:33 +0100548 find_valid_banks(denali);
549
550 detect_partition_feature(denali);
551
Jason Robertsce082592010-05-13 15:57:33 +0100552 /* If the user specified to override the default timings
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800553 * with a specific ONFI mode, we apply those changes here.
Jason Robertsce082592010-05-13 15:57:33 +0100554 */
555 if (onfi_timing_mode != NAND_DEFAULT_TIMINGS)
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800556 nand_onfi_timing_set(denali, onfi_timing_mode);
Jason Robertsce082592010-05-13 15:57:33 +0100557
558 return status;
559}
560
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800561static void denali_set_intr_modes(struct denali_nand_info *denali,
Jason Robertsce082592010-05-13 15:57:33 +0100562 uint16_t INT_ENABLE)
563{
Jamie Iles84457942011-05-06 15:28:55 +0100564 dev_dbg(denali->dev, "%s, Line %d, Function: %s\n",
Jason Robertsce082592010-05-13 15:57:33 +0100565 __FILE__, __LINE__, __func__);
566
567 if (INT_ENABLE)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800568 iowrite32(1, denali->flash_reg + GLOBAL_INT_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +0100569 else
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800570 iowrite32(0, denali->flash_reg + GLOBAL_INT_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +0100571}
572
573/* validation function to verify that the controlling software is making
Chuanxiao Dongb292c342010-08-11 17:46:00 +0800574 * a valid request
Jason Robertsce082592010-05-13 15:57:33 +0100575 */
576static inline bool is_flash_bank_valid(int flash_bank)
577{
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800578 return (flash_bank >= 0 && flash_bank < 4);
Jason Robertsce082592010-05-13 15:57:33 +0100579}
580
581static void denali_irq_init(struct denali_nand_info *denali)
582{
583 uint32_t int_mask = 0;
584
585 /* Disable global interrupts */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800586 denali_set_intr_modes(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +0100587
588 int_mask = DENALI_IRQ_ALL;
589
590 /* Clear all status bits */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800591 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS0);
592 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS1);
593 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS2);
594 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS3);
Jason Robertsce082592010-05-13 15:57:33 +0100595
596 denali_irq_enable(denali, int_mask);
597}
598
599static void denali_irq_cleanup(int irqnum, struct denali_nand_info *denali)
600{
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800601 denali_set_intr_modes(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +0100602 free_irq(irqnum, denali);
603}
604
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800605static void denali_irq_enable(struct denali_nand_info *denali,
606 uint32_t int_mask)
Jason Robertsce082592010-05-13 15:57:33 +0100607{
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800608 iowrite32(int_mask, denali->flash_reg + INTR_EN0);
609 iowrite32(int_mask, denali->flash_reg + INTR_EN1);
610 iowrite32(int_mask, denali->flash_reg + INTR_EN2);
611 iowrite32(int_mask, denali->flash_reg + INTR_EN3);
Jason Robertsce082592010-05-13 15:57:33 +0100612}
613
614/* This function only returns when an interrupt that this driver cares about
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800615 * occurs. This is to reduce the overhead of servicing interrupts
Jason Robertsce082592010-05-13 15:57:33 +0100616 */
617static inline uint32_t denali_irq_detected(struct denali_nand_info *denali)
618{
Chuanxiao Donga99d1792010-07-27 11:32:21 +0800619 return read_interrupt_status(denali) & DENALI_IRQ_ALL;
Jason Robertsce082592010-05-13 15:57:33 +0100620}
621
622/* Interrupts are cleared by writing a 1 to the appropriate status bit */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800623static inline void clear_interrupt(struct denali_nand_info *denali,
624 uint32_t irq_mask)
Jason Robertsce082592010-05-13 15:57:33 +0100625{
626 uint32_t intr_status_reg = 0;
627
628 intr_status_reg = intr_status_addresses[denali->flash_bank];
629
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800630 iowrite32(irq_mask, denali->flash_reg + intr_status_reg);
Jason Robertsce082592010-05-13 15:57:33 +0100631}
632
633static void clear_interrupts(struct denali_nand_info *denali)
634{
635 uint32_t status = 0x0;
636 spin_lock_irq(&denali->irq_lock);
637
638 status = read_interrupt_status(denali);
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800639 clear_interrupt(denali, status);
Jason Robertsce082592010-05-13 15:57:33 +0100640
Jason Robertsce082592010-05-13 15:57:33 +0100641 denali->irq_status = 0x0;
642 spin_unlock_irq(&denali->irq_lock);
643}
644
645static uint32_t read_interrupt_status(struct denali_nand_info *denali)
646{
647 uint32_t intr_status_reg = 0;
648
649 intr_status_reg = intr_status_addresses[denali->flash_bank];
650
651 return ioread32(denali->flash_reg + intr_status_reg);
652}
653
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800654/* This is the interrupt service routine. It handles all interrupts
655 * sent to this device. Note that on CE4100, this is a shared
656 * interrupt.
Jason Robertsce082592010-05-13 15:57:33 +0100657 */
658static irqreturn_t denali_isr(int irq, void *dev_id)
659{
660 struct denali_nand_info *denali = dev_id;
661 uint32_t irq_status = 0x0;
662 irqreturn_t result = IRQ_NONE;
663
664 spin_lock(&denali->irq_lock);
665
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800666 /* check to see if a valid NAND chip has
667 * been selected.
Jason Robertsce082592010-05-13 15:57:33 +0100668 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800669 if (is_flash_bank_valid(denali->flash_bank)) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800670 /* check to see if controller generated
Jason Robertsce082592010-05-13 15:57:33 +0100671 * the interrupt, since this is a shared interrupt */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800672 irq_status = denali_irq_detected(denali);
673 if (irq_status != 0) {
Jason Robertsce082592010-05-13 15:57:33 +0100674 /* handle interrupt */
675 /* first acknowledge it */
676 clear_interrupt(denali, irq_status);
677 /* store the status in the device context for someone
678 to read */
679 denali->irq_status |= irq_status;
680 /* notify anyone who cares that it happened */
681 complete(&denali->complete);
682 /* tell the OS that we've handled this */
683 result = IRQ_HANDLED;
684 }
685 }
686 spin_unlock(&denali->irq_lock);
687 return result;
688}
689#define BANK(x) ((x) << 24)
690
691static uint32_t wait_for_irq(struct denali_nand_info *denali, uint32_t irq_mask)
692{
693 unsigned long comp_res = 0;
694 uint32_t intr_status = 0;
695 bool retry = false;
696 unsigned long timeout = msecs_to_jiffies(1000);
697
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800698 do {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800699 comp_res =
700 wait_for_completion_timeout(&denali->complete, timeout);
Jason Robertsce082592010-05-13 15:57:33 +0100701 spin_lock_irq(&denali->irq_lock);
702 intr_status = denali->irq_status;
703
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800704 if (intr_status & irq_mask) {
Jason Robertsce082592010-05-13 15:57:33 +0100705 denali->irq_status &= ~irq_mask;
706 spin_unlock_irq(&denali->irq_lock);
Jason Robertsce082592010-05-13 15:57:33 +0100707 /* our interrupt was detected */
708 break;
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800709 } else {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800710 /* these are not the interrupts you are looking for -
711 * need to wait again */
Jason Robertsce082592010-05-13 15:57:33 +0100712 spin_unlock_irq(&denali->irq_lock);
Jason Robertsce082592010-05-13 15:57:33 +0100713 retry = true;
714 }
715 } while (comp_res != 0);
716
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800717 if (comp_res == 0) {
Jason Robertsce082592010-05-13 15:57:33 +0100718 /* timeout */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800719 printk(KERN_ERR "timeout occurred, status = 0x%x, mask = 0x%x\n",
720 intr_status, irq_mask);
Jason Robertsce082592010-05-13 15:57:33 +0100721
722 intr_status = 0;
723 }
724 return intr_status;
725}
726
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800727/* This helper function setups the registers for ECC and whether or not
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300728 * the spare area will be transferred. */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800729static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en,
Jason Robertsce082592010-05-13 15:57:33 +0100730 bool transfer_spare)
731{
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800732 int ecc_en_flag = 0, transfer_spare_flag = 0;
Jason Robertsce082592010-05-13 15:57:33 +0100733
734 /* set ECC, transfer spare bits if needed */
735 ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0;
736 transfer_spare_flag = transfer_spare ? TRANSFER_SPARE_REG__FLAG : 0;
737
738 /* Enable spare area/ECC per user's request. */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800739 iowrite32(ecc_en_flag, denali->flash_reg + ECC_ENABLE);
740 iowrite32(transfer_spare_flag,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800741 denali->flash_reg + TRANSFER_SPARE_REG);
Jason Robertsce082592010-05-13 15:57:33 +0100742}
743
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800744/* sends a pipeline command operation to the controller. See the Denali NAND
Chuanxiao Dongb292c342010-08-11 17:46:00 +0800745 * controller's user guide for more information (section 4.2.3.6).
Jason Robertsce082592010-05-13 15:57:33 +0100746 */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800747static int denali_send_pipeline_cmd(struct denali_nand_info *denali,
748 bool ecc_en,
749 bool transfer_spare,
750 int access_type,
751 int op)
Jason Robertsce082592010-05-13 15:57:33 +0100752{
753 int status = PASS;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800754 uint32_t addr = 0x0, cmd = 0x0, page_count = 1, irq_status = 0,
Jason Robertsce082592010-05-13 15:57:33 +0100755 irq_mask = 0;
756
Chuanxiao Donga99d1792010-07-27 11:32:21 +0800757 if (op == DENALI_READ)
758 irq_mask = INTR_STATUS0__LOAD_COMP;
759 else if (op == DENALI_WRITE)
760 irq_mask = 0;
761 else
762 BUG();
Jason Robertsce082592010-05-13 15:57:33 +0100763
764 setup_ecc_for_xfer(denali, ecc_en, transfer_spare);
765
Jason Robertsce082592010-05-13 15:57:33 +0100766 /* clear interrupts */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800767 clear_interrupts(denali);
Jason Robertsce082592010-05-13 15:57:33 +0100768
769 addr = BANK(denali->flash_bank) | denali->page;
770
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800771 if (op == DENALI_WRITE && access_type != SPARE_ACCESS) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800772 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800773 iowrite32(cmd, denali->flash_mem);
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800774 } else if (op == DENALI_WRITE && access_type == SPARE_ACCESS) {
Jason Robertsce082592010-05-13 15:57:33 +0100775 /* read spare area */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800776 cmd = MODE_10 | addr;
Jason Robertsce082592010-05-13 15:57:33 +0100777 index_addr(denali, (uint32_t)cmd, access_type);
778
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800779 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800780 iowrite32(cmd, denali->flash_mem);
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800781 } else if (op == DENALI_READ) {
Jason Robertsce082592010-05-13 15:57:33 +0100782 /* setup page read request for access type */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800783 cmd = MODE_10 | addr;
Jason Robertsce082592010-05-13 15:57:33 +0100784 index_addr(denali, (uint32_t)cmd, access_type);
785
786 /* page 33 of the NAND controller spec indicates we should not
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800787 use the pipeline commands in Spare area only mode. So we
Jason Robertsce082592010-05-13 15:57:33 +0100788 don't.
789 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800790 if (access_type == SPARE_ACCESS) {
Jason Robertsce082592010-05-13 15:57:33 +0100791 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800792 iowrite32(cmd, denali->flash_mem);
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800793 } else {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800794 index_addr(denali, (uint32_t)cmd,
795 0x2000 | op | page_count);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800796
797 /* wait for command to be accepted
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800798 * can always use status0 bit as the
799 * mask is identical for each
Jason Robertsce082592010-05-13 15:57:33 +0100800 * bank. */
801 irq_status = wait_for_irq(denali, irq_mask);
802
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800803 if (irq_status == 0) {
Jamie Iles84457942011-05-06 15:28:55 +0100804 dev_err(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800805 "cmd, page, addr on timeout "
806 "(0x%x, 0x%x, 0x%x)\n",
807 cmd, denali->page, addr);
Jason Robertsce082592010-05-13 15:57:33 +0100808 status = FAIL;
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800809 } else {
Jason Robertsce082592010-05-13 15:57:33 +0100810 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800811 iowrite32(cmd, denali->flash_mem);
Jason Robertsce082592010-05-13 15:57:33 +0100812 }
813 }
814 }
815 return status;
816}
817
818/* helper function that simply writes a buffer to the flash */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800819static int write_data_to_flash_mem(struct denali_nand_info *denali,
820 const uint8_t *buf,
821 int len)
Jason Robertsce082592010-05-13 15:57:33 +0100822{
823 uint32_t i = 0, *buf32;
824
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800825 /* verify that the len is a multiple of 4. see comment in
826 * read_data_from_flash_mem() */
Jason Robertsce082592010-05-13 15:57:33 +0100827 BUG_ON((len % 4) != 0);
828
829 /* write the data to the flash memory */
830 buf32 = (uint32_t *)buf;
831 for (i = 0; i < len / 4; i++)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800832 iowrite32(*buf32++, denali->flash_mem + 0x10);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800833 return i*4; /* intent is to return the number of bytes read */
Jason Robertsce082592010-05-13 15:57:33 +0100834}
835
836/* helper function that simply reads a buffer from the flash */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800837static int read_data_from_flash_mem(struct denali_nand_info *denali,
838 uint8_t *buf,
839 int len)
Jason Robertsce082592010-05-13 15:57:33 +0100840{
841 uint32_t i = 0, *buf32;
842
843 /* we assume that len will be a multiple of 4, if not
844 * it would be nice to know about it ASAP rather than
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800845 * have random failures...
846 * This assumption is based on the fact that this
847 * function is designed to be used to read flash pages,
Jason Robertsce082592010-05-13 15:57:33 +0100848 * which are typically multiples of 4...
849 */
850
851 BUG_ON((len % 4) != 0);
852
853 /* transfer the data from the flash */
854 buf32 = (uint32_t *)buf;
855 for (i = 0; i < len / 4; i++)
Jason Robertsce082592010-05-13 15:57:33 +0100856 *buf32++ = ioread32(denali->flash_mem + 0x10);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800857 return i*4; /* intent is to return the number of bytes read */
Jason Robertsce082592010-05-13 15:57:33 +0100858}
859
860/* writes OOB data to the device */
861static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
862{
863 struct denali_nand_info *denali = mtd_to_denali(mtd);
864 uint32_t irq_status = 0;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800865 uint32_t irq_mask = INTR_STATUS0__PROGRAM_COMP |
Jason Robertsce082592010-05-13 15:57:33 +0100866 INTR_STATUS0__PROGRAM_FAIL;
867 int status = 0;
868
869 denali->page = page;
870
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800871 if (denali_send_pipeline_cmd(denali, false, false, SPARE_ACCESS,
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800872 DENALI_WRITE) == PASS) {
Jason Robertsce082592010-05-13 15:57:33 +0100873 write_data_to_flash_mem(denali, buf, mtd->oobsize);
874
Jason Robertsce082592010-05-13 15:57:33 +0100875 /* wait for operation to complete */
876 irq_status = wait_for_irq(denali, irq_mask);
877
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800878 if (irq_status == 0) {
Jamie Iles84457942011-05-06 15:28:55 +0100879 dev_err(denali->dev, "OOB write failed\n");
Jason Robertsce082592010-05-13 15:57:33 +0100880 status = -EIO;
881 }
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800882 } else {
Jamie Iles84457942011-05-06 15:28:55 +0100883 dev_err(denali->dev, "unable to send pipeline command\n");
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800884 status = -EIO;
Jason Robertsce082592010-05-13 15:57:33 +0100885 }
886 return status;
887}
888
889/* reads OOB data from the device */
890static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
891{
892 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800893 uint32_t irq_mask = INTR_STATUS0__LOAD_COMP,
894 irq_status = 0, addr = 0x0, cmd = 0x0;
Jason Robertsce082592010-05-13 15:57:33 +0100895
896 denali->page = page;
897
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800898 if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800899 DENALI_READ) == PASS) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800900 read_data_from_flash_mem(denali, buf, mtd->oobsize);
Jason Robertsce082592010-05-13 15:57:33 +0100901
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800902 /* wait for command to be accepted
Jason Robertsce082592010-05-13 15:57:33 +0100903 * can always use status0 bit as the mask is identical for each
904 * bank. */
905 irq_status = wait_for_irq(denali, irq_mask);
906
907 if (irq_status == 0)
Jamie Iles84457942011-05-06 15:28:55 +0100908 dev_err(denali->dev, "page on OOB timeout %d\n",
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800909 denali->page);
Jason Robertsce082592010-05-13 15:57:33 +0100910
911 /* We set the device back to MAIN_ACCESS here as I observed
912 * instability with the controller if you do a block erase
913 * and the last transaction was a SPARE_ACCESS. Block erase
914 * is reliable (according to the MTD test infrastructure)
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800915 * if you are in MAIN_ACCESS.
Jason Robertsce082592010-05-13 15:57:33 +0100916 */
917 addr = BANK(denali->flash_bank) | denali->page;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800918 cmd = MODE_10 | addr;
Jason Robertsce082592010-05-13 15:57:33 +0100919 index_addr(denali, (uint32_t)cmd, MAIN_ACCESS);
Jason Robertsce082592010-05-13 15:57:33 +0100920 }
921}
922
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800923/* this function examines buffers to see if they contain data that
Jason Robertsce082592010-05-13 15:57:33 +0100924 * indicate that the buffer is part of an erased region of flash.
925 */
926bool is_erased(uint8_t *buf, int len)
927{
928 int i = 0;
929 for (i = 0; i < len; i++)
Jason Robertsce082592010-05-13 15:57:33 +0100930 if (buf[i] != 0xFF)
Jason Robertsce082592010-05-13 15:57:33 +0100931 return false;
Jason Robertsce082592010-05-13 15:57:33 +0100932 return true;
933}
934#define ECC_SECTOR_SIZE 512
935
936#define ECC_SECTOR(x) (((x) & ECC_ERROR_ADDRESS__SECTOR_NR) >> 12)
937#define ECC_BYTE(x) (((x) & ECC_ERROR_ADDRESS__OFFSET))
938#define ECC_CORRECTION_VALUE(x) ((x) & ERR_CORRECTION_INFO__BYTEMASK)
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800939#define ECC_ERROR_CORRECTABLE(x) (!((x) & ERR_CORRECTION_INFO__ERROR_TYPE))
940#define ECC_ERR_DEVICE(x) (((x) & ERR_CORRECTION_INFO__DEVICE_NR) >> 8)
Jason Robertsce082592010-05-13 15:57:33 +0100941#define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO)
942
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800943static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf,
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800944 uint32_t irq_status)
Jason Robertsce082592010-05-13 15:57:33 +0100945{
946 bool check_erased_page = false;
947
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800948 if (irq_status & INTR_STATUS0__ECC_ERR) {
Jason Robertsce082592010-05-13 15:57:33 +0100949 /* read the ECC errors. we'll ignore them for now */
950 uint32_t err_address = 0, err_correction_info = 0;
951 uint32_t err_byte = 0, err_sector = 0, err_device = 0;
952 uint32_t err_correction_value = 0;
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800953 denali_set_intr_modes(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +0100954
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800955 do {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800956 err_address = ioread32(denali->flash_reg +
Jason Robertsce082592010-05-13 15:57:33 +0100957 ECC_ERROR_ADDRESS);
958 err_sector = ECC_SECTOR(err_address);
959 err_byte = ECC_BYTE(err_address);
960
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800961 err_correction_info = ioread32(denali->flash_reg +
Jason Robertsce082592010-05-13 15:57:33 +0100962 ERR_CORRECTION_INFO);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800963 err_correction_value =
Jason Robertsce082592010-05-13 15:57:33 +0100964 ECC_CORRECTION_VALUE(err_correction_info);
965 err_device = ECC_ERR_DEVICE(err_correction_info);
966
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800967 if (ECC_ERROR_CORRECTABLE(err_correction_info)) {
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800968 /* If err_byte is larger than ECC_SECTOR_SIZE,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300969 * means error happened in OOB, so we ignore
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800970 * it. It's no need for us to correct it
971 * err_device is represented the NAND error
972 * bits are happened in if there are more
973 * than one NAND connected.
974 * */
975 if (err_byte < ECC_SECTOR_SIZE) {
976 int offset;
977 offset = (err_sector *
978 ECC_SECTOR_SIZE +
979 err_byte) *
980 denali->devnum +
981 err_device;
Jason Robertsce082592010-05-13 15:57:33 +0100982 /* correct the ECC error */
983 buf[offset] ^= err_correction_value;
984 denali->mtd.ecc_stats.corrected++;
Jason Robertsce082592010-05-13 15:57:33 +0100985 }
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800986 } else {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800987 /* if the error is not correctable, need to
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800988 * look at the page to see if it is an erased
989 * page. if so, then it's not a real ECC error
990 * */
Jason Robertsce082592010-05-13 15:57:33 +0100991 check_erased_page = true;
992 }
Jason Robertsce082592010-05-13 15:57:33 +0100993 } while (!ECC_LAST_ERR(err_correction_info));
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800994 /* Once handle all ecc errors, controller will triger
995 * a ECC_TRANSACTION_DONE interrupt, so here just wait
996 * for a while for this interrupt
997 * */
998 while (!(read_interrupt_status(denali) &
999 INTR_STATUS0__ECC_TRANSACTION_DONE))
1000 cpu_relax();
1001 clear_interrupts(denali);
1002 denali_set_intr_modes(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001003 }
1004 return check_erased_page;
1005}
1006
1007/* programs the controller to either enable/disable DMA transfers */
David Woodhouseaadff492010-05-13 16:12:43 +01001008static void denali_enable_dma(struct denali_nand_info *denali, bool en)
Jason Robertsce082592010-05-13 15:57:33 +01001009{
1010 uint32_t reg_val = 0x0;
1011
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001012 if (en)
1013 reg_val = DMA_ENABLE__FLAG;
Jason Robertsce082592010-05-13 15:57:33 +01001014
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001015 iowrite32(reg_val, denali->flash_reg + DMA_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +01001016 ioread32(denali->flash_reg + DMA_ENABLE);
1017}
1018
1019/* setups the HW to perform the data DMA */
David Woodhouseaadff492010-05-13 16:12:43 +01001020static void denali_setup_dma(struct denali_nand_info *denali, int op)
Jason Robertsce082592010-05-13 15:57:33 +01001021{
1022 uint32_t mode = 0x0;
1023 const int page_count = 1;
1024 dma_addr_t addr = denali->buf.dma_buf;
1025
1026 mode = MODE_10 | BANK(denali->flash_bank);
1027
1028 /* DMA is a four step process */
1029
1030 /* 1. setup transfer type and # of pages */
1031 index_addr(denali, mode | denali->page, 0x2000 | op | page_count);
1032
1033 /* 2. set memory high address bits 23:8 */
1034 index_addr(denali, mode | ((uint16_t)(addr >> 16) << 8), 0x2200);
1035
1036 /* 3. set memory low address bits 23:8 */
1037 index_addr(denali, mode | ((uint16_t)addr << 8), 0x2300);
1038
1039 /* 4. interrupt when complete, burst len = 64 bytes*/
1040 index_addr(denali, mode | 0x14000, 0x2400);
1041}
1042
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001043/* writes a page. user specifies type, and this function handles the
Chuanxiao Dongb292c342010-08-11 17:46:00 +08001044 * configuration details. */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001045static void write_page(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001046 const uint8_t *buf, bool raw_xfer)
1047{
1048 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jason Robertsce082592010-05-13 15:57:33 +01001049
1050 dma_addr_t addr = denali->buf.dma_buf;
1051 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1052
1053 uint32_t irq_status = 0;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001054 uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP |
Jason Robertsce082592010-05-13 15:57:33 +01001055 INTR_STATUS0__PROGRAM_FAIL;
1056
1057 /* if it is a raw xfer, we want to disable ecc, and send
1058 * the spare area.
1059 * !raw_xfer - enable ecc
1060 * raw_xfer - transfer spare
1061 */
1062 setup_ecc_for_xfer(denali, !raw_xfer, raw_xfer);
1063
1064 /* copy buffer into DMA buffer */
1065 memcpy(denali->buf.buf, buf, mtd->writesize);
1066
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001067 if (raw_xfer) {
Jason Robertsce082592010-05-13 15:57:33 +01001068 /* transfer the data to the spare area */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001069 memcpy(denali->buf.buf + mtd->writesize,
1070 chip->oob_poi,
1071 mtd->oobsize);
Jason Robertsce082592010-05-13 15:57:33 +01001072 }
1073
Jamie Iles84457942011-05-06 15:28:55 +01001074 dma_sync_single_for_device(denali->dev, addr, size, DMA_TO_DEVICE);
Jason Robertsce082592010-05-13 15:57:33 +01001075
1076 clear_interrupts(denali);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001077 denali_enable_dma(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001078
David Woodhouseaadff492010-05-13 16:12:43 +01001079 denali_setup_dma(denali, DENALI_WRITE);
Jason Robertsce082592010-05-13 15:57:33 +01001080
1081 /* wait for operation to complete */
1082 irq_status = wait_for_irq(denali, irq_mask);
1083
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001084 if (irq_status == 0) {
Jamie Iles84457942011-05-06 15:28:55 +01001085 dev_err(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001086 "timeout on write_page (type = %d)\n",
1087 raw_xfer);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001088 denali->status =
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001089 (irq_status & INTR_STATUS0__PROGRAM_FAIL) ?
1090 NAND_STATUS_FAIL : PASS;
Jason Robertsce082592010-05-13 15:57:33 +01001091 }
1092
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001093 denali_enable_dma(denali, false);
Jamie Iles84457942011-05-06 15:28:55 +01001094 dma_sync_single_for_cpu(denali->dev, addr, size, DMA_TO_DEVICE);
Jason Robertsce082592010-05-13 15:57:33 +01001095}
1096
1097/* NAND core entry points */
1098
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001099/* this is the callback that the NAND core calls to write a page. Since
Chuanxiao Dongb292c342010-08-11 17:46:00 +08001100 * writing a page with ECC or without is similar, all the work is done
1101 * by write_page above.
1102 * */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001103static void denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001104 const uint8_t *buf)
1105{
1106 /* for regular page writes, we let HW handle all the ECC
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001107 * data written to the device. */
Jason Robertsce082592010-05-13 15:57:33 +01001108 write_page(mtd, chip, buf, false);
1109}
1110
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001111/* This is the callback that the NAND core calls to write a page without ECC.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001112 * raw access is similar to ECC page writes, so all the work is done in the
Chuanxiao Dongb292c342010-08-11 17:46:00 +08001113 * write_page() function above.
Jason Robertsce082592010-05-13 15:57:33 +01001114 */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001115static void denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001116 const uint8_t *buf)
1117{
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001118 /* for raw page writes, we want to disable ECC and simply write
Jason Robertsce082592010-05-13 15:57:33 +01001119 whatever data is in the buffer. */
1120 write_page(mtd, chip, buf, true);
1121}
1122
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001123static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001124 int page)
1125{
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001126 return write_oob_data(mtd, chip->oob_poi, page);
Jason Robertsce082592010-05-13 15:57:33 +01001127}
1128
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001129static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001130 int page, int sndcmd)
1131{
1132 read_oob_data(mtd, chip->oob_poi, page);
1133
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001134 return 0; /* notify NAND core to send command to
1135 NAND device. */
Jason Robertsce082592010-05-13 15:57:33 +01001136}
1137
1138static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1139 uint8_t *buf, int page)
1140{
1141 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jason Robertsce082592010-05-13 15:57:33 +01001142
1143 dma_addr_t addr = denali->buf.dma_buf;
1144 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1145
1146 uint32_t irq_status = 0;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001147 uint32_t irq_mask = INTR_STATUS0__ECC_TRANSACTION_DONE |
Jason Robertsce082592010-05-13 15:57:33 +01001148 INTR_STATUS0__ECC_ERR;
1149 bool check_erased_page = false;
1150
Chuanxiao Dong7d8a26f2010-08-11 18:19:23 +08001151 if (page != denali->page) {
Jamie Iles84457942011-05-06 15:28:55 +01001152 dev_err(denali->dev, "IN %s: page %d is not"
Chuanxiao Dong7d8a26f2010-08-11 18:19:23 +08001153 " equal to denali->page %d, investigate!!",
1154 __func__, page, denali->page);
1155 BUG();
1156 }
1157
Jason Robertsce082592010-05-13 15:57:33 +01001158 setup_ecc_for_xfer(denali, true, false);
1159
David Woodhouseaadff492010-05-13 16:12:43 +01001160 denali_enable_dma(denali, true);
Jamie Iles84457942011-05-06 15:28:55 +01001161 dma_sync_single_for_device(denali->dev, addr, size, DMA_FROM_DEVICE);
Jason Robertsce082592010-05-13 15:57:33 +01001162
1163 clear_interrupts(denali);
David Woodhouseaadff492010-05-13 16:12:43 +01001164 denali_setup_dma(denali, DENALI_READ);
Jason Robertsce082592010-05-13 15:57:33 +01001165
1166 /* wait for operation to complete */
1167 irq_status = wait_for_irq(denali, irq_mask);
1168
Jamie Iles84457942011-05-06 15:28:55 +01001169 dma_sync_single_for_cpu(denali->dev, addr, size, DMA_FROM_DEVICE);
Jason Robertsce082592010-05-13 15:57:33 +01001170
1171 memcpy(buf, denali->buf.buf, mtd->writesize);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001172
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +08001173 check_erased_page = handle_ecc(denali, buf, irq_status);
David Woodhouseaadff492010-05-13 16:12:43 +01001174 denali_enable_dma(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +01001175
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001176 if (check_erased_page) {
Jason Robertsce082592010-05-13 15:57:33 +01001177 read_oob_data(&denali->mtd, chip->oob_poi, denali->page);
1178
1179 /* check ECC failures that may have occurred on erased pages */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001180 if (check_erased_page) {
Jason Robertsce082592010-05-13 15:57:33 +01001181 if (!is_erased(buf, denali->mtd.writesize))
Jason Robertsce082592010-05-13 15:57:33 +01001182 denali->mtd.ecc_stats.failed++;
Jason Robertsce082592010-05-13 15:57:33 +01001183 if (!is_erased(buf, denali->mtd.oobsize))
Jason Robertsce082592010-05-13 15:57:33 +01001184 denali->mtd.ecc_stats.failed++;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001185 }
Jason Robertsce082592010-05-13 15:57:33 +01001186 }
1187 return 0;
1188}
1189
1190static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1191 uint8_t *buf, int page)
1192{
1193 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jason Robertsce082592010-05-13 15:57:33 +01001194
1195 dma_addr_t addr = denali->buf.dma_buf;
1196 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1197
1198 uint32_t irq_status = 0;
1199 uint32_t irq_mask = INTR_STATUS0__DMA_CMD_COMP;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001200
Chuanxiao Dong7d8a26f2010-08-11 18:19:23 +08001201 if (page != denali->page) {
Jamie Iles84457942011-05-06 15:28:55 +01001202 dev_err(denali->dev, "IN %s: page %d is not"
Chuanxiao Dong7d8a26f2010-08-11 18:19:23 +08001203 " equal to denali->page %d, investigate!!",
1204 __func__, page, denali->page);
1205 BUG();
1206 }
1207
Jason Robertsce082592010-05-13 15:57:33 +01001208 setup_ecc_for_xfer(denali, false, true);
David Woodhouseaadff492010-05-13 16:12:43 +01001209 denali_enable_dma(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001210
Jamie Iles84457942011-05-06 15:28:55 +01001211 dma_sync_single_for_device(denali->dev, addr, size, DMA_FROM_DEVICE);
Jason Robertsce082592010-05-13 15:57:33 +01001212
1213 clear_interrupts(denali);
David Woodhouseaadff492010-05-13 16:12:43 +01001214 denali_setup_dma(denali, DENALI_READ);
Jason Robertsce082592010-05-13 15:57:33 +01001215
1216 /* wait for operation to complete */
1217 irq_status = wait_for_irq(denali, irq_mask);
1218
Jamie Iles84457942011-05-06 15:28:55 +01001219 dma_sync_single_for_cpu(denali->dev, addr, size, DMA_FROM_DEVICE);
Jason Robertsce082592010-05-13 15:57:33 +01001220
David Woodhouseaadff492010-05-13 16:12:43 +01001221 denali_enable_dma(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +01001222
1223 memcpy(buf, denali->buf.buf, mtd->writesize);
1224 memcpy(chip->oob_poi, denali->buf.buf + mtd->writesize, mtd->oobsize);
1225
1226 return 0;
1227}
1228
1229static uint8_t denali_read_byte(struct mtd_info *mtd)
1230{
1231 struct denali_nand_info *denali = mtd_to_denali(mtd);
1232 uint8_t result = 0xff;
1233
1234 if (denali->buf.head < denali->buf.tail)
Jason Robertsce082592010-05-13 15:57:33 +01001235 result = denali->buf.buf[denali->buf.head++];
Jason Robertsce082592010-05-13 15:57:33 +01001236
Jason Robertsce082592010-05-13 15:57:33 +01001237 return result;
1238}
1239
1240static void denali_select_chip(struct mtd_info *mtd, int chip)
1241{
1242 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001243
Jason Robertsce082592010-05-13 15:57:33 +01001244 spin_lock_irq(&denali->irq_lock);
1245 denali->flash_bank = chip;
1246 spin_unlock_irq(&denali->irq_lock);
1247}
1248
1249static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
1250{
1251 struct denali_nand_info *denali = mtd_to_denali(mtd);
1252 int status = denali->status;
1253 denali->status = 0;
1254
Jason Robertsce082592010-05-13 15:57:33 +01001255 return status;
1256}
1257
1258static void denali_erase(struct mtd_info *mtd, int page)
1259{
1260 struct denali_nand_info *denali = mtd_to_denali(mtd);
1261
1262 uint32_t cmd = 0x0, irq_status = 0;
1263
Jason Robertsce082592010-05-13 15:57:33 +01001264 /* clear interrupts */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001265 clear_interrupts(denali);
Jason Robertsce082592010-05-13 15:57:33 +01001266
1267 /* setup page read request for access type */
1268 cmd = MODE_10 | BANK(denali->flash_bank) | page;
1269 index_addr(denali, (uint32_t)cmd, 0x1);
1270
1271 /* wait for erase to complete or failure to occur */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001272 irq_status = wait_for_irq(denali, INTR_STATUS0__ERASE_COMP |
Jason Robertsce082592010-05-13 15:57:33 +01001273 INTR_STATUS0__ERASE_FAIL);
1274
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001275 denali->status = (irq_status & INTR_STATUS0__ERASE_FAIL) ?
1276 NAND_STATUS_FAIL : PASS;
Jason Robertsce082592010-05-13 15:57:33 +01001277}
1278
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001279static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col,
Jason Robertsce082592010-05-13 15:57:33 +01001280 int page)
1281{
1282 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +08001283 uint32_t addr, id;
1284 int i;
Jason Robertsce082592010-05-13 15:57:33 +01001285
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001286 switch (cmd) {
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001287 case NAND_CMD_PAGEPROG:
1288 break;
1289 case NAND_CMD_STATUS:
1290 read_status(denali);
1291 break;
1292 case NAND_CMD_READID:
Florian Fainelli42af8b52010-08-30 18:32:20 +02001293 case NAND_CMD_PARAM:
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001294 reset_buf(denali);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +08001295 /*sometimes ManufactureId read from register is not right
1296 * e.g. some of Micron MT29F32G08QAA MLC NAND chips
1297 * So here we send READID cmd to NAND insteand
1298 * */
1299 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
1300 index_addr(denali, (uint32_t)addr | 0, 0x90);
1301 index_addr(denali, (uint32_t)addr | 1, 0);
1302 for (i = 0; i < 5; i++) {
1303 index_addr_read_data(denali,
1304 (uint32_t)addr | 2,
1305 &id);
1306 write_byte_to_buf(denali, id);
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001307 }
1308 break;
1309 case NAND_CMD_READ0:
1310 case NAND_CMD_SEQIN:
1311 denali->page = page;
1312 break;
1313 case NAND_CMD_RESET:
1314 reset_bank(denali);
1315 break;
1316 case NAND_CMD_READOOB:
1317 /* TODO: Read OOB data */
1318 break;
1319 default:
1320 printk(KERN_ERR ": unsupported command"
1321 " received 0x%x\n", cmd);
1322 break;
Jason Robertsce082592010-05-13 15:57:33 +01001323 }
1324}
1325
1326/* stubs for ECC functions not used by the NAND core */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001327static int denali_ecc_calculate(struct mtd_info *mtd, const uint8_t *data,
Jason Robertsce082592010-05-13 15:57:33 +01001328 uint8_t *ecc_code)
1329{
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001330 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jamie Iles84457942011-05-06 15:28:55 +01001331 dev_err(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001332 "denali_ecc_calculate called unexpectedly\n");
Jason Robertsce082592010-05-13 15:57:33 +01001333 BUG();
1334 return -EIO;
1335}
1336
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001337static int denali_ecc_correct(struct mtd_info *mtd, uint8_t *data,
Jason Robertsce082592010-05-13 15:57:33 +01001338 uint8_t *read_ecc, uint8_t *calc_ecc)
1339{
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001340 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jamie Iles84457942011-05-06 15:28:55 +01001341 dev_err(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001342 "denali_ecc_correct called unexpectedly\n");
Jason Robertsce082592010-05-13 15:57:33 +01001343 BUG();
1344 return -EIO;
1345}
1346
1347static void denali_ecc_hwctl(struct mtd_info *mtd, int mode)
1348{
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001349 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jamie Iles84457942011-05-06 15:28:55 +01001350 dev_err(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001351 "denali_ecc_hwctl called unexpectedly\n");
Jason Robertsce082592010-05-13 15:57:33 +01001352 BUG();
1353}
1354/* end NAND core entry points */
1355
1356/* Initialization code to bring the device up to a known good state */
1357static void denali_hw_init(struct denali_nand_info *denali)
1358{
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001359 /* tell driver how many bit controller will skip before
1360 * writing ECC code in OOB, this register may be already
1361 * set by firmware. So we read this value out.
1362 * if this value is 0, just let it be.
1363 * */
1364 denali->bbtskipbytes = ioread32(denali->flash_reg +
1365 SPARE_AREA_SKIP_BYTES);
Chuanxiao Dongeda936e2010-07-27 14:17:37 +08001366 denali_nand_reset(denali);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001367 iowrite32(0x0F, denali->flash_reg + RB_PIN_ENABLED);
1368 iowrite32(CHIP_EN_DONT_CARE__FLAG,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001369 denali->flash_reg + CHIP_ENABLE_DONT_CARE);
Jason Robertsce082592010-05-13 15:57:33 +01001370
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001371 iowrite32(0xffff, denali->flash_reg + SPARE_AREA_MARKER);
Jason Robertsce082592010-05-13 15:57:33 +01001372
1373 /* Should set value for these registers when init */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001374 iowrite32(0, denali->flash_reg + TWO_ROW_ADDR_CYCLES);
1375 iowrite32(1, denali->flash_reg + ECC_ENABLE);
Chuanxiao Dong5eab6aaa2010-08-12 10:07:18 +08001376 denali_nand_timing_set(denali);
1377 denali_irq_init(denali);
Jason Robertsce082592010-05-13 15:57:33 +01001378}
1379
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001380/* Althogh controller spec said SLC ECC is forceb to be 4bit,
1381 * but denali controller in MRST only support 15bit and 8bit ECC
1382 * correction
1383 * */
1384#define ECC_8BITS 14
1385static struct nand_ecclayout nand_8bit_oob = {
1386 .eccbytes = 14,
Jason Robertsce082592010-05-13 15:57:33 +01001387};
1388
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001389#define ECC_15BITS 26
1390static struct nand_ecclayout nand_15bit_oob = {
1391 .eccbytes = 26,
Jason Robertsce082592010-05-13 15:57:33 +01001392};
1393
1394static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1395static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1396
1397static struct nand_bbt_descr bbt_main_descr = {
1398 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1399 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1400 .offs = 8,
1401 .len = 4,
1402 .veroffs = 12,
1403 .maxblocks = 4,
1404 .pattern = bbt_pattern,
1405};
1406
1407static struct nand_bbt_descr bbt_mirror_descr = {
1408 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1409 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1410 .offs = 8,
1411 .len = 4,
1412 .veroffs = 12,
1413 .maxblocks = 4,
1414 .pattern = mirror_pattern,
1415};
1416
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001417/* initialize driver data structures */
Jason Robertsce082592010-05-13 15:57:33 +01001418void denali_drv_init(struct denali_nand_info *denali)
1419{
1420 denali->idx = 0;
1421
1422 /* setup interrupt handler */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001423 /* the completion object will be used to notify
Jason Robertsce082592010-05-13 15:57:33 +01001424 * the callee that the interrupt is done */
1425 init_completion(&denali->complete);
1426
1427 /* the spinlock will be used to synchronize the ISR
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001428 * with any element that might be access shared
Jason Robertsce082592010-05-13 15:57:33 +01001429 * data (interrupt status) */
1430 spin_lock_init(&denali->irq_lock);
1431
1432 /* indicate that MTD has not selected a valid bank yet */
1433 denali->flash_bank = CHIP_SELECT_INVALID;
1434
1435 /* initialize our irq_status variable to indicate no interrupts */
1436 denali->irq_status = 0;
1437}
1438
1439/* driver entry point */
1440static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
1441{
1442 int ret = -ENODEV;
1443 resource_size_t csr_base, mem_base;
1444 unsigned long csr_len, mem_len;
1445 struct denali_nand_info *denali;
1446
Jason Robertsce082592010-05-13 15:57:33 +01001447 denali = kzalloc(sizeof(*denali), GFP_KERNEL);
1448 if (!denali)
1449 return -ENOMEM;
1450
1451 ret = pci_enable_device(dev);
1452 if (ret) {
1453 printk(KERN_ERR "Spectra: pci_enable_device failed.\n");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001454 goto failed_alloc_memery;
Jason Robertsce082592010-05-13 15:57:33 +01001455 }
1456
1457 if (id->driver_data == INTEL_CE4100) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001458 /* Due to a silicon limitation, we can only support
1459 * ONFI timing mode 1 and below.
1460 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001461 if (onfi_timing_mode < -1 || onfi_timing_mode > 1) {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001462 printk(KERN_ERR "Intel CE4100 only supports"
1463 " ONFI timing mode 1 or below\n");
Jason Robertsce082592010-05-13 15:57:33 +01001464 ret = -EINVAL;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001465 goto failed_enable_dev;
Jason Robertsce082592010-05-13 15:57:33 +01001466 }
1467 denali->platform = INTEL_CE4100;
1468 mem_base = pci_resource_start(dev, 0);
1469 mem_len = pci_resource_len(dev, 1);
1470 csr_base = pci_resource_start(dev, 1);
1471 csr_len = pci_resource_len(dev, 1);
1472 } else {
1473 denali->platform = INTEL_MRST;
1474 csr_base = pci_resource_start(dev, 0);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001475 csr_len = pci_resource_len(dev, 0);
Jason Robertsce082592010-05-13 15:57:33 +01001476 mem_base = pci_resource_start(dev, 1);
1477 mem_len = pci_resource_len(dev, 1);
1478 if (!mem_len) {
1479 mem_base = csr_base + csr_len;
1480 mem_len = csr_len;
Jason Robertsce082592010-05-13 15:57:33 +01001481 }
1482 }
1483
1484 /* Is 32-bit DMA supported? */
Jamie Iles84457942011-05-06 15:28:55 +01001485 ret = dma_set_mask(&dev->dev, DMA_BIT_MASK(32));
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001486 if (ret) {
Jason Robertsce082592010-05-13 15:57:33 +01001487 printk(KERN_ERR "Spectra: no usable DMA configuration\n");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001488 goto failed_enable_dev;
Jason Robertsce082592010-05-13 15:57:33 +01001489 }
Jamie Iles84457942011-05-06 15:28:55 +01001490 denali->buf.dma_buf = dma_map_single(&dev->dev, denali->buf.buf,
1491 DENALI_BUF_SIZE,
1492 DMA_BIDIRECTIONAL);
Jason Robertsce082592010-05-13 15:57:33 +01001493
Jamie Iles84457942011-05-06 15:28:55 +01001494 if (dma_mapping_error(&dev->dev, denali->buf.dma_buf)) {
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001495 dev_err(&dev->dev, "Spectra: failed to map DMA buffer\n");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001496 goto failed_enable_dev;
Jason Robertsce082592010-05-13 15:57:33 +01001497 }
1498
1499 pci_set_master(dev);
Jamie Iles84457942011-05-06 15:28:55 +01001500 denali->dev = &dev->dev;
Chuanxiao Dong5eab6aaa2010-08-12 10:07:18 +08001501 denali->mtd.dev.parent = &dev->dev;
Jason Robertsce082592010-05-13 15:57:33 +01001502
1503 ret = pci_request_regions(dev, DENALI_NAND_NAME);
1504 if (ret) {
1505 printk(KERN_ERR "Spectra: Unable to request memory regions\n");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001506 goto failed_dma_map;
Jason Robertsce082592010-05-13 15:57:33 +01001507 }
1508
1509 denali->flash_reg = ioremap_nocache(csr_base, csr_len);
1510 if (!denali->flash_reg) {
1511 printk(KERN_ERR "Spectra: Unable to remap memory region\n");
1512 ret = -ENOMEM;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001513 goto failed_req_regions;
Jason Robertsce082592010-05-13 15:57:33 +01001514 }
Jason Robertsce082592010-05-13 15:57:33 +01001515
1516 denali->flash_mem = ioremap_nocache(mem_base, mem_len);
1517 if (!denali->flash_mem) {
1518 printk(KERN_ERR "Spectra: ioremap_nocache failed!");
Jason Robertsce082592010-05-13 15:57:33 +01001519 ret = -ENOMEM;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001520 goto failed_remap_reg;
Jason Robertsce082592010-05-13 15:57:33 +01001521 }
1522
Jason Robertsce082592010-05-13 15:57:33 +01001523 denali_hw_init(denali);
1524 denali_drv_init(denali);
1525
Chuanxiao Dong5eab6aaa2010-08-12 10:07:18 +08001526 /* denali_isr register is done after all the hardware
1527 * initilization is finished*/
Jason Robertsce082592010-05-13 15:57:33 +01001528 if (request_irq(dev->irq, denali_isr, IRQF_SHARED,
1529 DENALI_NAND_NAME, denali)) {
1530 printk(KERN_ERR "Spectra: Unable to allocate IRQ\n");
1531 ret = -ENODEV;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001532 goto failed_remap_mem;
Jason Robertsce082592010-05-13 15:57:33 +01001533 }
1534
1535 /* now that our ISR is registered, we can enable interrupts */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +08001536 denali_set_intr_modes(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001537
1538 pci_set_drvdata(dev, denali);
1539
Chuanxiao Dong5eab6aaa2010-08-12 10:07:18 +08001540 denali->mtd.name = "denali-nand";
Jason Robertsce082592010-05-13 15:57:33 +01001541 denali->mtd.owner = THIS_MODULE;
1542 denali->mtd.priv = &denali->nand;
1543
1544 /* register the driver with the NAND core subsystem */
1545 denali->nand.select_chip = denali_select_chip;
1546 denali->nand.cmdfunc = denali_cmdfunc;
1547 denali->nand.read_byte = denali_read_byte;
1548 denali->nand.waitfunc = denali_waitfunc;
1549
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001550 /* scan for NAND devices attached to the controller
Jason Robertsce082592010-05-13 15:57:33 +01001551 * this is the first stage in a two step process to register
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001552 * with the nand subsystem */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001553 if (nand_scan_ident(&denali->mtd, LLD_MAX_FLASH_BANKS, NULL)) {
Jason Robertsce082592010-05-13 15:57:33 +01001554 ret = -ENXIO;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001555 goto failed_req_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001556 }
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001557
Chuanxiao.Dong66406522010-08-06 18:48:21 +08001558 /* MTD supported page sizes vary by kernel. We validate our
1559 * kernel supports the device here.
1560 */
1561 if (denali->mtd.writesize > NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE) {
1562 ret = -ENODEV;
1563 printk(KERN_ERR "Spectra: device size not supported by this "
1564 "version of MTD.");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001565 goto failed_req_irq;
Chuanxiao.Dong66406522010-08-06 18:48:21 +08001566 }
1567
Chuanxiao Dong08b9ab92010-08-06 18:19:09 +08001568 /* support for multi nand
1569 * MTD known nothing about multi nand,
1570 * so we should tell it the real pagesize
1571 * and anything necessery
1572 */
1573 denali->devnum = ioread32(denali->flash_reg + DEVICES_CONNECTED);
1574 denali->nand.chipsize <<= (denali->devnum - 1);
1575 denali->nand.page_shift += (denali->devnum - 1);
1576 denali->nand.pagemask = (denali->nand.chipsize >>
1577 denali->nand.page_shift) - 1;
1578 denali->nand.bbt_erase_shift += (denali->devnum - 1);
1579 denali->nand.phys_erase_shift = denali->nand.bbt_erase_shift;
1580 denali->nand.chip_shift += (denali->devnum - 1);
1581 denali->mtd.writesize <<= (denali->devnum - 1);
1582 denali->mtd.oobsize <<= (denali->devnum - 1);
1583 denali->mtd.erasesize <<= (denali->devnum - 1);
1584 denali->mtd.size = denali->nand.numchips * denali->nand.chipsize;
1585 denali->bbtskipbytes *= denali->devnum;
1586
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001587 /* second stage of the NAND scan
1588 * this stage requires information regarding ECC and
1589 * bad block management. */
Jason Robertsce082592010-05-13 15:57:33 +01001590
1591 /* Bad block management */
1592 denali->nand.bbt_td = &bbt_main_descr;
1593 denali->nand.bbt_md = &bbt_mirror_descr;
1594
1595 /* skip the scan for now until we have OOB read and write support */
1596 denali->nand.options |= NAND_USE_FLASH_BBT | NAND_SKIP_BBTSCAN;
1597 denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
1598
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001599 /* Denali Controller only support 15bit and 8bit ECC in MRST,
1600 * so just let controller do 15bit ECC for MLC and 8bit ECC for
1601 * SLC if possible.
1602 * */
1603 if (denali->nand.cellinfo & 0xc &&
1604 (denali->mtd.oobsize > (denali->bbtskipbytes +
1605 ECC_15BITS * (denali->mtd.writesize /
1606 ECC_SECTOR_SIZE)))) {
1607 /* if MLC OOB size is large enough, use 15bit ECC*/
1608 denali->nand.ecc.layout = &nand_15bit_oob;
1609 denali->nand.ecc.bytes = ECC_15BITS;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001610 iowrite32(15, denali->flash_reg + ECC_CORRECTION);
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001611 } else if (denali->mtd.oobsize < (denali->bbtskipbytes +
1612 ECC_8BITS * (denali->mtd.writesize /
1613 ECC_SECTOR_SIZE))) {
1614 printk(KERN_ERR "Your NAND chip OOB is not large enough to"
1615 " contain 8bit ECC correction codes");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001616 goto failed_req_irq;
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001617 } else {
1618 denali->nand.ecc.layout = &nand_8bit_oob;
1619 denali->nand.ecc.bytes = ECC_8BITS;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001620 iowrite32(8, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +01001621 }
1622
Chuanxiao Dong08b9ab92010-08-06 18:19:09 +08001623 denali->nand.ecc.bytes *= denali->devnum;
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001624 denali->nand.ecc.layout->eccbytes *=
1625 denali->mtd.writesize / ECC_SECTOR_SIZE;
1626 denali->nand.ecc.layout->oobfree[0].offset =
1627 denali->bbtskipbytes + denali->nand.ecc.layout->eccbytes;
1628 denali->nand.ecc.layout->oobfree[0].length =
1629 denali->mtd.oobsize - denali->nand.ecc.layout->eccbytes -
1630 denali->bbtskipbytes;
1631
Chuanxiao.Dong66406522010-08-06 18:48:21 +08001632 /* Let driver know the total blocks number and
1633 * how many blocks contained by each nand chip.
1634 * blksperchip will help driver to know how many
1635 * blocks is taken by FW.
1636 * */
1637 denali->totalblks = denali->mtd.size >>
1638 denali->nand.phys_erase_shift;
1639 denali->blksperchip = denali->totalblks / denali->nand.numchips;
1640
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001641 /* These functions are required by the NAND core framework, otherwise,
1642 * the NAND core will assert. However, we don't need them, so we'll stub
1643 * them out. */
Jason Robertsce082592010-05-13 15:57:33 +01001644 denali->nand.ecc.calculate = denali_ecc_calculate;
1645 denali->nand.ecc.correct = denali_ecc_correct;
1646 denali->nand.ecc.hwctl = denali_ecc_hwctl;
1647
1648 /* override the default read operations */
Chuanxiao Dong08b9ab92010-08-06 18:19:09 +08001649 denali->nand.ecc.size = ECC_SECTOR_SIZE * denali->devnum;
Jason Robertsce082592010-05-13 15:57:33 +01001650 denali->nand.ecc.read_page = denali_read_page;
1651 denali->nand.ecc.read_page_raw = denali_read_page_raw;
1652 denali->nand.ecc.write_page = denali_write_page;
1653 denali->nand.ecc.write_page_raw = denali_write_page_raw;
1654 denali->nand.ecc.read_oob = denali_read_oob;
1655 denali->nand.ecc.write_oob = denali_write_oob;
1656 denali->nand.erase_cmd = denali_erase;
1657
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001658 if (nand_scan_tail(&denali->mtd)) {
Jason Robertsce082592010-05-13 15:57:33 +01001659 ret = -ENXIO;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001660 goto failed_req_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001661 }
1662
1663 ret = add_mtd_device(&denali->mtd);
1664 if (ret) {
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001665 dev_err(&dev->dev, "Spectra: Failed to register MTD: %d\n",
1666 ret);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001667 goto failed_req_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001668 }
1669 return 0;
1670
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001671failed_req_irq:
Jason Robertsce082592010-05-13 15:57:33 +01001672 denali_irq_cleanup(dev->irq, denali);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001673failed_remap_mem:
Jason Robertsce082592010-05-13 15:57:33 +01001674 iounmap(denali->flash_mem);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001675failed_remap_reg:
1676 iounmap(denali->flash_reg);
1677failed_req_regions:
Jason Robertsce082592010-05-13 15:57:33 +01001678 pci_release_regions(dev);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001679failed_dma_map:
Jamie Iles84457942011-05-06 15:28:55 +01001680 dma_unmap_single(&dev->dev, denali->buf.dma_buf, DENALI_BUF_SIZE,
1681 DMA_BIDIRECTIONAL);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001682failed_enable_dev:
1683 pci_disable_device(dev);
1684failed_alloc_memery:
Jason Robertsce082592010-05-13 15:57:33 +01001685 kfree(denali);
1686 return ret;
1687}
1688
1689/* driver exit point */
1690static void denali_pci_remove(struct pci_dev *dev)
1691{
1692 struct denali_nand_info *denali = pci_get_drvdata(dev);
1693
Jason Robertsce082592010-05-13 15:57:33 +01001694 nand_release(&denali->mtd);
1695 del_mtd_device(&denali->mtd);
1696
1697 denali_irq_cleanup(dev->irq, denali);
1698
1699 iounmap(denali->flash_reg);
1700 iounmap(denali->flash_mem);
1701 pci_release_regions(dev);
1702 pci_disable_device(dev);
Jamie Iles84457942011-05-06 15:28:55 +01001703 dma_unmap_single(&dev->dev, denali->buf.dma_buf, DENALI_BUF_SIZE,
1704 DMA_BIDIRECTIONAL);
Jason Robertsce082592010-05-13 15:57:33 +01001705 pci_set_drvdata(dev, NULL);
1706 kfree(denali);
1707}
1708
1709MODULE_DEVICE_TABLE(pci, denali_pci_ids);
1710
1711static struct pci_driver denali_pci_driver = {
1712 .name = DENALI_NAND_NAME,
1713 .id_table = denali_pci_ids,
1714 .probe = denali_pci_probe,
1715 .remove = denali_pci_remove,
1716};
1717
1718static int __devinit denali_init(void)
1719{
Michal Marek9b5705a22011-04-05 16:59:03 +02001720 printk(KERN_INFO "Spectra MTD driver\n");
Jason Robertsce082592010-05-13 15:57:33 +01001721 return pci_register_driver(&denali_pci_driver);
1722}
1723
1724/* Free memory */
1725static void __devexit denali_exit(void)
1726{
1727 pci_unregister_driver(&denali_pci_driver);
1728}
1729
1730module_init(denali_init);
1731module_exit(denali_exit);