blob: 5af2370d534bbbce706f0758c30dfe65be33a961 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <stdio.h>
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000021#include <string.h>
22#include <stdlib.h>
23#include <ctype.h>
24#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000025#include "programmer.h"
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000026#include "spi.h"
27
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000028/* Note that CS# is active low, so val=0 means the chip is active. */
Stefan Tauner67d163d2013-01-15 17:37:48 +000029static void bitbang_spi_set_cs(const struct bitbang_spi_master * const master, int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000030{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000031 master->set_cs(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000032}
33
Stefan Tauner67d163d2013-01-15 17:37:48 +000034static void bitbang_spi_set_sck(const struct bitbang_spi_master * const master, int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000035{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000036 master->set_sck(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000037}
38
Stefan Tauner67d163d2013-01-15 17:37:48 +000039static void bitbang_spi_set_mosi(const struct bitbang_spi_master * const master, int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000040{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000041 master->set_mosi(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000042}
43
Stefan Tauner67d163d2013-01-15 17:37:48 +000044static int bitbang_spi_get_miso(const struct bitbang_spi_master * const master)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000045{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000046 return master->get_miso();
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000047}
48
Stefan Tauner67d163d2013-01-15 17:37:48 +000049static void bitbang_spi_request_bus(const struct bitbang_spi_master * const master)
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000050{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000051 if (master->request_bus)
52 master->request_bus();
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000053}
54
Stefan Tauner67d163d2013-01-15 17:37:48 +000055static void bitbang_spi_release_bus(const struct bitbang_spi_master * const master)
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000056{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000057 if (master->release_bus)
58 master->release_bus();
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000059}
60
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000061static int bitbang_spi_send_command(struct flashctx *flash,
62 unsigned int writecnt, unsigned int readcnt,
63 const unsigned char *writearr,
64 unsigned char *readarr);
Michael Karcherb9dbe482011-05-11 17:07:07 +000065
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000066static const struct spi_master spi_master_bitbang = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +000067 .type = SPI_CONTROLLER_BITBANG,
Nico Huber1cf407b2017-11-10 20:18:23 +010068 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-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,
Nico Huber7bca1262012-06-15 22:28:12 +000075 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +000076};
77
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000078#if 0 // until it is needed
79static int bitbang_spi_shutdown(const struct bitbang_spi_master *master)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000080{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000081 /* FIXME: Run bitbang_spi_release_bus here or per command? */
82 return 0;
83}
84#endif
85
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000086int register_spi_bitbang_master(const struct bitbang_spi_master *master)
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000087{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000088 struct spi_master mst = spi_master_bitbang;
Carl-Daniel Hailfinger17e23ac2010-07-18 14:42:28 +000089 /* BITBANG_SPI_INVALID is 0, so if someone forgot to initialize ->type,
90 * we catch it here. Same goes for missing initialization of bitbanging
91 * functions.
92 */
93 if (!master || master->type == BITBANG_SPI_INVALID || !master->set_cs ||
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000094 !master->set_sck || !master->set_mosi || !master->get_miso ||
95 (master->request_bus && !master->release_bus) ||
96 (!master->request_bus && master->release_bus)) {
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000097 msg_perr("Incomplete SPI bitbang master setting!\n"
98 "Please report a bug at flashrom@flashrom.org\n");
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000099 return ERROR_FLASHROM_BUG;
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000100 }
101
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000102 mst.data = master;
103 register_spi_master(&mst);
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +0000104
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000105 /* Only mess with the bus if we're sure nobody else uses it. */
106 bitbang_spi_request_bus(master);
107 bitbang_spi_set_cs(master, 1);
108 bitbang_spi_set_sck(master, 0);
109 bitbang_spi_set_mosi(master, 0);
110 /* FIXME: Release SPI bus here and request it again for each command or
111 * don't release it now and only release it on programmer shutdown?
112 */
113 bitbang_spi_release_bus(master);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000114 return 0;
115}
116
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000117static uint8_t bitbang_spi_rw_byte(const struct bitbang_spi_master *master,
118 uint8_t val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000119{
120 uint8_t ret = 0;
121 int i;
122
123 for (i = 7; i >= 0; i--) {
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000124 bitbang_spi_set_mosi(master, (val >> i) & 1);
125 programmer_delay(master->half_period);
126 bitbang_spi_set_sck(master, 1);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000127 ret <<= 1;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000128 ret |= bitbang_spi_get_miso(master);
129 programmer_delay(master->half_period);
130 bitbang_spi_set_sck(master, 0);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000131 }
132 return ret;
133}
134
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000135static int bitbang_spi_send_command(struct flashctx *flash,
136 unsigned int writecnt, unsigned int readcnt,
137 const unsigned char *writearr,
138 unsigned char *readarr)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000139{
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000140 int i;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000141 const struct bitbang_spi_master *master = flash->mst->spi.data;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000142
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000143 /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
144 * Requesting and releasing the SPI bus is handled in here to allow the
145 * programmer to use its own SPI engine for native accesses.
146 */
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000147 bitbang_spi_request_bus(master);
148 bitbang_spi_set_cs(master, 0);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000149 for (i = 0; i < writecnt; i++)
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000150 bitbang_spi_rw_byte(master, writearr[i]);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000151 for (i = 0; i < readcnt; i++)
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000152 readarr[i] = bitbang_spi_rw_byte(master, 0);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000153
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000154 programmer_delay(master->half_period);
155 bitbang_spi_set_cs(master, 1);
156 programmer_delay(master->half_period);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000157 /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000158 bitbang_spi_release_bus(master);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000159
160 return 0;
161}