Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 1 | /* |
| 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 Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 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 | |
| 34 | static const struct spi_master spi_master_mstarddc; |
| 35 | |
| 36 | static int mstarddc_fd; |
| 37 | static int mstarddc_addr; |
| 38 | static int mstarddc_doreset = 1; |
| 39 | |
| 40 | // MSTAR DDC Commands |
| 41 | #define MSTARDDC_SPI_WRITE 0x10 |
| 42 | #define MSTARDDC_SPI_READ 0x11 |
| 43 | #define MSTARDDC_SPI_END 0x12 |
| 44 | #define MSTARDDC_SPI_RESET 0x24 |
| 45 | |
| 46 | /* Returns 0 upon success, a negative number upon errors. */ |
| 47 | static int mstarddc_spi_shutdown(void *data) |
| 48 | { |
| 49 | // Reset, disables ISP mode |
| 50 | if (mstarddc_doreset == 1) { |
| 51 | uint8_t cmd = MSTARDDC_SPI_RESET; |
| 52 | if (write(mstarddc_fd, &cmd, 1) < 0) { |
| 53 | msg_perr("Error sending reset command: errno %d.\n", |
| 54 | errno); |
| 55 | return -1; |
| 56 | } |
| 57 | } else { |
| 58 | msg_pinfo("Info: Reset command was not sent. " |
| 59 | "Either the noreset=1 option was used, " |
Elyes HAOUAS | e2c90c4 | 2018-08-18 09:04:41 +0200 | [diff] [blame] | 60 | "or an error occurred.\n"); |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | if (close(mstarddc_fd) < 0) { |
| 64 | msg_perr("Error closing device: errno %d.\n", errno); |
| 65 | return -1; |
| 66 | } |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | /* Returns 0 upon success, a negative number upon errors. */ |
| 71 | int mstarddc_spi_init(void) |
| 72 | { |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 73 | int ret = 0; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 74 | |
| 75 | // Get device, address from command-line |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 76 | char *i2c_device = extract_programmer_param("dev"); |
| 77 | if (i2c_device != NULL && strlen(i2c_device) > 0) { |
| 78 | char *i2c_address = strchr(i2c_device, ':'); |
| 79 | if (i2c_address != NULL) { |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 80 | *i2c_address = '\0'; |
| 81 | i2c_address++; |
| 82 | } |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 83 | if (i2c_address == NULL || strlen(i2c_address) == 0) { |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 84 | msg_perr("Error: no address specified.\n" |
| 85 | "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n"); |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 86 | ret = -1; |
| 87 | goto out; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 88 | } |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 89 | mstarddc_addr = strtol(i2c_address, NULL, 16); // FIXME: error handling |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 90 | } else { |
| 91 | msg_perr("Error: no device specified.\n" |
| 92 | "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n"); |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 93 | ret = -1; |
| 94 | goto out; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 95 | } |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 96 | msg_pinfo("Info: Will try to use device %s and address 0x%02x.\n", i2c_device, mstarddc_addr); |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 97 | |
| 98 | // Get noreset=1 option from command-line |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 99 | char *noreset = extract_programmer_param("noreset"); |
| 100 | if (noreset != NULL && noreset[0] == '1') |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 101 | mstarddc_doreset = 0; |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 102 | free(noreset); |
| 103 | msg_pinfo("Info: Will %sreset the device at the end.\n", mstarddc_doreset ? "" : "NOT "); |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 104 | // Open device |
| 105 | if ((mstarddc_fd = open(i2c_device, O_RDWR)) < 0) { |
| 106 | switch (errno) { |
| 107 | case EACCES: |
| 108 | msg_perr("Error opening %s: Permission denied.\n" |
| 109 | "Please use sudo or run as root.\n", |
| 110 | i2c_device); |
| 111 | break; |
| 112 | case ENOENT: |
| 113 | msg_perr("Error opening %s: No such file.\n" |
| 114 | "Please check you specified the correct device.\n", |
| 115 | i2c_device); |
| 116 | break; |
| 117 | default: |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 118 | msg_perr("Error opening %s: %s.\n", i2c_device, strerror(errno)); |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 119 | } |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 120 | ret = -1; |
| 121 | goto out; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 122 | } |
| 123 | // Set slave address |
| 124 | if (ioctl(mstarddc_fd, I2C_SLAVE, mstarddc_addr) < 0) { |
| 125 | msg_perr("Error setting slave address 0x%02x: errno %d.\n", |
| 126 | mstarddc_addr, errno); |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 127 | ret = -1; |
| 128 | goto out; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 129 | } |
| 130 | // Enable ISP mode |
| 131 | uint8_t cmd[5] = { 'M', 'S', 'T', 'A', 'R' }; |
| 132 | if (write(mstarddc_fd, cmd, 5) < 0) { |
| 133 | int enable_err = errno; |
| 134 | uint8_t end_cmd = MSTARDDC_SPI_END; |
| 135 | |
| 136 | // Assume device is already in ISP mode, try to send END command |
| 137 | if (write(mstarddc_fd, &end_cmd, 1) < 0) { |
| 138 | msg_perr("Error enabling ISP mode: errno %d & %d.\n" |
| 139 | "Please check that device (%s) and address (0x%02x) are correct.\n", |
| 140 | enable_err, errno, i2c_device, mstarddc_addr); |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 141 | ret = -1; |
| 142 | goto out; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | // Register shutdown function |
| 146 | register_shutdown(mstarddc_spi_shutdown, NULL); |
| 147 | |
| 148 | // Register programmer |
| 149 | register_spi_master(&spi_master_mstarddc); |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 150 | out: |
| 151 | free(i2c_device); |
| 152 | return ret; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* Returns 0 upon success, a negative number upon errors. */ |
| 156 | static 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; |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 163 | uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t)); |
| 164 | if (cmd == NULL) { |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 165 | msg_perr("Error allocating memory: errno %d.\n", errno); |
| 166 | ret = -1; |
| 167 | } |
| 168 | |
| 169 | if (!ret && writecnt) { |
| 170 | cmd[0] = MSTARDDC_SPI_WRITE; |
| 171 | memcpy(cmd + 1, writearr, writecnt); |
| 172 | if (write(mstarddc_fd, cmd, writecnt + 1) < 0) { |
| 173 | msg_perr("Error sending write command: errno %d.\n", |
| 174 | errno); |
| 175 | ret = -1; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (!ret && readcnt) { |
| 180 | struct i2c_rdwr_ioctl_data i2c_data; |
| 181 | struct i2c_msg msg[2]; |
| 182 | |
| 183 | cmd[0] = MSTARDDC_SPI_READ; |
| 184 | i2c_data.nmsgs = 2; |
| 185 | i2c_data.msgs = msg; |
| 186 | i2c_data.msgs[0].addr = mstarddc_addr; |
| 187 | i2c_data.msgs[0].len = 1; |
| 188 | i2c_data.msgs[0].flags = 0; |
| 189 | i2c_data.msgs[0].buf = cmd; |
| 190 | i2c_data.msgs[1].addr = mstarddc_addr; |
| 191 | i2c_data.msgs[1].len = readcnt; |
| 192 | i2c_data.msgs[1].flags = I2C_M_RD; |
| 193 | i2c_data.msgs[1].buf = readarr; |
| 194 | |
| 195 | if (ioctl(mstarddc_fd, I2C_RDWR, &i2c_data) < 0) { |
| 196 | msg_perr("Error sending read command: errno %d.\n", |
| 197 | errno); |
| 198 | ret = -1; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (!ret && (writecnt || readcnt)) { |
| 203 | cmd[0] = MSTARDDC_SPI_END; |
| 204 | if (write(mstarddc_fd, cmd, 1) < 0) { |
| 205 | msg_perr("Error sending end command: errno %d.\n", |
| 206 | errno); |
| 207 | ret = -1; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* Do not reset if something went wrong, as it might prevent from |
| 212 | * retrying flashing. */ |
| 213 | if (ret != 0) |
| 214 | mstarddc_doreset = 0; |
| 215 | |
| 216 | if (cmd) |
| 217 | free(cmd); |
| 218 | |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | static const struct spi_master spi_master_mstarddc = { |
| 223 | .type = SPI_CONTROLLER_MSTARDDC, |
| 224 | .max_data_read = 256, |
| 225 | .max_data_write = 256, |
| 226 | .command = mstarddc_spi_send_command, |
| 227 | .multicommand = default_spi_send_multicommand, |
| 228 | .read = default_spi_read, |
| 229 | .write_256 = default_spi_write_256, |
| 230 | .write_aai = default_spi_write_aai, |
| 231 | }; |
| 232 | |
| 233 | #endif |