Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 2 | /* |
| 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 Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 10 | #include <config.h> |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 11 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 13 | #include <linux/string.h> |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 14 | #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 | */ |
| 22 | enum onenand_spl_pagesize { |
| 23 | PAGE_2K = 2048, |
| 24 | PAGE_4K = 4096, |
| 25 | }; |
| 26 | |
Ladislav Michl | e9c173e | 2017-06-20 11:44:29 +0200 | [diff] [blame] | 27 | static unsigned int density_mask; |
| 28 | |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 29 | #define ONENAND_PAGES_PER_BLOCK 64 |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 30 | #define onenand_sector_address(page) (page << 2) |
| 31 | #define onenand_buffer_address() ((1 << 3) << 8) |
Ladislav Michl | e9c173e | 2017-06-20 11:44:29 +0200 | [diff] [blame] | 32 | |
| 33 | static 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 | |
| 42 | static 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 Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 50 | |
| 51 | static inline uint16_t onenand_readw(uint32_t addr) |
| 52 | { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 53 | return readw(CFG_SYS_ONENAND_BASE + addr); |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static inline void onenand_writew(uint16_t value, uint32_t addr) |
| 57 | { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 58 | writew(value, CFG_SYS_ONENAND_BASE + addr); |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static enum onenand_spl_pagesize onenand_spl_get_geometry(void) |
| 62 | { |
Ladislav Michl | e9c173e | 2017-06-20 11:44:29 +0200 | [diff] [blame] | 63 | unsigned int dev_id, density, size; |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 64 | |
| 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 Michl | e9c173e | 2017-06-20 11:44:29 +0200 | [diff] [blame] | 73 | 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 Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 76 | return PAGE_2K; |
Ladislav Michl | e9c173e | 2017-06-20 11:44:29 +0200 | [diff] [blame] | 77 | } |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | return PAGE_4K; |
| 81 | } |
| 82 | |
| 83 | static int onenand_spl_read_page(uint32_t block, uint32_t page, uint32_t *buf, |
| 84 | enum onenand_spl_pagesize pagesize) |
| 85 | { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 86 | const uint32_t addr = CFG_SYS_ONENAND_BASE + ONENAND_DATARAM; |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 87 | 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 Michl | dcd7ed3 | 2016-07-12 20:28:11 +0200 | [diff] [blame] | 118 | #ifdef CONFIG_SPL_UBI |
| 119 | /* Temporary storage for non page aligned and non page sized reads. */ |
| 120 | static 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 | */ |
| 133 | int onenand_spl_read_block(int block, int offset, int len, void *dst) |
| 134 | { |
Ladislav Michl | f7081e9 | 2017-06-20 11:43:22 +0200 | [diff] [blame] | 135 | int page, read; |
| 136 | static int psize; |
| 137 | |
| 138 | if (!psize) |
| 139 | psize = onenand_spl_get_geometry(); |
Ladislav Michl | dcd7ed3 | 2016-07-12 20:28:11 +0200 | [diff] [blame] | 140 | |
Ladislav Michl | dcd7ed3 | 2016-07-12 20:28:11 +0200 | [diff] [blame] | 141 | /* 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 Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 169 | void onenand_spl_load_image(uint32_t offs, uint32_t size, void *dst) |
| 170 | { |
| 171 | uint32_t *addr = (uint32_t *)dst; |
Enric Balletbo i Serra | ad8afa3 | 2013-02-07 23:14:47 +0000 | [diff] [blame] | 172 | uint32_t to_page; |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 173 | 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 Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 185 | page = offs / 2048; |
Enric Balletbo i Serra | ad8afa3 | 2013-02-07 23:14:47 +0000 | [diff] [blame] | 186 | to_page = page + DIV_ROUND_UP(size, 2048); |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 187 | } else { |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 188 | page = offs / 4096; |
Enric Balletbo i Serra | ad8afa3 | 2013-02-07 23:14:47 +0000 | [diff] [blame] | 189 | to_page = page + DIV_ROUND_UP(size, 4096); |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Enric Balletbo i Serra | ad8afa3 | 2013-02-07 23:14:47 +0000 | [diff] [blame] | 192 | for (; page <= to_page; page++) { |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 193 | 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 Serra | ad8afa3 | 2013-02-07 23:14:47 +0000 | [diff] [blame] | 196 | if (ret) |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 197 | page += ONENAND_PAGES_PER_BLOCK - 1; |
Enric Balletbo i Serra | ad8afa3 | 2013-02-07 23:14:47 +0000 | [diff] [blame] | 198 | else |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 199 | addr += pagesize / 4; |
Marek Vasut | 07e69a2 | 2011-10-31 14:14:15 +0100 | [diff] [blame] | 200 | } |
| 201 | } |