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" |
Nico Huber | 5fc3154 | 2024-01-07 15:58:34 +0100 | [diff] [blame] | 23 | #include "spi_command.h" |
Nico Huber | d16a911 | 2024-01-07 00:11:44 +0100 | [diff] [blame] | 24 | #include "bitbang_spi.h" |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 25 | |
Edward O'Callaghan | cbc5ba0 | 2021-01-06 14:10:52 +1100 | [diff] [blame] | 26 | struct bitbang_spi_master_data { |
| 27 | const struct bitbang_spi_master *mst; |
Anastasia Klimchuk | a447c12 | 2021-05-31 11:20:01 +1000 | [diff] [blame] | 28 | void *spi_data; |
Edward O'Callaghan | cbc5ba0 | 2021-01-06 14:10:52 +1100 | [diff] [blame] | 29 | }; |
| 30 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 31 | /* 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] | 32 | 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] | 33 | { |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 34 | master->set_cs(val, spi_data); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 37 | 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] | 38 | { |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 39 | master->set_sck(val, spi_data); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 42 | 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] | 43 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 44 | if (master->request_bus) |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 45 | master->request_bus(spi_data); |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 48 | 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] | 49 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 50 | if (master->release_bus) |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 51 | master->release_bus(spi_data); |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 54 | static void bitbang_spi_set_sck_set_mosi(const struct bitbang_spi_master * const master, int sck, int mosi, |
| 55 | void *spi_data) |
Daniel Thompson | b623f40 | 2018-06-05 09:38:19 +0100 | [diff] [blame] | 56 | { |
| 57 | if (master->set_sck_set_mosi) { |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 58 | master->set_sck_set_mosi(sck, mosi, spi_data); |
Daniel Thompson | b623f40 | 2018-06-05 09:38:19 +0100 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 62 | master->set_sck(sck, spi_data); |
| 63 | master->set_mosi(mosi, spi_data); |
Daniel Thompson | b623f40 | 2018-06-05 09:38:19 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 66 | static int bitbang_spi_set_sck_get_miso(const struct bitbang_spi_master * const master, int sck, |
| 67 | void *spi_data) |
Daniel Thompson | b623f40 | 2018-06-05 09:38:19 +0100 | [diff] [blame] | 68 | { |
| 69 | if (master->set_sck_get_miso) |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 70 | return master->set_sck_get_miso(sck, spi_data); |
Daniel Thompson | b623f40 | 2018-06-05 09:38:19 +0100 | [diff] [blame] | 71 | |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 72 | master->set_sck(sck, spi_data); |
| 73 | return master->get_miso(spi_data); |
Daniel Thompson | b623f40 | 2018-06-05 09:38:19 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Nico Huber | 5fc3154 | 2024-01-07 15:58:34 +0100 | [diff] [blame] | 76 | static void bitbang_spi_idle_io(const struct bitbang_spi_master_data *bbs) |
| 77 | { |
| 78 | if (bbs->mst->set_idle_io) |
| 79 | bbs->mst->set_idle_io(bbs->spi_data); |
| 80 | } |
| 81 | |
| 82 | static void bitbang_spi_run_clock(const struct bitbang_spi_master_data *bbs, unsigned int cycles) |
| 83 | { |
| 84 | for (; cycles > 0; --cycles) { |
| 85 | bbs->mst->set_sck(0, bbs->spi_data); |
| 86 | programmer_delay(bbs->mst->half_period); |
| 87 | bbs->mst->set_sck(1, bbs->spi_data); |
| 88 | programmer_delay(bbs->mst->half_period); |
| 89 | } |
| 90 | } |
| 91 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 92 | static int bitbang_spi_send_command(const struct flashctx *flash, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 93 | unsigned int writecnt, unsigned int readcnt, |
| 94 | const unsigned char *writearr, |
| 95 | unsigned char *readarr); |
Nico Huber | 5fc3154 | 2024-01-07 15:58:34 +0100 | [diff] [blame] | 96 | static int bitbang_spi_send_multicommand(const struct flashctx *, struct spi_command *); |
Edward O'Callaghan | cbc5ba0 | 2021-01-06 14:10:52 +1100 | [diff] [blame] | 97 | static int bitbang_spi_shutdown(void *data); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 98 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 99 | static const struct spi_master spi_master_bitbang = { |
Nico Huber | 1cf407b | 2017-11-10 20:18:23 +0100 | [diff] [blame] | 100 | .features = SPI_MASTER_4BA, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 101 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 102 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
| 103 | .command = bitbang_spi_send_command, |
Nico Huber | 5fc3154 | 2024-01-07 15:58:34 +0100 | [diff] [blame] | 104 | .multicommand = bitbang_spi_send_multicommand, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 105 | .read = default_spi_read, |
| 106 | .write_256 = default_spi_write_256, |
Edward O'Callaghan | cbc5ba0 | 2021-01-06 14:10:52 +1100 | [diff] [blame] | 107 | .shutdown = bitbang_spi_shutdown, |
Aarya Chaumal | 0cea753 | 2022-07-04 18:21:50 +0530 | [diff] [blame] | 108 | .probe_opcode = default_spi_probe_opcode, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Edward O'Callaghan | cbc5ba0 | 2021-01-06 14:10:52 +1100 | [diff] [blame] | 111 | static int bitbang_spi_shutdown(void *data) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 112 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 113 | /* FIXME: Run bitbang_spi_release_bus here or per command? */ |
Edward O'Callaghan | cbc5ba0 | 2021-01-06 14:10:52 +1100 | [diff] [blame] | 114 | free(data); |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 115 | return 0; |
| 116 | } |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 117 | |
Anastasia Klimchuk | a447c12 | 2021-05-31 11:20:01 +1000 | [diff] [blame] | 118 | 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] | 119 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 120 | struct spi_master mst = spi_master_bitbang; |
Nico Huber | cb44eb7 | 2017-04-22 00:09:42 +0200 | [diff] [blame] | 121 | /* If someone forgot to initialize a bitbang function, we catch it here. */ |
| 122 | if (!master || !master->set_cs || |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 123 | !master->set_sck || !master->set_mosi || !master->get_miso || |
| 124 | (master->request_bus && !master->release_bus) || |
Nico Huber | 5fc3154 | 2024-01-07 15:58:34 +0100 | [diff] [blame] | 125 | (!master->request_bus && master->release_bus) || |
| 126 | (master->set_sck_set_dual_io && !master->set_sck_get_dual_io) || |
| 127 | (!master->set_sck_set_dual_io && master->set_sck_get_dual_io) || |
| 128 | (master->set_sck_set_quad_io && !master->set_sck_get_quad_io) || |
| 129 | (!master->set_sck_set_quad_io && master->set_sck_get_quad_io) || |
| 130 | ((master->set_sck_set_dual_io || master->set_sck_set_quad_io) && |
| 131 | !master->set_idle_io)) { |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 132 | msg_perr("Incomplete SPI bitbang master setting!\n" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 133 | "Please report a bug at flashprog@flashprog.org\n"); |
| 134 | return ERROR_FLASHPROG_BUG; |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Nico Huber | 5fc3154 | 2024-01-07 15:58:34 +0100 | [diff] [blame] | 137 | if (master->set_sck_set_dual_io) |
| 138 | mst.features |= SPI_MASTER_DUAL; |
| 139 | if (master->set_sck_set_quad_io) |
| 140 | mst.features |= SPI_MASTER_QUAD | SPI_MASTER_QPI; |
| 141 | |
Edward O'Callaghan | cbc5ba0 | 2021-01-06 14:10:52 +1100 | [diff] [blame] | 142 | struct bitbang_spi_master_data *data = calloc(1, sizeof(struct bitbang_spi_master_data)); |
| 143 | if (!data) { |
| 144 | msg_perr("Out of memory!\n"); |
| 145 | return ERROR_OOM; |
| 146 | } |
| 147 | |
| 148 | data->mst = master; |
Anastasia Klimchuk | a447c12 | 2021-05-31 11:20:01 +1000 | [diff] [blame] | 149 | data->spi_data = spi_data; |
Nico Huber | 89569d6 | 2023-01-12 23:31:40 +0100 | [diff] [blame] | 150 | register_spi_master(&mst, 0, data); |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 151 | |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 152 | /* 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] | 153 | bitbang_spi_request_bus(master, spi_data); |
Nico Huber | 5fc3154 | 2024-01-07 15:58:34 +0100 | [diff] [blame] | 154 | bitbang_spi_idle_io(data); |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 155 | bitbang_spi_set_cs(master, 1, spi_data); |
| 156 | bitbang_spi_set_sck_set_mosi(master, 0, 0, spi_data); |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 157 | /* FIXME: Release SPI bus here and request it again for each command or |
| 158 | * don't release it now and only release it on programmer shutdown? |
| 159 | */ |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 160 | bitbang_spi_release_bus(master, spi_data); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 164 | 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] | 165 | { |
| 166 | uint8_t ret = 0; |
| 167 | int i; |
| 168 | |
| 169 | for (i = 7; i >= 0; i--) { |
Daniel Thompson | 455a6fc | 2018-06-05 09:55:20 +0100 | [diff] [blame] | 170 | if (i == 0) |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 171 | bitbang_spi_set_sck_set_mosi(master, 0, 0, spi_data); |
Daniel Thompson | 455a6fc | 2018-06-05 09:55:20 +0100 | [diff] [blame] | 172 | else |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 173 | bitbang_spi_set_sck(master, 0, spi_data); |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 174 | programmer_delay(master->half_period); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 175 | ret <<= 1; |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 176 | ret |= bitbang_spi_set_sck_get_miso(master, 1, spi_data); |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 177 | programmer_delay(master->half_period); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 178 | } |
| 179 | return ret; |
| 180 | } |
| 181 | |
Nico Huber | 5fc3154 | 2024-01-07 15:58:34 +0100 | [diff] [blame] | 182 | static uint8_t bitbang_spi_read_dual(const struct bitbang_spi_master_data *const bbs) |
| 183 | { |
| 184 | uint8_t ret = 0; |
| 185 | int i; |
| 186 | |
| 187 | for (i = 6; i >= 0; i -= 2) { |
| 188 | bbs->mst->set_sck(0, bbs->spi_data); |
| 189 | programmer_delay(bbs->mst->half_period); |
| 190 | ret <<= 2; |
| 191 | ret |= bbs->mst->set_sck_get_dual_io(1, bbs->spi_data); |
| 192 | programmer_delay(bbs->mst->half_period); |
| 193 | } |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | static uint8_t bitbang_spi_read_quad(const struct bitbang_spi_master_data *const bbs) |
| 198 | { |
| 199 | uint8_t ret = 0; |
| 200 | int i; |
| 201 | |
| 202 | for (i = 4; i >= 0; i -= 4) { |
| 203 | bbs->mst->set_sck(0, bbs->spi_data); |
| 204 | programmer_delay(bbs->mst->half_period); |
| 205 | ret <<= 4; |
| 206 | ret |= bbs->mst->set_sck_get_quad_io(1, bbs->spi_data); |
| 207 | programmer_delay(bbs->mst->half_period); |
| 208 | } |
| 209 | return ret; |
| 210 | } |
| 211 | |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 212 | 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] | 213 | { |
| 214 | int i; |
| 215 | |
| 216 | for (i = 7; i >= 0; i--) { |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 217 | 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] | 218 | programmer_delay(master->half_period); |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 219 | bitbang_spi_set_sck(master, 1, spi_data); |
Daniel Thompson | 455a6fc | 2018-06-05 09:55:20 +0100 | [diff] [blame] | 220 | programmer_delay(master->half_period); |
| 221 | } |
| 222 | } |
| 223 | |
Nico Huber | 5fc3154 | 2024-01-07 15:58:34 +0100 | [diff] [blame] | 224 | static void bitbang_spi_write_dual(const struct bitbang_spi_master_data *bbs, uint8_t val) |
| 225 | { |
| 226 | int i; |
| 227 | |
| 228 | for (i = 6; i >= 0; i -= 2) { |
| 229 | bbs->mst->set_sck_set_dual_io(0, (val >> i) & 3, bbs->spi_data); |
| 230 | programmer_delay(bbs->mst->half_period); |
| 231 | bbs->mst->set_sck(1, bbs->spi_data); |
| 232 | programmer_delay(bbs->mst->half_period); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | static void bitbang_spi_write_quad(const struct bitbang_spi_master_data *bbs, uint8_t val) |
| 237 | { |
| 238 | int i; |
| 239 | |
| 240 | for (i = 4; i >= 0; i -= 4) { |
| 241 | bbs->mst->set_sck_set_quad_io(0, (val >> i) & 0xf, bbs->spi_data); |
| 242 | programmer_delay(bbs->mst->half_period); |
| 243 | bbs->mst->set_sck(1, bbs->spi_data); |
| 244 | programmer_delay(bbs->mst->half_period); |
| 245 | } |
| 246 | } |
| 247 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 248 | static int bitbang_spi_send_command(const struct flashctx *flash, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 249 | unsigned int writecnt, unsigned int readcnt, |
| 250 | const unsigned char *writearr, |
| 251 | unsigned char *readarr) |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 252 | { |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 253 | unsigned int i; |
Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 254 | const struct bitbang_spi_master_data *data = flash->mst.spi->data; |
Edward O'Callaghan | cbc5ba0 | 2021-01-06 14:10:52 +1100 | [diff] [blame] | 255 | const struct bitbang_spi_master *master = data->mst; |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 256 | |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 257 | /* FIXME: Run bitbang_spi_request_bus here or in programmer init? |
| 258 | * Requesting and releasing the SPI bus is handled in here to allow the |
| 259 | * programmer to use its own SPI engine for native accesses. |
| 260 | */ |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 261 | bitbang_spi_request_bus(master, data->spi_data); |
| 262 | bitbang_spi_set_cs(master, 0, data->spi_data); |
Michael Karcher | 1a854fc | 2010-07-17 10:42:34 +0000 | [diff] [blame] | 263 | for (i = 0; i < writecnt; i++) |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 264 | bitbang_spi_write_byte(master, writearr[i], data->spi_data); |
Michael Karcher | 1a854fc | 2010-07-17 10:42:34 +0000 | [diff] [blame] | 265 | for (i = 0; i < readcnt; i++) |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 266 | readarr[i] = bitbang_spi_read_byte(master, data->spi_data); |
Michael Karcher | 1a854fc | 2010-07-17 10:42:34 +0000 | [diff] [blame] | 267 | |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 268 | bitbang_spi_set_sck(master, 0, data->spi_data); |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 269 | programmer_delay(master->half_period); |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 270 | bitbang_spi_set_cs(master, 1, data->spi_data); |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 271 | programmer_delay(master->half_period); |
Carl-Daniel Hailfinger | 2822888 | 2010-09-15 00:17:37 +0000 | [diff] [blame] | 272 | /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */ |
Anastasia Klimchuk | 6e6cce0 | 2021-05-26 10:07:59 +1000 | [diff] [blame] | 273 | bitbang_spi_release_bus(master, data->spi_data); |
Carl-Daniel Hailfinger | 547872b | 2009-09-28 13:15:16 +0000 | [diff] [blame] | 274 | |
| 275 | return 0; |
| 276 | } |
Nico Huber | 5fc3154 | 2024-01-07 15:58:34 +0100 | [diff] [blame] | 277 | |
| 278 | static int bitbang_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds) |
| 279 | { |
| 280 | const struct bitbang_spi_master_data *const bbs = flash->mst.spi->data; |
| 281 | int ret = 0; |
| 282 | |
| 283 | bitbang_spi_request_bus(bbs->mst, bbs->spi_data); |
| 284 | |
| 285 | for (; !spi_is_empty(cmds); ++cmds) { |
| 286 | size_t write_single = 0, write_dual = 0, write_quad = 0; |
| 287 | size_t read_single = 0, read_dual = 0, read_quad = 0; |
| 288 | unsigned int high_z_cycles; |
| 289 | |
| 290 | switch (cmds->io_mode) { |
| 291 | case SINGLE_IO_1_1_1: |
| 292 | write_single = cmds->opcode_len + cmds->address_len + cmds->write_len; |
| 293 | high_z_cycles = 8 * cmds->high_z_len; |
| 294 | read_single = cmds->read_len; |
| 295 | break; |
| 296 | case DUAL_OUT_1_1_2: |
| 297 | write_single = cmds->opcode_len + cmds->address_len + cmds->write_len; |
| 298 | high_z_cycles = 4 * cmds->high_z_len; |
| 299 | read_dual = cmds->read_len; |
| 300 | break; |
| 301 | case DUAL_IO_1_2_2: |
| 302 | write_single = cmds->opcode_len; |
| 303 | write_dual = cmds->address_len + cmds->write_len; |
| 304 | high_z_cycles = 4 * cmds->high_z_len; |
| 305 | read_dual = cmds->read_len; |
| 306 | break; |
| 307 | case QUAD_OUT_1_1_4: |
| 308 | write_single = cmds->opcode_len + cmds->address_len + cmds->write_len; |
| 309 | high_z_cycles = 2 * cmds->high_z_len; |
| 310 | read_quad = cmds->read_len; |
| 311 | break; |
| 312 | case QUAD_IO_1_4_4: |
| 313 | write_single = cmds->opcode_len; |
| 314 | write_quad = cmds->address_len + cmds->write_len; |
| 315 | high_z_cycles = 2 * cmds->high_z_len; |
| 316 | read_quad = cmds->read_len; |
| 317 | break; |
| 318 | case QPI_4_4_4: |
| 319 | write_quad = cmds->opcode_len + cmds->address_len + cmds->write_len; |
| 320 | high_z_cycles = 2 * cmds->high_z_len; |
| 321 | read_quad = cmds->read_len; |
| 322 | break; |
| 323 | default: |
| 324 | return SPI_FLASHPROG_BUG; |
| 325 | } |
| 326 | |
| 327 | bitbang_spi_set_cs(bbs->mst, 0, bbs->spi_data); |
| 328 | |
| 329 | const unsigned char *out = cmds->writearr; |
| 330 | for (; write_single > 0; --write_single, ++out) |
| 331 | bitbang_spi_write_byte(bbs->mst, *out, bbs->spi_data); |
| 332 | for (; write_dual > 0; --write_dual, ++out) |
| 333 | bitbang_spi_write_dual(bbs, *out); |
| 334 | for (; write_quad > 0; --write_quad, ++out) |
| 335 | bitbang_spi_write_quad(bbs, *out); |
| 336 | |
| 337 | if (high_z_cycles || read_dual || read_quad) { |
| 338 | bitbang_spi_idle_io(bbs); |
| 339 | bitbang_spi_run_clock(bbs, high_z_cycles); |
| 340 | } |
| 341 | |
| 342 | unsigned char *in = cmds->readarr; |
| 343 | for (; read_quad > 0; --read_quad, ++in) |
| 344 | *in = bitbang_spi_read_quad(bbs); |
| 345 | for (; read_dual > 0; --read_dual, ++in) |
| 346 | *in = bitbang_spi_read_dual(bbs); |
| 347 | for (; read_single > 0; --read_single, ++in) |
| 348 | *in = bitbang_spi_read_byte(bbs->mst, bbs->spi_data); |
| 349 | |
| 350 | bitbang_spi_set_sck(bbs->mst, 0, bbs->spi_data); |
| 351 | programmer_delay(bbs->mst->half_period); |
| 352 | bitbang_spi_set_cs(bbs->mst, 1, bbs->spi_data); |
| 353 | programmer_delay(bbs->mst->half_period); |
| 354 | } |
| 355 | |
| 356 | bitbang_spi_release_bus(bbs->mst, bbs->spi_data); |
| 357 | |
| 358 | return ret; |
| 359 | } |