Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2008 Semihalf |
| 3 | * |
| 4 | * Written by: Piotr Ziecik <kosmo@semihalf.com> |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <common.h> |
| 27 | #include <flash.h> |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 28 | #include <malloc.h> |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 29 | |
| 30 | #include <asm/errno.h> |
| 31 | #include <linux/mtd/mtd.h> |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 32 | #include <linux/mtd/concat.h> |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 33 | |
Kim Phillips | 149bee5 | 2009-07-14 16:00:24 -0500 | [diff] [blame] | 34 | /* use CONFIG_SYS_MAX_FLASH_BANKS_DETECT if defined */ |
| 35 | #ifdef CONFIG_SYS_MAX_FLASH_BANKS_DETECT |
| 36 | # define CFI_MAX_FLASH_BANKS CONFIG_SYS_MAX_FLASH_BANKS_DETECT |
| 37 | #else |
| 38 | # define CFI_MAX_FLASH_BANKS CONFIG_SYS_MAX_FLASH_BANKS |
| 39 | #endif |
| 40 | |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 41 | extern flash_info_t flash_info[]; |
| 42 | |
Kim Phillips | 149bee5 | 2009-07-14 16:00:24 -0500 | [diff] [blame] | 43 | static struct mtd_info cfi_mtd_info[CFI_MAX_FLASH_BANKS]; |
| 44 | static char cfi_mtd_names[CFI_MAX_FLASH_BANKS][16]; |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 45 | #ifdef CONFIG_MTD_CONCAT |
| 46 | static char c_mtd_name[16]; |
| 47 | #endif |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 48 | |
| 49 | static int cfi_mtd_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 50 | { |
| 51 | flash_info_t *fi = mtd->priv; |
| 52 | size_t a_start = fi->start[0] + instr->addr; |
| 53 | size_t a_end = a_start + instr->len; |
| 54 | int s_first = -1; |
| 55 | int s_last = -1; |
| 56 | int error, sect; |
| 57 | |
Stefan Roese | 3a27b0c | 2009-05-11 15:54:13 +0200 | [diff] [blame] | 58 | for (sect = 0; sect < fi->sector_count; sect++) { |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 59 | if (a_start == fi->start[sect]) |
| 60 | s_first = sect; |
| 61 | |
Stefan Roese | 3a27b0c | 2009-05-11 15:54:13 +0200 | [diff] [blame] | 62 | if (sect < fi->sector_count - 1) { |
| 63 | if (a_end == fi->start[sect + 1]) { |
| 64 | s_last = sect; |
| 65 | break; |
| 66 | } |
| 67 | } else { |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 68 | s_last = sect; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (s_first >= 0 && s_first <= s_last) { |
| 74 | instr->state = MTD_ERASING; |
| 75 | |
| 76 | flash_set_verbose(0); |
| 77 | error = flash_erase(fi, s_first, s_last); |
| 78 | flash_set_verbose(1); |
| 79 | |
| 80 | if (error) { |
| 81 | instr->state = MTD_ERASE_FAILED; |
| 82 | return -EIO; |
| 83 | } |
| 84 | |
| 85 | instr->state = MTD_ERASE_DONE; |
| 86 | mtd_erase_callback(instr); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | return -EINVAL; |
| 91 | } |
| 92 | |
| 93 | static int cfi_mtd_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 94 | size_t *retlen, u_char *buf) |
| 95 | { |
| 96 | flash_info_t *fi = mtd->priv; |
| 97 | u_char *f = (u_char*)(fi->start[0]) + from; |
| 98 | |
| 99 | memcpy(buf, f, len); |
| 100 | *retlen = len; |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int cfi_mtd_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 106 | size_t *retlen, const u_char *buf) |
| 107 | { |
| 108 | flash_info_t *fi = mtd->priv; |
| 109 | u_long t = fi->start[0] + to; |
| 110 | int error; |
| 111 | |
| 112 | flash_set_verbose(0); |
| 113 | error = write_buff(fi, (u_char*)buf, t, len); |
| 114 | flash_set_verbose(1); |
| 115 | |
| 116 | if (!error) { |
| 117 | *retlen = len; |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | return -EIO; |
| 122 | } |
| 123 | |
| 124 | static void cfi_mtd_sync(struct mtd_info *mtd) |
| 125 | { |
| 126 | /* |
| 127 | * This function should wait until all pending operations |
| 128 | * finish. However this driver is fully synchronous, so |
| 129 | * this function returns immediately |
| 130 | */ |
| 131 | } |
| 132 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 133 | 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] | 134 | { |
| 135 | flash_info_t *fi = mtd->priv; |
| 136 | |
| 137 | flash_set_verbose(0); |
| 138 | flash_protect(FLAG_PROTECT_SET, fi->start[0] + ofs, |
| 139 | fi->start[0] + ofs + len - 1, fi); |
| 140 | flash_set_verbose(1); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Stefan Roese | 586b3a6 | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 145 | 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] | 146 | { |
| 147 | flash_info_t *fi = mtd->priv; |
| 148 | |
| 149 | flash_set_verbose(0); |
| 150 | flash_protect(FLAG_PROTECT_CLEAR, fi->start[0] + ofs, |
| 151 | fi->start[0] + ofs + len - 1, fi); |
| 152 | flash_set_verbose(1); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi) |
| 158 | { |
| 159 | int sect_size = 0; |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 160 | int sect_size_old = 0; |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 161 | int sect; |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 162 | int regions = 0; |
| 163 | int numblocks = 0; |
Ladislav Michl | e5d690f | 2010-01-28 12:27:14 +0100 | [diff] [blame] | 164 | ulong offset; |
| 165 | ulong base_addr; |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 166 | |
Stefan Roese | 337691d | 2009-03-18 11:17:37 +0100 | [diff] [blame] | 167 | /* |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 168 | * First detect the number of eraseregions so that we can allocate |
| 169 | * the array of eraseregions correctly |
Stefan Roese | 337691d | 2009-03-18 11:17:37 +0100 | [diff] [blame] | 170 | */ |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 171 | for (sect = 0; sect < fi->sector_count; sect++) { |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 172 | if (sect_size_old != flash_sector_size(fi, sect)) |
| 173 | regions++; |
| 174 | sect_size_old = flash_sector_size(fi, sect); |
| 175 | } |
| 176 | |
Ladislav Michl | e5d690f | 2010-01-28 12:27:14 +0100 | [diff] [blame] | 177 | switch (regions) { |
| 178 | case 0: |
| 179 | return 1; |
| 180 | case 1: /* flash has uniform erase size */ |
| 181 | mtd->numeraseregions = 0; |
| 182 | mtd->erasesize = sect_size_old; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | mtd->numeraseregions = regions; |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 187 | mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info) * regions); |
| 188 | |
| 189 | /* |
| 190 | * Now detect the largest sector and fill the eraseregions |
| 191 | */ |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 192 | regions = 0; |
Ladislav Michl | e5d690f | 2010-01-28 12:27:14 +0100 | [diff] [blame] | 193 | base_addr = offset = fi->start[0]; |
| 194 | sect_size_old = flash_sector_size(fi, 0); |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 195 | for (sect = 0; sect < fi->sector_count; sect++) { |
Ladislav Michl | e5d690f | 2010-01-28 12:27:14 +0100 | [diff] [blame] | 196 | if (sect_size_old != flash_sector_size(fi, sect)) { |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 197 | mtd->eraseregions[regions].offset = offset - base_addr; |
| 198 | mtd->eraseregions[regions].erasesize = sect_size_old; |
| 199 | mtd->eraseregions[regions].numblocks = numblocks; |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 200 | /* Now start counting the next eraseregions */ |
| 201 | numblocks = 0; |
| 202 | regions++; |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 203 | offset = fi->start[sect]; |
Ladislav Michl | e5d690f | 2010-01-28 12:27:14 +0100 | [diff] [blame] | 204 | } |
| 205 | numblocks++; |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 206 | |
| 207 | /* |
| 208 | * Select the largest sector size as erasesize (e.g. for UBI) |
| 209 | */ |
Stefan Roese | 337691d | 2009-03-18 11:17:37 +0100 | [diff] [blame] | 210 | if (flash_sector_size(fi, sect) > sect_size) |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 211 | sect_size = flash_sector_size(fi, sect); |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 212 | |
| 213 | sect_size_old = flash_sector_size(fi, sect); |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 216 | /* |
| 217 | * Set the last region |
| 218 | */ |
| 219 | mtd->eraseregions[regions].offset = offset - base_addr; |
| 220 | mtd->eraseregions[regions].erasesize = sect_size_old; |
Ladislav Michl | e5d690f | 2010-01-28 12:27:14 +0100 | [diff] [blame] | 221 | mtd->eraseregions[regions].numblocks = numblocks; |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 222 | |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 223 | mtd->erasesize = sect_size; |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | int cfi_mtd_init(void) |
| 229 | { |
| 230 | struct mtd_info *mtd; |
| 231 | flash_info_t *fi; |
| 232 | int error, i; |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 233 | int devices_found = 0; |
| 234 | struct mtd_info *mtd_list[CONFIG_SYS_MAX_FLASH_BANKS]; |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 235 | |
| 236 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
| 237 | fi = &flash_info[i]; |
| 238 | mtd = &cfi_mtd_info[i]; |
| 239 | |
| 240 | memset(mtd, 0, sizeof(struct mtd_info)); |
| 241 | |
| 242 | error = cfi_mtd_set_erasesize(mtd, fi); |
| 243 | if (error) |
| 244 | continue; |
| 245 | |
Andreas Huber | 7bf15f8 | 2009-04-02 17:15:34 +0200 | [diff] [blame] | 246 | sprintf(cfi_mtd_names[i], "nor%d", i); |
| 247 | mtd->name = cfi_mtd_names[i]; |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 248 | mtd->type = MTD_NORFLASH; |
| 249 | mtd->flags = MTD_CAP_NORFLASH; |
| 250 | mtd->size = fi->size; |
| 251 | mtd->writesize = 1; |
| 252 | |
| 253 | mtd->erase = cfi_mtd_erase; |
| 254 | mtd->read = cfi_mtd_read; |
| 255 | mtd->write = cfi_mtd_write; |
| 256 | mtd->sync = cfi_mtd_sync; |
| 257 | mtd->lock = cfi_mtd_lock; |
| 258 | mtd->unlock = cfi_mtd_unlock; |
| 259 | mtd->priv = fi; |
| 260 | |
| 261 | if (add_mtd_device(mtd)) |
| 262 | return -ENOMEM; |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 263 | |
| 264 | mtd_list[devices_found++] = mtd; |
| 265 | } |
| 266 | |
| 267 | #ifdef CONFIG_MTD_CONCAT |
| 268 | if (devices_found > 1) { |
| 269 | /* |
| 270 | * We detected multiple devices. Concatenate them together. |
| 271 | */ |
| 272 | sprintf(c_mtd_name, "nor%d", devices_found); |
| 273 | mtd = mtd_concat_create(mtd_list, devices_found, c_mtd_name); |
| 274 | |
| 275 | if (mtd == NULL) |
| 276 | return -ENXIO; |
| 277 | |
| 278 | if (add_mtd_device(mtd)) |
| 279 | return -ENOMEM; |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 280 | } |
Stefan Roese | e0860d4 | 2009-05-12 14:29:39 +0200 | [diff] [blame] | 281 | #endif /* CONFIG_MTD_CONCAT */ |
Piotr Ziecik | 3e939e9 | 2008-11-17 15:57:58 +0100 | [diff] [blame] | 282 | |
| 283 | return 0; |
| 284 | } |