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 |
| 4 | * Author: Tim Harvey <tharvey@gateworks.com> |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 5 | */ |
| 6 | #include <common.h> |
| 7 | #include <nand.h> |
| 8 | #include <malloc.h> |
| 9 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 10 | static struct mtd_info *mtd; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 11 | static struct nand_chip nand_chip; |
| 12 | |
| 13 | static void mxs_nand_command(struct mtd_info *mtd, unsigned int command, |
| 14 | int column, int page_addr) |
| 15 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 16 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 17 | u32 timeo, time_start; |
| 18 | |
| 19 | /* write out the command to the device */ |
| 20 | chip->cmd_ctrl(mtd, command, NAND_CLE); |
| 21 | |
| 22 | /* Serially input address */ |
| 23 | if (column != -1) { |
| 24 | chip->cmd_ctrl(mtd, column, NAND_ALE); |
| 25 | chip->cmd_ctrl(mtd, column >> 8, NAND_ALE); |
| 26 | } |
| 27 | if (page_addr != -1) { |
| 28 | chip->cmd_ctrl(mtd, page_addr, NAND_ALE); |
| 29 | chip->cmd_ctrl(mtd, page_addr >> 8, NAND_ALE); |
| 30 | /* One more address cycle for devices > 128MiB */ |
| 31 | if (chip->chipsize > (128 << 20)) |
| 32 | chip->cmd_ctrl(mtd, page_addr >> 16, NAND_ALE); |
| 33 | } |
| 34 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0); |
| 35 | |
| 36 | if (command == NAND_CMD_READ0) { |
| 37 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, NAND_CLE); |
| 38 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0); |
| 39 | } |
| 40 | |
| 41 | /* wait for nand ready */ |
| 42 | ndelay(100); |
| 43 | timeo = (CONFIG_SYS_HZ * 20) / 1000; |
| 44 | time_start = get_timer(0); |
| 45 | while (get_timer(time_start) < timeo) { |
| 46 | if (chip->dev_ready(mtd)) |
| 47 | break; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static int mxs_flash_ident(struct mtd_info *mtd) |
| 52 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 53 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 54 | int i; |
| 55 | u8 mfg_id, dev_id; |
| 56 | u8 id_data[8]; |
| 57 | struct nand_onfi_params *p = &chip->onfi_params; |
| 58 | |
| 59 | /* Reset the chip */ |
| 60 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 61 | |
| 62 | /* Send the command for reading device ID */ |
| 63 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 64 | |
| 65 | /* Read manufacturer and device IDs */ |
| 66 | mfg_id = chip->read_byte(mtd); |
| 67 | dev_id = chip->read_byte(mtd); |
| 68 | |
| 69 | /* Try again to make sure */ |
| 70 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 71 | for (i = 0; i < 8; i++) |
| 72 | id_data[i] = chip->read_byte(mtd); |
| 73 | if (id_data[0] != mfg_id || id_data[1] != dev_id) { |
| 74 | printf("second ID read did not match"); |
| 75 | return -1; |
| 76 | } |
| 77 | debug("0x%02x:0x%02x ", mfg_id, dev_id); |
| 78 | |
| 79 | /* read ONFI */ |
| 80 | chip->onfi_version = 0; |
| 81 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1); |
| 82 | if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' || |
| 83 | chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') { |
| 84 | return -2; |
| 85 | } |
| 86 | |
| 87 | /* we have ONFI, probe it */ |
| 88 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 89 | chip->read_buf(mtd, (uint8_t *)p, sizeof(*p)); |
| 90 | mtd->name = p->model; |
| 91 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 92 | mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize; |
| 93 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 94 | chip->chipsize = le32_to_cpu(p->blocks_per_lun); |
| 95 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 96 | /* Calculate the address shift from the page size */ |
| 97 | chip->page_shift = ffs(mtd->writesize) - 1; |
| 98 | chip->phys_erase_shift = ffs(mtd->erasesize) - 1; |
| 99 | /* Convert chipsize to number of pages per chip -1 */ |
| 100 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
| 101 | chip->badblockbits = 8; |
| 102 | |
| 103 | debug("erasesize=%d (>>%d)\n", mtd->erasesize, chip->phys_erase_shift); |
| 104 | debug("writesize=%d (>>%d)\n", mtd->writesize, chip->page_shift); |
| 105 | debug("oobsize=%d\n", mtd->oobsize); |
| 106 | debug("chipsize=%lld\n", chip->chipsize); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int mxs_read_page_ecc(struct mtd_info *mtd, void *buf, unsigned int page) |
| 112 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 113 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 114 | int ret; |
| 115 | |
| 116 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, page); |
| 117 | ret = nand_chip.ecc.read_page(mtd, chip, buf, 1, page); |
| 118 | if (ret < 0) { |
| 119 | printf("read_page failed %d\n", ret); |
| 120 | return -1; |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int is_badblock(struct mtd_info *mtd, loff_t offs, int allowbbt) |
| 126 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 127 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 128 | unsigned int block = offs >> chip->phys_erase_shift; |
| 129 | unsigned int page = offs >> chip->page_shift; |
| 130 | |
| 131 | debug("%s offs=0x%08x block:%d page:%d\n", __func__, (int)offs, block, |
| 132 | page); |
| 133 | chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page); |
| 134 | memset(chip->oob_poi, 0, mtd->oobsize); |
| 135 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 136 | |
| 137 | return chip->oob_poi[0] != 0xff; |
| 138 | } |
| 139 | |
| 140 | /* setup mtd and nand structs and init mxs_nand driver */ |
| 141 | static int mxs_nand_init(void) |
| 142 | { |
| 143 | /* return if already initalized */ |
| 144 | if (nand_chip.numchips) |
| 145 | return 0; |
| 146 | |
| 147 | /* init mxs nand driver */ |
| 148 | board_nand_init(&nand_chip); |
Boris Brezillon | 3b5f884 | 2016-06-15 20:56:10 +0200 | [diff] [blame] | 149 | mtd = nand_to_mtd(&nand_chip); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 150 | /* set mtd functions */ |
| 151 | nand_chip.cmdfunc = mxs_nand_command; |
| 152 | nand_chip.numchips = 1; |
| 153 | |
| 154 | /* identify flash device */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 155 | if (mxs_flash_ident(mtd)) { |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 156 | printf("Failed to identify\n"); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | /* allocate and initialize buffers */ |
| 161 | nand_chip.buffers = memalign(ARCH_DMA_MINALIGN, |
| 162 | sizeof(*nand_chip.buffers)); |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 163 | nand_chip.oob_poi = nand_chip.buffers->databuf + mtd->writesize; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 164 | /* setup flash layout (does not scan as we override that) */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 165 | mtd->size = nand_chip.chipsize; |
| 166 | nand_chip.scan_bbt(mtd); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 167 | |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | int nand_spl_load_image(uint32_t offs, unsigned int size, void *buf) |
| 172 | { |
| 173 | struct nand_chip *chip; |
| 174 | unsigned int page; |
| 175 | unsigned int nand_page_per_block; |
| 176 | unsigned int sz = 0; |
| 177 | |
| 178 | if (mxs_nand_init()) |
| 179 | return -ENODEV; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 180 | chip = mtd_to_nand(mtd); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 181 | page = offs >> chip->page_shift; |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 182 | nand_page_per_block = mtd->erasesize / mtd->writesize; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 183 | |
| 184 | debug("%s offset:0x%08x len:%d page:%d\n", __func__, offs, size, page); |
| 185 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 186 | size = roundup(size, mtd->writesize); |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 187 | while (sz < size) { |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 188 | if (mxs_read_page_ecc(mtd, buf, page) < 0) |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 189 | return -1; |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 190 | sz += mtd->writesize; |
| 191 | offs += mtd->writesize; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 192 | page++; |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 193 | buf += mtd->writesize; |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * Check if we have crossed a block boundary, and if so |
| 197 | * check for bad block. |
| 198 | */ |
| 199 | if (!(page % nand_page_per_block)) { |
| 200 | /* |
| 201 | * Yes, new block. See if this block is good. If not, |
| 202 | * loop until we find a good block. |
| 203 | */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 204 | while (is_badblock(mtd, offs, 1)) { |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 205 | page = page + nand_page_per_block; |
| 206 | /* Check i we've reached the end of flash. */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 207 | if (page >= mtd->size >> chip->page_shift) |
Tim Harvey | dcf4066 | 2014-06-02 16:13:18 -0700 | [diff] [blame] | 208 | return -ENOMEM; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | int nand_default_bbt(struct mtd_info *mtd) |
| 217 | { |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | void nand_init(void) |
| 222 | { |
| 223 | } |
| 224 | |
| 225 | void nand_deselect(void) |
| 226 | { |
| 227 | } |
| 228 | |