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