{drivers,southbridge}: Replace min() with MIN()
[coreboot.git] / src / drivers / spi / adesto.c
blob974f8ad146303d1ab3732cb2c7f8ee2fa36e8439
1 /*
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
17 * based on winbond.c
20 #include <console/console.h>
21 #include <commonlib/helpers.h>
22 #include <string.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 {
43 uint16_t id;
44 /* Log2 of page size in power-of-two mode */
45 uint8_t l2_page_size;
46 uint16_t pages_per_sector;
47 uint16_t sectors_per_block;
48 uint16_t nr_blocks;
49 const char *name;
52 static const struct adesto_spi_flash_params adesto_spi_flash_table[] = {
54 .id = 0x4218,
55 .l2_page_size = 8,
56 .pages_per_sector = 16,
57 .sectors_per_block = 16,
58 .nr_blocks = 256,
59 .name = "AT25SL128A",
62 .id = 0x4501,
63 .l2_page_size = 8,
64 .pages_per_sector = 16,
65 .sectors_per_block = 16,
66 .nr_blocks = 16,
67 .name = "AT25DF081A", /* Yes, 81A id < 81 */
70 .id = 0x4502,
71 .l2_page_size = 8,
72 .pages_per_sector = 16,
73 .sectors_per_block = 16,
74 .nr_blocks = 16,
75 .name = "AT25DF081",
78 .id = 0x4602,
79 .l2_page_size = 8,
80 .pages_per_sector = 16,
81 .sectors_per_block = 16,
82 .nr_blocks = 32,
83 .name = "AT25DF161",
86 .id = 0x4603,
87 .l2_page_size = 8,
88 .pages_per_sector = 16,
89 .sectors_per_block = 16,
90 .nr_blocks = 32,
91 .name = "AT25DL161",
94 .id = 0x4700,
95 .l2_page_size = 8,
96 .pages_per_sector = 16,
97 .sectors_per_block = 16,
98 .nr_blocks = 64,
99 .name = "AT25DF321",
102 .id = 0x4701,
103 .l2_page_size = 8,
104 .pages_per_sector = 16,
105 .sectors_per_block = 16,
106 .nr_blocks = 64,
107 .name = "AT25DF321A",
110 .id = 0x4800,
111 .l2_page_size = 8,
112 .pages_per_sector = 16,
113 .sectors_per_block = 16,
114 .nr_blocks = 128,
115 .name = "AT25DF641",
118 .id = 0x8501,
119 .l2_page_size = 8,
120 .pages_per_sector = 16,
121 .sectors_per_block = 16,
122 .nr_blocks = 16,
123 .name = "AT25SF081",
126 .id = 0x8600,
127 .l2_page_size = 8,
128 .pages_per_sector = 16,
129 .sectors_per_block = 16,
130 .nr_blocks = 32,
131 .name = "AT25DQ161",
134 .id = 0x8601,
135 .l2_page_size = 8,
136 .pages_per_sector = 16,
137 .sectors_per_block = 16,
138 .nr_blocks = 32,
139 .name = "AT25SF161",
142 .id = 0x8700,
143 .l2_page_size = 8,
144 .pages_per_sector = 16,
145 .sectors_per_block = 16,
146 .nr_blocks = 64,
147 .name = "AT25DQ321",
151 static int adesto_write(const struct spi_flash *flash, u32 offset, size_t len,
152 const void *buf)
154 unsigned long byte_addr;
155 unsigned long page_size;
156 size_t chunk_len;
157 size_t actual;
158 int ret;
159 u8 cmd[4];
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);
176 #endif
178 ret = spi_flash_cmd(&flash->spi, CMD_AT25DF_WREN, NULL, 0);
179 if (ret < 0) {
180 printk(BIOS_WARNING, "SF: Enabling Write failed\n");
181 goto out;
184 ret = spi_flash_cmd_write(&flash->spi, cmd, sizeof(cmd),
185 buf + actual, chunk_len);
186 if (ret < 0) {
187 printk(BIOS_WARNING, "SF: adesto Page Program failed\n");
188 goto out;
191 ret = spi_flash_cmd_wait_ready(flash,
192 SPI_FLASH_PROG_TIMEOUT_MS);
193 if (ret)
194 goto out;
196 offset += chunk_len;
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));
202 #endif
203 ret = 0;
205 out:
206 return ret;
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;
218 unsigned int i;
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]))
223 break;
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]);
229 return -1;
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 *
238 params->nr_blocks;
239 flash->erase_cmd = CMD_AT25DF_SE;
241 flash->ops = &spi_flash_ops;
243 return 0;