blob: a441db5caad4cf6da8548c5b1384a599b27172cd [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,
mkarcherd264e9e2011-05-11 17:07:07 +000075};
76
Craig Hesling65eb8812019-08-01 09:33:56 -070077int register_spi_bitbang_master(const struct bitbang_spi_master *master)
hailfingeracce2df2009-09-28 13:15:16 +000078{
Nico Huber1627df32017-04-22 00:09:42 +020079 /* If someone forgot to initialize a bitbang function, we catch it here. */
80 if (!master || !master->set_cs ||
David Hendricksac1d25c2016-08-09 17:00:58 -070081 !master->set_sck || !master->set_mosi || !master->get_miso) {
hailfinger12cba9a2010-09-15 00:17:37 +000082 msg_perr("Incomplete SPI bitbang master setting!\n"
83 "Please report a bug at flashrom@flashrom.org\n");
Craig Hesling65eb8812019-08-01 09:33:56 -070084 return 1;
85 }
86 if (bitbang_spi_master) {
87 msg_perr("SPI bitbang master already initialized!\n"
88 "Please report a bug at flashrom@flashrom.org\n");
89 return 1;
hailfinger12cba9a2010-09-15 00:17:37 +000090 }
91
Craig Hesling65eb8812019-08-01 09:33:56 -070092 bitbang_spi_master = master;
93 bitbang_spi_half_period = master->half_period;
hailfinger49ff41f2010-07-17 12:54:09 +000094
Craig Hesling65eb8812019-08-01 09:33:56 -070095 register_spi_master(&spi_master_bitbang);
David Hendricksac1d25c2016-08-09 17:00:58 -070096
Craig Hesling65eb8812019-08-01 09:33:56 -070097 /* FIXME: Run bitbang_spi_request_bus here or in programmer init? */
98 bitbang_spi_set_cs(1);
99 bitbang_spi_set_sck(0);
100 bitbang_spi_set_mosi(0);
hailfingeracce2df2009-09-28 13:15:16 +0000101 return 0;
102}
103
Craig Hesling65eb8812019-08-01 09:33:56 -0700104int bitbang_spi_shutdown(const struct bitbang_spi_master *master)
105{
106 if (!bitbang_spi_master) {
107 msg_perr("Shutting down an uninitialized SPI bitbang master!\n"
108 "Please report a bug at flashrom@flashrom.org\n");
109 return 1;
110 }
111 if (master != bitbang_spi_master) {
112 msg_perr("Shutting down a mismatched SPI bitbang master!\n"
113 "Please report a bug at flashrom@flashrom.org\n");
114 return 1;
115 }
116
117 /* FIXME: Run bitbang_spi_release_bus here or per command? */
118 bitbang_spi_master = NULL;
119 return 0;
120}
121
122static uint8_t bitbang_spi_readwrite_byte(uint8_t val)
hailfingeracce2df2009-09-28 13:15:16 +0000123{
124 uint8_t ret = 0;
125 int i;
126
127 for (i = 7; i >= 0; i--) {
Craig Hesling65eb8812019-08-01 09:33:56 -0700128 bitbang_spi_set_mosi((val >> i) & 1);
129 programmer_delay(bitbang_spi_half_period);
130 bitbang_spi_set_sck(1);
hailfingeracce2df2009-09-28 13:15:16 +0000131 ret <<= 1;
Craig Hesling65eb8812019-08-01 09:33:56 -0700132 ret |= bitbang_spi_get_miso();
133 programmer_delay(bitbang_spi_half_period);
134 bitbang_spi_set_sck(0);
hailfingeracce2df2009-09-28 13:15:16 +0000135 }
136 return ret;
137}
138
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700139static int bitbang_spi_send_command(const struct flashctx *flash,
140 unsigned int writecnt, unsigned int readcnt,
141 const unsigned char *writearr,
142 unsigned char *readarr)
hailfingeracce2df2009-09-28 13:15:16 +0000143{
hailfingeracce2df2009-09-28 13:15:16 +0000144 int i;
145
hailfinger12cba9a2010-09-15 00:17:37 +0000146 /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
147 * Requesting and releasing the SPI bus is handled in here to allow the
148 * programmer to use its own SPI engine for native accesses.
149 */
Craig Hesling65eb8812019-08-01 09:33:56 -0700150 bitbang_spi_request_bus();
151 bitbang_spi_set_cs(0);
mkarcher6b511512010-07-17 10:42:34 +0000152 for (i = 0; i < writecnt; i++)
Craig Hesling65eb8812019-08-01 09:33:56 -0700153 bitbang_spi_readwrite_byte(writearr[i]);
mkarcher6b511512010-07-17 10:42:34 +0000154 for (i = 0; i < readcnt; i++)
Craig Hesling65eb8812019-08-01 09:33:56 -0700155 readarr[i] = bitbang_spi_readwrite_byte(0);
mkarcher6b511512010-07-17 10:42:34 +0000156
Craig Hesling65eb8812019-08-01 09:33:56 -0700157 programmer_delay(bitbang_spi_half_period);
158 bitbang_spi_set_cs(1);
159 programmer_delay(bitbang_spi_half_period);
hailfinger12cba9a2010-09-15 00:17:37 +0000160 /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
Craig Hesling65eb8812019-08-01 09:33:56 -0700161 bitbang_spi_release_bus();
hailfingeracce2df2009-09-28 13:15:16 +0000162
163 return 0;
164}