sf: inline data constants
[oweals/u-boot.git] / drivers / mtd / spi / spansion.c
1 /*
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_SE          0xd8    /* Sector Erase */
36 #define CMD_S25FLXX_BE          0xc7    /* Bulk Erase */
37
38 struct spansion_spi_flash_params {
39         u16 idcode1;
40         u16 idcode2;
41         u16 pages_per_sector;
42         u16 nr_sectors;
43         const char *name;
44 };
45
46 static const struct spansion_spi_flash_params spansion_spi_flash_table[] = {
47         {
48                 .idcode1 = 0x0213,
49                 .idcode2 = 0,
50                 .pages_per_sector = 256,
51                 .nr_sectors = 16,
52                 .name = "S25FL008A",
53         },
54         {
55                 .idcode1 = 0x0214,
56                 .idcode2 = 0,
57                 .pages_per_sector = 256,
58                 .nr_sectors = 32,
59                 .name = "S25FL016A",
60         },
61         {
62                 .idcode1 = 0x0215,
63                 .idcode2 = 0,
64                 .pages_per_sector = 256,
65                 .nr_sectors = 64,
66                 .name = "S25FL032A",
67         },
68         {
69                 .idcode1 = 0x0216,
70                 .idcode2 = 0,
71                 .pages_per_sector = 256,
72                 .nr_sectors = 128,
73                 .name = "S25FL064A",
74         },
75         {
76                 .idcode1 = 0x2018,
77                 .idcode2 = 0x0301,
78                 .pages_per_sector = 256,
79                 .nr_sectors = 256,
80                 .name = "S25FL128P_64K",
81         },
82         {
83                 .idcode1 = 0x2018,
84                 .idcode2 = 0x0300,
85                 .pages_per_sector = 1024,
86                 .nr_sectors = 64,
87                 .name = "S25FL128P_256K",
88         },
89         {
90                 .idcode1 = 0x0215,
91                 .idcode2 = 0x4d00,
92                 .pages_per_sector = 256,
93                 .nr_sectors = 64,
94                 .name = "S25FL032P",
95         },
96         {
97                 .idcode1 = 0x2018,
98                 .idcode2 = 0x4d01,
99                 .pages_per_sector = 256,
100                 .nr_sectors = 256,
101                 .name = "S25FL129P_64K",
102         },
103 };
104
105 static int spansion_erase(struct spi_flash *flash, u32 offset, size_t len)
106 {
107         return spi_flash_cmd_erase(flash, CMD_S25FLXX_SE, offset, len);
108 }
109
110 struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode)
111 {
112         const struct spansion_spi_flash_params *params;
113         struct spi_flash *flash;
114         unsigned int i;
115         unsigned short jedec, ext_jedec;
116
117         jedec = idcode[1] << 8 | idcode[2];
118         ext_jedec = idcode[3] << 8 | idcode[4];
119
120         for (i = 0; i < ARRAY_SIZE(spansion_spi_flash_table); i++) {
121                 params = &spansion_spi_flash_table[i];
122                 if (params->idcode1 == jedec) {
123                         if (params->idcode2 == ext_jedec)
124                                 break;
125                 }
126         }
127
128         if (i == ARRAY_SIZE(spansion_spi_flash_table)) {
129                 debug("SF: Unsupported SPANSION ID %04x %04x\n", jedec, ext_jedec);
130                 return NULL;
131         }
132
133         flash = malloc(sizeof(*flash));
134         if (!flash) {
135                 debug("SF: Failed to allocate memory\n");
136                 return NULL;
137         }
138
139         flash->spi = spi;
140         flash->name = params->name;
141
142         flash->write = spi_flash_cmd_write_multi;
143         flash->erase = spansion_erase;
144         flash->read = spi_flash_cmd_read_fast;
145         flash->page_size = 256;
146         flash->sector_size = 256 * params->pages_per_sector;
147         flash->size = flash->sector_size * params->nr_sectors;
148
149         return flash;
150 }