blob: 809d69049e8d723120e345fdee3847d03693ef04 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#if CONFIG_MSTARDDC_SPI == 1
21
22#include <stdio.h>
23#include <string.h>
24#include <stdlib.h>
25#include <ctype.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <errno.h>
31#include <sys/ioctl.h>
32#include <linux/i2c-dev.h>
33#include <linux/i2c.h>
34#include "flash.h"
35#include "programmer.h"
36#include "spi.h"
37
38static const struct spi_master spi_master_mstarddc;
39
40static int mstarddc_fd;
41static int mstarddc_addr;
42static int mstarddc_doreset = 1;
43
44// MSTAR DDC Commands
45#define MSTARDDC_SPI_WRITE 0x10
46#define MSTARDDC_SPI_READ 0x11
47#define MSTARDDC_SPI_END 0x12
48#define MSTARDDC_SPI_RESET 0x24
49
50/* Returns 0 upon success, a negative number upon errors. */
51static int mstarddc_spi_shutdown(void *data)
52{
53 // Reset, disables ISP mode
54 if (mstarddc_doreset == 1) {
55 uint8_t cmd = MSTARDDC_SPI_RESET;
56 if (write(mstarddc_fd, &cmd, 1) < 0) {
57 msg_perr("Error sending reset command: errno %d.\n",
58 errno);
59 return -1;
60 }
61 } else {
62 msg_pinfo("Info: Reset command was not sent. "
63 "Either the noreset=1 option was used, "
64 "or an error occured.\n");
65 }
66
67 if (close(mstarddc_fd) < 0) {
68 msg_perr("Error closing device: errno %d.\n", errno);
69 return -1;
70 }
71 return 0;
72}
73
74/* Returns 0 upon success, a negative number upon errors. */
75int mstarddc_spi_init(void)
76{
Stefan Tauneradadca62015-02-18 23:28:30 +000077 int ret = 0;
Alexandre Boeglin80e64712014-12-20 20:25:19 +000078
79 // Get device, address from command-line
Stefan Tauneradadca62015-02-18 23:28:30 +000080 char *i2c_device = extract_programmer_param("dev");
81 if (i2c_device != NULL && strlen(i2c_device) > 0) {
82 char *i2c_address = strchr(i2c_device, ':');
83 if (i2c_address != NULL) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +000084 *i2c_address = '\0';
85 i2c_address++;
86 }
Stefan Tauneradadca62015-02-18 23:28:30 +000087 if (i2c_address == NULL || strlen(i2c_address) == 0) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +000088 msg_perr("Error: no address specified.\n"
89 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
Stefan Tauneradadca62015-02-18 23:28:30 +000090 ret = -1;
91 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +000092 }
Stefan Tauneradadca62015-02-18 23:28:30 +000093 mstarddc_addr = strtol(i2c_address, NULL, 16); // FIXME: error handling
Alexandre Boeglin80e64712014-12-20 20:25:19 +000094 } else {
95 msg_perr("Error: no device specified.\n"
96 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
Stefan Tauneradadca62015-02-18 23:28:30 +000097 ret = -1;
98 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +000099 }
Stefan Tauneradadca62015-02-18 23:28:30 +0000100 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 +0000101
102 // Get noreset=1 option from command-line
Stefan Tauneradadca62015-02-18 23:28:30 +0000103 char *noreset = extract_programmer_param("noreset");
104 if (noreset != NULL && noreset[0] == '1')
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000105 mstarddc_doreset = 0;
Stefan Tauneradadca62015-02-18 23:28:30 +0000106 free(noreset);
107 msg_pinfo("Info: Will %sreset the device at the end.\n", mstarddc_doreset ? "" : "NOT ");
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000108 // Open device
109 if ((mstarddc_fd = open(i2c_device, O_RDWR)) < 0) {
110 switch (errno) {
111 case EACCES:
112 msg_perr("Error opening %s: Permission denied.\n"
113 "Please use sudo or run as root.\n",
114 i2c_device);
115 break;
116 case ENOENT:
117 msg_perr("Error opening %s: No such file.\n"
118 "Please check you specified the correct device.\n",
119 i2c_device);
120 break;
121 default:
Stefan Tauneradadca62015-02-18 23:28:30 +0000122 msg_perr("Error opening %s: %s.\n", i2c_device, strerror(errno));
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000123 }
Stefan Tauneradadca62015-02-18 23:28:30 +0000124 ret = -1;
125 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000126 }
127 // Set slave address
128 if (ioctl(mstarddc_fd, I2C_SLAVE, mstarddc_addr) < 0) {
129 msg_perr("Error setting slave address 0x%02x: errno %d.\n",
130 mstarddc_addr, errno);
Stefan Tauneradadca62015-02-18 23:28:30 +0000131 ret = -1;
132 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000133 }
134 // Enable ISP mode
135 uint8_t cmd[5] = { 'M', 'S', 'T', 'A', 'R' };
136 if (write(mstarddc_fd, cmd, 5) < 0) {
137 int enable_err = errno;
138 uint8_t end_cmd = MSTARDDC_SPI_END;
139
140 // Assume device is already in ISP mode, try to send END command
141 if (write(mstarddc_fd, &end_cmd, 1) < 0) {
142 msg_perr("Error enabling ISP mode: errno %d & %d.\n"
143 "Please check that device (%s) and address (0x%02x) are correct.\n",
144 enable_err, errno, i2c_device, mstarddc_addr);
Stefan Tauneradadca62015-02-18 23:28:30 +0000145 ret = -1;
146 goto out;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000147 }
148 }
149 // Register shutdown function
150 register_shutdown(mstarddc_spi_shutdown, NULL);
151
152 // Register programmer
153 register_spi_master(&spi_master_mstarddc);
Stefan Tauneradadca62015-02-18 23:28:30 +0000154out:
155 free(i2c_device);
156 return ret;
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000157}
158
159/* Returns 0 upon success, a negative number upon errors. */
160static int mstarddc_spi_send_command(struct flashctx *flash,
161 unsigned int writecnt,
162 unsigned int readcnt,
163 const unsigned char *writearr,
164 unsigned char *readarr)
165{
166 int ret = 0;
Stefan Tauneradadca62015-02-18 23:28:30 +0000167 uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t));
168 if (cmd == NULL) {
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000169 msg_perr("Error allocating memory: errno %d.\n", errno);
170 ret = -1;
171 }
172
173 if (!ret && writecnt) {
174 cmd[0] = MSTARDDC_SPI_WRITE;
175 memcpy(cmd + 1, writearr, writecnt);
176 if (write(mstarddc_fd, cmd, writecnt + 1) < 0) {
177 msg_perr("Error sending write command: errno %d.\n",
178 errno);
179 ret = -1;
180 }
181 }
182
183 if (!ret && readcnt) {
184 struct i2c_rdwr_ioctl_data i2c_data;
185 struct i2c_msg msg[2];
186
187 cmd[0] = MSTARDDC_SPI_READ;
188 i2c_data.nmsgs = 2;
189 i2c_data.msgs = msg;
190 i2c_data.msgs[0].addr = mstarddc_addr;
191 i2c_data.msgs[0].len = 1;
192 i2c_data.msgs[0].flags = 0;
193 i2c_data.msgs[0].buf = cmd;
194 i2c_data.msgs[1].addr = mstarddc_addr;
195 i2c_data.msgs[1].len = readcnt;
196 i2c_data.msgs[1].flags = I2C_M_RD;
197 i2c_data.msgs[1].buf = readarr;
198
199 if (ioctl(mstarddc_fd, I2C_RDWR, &i2c_data) < 0) {
200 msg_perr("Error sending read command: errno %d.\n",
201 errno);
202 ret = -1;
203 }
204 }
205
206 if (!ret && (writecnt || readcnt)) {
207 cmd[0] = MSTARDDC_SPI_END;
208 if (write(mstarddc_fd, cmd, 1) < 0) {
209 msg_perr("Error sending end command: errno %d.\n",
210 errno);
211 ret = -1;
212 }
213 }
214
215 /* Do not reset if something went wrong, as it might prevent from
216 * retrying flashing. */
217 if (ret != 0)
218 mstarddc_doreset = 0;
219
220 if (cmd)
221 free(cmd);
222
223 return ret;
224}
225
226static const struct spi_master spi_master_mstarddc = {
227 .type = SPI_CONTROLLER_MSTARDDC,
228 .max_data_read = 256,
229 .max_data_write = 256,
230 .command = mstarddc_spi_send_command,
231 .multicommand = default_spi_send_multicommand,
232 .read = default_spi_read,
233 .write_256 = default_spi_write_256,
234 .write_aai = default_spi_write_aai,
235};
236
237#endif