2 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5 * Copyright (C) 2005-2009 Samsung Electronics
6 * Kyungmin Park <kyungmin.park@samsung.com>
8 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/mtd/onenand_regs.h>
14 #include <onenand_uboot.h>
18 * - 2048b page, 128k erase block.
19 * - 4096b page, 256k erase block.
21 enum onenand_spl_pagesize {
26 static unsigned int density_mask;
28 #define ONENAND_PAGES_PER_BLOCK 64
29 #define onenand_sector_address(page) (page << 2)
30 #define onenand_buffer_address() ((1 << 3) << 8)
32 static inline int onenand_block_address(int block)
34 /* Device Flash Core select, NAND Flash Block Address */
35 if (block & density_mask)
36 return ONENAND_DDP_CHIP1 | (block ^ density_mask);
41 static inline int onenand_bufferram_address(int block)
43 /* Device BufferRAM Select */
44 if (block & density_mask)
45 return ONENAND_DDP_CHIP1;
47 return ONENAND_DDP_CHIP0;
50 static inline uint16_t onenand_readw(uint32_t addr)
52 return readw(CONFIG_SYS_ONENAND_BASE + addr);
55 static inline void onenand_writew(uint16_t value, uint32_t addr)
57 writew(value, CONFIG_SYS_ONENAND_BASE + addr);
60 static enum onenand_spl_pagesize onenand_spl_get_geometry(void)
62 unsigned int dev_id, density, size;
64 if (!onenand_readw(ONENAND_REG_TECHNOLOGY)) {
65 dev_id = onenand_readw(ONENAND_REG_DEVICE_ID);
66 density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
67 density &= ONENAND_DEVICE_DENSITY_MASK;
69 if (density < ONENAND_DEVICE_DENSITY_4Gb)
72 if (dev_id & ONENAND_DEVICE_IS_DDP) {
73 size = onenand_readw(ONENAND_REG_DATA_BUFFER_SIZE);
74 density_mask = 1 << (18 + density - ffs(size));
82 static int onenand_spl_read_page(uint32_t block, uint32_t page, uint32_t *buf,
83 enum onenand_spl_pagesize pagesize)
85 const uint32_t addr = CONFIG_SYS_ONENAND_BASE + ONENAND_DATARAM;
88 onenand_writew(onenand_block_address(block),
89 ONENAND_REG_START_ADDRESS1);
91 onenand_writew(onenand_bufferram_address(block),
92 ONENAND_REG_START_ADDRESS2);
94 onenand_writew(onenand_sector_address(page),
95 ONENAND_REG_START_ADDRESS8);
97 onenand_writew(onenand_buffer_address(),
98 ONENAND_REG_START_BUFFER);
100 onenand_writew(ONENAND_INT_CLEAR, ONENAND_REG_INTERRUPT);
102 onenand_writew(ONENAND_CMD_READ, ONENAND_REG_COMMAND);
104 while (!(onenand_readw(ONENAND_REG_INTERRUPT) & ONENAND_INT_READ))
107 /* Check for invalid block mark */
108 if (page < 2 && (onenand_readw(ONENAND_SPARERAM) != 0xffff))
111 for (offset = 0; offset < pagesize; offset += 4)
112 buf[offset / 4] = readl(addr + offset);
117 #ifdef CONFIG_SPL_UBI
118 /* Temporary storage for non page aligned and non page sized reads. */
119 static u8 scratch_buf[PAGE_4K];
122 * onenand_spl_read_block - Read data from physical eraseblock into a buffer
123 * @block: Number of the physical eraseblock
124 * @offset: Data offset from the start of @peb
125 * @len: Data size to read
126 * @dst: Address of the destination buffer
129 * @offset + @len are not allowed to be larger than a physical
130 * erase block. No sanity check done for simplicity reasons.
132 int onenand_spl_read_block(int block, int offset, int len, void *dst)
138 psize = onenand_spl_get_geometry();
140 /* Calculate the page number */
141 page = offset / psize;
142 /* Offset to the start of a flash page */
143 offset = offset % psize;
147 * Non page aligned reads go to the scratch buffer.
148 * Page aligned reads go directly to the destination.
150 if (offset || len < psize) {
151 onenand_spl_read_page(block, page,
152 (uint32_t *)scratch_buf, psize);
153 read = min(len, psize - offset);
154 memcpy(dst, scratch_buf + offset, read);
157 onenand_spl_read_page(block, page, dst, psize);
168 void onenand_spl_load_image(uint32_t offs, uint32_t size, void *dst)
170 uint32_t *addr = (uint32_t *)dst;
173 uint32_t page, rpage;
174 enum onenand_spl_pagesize pagesize;
177 pagesize = onenand_spl_get_geometry();
180 * The page can be either 2k or 4k, avoid using DIV_ROUND_UP to avoid
181 * pulling further unwanted functions into the SPL.
183 if (pagesize == 2048) {
185 to_page = page + DIV_ROUND_UP(size, 2048);
188 to_page = page + DIV_ROUND_UP(size, 4096);
191 for (; page <= to_page; page++) {
192 block = page / ONENAND_PAGES_PER_BLOCK;
193 rpage = page & (ONENAND_PAGES_PER_BLOCK - 1);
194 ret = onenand_spl_read_page(block, rpage, addr, pagesize);
196 page += ONENAND_PAGES_PER_BLOCK - 1;
198 addr += pagesize / 4;