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