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