2 * Copyright © 2009 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; version 2 of the License.
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.
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.
19 #include "at45db161d.h"
21 /* Total bytes of available storage */
22 __xdata uint32_t ao_storage_total;
24 /* Block size - device is erased in these units. At least 256 bytes */
25 __xdata uint32_t ao_storage_block;
27 /* Byte offset of config block. Will be ao_storage_block bytes long */
28 __xdata uint32_t ao_storage_config;
31 #define FLASH_CS_INDEX 1
33 #define FLASH_BLOCK_SIZE_MAX 512
35 __xdata uint8_t ao_flash_mutex;
37 #define ao_flash_delay() do { \
43 void ao_flash_cs_low(void)
50 void ao_flash_cs_high(void)
57 struct ao_flash_instruction {
60 } __xdata ao_flash_instruction;
63 ao_flash_set_pagesize_512(void)
66 ao_flash_instruction.instruction = FLASH_SET_CONFIG;
67 ao_flash_instruction.address[0] = FLASH_SET_512_BYTE_0;
68 ao_flash_instruction.address[1] = FLASH_SET_512_BYTE_1;
69 ao_flash_instruction.address[2] = FLASH_SET_512_BYTE_2;
70 ao_spi_send(&ao_flash_instruction, 4);
76 ao_flash_read_status(void)
79 ao_flash_instruction.instruction = FLASH_READ_STATUS;
80 ao_spi_send(&ao_flash_instruction, 1);
81 ao_spi_recv(&ao_flash_instruction, 1);
83 return ao_flash_instruction.instruction;
86 #define FLASH_BLOCK_NONE 0xffff
88 static __xdata uint8_t ao_flash_data[FLASH_BLOCK_SIZE_MAX];
89 static __pdata uint16_t ao_flash_block = FLASH_BLOCK_NONE;
90 static __pdata uint8_t ao_flash_block_dirty;
91 static __pdata uint8_t ao_flash_write_pending;
92 static __pdata uint8_t ao_flash_setup_done;
93 static __data uint8_t ao_flash_block_shift;
94 static __data uint16_t ao_flash_block_size;
95 static __data uint16_t ao_flash_block_mask;
98 ao_storage_setup(void)
102 if (ao_flash_setup_done)
105 ao_mutex_get(&ao_flash_mutex);
106 if (ao_flash_setup_done) {
107 ao_mutex_put(&ao_flash_mutex);
111 /* On first use, check to see if the flash chip has
112 * been programmed to use 512 byte pages. If not, do so.
113 * And then, because the flash part must be power cycled
114 * for that change to take effect, panic.
116 status = ao_flash_read_status();
118 if (!(status & FLASH_STATUS_PAGESIZE_512)) {
119 ao_flash_set_pagesize_512();
120 ao_panic(AO_PANIC_FLASH);
123 switch (status & 0x3c) {
127 ao_flash_block_shift = 9;
128 ao_storage_total = ((uint32_t) 4 * (uint32_t) 1024 * (uint32_t) 1024);
133 ao_flash_block_shift = 9;
134 ao_storage_total = ((uint32_t) 2 * (uint32_t) 1024 * (uint32_t) 1024);
139 ao_flash_block_shift = 8;
140 ao_storage_total = ((uint32_t) 1024 * (uint32_t) 1024);
145 ao_flash_block_shift = 8;
146 ao_storage_total = ((uint32_t) 512 * (uint32_t) 1024);
151 ao_flash_block_shift = 8;
152 ao_storage_total = ((uint32_t) 256 * (uint32_t) 1024);
157 ao_flash_block_shift = 8;
158 ao_storage_total = ((uint32_t) 128 * (uint32_t) 1024);
162 ao_panic(AO_PANIC_FLASH);
164 ao_flash_block_size = 1 << ao_flash_block_shift;
166 ao_storage_block = ao_flash_block_size;
167 ao_storage_config = ao_storage_total - ao_storage_block;
169 ao_flash_setup_done = 1;
170 ao_mutex_put(&ao_flash_mutex);
174 ao_flash_wait_write(void)
176 if (ao_flash_write_pending) {
178 uint8_t status = ao_flash_read_status();
179 if ((status & FLASH_STATUS_RDY))
182 ao_flash_write_pending = 0;
186 /* Write the current block to the FLASHPROM */
188 ao_flash_write_block(void)
190 ao_flash_wait_write();
192 ao_flash_instruction.instruction = FLASH_WRITE;
194 /* 13/14 block bits + 9/8 byte bits (always 0) */
195 ao_flash_instruction.address[0] = ao_flash_block >> (16 - ao_flash_block_shift);
196 ao_flash_instruction.address[1] = ao_flash_block << (ao_flash_block_shift - 8);
197 ao_flash_instruction.address[2] = 0;
198 ao_spi_send(&ao_flash_instruction, 4);
199 ao_spi_send(ao_flash_data, ao_storage_block);
201 ao_flash_write_pending = 1;
204 /* Read the current block from the FLASHPROM */
206 ao_flash_read_block(void)
208 ao_flash_wait_write();
210 ao_flash_instruction.instruction = FLASH_READ;
212 /* 13/14 block bits + 9/8 byte bits (always 0) */
213 ao_flash_instruction.address[0] = ao_flash_block >> (16 - ao_flash_block_shift);
214 ao_flash_instruction.address[1] = ao_flash_block << (ao_flash_block_shift - 8);
215 ao_flash_instruction.address[2] = 0;
216 ao_spi_send(&ao_flash_instruction, 4);
217 ao_spi_recv(ao_flash_data, ao_flash_block_size);
222 ao_flash_flush_internal(void)
224 if (ao_flash_block_dirty) {
225 ao_flash_write_block();
226 ao_flash_block_dirty = 0;
231 ao_flash_fill(uint16_t block)
233 if (block != ao_flash_block) {
234 ao_flash_flush_internal();
235 ao_flash_block = block;
236 ao_flash_read_block();
241 ao_storage_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
248 if (pos >= ao_storage_total || pos + len > ao_storage_total)
252 /* Compute portion of transfer within
255 this_off = (uint16_t) pos & ao_flash_block_mask;
256 this_len = ao_flash_block_size - this_off;
257 block = (uint16_t) (pos >> ao_flash_block_shift);
261 /* Transfer the data */
262 ao_mutex_get(&ao_flash_mutex); {
263 if (this_len != ao_flash_block_size)
264 ao_flash_fill(block);
266 ao_flash_flush_internal();
267 ao_flash_block = block;
269 memcpy(ao_flash_data + this_off, buf, this_len);
270 ao_flash_block_dirty = 1;
271 } ao_mutex_put(&ao_flash_mutex);
273 /* See how much is left */
282 ao_storage_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
289 if (pos >= ao_storage_total || pos + len > ao_storage_total)
294 /* Compute portion of transfer within
297 this_off = (uint16_t) pos & ao_flash_block_mask;
298 this_len = ao_flash_block_size - this_off;
299 block = (uint16_t) (pos >> ao_flash_block_shift);
303 /* Transfer the data */
304 ao_mutex_get(&ao_flash_mutex); {
305 ao_flash_fill(block);
306 memcpy(buf, ao_flash_data + this_off, this_len);
307 } ao_mutex_put(&ao_flash_mutex);
309 /* See how much is left */
318 ao_storage_flush(void) __reentrant
320 ao_mutex_get(&ao_flash_mutex); {
321 ao_flash_flush_internal();
322 } ao_mutex_put(&ao_flash_mutex);
326 ao_storage_erase(uint32_t pos) __reentrant
328 ao_mutex_get(&ao_flash_mutex); {
329 uint16_t block = (uint16_t) (pos >> ao_flash_block_shift);
330 ao_flash_fill(block);
331 memset(ao_flash_data, 0xff, ao_flash_block_size);
332 ao_flash_block_dirty = 1;
333 } ao_mutex_put(&ao_flash_mutex);
338 flash_dump(void) __reentrant
340 static __xdata uint8_t b;
345 block = ao_cmd_lex_i;
346 if (ao_cmd_status != ao_cmd_success)
353 ao_cmd_put16((uint16_t) i);
356 ao_storage_read(((uint32_t) block << 8) | i, &b, 1);
364 flash_store(void) __reentrant
369 static __xdata uint8_t b;
373 block = ao_cmd_lex_i;
376 addr = ((uint32_t) block << 8) | i;
379 if (ao_cmd_status != ao_cmd_success)
383 if (ao_cmd_status != ao_cmd_success)
386 ao_storage_write(addr, &b, 1);
393 flash_status(void) __reentrant
398 ao_mutex_get(&ao_flash_mutex); {
399 status = ao_flash_read_status();
400 printf ("Flash status: 0x%02x\n", status);
401 printf ("Flash block shift: %d\n", ao_flash_block_shift);
402 printf ("Flash block size: %d\n", ao_flash_block_size);
403 printf ("Flash block mask: %d\n", ao_flash_block_mask);
404 printf ("Flash device size: %ld\n", ao_storage_total);
405 } ao_mutex_put(&ao_flash_mutex);
408 __code struct ao_cmds ao_flash_cmds[] = {
409 { 'e', flash_dump, "e <block> Dump a block of flash data" },
410 { 'w', flash_store, "w <block> <start> <len> <data> ... Write data to flash" },
411 { 'f', flash_status, "f Show flash status register" },
412 { 0, flash_store, NULL },
416 * To initialize the chip, set up the CS line and
420 ao_storage_init(void)
424 P1DIR |= (1 << FLASH_CS_INDEX);
425 P1SEL &= ~(1 << FLASH_CS_INDEX);
426 ao_cmd_register(&ao_flash_cmds[0]);