Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Jason Wang | 13f98ce | 2008-11-29 15:07:15 +0000 | [diff] [blame] | 4 | * Copyright (C) 2008 Wang Qingpei <Qingpei.Wang@amd.com> |
| 5 | * Copyright (C) 2008 Joe Bao <Zheng.Bao@amd.com> |
Uwe Hermann | 97e8f22 | 2009-04-13 21:35:49 +0000 | [diff] [blame] | 6 | * Copyright (C) 2008 Advanced Micro Devices, Inc. |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 7 | * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 8 | * |
| 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 | |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 24 | #include <string.h> |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 25 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 26 | #include "chipdrivers.h" |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 27 | #include "spi.h" |
| 28 | |
Carl-Daniel Hailfinger | 2c7ba8c | 2009-06-23 00:47:26 +0000 | [diff] [blame] | 29 | /* This struct is unused, but helps visualize the SB600 SPI BAR layout. |
| 30 | *struct sb600_spi_controller { |
| 31 | * unsigned int spi_cntrl0; / * 00h * / |
| 32 | * unsigned int restrictedcmd1; / * 04h * / |
| 33 | * unsigned int restrictedcmd2; / * 08h * / |
| 34 | * unsigned int spi_cntrl1; / * 0ch * / |
| 35 | * unsigned int spi_cmdvalue0; / * 10h * / |
| 36 | * unsigned int spi_cmdvalue1; / * 14h * / |
| 37 | * unsigned int spi_cmdvalue2; / * 18h * / |
| 38 | * unsigned int spi_fakeid; / * 1Ch * / |
| 39 | *}; |
| 40 | */ |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 41 | |
Carl-Daniel Hailfinger | f8555e2 | 2009-07-23 01:36:08 +0000 | [diff] [blame] | 42 | uint8_t *sb600_spibar = NULL; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 43 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 44 | int sb600_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 45 | { |
Carl-Daniel Hailfinger | 38a059d | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 46 | /* Maximum read length is 8 bytes. */ |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 47 | return spi_read_chunked(flash, buf, start, len, 8); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Carl-Daniel Hailfinger | 116081a | 2009-08-10 02:29:21 +0000 | [diff] [blame] | 50 | /* FIXME: SB600 can write 5 bytes per transaction. */ |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 51 | int sb600_spi_write_1(struct flashchip *flash, uint8_t *buf) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 52 | { |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 53 | int total_size = flash->total_size * 1024; |
Carl-Daniel Hailfinger | de75a5e | 2009-10-01 13:16:32 +0000 | [diff] [blame] | 54 | int result = 0; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 55 | |
Carl-Daniel Hailfinger | 116081a | 2009-08-10 02:29:21 +0000 | [diff] [blame] | 56 | spi_disable_blockprotect(); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 57 | /* Erase first */ |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 58 | msg_pinfo("Erasing flash before programming... "); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 59 | if (erase_flash(flash)) { |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 60 | msg_perr("ERASE FAILED!\n"); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 61 | return -1; |
| 62 | } |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 63 | msg_pinfo("done.\n"); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 64 | |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 65 | msg_pinfo("Programming flash"); |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 66 | result = spi_write_chunked(flash, buf, 0, total_size, 5); |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 67 | msg_pinfo(" done.\n"); |
Carl-Daniel Hailfinger | de75a5e | 2009-10-01 13:16:32 +0000 | [diff] [blame] | 68 | return result; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Carl-Daniel Hailfinger | 2c7ba8c | 2009-06-23 00:47:26 +0000 | [diff] [blame] | 71 | static void reset_internal_fifo_pointer(void) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 72 | { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 73 | mmio_writeb(mmio_readb(sb600_spibar + 2) | 0x10, sb600_spibar + 2); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 74 | |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 75 | while (mmio_readb(sb600_spibar + 0xD) & 0x7) |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 76 | msg_pspew("reset\n"); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Carl-Daniel Hailfinger | 2c7ba8c | 2009-06-23 00:47:26 +0000 | [diff] [blame] | 79 | static void execute_command(void) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 80 | { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 81 | mmio_writeb(mmio_readb(sb600_spibar + 2) | 1, sb600_spibar + 2); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 82 | |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 83 | while (mmio_readb(sb600_spibar + 2) & 1) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 84 | ; |
| 85 | } |
| 86 | |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 87 | int sb600_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 88 | const unsigned char *writearr, unsigned char *readarr) |
| 89 | { |
| 90 | int count; |
| 91 | /* First byte is cmd which can not being sent through FIFO. */ |
| 92 | unsigned char cmd = *writearr++; |
Carl-Daniel Hailfinger | f8555e2 | 2009-07-23 01:36:08 +0000 | [diff] [blame] | 93 | unsigned int readoffby1; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 94 | |
| 95 | writecnt--; |
| 96 | |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 97 | msg_pspew("%s, cmd=%x, writecnt=%x, readcnt=%x\n", |
| 98 | __func__, cmd, writecnt, readcnt); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 99 | |
| 100 | if (readcnt > 8) { |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 101 | msg_pinfo("%s, SB600 SPI controller can not receive %d bytes, " |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 102 | "it is limited to 8 bytes\n", __func__, readcnt); |
| 103 | return SPI_INVALID_LENGTH; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (writecnt > 8) { |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 107 | msg_pinfo("%s, SB600 SPI controller can not send %d bytes, " |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 108 | "it is limited to 8 bytes\n", __func__, writecnt); |
| 109 | return SPI_INVALID_LENGTH; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Carl-Daniel Hailfinger | f8555e2 | 2009-07-23 01:36:08 +0000 | [diff] [blame] | 112 | /* This is a workaround for a bug in SB600 and SB700. If we only send |
| 113 | * an opcode and no additional data/address, the SPI controller will |
| 114 | * read one byte too few from the chip. Basically, the last byte of |
| 115 | * the chip response is discarded and will not end up in the FIFO. |
| 116 | * It is unclear if the CS# line is set high too early as well. |
| 117 | */ |
| 118 | readoffby1 = (writecnt) ? 0 : 1; |
| 119 | mmio_writeb((readcnt + readoffby1) << 4 | (writecnt), sb600_spibar + 1); |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 120 | mmio_writeb(cmd, sb600_spibar + 0); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 121 | |
| 122 | /* Before we use the FIFO, reset it first. */ |
| 123 | reset_internal_fifo_pointer(); |
| 124 | |
| 125 | /* Send the write byte to FIFO. */ |
| 126 | for (count = 0; count < writecnt; count++, writearr++) { |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 127 | msg_pspew(" [%x]", *writearr); |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 128 | mmio_writeb(*writearr, sb600_spibar + 0xC); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 129 | } |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 130 | msg_pspew("\n"); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * We should send the data by sequence, which means we need to reset |
| 134 | * the FIFO pointer to the first byte we want to send. |
| 135 | */ |
| 136 | reset_internal_fifo_pointer(); |
| 137 | |
| 138 | execute_command(); |
| 139 | |
| 140 | /* |
| 141 | * After the command executed, we should find out the index of the |
Carl-Daniel Hailfinger | f8555e2 | 2009-07-23 01:36:08 +0000 | [diff] [blame] | 142 | * received byte. Here we just reset the FIFO pointer and skip the |
| 143 | * writecnt. |
| 144 | * It would be possible to increase the FIFO pointer by one instead |
| 145 | * of reading and discarding one byte from the FIFO. |
| 146 | * The FIFO is implemented on top of an 8 byte ring buffer and the |
| 147 | * buffer is never cleared. For every byte that is shifted out after |
| 148 | * the opcode, the FIFO already stores the response from the chip. |
| 149 | * Usually, the chip will respond with 0x00 or 0xff. |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 150 | */ |
| 151 | reset_internal_fifo_pointer(); |
| 152 | |
Carl-Daniel Hailfinger | f8555e2 | 2009-07-23 01:36:08 +0000 | [diff] [blame] | 153 | /* Skip the bytes we sent. */ |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 154 | for (count = 0; count < writecnt; count++) { |
Carl-Daniel Hailfinger | f8555e2 | 2009-07-23 01:36:08 +0000 | [diff] [blame] | 155 | cmd = mmio_readb(sb600_spibar + 0xC); |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 156 | msg_pspew("[ %2x]", cmd); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 159 | msg_pspew("The FIFO pointer after skipping is %d.\n", |
| 160 | mmio_readb(sb600_spibar + 0xd) & 0x07); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 161 | for (count = 0; count < readcnt; count++, readarr++) { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 162 | *readarr = mmio_readb(sb600_spibar + 0xC); |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 163 | msg_pspew("[%02x]", *readarr); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 164 | } |
Carl-Daniel Hailfinger | 643415b | 2010-01-10 01:59:50 +0000 | [diff] [blame] | 165 | msg_pspew("\n"); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |