Peter Stuge | bf196e9 | 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> |
Carl-Daniel Hailfinger | 5609fa7 | 2010-01-07 03:32:17 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009,2010 Carl-Daniel Hailfinger |
Peter Stuge | bf196e9 | 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. |
| 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 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 21 | #if defined(__i386__) || defined(__x86_64__) |
| 22 | |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 23 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 24 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 25 | #include "programmer.h" |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 26 | #include "spi.h" |
| 27 | |
| 28 | #define WBSIO_PORT1 0x2e |
| 29 | #define WBSIO_PORT2 0x4e |
| 30 | |
| 31 | static uint16_t wbsio_spibase = 0; |
| 32 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 33 | static uint16_t wbsio_get_spibase(uint16_t port) |
| 34 | { |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 35 | uint8_t id; |
| 36 | uint16_t flashport = 0; |
| 37 | |
| 38 | w836xx_ext_enter(port); |
Carl-Daniel Hailfinger | 24c1a16 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 39 | id = sio_read(port, 0x20); |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 40 | if (id != 0xa0) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 41 | msg_perr("\nW83627 not found at 0x%x, id=0x%02x want=0xa0.\n", port, id); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 42 | goto done; |
| 43 | } |
| 44 | |
Carl-Daniel Hailfinger | 24c1a16 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 45 | if (0 == (sio_read(port, 0x24) & 2)) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 46 | msg_perr("\nW83627 found at 0x%x, but SPI pins are not enabled. (CR[0x24] bit 1=0)\n", port); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 47 | goto done; |
| 48 | } |
| 49 | |
Carl-Daniel Hailfinger | 24c1a16 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 50 | sio_write(port, 0x07, 0x06); |
| 51 | if (0 == (sio_read(port, 0x30) & 1)) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 52 | msg_perr("\nW83627 found at 0x%x, but SPI is not enabled. (LDN6[0x30] bit 0=0)\n", port); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 53 | goto done; |
| 54 | } |
| 55 | |
Carl-Daniel Hailfinger | 24c1a16 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 56 | flashport = (sio_read(port, 0x62) << 8) | sio_read(port, 0x63); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 57 | |
| 58 | done: |
| 59 | w836xx_ext_leave(port); |
| 60 | return flashport; |
| 61 | } |
| 62 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 63 | static int wbsio_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 64 | const unsigned char *writearr, unsigned char *readarr); |
Stefan Tauner | 8c35745 | 2011-09-18 22:42:18 +0000 | [diff] [blame] | 65 | static int wbsio_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 66 | |
| 67 | static const struct spi_programmer spi_programmer_wbsio = { |
| 68 | .type = SPI_CONTROLLER_WBSIO, |
| 69 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 70 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 71 | .command = wbsio_spi_send_command, |
| 72 | .multicommand = default_spi_send_multicommand, |
| 73 | .read = wbsio_spi_read, |
| 74 | .write_256 = spi_chip_write_1, |
| 75 | }; |
| 76 | |
Uwe Hermann | 36dec8b | 2010-06-07 19:06:26 +0000 | [diff] [blame] | 77 | int wbsio_check_for_spi(void) |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 78 | { |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 79 | if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT1))) |
| 80 | if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT2))) |
| 81 | return 1; |
| 82 | |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 83 | msg_pspew("\nwbsio_spibase = 0x%x\n", wbsio_spibase); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 84 | |
Carl-Daniel Hailfinger | ca812d4 | 2010-07-14 19:57:52 +0000 | [diff] [blame] | 85 | msg_pdbg("%s: Winbond saved on 4 register bits so max chip size is " |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 86 | "1024 kB!\n", __func__); |
Carl-Daniel Hailfinger | ca812d4 | 2010-07-14 19:57:52 +0000 | [diff] [blame] | 87 | max_rom_decode.spi = 1024 * 1024; |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 88 | register_spi_programmer(&spi_programmer_wbsio); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 89 | |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /* W83627DHG has 11 command modes: |
| 94 | * 1=1 command only |
| 95 | * 2=1 command+1 data write |
| 96 | * 3=1 command+2 data read |
| 97 | * 4=1 command+3 address |
| 98 | * 5=1 command+3 address+1 data write |
| 99 | * 6=1 command+3 address+4 data write |
| 100 | * 7=1 command+3 address+1 dummy address inserted by wbsio+4 data read |
| 101 | * 8=1 command+3 address+1 data read |
| 102 | * 9=1 command+3 address+2 data read |
| 103 | * a=1 command+3 address+3 data read |
| 104 | * b=1 command+3 address+4 data read |
| 105 | * |
| 106 | * mode[7:4] holds the command mode |
| 107 | * mode[3:0] holds SPI address bits [19:16] |
| 108 | * |
| 109 | * The Winbond SPI master only supports 20 bit addresses on the SPI bus. :\ |
| 110 | * Would one more byte of RAM in the chip (to get all 24 bits) really make |
| 111 | * such a big difference? |
| 112 | */ |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 113 | static int wbsio_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 114 | const unsigned char *writearr, unsigned char *readarr) |
| 115 | { |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 116 | int i; |
| 117 | uint8_t mode = 0; |
| 118 | |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 119 | msg_pspew("%s:", __func__); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 120 | |
| 121 | if (1 == writecnt && 0 == readcnt) { |
| 122 | mode = 0x10; |
| 123 | } else if (2 == writecnt && 0 == readcnt) { |
| 124 | OUTB(writearr[1], wbsio_spibase + 4); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 125 | msg_pspew(" data=0x%02x", writearr[1]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 126 | mode = 0x20; |
| 127 | } else if (1 == writecnt && 2 == readcnt) { |
| 128 | mode = 0x30; |
| 129 | } else if (4 == writecnt && 0 == readcnt) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 130 | msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f)); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 131 | for (i = 2; i < writecnt; i++) { |
| 132 | OUTB(writearr[i], wbsio_spibase + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 133 | msg_pspew("%02x", writearr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 134 | } |
| 135 | mode = 0x40 | (writearr[1] & 0x0f); |
| 136 | } else if (5 == writecnt && 0 == readcnt) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 137 | msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f)); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 138 | for (i = 2; i < 4; i++) { |
| 139 | OUTB(writearr[i], wbsio_spibase + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 140 | msg_pspew("%02x", writearr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 141 | } |
| 142 | OUTB(writearr[i], wbsio_spibase + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 143 | msg_pspew(" data=0x%02x", writearr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 144 | mode = 0x50 | (writearr[1] & 0x0f); |
| 145 | } else if (8 == writecnt && 0 == readcnt) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 146 | msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f)); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 147 | for (i = 2; i < 4; i++) { |
| 148 | OUTB(writearr[i], wbsio_spibase + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 149 | msg_pspew("%02x", writearr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 150 | } |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 151 | msg_pspew(" data=0x"); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 152 | for (; i < writecnt; i++) { |
| 153 | OUTB(writearr[i], wbsio_spibase + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 154 | msg_pspew("%02x", writearr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 155 | } |
| 156 | mode = 0x60 | (writearr[1] & 0x0f); |
| 157 | } else if (5 == writecnt && 4 == readcnt) { |
| 158 | /* XXX: TODO not supported by flashrom infrastructure! |
| 159 | * This mode, 7, discards the fifth byte in writecnt, |
| 160 | * but since we can not express that in flashrom, fail |
| 161 | * the operation for now. |
| 162 | */ |
| 163 | ; |
| 164 | } else if (4 == writecnt && readcnt >= 1 && readcnt <= 4) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 165 | msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f)); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 166 | for (i = 2; i < writecnt; i++) { |
| 167 | OUTB(writearr[i], wbsio_spibase + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 168 | msg_pspew("%02x", writearr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 169 | } |
| 170 | mode = ((7 + readcnt) << 4) | (writearr[1] & 0x0f); |
| 171 | } |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 172 | msg_pspew(" cmd=%02x mode=%02x\n", writearr[0], mode); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 173 | |
| 174 | if (!mode) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 175 | msg_perr("%s: unsupported command type wr=%d rd=%d\n", |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 176 | __func__, writecnt, readcnt); |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 177 | /* Command type refers to the number of bytes read/written. */ |
| 178 | return SPI_INVALID_LENGTH; |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | OUTB(writearr[0], wbsio_spibase); |
| 182 | OUTB(mode, wbsio_spibase + 1); |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 183 | programmer_delay(10); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 184 | |
| 185 | if (!readcnt) |
| 186 | return 0; |
| 187 | |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 188 | msg_pspew("%s: returning data =", __func__); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 189 | for (i = 0; i < readcnt; i++) { |
| 190 | readarr[i] = INB(wbsio_spibase + 4 + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 191 | msg_pspew(" 0x%02x", readarr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 192 | } |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 193 | msg_pspew("\n"); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
Stefan Tauner | 8c35745 | 2011-09-18 22:42:18 +0000 | [diff] [blame] | 197 | static int wbsio_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 198 | { |
Carl-Daniel Hailfinger | 116081a | 2009-08-10 02:29:21 +0000 | [diff] [blame] | 199 | return read_memmapped(flash, buf, start, len); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 202 | #endif |