blob: 0b071a3136a2fe2ee2683f534a950ba740b6cd70 [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 */
Jason Robertsce082592010-05-13 15:57:33 +010019#include <linux/interrupt.h>
20#include <linux/delay.h>
Jamie Iles84457942011-05-06 15:28:55 +010021#include <linux/dma-mapping.h>
Jason Robertsce082592010-05-13 15:57:33 +010022#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/mtd/mtd.h>
26#include <linux/module.h>
27
28#include "denali.h"
29
30MODULE_LICENSE("GPL");
31
Chuanxiao5bac3ac2010-08-05 23:06:04 +080032/* We define a module parameter that allows the user to override
Jason Robertsce082592010-05-13 15:57:33 +010033 * the hardware and decide what timing mode should be used.
34 */
35#define NAND_DEFAULT_TIMINGS -1
36
37static int onfi_timing_mode = NAND_DEFAULT_TIMINGS;
38module_param(onfi_timing_mode, int, S_IRUGO);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +080039MODULE_PARM_DESC(onfi_timing_mode, "Overrides default ONFI setting."
40 " -1 indicates use default timings");
Jason Robertsce082592010-05-13 15:57:33 +010041
42#define DENALI_NAND_NAME "denali-nand"
43
44/* We define a macro here that combines all interrupts this driver uses into
45 * a single constant value, for convenience. */
Jamie Iles9589bf52011-05-06 15:28:56 +010046#define DENALI_IRQ_ALL (INTR_STATUS__DMA_CMD_COMP | \
47 INTR_STATUS__ECC_TRANSACTION_DONE | \
48 INTR_STATUS__ECC_ERR | \
49 INTR_STATUS__PROGRAM_FAIL | \
50 INTR_STATUS__LOAD_COMP | \
51 INTR_STATUS__PROGRAM_COMP | \
52 INTR_STATUS__TIME_OUT | \
53 INTR_STATUS__ERASE_FAIL | \
54 INTR_STATUS__RST_COMP | \
55 INTR_STATUS__ERASE_COMP)
Jason Robertsce082592010-05-13 15:57:33 +010056
Chuanxiao5bac3ac2010-08-05 23:06:04 +080057/* indicates whether or not the internal value for the flash bank is
Chuanxiao Dongb292c342010-08-11 17:46:00 +080058 * valid or not */
Chuanxiao5bac3ac2010-08-05 23:06:04 +080059#define CHIP_SELECT_INVALID -1
Jason Robertsce082592010-05-13 15:57:33 +010060
61#define SUPPORT_8BITECC 1
62
Chuanxiao5bac3ac2010-08-05 23:06:04 +080063/* This macro divides two integers and rounds fractional values up
Jason Robertsce082592010-05-13 15:57:33 +010064 * to the nearest integer value. */
65#define CEIL_DIV(X, Y) (((X)%(Y)) ? ((X)/(Y)+1) : ((X)/(Y)))
66
67/* this macro allows us to convert from an MTD structure to our own
68 * device context (denali) structure.
69 */
70#define mtd_to_denali(m) container_of(m, struct denali_nand_info, mtd)
71
72/* These constants are defined by the driver to enable common driver
Chuanxiao Dongb292c342010-08-11 17:46:00 +080073 * configuration options. */
Jason Robertsce082592010-05-13 15:57:33 +010074#define SPARE_ACCESS 0x41
75#define MAIN_ACCESS 0x42
76#define MAIN_SPARE_ACCESS 0x43
77
78#define DENALI_READ 0
79#define DENALI_WRITE 0x100
80
81/* types of device accesses. We can issue commands and get status */
82#define COMMAND_CYCLE 0
83#define ADDR_CYCLE 1
84#define STATUS_CYCLE 2
85
Chuanxiao5bac3ac2010-08-05 23:06:04 +080086/* this is a helper macro that allows us to
Jason Robertsce082592010-05-13 15:57:33 +010087 * format the bank into the proper bits for the controller */
88#define BANK(x) ((x) << 24)
89
Jason Robertsce082592010-05-13 15:57:33 +010090/* forward declarations */
91static void clear_interrupts(struct denali_nand_info *denali);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +080092static uint32_t wait_for_irq(struct denali_nand_info *denali,
93 uint32_t irq_mask);
94static void denali_irq_enable(struct denali_nand_info *denali,
95 uint32_t int_mask);
Jason Robertsce082592010-05-13 15:57:33 +010096static uint32_t read_interrupt_status(struct denali_nand_info *denali);
97
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +080098/* Certain operations for the denali NAND controller use
99 * an indexed mode to read/write data. The operation is
100 * performed by writing the address value of the command
101 * to the device memory followed by the data. This function
102 * abstracts this common operation.
Jason Robertsce082592010-05-13 15:57:33 +0100103*/
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800104static void index_addr(struct denali_nand_info *denali,
105 uint32_t address, uint32_t data)
Jason Robertsce082592010-05-13 15:57:33 +0100106{
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800107 iowrite32(address, denali->flash_mem);
108 iowrite32(data, denali->flash_mem + 0x10);
Jason Robertsce082592010-05-13 15:57:33 +0100109}
110
111/* Perform an indexed read of the device */
112static void index_addr_read_data(struct denali_nand_info *denali,
113 uint32_t address, uint32_t *pdata)
114{
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800115 iowrite32(address, denali->flash_mem);
Jason Robertsce082592010-05-13 15:57:33 +0100116 *pdata = ioread32(denali->flash_mem + 0x10);
117}
118
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800119/* We need to buffer some data for some of the NAND core routines.
Jason Robertsce082592010-05-13 15:57:33 +0100120 * The operations manage buffering that data. */
121static void reset_buf(struct denali_nand_info *denali)
122{
123 denali->buf.head = denali->buf.tail = 0;
124}
125
126static void write_byte_to_buf(struct denali_nand_info *denali, uint8_t byte)
127{
Jason Robertsce082592010-05-13 15:57:33 +0100128 denali->buf.buf[denali->buf.tail++] = byte;
129}
130
131/* reads the status of the device */
132static void read_status(struct denali_nand_info *denali)
133{
134 uint32_t cmd = 0x0;
135
136 /* initialize the data buffer to store status */
137 reset_buf(denali);
138
Chuanxiao Dongf0bc0c72010-08-11 17:14:59 +0800139 cmd = ioread32(denali->flash_reg + WRITE_PROTECT);
140 if (cmd)
141 write_byte_to_buf(denali, NAND_STATUS_WP);
142 else
143 write_byte_to_buf(denali, 0);
Jason Robertsce082592010-05-13 15:57:33 +0100144}
145
146/* resets a specific device connected to the core */
147static void reset_bank(struct denali_nand_info *denali)
148{
149 uint32_t irq_status = 0;
Jamie Iles9589bf52011-05-06 15:28:56 +0100150 uint32_t irq_mask = INTR_STATUS__RST_COMP |
151 INTR_STATUS__TIME_OUT;
Jason Robertsce082592010-05-13 15:57:33 +0100152
153 clear_interrupts(denali);
154
Jamie Iles9589bf52011-05-06 15:28:56 +0100155 iowrite32(1 << denali->flash_bank, denali->flash_reg + DEVICE_RESET);
Jason Robertsce082592010-05-13 15:57:33 +0100156
157 irq_status = wait_for_irq(denali, irq_mask);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800158
Jamie Iles9589bf52011-05-06 15:28:56 +0100159 if (irq_status & INTR_STATUS__TIME_OUT)
Jamie Iles84457942011-05-06 15:28:55 +0100160 dev_err(denali->dev, "reset bank failed.\n");
Jason Robertsce082592010-05-13 15:57:33 +0100161}
162
163/* Reset the flash controller */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800164static uint16_t denali_nand_reset(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +0100165{
166 uint32_t i;
167
Jamie Iles84457942011-05-06 15:28:55 +0100168 dev_dbg(denali->dev, "%s, Line %d, Function: %s\n",
Jason Robertsce082592010-05-13 15:57:33 +0100169 __FILE__, __LINE__, __func__);
170
Jamie Ilesc89eeda2011-05-06 15:28:57 +0100171 for (i = 0 ; i < denali->max_banks; i++)
Jamie Iles9589bf52011-05-06 15:28:56 +0100172 iowrite32(INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT,
173 denali->flash_reg + INTR_STATUS(i));
Jason Robertsce082592010-05-13 15:57:33 +0100174
Jamie Ilesc89eeda2011-05-06 15:28:57 +0100175 for (i = 0 ; i < denali->max_banks; i++) {
Jamie Iles9589bf52011-05-06 15:28:56 +0100176 iowrite32(1 << i, denali->flash_reg + DEVICE_RESET);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800177 while (!(ioread32(denali->flash_reg +
Jamie Iles9589bf52011-05-06 15:28:56 +0100178 INTR_STATUS(i)) &
179 (INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT)))
Chuanxiao Dong628bfd412010-08-11 17:53:29 +0800180 cpu_relax();
Jamie Iles9589bf52011-05-06 15:28:56 +0100181 if (ioread32(denali->flash_reg + INTR_STATUS(i)) &
182 INTR_STATUS__TIME_OUT)
Jamie Iles84457942011-05-06 15:28:55 +0100183 dev_dbg(denali->dev,
Jason Robertsce082592010-05-13 15:57:33 +0100184 "NAND Reset operation timed out on bank %d\n", i);
185 }
186
Jamie Ilesc89eeda2011-05-06 15:28:57 +0100187 for (i = 0; i < denali->max_banks; i++)
Jamie Iles9589bf52011-05-06 15:28:56 +0100188 iowrite32(INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT,
189 denali->flash_reg + INTR_STATUS(i));
Jason Robertsce082592010-05-13 15:57:33 +0100190
191 return PASS;
192}
193
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800194/* this routine calculates the ONFI timing values for a given mode and
195 * programs the clocking register accordingly. The mode is determined by
196 * the get_onfi_nand_para routine.
Jason Robertsce082592010-05-13 15:57:33 +0100197 */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800198static void nand_onfi_timing_set(struct denali_nand_info *denali,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800199 uint16_t mode)
Jason Robertsce082592010-05-13 15:57:33 +0100200{
201 uint16_t Trea[6] = {40, 30, 25, 20, 20, 16};
202 uint16_t Trp[6] = {50, 25, 17, 15, 12, 10};
203 uint16_t Treh[6] = {30, 15, 15, 10, 10, 7};
204 uint16_t Trc[6] = {100, 50, 35, 30, 25, 20};
205 uint16_t Trhoh[6] = {0, 15, 15, 15, 15, 15};
206 uint16_t Trloh[6] = {0, 0, 0, 0, 5, 5};
207 uint16_t Tcea[6] = {100, 45, 30, 25, 25, 25};
208 uint16_t Tadl[6] = {200, 100, 100, 100, 70, 70};
209 uint16_t Trhw[6] = {200, 100, 100, 100, 100, 100};
210 uint16_t Trhz[6] = {200, 100, 100, 100, 100, 100};
211 uint16_t Twhr[6] = {120, 80, 80, 60, 60, 60};
212 uint16_t Tcs[6] = {70, 35, 25, 25, 20, 15};
213
214 uint16_t TclsRising = 1;
215 uint16_t data_invalid_rhoh, data_invalid_rloh, data_invalid;
216 uint16_t dv_window = 0;
217 uint16_t en_lo, en_hi;
218 uint16_t acc_clks;
219 uint16_t addr_2_data, re_2_we, re_2_re, we_2_re, cs_cnt;
220
Jamie Iles84457942011-05-06 15:28:55 +0100221 dev_dbg(denali->dev, "%s, Line %d, Function: %s\n",
Jason Robertsce082592010-05-13 15:57:33 +0100222 __FILE__, __LINE__, __func__);
223
224 en_lo = CEIL_DIV(Trp[mode], CLK_X);
225 en_hi = CEIL_DIV(Treh[mode], CLK_X);
226#if ONFI_BLOOM_TIME
227 if ((en_hi * CLK_X) < (Treh[mode] + 2))
228 en_hi++;
229#endif
230
231 if ((en_lo + en_hi) * CLK_X < Trc[mode])
232 en_lo += CEIL_DIV((Trc[mode] - (en_lo + en_hi) * CLK_X), CLK_X);
233
234 if ((en_lo + en_hi) < CLK_MULTI)
235 en_lo += CLK_MULTI - en_lo - en_hi;
236
237 while (dv_window < 8) {
238 data_invalid_rhoh = en_lo * CLK_X + Trhoh[mode];
239
240 data_invalid_rloh = (en_lo + en_hi) * CLK_X + Trloh[mode];
241
242 data_invalid =
243 data_invalid_rhoh <
244 data_invalid_rloh ? data_invalid_rhoh : data_invalid_rloh;
245
246 dv_window = data_invalid - Trea[mode];
247
248 if (dv_window < 8)
249 en_lo++;
250 }
251
252 acc_clks = CEIL_DIV(Trea[mode], CLK_X);
253
254 while (((acc_clks * CLK_X) - Trea[mode]) < 3)
255 acc_clks++;
256
257 if ((data_invalid - acc_clks * CLK_X) < 2)
Jamie Iles84457942011-05-06 15:28:55 +0100258 dev_warn(denali->dev, "%s, Line %d: Warning!\n",
Jason Robertsce082592010-05-13 15:57:33 +0100259 __FILE__, __LINE__);
260
261 addr_2_data = CEIL_DIV(Tadl[mode], CLK_X);
262 re_2_we = CEIL_DIV(Trhw[mode], CLK_X);
263 re_2_re = CEIL_DIV(Trhz[mode], CLK_X);
264 we_2_re = CEIL_DIV(Twhr[mode], CLK_X);
265 cs_cnt = CEIL_DIV((Tcs[mode] - Trp[mode]), CLK_X);
266 if (!TclsRising)
267 cs_cnt = CEIL_DIV(Tcs[mode], CLK_X);
268 if (cs_cnt == 0)
269 cs_cnt = 1;
270
271 if (Tcea[mode]) {
272 while (((cs_cnt * CLK_X) + Trea[mode]) < Tcea[mode])
273 cs_cnt++;
274 }
275
276#if MODE5_WORKAROUND
277 if (mode == 5)
278 acc_clks = 5;
279#endif
280
281 /* Sighting 3462430: Temporary hack for MT29F128G08CJABAWP:B */
282 if ((ioread32(denali->flash_reg + MANUFACTURER_ID) == 0) &&
283 (ioread32(denali->flash_reg + DEVICE_ID) == 0x88))
284 acc_clks = 6;
285
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800286 iowrite32(acc_clks, denali->flash_reg + ACC_CLKS);
287 iowrite32(re_2_we, denali->flash_reg + RE_2_WE);
288 iowrite32(re_2_re, denali->flash_reg + RE_2_RE);
289 iowrite32(we_2_re, denali->flash_reg + WE_2_RE);
290 iowrite32(addr_2_data, denali->flash_reg + ADDR_2_DATA);
291 iowrite32(en_lo, denali->flash_reg + RDWR_EN_LO_CNT);
292 iowrite32(en_hi, denali->flash_reg + RDWR_EN_HI_CNT);
293 iowrite32(cs_cnt, denali->flash_reg + CS_SETUP_CNT);
Jason Robertsce082592010-05-13 15:57:33 +0100294}
295
Jason Robertsce082592010-05-13 15:57:33 +0100296/* queries the NAND device to see what ONFI modes it supports. */
297static uint16_t get_onfi_nand_para(struct denali_nand_info *denali)
298{
299 int i;
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800300 /* we needn't to do a reset here because driver has already
301 * reset all the banks before
302 * */
Jason Robertsce082592010-05-13 15:57:33 +0100303 if (!(ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
304 ONFI_TIMING_MODE__VALUE))
305 return FAIL;
306
307 for (i = 5; i > 0; i--) {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800308 if (ioread32(denali->flash_reg + ONFI_TIMING_MODE) &
309 (0x01 << i))
Jason Robertsce082592010-05-13 15:57:33 +0100310 break;
311 }
312
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800313 nand_onfi_timing_set(denali, i);
Jason Robertsce082592010-05-13 15:57:33 +0100314
315 /* By now, all the ONFI devices we know support the page cache */
316 /* rw feature. So here we enable the pipeline_rw_ahead feature */
317 /* iowrite32(1, denali->flash_reg + CACHE_WRITE_ENABLE); */
318 /* iowrite32(1, denali->flash_reg + CACHE_READ_ENABLE); */
319
320 return PASS;
321}
322
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800323static void get_samsung_nand_para(struct denali_nand_info *denali,
324 uint8_t device_id)
Jason Robertsce082592010-05-13 15:57:33 +0100325{
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800326 if (device_id == 0xd3) { /* Samsung K9WAG08U1A */
Jason Robertsce082592010-05-13 15:57:33 +0100327 /* Set timing register values according to datasheet */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800328 iowrite32(5, denali->flash_reg + ACC_CLKS);
329 iowrite32(20, denali->flash_reg + RE_2_WE);
330 iowrite32(12, denali->flash_reg + WE_2_RE);
331 iowrite32(14, denali->flash_reg + ADDR_2_DATA);
332 iowrite32(3, denali->flash_reg + RDWR_EN_LO_CNT);
333 iowrite32(2, denali->flash_reg + RDWR_EN_HI_CNT);
334 iowrite32(2, denali->flash_reg + CS_SETUP_CNT);
Jason Robertsce082592010-05-13 15:57:33 +0100335 }
Jason Robertsce082592010-05-13 15:57:33 +0100336}
337
338static void get_toshiba_nand_para(struct denali_nand_info *denali)
339{
Jason Robertsce082592010-05-13 15:57:33 +0100340 uint32_t tmp;
341
342 /* Workaround to fix a controller bug which reports a wrong */
343 /* spare area size for some kind of Toshiba NAND device */
344 if ((ioread32(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) == 4096) &&
345 (ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) == 64)) {
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800346 iowrite32(216, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
Jason Robertsce082592010-05-13 15:57:33 +0100347 tmp = ioread32(denali->flash_reg + DEVICES_CONNECTED) *
348 ioread32(denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800349 iowrite32(tmp,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800350 denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
Jason Robertsce082592010-05-13 15:57:33 +0100351#if SUPPORT_15BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800352 iowrite32(15, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100353#elif SUPPORT_8BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800354 iowrite32(8, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100355#endif
356 }
Jason Robertsce082592010-05-13 15:57:33 +0100357}
358
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800359static void get_hynix_nand_para(struct denali_nand_info *denali,
360 uint8_t device_id)
Jason Robertsce082592010-05-13 15:57:33 +0100361{
Jason Robertsce082592010-05-13 15:57:33 +0100362 uint32_t main_size, spare_size;
363
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800364 switch (device_id) {
Jason Robertsce082592010-05-13 15:57:33 +0100365 case 0xD5: /* Hynix H27UAG8T2A, H27UBG8U5A or H27UCG8VFA */
366 case 0xD7: /* Hynix H27UDG8VEM, H27UCG8UDM or H27UCG8V5A */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800367 iowrite32(128, denali->flash_reg + PAGES_PER_BLOCK);
368 iowrite32(4096, denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
369 iowrite32(224, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800370 main_size = 4096 *
371 ioread32(denali->flash_reg + DEVICES_CONNECTED);
372 spare_size = 224 *
373 ioread32(denali->flash_reg + DEVICES_CONNECTED);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800374 iowrite32(main_size,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800375 denali->flash_reg + LOGICAL_PAGE_DATA_SIZE);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800376 iowrite32(spare_size,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800377 denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800378 iowrite32(0, denali->flash_reg + DEVICE_WIDTH);
Jason Robertsce082592010-05-13 15:57:33 +0100379#if SUPPORT_15BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800380 iowrite32(15, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100381#elif SUPPORT_8BITECC
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800382 iowrite32(8, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +0100383#endif
Jason Robertsce082592010-05-13 15:57:33 +0100384 break;
385 default:
Jamie Iles84457942011-05-06 15:28:55 +0100386 dev_warn(denali->dev,
Jason Robertsce082592010-05-13 15:57:33 +0100387 "Spectra: Unknown Hynix NAND (Device ID: 0x%x)."
388 "Will use default parameter values instead.\n",
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800389 device_id);
Jason Robertsce082592010-05-13 15:57:33 +0100390 }
391}
392
393/* determines how many NAND chips are connected to the controller. Note for
Chuanxiao Dongb292c342010-08-11 17:46:00 +0800394 * Intel CE4100 devices we don't support more than one device.
Jason Robertsce082592010-05-13 15:57:33 +0100395 */
396static void find_valid_banks(struct denali_nand_info *denali)
397{
Jamie Ilesc89eeda2011-05-06 15:28:57 +0100398 uint32_t id[denali->max_banks];
Jason Robertsce082592010-05-13 15:57:33 +0100399 int i;
400
401 denali->total_used_banks = 1;
Jamie Ilesc89eeda2011-05-06 15:28:57 +0100402 for (i = 0; i < denali->max_banks; i++) {
Jason Robertsce082592010-05-13 15:57:33 +0100403 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 0), 0x90);
404 index_addr(denali, (uint32_t)(MODE_11 | (i << 24) | 1), 0);
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800405 index_addr_read_data(denali,
406 (uint32_t)(MODE_11 | (i << 24) | 2), &id[i]);
Jason Robertsce082592010-05-13 15:57:33 +0100407
Jamie Iles84457942011-05-06 15:28:55 +0100408 dev_dbg(denali->dev,
Jason Robertsce082592010-05-13 15:57:33 +0100409 "Return 1st ID for bank[%d]: %x\n", i, id[i]);
410
411 if (i == 0) {
412 if (!(id[i] & 0x0ff))
413 break; /* WTF? */
414 } else {
415 if ((id[i] & 0x0ff) == (id[0] & 0x0ff))
416 denali->total_used_banks++;
417 else
418 break;
419 }
420 }
421
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800422 if (denali->platform == INTEL_CE4100) {
Jason Robertsce082592010-05-13 15:57:33 +0100423 /* Platform limitations of the CE4100 device limit
424 * users to a single chip solution for NAND.
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800425 * Multichip support is not enabled.
426 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800427 if (denali->total_used_banks != 1) {
Jamie Iles84457942011-05-06 15:28:55 +0100428 dev_err(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800429 "Sorry, Intel CE4100 only supports "
Jason Robertsce082592010-05-13 15:57:33 +0100430 "a single NAND device.\n");
431 BUG();
432 }
433 }
Jamie Iles84457942011-05-06 15:28:55 +0100434 dev_dbg(denali->dev,
Jason Robertsce082592010-05-13 15:57:33 +0100435 "denali->total_used_banks: %d\n", denali->total_used_banks);
436}
437
Jamie Ilesc89eeda2011-05-06 15:28:57 +0100438/*
439 * Use the configuration feature register to determine the maximum number of
440 * banks that the hardware supports.
441 */
442static void detect_max_banks(struct denali_nand_info *denali)
443{
444 uint32_t features = ioread32(denali->flash_reg + FEATURES);
445
446 denali->max_banks = 2 << (features & FEATURES__N_BANKS);
447}
448
Jason Robertsce082592010-05-13 15:57:33 +0100449static void detect_partition_feature(struct denali_nand_info *denali)
450{
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800451 /* For MRST platform, denali->fwblks represent the
452 * number of blocks firmware is taken,
453 * FW is in protect partition and MTD driver has no
454 * permission to access it. So let driver know how many
455 * blocks it can't touch.
456 * */
Jason Robertsce082592010-05-13 15:57:33 +0100457 if (ioread32(denali->flash_reg + FEATURES) & FEATURES__PARTITION) {
Jamie Iles9589bf52011-05-06 15:28:56 +0100458 if ((ioread32(denali->flash_reg + PERM_SRC_ID(1)) &
459 PERM_SRC_ID__SRCID) == SPECTRA_PARTITION_ID) {
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800460 denali->fwblks =
Jamie Iles9589bf52011-05-06 15:28:56 +0100461 ((ioread32(denali->flash_reg + MIN_MAX_BANK(1)) &
462 MIN_MAX_BANK__MIN_VALUE) *
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800463 denali->blksperchip)
Jason Robertsce082592010-05-13 15:57:33 +0100464 +
Jamie Iles9589bf52011-05-06 15:28:56 +0100465 (ioread32(denali->flash_reg + MIN_BLK_ADDR(1)) &
466 MIN_BLK_ADDR__VALUE);
Chuanxiao.Dong66406522010-08-06 18:48:21 +0800467 } else
468 denali->fwblks = SPECTRA_START_BLOCK;
469 } else
470 denali->fwblks = SPECTRA_START_BLOCK;
Jason Robertsce082592010-05-13 15:57:33 +0100471}
472
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800473static uint16_t denali_nand_timing_set(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +0100474{
475 uint16_t status = PASS;
grmoore@altera.comd68a5c32014-06-23 14:21:10 -0500476 uint32_t id_bytes[8], addr;
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800477 uint8_t i, maf_id, device_id;
Jason Robertsce082592010-05-13 15:57:33 +0100478
Jamie Iles84457942011-05-06 15:28:55 +0100479 dev_dbg(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800480 "%s, Line %d, Function: %s\n",
481 __FILE__, __LINE__, __func__);
Jason Robertsce082592010-05-13 15:57:33 +0100482
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800483 /* Use read id method to get device ID and other
484 * params. For some NAND chips, controller can't
485 * report the correct device ID by reading from
486 * DEVICE_ID register
487 * */
488 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
489 index_addr(denali, (uint32_t)addr | 0, 0x90);
490 index_addr(denali, (uint32_t)addr | 1, 0);
grmoore@altera.comd68a5c32014-06-23 14:21:10 -0500491 for (i = 0; i < 8; i++)
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800492 index_addr_read_data(denali, addr | 2, &id_bytes[i]);
493 maf_id = id_bytes[0];
494 device_id = id_bytes[1];
Jason Robertsce082592010-05-13 15:57:33 +0100495
496 if (ioread32(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) &
497 ONFI_DEVICE_NO_OF_LUNS__ONFI_DEVICE) { /* ONFI 1.0 NAND */
498 if (FAIL == get_onfi_nand_para(denali))
499 return FAIL;
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800500 } else if (maf_id == 0xEC) { /* Samsung NAND */
Chuanxiao Dong4c03bbd2010-08-06 15:45:19 +0800501 get_samsung_nand_para(denali, device_id);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800502 } else if (maf_id == 0x98) { /* Toshiba NAND */
Jason Robertsce082592010-05-13 15:57:33 +0100503 get_toshiba_nand_para(denali);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +0800504 } else if (maf_id == 0xAD) { /* Hynix NAND */
505 get_hynix_nand_para(denali, device_id);
Jason Robertsce082592010-05-13 15:57:33 +0100506 }
507
Jamie Iles84457942011-05-06 15:28:55 +0100508 dev_info(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800509 "Dump timing register values:"
510 "acc_clks: %d, re_2_we: %d, re_2_re: %d\n"
511 "we_2_re: %d, addr_2_data: %d, rdwr_en_lo_cnt: %d\n"
Jason Robertsce082592010-05-13 15:57:33 +0100512 "rdwr_en_hi_cnt: %d, cs_setup_cnt: %d\n",
513 ioread32(denali->flash_reg + ACC_CLKS),
514 ioread32(denali->flash_reg + RE_2_WE),
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800515 ioread32(denali->flash_reg + RE_2_RE),
Jason Robertsce082592010-05-13 15:57:33 +0100516 ioread32(denali->flash_reg + WE_2_RE),
517 ioread32(denali->flash_reg + ADDR_2_DATA),
518 ioread32(denali->flash_reg + RDWR_EN_LO_CNT),
519 ioread32(denali->flash_reg + RDWR_EN_HI_CNT),
520 ioread32(denali->flash_reg + CS_SETUP_CNT));
521
Jason Robertsce082592010-05-13 15:57:33 +0100522 find_valid_banks(denali);
523
524 detect_partition_feature(denali);
525
Jason Robertsce082592010-05-13 15:57:33 +0100526 /* If the user specified to override the default timings
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800527 * with a specific ONFI mode, we apply those changes here.
Jason Robertsce082592010-05-13 15:57:33 +0100528 */
529 if (onfi_timing_mode != NAND_DEFAULT_TIMINGS)
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800530 nand_onfi_timing_set(denali, onfi_timing_mode);
Jason Robertsce082592010-05-13 15:57:33 +0100531
532 return status;
533}
534
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800535static void denali_set_intr_modes(struct denali_nand_info *denali,
Jason Robertsce082592010-05-13 15:57:33 +0100536 uint16_t INT_ENABLE)
537{
Jamie Iles84457942011-05-06 15:28:55 +0100538 dev_dbg(denali->dev, "%s, Line %d, Function: %s\n",
Jason Robertsce082592010-05-13 15:57:33 +0100539 __FILE__, __LINE__, __func__);
540
541 if (INT_ENABLE)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800542 iowrite32(1, denali->flash_reg + GLOBAL_INT_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +0100543 else
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800544 iowrite32(0, denali->flash_reg + GLOBAL_INT_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +0100545}
546
547/* validation function to verify that the controlling software is making
Chuanxiao Dongb292c342010-08-11 17:46:00 +0800548 * a valid request
Jason Robertsce082592010-05-13 15:57:33 +0100549 */
550static inline bool is_flash_bank_valid(int flash_bank)
551{
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800552 return (flash_bank >= 0 && flash_bank < 4);
Jason Robertsce082592010-05-13 15:57:33 +0100553}
554
555static void denali_irq_init(struct denali_nand_info *denali)
556{
557 uint32_t int_mask = 0;
Jamie Iles9589bf52011-05-06 15:28:56 +0100558 int i;
Jason Robertsce082592010-05-13 15:57:33 +0100559
560 /* Disable global interrupts */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800561 denali_set_intr_modes(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +0100562
563 int_mask = DENALI_IRQ_ALL;
564
565 /* Clear all status bits */
Jamie Ilesc89eeda2011-05-06 15:28:57 +0100566 for (i = 0; i < denali->max_banks; ++i)
Jamie Iles9589bf52011-05-06 15:28:56 +0100567 iowrite32(0xFFFF, denali->flash_reg + INTR_STATUS(i));
Jason Robertsce082592010-05-13 15:57:33 +0100568
569 denali_irq_enable(denali, int_mask);
570}
571
572static void denali_irq_cleanup(int irqnum, struct denali_nand_info *denali)
573{
Chuanxiao Dongeda936e2010-07-27 14:17:37 +0800574 denali_set_intr_modes(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +0100575 free_irq(irqnum, denali);
576}
577
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800578static void denali_irq_enable(struct denali_nand_info *denali,
579 uint32_t int_mask)
Jason Robertsce082592010-05-13 15:57:33 +0100580{
Jamie Iles9589bf52011-05-06 15:28:56 +0100581 int i;
582
Jamie Ilesc89eeda2011-05-06 15:28:57 +0100583 for (i = 0; i < denali->max_banks; ++i)
Jamie Iles9589bf52011-05-06 15:28:56 +0100584 iowrite32(int_mask, denali->flash_reg + INTR_EN(i));
Jason Robertsce082592010-05-13 15:57:33 +0100585}
586
587/* This function only returns when an interrupt that this driver cares about
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800588 * occurs. This is to reduce the overhead of servicing interrupts
Jason Robertsce082592010-05-13 15:57:33 +0100589 */
590static inline uint32_t denali_irq_detected(struct denali_nand_info *denali)
591{
Chuanxiao Donga99d1792010-07-27 11:32:21 +0800592 return read_interrupt_status(denali) & DENALI_IRQ_ALL;
Jason Robertsce082592010-05-13 15:57:33 +0100593}
594
595/* Interrupts are cleared by writing a 1 to the appropriate status bit */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800596static inline void clear_interrupt(struct denali_nand_info *denali,
597 uint32_t irq_mask)
Jason Robertsce082592010-05-13 15:57:33 +0100598{
599 uint32_t intr_status_reg = 0;
600
Jamie Iles9589bf52011-05-06 15:28:56 +0100601 intr_status_reg = INTR_STATUS(denali->flash_bank);
Jason Robertsce082592010-05-13 15:57:33 +0100602
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800603 iowrite32(irq_mask, denali->flash_reg + intr_status_reg);
Jason Robertsce082592010-05-13 15:57:33 +0100604}
605
606static void clear_interrupts(struct denali_nand_info *denali)
607{
608 uint32_t status = 0x0;
609 spin_lock_irq(&denali->irq_lock);
610
611 status = read_interrupt_status(denali);
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800612 clear_interrupt(denali, status);
Jason Robertsce082592010-05-13 15:57:33 +0100613
Jason Robertsce082592010-05-13 15:57:33 +0100614 denali->irq_status = 0x0;
615 spin_unlock_irq(&denali->irq_lock);
616}
617
618static uint32_t read_interrupt_status(struct denali_nand_info *denali)
619{
620 uint32_t intr_status_reg = 0;
621
Jamie Iles9589bf52011-05-06 15:28:56 +0100622 intr_status_reg = INTR_STATUS(denali->flash_bank);
Jason Robertsce082592010-05-13 15:57:33 +0100623
624 return ioread32(denali->flash_reg + intr_status_reg);
625}
626
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800627/* This is the interrupt service routine. It handles all interrupts
628 * sent to this device. Note that on CE4100, this is a shared
629 * interrupt.
Jason Robertsce082592010-05-13 15:57:33 +0100630 */
631static irqreturn_t denali_isr(int irq, void *dev_id)
632{
633 struct denali_nand_info *denali = dev_id;
634 uint32_t irq_status = 0x0;
635 irqreturn_t result = IRQ_NONE;
636
637 spin_lock(&denali->irq_lock);
638
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800639 /* check to see if a valid NAND chip has
640 * been selected.
Jason Robertsce082592010-05-13 15:57:33 +0100641 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800642 if (is_flash_bank_valid(denali->flash_bank)) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800643 /* check to see if controller generated
Jason Robertsce082592010-05-13 15:57:33 +0100644 * the interrupt, since this is a shared interrupt */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800645 irq_status = denali_irq_detected(denali);
646 if (irq_status != 0) {
Jason Robertsce082592010-05-13 15:57:33 +0100647 /* handle interrupt */
648 /* first acknowledge it */
649 clear_interrupt(denali, irq_status);
650 /* store the status in the device context for someone
651 to read */
652 denali->irq_status |= irq_status;
653 /* notify anyone who cares that it happened */
654 complete(&denali->complete);
655 /* tell the OS that we've handled this */
656 result = IRQ_HANDLED;
657 }
658 }
659 spin_unlock(&denali->irq_lock);
660 return result;
661}
662#define BANK(x) ((x) << 24)
663
664static uint32_t wait_for_irq(struct denali_nand_info *denali, uint32_t irq_mask)
665{
666 unsigned long comp_res = 0;
667 uint32_t intr_status = 0;
668 bool retry = false;
669 unsigned long timeout = msecs_to_jiffies(1000);
670
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800671 do {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800672 comp_res =
673 wait_for_completion_timeout(&denali->complete, timeout);
Jason Robertsce082592010-05-13 15:57:33 +0100674 spin_lock_irq(&denali->irq_lock);
675 intr_status = denali->irq_status;
676
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800677 if (intr_status & irq_mask) {
Jason Robertsce082592010-05-13 15:57:33 +0100678 denali->irq_status &= ~irq_mask;
679 spin_unlock_irq(&denali->irq_lock);
Jason Robertsce082592010-05-13 15:57:33 +0100680 /* our interrupt was detected */
681 break;
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800682 } else {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800683 /* these are not the interrupts you are looking for -
684 * need to wait again */
Jason Robertsce082592010-05-13 15:57:33 +0100685 spin_unlock_irq(&denali->irq_lock);
Jason Robertsce082592010-05-13 15:57:33 +0100686 retry = true;
687 }
688 } while (comp_res != 0);
689
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800690 if (comp_res == 0) {
Jason Robertsce082592010-05-13 15:57:33 +0100691 /* timeout */
Dinh Nguyen2a0a2882012-09-27 10:58:05 -0600692 pr_err("timeout occurred, status = 0x%x, mask = 0x%x\n",
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800693 intr_status, irq_mask);
Jason Robertsce082592010-05-13 15:57:33 +0100694
695 intr_status = 0;
696 }
697 return intr_status;
698}
699
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800700/* This helper function setups the registers for ECC and whether or not
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300701 * the spare area will be transferred. */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800702static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en,
Jason Robertsce082592010-05-13 15:57:33 +0100703 bool transfer_spare)
704{
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800705 int ecc_en_flag = 0, transfer_spare_flag = 0;
Jason Robertsce082592010-05-13 15:57:33 +0100706
707 /* set ECC, transfer spare bits if needed */
708 ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0;
709 transfer_spare_flag = transfer_spare ? TRANSFER_SPARE_REG__FLAG : 0;
710
711 /* Enable spare area/ECC per user's request. */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800712 iowrite32(ecc_en_flag, denali->flash_reg + ECC_ENABLE);
713 iowrite32(transfer_spare_flag,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800714 denali->flash_reg + TRANSFER_SPARE_REG);
Jason Robertsce082592010-05-13 15:57:33 +0100715}
716
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800717/* sends a pipeline command operation to the controller. See the Denali NAND
Chuanxiao Dongb292c342010-08-11 17:46:00 +0800718 * controller's user guide for more information (section 4.2.3.6).
Jason Robertsce082592010-05-13 15:57:33 +0100719 */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800720static int denali_send_pipeline_cmd(struct denali_nand_info *denali,
721 bool ecc_en,
722 bool transfer_spare,
723 int access_type,
724 int op)
Jason Robertsce082592010-05-13 15:57:33 +0100725{
726 int status = PASS;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800727 uint32_t addr = 0x0, cmd = 0x0, page_count = 1, irq_status = 0,
Jason Robertsce082592010-05-13 15:57:33 +0100728 irq_mask = 0;
729
Chuanxiao Donga99d1792010-07-27 11:32:21 +0800730 if (op == DENALI_READ)
Jamie Iles9589bf52011-05-06 15:28:56 +0100731 irq_mask = INTR_STATUS__LOAD_COMP;
Chuanxiao Donga99d1792010-07-27 11:32:21 +0800732 else if (op == DENALI_WRITE)
733 irq_mask = 0;
734 else
735 BUG();
Jason Robertsce082592010-05-13 15:57:33 +0100736
737 setup_ecc_for_xfer(denali, ecc_en, transfer_spare);
738
Jason Robertsce082592010-05-13 15:57:33 +0100739 /* clear interrupts */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800740 clear_interrupts(denali);
Jason Robertsce082592010-05-13 15:57:33 +0100741
742 addr = BANK(denali->flash_bank) | denali->page;
743
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800744 if (op == DENALI_WRITE && access_type != SPARE_ACCESS) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800745 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800746 iowrite32(cmd, denali->flash_mem);
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800747 } else if (op == DENALI_WRITE && access_type == SPARE_ACCESS) {
Jason Robertsce082592010-05-13 15:57:33 +0100748 /* read spare area */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800749 cmd = MODE_10 | addr;
Jason Robertsce082592010-05-13 15:57:33 +0100750 index_addr(denali, (uint32_t)cmd, access_type);
751
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800752 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800753 iowrite32(cmd, denali->flash_mem);
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800754 } else if (op == DENALI_READ) {
Jason Robertsce082592010-05-13 15:57:33 +0100755 /* setup page read request for access type */
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800756 cmd = MODE_10 | addr;
Jason Robertsce082592010-05-13 15:57:33 +0100757 index_addr(denali, (uint32_t)cmd, access_type);
758
759 /* page 33 of the NAND controller spec indicates we should not
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800760 use the pipeline commands in Spare area only mode. So we
Jason Robertsce082592010-05-13 15:57:33 +0100761 don't.
762 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800763 if (access_type == SPARE_ACCESS) {
Jason Robertsce082592010-05-13 15:57:33 +0100764 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800765 iowrite32(cmd, denali->flash_mem);
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800766 } else {
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800767 index_addr(denali, (uint32_t)cmd,
768 0x2000 | op | page_count);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800769
770 /* wait for command to be accepted
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800771 * can always use status0 bit as the
772 * mask is identical for each
Jason Robertsce082592010-05-13 15:57:33 +0100773 * bank. */
774 irq_status = wait_for_irq(denali, irq_mask);
775
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800776 if (irq_status == 0) {
Jamie Iles84457942011-05-06 15:28:55 +0100777 dev_err(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +0800778 "cmd, page, addr on timeout "
779 "(0x%x, 0x%x, 0x%x)\n",
780 cmd, denali->page, addr);
Jason Robertsce082592010-05-13 15:57:33 +0100781 status = FAIL;
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800782 } else {
Jason Robertsce082592010-05-13 15:57:33 +0100783 cmd = MODE_01 | addr;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800784 iowrite32(cmd, denali->flash_mem);
Jason Robertsce082592010-05-13 15:57:33 +0100785 }
786 }
787 }
788 return status;
789}
790
791/* helper function that simply writes a buffer to the flash */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800792static int write_data_to_flash_mem(struct denali_nand_info *denali,
793 const uint8_t *buf,
794 int len)
Jason Robertsce082592010-05-13 15:57:33 +0100795{
796 uint32_t i = 0, *buf32;
797
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800798 /* verify that the len is a multiple of 4. see comment in
799 * read_data_from_flash_mem() */
Jason Robertsce082592010-05-13 15:57:33 +0100800 BUG_ON((len % 4) != 0);
801
802 /* write the data to the flash memory */
803 buf32 = (uint32_t *)buf;
804 for (i = 0; i < len / 4; i++)
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800805 iowrite32(*buf32++, denali->flash_mem + 0x10);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800806 return i*4; /* intent is to return the number of bytes read */
Jason Robertsce082592010-05-13 15:57:33 +0100807}
808
809/* helper function that simply reads a buffer from the flash */
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800810static int read_data_from_flash_mem(struct denali_nand_info *denali,
811 uint8_t *buf,
812 int len)
Jason Robertsce082592010-05-13 15:57:33 +0100813{
814 uint32_t i = 0, *buf32;
815
816 /* we assume that len will be a multiple of 4, if not
817 * it would be nice to know about it ASAP rather than
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800818 * have random failures...
819 * This assumption is based on the fact that this
820 * function is designed to be used to read flash pages,
Jason Robertsce082592010-05-13 15:57:33 +0100821 * which are typically multiples of 4...
822 */
823
824 BUG_ON((len % 4) != 0);
825
826 /* transfer the data from the flash */
827 buf32 = (uint32_t *)buf;
828 for (i = 0; i < len / 4; i++)
Jason Robertsce082592010-05-13 15:57:33 +0100829 *buf32++ = ioread32(denali->flash_mem + 0x10);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800830 return i*4; /* intent is to return the number of bytes read */
Jason Robertsce082592010-05-13 15:57:33 +0100831}
832
833/* writes OOB data to the device */
834static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
835{
836 struct denali_nand_info *denali = mtd_to_denali(mtd);
837 uint32_t irq_status = 0;
Jamie Iles9589bf52011-05-06 15:28:56 +0100838 uint32_t irq_mask = INTR_STATUS__PROGRAM_COMP |
839 INTR_STATUS__PROGRAM_FAIL;
Jason Robertsce082592010-05-13 15:57:33 +0100840 int status = 0;
841
842 denali->page = page;
843
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800844 if (denali_send_pipeline_cmd(denali, false, false, SPARE_ACCESS,
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800845 DENALI_WRITE) == PASS) {
Jason Robertsce082592010-05-13 15:57:33 +0100846 write_data_to_flash_mem(denali, buf, mtd->oobsize);
847
Jason Robertsce082592010-05-13 15:57:33 +0100848 /* wait for operation to complete */
849 irq_status = wait_for_irq(denali, irq_mask);
850
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800851 if (irq_status == 0) {
Jamie Iles84457942011-05-06 15:28:55 +0100852 dev_err(denali->dev, "OOB write failed\n");
Jason Robertsce082592010-05-13 15:57:33 +0100853 status = -EIO;
854 }
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800855 } else {
Jamie Iles84457942011-05-06 15:28:55 +0100856 dev_err(denali->dev, "unable to send pipeline command\n");
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800857 status = -EIO;
Jason Robertsce082592010-05-13 15:57:33 +0100858 }
859 return status;
860}
861
862/* reads OOB data from the device */
863static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
864{
865 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jamie Iles9589bf52011-05-06 15:28:56 +0100866 uint32_t irq_mask = INTR_STATUS__LOAD_COMP,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800867 irq_status = 0, addr = 0x0, cmd = 0x0;
Jason Robertsce082592010-05-13 15:57:33 +0100868
869 denali->page = page;
870
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800871 if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800872 DENALI_READ) == PASS) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800873 read_data_from_flash_mem(denali, buf, mtd->oobsize);
Jason Robertsce082592010-05-13 15:57:33 +0100874
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800875 /* wait for command to be accepted
Jason Robertsce082592010-05-13 15:57:33 +0100876 * can always use status0 bit as the mask is identical for each
877 * bank. */
878 irq_status = wait_for_irq(denali, irq_mask);
879
880 if (irq_status == 0)
Jamie Iles84457942011-05-06 15:28:55 +0100881 dev_err(denali->dev, "page on OOB timeout %d\n",
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800882 denali->page);
Jason Robertsce082592010-05-13 15:57:33 +0100883
884 /* We set the device back to MAIN_ACCESS here as I observed
885 * instability with the controller if you do a block erase
886 * and the last transaction was a SPARE_ACCESS. Block erase
887 * is reliable (according to the MTD test infrastructure)
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800888 * if you are in MAIN_ACCESS.
Jason Robertsce082592010-05-13 15:57:33 +0100889 */
890 addr = BANK(denali->flash_bank) | denali->page;
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800891 cmd = MODE_10 | addr;
Jason Robertsce082592010-05-13 15:57:33 +0100892 index_addr(denali, (uint32_t)cmd, MAIN_ACCESS);
Jason Robertsce082592010-05-13 15:57:33 +0100893 }
894}
895
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800896/* this function examines buffers to see if they contain data that
Jason Robertsce082592010-05-13 15:57:33 +0100897 * indicate that the buffer is part of an erased region of flash.
898 */
Rashika Kheria919193c2013-12-13 12:46:04 +0530899static bool is_erased(uint8_t *buf, int len)
Jason Robertsce082592010-05-13 15:57:33 +0100900{
901 int i = 0;
902 for (i = 0; i < len; i++)
Jason Robertsce082592010-05-13 15:57:33 +0100903 if (buf[i] != 0xFF)
Jason Robertsce082592010-05-13 15:57:33 +0100904 return false;
Jason Robertsce082592010-05-13 15:57:33 +0100905 return true;
906}
907#define ECC_SECTOR_SIZE 512
908
909#define ECC_SECTOR(x) (((x) & ECC_ERROR_ADDRESS__SECTOR_NR) >> 12)
910#define ECC_BYTE(x) (((x) & ECC_ERROR_ADDRESS__OFFSET))
911#define ECC_CORRECTION_VALUE(x) ((x) & ERR_CORRECTION_INFO__BYTEMASK)
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800912#define ECC_ERROR_CORRECTABLE(x) (!((x) & ERR_CORRECTION_INFO__ERROR_TYPE))
913#define ECC_ERR_DEVICE(x) (((x) & ERR_CORRECTION_INFO__DEVICE_NR) >> 8)
Jason Robertsce082592010-05-13 15:57:33 +0100914#define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO)
915
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800916static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf,
Mike Dunn3f91e942012-04-25 12:06:09 -0700917 uint32_t irq_status, unsigned int *max_bitflips)
Jason Robertsce082592010-05-13 15:57:33 +0100918{
919 bool check_erased_page = false;
Mike Dunn3f91e942012-04-25 12:06:09 -0700920 unsigned int bitflips = 0;
Jason Robertsce082592010-05-13 15:57:33 +0100921
Jamie Iles9589bf52011-05-06 15:28:56 +0100922 if (irq_status & INTR_STATUS__ECC_ERR) {
Jason Robertsce082592010-05-13 15:57:33 +0100923 /* read the ECC errors. we'll ignore them for now */
924 uint32_t err_address = 0, err_correction_info = 0;
925 uint32_t err_byte = 0, err_sector = 0, err_device = 0;
926 uint32_t err_correction_value = 0;
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800927 denali_set_intr_modes(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +0100928
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800929 do {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800930 err_address = ioread32(denali->flash_reg +
Jason Robertsce082592010-05-13 15:57:33 +0100931 ECC_ERROR_ADDRESS);
932 err_sector = ECC_SECTOR(err_address);
933 err_byte = ECC_BYTE(err_address);
934
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800935 err_correction_info = ioread32(denali->flash_reg +
Jason Robertsce082592010-05-13 15:57:33 +0100936 ERR_CORRECTION_INFO);
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800937 err_correction_value =
Jason Robertsce082592010-05-13 15:57:33 +0100938 ECC_CORRECTION_VALUE(err_correction_info);
939 err_device = ECC_ERR_DEVICE(err_correction_info);
940
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800941 if (ECC_ERROR_CORRECTABLE(err_correction_info)) {
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800942 /* If err_byte is larger than ECC_SECTOR_SIZE,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300943 * means error happened in OOB, so we ignore
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800944 * it. It's no need for us to correct it
945 * err_device is represented the NAND error
946 * bits are happened in if there are more
947 * than one NAND connected.
948 * */
949 if (err_byte < ECC_SECTOR_SIZE) {
950 int offset;
951 offset = (err_sector *
952 ECC_SECTOR_SIZE +
953 err_byte) *
954 denali->devnum +
955 err_device;
Jason Robertsce082592010-05-13 15:57:33 +0100956 /* correct the ECC error */
957 buf[offset] ^= err_correction_value;
958 denali->mtd.ecc_stats.corrected++;
Mike Dunn3f91e942012-04-25 12:06:09 -0700959 bitflips++;
Jason Robertsce082592010-05-13 15:57:33 +0100960 }
Chuanxiao Dong345b1d32010-07-27 10:41:53 +0800961 } else {
Chuanxiao5bac3ac2010-08-05 23:06:04 +0800962 /* if the error is not correctable, need to
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +0800963 * look at the page to see if it is an erased
964 * page. if so, then it's not a real ECC error
965 * */
Jason Robertsce082592010-05-13 15:57:33 +0100966 check_erased_page = true;
967 }
Jason Robertsce082592010-05-13 15:57:33 +0100968 } while (!ECC_LAST_ERR(err_correction_info));
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800969 /* Once handle all ecc errors, controller will triger
970 * a ECC_TRANSACTION_DONE interrupt, so here just wait
971 * for a while for this interrupt
972 * */
973 while (!(read_interrupt_status(denali) &
Jamie Iles9589bf52011-05-06 15:28:56 +0100974 INTR_STATUS__ECC_TRANSACTION_DONE))
Chuanxiao Dong8ae61eb2010-08-10 00:07:01 +0800975 cpu_relax();
976 clear_interrupts(denali);
977 denali_set_intr_modes(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +0100978 }
Mike Dunn3f91e942012-04-25 12:06:09 -0700979 *max_bitflips = bitflips;
Jason Robertsce082592010-05-13 15:57:33 +0100980 return check_erased_page;
981}
982
983/* programs the controller to either enable/disable DMA transfers */
David Woodhouseaadff492010-05-13 16:12:43 +0100984static void denali_enable_dma(struct denali_nand_info *denali, bool en)
Jason Robertsce082592010-05-13 15:57:33 +0100985{
986 uint32_t reg_val = 0x0;
987
Chuanxiao Donga99d1792010-07-27 11:32:21 +0800988 if (en)
989 reg_val = DMA_ENABLE__FLAG;
Jason Robertsce082592010-05-13 15:57:33 +0100990
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +0800991 iowrite32(reg_val, denali->flash_reg + DMA_ENABLE);
Jason Robertsce082592010-05-13 15:57:33 +0100992 ioread32(denali->flash_reg + DMA_ENABLE);
993}
994
995/* setups the HW to perform the data DMA */
David Woodhouseaadff492010-05-13 16:12:43 +0100996static void denali_setup_dma(struct denali_nand_info *denali, int op)
Jason Robertsce082592010-05-13 15:57:33 +0100997{
998 uint32_t mode = 0x0;
999 const int page_count = 1;
1000 dma_addr_t addr = denali->buf.dma_buf;
1001
1002 mode = MODE_10 | BANK(denali->flash_bank);
1003
1004 /* DMA is a four step process */
1005
1006 /* 1. setup transfer type and # of pages */
1007 index_addr(denali, mode | denali->page, 0x2000 | op | page_count);
1008
1009 /* 2. set memory high address bits 23:8 */
1010 index_addr(denali, mode | ((uint16_t)(addr >> 16) << 8), 0x2200);
1011
1012 /* 3. set memory low address bits 23:8 */
1013 index_addr(denali, mode | ((uint16_t)addr << 8), 0x2300);
1014
1015 /* 4. interrupt when complete, burst len = 64 bytes*/
1016 index_addr(denali, mode | 0x14000, 0x2400);
1017}
1018
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001019/* writes a page. user specifies type, and this function handles the
Chuanxiao Dongb292c342010-08-11 17:46:00 +08001020 * configuration details. */
Josh Wufdbad98d2012-06-25 18:07:45 +08001021static int write_page(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001022 const uint8_t *buf, bool raw_xfer)
1023{
1024 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jason Robertsce082592010-05-13 15:57:33 +01001025
1026 dma_addr_t addr = denali->buf.dma_buf;
1027 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1028
1029 uint32_t irq_status = 0;
Jamie Iles9589bf52011-05-06 15:28:56 +01001030 uint32_t irq_mask = INTR_STATUS__DMA_CMD_COMP |
1031 INTR_STATUS__PROGRAM_FAIL;
Jason Robertsce082592010-05-13 15:57:33 +01001032
1033 /* if it is a raw xfer, we want to disable ecc, and send
1034 * the spare area.
1035 * !raw_xfer - enable ecc
1036 * raw_xfer - transfer spare
1037 */
1038 setup_ecc_for_xfer(denali, !raw_xfer, raw_xfer);
1039
1040 /* copy buffer into DMA buffer */
1041 memcpy(denali->buf.buf, buf, mtd->writesize);
1042
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001043 if (raw_xfer) {
Jason Robertsce082592010-05-13 15:57:33 +01001044 /* transfer the data to the spare area */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001045 memcpy(denali->buf.buf + mtd->writesize,
1046 chip->oob_poi,
1047 mtd->oobsize);
Jason Robertsce082592010-05-13 15:57:33 +01001048 }
1049
Jamie Iles84457942011-05-06 15:28:55 +01001050 dma_sync_single_for_device(denali->dev, addr, size, DMA_TO_DEVICE);
Jason Robertsce082592010-05-13 15:57:33 +01001051
1052 clear_interrupts(denali);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001053 denali_enable_dma(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001054
David Woodhouseaadff492010-05-13 16:12:43 +01001055 denali_setup_dma(denali, DENALI_WRITE);
Jason Robertsce082592010-05-13 15:57:33 +01001056
1057 /* wait for operation to complete */
1058 irq_status = wait_for_irq(denali, irq_mask);
1059
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001060 if (irq_status == 0) {
Jamie Iles84457942011-05-06 15:28:55 +01001061 dev_err(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001062 "timeout on write_page (type = %d)\n",
1063 raw_xfer);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001064 denali->status =
Jamie Iles9589bf52011-05-06 15:28:56 +01001065 (irq_status & INTR_STATUS__PROGRAM_FAIL) ?
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001066 NAND_STATUS_FAIL : PASS;
Jason Robertsce082592010-05-13 15:57:33 +01001067 }
1068
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001069 denali_enable_dma(denali, false);
Jamie Iles84457942011-05-06 15:28:55 +01001070 dma_sync_single_for_cpu(denali->dev, addr, size, DMA_TO_DEVICE);
Josh Wufdbad98d2012-06-25 18:07:45 +08001071
1072 return 0;
Jason Robertsce082592010-05-13 15:57:33 +01001073}
1074
1075/* NAND core entry points */
1076
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001077/* this is the callback that the NAND core calls to write a page. Since
Chuanxiao Dongb292c342010-08-11 17:46:00 +08001078 * writing a page with ECC or without is similar, all the work is done
1079 * by write_page above.
1080 * */
Josh Wufdbad98d2012-06-25 18:07:45 +08001081static int denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001082 const uint8_t *buf, int oob_required)
Jason Robertsce082592010-05-13 15:57:33 +01001083{
1084 /* for regular page writes, we let HW handle all the ECC
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001085 * data written to the device. */
Josh Wufdbad98d2012-06-25 18:07:45 +08001086 return write_page(mtd, chip, buf, false);
Jason Robertsce082592010-05-13 15:57:33 +01001087}
1088
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001089/* This is the callback that the NAND core calls to write a page without ECC.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001090 * raw access is similar to ECC page writes, so all the work is done in the
Chuanxiao Dongb292c342010-08-11 17:46:00 +08001091 * write_page() function above.
Jason Robertsce082592010-05-13 15:57:33 +01001092 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001093static int denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001094 const uint8_t *buf, int oob_required)
Jason Robertsce082592010-05-13 15:57:33 +01001095{
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001096 /* for raw page writes, we want to disable ECC and simply write
Jason Robertsce082592010-05-13 15:57:33 +01001097 whatever data is in the buffer. */
Josh Wufdbad98d2012-06-25 18:07:45 +08001098 return write_page(mtd, chip, buf, true);
Jason Robertsce082592010-05-13 15:57:33 +01001099}
1100
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001101static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
Jason Robertsce082592010-05-13 15:57:33 +01001102 int page)
1103{
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001104 return write_oob_data(mtd, chip->oob_poi, page);
Jason Robertsce082592010-05-13 15:57:33 +01001105}
1106
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001107static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001108 int page)
Jason Robertsce082592010-05-13 15:57:33 +01001109{
1110 read_oob_data(mtd, chip->oob_poi, page);
1111
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001112 return 0;
Jason Robertsce082592010-05-13 15:57:33 +01001113}
1114
1115static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001116 uint8_t *buf, int oob_required, int page)
Jason Robertsce082592010-05-13 15:57:33 +01001117{
Mike Dunn3f91e942012-04-25 12:06:09 -07001118 unsigned int max_bitflips;
Jason Robertsce082592010-05-13 15:57:33 +01001119 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jason Robertsce082592010-05-13 15:57:33 +01001120
1121 dma_addr_t addr = denali->buf.dma_buf;
1122 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1123
1124 uint32_t irq_status = 0;
Jamie Iles9589bf52011-05-06 15:28:56 +01001125 uint32_t irq_mask = INTR_STATUS__ECC_TRANSACTION_DONE |
1126 INTR_STATUS__ECC_ERR;
Jason Robertsce082592010-05-13 15:57:33 +01001127 bool check_erased_page = false;
1128
Chuanxiao Dong7d8a26f2010-08-11 18:19:23 +08001129 if (page != denali->page) {
Jamie Iles84457942011-05-06 15:28:55 +01001130 dev_err(denali->dev, "IN %s: page %d is not"
Chuanxiao Dong7d8a26f2010-08-11 18:19:23 +08001131 " equal to denali->page %d, investigate!!",
1132 __func__, page, denali->page);
1133 BUG();
1134 }
1135
Jason Robertsce082592010-05-13 15:57:33 +01001136 setup_ecc_for_xfer(denali, true, false);
1137
David Woodhouseaadff492010-05-13 16:12:43 +01001138 denali_enable_dma(denali, true);
Jamie Iles84457942011-05-06 15:28:55 +01001139 dma_sync_single_for_device(denali->dev, addr, size, DMA_FROM_DEVICE);
Jason Robertsce082592010-05-13 15:57:33 +01001140
1141 clear_interrupts(denali);
David Woodhouseaadff492010-05-13 16:12:43 +01001142 denali_setup_dma(denali, DENALI_READ);
Jason Robertsce082592010-05-13 15:57:33 +01001143
1144 /* wait for operation to complete */
1145 irq_status = wait_for_irq(denali, irq_mask);
1146
Jamie Iles84457942011-05-06 15:28:55 +01001147 dma_sync_single_for_cpu(denali->dev, addr, size, DMA_FROM_DEVICE);
Jason Robertsce082592010-05-13 15:57:33 +01001148
1149 memcpy(buf, denali->buf.buf, mtd->writesize);
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001150
Mike Dunn3f91e942012-04-25 12:06:09 -07001151 check_erased_page = handle_ecc(denali, buf, irq_status, &max_bitflips);
David Woodhouseaadff492010-05-13 16:12:43 +01001152 denali_enable_dma(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +01001153
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001154 if (check_erased_page) {
Jason Robertsce082592010-05-13 15:57:33 +01001155 read_oob_data(&denali->mtd, chip->oob_poi, denali->page);
1156
1157 /* check ECC failures that may have occurred on erased pages */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001158 if (check_erased_page) {
Jason Robertsce082592010-05-13 15:57:33 +01001159 if (!is_erased(buf, denali->mtd.writesize))
Jason Robertsce082592010-05-13 15:57:33 +01001160 denali->mtd.ecc_stats.failed++;
Jason Robertsce082592010-05-13 15:57:33 +01001161 if (!is_erased(buf, denali->mtd.oobsize))
Jason Robertsce082592010-05-13 15:57:33 +01001162 denali->mtd.ecc_stats.failed++;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001163 }
Jason Robertsce082592010-05-13 15:57:33 +01001164 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001165 return max_bitflips;
Jason Robertsce082592010-05-13 15:57:33 +01001166}
1167
1168static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001169 uint8_t *buf, int oob_required, int page)
Jason Robertsce082592010-05-13 15:57:33 +01001170{
1171 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jason Robertsce082592010-05-13 15:57:33 +01001172
1173 dma_addr_t addr = denali->buf.dma_buf;
1174 size_t size = denali->mtd.writesize + denali->mtd.oobsize;
1175
1176 uint32_t irq_status = 0;
Jamie Iles9589bf52011-05-06 15:28:56 +01001177 uint32_t irq_mask = INTR_STATUS__DMA_CMD_COMP;
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001178
Chuanxiao Dong7d8a26f2010-08-11 18:19:23 +08001179 if (page != denali->page) {
Jamie Iles84457942011-05-06 15:28:55 +01001180 dev_err(denali->dev, "IN %s: page %d is not"
Chuanxiao Dong7d8a26f2010-08-11 18:19:23 +08001181 " equal to denali->page %d, investigate!!",
1182 __func__, page, denali->page);
1183 BUG();
1184 }
1185
Jason Robertsce082592010-05-13 15:57:33 +01001186 setup_ecc_for_xfer(denali, false, true);
David Woodhouseaadff492010-05-13 16:12:43 +01001187 denali_enable_dma(denali, true);
Jason Robertsce082592010-05-13 15:57:33 +01001188
Jamie Iles84457942011-05-06 15:28:55 +01001189 dma_sync_single_for_device(denali->dev, addr, size, DMA_FROM_DEVICE);
Jason Robertsce082592010-05-13 15:57:33 +01001190
1191 clear_interrupts(denali);
David Woodhouseaadff492010-05-13 16:12:43 +01001192 denali_setup_dma(denali, DENALI_READ);
Jason Robertsce082592010-05-13 15:57:33 +01001193
1194 /* wait for operation to complete */
1195 irq_status = wait_for_irq(denali, irq_mask);
1196
Jamie Iles84457942011-05-06 15:28:55 +01001197 dma_sync_single_for_cpu(denali->dev, addr, size, DMA_FROM_DEVICE);
Jason Robertsce082592010-05-13 15:57:33 +01001198
David Woodhouseaadff492010-05-13 16:12:43 +01001199 denali_enable_dma(denali, false);
Jason Robertsce082592010-05-13 15:57:33 +01001200
1201 memcpy(buf, denali->buf.buf, mtd->writesize);
1202 memcpy(chip->oob_poi, denali->buf.buf + mtd->writesize, mtd->oobsize);
1203
1204 return 0;
1205}
1206
1207static uint8_t denali_read_byte(struct mtd_info *mtd)
1208{
1209 struct denali_nand_info *denali = mtd_to_denali(mtd);
1210 uint8_t result = 0xff;
1211
1212 if (denali->buf.head < denali->buf.tail)
Jason Robertsce082592010-05-13 15:57:33 +01001213 result = denali->buf.buf[denali->buf.head++];
Jason Robertsce082592010-05-13 15:57:33 +01001214
Jason Robertsce082592010-05-13 15:57:33 +01001215 return result;
1216}
1217
1218static void denali_select_chip(struct mtd_info *mtd, int chip)
1219{
1220 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001221
Jason Robertsce082592010-05-13 15:57:33 +01001222 spin_lock_irq(&denali->irq_lock);
1223 denali->flash_bank = chip;
1224 spin_unlock_irq(&denali->irq_lock);
1225}
1226
1227static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
1228{
1229 struct denali_nand_info *denali = mtd_to_denali(mtd);
1230 int status = denali->status;
1231 denali->status = 0;
1232
Jason Robertsce082592010-05-13 15:57:33 +01001233 return status;
1234}
1235
Brian Norris49c50b92014-05-06 16:02:19 -07001236static int denali_erase(struct mtd_info *mtd, int page)
Jason Robertsce082592010-05-13 15:57:33 +01001237{
1238 struct denali_nand_info *denali = mtd_to_denali(mtd);
1239
1240 uint32_t cmd = 0x0, irq_status = 0;
1241
Jason Robertsce082592010-05-13 15:57:33 +01001242 /* clear interrupts */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001243 clear_interrupts(denali);
Jason Robertsce082592010-05-13 15:57:33 +01001244
1245 /* setup page read request for access type */
1246 cmd = MODE_10 | BANK(denali->flash_bank) | page;
1247 index_addr(denali, (uint32_t)cmd, 0x1);
1248
1249 /* wait for erase to complete or failure to occur */
Jamie Iles9589bf52011-05-06 15:28:56 +01001250 irq_status = wait_for_irq(denali, INTR_STATUS__ERASE_COMP |
1251 INTR_STATUS__ERASE_FAIL);
Jason Robertsce082592010-05-13 15:57:33 +01001252
Brian Norris49c50b92014-05-06 16:02:19 -07001253 return (irq_status & INTR_STATUS__ERASE_FAIL) ? NAND_STATUS_FAIL : PASS;
Jason Robertsce082592010-05-13 15:57:33 +01001254}
1255
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001256static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col,
Jason Robertsce082592010-05-13 15:57:33 +01001257 int page)
1258{
1259 struct denali_nand_info *denali = mtd_to_denali(mtd);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +08001260 uint32_t addr, id;
1261 int i;
Jason Robertsce082592010-05-13 15:57:33 +01001262
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001263 switch (cmd) {
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001264 case NAND_CMD_PAGEPROG:
1265 break;
1266 case NAND_CMD_STATUS:
1267 read_status(denali);
1268 break;
1269 case NAND_CMD_READID:
Florian Fainelli42af8b52010-08-30 18:32:20 +02001270 case NAND_CMD_PARAM:
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001271 reset_buf(denali);
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +08001272 /*sometimes ManufactureId read from register is not right
1273 * e.g. some of Micron MT29F32G08QAA MLC NAND chips
1274 * So here we send READID cmd to NAND insteand
1275 * */
1276 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
1277 index_addr(denali, (uint32_t)addr | 0, 0x90);
1278 index_addr(denali, (uint32_t)addr | 1, 0);
grmoore@altera.comd68a5c32014-06-23 14:21:10 -05001279 for (i = 0; i < 8; i++) {
Chuanxiao Dongef41e1b2010-08-06 00:48:49 +08001280 index_addr_read_data(denali,
1281 (uint32_t)addr | 2,
1282 &id);
1283 write_byte_to_buf(denali, id);
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001284 }
1285 break;
1286 case NAND_CMD_READ0:
1287 case NAND_CMD_SEQIN:
1288 denali->page = page;
1289 break;
1290 case NAND_CMD_RESET:
1291 reset_bank(denali);
1292 break;
1293 case NAND_CMD_READOOB:
1294 /* TODO: Read OOB data */
1295 break;
1296 default:
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001297 pr_err(": unsupported command received 0x%x\n", cmd);
Chuanxiao Donga99d1792010-07-27 11:32:21 +08001298 break;
Jason Robertsce082592010-05-13 15:57:33 +01001299 }
1300}
1301
1302/* stubs for ECC functions not used by the NAND core */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001303static int denali_ecc_calculate(struct mtd_info *mtd, const uint8_t *data,
Jason Robertsce082592010-05-13 15:57:33 +01001304 uint8_t *ecc_code)
1305{
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001306 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jamie Iles84457942011-05-06 15:28:55 +01001307 dev_err(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001308 "denali_ecc_calculate called unexpectedly\n");
Jason Robertsce082592010-05-13 15:57:33 +01001309 BUG();
1310 return -EIO;
1311}
1312
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001313static int denali_ecc_correct(struct mtd_info *mtd, uint8_t *data,
Jason Robertsce082592010-05-13 15:57:33 +01001314 uint8_t *read_ecc, uint8_t *calc_ecc)
1315{
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001316 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jamie Iles84457942011-05-06 15:28:55 +01001317 dev_err(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001318 "denali_ecc_correct called unexpectedly\n");
Jason Robertsce082592010-05-13 15:57:33 +01001319 BUG();
1320 return -EIO;
1321}
1322
1323static void denali_ecc_hwctl(struct mtd_info *mtd, int mode)
1324{
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001325 struct denali_nand_info *denali = mtd_to_denali(mtd);
Jamie Iles84457942011-05-06 15:28:55 +01001326 dev_err(denali->dev,
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001327 "denali_ecc_hwctl called unexpectedly\n");
Jason Robertsce082592010-05-13 15:57:33 +01001328 BUG();
1329}
1330/* end NAND core entry points */
1331
1332/* Initialization code to bring the device up to a known good state */
1333static void denali_hw_init(struct denali_nand_info *denali)
1334{
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001335 /* tell driver how many bit controller will skip before
1336 * writing ECC code in OOB, this register may be already
1337 * set by firmware. So we read this value out.
1338 * if this value is 0, just let it be.
1339 * */
1340 denali->bbtskipbytes = ioread32(denali->flash_reg +
1341 SPARE_AREA_SKIP_BYTES);
Jamie Ilesbc27ede2011-06-06 17:11:34 +01001342 detect_max_banks(denali);
Chuanxiao Dongeda936e2010-07-27 14:17:37 +08001343 denali_nand_reset(denali);
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001344 iowrite32(0x0F, denali->flash_reg + RB_PIN_ENABLED);
1345 iowrite32(CHIP_EN_DONT_CARE__FLAG,
Chuanxiao Dongbdca6da2010-07-27 11:28:09 +08001346 denali->flash_reg + CHIP_ENABLE_DONT_CARE);
Jason Robertsce082592010-05-13 15:57:33 +01001347
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001348 iowrite32(0xffff, denali->flash_reg + SPARE_AREA_MARKER);
Jason Robertsce082592010-05-13 15:57:33 +01001349
1350 /* Should set value for these registers when init */
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001351 iowrite32(0, denali->flash_reg + TWO_ROW_ADDR_CYCLES);
1352 iowrite32(1, denali->flash_reg + ECC_ENABLE);
Chuanxiao Dong5eab6aaa2010-08-12 10:07:18 +08001353 denali_nand_timing_set(denali);
1354 denali_irq_init(denali);
Jason Robertsce082592010-05-13 15:57:33 +01001355}
1356
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001357/* Althogh controller spec said SLC ECC is forceb to be 4bit,
1358 * but denali controller in MRST only support 15bit and 8bit ECC
1359 * correction
1360 * */
1361#define ECC_8BITS 14
1362static struct nand_ecclayout nand_8bit_oob = {
1363 .eccbytes = 14,
Jason Robertsce082592010-05-13 15:57:33 +01001364};
1365
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001366#define ECC_15BITS 26
1367static struct nand_ecclayout nand_15bit_oob = {
1368 .eccbytes = 26,
Jason Robertsce082592010-05-13 15:57:33 +01001369};
1370
1371static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1372static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1373
1374static struct nand_bbt_descr bbt_main_descr = {
1375 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1376 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1377 .offs = 8,
1378 .len = 4,
1379 .veroffs = 12,
1380 .maxblocks = 4,
1381 .pattern = bbt_pattern,
1382};
1383
1384static struct nand_bbt_descr bbt_mirror_descr = {
1385 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1386 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1387 .offs = 8,
1388 .len = 4,
1389 .veroffs = 12,
1390 .maxblocks = 4,
1391 .pattern = mirror_pattern,
1392};
1393
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001394/* initialize driver data structures */
Brian Norris8c519432013-08-10 22:57:30 -07001395static void denali_drv_init(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +01001396{
1397 denali->idx = 0;
1398
1399 /* setup interrupt handler */
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001400 /* the completion object will be used to notify
Jason Robertsce082592010-05-13 15:57:33 +01001401 * the callee that the interrupt is done */
1402 init_completion(&denali->complete);
1403
1404 /* the spinlock will be used to synchronize the ISR
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001405 * with any element that might be access shared
Jason Robertsce082592010-05-13 15:57:33 +01001406 * data (interrupt status) */
1407 spin_lock_init(&denali->irq_lock);
1408
1409 /* indicate that MTD has not selected a valid bank yet */
1410 denali->flash_bank = CHIP_SELECT_INVALID;
1411
1412 /* initialize our irq_status variable to indicate no interrupts */
1413 denali->irq_status = 0;
1414}
1415
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001416int denali_init(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +01001417{
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001418 int ret;
Jason Robertsce082592010-05-13 15:57:33 +01001419
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001420 if (denali->platform == INTEL_CE4100) {
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001421 /* Due to a silicon limitation, we can only support
1422 * ONFI timing mode 1 and below.
1423 */
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001424 if (onfi_timing_mode < -1 || onfi_timing_mode > 1) {
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001425 pr_err("Intel CE4100 only supports ONFI timing mode 1 or below\n");
1426 return -EINVAL;
Jason Robertsce082592010-05-13 15:57:33 +01001427 }
1428 }
1429
Huang Shijiee07caa32013-12-21 00:02:28 +08001430 /* allocate a temporary buffer for nand_scan_ident() */
1431 denali->buf.buf = devm_kzalloc(denali->dev, PAGE_SIZE,
1432 GFP_DMA | GFP_KERNEL);
1433 if (!denali->buf.buf)
1434 return -ENOMEM;
Jason Robertsce082592010-05-13 15:57:33 +01001435
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001436 denali->mtd.dev.parent = denali->dev;
Jason Robertsce082592010-05-13 15:57:33 +01001437 denali_hw_init(denali);
1438 denali_drv_init(denali);
1439
Chuanxiao Dong5eab6aaa2010-08-12 10:07:18 +08001440 /* denali_isr register is done after all the hardware
1441 * initilization is finished*/
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001442 if (request_irq(denali->irq, denali_isr, IRQF_SHARED,
Jason Robertsce082592010-05-13 15:57:33 +01001443 DENALI_NAND_NAME, denali)) {
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001444 pr_err("Spectra: Unable to allocate IRQ\n");
1445 return -ENODEV;
Jason Robertsce082592010-05-13 15:57:33 +01001446 }
1447
1448 /* now that our ISR is registered, we can enable interrupts */
Chuanxiao Dongeda936e2010-07-27 14:17:37 +08001449 denali_set_intr_modes(denali, true);
Chuanxiao Dong5eab6aaa2010-08-12 10:07:18 +08001450 denali->mtd.name = "denali-nand";
Jason Robertsce082592010-05-13 15:57:33 +01001451 denali->mtd.owner = THIS_MODULE;
1452 denali->mtd.priv = &denali->nand;
1453
1454 /* register the driver with the NAND core subsystem */
1455 denali->nand.select_chip = denali_select_chip;
1456 denali->nand.cmdfunc = denali_cmdfunc;
1457 denali->nand.read_byte = denali_read_byte;
1458 denali->nand.waitfunc = denali_waitfunc;
1459
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001460 /* scan for NAND devices attached to the controller
Jason Robertsce082592010-05-13 15:57:33 +01001461 * this is the first stage in a two step process to register
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001462 * with the nand subsystem */
Jamie Ilesc89eeda2011-05-06 15:28:57 +01001463 if (nand_scan_ident(&denali->mtd, denali->max_banks, NULL)) {
Jason Robertsce082592010-05-13 15:57:33 +01001464 ret = -ENXIO;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001465 goto failed_req_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001466 }
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001467
Huang Shijiee07caa32013-12-21 00:02:28 +08001468 /* allocate the right size buffer now */
1469 devm_kfree(denali->dev, denali->buf.buf);
1470 denali->buf.buf = devm_kzalloc(denali->dev,
1471 denali->mtd.writesize + denali->mtd.oobsize,
1472 GFP_KERNEL);
1473 if (!denali->buf.buf) {
1474 ret = -ENOMEM;
1475 goto failed_req_irq;
1476 }
1477
1478 /* Is 32-bit DMA supported? */
1479 ret = dma_set_mask(denali->dev, DMA_BIT_MASK(32));
1480 if (ret) {
1481 pr_err("Spectra: no usable DMA configuration\n");
1482 goto failed_req_irq;
1483 }
1484
1485 denali->buf.dma_buf = dma_map_single(denali->dev, denali->buf.buf,
1486 denali->mtd.writesize + denali->mtd.oobsize,
1487 DMA_BIDIRECTIONAL);
1488 if (dma_mapping_error(denali->dev, denali->buf.dma_buf)) {
1489 dev_err(denali->dev, "Spectra: failed to map DMA buffer\n");
1490 ret = -EIO;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001491 goto failed_req_irq;
Chuanxiao.Dong66406522010-08-06 18:48:21 +08001492 }
1493
Chuanxiao Dong08b9ab92010-08-06 18:19:09 +08001494 /* support for multi nand
1495 * MTD known nothing about multi nand,
1496 * so we should tell it the real pagesize
1497 * and anything necessery
1498 */
1499 denali->devnum = ioread32(denali->flash_reg + DEVICES_CONNECTED);
1500 denali->nand.chipsize <<= (denali->devnum - 1);
1501 denali->nand.page_shift += (denali->devnum - 1);
1502 denali->nand.pagemask = (denali->nand.chipsize >>
1503 denali->nand.page_shift) - 1;
1504 denali->nand.bbt_erase_shift += (denali->devnum - 1);
1505 denali->nand.phys_erase_shift = denali->nand.bbt_erase_shift;
1506 denali->nand.chip_shift += (denali->devnum - 1);
1507 denali->mtd.writesize <<= (denali->devnum - 1);
1508 denali->mtd.oobsize <<= (denali->devnum - 1);
1509 denali->mtd.erasesize <<= (denali->devnum - 1);
1510 denali->mtd.size = denali->nand.numchips * denali->nand.chipsize;
1511 denali->bbtskipbytes *= denali->devnum;
1512
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001513 /* second stage of the NAND scan
1514 * this stage requires information regarding ECC and
1515 * bad block management. */
Jason Robertsce082592010-05-13 15:57:33 +01001516
1517 /* Bad block management */
1518 denali->nand.bbt_td = &bbt_main_descr;
1519 denali->nand.bbt_md = &bbt_mirror_descr;
1520
1521 /* skip the scan for now until we have OOB read and write support */
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001522 denali->nand.bbt_options |= NAND_BBT_USE_FLASH;
Brian Norrisa40f7342011-05-31 16:31:22 -07001523 denali->nand.options |= NAND_SKIP_BBTSCAN;
Jason Robertsce082592010-05-13 15:57:33 +01001524 denali->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
1525
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001526 /* Denali Controller only support 15bit and 8bit ECC in MRST,
1527 * so just let controller do 15bit ECC for MLC and 8bit ECC for
1528 * SLC if possible.
1529 * */
Huang Shijie1d0ed692013-09-25 14:58:10 +08001530 if (!nand_is_slc(&denali->nand) &&
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001531 (denali->mtd.oobsize > (denali->bbtskipbytes +
1532 ECC_15BITS * (denali->mtd.writesize /
1533 ECC_SECTOR_SIZE)))) {
1534 /* if MLC OOB size is large enough, use 15bit ECC*/
Mike Dunn6a918ba2012-03-11 14:21:11 -07001535 denali->nand.ecc.strength = 15;
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001536 denali->nand.ecc.layout = &nand_15bit_oob;
1537 denali->nand.ecc.bytes = ECC_15BITS;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001538 iowrite32(15, denali->flash_reg + ECC_CORRECTION);
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001539 } else if (denali->mtd.oobsize < (denali->bbtskipbytes +
1540 ECC_8BITS * (denali->mtd.writesize /
1541 ECC_SECTOR_SIZE))) {
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001542 pr_err("Your NAND chip OOB is not large enough to \
1543 contain 8bit ECC correction codes");
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001544 goto failed_req_irq;
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001545 } else {
Mike Dunn6a918ba2012-03-11 14:21:11 -07001546 denali->nand.ecc.strength = 8;
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001547 denali->nand.ecc.layout = &nand_8bit_oob;
1548 denali->nand.ecc.bytes = ECC_8BITS;
Chuanxiao Dong24c3fa32010-08-09 23:59:23 +08001549 iowrite32(8, denali->flash_reg + ECC_CORRECTION);
Jason Robertsce082592010-05-13 15:57:33 +01001550 }
1551
Chuanxiao Dong08b9ab92010-08-06 18:19:09 +08001552 denali->nand.ecc.bytes *= denali->devnum;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001553 denali->nand.ecc.strength *= denali->devnum;
Chuanxiao Dongdb9a3212010-08-06 18:02:03 +08001554 denali->nand.ecc.layout->eccbytes *=
1555 denali->mtd.writesize / ECC_SECTOR_SIZE;
1556 denali->nand.ecc.layout->oobfree[0].offset =
1557 denali->bbtskipbytes + denali->nand.ecc.layout->eccbytes;
1558 denali->nand.ecc.layout->oobfree[0].length =
1559 denali->mtd.oobsize - denali->nand.ecc.layout->eccbytes -
1560 denali->bbtskipbytes;
1561
Chuanxiao.Dong66406522010-08-06 18:48:21 +08001562 /* Let driver know the total blocks number and
1563 * how many blocks contained by each nand chip.
1564 * blksperchip will help driver to know how many
1565 * blocks is taken by FW.
1566 * */
1567 denali->totalblks = denali->mtd.size >>
1568 denali->nand.phys_erase_shift;
1569 denali->blksperchip = denali->totalblks / denali->nand.numchips;
1570
Chuanxiao5bac3ac2010-08-05 23:06:04 +08001571 /* These functions are required by the NAND core framework, otherwise,
1572 * the NAND core will assert. However, we don't need them, so we'll stub
1573 * them out. */
Jason Robertsce082592010-05-13 15:57:33 +01001574 denali->nand.ecc.calculate = denali_ecc_calculate;
1575 denali->nand.ecc.correct = denali_ecc_correct;
1576 denali->nand.ecc.hwctl = denali_ecc_hwctl;
1577
1578 /* override the default read operations */
Chuanxiao Dong08b9ab92010-08-06 18:19:09 +08001579 denali->nand.ecc.size = ECC_SECTOR_SIZE * denali->devnum;
Jason Robertsce082592010-05-13 15:57:33 +01001580 denali->nand.ecc.read_page = denali_read_page;
1581 denali->nand.ecc.read_page_raw = denali_read_page_raw;
1582 denali->nand.ecc.write_page = denali_write_page;
1583 denali->nand.ecc.write_page_raw = denali_write_page_raw;
1584 denali->nand.ecc.read_oob = denali_read_oob;
1585 denali->nand.ecc.write_oob = denali_write_oob;
Brian Norris49c50b92014-05-06 16:02:19 -07001586 denali->nand.erase = denali_erase;
Jason Robertsce082592010-05-13 15:57:33 +01001587
Chuanxiao Dong345b1d32010-07-27 10:41:53 +08001588 if (nand_scan_tail(&denali->mtd)) {
Jason Robertsce082592010-05-13 15:57:33 +01001589 ret = -ENXIO;
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001590 goto failed_req_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001591 }
1592
Jamie Ilesee0e87b2011-05-23 10:23:40 +01001593 ret = mtd_device_register(&denali->mtd, NULL, 0);
Jason Robertsce082592010-05-13 15:57:33 +01001594 if (ret) {
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001595 dev_err(denali->dev, "Spectra: Failed to register MTD: %d\n",
Chuanxiao Dong7cfffac2010-08-10 00:16:51 +08001596 ret);
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001597 goto failed_req_irq;
Jason Robertsce082592010-05-13 15:57:33 +01001598 }
1599 return 0;
1600
Chuanxiao Dong5c0eb902010-08-09 18:37:00 +08001601failed_req_irq:
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001602 denali_irq_cleanup(denali->irq, denali);
1603
Jason Robertsce082592010-05-13 15:57:33 +01001604 return ret;
1605}
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001606EXPORT_SYMBOL(denali_init);
Jason Robertsce082592010-05-13 15:57:33 +01001607
1608/* driver exit point */
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001609void denali_remove(struct denali_nand_info *denali)
Jason Robertsce082592010-05-13 15:57:33 +01001610{
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001611 denali_irq_cleanup(denali->irq, denali);
Huang Shijiee07caa32013-12-21 00:02:28 +08001612 dma_unmap_single(denali->dev, denali->buf.dma_buf,
1613 denali->mtd.writesize + denali->mtd.oobsize,
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001614 DMA_BIDIRECTIONAL);
Jason Robertsce082592010-05-13 15:57:33 +01001615}
Dinh Nguyen2a0a2882012-09-27 10:58:05 -06001616EXPORT_SYMBOL(denali_remove);