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