blob: 0286e09cdc61172ca5336ac95e7cfb4d0943427f [file] [log] [blame]
Kyösti Mälkki96d92762014-11-11 15:04:38 +02001/*
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
27struct 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 */
38struct atmel_spi_flash {
39 struct spi_flash flash;
40 const struct atmel_spi_flash_params *params;
41};
42
43static inline struct atmel_spi_flash *
44to_atmel_spi_flash(struct spi_flash *flash)
45{
46 return container_of(flash, struct atmel_spi_flash, flash);
47}
48
49static 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
108static 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
170out:
171 spi_release_bus(flash->spi);
172 return ret;
173}
174
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200175struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode)
176{
177 const struct atmel_spi_flash_params *params;
178 unsigned page_size;
179 struct atmel_spi_flash *stm;
180 unsigned int i;
181
182 for (i = 0; i < ARRAY_SIZE(atmel_spi_flash_table); i++) {
183 params = &atmel_spi_flash_table[i];
184 if (params->id == ((idcode[1] << 8) | idcode[2]))
185 break;
186 }
187
188 if (i == ARRAY_SIZE(atmel_spi_flash_table)) {
189 printk(BIOS_WARNING, "SF: Unsupported Atmel ID %02x%02x\n",
190 idcode[1], idcode[2]);
191 return NULL;
192 }
193
194 stm = malloc(sizeof(struct atmel_spi_flash));
195 if (!stm) {
196 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
197 return NULL;
198 }
199
200 stm->params = params;
201 stm->flash.spi = spi;
202 stm->flash.name = params->name;
203
204 /* Assuming power-of-two page size initially. */
205 page_size = 1 << params->l2_page_size;
206
207 stm->flash.write = atmel_write;
Dan Ehrenberga5aac762015-01-08 10:29:19 -0800208 stm->flash.erase = spi_flash_cmd_erase;
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200209#if CONFIG_SPI_FLASH_NO_FAST_READ
210 stm->flash.read = spi_flash_cmd_read_slow;
211#else
212 stm->flash.read = spi_flash_cmd_read_fast;
213#endif
214 stm->flash.sector_size = (1 << stm->params->l2_page_size) *
215 stm->params->pages_per_sector;
216 stm->flash.size = page_size * params->pages_per_sector
217 * params->sectors_per_block
218 * params->nr_blocks;
Dan Ehrenberga5aac762015-01-08 10:29:19 -0800219 stm->flash.erase_cmd = CMD_AT25_SE;
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200220
221 return &stm->flash;
222}