Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <ctype.h> |
| 25 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 26 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 27 | #include "spi.h" |
| 28 | |
| 29 | /* Length of half a clock period in usecs */ |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 30 | int bitbang_spi_half_period = 0; |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 31 | |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 32 | enum bitbang_spi_master bitbang_spi_master = BITBANG_SPI_INVALID; |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 33 | |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 34 | const struct bitbang_spi_master_entry bitbang_spi_master_table[] = { |
| 35 | {}, /* This entry corresponds to BITBANG_SPI_INVALID. */ |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 38 | const int bitbang_spi_master_count = ARRAY_SIZE(bitbang_spi_master_table); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 39 | |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 40 | void bitbang_spi_set_cs(int val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 41 | { |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 42 | bitbang_spi_master_table[bitbang_spi_master].set_cs(val); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 45 | void bitbang_spi_set_sck(int val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 46 | { |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 47 | bitbang_spi_master_table[bitbang_spi_master].set_sck(val); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 50 | void bitbang_spi_set_mosi(int val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 51 | { |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 52 | bitbang_spi_master_table[bitbang_spi_master].set_mosi(val); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 55 | int bitbang_spi_get_miso(void) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 56 | { |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 57 | return bitbang_spi_master_table[bitbang_spi_master].get_miso(); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | int bitbang_spi_init(void) |
| 61 | { |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 62 | bitbang_spi_set_cs(1); |
| 63 | bitbang_spi_set_sck(0); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 64 | buses_supported = CHIP_BUSTYPE_SPI; |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | uint8_t bitbang_spi_readwrite_byte(uint8_t val) |
| 69 | { |
| 70 | uint8_t ret = 0; |
| 71 | int i; |
| 72 | |
| 73 | for (i = 7; i >= 0; i--) { |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 74 | bitbang_spi_set_mosi((val >> i) & 1); |
| 75 | programmer_delay(bitbang_spi_half_period); |
| 76 | bitbang_spi_set_sck(1); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 77 | ret <<= 1; |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 78 | ret |= bitbang_spi_get_miso(); |
| 79 | programmer_delay(bitbang_spi_half_period); |
| 80 | bitbang_spi_set_sck(0); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 81 | } |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | int bitbang_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 86 | const unsigned char *writearr, unsigned char *readarr) |
| 87 | { |
| 88 | static unsigned char *bufout = NULL; |
| 89 | static unsigned char *bufin = NULL; |
| 90 | static int oldbufsize = 0; |
Carl-Daniel Hailfinger | b7e0145 | 2009-11-25 16:58:17 +0000 | [diff] [blame] | 91 | int bufsize; |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 92 | int i; |
| 93 | |
| 94 | /* Arbitrary size limitation here. We're only constrained by memory. */ |
| 95 | if (writecnt > 65536 || readcnt > 65536) |
| 96 | return SPI_INVALID_LENGTH; |
| 97 | |
Carl-Daniel Hailfinger | b7e0145 | 2009-11-25 16:58:17 +0000 | [diff] [blame] | 98 | bufsize = max(writecnt + readcnt, 260); |
| 99 | /* Never shrink. realloc() calls are expensive. */ |
| 100 | if (bufsize > oldbufsize) { |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 101 | bufout = realloc(bufout, bufsize); |
| 102 | if (!bufout) { |
Sean Nelson | 676a232 | 2010-01-09 23:58:37 +0000 | [diff] [blame] | 103 | msg_perr("Out of memory!\n"); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 104 | if (bufin) |
| 105 | free(bufin); |
| 106 | exit(1); |
| 107 | } |
| 108 | bufin = realloc(bufout, bufsize); |
| 109 | if (!bufin) { |
Sean Nelson | 676a232 | 2010-01-09 23:58:37 +0000 | [diff] [blame] | 110 | msg_perr("Out of memory!\n"); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 111 | if (bufout) |
| 112 | free(bufout); |
| 113 | exit(1); |
| 114 | } |
Carl-Daniel Hailfinger | b7e0145 | 2009-11-25 16:58:17 +0000 | [diff] [blame] | 115 | oldbufsize = bufsize; |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | memcpy(bufout, writearr, writecnt); |
| 119 | /* Shift out 0x00 while reading data. */ |
| 120 | memset(bufout + writecnt, 0x00, readcnt); |
| 121 | /* Make sure any non-read data is 0xff. */ |
| 122 | memset(bufin + writecnt, 0xff, readcnt); |
| 123 | |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 124 | bitbang_spi_set_cs(0); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 125 | for (i = 0; i < readcnt + writecnt; i++) { |
| 126 | bufin[i] = bitbang_spi_readwrite_byte(bufout[i]); |
| 127 | } |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 128 | programmer_delay(bitbang_spi_half_period); |
| 129 | bitbang_spi_set_cs(1); |
| 130 | programmer_delay(bitbang_spi_half_period); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 131 | memcpy(readarr, bufin + writecnt, readcnt); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | int bitbang_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 137 | { |
| 138 | /* Maximum read length is unlimited, use 64k bytes. */ |
| 139 | return spi_read_chunked(flash, buf, start, len, 64 * 1024); |
| 140 | } |
| 141 | |
| 142 | int bitbang_spi_write_256(struct flashchip *flash, uint8_t *buf) |
| 143 | { |
| 144 | int total_size = 1024 * flash->total_size; |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 145 | |
Sean Nelson | 676a232 | 2010-01-09 23:58:37 +0000 | [diff] [blame] | 146 | msg_pdbg("total_size is %d\n", total_size); |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 147 | return spi_write_chunked(flash, buf, 0, total_size, 256); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 148 | } |