blob: 2f62912d4ea12e2eb78088bc14af48cf5c025380 [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.
hailfingeracce2df2009-09-28 13:15:16 +000014 */
15
16#include <stdio.h>
hailfingeracce2df2009-09-28 13:15:16 +000017#include <string.h>
18#include <stdlib.h>
19#include <ctype.h>
20#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000021#include "programmer.h"
hailfingeracce2df2009-09-28 13:15:16 +000022#include "spi.h"
23
Craig Hesling65eb8812019-08-01 09:33:56 -070024/* Length of half a clock period in usecs. */
25static int bitbang_spi_half_period;
26
27static const struct bitbang_spi_master *bitbang_spi_master = NULL;
28
hailfinger49ff41f2010-07-17 12:54:09 +000029/* Note that CS# is active low, so val=0 means the chip is active. */
Craig Hesling65eb8812019-08-01 09:33:56 -070030static void bitbang_spi_set_cs(int val)
hailfingeracce2df2009-09-28 13:15:16 +000031{
Craig Hesling65eb8812019-08-01 09:33:56 -070032 bitbang_spi_master->set_cs(val);
hailfingeracce2df2009-09-28 13:15:16 +000033}
34
Craig Hesling65eb8812019-08-01 09:33:56 -070035static void bitbang_spi_set_sck(int val)
hailfingeracce2df2009-09-28 13:15:16 +000036{
Craig Hesling65eb8812019-08-01 09:33:56 -070037 bitbang_spi_master->set_sck(val);
hailfingeracce2df2009-09-28 13:15:16 +000038}
39
Craig Hesling65eb8812019-08-01 09:33:56 -070040static void bitbang_spi_set_mosi(int val)
hailfingeracce2df2009-09-28 13:15:16 +000041{
Craig Hesling65eb8812019-08-01 09:33:56 -070042 bitbang_spi_master->set_mosi(val);
hailfingeracce2df2009-09-28 13:15:16 +000043}
44
Craig Hesling65eb8812019-08-01 09:33:56 -070045static int bitbang_spi_get_miso(void)
hailfingeracce2df2009-09-28 13:15:16 +000046{
Craig Hesling65eb8812019-08-01 09:33:56 -070047 return bitbang_spi_master->get_miso();
hailfingeracce2df2009-09-28 13:15:16 +000048}
49
Craig Hesling65eb8812019-08-01 09:33:56 -070050static void bitbang_spi_request_bus(void)
hailfinger12cba9a2010-09-15 00:17:37 +000051{
Craig Hesling65eb8812019-08-01 09:33:56 -070052 if (bitbang_spi_master->request_bus)
53 bitbang_spi_master->request_bus();
hailfinger12cba9a2010-09-15 00:17:37 +000054}
55
Craig Hesling65eb8812019-08-01 09:33:56 -070056static void bitbang_spi_release_bus(void)
hailfinger12cba9a2010-09-15 00:17:37 +000057{
Craig Hesling65eb8812019-08-01 09:33:56 -070058 if (bitbang_spi_master->release_bus)
59 bitbang_spi_master->release_bus();
hailfinger12cba9a2010-09-15 00:17:37 +000060}
61
Souvik Ghoshd75cd672016-06-17 14:21:39 -070062static int bitbang_spi_send_command(const struct flashctx *flash,
63 unsigned int writecnt, unsigned int readcnt,
64 const unsigned char *writearr,
65 unsigned char *readarr);
mkarcherd264e9e2011-05-11 17:07:07 +000066
Patrick Georgif4f1e2f2017-03-10 17:38:40 +010067static const struct spi_master spi_master_bitbang = {
Edward O'Callaghana6673bd2019-06-24 15:22:28 +100068 .features = SPI_MASTER_4BA,
uwe8d342eb2011-07-28 08:13:25 +000069 .max_data_read = MAX_DATA_READ_UNLIMITED,
70 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
71 .command = bitbang_spi_send_command,
72 .multicommand = default_spi_send_multicommand,
73 .read = default_spi_read,
74 .write_256 = default_spi_write_256,
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +110075 .write_aai = default_spi_write_aai,
mkarcherd264e9e2011-05-11 17:07:07 +000076};
77
Craig Hesling65eb8812019-08-01 09:33:56 -070078int register_spi_bitbang_master(const struct bitbang_spi_master *master)
hailfingeracce2df2009-09-28 13:15:16 +000079{
Nico Huber1627df32017-04-22 00:09:42 +020080 /* If someone forgot to initialize a bitbang function, we catch it here. */
81 if (!master || !master->set_cs ||
David Hendricksac1d25c2016-08-09 17:00:58 -070082 !master->set_sck || !master->set_mosi || !master->get_miso) {
hailfinger12cba9a2010-09-15 00:17:37 +000083 msg_perr("Incomplete SPI bitbang master setting!\n"
84 "Please report a bug at flashrom@flashrom.org\n");
Craig Hesling65eb8812019-08-01 09:33:56 -070085 return 1;
86 }
87 if (bitbang_spi_master) {
88 msg_perr("SPI bitbang master already initialized!\n"
89 "Please report a bug at flashrom@flashrom.org\n");
90 return 1;
hailfinger12cba9a2010-09-15 00:17:37 +000091 }
92
Craig Hesling65eb8812019-08-01 09:33:56 -070093 bitbang_spi_master = master;
94 bitbang_spi_half_period = master->half_period;
hailfinger49ff41f2010-07-17 12:54:09 +000095
Craig Hesling65eb8812019-08-01 09:33:56 -070096 register_spi_master(&spi_master_bitbang);
David Hendricksac1d25c2016-08-09 17:00:58 -070097
Craig Hesling65eb8812019-08-01 09:33:56 -070098 /* FIXME: Run bitbang_spi_request_bus here or in programmer init? */
99 bitbang_spi_set_cs(1);
100 bitbang_spi_set_sck(0);
101 bitbang_spi_set_mosi(0);
hailfingeracce2df2009-09-28 13:15:16 +0000102 return 0;
103}
104
Craig Hesling65eb8812019-08-01 09:33:56 -0700105int bitbang_spi_shutdown(const struct bitbang_spi_master *master)
106{
107 if (!bitbang_spi_master) {
108 msg_perr("Shutting down an uninitialized SPI bitbang master!\n"
109 "Please report a bug at flashrom@flashrom.org\n");
110 return 1;
111 }
112 if (master != bitbang_spi_master) {
113 msg_perr("Shutting down a mismatched SPI bitbang master!\n"
114 "Please report a bug at flashrom@flashrom.org\n");
115 return 1;
116 }
117
118 /* FIXME: Run bitbang_spi_release_bus here or per command? */
119 bitbang_spi_master = NULL;
120 return 0;
121}
122
123static uint8_t bitbang_spi_readwrite_byte(uint8_t val)
hailfingeracce2df2009-09-28 13:15:16 +0000124{
125 uint8_t ret = 0;
126 int i;
127
128 for (i = 7; i >= 0; i--) {
Craig Hesling65eb8812019-08-01 09:33:56 -0700129 bitbang_spi_set_mosi((val >> i) & 1);
130 programmer_delay(bitbang_spi_half_period);
131 bitbang_spi_set_sck(1);
hailfingeracce2df2009-09-28 13:15:16 +0000132 ret <<= 1;
Craig Hesling65eb8812019-08-01 09:33:56 -0700133 ret |= bitbang_spi_get_miso();
134 programmer_delay(bitbang_spi_half_period);
135 bitbang_spi_set_sck(0);
hailfingeracce2df2009-09-28 13:15:16 +0000136 }
137 return ret;
138}
139
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700140static int bitbang_spi_send_command(const struct flashctx *flash,
141 unsigned int writecnt, unsigned int readcnt,
142 const unsigned char *writearr,
143 unsigned char *readarr)
hailfingeracce2df2009-09-28 13:15:16 +0000144{
hailfingeracce2df2009-09-28 13:15:16 +0000145 int i;
146
hailfinger12cba9a2010-09-15 00:17:37 +0000147 /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
148 * Requesting and releasing the SPI bus is handled in here to allow the
149 * programmer to use its own SPI engine for native accesses.
150 */
Craig Hesling65eb8812019-08-01 09:33:56 -0700151 bitbang_spi_request_bus();
152 bitbang_spi_set_cs(0);
mkarcher6b511512010-07-17 10:42:34 +0000153 for (i = 0; i < writecnt; i++)
Craig Hesling65eb8812019-08-01 09:33:56 -0700154 bitbang_spi_readwrite_byte(writearr[i]);
mkarcher6b511512010-07-17 10:42:34 +0000155 for (i = 0; i < readcnt; i++)
Craig Hesling65eb8812019-08-01 09:33:56 -0700156 readarr[i] = bitbang_spi_readwrite_byte(0);
mkarcher6b511512010-07-17 10:42:34 +0000157
Craig Hesling65eb8812019-08-01 09:33:56 -0700158 programmer_delay(bitbang_spi_half_period);
159 bitbang_spi_set_cs(1);
160 programmer_delay(bitbang_spi_half_period);
hailfinger12cba9a2010-09-15 00:17:37 +0000161 /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
Craig Hesling65eb8812019-08-01 09:33:56 -0700162 bitbang_spi_release_bus();
hailfingeracce2df2009-09-28 13:15:16 +0000163
164 return 0;
165}