blob: b0d400717a3158b6b143723d0d248d5448ced857 [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>
6 * Copyright (C) Adavanced 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
23#include <stdio.h>
24#include <string.h>
25#include <stdint.h>
26#include <sys/mman.h>
27#include <pci/pci.h>
28#include "flash.h"
29#include "spi.h"
30
31typedef struct _spi_controller {
32 unsigned int spi_cntrl0; /* 00h */
33 unsigned int restrictedcmd1; /* 04h */
34 unsigned int restrictedcmd2; /* 08h */
35 unsigned int spi_cntrl1; /* 0ch */
36 unsigned int spi_cmdvalue0; /* 10h */
37 unsigned int spi_cmdvalue1; /* 14h */
38 unsigned int spi_cmdvalue2; /* 18h */
39 unsigned int spi_fakeid; /* 1Ch */
40} sb600_spi_controller;
41
42sb600_spi_controller *spi_bar = NULL;
43uint8_t volatile *sb600_spibar;
44
45int sb600_spi_read(struct flashchip *flash, uint8_t *buf)
46{
47 int rc = 0, i;
48 int total_size = flash->total_size * 1024;
49 int page_size = 8;
50
51 for (i = 0; i < total_size / page_size; i++)
52 spi_nbyte_read(i * page_size, (void *)(buf + i * page_size),
53 page_size);
54 return rc;
55}
56
57uint8_t sb600_read_status_register(void)
58{
59 const unsigned char cmd[0x02] = { JEDEC_RDSR, 0x00 };
60 unsigned char readarr[JEDEC_RDSR_INSIZE];
61
62 /* Read Status Register */
63 spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
64 return readarr[0];
65}
66
67int sb600_spi_write(struct flashchip *flash, uint8_t *buf)
68{
69 int rc = 0, i;
70 int total_size = flash->total_size * 1024;
71
72 /* Erase first */
73 printf("Erasing flash before programming... ");
74 flash->erase(flash);
75 printf("done.\n");
76
77 printf("Programming flash");
78 for (i = 0; i < total_size; i++, buf++) {
79 spi_disable_blockprotect();
80 spi_write_enable();
81 spi_byte_program(i, *buf);
82 /* wait program complete. */
83 if (i % 0x8000 == 0)
84 printf(".");
85 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
86 ;
87 }
88 printf(" done.\n");
89 return rc;
90}
91
92void reset_internal_fifo_pointer(void)
93{
94 sb600_spibar[2] |= 0x10;
95
96 while (sb600_spibar[0xD] & 0x7)
97 printf("reset\n");
98}
99
100void execute_command(void)
101{
102 sb600_spibar[2] |= 1;
103
104 while (sb600_spibar[2] & 1)
105 ;
106}
107
108int sb600_spi_command(unsigned int writecnt, unsigned int readcnt,
109 const unsigned char *writearr, unsigned char *readarr)
110{
111 int count;
112 /* First byte is cmd which can not being sent through FIFO. */
113 unsigned char cmd = *writearr++;
114
115 writecnt--;
116
117 spi_bar = (sb600_spi_controller *) sb600_spibar;
118
119 printf_debug("%s, cmd=%x, writecnt=%x, readcnt=%x\n",
120 __func__, cmd, writecnt, readcnt);
121
122 if (readcnt > 8) {
123 printf("%s, SB600 SPI controller can not receive %d bytes, "
124 "which is limited with 8 bytes\n", __func__, readcnt);
125 return 1;
126 }
127
128 if (writecnt > 8) {
129 printf("%s, SB600 SPI controller can not sent %d bytes, "
130 "which is limited with 8 bytes\n", __func__, writecnt);
131 return 1;
132 }
133
134 sb600_spibar[0] = cmd;
135 sb600_spibar[1] = readcnt << 4 | (writecnt);
136
137 /* Before we use the FIFO, reset it first. */
138 reset_internal_fifo_pointer();
139
140 /* Send the write byte to FIFO. */
141 for (count = 0; count < writecnt; count++, writearr++) {
142 printf_debug(" [%x]", *writearr);
143 sb600_spibar[0xC] = *writearr;
144 }
145 printf_debug("\n");
146
147 /*
148 * We should send the data by sequence, which means we need to reset
149 * the FIFO pointer to the first byte we want to send.
150 */
151 reset_internal_fifo_pointer();
152
153 execute_command();
154
155 /*
156 * After the command executed, we should find out the index of the
157 * received byte. Here we just reset the FIFO pointer, skip the
158 * writecnt, is there anyone who have anther method to replace it?
159 */
160 reset_internal_fifo_pointer();
161
162 for (count = 0; count < writecnt; count++) {
163 cmd = sb600_spibar[0xC]; /* Skip the byte we send. */
164 printf_debug("[ %2x]", cmd);
165 }
166
167 printf_debug("The FIFO pointer 6 is %d.\n", sb600_spibar[0xd] & 0x07);
168 for (count = 0; count < readcnt; count++, readarr++) {
169 *readarr = sb600_spibar[0xC];
170 printf_debug("[%02x]", *readarr);
171 }
172 printf_debug("\n");
173
174 return 0;
175}