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. |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 17 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 18 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 19 | #include "programmer.h" |
Thomas Heijligen | 3f4d35d | 2022-01-17 15:11:43 +0100 | [diff] [blame] | 20 | #include "hwaccess_physmap.h" |
Thomas Heijligen | a065520 | 2021-12-14 16:36:05 +0100 | [diff] [blame] | 21 | #include "hwaccess_x86_io.h" |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 22 | #include "spi.h" |
| 23 | |
| 24 | #define WBSIO_PORT1 0x2e |
| 25 | #define WBSIO_PORT2 0x4e |
| 26 | |
| 27 | static uint16_t wbsio_spibase = 0; |
| 28 | |
Nico Huber | 56b53dd | 2023-01-11 23:15:20 +0100 | [diff] [blame^] | 29 | const size_t wbsio_max_mmapped = 1*MiB; /* maximum of memory mapped flash these SIOs can handle */ |
| 30 | static unsigned char *wbsio_mmapped_flash; |
| 31 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 32 | static uint16_t wbsio_get_spibase(uint16_t port) |
| 33 | { |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 34 | uint8_t id; |
| 35 | uint16_t flashport = 0; |
| 36 | |
| 37 | w836xx_ext_enter(port); |
Carl-Daniel Hailfinger | 24c1a16 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 38 | id = sio_read(port, 0x20); |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 39 | if (id != 0xa0) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 40 | 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] | 41 | goto done; |
| 42 | } |
| 43 | |
Carl-Daniel Hailfinger | 24c1a16 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 44 | if (0 == (sio_read(port, 0x24) & 2)) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 45 | 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] | 46 | goto done; |
| 47 | } |
| 48 | |
Carl-Daniel Hailfinger | 24c1a16 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 49 | sio_write(port, 0x07, 0x06); |
| 50 | if (0 == (sio_read(port, 0x30) & 1)) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 51 | 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] | 52 | goto done; |
| 53 | } |
| 54 | |
Carl-Daniel Hailfinger | 24c1a16 | 2009-05-25 23:26:50 +0000 | [diff] [blame] | 55 | flashport = (sio_read(port, 0x62) << 8) | sio_read(port, 0x63); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 56 | |
| 57 | done: |
| 58 | w836xx_ext_leave(port); |
| 59 | return flashport; |
| 60 | } |
| 61 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 62 | static int wbsio_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 63 | unsigned int readcnt, |
| 64 | const unsigned char *writearr, |
| 65 | unsigned char *readarr); |
| 66 | static int wbsio_spi_read(struct flashctx *flash, uint8_t *buf, |
| 67 | unsigned int start, unsigned int len); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 68 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 69 | static const struct spi_master spi_master_wbsio = { |
Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 70 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 71 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 72 | .command = wbsio_spi_send_command, |
| 73 | .multicommand = default_spi_send_multicommand, |
| 74 | .read = wbsio_spi_read, |
| 75 | .write_256 = spi_chip_write_1, |
| 76 | .write_aai = spi_chip_write_1, |
Aarya Chaumal | 0cea753 | 2022-07-04 18:21:50 +0530 | [diff] [blame] | 77 | .probe_opcode = default_spi_probe_opcode, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Uwe Hermann | 36dec8b | 2010-06-07 19:06:26 +0000 | [diff] [blame] | 80 | int wbsio_check_for_spi(void) |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 81 | { |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 82 | if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT1))) |
| 83 | if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT2))) |
| 84 | return 1; |
| 85 | |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 86 | msg_pspew("\nwbsio_spibase = 0x%x\n", wbsio_spibase); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 87 | |
Nico Huber | 56b53dd | 2023-01-11 23:15:20 +0100 | [diff] [blame^] | 88 | wbsio_mmapped_flash = rphysmap("wbsio memory mapped SPI", |
| 89 | 0xffffffff - wbsio_max_mmapped + 1, wbsio_max_mmapped); |
| 90 | if (wbsio_mmapped_flash == ERROR_PTR) |
| 91 | return 1; |
| 92 | |
Carl-Daniel Hailfinger | ca812d4 | 2010-07-14 19:57:52 +0000 | [diff] [blame] | 93 | 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] | 94 | "1024 kB!\n", __func__); |
Nico Huber | 56b53dd | 2023-01-11 23:15:20 +0100 | [diff] [blame^] | 95 | max_rom_decode.spi = wbsio_max_mmapped; |
Nico Huber | 5e08e3e | 2021-05-11 17:38:14 +0200 | [diff] [blame] | 96 | register_spi_master(&spi_master_wbsio, NULL); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 97 | |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /* W83627DHG has 11 command modes: |
| 102 | * 1=1 command only |
| 103 | * 2=1 command+1 data write |
| 104 | * 3=1 command+2 data read |
| 105 | * 4=1 command+3 address |
| 106 | * 5=1 command+3 address+1 data write |
| 107 | * 6=1 command+3 address+4 data write |
| 108 | * 7=1 command+3 address+1 dummy address inserted by wbsio+4 data read |
| 109 | * 8=1 command+3 address+1 data read |
| 110 | * 9=1 command+3 address+2 data read |
| 111 | * a=1 command+3 address+3 data read |
| 112 | * b=1 command+3 address+4 data read |
| 113 | * |
| 114 | * mode[7:4] holds the command mode |
| 115 | * mode[3:0] holds SPI address bits [19:16] |
| 116 | * |
| 117 | * The Winbond SPI master only supports 20 bit addresses on the SPI bus. :\ |
| 118 | * Would one more byte of RAM in the chip (to get all 24 bits) really make |
| 119 | * such a big difference? |
| 120 | */ |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 121 | static int wbsio_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 122 | unsigned int readcnt, |
| 123 | const unsigned char *writearr, |
| 124 | unsigned char *readarr) |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 125 | { |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 126 | unsigned int i; |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 127 | uint8_t mode = 0; |
| 128 | |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 129 | msg_pspew("%s:", __func__); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 130 | |
| 131 | if (1 == writecnt && 0 == readcnt) { |
| 132 | mode = 0x10; |
| 133 | } else if (2 == writecnt && 0 == readcnt) { |
| 134 | OUTB(writearr[1], wbsio_spibase + 4); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 135 | msg_pspew(" data=0x%02x", writearr[1]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 136 | mode = 0x20; |
| 137 | } else if (1 == writecnt && 2 == readcnt) { |
| 138 | mode = 0x30; |
| 139 | } else if (4 == writecnt && 0 == readcnt) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 140 | msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f)); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 141 | for (i = 2; i < writecnt; i++) { |
| 142 | OUTB(writearr[i], wbsio_spibase + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 143 | msg_pspew("%02x", writearr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 144 | } |
| 145 | mode = 0x40 | (writearr[1] & 0x0f); |
| 146 | } else if (5 == writecnt && 0 == readcnt) { |
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 < 4; 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 | OUTB(writearr[i], wbsio_spibase + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 153 | msg_pspew(" data=0x%02x", writearr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 154 | mode = 0x50 | (writearr[1] & 0x0f); |
| 155 | } else if (8 == writecnt && 0 == readcnt) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 156 | msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f)); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 157 | for (i = 2; i < 4; i++) { |
| 158 | OUTB(writearr[i], wbsio_spibase + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 159 | msg_pspew("%02x", writearr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 160 | } |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 161 | msg_pspew(" data=0x"); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 162 | for (; i < writecnt; i++) { |
| 163 | OUTB(writearr[i], wbsio_spibase + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 164 | msg_pspew("%02x", writearr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 165 | } |
| 166 | mode = 0x60 | (writearr[1] & 0x0f); |
| 167 | } else if (5 == writecnt && 4 == readcnt) { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 168 | /* XXX: TODO not supported by flashprog infrastructure! |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 169 | * This mode, 7, discards the fifth byte in writecnt, |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 170 | * but since we can not express that in flashprog, fail |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 171 | * the operation for now. |
| 172 | */ |
| 173 | ; |
| 174 | } else if (4 == writecnt && readcnt >= 1 && readcnt <= 4) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 175 | msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f)); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 176 | for (i = 2; i < writecnt; i++) { |
| 177 | OUTB(writearr[i], wbsio_spibase + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 178 | msg_pspew("%02x", writearr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 179 | } |
| 180 | mode = ((7 + readcnt) << 4) | (writearr[1] & 0x0f); |
| 181 | } |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 182 | msg_pspew(" cmd=%02x mode=%02x\n", writearr[0], mode); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 183 | |
| 184 | if (!mode) { |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 185 | msg_perr("%s: unsupported command type wr=%d rd=%d\n", |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 186 | __func__, writecnt, readcnt); |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 187 | /* Command type refers to the number of bytes read/written. */ |
| 188 | return SPI_INVALID_LENGTH; |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | OUTB(writearr[0], wbsio_spibase); |
| 192 | OUTB(mode, wbsio_spibase + 1); |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 193 | programmer_delay(10); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 194 | |
| 195 | if (!readcnt) |
| 196 | return 0; |
| 197 | |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 198 | msg_pspew("%s: returning data =", __func__); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 199 | for (i = 0; i < readcnt; i++) { |
| 200 | readarr[i] = INB(wbsio_spibase + 4 + i); |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 201 | msg_pspew(" 0x%02x", readarr[i]); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 202 | } |
Sean Nelson | f7f7a55 | 2010-01-09 23:34:45 +0000 | [diff] [blame] | 203 | msg_pspew("\n"); |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 207 | static int wbsio_spi_read(struct flashctx *flash, uint8_t *buf, |
| 208 | unsigned int start, unsigned int len) |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 209 | { |
Nico Huber | 56b53dd | 2023-01-11 23:15:20 +0100 | [diff] [blame^] | 210 | unsigned char *const bios = wbsio_mmapped_flash + |
| 211 | wbsio_max_mmapped - flashprog_flash_getsize(flash); |
| 212 | mmio_readn(bios + start, buf, len); |
Carl-Daniel Hailfinger | ccd71c2 | 2012-03-01 22:38:27 +0000 | [diff] [blame] | 213 | return 0; |
Peter Stuge | bf196e9 | 2009-01-26 03:08:45 +0000 | [diff] [blame] | 214 | } |