Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 1 | /* |
| 2 | * adesto.c |
| 3 | * Driver for Adesto Technologies SPI flash |
| 4 | * Copyright 2014 Orion Technologies, LLC |
| 5 | * Author: Chris Douglass <cdouglass <at> oriontechnologies.com> |
| 6 | * |
| 7 | * based on winbond.c |
| 8 | * Copyright 2008, Network Appliance Inc. |
| 9 | * Author: Jason McMullan <mcmullan <at> netapp.com> |
| 10 | * Licensed under the GPL-2 or later. |
| 11 | */ |
| 12 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 13 | #include <console/console.h> |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 14 | #include <stdlib.h> |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 15 | #include <string.h> |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 16 | #include <spi_flash.h> |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 17 | #include <spi-generic.h> |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 18 | |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 19 | #include "spi_flash_internal.h" |
| 20 | |
| 21 | /* at25dfxx-specific commands */ |
| 22 | #define CMD_AT25DF_WREN 0x06 /* Write Enable */ |
| 23 | #define CMD_AT25DF_WRDI 0x04 /* Write Disable */ |
| 24 | #define CMD_AT25DF_RDSR 0x05 /* Read Status Register */ |
| 25 | #define CMD_AT25DF_WRSR 0x01 /* Write Status Register */ |
| 26 | #define CMD_AT25DF_READ 0x03 /* Read Data Bytes */ |
| 27 | #define CMD_AT25DF_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 28 | #define CMD_AT25DF_PP 0x02 /* Page Program */ |
| 29 | #define CMD_AT25DF_SE 0x20 /* Sector (4K) Erase */ |
| 30 | #define CMD_AT25DF_BE 0xd8 /* Block (64K) Erase */ |
| 31 | #define CMD_AT25DF_CE 0xc7 /* Chip Erase */ |
| 32 | #define CMD_AT25DF_DP 0xb9 /* Deep Power-down */ |
| 33 | #define CMD_AT25DF_RES 0xab /* Release from DP, and Read Signature */ |
| 34 | |
| 35 | struct adesto_spi_flash_params { |
| 36 | uint16_t id; |
| 37 | /* Log2 of page size in power-of-two mode */ |
| 38 | uint8_t l2_page_size; |
| 39 | uint16_t pages_per_sector; |
| 40 | uint16_t sectors_per_block; |
| 41 | uint16_t nr_blocks; |
| 42 | const char *name; |
| 43 | }; |
| 44 | |
| 45 | /* spi_flash needs to be first so upper layers can free() it */ |
| 46 | struct adesto_spi_flash { |
| 47 | struct spi_flash flash; |
| 48 | const struct adesto_spi_flash_params *params; |
| 49 | }; |
| 50 | |
| 51 | static inline struct adesto_spi_flash * |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 52 | to_adesto_spi_flash(const struct spi_flash *flash) |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 53 | { |
| 54 | return container_of(flash, struct adesto_spi_flash, flash); |
| 55 | } |
| 56 | |
| 57 | static const struct adesto_spi_flash_params adesto_spi_flash_table[] = { |
| 58 | { |
| 59 | .id = 0x4501, |
| 60 | .l2_page_size = 8, |
| 61 | .pages_per_sector = 16, |
| 62 | .sectors_per_block = 16, |
| 63 | .nr_blocks = 16, |
| 64 | .name = "AT25DF081", |
| 65 | }, |
| 66 | { |
| 67 | .id = 0x4701, |
| 68 | .l2_page_size = 8, |
| 69 | .pages_per_sector = 16, |
| 70 | .sectors_per_block = 16, |
| 71 | .nr_blocks = 64, |
| 72 | .name = "AT25DF321", |
| 73 | }, |
| 74 | { |
| 75 | .id = 0x4800, |
| 76 | .l2_page_size = 8, |
| 77 | .pages_per_sector = 16, |
| 78 | .sectors_per_block = 16, |
| 79 | .nr_blocks = 128, |
| 80 | .name = "AT25DF641", |
| 81 | }, |
| 82 | }; |
| 83 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 84 | static int adesto_write(const struct spi_flash *flash, u32 offset, size_t len, |
| 85 | const void *buf) |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 86 | { |
| 87 | struct adesto_spi_flash *stm = to_adesto_spi_flash(flash); |
| 88 | unsigned long byte_addr; |
| 89 | unsigned long page_size; |
| 90 | size_t chunk_len; |
| 91 | size_t actual; |
| 92 | int ret; |
| 93 | u8 cmd[4]; |
| 94 | |
Kyösti Mälkki | 77d1280 | 2014-06-29 16:15:39 +0300 | [diff] [blame] | 95 | page_size = 1 << stm->params->l2_page_size; |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 96 | |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 97 | for (actual = 0; actual < len; actual += chunk_len) { |
Aaron Durbin | 41f6690 | 2016-12-17 13:16:07 -0600 | [diff] [blame^] | 98 | byte_addr = offset % page_size; |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 99 | chunk_len = min(len - actual, page_size - byte_addr); |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 100 | chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 101 | |
| 102 | cmd[0] = CMD_AT25DF_PP; |
| 103 | cmd[1] = (offset >> 16) & 0xff; |
| 104 | cmd[2] = (offset >> 8) & 0xff; |
| 105 | cmd[3] = offset & 0xff; |
| 106 | #if CONFIG_DEBUG_SPI_FLASH |
| 107 | printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }" |
| 108 | " chunk_len = %zu\n", buf + actual, |
| 109 | cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
| 110 | #endif |
| 111 | |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 112 | ret = spi_flash_cmd(&flash->spi, CMD_AT25DF_WREN, NULL, 0); |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 113 | if (ret < 0) { |
| 114 | printk(BIOS_WARNING, "SF: Enabling Write failed\n"); |
| 115 | goto out; |
| 116 | } |
| 117 | |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 118 | ret = spi_flash_cmd_write(&flash->spi, cmd, sizeof(cmd), |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 119 | buf + actual, chunk_len); |
| 120 | if (ret < 0) { |
| 121 | printk(BIOS_WARNING, "SF: adesto Page Program failed\n"); |
| 122 | goto out; |
| 123 | } |
| 124 | |
| 125 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 126 | if (ret) |
| 127 | goto out; |
| 128 | |
| 129 | offset += chunk_len; |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | #if CONFIG_DEBUG_SPI_FLASH |
| 133 | printk(BIOS_SPEW, "SF: adesto: Successfully programmed %zu bytes @" |
| 134 | " 0x%lx\n", len, (unsigned long)(offset - len)); |
| 135 | #endif |
| 136 | ret = 0; |
| 137 | |
| 138 | out: |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 139 | return ret; |
| 140 | } |
| 141 | |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 142 | struct spi_flash *spi_flash_probe_adesto(struct spi_slave *spi, u8 *idcode) |
| 143 | { |
| 144 | const struct adesto_spi_flash_params *params; |
| 145 | unsigned page_size; |
| 146 | struct adesto_spi_flash *stm; |
| 147 | unsigned int i; |
| 148 | |
| 149 | for (i = 0; i < ARRAY_SIZE(adesto_spi_flash_table); i++) { |
| 150 | params = &adesto_spi_flash_table[i]; |
| 151 | if (params->id == ((idcode[1] << 8) | idcode[2])) |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | if (i == ARRAY_SIZE(adesto_spi_flash_table)) { |
| 156 | printk(BIOS_WARNING, "SF: Unsupported adesto ID %02x%02x\n", |
| 157 | idcode[1], idcode[2]); |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | stm = malloc(sizeof(struct adesto_spi_flash)); |
| 162 | if (!stm) { |
| 163 | printk(BIOS_WARNING, "SF: Failed to allocate memory\n"); |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | stm->params = params; |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 168 | memcpy(&stm->flash.spi, spi, sizeof(*spi)); |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 169 | stm->flash.name = params->name; |
| 170 | |
| 171 | /* Assuming power-of-two page size initially. */ |
| 172 | page_size = 1 << params->l2_page_size; |
| 173 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 174 | stm->flash.internal_write = adesto_write; |
| 175 | stm->flash.internal_erase = spi_flash_cmd_erase; |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 176 | #if CONFIG_SPI_FLASH_NO_FAST_READ |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 177 | stm->flash.internal_read = spi_flash_cmd_read_slow; |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 178 | #else |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 179 | stm->flash.internal_read = spi_flash_cmd_read_fast; |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 180 | #endif |
| 181 | stm->flash.sector_size = (1 << stm->params->l2_page_size) * |
| 182 | stm->params->pages_per_sector; |
| 183 | stm->flash.size = page_size * params->pages_per_sector |
| 184 | * params->sectors_per_block |
| 185 | * params->nr_blocks; |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 186 | stm->flash.erase_cmd = CMD_AT25DF_SE; |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 187 | |
| 188 | return &stm->flash; |
| 189 | } |