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