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"
22 * Using SPI on USART 0, with P1_1 as the chip select
26 #define FLASH_CS_INDEX 1
28 __xdata uint8_t ao_flash_dma_in_done;
29 __xdata uint8_t ao_flash_dma_out_done;
30 __xdata uint8_t ao_flash_mutex;
32 uint8_t ao_flash_dma_out_id;
33 uint8_t ao_flash_dma_in_id;
35 static __xdata uint8_t ao_flash_const = 0xff;
37 #define ao_flash_delay() do { \
43 void ao_flash_cs_low(void)
50 void ao_flash_cs_high(void)
57 /* Send bytes over SPI.
59 * This sets up two DMA engines, one writing the data and another reading
60 * bytes coming back. We use the bytes coming back to tell when the transfer
61 * is complete, as the transmit register is double buffered and hence signals
62 * completion one byte before the transfer is actually complete
65 ao_flash_send(void __xdata *block, uint16_t len)
67 ao_dma_set_transfer(ao_flash_dma_in_id,
72 DMA_CFG0_TMODE_SINGLE |
73 DMA_CFG0_TRIGGER_URX0,
76 DMA_CFG1_PRIORITY_NORMAL);
78 ao_dma_set_transfer(ao_flash_dma_out_id,
83 DMA_CFG0_TMODE_SINGLE |
84 DMA_CFG0_TRIGGER_UTX0,
87 DMA_CFG1_PRIORITY_NORMAL);
89 ao_dma_start(ao_flash_dma_in_id);
90 ao_dma_start(ao_flash_dma_out_id);
91 ao_dma_trigger(ao_flash_dma_out_id);
92 __critical while (!ao_flash_dma_in_done)
93 ao_sleep(&ao_flash_dma_in_done);
96 /* Receive bytes over SPI.
98 * This sets up tow DMA engines, one reading the data and another
99 * writing constant values to the SPI transmitter as that is what
100 * clocks the data coming in.
103 ao_flash_recv(void __xdata *block, uint16_t len)
105 ao_dma_set_transfer(ao_flash_dma_in_id,
109 DMA_CFG0_WORDSIZE_8 |
110 DMA_CFG0_TMODE_SINGLE |
111 DMA_CFG0_TRIGGER_URX0,
114 DMA_CFG1_PRIORITY_NORMAL);
116 ao_dma_set_transfer(ao_flash_dma_out_id,
120 DMA_CFG0_WORDSIZE_8 |
121 DMA_CFG0_TMODE_SINGLE |
122 DMA_CFG0_TRIGGER_UTX0,
125 DMA_CFG1_PRIORITY_NORMAL);
127 ao_dma_start(ao_flash_dma_in_id);
128 ao_dma_start(ao_flash_dma_out_id);
129 ao_dma_trigger(ao_flash_dma_out_id);
130 __critical while (!ao_flash_dma_in_done)
131 ao_sleep(&ao_flash_dma_in_done);
134 struct ao_flash_instruction {
137 } __xdata ao_flash_instruction;
140 ao_flash_set_pagesize_512(void)
143 ao_flash_instruction.instruction = FLASH_SET_CONFIG;
144 ao_flash_instruction.address[0] = FLASH_SET_512_BYTE_0;
145 ao_flash_instruction.address[1] = FLASH_SET_512_BYTE_1;
146 ao_flash_instruction.address[2] = FLASH_SET_512_BYTE_2;
147 ao_flash_send(&ao_flash_instruction, 4);
153 ao_flash_read_status(void)
156 ao_flash_instruction.instruction = FLASH_READ_STATUS;
157 ao_flash_send(&ao_flash_instruction, 1);
158 ao_flash_recv(&ao_flash_instruction, 1);
160 return ao_flash_instruction.instruction;
163 #define FLASH_BLOCK_NONE 0xffff
165 static __xdata uint8_t ao_flash_data[FLASH_BLOCK_SIZE_MAX];
166 static __pdata uint16_t ao_flash_block = FLASH_BLOCK_NONE;
167 static __pdata uint8_t ao_flash_block_dirty;
168 static __pdata uint8_t ao_flash_write_pending;
169 static __pdata uint8_t ao_flash_setup_done;
170 static __data uint32_t ao_flash_device_size;
171 static __data uint8_t ao_flash_block_shift;
172 static __data uint16_t ao_flash_block_size;
179 if (ao_flash_setup_done)
182 ao_mutex_get(&ao_flash_mutex);
183 if (ao_flash_setup_done) {
184 ao_mutex_put(&ao_flash_mutex);
188 /* On first use, check to see if the flash chip has
189 * been programmed to use 512 byte pages. If not, do so.
190 * And then, because the flash part must be power cycled
191 * for that change to take effect, panic.
193 status = ao_flash_read_status();
195 if (!(status & FLASH_STATUS_PAGESIZE_512)) {
196 ao_flash_set_pagesize_512();
197 ao_panic(AO_PANIC_FLASH);
200 switch (status & 0x3c) {
204 ao_flash_block_shift = 9;
205 ao_flash_device_size = ((uint32_t) 4 * (uint32_t) 1024 * (uint32_t) 1024);
210 ao_flash_block_shift = 9;
211 ao_flash_device_size = ((uint32_t) 2 * (uint32_t) 1024 * (uint32_t) 1024);
216 ao_flash_block_shift = 8;
217 ao_flash_device_size = ((uint32_t) 1024 * (uint32_t) 1024);
222 ao_flash_block_shift = 8;
223 ao_flash_device_size = ((uint32_t) 512 * (uint32_t) 1024);
228 ao_flash_block_shift = 8;
229 ao_flash_device_size = ((uint32_t) 256 * (uint32_t) 1024);
234 ao_flash_block_shift = 8;
235 ao_flash_device_size = ((uint32_t) 128 * (uint32_t) 1024);
239 ao_panic(AO_PANIC_FLASH);
241 ao_flash_block_size = 1 << ao_flash_block_shift;
242 ao_flash_setup_done = 1;
243 ao_mutex_put(&ao_flash_mutex);
247 ao_flash_wait_write(void)
249 if (ao_flash_write_pending) {
251 uint8_t status = ao_flash_read_status();
252 if ((status & FLASH_STATUS_RDY))
255 ao_flash_write_pending = 0;
259 /* Write the current block to the FLASHPROM */
261 ao_flash_write_block(void)
263 ao_flash_wait_write();
265 ao_flash_instruction.instruction = FLASH_WRITE;
267 /* 13/14 block bits + 9/8 byte bits (always 0) */
268 ao_flash_instruction.address[0] = ao_flash_block >> (16 - ao_flash_block_shift);
269 ao_flash_instruction.address[1] = ao_flash_block << (ao_flash_block_shift - 8);
270 ao_flash_instruction.address[2] = 0;
271 ao_flash_send(&ao_flash_instruction, 4);
272 ao_flash_send(ao_flash_data, FLASH_BLOCK_SIZE);
274 ao_flash_write_pending = 1;
277 /* Read the current block from the FLASHPROM */
279 ao_flash_read_block(void)
281 ao_flash_wait_write();
283 ao_flash_instruction.instruction = FLASH_READ;
285 /* 13/14 block bits + 9/8 byte bits (always 0) */
286 ao_flash_instruction.address[0] = ao_flash_block >> (16 - ao_flash_block_shift);
287 ao_flash_instruction.address[1] = ao_flash_block << (ao_flash_block_shift - 8);
288 ao_flash_instruction.address[2] = 0;
289 ao_flash_send(&ao_flash_instruction, 4);
290 ao_flash_recv(ao_flash_data, FLASH_BLOCK_SIZE);
295 ao_flash_flush_internal(void)
297 if (ao_flash_block_dirty) {
298 ao_flash_write_block();
299 ao_flash_block_dirty = 0;
304 ao_flash_fill(uint16_t block)
306 if (block != ao_flash_block) {
307 ao_flash_flush_internal();
308 ao_flash_block = block;
309 ao_flash_read_block();
314 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant
321 if (pos >= FLASH_DATA_SIZE || pos + len > FLASH_DATA_SIZE)
325 /* Compute portion of transfer within
328 this_off = (uint16_t) pos & FLASH_BLOCK_MASK;
329 this_len = FLASH_BLOCK_SIZE - this_off;
330 block = (uint16_t) (pos >> FLASH_BLOCK_SHIFT);
334 /* Transfer the data */
335 ao_mutex_get(&ao_flash_mutex); {
336 if (this_len != FLASH_BLOCK_SIZE)
337 ao_flash_fill(block);
339 ao_flash_flush_internal();
340 ao_flash_block = block;
342 memcpy(ao_flash_data + this_off, buf, this_len);
343 ao_flash_block_dirty = 1;
344 } ao_mutex_put(&ao_flash_mutex);
346 /* See how much is left */
355 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant
362 if (pos >= FLASH_DATA_SIZE || pos + len > FLASH_DATA_SIZE)
367 /* Compute portion of transfer within
370 this_off = (uint16_t) pos & FLASH_BLOCK_MASK;
371 this_len = FLASH_BLOCK_SIZE - this_off;
372 block = (uint16_t) (pos >> FLASH_BLOCK_SHIFT);
376 /* Transfer the data */
377 ao_mutex_get(&ao_flash_mutex); {
378 ao_flash_fill(block);
379 memcpy(buf, ao_flash_data + this_off, this_len);
380 } ao_mutex_put(&ao_flash_mutex);
382 /* See how much is left */
391 ao_ee_flush(void) __reentrant
393 ao_mutex_get(&ao_flash_mutex); {
394 ao_flash_flush_internal();
395 } ao_mutex_put(&ao_flash_mutex);
399 * Read/write the config block, which is in
400 * the last block of the flash
404 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant
407 if (len > FLASH_BLOCK_SIZE)
409 ao_mutex_get(&ao_flash_mutex); {
410 ao_flash_fill(FLASH_CONFIG_BLOCK);
411 memcpy(ao_flash_data, buf, len);
412 ao_flash_block_dirty = 1;
413 ao_flash_flush_internal();
414 } ao_mutex_put(&ao_flash_mutex);
419 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant
422 if (len > FLASH_BLOCK_SIZE)
424 ao_mutex_get(&ao_flash_mutex); {
425 ao_flash_fill(FLASH_CONFIG_BLOCK);
426 memcpy(buf, ao_flash_data, len);
427 } ao_mutex_put(&ao_flash_mutex);
432 flash_dump(void) __reentrant
439 block = ao_cmd_lex_i;
440 if (ao_cmd_status != ao_cmd_success)
447 ao_cmd_put16((uint16_t) i);
450 ao_ee_read(((uint32_t) block << 8) | i, &b, 1);
458 flash_store(void) __reentrant
467 block = ao_cmd_lex_i;
470 addr = ((uint32_t) block << 8) | i;
473 if (ao_cmd_status != ao_cmd_success)
477 if (ao_cmd_status != ao_cmd_success)
480 ao_ee_write(addr, &b, 1);
487 ao_ee_dump_config(void) __reentrant
490 printf("Configuration block %d\n", FLASH_CONFIG_BLOCK);
491 ao_mutex_get(&ao_flash_mutex); {
492 ao_flash_flush_internal();
493 ao_flash_block = FLASH_BLOCK_NONE;
494 ao_flash_fill(FLASH_CONFIG_BLOCK);
500 ao_cmd_put16((uint16_t) i);
503 ao_cmd_put8(ao_flash_data[i]);
505 } while (i < sizeof (ao_config));
506 } ao_mutex_put(&ao_flash_mutex);
510 flash_status(void) __reentrant
515 ao_mutex_get(&ao_flash_mutex); {
516 status = ao_flash_read_status();
517 printf ("Flash status: 0x%02x\n", status);
518 printf ("Flash block shift: %d\n", FLASH_BLOCK_SHIFT);
519 printf ("Flash block size: %d\n", FLASH_BLOCK_SIZE);
520 printf ("Flash block mask: %d\n", FLASH_BLOCK_MASK);
521 printf ("Flash device size: %ld\n", FLASH_DEVICE_SIZE);
522 printf ("Flash data size: %ld\n", FLASH_DATA_SIZE);
523 printf ("Flash config block: %d\n", FLASH_CONFIG_BLOCK);
524 } ao_mutex_put(&ao_flash_mutex);
528 __code struct ao_cmds ao_flash_cmds[] = {
529 { 'e', flash_dump, "e <block> Dump a block of flash data" },
530 { 'w', flash_store, "w <block> <start> <len> <data> ... Write data to flash" },
531 { 'f', flash_status, "f Show flash status register" },
532 { 0, flash_store, NULL },
536 * To initialize the chip, set up the CS line and
544 P1DIR |= (1 << FLASH_CS_INDEX);
545 P1SEL &= ~(1 << FLASH_CS_INDEX);
547 /* Set up the USART pin assignment */
548 PERCFG = (PERCFG & ~PERCFG_U0CFG_ALT_MASK) | PERCFG_U0CFG_ALT_2;
550 /* Ensure that USART0 takes precidence over USART1 for pins that
553 P2SEL = (P2SEL & ~P2SEL_PRI3P1_MASK) | P2SEL_PRI3P1_USART0;
555 /* Make the SPI pins be controlled by the USART peripheral */
556 P1SEL |= ((1 << 5) | (1 << 4) | (1 << 3));
559 ao_flash_dma_out_id = ao_dma_alloc(&ao_flash_dma_out_done);
562 ao_flash_dma_in_id = ao_dma_alloc(&ao_flash_dma_in_done);
568 U0CSR = (UxCSR_MODE_SPI | UxCSR_RE | UxCSR_MASTER);
570 /* Set the baud rate and signal parameters
572 * The cc1111 is limited to a 24/8 MHz SPI clock,
573 * while the at45db161d.h is limited to 20MHz. So,
574 * use the 3MHz clock (BAUD_E 17, BAUD_M 0)
577 U0GCR = (UxGCR_CPOL_NEGATIVE |
578 UxGCR_CPHA_FIRST_EDGE |
580 (17 << UxGCR_BAUD_E_SHIFT));
581 ao_cmd_register(&ao_flash_cmds[0]);