Kyösti Mälkki | 96d9276 | 2014-11-11 15:04:38 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2008, Network Appliance Inc. |
| 3 | * Copyright 2014, Sage Electronic Engineering, LLC. |
| 4 | * |
| 5 | * Author: Jason McMullan <mcmullan <at> netapp.com> |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <spi_flash.h> |
| 11 | #include "spi_flash_internal.h" |
| 12 | |
| 13 | /* M25Pxx-specific commands */ |
| 14 | #define CMD_AT25_WREN 0x06 /* Write Enable */ |
| 15 | #define CMD_AT25_WRDI 0x04 /* Write Disable */ |
| 16 | #define CMD_AT25_RDSR 0x05 /* Read Status Register */ |
| 17 | #define CMD_AT25_WRSR 0x01 /* Write Status Register */ |
| 18 | #define CMD_AT25_READ 0x03 /* Read Data Bytes */ |
| 19 | #define CMD_AT25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 20 | #define CMD_AT25_PP 0x02 /* Page Program */ |
| 21 | #define CMD_AT25_SE 0x20 /* Sector (4K) Erase */ |
| 22 | #define CMD_AT25_BE 0xd8 /* Block (64K) Erase */ |
| 23 | #define CMD_AT25_CE 0xc7 /* Chip Erase */ |
| 24 | #define CMD_AT25_DP 0xb9 /* Deep Power-down */ |
| 25 | #define CMD_AT25_RES 0xab /* Release from DP, and Read Signature */ |
| 26 | |
| 27 | struct atmel_spi_flash_params { |
| 28 | uint16_t id; |
| 29 | /* Log2 of page size in power-of-two mode */ |
| 30 | uint8_t l2_page_size; |
| 31 | uint16_t pages_per_sector; |
| 32 | uint16_t sectors_per_block; |
| 33 | uint16_t nr_blocks; |
| 34 | const char *name; |
| 35 | }; |
| 36 | |
| 37 | /* spi_flash needs to be first so upper layers can free() it */ |
| 38 | struct atmel_spi_flash { |
| 39 | struct spi_flash flash; |
| 40 | const struct atmel_spi_flash_params *params; |
| 41 | }; |
| 42 | |
| 43 | static inline struct atmel_spi_flash * |
| 44 | to_atmel_spi_flash(struct spi_flash *flash) |
| 45 | { |
| 46 | return container_of(flash, struct atmel_spi_flash, flash); |
| 47 | } |
| 48 | |
| 49 | static const struct atmel_spi_flash_params atmel_spi_flash_table[] = { |
| 50 | { |
| 51 | .id = 0x3015, |
| 52 | .l2_page_size = 8, |
| 53 | .pages_per_sector = 16, |
| 54 | .sectors_per_block = 16, |
| 55 | .nr_blocks = 32, |
| 56 | .name = "AT25X16", |
| 57 | }, |
| 58 | { |
| 59 | .id = 0x47, |
| 60 | .l2_page_size = 8, |
| 61 | .pages_per_sector = 16, |
| 62 | .sectors_per_block = 16, |
| 63 | .nr_blocks = 64, |
| 64 | .name = "AT25DF32", |
| 65 | }, |
| 66 | { |
| 67 | .id = 0x3017, |
| 68 | .l2_page_size = 8, |
| 69 | .pages_per_sector = 16, |
| 70 | .sectors_per_block = 16, |
| 71 | .nr_blocks = 128, |
| 72 | .name = "AT25X64", |
| 73 | }, |
| 74 | { |
| 75 | .id = 0x4015, |
| 76 | .l2_page_size = 8, |
| 77 | .pages_per_sector = 16, |
| 78 | .sectors_per_block = 16, |
| 79 | .nr_blocks = 32, |
| 80 | .name = "AT25Q16", |
| 81 | }, |
| 82 | { |
| 83 | .id = 0x4016, |
| 84 | .l2_page_size = 8, |
| 85 | .pages_per_sector = 16, |
| 86 | .sectors_per_block = 16, |
| 87 | .nr_blocks = 64, |
| 88 | .name = "AT25Q32", |
| 89 | }, |
| 90 | { |
| 91 | .id = 0x4017, |
| 92 | .l2_page_size = 8, |
| 93 | .pages_per_sector = 16, |
| 94 | .sectors_per_block = 16, |
| 95 | .nr_blocks = 128, |
| 96 | .name = "AT25Q64", |
| 97 | }, |
| 98 | { |
| 99 | .id = 0x4018, |
| 100 | .l2_page_size = 8, |
| 101 | .pages_per_sector = 16, |
| 102 | .sectors_per_block = 16, |
| 103 | .nr_blocks = 256, |
| 104 | .name = "AT25Q128", |
| 105 | }, |
| 106 | }; |
| 107 | |
| 108 | static int atmel_write(struct spi_flash *flash, |
| 109 | u32 offset, size_t len, const void *buf) |
| 110 | { |
| 111 | struct atmel_spi_flash *stm = to_atmel_spi_flash(flash); |
| 112 | unsigned long byte_addr; |
| 113 | unsigned long page_size; |
| 114 | size_t chunk_len; |
| 115 | size_t actual; |
| 116 | int ret; |
| 117 | u8 cmd[4]; |
| 118 | |
| 119 | page_size = 1 << stm->params->l2_page_size; |
| 120 | byte_addr = offset % page_size; |
| 121 | |
| 122 | flash->spi->rw = SPI_WRITE_FLAG; |
| 123 | ret = spi_claim_bus(flash->spi); |
| 124 | if (ret) { |
| 125 | printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n"); |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | for (actual = 0; actual < len; actual += chunk_len) { |
| 130 | chunk_len = min(len - actual, page_size - byte_addr); |
| 131 | chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); |
| 132 | |
| 133 | cmd[0] = CMD_AT25_PP; |
| 134 | cmd[1] = (offset >> 16) & 0xff; |
| 135 | cmd[2] = (offset >> 8) & 0xff; |
| 136 | cmd[3] = offset & 0xff; |
| 137 | #if CONFIG_DEBUG_SPI_FLASH |
| 138 | printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }" |
| 139 | " chunk_len = %zu\n", buf + actual, |
| 140 | cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
| 141 | #endif |
| 142 | |
| 143 | ret = spi_flash_cmd(flash->spi, CMD_AT25_WREN, NULL, 0); |
| 144 | if (ret < 0) { |
| 145 | printk(BIOS_WARNING, "SF: Enabling Write failed\n"); |
| 146 | goto out; |
| 147 | } |
| 148 | |
| 149 | ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), |
| 150 | buf + actual, chunk_len); |
| 151 | if (ret < 0) { |
| 152 | printk(BIOS_WARNING, "SF: Atmel Page Program failed\n"); |
| 153 | goto out; |
| 154 | } |
| 155 | |
| 156 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 157 | if (ret) |
| 158 | goto out; |
| 159 | |
| 160 | offset += chunk_len; |
| 161 | byte_addr = 0; |
| 162 | } |
| 163 | |
| 164 | #if CONFIG_DEBUG_SPI_FLASH |
| 165 | printk(BIOS_SPEW, "SF: Atmel: Successfully programmed %zu bytes @" |
| 166 | " 0x%lx\n", len, (unsigned long)(offset - len)); |
| 167 | #endif |
| 168 | ret = 0; |
| 169 | |
| 170 | out: |
| 171 | spi_release_bus(flash->spi); |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | static int atmel_erase(struct spi_flash *flash, u32 offset, size_t len) |
| 176 | { |
| 177 | return spi_flash_cmd_erase(flash, CMD_AT25_SE, offset, len); |
| 178 | } |
| 179 | |
| 180 | struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode) |
| 181 | { |
| 182 | const struct atmel_spi_flash_params *params; |
| 183 | unsigned page_size; |
| 184 | struct atmel_spi_flash *stm; |
| 185 | unsigned int i; |
| 186 | |
| 187 | for (i = 0; i < ARRAY_SIZE(atmel_spi_flash_table); i++) { |
| 188 | params = &atmel_spi_flash_table[i]; |
| 189 | if (params->id == ((idcode[1] << 8) | idcode[2])) |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | if (i == ARRAY_SIZE(atmel_spi_flash_table)) { |
| 194 | printk(BIOS_WARNING, "SF: Unsupported Atmel ID %02x%02x\n", |
| 195 | idcode[1], idcode[2]); |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | stm = malloc(sizeof(struct atmel_spi_flash)); |
| 200 | if (!stm) { |
| 201 | printk(BIOS_WARNING, "SF: Failed to allocate memory\n"); |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | stm->params = params; |
| 206 | stm->flash.spi = spi; |
| 207 | stm->flash.name = params->name; |
| 208 | |
| 209 | /* Assuming power-of-two page size initially. */ |
| 210 | page_size = 1 << params->l2_page_size; |
| 211 | |
| 212 | stm->flash.write = atmel_write; |
| 213 | stm->flash.erase = atmel_erase; |
| 214 | #if CONFIG_SPI_FLASH_NO_FAST_READ |
| 215 | stm->flash.read = spi_flash_cmd_read_slow; |
| 216 | #else |
| 217 | stm->flash.read = spi_flash_cmd_read_fast; |
| 218 | #endif |
| 219 | stm->flash.sector_size = (1 << stm->params->l2_page_size) * |
| 220 | stm->params->pages_per_sector; |
| 221 | stm->flash.size = page_size * params->pages_per_sector |
| 222 | * params->sectors_per_block |
| 223 | * params->nr_blocks; |
| 224 | |
| 225 | return &stm->flash; |
| 226 | } |