blob: f4f3492ea2a8ab1af2080ccd9008e4d3d991abe1 [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 Hailfinger1dfe0ff2009-05-31 17:57:34 +000028struct sb600_spi_controller {
Jason Wanga3f04be2008-11-28 21:36:51 +000029 unsigned int spi_cntrl0; /* 00h */
30 unsigned int restrictedcmd1; /* 04h */
31 unsigned int restrictedcmd2; /* 08h */
32 unsigned int spi_cntrl1; /* 0ch */
33 unsigned int spi_cmdvalue0; /* 10h */
34 unsigned int spi_cmdvalue1; /* 14h */
35 unsigned int spi_cmdvalue2; /* 18h */
36 unsigned int spi_fakeid; /* 1Ch */
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000037};
Jason Wanga3f04be2008-11-28 21:36:51 +000038
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000039struct sb600_spi_controller *spi_bar = NULL;
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000040uint8_t *sb600_spibar;
Jason Wanga3f04be2008-11-28 21:36:51 +000041
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +000042int sb600_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Jason Wanga3f04be2008-11-28 21:36:51 +000043{
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +000044 /* Maximum read length is 8 bytes. */
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +000045 return spi_read_chunked(flash, buf, start, len, 8);
Jason Wanga3f04be2008-11-28 21:36:51 +000046}
47
48uint8_t sb600_read_status_register(void)
49{
50 const unsigned char cmd[0x02] = { JEDEC_RDSR, 0x00 };
51 unsigned char readarr[JEDEC_RDSR_INSIZE];
52
53 /* Read Status Register */
54 spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
55 return readarr[0];
56}
57
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +000058int sb600_spi_write_1(struct flashchip *flash, uint8_t *buf)
Jason Wanga3f04be2008-11-28 21:36:51 +000059{
60 int rc = 0, i;
61 int total_size = flash->total_size * 1024;
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +000062 int result;
Jason Wanga3f04be2008-11-28 21:36:51 +000063
64 /* Erase first */
65 printf("Erasing flash before programming... ");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +000066 if (flash->erase(flash)) {
67 fprintf(stderr, "ERASE FAILED!\n");
68 return -1;
69 }
Jason Wanga3f04be2008-11-28 21:36:51 +000070 printf("done.\n");
71
72 printf("Programming flash");
73 for (i = 0; i < total_size; i++, buf++) {
74 spi_disable_blockprotect();
Carl-Daniel Hailfinger03adbe12009-05-09 02:09:45 +000075 result = spi_write_enable();
76 if (result)
77 return result;
Jason Wanga3f04be2008-11-28 21:36:51 +000078 spi_byte_program(i, *buf);
79 /* wait program complete. */
80 if (i % 0x8000 == 0)
81 printf(".");
82 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
83 ;
84 }
85 printf(" done.\n");
86 return rc;
87}
88
89void reset_internal_fifo_pointer(void)
90{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000091 mmio_writeb(mmio_readb(sb600_spibar + 2) | 0x10, sb600_spibar + 2);
Jason Wanga3f04be2008-11-28 21:36:51 +000092
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000093 while (mmio_readb(sb600_spibar + 0xD) & 0x7)
Jason Wanga3f04be2008-11-28 21:36:51 +000094 printf("reset\n");
95}
96
97void execute_command(void)
98{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +000099 mmio_writeb(mmio_readb(sb600_spibar + 2) | 1, sb600_spibar + 2);
Jason Wanga3f04be2008-11-28 21:36:51 +0000100
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000101 while (mmio_readb(sb600_spibar + 2) & 1)
Jason Wanga3f04be2008-11-28 21:36:51 +0000102 ;
103}
104
105int sb600_spi_command(unsigned int writecnt, unsigned int readcnt,
106 const unsigned char *writearr, unsigned char *readarr)
107{
108 int count;
109 /* First byte is cmd which can not being sent through FIFO. */
110 unsigned char cmd = *writearr++;
111
112 writecnt--;
113
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000114 spi_bar = (struct sb600_spi_controller *) sb600_spibar;
Jason Wanga3f04be2008-11-28 21:36:51 +0000115
116 printf_debug("%s, cmd=%x, writecnt=%x, readcnt=%x\n",
117 __func__, cmd, writecnt, readcnt);
118
119 if (readcnt > 8) {
120 printf("%s, SB600 SPI controller can not receive %d bytes, "
121 "which is limited with 8 bytes\n", __func__, readcnt);
122 return 1;
123 }
124
125 if (writecnt > 8) {
126 printf("%s, SB600 SPI controller can not sent %d bytes, "
127 "which is limited with 8 bytes\n", __func__, writecnt);
128 return 1;
129 }
130
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000131 mmio_writeb(cmd, sb600_spibar + 0);
132 mmio_writeb(readcnt << 4 | (writecnt), sb600_spibar + 1);
Jason Wanga3f04be2008-11-28 21:36:51 +0000133
134 /* Before we use the FIFO, reset it first. */
135 reset_internal_fifo_pointer();
136
137 /* Send the write byte to FIFO. */
138 for (count = 0; count < writecnt; count++, writearr++) {
139 printf_debug(" [%x]", *writearr);
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000140 mmio_writeb(*writearr, sb600_spibar + 0xC);
Jason Wanga3f04be2008-11-28 21:36:51 +0000141 }
142 printf_debug("\n");
143
144 /*
145 * We should send the data by sequence, which means we need to reset
146 * the FIFO pointer to the first byte we want to send.
147 */
148 reset_internal_fifo_pointer();
149
150 execute_command();
151
152 /*
153 * After the command executed, we should find out the index of the
154 * received byte. Here we just reset the FIFO pointer, skip the
155 * writecnt, is there anyone who have anther method to replace it?
156 */
157 reset_internal_fifo_pointer();
158
159 for (count = 0; count < writecnt; count++) {
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000160 cmd = mmio_readb(sb600_spibar + 0xC); /* Skip the byte we send. */
Jason Wanga3f04be2008-11-28 21:36:51 +0000161 printf_debug("[ %2x]", cmd);
162 }
163
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000164 printf_debug("The FIFO pointer 6 is %d.\n", mmio_readb(sb600_spibar + 0xd) & 0x07);
Jason Wanga3f04be2008-11-28 21:36:51 +0000165 for (count = 0; count < readcnt; count++, readarr++) {
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000166 *readarr = mmio_readb(sb600_spibar + 0xC);
Jason Wanga3f04be2008-11-28 21:36:51 +0000167 printf_debug("[%02x]", *readarr);
168 }
169 printf_debug("\n");
170
171 return 0;
172}