blob: f359b205f4cf3814575095eb64b401fd6af83e6b [file] [log] [blame]
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001/*
2 * Copyright (c) 2008, Google Inc.
3 * All rights reserved.
4 * Copyright (c) 2009-2012, The Linux Foundation. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <qpic_nand.h>
30#include <bam.h>
31#include <dev/flash.h>
32#include <lib/ptable.h>
33#include <debug.h>
34#include <string.h>
35#include <malloc.h>
36#include <sys/types.h>
Amol Jadib726c3b2012-09-13 13:51:23 -070037#include <platform/clock.h>
Deepa Dinamanie4573be2012-08-03 16:32:29 -070038
39static uint32_t nand_base;
40static struct ptable *flash_ptable;
41static struct flash_info flash;
42static unsigned char *flash_spare_bytes;
43static uint32_t cfg0;
44static uint32_t cfg1;
45static uint32_t cfg0_raw;
46static uint32_t cfg1_raw;
47static uint32_t ecc_bch_cfg;
48
49struct cmd_element ce_array[100];
50
51#define QPIC_BAM_DATA_FIFO_SIZE 64
52#define QPIC_BAM_CMD_FIFO_SIZE 64
53
54static struct bam_desc cmd_desc_fifo[QPIC_BAM_CMD_FIFO_SIZE] __attribute__ ((aligned(BAM_DESC_SIZE)));
55static struct bam_desc data_desc_fifo[QPIC_BAM_DATA_FIFO_SIZE] __attribute__ ((aligned(BAM_DESC_SIZE)));
56
57static struct bam_instance bam;
58static uint8_t *bbtbl;
59
60static struct flash_id supported_flash[] = {
61 /* Flash ID ID Mask Density(MB) Wid Pgsz Blksz oobsz onenand Manuf */
62 {0x1590aa2c, 0xFFFFFFFF, (256 << 20), 0, 2048, (2048 << 6), 64, 0, 0}, /*Micr */
63 /* Note: Width flag is 0 for 8 bit Flash and 1 for 16 bit flash */
64 /* Note: Onenand flag is 0 for NAND Flash and 1 for OneNAND flash */
65};
66
67static nand_result_t
68qpic_nand_check_status(uint32_t status)
69{
70 /* Check for errors */
71 if (status & NAND_FLASH_ERR)
72 {
73 dprintf(CRITICAL, "Nand Flash error for Fetch id cmd. Status = %d\n",
74 status);
75 if (status & NAND_FLASH_TIMEOUT_ERR)
76 return NANDC_RESULT_TIMEOUT;
77 else
78 return NANDC_RESULT_FAILURE;
79 }
80 return NANDC_RESULT_SUCCESS;
81}
82
83static void
84qpic_nand_wait_for_cmd_exec(uint32_t num_desc)
85{
86 /* Create a read/write event to notify the periperal of the added desc. */
87 bam_sys_gen_event(&bam, CMD_PIPE_INDEX, num_desc);
88
89 /* Wait for the descriptors to be processed */
90 bam_wait_for_interrupt(&bam, CMD_PIPE_INDEX, P_PRCSD_DESC_EN_MASK);
91
92 /* Read offset update for the circular FIFO */
93 bam_read_offset_update(&bam, CMD_PIPE_INDEX);
94}
95
96static void
97qpic_nand_wait_for_data(uint32_t pipe_num)
98{
99 /* Wait for the descriptors to be processed */
100 bam_wait_for_interrupt(&bam, pipe_num, P_PRCSD_DESC_EN_MASK);
101
102 /* Read offset update for the circular FIFO */
103 bam_read_offset_update(&bam, pipe_num);
104}
105
106static uint32_t
107qpic_nand_read_reg(uint32_t reg_addr,
108 uint8_t flags,
109 struct cmd_element *cmd_list_ptr)
110{
111 uint32_t val;
112
113 bam_add_cmd_element(cmd_list_ptr, reg_addr, (uint32_t)&val, CE_READ_TYPE);
114
115 /* Enqueue the desc for the above command */
116 bam_add_one_desc(&bam,
117 CMD_PIPE_INDEX,
118 (unsigned char*)cmd_list_ptr,
119 BAM_CE_SIZE,
120 BAM_DESC_CMD_FLAG| BAM_DESC_INT_FLAG | flags);
121
122 qpic_nand_wait_for_cmd_exec(1);
123
124 return val;
125}
126
127static uint32_t
128qpic_nand_fetch_id(struct flash_info *flash)
129{
130 struct cmd_element *cmd_list_ptr = ce_array;
131 struct cmd_element *cmd_list_ptr_start = ce_array;
132 int num_desc = 0;
133 uint32_t status;
134 uint32_t id;
135 uint32_t flash_cmd = NAND_CMD_FETCH_ID;
136 uint32_t exec_cmd = 1;
137 int nand_ret = NANDC_RESULT_SUCCESS;
138
139 /* Issue the Fetch id command to the NANDc */
140 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)flash_cmd, CE_WRITE_TYPE);
141 cmd_list_ptr++;
142
143 /* Execute the cmd */
144 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)exec_cmd, CE_WRITE_TYPE);
145 cmd_list_ptr++;
146
147 /* Prepare the cmd desc for the above commands */
148 bam_add_one_desc(&bam,
149 CMD_PIPE_INDEX,
150 (unsigned char*)cmd_list_ptr_start,
151 (uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start,
152 BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG |
153 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG);
154
155 /* Keep track of the number of desc added. */
156 num_desc++;
157 qpic_nand_wait_for_cmd_exec(num_desc);
158
159 cmd_list_ptr_start = ce_array;
160 cmd_list_ptr = ce_array;
161
162 /* Read the status register */
163 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr);
164
165 /* Check for errors */
166 nand_ret = qpic_nand_check_status(status);
167 if (nand_ret)
168 {
169 dprintf( CRITICAL, "Read ID cmd status failed\n");
170 goto qpic_nand_fetch_id_err;
171 }
172
173 /* Read the id */
174 id = qpic_nand_read_reg(NAND_READ_ID, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr);
175
176 flash->id = id;
177 flash->vendor = id & 0xff;
178 flash->device = (id >> 8) & 0xff;
179 flash->dev_cfg = (id >> 24) & 0xFF;
180 flash->widebus = 0;
181 flash->widebus &= (id >> 24) & 0xFF;
182 flash->widebus = flash->widebus? 1: 0;
183
184qpic_nand_fetch_id_err:
185 return nand_ret;
186}
187
188static int
189qpic_bam_init(uint32_t bam_base, struct qpic_nand_bam_pipes *pipes)
190{
191 uint32_t bam_ret = NANDC_RESULT_SUCCESS;
192
193 bam.base = bam_base;
194 /* Set Read pipe params. */
195 bam.pipe[DATA_PRODUCER_PIPE_INDEX].pipe_num = pipes->read_pipe;
196 /* System consumer */
197 bam.pipe[DATA_PRODUCER_PIPE_INDEX].trans_type = BAM2SYS;
198 bam.pipe[DATA_PRODUCER_PIPE_INDEX].fifo.size = QPIC_BAM_DATA_FIFO_SIZE;
199 bam.pipe[DATA_PRODUCER_PIPE_INDEX].fifo.head = data_desc_fifo;
200
201 /* Set Write pipe params. */
202 bam.pipe[DATA_CONSUMER_PIPE_INDEX].pipe_num = pipes->write_pipe;
203 /* System producer */
204 bam.pipe[DATA_CONSUMER_PIPE_INDEX].trans_type = SYS2BAM;
205 bam.pipe[DATA_CONSUMER_PIPE_INDEX].fifo.size = QPIC_BAM_DATA_FIFO_SIZE;
206 bam.pipe[DATA_CONSUMER_PIPE_INDEX].fifo.head = data_desc_fifo;
207
208 /* Set Cmd pipe params. */
209 bam.pipe[CMD_PIPE_INDEX].pipe_num = pipes->cmd_pipe;
210 /* System consumer */
211 bam.pipe[CMD_PIPE_INDEX].trans_type = BAM2SYS;
212 bam.pipe[CMD_PIPE_INDEX].fifo.size = QPIC_BAM_CMD_FIFO_SIZE;
213 bam.pipe[CMD_PIPE_INDEX].fifo.head = cmd_desc_fifo;
214
215 /* Programs the threshold for BAM transfer
216 * When this threshold is reached, BAM signals the peripheral via the pipe_bytes_available
217 * interface.
218 * The peripheral is signalled with this notification in the following cases:
219 * a. It has accumulated all the descriptors.
220 * b. It has accumulated more than threshold bytes.
221 * c. It has reached EOT (End Of Transfer).
222 * Note: this value needs to be set by the h/w folks and is specific for each peripheral.
223 */
224 bam.threshold = 32;
225
226 /* BAM Init. */
227 bam_init(&bam);
228
229 /* Initialize BAM QPIC read pipe */
230 bam_sys_pipe_init(&bam, DATA_PRODUCER_PIPE_INDEX);
231
232 /* Init read fifo */
233 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[DATA_PRODUCER_PIPE_INDEX].pipe_num);
234
235 if (bam_ret)
236 {
237 dprintf(CRITICAL, "QPIC:NANDc BAM Read FIFO init error\n");
238 bam_ret = NANDC_RESULT_FAILURE;
239 goto qpic_nand_bam_init_error;
240 }
241
242 /* Initialize BAM QPIC write pipe */
243 bam_sys_pipe_init(&bam, DATA_CONSUMER_PIPE_INDEX);
244
245 /* Init write fifo. Use the same fifo as read fifo. */
246 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[DATA_CONSUMER_PIPE_INDEX].pipe_num);
247
248 if (bam_ret)
249 {
250 dprintf(CRITICAL, "QPIC: NANDc: BAM Write FIFO init error\n");
251 bam_ret = NANDC_RESULT_FAILURE;
252 goto qpic_nand_bam_init_error;
253 }
254
255 /* Initialize BAM QPIC cmd pipe */
256 bam_sys_pipe_init(&bam, CMD_PIPE_INDEX);
257
258 /* Init cmd fifo */
259 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[CMD_PIPE_INDEX].pipe_num);
260
261 if (bam_ret)
262 {
263 dprintf(CRITICAL, "QPIC:NANDc BAM CMD FIFO init error\n");
264 bam_ret = NANDC_RESULT_FAILURE;
265 goto qpic_nand_bam_init_error;
266 }
267
268qpic_nand_bam_init_error:
269return bam_ret;
270}
271
272/* Adds command elements for addr and cfg register writes.
273 * cfg: Defines the configuration for the flash cmd.
274 * start: Address where the command elements are added.
275 *
276 * Returns the address where the next cmd element can be added.
277 */
278static struct cmd_element*
279qpic_nand_add_addr_n_cfg_ce(struct cfg_params *cfg,
280 struct cmd_element *start)
281{
282 struct cmd_element *cmd_list_ptr = start;
283
284 bam_add_cmd_element(cmd_list_ptr, NAND_ADDR0, (uint32_t)cfg->addr0, CE_WRITE_TYPE);
285 cmd_list_ptr++;
286 bam_add_cmd_element(cmd_list_ptr, NAND_ADDR1, (uint32_t)cfg->addr1, CE_WRITE_TYPE);
287 cmd_list_ptr++;
288 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_CFG0, (uint32_t)cfg->cfg0, CE_WRITE_TYPE);
289 cmd_list_ptr++;
290 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_CFG1, (uint32_t)cfg->cfg1, CE_WRITE_TYPE);
291 cmd_list_ptr++;
292
293 return cmd_list_ptr;
294}
295
296
297static struct cmd_element*
298qpic_nand_add_onfi_probe_ce(struct onfi_probe_params *params,
299 struct cmd_element *start)
300{
301 struct cmd_element *cmd_list_ptr = start;
302
303 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(&params->cfg, cmd_list_ptr);
304
305 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD1, (uint32_t)params->dev_cmd1, CE_WRITE_TYPE);
306 cmd_list_ptr++;
307 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD_VLD, (uint32_t)params->vld, CE_WRITE_TYPE);
308 cmd_list_ptr++;
309 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(0), (uint32_t)params->cfg.addr_loc_0, CE_WRITE_TYPE);
310 cmd_list_ptr++;
311 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)params->cfg.cmd, CE_WRITE_TYPE);
312 cmd_list_ptr++;
313 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)params->cfg.exec, CE_WRITE_TYPE);
314 cmd_list_ptr++;
315
316 return cmd_list_ptr;
317}
318
319static int
320onfi_probe_cmd_exec(struct onfi_probe_params *params,
321 unsigned char* data_ptr,
322 int data_len)
323{
324 struct cmd_element *cmd_list_ptr = ce_array;
325 struct cmd_element *cmd_list_ptr_start = ce_array;
326 int num_desc = 0;
327 uint32_t status = 0;
328 int nand_ret = NANDC_RESULT_SUCCESS;
329 uint8_t desc_flags = BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG
330 | BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG;
331
332 params->cfg.addr_loc_0 = 0;
333 params->cfg.addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
334 params->cfg.addr_loc_0 |= NAND_RD_LOC_OFFSET(0);
335 params->cfg.addr_loc_0 |= NAND_RD_LOC_SIZE(data_len);
336
337 cmd_list_ptr = qpic_nand_add_onfi_probe_ce(params, cmd_list_ptr);
338
339 /* Enqueue the desc for the above commands */
340 bam_add_one_desc(&bam,
341 CMD_PIPE_INDEX,
342 (unsigned char*)cmd_list_ptr_start,
343 (uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start,
344 desc_flags);
345
346 cmd_list_ptr_start = cmd_list_ptr;
347 num_desc++;
348
349 /* Add Data desc */
350 bam_add_desc(&bam,
351 DATA_PRODUCER_PIPE_INDEX,
352 (unsigned char *)data_ptr,
353 data_len,
354 BAM_DESC_INT_FLAG);
355
356 /* Wait for the commands to be executed */
357 qpic_nand_wait_for_cmd_exec(num_desc);
358
359 /* Read buffer status and check for errors. */
360 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr++);
361
362 if (qpic_nand_check_status(status))
363 {
364 nand_ret = NANDC_RESULT_FAILURE;
365 goto onfi_probe_exec_err;
366 }
367
368 /* Wait for data to be available */
369 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
370
371 /* Check for errors */
372 nand_ret = qpic_nand_check_status(status);
373
374onfi_probe_exec_err:
375 return nand_ret;
376}
377
378/* TODO: check why both vld and cmd need to be written. */
379void
380qpic_nand_onfi_probe_cleanup(uint32_t vld, uint32_t dev_cmd1)
381{
382 struct cmd_element *cmd_list_ptr = ce_array;
383 struct cmd_element *cmd_list_ptr_start = ce_array;
384
385 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD1, dev_cmd1, CE_WRITE_TYPE);
386 cmd_list_ptr++;
387 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD_VLD, vld, CE_WRITE_TYPE);
388 cmd_list_ptr++;
389
390 /* Enqueue the desc for the above commands */
391 bam_add_one_desc(&bam,
392 CMD_PIPE_INDEX,
393 (unsigned char*)cmd_list_ptr_start,
394 (uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start,
395 BAM_DESC_UNLOCK_FLAG | BAM_DESC_CMD_FLAG| BAM_DESC_INT_FLAG);
396
397 qpic_nand_wait_for_cmd_exec(1);
398}
399
400static int
401qpic_nand_onfi_save_params(struct onfi_param_page *param_page, struct flash_info *flash)
402{
403 int onfi_ret = NANDC_RESULT_SUCCESS;
404 uint32_t ecc_bits;
405
406 onfi_ret = qpic_nand_fetch_id(flash);
407
408 if (onfi_ret)
409 {
410 dprintf(CRITICAL, "Fetch ID cmd failed\n");
411 goto onfi_save_params_err;
412 }
413
414 flash->page_size = param_page->data_per_pg;
415 flash->block_size = param_page->pgs_per_blk * flash->page_size;
416 flash->num_blocks = param_page->blks_per_LUN;
417 flash->widebus = param_page->feature_supported & 0x1;
418 flash->density = param_page->blks_per_LUN * flash->blksize;
419 flash->spare_size = param_page->spare_per_pg;
420 ecc_bits = param_page->num_bits_ecc_correctability;
421 flash->num_pages_per_blk = param_page->pgs_per_blk;
422 flash->num_pages_per_blk_mask = param_page->pgs_per_blk - 1;
423
424 if (ecc_bits >= 8)
425 flash->ecc_width = NAND_WITH_8_BIT_ECC;
426 else
427 flash->ecc_width = NAND_WITH_4_BIT_ECC;
428
429 onfi_save_params_err:
430 return onfi_ret;
431}
432
433static void
434qpic_nand_save_config(struct flash_info *flash)
435{
436
437 /* Save Configurations */
438 flash->cws_per_page = flash->page_size >> NAND_CW_DIV_RIGHT_SHIFT;
439
440 /* Codeword Size = UD_SIZE_BYTES + ECC_PARITY_SIZE_BYTES
441 * + SPARE_SIZE_BYTES + Bad Block size
442 */
443 if (flash->ecc_width & NAND_WITH_8_BIT_ECC)
444 {
445 flash->cw_size = NAND_CW_SIZE_8_BIT_ECC;
446 ecc_bch_cfg |= (1 << NAND_DEV0_ECC_MODE_SHIFT); /* Use 8-bit ecc */
447
448 if (flash->widebus)
449 {
450 cfg0 |= (0 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
451 ecc_bch_cfg |= (14 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
452 }
453 else
454 {
455 cfg0 |= (2 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
456 ecc_bch_cfg |= (13 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
457 }
458 }
459 else
460 {
461 flash->cw_size = NAND_CW_SIZE_4_BIT_ECC;
462
463 if (flash->widebus)
464 {
465 cfg0 |= (2 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
466 ecc_bch_cfg |= (8 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
467 }
468 else
469 {
470 cfg0 |= (4 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
471 ecc_bch_cfg |= (7 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
472 }
473 }
474
475 /* BAD_BLOCK_BYTE_NUM = Page Size -
476 * (CW_PER_PAGE * Codeword Size) + 1
477 * Note: Set CW_PER_PAGE to 1 less than the actual number.
478 */
479 flash->bad_blk_loc = flash->page_size - flash->cw_size * (flash->cws_per_page - 1) + 1;
480
481 cfg0 |= ((flash->cws_per_page - 1) << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT) /* 4/8 cw/pg for 2/4k */
482 |(DATA_BYTES_IN_IMG_PER_CW << NAND_DEV0_CFG0_UD_SIZE_BYTES_SHIFT) /* 516 user data bytes */
483 |(5 << NAND_DEV0_CFG0_ADDR_CYCLE_SHIFT) /* 5 address cycles */
484 |(0 << NAND_DEV0_CFG0_DIS_STS_AFTER_WR_SHIFT);/* Send read status cmd after each write. */
485
486 cfg1 |= (7 << NAND_DEV0_CFG1_RECOVERY_CYCLES_SHIFT) /* 8 recovery cycles */
487 |(0 << NAND_DEV0_CFG1_CS_ACTIVE_BSY_SHIFT) /* Allow CS deassertion */
488 |(flash->bad_blk_loc << NAND_DEV0_CFG1_BAD_BLK_BYTE_NUM_SHIFT)/* Bad block marker location */
489 |(0 << NAND_DEV0_CFG1_BAD_BLK_IN_SPARE_SHIFT) /* Bad block in user data area */
490 |(2 << NAND_DEV0_CFG1_WR_RD_BSY_GAP_SHIFT) /* 8 cycle tWB/tRB */
491 |(flash->widebus << NAND_DEV0_CFG1_WIDE_BUS_SHIFT); /* preserve wide flash flag */
492
493 cfg0_raw = ((flash->cws_per_page- 1) << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT)
494 |(5 << NAND_DEV0_CFG0_ADDR_CYCLE_SHIFT)
495 |(516 << NAND_DEV0_CFG0_UD_SIZE_BYTES_SHIFT) //figure out the size of cw
496 | (1 << NAND_DEV0_CFG0_DIS_STS_AFTER_WR_SHIFT);
497
498 cfg1_raw = (7 << NAND_DEV0_CFG1_RECOVERY_CYCLES_SHIFT)
499 | (0 << NAND_DEV0_CFG1_CS_ACTIVE_BSY_SHIFT)
500 | (17 << NAND_DEV0_CFG1_BAD_BLK_BYTE_NUM_SHIFT)
501 | (1 << NAND_DEV0_CFG1_BAD_BLK_IN_SPARE_SHIFT)
502 | (2 << NAND_DEV0_CFG1_WR_RD_BSY_GAP_SHIFT)
503 | (flash->widebus << NAND_DEV0_CFG1_WIDE_BUS_SHIFT)
504 |1 ; /* to disable reed solomon ecc..this feild is now read only. */
505
506 ecc_bch_cfg |= (0 << NAND_DEV0_ECC_DISABLE_SHIFT) /* Enable ECC */
507 | (0 << NAND_DEV0_ECC_SW_RESET_SHIFT) /* Put ECC core in op mode */
508 | (DATA_BYTES_IN_IMG_PER_CW << NAND_DEV0_ECC_NUM_DATA_BYTES)
509 | (1 << NAND_DEV0_ECC_FORCE_CLK_OPEN_SHIFT); /* Enable all clocks */
510}
511
512/* Onfi probe should issue the following commands to the flash device:
513 * 1. Read ID - with addr ONFI_READ_ID_ADDR.
514 * This returns the ONFI ASCII string indicating support for ONFI.
515 * 2. Read Prameter Page - with addr ONFI_READ_PARAM_PAGE_ADDR.
516 * This returns the params for the device.
517 * Each command inturn issues commands- ADDR0, ADDR1, chip_select,
518 * cfg0, cfg1, cmd_vld, dev_cmd1, read_loc0, flash, exec.
519 */
520static int
521qpic_nand_onfi_probe(struct flash_info *flash)
522{
523 struct onfi_probe_params params;
524 uint32_t vld;
525 uint32_t dev_cmd1;
526 unsigned char *buffer;
527 unsigned char onfi_str[4];
528 uint32_t *id;
529 struct onfi_param_page *param_page;
530 int onfi_ret = NANDC_RESULT_SUCCESS;
531
532 /* Allocate memory required to read the onfi param page */
533 buffer = (unsigned char*) malloc(ONFI_READ_PARAM_PAGE_BUFFER_SIZE);
534
535 /* Read the vld and dev_cmd1 registers before modifying */
536 vld = qpic_nand_read_reg(NAND_DEV_CMD_VLD, 0, ce_array);
537 dev_cmd1 = qpic_nand_read_reg(NAND_DEV_CMD1, 0, ce_array);
538
539 /* Initialize flash cmd */
540 params.cfg.cmd = NAND_CMD_PAGE_READ;
541 params.cfg.exec = 1;
542
543 /* Execute Read ID cmd */
544
545 /* Initialize the config */
546 params.cfg.cfg0 = NAND_CFG0_RAW_ONFI_ID;
547 params.cfg.cfg1 = NAND_CFG1_RAW_ONFI_ID;
548
549 /* Initialize the cmd and vld */
550 params.dev_cmd1 = (dev_cmd1 & 0xFFFFFF00) | ONFI_READ_ID_CMD;
551 params.vld = vld & 0xFFFFFFFE;
552
553 /* Initialize the address
554 * addr1 is not used bcos of the cfg.
555 */
556 params.cfg.addr0 = ONFI_READ_ID_ADDR;
557 params.cfg.addr1 = 0;
558
559 /* Lock the pipe and execute the cmd. */
560 onfi_ret = onfi_probe_cmd_exec(&params, onfi_str, ONFI_READ_ID_BUFFER_SIZE);
561 if (onfi_ret)
562 {
563 dprintf(CRITICAL, "ONFI Read id cmd failed\n");
564 goto qpic_nand_onfi_probe_err;
565 }
566
567 /* Write back vld and cmd and unlock the pipe. */
568 qpic_nand_onfi_probe_cleanup(vld, dev_cmd1);
569
570 /* Check for onfi string */
571 id = (uint32_t*)onfi_str;
572 if (*id != ONFI_SIGNATURE)
573 {
574 dprintf(CRITICAL, "Not an ONFI device\n");
575 /* Not an onfi device. Return error. */
576 onfi_ret = NANDC_RESULT_DEV_NOT_SUPPORTED;
577 goto qpic_nand_onfi_probe_err;
578 }
579
580 dprintf(INFO, "ONFI device found\n");
581 /* Now read the param page */
582 /* Initialize the config */
583 params.cfg.cfg0 = NAND_CFG0_RAW_ONFI_PARAM_PAGE;
584 params.cfg.cfg1 = NAND_CFG1_RAW_ONFI_PARAM_PAGE;
585
586 /* Initialize the cmd and vld */
587 params.dev_cmd1 = (dev_cmd1 & 0xFFFFFF00) | ONFI_READ_PARAM_PAGE_CMD;
588 params.vld = vld & 0xFFFFFFFE;
589
590 /* Initialize the address
591 * addr1 is not used bcos of the cfg.
592 */
593 params.cfg.addr0 = ONFI_READ_PARAM_PAGE_ADDR;
594 params.cfg.addr1 = 0;
595
596 /* Lock the pipe and execute the cmd. */
597 onfi_ret = onfi_probe_cmd_exec(&params, buffer, ONFI_READ_PARAM_PAGE_BUFFER_SIZE);
598 if (onfi_ret)
599 {
600 dprintf(CRITICAL, "ONFI Read param page failed\n");
601 goto qpic_nand_onfi_probe_err;
602 }
603
604 /* Write back vld and cmd and unlock the pipe. */
605 qpic_nand_onfi_probe_cleanup(vld, dev_cmd1);
606
607 /* Verify the integrity of the returned page */
608 param_page = (struct onfi_param_page*)buffer;
609
610 /* TODO: Add CRC check to validate the param page. */
611
612 /* Save the parameter values */
613 onfi_ret = qpic_nand_onfi_save_params(param_page, flash);
614
615qpic_nand_onfi_probe_err:
616 if (onfi_ret)
617 dprintf(CRITICAL, "ONFI probe failed\n");
618
619 free(buffer);
620
621 return onfi_ret;
622}
623
624/* Enquues a desc for a flash cmd with NWD flag set:
625 * cfg: Defines the configuration for the flash cmd.
626 * start: Address where the command elements are added.
627 *
628 * Returns the address where the next cmd element can be added.
629 */
630struct cmd_element*
631qpic_nand_add_cmd_ce(struct cfg_params *cfg,
632 struct cmd_element *start)
633{
634 struct cmd_element *cmd_list_ptr;
635
636 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(cfg, start);
637
638 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)cfg->cmd, CE_WRITE_TYPE);
639 cmd_list_ptr++;
640
641 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)cfg->exec, CE_WRITE_TYPE);
642 cmd_list_ptr++;
643
644 return cmd_list_ptr;
645}
646
647/* Reads nand_flash_status and resets nand_flash_status and nand_read_status */
648struct cmd_element*
649qpic_nand_add_read_n_reset_status_ce(struct cmd_element *start,
650 uint32_t *flash_status_read,
651 uint32_t read_status)
652{
653 struct cmd_element *cmd_list_ptr = start;
654 uint32_t flash_status_reset;
655 uint32_t read_status_reset;
656
657 /* Read and reset the status registers. */
658 flash_status_reset = NAND_FLASH_STATUS_RESET;
659 read_status_reset = NAND_READ_STATUS_RESET;
660
661 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_STATUS, (uint32_t)flash_status_read, CE_READ_TYPE);
662 cmd_list_ptr++;
663 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_STATUS, (uint32_t)flash_status_reset, CE_WRITE_TYPE);
664 cmd_list_ptr++;
665
666 if (read_status)
667 {
668 bam_add_cmd_element(cmd_list_ptr, NAND_READ_STATUS, (uint32_t)read_status_reset, CE_WRITE_TYPE);
669 cmd_list_ptr++;
670 }
671
672 return cmd_list_ptr;
673}
674
675struct cmd_element*
676qpic_nand_add_isbad_cmd_ce(struct cfg_params *cfg,
677 struct cmd_element *start)
678{
679 struct cmd_element *cmd_list_ptr = start;
680
681 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG, (uint32_t)cfg->ecc_cfg, CE_WRITE_TYPE);
682 cmd_list_ptr++;
683
684 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(0), (uint32_t)cfg->addr_loc_0, CE_WRITE_TYPE);
685 cmd_list_ptr++;
686
687 cmd_list_ptr = qpic_nand_add_cmd_ce(cfg, cmd_list_ptr);
688
689 return cmd_list_ptr;
690}
691
692static int
693qpic_nand_block_isbad_exec(struct cfg_params *params,
694 uint8_t *bad_block)
695{
696
697 struct cmd_element *cmd_list_ptr = ce_array;
698 struct cmd_element *cmd_list_ptr_start = ce_array;
699 uint8_t desc_flags = BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG
700 | BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG;
701 int num_desc = 0;
702 uint32_t status = 0;
703 int nand_ret = NANDC_RESULT_SUCCESS;
704
705 cmd_list_ptr = qpic_nand_add_isbad_cmd_ce(params, cmd_list_ptr);
706
707 /* Enqueue the desc for the above commands */
708 bam_add_one_desc(&bam,
709 CMD_PIPE_INDEX,
710 (unsigned char*)cmd_list_ptr_start,
711 (uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start,
712 desc_flags);
713
714 num_desc++;
715
716 /* Add Data desc */
717 bam_add_desc(&bam,
718 DATA_PRODUCER_PIPE_INDEX,
719 (unsigned char *)bad_block,
720 4,
721 BAM_DESC_INT_FLAG);
722
723 qpic_nand_wait_for_cmd_exec(num_desc);
724
725 status = qpic_nand_read_reg(NAND_FLASH_STATUS, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr);
726
727 if ((nand_ret = qpic_nand_check_status(status)))
728 return NANDC_RESULT_FAILURE;
729
730 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
731
732 return nand_ret;
733}
734
735static int
736qpic_nand_block_isbad(unsigned block)
737{
738 unsigned cwperpage;
739 struct cfg_params params;
740 uint8_t bad_block[4];
741 unsigned nand_ret = NANDC_RESULT_SUCCESS;
742
743 if (bbtbl[block] == NAND_BAD_BLK_VALUE_IS_GOOD)
744 return NANDC_RESULT_SUCCESS;
745 else if (bbtbl[block] == NAND_BAD_BLK_VALUE_IS_BAD)
746 return NANDC_RESULT_BAD_BLOCK;
747 else
748 {
749 /* Read the bad block value from the flash.
750 * Bad block value is stored in the first page of the block.
751 */
752 /* Read the first page in the block. */
753 cwperpage = flash.cws_per_page;
754
755 /* Read page cmd */
756 params.cmd = NAND_CMD_PAGE_READ;
757 /* Clear the CW per page bits */
758 params.cfg0 = cfg0_raw & ~(7U << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT);
759 params.cfg1 = cfg1_raw;
760 /* addr0 - Write column addr + few bits in row addr upto 32 bits.
761 * Figure out the bad block status offset.
762 */
763 if (flash.widebus)
764 {
765 if (flash.ecc_width == NAND_WITH_8_BIT_ECC)
766 params.addr0 = ((block << 16) | ((532 * (cwperpage - 1)) >> 1));
767 else
768 params.addr0 = ((block << 16) | ((528 * (cwperpage - 1)) >> 1));
769 }
770 else
771 {
772 if (flash.ecc_width == NAND_WITH_8_BIT_ECC)
773 params.addr0 = (block << 16) | (532 * (cwperpage - 1));
774 else
775 params.addr0 = (block << 16) | (528 * (cwperpage - 1));
776 }
777
778 /* addr1 - Write rest of row addr.
779 * This will be all 0s.
780 */
781 params.addr1 = (block >> 16) & 0xff;
782 params.addr_loc_0 = NAND_RD_LOC_OFFSET(0);
783 params.addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
784 params.addr_loc_0 |= NAND_RD_LOC_SIZE(4); /* Read 4 bytes */
785 params.ecc_cfg = ecc_bch_cfg & 0xFFFFFFFE; /* Disable ECC */
786 params.exec = 1;
787
788 if (qpic_nand_block_isbad_exec(&params, bad_block))
789 {
790 dprintf(CRITICAL,
791 "Could not read bad block value\n");
792 return NANDC_RESULT_FAILURE;
793 }
794
795 if (flash.widebus)
796 {
797 if (bad_block[0] != 0xFF && bad_block[1] != 0xFF)
798 {
799 bbtbl[block] = NAND_BAD_BLK_VALUE_IS_BAD;
800 nand_ret = NANDC_RESULT_BAD_BLOCK;
801 }
802 }
803 else if (bad_block[0] != 0xFF)
804 {
805 bbtbl[block] = NAND_BAD_BLK_VALUE_IS_BAD;
806 nand_ret = NANDC_RESULT_BAD_BLOCK;
807 }
808 else
809 bbtbl[block] = NAND_BAD_BLK_VALUE_IS_GOOD;
810
811 return nand_ret;
812 }
813}
814
815/* Function to erase a block on the nand.
816 * page: Starting page address for the block.
817 */
818static int
819qpic_nand_blk_erase(uint32_t page)
820{
821 struct cfg_params cfg;
822 struct cmd_element *cmd_list_ptr = ce_array;
823 struct cmd_element *cmd_list_ptr_start = ce_array;
824 uint32_t status;
825 int num_desc = 0;
826 uint32_t blk_addr = page / flash.num_pages_per_blk;
827
828 /* Erase only if the block is not bad */
829 if (qpic_nand_block_isbad(blk_addr))
830 {
831 dprintf(CRITICAL,
832 "NAND Erase error: Block address belongs to bad block: %d\n",
833 blk_addr);
834 return NANDC_RESULT_FAILURE;
835 }
836
837 /* Fill in params for the erase flash cmd */
838 cfg.addr0 = page;
839 cfg.addr1 = 0;
840 /* Clear CW_PER_PAGE in cfg0 */
841 cfg.cfg0 = cfg0 & ~(7U << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT);
842 cfg.cfg1 = cfg1;
843 cfg.cmd = NAND_CMD_BLOCK_ERASE;
844 cfg.exec = 1;
845
846 cmd_list_ptr = qpic_nand_add_cmd_ce(&cfg, cmd_list_ptr);
847
848 /* Enqueue the desc for the above commands */
849 bam_add_one_desc(&bam,
850 CMD_PIPE_INDEX,
851 (unsigned char*)cmd_list_ptr_start,
852 (uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start,
Deepa Dinamani2467bbb2012-10-02 13:59:58 -0700853 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG | BAM_DESC_LOCK_FLAG);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700854
855 cmd_list_ptr_start = cmd_list_ptr;
856 num_desc++;
857
858 qpic_nand_wait_for_cmd_exec(num_desc);
859
860 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr);
861
862 cmd_list_ptr++;
863 cmd_list_ptr_start = cmd_list_ptr;
864
865 /* QPIC controller automatically sends
866 * GET_STATUS cmd to the nand card because
867 * of the configuration programmed.
868 * Read the result of GET_STATUS cmd.
869 */
870 cmd_list_ptr = qpic_nand_add_read_n_reset_status_ce(cmd_list_ptr, &status, 1);
871
872 /* Enqueue the desc for the above commands */
873 bam_add_one_desc(&bam,
874 CMD_PIPE_INDEX,
875 (unsigned char*)cmd_list_ptr_start,
876 (uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start,
Deepa Dinamani2467bbb2012-10-02 13:59:58 -0700877 BAM_DESC_INT_FLAG | BAM_DESC_CMD_FLAG | BAM_DESC_UNLOCK_FLAG) ;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700878
879 num_desc = 1;
880 qpic_nand_wait_for_cmd_exec(num_desc);
881
882 /* Check for status errors*/
883 if (qpic_nand_check_status(status))
884 {
885 dprintf(CRITICAL,
886 "NAND Erase error: Block address belongs to bad block: %d\n",
887 blk_addr);
888 return NANDC_RESULT_FAILURE;
889 }
890
891 /* Check for PROG_ERASE_OP_RESULT bit for the result of erase operation. */
Deepa Dinamani2467bbb2012-10-02 13:59:58 -0700892 if (!(status & PROG_ERASE_OP_RESULT))
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700893 return NANDC_RESULT_SUCCESS;
894
895 return NANDC_RESULT_FAILURE;
896}
897
898/* Return num of desc added. */
899static int
900qpic_nand_add_wr_page_cws_cmd_desc(struct cfg_params *cfg,
901 uint32_t status[],
902 enum nand_cfg_value cfg_mode)
903{
904 struct cmd_element *cmd_list_ptr = ce_array;
905 struct cmd_element *cmd_list_ptr_start = ce_array;
906 uint32_t ecc;
907 int num_desc = 0;
908 int int_flag = 0;
909
910 if (cfg_mode == NAND_CFG)
911 ecc = ecc_bch_cfg;
912 else
913 ecc = ecc_bch_cfg & 0xFFFFFFFE; /* Disable ECC */
914
915 /* Add ECC configuration */
916 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG,
917 (uint32_t)ecc, CE_WRITE_TYPE);
918 cmd_list_ptr++;
919 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(cfg, cmd_list_ptr);
920
921 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD,
922 (uint32_t)cfg->cmd, CE_WRITE_TYPE);
923 cmd_list_ptr++;
924
925 /* Enqueue the desc for the above commands */
926 bam_add_one_desc(&bam,
927 CMD_PIPE_INDEX,
928 (unsigned char*)cmd_list_ptr_start,
929 (uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start,
930 BAM_DESC_CMD_FLAG | BAM_DESC_LOCK_FLAG);
931
932 num_desc++;
933
934 /* Add CE for all the CWs */
935 for (unsigned i = 0; i < flash.cws_per_page; i++)
936 {
937 cmd_list_ptr_start = cmd_list_ptr;
938
939 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)cfg->exec, CE_WRITE_TYPE);
940 cmd_list_ptr++;
941
942 /* Enqueue the desc for the above commands */
943 bam_add_one_desc(&bam,
944 CMD_PIPE_INDEX,
945 (unsigned char*)cmd_list_ptr_start,
946 (uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start,
947 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG);
948
949 num_desc++;
950 cmd_list_ptr_start = cmd_list_ptr;
951
952 /* Set interrupt bit only for the last CW */
953 if (i == flash.cws_per_page - 1)
954 {
955 cmd_list_ptr = qpic_nand_add_read_n_reset_status_ce(cmd_list_ptr,
956 &status[i],
957 1);
958 int_flag = BAM_DESC_INT_FLAG | BAM_DESC_UNLOCK_FLAG;
959 }
960 else
961 cmd_list_ptr = qpic_nand_add_read_n_reset_status_ce(cmd_list_ptr,
962 &status[i],
963 0);
964
965 /* Enqueue the desc for the above commands */
966 bam_add_one_desc(&bam,
967 CMD_PIPE_INDEX,
968 (unsigned char*)cmd_list_ptr_start,
969 (uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start,
970 int_flag | BAM_DESC_CMD_FLAG);
971 num_desc++;
972 }
973 return num_desc;
974}
975
976void
977qpic_add_wr_page_cws_data_desc(const void *buffer,
978 enum nand_cfg_value cfg_mode,
979 const void *spareaddr)
980{
981 int len;
982 int flags;
983 uint32_t start;
984 unsigned num_desc = 0;
985
986 for( unsigned i = 0; i < flash.cws_per_page; i++)
987 {
988 flags = 0;
989
990 /* Set the interrupt flag on the last CW write for the page. */
991 if( i == flash.cws_per_page - 1)
992 flags |= BAM_DESC_INT_FLAG;
993
994 if (cfg_mode != NAND_CFG_RAW)
995 {
996 start = (uint32_t)buffer + i * DATA_BYTES_IN_IMG_PER_CW;
997
998 if (i < (flash.cws_per_page - 1))
999 {
1000 len = DATA_BYTES_IN_IMG_PER_CW;
1001 flags |= BAM_DESC_EOT_FLAG;
1002 }
1003 else
1004 {
1005 /* Allow space for spare bytes in the last page */
1006 len = USER_DATA_BYTES_PER_CW - ((flash.cws_per_page - 1) << 2);
1007 flags = 0;
1008 }
1009 }
1010 else
1011 {
1012 start = (uint32_t)buffer;
1013 len = flash.cw_size;
1014 flags |= BAM_DESC_EOT_FLAG;
1015 }
1016 bam_add_one_desc(&bam, DATA_CONSUMER_PIPE_INDEX, (unsigned char*)start, len, flags);
1017 num_desc++;
1018
1019 if ((i == (flash.cws_per_page - 1)) && (cfg_mode == NAND_CFG))
1020 {
1021 /* write extra data */
1022 start = (uint32_t)spareaddr;
1023 len = (flash.cws_per_page << 2);
1024 flags = BAM_DESC_EOT_FLAG | BAM_DESC_INT_FLAG;
1025 bam_add_one_desc(&bam, DATA_CONSUMER_PIPE_INDEX, (unsigned char*)start, len, flags);
1026 num_desc++;
1027 }
1028 }
1029
1030 bam_sys_gen_event(&bam, DATA_CONSUMER_PIPE_INDEX, num_desc);
1031}
1032
1033static nand_result_t
1034qpic_nand_write_page(uint32_t pg_addr,
1035 enum nand_cfg_value cfg_mode,
1036 const void* buffer,
1037 const void* spareaddr)
1038{
1039 struct cfg_params cfg;
1040 uint32_t status[4];
1041 int num_cmd_desc = 0;
1042 int nand_ret = NANDC_RESULT_SUCCESS;
1043
1044 if (cfg_mode == NAND_CFG_RAW)
1045 {
1046 cfg.cfg0 = cfg0_raw;
1047 cfg.cfg1 = cfg1_raw;
1048 }
1049 else
1050 {
1051 cfg.cfg0 = cfg0;
1052 cfg.cfg1 = cfg1;
1053 }
1054
1055 cfg.cmd = NAND_CMD_PRG_PAGE;
1056 cfg.exec = 1;
1057
1058 cfg.addr0 = pg_addr << 16;
1059 cfg.addr1 = (pg_addr >> 16) & 0xff;
1060
1061 num_cmd_desc = qpic_nand_add_wr_page_cws_cmd_desc(&cfg, status, cfg_mode);
1062
1063 qpic_add_wr_page_cws_data_desc(buffer, cfg_mode, spareaddr);
1064
1065 /* Wait for the commands to be executed */
1066 qpic_nand_wait_for_cmd_exec(num_cmd_desc);
1067
1068 /* Check for errors */
1069 for(unsigned i = 0; i < flash.cws_per_page; i++)
1070 {
1071 nand_ret = qpic_nand_check_status(status[i]);
1072 if (nand_ret)
1073 {
1074 dprintf(CRITICAL,
1075 "Failed to write CW %d for page: %d\n",
1076 i, pg_addr);
1077 break;
1078 }
1079 }
1080
1081 /* Wait for data to be available */
1082 qpic_nand_wait_for_data(DATA_CONSUMER_PIPE_INDEX);
1083
1084 return nand_ret;
1085}
1086
1087static int
1088qpic_nand_mark_badblock(uint32_t page)
1089{
1090 char empty_buf[NAND_CW_SIZE_8_BIT_ECC];
1091
1092 memset(empty_buf, 0, NAND_CW_SIZE_8_BIT_ECC);
1093
1094 /* Going to first page of the block */
1095 if (page & flash.num_pages_per_blk_mask)
1096 page = page - (page & flash.num_pages_per_blk_mask);
1097
1098 return qpic_nand_write_page(page, NAND_CFG_RAW, empty_buf, 0);
1099}
1100
1101static void
1102qpic_nand_non_onfi_probe(struct flash_info *flash)
1103{
1104 int dev_found = 0;
1105 unsigned index;
1106 uint32_t ecc_bits;
1107
1108 /* Read the nand id. */
1109 qpic_nand_fetch_id(flash);
1110
1111 /* Check if we support the device */
1112 for (index = 1; index < (ARRAY_SIZE(supported_flash)); index++)
1113 {
1114 if ((flash->id & supported_flash[index].mask) ==
1115 (supported_flash[index].flash_id & (supported_flash[index].mask)))
1116 {
1117 dev_found = 1;
1118 break;
1119 }
1120 }
1121
1122 if (dev_found)
1123 {
1124 flash->page_size = supported_flash[index].pagesize;
1125 flash->block_size = supported_flash[index].blksize;
1126 flash->spare_size = supported_flash[index].oobsize;
1127 ecc_bits = supported_flash[index].ecc_8_bits;
1128
1129 /* Make sure that the block size and page size are defined. */
1130 ASSERT(flash->block_size);
1131 ASSERT(flash->page_size);
1132
1133 flash->num_blocks = supported_flash[index].density;
1134 flash->num_blocks /= (flash->block_size);
1135 flash->num_pages_per_blk = flash->block_size / flash->page_size;
1136 flash->num_pages_per_blk_mask = flash->num_pages_per_blk - 1;
1137
1138 /* Look for 8bit BCH ECC Nand, TODO: ECC Correctability >= 8 */
1139 if (ecc_bits)
1140 flash->ecc_width = NAND_WITH_8_BIT_ECC;
1141 else
1142 flash->ecc_width = NAND_WITH_4_BIT_ECC;
1143
1144 flash->density = supported_flash[index].density;
1145 flash->widebus = supported_flash[index].widebus;
1146
1147 return;
1148 }
1149
1150 /* Flash device is not supported, print flash device info and halt */
1151 if (dev_found == 0)
1152 {
1153 dprintf(CRITICAL, "NAND device is not supported: nandid: 0x%x"
1154 "maker=0x%02x device=0x%02x\n",
1155 flash->id,
1156 flash->vendor,
1157 flash->device);
1158 ASSERT(0);
1159 }
1160
1161 dprintf(INFO, "nandid: 0x%x maker=0x%02x device=0x%02x page_size=%d\n",
1162 flash->id,
1163 flash->vendor,
1164 flash->device,
1165 flash->page_size);
1166
1167 dprintf(INFO, "spare_size=%d block_size=%d num_blocks=%d\n",
1168 flash->spare_size,
1169 flash->block_size,
1170 flash->num_blocks);
1171}
1172
1173void
1174qpic_nand_init(struct qpic_nand_init_config *config)
1175{
1176 uint32_t i;
1177 int nand_ret;
1178
Amol Jadib726c3b2012-09-13 13:51:23 -07001179 qpic_nand_clock_init();
1180
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001181 nand_base = config->nand_base;
1182
1183 qpic_bam_init(config->bam_base, &(config->pipes));
1184
1185 /* Do an ONFI probe. */
1186 nand_ret = qpic_nand_onfi_probe(&flash);
1187
1188 if (nand_ret == NANDC_RESULT_DEV_NOT_SUPPORTED)
1189 {
1190 /* Not an ONFI Device.
1191 * Check if it is one of the devices we support.
1192 */
1193 qpic_nand_non_onfi_probe(&flash);
1194
1195 }
1196
1197 /* Save the RAW and read/write configs */
1198 qpic_nand_save_config(&flash);
1199
1200 flash_spare_bytes = (unsigned char *)malloc(flash.spare_size);
1201
1202 if (flash_spare_bytes == NULL)
1203 {
1204 dprintf(CRITICAL, "Failed to allocate memory for spare bytes\n");
1205 return;
1206 }
1207
1208 /* Create a bad block table */
1209 bbtbl = (unsigned int *)malloc(sizeof(uint8_t) * flash.num_blocks);
1210
1211 if (bbtbl == NULL)
1212 {
1213 dprintf(CRITICAL, "Failed to allocate memory for bad block table\n");
1214 return;
1215 }
1216
1217 for (i = 0; i < flash.num_blocks; i++)
1218 bbtbl[i] = NAND_BAD_BLK_VALUE_NOT_READ;
1219}
1220
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001221unsigned
1222flash_page_size(void)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001223{
1224 return flash.page_size;
1225}
1226
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001227unsigned
1228flash_block_size(void)
1229{
1230 return flash.block_size;
1231}
1232
1233
1234struct ptable *
1235flash_get_ptable(void)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001236{
1237 return flash_ptable;
1238}
1239
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001240void
Deepa Dinamani87feab82012-10-04 14:28:05 -07001241qpic_nand_uninit()
1242{
1243 bam_pipe_reset(&bam, DATA_PRODUCER_PIPE_INDEX);
1244 bam_pipe_reset(&bam, DATA_CONSUMER_PIPE_INDEX);
1245 bam_pipe_reset(&bam, CMD_PIPE_INDEX);
1246
1247}
1248void
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001249flash_set_ptable(struct ptable *new_ptable)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001250{
1251 ASSERT(flash_ptable == NULL && new_ptable != NULL);
1252 flash_ptable = new_ptable;
1253}
1254
1255/* Note: No support for raw reads. */
1256static int
1257qpic_nand_read_page(uint32_t page, unsigned char* buffer, unsigned char* spareaddr)
1258{
1259 struct cfg_params params;
1260 uint32_t ecc;
1261 uint32_t flash_sts[4];
1262 uint32_t buffer_sts[4];
1263 uint32_t addr_loc_0;
1264 uint32_t addr_loc_1;
1265 struct cmd_element *cmd_list_ptr = ce_array;
1266 struct cmd_element *cmd_list_ptr_start = ce_array;
1267 uint32_t num_cmd_desc = 0;
1268 uint32_t num_data_desc = 0;
1269 uint32_t status;
1270 uint32_t i;
1271 int nand_ret = NANDC_RESULT_SUCCESS;
1272 /* UD bytes in last CW is 512 - cws_per_page *4.
1273 * Since each of the CW read earlier reads 4 spare bytes.
1274 */
1275 uint16_t ud_bytes_in_last_cw = USER_DATA_BYTES_PER_CW - ((flash.cws_per_page - 1) << 2);
1276 uint16_t oob_bytes = DATA_BYTES_IN_IMG_PER_CW - ud_bytes_in_last_cw;
1277
1278 params.addr0 = page << 16;
1279 params.addr1 = (page >> 16) & 0xff;
1280 params.cfg0 = cfg0;
1281 params.cfg1 = cfg1;
1282 params.cmd = NAND_CMD_PAGE_READ_ALL;
1283 params.exec = 1;
1284 ecc = ecc_bch_cfg;
1285
1286 /* Read all the Data bytes in the first 3 CWs. */
1287 addr_loc_0 = NAND_RD_LOC_OFFSET(0);
1288 addr_loc_0 |= NAND_RD_LOC_SIZE(DATA_BYTES_IN_IMG_PER_CW);
1289 addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
1290
1291
1292 addr_loc_1 = NAND_RD_LOC_OFFSET(ud_bytes_in_last_cw);
1293 addr_loc_1 |= NAND_RD_LOC_SIZE(oob_bytes);
1294 addr_loc_1 |= NAND_RD_LOC_LAST_BIT(1);
1295
1296 status = qpic_nand_block_isbad(page / flash.num_pages_per_blk);
1297
1298 if (status)
1299 return status;
1300
1301 for (i = 0; i < flash.cws_per_page; i++)
1302 {
1303 num_cmd_desc = 0;
1304 num_data_desc = 0;
1305
1306 if (i == 0)
1307 {
1308 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(&params, cmd_list_ptr);
1309
1310 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG,(uint32_t)ecc, CE_WRITE_TYPE);
1311 cmd_list_ptr++;
1312 }
1313 else
1314 cmd_list_ptr_start = cmd_list_ptr;
1315
1316 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)params.cmd, CE_WRITE_TYPE);
1317 cmd_list_ptr++;
1318
1319 if (i == flash.cws_per_page - 1)
1320 {
1321 addr_loc_0 = NAND_RD_LOC_OFFSET(0);
1322 addr_loc_0 |= NAND_RD_LOC_SIZE(ud_bytes_in_last_cw);
1323 addr_loc_0 |= NAND_RD_LOC_LAST_BIT(0);
1324
1325 /* Write addr loc 1 only for the last CW. */
1326 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(1), (uint32_t)addr_loc_1, CE_WRITE_TYPE);
1327 cmd_list_ptr++;
1328
1329 /* Add Data desc */
1330 bam_add_one_desc(&bam,
1331 DATA_PRODUCER_PIPE_INDEX,
1332 (unsigned char *)buffer,
1333 ud_bytes_in_last_cw,
1334 0);
1335 num_data_desc++;
1336
1337 bam_add_one_desc(&bam,
1338 DATA_PRODUCER_PIPE_INDEX,
1339 (unsigned char *)spareaddr,
1340 oob_bytes,
1341 BAM_DESC_INT_FLAG);
1342 num_data_desc++;
1343
1344 bam_sys_gen_event(&bam, DATA_PRODUCER_PIPE_INDEX, num_data_desc);
1345 }
1346 else
1347 {
1348 /* Add Data desc */
1349 bam_add_one_desc(&bam,
1350 DATA_PRODUCER_PIPE_INDEX,
1351 (unsigned char *)buffer,
1352 DATA_BYTES_IN_IMG_PER_CW,
1353 BAM_DESC_INT_FLAG);
1354 num_data_desc++;
1355 bam_sys_gen_event(&bam, DATA_PRODUCER_PIPE_INDEX, num_data_desc);
1356 }
1357
1358 /* Write addr loc 0. */
1359 bam_add_cmd_element(cmd_list_ptr,
1360 NAND_READ_LOCATION_n(0),
1361 (uint32_t)addr_loc_0,
1362 CE_WRITE_TYPE);
1363
1364 cmd_list_ptr++;
1365 bam_add_cmd_element(cmd_list_ptr,
1366 NAND_EXEC_CMD,
1367 (uint32_t)params.exec,
1368 CE_WRITE_TYPE);
1369 cmd_list_ptr++;
1370
1371 /* Enqueue the desc for the above commands */
1372 bam_add_one_desc(&bam,
1373 CMD_PIPE_INDEX,
1374 (unsigned char*)cmd_list_ptr_start,
1375 (uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start,
Deepa Dinamani4b718ec2012-09-20 11:24:47 -07001376 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG | BAM_DESC_LOCK_FLAG);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001377 num_cmd_desc++;
1378
1379 qpic_nand_wait_for_cmd_exec(num_cmd_desc);
1380
1381 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
1382
1383 /* Save the status registers. */
1384 flash_sts[i] = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr++);
1385 buffer_sts[i] = qpic_nand_read_reg(NAND_BUFFER_STATUS, 0, cmd_list_ptr++);
1386
1387 buffer += DATA_BYTES_IN_IMG_PER_CW;
1388 }
1389
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001390 /* Read the buffer status again so that we can unlock the bam with this desc. */
1391 buffer_sts[--i] = qpic_nand_read_reg(NAND_BUFFER_STATUS, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr++);
Deepa Dinamani4b718ec2012-09-20 11:24:47 -07001392
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001393 /* Check status */
1394 for (i = 0; i < flash.cws_per_page ; i ++)
1395 if (qpic_nand_check_status(flash_sts[i]))
1396 {
1397 nand_ret = NANDC_RESULT_BAD_PAGE;
1398 dprintf(CRITICAL, "NAND page read failed. page: %x\n", page);
1399 goto qpic_nand_read_page_error;
1400 }
1401
1402qpic_nand_read_page_error:
1403return nand_ret;
1404}
1405
1406/* Function to read a flash partition.
1407 * ptn : Partition to read.
1408 * extra_per_page : Spare data to be read.
1409 * offset : Num of bytes offset into the partition.
1410 * data : Buffer to read the data into.
1411 * bytes : Num of bytes to be read.
1412 */
1413 /* TODO: call this func read_partition. */
1414int
1415flash_read_ext(struct ptentry *ptn,
1416 unsigned extra_per_page,
1417 unsigned offset,
1418 void *data,
1419 unsigned bytes)
1420{
1421 uint32_t page =
1422 (ptn->start * flash.num_pages_per_blk) + (offset / flash.page_size);
1423 uint32_t lastpage = (ptn->start + ptn->length) * flash.num_pages_per_blk;
1424 uint32_t count =
1425 (bytes + flash.page_size - 1 + extra_per_page) / (flash.page_size +
1426 extra_per_page);
1427 uint32_t *spare = (unsigned *)flash_spare_bytes;
1428 uint32_t errors = 0;
1429 unsigned char *image = data;
1430 int result = 0;
1431 uint32_t current_block =
1432 (page - (page & flash.num_pages_per_blk_mask)) / flash.num_pages_per_blk;
1433 uint32_t start_block = ptn->start;
1434 uint32_t start_block_count = 0;
1435 uint32_t isbad = 0;
1436
1437 /* Verify first byte is at page boundary. */
1438 if (offset & (flash.page_size - 1))
1439 {
1440 dprintf(CRITICAL, "Read request start not at page boundary: %d\n",
1441 offset);
1442 return NANDC_RESULT_PARAM_INVALID;
1443 }
1444
1445 /* Adjust page offset based on number of bad blocks from start to current page */
1446 if (start_block < current_block)
1447 {
1448 start_block_count = (current_block - start_block);
1449 while (start_block_count
1450 && (start_block < (ptn->start + ptn->length)))
1451 {
Deepa Dinamani4b718ec2012-09-20 11:24:47 -07001452 isbad = qpic_nand_block_isbad(start_block);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001453 if (isbad)
1454 page += flash.num_pages_per_blk;
1455 else
1456 start_block_count--;
1457 start_block++;
1458 }
1459 }
1460
1461 while ((page < lastpage) && !start_block_count)
1462 {
1463 if (count == 0)
1464 {
1465 dprintf(INFO, "flash_read_image: success (%d errors)\n",
1466 errors);
1467 return NANDC_RESULT_SUCCESS;
1468 }
1469
1470 result = qpic_nand_read_page(page, image, (unsigned char *)spare);
1471
1472 if (result == NANDC_RESULT_BAD_PAGE)
1473 {
1474 /* bad page, go to next page. */
1475 page++;
1476 errors++;
1477 continue;
1478 }
1479 else if (result == NANDC_RESULT_BAD_BLOCK)
1480 {
1481 /* bad block, go to next block same offset. */
1482 page += flash.num_pages_per_blk;
1483 errors++;
1484 continue;
1485 }
1486
1487 page++;
1488 image += flash.page_size;
1489 /* Copy spare bytes to image */
1490 memcpy(image, spare, extra_per_page);
1491 image += extra_per_page;
1492 count -= 1;
1493 }
1494
1495 /* could not find enough valid pages before we hit the end */
1496 dprintf(CRITICAL, "flash_read_image: failed (%d errors)\n", errors);
1497 return NANDC_RESULT_FAILURE;
1498}
1499
1500int
1501flash_erase(struct ptentry *ptn)
1502{
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001503 uint32_t block = ptn->start;
1504 uint32_t count = ptn->length;
1505 int ret = 0;
1506
1507 ret = qpic_nand_blk_erase(ptn->start * flash.num_pages_per_blk);
1508
1509 if (ret)
1510 dprintf(CRITICAL, "Erase operation failed \n");
1511
1512 return ret;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001513}
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001514
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001515int
1516flash_ecc_bch_enabled()
1517{
1518 return (flash.ecc_width == NAND_WITH_4_BIT_ECC)? 0 : 1;
1519}
1520
1521int
1522flash_write(struct ptentry *ptn,
1523 unsigned extra_per_page,
1524 const void *data,
1525 unsigned bytes)
1526{
1527 uint32_t page = ptn->start * flash.num_pages_per_blk;
1528 uint32_t lastpage = (ptn->start + ptn->length) * flash.num_pages_per_blk;
1529 uint32_t *spare = (unsigned *)flash_spare_bytes;
1530 const unsigned char *image = data;
1531 uint32_t wsize = flash.page_size + extra_per_page;
1532 int r;
1533
1534 memset(spare, 0xff, (flash.spare_size / flash.cws_per_page));
1535
1536 while (bytes > 0)
1537 {
1538 if (bytes < wsize)
1539 {
1540 dprintf(CRITICAL,
1541 "flash_write_image: image undersized (%d < %d)\n",
1542 bytes,
1543 wsize);
1544 return -1;
1545 }
1546
1547 if (page >= lastpage)
1548 {
1549 dprintf(CRITICAL, "flash_write_image: out of space\n");
1550 return -1;
1551 }
1552
1553 if ((page & flash.num_pages_per_blk_mask) == 0)
1554 {
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001555 if (qpic_nand_blk_erase(page))
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001556 {
1557 dprintf(INFO,
1558 "flash_write_image: bad block @ %d\n",
1559 page / flash.num_pages_per_blk);
1560
1561 page += flash.num_pages_per_blk;
1562 continue;
1563 }
1564 }
1565
1566 if (extra_per_page)
1567 {
1568 r = qpic_nand_write_page(page,
1569 NAND_CFG,
1570 image,
1571 image + flash.page_size);
1572 }
1573 else
1574 {
1575 r = qpic_nand_write_page(page, NAND_CFG, image, spare);
1576 }
1577
1578 if (r)
1579 {
1580 dprintf(INFO,
1581 "flash_write_image: write failure @ page %d (src %d)\n",
1582 page,
1583 image - (const unsigned char *)data);
1584
1585 image -= (page & flash.num_pages_per_blk_mask) * wsize;
1586 bytes += (page & flash.num_pages_per_blk_mask) * wsize;
1587 page &= ~flash.num_pages_per_blk_mask;
1588 if (qpic_nand_blk_erase(page))
1589 {
1590 dprintf(INFO,
1591 "flash_write_image: erase failure @ page %d\n",
1592 page);
1593 }
1594
1595 qpic_nand_mark_badblock(page);
1596
1597 dprintf(INFO,
1598 "flash_write_image: restart write @ page %d (src %d)\n",
1599 page, image - (const unsigned char *)data);
1600
1601 page += flash.num_pages_per_blk;
1602 continue;
1603 }
1604 page++;
1605 image += wsize;
1606 bytes -= wsize;
1607 }
1608
1609 /* erase any remaining pages in the partition */
1610 page = (page + flash.num_pages_per_blk_mask) & (~flash.num_pages_per_blk_mask);
1611
1612 while (page < lastpage)
1613 {
1614 if (qpic_nand_blk_erase(page))
1615 {
1616 dprintf(INFO, "flash_write_image: bad block @ %d\n",
1617 page / flash.num_pages_per_blk);
1618 }
1619 page += flash.num_pages_per_blk;
1620 }
1621
1622 dprintf(INFO, "flash_write_image: success\n");
1623 return 0;
1624}