blob: 46bcd32aa8c3fe1c9b0f50c09a139d59286e9227 [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
Alexandre Boeglin80e64712014-12-20 20:25:19 +000016#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
19#include <ctype.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23#include <unistd.h>
24#include <errno.h>
25#include <sys/ioctl.h>
26#include <linux/i2c-dev.h>
27#include <linux/i2c.h>
28#include "flash.h"
29#include "programmer.h"
30#include "spi.h"
31
32static const struct spi_master spi_master_mstarddc;
33
34static int mstarddc_fd;
35static int mstarddc_addr;
36static int mstarddc_doreset = 1;
37
38// MSTAR DDC Commands
39#define MSTARDDC_SPI_WRITE 0x10
40#define MSTARDDC_SPI_READ 0x11
41#define MSTARDDC_SPI_END 0x12
42#define MSTARDDC_SPI_RESET 0x24
43
44/* Returns 0 upon success, a negative number upon errors. */
45static int mstarddc_spi_shutdown(void *data)
46{
47 // Reset, disables ISP mode
48 if (mstarddc_doreset == 1) {
49 uint8_t cmd = MSTARDDC_SPI_RESET;
50 if (write(mstarddc_fd, &cmd, 1) < 0) {
51 msg_perr("Error sending reset command: errno %d.\n",
52 errno);
53 return -1;
54 }
55 } else {
56 msg_pinfo("Info: Reset command was not sent. "
57 "Either the noreset=1 option was used, "
Elyes HAOUASe2c90c42018-08-18 09:04:41 +020058 "or an error occurred.\n");
Alexandre Boeglin80e64712014-12-20 20:25:19 +000059 }
60
61 if (close(mstarddc_fd) < 0) {
62 msg_perr("Error closing device: errno %d.\n", errno);
63 return -1;
64 }
65 return 0;
66}
67
68/* Returns 0 upon success, a negative number upon errors. */
Thomas Heijligencc853d82021-05-04 15:32:17 +020069static int mstarddc_spi_init(void)
Alexandre Boeglin80e64712014-12-20 20:25:19 +000070{
Stefan Tauneradadca62015-02-18 23:28:30 +000071 int ret = 0;
Alexandre Boeglin80e64712014-12-20 20:25:19 +000072
73 // Get device, address from command-line
Stefan Tauneradadca62015-02-18 23:28:30 +000074 char *i2c_device = extract_programmer_param("dev");
75 if (i2c_device != NULL && strlen(i2c_device) > 0) {
76 char *i2c_address = strchr(i2c_device, ':');
77 if (i2c_address != NULL) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +000078 *i2c_address = '\0';
79 i2c_address++;
80 }
Stefan Tauneradadca62015-02-18 23:28:30 +000081 if (i2c_address == NULL || strlen(i2c_address) == 0) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +000082 msg_perr("Error: no address specified.\n"
83 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
Stefan Tauneradadca62015-02-18 23:28:30 +000084 ret = -1;
85 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +000086 }
Stefan Tauneradadca62015-02-18 23:28:30 +000087 mstarddc_addr = strtol(i2c_address, NULL, 16); // FIXME: error handling
Alexandre Boeglin80e64712014-12-20 20:25:19 +000088 } else {
89 msg_perr("Error: no device specified.\n"
90 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
Stefan Tauneradadca62015-02-18 23:28:30 +000091 ret = -1;
92 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +000093 }
Stefan Tauneradadca62015-02-18 23:28:30 +000094 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 +000095
96 // Get noreset=1 option from command-line
Stefan Tauneradadca62015-02-18 23:28:30 +000097 char *noreset = extract_programmer_param("noreset");
98 if (noreset != NULL && noreset[0] == '1')
Alexandre Boeglin80e64712014-12-20 20:25:19 +000099 mstarddc_doreset = 0;
Stefan Tauneradadca62015-02-18 23:28:30 +0000100 free(noreset);
101 msg_pinfo("Info: Will %sreset the device at the end.\n", mstarddc_doreset ? "" : "NOT ");
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000102 // Open device
103 if ((mstarddc_fd = open(i2c_device, O_RDWR)) < 0) {
104 switch (errno) {
105 case EACCES:
106 msg_perr("Error opening %s: Permission denied.\n"
107 "Please use sudo or run as root.\n",
108 i2c_device);
109 break;
110 case ENOENT:
111 msg_perr("Error opening %s: No such file.\n"
112 "Please check you specified the correct device.\n",
113 i2c_device);
114 break;
115 default:
Stefan Tauneradadca62015-02-18 23:28:30 +0000116 msg_perr("Error opening %s: %s.\n", i2c_device, strerror(errno));
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000117 }
Stefan Tauneradadca62015-02-18 23:28:30 +0000118 ret = -1;
119 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000120 }
121 // Set slave address
122 if (ioctl(mstarddc_fd, I2C_SLAVE, mstarddc_addr) < 0) {
123 msg_perr("Error setting slave address 0x%02x: errno %d.\n",
124 mstarddc_addr, errno);
Stefan Tauneradadca62015-02-18 23:28:30 +0000125 ret = -1;
126 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000127 }
128 // Enable ISP mode
129 uint8_t cmd[5] = { 'M', 'S', 'T', 'A', 'R' };
130 if (write(mstarddc_fd, cmd, 5) < 0) {
131 int enable_err = errno;
132 uint8_t end_cmd = MSTARDDC_SPI_END;
133
134 // Assume device is already in ISP mode, try to send END command
135 if (write(mstarddc_fd, &end_cmd, 1) < 0) {
136 msg_perr("Error enabling ISP mode: errno %d & %d.\n"
137 "Please check that device (%s) and address (0x%02x) are correct.\n",
138 enable_err, errno, i2c_device, mstarddc_addr);
Stefan Tauneradadca62015-02-18 23:28:30 +0000139 ret = -1;
140 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000141 }
142 }
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000143
144 // Register programmer
Nico Huber5e08e3e2021-05-11 17:38:14 +0200145 register_spi_master(&spi_master_mstarddc, NULL);
Stefan Tauneradadca62015-02-18 23:28:30 +0000146out:
147 free(i2c_device);
148 return ret;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000149}
150
151/* Returns 0 upon success, a negative number upon errors. */
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000152static int mstarddc_spi_send_command(const struct flashctx *flash,
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000153 unsigned int writecnt,
154 unsigned int readcnt,
155 const unsigned char *writearr,
156 unsigned char *readarr)
157{
158 int ret = 0;
Stefan Tauneradadca62015-02-18 23:28:30 +0000159 uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t));
160 if (cmd == NULL) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000161 msg_perr("Error allocating memory: errno %d.\n", errno);
162 ret = -1;
163 }
164
165 if (!ret && writecnt) {
166 cmd[0] = MSTARDDC_SPI_WRITE;
167 memcpy(cmd + 1, writearr, writecnt);
168 if (write(mstarddc_fd, cmd, writecnt + 1) < 0) {
169 msg_perr("Error sending write command: errno %d.\n",
170 errno);
171 ret = -1;
172 }
173 }
174
175 if (!ret && readcnt) {
176 struct i2c_rdwr_ioctl_data i2c_data;
177 struct i2c_msg msg[2];
178
179 cmd[0] = MSTARDDC_SPI_READ;
180 i2c_data.nmsgs = 2;
181 i2c_data.msgs = msg;
182 i2c_data.msgs[0].addr = mstarddc_addr;
183 i2c_data.msgs[0].len = 1;
184 i2c_data.msgs[0].flags = 0;
185 i2c_data.msgs[0].buf = cmd;
186 i2c_data.msgs[1].addr = mstarddc_addr;
187 i2c_data.msgs[1].len = readcnt;
188 i2c_data.msgs[1].flags = I2C_M_RD;
189 i2c_data.msgs[1].buf = readarr;
190
191 if (ioctl(mstarddc_fd, I2C_RDWR, &i2c_data) < 0) {
192 msg_perr("Error sending read command: errno %d.\n",
193 errno);
194 ret = -1;
195 }
196 }
197
198 if (!ret && (writecnt || readcnt)) {
199 cmd[0] = MSTARDDC_SPI_END;
200 if (write(mstarddc_fd, cmd, 1) < 0) {
201 msg_perr("Error sending end command: errno %d.\n",
202 errno);
203 ret = -1;
204 }
205 }
206
207 /* Do not reset if something went wrong, as it might prevent from
208 * retrying flashing. */
209 if (ret != 0)
210 mstarddc_doreset = 0;
211
212 if (cmd)
213 free(cmd);
214
215 return ret;
216}
217
218static const struct spi_master spi_master_mstarddc = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200219 .max_data_read = 256,
220 .max_data_write = 256,
221 .command = mstarddc_spi_send_command,
222 .multicommand = default_spi_send_multicommand,
223 .read = default_spi_read,
224 .write_256 = default_spi_write_256,
225 .write_aai = default_spi_write_aai,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +1000226 .shutdown = mstarddc_spi_shutdown,
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000227};
228
Thomas Heijligencc853d82021-05-04 15:32:17 +0200229const struct programmer_entry programmer_mstarddc_spi = {
230 .name = "mstarddc_spi",
231 .type = OTHER,
232 .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n",
233 .init = mstarddc_spi_init,
234 .map_flash_region = fallback_map,
235 .unmap_flash_region = fallback_unmap,
236 .delay = internal_delay,
237};