{drivers,southbridge}: Replace min() with MIN()
[coreboot.git] / src / drivers / spi / atmel.c
blob4496f4b08c89c46019a8a18a8e5d91e0c0ce2fd8
1 /*
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>
19 #include <string.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 {
38 uint16_t id;
39 /* Log2 of page size in power-of-two mode */
40 uint8_t l2_page_size;
41 uint16_t pages_per_sector;
42 uint16_t sectors_per_block;
43 uint16_t nr_blocks;
44 const char *name;
47 static const struct atmel_spi_flash_params atmel_spi_flash_table[] = {
49 .id = 0x3015,
50 .l2_page_size = 8,
51 .pages_per_sector = 16,
52 .sectors_per_block = 16,
53 .nr_blocks = 32,
54 .name = "AT25X16",
57 .id = 0x47,
58 .l2_page_size = 8,
59 .pages_per_sector = 16,
60 .sectors_per_block = 16,
61 .nr_blocks = 64,
62 .name = "AT25DF32",
65 .id = 0x3017,
66 .l2_page_size = 8,
67 .pages_per_sector = 16,
68 .sectors_per_block = 16,
69 .nr_blocks = 128,
70 .name = "AT25X64",
73 .id = 0x4015,
74 .l2_page_size = 8,
75 .pages_per_sector = 16,
76 .sectors_per_block = 16,
77 .nr_blocks = 32,
78 .name = "AT25Q16",
81 .id = 0x4016,
82 .l2_page_size = 8,
83 .pages_per_sector = 16,
84 .sectors_per_block = 16,
85 .nr_blocks = 64,
86 .name = "AT25Q32",
89 .id = 0x4017,
90 .l2_page_size = 8,
91 .pages_per_sector = 16,
92 .sectors_per_block = 16,
93 .nr_blocks = 128,
94 .name = "AT25Q64",
97 .id = 0x4018,
98 .l2_page_size = 8,
99 .pages_per_sector = 16,
100 .sectors_per_block = 16,
101 .nr_blocks = 256,
102 .name = "AT25Q128",
106 static int atmel_write(const struct spi_flash *flash, u32 offset, size_t len,
107 const void *buf)
109 unsigned long byte_addr;
110 unsigned long page_size;
111 size_t chunk_len;
112 size_t actual;
113 int ret;
114 u8 cmd[4];
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);
131 #endif
133 ret = spi_flash_cmd(&flash->spi, CMD_AT25_WREN, NULL, 0);
134 if (ret < 0) {
135 printk(BIOS_WARNING, "SF: Enabling Write failed\n");
136 goto out;
139 ret = spi_flash_cmd_write(&flash->spi, cmd, sizeof(cmd),
140 buf + actual, chunk_len);
141 if (ret < 0) {
142 printk(BIOS_WARNING, "SF: Atmel Page Program failed\n");
143 goto out;
146 ret = spi_flash_cmd_wait_ready(flash,
147 SPI_FLASH_PROG_TIMEOUT_MS);
148 if (ret)
149 goto out;
151 offset += chunk_len;
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));
157 #endif
158 ret = 0;
160 out:
161 return ret;
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;
173 unsigned int i;
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]))
178 break;
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]);
184 return -1;
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 *
194 params->nr_blocks;
195 flash->erase_cmd = CMD_AT25_SE;
197 flash->ops = &spi_flash_ops;
199 return 0;