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 | |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <ctype.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <unistd.h> |
| 24 | #include <errno.h> |
| 25 | #include <sys/ioctl.h> |
| 26 | #include <linux/i2c-dev.h> |
| 27 | #include <linux/i2c.h> |
| 28 | #include "flash.h" |
| 29 | #include "programmer.h" |
| 30 | #include "spi.h" |
| 31 | |
| 32 | static const struct spi_master spi_master_mstarddc; |
| 33 | |
| 34 | static int mstarddc_fd; |
| 35 | static int mstarddc_addr; |
| 36 | static 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. */ |
| 45 | static 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 HAOUAS | e2c90c4 | 2018-08-18 09:04:41 +0200 | [diff] [blame] | 58 | "or an error occurred.\n"); |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 59 | } |
| 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. */ |
Nico Huber | e3a2688 | 2023-01-11 21:45:51 +0100 | [diff] [blame] | 69 | static int mstarddc_spi_init(struct flashprog_programmer *const prog) |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 70 | { |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 71 | int ret = 0; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 72 | |
| 73 | // Get device, address from command-line |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 74 | char *i2c_device = extract_programmer_param("dev"); |
| 75 | if (i2c_device != NULL && strlen(i2c_device) > 0) { |
| 76 | char *i2c_address = strchr(i2c_device, ':'); |
| 77 | if (i2c_address != NULL) { |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 78 | *i2c_address = '\0'; |
| 79 | i2c_address++; |
| 80 | } |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 81 | if (i2c_address == NULL || strlen(i2c_address) == 0) { |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 82 | msg_perr("Error: no address specified.\n" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 83 | "Use flashprog -p mstarddc_spi:dev=/dev/device:address.\n"); |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 84 | ret = -1; |
| 85 | goto out; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 86 | } |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 87 | mstarddc_addr = strtol(i2c_address, NULL, 16); // FIXME: error handling |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 88 | } else { |
| 89 | msg_perr("Error: no device specified.\n" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 90 | "Use flashprog -p mstarddc_spi:dev=/dev/device:address.\n"); |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 91 | ret = -1; |
| 92 | goto out; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 93 | } |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 94 | 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] | 95 | |
| 96 | // Get noreset=1 option from command-line |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 97 | char *noreset = extract_programmer_param("noreset"); |
| 98 | if (noreset != NULL && noreset[0] == '1') |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 99 | mstarddc_doreset = 0; |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 100 | free(noreset); |
| 101 | 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] | 102 | // Open device |
| 103 | if ((mstarddc_fd = open(i2c_device, O_RDWR)) < 0) { |
| 104 | switch (errno) { |
| 105 | case EACCES: |
| 106 | msg_perr("Error opening %s: Permission denied.\n" |
| 107 | "Please use sudo or run as root.\n", |
| 108 | i2c_device); |
| 109 | break; |
| 110 | case ENOENT: |
| 111 | msg_perr("Error opening %s: No such file.\n" |
| 112 | "Please check you specified the correct device.\n", |
| 113 | i2c_device); |
| 114 | break; |
| 115 | default: |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 116 | msg_perr("Error opening %s: %s.\n", i2c_device, strerror(errno)); |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 117 | } |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 118 | ret = -1; |
| 119 | goto out; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 120 | } |
| 121 | // Set slave address |
| 122 | if (ioctl(mstarddc_fd, I2C_SLAVE, mstarddc_addr) < 0) { |
| 123 | msg_perr("Error setting slave address 0x%02x: errno %d.\n", |
| 124 | mstarddc_addr, errno); |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 125 | ret = -1; |
| 126 | goto out; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 127 | } |
| 128 | // Enable ISP mode |
| 129 | uint8_t cmd[5] = { 'M', 'S', 'T', 'A', 'R' }; |
| 130 | if (write(mstarddc_fd, cmd, 5) < 0) { |
| 131 | int enable_err = errno; |
| 132 | uint8_t end_cmd = MSTARDDC_SPI_END; |
| 133 | |
| 134 | // Assume device is already in ISP mode, try to send END command |
| 135 | if (write(mstarddc_fd, &end_cmd, 1) < 0) { |
| 136 | msg_perr("Error enabling ISP mode: errno %d & %d.\n" |
| 137 | "Please check that device (%s) and address (0x%02x) are correct.\n", |
| 138 | enable_err, errno, i2c_device, mstarddc_addr); |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 139 | ret = -1; |
| 140 | goto out; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 143 | |
| 144 | // Register programmer |
Nico Huber | 89569d6 | 2023-01-12 23:31:40 +0100 | [diff] [blame] | 145 | register_spi_master(&spi_master_mstarddc, 0, NULL); |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 146 | out: |
| 147 | free(i2c_device); |
| 148 | return ret; |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* Returns 0 upon success, a negative number upon errors. */ |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 152 | static int mstarddc_spi_send_command(const struct flashctx *flash, |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 153 | unsigned int writecnt, |
| 154 | unsigned int readcnt, |
| 155 | const unsigned char *writearr, |
| 156 | unsigned char *readarr) |
| 157 | { |
| 158 | int ret = 0; |
Stefan Tauner | adadca6 | 2015-02-18 23:28:30 +0000 | [diff] [blame] | 159 | uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t)); |
| 160 | if (cmd == NULL) { |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 161 | msg_perr("Error allocating memory: errno %d.\n", errno); |
| 162 | ret = -1; |
| 163 | } |
| 164 | |
| 165 | if (!ret && writecnt) { |
| 166 | cmd[0] = MSTARDDC_SPI_WRITE; |
| 167 | memcpy(cmd + 1, writearr, writecnt); |
| 168 | if (write(mstarddc_fd, cmd, writecnt + 1) < 0) { |
| 169 | msg_perr("Error sending write command: errno %d.\n", |
| 170 | errno); |
| 171 | ret = -1; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (!ret && readcnt) { |
| 176 | struct i2c_rdwr_ioctl_data i2c_data; |
| 177 | struct i2c_msg msg[2]; |
| 178 | |
| 179 | cmd[0] = MSTARDDC_SPI_READ; |
| 180 | i2c_data.nmsgs = 2; |
| 181 | i2c_data.msgs = msg; |
| 182 | i2c_data.msgs[0].addr = mstarddc_addr; |
| 183 | i2c_data.msgs[0].len = 1; |
| 184 | i2c_data.msgs[0].flags = 0; |
| 185 | i2c_data.msgs[0].buf = cmd; |
| 186 | i2c_data.msgs[1].addr = mstarddc_addr; |
| 187 | i2c_data.msgs[1].len = readcnt; |
| 188 | i2c_data.msgs[1].flags = I2C_M_RD; |
| 189 | i2c_data.msgs[1].buf = readarr; |
| 190 | |
| 191 | if (ioctl(mstarddc_fd, I2C_RDWR, &i2c_data) < 0) { |
| 192 | msg_perr("Error sending read command: errno %d.\n", |
| 193 | errno); |
| 194 | ret = -1; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if (!ret && (writecnt || readcnt)) { |
| 199 | cmd[0] = MSTARDDC_SPI_END; |
| 200 | if (write(mstarddc_fd, cmd, 1) < 0) { |
| 201 | msg_perr("Error sending end command: errno %d.\n", |
| 202 | errno); |
| 203 | ret = -1; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /* Do not reset if something went wrong, as it might prevent from |
| 208 | * retrying flashing. */ |
| 209 | if (ret != 0) |
| 210 | mstarddc_doreset = 0; |
| 211 | |
| 212 | if (cmd) |
| 213 | free(cmd); |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | static const struct spi_master spi_master_mstarddc = { |
Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 219 | .max_data_read = 256, |
| 220 | .max_data_write = 256, |
| 221 | .command = mstarddc_spi_send_command, |
| 222 | .multicommand = default_spi_send_multicommand, |
| 223 | .read = default_spi_read, |
| 224 | .write_256 = default_spi_write_256, |
Anastasia Klimchuk | c63d918 | 2021-07-06 16:18:44 +1000 | [diff] [blame] | 225 | .shutdown = mstarddc_spi_shutdown, |
Aarya Chaumal | 0cea753 | 2022-07-04 18:21:50 +0530 | [diff] [blame] | 226 | .probe_opcode = default_spi_probe_opcode, |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 229 | const struct programmer_entry programmer_mstarddc_spi = { |
| 230 | .name = "mstarddc_spi", |
| 231 | .type = OTHER, |
| 232 | .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n", |
| 233 | .init = mstarddc_spi_init, |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 234 | }; |