blob: 2df5bddd5b99fef37649f799c51f52ded23dcdaf [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>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
Peter Stugebf196e92009-01-26 03:08:45 +000020#include <string.h>
21#include "flash.h"
22#include "spi.h"
23
24#define WBSIO_PORT1 0x2e
25#define WBSIO_PORT2 0x4e
26
27static uint16_t wbsio_spibase = 0;
28
Uwe Hermann7b2969b2009-04-15 10:52:49 +000029static uint16_t wbsio_get_spibase(uint16_t port)
30{
Peter Stugebf196e92009-01-26 03:08:45 +000031 uint8_t id;
32 uint16_t flashport = 0;
33
34 w836xx_ext_enter(port);
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000035 id = sio_read(port, 0x20);
Uwe Hermann7b2969b2009-04-15 10:52:49 +000036 if (id != 0xa0) {
Peter Stugebf196e92009-01-26 03:08:45 +000037 fprintf(stderr, "\nW83627 not found at 0x%x, id=0x%02x want=0xa0.\n", port, id);
38 goto done;
39 }
40
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000041 if (0 == (sio_read(port, 0x24) & 2)) {
Peter Stugebf196e92009-01-26 03:08:45 +000042 fprintf(stderr, "\nW83627 found at 0x%x, but SPI pins are not enabled. (CR[0x24] bit 1=0)\n", port);
43 goto done;
44 }
45
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000046 sio_write(port, 0x07, 0x06);
47 if (0 == (sio_read(port, 0x30) & 1)) {
Peter Stugebf196e92009-01-26 03:08:45 +000048 fprintf(stderr, "\nW83627 found at 0x%x, but SPI is not enabled. (LDN6[0x30] bit 0=0)\n", port);
49 goto done;
50 }
51
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000052 flashport = (sio_read(port, 0x62) << 8) | sio_read(port, 0x63);
Peter Stugebf196e92009-01-26 03:08:45 +000053
54done:
55 w836xx_ext_leave(port);
56 return flashport;
57}
58
Uwe Hermann7b2969b2009-04-15 10:52:49 +000059int wbsio_check_for_spi(const char *name)
60{
Peter Stugebf196e92009-01-26 03:08:45 +000061 if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT1)))
62 if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT2)))
63 return 1;
64
65 printf_debug("\nwbsio_spibase = 0x%x\n", wbsio_spibase);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000066 spi_controller = SPI_CONTROLLER_WBSIO;
Peter Stugebf196e92009-01-26 03:08:45 +000067 return 0;
68}
69
70/* W83627DHG has 11 command modes:
71 * 1=1 command only
72 * 2=1 command+1 data write
73 * 3=1 command+2 data read
74 * 4=1 command+3 address
75 * 5=1 command+3 address+1 data write
76 * 6=1 command+3 address+4 data write
77 * 7=1 command+3 address+1 dummy address inserted by wbsio+4 data read
78 * 8=1 command+3 address+1 data read
79 * 9=1 command+3 address+2 data read
80 * a=1 command+3 address+3 data read
81 * b=1 command+3 address+4 data read
82 *
83 * mode[7:4] holds the command mode
84 * mode[3:0] holds SPI address bits [19:16]
85 *
86 * The Winbond SPI master only supports 20 bit addresses on the SPI bus. :\
87 * Would one more byte of RAM in the chip (to get all 24 bits) really make
88 * such a big difference?
89 */
Uwe Hermann7b2969b2009-04-15 10:52:49 +000090int wbsio_spi_command(unsigned int writecnt, unsigned int readcnt,
91 const unsigned char *writearr, unsigned char *readarr)
92{
Peter Stugebf196e92009-01-26 03:08:45 +000093 int i;
94 uint8_t mode = 0;
95
96 printf_debug("%s:", __func__);
97
98 if (1 == writecnt && 0 == readcnt) {
99 mode = 0x10;
100 } else if (2 == writecnt && 0 == readcnt) {
101 OUTB(writearr[1], wbsio_spibase + 4);
102 printf_debug(" data=0x%02x", writearr[1]);
103 mode = 0x20;
104 } else if (1 == writecnt && 2 == readcnt) {
105 mode = 0x30;
106 } else if (4 == writecnt && 0 == readcnt) {
107 printf_debug(" addr=0x%02x", (writearr[1] & 0x0f));
108 for (i = 2; i < writecnt; i++) {
109 OUTB(writearr[i], wbsio_spibase + i);
110 printf_debug("%02x", writearr[i]);
111 }
112 mode = 0x40 | (writearr[1] & 0x0f);
113 } else if (5 == writecnt && 0 == readcnt) {
114 printf_debug(" addr=0x%02x", (writearr[1] & 0x0f));
115 for (i = 2; i < 4; i++) {
116 OUTB(writearr[i], wbsio_spibase + i);
117 printf_debug("%02x", writearr[i]);
118 }
119 OUTB(writearr[i], wbsio_spibase + i);
120 printf_debug(" data=0x%02x", writearr[i]);
121 mode = 0x50 | (writearr[1] & 0x0f);
122 } else if (8 == writecnt && 0 == readcnt) {
123 printf_debug(" addr=0x%02x", (writearr[1] & 0x0f));
124 for (i = 2; i < 4; i++) {
125 OUTB(writearr[i], wbsio_spibase + i);
126 printf_debug("%02x", writearr[i]);
127 }
128 printf_debug(" data=0x");
129 for (; i < writecnt; i++) {
130 OUTB(writearr[i], wbsio_spibase + i);
131 printf_debug("%02x", writearr[i]);
132 }
133 mode = 0x60 | (writearr[1] & 0x0f);
134 } else if (5 == writecnt && 4 == readcnt) {
135 /* XXX: TODO not supported by flashrom infrastructure!
136 * This mode, 7, discards the fifth byte in writecnt,
137 * but since we can not express that in flashrom, fail
138 * the operation for now.
139 */
140 ;
141 } else if (4 == writecnt && readcnt >= 1 && readcnt <= 4) {
142 printf_debug(" addr=0x%02x", (writearr[1] & 0x0f));
143 for (i = 2; i < writecnt; i++) {
144 OUTB(writearr[i], wbsio_spibase + i);
145 printf_debug("%02x", writearr[i]);
146 }
147 mode = ((7 + readcnt) << 4) | (writearr[1] & 0x0f);
148 }
149 printf_debug(" cmd=%02x mode=%02x\n", writearr[0], mode);
150
151 if (!mode) {
152 fprintf(stderr, "%s: unsupported command type wr=%d rd=%d\n",
153 __func__, writecnt, readcnt);
154 return 1;
155 }
156
157 OUTB(writearr[0], wbsio_spibase);
158 OUTB(mode, wbsio_spibase + 1);
159 myusec_delay(10);
160
161 if (!readcnt)
162 return 0;
163
164 printf_debug("%s: returning data =", __func__);
165 for (i = 0; i < readcnt; i++) {
166 readarr[i] = INB(wbsio_spibase + 4 + i);
167 printf_debug(" 0x%02x", readarr[i]);
168 }
169 printf_debug("\n");
170 return 0;
171}
172
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000173int wbsio_spi_read(struct flashchip *flash, uint8_t *buf)
174{
Peter Stugebf196e92009-01-26 03:08:45 +0000175 int size = flash->total_size * 1024;
176
177 if (flash->total_size > 1024) {
178 fprintf(stderr, "%s: Winbond saved on 4 register bits so max chip size is 1024 KB!\n", __func__);
179 return 1;
180 }
181
182 memcpy(buf, (const char *)flash->virtual_memory, size);
183 return 0;
184}
185
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000186int wbsio_spi_write_1(struct flashchip *flash, uint8_t *buf)
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000187{
Peter Stugebf196e92009-01-26 03:08:45 +0000188 int pos, size = flash->total_size * 1024;
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000189 int result;
Peter Stugebf196e92009-01-26 03:08:45 +0000190
191 if (flash->total_size > 1024) {
192 fprintf(stderr, "%s: Winbond saved on 4 register bits so max chip size is 1024 KB!\n", __func__);
193 return 1;
194 }
195
196 flash->erase(flash);
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +0000197 result = spi_write_enable();
198 if (result)
199 return result;
Peter Stugebf196e92009-01-26 03:08:45 +0000200 for (pos = 0; pos < size; pos++) {
201 spi_byte_program(pos, buf[pos]);
202 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
203 myusec_delay(10);
204 }
205 spi_write_disable();
206 return 0;
207}