2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 /* Total bytes of available storage */
22 __pdata uint32_t ao_storage_total;
24 /* Block size - device is erased in these units. At least 256 bytes */
25 __pdata uint32_t ao_storage_block;
27 /* Byte offset of config block. Will be ao_storage_block bytes long */
28 __pdata uint32_t ao_storage_config;
30 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
31 __pdata uint16_t ao_storage_unit;
34 * MRAM is entirely random access; no erase operations are required,
35 * nor are reads or writes restricted to a particular alignment.
38 #define MR25_WREN 0x06 /* Write Enable */
39 #define MR25_WRDI 0x04 /* Write Disable */
40 #define MR25_RDSR 0x05 /* Read Status Register */
41 #define MR25_WRSR 0x01 /* Write Status Register */
42 #define MR25_READ 0x03 /* Read Data Bytes */
43 #define MR25_WRITE 0x02 /* Write Data Bytes */
46 * Status register bits
49 #define MR25_STATUS_SRWD (1 << 7) /* Status register write disable */
50 #define MR25_STATUS_BP_MASK (3 << 2) /* Block protect bits */
51 #define MR25_STATUS_BP_SHIFT (2)
52 #define MR25_STATUS_WEL (1 << 1) /* Write enable latch */
54 static __xdata uint8_t ao_mr25_mutex;
57 * This little array is abused to send and receive data. A particular
58 * caution -- the read and write addresses are written into the last
59 * three bytes of the array by ao_mr25_set_page_address and then the
60 * first byte is used by ao_mr25_write_enable, neither of which touch
61 * those last three bytes.
64 static __xdata uint8_t ao_mr25_instruction[4];
66 #define MR25_SELECT() ao_spi_get_mask(AO_MR25_SPI_CS_PORT,(1 << AO_MR25_SPI_CS_PIN),AO_MR25_SPI_BUS, AO_SPI_SPEED_FAST)
67 #define MR25_DESELECT() ao_spi_put_mask(AO_MR25_SPI_CS_PORT,(1 << AO_MR25_SPI_CS_PIN),AO_MR25_SPI_BUS)
70 * Set the write enable latch so that page program and sector
71 * erase commands will work. Also mark the chip as busy writing
72 * so that future operations will block until the WIP bit goes off
75 ao_mr25_write_enable(void)
78 ao_mr25_instruction[0] = MR25_WREN;
79 ao_spi_send(&ao_mr25_instruction, 1, AO_MR25_SPI_BUS);
85 ao_mr25_set_address(uint32_t pos)
87 ao_mr25_instruction[1] = pos >> 16;
88 ao_mr25_instruction[2] = pos >> 8;
89 ao_mr25_instruction[3] = pos;
93 * Erase the specified sector (no-op for MRAM)
96 ao_storage_erase(uint32_t pos) __reentrant
98 if (pos >= ao_storage_total || pos + ao_storage_block > ao_storage_total)
107 ao_storage_device_write(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
109 if (pos >= ao_storage_total || pos + len > ao_storage_total)
112 ao_mutex_get(&ao_mr25_mutex);
114 ao_mr25_set_address(pos);
115 ao_mr25_write_enable();
117 ao_mr25_instruction[0] = MR25_WRITE;
119 ao_spi_send(ao_mr25_instruction, 4, AO_MR25_SPI_BUS);
120 ao_spi_send(d, len, AO_MR25_SPI_BUS);
123 ao_mutex_put(&ao_mr25_mutex);
131 ao_storage_device_read(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
133 if (pos >= ao_storage_total || pos + len > ao_storage_total)
135 ao_mutex_get(&ao_mr25_mutex);
137 ao_mr25_set_address(pos);
139 ao_mr25_instruction[0] = MR25_READ;
141 ao_spi_send(ao_mr25_instruction, 4, AO_MR25_SPI_BUS);
142 ao_spi_recv(d, len, AO_MR25_SPI_BUS);
145 ao_mutex_put(&ao_mr25_mutex);
150 ao_storage_flush(void) __reentrant
155 ao_storage_setup(void)
160 ao_storage_device_info(void) __reentrant
162 printf ("Detected chips 1 size %d\n", ao_storage_total >> 8);
166 ao_storage_device_init(void)
168 ao_storage_total = 512 * 1024; /* 4Mb */
169 ao_storage_block = 256;
170 ao_storage_config = ao_storage_total - ao_storage_block;
171 ao_storage_unit = 256;
172 ao_spi_init_cs (AO_MR25_SPI_CS_PORT, (1 << AO_MR25_SPI_CS_PIN));