blob: b12d81acb12dfabedc26c8c77c2777602261411c [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"
23
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000024/* Note that CS# is active low, so val=0 means the chip is active. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000025static void bitbang_spi_set_cs(const struct bitbang_spi_master * const master, int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000026{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000027 master->set_cs(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000028}
29
Stefan Tauner67d163d2013-01-15 17:37:48 +000030static void bitbang_spi_set_sck(const struct bitbang_spi_master * const master, int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000031{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000032 master->set_sck(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000033}
34
Stefan Tauner67d163d2013-01-15 17:37:48 +000035static void bitbang_spi_request_bus(const struct bitbang_spi_master * const master)
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000036{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000037 if (master->request_bus)
38 master->request_bus();
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000039}
40
Stefan Tauner67d163d2013-01-15 17:37:48 +000041static void bitbang_spi_release_bus(const struct bitbang_spi_master * const master)
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000042{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000043 if (master->release_bus)
44 master->release_bus();
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000045}
46
Daniel Thompsonb623f402018-06-05 09:38:19 +010047static 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
58static 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
Daniel Thompson455a6fc2018-06-05 09:55:20 +010067static uint8_t bitbang_spi_read_byte(const struct bitbang_spi_master *master)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000068{
69 uint8_t ret = 0;
70 int i;
71
72 for (i = 7; i >= 0; i--) {
Daniel Thompson455a6fc2018-06-05 09:55:20 +010073 if (i == 0)
74 bitbang_spi_set_sck_set_mosi(master, 0, 0);
75 else
76 bitbang_spi_set_sck(master, 0);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000077 programmer_delay(master->half_period);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000078 ret <<= 1;
Daniel Thompsonb623f402018-06-05 09:38:19 +010079 ret |= bitbang_spi_set_sck_get_miso(master, 1);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000080 programmer_delay(master->half_period);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000081 }
82 return ret;
83}
84
Daniel Thompson455a6fc2018-06-05 09:55:20 +010085static void bitbang_spi_write_byte(const struct bitbang_spi_master *master, uint8_t val)
86{
87 int i;
88
89 for (i = 7; i >= 0; i--) {
90 bitbang_spi_set_sck_set_mosi(master, 0, (val >> i) & 1);
91 programmer_delay(master->half_period);
92 bitbang_spi_set_sck(master, 1);
93 programmer_delay(master->half_period);
94 }
95}
96
Edward O'Callaghanec942502021-01-06 14:10:52 +110097struct bitbang_spi_master_data {
98 const struct bitbang_spi_master *mst;
99};
100
Edward O'Callaghane4ddc362020-04-12 17:27:53 +1000101static int bitbang_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000102 unsigned int writecnt, unsigned int readcnt,
103 const unsigned char *writearr,
104 unsigned char *readarr)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000105{
Nico Huber519be662018-12-23 20:03:35 +0100106 unsigned int i;
Edward O'Callaghanec942502021-01-06 14:10:52 +1100107 const struct bitbang_spi_master_data *data = flash->mst->spi.data;
108 const struct bitbang_spi_master *master = data->mst;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000109
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000110 /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
111 * Requesting and releasing the SPI bus is handled in here to allow the
112 * programmer to use its own SPI engine for native accesses.
113 */
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000114 bitbang_spi_request_bus(master);
115 bitbang_spi_set_cs(master, 0);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000116 for (i = 0; i < writecnt; i++)
Daniel Thompson455a6fc2018-06-05 09:55:20 +0100117 bitbang_spi_write_byte(master, writearr[i]);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000118 for (i = 0; i < readcnt; i++)
Daniel Thompson455a6fc2018-06-05 09:55:20 +0100119 readarr[i] = bitbang_spi_read_byte(master);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000120
Daniel Thompsonb623f402018-06-05 09:38:19 +0100121 bitbang_spi_set_sck(master, 0);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000122 programmer_delay(master->half_period);
123 bitbang_spi_set_cs(master, 1);
124 programmer_delay(master->half_period);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000125 /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000126 bitbang_spi_release_bus(master);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000127
128 return 0;
129}
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100130
131static const struct spi_master spi_master_bitbang = {
132 .features = SPI_MASTER_4BA,
133 .max_data_read = MAX_DATA_READ_UNLIMITED,
134 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
135 .command = bitbang_spi_send_command,
136 .multicommand = default_spi_send_multicommand,
137 .read = default_spi_read,
138 .write_256 = default_spi_write_256,
139 .write_aai = default_spi_write_aai,
140};
141
Edward O'Callaghanec942502021-01-06 14:10:52 +1100142static int bitbang_spi_shutdown(void *data)
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100143{
144 /* FIXME: Run bitbang_spi_release_bus here or per command? */
Edward O'Callaghanec942502021-01-06 14:10:52 +1100145 free(data);
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100146 return 0;
147}
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100148
149int register_spi_bitbang_master(const struct bitbang_spi_master *master)
150{
151 struct spi_master mst = spi_master_bitbang;
152 /* If someone forgot to initialize a bitbang function, we catch it here. */
153 if (!master || !master->set_cs ||
154 !master->set_sck || !master->set_mosi || !master->get_miso ||
155 (master->request_bus && !master->release_bus) ||
156 (!master->request_bus && master->release_bus)) {
157 msg_perr("Incomplete SPI bitbang master setting!\n"
158 "Please report a bug at flashrom@flashrom.org\n");
159 return ERROR_FLASHROM_BUG;
160 }
161
Edward O'Callaghanec942502021-01-06 14:10:52 +1100162 struct bitbang_spi_master_data *data = calloc(1, sizeof(struct bitbang_spi_master_data));
163 data->mst = master;
164 mst.data = data;
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100165 register_spi_master(&mst);
Edward O'Callaghanec942502021-01-06 14:10:52 +1100166 register_shutdown(bitbang_spi_shutdown, data);
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100167
168 /* Only mess with the bus if we're sure nobody else uses it. */
169 bitbang_spi_request_bus(master);
170 bitbang_spi_set_cs(master, 1);
171 bitbang_spi_set_sck_set_mosi(master, 0, 0);
172 /* FIXME: Release SPI bus here and request it again for each command or
173 * don't release it now and only release it on programmer shutdown?
174 */
175 bitbang_spi_release_bus(master);
176 return 0;
177}