altos: Make cmd echo per-connection instead of global
[fw/altos] / src / ao_flash.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 "at45db161d.h"
20
21 /* Total bytes of available storage */
22 __xdata uint32_t        ao_storage_total;
23
24 /* Block size - device is erased in these units. At least 256 bytes */
25 __xdata uint32_t        ao_storage_block;
26
27 /* Byte offset of config block. Will be ao_storage_block bytes long */
28 __xdata uint32_t        ao_storage_config;
29
30 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
31 __xdata uint16_t        ao_storage_unit;
32
33 #define FLASH_CS                P1_1
34 #define FLASH_CS_INDEX          1
35
36 #define FLASH_BLOCK_SIZE_MAX    512
37
38 __xdata uint8_t ao_flash_mutex;
39
40 #define ao_flash_delay() do { \
41         _asm nop _endasm; \
42         _asm nop _endasm; \
43         _asm nop _endasm; \
44 } while(0)
45
46 void ao_flash_cs_low(void)
47 {
48         ao_flash_delay();
49         FLASH_CS = 0;
50         ao_flash_delay();
51 }
52
53 void ao_flash_cs_high(void)
54 {
55         ao_flash_delay();
56         FLASH_CS = 1;
57         ao_flash_delay();
58 }
59
60 struct ao_flash_instruction {
61         uint8_t instruction;
62         uint8_t address[3];
63 } __xdata ao_flash_instruction;
64
65 static void
66 ao_flash_set_pagesize_512(void)
67 {
68         ao_flash_cs_low();
69         ao_flash_instruction.instruction = FLASH_SET_CONFIG;
70         ao_flash_instruction.address[0] = FLASH_SET_512_BYTE_0;
71         ao_flash_instruction.address[1] = FLASH_SET_512_BYTE_1;
72         ao_flash_instruction.address[2] = FLASH_SET_512_BYTE_2;
73         ao_spi_send(&ao_flash_instruction, 4);
74         ao_flash_cs_high();
75 }
76
77
78 static uint8_t
79 ao_flash_read_status(void)
80 {
81         ao_flash_cs_low();
82         ao_flash_instruction.instruction = FLASH_READ_STATUS;
83         ao_spi_send(&ao_flash_instruction, 1);
84         ao_spi_recv(&ao_flash_instruction, 1);
85         ao_flash_cs_high();
86         return ao_flash_instruction.instruction;
87 }
88
89 #define FLASH_BLOCK_NONE        0xffff
90
91 static __xdata uint8_t ao_flash_data[FLASH_BLOCK_SIZE_MAX];
92 static __pdata uint16_t ao_flash_block = FLASH_BLOCK_NONE;
93 static __pdata uint8_t  ao_flash_block_dirty;
94 static __pdata uint8_t  ao_flash_write_pending;
95 static __pdata uint8_t  ao_flash_setup_done;
96 static __pdata uint8_t  ao_flash_block_shift;
97 static __pdata uint16_t ao_flash_block_size;
98 static __pdata uint16_t ao_flash_block_mask;
99
100 void
101 ao_storage_setup(void) __reentrant
102 {
103         uint8_t status;
104
105         if (ao_flash_setup_done)
106                 return;
107
108         ao_mutex_get(&ao_flash_mutex);
109         if (ao_flash_setup_done) {
110                 ao_mutex_put(&ao_flash_mutex);
111                 return;
112         }
113
114         /* On first use, check to see if the flash chip has
115          * been programmed to use 512 byte pages. If not, do so.
116          * And then, because the flash part must be power cycled
117          * for that change to take effect, panic.
118          */
119         status = ao_flash_read_status();
120
121         if (!(status & FLASH_STATUS_PAGESIZE_512)) {
122                 ao_flash_set_pagesize_512();
123                 ao_panic(AO_PANIC_FLASH);
124         }
125
126         switch (status & 0x3c) {
127
128         /* AT45DB321D */
129         case 0x34:
130                 ao_flash_block_shift = 9;
131                 ao_storage_total = ((uint32_t) 4 * (uint32_t) 1024 * (uint32_t) 1024);
132                 break;
133
134         /* AT45DB161D */
135         case 0x2c:
136                 ao_flash_block_shift = 9;
137                 ao_storage_total = ((uint32_t) 2 * (uint32_t) 1024 * (uint32_t) 1024);
138                 break;
139
140         /* AT45DB081D */
141         case 0x24:
142                 ao_flash_block_shift = 8;
143                 ao_storage_total = ((uint32_t) 1024 * (uint32_t) 1024);
144                 break;
145
146         /* AT45DB041D */
147         case 0x1c:
148                 ao_flash_block_shift = 8;
149                 ao_storage_total = ((uint32_t) 512 * (uint32_t) 1024);
150                 break;
151
152         /* AT45DB021D */
153         case 0x14:
154                 ao_flash_block_shift = 8;
155                 ao_storage_total = ((uint32_t) 256 * (uint32_t) 1024);
156                 break;
157
158         /* AT45DB011D */
159         case 0x0c:
160                 ao_flash_block_shift = 8;
161                 ao_storage_total = ((uint32_t) 128 * (uint32_t) 1024);
162                 break;
163
164         default:
165                 ao_panic(AO_PANIC_FLASH);
166         }
167         ao_flash_block_size = 1 << ao_flash_block_shift;
168         ao_flash_block_mask = ao_flash_block_size - 1;
169
170         ao_storage_block = ao_flash_block_size;
171         ao_storage_config = ao_storage_total - ao_storage_block;
172         ao_storage_unit = ao_flash_block_size;
173
174         ao_flash_setup_done = 1;
175         ao_mutex_put(&ao_flash_mutex);
176 }
177
178 static void
179 ao_flash_wait_write(void)
180 {
181         if (ao_flash_write_pending) {
182                 for (;;) {
183                         uint8_t status = ao_flash_read_status();
184                         if ((status & FLASH_STATUS_RDY))
185                                 break;
186                 }
187                 ao_flash_write_pending = 0;
188         }
189 }
190
191 /* Write the current block to the FLASHPROM */
192 static void
193 ao_flash_write_block(void)
194 {
195         ao_flash_wait_write();
196         ao_flash_cs_low();
197         ao_flash_instruction.instruction = FLASH_WRITE;
198
199         /* 13/14 block bits + 9/8 byte bits (always 0) */
200         ao_flash_instruction.address[0] = ao_flash_block >> (16 - ao_flash_block_shift);
201         ao_flash_instruction.address[1] = ao_flash_block << (ao_flash_block_shift - 8);
202         ao_flash_instruction.address[2] = 0;
203         ao_spi_send(&ao_flash_instruction, 4);
204         ao_spi_send(ao_flash_data, ao_storage_block);
205         ao_flash_cs_high();
206         ao_flash_write_pending = 1;
207 }
208
209 /* Read the current block from the FLASHPROM */
210 static void
211 ao_flash_read_block(void)
212 {
213         ao_flash_wait_write();
214         ao_flash_cs_low();
215         ao_flash_instruction.instruction = FLASH_READ;
216
217         /* 13/14 block bits + 9/8 byte bits (always 0) */
218         ao_flash_instruction.address[0] = ao_flash_block >> (16 - ao_flash_block_shift);
219         ao_flash_instruction.address[1] = ao_flash_block << (ao_flash_block_shift - 8);
220         ao_flash_instruction.address[2] = 0;
221         ao_spi_send(&ao_flash_instruction, 4);
222         ao_spi_recv(ao_flash_data, ao_flash_block_size);
223         ao_flash_cs_high();
224 }
225
226 static void
227 ao_flash_flush_internal(void)
228 {
229         if (ao_flash_block_dirty) {
230                 ao_flash_write_block();
231                 ao_flash_block_dirty = 0;
232         }
233 }
234
235 static void
236 ao_flash_fill(uint16_t block)
237 {
238         if (block != ao_flash_block) {
239                 ao_flash_flush_internal();
240                 ao_flash_block = block;
241                 ao_flash_read_block();
242         }
243 }
244
245 uint8_t
246 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
247 {
248         uint16_t block = (uint16_t) (pos >> ao_flash_block_shift);
249
250         /* Transfer the data */
251         ao_mutex_get(&ao_flash_mutex); {
252                 if (len != ao_flash_block_size)
253                         ao_flash_fill(block);
254                 else {
255                         ao_flash_flush_internal();
256                         ao_flash_block = block;
257                 }
258                 memcpy(ao_flash_data + (uint16_t) (pos & ao_flash_block_mask),
259                        buf,
260                        len);
261                 ao_flash_block_dirty = 1;
262         } ao_mutex_put(&ao_flash_mutex);
263         return 1;
264 }
265
266 uint8_t
267 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
268 {
269         uint16_t block = (uint16_t) (pos >> ao_flash_block_shift);
270
271         /* Transfer the data */
272         ao_mutex_get(&ao_flash_mutex); {
273                 ao_flash_fill(block);
274                 memcpy(buf,
275                        ao_flash_data + (uint16_t) (pos & ao_flash_block_mask),
276                        len);
277         } ao_mutex_put(&ao_flash_mutex);
278         return 1;
279 }
280
281 void
282 ao_storage_flush(void) __reentrant
283 {
284         ao_mutex_get(&ao_flash_mutex); {
285                 ao_flash_flush_internal();
286         } ao_mutex_put(&ao_flash_mutex);
287 }
288
289 uint8_t
290 ao_storage_erase(uint32_t pos) __reentrant
291 {
292         ao_mutex_get(&ao_flash_mutex); {
293                 ao_flash_flush_internal();
294                 ao_flash_block = (uint16_t) (pos >> ao_flash_block_shift);
295                 memset(ao_flash_data, 0xff, ao_flash_block_size);
296                 ao_flash_block_dirty = 1;
297         } ao_mutex_put(&ao_flash_mutex);
298         return 1;
299 }
300
301 void
302 ao_storage_device_info(void) __reentrant
303 {
304         uint8_t status;
305
306         ao_storage_setup();
307         ao_mutex_get(&ao_flash_mutex); {
308                 status = ao_flash_read_status();
309                 printf ("Flash status: 0x%02x\n", status);
310                 printf ("Flash block shift: %d\n", ao_flash_block_shift);
311                 printf ("Flash block size: %d\n", ao_flash_block_size);
312                 printf ("Flash block mask: %d\n", ao_flash_block_mask);
313                 printf ("Flash device size: %ld\n", ao_storage_total);
314         } ao_mutex_put(&ao_flash_mutex);
315 }
316
317 /*
318  * To initialize the chip, set up the CS line and
319  * the SPI interface
320  */
321 void
322 ao_storage_device_init(void)
323 {
324         /* set up CS */
325         FLASH_CS = 1;
326         P1DIR |= (1 << FLASH_CS_INDEX);
327         P1SEL &= ~(1 << FLASH_CS_INDEX);
328 }