blob: 47d6d3a154668f54b516bda4bc31af032f8c6d2c [file] [log] [blame]
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001/*
2 * Copyright (c) 2008, Google Inc.
3 * All rights reserved.
Mayank Grover20fdd882016-10-25 16:53:30 +05304 * Copyright (c) 2009-2016, 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>
Sridhar Parasuramfb0d9c82015-02-02 15:23:13 -080036#include <bits.h>
Deepa Dinamanie4573be2012-08-03 16:32:29 -070037#include <sys/types.h>
Deepa Dinamani0bf2f442012-10-19 11:41:06 -070038#include <platform.h>
Amol Jadib726c3b2012-09-13 13:51:23 -070039#include <platform/clock.h>
Smita Ghoshf5431c62014-09-18 14:11:14 -070040#include <platform/iomap.h>
Deepa Dinamanie4573be2012-08-03 16:32:29 -070041
42static uint32_t nand_base;
43static struct ptable *flash_ptable;
44static struct flash_info flash;
45static unsigned char *flash_spare_bytes;
46static uint32_t cfg0;
47static uint32_t cfg1;
48static uint32_t cfg0_raw;
49static uint32_t cfg1_raw;
50static uint32_t ecc_bch_cfg;
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -070051static uint32_t ecc_cfg_raw;
52static uint32_t ecc_parity_bytes;
Deepa Dinamanie4573be2012-08-03 16:32:29 -070053
Sridhar Parasuramf5188d82014-11-17 15:39:55 -080054struct cmd_element ce_array[100] __attribute__ ((aligned(16)));
55struct cmd_element ce_read_array[20] __attribute__ ((aligned(16)));
Deepa Dinamanie4573be2012-08-03 16:32:29 -070056
57#define QPIC_BAM_DATA_FIFO_SIZE 64
58#define QPIC_BAM_CMD_FIFO_SIZE 64
59
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -070060#define THRESHOLD_BIT_FLIPS 4
61
Deepa Dinamanie4573be2012-08-03 16:32:29 -070062static struct bam_desc cmd_desc_fifo[QPIC_BAM_CMD_FIFO_SIZE] __attribute__ ((aligned(BAM_DESC_SIZE)));
63static struct bam_desc data_desc_fifo[QPIC_BAM_DATA_FIFO_SIZE] __attribute__ ((aligned(BAM_DESC_SIZE)));
64
65static struct bam_instance bam;
66static uint8_t *bbtbl;
67
Deepa Dinamani0bf2f442012-10-19 11:41:06 -070068static uint8_t* rdwr_buf;
69
Deepa Dinamanie4573be2012-08-03 16:32:29 -070070static struct flash_id supported_flash[] = {
Sridhar Parasuramfb0d9c82015-02-02 15:23:13 -080071 /* Flash ID Flash ID2 ID Mask ID Mask2 Density(MB) Wid Pgsz Blksz oobsz 8-bit ECCf */
72 {0x1590AC2C, 0x56, 0xFFFFFFFF, 0xFF, 0x20000000, 0, 2048, 0x00020000, 0x40, 0},
73 {0x1590AC2C, 0x57, 0xFFFFFFFF, 0xFF, 0x20000000, 0, 2048, 0x00020000, 0x40, 1},
vijay kumareb2b60a2015-11-16 12:36:38 +053074 {0x1590AA2C, 0x06, 0xFFFFFFFF, 0x0, 0x10000000, 0, 2048, 0x00020000, 0xE0, 0},
Sridhar Parasuramfb0d9c82015-02-02 15:23:13 -080075 {0x2690AC2C, 0x54, 0xFFFFFFFF, 0x0, 0x20000000, 0, 4096, 0x00040000, 0xE0, 1},
76 {0x1590ACAD, 0, 0xFFFFFFFF, 0x0, 0x20000000, 0, 2048, 0x00020000, 0x80, 0},
77 {0x9590DC2C, 0x56, 0xFFFFFFFF, 0x0, 0x10000000, 0, 2048, 0x00020000, 0x40, 0},
78 {0x1590aa98, 0x76, 0xFFFFFFFF, 0x0, 0x10000000, 0, 2048, 0x00020000, 0x80, 1},
vijay kumar94451f82015-07-23 12:41:24 +053079 {0x2690A32C, 0x64, 0xFFFFFFFF, 0x0, 0x20000000, 0, 4096, 0x00040000, 0xE0, 1},
vijay kumarc395fb52015-09-30 19:16:33 +053080 {0x2690AC98, 0x81676, 0xFFFFFFFF, 0x0, 0x20000000, 0, 4096, 0x00040000, 0xE0, 1},
Mayank Grover5338b502016-10-19 19:20:00 +053081 {0x1580a1c2, 0x02, 0xFFFFFFFF, 0xFF, 0x08000000, 0, 2048, 0x00020000, 0x40, 0},
Deepa Dinamanie4573be2012-08-03 16:32:29 -070082 /* Note: Width flag is 0 for 8 bit Flash and 1 for 16 bit flash */
Deepa Dinamanie4573be2012-08-03 16:32:29 -070083};
84
Tanya Brokhman72b44dc2015-01-07 10:20:05 +020085static int qpic_nand_mark_badblock(uint32_t page);
86
Deepa Dinamanie4573be2012-08-03 16:32:29 -070087static void
88qpic_nand_wait_for_cmd_exec(uint32_t num_desc)
89{
90 /* Create a read/write event to notify the periperal of the added desc. */
91 bam_sys_gen_event(&bam, CMD_PIPE_INDEX, num_desc);
92
93 /* Wait for the descriptors to be processed */
94 bam_wait_for_interrupt(&bam, CMD_PIPE_INDEX, P_PRCSD_DESC_EN_MASK);
95
96 /* Read offset update for the circular FIFO */
97 bam_read_offset_update(&bam, CMD_PIPE_INDEX);
98}
99
100static void
101qpic_nand_wait_for_data(uint32_t pipe_num)
102{
103 /* Wait for the descriptors to be processed */
104 bam_wait_for_interrupt(&bam, pipe_num, P_PRCSD_DESC_EN_MASK);
105
106 /* Read offset update for the circular FIFO */
107 bam_read_offset_update(&bam, pipe_num);
108}
109
110static uint32_t
111qpic_nand_read_reg(uint32_t reg_addr,
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800112 uint8_t flags)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700113{
114 uint32_t val;
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800115 struct cmd_element *cmd_list_read_ptr = ce_read_array;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700116
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800117 bam_add_cmd_element(cmd_list_read_ptr, reg_addr, (uint32_t)PA((addr_t)&val), CE_READ_TYPE);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700118
119 /* Enqueue the desc for the above command */
120 bam_add_one_desc(&bam,
121 CMD_PIPE_INDEX,
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800122 (unsigned char*)PA((addr_t)cmd_list_read_ptr),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700123 BAM_CE_SIZE,
124 BAM_DESC_CMD_FLAG| BAM_DESC_INT_FLAG | flags);
125
126 qpic_nand_wait_for_cmd_exec(1);
127
128 return val;
129}
130
Deepa Dinamani19530062012-10-03 14:43:05 -0700131/* Assume the BAM is in a locked state. */
132void
sundarajan srinivasan7ced6482013-03-21 16:01:18 -0700133qpic_nand_erased_status_reset(struct cmd_element *cmd_list_ptr, uint8_t flags)
Deepa Dinamani19530062012-10-03 14:43:05 -0700134{
135 uint32_t val = 0;
136
137 /* Reset the Erased Codeword/Page detection controller. */
138 val = NAND_ERASED_CW_DETECT_CFG_RESET_CTRL;
139
140 bam_add_cmd_element(cmd_list_ptr, NAND_ERASED_CW_DETECT_CFG, val, CE_WRITE_TYPE);
141
142 /* Enqueue the desc for the above command */
143 bam_add_one_desc(&bam,
144 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +0530145 (unsigned char*)PA((addr_t)cmd_list_ptr),
Deepa Dinamani19530062012-10-03 14:43:05 -0700146 BAM_CE_SIZE,
sundarajan srinivasan7ced6482013-03-21 16:01:18 -0700147 BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG | flags);
Deepa Dinamani19530062012-10-03 14:43:05 -0700148
149 qpic_nand_wait_for_cmd_exec(1);
150
151 /* Enable the Erased Codeword/Page detection
152 * controller to check the data as it arrives.
153 * Also disable ECC reporting for an erased CW.
154 */
155 val = NAND_ERASED_CW_DETECT_CFG_ACTIVATE_CTRL | NAND_ERASED_CW_DETECT_ERASED_CW_ECC_MASK;
156
157 bam_add_cmd_element(cmd_list_ptr, NAND_ERASED_CW_DETECT_CFG, val, CE_WRITE_TYPE);
158
159 /* Enqueue the desc for the above command */
160 bam_add_one_desc(&bam,
161 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +0530162 (unsigned char*)PA((addr_t)cmd_list_ptr),
Deepa Dinamani19530062012-10-03 14:43:05 -0700163 BAM_CE_SIZE,
164 BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG);
165
166 qpic_nand_wait_for_cmd_exec(1);
167}
168
169static nand_result_t
170qpic_nand_check_status(uint32_t status)
171{
172 uint32_t erase_sts;
173
174 /* Check for errors */
175 if (status & NAND_FLASH_ERR)
176 {
177 /* Check if this is an ECC error on an erased page. */
178 if (status & NAND_FLASH_OP_ERR)
179 {
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800180 erase_sts = qpic_nand_read_reg(NAND_ERASED_CW_DETECT_STATUS, 0);
Deepa Dinamani19530062012-10-03 14:43:05 -0700181 if ((erase_sts & (1 << NAND_ERASED_CW_DETECT_STATUS_PAGE_ALL_ERASED)))
182 {
183 /* Mask the OP ERROR. */
184 status &= ~NAND_FLASH_OP_ERR;
sundarajan srinivasan7ced6482013-03-21 16:01:18 -0700185 qpic_nand_erased_status_reset(ce_array, 0);
Deepa Dinamani19530062012-10-03 14:43:05 -0700186 }
187 }
188
189 /* ECC error flagged on an erased page read.
190 * Ignore and return success.
191 */
192 if (!(status & NAND_FLASH_ERR))
193 return NANDC_RESULT_SUCCESS;
194
195 dprintf(CRITICAL, "Nand Flash error. Status = %d\n", status);
196
197 if (status & NAND_FLASH_TIMEOUT_ERR)
198 return NANDC_RESULT_TIMEOUT;
199 else
200 return NANDC_RESULT_FAILURE;
201 }
202
203 return NANDC_RESULT_SUCCESS;
204}
205
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700206static uint32_t
207qpic_nand_fetch_id(struct flash_info *flash)
208{
209 struct cmd_element *cmd_list_ptr = ce_array;
210 struct cmd_element *cmd_list_ptr_start = ce_array;
211 int num_desc = 0;
212 uint32_t status;
Sridhar Parasuramfb0d9c82015-02-02 15:23:13 -0800213 uint32_t id, id2;
214 uint32_t flash_cmd = NAND_CMD_FETCH_ID | BIT(19); //bit 19 needs to be set to get extended NAND ID
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700215 uint32_t exec_cmd = 1;
216 int nand_ret = NANDC_RESULT_SUCCESS;
217
218 /* Issue the Fetch id command to the NANDc */
219 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)flash_cmd, CE_WRITE_TYPE);
220 cmd_list_ptr++;
221
222 /* Execute the cmd */
223 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)exec_cmd, CE_WRITE_TYPE);
224 cmd_list_ptr++;
225
226 /* Prepare the cmd desc for the above commands */
227 bam_add_one_desc(&bam,
228 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +0530229 (unsigned char*)PA((addr_t)cmd_list_ptr_start),
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700230 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700231 BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG |
232 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG);
233
234 /* Keep track of the number of desc added. */
235 num_desc++;
236 qpic_nand_wait_for_cmd_exec(num_desc);
237
238 cmd_list_ptr_start = ce_array;
239 cmd_list_ptr = ce_array;
240
241 /* Read the status register */
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800242 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700243
244 /* Check for errors */
245 nand_ret = qpic_nand_check_status(status);
246 if (nand_ret)
247 {
248 dprintf( CRITICAL, "Read ID cmd status failed\n");
249 goto qpic_nand_fetch_id_err;
250 }
251
252 /* Read the id */
Sridhar Parasuramfb0d9c82015-02-02 15:23:13 -0800253 id = qpic_nand_read_reg(NAND_READ_ID, 0);
254 id2 = qpic_nand_read_reg(NAND_READ_ID2, BAM_DESC_UNLOCK_FLAG);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700255
256 flash->id = id;
Sridhar Parasuramfb0d9c82015-02-02 15:23:13 -0800257 flash->id2 = id2;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700258 flash->vendor = id & 0xff;
259 flash->device = (id >> 8) & 0xff;
260 flash->dev_cfg = (id >> 24) & 0xFF;
261 flash->widebus = 0;
262 flash->widebus &= (id >> 24) & 0xFF;
263 flash->widebus = flash->widebus? 1: 0;
264
265qpic_nand_fetch_id_err:
266 return nand_ret;
267}
268
269static int
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800270qpic_bam_init(struct qpic_nand_init_config *config)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700271{
272 uint32_t bam_ret = NANDC_RESULT_SUCCESS;
273
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800274 bam.base = config->bam_base;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700275 /* Set Read pipe params. */
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800276 bam.pipe[DATA_PRODUCER_PIPE_INDEX].pipe_num = config->pipes.read_pipe;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700277 /* System consumer */
278 bam.pipe[DATA_PRODUCER_PIPE_INDEX].trans_type = BAM2SYS;
279 bam.pipe[DATA_PRODUCER_PIPE_INDEX].fifo.size = QPIC_BAM_DATA_FIFO_SIZE;
280 bam.pipe[DATA_PRODUCER_PIPE_INDEX].fifo.head = data_desc_fifo;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700281 bam.pipe[DATA_PRODUCER_PIPE_INDEX].lock_grp = config->pipes.read_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700282
283 /* Set Write pipe params. */
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800284 bam.pipe[DATA_CONSUMER_PIPE_INDEX].pipe_num = config->pipes.write_pipe;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700285 /* System producer */
286 bam.pipe[DATA_CONSUMER_PIPE_INDEX].trans_type = SYS2BAM;
287 bam.pipe[DATA_CONSUMER_PIPE_INDEX].fifo.size = QPIC_BAM_DATA_FIFO_SIZE;
288 bam.pipe[DATA_CONSUMER_PIPE_INDEX].fifo.head = data_desc_fifo;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700289 bam.pipe[DATA_CONSUMER_PIPE_INDEX].lock_grp = config->pipes.write_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700290
291 /* Set Cmd pipe params. */
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800292 bam.pipe[CMD_PIPE_INDEX].pipe_num = config->pipes.cmd_pipe;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700293 /* System consumer */
Sundarajan Srinivasan357c35a2013-07-23 17:00:54 -0700294 bam.pipe[CMD_PIPE_INDEX].trans_type = SYS2BAM;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700295 bam.pipe[CMD_PIPE_INDEX].fifo.size = QPIC_BAM_CMD_FIFO_SIZE;
296 bam.pipe[CMD_PIPE_INDEX].fifo.head = cmd_desc_fifo;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700297 bam.pipe[CMD_PIPE_INDEX].lock_grp = config->pipes.cmd_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700298
299 /* Programs the threshold for BAM transfer
300 * When this threshold is reached, BAM signals the peripheral via the pipe_bytes_available
301 * interface.
302 * The peripheral is signalled with this notification in the following cases:
303 * a. It has accumulated all the descriptors.
304 * b. It has accumulated more than threshold bytes.
305 * c. It has reached EOT (End Of Transfer).
306 * Note: this value needs to be set by the h/w folks and is specific for each peripheral.
307 */
308 bam.threshold = 32;
309
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800310 /* Set the EE. */
311 bam.ee = config->ee;
312
313 /* Set the max desc length for this BAM. */
314 bam.max_desc_len = config->max_desc_len;
315
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700316 /* BAM Init. */
317 bam_init(&bam);
318
319 /* Initialize BAM QPIC read pipe */
320 bam_sys_pipe_init(&bam, DATA_PRODUCER_PIPE_INDEX);
321
322 /* Init read fifo */
323 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[DATA_PRODUCER_PIPE_INDEX].pipe_num);
324
325 if (bam_ret)
326 {
327 dprintf(CRITICAL, "QPIC:NANDc BAM Read FIFO init error\n");
328 bam_ret = NANDC_RESULT_FAILURE;
329 goto qpic_nand_bam_init_error;
330 }
331
332 /* Initialize BAM QPIC write pipe */
333 bam_sys_pipe_init(&bam, DATA_CONSUMER_PIPE_INDEX);
334
335 /* Init write fifo. Use the same fifo as read fifo. */
336 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[DATA_CONSUMER_PIPE_INDEX].pipe_num);
337
338 if (bam_ret)
339 {
340 dprintf(CRITICAL, "QPIC: NANDc: BAM Write FIFO init error\n");
341 bam_ret = NANDC_RESULT_FAILURE;
342 goto qpic_nand_bam_init_error;
343 }
344
345 /* Initialize BAM QPIC cmd pipe */
346 bam_sys_pipe_init(&bam, CMD_PIPE_INDEX);
347
348 /* Init cmd fifo */
349 bam_ret = bam_pipe_fifo_init(&bam, bam.pipe[CMD_PIPE_INDEX].pipe_num);
350
351 if (bam_ret)
352 {
353 dprintf(CRITICAL, "QPIC:NANDc BAM CMD FIFO init error\n");
354 bam_ret = NANDC_RESULT_FAILURE;
355 goto qpic_nand_bam_init_error;
356 }
357
358qpic_nand_bam_init_error:
359return bam_ret;
360}
361
362/* Adds command elements for addr and cfg register writes.
363 * cfg: Defines the configuration for the flash cmd.
364 * start: Address where the command elements are added.
365 *
366 * Returns the address where the next cmd element can be added.
367 */
368static struct cmd_element*
369qpic_nand_add_addr_n_cfg_ce(struct cfg_params *cfg,
370 struct cmd_element *start)
371{
372 struct cmd_element *cmd_list_ptr = start;
373
374 bam_add_cmd_element(cmd_list_ptr, NAND_ADDR0, (uint32_t)cfg->addr0, CE_WRITE_TYPE);
375 cmd_list_ptr++;
376 bam_add_cmd_element(cmd_list_ptr, NAND_ADDR1, (uint32_t)cfg->addr1, CE_WRITE_TYPE);
377 cmd_list_ptr++;
378 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_CFG0, (uint32_t)cfg->cfg0, CE_WRITE_TYPE);
379 cmd_list_ptr++;
380 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_CFG1, (uint32_t)cfg->cfg1, CE_WRITE_TYPE);
381 cmd_list_ptr++;
382
383 return cmd_list_ptr;
384}
385
386
387static struct cmd_element*
388qpic_nand_add_onfi_probe_ce(struct onfi_probe_params *params,
389 struct cmd_element *start)
390{
391 struct cmd_element *cmd_list_ptr = start;
392
393 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(&params->cfg, cmd_list_ptr);
394
395 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD1, (uint32_t)params->dev_cmd1, CE_WRITE_TYPE);
396 cmd_list_ptr++;
397 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD_VLD, (uint32_t)params->vld, CE_WRITE_TYPE);
398 cmd_list_ptr++;
399 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(0), (uint32_t)params->cfg.addr_loc_0, CE_WRITE_TYPE);
400 cmd_list_ptr++;
401 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)params->cfg.cmd, CE_WRITE_TYPE);
402 cmd_list_ptr++;
403 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)params->cfg.exec, CE_WRITE_TYPE);
404 cmd_list_ptr++;
405
406 return cmd_list_ptr;
407}
408
409static int
410onfi_probe_cmd_exec(struct onfi_probe_params *params,
411 unsigned char* data_ptr,
412 int data_len)
413{
414 struct cmd_element *cmd_list_ptr = ce_array;
415 struct cmd_element *cmd_list_ptr_start = ce_array;
416 int num_desc = 0;
417 uint32_t status = 0;
418 int nand_ret = NANDC_RESULT_SUCCESS;
419 uint8_t desc_flags = BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG
420 | BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG;
421
422 params->cfg.addr_loc_0 = 0;
423 params->cfg.addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
424 params->cfg.addr_loc_0 |= NAND_RD_LOC_OFFSET(0);
425 params->cfg.addr_loc_0 |= NAND_RD_LOC_SIZE(data_len);
426
427 cmd_list_ptr = qpic_nand_add_onfi_probe_ce(params, cmd_list_ptr);
428
429 /* Enqueue the desc for the above commands */
430 bam_add_one_desc(&bam,
431 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +0530432 (unsigned char*)PA((addr_t)cmd_list_ptr_start),
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700433 PA((addr_t)(uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700434 desc_flags);
435
436 cmd_list_ptr_start = cmd_list_ptr;
437 num_desc++;
438
439 /* Add Data desc */
440 bam_add_desc(&bam,
441 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700442 (unsigned char *)PA((addr_t)data_ptr),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700443 data_len,
444 BAM_DESC_INT_FLAG);
445
446 /* Wait for the commands to be executed */
447 qpic_nand_wait_for_cmd_exec(num_desc);
448
449 /* Read buffer status and check for errors. */
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800450 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700451
452 if (qpic_nand_check_status(status))
453 {
454 nand_ret = NANDC_RESULT_FAILURE;
455 goto onfi_probe_exec_err;
456 }
457
458 /* Wait for data to be available */
459 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
460
461 /* Check for errors */
462 nand_ret = qpic_nand_check_status(status);
463
464onfi_probe_exec_err:
465 return nand_ret;
466}
467
468/* TODO: check why both vld and cmd need to be written. */
469void
470qpic_nand_onfi_probe_cleanup(uint32_t vld, uint32_t dev_cmd1)
471{
472 struct cmd_element *cmd_list_ptr = ce_array;
473 struct cmd_element *cmd_list_ptr_start = ce_array;
474
475 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD1, dev_cmd1, CE_WRITE_TYPE);
476 cmd_list_ptr++;
477 bam_add_cmd_element(cmd_list_ptr, NAND_DEV_CMD_VLD, vld, CE_WRITE_TYPE);
478 cmd_list_ptr++;
479
480 /* Enqueue the desc for the above commands */
481 bam_add_one_desc(&bam,
482 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +0530483 (unsigned char*)PA((addr_t)cmd_list_ptr_start),
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700484 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700485 BAM_DESC_UNLOCK_FLAG | BAM_DESC_CMD_FLAG| BAM_DESC_INT_FLAG);
486
487 qpic_nand_wait_for_cmd_exec(1);
488}
489
490static int
491qpic_nand_onfi_save_params(struct onfi_param_page *param_page, struct flash_info *flash)
492{
493 int onfi_ret = NANDC_RESULT_SUCCESS;
494 uint32_t ecc_bits;
495
496 onfi_ret = qpic_nand_fetch_id(flash);
497
498 if (onfi_ret)
499 {
500 dprintf(CRITICAL, "Fetch ID cmd failed\n");
501 goto onfi_save_params_err;
502 }
503
504 flash->page_size = param_page->data_per_pg;
505 flash->block_size = param_page->pgs_per_blk * flash->page_size;
506 flash->num_blocks = param_page->blks_per_LUN;
507 flash->widebus = param_page->feature_supported & 0x1;
508 flash->density = param_page->blks_per_LUN * flash->blksize;
509 flash->spare_size = param_page->spare_per_pg;
510 ecc_bits = param_page->num_bits_ecc_correctability;
511 flash->num_pages_per_blk = param_page->pgs_per_blk;
512 flash->num_pages_per_blk_mask = param_page->pgs_per_blk - 1;
513
514 if (ecc_bits >= 8)
515 flash->ecc_width = NAND_WITH_8_BIT_ECC;
516 else
517 flash->ecc_width = NAND_WITH_4_BIT_ECC;
518
519 onfi_save_params_err:
520 return onfi_ret;
521}
522
523static void
524qpic_nand_save_config(struct flash_info *flash)
525{
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -0700526 uint32_t spare_bytes = 0;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700527
528 /* Save Configurations */
529 flash->cws_per_page = flash->page_size >> NAND_CW_DIV_RIGHT_SHIFT;
530
Deepa Dinamani16663a62013-02-07 16:25:59 -0800531 /* Verify that we have enough buffer to handle all the cws in a page. */
532 ASSERT(flash->cws_per_page <= QPIC_NAND_MAX_CWS_IN_PAGE);
533
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700534 /* Codeword Size = UD_SIZE_BYTES + ECC_PARITY_SIZE_BYTES
535 * + SPARE_SIZE_BYTES + Bad Block size
536 */
537 if (flash->ecc_width & NAND_WITH_8_BIT_ECC)
538 {
539 flash->cw_size = NAND_CW_SIZE_8_BIT_ECC;
540 ecc_bch_cfg |= (1 << NAND_DEV0_ECC_MODE_SHIFT); /* Use 8-bit ecc */
541
542 if (flash->widebus)
543 {
544 cfg0 |= (0 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
545 ecc_bch_cfg |= (14 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
546 }
547 else
548 {
549 cfg0 |= (2 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
550 ecc_bch_cfg |= (13 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
551 }
552 }
553 else
554 {
555 flash->cw_size = NAND_CW_SIZE_4_BIT_ECC;
556
557 if (flash->widebus)
558 {
559 cfg0 |= (2 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
560 ecc_bch_cfg |= (8 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
561 }
562 else
563 {
564 cfg0 |= (4 << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT); /* spare size bytes in each CW */
565 ecc_bch_cfg |= (7 << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT); /* parity bytes in each CW */
566 }
567 }
568
569 /* BAD_BLOCK_BYTE_NUM = Page Size -
570 * (CW_PER_PAGE * Codeword Size) + 1
571 * Note: Set CW_PER_PAGE to 1 less than the actual number.
572 */
573 flash->bad_blk_loc = flash->page_size - flash->cw_size * (flash->cws_per_page - 1) + 1;
574
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -0700575 /* Calculate the parity and spare bytes */
576 ecc_parity_bytes = (flash->ecc_width & NAND_WITH_8_BIT_ECC) ? (flash->widebus ? 14 : 13) : (flash->widebus ? 8 : 7) ;
577 spare_bytes = flash->cw_size - (USER_DATA_BYTES_PER_CW + ecc_parity_bytes);
578
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700579 cfg0 |= ((flash->cws_per_page - 1) << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT) /* 4/8 cw/pg for 2/4k */
580 |(DATA_BYTES_IN_IMG_PER_CW << NAND_DEV0_CFG0_UD_SIZE_BYTES_SHIFT) /* 516 user data bytes */
581 |(5 << NAND_DEV0_CFG0_ADDR_CYCLE_SHIFT) /* 5 address cycles */
582 |(0 << NAND_DEV0_CFG0_DIS_STS_AFTER_WR_SHIFT);/* Send read status cmd after each write. */
583
584 cfg1 |= (7 << NAND_DEV0_CFG1_RECOVERY_CYCLES_SHIFT) /* 8 recovery cycles */
585 |(0 << NAND_DEV0_CFG1_CS_ACTIVE_BSY_SHIFT) /* Allow CS deassertion */
586 |(flash->bad_blk_loc << NAND_DEV0_CFG1_BAD_BLK_BYTE_NUM_SHIFT)/* Bad block marker location */
587 |(0 << NAND_DEV0_CFG1_BAD_BLK_IN_SPARE_SHIFT) /* Bad block in user data area */
588 |(2 << NAND_DEV0_CFG1_WR_RD_BSY_GAP_SHIFT) /* 8 cycle tWB/tRB */
589 |(flash->widebus << NAND_DEV0_CFG1_WIDE_BUS_SHIFT); /* preserve wide flash flag */
590
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -0700591 cfg0_raw = ((flash->cws_per_page - 1) << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700592 |(5 << NAND_DEV0_CFG0_ADDR_CYCLE_SHIFT)
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -0700593 |(512 << NAND_DEV0_CFG0_UD_SIZE_BYTES_SHIFT)
594 | (spare_bytes << NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700595
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -0700596 cfg1_raw = (2 << NAND_DEV0_CFG1_WR_RD_BSY_GAP_SHIFT)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700597 | (1 << NAND_DEV0_CFG1_BAD_BLK_IN_SPARE_SHIFT)
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -0700598 | (21 << NAND_DEV0_CFG1_BAD_BLK_BYTE_NUM_SHIFT)
599 | (0 << NAND_DEV0_CFG1_CS_ACTIVE_BSY_SHIFT)
600 | (7 << NAND_DEV0_CFG1_RECOVERY_CYCLES_SHIFT)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700601 | (flash->widebus << NAND_DEV0_CFG1_WIDE_BUS_SHIFT)
602 |1 ; /* to disable reed solomon ecc..this feild is now read only. */
603
604 ecc_bch_cfg |= (0 << NAND_DEV0_ECC_DISABLE_SHIFT) /* Enable ECC */
605 | (0 << NAND_DEV0_ECC_SW_RESET_SHIFT) /* Put ECC core in op mode */
606 | (DATA_BYTES_IN_IMG_PER_CW << NAND_DEV0_ECC_NUM_DATA_BYTES)
607 | (1 << NAND_DEV0_ECC_FORCE_CLK_OPEN_SHIFT); /* Enable all clocks */
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -0700608
609 ecc_cfg_raw = (1 << NAND_DEV0_ECC_FORCE_CLK_OPEN_SHIFT)
610 | (DATA_BYTES_IN_IMG_PER_CW << NAND_DEV0_ECC_NUM_DATA_BYTES)
611 | (ecc_parity_bytes << NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT)
612 | (0 << NAND_DEV0_ECC_SW_RESET_SHIFT)
613 | (1 << NAND_DEV0_ECC_DISABLE_SHIFT);
614
615#if DEBUG_QPIC_NAND
616 dprintf(INFO, "CFG0: 0x%08x CFG1: 0x%08x\n", cfg0, cfg1);
617 dprintf(INFO, "CFG0_RAW: 0x%08x CFG1_RAW: 0x%08x\n", cfg0_raw, cfg1_raw);
618 dprintf(INFO, "ECC_BCH_CFG: 0x%08x ECC_CFG_RAW: 0x%08x\n", ecc_bch_cfg, ecc_cfg_raw);
619#endif
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700620}
621
622/* Onfi probe should issue the following commands to the flash device:
623 * 1. Read ID - with addr ONFI_READ_ID_ADDR.
624 * This returns the ONFI ASCII string indicating support for ONFI.
625 * 2. Read Prameter Page - with addr ONFI_READ_PARAM_PAGE_ADDR.
626 * This returns the params for the device.
627 * Each command inturn issues commands- ADDR0, ADDR1, chip_select,
628 * cfg0, cfg1, cmd_vld, dev_cmd1, read_loc0, flash, exec.
629 */
630static int
631qpic_nand_onfi_probe(struct flash_info *flash)
632{
633 struct onfi_probe_params params;
634 uint32_t vld;
635 uint32_t dev_cmd1;
636 unsigned char *buffer;
637 unsigned char onfi_str[4];
638 uint32_t *id;
639 struct onfi_param_page *param_page;
640 int onfi_ret = NANDC_RESULT_SUCCESS;
641
642 /* Allocate memory required to read the onfi param page */
643 buffer = (unsigned char*) malloc(ONFI_READ_PARAM_PAGE_BUFFER_SIZE);
Maria Yu71909742014-07-04 17:30:00 +0800644 ASSERT(buffer != NULL);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700645
646 /* Read the vld and dev_cmd1 registers before modifying */
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800647 vld = qpic_nand_read_reg(NAND_DEV_CMD_VLD, 0);
648 dev_cmd1 = qpic_nand_read_reg(NAND_DEV_CMD1, 0);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700649
650 /* Initialize flash cmd */
651 params.cfg.cmd = NAND_CMD_PAGE_READ;
652 params.cfg.exec = 1;
653
654 /* Execute Read ID cmd */
655
656 /* Initialize the config */
657 params.cfg.cfg0 = NAND_CFG0_RAW_ONFI_ID;
658 params.cfg.cfg1 = NAND_CFG1_RAW_ONFI_ID;
659
660 /* Initialize the cmd and vld */
661 params.dev_cmd1 = (dev_cmd1 & 0xFFFFFF00) | ONFI_READ_ID_CMD;
662 params.vld = vld & 0xFFFFFFFE;
663
664 /* Initialize the address
665 * addr1 is not used bcos of the cfg.
666 */
667 params.cfg.addr0 = ONFI_READ_ID_ADDR;
668 params.cfg.addr1 = 0;
669
670 /* Lock the pipe and execute the cmd. */
671 onfi_ret = onfi_probe_cmd_exec(&params, onfi_str, ONFI_READ_ID_BUFFER_SIZE);
672 if (onfi_ret)
673 {
674 dprintf(CRITICAL, "ONFI Read id cmd failed\n");
675 goto qpic_nand_onfi_probe_err;
676 }
677
678 /* Write back vld and cmd and unlock the pipe. */
679 qpic_nand_onfi_probe_cleanup(vld, dev_cmd1);
680
681 /* Check for onfi string */
682 id = (uint32_t*)onfi_str;
683 if (*id != ONFI_SIGNATURE)
684 {
685 dprintf(CRITICAL, "Not an ONFI device\n");
686 /* Not an onfi device. Return error. */
687 onfi_ret = NANDC_RESULT_DEV_NOT_SUPPORTED;
688 goto qpic_nand_onfi_probe_err;
689 }
690
691 dprintf(INFO, "ONFI device found\n");
692 /* Now read the param page */
693 /* Initialize the config */
694 params.cfg.cfg0 = NAND_CFG0_RAW_ONFI_PARAM_PAGE;
695 params.cfg.cfg1 = NAND_CFG1_RAW_ONFI_PARAM_PAGE;
696
697 /* Initialize the cmd and vld */
698 params.dev_cmd1 = (dev_cmd1 & 0xFFFFFF00) | ONFI_READ_PARAM_PAGE_CMD;
699 params.vld = vld & 0xFFFFFFFE;
700
701 /* Initialize the address
702 * addr1 is not used bcos of the cfg.
703 */
704 params.cfg.addr0 = ONFI_READ_PARAM_PAGE_ADDR;
705 params.cfg.addr1 = 0;
706
707 /* Lock the pipe and execute the cmd. */
708 onfi_ret = onfi_probe_cmd_exec(&params, buffer, ONFI_READ_PARAM_PAGE_BUFFER_SIZE);
709 if (onfi_ret)
710 {
711 dprintf(CRITICAL, "ONFI Read param page failed\n");
712 goto qpic_nand_onfi_probe_err;
713 }
714
715 /* Write back vld and cmd and unlock the pipe. */
716 qpic_nand_onfi_probe_cleanup(vld, dev_cmd1);
717
718 /* Verify the integrity of the returned page */
719 param_page = (struct onfi_param_page*)buffer;
720
721 /* TODO: Add CRC check to validate the param page. */
722
723 /* Save the parameter values */
724 onfi_ret = qpic_nand_onfi_save_params(param_page, flash);
725
726qpic_nand_onfi_probe_err:
727 if (onfi_ret)
728 dprintf(CRITICAL, "ONFI probe failed\n");
729
730 free(buffer);
731
732 return onfi_ret;
733}
734
735/* Enquues a desc for a flash cmd with NWD flag set:
736 * cfg: Defines the configuration for the flash cmd.
737 * start: Address where the command elements are added.
738 *
739 * Returns the address where the next cmd element can be added.
740 */
741struct cmd_element*
742qpic_nand_add_cmd_ce(struct cfg_params *cfg,
743 struct cmd_element *start)
744{
745 struct cmd_element *cmd_list_ptr;
746
747 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(cfg, start);
748
749 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)cfg->cmd, CE_WRITE_TYPE);
750 cmd_list_ptr++;
751
752 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)cfg->exec, CE_WRITE_TYPE);
753 cmd_list_ptr++;
754
755 return cmd_list_ptr;
756}
757
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800758/* Reads nand_flash_status */
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700759struct cmd_element*
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800760qpic_nand_add_read_ce(struct cmd_element *start, uint32_t *flash_status_read)
761{
762 struct cmd_element *cmd_list_ptr = start;
763
764 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_STATUS, (uint32_t)PA((addr_t)flash_status_read), CE_READ_TYPE);
765 cmd_list_ptr++;
766
767 return cmd_list_ptr;
768}
769
770/* Resets nand_flash_status and nand_read_status */
771struct cmd_element*
772qpic_nand_reset_status_ce(struct cmd_element *start, uint32_t read_status)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700773{
774 struct cmd_element *cmd_list_ptr = start;
775 uint32_t flash_status_reset;
776 uint32_t read_status_reset;
777
778 /* Read and reset the status registers. */
779 flash_status_reset = NAND_FLASH_STATUS_RESET;
780 read_status_reset = NAND_READ_STATUS_RESET;
781
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700782 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_STATUS, (uint32_t)flash_status_reset, CE_WRITE_TYPE);
783 cmd_list_ptr++;
784
785 if (read_status)
786 {
787 bam_add_cmd_element(cmd_list_ptr, NAND_READ_STATUS, (uint32_t)read_status_reset, CE_WRITE_TYPE);
788 cmd_list_ptr++;
789 }
790
791 return cmd_list_ptr;
792}
793
794struct cmd_element*
795qpic_nand_add_isbad_cmd_ce(struct cfg_params *cfg,
796 struct cmd_element *start)
797{
798 struct cmd_element *cmd_list_ptr = start;
799
800 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG, (uint32_t)cfg->ecc_cfg, CE_WRITE_TYPE);
801 cmd_list_ptr++;
802
803 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(0), (uint32_t)cfg->addr_loc_0, CE_WRITE_TYPE);
804 cmd_list_ptr++;
805
806 cmd_list_ptr = qpic_nand_add_cmd_ce(cfg, cmd_list_ptr);
807
808 return cmd_list_ptr;
809}
810
811static int
812qpic_nand_block_isbad_exec(struct cfg_params *params,
813 uint8_t *bad_block)
814{
815
816 struct cmd_element *cmd_list_ptr = ce_array;
817 struct cmd_element *cmd_list_ptr_start = ce_array;
818 uint8_t desc_flags = BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG
819 | BAM_DESC_LOCK_FLAG | BAM_DESC_INT_FLAG;
820 int num_desc = 0;
821 uint32_t status = 0;
822 int nand_ret = NANDC_RESULT_SUCCESS;
823
824 cmd_list_ptr = qpic_nand_add_isbad_cmd_ce(params, cmd_list_ptr);
825
826 /* Enqueue the desc for the above commands */
827 bam_add_one_desc(&bam,
828 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +0530829 (unsigned char*)PA((addr_t)cmd_list_ptr_start),
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700830 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700831 desc_flags);
832
833 num_desc++;
834
835 /* Add Data desc */
836 bam_add_desc(&bam,
837 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700838 (unsigned char *)PA((addr_t)bad_block),
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700839 4,
840 BAM_DESC_INT_FLAG);
841
842 qpic_nand_wait_for_cmd_exec(num_desc);
843
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800844 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0);
Deepa Dinamani19530062012-10-03 14:43:05 -0700845
846 nand_ret = qpic_nand_check_status(status);
847
848 /* Dummy read to unlock pipe. */
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800849 status = qpic_nand_read_reg(NAND_FLASH_STATUS, BAM_DESC_UNLOCK_FLAG);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700850
Deepa Dinamani19530062012-10-03 14:43:05 -0700851 if (nand_ret)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700852 return NANDC_RESULT_FAILURE;
853
854 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
855
856 return nand_ret;
857}
858
Tanya Brokhman72b44dc2015-01-07 10:20:05 +0200859/**
860 * qpic_nand_block_isbad() - Checks is given block is bad
861 * @page - number of page the block starts at
862 *
863 * Returns nand_result_t
864 */
865nand_result_t qpic_nand_block_isbad(unsigned page)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700866{
867 unsigned cwperpage;
868 struct cfg_params params;
869 uint8_t bad_block[4];
870 unsigned nand_ret = NANDC_RESULT_SUCCESS;
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800871 uint32_t blk = page / flash.num_pages_per_blk;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700872
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800873 if (bbtbl[blk] == NAND_BAD_BLK_VALUE_IS_GOOD)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700874 return NANDC_RESULT_SUCCESS;
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800875 else if (bbtbl[blk] == NAND_BAD_BLK_VALUE_IS_BAD)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700876 return NANDC_RESULT_BAD_BLOCK;
877 else
878 {
879 /* Read the bad block value from the flash.
880 * Bad block value is stored in the first page of the block.
881 */
882 /* Read the first page in the block. */
883 cwperpage = flash.cws_per_page;
884
885 /* Read page cmd */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800886 params.cmd = NAND_CMD_PAGE_READ_ECC;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700887 /* Clear the CW per page bits */
888 params.cfg0 = cfg0_raw & ~(7U << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT);
889 params.cfg1 = cfg1_raw;
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800890 /* addr0 - Write column addr + few bits in row addr upto 32 bits. */
891 params.addr0 = (page << 16) | (USER_DATA_BYTES_PER_CW * cwperpage);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700892
893 /* addr1 - Write rest of row addr.
894 * This will be all 0s.
895 */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800896 params.addr1 = (page >> 16) & 0xff;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700897 params.addr_loc_0 = NAND_RD_LOC_OFFSET(0);
898 params.addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
899 params.addr_loc_0 |= NAND_RD_LOC_SIZE(4); /* Read 4 bytes */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800900 params.ecc_cfg = ecc_bch_cfg | 0x1; /* Disable ECC */
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700901 params.exec = 1;
902
903 if (qpic_nand_block_isbad_exec(&params, bad_block))
904 {
905 dprintf(CRITICAL,
906 "Could not read bad block value\n");
907 return NANDC_RESULT_FAILURE;
908 }
909
910 if (flash.widebus)
911 {
912 if (bad_block[0] != 0xFF && bad_block[1] != 0xFF)
913 {
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800914 bbtbl[blk] = NAND_BAD_BLK_VALUE_IS_BAD;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700915 nand_ret = NANDC_RESULT_BAD_BLOCK;
916 }
917 }
918 else if (bad_block[0] != 0xFF)
919 {
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800920 bbtbl[blk] = NAND_BAD_BLK_VALUE_IS_BAD;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700921 nand_ret = NANDC_RESULT_BAD_BLOCK;
922 }
923 else
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800924 bbtbl[blk] = NAND_BAD_BLK_VALUE_IS_GOOD;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700925
926 return nand_ret;
927 }
928}
929
930/* Function to erase a block on the nand.
931 * page: Starting page address for the block.
932 */
Tanya Brokhman72b44dc2015-01-07 10:20:05 +0200933nand_result_t qpic_nand_blk_erase(uint32_t page)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700934{
935 struct cfg_params cfg;
936 struct cmd_element *cmd_list_ptr = ce_array;
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800937 struct cmd_element *cmd_list_read_ptr = ce_read_array;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700938 struct cmd_element *cmd_list_ptr_start = ce_array;
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800939 struct cmd_element *cmd_list_read_ptr_start = ce_read_array;
Veera Sundaram Sankaran00181512014-12-09 11:23:39 -0800940 uint32_t status;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700941 int num_desc = 0;
942 uint32_t blk_addr = page / flash.num_pages_per_blk;
943
944 /* Erase only if the block is not bad */
Deepa Dinamanidc1381e2012-11-15 14:53:24 -0800945 if (qpic_nand_block_isbad(page))
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700946 {
947 dprintf(CRITICAL,
948 "NAND Erase error: Block address belongs to bad block: %d\n",
949 blk_addr);
950 return NANDC_RESULT_FAILURE;
951 }
952
953 /* Fill in params for the erase flash cmd */
954 cfg.addr0 = page;
955 cfg.addr1 = 0;
956 /* Clear CW_PER_PAGE in cfg0 */
957 cfg.cfg0 = cfg0 & ~(7U << NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT);
958 cfg.cfg1 = cfg1;
959 cfg.cmd = NAND_CMD_BLOCK_ERASE;
960 cfg.exec = 1;
961
962 cmd_list_ptr = qpic_nand_add_cmd_ce(&cfg, cmd_list_ptr);
963
964 /* Enqueue the desc for the above commands */
965 bam_add_one_desc(&bam,
966 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +0530967 (unsigned char*)PA((addr_t)cmd_list_ptr_start),
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700968 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamani2467bbb2012-10-02 13:59:58 -0700969 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG | BAM_DESC_INT_FLAG | BAM_DESC_LOCK_FLAG);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700970
971 cmd_list_ptr_start = cmd_list_ptr;
972 num_desc++;
973
974 qpic_nand_wait_for_cmd_exec(num_desc);
975
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800976 status = qpic_nand_read_reg(NAND_FLASH_STATUS, 0);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700977
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700978 cmd_list_ptr_start = cmd_list_ptr;
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800979 cmd_list_read_ptr_start = cmd_list_read_ptr;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700980
981 /* QPIC controller automatically sends
982 * GET_STATUS cmd to the nand card because
983 * of the configuration programmed.
984 * Read the result of GET_STATUS cmd.
985 */
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800986 cmd_list_read_ptr = qpic_nand_add_read_ce(cmd_list_read_ptr, &status);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700987
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800988 /* Enqueue the desc for the NAND_FLASH_STATUS read command */
989 bam_add_one_desc(&bam,
990 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +0530991 (unsigned char*)PA((addr_t)cmd_list_read_ptr_start),
Sridhar Parasuramf5188d82014-11-17 15:39:55 -0800992 PA((uint32_t)cmd_list_read_ptr - (uint32_t)cmd_list_read_ptr_start),
993 BAM_DESC_CMD_FLAG) ;
994
995 cmd_list_ptr = qpic_nand_reset_status_ce(cmd_list_ptr, 1);
996
997 /* Enqueue the desc for NAND_FLASH_STATUS and NAND_READ_STATUS write commands */
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700998 bam_add_one_desc(&bam,
999 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +05301000 (unsigned char*)PA((addr_t)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 Dinamani19530062012-10-03 14:43:05 -07001002 BAM_DESC_INT_FLAG | BAM_DESC_CMD_FLAG) ;
Sridhar Parasuramf5188d82014-11-17 15:39:55 -08001003 num_desc = 2;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001004 qpic_nand_wait_for_cmd_exec(num_desc);
1005
Deepa Dinamani19530062012-10-03 14:43:05 -07001006 status = qpic_nand_check_status(status);
1007
1008 /* Dummy read to unlock pipe. */
Veera Sundaram Sankaran00181512014-12-09 11:23:39 -08001009 qpic_nand_read_reg(NAND_FLASH_STATUS, BAM_DESC_UNLOCK_FLAG);
Deepa Dinamani19530062012-10-03 14:43:05 -07001010
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001011 /* Check for status errors*/
Deepa Dinamani19530062012-10-03 14:43:05 -07001012 if (status)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001013 {
1014 dprintf(CRITICAL,
1015 "NAND Erase error: Block address belongs to bad block: %d\n",
1016 blk_addr);
Tanya Brokhman72b44dc2015-01-07 10:20:05 +02001017 qpic_nand_mark_badblock(page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001018 return NANDC_RESULT_FAILURE;
1019 }
1020
1021 /* Check for PROG_ERASE_OP_RESULT bit for the result of erase operation. */
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07001022 if (!(status & PROG_ERASE_OP_RESULT))
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001023 return NANDC_RESULT_SUCCESS;
1024
Tanya Brokhman72b44dc2015-01-07 10:20:05 +02001025 qpic_nand_mark_badblock(page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001026 return NANDC_RESULT_FAILURE;
1027}
1028
1029/* Return num of desc added. */
Deepa Dinamani19530062012-10-03 14:43:05 -07001030static void
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001031qpic_nand_add_wr_page_cws_cmd_desc(struct cfg_params *cfg,
1032 uint32_t status[],
1033 enum nand_cfg_value cfg_mode)
1034{
1035 struct cmd_element *cmd_list_ptr = ce_array;
Sridhar Parasuramf5188d82014-11-17 15:39:55 -08001036 struct cmd_element *cmd_list_read_ptr = ce_read_array;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001037 struct cmd_element *cmd_list_ptr_start = ce_array;
Sridhar Parasuramf5188d82014-11-17 15:39:55 -08001038 struct cmd_element *cmd_list_read_ptr_start = ce_read_array;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001039 uint32_t ecc;
1040 int num_desc = 0;
1041 int int_flag = 0;
1042
1043 if (cfg_mode == NAND_CFG)
1044 ecc = ecc_bch_cfg;
1045 else
Deepa Dinamanidc1381e2012-11-15 14:53:24 -08001046 ecc = ecc_bch_cfg | 0x1; /* Disable ECC */
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001047
1048 /* Add ECC configuration */
1049 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG,
1050 (uint32_t)ecc, CE_WRITE_TYPE);
1051 cmd_list_ptr++;
1052 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(cfg, cmd_list_ptr);
1053
1054 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD,
1055 (uint32_t)cfg->cmd, CE_WRITE_TYPE);
1056 cmd_list_ptr++;
1057
1058 /* Enqueue the desc for the above commands */
1059 bam_add_one_desc(&bam,
1060 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +05301061 (unsigned char*)PA((addr_t)cmd_list_ptr_start),
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001062 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001063 BAM_DESC_CMD_FLAG | BAM_DESC_LOCK_FLAG);
1064
1065 num_desc++;
1066
1067 /* Add CE for all the CWs */
1068 for (unsigned i = 0; i < flash.cws_per_page; i++)
1069 {
1070 cmd_list_ptr_start = cmd_list_ptr;
Deepa Dinamani19530062012-10-03 14:43:05 -07001071 int_flag = BAM_DESC_INT_FLAG;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001072
1073 bam_add_cmd_element(cmd_list_ptr, NAND_EXEC_CMD, (uint32_t)cfg->exec, CE_WRITE_TYPE);
1074 cmd_list_ptr++;
1075
1076 /* Enqueue the desc for the above commands */
1077 bam_add_one_desc(&bam,
1078 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +05301079 (unsigned char*)PA((addr_t)cmd_list_ptr_start),
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001080 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001081 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG);
1082
1083 num_desc++;
1084 cmd_list_ptr_start = cmd_list_ptr;
Sridhar Parasuramf5188d82014-11-17 15:39:55 -08001085 cmd_list_read_ptr_start = cmd_list_read_ptr;
1086
1087 cmd_list_read_ptr = qpic_nand_add_read_ce(cmd_list_read_ptr_start, &status[i]);
1088 /* Enqueue the desc for the NAND_FLASH_STATUS read command */
1089 bam_add_one_desc(&bam,
1090 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +05301091 (unsigned char*)PA((addr_t)cmd_list_read_ptr_start),
Sridhar Parasuramf5188d82014-11-17 15:39:55 -08001092 PA((uint32_t)cmd_list_read_ptr - (uint32_t)cmd_list_read_ptr_start),
1093 BAM_DESC_CMD_FLAG);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001094
1095 /* Set interrupt bit only for the last CW */
1096 if (i == flash.cws_per_page - 1)
Sridhar Parasuramf5188d82014-11-17 15:39:55 -08001097 cmd_list_ptr = qpic_nand_reset_status_ce(cmd_list_ptr, 1);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001098 else
Sridhar Parasuramf5188d82014-11-17 15:39:55 -08001099 cmd_list_ptr = qpic_nand_reset_status_ce(cmd_list_ptr, 0);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001100
Sridhar Parasuramf5188d82014-11-17 15:39:55 -08001101 /* Enqueue the desc for NAND_FLASH_STATUS and NAND_READ_STATUS write commands */
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001102 bam_add_one_desc(&bam,
1103 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +05301104 (unsigned char*)PA((addr_t)cmd_list_ptr_start),
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001105 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001106 int_flag | BAM_DESC_CMD_FLAG);
Sridhar Parasuramf5188d82014-11-17 15:39:55 -08001107 num_desc += 2;
Deepa Dinamani19530062012-10-03 14:43:05 -07001108
1109 qpic_nand_wait_for_cmd_exec(num_desc);
1110
1111 status[i] = qpic_nand_check_status(status[i]);
1112
1113 num_desc = 0;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001114 }
Deepa Dinamani19530062012-10-03 14:43:05 -07001115 return;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001116}
1117
1118void
1119qpic_add_wr_page_cws_data_desc(const void *buffer,
1120 enum nand_cfg_value cfg_mode,
1121 const void *spareaddr)
1122{
1123 int len;
1124 int flags;
1125 uint32_t start;
1126 unsigned num_desc = 0;
1127
1128 for( unsigned i = 0; i < flash.cws_per_page; i++)
1129 {
1130 flags = 0;
1131
1132 /* Set the interrupt flag on the last CW write for the page. */
1133 if( i == flash.cws_per_page - 1)
1134 flags |= BAM_DESC_INT_FLAG;
1135
1136 if (cfg_mode != NAND_CFG_RAW)
1137 {
1138 start = (uint32_t)buffer + i * DATA_BYTES_IN_IMG_PER_CW;
1139
1140 if (i < (flash.cws_per_page - 1))
1141 {
1142 len = DATA_BYTES_IN_IMG_PER_CW;
1143 flags |= BAM_DESC_EOT_FLAG;
1144 }
1145 else
1146 {
1147 /* Allow space for spare bytes in the last page */
1148 len = USER_DATA_BYTES_PER_CW - ((flash.cws_per_page - 1) << 2);
1149 flags = 0;
1150 }
1151 }
1152 else
1153 {
1154 start = (uint32_t)buffer;
1155 len = flash.cw_size;
1156 flags |= BAM_DESC_EOT_FLAG;
1157 }
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001158 bam_add_one_desc(&bam, DATA_CONSUMER_PIPE_INDEX, (unsigned char*)PA(start), len, flags);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001159 num_desc++;
1160
1161 if ((i == (flash.cws_per_page - 1)) && (cfg_mode == NAND_CFG))
1162 {
1163 /* write extra data */
1164 start = (uint32_t)spareaddr;
1165 len = (flash.cws_per_page << 2);
1166 flags = BAM_DESC_EOT_FLAG | BAM_DESC_INT_FLAG;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001167 bam_add_one_desc(&bam, DATA_CONSUMER_PIPE_INDEX, (unsigned char*)PA(start), len, flags);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001168 num_desc++;
1169 }
1170 }
1171
1172 bam_sys_gen_event(&bam, DATA_CONSUMER_PIPE_INDEX, num_desc);
1173}
1174
1175static nand_result_t
1176qpic_nand_write_page(uint32_t pg_addr,
1177 enum nand_cfg_value cfg_mode,
1178 const void* buffer,
1179 const void* spareaddr)
1180{
1181 struct cfg_params cfg;
Deepa Dinamani16663a62013-02-07 16:25:59 -08001182 uint32_t status[QPIC_NAND_MAX_CWS_IN_PAGE];
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001183 int nand_ret = NANDC_RESULT_SUCCESS;
1184
1185 if (cfg_mode == NAND_CFG_RAW)
1186 {
1187 cfg.cfg0 = cfg0_raw;
1188 cfg.cfg1 = cfg1_raw;
1189 }
1190 else
1191 {
1192 cfg.cfg0 = cfg0;
1193 cfg.cfg1 = cfg1;
1194 }
1195
1196 cfg.cmd = NAND_CMD_PRG_PAGE;
1197 cfg.exec = 1;
1198
1199 cfg.addr0 = pg_addr << 16;
1200 cfg.addr1 = (pg_addr >> 16) & 0xff;
1201
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001202 qpic_add_wr_page_cws_data_desc(buffer, cfg_mode, spareaddr);
1203
Deepa Dinamani19530062012-10-03 14:43:05 -07001204 qpic_nand_add_wr_page_cws_cmd_desc(&cfg, status, cfg_mode);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001205
1206 /* Check for errors */
1207 for(unsigned i = 0; i < flash.cws_per_page; i++)
1208 {
1209 nand_ret = qpic_nand_check_status(status[i]);
1210 if (nand_ret)
1211 {
1212 dprintf(CRITICAL,
1213 "Failed to write CW %d for page: %d\n",
1214 i, pg_addr);
1215 break;
1216 }
1217 }
1218
1219 /* Wait for data to be available */
1220 qpic_nand_wait_for_data(DATA_CONSUMER_PIPE_INDEX);
1221
1222 return nand_ret;
1223}
1224
1225static int
1226qpic_nand_mark_badblock(uint32_t page)
1227{
1228 char empty_buf[NAND_CW_SIZE_8_BIT_ECC];
1229
1230 memset(empty_buf, 0, NAND_CW_SIZE_8_BIT_ECC);
1231
1232 /* Going to first page of the block */
1233 if (page & flash.num_pages_per_blk_mask)
1234 page = page - (page & flash.num_pages_per_blk_mask);
1235
1236 return qpic_nand_write_page(page, NAND_CFG_RAW, empty_buf, 0);
1237}
1238
1239static void
1240qpic_nand_non_onfi_probe(struct flash_info *flash)
1241{
1242 int dev_found = 0;
1243 unsigned index;
1244 uint32_t ecc_bits;
1245
1246 /* Read the nand id. */
1247 qpic_nand_fetch_id(flash);
1248
1249 /* Check if we support the device */
Deepa Dinamani649a94a2013-03-07 14:37:31 -08001250 for (index = 0; index < (ARRAY_SIZE(supported_flash)); index++)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001251 {
Sridhar Parasuramfb0d9c82015-02-02 15:23:13 -08001252 if (((flash->id & supported_flash[index].mask) ==
1253 (supported_flash[index].flash_id & (supported_flash[index].mask))) &&
1254 ((flash->id2 & supported_flash[index].mask2) ==
1255 (supported_flash[index].flash_id2 & (supported_flash[index].mask2))))
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001256 {
1257 dev_found = 1;
1258 break;
1259 }
1260 }
1261
1262 if (dev_found)
1263 {
1264 flash->page_size = supported_flash[index].pagesize;
1265 flash->block_size = supported_flash[index].blksize;
1266 flash->spare_size = supported_flash[index].oobsize;
1267 ecc_bits = supported_flash[index].ecc_8_bits;
1268
1269 /* Make sure that the block size and page size are defined. */
1270 ASSERT(flash->block_size);
1271 ASSERT(flash->page_size);
1272
1273 flash->num_blocks = supported_flash[index].density;
1274 flash->num_blocks /= (flash->block_size);
1275 flash->num_pages_per_blk = flash->block_size / flash->page_size;
1276 flash->num_pages_per_blk_mask = flash->num_pages_per_blk - 1;
1277
1278 /* Look for 8bit BCH ECC Nand, TODO: ECC Correctability >= 8 */
1279 if (ecc_bits)
1280 flash->ecc_width = NAND_WITH_8_BIT_ECC;
1281 else
1282 flash->ecc_width = NAND_WITH_4_BIT_ECC;
1283
1284 flash->density = supported_flash[index].density;
1285 flash->widebus = supported_flash[index].widebus;
1286
1287 return;
1288 }
1289
1290 /* Flash device is not supported, print flash device info and halt */
1291 if (dev_found == 0)
1292 {
1293 dprintf(CRITICAL, "NAND device is not supported: nandid: 0x%x"
1294 "maker=0x%02x device=0x%02x\n",
1295 flash->id,
1296 flash->vendor,
1297 flash->device);
1298 ASSERT(0);
1299 }
1300
1301 dprintf(INFO, "nandid: 0x%x maker=0x%02x device=0x%02x page_size=%d\n",
1302 flash->id,
1303 flash->vendor,
1304 flash->device,
1305 flash->page_size);
1306
1307 dprintf(INFO, "spare_size=%d block_size=%d num_blocks=%d\n",
1308 flash->spare_size,
1309 flash->block_size,
1310 flash->num_blocks);
1311}
1312
1313void
1314qpic_nand_init(struct qpic_nand_init_config *config)
1315{
1316 uint32_t i;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001317
1318 nand_base = config->nand_base;
1319
Deepa Dinamanie9ded132012-11-27 15:03:38 -08001320 qpic_bam_init(config);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001321
Deepa Dinamani649a94a2013-03-07 14:37:31 -08001322 qpic_nand_non_onfi_probe(&flash);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001323
1324 /* Save the RAW and read/write configs */
1325 qpic_nand_save_config(&flash);
1326
1327 flash_spare_bytes = (unsigned char *)malloc(flash.spare_size);
1328
1329 if (flash_spare_bytes == NULL)
1330 {
1331 dprintf(CRITICAL, "Failed to allocate memory for spare bytes\n");
1332 return;
1333 }
1334
1335 /* Create a bad block table */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001336 bbtbl = (uint8_t *) malloc(sizeof(uint8_t) * flash.num_blocks);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001337
1338 if (bbtbl == NULL)
1339 {
1340 dprintf(CRITICAL, "Failed to allocate memory for bad block table\n");
1341 return;
1342 }
1343
1344 for (i = 0; i < flash.num_blocks; i++)
1345 bbtbl[i] = NAND_BAD_BLK_VALUE_NOT_READ;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001346
1347 /* Set aside contiguous memory for reads/writes.
1348 * This is needed as the BAM transfers only work with
1349 * physically contiguous buffers.
1350 * We will copy any data to be written/ to be read from
1351 * nand to this buffer and this buffer will be submitted to BAM.
1352 */
1353 rdwr_buf = (uint8_t*) malloc(flash.page_size + flash.spare_size);
1354
1355 if (rdwr_buf == NULL)
1356 {
1357 dprintf(CRITICAL, "Failed to allocate memory for page reads or writes\n");
1358 return;
1359 }
1360
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001361}
1362
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001363unsigned
1364flash_page_size(void)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001365{
1366 return flash.page_size;
1367}
1368
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001369unsigned
1370flash_block_size(void)
1371{
1372 return flash.block_size;
1373}
1374
Deepa Dinamani8e6b2432012-10-17 17:12:44 -07001375unsigned
1376flash_num_blocks(void)
1377{
1378 return flash.num_blocks;
1379}
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001380
Tanya Brokhman72b44dc2015-01-07 10:20:05 +02001381unsigned
Gaurav Nebhwani64092f22016-05-20 13:58:21 +05301382flash_num_pages_per_blk(void)
1383{
1384 return flash.num_pages_per_blk;
1385}
1386
1387unsigned
Tanya Brokhman72b44dc2015-01-07 10:20:05 +02001388flash_spare_size(void)
1389{
1390 return flash.spare_size;
1391}
1392
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001393struct ptable *
1394flash_get_ptable(void)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001395{
1396 return flash_ptable;
1397}
1398
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001399void
Deepa Dinamani87feab82012-10-04 14:28:05 -07001400qpic_nand_uninit()
1401{
1402 bam_pipe_reset(&bam, DATA_PRODUCER_PIPE_INDEX);
1403 bam_pipe_reset(&bam, DATA_CONSUMER_PIPE_INDEX);
1404 bam_pipe_reset(&bam, CMD_PIPE_INDEX);
1405
1406}
1407void
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001408flash_set_ptable(struct ptable *new_ptable)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001409{
1410 ASSERT(flash_ptable == NULL && new_ptable != NULL);
1411 flash_ptable = new_ptable;
1412}
1413
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -07001414static int find_num_zeros_per_cw(uint8_t *ecc_buf, uint32_t ecc_bytes)
1415{
1416 uint8_t val;
1417 uint32_t i;
1418 int num_zeros = 0;
1419
1420 for (i = 0; i < ecc_bytes; i++)
1421 {
1422 val = ecc_buf[i];
1423 while (val)
1424 {
1425 if ((val & 1) == 0)
1426 num_zeros++;
1427 if (num_zeros > THRESHOLD_BIT_FLIPS)
1428 goto out;
1429 val >>= 1;
1430 }
1431 }
1432
1433out:
1434 return num_zeros;
1435}
1436
1437static int qpic_nand_read_erased_page(uint32_t page)
1438{
1439 struct cfg_params params;
1440 uint32_t ecc;
1441 uint32_t flash_sts[QPIC_NAND_MAX_CWS_IN_PAGE];
1442 uint32_t buffer_sts[QPIC_NAND_MAX_CWS_IN_PAGE];
1443 uint32_t addr_loc_0;
1444 uint32_t total_ecc_bytes = 0;
1445 struct cmd_element *cmd_list_ptr = ce_array;
1446 struct cmd_element *cmd_list_ptr_start = ce_array;
1447 uint32_t num_cmd_desc = 0;
1448 uint32_t num_data_desc = 0;
1449 uint32_t i;
1450 int nand_ret = NANDC_RESULT_SUCCESS;
1451 uint8_t flags = 0;
1452 uint32_t *cmd_list_temp = NULL;
1453 uint8_t *ecc_buf = NULL;
1454 uint8_t *ecc_temp = NULL;
1455 int num_zeros = 0;
1456#if DEBUG_QPIC_NAND
1457 uint32_t *buffer_temp = NULL;
1458#endif
1459
1460 total_ecc_bytes = (ecc_parity_bytes * flash.cws_per_page);
1461 ecc_buf = memalign(16, total_ecc_bytes);
1462 ASSERT(ecc_buf);
1463
1464 memset(ecc_buf, 0, total_ecc_bytes);
1465
1466 ecc_temp = ecc_buf;
1467#if DEBUG_QPIC_NAND
1468 buffer_temp = (uint32_t*)ecc_buf;
1469#endif
1470 params.addr0 = page << 16;
1471 params.addr1 = (page >> 16) & 0xff;
1472 params.cfg0 = cfg0_raw;
1473 params.cfg1 = cfg1_raw;
1474 params.cmd = NAND_CMD_PAGE_READ;
1475 params.exec = 1;
1476 ecc = ecc_cfg_raw;
1477
1478 /* Read all the Data bytes in the first 3 CWs. */
1479 addr_loc_0 = NAND_RD_LOC_OFFSET(517);
1480 addr_loc_0 |= NAND_RD_LOC_SIZE(ecc_parity_bytes);
1481 addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
1482
1483 /* Queue up the command and data descriptors for all the codewords in a page
1484 * and do a single bam transfer at the end.*/
1485 for (i = 0; i < flash.cws_per_page; i++)
1486 {
1487 num_cmd_desc = 0;
1488 num_data_desc = 0;
1489 flags = 0;
1490
1491 if (i == 0)
1492 {
1493 /* Set the lock flag for the first CW */
1494 flags = BAM_DESC_LOCK_FLAG;
1495
1496 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(&params, cmd_list_ptr);
1497
1498 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG,(uint32_t)ecc, CE_WRITE_TYPE);
1499 cmd_list_ptr++;
1500
1501 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)params.cmd, CE_WRITE_TYPE);
1502 cmd_list_ptr++;
1503
1504 /* Write addr loc 0. */
1505 bam_add_cmd_element(cmd_list_ptr,
1506 NAND_READ_LOCATION_n(0),
1507 (uint32_t)addr_loc_0,
1508 CE_WRITE_TYPE);
1509
1510 cmd_list_ptr++;
1511 }
1512 else
1513 cmd_list_ptr_start = cmd_list_ptr;
1514
1515 if (i == flash.cws_per_page - 1)
1516 flags = BAM_DESC_INT_FLAG;
1517
1518 /* Add Data desc */
1519 bam_add_one_desc(&bam,
1520 DATA_PRODUCER_PIPE_INDEX,
1521 (unsigned char *)PA((addr_t)ecc_temp),
1522 ecc_parity_bytes,
1523 flags);
1524 num_data_desc++;
1525 bam_sys_gen_event(&bam, DATA_PRODUCER_PIPE_INDEX, num_data_desc);
1526
1527 bam_add_cmd_element(cmd_list_ptr,
1528 NAND_EXEC_CMD,
1529 (uint32_t)params.exec,
1530 CE_WRITE_TYPE);
1531 cmd_list_ptr++;
1532
1533 /* Enqueue the desc for the above commands */
1534 bam_add_one_desc(&bam,
1535 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +05301536 (unsigned char*)PA((addr_t)cmd_list_ptr_start),
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -07001537 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
1538 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG | flags);
1539 num_cmd_desc++;
1540
1541 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_STATUS, (uint32_t)PA((addr_t)&(flash_sts[i])), CE_READ_TYPE);
1542
1543 cmd_list_temp = (uint32_t *)cmd_list_ptr;
1544
1545 cmd_list_ptr++;
1546
1547 bam_add_cmd_element(cmd_list_ptr, NAND_BUFFER_STATUS, (uint32_t)PA((addr_t)&(buffer_sts[i])), CE_READ_TYPE);
1548 cmd_list_ptr++;
1549
1550 if (i == flash.cws_per_page - 1)
1551 {
1552 /* Unlock flag for the last CW */
1553 flags = BAM_DESC_CMD_FLAG | BAM_DESC_UNLOCK_FLAG;
1554 }
1555 else
1556 flags = BAM_DESC_CMD_FLAG;
1557
1558 /* Enqueue the desc for the above command */
1559 bam_add_one_desc(&bam,
1560 CMD_PIPE_INDEX,
1561 (unsigned char*)PA((addr_t)cmd_list_temp),
1562 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_temp),
1563 flags);
1564 num_cmd_desc++;
1565
1566 ecc_temp += ecc_parity_bytes;
1567
1568 /* Notify BAM HW about the newly added descriptors */
1569 bam_sys_gen_event(&bam, CMD_PIPE_INDEX, num_cmd_desc);
1570 }
1571
1572 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
1573
1574 /* Find number of bit flips in the ecc & if there are more than "threshold" bit flips then
1575 * the page is bad otherwise the page is erased page
1576 */
1577 ecc_temp = ecc_buf;
1578
1579 for (i = 0; i < flash.cws_per_page; i++)
1580 {
1581 num_zeros = find_num_zeros_per_cw(ecc_temp, ecc_parity_bytes);
1582
1583 if (num_zeros > THRESHOLD_BIT_FLIPS)
1584 {
1585 nand_ret = NANDC_RESULT_BAD_PAGE;
1586 goto qpic_nand_read_page_error;
1587 }
1588
1589 ecc_temp += ecc_parity_bytes;
1590 }
1591
1592qpic_nand_read_page_error:
1593
1594#if DEBUG_QPIC_NAND
1595 for(i = 0; i < 24; i += 8)
1596 {
1597 printf("ECC: %08x %08x %08x %08x %08x %08x %08x %08x\n",
1598 buffer_temp[i], buffer_temp[i+1], buffer_temp[i+2], buffer_temp[i+3],
1599 buffer_temp[i+4], buffer_temp[i+5], buffer_temp[i+6], buffer_temp[i+7]);
1600 }
1601 printf("ECC: %08x %08x\n", buffer_temp[24], buffer_temp[25]);
1602#endif
1603
1604 free(ecc_buf);
1605 return nand_ret;
1606}
1607
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001608/* Note: No support for raw reads. */
1609static int
1610qpic_nand_read_page(uint32_t page, unsigned char* buffer, unsigned char* spareaddr)
1611{
1612 struct cfg_params params;
1613 uint32_t ecc;
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -07001614 uint32_t flash_sts[QPIC_NAND_MAX_CWS_IN_PAGE] = {0};
1615 uint32_t buffer_sts[QPIC_NAND_MAX_CWS_IN_PAGE] = {0};
1616 uint32_t erased_cw_sts[QPIC_NAND_MAX_CWS_IN_PAGE] = {0};
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001617 uint32_t addr_loc_0;
1618 uint32_t addr_loc_1;
1619 struct cmd_element *cmd_list_ptr = ce_array;
1620 struct cmd_element *cmd_list_ptr_start = ce_array;
1621 uint32_t num_cmd_desc = 0;
1622 uint32_t num_data_desc = 0;
1623 uint32_t status;
1624 uint32_t i;
1625 int nand_ret = NANDC_RESULT_SUCCESS;
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001626 uint8_t flags = 0;
1627 uint32_t *cmd_list_temp = NULL;
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -07001628#if DEBUG_QPIC_NAND
1629 uint8_t *buffer_temp = buffer;
1630#endif
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001631
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001632 /* UD bytes in last CW is 512 - cws_per_page *4.
1633 * Since each of the CW read earlier reads 4 spare bytes.
1634 */
1635 uint16_t ud_bytes_in_last_cw = USER_DATA_BYTES_PER_CW - ((flash.cws_per_page - 1) << 2);
1636 uint16_t oob_bytes = DATA_BYTES_IN_IMG_PER_CW - ud_bytes_in_last_cw;
1637
1638 params.addr0 = page << 16;
1639 params.addr1 = (page >> 16) & 0xff;
1640 params.cfg0 = cfg0;
1641 params.cfg1 = cfg1;
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -07001642 params.cmd = NAND_CMD_PAGE_READ_ECC;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001643 params.exec = 1;
1644 ecc = ecc_bch_cfg;
1645
1646 /* Read all the Data bytes in the first 3 CWs. */
1647 addr_loc_0 = NAND_RD_LOC_OFFSET(0);
1648 addr_loc_0 |= NAND_RD_LOC_SIZE(DATA_BYTES_IN_IMG_PER_CW);
1649 addr_loc_0 |= NAND_RD_LOC_LAST_BIT(1);
1650
1651
1652 addr_loc_1 = NAND_RD_LOC_OFFSET(ud_bytes_in_last_cw);
1653 addr_loc_1 |= NAND_RD_LOC_SIZE(oob_bytes);
1654 addr_loc_1 |= NAND_RD_LOC_LAST_BIT(1);
1655
Deepa Dinamanidc1381e2012-11-15 14:53:24 -08001656 status = qpic_nand_block_isbad(page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001657
1658 if (status)
1659 return status;
1660
sundarajan srinivasan7ced6482013-03-21 16:01:18 -07001661 /* Reset and Configure erased CW/page detection controller */
1662 qpic_nand_erased_status_reset(ce_array, BAM_DESC_LOCK_FLAG);
1663
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001664 /* Queue up the command and data descriptors for all the codewords in a page
1665 * and do a single bam transfer at the end.*/
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001666 for (i = 0; i < flash.cws_per_page; i++)
1667 {
1668 num_cmd_desc = 0;
1669 num_data_desc = 0;
1670
1671 if (i == 0)
1672 {
1673 cmd_list_ptr = qpic_nand_add_addr_n_cfg_ce(&params, cmd_list_ptr);
1674
1675 bam_add_cmd_element(cmd_list_ptr, NAND_DEV0_ECC_CFG,(uint32_t)ecc, CE_WRITE_TYPE);
1676 cmd_list_ptr++;
1677 }
1678 else
1679 cmd_list_ptr_start = cmd_list_ptr;
1680
1681 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_CMD, (uint32_t)params.cmd, CE_WRITE_TYPE);
1682 cmd_list_ptr++;
1683
1684 if (i == flash.cws_per_page - 1)
1685 {
1686 addr_loc_0 = NAND_RD_LOC_OFFSET(0);
1687 addr_loc_0 |= NAND_RD_LOC_SIZE(ud_bytes_in_last_cw);
1688 addr_loc_0 |= NAND_RD_LOC_LAST_BIT(0);
1689
1690 /* Write addr loc 1 only for the last CW. */
1691 bam_add_cmd_element(cmd_list_ptr, NAND_READ_LOCATION_n(1), (uint32_t)addr_loc_1, CE_WRITE_TYPE);
1692 cmd_list_ptr++;
1693
1694 /* Add Data desc */
1695 bam_add_one_desc(&bam,
1696 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001697 (unsigned char *)PA((addr_t)buffer),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001698 ud_bytes_in_last_cw,
1699 0);
1700 num_data_desc++;
1701
1702 bam_add_one_desc(&bam,
1703 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001704 (unsigned char *)PA((addr_t)spareaddr),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001705 oob_bytes,
1706 BAM_DESC_INT_FLAG);
1707 num_data_desc++;
1708
1709 bam_sys_gen_event(&bam, DATA_PRODUCER_PIPE_INDEX, num_data_desc);
1710 }
1711 else
1712 {
1713 /* Add Data desc */
1714 bam_add_one_desc(&bam,
1715 DATA_PRODUCER_PIPE_INDEX,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001716 (unsigned char *)PA((addr_t)buffer),
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001717 DATA_BYTES_IN_IMG_PER_CW,
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001718 0);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001719 num_data_desc++;
1720 bam_sys_gen_event(&bam, DATA_PRODUCER_PIPE_INDEX, num_data_desc);
1721 }
1722
1723 /* Write addr loc 0. */
1724 bam_add_cmd_element(cmd_list_ptr,
1725 NAND_READ_LOCATION_n(0),
1726 (uint32_t)addr_loc_0,
1727 CE_WRITE_TYPE);
1728
1729 cmd_list_ptr++;
1730 bam_add_cmd_element(cmd_list_ptr,
1731 NAND_EXEC_CMD,
1732 (uint32_t)params.exec,
1733 CE_WRITE_TYPE);
1734 cmd_list_ptr++;
1735
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001736 /* Enqueue the desc for the above commands */
1737 bam_add_one_desc(&bam,
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001738 CMD_PIPE_INDEX,
Mayank Grover20fdd882016-10-25 16:53:30 +05301739 (unsigned char*)PA((addr_t)cmd_list_ptr_start),
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001740 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_ptr_start),
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001741 BAM_DESC_NWD_FLAG | BAM_DESC_CMD_FLAG);
1742 num_cmd_desc++;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001743
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001744 bam_add_cmd_element(cmd_list_ptr, NAND_FLASH_STATUS, (uint32_t)PA((addr_t)&(flash_sts[i])), CE_READ_TYPE);
1745
vijay kumar4f4405f2014-08-08 11:49:53 +05301746 cmd_list_temp = (uint32_t *)cmd_list_ptr;
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001747
1748 cmd_list_ptr++;
1749
1750 bam_add_cmd_element(cmd_list_ptr, NAND_BUFFER_STATUS, (uint32_t)PA((addr_t)&(buffer_sts[i])), CE_READ_TYPE);
1751 cmd_list_ptr++;
1752
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -07001753 /* Read erased CW status */
1754 bam_add_cmd_element(cmd_list_ptr, NAND_ERASED_CW_DETECT_STATUS, (uint32_t)PA((addr_t)&erased_cw_sts[i]), CE_READ_TYPE);
1755 cmd_list_ptr++;
1756
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001757 if (i == flash.cws_per_page - 1)
1758 {
1759 flags = BAM_DESC_CMD_FLAG | BAM_DESC_UNLOCK_FLAG;
1760 }
1761 else
1762 flags = BAM_DESC_CMD_FLAG;
1763
1764 /* Enqueue the desc for the above command */
1765 bam_add_one_desc(&bam,
1766 CMD_PIPE_INDEX,
1767 (unsigned char*)PA((addr_t)cmd_list_temp),
1768 PA((uint32_t)cmd_list_ptr - (uint32_t)cmd_list_temp),
1769 flags);
1770 num_cmd_desc++;
1771
1772 buffer += DATA_BYTES_IN_IMG_PER_CW;
1773
1774 /* Notify BAM HW about the newly added descriptors */
1775 bam_sys_gen_event(&bam, CMD_PIPE_INDEX, num_cmd_desc);
1776 }
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001777
1778 qpic_nand_wait_for_data(DATA_PRODUCER_PIPE_INDEX);
1779
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -07001780 /* Check flash read status & errors */
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001781 for (i = 0; i < flash.cws_per_page ; i ++)
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001782 {
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -07001783#if DEBUG_QPIC_NAND
1784 dprintf(INFO, "FLASH STATUS: 0x%08x, BUFFER STATUS: 0x%08x, ERASED CW STATUS: 0x%08x\n",
1785 flash_sts[i], buffer_sts[i], erased_cw_sts[i]);
1786#endif
1787
1788 /* If MPU or flash op erros are set, look for erased cw status.
1789 * If erased CW status is not set then look for bit flips to confirm
1790 * if the page is and erased page or a bad page
1791 */
1792 if (flash_sts[i] & (NAND_FLASH_OP_ERR | NAND_FLASH_MPU_ERR))
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001793 {
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -07001794 if ((erased_cw_sts[i] & NAND_ERASED_CW) != NAND_ERASED_CW)
1795 {
1796#if DEBUG_QPIC_NAND
1797 dprintf(CRITICAL, "Page: 0x%08x, addr0: 0x%08x, addr1: 0x%08x\n", page, params.addr0, params.addr1);
1798#endif
1799 /*
1800 * Depending on the process technology used there could be bit flips on
1801 * pages on the NAND card
1802 * When any page is erased the controller fills the page with all 1's.
1803 * When we try to read from an erased page and there are bit flips the
1804 * controller would not detect the page as erased page instead throws
1805 * an uncorrectable ecc error.
1806 * The NAND data sheet for that card would specify the number of bit flips
1807 * expected per code word. If the number of bit flips is less than expected
1808 * bit flips then we should ignore the uncorrectable ECC error and consider
1809 * the page as an erased page.
1810 */
1811#if DEBUG_QPIC_NAND
1812 for(i = 0; i < 4096; i += 8)
1813 {
1814 printf("DATA: %x %x %x %x %x %x %x %x",
1815 buffer_temp[i], buffer_temp[i+1], buffer_temp[i+2], buffer_temp[i+3],
1816 buffer_temp[i+4], buffer_temp[i+5], buffer_temp[i+6], buffer_temp[i+7]);
1817 i += 8;
1818 printf("DATA: %x %x %x %x %x %x %x %x\n",
1819 buffer_temp[i], buffer_temp[i+1], buffer_temp[i+2], buffer_temp[i+3],
1820 buffer_temp[i+4], buffer_temp[i+5], buffer_temp[i+6], buffer_temp[i+7]);
1821 }
1822#endif
1823 nand_ret = qpic_nand_read_erased_page(page);
1824 goto qpic_nand_read_page_error;
1825 }
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001826 }
Sundarajan Srinivasan024bda52014-02-27 16:48:48 -08001827 }
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -07001828
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001829qpic_nand_read_page_error:
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -07001830 return nand_ret;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001831}
1832
Tanya Brokhman72b44dc2015-01-07 10:20:05 +02001833/**
1834 * qpic_nand_read() - read data
1835 * @start_page: number of page to begin reading from
1836 * @num_pages: number of pages to read
1837 * @buffer: buffer where to store the read data
1838 * @spareaddr: buffer where to store spare data.
1839 * If null, spare data wont be read
1840 *
1841 * This function reads @num_pages starting from @start_page and stores the
1842 * read data in buffer. Note that it's in the caller responsibility to make
1843 * sure the read pages are all from same partition.
1844 *
1845 * Returns nand_result_t
1846 */
1847nand_result_t qpic_nand_read(uint32_t start_page, uint32_t num_pages,
1848 unsigned char* buffer, unsigned char* spareaddr)
1849{
1850 unsigned i = 0, ret = 0;
1851
1852 if (!buffer) {
1853 dprintf(CRITICAL, "qpic_nand_read: buffer = null\n");
1854 return NANDC_RESULT_PARAM_INVALID;
1855 }
1856 while (i < num_pages) {
1857 ret = qpic_nand_read_page(start_page + i, buffer + flash.page_size * i,
1858 spareaddr);
1859 i++;
1860 if (ret == NANDC_RESULT_BAD_PAGE)
1861 qpic_nand_mark_badblock(start_page + i);
1862 if (ret) {
1863 dprintf(CRITICAL,
1864 "qpic_nand_read: reading page %d failed with %d err\n",
1865 start_page + i, ret);
1866 return ret;
1867 }
1868 }
1869 return NANDC_RESULT_SUCCESS;
1870}
1871
1872/**
1873 * qpic_nand_write() - read data
1874 * @start_page: number of page to begin writing to
1875 * @num_pages: number of pages to write
1876 * @buffer: buffer to be written
1877 * @write_extra_bytes: true if spare data (ox 0xff) to be written
1878 *
1879 * This function writes @num_pages starting from @start_page. Note that it's
1880 * in the caller responsibility to make sure the written pages are all from
1881 * same partition.
1882 *
1883 * Returns nand_result_t
1884 */
1885nand_result_t qpic_nand_write(uint32_t start_page, uint32_t num_pages,
1886 unsigned char* buffer, unsigned write_extra_bytes)
1887{
1888 int i = 0, ret = NANDC_RESULT_SUCCESS;
1889 uint32_t *spare = (unsigned *)flash_spare_bytes;
1890 uint32_t wsize;
1891 uint32_t spare_byte_count = 0;
1892
1893 if (!buffer) {
1894 dprintf(CRITICAL, "qpic_nand_write: buffer = null\n");
1895 return NANDC_RESULT_PARAM_INVALID;
1896 }
1897 spare_byte_count = ((flash.cw_size * flash.cws_per_page)- flash.page_size);
1898
1899 if (write_extra_bytes)
1900 wsize = flash.page_size + spare_byte_count;
1901 else
1902 wsize = flash.page_size;
1903
1904 memset(spare, 0xff, (spare_byte_count / flash.cws_per_page));
1905
1906 for (i = 0; i < (int)num_pages; i++) {
1907 memcpy(rdwr_buf, buffer, flash.page_size);
1908 if (write_extra_bytes) {
1909 memcpy(rdwr_buf + flash.page_size,
1910 buffer + flash.page_size, spare_byte_count);
1911 ret = qpic_nand_write_page(start_page + i,
1912 NAND_CFG, rdwr_buf, rdwr_buf + flash.page_size);
1913 } else {
1914 ret = qpic_nand_write_page(start_page + i,
1915 NAND_CFG, rdwr_buf, spare);
1916 }
1917 if (ret) {
1918 dprintf(CRITICAL,
1919 "flash_write: write failure @ page %d, block %d\n",
1920 start_page + i,
1921 (start_page + i) / flash.num_pages_per_blk);
1922 if (ret == NANDC_RESULT_BAD_PAGE)
1923 qpic_nand_mark_badblock(start_page + i);
1924 goto out;
1925 }
1926 buffer += wsize;
1927 }
1928out:
1929 return ret;
1930}
1931
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001932/* Function to read a flash partition.
1933 * ptn : Partition to read.
1934 * extra_per_page : Spare data to be read.
1935 * offset : Num of bytes offset into the partition.
1936 * data : Buffer to read the data into.
1937 * bytes : Num of bytes to be read.
1938 */
1939 /* TODO: call this func read_partition. */
1940int
1941flash_read_ext(struct ptentry *ptn,
1942 unsigned extra_per_page,
1943 unsigned offset,
1944 void *data,
1945 unsigned bytes)
1946{
1947 uint32_t page =
1948 (ptn->start * flash.num_pages_per_blk) + (offset / flash.page_size);
1949 uint32_t lastpage = (ptn->start + ptn->length) * flash.num_pages_per_blk;
1950 uint32_t count =
1951 (bytes + flash.page_size - 1 + extra_per_page) / (flash.page_size +
1952 extra_per_page);
1953 uint32_t *spare = (unsigned *)flash_spare_bytes;
1954 uint32_t errors = 0;
1955 unsigned char *image = data;
1956 int result = 0;
1957 uint32_t current_block =
1958 (page - (page & flash.num_pages_per_blk_mask)) / flash.num_pages_per_blk;
1959 uint32_t start_block = ptn->start;
1960 uint32_t start_block_count = 0;
1961 uint32_t isbad = 0;
Deepa Dinamani2f7006c2013-08-19 11:59:38 -07001962 uint32_t current_page;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001963
1964 /* Verify first byte is at page boundary. */
1965 if (offset & (flash.page_size - 1))
1966 {
1967 dprintf(CRITICAL, "Read request start not at page boundary: %d\n",
1968 offset);
1969 return NANDC_RESULT_PARAM_INVALID;
1970 }
1971
Deepa Dinamani2f7006c2013-08-19 11:59:38 -07001972 current_page = start_block * flash.num_pages_per_blk;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001973 /* Adjust page offset based on number of bad blocks from start to current page */
1974 if (start_block < current_block)
1975 {
1976 start_block_count = (current_block - start_block);
1977 while (start_block_count
1978 && (start_block < (ptn->start + ptn->length)))
1979 {
Deepa Dinamani2f7006c2013-08-19 11:59:38 -07001980 isbad = qpic_nand_block_isbad(current_page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001981 if (isbad)
1982 page += flash.num_pages_per_blk;
1983 else
1984 start_block_count--;
1985 start_block++;
Deepa Dinamani2f7006c2013-08-19 11:59:38 -07001986 current_page += flash.num_pages_per_blk;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001987 }
1988 }
1989
1990 while ((page < lastpage) && !start_block_count)
1991 {
1992 if (count == 0)
1993 {
Deepa Dinamani52aca8d2013-02-05 11:41:41 -08001994 dprintf(SPEW, "flash_read_image: success (%d errors)\n",
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001995 errors);
1996 return NANDC_RESULT_SUCCESS;
1997 }
1998
Sundarajan Srinivasan29d927c2014-02-10 14:11:24 -08001999#if CONTIGUOUS_MEMORY
2000 result = qpic_nand_read_page(page, image, (unsigned char *) spare);
2001#else
2002 result = qpic_nand_read_page(page, rdwr_buf, (unsigned char *) spare);
2003#endif
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002004 if (result == NANDC_RESULT_BAD_PAGE)
2005 {
2006 /* bad page, go to next page. */
2007 page++;
2008 errors++;
2009 continue;
2010 }
2011 else if (result == NANDC_RESULT_BAD_BLOCK)
2012 {
2013 /* bad block, go to next block same offset. */
2014 page += flash.num_pages_per_blk;
2015 errors++;
2016 continue;
2017 }
2018
Sundarajan Srinivasan29d927c2014-02-10 14:11:24 -08002019#ifndef CONTIGUOUS_MEMORY
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002020 /* Copy the read page into correct location. */
2021 memcpy(image, rdwr_buf, flash.page_size);
Sundarajan Srinivasan29d927c2014-02-10 14:11:24 -08002022#endif
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002023 page++;
2024 image += flash.page_size;
2025 /* Copy spare bytes to image */
Sundarajan Srinivasan29d927c2014-02-10 14:11:24 -08002026 if(extra_per_page)
2027 {
2028 memcpy(image, spare, extra_per_page);
2029 image += extra_per_page;
2030 }
2031
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002032 count -= 1;
2033 }
2034
2035 /* could not find enough valid pages before we hit the end */
2036 dprintf(CRITICAL, "flash_read_image: failed (%d errors)\n", errors);
2037 return NANDC_RESULT_FAILURE;
2038}
2039
2040int
2041flash_erase(struct ptentry *ptn)
2042{
Tanya Brokhman646eedb2015-01-06 23:09:54 +02002043 int ret = 0, i;
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07002044
Tanya Brokhman646eedb2015-01-06 23:09:54 +02002045 for (i = 0; i < (int)ptn->length; i++) {
2046 ret = qpic_nand_blk_erase((ptn->start + i) * flash.num_pages_per_blk);
2047 if (ret)
2048 dprintf(CRITICAL, "Erase operation failed @ page #%d\n",
2049 ptn->start + i);
2050 }
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07002051 return ret;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002052}
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07002053
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002054int
2055flash_ecc_bch_enabled()
2056{
2057 return (flash.ecc_width == NAND_WITH_4_BIT_ECC)? 0 : 1;
2058}
2059
2060int
2061flash_write(struct ptentry *ptn,
Deepa Dinamanic13d5942013-04-30 15:48:53 -07002062 unsigned write_extra_bytes,
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002063 const void *data,
2064 unsigned bytes)
2065{
2066 uint32_t page = ptn->start * flash.num_pages_per_blk;
2067 uint32_t lastpage = (ptn->start + ptn->length) * flash.num_pages_per_blk;
2068 uint32_t *spare = (unsigned *)flash_spare_bytes;
2069 const unsigned char *image = data;
Deepa Dinamanic13d5942013-04-30 15:48:53 -07002070 uint32_t wsize;
Sundarajan Srinivasan223d7002014-02-11 19:48:11 -08002071 uint32_t spare_byte_count = 0;
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002072 int r;
2073
Sundarajan Srinivasan223d7002014-02-11 19:48:11 -08002074 spare_byte_count = ((flash.cw_size * flash.cws_per_page)- flash.page_size);
2075
Deepa Dinamanic13d5942013-04-30 15:48:53 -07002076 if(write_extra_bytes)
Sundarajan Srinivasan223d7002014-02-11 19:48:11 -08002077 wsize = flash.page_size + spare_byte_count;
Deepa Dinamanic13d5942013-04-30 15:48:53 -07002078 else
2079 wsize = flash.page_size;
2080
Sundarajan Srinivasan223d7002014-02-11 19:48:11 -08002081 memset(spare, 0xff, (spare_byte_count / flash.cws_per_page));
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002082
2083 while (bytes > 0)
2084 {
2085 if (bytes < wsize)
2086 {
2087 dprintf(CRITICAL,
2088 "flash_write_image: image undersized (%d < %d)\n",
2089 bytes,
2090 wsize);
2091 return -1;
2092 }
2093
2094 if (page >= lastpage)
2095 {
2096 dprintf(CRITICAL, "flash_write_image: out of space\n");
2097 return -1;
2098 }
2099
2100 if ((page & flash.num_pages_per_blk_mask) == 0)
2101 {
Deepa Dinamani2467bbb2012-10-02 13:59:58 -07002102 if (qpic_nand_blk_erase(page))
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002103 {
2104 dprintf(INFO,
2105 "flash_write_image: bad block @ %d\n",
2106 page / flash.num_pages_per_blk);
2107
2108 page += flash.num_pages_per_blk;
2109 continue;
2110 }
2111 }
2112
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002113 memcpy(rdwr_buf, image, flash.page_size);
2114
Deepa Dinamanic13d5942013-04-30 15:48:53 -07002115 if (write_extra_bytes)
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002116 {
Sundarajan Srinivasan223d7002014-02-11 19:48:11 -08002117 memcpy(rdwr_buf + flash.page_size, image + flash.page_size, spare_byte_count);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002118 r = qpic_nand_write_page(page,
2119 NAND_CFG,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002120 rdwr_buf,
2121 rdwr_buf + flash.page_size);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002122 }
2123 else
2124 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002125 r = qpic_nand_write_page(page, NAND_CFG, rdwr_buf, spare);
Deepa Dinamanie4573be2012-08-03 16:32:29 -07002126 }
2127
2128 if (r)
2129 {
2130 dprintf(INFO,
2131 "flash_write_image: write failure @ page %d (src %d)\n",
2132 page,
2133 image - (const unsigned char *)data);
2134
2135 image -= (page & flash.num_pages_per_blk_mask) * wsize;
2136 bytes += (page & flash.num_pages_per_blk_mask) * wsize;
2137 page &= ~flash.num_pages_per_blk_mask;
2138 if (qpic_nand_blk_erase(page))
2139 {
2140 dprintf(INFO,
2141 "flash_write_image: erase failure @ page %d\n",
2142 page);
2143 }
2144
2145 qpic_nand_mark_badblock(page);
2146
2147 dprintf(INFO,
2148 "flash_write_image: restart write @ page %d (src %d)\n",
2149 page, image - (const unsigned char *)data);
2150
2151 page += flash.num_pages_per_blk;
2152 continue;
2153 }
2154 page++;
2155 image += wsize;
2156 bytes -= wsize;
2157 }
2158
2159 /* erase any remaining pages in the partition */
2160 page = (page + flash.num_pages_per_blk_mask) & (~flash.num_pages_per_blk_mask);
2161
2162 while (page < lastpage)
2163 {
2164 if (qpic_nand_blk_erase(page))
2165 {
2166 dprintf(INFO, "flash_write_image: bad block @ %d\n",
2167 page / flash.num_pages_per_blk);
2168 }
2169 page += flash.num_pages_per_blk;
2170 }
2171
2172 dprintf(INFO, "flash_write_image: success\n");
2173 return 0;
2174}
Smita Ghoshf5431c62014-09-18 14:11:14 -07002175
2176uint32_t nand_device_base()
2177{
2178 return nand_base;
2179}