2 * This file is part of the coreboot project.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 * Driver for Adesto Technologies SPI flash
20 #include <console/console.h>
21 #include <commonlib/helpers.h>
23 #include <spi_flash.h>
24 #include <spi-generic.h>
26 #include "spi_flash_internal.h"
28 /* at25dfxx-specific commands */
29 #define CMD_AT25DF_WREN 0x06 /* Write Enable */
30 #define CMD_AT25DF_WRDI 0x04 /* Write Disable */
31 #define CMD_AT25DF_RDSR 0x05 /* Read Status Register */
32 #define CMD_AT25DF_WRSR 0x01 /* Write Status Register */
33 #define CMD_AT25DF_READ 0x03 /* Read Data Bytes */
34 #define CMD_AT25DF_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
35 #define CMD_AT25DF_PP 0x02 /* Page Program */
36 #define CMD_AT25DF_SE 0x20 /* Sector (4K) Erase */
37 #define CMD_AT25DF_BE 0xd8 /* Block (64K) Erase */
38 #define CMD_AT25DF_CE 0xc7 /* Chip Erase */
39 #define CMD_AT25DF_DP 0xb9 /* Deep Power-down */
40 #define CMD_AT25DF_RES 0xab /* Release from DP, and Read Signature */
42 struct adesto_spi_flash_params
{
44 /* Log2 of page size in power-of-two mode */
46 uint16_t pages_per_sector
;
47 uint16_t sectors_per_block
;
52 static const struct adesto_spi_flash_params adesto_spi_flash_table
[] = {
56 .pages_per_sector
= 16,
57 .sectors_per_block
= 16,
64 .pages_per_sector
= 16,
65 .sectors_per_block
= 16,
67 .name
= "AT25DF081A", /* Yes, 81A id < 81 */
72 .pages_per_sector
= 16,
73 .sectors_per_block
= 16,
80 .pages_per_sector
= 16,
81 .sectors_per_block
= 16,
88 .pages_per_sector
= 16,
89 .sectors_per_block
= 16,
96 .pages_per_sector
= 16,
97 .sectors_per_block
= 16,
104 .pages_per_sector
= 16,
105 .sectors_per_block
= 16,
107 .name
= "AT25DF321A",
112 .pages_per_sector
= 16,
113 .sectors_per_block
= 16,
120 .pages_per_sector
= 16,
121 .sectors_per_block
= 16,
128 .pages_per_sector
= 16,
129 .sectors_per_block
= 16,
136 .pages_per_sector
= 16,
137 .sectors_per_block
= 16,
144 .pages_per_sector
= 16,
145 .sectors_per_block
= 16,
151 static int adesto_write(const struct spi_flash
*flash
, u32 offset
, size_t len
,
154 unsigned long byte_addr
;
155 unsigned long page_size
;
161 page_size
= flash
->page_size
;
163 for (actual
= 0; actual
< len
; actual
+= chunk_len
) {
164 byte_addr
= offset
% page_size
;
165 chunk_len
= MIN(len
- actual
, page_size
- byte_addr
);
166 chunk_len
= spi_crop_chunk(&flash
->spi
, sizeof(cmd
), chunk_len
);
168 cmd
[0] = CMD_AT25DF_PP
;
169 cmd
[1] = (offset
>> 16) & 0xff;
170 cmd
[2] = (offset
>> 8) & 0xff;
171 cmd
[3] = offset
& 0xff;
172 #if CONFIG(DEBUG_SPI_FLASH)
173 printk(BIOS_SPEW
, "PP: %p => cmd = { 0x%02x 0x%02x%02x%02x }"
174 " chunk_len = %zu\n", buf
+ actual
,
175 cmd
[0], cmd
[1], cmd
[2], cmd
[3], chunk_len
);
178 ret
= spi_flash_cmd(&flash
->spi
, CMD_AT25DF_WREN
, NULL
, 0);
180 printk(BIOS_WARNING
, "SF: Enabling Write failed\n");
184 ret
= spi_flash_cmd_write(&flash
->spi
, cmd
, sizeof(cmd
),
185 buf
+ actual
, chunk_len
);
187 printk(BIOS_WARNING
, "SF: adesto Page Program failed\n");
191 ret
= spi_flash_cmd_wait_ready(flash
,
192 SPI_FLASH_PROG_TIMEOUT_MS
);
199 #if CONFIG(DEBUG_SPI_FLASH)
200 printk(BIOS_SPEW
, "SF: adesto: Successfully programmed %zu bytes @"
201 " 0x%lx\n", len
, (unsigned long)(offset
- len
));
209 static const struct spi_flash_ops spi_flash_ops
= {
210 .write
= adesto_write
,
211 .erase
= spi_flash_cmd_erase
,
214 int spi_flash_probe_adesto(const struct spi_slave
*spi
, u8
*idcode
,
215 struct spi_flash
*flash
)
217 const struct adesto_spi_flash_params
*params
;
220 for (i
= 0; i
< ARRAY_SIZE(adesto_spi_flash_table
); i
++) {
221 params
= &adesto_spi_flash_table
[i
];
222 if (params
->id
== ((idcode
[1] << 8) | idcode
[2]))
226 if (i
== ARRAY_SIZE(adesto_spi_flash_table
)) {
227 printk(BIOS_WARNING
, "SF: Unsupported adesto ID %02x%02x\n",
228 idcode
[1], idcode
[2]);
232 memcpy(&flash
->spi
, spi
, sizeof(*spi
));
233 flash
->name
= params
->name
;
234 /* Assuming power-of-two page size initially. */
235 flash
->page_size
= 1 << params
->l2_page_size
;
236 flash
->sector_size
= flash
->page_size
* params
->pages_per_sector
;
237 flash
->size
= flash
->sector_size
*params
->sectors_per_block
*
239 flash
->erase_cmd
= CMD_AT25DF_SE
;
241 flash
->ops
= &spi_flash_ops
;