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; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
20 #include "ao_at45db161d.h"
22 /* Total bytes of available storage */
23 uint32_t ao_storage_total;
25 /* Block size - device is erased in these units. At least 256 bytes */
26 uint32_t ao_storage_block;
28 /* Byte offset of config block. Will be ao_storage_block bytes long */
29 uint32_t ao_storage_config;
31 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
32 uint16_t ao_storage_unit;
35 #define FLASH_CS_INDEX 1
37 #define FLASH_BLOCK_SIZE_MAX 512
39 uint8_t ao_flash_mutex;
41 #define ao_flash_delay() do { \
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)
49 #define ao_flash_cs_high() ao_spi_put_bit(FLASH_CS_PORT, FLASH_CS_PIN, FLASH_CS, AO_FLASH_SPI_BUS)
51 struct ao_flash_instruction {
54 } ao_flash_instruction;
57 ao_flash_set_pagesize_512(void)
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);
70 ao_flash_read_status(void)
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);
77 return ao_flash_instruction.instruction;
80 #define FLASH_BLOCK_NONE 0xffff
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;
92 ao_storage_setup(void)
96 if (ao_flash_setup_done)
99 ao_mutex_get(&ao_flash_mutex);
100 if (ao_flash_setup_done) {
101 ao_mutex_put(&ao_flash_mutex);
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.
110 status = ao_flash_read_status();
112 if (!(status & FLASH_STATUS_PAGESIZE_512)) {
113 ao_flash_set_pagesize_512();
114 ao_panic(AO_PANIC_FLASH);
117 switch (status & 0x3c) {
121 ao_flash_block_shift = 9;
122 ao_storage_total = ((uint32_t) 4 * (uint32_t) 1024 * (uint32_t) 1024);
127 ao_flash_block_shift = 9;
128 ao_storage_total = ((uint32_t) 2 * (uint32_t) 1024 * (uint32_t) 1024);
133 ao_flash_block_shift = 8;
134 ao_storage_total = ((uint32_t) 1024 * (uint32_t) 1024);
139 ao_flash_block_shift = 8;
140 ao_storage_total = ((uint32_t) 512 * (uint32_t) 1024);
145 ao_flash_block_shift = 8;
146 ao_storage_total = ((uint32_t) 256 * (uint32_t) 1024);
151 ao_flash_block_shift = 8;
152 ao_storage_total = ((uint32_t) 128 * (uint32_t) 1024);
156 ao_panic(AO_PANIC_FLASH);
158 ao_flash_block_size = 1 << ao_flash_block_shift;
159 ao_flash_block_mask = ao_flash_block_size - 1;
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;
165 ao_flash_setup_done = 1;
166 ao_mutex_put(&ao_flash_mutex);
170 ao_flash_wait_write(void)
172 if (ao_flash_write_pending) {
174 uint8_t status = ao_flash_read_status();
175 if ((status & FLASH_STATUS_RDY))
178 ao_flash_write_pending = 0;
182 /* Write the current block to the FLASHPROM */
184 ao_flash_write_block(void)
186 ao_flash_wait_write();
188 ao_flash_instruction.instruction = FLASH_WRITE;
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);
197 ao_flash_write_pending = 1;
200 /* Read the current block from the FLASHPROM */
202 ao_flash_read_block(void)
204 ao_flash_wait_write();
206 ao_flash_instruction.instruction = FLASH_READ;
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);
218 ao_flash_flush_internal(void)
220 if (ao_flash_block_dirty) {
221 ao_flash_write_block();
222 ao_flash_block_dirty = 0;
227 ao_flash_fill(uint16_t block)
229 if (block != ao_flash_block) {
230 ao_flash_flush_internal();
231 ao_flash_block = block;
232 ao_flash_read_block();
237 ao_storage_device_write(uint32_t pos, void *buf, uint16_t len)
239 uint16_t block = (uint16_t) (pos >> ao_flash_block_shift);
241 /* Transfer the data */
242 ao_mutex_get(&ao_flash_mutex); {
243 if (len != ao_flash_block_size)
244 ao_flash_fill(block);
246 ao_flash_flush_internal();
247 ao_flash_block = block;
249 memcpy(ao_flash_data + (uint16_t) (pos & ao_flash_block_mask),
252 ao_flash_block_dirty = 1;
253 } ao_mutex_put(&ao_flash_mutex);
258 ao_storage_device_read(uint32_t pos, void *buf, uint16_t len)
260 uint16_t block = (uint16_t) (pos >> ao_flash_block_shift);
262 /* Transfer the data */
263 ao_mutex_get(&ao_flash_mutex); {
264 ao_flash_fill(block);
266 ao_flash_data + (uint16_t) (pos & ao_flash_block_mask),
268 } ao_mutex_put(&ao_flash_mutex);
273 ao_storage_flush(void)
275 ao_mutex_get(&ao_flash_mutex); {
276 ao_flash_flush_internal();
277 } ao_mutex_put(&ao_flash_mutex);
281 ao_storage_erase(uint32_t pos)
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);
293 ao_storage_device_info(void)
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);
309 * To initialize the chip, set up the CS line and
313 ao_storage_device_init(void)
317 P1DIR |= (1 << FLASH_CS_INDEX);
318 P1SEL &= ~(1 << FLASH_CS_INDEX);