blob: c041bd3395b1c8e243bdc1852be3c20180bb9e64 [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
Alexandre Boeglin80e64712014-12-20 20:25:19 +000034static 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. */
Anastasia Klimchuke7045832021-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. */
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000146int mstarddc_spi_init(void)
147{
Stefan Tauneradadca62015-02-18 23:28:30 +0000148 int ret = 0;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000149
150 // Get device, address from command-line
Stefan Tauneradadca62015-02-18 23:28:30 +0000151 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) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000155 *i2c_address = '\0';
156 i2c_address++;
157 }
Stefan Tauneradadca62015-02-18 23:28:30 +0000158 if (i2c_address == NULL || strlen(i2c_address) == 0) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000159 msg_perr("Error: no address specified.\n"
160 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
Stefan Tauneradadca62015-02-18 23:28:30 +0000161 ret = -1;
162 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000163 }
Stefan Tauneradadca62015-02-18 23:28:30 +0000164 mstarddc_addr = strtol(i2c_address, NULL, 16); // FIXME: error handling
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000165 } else {
166 msg_perr("Error: no device specified.\n"
167 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
Stefan Tauneradadca62015-02-18 23:28:30 +0000168 ret = -1;
169 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000170 }
Stefan Tauneradadca62015-02-18 23:28:30 +0000171 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 +0000172
173 // Get noreset=1 option from command-line
Stefan Tauneradadca62015-02-18 23:28:30 +0000174 char *noreset = extract_programmer_param("noreset");
175 if (noreset != NULL && noreset[0] == '1')
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000176 mstarddc_doreset = 0;
Stefan Tauneradadca62015-02-18 23:28:30 +0000177 free(noreset);
178 msg_pinfo("Info: Will %sreset the device at the end.\n", mstarddc_doreset ? "" : "NOT ");
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000179 // 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:
Stefan Tauneradadca62015-02-18 23:28:30 +0000193 msg_perr("Error opening %s: %s.\n", i2c_device, strerror(errno));
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000194 }
Stefan Tauneradadca62015-02-18 23:28:30 +0000195 ret = -1;
196 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000197 }
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);
Stefan Tauneradadca62015-02-18 23:28:30 +0000202 ret = -1;
203 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000204 }
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);
Stefan Tauneradadca62015-02-18 23:28:30 +0000216 ret = -1;
217 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000218 }
219 }
220 // Register shutdown function
221 register_shutdown(mstarddc_spi_shutdown, NULL);
222
223 // Register programmer
224 register_spi_master(&spi_master_mstarddc);
Stefan Tauneradadca62015-02-18 23:28:30 +0000225out:
226 free(i2c_device);
227 return ret;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000228}
229
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000230#endif