common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / mtd / nand / raw / mxs_nand_spl.c
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/delay.h>
14 #include <linux/err.h>
15
16 static struct mtd_info *mtd;
17 static struct nand_chip nand_chip;
18
19 static void mxs_nand_command(struct mtd_info *mtd, unsigned int command,
20                              int column, int page_addr)
21 {
22         register struct nand_chip *chip = mtd_to_nand(mtd);
23         u32 timeo, time_start;
24
25         /* write out the command to the device */
26         chip->cmd_ctrl(mtd, command, NAND_CLE);
27
28         /* Serially input address */
29         if (column != -1) {
30                 chip->cmd_ctrl(mtd, column, NAND_ALE);
31                 chip->cmd_ctrl(mtd, column >> 8, NAND_ALE);
32         }
33         if (page_addr != -1) {
34                 chip->cmd_ctrl(mtd, page_addr, NAND_ALE);
35                 chip->cmd_ctrl(mtd, page_addr >> 8, NAND_ALE);
36                 /* One more address cycle for devices > 128MiB */
37                 if (chip->chipsize > (128 << 20))
38                         chip->cmd_ctrl(mtd, page_addr >> 16, NAND_ALE);
39         }
40         chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0);
41
42         if (command == NAND_CMD_READ0) {
43                 chip->cmd_ctrl(mtd, NAND_CMD_READSTART, NAND_CLE);
44                 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0);
45         } else if (command == NAND_CMD_RNDOUT) {
46                 /* No ready / busy check necessary */
47                 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
48                                NAND_NCE | NAND_CLE);
49                 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
50                                NAND_NCE);
51         }
52
53         /* wait for nand ready */
54         ndelay(100);
55         timeo = (CONFIG_SYS_HZ * 20) / 1000;
56         time_start = get_timer(0);
57         while (get_timer(time_start) < timeo) {
58                 if (chip->dev_ready(mtd))
59                         break;
60         }
61 }
62
63 #if defined (CONFIG_SPL_NAND_IDENT)
64
65 /* Trying to detect the NAND flash using ONFi, JEDEC, and (extended) IDs */
66 static int mxs_flash_full_ident(struct mtd_info *mtd)
67 {
68         int nand_maf_id, nand_dev_id;
69         struct nand_chip *chip = mtd_to_nand(mtd);
70         struct nand_flash_dev *type;
71
72         type = nand_get_flash_type(mtd, chip, &nand_maf_id, &nand_dev_id, NULL);
73
74         if (IS_ERR(type)) {
75                 chip->select_chip(mtd, -1);
76                 return PTR_ERR(type);
77         }
78
79         return 0;
80 }
81
82 #else
83
84 /* Trying to detect the NAND flash using ONFi only */
85 static int mxs_flash_onfi_ident(struct mtd_info *mtd)
86 {
87         register struct nand_chip *chip = mtd_to_nand(mtd);
88         int i;
89         u8 mfg_id, dev_id;
90         u8 id_data[8];
91         struct nand_onfi_params *p = &chip->onfi_params;
92
93         /* Reset the chip */
94         chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
95
96         /* Send the command for reading device ID */
97         chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
98
99         /* Read manufacturer and device IDs */
100         mfg_id = chip->read_byte(mtd);
101         dev_id = chip->read_byte(mtd);
102
103         /* Try again to make sure */
104         chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
105         for (i = 0; i < 8; i++)
106                 id_data[i] = chip->read_byte(mtd);
107         if (id_data[0] != mfg_id || id_data[1] != dev_id) {
108                 printf("second ID read did not match");
109                 return -1;
110         }
111         debug("0x%02x:0x%02x ", mfg_id, dev_id);
112
113         /* read ONFI */
114         chip->onfi_version = 0;
115         chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
116         if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
117             chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') {
118                 return -2;
119         }
120
121         /* we have ONFI, probe it */
122         chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
123         chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
124         mtd->name = p->model;
125         mtd->writesize = le32_to_cpu(p->byte_per_page);
126         mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
127         mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
128         chip->chipsize = le32_to_cpu(p->blocks_per_lun);
129         chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
130         /* Calculate the address shift from the page size */
131         chip->page_shift = ffs(mtd->writesize) - 1;
132         chip->phys_erase_shift = ffs(mtd->erasesize) - 1;
133         /* Convert chipsize to number of pages per chip -1 */
134         chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
135         chip->badblockbits = 8;
136
137         debug("erasesize=%d (>>%d)\n", mtd->erasesize, chip->phys_erase_shift);
138         debug("writesize=%d (>>%d)\n", mtd->writesize, chip->page_shift);
139         debug("oobsize=%d\n", mtd->oobsize);
140         debug("chipsize=%lld\n", chip->chipsize);
141
142         return 0;
143 }
144
145 #endif /* CONFIG_SPL_NAND_IDENT */
146
147 static int mxs_flash_ident(struct mtd_info *mtd)
148 {
149         int ret;
150 #if defined (CONFIG_SPL_NAND_IDENT)
151         ret = mxs_flash_full_ident(mtd);
152 #else
153         ret = mxs_flash_onfi_ident(mtd);
154 #endif
155         return ret;
156 }
157
158 static int mxs_read_page_ecc(struct mtd_info *mtd, void *buf, unsigned int page)
159 {
160         register struct nand_chip *chip = mtd_to_nand(mtd);
161         int ret;
162
163         chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, page);
164         ret = nand_chip.ecc.read_page(mtd, chip, buf, 1, page);
165         if (ret < 0) {
166                 printf("read_page failed %d\n", ret);
167                 return -1;
168         }
169         return 0;
170 }
171
172 static int is_badblock(struct mtd_info *mtd, loff_t offs, int allowbbt)
173 {
174         register struct nand_chip *chip = mtd_to_nand(mtd);
175         unsigned int block = offs >> chip->phys_erase_shift;
176         unsigned int page = offs >> chip->page_shift;
177
178         debug("%s offs=0x%08x block:%d page:%d\n", __func__, (int)offs, block,
179               page);
180         chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
181         memset(chip->oob_poi, 0, mtd->oobsize);
182         chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
183
184         return chip->oob_poi[0] != 0xff;
185 }
186
187 /* setup mtd and nand structs and init mxs_nand driver */
188 void nand_init(void)
189 {
190         /* return if already initalized */
191         if (nand_chip.numchips)
192                 return;
193
194         /* init mxs nand driver */
195         mxs_nand_init_spl(&nand_chip);
196         mtd = nand_to_mtd(&nand_chip);
197         /* set mtd functions */
198         nand_chip.cmdfunc = mxs_nand_command;
199         nand_chip.scan_bbt = nand_default_bbt;
200         nand_chip.numchips = 1;
201
202         /* identify flash device */
203         if (mxs_flash_ident(mtd)) {
204                 printf("Failed to identify\n");
205                 nand_chip.numchips = 0; /* If fail, don't use nand */
206                 return;
207         }
208
209         /* allocate and initialize buffers */
210         nand_chip.buffers = memalign(ARCH_DMA_MINALIGN,
211                                      sizeof(*nand_chip.buffers));
212         nand_chip.oob_poi = nand_chip.buffers->databuf + mtd->writesize;
213         /* setup flash layout (does not scan as we override that) */
214         mtd->size = nand_chip.chipsize;
215         nand_chip.scan_bbt(mtd);
216         mxs_nand_setup_ecc(mtd);
217 }
218
219 int nand_spl_load_image(uint32_t offs, unsigned int size, void *buf)
220 {
221         struct nand_chip *chip;
222         unsigned int page;
223         unsigned int nand_page_per_block;
224         unsigned int sz = 0;
225         u8 *page_buf = NULL;
226         u32 page_off;
227
228         chip = mtd_to_nand(mtd);
229         if (!chip->numchips)
230                 return -ENODEV;
231
232         page_buf = malloc(mtd->writesize);
233         if (!page_buf)
234                 return -ENOMEM;
235
236         page = offs >> chip->page_shift;
237         page_off = offs & (mtd->writesize - 1);
238         nand_page_per_block = mtd->erasesize / mtd->writesize;
239
240         debug("%s offset:0x%08x len:%d page:%x\n", __func__, offs, size, page);
241
242         while (size) {
243                 if (mxs_read_page_ecc(mtd, page_buf, page) < 0)
244                         return -1;
245
246                 if (size > (mtd->writesize - page_off))
247                         sz = (mtd->writesize - page_off);
248                 else
249                         sz = size;
250
251                 memcpy(buf, page_buf + page_off, sz);
252
253                 offs += mtd->writesize;
254                 page++;
255                 buf += (mtd->writesize - page_off);
256                 page_off = 0;
257                 size -= sz;
258
259                 /*
260                  * Check if we have crossed a block boundary, and if so
261                  * check for bad block.
262                  */
263                 if (!(page % nand_page_per_block)) {
264                         /*
265                          * Yes, new block. See if this block is good. If not,
266                          * loop until we find a good block.
267                          */
268                         while (is_badblock(mtd, offs, 1)) {
269                                 page = page + nand_page_per_block;
270                                 /* Check i we've reached the end of flash. */
271                                 if (page >= mtd->size >> chip->page_shift) {
272                                         free(page_buf);
273                                         return -ENOMEM;
274                                 }
275                         }
276                 }
277         }
278
279         free(page_buf);
280
281         return 0;
282 }
283
284 int nand_default_bbt(struct mtd_info *mtd)
285 {
286         return 0;
287 }
288
289 void nand_deselect(void)
290 {
291 }
292