blob: fe85b6091603d6aeb2d50f5eea6d70600f5c300e [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>
21#include <stdint.h>
22#include <string.h>
23#include <stdlib.h>
24#include <ctype.h>
25#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000026#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000027#include "programmer.h"
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000028#include "spi.h"
29
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000030/* Length of half a clock period in usecs. */
31static int bitbang_spi_half_period;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000032
Michael Karcher62175a02010-07-17 23:27:47 +000033static const struct bitbang_spi_master *bitbang_spi_master = NULL;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000034
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000035/* Note that CS# is active low, so val=0 means the chip is active. */
36static void bitbang_spi_set_cs(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000037{
Michael Karcher62175a02010-07-17 23:27:47 +000038 bitbang_spi_master->set_cs(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000039}
40
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000041static void bitbang_spi_set_sck(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000042{
Michael Karcher62175a02010-07-17 23:27:47 +000043 bitbang_spi_master->set_sck(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000044}
45
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000046static void bitbang_spi_set_mosi(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000047{
Michael Karcher62175a02010-07-17 23:27:47 +000048 bitbang_spi_master->set_mosi(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000049}
50
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000051static int bitbang_spi_get_miso(void)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000052{
Michael Karcher62175a02010-07-17 23:27:47 +000053 return bitbang_spi_master->get_miso();
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000054}
55
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000056static void bitbang_spi_request_bus(void)
57{
58 if (bitbang_spi_master->request_bus)
59 bitbang_spi_master->request_bus();
60}
61
62static void bitbang_spi_release_bus(void)
63{
64 if (bitbang_spi_master->release_bus)
65 bitbang_spi_master->release_bus();
66}
67
Michael Karcher62175a02010-07-17 23:27:47 +000068int bitbang_spi_init(const struct bitbang_spi_master *master, int halfperiod)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000069{
Carl-Daniel Hailfinger17e23ac2010-07-18 14:42:28 +000070 /* BITBANG_SPI_INVALID is 0, so if someone forgot to initialize ->type,
71 * we catch it here. Same goes for missing initialization of bitbanging
72 * functions.
73 */
74 if (!master || master->type == BITBANG_SPI_INVALID || !master->set_cs ||
75 !master->set_sck || !master->set_mosi || !master->get_miso) {
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000076 msg_perr("Incomplete SPI bitbang master setting!\n"
77 "Please report a bug at flashrom@flashrom.org\n");
Carl-Daniel Hailfinger17e23ac2010-07-18 14:42:28 +000078 return 1;
79 }
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000080 if (bitbang_spi_master) {
81 msg_perr("SPI bitbang master already initialized!\n"
82 "Please report a bug at flashrom@flashrom.org\n");
83 return 1;
84 }
85
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000086 bitbang_spi_master = master;
87 bitbang_spi_half_period = halfperiod;
88
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000089 /* FIXME: Run bitbang_spi_request_bus here or in programmer init? */
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000090 bitbang_spi_set_cs(1);
91 bitbang_spi_set_sck(0);
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000092 bitbang_spi_set_mosi(0);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000093 return 0;
94}
95
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +000096int bitbang_spi_shutdown(const struct bitbang_spi_master *master)
97{
98 if (!bitbang_spi_master) {
99 msg_perr("Shutting down an uninitialized SPI bitbang master!\n"
100 "Please report a bug at flashrom@flashrom.org\n");
101 return 1;
102 }
103 if (master != bitbang_spi_master) {
104 msg_perr("Shutting down a mismatched SPI bitbang master!\n"
105 "Please report a bug at flashrom@flashrom.org\n");
106 return 1;
107 }
108
109 /* FIXME: Run bitbang_spi_release_bus here or per command? */
110 bitbang_spi_master = NULL;
111 return 0;
112}
113
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +0000114static uint8_t bitbang_spi_readwrite_byte(uint8_t val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000115{
116 uint8_t ret = 0;
117 int i;
118
119 for (i = 7; i >= 0; i--) {
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +0000120 bitbang_spi_set_mosi((val >> i) & 1);
121 programmer_delay(bitbang_spi_half_period);
122 bitbang_spi_set_sck(1);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000123 ret <<= 1;
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +0000124 ret |= bitbang_spi_get_miso();
125 programmer_delay(bitbang_spi_half_period);
126 bitbang_spi_set_sck(0);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000127 }
128 return ret;
129}
130
131int bitbang_spi_send_command(unsigned int writecnt, unsigned int readcnt,
132 const unsigned char *writearr, unsigned char *readarr)
133{
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000134 int i;
135
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000136 /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
137 * Requesting and releasing the SPI bus is handled in here to allow the
138 * programmer to use its own SPI engine for native accesses.
139 */
140 bitbang_spi_request_bus();
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +0000141 bitbang_spi_set_cs(0);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000142 for (i = 0; i < writecnt; i++)
143 bitbang_spi_readwrite_byte(writearr[i]);
144 for (i = 0; i < readcnt; i++)
145 readarr[i] = bitbang_spi_readwrite_byte(0);
146
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +0000147 programmer_delay(bitbang_spi_half_period);
148 bitbang_spi_set_cs(1);
149 programmer_delay(bitbang_spi_half_period);
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000150 /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
151 bitbang_spi_release_bus();
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000152
153 return 0;
154}
155
156int bitbang_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
157{
158 /* Maximum read length is unlimited, use 64k bytes. */
159 return spi_read_chunked(flash, buf, start, len, 64 * 1024);
160}
161
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000162int bitbang_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000163{
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000164 return spi_write_chunked(flash, buf, start, len, 256);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000165}