blob: 2bfb181007b73d1c56b8c46970365a699d40d83e [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>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Tim Harveydcf40662014-06-02 16:13:18 -07009#include <nand.h>
10#include <malloc.h>
Shyam Sainif63ef492019-06-14 13:05:33 +053011#include <mxs_nand.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <asm/cache.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060013#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070015#include <linux/err.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040016#include <linux/mtd/rawnand.h>
Tim Harveydcf40662014-06-02 16:13:18 -070017
Scott Wood2c1b7e12016-05-30 13:57:55 -050018static struct mtd_info *mtd;
Tim Harveydcf40662014-06-02 16:13:18 -070019static struct nand_chip nand_chip;
20
21static void mxs_nand_command(struct mtd_info *mtd, unsigned int command,
22 int column, int page_addr)
23{
Scott Wood17fed142016-05-30 13:57:56 -050024 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -070025 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 Licf639232020-05-04 22:08:55 +080047 } 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 Harveydcf40662014-06-02 16:13:18 -070053 }
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 Krause7440e4b2018-01-14 19:26:40 +010065#if defined (CONFIG_SPL_NAND_IDENT)
66
67/* Trying to detect the NAND flash using ONFi, JEDEC, and (extended) IDs */
68static 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 Krause404a9db2018-01-14 19:26:39 +010087static int mxs_flash_onfi_ident(struct mtd_info *mtd)
Tim Harveydcf40662014-06-02 16:13:18 -070088{
Scott Wood17fed142016-05-30 13:57:56 -050089 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -070090 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 Krause7440e4b2018-01-14 19:26:40 +0100147#endif /* CONFIG_SPL_NAND_IDENT */
148
Jörg Krause404a9db2018-01-14 19:26:39 +0100149static int mxs_flash_ident(struct mtd_info *mtd)
150{
151 int ret;
Jörg Krause7440e4b2018-01-14 19:26:40 +0100152#if defined (CONFIG_SPL_NAND_IDENT)
153 ret = mxs_flash_full_ident(mtd);
154#else
Jörg Krause404a9db2018-01-14 19:26:39 +0100155 ret = mxs_flash_onfi_ident(mtd);
Jörg Krause7440e4b2018-01-14 19:26:40 +0100156#endif
Jörg Krause404a9db2018-01-14 19:26:39 +0100157 return ret;
158}
159
Tim Harveydcf40662014-06-02 16:13:18 -0700160static int mxs_read_page_ecc(struct mtd_info *mtd, void *buf, unsigned int page)
161{
Scott Wood17fed142016-05-30 13:57:56 -0500162 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700163 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
174static int is_badblock(struct mtd_info *mtd, loff_t offs, int allowbbt)
175{
Scott Wood17fed142016-05-30 13:57:56 -0500176 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700177 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 Ford858dd272019-02-18 17:58:17 -0600190void nand_init(void)
Tim Harveydcf40662014-06-02 16:13:18 -0700191{
192 /* return if already initalized */
193 if (nand_chip.numchips)
Adam Ford858dd272019-02-18 17:58:17 -0600194 return;
Tim Harveydcf40662014-06-02 16:13:18 -0700195
196 /* init mxs nand driver */
Stefan Agner7152f342018-06-22 17:19:46 +0200197 mxs_nand_init_spl(&nand_chip);
Boris Brezillon3b5f8842016-06-15 20:56:10 +0200198 mtd = nand_to_mtd(&nand_chip);
Tim Harveydcf40662014-06-02 16:13:18 -0700199 /* set mtd functions */
200 nand_chip.cmdfunc = mxs_nand_command;
Adam Fordcf873712018-12-30 10:11:16 -0600201 nand_chip.scan_bbt = nand_default_bbt;
Tim Harveydcf40662014-06-02 16:13:18 -0700202 nand_chip.numchips = 1;
203
204 /* identify flash device */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500205 if (mxs_flash_ident(mtd)) {
Tim Harveydcf40662014-06-02 16:13:18 -0700206 printf("Failed to identify\n");
Adam Ford858dd272019-02-18 17:58:17 -0600207 nand_chip.numchips = 0; /* If fail, don't use nand */
208 return;
Tim Harveydcf40662014-06-02 16:13:18 -0700209 }
210
211 /* allocate and initialize buffers */
212 nand_chip.buffers = memalign(ARCH_DMA_MINALIGN,
213 sizeof(*nand_chip.buffers));
Scott Wood2c1b7e12016-05-30 13:57:55 -0500214 nand_chip.oob_poi = nand_chip.buffers->databuf + mtd->writesize;
Tim Harveydcf40662014-06-02 16:13:18 -0700215 /* setup flash layout (does not scan as we override that) */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500216 mtd->size = nand_chip.chipsize;
217 nand_chip.scan_bbt(mtd);
Adam Ford10210732019-01-02 20:36:52 -0600218 mxs_nand_setup_ecc(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700219}
220
Michael Trimarchi95f42382022-05-15 11:35:31 +0200221int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
Tim Harveydcf40662014-06-02 16:13:18 -0700222{
Michael Trimarchi95f42382022-05-15 11:35:31 +0200223 unsigned int sz;
224 unsigned int block, lastblock;
225 unsigned int page, page_offset;
Tim Harveydcf40662014-06-02 16:13:18 -0700226 unsigned int nand_page_per_block;
Michael Trimarchi95f42382022-05-15 11:35:31 +0200227 struct nand_chip *chip;
Ye Licf639232020-05-04 22:08:55 +0800228 u8 *page_buf = NULL;
Tim Harveydcf40662014-06-02 16:13:18 -0700229
Scott Wood17fed142016-05-30 13:57:56 -0500230 chip = mtd_to_nand(mtd);
Adam Ford858dd272019-02-18 17:58:17 -0600231 if (!chip->numchips)
232 return -ENODEV;
Ye Licf639232020-05-04 22:08:55 +0800233
234 page_buf = malloc(mtd->writesize);
235 if (!page_buf)
236 return -ENOMEM;
237
Michael Trimarchi95f42382022-05-15 11:35:31 +0200238 /* 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 Wood2c1b7e12016-05-30 13:57:55 -0500243 nand_page_per_block = mtd->erasesize / mtd->writesize;
Tim Harveydcf40662014-06-02 16:13:18 -0700244
Michael Trimarchi95f42382022-05-15 11:35:31 +0200245 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 Licf639232020-05-04 22:08:55 +0800250
Michael Trimarchi95f42382022-05-15 11:35:31 +0200251 if (mxs_read_page_ecc(mtd, page_buf, curr_page) < 0) {
Ye Licf639232020-05-04 22:08:55 +0800252 free(page_buf);
Michael Trimarchi95f42382022-05-15 11:35:31 +0200253 return -EIO;
Ye Licf639232020-05-04 22:08:55 +0800254 }
Michael Trimarchi95f42382022-05-15 11:35:31 +0200255
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 Harveydcf40662014-06-02 16:13:18 -0700266 }
Michael Trimarchi95f42382022-05-15 11:35:31 +0200267
268 page = 0;
269 } else {
270 lastblock++;
Tim Harveydcf40662014-06-02 16:13:18 -0700271 }
Michael Trimarchi95f42382022-05-15 11:35:31 +0200272
273 block++;
Tim Harveydcf40662014-06-02 16:13:18 -0700274 }
275
Ye Licf639232020-05-04 22:08:55 +0800276 free(page_buf);
277
Tim Harveydcf40662014-06-02 16:13:18 -0700278 return 0;
279}
280
281int nand_default_bbt(struct mtd_info *mtd)
282{
283 return 0;
284}
285
Tim Harveydcf40662014-06-02 16:13:18 -0700286void nand_deselect(void)
287{
288}
Ye Li9caf9512021-08-17 17:24:47 +0800289
290u32 nand_spl_adjust_offset(u32 sector, u32 offs)
291{
Michael Trimarchi95f42382022-05-15 11:35:31 +0200292 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 Li9caf9512021-08-17 17:24:47 +0800306 return offs;
307}