blob: 3d973c587c79b24a263613368242e4c907b37626 [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. */
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100025static 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 +000026{
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100027 master->set_cs(val, spi_data);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000028}
29
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100030static 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 +000031{
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100032 master->set_sck(val, spi_data);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000033}
34
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100035static void bitbang_spi_request_bus(const struct bitbang_spi_master * const master, void *spi_data)
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000036{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000037 if (master->request_bus)
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100038 master->request_bus(spi_data);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000039}
40
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100041static void bitbang_spi_release_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->release_bus)
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100044 master->release_bus(spi_data);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000045}
46
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100047static void bitbang_spi_set_sck_set_mosi(const struct bitbang_spi_master * const master, int sck, int mosi,
48 void *spi_data)
Daniel Thompsonb623f402018-06-05 09:38:19 +010049{
50 if (master->set_sck_set_mosi) {
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100051 master->set_sck_set_mosi(sck, mosi, spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +010052 return;
53 }
54
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100055 master->set_sck(sck, spi_data);
56 master->set_mosi(mosi, spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +010057}
58
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100059static int bitbang_spi_set_sck_get_miso(const struct bitbang_spi_master * const master, int sck,
60 void *spi_data)
Daniel Thompsonb623f402018-06-05 09:38:19 +010061{
62 if (master->set_sck_get_miso)
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100063 return master->set_sck_get_miso(sck, spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +010064
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100065 master->set_sck(sck, spi_data);
66 return master->get_miso(spi_data);
Daniel Thompsonb623f402018-06-05 09:38:19 +010067}
68
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100069static uint8_t bitbang_spi_read_byte(const struct bitbang_spi_master *master, void *spi_data)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000070{
71 uint8_t ret = 0;
72 int i;
73
74 for (i = 7; i >= 0; i--) {
Daniel Thompson455a6fc2018-06-05 09:55:20 +010075 if (i == 0)
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100076 bitbang_spi_set_sck_set_mosi(master, 0, 0, spi_data);
Daniel Thompson455a6fc2018-06-05 09:55:20 +010077 else
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100078 bitbang_spi_set_sck(master, 0, spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000079 programmer_delay(master->half_period);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000080 ret <<= 1;
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100081 ret |= bitbang_spi_set_sck_get_miso(master, 1, spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000082 programmer_delay(master->half_period);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000083 }
84 return ret;
85}
86
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100087static void bitbang_spi_write_byte(const struct bitbang_spi_master *master, uint8_t val, void *spi_data)
Daniel Thompson455a6fc2018-06-05 09:55:20 +010088{
89 int i;
90
91 for (i = 7; i >= 0; i--) {
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100092 bitbang_spi_set_sck_set_mosi(master, 0, (val >> i) & 1, spi_data);
Daniel Thompson455a6fc2018-06-05 09:55:20 +010093 programmer_delay(master->half_period);
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +100094 bitbang_spi_set_sck(master, 1, spi_data);
Daniel Thompson455a6fc2018-06-05 09:55:20 +010095 programmer_delay(master->half_period);
96 }
97}
98
Edward O'Callaghanec942502021-01-06 14:10:52 +110099struct bitbang_spi_master_data {
Anastasia Klimchuk9055ef92021-05-26 08:48:24 +1000100 const struct bitbang_spi_master *master;
Anastasia Klimchuk30815fc2021-05-31 11:20:01 +1000101 void *spi_data;
Edward O'Callaghanec942502021-01-06 14:10:52 +1100102};
103
Edward O'Callaghane4ddc362020-04-12 17:27:53 +1000104static int bitbang_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000105 unsigned int writecnt, unsigned int readcnt,
106 const unsigned char *writearr,
107 unsigned char *readarr)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000108{
Nico Huber519be662018-12-23 20:03:35 +0100109 unsigned int i;
Edward O'Callaghanec942502021-01-06 14:10:52 +1100110 const struct bitbang_spi_master_data *data = flash->mst->spi.data;
Anastasia Klimchuk9055ef92021-05-26 08:48:24 +1000111 const struct bitbang_spi_master *master = data->master;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000112
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000113 /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
114 * Requesting and releasing the SPI bus is handled in here to allow the
115 * programmer to use its own SPI engine for native accesses.
116 */
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +1000117 bitbang_spi_request_bus(master, data->spi_data);
118 bitbang_spi_set_cs(master, 0, data->spi_data);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000119 for (i = 0; i < writecnt; i++)
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +1000120 bitbang_spi_write_byte(master, writearr[i], data->spi_data);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000121 for (i = 0; i < readcnt; i++)
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +1000122 readarr[i] = bitbang_spi_read_byte(master, data->spi_data);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000123
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +1000124 bitbang_spi_set_sck(master, 0, data->spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000125 programmer_delay(master->half_period);
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +1000126 bitbang_spi_set_cs(master, 1, data->spi_data);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000127 programmer_delay(master->half_period);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000128 /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +1000129 bitbang_spi_release_bus(master, data->spi_data);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000130
131 return 0;
132}
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100133
134static const struct spi_master spi_master_bitbang = {
135 .features = SPI_MASTER_4BA,
136 .max_data_read = MAX_DATA_READ_UNLIMITED,
137 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
138 .command = bitbang_spi_send_command,
139 .multicommand = default_spi_send_multicommand,
140 .read = default_spi_read,
141 .write_256 = default_spi_write_256,
142 .write_aai = default_spi_write_aai,
143};
144
Edward O'Callaghanec942502021-01-06 14:10:52 +1100145static int bitbang_spi_shutdown(void *data)
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100146{
147 /* FIXME: Run bitbang_spi_release_bus here or per command? */
Edward O'Callaghanec942502021-01-06 14:10:52 +1100148 free(data);
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100149 return 0;
150}
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100151
Anastasia Klimchuk30815fc2021-05-31 11:20:01 +1000152int register_spi_bitbang_master(const struct bitbang_spi_master *master, void *spi_data)
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100153{
154 struct spi_master mst = spi_master_bitbang;
155 /* If someone forgot to initialize a bitbang function, we catch it here. */
156 if (!master || !master->set_cs ||
157 !master->set_sck || !master->set_mosi || !master->get_miso ||
158 (master->request_bus && !master->release_bus) ||
159 (!master->request_bus && master->release_bus)) {
160 msg_perr("Incomplete SPI bitbang master setting!\n"
161 "Please report a bug at flashrom@flashrom.org\n");
162 return ERROR_FLASHROM_BUG;
163 }
164
Angel Pons3bd47522021-06-07 12:33:53 +0200165 struct bitbang_spi_master_data *data = calloc(1, sizeof(*data));
Anastasia Klimchuk9055ef92021-05-26 08:48:24 +1000166 data->master = master;
Anastasia Klimchuk30815fc2021-05-31 11:20:01 +1000167 if (spi_data)
168 data->spi_data = spi_data;
169
Anastasia Klimchuk0bc0ab92021-05-31 11:15:59 +1000170 register_spi_master(&mst, data);
Edward O'Callaghanec942502021-01-06 14:10:52 +1100171 register_shutdown(bitbang_spi_shutdown, data);
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100172
173 /* Only mess with the bus if we're sure nobody else uses it. */
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +1000174 bitbang_spi_request_bus(master, spi_data);
175 bitbang_spi_set_cs(master, 1, spi_data);
176 bitbang_spi_set_sck_set_mosi(master, 0, 0, spi_data);
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100177 /* FIXME: Release SPI bus here and request it again for each command or
178 * don't release it now and only release it on programmer shutdown?
179 */
Anastasia Klimchuk848bf9d2021-05-26 10:07:59 +1000180 bitbang_spi_release_bus(master, spi_data);
Edward O'Callaghan36e5bd32020-11-17 19:19:57 +1100181 return 0;
182}