blob: 32b77cdd6c74176d0d0a4434d2d96e3697b9deb1 [file] [log] [blame]
Edward O'Callaghan611253c2019-09-09 00:22:07 +10001/*
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.
14 */
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
Edward O'Callaghan611253c2019-09-09 00:22:07 +100034static 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, "
58 "or an error occurred.\n");
59 }
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. */
Anastasia Klimchuk54cc6b52021-03-23 16:34:07 +110069static int mstarddc_spi_send_command(const struct flashctx *flash,
70 unsigned int writecnt,
71 unsigned int readcnt,
72 const unsigned char *writearr,
73 unsigned char *readarr)
74{
75 int ret = 0;
76 uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t));
77 if (cmd == NULL) {
78 msg_perr("Error allocating memory: errno %d.\n", errno);
79 ret = -1;
80 }
81
82 if (!ret && writecnt) {
83 cmd[0] = MSTARDDC_SPI_WRITE;
84 memcpy(cmd + 1, writearr, writecnt);
85 if (write(mstarddc_fd, cmd, writecnt + 1) < 0) {
86 msg_perr("Error sending write command: errno %d.\n",
87 errno);
88 ret = -1;
89 }
90 }
91
92 if (!ret && readcnt) {
93 struct i2c_rdwr_ioctl_data i2c_data;
94 struct i2c_msg msg[2];
95
96 cmd[0] = MSTARDDC_SPI_READ;
97 i2c_data.nmsgs = 2;
98 i2c_data.msgs = msg;
99 i2c_data.msgs[0].addr = mstarddc_addr;
100 i2c_data.msgs[0].len = 1;
101 i2c_data.msgs[0].flags = 0;
102 i2c_data.msgs[0].buf = cmd;
103 i2c_data.msgs[1].addr = mstarddc_addr;
104 i2c_data.msgs[1].len = readcnt;
105 i2c_data.msgs[1].flags = I2C_M_RD;
106 i2c_data.msgs[1].buf = readarr;
107
108 if (ioctl(mstarddc_fd, I2C_RDWR, &i2c_data) < 0) {
109 msg_perr("Error sending read command: errno %d.\n",
110 errno);
111 ret = -1;
112 }
113 }
114
115 if (!ret && (writecnt || readcnt)) {
116 cmd[0] = MSTARDDC_SPI_END;
117 if (write(mstarddc_fd, cmd, 1) < 0) {
118 msg_perr("Error sending end command: errno %d.\n",
119 errno);
120 ret = -1;
121 }
122 }
123
124 /* Do not reset if something went wrong, as it might prevent from
125 * retrying flashing. */
126 if (ret != 0)
127 mstarddc_doreset = 0;
128
129 if (cmd)
130 free(cmd);
131
132 return ret;
133}
134
135static const struct spi_master spi_master_mstarddc = {
136 .max_data_read = 256,
137 .max_data_write = 256,
138 .command = mstarddc_spi_send_command,
139 .multicommand = default_spi_send_multicommand,
140 .read = default_spi_read,
141 .write_256 = default_spi_write_256,
142 .write_aai = default_spi_write_aai,
143};
144
145/* Returns 0 upon success, a negative number upon errors. */
Edward O'Callaghan611253c2019-09-09 00:22:07 +1000146int mstarddc_spi_init(void)
147{
148 int ret = 0;
149
150 // Get device, address from command-line
151 char *i2c_device = extract_programmer_param("dev");
152 if (i2c_device != NULL && strlen(i2c_device) > 0) {
153 char *i2c_address = strchr(i2c_device, ':');
154 if (i2c_address != NULL) {
155 *i2c_address = '\0';
156 i2c_address++;
157 }
158 if (i2c_address == NULL || strlen(i2c_address) == 0) {
159 msg_perr("Error: no address specified.\n"
160 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
161 ret = -1;
162 goto out;
163 }
164 mstarddc_addr = strtol(i2c_address, NULL, 16); // FIXME: error handling
165 } else {
166 msg_perr("Error: no device specified.\n"
167 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
168 ret = -1;
169 goto out;
170 }
171 msg_pinfo("Info: Will try to use device %s and address 0x%02x.\n", i2c_device, mstarddc_addr);
172
173 // Get noreset=1 option from command-line
174 char *noreset = extract_programmer_param("noreset");
175 if (noreset != NULL && noreset[0] == '1')
176 mstarddc_doreset = 0;
177 free(noreset);
178 msg_pinfo("Info: Will %sreset the device at the end.\n", mstarddc_doreset ? "" : "NOT ");
179 // Open device
180 if ((mstarddc_fd = open(i2c_device, O_RDWR)) < 0) {
181 switch (errno) {
182 case EACCES:
183 msg_perr("Error opening %s: Permission denied.\n"
184 "Please use sudo or run as root.\n",
185 i2c_device);
186 break;
187 case ENOENT:
188 msg_perr("Error opening %s: No such file.\n"
189 "Please check you specified the correct device.\n",
190 i2c_device);
191 break;
192 default:
193 msg_perr("Error opening %s: %s.\n", i2c_device, strerror(errno));
194 }
195 ret = -1;
196 goto out;
197 }
198 // Set slave address
199 if (ioctl(mstarddc_fd, I2C_SLAVE, mstarddc_addr) < 0) {
200 msg_perr("Error setting slave address 0x%02x: errno %d.\n",
201 mstarddc_addr, errno);
202 ret = -1;
203 goto out;
204 }
205 // Enable ISP mode
206 uint8_t cmd[5] = { 'M', 'S', 'T', 'A', 'R' };
207 if (write(mstarddc_fd, cmd, 5) < 0) {
208 int enable_err = errno;
209 uint8_t end_cmd = MSTARDDC_SPI_END;
210
211 // Assume device is already in ISP mode, try to send END command
212 if (write(mstarddc_fd, &end_cmd, 1) < 0) {
213 msg_perr("Error enabling ISP mode: errno %d & %d.\n"
214 "Please check that device (%s) and address (0x%02x) are correct.\n",
215 enable_err, errno, i2c_device, mstarddc_addr);
216 ret = -1;
217 goto out;
218 }
219 }
220 // Register shutdown function
221 register_shutdown(mstarddc_spi_shutdown, NULL);
222
223 // Register programmer
Nico Huberf1eeda62021-05-11 17:38:14 +0200224 register_spi_master(&spi_master_mstarddc, NULL);
Edward O'Callaghan611253c2019-09-09 00:22:07 +1000225out:
226 free(i2c_device);
227 return ret;
228}
229
Edward O'Callaghan611253c2019-09-09 00:22:07 +1000230#endif