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