Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008 Carl-Daniel Hailfinger |
| 5 | * Copyright (C) 2008 Ronald Hoogenboom <ronald@zonnet.nl> |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 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; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Contains the generic SPI framework |
| 23 | */ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <pci/pci.h> |
| 27 | #include <stdint.h> |
| 28 | #include <string.h> |
| 29 | #include "flash.h" |
Carl-Daniel Hailfinger | d6cbf76 | 2008-05-13 14:58:23 +0000 | [diff] [blame^] | 30 | #include "spi.h" |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 31 | |
| 32 | #define ITE_SUPERIO_PORT1 0x2e |
| 33 | #define ITE_SUPERIO_PORT2 0x4e |
| 34 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 35 | |
| 36 | uint16_t it8716f_flashport = 0; |
| 37 | /* use fast 33MHz SPI (<>0) or slow 16MHz (0) */ |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 38 | int fast_spi = 1; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 39 | |
| 40 | void spi_prettyprint_status_register(struct flashchip *flash); |
| 41 | void spi_disable_blockprotect(void); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 42 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 43 | /* Generic Super I/O helper functions */ |
| 44 | uint8_t regval(uint16_t port, uint8_t reg) |
| 45 | { |
| 46 | outb(reg, port); |
| 47 | return inb(port + 1); |
| 48 | } |
| 49 | |
| 50 | void regwrite(uint16_t port, uint8_t reg, uint8_t val) |
| 51 | { |
| 52 | outb(reg, port); |
| 53 | outb(val, port + 1); |
| 54 | } |
| 55 | |
| 56 | /* Helper functions for most recent ITE IT87xx Super I/O chips */ |
| 57 | #define CHIP_ID_BYTE1_REG 0x20 |
| 58 | #define CHIP_ID_BYTE2_REG 0x21 |
| 59 | static void enter_conf_mode_ite(uint16_t port) |
| 60 | { |
| 61 | outb(0x87, port); |
| 62 | outb(0x01, port); |
| 63 | outb(0x55, port); |
| 64 | if (port == ITE_SUPERIO_PORT1) |
| 65 | outb(0x55, port); |
| 66 | else |
| 67 | outb(0xaa, port); |
| 68 | } |
| 69 | |
| 70 | static void exit_conf_mode_ite(uint16_t port) |
| 71 | { |
| 72 | regwrite(port, 0x02, 0x02); |
| 73 | } |
| 74 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 75 | static uint16_t find_ite_spi_flash_port(uint16_t port) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 76 | { |
| 77 | uint8_t tmp = 0; |
| 78 | uint16_t id, flashport = 0; |
| 79 | |
| 80 | enter_conf_mode_ite(port); |
| 81 | |
| 82 | id = regval(port, CHIP_ID_BYTE1_REG) << 8; |
| 83 | id |= regval(port, CHIP_ID_BYTE2_REG); |
| 84 | |
| 85 | /* TODO: Handle more IT87xx if they support flash translation */ |
| 86 | if (id == 0x8716) { |
| 87 | /* NOLDN, reg 0x24, mask out lowest bit (suspend) */ |
| 88 | tmp = regval(port, 0x24) & 0xFE; |
| 89 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 90 | 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
| 91 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 92 | 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
| 93 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 94 | 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis"); |
| 95 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 96 | 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis"); |
| 97 | printf("LPC write to serial flash %sabled\n", |
| 98 | (tmp & 1 << 4) ? "en" : "dis"); |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 99 | printf("serial flash pin %i\n", (tmp & 1 << 5) ? 87 : 29); |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 100 | /* LDN 0x7, reg 0x64/0x65 */ |
| 101 | regwrite(port, 0x07, 0x7); |
| 102 | flashport = regval(port, 0x64) << 8; |
| 103 | flashport |= regval(port, 0x65); |
| 104 | } |
| 105 | exit_conf_mode_ite(port); |
| 106 | return flashport; |
| 107 | } |
| 108 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 109 | int it87xx_probe_spi_flash(const char *name) |
| 110 | { |
| 111 | it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT1); |
| 112 | if (!it8716f_flashport) |
| 113 | it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT2); |
| 114 | return (!it8716f_flashport); |
| 115 | } |
| 116 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 117 | /* The IT8716F only supports commands with length 1,2,4,5 bytes including |
| 118 | command byte and can not read more than 3 bytes from the device. |
| 119 | This function expects writearr[0] to be the first byte sent to the device, |
| 120 | whereas the IT8716F splits commands internally into address and non-address |
| 121 | commands with the address in inverse wire order. That's why the register |
| 122 | ordering in case 4 and 5 may seem strange. */ |
Carl-Daniel Hailfinger | a5b8efd | 2008-05-10 23:40:51 +0000 | [diff] [blame] | 123 | static int it8716f_spi_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 124 | { |
| 125 | uint8_t busy, writeenc; |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 126 | int i; |
| 127 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 128 | do { |
Carl-Daniel Hailfinger | a5b8efd | 2008-05-10 23:40:51 +0000 | [diff] [blame] | 129 | busy = inb(it8716f_flashport) & 0x80; |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 130 | } while (busy); |
| 131 | if (readcnt > 3) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 132 | printf("%s called with unsupported readcnt %i.\n", |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 133 | __FUNCTION__, readcnt); |
| 134 | return 1; |
| 135 | } |
| 136 | switch (writecnt) { |
| 137 | case 1: |
Carl-Daniel Hailfinger | a5b8efd | 2008-05-10 23:40:51 +0000 | [diff] [blame] | 138 | outb(writearr[0], it8716f_flashport + 1); |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 139 | writeenc = 0x0; |
| 140 | break; |
| 141 | case 2: |
Carl-Daniel Hailfinger | a5b8efd | 2008-05-10 23:40:51 +0000 | [diff] [blame] | 142 | outb(writearr[0], it8716f_flashport + 1); |
| 143 | outb(writearr[1], it8716f_flashport + 7); |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 144 | writeenc = 0x1; |
| 145 | break; |
| 146 | case 4: |
Carl-Daniel Hailfinger | a5b8efd | 2008-05-10 23:40:51 +0000 | [diff] [blame] | 147 | outb(writearr[0], it8716f_flashport + 1); |
| 148 | outb(writearr[1], it8716f_flashport + 4); |
| 149 | outb(writearr[2], it8716f_flashport + 3); |
| 150 | outb(writearr[3], it8716f_flashport + 2); |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 151 | writeenc = 0x2; |
| 152 | break; |
| 153 | case 5: |
Carl-Daniel Hailfinger | a5b8efd | 2008-05-10 23:40:51 +0000 | [diff] [blame] | 154 | outb(writearr[0], it8716f_flashport + 1); |
| 155 | outb(writearr[1], it8716f_flashport + 4); |
| 156 | outb(writearr[2], it8716f_flashport + 3); |
| 157 | outb(writearr[3], it8716f_flashport + 2); |
| 158 | outb(writearr[4], it8716f_flashport + 7); |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 159 | writeenc = 0x3; |
| 160 | break; |
| 161 | default: |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 162 | printf("%s called with unsupported writecnt %i.\n", |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 163 | __FUNCTION__, writecnt); |
| 164 | return 1; |
| 165 | } |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 166 | /* Start IO, 33 or 16 MHz, readcnt input bytes, writecnt output bytes. |
| 167 | * Note: |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 168 | * We can't use writecnt directly, but have to use a strange encoding. |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 169 | */ |
Carl-Daniel Hailfinger | a5b8efd | 2008-05-10 23:40:51 +0000 | [diff] [blame] | 170 | outb(((0x4 + (fast_spi ? 1 : 0)) << 4) | ((readcnt & 0x3) << 2) | (writeenc), it8716f_flashport); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 171 | |
Ronald Hoogenboom | d4554c5 | 2008-01-21 23:55:08 +0000 | [diff] [blame] | 172 | if (readcnt > 0) { |
| 173 | do { |
Carl-Daniel Hailfinger | a5b8efd | 2008-05-10 23:40:51 +0000 | [diff] [blame] | 174 | busy = inb(it8716f_flashport) & 0x80; |
Ronald Hoogenboom | d4554c5 | 2008-01-21 23:55:08 +0000 | [diff] [blame] | 175 | } while (busy); |
| 176 | |
| 177 | for (i = 0; i < readcnt; i++) { |
Carl-Daniel Hailfinger | a5b8efd | 2008-05-10 23:40:51 +0000 | [diff] [blame] | 178 | readarr[i] = inb(it8716f_flashport + 5 + i); |
Ronald Hoogenboom | d4554c5 | 2008-01-21 23:55:08 +0000 | [diff] [blame] | 179 | } |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 185 | int spi_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr) |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 186 | { |
| 187 | if (it8716f_flashport) |
Carl-Daniel Hailfinger | a5b8efd | 2008-05-10 23:40:51 +0000 | [diff] [blame] | 188 | return it8716f_spi_command(writecnt, readcnt, writearr, readarr); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 189 | printf_debug("%s called, but no SPI chipset detected\n", __FUNCTION__); |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 190 | return 1; |
| 191 | } |
| 192 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 193 | static int spi_rdid(unsigned char *readarr) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 194 | { |
Carl-Daniel Hailfinger | 228231f | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 195 | const unsigned char cmd[JEDEC_RDID_OUTSIZE] = {JEDEC_RDID}; |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 196 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 197 | if (spi_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, cmd, readarr)) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 198 | return 1; |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 199 | printf_debug("RDID returned %02x %02x %02x.\n", readarr[0], readarr[1], readarr[2]); |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 203 | void spi_write_enable() |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 204 | { |
Carl-Daniel Hailfinger | 228231f | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 205 | const unsigned char cmd[JEDEC_WREN_OUTSIZE] = {JEDEC_WREN}; |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 206 | |
| 207 | /* Send WREN (Write Enable) */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 208 | spi_command(JEDEC_WREN_OUTSIZE, JEDEC_WREN_INSIZE, cmd, NULL); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 211 | void spi_write_disable() |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 212 | { |
Carl-Daniel Hailfinger | 228231f | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 213 | const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = {JEDEC_WRDI}; |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 214 | |
| 215 | /* Send WRDI (Write Disable) */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 216 | spi_command(JEDEC_WRDI_OUTSIZE, JEDEC_WRDI_INSIZE, cmd, NULL); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 219 | int probe_spi(struct flashchip *flash) |
| 220 | { |
| 221 | unsigned char readarr[3]; |
Carl-Daniel Hailfinger | 1263d2a | 2008-02-06 22:07:58 +0000 | [diff] [blame] | 222 | uint32_t manuf_id; |
| 223 | uint32_t model_id; |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 224 | if (!spi_rdid(readarr)) { |
Carl-Daniel Hailfinger | 1263d2a | 2008-02-06 22:07:58 +0000 | [diff] [blame] | 225 | /* Check if this is a continuation vendor ID */ |
| 226 | if (readarr[0] == 0x7f) { |
| 227 | manuf_id = (readarr[0] << 8) | readarr[1]; |
| 228 | model_id = readarr[2]; |
| 229 | } else { |
| 230 | manuf_id = readarr[0]; |
| 231 | model_id = (readarr[1] << 8) | readarr[2]; |
| 232 | } |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 233 | printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id, model_id); |
Carl-Daniel Hailfinger | e973b05 | 2008-01-04 16:22:09 +0000 | [diff] [blame] | 234 | if (manuf_id == flash->manufacture_id && |
| 235 | model_id == flash->model_id) { |
| 236 | /* Print the status register to tell the |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 237 | * user about possible write protection. |
| 238 | */ |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 239 | spi_prettyprint_status_register(flash); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 240 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 241 | return 1; |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 242 | } |
Carl-Daniel Hailfinger | e973b05 | 2008-01-04 16:22:09 +0000 | [diff] [blame] | 243 | /* Test if this is a pure vendor match. */ |
| 244 | if (manuf_id == flash->manufacture_id && |
| 245 | GENERIC_DEVICE_ID == flash->model_id) |
| 246 | return 1; |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 252 | uint8_t spi_read_status_register() |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 253 | { |
Carl-Daniel Hailfinger | 228231f | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 254 | const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = {JEDEC_RDSR}; |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 255 | unsigned char readarr[1]; |
| 256 | |
| 257 | /* Read Status Register */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 258 | spi_command(JEDEC_RDSR_OUTSIZE, JEDEC_RDSR_INSIZE, cmd, readarr); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 259 | return readarr[0]; |
| 260 | } |
| 261 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 262 | /* Prettyprint the status register. Common definitions. |
| 263 | */ |
| 264 | void spi_prettyprint_status_register_common(uint8_t status) |
| 265 | { |
| 266 | printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is " |
| 267 | "%sset\n", (status & (1 << 5)) ? "" : "not "); |
| 268 | printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is " |
| 269 | "%sset\n", (status & (1 << 4)) ? "" : "not "); |
| 270 | printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is " |
| 271 | "%sset\n", (status & (1 << 3)) ? "" : "not "); |
| 272 | printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is " |
| 273 | "%sset\n", (status & (1 << 2)) ? "" : "not "); |
| 274 | printf_debug("Chip status register: Write Enable Latch (WEL) is " |
| 275 | "%sset\n", (status & (1 << 1)) ? "" : "not "); |
| 276 | printf_debug("Chip status register: Write In Progress (WIP/BUSY) is " |
| 277 | "%sset\n", (status & (1 << 0)) ? "" : "not "); |
| 278 | } |
| 279 | |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 280 | /* Prettyprint the status register. Works for |
| 281 | * ST M25P series |
| 282 | * MX MX25L series |
| 283 | */ |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 284 | void spi_prettyprint_status_register_st_m25p(uint8_t status) |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 285 | { |
| 286 | printf_debug("Chip status register: Status Register Write Disable " |
| 287 | "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
| 288 | printf_debug("Chip status register: Bit 6 is " |
| 289 | "%sset\n", (status & (1 << 6)) ? "" : "not "); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 290 | spi_prettyprint_status_register_common(status); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 293 | /* Prettyprint the status register. Works for |
| 294 | * SST 25VF016 |
| 295 | */ |
| 296 | void spi_prettyprint_status_register_sst25vf016(uint8_t status) |
| 297 | { |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 298 | const char *bpt[] = { |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 299 | "none", |
| 300 | "1F0000H-1FFFFFH", |
| 301 | "1E0000H-1FFFFFH", |
| 302 | "1C0000H-1FFFFFH", |
| 303 | "180000H-1FFFFFH", |
| 304 | "100000H-1FFFFFH", |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 305 | "all", "all" |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 306 | }; |
| 307 | printf_debug("Chip status register: Block Protect Write Disable " |
| 308 | "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not "); |
| 309 | printf_debug("Chip status register: Auto Address Increment Programming " |
| 310 | "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not "); |
| 311 | spi_prettyprint_status_register_common(status); |
| 312 | printf_debug("Resulting block protection : %s\n", |
| 313 | bpt[(status & 0x1c) >> 2]); |
| 314 | } |
| 315 | |
| 316 | void spi_prettyprint_status_register(struct flashchip *flash) |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 317 | { |
| 318 | uint8_t status; |
| 319 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 320 | status = spi_read_status_register(); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 321 | printf_debug("Chip status register is %02x\n", status); |
| 322 | switch (flash->manufacture_id) { |
| 323 | case ST_ID: |
| 324 | case MX_ID: |
| 325 | if ((flash->model_id & 0xff00) == 0x2000) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 326 | spi_prettyprint_status_register_st_m25p(status); |
| 327 | break; |
| 328 | case SST_ID: |
| 329 | if (flash->model_id == SST_25VF016B) |
| 330 | spi_prettyprint_status_register_sst25vf016(status); |
Carl-Daniel Hailfinger | 9a3ec82 | 2007-12-29 10:15:58 +0000 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 335 | int spi_chip_erase_c7(struct flashchip *flash) |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 336 | { |
Carl-Daniel Hailfinger | 228231f | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 337 | const unsigned char cmd[JEDEC_CE_C7_OUTSIZE] = {JEDEC_CE_C7}; |
Carl-Daniel Hailfinger | f5df46f | 2007-12-16 21:15:27 +0000 | [diff] [blame] | 338 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 339 | spi_disable_blockprotect(); |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 340 | spi_write_enable(); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 341 | /* Send CE (Chip Erase) */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 342 | spi_command(JEDEC_CE_C7_OUTSIZE, JEDEC_CE_C7_INSIZE, cmd, NULL); |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 343 | /* Wait until the Write-In-Progress bit is cleared. |
| 344 | * This usually takes 1-85 s, so wait in 1 s steps. |
| 345 | */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 346 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 347 | sleep(1); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 351 | /* Block size is usually |
| 352 | * 64k for Macronix |
| 353 | * 32k for SST |
| 354 | * 4-32k non-uniform for EON |
| 355 | */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 356 | int spi_block_erase_d8(const struct flashchip *flash, unsigned long addr) |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 357 | { |
Carl-Daniel Hailfinger | 228231f | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 358 | unsigned char cmd[JEDEC_BE_D8_OUTSIZE] = {JEDEC_BE_D8}; |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 359 | |
| 360 | cmd[1] = (addr & 0x00ff0000) >> 16; |
| 361 | cmd[2] = (addr & 0x0000ff00) >> 8; |
| 362 | cmd[3] = (addr & 0x000000ff); |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 363 | spi_write_enable(); |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 364 | /* Send BE (Block Erase) */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 365 | spi_command(JEDEC_BE_D8_OUTSIZE, JEDEC_BE_D8_INSIZE, cmd, NULL); |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 366 | /* Wait until the Write-In-Progress bit is cleared. |
| 367 | * This usually takes 100-4000 ms, so wait in 100 ms steps. |
| 368 | */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 369 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 370 | usleep(100 * 1000); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | /* Sector size is usually 4k, though Macronix eliteflash has 64k */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 375 | int spi_sector_erase(const struct flashchip *flash, unsigned long addr) |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 376 | { |
Carl-Daniel Hailfinger | 228231f | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 377 | unsigned char cmd[JEDEC_SE_OUTSIZE] = {JEDEC_SE}; |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 378 | cmd[1] = (addr & 0x00ff0000) >> 16; |
| 379 | cmd[2] = (addr & 0x0000ff00) >> 8; |
| 380 | cmd[3] = (addr & 0x000000ff); |
| 381 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 382 | spi_write_enable(); |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 383 | /* Send SE (Sector Erase) */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 384 | spi_command(JEDEC_SE_OUTSIZE, JEDEC_SE_INSIZE, cmd, NULL); |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 385 | /* Wait until the Write-In-Progress bit is cleared. |
| 386 | * This usually takes 15-800 ms, so wait in 10 ms steps. |
| 387 | */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 388 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 389 | usleep(10 * 1000); |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | /* Page size is usually 256 bytes */ |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 394 | void it8716f_spi_page_program(int block, uint8_t *buf, uint8_t *bios) { |
| 395 | int i; |
| 396 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 397 | spi_write_enable(); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 398 | outb(0x06 , it8716f_flashport + 1); |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 399 | outb(((2 + (fast_spi ? 1 : 0)) << 4), it8716f_flashport); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 400 | for (i = 0; i < 256; i++) { |
| 401 | bios[256 * block + i] = buf[256 * block + i]; |
| 402 | } |
| 403 | outb(0, it8716f_flashport); |
Carl-Daniel Hailfinger | 5b1c6ed | 2007-10-22 16:15:28 +0000 | [diff] [blame] | 404 | /* Wait until the Write-In-Progress bit is cleared. |
| 405 | * This usually takes 1-10 ms, so wait in 1 ms steps. |
| 406 | */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 407 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 408 | usleep(1000); |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 411 | void spi_page_program(int block, uint8_t *buf, uint8_t *bios) |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 412 | { |
| 413 | if (it8716f_flashport) |
| 414 | it8716f_spi_page_program(block, buf, bios); |
| 415 | } |
| 416 | |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 417 | /* |
| 418 | * This is according the SST25VF016 datasheet, who knows it is more |
| 419 | * generic that this... |
| 420 | */ |
| 421 | void spi_write_status_register(int status) |
| 422 | { |
Carl-Daniel Hailfinger | 228231f | 2008-05-13 14:01:22 +0000 | [diff] [blame] | 423 | const unsigned char cmd[JEDEC_WRSR_OUTSIZE] = {JEDEC_WRSR, (unsigned char)status}; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 424 | |
| 425 | /* Send WRSR (Write Status Register) */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 426 | spi_command(JEDEC_WRSR_OUTSIZE, JEDEC_WRSR_INSIZE, cmd, NULL); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | void spi_byte_program(int address, uint8_t byte) |
| 430 | { |
| 431 | const unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE] = {JEDEC_BYTE_PROGRAM, |
| 432 | (address>>16)&0xff, |
| 433 | (address>>8)&0xff, |
| 434 | (address>>0)&0xff, |
| 435 | byte |
| 436 | }; |
| 437 | |
| 438 | /* Send Byte-Program */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 439 | spi_command(JEDEC_BYTE_PROGRAM_OUTSIZE, JEDEC_BYTE_PROGRAM_INSIZE, cmd, NULL); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | void spi_disable_blockprotect(void) |
| 443 | { |
| 444 | uint8_t status; |
| 445 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 446 | status = spi_read_status_register(); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 447 | /* If there is block protection in effect, unprotect it first. */ |
| 448 | if ((status & 0x3c) != 0) { |
| 449 | printf_debug("Some block protection in effect, disabling\n"); |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 450 | spi_write_enable(); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 451 | spi_write_status_register(status & ~0x3c); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles |
| 457 | * Program chip using firmware cycle byte programming. (SLOW!) |
| 458 | */ |
| 459 | int it8716f_over512k_spi_chip_write(struct flashchip *flash, uint8_t *buf) |
| 460 | { |
| 461 | int total_size = 1024 * flash->total_size; |
| 462 | int i; |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 463 | fast_spi = 0; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 464 | |
| 465 | spi_disable_blockprotect(); |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 466 | for (i = 0; i < total_size; i++) { |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 467 | spi_write_enable(); |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 468 | spi_byte_program(i, buf[i]); |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 469 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 470 | myusec_delay(10); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 471 | } |
| 472 | /* resume normal ops... */ |
| 473 | outb(0x20, it8716f_flashport); |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | void spi_3byte_read(int address, uint8_t *bytes, int len) |
| 478 | { |
| 479 | const unsigned char cmd[JEDEC_READ_OUTSIZE] = {JEDEC_READ, |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 480 | (address >> 16) & 0xff, |
| 481 | (address >> 8) & 0xff, |
| 482 | (address >> 0) & 0xff, |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 483 | }; |
| 484 | |
| 485 | /* Send Read */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 486 | spi_command(JEDEC_READ_OUTSIZE, len, cmd, bytes); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* |
| 490 | * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles |
| 491 | * Need to read this big flash using firmware cycles 3 byte at a time. |
| 492 | */ |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 493 | int spi_chip_read(struct flashchip *flash, uint8_t *buf) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 494 | { |
| 495 | int total_size = 1024 * flash->total_size; |
| 496 | int i; |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 497 | fast_spi = 0; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 498 | |
| 499 | if (total_size > 512 * 1024) { |
Carl-Daniel Hailfinger | d3568ad | 2008-01-22 14:37:31 +0000 | [diff] [blame] | 500 | for (i = 0; i < total_size; i += 3) { |
| 501 | int toread = 3; |
| 502 | if (total_size - i < toread) |
| 503 | toread = total_size - i; |
| 504 | spi_3byte_read(i, buf + i, toread); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 505 | } |
| 506 | } else { |
| 507 | memcpy(buf, (const char *)flash->virtual_memory, total_size); |
| 508 | } |
| 509 | return 0; |
| 510 | } |
| 511 | |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 512 | int spi_chip_write(struct flashchip *flash, uint8_t *buf) { |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 513 | int total_size = 1024 * flash->total_size; |
| 514 | int i; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 515 | if (total_size > 512 * 1024) { |
| 516 | it8716f_over512k_spi_chip_write(flash, buf); |
| 517 | } else { |
| 518 | for (i = 0; i < total_size / 256; i++) { |
Peter Stuge | fa8c550 | 2008-05-10 23:07:52 +0000 | [diff] [blame] | 519 | spi_page_program(i, buf, (uint8_t *)flash->virtual_memory); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 520 | } |
Carl-Daniel Hailfinger | 6b44496 | 2007-10-18 00:24:07 +0000 | [diff] [blame] | 521 | } |
| 522 | return 0; |
| 523 | } |
| 524 | |