Ronald G. Minnich | 5b582f2 | 2006-02-23 17:16:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * sst28sf040.c: driver for SST28SF040C flash models. |
| 3 | * |
| 4 | * |
| 5 | * Copyright 2000 Silicon Integrated System Corporation |
| 6 | * Copyright 2005 coresystems GmbH <stepan@openbios.org> |
| 7 | * |
| 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: |
| 24 | * 8 MEgabit (1024K x 8) SHARP LHF00L04, data sheet |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdint.h> |
| 30 | #include "flash.h" |
| 31 | #include "debug.h" |
| 32 | |
| 33 | |
| 34 | #define AUTO_PG_ERASE1 0x20 |
| 35 | #define AUTO_PG_ERASE2 0xD0 |
| 36 | #define AUTO_PGRM 0x10 |
| 37 | #define CHIP_ERASE 0x30 |
| 38 | #define RESET 0xFF |
| 39 | #define READ_ID 0x90 |
| 40 | |
| 41 | static __inline__ void protect_lhf00l04(volatile uint8_t *bios) |
| 42 | { |
| 43 | /* ask compiler not to optimize this */ |
| 44 | // volatile uint8_t tmp; |
| 45 | |
| 46 | } |
| 47 | |
| 48 | static __inline__ void unprotect_lhf00l04(volatile uint8_t *bios) |
| 49 | { |
| 50 | /* ask compiler not to optimize this */ |
| 51 | // volatile uint8_t tmp; |
| 52 | } |
| 53 | |
| 54 | static __inline__ int erase_sector_lhf00l04(volatile uint8_t *bios, |
| 55 | unsigned long address) |
| 56 | { |
| 57 | |
| 58 | return (0); |
| 59 | } |
| 60 | |
| 61 | static __inline__ int write_sector_lhf00l04(volatile uint8_t *bios, |
| 62 | uint8_t *src, |
| 63 | volatile uint8_t *dst, |
| 64 | unsigned int page_size) |
| 65 | { |
| 66 | int i; |
| 67 | |
| 68 | for (i = 0; i < page_size; i++) { |
| 69 | /* transfer data from source to destination */ |
| 70 | if (*src == 0xFF) { |
| 71 | dst++, src++; |
| 72 | /* If the data is 0xFF, don't program it */ |
| 73 | continue; |
| 74 | } |
| 75 | /*issue AUTO PROGRAM command */ |
| 76 | *dst = AUTO_PGRM; |
| 77 | *dst++ = *src++; |
| 78 | |
| 79 | /* wait for Toggle bit ready */ |
| 80 | // toggle_ready_jedec(bios); |
| 81 | } |
| 82 | |
| 83 | return (0); |
| 84 | } |
| 85 | |
| 86 | int probe_lhf00l04(struct flashchip *flash) |
| 87 | { |
| 88 | volatile uint8_t *bios = flash->virt_addr; |
| 89 | uint8_t id1, id2, tmp; |
| 90 | |
| 91 | /* save the value at the beginning of the Flash */ |
| 92 | tmp = *bios; |
| 93 | |
| 94 | *bios = RESET; |
| 95 | myusec_delay(10); |
| 96 | |
| 97 | *bios = READ_ID; |
| 98 | myusec_delay(10); |
| 99 | id1 = *(volatile uint8_t *) bios; |
| 100 | myusec_delay(10); |
| 101 | id2 = *(volatile uint8_t *) (bios + 0x01); |
| 102 | |
| 103 | *bios = RESET; |
| 104 | myusec_delay(10); |
| 105 | |
| 106 | printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2); |
| 107 | if (id1 == flash->manufacture_id && id2 == flash->model_id) |
| 108 | return 1; |
| 109 | |
| 110 | /* if there is no lhf00l04, restore the original value */ |
| 111 | *bios = tmp; |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int erase_lhf00l04(struct flashchip *flash) |
| 116 | { |
| 117 | volatile uint8_t *bios = flash->virt_addr; |
| 118 | |
| 119 | unprotect_lhf00l04(bios); |
| 120 | *bios = CHIP_ERASE; |
| 121 | *bios = CHIP_ERASE; |
| 122 | protect_lhf00l04(bios); |
| 123 | |
| 124 | myusec_delay(10); |
| 125 | // toggle_ready_jedec(bios); |
| 126 | |
| 127 | return (0); |
| 128 | } |
| 129 | |
| 130 | int write_lhf00l04(struct flashchip *flash, uint8_t *buf) |
| 131 | { |
| 132 | int i; |
| 133 | int total_size = flash->total_size * 1024, page_size = |
| 134 | flash->page_size; |
| 135 | volatile uint8_t *bios = flash->virt_addr; |
| 136 | |
| 137 | unprotect_lhf00l04(bios); |
| 138 | |
| 139 | printf("Programming Page: "); |
| 140 | for (i = 0; i < total_size / page_size; i++) { |
| 141 | /* erase the page before programming */ |
| 142 | erase_sector_lhf00l04(bios, i * page_size); |
| 143 | |
| 144 | /* write to the sector */ |
| 145 | printf("%04d at address: 0x%08x", i, i * page_size); |
| 146 | write_sector_lhf00l04(bios, buf + i * page_size, |
| 147 | bios + i * page_size, page_size); |
| 148 | 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"); |
| 149 | } |
| 150 | printf("\n"); |
| 151 | |
| 152 | protect_lhf00l04(bios); |
| 153 | |
| 154 | return (0); |
| 155 | } |