altos: Switch flash drivers __xdata to __pdata
[fw/altos] / src / ao_ee.c
1 /*
2  * Copyright © 2009 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 #include "25lc1024.h"
20
21 #define EE_BLOCK_SIZE   ((uint16_t) (256))
22 #define EE_BLOCK_SHIFT  8
23 #define EE_DEVICE_SIZE  ((uint32_t) 128 * (uint32_t) 1024)
24
25 /* Total bytes of available storage */
26 __pdata uint32_t        ao_storage_total;
27
28 /* Block size - device is erased in these units. At least 256 bytes */
29 __pdata uint32_t        ao_storage_block;
30
31 /* Byte offset of config block. Will be ao_storage_block bytes long */
32 __pdata uint32_t        ao_storage_config;
33
34 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
35 __pdata uint16_t        ao_storage_unit;
36
37 /*
38  * Using SPI on USART 0, with P1_2 as the chip select
39  */
40
41 #define EE_CS           P1_2
42 #define EE_CS_INDEX     2
43
44 static __xdata uint8_t ao_ee_mutex;
45
46 #define ao_ee_delay() do { \
47         _asm nop _endasm; \
48         _asm nop _endasm; \
49         _asm nop _endasm; \
50 } while(0)
51
52 static void ao_ee_cs_low(void)
53 {
54         ao_ee_delay();
55         EE_CS = 0;
56         ao_ee_delay();
57 }
58
59 static void ao_ee_cs_high(void)
60 {
61         ao_ee_delay();
62         EE_CS = 1;
63         ao_ee_delay();
64 }
65
66 struct ao_ee_instruction {
67         uint8_t instruction;
68         uint8_t address[3];
69 } __xdata ao_ee_instruction;
70
71 static void
72 ao_ee_write_enable(void)
73 {
74         ao_ee_cs_low();
75         ao_ee_instruction.instruction = EE_WREN;
76         ao_spi_send(&ao_ee_instruction, 1);
77         ao_ee_cs_high();
78 }
79
80 static uint8_t
81 ao_ee_rdsr(void)
82 {
83         ao_ee_cs_low();
84         ao_ee_instruction.instruction = EE_RDSR;
85         ao_spi_send(&ao_ee_instruction, 1);
86         ao_spi_recv(&ao_ee_instruction, 1);
87         ao_ee_cs_high();
88         return ao_ee_instruction.instruction;
89 }
90
91 static void
92 ao_ee_wrsr(uint8_t status)
93 {
94         ao_ee_cs_low();
95         ao_ee_instruction.instruction = EE_WRSR;
96         ao_ee_instruction.address[0] = status;
97         ao_spi_send(&ao_ee_instruction, 2);
98         ao_ee_cs_high();
99 }
100
101 #define EE_BLOCK_NONE   0xffff
102
103 static __xdata uint8_t ao_ee_data[EE_BLOCK_SIZE];
104 static __pdata uint16_t ao_ee_block = EE_BLOCK_NONE;
105 static __pdata uint8_t  ao_ee_block_dirty;
106
107 /* Write the current block to the EEPROM */
108 static void
109 ao_ee_write_block(void)
110 {
111         uint8_t status;
112
113         status = ao_ee_rdsr();
114         if (status & (EE_STATUS_BP0|EE_STATUS_BP1|EE_STATUS_WPEN)) {
115                 status &= ~(EE_STATUS_BP0|EE_STATUS_BP1|EE_STATUS_WPEN);
116                 ao_ee_wrsr(status);
117         }
118         ao_ee_write_enable();
119         ao_ee_cs_low();
120         ao_ee_instruction.instruction = EE_WRITE;
121         ao_ee_instruction.address[0] = ao_ee_block >> 8;
122         ao_ee_instruction.address[1] = ao_ee_block;
123         ao_ee_instruction.address[2] = 0;
124         ao_spi_send(&ao_ee_instruction, 4);
125         ao_spi_send(ao_ee_data, EE_BLOCK_SIZE);
126         ao_ee_cs_high();
127         for (;;) {
128                 uint8_t status = ao_ee_rdsr();
129                 if ((status & EE_STATUS_WIP) == 0)
130                         break;
131         }
132 }
133
134 /* Read the current block from the EEPROM */
135 static void
136 ao_ee_read_block(void)
137 {
138         ao_ee_cs_low();
139         ao_ee_instruction.instruction = EE_READ;
140         ao_ee_instruction.address[0] = ao_ee_block >> 8;
141         ao_ee_instruction.address[1] = ao_ee_block;
142         ao_ee_instruction.address[2] = 0;
143         ao_spi_send(&ao_ee_instruction, 4);
144         ao_spi_recv(ao_ee_data, EE_BLOCK_SIZE);
145         ao_ee_cs_high();
146 }
147
148 static void
149 ao_ee_flush_internal(void)
150 {
151         if (ao_ee_block_dirty) {
152                 ao_ee_write_block();
153                 ao_ee_block_dirty = 0;
154         }
155 }
156
157 static void
158 ao_ee_fill(uint16_t block)
159 {
160         if (block != ao_ee_block) {
161                 ao_ee_flush_internal();
162                 ao_ee_block = block;
163                 ao_ee_read_block();
164         }
165 }
166
167 uint8_t
168 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
169 {
170         uint16_t block = (uint16_t) (pos >> EE_BLOCK_SHIFT);
171
172         /* Transfer the data */
173         ao_mutex_get(&ao_ee_mutex); {
174                 if (len != EE_BLOCK_SIZE)
175                         ao_ee_fill(block);
176                 else {
177                         ao_ee_flush_internal();
178                         ao_ee_block = block;
179                 }
180                 memcpy(ao_ee_data + (uint16_t) (pos & 0xff), buf, len);
181                 ao_ee_block_dirty = 1;
182         } ao_mutex_put(&ao_ee_mutex);
183         return 1;
184 }
185
186 uint8_t
187 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
188 {
189         uint16_t block = (uint16_t) (pos >> EE_BLOCK_SHIFT);
190
191         /* Transfer the data */
192         ao_mutex_get(&ao_ee_mutex); {
193                 ao_ee_fill(block);
194                 memcpy(buf, ao_ee_data + (uint16_t) (pos & 0xff), len);
195         } ao_mutex_put(&ao_ee_mutex);
196         return 1;
197 }
198
199 void
200 ao_storage_flush(void) __reentrant
201 {
202         ao_mutex_get(&ao_ee_mutex); {
203                 ao_ee_flush_internal();
204         } ao_mutex_put(&ao_ee_mutex);
205 }
206
207 uint8_t
208 ao_storage_erase(uint32_t pos) __reentrant
209 {
210         ao_mutex_get(&ao_ee_mutex); {
211                 ao_ee_flush_internal();
212                 ao_ee_block = (uint16_t) (pos >> EE_BLOCK_SHIFT);
213                 memset(ao_ee_data, 0xff, EE_BLOCK_SIZE);
214                 ao_ee_block_dirty = 1;
215         } ao_mutex_put(&ao_ee_mutex);
216         return 1;
217 }
218
219 static void
220 ee_store(void) __reentrant
221 {
222 }
223
224 void
225 ao_storage_setup(void)
226 {
227         if (ao_storage_total == 0) {
228                 ao_storage_total = EE_DEVICE_SIZE;
229                 ao_storage_block = EE_BLOCK_SIZE;
230                 ao_storage_config = EE_DEVICE_SIZE - EE_BLOCK_SIZE;
231                 ao_storage_unit = EE_BLOCK_SIZE;
232         }
233 }
234
235 void
236 ao_storage_device_info(void) __reentrant
237 {
238 }
239
240 /*
241  * To initialize the chip, set up the CS line and
242  * the SPI interface
243  */
244 void
245 ao_storage_device_init(void)
246 {
247         /* set up CS */
248         EE_CS = 1;
249         P1DIR |= (1 << EE_CS_INDEX);
250         P1SEL &= ~(1 << EE_CS_INDEX);
251 }