1 /****************************************************************************
3 * SPI Serial EEPROM (25xxx/95xxx) support
5 * Copyright (C) 2012 Eke-Eke (Genesis Plus GX)
7 * Redistribution and use of this code or any derivative works are permitted
8 * provided that the following conditions are met:
10 * - Redistributions may not be sold, nor may they be used in a commercial
11 * product or activity.
13 * - Redistributions that are modified from the original source must include the
14 * complete source code, including the source code for all components used by a
15 * binary built from the modified sources. However, as a special exception, the
16 * source code distributed need not include anything that is normally distributed
17 * (in either source or binary form) with the major components (compiler, kernel,
18 * and so on) of the operating system on which the executable runs, unless that
19 * component itself accompanies the executable.
21 * - Redistributions must reproduce the above copyright notice, this list of
22 * conditions and the following disclaimer in the documentation and/or other
23 * materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
37 ****************************************************************************************/
41 /* max supported size 64KB (25x512/95x512) */
42 #define SIZE_MASK 0xffff
43 #define PAGE_MASK 0x7f
45 /* hard-coded board implementation (!WP pin not used) */
62 uint8 cs; /* !CS line state */
63 uint8 clk; /* SCLK line state */
64 uint8 out; /* SO line state */
65 uint8 status; /* status register */
66 uint8 opcode; /* 8-bit opcode */
67 uint8 buffer; /* 8-bit data buffer */
68 uint16 addr; /* 16-bit address */
69 uint32 cycles; /* current operation cycle */
70 T_STATE_SPI state; /* current operation state */
73 static T_EEPROM_SPI spi_eeprom;
75 void eeprom_spi_init()
77 /* reset eeprom state */
78 memset(&spi_eeprom, 0, sizeof(T_EEPROM_SPI));
80 spi_eeprom.state = GET_OPCODE;
82 /* enable backup RAM */
87 void eeprom_spi_write(unsigned char data)
89 /* Make sure !HOLD is high */
90 if (data & (1 << BIT_HOLD))
93 if (data & (1 << BIT_CS))
95 /* !CS high -> end of current operation */
96 spi_eeprom.cycles = 0;
98 spi_eeprom.opcode = 0;
99 spi_eeprom.state = GET_OPCODE;
103 /* !CS low -> process current operation */
104 switch (spi_eeprom.state)
108 /* latch data on CLK positive edge */
109 if ((data & (1 << BIT_CLK)) && !spi_eeprom.clk)
111 /* 8-bit opcode buffer */
112 spi_eeprom.opcode |= ((data >> BIT_DATA) & 1);
116 if (spi_eeprom.cycles == 8)
118 /* reset cycles count */
119 spi_eeprom.cycles = 0;
121 /* Decode instruction */
122 switch (spi_eeprom.opcode)
127 spi_eeprom.buffer = 0;
128 spi_eeprom.state = WRITE_BYTE;
136 spi_eeprom.state = GET_ADDRESS;
144 spi_eeprom.state = GET_ADDRESS;
151 spi_eeprom.status &= ~0x02;
152 spi_eeprom.state = STANDBY;
159 spi_eeprom.buffer = spi_eeprom.status;
160 spi_eeprom.state = READ_BYTE;
167 spi_eeprom.status |= 0x02;
168 spi_eeprom.state = STANDBY;
174 /* specific instructions (not supported) */
175 spi_eeprom.state = STANDBY;
182 /* shift opcode value */
183 spi_eeprom.opcode = spi_eeprom.opcode << 1;
191 /* latch data on CLK positive edge */
192 if ((data & (1 << BIT_CLK)) && !spi_eeprom.clk)
195 spi_eeprom.addr |= ((data >> BIT_DATA) & 1);
199 if (spi_eeprom.cycles == 16)
201 /* reset cycles count */
202 spi_eeprom.cycles = 0;
204 /* mask unused address bits */
205 spi_eeprom.addr &= SIZE_MASK;
208 if (spi_eeprom.opcode & 0x01)
211 spi_eeprom.buffer = sram.sram[spi_eeprom.addr];
212 spi_eeprom.state = READ_BYTE;
216 /* WRITE operation */
217 spi_eeprom.buffer = 0;
218 spi_eeprom.state = WRITE_BYTE;
223 /* shift address value */
224 spi_eeprom.addr = spi_eeprom.addr << 1;
232 /* latch data on CLK positive edge */
233 if ((data & (1 << BIT_CLK)) && !spi_eeprom.clk)
235 /* 8-bit data buffer */
236 spi_eeprom.buffer |= ((data >> BIT_DATA) & 1);
240 if (spi_eeprom.cycles == 8)
242 /* reset cycles count */
243 spi_eeprom.cycles = 0;
245 /* write data to destination */
246 if (spi_eeprom.opcode & 0x01)
248 /* update status register */
249 spi_eeprom.status = (spi_eeprom.status & 0x02) | (spi_eeprom.buffer & 0x0c);
251 /* wait for operation end */
252 spi_eeprom.state = STANDBY;
256 /* Memory Array (write-protected) */
257 if (spi_eeprom.status & 2)
259 /* check array protection bits (BP0, BP1) */
260 switch ((spi_eeprom.status >> 2) & 0x03)
264 /* $C000-$FFFF (sector #3) is protected */
265 if (spi_eeprom.addr < 0xC000)
267 sram.sram[spi_eeprom.addr] = spi_eeprom.buffer;
274 /* $8000-$FFFF (sectors #2 and #3) is protected */
275 if (spi_eeprom.addr < 0x8000)
277 sram.sram[spi_eeprom.addr] = spi_eeprom.buffer;
284 /* $0000-$FFFF (all sectors) is protected */
290 /* no sectors protected */
291 sram.sram[spi_eeprom.addr] = spi_eeprom.buffer;
297 /* reset data buffer */
298 spi_eeprom.buffer = 0;
300 /* increase array address (sequential writes are limited within the same page) */
301 spi_eeprom.addr = (spi_eeprom.addr & ~PAGE_MASK) | ((spi_eeprom.addr + 1) & PAGE_MASK);
306 /* shift data buffer value */
307 spi_eeprom.buffer = spi_eeprom.buffer << 1;
315 /* output data on CLK positive edge */
316 if ((data & (1 << BIT_CLK)) && !spi_eeprom.clk)
319 spi_eeprom.out = (spi_eeprom.buffer >> (7 - spi_eeprom.cycles)) & 1;
323 if (spi_eeprom.cycles == 8)
325 /* reset cycles count */
326 spi_eeprom.cycles = 0;
328 /* read from memory array ? */
329 if (spi_eeprom.opcode == 0x03)
331 /* read next array byte */
332 spi_eeprom.addr = (spi_eeprom.addr + 1) & SIZE_MASK;
333 spi_eeprom.buffer = sram.sram[spi_eeprom.addr];
342 /* wait for !CS low->high transition */
349 /* update input lines */
350 spi_eeprom.cs = (data >> BIT_CS) & 1;
351 spi_eeprom.clk = (data >> BIT_CLK) & 1;
354 unsigned int eeprom_spi_read(unsigned int address)
356 return (spi_eeprom.out << BIT_DATA);