Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, Google Inc. |
| 3 | * |
| 4 | * Based on drivers/spi/winbond.c |
| 5 | * |
| 6 | * Copyright 2008, Network Appliance Inc. |
| 7 | * Jason McMullan <mcmullan@netapp.com> |
| 8 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 25 | * MA 02110-1301 USA |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | #include <spi_flash.h> |
| 31 | #include "spi_flash_internal.h" |
| 32 | |
| 33 | /* GD25Pxx-specific commands */ |
| 34 | #define CMD_GD25_WREN 0x06 /* Write Enable */ |
| 35 | #define CMD_GD25_WRDI 0x04 /* Write Disable */ |
| 36 | #define CMD_GD25_RDSR 0x05 /* Read Status Register */ |
| 37 | #define CMD_GD25_WRSR 0x01 /* Write Status Register */ |
| 38 | #define CMD_GD25_READ 0x03 /* Read Data Bytes */ |
| 39 | #define CMD_GD25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 40 | #define CMD_GD25_PP 0x02 /* Page Program */ |
| 41 | #define CMD_GD25_SE 0x20 /* Sector (4K) Erase */ |
| 42 | #define CMD_GD25_BE 0xd8 /* Block (64K) Erase */ |
| 43 | #define CMD_GD25_CE 0xc7 /* Chip Erase */ |
| 44 | #define CMD_GD25_DP 0xb9 /* Deep Power-down */ |
| 45 | #define CMD_GD25_RES 0xab /* Release from DP, and Read Signature */ |
| 46 | |
| 47 | struct gigadevice_spi_flash_params { |
| 48 | uint16_t id; |
| 49 | /* Log2 of page size in power-of-two mode */ |
| 50 | uint8_t l2_page_size; |
| 51 | uint16_t pages_per_sector; |
| 52 | uint16_t sectors_per_block; |
| 53 | uint16_t nr_blocks; |
| 54 | const char *name; |
| 55 | }; |
| 56 | |
| 57 | /* spi_flash needs to be first so upper layers can free() it */ |
| 58 | struct gigadevice_spi_flash { |
| 59 | struct spi_flash flash; |
| 60 | const struct gigadevice_spi_flash_params *params; |
| 61 | }; |
| 62 | |
| 63 | static inline struct gigadevice_spi_flash * |
| 64 | to_gigadevice_spi_flash(struct spi_flash *flash) |
| 65 | { |
| 66 | return container_of(flash, struct gigadevice_spi_flash, flash); |
| 67 | } |
| 68 | |
| 69 | static const struct gigadevice_spi_flash_params gigadevice_spi_flash_table[] = { |
| 70 | { |
| 71 | .id = 0x4014, |
| 72 | .l2_page_size = 8, |
| 73 | .pages_per_sector = 16, |
| 74 | .sectors_per_block = 16, |
| 75 | .nr_blocks = 16, |
| 76 | .name = "GD25Q80", |
| 77 | }, |
| 78 | { |
| 79 | .id = 0x4015, |
| 80 | .l2_page_size = 8, |
| 81 | .pages_per_sector = 16, |
| 82 | .sectors_per_block = 16, |
| 83 | .nr_blocks = 32, |
| 84 | .name = "GD25Q16(B)", |
| 85 | }, |
| 86 | { |
| 87 | .id = 0x4016, |
| 88 | .l2_page_size = 8, |
| 89 | .pages_per_sector = 16, |
| 90 | .sectors_per_block = 16, |
| 91 | .nr_blocks = 64, |
| 92 | .name = "GD25Q32(B)", |
| 93 | }, |
| 94 | { |
| 95 | .id = 0x6016, |
| 96 | .l2_page_size = 8, |
| 97 | .pages_per_sector = 16, |
| 98 | .sectors_per_block = 16, |
| 99 | .nr_blocks = 64, |
| 100 | .name = "GD25LQ32", |
| 101 | }, |
| 102 | { |
| 103 | .id = 0x4017, |
| 104 | .l2_page_size = 8, |
| 105 | .pages_per_sector = 16, |
| 106 | .sectors_per_block = 16, |
| 107 | .nr_blocks = 128, |
| 108 | .name = "GD25Q64(B)", |
| 109 | }, |
| 110 | { |
| 111 | .id = 0x4018, |
| 112 | .l2_page_size = 8, |
| 113 | .pages_per_sector = 16, |
| 114 | .sectors_per_block = 16, |
| 115 | .nr_blocks = 256, |
| 116 | .name = "GD25Q128(B)", |
| 117 | }, |
| 118 | }; |
| 119 | |
| 120 | static int gigadevice_write(struct spi_flash *flash, u32 offset, |
| 121 | size_t len, const void *buf) |
| 122 | { |
| 123 | struct gigadevice_spi_flash *stm = to_gigadevice_spi_flash(flash); |
| 124 | unsigned long byte_addr; |
| 125 | unsigned long page_size; |
| 126 | size_t chunk_len; |
| 127 | size_t actual; |
| 128 | int ret; |
| 129 | u8 cmd[4]; |
| 130 | |
| 131 | page_size = min(1 << stm->params->l2_page_size, CONTROLLER_PAGE_LIMIT); |
| 132 | byte_addr = offset % page_size; |
| 133 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 134 | flash->spi->rw = SPI_WRITE_FLAG; |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 135 | ret = spi_claim_bus(flash->spi); |
| 136 | if (ret) { |
| 137 | printk(BIOS_WARNING, |
| 138 | "SF gigadevice.c: Unable to claim SPI bus\n"); |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | for (actual = 0; actual < len; actual += chunk_len) { |
| 143 | chunk_len = min(len - actual, page_size - byte_addr); |
| 144 | |
| 145 | ret = spi_flash_cmd(flash->spi, CMD_GD25_WREN, NULL, 0); |
| 146 | if (ret < 0) { |
| 147 | printk(BIOS_WARNING, |
| 148 | "SF gigadevice.c: Enabling Write failed\n"); |
| 149 | goto out; |
| 150 | } |
| 151 | |
| 152 | cmd[0] = CMD_GD25_PP; |
| 153 | cmd[1] = (offset >> 16) & 0xff; |
| 154 | cmd[2] = (offset >> 8) & 0xff; |
| 155 | cmd[3] = offset & 0xff; |
| 156 | #if CONFIG_DEBUG_SPI_FLASH |
| 157 | printk(BIOS_SPEW, |
Duncan Laurie | 23b0053 | 2012-10-10 14:21:23 -0700 | [diff] [blame] | 158 | "PP gigadevice.c: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }" |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 159 | " chunk_len = %zu\n", buf + actual, |
| 160 | cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
| 161 | #endif |
| 162 | |
| 163 | ret = spi_flash_cmd_write(flash->spi, cmd, 4, |
| 164 | buf + actual, chunk_len); |
| 165 | if (ret < 0) { |
| 166 | printk(BIOS_WARNING, |
| 167 | "SF gigadevice.c: Page Program failed\n"); |
| 168 | goto out; |
| 169 | } |
| 170 | |
| 171 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 172 | if (ret) |
| 173 | goto out; |
| 174 | |
| 175 | offset += chunk_len; |
| 176 | byte_addr = 0; |
| 177 | } |
| 178 | |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 179 | #if CONFIG_DEBUG_SPI_FLASH |
| 180 | printk(BIOS_SPEW, |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 181 | "SF gigadevice.c: Successfully programmed %zu bytes @ %#x\n", |
| 182 | len, (unsigned int)(offset - len)); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 183 | #endif |
| 184 | |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 185 | ret = 0; |
| 186 | |
| 187 | out: |
| 188 | spi_release_bus(flash->spi); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | static int gigadevice_erase(struct spi_flash *flash, u32 offset, size_t len) |
| 193 | { |
| 194 | return spi_flash_cmd_erase(flash, CMD_GD25_SE, offset, len); |
| 195 | } |
| 196 | |
| 197 | struct spi_flash *spi_flash_probe_gigadevice(struct spi_slave *spi, u8 *idcode) |
| 198 | { |
| 199 | const struct gigadevice_spi_flash_params *params; |
| 200 | unsigned page_size; |
| 201 | struct gigadevice_spi_flash *stm; |
| 202 | unsigned int i; |
| 203 | |
| 204 | for (i = 0; i < ARRAY_SIZE(gigadevice_spi_flash_table); i++) { |
| 205 | params = &gigadevice_spi_flash_table[i]; |
| 206 | if (params->id == ((idcode[1] << 8) | idcode[2])) |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | if (i == ARRAY_SIZE(gigadevice_spi_flash_table)) { |
| 211 | printk(BIOS_WARNING, |
| 212 | "SF gigadevice.c: Unsupported ID %#02x%02x\n", |
| 213 | idcode[1], idcode[2]); |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | stm = malloc(sizeof(struct gigadevice_spi_flash)); |
| 218 | if (!stm) { |
| 219 | printk(BIOS_WARNING, |
| 220 | "SF gigadevice.c: Failed to allocate memory\n"); |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | stm->params = params; |
| 225 | stm->flash.spi = spi; |
| 226 | stm->flash.name = params->name; |
| 227 | |
| 228 | /* Assuming power-of-two page size initially. */ |
| 229 | page_size = 1 << params->l2_page_size; |
| 230 | |
| 231 | stm->flash.write = gigadevice_write; |
| 232 | stm->flash.erase = gigadevice_erase; |
Duncan Laurie | 23b0053 | 2012-10-10 14:21:23 -0700 | [diff] [blame] | 233 | #if CONFIG_SPI_FLASH_NO_FAST_READ |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 234 | stm->flash.read = spi_flash_cmd_read_slow; |
| 235 | #else |
| 236 | stm->flash.read = spi_flash_cmd_read_fast; |
| 237 | #endif |
| 238 | stm->flash.sector_size = (1 << stm->params->l2_page_size) * |
| 239 | stm->params->pages_per_sector; |
| 240 | stm->flash.size = page_size * params->pages_per_sector |
| 241 | * params->sectors_per_block |
| 242 | * params->nr_blocks; |
| 243 | |
| 244 | return &stm->flash; |
| 245 | } |