blob: 28bb51a461948bc0c87e8651bad1967b2d4d62e3 [file] [log] [blame]
Mingkai Hu6805e4b2009-03-31 14:09:41 +08001/*
2 * Copyright (C) 2009 Freescale Semiconductor, Inc.
3 *
4 * Author: Mingkai Hu (Mingkai.hu@freescale.com)
5 * Based on stmicro.c by Wolfgang Denk (wd@denx.de),
6 * TsiChung Liew (Tsi-Chung.Liew@freescale.com),
7 * and Jason McMullan (mcmullan@netapp.com)
8 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
28#include <common.h>
29#include <malloc.h>
30#include <spi_flash.h>
31
32#include "spi_flash_internal.h"
33
34/* S25FLxx-specific commands */
35#define CMD_S25FLXX_READ 0x03 /* Read Data Bytes */
36#define CMD_S25FLXX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
37#define CMD_S25FLXX_READID 0x90 /* Read Manufacture ID and Device ID */
38#define CMD_S25FLXX_WREN 0x06 /* Write Enable */
39#define CMD_S25FLXX_WRDI 0x04 /* Write Disable */
40#define CMD_S25FLXX_RDSR 0x05 /* Read Status Register */
41#define CMD_S25FLXX_WRSR 0x01 /* Write Status Register */
42#define CMD_S25FLXX_PP 0x02 /* Page Program */
43#define CMD_S25FLXX_SE 0xd8 /* Sector Erase */
44#define CMD_S25FLXX_BE 0xc7 /* Bulk Erase */
45#define CMD_S25FLXX_DP 0xb9 /* Deep Power-down */
46#define CMD_S25FLXX_RES 0xab /* Release from DP, and Read Signature */
47
48#define SPSN_ID_S25FL008A 0x0213
49#define SPSN_ID_S25FL016A 0x0214
50#define SPSN_ID_S25FL032A 0x0215
51#define SPSN_ID_S25FL064A 0x0216
52#define SPSN_ID_S25FL128P 0x2018
53#define SPSN_EXT_ID_S25FL128P_256KB 0x0300
54#define SPSN_EXT_ID_S25FL128P_64KB 0x0301
David Janderff0dc2c2010-08-23 15:12:16 +020055#define SPSN_EXT_ID_S25FL032P 0x4d00
Mingkai Hu6805e4b2009-03-31 14:09:41 +080056
Mingkai Hu6805e4b2009-03-31 14:09:41 +080057struct spansion_spi_flash_params {
58 u16 idcode1;
59 u16 idcode2;
60 u16 page_size;
61 u16 pages_per_sector;
62 u16 nr_sectors;
63 const char *name;
64};
65
Mingkai Hu6805e4b2009-03-31 14:09:41 +080066static const struct spansion_spi_flash_params spansion_spi_flash_table[] = {
67 {
68 .idcode1 = SPSN_ID_S25FL008A,
69 .idcode2 = 0,
70 .page_size = 256,
71 .pages_per_sector = 256,
72 .nr_sectors = 16,
73 .name = "S25FL008A",
74 },
75 {
76 .idcode1 = SPSN_ID_S25FL016A,
77 .idcode2 = 0,
78 .page_size = 256,
79 .pages_per_sector = 256,
80 .nr_sectors = 32,
81 .name = "S25FL016A",
82 },
83 {
84 .idcode1 = SPSN_ID_S25FL032A,
85 .idcode2 = 0,
86 .page_size = 256,
87 .pages_per_sector = 256,
88 .nr_sectors = 64,
89 .name = "S25FL032A",
90 },
91 {
92 .idcode1 = SPSN_ID_S25FL064A,
93 .idcode2 = 0,
94 .page_size = 256,
95 .pages_per_sector = 256,
96 .nr_sectors = 128,
97 .name = "S25FL064A",
98 },
99 {
100 .idcode1 = SPSN_ID_S25FL128P,
101 .idcode2 = SPSN_EXT_ID_S25FL128P_64KB,
102 .page_size = 256,
103 .pages_per_sector = 256,
104 .nr_sectors = 256,
105 .name = "S25FL128P_64K",
106 },
107 {
108 .idcode1 = SPSN_ID_S25FL128P,
109 .idcode2 = SPSN_EXT_ID_S25FL128P_256KB,
110 .page_size = 256,
111 .pages_per_sector = 1024,
112 .nr_sectors = 64,
113 .name = "S25FL128P_256K",
114 },
David Janderff0dc2c2010-08-23 15:12:16 +0200115 {
116 .idcode1 = SPSN_ID_S25FL032A,
117 .idcode2 = SPSN_EXT_ID_S25FL032P,
118 .page_size = 256,
119 .pages_per_sector = 256,
120 .nr_sectors = 64,
121 .name = "S25FL032P",
122 },
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800123};
124
Mike Frysingerf8f07572011-04-12 01:51:29 -0400125static int spansion_erase(struct spi_flash *flash, u32 offset, size_t len)
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800126{
Richard Retanubun4e6a5152011-02-16 16:37:22 -0500127 return spi_flash_cmd_erase(flash, CMD_S25FLXX_SE, offset, len);
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800128}
129
130struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode)
131{
132 const struct spansion_spi_flash_params *params;
Mike Frysingerb06afa72011-06-28 07:38:10 +0000133 struct spi_flash *flash;
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800134 unsigned int i;
135 unsigned short jedec, ext_jedec;
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800136
Mike Frysinger0dcdbb12009-03-28 06:41:09 -0400137 jedec = idcode[1] << 8 | idcode[2];
138 ext_jedec = idcode[3] << 8 | idcode[4];
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800139
140 for (i = 0; i < ARRAY_SIZE(spansion_spi_flash_table); i++) {
141 params = &spansion_spi_flash_table[i];
142 if (params->idcode1 == jedec) {
143 if (params->idcode2 == ext_jedec)
144 break;
145 }
146 }
147
148 if (i == ARRAY_SIZE(spansion_spi_flash_table)) {
149 debug("SF: Unsupported SPANSION ID %04x %04x\n", jedec, ext_jedec);
150 return NULL;
151 }
152
Mike Frysingerb06afa72011-06-28 07:38:10 +0000153 flash = malloc(sizeof(*flash));
154 if (!flash) {
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800155 debug("SF: Failed to allocate memory\n");
156 return NULL;
157 }
158
Mike Frysingerb06afa72011-06-28 07:38:10 +0000159 flash->spi = spi;
160 flash->name = params->name;
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800161
Mike Frysingerb06afa72011-06-28 07:38:10 +0000162 flash->write = spi_flash_cmd_write_multi;
163 flash->erase = spansion_erase;
164 flash->read = spi_flash_cmd_read_fast;
165 flash->page_size = params->page_size;
166 flash->sector_size = params->page_size * params->pages_per_sector;
167 flash->size = flash->sector_size * params->nr_sectors;
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800168
Mike Frysingerb06afa72011-06-28 07:38:10 +0000169 return flash;
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800170}