Prafulla Wadaskar | 0956907 | 2009-04-06 21:24:43 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2009(C) Marvell International Ltd. and its affiliates |
| 3 | * Prafulla Wadaskar <prafulla@marvell.com> |
| 4 | * |
| 5 | * Based on drivers/mtd/spi/stmicro.c |
| 6 | * |
| 7 | * Copyright 2008, Network Appliance Inc. |
| 8 | * Jason McMullan <mcmullan@netapp.com> |
| 9 | * |
| 10 | * Copyright (C) 2004-2007 Freescale Semiconductor, Inc. |
| 11 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
| 12 | * |
| 13 | * See file CREDITS for list of people who contributed to this |
| 14 | * project. |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License as |
| 18 | * published by the Free Software Foundation; either version 2 of |
| 19 | * the License, or (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 29 | * MA 02110-1301 USA |
| 30 | */ |
| 31 | |
| 32 | #include <common.h> |
| 33 | #include <malloc.h> |
| 34 | #include <spi_flash.h> |
| 35 | |
| 36 | #include "spi_flash_internal.h" |
| 37 | |
| 38 | /* MX25xx-specific commands */ |
| 39 | #define CMD_MX25XX_WREN 0x06 /* Write Enable */ |
| 40 | #define CMD_MX25XX_WRDI 0x04 /* Write Disable */ |
| 41 | #define CMD_MX25XX_RDSR 0x05 /* Read Status Register */ |
| 42 | #define CMD_MX25XX_WRSR 0x01 /* Write Status Register */ |
| 43 | #define CMD_MX25XX_READ 0x03 /* Read Data Bytes */ |
| 44 | #define CMD_MX25XX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 45 | #define CMD_MX25XX_PP 0x02 /* Page Program */ |
| 46 | #define CMD_MX25XX_SE 0x20 /* Sector Erase */ |
| 47 | #define CMD_MX25XX_BE 0xD8 /* Block Erase */ |
| 48 | #define CMD_MX25XX_CE 0xc7 /* Chip Erase */ |
| 49 | #define CMD_MX25XX_DP 0xb9 /* Deep Power-down */ |
| 50 | #define CMD_MX25XX_RES 0xab /* Release from DP, and Read Signature */ |
| 51 | |
| 52 | #define MXIC_ID_MX2516 0x15 |
| 53 | #define MXIC_ID_MX2520 0x12 |
| 54 | #define MXIC_ID_MX2532 0x16 |
| 55 | #define MXIC_ID_MX2540 0x13 |
| 56 | #define MXIC_ID_MX2564 0x17 |
| 57 | #define MXIC_ID_MX2580 0x14 |
| 58 | #define MXIC_ID_MX25128 0x18 |
| 59 | |
| 60 | #define MACRONIX_SR_WIP (1 << 0) /* Write-in-Progress */ |
| 61 | |
| 62 | struct macronix_spi_flash_params { |
| 63 | u8 idcode1; |
| 64 | u16 page_size; |
| 65 | u16 pages_per_sector; |
| 66 | u16 sectors_per_block; |
| 67 | u16 nr_blocks; |
| 68 | const char *name; |
| 69 | }; |
| 70 | |
| 71 | struct macronix_spi_flash { |
| 72 | struct spi_flash flash; |
| 73 | const struct macronix_spi_flash_params *params; |
| 74 | }; |
| 75 | |
| 76 | static inline struct macronix_spi_flash *to_macronix_spi_flash(struct spi_flash |
| 77 | *flash) |
| 78 | { |
| 79 | return container_of(flash, struct macronix_spi_flash, flash); |
| 80 | } |
| 81 | |
| 82 | static const struct macronix_spi_flash_params macronix_spi_flash_table[] = { |
| 83 | { |
| 84 | .idcode1 = MXIC_ID_MX25128, |
| 85 | .page_size = 256, |
| 86 | .pages_per_sector = 16, |
| 87 | .sectors_per_block = 16, |
| 88 | .nr_blocks = 256, |
| 89 | .name = "MX25L12805D", |
| 90 | }, |
| 91 | }; |
| 92 | |
| 93 | static int macronix_wait_ready(struct spi_flash *flash, unsigned long timeout) |
| 94 | { |
| 95 | struct spi_slave *spi = flash->spi; |
| 96 | unsigned long timebase; |
| 97 | int ret; |
| 98 | u8 status; |
| 99 | u8 cmd = CMD_MX25XX_RDSR; |
| 100 | |
| 101 | ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN); |
| 102 | if (ret) { |
| 103 | debug("SF: Failed to send command %02x: %d\n", cmd, ret); |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | timebase = get_timer(0); |
| 108 | do { |
| 109 | ret = spi_xfer(spi, 8, NULL, &status, 0); |
| 110 | if (ret) |
| 111 | return -1; |
| 112 | |
| 113 | if ((status & MACRONIX_SR_WIP) == 0) |
| 114 | break; |
| 115 | |
| 116 | } while (get_timer(timebase) < timeout); |
| 117 | |
| 118 | spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END); |
| 119 | |
| 120 | if ((status & MACRONIX_SR_WIP) == 0) |
| 121 | return 0; |
| 122 | |
| 123 | /* Timed out */ |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | static int macronix_read_fast(struct spi_flash *flash, |
| 128 | u32 offset, size_t len, void *buf) |
| 129 | { |
| 130 | struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash); |
| 131 | unsigned long page_addr; |
| 132 | unsigned long page_size; |
| 133 | u8 cmd[5]; |
| 134 | |
| 135 | page_size = mcx->params->page_size; |
| 136 | page_addr = offset / page_size; |
| 137 | |
| 138 | cmd[0] = CMD_READ_ARRAY_FAST; |
| 139 | cmd[1] = page_addr >> 8; |
| 140 | cmd[2] = page_addr; |
| 141 | cmd[3] = offset % page_size; |
| 142 | cmd[4] = 0x00; |
| 143 | |
| 144 | return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len); |
| 145 | } |
| 146 | |
| 147 | static int macronix_write(struct spi_flash *flash, |
| 148 | u32 offset, size_t len, const void *buf) |
| 149 | { |
| 150 | struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash); |
| 151 | unsigned long page_addr; |
| 152 | unsigned long byte_addr; |
| 153 | unsigned long page_size; |
| 154 | size_t chunk_len; |
| 155 | size_t actual; |
| 156 | int ret; |
| 157 | u8 cmd[4]; |
| 158 | |
| 159 | page_size = mcx->params->page_size; |
| 160 | page_addr = offset / page_size; |
| 161 | byte_addr = offset % page_size; |
| 162 | |
| 163 | ret = spi_claim_bus(flash->spi); |
| 164 | if (ret) { |
| 165 | debug("SF: Unable to claim SPI bus\n"); |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | ret = 0; |
| 170 | for (actual = 0; actual < len; actual += chunk_len) { |
| 171 | chunk_len = min(len - actual, page_size - byte_addr); |
| 172 | |
| 173 | cmd[0] = CMD_MX25XX_PP; |
| 174 | cmd[1] = page_addr >> 8; |
| 175 | cmd[2] = page_addr; |
| 176 | cmd[3] = byte_addr; |
| 177 | |
| 178 | debug |
| 179 | ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n", |
| 180 | buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
| 181 | |
| 182 | ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0); |
| 183 | if (ret < 0) { |
| 184 | debug("SF: Enabling Write failed\n"); |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | ret = spi_flash_cmd_write(flash->spi, cmd, 4, |
| 189 | buf + actual, chunk_len); |
| 190 | if (ret < 0) { |
| 191 | debug("SF: Macronix Page Program failed\n"); |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | ret = macronix_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 196 | if (ret < 0) { |
| 197 | debug("SF: Macronix page programming timed out\n"); |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | page_addr++; |
| 202 | byte_addr = 0; |
| 203 | } |
| 204 | |
| 205 | debug("SF: Macronix: Successfully programmed %u bytes @ 0x%x\n", |
| 206 | len, offset); |
| 207 | |
| 208 | spi_release_bus(flash->spi); |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | int macronix_erase(struct spi_flash *flash, u32 offset, size_t len) |
| 213 | { |
| 214 | struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash); |
| 215 | unsigned long sector_size; |
| 216 | size_t actual; |
| 217 | int ret; |
| 218 | u8 cmd[4]; |
| 219 | |
| 220 | /* |
| 221 | * This function currently uses sector erase only. |
| 222 | * probably speed things up by using bulk erase |
| 223 | * when possible. |
| 224 | */ |
| 225 | |
| 226 | sector_size = mcx->params->page_size * mcx->params->pages_per_sector |
| 227 | * mcx->params->sectors_per_block; |
| 228 | |
| 229 | if (offset % sector_size || len % sector_size) { |
| 230 | debug("SF: Erase offset/length not multiple of sector size\n"); |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | len /= sector_size; |
| 235 | cmd[0] = CMD_MX25XX_BE; |
| 236 | cmd[2] = 0x00; |
| 237 | cmd[3] = 0x00; |
| 238 | |
| 239 | ret = spi_claim_bus(flash->spi); |
| 240 | if (ret) { |
| 241 | debug("SF: Unable to claim SPI bus\n"); |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | ret = 0; |
| 246 | for (actual = 0; actual < len; actual++) { |
| 247 | cmd[1] = (offset / sector_size) + actual; |
| 248 | |
| 249 | ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0); |
| 250 | if (ret < 0) { |
| 251 | debug("SF: Enabling Write failed\n"); |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0); |
| 256 | if (ret < 0) { |
| 257 | debug("SF: Macronix page erase failed\n"); |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | ret = macronix_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT); |
| 262 | if (ret < 0) { |
| 263 | debug("SF: Macronix page erase timed out\n"); |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | debug("SF: Macronix: Successfully erased %u bytes @ 0x%x\n", |
| 269 | len * sector_size, offset); |
| 270 | |
| 271 | spi_release_bus(flash->spi); |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode) |
| 276 | { |
| 277 | const struct macronix_spi_flash_params *params; |
| 278 | struct macronix_spi_flash *mcx; |
| 279 | unsigned int i; |
| 280 | |
| 281 | for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) { |
| 282 | params = ¯onix_spi_flash_table[i]; |
| 283 | if (params->idcode1 == idcode[2]) |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | if (i == ARRAY_SIZE(macronix_spi_flash_table)) { |
| 288 | debug("SF: Unsupported Macronix ID %02x\n", idcode[1]); |
| 289 | return NULL; |
| 290 | } |
| 291 | |
| 292 | mcx = malloc(sizeof(*mcx)); |
| 293 | if (!mcx) { |
| 294 | debug("SF: Failed to allocate memory\n"); |
| 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | mcx->params = params; |
| 299 | mcx->flash.spi = spi; |
| 300 | mcx->flash.name = params->name; |
| 301 | |
| 302 | mcx->flash.write = macronix_write; |
| 303 | mcx->flash.erase = macronix_erase; |
| 304 | mcx->flash.read = macronix_read_fast; |
| 305 | mcx->flash.size = params->page_size * params->pages_per_sector |
| 306 | * params->sectors_per_block * params->nr_blocks; |
| 307 | |
| 308 | printf("SF: Detected %s with page size %u, total %u bytes\n", |
| 309 | params->name, params->page_size, mcx->flash.size); |
| 310 | |
| 311 | return &mcx->flash; |
| 312 | } |