Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2008 Semihalf |
| 4 | * |
| 5 | * Written by: Piotr Ziecik <kosmo@semihalf.com> |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Vignesh Raghavendra | 3f891a1 | 2020-09-17 16:53:08 +0530 | [diff] [blame^] | 9 | #include <dma.h> |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 10 | #include <flash.h> |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 11 | #include <malloc.h> |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 12 | |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 13 | #include <linux/errno.h> |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 14 | #include <linux/mtd/mtd.h> |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 15 | #include <linux/mtd/concat.h> |
Stefan Roese | 3c29975 | 2010-08-31 10:04:11 +0200 | [diff] [blame] | 16 | #include <mtd/cfi_flash.h> |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 17 | |
Kim Phillips | 9578718 | 2009-07-14 16:00:24 -0500 | [diff] [blame] | 18 | static struct mtd_info cfi_mtd_info[CFI_MAX_FLASH_BANKS]; |
| 19 | static char cfi_mtd_names[CFI_MAX_FLASH_BANKS][16]; |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 20 | #ifdef CONFIG_MTD_CONCAT |
| 21 | static char c_mtd_name[16]; |
| 22 | #endif |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 23 | |
| 24 | static int cfi_mtd_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 25 | { |
| 26 | flash_info_t *fi = mtd->priv; |
| 27 | size_t a_start = fi->start[0] + instr->addr; |
| 28 | size_t a_end = a_start + instr->len; |
| 29 | int s_first = -1; |
| 30 | int s_last = -1; |
| 31 | int error, sect; |
| 32 | |
Stefan Roese | dba6fcf | 2009-05-11 15:54:13 +0200 | [diff] [blame] | 33 | for (sect = 0; sect < fi->sector_count; sect++) { |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 34 | if (a_start == fi->start[sect]) |
| 35 | s_first = sect; |
| 36 | |
Stefan Roese | dba6fcf | 2009-05-11 15:54:13 +0200 | [diff] [blame] | 37 | if (sect < fi->sector_count - 1) { |
| 38 | if (a_end == fi->start[sect + 1]) { |
| 39 | s_last = sect; |
| 40 | break; |
| 41 | } |
| 42 | } else { |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 43 | s_last = sect; |
| 44 | break; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | if (s_first >= 0 && s_first <= s_last) { |
| 49 | instr->state = MTD_ERASING; |
| 50 | |
| 51 | flash_set_verbose(0); |
| 52 | error = flash_erase(fi, s_first, s_last); |
| 53 | flash_set_verbose(1); |
| 54 | |
| 55 | if (error) { |
| 56 | instr->state = MTD_ERASE_FAILED; |
| 57 | return -EIO; |
| 58 | } |
| 59 | |
| 60 | instr->state = MTD_ERASE_DONE; |
| 61 | mtd_erase_callback(instr); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | return -EINVAL; |
| 66 | } |
| 67 | |
| 68 | static int cfi_mtd_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 69 | size_t *retlen, u_char *buf) |
| 70 | { |
| 71 | flash_info_t *fi = mtd->priv; |
| 72 | u_char *f = (u_char*)(fi->start[0]) + from; |
| 73 | |
Vignesh Raghavendra | 3f891a1 | 2020-09-17 16:53:08 +0530 | [diff] [blame^] | 74 | if (dma_memcpy(buf, f, len) < 0) |
| 75 | memcpy(buf, f, len); |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 76 | *retlen = len; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int cfi_mtd_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 82 | size_t *retlen, const u_char *buf) |
| 83 | { |
| 84 | flash_info_t *fi = mtd->priv; |
| 85 | u_long t = fi->start[0] + to; |
| 86 | int error; |
| 87 | |
| 88 | flash_set_verbose(0); |
| 89 | error = write_buff(fi, (u_char*)buf, t, len); |
| 90 | flash_set_verbose(1); |
| 91 | |
| 92 | if (!error) { |
| 93 | *retlen = len; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | return -EIO; |
| 98 | } |
| 99 | |
| 100 | static void cfi_mtd_sync(struct mtd_info *mtd) |
| 101 | { |
| 102 | /* |
| 103 | * This function should wait until all pending operations |
| 104 | * finish. However this driver is fully synchronous, so |
| 105 | * this function returns immediately |
| 106 | */ |
| 107 | } |
| 108 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 109 | static int cfi_mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 110 | { |
| 111 | flash_info_t *fi = mtd->priv; |
| 112 | |
| 113 | flash_set_verbose(0); |
| 114 | flash_protect(FLAG_PROTECT_SET, fi->start[0] + ofs, |
| 115 | fi->start[0] + ofs + len - 1, fi); |
| 116 | flash_set_verbose(1); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 121 | static int cfi_mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 122 | { |
| 123 | flash_info_t *fi = mtd->priv; |
| 124 | |
| 125 | flash_set_verbose(0); |
| 126 | flash_protect(FLAG_PROTECT_CLEAR, fi->start[0] + ofs, |
| 127 | fi->start[0] + ofs + len - 1, fi); |
| 128 | flash_set_verbose(1); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi) |
| 134 | { |
| 135 | int sect_size = 0; |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 136 | int sect_size_old = 0; |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 137 | int sect; |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 138 | int regions = 0; |
| 139 | int numblocks = 0; |
Ladislav Michl | f3dec79 | 2010-01-28 12:27:14 +0100 | [diff] [blame] | 140 | ulong offset; |
| 141 | ulong base_addr; |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 142 | |
Stefan Roese | f8e2b31 | 2009-03-18 11:17:37 +0100 | [diff] [blame] | 143 | /* |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 144 | * First detect the number of eraseregions so that we can allocate |
| 145 | * the array of eraseregions correctly |
Stefan Roese | f8e2b31 | 2009-03-18 11:17:37 +0100 | [diff] [blame] | 146 | */ |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 147 | for (sect = 0; sect < fi->sector_count; sect++) { |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 148 | if (sect_size_old != flash_sector_size(fi, sect)) |
| 149 | regions++; |
| 150 | sect_size_old = flash_sector_size(fi, sect); |
| 151 | } |
| 152 | |
Ladislav Michl | f3dec79 | 2010-01-28 12:27:14 +0100 | [diff] [blame] | 153 | switch (regions) { |
| 154 | case 0: |
| 155 | return 1; |
| 156 | case 1: /* flash has uniform erase size */ |
| 157 | mtd->numeraseregions = 0; |
| 158 | mtd->erasesize = sect_size_old; |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | mtd->numeraseregions = regions; |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 163 | mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info) * regions); |
| 164 | |
| 165 | /* |
| 166 | * Now detect the largest sector and fill the eraseregions |
| 167 | */ |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 168 | regions = 0; |
Ladislav Michl | f3dec79 | 2010-01-28 12:27:14 +0100 | [diff] [blame] | 169 | base_addr = offset = fi->start[0]; |
| 170 | sect_size_old = flash_sector_size(fi, 0); |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 171 | for (sect = 0; sect < fi->sector_count; sect++) { |
Ladislav Michl | f3dec79 | 2010-01-28 12:27:14 +0100 | [diff] [blame] | 172 | if (sect_size_old != flash_sector_size(fi, sect)) { |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 173 | mtd->eraseregions[regions].offset = offset - base_addr; |
| 174 | mtd->eraseregions[regions].erasesize = sect_size_old; |
| 175 | mtd->eraseregions[regions].numblocks = numblocks; |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 176 | /* Now start counting the next eraseregions */ |
| 177 | numblocks = 0; |
| 178 | regions++; |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 179 | offset = fi->start[sect]; |
Ladislav Michl | f3dec79 | 2010-01-28 12:27:14 +0100 | [diff] [blame] | 180 | } |
| 181 | numblocks++; |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * Select the largest sector size as erasesize (e.g. for UBI) |
| 185 | */ |
Stefan Roese | f8e2b31 | 2009-03-18 11:17:37 +0100 | [diff] [blame] | 186 | if (flash_sector_size(fi, sect) > sect_size) |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 187 | sect_size = flash_sector_size(fi, sect); |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 188 | |
| 189 | sect_size_old = flash_sector_size(fi, sect); |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 192 | /* |
| 193 | * Set the last region |
| 194 | */ |
| 195 | mtd->eraseregions[regions].offset = offset - base_addr; |
| 196 | mtd->eraseregions[regions].erasesize = sect_size_old; |
Ladislav Michl | f3dec79 | 2010-01-28 12:27:14 +0100 | [diff] [blame] | 197 | mtd->eraseregions[regions].numblocks = numblocks; |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 198 | |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 199 | mtd->erasesize = sect_size; |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | int cfi_mtd_init(void) |
| 205 | { |
| 206 | struct mtd_info *mtd; |
| 207 | flash_info_t *fi; |
| 208 | int error, i; |
Wolfgang Denk | 419a1fe | 2011-10-05 22:48:12 +0200 | [diff] [blame] | 209 | #ifdef CONFIG_MTD_CONCAT |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 210 | int devices_found = 0; |
| 211 | struct mtd_info *mtd_list[CONFIG_SYS_MAX_FLASH_BANKS]; |
Wolfgang Denk | 419a1fe | 2011-10-05 22:48:12 +0200 | [diff] [blame] | 212 | #endif |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 213 | |
| 214 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
| 215 | fi = &flash_info[i]; |
| 216 | mtd = &cfi_mtd_info[i]; |
| 217 | |
| 218 | memset(mtd, 0, sizeof(struct mtd_info)); |
| 219 | |
| 220 | error = cfi_mtd_set_erasesize(mtd, fi); |
| 221 | if (error) |
| 222 | continue; |
| 223 | |
Andreas Huber | c203ef5 | 2009-04-02 17:15:34 +0200 | [diff] [blame] | 224 | sprintf(cfi_mtd_names[i], "nor%d", i); |
| 225 | mtd->name = cfi_mtd_names[i]; |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 226 | mtd->type = MTD_NORFLASH; |
| 227 | mtd->flags = MTD_CAP_NORFLASH; |
| 228 | mtd->size = fi->size; |
| 229 | mtd->writesize = 1; |
Heiko Schocher | 65e5cb5 | 2014-10-31 11:29:45 +0100 | [diff] [blame] | 230 | mtd->writebufsize = mtd->writesize; |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 231 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 232 | mtd->_erase = cfi_mtd_erase; |
| 233 | mtd->_read = cfi_mtd_read; |
| 234 | mtd->_write = cfi_mtd_write; |
| 235 | mtd->_sync = cfi_mtd_sync; |
| 236 | mtd->_lock = cfi_mtd_lock; |
| 237 | mtd->_unlock = cfi_mtd_unlock; |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 238 | mtd->priv = fi; |
| 239 | |
| 240 | if (add_mtd_device(mtd)) |
| 241 | return -ENOMEM; |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 242 | |
Wolfgang Denk | 419a1fe | 2011-10-05 22:48:12 +0200 | [diff] [blame] | 243 | #ifdef CONFIG_MTD_CONCAT |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 244 | mtd_list[devices_found++] = mtd; |
Wolfgang Denk | 419a1fe | 2011-10-05 22:48:12 +0200 | [diff] [blame] | 245 | #endif |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Stefan Roese | 0a57265 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 248 | #ifdef CONFIG_MTD_CONCAT |
| 249 | if (devices_found > 1) { |
| 250 | /* |
| 251 | * We detected multiple devices. Concatenate them together. |
| 252 | */ |
| 253 | sprintf(c_mtd_name, "nor%d", devices_found); |
| 254 | mtd = mtd_concat_create(mtd_list, devices_found, c_mtd_name); |
| 255 | |
| 256 | if (mtd == NULL) |
| 257 | return -ENXIO; |
| 258 | |
| 259 | if (add_mtd_device(mtd)) |
| 260 | return -ENOMEM; |
| 261 | } |
| 262 | #endif /* CONFIG_MTD_CONCAT */ |
| 263 | |
Piotr Ziecik | 91809ed | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 264 | return 0; |
| 265 | } |