Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jedec.c: driver for programming JEDEC standard flash parts |
| 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 | * |
| 24 | * $Id$ |
| 25 | */ |
| 26 | |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 28 | #include "flash.h" |
| 29 | #include "jedec.h" |
| 30 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 31 | int probe_jedec(struct flashchip *flash) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 32 | { |
Ollie Lho | afdfce8 | 2004-03-27 00:31:03 +0000 | [diff] [blame] | 33 | volatile unsigned char *bios = flash->virt_addr; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 34 | unsigned char id1, id2; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 35 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 36 | /* Issue JEDEC Product ID Entry command */ |
| 37 | *(volatile char *) (bios + 0x5555) = 0xAA; |
| 38 | myusec_delay(10); |
| 39 | *(volatile char *) (bios + 0x2AAA) = 0x55; |
| 40 | myusec_delay(10); |
| 41 | *(volatile char *) (bios + 0x5555) = 0x90; |
| 42 | myusec_delay(10); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 43 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 44 | /* Read product ID */ |
| 45 | id1 = *(volatile unsigned char *) bios; |
| 46 | id2 = *(volatile unsigned char *) (bios + 0x01); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 47 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 48 | /* Issue JEDEC Product ID Exit command */ |
| 49 | *(volatile char *) (bios + 0x5555) = 0xAA; |
| 50 | myusec_delay(10); |
| 51 | *(volatile char *) (bios + 0x2AAA) = 0x55; |
| 52 | myusec_delay(10); |
| 53 | *(volatile char *) (bios + 0x5555) = 0xF0; |
| 54 | myusec_delay(10); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 55 | |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 56 | printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 57 | if (id1 == flash->manufacture_id && id2 == flash->model_id) |
| 58 | return 1; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 59 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 60 | return 0; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Ollie Lho | afdfce8 | 2004-03-27 00:31:03 +0000 | [diff] [blame] | 63 | int erase_sector_jedec(volatile unsigned char *bios, unsigned int page) |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 64 | { |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 65 | volatile unsigned char *Temp; |
| 66 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 67 | /* Issue the Sector Erase command */ |
| 68 | Temp = bios + 0x5555; /* set up address to be BASE:5555h */ |
| 69 | *Temp = 0xAA; /* write data 0xAA to the address */ |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 70 | myusec_delay(10); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 71 | Temp = bios + 0x2AAA; /* set up address to be BASE:2AAAh */ |
| 72 | *Temp = 0x55; /* write data 0x55 to the address */ |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 73 | myusec_delay(10); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 74 | Temp = bios + 0x5555; /* set up address to be BASE:5555h */ |
| 75 | *Temp = 0x80; /* write data 0x80 to the address */ |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 76 | myusec_delay(10); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 77 | Temp = bios + 0x5555; /* set up address to be BASE:5555h */ |
| 78 | *Temp = 0xAA; /* write data 0xAA to the address */ |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 79 | myusec_delay(10); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 80 | Temp = bios + 0x2AAA; /* set up address to be BASE:2AAAh */ |
| 81 | *Temp = 0x55; /* write data 0x55 to the address */ |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 82 | myusec_delay(10); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 83 | Temp = bios + page; /* set up address to be the current sector */ |
| 84 | *Temp = 0x30; /* write data 0x30 to the address */ |
| 85 | myusec_delay(10); |
| 86 | |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 87 | /* wait for Toggle bit ready */ |
| 88 | toggle_ready_jedec(bios); |
| 89 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 90 | return (0); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 91 | } |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame^] | 92 | |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 93 | int erase_block_jedec(volatile unsigned char *bios, unsigned int block) |
| 94 | { |
| 95 | volatile unsigned char *Temp; |
| 96 | |
| 97 | /* Issue the Sector Erase command */ |
| 98 | Temp = bios + 0x5555; /* set up address to be BASE:5555h */ |
| 99 | *Temp = 0xAA; /* write data 0xAA to the address */ |
| 100 | myusec_delay(10); |
| 101 | Temp = bios + 0x2AAA; /* set up address to be BASE:2AAAh */ |
| 102 | *Temp = 0x55; /* write data 0x55 to the address */ |
| 103 | myusec_delay(10); |
| 104 | Temp = bios + 0x5555; /* set up address to be BASE:5555h */ |
| 105 | *Temp = 0x80; /* write data 0x80 to the address */ |
| 106 | myusec_delay(10); |
| 107 | Temp = bios + 0x5555; /* set up address to be BASE:5555h */ |
| 108 | *Temp = 0xAA; /* write data 0xAA to the address */ |
| 109 | myusec_delay(10); |
| 110 | Temp = bios + 0x2AAA; /* set up address to be BASE:2AAAh */ |
| 111 | *Temp = 0x55; /* write data 0x55 to the address */ |
| 112 | myusec_delay(10); |
| 113 | Temp = bios + block; /* set up address to be the current sector */ |
| 114 | *Temp = 0x50; /* write data 0x30 to the address */ |
| 115 | myusec_delay(10); |
| 116 | |
| 117 | /* wait for Toggle bit ready */ |
| 118 | toggle_ready_jedec(bios); |
| 119 | |
| 120 | return (0); |
| 121 | } |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 122 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 123 | int erase_chip_jedec(struct flashchip *flash) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 124 | { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 125 | volatile unsigned char *bios = flash->virt_addr; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 126 | volatile unsigned char *Temp; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 127 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 128 | /* Issue the JEDEC Chip Erase command */ |
| 129 | Temp = bios + 0x5555; /* set up address to be BASE:5555h */ |
| 130 | *Temp = 0xAA; /* write data 0xAA to the address */ |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 131 | myusec_delay(10); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 132 | Temp = bios + 0x2AAA; /* set up address to be BASE:2AAAh */ |
| 133 | *Temp = 0x55; /* write data 0x55 to the address */ |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 134 | myusec_delay(10); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 135 | Temp = bios + 0x5555; /* set up address to be BASE:5555h */ |
| 136 | *Temp = 0x80; /* write data 0x80 to the address */ |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 137 | myusec_delay(10); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 138 | Temp = bios + 0x5555; /* set up address to be BASE:5555h */ |
| 139 | *Temp = 0xAA; /* write data 0xAA to the address */ |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 140 | myusec_delay(10); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 141 | Temp = bios + 0x2AAA; /* set up address to be BASE:2AAAh */ |
| 142 | *Temp = 0x55; /* write data 0x55 to the address */ |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 143 | myusec_delay(10); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 144 | Temp = bios + 0x5555; /* set up address to be BASEy:5555h */ |
| 145 | *Temp = 0x10; /* write data 0x10 to the address */ |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 146 | myusec_delay(10); |
| 147 | |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 148 | toggle_ready_jedec(bios); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 149 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 150 | return (0); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame^] | 153 | int write_page_write_jedec(volatile unsigned char *bios, unsigned char *src, |
| 154 | volatile unsigned char *dst, int page_size) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 155 | { |
| 156 | int i; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 157 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 158 | /* Issue JEDEC Data Unprotect comand */ |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame^] | 159 | *(volatile unsigned char *) (bios + 0x5555) = 0xAA; |
| 160 | *(volatile unsigned char *) (bios + 0x2AAA) = 0x55; |
| 161 | *(volatile unsigned char *) (bios + 0x5555) = 0xA0; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 162 | |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame^] | 163 | /* transfer data from source to destination */ |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 164 | for (i = 0; i < page_size; i++) { |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame^] | 165 | /* If the data is 0xFF, don't program it */ |
| 166 | if (*src == 0xFF) |
| 167 | continue; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 168 | *dst++ = *src++; |
| 169 | } |
| 170 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 171 | toggle_ready_jedec(dst - 1); |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame^] | 172 | |
| 173 | return 0; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 176 | int write_byte_program_jedec(volatile unsigned char *bios, unsigned char *src, |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 177 | volatile unsigned char *dst) |
| 178 | { |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame^] | 179 | /* If the data is 0xFF, don't program it */ |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 180 | if (*src == 0xFF) { |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 181 | return 0; |
| 182 | } |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame^] | 183 | |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 184 | /* Issue JEDEC Byte Program command */ |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame^] | 185 | *(volatile unsigned char *) (bios + 0x5555) = 0xAA; |
| 186 | *(volatile unsigned char *) (bios + 0x2AAA) = 0x55; |
| 187 | *(volatile unsigned char *) (bios + 0x5555) = 0xA0; |
| 188 | |
| 189 | /* transfer data from source to destination */ |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 190 | *dst = *src; |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame^] | 191 | |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 192 | toggle_ready_jedec(bios); |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 193 | |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | int write_sector_jedec(volatile unsigned char *bios, unsigned char *src, |
Ollie Lho | afdfce8 | 2004-03-27 00:31:03 +0000 | [diff] [blame] | 198 | volatile unsigned char *dst, unsigned int page_size) |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 199 | { |
| 200 | int i; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 201 | |
| 202 | for (i = 0; i < page_size; i++) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 203 | write_byte_program_jedec(bios, src, dst); |
| 204 | dst++, src++; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | return (0); |
| 208 | } |
| 209 | |
| 210 | int write_jedec(struct flashchip *flash, unsigned char *buf) |
| 211 | { |
| 212 | int i; |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 213 | int total_size = flash->total_size * 1024; |
| 214 | int page_size = flash->page_size; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 215 | volatile unsigned char *bios = flash->virt_addr; |
| 216 | |
| 217 | erase_chip_jedec(flash); |
| 218 | if (*bios != (unsigned char) 0xff) { |
Ronald G. Minnich | 5643942 | 2002-09-06 16:58:14 +0000 | [diff] [blame] | 219 | printf("ERASE FAILED\n"); |
| 220 | return -1; |
| 221 | } |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 222 | printf("Programming Page: "); |
| 223 | for (i = 0; i < total_size / page_size; i++) { |
| 224 | printf("%04d at address: 0x%08x", i, i * page_size); |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 225 | write_page_write_jedec(bios, buf + i * page_size, |
| 226 | bios + i * page_size, page_size); |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 227 | 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] | 228 | } |
| 229 | printf("\n"); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 230 | protect_jedec(bios); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 231 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 232 | return (0); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 233 | } |