Prafulla Wadaskar | 7ce6031 | 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 | |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 52 | #define MACRONIX_SR_WIP (1 << 0) /* Write-in-Progress */ |
| 53 | |
| 54 | struct macronix_spi_flash_params { |
Prafulla Wadaskar | 2efee52 | 2009-07-06 20:29:15 +0530 | [diff] [blame] | 55 | u16 idcode; |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 56 | u16 page_size; |
| 57 | u16 pages_per_sector; |
| 58 | u16 sectors_per_block; |
| 59 | u16 nr_blocks; |
| 60 | const char *name; |
| 61 | }; |
| 62 | |
| 63 | struct macronix_spi_flash { |
| 64 | struct spi_flash flash; |
| 65 | const struct macronix_spi_flash_params *params; |
| 66 | }; |
| 67 | |
| 68 | static inline struct macronix_spi_flash *to_macronix_spi_flash(struct spi_flash |
| 69 | *flash) |
| 70 | { |
| 71 | return container_of(flash, struct macronix_spi_flash, flash); |
| 72 | } |
| 73 | |
| 74 | static const struct macronix_spi_flash_params macronix_spi_flash_table[] = { |
| 75 | { |
Prafulla Wadaskar | 2efee52 | 2009-07-06 20:29:15 +0530 | [diff] [blame] | 76 | .idcode = 0x2015, |
| 77 | .page_size = 256, |
| 78 | .pages_per_sector = 16, |
| 79 | .sectors_per_block = 16, |
| 80 | .nr_blocks = 32, |
| 81 | .name = "MX25L1605D", |
| 82 | }, |
| 83 | { |
| 84 | .idcode = 0x2016, |
| 85 | .page_size = 256, |
| 86 | .pages_per_sector = 16, |
| 87 | .sectors_per_block = 16, |
| 88 | .nr_blocks = 64, |
| 89 | .name = "MX25L3205D", |
| 90 | }, |
| 91 | { |
| 92 | .idcode = 0x2017, |
| 93 | .page_size = 256, |
| 94 | .pages_per_sector = 16, |
| 95 | .sectors_per_block = 16, |
| 96 | .nr_blocks = 128, |
| 97 | .name = "MX25L6405D", |
| 98 | }, |
| 99 | { |
| 100 | .idcode = 0x2018, |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 101 | .page_size = 256, |
| 102 | .pages_per_sector = 16, |
| 103 | .sectors_per_block = 16, |
| 104 | .nr_blocks = 256, |
| 105 | .name = "MX25L12805D", |
| 106 | }, |
Prafulla Wadaskar | 2efee52 | 2009-07-06 20:29:15 +0530 | [diff] [blame] | 107 | { |
| 108 | .idcode = 0x2618, |
| 109 | .page_size = 256, |
| 110 | .pages_per_sector = 16, |
| 111 | .sectors_per_block = 16, |
| 112 | .nr_blocks = 256, |
| 113 | .name = "MX25L12855E", |
| 114 | }, |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static int macronix_wait_ready(struct spi_flash *flash, unsigned long timeout) |
| 118 | { |
| 119 | struct spi_slave *spi = flash->spi; |
| 120 | unsigned long timebase; |
| 121 | int ret; |
| 122 | u8 status; |
| 123 | u8 cmd = CMD_MX25XX_RDSR; |
| 124 | |
| 125 | ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN); |
| 126 | if (ret) { |
| 127 | debug("SF: Failed to send command %02x: %d\n", cmd, ret); |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | timebase = get_timer(0); |
| 132 | do { |
| 133 | ret = spi_xfer(spi, 8, NULL, &status, 0); |
| 134 | if (ret) |
| 135 | return -1; |
| 136 | |
| 137 | if ((status & MACRONIX_SR_WIP) == 0) |
| 138 | break; |
| 139 | |
| 140 | } while (get_timer(timebase) < timeout); |
| 141 | |
| 142 | spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END); |
| 143 | |
| 144 | if ((status & MACRONIX_SR_WIP) == 0) |
| 145 | return 0; |
| 146 | |
| 147 | /* Timed out */ |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | static int macronix_read_fast(struct spi_flash *flash, |
| 152 | u32 offset, size_t len, void *buf) |
| 153 | { |
| 154 | struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash); |
| 155 | unsigned long page_addr; |
| 156 | unsigned long page_size; |
| 157 | u8 cmd[5]; |
| 158 | |
| 159 | page_size = mcx->params->page_size; |
| 160 | page_addr = offset / page_size; |
| 161 | |
| 162 | cmd[0] = CMD_READ_ARRAY_FAST; |
| 163 | cmd[1] = page_addr >> 8; |
| 164 | cmd[2] = page_addr; |
| 165 | cmd[3] = offset % page_size; |
| 166 | cmd[4] = 0x00; |
| 167 | |
| 168 | return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len); |
| 169 | } |
| 170 | |
| 171 | static int macronix_write(struct spi_flash *flash, |
| 172 | u32 offset, size_t len, const void *buf) |
| 173 | { |
| 174 | struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash); |
| 175 | unsigned long page_addr; |
| 176 | unsigned long byte_addr; |
| 177 | unsigned long page_size; |
| 178 | size_t chunk_len; |
| 179 | size_t actual; |
| 180 | int ret; |
| 181 | u8 cmd[4]; |
| 182 | |
| 183 | page_size = mcx->params->page_size; |
| 184 | page_addr = offset / page_size; |
| 185 | byte_addr = offset % page_size; |
| 186 | |
| 187 | ret = spi_claim_bus(flash->spi); |
| 188 | if (ret) { |
| 189 | debug("SF: Unable to claim SPI bus\n"); |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | ret = 0; |
| 194 | for (actual = 0; actual < len; actual += chunk_len) { |
| 195 | chunk_len = min(len - actual, page_size - byte_addr); |
| 196 | |
| 197 | cmd[0] = CMD_MX25XX_PP; |
| 198 | cmd[1] = page_addr >> 8; |
| 199 | cmd[2] = page_addr; |
| 200 | cmd[3] = byte_addr; |
| 201 | |
| 202 | debug |
| 203 | ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n", |
| 204 | buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
| 205 | |
| 206 | ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0); |
| 207 | if (ret < 0) { |
| 208 | debug("SF: Enabling Write failed\n"); |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | ret = spi_flash_cmd_write(flash->spi, cmd, 4, |
| 213 | buf + actual, chunk_len); |
| 214 | if (ret < 0) { |
| 215 | debug("SF: Macronix Page Program failed\n"); |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | ret = macronix_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 220 | if (ret < 0) { |
| 221 | debug("SF: Macronix page programming timed out\n"); |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | page_addr++; |
| 226 | byte_addr = 0; |
| 227 | } |
| 228 | |
| 229 | debug("SF: Macronix: Successfully programmed %u bytes @ 0x%x\n", |
| 230 | len, offset); |
| 231 | |
| 232 | spi_release_bus(flash->spi); |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | int macronix_erase(struct spi_flash *flash, u32 offset, size_t len) |
| 237 | { |
| 238 | struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash); |
| 239 | unsigned long sector_size; |
| 240 | size_t actual; |
| 241 | int ret; |
| 242 | u8 cmd[4]; |
| 243 | |
| 244 | /* |
| 245 | * This function currently uses sector erase only. |
| 246 | * probably speed things up by using bulk erase |
| 247 | * when possible. |
| 248 | */ |
| 249 | |
| 250 | sector_size = mcx->params->page_size * mcx->params->pages_per_sector |
| 251 | * mcx->params->sectors_per_block; |
| 252 | |
| 253 | if (offset % sector_size || len % sector_size) { |
| 254 | debug("SF: Erase offset/length not multiple of sector size\n"); |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | len /= sector_size; |
| 259 | cmd[0] = CMD_MX25XX_BE; |
| 260 | cmd[2] = 0x00; |
| 261 | cmd[3] = 0x00; |
| 262 | |
| 263 | ret = spi_claim_bus(flash->spi); |
| 264 | if (ret) { |
| 265 | debug("SF: Unable to claim SPI bus\n"); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | ret = 0; |
| 270 | for (actual = 0; actual < len; actual++) { |
| 271 | cmd[1] = (offset / sector_size) + actual; |
| 272 | |
| 273 | ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0); |
| 274 | if (ret < 0) { |
| 275 | debug("SF: Enabling Write failed\n"); |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0); |
| 280 | if (ret < 0) { |
| 281 | debug("SF: Macronix page erase failed\n"); |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | ret = macronix_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT); |
| 286 | if (ret < 0) { |
| 287 | debug("SF: Macronix page erase timed out\n"); |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | debug("SF: Macronix: Successfully erased %u bytes @ 0x%x\n", |
| 293 | len * sector_size, offset); |
| 294 | |
| 295 | spi_release_bus(flash->spi); |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode) |
| 300 | { |
| 301 | const struct macronix_spi_flash_params *params; |
| 302 | struct macronix_spi_flash *mcx; |
| 303 | unsigned int i; |
Prafulla Wadaskar | 2efee52 | 2009-07-06 20:29:15 +0530 | [diff] [blame] | 304 | u16 id = idcode[2] | idcode[1] << 8; |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 305 | |
| 306 | for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) { |
| 307 | params = ¯onix_spi_flash_table[i]; |
Prafulla Wadaskar | 2efee52 | 2009-07-06 20:29:15 +0530 | [diff] [blame] | 308 | if (params->idcode == id) |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 309 | break; |
| 310 | } |
| 311 | |
| 312 | if (i == ARRAY_SIZE(macronix_spi_flash_table)) { |
Prafulla Wadaskar | 2efee52 | 2009-07-06 20:29:15 +0530 | [diff] [blame] | 313 | debug("SF: Unsupported Macronix ID %04x\n", id); |
Prafulla Wadaskar | 7ce6031 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | mcx = malloc(sizeof(*mcx)); |
| 318 | if (!mcx) { |
| 319 | debug("SF: Failed to allocate memory\n"); |
| 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | mcx->params = params; |
| 324 | mcx->flash.spi = spi; |
| 325 | mcx->flash.name = params->name; |
| 326 | |
| 327 | mcx->flash.write = macronix_write; |
| 328 | mcx->flash.erase = macronix_erase; |
| 329 | mcx->flash.read = macronix_read_fast; |
| 330 | mcx->flash.size = params->page_size * params->pages_per_sector |
| 331 | * params->sectors_per_block * params->nr_blocks; |
| 332 | |
| 333 | printf("SF: Detected %s with page size %u, total %u bytes\n", |
| 334 | params->name, params->page_size, mcx->flash.size); |
| 335 | |
| 336 | return &mcx->flash; |
| 337 | } |