blob: e53889205db45e5ef98aff5145527b29f06efb97 [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
Furquan Shaikhc28984d2016-11-20 21:04:00 -08009#include <console/console.h>
Kyösti Mälkki96d92762014-11-11 15:04:38 +020010#include <stdlib.h>
11#include <spi_flash.h>
Furquan Shaikhc28984d2016-11-20 21:04:00 -080012#include <spi-generic.h>
Kyösti Mälkki96d92762014-11-11 15:04:38 +020013#include "spi_flash_internal.h"
14
15/* M25Pxx-specific commands */
16#define CMD_AT25_WREN 0x06 /* Write Enable */
17#define CMD_AT25_WRDI 0x04 /* Write Disable */
18#define CMD_AT25_RDSR 0x05 /* Read Status Register */
19#define CMD_AT25_WRSR 0x01 /* Write Status Register */
20#define CMD_AT25_READ 0x03 /* Read Data Bytes */
21#define CMD_AT25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
22#define CMD_AT25_PP 0x02 /* Page Program */
23#define CMD_AT25_SE 0x20 /* Sector (4K) Erase */
24#define CMD_AT25_BE 0xd8 /* Block (64K) Erase */
25#define CMD_AT25_CE 0xc7 /* Chip Erase */
26#define CMD_AT25_DP 0xb9 /* Deep Power-down */
27#define CMD_AT25_RES 0xab /* Release from DP, and Read Signature */
28
29struct atmel_spi_flash_params {
30 uint16_t id;
31 /* Log2 of page size in power-of-two mode */
32 uint8_t l2_page_size;
33 uint16_t pages_per_sector;
34 uint16_t sectors_per_block;
35 uint16_t nr_blocks;
36 const char *name;
37};
38
39/* spi_flash needs to be first so upper layers can free() it */
40struct atmel_spi_flash {
41 struct spi_flash flash;
42 const struct atmel_spi_flash_params *params;
43};
44
45static inline struct atmel_spi_flash *
Furquan Shaikhc28984d2016-11-20 21:04:00 -080046to_atmel_spi_flash(const struct spi_flash *flash)
Kyösti Mälkki96d92762014-11-11 15:04:38 +020047{
48 return container_of(flash, struct atmel_spi_flash, flash);
49}
50
51static const struct atmel_spi_flash_params atmel_spi_flash_table[] = {
52 {
53 .id = 0x3015,
54 .l2_page_size = 8,
55 .pages_per_sector = 16,
56 .sectors_per_block = 16,
57 .nr_blocks = 32,
58 .name = "AT25X16",
59 },
60 {
61 .id = 0x47,
62 .l2_page_size = 8,
63 .pages_per_sector = 16,
64 .sectors_per_block = 16,
65 .nr_blocks = 64,
66 .name = "AT25DF32",
67 },
68 {
69 .id = 0x3017,
70 .l2_page_size = 8,
71 .pages_per_sector = 16,
72 .sectors_per_block = 16,
73 .nr_blocks = 128,
74 .name = "AT25X64",
75 },
76 {
77 .id = 0x4015,
78 .l2_page_size = 8,
79 .pages_per_sector = 16,
80 .sectors_per_block = 16,
81 .nr_blocks = 32,
82 .name = "AT25Q16",
83 },
84 {
85 .id = 0x4016,
86 .l2_page_size = 8,
87 .pages_per_sector = 16,
88 .sectors_per_block = 16,
89 .nr_blocks = 64,
90 .name = "AT25Q32",
91 },
92 {
93 .id = 0x4017,
94 .l2_page_size = 8,
95 .pages_per_sector = 16,
96 .sectors_per_block = 16,
97 .nr_blocks = 128,
98 .name = "AT25Q64",
99 },
100 {
101 .id = 0x4018,
102 .l2_page_size = 8,
103 .pages_per_sector = 16,
104 .sectors_per_block = 16,
105 .nr_blocks = 256,
106 .name = "AT25Q128",
107 },
108};
109
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800110static int atmel_write(const struct spi_flash *flash, u32 offset, size_t len,
111 const void *buf)
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200112{
113 struct atmel_spi_flash *stm = to_atmel_spi_flash(flash);
114 unsigned long byte_addr;
115 unsigned long page_size;
116 size_t chunk_len;
117 size_t actual;
118 int ret;
119 u8 cmd[4];
120
121 page_size = 1 << stm->params->l2_page_size;
122 byte_addr = offset % page_size;
123
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200124 for (actual = 0; actual < len; actual += chunk_len) {
125 chunk_len = min(len - actual, page_size - byte_addr);
126 chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
127
128 cmd[0] = CMD_AT25_PP;
129 cmd[1] = (offset >> 16) & 0xff;
130 cmd[2] = (offset >> 8) & 0xff;
131 cmd[3] = offset & 0xff;
132#if CONFIG_DEBUG_SPI_FLASH
133 printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }"
134 " chunk_len = %zu\n", buf + actual,
135 cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
136#endif
137
138 ret = spi_flash_cmd(flash->spi, CMD_AT25_WREN, NULL, 0);
139 if (ret < 0) {
140 printk(BIOS_WARNING, "SF: Enabling Write failed\n");
141 goto out;
142 }
143
144 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd),
145 buf + actual, chunk_len);
146 if (ret < 0) {
147 printk(BIOS_WARNING, "SF: Atmel Page Program failed\n");
148 goto out;
149 }
150
151 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
152 if (ret)
153 goto out;
154
155 offset += chunk_len;
156 byte_addr = 0;
157 }
158
159#if CONFIG_DEBUG_SPI_FLASH
160 printk(BIOS_SPEW, "SF: Atmel: Successfully programmed %zu bytes @"
161 " 0x%lx\n", len, (unsigned long)(offset - len));
162#endif
163 ret = 0;
164
165out:
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200166 return ret;
167}
168
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200169struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode)
170{
171 const struct atmel_spi_flash_params *params;
172 unsigned page_size;
173 struct atmel_spi_flash *stm;
174 unsigned int i;
175
176 for (i = 0; i < ARRAY_SIZE(atmel_spi_flash_table); i++) {
177 params = &atmel_spi_flash_table[i];
178 if (params->id == ((idcode[1] << 8) | idcode[2]))
179 break;
180 }
181
182 if (i == ARRAY_SIZE(atmel_spi_flash_table)) {
183 printk(BIOS_WARNING, "SF: Unsupported Atmel ID %02x%02x\n",
184 idcode[1], idcode[2]);
185 return NULL;
186 }
187
188 stm = malloc(sizeof(struct atmel_spi_flash));
189 if (!stm) {
190 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
191 return NULL;
192 }
193
194 stm->params = params;
195 stm->flash.spi = spi;
196 stm->flash.name = params->name;
197
198 /* Assuming power-of-two page size initially. */
199 page_size = 1 << params->l2_page_size;
200
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800201 stm->flash.internal_write = atmel_write;
202 stm->flash.internal_erase = spi_flash_cmd_erase;
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200203#if CONFIG_SPI_FLASH_NO_FAST_READ
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800204 stm->flash.internal_read = spi_flash_cmd_read_slow;
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200205#else
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800206 stm->flash.internal_read = spi_flash_cmd_read_fast;
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200207#endif
208 stm->flash.sector_size = (1 << stm->params->l2_page_size) *
209 stm->params->pages_per_sector;
210 stm->flash.size = page_size * params->pages_per_sector
211 * params->sectors_per_block
212 * params->nr_blocks;
Dan Ehrenberga5aac762015-01-08 10:29:19 -0800213 stm->flash.erase_cmd = CMD_AT25_SE;
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200214
215 return &stm->flash;
216}