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> |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 31 | |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 32 | #include "spi_flash_internal.h" |
| 33 | |
| 34 | /* GD25Pxx-specific commands */ |
| 35 | #define CMD_GD25_WREN 0x06 /* Write Enable */ |
| 36 | #define CMD_GD25_WRDI 0x04 /* Write Disable */ |
| 37 | #define CMD_GD25_RDSR 0x05 /* Read Status Register */ |
| 38 | #define CMD_GD25_WRSR 0x01 /* Write Status Register */ |
| 39 | #define CMD_GD25_READ 0x03 /* Read Data Bytes */ |
| 40 | #define CMD_GD25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 41 | #define CMD_GD25_PP 0x02 /* Page Program */ |
| 42 | #define CMD_GD25_SE 0x20 /* Sector (4K) Erase */ |
| 43 | #define CMD_GD25_BE 0xd8 /* Block (64K) Erase */ |
| 44 | #define CMD_GD25_CE 0xc7 /* Chip Erase */ |
| 45 | #define CMD_GD25_DP 0xb9 /* Deep Power-down */ |
| 46 | #define CMD_GD25_RES 0xab /* Release from DP, and Read Signature */ |
| 47 | |
| 48 | struct gigadevice_spi_flash_params { |
| 49 | uint16_t id; |
| 50 | /* Log2 of page size in power-of-two mode */ |
| 51 | uint8_t l2_page_size; |
| 52 | uint16_t pages_per_sector; |
| 53 | uint16_t sectors_per_block; |
| 54 | uint16_t nr_blocks; |
| 55 | const char *name; |
| 56 | }; |
| 57 | |
| 58 | /* spi_flash needs to be first so upper layers can free() it */ |
| 59 | struct gigadevice_spi_flash { |
| 60 | struct spi_flash flash; |
| 61 | const struct gigadevice_spi_flash_params *params; |
| 62 | }; |
| 63 | |
| 64 | static inline struct gigadevice_spi_flash * |
| 65 | to_gigadevice_spi_flash(struct spi_flash *flash) |
| 66 | { |
| 67 | return container_of(flash, struct gigadevice_spi_flash, flash); |
| 68 | } |
| 69 | |
| 70 | static const struct gigadevice_spi_flash_params gigadevice_spi_flash_table[] = { |
| 71 | { |
| 72 | .id = 0x4014, |
| 73 | .l2_page_size = 8, |
| 74 | .pages_per_sector = 16, |
| 75 | .sectors_per_block = 16, |
| 76 | .nr_blocks = 16, |
| 77 | .name = "GD25Q80", |
| 78 | }, |
| 79 | { |
| 80 | .id = 0x4015, |
| 81 | .l2_page_size = 8, |
| 82 | .pages_per_sector = 16, |
| 83 | .sectors_per_block = 16, |
| 84 | .nr_blocks = 32, |
| 85 | .name = "GD25Q16(B)", |
| 86 | }, |
| 87 | { |
| 88 | .id = 0x4016, |
| 89 | .l2_page_size = 8, |
| 90 | .pages_per_sector = 16, |
| 91 | .sectors_per_block = 16, |
| 92 | .nr_blocks = 64, |
| 93 | .name = "GD25Q32(B)", |
| 94 | }, |
| 95 | { |
| 96 | .id = 0x6016, |
| 97 | .l2_page_size = 8, |
| 98 | .pages_per_sector = 16, |
| 99 | .sectors_per_block = 16, |
| 100 | .nr_blocks = 64, |
| 101 | .name = "GD25LQ32", |
| 102 | }, |
| 103 | { |
| 104 | .id = 0x4017, |
| 105 | .l2_page_size = 8, |
| 106 | .pages_per_sector = 16, |
| 107 | .sectors_per_block = 16, |
| 108 | .nr_blocks = 128, |
Marc Jones | 9bab54b | 2014-04-03 16:37:06 -0600 | [diff] [blame] | 109 | .name = "GD25Q64B/GD25B64C", |
| 110 | }, |
| 111 | { |
| 112 | .id = 0x6017, |
| 113 | .l2_page_size = 8, |
| 114 | .pages_per_sector = 16, |
| 115 | .sectors_per_block = 16, |
| 116 | .nr_blocks = 128, |
| 117 | .name = "GD25LQ64C/GD25LB64C", |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 118 | }, |
| 119 | { |
| 120 | .id = 0x4018, |
| 121 | .l2_page_size = 8, |
| 122 | .pages_per_sector = 16, |
| 123 | .sectors_per_block = 16, |
| 124 | .nr_blocks = 256, |
| 125 | .name = "GD25Q128(B)", |
| 126 | }, |
| 127 | }; |
| 128 | |
| 129 | static int gigadevice_write(struct spi_flash *flash, u32 offset, |
| 130 | size_t len, const void *buf) |
| 131 | { |
| 132 | struct gigadevice_spi_flash *stm = to_gigadevice_spi_flash(flash); |
| 133 | unsigned long byte_addr; |
| 134 | unsigned long page_size; |
| 135 | size_t chunk_len; |
| 136 | size_t actual; |
David Hendricks | 032c843 | 2014-04-11 19:48:55 -0700 | [diff] [blame] | 137 | int ret = 0; |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 138 | u8 cmd[4]; |
| 139 | |
Kyösti Mälkki | 77d1280 | 2014-06-29 16:15:39 +0300 | [diff] [blame] | 140 | page_size = 1 << stm->params->l2_page_size; |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 141 | byte_addr = offset % page_size; |
| 142 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 143 | flash->spi->rw = SPI_WRITE_FLAG; |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 144 | |
| 145 | for (actual = 0; actual < len; actual += chunk_len) { |
| 146 | chunk_len = min(len - actual, page_size - byte_addr); |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 147 | chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 148 | |
| 149 | ret = spi_flash_cmd(flash->spi, CMD_GD25_WREN, NULL, 0); |
| 150 | if (ret < 0) { |
| 151 | printk(BIOS_WARNING, |
| 152 | "SF gigadevice.c: Enabling Write failed\n"); |
| 153 | goto out; |
| 154 | } |
| 155 | |
| 156 | cmd[0] = CMD_GD25_PP; |
| 157 | cmd[1] = (offset >> 16) & 0xff; |
| 158 | cmd[2] = (offset >> 8) & 0xff; |
| 159 | cmd[3] = offset & 0xff; |
| 160 | #if CONFIG_DEBUG_SPI_FLASH |
| 161 | printk(BIOS_SPEW, |
Duncan Laurie | 23b0053 | 2012-10-10 14:21:23 -0700 | [diff] [blame] | 162 | "PP gigadevice.c: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }" |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 163 | " chunk_len = %zu\n", buf + actual, |
| 164 | cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
| 165 | #endif |
| 166 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 167 | ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 168 | buf + actual, chunk_len); |
| 169 | if (ret < 0) { |
| 170 | printk(BIOS_WARNING, |
| 171 | "SF gigadevice.c: Page Program failed\n"); |
| 172 | goto out; |
| 173 | } |
| 174 | |
| 175 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 176 | if (ret) |
| 177 | goto out; |
| 178 | |
| 179 | offset += chunk_len; |
| 180 | byte_addr = 0; |
| 181 | } |
| 182 | |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 183 | #if CONFIG_DEBUG_SPI_FLASH |
| 184 | printk(BIOS_SPEW, |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 185 | "SF gigadevice.c: Successfully programmed %zu bytes @ %#x\n", |
| 186 | len, (unsigned int)(offset - len)); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 187 | #endif |
| 188 | |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 189 | ret = 0; |
| 190 | |
| 191 | out: |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 192 | return ret; |
| 193 | } |
| 194 | |
David Hendricks | fccc7de | 2014-12-12 14:07:08 -0800 | [diff] [blame] | 195 | static struct gigadevice_spi_flash stm; |
| 196 | |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 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; |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 201 | unsigned int i; |
| 202 | |
| 203 | for (i = 0; i < ARRAY_SIZE(gigadevice_spi_flash_table); i++) { |
| 204 | params = &gigadevice_spi_flash_table[i]; |
| 205 | if (params->id == ((idcode[1] << 8) | idcode[2])) |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | if (i == ARRAY_SIZE(gigadevice_spi_flash_table)) { |
| 210 | printk(BIOS_WARNING, |
| 211 | "SF gigadevice.c: Unsupported ID %#02x%02x\n", |
| 212 | idcode[1], idcode[2]); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
David Hendricks | fccc7de | 2014-12-12 14:07:08 -0800 | [diff] [blame] | 216 | stm.params = params; |
| 217 | stm.flash.spi = spi; |
| 218 | stm.flash.name = params->name; |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 219 | |
| 220 | /* Assuming power-of-two page size initially. */ |
| 221 | page_size = 1 << params->l2_page_size; |
| 222 | |
David Hendricks | fccc7de | 2014-12-12 14:07:08 -0800 | [diff] [blame] | 223 | stm.flash.write = gigadevice_write; |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 224 | stm.flash.erase = spi_flash_cmd_erase; |
Duncan Laurie | fb03239 | 2015-01-15 15:28:46 -0800 | [diff] [blame^] | 225 | stm.flash.status = spi_flash_cmd_status; |
Duncan Laurie | 23b0053 | 2012-10-10 14:21:23 -0700 | [diff] [blame] | 226 | #if CONFIG_SPI_FLASH_NO_FAST_READ |
David Hendricks | fccc7de | 2014-12-12 14:07:08 -0800 | [diff] [blame] | 227 | stm.flash.read = spi_flash_cmd_read_slow; |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 228 | #else |
David Hendricks | fccc7de | 2014-12-12 14:07:08 -0800 | [diff] [blame] | 229 | stm.flash.read = spi_flash_cmd_read_fast; |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 230 | #endif |
David Hendricks | fccc7de | 2014-12-12 14:07:08 -0800 | [diff] [blame] | 231 | stm.flash.sector_size = (1 << stm.params->l2_page_size) * |
| 232 | stm.params->pages_per_sector; |
| 233 | stm.flash.size = page_size * params->pages_per_sector |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 234 | * params->sectors_per_block |
| 235 | * params->nr_blocks; |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 236 | stm.flash.erase_cmd = CMD_GD25_SE; |
Duncan Laurie | fb03239 | 2015-01-15 15:28:46 -0800 | [diff] [blame^] | 237 | stm.flash.status_cmd = CMD_GD25_RDSR; |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 238 | |
David Hendricks | fccc7de | 2014-12-12 14:07:08 -0800 | [diff] [blame] | 239 | return &stm.flash; |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 240 | } |