blob: adb12c531ec118918057339f71cf856b3f207da7 [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
4 * Author: Tim Harvey <tharvey@gateworks.com>
Tim Harveydcf40662014-06-02 16:13:18 -07005 */
6#include <common.h>
7#include <nand.h>
8#include <malloc.h>
Stefan Agner7152f342018-06-22 17:19:46 +02009#include "mxs_nand.h"
Tim Harveydcf40662014-06-02 16:13:18 -070010
Scott Wood2c1b7e12016-05-30 13:57:55 -050011static struct mtd_info *mtd;
Tim Harveydcf40662014-06-02 16:13:18 -070012static struct nand_chip nand_chip;
13
14static void mxs_nand_command(struct mtd_info *mtd, unsigned int command,
15 int column, int page_addr)
16{
Scott Wood17fed142016-05-30 13:57:56 -050017 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -070018 u32 timeo, time_start;
19
20 /* write out the command to the device */
21 chip->cmd_ctrl(mtd, command, NAND_CLE);
22
23 /* Serially input address */
24 if (column != -1) {
25 chip->cmd_ctrl(mtd, column, NAND_ALE);
26 chip->cmd_ctrl(mtd, column >> 8, NAND_ALE);
27 }
28 if (page_addr != -1) {
29 chip->cmd_ctrl(mtd, page_addr, NAND_ALE);
30 chip->cmd_ctrl(mtd, page_addr >> 8, NAND_ALE);
31 /* One more address cycle for devices > 128MiB */
32 if (chip->chipsize > (128 << 20))
33 chip->cmd_ctrl(mtd, page_addr >> 16, NAND_ALE);
34 }
35 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0);
36
37 if (command == NAND_CMD_READ0) {
38 chip->cmd_ctrl(mtd, NAND_CMD_READSTART, NAND_CLE);
39 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0);
40 }
41
42 /* wait for nand ready */
43 ndelay(100);
44 timeo = (CONFIG_SYS_HZ * 20) / 1000;
45 time_start = get_timer(0);
46 while (get_timer(time_start) < timeo) {
47 if (chip->dev_ready(mtd))
48 break;
49 }
50}
51
Jörg Krause404a9db2018-01-14 19:26:39 +010052static int mxs_flash_onfi_ident(struct mtd_info *mtd)
Tim Harveydcf40662014-06-02 16:13:18 -070053{
Scott Wood17fed142016-05-30 13:57:56 -050054 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -070055 int i;
56 u8 mfg_id, dev_id;
57 u8 id_data[8];
58 struct nand_onfi_params *p = &chip->onfi_params;
59
60 /* Reset the chip */
61 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
62
63 /* Send the command for reading device ID */
64 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
65
66 /* Read manufacturer and device IDs */
67 mfg_id = chip->read_byte(mtd);
68 dev_id = chip->read_byte(mtd);
69
70 /* Try again to make sure */
71 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
72 for (i = 0; i < 8; i++)
73 id_data[i] = chip->read_byte(mtd);
74 if (id_data[0] != mfg_id || id_data[1] != dev_id) {
75 printf("second ID read did not match");
76 return -1;
77 }
78 debug("0x%02x:0x%02x ", mfg_id, dev_id);
79
80 /* read ONFI */
81 chip->onfi_version = 0;
82 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
83 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
84 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') {
85 return -2;
86 }
87
88 /* we have ONFI, probe it */
89 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
90 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
91 mtd->name = p->model;
92 mtd->writesize = le32_to_cpu(p->byte_per_page);
93 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
94 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
95 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
96 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
97 /* Calculate the address shift from the page size */
98 chip->page_shift = ffs(mtd->writesize) - 1;
99 chip->phys_erase_shift = ffs(mtd->erasesize) - 1;
100 /* Convert chipsize to number of pages per chip -1 */
101 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
102 chip->badblockbits = 8;
103
104 debug("erasesize=%d (>>%d)\n", mtd->erasesize, chip->phys_erase_shift);
105 debug("writesize=%d (>>%d)\n", mtd->writesize, chip->page_shift);
106 debug("oobsize=%d\n", mtd->oobsize);
107 debug("chipsize=%lld\n", chip->chipsize);
108
109 return 0;
110}
111
Jörg Krause404a9db2018-01-14 19:26:39 +0100112static int mxs_flash_ident(struct mtd_info *mtd)
113{
114 int ret;
115 ret = mxs_flash_onfi_ident(mtd);
116 return ret;
117}
118
Tim Harveydcf40662014-06-02 16:13:18 -0700119static int mxs_read_page_ecc(struct mtd_info *mtd, void *buf, unsigned int page)
120{
Scott Wood17fed142016-05-30 13:57:56 -0500121 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700122 int ret;
123
124 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, page);
125 ret = nand_chip.ecc.read_page(mtd, chip, buf, 1, page);
126 if (ret < 0) {
127 printf("read_page failed %d\n", ret);
128 return -1;
129 }
130 return 0;
131}
132
133static int is_badblock(struct mtd_info *mtd, loff_t offs, int allowbbt)
134{
Scott Wood17fed142016-05-30 13:57:56 -0500135 register struct nand_chip *chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700136 unsigned int block = offs >> chip->phys_erase_shift;
137 unsigned int page = offs >> chip->page_shift;
138
139 debug("%s offs=0x%08x block:%d page:%d\n", __func__, (int)offs, block,
140 page);
141 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
142 memset(chip->oob_poi, 0, mtd->oobsize);
143 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
144
145 return chip->oob_poi[0] != 0xff;
146}
147
148/* setup mtd and nand structs and init mxs_nand driver */
149static int mxs_nand_init(void)
150{
151 /* return if already initalized */
152 if (nand_chip.numchips)
153 return 0;
154
155 /* init mxs nand driver */
Stefan Agner7152f342018-06-22 17:19:46 +0200156 mxs_nand_init_spl(&nand_chip);
Boris Brezillon3b5f8842016-06-15 20:56:10 +0200157 mtd = nand_to_mtd(&nand_chip);
Tim Harveydcf40662014-06-02 16:13:18 -0700158 /* set mtd functions */
159 nand_chip.cmdfunc = mxs_nand_command;
160 nand_chip.numchips = 1;
161
162 /* identify flash device */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500163 if (mxs_flash_ident(mtd)) {
Tim Harveydcf40662014-06-02 16:13:18 -0700164 printf("Failed to identify\n");
165 return -1;
166 }
167
168 /* allocate and initialize buffers */
169 nand_chip.buffers = memalign(ARCH_DMA_MINALIGN,
170 sizeof(*nand_chip.buffers));
Scott Wood2c1b7e12016-05-30 13:57:55 -0500171 nand_chip.oob_poi = nand_chip.buffers->databuf + mtd->writesize;
Tim Harveydcf40662014-06-02 16:13:18 -0700172 /* setup flash layout (does not scan as we override that) */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500173 mtd->size = nand_chip.chipsize;
174 nand_chip.scan_bbt(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700175
Tim Harveydcf40662014-06-02 16:13:18 -0700176 return 0;
177}
178
179int nand_spl_load_image(uint32_t offs, unsigned int size, void *buf)
180{
181 struct nand_chip *chip;
182 unsigned int page;
183 unsigned int nand_page_per_block;
184 unsigned int sz = 0;
185
186 if (mxs_nand_init())
187 return -ENODEV;
Scott Wood17fed142016-05-30 13:57:56 -0500188 chip = mtd_to_nand(mtd);
Tim Harveydcf40662014-06-02 16:13:18 -0700189 page = offs >> chip->page_shift;
Scott Wood2c1b7e12016-05-30 13:57:55 -0500190 nand_page_per_block = mtd->erasesize / mtd->writesize;
Tim Harveydcf40662014-06-02 16:13:18 -0700191
192 debug("%s offset:0x%08x len:%d page:%d\n", __func__, offs, size, page);
193
Scott Wood2c1b7e12016-05-30 13:57:55 -0500194 size = roundup(size, mtd->writesize);
Tim Harveydcf40662014-06-02 16:13:18 -0700195 while (sz < size) {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500196 if (mxs_read_page_ecc(mtd, buf, page) < 0)
Tim Harveydcf40662014-06-02 16:13:18 -0700197 return -1;
Scott Wood2c1b7e12016-05-30 13:57:55 -0500198 sz += mtd->writesize;
199 offs += mtd->writesize;
Tim Harveydcf40662014-06-02 16:13:18 -0700200 page++;
Scott Wood2c1b7e12016-05-30 13:57:55 -0500201 buf += mtd->writesize;
Tim Harveydcf40662014-06-02 16:13:18 -0700202
203 /*
204 * Check if we have crossed a block boundary, and if so
205 * check for bad block.
206 */
207 if (!(page % nand_page_per_block)) {
208 /*
209 * Yes, new block. See if this block is good. If not,
210 * loop until we find a good block.
211 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500212 while (is_badblock(mtd, offs, 1)) {
Tim Harveydcf40662014-06-02 16:13:18 -0700213 page = page + nand_page_per_block;
214 /* Check i we've reached the end of flash. */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500215 if (page >= mtd->size >> chip->page_shift)
Tim Harveydcf40662014-06-02 16:13:18 -0700216 return -ENOMEM;
217 }
218 }
219 }
220
221 return 0;
222}
223
224int nand_default_bbt(struct mtd_info *mtd)
225{
226 return 0;
227}
228
229void nand_init(void)
230{
231}
232
233void nand_deselect(void)
234{
235}
236