linux_spi.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2011 Sven Schnelle <svens@stackframe.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. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. #include <ctype.h>
  25. #include <unistd.h>
  26. #include <linux/spi/spidev.h>
  27. #include <linux/ioctl.h>
  28. #include <sys/ioctl.h>
  29. #include <sys/stat.h>
  30. #include <sys/types.h>
  31. #include "file.h"
  32. #include "flash.h"
  33. #include "chipdrivers.h"
  34. #include "programmer.h"
  35. #include "spi.h"
  36. /* TODO: this information should come from the SPI master driver. */
  37. #define SPI_DMA_SIZE 64
  38. #ifndef SPIDEV_MAJOR
  39. #define SPIDEV_MAJOR 153 /* refer to kernel/files/drivers/spi/spidev.c */
  40. #endif
  41. #define MODALIAS_FILE "modalias"
  42. #define LINUX_SPI_SYSFS_ROOT "/sys/bus/spi/devices"
  43. static int fd = -1;
  44. static int linux_spi_shutdown(void *data);
  45. static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  46. const unsigned char *txbuf, unsigned char *rxbuf);
  47. static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
  48. unsigned int start, unsigned int len);
  49. static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf,
  50. unsigned int start, unsigned int len);
  51. static const struct spi_programmer spi_programmer_linux = {
  52. .type = SPI_CONTROLLER_LINUX,
  53. .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
  54. .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
  55. .command = linux_spi_send_command,
  56. .multicommand = default_spi_send_multicommand,
  57. .read = linux_spi_read,
  58. .write_256 = linux_spi_write_256,
  59. };
  60. static char devfs_path[32]; /* at least big enough to fit /dev/spidevX.Y */
  61. static char *check_sysfs(void)
  62. {
  63. int i;
  64. const char *sysfs_path = NULL;
  65. char *p;
  66. char *modalias[] = {
  67. "spi:spidev", /* raw access over SPI bus (newer kernels) */
  68. "spidev", /* raw access over SPI bus (older kernels) */
  69. "m25p80", /* generic MTD device */
  70. };
  71. for (i = 0; i < ARRAY_SIZE(modalias); i++) {
  72. int major, minor;
  73. /* Path should look like: /sys/blah/spiX.Y/modalias */
  74. sysfs_path = scanft(LINUX_SPI_SYSFS_ROOT,
  75. MODALIAS_FILE, modalias[i], 1);
  76. if (!sysfs_path)
  77. continue;
  78. p = (char *)sysfs_path + strlen(LINUX_SPI_SYSFS_ROOT);
  79. if (p[0] == '/')
  80. p++;
  81. if (sscanf(p, "spi%u.%u", &major, &minor) == 2) {
  82. msg_pdbg("Found SPI device %s on spi%u.%u\n",
  83. modalias[i], major, minor);
  84. sprintf(devfs_path, "/dev/spidev%u.%u", major, minor);
  85. free((void *)sysfs_path);
  86. break;
  87. }
  88. free((void *)sysfs_path);
  89. }
  90. if (i == ARRAY_SIZE(modalias))
  91. return NULL;
  92. return devfs_path;
  93. }
  94. static char *check_fdt(void)
  95. {
  96. unsigned int bus, cs;
  97. if (fdt_find_spi_nor_flash(&bus, &cs) < 0)
  98. return NULL;
  99. sprintf(devfs_path, "/dev/spidev%u.%u", bus, cs);
  100. return devfs_path;
  101. }
  102. static char *linux_spi_probe(void)
  103. {
  104. char *ret;
  105. ret = check_fdt();
  106. if (ret)
  107. return ret;
  108. ret = check_sysfs();
  109. if (ret)
  110. return ret;
  111. return NULL;
  112. }
  113. /*
  114. * This is used when /dev/spidevX.Y is not created yet, for example, when
  115. * udev is not started.
  116. */
  117. static int manual_mknod(const char *dev)
  118. {
  119. char cmd[256];
  120. msg_pdbg("Creating SPI device node %s...\n", dev);
  121. strcpy(cmd, "modprobe spidev");
  122. msg_pdbg("CMD: [%s]\n", cmd);
  123. if (system(cmd)) {
  124. msg_perr("%s: failed to run '%s': %s\n", __func__,
  125. cmd, strerror(errno));
  126. return -1;
  127. }
  128. snprintf(cmd, sizeof(cmd), "mknod %s c %d 0", dev, SPIDEV_MAJOR);
  129. msg_pdbg("CMD: [%s]\n", cmd);
  130. if (system(cmd)) {
  131. msg_perr("%s: failed to run '%s': %s\n", __func__,
  132. cmd, strerror(errno));
  133. return -1;
  134. }
  135. if ((fd = open(dev, O_RDWR)) == -1) {
  136. msg_perr("%s: failed to open %s: %s\n", __func__,
  137. dev, strerror(errno));
  138. }
  139. return fd;
  140. }
  141. int linux_spi_init(void)
  142. {
  143. char *p, *endp, *dev;
  144. uint32_t speed = 0;
  145. /*
  146. * FIXME: There might be other programmers with flash memory (such as
  147. * an EC) connected via SPI. For now we rely on the device's driver to
  148. * distinguish it and assume generic SPI implies host.
  149. */
  150. if (alias && alias->type != ALIAS_HOST)
  151. return 1;
  152. dev = extract_programmer_param("dev");
  153. if (!dev)
  154. dev = linux_spi_probe();
  155. if (!dev || !strlen(dev)) {
  156. msg_perr("No SPI device found. Use flashrom -p "
  157. "linux_spi:dev=/dev/spidevX.Y\n");
  158. return 1;
  159. }
  160. p = extract_programmer_param("speed");
  161. if (p && strlen(p)) {
  162. speed = (uint32_t)strtoul(p, &endp, 10) * 1024;
  163. if (p == endp) {
  164. msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
  165. return 1;
  166. }
  167. }
  168. msg_pdbg("Using device %s\n", dev);
  169. if ((fd = open(dev, O_RDWR)) == -1) {
  170. msg_pdbg("%s: failed to open %s: %s\n", __func__,
  171. dev, strerror(errno));
  172. if (manual_mknod(dev) == -1) { // global fd is effected.
  173. return 1;
  174. }
  175. }
  176. if (speed > 0) {
  177. if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
  178. msg_perr("%s: failed to set speed %dHz: %s\n",
  179. __func__, speed, strerror(errno));
  180. close(fd);
  181. return 1;
  182. }
  183. msg_pdbg("Using %d kHz clock\n", speed);
  184. }
  185. if (register_shutdown(linux_spi_shutdown, NULL))
  186. return 1;
  187. register_spi_programmer(&spi_programmer_linux);
  188. return 0;
  189. }
  190. static int linux_spi_shutdown(void *data)
  191. {
  192. if (fd != -1) {
  193. close(fd);
  194. fd = -1;
  195. }
  196. return 0;
  197. }
  198. static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  199. const unsigned char *txbuf, unsigned char *rxbuf)
  200. {
  201. int msg_start = 0, msg_count = 0;
  202. struct spi_ioc_transfer msg[2] = {
  203. {
  204. .tx_buf = (uint64_t)(uintptr_t)txbuf,
  205. .len = writecnt,
  206. },
  207. {
  208. .rx_buf = (uint64_t)(uintptr_t)rxbuf,
  209. .len = readcnt,
  210. },
  211. };
  212. if (fd == -1)
  213. return -1;
  214. /* Only pass necessary msg[] to ioctl() to avoid the empty message
  215. * which drives an un-expected CS line and clocks. */
  216. if (writecnt) {
  217. msg_start = 0; /* tx: msg[0] */
  218. msg_count++;
  219. if (readcnt) {
  220. msg_count++;
  221. }
  222. } else if (readcnt) {
  223. msg_start = 1; /* rx: msg[1] */
  224. msg_count++;
  225. } else {
  226. msg_cerr("%s: both writecnt and readcnt are 0.\n",
  227. __func__);
  228. return -1;
  229. }
  230. if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) {
  231. msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
  232. return -1;
  233. }
  234. return 0;
  235. }
  236. static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
  237. unsigned int start, unsigned int len)
  238. {
  239. return spi_read_chunked(flash, buf, start, len, SPI_DMA_SIZE);
  240. }
  241. static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf,
  242. unsigned int start, unsigned int len)
  243. {
  244. return spi_write_chunked(flash, buf, start, len,
  245. SPI_DMA_SIZE - 5 /* WREN, Page Program */);
  246. }