1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
5 *
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
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <ctype.h>
20 #include "flash.h"
21 #include "programmer.h"
22 #include "spi.h"
23
24 /* Note that CS# is active low, so val=0 means the chip is active. */
bitbang_spi_set_cs(const struct bitbang_spi_master * const master,int val,void * spi_data)25 static void bitbang_spi_set_cs(const struct bitbang_spi_master * const master, int val, void *spi_data)
26 {
27 master->set_cs(val, spi_data);
28 }
29
bitbang_spi_set_sck(const struct bitbang_spi_master * const master,int val,void * spi_data)30 static void bitbang_spi_set_sck(const struct bitbang_spi_master * const master, int val, void *spi_data)
31 {
32 master->set_sck(val, spi_data);
33 }
34
bitbang_spi_request_bus(const struct bitbang_spi_master * const master,void * spi_data)35 static void bitbang_spi_request_bus(const struct bitbang_spi_master * const master, void *spi_data)
36 {
37 if (master->request_bus)
38 master->request_bus(spi_data);
39 }
40
bitbang_spi_release_bus(const struct bitbang_spi_master * const master,void * spi_data)41 static void bitbang_spi_release_bus(const struct bitbang_spi_master * const master, void *spi_data)
42 {
43 if (master->release_bus)
44 master->release_bus(spi_data);
45 }
46
bitbang_spi_set_sck_set_mosi(const struct bitbang_spi_master * const master,int sck,int mosi,void * spi_data)47 static void bitbang_spi_set_sck_set_mosi(const struct bitbang_spi_master * const master, int sck, int mosi,
48 void *spi_data)
49 {
50 if (master->set_sck_set_mosi) {
51 master->set_sck_set_mosi(sck, mosi, spi_data);
52 return;
53 }
54
55 master->set_sck(sck, spi_data);
56 master->set_mosi(mosi, spi_data);
57 }
58
bitbang_spi_set_sck_get_miso(const struct bitbang_spi_master * const master,int sck,void * spi_data)59 static int bitbang_spi_set_sck_get_miso(const struct bitbang_spi_master * const master, int sck,
60 void *spi_data)
61 {
62 if (master->set_sck_get_miso)
63 return master->set_sck_get_miso(sck, spi_data);
64
65 master->set_sck(sck, spi_data);
66 return master->get_miso(spi_data);
67 }
68
bitbang_spi_read_byte(const struct bitbang_spi_master * master,void * spi_data)69 static uint8_t bitbang_spi_read_byte(const struct bitbang_spi_master *master, void *spi_data)
70 {
71 uint8_t ret = 0;
72 int i;
73
74 for (i = 7; i >= 0; i--) {
75 if (i == 0)
76 bitbang_spi_set_sck_set_mosi(master, 0, 0, spi_data);
77 else
78 bitbang_spi_set_sck(master, 0, spi_data);
79 default_delay(master->half_period);
80 ret <<= 1;
81 ret |= bitbang_spi_set_sck_get_miso(master, 1, spi_data);
82 default_delay(master->half_period);
83 }
84 return ret;
85 }
86
bitbang_spi_write_byte(const struct bitbang_spi_master * master,uint8_t val,void * spi_data)87 static void bitbang_spi_write_byte(const struct bitbang_spi_master *master, uint8_t val, void *spi_data)
88 {
89 int i;
90
91 for (i = 7; i >= 0; i--) {
92 bitbang_spi_set_sck_set_mosi(master, 0, (val >> i) & 1, spi_data);
93 default_delay(master->half_period);
94 bitbang_spi_set_sck(master, 1, spi_data);
95 default_delay(master->half_period);
96 }
97 }
98
99 struct bitbang_spi_master_data {
100 const struct bitbang_spi_master *master;
101 void *spi_data;
102 };
103
bitbang_spi_send_command(const struct flashctx * flash,unsigned int writecnt,unsigned int readcnt,const unsigned char * writearr,unsigned char * readarr)104 static int bitbang_spi_send_command(const struct flashctx *flash,
105 unsigned int writecnt, unsigned int readcnt,
106 const unsigned char *writearr,
107 unsigned char *readarr)
108 {
109 unsigned int i;
110 const struct bitbang_spi_master_data *data = flash->mst->spi.data;
111 const struct bitbang_spi_master *master = data->master;
112
113 /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
114 * Requesting and releasing the SPI bus is handled in here to allow the
115 * programmer to use its own SPI engine for native accesses.
116 */
117 bitbang_spi_request_bus(master, data->spi_data);
118 bitbang_spi_set_cs(master, 0, data->spi_data);
119 for (i = 0; i < writecnt; i++)
120 bitbang_spi_write_byte(master, writearr[i], data->spi_data);
121 for (i = 0; i < readcnt; i++)
122 readarr[i] = bitbang_spi_read_byte(master, data->spi_data);
123
124 bitbang_spi_set_sck(master, 0, data->spi_data);
125 default_delay(master->half_period);
126 bitbang_spi_set_cs(master, 1, data->spi_data);
127 default_delay(master->half_period);
128 /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
129 bitbang_spi_release_bus(master, data->spi_data);
130
131 return 0;
132 }
133
bitbang_spi_shutdown(void * data)134 static int bitbang_spi_shutdown(void *data)
135 {
136 /* FIXME: Run bitbang_spi_release_bus here or per command? */
137 free(data);
138 return 0;
139 }
140
141 static const struct spi_master spi_master_bitbang = {
142 .features = SPI_MASTER_4BA,
143 .max_data_read = MAX_DATA_READ_UNLIMITED,
144 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
145 .command = bitbang_spi_send_command,
146 .read = default_spi_read,
147 .write_256 = default_spi_write_256,
148 .shutdown = bitbang_spi_shutdown,
149 };
150
register_spi_bitbang_master(const struct bitbang_spi_master * master,void * spi_data)151 int register_spi_bitbang_master(const struct bitbang_spi_master *master, void *spi_data)
152 {
153 struct spi_master mst = spi_master_bitbang;
154 /* If someone forgot to initialize a bitbang function, we catch it here. */
155 if (!master || !master->set_cs ||
156 !master->set_sck || !master->set_mosi || !master->get_miso ||
157 (master->request_bus && !master->release_bus) ||
158 (!master->request_bus && master->release_bus)) {
159 msg_perr("Incomplete SPI bitbang master setting!\n"
160 "Please report a bug at [email protected]\n");
161 return ERROR_FLASHROM_BUG;
162 }
163
164 struct bitbang_spi_master_data *data = calloc(1, sizeof(*data));
165 if (!data)
166 return ERROR_FLASHROM_FATAL;
167
168 data->master = master;
169 if (spi_data)
170 data->spi_data = spi_data;
171
172 register_spi_master(&mst, data);
173
174 /* Only mess with the bus if we're sure nobody else uses it. */
175 bitbang_spi_request_bus(master, spi_data);
176 bitbang_spi_set_cs(master, 1, spi_data);
177 bitbang_spi_set_sck_set_mosi(master, 0, 0, spi_data);
178 /* FIXME: Release SPI bus here and request it again for each command or
179 * don't release it now and only release it on programmer shutdown?
180 */
181 bitbang_spi_release_bus(master, spi_data);
182 return 0;
183 }
184