Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for SST serial flashes |
| 3 | * |
| 4 | * (C) Copyright 2000-2002 |
| 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 6 | * Copyright 2008, Network Appliance Inc. |
| 7 | * Jason McMullan <mcmullan@netapp.com> |
| 8 | * Copyright (C) 2004-2007 Freescale Semiconductor, Inc. |
| 9 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
| 10 | * Copyright (c) 2008-2009 Analog Devices Inc. |
| 11 | * |
| 12 | * Licensed under the GPL-2 or later. |
| 13 | */ |
| 14 | |
| 15 | #include <stdlib.h> |
| 16 | #include <spi_flash.h> |
| 17 | #include "spi_flash_internal.h" |
| 18 | |
| 19 | #define CMD_SST_WREN 0x06 /* Write Enable */ |
| 20 | #define CMD_SST_WRDI 0x04 /* Write Disable */ |
| 21 | #define CMD_SST_RDSR 0x05 /* Read Status Register */ |
| 22 | #define CMD_SST_WRSR 0x01 /* Write Status Register */ |
Zheng Bao | b8117b0 | 2012-11-27 18:08:53 +0800 | [diff] [blame] | 23 | #define CMD_SST_EWSR 0x50 /* Enable Write Status Register */ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 24 | #define CMD_SST_READ 0x03 /* Read Data Bytes */ |
| 25 | #define CMD_SST_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 26 | #define CMD_SST_BP 0x02 /* Byte Program */ |
| 27 | #define CMD_SST_AAI_WP 0xAD /* Auto Address Increment Word Program */ |
| 28 | #define CMD_SST_SE 0x20 /* Sector Erase */ |
| 29 | |
| 30 | #define SST_SR_WIP (1 << 0) /* Write-in-Progress */ |
| 31 | #define SST_SR_WEL (1 << 1) /* Write enable */ |
| 32 | #define SST_SR_BP0 (1 << 2) /* Block Protection 0 */ |
| 33 | #define SST_SR_BP1 (1 << 3) /* Block Protection 1 */ |
| 34 | #define SST_SR_BP2 (1 << 4) /* Block Protection 2 */ |
| 35 | #define SST_SR_AAI (1 << 6) /* Addressing mode */ |
| 36 | #define SST_SR_BPL (1 << 7) /* BP bits lock */ |
| 37 | |
| 38 | struct sst_spi_flash_params { |
| 39 | u8 idcode1; |
| 40 | u16 nr_sectors; |
| 41 | const char *name; |
| 42 | }; |
| 43 | |
| 44 | struct sst_spi_flash { |
| 45 | struct spi_flash flash; |
| 46 | const struct sst_spi_flash_params *params; |
| 47 | }; |
| 48 | |
| 49 | static inline struct sst_spi_flash *to_sst_spi_flash(struct spi_flash *flash) |
| 50 | { |
| 51 | return container_of(flash, struct sst_spi_flash, flash); |
| 52 | } |
| 53 | |
| 54 | #define SST_SECTOR_SIZE (4 * 1024) |
| 55 | static const struct sst_spi_flash_params sst_spi_flash_table[] = { |
| 56 | { |
| 57 | .idcode1 = 0x8d, |
| 58 | .nr_sectors = 128, |
| 59 | .name = "SST25VF040B", |
| 60 | },{ |
| 61 | .idcode1 = 0x8e, |
| 62 | .nr_sectors = 256, |
| 63 | .name = "SST25VF080B", |
| 64 | },{ |
| 65 | .idcode1 = 0x41, |
| 66 | .nr_sectors = 512, |
| 67 | .name = "SST25VF016B", |
| 68 | },{ |
| 69 | .idcode1 = 0x4a, |
| 70 | .nr_sectors = 1024, |
| 71 | .name = "SST25VF032B", |
| 72 | },{ |
| 73 | .idcode1 = 0x4b, |
| 74 | .nr_sectors = 2048, |
| 75 | .name = "SST25VF064C", |
| 76 | },{ |
| 77 | .idcode1 = 0x01, |
| 78 | .nr_sectors = 16, |
| 79 | .name = "SST25WF512", |
| 80 | },{ |
| 81 | .idcode1 = 0x02, |
| 82 | .nr_sectors = 32, |
| 83 | .name = "SST25WF010", |
| 84 | },{ |
| 85 | .idcode1 = 0x03, |
| 86 | .nr_sectors = 64, |
| 87 | .name = "SST25WF020", |
| 88 | },{ |
| 89 | .idcode1 = 0x04, |
| 90 | .nr_sectors = 128, |
| 91 | .name = "SST25WF040", |
| 92 | }, |
| 93 | }; |
| 94 | |
| 95 | static int |
| 96 | sst_enable_writing(struct spi_flash *flash) |
| 97 | { |
| 98 | int ret = spi_flash_cmd(flash->spi, CMD_SST_WREN, NULL, 0); |
| 99 | if (ret) |
| 100 | printk(BIOS_WARNING, "SF: Enabling Write failed\n"); |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static int |
Zheng Bao | b8117b0 | 2012-11-27 18:08:53 +0800 | [diff] [blame] | 105 | sst_enable_writing_status(struct spi_flash *flash) |
| 106 | { |
| 107 | int ret = spi_flash_cmd(flash->spi, CMD_SST_EWSR, NULL, 0); |
| 108 | if (ret) |
| 109 | printk(BIOS_WARNING, "SF: Enabling Write Status failed\n"); |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | static int |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 114 | sst_disable_writing(struct spi_flash *flash) |
| 115 | { |
| 116 | int ret = spi_flash_cmd(flash->spi, CMD_SST_WRDI, NULL, 0); |
| 117 | if (ret) |
| 118 | printk(BIOS_WARNING, "SF: Disabling Write failed\n"); |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | static int |
| 123 | sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf) |
| 124 | { |
| 125 | int ret; |
| 126 | u8 cmd[4] = { |
| 127 | CMD_SST_BP, |
| 128 | offset >> 16, |
| 129 | offset >> 8, |
| 130 | offset, |
| 131 | }; |
| 132 | |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 133 | #if CONFIG_DEBUG_SPI_FLASH |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 134 | printk(BIOS_SPEW, "BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n", |
| 135 | spi_w8r8(flash->spi, CMD_SST_RDSR), buf, cmd[0], offset); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 136 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 137 | |
| 138 | ret = sst_enable_writing(flash); |
| 139 | if (ret) |
| 140 | return ret; |
| 141 | |
| 142 | ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1); |
| 143 | if (ret) |
| 144 | return ret; |
| 145 | |
| 146 | return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 147 | } |
| 148 | |
| 149 | static int |
| 150 | sst_write(struct spi_flash *flash, u32 offset, size_t len, const void *buf) |
| 151 | { |
| 152 | size_t actual, cmd_len; |
| 153 | int ret; |
| 154 | u8 cmd[4]; |
| 155 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame^] | 156 | flash->spi->rw = SPI_WRITE_FLAG; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 157 | ret = spi_claim_bus(flash->spi); |
| 158 | if (ret) { |
| 159 | printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n"); |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | /* If the data is not word aligned, write out leading single byte */ |
| 164 | actual = offset % 2; |
| 165 | if (actual) { |
| 166 | ret = sst_byte_write(flash, offset, buf); |
| 167 | if (ret) |
| 168 | goto done; |
| 169 | } |
| 170 | offset += actual; |
| 171 | |
| 172 | ret = sst_enable_writing(flash); |
| 173 | if (ret) |
| 174 | goto done; |
| 175 | |
| 176 | cmd_len = 4; |
| 177 | cmd[0] = CMD_SST_AAI_WP; |
| 178 | cmd[1] = offset >> 16; |
| 179 | cmd[2] = offset >> 8; |
| 180 | cmd[3] = offset; |
| 181 | |
| 182 | for (; actual < len - 1; actual += 2) { |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 183 | #if CONFIG_DEBUG_SPI_FLASH |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 184 | printk(BIOS_SPEW, "WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n", |
| 185 | spi_w8r8(flash->spi, CMD_SST_RDSR), buf + actual, cmd[0], |
| 186 | offset); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 187 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 188 | |
| 189 | ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len, |
| 190 | buf + actual, 2); |
| 191 | if (ret) { |
| 192 | printk(BIOS_WARNING, "SF: SST word program failed\n"); |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 197 | if (ret) |
| 198 | break; |
| 199 | |
| 200 | cmd_len = 1; |
| 201 | offset += 2; |
| 202 | } |
| 203 | |
| 204 | if (!ret) |
| 205 | ret = sst_disable_writing(flash); |
| 206 | |
| 207 | /* If there is a single trailing byte, write it out */ |
| 208 | if (!ret && actual != len) |
| 209 | ret = sst_byte_write(flash, offset, buf + actual); |
| 210 | |
Patrick Georgi | 20959ba | 2012-05-12 23:30:36 +0200 | [diff] [blame] | 211 | done: |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 212 | #if CONFIG_DEBUG_SPI_FLASH |
| 213 | printk(BIOS_SPEW, "SF: SST: program %s %zu bytes @ 0x%lx\n", |
Stefan Reinauer | 8ea5a34 | 2012-05-14 13:52:32 -0700 | [diff] [blame] | 214 | ret ? "failure" : "success", len, (unsigned long)offset - actual); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 215 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 216 | spi_release_bus(flash->spi); |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static int sst_erase(struct spi_flash *flash, u32 offset, size_t len) |
| 221 | { |
| 222 | return spi_flash_cmd_erase(flash, CMD_SST_SE, offset, len); |
| 223 | } |
| 224 | |
| 225 | static int |
| 226 | sst_unlock(struct spi_flash *flash) |
| 227 | { |
| 228 | int ret; |
| 229 | u8 cmd, status; |
| 230 | |
Zheng Bao | b8117b0 | 2012-11-27 18:08:53 +0800 | [diff] [blame] | 231 | ret = sst_enable_writing_status(flash); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 232 | if (ret) |
| 233 | return ret; |
| 234 | |
| 235 | cmd = CMD_SST_WRSR; |
| 236 | status = 0; |
| 237 | ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1); |
| 238 | if (ret) |
| 239 | printk(BIOS_WARNING, "SF: Unable to set status byte\n"); |
| 240 | |
| 241 | printk(BIOS_INFO, "SF: SST: status = %x\n", spi_w8r8(flash->spi, CMD_SST_RDSR)); |
| 242 | |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | struct spi_flash * |
| 247 | spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode) |
| 248 | { |
| 249 | const struct sst_spi_flash_params *params; |
| 250 | struct sst_spi_flash *stm; |
| 251 | size_t i; |
| 252 | |
| 253 | for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) { |
| 254 | params = &sst_spi_flash_table[i]; |
| 255 | if (params->idcode1 == idcode[2]) |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | if (i == ARRAY_SIZE(sst_spi_flash_table)) { |
| 260 | printk(BIOS_WARNING, "SF: Unsupported SST ID %02x\n", idcode[1]); |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | stm = malloc(sizeof(*stm)); |
| 265 | if (!stm) { |
| 266 | printk(BIOS_WARNING, "SF: Failed to allocate memory\n"); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | stm->params = params; |
| 271 | stm->flash.spi = spi; |
| 272 | stm->flash.name = params->name; |
| 273 | |
| 274 | stm->flash.write = sst_write; |
| 275 | stm->flash.erase = sst_erase; |
| 276 | stm->flash.read = spi_flash_cmd_read_fast; |
| 277 | stm->flash.sector_size = SST_SECTOR_SIZE; |
| 278 | stm->flash.size = stm->flash.sector_size * params->nr_sectors; |
| 279 | |
| 280 | /* Flash powers up read-only, so clear BP# bits */ |
| 281 | sst_unlock(&stm->flash); |
| 282 | |
| 283 | return &stm->flash; |
| 284 | } |