stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2008 Peter Stuge <peter@stuge.se> |
hailfinger | 1220e59 | 2010-01-07 03:32:17 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009,2010 Carl-Daniel Hailfinger |
stuge | a564bcf | 2009-01-26 03:08:45 +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. |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 17 | #if defined(__i386__) || defined(__x86_64__) |
| 18 | |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 21 | #include "flash.h" |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 22 | #include "chipdrivers.h" |
hailfinger | 428f685 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 23 | #include "programmer.h" |
Edward O'Callaghan | e2f0135 | 2019-08-02 17:22:22 +1000 | [diff] [blame] | 24 | #include "hwaccess.h" |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 25 | #include "spi.h" |
| 26 | |
| 27 | #define WBSIO_PORT1 0x2e |
| 28 | #define WBSIO_PORT2 0x4e |
| 29 | |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 30 | struct wbsio_spi_data { |
| 31 | uint16_t spibase; |
| 32 | }; |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 33 | |
uwe | 5e931bc | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 34 | static uint16_t wbsio_get_spibase(uint16_t port) |
| 35 | { |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 36 | uint8_t id; |
| 37 | uint16_t flashport = 0; |
| 38 | |
| 39 | w836xx_ext_enter(port); |
hailfinger | 7bac0e5 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 40 | id = sio_read(port, 0x20); |
uwe | 5e931bc | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 41 | if (id != 0xa0) { |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 42 | msg_perr("\nW83627 not found at 0x%x, id=0x%02x want=0xa0.\n", port, id); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 43 | goto done; |
| 44 | } |
| 45 | |
hailfinger | 7bac0e5 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 46 | if (0 == (sio_read(port, 0x24) & 2)) { |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 47 | msg_perr("\nW83627 found at 0x%x, but SPI pins are not enabled. (CR[0x24] bit 1=0)\n", port); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 48 | goto done; |
| 49 | } |
| 50 | |
hailfinger | 7bac0e5 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 51 | sio_write(port, 0x07, 0x06); |
| 52 | if (0 == (sio_read(port, 0x30) & 1)) { |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 53 | msg_perr("\nW83627 found at 0x%x, but SPI is not enabled. (LDN6[0x30] bit 0=0)\n", port); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 54 | goto done; |
| 55 | } |
| 56 | |
hailfinger | 7bac0e5 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 57 | flashport = (sio_read(port, 0x62) << 8) | sio_read(port, 0x63); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 58 | |
| 59 | done: |
| 60 | w836xx_ext_leave(port); |
| 61 | return flashport; |
| 62 | } |
| 63 | |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 64 | /* W83627DHG has 11 command modes: |
| 65 | * 1=1 command only |
| 66 | * 2=1 command+1 data write |
| 67 | * 3=1 command+2 data read |
| 68 | * 4=1 command+3 address |
| 69 | * 5=1 command+3 address+1 data write |
| 70 | * 6=1 command+3 address+4 data write |
| 71 | * 7=1 command+3 address+1 dummy address inserted by wbsio+4 data read |
| 72 | * 8=1 command+3 address+1 data read |
| 73 | * 9=1 command+3 address+2 data read |
| 74 | * a=1 command+3 address+3 data read |
| 75 | * b=1 command+3 address+4 data read |
| 76 | * |
| 77 | * mode[7:4] holds the command mode |
| 78 | * mode[3:0] holds SPI address bits [19:16] |
| 79 | * |
| 80 | * The Winbond SPI master only supports 20 bit addresses on the SPI bus. :\ |
| 81 | * Would one more byte of RAM in the chip (to get all 24 bits) really make |
| 82 | * such a big difference? |
| 83 | */ |
Edward O'Callaghan | e2f0135 | 2019-08-02 17:22:22 +1000 | [diff] [blame] | 84 | static int wbsio_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
| 85 | unsigned int readcnt, |
| 86 | const unsigned char *writearr, |
| 87 | unsigned char *readarr) |
uwe | 5e931bc | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 88 | { |
Edward O'Callaghan | e2f0135 | 2019-08-02 17:22:22 +1000 | [diff] [blame] | 89 | unsigned int i; |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 90 | uint8_t mode = 0; |
| 91 | |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 92 | msg_pspew("%s:", __func__); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 93 | |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 94 | const struct wbsio_spi_data *data = |
| 95 | (const struct wbsio_spi_data *)flash->mst->spi.data; |
| 96 | |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 97 | if (1 == writecnt && 0 == readcnt) { |
| 98 | mode = 0x10; |
| 99 | } else if (2 == writecnt && 0 == readcnt) { |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 100 | OUTB(writearr[1], data->spibase + 4); |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 101 | msg_pspew(" data=0x%02x", writearr[1]); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 102 | mode = 0x20; |
| 103 | } else if (1 == writecnt && 2 == readcnt) { |
| 104 | mode = 0x30; |
| 105 | } else if (4 == writecnt && 0 == readcnt) { |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 106 | msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f)); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 107 | for (i = 2; i < writecnt; i++) { |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 108 | OUTB(writearr[i], data->spibase + i); |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 109 | msg_pspew("%02x", writearr[i]); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 110 | } |
| 111 | mode = 0x40 | (writearr[1] & 0x0f); |
| 112 | } else if (5 == writecnt && 0 == readcnt) { |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 113 | msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f)); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 114 | for (i = 2; i < 4; i++) { |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 115 | OUTB(writearr[i], data->spibase + i); |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 116 | msg_pspew("%02x", writearr[i]); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 117 | } |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 118 | OUTB(writearr[i], data->spibase + i); |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 119 | msg_pspew(" data=0x%02x", writearr[i]); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 120 | mode = 0x50 | (writearr[1] & 0x0f); |
| 121 | } else if (8 == writecnt && 0 == readcnt) { |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 122 | msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f)); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 123 | for (i = 2; i < 4; i++) { |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 124 | OUTB(writearr[i], data->spibase + i); |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 125 | msg_pspew("%02x", writearr[i]); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 126 | } |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 127 | msg_pspew(" data=0x"); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 128 | for (; i < writecnt; i++) { |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 129 | OUTB(writearr[i], data->spibase + i); |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 130 | msg_pspew("%02x", writearr[i]); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 131 | } |
| 132 | mode = 0x60 | (writearr[1] & 0x0f); |
| 133 | } else if (5 == writecnt && 4 == readcnt) { |
| 134 | /* XXX: TODO not supported by flashrom infrastructure! |
| 135 | * This mode, 7, discards the fifth byte in writecnt, |
| 136 | * but since we can not express that in flashrom, fail |
| 137 | * the operation for now. |
| 138 | */ |
| 139 | ; |
| 140 | } else if (4 == writecnt && readcnt >= 1 && readcnt <= 4) { |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 141 | msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f)); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 142 | for (i = 2; i < writecnt; i++) { |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 143 | OUTB(writearr[i], data->spibase + i); |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 144 | msg_pspew("%02x", writearr[i]); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 145 | } |
| 146 | mode = ((7 + readcnt) << 4) | (writearr[1] & 0x0f); |
| 147 | } |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 148 | msg_pspew(" cmd=%02x mode=%02x\n", writearr[0], mode); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 149 | |
| 150 | if (!mode) { |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 151 | msg_perr("%s: unsupported command type wr=%d rd=%d\n", |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 152 | __func__, writecnt, readcnt); |
hailfinger | 9c290a7 | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 153 | /* Command type refers to the number of bytes read/written. */ |
| 154 | return SPI_INVALID_LENGTH; |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 157 | OUTB(writearr[0], data->spibase); |
| 158 | OUTB(mode, data->spibase + 1); |
hailfinger | e5829f6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 159 | programmer_delay(10); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 160 | |
| 161 | if (!readcnt) |
| 162 | return 0; |
| 163 | |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 164 | msg_pspew("%s: returning data =", __func__); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 165 | for (i = 0; i < readcnt; i++) { |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 166 | readarr[i] = INB(data->spibase + 4 + i); |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 167 | msg_pspew(" 0x%02x", readarr[i]); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 168 | } |
snelson | cf6a285 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 169 | msg_pspew("\n"); |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
Edward O'Callaghan | e2f0135 | 2019-08-02 17:22:22 +1000 | [diff] [blame] | 173 | static int wbsio_spi_read(struct flashctx *flash, uint8_t *buf, |
| 174 | unsigned int start, unsigned int len) |
uwe | 5e931bc | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 175 | { |
Carl-Daniel Hailfinger | 5b0c465 | 2012-03-01 22:38:27 +0000 | [diff] [blame] | 176 | mmio_readn((void *)(flash->virtual_memory + start), buf, len); |
| 177 | return 0; |
stuge | a564bcf | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 180 | static const struct spi_master spi_master_wbsio = { |
Edward O'Callaghan | 07b19dd | 2020-11-23 11:54:49 +1100 | [diff] [blame] | 181 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 182 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 183 | .command = wbsio_spi_send_command, |
| 184 | .multicommand = default_spi_send_multicommand, |
| 185 | .read = wbsio_spi_read, |
| 186 | .write_256 = spi_chip_write_1, |
| 187 | .write_aai = spi_chip_write_1, |
| 188 | }; |
| 189 | |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 190 | static int wbsio_spi_shutdown(void *data) |
| 191 | { |
| 192 | free(data); |
| 193 | return 0; |
| 194 | } |
| 195 | |
Edward O'Callaghan | 07b19dd | 2020-11-23 11:54:49 +1100 | [diff] [blame] | 196 | int wbsio_check_for_spi(void) |
| 197 | { |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 198 | uint16_t wbsio_spibase = 0; |
| 199 | |
Edward O'Callaghan | 07b19dd | 2020-11-23 11:54:49 +1100 | [diff] [blame] | 200 | if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT1))) |
| 201 | if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT2))) |
| 202 | return 1; |
| 203 | |
| 204 | msg_pspew("\nwbsio_spibase = 0x%x\n", wbsio_spibase); |
| 205 | |
| 206 | msg_pdbg("%s: Winbond saved on 4 register bits so max chip size is " |
| 207 | "1024 kB!\n", __func__); |
| 208 | max_rom_decode.spi = 1024 * 1024; |
Edward O'Callaghan | 6665370 | 2020-11-23 12:07:30 +1100 | [diff] [blame] | 209 | |
| 210 | struct wbsio_spi_data * data = calloc(1, sizeof(struct wbsio_spi_data)); |
| 211 | if (!data) { |
| 212 | msg_perr("Unable to allocate space for extra SPI master data.\n"); |
| 213 | return SPI_GENERIC_ERROR; |
| 214 | } |
| 215 | data->spibase = wbsio_spibase; |
| 216 | |
| 217 | register_shutdown(wbsio_spi_shutdown, data); |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 218 | register_spi_master(&spi_master_wbsio, data); |
Edward O'Callaghan | 07b19dd | 2020-11-23 11:54:49 +1100 | [diff] [blame] | 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | #endif /* defined(__i386__) || defined(__x86_64__) */ |