Idwer Vollering | 73a1018 | 2014-02-16 00:32:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Idwer Vollering <vidwer@gmail.com> |
| 3 | * |
| 4 | * Based on winbond.c |
| 5 | * |
| 6 | * Copyright 2008, Network Appliance Inc. |
| 7 | * Author: Jason McMullan <mcmullan <at> netapp.com> |
| 8 | * Licensed under the GPL-2 or later. |
| 9 | */ |
| 10 | |
| 11 | #include <stdlib.h> |
| 12 | #include <spi_flash.h> |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame^] | 13 | |
Idwer Vollering | 73a1018 | 2014-02-16 00:32:13 +0000 | [diff] [blame] | 14 | #include "spi_flash_internal.h" |
| 15 | |
| 16 | /* A25L-specific commands */ |
| 17 | #define CMD_A25_WREN 0x06 /* Write Enable */ |
| 18 | #define CMD_A25_WRDI 0x04 /* Write Disable */ |
| 19 | #define CMD_A25_RDSR 0x05 /* Read Status Register */ |
| 20 | #define CMD_A25_WRSR 0x01 /* Write Status Register */ |
| 21 | #define CMD_A25_READ 0x03 /* Read Data Bytes */ |
| 22 | #define CMD_A25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 23 | #define CMD_A25_PP 0x02 /* Page Program */ |
| 24 | #define CMD_A25_SE 0x20 /* Sector (4K) Erase */ |
| 25 | #define CMD_A25_BE 0xd8 /* Block (64K) Erase */ |
| 26 | #define CMD_A25_CE 0xc7 /* Chip Erase */ |
| 27 | #define CMD_A25_DP 0xb9 /* Deep Power-down */ |
| 28 | #define CMD_A25_RES 0xab /* Release from DP, and Read Signature */ |
| 29 | |
| 30 | struct amic_spi_flash_params { |
| 31 | uint16_t id; |
| 32 | /* Log2 of page size in power-of-two mode */ |
| 33 | uint8_t l2_page_size; |
| 34 | uint16_t pages_per_sector; |
| 35 | uint16_t sectors_per_block; |
| 36 | uint16_t nr_blocks; |
| 37 | const char *name; |
| 38 | }; |
| 39 | |
| 40 | /* spi_flash needs to be first so upper layers can free() it */ |
| 41 | struct amic_spi_flash { |
| 42 | struct spi_flash flash; |
| 43 | const struct amic_spi_flash_params *params; |
| 44 | }; |
| 45 | |
| 46 | static inline struct amic_spi_flash * |
| 47 | to_amic_spi_flash(struct spi_flash *flash) |
| 48 | { |
| 49 | return container_of(flash, struct amic_spi_flash, flash); |
| 50 | } |
| 51 | |
| 52 | static const struct amic_spi_flash_params amic_spi_flash_table[] = { |
| 53 | { |
| 54 | .id = 0x3016, |
| 55 | .l2_page_size = 8, |
| 56 | .pages_per_sector = 16, |
| 57 | .sectors_per_block = 16, |
| 58 | .nr_blocks = 64, |
| 59 | .name = "A25L032", |
| 60 | }, |
| 61 | }; |
| 62 | |
| 63 | static int amic_write(struct spi_flash *flash, |
| 64 | u32 offset, size_t len, const void *buf) |
| 65 | { |
| 66 | struct amic_spi_flash *amic = to_amic_spi_flash(flash); |
| 67 | unsigned long byte_addr; |
| 68 | unsigned long page_size; |
| 69 | size_t chunk_len; |
| 70 | size_t actual; |
| 71 | int ret; |
| 72 | u8 cmd[4]; |
| 73 | |
Kyösti Mälkki | 77d1280 | 2014-06-29 16:15:39 +0300 | [diff] [blame] | 74 | page_size = 1 << amic->params->l2_page_size; |
Idwer Vollering | 73a1018 | 2014-02-16 00:32:13 +0000 | [diff] [blame] | 75 | byte_addr = offset % page_size; |
| 76 | |
| 77 | flash->spi->rw = SPI_WRITE_FLAG; |
| 78 | ret = spi_claim_bus(flash->spi); |
| 79 | if (ret) { |
| 80 | printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n"); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | for (actual = 0; actual < len; actual += chunk_len) { |
| 85 | chunk_len = min(len - actual, page_size - byte_addr); |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 86 | chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); |
Idwer Vollering | 73a1018 | 2014-02-16 00:32:13 +0000 | [diff] [blame] | 87 | |
| 88 | cmd[0] = CMD_A25_PP; |
| 89 | cmd[1] = (offset >> 16) & 0xff; |
| 90 | cmd[2] = (offset >> 8) & 0xff; |
| 91 | cmd[3] = offset & 0xff; |
| 92 | #if CONFIG_DEBUG_SPI_FLASH |
| 93 | printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }" |
| 94 | " chunk_len = %zu\n", buf + actual, |
| 95 | cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
| 96 | #endif |
| 97 | |
| 98 | ret = spi_flash_cmd(flash->spi, CMD_A25_WREN, NULL, 0); |
| 99 | if (ret < 0) { |
| 100 | printk(BIOS_WARNING, "SF: Enabling Write failed\n"); |
| 101 | goto out; |
| 102 | } |
| 103 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 104 | ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), |
Idwer Vollering | 73a1018 | 2014-02-16 00:32:13 +0000 | [diff] [blame] | 105 | buf + actual, chunk_len); |
| 106 | if (ret < 0) { |
| 107 | printk(BIOS_WARNING, "SF: AMIC Page Program failed\n"); |
| 108 | goto out; |
| 109 | } |
| 110 | |
| 111 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 112 | if (ret) |
| 113 | goto out; |
| 114 | |
| 115 | offset += chunk_len; |
| 116 | byte_addr = 0; |
| 117 | } |
| 118 | |
| 119 | #if CONFIG_DEBUG_SPI_FLASH |
| 120 | printk(BIOS_SPEW, "SF: AMIC: Successfully programmed %zu bytes @" |
| 121 | " 0x%lx\n", len, (unsigned long)(offset - len)); |
| 122 | #endif |
| 123 | ret = 0; |
| 124 | |
| 125 | out: |
| 126 | spi_release_bus(flash->spi); |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | static int amic_erase(struct spi_flash *flash, u32 offset, size_t len) |
| 131 | { |
| 132 | return spi_flash_cmd_erase(flash, CMD_A25_SE, offset, len); |
| 133 | } |
| 134 | |
| 135 | struct spi_flash *spi_flash_probe_amic(struct spi_slave *spi, u8 *idcode) |
| 136 | { |
| 137 | const struct amic_spi_flash_params *params; |
| 138 | unsigned page_size; |
| 139 | struct amic_spi_flash *amic; |
| 140 | unsigned int i; |
| 141 | |
| 142 | for (i = 0; i < ARRAY_SIZE(amic_spi_flash_table); i++) { |
| 143 | params = &amic_spi_flash_table[i]; |
| 144 | if (params->id == ((idcode[1] << 8) | idcode[2])) |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | if (i == ARRAY_SIZE(amic_spi_flash_table)) { |
| 149 | printk(BIOS_WARNING, "SF: Unsupported AMIC ID %02x%02x\n", |
| 150 | idcode[1], idcode[2]); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | amic = malloc(sizeof(struct amic_spi_flash)); |
| 155 | if (!amic) { |
| 156 | printk(BIOS_WARNING, "SF: Failed to allocate memory\n"); |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | amic->params = params; |
| 161 | amic->flash.spi = spi; |
| 162 | amic->flash.name = params->name; |
| 163 | |
| 164 | /* Assuming power-of-two page size initially. */ |
| 165 | page_size = 1 << params->l2_page_size; |
| 166 | |
| 167 | amic->flash.write = amic_write; |
| 168 | amic->flash.erase = amic_erase; |
| 169 | #if CONFIG_SPI_FLASH_NO_FAST_READ |
| 170 | amic->flash.read = spi_flash_cmd_read_slow; |
| 171 | #else |
| 172 | amic->flash.read = spi_flash_cmd_read_fast; |
| 173 | #endif |
| 174 | amic->flash.sector_size = (1 << amic->params->l2_page_size) * |
| 175 | amic->params->pages_per_sector; |
| 176 | amic->flash.size = page_size * params->pages_per_sector |
| 177 | * params->sectors_per_block |
| 178 | * params->nr_blocks; |
| 179 | |
| 180 | return &amic->flash; |
| 181 | } |