blob: 16a498ba29a9d31d58ff651aa6786ce04bf54c52 [file] [log] [blame]
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001/*
2 * Copyright (c) 2008, Google Inc.
3 * All rights reserved.
Deepa Dinamani52aca8d2013-02-05 11:41:41 -08004 * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
Deepa Dinamanie4573be2012-08-03 16:32:29 -07005 * 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>
Deepa Dinamani0bf2f442012-10-19 11:41:06 -070037#include <platform.h>
Amol Jadib726c3b2012-09-13 13:51:23 -070038#include <platform/clock.h>
Deepa Dinamanie4573be2012-08-03 16:32:29 -070039
40static uint32_t nand_base;
41static struct ptable *flash_ptable;
42static struct flash_info flash;
43static unsigned char *flash_spare_bytes;
44static uint32_t cfg0;
45static uint32_t cfg1;
46static uint32_t cfg0_raw;
47static uint32_t cfg1_raw;
48static uint32_t ecc_bch_cfg;
49
50struct cmd_element ce_array[100];
51
52#define QPIC_BAM_DATA_FIFO_SIZE 64
53#define QPIC_BAM_CMD_FIFO_SIZE 64
54
55static struct bam_desc cmd_desc_fifo[QPIC_BAM_CMD_FIFO_SIZE] __attribute__ ((aligned(BAM_DESC_SIZE)));
56static struct bam_desc data_desc_fifo[QPIC_BAM_DATA_FIFO_SIZE] __attribute__ ((aligned(BAM_DESC_SIZE)));
57
58static struct bam_instance bam;
59static uint8_t *bbtbl;
60
Deepa Dinamani0bf2f442012-10-19 11:41:06 -070061static uint8_t* rdwr_buf;
62
Deepa Dinamanie4573be2012-08-03 16:32:29 -070063static struct flash_id supported_flash[] = {
Deepa Dinamani649a94a2013-03-07 14:37:31 -080064 /* Flash ID ID Mask Density(MB) Wid Pgsz Blksz oobsz 8-bit ECCf */
65 {0x1590AC2C, 0xFFFFFFFF, 0x20000000, 0, 2048, 0x00020000, 0x40, 0},
66 {0x2690AC2C, 0xFFFFFFFF, 0x20000000, 0, 4096, 0x00040000, 0xE0, 1},
Deepa Dinamanie4573be2012-08-03 16:32:29 -070067 /* Note: Width flag is 0 for 8 bit Flash and 1 for 16 bit flash */
Deepa Dinamanie4573be2012-08-03 16:32:29 -070068};
69
Deepa Dinamanie4573be2012-08-03 16:32:29 -070070static void
71qpic_nand_wait_for_cmd_exec(uint32_t num_desc)
72{
73 /* Create a read/write event to notify the periperal of the added desc. */
74 bam_sys_gen_event(&bam, CMD_PIPE_INDEX, num_desc);
75
76 /* Wait for the descriptors to be processed */
77 bam_wait_for_interrupt(&bam, CMD_PIPE_INDEX, P_PRCSD_DESC_EN_MASK);
78
79 /* Read offset update for the circular FIFO */
80 bam_read_offset_update(&bam, CMD_PIPE_INDEX);
81}
82
83static void
84qpic_nand_wait_for_data(uint32_t pipe_num)
85{
86 /* Wait for the descriptors to be processed */
87 bam_wait_for_interrupt(&bam, pipe_num, P_PRCSD_DESC_EN_MASK);
88
89 /* Read offset update for the circular FIFO */
90 bam_read_offset_update(&bam, pipe_num);
91}
92
93static uint32_t
94qpic_nand_read_reg(uint32_t reg_addr,
95 uint8_t flags,
96 struct cmd_element *cmd_list_ptr)
97{
98 uint32_t val;
99
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700100 bam_add_cmd_element(cmd_list_ptr, reg_addr, (uint32_t)PA((addr_t)&val), CE_READ_TYPE);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700101
102 /* Enqueue the desc for the above command */
103 bam_add_one_desc(&bam,
104 CMD_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700105 (unsigned char*)PA((addr_t)cmd_list_ptr),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700106 BAM_CE_SIZE,
107 BAM_DESC_CMD_FLAG| BAM_DESC_INT_FLAG | flags);
108
109 qpic_nand_wait_for_cmd_exec(1);
110
111 return val;
112}
113
Deepa Dinamani19530062012-10-03 14:43:05 -0700114/* Assume the BAM is in a locked state. */
115void
sundarajan srinivasan7ced6482013-03-21 16:01:18 -0700116qpic_nand_erased_status_reset(struct cmd_element *cmd_list_ptr, uint8_t flags)
Deepa Dinamani19530062012-10-03 14:43:05 -0700117{
118 uint32_t val = 0;
119
120 /* Reset the Erased Codeword/Page detection controller. */
121 val = NAND_ERASED_CW_DETECT_CFG_RESET_CTRL;
122
123 bam_add_cmd_element(cmd_list_ptr, NAND_ERASED_CW_DETECT_CFG, val, CE_WRITE_TYPE);
124
125 /* Enqueue the desc for the above command */
126 bam_add_one_desc(&bam,
127 CMD_PIPE_INDEX,
128 (unsigned char*)cmd_list_ptr,
129 BAM_CE_SIZE,
sundarajan srinivasan7ced6482013-03-21 16:01:18 -0700130 BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG | flags);
Deepa Dinamani19530062012-10-03 14:43:05 -0700131
132 qpic_nand_wait_for_cmd_exec(1);
133
134 /* Enable the Erased Codeword/Page detection
135 * controller to check the data as it arrives.
136 * Also disable ECC reporting for an erased CW.
137 */
138 val = NAND_ERASED_CW_DETECT_CFG_ACTIVATE_CTRL | NAND_ERASED_CW_DETECT_ERASED_CW_ECC_MASK;
139
140 bam_add_cmd_element(cmd_list_ptr, NAND_ERASED_CW_DETECT_CFG, val, CE_WRITE_TYPE);
141
142 /* Enqueue the desc for the above command */
143 bam_add_one_desc(&bam,
144 CMD_PIPE_INDEX,
145 (unsigned char*)cmd_list_ptr,
146 BAM_CE_SIZE,
147 BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG);
148
149 qpic_nand_wait_for_cmd_exec(1);
150}
151
152static nand_result_t
153qpic_nand_check_status(uint32_t status)
154{
155 uint32_t erase_sts;
156
157 /* Check for errors */
158 if (status & NAND_FLASH_ERR)
159 {
160 /* Check if this is an ECC error on an erased page. */
161 if (status & NAND_FLASH_OP_ERR)
162 {
163 erase_sts = qpic_nand_read_reg(NAND_ERASED_CW_DETECT_STATUS, 0, ce_array);
164 if ((erase_sts & (1 << NAND_ERASED_CW_DETECT_STATUS_PAGE_ALL_ERASED)))
165 {
166 /* Mask the OP ERROR. */
167 status &= ~NAND_FLASH_OP_ERR;
sundarajan srinivasan7ced6482013-03-21 16:01:18 -0700168 qpic_nand_erased_status_reset(ce_array, 0);
Deepa Dinamani19530062012-10-03 14:43:05 -0700169 }
170 }
171
172 /* ECC error flagged on an erased page read.
173 * Ignore and return success.
174 */
175 if (!(status & NAND_FLASH_ERR))
176 return NANDC_RESULT_SUCCESS;
177
178 dprintf(CRITICAL, "Nand Flash error. Status = %d\n", status);
179
180 if (status & NAND_FLASH_TIMEOUT_ERR)
181 return NANDC_RESULT_TIMEOUT;
182 else
183 return NANDC_RESULT_FAILURE;
184 }
185
186 return NANDC_RESULT_SUCCESS;
187}
188
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700189static uint32_t
190qpic_nand_fetch_id(struct flash_info *flash)
191{
192 struct cmd_element *cmd_list_ptr = ce_array;
193 struct cmd_element *cmd_list_ptr_start = ce_array;
194 int num_desc = 0;
195 uint32_t status;
196 uint32_t id;
197 uint32_t flash_cmd = NAND_CMD_FETCH_ID;
198 uint32_t exec_cmd = 1;
199 int nand_ret = NANDC_RESULT_SUCCESS;
200
201 /* Issue the Fetch id command to the NANDc */
202 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)flash_cmd, CE_WRITE_TYPE);
203 cmd_list_ptr++;
204
205 /* Execute the cmd */
206 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)exec_cmd, CE_WRITE_TYPE);
207 cmd_list_ptr++;
208
209 /* Prepare the cmd desc for the above commands */
210 bam_add_one_desc(&bam,
211 CMD_PIPE_INDEX,
212 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700213 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700214 BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG |
215 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG);
216
217 /* Keep track of the number of desc added. */
218 num_desc++;
219 qpic_nand_wait_for_cmd_exec(num_desc);
220
221 cmd_list_ptr_start = ce_array;
222 cmd_list_ptr = ce_array;
223
224 /* Read the status register */
225 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr);
226
227 /* Check for errors */
228 nand_ret = qpic_nand_check_status(status);
229 if (nand_ret)
230 {
231 dprintf( CRITICAL, "Read ID cmd status failed\n");
232 goto qpic_nand_fetch_id_err;
233 }
234
235 /* Read the id */
236 id = qpic_nand_read_reg(NAND_READ_ID, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr);
237
238 flash->id = id;
239 flash->vendor = id & 0xff;
240 flash->device = (id >> 8) & 0xff;
241 flash->dev_cfg = (id >> 24) & 0xFF;
242 flash->widebus = 0;
243 flash->widebus &= (id >> 24) & 0xFF;
244 flash->widebus = flash->widebus? 1: 0;
245
246qpic_nand_fetch_id_err:
247 return nand_ret;
248}
249
250static int
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800251qpic_bam_init(struct qpic_nand_init_config *config)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700252{
253 uint32_t bam_ret = NANDC_RESULT_SUCCESS;
254
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800255 bam.base = config->bam_base;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700256 /* Set Read pipe params. */
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800257 bam.pipe[DATA_PRODUCER_PIPE_INDEX].pipe_num = config->pipes.read_pipe;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700258 /* System consumer */
259 bam.pipe[DATA_PRODUCER_PIPE_INDEX].trans_type = BAM2SYS;
260 bam.pipe[DATA_PRODUCER_PIPE_INDEX].fifo.size = QPIC_BAM_DATA_FIFO_SIZE;
261 bam.pipe[DATA_PRODUCER_PIPE_INDEX].fifo.head = data_desc_fifo;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700262 bam.pipe[DATA_PRODUCER_PIPE_INDEX].lock_grp = config->pipes.read_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700263
264 /* Set Write pipe params. */
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800265 bam.pipe[DATA_CONSUMER_PIPE_INDEX].pipe_num = config->pipes.write_pipe;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700266 /* System producer */
267 bam.pipe[DATA_CONSUMER_PIPE_INDEX].trans_type = SYS2BAM;
268 bam.pipe[DATA_CONSUMER_PIPE_INDEX].fifo.size = QPIC_BAM_DATA_FIFO_SIZE;
269 bam.pipe[DATA_CONSUMER_PIPE_INDEX].fifo.head = data_desc_fifo;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700270 bam.pipe[DATA_CONSUMER_PIPE_INDEX].lock_grp = config->pipes.write_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700271
272 /* Set Cmd pipe params. */
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800273 bam.pipe[CMD_PIPE_INDEX].pipe_num = config->pipes.cmd_pipe;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700274 /* System consumer */
275 bam.pipe[CMD_PIPE_INDEX].trans_type = BAM2SYS;
276 bam.pipe[CMD_PIPE_INDEX].fifo.size = QPIC_BAM_CMD_FIFO_SIZE;
277 bam.pipe[CMD_PIPE_INDEX].fifo.head = cmd_desc_fifo;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700278 bam.pipe[CMD_PIPE_INDEX].lock_grp = config->pipes.cmd_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700279
280 /* Programs the threshold for BAM transfer
281 * When this threshold is reached, BAM signals the peripheral via the pipe_bytes_available
282 * interface.
283 * The peripheral is signalled with this notification in the following cases:
284 * a. It has accumulated all the descriptors.
285 * b. It has accumulated more than threshold bytes.
286 * c. It has reached EOT (End Of Transfer).
287 * Note: this value needs to be set by the h/w folks and is specific for each peripheral.
288 */
289 bam.threshold = 32;
290
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800291 /* Set the EE. */
292 bam.ee = config->ee;
293
294 /* Set the max desc length for this BAM. */
295 bam.max_desc_len = config->max_desc_len;
296
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700297 /* BAM Init. */
298 bam_init(&bam);
299
300 /* Initialize BAM QPIC read pipe */
301 bam_sys_pipe_init(&bam, DATA_PRODUCER_PIPE_INDEX);
302
303 /* Init read fifo */
304 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[DATA_PRODUCER_PIPE_INDEX].pipe_num);
305
306 if (bam_ret)
307 {
308 dprintf(CRITICAL, "QPIC:NANDc BAM Read FIFO init error\n");
309 bam_ret = NANDC_RESULT_FAILURE;
310 goto qpic_nand_bam_init_error;
311 }
312
313 /* Initialize BAM QPIC write pipe */
314 bam_sys_pipe_init(&bam, DATA_CONSUMER_PIPE_INDEX);
315
316 /* Init write fifo. Use the same fifo as read fifo. */
317 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[DATA_CONSUMER_PIPE_INDEX].pipe_num);
318
319 if (bam_ret)
320 {
321 dprintf(CRITICAL, "QPIC: NANDc: BAM Write FIFO init error\n");
322 bam_ret = NANDC_RESULT_FAILURE;
323 goto qpic_nand_bam_init_error;
324 }
325
326 /* Initialize BAM QPIC cmd pipe */
327 bam_sys_pipe_init(&bam, CMD_PIPE_INDEX);
328
329 /* Init cmd fifo */
330 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[CMD_PIPE_INDEX].pipe_num);
331
332 if (bam_ret)
333 {
334 dprintf(CRITICAL, "QPIC:NANDc BAM CMD FIFO init error\n");
335 bam_ret = NANDC_RESULT_FAILURE;
336 goto qpic_nand_bam_init_error;
337 }
338
339qpic_nand_bam_init_error:
340return bam_ret;
341}
342
343/* Adds command elements for addr and cfg register writes.
344 * cfg: Defines the configuration for the flash cmd.
345 * start: Address where the command elements are added.
346 *
347 * Returns the address where the next cmd element can be added.
348 */
349static struct cmd_element*
350qpic_nand_add_addr_n_cfg_ce(struct cfg_params *cfg,
351 struct cmd_element *start)
352{
353 struct cmd_element *cmd_list_ptr = start;
354
355 bam_add_cmd_element(cmd_list_ptr, NAND_ADDR0, (uint32_t)cfg->addr0, CE_WRITE_TYPE);
356 cmd_list_ptr++;
357 bam_add_cmd_element(cmd_list_ptr, NAND_ADDR1, (uint32_t)cfg->addr1, CE_WRITE_TYPE);
358 cmd_list_ptr++;
359 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_CFG0, (uint32_t)cfg->cfg0, CE_WRITE_TYPE);
360 cmd_list_ptr++;
361 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_CFG1, (uint32_t)cfg->cfg1, CE_WRITE_TYPE);
362 cmd_list_ptr++;
363
364 return cmd_list_ptr;
365}
366
367
368static struct cmd_element*
369qpic_nand_add_onfi_probe_ce(struct onfi_probe_params *params,
370 struct cmd_element *start)
371{
372 struct cmd_element *cmd_list_ptr = start;
373
374 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(&params->cfg, cmd_list_ptr);
375
376 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD1, (uint32_t)params->dev_cmd1, CE_WRITE_TYPE);
377 cmd_list_ptr++;
378 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD_VLD, (uint32_t)params->vld, CE_WRITE_TYPE);
379 cmd_list_ptr++;
380 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(0), (uint32_t)params->cfg.addr_loc_0, CE_WRITE_TYPE);
381 cmd_list_ptr++;
382 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)params->cfg.cmd, CE_WRITE_TYPE);
383 cmd_list_ptr++;
384 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)params->cfg.exec, CE_WRITE_TYPE);
385 cmd_list_ptr++;
386
387 return cmd_list_ptr;
388}
389
390static int
391onfi_probe_cmd_exec(struct onfi_probe_params *params,
392 unsigned char* data_ptr,
393 int data_len)
394{
395 struct cmd_element *cmd_list_ptr = ce_array;
396 struct cmd_element *cmd_list_ptr_start = ce_array;
397 int num_desc = 0;
398 uint32_t status = 0;
399 int nand_ret = NANDC_RESULT_SUCCESS;
400 uint8_t desc_flags = BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG
401 | BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG;
402
403 params->cfg.addr_loc_0 = 0;
404 params->cfg.addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
405 params->cfg.addr_loc_0 |= NAND_RD_LOC_OFFSET(0);
406 params->cfg.addr_loc_0 |= NAND_RD_LOC_SIZE(data_len);
407
408 cmd_list_ptr = qpic_nand_add_onfi_probe_ce(params, cmd_list_ptr);
409
410 /* Enqueue the desc for the above commands */
411 bam_add_one_desc(&bam,
412 CMD_PIPE_INDEX,
413 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700414 PA((addr_t)(uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700415 desc_flags);
416
417 cmd_list_ptr_start = cmd_list_ptr;
418 num_desc++;
419
420 /* Add Data desc */
421 bam_add_desc(&bam,
422 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700423 (unsigned char *)PA((addr_t)data_ptr),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700424 data_len,
425 BAM_DESC_INT_FLAG);
426
427 /* Wait for the commands to be executed */
428 qpic_nand_wait_for_cmd_exec(num_desc);
429
430 /* Read buffer status and check for errors. */
431 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr++);
432
433 if (qpic_nand_check_status(status))
434 {
435 nand_ret = NANDC_RESULT_FAILURE;
436 goto onfi_probe_exec_err;
437 }
438
439 /* Wait for data to be available */
440 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
441
442 /* Check for errors */
443 nand_ret = qpic_nand_check_status(status);
444
445onfi_probe_exec_err:
446 return nand_ret;
447}
448
449/* TODO: check why both vld and cmd need to be written. */
450void
451qpic_nand_onfi_probe_cleanup(uint32_t vld, uint32_t dev_cmd1)
452{
453 struct cmd_element *cmd_list_ptr = ce_array;
454 struct cmd_element *cmd_list_ptr_start = ce_array;
455
456 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD1, dev_cmd1, CE_WRITE_TYPE);
457 cmd_list_ptr++;
458 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD_VLD, vld, CE_WRITE_TYPE);
459 cmd_list_ptr++;
460
461 /* Enqueue the desc for the above commands */
462 bam_add_one_desc(&bam,
463 CMD_PIPE_INDEX,
464 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700465 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700466 BAM_DESC_UNLOCK_FLAG | BAM_DESC_CMD_FLAG| BAM_DESC_INT_FLAG);
467
468 qpic_nand_wait_for_cmd_exec(1);
469}
470
471static int
472qpic_nand_onfi_save_params(struct onfi_param_page *param_page, struct flash_info *flash)
473{
474 int onfi_ret = NANDC_RESULT_SUCCESS;
475 uint32_t ecc_bits;
476
477 onfi_ret = qpic_nand_fetch_id(flash);
478
479 if (onfi_ret)
480 {
481 dprintf(CRITICAL, "Fetch ID cmd failed\n");
482 goto onfi_save_params_err;
483 }
484
485 flash->page_size = param_page->data_per_pg;
486 flash->block_size = param_page->pgs_per_blk * flash->page_size;
487 flash->num_blocks = param_page->blks_per_LUN;
488 flash->widebus = param_page->feature_supported & 0x1;
489 flash->density = param_page->blks_per_LUN * flash->blksize;
490 flash->spare_size = param_page->spare_per_pg;
491 ecc_bits = param_page->num_bits_ecc_correctability;
492 flash->num_pages_per_blk = param_page->pgs_per_blk;
493 flash->num_pages_per_blk_mask = param_page->pgs_per_blk - 1;
494
495 if (ecc_bits >= 8)
496 flash->ecc_width = NAND_WITH_8_BIT_ECC;
497 else
498 flash->ecc_width = NAND_WITH_4_BIT_ECC;
499
500 onfi_save_params_err:
501 return onfi_ret;
502}
503
504static void
505qpic_nand_save_config(struct flash_info *flash)
506{
507
508 /* Save Configurations */
509 flash->cws_per_page = flash->page_size >> NAND_CW_DIV_RIGHT_SHIFT;
510
Deepa Dinamani16663a62013-02-07 16:25:59 -0800511 /* Verify that we have enough buffer to handle all the cws in a page. */
512 ASSERT(flash->cws_per_page <= QPIC_NAND_MAX_CWS_IN_PAGE);
513
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700514 /* Codeword Size = UD_SIZE_BYTES + ECC_PARITY_SIZE_BYTES
515 * + SPARE_SIZE_BYTES + Bad Block size
516 */
517 if (flash->ecc_width & NAND_WITH_8_BIT_ECC)
518 {
519 flash->cw_size = NAND_CW_SIZE_8_BIT_ECC;
520 ecc_bch_cfg |= (1 << NAND_DEV0_ECC_MODE_SHIFT); /* Use 8-bit ecc */
521
522 if (flash->widebus)
523 {
524 cfg0 |= (0 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
525 ecc_bch_cfg |= (14 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
526 }
527 else
528 {
529 cfg0 |= (2 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
530 ecc_bch_cfg |= (13 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
531 }
532 }
533 else
534 {
535 flash->cw_size = NAND_CW_SIZE_4_BIT_ECC;
536
537 if (flash->widebus)
538 {
539 cfg0 |= (2 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
540 ecc_bch_cfg |= (8 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
541 }
542 else
543 {
544 cfg0 |= (4 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
545 ecc_bch_cfg |= (7 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
546 }
547 }
548
549 /* BAD_BLOCK_BYTE_NUM = Page Size -
550 * (CW_PER_PAGE * Codeword Size) + 1
551 * Note: Set CW_PER_PAGE to 1 less than the actual number.
552 */
553 flash->bad_blk_loc = flash->page_size - flash->cw_size * (flash->cws_per_page - 1) + 1;
554
555 cfg0 |= ((flash->cws_per_page - 1) << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT) /* 4/8 cw/pg for 2/4k */
556 |(DATA_BYTES_IN_IMG_PER_CW << NAND_DEV0_CFG0_UD_SIZE_BYTES_SHIFT) /* 516 user data bytes */
557 |(5 << NAND_DEV0_CFG0_ADDR_CYCLE_SHIFT) /* 5 address cycles */
558 |(0 << NAND_DEV0_CFG0_DIS_STS_AFTER_WR_SHIFT);/* Send read status cmd after each write. */
559
560 cfg1 |= (7 << NAND_DEV0_CFG1_RECOVERY_CYCLES_SHIFT) /* 8 recovery cycles */
561 |(0 << NAND_DEV0_CFG1_CS_ACTIVE_BSY_SHIFT) /* Allow CS deassertion */
562 |(flash->bad_blk_loc << NAND_DEV0_CFG1_BAD_BLK_BYTE_NUM_SHIFT)/* Bad block marker location */
563 |(0 << NAND_DEV0_CFG1_BAD_BLK_IN_SPARE_SHIFT) /* Bad block in user data area */
564 |(2 << NAND_DEV0_CFG1_WR_RD_BSY_GAP_SHIFT) /* 8 cycle tWB/tRB */
565 |(flash->widebus << NAND_DEV0_CFG1_WIDE_BUS_SHIFT); /* preserve wide flash flag */
566
567 cfg0_raw = ((flash->cws_per_page- 1) << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT)
568 |(5 << NAND_DEV0_CFG0_ADDR_CYCLE_SHIFT)
569 |(516 << NAND_DEV0_CFG0_UD_SIZE_BYTES_SHIFT) //figure out the size of cw
570 | (1 << NAND_DEV0_CFG0_DIS_STS_AFTER_WR_SHIFT);
571
572 cfg1_raw = (7 << NAND_DEV0_CFG1_RECOVERY_CYCLES_SHIFT)
573 | (0 << NAND_DEV0_CFG1_CS_ACTIVE_BSY_SHIFT)
574 | (17 << NAND_DEV0_CFG1_BAD_BLK_BYTE_NUM_SHIFT)
575 | (1 << NAND_DEV0_CFG1_BAD_BLK_IN_SPARE_SHIFT)
576 | (2 << NAND_DEV0_CFG1_WR_RD_BSY_GAP_SHIFT)
577 | (flash->widebus << NAND_DEV0_CFG1_WIDE_BUS_SHIFT)
578 |1 ; /* to disable reed solomon ecc..this feild is now read only. */
579
580 ecc_bch_cfg |= (0 << NAND_DEV0_ECC_DISABLE_SHIFT) /* Enable ECC */
581 | (0 << NAND_DEV0_ECC_SW_RESET_SHIFT) /* Put ECC core in op mode */
582 | (DATA_BYTES_IN_IMG_PER_CW << NAND_DEV0_ECC_NUM_DATA_BYTES)
583 | (1 << NAND_DEV0_ECC_FORCE_CLK_OPEN_SHIFT); /* Enable all clocks */
584}
585
586/* Onfi probe should issue the following commands to the flash device:
587 * 1. Read ID - with addr ONFI_READ_ID_ADDR.
588 * This returns the ONFI ASCII string indicating support for ONFI.
589 * 2. Read Prameter Page - with addr ONFI_READ_PARAM_PAGE_ADDR.
590 * This returns the params for the device.
591 * Each command inturn issues commands- ADDR0, ADDR1, chip_select,
592 * cfg0, cfg1, cmd_vld, dev_cmd1, read_loc0, flash, exec.
593 */
594static int
595qpic_nand_onfi_probe(struct flash_info *flash)
596{
597 struct onfi_probe_params params;
598 uint32_t vld;
599 uint32_t dev_cmd1;
600 unsigned char *buffer;
601 unsigned char onfi_str[4];
602 uint32_t *id;
603 struct onfi_param_page *param_page;
604 int onfi_ret = NANDC_RESULT_SUCCESS;
605
606 /* Allocate memory required to read the onfi param page */
607 buffer = (unsigned char*) malloc(ONFI_READ_PARAM_PAGE_BUFFER_SIZE);
608
609 /* Read the vld and dev_cmd1 registers before modifying */
610 vld = qpic_nand_read_reg(NAND_DEV_CMD_VLD, 0, ce_array);
611 dev_cmd1 = qpic_nand_read_reg(NAND_DEV_CMD1, 0, ce_array);
612
613 /* Initialize flash cmd */
614 params.cfg.cmd = NAND_CMD_PAGE_READ;
615 params.cfg.exec = 1;
616
617 /* Execute Read ID cmd */
618
619 /* Initialize the config */
620 params.cfg.cfg0 = NAND_CFG0_RAW_ONFI_ID;
621 params.cfg.cfg1 = NAND_CFG1_RAW_ONFI_ID;
622
623 /* Initialize the cmd and vld */
624 params.dev_cmd1 = (dev_cmd1 & 0xFFFFFF00) | ONFI_READ_ID_CMD;
625 params.vld = vld & 0xFFFFFFFE;
626
627 /* Initialize the address
628 * addr1 is not used bcos of the cfg.
629 */
630 params.cfg.addr0 = ONFI_READ_ID_ADDR;
631 params.cfg.addr1 = 0;
632
633 /* Lock the pipe and execute the cmd. */
634 onfi_ret = onfi_probe_cmd_exec(&params, onfi_str, ONFI_READ_ID_BUFFER_SIZE);
635 if (onfi_ret)
636 {
637 dprintf(CRITICAL, "ONFI Read id cmd failed\n");
638 goto qpic_nand_onfi_probe_err;
639 }
640
641 /* Write back vld and cmd and unlock the pipe. */
642 qpic_nand_onfi_probe_cleanup(vld, dev_cmd1);
643
644 /* Check for onfi string */
645 id = (uint32_t*)onfi_str;
646 if (*id != ONFI_SIGNATURE)
647 {
648 dprintf(CRITICAL, "Not an ONFI device\n");
649 /* Not an onfi device. Return error. */
650 onfi_ret = NANDC_RESULT_DEV_NOT_SUPPORTED;
651 goto qpic_nand_onfi_probe_err;
652 }
653
654 dprintf(INFO, "ONFI device found\n");
655 /* Now read the param page */
656 /* Initialize the config */
657 params.cfg.cfg0 = NAND_CFG0_RAW_ONFI_PARAM_PAGE;
658 params.cfg.cfg1 = NAND_CFG1_RAW_ONFI_PARAM_PAGE;
659
660 /* Initialize the cmd and vld */
661 params.dev_cmd1 = (dev_cmd1 & 0xFFFFFF00) | ONFI_READ_PARAM_PAGE_CMD;
662 params.vld = vld & 0xFFFFFFFE;
663
664 /* Initialize the address
665 * addr1 is not used bcos of the cfg.
666 */
667 params.cfg.addr0 = ONFI_READ_PARAM_PAGE_ADDR;
668 params.cfg.addr1 = 0;
669
670 /* Lock the pipe and execute the cmd. */
671 onfi_ret = onfi_probe_cmd_exec(&params, buffer, ONFI_READ_PARAM_PAGE_BUFFER_SIZE);
672 if (onfi_ret)
673 {
674 dprintf(CRITICAL, "ONFI Read param page failed\n");
675 goto qpic_nand_onfi_probe_err;
676 }
677
678 /* Write back vld and cmd and unlock the pipe. */
679 qpic_nand_onfi_probe_cleanup(vld, dev_cmd1);
680
681 /* Verify the integrity of the returned page */
682 param_page = (struct onfi_param_page*)buffer;
683
684 /* TODO: Add CRC check to validate the param page. */
685
686 /* Save the parameter values */
687 onfi_ret = qpic_nand_onfi_save_params(param_page, flash);
688
689qpic_nand_onfi_probe_err:
690 if (onfi_ret)
691 dprintf(CRITICAL, "ONFI probe failed\n");
692
693 free(buffer);
694
695 return onfi_ret;
696}
697
698/* Enquues a desc for a flash cmd with NWD flag set:
699 * cfg: Defines the configuration for the flash cmd.
700 * start: Address where the command elements are added.
701 *
702 * Returns the address where the next cmd element can be added.
703 */
704struct cmd_element*
705qpic_nand_add_cmd_ce(struct cfg_params *cfg,
706 struct cmd_element *start)
707{
708 struct cmd_element *cmd_list_ptr;
709
710 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(cfg, start);
711
712 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)cfg->cmd, CE_WRITE_TYPE);
713 cmd_list_ptr++;
714
715 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)cfg->exec, CE_WRITE_TYPE);
716 cmd_list_ptr++;
717
718 return cmd_list_ptr;
719}
720
721/* Reads nand_flash_status and resets nand_flash_status and nand_read_status */
722struct cmd_element*
723qpic_nand_add_read_n_reset_status_ce(struct cmd_element *start,
724 uint32_t *flash_status_read,
725 uint32_t read_status)
726{
727 struct cmd_element *cmd_list_ptr = start;
728 uint32_t flash_status_reset;
729 uint32_t read_status_reset;
730
731 /* Read and reset the status registers. */
732 flash_status_reset = NAND_FLASH_STATUS_RESET;
733 read_status_reset = NAND_READ_STATUS_RESET;
734
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700735 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_STATUS, (uint32_t)PA((addr_t)flash_status_read), CE_READ_TYPE);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700736 cmd_list_ptr++;
737 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_STATUS, (uint32_t)flash_status_reset, CE_WRITE_TYPE);
738 cmd_list_ptr++;
739
740 if (read_status)
741 {
742 bam_add_cmd_element(cmd_list_ptr, NAND_READ_STATUS, (uint32_t)read_status_reset, CE_WRITE_TYPE);
743 cmd_list_ptr++;
744 }
745
746 return cmd_list_ptr;
747}
748
749struct cmd_element*
750qpic_nand_add_isbad_cmd_ce(struct cfg_params *cfg,
751 struct cmd_element *start)
752{
753 struct cmd_element *cmd_list_ptr = start;
754
755 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG, (uint32_t)cfg->ecc_cfg, CE_WRITE_TYPE);
756 cmd_list_ptr++;
757
758 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(0), (uint32_t)cfg->addr_loc_0, CE_WRITE_TYPE);
759 cmd_list_ptr++;
760
761 cmd_list_ptr = qpic_nand_add_cmd_ce(cfg, cmd_list_ptr);
762
763 return cmd_list_ptr;
764}
765
766static int
767qpic_nand_block_isbad_exec(struct cfg_params *params,
768 uint8_t *bad_block)
769{
770
771 struct cmd_element *cmd_list_ptr = ce_array;
772 struct cmd_element *cmd_list_ptr_start = ce_array;
773 uint8_t desc_flags = BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG
774 | BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG;
775 int num_desc = 0;
776 uint32_t status = 0;
777 int nand_ret = NANDC_RESULT_SUCCESS;
778
779 cmd_list_ptr = qpic_nand_add_isbad_cmd_ce(params, cmd_list_ptr);
780
781 /* Enqueue the desc for the above commands */
782 bam_add_one_desc(&bam,
783 CMD_PIPE_INDEX,
784 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700785 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700786 desc_flags);
787
788 num_desc++;
789
790 /* Add Data desc */
791 bam_add_desc(&bam,
792 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700793 (unsigned char *)PA((addr_t)bad_block),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700794 4,
795 BAM_DESC_INT_FLAG);
796
797 qpic_nand_wait_for_cmd_exec(num_desc);
798
Deepa Dinamani19530062012-10-03 14:43:05 -0700799 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr);
800
801 nand_ret = qpic_nand_check_status(status);
802
803 /* Dummy read to unlock pipe. */
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700804 status = qpic_nand_read_reg(NAND_FLASH_STATUS, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr);
805
Deepa Dinamani19530062012-10-03 14:43:05 -0700806 if (nand_ret)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700807 return NANDC_RESULT_FAILURE;
808
809 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
810
811 return nand_ret;
812}
813
814static int
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800815qpic_nand_block_isbad(unsigned page)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700816{
817 unsigned cwperpage;
818 struct cfg_params params;
819 uint8_t bad_block[4];
820 unsigned nand_ret = NANDC_RESULT_SUCCESS;
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800821 uint32_t blk = page / flash.num_pages_per_blk;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700822
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800823 if (bbtbl[blk] == NAND_BAD_BLK_VALUE_IS_GOOD)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700824 return NANDC_RESULT_SUCCESS;
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800825 else if (bbtbl[blk] == NAND_BAD_BLK_VALUE_IS_BAD)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700826 return NANDC_RESULT_BAD_BLOCK;
827 else
828 {
829 /* Read the bad block value from the flash.
830 * Bad block value is stored in the first page of the block.
831 */
832 /* Read the first page in the block. */
833 cwperpage = flash.cws_per_page;
834
835 /* Read page cmd */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800836 params.cmd = NAND_CMD_PAGE_READ_ECC;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700837 /* Clear the CW per page bits */
838 params.cfg0 = cfg0_raw & ~(7U << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT);
839 params.cfg1 = cfg1_raw;
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800840 /* addr0 - Write column addr + few bits in row addr upto 32 bits. */
841 params.addr0 = (page << 16) | (USER_DATA_BYTES_PER_CW * cwperpage);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700842
843 /* addr1 - Write rest of row addr.
844 * This will be all 0s.
845 */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800846 params.addr1 = (page >> 16) & 0xff;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700847 params.addr_loc_0 = NAND_RD_LOC_OFFSET(0);
848 params.addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
849 params.addr_loc_0 |= NAND_RD_LOC_SIZE(4); /* Read 4 bytes */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800850 params.ecc_cfg = ecc_bch_cfg | 0x1; /* Disable ECC */
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700851 params.exec = 1;
852
853 if (qpic_nand_block_isbad_exec(&params, bad_block))
854 {
855 dprintf(CRITICAL,
856 "Could not read bad block value\n");
857 return NANDC_RESULT_FAILURE;
858 }
859
860 if (flash.widebus)
861 {
862 if (bad_block[0] != 0xFF && bad_block[1] != 0xFF)
863 {
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800864 bbtbl[blk] = NAND_BAD_BLK_VALUE_IS_BAD;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700865 nand_ret = NANDC_RESULT_BAD_BLOCK;
866 }
867 }
868 else if (bad_block[0] != 0xFF)
869 {
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800870 bbtbl[blk] = NAND_BAD_BLK_VALUE_IS_BAD;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700871 nand_ret = NANDC_RESULT_BAD_BLOCK;
872 }
873 else
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800874 bbtbl[blk] = NAND_BAD_BLK_VALUE_IS_GOOD;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700875
876 return nand_ret;
877 }
878}
879
880/* Function to erase a block on the nand.
881 * page: Starting page address for the block.
882 */
883static int
884qpic_nand_blk_erase(uint32_t page)
885{
886 struct cfg_params cfg;
887 struct cmd_element *cmd_list_ptr = ce_array;
888 struct cmd_element *cmd_list_ptr_start = ce_array;
889 uint32_t status;
890 int num_desc = 0;
891 uint32_t blk_addr = page / flash.num_pages_per_blk;
Deepa Dinamani19530062012-10-03 14:43:05 -0700892 int nand_ret;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700893
894 /* Erase only if the block is not bad */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800895 if (qpic_nand_block_isbad(page))
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700896 {
897 dprintf(CRITICAL,
898 "NAND Erase error: Block address belongs to bad block: %d\n",
899 blk_addr);
900 return NANDC_RESULT_FAILURE;
901 }
902
903 /* Fill in params for the erase flash cmd */
904 cfg.addr0 = page;
905 cfg.addr1 = 0;
906 /* Clear CW_PER_PAGE in cfg0 */
907 cfg.cfg0 = cfg0 & ~(7U << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT);
908 cfg.cfg1 = cfg1;
909 cfg.cmd = NAND_CMD_BLOCK_ERASE;
910 cfg.exec = 1;
911
912 cmd_list_ptr = qpic_nand_add_cmd_ce(&cfg, cmd_list_ptr);
913
914 /* Enqueue the desc for the above commands */
915 bam_add_one_desc(&bam,
916 CMD_PIPE_INDEX,
917 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700918 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamani2467bbb2012-10-02 13:59:58 -0700919 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG | BAM_DESC_LOCK_FLAG);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700920
921 cmd_list_ptr_start = cmd_list_ptr;
922 num_desc++;
923
924 qpic_nand_wait_for_cmd_exec(num_desc);
925
926 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr);
927
928 cmd_list_ptr++;
929 cmd_list_ptr_start = cmd_list_ptr;
930
931 /* QPIC controller automatically sends
932 * GET_STATUS cmd to the nand card because
933 * of the configuration programmed.
934 * Read the result of GET_STATUS cmd.
935 */
936 cmd_list_ptr = qpic_nand_add_read_n_reset_status_ce(cmd_list_ptr, &status, 1);
937
938 /* Enqueue the desc for the above commands */
939 bam_add_one_desc(&bam,
940 CMD_PIPE_INDEX,
941 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700942 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamani19530062012-10-03 14:43:05 -0700943 BAM_DESC_INT_FLAG | BAM_DESC_CMD_FLAG) ;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700944
945 num_desc = 1;
946 qpic_nand_wait_for_cmd_exec(num_desc);
947
Deepa Dinamani19530062012-10-03 14:43:05 -0700948 status = qpic_nand_check_status(status);
949
950 /* Dummy read to unlock pipe. */
951 nand_ret = qpic_nand_read_reg(NAND_FLASH_STATUS, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr);
952
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700953 /* Check for status errors*/
Deepa Dinamani19530062012-10-03 14:43:05 -0700954 if (status)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700955 {
956 dprintf(CRITICAL,
957 "NAND Erase error: Block address belongs to bad block: %d\n",
958 blk_addr);
959 return NANDC_RESULT_FAILURE;
960 }
961
962 /* Check for PROG_ERASE_OP_RESULT bit for the result of erase operation. */
Deepa Dinamani2467bbb2012-10-02 13:59:58 -0700963 if (!(status & PROG_ERASE_OP_RESULT))
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700964 return NANDC_RESULT_SUCCESS;
965
966 return NANDC_RESULT_FAILURE;
967}
968
969/* Return num of desc added. */
Deepa Dinamani19530062012-10-03 14:43:05 -0700970static void
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700971qpic_nand_add_wr_page_cws_cmd_desc(struct cfg_params *cfg,
972 uint32_t status[],
973 enum nand_cfg_value cfg_mode)
974{
975 struct cmd_element *cmd_list_ptr = ce_array;
976 struct cmd_element *cmd_list_ptr_start = ce_array;
977 uint32_t ecc;
978 int num_desc = 0;
979 int int_flag = 0;
980
981 if (cfg_mode == NAND_CFG)
982 ecc = ecc_bch_cfg;
983 else
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800984 ecc = ecc_bch_cfg | 0x1; /* Disable ECC */
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700985
986 /* Add ECC configuration */
987 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG,
988 (uint32_t)ecc, CE_WRITE_TYPE);
989 cmd_list_ptr++;
990 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(cfg, cmd_list_ptr);
991
992 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD,
993 (uint32_t)cfg->cmd, CE_WRITE_TYPE);
994 cmd_list_ptr++;
995
996 /* Enqueue the desc for the above commands */
997 bam_add_one_desc(&bam,
998 CMD_PIPE_INDEX,
999 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001000 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001001 BAM_DESC_CMD_FLAG | BAM_DESC_LOCK_FLAG);
1002
1003 num_desc++;
1004
1005 /* Add CE for all the CWs */
1006 for (unsigned i = 0; i < flash.cws_per_page; i++)
1007 {
1008 cmd_list_ptr_start = cmd_list_ptr;
Deepa Dinamani19530062012-10-03 14:43:05 -07001009 int_flag = BAM_DESC_INT_FLAG;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001010
1011 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)cfg->exec, CE_WRITE_TYPE);
1012 cmd_list_ptr++;
1013
1014 /* Enqueue the desc for the above commands */
1015 bam_add_one_desc(&bam,
1016 CMD_PIPE_INDEX,
1017 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001018 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001019 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG);
1020
1021 num_desc++;
1022 cmd_list_ptr_start = cmd_list_ptr;
1023
1024 /* Set interrupt bit only for the last CW */
1025 if (i == flash.cws_per_page - 1)
1026 {
1027 cmd_list_ptr = qpic_nand_add_read_n_reset_status_ce(cmd_list_ptr,
1028 &status[i],
1029 1);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001030 }
1031 else
1032 cmd_list_ptr = qpic_nand_add_read_n_reset_status_ce(cmd_list_ptr,
1033 &status[i],
1034 0);
1035
1036 /* Enqueue the desc for the above commands */
1037 bam_add_one_desc(&bam,
1038 CMD_PIPE_INDEX,
1039 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001040 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001041 int_flag | BAM_DESC_CMD_FLAG);
1042 num_desc++;
Deepa Dinamani19530062012-10-03 14:43:05 -07001043
1044 qpic_nand_wait_for_cmd_exec(num_desc);
1045
1046 status[i] = qpic_nand_check_status(status[i]);
1047
1048 num_desc = 0;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001049 }
Deepa Dinamani19530062012-10-03 14:43:05 -07001050 return;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001051}
1052
1053void
1054qpic_add_wr_page_cws_data_desc(const void *buffer,
1055 enum nand_cfg_value cfg_mode,
1056 const void *spareaddr)
1057{
1058 int len;
1059 int flags;
1060 uint32_t start;
1061 unsigned num_desc = 0;
1062
1063 for( unsigned i = 0; i < flash.cws_per_page; i++)
1064 {
1065 flags = 0;
1066
1067 /* Set the interrupt flag on the last CW write for the page. */
1068 if( i == flash.cws_per_page - 1)
1069 flags |= BAM_DESC_INT_FLAG;
1070
1071 if (cfg_mode != NAND_CFG_RAW)
1072 {
1073 start = (uint32_t)buffer + i * DATA_BYTES_IN_IMG_PER_CW;
1074
1075 if (i < (flash.cws_per_page - 1))
1076 {
1077 len = DATA_BYTES_IN_IMG_PER_CW;
1078 flags |= BAM_DESC_EOT_FLAG;
1079 }
1080 else
1081 {
1082 /* Allow space for spare bytes in the last page */
1083 len = USER_DATA_BYTES_PER_CW - ((flash.cws_per_page - 1) << 2);
1084 flags = 0;
1085 }
1086 }
1087 else
1088 {
1089 start = (uint32_t)buffer;
1090 len = flash.cw_size;
1091 flags |= BAM_DESC_EOT_FLAG;
1092 }
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001093 bam_add_one_desc(&bam, DATA_CONSUMER_PIPE_INDEX, (unsigned char*)PA(start), len, flags);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001094 num_desc++;
1095
1096 if ((i == (flash.cws_per_page - 1)) && (cfg_mode == NAND_CFG))
1097 {
1098 /* write extra data */
1099 start = (uint32_t)spareaddr;
1100 len = (flash.cws_per_page << 2);
1101 flags = BAM_DESC_EOT_FLAG | BAM_DESC_INT_FLAG;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001102 bam_add_one_desc(&bam, DATA_CONSUMER_PIPE_INDEX, (unsigned char*)PA(start), len, flags);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001103 num_desc++;
1104 }
1105 }
1106
1107 bam_sys_gen_event(&bam, DATA_CONSUMER_PIPE_INDEX, num_desc);
1108}
1109
1110static nand_result_t
1111qpic_nand_write_page(uint32_t pg_addr,
1112 enum nand_cfg_value cfg_mode,
1113 const void* buffer,
1114 const void* spareaddr)
1115{
1116 struct cfg_params cfg;
Deepa Dinamani16663a62013-02-07 16:25:59 -08001117 uint32_t status[QPIC_NAND_MAX_CWS_IN_PAGE];
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001118 int nand_ret = NANDC_RESULT_SUCCESS;
1119
1120 if (cfg_mode == NAND_CFG_RAW)
1121 {
1122 cfg.cfg0 = cfg0_raw;
1123 cfg.cfg1 = cfg1_raw;
1124 }
1125 else
1126 {
1127 cfg.cfg0 = cfg0;
1128 cfg.cfg1 = cfg1;
1129 }
1130
1131 cfg.cmd = NAND_CMD_PRG_PAGE;
1132 cfg.exec = 1;
1133
1134 cfg.addr0 = pg_addr << 16;
1135 cfg.addr1 = (pg_addr >> 16) & 0xff;
1136
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001137 qpic_add_wr_page_cws_data_desc(buffer, cfg_mode, spareaddr);
1138
Deepa Dinamani19530062012-10-03 14:43:05 -07001139 qpic_nand_add_wr_page_cws_cmd_desc(&cfg, status, cfg_mode);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001140
1141 /* Check for errors */
1142 for(unsigned i = 0; i < flash.cws_per_page; i++)
1143 {
1144 nand_ret = qpic_nand_check_status(status[i]);
1145 if (nand_ret)
1146 {
1147 dprintf(CRITICAL,
1148 "Failed to write CW %d for page: %d\n",
1149 i, pg_addr);
1150 break;
1151 }
1152 }
1153
1154 /* Wait for data to be available */
1155 qpic_nand_wait_for_data(DATA_CONSUMER_PIPE_INDEX);
1156
1157 return nand_ret;
1158}
1159
1160static int
1161qpic_nand_mark_badblock(uint32_t page)
1162{
1163 char empty_buf[NAND_CW_SIZE_8_BIT_ECC];
1164
1165 memset(empty_buf, 0, NAND_CW_SIZE_8_BIT_ECC);
1166
1167 /* Going to first page of the block */
1168 if (page & flash.num_pages_per_blk_mask)
1169 page = page - (page & flash.num_pages_per_blk_mask);
1170
1171 return qpic_nand_write_page(page, NAND_CFG_RAW, empty_buf, 0);
1172}
1173
1174static void
1175qpic_nand_non_onfi_probe(struct flash_info *flash)
1176{
1177 int dev_found = 0;
1178 unsigned index;
1179 uint32_t ecc_bits;
1180
1181 /* Read the nand id. */
1182 qpic_nand_fetch_id(flash);
1183
1184 /* Check if we support the device */
Deepa Dinamani649a94a2013-03-07 14:37:31 -08001185 for (index = 0; index < (ARRAY_SIZE(supported_flash)); index++)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001186 {
1187 if ((flash->id & supported_flash[index].mask) ==
1188 (supported_flash[index].flash_id & (supported_flash[index].mask)))
1189 {
1190 dev_found = 1;
1191 break;
1192 }
1193 }
1194
1195 if (dev_found)
1196 {
1197 flash->page_size = supported_flash[index].pagesize;
1198 flash->block_size = supported_flash[index].blksize;
1199 flash->spare_size = supported_flash[index].oobsize;
1200 ecc_bits = supported_flash[index].ecc_8_bits;
1201
1202 /* Make sure that the block size and page size are defined. */
1203 ASSERT(flash->block_size);
1204 ASSERT(flash->page_size);
1205
1206 flash->num_blocks = supported_flash[index].density;
1207 flash->num_blocks /= (flash->block_size);
1208 flash->num_pages_per_blk = flash->block_size / flash->page_size;
1209 flash->num_pages_per_blk_mask = flash->num_pages_per_blk - 1;
1210
1211 /* Look for 8bit BCH ECC Nand, TODO: ECC Correctability >= 8 */
1212 if (ecc_bits)
1213 flash->ecc_width = NAND_WITH_8_BIT_ECC;
1214 else
1215 flash->ecc_width = NAND_WITH_4_BIT_ECC;
1216
1217 flash->density = supported_flash[index].density;
1218 flash->widebus = supported_flash[index].widebus;
1219
1220 return;
1221 }
1222
1223 /* Flash device is not supported, print flash device info and halt */
1224 if (dev_found == 0)
1225 {
1226 dprintf(CRITICAL, "NAND device is not supported: nandid: 0x%x"
1227 "maker=0x%02x device=0x%02x\n",
1228 flash->id,
1229 flash->vendor,
1230 flash->device);
1231 ASSERT(0);
1232 }
1233
1234 dprintf(INFO, "nandid: 0x%x maker=0x%02x device=0x%02x page_size=%d\n",
1235 flash->id,
1236 flash->vendor,
1237 flash->device,
1238 flash->page_size);
1239
1240 dprintf(INFO, "spare_size=%d block_size=%d num_blocks=%d\n",
1241 flash->spare_size,
1242 flash->block_size,
1243 flash->num_blocks);
1244}
1245
1246void
1247qpic_nand_init(struct qpic_nand_init_config *config)
1248{
1249 uint32_t i;
1250 int nand_ret;
1251
1252 nand_base = config->nand_base;
1253
Deepa Dinamanie9ded132012-11-27 15:03:38 -08001254 qpic_bam_init(config);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001255
Deepa Dinamani649a94a2013-03-07 14:37:31 -08001256 qpic_nand_non_onfi_probe(&flash);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001257
1258 /* Save the RAW and read/write configs */
1259 qpic_nand_save_config(&flash);
1260
1261 flash_spare_bytes = (unsigned char *)malloc(flash.spare_size);
1262
1263 if (flash_spare_bytes == NULL)
1264 {
1265 dprintf(CRITICAL, "Failed to allocate memory for spare bytes\n");
1266 return;
1267 }
1268
1269 /* Create a bad block table */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001270 bbtbl = (uint8_t *) malloc(sizeof(uint8_t) * flash.num_blocks);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001271
1272 if (bbtbl == NULL)
1273 {
1274 dprintf(CRITICAL, "Failed to allocate memory for bad block table\n");
1275 return;
1276 }
1277
1278 for (i = 0; i < flash.num_blocks; i++)
1279 bbtbl[i] = NAND_BAD_BLK_VALUE_NOT_READ;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001280
1281 /* Set aside contiguous memory for reads/writes.
1282 * This is needed as the BAM transfers only work with
1283 * physically contiguous buffers.
1284 * We will copy any data to be written/ to be read from
1285 * nand to this buffer and this buffer will be submitted to BAM.
1286 */
1287 rdwr_buf = (uint8_t*) malloc(flash.page_size + flash.spare_size);
1288
1289 if (rdwr_buf == NULL)
1290 {
1291 dprintf(CRITICAL, "Failed to allocate memory for page reads or writes\n");
1292 return;
1293 }
1294
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001295}
1296
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001297unsigned
1298flash_page_size(void)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001299{
1300 return flash.page_size;
1301}
1302
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001303unsigned
1304flash_block_size(void)
1305{
1306 return flash.block_size;
1307}
1308
Deepa Dinamani8e6b2432012-10-17 17:12:44 -07001309unsigned
1310flash_num_blocks(void)
1311{
1312 return flash.num_blocks;
1313}
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001314
1315struct ptable *
1316flash_get_ptable(void)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001317{
1318 return flash_ptable;
1319}
1320
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001321void
Deepa Dinamani87feab82012-10-04 14:28:05 -07001322qpic_nand_uninit()
1323{
1324 bam_pipe_reset(&bam, DATA_PRODUCER_PIPE_INDEX);
1325 bam_pipe_reset(&bam, DATA_CONSUMER_PIPE_INDEX);
1326 bam_pipe_reset(&bam, CMD_PIPE_INDEX);
1327
1328}
1329void
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001330flash_set_ptable(struct ptable *new_ptable)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001331{
1332 ASSERT(flash_ptable == NULL && new_ptable != NULL);
1333 flash_ptable = new_ptable;
1334}
1335
1336/* Note: No support for raw reads. */
1337static int
1338qpic_nand_read_page(uint32_t page, unsigned char* buffer, unsigned char* spareaddr)
1339{
1340 struct cfg_params params;
1341 uint32_t ecc;
Deepa Dinamani16663a62013-02-07 16:25:59 -08001342 uint32_t flash_sts[QPIC_NAND_MAX_CWS_IN_PAGE];
1343 uint32_t buffer_sts[QPIC_NAND_MAX_CWS_IN_PAGE];
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001344 uint32_t addr_loc_0;
1345 uint32_t addr_loc_1;
1346 struct cmd_element *cmd_list_ptr = ce_array;
1347 struct cmd_element *cmd_list_ptr_start = ce_array;
1348 uint32_t num_cmd_desc = 0;
1349 uint32_t num_data_desc = 0;
1350 uint32_t status;
1351 uint32_t i;
1352 int nand_ret = NANDC_RESULT_SUCCESS;
1353 /* UD bytes in last CW is 512 - cws_per_page *4.
1354 * Since each of the CW read earlier reads 4 spare bytes.
1355 */
1356 uint16_t ud_bytes_in_last_cw = USER_DATA_BYTES_PER_CW - ((flash.cws_per_page - 1) << 2);
1357 uint16_t oob_bytes = DATA_BYTES_IN_IMG_PER_CW - ud_bytes_in_last_cw;
1358
1359 params.addr0 = page << 16;
1360 params.addr1 = (page >> 16) & 0xff;
1361 params.cfg0 = cfg0;
1362 params.cfg1 = cfg1;
1363 params.cmd = NAND_CMD_PAGE_READ_ALL;
1364 params.exec = 1;
1365 ecc = ecc_bch_cfg;
1366
1367 /* Read all the Data bytes in the first 3 CWs. */
1368 addr_loc_0 = NAND_RD_LOC_OFFSET(0);
1369 addr_loc_0 |= NAND_RD_LOC_SIZE(DATA_BYTES_IN_IMG_PER_CW);
1370 addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
1371
1372
1373 addr_loc_1 = NAND_RD_LOC_OFFSET(ud_bytes_in_last_cw);
1374 addr_loc_1 |= NAND_RD_LOC_SIZE(oob_bytes);
1375 addr_loc_1 |= NAND_RD_LOC_LAST_BIT(1);
1376
Deepa Dinamanidc1381e2012-11-15 14:53:24 -08001377 status = qpic_nand_block_isbad(page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001378
1379 if (status)
1380 return status;
1381
sundarajan srinivasan7ced6482013-03-21 16:01:18 -07001382 /* Reset and Configure erased CW/page detection controller */
1383 qpic_nand_erased_status_reset(ce_array, BAM_DESC_LOCK_FLAG);
1384
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001385 for (i = 0; i < flash.cws_per_page; i++)
1386 {
1387 num_cmd_desc = 0;
1388 num_data_desc = 0;
1389
1390 if (i == 0)
1391 {
1392 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(&params, cmd_list_ptr);
1393
1394 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG,(uint32_t)ecc, CE_WRITE_TYPE);
1395 cmd_list_ptr++;
1396 }
1397 else
1398 cmd_list_ptr_start = cmd_list_ptr;
1399
1400 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)params.cmd, CE_WRITE_TYPE);
1401 cmd_list_ptr++;
1402
1403 if (i == flash.cws_per_page - 1)
1404 {
1405 addr_loc_0 = NAND_RD_LOC_OFFSET(0);
1406 addr_loc_0 |= NAND_RD_LOC_SIZE(ud_bytes_in_last_cw);
1407 addr_loc_0 |= NAND_RD_LOC_LAST_BIT(0);
1408
1409 /* Write addr loc 1 only for the last CW. */
1410 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(1), (uint32_t)addr_loc_1, CE_WRITE_TYPE);
1411 cmd_list_ptr++;
1412
1413 /* Add Data desc */
1414 bam_add_one_desc(&bam,
1415 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001416 (unsigned char *)PA((addr_t)buffer),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001417 ud_bytes_in_last_cw,
1418 0);
1419 num_data_desc++;
1420
1421 bam_add_one_desc(&bam,
1422 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001423 (unsigned char *)PA((addr_t)spareaddr),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001424 oob_bytes,
1425 BAM_DESC_INT_FLAG);
1426 num_data_desc++;
1427
1428 bam_sys_gen_event(&bam, DATA_PRODUCER_PIPE_INDEX, num_data_desc);
1429 }
1430 else
1431 {
1432 /* Add Data desc */
1433 bam_add_one_desc(&bam,
1434 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001435 (unsigned char *)PA((addr_t)buffer),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001436 DATA_BYTES_IN_IMG_PER_CW,
1437 BAM_DESC_INT_FLAG);
1438 num_data_desc++;
1439 bam_sys_gen_event(&bam, DATA_PRODUCER_PIPE_INDEX, num_data_desc);
1440 }
1441
1442 /* Write addr loc 0. */
1443 bam_add_cmd_element(cmd_list_ptr,
1444 NAND_READ_LOCATION_n(0),
1445 (uint32_t)addr_loc_0,
1446 CE_WRITE_TYPE);
1447
1448 cmd_list_ptr++;
1449 bam_add_cmd_element(cmd_list_ptr,
1450 NAND_EXEC_CMD,
1451 (uint32_t)params.exec,
1452 CE_WRITE_TYPE);
1453 cmd_list_ptr++;
1454
1455 /* Enqueue the desc for the above commands */
1456 bam_add_one_desc(&bam,
1457 CMD_PIPE_INDEX,
1458 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001459 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
sundarajan srinivasan7ced6482013-03-21 16:01:18 -07001460 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001461 num_cmd_desc++;
1462
1463 qpic_nand_wait_for_cmd_exec(num_cmd_desc);
1464
1465 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
1466
1467 /* Save the status registers. */
1468 flash_sts[i] = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr++);
1469 buffer_sts[i] = qpic_nand_read_reg(NAND_BUFFER_STATUS, 0, cmd_list_ptr++);
1470
Deepa Dinamani19530062012-10-03 14:43:05 -07001471 flash_sts[i] = qpic_nand_check_status(flash_sts[i]);
1472
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001473 buffer += DATA_BYTES_IN_IMG_PER_CW;
1474 }
1475
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001476 /* Read the buffer status again so that we can unlock the bam with this desc. */
1477 buffer_sts[--i] = qpic_nand_read_reg(NAND_BUFFER_STATUS, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr++);
Deepa Dinamani4b718ec2012-09-20 11:24:47 -07001478
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001479 /* Check status */
1480 for (i = 0; i < flash.cws_per_page ; i ++)
Deepa Dinamani19530062012-10-03 14:43:05 -07001481 if (flash_sts[i])
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001482 {
1483 nand_ret = NANDC_RESULT_BAD_PAGE;
1484 dprintf(CRITICAL, "NAND page read failed. page: %x\n", page);
1485 goto qpic_nand_read_page_error;
1486 }
1487
1488qpic_nand_read_page_error:
1489return nand_ret;
1490}
1491
1492/* Function to read a flash partition.
1493 * ptn : Partition to read.
1494 * extra_per_page : Spare data to be read.
1495 * offset : Num of bytes offset into the partition.
1496 * data : Buffer to read the data into.
1497 * bytes : Num of bytes to be read.
1498 */
1499 /* TODO: call this func read_partition. */
1500int
1501flash_read_ext(struct ptentry *ptn,
1502 unsigned extra_per_page,
1503 unsigned offset,
1504 void *data,
1505 unsigned bytes)
1506{
1507 uint32_t page =
1508 (ptn->start * flash.num_pages_per_blk) + (offset / flash.page_size);
1509 uint32_t lastpage = (ptn->start + ptn->length) * flash.num_pages_per_blk;
1510 uint32_t count =
1511 (bytes + flash.page_size - 1 + extra_per_page) / (flash.page_size +
1512 extra_per_page);
1513 uint32_t *spare = (unsigned *)flash_spare_bytes;
1514 uint32_t errors = 0;
1515 unsigned char *image = data;
1516 int result = 0;
1517 uint32_t current_block =
1518 (page - (page & flash.num_pages_per_blk_mask)) / flash.num_pages_per_blk;
1519 uint32_t start_block = ptn->start;
1520 uint32_t start_block_count = 0;
1521 uint32_t isbad = 0;
1522
1523 /* Verify first byte is at page boundary. */
1524 if (offset & (flash.page_size - 1))
1525 {
1526 dprintf(CRITICAL, "Read request start not at page boundary: %d\n",
1527 offset);
1528 return NANDC_RESULT_PARAM_INVALID;
1529 }
1530
1531 /* Adjust page offset based on number of bad blocks from start to current page */
1532 if (start_block < current_block)
1533 {
1534 start_block_count = (current_block - start_block);
1535 while (start_block_count
1536 && (start_block < (ptn->start + ptn->length)))
1537 {
Deepa Dinamanidc1381e2012-11-15 14:53:24 -08001538 isbad = qpic_nand_block_isbad(page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001539 if (isbad)
1540 page += flash.num_pages_per_blk;
1541 else
1542 start_block_count--;
1543 start_block++;
1544 }
1545 }
1546
1547 while ((page < lastpage) && !start_block_count)
1548 {
1549 if (count == 0)
1550 {
Deepa Dinamani52aca8d2013-02-05 11:41:41 -08001551 dprintf(SPEW, "flash_read_image: success (%d errors)\n",
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001552 errors);
1553 return NANDC_RESULT_SUCCESS;
1554 }
1555
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001556 result = qpic_nand_read_page(page, rdwr_buf, (unsigned char *)spare);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001557
1558 if (result == NANDC_RESULT_BAD_PAGE)
1559 {
1560 /* bad page, go to next page. */
1561 page++;
1562 errors++;
1563 continue;
1564 }
1565 else if (result == NANDC_RESULT_BAD_BLOCK)
1566 {
1567 /* bad block, go to next block same offset. */
1568 page += flash.num_pages_per_blk;
1569 errors++;
1570 continue;
1571 }
1572
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001573 /* Copy the read page into correct location. */
1574 memcpy(image, rdwr_buf, flash.page_size);
1575
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001576 page++;
1577 image += flash.page_size;
1578 /* Copy spare bytes to image */
1579 memcpy(image, spare, extra_per_page);
1580 image += extra_per_page;
1581 count -= 1;
1582 }
1583
1584 /* could not find enough valid pages before we hit the end */
1585 dprintf(CRITICAL, "flash_read_image: failed (%d errors)\n", errors);
1586 return NANDC_RESULT_FAILURE;
1587}
1588
1589int
1590flash_erase(struct ptentry *ptn)
1591{
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001592 int ret = 0;
1593
1594 ret = qpic_nand_blk_erase(ptn->start * flash.num_pages_per_blk);
1595
1596 if (ret)
1597 dprintf(CRITICAL, "Erase operation failed \n");
1598
1599 return ret;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001600}
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001601
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001602int
1603flash_ecc_bch_enabled()
1604{
1605 return (flash.ecc_width == NAND_WITH_4_BIT_ECC)? 0 : 1;
1606}
1607
1608int
1609flash_write(struct ptentry *ptn,
1610 unsigned extra_per_page,
1611 const void *data,
1612 unsigned bytes)
1613{
1614 uint32_t page = ptn->start * flash.num_pages_per_blk;
1615 uint32_t lastpage = (ptn->start + ptn->length) * flash.num_pages_per_blk;
1616 uint32_t *spare = (unsigned *)flash_spare_bytes;
1617 const unsigned char *image = data;
1618 uint32_t wsize = flash.page_size + extra_per_page;
1619 int r;
1620
1621 memset(spare, 0xff, (flash.spare_size / flash.cws_per_page));
1622
1623 while (bytes > 0)
1624 {
1625 if (bytes < wsize)
1626 {
1627 dprintf(CRITICAL,
1628 "flash_write_image: image undersized (%d < %d)\n",
1629 bytes,
1630 wsize);
1631 return -1;
1632 }
1633
1634 if (page >= lastpage)
1635 {
1636 dprintf(CRITICAL, "flash_write_image: out of space\n");
1637 return -1;
1638 }
1639
1640 if ((page & flash.num_pages_per_blk_mask) == 0)
1641 {
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001642 if (qpic_nand_blk_erase(page))
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001643 {
1644 dprintf(INFO,
1645 "flash_write_image: bad block @ %d\n",
1646 page / flash.num_pages_per_blk);
1647
1648 page += flash.num_pages_per_blk;
1649 continue;
1650 }
1651 }
1652
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001653 memcpy(rdwr_buf, image, flash.page_size);
1654
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001655 if (extra_per_page)
1656 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001657 memcpy(rdwr_buf + flash.page_size, image + flash.page_size, extra_per_page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001658 r = qpic_nand_write_page(page,
1659 NAND_CFG,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001660 rdwr_buf,
1661 rdwr_buf + flash.page_size);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001662 }
1663 else
1664 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001665 r = qpic_nand_write_page(page, NAND_CFG, rdwr_buf, spare);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001666 }
1667
1668 if (r)
1669 {
1670 dprintf(INFO,
1671 "flash_write_image: write failure @ page %d (src %d)\n",
1672 page,
1673 image - (const unsigned char *)data);
1674
1675 image -= (page & flash.num_pages_per_blk_mask) * wsize;
1676 bytes += (page & flash.num_pages_per_blk_mask) * wsize;
1677 page &= ~flash.num_pages_per_blk_mask;
1678 if (qpic_nand_blk_erase(page))
1679 {
1680 dprintf(INFO,
1681 "flash_write_image: erase failure @ page %d\n",
1682 page);
1683 }
1684
1685 qpic_nand_mark_badblock(page);
1686
1687 dprintf(INFO,
1688 "flash_write_image: restart write @ page %d (src %d)\n",
1689 page, image - (const unsigned char *)data);
1690
1691 page += flash.num_pages_per_blk;
1692 continue;
1693 }
1694 page++;
1695 image += wsize;
1696 bytes -= wsize;
1697 }
1698
1699 /* erase any remaining pages in the partition */
1700 page = (page + flash.num_pages_per_blk_mask) & (~flash.num_pages_per_blk_mask);
1701
1702 while (page < lastpage)
1703 {
1704 if (qpic_nand_blk_erase(page))
1705 {
1706 dprintf(INFO, "flash_write_image: bad block @ %d\n",
1707 page / flash.num_pages_per_blk);
1708 }
1709 page += flash.num_pages_per_blk;
1710 }
1711
1712 dprintf(INFO, "flash_write_image: success\n");
1713 return 0;
1714}