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