blob: 780cc82b23ff797655c09bf93dae10d6f11c1a66 [file] [log] [blame]
Martin Rothbceaf7f2012-09-07 15:02:35 -06001/*
2 * Copyright (c) 2012, Google Inc.
3 *
4 * Based on drivers/spi/winbond.c
5 *
6 * Copyright 2008, Network Appliance Inc.
7 * Jason McMullan <mcmullan@netapp.com>
8 *
Martin Rothbceaf7f2012-09-07 15:02:35 -06009 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
Martin Rothbceaf7f2012-09-07 15:02:35 -060018 */
19
20
21#include <stdlib.h>
22#include <spi_flash.h>
Edward O'Callaghanc4561e22014-06-26 15:02:40 +100023
Martin Rothbceaf7f2012-09-07 15:02:35 -060024#include "spi_flash_internal.h"
25
26/* GD25Pxx-specific commands */
27#define CMD_GD25_WREN 0x06 /* Write Enable */
28#define CMD_GD25_WRDI 0x04 /* Write Disable */
29#define CMD_GD25_RDSR 0x05 /* Read Status Register */
30#define CMD_GD25_WRSR 0x01 /* Write Status Register */
31#define CMD_GD25_READ 0x03 /* Read Data Bytes */
32#define CMD_GD25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
33#define CMD_GD25_PP 0x02 /* Page Program */
34#define CMD_GD25_SE 0x20 /* Sector (4K) Erase */
35#define CMD_GD25_BE 0xd8 /* Block (64K) Erase */
36#define CMD_GD25_CE 0xc7 /* Chip Erase */
37#define CMD_GD25_DP 0xb9 /* Deep Power-down */
38#define CMD_GD25_RES 0xab /* Release from DP, and Read Signature */
39
40struct gigadevice_spi_flash_params {
41 uint16_t id;
42 /* Log2 of page size in power-of-two mode */
43 uint8_t l2_page_size;
44 uint16_t pages_per_sector;
45 uint16_t sectors_per_block;
46 uint16_t nr_blocks;
47 const char *name;
48};
49
50/* spi_flash needs to be first so upper layers can free() it */
51struct gigadevice_spi_flash {
52 struct spi_flash flash;
53 const struct gigadevice_spi_flash_params *params;
54};
55
56static inline struct gigadevice_spi_flash *
57to_gigadevice_spi_flash(struct spi_flash *flash)
58{
59 return container_of(flash, struct gigadevice_spi_flash, flash);
60}
61
62static const struct gigadevice_spi_flash_params gigadevice_spi_flash_table[] = {
63 {
64 .id = 0x4014,
65 .l2_page_size = 8,
66 .pages_per_sector = 16,
67 .sectors_per_block = 16,
68 .nr_blocks = 16,
69 .name = "GD25Q80",
70 },
71 {
72 .id = 0x4015,
73 .l2_page_size = 8,
74 .pages_per_sector = 16,
75 .sectors_per_block = 16,
76 .nr_blocks = 32,
77 .name = "GD25Q16(B)",
78 },
79 {
80 .id = 0x4016,
81 .l2_page_size = 8,
82 .pages_per_sector = 16,
83 .sectors_per_block = 16,
84 .nr_blocks = 64,
85 .name = "GD25Q32(B)",
86 },
87 {
88 .id = 0x6016,
89 .l2_page_size = 8,
90 .pages_per_sector = 16,
91 .sectors_per_block = 16,
92 .nr_blocks = 64,
93 .name = "GD25LQ32",
94 },
95 {
96 .id = 0x4017,
97 .l2_page_size = 8,
98 .pages_per_sector = 16,
99 .sectors_per_block = 16,
100 .nr_blocks = 128,
Marc Jones9bab54b2014-04-03 16:37:06 -0600101 .name = "GD25Q64B/GD25B64C",
102 },
103 {
104 .id = 0x6017,
105 .l2_page_size = 8,
106 .pages_per_sector = 16,
107 .sectors_per_block = 16,
108 .nr_blocks = 128,
109 .name = "GD25LQ64C/GD25LB64C",
Martin Rothbceaf7f2012-09-07 15:02:35 -0600110 },
111 {
112 .id = 0x4018,
113 .l2_page_size = 8,
114 .pages_per_sector = 16,
115 .sectors_per_block = 16,
116 .nr_blocks = 256,
117 .name = "GD25Q128(B)",
118 },
119};
120
121static int gigadevice_write(struct spi_flash *flash, u32 offset,
122 size_t len, const void *buf)
123{
124 struct gigadevice_spi_flash *stm = to_gigadevice_spi_flash(flash);
125 unsigned long byte_addr;
126 unsigned long page_size;
127 size_t chunk_len;
128 size_t actual;
David Hendricks032c8432014-04-11 19:48:55 -0700129 int ret = 0;
Martin Rothbceaf7f2012-09-07 15:02:35 -0600130 u8 cmd[4];
131
Kyösti Mälkki77d12802014-06-29 16:15:39 +0300132 page_size = 1 << stm->params->l2_page_size;
Martin Rothbceaf7f2012-09-07 15:02:35 -0600133 byte_addr = offset % page_size;
134
Martin Roth3316cf22012-12-05 16:22:54 -0700135 flash->spi->rw = SPI_WRITE_FLAG;
Martin Rothbceaf7f2012-09-07 15:02:35 -0600136
137 for (actual = 0; actual < len; actual += chunk_len) {
138 chunk_len = min(len - actual, page_size - byte_addr);
Kyösti Mälkki11104952014-06-29 16:17:33 +0300139 chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
Martin Rothbceaf7f2012-09-07 15:02:35 -0600140
141 ret = spi_flash_cmd(flash->spi, CMD_GD25_WREN, NULL, 0);
142 if (ret < 0) {
143 printk(BIOS_WARNING,
144 "SF gigadevice.c: Enabling Write failed\n");
145 goto out;
146 }
147
148 cmd[0] = CMD_GD25_PP;
149 cmd[1] = (offset >> 16) & 0xff;
150 cmd[2] = (offset >> 8) & 0xff;
151 cmd[3] = offset & 0xff;
152#if CONFIG_DEBUG_SPI_FLASH
153 printk(BIOS_SPEW,
Duncan Laurie23b00532012-10-10 14:21:23 -0700154 "PP gigadevice.c: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }"
Martin Rothbceaf7f2012-09-07 15:02:35 -0600155 " chunk_len = %zu\n", buf + actual,
156 cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
157#endif
158
Kyösti Mälkki11104952014-06-29 16:17:33 +0300159 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd),
Martin Rothbceaf7f2012-09-07 15:02:35 -0600160 buf + actual, chunk_len);
161 if (ret < 0) {
162 printk(BIOS_WARNING,
163 "SF gigadevice.c: Page Program failed\n");
164 goto out;
165 }
166
167 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
168 if (ret)
169 goto out;
170
171 offset += chunk_len;
172 byte_addr = 0;
173 }
174
Marc Jones747127d2012-12-03 22:16:29 -0700175#if CONFIG_DEBUG_SPI_FLASH
176 printk(BIOS_SPEW,
Martin Rothbceaf7f2012-09-07 15:02:35 -0600177 "SF gigadevice.c: Successfully programmed %zu bytes @ %#x\n",
178 len, (unsigned int)(offset - len));
Marc Jones747127d2012-12-03 22:16:29 -0700179#endif
180
Martin Rothbceaf7f2012-09-07 15:02:35 -0600181 ret = 0;
182
183out:
Martin Rothbceaf7f2012-09-07 15:02:35 -0600184 return ret;
185}
186
David Hendricksfccc7de2014-12-12 14:07:08 -0800187static struct gigadevice_spi_flash stm;
188
Martin Rothbceaf7f2012-09-07 15:02:35 -0600189struct spi_flash *spi_flash_probe_gigadevice(struct spi_slave *spi, u8 *idcode)
190{
191 const struct gigadevice_spi_flash_params *params;
192 unsigned page_size;
Martin Rothbceaf7f2012-09-07 15:02:35 -0600193 unsigned int i;
194
195 for (i = 0; i < ARRAY_SIZE(gigadevice_spi_flash_table); i++) {
196 params = &gigadevice_spi_flash_table[i];
197 if (params->id == ((idcode[1] << 8) | idcode[2]))
198 break;
199 }
200
201 if (i == ARRAY_SIZE(gigadevice_spi_flash_table)) {
202 printk(BIOS_WARNING,
203 "SF gigadevice.c: Unsupported ID %#02x%02x\n",
204 idcode[1], idcode[2]);
205 return NULL;
206 }
207
David Hendricksfccc7de2014-12-12 14:07:08 -0800208 stm.params = params;
209 stm.flash.spi = spi;
210 stm.flash.name = params->name;
Martin Rothbceaf7f2012-09-07 15:02:35 -0600211
212 /* Assuming power-of-two page size initially. */
213 page_size = 1 << params->l2_page_size;
214
David Hendricksfccc7de2014-12-12 14:07:08 -0800215 stm.flash.write = gigadevice_write;
Dan Ehrenberga5aac762015-01-08 10:29:19 -0800216 stm.flash.erase = spi_flash_cmd_erase;
Duncan Lauriefb032392015-01-15 15:28:46 -0800217 stm.flash.status = spi_flash_cmd_status;
Duncan Laurie23b00532012-10-10 14:21:23 -0700218#if CONFIG_SPI_FLASH_NO_FAST_READ
David Hendricksfccc7de2014-12-12 14:07:08 -0800219 stm.flash.read = spi_flash_cmd_read_slow;
Martin Rothbceaf7f2012-09-07 15:02:35 -0600220#else
David Hendricksfccc7de2014-12-12 14:07:08 -0800221 stm.flash.read = spi_flash_cmd_read_fast;
Martin Rothbceaf7f2012-09-07 15:02:35 -0600222#endif
David Hendricksfccc7de2014-12-12 14:07:08 -0800223 stm.flash.sector_size = (1 << stm.params->l2_page_size) *
224 stm.params->pages_per_sector;
225 stm.flash.size = page_size * params->pages_per_sector
Martin Rothbceaf7f2012-09-07 15:02:35 -0600226 * params->sectors_per_block
227 * params->nr_blocks;
Dan Ehrenberga5aac762015-01-08 10:29:19 -0800228 stm.flash.erase_cmd = CMD_GD25_SE;
Duncan Lauriefb032392015-01-15 15:28:46 -0800229 stm.flash.status_cmd = CMD_GD25_RDSR;
Martin Rothbceaf7f2012-09-07 15:02:35 -0600230
David Hendricksfccc7de2014-12-12 14:07:08 -0800231 return &stm.flash;
Martin Rothbceaf7f2012-09-07 15:02:35 -0600232}