2 * This file is part of the coreboot project.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (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.
15 #include <console/console.h>
16 #include <commonlib/helpers.h>
17 #include <spi_flash.h>
18 #include <spi-generic.h>
21 #include "spi_flash_internal.h"
23 /* M25Pxx-specific commands */
24 #define CMD_AT25_WREN 0x06 /* Write Enable */
25 #define CMD_AT25_WRDI 0x04 /* Write Disable */
26 #define CMD_AT25_RDSR 0x05 /* Read Status Register */
27 #define CMD_AT25_WRSR 0x01 /* Write Status Register */
28 #define CMD_AT25_READ 0x03 /* Read Data Bytes */
29 #define CMD_AT25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
30 #define CMD_AT25_PP 0x02 /* Page Program */
31 #define CMD_AT25_SE 0x20 /* Sector (4K) Erase */
32 #define CMD_AT25_BE 0xd8 /* Block (64K) Erase */
33 #define CMD_AT25_CE 0xc7 /* Chip Erase */
34 #define CMD_AT25_DP 0xb9 /* Deep Power-down */
35 #define CMD_AT25_RES 0xab /* Release from DP, and Read Signature */
37 struct atmel_spi_flash_params
{
39 /* Log2 of page size in power-of-two mode */
41 uint16_t pages_per_sector
;
42 uint16_t sectors_per_block
;
47 static const struct atmel_spi_flash_params atmel_spi_flash_table
[] = {
51 .pages_per_sector
= 16,
52 .sectors_per_block
= 16,
59 .pages_per_sector
= 16,
60 .sectors_per_block
= 16,
67 .pages_per_sector
= 16,
68 .sectors_per_block
= 16,
75 .pages_per_sector
= 16,
76 .sectors_per_block
= 16,
83 .pages_per_sector
= 16,
84 .sectors_per_block
= 16,
91 .pages_per_sector
= 16,
92 .sectors_per_block
= 16,
99 .pages_per_sector
= 16,
100 .sectors_per_block
= 16,
106 static int atmel_write(const struct spi_flash
*flash
, u32 offset
, size_t len
,
109 unsigned long byte_addr
;
110 unsigned long page_size
;
116 page_size
= flash
->page_size
;
118 for (actual
= 0; actual
< len
; actual
+= chunk_len
) {
119 byte_addr
= offset
% page_size
;
120 chunk_len
= MIN(len
- actual
, page_size
- byte_addr
);
121 chunk_len
= spi_crop_chunk(&flash
->spi
, sizeof(cmd
), chunk_len
);
123 cmd
[0] = CMD_AT25_PP
;
124 cmd
[1] = (offset
>> 16) & 0xff;
125 cmd
[2] = (offset
>> 8) & 0xff;
126 cmd
[3] = offset
& 0xff;
127 #if CONFIG(DEBUG_SPI_FLASH)
128 printk(BIOS_SPEW
, "PP: %p => cmd = { 0x%02x 0x%02x%02x%02x }"
129 " chunk_len = %zu\n", buf
+ actual
,
130 cmd
[0], cmd
[1], cmd
[2], cmd
[3], chunk_len
);
133 ret
= spi_flash_cmd(&flash
->spi
, CMD_AT25_WREN
, NULL
, 0);
135 printk(BIOS_WARNING
, "SF: Enabling Write failed\n");
139 ret
= spi_flash_cmd_write(&flash
->spi
, cmd
, sizeof(cmd
),
140 buf
+ actual
, chunk_len
);
142 printk(BIOS_WARNING
, "SF: Atmel Page Program failed\n");
146 ret
= spi_flash_cmd_wait_ready(flash
,
147 SPI_FLASH_PROG_TIMEOUT_MS
);
154 #if CONFIG(DEBUG_SPI_FLASH)
155 printk(BIOS_SPEW
, "SF: Atmel: Successfully programmed %zu bytes @"
156 " 0x%lx\n", len
, (unsigned long)(offset
- len
));
164 static const struct spi_flash_ops spi_flash_ops
= {
165 .write
= atmel_write
,
166 .erase
= spi_flash_cmd_erase
,
169 int spi_flash_probe_atmel(const struct spi_slave
*spi
, u8
*idcode
,
170 struct spi_flash
*flash
)
172 const struct atmel_spi_flash_params
*params
;
175 for (i
= 0; i
< ARRAY_SIZE(atmel_spi_flash_table
); i
++) {
176 params
= &atmel_spi_flash_table
[i
];
177 if (params
->id
== ((idcode
[1] << 8) | idcode
[2]))
181 if (i
== ARRAY_SIZE(atmel_spi_flash_table
)) {
182 printk(BIOS_WARNING
, "SF: Unsupported Atmel ID %02x%02x\n",
183 idcode
[1], idcode
[2]);
187 memcpy(&flash
->spi
, spi
, sizeof(*spi
));
188 flash
->name
= params
->name
;
190 /* Assuming power-of-two page size initially. */
191 flash
->page_size
= 1 << params
->l2_page_size
;
192 flash
->sector_size
= flash
->page_size
* params
->pages_per_sector
;
193 flash
->size
= flash
->sector_size
* params
->sectors_per_block
*
195 flash
->erase_cmd
= CMD_AT25_SE
;
197 flash
->ops
= &spi_flash_ops
;