Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2003 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 7 | #include <config.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 8 | #include <console.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Simon Glass | 8e20188 | 2020-05-10 11:39:54 -0600 | [diff] [blame] | 10 | #include <flash.h> |
Simon Glass | 8f3f761 | 2019-11-14 12:57:42 -0700 | [diff] [blame] | 11 | #include <irq_func.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <time.h> |
Simon Glass | 8e20188 | 2020-05-10 11:39:54 -0600 | [diff] [blame] | 14 | #include <uuid.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 15 | #include <vsprintf.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 16 | #include <linux/delay.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 17 | #include <linux/string.h> |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 18 | |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 19 | #define PHYS_FLASH_1 CFG_SYS_FLASH_BASE |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 20 | #define FLASH_BANK_SIZE 0x200000 |
| 21 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 22 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 23 | |
Simon Glass | c744316 | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 24 | void flash_print_info(flash_info_t *info) |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 25 | { |
| 26 | int i; |
| 27 | |
| 28 | switch (info->flash_id & FLASH_VENDMASK) { |
| 29 | case (AMD_MANUFACT & FLASH_VENDMASK): |
| 30 | printf ("AMD: "); |
| 31 | break; |
| 32 | default: |
| 33 | printf ("Unknown Vendor "); |
| 34 | break; |
| 35 | } |
| 36 | |
| 37 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 38 | case (AMD_ID_PL160CB & FLASH_TYPEMASK): |
| 39 | printf ("AM29PL160CB (16Mbit)\n"); |
| 40 | break; |
| 41 | default: |
| 42 | printf ("Unknown Chip Type\n"); |
| 43 | goto Done; |
| 44 | break; |
| 45 | } |
| 46 | |
| 47 | printf (" Size: %ld MB in %d Sectors\n", |
| 48 | info->size >> 20, info->sector_count); |
| 49 | |
| 50 | printf (" Sector Start Addresses:"); |
| 51 | for (i = 0; i < info->sector_count; i++) { |
| 52 | if ((i % 5) == 0) { |
| 53 | printf ("\n "); |
| 54 | } |
| 55 | printf (" %08lX%s", info->start[i], |
| 56 | info->protect[i] ? " (RO)" : " "); |
| 57 | } |
| 58 | printf ("\n"); |
| 59 | |
| 60 | Done: |
Marian Balakowicz | 3e8b1dc | 2006-05-09 11:28:36 +0200 | [diff] [blame] | 61 | return; |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Simon Glass | c744316 | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 64 | unsigned long flash_init(void) |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 65 | { |
| 66 | int i, j; |
| 67 | ulong size = 0; |
| 68 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 69 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 70 | ulong flashbase = 0; |
| 71 | |
| 72 | flash_info[i].flash_id = |
| 73 | (AMD_MANUFACT & FLASH_VENDMASK) | |
| 74 | (AMD_ID_PL160CB & FLASH_TYPEMASK); |
| 75 | flash_info[i].size = FLASH_BANK_SIZE; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 76 | flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT; |
| 77 | memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT); |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 78 | if (i == 0) |
| 79 | flashbase = PHYS_FLASH_1; |
| 80 | else |
| 81 | panic ("configured to many flash banks!\n"); |
| 82 | |
| 83 | for (j = 0; j < flash_info[i].sector_count; j++) { |
| 84 | if (j == 0) { |
| 85 | /* 1st is 16 KiB */ |
| 86 | flash_info[i].start[j] = flashbase; |
| 87 | } |
| 88 | if ((j >= 1) && (j <= 2)) { |
| 89 | /* 2nd and 3rd are 8 KiB */ |
| 90 | flash_info[i].start[j] = |
| 91 | flashbase + 0x4000 + 0x2000 * (j - 1); |
| 92 | } |
| 93 | if (j == 3) { |
| 94 | /* 4th is 224 KiB */ |
| 95 | flash_info[i].start[j] = flashbase + 0x8000; |
| 96 | } |
| 97 | if ((j >= 4) && (j <= 10)) { |
| 98 | /* rest is 256 KiB */ |
| 99 | flash_info[i].start[j] = |
| 100 | flashbase + 0x40000 + 0x40000 * (j - |
| 101 | 4); |
| 102 | } |
| 103 | } |
| 104 | size += flash_info[i].size; |
| 105 | } |
| 106 | |
Simon Glass | c744316 | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 107 | flash_protect(FLAG_PROTECT_SET, |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 108 | CFG_SYS_FLASH_BASE, |
| 109 | CFG_SYS_FLASH_BASE + 0x3ffff, &flash_info[0]); |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 110 | |
| 111 | return size; |
| 112 | } |
| 113 | |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 114 | #define CMD_READ_ARRAY 0x00F0 |
| 115 | #define CMD_UNLOCK1 0x00AA |
| 116 | #define CMD_UNLOCK2 0x0055 |
| 117 | #define CMD_ERASE_SETUP 0x0080 |
| 118 | #define CMD_ERASE_CONFIRM 0x0030 |
| 119 | #define CMD_PROGRAM 0x00A0 |
| 120 | #define CMD_UNLOCK_BYPASS 0x0020 |
| 121 | |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 122 | #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_SYS_FLASH_BASE + (0x00000555<<1))) |
| 123 | #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_SYS_FLASH_BASE + (0x000002AA<<1))) |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 124 | |
| 125 | #define BIT_ERASE_DONE 0x0080 |
| 126 | #define BIT_RDY_MASK 0x0080 |
| 127 | #define BIT_PROGRAM_ERROR 0x0020 |
| 128 | #define BIT_TIMEOUT 0x80000000 /* our flag */ |
| 129 | |
| 130 | #define READY 1 |
| 131 | #define ERR 2 |
| 132 | #define TMO 4 |
| 133 | |
Simon Glass | c744316 | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 134 | int flash_erase(flash_info_t *info, int s_first, int s_last) |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 135 | { |
| 136 | ulong result; |
| 137 | int iflag, cflag, prot, sect; |
| 138 | int rc = ERR_OK; |
| 139 | int chip1; |
Graeme Russ | 43f0255 | 2011-06-28 01:40:55 +0000 | [diff] [blame] | 140 | ulong start; |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 141 | |
| 142 | /* first look for protection bits */ |
| 143 | |
| 144 | if (info->flash_id == FLASH_UNKNOWN) |
| 145 | return ERR_UNKNOWN_FLASH_TYPE; |
| 146 | |
| 147 | if ((s_first < 0) || (s_first > s_last)) { |
| 148 | return ERR_INVAL; |
| 149 | } |
| 150 | |
| 151 | if ((info->flash_id & FLASH_VENDMASK) != |
| 152 | (AMD_MANUFACT & FLASH_VENDMASK)) { |
| 153 | return ERR_UNKNOWN_FLASH_VENDOR; |
| 154 | } |
| 155 | |
| 156 | prot = 0; |
| 157 | for (sect = s_first; sect <= s_last; ++sect) { |
| 158 | if (info->protect[sect]) { |
| 159 | prot++; |
| 160 | } |
| 161 | } |
| 162 | if (prot) |
| 163 | return ERR_PROTECTED; |
| 164 | |
| 165 | /* |
| 166 | * Disable interrupts which might cause a timeout |
| 167 | * here. Remember that our exception vectors are |
| 168 | * at address 0 in the flash, and we don't want a |
| 169 | * (ticker) exception to happen while the flash |
| 170 | * chip is in programming mode. |
| 171 | */ |
| 172 | |
Simon Glass | fbf091b | 2019-11-14 12:57:36 -0700 | [diff] [blame] | 173 | cflag = icache_status(); |
| 174 | icache_disable(); |
Simon Glass | f87959b | 2019-11-14 12:57:40 -0700 | [diff] [blame] | 175 | iflag = disable_interrupts(); |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 176 | |
| 177 | printf ("\n"); |
| 178 | |
| 179 | /* Start erase on unprotected sectors */ |
| 180 | for (sect = s_first; sect <= s_last && !ctrlc (); sect++) { |
| 181 | printf ("Erasing sector %2d ... ", sect); |
| 182 | |
| 183 | /* arm simple, non interrupt dependent timer */ |
Graeme Russ | 43f0255 | 2011-06-28 01:40:55 +0000 | [diff] [blame] | 184 | start = get_timer(0); |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 185 | |
| 186 | if (info->protect[sect] == 0) { /* not protected */ |
| 187 | volatile u16 *addr = |
| 188 | (volatile u16 *) (info->start[sect]); |
| 189 | |
| 190 | MEM_FLASH_ADDR1 = CMD_UNLOCK1; |
| 191 | MEM_FLASH_ADDR2 = CMD_UNLOCK2; |
| 192 | MEM_FLASH_ADDR1 = CMD_ERASE_SETUP; |
| 193 | |
| 194 | MEM_FLASH_ADDR1 = CMD_UNLOCK1; |
| 195 | MEM_FLASH_ADDR2 = CMD_UNLOCK2; |
| 196 | *addr = CMD_ERASE_CONFIRM; |
| 197 | |
| 198 | /* wait until flash is ready */ |
| 199 | chip1 = 0; |
| 200 | |
| 201 | do { |
| 202 | result = *addr; |
| 203 | |
Tom Rini | 78f8800 | 2022-07-23 13:05:00 -0400 | [diff] [blame] | 204 | /* check timeout, 1000ms */ |
| 205 | if (get_timer(start) > 1000) { |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 206 | MEM_FLASH_ADDR1 = CMD_READ_ARRAY; |
| 207 | chip1 = TMO; |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | if (!chip1 |
| 212 | && (result & 0xFFFF) & BIT_ERASE_DONE) |
| 213 | chip1 = READY; |
| 214 | |
| 215 | } while (!chip1); |
| 216 | |
| 217 | MEM_FLASH_ADDR1 = CMD_READ_ARRAY; |
| 218 | |
| 219 | if (chip1 == ERR) { |
| 220 | rc = ERR_PROG_ERROR; |
| 221 | goto outahere; |
| 222 | } |
| 223 | if (chip1 == TMO) { |
Mario Six | 324b940 | 2018-01-26 14:43:52 +0100 | [diff] [blame] | 224 | rc = ERR_TIMEOUT; |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 225 | goto outahere; |
| 226 | } |
| 227 | |
| 228 | printf ("ok.\n"); |
| 229 | } else { /* it was protected */ |
| 230 | |
| 231 | printf ("protected!\n"); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (ctrlc ()) |
| 236 | printf ("User Interrupt!\n"); |
| 237 | |
| 238 | outahere: |
| 239 | /* allow flash to settle - wait 10 ms */ |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 240 | mdelay(10); |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 241 | |
| 242 | if (iflag) |
Simon Glass | f87959b | 2019-11-14 12:57:40 -0700 | [diff] [blame] | 243 | enable_interrupts(); |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 244 | |
| 245 | if (cflag) |
Simon Glass | fbf091b | 2019-11-14 12:57:36 -0700 | [diff] [blame] | 246 | icache_enable(); |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 247 | |
| 248 | return rc; |
| 249 | } |
| 250 | |
Simon Glass | c744316 | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 251 | static int write_word(flash_info_t *info, ulong dest, ulong data) |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 252 | { |
| 253 | volatile u16 *addr = (volatile u16 *) dest; |
| 254 | ulong result; |
| 255 | int rc = ERR_OK; |
| 256 | int cflag, iflag; |
| 257 | int chip1; |
Graeme Russ | 43f0255 | 2011-06-28 01:40:55 +0000 | [diff] [blame] | 258 | ulong start; |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 259 | |
| 260 | /* |
| 261 | * Check if Flash is (sufficiently) erased |
| 262 | */ |
| 263 | result = *addr; |
| 264 | if ((result & data) != data) |
| 265 | return ERR_NOT_ERASED; |
| 266 | |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 267 | /* |
| 268 | * Disable interrupts which might cause a timeout |
| 269 | * here. Remember that our exception vectors are |
| 270 | * at address 0 in the flash, and we don't want a |
| 271 | * (ticker) exception to happen while the flash |
| 272 | * chip is in programming mode. |
| 273 | */ |
| 274 | |
Simon Glass | fbf091b | 2019-11-14 12:57:36 -0700 | [diff] [blame] | 275 | cflag = icache_status(); |
| 276 | icache_disable(); |
Simon Glass | f87959b | 2019-11-14 12:57:40 -0700 | [diff] [blame] | 277 | iflag = disable_interrupts(); |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 278 | |
| 279 | MEM_FLASH_ADDR1 = CMD_UNLOCK1; |
| 280 | MEM_FLASH_ADDR2 = CMD_UNLOCK2; |
| 281 | MEM_FLASH_ADDR1 = CMD_PROGRAM; |
| 282 | *addr = data; |
| 283 | |
| 284 | /* arm simple, non interrupt dependent timer */ |
Graeme Russ | 43f0255 | 2011-06-28 01:40:55 +0000 | [diff] [blame] | 285 | start = get_timer(0); |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 286 | |
| 287 | /* wait until flash is ready */ |
| 288 | chip1 = 0; |
| 289 | do { |
| 290 | result = *addr; |
| 291 | |
Tom Rini | 78f8800 | 2022-07-23 13:05:00 -0400 | [diff] [blame] | 292 | /* check timeout, 1000ms */ |
| 293 | if (get_timer(start) > 1000) { |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 294 | chip1 = ERR | TMO; |
| 295 | break; |
| 296 | } |
| 297 | if (!chip1 && ((result & 0x80) == (data & 0x80))) |
| 298 | chip1 = READY; |
| 299 | |
| 300 | } while (!chip1); |
| 301 | |
| 302 | *addr = CMD_READ_ARRAY; |
| 303 | |
| 304 | if (chip1 == ERR || *addr != data) |
| 305 | rc = ERR_PROG_ERROR; |
| 306 | |
| 307 | if (iflag) |
Simon Glass | f87959b | 2019-11-14 12:57:40 -0700 | [diff] [blame] | 308 | enable_interrupts(); |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 309 | |
| 310 | if (cflag) |
Simon Glass | fbf091b | 2019-11-14 12:57:36 -0700 | [diff] [blame] | 311 | icache_enable(); |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 312 | |
| 313 | return rc; |
| 314 | } |
| 315 | |
Simon Glass | c744316 | 2020-05-10 11:39:53 -0600 | [diff] [blame] | 316 | int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
wdenk | c4cbd34 | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 317 | { |
| 318 | ulong wp, data; |
| 319 | int rc; |
| 320 | |
| 321 | if (addr & 1) { |
| 322 | printf ("unaligned destination not supported\n"); |
| 323 | return ERR_ALIGN; |
| 324 | } |
| 325 | |
| 326 | #if 0 |
| 327 | if (cnt & 1) { |
| 328 | printf ("odd transfer sizes not supported\n"); |
| 329 | return ERR_ALIGN; |
| 330 | } |
| 331 | #endif |
| 332 | |
| 333 | wp = addr; |
| 334 | |
| 335 | if (addr & 1) { |
| 336 | data = (*((volatile u8 *) addr) << 8) | *((volatile u8 *) |
| 337 | src); |
| 338 | if ((rc = write_word (info, wp - 1, data)) != 0) { |
| 339 | return (rc); |
| 340 | } |
| 341 | src += 1; |
| 342 | wp += 1; |
| 343 | cnt -= 1; |
| 344 | } |
| 345 | |
| 346 | while (cnt >= 2) { |
| 347 | data = *((volatile u16 *) src); |
| 348 | if ((rc = write_word (info, wp, data)) != 0) { |
| 349 | return (rc); |
| 350 | } |
| 351 | src += 2; |
| 352 | wp += 2; |
| 353 | cnt -= 2; |
| 354 | } |
| 355 | |
| 356 | if (cnt == 1) { |
| 357 | data = (*((volatile u8 *) src) << 8) | |
| 358 | *((volatile u8 *) (wp + 1)); |
| 359 | if ((rc = write_word (info, wp, data)) != 0) { |
| 360 | return (rc); |
| 361 | } |
| 362 | src += 1; |
| 363 | wp += 1; |
| 364 | cnt -= 1; |
| 365 | } |
| 366 | |
| 367 | return ERR_OK; |
| 368 | } |