blob: 1c2c1787183b33c63c4e454fa8c0be3fb2f2e404 [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 Huberd16a9112024-01-07 00:11:44 +010023#include "bitbang_spi.h"
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000024
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +110025struct bitbang_spi_master_data {
26 const struct bitbang_spi_master *mst;
Anastasia Klimchuka447c122021-05-31 11:20:01 +100027 void *spi_data;
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +110028};
29
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000030/* Note that CS# is active low, so val=0 means the chip is active. */
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100031static 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 +000032{
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100033 master->set_cs(val, spi_data);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000034}
35
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100036static 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 +000037{
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100038 master->set_sck(val, spi_data);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000039}
40
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100041static void bitbang_spi_request_bus(const struct bitbang_spi_master * const master, void *spi_data)
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000042{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000043 if (master->request_bus)
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100044 master->request_bus(spi_data);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000045}
46
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100047static void bitbang_spi_release_bus(const struct bitbang_spi_master * const master, void *spi_data)
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000048{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000049 if (master->release_bus)
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100050 master->release_bus(spi_data);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000051}
52
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100053static void bitbang_spi_set_sck_set_mosi(const struct bitbang_spi_master * const master, int sck, int mosi,
54 void *spi_data)
Daniel Thompsonb623f402018-06-05 09:38:19 +010055{
56 if (master->set_sck_set_mosi) {
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100057 master->set_sck_set_mosi(sck, mosi, spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +010058 return;
59 }
60
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100061 master->set_sck(sck, spi_data);
62 master->set_mosi(mosi, spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +010063}
64
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100065static int bitbang_spi_set_sck_get_miso(const struct bitbang_spi_master * const master, int sck,
66 void *spi_data)
Daniel Thompsonb623f402018-06-05 09:38:19 +010067{
68 if (master->set_sck_get_miso)
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100069 return master->set_sck_get_miso(sck, spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +010070
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +100071 master->set_sck(sck, spi_data);
72 return master->get_miso(spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +010073}
74
Edward O'Callaghan5eca4272020-04-12 17:27:53 +100075static int bitbang_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000076 unsigned int writecnt, unsigned int readcnt,
77 const unsigned char *writearr,
78 unsigned char *readarr);
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +110079static int bitbang_spi_shutdown(void *data);
Michael Karcherb9dbe482011-05-11 17:07:07 +000080
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000081static const struct spi_master spi_master_bitbang = {
Nico Huber1cf407b2017-11-10 20:18:23 +010082 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +000083 .max_data_read = MAX_DATA_READ_UNLIMITED,
84 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
85 .command = bitbang_spi_send_command,
86 .multicommand = default_spi_send_multicommand,
87 .read = default_spi_read,
88 .write_256 = default_spi_write_256,
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +110089 .shutdown = bitbang_spi_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +053090 .probe_opcode = default_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +000091};
92
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +110093static int bitbang_spi_shutdown(void *data)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000094{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000095 /* FIXME: Run bitbang_spi_release_bus here or per command? */
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +110096 free(data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000097 return 0;
98}
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000099
Anastasia Klimchuka447c122021-05-31 11:20:01 +1000100int register_spi_bitbang_master(const struct bitbang_spi_master *master, void *spi_data)
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000101{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000102 struct spi_master mst = spi_master_bitbang;
Nico Hubercb44eb72017-04-22 00:09:42 +0200103 /* If someone forgot to initialize a bitbang function, we catch it here. */
104 if (!master || !master->set_cs ||
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000105 !master->set_sck || !master->set_mosi || !master->get_miso ||
106 (master->request_bus && !master->release_bus) ||
107 (!master->request_bus && master->release_bus)) {
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000108 msg_perr("Incomplete SPI bitbang master setting!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200109 "Please report a bug at flashprog@flashprog.org\n");
110 return ERROR_FLASHPROG_BUG;
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000111 }
112
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +1100113 struct bitbang_spi_master_data *data = calloc(1, sizeof(struct bitbang_spi_master_data));
114 if (!data) {
115 msg_perr("Out of memory!\n");
116 return ERROR_OOM;
117 }
118
119 data->mst = master;
Anastasia Klimchuka447c122021-05-31 11:20:01 +1000120 data->spi_data = spi_data;
Nico Huber89569d62023-01-12 23:31:40 +0100121 register_spi_master(&mst, 0, data);
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +0000122
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000123 /* Only mess with the bus if we're sure nobody else uses it. */
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000124 bitbang_spi_request_bus(master, spi_data);
125 bitbang_spi_set_cs(master, 1, spi_data);
126 bitbang_spi_set_sck_set_mosi(master, 0, 0, spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000127 /* FIXME: Release SPI bus here and request it again for each command or
128 * don't release it now and only release it on programmer shutdown?
129 */
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000130 bitbang_spi_release_bus(master, spi_data);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000131 return 0;
132}
133
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000134static uint8_t bitbang_spi_read_byte(const struct bitbang_spi_master *master, void *spi_data)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000135{
136 uint8_t ret = 0;
137 int i;
138
139 for (i = 7; i >= 0; i--) {
Daniel Thompson455a6fc2018-06-05 09:55:20 +0100140 if (i == 0)
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000141 bitbang_spi_set_sck_set_mosi(master, 0, 0, spi_data);
Daniel Thompson455a6fc2018-06-05 09:55:20 +0100142 else
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000143 bitbang_spi_set_sck(master, 0, spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000144 programmer_delay(master->half_period);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000145 ret <<= 1;
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000146 ret |= bitbang_spi_set_sck_get_miso(master, 1, spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000147 programmer_delay(master->half_period);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000148 }
149 return ret;
150}
151
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000152static void bitbang_spi_write_byte(const struct bitbang_spi_master *master, uint8_t val, void *spi_data)
Daniel Thompson455a6fc2018-06-05 09:55:20 +0100153{
154 int i;
155
156 for (i = 7; i >= 0; i--) {
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000157 bitbang_spi_set_sck_set_mosi(master, 0, (val >> i) & 1, spi_data);
Daniel Thompson455a6fc2018-06-05 09:55:20 +0100158 programmer_delay(master->half_period);
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000159 bitbang_spi_set_sck(master, 1, spi_data);
Daniel Thompson455a6fc2018-06-05 09:55:20 +0100160 programmer_delay(master->half_period);
161 }
162}
163
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000164static int bitbang_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000165 unsigned int writecnt, unsigned int readcnt,
166 const unsigned char *writearr,
167 unsigned char *readarr)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000168{
Nico Huber519be662018-12-23 20:03:35 +0100169 unsigned int i;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100170 const struct bitbang_spi_master_data *data = flash->mst.spi->data;
Edward O'Callaghancbc5ba02021-01-06 14:10:52 +1100171 const struct bitbang_spi_master *master = data->mst;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000172
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000173 /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
174 * Requesting and releasing the SPI bus is handled in here to allow the
175 * programmer to use its own SPI engine for native accesses.
176 */
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000177 bitbang_spi_request_bus(master, data->spi_data);
178 bitbang_spi_set_cs(master, 0, data->spi_data);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000179 for (i = 0; i < writecnt; i++)
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000180 bitbang_spi_write_byte(master, writearr[i], data->spi_data);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000181 for (i = 0; i < readcnt; i++)
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000182 readarr[i] = bitbang_spi_read_byte(master, data->spi_data);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000183
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000184 bitbang_spi_set_sck(master, 0, data->spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000185 programmer_delay(master->half_period);
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000186 bitbang_spi_set_cs(master, 1, data->spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000187 programmer_delay(master->half_period);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000188 /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
Anastasia Klimchuk6e6cce02021-05-26 10:07:59 +1000189 bitbang_spi_release_bus(master, data->spi_data);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000190
191 return 0;
192}