blob: a9d54a243ade6bf00ed0a1329d056cefecbb7b12 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasut07e69a22011-10-31 14:14:15 +01002/*
3 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
4 *
5 * Based on code:
6 * Copyright (C) 2005-2009 Samsung Electronics
7 * Kyungmin Park <kyungmin.park@samsung.com>
Marek Vasut07e69a22011-10-31 14:14:15 +01008 */
9
Tom Rinidec7ea02024-05-20 13:35:03 -060010#include <config.h>
Marek Vasut07e69a22011-10-31 14:14:15 +010011#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060012#include <linux/bitops.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060013#include <linux/string.h>
Marek Vasut07e69a22011-10-31 14:14:15 +010014#include <linux/mtd/onenand_regs.h>
15#include <onenand_uboot.h>
16
17/*
18 * Device geometry:
19 * - 2048b page, 128k erase block.
20 * - 4096b page, 256k erase block.
21 */
22enum onenand_spl_pagesize {
23 PAGE_2K = 2048,
24 PAGE_4K = 4096,
25};
26
Ladislav Michle9c173e2017-06-20 11:44:29 +020027static unsigned int density_mask;
28
Marek Vasut07e69a22011-10-31 14:14:15 +010029#define ONENAND_PAGES_PER_BLOCK 64
Marek Vasut07e69a22011-10-31 14:14:15 +010030#define onenand_sector_address(page) (page << 2)
31#define onenand_buffer_address() ((1 << 3) << 8)
Ladislav Michle9c173e2017-06-20 11:44:29 +020032
33static inline int onenand_block_address(int block)
34{
35 /* Device Flash Core select, NAND Flash Block Address */
36 if (block & density_mask)
37 return ONENAND_DDP_CHIP1 | (block ^ density_mask);
38
39 return block;
40}
41
42static inline int onenand_bufferram_address(int block)
43{
44 /* Device BufferRAM Select */
45 if (block & density_mask)
46 return ONENAND_DDP_CHIP1;
47
48 return ONENAND_DDP_CHIP0;
49}
Marek Vasut07e69a22011-10-31 14:14:15 +010050
51static inline uint16_t onenand_readw(uint32_t addr)
52{
Tom Rini6a5dccc2022-11-16 13:10:41 -050053 return readw(CFG_SYS_ONENAND_BASE + addr);
Marek Vasut07e69a22011-10-31 14:14:15 +010054}
55
56static inline void onenand_writew(uint16_t value, uint32_t addr)
57{
Tom Rini6a5dccc2022-11-16 13:10:41 -050058 writew(value, CFG_SYS_ONENAND_BASE + addr);
Marek Vasut07e69a22011-10-31 14:14:15 +010059}
60
61static enum onenand_spl_pagesize onenand_spl_get_geometry(void)
62{
Ladislav Michle9c173e2017-06-20 11:44:29 +020063 unsigned int dev_id, density, size;
Marek Vasut07e69a22011-10-31 14:14:15 +010064
65 if (!onenand_readw(ONENAND_REG_TECHNOLOGY)) {
66 dev_id = onenand_readw(ONENAND_REG_DEVICE_ID);
67 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
68 density &= ONENAND_DEVICE_DENSITY_MASK;
69
70 if (density < ONENAND_DEVICE_DENSITY_4Gb)
71 return PAGE_2K;
72
Ladislav Michle9c173e2017-06-20 11:44:29 +020073 if (dev_id & ONENAND_DEVICE_IS_DDP) {
74 size = onenand_readw(ONENAND_REG_DATA_BUFFER_SIZE);
75 density_mask = 1 << (18 + density - ffs(size));
Marek Vasut07e69a22011-10-31 14:14:15 +010076 return PAGE_2K;
Ladislav Michle9c173e2017-06-20 11:44:29 +020077 }
Marek Vasut07e69a22011-10-31 14:14:15 +010078 }
79
80 return PAGE_4K;
81}
82
83static int onenand_spl_read_page(uint32_t block, uint32_t page, uint32_t *buf,
84 enum onenand_spl_pagesize pagesize)
85{
Tom Rini6a5dccc2022-11-16 13:10:41 -050086 const uint32_t addr = CFG_SYS_ONENAND_BASE + ONENAND_DATARAM;
Marek Vasut07e69a22011-10-31 14:14:15 +010087 uint32_t offset;
88
89 onenand_writew(onenand_block_address(block),
90 ONENAND_REG_START_ADDRESS1);
91
92 onenand_writew(onenand_bufferram_address(block),
93 ONENAND_REG_START_ADDRESS2);
94
95 onenand_writew(onenand_sector_address(page),
96 ONENAND_REG_START_ADDRESS8);
97
98 onenand_writew(onenand_buffer_address(),
99 ONENAND_REG_START_BUFFER);
100
101 onenand_writew(ONENAND_INT_CLEAR, ONENAND_REG_INTERRUPT);
102
103 onenand_writew(ONENAND_CMD_READ, ONENAND_REG_COMMAND);
104
105 while (!(onenand_readw(ONENAND_REG_INTERRUPT) & ONENAND_INT_READ))
106 continue;
107
108 /* Check for invalid block mark */
109 if (page < 2 && (onenand_readw(ONENAND_SPARERAM) != 0xffff))
110 return 1;
111
112 for (offset = 0; offset < pagesize; offset += 4)
113 buf[offset / 4] = readl(addr + offset);
114
115 return 0;
116}
117
Ladislav Michldcd7ed32016-07-12 20:28:11 +0200118#ifdef CONFIG_SPL_UBI
119/* Temporary storage for non page aligned and non page sized reads. */
120static u8 scratch_buf[PAGE_4K];
121
122/**
123 * onenand_spl_read_block - Read data from physical eraseblock into a buffer
124 * @block: Number of the physical eraseblock
125 * @offset: Data offset from the start of @peb
126 * @len: Data size to read
127 * @dst: Address of the destination buffer
128 *
129 * Notes:
130 * @offset + @len are not allowed to be larger than a physical
131 * erase block. No sanity check done for simplicity reasons.
132 */
133int onenand_spl_read_block(int block, int offset, int len, void *dst)
134{
Ladislav Michlf7081e92017-06-20 11:43:22 +0200135 int page, read;
136 static int psize;
137
138 if (!psize)
139 psize = onenand_spl_get_geometry();
Ladislav Michldcd7ed32016-07-12 20:28:11 +0200140
Ladislav Michldcd7ed32016-07-12 20:28:11 +0200141 /* Calculate the page number */
142 page = offset / psize;
143 /* Offset to the start of a flash page */
144 offset = offset % psize;
145
146 while (len) {
147 /*
148 * Non page aligned reads go to the scratch buffer.
149 * Page aligned reads go directly to the destination.
150 */
151 if (offset || len < psize) {
152 onenand_spl_read_page(block, page,
153 (uint32_t *)scratch_buf, psize);
154 read = min(len, psize - offset);
155 memcpy(dst, scratch_buf + offset, read);
156 offset = 0;
157 } else {
158 onenand_spl_read_page(block, page, dst, psize);
159 read = psize;
160 }
161 page++;
162 len -= read;
163 dst += read;
164 }
165 return 0;
166}
167#endif
168
Marek Vasut07e69a22011-10-31 14:14:15 +0100169void onenand_spl_load_image(uint32_t offs, uint32_t size, void *dst)
170{
171 uint32_t *addr = (uint32_t *)dst;
Enric Balletbo i Serraad8afa32013-02-07 23:14:47 +0000172 uint32_t to_page;
Marek Vasut07e69a22011-10-31 14:14:15 +0100173 uint32_t block;
174 uint32_t page, rpage;
175 enum onenand_spl_pagesize pagesize;
176 int ret;
177
178 pagesize = onenand_spl_get_geometry();
179
180 /*
181 * The page can be either 2k or 4k, avoid using DIV_ROUND_UP to avoid
182 * pulling further unwanted functions into the SPL.
183 */
184 if (pagesize == 2048) {
Marek Vasut07e69a22011-10-31 14:14:15 +0100185 page = offs / 2048;
Enric Balletbo i Serraad8afa32013-02-07 23:14:47 +0000186 to_page = page + DIV_ROUND_UP(size, 2048);
Marek Vasut07e69a22011-10-31 14:14:15 +0100187 } else {
Marek Vasut07e69a22011-10-31 14:14:15 +0100188 page = offs / 4096;
Enric Balletbo i Serraad8afa32013-02-07 23:14:47 +0000189 to_page = page + DIV_ROUND_UP(size, 4096);
Marek Vasut07e69a22011-10-31 14:14:15 +0100190 }
191
Enric Balletbo i Serraad8afa32013-02-07 23:14:47 +0000192 for (; page <= to_page; page++) {
Marek Vasut07e69a22011-10-31 14:14:15 +0100193 block = page / ONENAND_PAGES_PER_BLOCK;
194 rpage = page & (ONENAND_PAGES_PER_BLOCK - 1);
195 ret = onenand_spl_read_page(block, rpage, addr, pagesize);
Enric Balletbo i Serraad8afa32013-02-07 23:14:47 +0000196 if (ret)
Marek Vasut07e69a22011-10-31 14:14:15 +0100197 page += ONENAND_PAGES_PER_BLOCK - 1;
Enric Balletbo i Serraad8afa32013-02-07 23:14:47 +0000198 else
Marek Vasut07e69a22011-10-31 14:14:15 +0100199 addr += pagesize / 4;
Marek Vasut07e69a22011-10-31 14:14:15 +0100200 }
201}