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;
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;
34 #define FLASH_CS_INDEX 1
36 #define FLASH_BLOCK_SIZE_MAX 512
38 __xdata uint8_t ao_flash_mutex;
40 #define ao_flash_delay() do { \
46 void ao_flash_cs_low(void)
53 void ao_flash_cs_high(void)
60 struct ao_flash_instruction {
63 } __xdata ao_flash_instruction;
66 ao_flash_set_pagesize_512(void)
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);
79 ao_flash_read_status(void)
82 ao_flash_instruction.instruction = FLASH_READ_STATUS;
83 ao_spi_send(&ao_flash_instruction, 1);
84 ao_spi_recv(&ao_flash_instruction, 1);
86 return ao_flash_instruction.instruction;
89 #define FLASH_BLOCK_NONE 0xffff
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 __data uint8_t ao_flash_block_shift;
97 static __data uint16_t ao_flash_block_size;
98 static __data uint16_t ao_flash_block_mask;
101 ao_storage_setup(void)
105 if (ao_flash_setup_done)
108 ao_mutex_get(&ao_flash_mutex);
109 if (ao_flash_setup_done) {
110 ao_mutex_put(&ao_flash_mutex);
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.
119 status = ao_flash_read_status();
121 if (!(status & FLASH_STATUS_PAGESIZE_512)) {
122 ao_flash_set_pagesize_512();
123 ao_panic(AO_PANIC_FLASH);
126 switch (status & 0x3c) {
130 ao_flash_block_shift = 9;
131 ao_storage_total = ((uint32_t) 4 * (uint32_t) 1024 * (uint32_t) 1024);
136 ao_flash_block_shift = 9;
137 ao_storage_total = ((uint32_t) 2 * (uint32_t) 1024 * (uint32_t) 1024);
142 ao_flash_block_shift = 8;
143 ao_storage_total = ((uint32_t) 1024 * (uint32_t) 1024);
148 ao_flash_block_shift = 8;
149 ao_storage_total = ((uint32_t) 512 * (uint32_t) 1024);
154 ao_flash_block_shift = 8;
155 ao_storage_total = ((uint32_t) 256 * (uint32_t) 1024);
160 ao_flash_block_shift = 8;
161 ao_storage_total = ((uint32_t) 128 * (uint32_t) 1024);
165 ao_panic(AO_PANIC_FLASH);
167 ao_flash_block_size = 1 << ao_flash_block_shift;
168 ao_flash_block_mask = ao_flash_block_size - 1;
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;
174 ao_flash_setup_done = 1;
175 ao_mutex_put(&ao_flash_mutex);
179 ao_flash_wait_write(void)
181 if (ao_flash_write_pending) {
183 uint8_t status = ao_flash_read_status();
184 if ((status & FLASH_STATUS_RDY))
187 ao_flash_write_pending = 0;
191 /* Write the current block to the FLASHPROM */
193 ao_flash_write_block(void)
195 ao_flash_wait_write();
197 ao_flash_instruction.instruction = FLASH_WRITE;
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);
206 ao_flash_write_pending = 1;
209 /* Read the current block from the FLASHPROM */
211 ao_flash_read_block(void)
213 ao_flash_wait_write();
215 ao_flash_instruction.instruction = FLASH_READ;
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);
227 ao_flash_flush_internal(void)
229 if (ao_flash_block_dirty) {
230 ao_flash_write_block();
231 ao_flash_block_dirty = 0;
236 ao_flash_fill(uint16_t block)
238 if (block != ao_flash_block) {
239 ao_flash_flush_internal();
240 ao_flash_block = block;
241 ao_flash_read_block();
246 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
248 uint16_t block = (uint16_t) (pos >> ao_flash_block_shift);
250 /* Transfer the data */
251 ao_mutex_get(&ao_flash_mutex); {
252 if (len != ao_flash_block_size)
253 ao_flash_fill(block);
255 ao_flash_flush_internal();
256 ao_flash_block = block;
258 memcpy(ao_flash_data + (uint16_t) (pos & ao_flash_block_mask),
261 ao_flash_block_dirty = 1;
262 } ao_mutex_put(&ao_flash_mutex);
267 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
269 uint16_t block = (uint16_t) (pos >> ao_flash_block_shift);
271 /* Transfer the data */
272 ao_mutex_get(&ao_flash_mutex); {
273 ao_flash_fill(block);
275 ao_flash_data + (uint16_t) (pos & ao_flash_block_mask),
277 } ao_mutex_put(&ao_flash_mutex);
282 ao_storage_flush(void) __reentrant
284 ao_mutex_get(&ao_flash_mutex); {
285 ao_flash_flush_internal();
286 } ao_mutex_put(&ao_flash_mutex);
290 ao_storage_erase(uint32_t pos) __reentrant
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);
302 ao_storage_device_info(void) __reentrant
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);
318 * To initialize the chip, set up the CS line and
322 ao_storage_device_init(void)
326 P1DIR |= (1 << FLASH_CS_INDEX);
327 P1SEL &= ~(1 << FLASH_CS_INDEX);