blob: fb404acc28d5e702d73bf88117125e00edf4d5b3 [file] [log] [blame]
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00004 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +00005 *
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>
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000021#include <string.h>
22#include <stdlib.h>
23#include <ctype.h>
24#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000025#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000026#include "programmer.h"
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000027#include "spi.h"
28
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000029/* Length of half a clock period in usecs. */
30static int bitbang_spi_half_period;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000031
Michael Karcher62175a02010-07-17 23:27:47 +000032static const struct bitbang_spi_master *bitbang_spi_master = NULL;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000033
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000034/* Note that CS# is active low, so val=0 means the chip is active. */
35static void bitbang_spi_set_cs(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000036{
Michael Karcher62175a02010-07-17 23:27:47 +000037 bitbang_spi_master->set_cs(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000038}
39
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000040static void bitbang_spi_set_sck(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000041{
Michael Karcher62175a02010-07-17 23:27:47 +000042 bitbang_spi_master->set_sck(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000043}
44
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000045static void bitbang_spi_set_mosi(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000046{
Michael Karcher62175a02010-07-17 23:27:47 +000047 bitbang_spi_master->set_mosi(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000048}
49
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000050static int bitbang_spi_get_miso(void)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000051{
Michael Karcher62175a02010-07-17 23:27:47 +000052 return bitbang_spi_master->get_miso();
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000053}
54
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000055static void bitbang_spi_request_bus(void)
56{
57 if (bitbang_spi_master->request_bus)
58 bitbang_spi_master->request_bus();
59}
60
61static void bitbang_spi_release_bus(void)
62{
63 if (bitbang_spi_master->release_bus)
64 bitbang_spi_master->release_bus();
65}
66
Michael Karcher62175a02010-07-17 23:27:47 +000067int bitbang_spi_init(const struct bitbang_spi_master *master, int halfperiod)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000068{
Carl-Daniel Hailfinger17e23ac2010-07-18 14:42:28 +000069 /* BITBANG_SPI_INVALID is 0, so if someone forgot to initialize ->type,
70 * we catch it here. Same goes for missing initialization of bitbanging
71 * functions.
72 */
73 if (!master || master->type == BITBANG_SPI_INVALID || !master->set_cs ||
74 !master->set_sck || !master->set_mosi || !master->get_miso) {
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000075 msg_perr("Incomplete SPI bitbang master setting!\n"
76 "Please report a bug at flashrom@flashrom.org\n");
Carl-Daniel Hailfinger17e23ac2010-07-18 14:42:28 +000077 return 1;
78 }
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000079 if (bitbang_spi_master) {
80 msg_perr("SPI bitbang master already initialized!\n"
81 "Please report a bug at flashrom@flashrom.org\n");
82 return 1;
83 }
84
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000085 bitbang_spi_master = master;
86 bitbang_spi_half_period = halfperiod;
87
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000088 /* FIXME: Run bitbang_spi_request_bus here or in programmer init? */
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000089 bitbang_spi_set_cs(1);
90 bitbang_spi_set_sck(0);
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000091 bitbang_spi_set_mosi(0);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000092 return 0;
93}
94
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000095int bitbang_spi_shutdown(const struct bitbang_spi_master *master)
96{
97 if (!bitbang_spi_master) {
98 msg_perr("Shutting down an uninitialized SPI bitbang master!\n"
99 "Please report a bug at flashrom@flashrom.org\n");
100 return 1;
101 }
102 if (master != bitbang_spi_master) {
103 msg_perr("Shutting down a mismatched SPI bitbang master!\n"
104 "Please report a bug at flashrom@flashrom.org\n");
105 return 1;
106 }
107
108 /* FIXME: Run bitbang_spi_release_bus here or per command? */
109 bitbang_spi_master = NULL;
110 return 0;
111}
112
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +0000113static uint8_t bitbang_spi_readwrite_byte(uint8_t val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000114{
115 uint8_t ret = 0;
116 int i;
117
118 for (i = 7; i >= 0; i--) {
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +0000119 bitbang_spi_set_mosi((val >> i) & 1);
120 programmer_delay(bitbang_spi_half_period);
121 bitbang_spi_set_sck(1);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000122 ret <<= 1;
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +0000123 ret |= bitbang_spi_get_miso();
124 programmer_delay(bitbang_spi_half_period);
125 bitbang_spi_set_sck(0);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000126 }
127 return ret;
128}
129
130int bitbang_spi_send_command(unsigned int writecnt, unsigned int readcnt,
131 const unsigned char *writearr, unsigned char *readarr)
132{
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000133 int i;
134
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000135 /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
136 * Requesting and releasing the SPI bus is handled in here to allow the
137 * programmer to use its own SPI engine for native accesses.
138 */
139 bitbang_spi_request_bus();
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +0000140 bitbang_spi_set_cs(0);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000141 for (i = 0; i < writecnt; i++)
142 bitbang_spi_readwrite_byte(writearr[i]);
143 for (i = 0; i < readcnt; i++)
144 readarr[i] = bitbang_spi_readwrite_byte(0);
145
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +0000146 programmer_delay(bitbang_spi_half_period);
147 bitbang_spi_set_cs(1);
148 programmer_delay(bitbang_spi_half_period);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000149 /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
150 bitbang_spi_release_bus();
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000151
152 return 0;
153}
154
155int bitbang_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
156{
157 /* Maximum read length is unlimited, use 64k bytes. */
158 return spi_read_chunked(flash, buf, start, len, 64 * 1024);
159}
160
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000161int bitbang_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000162{
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000163 return spi_write_chunked(flash, buf, start, len, 256);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000164}