Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 1 | /* |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 2 | * This file is part of the flashrom project. |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 3 | * |
Uwe Hermann | d22a1d4 | 2007-09-09 20:21:05 +0000 | [diff] [blame] | 4 | * Copyright (C) 2000 Silicon Integrated System Corporation |
| 5 | * Copyright (C) 2006 Giampiero Giancipoli <gianci@email.it> |
| 6 | * Copyright (C) 2006 coresystems GmbH <info@coresystems.de> |
Carl-Daniel Hailfinger | ae8afa9 | 2007-12-31 01:49:00 +0000 | [diff] [blame] | 7 | * Copyright (C) 2007 Carl-Daniel Hailfinger |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 8 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 13 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 18 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 25 | #include <stdint.h> |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 26 | #include "flash.h" |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 27 | |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 28 | #define MAX_REFLASH_TRIES 0x10 |
| 29 | |
Carl-Daniel Hailfinger | a758f51 | 2008-05-14 12:03:06 +0000 | [diff] [blame] | 30 | /* Check one byte for odd parity */ |
| 31 | uint8_t oddparity(uint8_t val) |
| 32 | { |
| 33 | val = (val ^ (val >> 4)) & 0xf; |
| 34 | val = (val ^ (val >> 2)) & 0x3; |
| 35 | return (val ^ (val >> 1)) & 0x1; |
| 36 | } |
| 37 | |
Uwe Hermann | 51582f2 | 2007-08-23 10:20:40 +0000 | [diff] [blame] | 38 | void toggle_ready_jedec(volatile uint8_t *dst) |
| 39 | { |
| 40 | unsigned int i = 0; |
| 41 | uint8_t tmp1, tmp2; |
| 42 | |
| 43 | tmp1 = *dst & 0x40; |
| 44 | |
| 45 | while (i++ < 0xFFFFFFF) { |
| 46 | tmp2 = *dst & 0x40; |
| 47 | if (tmp1 == tmp2) { |
| 48 | break; |
| 49 | } |
| 50 | tmp1 = tmp2; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void data_polling_jedec(volatile uint8_t *dst, uint8_t data) |
| 55 | { |
| 56 | unsigned int i = 0; |
| 57 | uint8_t tmp; |
| 58 | |
| 59 | data &= 0x80; |
| 60 | |
| 61 | while (i++ < 0xFFFFFFF) { |
| 62 | tmp = *dst & 0x80; |
| 63 | if (tmp == data) { |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void unprotect_jedec(volatile uint8_t *bios) |
| 70 | { |
| 71 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
| 72 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
| 73 | *(volatile uint8_t *)(bios + 0x5555) = 0x80; |
| 74 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
| 75 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
| 76 | *(volatile uint8_t *)(bios + 0x5555) = 0x20; |
| 77 | |
| 78 | usleep(200); |
| 79 | } |
| 80 | |
| 81 | void protect_jedec(volatile uint8_t *bios) |
| 82 | { |
| 83 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
| 84 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
| 85 | *(volatile uint8_t *)(bios + 0x5555) = 0xA0; |
| 86 | |
| 87 | usleep(200); |
| 88 | } |
| 89 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 90 | int probe_jedec(struct flashchip *flash) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 91 | { |
Stefan Reinauer | ce53297 | 2007-05-23 17:20:56 +0000 | [diff] [blame] | 92 | volatile uint8_t *bios = flash->virtual_memory; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 93 | uint8_t id1, id2; |
Carl-Daniel Hailfinger | ae8afa9 | 2007-12-31 01:49:00 +0000 | [diff] [blame] | 94 | uint32_t largeid1, largeid2; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 95 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 96 | /* Issue JEDEC Product ID Entry command */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 97 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 98 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 99 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 100 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 101 | *(volatile uint8_t *)(bios + 0x5555) = 0x90; |
Carl-Daniel Hailfinger | 03d2826 | 2007-11-13 14:56:54 +0000 | [diff] [blame] | 102 | /* Older chips may need up to 100 us to respond. The ATMEL 29C020 |
| 103 | * needs 10 ms according to the data sheet, but it has been tested |
Peter Stuge | bc0d857 | 2008-06-21 01:02:20 +0000 | [diff] [blame] | 104 | * to work reliably with 2 ms. |
Carl-Daniel Hailfinger | 03d2826 | 2007-11-13 14:56:54 +0000 | [diff] [blame] | 105 | */ |
Peter Stuge | 6a9fd1d | 2008-06-21 00:19:52 +0000 | [diff] [blame] | 106 | myusec_delay(2000); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 107 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 108 | /* Read product ID */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 109 | id1 = *(volatile uint8_t *)bios; |
| 110 | id2 = *(volatile uint8_t *)(bios + 0x01); |
Carl-Daniel Hailfinger | ae8afa9 | 2007-12-31 01:49:00 +0000 | [diff] [blame] | 111 | largeid1 = id1; |
| 112 | largeid2 = id2; |
| 113 | |
| 114 | /* Check if it is a continuation ID, this should be a while loop. */ |
| 115 | if (id1 == 0x7F) { |
| 116 | largeid1 <<= 8; |
| 117 | id1 = *(volatile uint8_t *)(bios + 0x100); |
| 118 | largeid1 |= id1; |
| 119 | } |
| 120 | if (id2 == 0x7F) { |
| 121 | largeid2 <<= 8; |
| 122 | id2 = *(volatile uint8_t *)(bios + 0x101); |
| 123 | largeid2 |= id2; |
| 124 | } |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 125 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 126 | /* Issue JEDEC Product ID Exit command */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 127 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 128 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 129 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 130 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 131 | *(volatile uint8_t *)(bios + 0x5555) = 0xF0; |
Carl-Daniel Hailfinger | 03d2826 | 2007-11-13 14:56:54 +0000 | [diff] [blame] | 132 | myusec_delay(40); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 133 | |
Carl-Daniel Hailfinger | a758f51 | 2008-05-14 12:03:06 +0000 | [diff] [blame] | 134 | printf_debug("%s: id1 0x%x, id2 0x%x", __FUNCTION__, largeid1, largeid2); |
| 135 | if (!oddparity(id1)) |
| 136 | printf_debug(", id1 parity violation"); |
| 137 | printf_debug("\n"); |
Carl-Daniel Hailfinger | ae8afa9 | 2007-12-31 01:49:00 +0000 | [diff] [blame] | 138 | if (largeid1 == flash->manufacture_id && largeid2 == flash->model_id) |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 139 | return 1; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 140 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 141 | return 0; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 144 | int erase_sector_jedec(volatile uint8_t *bios, unsigned int page) |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 145 | { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 146 | /* Issue the Sector Erase command */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 147 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 148 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 149 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 150 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 151 | *(volatile uint8_t *)(bios + 0x5555) = 0x80; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 152 | myusec_delay(10); |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 153 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 154 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 155 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 156 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 157 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 158 | *(volatile uint8_t *)(bios + page) = 0x30; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 159 | myusec_delay(10); |
| 160 | |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 161 | /* wait for Toggle bit ready */ |
| 162 | toggle_ready_jedec(bios); |
| 163 | |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 164 | return 0; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 165 | } |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 166 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 167 | int erase_block_jedec(volatile uint8_t *bios, unsigned int block) |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 168 | { |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 169 | /* Issue the Sector Erase command */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 170 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 171 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 172 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 173 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 174 | *(volatile uint8_t *)(bios + 0x5555) = 0x80; |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 175 | myusec_delay(10); |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 176 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 177 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 178 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 179 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 180 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 181 | *(volatile uint8_t *)(bios + block) = 0x50; |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 182 | myusec_delay(10); |
| 183 | |
| 184 | /* wait for Toggle bit ready */ |
| 185 | toggle_ready_jedec(bios); |
| 186 | |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 187 | return 0; |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 188 | } |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 189 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 190 | int erase_chip_jedec(struct flashchip *flash) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 191 | { |
Stefan Reinauer | ce53297 | 2007-05-23 17:20:56 +0000 | [diff] [blame] | 192 | volatile uint8_t *bios = flash->virtual_memory; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 193 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 194 | /* Issue the JEDEC Chip Erase command */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 195 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 196 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 197 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 198 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 199 | *(volatile uint8_t *)(bios + 0x5555) = 0x80; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 200 | myusec_delay(10); |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 201 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 202 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 203 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 204 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 205 | myusec_delay(10); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 206 | *(volatile uint8_t *)(bios + 0x5555) = 0x10; |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 207 | myusec_delay(10); |
| 208 | |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 209 | toggle_ready_jedec(bios); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 210 | |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 211 | return 0; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 214 | int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src, |
| 215 | volatile uint8_t *dst, int page_size) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 216 | { |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 217 | int i, tried = 0, start_index = 0, ok; |
| 218 | volatile uint8_t *d = dst; |
| 219 | uint8_t *s = src; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 220 | |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 221 | retry: |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 222 | /* Issue JEDEC Data Unprotect comand */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 223 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
| 224 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
| 225 | *(volatile uint8_t *)(bios + 0x5555) = 0xA0; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 226 | |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 227 | /* transfer data from source to destination */ |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 228 | for (i = start_index; i < page_size; i++) { |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 229 | /* If the data is 0xFF, don't program it */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 230 | if (*src != 0xFF) |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 231 | *dst = *src; |
| 232 | dst++; |
| 233 | src++; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 236 | toggle_ready_jedec(dst - 1); |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 237 | |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 238 | dst = d; |
| 239 | src = s; |
| 240 | ok = 1; |
| 241 | for (i = 0; i < page_size; i++) { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 242 | if (*dst != *src) { |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 243 | ok = 0; |
| 244 | break; |
| 245 | } |
| 246 | dst++; |
| 247 | src++; |
| 248 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 249 | |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 250 | if (!ok && tried++ < MAX_REFLASH_TRIES) { |
| 251 | start_index = i; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 252 | goto retry; |
| 253 | } |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 254 | if (!ok) { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 255 | fprintf(stderr, " page %d failed!\n", |
| 256 | (unsigned int)(d - bios) / page_size); |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 257 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 258 | return !ok; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 261 | int write_byte_program_jedec(volatile uint8_t *bios, uint8_t *src, |
| 262 | volatile uint8_t *dst) |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 263 | { |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 264 | int tried = 0, ok = 1; |
Ollie Lho | 1b8b660 | 2004-12-08 02:10:33 +0000 | [diff] [blame] | 265 | |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 266 | /* If the data is 0xFF, don't program it */ |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 267 | if (*src == 0xFF) { |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 268 | return -1; |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 269 | } |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 270 | |
Ollie Lho | 1b8b660 | 2004-12-08 02:10:33 +0000 | [diff] [blame] | 271 | retry: |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 272 | /* Issue JEDEC Byte Program command */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 273 | *(volatile uint8_t *)(bios + 0x5555) = 0xAA; |
| 274 | *(volatile uint8_t *)(bios + 0x2AAA) = 0x55; |
| 275 | *(volatile uint8_t *)(bios + 0x5555) = 0xA0; |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 276 | |
| 277 | /* transfer data from source to destination */ |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 278 | *dst = *src; |
| 279 | toggle_ready_jedec(bios); |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 280 | |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 281 | if (*dst != *src && tried++ < MAX_REFLASH_TRIES) { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 282 | goto retry; |
| 283 | } |
Ollie Lho | 1b8b660 | 2004-12-08 02:10:33 +0000 | [diff] [blame] | 284 | |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 285 | if (tried >= MAX_REFLASH_TRIES) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 286 | ok = 0; |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 287 | |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 288 | return !ok; |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 291 | int write_sector_jedec(volatile uint8_t *bios, uint8_t *src, |
| 292 | volatile uint8_t *dst, unsigned int page_size) |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 293 | { |
| 294 | int i; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 295 | |
| 296 | for (i = 0; i < page_size; i++) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 297 | write_byte_program_jedec(bios, src, dst); |
| 298 | dst++, src++; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 301 | return 0; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 304 | int write_jedec(struct flashchip *flash, uint8_t *buf) |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 305 | { |
| 306 | int i; |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 307 | int total_size = flash->total_size * 1024; |
| 308 | int page_size = flash->page_size; |
Stefan Reinauer | ce53297 | 2007-05-23 17:20:56 +0000 | [diff] [blame] | 309 | volatile uint8_t *bios = flash->virtual_memory; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 310 | |
| 311 | erase_chip_jedec(flash); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 312 | // dumb check if erase was successful. |
| 313 | for (i = 0; i < total_size; i++) { |
| 314 | if (bios[i] != (uint8_t) 0xff) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 315 | printf("ERASE FAILED @%d, val %02x!\n", i, bios[i]); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 316 | return -1; |
| 317 | } |
| 318 | } |
Giampiero Giancipoli | 8c5299f | 2006-11-22 00:29:51 +0000 | [diff] [blame] | 319 | |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 320 | printf("Programming page: "); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 321 | for (i = 0; i < total_size / page_size; i++) { |
| 322 | printf("%04d at address: 0x%08x", i, i * page_size); |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 323 | write_page_write_jedec(bios, buf + i * page_size, |
| 324 | bios + i * page_size, page_size); |
Ollie Lho | 070647d | 2004-03-22 22:19:17 +0000 | [diff] [blame] | 325 | 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] | 326 | } |
| 327 | printf("\n"); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 328 | protect_jedec(bios); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 329 | |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 330 | return 0; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 331 | } |