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 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame^] | 71 | static uint16_t find_ite_spi_flash_port(uint16_t port) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 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 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame^] | 105 | int it87xx_probe_spi_flash(const char *name) |
| 106 | { |
| 107 | it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT1); |
| 108 | if (!it8716f_flashport) |
| 109 | it8716f_flashport = find_ite_spi_flash_port(ITE_SUPERIO_PORT2); |
| 110 | return (!it8716f_flashport); |
| 111 | } |
| 112 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 113 | /* The IT8716F only supports commands with length 1,2,4,5 bytes including |
| 114 | command byte and can not read more than 3 bytes from the device. |
| 115 | This function expects writearr[0] to be the first byte sent to the device, |
| 116 | whereas the IT8716F splits commands internally into address and non-address |
| 117 | commands with the address in inverse wire order. That's why the register |
| 118 | ordering in case 4 and 5 may seem strange. */ |
| 119 | static int it8716f_spi_command(uint16_t port, unsigned char writecnt, unsigned char readcnt, const unsigned char *writearr, unsigned char *readarr) |
| 120 | { |
| 121 | uint8_t busy, writeenc; |
| 122 | do { |
| 123 | busy = inb(port) & 0x80; |
| 124 | } while (busy); |
| 125 | if (readcnt > 3) { |
| 126 | printf("%s called with unsupported readcnt %i\n", |
| 127 | __FUNCTION__, readcnt); |
| 128 | return 1; |
| 129 | } |
| 130 | switch (writecnt) { |
| 131 | case 1: |
| 132 | outb(writearr[0], port + 1); |
| 133 | writeenc = 0x0; |
| 134 | break; |
| 135 | case 2: |
| 136 | outb(writearr[0], port + 1); |
| 137 | outb(writearr[1], port + 7); |
| 138 | writeenc = 0x1; |
| 139 | break; |
| 140 | case 4: |
| 141 | outb(writearr[0], port + 1); |
| 142 | outb(writearr[1], port + 4); |
| 143 | outb(writearr[2], port + 3); |
| 144 | outb(writearr[3], port + 2); |
| 145 | writeenc = 0x2; |
| 146 | break; |
| 147 | case 5: |
| 148 | outb(writearr[0], port + 1); |
| 149 | outb(writearr[1], port + 4); |
| 150 | outb(writearr[2], port + 3); |
| 151 | outb(writearr[3], port + 2); |
| 152 | outb(writearr[4], port + 7); |
| 153 | writeenc = 0x3; |
| 154 | break; |
| 155 | default: |
| 156 | printf("%s called with unsupported writecnt %i\n", |
| 157 | __FUNCTION__, writecnt); |
| 158 | return 1; |
| 159 | } |
| 160 | /* Start IO, 33MHz, readcnt input bytes, writecnt output bytes. Note: |
| 161 | * We can't use writecnt directly, but have to use a strange encoding |
| 162 | */ |
| 163 | outb((0x5 << 4) | ((readcnt & 0x3) << 2) | (writeenc), port); |
| 164 | do { |
| 165 | busy = inb(port) & 0x80; |
| 166 | } while (busy); |
| 167 | readarr[0] = inb(port + 5); |
| 168 | readarr[1] = inb(port + 6); |
| 169 | readarr[2] = inb(port + 7); |
| 170 | return 0; |
| 171 | } |
| 172 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame^] | 173 | static int generic_spi_command(unsigned char writecnt, unsigned char readcnt, const unsigned char *writearr, unsigned char *readarr) |
| 174 | { |
| 175 | if (it8716f_flashport) |
| 176 | return it8716f_spi_command(it8716f_flashport, writecnt, readcnt, writearr, readarr); |
| 177 | printf("%s called, but no SPI chipset detected\n", __FUNCTION__); |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | static int generic_spi_rdid(unsigned char *readarr) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 182 | { |
| 183 | const unsigned char cmd[] = JEDEC_RDID; |
| 184 | |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame^] | 185 | if (generic_spi_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, cmd, readarr)) |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 186 | return 1; |
| 187 | printf("RDID returned %02x %02x %02x\n", readarr[0], readarr[1], readarr[2]); |
| 188 | return 0; |
| 189 | } |
| 190 | |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 191 | int probe_spi(struct flashchip *flash) |
| 192 | { |
| 193 | unsigned char readarr[3]; |
| 194 | uint8_t manuf_id; |
| 195 | uint16_t model_id; |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame^] | 196 | if (!generic_spi_rdid(readarr)) { |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 197 | manuf_id = readarr[0]; |
| 198 | model_id = (readarr[1] << 8) | readarr[2]; |
| 199 | printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, manuf_id, model_id); |
| 200 | if (manuf_id == flash->manufacture_id && model_id == flash->model_id) |
| 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |