blob: 7bfcbf095a6b9f722cec0c653e72fd59c9537429 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tim Harveydcf40662014-06-02 16:13:18 -07002/*
3 * Copyright (C) 2014 Gateworks Corporation
Ye Licf639232020-05-04 22:08:55 +08004 * Copyright 2019 NXP
Tim Harveydcf40662014-06-02 16:13:18 -07005 * Author: Tim Harvey <tharvey@gateworks.com>
Tim Harveydcf40662014-06-02 16:13:18 -07006 */
7#include <common.h>
8#include <nand.h>
9#include <malloc.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053010#include <mxs_nand.h>
Simon Glass274e0b02020-05-10 11:39:56 -060011#include <asm/cache.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070012#include <linux/err.h>
Tim Harveydcf40662014-06-02 16:13:18 -070013
Scott Wood2c1b7e12016-05-30 13:57:55 -050014static struct mtd_info *mtd;
Tim Harveydcf40662014-06-02 16:13:18 -070015static struct nand_chip nand_chip;
16
17static void mxs_nand_command(struct mtd_info *mtd, unsigned int command,
18 int column, int page_addr)
19{
Scott Wood17fed142016-05-30 13:57:56 -050020 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -070021 u32 timeo, time_start;
22
23 /* write out the command to the device */
24 chip->cmd_ctrl(mtd, command, NAND_CLE);
25
26 /* Serially input address */
27 if (column != -1) {
28 chip->cmd_ctrl(mtd, column, NAND_ALE);
29 chip->cmd_ctrl(mtd, column >> 8, NAND_ALE);
30 }
31 if (page_addr != -1) {
32 chip->cmd_ctrl(mtd, page_addr, NAND_ALE);
33 chip->cmd_ctrl(mtd, page_addr >> 8, NAND_ALE);
34 /* One more address cycle for devices > 128MiB */
35 if (chip->chipsize > (128 << 20))
36 chip->cmd_ctrl(mtd, page_addr >> 16, NAND_ALE);
37 }
38 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0);
39
40 if (command == NAND_CMD_READ0) {
41 chip->cmd_ctrl(mtd, NAND_CMD_READSTART, NAND_CLE);
42 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0);
Ye Licf639232020-05-04 22:08:55 +080043 } else if (command == NAND_CMD_RNDOUT) {
44 /* No ready / busy check necessary */
45 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
46 NAND_NCE | NAND_CLE);
47 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
48 NAND_NCE);
Tim Harveydcf40662014-06-02 16:13:18 -070049 }
50
51 /* wait for nand ready */
52 ndelay(100);
53 timeo = (CONFIG_SYS_HZ * 20) / 1000;
54 time_start = get_timer(0);
55 while (get_timer(time_start) < timeo) {
56 if (chip->dev_ready(mtd))
57 break;
58 }
59}
60
Jörg Krause7440e4b2018-01-14 19:26:40 +010061#if defined (CONFIG_SPL_NAND_IDENT)
62
63/* Trying to detect the NAND flash using ONFi, JEDEC, and (extended) IDs */
64static int mxs_flash_full_ident(struct mtd_info *mtd)
65{
66 int nand_maf_id, nand_dev_id;
67 struct nand_chip *chip = mtd_to_nand(mtd);
68 struct nand_flash_dev *type;
69
70 type = nand_get_flash_type(mtd, chip, &nand_maf_id, &nand_dev_id, NULL);
71
72 if (IS_ERR(type)) {
73 chip->select_chip(mtd, -1);
74 return PTR_ERR(type);
75 }
76
77 return 0;
78}
79
80#else
81
82/* Trying to detect the NAND flash using ONFi only */
Jörg Krause404a9db2018-01-14 19:26:39 +010083static int mxs_flash_onfi_ident(struct mtd_info *mtd)
Tim Harveydcf40662014-06-02 16:13:18 -070084{
Scott Wood17fed142016-05-30 13:57:56 -050085 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -070086 int i;
87 u8 mfg_id, dev_id;
88 u8 id_data[8];
89 struct nand_onfi_params *p = &chip->onfi_params;
90
91 /* Reset the chip */
92 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
93
94 /* Send the command for reading device ID */
95 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
96
97 /* Read manufacturer and device IDs */
98 mfg_id = chip->read_byte(mtd);
99 dev_id = chip->read_byte(mtd);
100
101 /* Try again to make sure */
102 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
103 for (i = 0; i < 8; i++)
104 id_data[i] = chip->read_byte(mtd);
105 if (id_data[0] != mfg_id || id_data[1] != dev_id) {
106 printf("second ID read did not match");
107 return -1;
108 }
109 debug("0x%02x:0x%02x ", mfg_id, dev_id);
110
111 /* read ONFI */
112 chip->onfi_version = 0;
113 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
114 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
115 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') {
116 return -2;
117 }
118
119 /* we have ONFI, probe it */
120 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
121 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
122 mtd->name = p->model;
123 mtd->writesize = le32_to_cpu(p->byte_per_page);
124 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
125 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
126 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
127 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
128 /* Calculate the address shift from the page size */
129 chip->page_shift = ffs(mtd->writesize) - 1;
130 chip->phys_erase_shift = ffs(mtd->erasesize) - 1;
131 /* Convert chipsize to number of pages per chip -1 */
132 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
133 chip->badblockbits = 8;
134
135 debug("erasesize=%d (>>%d)\n", mtd->erasesize, chip->phys_erase_shift);
136 debug("writesize=%d (>>%d)\n", mtd->writesize, chip->page_shift);
137 debug("oobsize=%d\n", mtd->oobsize);
138 debug("chipsize=%lld\n", chip->chipsize);
139
140 return 0;
141}
142
Jörg Krause7440e4b2018-01-14 19:26:40 +0100143#endif /* CONFIG_SPL_NAND_IDENT */
144
Jörg Krause404a9db2018-01-14 19:26:39 +0100145static int mxs_flash_ident(struct mtd_info *mtd)
146{
147 int ret;
Jörg Krause7440e4b2018-01-14 19:26:40 +0100148#if defined (CONFIG_SPL_NAND_IDENT)
149 ret = mxs_flash_full_ident(mtd);
150#else
Jörg Krause404a9db2018-01-14 19:26:39 +0100151 ret = mxs_flash_onfi_ident(mtd);
Jörg Krause7440e4b2018-01-14 19:26:40 +0100152#endif
Jörg Krause404a9db2018-01-14 19:26:39 +0100153 return ret;
154}
155
Tim Harveydcf40662014-06-02 16:13:18 -0700156static int mxs_read_page_ecc(struct mtd_info *mtd, void *buf, unsigned int page)
157{
Scott Wood17fed142016-05-30 13:57:56 -0500158 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700159 int ret;
160
161 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, page);
162 ret = nand_chip.ecc.read_page(mtd, chip, buf, 1, page);
163 if (ret < 0) {
164 printf("read_page failed %d\n", ret);
165 return -1;
166 }
167 return 0;
168}
169
170static int is_badblock(struct mtd_info *mtd, loff_t offs, int allowbbt)
171{
Scott Wood17fed142016-05-30 13:57:56 -0500172 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700173 unsigned int block = offs >> chip->phys_erase_shift;
174 unsigned int page = offs >> chip->page_shift;
175
176 debug("%s offs=0x%08x block:%d page:%d\n", __func__, (int)offs, block,
177 page);
178 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
179 memset(chip->oob_poi, 0, mtd->oobsize);
180 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
181
182 return chip->oob_poi[0] != 0xff;
183}
184
185/* setup mtd and nand structs and init mxs_nand driver */
Adam Ford858dd272019-02-18 17:58:17 -0600186void nand_init(void)
Tim Harveydcf40662014-06-02 16:13:18 -0700187{
188 /* return if already initalized */
189 if (nand_chip.numchips)
Adam Ford858dd272019-02-18 17:58:17 -0600190 return;
Tim Harveydcf40662014-06-02 16:13:18 -0700191
192 /* init mxs nand driver */
Stefan Agner7152f342018-06-22 17:19:46 +0200193 mxs_nand_init_spl(&nand_chip);
Boris Brezillon3b5f8842016-06-15 20:56:10 +0200194 mtd = nand_to_mtd(&nand_chip);
Tim Harveydcf40662014-06-02 16:13:18 -0700195 /* set mtd functions */
196 nand_chip.cmdfunc = mxs_nand_command;
Adam Fordcf873712018-12-30 10:11:16 -0600197 nand_chip.scan_bbt = nand_default_bbt;
Tim Harveydcf40662014-06-02 16:13:18 -0700198 nand_chip.numchips = 1;
199
200 /* identify flash device */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500201 if (mxs_flash_ident(mtd)) {
Tim Harveydcf40662014-06-02 16:13:18 -0700202 printf("Failed to identify\n");
Adam Ford858dd272019-02-18 17:58:17 -0600203 nand_chip.numchips = 0; /* If fail, don't use nand */
204 return;
Tim Harveydcf40662014-06-02 16:13:18 -0700205 }
206
207 /* allocate and initialize buffers */
208 nand_chip.buffers = memalign(ARCH_DMA_MINALIGN,
209 sizeof(*nand_chip.buffers));
Scott Wood2c1b7e12016-05-30 13:57:55 -0500210 nand_chip.oob_poi = nand_chip.buffers->databuf + mtd->writesize;
Tim Harveydcf40662014-06-02 16:13:18 -0700211 /* setup flash layout (does not scan as we override that) */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500212 mtd->size = nand_chip.chipsize;
213 nand_chip.scan_bbt(mtd);
Adam Ford10210732019-01-02 20:36:52 -0600214 mxs_nand_setup_ecc(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700215}
216
217int nand_spl_load_image(uint32_t offs, unsigned int size, void *buf)
218{
219 struct nand_chip *chip;
220 unsigned int page;
221 unsigned int nand_page_per_block;
222 unsigned int sz = 0;
Ye Licf639232020-05-04 22:08:55 +0800223 u8 *page_buf = NULL;
224 u32 page_off;
Tim Harveydcf40662014-06-02 16:13:18 -0700225
Scott Wood17fed142016-05-30 13:57:56 -0500226 chip = mtd_to_nand(mtd);
Adam Ford858dd272019-02-18 17:58:17 -0600227 if (!chip->numchips)
228 return -ENODEV;
Ye Licf639232020-05-04 22:08:55 +0800229
230 page_buf = malloc(mtd->writesize);
231 if (!page_buf)
232 return -ENOMEM;
233
Tim Harveydcf40662014-06-02 16:13:18 -0700234 page = offs >> chip->page_shift;
Ye Licf639232020-05-04 22:08:55 +0800235 page_off = offs & (mtd->writesize - 1);
Scott Wood2c1b7e12016-05-30 13:57:55 -0500236 nand_page_per_block = mtd->erasesize / mtd->writesize;
Tim Harveydcf40662014-06-02 16:13:18 -0700237
Ye Licf639232020-05-04 22:08:55 +0800238 debug("%s offset:0x%08x len:%d page:%x\n", __func__, offs, size, page);
Tim Harveydcf40662014-06-02 16:13:18 -0700239
Ye Licf639232020-05-04 22:08:55 +0800240 while (size) {
241 if (mxs_read_page_ecc(mtd, page_buf, page) < 0)
Tim Harveydcf40662014-06-02 16:13:18 -0700242 return -1;
Ye Licf639232020-05-04 22:08:55 +0800243
244 if (size > (mtd->writesize - page_off))
245 sz = (mtd->writesize - page_off);
246 else
247 sz = size;
248
249 memcpy(buf, page_buf + page_off, sz);
250
Scott Wood2c1b7e12016-05-30 13:57:55 -0500251 offs += mtd->writesize;
Tim Harveydcf40662014-06-02 16:13:18 -0700252 page++;
Ye Licf639232020-05-04 22:08:55 +0800253 buf += (mtd->writesize - page_off);
254 page_off = 0;
255 size -= sz;
Tim Harveydcf40662014-06-02 16:13:18 -0700256
257 /*
258 * Check if we have crossed a block boundary, and if so
259 * check for bad block.
260 */
261 if (!(page % nand_page_per_block)) {
262 /*
263 * Yes, new block. See if this block is good. If not,
264 * loop until we find a good block.
265 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500266 while (is_badblock(mtd, offs, 1)) {
Tim Harveydcf40662014-06-02 16:13:18 -0700267 page = page + nand_page_per_block;
268 /* Check i we've reached the end of flash. */
Ye Licf639232020-05-04 22:08:55 +0800269 if (page >= mtd->size >> chip->page_shift) {
270 free(page_buf);
Tim Harveydcf40662014-06-02 16:13:18 -0700271 return -ENOMEM;
Ye Licf639232020-05-04 22:08:55 +0800272 }
Tim Harveydcf40662014-06-02 16:13:18 -0700273 }
274 }
275 }
276
Ye Licf639232020-05-04 22:08:55 +0800277 free(page_buf);
278
Tim Harveydcf40662014-06-02 16:13:18 -0700279 return 0;
280}
281
282int nand_default_bbt(struct mtd_info *mtd)
283{
284 return 0;
285}
286
Tim Harveydcf40662014-06-02 16:13:18 -0700287void nand_deselect(void)
288{
289}
290