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 | /* Note that CS# is active low, so val=0 means the chip is active. */ |
Stefan Tauner | 67d163d | 2013-01-15 17:37:48 +0000 | [diff] [blame] | 29 | static void bitbang_spi_set_cs(const struct bitbang_spi_master * const master, int val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 30 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 31 | master->set_cs(val); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Stefan Tauner | 67d163d | 2013-01-15 17:37:48 +0000 | [diff] [blame] | 34 | static void bitbang_spi_set_sck(const struct bitbang_spi_master * const master, int val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 35 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 36 | master->set_sck(val); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Stefan Tauner | 67d163d | 2013-01-15 17:37:48 +0000 | [diff] [blame] | 39 | static void bitbang_spi_set_mosi(const struct bitbang_spi_master * const master, int val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 40 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 41 | master->set_mosi(val); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Stefan Tauner | 67d163d | 2013-01-15 17:37:48 +0000 | [diff] [blame] | 44 | static int bitbang_spi_get_miso(const struct bitbang_spi_master * const master) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 45 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 46 | return master->get_miso(); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Stefan Tauner | 67d163d | 2013-01-15 17:37:48 +0000 | [diff] [blame] | 49 | static void bitbang_spi_request_bus(const struct bitbang_spi_master * const master) |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 50 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 51 | if (master->request_bus) |
| 52 | master->request_bus(); |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Stefan Tauner | 67d163d | 2013-01-15 17:37:48 +0000 | [diff] [blame] | 55 | static void bitbang_spi_release_bus(const struct bitbang_spi_master * const master) |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 56 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 57 | if (master->release_bus) |
| 58 | master->release_bus(); |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 61 | static int bitbang_spi_send_command(struct flashctx *flash, |
| 62 | unsigned int writecnt, unsigned int readcnt, |
| 63 | const unsigned char *writearr, |
| 64 | unsigned char *readarr); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 65 | |
| 66 | static const struct spi_programmer spi_programmer_bitbang = { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 67 | .type = SPI_CONTROLLER_BITBANG, |
| 68 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 69 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
| 70 | .command = bitbang_spi_send_command, |
| 71 | .multicommand = default_spi_send_multicommand, |
| 72 | .read = default_spi_read, |
| 73 | .write_256 = default_spi_write_256, |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 74 | .write_aai = default_spi_write_aai, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 77 | #if 0 // until it is needed |
| 78 | static int bitbang_spi_shutdown(const struct bitbang_spi_master *master) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 79 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 80 | /* FIXME: Run bitbang_spi_release_bus here or per command? */ |
| 81 | return 0; |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | int bitbang_spi_init(const struct bitbang_spi_master *master) |
| 86 | { |
| 87 | struct spi_programmer pgm = spi_programmer_bitbang; |
Carl-Daniel Hailfinger | 17e23ac | 2010-07-18 14:42:28 +0000 | [diff] [blame] | 88 | /* BITBANG_SPI_INVALID is 0, so if someone forgot to initialize ->type, |
| 89 | * we catch it here. Same goes for missing initialization of bitbanging |
| 90 | * functions. |
| 91 | */ |
| 92 | if (!master || master->type == BITBANG_SPI_INVALID || !master->set_cs || |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 93 | !master->set_sck || !master->set_mosi || !master->get_miso || |
| 94 | (master->request_bus && !master->release_bus) || |
| 95 | (!master->request_bus && master->release_bus)) { |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 96 | msg_perr("Incomplete SPI bitbang master setting!\n" |
| 97 | "Please report a bug at flashrom@flashrom.org\n"); |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 98 | return ERROR_FLASHROM_BUG; |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 101 | pgm.data = master; |
| 102 | register_spi_programmer(&pgm); |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 103 | |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 104 | /* Only mess with the bus if we're sure nobody else uses it. */ |
| 105 | bitbang_spi_request_bus(master); |
| 106 | bitbang_spi_set_cs(master, 1); |
| 107 | bitbang_spi_set_sck(master, 0); |
| 108 | bitbang_spi_set_mosi(master, 0); |
| 109 | /* FIXME: Release SPI bus here and request it again for each command or |
| 110 | * don't release it now and only release it on programmer shutdown? |
| 111 | */ |
| 112 | bitbang_spi_release_bus(master); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 116 | static uint8_t bitbang_spi_rw_byte(const struct bitbang_spi_master *master, |
| 117 | uint8_t val) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 118 | { |
| 119 | uint8_t ret = 0; |
| 120 | int i; |
| 121 | |
| 122 | for (i = 7; i >= 0; i--) { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 123 | bitbang_spi_set_mosi(master, (val >> i) & 1); |
| 124 | programmer_delay(master->half_period); |
| 125 | bitbang_spi_set_sck(master, 1); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 126 | ret <<= 1; |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 127 | ret |= bitbang_spi_get_miso(master); |
| 128 | programmer_delay(master->half_period); |
| 129 | bitbang_spi_set_sck(master, 0); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 130 | } |
| 131 | return ret; |
| 132 | } |
| 133 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 134 | static int bitbang_spi_send_command(struct flashctx *flash, |
| 135 | unsigned int writecnt, unsigned int readcnt, |
| 136 | const unsigned char *writearr, |
| 137 | unsigned char *readarr) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 138 | { |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 139 | int i; |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 140 | const struct bitbang_spi_master *master = flash->pgm->spi.data; |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 141 | |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 142 | /* FIXME: Run bitbang_spi_request_bus here or in programmer init? |
| 143 | * Requesting and releasing the SPI bus is handled in here to allow the |
| 144 | * programmer to use its own SPI engine for native accesses. |
| 145 | */ |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 146 | bitbang_spi_request_bus(master); |
| 147 | bitbang_spi_set_cs(master, 0); |
Michael Karcher | 1a854fc | 2010-07-17 10:42:34 +0000 | [diff] [blame] | 148 | for (i = 0; i < writecnt; i++) |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 149 | bitbang_spi_rw_byte(master, writearr[i]); |
Michael Karcher | 1a854fc | 2010-07-17 10:42:34 +0000 | [diff] [blame] | 150 | for (i = 0; i < readcnt; i++) |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 151 | readarr[i] = bitbang_spi_rw_byte(master, 0); |
Michael Karcher | 1a854fc | 2010-07-17 10:42:34 +0000 | [diff] [blame] | 152 | |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 153 | programmer_delay(master->half_period); |
| 154 | bitbang_spi_set_cs(master, 1); |
| 155 | programmer_delay(master->half_period); |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 156 | /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */ |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 157 | bitbang_spi_release_bus(master); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 158 | |
| 159 | return 0; |
| 160 | } |