Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2007 Carl-Daniel Hailfinger |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Contains the generic SPI framework |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <pci/pci.h> |
| 26 | #include <stdint.h> |
| 27 | #include <string.h> |
| 28 | #include "flash.h" |
| 29 | |
| 30 | #define ITE_SUPERIO_PORT1 0x2e |
| 31 | #define ITE_SUPERIO_PORT2 0x4e |
| 32 | |
| 33 | #define JEDEC_RDID {0x9f} |
| 34 | #define JEDEC_RDID_OUTSIZE 0x01 |
| 35 | #define JEDEC_RDID_INSIZE 0x03 |
| 36 | |
| 37 | static uint16_t it8716f_flashport = 0; |
| 38 | |
| 39 | /* Generic Super I/O helper functions */ |
| 40 | uint8_t regval(uint16_t port, uint8_t reg) |
| 41 | { |
| 42 | outb(reg, port); |
| 43 | return inb(port + 1); |
| 44 | } |
| 45 | |
| 46 | void regwrite(uint16_t port, uint8_t reg, uint8_t val) |
| 47 | { |
| 48 | outb(reg, port); |
| 49 | outb(val, port + 1); |
| 50 | } |
| 51 | |
| 52 | /* Helper functions for most recent ITE IT87xx Super I/O chips */ |
| 53 | #define CHIP_ID_BYTE1_REG 0x20 |
| 54 | #define CHIP_ID_BYTE2_REG 0x21 |
| 55 | static void enter_conf_mode_ite(uint16_t port) |
| 56 | { |
| 57 | outb(0x87, port); |
| 58 | outb(0x01, port); |
| 59 | outb(0x55, port); |
| 60 | if (port == ITE_SUPERIO_PORT1) |
| 61 | outb(0x55, port); |
| 62 | else |
| 63 | outb(0xaa, port); |
| 64 | } |
| 65 | |
| 66 | static void exit_conf_mode_ite(uint16_t port) |
| 67 | { |
| 68 | regwrite(port, 0x02, 0x02); |
| 69 | } |
| 70 | |
| 71 | static uint16_t find_ite_serial_flash_port(uint16_t port) |
| 72 | { |
| 73 | uint8_t tmp = 0; |
| 74 | uint16_t id, flashport = 0; |
| 75 | |
| 76 | enter_conf_mode_ite(port); |
| 77 | |
| 78 | id = regval(port, CHIP_ID_BYTE1_REG) << 8; |
| 79 | id |= regval(port, CHIP_ID_BYTE2_REG); |
| 80 | |
| 81 | /* TODO: Handle more IT87xx if they support flash translation */ |
| 82 | if (id == 0x8716) { |
| 83 | /* NOLDN, reg 0x24, mask out lowest bit (suspend) */ |
| 84 | tmp = regval(port, 0x24) & 0xFE; |
| 85 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 86 | 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
| 87 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 88 | 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis"); |
| 89 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 90 | 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis"); |
| 91 | printf("Serial flash segment 0x%08x-0x%08x %sabled\n", |
| 92 | 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis"); |
| 93 | printf("LPC write to serial flash %sabled\n", |
| 94 | (tmp & 1 << 4) ? "en" : "dis"); |
| 95 | printf("serial flash pin %i\n", (tmp & 1 << 5) ? 87 : 29); |
| 96 | /* LDN 0x7, reg 0x64/0x65 */ |
| 97 | regwrite(port, 0x07, 0x7); |
| 98 | flashport = regval(port, 0x64) << 8; |
| 99 | flashport |= regval(port, 0x65); |
| 100 | } |
| 101 | exit_conf_mode_ite(port); |
| 102 | return flashport; |
| 103 | } |
| 104 | |
| 105 | /* The IT8716F only supports commands with length 1,2,4,5 bytes including |
| 106 | command byte and can not read more than 3 bytes from the device. |
| 107 | This function expects writearr[0] to be the first byte sent to the device, |
| 108 | whereas the IT8716F splits commands internally into address and non-address |
| 109 | commands with the address in inverse wire order. That's why the register |
| 110 | ordering in case 4 and 5 may seem strange. */ |
| 111 | static int it8716f_spi_command(uint16_t port, unsigned char writecnt, unsigned char readcnt, const unsigned char *writearr, unsigned char *readarr) |
| 112 | { |
| 113 | uint8_t busy, writeenc; |
| 114 | do { |
| 115 | busy = inb(port) & 0x80; |
| 116 | } while (busy); |
| 117 | if (readcnt > 3) { |
| 118 | printf("%s called with unsupported readcnt %i\n", |
| 119 | __FUNCTION__, readcnt); |
| 120 | return 1; |
| 121 | } |
| 122 | switch (writecnt) { |
| 123 | case 1: |
| 124 | outb(writearr[0], port + 1); |
| 125 | writeenc = 0x0; |
| 126 | break; |
| 127 | case 2: |
| 128 | outb(writearr[0], port + 1); |
| 129 | outb(writearr[1], port + 7); |
| 130 | writeenc = 0x1; |
| 131 | break; |
| 132 | case 4: |
| 133 | outb(writearr[0], port + 1); |
| 134 | outb(writearr[1], port + 4); |
| 135 | outb(writearr[2], port + 3); |
| 136 | outb(writearr[3], port + 2); |
| 137 | writeenc = 0x2; |
| 138 | break; |
| 139 | case 5: |
| 140 | outb(writearr[0], port + 1); |
| 141 | outb(writearr[1], port + 4); |
| 142 | outb(writearr[2], port + 3); |
| 143 | outb(writearr[3], port + 2); |
| 144 | outb(writearr[4], port + 7); |
| 145 | writeenc = 0x3; |
| 146 | break; |
| 147 | default: |
| 148 | printf("%s called with unsupported writecnt %i\n", |
| 149 | __FUNCTION__, writecnt); |
| 150 | return 1; |
| 151 | } |
| 152 | /* Start IO, 33MHz, readcnt input bytes, writecnt output bytes. Note: |
| 153 | * We can't use writecnt directly, but have to use a strange encoding |
| 154 | */ |
| 155 | outb((0x5 << 4) | ((readcnt & 0x3) << 2) | (writeenc), port); |
| 156 | do { |
| 157 | busy = inb(port) & 0x80; |
| 158 | } while (busy); |
| 159 | readarr[0] = inb(port + 5); |
| 160 | readarr[1] = inb(port + 6); |
| 161 | readarr[2] = inb(port + 7); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int it8716f_serial_rdid(uint16_t port, unsigned char *readarr) |
| 166 | { |
| 167 | const unsigned char cmd[] = JEDEC_RDID; |
| 168 | |
| 169 | if (it8716f_spi_command(port, JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, cmd, readarr)) |
| 170 | return 1; |
| 171 | printf("RDID returned %02x %02x %02x\n", readarr[0], readarr[1], readarr[2]); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int it87xx_probe_serial_flash(const char *name) |
| 176 | { |
| 177 | it8716f_flashport = find_ite_serial_flash_port(ITE_SUPERIO_PORT1); |
| 178 | if (!it8716f_flashport) |
| 179 | it8716f_flashport = find_ite_serial_flash_port(ITE_SUPERIO_PORT2); |
| 180 | return (!it8716f_flashport); |
| 181 | } |
| 182 | |
| 183 | int probe_spi(struct flashchip *flash) |
| 184 | { |
| 185 | unsigned char readarr[3]; |
| 186 | uint8_t manuf_id; |
| 187 | uint16_t model_id; |
| 188 | if (it8716f_flashport) { |
| 189 | it8716f_serial_rdid(it8716f_flashport, readarr); |
| 190 | manuf_id = readarr[0]; |
| 191 | model_id = (readarr[1] << 8) | readarr[2]; |
| 192 | printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id, model_id); |
| 193 | if (manuf_id == flash->manufacture_id && model_id == flash->model_id) |
| 194 | return 1; |
| 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |