blob: 248dba67cb4e5f7533f959163387066d948db74a [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
34static const struct spi_master spi_master_mstarddc;
35
36static int mstarddc_fd;
37static int mstarddc_addr;
38static int mstarddc_doreset = 1;
39
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{
49 // Reset, disables ISP mode
50 if (mstarddc_doreset == 1) {
51 uint8_t cmd = MSTARDDC_SPI_RESET;
52 if (write(mstarddc_fd, &cmd, 1) < 0) {
53 msg_perr("Error sending reset command: errno %d.\n",
54 errno);
55 return -1;
56 }
57 } else {
58 msg_pinfo("Info: Reset command was not sent. "
59 "Either the noreset=1 option was used, "
Elyes HAOUASe2c90c42018-08-18 09:04:41 +020060 "or an error occurred.\n");
Alexandre Boeglin80e64712014-12-20 20:25:19 +000061 }
62
63 if (close(mstarddc_fd) < 0) {
64 msg_perr("Error closing device: errno %d.\n", errno);
65 return -1;
66 }
67 return 0;
68}
69
70/* Returns 0 upon success, a negative number upon errors. */
Thomas Heijligencc853d82021-05-04 15:32:17 +020071static int mstarddc_spi_init(void)
Alexandre Boeglin80e64712014-12-20 20:25:19 +000072{
Stefan Tauneradadca62015-02-18 23:28:30 +000073 int ret = 0;
Alexandre Boeglin80e64712014-12-20 20:25:19 +000074
75 // Get device, address from command-line
Stefan Tauneradadca62015-02-18 23:28:30 +000076 char *i2c_device = extract_programmer_param("dev");
77 if (i2c_device != NULL && strlen(i2c_device) > 0) {
78 char *i2c_address = strchr(i2c_device, ':');
79 if (i2c_address != NULL) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +000080 *i2c_address = '\0';
81 i2c_address++;
82 }
Stefan Tauneradadca62015-02-18 23:28:30 +000083 if (i2c_address == NULL || strlen(i2c_address) == 0) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +000084 msg_perr("Error: no address specified.\n"
85 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
Stefan Tauneradadca62015-02-18 23:28:30 +000086 ret = -1;
87 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +000088 }
Stefan Tauneradadca62015-02-18 23:28:30 +000089 mstarddc_addr = strtol(i2c_address, NULL, 16); // FIXME: error handling
Alexandre Boeglin80e64712014-12-20 20:25:19 +000090 } else {
91 msg_perr("Error: no device specified.\n"
92 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
Stefan Tauneradadca62015-02-18 23:28:30 +000093 ret = -1;
94 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +000095 }
Stefan Tauneradadca62015-02-18 23:28:30 +000096 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 +000097
98 // Get noreset=1 option from command-line
Stefan Tauneradadca62015-02-18 23:28:30 +000099 char *noreset = extract_programmer_param("noreset");
100 if (noreset != NULL && noreset[0] == '1')
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000101 mstarddc_doreset = 0;
Stefan Tauneradadca62015-02-18 23:28:30 +0000102 free(noreset);
103 msg_pinfo("Info: Will %sreset the device at the end.\n", mstarddc_doreset ? "" : "NOT ");
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000104 // Open device
105 if ((mstarddc_fd = open(i2c_device, O_RDWR)) < 0) {
106 switch (errno) {
107 case EACCES:
108 msg_perr("Error opening %s: Permission denied.\n"
109 "Please use sudo or run as root.\n",
110 i2c_device);
111 break;
112 case ENOENT:
113 msg_perr("Error opening %s: No such file.\n"
114 "Please check you specified the correct device.\n",
115 i2c_device);
116 break;
117 default:
Stefan Tauneradadca62015-02-18 23:28:30 +0000118 msg_perr("Error opening %s: %s.\n", i2c_device, strerror(errno));
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000119 }
Stefan Tauneradadca62015-02-18 23:28:30 +0000120 ret = -1;
121 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000122 }
123 // Set slave address
124 if (ioctl(mstarddc_fd, I2C_SLAVE, mstarddc_addr) < 0) {
125 msg_perr("Error setting slave address 0x%02x: errno %d.\n",
126 mstarddc_addr, errno);
Stefan Tauneradadca62015-02-18 23:28:30 +0000127 ret = -1;
128 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000129 }
130 // Enable ISP mode
131 uint8_t cmd[5] = { 'M', 'S', 'T', 'A', 'R' };
132 if (write(mstarddc_fd, cmd, 5) < 0) {
133 int enable_err = errno;
134 uint8_t end_cmd = MSTARDDC_SPI_END;
135
136 // Assume device is already in ISP mode, try to send END command
137 if (write(mstarddc_fd, &end_cmd, 1) < 0) {
138 msg_perr("Error enabling ISP mode: errno %d & %d.\n"
139 "Please check that device (%s) and address (0x%02x) are correct.\n",
140 enable_err, errno, i2c_device, mstarddc_addr);
Stefan Tauneradadca62015-02-18 23:28:30 +0000141 ret = -1;
142 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000143 }
144 }
145 // Register shutdown function
146 register_shutdown(mstarddc_spi_shutdown, NULL);
147
148 // Register programmer
149 register_spi_master(&spi_master_mstarddc);
Stefan Tauneradadca62015-02-18 23:28:30 +0000150out:
151 free(i2c_device);
152 return ret;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000153}
154
155/* Returns 0 upon success, a negative number upon errors. */
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000156static int mstarddc_spi_send_command(const struct flashctx *flash,
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000157 unsigned int writecnt,
158 unsigned int readcnt,
159 const unsigned char *writearr,
160 unsigned char *readarr)
161{
162 int ret = 0;
Stefan Tauneradadca62015-02-18 23:28:30 +0000163 uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t));
164 if (cmd == NULL) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000165 msg_perr("Error allocating memory: errno %d.\n", errno);
166 ret = -1;
167 }
168
169 if (!ret && writecnt) {
170 cmd[0] = MSTARDDC_SPI_WRITE;
171 memcpy(cmd + 1, writearr, writecnt);
172 if (write(mstarddc_fd, cmd, writecnt + 1) < 0) {
173 msg_perr("Error sending write command: errno %d.\n",
174 errno);
175 ret = -1;
176 }
177 }
178
179 if (!ret && readcnt) {
180 struct i2c_rdwr_ioctl_data i2c_data;
181 struct i2c_msg msg[2];
182
183 cmd[0] = MSTARDDC_SPI_READ;
184 i2c_data.nmsgs = 2;
185 i2c_data.msgs = msg;
186 i2c_data.msgs[0].addr = mstarddc_addr;
187 i2c_data.msgs[0].len = 1;
188 i2c_data.msgs[0].flags = 0;
189 i2c_data.msgs[0].buf = cmd;
190 i2c_data.msgs[1].addr = mstarddc_addr;
191 i2c_data.msgs[1].len = readcnt;
192 i2c_data.msgs[1].flags = I2C_M_RD;
193 i2c_data.msgs[1].buf = readarr;
194
195 if (ioctl(mstarddc_fd, I2C_RDWR, &i2c_data) < 0) {
196 msg_perr("Error sending read command: errno %d.\n",
197 errno);
198 ret = -1;
199 }
200 }
201
202 if (!ret && (writecnt || readcnt)) {
203 cmd[0] = MSTARDDC_SPI_END;
204 if (write(mstarddc_fd, cmd, 1) < 0) {
205 msg_perr("Error sending end command: errno %d.\n",
206 errno);
207 ret = -1;
208 }
209 }
210
211 /* Do not reset if something went wrong, as it might prevent from
212 * retrying flashing. */
213 if (ret != 0)
214 mstarddc_doreset = 0;
215
216 if (cmd)
217 free(cmd);
218
219 return ret;
220}
221
222static const struct spi_master spi_master_mstarddc = {
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000223 .max_data_read = 256,
224 .max_data_write = 256,
225 .command = mstarddc_spi_send_command,
226 .multicommand = default_spi_send_multicommand,
227 .read = default_spi_read,
228 .write_256 = default_spi_write_256,
229 .write_aai = default_spi_write_aai,
230};
231
Thomas Heijligencc853d82021-05-04 15:32:17 +0200232const struct programmer_entry programmer_mstarddc_spi = {
233 .name = "mstarddc_spi",
234 .type = OTHER,
235 .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n",
236 .init = mstarddc_spi_init,
237 .map_flash_region = fallback_map,
238 .unmap_flash_region = fallback_unmap,
239 .delay = internal_delay,
240};
241
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000242#endif