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> |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <ctype.h> |
| 24 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 25 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 26 | #include "programmer.h" |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 27 | #include "spi.h" |
| 28 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 29 | /* Length of half a clock period in usecs. */ |
| 30 | static int bitbang_spi_half_period; |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 31 | |
Michael Karcher | 62175a0 | 2010-07-17 23:27:47 +0000 | [diff] [blame] | 32 | static const struct bitbang_spi_master *bitbang_spi_master = NULL; |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 33 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 34 | /* Note that CS# is active low, so val=0 means the chip is active. */ |
| 35 | static void bitbang_spi_set_cs(int val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 36 | { |
Michael Karcher | 62175a0 | 2010-07-17 23:27:47 +0000 | [diff] [blame] | 37 | bitbang_spi_master->set_cs(val); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 40 | static void bitbang_spi_set_sck(int val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 41 | { |
Michael Karcher | 62175a0 | 2010-07-17 23:27:47 +0000 | [diff] [blame] | 42 | bitbang_spi_master->set_sck(val); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 45 | static void bitbang_spi_set_mosi(int val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 46 | { |
Michael Karcher | 62175a0 | 2010-07-17 23:27:47 +0000 | [diff] [blame] | 47 | bitbang_spi_master->set_mosi(val); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 50 | static int bitbang_spi_get_miso(void) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 51 | { |
Michael Karcher | 62175a0 | 2010-07-17 23:27:47 +0000 | [diff] [blame] | 52 | return bitbang_spi_master->get_miso(); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 55 | static void bitbang_spi_request_bus(void) |
| 56 | { |
| 57 | if (bitbang_spi_master->request_bus) |
| 58 | bitbang_spi_master->request_bus(); |
| 59 | } |
| 60 | |
| 61 | static void bitbang_spi_release_bus(void) |
| 62 | { |
| 63 | if (bitbang_spi_master->release_bus) |
| 64 | bitbang_spi_master->release_bus(); |
| 65 | } |
| 66 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 67 | static int bitbang_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 68 | const unsigned char *writearr, unsigned char *readarr); |
| 69 | |
| 70 | static const struct spi_programmer spi_programmer_bitbang = { |
| 71 | .type = SPI_CONTROLLER_BITBANG, |
| 72 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 73 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
| 74 | .command = bitbang_spi_send_command, |
| 75 | .multicommand = default_spi_send_multicommand, |
| 76 | .read = default_spi_read, |
| 77 | .write_256 = default_spi_write_256, |
| 78 | }; |
| 79 | |
Michael Karcher | 62175a0 | 2010-07-17 23:27:47 +0000 | [diff] [blame] | 80 | 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] | 81 | { |
Carl-Daniel Hailfinger | 17e23ac | 2010-07-18 14:42:28 +0000 | [diff] [blame] | 82 | /* BITBANG_SPI_INVALID is 0, so if someone forgot to initialize ->type, |
| 83 | * we catch it here. Same goes for missing initialization of bitbanging |
| 84 | * functions. |
| 85 | */ |
| 86 | if (!master || master->type == BITBANG_SPI_INVALID || !master->set_cs || |
| 87 | !master->set_sck || !master->set_mosi || !master->get_miso) { |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 88 | msg_perr("Incomplete SPI bitbang master setting!\n" |
| 89 | "Please report a bug at flashrom@flashrom.org\n"); |
Carl-Daniel Hailfinger | 17e23ac | 2010-07-18 14:42:28 +0000 | [diff] [blame] | 90 | return 1; |
| 91 | } |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 92 | if (bitbang_spi_master) { |
| 93 | msg_perr("SPI bitbang master already initialized!\n" |
| 94 | "Please report a bug at flashrom@flashrom.org\n"); |
| 95 | return 1; |
| 96 | } |
| 97 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 98 | bitbang_spi_master = master; |
| 99 | bitbang_spi_half_period = halfperiod; |
| 100 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 101 | register_spi_programmer(&spi_programmer_bitbang); |
| 102 | |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 103 | /* FIXME: Run bitbang_spi_request_bus here or in programmer init? */ |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 104 | bitbang_spi_set_cs(1); |
| 105 | bitbang_spi_set_sck(0); |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 106 | bitbang_spi_set_mosi(0); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 110 | int bitbang_spi_shutdown(const struct bitbang_spi_master *master) |
| 111 | { |
| 112 | if (!bitbang_spi_master) { |
| 113 | msg_perr("Shutting down an uninitialized SPI bitbang master!\n" |
| 114 | "Please report a bug at flashrom@flashrom.org\n"); |
| 115 | return 1; |
| 116 | } |
| 117 | if (master != bitbang_spi_master) { |
| 118 | msg_perr("Shutting down a mismatched SPI bitbang master!\n" |
| 119 | "Please report a bug at flashrom@flashrom.org\n"); |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | /* FIXME: Run bitbang_spi_release_bus here or per command? */ |
| 124 | bitbang_spi_master = NULL; |
| 125 | return 0; |
| 126 | } |
| 127 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 128 | static uint8_t bitbang_spi_readwrite_byte(uint8_t val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 129 | { |
| 130 | uint8_t ret = 0; |
| 131 | int i; |
| 132 | |
| 133 | for (i = 7; i >= 0; i--) { |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 134 | bitbang_spi_set_mosi((val >> i) & 1); |
| 135 | programmer_delay(bitbang_spi_half_period); |
| 136 | bitbang_spi_set_sck(1); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 137 | ret <<= 1; |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 138 | ret |= bitbang_spi_get_miso(); |
| 139 | programmer_delay(bitbang_spi_half_period); |
| 140 | bitbang_spi_set_sck(0); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 141 | } |
| 142 | return ret; |
| 143 | } |
| 144 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 145 | static int bitbang_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 146 | const unsigned char *writearr, unsigned char *readarr) |
| 147 | { |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 148 | int i; |
| 149 | |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 150 | /* FIXME: Run bitbang_spi_request_bus here or in programmer init? |
| 151 | * Requesting and releasing the SPI bus is handled in here to allow the |
| 152 | * programmer to use its own SPI engine for native accesses. |
| 153 | */ |
| 154 | bitbang_spi_request_bus(); |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 155 | bitbang_spi_set_cs(0); |
Michael Karcher | 1a854fc | 2010-07-17 10:42:34 +0000 | [diff] [blame] | 156 | for (i = 0; i < writecnt; i++) |
| 157 | bitbang_spi_readwrite_byte(writearr[i]); |
| 158 | for (i = 0; i < readcnt; i++) |
| 159 | readarr[i] = bitbang_spi_readwrite_byte(0); |
| 160 | |
Carl-Daniel Hailfinger | 3a4781e | 2009-10-01 14:51:25 +0000 | [diff] [blame] | 161 | programmer_delay(bitbang_spi_half_period); |
| 162 | bitbang_spi_set_cs(1); |
| 163 | programmer_delay(bitbang_spi_half_period); |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 164 | /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */ |
| 165 | bitbang_spi_release_bus(); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |