blob: abf553017bd695f118dd9f2783b8d859531f6e38 [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 */
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000029int bitbang_spi_half_period = 0;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000030
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000031enum bitbang_spi_master bitbang_spi_master = BITBANG_SPI_INVALID;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000032
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000033const struct bitbang_spi_master_entry bitbang_spi_master_table[] = {
34 {}, /* This entry corresponds to BITBANG_SPI_INVALID. */
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000035};
36
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000037const int bitbang_spi_master_count = ARRAY_SIZE(bitbang_spi_master_table);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000038
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000039void bitbang_spi_set_cs(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000040{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000041 bitbang_spi_master_table[bitbang_spi_master].set_cs(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000042}
43
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000044void bitbang_spi_set_sck(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000045{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000046 bitbang_spi_master_table[bitbang_spi_master].set_sck(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000047}
48
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000049void bitbang_spi_set_mosi(int val)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000050{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000051 bitbang_spi_master_table[bitbang_spi_master].set_mosi(val);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000052}
53
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000054int bitbang_spi_get_miso(void)
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000055{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000056 return bitbang_spi_master_table[bitbang_spi_master].get_miso();
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000057}
58
59int bitbang_spi_init(void)
60{
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000061 bitbang_spi_set_cs(1);
62 bitbang_spi_set_sck(0);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000063 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--) {
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000073 bitbang_spi_set_mosi((val >> i) & 1);
74 programmer_delay(bitbang_spi_half_period);
75 bitbang_spi_set_sck(1);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000076 ret <<= 1;
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +000077 ret |= bitbang_spi_get_miso();
78 programmer_delay(bitbang_spi_half_period);
79 bitbang_spi_set_sck(0);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000080 }
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;
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +000090 int bufsize;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +000091 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
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +000097 bufsize = max(writecnt + readcnt, 260);
98 /* Never shrink. realloc() calls are expensive. */
99 if (bufsize > oldbufsize) {
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000100 bufout = realloc(bufout, bufsize);
101 if (!bufout) {
102 fprintf(stderr, "Out of memory!\n");
103 if (bufin)
104 free(bufin);
105 exit(1);
106 }
107 bufin = realloc(bufout, bufsize);
108 if (!bufin) {
109 fprintf(stderr, "Out of memory!\n");
110 if (bufout)
111 free(bufout);
112 exit(1);
113 }
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000114 oldbufsize = bufsize;
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000115 }
116
117 memcpy(bufout, writearr, writecnt);
118 /* Shift out 0x00 while reading data. */
119 memset(bufout + writecnt, 0x00, readcnt);
120 /* Make sure any non-read data is 0xff. */
121 memset(bufin + writecnt, 0xff, readcnt);
122
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +0000123 bitbang_spi_set_cs(0);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000124 for (i = 0; i < readcnt + writecnt; i++) {
125 bufin[i] = bitbang_spi_readwrite_byte(bufout[i]);
126 }
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +0000127 programmer_delay(bitbang_spi_half_period);
128 bitbang_spi_set_cs(1);
129 programmer_delay(bitbang_spi_half_period);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000130 memcpy(readarr, bufin + writecnt, readcnt);
131
132 return 0;
133}
134
135int bitbang_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
136{
137 /* Maximum read length is unlimited, use 64k bytes. */
138 return spi_read_chunked(flash, buf, start, len, 64 * 1024);
139}
140
141int bitbang_spi_write_256(struct flashchip *flash, uint8_t *buf)
142{
143 int total_size = 1024 * flash->total_size;
144 int i;
145
146 printf_debug("total_size is %d\n", total_size);
147 for (i = 0; i < total_size; i += 256) {
148 int l, r;
149 if (i + 256 <= total_size)
150 l = 256;
151 else
152 l = total_size - i;
153
154 if ((r = spi_nbyte_program(i, &buf[i], l))) {
Carl-Daniel Hailfinger3a4781e2009-10-01 14:51:25 +0000155 fprintf(stderr, "%s: write fail %d\n", __func__, r);
Carl-Daniel Hailfinger547872b2009-09-28 13:15:16 +0000156 return 1;
157 }
158
159 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
160 /* loop */;
161 }
162
163 return 0;
164}