blob: 43bbc783f0d01c5dc392bfef0bc7af4855fbc232 [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},
Sundarajan Srinivasand69c0d32013-08-09 16:47:33 -070067 {0x1590ACAD, 0xFFFFFFFF, 0x20000000, 0, 2048, 0x00020000, 0x80, 0},
Deepa Dinamanie4573be2012-08-03 16:32:29 -070068 /* Note: Width flag is 0 for 8 bit Flash and 1 for 16 bit flash */
Deepa Dinamanie4573be2012-08-03 16:32:29 -070069};
70
Deepa Dinamanie4573be2012-08-03 16:32:29 -070071static void
72qpic_nand_wait_for_cmd_exec(uint32_t num_desc)
73{
74 /* Create a read/write event to notify the periperal of the added desc. */
75 bam_sys_gen_event(&bam, CMD_PIPE_INDEX, num_desc);
76
77 /* Wait for the descriptors to be processed */
78 bam_wait_for_interrupt(&bam, CMD_PIPE_INDEX, P_PRCSD_DESC_EN_MASK);
79
80 /* Read offset update for the circular FIFO */
81 bam_read_offset_update(&bam, CMD_PIPE_INDEX);
82}
83
84static void
85qpic_nand_wait_for_data(uint32_t pipe_num)
86{
87 /* Wait for the descriptors to be processed */
88 bam_wait_for_interrupt(&bam, pipe_num, P_PRCSD_DESC_EN_MASK);
89
90 /* Read offset update for the circular FIFO */
91 bam_read_offset_update(&bam, pipe_num);
92}
93
94static uint32_t
95qpic_nand_read_reg(uint32_t reg_addr,
96 uint8_t flags,
97 struct cmd_element *cmd_list_ptr)
98{
99 uint32_t val;
100
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700101 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 -0700102
103 /* Enqueue the desc for the above command */
104 bam_add_one_desc(&bam,
105 CMD_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700106 (unsigned char*)PA((addr_t)cmd_list_ptr),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700107 BAM_CE_SIZE,
108 BAM_DESC_CMD_FLAG| BAM_DESC_INT_FLAG | flags);
109
110 qpic_nand_wait_for_cmd_exec(1);
111
112 return val;
113}
114
Deepa Dinamani19530062012-10-03 14:43:05 -0700115/* Assume the BAM is in a locked state. */
116void
sundarajan srinivasan7ced6482013-03-21 16:01:18 -0700117qpic_nand_erased_status_reset(struct cmd_element *cmd_list_ptr, uint8_t flags)
Deepa Dinamani19530062012-10-03 14:43:05 -0700118{
119 uint32_t val = 0;
120
121 /* Reset the Erased Codeword/Page detection controller. */
122 val = NAND_ERASED_CW_DETECT_CFG_RESET_CTRL;
123
124 bam_add_cmd_element(cmd_list_ptr, NAND_ERASED_CW_DETECT_CFG, val, CE_WRITE_TYPE);
125
126 /* Enqueue the desc for the above command */
127 bam_add_one_desc(&bam,
128 CMD_PIPE_INDEX,
129 (unsigned char*)cmd_list_ptr,
130 BAM_CE_SIZE,
sundarajan srinivasan7ced6482013-03-21 16:01:18 -0700131 BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG | flags);
Deepa Dinamani19530062012-10-03 14:43:05 -0700132
133 qpic_nand_wait_for_cmd_exec(1);
134
135 /* Enable the Erased Codeword/Page detection
136 * controller to check the data as it arrives.
137 * Also disable ECC reporting for an erased CW.
138 */
139 val = NAND_ERASED_CW_DETECT_CFG_ACTIVATE_CTRL | NAND_ERASED_CW_DETECT_ERASED_CW_ECC_MASK;
140
141 bam_add_cmd_element(cmd_list_ptr, NAND_ERASED_CW_DETECT_CFG, val, CE_WRITE_TYPE);
142
143 /* Enqueue the desc for the above command */
144 bam_add_one_desc(&bam,
145 CMD_PIPE_INDEX,
146 (unsigned char*)cmd_list_ptr,
147 BAM_CE_SIZE,
148 BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG);
149
150 qpic_nand_wait_for_cmd_exec(1);
151}
152
153static nand_result_t
154qpic_nand_check_status(uint32_t status)
155{
156 uint32_t erase_sts;
157
158 /* Check for errors */
159 if (status & NAND_FLASH_ERR)
160 {
161 /* Check if this is an ECC error on an erased page. */
162 if (status & NAND_FLASH_OP_ERR)
163 {
164 erase_sts = qpic_nand_read_reg(NAND_ERASED_CW_DETECT_STATUS, 0, ce_array);
165 if ((erase_sts & (1 << NAND_ERASED_CW_DETECT_STATUS_PAGE_ALL_ERASED)))
166 {
167 /* Mask the OP ERROR. */
168 status &= ~NAND_FLASH_OP_ERR;
sundarajan srinivasan7ced6482013-03-21 16:01:18 -0700169 qpic_nand_erased_status_reset(ce_array, 0);
Deepa Dinamani19530062012-10-03 14:43:05 -0700170 }
171 }
172
173 /* ECC error flagged on an erased page read.
174 * Ignore and return success.
175 */
176 if (!(status & NAND_FLASH_ERR))
177 return NANDC_RESULT_SUCCESS;
178
179 dprintf(CRITICAL, "Nand Flash error. Status = %d\n", status);
180
181 if (status & NAND_FLASH_TIMEOUT_ERR)
182 return NANDC_RESULT_TIMEOUT;
183 else
184 return NANDC_RESULT_FAILURE;
185 }
186
187 return NANDC_RESULT_SUCCESS;
188}
189
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700190static uint32_t
191qpic_nand_fetch_id(struct flash_info *flash)
192{
193 struct cmd_element *cmd_list_ptr = ce_array;
194 struct cmd_element *cmd_list_ptr_start = ce_array;
195 int num_desc = 0;
196 uint32_t status;
197 uint32_t id;
198 uint32_t flash_cmd = NAND_CMD_FETCH_ID;
199 uint32_t exec_cmd = 1;
200 int nand_ret = NANDC_RESULT_SUCCESS;
201
202 /* Issue the Fetch id command to the NANDc */
203 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)flash_cmd, CE_WRITE_TYPE);
204 cmd_list_ptr++;
205
206 /* Execute the cmd */
207 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)exec_cmd, CE_WRITE_TYPE);
208 cmd_list_ptr++;
209
210 /* Prepare the cmd desc for the above commands */
211 bam_add_one_desc(&bam,
212 CMD_PIPE_INDEX,
213 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700214 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700215 BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG |
216 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG);
217
218 /* Keep track of the number of desc added. */
219 num_desc++;
220 qpic_nand_wait_for_cmd_exec(num_desc);
221
222 cmd_list_ptr_start = ce_array;
223 cmd_list_ptr = ce_array;
224
225 /* Read the status register */
226 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr);
227
228 /* Check for errors */
229 nand_ret = qpic_nand_check_status(status);
230 if (nand_ret)
231 {
232 dprintf( CRITICAL, "Read ID cmd status failed\n");
233 goto qpic_nand_fetch_id_err;
234 }
235
236 /* Read the id */
237 id = qpic_nand_read_reg(NAND_READ_ID, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr);
238
239 flash->id = id;
240 flash->vendor = id & 0xff;
241 flash->device = (id >> 8) & 0xff;
242 flash->dev_cfg = (id >> 24) & 0xFF;
243 flash->widebus = 0;
244 flash->widebus &= (id >> 24) & 0xFF;
245 flash->widebus = flash->widebus? 1: 0;
246
247qpic_nand_fetch_id_err:
248 return nand_ret;
249}
250
251static int
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800252qpic_bam_init(struct qpic_nand_init_config *config)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700253{
254 uint32_t bam_ret = NANDC_RESULT_SUCCESS;
255
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800256 bam.base = config->bam_base;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700257 /* Set Read pipe params. */
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800258 bam.pipe[DATA_PRODUCER_PIPE_INDEX].pipe_num = config->pipes.read_pipe;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700259 /* System consumer */
260 bam.pipe[DATA_PRODUCER_PIPE_INDEX].trans_type = BAM2SYS;
261 bam.pipe[DATA_PRODUCER_PIPE_INDEX].fifo.size = QPIC_BAM_DATA_FIFO_SIZE;
262 bam.pipe[DATA_PRODUCER_PIPE_INDEX].fifo.head = data_desc_fifo;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700263 bam.pipe[DATA_PRODUCER_PIPE_INDEX].lock_grp = config->pipes.read_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700264
265 /* Set Write pipe params. */
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800266 bam.pipe[DATA_CONSUMER_PIPE_INDEX].pipe_num = config->pipes.write_pipe;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700267 /* System producer */
268 bam.pipe[DATA_CONSUMER_PIPE_INDEX].trans_type = SYS2BAM;
269 bam.pipe[DATA_CONSUMER_PIPE_INDEX].fifo.size = QPIC_BAM_DATA_FIFO_SIZE;
270 bam.pipe[DATA_CONSUMER_PIPE_INDEX].fifo.head = data_desc_fifo;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700271 bam.pipe[DATA_CONSUMER_PIPE_INDEX].lock_grp = config->pipes.write_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700272
273 /* Set Cmd pipe params. */
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800274 bam.pipe[CMD_PIPE_INDEX].pipe_num = config->pipes.cmd_pipe;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700275 /* System consumer */
276 bam.pipe[CMD_PIPE_INDEX].trans_type = BAM2SYS;
277 bam.pipe[CMD_PIPE_INDEX].fifo.size = QPIC_BAM_CMD_FIFO_SIZE;
278 bam.pipe[CMD_PIPE_INDEX].fifo.head = cmd_desc_fifo;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700279 bam.pipe[CMD_PIPE_INDEX].lock_grp = config->pipes.cmd_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700280
281 /* Programs the threshold for BAM transfer
282 * When this threshold is reached, BAM signals the peripheral via the pipe_bytes_available
283 * interface.
284 * The peripheral is signalled with this notification in the following cases:
285 * a. It has accumulated all the descriptors.
286 * b. It has accumulated more than threshold bytes.
287 * c. It has reached EOT (End Of Transfer).
288 * Note: this value needs to be set by the h/w folks and is specific for each peripheral.
289 */
290 bam.threshold = 32;
291
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800292 /* Set the EE. */
293 bam.ee = config->ee;
294
295 /* Set the max desc length for this BAM. */
296 bam.max_desc_len = config->max_desc_len;
297
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700298 /* BAM Init. */
299 bam_init(&bam);
300
301 /* Initialize BAM QPIC read pipe */
302 bam_sys_pipe_init(&bam, DATA_PRODUCER_PIPE_INDEX);
303
304 /* Init read fifo */
305 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[DATA_PRODUCER_PIPE_INDEX].pipe_num);
306
307 if (bam_ret)
308 {
309 dprintf(CRITICAL, "QPIC:NANDc BAM Read FIFO init error\n");
310 bam_ret = NANDC_RESULT_FAILURE;
311 goto qpic_nand_bam_init_error;
312 }
313
314 /* Initialize BAM QPIC write pipe */
315 bam_sys_pipe_init(&bam, DATA_CONSUMER_PIPE_INDEX);
316
317 /* Init write fifo. Use the same fifo as read fifo. */
318 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[DATA_CONSUMER_PIPE_INDEX].pipe_num);
319
320 if (bam_ret)
321 {
322 dprintf(CRITICAL, "QPIC: NANDc: BAM Write FIFO init error\n");
323 bam_ret = NANDC_RESULT_FAILURE;
324 goto qpic_nand_bam_init_error;
325 }
326
327 /* Initialize BAM QPIC cmd pipe */
328 bam_sys_pipe_init(&bam, CMD_PIPE_INDEX);
329
330 /* Init cmd fifo */
331 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[CMD_PIPE_INDEX].pipe_num);
332
333 if (bam_ret)
334 {
335 dprintf(CRITICAL, "QPIC:NANDc BAM CMD FIFO init error\n");
336 bam_ret = NANDC_RESULT_FAILURE;
337 goto qpic_nand_bam_init_error;
338 }
339
340qpic_nand_bam_init_error:
341return bam_ret;
342}
343
344/* Adds command elements for addr and cfg register writes.
345 * cfg: Defines the configuration for the flash cmd.
346 * start: Address where the command elements are added.
347 *
348 * Returns the address where the next cmd element can be added.
349 */
350static struct cmd_element*
351qpic_nand_add_addr_n_cfg_ce(struct cfg_params *cfg,
352 struct cmd_element *start)
353{
354 struct cmd_element *cmd_list_ptr = start;
355
356 bam_add_cmd_element(cmd_list_ptr, NAND_ADDR0, (uint32_t)cfg->addr0, CE_WRITE_TYPE);
357 cmd_list_ptr++;
358 bam_add_cmd_element(cmd_list_ptr, NAND_ADDR1, (uint32_t)cfg->addr1, CE_WRITE_TYPE);
359 cmd_list_ptr++;
360 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_CFG0, (uint32_t)cfg->cfg0, CE_WRITE_TYPE);
361 cmd_list_ptr++;
362 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_CFG1, (uint32_t)cfg->cfg1, CE_WRITE_TYPE);
363 cmd_list_ptr++;
364
365 return cmd_list_ptr;
366}
367
368
369static struct cmd_element*
370qpic_nand_add_onfi_probe_ce(struct onfi_probe_params *params,
371 struct cmd_element *start)
372{
373 struct cmd_element *cmd_list_ptr = start;
374
375 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(&params->cfg, cmd_list_ptr);
376
377 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD1, (uint32_t)params->dev_cmd1, CE_WRITE_TYPE);
378 cmd_list_ptr++;
379 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD_VLD, (uint32_t)params->vld, CE_WRITE_TYPE);
380 cmd_list_ptr++;
381 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(0), (uint32_t)params->cfg.addr_loc_0, CE_WRITE_TYPE);
382 cmd_list_ptr++;
383 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)params->cfg.cmd, CE_WRITE_TYPE);
384 cmd_list_ptr++;
385 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)params->cfg.exec, CE_WRITE_TYPE);
386 cmd_list_ptr++;
387
388 return cmd_list_ptr;
389}
390
391static int
392onfi_probe_cmd_exec(struct onfi_probe_params *params,
393 unsigned char* data_ptr,
394 int data_len)
395{
396 struct cmd_element *cmd_list_ptr = ce_array;
397 struct cmd_element *cmd_list_ptr_start = ce_array;
398 int num_desc = 0;
399 uint32_t status = 0;
400 int nand_ret = NANDC_RESULT_SUCCESS;
401 uint8_t desc_flags = BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG
402 | BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG;
403
404 params->cfg.addr_loc_0 = 0;
405 params->cfg.addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
406 params->cfg.addr_loc_0 |= NAND_RD_LOC_OFFSET(0);
407 params->cfg.addr_loc_0 |= NAND_RD_LOC_SIZE(data_len);
408
409 cmd_list_ptr = qpic_nand_add_onfi_probe_ce(params, cmd_list_ptr);
410
411 /* Enqueue the desc for the above commands */
412 bam_add_one_desc(&bam,
413 CMD_PIPE_INDEX,
414 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700415 PA((addr_t)(uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700416 desc_flags);
417
418 cmd_list_ptr_start = cmd_list_ptr;
419 num_desc++;
420
421 /* Add Data desc */
422 bam_add_desc(&bam,
423 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700424 (unsigned char *)PA((addr_t)data_ptr),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700425 data_len,
426 BAM_DESC_INT_FLAG);
427
428 /* Wait for the commands to be executed */
429 qpic_nand_wait_for_cmd_exec(num_desc);
430
431 /* Read buffer status and check for errors. */
432 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr++);
433
434 if (qpic_nand_check_status(status))
435 {
436 nand_ret = NANDC_RESULT_FAILURE;
437 goto onfi_probe_exec_err;
438 }
439
440 /* Wait for data to be available */
441 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
442
443 /* Check for errors */
444 nand_ret = qpic_nand_check_status(status);
445
446onfi_probe_exec_err:
447 return nand_ret;
448}
449
450/* TODO: check why both vld and cmd need to be written. */
451void
452qpic_nand_onfi_probe_cleanup(uint32_t vld, uint32_t dev_cmd1)
453{
454 struct cmd_element *cmd_list_ptr = ce_array;
455 struct cmd_element *cmd_list_ptr_start = ce_array;
456
457 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD1, dev_cmd1, CE_WRITE_TYPE);
458 cmd_list_ptr++;
459 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD_VLD, vld, CE_WRITE_TYPE);
460 cmd_list_ptr++;
461
462 /* Enqueue the desc for the above commands */
463 bam_add_one_desc(&bam,
464 CMD_PIPE_INDEX,
465 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700466 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700467 BAM_DESC_UNLOCK_FLAG | BAM_DESC_CMD_FLAG| BAM_DESC_INT_FLAG);
468
469 qpic_nand_wait_for_cmd_exec(1);
470}
471
472static int
473qpic_nand_onfi_save_params(struct onfi_param_page *param_page, struct flash_info *flash)
474{
475 int onfi_ret = NANDC_RESULT_SUCCESS;
476 uint32_t ecc_bits;
477
478 onfi_ret = qpic_nand_fetch_id(flash);
479
480 if (onfi_ret)
481 {
482 dprintf(CRITICAL, "Fetch ID cmd failed\n");
483 goto onfi_save_params_err;
484 }
485
486 flash->page_size = param_page->data_per_pg;
487 flash->block_size = param_page->pgs_per_blk * flash->page_size;
488 flash->num_blocks = param_page->blks_per_LUN;
489 flash->widebus = param_page->feature_supported & 0x1;
490 flash->density = param_page->blks_per_LUN * flash->blksize;
491 flash->spare_size = param_page->spare_per_pg;
492 ecc_bits = param_page->num_bits_ecc_correctability;
493 flash->num_pages_per_blk = param_page->pgs_per_blk;
494 flash->num_pages_per_blk_mask = param_page->pgs_per_blk - 1;
495
496 if (ecc_bits >= 8)
497 flash->ecc_width = NAND_WITH_8_BIT_ECC;
498 else
499 flash->ecc_width = NAND_WITH_4_BIT_ECC;
500
501 onfi_save_params_err:
502 return onfi_ret;
503}
504
505static void
506qpic_nand_save_config(struct flash_info *flash)
507{
508
509 /* Save Configurations */
510 flash->cws_per_page = flash->page_size >> NAND_CW_DIV_RIGHT_SHIFT;
511
Deepa Dinamani16663a62013-02-07 16:25:59 -0800512 /* Verify that we have enough buffer to handle all the cws in a page. */
513 ASSERT(flash->cws_per_page <= QPIC_NAND_MAX_CWS_IN_PAGE);
514
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700515 /* Codeword Size = UD_SIZE_BYTES + ECC_PARITY_SIZE_BYTES
516 * + SPARE_SIZE_BYTES + Bad Block size
517 */
518 if (flash->ecc_width & NAND_WITH_8_BIT_ECC)
519 {
520 flash->cw_size = NAND_CW_SIZE_8_BIT_ECC;
521 ecc_bch_cfg |= (1 << NAND_DEV0_ECC_MODE_SHIFT); /* Use 8-bit ecc */
522
523 if (flash->widebus)
524 {
525 cfg0 |= (0 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
526 ecc_bch_cfg |= (14 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
527 }
528 else
529 {
530 cfg0 |= (2 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
531 ecc_bch_cfg |= (13 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
532 }
533 }
534 else
535 {
536 flash->cw_size = NAND_CW_SIZE_4_BIT_ECC;
537
538 if (flash->widebus)
539 {
540 cfg0 |= (2 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
541 ecc_bch_cfg |= (8 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
542 }
543 else
544 {
545 cfg0 |= (4 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
546 ecc_bch_cfg |= (7 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
547 }
548 }
549
550 /* BAD_BLOCK_BYTE_NUM = Page Size -
551 * (CW_PER_PAGE * Codeword Size) + 1
552 * Note: Set CW_PER_PAGE to 1 less than the actual number.
553 */
554 flash->bad_blk_loc = flash->page_size - flash->cw_size * (flash->cws_per_page - 1) + 1;
555
556 cfg0 |= ((flash->cws_per_page - 1) << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT) /* 4/8 cw/pg for 2/4k */
557 |(DATA_BYTES_IN_IMG_PER_CW << NAND_DEV0_CFG0_UD_SIZE_BYTES_SHIFT) /* 516 user data bytes */
558 |(5 << NAND_DEV0_CFG0_ADDR_CYCLE_SHIFT) /* 5 address cycles */
559 |(0 << NAND_DEV0_CFG0_DIS_STS_AFTER_WR_SHIFT);/* Send read status cmd after each write. */
560
561 cfg1 |= (7 << NAND_DEV0_CFG1_RECOVERY_CYCLES_SHIFT) /* 8 recovery cycles */
562 |(0 << NAND_DEV0_CFG1_CS_ACTIVE_BSY_SHIFT) /* Allow CS deassertion */
563 |(flash->bad_blk_loc << NAND_DEV0_CFG1_BAD_BLK_BYTE_NUM_SHIFT)/* Bad block marker location */
564 |(0 << NAND_DEV0_CFG1_BAD_BLK_IN_SPARE_SHIFT) /* Bad block in user data area */
565 |(2 << NAND_DEV0_CFG1_WR_RD_BSY_GAP_SHIFT) /* 8 cycle tWB/tRB */
566 |(flash->widebus << NAND_DEV0_CFG1_WIDE_BUS_SHIFT); /* preserve wide flash flag */
567
568 cfg0_raw = ((flash->cws_per_page- 1) << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT)
569 |(5 << NAND_DEV0_CFG0_ADDR_CYCLE_SHIFT)
570 |(516 << NAND_DEV0_CFG0_UD_SIZE_BYTES_SHIFT) //figure out the size of cw
571 | (1 << NAND_DEV0_CFG0_DIS_STS_AFTER_WR_SHIFT);
572
573 cfg1_raw = (7 << NAND_DEV0_CFG1_RECOVERY_CYCLES_SHIFT)
574 | (0 << NAND_DEV0_CFG1_CS_ACTIVE_BSY_SHIFT)
575 | (17 << NAND_DEV0_CFG1_BAD_BLK_BYTE_NUM_SHIFT)
576 | (1 << NAND_DEV0_CFG1_BAD_BLK_IN_SPARE_SHIFT)
577 | (2 << NAND_DEV0_CFG1_WR_RD_BSY_GAP_SHIFT)
578 | (flash->widebus << NAND_DEV0_CFG1_WIDE_BUS_SHIFT)
579 |1 ; /* to disable reed solomon ecc..this feild is now read only. */
580
581 ecc_bch_cfg |= (0 << NAND_DEV0_ECC_DISABLE_SHIFT) /* Enable ECC */
582 | (0 << NAND_DEV0_ECC_SW_RESET_SHIFT) /* Put ECC core in op mode */
583 | (DATA_BYTES_IN_IMG_PER_CW << NAND_DEV0_ECC_NUM_DATA_BYTES)
584 | (1 << NAND_DEV0_ECC_FORCE_CLK_OPEN_SHIFT); /* Enable all clocks */
585}
586
587/* Onfi probe should issue the following commands to the flash device:
588 * 1. Read ID - with addr ONFI_READ_ID_ADDR.
589 * This returns the ONFI ASCII string indicating support for ONFI.
590 * 2. Read Prameter Page - with addr ONFI_READ_PARAM_PAGE_ADDR.
591 * This returns the params for the device.
592 * Each command inturn issues commands- ADDR0, ADDR1, chip_select,
593 * cfg0, cfg1, cmd_vld, dev_cmd1, read_loc0, flash, exec.
594 */
595static int
596qpic_nand_onfi_probe(struct flash_info *flash)
597{
598 struct onfi_probe_params params;
599 uint32_t vld;
600 uint32_t dev_cmd1;
601 unsigned char *buffer;
602 unsigned char onfi_str[4];
603 uint32_t *id;
604 struct onfi_param_page *param_page;
605 int onfi_ret = NANDC_RESULT_SUCCESS;
606
607 /* Allocate memory required to read the onfi param page */
608 buffer = (unsigned char*) malloc(ONFI_READ_PARAM_PAGE_BUFFER_SIZE);
609
610 /* Read the vld and dev_cmd1 registers before modifying */
611 vld = qpic_nand_read_reg(NAND_DEV_CMD_VLD, 0, ce_array);
612 dev_cmd1 = qpic_nand_read_reg(NAND_DEV_CMD1, 0, ce_array);
613
614 /* Initialize flash cmd */
615 params.cfg.cmd = NAND_CMD_PAGE_READ;
616 params.cfg.exec = 1;
617
618 /* Execute Read ID cmd */
619
620 /* Initialize the config */
621 params.cfg.cfg0 = NAND_CFG0_RAW_ONFI_ID;
622 params.cfg.cfg1 = NAND_CFG1_RAW_ONFI_ID;
623
624 /* Initialize the cmd and vld */
625 params.dev_cmd1 = (dev_cmd1 & 0xFFFFFF00) | ONFI_READ_ID_CMD;
626 params.vld = vld & 0xFFFFFFFE;
627
628 /* Initialize the address
629 * addr1 is not used bcos of the cfg.
630 */
631 params.cfg.addr0 = ONFI_READ_ID_ADDR;
632 params.cfg.addr1 = 0;
633
634 /* Lock the pipe and execute the cmd. */
635 onfi_ret = onfi_probe_cmd_exec(&params, onfi_str, ONFI_READ_ID_BUFFER_SIZE);
636 if (onfi_ret)
637 {
638 dprintf(CRITICAL, "ONFI Read id cmd failed\n");
639 goto qpic_nand_onfi_probe_err;
640 }
641
642 /* Write back vld and cmd and unlock the pipe. */
643 qpic_nand_onfi_probe_cleanup(vld, dev_cmd1);
644
645 /* Check for onfi string */
646 id = (uint32_t*)onfi_str;
647 if (*id != ONFI_SIGNATURE)
648 {
649 dprintf(CRITICAL, "Not an ONFI device\n");
650 /* Not an onfi device. Return error. */
651 onfi_ret = NANDC_RESULT_DEV_NOT_SUPPORTED;
652 goto qpic_nand_onfi_probe_err;
653 }
654
655 dprintf(INFO, "ONFI device found\n");
656 /* Now read the param page */
657 /* Initialize the config */
658 params.cfg.cfg0 = NAND_CFG0_RAW_ONFI_PARAM_PAGE;
659 params.cfg.cfg1 = NAND_CFG1_RAW_ONFI_PARAM_PAGE;
660
661 /* Initialize the cmd and vld */
662 params.dev_cmd1 = (dev_cmd1 & 0xFFFFFF00) | ONFI_READ_PARAM_PAGE_CMD;
663 params.vld = vld & 0xFFFFFFFE;
664
665 /* Initialize the address
666 * addr1 is not used bcos of the cfg.
667 */
668 params.cfg.addr0 = ONFI_READ_PARAM_PAGE_ADDR;
669 params.cfg.addr1 = 0;
670
671 /* Lock the pipe and execute the cmd. */
672 onfi_ret = onfi_probe_cmd_exec(&params, buffer, ONFI_READ_PARAM_PAGE_BUFFER_SIZE);
673 if (onfi_ret)
674 {
675 dprintf(CRITICAL, "ONFI Read param page failed\n");
676 goto qpic_nand_onfi_probe_err;
677 }
678
679 /* Write back vld and cmd and unlock the pipe. */
680 qpic_nand_onfi_probe_cleanup(vld, dev_cmd1);
681
682 /* Verify the integrity of the returned page */
683 param_page = (struct onfi_param_page*)buffer;
684
685 /* TODO: Add CRC check to validate the param page. */
686
687 /* Save the parameter values */
688 onfi_ret = qpic_nand_onfi_save_params(param_page, flash);
689
690qpic_nand_onfi_probe_err:
691 if (onfi_ret)
692 dprintf(CRITICAL, "ONFI probe failed\n");
693
694 free(buffer);
695
696 return onfi_ret;
697}
698
699/* Enquues a desc for a flash cmd with NWD flag set:
700 * cfg: Defines the configuration for the flash cmd.
701 * start: Address where the command elements are added.
702 *
703 * Returns the address where the next cmd element can be added.
704 */
705struct cmd_element*
706qpic_nand_add_cmd_ce(struct cfg_params *cfg,
707 struct cmd_element *start)
708{
709 struct cmd_element *cmd_list_ptr;
710
711 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(cfg, start);
712
713 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)cfg->cmd, CE_WRITE_TYPE);
714 cmd_list_ptr++;
715
716 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)cfg->exec, CE_WRITE_TYPE);
717 cmd_list_ptr++;
718
719 return cmd_list_ptr;
720}
721
722/* Reads nand_flash_status and resets nand_flash_status and nand_read_status */
723struct cmd_element*
724qpic_nand_add_read_n_reset_status_ce(struct cmd_element *start,
725 uint32_t *flash_status_read,
726 uint32_t read_status)
727{
728 struct cmd_element *cmd_list_ptr = start;
729 uint32_t flash_status_reset;
730 uint32_t read_status_reset;
731
732 /* Read and reset the status registers. */
733 flash_status_reset = NAND_FLASH_STATUS_RESET;
734 read_status_reset = NAND_READ_STATUS_RESET;
735
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700736 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 -0700737 cmd_list_ptr++;
738 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_STATUS, (uint32_t)flash_status_reset, CE_WRITE_TYPE);
739 cmd_list_ptr++;
740
741 if (read_status)
742 {
743 bam_add_cmd_element(cmd_list_ptr, NAND_READ_STATUS, (uint32_t)read_status_reset, CE_WRITE_TYPE);
744 cmd_list_ptr++;
745 }
746
747 return cmd_list_ptr;
748}
749
750struct cmd_element*
751qpic_nand_add_isbad_cmd_ce(struct cfg_params *cfg,
752 struct cmd_element *start)
753{
754 struct cmd_element *cmd_list_ptr = start;
755
756 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG, (uint32_t)cfg->ecc_cfg, CE_WRITE_TYPE);
757 cmd_list_ptr++;
758
759 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(0), (uint32_t)cfg->addr_loc_0, CE_WRITE_TYPE);
760 cmd_list_ptr++;
761
762 cmd_list_ptr = qpic_nand_add_cmd_ce(cfg, cmd_list_ptr);
763
764 return cmd_list_ptr;
765}
766
767static int
768qpic_nand_block_isbad_exec(struct cfg_params *params,
769 uint8_t *bad_block)
770{
771
772 struct cmd_element *cmd_list_ptr = ce_array;
773 struct cmd_element *cmd_list_ptr_start = ce_array;
774 uint8_t desc_flags = BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG
775 | BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG;
776 int num_desc = 0;
777 uint32_t status = 0;
778 int nand_ret = NANDC_RESULT_SUCCESS;
779
780 cmd_list_ptr = qpic_nand_add_isbad_cmd_ce(params, cmd_list_ptr);
781
782 /* Enqueue the desc for the above commands */
783 bam_add_one_desc(&bam,
784 CMD_PIPE_INDEX,
785 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700786 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700787 desc_flags);
788
789 num_desc++;
790
791 /* Add Data desc */
792 bam_add_desc(&bam,
793 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700794 (unsigned char *)PA((addr_t)bad_block),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700795 4,
796 BAM_DESC_INT_FLAG);
797
798 qpic_nand_wait_for_cmd_exec(num_desc);
799
Deepa Dinamani19530062012-10-03 14:43:05 -0700800 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr);
801
802 nand_ret = qpic_nand_check_status(status);
803
804 /* Dummy read to unlock pipe. */
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700805 status = qpic_nand_read_reg(NAND_FLASH_STATUS, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr);
806
Deepa Dinamani19530062012-10-03 14:43:05 -0700807 if (nand_ret)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700808 return NANDC_RESULT_FAILURE;
809
810 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
811
812 return nand_ret;
813}
814
815static int
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800816qpic_nand_block_isbad(unsigned page)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700817{
818 unsigned cwperpage;
819 struct cfg_params params;
820 uint8_t bad_block[4];
821 unsigned nand_ret = NANDC_RESULT_SUCCESS;
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800822 uint32_t blk = page / flash.num_pages_per_blk;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700823
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800824 if (bbtbl[blk] == NAND_BAD_BLK_VALUE_IS_GOOD)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700825 return NANDC_RESULT_SUCCESS;
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800826 else if (bbtbl[blk] == NAND_BAD_BLK_VALUE_IS_BAD)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700827 return NANDC_RESULT_BAD_BLOCK;
828 else
829 {
830 /* Read the bad block value from the flash.
831 * Bad block value is stored in the first page of the block.
832 */
833 /* Read the first page in the block. */
834 cwperpage = flash.cws_per_page;
835
836 /* Read page cmd */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800837 params.cmd = NAND_CMD_PAGE_READ_ECC;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700838 /* Clear the CW per page bits */
839 params.cfg0 = cfg0_raw & ~(7U << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT);
840 params.cfg1 = cfg1_raw;
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800841 /* addr0 - Write column addr + few bits in row addr upto 32 bits. */
842 params.addr0 = (page << 16) | (USER_DATA_BYTES_PER_CW * cwperpage);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700843
844 /* addr1 - Write rest of row addr.
845 * This will be all 0s.
846 */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800847 params.addr1 = (page >> 16) & 0xff;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700848 params.addr_loc_0 = NAND_RD_LOC_OFFSET(0);
849 params.addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
850 params.addr_loc_0 |= NAND_RD_LOC_SIZE(4); /* Read 4 bytes */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800851 params.ecc_cfg = ecc_bch_cfg | 0x1; /* Disable ECC */
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700852 params.exec = 1;
853
854 if (qpic_nand_block_isbad_exec(&params, bad_block))
855 {
856 dprintf(CRITICAL,
857 "Could not read bad block value\n");
858 return NANDC_RESULT_FAILURE;
859 }
860
861 if (flash.widebus)
862 {
863 if (bad_block[0] != 0xFF && bad_block[1] != 0xFF)
864 {
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800865 bbtbl[blk] = NAND_BAD_BLK_VALUE_IS_BAD;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700866 nand_ret = NANDC_RESULT_BAD_BLOCK;
867 }
868 }
869 else if (bad_block[0] != 0xFF)
870 {
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800871 bbtbl[blk] = NAND_BAD_BLK_VALUE_IS_BAD;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700872 nand_ret = NANDC_RESULT_BAD_BLOCK;
873 }
874 else
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800875 bbtbl[blk] = NAND_BAD_BLK_VALUE_IS_GOOD;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700876
877 return nand_ret;
878 }
879}
880
881/* Function to erase a block on the nand.
882 * page: Starting page address for the block.
883 */
884static int
885qpic_nand_blk_erase(uint32_t page)
886{
887 struct cfg_params cfg;
888 struct cmd_element *cmd_list_ptr = ce_array;
889 struct cmd_element *cmd_list_ptr_start = ce_array;
890 uint32_t status;
891 int num_desc = 0;
892 uint32_t blk_addr = page / flash.num_pages_per_blk;
Deepa Dinamani19530062012-10-03 14:43:05 -0700893 int nand_ret;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700894
895 /* Erase only if the block is not bad */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800896 if (qpic_nand_block_isbad(page))
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700897 {
898 dprintf(CRITICAL,
899 "NAND Erase error: Block address belongs to bad block: %d\n",
900 blk_addr);
901 return NANDC_RESULT_FAILURE;
902 }
903
904 /* Fill in params for the erase flash cmd */
905 cfg.addr0 = page;
906 cfg.addr1 = 0;
907 /* Clear CW_PER_PAGE in cfg0 */
908 cfg.cfg0 = cfg0 & ~(7U << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT);
909 cfg.cfg1 = cfg1;
910 cfg.cmd = NAND_CMD_BLOCK_ERASE;
911 cfg.exec = 1;
912
913 cmd_list_ptr = qpic_nand_add_cmd_ce(&cfg, cmd_list_ptr);
914
915 /* Enqueue the desc for the above commands */
916 bam_add_one_desc(&bam,
917 CMD_PIPE_INDEX,
918 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700919 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamani2467bbb2012-10-02 13:59:58 -0700920 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG | BAM_DESC_LOCK_FLAG);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700921
922 cmd_list_ptr_start = cmd_list_ptr;
923 num_desc++;
924
925 qpic_nand_wait_for_cmd_exec(num_desc);
926
927 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr);
928
929 cmd_list_ptr++;
930 cmd_list_ptr_start = cmd_list_ptr;
931
932 /* QPIC controller automatically sends
933 * GET_STATUS cmd to the nand card because
934 * of the configuration programmed.
935 * Read the result of GET_STATUS cmd.
936 */
937 cmd_list_ptr = qpic_nand_add_read_n_reset_status_ce(cmd_list_ptr, &status, 1);
938
939 /* Enqueue the desc for the above commands */
940 bam_add_one_desc(&bam,
941 CMD_PIPE_INDEX,
942 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700943 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamani19530062012-10-03 14:43:05 -0700944 BAM_DESC_INT_FLAG | BAM_DESC_CMD_FLAG) ;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700945
946 num_desc = 1;
947 qpic_nand_wait_for_cmd_exec(num_desc);
948
Deepa Dinamani19530062012-10-03 14:43:05 -0700949 status = qpic_nand_check_status(status);
950
951 /* Dummy read to unlock pipe. */
952 nand_ret = qpic_nand_read_reg(NAND_FLASH_STATUS, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr);
953
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700954 /* Check for status errors*/
Deepa Dinamani19530062012-10-03 14:43:05 -0700955 if (status)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700956 {
957 dprintf(CRITICAL,
958 "NAND Erase error: Block address belongs to bad block: %d\n",
959 blk_addr);
960 return NANDC_RESULT_FAILURE;
961 }
962
963 /* Check for PROG_ERASE_OP_RESULT bit for the result of erase operation. */
Deepa Dinamani2467bbb2012-10-02 13:59:58 -0700964 if (!(status & PROG_ERASE_OP_RESULT))
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700965 return NANDC_RESULT_SUCCESS;
966
967 return NANDC_RESULT_FAILURE;
968}
969
970/* Return num of desc added. */
Deepa Dinamani19530062012-10-03 14:43:05 -0700971static void
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700972qpic_nand_add_wr_page_cws_cmd_desc(struct cfg_params *cfg,
973 uint32_t status[],
974 enum nand_cfg_value cfg_mode)
975{
976 struct cmd_element *cmd_list_ptr = ce_array;
977 struct cmd_element *cmd_list_ptr_start = ce_array;
978 uint32_t ecc;
979 int num_desc = 0;
980 int int_flag = 0;
981
982 if (cfg_mode == NAND_CFG)
983 ecc = ecc_bch_cfg;
984 else
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800985 ecc = ecc_bch_cfg | 0x1; /* Disable ECC */
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700986
987 /* Add ECC configuration */
988 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG,
989 (uint32_t)ecc, CE_WRITE_TYPE);
990 cmd_list_ptr++;
991 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(cfg, cmd_list_ptr);
992
993 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD,
994 (uint32_t)cfg->cmd, CE_WRITE_TYPE);
995 cmd_list_ptr++;
996
997 /* Enqueue the desc for the above commands */
998 bam_add_one_desc(&bam,
999 CMD_PIPE_INDEX,
1000 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001001 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001002 BAM_DESC_CMD_FLAG | BAM_DESC_LOCK_FLAG);
1003
1004 num_desc++;
1005
1006 /* Add CE for all the CWs */
1007 for (unsigned i = 0; i < flash.cws_per_page; i++)
1008 {
1009 cmd_list_ptr_start = cmd_list_ptr;
Deepa Dinamani19530062012-10-03 14:43:05 -07001010 int_flag = BAM_DESC_INT_FLAG;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001011
1012 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)cfg->exec, CE_WRITE_TYPE);
1013 cmd_list_ptr++;
1014
1015 /* Enqueue the desc for the above commands */
1016 bam_add_one_desc(&bam,
1017 CMD_PIPE_INDEX,
1018 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001019 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001020 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG);
1021
1022 num_desc++;
1023 cmd_list_ptr_start = cmd_list_ptr;
1024
1025 /* Set interrupt bit only for the last CW */
1026 if (i == flash.cws_per_page - 1)
1027 {
1028 cmd_list_ptr = qpic_nand_add_read_n_reset_status_ce(cmd_list_ptr,
1029 &status[i],
1030 1);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001031 }
1032 else
1033 cmd_list_ptr = qpic_nand_add_read_n_reset_status_ce(cmd_list_ptr,
1034 &status[i],
1035 0);
1036
1037 /* Enqueue the desc for the above commands */
1038 bam_add_one_desc(&bam,
1039 CMD_PIPE_INDEX,
1040 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001041 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001042 int_flag | BAM_DESC_CMD_FLAG);
1043 num_desc++;
Deepa Dinamani19530062012-10-03 14:43:05 -07001044
1045 qpic_nand_wait_for_cmd_exec(num_desc);
1046
1047 status[i] = qpic_nand_check_status(status[i]);
1048
1049 num_desc = 0;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001050 }
Deepa Dinamani19530062012-10-03 14:43:05 -07001051 return;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001052}
1053
1054void
1055qpic_add_wr_page_cws_data_desc(const void *buffer,
1056 enum nand_cfg_value cfg_mode,
1057 const void *spareaddr)
1058{
1059 int len;
1060 int flags;
1061 uint32_t start;
1062 unsigned num_desc = 0;
1063
1064 for( unsigned i = 0; i < flash.cws_per_page; i++)
1065 {
1066 flags = 0;
1067
1068 /* Set the interrupt flag on the last CW write for the page. */
1069 if( i == flash.cws_per_page - 1)
1070 flags |= BAM_DESC_INT_FLAG;
1071
1072 if (cfg_mode != NAND_CFG_RAW)
1073 {
1074 start = (uint32_t)buffer + i * DATA_BYTES_IN_IMG_PER_CW;
1075
1076 if (i < (flash.cws_per_page - 1))
1077 {
1078 len = DATA_BYTES_IN_IMG_PER_CW;
1079 flags |= BAM_DESC_EOT_FLAG;
1080 }
1081 else
1082 {
1083 /* Allow space for spare bytes in the last page */
1084 len = USER_DATA_BYTES_PER_CW - ((flash.cws_per_page - 1) << 2);
1085 flags = 0;
1086 }
1087 }
1088 else
1089 {
1090 start = (uint32_t)buffer;
1091 len = flash.cw_size;
1092 flags |= BAM_DESC_EOT_FLAG;
1093 }
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001094 bam_add_one_desc(&bam, DATA_CONSUMER_PIPE_INDEX, (unsigned char*)PA(start), len, flags);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001095 num_desc++;
1096
1097 if ((i == (flash.cws_per_page - 1)) && (cfg_mode == NAND_CFG))
1098 {
1099 /* write extra data */
1100 start = (uint32_t)spareaddr;
1101 len = (flash.cws_per_page << 2);
1102 flags = BAM_DESC_EOT_FLAG | BAM_DESC_INT_FLAG;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001103 bam_add_one_desc(&bam, DATA_CONSUMER_PIPE_INDEX, (unsigned char*)PA(start), len, flags);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001104 num_desc++;
1105 }
1106 }
1107
1108 bam_sys_gen_event(&bam, DATA_CONSUMER_PIPE_INDEX, num_desc);
1109}
1110
1111static nand_result_t
1112qpic_nand_write_page(uint32_t pg_addr,
1113 enum nand_cfg_value cfg_mode,
1114 const void* buffer,
1115 const void* spareaddr)
1116{
1117 struct cfg_params cfg;
Deepa Dinamani16663a62013-02-07 16:25:59 -08001118 uint32_t status[QPIC_NAND_MAX_CWS_IN_PAGE];
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001119 int nand_ret = NANDC_RESULT_SUCCESS;
1120
1121 if (cfg_mode == NAND_CFG_RAW)
1122 {
1123 cfg.cfg0 = cfg0_raw;
1124 cfg.cfg1 = cfg1_raw;
1125 }
1126 else
1127 {
1128 cfg.cfg0 = cfg0;
1129 cfg.cfg1 = cfg1;
1130 }
1131
1132 cfg.cmd = NAND_CMD_PRG_PAGE;
1133 cfg.exec = 1;
1134
1135 cfg.addr0 = pg_addr << 16;
1136 cfg.addr1 = (pg_addr >> 16) & 0xff;
1137
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001138 qpic_add_wr_page_cws_data_desc(buffer, cfg_mode, spareaddr);
1139
Deepa Dinamani19530062012-10-03 14:43:05 -07001140 qpic_nand_add_wr_page_cws_cmd_desc(&cfg, status, cfg_mode);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001141
1142 /* Check for errors */
1143 for(unsigned i = 0; i < flash.cws_per_page; i++)
1144 {
1145 nand_ret = qpic_nand_check_status(status[i]);
1146 if (nand_ret)
1147 {
1148 dprintf(CRITICAL,
1149 "Failed to write CW %d for page: %d\n",
1150 i, pg_addr);
1151 break;
1152 }
1153 }
1154
1155 /* Wait for data to be available */
1156 qpic_nand_wait_for_data(DATA_CONSUMER_PIPE_INDEX);
1157
1158 return nand_ret;
1159}
1160
1161static int
1162qpic_nand_mark_badblock(uint32_t page)
1163{
1164 char empty_buf[NAND_CW_SIZE_8_BIT_ECC];
1165
1166 memset(empty_buf, 0, NAND_CW_SIZE_8_BIT_ECC);
1167
1168 /* Going to first page of the block */
1169 if (page & flash.num_pages_per_blk_mask)
1170 page = page - (page & flash.num_pages_per_blk_mask);
1171
1172 return qpic_nand_write_page(page, NAND_CFG_RAW, empty_buf, 0);
1173}
1174
1175static void
1176qpic_nand_non_onfi_probe(struct flash_info *flash)
1177{
1178 int dev_found = 0;
1179 unsigned index;
1180 uint32_t ecc_bits;
1181
1182 /* Read the nand id. */
1183 qpic_nand_fetch_id(flash);
1184
1185 /* Check if we support the device */
Deepa Dinamani649a94a2013-03-07 14:37:31 -08001186 for (index = 0; index < (ARRAY_SIZE(supported_flash)); index++)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001187 {
1188 if ((flash->id & supported_flash[index].mask) ==
1189 (supported_flash[index].flash_id & (supported_flash[index].mask)))
1190 {
1191 dev_found = 1;
1192 break;
1193 }
1194 }
1195
1196 if (dev_found)
1197 {
1198 flash->page_size = supported_flash[index].pagesize;
1199 flash->block_size = supported_flash[index].blksize;
1200 flash->spare_size = supported_flash[index].oobsize;
1201 ecc_bits = supported_flash[index].ecc_8_bits;
1202
1203 /* Make sure that the block size and page size are defined. */
1204 ASSERT(flash->block_size);
1205 ASSERT(flash->page_size);
1206
1207 flash->num_blocks = supported_flash[index].density;
1208 flash->num_blocks /= (flash->block_size);
1209 flash->num_pages_per_blk = flash->block_size / flash->page_size;
1210 flash->num_pages_per_blk_mask = flash->num_pages_per_blk - 1;
1211
1212 /* Look for 8bit BCH ECC Nand, TODO: ECC Correctability >= 8 */
1213 if (ecc_bits)
1214 flash->ecc_width = NAND_WITH_8_BIT_ECC;
1215 else
1216 flash->ecc_width = NAND_WITH_4_BIT_ECC;
1217
1218 flash->density = supported_flash[index].density;
1219 flash->widebus = supported_flash[index].widebus;
1220
1221 return;
1222 }
1223
1224 /* Flash device is not supported, print flash device info and halt */
1225 if (dev_found == 0)
1226 {
1227 dprintf(CRITICAL, "NAND device is not supported: nandid: 0x%x"
1228 "maker=0x%02x device=0x%02x\n",
1229 flash->id,
1230 flash->vendor,
1231 flash->device);
1232 ASSERT(0);
1233 }
1234
1235 dprintf(INFO, "nandid: 0x%x maker=0x%02x device=0x%02x page_size=%d\n",
1236 flash->id,
1237 flash->vendor,
1238 flash->device,
1239 flash->page_size);
1240
1241 dprintf(INFO, "spare_size=%d block_size=%d num_blocks=%d\n",
1242 flash->spare_size,
1243 flash->block_size,
1244 flash->num_blocks);
1245}
1246
1247void
1248qpic_nand_init(struct qpic_nand_init_config *config)
1249{
1250 uint32_t i;
1251 int nand_ret;
1252
1253 nand_base = config->nand_base;
1254
Deepa Dinamanie9ded132012-11-27 15:03:38 -08001255 qpic_bam_init(config);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001256
Deepa Dinamani649a94a2013-03-07 14:37:31 -08001257 qpic_nand_non_onfi_probe(&flash);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001258
1259 /* Save the RAW and read/write configs */
1260 qpic_nand_save_config(&flash);
1261
1262 flash_spare_bytes = (unsigned char *)malloc(flash.spare_size);
1263
1264 if (flash_spare_bytes == NULL)
1265 {
1266 dprintf(CRITICAL, "Failed to allocate memory for spare bytes\n");
1267 return;
1268 }
1269
1270 /* Create a bad block table */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001271 bbtbl = (uint8_t *) malloc(sizeof(uint8_t) * flash.num_blocks);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001272
1273 if (bbtbl == NULL)
1274 {
1275 dprintf(CRITICAL, "Failed to allocate memory for bad block table\n");
1276 return;
1277 }
1278
1279 for (i = 0; i < flash.num_blocks; i++)
1280 bbtbl[i] = NAND_BAD_BLK_VALUE_NOT_READ;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001281
1282 /* Set aside contiguous memory for reads/writes.
1283 * This is needed as the BAM transfers only work with
1284 * physically contiguous buffers.
1285 * We will copy any data to be written/ to be read from
1286 * nand to this buffer and this buffer will be submitted to BAM.
1287 */
1288 rdwr_buf = (uint8_t*) malloc(flash.page_size + flash.spare_size);
1289
1290 if (rdwr_buf == NULL)
1291 {
1292 dprintf(CRITICAL, "Failed to allocate memory for page reads or writes\n");
1293 return;
1294 }
1295
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001296}
1297
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001298unsigned
Gaurav Nebhwani575fe1c2017-06-21 14:38:46 +08001299flash_num_pages_per_blk(void)
1300{
1301 return flash.num_pages_per_blk;
1302}
1303
1304unsigned
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001305flash_page_size(void)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001306{
1307 return flash.page_size;
1308}
1309
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001310unsigned
1311flash_block_size(void)
1312{
1313 return flash.block_size;
1314}
1315
Deepa Dinamani8e6b2432012-10-17 17:12:44 -07001316unsigned
1317flash_num_blocks(void)
1318{
1319 return flash.num_blocks;
1320}
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001321
1322struct ptable *
1323flash_get_ptable(void)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001324{
1325 return flash_ptable;
1326}
1327
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001328void
Deepa Dinamani87feab82012-10-04 14:28:05 -07001329qpic_nand_uninit()
1330{
1331 bam_pipe_reset(&bam, DATA_PRODUCER_PIPE_INDEX);
1332 bam_pipe_reset(&bam, DATA_CONSUMER_PIPE_INDEX);
1333 bam_pipe_reset(&bam, CMD_PIPE_INDEX);
1334
1335}
1336void
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001337flash_set_ptable(struct ptable *new_ptable)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001338{
1339 ASSERT(flash_ptable == NULL && new_ptable != NULL);
1340 flash_ptable = new_ptable;
1341}
1342
1343/* Note: No support for raw reads. */
1344static int
1345qpic_nand_read_page(uint32_t page, unsigned char* buffer, unsigned char* spareaddr)
1346{
1347 struct cfg_params params;
1348 uint32_t ecc;
Deepa Dinamani16663a62013-02-07 16:25:59 -08001349 uint32_t flash_sts[QPIC_NAND_MAX_CWS_IN_PAGE];
1350 uint32_t buffer_sts[QPIC_NAND_MAX_CWS_IN_PAGE];
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001351 uint32_t addr_loc_0;
1352 uint32_t addr_loc_1;
1353 struct cmd_element *cmd_list_ptr = ce_array;
1354 struct cmd_element *cmd_list_ptr_start = ce_array;
1355 uint32_t num_cmd_desc = 0;
1356 uint32_t num_data_desc = 0;
1357 uint32_t status;
1358 uint32_t i;
1359 int nand_ret = NANDC_RESULT_SUCCESS;
1360 /* UD bytes in last CW is 512 - cws_per_page *4.
1361 * Since each of the CW read earlier reads 4 spare bytes.
1362 */
1363 uint16_t ud_bytes_in_last_cw = USER_DATA_BYTES_PER_CW - ((flash.cws_per_page - 1) << 2);
1364 uint16_t oob_bytes = DATA_BYTES_IN_IMG_PER_CW - ud_bytes_in_last_cw;
1365
1366 params.addr0 = page << 16;
1367 params.addr1 = (page >> 16) & 0xff;
1368 params.cfg0 = cfg0;
1369 params.cfg1 = cfg1;
1370 params.cmd = NAND_CMD_PAGE_READ_ALL;
1371 params.exec = 1;
1372 ecc = ecc_bch_cfg;
1373
1374 /* Read all the Data bytes in the first 3 CWs. */
1375 addr_loc_0 = NAND_RD_LOC_OFFSET(0);
1376 addr_loc_0 |= NAND_RD_LOC_SIZE(DATA_BYTES_IN_IMG_PER_CW);
1377 addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
1378
1379
1380 addr_loc_1 = NAND_RD_LOC_OFFSET(ud_bytes_in_last_cw);
1381 addr_loc_1 |= NAND_RD_LOC_SIZE(oob_bytes);
1382 addr_loc_1 |= NAND_RD_LOC_LAST_BIT(1);
1383
Deepa Dinamanidc1381e2012-11-15 14:53:24 -08001384 status = qpic_nand_block_isbad(page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001385
1386 if (status)
1387 return status;
1388
sundarajan srinivasan7ced6482013-03-21 16:01:18 -07001389 /* Reset and Configure erased CW/page detection controller */
1390 qpic_nand_erased_status_reset(ce_array, BAM_DESC_LOCK_FLAG);
1391
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001392 for (i = 0; i < flash.cws_per_page; i++)
1393 {
1394 num_cmd_desc = 0;
1395 num_data_desc = 0;
1396
1397 if (i == 0)
1398 {
1399 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(&params, cmd_list_ptr);
1400
1401 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG,(uint32_t)ecc, CE_WRITE_TYPE);
1402 cmd_list_ptr++;
1403 }
1404 else
1405 cmd_list_ptr_start = cmd_list_ptr;
1406
1407 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)params.cmd, CE_WRITE_TYPE);
1408 cmd_list_ptr++;
1409
1410 if (i == flash.cws_per_page - 1)
1411 {
1412 addr_loc_0 = NAND_RD_LOC_OFFSET(0);
1413 addr_loc_0 |= NAND_RD_LOC_SIZE(ud_bytes_in_last_cw);
1414 addr_loc_0 |= NAND_RD_LOC_LAST_BIT(0);
1415
1416 /* Write addr loc 1 only for the last CW. */
1417 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(1), (uint32_t)addr_loc_1, CE_WRITE_TYPE);
1418 cmd_list_ptr++;
1419
1420 /* Add Data desc */
1421 bam_add_one_desc(&bam,
1422 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001423 (unsigned char *)PA((addr_t)buffer),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001424 ud_bytes_in_last_cw,
1425 0);
1426 num_data_desc++;
1427
1428 bam_add_one_desc(&bam,
1429 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001430 (unsigned char *)PA((addr_t)spareaddr),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001431 oob_bytes,
1432 BAM_DESC_INT_FLAG);
1433 num_data_desc++;
1434
1435 bam_sys_gen_event(&bam, DATA_PRODUCER_PIPE_INDEX, num_data_desc);
1436 }
1437 else
1438 {
1439 /* Add Data desc */
1440 bam_add_one_desc(&bam,
1441 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001442 (unsigned char *)PA((addr_t)buffer),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001443 DATA_BYTES_IN_IMG_PER_CW,
1444 BAM_DESC_INT_FLAG);
1445 num_data_desc++;
1446 bam_sys_gen_event(&bam, DATA_PRODUCER_PIPE_INDEX, num_data_desc);
1447 }
1448
1449 /* Write addr loc 0. */
1450 bam_add_cmd_element(cmd_list_ptr,
1451 NAND_READ_LOCATION_n(0),
1452 (uint32_t)addr_loc_0,
1453 CE_WRITE_TYPE);
1454
1455 cmd_list_ptr++;
1456 bam_add_cmd_element(cmd_list_ptr,
1457 NAND_EXEC_CMD,
1458 (uint32_t)params.exec,
1459 CE_WRITE_TYPE);
1460 cmd_list_ptr++;
1461
1462 /* Enqueue the desc for the above commands */
1463 bam_add_one_desc(&bam,
1464 CMD_PIPE_INDEX,
1465 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001466 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
sundarajan srinivasan7ced6482013-03-21 16:01:18 -07001467 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001468 num_cmd_desc++;
1469
1470 qpic_nand_wait_for_cmd_exec(num_cmd_desc);
1471
1472 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
1473
1474 /* Save the status registers. */
1475 flash_sts[i] = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr++);
1476 buffer_sts[i] = qpic_nand_read_reg(NAND_BUFFER_STATUS, 0, cmd_list_ptr++);
1477
Deepa Dinamani19530062012-10-03 14:43:05 -07001478 flash_sts[i] = qpic_nand_check_status(flash_sts[i]);
1479
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001480 buffer += DATA_BYTES_IN_IMG_PER_CW;
1481 }
1482
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001483 /* Read the buffer status again so that we can unlock the bam with this desc. */
1484 buffer_sts[--i] = qpic_nand_read_reg(NAND_BUFFER_STATUS, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr++);
Deepa Dinamani4b718ec2012-09-20 11:24:47 -07001485
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001486 /* Check status */
1487 for (i = 0; i < flash.cws_per_page ; i ++)
Deepa Dinamani19530062012-10-03 14:43:05 -07001488 if (flash_sts[i])
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001489 {
1490 nand_ret = NANDC_RESULT_BAD_PAGE;
1491 dprintf(CRITICAL, "NAND page read failed. page: %x\n", page);
1492 goto qpic_nand_read_page_error;
1493 }
1494
1495qpic_nand_read_page_error:
1496return nand_ret;
1497}
1498
1499/* Function to read a flash partition.
1500 * ptn : Partition to read.
1501 * extra_per_page : Spare data to be read.
1502 * offset : Num of bytes offset into the partition.
1503 * data : Buffer to read the data into.
1504 * bytes : Num of bytes to be read.
1505 */
1506 /* TODO: call this func read_partition. */
1507int
1508flash_read_ext(struct ptentry *ptn,
1509 unsigned extra_per_page,
1510 unsigned offset,
1511 void *data,
1512 unsigned bytes)
1513{
1514 uint32_t page =
1515 (ptn->start * flash.num_pages_per_blk) + (offset / flash.page_size);
1516 uint32_t lastpage = (ptn->start + ptn->length) * flash.num_pages_per_blk;
1517 uint32_t count =
1518 (bytes + flash.page_size - 1 + extra_per_page) / (flash.page_size +
1519 extra_per_page);
1520 uint32_t *spare = (unsigned *)flash_spare_bytes;
1521 uint32_t errors = 0;
1522 unsigned char *image = data;
1523 int result = 0;
1524 uint32_t current_block =
1525 (page - (page & flash.num_pages_per_blk_mask)) / flash.num_pages_per_blk;
1526 uint32_t start_block = ptn->start;
1527 uint32_t start_block_count = 0;
1528 uint32_t isbad = 0;
Deepa Dinamani2f7006c2013-08-19 11:59:38 -07001529 uint32_t current_page;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001530
1531 /* Verify first byte is at page boundary. */
1532 if (offset & (flash.page_size - 1))
1533 {
1534 dprintf(CRITICAL, "Read request start not at page boundary: %d\n",
1535 offset);
1536 return NANDC_RESULT_PARAM_INVALID;
1537 }
1538
Deepa Dinamani2f7006c2013-08-19 11:59:38 -07001539 current_page = start_block * flash.num_pages_per_blk;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001540 /* Adjust page offset based on number of bad blocks from start to current page */
1541 if (start_block < current_block)
1542 {
1543 start_block_count = (current_block - start_block);
1544 while (start_block_count
1545 && (start_block < (ptn->start + ptn->length)))
1546 {
Deepa Dinamani2f7006c2013-08-19 11:59:38 -07001547 isbad = qpic_nand_block_isbad(current_page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001548 if (isbad)
1549 page += flash.num_pages_per_blk;
1550 else
1551 start_block_count--;
1552 start_block++;
Deepa Dinamani2f7006c2013-08-19 11:59:38 -07001553 current_page += flash.num_pages_per_blk;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001554 }
1555 }
1556
1557 while ((page < lastpage) && !start_block_count)
1558 {
1559 if (count == 0)
1560 {
Deepa Dinamani52aca8d2013-02-05 11:41:41 -08001561 dprintf(SPEW, "flash_read_image: success (%d errors)\n",
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001562 errors);
1563 return NANDC_RESULT_SUCCESS;
1564 }
1565
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001566 result = qpic_nand_read_page(page, rdwr_buf, (unsigned char *)spare);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001567
1568 if (result == NANDC_RESULT_BAD_PAGE)
1569 {
1570 /* bad page, go to next page. */
1571 page++;
1572 errors++;
1573 continue;
1574 }
1575 else if (result == NANDC_RESULT_BAD_BLOCK)
1576 {
1577 /* bad block, go to next block same offset. */
1578 page += flash.num_pages_per_blk;
1579 errors++;
1580 continue;
1581 }
1582
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001583 /* Copy the read page into correct location. */
1584 memcpy(image, rdwr_buf, flash.page_size);
1585
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001586 page++;
1587 image += flash.page_size;
1588 /* Copy spare bytes to image */
1589 memcpy(image, spare, extra_per_page);
1590 image += extra_per_page;
1591 count -= 1;
1592 }
1593
1594 /* could not find enough valid pages before we hit the end */
1595 dprintf(CRITICAL, "flash_read_image: failed (%d errors)\n", errors);
1596 return NANDC_RESULT_FAILURE;
1597}
1598
1599int
1600flash_erase(struct ptentry *ptn)
1601{
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001602 int ret = 0;
1603
1604 ret = qpic_nand_blk_erase(ptn->start * flash.num_pages_per_blk);
1605
1606 if (ret)
1607 dprintf(CRITICAL, "Erase operation failed \n");
1608
1609 return ret;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001610}
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001611
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001612int
1613flash_ecc_bch_enabled()
1614{
1615 return (flash.ecc_width == NAND_WITH_4_BIT_ECC)? 0 : 1;
1616}
1617
1618int
1619flash_write(struct ptentry *ptn,
1620 unsigned extra_per_page,
1621 const void *data,
1622 unsigned bytes)
1623{
1624 uint32_t page = ptn->start * flash.num_pages_per_blk;
1625 uint32_t lastpage = (ptn->start + ptn->length) * flash.num_pages_per_blk;
1626 uint32_t *spare = (unsigned *)flash_spare_bytes;
1627 const unsigned char *image = data;
1628 uint32_t wsize = flash.page_size + extra_per_page;
1629 int r;
1630
1631 memset(spare, 0xff, (flash.spare_size / flash.cws_per_page));
1632
1633 while (bytes > 0)
1634 {
1635 if (bytes < wsize)
1636 {
1637 dprintf(CRITICAL,
1638 "flash_write_image: image undersized (%d < %d)\n",
1639 bytes,
1640 wsize);
1641 return -1;
1642 }
1643
1644 if (page >= lastpage)
1645 {
1646 dprintf(CRITICAL, "flash_write_image: out of space\n");
1647 return -1;
1648 }
1649
1650 if ((page & flash.num_pages_per_blk_mask) == 0)
1651 {
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001652 if (qpic_nand_blk_erase(page))
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001653 {
1654 dprintf(INFO,
1655 "flash_write_image: bad block @ %d\n",
1656 page / flash.num_pages_per_blk);
1657
1658 page += flash.num_pages_per_blk;
1659 continue;
1660 }
1661 }
1662
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001663 memcpy(rdwr_buf, image, flash.page_size);
1664
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001665 if (extra_per_page)
1666 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001667 memcpy(rdwr_buf + flash.page_size, image + flash.page_size, extra_per_page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001668 r = qpic_nand_write_page(page,
1669 NAND_CFG,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001670 rdwr_buf,
1671 rdwr_buf + flash.page_size);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001672 }
1673 else
1674 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001675 r = qpic_nand_write_page(page, NAND_CFG, rdwr_buf, spare);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001676 }
1677
1678 if (r)
1679 {
1680 dprintf(INFO,
1681 "flash_write_image: write failure @ page %d (src %d)\n",
1682 page,
1683 image - (const unsigned char *)data);
1684
1685 image -= (page & flash.num_pages_per_blk_mask) * wsize;
1686 bytes += (page & flash.num_pages_per_blk_mask) * wsize;
1687 page &= ~flash.num_pages_per_blk_mask;
1688 if (qpic_nand_blk_erase(page))
1689 {
1690 dprintf(INFO,
1691 "flash_write_image: erase failure @ page %d\n",
1692 page);
1693 }
1694
1695 qpic_nand_mark_badblock(page);
1696
1697 dprintf(INFO,
1698 "flash_write_image: restart write @ page %d (src %d)\n",
1699 page, image - (const unsigned char *)data);
1700
1701 page += flash.num_pages_per_blk;
1702 continue;
1703 }
1704 page++;
1705 image += wsize;
1706 bytes -= wsize;
1707 }
1708
1709 /* erase any remaining pages in the partition */
1710 page = (page + flash.num_pages_per_blk_mask) & (~flash.num_pages_per_blk_mask);
1711
1712 while (page < lastpage)
1713 {
1714 if (qpic_nand_blk_erase(page))
1715 {
1716 dprintf(INFO, "flash_write_image: bad block @ %d\n",
1717 page / flash.num_pages_per_blk);
1718 }
1719 page += flash.num_pages_per_blk;
1720 }
1721
1722 dprintf(INFO, "flash_write_image: success\n");
1723 return 0;
1724}