blob: 882d1292bab1fff7b44e93b0a7cb333acb8f66c6 [file] [log] [blame]
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Carl-Daniel Hailfinger
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 <stdint.h>
22#include <string.h>
23#include <stdlib.h>
24#include <ctype.h>
25#include "flash.h"
26#include "spi.h"
27
28/* Length of half a clock period in usecs */
29int bitbang_half_period = 0;
30
31enum spi_bitbang_master spi_bitbang_master = SPI_BITBANG_INVALID;
32
33const struct spi_bitbang_master_entry spi_bitbang_master_table[] = {
34 {}, /* This entry corresponds to SPI_BITBANG_INVALID. */
35};
36
37const int spi_bitbang_master_count = ARRAY_SIZE(spi_bitbang_master_table);
38
39void bitbang_set_cs(int val)
40{
41 spi_bitbang_master_table[spi_bitbang_master].set_cs(val);
42}
43
44void bitbang_set_sck(int val)
45{
46 spi_bitbang_master_table[spi_bitbang_master].set_sck(val);
47}
48
49void bitbang_set_mosi(int val)
50{
51 spi_bitbang_master_table[spi_bitbang_master].set_mosi(val);
52}
53
54int bitbang_get_miso(void)
55{
56 return spi_bitbang_master_table[spi_bitbang_master].get_miso();
57}
58
59int bitbang_spi_init(void)
60{
61 bitbang_set_cs(1);
62 bitbang_set_sck(0);
63 buses_supported = CHIP_BUSTYPE_SPI;
64 return 0;
65}
66
67uint8_t bitbang_spi_readwrite_byte(uint8_t val)
68{
69 uint8_t ret = 0;
70 int i;
71
72 for (i = 7; i >= 0; i--) {
73 bitbang_set_mosi((val >> i) & 1);
74 programmer_delay(bitbang_half_period);
75 bitbang_set_sck(1);
76 ret <<= 1;
77 ret |= bitbang_get_miso();
78 programmer_delay(bitbang_half_period);
79 bitbang_set_sck(0);
80 }
81 return ret;
82}
83
84int bitbang_spi_send_command(unsigned int writecnt, unsigned int readcnt,
85 const unsigned char *writearr, unsigned char *readarr)
86{
87 static unsigned char *bufout = NULL;
88 static unsigned char *bufin = NULL;
89 static int oldbufsize = 0;
90 int bufsize = max(writecnt + readcnt, 260);
91 int i;
92
93 /* Arbitrary size limitation here. We're only constrained by memory. */
94 if (writecnt > 65536 || readcnt > 65536)
95 return SPI_INVALID_LENGTH;
96
97 if (bufsize != oldbufsize) {
98 bufout = realloc(bufout, bufsize);
99 if (!bufout) {
100 fprintf(stderr, "Out of memory!\n");
101 if (bufin)
102 free(bufin);
103 exit(1);
104 }
105 bufin = realloc(bufout, bufsize);
106 if (!bufin) {
107 fprintf(stderr, "Out of memory!\n");
108 if (bufout)
109 free(bufout);
110 exit(1);
111 }
112 }
113
114 memcpy(bufout, writearr, writecnt);
115 /* Shift out 0x00 while reading data. */
116 memset(bufout + writecnt, 0x00, readcnt);
117 /* Make sure any non-read data is 0xff. */
118 memset(bufin + writecnt, 0xff, readcnt);
119
120 bitbang_set_cs(0);
121 for (i = 0; i < readcnt + writecnt; i++) {
122 bufin[i] = bitbang_spi_readwrite_byte(bufout[i]);
123 }
124 programmer_delay(bitbang_half_period);
125 bitbang_set_cs(1);
126 programmer_delay(bitbang_half_period);
127 memcpy(readarr, bufin + writecnt, readcnt);
128
129 return 0;
130}
131
132int bitbang_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
133{
134 /* Maximum read length is unlimited, use 64k bytes. */
135 return spi_read_chunked(flash, buf, start, len, 64 * 1024);
136}
137
138int bitbang_spi_write_256(struct flashchip *flash, uint8_t *buf)
139{
140 int total_size = 1024 * flash->total_size;
141 int i;
142
143 printf_debug("total_size is %d\n", total_size);
144 for (i = 0; i < total_size; i += 256) {
145 int l, r;
146 if (i + 256 <= total_size)
147 l = 256;
148 else
149 l = total_size - i;
150
151 if ((r = spi_nbyte_program(i, &buf[i], l))) {
152 fprintf(stderr, "%s: write fail %d\n", __FUNCTION__, r);
153 return 1;
154 }
155
156 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
157 /* loop */;
158 }
159
160 return 0;
161}