blob: 0b41a1b7453990d257d6a2673909e3eb106abe72 [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{
77 char *i2c_device;
78 char *i2c_address;
79 char *noreset;
80
81 // Get device, address from command-line
82 i2c_device = extract_programmer_param("dev");
83 if (i2c_device && strlen(i2c_device)) {
84 if ((i2c_address = strchr(i2c_device, ':'))) {
85 *i2c_address = '\0';
86 i2c_address++;
87 }
88 if (!i2c_address || !strlen(i2c_address)) {
89 msg_perr("Error: no address specified.\n"
90 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
91 return -1;
92 }
93 mstarddc_addr = strtol(i2c_address, NULL, 16);
94 } else {
95 msg_perr("Error: no device specified.\n"
96 "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n");
97 return -1;
98 }
99 msg_pinfo("Info: Will try to use device %s and address 0x%02x.\n",
100 i2c_device, mstarddc_addr);
101
102 // Get noreset=1 option from command-line
103 if ((noreset = extract_programmer_param("noreset"))
104 && noreset[0] == '1')
105 mstarddc_doreset = 0;
106 msg_pinfo("Info: WILL %sreset the device at the end.\n",
107 mstarddc_doreset ? "" : "NOT ");
108 // 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:
122 msg_perr("Error opening %s: errno %d.\n",
123 i2c_device, errno);
124 }
125 return -1;
126 }
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);
131 return -1;
132 }
133 // Enable ISP mode
134 uint8_t cmd[5] = { 'M', 'S', 'T', 'A', 'R' };
135 if (write(mstarddc_fd, cmd, 5) < 0) {
136 int enable_err = errno;
137 uint8_t end_cmd = MSTARDDC_SPI_END;
138
139 // Assume device is already in ISP mode, try to send END command
140 if (write(mstarddc_fd, &end_cmd, 1) < 0) {
141 msg_perr("Error enabling ISP mode: errno %d & %d.\n"
142 "Please check that device (%s) and address (0x%02x) are correct.\n",
143 enable_err, errno, i2c_device, mstarddc_addr);
144 return -1;
145 }
146 }
147 // Register shutdown function
148 register_shutdown(mstarddc_spi_shutdown, NULL);
149
150 // Register programmer
151 register_spi_master(&spi_master_mstarddc);
152 return 0;
153}
154
155/* Returns 0 upon success, a negative number upon errors. */
156static int mstarddc_spi_send_command(struct flashctx *flash,
157 unsigned int writecnt,
158 unsigned int readcnt,
159 const unsigned char *writearr,
160 unsigned char *readarr)
161{
162 int ret = 0;
163 uint8_t *cmd;
164
165 if ((cmd = malloc((writecnt + 1) * sizeof(uint8_t))) < 0) {
166 msg_perr("Error allocating memory: errno %d.\n", errno);
167 ret = -1;
168 }
169
170 if (!ret && writecnt) {
171 cmd[0] = MSTARDDC_SPI_WRITE;
172 memcpy(cmd + 1, writearr, writecnt);
173 if (write(mstarddc_fd, cmd, writecnt + 1) < 0) {
174 msg_perr("Error sending write command: errno %d.\n",
175 errno);
176 ret = -1;
177 }
178 }
179
180 if (!ret && readcnt) {
181 struct i2c_rdwr_ioctl_data i2c_data;
182 struct i2c_msg msg[2];
183
184 cmd[0] = MSTARDDC_SPI_READ;
185 i2c_data.nmsgs = 2;
186 i2c_data.msgs = msg;
187 i2c_data.msgs[0].addr = mstarddc_addr;
188 i2c_data.msgs[0].len = 1;
189 i2c_data.msgs[0].flags = 0;
190 i2c_data.msgs[0].buf = cmd;
191 i2c_data.msgs[1].addr = mstarddc_addr;
192 i2c_data.msgs[1].len = readcnt;
193 i2c_data.msgs[1].flags = I2C_M_RD;
194 i2c_data.msgs[1].buf = readarr;
195
196 if (ioctl(mstarddc_fd, I2C_RDWR, &i2c_data) < 0) {
197 msg_perr("Error sending read command: errno %d.\n",
198 errno);
199 ret = -1;
200 }
201 }
202
203 if (!ret && (writecnt || readcnt)) {
204 cmd[0] = MSTARDDC_SPI_END;
205 if (write(mstarddc_fd, cmd, 1) < 0) {
206 msg_perr("Error sending end command: errno %d.\n",
207 errno);
208 ret = -1;
209 }
210 }
211
212 /* Do not reset if something went wrong, as it might prevent from
213 * retrying flashing. */
214 if (ret != 0)
215 mstarddc_doreset = 0;
216
217 if (cmd)
218 free(cmd);
219
220 return ret;
221}
222
223static const struct spi_master spi_master_mstarddc = {
224 .type = SPI_CONTROLLER_MSTARDDC,
225 .max_data_read = 256,
226 .max_data_write = 256,
227 .command = mstarddc_spi_send_command,
228 .multicommand = default_spi_send_multicommand,
229 .read = default_spi_read,
230 .write_256 = default_spi_write_256,
231 .write_aai = default_spi_write_aai,
232};
233
234#endif