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 "ao_at45db161d.h"
21 /* Total bytes of available storage */
22 __pdata uint32_t ao_storage_total;
24 /* Block size - device is erased in these units. At least 256 bytes */
25 __pdata uint32_t ao_storage_block;
27 /* Byte offset of config block. Will be ao_storage_block bytes long */
28 __pdata uint32_t ao_storage_config;
30 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
31 __pdata 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 #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 #define ao_flash_cs_high() ao_spi_put_bit(FLASH_CS_PORT, FLASH_CS_PIN, FLASH_CS, AO_FLASH_SPI_BUS)
50 struct ao_flash_instruction {
53 } __xdata ao_flash_instruction;
56 ao_flash_set_pagesize_512(void)
59 ao_flash_instruction.instruction = FLASH_SET_CONFIG;
60 ao_flash_instruction.address[0] = FLASH_SET_512_BYTE_0;
61 ao_flash_instruction.address[1] = FLASH_SET_512_BYTE_1;
62 ao_flash_instruction.address[2] = FLASH_SET_512_BYTE_2;
63 ao_spi_send(&ao_flash_instruction, 4, AO_FLASH_SPI_BUS);
69 ao_flash_read_status(void)
72 ao_flash_instruction.instruction = FLASH_READ_STATUS;
73 ao_spi_send(&ao_flash_instruction, 1, AO_FLASH_SPI_BUS);
74 ao_spi_recv(&ao_flash_instruction, 1, AO_FLASH_SPI_BUS);
76 return ao_flash_instruction.instruction;
79 #define FLASH_BLOCK_NONE 0xffff
81 static __xdata uint8_t ao_flash_data[FLASH_BLOCK_SIZE_MAX];
82 static __pdata uint16_t ao_flash_block = FLASH_BLOCK_NONE;
83 static __pdata uint8_t ao_flash_block_dirty;
84 static __pdata uint8_t ao_flash_write_pending;
85 static __pdata uint8_t ao_flash_setup_done;
86 static __pdata uint8_t ao_flash_block_shift;
87 static __pdata uint16_t ao_flash_block_size;
88 static __pdata uint16_t ao_flash_block_mask;
91 ao_storage_setup(void) __reentrant
95 if (ao_flash_setup_done)
98 ao_mutex_get(&ao_flash_mutex);
99 if (ao_flash_setup_done) {
100 ao_mutex_put(&ao_flash_mutex);
104 /* On first use, check to see if the flash chip has
105 * been programmed to use 512 byte pages. If not, do so.
106 * And then, because the flash part must be power cycled
107 * for that change to take effect, panic.
109 status = ao_flash_read_status();
111 if (!(status & FLASH_STATUS_PAGESIZE_512)) {
112 ao_flash_set_pagesize_512();
113 ao_panic(AO_PANIC_FLASH);
116 switch (status & 0x3c) {
120 ao_flash_block_shift = 9;
121 ao_storage_total = ((uint32_t) 4 * (uint32_t) 1024 * (uint32_t) 1024);
126 ao_flash_block_shift = 9;
127 ao_storage_total = ((uint32_t) 2 * (uint32_t) 1024 * (uint32_t) 1024);
132 ao_flash_block_shift = 8;
133 ao_storage_total = ((uint32_t) 1024 * (uint32_t) 1024);
138 ao_flash_block_shift = 8;
139 ao_storage_total = ((uint32_t) 512 * (uint32_t) 1024);
144 ao_flash_block_shift = 8;
145 ao_storage_total = ((uint32_t) 256 * (uint32_t) 1024);
150 ao_flash_block_shift = 8;
151 ao_storage_total = ((uint32_t) 128 * (uint32_t) 1024);
155 ao_panic(AO_PANIC_FLASH);
157 ao_flash_block_size = 1 << ao_flash_block_shift;
158 ao_flash_block_mask = ao_flash_block_size - 1;
160 ao_storage_block = ao_flash_block_size;
161 ao_storage_config = ao_storage_total - ao_storage_block;
162 ao_storage_unit = ao_flash_block_size;
164 ao_flash_setup_done = 1;
165 ao_mutex_put(&ao_flash_mutex);
169 ao_flash_wait_write(void)
171 if (ao_flash_write_pending) {
173 uint8_t status = ao_flash_read_status();
174 if ((status & FLASH_STATUS_RDY))
177 ao_flash_write_pending = 0;
181 /* Write the current block to the FLASHPROM */
183 ao_flash_write_block(void)
185 ao_flash_wait_write();
187 ao_flash_instruction.instruction = FLASH_WRITE;
189 /* 13/14 block bits + 9/8 byte bits (always 0) */
190 ao_flash_instruction.address[0] = ao_flash_block >> (16 - ao_flash_block_shift);
191 ao_flash_instruction.address[1] = ao_flash_block << (ao_flash_block_shift - 8);
192 ao_flash_instruction.address[2] = 0;
193 ao_spi_send(&ao_flash_instruction, 4, AO_FLASH_SPI_BUS);
194 ao_spi_send(ao_flash_data, ao_storage_block, AO_FLASH_SPI_BUS);
196 ao_flash_write_pending = 1;
199 /* Read the current block from the FLASHPROM */
201 ao_flash_read_block(void)
203 ao_flash_wait_write();
205 ao_flash_instruction.instruction = FLASH_READ;
207 /* 13/14 block bits + 9/8 byte bits (always 0) */
208 ao_flash_instruction.address[0] = ao_flash_block >> (16 - ao_flash_block_shift);
209 ao_flash_instruction.address[1] = ao_flash_block << (ao_flash_block_shift - 8);
210 ao_flash_instruction.address[2] = 0;
211 ao_spi_send(&ao_flash_instruction, 4, AO_FLASH_SPI_BUS);
212 ao_spi_recv(ao_flash_data, ao_flash_block_size, AO_FLASH_SPI_BUS);
217 ao_flash_flush_internal(void)
219 if (ao_flash_block_dirty) {
220 ao_flash_write_block();
221 ao_flash_block_dirty = 0;
226 ao_flash_fill(uint16_t block)
228 if (block != ao_flash_block) {
229 ao_flash_flush_internal();
230 ao_flash_block = block;
231 ao_flash_read_block();
236 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
238 uint16_t block = (uint16_t) (pos >> ao_flash_block_shift);
240 /* Transfer the data */
241 ao_mutex_get(&ao_flash_mutex); {
242 if (len != ao_flash_block_size)
243 ao_flash_fill(block);
245 ao_flash_flush_internal();
246 ao_flash_block = block;
248 ao_xmemcpy(ao_flash_data + (uint16_t) (pos & ao_flash_block_mask),
251 ao_flash_block_dirty = 1;
252 } ao_mutex_put(&ao_flash_mutex);
257 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
259 uint16_t block = (uint16_t) (pos >> ao_flash_block_shift);
261 /* Transfer the data */
262 ao_mutex_get(&ao_flash_mutex); {
263 ao_flash_fill(block);
265 ao_flash_data + (uint16_t) (pos & ao_flash_block_mask),
267 } ao_mutex_put(&ao_flash_mutex);
272 ao_storage_flush(void) __reentrant
274 ao_mutex_get(&ao_flash_mutex); {
275 ao_flash_flush_internal();
276 } ao_mutex_put(&ao_flash_mutex);
280 ao_storage_erase(uint32_t pos) __reentrant
282 ao_mutex_get(&ao_flash_mutex); {
283 ao_flash_flush_internal();
284 ao_flash_block = (uint16_t) (pos >> ao_flash_block_shift);
285 ao_xmemset(ao_flash_data, 0xff, ao_flash_block_size);
286 ao_flash_block_dirty = 1;
287 } ao_mutex_put(&ao_flash_mutex);
292 ao_storage_device_info(void) __reentrant
297 ao_mutex_get(&ao_flash_mutex); {
298 status = ao_flash_read_status();
299 printf ("Flash status: 0x%02x\n", status);
300 printf ("Flash block shift: %d\n", ao_flash_block_shift);
301 printf ("Flash block size: %d\n", ao_flash_block_size);
302 printf ("Flash block mask: %d\n", ao_flash_block_mask);
303 printf ("Flash device size: %ld\n", ao_storage_total);
304 } ao_mutex_put(&ao_flash_mutex);
308 * To initialize the chip, set up the CS line and
312 ao_storage_device_init(void)
316 P1DIR |= (1 << FLASH_CS_INDEX);
317 P1SEL &= ~(1 << FLASH_CS_INDEX);