blob: 4ca905becb3c526f9cb451494d9a4972e38fe9e5 [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 <sys/mman.h>
Jason Wanga3f04be2008-11-28 21:36:51 +000025#include "flash.h"
26#include "spi.h"
27
Carl-Daniel Hailfinger2c7ba8c2009-06-23 00:47:26 +000028/* This struct is unused, but helps visualize the SB600 SPI BAR layout.
29 *struct sb600_spi_controller {
30 * unsigned int spi_cntrl0; / * 00h * /
31 * unsigned int restrictedcmd1; / * 04h * /
32 * unsigned int restrictedcmd2; / * 08h * /
33 * unsigned int spi_cntrl1; / * 0ch * /
34 * unsigned int spi_cmdvalue0; / * 10h * /
35 * unsigned int spi_cmdvalue1; / * 14h * /
36 * unsigned int spi_cmdvalue2; / * 18h * /
37 * unsigned int spi_fakeid; / * 1Ch * /
38 *};
39 */
Jason Wanga3f04be2008-11-28 21:36:51 +000040
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000041uint8_t *sb600_spibar;
Jason Wanga3f04be2008-11-28 21:36:51 +000042
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +000043int sb600_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Jason Wanga3f04be2008-11-28 21:36:51 +000044{
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +000045 /* Maximum read length is 8 bytes. */
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +000046 return spi_read_chunked(flash, buf, start, len, 8);
Jason Wanga3f04be2008-11-28 21:36:51 +000047}
48
49uint8_t sb600_read_status_register(void)
50{
51 const unsigned char cmd[0x02] = { JEDEC_RDSR, 0x00 };
52 unsigned char readarr[JEDEC_RDSR_INSIZE];
53
54 /* Read Status Register */
55 spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
56 return readarr[0];
57}
58
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +000059int sb600_spi_write_1(struct flashchip *flash, uint8_t *buf)
Jason Wanga3f04be2008-11-28 21:36:51 +000060{
61 int rc = 0, i;
62 int total_size = flash->total_size * 1024;
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +000063 int result;
Jason Wanga3f04be2008-11-28 21:36:51 +000064
65 /* Erase first */
66 printf("Erasing flash before programming... ");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000067 if (flash->erase(flash)) {
68 fprintf(stderr, "ERASE FAILED!\n");
69 return -1;
70 }
Jason Wanga3f04be2008-11-28 21:36:51 +000071 printf("done.\n");
72
73 printf("Programming flash");
74 for (i = 0; i < total_size; i++, buf++) {
75 spi_disable_blockprotect();
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +000076 result = spi_write_enable();
77 if (result)
78 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +000079 spi_byte_program(i, *buf);
80 /* wait program complete. */
81 if (i % 0x8000 == 0)
82 printf(".");
83 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
84 ;
85 }
86 printf(" done.\n");
87 return rc;
88}
89
Carl-Daniel Hailfinger2c7ba8c2009-06-23 00:47:26 +000090static void reset_internal_fifo_pointer(void)
Jason Wanga3f04be2008-11-28 21:36:51 +000091{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000092 mmio_writeb(mmio_readb(sb600_spibar + 2) | 0x10, sb600_spibar + 2);
Jason Wanga3f04be2008-11-28 21:36:51 +000093
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000094 while (mmio_readb(sb600_spibar + 0xD) & 0x7)
Jason Wanga3f04be2008-11-28 21:36:51 +000095 printf("reset\n");
96}
97
Carl-Daniel Hailfinger2c7ba8c2009-06-23 00:47:26 +000098static void execute_command(void)
Jason Wanga3f04be2008-11-28 21:36:51 +000099{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000100 mmio_writeb(mmio_readb(sb600_spibar + 2) | 1, sb600_spibar + 2);
Jason Wanga3f04be2008-11-28 21:36:51 +0000101
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000102 while (mmio_readb(sb600_spibar + 2) & 1)
Jason Wanga3f04be2008-11-28 21:36:51 +0000103 ;
104}
105
106int sb600_spi_command(unsigned int writecnt, unsigned int readcnt,
107 const unsigned char *writearr, unsigned char *readarr)
108{
109 int count;
110 /* First byte is cmd which can not being sent through FIFO. */
111 unsigned char cmd = *writearr++;
112
113 writecnt--;
114
Jason Wanga3f04be2008-11-28 21:36:51 +0000115 printf_debug("%s, cmd=%x, writecnt=%x, readcnt=%x\n",
116 __func__, cmd, writecnt, readcnt);
117
118 if (readcnt > 8) {
119 printf("%s, SB600 SPI controller can not receive %d bytes, "
120 "which is limited with 8 bytes\n", __func__, readcnt);
121 return 1;
122 }
123
124 if (writecnt > 8) {
125 printf("%s, SB600 SPI controller can not sent %d bytes, "
126 "which is limited with 8 bytes\n", __func__, writecnt);
127 return 1;
128 }
129
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000130 mmio_writeb(cmd, sb600_spibar + 0);
131 mmio_writeb(readcnt << 4 | (writecnt), sb600_spibar + 1);
Jason Wanga3f04be2008-11-28 21:36:51 +0000132
133 /* Before we use the FIFO, reset it first. */
134 reset_internal_fifo_pointer();
135
136 /* Send the write byte to FIFO. */
137 for (count = 0; count < writecnt; count++, writearr++) {
138 printf_debug(" [%x]", *writearr);
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000139 mmio_writeb(*writearr, sb600_spibar + 0xC);
Jason Wanga3f04be2008-11-28 21:36:51 +0000140 }
141 printf_debug("\n");
142
143 /*
144 * We should send the data by sequence, which means we need to reset
145 * the FIFO pointer to the first byte we want to send.
146 */
147 reset_internal_fifo_pointer();
148
149 execute_command();
150
151 /*
152 * After the command executed, we should find out the index of the
153 * received byte. Here we just reset the FIFO pointer, skip the
154 * writecnt, is there anyone who have anther method to replace it?
155 */
156 reset_internal_fifo_pointer();
157
158 for (count = 0; count < writecnt; count++) {
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000159 cmd = mmio_readb(sb600_spibar + 0xC); /* Skip the byte we send. */
Jason Wanga3f04be2008-11-28 21:36:51 +0000160 printf_debug("[ %2x]", cmd);
161 }
162
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000163 printf_debug("The FIFO pointer 6 is %d.\n", mmio_readb(sb600_spibar + 0xd) & 0x07);
Jason Wanga3f04be2008-11-28 21:36:51 +0000164 for (count = 0; count < readcnt; count++, readarr++) {
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000165 *readarr = mmio_readb(sb600_spibar + 0xC);
Jason Wanga3f04be2008-11-28 21:36:51 +0000166 printf_debug("[%02x]", *readarr);
167 }
168 printf_debug("\n");
169
170 return 0;
171}