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. */ |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 71 | static int mstarddc_spi_init(void) |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 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 | } |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 145 | |
| 146 | // Register programmer |
Nico Huber | 5e08e3e | 2021-05-11 17:38:14 +0200 | [diff] [blame] | 147 | register_spi_master(&spi_master_mstarddc, NULL); |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 148 | out: |
| 149 | free(i2c_device); |
| 150 | return ret; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* Returns 0 upon success, a negative number upon errors. */ |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 154 | static int mstarddc_spi_send_command(const struct flashctx *flash, |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 155 | unsigned int writecnt, |
| 156 | unsigned int readcnt, |
| 157 | const unsigned char *writearr, |
| 158 | unsigned char *readarr) |
| 159 | { |
| 160 | int ret = 0; |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 161 | uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t)); |
| 162 | if (cmd == NULL) { |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 163 | msg_perr("Error allocating memory: errno %d.\n", errno); |
| 164 | ret = -1; |
| 165 | } |
| 166 | |
| 167 | if (!ret && writecnt) { |
| 168 | cmd[0] = MSTARDDC_SPI_WRITE; |
| 169 | memcpy(cmd + 1, writearr, writecnt); |
| 170 | if (write(mstarddc_fd, cmd, writecnt + 1) < 0) { |
| 171 | msg_perr("Error sending write command: errno %d.\n", |
| 172 | errno); |
| 173 | ret = -1; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (!ret && readcnt) { |
| 178 | struct i2c_rdwr_ioctl_data i2c_data; |
| 179 | struct i2c_msg msg[2]; |
| 180 | |
| 181 | cmd[0] = MSTARDDC_SPI_READ; |
| 182 | i2c_data.nmsgs = 2; |
| 183 | i2c_data.msgs = msg; |
| 184 | i2c_data.msgs[0].addr = mstarddc_addr; |
| 185 | i2c_data.msgs[0].len = 1; |
| 186 | i2c_data.msgs[0].flags = 0; |
| 187 | i2c_data.msgs[0].buf = cmd; |
| 188 | i2c_data.msgs[1].addr = mstarddc_addr; |
| 189 | i2c_data.msgs[1].len = readcnt; |
| 190 | i2c_data.msgs[1].flags = I2C_M_RD; |
| 191 | i2c_data.msgs[1].buf = readarr; |
| 192 | |
| 193 | if (ioctl(mstarddc_fd, I2C_RDWR, &i2c_data) < 0) { |
| 194 | msg_perr("Error sending read command: errno %d.\n", |
| 195 | errno); |
| 196 | ret = -1; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (!ret && (writecnt || readcnt)) { |
| 201 | cmd[0] = MSTARDDC_SPI_END; |
| 202 | if (write(mstarddc_fd, cmd, 1) < 0) { |
| 203 | msg_perr("Error sending end command: errno %d.\n", |
| 204 | errno); |
| 205 | ret = -1; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* Do not reset if something went wrong, as it might prevent from |
| 210 | * retrying flashing. */ |
| 211 | if (ret != 0) |
| 212 | mstarddc_doreset = 0; |
| 213 | |
| 214 | if (cmd) |
| 215 | free(cmd); |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static const struct spi_master spi_master_mstarddc = { |
Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 221 | .max_data_read = 256, |
| 222 | .max_data_write = 256, |
| 223 | .command = mstarddc_spi_send_command, |
| 224 | .multicommand = default_spi_send_multicommand, |
| 225 | .read = default_spi_read, |
| 226 | .write_256 = default_spi_write_256, |
| 227 | .write_aai = default_spi_write_aai, |
Anastasia Klimchuk | c63d918 | 2021-07-06 16:18:44 +1000 | [diff] [blame] | 228 | .shutdown = mstarddc_spi_shutdown, |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 231 | const struct programmer_entry programmer_mstarddc_spi = { |
| 232 | .name = "mstarddc_spi", |
| 233 | .type = OTHER, |
| 234 | .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n", |
| 235 | .init = mstarddc_spi_init, |
| 236 | .map_flash_region = fallback_map, |
| 237 | .unmap_flash_region = fallback_unmap, |
| 238 | .delay = internal_delay, |
| 239 | }; |
| 240 | |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 241 | #endif |