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