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