blob: c55d7432e848fb0d3fa51862680a8c26b18a058b [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. */
Nico Hubere3a26882023-01-11 21:45:51 +010069static int mstarddc_spi_init(struct flashprog_programmer *const prog)
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"
Nico Huberc3b02dc2023-08-12 01:13:45 +020083 "Use flashprog -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"
Nico Huberc3b02dc2023-08-12 01:13:45 +020090 "Use flashprog -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 Huber89569d62023-01-12 23:31:40 +0100145 register_spi_master(&spi_master_mstarddc, 0, 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. */
Nico Huber610c1aa2023-02-15 02:56:05 +0100152static int mstarddc_spi_send_command(
153 const struct spi_master *mst, unsigned int writecnt, unsigned int readcnt,
154 const unsigned char *writearr, unsigned char *readarr)
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000155{
156 int ret = 0;
Stefan Tauneradadca62015-02-18 23:28:30 +0000157 uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t));
158 if (cmd == NULL) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000159 msg_perr("Error allocating memory: errno %d.\n", errno);
160 ret = -1;
161 }
162
163 if (!ret && writecnt) {
164 cmd[0] = MSTARDDC_SPI_WRITE;
165 memcpy(cmd + 1, writearr, writecnt);
166 if (write(mstarddc_fd, cmd, writecnt + 1) < 0) {
167 msg_perr("Error sending write command: errno %d.\n",
168 errno);
169 ret = -1;
170 }
171 }
172
173 if (!ret && readcnt) {
174 struct i2c_rdwr_ioctl_data i2c_data;
175 struct i2c_msg msg[2];
176
177 cmd[0] = MSTARDDC_SPI_READ;
178 i2c_data.nmsgs = 2;
179 i2c_data.msgs = msg;
180 i2c_data.msgs[0].addr = mstarddc_addr;
181 i2c_data.msgs[0].len = 1;
182 i2c_data.msgs[0].flags = 0;
183 i2c_data.msgs[0].buf = cmd;
184 i2c_data.msgs[1].addr = mstarddc_addr;
185 i2c_data.msgs[1].len = readcnt;
186 i2c_data.msgs[1].flags = I2C_M_RD;
187 i2c_data.msgs[1].buf = readarr;
188
189 if (ioctl(mstarddc_fd, I2C_RDWR, &i2c_data) < 0) {
190 msg_perr("Error sending read command: errno %d.\n",
191 errno);
192 ret = -1;
193 }
194 }
195
196 if (!ret && (writecnt || readcnt)) {
197 cmd[0] = MSTARDDC_SPI_END;
198 if (write(mstarddc_fd, cmd, 1) < 0) {
199 msg_perr("Error sending end command: errno %d.\n",
200 errno);
201 ret = -1;
202 }
203 }
204
205 /* Do not reset if something went wrong, as it might prevent from
206 * retrying flashing. */
207 if (ret != 0)
208 mstarddc_doreset = 0;
209
210 if (cmd)
211 free(cmd);
212
213 return ret;
214}
215
216static const struct spi_master spi_master_mstarddc = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200217 .max_data_read = 256,
218 .max_data_write = 256,
219 .command = mstarddc_spi_send_command,
220 .multicommand = default_spi_send_multicommand,
221 .read = default_spi_read,
222 .write_256 = default_spi_write_256,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +1000223 .shutdown = mstarddc_spi_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +0530224 .probe_opcode = default_spi_probe_opcode,
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000225};
226
Thomas Heijligencc853d82021-05-04 15:32:17 +0200227const struct programmer_entry programmer_mstarddc_spi = {
228 .name = "mstarddc_spi",
229 .type = OTHER,
230 .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n",
231 .init = mstarddc_spi_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +0200232};