1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2014 Alexandre Boeglin <[email protected]>
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 <stdbool.h>
19 #include <stdlib.h>
20 #include <ctype.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <sys/ioctl.h>
27 #include <linux/i2c-dev.h>
28 #include <linux/i2c.h>
29 #include "flash.h"
30 #include "programmer.h"
31 #include "spi.h"
32
33 struct mstarddc_spi_data {
34 int fd;
35 int addr;
36 bool doreset;
37 };
38
39 // MSTAR DDC Commands
40 #define MSTARDDC_SPI_WRITE 0x10
41 #define MSTARDDC_SPI_READ 0x11
42 #define MSTARDDC_SPI_END 0x12
43 #define MSTARDDC_SPI_RESET 0x24
44
45 /* Returns 0 upon success, a negative number upon errors. */
mstarddc_spi_shutdown(void * data)46 static int mstarddc_spi_shutdown(void *data)
47 {
48 struct mstarddc_spi_data *mstarddc_data = data;
49
50 // Reset, disables ISP mode
51 if (mstarddc_data->doreset) {
52 uint8_t cmd = MSTARDDC_SPI_RESET;
53 if (write(mstarddc_data->fd, &cmd, 1) < 0) {
54 msg_perr("Error sending reset command: errno %d.\n",
55 errno);
56 return -1;
57 }
58 } else {
59 msg_pinfo("Info: Reset command was not sent. "
60 "Either the noreset=1 option was used, "
61 "or an error occurred.\n");
62 }
63
64 if (close(mstarddc_data->fd) < 0) {
65 msg_perr("Error closing device: errno %d.\n", errno);
66 return -1;
67 }
68
69 free(data);
70 return 0;
71 }
72
73 /* Returns 0 upon success, a negative number upon errors. */
mstarddc_spi_send_command(const struct flashctx * flash,unsigned int writecnt,unsigned int readcnt,const unsigned char * writearr,unsigned char * readarr)74 static int mstarddc_spi_send_command(const struct flashctx *flash,
75 unsigned int writecnt,
76 unsigned int readcnt,
77 const unsigned char *writearr,
78 unsigned char *readarr)
79 {
80 struct mstarddc_spi_data *mstarddc_data = flash->mst->spi.data;
81 int ret = 0;
82 uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t));
83 if (cmd == NULL) {
84 msg_perr("Error allocating memory: errno %d.\n", errno);
85 ret = -1;
86 }
87
88 if (!ret && writecnt) {
89 cmd[0] = MSTARDDC_SPI_WRITE;
90 memcpy(cmd + 1, writearr, writecnt);
91 if (write(mstarddc_data->fd, cmd, writecnt + 1) < 0) {
92 msg_perr("Error sending write command: errno %d.\n",
93 errno);
94 ret = -1;
95 }
96 }
97
98 if (!ret && readcnt) {
99 struct i2c_rdwr_ioctl_data i2c_data;
100 struct i2c_msg msg[2];
101
102 cmd[0] = MSTARDDC_SPI_READ;
103 i2c_data.nmsgs = 2;
104 i2c_data.msgs = msg;
105 i2c_data.msgs[0].addr = mstarddc_data->addr;
106 i2c_data.msgs[0].len = 1;
107 i2c_data.msgs[0].flags = 0;
108 i2c_data.msgs[0].buf = cmd;
109 i2c_data.msgs[1].addr = mstarddc_data->addr;
110 i2c_data.msgs[1].len = readcnt;
111 i2c_data.msgs[1].flags = I2C_M_RD;
112 i2c_data.msgs[1].buf = readarr;
113
114 if (ioctl(mstarddc_data->fd, I2C_RDWR, &i2c_data) < 0) {
115 msg_perr("Error sending read command: errno %d.\n",
116 errno);
117 ret = -1;
118 }
119 }
120
121 if (!ret && (writecnt || readcnt)) {
122 cmd[0] = MSTARDDC_SPI_END;
123 if (write(mstarddc_data->fd, cmd, 1) < 0) {
124 msg_perr("Error sending end command: errno %d.\n",
125 errno);
126 ret = -1;
127 }
128 }
129
130 /* Do not reset if something went wrong, as it might prevent from
131 * retrying flashing. */
132 if (ret != 0)
133 mstarddc_data->doreset = false;
134
135 if (cmd)
136 free(cmd);
137
138 return ret;
139 }
140
141 static const struct spi_master spi_master_mstarddc = {
142 .max_data_read = 256,
143 .max_data_write = 256,
144 .command = mstarddc_spi_send_command,
145 .read = default_spi_read,
146 .write_256 = default_spi_write_256,
147 .shutdown = mstarddc_spi_shutdown,
148 };
149
150 /* Returns 0 upon success, a negative number upon errors. */
mstarddc_spi_init(const struct programmer_cfg * cfg)151 static int mstarddc_spi_init(const struct programmer_cfg *cfg)
152 {
153 int ret = 0;
154 int mstarddc_fd = -1;
155 int mstarddc_addr;
156 bool mstarddc_doreset = true;
157 struct mstarddc_spi_data *mstarddc_data;
158
159 // Get device, address from command-line
160 char *i2c_device = extract_programmer_param_str(cfg, "dev");
161 if (i2c_device != NULL && strlen(i2c_device) > 0) {
162 char *i2c_address = strchr(i2c_device, ':');
163 if (i2c_address != NULL) {
164 *i2c_address = '\0';
165 i2c_address++;
166 }
167 if (i2c_address == NULL || strlen(i2c_address) == 0) {
168 msg_perr("Error: no address specified.\n"
169 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
170 ret = -1;
171 goto out;
172 }
173 mstarddc_addr = strtol(i2c_address, NULL, 16); // FIXME: error handling
174 } else {
175 msg_perr("Error: no device specified.\n"
176 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
177 ret = -1;
178 goto out;
179 }
180 msg_pinfo("Info: Will try to use device %s and address 0x%02x.\n", i2c_device, mstarddc_addr);
181
182 // Get noreset=1 option from command-line
183 char *noreset = extract_programmer_param_str(cfg, "noreset");
184 if (noreset != NULL && noreset[0] == '1')
185 mstarddc_doreset = false;
186 free(noreset);
187 msg_pinfo("Info: Will %sreset the device at the end.\n", mstarddc_doreset ? "" : "NOT ");
188 // Open device
189 if ((mstarddc_fd = open(i2c_device, O_RDWR)) < 0) {
190 switch (errno) {
191 case EACCES:
192 msg_perr("Error opening %s: Permission denied.\n"
193 "Please use sudo or run as root.\n",
194 i2c_device);
195 break;
196 case ENOENT:
197 msg_perr("Error opening %s: No such file.\n"
198 "Please check you specified the correct device.\n",
199 i2c_device);
200 break;
201 default:
202 msg_perr("Error opening %s: %s.\n", i2c_device, strerror(errno));
203 }
204 ret = -1;
205 goto out;
206 }
207 // Set slave address
208 if (ioctl(mstarddc_fd, I2C_SLAVE, mstarddc_addr) < 0) {
209 msg_perr("Error setting slave address 0x%02x: errno %d.\n",
210 mstarddc_addr, errno);
211 ret = -1;
212 goto out;
213 }
214 // Enable ISP mode
215 uint8_t cmd[5] = { 'M', 'S', 'T', 'A', 'R' };
216 if (write(mstarddc_fd, cmd, 5) < 0) {
217 int enable_err = errno;
218 uint8_t end_cmd = MSTARDDC_SPI_END;
219
220 // Assume device is already in ISP mode, try to send END command
221 if (write(mstarddc_fd, &end_cmd, 1) < 0) {
222 msg_perr("Error enabling ISP mode: errno %d & %d.\n"
223 "Please check that device (%s) and address (0x%02x) are correct.\n",
224 enable_err, errno, i2c_device, mstarddc_addr);
225 ret = -1;
226 goto out;
227 }
228 }
229
230 mstarddc_data = calloc(1, sizeof(*mstarddc_data));
231 if (!mstarddc_data) {
232 msg_perr("Unable to allocate space for SPI master data\n");
233 ret = -1;
234 goto out;
235 }
236
237 mstarddc_data->fd = mstarddc_fd;
238 mstarddc_data->addr = mstarddc_addr;
239 mstarddc_data->doreset = mstarddc_doreset;
240
241 // Register programmer
242 register_spi_master(&spi_master_mstarddc, mstarddc_data);
243 out:
244 free(i2c_device);
245 if (ret && (mstarddc_fd >= 0))
246 close(mstarddc_fd);
247 return ret;
248 }
249
250 const struct programmer_entry programmer_mstarddc_spi = {
251 .name = "mstarddc_spi",
252 .type = OTHER,
253 .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n",
254 .init = mstarddc_spi_init,
255 };
256