blob: 4042e8bf9677b6cd70a0d3ffe1496a41037b97b6 [file] [log] [blame]
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00001/*
2 * sst28sf040.c: driver for SST28SF040C flash models.
3 *
4 *
5 * Copyright 2000 Silicon Integrated System Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 *
22 * Reference:
23 * 4 MEgabit (512K x 8) SuperFlash EEPROM, SST28SF040 data sheet
24 *
25 * $Id$
26 */
27
28#include "flash.h"
29#include "jedec.h"
30
31#define AUTO_PG_ERASE1 0x20
32#define AUTO_PG_ERASE2 0xD0
33#define AUTO_PGRM 0x10
34#define CHIP_ERASE 0x30
35#define RESET 0xFF
36#define READ_ID 0x90
37
Ronald G. Minnichef5779d2002-01-29 20:18:02 +000038static __inline__ void protect_28sf040 (volatile char * bios)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000039{
40 /* ask compiler not to optimize this */
41 volatile unsigned char tmp;
42
Ronald G. Minnichef5779d2002-01-29 20:18:02 +000043 tmp = *(volatile unsigned char *) (bios + 0x1823);
44 tmp = *(volatile unsigned char *) (bios + 0x1820);
45 tmp = *(volatile unsigned char *) (bios + 0x1822);
46 tmp = *(volatile unsigned char *) (bios + 0x0418);
47 tmp = *(volatile unsigned char *) (bios + 0x041B);
48 tmp = *(volatile unsigned char *) (bios + 0x0419);
49 tmp = *(volatile unsigned char *) (bios + 0x040A);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000050}
51
Ronald G. Minnichef5779d2002-01-29 20:18:02 +000052static __inline__ void unprotect_28sf040 (volatile char * bios)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000053{
54 /* ask compiler not to optimize this */
55 volatile unsigned char tmp;
56
Ronald G. Minnichef5779d2002-01-29 20:18:02 +000057 tmp = *(volatile unsigned char *) (bios + 0x1823);
58 tmp = *(volatile unsigned char *) (bios + 0x1820);
59 tmp = *(volatile unsigned char *) (bios + 0x1822);
60 tmp = *(volatile unsigned char *) (bios + 0x0418);
61 tmp = *(volatile unsigned char *) (bios + 0x041B);
62 tmp = *(volatile unsigned char *) (bios + 0x0419);
63 tmp = *(volatile unsigned char *) (bios + 0x041A);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000064}
65
Ronald G. Minnichef5779d2002-01-29 20:18:02 +000066static __inline__ erase_sector_28sf040 (volatile char * bios, unsigned long address)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000067{
68 *bios = AUTO_PG_ERASE1;
69 *(bios + address) = AUTO_PG_ERASE2;
70
71 /* wait for Toggle bit ready */
72 toggle_ready_jedec(bios);
73}
74
Ronald G. Minnichef5779d2002-01-29 20:18:02 +000075static __inline__ write_sector_28sf040(volatile char * bios, unsigned char * src,
76 volatile unsigned char * dst, unsigned int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000077{
78 int i;
79
80 for (i = 0; i < page_size; i++) {
81 /* transfer data from source to destination */
82 if (*src == 0xFF) {
83 dst++, src++;
84 /* If the data is 0xFF, don't program it */
85 continue;
86 }
87 /*issue AUTO PROGRAM command */
88 *dst = AUTO_PGRM;
89 *dst++ = *src++;
90
91 /* wait for Toggle bit ready */
92 toggle_ready_jedec(bios);
93 }
94}
95
96int probe_28sf040 (struct flashchip * flash)
97{
Ronald G. Minnichef5779d2002-01-29 20:18:02 +000098 volatile char * bios = flash->virt_addr;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000099 unsigned char id1, id2, tmp;
100
101 /* save the value at the beginning of the Flash */
102 tmp = *bios;
103
104 *bios = RESET;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000105 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000106
107 *bios = READ_ID;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000108 myusec_delay(10);
109 id1 = *(volatile unsigned char *) bios;
110 myusec_delay(10);
111 id2 = *(volatile unsigned char *) (bios + 0x01);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000112
113 *bios = RESET;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000114 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000115
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000116 printf(__FUNCTION__ "id1 %d, id2 %d\n", id1, id2);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000117 if (id1 == flash->manufacture_id && id2 == flash->model_id)
118 return 1;
119
120 /* if there is no SST28SF040, restore the original value */
121 *bios = tmp;
122 return 0;
123}
124
125int erase_28sf040 (struct flashchip * flash)
126{
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000127 volatile char * bios = flash->virt_addr;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000128
129 unprotect_28sf040 (bios);
130 *bios = CHIP_ERASE;
131 *bios = CHIP_ERASE;
132 protect_28sf040 (bios);
133
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000134 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000135 toggle_ready_jedec(bios);
136}
137
138int write_28sf040 (struct flashchip * flash, char * buf)
139{
140 int i;
141 int total_size = flash->total_size * 1024, page_size = flash->page_size;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000142 volatile char * bios = flash->virt_addr;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000143
144 unprotect_28sf040 (bios);
145
146 printf ("Programming Page: ");
147 for (i = 0; i < total_size/page_size; i++) {
148 /* erase the page before programming */
149 erase_sector_28sf040(bios, i * page_size);
150
151 /* write to the sector */
152 printf ("%04d at address: 0x%08x", i, i * page_size);
153 write_sector_28sf040(bios, buf + i * page_size, bios + i * page_size,
154 page_size);
155 printf ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
156 }
157 printf("\n");
158
159 protect_28sf040 (bios);
160}