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