blob: a7816a4b65b0d972dfff3d3f46f61f48b9f4face [file] [log] [blame]
Alexandre Boeglin80e64712014-12-20 20:25:19 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2014 Alexandre Boeglin <alex@boeglin.org>
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.
Alexandre Boeglin80e64712014-12-20 20:25:19 +000014 */
15
16#if CONFIG_MSTARDDC_SPI == 1
17
18#include <stdio.h>
19#include <string.h>
20#include <stdlib.h>
21#include <ctype.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <unistd.h>
26#include <errno.h>
27#include <sys/ioctl.h>
28#include <linux/i2c-dev.h>
29#include <linux/i2c.h>
30#include "flash.h"
31#include "programmer.h"
32#include "spi.h"
33
Anastasia Klimchukbfb211c2021-05-18 09:25:01 +100034struct mstarddc_spi_data {
Anastasia Klimchukf57486e2021-05-18 09:34:55 +100035 int fd;
36 int addr;
37 int doreset;
Anastasia Klimchukbfb211c2021-05-18 09:25:01 +100038};
Alexandre Boeglin80e64712014-12-20 20:25:19 +000039
40// MSTAR DDC Commands
41#define MSTARDDC_SPI_WRITE 0x10
42#define MSTARDDC_SPI_READ 0x11
43#define MSTARDDC_SPI_END 0x12
44#define MSTARDDC_SPI_RESET 0x24
45
46/* Returns 0 upon success, a negative number upon errors. */
47static int mstarddc_spi_shutdown(void *data)
48{
Anastasia Klimchukbfb211c2021-05-18 09:25:01 +100049 struct mstarddc_spi_data *mstarddc_data = data;
50
Alexandre Boeglin80e64712014-12-20 20:25:19 +000051 // Reset, disables ISP mode
Anastasia Klimchukf57486e2021-05-18 09:34:55 +100052 if (mstarddc_data->doreset == 1) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +000053 uint8_t cmd = MSTARDDC_SPI_RESET;
Anastasia Klimchukf57486e2021-05-18 09:34:55 +100054 if (write(mstarddc_data->fd, &cmd, 1) < 0) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +000055 msg_perr("Error sending reset command: errno %d.\n",
56 errno);
57 return -1;
58 }
59 } else {
60 msg_pinfo("Info: Reset command was not sent. "
61 "Either the noreset=1 option was used, "
Elyes HAOUASe2c90c42018-08-18 09:04:41 +020062 "or an error occurred.\n");
Alexandre Boeglin80e64712014-12-20 20:25:19 +000063 }
64
Anastasia Klimchukf57486e2021-05-18 09:34:55 +100065 if (close(mstarddc_data->fd) < 0) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +000066 msg_perr("Error closing device: errno %d.\n", errno);
67 return -1;
68 }
Anastasia Klimchukbfb211c2021-05-18 09:25:01 +100069
70 free(data);
Alexandre Boeglin80e64712014-12-20 20:25:19 +000071 return 0;
72}
73
74/* Returns 0 upon success, a negative number upon errors. */
Anastasia Klimchuke7045832021-03-23 16:34:07 +110075static int mstarddc_spi_send_command(const struct flashctx *flash,
76 unsigned int writecnt,
77 unsigned int readcnt,
78 const unsigned char *writearr,
79 unsigned char *readarr)
80{
Anastasia Klimchukbfb211c2021-05-18 09:25:01 +100081 struct mstarddc_spi_data *mstarddc_data = flash->mst->spi.data;
Anastasia Klimchuke7045832021-03-23 16:34:07 +110082 int ret = 0;
83 uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t));
84 if (cmd == NULL) {
85 msg_perr("Error allocating memory: errno %d.\n", errno);
86 ret = -1;
87 }
88
89 if (!ret && writecnt) {
90 cmd[0] = MSTARDDC_SPI_WRITE;
91 memcpy(cmd + 1, writearr, writecnt);
Anastasia Klimchukf57486e2021-05-18 09:34:55 +100092 if (write(mstarddc_data->fd, cmd, writecnt + 1) < 0) {
Anastasia Klimchuke7045832021-03-23 16:34:07 +110093 msg_perr("Error sending write command: errno %d.\n",
94 errno);
95 ret = -1;
96 }
97 }
98
99 if (!ret && readcnt) {
100 struct i2c_rdwr_ioctl_data i2c_data;
101 struct i2c_msg msg[2];
102
103 cmd[0] = MSTARDDC_SPI_READ;
104 i2c_data.nmsgs = 2;
105 i2c_data.msgs = msg;
Anastasia Klimchukf57486e2021-05-18 09:34:55 +1000106 i2c_data.msgs[0].addr = mstarddc_data->addr;
Anastasia Klimchuke7045832021-03-23 16:34:07 +1100107 i2c_data.msgs[0].len = 1;
108 i2c_data.msgs[0].flags = 0;
109 i2c_data.msgs[0].buf = cmd;
Anastasia Klimchukf57486e2021-05-18 09:34:55 +1000110 i2c_data.msgs[1].addr = mstarddc_data->addr;
Anastasia Klimchuke7045832021-03-23 16:34:07 +1100111 i2c_data.msgs[1].len = readcnt;
112 i2c_data.msgs[1].flags = I2C_M_RD;
113 i2c_data.msgs[1].buf = readarr;
114
Anastasia Klimchukf57486e2021-05-18 09:34:55 +1000115 if (ioctl(mstarddc_data->fd, I2C_RDWR, &i2c_data) < 0) {
Anastasia Klimchuke7045832021-03-23 16:34:07 +1100116 msg_perr("Error sending read command: errno %d.\n",
117 errno);
118 ret = -1;
119 }
120 }
121
122 if (!ret && (writecnt || readcnt)) {
123 cmd[0] = MSTARDDC_SPI_END;
Anastasia Klimchukf57486e2021-05-18 09:34:55 +1000124 if (write(mstarddc_data->fd, cmd, 1) < 0) {
Anastasia Klimchuke7045832021-03-23 16:34:07 +1100125 msg_perr("Error sending end command: errno %d.\n",
126 errno);
127 ret = -1;
128 }
129 }
130
131 /* Do not reset if something went wrong, as it might prevent from
132 * retrying flashing. */
133 if (ret != 0)
Anastasia Klimchukf57486e2021-05-18 09:34:55 +1000134 mstarddc_data->doreset = 0;
Anastasia Klimchuke7045832021-03-23 16:34:07 +1100135
136 if (cmd)
137 free(cmd);
138
139 return ret;
140}
141
142static const struct spi_master spi_master_mstarddc = {
143 .max_data_read = 256,
144 .max_data_write = 256,
145 .command = mstarddc_spi_send_command,
146 .multicommand = default_spi_send_multicommand,
147 .read = default_spi_read,
148 .write_256 = default_spi_write_256,
149 .write_aai = default_spi_write_aai,
150};
151
152/* Returns 0 upon success, a negative number upon errors. */
Thomas Heijligen4f5169d2021-05-04 15:32:17 +0200153static int mstarddc_spi_init(void)
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000154{
Stefan Tauneradadca62015-02-18 23:28:30 +0000155 int ret = 0;
Anastasia Klimchukbfb211c2021-05-18 09:25:01 +1000156 int mstarddc_fd = -1;
157 int mstarddc_addr;
158 int mstarddc_doreset = 1;
159 struct mstarddc_spi_data *mstarddc_data;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000160
161 // Get device, address from command-line
Stefan Tauneradadca62015-02-18 23:28:30 +0000162 char *i2c_device = extract_programmer_param("dev");
163 if (i2c_device != NULL && strlen(i2c_device) > 0) {
164 char *i2c_address = strchr(i2c_device, ':');
165 if (i2c_address != NULL) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000166 *i2c_address = '\0';
167 i2c_address++;
168 }
Stefan Tauneradadca62015-02-18 23:28:30 +0000169 if (i2c_address == NULL || strlen(i2c_address) == 0) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000170 msg_perr("Error: no address specified.\n"
171 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
Stefan Tauneradadca62015-02-18 23:28:30 +0000172 ret = -1;
173 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000174 }
Stefan Tauneradadca62015-02-18 23:28:30 +0000175 mstarddc_addr = strtol(i2c_address, NULL, 16); // FIXME: error handling
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000176 } else {
177 msg_perr("Error: no device specified.\n"
178 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
Stefan Tauneradadca62015-02-18 23:28:30 +0000179 ret = -1;
180 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000181 }
Stefan Tauneradadca62015-02-18 23:28:30 +0000182 msg_pinfo("Info: Will try to use device %s and address 0x%02x.\n", i2c_device, mstarddc_addr);
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000183
184 // Get noreset=1 option from command-line
Stefan Tauneradadca62015-02-18 23:28:30 +0000185 char *noreset = extract_programmer_param("noreset");
186 if (noreset != NULL && noreset[0] == '1')
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000187 mstarddc_doreset = 0;
Stefan Tauneradadca62015-02-18 23:28:30 +0000188 free(noreset);
189 msg_pinfo("Info: Will %sreset the device at the end.\n", mstarddc_doreset ? "" : "NOT ");
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000190 // Open device
191 if ((mstarddc_fd = open(i2c_device, O_RDWR)) < 0) {
192 switch (errno) {
193 case EACCES:
194 msg_perr("Error opening %s: Permission denied.\n"
195 "Please use sudo or run as root.\n",
196 i2c_device);
197 break;
198 case ENOENT:
199 msg_perr("Error opening %s: No such file.\n"
200 "Please check you specified the correct device.\n",
201 i2c_device);
202 break;
203 default:
Stefan Tauneradadca62015-02-18 23:28:30 +0000204 msg_perr("Error opening %s: %s.\n", i2c_device, strerror(errno));
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000205 }
Stefan Tauneradadca62015-02-18 23:28:30 +0000206 ret = -1;
207 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000208 }
209 // Set slave address
210 if (ioctl(mstarddc_fd, I2C_SLAVE, mstarddc_addr) < 0) {
211 msg_perr("Error setting slave address 0x%02x: errno %d.\n",
212 mstarddc_addr, errno);
Stefan Tauneradadca62015-02-18 23:28:30 +0000213 ret = -1;
214 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000215 }
216 // Enable ISP mode
217 uint8_t cmd[5] = { 'M', 'S', 'T', 'A', 'R' };
218 if (write(mstarddc_fd, cmd, 5) < 0) {
219 int enable_err = errno;
220 uint8_t end_cmd = MSTARDDC_SPI_END;
221
222 // Assume device is already in ISP mode, try to send END command
223 if (write(mstarddc_fd, &end_cmd, 1) < 0) {
224 msg_perr("Error enabling ISP mode: errno %d & %d.\n"
225 "Please check that device (%s) and address (0x%02x) are correct.\n",
226 enable_err, errno, i2c_device, mstarddc_addr);
Stefan Tauneradadca62015-02-18 23:28:30 +0000227 ret = -1;
228 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000229 }
230 }
Anastasia Klimchukbfb211c2021-05-18 09:25:01 +1000231
232 mstarddc_data = calloc(1, sizeof(*mstarddc_data));
233 if (!mstarddc_data) {
234 msg_perr("Unable to allocate space for SPI master data\n");
235 ret = -1;
236 goto out;
237 }
Anastasia Klimchukf57486e2021-05-18 09:34:55 +1000238
239 mstarddc_data->fd = mstarddc_fd;
240 mstarddc_data->addr = mstarddc_addr;
241 mstarddc_data->doreset = mstarddc_doreset;
Anastasia Klimchukbfb211c2021-05-18 09:25:01 +1000242
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000243 // Register shutdown function
Anastasia Klimchukbfb211c2021-05-18 09:25:01 +1000244 register_shutdown(mstarddc_spi_shutdown, mstarddc_data);
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000245
246 // Register programmer
Anastasia Klimchukbfb211c2021-05-18 09:25:01 +1000247 register_spi_master(&spi_master_mstarddc, mstarddc_data);
Stefan Tauneradadca62015-02-18 23:28:30 +0000248out:
249 free(i2c_device);
Anastasia Klimchukbfb211c2021-05-18 09:25:01 +1000250 if (ret && (mstarddc_fd >= 0))
251 close(mstarddc_fd);
Stefan Tauneradadca62015-02-18 23:28:30 +0000252 return ret;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000253}
254
Thomas Heijligen4f5169d2021-05-04 15:32:17 +0200255const struct programmer_entry programmer_mstarddc_spi = {
256 .name = "mstarddc_spi",
257 .type = OTHER,
258 .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n",
259 .init = mstarddc_spi_init,
260 .map_flash_region = fallback_map,
261 .unmap_flash_region = fallback_unmap,
262 .delay = internal_delay,
263};
264
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000265#endif