common: Drop net.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 <nand.h>
9 #include <malloc.h>
10 #include <mxs_nand.h>
11 #include <asm/cache.h>
12 #include <linux/err.h>
13
14 static struct mtd_info *mtd;
15 static struct nand_chip nand_chip;
16
17 static void mxs_nand_command(struct mtd_info *mtd, unsigned int command,
18                              int column, int page_addr)
19 {
20         register struct nand_chip *chip = mtd_to_nand(mtd);
21         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);
43         } 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);
49         }
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
61 #if defined (CONFIG_SPL_NAND_IDENT)
62
63 /* Trying to detect the NAND flash using ONFi, JEDEC, and (extended) IDs */
64 static 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 */
83 static int mxs_flash_onfi_ident(struct mtd_info *mtd)
84 {
85         register struct nand_chip *chip = mtd_to_nand(mtd);
86         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
143 #endif /* CONFIG_SPL_NAND_IDENT */
144
145 static int mxs_flash_ident(struct mtd_info *mtd)
146 {
147         int ret;
148 #if defined (CONFIG_SPL_NAND_IDENT)
149         ret = mxs_flash_full_ident(mtd);
150 #else
151         ret = mxs_flash_onfi_ident(mtd);
152 #endif
153         return ret;
154 }
155
156 static int mxs_read_page_ecc(struct mtd_info *mtd, void *buf, unsigned int page)
157 {
158         register struct nand_chip *chip = mtd_to_nand(mtd);
159         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
170 static int is_badblock(struct mtd_info *mtd, loff_t offs, int allowbbt)
171 {
172         register struct nand_chip *chip = mtd_to_nand(mtd);
173         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 */
186 void nand_init(void)
187 {
188         /* return if already initalized */
189         if (nand_chip.numchips)
190                 return;
191
192         /* init mxs nand driver */
193         mxs_nand_init_spl(&nand_chip);
194         mtd = nand_to_mtd(&nand_chip);
195         /* set mtd functions */
196         nand_chip.cmdfunc = mxs_nand_command;
197         nand_chip.scan_bbt = nand_default_bbt;
198         nand_chip.numchips = 1;
199
200         /* identify flash device */
201         if (mxs_flash_ident(mtd)) {
202                 printf("Failed to identify\n");
203                 nand_chip.numchips = 0; /* If fail, don't use nand */
204                 return;
205         }
206
207         /* allocate and initialize buffers */
208         nand_chip.buffers = memalign(ARCH_DMA_MINALIGN,
209                                      sizeof(*nand_chip.buffers));
210         nand_chip.oob_poi = nand_chip.buffers->databuf + mtd->writesize;
211         /* setup flash layout (does not scan as we override that) */
212         mtd->size = nand_chip.chipsize;
213         nand_chip.scan_bbt(mtd);
214         mxs_nand_setup_ecc(mtd);
215 }
216
217 int 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;
223         u8 *page_buf = NULL;
224         u32 page_off;
225
226         chip = mtd_to_nand(mtd);
227         if (!chip->numchips)
228                 return -ENODEV;
229
230         page_buf = malloc(mtd->writesize);
231         if (!page_buf)
232                 return -ENOMEM;
233
234         page = offs >> chip->page_shift;
235         page_off = offs & (mtd->writesize - 1);
236         nand_page_per_block = mtd->erasesize / mtd->writesize;
237
238         debug("%s offset:0x%08x len:%d page:%x\n", __func__, offs, size, page);
239
240         while (size) {
241                 if (mxs_read_page_ecc(mtd, page_buf, page) < 0)
242                         return -1;
243
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
251                 offs += mtd->writesize;
252                 page++;
253                 buf += (mtd->writesize - page_off);
254                 page_off = 0;
255                 size -= sz;
256
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                          */
266                         while (is_badblock(mtd, offs, 1)) {
267                                 page = page + nand_page_per_block;
268                                 /* Check i we've reached the end of flash. */
269                                 if (page >= mtd->size >> chip->page_shift) {
270                                         free(page_buf);
271                                         return -ENOMEM;
272                                 }
273                         }
274                 }
275         }
276
277         free(page_buf);
278
279         return 0;
280 }
281
282 int nand_default_bbt(struct mtd_info *mtd)
283 {
284         return 0;
285 }
286
287 void nand_deselect(void)
288 {
289 }
290