blob: f21e03272aa322facf58aee229a016a5c9427f45 [file] [log] [blame]
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001/*
2 * Copyright (c) 2008, Google Inc.
3 * All rights reserved.
Joonwoo Park342b2362014-08-25 15:41:39 -07004 * Copyright (c) 2009-2014, 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>
Smita Ghoshf5431c62014-09-18 14:11:14 -070039#include <platform/iomap.h>
Deepa Dinamanie4573be2012-08-03 16:32:29 -070040
41static uint32_t nand_base;
42static struct ptable *flash_ptable;
43static struct flash_info flash;
44static unsigned char *flash_spare_bytes;
45static uint32_t cfg0;
46static uint32_t cfg1;
47static uint32_t cfg0_raw;
48static uint32_t cfg1_raw;
49static uint32_t ecc_bch_cfg;
50
51struct cmd_element ce_array[100];
52
53#define QPIC_BAM_DATA_FIFO_SIZE 64
54#define QPIC_BAM_CMD_FIFO_SIZE 64
55
56static struct bam_desc cmd_desc_fifo[QPIC_BAM_CMD_FIFO_SIZE] __attribute__ ((aligned(BAM_DESC_SIZE)));
57static struct bam_desc data_desc_fifo[QPIC_BAM_DATA_FIFO_SIZE] __attribute__ ((aligned(BAM_DESC_SIZE)));
58
59static struct bam_instance bam;
60static uint8_t *bbtbl;
61
Deepa Dinamani0bf2f442012-10-19 11:41:06 -070062static uint8_t* rdwr_buf;
63
Deepa Dinamanie4573be2012-08-03 16:32:29 -070064static struct flash_id supported_flash[] = {
Deepa Dinamani649a94a2013-03-07 14:37:31 -080065 /* Flash ID ID Mask Density(MB) Wid Pgsz Blksz oobsz 8-bit ECCf */
66 {0x1590AC2C, 0xFFFFFFFF, 0x20000000, 0, 2048, 0x00020000, 0x40, 0},
Sundarajan Srinivasand4e21952013-08-27 15:59:10 -070067 {0x1590AA2C, 0xFFFFFFFF, 0x10000000, 0, 2048, 0x00020000, 0xE0, 1},
Deepa Dinamani649a94a2013-03-07 14:37:31 -080068 {0x2690AC2C, 0xFFFFFFFF, 0x20000000, 0, 4096, 0x00040000, 0xE0, 1},
Sundarajan Srinivasand69c0d32013-08-09 16:47:33 -070069 {0x1590ACAD, 0xFFFFFFFF, 0x20000000, 0, 2048, 0x00020000, 0x80, 0},
Joonwoo Park342b2362014-08-25 15:41:39 -070070 {0x9590DC2C, 0xFFFFFFFF, 0x10000000, 0, 2048, 0x00020000, 0x40, 0},
Deepa Dinamanie4573be2012-08-03 16:32:29 -070071 /* Note: Width flag is 0 for 8 bit Flash and 1 for 16 bit flash */
Deepa Dinamanie4573be2012-08-03 16:32:29 -070072};
73
Deepa Dinamanie4573be2012-08-03 16:32:29 -070074static void
75qpic_nand_wait_for_cmd_exec(uint32_t num_desc)
76{
77 /* Create a read/write event to notify the periperal of the added desc. */
78 bam_sys_gen_event(&bam, CMD_PIPE_INDEX, num_desc);
79
80 /* Wait for the descriptors to be processed */
81 bam_wait_for_interrupt(&bam, CMD_PIPE_INDEX, P_PRCSD_DESC_EN_MASK);
82
83 /* Read offset update for the circular FIFO */
84 bam_read_offset_update(&bam, CMD_PIPE_INDEX);
85}
86
87static void
88qpic_nand_wait_for_data(uint32_t pipe_num)
89{
90 /* Wait for the descriptors to be processed */
91 bam_wait_for_interrupt(&bam, pipe_num, P_PRCSD_DESC_EN_MASK);
92
93 /* Read offset update for the circular FIFO */
94 bam_read_offset_update(&bam, pipe_num);
95}
96
97static uint32_t
98qpic_nand_read_reg(uint32_t reg_addr,
99 uint8_t flags,
100 struct cmd_element *cmd_list_ptr)
101{
102 uint32_t val;
103
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700104 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 -0700105
106 /* Enqueue the desc for the above command */
107 bam_add_one_desc(&bam,
108 CMD_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700109 (unsigned char*)PA((addr_t)cmd_list_ptr),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700110 BAM_CE_SIZE,
111 BAM_DESC_CMD_FLAG| BAM_DESC_INT_FLAG | flags);
112
113 qpic_nand_wait_for_cmd_exec(1);
114
115 return val;
116}
117
Deepa Dinamani19530062012-10-03 14:43:05 -0700118/* Assume the BAM is in a locked state. */
119void
sundarajan srinivasan7ced6482013-03-21 16:01:18 -0700120qpic_nand_erased_status_reset(struct cmd_element *cmd_list_ptr, uint8_t flags)
Deepa Dinamani19530062012-10-03 14:43:05 -0700121{
122 uint32_t val = 0;
123
124 /* Reset the Erased Codeword/Page detection controller. */
125 val = NAND_ERASED_CW_DETECT_CFG_RESET_CTRL;
126
127 bam_add_cmd_element(cmd_list_ptr, NAND_ERASED_CW_DETECT_CFG, val, CE_WRITE_TYPE);
128
129 /* Enqueue the desc for the above command */
130 bam_add_one_desc(&bam,
131 CMD_PIPE_INDEX,
132 (unsigned char*)cmd_list_ptr,
133 BAM_CE_SIZE,
sundarajan srinivasan7ced6482013-03-21 16:01:18 -0700134 BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG | flags);
Deepa Dinamani19530062012-10-03 14:43:05 -0700135
136 qpic_nand_wait_for_cmd_exec(1);
137
138 /* Enable the Erased Codeword/Page detection
139 * controller to check the data as it arrives.
140 * Also disable ECC reporting for an erased CW.
141 */
142 val = NAND_ERASED_CW_DETECT_CFG_ACTIVATE_CTRL | NAND_ERASED_CW_DETECT_ERASED_CW_ECC_MASK;
143
144 bam_add_cmd_element(cmd_list_ptr, NAND_ERASED_CW_DETECT_CFG, val, CE_WRITE_TYPE);
145
146 /* Enqueue the desc for the above command */
147 bam_add_one_desc(&bam,
148 CMD_PIPE_INDEX,
149 (unsigned char*)cmd_list_ptr,
150 BAM_CE_SIZE,
151 BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG);
152
153 qpic_nand_wait_for_cmd_exec(1);
154}
155
156static nand_result_t
157qpic_nand_check_status(uint32_t status)
158{
159 uint32_t erase_sts;
160
161 /* Check for errors */
162 if (status & NAND_FLASH_ERR)
163 {
164 /* Check if this is an ECC error on an erased page. */
165 if (status & NAND_FLASH_OP_ERR)
166 {
167 erase_sts = qpic_nand_read_reg(NAND_ERASED_CW_DETECT_STATUS, 0, ce_array);
168 if ((erase_sts & (1 << NAND_ERASED_CW_DETECT_STATUS_PAGE_ALL_ERASED)))
169 {
170 /* Mask the OP ERROR. */
171 status &= ~NAND_FLASH_OP_ERR;
sundarajan srinivasan7ced6482013-03-21 16:01:18 -0700172 qpic_nand_erased_status_reset(ce_array, 0);
Deepa Dinamani19530062012-10-03 14:43:05 -0700173 }
174 }
175
176 /* ECC error flagged on an erased page read.
177 * Ignore and return success.
178 */
179 if (!(status & NAND_FLASH_ERR))
180 return NANDC_RESULT_SUCCESS;
181
182 dprintf(CRITICAL, "Nand Flash error. Status = %d\n", status);
183
184 if (status & NAND_FLASH_TIMEOUT_ERR)
185 return NANDC_RESULT_TIMEOUT;
186 else
187 return NANDC_RESULT_FAILURE;
188 }
189
190 return NANDC_RESULT_SUCCESS;
191}
192
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700193static uint32_t
194qpic_nand_fetch_id(struct flash_info *flash)
195{
196 struct cmd_element *cmd_list_ptr = ce_array;
197 struct cmd_element *cmd_list_ptr_start = ce_array;
198 int num_desc = 0;
199 uint32_t status;
200 uint32_t id;
201 uint32_t flash_cmd = NAND_CMD_FETCH_ID;
202 uint32_t exec_cmd = 1;
203 int nand_ret = NANDC_RESULT_SUCCESS;
204
205 /* Issue the Fetch id command to the NANDc */
206 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)flash_cmd, CE_WRITE_TYPE);
207 cmd_list_ptr++;
208
209 /* Execute the cmd */
210 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)exec_cmd, CE_WRITE_TYPE);
211 cmd_list_ptr++;
212
213 /* Prepare the cmd desc for the above commands */
214 bam_add_one_desc(&bam,
215 CMD_PIPE_INDEX,
216 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700217 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700218 BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG |
219 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG);
220
221 /* Keep track of the number of desc added. */
222 num_desc++;
223 qpic_nand_wait_for_cmd_exec(num_desc);
224
225 cmd_list_ptr_start = ce_array;
226 cmd_list_ptr = ce_array;
227
228 /* Read the status register */
229 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr);
230
231 /* Check for errors */
232 nand_ret = qpic_nand_check_status(status);
233 if (nand_ret)
234 {
235 dprintf( CRITICAL, "Read ID cmd status failed\n");
236 goto qpic_nand_fetch_id_err;
237 }
238
239 /* Read the id */
240 id = qpic_nand_read_reg(NAND_READ_ID, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr);
241
242 flash->id = id;
243 flash->vendor = id & 0xff;
244 flash->device = (id >> 8) & 0xff;
245 flash->dev_cfg = (id >> 24) & 0xFF;
246 flash->widebus = 0;
247 flash->widebus &= (id >> 24) & 0xFF;
248 flash->widebus = flash->widebus? 1: 0;
249
250qpic_nand_fetch_id_err:
251 return nand_ret;
252}
253
254static int
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800255qpic_bam_init(struct qpic_nand_init_config *config)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700256{
257 uint32_t bam_ret = NANDC_RESULT_SUCCESS;
258
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800259 bam.base = config->bam_base;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700260 /* Set Read pipe params. */
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800261 bam.pipe[DATA_PRODUCER_PIPE_INDEX].pipe_num = config->pipes.read_pipe;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700262 /* System consumer */
263 bam.pipe[DATA_PRODUCER_PIPE_INDEX].trans_type = BAM2SYS;
264 bam.pipe[DATA_PRODUCER_PIPE_INDEX].fifo.size = QPIC_BAM_DATA_FIFO_SIZE;
265 bam.pipe[DATA_PRODUCER_PIPE_INDEX].fifo.head = data_desc_fifo;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700266 bam.pipe[DATA_PRODUCER_PIPE_INDEX].lock_grp = config->pipes.read_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700267
268 /* Set Write pipe params. */
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800269 bam.pipe[DATA_CONSUMER_PIPE_INDEX].pipe_num = config->pipes.write_pipe;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700270 /* System producer */
271 bam.pipe[DATA_CONSUMER_PIPE_INDEX].trans_type = SYS2BAM;
272 bam.pipe[DATA_CONSUMER_PIPE_INDEX].fifo.size = QPIC_BAM_DATA_FIFO_SIZE;
273 bam.pipe[DATA_CONSUMER_PIPE_INDEX].fifo.head = data_desc_fifo;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700274 bam.pipe[DATA_CONSUMER_PIPE_INDEX].lock_grp = config->pipes.write_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700275
276 /* Set Cmd pipe params. */
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800277 bam.pipe[CMD_PIPE_INDEX].pipe_num = config->pipes.cmd_pipe;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700278 /* System consumer */
Sundarajan Srinivasan357c35a2013-07-23 17:00:54 -0700279 bam.pipe[CMD_PIPE_INDEX].trans_type = SYS2BAM;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700280 bam.pipe[CMD_PIPE_INDEX].fifo.size = QPIC_BAM_CMD_FIFO_SIZE;
281 bam.pipe[CMD_PIPE_INDEX].fifo.head = cmd_desc_fifo;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700282 bam.pipe[CMD_PIPE_INDEX].lock_grp = config->pipes.cmd_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700283
284 /* Programs the threshold for BAM transfer
285 * When this threshold is reached, BAM signals the peripheral via the pipe_bytes_available
286 * interface.
287 * The peripheral is signalled with this notification in the following cases:
288 * a. It has accumulated all the descriptors.
289 * b. It has accumulated more than threshold bytes.
290 * c. It has reached EOT (End Of Transfer).
291 * Note: this value needs to be set by the h/w folks and is specific for each peripheral.
292 */
293 bam.threshold = 32;
294
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800295 /* Set the EE. */
296 bam.ee = config->ee;
297
298 /* Set the max desc length for this BAM. */
299 bam.max_desc_len = config->max_desc_len;
300
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700301 /* BAM Init. */
302 bam_init(&bam);
303
304 /* Initialize BAM QPIC read pipe */
305 bam_sys_pipe_init(&bam, DATA_PRODUCER_PIPE_INDEX);
306
307 /* Init read fifo */
308 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[DATA_PRODUCER_PIPE_INDEX].pipe_num);
309
310 if (bam_ret)
311 {
312 dprintf(CRITICAL, "QPIC:NANDc BAM Read FIFO init error\n");
313 bam_ret = NANDC_RESULT_FAILURE;
314 goto qpic_nand_bam_init_error;
315 }
316
317 /* Initialize BAM QPIC write pipe */
318 bam_sys_pipe_init(&bam, DATA_CONSUMER_PIPE_INDEX);
319
320 /* Init write fifo. Use the same fifo as read fifo. */
321 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[DATA_CONSUMER_PIPE_INDEX].pipe_num);
322
323 if (bam_ret)
324 {
325 dprintf(CRITICAL, "QPIC: NANDc: BAM Write FIFO init error\n");
326 bam_ret = NANDC_RESULT_FAILURE;
327 goto qpic_nand_bam_init_error;
328 }
329
330 /* Initialize BAM QPIC cmd pipe */
331 bam_sys_pipe_init(&bam, CMD_PIPE_INDEX);
332
333 /* Init cmd fifo */
334 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[CMD_PIPE_INDEX].pipe_num);
335
336 if (bam_ret)
337 {
338 dprintf(CRITICAL, "QPIC:NANDc BAM CMD FIFO init error\n");
339 bam_ret = NANDC_RESULT_FAILURE;
340 goto qpic_nand_bam_init_error;
341 }
342
343qpic_nand_bam_init_error:
344return bam_ret;
345}
346
347/* Adds command elements for addr and cfg register writes.
348 * cfg: Defines the configuration for the flash cmd.
349 * start: Address where the command elements are added.
350 *
351 * Returns the address where the next cmd element can be added.
352 */
353static struct cmd_element*
354qpic_nand_add_addr_n_cfg_ce(struct cfg_params *cfg,
355 struct cmd_element *start)
356{
357 struct cmd_element *cmd_list_ptr = start;
358
359 bam_add_cmd_element(cmd_list_ptr, NAND_ADDR0, (uint32_t)cfg->addr0, CE_WRITE_TYPE);
360 cmd_list_ptr++;
361 bam_add_cmd_element(cmd_list_ptr, NAND_ADDR1, (uint32_t)cfg->addr1, CE_WRITE_TYPE);
362 cmd_list_ptr++;
363 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_CFG0, (uint32_t)cfg->cfg0, CE_WRITE_TYPE);
364 cmd_list_ptr++;
365 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_CFG1, (uint32_t)cfg->cfg1, CE_WRITE_TYPE);
366 cmd_list_ptr++;
367
368 return cmd_list_ptr;
369}
370
371
372static struct cmd_element*
373qpic_nand_add_onfi_probe_ce(struct onfi_probe_params *params,
374 struct cmd_element *start)
375{
376 struct cmd_element *cmd_list_ptr = start;
377
378 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(&params->cfg, cmd_list_ptr);
379
380 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD1, (uint32_t)params->dev_cmd1, CE_WRITE_TYPE);
381 cmd_list_ptr++;
382 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD_VLD, (uint32_t)params->vld, CE_WRITE_TYPE);
383 cmd_list_ptr++;
384 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(0), (uint32_t)params->cfg.addr_loc_0, CE_WRITE_TYPE);
385 cmd_list_ptr++;
386 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)params->cfg.cmd, CE_WRITE_TYPE);
387 cmd_list_ptr++;
388 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)params->cfg.exec, CE_WRITE_TYPE);
389 cmd_list_ptr++;
390
391 return cmd_list_ptr;
392}
393
394static int
395onfi_probe_cmd_exec(struct onfi_probe_params *params,
396 unsigned char* data_ptr,
397 int data_len)
398{
399 struct cmd_element *cmd_list_ptr = ce_array;
400 struct cmd_element *cmd_list_ptr_start = ce_array;
401 int num_desc = 0;
402 uint32_t status = 0;
403 int nand_ret = NANDC_RESULT_SUCCESS;
404 uint8_t desc_flags = BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG
405 | BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG;
406
407 params->cfg.addr_loc_0 = 0;
408 params->cfg.addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
409 params->cfg.addr_loc_0 |= NAND_RD_LOC_OFFSET(0);
410 params->cfg.addr_loc_0 |= NAND_RD_LOC_SIZE(data_len);
411
412 cmd_list_ptr = qpic_nand_add_onfi_probe_ce(params, cmd_list_ptr);
413
414 /* Enqueue the desc for the above commands */
415 bam_add_one_desc(&bam,
416 CMD_PIPE_INDEX,
417 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700418 PA((addr_t)(uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700419 desc_flags);
420
421 cmd_list_ptr_start = cmd_list_ptr;
422 num_desc++;
423
424 /* Add Data desc */
425 bam_add_desc(&bam,
426 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700427 (unsigned char *)PA((addr_t)data_ptr),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700428 data_len,
429 BAM_DESC_INT_FLAG);
430
431 /* Wait for the commands to be executed */
432 qpic_nand_wait_for_cmd_exec(num_desc);
433
434 /* Read buffer status and check for errors. */
435 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr++);
436
437 if (qpic_nand_check_status(status))
438 {
439 nand_ret = NANDC_RESULT_FAILURE;
440 goto onfi_probe_exec_err;
441 }
442
443 /* Wait for data to be available */
444 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
445
446 /* Check for errors */
447 nand_ret = qpic_nand_check_status(status);
448
449onfi_probe_exec_err:
450 return nand_ret;
451}
452
453/* TODO: check why both vld and cmd need to be written. */
454void
455qpic_nand_onfi_probe_cleanup(uint32_t vld, uint32_t dev_cmd1)
456{
457 struct cmd_element *cmd_list_ptr = ce_array;
458 struct cmd_element *cmd_list_ptr_start = ce_array;
459
460 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD1, dev_cmd1, CE_WRITE_TYPE);
461 cmd_list_ptr++;
462 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD_VLD, vld, CE_WRITE_TYPE);
463 cmd_list_ptr++;
464
465 /* Enqueue the desc for the above commands */
466 bam_add_one_desc(&bam,
467 CMD_PIPE_INDEX,
468 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700469 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700470 BAM_DESC_UNLOCK_FLAG | BAM_DESC_CMD_FLAG| BAM_DESC_INT_FLAG);
471
472 qpic_nand_wait_for_cmd_exec(1);
473}
474
475static int
476qpic_nand_onfi_save_params(struct onfi_param_page *param_page, struct flash_info *flash)
477{
478 int onfi_ret = NANDC_RESULT_SUCCESS;
479 uint32_t ecc_bits;
480
481 onfi_ret = qpic_nand_fetch_id(flash);
482
483 if (onfi_ret)
484 {
485 dprintf(CRITICAL, "Fetch ID cmd failed\n");
486 goto onfi_save_params_err;
487 }
488
489 flash->page_size = param_page->data_per_pg;
490 flash->block_size = param_page->pgs_per_blk * flash->page_size;
491 flash->num_blocks = param_page->blks_per_LUN;
492 flash->widebus = param_page->feature_supported & 0x1;
493 flash->density = param_page->blks_per_LUN * flash->blksize;
494 flash->spare_size = param_page->spare_per_pg;
495 ecc_bits = param_page->num_bits_ecc_correctability;
496 flash->num_pages_per_blk = param_page->pgs_per_blk;
497 flash->num_pages_per_blk_mask = param_page->pgs_per_blk - 1;
498
499 if (ecc_bits >= 8)
500 flash->ecc_width = NAND_WITH_8_BIT_ECC;
501 else
502 flash->ecc_width = NAND_WITH_4_BIT_ECC;
503
504 onfi_save_params_err:
505 return onfi_ret;
506}
507
508static void
509qpic_nand_save_config(struct flash_info *flash)
510{
511
512 /* Save Configurations */
513 flash->cws_per_page = flash->page_size >> NAND_CW_DIV_RIGHT_SHIFT;
514
Deepa Dinamani16663a62013-02-07 16:25:59 -0800515 /* Verify that we have enough buffer to handle all the cws in a page. */
516 ASSERT(flash->cws_per_page <= QPIC_NAND_MAX_CWS_IN_PAGE);
517
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700518 /* Codeword Size = UD_SIZE_BYTES + ECC_PARITY_SIZE_BYTES
519 * + SPARE_SIZE_BYTES + Bad Block size
520 */
521 if (flash->ecc_width & NAND_WITH_8_BIT_ECC)
522 {
523 flash->cw_size = NAND_CW_SIZE_8_BIT_ECC;
524 ecc_bch_cfg |= (1 << NAND_DEV0_ECC_MODE_SHIFT); /* Use 8-bit ecc */
525
526 if (flash->widebus)
527 {
528 cfg0 |= (0 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
529 ecc_bch_cfg |= (14 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
530 }
531 else
532 {
533 cfg0 |= (2 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
534 ecc_bch_cfg |= (13 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
535 }
536 }
537 else
538 {
539 flash->cw_size = NAND_CW_SIZE_4_BIT_ECC;
540
541 if (flash->widebus)
542 {
543 cfg0 |= (2 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
544 ecc_bch_cfg |= (8 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
545 }
546 else
547 {
548 cfg0 |= (4 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
549 ecc_bch_cfg |= (7 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
550 }
551 }
552
553 /* BAD_BLOCK_BYTE_NUM = Page Size -
554 * (CW_PER_PAGE * Codeword Size) + 1
555 * Note: Set CW_PER_PAGE to 1 less than the actual number.
556 */
557 flash->bad_blk_loc = flash->page_size - flash->cw_size * (flash->cws_per_page - 1) + 1;
558
559 cfg0 |= ((flash->cws_per_page - 1) << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT) /* 4/8 cw/pg for 2/4k */
560 |(DATA_BYTES_IN_IMG_PER_CW << NAND_DEV0_CFG0_UD_SIZE_BYTES_SHIFT) /* 516 user data bytes */
561 |(5 << NAND_DEV0_CFG0_ADDR_CYCLE_SHIFT) /* 5 address cycles */
562 |(0 << NAND_DEV0_CFG0_DIS_STS_AFTER_WR_SHIFT);/* Send read status cmd after each write. */
563
564 cfg1 |= (7 << NAND_DEV0_CFG1_RECOVERY_CYCLES_SHIFT) /* 8 recovery cycles */
565 |(0 << NAND_DEV0_CFG1_CS_ACTIVE_BSY_SHIFT) /* Allow CS deassertion */
566 |(flash->bad_blk_loc << NAND_DEV0_CFG1_BAD_BLK_BYTE_NUM_SHIFT)/* Bad block marker location */
567 |(0 << NAND_DEV0_CFG1_BAD_BLK_IN_SPARE_SHIFT) /* Bad block in user data area */
568 |(2 << NAND_DEV0_CFG1_WR_RD_BSY_GAP_SHIFT) /* 8 cycle tWB/tRB */
569 |(flash->widebus << NAND_DEV0_CFG1_WIDE_BUS_SHIFT); /* preserve wide flash flag */
570
571 cfg0_raw = ((flash->cws_per_page- 1) << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT)
572 |(5 << NAND_DEV0_CFG0_ADDR_CYCLE_SHIFT)
573 |(516 << NAND_DEV0_CFG0_UD_SIZE_BYTES_SHIFT) //figure out the size of cw
574 | (1 << NAND_DEV0_CFG0_DIS_STS_AFTER_WR_SHIFT);
575
576 cfg1_raw = (7 << NAND_DEV0_CFG1_RECOVERY_CYCLES_SHIFT)
577 | (0 << NAND_DEV0_CFG1_CS_ACTIVE_BSY_SHIFT)
578 | (17 << NAND_DEV0_CFG1_BAD_BLK_BYTE_NUM_SHIFT)
579 | (1 << NAND_DEV0_CFG1_BAD_BLK_IN_SPARE_SHIFT)
580 | (2 << NAND_DEV0_CFG1_WR_RD_BSY_GAP_SHIFT)
581 | (flash->widebus << NAND_DEV0_CFG1_WIDE_BUS_SHIFT)
582 |1 ; /* to disable reed solomon ecc..this feild is now read only. */
583
584 ecc_bch_cfg |= (0 << NAND_DEV0_ECC_DISABLE_SHIFT) /* Enable ECC */
585 | (0 << NAND_DEV0_ECC_SW_RESET_SHIFT) /* Put ECC core in op mode */
586 | (DATA_BYTES_IN_IMG_PER_CW << NAND_DEV0_ECC_NUM_DATA_BYTES)
587 | (1 << NAND_DEV0_ECC_FORCE_CLK_OPEN_SHIFT); /* Enable all clocks */
588}
589
590/* Onfi probe should issue the following commands to the flash device:
591 * 1. Read ID - with addr ONFI_READ_ID_ADDR.
592 * This returns the ONFI ASCII string indicating support for ONFI.
593 * 2. Read Prameter Page - with addr ONFI_READ_PARAM_PAGE_ADDR.
594 * This returns the params for the device.
595 * Each command inturn issues commands- ADDR0, ADDR1, chip_select,
596 * cfg0, cfg1, cmd_vld, dev_cmd1, read_loc0, flash, exec.
597 */
598static int
599qpic_nand_onfi_probe(struct flash_info *flash)
600{
601 struct onfi_probe_params params;
602 uint32_t vld;
603 uint32_t dev_cmd1;
604 unsigned char *buffer;
605 unsigned char onfi_str[4];
606 uint32_t *id;
607 struct onfi_param_page *param_page;
608 int onfi_ret = NANDC_RESULT_SUCCESS;
609
610 /* Allocate memory required to read the onfi param page */
611 buffer = (unsigned char*) malloc(ONFI_READ_PARAM_PAGE_BUFFER_SIZE);
Maria Yu71909742014-07-04 17:30:00 +0800612 ASSERT(buffer != NULL);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700613
614 /* Read the vld and dev_cmd1 registers before modifying */
615 vld = qpic_nand_read_reg(NAND_DEV_CMD_VLD, 0, ce_array);
616 dev_cmd1 = qpic_nand_read_reg(NAND_DEV_CMD1, 0, ce_array);
617
618 /* Initialize flash cmd */
619 params.cfg.cmd = NAND_CMD_PAGE_READ;
620 params.cfg.exec = 1;
621
622 /* Execute Read ID cmd */
623
624 /* Initialize the config */
625 params.cfg.cfg0 = NAND_CFG0_RAW_ONFI_ID;
626 params.cfg.cfg1 = NAND_CFG1_RAW_ONFI_ID;
627
628 /* Initialize the cmd and vld */
629 params.dev_cmd1 = (dev_cmd1 & 0xFFFFFF00) | ONFI_READ_ID_CMD;
630 params.vld = vld & 0xFFFFFFFE;
631
632 /* Initialize the address
633 * addr1 is not used bcos of the cfg.
634 */
635 params.cfg.addr0 = ONFI_READ_ID_ADDR;
636 params.cfg.addr1 = 0;
637
638 /* Lock the pipe and execute the cmd. */
639 onfi_ret = onfi_probe_cmd_exec(&params, onfi_str, ONFI_READ_ID_BUFFER_SIZE);
640 if (onfi_ret)
641 {
642 dprintf(CRITICAL, "ONFI Read id cmd failed\n");
643 goto qpic_nand_onfi_probe_err;
644 }
645
646 /* Write back vld and cmd and unlock the pipe. */
647 qpic_nand_onfi_probe_cleanup(vld, dev_cmd1);
648
649 /* Check for onfi string */
650 id = (uint32_t*)onfi_str;
651 if (*id != ONFI_SIGNATURE)
652 {
653 dprintf(CRITICAL, "Not an ONFI device\n");
654 /* Not an onfi device. Return error. */
655 onfi_ret = NANDC_RESULT_DEV_NOT_SUPPORTED;
656 goto qpic_nand_onfi_probe_err;
657 }
658
659 dprintf(INFO, "ONFI device found\n");
660 /* Now read the param page */
661 /* Initialize the config */
662 params.cfg.cfg0 = NAND_CFG0_RAW_ONFI_PARAM_PAGE;
663 params.cfg.cfg1 = NAND_CFG1_RAW_ONFI_PARAM_PAGE;
664
665 /* Initialize the cmd and vld */
666 params.dev_cmd1 = (dev_cmd1 & 0xFFFFFF00) | ONFI_READ_PARAM_PAGE_CMD;
667 params.vld = vld & 0xFFFFFFFE;
668
669 /* Initialize the address
670 * addr1 is not used bcos of the cfg.
671 */
672 params.cfg.addr0 = ONFI_READ_PARAM_PAGE_ADDR;
673 params.cfg.addr1 = 0;
674
675 /* Lock the pipe and execute the cmd. */
676 onfi_ret = onfi_probe_cmd_exec(&params, buffer, ONFI_READ_PARAM_PAGE_BUFFER_SIZE);
677 if (onfi_ret)
678 {
679 dprintf(CRITICAL, "ONFI Read param page failed\n");
680 goto qpic_nand_onfi_probe_err;
681 }
682
683 /* Write back vld and cmd and unlock the pipe. */
684 qpic_nand_onfi_probe_cleanup(vld, dev_cmd1);
685
686 /* Verify the integrity of the returned page */
687 param_page = (struct onfi_param_page*)buffer;
688
689 /* TODO: Add CRC check to validate the param page. */
690
691 /* Save the parameter values */
692 onfi_ret = qpic_nand_onfi_save_params(param_page, flash);
693
694qpic_nand_onfi_probe_err:
695 if (onfi_ret)
696 dprintf(CRITICAL, "ONFI probe failed\n");
697
698 free(buffer);
699
700 return onfi_ret;
701}
702
703/* Enquues a desc for a flash cmd with NWD flag set:
704 * cfg: Defines the configuration for the flash cmd.
705 * start: Address where the command elements are added.
706 *
707 * Returns the address where the next cmd element can be added.
708 */
709struct cmd_element*
710qpic_nand_add_cmd_ce(struct cfg_params *cfg,
711 struct cmd_element *start)
712{
713 struct cmd_element *cmd_list_ptr;
714
715 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(cfg, start);
716
717 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)cfg->cmd, CE_WRITE_TYPE);
718 cmd_list_ptr++;
719
720 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)cfg->exec, CE_WRITE_TYPE);
721 cmd_list_ptr++;
722
723 return cmd_list_ptr;
724}
725
726/* Reads nand_flash_status and resets nand_flash_status and nand_read_status */
727struct cmd_element*
728qpic_nand_add_read_n_reset_status_ce(struct cmd_element *start,
729 uint32_t *flash_status_read,
730 uint32_t read_status)
731{
732 struct cmd_element *cmd_list_ptr = start;
733 uint32_t flash_status_reset;
734 uint32_t read_status_reset;
735
736 /* Read and reset the status registers. */
737 flash_status_reset = NAND_FLASH_STATUS_RESET;
738 read_status_reset = NAND_READ_STATUS_RESET;
739
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700740 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 -0700741 cmd_list_ptr++;
742 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_STATUS, (uint32_t)flash_status_reset, CE_WRITE_TYPE);
743 cmd_list_ptr++;
744
745 if (read_status)
746 {
747 bam_add_cmd_element(cmd_list_ptr, NAND_READ_STATUS, (uint32_t)read_status_reset, CE_WRITE_TYPE);
748 cmd_list_ptr++;
749 }
750
751 return cmd_list_ptr;
752}
753
754struct cmd_element*
755qpic_nand_add_isbad_cmd_ce(struct cfg_params *cfg,
756 struct cmd_element *start)
757{
758 struct cmd_element *cmd_list_ptr = start;
759
760 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG, (uint32_t)cfg->ecc_cfg, CE_WRITE_TYPE);
761 cmd_list_ptr++;
762
763 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(0), (uint32_t)cfg->addr_loc_0, CE_WRITE_TYPE);
764 cmd_list_ptr++;
765
766 cmd_list_ptr = qpic_nand_add_cmd_ce(cfg, cmd_list_ptr);
767
768 return cmd_list_ptr;
769}
770
771static int
772qpic_nand_block_isbad_exec(struct cfg_params *params,
773 uint8_t *bad_block)
774{
775
776 struct cmd_element *cmd_list_ptr = ce_array;
777 struct cmd_element *cmd_list_ptr_start = ce_array;
778 uint8_t desc_flags = BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG
779 | BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG;
780 int num_desc = 0;
781 uint32_t status = 0;
782 int nand_ret = NANDC_RESULT_SUCCESS;
783
784 cmd_list_ptr = qpic_nand_add_isbad_cmd_ce(params, cmd_list_ptr);
785
786 /* Enqueue the desc for the above commands */
787 bam_add_one_desc(&bam,
788 CMD_PIPE_INDEX,
789 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700790 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700791 desc_flags);
792
793 num_desc++;
794
795 /* Add Data desc */
796 bam_add_desc(&bam,
797 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700798 (unsigned char *)PA((addr_t)bad_block),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700799 4,
800 BAM_DESC_INT_FLAG);
801
802 qpic_nand_wait_for_cmd_exec(num_desc);
803
Deepa Dinamani19530062012-10-03 14:43:05 -0700804 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr);
805
806 nand_ret = qpic_nand_check_status(status);
807
808 /* Dummy read to unlock pipe. */
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700809 status = qpic_nand_read_reg(NAND_FLASH_STATUS, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr);
810
Deepa Dinamani19530062012-10-03 14:43:05 -0700811 if (nand_ret)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700812 return NANDC_RESULT_FAILURE;
813
814 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
815
816 return nand_ret;
817}
818
819static int
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800820qpic_nand_block_isbad(unsigned page)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700821{
822 unsigned cwperpage;
823 struct cfg_params params;
824 uint8_t bad_block[4];
825 unsigned nand_ret = NANDC_RESULT_SUCCESS;
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800826 uint32_t blk = page / flash.num_pages_per_blk;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700827
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800828 if (bbtbl[blk] == NAND_BAD_BLK_VALUE_IS_GOOD)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700829 return NANDC_RESULT_SUCCESS;
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800830 else if (bbtbl[blk] == NAND_BAD_BLK_VALUE_IS_BAD)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700831 return NANDC_RESULT_BAD_BLOCK;
832 else
833 {
834 /* Read the bad block value from the flash.
835 * Bad block value is stored in the first page of the block.
836 */
837 /* Read the first page in the block. */
838 cwperpage = flash.cws_per_page;
839
840 /* Read page cmd */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800841 params.cmd = NAND_CMD_PAGE_READ_ECC;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700842 /* Clear the CW per page bits */
843 params.cfg0 = cfg0_raw & ~(7U << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT);
844 params.cfg1 = cfg1_raw;
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800845 /* addr0 - Write column addr + few bits in row addr upto 32 bits. */
846 params.addr0 = (page << 16) | (USER_DATA_BYTES_PER_CW * cwperpage);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700847
848 /* addr1 - Write rest of row addr.
849 * This will be all 0s.
850 */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800851 params.addr1 = (page >> 16) & 0xff;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700852 params.addr_loc_0 = NAND_RD_LOC_OFFSET(0);
853 params.addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
854 params.addr_loc_0 |= NAND_RD_LOC_SIZE(4); /* Read 4 bytes */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800855 params.ecc_cfg = ecc_bch_cfg | 0x1; /* Disable ECC */
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700856 params.exec = 1;
857
858 if (qpic_nand_block_isbad_exec(&params, bad_block))
859 {
860 dprintf(CRITICAL,
861 "Could not read bad block value\n");
862 return NANDC_RESULT_FAILURE;
863 }
864
865 if (flash.widebus)
866 {
867 if (bad_block[0] != 0xFF && bad_block[1] != 0xFF)
868 {
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800869 bbtbl[blk] = NAND_BAD_BLK_VALUE_IS_BAD;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700870 nand_ret = NANDC_RESULT_BAD_BLOCK;
871 }
872 }
873 else if (bad_block[0] != 0xFF)
874 {
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800875 bbtbl[blk] = NAND_BAD_BLK_VALUE_IS_BAD;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700876 nand_ret = NANDC_RESULT_BAD_BLOCK;
877 }
878 else
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800879 bbtbl[blk] = NAND_BAD_BLK_VALUE_IS_GOOD;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700880
881 return nand_ret;
882 }
883}
884
885/* Function to erase a block on the nand.
886 * page: Starting page address for the block.
887 */
888static int
889qpic_nand_blk_erase(uint32_t page)
890{
891 struct cfg_params cfg;
892 struct cmd_element *cmd_list_ptr = ce_array;
893 struct cmd_element *cmd_list_ptr_start = ce_array;
894 uint32_t status;
895 int num_desc = 0;
896 uint32_t blk_addr = page / flash.num_pages_per_blk;
Deepa Dinamani19530062012-10-03 14:43:05 -0700897 int nand_ret;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700898
899 /* Erase only if the block is not bad */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800900 if (qpic_nand_block_isbad(page))
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700901 {
902 dprintf(CRITICAL,
903 "NAND Erase error: Block address belongs to bad block: %d\n",
904 blk_addr);
905 return NANDC_RESULT_FAILURE;
906 }
907
908 /* Fill in params for the erase flash cmd */
909 cfg.addr0 = page;
910 cfg.addr1 = 0;
911 /* Clear CW_PER_PAGE in cfg0 */
912 cfg.cfg0 = cfg0 & ~(7U << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT);
913 cfg.cfg1 = cfg1;
914 cfg.cmd = NAND_CMD_BLOCK_ERASE;
915 cfg.exec = 1;
916
917 cmd_list_ptr = qpic_nand_add_cmd_ce(&cfg, cmd_list_ptr);
918
919 /* Enqueue the desc for the above commands */
920 bam_add_one_desc(&bam,
921 CMD_PIPE_INDEX,
922 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700923 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamani2467bbb2012-10-02 13:59:58 -0700924 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG | BAM_DESC_LOCK_FLAG);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700925
926 cmd_list_ptr_start = cmd_list_ptr;
927 num_desc++;
928
929 qpic_nand_wait_for_cmd_exec(num_desc);
930
931 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0, cmd_list_ptr);
932
933 cmd_list_ptr++;
934 cmd_list_ptr_start = cmd_list_ptr;
935
936 /* QPIC controller automatically sends
937 * GET_STATUS cmd to the nand card because
938 * of the configuration programmed.
939 * Read the result of GET_STATUS cmd.
940 */
941 cmd_list_ptr = qpic_nand_add_read_n_reset_status_ce(cmd_list_ptr, &status, 1);
942
943 /* Enqueue the desc for the above commands */
944 bam_add_one_desc(&bam,
945 CMD_PIPE_INDEX,
946 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700947 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamani19530062012-10-03 14:43:05 -0700948 BAM_DESC_INT_FLAG | BAM_DESC_CMD_FLAG) ;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700949
950 num_desc = 1;
951 qpic_nand_wait_for_cmd_exec(num_desc);
952
Deepa Dinamani19530062012-10-03 14:43:05 -0700953 status = qpic_nand_check_status(status);
954
955 /* Dummy read to unlock pipe. */
956 nand_ret = qpic_nand_read_reg(NAND_FLASH_STATUS, BAM_DESC_UNLOCK_FLAG, cmd_list_ptr);
957
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700958 /* Check for status errors*/
Deepa Dinamani19530062012-10-03 14:43:05 -0700959 if (status)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700960 {
961 dprintf(CRITICAL,
962 "NAND Erase error: Block address belongs to bad block: %d\n",
963 blk_addr);
964 return NANDC_RESULT_FAILURE;
965 }
966
967 /* Check for PROG_ERASE_OP_RESULT bit for the result of erase operation. */
Deepa Dinamani2467bbb2012-10-02 13:59:58 -0700968 if (!(status & PROG_ERASE_OP_RESULT))
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700969 return NANDC_RESULT_SUCCESS;
970
971 return NANDC_RESULT_FAILURE;
972}
973
974/* Return num of desc added. */
Deepa Dinamani19530062012-10-03 14:43:05 -0700975static void
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700976qpic_nand_add_wr_page_cws_cmd_desc(struct cfg_params *cfg,
977 uint32_t status[],
978 enum nand_cfg_value cfg_mode)
979{
980 struct cmd_element *cmd_list_ptr = ce_array;
981 struct cmd_element *cmd_list_ptr_start = ce_array;
982 uint32_t ecc;
983 int num_desc = 0;
984 int int_flag = 0;
985
986 if (cfg_mode == NAND_CFG)
987 ecc = ecc_bch_cfg;
988 else
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800989 ecc = ecc_bch_cfg | 0x1; /* Disable ECC */
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700990
991 /* Add ECC configuration */
992 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG,
993 (uint32_t)ecc, CE_WRITE_TYPE);
994 cmd_list_ptr++;
995 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(cfg, cmd_list_ptr);
996
997 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD,
998 (uint32_t)cfg->cmd, CE_WRITE_TYPE);
999 cmd_list_ptr++;
1000
1001 /* Enqueue the desc for the above commands */
1002 bam_add_one_desc(&bam,
1003 CMD_PIPE_INDEX,
1004 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001005 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001006 BAM_DESC_CMD_FLAG | BAM_DESC_LOCK_FLAG);
1007
1008 num_desc++;
1009
1010 /* Add CE for all the CWs */
1011 for (unsigned i = 0; i < flash.cws_per_page; i++)
1012 {
1013 cmd_list_ptr_start = cmd_list_ptr;
Deepa Dinamani19530062012-10-03 14:43:05 -07001014 int_flag = BAM_DESC_INT_FLAG;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001015
1016 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)cfg->exec, CE_WRITE_TYPE);
1017 cmd_list_ptr++;
1018
1019 /* Enqueue the desc for the above commands */
1020 bam_add_one_desc(&bam,
1021 CMD_PIPE_INDEX,
1022 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001023 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001024 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG);
1025
1026 num_desc++;
1027 cmd_list_ptr_start = cmd_list_ptr;
1028
1029 /* Set interrupt bit only for the last CW */
1030 if (i == flash.cws_per_page - 1)
1031 {
1032 cmd_list_ptr = qpic_nand_add_read_n_reset_status_ce(cmd_list_ptr,
1033 &status[i],
1034 1);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001035 }
1036 else
1037 cmd_list_ptr = qpic_nand_add_read_n_reset_status_ce(cmd_list_ptr,
1038 &status[i],
1039 0);
1040
1041 /* Enqueue the desc for the above commands */
1042 bam_add_one_desc(&bam,
1043 CMD_PIPE_INDEX,
1044 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001045 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001046 int_flag | BAM_DESC_CMD_FLAG);
1047 num_desc++;
Deepa Dinamani19530062012-10-03 14:43:05 -07001048
1049 qpic_nand_wait_for_cmd_exec(num_desc);
1050
1051 status[i] = qpic_nand_check_status(status[i]);
1052
1053 num_desc = 0;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001054 }
Deepa Dinamani19530062012-10-03 14:43:05 -07001055 return;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001056}
1057
1058void
1059qpic_add_wr_page_cws_data_desc(const void *buffer,
1060 enum nand_cfg_value cfg_mode,
1061 const void *spareaddr)
1062{
1063 int len;
1064 int flags;
1065 uint32_t start;
1066 unsigned num_desc = 0;
1067
1068 for( unsigned i = 0; i < flash.cws_per_page; i++)
1069 {
1070 flags = 0;
1071
1072 /* Set the interrupt flag on the last CW write for the page. */
1073 if( i == flash.cws_per_page - 1)
1074 flags |= BAM_DESC_INT_FLAG;
1075
1076 if (cfg_mode != NAND_CFG_RAW)
1077 {
1078 start = (uint32_t)buffer + i * DATA_BYTES_IN_IMG_PER_CW;
1079
1080 if (i < (flash.cws_per_page - 1))
1081 {
1082 len = DATA_BYTES_IN_IMG_PER_CW;
1083 flags |= BAM_DESC_EOT_FLAG;
1084 }
1085 else
1086 {
1087 /* Allow space for spare bytes in the last page */
1088 len = USER_DATA_BYTES_PER_CW - ((flash.cws_per_page - 1) << 2);
1089 flags = 0;
1090 }
1091 }
1092 else
1093 {
1094 start = (uint32_t)buffer;
1095 len = flash.cw_size;
1096 flags |= BAM_DESC_EOT_FLAG;
1097 }
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001098 bam_add_one_desc(&bam, DATA_CONSUMER_PIPE_INDEX, (unsigned char*)PA(start), len, flags);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001099 num_desc++;
1100
1101 if ((i == (flash.cws_per_page - 1)) && (cfg_mode == NAND_CFG))
1102 {
1103 /* write extra data */
1104 start = (uint32_t)spareaddr;
1105 len = (flash.cws_per_page << 2);
1106 flags = BAM_DESC_EOT_FLAG | BAM_DESC_INT_FLAG;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001107 bam_add_one_desc(&bam, DATA_CONSUMER_PIPE_INDEX, (unsigned char*)PA(start), len, flags);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001108 num_desc++;
1109 }
1110 }
1111
1112 bam_sys_gen_event(&bam, DATA_CONSUMER_PIPE_INDEX, num_desc);
1113}
1114
1115static nand_result_t
1116qpic_nand_write_page(uint32_t pg_addr,
1117 enum nand_cfg_value cfg_mode,
1118 const void* buffer,
1119 const void* spareaddr)
1120{
1121 struct cfg_params cfg;
Deepa Dinamani16663a62013-02-07 16:25:59 -08001122 uint32_t status[QPIC_NAND_MAX_CWS_IN_PAGE];
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001123 int nand_ret = NANDC_RESULT_SUCCESS;
1124
1125 if (cfg_mode == NAND_CFG_RAW)
1126 {
1127 cfg.cfg0 = cfg0_raw;
1128 cfg.cfg1 = cfg1_raw;
1129 }
1130 else
1131 {
1132 cfg.cfg0 = cfg0;
1133 cfg.cfg1 = cfg1;
1134 }
1135
1136 cfg.cmd = NAND_CMD_PRG_PAGE;
1137 cfg.exec = 1;
1138
1139 cfg.addr0 = pg_addr << 16;
1140 cfg.addr1 = (pg_addr >> 16) & 0xff;
1141
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001142 qpic_add_wr_page_cws_data_desc(buffer, cfg_mode, spareaddr);
1143
Deepa Dinamani19530062012-10-03 14:43:05 -07001144 qpic_nand_add_wr_page_cws_cmd_desc(&cfg, status, cfg_mode);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001145
1146 /* Check for errors */
1147 for(unsigned i = 0; i < flash.cws_per_page; i++)
1148 {
1149 nand_ret = qpic_nand_check_status(status[i]);
1150 if (nand_ret)
1151 {
1152 dprintf(CRITICAL,
1153 "Failed to write CW %d for page: %d\n",
1154 i, pg_addr);
1155 break;
1156 }
1157 }
1158
1159 /* Wait for data to be available */
1160 qpic_nand_wait_for_data(DATA_CONSUMER_PIPE_INDEX);
1161
1162 return nand_ret;
1163}
1164
1165static int
1166qpic_nand_mark_badblock(uint32_t page)
1167{
1168 char empty_buf[NAND_CW_SIZE_8_BIT_ECC];
1169
1170 memset(empty_buf, 0, NAND_CW_SIZE_8_BIT_ECC);
1171
1172 /* Going to first page of the block */
1173 if (page & flash.num_pages_per_blk_mask)
1174 page = page - (page & flash.num_pages_per_blk_mask);
1175
1176 return qpic_nand_write_page(page, NAND_CFG_RAW, empty_buf, 0);
1177}
1178
1179static void
1180qpic_nand_non_onfi_probe(struct flash_info *flash)
1181{
1182 int dev_found = 0;
1183 unsigned index;
1184 uint32_t ecc_bits;
1185
1186 /* Read the nand id. */
1187 qpic_nand_fetch_id(flash);
1188
1189 /* Check if we support the device */
Deepa Dinamani649a94a2013-03-07 14:37:31 -08001190 for (index = 0; index < (ARRAY_SIZE(supported_flash)); index++)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001191 {
1192 if ((flash->id & supported_flash[index].mask) ==
1193 (supported_flash[index].flash_id & (supported_flash[index].mask)))
1194 {
1195 dev_found = 1;
1196 break;
1197 }
1198 }
1199
1200 if (dev_found)
1201 {
1202 flash->page_size = supported_flash[index].pagesize;
1203 flash->block_size = supported_flash[index].blksize;
1204 flash->spare_size = supported_flash[index].oobsize;
1205 ecc_bits = supported_flash[index].ecc_8_bits;
1206
1207 /* Make sure that the block size and page size are defined. */
1208 ASSERT(flash->block_size);
1209 ASSERT(flash->page_size);
1210
1211 flash->num_blocks = supported_flash[index].density;
1212 flash->num_blocks /= (flash->block_size);
1213 flash->num_pages_per_blk = flash->block_size / flash->page_size;
1214 flash->num_pages_per_blk_mask = flash->num_pages_per_blk - 1;
1215
1216 /* Look for 8bit BCH ECC Nand, TODO: ECC Correctability >= 8 */
1217 if (ecc_bits)
1218 flash->ecc_width = NAND_WITH_8_BIT_ECC;
1219 else
1220 flash->ecc_width = NAND_WITH_4_BIT_ECC;
1221
1222 flash->density = supported_flash[index].density;
1223 flash->widebus = supported_flash[index].widebus;
1224
1225 return;
1226 }
1227
1228 /* Flash device is not supported, print flash device info and halt */
1229 if (dev_found == 0)
1230 {
1231 dprintf(CRITICAL, "NAND device is not supported: nandid: 0x%x"
1232 "maker=0x%02x device=0x%02x\n",
1233 flash->id,
1234 flash->vendor,
1235 flash->device);
1236 ASSERT(0);
1237 }
1238
1239 dprintf(INFO, "nandid: 0x%x maker=0x%02x device=0x%02x page_size=%d\n",
1240 flash->id,
1241 flash->vendor,
1242 flash->device,
1243 flash->page_size);
1244
1245 dprintf(INFO, "spare_size=%d block_size=%d num_blocks=%d\n",
1246 flash->spare_size,
1247 flash->block_size,
1248 flash->num_blocks);
1249}
1250
1251void
1252qpic_nand_init(struct qpic_nand_init_config *config)
1253{
1254 uint32_t i;
1255 int nand_ret;
1256
1257 nand_base = config->nand_base;
1258
Deepa Dinamanie9ded132012-11-27 15:03:38 -08001259 qpic_bam_init(config);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001260
Deepa Dinamani649a94a2013-03-07 14:37:31 -08001261 qpic_nand_non_onfi_probe(&flash);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001262
1263 /* Save the RAW and read/write configs */
1264 qpic_nand_save_config(&flash);
1265
1266 flash_spare_bytes = (unsigned char *)malloc(flash.spare_size);
1267
1268 if (flash_spare_bytes == NULL)
1269 {
1270 dprintf(CRITICAL, "Failed to allocate memory for spare bytes\n");
1271 return;
1272 }
1273
1274 /* Create a bad block table */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001275 bbtbl = (uint8_t *) malloc(sizeof(uint8_t) * flash.num_blocks);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001276
1277 if (bbtbl == NULL)
1278 {
1279 dprintf(CRITICAL, "Failed to allocate memory for bad block table\n");
1280 return;
1281 }
1282
1283 for (i = 0; i < flash.num_blocks; i++)
1284 bbtbl[i] = NAND_BAD_BLK_VALUE_NOT_READ;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001285
1286 /* Set aside contiguous memory for reads/writes.
1287 * This is needed as the BAM transfers only work with
1288 * physically contiguous buffers.
1289 * We will copy any data to be written/ to be read from
1290 * nand to this buffer and this buffer will be submitted to BAM.
1291 */
1292 rdwr_buf = (uint8_t*) malloc(flash.page_size + flash.spare_size);
1293
1294 if (rdwr_buf == NULL)
1295 {
1296 dprintf(CRITICAL, "Failed to allocate memory for page reads or writes\n");
1297 return;
1298 }
1299
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001300}
1301
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001302unsigned
1303flash_page_size(void)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001304{
1305 return flash.page_size;
1306}
1307
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001308unsigned
1309flash_block_size(void)
1310{
1311 return flash.block_size;
1312}
1313
Deepa Dinamani8e6b2432012-10-17 17:12:44 -07001314unsigned
1315flash_num_blocks(void)
1316{
1317 return flash.num_blocks;
1318}
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001319
1320struct ptable *
1321flash_get_ptable(void)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001322{
1323 return flash_ptable;
1324}
1325
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001326void
Deepa Dinamani87feab82012-10-04 14:28:05 -07001327qpic_nand_uninit()
1328{
1329 bam_pipe_reset(&bam, DATA_PRODUCER_PIPE_INDEX);
1330 bam_pipe_reset(&bam, DATA_CONSUMER_PIPE_INDEX);
1331 bam_pipe_reset(&bam, CMD_PIPE_INDEX);
1332
1333}
1334void
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001335flash_set_ptable(struct ptable *new_ptable)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001336{
1337 ASSERT(flash_ptable == NULL && new_ptable != NULL);
1338 flash_ptable = new_ptable;
1339}
1340
1341/* Note: No support for raw reads. */
1342static int
1343qpic_nand_read_page(uint32_t page, unsigned char* buffer, unsigned char* spareaddr)
1344{
1345 struct cfg_params params;
1346 uint32_t ecc;
Deepa Dinamani16663a62013-02-07 16:25:59 -08001347 uint32_t flash_sts[QPIC_NAND_MAX_CWS_IN_PAGE];
1348 uint32_t buffer_sts[QPIC_NAND_MAX_CWS_IN_PAGE];
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001349 uint32_t addr_loc_0;
1350 uint32_t addr_loc_1;
1351 struct cmd_element *cmd_list_ptr = ce_array;
1352 struct cmd_element *cmd_list_ptr_start = ce_array;
1353 uint32_t num_cmd_desc = 0;
1354 uint32_t num_data_desc = 0;
1355 uint32_t status;
1356 uint32_t i;
1357 int nand_ret = NANDC_RESULT_SUCCESS;
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001358 uint8_t flags = 0;
1359 uint32_t *cmd_list_temp = NULL;
1360
1361 uint32_t temp_status = 0;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001362 /* UD bytes in last CW is 512 - cws_per_page *4.
1363 * Since each of the CW read earlier reads 4 spare bytes.
1364 */
1365 uint16_t ud_bytes_in_last_cw = USER_DATA_BYTES_PER_CW - ((flash.cws_per_page - 1) << 2);
1366 uint16_t oob_bytes = DATA_BYTES_IN_IMG_PER_CW - ud_bytes_in_last_cw;
1367
1368 params.addr0 = page << 16;
1369 params.addr1 = (page >> 16) & 0xff;
1370 params.cfg0 = cfg0;
1371 params.cfg1 = cfg1;
1372 params.cmd = NAND_CMD_PAGE_READ_ALL;
1373 params.exec = 1;
1374 ecc = ecc_bch_cfg;
1375
1376 /* Read all the Data bytes in the first 3 CWs. */
1377 addr_loc_0 = NAND_RD_LOC_OFFSET(0);
1378 addr_loc_0 |= NAND_RD_LOC_SIZE(DATA_BYTES_IN_IMG_PER_CW);
1379 addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
1380
1381
1382 addr_loc_1 = NAND_RD_LOC_OFFSET(ud_bytes_in_last_cw);
1383 addr_loc_1 |= NAND_RD_LOC_SIZE(oob_bytes);
1384 addr_loc_1 |= NAND_RD_LOC_LAST_BIT(1);
1385
Deepa Dinamanidc1381e2012-11-15 14:53:24 -08001386 status = qpic_nand_block_isbad(page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001387
1388 if (status)
1389 return status;
1390
sundarajan srinivasan7ced6482013-03-21 16:01:18 -07001391 /* Reset and Configure erased CW/page detection controller */
1392 qpic_nand_erased_status_reset(ce_array, BAM_DESC_LOCK_FLAG);
1393
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001394 /* Queue up the command and data descriptors for all the codewords in a page
1395 * and do a single bam transfer at the end.*/
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001396 for (i = 0; i < flash.cws_per_page; i++)
1397 {
1398 num_cmd_desc = 0;
1399 num_data_desc = 0;
1400
1401 if (i == 0)
1402 {
1403 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(&params, cmd_list_ptr);
1404
1405 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG,(uint32_t)ecc, CE_WRITE_TYPE);
1406 cmd_list_ptr++;
1407 }
1408 else
1409 cmd_list_ptr_start = cmd_list_ptr;
1410
1411 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)params.cmd, CE_WRITE_TYPE);
1412 cmd_list_ptr++;
1413
1414 if (i == flash.cws_per_page - 1)
1415 {
1416 addr_loc_0 = NAND_RD_LOC_OFFSET(0);
1417 addr_loc_0 |= NAND_RD_LOC_SIZE(ud_bytes_in_last_cw);
1418 addr_loc_0 |= NAND_RD_LOC_LAST_BIT(0);
1419
1420 /* Write addr loc 1 only for the last CW. */
1421 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(1), (uint32_t)addr_loc_1, CE_WRITE_TYPE);
1422 cmd_list_ptr++;
1423
1424 /* Add Data desc */
1425 bam_add_one_desc(&bam,
1426 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001427 (unsigned char *)PA((addr_t)buffer),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001428 ud_bytes_in_last_cw,
1429 0);
1430 num_data_desc++;
1431
1432 bam_add_one_desc(&bam,
1433 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001434 (unsigned char *)PA((addr_t)spareaddr),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001435 oob_bytes,
1436 BAM_DESC_INT_FLAG);
1437 num_data_desc++;
1438
1439 bam_sys_gen_event(&bam, DATA_PRODUCER_PIPE_INDEX, num_data_desc);
1440 }
1441 else
1442 {
1443 /* Add Data desc */
1444 bam_add_one_desc(&bam,
1445 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001446 (unsigned char *)PA((addr_t)buffer),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001447 DATA_BYTES_IN_IMG_PER_CW,
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001448 0);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001449 num_data_desc++;
1450 bam_sys_gen_event(&bam, DATA_PRODUCER_PIPE_INDEX, num_data_desc);
1451 }
1452
1453 /* Write addr loc 0. */
1454 bam_add_cmd_element(cmd_list_ptr,
1455 NAND_READ_LOCATION_n(0),
1456 (uint32_t)addr_loc_0,
1457 CE_WRITE_TYPE);
1458
1459 cmd_list_ptr++;
1460 bam_add_cmd_element(cmd_list_ptr,
1461 NAND_EXEC_CMD,
1462 (uint32_t)params.exec,
1463 CE_WRITE_TYPE);
1464 cmd_list_ptr++;
1465
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001466 /* Enqueue the desc for the above commands */
1467 bam_add_one_desc(&bam,
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001468 CMD_PIPE_INDEX,
1469 (unsigned char*)cmd_list_ptr_start,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001470 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001471 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG);
1472 num_cmd_desc++;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001473
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001474 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_STATUS, (uint32_t)PA((addr_t)&(flash_sts[i])), CE_READ_TYPE);
1475
1476 cmd_list_temp = cmd_list_ptr;
1477
1478 cmd_list_ptr++;
1479
1480 bam_add_cmd_element(cmd_list_ptr, NAND_BUFFER_STATUS, (uint32_t)PA((addr_t)&(buffer_sts[i])), CE_READ_TYPE);
1481 cmd_list_ptr++;
1482
1483 if (i == flash.cws_per_page - 1)
1484 {
1485 flags = BAM_DESC_CMD_FLAG | BAM_DESC_UNLOCK_FLAG;
1486 }
1487 else
1488 flags = BAM_DESC_CMD_FLAG;
1489
1490 /* Enqueue the desc for the above command */
1491 bam_add_one_desc(&bam,
1492 CMD_PIPE_INDEX,
1493 (unsigned char*)PA((addr_t)cmd_list_temp),
1494 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_temp),
1495 flags);
1496 num_cmd_desc++;
1497
1498 buffer += DATA_BYTES_IN_IMG_PER_CW;
1499
1500 /* Notify BAM HW about the newly added descriptors */
1501 bam_sys_gen_event(&bam, CMD_PIPE_INDEX, num_cmd_desc);
1502 }
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001503
1504 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
1505
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001506 /* Check status */
1507 for (i = 0; i < flash.cws_per_page ; i ++)
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001508 {
1509 flash_sts[i] = qpic_nand_check_status(flash_sts[i]);
Deepa Dinamani19530062012-10-03 14:43:05 -07001510 if (flash_sts[i])
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001511 {
1512 nand_ret = NANDC_RESULT_BAD_PAGE;
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001513 dprintf(CRITICAL, "NAND page read failed. page: %x status %x\n", page, flash_sts[i]);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001514 goto qpic_nand_read_page_error;
1515 }
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001516 }
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001517qpic_nand_read_page_error:
1518return nand_ret;
1519}
1520
1521/* Function to read a flash partition.
1522 * ptn : Partition to read.
1523 * extra_per_page : Spare data to be read.
1524 * offset : Num of bytes offset into the partition.
1525 * data : Buffer to read the data into.
1526 * bytes : Num of bytes to be read.
1527 */
1528 /* TODO: call this func read_partition. */
1529int
1530flash_read_ext(struct ptentry *ptn,
1531 unsigned extra_per_page,
1532 unsigned offset,
1533 void *data,
1534 unsigned bytes)
1535{
1536 uint32_t page =
1537 (ptn->start * flash.num_pages_per_blk) + (offset / flash.page_size);
1538 uint32_t lastpage = (ptn->start + ptn->length) * flash.num_pages_per_blk;
1539 uint32_t count =
1540 (bytes + flash.page_size - 1 + extra_per_page) / (flash.page_size +
1541 extra_per_page);
1542 uint32_t *spare = (unsigned *)flash_spare_bytes;
1543 uint32_t errors = 0;
1544 unsigned char *image = data;
1545 int result = 0;
1546 uint32_t current_block =
1547 (page - (page & flash.num_pages_per_blk_mask)) / flash.num_pages_per_blk;
1548 uint32_t start_block = ptn->start;
1549 uint32_t start_block_count = 0;
1550 uint32_t isbad = 0;
Deepa Dinamani2f7006c2013-08-19 11:59:38 -07001551 uint32_t current_page;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001552
1553 /* Verify first byte is at page boundary. */
1554 if (offset & (flash.page_size - 1))
1555 {
1556 dprintf(CRITICAL, "Read request start not at page boundary: %d\n",
1557 offset);
1558 return NANDC_RESULT_PARAM_INVALID;
1559 }
1560
Deepa Dinamani2f7006c2013-08-19 11:59:38 -07001561 current_page = start_block * flash.num_pages_per_blk;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001562 /* Adjust page offset based on number of bad blocks from start to current page */
1563 if (start_block < current_block)
1564 {
1565 start_block_count = (current_block - start_block);
1566 while (start_block_count
1567 && (start_block < (ptn->start + ptn->length)))
1568 {
Deepa Dinamani2f7006c2013-08-19 11:59:38 -07001569 isbad = qpic_nand_block_isbad(current_page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001570 if (isbad)
1571 page += flash.num_pages_per_blk;
1572 else
1573 start_block_count--;
1574 start_block++;
Deepa Dinamani2f7006c2013-08-19 11:59:38 -07001575 current_page += flash.num_pages_per_blk;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001576 }
1577 }
1578
1579 while ((page < lastpage) && !start_block_count)
1580 {
1581 if (count == 0)
1582 {
Deepa Dinamani52aca8d2013-02-05 11:41:41 -08001583 dprintf(SPEW, "flash_read_image: success (%d errors)\n",
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001584 errors);
1585 return NANDC_RESULT_SUCCESS;
1586 }
1587
Sundarajan Srinivasan29d927c2014-02-10 14:11:24 -08001588#if CONTIGUOUS_MEMORY
1589 result = qpic_nand_read_page(page, image, (unsigned char *) spare);
1590#else
1591 result = qpic_nand_read_page(page, rdwr_buf, (unsigned char *) spare);
1592#endif
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001593 if (result == NANDC_RESULT_BAD_PAGE)
1594 {
1595 /* bad page, go to next page. */
1596 page++;
1597 errors++;
1598 continue;
1599 }
1600 else if (result == NANDC_RESULT_BAD_BLOCK)
1601 {
1602 /* bad block, go to next block same offset. */
1603 page += flash.num_pages_per_blk;
1604 errors++;
1605 continue;
1606 }
1607
Sundarajan Srinivasan29d927c2014-02-10 14:11:24 -08001608#ifndef CONTIGUOUS_MEMORY
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001609 /* Copy the read page into correct location. */
1610 memcpy(image, rdwr_buf, flash.page_size);
Sundarajan Srinivasan29d927c2014-02-10 14:11:24 -08001611#endif
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001612 page++;
1613 image += flash.page_size;
1614 /* Copy spare bytes to image */
Sundarajan Srinivasan29d927c2014-02-10 14:11:24 -08001615 if(extra_per_page)
1616 {
1617 memcpy(image, spare, extra_per_page);
1618 image += extra_per_page;
1619 }
1620
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001621 count -= 1;
1622 }
1623
1624 /* could not find enough valid pages before we hit the end */
1625 dprintf(CRITICAL, "flash_read_image: failed (%d errors)\n", errors);
1626 return NANDC_RESULT_FAILURE;
1627}
1628
1629int
1630flash_erase(struct ptentry *ptn)
1631{
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001632 int ret = 0;
1633
1634 ret = qpic_nand_blk_erase(ptn->start * flash.num_pages_per_blk);
1635
1636 if (ret)
1637 dprintf(CRITICAL, "Erase operation failed \n");
1638
1639 return ret;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001640}
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001641
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001642int
1643flash_ecc_bch_enabled()
1644{
1645 return (flash.ecc_width == NAND_WITH_4_BIT_ECC)? 0 : 1;
1646}
1647
1648int
1649flash_write(struct ptentry *ptn,
Deepa Dinamanic13d5942013-04-30 15:48:53 -07001650 unsigned write_extra_bytes,
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001651 const void *data,
1652 unsigned bytes)
1653{
1654 uint32_t page = ptn->start * flash.num_pages_per_blk;
1655 uint32_t lastpage = (ptn->start + ptn->length) * flash.num_pages_per_blk;
1656 uint32_t *spare = (unsigned *)flash_spare_bytes;
1657 const unsigned char *image = data;
Deepa Dinamanic13d5942013-04-30 15:48:53 -07001658 uint32_t wsize;
Sundarajan Srinivasan223d7002014-02-11 19:48:11 -08001659 uint32_t spare_byte_count = 0;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001660 int r;
1661
Sundarajan Srinivasan223d7002014-02-11 19:48:11 -08001662 spare_byte_count = ((flash.cw_size * flash.cws_per_page)- flash.page_size);
1663
Deepa Dinamanic13d5942013-04-30 15:48:53 -07001664 if(write_extra_bytes)
Sundarajan Srinivasan223d7002014-02-11 19:48:11 -08001665 wsize = flash.page_size + spare_byte_count;
Deepa Dinamanic13d5942013-04-30 15:48:53 -07001666 else
1667 wsize = flash.page_size;
1668
Sundarajan Srinivasan223d7002014-02-11 19:48:11 -08001669 memset(spare, 0xff, (spare_byte_count / flash.cws_per_page));
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001670
1671 while (bytes > 0)
1672 {
1673 if (bytes < wsize)
1674 {
1675 dprintf(CRITICAL,
1676 "flash_write_image: image undersized (%d < %d)\n",
1677 bytes,
1678 wsize);
1679 return -1;
1680 }
1681
1682 if (page >= lastpage)
1683 {
1684 dprintf(CRITICAL, "flash_write_image: out of space\n");
1685 return -1;
1686 }
1687
1688 if ((page & flash.num_pages_per_blk_mask) == 0)
1689 {
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001690 if (qpic_nand_blk_erase(page))
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001691 {
1692 dprintf(INFO,
1693 "flash_write_image: bad block @ %d\n",
1694 page / flash.num_pages_per_blk);
1695
1696 page += flash.num_pages_per_blk;
1697 continue;
1698 }
1699 }
1700
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001701 memcpy(rdwr_buf, image, flash.page_size);
1702
Deepa Dinamanic13d5942013-04-30 15:48:53 -07001703 if (write_extra_bytes)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001704 {
Sundarajan Srinivasan223d7002014-02-11 19:48:11 -08001705 memcpy(rdwr_buf + flash.page_size, image + flash.page_size, spare_byte_count);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001706 r = qpic_nand_write_page(page,
1707 NAND_CFG,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001708 rdwr_buf,
1709 rdwr_buf + flash.page_size);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001710 }
1711 else
1712 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001713 r = qpic_nand_write_page(page, NAND_CFG, rdwr_buf, spare);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001714 }
1715
1716 if (r)
1717 {
1718 dprintf(INFO,
1719 "flash_write_image: write failure @ page %d (src %d)\n",
1720 page,
1721 image - (const unsigned char *)data);
1722
1723 image -= (page & flash.num_pages_per_blk_mask) * wsize;
1724 bytes += (page & flash.num_pages_per_blk_mask) * wsize;
1725 page &= ~flash.num_pages_per_blk_mask;
1726 if (qpic_nand_blk_erase(page))
1727 {
1728 dprintf(INFO,
1729 "flash_write_image: erase failure @ page %d\n",
1730 page);
1731 }
1732
1733 qpic_nand_mark_badblock(page);
1734
1735 dprintf(INFO,
1736 "flash_write_image: restart write @ page %d (src %d)\n",
1737 page, image - (const unsigned char *)data);
1738
1739 page += flash.num_pages_per_blk;
1740 continue;
1741 }
1742 page++;
1743 image += wsize;
1744 bytes -= wsize;
1745 }
1746
1747 /* erase any remaining pages in the partition */
1748 page = (page + flash.num_pages_per_blk_mask) & (~flash.num_pages_per_blk_mask);
1749
1750 while (page < lastpage)
1751 {
1752 if (qpic_nand_blk_erase(page))
1753 {
1754 dprintf(INFO, "flash_write_image: bad block @ %d\n",
1755 page / flash.num_pages_per_blk);
1756 }
1757 page += flash.num_pages_per_blk;
1758 }
1759
1760 dprintf(INFO, "flash_write_image: success\n");
1761 return 0;
1762}
Smita Ghoshf5431c62014-09-18 14:11:14 -07001763
1764uint32_t nand_device_base()
1765{
1766 return nand_base;
1767}