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 | |
| 31 | int probe_jedec (struct flashchip * flash) |
| 32 | { |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame^] | 33 | volatile char * bios = flash->virt_addr; |
| 34 | unsigned char id1, id2; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 35 | |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame^] | 36 | *(volatile char *) (bios + 0x5555) = 0xAA; |
| 37 | myusec_delay(10); |
| 38 | *(volatile char *) (bios + 0x2AAA) = 0x55; |
| 39 | myusec_delay(10); |
| 40 | *(volatile char *) (bios + 0x5555) = 0x90; |
| 41 | myusec_delay(10); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 42 | |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame^] | 43 | id1 = *(volatile unsigned char *) bios; |
| 44 | id2 = *(volatile unsigned char *) (bios + 0x01); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 45 | |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame^] | 46 | *(volatile char *) (bios + 0x5555) = 0xAA; |
| 47 | myusec_delay(10); |
| 48 | *(volatile char *) (bios + 0x2AAA) = 0x55; |
| 49 | myusec_delay(10); |
| 50 | *(volatile char *) (bios + 0x5555) = 0xF0; |
| 51 | myusec_delay(10); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 52 | |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 53 | printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2); |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame^] | 54 | if (id1 == flash->manufacture_id && id2 == flash->model_id) |
| 55 | return 1; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 56 | |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame^] | 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int erase_sector_jedec (volatile char * bios, unsigned int page) |
| 61 | { |
| 62 | /* Chip erase function does not exist for LPC mode on 49lf040. |
| 63 | * Erase sector-by-sector instead. */ |
| 64 | volatile unsigned char *Temp; |
| 65 | |
| 66 | /* Issue the Sector Erase command to 40LF040 */ |
| 67 | Temp = bios + 0x5555; /* set up address to be C000:5555h */ |
| 68 | *Temp = 0xAA; /* write data 0xAA to the address */ |
| 69 | myusec_delay(10); |
| 70 | Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */ |
| 71 | *Temp = 0x55; /* write data 0x55 to the address */ |
| 72 | myusec_delay(10); |
| 73 | Temp = bios + 0x5555; /* set up address to be C000:5555h */ |
| 74 | *Temp = 0x80; /* write data 0x80 to the address */ |
| 75 | myusec_delay(10); |
| 76 | Temp = bios + 0x5555; /* set up address to be C000:5555h */ |
| 77 | *Temp = 0xAA; /* write data 0xAA to the address */ |
| 78 | myusec_delay(10); |
| 79 | Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */ |
| 80 | *Temp = 0x55; /* write data 0x55 to the address */ |
| 81 | myusec_delay(10); |
| 82 | Temp = bios + page; /* set up address to be the current sector */ |
| 83 | *Temp = 0x30; /* write data 0x30 to the address */ |
| 84 | |
| 85 | /* wait for Toggle bit ready */ |
| 86 | toggle_ready_jedec(bios); |
| 87 | |
| 88 | return(0); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | int erase_jedec (struct flashchip * flash) |
| 92 | { |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame^] | 93 | volatile unsigned char * bios = flash->virt_addr; |
| 94 | volatile unsigned char *Temp; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 95 | |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame^] | 96 | /* Issue the Sector Erase command to 39SF020 */ |
| 97 | Temp = bios + 0x5555; /* set up address to be C000:5555h */ |
| 98 | *Temp = 0xAA; /* write data 0xAA to the address */ |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 99 | myusec_delay(10); |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame^] | 100 | Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */ |
| 101 | *Temp = 0x55; /* write data 0x55 to the address */ |
| 102 | myusec_delay(10); |
| 103 | Temp = bios + 0x5555; /* set up address to be C000:5555h */ |
| 104 | *Temp = 0x80; /* write data 0x80 to the address */ |
| 105 | myusec_delay(10); |
| 106 | Temp = bios + 0x5555; /* set up address to be C000:5555h */ |
| 107 | *Temp = 0xAA; /* write data 0xAA to the address */ |
| 108 | myusec_delay(10); |
| 109 | Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */ |
| 110 | *Temp = 0x55; /* write data 0x55 to the address */ |
| 111 | myusec_delay(10); |
| 112 | Temp = bios + 0x5555; /* set up address to be C000:5555h */ |
| 113 | *Temp = 0x10; /* write data 0x55 to the address */ |
| 114 | myusec_delay(10); |
| 115 | |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 116 | toggle_ready_jedec(bios); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 117 | |
| 118 | return(0); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 121 | int write_jedec (struct flashchip * flash, unsigned char * buf) |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 122 | { |
| 123 | int i; |
| 124 | int total_size = flash->total_size *1024, page_size = flash->page_size; |
Ronald G. Minnich | 5643942 | 2002-09-06 16:58:14 +0000 | [diff] [blame] | 125 | volatile unsigned char * bios = flash->virt_addr; |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 126 | |
| 127 | erase_jedec (flash); |
Ronald G. Minnich | 5643942 | 2002-09-06 16:58:14 +0000 | [diff] [blame] | 128 | if (*bios != (unsigned char ) 0xff) { |
| 129 | printf("ERASE FAILED\n"); |
| 130 | return -1; |
| 131 | } |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 132 | printf ("Programming Page: "); |
| 133 | for (i = 0; i < total_size/page_size; i++) { |
| 134 | printf ("%04d at address: 0x%08x", i, i * page_size); |
| 135 | write_page_jedec(bios, buf + i * page_size, bios + i * page_size, |
| 136 | page_size); |
| 137 | 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"); |
| 138 | } |
| 139 | printf("\n"); |
| 140 | protect_jedec (bios); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 141 | |
| 142 | return(0); |
Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame] | 143 | } |