blob: b543fd457c94b5807ee55ad304f0c7c3ea336d91 [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 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
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000032static enum bitbang_spi_master bitbang_spi_master = BITBANG_SPI_INVALID;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000033
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000034static const struct bitbang_spi_master_entry bitbang_spi_master_table[] = {
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000035 {}, /* This entry corresponds to BITBANG_SPI_INVALID. */
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000036};
37
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000038const int bitbang_spi_master_count = ARRAY_SIZE(bitbang_spi_master_table);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000039
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000040/* Note that CS# is active low, so val=0 means the chip is active. */
41static void bitbang_spi_set_cs(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000042{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000043 bitbang_spi_master_table[bitbang_spi_master].set_cs(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000044}
45
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000046static void bitbang_spi_set_sck(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000047{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000048 bitbang_spi_master_table[bitbang_spi_master].set_sck(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000049}
50
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000051static void bitbang_spi_set_mosi(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000052{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000053 bitbang_spi_master_table[bitbang_spi_master].set_mosi(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000054}
55
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000056static int bitbang_spi_get_miso(void)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000057{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000058 return bitbang_spi_master_table[bitbang_spi_master].get_miso();
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000059}
60
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000061int bitbang_spi_init(enum bitbang_spi_master master, int halfperiod)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000062{
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000063 bitbang_spi_master = master;
64 bitbang_spi_half_period = halfperiod;
65
66 if (bitbang_spi_master == BITBANG_SPI_INVALID) {
67 msg_perr("Invalid bitbang SPI master. \n"
68 "Please report a bug at flashrom@flashrom.org\n");
69 return 1;
70 }
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000071 bitbang_spi_set_cs(1);
72 bitbang_spi_set_sck(0);
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000073 bitbang_spi_set_mosi(0);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000074 return 0;
75}
76
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +000077static uint8_t bitbang_spi_readwrite_byte(uint8_t val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000078{
79 uint8_t ret = 0;
80 int i;
81
82 for (i = 7; i >= 0; i--) {
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000083 bitbang_spi_set_mosi((val >> i) & 1);
84 programmer_delay(bitbang_spi_half_period);
85 bitbang_spi_set_sck(1);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000086 ret <<= 1;
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000087 ret |= bitbang_spi_get_miso();
88 programmer_delay(bitbang_spi_half_period);
89 bitbang_spi_set_sck(0);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000090 }
91 return ret;
92}
93
94int bitbang_spi_send_command(unsigned int writecnt, unsigned int readcnt,
95 const unsigned char *writearr, unsigned char *readarr)
96{
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000097 int i;
98
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000099 bitbang_spi_set_cs(0);
Michael Karcher1a854fc2010-07-17 10:42:34 +0000100 for (i = 0; i < writecnt; i++)
101 bitbang_spi_readwrite_byte(writearr[i]);
102 for (i = 0; i < readcnt; i++)
103 readarr[i] = bitbang_spi_readwrite_byte(0);
104
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +0000105 programmer_delay(bitbang_spi_half_period);
106 bitbang_spi_set_cs(1);
107 programmer_delay(bitbang_spi_half_period);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000108
109 return 0;
110}
111
112int bitbang_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
113{
114 /* Maximum read length is unlimited, use 64k bytes. */
115 return spi_read_chunked(flash, buf, start, len, 64 * 1024);
116}
117
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000118int bitbang_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000119{
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000120 return spi_write_chunked(flash, buf, start, len, 256);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000121}