blob: 973d1ad64393868010ed0cd45e02b73d41a3dce6 [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.
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00007 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
Jason Wanga3f04be2008-11-28 21:36:51 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000024#if defined(__i386__) || defined(__x86_64__)
25
Jason Wanga3f04be2008-11-28 21:36:51 +000026#include <string.h>
Jason Wanga3f04be2008-11-28 21:36:51 +000027#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000028#include "chipdrivers.h"
Jason Wanga3f04be2008-11-28 21:36:51 +000029#include "spi.h"
30
Carl-Daniel Hailfinger2c7ba8c2009-06-23 00:47:26 +000031/* This struct is unused, but helps visualize the SB600 SPI BAR layout.
32 *struct sb600_spi_controller {
33 * unsigned int spi_cntrl0; / * 00h * /
34 * unsigned int restrictedcmd1; / * 04h * /
35 * unsigned int restrictedcmd2; / * 08h * /
36 * unsigned int spi_cntrl1; / * 0ch * /
37 * unsigned int spi_cmdvalue0; / * 10h * /
38 * unsigned int spi_cmdvalue1; / * 14h * /
39 * unsigned int spi_cmdvalue2; / * 18h * /
40 * unsigned int spi_fakeid; / * 1Ch * /
41 *};
42 */
Jason Wanga3f04be2008-11-28 21:36:51 +000043
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +000044uint8_t *sb600_spibar = NULL;
Jason Wanga3f04be2008-11-28 21:36:51 +000045
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +000046int sb600_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Jason Wanga3f04be2008-11-28 21:36:51 +000047{
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +000048 /* Maximum read length is 8 bytes. */
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +000049 return spi_read_chunked(flash, buf, start, len, 8);
Jason Wanga3f04be2008-11-28 21:36:51 +000050}
51
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +000052/* FIXME: SB600 can write 5 bytes per transaction. */
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +000053int sb600_spi_write_1(struct flashchip *flash, uint8_t *buf)
Jason Wanga3f04be2008-11-28 21:36:51 +000054{
Jason Wanga3f04be2008-11-28 21:36:51 +000055 int total_size = flash->total_size * 1024;
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +000056 int result = 0;
Jason Wanga3f04be2008-11-28 21:36:51 +000057
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +000058 spi_disable_blockprotect();
Jason Wanga3f04be2008-11-28 21:36:51 +000059 /* Erase first */
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +000060 msg_pinfo("Erasing flash before programming... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +000061 if (erase_flash(flash)) {
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +000062 msg_perr("ERASE FAILED!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000063 return -1;
64 }
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +000065 msg_pinfo("done.\n");
Jason Wanga3f04be2008-11-28 21:36:51 +000066
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +000067 msg_pinfo("Programming flash");
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +000068 result = spi_write_chunked(flash, buf, 0, total_size, 5);
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +000069 msg_pinfo(" done.\n");
Carl-Daniel Hailfingerde75a5e2009-10-01 13:16:32 +000070 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +000071}
72
Carl-Daniel Hailfinger2c7ba8c2009-06-23 00:47:26 +000073static void reset_internal_fifo_pointer(void)
Jason Wanga3f04be2008-11-28 21:36:51 +000074{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000075 mmio_writeb(mmio_readb(sb600_spibar + 2) | 0x10, sb600_spibar + 2);
Jason Wanga3f04be2008-11-28 21:36:51 +000076
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000077 while (mmio_readb(sb600_spibar + 0xD) & 0x7)
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +000078 msg_pspew("reset\n");
Jason Wanga3f04be2008-11-28 21:36:51 +000079}
80
Carl-Daniel Hailfinger2c7ba8c2009-06-23 00:47:26 +000081static void execute_command(void)
Jason Wanga3f04be2008-11-28 21:36:51 +000082{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000083 mmio_writeb(mmio_readb(sb600_spibar + 2) | 1, sb600_spibar + 2);
Jason Wanga3f04be2008-11-28 21:36:51 +000084
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000085 while (mmio_readb(sb600_spibar + 2) & 1)
Jason Wanga3f04be2008-11-28 21:36:51 +000086 ;
87}
88
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000089int sb600_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Jason Wanga3f04be2008-11-28 21:36:51 +000090 const unsigned char *writearr, unsigned char *readarr)
91{
92 int count;
93 /* First byte is cmd which can not being sent through FIFO. */
94 unsigned char cmd = *writearr++;
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +000095 unsigned int readoffby1;
Jason Wanga3f04be2008-11-28 21:36:51 +000096
97 writecnt--;
98
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +000099 msg_pspew("%s, cmd=%x, writecnt=%x, readcnt=%x\n",
100 __func__, cmd, writecnt, readcnt);
Jason Wanga3f04be2008-11-28 21:36:51 +0000101
102 if (readcnt > 8) {
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +0000103 msg_pinfo("%s, SB600 SPI controller can not receive %d bytes, "
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000104 "it is limited to 8 bytes\n", __func__, readcnt);
105 return SPI_INVALID_LENGTH;
Jason Wanga3f04be2008-11-28 21:36:51 +0000106 }
107
108 if (writecnt > 8) {
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +0000109 msg_pinfo("%s, SB600 SPI controller can not send %d bytes, "
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000110 "it is limited to 8 bytes\n", __func__, writecnt);
111 return SPI_INVALID_LENGTH;
Jason Wanga3f04be2008-11-28 21:36:51 +0000112 }
113
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000114 /* This is a workaround for a bug in SB600 and SB700. If we only send
115 * an opcode and no additional data/address, the SPI controller will
116 * read one byte too few from the chip. Basically, the last byte of
117 * the chip response is discarded and will not end up in the FIFO.
118 * It is unclear if the CS# line is set high too early as well.
119 */
120 readoffby1 = (writecnt) ? 0 : 1;
121 mmio_writeb((readcnt + readoffby1) << 4 | (writecnt), sb600_spibar + 1);
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000122 mmio_writeb(cmd, sb600_spibar + 0);
Jason Wanga3f04be2008-11-28 21:36:51 +0000123
124 /* Before we use the FIFO, reset it first. */
125 reset_internal_fifo_pointer();
126
127 /* Send the write byte to FIFO. */
128 for (count = 0; count < writecnt; count++, writearr++) {
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +0000129 msg_pspew(" [%x]", *writearr);
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000130 mmio_writeb(*writearr, sb600_spibar + 0xC);
Jason Wanga3f04be2008-11-28 21:36:51 +0000131 }
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +0000132 msg_pspew("\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000133
134 /*
135 * We should send the data by sequence, which means we need to reset
136 * the FIFO pointer to the first byte we want to send.
137 */
138 reset_internal_fifo_pointer();
139
140 execute_command();
141
142 /*
143 * After the command executed, we should find out the index of the
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000144 * received byte. Here we just reset the FIFO pointer and skip the
145 * writecnt.
146 * It would be possible to increase the FIFO pointer by one instead
147 * of reading and discarding one byte from the FIFO.
148 * The FIFO is implemented on top of an 8 byte ring buffer and the
149 * buffer is never cleared. For every byte that is shifted out after
150 * the opcode, the FIFO already stores the response from the chip.
151 * Usually, the chip will respond with 0x00 or 0xff.
Jason Wanga3f04be2008-11-28 21:36:51 +0000152 */
153 reset_internal_fifo_pointer();
154
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000155 /* Skip the bytes we sent. */
Jason Wanga3f04be2008-11-28 21:36:51 +0000156 for (count = 0; count < writecnt; count++) {
Carl-Daniel Hailfingerf8555e22009-07-23 01:36:08 +0000157 cmd = mmio_readb(sb600_spibar + 0xC);
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +0000158 msg_pspew("[ %2x]", cmd);
Jason Wanga3f04be2008-11-28 21:36:51 +0000159 }
160
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +0000161 msg_pspew("The FIFO pointer after skipping is %d.\n",
162 mmio_readb(sb600_spibar + 0xd) & 0x07);
Jason Wanga3f04be2008-11-28 21:36:51 +0000163 for (count = 0; count < readcnt; count++, readarr++) {
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000164 *readarr = mmio_readb(sb600_spibar + 0xC);
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +0000165 msg_pspew("[%02x]", *readarr);
Jason Wanga3f04be2008-11-28 21:36:51 +0000166 }
Carl-Daniel Hailfinger643415b2010-01-10 01:59:50 +0000167 msg_pspew("\n");
Jason Wanga3f04be2008-11-28 21:36:51 +0000168
169 return 0;
170}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000171
172#endif