source: trunk/bitbang_spi.c @ 1475

Revision 1475, 4.8 KB checked in by hailfinger, 7 weeks ago (diff)

Have all programmer init functions register bus masters/programmers

All programmer types (Parallel, SPI, Opaque) now register themselves
into a generic programmer list and probing is now programmer-centric
instead of chip-centric.
Registering multiple SPI/... masters at the same time is now possible
without any problems. Handling multiple flash chips is still unchanged,
but now we have the infrastructure to deal with "dual BIOS" and "one
flash behind southbridge and one flash behind EC" sanely.

A nice side effect is that this patch kills quite a few global variables
and improves the situation for libflashrom.

Hint for developers:
struct {spi,par,opaque}_programmer now have a void *data pointer to
store any additional programmer-specific data, e.g. hardware
configuration info.

Note:
flashrom -f -c FOO -r forced_read.bin
does not work anymore. We have to find an architecturally clean way to
solve this.

Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@…>
Acked-by: Michael Karcher <flashrom@…>

Line 
1/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009, 2010 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 <string.h>
22#include <stdlib.h>
23#include <ctype.h>
24#include "flash.h"
25#include "programmer.h"
26#include "spi.h"
27
28/* Note that CS# is active low, so val=0 means the chip is active. */
29static void bitbang_spi_set_cs(const const struct bitbang_spi_master *master, int val)
30{
31        master->set_cs(val);
32}
33
34static void bitbang_spi_set_sck(const const struct bitbang_spi_master *master, int val)
35{
36        master->set_sck(val);
37}
38
39static void bitbang_spi_set_mosi(const const struct bitbang_spi_master *master, int val)
40{
41        master->set_mosi(val);
42}
43
44static int bitbang_spi_get_miso(const const struct bitbang_spi_master *master)
45{
46        return master->get_miso();
47}
48
49static void bitbang_spi_request_bus(const const struct bitbang_spi_master *master)
50{
51        if (master->request_bus)
52                master->request_bus();
53}
54
55static void bitbang_spi_release_bus(const const struct bitbang_spi_master *master)
56{
57        if (master->release_bus)
58                master->release_bus();
59}
60
61static int bitbang_spi_send_command(struct flashctx *flash,
62                                    unsigned int writecnt, unsigned int readcnt,
63                                    const unsigned char *writearr,
64                                    unsigned char *readarr);
65
66static const struct spi_programmer spi_programmer_bitbang = {
67        .type           = SPI_CONTROLLER_BITBANG,
68        .max_data_read  = MAX_DATA_READ_UNLIMITED,
69        .max_data_write = MAX_DATA_WRITE_UNLIMITED,
70        .command        = bitbang_spi_send_command,
71        .multicommand   = default_spi_send_multicommand,
72        .read           = default_spi_read,
73        .write_256      = default_spi_write_256,
74};
75
76#if 0 // until it is needed
77static int bitbang_spi_shutdown(const struct bitbang_spi_master *master)
78{
79        /* FIXME: Run bitbang_spi_release_bus here or per command? */
80        return 0;
81}
82#endif
83
84int bitbang_spi_init(const struct bitbang_spi_master *master)
85{
86        struct spi_programmer pgm = spi_programmer_bitbang;
87        /* BITBANG_SPI_INVALID is 0, so if someone forgot to initialize ->type,
88         * we catch it here. Same goes for missing initialization of bitbanging
89         * functions.
90         */
91        if (!master || master->type == BITBANG_SPI_INVALID || !master->set_cs ||
92            !master->set_sck || !master->set_mosi || !master->get_miso ||
93            (master->request_bus && !master->release_bus) ||
94            (!master->request_bus && master->release_bus)) {
95                msg_perr("Incomplete SPI bitbang master setting!\n"
96                         "Please report a bug at flashrom@flashrom.org\n");
97                return ERROR_FLASHROM_BUG;
98        }
99
100        pgm.data = master;
101        register_spi_programmer(&pgm);
102
103        /* Only mess with the bus if we're sure nobody else uses it. */
104        bitbang_spi_request_bus(master);
105        bitbang_spi_set_cs(master, 1);
106        bitbang_spi_set_sck(master, 0);
107        bitbang_spi_set_mosi(master, 0);
108        /* FIXME: Release SPI bus here and request it again for each command or
109         * don't release it now and only release it on programmer shutdown?
110         */
111        bitbang_spi_release_bus(master);
112        return 0;
113}
114
115static uint8_t bitbang_spi_rw_byte(const struct bitbang_spi_master *master,
116                                   uint8_t val)
117{
118        uint8_t ret = 0;
119        int i;
120
121        for (i = 7; i >= 0; i--) {
122                bitbang_spi_set_mosi(master, (val >> i) & 1);
123                programmer_delay(master->half_period);
124                bitbang_spi_set_sck(master, 1);
125                ret <<= 1;
126                ret |= bitbang_spi_get_miso(master);
127                programmer_delay(master->half_period);
128                bitbang_spi_set_sck(master, 0);
129        }
130        return ret;
131}
132
133static int bitbang_spi_send_command(struct flashctx *flash,
134                                    unsigned int writecnt, unsigned int readcnt,
135                                    const unsigned char *writearr,
136                                    unsigned char *readarr)
137{
138        int i;
139        const struct bitbang_spi_master *master = flash->pgm->spi.data;
140
141        /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
142         * Requesting and releasing the SPI bus is handled in here to allow the
143         * programmer to use its own SPI engine for native accesses.
144         */
145        bitbang_spi_request_bus(master);
146        bitbang_spi_set_cs(master, 0);
147        for (i = 0; i < writecnt; i++)
148                bitbang_spi_rw_byte(master, writearr[i]);
149        for (i = 0; i < readcnt; i++)
150                readarr[i] = bitbang_spi_rw_byte(master, 0);
151
152        programmer_delay(master->half_period);
153        bitbang_spi_set_cs(master, 1);
154        programmer_delay(master->half_period);
155        /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
156        bitbang_spi_release_bus(master);
157
158        return 0;
159}
Note: See TracBrowser for help on using the repository browser.