Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 29 | #include "flash.h" |
| 30 | #include "jedec.h" |
| 31 | |
| 32 | #define AUTO_PG_ERASE1 0x20 |
| 33 | #define AUTO_PG_ERASE2 0xD0 |
Ollie Lho | cf29de8 | 2004-03-18 19:40:07 +0000 | [diff] [blame] | 34 | #define AUTO_PGRM 0x10 |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 35 | #define CHIP_ERASE 0x30 |
| 36 | #define RESET 0xFF |
| 37 | #define READ_ID 0x90 |
| 38 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 39 | static __inline__ void protect_28sf040(volatile char *bios) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 40 | { |
| 41 | /* ask compiler not to optimize this */ |
| 42 | volatile unsigned char tmp; |
| 43 | |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 44 | tmp = *(volatile unsigned char *) (bios + 0x1823); |
| 45 | tmp = *(volatile unsigned char *) (bios + 0x1820); |
| 46 | tmp = *(volatile unsigned char *) (bios + 0x1822); |
| 47 | tmp = *(volatile unsigned char *) (bios + 0x0418); |
| 48 | tmp = *(volatile unsigned char *) (bios + 0x041B); |
| 49 | tmp = *(volatile unsigned char *) (bios + 0x0419); |
| 50 | tmp = *(volatile unsigned char *) (bios + 0x040A); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 53 | static __inline__ void unprotect_28sf040(volatile char *bios) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 54 | { |
| 55 | /* ask compiler not to optimize this */ |
| 56 | volatile unsigned char tmp; |
| 57 | |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 58 | tmp = *(volatile unsigned char *) (bios + 0x1823); |
| 59 | tmp = *(volatile unsigned char *) (bios + 0x1820); |
| 60 | tmp = *(volatile unsigned char *) (bios + 0x1822); |
| 61 | tmp = *(volatile unsigned char *) (bios + 0x0418); |
| 62 | tmp = *(volatile unsigned char *) (bios + 0x041B); |
| 63 | tmp = *(volatile unsigned char *) (bios + 0x0419); |
| 64 | tmp = *(volatile unsigned char *) (bios + 0x041A); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 67 | static __inline__ int erase_sector_28sf040(volatile char *bios, |
| 68 | unsigned long address) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 69 | { |
| 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. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 75 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 76 | return (0); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 79 | static __inline__ int write_sector_28sf040(volatile char *bios, |
| 80 | unsigned char *src, |
| 81 | volatile unsigned char *dst, |
| 82 | unsigned int page_size) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 83 | { |
| 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 Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 94 | *dst = AUTO_PGRM; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 95 | *dst++ = *src++; |
| 96 | |
| 97 | /* wait for Toggle bit ready */ |
| 98 | toggle_ready_jedec(bios); |
| 99 | } |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 100 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 101 | return (0); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 104 | int probe_28sf040(struct flashchip *flash) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 105 | { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 106 | volatile char *bios = flash->virt_addr; |
| 107 | unsigned char id1, id2, tmp; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 108 | |
| 109 | /* save the value at the beginning of the Flash */ |
| 110 | tmp = *bios; |
| 111 | |
| 112 | *bios = RESET; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 113 | myusec_delay(10); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 114 | |
| 115 | *bios = READ_ID; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 116 | myusec_delay(10); |
| 117 | id1 = *(volatile unsigned char *) bios; |
| 118 | myusec_delay(10); |
| 119 | id2 = *(volatile unsigned char *) (bios + 0x01); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 120 | |
| 121 | *bios = RESET; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 122 | myusec_delay(10); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 123 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 124 | printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 125 | if (id1 == flash->manufacture_id && id2 == flash->model_id) |
| 126 | return 1; |
| 127 | |
| 128 | /* if there is no SST28SF040, restore the original value */ |
| 129 | *bios = tmp; |
| 130 | return 0; |
| 131 | } |
| 132 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 133 | int erase_28sf040(struct flashchip *flash) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 134 | { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 135 | volatile char *bios = flash->virt_addr; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 136 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 137 | unprotect_28sf040(bios); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 138 | *bios = CHIP_ERASE; |
| 139 | *bios = CHIP_ERASE; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 140 | protect_28sf040(bios); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 141 | |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 142 | myusec_delay(10); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 143 | toggle_ready_jedec(bios); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 144 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 145 | return (0); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 148 | int write_28sf040(struct flashchip *flash, unsigned char *buf) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 149 | { |
| 150 | int i; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 151 | int total_size = flash->total_size * 1024, page_size = |
| 152 | flash->page_size; |
| 153 | volatile char *bios = flash->virt_addr; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 154 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 155 | unprotect_28sf040(bios); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 156 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 157 | printf("Programming Page: "); |
| 158 | for (i = 0; i < total_size / page_size; i++) { |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 159 | /* erase the page before programming */ |
| 160 | erase_sector_28sf040(bios, i * page_size); |
| 161 | |
| 162 | /* write to the sector */ |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 163 | printf("%04d at address: 0x%08x", i, i * page_size); |
| 164 | write_sector_28sf040(bios, buf + i * page_size, |
| 165 | bios + i * page_size, page_size); |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 166 | 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. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 167 | } |
| 168 | printf("\n"); |
| 169 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 170 | protect_28sf040(bios); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 171 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 172 | return (0); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 173 | } |