blob: b8f8b38a120dd9db8cba36201ca1b6cfbb5fe463 [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
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000021#if defined(__i386__) || defined(__x86_64__)
22
Peter Stugebf196e92009-01-26 03:08:45 +000023#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000024#include "chipdrivers.h"
Peter Stugebf196e92009-01-26 03:08:45 +000025#include "spi.h"
26
27#define WBSIO_PORT1 0x2e
28#define WBSIO_PORT2 0x4e
29
30static uint16_t wbsio_spibase = 0;
31
Uwe Hermann7b2969b2009-04-15 10:52:49 +000032static uint16_t wbsio_get_spibase(uint16_t port)
33{
Peter Stugebf196e92009-01-26 03:08:45 +000034 uint8_t id;
35 uint16_t flashport = 0;
36
37 w836xx_ext_enter(port);
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000038 id = sio_read(port, 0x20);
Uwe Hermann7b2969b2009-04-15 10:52:49 +000039 if (id != 0xa0) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +000040 msg_perr("\nW83627 not found at 0x%x, id=0x%02x want=0xa0.\n", port, id);
Peter Stugebf196e92009-01-26 03:08:45 +000041 goto done;
42 }
43
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000044 if (0 == (sio_read(port, 0x24) & 2)) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +000045 msg_perr("\nW83627 found at 0x%x, but SPI pins are not enabled. (CR[0x24] bit 1=0)\n", port);
Peter Stugebf196e92009-01-26 03:08:45 +000046 goto done;
47 }
48
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000049 sio_write(port, 0x07, 0x06);
50 if (0 == (sio_read(port, 0x30) & 1)) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +000051 msg_perr("\nW83627 found at 0x%x, but SPI is not enabled. (LDN6[0x30] bit 0=0)\n", port);
Peter Stugebf196e92009-01-26 03:08:45 +000052 goto done;
53 }
54
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000055 flashport = (sio_read(port, 0x62) << 8) | sio_read(port, 0x63);
Peter Stugebf196e92009-01-26 03:08:45 +000056
57done:
58 w836xx_ext_leave(port);
59 return flashport;
60}
61
Uwe Hermann36dec8b2010-06-07 19:06:26 +000062int wbsio_check_for_spi(void)
Uwe Hermann7b2969b2009-04-15 10:52:49 +000063{
Peter Stugebf196e92009-01-26 03:08:45 +000064 if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT1)))
65 if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT2)))
66 return 1;
67
Sean Nelsonf7f7a552010-01-09 23:34:45 +000068 msg_pspew("\nwbsio_spibase = 0x%x\n", wbsio_spibase);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +000069
70 buses_supported |= CHIP_BUSTYPE_SPI;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000071 spi_controller = SPI_CONTROLLER_WBSIO;
Carl-Daniel Hailfingerca812d42010-07-14 19:57:52 +000072 msg_pdbg("%s: Winbond saved on 4 register bits so max chip size is "
73 "1024 KB!\n", __func__);
74 max_rom_decode.spi = 1024 * 1024;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +000075
Peter Stugebf196e92009-01-26 03:08:45 +000076 return 0;
77}
78
79/* W83627DHG has 11 command modes:
80 * 1=1 command only
81 * 2=1 command+1 data write
82 * 3=1 command+2 data read
83 * 4=1 command+3 address
84 * 5=1 command+3 address+1 data write
85 * 6=1 command+3 address+4 data write
86 * 7=1 command+3 address+1 dummy address inserted by wbsio+4 data read
87 * 8=1 command+3 address+1 data read
88 * 9=1 command+3 address+2 data read
89 * a=1 command+3 address+3 data read
90 * b=1 command+3 address+4 data read
91 *
92 * mode[7:4] holds the command mode
93 * mode[3:0] holds SPI address bits [19:16]
94 *
95 * The Winbond SPI master only supports 20 bit addresses on the SPI bus. :\
96 * Would one more byte of RAM in the chip (to get all 24 bits) really make
97 * such a big difference?
98 */
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000099int wbsio_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000100 const unsigned char *writearr, unsigned char *readarr)
101{
Peter Stugebf196e92009-01-26 03:08:45 +0000102 int i;
103 uint8_t mode = 0;
104
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000105 msg_pspew("%s:", __func__);
Peter Stugebf196e92009-01-26 03:08:45 +0000106
107 if (1 == writecnt && 0 == readcnt) {
108 mode = 0x10;
109 } else if (2 == writecnt && 0 == readcnt) {
110 OUTB(writearr[1], wbsio_spibase + 4);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000111 msg_pspew(" data=0x%02x", writearr[1]);
Peter Stugebf196e92009-01-26 03:08:45 +0000112 mode = 0x20;
113 } else if (1 == writecnt && 2 == readcnt) {
114 mode = 0x30;
115 } else if (4 == writecnt && 0 == readcnt) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000116 msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000117 for (i = 2; i < writecnt; i++) {
118 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000119 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000120 }
121 mode = 0x40 | (writearr[1] & 0x0f);
122 } else if (5 == writecnt && 0 == readcnt) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000123 msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000124 for (i = 2; i < 4; i++) {
125 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000126 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000127 }
128 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000129 msg_pspew(" data=0x%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000130 mode = 0x50 | (writearr[1] & 0x0f);
131 } else if (8 == writecnt && 0 == readcnt) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000132 msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000133 for (i = 2; i < 4; i++) {
134 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000135 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000136 }
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000137 msg_pspew(" data=0x");
Peter Stugebf196e92009-01-26 03:08:45 +0000138 for (; i < writecnt; i++) {
139 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000140 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000141 }
142 mode = 0x60 | (writearr[1] & 0x0f);
143 } else if (5 == writecnt && 4 == readcnt) {
144 /* XXX: TODO not supported by flashrom infrastructure!
145 * This mode, 7, discards the fifth byte in writecnt,
146 * but since we can not express that in flashrom, fail
147 * the operation for now.
148 */
149 ;
150 } else if (4 == writecnt && readcnt >= 1 && readcnt <= 4) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000151 msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000152 for (i = 2; i < writecnt; i++) {
153 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000154 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000155 }
156 mode = ((7 + readcnt) << 4) | (writearr[1] & 0x0f);
157 }
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000158 msg_pspew(" cmd=%02x mode=%02x\n", writearr[0], mode);
Peter Stugebf196e92009-01-26 03:08:45 +0000159
160 if (!mode) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000161 msg_perr("%s: unsupported command type wr=%d rd=%d\n",
Peter Stugebf196e92009-01-26 03:08:45 +0000162 __func__, writecnt, readcnt);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000163 /* Command type refers to the number of bytes read/written. */
164 return SPI_INVALID_LENGTH;
Peter Stugebf196e92009-01-26 03:08:45 +0000165 }
166
167 OUTB(writearr[0], wbsio_spibase);
168 OUTB(mode, wbsio_spibase + 1);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000169 programmer_delay(10);
Peter Stugebf196e92009-01-26 03:08:45 +0000170
171 if (!readcnt)
172 return 0;
173
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000174 msg_pspew("%s: returning data =", __func__);
Peter Stugebf196e92009-01-26 03:08:45 +0000175 for (i = 0; i < readcnt; i++) {
176 readarr[i] = INB(wbsio_spibase + 4 + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000177 msg_pspew(" 0x%02x", readarr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000178 }
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000179 msg_pspew("\n");
Peter Stugebf196e92009-01-26 03:08:45 +0000180 return 0;
181}
182
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000183int wbsio_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000184{
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000185 return read_memmapped(flash, buf, start, len);
Peter Stugebf196e92009-01-26 03:08:45 +0000186}
187
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000188#endif