Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2010, ucRobotics Inc. |
| 3 | * Author: Chong Huang <chuang@ucrobotics.com> |
| 4 | * Licensed under the GPL-2 or later. |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <malloc.h> |
| 9 | #include <spi_flash.h> |
| 10 | |
| 11 | #include "spi_flash_internal.h" |
| 12 | |
| 13 | /* EN25Q128-specific commands */ |
| 14 | #define CMD_EN25Q128_WREN 0x06 /* Write Enable */ |
| 15 | #define CMD_EN25Q128_WRDI 0x04 /* Write Disable */ |
| 16 | #define CMD_EN25Q128_RDSR 0x05 /* Read Status Register */ |
| 17 | #define CMD_EN25Q128_WRSR 0x01 /* Write Status Register */ |
| 18 | #define CMD_EN25Q128_READ 0x03 /* Read Data Bytes */ |
| 19 | #define CMD_EN25Q128_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 20 | #define CMD_EN25Q128_PP 0x02 /* Page Program */ |
| 21 | #define CMD_EN25Q128_SE 0x20 /* Sector Erase */ |
| 22 | #define CMD_EN25Q128_BE 0xd8 /* Block Erase */ |
| 23 | #define CMD_EN25Q128_DP 0xb9 /* Deep Power-down */ |
| 24 | #define CMD_EN25Q128_RES 0xab /* Release from DP, and Read Signature */ |
| 25 | |
| 26 | #define EON_ID_EN25Q128 0x18 |
| 27 | |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 28 | struct eon_spi_flash_params { |
| 29 | u8 idcode1; |
| 30 | u16 page_size; |
| 31 | u16 pages_per_sector; |
| 32 | u16 sectors_per_block; |
| 33 | u16 nr_sectors; |
| 34 | const char *name; |
| 35 | }; |
| 36 | |
| 37 | /* spi_flash needs to be first so upper layers can free() it */ |
| 38 | struct eon_spi_flash { |
| 39 | struct spi_flash flash; |
| 40 | const struct eon_spi_flash_params *params; |
| 41 | }; |
| 42 | |
| 43 | static inline struct eon_spi_flash *to_eon_spi_flash(struct spi_flash *flash) |
| 44 | { |
| 45 | return container_of(flash, struct eon_spi_flash, flash); |
| 46 | } |
| 47 | |
| 48 | static const struct eon_spi_flash_params eon_spi_flash_table[] = { |
| 49 | { |
| 50 | .idcode1 = EON_ID_EN25Q128, |
| 51 | .page_size = 256, |
| 52 | .pages_per_sector = 16, |
| 53 | .sectors_per_block = 16, |
| 54 | .nr_sectors = 4096, |
| 55 | .name = "EN25Q128", |
| 56 | }, |
| 57 | }; |
| 58 | |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 59 | static int eon_read_fast(struct spi_flash *flash, |
| 60 | u32 offset, size_t len, void *buf) |
| 61 | { |
| 62 | struct eon_spi_flash *eon = to_eon_spi_flash(flash); |
| 63 | unsigned long page_addr; |
| 64 | unsigned long page_size; |
| 65 | u8 cmd[5]; |
| 66 | |
| 67 | page_size = eon->params->page_size; |
| 68 | page_addr = offset / page_size; |
| 69 | |
| 70 | cmd[0] = CMD_READ_ARRAY_FAST; |
| 71 | cmd[1] = page_addr >> 8; |
| 72 | cmd[2] = page_addr; |
| 73 | cmd[3] = offset % page_size; |
| 74 | cmd[4] = 0x00; |
| 75 | |
| 76 | return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len); |
| 77 | } |
| 78 | |
| 79 | static int eon_write(struct spi_flash *flash, |
| 80 | u32 offset, size_t len, const void *buf) |
| 81 | { |
| 82 | struct eon_spi_flash *eon = to_eon_spi_flash(flash); |
| 83 | unsigned long page_addr; |
| 84 | unsigned long byte_addr; |
| 85 | unsigned long page_size; |
| 86 | size_t chunk_len; |
| 87 | size_t actual; |
| 88 | int ret; |
| 89 | u8 cmd[4]; |
| 90 | |
| 91 | page_size = eon->params->page_size; |
| 92 | page_addr = offset / page_size; |
| 93 | byte_addr = offset % page_size; |
| 94 | |
| 95 | ret = spi_claim_bus(flash->spi); |
| 96 | if (ret) { |
| 97 | debug("SF: Unable to claim SPI bus\n"); |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | ret = 0; |
| 102 | for (actual = 0; actual < len; actual += chunk_len) { |
| 103 | chunk_len = min(len - actual, page_size - byte_addr); |
| 104 | |
| 105 | cmd[0] = CMD_EN25Q128_PP; |
| 106 | cmd[1] = page_addr >> 8; |
| 107 | cmd[2] = page_addr; |
| 108 | cmd[3] = byte_addr; |
| 109 | |
| 110 | debug |
| 111 | ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n", |
| 112 | buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
| 113 | |
| 114 | ret = spi_flash_cmd(flash->spi, CMD_EN25Q128_WREN, NULL, 0); |
| 115 | if (ret < 0) { |
| 116 | debug("SF: Enabling Write failed\n"); |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | ret = spi_flash_cmd_write(flash->spi, cmd, 4, |
| 121 | buf + actual, chunk_len); |
| 122 | if (ret < 0) { |
| 123 | debug("SF: EON Page Program failed\n"); |
| 124 | break; |
| 125 | } |
| 126 | |
Mike Frysinger | 6163045 | 2011-01-10 02:20:12 -0500 | [diff] [blame^] | 127 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 128 | if (ret) |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 129 | break; |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 130 | |
| 131 | page_addr++; |
| 132 | byte_addr = 0; |
| 133 | } |
| 134 | |
| 135 | debug("SF: EON: Successfully programmed %u bytes @ 0x%x\n", |
| 136 | len, offset); |
| 137 | |
| 138 | spi_release_bus(flash->spi); |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | int eon_erase(struct spi_flash *flash, u32 offset, size_t len) |
| 143 | { |
| 144 | /* block erase */ |
| 145 | struct eon_spi_flash *eon = to_eon_spi_flash(flash); |
| 146 | unsigned long block_size; |
| 147 | size_t actual; |
| 148 | int ret; |
| 149 | u8 cmd[4]; |
| 150 | |
| 151 | |
| 152 | block_size = eon->params->page_size * eon->params->pages_per_sector |
| 153 | * eon->params->sectors_per_block; |
| 154 | |
| 155 | if (offset % block_size || len % block_size) { |
| 156 | debug("SF: Erase offset/length not multiple of block size\n"); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | len /= block_size; |
| 161 | cmd[0] = CMD_EN25Q128_BE; |
| 162 | cmd[2] = 0x00; |
| 163 | cmd[3] = 0x00; |
| 164 | |
| 165 | ret = spi_claim_bus(flash->spi); |
| 166 | if (ret) { |
| 167 | debug("SF: Unable to claim SPI bus\n"); |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | ret = 0; |
| 172 | for (actual = 0; actual < len; actual++) { |
| 173 | cmd[1] = (offset / block_size) + actual; |
| 174 | ret = spi_flash_cmd(flash->spi, CMD_EN25Q128_WREN, NULL, 0); |
| 175 | if (ret < 0) { |
| 176 | debug("SF: Enabling Write failed\n"); |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0); |
| 181 | if (ret < 0) { |
| 182 | debug("SF: EON page erase failed\n"); |
| 183 | break; |
| 184 | } |
| 185 | |
Mike Frysinger | 6163045 | 2011-01-10 02:20:12 -0500 | [diff] [blame^] | 186 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT); |
| 187 | if (ret) |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 188 | break; |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | debug("SF: EON: Successfully erased %u bytes @ 0x%x\n", |
| 192 | len * block_size, offset); |
| 193 | |
| 194 | spi_release_bus(flash->spi); |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode) |
| 199 | { |
| 200 | const struct eon_spi_flash_params *params; |
| 201 | struct eon_spi_flash *eon; |
| 202 | unsigned int i; |
| 203 | |
| 204 | for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) { |
| 205 | params = &eon_spi_flash_table[i]; |
| 206 | if (params->idcode1 == idcode[2]) |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | if (i == ARRAY_SIZE(eon_spi_flash_table)) { |
| 211 | debug("SF: Unsupported EON ID %02x\n", idcode[1]); |
| 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | eon = malloc(sizeof(*eon)); |
| 216 | if (!eon) { |
| 217 | debug("SF: Failed to allocate memory\n"); |
| 218 | return NULL; |
| 219 | } |
| 220 | |
| 221 | eon->params = params; |
| 222 | eon->flash.spi = spi; |
| 223 | eon->flash.name = params->name; |
| 224 | |
| 225 | eon->flash.write = eon_write; |
| 226 | eon->flash.erase = eon_erase; |
| 227 | eon->flash.read = eon_read_fast; |
| 228 | eon->flash.size = params->page_size * params->pages_per_sector |
| 229 | * params->nr_sectors; |
| 230 | |
| 231 | debug("SF: Detected %s with page size %u, total %u bytes\n", |
| 232 | params->name, params->page_size, eon->flash.size); |
| 233 | |
| 234 | return &eon->flash; |
| 235 | } |