blob: fa6ec17135c145d1f027a121d909c2cb55280592 [file] [log] [blame]
Sven Schnelle5ce5f702011-09-03 18:37:52 +00001/*
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
20#include <stdio.h>
21#include <string.h>
22#include <stdlib.h>
23#include <sys/fcntl.h>
24#include <errno.h>
25#include <ctype.h>
26#include <unistd.h>
27#include <linux/spi/spidev.h>
28#include <sys/ioctl.h>
29#include "flash.h"
30#include "chipdrivers.h"
31#include "programmer.h"
32#include "spi.h"
33
34static int fd = -1;
35
36static int linux_spi_shutdown(void *data);
37static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt,
38 const unsigned char *txbuf, unsigned char *rxbuf);
39static int linux_spi_read(struct flashchip *flash, uint8_t *buf, int start,
40 int len);
41static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf,
42 int start, int len);
43
44static const struct spi_programmer spi_programmer_linux = {
45 .type = SPI_CONTROLLER_LINUX,
46 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
47 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
48 .command = linux_spi_send_command,
49 .multicommand = default_spi_send_multicommand,
50 .read = linux_spi_read,
51 .write_256 = linux_spi_write_256,
52};
53
54int linux_spi_init(void)
55{
56 char *p, *endp, *dev;
57 int speed = 0;
58
59 dev = extract_programmer_param("dev");
60 if (!dev || !strlen(dev)) {
61 msg_perr("No SPI device given. Use flashrom -p "
62 "linux_spi:dev=/dev/spidevX.Y\n");
63 return 1;
64 }
65
66 p = extract_programmer_param("speed");
67 if (p && strlen(p)) {
68 speed = strtoul(p, &endp, 10) * 1024;
69 if (p == endp) {
70 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
71 return 1;
72 }
73 }
74
75 if ((fd = open(dev, O_RDWR)) == -1) {
76 msg_perr("%s: failed to open %s: %s\n", __func__,
77 dev, strerror(errno));
78 return 1;
79 }
80
81 if (speed > 0 && ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
82 msg_perr("%s: failed to set speed %dHz: %s\n",
83 __func__, speed, strerror(errno));
84 close(fd);
85 return 1;
86 }
87
88 if (register_shutdown(linux_spi_shutdown, NULL))
89 return 1;
90
91 register_spi_programmer(&spi_programmer_linux);
92
93 return 0;
94}
95
96static int linux_spi_shutdown(void *data)
97{
98 if (fd != -1) {
99 close(fd);
100 fd = -1;
101 }
102 return 0;
103}
104
105static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt,
106 const unsigned char *txbuf, unsigned char *rxbuf)
107{
108 struct spi_ioc_transfer msg[2] = {
109 {
110 .tx_buf = (uint64_t)txbuf,
111 .len = writecnt,
112 },
113 {
114 .rx_buf = (uint64_t)rxbuf,
115 .len = readcnt,
116 },
117 };
118
119 if (fd == -1)
120 return -1;
121
122 if (ioctl(fd, SPI_IOC_MESSAGE(2), msg) == -1) {
123 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
124 return -1;
125 }
126 return 0;
127}
128
129static int linux_spi_read(struct flashchip *flash, uint8_t *buf, int start,
130 int len)
131{
132 return spi_read_chunked(flash, buf, start, len, getpagesize());
133}
134
135static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf,
136 int start, int len)
137{
138 return spi_write_chunked(flash, buf, start, len, getpagesize() - 4);
139}