blob: 33ea050ee89472c2790d697d510cd1bb79809323 [file] [log] [blame]
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00004 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +00005 *
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 Hailfinger547872b2009-09-28 13:15:16 +000014 */
15
16#include <stdio.h>
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000017#include <string.h>
18#include <stdlib.h>
19#include <ctype.h>
20#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000021#include "programmer.h"
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000022#include "spi.h"
Nico Huber5fc31542024-01-07 15:58:34 +010023#include "spi_command.h"
Nico Huberd16a9112024-01-07 00:11:44 +010024#include "bitbang_spi.h"
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000025
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +110026struct bitbang_spi_master_data {
27 const struct bitbang_spi_master *mst;
Anastasia Klimchuka447c122021-05-31 11:20:01 +100028 void *spi_data;
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +110029};
30
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000031/* Note that CS# is active low, so val=0 means the chip is active. */
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100032static void bitbang_spi_set_cs(const struct bitbang_spi_master * const master, int val, void *spi_data)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000033{
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100034 master->set_cs(val, spi_data);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000035}
36
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100037static void bitbang_spi_set_sck(const struct bitbang_spi_master * const master, int val, void *spi_data)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000038{
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100039 master->set_sck(val, spi_data);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000040}
41
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100042static void bitbang_spi_request_bus(const struct bitbang_spi_master * const master, void *spi_data)
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000043{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000044 if (master->request_bus)
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100045 master->request_bus(spi_data);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000046}
47
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100048static void bitbang_spi_release_bus(const struct bitbang_spi_master * const master, void *spi_data)
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000049{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000050 if (master->release_bus)
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100051 master->release_bus(spi_data);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000052}
53
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100054static void bitbang_spi_set_sck_set_mosi(const struct bitbang_spi_master * const master, int sck, int mosi,
55 void *spi_data)
Daniel Thompsonb623f402018-06-05 09:38:19 +010056{
57 if (master->set_sck_set_mosi) {
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100058 master->set_sck_set_mosi(sck, mosi, spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +010059 return;
60 }
61
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100062 master->set_sck(sck, spi_data);
63 master->set_mosi(mosi, spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +010064}
65
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100066static int bitbang_spi_set_sck_get_miso(const struct bitbang_spi_master * const master, int sck,
67 void *spi_data)
Daniel Thompsonb623f402018-06-05 09:38:19 +010068{
69 if (master->set_sck_get_miso)
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100070 return master->set_sck_get_miso(sck, spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +010071
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100072 master->set_sck(sck, spi_data);
73 return master->get_miso(spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +010074}
75
Nico Huber5fc31542024-01-07 15:58:34 +010076static 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
82static 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'Callaghan5eca4272020-04-12 17:27:53 +100092static int bitbang_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000093 unsigned int writecnt, unsigned int readcnt,
94 const unsigned char *writearr,
95 unsigned char *readarr);
Nico Huber5fc31542024-01-07 15:58:34 +010096static int bitbang_spi_send_multicommand(const struct flashctx *, struct spi_command *);
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +110097static int bitbang_spi_shutdown(void *data);
Michael Karcherb9dbe482011-05-11 17:07:07 +000098
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000099static const struct spi_master spi_master_bitbang = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100100 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000101 .max_data_read = MAX_DATA_READ_UNLIMITED,
102 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
103 .command = bitbang_spi_send_command,
Nico Huber5fc31542024-01-07 15:58:34 +0100104 .multicommand = bitbang_spi_send_multicommand,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000105 .read = default_spi_read,
106 .write_256 = default_spi_write_256,
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +1100107 .shutdown = bitbang_spi_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +0530108 .probe_opcode = default_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000109};
110
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +1100111static int bitbang_spi_shutdown(void *data)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000112{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000113 /* FIXME: Run bitbang_spi_release_bus here or per command? */
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +1100114 free(data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000115 return 0;
116}
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000117
Anastasia Klimchuka447c122021-05-31 11:20:01 +1000118int register_spi_bitbang_master(const struct bitbang_spi_master *master, void *spi_data)
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000119{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000120 struct spi_master mst = spi_master_bitbang;
Nico Hubercb44eb72017-04-22 00:09:42 +0200121 /* If someone forgot to initialize a bitbang function, we catch it here. */
122 if (!master || !master->set_cs ||
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000123 !master->set_sck || !master->set_mosi || !master->get_miso ||
124 (master->request_bus && !master->release_bus) ||
Nico Huber5fc31542024-01-07 15:58:34 +0100125 (!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 Hailfinger28228882010-09-15 00:17:37 +0000132 msg_perr("Incomplete SPI bitbang master setting!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200133 "Please report a bug at flashprog@flashprog.org\n");
134 return ERROR_FLASHPROG_BUG;
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000135 }
136
Nico Huber5fc31542024-01-07 15:58:34 +0100137 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'Callaghancbc5ba02021-01-06 14:10:52 +1100142 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 Klimchuka447c122021-05-31 11:20:01 +1000149 data->spi_data = spi_data;
Nico Huber89569d62023-01-12 23:31:40 +0100150 register_spi_master(&mst, 0, data);
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +0000151
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000152 /* Only mess with the bus if we're sure nobody else uses it. */
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000153 bitbang_spi_request_bus(master, spi_data);
Nico Huber5fc31542024-01-07 15:58:34 +0100154 bitbang_spi_idle_io(data);
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000155 bitbang_spi_set_cs(master, 1, spi_data);
156 bitbang_spi_set_sck_set_mosi(master, 0, 0, spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000157 /* 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 Klimchuk6e6cce02021-05-26 10:07:59 +1000160 bitbang_spi_release_bus(master, spi_data);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000161 return 0;
162}
163
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000164static uint8_t bitbang_spi_read_byte(const struct bitbang_spi_master *master, void *spi_data)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000165{
166 uint8_t ret = 0;
167 int i;
168
169 for (i = 7; i >= 0; i--) {
Daniel Thompson455a6fc2018-06-05 09:55:20 +0100170 if (i == 0)
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000171 bitbang_spi_set_sck_set_mosi(master, 0, 0, spi_data);
Daniel Thompson455a6fc2018-06-05 09:55:20 +0100172 else
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000173 bitbang_spi_set_sck(master, 0, spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000174 programmer_delay(master->half_period);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000175 ret <<= 1;
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000176 ret |= bitbang_spi_set_sck_get_miso(master, 1, spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000177 programmer_delay(master->half_period);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000178 }
179 return ret;
180}
181
Nico Huber5fc31542024-01-07 15:58:34 +0100182static 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
197static 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 Klimchuk6e6cce02021-05-26 10:07:59 +1000212static void bitbang_spi_write_byte(const struct bitbang_spi_master *master, uint8_t val, void *spi_data)
Daniel Thompson455a6fc2018-06-05 09:55:20 +0100213{
214 int i;
215
216 for (i = 7; i >= 0; i--) {
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000217 bitbang_spi_set_sck_set_mosi(master, 0, (val >> i) & 1, spi_data);
Daniel Thompson455a6fc2018-06-05 09:55:20 +0100218 programmer_delay(master->half_period);
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000219 bitbang_spi_set_sck(master, 1, spi_data);
Daniel Thompson455a6fc2018-06-05 09:55:20 +0100220 programmer_delay(master->half_period);
221 }
222}
223
Nico Huber5fc31542024-01-07 15:58:34 +0100224static 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
236static 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'Callaghan5eca4272020-04-12 17:27:53 +1000248static int bitbang_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000249 unsigned int writecnt, unsigned int readcnt,
250 const unsigned char *writearr,
251 unsigned char *readarr)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000252{
Nico Huber519be662018-12-23 20:03:35 +0100253 unsigned int i;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100254 const struct bitbang_spi_master_data *data = flash->mst.spi->data;
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +1100255 const struct bitbang_spi_master *master = data->mst;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000256
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000257 /* 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 Klimchuk6e6cce02021-05-26 10:07:59 +1000261 bitbang_spi_request_bus(master, data->spi_data);
262 bitbang_spi_set_cs(master, 0, data->spi_data);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000263 for (i = 0; i < writecnt; i++)
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000264 bitbang_spi_write_byte(master, writearr[i], data->spi_data);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000265 for (i = 0; i < readcnt; i++)
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000266 readarr[i] = bitbang_spi_read_byte(master, data->spi_data);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000267
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000268 bitbang_spi_set_sck(master, 0, data->spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000269 programmer_delay(master->half_period);
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000270 bitbang_spi_set_cs(master, 1, data->spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000271 programmer_delay(master->half_period);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000272 /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000273 bitbang_spi_release_bus(master, data->spi_data);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000274
275 return 0;
276}
Nico Huber5fc31542024-01-07 15:58:34 +0100277
278static 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}