blob: ac50e67d488eab2287d5c65e62e97888f96e0fe7 [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
hailfinger49ff41f2010-07-17 12:54:09 +000024/* Note that CS# is active low, so val=0 means the chip is active. */
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110025static void bitbang_spi_set_cs(const struct bitbang_spi_master * const master, int val)
hailfingeracce2df2009-09-28 13:15:16 +000026{
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110027 master->set_cs(val);
hailfingeracce2df2009-09-28 13:15:16 +000028}
29
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110030static void bitbang_spi_set_sck(const struct bitbang_spi_master * const master, int val)
hailfingeracce2df2009-09-28 13:15:16 +000031{
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110032 master->set_sck(val);
hailfingeracce2df2009-09-28 13:15:16 +000033}
34
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110035static void bitbang_spi_request_bus(const struct bitbang_spi_master * const master)
hailfingeracce2df2009-09-28 13:15:16 +000036{
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110037 if (master->request_bus)
38 master->request_bus();
hailfingeracce2df2009-09-28 13:15:16 +000039}
40
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110041static void bitbang_spi_release_bus(const struct bitbang_spi_master * const master)
hailfingeracce2df2009-09-28 13:15:16 +000042{
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110043 if (master->release_bus)
44 master->release_bus();
hailfingeracce2df2009-09-28 13:15:16 +000045}
46
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110047static void bitbang_spi_set_sck_set_mosi(const struct bitbang_spi_master * const master, int sck, int mosi)
hailfinger12cba9a2010-09-15 00:17:37 +000048{
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110049 if (master->set_sck_set_mosi) {
50 master->set_sck_set_mosi(sck, mosi);
51 return;
52 }
53
54 master->set_sck(sck);
55 master->set_mosi(mosi);
hailfinger12cba9a2010-09-15 00:17:37 +000056}
57
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110058static int bitbang_spi_set_sck_get_miso(const struct bitbang_spi_master * const master, int sck)
hailfinger12cba9a2010-09-15 00:17:37 +000059{
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110060 if (master->set_sck_get_miso)
61 return master->set_sck_get_miso(sck);
62
63 master->set_sck(sck);
64 return master->get_miso();
hailfinger12cba9a2010-09-15 00:17:37 +000065}
66
Souvik Ghoshd75cd672016-06-17 14:21:39 -070067static int bitbang_spi_send_command(const struct flashctx *flash,
68 unsigned int writecnt, unsigned int readcnt,
69 const unsigned char *writearr,
70 unsigned char *readarr);
mkarcherd264e9e2011-05-11 17:07:07 +000071
Patrick Georgif4f1e2f2017-03-10 17:38:40 +010072static const struct spi_master spi_master_bitbang = {
Edward O'Callaghana6673bd2019-06-24 15:22:28 +100073 .features = SPI_MASTER_4BA,
uwe8d342eb2011-07-28 08:13:25 +000074 .max_data_read = MAX_DATA_READ_UNLIMITED,
75 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
76 .command = bitbang_spi_send_command,
77 .multicommand = default_spi_send_multicommand,
78 .read = default_spi_read,
79 .write_256 = default_spi_write_256,
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +110080 .write_aai = default_spi_write_aai,
mkarcherd264e9e2011-05-11 17:07:07 +000081};
82
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110083#if 0 // until it is needed
84static int bitbang_spi_shutdown(const struct bitbang_spi_master *master)
85{
86 /* FIXME: Run bitbang_spi_release_bus here or per command? */
87 return 0;
88}
89#endif
90
Craig Hesling65eb8812019-08-01 09:33:56 -070091int register_spi_bitbang_master(const struct bitbang_spi_master *master)
hailfingeracce2df2009-09-28 13:15:16 +000092{
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110093 struct spi_master mst = spi_master_bitbang;
Nico Huber1627df32017-04-22 00:09:42 +020094 /* If someone forgot to initialize a bitbang function, we catch it here. */
95 if (!master || !master->set_cs ||
Edward O'Callaghanc66827e2020-10-09 12:22:04 +110096 !master->set_sck || !master->set_mosi || !master->get_miso ||
97 (master->request_bus && !master->release_bus) ||
98 (!master->request_bus && master->release_bus)) {
hailfinger12cba9a2010-09-15 00:17:37 +000099 msg_perr("Incomplete SPI bitbang master setting!\n"
100 "Please report a bug at flashrom@flashrom.org\n");
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100101 return ERROR_FLASHROM_BUG;
hailfinger12cba9a2010-09-15 00:17:37 +0000102 }
103
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100104 mst.data = master;
105 register_spi_master(&mst);
hailfinger49ff41f2010-07-17 12:54:09 +0000106
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100107 /* Only mess with the bus if we're sure nobody else uses it. */
108 bitbang_spi_request_bus(master);
109 bitbang_spi_set_cs(master, 1);
110 bitbang_spi_set_sck_set_mosi(master, 0, 0);
111 /* FIXME: Release SPI bus here and request it again for each command or
112 * don't release it now and only release it on programmer shutdown?
113 */
114 bitbang_spi_release_bus(master);
hailfingeracce2df2009-09-28 13:15:16 +0000115 return 0;
116}
117
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100118static uint8_t bitbang_spi_read_byte(const struct bitbang_spi_master *master)
hailfingeracce2df2009-09-28 13:15:16 +0000119{
120 uint8_t ret = 0;
121 int i;
122
123 for (i = 7; i >= 0; i--) {
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100124 if (i == 0)
125 bitbang_spi_set_sck_set_mosi(master, 0, 0);
126 else
127 bitbang_spi_set_sck(master, 0);
128 programmer_delay(master->half_period);
hailfingeracce2df2009-09-28 13:15:16 +0000129 ret <<= 1;
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100130 ret |= bitbang_spi_set_sck_get_miso(master, 1);
131 programmer_delay(master->half_period);
hailfingeracce2df2009-09-28 13:15:16 +0000132 }
133 return ret;
134}
135
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100136static void bitbang_spi_write_byte(const struct bitbang_spi_master *master, uint8_t val)
137{
138 int i;
139
140 for (i = 7; i >= 0; i--) {
141 bitbang_spi_set_sck_set_mosi(master, 0, (val >> i) & 1);
142 programmer_delay(master->half_period);
143 bitbang_spi_set_sck(master, 1);
144 programmer_delay(master->half_period);
145 }
146}
147
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700148static int bitbang_spi_send_command(const struct flashctx *flash,
149 unsigned int writecnt, unsigned int readcnt,
150 const unsigned char *writearr,
151 unsigned char *readarr)
hailfingeracce2df2009-09-28 13:15:16 +0000152{
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100153 unsigned int i;
154 const struct bitbang_spi_master *master = flash->mst->spi.data;
hailfingeracce2df2009-09-28 13:15:16 +0000155
hailfinger12cba9a2010-09-15 00:17:37 +0000156 /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
157 * Requesting and releasing the SPI bus is handled in here to allow the
158 * programmer to use its own SPI engine for native accesses.
159 */
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100160 bitbang_spi_request_bus(master);
161 bitbang_spi_set_cs(master, 0);
mkarcher6b511512010-07-17 10:42:34 +0000162 for (i = 0; i < writecnt; i++)
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100163 bitbang_spi_write_byte(master, writearr[i]);
mkarcher6b511512010-07-17 10:42:34 +0000164 for (i = 0; i < readcnt; i++)
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100165 readarr[i] = bitbang_spi_read_byte(master);
mkarcher6b511512010-07-17 10:42:34 +0000166
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100167 bitbang_spi_set_sck(master, 0);
168 programmer_delay(master->half_period);
169 bitbang_spi_set_cs(master, 1);
170 programmer_delay(master->half_period);
hailfinger12cba9a2010-09-15 00:17:37 +0000171 /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
Edward O'Callaghanc66827e2020-10-09 12:22:04 +1100172 bitbang_spi_release_bus(master);
hailfingeracce2df2009-09-28 13:15:16 +0000173
174 return 0;
175}