Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Gateworks Corporation |
Ye Li | cf63923 | 2020-05-04 22:08:55 +0800 | [diff] [blame^] | 4 | * Copyright 2019 NXP |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 5 | * Author: Tim Harvey <tharvey@gateworks.com> |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
| 8 | #include <nand.h> |
| 9 | #include <malloc.h> |
Shyam Saini | f63ef49 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 10 | #include <mxs_nand.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 11 | #include <linux/err.h> |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 12 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 13 | static struct mtd_info *mtd; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 14 | static struct nand_chip nand_chip; |
| 15 | |
| 16 | static void mxs_nand_command(struct mtd_info *mtd, unsigned int command, |
| 17 | int column, int page_addr) |
| 18 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 19 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 20 | u32 timeo, time_start; |
| 21 | |
| 22 | /* write out the command to the device */ |
| 23 | chip->cmd_ctrl(mtd, command, NAND_CLE); |
| 24 | |
| 25 | /* Serially input address */ |
| 26 | if (column != -1) { |
| 27 | chip->cmd_ctrl(mtd, column, NAND_ALE); |
| 28 | chip->cmd_ctrl(mtd, column >> 8, NAND_ALE); |
| 29 | } |
| 30 | if (page_addr != -1) { |
| 31 | chip->cmd_ctrl(mtd, page_addr, NAND_ALE); |
| 32 | chip->cmd_ctrl(mtd, page_addr >> 8, NAND_ALE); |
| 33 | /* One more address cycle for devices > 128MiB */ |
| 34 | if (chip->chipsize > (128 << 20)) |
| 35 | chip->cmd_ctrl(mtd, page_addr >> 16, NAND_ALE); |
| 36 | } |
| 37 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0); |
| 38 | |
| 39 | if (command == NAND_CMD_READ0) { |
| 40 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, NAND_CLE); |
| 41 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0); |
Ye Li | cf63923 | 2020-05-04 22:08:55 +0800 | [diff] [blame^] | 42 | } else if (command == NAND_CMD_RNDOUT) { |
| 43 | /* No ready / busy check necessary */ |
| 44 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 45 | NAND_NCE | NAND_CLE); |
| 46 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 47 | NAND_NCE); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /* wait for nand ready */ |
| 51 | ndelay(100); |
| 52 | timeo = (CONFIG_SYS_HZ * 20) / 1000; |
| 53 | time_start = get_timer(0); |
| 54 | while (get_timer(time_start) < timeo) { |
| 55 | if (chip->dev_ready(mtd)) |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
Jörg Krause | 7440e4b | 2018-01-14 19:26:40 +0100 | [diff] [blame] | 60 | #if defined (CONFIG_SPL_NAND_IDENT) |
| 61 | |
| 62 | /* Trying to detect the NAND flash using ONFi, JEDEC, and (extended) IDs */ |
| 63 | static int mxs_flash_full_ident(struct mtd_info *mtd) |
| 64 | { |
| 65 | int nand_maf_id, nand_dev_id; |
| 66 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 67 | struct nand_flash_dev *type; |
| 68 | |
| 69 | type = nand_get_flash_type(mtd, chip, &nand_maf_id, &nand_dev_id, NULL); |
| 70 | |
| 71 | if (IS_ERR(type)) { |
| 72 | chip->select_chip(mtd, -1); |
| 73 | return PTR_ERR(type); |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | #else |
| 80 | |
| 81 | /* Trying to detect the NAND flash using ONFi only */ |
Jörg Krause | 404a9db | 2018-01-14 19:26:39 +0100 | [diff] [blame] | 82 | static int mxs_flash_onfi_ident(struct mtd_info *mtd) |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 83 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 84 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 85 | int i; |
| 86 | u8 mfg_id, dev_id; |
| 87 | u8 id_data[8]; |
| 88 | struct nand_onfi_params *p = &chip->onfi_params; |
| 89 | |
| 90 | /* Reset the chip */ |
| 91 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 92 | |
| 93 | /* Send the command for reading device ID */ |
| 94 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 95 | |
| 96 | /* Read manufacturer and device IDs */ |
| 97 | mfg_id = chip->read_byte(mtd); |
| 98 | dev_id = chip->read_byte(mtd); |
| 99 | |
| 100 | /* Try again to make sure */ |
| 101 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 102 | for (i = 0; i < 8; i++) |
| 103 | id_data[i] = chip->read_byte(mtd); |
| 104 | if (id_data[0] != mfg_id || id_data[1] != dev_id) { |
| 105 | printf("second ID read did not match"); |
| 106 | return -1; |
| 107 | } |
| 108 | debug("0x%02x:0x%02x ", mfg_id, dev_id); |
| 109 | |
| 110 | /* read ONFI */ |
| 111 | chip->onfi_version = 0; |
| 112 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1); |
| 113 | if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' || |
| 114 | chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') { |
| 115 | return -2; |
| 116 | } |
| 117 | |
| 118 | /* we have ONFI, probe it */ |
| 119 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 120 | chip->read_buf(mtd, (uint8_t *)p, sizeof(*p)); |
| 121 | mtd->name = p->model; |
| 122 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 123 | mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize; |
| 124 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 125 | chip->chipsize = le32_to_cpu(p->blocks_per_lun); |
| 126 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 127 | /* Calculate the address shift from the page size */ |
| 128 | chip->page_shift = ffs(mtd->writesize) - 1; |
| 129 | chip->phys_erase_shift = ffs(mtd->erasesize) - 1; |
| 130 | /* Convert chipsize to number of pages per chip -1 */ |
| 131 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
| 132 | chip->badblockbits = 8; |
| 133 | |
| 134 | debug("erasesize=%d (>>%d)\n", mtd->erasesize, chip->phys_erase_shift); |
| 135 | debug("writesize=%d (>>%d)\n", mtd->writesize, chip->page_shift); |
| 136 | debug("oobsize=%d\n", mtd->oobsize); |
| 137 | debug("chipsize=%lld\n", chip->chipsize); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Jörg Krause | 7440e4b | 2018-01-14 19:26:40 +0100 | [diff] [blame] | 142 | #endif /* CONFIG_SPL_NAND_IDENT */ |
| 143 | |
Jörg Krause | 404a9db | 2018-01-14 19:26:39 +0100 | [diff] [blame] | 144 | static int mxs_flash_ident(struct mtd_info *mtd) |
| 145 | { |
| 146 | int ret; |
Jörg Krause | 7440e4b | 2018-01-14 19:26:40 +0100 | [diff] [blame] | 147 | #if defined (CONFIG_SPL_NAND_IDENT) |
| 148 | ret = mxs_flash_full_ident(mtd); |
| 149 | #else |
Jörg Krause | 404a9db | 2018-01-14 19:26:39 +0100 | [diff] [blame] | 150 | ret = mxs_flash_onfi_ident(mtd); |
Jörg Krause | 7440e4b | 2018-01-14 19:26:40 +0100 | [diff] [blame] | 151 | #endif |
Jörg Krause | 404a9db | 2018-01-14 19:26:39 +0100 | [diff] [blame] | 152 | return ret; |
| 153 | } |
| 154 | |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 155 | static int mxs_read_page_ecc(struct mtd_info *mtd, void *buf, unsigned int page) |
| 156 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 157 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 158 | int ret; |
| 159 | |
| 160 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, page); |
| 161 | ret = nand_chip.ecc.read_page(mtd, chip, buf, 1, page); |
| 162 | if (ret < 0) { |
| 163 | printf("read_page failed %d\n", ret); |
| 164 | return -1; |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int is_badblock(struct mtd_info *mtd, loff_t offs, int allowbbt) |
| 170 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 171 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 172 | unsigned int block = offs >> chip->phys_erase_shift; |
| 173 | unsigned int page = offs >> chip->page_shift; |
| 174 | |
| 175 | debug("%s offs=0x%08x block:%d page:%d\n", __func__, (int)offs, block, |
| 176 | page); |
| 177 | chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page); |
| 178 | memset(chip->oob_poi, 0, mtd->oobsize); |
| 179 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 180 | |
| 181 | return chip->oob_poi[0] != 0xff; |
| 182 | } |
| 183 | |
| 184 | /* setup mtd and nand structs and init mxs_nand driver */ |
Adam Ford | 858dd27 | 2019-02-18 17:58:17 -0600 | [diff] [blame] | 185 | void nand_init(void) |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 186 | { |
| 187 | /* return if already initalized */ |
| 188 | if (nand_chip.numchips) |
Adam Ford | 858dd27 | 2019-02-18 17:58:17 -0600 | [diff] [blame] | 189 | return; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 190 | |
| 191 | /* init mxs nand driver */ |
Stefan Agner | 7152f34 | 2018-06-22 17:19:46 +0200 | [diff] [blame] | 192 | mxs_nand_init_spl(&nand_chip); |
Boris Brezillon | 3b5f884 | 2016-06-15 20:56:10 +0200 | [diff] [blame] | 193 | mtd = nand_to_mtd(&nand_chip); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 194 | /* set mtd functions */ |
| 195 | nand_chip.cmdfunc = mxs_nand_command; |
Adam Ford | cf87371 | 2018-12-30 10:11:16 -0600 | [diff] [blame] | 196 | nand_chip.scan_bbt = nand_default_bbt; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 197 | nand_chip.numchips = 1; |
| 198 | |
| 199 | /* identify flash device */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 200 | if (mxs_flash_ident(mtd)) { |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 201 | printf("Failed to identify\n"); |
Adam Ford | 858dd27 | 2019-02-18 17:58:17 -0600 | [diff] [blame] | 202 | nand_chip.numchips = 0; /* If fail, don't use nand */ |
| 203 | return; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* allocate and initialize buffers */ |
| 207 | nand_chip.buffers = memalign(ARCH_DMA_MINALIGN, |
| 208 | sizeof(*nand_chip.buffers)); |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 209 | nand_chip.oob_poi = nand_chip.buffers->databuf + mtd->writesize; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 210 | /* setup flash layout (does not scan as we override that) */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 211 | mtd->size = nand_chip.chipsize; |
| 212 | nand_chip.scan_bbt(mtd); |
Adam Ford | 1021073 | 2019-01-02 20:36:52 -0600 | [diff] [blame] | 213 | mxs_nand_setup_ecc(mtd); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | int nand_spl_load_image(uint32_t offs, unsigned int size, void *buf) |
| 217 | { |
| 218 | struct nand_chip *chip; |
| 219 | unsigned int page; |
| 220 | unsigned int nand_page_per_block; |
| 221 | unsigned int sz = 0; |
Ye Li | cf63923 | 2020-05-04 22:08:55 +0800 | [diff] [blame^] | 222 | u8 *page_buf = NULL; |
| 223 | u32 page_off; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 224 | |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 225 | chip = mtd_to_nand(mtd); |
Adam Ford | 858dd27 | 2019-02-18 17:58:17 -0600 | [diff] [blame] | 226 | if (!chip->numchips) |
| 227 | return -ENODEV; |
Ye Li | cf63923 | 2020-05-04 22:08:55 +0800 | [diff] [blame^] | 228 | |
| 229 | page_buf = malloc(mtd->writesize); |
| 230 | if (!page_buf) |
| 231 | return -ENOMEM; |
| 232 | |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 233 | page = offs >> chip->page_shift; |
Ye Li | cf63923 | 2020-05-04 22:08:55 +0800 | [diff] [blame^] | 234 | page_off = offs & (mtd->writesize - 1); |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 235 | nand_page_per_block = mtd->erasesize / mtd->writesize; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 236 | |
Ye Li | cf63923 | 2020-05-04 22:08:55 +0800 | [diff] [blame^] | 237 | debug("%s offset:0x%08x len:%d page:%x\n", __func__, offs, size, page); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 238 | |
Ye Li | cf63923 | 2020-05-04 22:08:55 +0800 | [diff] [blame^] | 239 | while (size) { |
| 240 | if (mxs_read_page_ecc(mtd, page_buf, page) < 0) |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 241 | return -1; |
Ye Li | cf63923 | 2020-05-04 22:08:55 +0800 | [diff] [blame^] | 242 | |
| 243 | if (size > (mtd->writesize - page_off)) |
| 244 | sz = (mtd->writesize - page_off); |
| 245 | else |
| 246 | sz = size; |
| 247 | |
| 248 | memcpy(buf, page_buf + page_off, sz); |
| 249 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 250 | offs += mtd->writesize; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 251 | page++; |
Ye Li | cf63923 | 2020-05-04 22:08:55 +0800 | [diff] [blame^] | 252 | buf += (mtd->writesize - page_off); |
| 253 | page_off = 0; |
| 254 | size -= sz; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 255 | |
| 256 | /* |
| 257 | * Check if we have crossed a block boundary, and if so |
| 258 | * check for bad block. |
| 259 | */ |
| 260 | if (!(page % nand_page_per_block)) { |
| 261 | /* |
| 262 | * Yes, new block. See if this block is good. If not, |
| 263 | * loop until we find a good block. |
| 264 | */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 265 | while (is_badblock(mtd, offs, 1)) { |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 266 | page = page + nand_page_per_block; |
| 267 | /* Check i we've reached the end of flash. */ |
Ye Li | cf63923 | 2020-05-04 22:08:55 +0800 | [diff] [blame^] | 268 | if (page >= mtd->size >> chip->page_shift) { |
| 269 | free(page_buf); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 270 | return -ENOMEM; |
Ye Li | cf63923 | 2020-05-04 22:08:55 +0800 | [diff] [blame^] | 271 | } |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Ye Li | cf63923 | 2020-05-04 22:08:55 +0800 | [diff] [blame^] | 276 | free(page_buf); |
| 277 | |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | int nand_default_bbt(struct mtd_info *mtd) |
| 282 | { |
| 283 | return 0; |
| 284 | } |
| 285 | |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 286 | void nand_deselect(void) |
| 287 | { |
| 288 | } |
| 289 | |