blob: 52a8c7de0015c5818aa4709990fcf1b0443d61eb [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.
Peter Stugebf196e92009-01-26 03:08:45 +000015 */
16
Peter Stugebf196e92009-01-26 03:08:45 +000017#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000018#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000019#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000020#include "hwaccess.h"
Thomas Heijligena0655202021-12-14 16:36:05 +010021#include "hwaccess_x86_io.h"
Peter Stugebf196e92009-01-26 03:08:45 +000022#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) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +000037 msg_perr("\nW83627 not found at 0x%x, id=0x%02x want=0xa0.\n", port, id);
Peter Stugebf196e92009-01-26 03:08:45 +000038 goto done;
39 }
40
Carl-Daniel Hailfinger24c1a162009-05-25 23:26:50 +000041 if (0 == (sio_read(port, 0x24) & 2)) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +000042 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 +000043 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)) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +000048 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 +000049 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
Edward O'Callaghan5eca4272020-04-12 17:27:53 +100059static int wbsio_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000060 unsigned int readcnt,
61 const unsigned char *writearr,
62 unsigned char *readarr);
63static int wbsio_spi_read(struct flashctx *flash, uint8_t *buf,
64 unsigned int start, unsigned int len);
Michael Karcherb9dbe482011-05-11 17:07:07 +000065
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000066static const struct spi_master spi_master_wbsio = {
Thomas Heijligen43040f22022-06-23 14:38:35 +020067 .max_data_read = MAX_DATA_UNSPECIFIED,
68 .max_data_write = MAX_DATA_UNSPECIFIED,
69 .command = wbsio_spi_send_command,
70 .multicommand = default_spi_send_multicommand,
71 .read = wbsio_spi_read,
72 .write_256 = spi_chip_write_1,
73 .write_aai = spi_chip_write_1,
Michael Karcherb9dbe482011-05-11 17:07:07 +000074};
75
Uwe Hermann36dec8b2010-06-07 19:06:26 +000076int wbsio_check_for_spi(void)
Uwe Hermann7b2969b2009-04-15 10:52:49 +000077{
Peter Stugebf196e92009-01-26 03:08:45 +000078 if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT1)))
79 if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT2)))
80 return 1;
81
Sean Nelsonf7f7a552010-01-09 23:34:45 +000082 msg_pspew("\nwbsio_spibase = 0x%x\n", wbsio_spibase);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +000083
Carl-Daniel Hailfingerca812d42010-07-14 19:57:52 +000084 msg_pdbg("%s: Winbond saved on 4 register bits so max chip size is "
Stefan Taunerc0aaf952011-05-19 02:58:17 +000085 "1024 kB!\n", __func__);
Carl-Daniel Hailfingerca812d42010-07-14 19:57:52 +000086 max_rom_decode.spi = 1024 * 1024;
Nico Huber5e08e3e2021-05-11 17:38:14 +020087 register_spi_master(&spi_master_wbsio, NULL);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +000088
Peter Stugebf196e92009-01-26 03:08:45 +000089 return 0;
90}
91
92/* W83627DHG has 11 command modes:
93 * 1=1 command only
94 * 2=1 command+1 data write
95 * 3=1 command+2 data read
96 * 4=1 command+3 address
97 * 5=1 command+3 address+1 data write
98 * 6=1 command+3 address+4 data write
99 * 7=1 command+3 address+1 dummy address inserted by wbsio+4 data read
100 * 8=1 command+3 address+1 data read
101 * 9=1 command+3 address+2 data read
102 * a=1 command+3 address+3 data read
103 * b=1 command+3 address+4 data read
104 *
105 * mode[7:4] holds the command mode
106 * mode[3:0] holds SPI address bits [19:16]
107 *
108 * The Winbond SPI master only supports 20 bit addresses on the SPI bus. :\
109 * Would one more byte of RAM in the chip (to get all 24 bits) really make
110 * such a big difference?
111 */
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000112static int wbsio_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000113 unsigned int readcnt,
114 const unsigned char *writearr,
115 unsigned char *readarr)
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000116{
Nico Huber519be662018-12-23 20:03:35 +0100117 unsigned int i;
Peter Stugebf196e92009-01-26 03:08:45 +0000118 uint8_t mode = 0;
119
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000120 msg_pspew("%s:", __func__);
Peter Stugebf196e92009-01-26 03:08:45 +0000121
122 if (1 == writecnt && 0 == readcnt) {
123 mode = 0x10;
124 } else if (2 == writecnt && 0 == readcnt) {
125 OUTB(writearr[1], wbsio_spibase + 4);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000126 msg_pspew(" data=0x%02x", writearr[1]);
Peter Stugebf196e92009-01-26 03:08:45 +0000127 mode = 0x20;
128 } else if (1 == writecnt && 2 == readcnt) {
129 mode = 0x30;
130 } else if (4 == writecnt && 0 == readcnt) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000131 msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000132 for (i = 2; i < writecnt; i++) {
133 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000134 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000135 }
136 mode = 0x40 | (writearr[1] & 0x0f);
137 } else if (5 == writecnt && 0 == readcnt) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000138 msg_pspew(" 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);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000141 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000142 }
143 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000144 msg_pspew(" data=0x%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000145 mode = 0x50 | (writearr[1] & 0x0f);
146 } else if (8 == writecnt && 0 == readcnt) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000147 msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000148 for (i = 2; i < 4; i++) {
149 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000150 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000151 }
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000152 msg_pspew(" data=0x");
Peter Stugebf196e92009-01-26 03:08:45 +0000153 for (; i < writecnt; i++) {
154 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000155 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000156 }
157 mode = 0x60 | (writearr[1] & 0x0f);
158 } else if (5 == writecnt && 4 == readcnt) {
159 /* XXX: TODO not supported by flashrom infrastructure!
160 * This mode, 7, discards the fifth byte in writecnt,
161 * but since we can not express that in flashrom, fail
162 * the operation for now.
163 */
164 ;
165 } else if (4 == writecnt && readcnt >= 1 && readcnt <= 4) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000166 msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000167 for (i = 2; i < writecnt; i++) {
168 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000169 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000170 }
171 mode = ((7 + readcnt) << 4) | (writearr[1] & 0x0f);
172 }
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000173 msg_pspew(" cmd=%02x mode=%02x\n", writearr[0], mode);
Peter Stugebf196e92009-01-26 03:08:45 +0000174
175 if (!mode) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000176 msg_perr("%s: unsupported command type wr=%d rd=%d\n",
Peter Stugebf196e92009-01-26 03:08:45 +0000177 __func__, writecnt, readcnt);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000178 /* Command type refers to the number of bytes read/written. */
179 return SPI_INVALID_LENGTH;
Peter Stugebf196e92009-01-26 03:08:45 +0000180 }
181
182 OUTB(writearr[0], wbsio_spibase);
183 OUTB(mode, wbsio_spibase + 1);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000184 programmer_delay(10);
Peter Stugebf196e92009-01-26 03:08:45 +0000185
186 if (!readcnt)
187 return 0;
188
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000189 msg_pspew("%s: returning data =", __func__);
Peter Stugebf196e92009-01-26 03:08:45 +0000190 for (i = 0; i < readcnt; i++) {
191 readarr[i] = INB(wbsio_spibase + 4 + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000192 msg_pspew(" 0x%02x", readarr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000193 }
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000194 msg_pspew("\n");
Peter Stugebf196e92009-01-26 03:08:45 +0000195 return 0;
196}
197
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000198static int wbsio_spi_read(struct flashctx *flash, uint8_t *buf,
199 unsigned int start, unsigned int len)
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000200{
Carl-Daniel Hailfingerccd71c22012-03-01 22:38:27 +0000201 mmio_readn((void *)(flash->virtual_memory + start), buf, len);
202 return 0;
Peter Stugebf196e92009-01-26 03:08:45 +0000203}