blob: e6b94ad7a4a16f6682acb9f4fc453d4c9c8c86ff [file] [log] [blame]
Peter Stugebf196e92009-01-26 03:08:45 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2008 Peter Stuge <peter@stuge.se>
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +00005 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
Peter Stugebf196e92009-01-26 03:08:45 +00006 *
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 Stugebf196e92009-01-26 03:08:45 +000021#include <string.h>
22#include "flash.h"
23#include "spi.h"
24
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +000025/* Change this to #define if you want lowlevel debugging of commands
26 * sent to the Winbond W836xx SPI controller.
27 */
28#undef COMM_DEBUG
29
30#ifdef COMM_DEBUG
31#define msg_comm_debug printf_debug
32#else
33#define msg_comm_debug(...) do {} while (0)
34#endif
35
Peter Stugebf196e92009-01-26 03:08:45 +000036#define WBSIO_PORT1 0x2e
37#define WBSIO_PORT2 0x4e
38
39static uint16_t wbsio_spibase = 0;
40
Uwe Hermann7b2969b2009-04-15 10:52:49 +000041static uint16_t wbsio_get_spibase(uint16_t port)
42{
Peter Stugebf196e92009-01-26 03:08:45 +000043 uint8_t id;
44 uint16_t flashport = 0;
45
46 w836xx_ext_enter(port);
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000047 id = sio_read(port, 0x20);
Uwe Hermann7b2969b2009-04-15 10:52:49 +000048 if (id != 0xa0) {
Peter Stugebf196e92009-01-26 03:08:45 +000049 fprintf(stderr, "\nW83627 not found at 0x%x, id=0x%02x want=0xa0.\n", port, id);
50 goto done;
51 }
52
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000053 if (0 == (sio_read(port, 0x24) & 2)) {
Peter Stugebf196e92009-01-26 03:08:45 +000054 fprintf(stderr, "\nW83627 found at 0x%x, but SPI pins are not enabled. (CR[0x24] bit 1=0)\n", port);
55 goto done;
56 }
57
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000058 sio_write(port, 0x07, 0x06);
59 if (0 == (sio_read(port, 0x30) & 1)) {
Peter Stugebf196e92009-01-26 03:08:45 +000060 fprintf(stderr, "\nW83627 found at 0x%x, but SPI is not enabled. (LDN6[0x30] bit 0=0)\n", port);
61 goto done;
62 }
63
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000064 flashport = (sio_read(port, 0x62) << 8) | sio_read(port, 0x63);
Peter Stugebf196e92009-01-26 03:08:45 +000065
66done:
67 w836xx_ext_leave(port);
68 return flashport;
69}
70
Uwe Hermann7b2969b2009-04-15 10:52:49 +000071int wbsio_check_for_spi(const char *name)
72{
Peter Stugebf196e92009-01-26 03:08:45 +000073 if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT1)))
74 if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT2)))
75 return 1;
76
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +000077 msg_comm_debug("\nwbsio_spibase = 0x%x\n", wbsio_spibase);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +000078
79 buses_supported |= CHIP_BUSTYPE_SPI;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000080 spi_controller = SPI_CONTROLLER_WBSIO;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +000081
Peter Stugebf196e92009-01-26 03:08:45 +000082 return 0;
83}
84
85/* W83627DHG has 11 command modes:
86 * 1=1 command only
87 * 2=1 command+1 data write
88 * 3=1 command+2 data read
89 * 4=1 command+3 address
90 * 5=1 command+3 address+1 data write
91 * 6=1 command+3 address+4 data write
92 * 7=1 command+3 address+1 dummy address inserted by wbsio+4 data read
93 * 8=1 command+3 address+1 data read
94 * 9=1 command+3 address+2 data read
95 * a=1 command+3 address+3 data read
96 * b=1 command+3 address+4 data read
97 *
98 * mode[7:4] holds the command mode
99 * mode[3:0] holds SPI address bits [19:16]
100 *
101 * The Winbond SPI master only supports 20 bit addresses on the SPI bus. :\
102 * Would one more byte of RAM in the chip (to get all 24 bits) really make
103 * such a big difference?
104 */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000105int wbsio_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000106 const unsigned char *writearr, unsigned char *readarr)
107{
Peter Stugebf196e92009-01-26 03:08:45 +0000108 int i;
109 uint8_t mode = 0;
110
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000111 msg_comm_debug("%s:", __func__);
Peter Stugebf196e92009-01-26 03:08:45 +0000112
113 if (1 == writecnt && 0 == readcnt) {
114 mode = 0x10;
115 } else if (2 == writecnt && 0 == readcnt) {
116 OUTB(writearr[1], wbsio_spibase + 4);
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000117 msg_comm_debug(" data=0x%02x", writearr[1]);
Peter Stugebf196e92009-01-26 03:08:45 +0000118 mode = 0x20;
119 } else if (1 == writecnt && 2 == readcnt) {
120 mode = 0x30;
121 } else if (4 == writecnt && 0 == readcnt) {
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000122 msg_comm_debug(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000123 for (i = 2; i < writecnt; i++) {
124 OUTB(writearr[i], wbsio_spibase + i);
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000125 msg_comm_debug("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000126 }
127 mode = 0x40 | (writearr[1] & 0x0f);
128 } else if (5 == writecnt && 0 == readcnt) {
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000129 msg_comm_debug(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000130 for (i = 2; i < 4; i++) {
131 OUTB(writearr[i], wbsio_spibase + i);
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000132 msg_comm_debug("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000133 }
134 OUTB(writearr[i], wbsio_spibase + i);
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000135 msg_comm_debug(" data=0x%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000136 mode = 0x50 | (writearr[1] & 0x0f);
137 } else if (8 == writecnt && 0 == readcnt) {
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000138 msg_comm_debug(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000139 for (i = 2; i < 4; i++) {
140 OUTB(writearr[i], wbsio_spibase + i);
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000141 msg_comm_debug("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000142 }
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000143 msg_comm_debug(" data=0x");
Peter Stugebf196e92009-01-26 03:08:45 +0000144 for (; i < writecnt; i++) {
145 OUTB(writearr[i], wbsio_spibase + i);
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000146 msg_comm_debug("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000147 }
148 mode = 0x60 | (writearr[1] & 0x0f);
149 } else if (5 == writecnt && 4 == readcnt) {
150 /* XXX: TODO not supported by flashrom infrastructure!
151 * This mode, 7, discards the fifth byte in writecnt,
152 * but since we can not express that in flashrom, fail
153 * the operation for now.
154 */
155 ;
156 } else if (4 == writecnt && readcnt >= 1 && readcnt <= 4) {
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000157 msg_comm_debug(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000158 for (i = 2; i < writecnt; i++) {
159 OUTB(writearr[i], wbsio_spibase + i);
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000160 msg_comm_debug("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000161 }
162 mode = ((7 + readcnt) << 4) | (writearr[1] & 0x0f);
163 }
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000164 msg_comm_debug(" cmd=%02x mode=%02x\n", writearr[0], mode);
Peter Stugebf196e92009-01-26 03:08:45 +0000165
166 if (!mode) {
167 fprintf(stderr, "%s: unsupported command type wr=%d rd=%d\n",
168 __func__, writecnt, readcnt);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000169 /* Command type refers to the number of bytes read/written. */
170 return SPI_INVALID_LENGTH;
Peter Stugebf196e92009-01-26 03:08:45 +0000171 }
172
173 OUTB(writearr[0], wbsio_spibase);
174 OUTB(mode, wbsio_spibase + 1);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000175 programmer_delay(10);
Peter Stugebf196e92009-01-26 03:08:45 +0000176
177 if (!readcnt)
178 return 0;
179
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000180 msg_comm_debug("%s: returning data =", __func__);
Peter Stugebf196e92009-01-26 03:08:45 +0000181 for (i = 0; i < readcnt; i++) {
182 readarr[i] = INB(wbsio_spibase + 4 + i);
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000183 msg_comm_debug(" 0x%02x", readarr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000184 }
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000185 msg_comm_debug("\n");
Peter Stugebf196e92009-01-26 03:08:45 +0000186 return 0;
187}
188
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000189int wbsio_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000190{
Peter Stugebf196e92009-01-26 03:08:45 +0000191 int size = flash->total_size * 1024;
192
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000193 if (size > 1024 * 1024) {
Peter Stugebf196e92009-01-26 03:08:45 +0000194 fprintf(stderr, "%s: Winbond saved on 4 register bits so max chip size is 1024 KB!\n", __func__);
195 return 1;
196 }
197
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000198 return read_memmapped(flash, buf, start, len);
Peter Stugebf196e92009-01-26 03:08:45 +0000199}
200
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000201int wbsio_spi_write_1(struct flashchip *flash, uint8_t *buf)
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000202{
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000203 int size = flash->total_size * 1024;
Peter Stugebf196e92009-01-26 03:08:45 +0000204
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000205 if (size > 1024 * 1024) {
Peter Stugebf196e92009-01-26 03:08:45 +0000206 fprintf(stderr, "%s: Winbond saved on 4 register bits so max chip size is 1024 KB!\n", __func__);
207 return 1;
208 }
209
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000210 return spi_chip_write_1(flash, buf);
Peter Stugebf196e92009-01-26 03:08:45 +0000211}