blob: 6316a558c3ef13730070d2d139c7e391473476fe [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
29/* Length of half a clock period in usecs */
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000030int bitbang_spi_half_period = 0;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000031
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000032enum bitbang_spi_master bitbang_spi_master = BITBANG_SPI_INVALID;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000033
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000034const struct bitbang_spi_master_entry bitbang_spi_master_table[] = {
35 {}, /* 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 Hailfinger3a4781e2009-10-01 14:51:25 +000040void bitbang_spi_set_cs(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000041{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000042 bitbang_spi_master_table[bitbang_spi_master].set_cs(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000043}
44
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000045void bitbang_spi_set_sck(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000046{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000047 bitbang_spi_master_table[bitbang_spi_master].set_sck(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000048}
49
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000050void bitbang_spi_set_mosi(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000051{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000052 bitbang_spi_master_table[bitbang_spi_master].set_mosi(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000053}
54
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000055int bitbang_spi_get_miso(void)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000056{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000057 return bitbang_spi_master_table[bitbang_spi_master].get_miso();
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000058}
59
60int bitbang_spi_init(void)
61{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000062 bitbang_spi_set_cs(1);
63 bitbang_spi_set_sck(0);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000064 buses_supported = CHIP_BUSTYPE_SPI;
65 return 0;
66}
67
68uint8_t bitbang_spi_readwrite_byte(uint8_t val)
69{
70 uint8_t ret = 0;
71 int i;
72
73 for (i = 7; i >= 0; i--) {
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000074 bitbang_spi_set_mosi((val >> i) & 1);
75 programmer_delay(bitbang_spi_half_period);
76 bitbang_spi_set_sck(1);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000077 ret <<= 1;
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000078 ret |= bitbang_spi_get_miso();
79 programmer_delay(bitbang_spi_half_period);
80 bitbang_spi_set_sck(0);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000081 }
82 return ret;
83}
84
85int bitbang_spi_send_command(unsigned int writecnt, unsigned int readcnt,
86 const unsigned char *writearr, unsigned char *readarr)
87{
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000088 int i;
89
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000090 bitbang_spi_set_cs(0);
Michael Karcher1a854fc2010-07-17 10:42:34 +000091 for (i = 0; i < writecnt; i++)
92 bitbang_spi_readwrite_byte(writearr[i]);
93 for (i = 0; i < readcnt; i++)
94 readarr[i] = bitbang_spi_readwrite_byte(0);
95
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000096 programmer_delay(bitbang_spi_half_period);
97 bitbang_spi_set_cs(1);
98 programmer_delay(bitbang_spi_half_period);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000099
100 return 0;
101}
102
103int bitbang_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
104{
105 /* Maximum read length is unlimited, use 64k bytes. */
106 return spi_read_chunked(flash, buf, start, len, 64 * 1024);
107}
108
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000109int bitbang_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000110{
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000111 return spi_write_chunked(flash, buf, start, len, 256);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000112}