blob: 910b874cd4eec29383264e6e22c0987955c59da5 [file] [log] [blame]
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07001/*
2 * Copyright 2009(C) Marvell International Ltd. and its affiliates
3 * Prafulla Wadaskar <prafulla@marvell.com>
4 *
5 * Based on drivers/mtd/spi/stmicro.c
6 *
7 * Copyright 2008, Network Appliance Inc.
8 * Jason McMullan <mcmullan@netapp.com>
9 *
10 * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
11 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
12 *
13 * See file CREDITS for list of people who contributed to this
14 * project.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
29 * MA 02110-1301 USA
30 */
31
32#include <stdlib.h>
33#include <spi_flash.h>
34#include "spi_flash_internal.h"
35
36/* MX25xx-specific commands */
37#define CMD_MX25XX_WREN 0x06 /* Write Enable */
38#define CMD_MX25XX_WRDI 0x04 /* Write Disable */
39#define CMD_MX25XX_RDSR 0x05 /* Read Status Register */
40#define CMD_MX25XX_WRSR 0x01 /* Write Status Register */
41#define CMD_MX25XX_READ 0x03 /* Read Data Bytes */
42#define CMD_MX25XX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
43#define CMD_MX25XX_PP 0x02 /* Page Program */
44#define CMD_MX25XX_SE 0x20 /* Sector Erase */
45#define CMD_MX25XX_BE 0xD8 /* Block Erase */
46#define CMD_MX25XX_CE 0xc7 /* Chip Erase */
47#define CMD_MX25XX_DP 0xb9 /* Deep Power-down */
48#define CMD_MX25XX_RES 0xab /* Release from DP, and Read Signature */
49
50#define MACRONIX_SR_WIP (1 << 0) /* Write-in-Progress */
51
52struct macronix_spi_flash_params {
53 u16 idcode;
54 u16 page_size;
55 u16 pages_per_sector;
56 u16 sectors_per_block;
57 u16 nr_blocks;
58 const char *name;
59};
60
61struct macronix_spi_flash {
62 struct spi_flash flash;
63 const struct macronix_spi_flash_params *params;
64};
65
66static inline struct macronix_spi_flash *to_macronix_spi_flash(struct spi_flash
67 *flash)
68{
69 return container_of(flash, struct macronix_spi_flash, flash);
70}
71
72static const struct macronix_spi_flash_params macronix_spi_flash_table[] = {
73 {
74 .idcode = 0x2015,
75 .page_size = 256,
76 .pages_per_sector = 16,
77 .sectors_per_block = 16,
78 .nr_blocks = 32,
79 .name = "MX25L1605D",
80 },
81 {
82 .idcode = 0x2016,
83 .page_size = 256,
84 .pages_per_sector = 16,
85 .sectors_per_block = 16,
86 .nr_blocks = 64,
87 .name = "MX25L3205D",
88 },
89 {
Zheng Baoc269a9b2012-12-06 11:30:33 +080090 .idcode = 0x5e16,
91 .page_size = 256,
92 .pages_per_sector = 16,
93 .sectors_per_block = 16,
94 .nr_blocks = 64,
95 .name = "MX25L3235D", /* MX25L3225D/MX25L3235D/MX25L3237D */
96 },
97 {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070098 .idcode = 0x2017,
99 .page_size = 256,
100 .pages_per_sector = 16,
101 .sectors_per_block = 16,
102 .nr_blocks = 128,
103 .name = "MX25L6405D",
104 },
105 {
106 .idcode = 0x2018,
107 .page_size = 256,
108 .pages_per_sector = 16,
109 .sectors_per_block = 16,
110 .nr_blocks = 256,
111 .name = "MX25L12805D",
112 },
113 {
114 .idcode = 0x2618,
115 .page_size = 256,
116 .pages_per_sector = 16,
117 .sectors_per_block = 16,
118 .nr_blocks = 256,
119 .name = "MX25L12855E",
120 },
121};
122
123static int macronix_write(struct spi_flash *flash,
124 u32 offset, size_t len, const void *buf)
125{
126 struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash);
127 unsigned long byte_addr;
128 unsigned long page_size;
129 size_t chunk_len;
130 size_t actual;
131 int ret;
132 u8 cmd[4];
133
134 page_size = min(mcx->params->page_size, CONTROLLER_PAGE_LIMIT);
135 byte_addr = offset % page_size;
136
Martin Roth3316cf22012-12-05 16:22:54 -0700137 flash->spi->rw = SPI_WRITE_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700138 ret = spi_claim_bus(flash->spi);
139 if (ret) {
140 printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n");
141 return ret;
142 }
143
144 ret = 0;
145 for (actual = 0; actual < len; actual += chunk_len) {
146 chunk_len = min(len - actual, page_size - byte_addr);
147
148 cmd[0] = CMD_MX25XX_PP;
149 cmd[1] = (offset >> 16) & 0xff;
150 cmd[2] = (offset >> 8) & 0xff;
151 cmd[3] = offset & 0xff;
152
Stefan Reinauer5649b082012-05-23 11:03:29 -0700153#if CONFIG_DEBUG_SPI_FLASH
Stefan Reinauer8ea5a342012-05-14 13:52:32 -0700154 printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }"
155 " chunk_len = %zu\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700156 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
Stefan Reinauer5649b082012-05-23 11:03:29 -0700157#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700158
159 ret = spi_flash_cmd(flash->spi, CMD_MX25XX_WREN, NULL, 0);
160 if (ret < 0) {
161 printk(BIOS_WARNING, "SF: Enabling Write failed\n");
162 break;
163 }
164
165 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
166 buf + actual, chunk_len);
167 if (ret < 0) {
168 printk(BIOS_WARNING, "SF: Macronix Page Program failed\n");
169 break;
170 }
171
172 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
173 if (ret)
174 break;
175
176 offset += chunk_len;
177 byte_addr = 0;
178 }
179
Marc Jones747127d2012-12-03 22:16:29 -0700180#if CONFIG_DEBUG_SPI_FLASH
181 printk(BIOS_SPEW, "SF: Macronix: Successfully programmed %zu bytes @"
Stefan Reinauer8ea5a342012-05-14 13:52:32 -0700182 " 0x%lx\n", len, (unsigned long)(offset - len));
Marc Jones747127d2012-12-03 22:16:29 -0700183#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700184
185 spi_release_bus(flash->spi);
186 return ret;
187}
188
189static int macronix_erase(struct spi_flash *flash, u32 offset, size_t len)
190{
191 return spi_flash_cmd_erase(flash, CMD_MX25XX_SE, offset, len);
192}
193
194struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode)
195{
196 const struct macronix_spi_flash_params *params;
197 struct macronix_spi_flash *mcx;
198 unsigned int i;
199 u16 id = idcode[2] | idcode[1] << 8;
200
201 for (i = 0; i < ARRAY_SIZE(macronix_spi_flash_table); i++) {
202 params = &macronix_spi_flash_table[i];
203 if (params->idcode == id)
204 break;
205 }
206
207 if (i == ARRAY_SIZE(macronix_spi_flash_table)) {
208 printk(BIOS_WARNING, "SF: Unsupported Macronix ID %04x\n", id);
209 return NULL;
210 }
211
212 mcx = malloc(sizeof(*mcx));
213 if (!mcx) {
214 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
215 return NULL;
216 }
217
218 mcx->params = params;
219 mcx->flash.spi = spi;
220 mcx->flash.name = params->name;
221
222 mcx->flash.write = macronix_write;
223 mcx->flash.erase = macronix_erase;
Duncan Laurie23b00532012-10-10 14:21:23 -0700224#if CONFIG_SPI_FLASH_NO_FAST_READ
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700225 mcx->flash.read = spi_flash_cmd_read_slow;
226#else
227 mcx->flash.read = spi_flash_cmd_read_fast;
228#endif
229 mcx->flash.sector_size = params->page_size * params->pages_per_sector;
230 mcx->flash.size = mcx->flash.sector_size * params->sectors_per_block *
231 params->nr_blocks;
232
233 return &mcx->flash;
234}