blob: a37116b8fb87f6f742f5ceb27f20348018e25468 [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"
Thomas Heijligen3f4d35d2022-01-17 15:11:43 +010020#include "hwaccess_physmap.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
Nico Huber56b53dd2023-01-11 23:15:20 +010029const size_t wbsio_max_mmapped = 1*MiB; /* maximum of memory mapped flash these SIOs can handle */
30static unsigned char *wbsio_mmapped_flash;
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
Nico Huber610c1aa2023-02-15 02:56:05 +010062static int wbsio_spi_send_command(const struct spi_master *,
63 unsigned int writecnt, unsigned int readcnt,
64 const unsigned char *writearr, unsigned char *readarr);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000065static int wbsio_spi_read(struct flashctx *flash, uint8_t *buf,
66 unsigned int start, unsigned int len);
Michael Karcherb9dbe482011-05-11 17:07:07 +000067
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000068static const struct spi_master spi_master_wbsio = {
Thomas Heijligen43040f22022-06-23 14:38:35 +020069 .max_data_read = MAX_DATA_UNSPECIFIED,
70 .max_data_write = MAX_DATA_UNSPECIFIED,
71 .command = wbsio_spi_send_command,
72 .multicommand = default_spi_send_multicommand,
73 .read = wbsio_spi_read,
74 .write_256 = spi_chip_write_1,
75 .write_aai = spi_chip_write_1,
Aarya Chaumal0cea7532022-07-04 18:21:50 +053076 .probe_opcode = default_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +000077};
78
Nico Huber7c717c32023-01-12 00:28:15 +010079int wbsio_check_for_spi(struct flashprog_programmer *const prog)
Uwe Hermann7b2969b2009-04-15 10:52:49 +000080{
Peter Stugebf196e92009-01-26 03:08:45 +000081 if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT1)))
82 if (0 == (wbsio_spibase = wbsio_get_spibase(WBSIO_PORT2)))
83 return 1;
84
Sean Nelsonf7f7a552010-01-09 23:34:45 +000085 msg_pspew("\nwbsio_spibase = 0x%x\n", wbsio_spibase);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +000086
Nico Huber56b53dd2023-01-11 23:15:20 +010087 wbsio_mmapped_flash = rphysmap("wbsio memory mapped SPI",
88 0xffffffff - wbsio_max_mmapped + 1, wbsio_max_mmapped);
89 if (wbsio_mmapped_flash == ERROR_PTR)
90 return 1;
91
Carl-Daniel Hailfingerca812d42010-07-14 19:57:52 +000092 msg_pdbg("%s: Winbond saved on 4 register bits so max chip size is "
Stefan Taunerc0aaf952011-05-19 02:58:17 +000093 "1024 kB!\n", __func__);
Nico Huber89569d62023-01-12 23:31:40 +010094 register_spi_master(&spi_master_wbsio, wbsio_max_mmapped, NULL);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +000095
Peter Stugebf196e92009-01-26 03:08:45 +000096 return 0;
97}
98
99/* W83627DHG has 11 command modes:
100 * 1=1 command only
101 * 2=1 command+1 data write
102 * 3=1 command+2 data read
103 * 4=1 command+3 address
104 * 5=1 command+3 address+1 data write
105 * 6=1 command+3 address+4 data write
106 * 7=1 command+3 address+1 dummy address inserted by wbsio+4 data read
107 * 8=1 command+3 address+1 data read
108 * 9=1 command+3 address+2 data read
109 * a=1 command+3 address+3 data read
110 * b=1 command+3 address+4 data read
111 *
112 * mode[7:4] holds the command mode
113 * mode[3:0] holds SPI address bits [19:16]
114 *
115 * The Winbond SPI master only supports 20 bit addresses on the SPI bus. :\
116 * Would one more byte of RAM in the chip (to get all 24 bits) really make
117 * such a big difference?
118 */
Nico Huber610c1aa2023-02-15 02:56:05 +0100119static int wbsio_spi_send_command(const struct spi_master *mst,
120 unsigned int writecnt, unsigned int readcnt,
121 const unsigned char *writearr, unsigned char *readarr)
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000122{
Nico Huber519be662018-12-23 20:03:35 +0100123 unsigned int i;
Peter Stugebf196e92009-01-26 03:08:45 +0000124 uint8_t mode = 0;
125
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000126 msg_pspew("%s:", __func__);
Peter Stugebf196e92009-01-26 03:08:45 +0000127
128 if (1 == writecnt && 0 == readcnt) {
129 mode = 0x10;
130 } else if (2 == writecnt && 0 == readcnt) {
131 OUTB(writearr[1], wbsio_spibase + 4);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000132 msg_pspew(" data=0x%02x", writearr[1]);
Peter Stugebf196e92009-01-26 03:08:45 +0000133 mode = 0x20;
134 } else if (1 == writecnt && 2 == readcnt) {
135 mode = 0x30;
136 } else if (4 == writecnt && 0 == readcnt) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000137 msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000138 for (i = 2; 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 = 0x40 | (writearr[1] & 0x0f);
143 } else if (5 == writecnt && 0 == readcnt) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000144 msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000145 for (i = 2; i < 4; i++) {
146 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000147 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000148 }
149 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000150 msg_pspew(" data=0x%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000151 mode = 0x50 | (writearr[1] & 0x0f);
152 } else if (8 == writecnt && 0 == readcnt) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000153 msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000154 for (i = 2; i < 4; i++) {
155 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000156 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000157 }
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000158 msg_pspew(" data=0x");
Peter Stugebf196e92009-01-26 03:08:45 +0000159 for (; i < writecnt; i++) {
160 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000161 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000162 }
163 mode = 0x60 | (writearr[1] & 0x0f);
164 } else if (5 == writecnt && 4 == readcnt) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200165 /* XXX: TODO not supported by flashprog infrastructure!
Peter Stugebf196e92009-01-26 03:08:45 +0000166 * This mode, 7, discards the fifth byte in writecnt,
Nico Huberc3b02dc2023-08-12 01:13:45 +0200167 * but since we can not express that in flashprog, fail
Peter Stugebf196e92009-01-26 03:08:45 +0000168 * the operation for now.
169 */
170 ;
171 } else if (4 == writecnt && readcnt >= 1 && readcnt <= 4) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000172 msg_pspew(" addr=0x%02x", (writearr[1] & 0x0f));
Peter Stugebf196e92009-01-26 03:08:45 +0000173 for (i = 2; i < writecnt; i++) {
174 OUTB(writearr[i], wbsio_spibase + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000175 msg_pspew("%02x", writearr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000176 }
177 mode = ((7 + readcnt) << 4) | (writearr[1] & 0x0f);
178 }
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000179 msg_pspew(" cmd=%02x mode=%02x\n", writearr[0], mode);
Peter Stugebf196e92009-01-26 03:08:45 +0000180
181 if (!mode) {
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000182 msg_perr("%s: unsupported command type wr=%d rd=%d\n",
Peter Stugebf196e92009-01-26 03:08:45 +0000183 __func__, writecnt, readcnt);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000184 /* Command type refers to the number of bytes read/written. */
185 return SPI_INVALID_LENGTH;
Peter Stugebf196e92009-01-26 03:08:45 +0000186 }
187
188 OUTB(writearr[0], wbsio_spibase);
189 OUTB(mode, wbsio_spibase + 1);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000190 programmer_delay(10);
Peter Stugebf196e92009-01-26 03:08:45 +0000191
192 if (!readcnt)
193 return 0;
194
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000195 msg_pspew("%s: returning data =", __func__);
Peter Stugebf196e92009-01-26 03:08:45 +0000196 for (i = 0; i < readcnt; i++) {
197 readarr[i] = INB(wbsio_spibase + 4 + i);
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000198 msg_pspew(" 0x%02x", readarr[i]);
Peter Stugebf196e92009-01-26 03:08:45 +0000199 }
Sean Nelsonf7f7a552010-01-09 23:34:45 +0000200 msg_pspew("\n");
Peter Stugebf196e92009-01-26 03:08:45 +0000201 return 0;
202}
203
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000204static int wbsio_spi_read(struct flashctx *flash, uint8_t *buf,
205 unsigned int start, unsigned int len)
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000206{
Nico Huber56b53dd2023-01-11 23:15:20 +0100207 unsigned char *const bios = wbsio_mmapped_flash +
208 wbsio_max_mmapped - flashprog_flash_getsize(flash);
209 mmio_readn(bios + start, buf, len);
Carl-Daniel Hailfingerccd71c22012-03-01 22:38:27 +0000210 return 0;
Peter Stugebf196e92009-01-26 03:08:45 +0000211}