blob: 3460f2b61a93729dd494b2c512b703d025b21dca [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 Glassd66c5f72020-02-03 07:36:15 -070013#include <linux/err.h>
Tim Harveydcf40662014-06-02 16:13:18 -070014
Scott Wood2c1b7e12016-05-30 13:57:55 -050015static struct mtd_info *mtd;
Tim Harveydcf40662014-06-02 16:13:18 -070016static struct nand_chip nand_chip;
17
18static void mxs_nand_command(struct mtd_info *mtd, unsigned int command,
19 int column, int page_addr)
20{
Scott Wood17fed142016-05-30 13:57:56 -050021 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -070022 u32 timeo, time_start;
23
24 /* write out the command to the device */
25 chip->cmd_ctrl(mtd, command, NAND_CLE);
26
27 /* Serially input address */
28 if (column != -1) {
29 chip->cmd_ctrl(mtd, column, NAND_ALE);
30 chip->cmd_ctrl(mtd, column >> 8, NAND_ALE);
31 }
32 if (page_addr != -1) {
33 chip->cmd_ctrl(mtd, page_addr, NAND_ALE);
34 chip->cmd_ctrl(mtd, page_addr >> 8, NAND_ALE);
35 /* One more address cycle for devices > 128MiB */
36 if (chip->chipsize > (128 << 20))
37 chip->cmd_ctrl(mtd, page_addr >> 16, NAND_ALE);
38 }
39 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0);
40
41 if (command == NAND_CMD_READ0) {
42 chip->cmd_ctrl(mtd, NAND_CMD_READSTART, NAND_CLE);
43 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0);
Ye Licf639232020-05-04 22:08:55 +080044 } else if (command == NAND_CMD_RNDOUT) {
45 /* No ready / busy check necessary */
46 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
47 NAND_NCE | NAND_CLE);
48 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
49 NAND_NCE);
Tim Harveydcf40662014-06-02 16:13:18 -070050 }
51
52 /* wait for nand ready */
53 ndelay(100);
54 timeo = (CONFIG_SYS_HZ * 20) / 1000;
55 time_start = get_timer(0);
56 while (get_timer(time_start) < timeo) {
57 if (chip->dev_ready(mtd))
58 break;
59 }
60}
61
Jörg Krause7440e4b2018-01-14 19:26:40 +010062#if defined (CONFIG_SPL_NAND_IDENT)
63
64/* Trying to detect the NAND flash using ONFi, JEDEC, and (extended) IDs */
65static int mxs_flash_full_ident(struct mtd_info *mtd)
66{
67 int nand_maf_id, nand_dev_id;
68 struct nand_chip *chip = mtd_to_nand(mtd);
69 struct nand_flash_dev *type;
70
71 type = nand_get_flash_type(mtd, chip, &nand_maf_id, &nand_dev_id, NULL);
72
73 if (IS_ERR(type)) {
74 chip->select_chip(mtd, -1);
75 return PTR_ERR(type);
76 }
77
78 return 0;
79}
80
81#else
82
83/* Trying to detect the NAND flash using ONFi only */
Jörg Krause404a9db2018-01-14 19:26:39 +010084static int mxs_flash_onfi_ident(struct mtd_info *mtd)
Tim Harveydcf40662014-06-02 16:13:18 -070085{
Scott Wood17fed142016-05-30 13:57:56 -050086 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -070087 int i;
88 u8 mfg_id, dev_id;
89 u8 id_data[8];
90 struct nand_onfi_params *p = &chip->onfi_params;
91
92 /* Reset the chip */
93 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
94
95 /* Send the command for reading device ID */
96 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
97
98 /* Read manufacturer and device IDs */
99 mfg_id = chip->read_byte(mtd);
100 dev_id = chip->read_byte(mtd);
101
102 /* Try again to make sure */
103 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
104 for (i = 0; i < 8; i++)
105 id_data[i] = chip->read_byte(mtd);
106 if (id_data[0] != mfg_id || id_data[1] != dev_id) {
107 printf("second ID read did not match");
108 return -1;
109 }
110 debug("0x%02x:0x%02x ", mfg_id, dev_id);
111
112 /* read ONFI */
113 chip->onfi_version = 0;
114 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
115 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
116 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') {
117 return -2;
118 }
119
120 /* we have ONFI, probe it */
121 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
122 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
123 mtd->name = p->model;
124 mtd->writesize = le32_to_cpu(p->byte_per_page);
125 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
126 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
127 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
128 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
129 /* Calculate the address shift from the page size */
130 chip->page_shift = ffs(mtd->writesize) - 1;
131 chip->phys_erase_shift = ffs(mtd->erasesize) - 1;
132 /* Convert chipsize to number of pages per chip -1 */
133 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
134 chip->badblockbits = 8;
135
136 debug("erasesize=%d (>>%d)\n", mtd->erasesize, chip->phys_erase_shift);
137 debug("writesize=%d (>>%d)\n", mtd->writesize, chip->page_shift);
138 debug("oobsize=%d\n", mtd->oobsize);
139 debug("chipsize=%lld\n", chip->chipsize);
140
141 return 0;
142}
143
Jörg Krause7440e4b2018-01-14 19:26:40 +0100144#endif /* CONFIG_SPL_NAND_IDENT */
145
Jörg Krause404a9db2018-01-14 19:26:39 +0100146static int mxs_flash_ident(struct mtd_info *mtd)
147{
148 int ret;
Jörg Krause7440e4b2018-01-14 19:26:40 +0100149#if defined (CONFIG_SPL_NAND_IDENT)
150 ret = mxs_flash_full_ident(mtd);
151#else
Jörg Krause404a9db2018-01-14 19:26:39 +0100152 ret = mxs_flash_onfi_ident(mtd);
Jörg Krause7440e4b2018-01-14 19:26:40 +0100153#endif
Jörg Krause404a9db2018-01-14 19:26:39 +0100154 return ret;
155}
156
Tim Harveydcf40662014-06-02 16:13:18 -0700157static int mxs_read_page_ecc(struct mtd_info *mtd, void *buf, unsigned int page)
158{
Scott Wood17fed142016-05-30 13:57:56 -0500159 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700160 int ret;
161
162 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, page);
163 ret = nand_chip.ecc.read_page(mtd, chip, buf, 1, page);
164 if (ret < 0) {
165 printf("read_page failed %d\n", ret);
166 return -1;
167 }
168 return 0;
169}
170
171static int is_badblock(struct mtd_info *mtd, loff_t offs, int allowbbt)
172{
Scott Wood17fed142016-05-30 13:57:56 -0500173 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700174 unsigned int block = offs >> chip->phys_erase_shift;
175 unsigned int page = offs >> chip->page_shift;
176
177 debug("%s offs=0x%08x block:%d page:%d\n", __func__, (int)offs, block,
178 page);
179 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
180 memset(chip->oob_poi, 0, mtd->oobsize);
181 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
182
183 return chip->oob_poi[0] != 0xff;
184}
185
186/* setup mtd and nand structs and init mxs_nand driver */
Adam Ford858dd272019-02-18 17:58:17 -0600187void nand_init(void)
Tim Harveydcf40662014-06-02 16:13:18 -0700188{
189 /* return if already initalized */
190 if (nand_chip.numchips)
Adam Ford858dd272019-02-18 17:58:17 -0600191 return;
Tim Harveydcf40662014-06-02 16:13:18 -0700192
193 /* init mxs nand driver */
Stefan Agner7152f342018-06-22 17:19:46 +0200194 mxs_nand_init_spl(&nand_chip);
Boris Brezillon3b5f8842016-06-15 20:56:10 +0200195 mtd = nand_to_mtd(&nand_chip);
Tim Harveydcf40662014-06-02 16:13:18 -0700196 /* set mtd functions */
197 nand_chip.cmdfunc = mxs_nand_command;
Adam Fordcf873712018-12-30 10:11:16 -0600198 nand_chip.scan_bbt = nand_default_bbt;
Tim Harveydcf40662014-06-02 16:13:18 -0700199 nand_chip.numchips = 1;
200
201 /* identify flash device */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500202 if (mxs_flash_ident(mtd)) {
Tim Harveydcf40662014-06-02 16:13:18 -0700203 printf("Failed to identify\n");
Adam Ford858dd272019-02-18 17:58:17 -0600204 nand_chip.numchips = 0; /* If fail, don't use nand */
205 return;
Tim Harveydcf40662014-06-02 16:13:18 -0700206 }
207
208 /* allocate and initialize buffers */
209 nand_chip.buffers = memalign(ARCH_DMA_MINALIGN,
210 sizeof(*nand_chip.buffers));
Scott Wood2c1b7e12016-05-30 13:57:55 -0500211 nand_chip.oob_poi = nand_chip.buffers->databuf + mtd->writesize;
Tim Harveydcf40662014-06-02 16:13:18 -0700212 /* setup flash layout (does not scan as we override that) */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500213 mtd->size = nand_chip.chipsize;
214 nand_chip.scan_bbt(mtd);
Adam Ford10210732019-01-02 20:36:52 -0600215 mxs_nand_setup_ecc(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700216}
217
218int nand_spl_load_image(uint32_t offs, unsigned int size, void *buf)
219{
220 struct nand_chip *chip;
221 unsigned int page;
222 unsigned int nand_page_per_block;
223 unsigned int sz = 0;
Ye Licf639232020-05-04 22:08:55 +0800224 u8 *page_buf = NULL;
225 u32 page_off;
Tim Harveydcf40662014-06-02 16:13:18 -0700226
Scott Wood17fed142016-05-30 13:57:56 -0500227 chip = mtd_to_nand(mtd);
Adam Ford858dd272019-02-18 17:58:17 -0600228 if (!chip->numchips)
229 return -ENODEV;
Ye Licf639232020-05-04 22:08:55 +0800230
231 page_buf = malloc(mtd->writesize);
232 if (!page_buf)
233 return -ENOMEM;
234
Tim Harveydcf40662014-06-02 16:13:18 -0700235 page = offs >> chip->page_shift;
Ye Licf639232020-05-04 22:08:55 +0800236 page_off = offs & (mtd->writesize - 1);
Scott Wood2c1b7e12016-05-30 13:57:55 -0500237 nand_page_per_block = mtd->erasesize / mtd->writesize;
Tim Harveydcf40662014-06-02 16:13:18 -0700238
Ye Licf639232020-05-04 22:08:55 +0800239 debug("%s offset:0x%08x len:%d page:%x\n", __func__, offs, size, page);
Tim Harveydcf40662014-06-02 16:13:18 -0700240
Ye Licf639232020-05-04 22:08:55 +0800241 while (size) {
242 if (mxs_read_page_ecc(mtd, page_buf, page) < 0)
Tim Harveydcf40662014-06-02 16:13:18 -0700243 return -1;
Ye Licf639232020-05-04 22:08:55 +0800244
245 if (size > (mtd->writesize - page_off))
246 sz = (mtd->writesize - page_off);
247 else
248 sz = size;
249
250 memcpy(buf, page_buf + page_off, sz);
251
Scott Wood2c1b7e12016-05-30 13:57:55 -0500252 offs += mtd->writesize;
Tim Harveydcf40662014-06-02 16:13:18 -0700253 page++;
Ye Licf639232020-05-04 22:08:55 +0800254 buf += (mtd->writesize - page_off);
255 page_off = 0;
256 size -= sz;
Tim Harveydcf40662014-06-02 16:13:18 -0700257
258 /*
259 * Check if we have crossed a block boundary, and if so
260 * check for bad block.
261 */
262 if (!(page % nand_page_per_block)) {
263 /*
264 * Yes, new block. See if this block is good. If not,
265 * loop until we find a good block.
266 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500267 while (is_badblock(mtd, offs, 1)) {
Tim Harveydcf40662014-06-02 16:13:18 -0700268 page = page + nand_page_per_block;
269 /* Check i we've reached the end of flash. */
Ye Licf639232020-05-04 22:08:55 +0800270 if (page >= mtd->size >> chip->page_shift) {
271 free(page_buf);
Tim Harveydcf40662014-06-02 16:13:18 -0700272 return -ENOMEM;
Ye Licf639232020-05-04 22:08:55 +0800273 }
Tim Harveydcf40662014-06-02 16:13:18 -0700274 }
275 }
276 }
277
Ye Licf639232020-05-04 22:08:55 +0800278 free(page_buf);
279
Tim Harveydcf40662014-06-02 16:13:18 -0700280 return 0;
281}
282
283int nand_default_bbt(struct mtd_info *mtd)
284{
285 return 0;
286}
287
Tim Harveydcf40662014-06-02 16:13:18 -0700288void nand_deselect(void)
289{
290}
291