wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * MOUSSE/MPC8240 Board definitions. |
| 3 | * Flash Routines for MOUSSE onboard AMD29LV106DB devices |
| 4 | * |
| 5 | * (C) Copyright 2000 |
| 6 | * Marius Groeger <mgroeger@sysgo.de> |
| 7 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 8 | * |
| 9 | * (C) Copyright 2000 |
| 10 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 11 | * |
| 12 | * (C) Copyright 1999, by Curt McDowell, 08-06-99, Broadcom Corp. |
| 13 | * (C) Copyright 2001, James Dougherty, 07/18/01, Broadcom Corp. |
| 14 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 15 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <common.h> |
| 19 | #include <mpc8xx.h> |
| 20 | #include <malloc.h> |
| 21 | #include "mousse.h" |
| 22 | #include "flash.h" |
| 23 | |
| 24 | int flashLibDebug = 0; |
| 25 | int flashLibInited = 0; |
| 26 | |
| 27 | #define OK 0 |
| 28 | #define ERROR -1 |
| 29 | #define STATUS int |
| 30 | #define PRINTF if (flashLibDebug) printf |
| 31 | #if 0 |
| 32 | #define PRIVATE static |
| 33 | #else |
| 34 | #define PRIVATE |
| 35 | #endif |
| 36 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 37 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 38 | |
| 39 | #define SLEEP_DELAY 166 |
| 40 | #define FLASH_SECTOR_SIZE (64*1024) |
| 41 | /*********************************************************************** |
| 42 | * |
| 43 | * Virtual Flash Devices on Mousse board |
| 44 | * |
| 45 | * These must be kept in sync with the definitions in flashLib.h. |
| 46 | * |
| 47 | ***********************************************************************/ |
| 48 | |
| 49 | PRIVATE flash_dev_t flashDev[] = { |
| 50 | /* Bank 0 sector SA0 (16 kB) */ |
| 51 | { "SA0",FLASH0_BANK, FLASH0_SEG0_START, 1, 14, |
| 52 | FLASH0_VENDOR_ID, FLASH0_DEVICE_ID |
| 53 | }, |
| 54 | /* Bank 0 sector SA1 (8 kB) */ |
| 55 | { "SA1", FLASH0_BANK, FLASH0_SEG0_START + 0x4000, 1, 13, |
| 56 | FLASH0_VENDOR_ID, FLASH0_DEVICE_ID |
| 57 | }, |
| 58 | /* Bank 0 sector SA2 (8 kB) */ |
| 59 | { "SA2", FLASH0_BANK, FLASH0_SEG0_START + 0x6000, 1, 13, |
| 60 | FLASH0_VENDOR_ID, FLASH0_DEVICE_ID |
| 61 | }, |
| 62 | /* Bank 0 sector SA3 is occluded by Mousse I/O devices */ |
| 63 | /* Bank 0 sectors SA4-SA18, after Mousse devices up to PLCC (960 kB) */ |
| 64 | { "KERNEL", FLASH0_BANK, FLASH0_SEG1_START, 15, 16, |
| 65 | FLASH0_VENDOR_ID, FLASH0_DEVICE_ID |
| 66 | }, |
| 67 | /* Bank 0 sectors SA19-SA26, jumper can occlude this by PLCC (512 kB) */ |
| 68 | /* This is where the Kahlua boot vector and boot ROM code resides. */ |
| 69 | { "BOOT",FLASH0_BANK, FLASH0_SEG2_START, 8, 16, |
| 70 | FLASH0_VENDOR_ID, FLASH0_DEVICE_ID |
| 71 | }, |
| 72 | /* Bank 0 sectors SA27-SA34 (512 kB) */ |
| 73 | { "RAMDISK",FLASH0_BANK, FLASH0_SEG3_START, 8, 16, |
| 74 | FLASH0_VENDOR_ID, FLASH0_DEVICE_ID |
| 75 | }, |
| 76 | }; |
| 77 | |
| 78 | int flashDevCount = (sizeof (flashDev) / sizeof (flashDev[0])); |
| 79 | |
| 80 | #define DEV(no) (&flashDev[no]) |
| 81 | #define DEV_NO(dev) ((dev) - flashDev) |
| 82 | |
| 83 | /*********************************************************************** |
| 84 | * |
| 85 | * Private Flash Routines |
| 86 | * |
| 87 | ***********************************************************************/ |
| 88 | |
| 89 | /* |
| 90 | * The convention is: |
| 91 | * |
| 92 | * "addr" is always the PROM raw address, which is the address of an |
| 93 | * 8-bit quantity for flash 0 and 16-bit quantity for flash 1. |
| 94 | * |
| 95 | * "pos" is always a logical byte position from the PROM beginning. |
| 96 | */ |
| 97 | |
| 98 | #define FLASH0_ADDR(dev, addr) \ |
| 99 | ((unsigned char *) ((dev)->base + (addr))) |
| 100 | |
| 101 | #define FLASH0_WRITE(dev, addr, value) \ |
| 102 | (*FLASH0_ADDR(dev, addr) = (value)) |
| 103 | |
| 104 | #define FLASH0_READ(dev, addr) \ |
| 105 | (*FLASH0_ADDR(dev, addr)) |
| 106 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 107 | PRIVATE int flashCheck (flash_dev_t * dev) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 108 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 109 | if (!flashLibInited) { |
| 110 | printf ("flashCheck: flashLib not initialized\n"); |
| 111 | return ERROR; |
| 112 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 113 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 114 | if (dev < &flashDev[0] || dev >= &flashDev[flashDevCount]) { |
| 115 | printf ("flashCheck: Bad dev parameter\n"); |
| 116 | return ERROR; |
| 117 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 118 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 119 | if (!dev->found) { |
| 120 | printf ("flashCheck: Device %d not available\n", DEV_NO (dev)); |
| 121 | return ERROR; |
| 122 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 123 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 124 | return OK; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 125 | } |
| 126 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 127 | PRIVATE void flashReset (flash_dev_t * dev) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 128 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 129 | PRINTF ("flashReset: dev=%d\n", DEV_NO (dev)); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 130 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 131 | if (dev->bank == FLASH0_BANK) { |
| 132 | FLASH0_WRITE (dev, 0x555, 0xaa); |
| 133 | FLASH0_WRITE (dev, 0xaaa, 0x55); |
| 134 | FLASH0_WRITE (dev, 0x555, 0xf0); |
| 135 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 136 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 137 | udelay (SLEEP_DELAY); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 138 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 139 | PRINTF ("flashReset: done\n"); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 140 | } |
| 141 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 142 | PRIVATE int flashProbe (flash_dev_t * dev) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 143 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 144 | int rv, deviceID, vendorID; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 145 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 146 | PRINTF ("flashProbe: dev=%d\n", DEV_NO (dev)); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 147 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 148 | if (dev->bank != FLASH0_BANK) { |
| 149 | rv = ERROR; |
| 150 | goto DONE; |
| 151 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 152 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 153 | FLASH0_WRITE (dev, 0xaaa, 0xaa); |
| 154 | FLASH0_WRITE (dev, 0x555, 0x55); |
| 155 | FLASH0_WRITE (dev, 0xaaa, 0x90); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 156 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 157 | udelay (SLEEP_DELAY); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 158 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 159 | vendorID = FLASH0_READ (dev, 0); |
| 160 | deviceID = FLASH0_READ (dev, 2); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 161 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 162 | FLASH0_WRITE (dev, 0, 0xf0); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 163 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 164 | PRINTF ("flashProbe: vendor=0x%x device=0x%x\n", vendorID, deviceID); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 165 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 166 | if (vendorID == dev->vendorID && deviceID == dev->deviceID) |
| 167 | rv = OK; |
| 168 | else |
| 169 | rv = ERROR; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 170 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 171 | DONE: |
| 172 | PRINTF ("flashProbe: rv=%d\n", rv); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 173 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 174 | return rv; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 175 | } |
| 176 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 177 | PRIVATE int flashWait (flash_dev_t * dev, int addr, int expect, int erase) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 178 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 179 | int rv = ERROR; |
| 180 | int i, data; |
| 181 | int polls; |
| 182 | |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 183 | #if 0 |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 184 | PRINTF ("flashWait: dev=%d addr=0x%x expect=0x%x erase=%d\n", |
| 185 | DEV_NO (dev), addr, expect, erase); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 186 | #endif |
| 187 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 188 | if (dev->bank != FLASH0_BANK) { |
| 189 | rv = ERROR; |
| 190 | goto done; |
| 191 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 192 | |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 193 | if (erase) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 194 | polls = FLASH_ERASE_SECTOR_TIMEOUT; /* Ticks */ |
| 195 | else |
| 196 | polls = FLASH_PROGRAM_POLLS; /* Loops */ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 197 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 198 | for (i = 0; i < polls; i++) { |
| 199 | if (erase) |
| 200 | udelay (SLEEP_DELAY); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 201 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 202 | data = FLASH0_READ (dev, addr); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 203 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 204 | if (((data ^ expect) & 0x80) == 0) { |
| 205 | rv = OK; |
| 206 | goto done; |
| 207 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 208 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 209 | if (data & 0x20) { |
| 210 | /* |
| 211 | * If the 0x20 bit has come on, it could actually be because |
| 212 | * the operation succeeded, so check the done bit again. |
| 213 | */ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 214 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 215 | data = FLASH0_READ (dev, addr); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 216 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 217 | if (((data ^ expect) & 0x80) == 0) { |
| 218 | rv = OK; |
| 219 | goto done; |
| 220 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 221 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 222 | printf ("flashWait: Program error (dev: %d, addr: 0x%x)\n", |
| 223 | DEV_NO (dev), addr); |
| 224 | |
| 225 | flashReset (dev); |
| 226 | rv = ERROR; |
| 227 | goto done; |
| 228 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 229 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 230 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 231 | printf ("flashWait: Timeout %s (dev: %d, addr: 0x%x)\n", |
| 232 | erase ? "erasing sector" : "programming byte", |
| 233 | DEV_NO (dev), addr); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 234 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 235 | done: |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 236 | |
| 237 | #if 0 |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 238 | PRINTF ("flashWait: rv=%d\n", rv); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 239 | #endif |
| 240 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 241 | return rv; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /*********************************************************************** |
| 245 | * |
| 246 | * Public Flash Routines |
| 247 | * |
| 248 | ***********************************************************************/ |
| 249 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 250 | STATUS flashLibInit (void) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 251 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 252 | int i; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 253 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 254 | PRINTF ("flashLibInit: devices=%d\n", flashDevCount); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 255 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 256 | for (i = 0; i < flashDevCount; i++) { |
| 257 | flash_dev_t *dev = &flashDev[i]; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 258 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 259 | /* |
| 260 | * For bank 1, probe both without and with byte swappage, |
| 261 | * so that this module works on both old and new Mousse boards. |
| 262 | */ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 263 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 264 | flashReset (dev); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 265 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 266 | if (flashProbe (dev) != ERROR) |
| 267 | dev->found = 1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 268 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 269 | flashReset (dev); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 270 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 271 | if (flashProbe (dev) != ERROR) |
| 272 | dev->found = 1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 273 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 274 | dev->swap = 0; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 275 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 276 | if (dev->found) { |
| 277 | PRINTF ("\n FLASH %s[%d]: iobase=0x%x - %d sectors %d KB", |
| 278 | flashDev[i].name, i, flashDev[i].base, |
| 279 | flashDev[i].sectors, |
| 280 | (flashDev[i].sectors * FLASH_SECTOR_SIZE) / 1024); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 281 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | flashLibInited = 1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 286 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 287 | PRINTF ("flashLibInit: done\n"); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 288 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 289 | return OK; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 290 | } |
| 291 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 292 | STATUS flashEraseSector (flash_dev_t * dev, int sector) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 293 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 294 | int pos, addr; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 295 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 296 | PRINTF ("flashErasesector: dev=%d sector=%d\n", DEV_NO (dev), sector); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 297 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 298 | if (flashCheck (dev) == ERROR) |
| 299 | return ERROR; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 300 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 301 | if (sector < 0 || sector >= dev->sectors) { |
| 302 | printf ("flashEraseSector: Sector out of range (dev: %d, sector: %d)\n", DEV_NO (dev), sector); |
| 303 | return ERROR; |
| 304 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 305 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 306 | pos = FLASH_SECTOR_POS (dev, sector); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 307 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 308 | if (dev->bank != FLASH0_BANK) { |
| 309 | return ERROR; |
| 310 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 311 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 312 | addr = pos; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 313 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 314 | FLASH0_WRITE (dev, 0xaaa, 0xaa); |
| 315 | FLASH0_WRITE (dev, 0x555, 0x55); |
| 316 | FLASH0_WRITE (dev, 0xaaa, 0x80); |
| 317 | FLASH0_WRITE (dev, 0xaaa, 0xaa); |
| 318 | FLASH0_WRITE (dev, 0x555, 0x55); |
| 319 | FLASH0_WRITE (dev, addr, 0x30); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 320 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 321 | return flashWait (dev, addr, 0xff, 1); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Note: it takes about as long to flash all sectors together with Chip |
| 326 | * Erase as it does to flash them one at a time (about 30 seconds for 2 |
| 327 | * MB). Also since we want to be able to treat subsets of sectors as if |
| 328 | * they were complete devices, we don't use Chip Erase. |
| 329 | */ |
| 330 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 331 | STATUS flashErase (flash_dev_t * dev) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 332 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 333 | int sector; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 334 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 335 | PRINTF ("flashErase: dev=%d sectors=%d\n", DEV_NO (dev), dev->sectors); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 336 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 337 | if (flashCheck (dev) == ERROR) |
| 338 | return ERROR; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 339 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 340 | for (sector = 0; sector < dev->sectors; sector++) { |
| 341 | if (flashEraseSector (dev, sector) == ERROR) |
| 342 | return ERROR; |
| 343 | } |
| 344 | return OK; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Read and write bytes |
| 349 | */ |
| 350 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 351 | STATUS flashRead (flash_dev_t * dev, int pos, char *buf, int len) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 352 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 353 | int addr, words; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 354 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 355 | PRINTF ("flashRead: dev=%d pos=0x%x buf=0x%x len=0x%x\n", |
| 356 | DEV_NO (dev), pos, (int) buf, len); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 357 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 358 | if (flashCheck (dev) == ERROR) |
| 359 | return ERROR; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 360 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 361 | if (pos < 0 || len < 0 || pos + len > FLASH_MAX_POS (dev)) { |
| 362 | printf ("flashRead: Position out of range " |
| 363 | "(dev: %d, pos: 0x%x, len: 0x%x)\n", |
| 364 | DEV_NO (dev), pos, len); |
| 365 | return ERROR; |
| 366 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 367 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 368 | if (len == 0) |
| 369 | return OK; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 370 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 371 | if (dev->bank == FLASH0_BANK) { |
| 372 | addr = pos; |
| 373 | words = len; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 374 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 375 | PRINTF ("flashRead: memcpy(0x%x, 0x%x, 0x%x)\n", |
| 376 | (int) buf, (int) FLASH0_ADDR (dev, pos), len); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 377 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 378 | memcpy (buf, FLASH0_ADDR (dev, addr), words); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 379 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 380 | } |
| 381 | PRINTF ("flashRead: rv=OK\n"); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 382 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 383 | return OK; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 384 | } |
| 385 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 386 | STATUS flashWrite (flash_dev_t * dev, int pos, char *buf, int len) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 387 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 388 | int addr, words; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 389 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 390 | PRINTF ("flashWrite: dev=%d pos=0x%x buf=0x%x len=0x%x\n", |
| 391 | DEV_NO (dev), pos, (int) buf, len); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 392 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 393 | if (flashCheck (dev) == ERROR) |
| 394 | return ERROR; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 395 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 396 | if (pos < 0 || len < 0 || pos + len > FLASH_MAX_POS (dev)) { |
| 397 | printf ("flashWrite: Position out of range " |
| 398 | "(dev: %d, pos: 0x%x, len: 0x%x)\n", |
| 399 | DEV_NO (dev), pos, len); |
| 400 | return ERROR; |
| 401 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 402 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 403 | if (len == 0) |
| 404 | return OK; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 405 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 406 | if (dev->bank == FLASH0_BANK) { |
| 407 | unsigned char tmp; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 408 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 409 | addr = pos; |
| 410 | words = len; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 411 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 412 | while (words--) { |
| 413 | tmp = *buf; |
| 414 | if (~FLASH0_READ (dev, addr) & tmp) { |
| 415 | printf ("flashWrite: Attempt to program 0 to 1 " |
| 416 | "(dev: %d, addr: 0x%x, data: 0x%x)\n", |
| 417 | DEV_NO (dev), addr, tmp); |
| 418 | return ERROR; |
| 419 | } |
| 420 | FLASH0_WRITE (dev, 0xaaa, 0xaa); |
| 421 | FLASH0_WRITE (dev, 0x555, 0x55); |
| 422 | FLASH0_WRITE (dev, 0xaaa, 0xa0); |
| 423 | FLASH0_WRITE (dev, addr, tmp); |
| 424 | if (flashWait (dev, addr, tmp, 0) < 0) |
| 425 | return ERROR; |
| 426 | buf++; |
| 427 | addr++; |
| 428 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 429 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 430 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 431 | PRINTF ("flashWrite: rv=OK\n"); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 432 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 433 | return OK; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 437 | * flashWritable returns true if a range contains all F's. |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 438 | */ |
| 439 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 440 | STATUS flashWritable (flash_dev_t * dev, int pos, int len) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 441 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 442 | int addr, words; |
| 443 | int rv = ERROR; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 444 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 445 | PRINTF ("flashWritable: dev=%d pos=0x%x len=0x%x\n", |
| 446 | DEV_NO (dev), pos, len); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 447 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 448 | if (flashCheck (dev) == ERROR) |
| 449 | goto done; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 450 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 451 | if (pos < 0 || len < 0 || pos + len > FLASH_MAX_POS (dev)) { |
| 452 | printf ("flashWritable: Position out of range " |
| 453 | "(dev: %d, pos: 0x%x, len: 0x%x)\n", |
| 454 | DEV_NO (dev), pos, len); |
| 455 | goto done; |
| 456 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 457 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 458 | if (len == 0) { |
| 459 | rv = 1; |
| 460 | goto done; |
| 461 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 462 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 463 | if (dev->bank == FLASH0_BANK) { |
| 464 | addr = pos; |
| 465 | words = len; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 466 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 467 | while (words--) { |
| 468 | if (FLASH0_READ (dev, addr) != 0xff) { |
| 469 | rv = 0; |
| 470 | goto done; |
| 471 | } |
| 472 | addr++; |
| 473 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 474 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 475 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 476 | rv = 1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 477 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 478 | done: |
| 479 | PRINTF ("flashWrite: rv=%d\n", rv); |
| 480 | return rv; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | |
| 484 | /* |
| 485 | * NOTE: the below code cannot run from FLASH!!! |
| 486 | */ |
| 487 | /*********************************************************************** |
| 488 | * |
| 489 | * Flash Diagnostics |
| 490 | * |
| 491 | ***********************************************************************/ |
| 492 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 493 | STATUS flashDiag (flash_dev_t * dev) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 494 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 495 | unsigned int *buf = 0; |
| 496 | int i, len, sector; |
| 497 | int rv = ERROR; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 498 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 499 | if (flashCheck (dev) == ERROR) |
| 500 | return ERROR; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 501 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 502 | printf ("flashDiag: Testing device %d, " |
| 503 | "base: 0x%x, %d sectors @ %d kB = %d kB\n", |
| 504 | DEV_NO (dev), dev->base, |
| 505 | dev->sectors, |
| 506 | 1 << (dev->lgSectorSize - 10), |
| 507 | dev->sectors << (dev->lgSectorSize - 10)); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 508 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 509 | len = 1 << dev->lgSectorSize; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 510 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 511 | printf ("flashDiag: Erasing\n"); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 512 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 513 | if (flashErase (dev) == ERROR) { |
| 514 | printf ("flashDiag: Erase failed\n"); |
| 515 | goto done; |
| 516 | } |
| 517 | printf ("%d bytes requested ...\n", len); |
| 518 | buf = malloc (len); |
| 519 | printf ("allocated %d bytes ...\n", len); |
| 520 | if (buf == 0) { |
| 521 | printf ("flashDiag: Out of memory\n"); |
| 522 | goto done; |
| 523 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 524 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 525 | /* |
| 526 | * Write unique counting pattern to each sector |
| 527 | */ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 528 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 529 | for (sector = 0; sector < dev->sectors; sector++) { |
| 530 | printf ("flashDiag: Write sector %d\n", sector); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 531 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 532 | for (i = 0; i < len / 4; i++) |
| 533 | buf[i] = sector << 24 | i; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 534 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 535 | if (flashWrite (dev, |
| 536 | sector << dev->lgSectorSize, |
| 537 | (char *) buf, len) == ERROR) { |
| 538 | printf ("flashDiag: Write failed (dev: %d, sector: %d)\n", |
| 539 | DEV_NO (dev), sector); |
| 540 | goto done; |
| 541 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 542 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 543 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 544 | /* |
| 545 | * Verify |
| 546 | */ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 547 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 548 | for (sector = 0; sector < dev->sectors; sector++) { |
| 549 | printf ("flashDiag: Verify sector %d\n", sector); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 550 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 551 | if (flashRead (dev, |
| 552 | sector << dev->lgSectorSize, |
| 553 | (char *) buf, len) == ERROR) { |
| 554 | printf ("flashDiag: Read failed (dev: %d, sector: %d)\n", |
| 555 | DEV_NO (dev), sector); |
| 556 | goto done; |
| 557 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 558 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 559 | for (i = 0; i < len / 4; i++) { |
| 560 | if (buf[i] != (sector << 24 | i)) { |
| 561 | printf ("flashDiag: Verify error " |
| 562 | "(dev: %d, sector: %d, offset: 0x%x)\n", |
| 563 | DEV_NO (dev), sector, i); |
| 564 | printf ("flashDiag: Expected 0x%08x, got 0x%08x\n", |
| 565 | sector << 24 | i, buf[i]); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 566 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 567 | goto done; |
| 568 | } |
| 569 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 570 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 571 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 572 | printf ("flashDiag: Erasing\n"); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 573 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 574 | if (flashErase (dev) == ERROR) { |
| 575 | printf ("flashDiag: Final erase failed\n"); |
| 576 | goto done; |
| 577 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 578 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 579 | rv = OK; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 580 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 581 | done: |
| 582 | if (buf) |
| 583 | free (buf); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 584 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 585 | if (rv == OK) |
| 586 | printf ("flashDiag: Device %d passed\n", DEV_NO (dev)); |
| 587 | else |
| 588 | printf ("flashDiag: Device %d failed\n", DEV_NO (dev)); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 589 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 590 | return rv; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 591 | } |
| 592 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 593 | STATUS flashDiagAll (void) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 594 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 595 | int i; |
| 596 | int rv = OK; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 597 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 598 | PRINTF ("flashDiagAll: devices=%d\n", flashDevCount); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 599 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 600 | for (i = 0; i < flashDevCount; i++) { |
| 601 | flash_dev_t *dev = &flashDev[i]; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 602 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 603 | if (dev->found && flashDiag (dev) == ERROR) |
| 604 | rv = ERROR; |
| 605 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 606 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 607 | if (rv == OK) |
| 608 | printf ("flashDiagAll: Passed\n"); |
| 609 | else |
| 610 | printf ("flashDiagAll: Failed because of earlier errors\n"); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 611 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 612 | return OK; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | |
| 616 | /*----------------------------------------------------------------------- |
| 617 | */ |
| 618 | unsigned long flash_init (void) |
| 619 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 620 | unsigned long size = 0; |
| 621 | flash_dev_t *dev = NULL; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 622 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 623 | flashLibInit (); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 624 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 625 | /* |
| 626 | * Provide info for FLASH (up to 960K) of Kernel Image data. |
| 627 | */ |
| 628 | dev = FLASH_DEV_BANK0_LOW; |
| 629 | flash_info[FLASH_BANK_KERNEL].flash_id = |
| 630 | (dev->vendorID << 16) | dev->deviceID; |
| 631 | flash_info[FLASH_BANK_KERNEL].sector_count = dev->sectors; |
| 632 | flash_info[FLASH_BANK_KERNEL].size = |
| 633 | flash_info[FLASH_BANK_KERNEL].sector_count * FLASH_SECTOR_SIZE; |
| 634 | flash_info[FLASH_BANK_KERNEL].start[FIRST_SECTOR] = dev->base; |
| 635 | size += flash_info[FLASH_BANK_KERNEL].size; |
| 636 | |
| 637 | /* |
| 638 | * Provide info for 512K PLCC FLASH ROM (U-Boot) |
| 639 | */ |
| 640 | dev = FLASH_DEV_BANK0_BOOT; |
| 641 | flash_info[FLASH_BANK_BOOT].flash_id = |
| 642 | (dev->vendorID << 16) | dev->deviceID; |
| 643 | flash_info[FLASH_BANK_BOOT].sector_count = dev->sectors; |
| 644 | flash_info[FLASH_BANK_BOOT].size = |
| 645 | flash_info[FLASH_BANK_BOOT].sector_count * FLASH_SECTOR_SIZE; |
| 646 | flash_info[FLASH_BANK_BOOT].start[FIRST_SECTOR] = dev->base; |
| 647 | size += flash_info[FLASH_BANK_BOOT].size; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 648 | |
| 649 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 650 | /* |
| 651 | * Provide info for 512K FLASH0 segment (U-Boot) |
| 652 | */ |
| 653 | dev = FLASH_DEV_BANK0_HIGH; |
| 654 | flash_info[FLASH_BANK_AUX].flash_id = |
| 655 | (dev->vendorID << 16) | dev->deviceID; |
| 656 | flash_info[FLASH_BANK_AUX].sector_count = dev->sectors; |
| 657 | flash_info[FLASH_BANK_AUX].size = |
| 658 | flash_info[FLASH_BANK_AUX].sector_count * FLASH_SECTOR_SIZE; |
| 659 | flash_info[FLASH_BANK_AUX].start[FIRST_SECTOR] = dev->base; |
| 660 | size += flash_info[FLASH_BANK_AUX].size; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 661 | |
| 662 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 663 | return size; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Get flash device from U-Boot flash info. |
| 668 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 669 | flash_dev_t *getFlashDevFromInfo (flash_info_t * info) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 670 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 671 | int i; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 672 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 673 | if (!info) |
| 674 | return NULL; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 675 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 676 | for (i = 0; i < flashDevCount; i++) { |
| 677 | flash_dev_t *dev = &flashDev[i]; |
| 678 | |
| 679 | if (dev->found && (dev->base == info->start[0])) |
| 680 | return dev; |
| 681 | } |
| 682 | printf ("ERROR: notice, no FLASH mapped at address 0x%x\n", |
| 683 | (unsigned int) info->start[0]); |
| 684 | return NULL; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 685 | } |
| 686 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 687 | ulong flash_get_size (vu_long * addr, flash_info_t * info) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 688 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 689 | int i; |
| 690 | |
| 691 | for (i = 0; i < flashDevCount; i++) { |
| 692 | flash_dev_t *dev = &flashDev[i]; |
| 693 | |
| 694 | if (dev->found) { |
| 695 | if (dev->base == (unsigned int) addr) { |
| 696 | info->flash_id = (dev->vendorID << 16) | dev->deviceID; |
| 697 | info->sector_count = dev->sectors; |
| 698 | info->size = info->sector_count * FLASH_SECTOR_SIZE; |
| 699 | return dev->sectors * FLASH_SECTOR_SIZE; |
| 700 | } |
| 701 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 702 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 703 | return 0; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 704 | } |
| 705 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 706 | void flash_print_info (flash_info_t * info) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 707 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 708 | int i; |
| 709 | unsigned int chip; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 710 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 711 | if (info->flash_id == FLASH_UNKNOWN) { |
| 712 | printf ("missing or unknown FLASH type\n"); |
| 713 | return; |
| 714 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 715 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 716 | switch ((info->flash_id >> 16) & 0xff) { |
| 717 | case 0x1: |
| 718 | printf ("AMD "); |
| 719 | break; |
| 720 | default: |
| 721 | printf ("Unknown Vendor "); |
| 722 | break; |
| 723 | } |
| 724 | chip = (unsigned int) info->flash_id & 0x000000ff; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 725 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 726 | switch (chip) { |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 727 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 728 | case AMD_ID_F040B: |
| 729 | printf ("AM29F040B (4 Mbit)\n"); |
| 730 | break; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 731 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 732 | case AMD_ID_LV160B: |
| 733 | case FLASH_AM160LV: |
| 734 | case 0x49: |
| 735 | printf ("AM29LV160B (16 Mbit / 2M x 8bit)\n"); |
| 736 | break; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 737 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 738 | default: |
| 739 | printf ("Unknown Chip Type:0x%x\n", chip); |
| 740 | break; |
| 741 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 742 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 743 | printf (" Size: %ld bytes in %d Sectors\n", |
| 744 | info->size, info->sector_count); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 745 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 746 | printf (" Sector Start Addresses:"); |
| 747 | for (i = 0; i < info->sector_count; ++i) { |
| 748 | if ((i % 5) == 0) |
| 749 | printf ("\n "); |
| 750 | printf (" %08lX%s", |
| 751 | info->start[FIRST_SECTOR] + i * FLASH_SECTOR_SIZE, |
| 752 | info->protect[i] ? " (RO)" : " "); |
| 753 | } |
| 754 | printf ("\n"); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | |
| 758 | /* |
| 759 | * Erase a range of flash sectors. |
| 760 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 761 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 762 | { |
Wolfgang Denk | e3e3e97a6 | 2011-11-05 05:13:00 +0000 | [diff] [blame] | 763 | int prot, sect; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 764 | flash_dev_t *dev = NULL; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 765 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 766 | if ((s_first < 0) || (s_first > s_last)) { |
| 767 | if (info->flash_id == FLASH_UNKNOWN) { |
| 768 | printf ("- missing\n"); |
| 769 | } else { |
| 770 | printf ("- no sectors to erase\n"); |
| 771 | } |
| 772 | return 1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 773 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 774 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 775 | prot = 0; |
| 776 | for (sect = s_first; sect <= s_last; sect++) { |
| 777 | if (info->protect[sect]) { |
| 778 | prot++; |
| 779 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 780 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 781 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 782 | if (prot) { |
| 783 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 784 | prot); |
| 785 | } else { |
| 786 | printf ("\n"); |
| 787 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 788 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 789 | /* Start erase on unprotected sectors */ |
| 790 | dev = getFlashDevFromInfo (info); |
| 791 | if (dev) { |
| 792 | printf ("Erase FLASH[%s] -%d sectors:", dev->name, dev->sectors); |
| 793 | for (sect = s_first; sect <= s_last; sect++) { |
| 794 | if (info->protect[sect] == 0) { /* not protected */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 795 | printf ("."); |
| 796 | if (ERROR == flashEraseSector (dev, sect)) { |
| 797 | printf ("ERROR: could not erase sector %d on FLASH[%s]\n", sect, dev->name); |
| 798 | return 1; |
| 799 | } |
| 800 | } |
| 801 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 802 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 803 | printf (" done\n"); |
| 804 | return 0; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /*----------------------------------------------------------------------- |
| 808 | * Write a word to Flash, returns: |
| 809 | * 0 - OK |
| 810 | * 1 - write timeout |
| 811 | * 2 - Flash not erased |
| 812 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 813 | static int write_word (flash_info_t * info, ulong dest, ulong data) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 814 | { |
| 815 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 816 | flash_dev_t *dev = getFlashDevFromInfo (info); |
| 817 | int addr = dest - info->start[0]; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 818 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 819 | if (!dev) |
| 820 | return 1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 821 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 822 | if (OK != flashWrite (dev, addr, (char *) &data, sizeof (ulong))) { |
| 823 | printf ("ERROR: could not write to addr=0x%x, data=0x%x\n", |
| 824 | (unsigned int) addr, (unsigned) data); |
| 825 | return 1; |
| 826 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 827 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 828 | if ((addr % FLASH_SECTOR_SIZE) == 0) |
| 829 | printf ("."); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 830 | |
| 831 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 832 | PRINTF ("write_word:0x%x, base=0x%x, addr=0x%x, data=0x%x\n", |
| 833 | (unsigned) info->start[0], |
| 834 | (unsigned) dest, |
| 835 | (unsigned) (dest - info->start[0]), (unsigned) data); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 836 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 837 | return (0); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | |
| 841 | /*----------------------------------------------------------------------- |
| 842 | * Copy memory to flash, returns: |
| 843 | * 0 - OK |
| 844 | * 1 - write timeout |
| 845 | * 2 - Flash not erased |
| 846 | */ |
| 847 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 848 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 849 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 850 | ulong cp, wp, data; |
| 851 | int i, l, rc; |
| 852 | flash_dev_t *dev = getFlashDevFromInfo (info); |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 853 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 854 | if (dev) { |
| 855 | printf ("FLASH[%s]:", dev->name); |
| 856 | wp = (addr & ~3); /* get lower word aligned address */ |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 857 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 858 | /* |
| 859 | * handle unaligned start bytes |
| 860 | */ |
| 861 | if ((l = addr - wp) != 0) { |
| 862 | data = 0; |
| 863 | for (i = 0, cp = wp; i < l; ++i, ++cp) { |
| 864 | data = (data << 8) | (*(uchar *) cp); |
| 865 | } |
| 866 | for (; i < 4 && cnt > 0; ++i) { |
| 867 | data = (data << 8) | *src++; |
| 868 | --cnt; |
| 869 | ++cp; |
| 870 | } |
| 871 | for (; cnt == 0 && i < 4; ++i, ++cp) { |
| 872 | data = (data << 8) | (*(uchar *) cp); |
| 873 | } |
| 874 | if ((rc = write_word (info, wp, data)) != 0) { |
| 875 | return (rc); |
| 876 | } |
| 877 | wp += 4; |
| 878 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 879 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 880 | /* |
| 881 | * handle word aligned part |
| 882 | */ |
| 883 | while (cnt >= 4) { |
| 884 | data = 0; |
| 885 | for (i = 0; i < 4; ++i) { |
| 886 | data = (data << 8) | *src++; |
| 887 | } |
| 888 | if ((rc = write_word (info, wp, data)) != 0) { |
| 889 | return (rc); |
| 890 | } |
| 891 | wp += 4; |
| 892 | cnt -= 4; |
| 893 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 894 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 895 | if (cnt == 0) { |
| 896 | return (0); |
| 897 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 898 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 899 | /* |
| 900 | * handle unaligned tail bytes |
| 901 | */ |
| 902 | data = 0; |
| 903 | for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) { |
| 904 | data = (data << 8) | *src++; |
| 905 | --cnt; |
| 906 | } |
| 907 | for (; i < 4; ++i, ++cp) { |
| 908 | data = (data << 8) | (*(uchar *) cp); |
| 909 | } |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 910 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 911 | return (write_word (info, wp, data)); |
| 912 | } |
| 913 | return 1; |
wdenk | cc1c8a1 | 2002-11-02 22:58:18 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /*----------------------------------------------------------------------- |
| 917 | */ |