blob: f84a6711ff42f7d3294764d7a41db5c3757f7606 [file] [log] [blame]
hailfingeracce2df2009-09-28 13:15:16 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfinger39d159a2010-05-21 23:09:42 +00004 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
hailfingeracce2df2009-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.
14 *
hailfingeracce2df2009-09-28 13:15:16 +000015 */
16
17#include <stdio.h>
hailfingeracce2df2009-09-28 13:15:16 +000018#include <string.h>
19#include <stdlib.h>
20#include <ctype.h>
21#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000022#include "programmer.h"
hailfingeracce2df2009-09-28 13:15:16 +000023#include "spi.h"
24
Craig Hesling65eb8812019-08-01 09:33:56 -070025/* Length of half a clock period in usecs. */
26static int bitbang_spi_half_period;
27
28static const struct bitbang_spi_master *bitbang_spi_master = NULL;
29
hailfinger49ff41f2010-07-17 12:54:09 +000030/* Note that CS# is active low, so val=0 means the chip is active. */
Craig Hesling65eb8812019-08-01 09:33:56 -070031static void bitbang_spi_set_cs(int val)
hailfingeracce2df2009-09-28 13:15:16 +000032{
Craig Hesling65eb8812019-08-01 09:33:56 -070033 bitbang_spi_master->set_cs(val);
hailfingeracce2df2009-09-28 13:15:16 +000034}
35
Craig Hesling65eb8812019-08-01 09:33:56 -070036static void bitbang_spi_set_sck(int val)
hailfingeracce2df2009-09-28 13:15:16 +000037{
Craig Hesling65eb8812019-08-01 09:33:56 -070038 bitbang_spi_master->set_sck(val);
hailfingeracce2df2009-09-28 13:15:16 +000039}
40
Craig Hesling65eb8812019-08-01 09:33:56 -070041static void bitbang_spi_set_mosi(int val)
hailfingeracce2df2009-09-28 13:15:16 +000042{
Craig Hesling65eb8812019-08-01 09:33:56 -070043 bitbang_spi_master->set_mosi(val);
hailfingeracce2df2009-09-28 13:15:16 +000044}
45
Craig Hesling65eb8812019-08-01 09:33:56 -070046static int bitbang_spi_get_miso(void)
hailfingeracce2df2009-09-28 13:15:16 +000047{
Craig Hesling65eb8812019-08-01 09:33:56 -070048 return bitbang_spi_master->get_miso();
hailfingeracce2df2009-09-28 13:15:16 +000049}
50
Craig Hesling65eb8812019-08-01 09:33:56 -070051static void bitbang_spi_request_bus(void)
hailfinger12cba9a2010-09-15 00:17:37 +000052{
Craig Hesling65eb8812019-08-01 09:33:56 -070053 if (bitbang_spi_master->request_bus)
54 bitbang_spi_master->request_bus();
hailfinger12cba9a2010-09-15 00:17:37 +000055}
56
Craig Hesling65eb8812019-08-01 09:33:56 -070057static void bitbang_spi_release_bus(void)
hailfinger12cba9a2010-09-15 00:17:37 +000058{
Craig Hesling65eb8812019-08-01 09:33:56 -070059 if (bitbang_spi_master->release_bus)
60 bitbang_spi_master->release_bus();
hailfinger12cba9a2010-09-15 00:17:37 +000061}
62
Souvik Ghoshd75cd672016-06-17 14:21:39 -070063static int bitbang_spi_send_command(const struct flashctx *flash,
64 unsigned int writecnt, unsigned int readcnt,
65 const unsigned char *writearr,
66 unsigned char *readarr);
mkarcherd264e9e2011-05-11 17:07:07 +000067
Patrick Georgif4f1e2f2017-03-10 17:38:40 +010068static const struct spi_master spi_master_bitbang = {
uwe8d342eb2011-07-28 08:13:25 +000069 .type = SPI_CONTROLLER_BITBANG,
Edward O'Callaghana6673bd2019-06-24 15:22:28 +100070 .features = SPI_MASTER_4BA,
uwe8d342eb2011-07-28 08:13:25 +000071 .max_data_read = MAX_DATA_READ_UNLIMITED,
72 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
73 .command = bitbang_spi_send_command,
74 .multicommand = default_spi_send_multicommand,
75 .read = default_spi_read,
76 .write_256 = default_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +000077};
78
Craig Hesling65eb8812019-08-01 09:33:56 -070079int register_spi_bitbang_master(const struct bitbang_spi_master *master)
hailfingeracce2df2009-09-28 13:15:16 +000080{
hailfingerfddbeb62010-07-18 14:42:28 +000081 /* BITBANG_SPI_INVALID is 0, so if someone forgot to initialize ->type,
82 * we catch it here. Same goes for missing initialization of bitbanging
83 * functions.
84 */
85 if (!master || master->type == BITBANG_SPI_INVALID || !master->set_cs ||
David Hendricksac1d25c2016-08-09 17:00:58 -070086 !master->set_sck || !master->set_mosi || !master->get_miso) {
hailfinger12cba9a2010-09-15 00:17:37 +000087 msg_perr("Incomplete SPI bitbang master setting!\n"
88 "Please report a bug at flashrom@flashrom.org\n");
Craig Hesling65eb8812019-08-01 09:33:56 -070089 return 1;
90 }
91 if (bitbang_spi_master) {
92 msg_perr("SPI bitbang master already initialized!\n"
93 "Please report a bug at flashrom@flashrom.org\n");
94 return 1;
hailfinger12cba9a2010-09-15 00:17:37 +000095 }
96
Craig Hesling65eb8812019-08-01 09:33:56 -070097 bitbang_spi_master = master;
98 bitbang_spi_half_period = master->half_period;
hailfinger49ff41f2010-07-17 12:54:09 +000099
Craig Hesling65eb8812019-08-01 09:33:56 -0700100 register_spi_master(&spi_master_bitbang);
David Hendricksac1d25c2016-08-09 17:00:58 -0700101
Craig Hesling65eb8812019-08-01 09:33:56 -0700102 /* FIXME: Run bitbang_spi_request_bus here or in programmer init? */
103 bitbang_spi_set_cs(1);
104 bitbang_spi_set_sck(0);
105 bitbang_spi_set_mosi(0);
hailfingeracce2df2009-09-28 13:15:16 +0000106 return 0;
107}
108
Craig Hesling65eb8812019-08-01 09:33:56 -0700109int bitbang_spi_shutdown(const struct bitbang_spi_master *master)
110{
111 if (!bitbang_spi_master) {
112 msg_perr("Shutting down an uninitialized SPI bitbang master!\n"
113 "Please report a bug at flashrom@flashrom.org\n");
114 return 1;
115 }
116 if (master != bitbang_spi_master) {
117 msg_perr("Shutting down a mismatched SPI bitbang master!\n"
118 "Please report a bug at flashrom@flashrom.org\n");
119 return 1;
120 }
121
122 /* FIXME: Run bitbang_spi_release_bus here or per command? */
123 bitbang_spi_master = NULL;
124 return 0;
125}
126
127static uint8_t bitbang_spi_readwrite_byte(uint8_t val)
hailfingeracce2df2009-09-28 13:15:16 +0000128{
129 uint8_t ret = 0;
130 int i;
131
132 for (i = 7; i >= 0; i--) {
Craig Hesling65eb8812019-08-01 09:33:56 -0700133 bitbang_spi_set_mosi((val >> i) & 1);
134 programmer_delay(bitbang_spi_half_period);
135 bitbang_spi_set_sck(1);
hailfingeracce2df2009-09-28 13:15:16 +0000136 ret <<= 1;
Craig Hesling65eb8812019-08-01 09:33:56 -0700137 ret |= bitbang_spi_get_miso();
138 programmer_delay(bitbang_spi_half_period);
139 bitbang_spi_set_sck(0);
hailfingeracce2df2009-09-28 13:15:16 +0000140 }
141 return ret;
142}
143
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700144static int bitbang_spi_send_command(const struct flashctx *flash,
145 unsigned int writecnt, unsigned int readcnt,
146 const unsigned char *writearr,
147 unsigned char *readarr)
hailfingeracce2df2009-09-28 13:15:16 +0000148{
hailfingeracce2df2009-09-28 13:15:16 +0000149 int i;
150
hailfinger12cba9a2010-09-15 00:17:37 +0000151 /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
152 * Requesting and releasing the SPI bus is handled in here to allow the
153 * programmer to use its own SPI engine for native accesses.
154 */
Craig Hesling65eb8812019-08-01 09:33:56 -0700155 bitbang_spi_request_bus();
156 bitbang_spi_set_cs(0);
mkarcher6b511512010-07-17 10:42:34 +0000157 for (i = 0; i < writecnt; i++)
Craig Hesling65eb8812019-08-01 09:33:56 -0700158 bitbang_spi_readwrite_byte(writearr[i]);
mkarcher6b511512010-07-17 10:42:34 +0000159 for (i = 0; i < readcnt; i++)
Craig Hesling65eb8812019-08-01 09:33:56 -0700160 readarr[i] = bitbang_spi_readwrite_byte(0);
mkarcher6b511512010-07-17 10:42:34 +0000161
Craig Hesling65eb8812019-08-01 09:33:56 -0700162 programmer_delay(bitbang_spi_half_period);
163 bitbang_spi_set_cs(1);
164 programmer_delay(bitbang_spi_half_period);
hailfinger12cba9a2010-09-15 00:17:37 +0000165 /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
Craig Hesling65eb8812019-08-01 09:33:56 -0700166 bitbang_spi_release_bus();
hailfingeracce2df2009-09-28 13:15:16 +0000167
168 return 0;
169}