1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2014 Gateworks Corporation
4 * Copyright 2019 NXP
5 * Author: Tim Harvey <tharvey@gateworks.com>
6 */
7 #include <common.h>
8 #include <log.h>
9 #include <nand.h>
10 #include <malloc.h>
11 #include <mxs_nand.h>
12 #include <asm/cache.h>
13 #include <linux/bitops.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/mtd/rawnand.h>
17
18 static struct mtd_info *mtd;
19 static struct nand_chip nand_chip;
20
mxs_nand_command(struct mtd_info * mtd,unsigned int command,int column,int page_addr)21 static void mxs_nand_command(struct mtd_info *mtd, unsigned int command,
22 int column, int page_addr)
23 {
24 register struct nand_chip *chip = mtd_to_nand(mtd);
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);
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);
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
65 #if defined (CONFIG_SPL_NAND_IDENT)
66
67 /* Trying to detect the NAND flash using ONFi, JEDEC, and (extended) IDs */
mxs_flash_full_ident(struct mtd_info * mtd)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 */
mxs_flash_onfi_ident(struct mtd_info * mtd)87 static int mxs_flash_onfi_ident(struct mtd_info *mtd)
88 {
89 register struct nand_chip *chip = mtd_to_nand(mtd);
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
147 #endif /* CONFIG_SPL_NAND_IDENT */
148
mxs_flash_ident(struct mtd_info * mtd)149 static int mxs_flash_ident(struct mtd_info *mtd)
150 {
151 int ret;
152 #if defined (CONFIG_SPL_NAND_IDENT)
153 ret = mxs_flash_full_ident(mtd);
154 #else
155 ret = mxs_flash_onfi_ident(mtd);
156 #endif
157 return ret;
158 }
159
mxs_read_page_ecc(struct mtd_info * mtd,void * buf,unsigned int page)160 static int mxs_read_page_ecc(struct mtd_info *mtd, void *buf, unsigned int page)
161 {
162 register struct nand_chip *chip = mtd_to_nand(mtd);
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
is_badblock(struct mtd_info * mtd,loff_t offs,int allowbbt)174 static int is_badblock(struct mtd_info *mtd, loff_t offs, int allowbbt)
175 {
176 register struct nand_chip *chip = mtd_to_nand(mtd);
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 */
nand_init(void)190 void nand_init(void)
191 {
192 /* return if already initalized */
193 if (nand_chip.numchips)
194 return;
195
196 /* init mxs nand driver */
197 mxs_nand_init_spl(&nand_chip);
198 mtd = nand_to_mtd(&nand_chip);
199 /* set mtd functions */
200 nand_chip.cmdfunc = mxs_nand_command;
201 nand_chip.scan_bbt = nand_default_bbt;
202 nand_chip.numchips = 1;
203
204 /* identify flash device */
205 if (mxs_flash_ident(mtd)) {
206 printf("Failed to identify\n");
207 nand_chip.numchips = 0; /* If fail, don't use nand */
208 return;
209 }
210
211 /* allocate and initialize buffers */
212 nand_chip.buffers = memalign(ARCH_DMA_MINALIGN,
213 sizeof(*nand_chip.buffers));
214 nand_chip.oob_poi = nand_chip.buffers->databuf + mtd->writesize;
215 /* setup flash layout (does not scan as we override that) */
216 mtd->size = nand_chip.chipsize;
217 nand_chip.scan_bbt(mtd);
218 mxs_nand_setup_ecc(mtd);
219 }
220
nand_spl_load_image(uint32_t offs,unsigned int size,void * buf)221 int nand_spl_load_image(uint32_t offs, unsigned int size, void *buf)
222 {
223 struct nand_chip *chip;
224 unsigned int page;
225 unsigned int nand_page_per_block;
226 unsigned int sz = 0;
227 u8 *page_buf = NULL;
228 u32 page_off;
229
230 chip = mtd_to_nand(mtd);
231 if (!chip->numchips)
232 return -ENODEV;
233
234 page_buf = malloc(mtd->writesize);
235 if (!page_buf)
236 return -ENOMEM;
237
238 page = offs >> chip->page_shift;
239 page_off = offs & (mtd->writesize - 1);
240 nand_page_per_block = mtd->erasesize / mtd->writesize;
241
242 debug("%s offset:0x%08x len:%d page:%x\n", __func__, offs, size, page);
243
244 while (size) {
245 if (mxs_read_page_ecc(mtd, page_buf, page) < 0)
246 return -1;
247
248 if (size > (mtd->writesize - page_off))
249 sz = (mtd->writesize - page_off);
250 else
251 sz = size;
252
253 memcpy(buf, page_buf + page_off, sz);
254
255 offs += mtd->writesize;
256 page++;
257 buf += (mtd->writesize - page_off);
258 page_off = 0;
259 size -= sz;
260
261 /*
262 * Check if we have crossed a block boundary, and if so
263 * check for bad block.
264 */
265 if (!(page % nand_page_per_block)) {
266 /*
267 * Yes, new block. See if this block is good. If not,
268 * loop until we find a good block.
269 */
270 while (is_badblock(mtd, offs, 1)) {
271 page = page + nand_page_per_block;
272 /* Check i we've reached the end of flash. */
273 if (page >= mtd->size >> chip->page_shift) {
274 free(page_buf);
275 return -ENOMEM;
276 }
277 }
278 }
279 }
280
281 free(page_buf);
282
283 return 0;
284 }
285
nand_get_mtd(void)286 struct mtd_info *nand_get_mtd(void)
287 {
288 return mtd;
289 }
290
nand_default_bbt(struct mtd_info * mtd)291 int nand_default_bbt(struct mtd_info *mtd)
292 {
293 return 0;
294 }
295
nand_deselect(void)296 void nand_deselect(void)
297 {
298 }
299
nand_spl_adjust_offset(u32 sector,u32 offs)300 u32 nand_spl_adjust_offset(u32 sector, u32 offs)
301 {
302 /* Handle the offset adjust in nand_spl_load_image,*/
303 return offs;
304 }
305