blob: 54718f418c669f6bc50fe91385c7dee09f0d06be [file] [log] [blame]
Chin Liang See03534df2014-09-12 00:42:17 -05001/*
2 * Copyright (C) 2014 Panasonic Corporation
3 * Copyright (C) 2013-2014, Altera Corporation <www.altera.com>
4 * Copyright (C) 2009-2010, Intel Corporation and its suppliers.
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <malloc.h>
11#include <nand.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090012#include <linux/errno.h>
Masahiro Yamada54fde8e2017-09-15 21:43:19 +090013#include <linux/io.h>
Chin Liang See03534df2014-09-12 00:42:17 -050014
15#include "denali.h"
16
17#define NAND_DEFAULT_TIMINGS -1
18
19static int onfi_timing_mode = NAND_DEFAULT_TIMINGS;
20
Scott Wood3ea94ed2015-06-26 19:03:26 -050021/*
22 * We define a macro here that combines all interrupts this driver uses into
23 * a single constant value, for convenience.
24 */
Chin Liang See03534df2014-09-12 00:42:17 -050025#define DENALI_IRQ_ALL (INTR_STATUS__DMA_CMD_COMP | \
26 INTR_STATUS__ECC_TRANSACTION_DONE | \
27 INTR_STATUS__ECC_ERR | \
28 INTR_STATUS__PROGRAM_FAIL | \
29 INTR_STATUS__LOAD_COMP | \
30 INTR_STATUS__PROGRAM_COMP | \
31 INTR_STATUS__TIME_OUT | \
32 INTR_STATUS__ERASE_FAIL | \
33 INTR_STATUS__RST_COMP | \
34 INTR_STATUS__ERASE_COMP | \
35 INTR_STATUS__ECC_UNCOR_ERR | \
36 INTR_STATUS__INT_ACT | \
37 INTR_STATUS__LOCKED_BLK)
38
Scott Wood3ea94ed2015-06-26 19:03:26 -050039/*
40 * indicates whether or not the internal value for the flash bank is
41 * valid or not
42 */
Chin Liang See03534df2014-09-12 00:42:17 -050043#define CHIP_SELECT_INVALID -1
44
45#define SUPPORT_8BITECC 1
46
47/*
48 * this macro allows us to convert from an MTD structure to our own
49 * device context (denali) structure.
50 */
Scott Wood52ab7ce2016-05-30 13:57:58 -050051static inline struct denali_nand_info *mtd_to_denali(struct mtd_info *mtd)
52{
53 return container_of(mtd_to_nand(mtd), struct denali_nand_info, nand);
54}
Chin Liang See03534df2014-09-12 00:42:17 -050055
Scott Wood3ea94ed2015-06-26 19:03:26 -050056/*
57 * These constants are defined by the driver to enable common driver
58 * configuration options.
59 */
Chin Liang See03534df2014-09-12 00:42:17 -050060#define SPARE_ACCESS 0x41
61#define MAIN_ACCESS 0x42
62#define MAIN_SPARE_ACCESS 0x43
Scott Wood3ea94ed2015-06-26 19:03:26 -050063#define PIPELINE_ACCESS 0x2000
Chin Liang See03534df2014-09-12 00:42:17 -050064
65#define DENALI_UNLOCK_START 0x10
66#define DENALI_UNLOCK_END 0x11
67#define DENALI_LOCK 0x21
68#define DENALI_LOCK_TIGHT 0x31
69#define DENALI_BUFFER_LOAD 0x60
70#define DENALI_BUFFER_WRITE 0x62
71
72#define DENALI_READ 0
73#define DENALI_WRITE 0x100
74
75/* types of device accesses. We can issue commands and get status */
76#define COMMAND_CYCLE 0
77#define ADDR_CYCLE 1
78#define STATUS_CYCLE 2
79
Scott Wood3ea94ed2015-06-26 19:03:26 -050080/*
81 * this is a helper macro that allows us to
82 * format the bank into the proper bits for the controller
83 */
Chin Liang See03534df2014-09-12 00:42:17 -050084#define BANK(x) ((x) << 24)
85
86/* Interrupts are cleared by writing a 1 to the appropriate status bit */
87static inline void clear_interrupt(struct denali_nand_info *denali,
88 uint32_t irq_mask)
89{
90 uint32_t intr_status_reg;
91
92 intr_status_reg = INTR_STATUS(denali->flash_bank);
93
94 writel(irq_mask, denali->flash_reg + intr_status_reg);
95}
96
97static uint32_t read_interrupt_status(struct denali_nand_info *denali)
98{
99 uint32_t intr_status_reg;
100
101 intr_status_reg = INTR_STATUS(denali->flash_bank);
102
103 return readl(denali->flash_reg + intr_status_reg);
104}
105
106static void clear_interrupts(struct denali_nand_info *denali)
107{
108 uint32_t status;
109
110 status = read_interrupt_status(denali);
111 clear_interrupt(denali, status);
112
113 denali->irq_status = 0;
114}
115
116static void denali_irq_enable(struct denali_nand_info *denali,
117 uint32_t int_mask)
118{
119 int i;
120
121 for (i = 0; i < denali->max_banks; ++i)
122 writel(int_mask, denali->flash_reg + INTR_EN(i));
123}
124
125static uint32_t wait_for_irq(struct denali_nand_info *denali, uint32_t irq_mask)
126{
127 unsigned long timeout = 1000000;
128 uint32_t intr_status;
129
130 do {
131 intr_status = read_interrupt_status(denali) & DENALI_IRQ_ALL;
132 if (intr_status & irq_mask) {
133 denali->irq_status &= ~irq_mask;
134 /* our interrupt was detected */
135 break;
136 }
137 udelay(1);
138 timeout--;
139 } while (timeout != 0);
140
141 if (timeout == 0) {
142 /* timeout */
143 printf("Denali timeout with interrupt status %08x\n",
144 read_interrupt_status(denali));
145 intr_status = 0;
146 }
147 return intr_status;
148}
149
150/*
151 * Certain operations for the denali NAND controller use an indexed mode to
152 * read/write data. The operation is performed by writing the address value
153 * of the command to the device memory followed by the data. This function
154 * abstracts this common operation.
Scott Wood3ea94ed2015-06-26 19:03:26 -0500155 */
Chin Liang See03534df2014-09-12 00:42:17 -0500156static void index_addr(struct denali_nand_info *denali,
157 uint32_t address, uint32_t data)
158{
159 writel(address, denali->flash_mem + INDEX_CTRL_REG);
160 writel(data, denali->flash_mem + INDEX_DATA_REG);
161}
162
163/* Perform an indexed read of the device */
164static void index_addr_read_data(struct denali_nand_info *denali,
165 uint32_t address, uint32_t *pdata)
166{
167 writel(address, denali->flash_mem + INDEX_CTRL_REG);
168 *pdata = readl(denali->flash_mem + INDEX_DATA_REG);
169}
170
Scott Wood3ea94ed2015-06-26 19:03:26 -0500171/*
172 * We need to buffer some data for some of the NAND core routines.
173 * The operations manage buffering that data.
174 */
Chin Liang See03534df2014-09-12 00:42:17 -0500175static void reset_buf(struct denali_nand_info *denali)
176{
177 denali->buf.head = 0;
178 denali->buf.tail = 0;
179}
180
181static void write_byte_to_buf(struct denali_nand_info *denali, uint8_t byte)
182{
183 denali->buf.buf[denali->buf.tail++] = byte;
184}
185
186/* resets a specific device connected to the core */
187static void reset_bank(struct denali_nand_info *denali)
188{
189 uint32_t irq_status;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500190 uint32_t irq_mask = INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT;
Chin Liang See03534df2014-09-12 00:42:17 -0500191
192 clear_interrupts(denali);
193
194 writel(1 << denali->flash_bank, denali->flash_reg + DEVICE_RESET);
195
196 irq_status = wait_for_irq(denali, irq_mask);
197 if (irq_status & INTR_STATUS__TIME_OUT)
198 debug("reset bank failed.\n");
199}
200
201/* Reset the flash controller */
202static uint32_t denali_nand_reset(struct denali_nand_info *denali)
203{
Scott Wood3ea94ed2015-06-26 19:03:26 -0500204 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500205
206 for (i = 0; i < denali->max_banks; i++)
207 writel(INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT,
208 denali->flash_reg + INTR_STATUS(i));
209
210 for (i = 0; i < denali->max_banks; i++) {
211 writel(1 << i, denali->flash_reg + DEVICE_RESET);
212 while (!(readl(denali->flash_reg + INTR_STATUS(i)) &
213 (INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT)))
214 if (readl(denali->flash_reg + INTR_STATUS(i)) &
215 INTR_STATUS__TIME_OUT)
216 debug("NAND Reset operation timed out on bank"
217 " %d\n", i);
218 }
219
220 for (i = 0; i < denali->max_banks; i++)
221 writel(INTR_STATUS__RST_COMP | INTR_STATUS__TIME_OUT,
222 denali->flash_reg + INTR_STATUS(i));
223
224 return 0;
225}
226
227/*
228 * this routine calculates the ONFI timing values for a given mode and
229 * programs the clocking register accordingly. The mode is determined by
230 * the get_onfi_nand_para routine.
231 */
232static void nand_onfi_timing_set(struct denali_nand_info *denali,
233 uint16_t mode)
234{
235 uint32_t trea[6] = {40, 30, 25, 20, 20, 16};
236 uint32_t trp[6] = {50, 25, 17, 15, 12, 10};
237 uint32_t treh[6] = {30, 15, 15, 10, 10, 7};
238 uint32_t trc[6] = {100, 50, 35, 30, 25, 20};
239 uint32_t trhoh[6] = {0, 15, 15, 15, 15, 15};
240 uint32_t trloh[6] = {0, 0, 0, 0, 5, 5};
241 uint32_t tcea[6] = {100, 45, 30, 25, 25, 25};
242 uint32_t tadl[6] = {200, 100, 100, 100, 70, 70};
243 uint32_t trhw[6] = {200, 100, 100, 100, 100, 100};
244 uint32_t trhz[6] = {200, 100, 100, 100, 100, 100};
245 uint32_t twhr[6] = {120, 80, 80, 60, 60, 60};
246 uint32_t tcs[6] = {70, 35, 25, 25, 20, 15};
247
Chin Liang See03534df2014-09-12 00:42:17 -0500248 uint32_t data_invalid_rhoh, data_invalid_rloh, data_invalid;
249 uint32_t dv_window = 0;
250 uint32_t en_lo, en_hi;
251 uint32_t acc_clks;
252 uint32_t addr_2_data, re_2_we, re_2_re, we_2_re, cs_cnt;
253
254 en_lo = DIV_ROUND_UP(trp[mode], CLK_X);
255 en_hi = DIV_ROUND_UP(treh[mode], CLK_X);
256 if ((en_hi * CLK_X) < (treh[mode] + 2))
257 en_hi++;
258
259 if ((en_lo + en_hi) * CLK_X < trc[mode])
260 en_lo += DIV_ROUND_UP((trc[mode] - (en_lo + en_hi) * CLK_X),
261 CLK_X);
262
263 if ((en_lo + en_hi) < CLK_MULTI)
264 en_lo += CLK_MULTI - en_lo - en_hi;
265
266 while (dv_window < 8) {
267 data_invalid_rhoh = en_lo * CLK_X + trhoh[mode];
268
269 data_invalid_rloh = (en_lo + en_hi) * CLK_X + trloh[mode];
270
Scott Wood3ea94ed2015-06-26 19:03:26 -0500271 data_invalid = data_invalid_rhoh < data_invalid_rloh ?
272 data_invalid_rhoh : data_invalid_rloh;
Chin Liang See03534df2014-09-12 00:42:17 -0500273
274 dv_window = data_invalid - trea[mode];
275
276 if (dv_window < 8)
277 en_lo++;
278 }
279
280 acc_clks = DIV_ROUND_UP(trea[mode], CLK_X);
281
Scott Wood3ea94ed2015-06-26 19:03:26 -0500282 while (acc_clks * CLK_X - trea[mode] < 3)
Chin Liang See03534df2014-09-12 00:42:17 -0500283 acc_clks++;
284
Scott Wood3ea94ed2015-06-26 19:03:26 -0500285 if (data_invalid - acc_clks * CLK_X < 2)
Chin Liang See03534df2014-09-12 00:42:17 -0500286 debug("%s, Line %d: Warning!\n", __FILE__, __LINE__);
287
288 addr_2_data = DIV_ROUND_UP(tadl[mode], CLK_X);
289 re_2_we = DIV_ROUND_UP(trhw[mode], CLK_X);
290 re_2_re = DIV_ROUND_UP(trhz[mode], CLK_X);
291 we_2_re = DIV_ROUND_UP(twhr[mode], CLK_X);
292 cs_cnt = DIV_ROUND_UP((tcs[mode] - trp[mode]), CLK_X);
Chin Liang See03534df2014-09-12 00:42:17 -0500293 if (cs_cnt == 0)
294 cs_cnt = 1;
295
296 if (tcea[mode]) {
Scott Wood3ea94ed2015-06-26 19:03:26 -0500297 while (cs_cnt * CLK_X + trea[mode] < tcea[mode])
Chin Liang See03534df2014-09-12 00:42:17 -0500298 cs_cnt++;
299 }
300
301 /* Sighting 3462430: Temporary hack for MT29F128G08CJABAWP:B */
Scott Wood3ea94ed2015-06-26 19:03:26 -0500302 if (readl(denali->flash_reg + MANUFACTURER_ID) == 0 &&
303 readl(denali->flash_reg + DEVICE_ID) == 0x88)
Chin Liang See03534df2014-09-12 00:42:17 -0500304 acc_clks = 6;
305
306 writel(acc_clks, denali->flash_reg + ACC_CLKS);
307 writel(re_2_we, denali->flash_reg + RE_2_WE);
308 writel(re_2_re, denali->flash_reg + RE_2_RE);
309 writel(we_2_re, denali->flash_reg + WE_2_RE);
310 writel(addr_2_data, denali->flash_reg + ADDR_2_DATA);
311 writel(en_lo, denali->flash_reg + RDWR_EN_LO_CNT);
312 writel(en_hi, denali->flash_reg + RDWR_EN_HI_CNT);
313 writel(cs_cnt, denali->flash_reg + CS_SETUP_CNT);
314}
315
316/* queries the NAND device to see what ONFI modes it supports. */
317static uint32_t get_onfi_nand_para(struct denali_nand_info *denali)
318{
319 int i;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500320
Chin Liang See03534df2014-09-12 00:42:17 -0500321 /*
322 * we needn't to do a reset here because driver has already
323 * reset all the banks before
324 */
325 if (!(readl(denali->flash_reg + ONFI_TIMING_MODE) &
326 ONFI_TIMING_MODE__VALUE))
327 return -EIO;
328
329 for (i = 5; i > 0; i--) {
330 if (readl(denali->flash_reg + ONFI_TIMING_MODE) &
331 (0x01 << i))
332 break;
333 }
334
335 nand_onfi_timing_set(denali, i);
336
Scott Wood3ea94ed2015-06-26 19:03:26 -0500337 /*
338 * By now, all the ONFI devices we know support the page cache
339 * rw feature. So here we enable the pipeline_rw_ahead feature
340 */
341
Chin Liang See03534df2014-09-12 00:42:17 -0500342 return 0;
343}
344
345static void get_samsung_nand_para(struct denali_nand_info *denali,
346 uint8_t device_id)
347{
348 if (device_id == 0xd3) { /* Samsung K9WAG08U1A */
349 /* Set timing register values according to datasheet */
350 writel(5, denali->flash_reg + ACC_CLKS);
351 writel(20, denali->flash_reg + RE_2_WE);
352 writel(12, denali->flash_reg + WE_2_RE);
353 writel(14, denali->flash_reg + ADDR_2_DATA);
354 writel(3, denali->flash_reg + RDWR_EN_LO_CNT);
355 writel(2, denali->flash_reg + RDWR_EN_HI_CNT);
356 writel(2, denali->flash_reg + CS_SETUP_CNT);
357 }
358}
359
360static void get_toshiba_nand_para(struct denali_nand_info *denali)
361{
362 uint32_t tmp;
363
Scott Wood3ea94ed2015-06-26 19:03:26 -0500364 /*
365 * Workaround to fix a controller bug which reports a wrong
366 * spare area size for some kind of Toshiba NAND device
367 */
Chin Liang See03534df2014-09-12 00:42:17 -0500368 if ((readl(denali->flash_reg + DEVICE_MAIN_AREA_SIZE) == 4096) &&
369 (readl(denali->flash_reg + DEVICE_SPARE_AREA_SIZE) == 64)) {
370 writel(216, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
371 tmp = readl(denali->flash_reg + DEVICES_CONNECTED) *
372 readl(denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
373 writel(tmp, denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
374 }
375}
376
377static void get_hynix_nand_para(struct denali_nand_info *denali,
378 uint8_t device_id)
379{
380 uint32_t main_size, spare_size;
381
382 switch (device_id) {
383 case 0xD5: /* Hynix H27UAG8T2A, H27UBG8U5A or H27UCG8VFA */
384 case 0xD7: /* Hynix H27UDG8VEM, H27UCG8UDM or H27UCG8V5A */
385 writel(128, denali->flash_reg + PAGES_PER_BLOCK);
386 writel(4096, denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
387 writel(224, denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
388 main_size = 4096 *
389 readl(denali->flash_reg + DEVICES_CONNECTED);
390 spare_size = 224 *
391 readl(denali->flash_reg + DEVICES_CONNECTED);
392 writel(main_size, denali->flash_reg + LOGICAL_PAGE_DATA_SIZE);
393 writel(spare_size, denali->flash_reg + LOGICAL_PAGE_SPARE_SIZE);
394 writel(0, denali->flash_reg + DEVICE_WIDTH);
395 break;
396 default:
Scott Wood3ea94ed2015-06-26 19:03:26 -0500397 debug("Spectra: Unknown Hynix NAND (Device ID: 0x%x).\n"
Chin Liang See03534df2014-09-12 00:42:17 -0500398 "Will use default parameter values instead.\n",
399 device_id);
400 }
401}
402
403/*
404 * determines how many NAND chips are connected to the controller. Note for
405 * Intel CE4100 devices we don't support more than one device.
406 */
407static void find_valid_banks(struct denali_nand_info *denali)
408{
409 uint32_t id[denali->max_banks];
410 int i;
411
412 denali->total_used_banks = 1;
413 for (i = 0; i < denali->max_banks; i++) {
Scott Wood3ea94ed2015-06-26 19:03:26 -0500414 index_addr(denali, MODE_11 | (i << 24) | 0, 0x90);
415 index_addr(denali, MODE_11 | (i << 24) | 1, 0);
416 index_addr_read_data(denali, MODE_11 | (i << 24) | 2, &id[i]);
Chin Liang See03534df2014-09-12 00:42:17 -0500417
418 if (i == 0) {
419 if (!(id[i] & 0x0ff))
420 break;
421 } else {
422 if ((id[i] & 0x0ff) == (id[0] & 0x0ff))
423 denali->total_used_banks++;
424 else
425 break;
426 }
427 }
428}
429
430/*
431 * Use the configuration feature register to determine the maximum number of
432 * banks that the hardware supports.
433 */
434static void detect_max_banks(struct denali_nand_info *denali)
435{
Masahiro Yamada54fde8e2017-09-15 21:43:19 +0900436 uint32_t features = ioread32(denali->flash_reg + FEATURES);
437
438 denali->max_banks = 1 << (features & FEATURES__N_BANKS);
439
440 /* the encoding changed from rev 5.0 to 5.1 */
441 if (denali->revision < 0x0501)
442 denali->max_banks <<= 1;
Chin Liang See03534df2014-09-12 00:42:17 -0500443}
444
445static void detect_partition_feature(struct denali_nand_info *denali)
446{
447 /*
448 * For MRST platform, denali->fwblks represent the
449 * number of blocks firmware is taken,
450 * FW is in protect partition and MTD driver has no
451 * permission to access it. So let driver know how many
452 * blocks it can't touch.
453 */
454 if (readl(denali->flash_reg + FEATURES) & FEATURES__PARTITION) {
455 if ((readl(denali->flash_reg + PERM_SRC_ID(1)) &
456 PERM_SRC_ID__SRCID) == SPECTRA_PARTITION_ID) {
457 denali->fwblks =
458 ((readl(denali->flash_reg + MIN_MAX_BANK(1)) &
459 MIN_MAX_BANK__MIN_VALUE) *
460 denali->blksperchip)
461 +
462 (readl(denali->flash_reg + MIN_BLK_ADDR(1)) &
463 MIN_BLK_ADDR__VALUE);
464 } else {
465 denali->fwblks = SPECTRA_START_BLOCK;
466 }
467 } else {
468 denali->fwblks = SPECTRA_START_BLOCK;
469 }
470}
471
472static uint32_t denali_nand_timing_set(struct denali_nand_info *denali)
473{
Scott Wood3ea94ed2015-06-26 19:03:26 -0500474 uint32_t id_bytes[8], addr;
475 uint8_t maf_id, device_id;
476 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500477
Scott Wood3ea94ed2015-06-26 19:03:26 -0500478 /*
479 * Use read id method to get device ID and other params.
480 * For some NAND chips, controller can't report the correct
481 * device ID by reading from DEVICE_ID register
482 */
483 addr = MODE_11 | BANK(denali->flash_bank);
484 index_addr(denali, addr | 0, 0x90);
485 index_addr(denali, addr | 1, 0);
486 for (i = 0; i < 8; i++)
Chin Liang See03534df2014-09-12 00:42:17 -0500487 index_addr_read_data(denali, addr | 2, &id_bytes[i]);
488 maf_id = id_bytes[0];
489 device_id = id_bytes[1];
490
491 if (readl(denali->flash_reg + ONFI_DEVICE_NO_OF_LUNS) &
492 ONFI_DEVICE_NO_OF_LUNS__ONFI_DEVICE) { /* ONFI 1.0 NAND */
493 if (get_onfi_nand_para(denali))
494 return -EIO;
495 } else if (maf_id == 0xEC) { /* Samsung NAND */
496 get_samsung_nand_para(denali, device_id);
497 } else if (maf_id == 0x98) { /* Toshiba NAND */
498 get_toshiba_nand_para(denali);
499 } else if (maf_id == 0xAD) { /* Hynix NAND */
500 get_hynix_nand_para(denali, device_id);
501 }
502
503 find_valid_banks(denali);
504
505 detect_partition_feature(denali);
506
Scott Wood3ea94ed2015-06-26 19:03:26 -0500507 /*
508 * If the user specified to override the default timings
Chin Liang See03534df2014-09-12 00:42:17 -0500509 * with a specific ONFI mode, we apply those changes here.
510 */
511 if (onfi_timing_mode != NAND_DEFAULT_TIMINGS)
512 nand_onfi_timing_set(denali, onfi_timing_mode);
513
514 return 0;
515}
516
Scott Wood3ea94ed2015-06-26 19:03:26 -0500517/*
518 * validation function to verify that the controlling software is making
Chin Liang See03534df2014-09-12 00:42:17 -0500519 * a valid request
520 */
521static inline bool is_flash_bank_valid(int flash_bank)
522{
523 return flash_bank >= 0 && flash_bank < 4;
524}
525
526static void denali_irq_init(struct denali_nand_info *denali)
527{
Scott Wood3ea94ed2015-06-26 19:03:26 -0500528 uint32_t int_mask;
Chin Liang See03534df2014-09-12 00:42:17 -0500529 int i;
530
531 /* Disable global interrupts */
532 writel(0, denali->flash_reg + GLOBAL_INT_ENABLE);
533
534 int_mask = DENALI_IRQ_ALL;
535
536 /* Clear all status bits */
537 for (i = 0; i < denali->max_banks; ++i)
538 writel(0xFFFF, denali->flash_reg + INTR_STATUS(i));
539
540 denali_irq_enable(denali, int_mask);
541}
542
Scott Wood3ea94ed2015-06-26 19:03:26 -0500543/*
544 * This helper function setups the registers for ECC and whether or not
545 * the spare area will be transferred.
546 */
Chin Liang See03534df2014-09-12 00:42:17 -0500547static void setup_ecc_for_xfer(struct denali_nand_info *denali, bool ecc_en,
548 bool transfer_spare)
549{
Scott Wood3ea94ed2015-06-26 19:03:26 -0500550 int ecc_en_flag, transfer_spare_flag;
Chin Liang See03534df2014-09-12 00:42:17 -0500551
552 /* set ECC, transfer spare bits if needed */
553 ecc_en_flag = ecc_en ? ECC_ENABLE__FLAG : 0;
554 transfer_spare_flag = transfer_spare ? TRANSFER_SPARE_REG__FLAG : 0;
555
556 /* Enable spare area/ECC per user's request. */
557 writel(ecc_en_flag, denali->flash_reg + ECC_ENABLE);
558 /* applicable for MAP01 only */
559 writel(transfer_spare_flag, denali->flash_reg + TRANSFER_SPARE_REG);
560}
561
Scott Wood3ea94ed2015-06-26 19:03:26 -0500562/*
563 * sends a pipeline command operation to the controller. See the Denali NAND
Chin Liang See03534df2014-09-12 00:42:17 -0500564 * controller's user guide for more information (section 4.2.3.6).
565 */
566static int denali_send_pipeline_cmd(struct denali_nand_info *denali,
Scott Wood3ea94ed2015-06-26 19:03:26 -0500567 bool ecc_en, bool transfer_spare,
568 int access_type, int op)
Chin Liang See03534df2014-09-12 00:42:17 -0500569{
570 uint32_t addr, cmd, irq_status;
571 static uint32_t page_count = 1;
572
573 setup_ecc_for_xfer(denali, ecc_en, transfer_spare);
574
Chin Liang See03534df2014-09-12 00:42:17 -0500575 clear_interrupts(denali);
576
577 addr = BANK(denali->flash_bank) | denali->page;
578
579 /* setup the acccess type */
580 cmd = MODE_10 | addr;
581 index_addr(denali, cmd, access_type);
582
583 /* setup the pipeline command */
584 index_addr(denali, cmd, 0x2000 | op | page_count);
585
586 cmd = MODE_01 | addr;
587 writel(cmd, denali->flash_mem + INDEX_CTRL_REG);
588
589 if (op == DENALI_READ) {
590 /* wait for command to be accepted */
591 irq_status = wait_for_irq(denali, INTR_STATUS__LOAD_COMP);
592
593 if (irq_status == 0)
594 return -EIO;
595 }
596
597 return 0;
598}
599
600/* helper function that simply writes a buffer to the flash */
601static int write_data_to_flash_mem(struct denali_nand_info *denali,
Scott Wood3ea94ed2015-06-26 19:03:26 -0500602 const uint8_t *buf, int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500603{
Scott Wood3ea94ed2015-06-26 19:03:26 -0500604 uint32_t *buf32;
605 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500606
Scott Wood3ea94ed2015-06-26 19:03:26 -0500607 /*
608 * verify that the len is a multiple of 4.
609 * see comment in read_data_from_flash_mem()
610 */
Chin Liang See03534df2014-09-12 00:42:17 -0500611 BUG_ON((len % 4) != 0);
612
613 /* write the data to the flash memory */
614 buf32 = (uint32_t *)buf;
615 for (i = 0; i < len / 4; i++)
616 writel(*buf32++, denali->flash_mem + INDEX_DATA_REG);
617 return i * 4; /* intent is to return the number of bytes read */
618}
619
620/* helper function that simply reads a buffer from the flash */
621static int read_data_from_flash_mem(struct denali_nand_info *denali,
Scott Wood3ea94ed2015-06-26 19:03:26 -0500622 uint8_t *buf, int len)
Chin Liang See03534df2014-09-12 00:42:17 -0500623{
Scott Wood3ea94ed2015-06-26 19:03:26 -0500624 uint32_t *buf32;
625 int i;
Chin Liang See03534df2014-09-12 00:42:17 -0500626
627 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -0500628 * we assume that len will be a multiple of 4, if not it would be nice
629 * to know about it ASAP rather than have random failures...
630 * This assumption is based on the fact that this function is designed
631 * to be used to read flash pages, which are typically multiples of 4.
Chin Liang See03534df2014-09-12 00:42:17 -0500632 */
Chin Liang See03534df2014-09-12 00:42:17 -0500633 BUG_ON((len % 4) != 0);
634
635 /* transfer the data from the flash */
636 buf32 = (uint32_t *)buf;
637 for (i = 0; i < len / 4; i++)
638 *buf32++ = readl(denali->flash_mem + INDEX_DATA_REG);
639
640 return i * 4; /* intent is to return the number of bytes read */
641}
642
643static void denali_mode_main_access(struct denali_nand_info *denali)
644{
645 uint32_t addr, cmd;
646
647 addr = BANK(denali->flash_bank) | denali->page;
648 cmd = MODE_10 | addr;
649 index_addr(denali, cmd, MAIN_ACCESS);
650}
651
652static void denali_mode_main_spare_access(struct denali_nand_info *denali)
653{
654 uint32_t addr, cmd;
655
656 addr = BANK(denali->flash_bank) | denali->page;
657 cmd = MODE_10 | addr;
658 index_addr(denali, cmd, MAIN_SPARE_ACCESS);
659}
660
661/* writes OOB data to the device */
662static int write_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
663{
664 struct denali_nand_info *denali = mtd_to_denali(mtd);
665 uint32_t irq_status;
666 uint32_t irq_mask = INTR_STATUS__PROGRAM_COMP |
667 INTR_STATUS__PROGRAM_FAIL;
668 int status = 0;
669
670 denali->page = page;
671
672 if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
673 DENALI_WRITE) == 0) {
674 write_data_to_flash_mem(denali, buf, mtd->oobsize);
675
676 /* wait for operation to complete */
677 irq_status = wait_for_irq(denali, irq_mask);
678
679 if (irq_status == 0) {
680 dev_err(denali->dev, "OOB write failed\n");
681 status = -EIO;
682 }
683 } else {
684 printf("unable to send pipeline command\n");
685 status = -EIO;
686 }
687 return status;
688}
689
690/* reads OOB data from the device */
691static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
692{
693 struct denali_nand_info *denali = mtd_to_denali(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500694 uint32_t irq_mask = INTR_STATUS__LOAD_COMP;
695 uint32_t irq_status, addr, cmd;
Chin Liang See03534df2014-09-12 00:42:17 -0500696
697 denali->page = page;
698
699 if (denali_send_pipeline_cmd(denali, false, true, SPARE_ACCESS,
700 DENALI_READ) == 0) {
701 read_data_from_flash_mem(denali, buf, mtd->oobsize);
702
Scott Wood3ea94ed2015-06-26 19:03:26 -0500703 /*
704 * wait for command to be accepted
705 * can always use status0 bit as the
706 * mask is identical for each bank.
707 */
Chin Liang See03534df2014-09-12 00:42:17 -0500708 irq_status = wait_for_irq(denali, irq_mask);
709
710 if (irq_status == 0)
711 printf("page on OOB timeout %d\n", denali->page);
712
Scott Wood3ea94ed2015-06-26 19:03:26 -0500713 /*
714 * We set the device back to MAIN_ACCESS here as I observed
Chin Liang See03534df2014-09-12 00:42:17 -0500715 * instability with the controller if you do a block erase
716 * and the last transaction was a SPARE_ACCESS. Block erase
717 * is reliable (according to the MTD test infrastructure)
718 * if you are in MAIN_ACCESS.
719 */
720 addr = BANK(denali->flash_bank) | denali->page;
721 cmd = MODE_10 | addr;
722 index_addr(denali, cmd, MAIN_ACCESS);
723 }
724}
725
Scott Wood3ea94ed2015-06-26 19:03:26 -0500726/*
727 * this function examines buffers to see if they contain data that
Chin Liang See03534df2014-09-12 00:42:17 -0500728 * indicate that the buffer is part of an erased region of flash.
729 */
730static bool is_erased(uint8_t *buf, int len)
731{
Scott Wood3ea94ed2015-06-26 19:03:26 -0500732 int i;
733
Chin Liang See03534df2014-09-12 00:42:17 -0500734 for (i = 0; i < len; i++)
735 if (buf[i] != 0xFF)
736 return false;
737 return true;
738}
739
740/* programs the controller to either enable/disable DMA transfers */
741static void denali_enable_dma(struct denali_nand_info *denali, bool en)
742{
Scott Wood3ea94ed2015-06-26 19:03:26 -0500743 writel(en ? DMA_ENABLE__FLAG : 0, denali->flash_reg + DMA_ENABLE);
Chin Liang See03534df2014-09-12 00:42:17 -0500744 readl(denali->flash_reg + DMA_ENABLE);
745}
746
747/* setups the HW to perform the data DMA */
748static void denali_setup_dma(struct denali_nand_info *denali, int op)
749{
750 uint32_t mode;
751 const int page_count = 1;
Masahiro Yamada4108dc82016-02-29 20:57:29 +0900752 uint64_t addr = (unsigned long)denali->buf.dma_buf;
Chin Liang See03534df2014-09-12 00:42:17 -0500753
754 flush_dcache_range(addr, addr + sizeof(denali->buf.dma_buf));
755
756/* For Denali controller that is 64 bit bus IP core */
757#ifdef CONFIG_SYS_NAND_DENALI_64BIT
758 mode = MODE_10 | BANK(denali->flash_bank) | denali->page;
759
760 /* DMA is a three step process */
761
762 /* 1. setup transfer type, interrupt when complete,
763 burst len = 64 bytes, the number of pages */
764 index_addr(denali, mode, 0x01002000 | (64 << 16) | op | page_count);
765
766 /* 2. set memory low address bits 31:0 */
767 index_addr(denali, mode, addr);
768
769 /* 3. set memory high address bits 64:32 */
Masahiro Yamada4108dc82016-02-29 20:57:29 +0900770 index_addr(denali, mode, addr >> 32);
Chin Liang See03534df2014-09-12 00:42:17 -0500771#else
772 mode = MODE_10 | BANK(denali->flash_bank);
773
774 /* DMA is a four step process */
775
776 /* 1. setup transfer type and # of pages */
777 index_addr(denali, mode | denali->page, 0x2000 | op | page_count);
778
779 /* 2. set memory high address bits 23:8 */
Masahiro Yamada4108dc82016-02-29 20:57:29 +0900780 index_addr(denali, mode | (((addr >> 16) & 0xffff) << 8), 0x2200);
Chin Liang See03534df2014-09-12 00:42:17 -0500781
782 /* 3. set memory low address bits 23:8 */
Scott Wood3ea94ed2015-06-26 19:03:26 -0500783 index_addr(denali, mode | ((addr & 0xffff) << 8), 0x2300);
Chin Liang See03534df2014-09-12 00:42:17 -0500784
Scott Wood3ea94ed2015-06-26 19:03:26 -0500785 /* 4. interrupt when complete, burst len = 64 bytes */
Chin Liang See03534df2014-09-12 00:42:17 -0500786 index_addr(denali, mode | 0x14000, 0x2400);
787#endif
788}
789
790/* Common DMA function */
791static uint32_t denali_dma_configuration(struct denali_nand_info *denali,
792 uint32_t ops, bool raw_xfer,
793 uint32_t irq_mask, int oob_required)
794{
795 uint32_t irq_status = 0;
796 /* setup_ecc_for_xfer(bool ecc_en, bool transfer_spare) */
797 setup_ecc_for_xfer(denali, !raw_xfer, oob_required);
798
799 /* clear any previous interrupt flags */
800 clear_interrupts(denali);
801
802 /* enable the DMA */
803 denali_enable_dma(denali, true);
804
805 /* setup the DMA */
806 denali_setup_dma(denali, ops);
807
808 /* wait for operation to complete */
809 irq_status = wait_for_irq(denali, irq_mask);
810
811 /* if ECC fault happen, seems we need delay before turning off DMA.
812 * If not, the controller will go into non responsive condition */
813 if (irq_status & INTR_STATUS__ECC_UNCOR_ERR)
814 udelay(100);
815
816 /* disable the DMA */
817 denali_enable_dma(denali, false);
818
819 return irq_status;
820}
821
822static int write_page(struct mtd_info *mtd, struct nand_chip *chip,
823 const uint8_t *buf, bool raw_xfer, int oob_required)
824{
825 struct denali_nand_info *denali = mtd_to_denali(mtd);
826
827 uint32_t irq_status = 0;
828 uint32_t irq_mask = INTR_STATUS__DMA_CMD_COMP;
829
830 denali->status = 0;
831
832 /* copy buffer into DMA buffer */
833 memcpy(denali->buf.dma_buf, buf, mtd->writesize);
834
835 /* need extra memcpy for raw transfer */
836 if (raw_xfer)
837 memcpy(denali->buf.dma_buf + mtd->writesize,
838 chip->oob_poi, mtd->oobsize);
839
840 /* setting up DMA */
841 irq_status = denali_dma_configuration(denali, DENALI_WRITE, raw_xfer,
842 irq_mask, oob_required);
843
844 /* if timeout happen, error out */
845 if (!(irq_status & INTR_STATUS__DMA_CMD_COMP)) {
846 debug("DMA timeout for denali write_page\n");
847 denali->status = NAND_STATUS_FAIL;
848 return -EIO;
849 }
850
851 if (irq_status & INTR_STATUS__LOCKED_BLK) {
852 debug("Failed as write to locked block\n");
853 denali->status = NAND_STATUS_FAIL;
854 return -EIO;
855 }
856 return 0;
857}
858
859/* NAND core entry points */
860
861/*
862 * this is the callback that the NAND core calls to write a page. Since
863 * writing a page with ECC or without is similar, all the work is done
864 * by write_page above.
865 */
866static int denali_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -0500867 const uint8_t *buf, int oob_required, int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500868{
869 struct denali_nand_info *denali = mtd_to_denali(mtd);
870
871 /*
872 * for regular page writes, we let HW handle all the ECC
873 * data written to the device.
874 */
875 if (oob_required)
876 /* switch to main + spare access */
877 denali_mode_main_spare_access(denali);
878 else
879 /* switch to main access only */
880 denali_mode_main_access(denali);
881
882 return write_page(mtd, chip, buf, false, oob_required);
883}
884
885/*
886 * This is the callback that the NAND core calls to write a page without ECC.
887 * raw access is similar to ECC page writes, so all the work is done in the
888 * write_page() function above.
889 */
890static int denali_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -0500891 const uint8_t *buf, int oob_required,
892 int page)
Chin Liang See03534df2014-09-12 00:42:17 -0500893{
894 struct denali_nand_info *denali = mtd_to_denali(mtd);
895
896 /*
897 * for raw page writes, we want to disable ECC and simply write
898 * whatever data is in the buffer.
899 */
900
901 if (oob_required)
902 /* switch to main + spare access */
903 denali_mode_main_spare_access(denali);
904 else
905 /* switch to main access only */
906 denali_mode_main_access(denali);
907
908 return write_page(mtd, chip, buf, true, oob_required);
909}
910
911static int denali_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
912 int page)
913{
914 return write_oob_data(mtd, chip->oob_poi, page);
915}
916
917/* raw include ECC value and all the spare area */
918static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
919 uint8_t *buf, int oob_required, int page)
920{
921 struct denali_nand_info *denali = mtd_to_denali(mtd);
922
923 uint32_t irq_status, irq_mask = INTR_STATUS__DMA_CMD_COMP;
924
925 if (denali->page != page) {
926 debug("Missing NAND_CMD_READ0 command\n");
927 return -EIO;
928 }
929
930 if (oob_required)
931 /* switch to main + spare access */
932 denali_mode_main_spare_access(denali);
933 else
934 /* switch to main access only */
935 denali_mode_main_access(denali);
936
937 /* setting up the DMA where ecc_enable is false */
938 irq_status = denali_dma_configuration(denali, DENALI_READ, true,
939 irq_mask, oob_required);
940
941 /* if timeout happen, error out */
942 if (!(irq_status & INTR_STATUS__DMA_CMD_COMP)) {
943 debug("DMA timeout for denali_read_page_raw\n");
944 return -EIO;
945 }
946
947 /* splitting the content to destination buffer holder */
948 memcpy(chip->oob_poi, (denali->buf.dma_buf + mtd->writesize),
949 mtd->oobsize);
950 memcpy(buf, denali->buf.dma_buf, mtd->writesize);
951
952 return 0;
953}
954
955static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
956 uint8_t *buf, int oob_required, int page)
957{
958 struct denali_nand_info *denali = mtd_to_denali(mtd);
959 uint32_t irq_status, irq_mask = INTR_STATUS__DMA_CMD_COMP;
960
961 if (denali->page != page) {
962 debug("Missing NAND_CMD_READ0 command\n");
963 return -EIO;
964 }
965
966 if (oob_required)
967 /* switch to main + spare access */
968 denali_mode_main_spare_access(denali);
969 else
970 /* switch to main access only */
971 denali_mode_main_access(denali);
972
973 /* setting up the DMA where ecc_enable is true */
974 irq_status = denali_dma_configuration(denali, DENALI_READ, false,
975 irq_mask, oob_required);
976
977 memcpy(buf, denali->buf.dma_buf, mtd->writesize);
978
979 /* check whether any ECC error */
980 if (irq_status & INTR_STATUS__ECC_UNCOR_ERR) {
981 /* is the ECC cause by erase page, check using read_page_raw */
982 debug(" Uncorrected ECC detected\n");
983 denali_read_page_raw(mtd, chip, buf, oob_required,
984 denali->page);
985
986 if (is_erased(buf, mtd->writesize) == true &&
987 is_erased(chip->oob_poi, mtd->oobsize) == true) {
988 debug(" ECC error cause by erased block\n");
989 /* false alarm, return the 0xFF */
990 } else {
Scott Wood52ab7ce2016-05-30 13:57:58 -0500991 return -EBADMSG;
Chin Liang See03534df2014-09-12 00:42:17 -0500992 }
993 }
994 memcpy(buf, denali->buf.dma_buf, mtd->writesize);
995 return 0;
996}
997
998static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
999 int page)
1000{
1001 read_oob_data(mtd, chip->oob_poi, page);
1002
1003 return 0;
1004}
1005
1006static uint8_t denali_read_byte(struct mtd_info *mtd)
1007{
1008 struct denali_nand_info *denali = mtd_to_denali(mtd);
1009 uint32_t addr, result;
1010
1011 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
1012 index_addr_read_data(denali, addr | 2, &result);
1013 return (uint8_t)result & 0xFF;
1014}
1015
1016static void denali_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1017{
1018 struct denali_nand_info *denali = mtd_to_denali(mtd);
1019 uint32_t i, addr, result;
1020
1021 /* delay for tR (data transfer from Flash array to data register) */
1022 udelay(25);
1023
1024 /* ensure device completed else additional delay and polling */
1025 wait_for_irq(denali, INTR_STATUS__INT_ACT);
1026
1027 addr = (uint32_t)MODE_11 | BANK(denali->flash_bank);
1028 for (i = 0; i < len; i++) {
1029 index_addr_read_data(denali, (uint32_t)addr | 2, &result);
1030 write_byte_to_buf(denali, result);
1031 }
1032 memcpy(buf, denali->buf.buf, len);
1033}
1034
1035static void denali_select_chip(struct mtd_info *mtd, int chip)
1036{
1037 struct denali_nand_info *denali = mtd_to_denali(mtd);
1038
1039 denali->flash_bank = chip;
1040}
1041
1042static int denali_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
1043{
1044 struct denali_nand_info *denali = mtd_to_denali(mtd);
1045 int status = denali->status;
Scott Wood3ea94ed2015-06-26 19:03:26 -05001046
Chin Liang See03534df2014-09-12 00:42:17 -05001047 denali->status = 0;
1048
1049 return status;
1050}
1051
Scott Wood3ea94ed2015-06-26 19:03:26 -05001052static int denali_erase(struct mtd_info *mtd, int page)
Chin Liang See03534df2014-09-12 00:42:17 -05001053{
1054 struct denali_nand_info *denali = mtd_to_denali(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05001055
Chin Liang See03534df2014-09-12 00:42:17 -05001056 uint32_t cmd, irq_status;
1057
Chin Liang See03534df2014-09-12 00:42:17 -05001058 clear_interrupts(denali);
1059
1060 /* setup page read request for access type */
1061 cmd = MODE_10 | BANK(denali->flash_bank) | page;
1062 index_addr(denali, cmd, 0x1);
1063
1064 /* wait for erase to complete or failure to occur */
1065 irq_status = wait_for_irq(denali, INTR_STATUS__ERASE_COMP |
1066 INTR_STATUS__ERASE_FAIL);
1067
1068 if (irq_status & INTR_STATUS__ERASE_FAIL ||
1069 irq_status & INTR_STATUS__LOCKED_BLK)
Scott Wood3ea94ed2015-06-26 19:03:26 -05001070 return NAND_STATUS_FAIL;
1071
1072 return 0;
Chin Liang See03534df2014-09-12 00:42:17 -05001073}
1074
1075static void denali_cmdfunc(struct mtd_info *mtd, unsigned int cmd, int col,
1076 int page)
1077{
1078 struct denali_nand_info *denali = mtd_to_denali(mtd);
1079 uint32_t addr;
1080
1081 switch (cmd) {
1082 case NAND_CMD_PAGEPROG:
1083 break;
1084 case NAND_CMD_STATUS:
1085 addr = MODE_11 | BANK(denali->flash_bank);
1086 index_addr(denali, addr | 0, cmd);
1087 break;
Chin Liang See03534df2014-09-12 00:42:17 -05001088 case NAND_CMD_READID:
Masahiro Yamada40525e22014-10-03 20:03:03 +09001089 case NAND_CMD_PARAM:
Chin Liang See03534df2014-09-12 00:42:17 -05001090 reset_buf(denali);
Scott Wood3ea94ed2015-06-26 19:03:26 -05001091 /*
1092 * sometimes ManufactureId read from register is not right
Chin Liang See03534df2014-09-12 00:42:17 -05001093 * e.g. some of Micron MT29F32G08QAA MLC NAND chips
1094 * So here we send READID cmd to NAND insteand
Scott Wood3ea94ed2015-06-26 19:03:26 -05001095 */
Chin Liang See03534df2014-09-12 00:42:17 -05001096 addr = MODE_11 | BANK(denali->flash_bank);
1097 index_addr(denali, addr | 0, cmd);
1098 index_addr(denali, addr | 1, col & 0xFF);
Masahiro Yamada40525e22014-10-03 20:03:03 +09001099 if (cmd == NAND_CMD_PARAM)
1100 udelay(50);
Chin Liang See03534df2014-09-12 00:42:17 -05001101 break;
Masahiro Yamadab692b3f2014-10-03 20:03:04 +09001102 case NAND_CMD_RNDOUT:
1103 addr = MODE_11 | BANK(denali->flash_bank);
1104 index_addr(denali, addr | 0, cmd);
1105 index_addr(denali, addr | 1, col & 0xFF);
1106 index_addr(denali, addr | 1, col >> 8);
1107 index_addr(denali, addr | 0, NAND_CMD_RNDOUTSTART);
1108 break;
Chin Liang See03534df2014-09-12 00:42:17 -05001109 case NAND_CMD_READ0:
1110 case NAND_CMD_SEQIN:
1111 denali->page = page;
1112 break;
1113 case NAND_CMD_RESET:
1114 reset_bank(denali);
1115 break;
1116 case NAND_CMD_READOOB:
1117 /* TODO: Read OOB data */
1118 break;
1119 case NAND_CMD_ERASE1:
1120 /*
1121 * supporting block erase only, not multiblock erase as
1122 * it will cross plane and software need complex calculation
1123 * to identify the block count for the cross plane
1124 */
1125 denali_erase(mtd, page);
1126 break;
1127 case NAND_CMD_ERASE2:
1128 /* nothing to do here as it was done during NAND_CMD_ERASE1 */
1129 break;
1130 case NAND_CMD_UNLOCK1:
1131 addr = MODE_10 | BANK(denali->flash_bank) | page;
1132 index_addr(denali, addr | 0, DENALI_UNLOCK_START);
1133 break;
1134 case NAND_CMD_UNLOCK2:
1135 addr = MODE_10 | BANK(denali->flash_bank) | page;
1136 index_addr(denali, addr | 0, DENALI_UNLOCK_END);
1137 break;
1138 case NAND_CMD_LOCK:
1139 addr = MODE_10 | BANK(denali->flash_bank);
1140 index_addr(denali, addr | 0, DENALI_LOCK);
1141 break;
1142 default:
1143 printf(": unsupported command received 0x%x\n", cmd);
1144 break;
1145 }
1146}
1147/* end NAND core entry points */
1148
1149/* Initialization code to bring the device up to a known good state */
1150static void denali_hw_init(struct denali_nand_info *denali)
1151{
1152 /*
Masahiro Yamada54fde8e2017-09-15 21:43:19 +09001153 * The REVISION register may not be reliable. Platforms are allowed to
1154 * override it.
1155 */
1156 if (!denali->revision)
1157 denali->revision = swab16(ioread32(denali->flash_reg + REVISION));
1158
1159 /*
Chin Liang See03534df2014-09-12 00:42:17 -05001160 * tell driver how many bit controller will skip before writing
1161 * ECC code in OOB. This is normally used for bad block marker
1162 */
1163 writel(CONFIG_NAND_DENALI_SPARE_AREA_SKIP_BYTES,
1164 denali->flash_reg + SPARE_AREA_SKIP_BYTES);
1165 detect_max_banks(denali);
1166 denali_nand_reset(denali);
1167 writel(0x0F, denali->flash_reg + RB_PIN_ENABLED);
1168 writel(CHIP_EN_DONT_CARE__FLAG,
1169 denali->flash_reg + CHIP_ENABLE_DONT_CARE);
1170 writel(0xffff, denali->flash_reg + SPARE_AREA_MARKER);
1171
1172 /* Should set value for these registers when init */
1173 writel(0, denali->flash_reg + TWO_ROW_ADDR_CYCLES);
1174 writel(1, denali->flash_reg + ECC_ENABLE);
1175 denali_nand_timing_set(denali);
1176 denali_irq_init(denali);
1177}
1178
1179static struct nand_ecclayout nand_oob;
1180
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +09001181int denali_init(struct denali_nand_info *denali)
Chin Liang See03534df2014-09-12 00:42:17 -05001182{
Scott Wood52ab7ce2016-05-30 13:57:58 -05001183 struct mtd_info *mtd = nand_to_mtd(&denali->nand);
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001184 int ret;
Chin Liang See03534df2014-09-12 00:42:17 -05001185
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001186 denali_hw_init(denali);
Chin Liang See03534df2014-09-12 00:42:17 -05001187
Scott Wood52ab7ce2016-05-30 13:57:58 -05001188 mtd->name = "denali-nand";
1189 mtd->owner = THIS_MODULE;
Chin Liang See03534df2014-09-12 00:42:17 -05001190
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001191 /* register the driver with the NAND core subsystem */
1192 denali->nand.select_chip = denali_select_chip;
1193 denali->nand.cmdfunc = denali_cmdfunc;
1194 denali->nand.read_byte = denali_read_byte;
1195 denali->nand.read_buf = denali_read_buf;
1196 denali->nand.waitfunc = denali_waitfunc;
1197
1198 /*
1199 * scan for NAND devices attached to the controller
1200 * this is the first stage in a two step process to register
1201 * with the nand subsystem
1202 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05001203 if (nand_scan_ident(mtd, denali->max_banks, NULL)) {
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001204 ret = -ENXIO;
1205 goto fail;
1206 }
Chin Liang See03534df2014-09-12 00:42:17 -05001207
1208#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1209 /* check whether flash got BBT table (located at end of flash). As we
1210 * use NAND_BBT_NO_OOB, the BBT page will start with
1211 * bbt_pattern. We will have mirror pattern too */
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001212 denali->nand.bbt_options |= NAND_BBT_USE_FLASH;
Chin Liang See03534df2014-09-12 00:42:17 -05001213 /*
1214 * We are using main + spare with ECC support. As BBT need ECC support,
1215 * we need to ensure BBT code don't write to OOB for the BBT pattern.
1216 * All BBT info will be stored into data area with ECC support.
1217 */
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001218 denali->nand.bbt_options |= NAND_BBT_NO_OOB;
Chin Liang See03534df2014-09-12 00:42:17 -05001219#endif
1220
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001221 denali->nand.ecc.mode = NAND_ECC_HW;
1222 denali->nand.ecc.size = CONFIG_NAND_DENALI_ECC_SIZE;
1223
Scott Wood3ea94ed2015-06-26 19:03:26 -05001224 /* no subpage writes on denali */
1225 denali->nand.options |= NAND_NO_SUBPAGE_WRITE;
1226
Chin Liang See03534df2014-09-12 00:42:17 -05001227 /*
1228 * Tell driver the ecc strength. This register may be already set
1229 * correctly. So we read this value out.
1230 */
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001231 denali->nand.ecc.strength = readl(denali->flash_reg + ECC_CORRECTION);
1232 switch (denali->nand.ecc.size) {
Chin Liang See03534df2014-09-12 00:42:17 -05001233 case 512:
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001234 denali->nand.ecc.bytes =
1235 (denali->nand.ecc.strength * 13 + 15) / 16 * 2;
Chin Liang See03534df2014-09-12 00:42:17 -05001236 break;
1237 case 1024:
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001238 denali->nand.ecc.bytes =
1239 (denali->nand.ecc.strength * 14 + 15) / 16 * 2;
Chin Liang See03534df2014-09-12 00:42:17 -05001240 break;
1241 default:
1242 pr_err("Unsupported ECC size\n");
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001243 ret = -EINVAL;
1244 goto fail;
Chin Liang See03534df2014-09-12 00:42:17 -05001245 }
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001246 nand_oob.eccbytes = denali->nand.ecc.bytes;
1247 denali->nand.ecc.layout = &nand_oob;
Chin Liang See03534df2014-09-12 00:42:17 -05001248
Scott Wood52ab7ce2016-05-30 13:57:58 -05001249 writel(mtd->erasesize / mtd->writesize,
Masahiro Yamada628ee1e2014-11-13 20:31:51 +09001250 denali->flash_reg + PAGES_PER_BLOCK);
1251 writel(denali->nand.options & NAND_BUSWIDTH_16 ? 1 : 0,
1252 denali->flash_reg + DEVICE_WIDTH);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001253 writel(mtd->writesize,
Masahiro Yamada628ee1e2014-11-13 20:31:51 +09001254 denali->flash_reg + DEVICE_MAIN_AREA_SIZE);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001255 writel(mtd->oobsize,
Masahiro Yamada628ee1e2014-11-13 20:31:51 +09001256 denali->flash_reg + DEVICE_SPARE_AREA_SIZE);
1257 if (readl(denali->flash_reg + DEVICES_CONNECTED) == 0)
1258 writel(1, denali->flash_reg + DEVICES_CONNECTED);
1259
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001260 /* override the default operations */
1261 denali->nand.ecc.read_page = denali_read_page;
1262 denali->nand.ecc.read_page_raw = denali_read_page_raw;
1263 denali->nand.ecc.write_page = denali_write_page;
1264 denali->nand.ecc.write_page_raw = denali_write_page_raw;
1265 denali->nand.ecc.read_oob = denali_read_oob;
1266 denali->nand.ecc.write_oob = denali_write_oob;
1267
Scott Wood52ab7ce2016-05-30 13:57:58 -05001268 if (nand_scan_tail(mtd)) {
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001269 ret = -ENXIO;
1270 goto fail;
1271 }
1272
Scott Wood52ab7ce2016-05-30 13:57:58 -05001273 ret = nand_register(0, mtd);
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001274
1275fail:
1276 return ret;
Chin Liang See03534df2014-09-12 00:42:17 -05001277}
1278
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +09001279#ifndef CONFIG_NAND_DENALI_DT
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001280static int __board_nand_init(void)
1281{
1282 struct denali_nand_info *denali;
1283
1284 denali = kzalloc(sizeof(*denali), GFP_KERNEL);
1285 if (!denali)
1286 return -ENOMEM;
1287
1288 /*
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001289 * In the future, these base addresses should be taken from
1290 * Device Tree or platform data.
1291 */
1292 denali->flash_reg = (void __iomem *)CONFIG_SYS_NAND_REGS_BASE;
1293 denali->flash_mem = (void __iomem *)CONFIG_SYS_NAND_DATA_BASE;
1294
1295 return denali_init(denali);
1296}
1297
1298void board_nand_init(void)
Chin Liang See03534df2014-09-12 00:42:17 -05001299{
Masahiro Yamadada0763d2014-11-13 20:31:50 +09001300 if (__board_nand_init() < 0)
1301 pr_warn("Failed to initialize Denali NAND controller.\n");
Chin Liang See03534df2014-09-12 00:42:17 -05001302}
Masahiro Yamada9c5a5dd2017-08-26 01:12:31 +09001303#endif