xref: /rk3399_rockchip-uboot/drivers/mtd/jedec_flash.c (revision 81b20ccc2d795ae9a1199db5a50ad9c28d1e4d22)
1*81b20cccSMichael Schwingen /*
2*81b20cccSMichael Schwingen  * (C) Copyright 2007
3*81b20cccSMichael Schwingen  * Michael Schwingen, <michael@schwingen.org>
4*81b20cccSMichael Schwingen  *
5*81b20cccSMichael Schwingen  * based in great part on jedec_probe.c from linux kernel:
6*81b20cccSMichael Schwingen  * (C) 2000 Red Hat. GPL'd.
7*81b20cccSMichael Schwingen  * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
8*81b20cccSMichael Schwingen  *
9*81b20cccSMichael Schwingen  * See file CREDITS for list of people who contributed to this
10*81b20cccSMichael Schwingen  * project.
11*81b20cccSMichael Schwingen  *
12*81b20cccSMichael Schwingen  * This program is free software; you can redistribute it and/or
13*81b20cccSMichael Schwingen  * modify it under the terms of the GNU General Public License as
14*81b20cccSMichael Schwingen  * published by the Free Software Foundation; either version 2 of
15*81b20cccSMichael Schwingen  * the License, or (at your option) any later version.
16*81b20cccSMichael Schwingen  *
17*81b20cccSMichael Schwingen  * This program is distributed in the hope that it will be useful,
18*81b20cccSMichael Schwingen  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19*81b20cccSMichael Schwingen  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
20*81b20cccSMichael Schwingen  * GNU General Public License for more details.
21*81b20cccSMichael Schwingen  *
22*81b20cccSMichael Schwingen  * You should have received a copy of the GNU General Public License
23*81b20cccSMichael Schwingen  * along with this program; if not, write to the Free Software
24*81b20cccSMichael Schwingen  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25*81b20cccSMichael Schwingen  * MA 02111-1307 USA
26*81b20cccSMichael Schwingen  *
27*81b20cccSMichael Schwingen  */
28*81b20cccSMichael Schwingen 
29*81b20cccSMichael Schwingen /* The DEBUG define must be before common to enable debugging */
30*81b20cccSMichael Schwingen /*#define DEBUG*/
31*81b20cccSMichael Schwingen 
32*81b20cccSMichael Schwingen #include <common.h>
33*81b20cccSMichael Schwingen #include <asm/processor.h>
34*81b20cccSMichael Schwingen #include <asm/io.h>
35*81b20cccSMichael Schwingen #include <asm/byteorder.h>
36*81b20cccSMichael Schwingen #include <environment.h>
37*81b20cccSMichael Schwingen 
38*81b20cccSMichael Schwingen #define P_ID_AMD_STD CFI_CMDSET_AMD_LEGACY
39*81b20cccSMichael Schwingen 
40*81b20cccSMichael Schwingen /* Manufacturers */
41*81b20cccSMichael Schwingen #define MANUFACTURER_AMD	0x0001
42*81b20cccSMichael Schwingen #define MANUFACTURER_SST	0x00BF
43*81b20cccSMichael Schwingen 
44*81b20cccSMichael Schwingen /* AMD */
45*81b20cccSMichael Schwingen #define AM29DL800BB	0x22C8
46*81b20cccSMichael Schwingen #define AM29DL800BT	0x224A
47*81b20cccSMichael Schwingen 
48*81b20cccSMichael Schwingen #define AM29F800BB	0x2258
49*81b20cccSMichael Schwingen #define AM29F800BT	0x22D6
50*81b20cccSMichael Schwingen #define AM29LV400BB	0x22BA
51*81b20cccSMichael Schwingen #define AM29LV400BT	0x22B9
52*81b20cccSMichael Schwingen #define AM29LV800BB	0x225B
53*81b20cccSMichael Schwingen #define AM29LV800BT	0x22DA
54*81b20cccSMichael Schwingen #define AM29LV160DT	0x22C4
55*81b20cccSMichael Schwingen #define AM29LV160DB	0x2249
56*81b20cccSMichael Schwingen #define AM29F017D	0x003D
57*81b20cccSMichael Schwingen #define AM29F016D	0x00AD
58*81b20cccSMichael Schwingen #define AM29F080	0x00D5
59*81b20cccSMichael Schwingen #define AM29F040	0x00A4
60*81b20cccSMichael Schwingen #define AM29LV040B	0x004F
61*81b20cccSMichael Schwingen #define AM29F032B	0x0041
62*81b20cccSMichael Schwingen #define AM29F002T	0x00B0
63*81b20cccSMichael Schwingen 
64*81b20cccSMichael Schwingen /* SST */
65*81b20cccSMichael Schwingen #define SST39LF800	0x2781
66*81b20cccSMichael Schwingen #define SST39LF160	0x2782
67*81b20cccSMichael Schwingen #define SST39VF1601	0x234b
68*81b20cccSMichael Schwingen #define SST39LF512	0x00D4
69*81b20cccSMichael Schwingen #define SST39LF010	0x00D5
70*81b20cccSMichael Schwingen #define SST39LF020	0x00D6
71*81b20cccSMichael Schwingen #define SST39LF040	0x00D7
72*81b20cccSMichael Schwingen #define SST39SF010A	0x00B5
73*81b20cccSMichael Schwingen #define SST39SF020A	0x00B6
74*81b20cccSMichael Schwingen 
75*81b20cccSMichael Schwingen 
76*81b20cccSMichael Schwingen /*
77*81b20cccSMichael Schwingen  * Unlock address sets for AMD command sets.
78*81b20cccSMichael Schwingen  * Intel command sets use the MTD_UADDR_UNNECESSARY.
79*81b20cccSMichael Schwingen  * Each identifier, except MTD_UADDR_UNNECESSARY, and
80*81b20cccSMichael Schwingen  * MTD_UADDR_NO_SUPPORT must be defined below in unlock_addrs[].
81*81b20cccSMichael Schwingen  * MTD_UADDR_NOT_SUPPORTED must be 0 so that structure
82*81b20cccSMichael Schwingen  * initialization need not require initializing all of the
83*81b20cccSMichael Schwingen  * unlock addresses for all bit widths.
84*81b20cccSMichael Schwingen  */
85*81b20cccSMichael Schwingen enum uaddr {
86*81b20cccSMichael Schwingen 	MTD_UADDR_NOT_SUPPORTED = 0,	/* data width not supported */
87*81b20cccSMichael Schwingen 	MTD_UADDR_0x0555_0x02AA,
88*81b20cccSMichael Schwingen 	MTD_UADDR_0x0555_0x0AAA,
89*81b20cccSMichael Schwingen 	MTD_UADDR_0x5555_0x2AAA,
90*81b20cccSMichael Schwingen 	MTD_UADDR_0x0AAA_0x0555,
91*81b20cccSMichael Schwingen 	MTD_UADDR_DONT_CARE,		/* Requires an arbitrary address */
92*81b20cccSMichael Schwingen 	MTD_UADDR_UNNECESSARY,		/* Does not require any address */
93*81b20cccSMichael Schwingen };
94*81b20cccSMichael Schwingen 
95*81b20cccSMichael Schwingen 
96*81b20cccSMichael Schwingen struct unlock_addr {
97*81b20cccSMichael Schwingen 	u32 addr1;
98*81b20cccSMichael Schwingen 	u32 addr2;
99*81b20cccSMichael Schwingen };
100*81b20cccSMichael Schwingen 
101*81b20cccSMichael Schwingen 
102*81b20cccSMichael Schwingen /*
103*81b20cccSMichael Schwingen  * I don't like the fact that the first entry in unlock_addrs[]
104*81b20cccSMichael Schwingen  * exists, but is for MTD_UADDR_NOT_SUPPORTED - and, therefore,
105*81b20cccSMichael Schwingen  * should not be used.  The  problem is that structures with
106*81b20cccSMichael Schwingen  * initializers have extra fields initialized to 0.  It is _very_
107*81b20cccSMichael Schwingen  * desireable to have the unlock address entries for unsupported
108*81b20cccSMichael Schwingen  * data widths automatically initialized - that means that
109*81b20cccSMichael Schwingen  * MTD_UADDR_NOT_SUPPORTED must be 0 and the first entry here
110*81b20cccSMichael Schwingen  * must go unused.
111*81b20cccSMichael Schwingen  */
112*81b20cccSMichael Schwingen static const struct unlock_addr  unlock_addrs[] = {
113*81b20cccSMichael Schwingen 	[MTD_UADDR_NOT_SUPPORTED] = {
114*81b20cccSMichael Schwingen 		.addr1 = 0xffff,
115*81b20cccSMichael Schwingen 		.addr2 = 0xffff
116*81b20cccSMichael Schwingen 	},
117*81b20cccSMichael Schwingen 
118*81b20cccSMichael Schwingen 	[MTD_UADDR_0x0555_0x02AA] = {
119*81b20cccSMichael Schwingen 		.addr1 = 0x0555,
120*81b20cccSMichael Schwingen 		.addr2 = 0x02aa
121*81b20cccSMichael Schwingen 	},
122*81b20cccSMichael Schwingen 
123*81b20cccSMichael Schwingen 	[MTD_UADDR_0x0555_0x0AAA] = {
124*81b20cccSMichael Schwingen 		.addr1 = 0x0555,
125*81b20cccSMichael Schwingen 		.addr2 = 0x0aaa
126*81b20cccSMichael Schwingen 	},
127*81b20cccSMichael Schwingen 
128*81b20cccSMichael Schwingen 	[MTD_UADDR_0x5555_0x2AAA] = {
129*81b20cccSMichael Schwingen 		.addr1 = 0x5555,
130*81b20cccSMichael Schwingen 		.addr2 = 0x2aaa
131*81b20cccSMichael Schwingen 	},
132*81b20cccSMichael Schwingen 
133*81b20cccSMichael Schwingen 	[MTD_UADDR_0x0AAA_0x0555] = {
134*81b20cccSMichael Schwingen 		.addr1 = 0x0AAA,
135*81b20cccSMichael Schwingen 		.addr2 = 0x0555
136*81b20cccSMichael Schwingen 	},
137*81b20cccSMichael Schwingen 
138*81b20cccSMichael Schwingen 	[MTD_UADDR_DONT_CARE] = {
139*81b20cccSMichael Schwingen 		.addr1 = 0x0000,      /* Doesn't matter which address */
140*81b20cccSMichael Schwingen 		.addr2 = 0x0000       /* is used - must be last entry */
141*81b20cccSMichael Schwingen 	},
142*81b20cccSMichael Schwingen 
143*81b20cccSMichael Schwingen 	[MTD_UADDR_UNNECESSARY] = {
144*81b20cccSMichael Schwingen 		.addr1 = 0x0000,
145*81b20cccSMichael Schwingen 		.addr2 = 0x0000
146*81b20cccSMichael Schwingen 	}
147*81b20cccSMichael Schwingen };
148*81b20cccSMichael Schwingen 
149*81b20cccSMichael Schwingen 
150*81b20cccSMichael Schwingen struct amd_flash_info {
151*81b20cccSMichael Schwingen 	const __u16 mfr_id;
152*81b20cccSMichael Schwingen 	const __u16 dev_id;
153*81b20cccSMichael Schwingen 	const char *name;
154*81b20cccSMichael Schwingen 	const int DevSize;
155*81b20cccSMichael Schwingen 	const int NumEraseRegions;
156*81b20cccSMichael Schwingen 	const int CmdSet;
157*81b20cccSMichael Schwingen 	const __u8 uaddr[4];		/* unlock addrs for 8, 16, 32, 64 */
158*81b20cccSMichael Schwingen 	const ulong regions[6];
159*81b20cccSMichael Schwingen };
160*81b20cccSMichael Schwingen 
161*81b20cccSMichael Schwingen #define ERASEINFO(size,blocks) (size<<8)|(blocks-1)
162*81b20cccSMichael Schwingen 
163*81b20cccSMichael Schwingen #define SIZE_64KiB  16
164*81b20cccSMichael Schwingen #define SIZE_128KiB 17
165*81b20cccSMichael Schwingen #define SIZE_256KiB 18
166*81b20cccSMichael Schwingen #define SIZE_512KiB 19
167*81b20cccSMichael Schwingen #define SIZE_1MiB   20
168*81b20cccSMichael Schwingen #define SIZE_2MiB   21
169*81b20cccSMichael Schwingen #define SIZE_4MiB   22
170*81b20cccSMichael Schwingen #define SIZE_8MiB   23
171*81b20cccSMichael Schwingen 
172*81b20cccSMichael Schwingen static const struct amd_flash_info jedec_table[] = {
173*81b20cccSMichael Schwingen #ifdef CFG_FLASH_LEGACY_256Kx8
174*81b20cccSMichael Schwingen 	{
175*81b20cccSMichael Schwingen 		.mfr_id		= MANUFACTURER_SST,
176*81b20cccSMichael Schwingen 		.dev_id		= SST39LF020,
177*81b20cccSMichael Schwingen 		.name		= "SST 39LF020",
178*81b20cccSMichael Schwingen  		.uaddr		= {
179*81b20cccSMichael Schwingen 			[0] = MTD_UADDR_0x5555_0x2AAA /* x8 */
180*81b20cccSMichael Schwingen 		},
181*81b20cccSMichael Schwingen 		.DevSize	= SIZE_256KiB,
182*81b20cccSMichael Schwingen 		.CmdSet		= P_ID_AMD_STD,
183*81b20cccSMichael Schwingen 		.NumEraseRegions= 1,
184*81b20cccSMichael Schwingen 		.regions	= {
185*81b20cccSMichael Schwingen 			ERASEINFO(0x01000,64),
186*81b20cccSMichael Schwingen 		}
187*81b20cccSMichael Schwingen         },
188*81b20cccSMichael Schwingen #endif
189*81b20cccSMichael Schwingen #ifdef CFG_FLASH_LEGACY_512Kx8
190*81b20cccSMichael Schwingen 	{
191*81b20cccSMichael Schwingen 		.mfr_id		= MANUFACTURER_AMD,
192*81b20cccSMichael Schwingen 		.dev_id		= AM29LV040B,
193*81b20cccSMichael Schwingen 		.name		= "AMD AM29LV040B",
194*81b20cccSMichael Schwingen 		.uaddr		= {
195*81b20cccSMichael Schwingen 			[0] = MTD_UADDR_0x0555_0x02AA /* x8 */
196*81b20cccSMichael Schwingen 		},
197*81b20cccSMichael Schwingen 		.DevSize	= SIZE_512KiB,
198*81b20cccSMichael Schwingen 		.CmdSet		= P_ID_AMD_STD,
199*81b20cccSMichael Schwingen 		.NumEraseRegions= 1,
200*81b20cccSMichael Schwingen 		.regions	= {
201*81b20cccSMichael Schwingen 			ERASEINFO(0x10000,8),
202*81b20cccSMichael Schwingen 		}
203*81b20cccSMichael Schwingen 	},
204*81b20cccSMichael Schwingen 	{
205*81b20cccSMichael Schwingen 		.mfr_id		= MANUFACTURER_SST,
206*81b20cccSMichael Schwingen 		.dev_id		= SST39LF040,
207*81b20cccSMichael Schwingen 		.name		= "SST 39LF040",
208*81b20cccSMichael Schwingen  		.uaddr		= {
209*81b20cccSMichael Schwingen 			[0] = MTD_UADDR_0x5555_0x2AAA /* x8 */
210*81b20cccSMichael Schwingen 		},
211*81b20cccSMichael Schwingen 		.DevSize	= SIZE_512KiB,
212*81b20cccSMichael Schwingen 		.CmdSet		= P_ID_AMD_STD,
213*81b20cccSMichael Schwingen 		.NumEraseRegions= 1,
214*81b20cccSMichael Schwingen 		.regions	= {
215*81b20cccSMichael Schwingen 			ERASEINFO(0x01000,128),
216*81b20cccSMichael Schwingen 		}
217*81b20cccSMichael Schwingen         },
218*81b20cccSMichael Schwingen #endif
219*81b20cccSMichael Schwingen };
220*81b20cccSMichael Schwingen 
221*81b20cccSMichael Schwingen 
222*81b20cccSMichael Schwingen #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
223*81b20cccSMichael Schwingen 
224*81b20cccSMichael Schwingen 
225*81b20cccSMichael Schwingen static inline void fill_info(flash_info_t *info, const struct amd_flash_info *jedec_entry, ulong base)
226*81b20cccSMichael Schwingen {
227*81b20cccSMichael Schwingen 	int i,j;
228*81b20cccSMichael Schwingen 	int sect_cnt;
229*81b20cccSMichael Schwingen 	int size_ratio;
230*81b20cccSMichael Schwingen 	int total_size;
231*81b20cccSMichael Schwingen 	enum uaddr uaddr_idx;
232*81b20cccSMichael Schwingen 
233*81b20cccSMichael Schwingen 	size_ratio = info->portwidth / info->chipwidth;
234*81b20cccSMichael Schwingen 
235*81b20cccSMichael Schwingen 	debug("Found JEDEC Flash: %s\n", jedec_entry->name);
236*81b20cccSMichael Schwingen 	info->vendor = jedec_entry->CmdSet;
237*81b20cccSMichael Schwingen 	/* Todo: do we need device-specific timeouts? */
238*81b20cccSMichael Schwingen 	info->erase_blk_tout = 30000;
239*81b20cccSMichael Schwingen 	info->buffer_write_tout = 1000;
240*81b20cccSMichael Schwingen 	info->write_tout = 100;
241*81b20cccSMichael Schwingen 	info->name = jedec_entry->name;
242*81b20cccSMichael Schwingen 
243*81b20cccSMichael Schwingen 	/* copy unlock addresses from device table to CFI info struct. This
244*81b20cccSMichael Schwingen 	   is just here because the addresses are in the table anyway - if
245*81b20cccSMichael Schwingen 	   the flash is not detected due to wrong unlock addresses,
246*81b20cccSMichael Schwingen 	   flash_detect_legacy would have to try all of them before we even
247*81b20cccSMichael Schwingen 	   get here. */
248*81b20cccSMichael Schwingen 	switch(info->chipwidth) {
249*81b20cccSMichael Schwingen 	case FLASH_CFI_8BIT:
250*81b20cccSMichael Schwingen 		uaddr_idx = jedec_entry->uaddr[0];
251*81b20cccSMichael Schwingen 		break;
252*81b20cccSMichael Schwingen 	case FLASH_CFI_16BIT:
253*81b20cccSMichael Schwingen 		uaddr_idx = jedec_entry->uaddr[1];
254*81b20cccSMichael Schwingen 		break;
255*81b20cccSMichael Schwingen 	case FLASH_CFI_32BIT:
256*81b20cccSMichael Schwingen 		uaddr_idx = jedec_entry->uaddr[2];
257*81b20cccSMichael Schwingen 		break;
258*81b20cccSMichael Schwingen 	default:
259*81b20cccSMichael Schwingen 		uaddr_idx = MTD_UADDR_NOT_SUPPORTED;
260*81b20cccSMichael Schwingen 		break;
261*81b20cccSMichael Schwingen 	}
262*81b20cccSMichael Schwingen 
263*81b20cccSMichael Schwingen 	debug("unlock address index %d\n", uaddr_idx);
264*81b20cccSMichael Schwingen 	info->addr_unlock1 = unlock_addrs[uaddr_idx].addr1;
265*81b20cccSMichael Schwingen 	info->addr_unlock2 = unlock_addrs[uaddr_idx].addr2;
266*81b20cccSMichael Schwingen 	debug("unlock addresses are 0x%x/0x%x\n", info->addr_unlock1, info->addr_unlock2);
267*81b20cccSMichael Schwingen 
268*81b20cccSMichael Schwingen 	sect_cnt = 0;
269*81b20cccSMichael Schwingen 	total_size = 0;
270*81b20cccSMichael Schwingen 	for (i = 0; i < jedec_entry->NumEraseRegions; i++) {
271*81b20cccSMichael Schwingen 		ulong erase_region_size = jedec_entry->regions[i] >> 8;
272*81b20cccSMichael Schwingen 		ulong erase_region_count = (jedec_entry->regions[i] & 0xff) + 1;
273*81b20cccSMichael Schwingen 
274*81b20cccSMichael Schwingen 		total_size += erase_region_size * erase_region_count;
275*81b20cccSMichael Schwingen 		debug ("erase_region_count = %d erase_region_size = %d\n",
276*81b20cccSMichael Schwingen 		       erase_region_count, erase_region_size);
277*81b20cccSMichael Schwingen 		for (j = 0; j < erase_region_count; j++) {
278*81b20cccSMichael Schwingen 			if (sect_cnt >= CFG_MAX_FLASH_SECT) {
279*81b20cccSMichael Schwingen 				printf("ERROR: too many flash sectors\n");
280*81b20cccSMichael Schwingen 				break;
281*81b20cccSMichael Schwingen 			}
282*81b20cccSMichael Schwingen 			info->start[sect_cnt] = base;
283*81b20cccSMichael Schwingen 			base += (erase_region_size * size_ratio);
284*81b20cccSMichael Schwingen 			sect_cnt++;
285*81b20cccSMichael Schwingen 		}
286*81b20cccSMichael Schwingen 	}
287*81b20cccSMichael Schwingen 	info->sector_count = sect_cnt;
288*81b20cccSMichael Schwingen 	info->size = total_size * size_ratio;
289*81b20cccSMichael Schwingen }
290*81b20cccSMichael Schwingen 
291*81b20cccSMichael Schwingen /*-----------------------------------------------------------------------
292*81b20cccSMichael Schwingen  * match jedec ids against table. If a match is found, fill flash_info entry
293*81b20cccSMichael Schwingen  */
294*81b20cccSMichael Schwingen int jedec_flash_match(flash_info_t *info, ulong base)
295*81b20cccSMichael Schwingen {
296*81b20cccSMichael Schwingen 	int ret = 0;
297*81b20cccSMichael Schwingen 	int i;
298*81b20cccSMichael Schwingen 	ulong mask = 0xFFFF;
299*81b20cccSMichael Schwingen 	if (info->chipwidth == 1)
300*81b20cccSMichael Schwingen 		mask = 0xFF;
301*81b20cccSMichael Schwingen 
302*81b20cccSMichael Schwingen 	for (i = 0; i < ARRAY_SIZE(jedec_table); i++) {
303*81b20cccSMichael Schwingen 		if ((jedec_table[i].mfr_id & mask) == (info->manufacturer_id & mask) &&
304*81b20cccSMichael Schwingen 		    (jedec_table[i].dev_id & mask) == (info->device_id & mask)) {
305*81b20cccSMichael Schwingen 			fill_info(info, &jedec_table[i], base);
306*81b20cccSMichael Schwingen 			ret = 1;
307*81b20cccSMichael Schwingen 			break;
308*81b20cccSMichael Schwingen 		}
309*81b20cccSMichael Schwingen 	}
310*81b20cccSMichael Schwingen 	return ret;
311*81b20cccSMichael Schwingen }
312*81b20cccSMichael Schwingen 
313