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 | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame^] | 27 | #include "programmer.h" |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 28 | #include "spi.h" |
| 29 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 30 | /* Length of half a clock period in usecs. */ |
| 31 | static int bitbang_spi_half_period; |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 32 | |
Michael Karcher | 62175a0 | 2010-07-17 23:27:47 +0000 | [diff] [blame] | 33 | static const struct bitbang_spi_master *bitbang_spi_master = NULL; |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 34 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 35 | /* Note that CS# is active low, so val=0 means the chip is active. */ |
| 36 | static void bitbang_spi_set_cs(int val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 37 | { |
Michael Karcher | 62175a0 | 2010-07-17 23:27:47 +0000 | [diff] [blame] | 38 | bitbang_spi_master->set_cs(val); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 41 | static void bitbang_spi_set_sck(int val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 42 | { |
Michael Karcher | 62175a0 | 2010-07-17 23:27:47 +0000 | [diff] [blame] | 43 | bitbang_spi_master->set_sck(val); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 46 | static void bitbang_spi_set_mosi(int val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 47 | { |
Michael Karcher | 62175a0 | 2010-07-17 23:27:47 +0000 | [diff] [blame] | 48 | bitbang_spi_master->set_mosi(val); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 51 | static int bitbang_spi_get_miso(void) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 52 | { |
Michael Karcher | 62175a0 | 2010-07-17 23:27:47 +0000 | [diff] [blame] | 53 | return bitbang_spi_master->get_miso(); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Michael Karcher | 62175a0 | 2010-07-17 23:27:47 +0000 | [diff] [blame] | 56 | int bitbang_spi_init(const struct bitbang_spi_master *master, int halfperiod) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 57 | { |
Carl-Daniel Hailfinger | 17e23ac | 2010-07-18 14:42:28 +0000 | [diff] [blame] | 58 | /* BITBANG_SPI_INVALID is 0, so if someone forgot to initialize ->type, |
| 59 | * we catch it here. Same goes for missing initialization of bitbanging |
| 60 | * functions. |
| 61 | */ |
| 62 | if (!master || master->type == BITBANG_SPI_INVALID || !master->set_cs || |
| 63 | !master->set_sck || !master->set_mosi || !master->get_miso) { |
| 64 | msg_perr("Incomplete bitbanging SPI master setting! Please " |
| 65 | "report a bug at flashrom@flashrom.org\n"); |
| 66 | return 1; |
| 67 | } |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 68 | bitbang_spi_master = master; |
| 69 | bitbang_spi_half_period = halfperiod; |
| 70 | |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 71 | bitbang_spi_set_cs(1); |
| 72 | bitbang_spi_set_sck(0); |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 73 | bitbang_spi_set_mosi(0); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 77 | static uint8_t bitbang_spi_readwrite_byte(uint8_t val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 78 | { |
| 79 | uint8_t ret = 0; |
| 80 | int i; |
| 81 | |
| 82 | for (i = 7; i >= 0; i--) { |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 83 | bitbang_spi_set_mosi((val >> i) & 1); |
| 84 | programmer_delay(bitbang_spi_half_period); |
| 85 | bitbang_spi_set_sck(1); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 86 | ret <<= 1; |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 87 | ret |= bitbang_spi_get_miso(); |
| 88 | programmer_delay(bitbang_spi_half_period); |
| 89 | bitbang_spi_set_sck(0); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 90 | } |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | int bitbang_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 95 | const unsigned char *writearr, unsigned char *readarr) |
| 96 | { |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 97 | int i; |
| 98 | |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 99 | bitbang_spi_set_cs(0); |
Michael Karcher | 1a854fc | 2010-07-17 10:42:34 +0000 | [diff] [blame] | 100 | for (i = 0; i < writecnt; i++) |
| 101 | bitbang_spi_readwrite_byte(writearr[i]); |
| 102 | for (i = 0; i < readcnt; i++) |
| 103 | readarr[i] = bitbang_spi_readwrite_byte(0); |
| 104 | |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 105 | programmer_delay(bitbang_spi_half_period); |
| 106 | bitbang_spi_set_cs(1); |
| 107 | programmer_delay(bitbang_spi_half_period); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | int bitbang_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 113 | { |
| 114 | /* Maximum read length is unlimited, use 64k bytes. */ |
| 115 | return spi_read_chunked(flash, buf, start, len, 64 * 1024); |
| 116 | } |
| 117 | |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 118 | int bitbang_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 119 | { |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 120 | return spi_write_chunked(flash, buf, start, len, 256); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 121 | } |