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