blob: 66943ba87943f226261d69db3d57cad0ad71af38 [file] [log] [blame]
Jason Wanga3f04be2008-11-28 21:36:51 +00001/*
2 * This file is part of the flashrom project.
3 *
Jason Wang13f98ce2008-11-29 15:07:15 +00004 * Copyright (C) 2008 Wang Qingpei <Qingpei.Wang@amd.com>
5 * Copyright (C) 2008 Joe Bao <Zheng.Bao@amd.com>
Uwe Hermann97e8f222009-04-13 21:35:49 +00006 * Copyright (C) 2008 Advanced Micro Devices, Inc.
Jason Wanga3f04be2008-11-28 21:36:51 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
Jason Wanga3f04be2008-11-28 21:36:51 +000023#include <string.h>
Jason Wanga3f04be2008-11-28 21:36:51 +000024#include "flash.h"
25#include "spi.h"
26
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +000027/* Change this to #define if you want lowlevel debugging of commands
28 * sent to the SB600/SB700 SPI controller.
29 */
30#undef COMM_DEBUG
31
32#ifdef COMM_DEBUG
33#define msg_comm_debug printf_debug
34#else
35#define msg_comm_debug(...) do {} while (0)
36#endif
37
Carl-Daniel Hailfinger2c7ba8c2009-06-23 00:47:26 +000038/* This struct is unused, but helps visualize the SB600 SPI BAR layout.
39 *struct sb600_spi_controller {
40 * unsigned int spi_cntrl0; / * 00h * /
41 * unsigned int restrictedcmd1; / * 04h * /
42 * unsigned int restrictedcmd2; / * 08h * /
43 * unsigned int spi_cntrl1; / * 0ch * /
44 * unsigned int spi_cmdvalue0; / * 10h * /
45 * unsigned int spi_cmdvalue1; / * 14h * /
46 * unsigned int spi_cmdvalue2; / * 18h * /
47 * unsigned int spi_fakeid; / * 1Ch * /
48 *};
49 */
Jason Wanga3f04be2008-11-28 21:36:51 +000050
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +000051uint8_t *sb600_spibar = NULL;
Jason Wanga3f04be2008-11-28 21:36:51 +000052
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +000053int sb600_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Jason Wanga3f04be2008-11-28 21:36:51 +000054{
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +000055 /* Maximum read length is 8 bytes. */
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +000056 return spi_read_chunked(flash, buf, start, len, 8);
Jason Wanga3f04be2008-11-28 21:36:51 +000057}
58
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +000059/* FIXME: SB600 can write 5 bytes per transaction. */
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +000060int sb600_spi_write_1(struct flashchip *flash, uint8_t *buf)
Jason Wanga3f04be2008-11-28 21:36:51 +000061{
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +000062 int i;
Jason Wanga3f04be2008-11-28 21:36:51 +000063 int total_size = flash->total_size * 1024;
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +000064 int result = 0;
Jason Wanga3f04be2008-11-28 21:36:51 +000065
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +000066 spi_disable_blockprotect();
Jason Wanga3f04be2008-11-28 21:36:51 +000067 /* Erase first */
68 printf("Erasing flash before programming... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +000069 if (erase_flash(flash)) {
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000070 fprintf(stderr, "ERASE FAILED!\n");
71 return -1;
72 }
Jason Wanga3f04be2008-11-28 21:36:51 +000073 printf("done.\n");
74
75 printf("Programming flash");
76 for (i = 0; i < total_size; i++, buf++) {
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +000077 result = spi_nbyte_program(i, buf, 1);
Stefan Reinauerab044b22009-09-16 08:26:59 +000078 if (result) {
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +000079 fprintf(stderr, "Write error!\n");
80 return result;
Stefan Reinauerab044b22009-09-16 08:26:59 +000081 }
82
Jason Wanga3f04be2008-11-28 21:36:51 +000083 /* wait program complete. */
84 if (i % 0x8000 == 0)
85 printf(".");
86 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
87 ;
88 }
89 printf(" done.\n");
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +000090 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +000091}
92
Carl-Daniel Hailfinger2c7ba8c2009-06-23 00:47:26 +000093static void reset_internal_fifo_pointer(void)
Jason Wanga3f04be2008-11-28 21:36:51 +000094{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000095 mmio_writeb(mmio_readb(sb600_spibar + 2) | 0x10, sb600_spibar + 2);
Jason Wanga3f04be2008-11-28 21:36:51 +000096
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000097 while (mmio_readb(sb600_spibar + 0xD) & 0x7)
Jason Wanga3f04be2008-11-28 21:36:51 +000098 printf("reset\n");
99}
100
Carl-Daniel Hailfinger2c7ba8c2009-06-23 00:47:26 +0000101static void execute_command(void)
Jason Wanga3f04be2008-11-28 21:36:51 +0000102{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000103 mmio_writeb(mmio_readb(sb600_spibar + 2) | 1, sb600_spibar + 2);
Jason Wanga3f04be2008-11-28 21:36:51 +0000104
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000105 while (mmio_readb(sb600_spibar + 2) & 1)
Jason Wanga3f04be2008-11-28 21:36:51 +0000106 ;
107}
108
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000109int sb600_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Jason Wanga3f04be2008-11-28 21:36:51 +0000110 const unsigned char *writearr, unsigned char *readarr)
111{
112 int count;
113 /* First byte is cmd which can not being sent through FIFO. */
114 unsigned char cmd = *writearr++;
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000115 unsigned int readoffby1;
Jason Wanga3f04be2008-11-28 21:36:51 +0000116
117 writecnt--;
118
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000119 msg_comm_debug("%s, cmd=%x, writecnt=%x, readcnt=%x\n",
Jason Wanga3f04be2008-11-28 21:36:51 +0000120 __func__, cmd, writecnt, readcnt);
121
122 if (readcnt > 8) {
123 printf("%s, SB600 SPI controller can not receive %d bytes, "
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000124 "it is limited to 8 bytes\n", __func__, readcnt);
125 return SPI_INVALID_LENGTH;
Jason Wanga3f04be2008-11-28 21:36:51 +0000126 }
127
128 if (writecnt > 8) {
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000129 printf("%s, SB600 SPI controller can not send %d bytes, "
130 "it is limited to 8 bytes\n", __func__, writecnt);
131 return SPI_INVALID_LENGTH;
Jason Wanga3f04be2008-11-28 21:36:51 +0000132 }
133
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000134 /* This is a workaround for a bug in SB600 and SB700. If we only send
135 * an opcode and no additional data/address, the SPI controller will
136 * read one byte too few from the chip. Basically, the last byte of
137 * the chip response is discarded and will not end up in the FIFO.
138 * It is unclear if the CS# line is set high too early as well.
139 */
140 readoffby1 = (writecnt) ? 0 : 1;
141 mmio_writeb((readcnt + readoffby1) << 4 | (writecnt), sb600_spibar + 1);
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000142 mmio_writeb(cmd, sb600_spibar + 0);
Jason Wanga3f04be2008-11-28 21:36:51 +0000143
144 /* Before we use the FIFO, reset it first. */
145 reset_internal_fifo_pointer();
146
147 /* Send the write byte to FIFO. */
148 for (count = 0; count < writecnt; count++, writearr++) {
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000149 msg_comm_debug(" [%x]", *writearr);
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000150 mmio_writeb(*writearr, sb600_spibar + 0xC);
Jason Wanga3f04be2008-11-28 21:36:51 +0000151 }
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000152 msg_comm_debug("\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000153
154 /*
155 * We should send the data by sequence, which means we need to reset
156 * the FIFO pointer to the first byte we want to send.
157 */
158 reset_internal_fifo_pointer();
159
160 execute_command();
161
162 /*
163 * After the command executed, we should find out the index of the
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000164 * received byte. Here we just reset the FIFO pointer and skip the
165 * writecnt.
166 * It would be possible to increase the FIFO pointer by one instead
167 * of reading and discarding one byte from the FIFO.
168 * The FIFO is implemented on top of an 8 byte ring buffer and the
169 * buffer is never cleared. For every byte that is shifted out after
170 * the opcode, the FIFO already stores the response from the chip.
171 * Usually, the chip will respond with 0x00 or 0xff.
Jason Wanga3f04be2008-11-28 21:36:51 +0000172 */
173 reset_internal_fifo_pointer();
174
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000175 /* Skip the bytes we sent. */
Jason Wanga3f04be2008-11-28 21:36:51 +0000176 for (count = 0; count < writecnt; count++) {
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000177 cmd = mmio_readb(sb600_spibar + 0xC);
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000178 msg_comm_debug("[ %2x]", cmd);
Jason Wanga3f04be2008-11-28 21:36:51 +0000179 }
180
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000181 msg_comm_debug("The FIFO pointer after skipping is %d.\n",
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000182 mmio_readb(sb600_spibar + 0xd) & 0x07);
Jason Wanga3f04be2008-11-28 21:36:51 +0000183 for (count = 0; count < readcnt; count++, readarr++) {
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000184 *readarr = mmio_readb(sb600_spibar + 0xC);
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000185 msg_comm_debug("[%02x]", *readarr);
Jason Wanga3f04be2008-11-28 21:36:51 +0000186 }
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000187 msg_comm_debug("\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000188
189 return 0;
190}