blob: 7f14f49cc57f95285af0654060fa105955d54f72 [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
Ollie Lho184a4042005-11-26 21:55:36 +00006 * Copyright 2005 coresystems GmbH <stepan@openbios.org>
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 *
23 * Reference:
Stefan Reinauer7ed78c82007-05-24 09:26:39 +000024 * 4 Megabit (512K x 8) SuperFlash EEPROM, SST28SF040 data sheet
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000025 *
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000026 */
27
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000028#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000029#include <stdint.h>
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000030#include "flash.h"
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000031
32#define AUTO_PG_ERASE1 0x20
33#define AUTO_PG_ERASE2 0xD0
Ollie Lhocf29de82004-03-18 19:40:07 +000034#define AUTO_PGRM 0x10
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000035#define CHIP_ERASE 0x30
36#define RESET 0xFF
37#define READ_ID 0x90
38
Ollie Lho184a4042005-11-26 21:55:36 +000039static __inline__ void protect_28sf040(volatile uint8_t *bios)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000040{
41 /* ask compiler not to optimize this */
Ollie Lho184a4042005-11-26 21:55:36 +000042 volatile uint8_t tmp;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000043
Uwe Hermanna7e05482007-05-09 10:17:44 +000044 tmp = *(volatile uint8_t *)(bios + 0x1823);
45 tmp = *(volatile uint8_t *)(bios + 0x1820);
46 tmp = *(volatile uint8_t *)(bios + 0x1822);
47 tmp = *(volatile uint8_t *)(bios + 0x0418);
48 tmp = *(volatile uint8_t *)(bios + 0x041B);
49 tmp = *(volatile uint8_t *)(bios + 0x0419);
50 tmp = *(volatile uint8_t *)(bios + 0x040A);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000051}
52
Ollie Lho184a4042005-11-26 21:55:36 +000053static __inline__ void unprotect_28sf040(volatile uint8_t *bios)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000054{
55 /* ask compiler not to optimize this */
Ollie Lho184a4042005-11-26 21:55:36 +000056 volatile uint8_t tmp;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000057
Uwe Hermanna7e05482007-05-09 10:17:44 +000058 tmp = *(volatile uint8_t *)(bios + 0x1823);
59 tmp = *(volatile uint8_t *)(bios + 0x1820);
60 tmp = *(volatile uint8_t *)(bios + 0x1822);
61 tmp = *(volatile uint8_t *)(bios + 0x0418);
62 tmp = *(volatile uint8_t *)(bios + 0x041B);
63 tmp = *(volatile uint8_t *)(bios + 0x0419);
64 tmp = *(volatile uint8_t *)(bios + 0x041A);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000065}
66
Ollie Lho184a4042005-11-26 21:55:36 +000067static __inline__ int erase_sector_28sf040(volatile uint8_t *bios,
Ollie Lho761bf1b2004-03-20 16:46:10 +000068 unsigned long address)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000069{
70 *bios = AUTO_PG_ERASE1;
71 *(bios + address) = AUTO_PG_ERASE2;
72
73 /* wait for Toggle bit ready */
74 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000075
Uwe Hermannffec5f32007-08-23 16:08:21 +000076 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000077}
78
Ollie Lho184a4042005-11-26 21:55:36 +000079static __inline__ int write_sector_28sf040(volatile uint8_t *bios,
80 uint8_t *src,
81 volatile uint8_t *dst,
Ollie Lho761bf1b2004-03-20 16:46:10 +000082 unsigned int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000083{
84 int i;
85
86 for (i = 0; i < page_size; i++) {
87 /* transfer data from source to destination */
88 if (*src == 0xFF) {
89 dst++, src++;
90 /* If the data is 0xFF, don't program it */
91 continue;
92 }
93 /*issue AUTO PROGRAM command */
Ollie Lho761bf1b2004-03-20 16:46:10 +000094 *dst = AUTO_PGRM;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000095 *dst++ = *src++;
96
97 /* wait for Toggle bit ready */
98 toggle_ready_jedec(bios);
99 }
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000100
Uwe Hermannffec5f32007-08-23 16:08:21 +0000101 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000102}
103
Ollie Lho761bf1b2004-03-20 16:46:10 +0000104int probe_28sf040(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000105{
Stefan Reinauerce532972007-05-23 17:20:56 +0000106 volatile uint8_t *bios = flash->virtual_memory;
Ed Swierk966dc202007-08-13 04:10:32 +0000107 uint8_t id1, id2;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000108
109 *bios = RESET;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000110 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000111
112 *bios = READ_ID;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000113 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000114 id1 = *(volatile uint8_t *)bios;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000115 myusec_delay(10);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000116 id2 = *(volatile uint8_t *)(bios + 0x01);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000117
118 *bios = RESET;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000119 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000120
Ollie Lho184a4042005-11-26 21:55:36 +0000121 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000122 if (id1 == flash->manufacture_id && id2 == flash->model_id)
123 return 1;
124
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000125 return 0;
126}
127
Ollie Lho761bf1b2004-03-20 16:46:10 +0000128int erase_28sf040(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000129{
Stefan Reinauerce532972007-05-23 17:20:56 +0000130 volatile uint8_t *bios = flash->virtual_memory;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000131
Ollie Lho761bf1b2004-03-20 16:46:10 +0000132 unprotect_28sf040(bios);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000133 *bios = CHIP_ERASE;
134 *bios = CHIP_ERASE;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000135 protect_28sf040(bios);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000136
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000137 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000138 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000139
Uwe Hermannffec5f32007-08-23 16:08:21 +0000140 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000141}
142
Ollie Lho184a4042005-11-26 21:55:36 +0000143int write_28sf040(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000144{
145 int i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000146 int total_size = flash->total_size * 1024;
147 int page_size = flash->page_size;
Stefan Reinauerce532972007-05-23 17:20:56 +0000148 volatile uint8_t *bios = flash->virtual_memory;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000149
Ollie Lho761bf1b2004-03-20 16:46:10 +0000150 unprotect_28sf040(bios);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000151
Ollie Lho761bf1b2004-03-20 16:46:10 +0000152 printf("Programming Page: ");
153 for (i = 0; i < total_size / page_size; i++) {
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000154 /* erase the page before programming */
155 erase_sector_28sf040(bios, i * page_size);
156
157 /* write to the sector */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000158 printf("%04d at address: 0x%08x", i, i * page_size);
159 write_sector_28sf040(bios, buf + i * page_size,
160 bios + i * page_size, page_size);
Ollie Lho8b8897a2004-03-27 00:18:15 +0000161 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");
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000162 }
163 printf("\n");
164
Ollie Lho761bf1b2004-03-20 16:46:10 +0000165 protect_28sf040(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000166
Uwe Hermannffec5f32007-08-23 16:08:21 +0000167 return 0;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000168}