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. |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 7 | * |
| 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 Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 23 | #include <string.h> |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 24 | #include <sys/mman.h> |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 25 | #include "flash.h" |
| 26 | #include "spi.h" |
| 27 | |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 28 | struct sb600_spi_controller { |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 29 | 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 Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 37 | }; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 38 | |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 39 | struct sb600_spi_controller *spi_bar = NULL; |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 40 | uint8_t *sb600_spibar; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 41 | |
| 42 | int sb600_spi_read(struct flashchip *flash, uint8_t *buf) |
| 43 | { |
| 44 | int rc = 0, i; |
| 45 | int total_size = flash->total_size * 1024; |
| 46 | int page_size = 8; |
| 47 | |
| 48 | for (i = 0; i < total_size / page_size; i++) |
| 49 | spi_nbyte_read(i * page_size, (void *)(buf + i * page_size), |
| 50 | page_size); |
| 51 | return rc; |
| 52 | } |
| 53 | |
| 54 | uint8_t sb600_read_status_register(void) |
| 55 | { |
| 56 | const unsigned char cmd[0x02] = { JEDEC_RDSR, 0x00 }; |
| 57 | unsigned char readarr[JEDEC_RDSR_INSIZE]; |
| 58 | |
| 59 | /* Read Status Register */ |
| 60 | spi_command(sizeof(cmd), sizeof(readarr), cmd, readarr); |
| 61 | return readarr[0]; |
| 62 | } |
| 63 | |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 64 | int sb600_spi_write_1(struct flashchip *flash, uint8_t *buf) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 65 | { |
| 66 | int rc = 0, i; |
| 67 | int total_size = flash->total_size * 1024; |
Carl-Daniel Hailfinger | 03adbe1 | 2009-05-09 02:09:45 +0000 | [diff] [blame] | 68 | int result; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 69 | |
| 70 | /* Erase first */ |
| 71 | printf("Erasing flash before programming... "); |
| 72 | flash->erase(flash); |
| 73 | printf("done.\n"); |
| 74 | |
| 75 | printf("Programming flash"); |
| 76 | for (i = 0; i < total_size; i++, buf++) { |
| 77 | spi_disable_blockprotect(); |
Carl-Daniel Hailfinger | 03adbe1 | 2009-05-09 02:09:45 +0000 | [diff] [blame] | 78 | result = spi_write_enable(); |
| 79 | if (result) |
| 80 | return result; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 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 | |
| 92 | void reset_internal_fifo_pointer(void) |
| 93 | { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 94 | mmio_writeb(mmio_readb(sb600_spibar + 2) | 0x10, sb600_spibar + 2); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 95 | |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 96 | while (mmio_readb(sb600_spibar + 0xD) & 0x7) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 97 | printf("reset\n"); |
| 98 | } |
| 99 | |
| 100 | void execute_command(void) |
| 101 | { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 102 | mmio_writeb(mmio_readb(sb600_spibar + 2) | 1, sb600_spibar + 2); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 103 | |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 104 | while (mmio_readb(sb600_spibar + 2) & 1) |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 105 | ; |
| 106 | } |
| 107 | |
| 108 | int 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 | |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 117 | spi_bar = (struct sb600_spi_controller *) sb600_spibar; |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 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 | |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 134 | mmio_writeb(cmd, sb600_spibar + 0); |
| 135 | mmio_writeb(readcnt << 4 | (writecnt), sb600_spibar + 1); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 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); |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 143 | mmio_writeb(*writearr, sb600_spibar + 0xC); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 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++) { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 163 | cmd = mmio_readb(sb600_spibar + 0xC); /* Skip the byte we send. */ |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 164 | printf_debug("[ %2x]", cmd); |
| 165 | } |
| 166 | |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 167 | printf_debug("The FIFO pointer 6 is %d.\n", mmio_readb(sb600_spibar + 0xd) & 0x07); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 168 | for (count = 0; count < readcnt; count++, readarr++) { |
Carl-Daniel Hailfinger | 78185dc | 2009-05-17 15:49:24 +0000 | [diff] [blame] | 169 | *readarr = mmio_readb(sb600_spibar + 0xC); |
Jason Wang | a3f04be | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 170 | printf_debug("[%02x]", *readarr); |
| 171 | } |
| 172 | printf_debug("\n"); |
| 173 | |
| 174 | return 0; |
| 175 | } |