altos: Add MR25 everspin MRAM driver
[fw/altos] / src / drivers / ao_mr25.c
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 /* Total bytes of available storage */
21 __pdata uint32_t        ao_storage_total;
22
23 /* Block size - device is erased in these units. At least 256 bytes */
24 __pdata uint32_t        ao_storage_block;
25
26 /* Byte offset of config block. Will be ao_storage_block bytes long */
27 __pdata uint32_t        ao_storage_config;
28
29 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
30 __pdata uint16_t        ao_storage_unit;
31
32 /*
33  * MRAM is entirely random access; no erase operations are required,
34  * nor are reads or writes restricted to a particular alignment.
35  */
36
37 #define MR25_WREN       0x06    /* Write Enable */
38 #define MR25_WRDI       0x04    /* Write Disable */
39 #define MR25_RDSR       0x05    /* Read Status Register */
40 #define MR25_WRSR       0x01    /* Write Status Register */
41 #define MR25_READ       0x03    /* Read Data Bytes */
42 #define MR25_WRITE      0x02    /* Write Data Bytes */
43
44 /*
45  * Status register bits
46  */
47
48 #define MR25_STATUS_SRWD        (1 << 7)        /* Status register write disable */
49 #define MR25_STATUS_BP_MASK     (3 << 2)        /* Block protect bits */
50 #define MR25_STATUS_BP_SHIFT    (2)
51 #define MR25_STATUS_WEL         (1 << 1)        /* Write enable latch */
52
53 static __xdata uint8_t ao_mr25_mutex;
54
55 /*
56  * This little array is abused to send and receive data. A particular
57  * caution -- the read and write addresses are written into the last
58  * three bytes of the array by ao_mr25_set_page_address and then the
59  * first byte is used by ao_mr25_write_enable, neither of which touch
60  * those last three bytes.
61  */
62
63 static __xdata uint8_t  ao_mr25_instruction[4];
64
65 #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)
66 #define MR25_DESELECT()         ao_spi_put_mask(AO_MR25_SPI_CS_PORT,(1 << AO_MR25_SPI_CS_PIN),AO_MR25_SPI_BUS)
67
68 /*
69  * Set the write enable latch so that page program and sector
70  * erase commands will work. Also mark the chip as busy writing
71  * so that future operations will block until the WIP bit goes off
72  */
73 static void
74 ao_mr25_write_enable(void)
75 {
76         MR25_SELECT();
77         ao_mr25_instruction[0] = MR25_WREN;
78         ao_spi_send(&ao_mr25_instruction, 1, AO_MR25_SPI_BUS);
79         MR25_DESELECT();
80 }
81
82
83 static void
84 ao_mr25_set_address(uint32_t pos)
85 {
86         ao_mr25_instruction[1] = pos >> 16;
87         ao_mr25_instruction[2] = pos >> 8;
88         ao_mr25_instruction[3] = pos;
89 }
90
91 /*
92  * Erase the specified sector (no-op for MRAM)
93  */
94 uint8_t
95 ao_storage_erase(uint32_t pos) __reentrant
96 {
97         if (pos >= ao_storage_total || pos + ao_storage_block > ao_storage_total)
98                 return 0;
99         return 1;
100 }
101
102 /*
103  * Write to flash
104  */
105 uint8_t
106 ao_storage_device_write(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
107 {
108         if (pos >= ao_storage_total || pos + len > ao_storage_total)
109                 return 0;
110
111         ao_mutex_get(&ao_mr25_mutex);
112
113         ao_mr25_set_address(pos);
114         ao_mr25_write_enable();
115
116         ao_mr25_instruction[0] = MR25_WRITE;
117         MR25_SELECT();
118         ao_spi_send(ao_mr25_instruction, 4, AO_MR25_SPI_BUS);
119         ao_spi_send(d, len, AO_MR25_SPI_BUS);
120         MR25_DESELECT();
121
122         ao_mutex_put(&ao_mr25_mutex);
123         return 1;
124 }
125
126 /*
127  * Read from flash
128  */
129 uint8_t
130 ao_storage_device_read(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
131 {
132         if (pos >= ao_storage_total || pos + len > ao_storage_total)
133                 return 0;
134         ao_mutex_get(&ao_mr25_mutex);
135
136         ao_mr25_set_address(pos);
137
138         ao_mr25_instruction[0] = MR25_READ;
139         MR25_SELECT();
140         ao_spi_send(ao_mr25_instruction, 4, AO_MR25_SPI_BUS);
141         ao_spi_recv(d, len, AO_MR25_SPI_BUS);
142         MR25_DESELECT();
143
144         ao_mutex_put(&ao_mr25_mutex);
145         return 1;
146 }
147
148 void
149 ao_storage_flush(void) __reentrant
150 {
151 }
152
153 void
154 ao_storage_setup(void)
155 {
156 }
157
158 void
159 ao_storage_device_info(void) __reentrant
160 {
161         printf ("Detected chips 1 size %d\n", ao_storage_total >> 8);
162 }
163
164 void
165 ao_storage_device_init(void)
166 {
167         ao_storage_total = 512 * 1024;  /* 4Mb */
168         ao_storage_block = 256;
169         ao_storage_config = ao_storage_total - ao_storage_block;
170         ao_storage_unit = 256;
171         ao_spi_init_cs (AO_MR25_SPI_CS_PORT, (1 << AO_MR25_SPI_CS_PIN));
172 }