blob: cf00898145ce1e58b6d63f5286f00a8e34e8cabf [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 }
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000145
146 // Register programmer
Nico Huber5e08e3e2021-05-11 17:38:14 +0200147 register_spi_master(&spi_master_mstarddc, NULL);
Stefan Tauneradadca62015-02-18 23:28:30 +0000148out:
149 free(i2c_device);
150 return ret;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000151}
152
153/* Returns 0 upon success, a negative number upon errors. */
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000154static int mstarddc_spi_send_command(const struct flashctx *flash,
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000155 unsigned int writecnt,
156 unsigned int readcnt,
157 const unsigned char *writearr,
158 unsigned char *readarr)
159{
160 int ret = 0;
Stefan Tauneradadca62015-02-18 23:28:30 +0000161 uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t));
162 if (cmd == NULL) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000163 msg_perr("Error allocating memory: errno %d.\n", errno);
164 ret = -1;
165 }
166
167 if (!ret && writecnt) {
168 cmd[0] = MSTARDDC_SPI_WRITE;
169 memcpy(cmd + 1, writearr, writecnt);
170 if (write(mstarddc_fd, cmd, writecnt + 1) < 0) {
171 msg_perr("Error sending write command: errno %d.\n",
172 errno);
173 ret = -1;
174 }
175 }
176
177 if (!ret && readcnt) {
178 struct i2c_rdwr_ioctl_data i2c_data;
179 struct i2c_msg msg[2];
180
181 cmd[0] = MSTARDDC_SPI_READ;
182 i2c_data.nmsgs = 2;
183 i2c_data.msgs = msg;
184 i2c_data.msgs[0].addr = mstarddc_addr;
185 i2c_data.msgs[0].len = 1;
186 i2c_data.msgs[0].flags = 0;
187 i2c_data.msgs[0].buf = cmd;
188 i2c_data.msgs[1].addr = mstarddc_addr;
189 i2c_data.msgs[1].len = readcnt;
190 i2c_data.msgs[1].flags = I2C_M_RD;
191 i2c_data.msgs[1].buf = readarr;
192
193 if (ioctl(mstarddc_fd, I2C_RDWR, &i2c_data) < 0) {
194 msg_perr("Error sending read command: errno %d.\n",
195 errno);
196 ret = -1;
197 }
198 }
199
200 if (!ret && (writecnt || readcnt)) {
201 cmd[0] = MSTARDDC_SPI_END;
202 if (write(mstarddc_fd, cmd, 1) < 0) {
203 msg_perr("Error sending end command: errno %d.\n",
204 errno);
205 ret = -1;
206 }
207 }
208
209 /* Do not reset if something went wrong, as it might prevent from
210 * retrying flashing. */
211 if (ret != 0)
212 mstarddc_doreset = 0;
213
214 if (cmd)
215 free(cmd);
216
217 return ret;
218}
219
220static const struct spi_master spi_master_mstarddc = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200221 .max_data_read = 256,
222 .max_data_write = 256,
223 .command = mstarddc_spi_send_command,
224 .multicommand = default_spi_send_multicommand,
225 .read = default_spi_read,
226 .write_256 = default_spi_write_256,
227 .write_aai = default_spi_write_aai,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +1000228 .shutdown = mstarddc_spi_shutdown,
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000229};
230
Thomas Heijligencc853d82021-05-04 15:32:17 +0200231const struct programmer_entry programmer_mstarddc_spi = {
232 .name = "mstarddc_spi",
233 .type = OTHER,
234 .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n",
235 .init = mstarddc_spi_init,
236 .map_flash_region = fallback_map,
237 .unmap_flash_region = fallback_unmap,
238 .delay = internal_delay,
239};
240
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000241#endif