3 struct common_area common_area
__attribute__((section(".commonarea")));
4 unsigned int start_addr
, end_addr
, bootrom_unlocked
;
5 extern char _bootrom_start
, _bootrom_end
, _flash_start
, _flash_end
;
7 static void ConfigClocks(void)
9 // we are using a 16 MHz crystal as the basis for everything
10 // slow clock runs at 32Khz typical regardless of crystal
12 // enable system clock and USB clock
13 AT91C_BASE_PMC
->PMC_SCER
= AT91C_PMC_PCK
| AT91C_PMC_UDP
;
15 // enable the clock to the following peripherals
16 AT91C_BASE_PMC
->PMC_PCER
=
24 // worst case scenario, with 16Mhz xtal startup delay is 14.5ms
25 // with a slow clock running at it worst case (max) frequency of 42khz
26 // max startup delay = (14.5ms*42k)/8 = 76 = 0x4C round up to 0x50
28 // enable main oscillator and set startup delay
29 AT91C_BASE_PMC
->PMC_MOR
=
31 PMC_MAIN_OSC_STARTUP_DELAY(0x50);
33 // wait for main oscillator to stabilize
34 while ( !(AT91C_BASE_PMC
->PMC_SR
& PMC_MAIN_OSC_STABILIZED
) )
37 // minimum PLL clock frequency is 80 MHz in range 00 (96 here so okay)
38 // frequency is crystal * multiplier / divisor = 16Mhz * 12 / 2 = 96Mhz
39 AT91C_BASE_PMC
->PMC_PLLR
=
41 PMC_PLL_COUNT_BEFORE_LOCK(0x50) |
42 PMC_PLL_FREQUENCY_RANGE(0) |
43 PMC_PLL_MULTIPLIER(12) |
44 PMC_PLL_USB_DIVISOR(1);
46 // wait for PLL to lock
47 while ( !(AT91C_BASE_PMC
->PMC_SR
& PMC_MAIN_OSC_PLL_LOCK
) )
50 // we want a master clock (MCK) to be PLL clock / 2 = 96Mhz / 2 = 48Mhz
51 // as per datasheet, this register must be programmed in two operations
52 // when changing to PLL, program the prescaler first then the source
53 AT91C_BASE_PMC
->PMC_MCKR
= PMC_CLK_PRESCALE_DIV_2
;
55 // wait for main clock ready signal
56 while ( !(AT91C_BASE_PMC
->PMC_SR
& PMC_MAIN_OSC_MCK_READY
) )
59 // set the source to PLL
60 AT91C_BASE_PMC
->PMC_MCKR
= AT91C_PMC_CSS_PLL_CLK
| PMC_CLK_PRESCALE_DIV_2
;
62 // wait for main clock ready signal
63 while ( !(AT91C_BASE_PMC
->PMC_SR
& PMC_MAIN_OSC_MCK_READY
) )
67 static void Fatal(void)
72 void UsbPacketReceived(BYTE
*packet
, int len
)
75 UsbCommand
*c
= (UsbCommand
*)packet
;
78 if(len
!= sizeof(*c
)) {
85 c
->cmd
= CMD_DEVICE_INFO
;
86 c
->arg
[0] = DEVICE_INFO_FLAG_BOOTROM_PRESENT
| DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
|
87 DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH
;
88 if(common_area
.flags
.osimage_present
) c
->arg
[0] |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT
;
89 UsbSendPacket(packet
, len
);
93 /* The temporary write buffer of the embedded flash controller is mapped to the
94 * whole memory region, only the last 8 bits are decoded.
96 p
= (volatile DWORD
*)&_flash_start
;
97 for(i
= 0; i
< 12; i
++) {
98 p
[i
+c
->arg
[0]] = c
->d
.asDwords
[i
];
102 case CMD_FINISH_WRITE
:
103 p
= (volatile DWORD
*)&_flash_start
;
104 for(i
= 0; i
< 4; i
++) {
105 p
[i
+60] = c
->d
.asDwords
[i
];
108 /* Check that the address that we are supposed to write to is within our allowed region */
109 if( ((c
->arg
[0]+AT91C_IFLASH_PAGE_SIZE
-1) >= end_addr
) || (c
->arg
[0] < start_addr
) ) {
113 UsbSendPacket(packet
, len
);
115 /* Translate address to flash page and do flash, update here for the 512k part */
116 AT91C_BASE_EFC0
->EFC_FCR
= MC_FLASH_COMMAND_KEY
|
117 MC_FLASH_COMMAND_PAGEN((c
->arg
[0]-(int)&_flash_start
)/AT91C_IFLASH_PAGE_SIZE
) |
118 AT91C_MC_FCMD_START_PROG
;
120 while(!(AT91C_BASE_EFC0
->EFC_FSR
& MC_FLASH_STATUS_READY
))
124 case CMD_HARDWARE_RESET
:
125 USB_D_PLUS_PULLUP_OFF();
126 AT91C_BASE_RSTC
->RSTC_RCR
= RST_CONTROL_KEY
| AT91C_RSTC_PROCRST
;
129 case CMD_START_FLASH
:
130 if(c
->arg
[2] == START_FLASH_MAGIC
) bootrom_unlocked
= 1;
131 else bootrom_unlocked
= 0;
133 int prot_start
= (int)&_bootrom_start
;
134 int prot_end
= (int)&_bootrom_end
;
135 int allow_start
= (int)&_flash_start
;
136 int allow_end
= (int)&_flash_end
;
137 int cmd_start
= c
->arg
[0];
138 int cmd_end
= c
->arg
[1];
140 /* Only allow command if the bootrom is unlocked, or the parameters are outside of the protected
141 * bootrom area. In any case they must be within the flash area.
143 if( (bootrom_unlocked
|| ((cmd_start
>= prot_end
) || (cmd_end
< prot_start
)))
144 && (cmd_start
>= allow_start
) && (cmd_end
<= allow_end
) ) {
145 start_addr
= cmd_start
;
148 start_addr
= end_addr
= 0;
151 UsbSendPacket(packet
, len
);
163 UsbSendPacket(packet
, len
);
167 static void flash_mode(int externally_entered
)
171 bootrom_unlocked
= 0;
179 if(!externally_entered
&& !BUTTON_PRESS()) {
180 /* Perform a reset to leave flash mode */
181 USB_D_PLUS_PULLUP_OFF();
183 AT91C_BASE_RSTC
->RSTC_RCR
= RST_CONTROL_KEY
| AT91C_RSTC_PROCRST
;
186 if(externally_entered
&& BUTTON_PRESS()) {
187 /* Let the user's button press override the automatic leave */
188 externally_entered
= 0;
193 extern char _osimage_entry
;
197 // First set up all the I/O pins; GPIOs configured directly, other ones
198 // just need to be assigned to the appropriate peripheral.
200 // Kill all the pullups, especially the one on USB D+; leave them for
201 // the unused pins, though.
202 AT91C_BASE_PIOA
->PIO_PPUDR
=
220 // (and add GPIO_FPGA_ON)
221 // These pins are outputs
222 AT91C_BASE_PIOA
->PIO_OER
=
229 // PIO controls the following pins
230 AT91C_BASE_PIOA
->PIO_PER
=
237 USB_D_PLUS_PULLUP_OFF();
243 // if 512K FLASH part - TODO make some defines :)
244 if ((AT91C_BASE_DBGU
->DBGU_CIDR
| 0xf00) == 0xa00) {
245 AT91C_BASE_EFC0
->EFC_FMR
=
246 MC_FLASH_MODE_FLASH_WAIT_STATES(1) |
247 MC_FLASH_MODE_MASTER_CLK_IN_MHZ(0x48);
248 AT91C_BASE_EFC1
->EFC_FMR
=
249 MC_FLASH_MODE_FLASH_WAIT_STATES(1) |
250 MC_FLASH_MODE_MASTER_CLK_IN_MHZ(0x48);
252 AT91C_BASE_EFC0
->EFC_FMR
=
253 MC_FLASH_MODE_FLASH_WAIT_STATES(0) |
254 MC_FLASH_MODE_MASTER_CLK_IN_MHZ(48);
257 // Initialize all system clocks
262 int common_area_present
= 0;
263 switch(AT91C_BASE_RSTC
->RSTC_RSR
& AT91C_RSTC_RSTTYP
) {
264 case AT91C_RSTC_RSTTYP_WATCHDOG
:
265 case AT91C_RSTC_RSTTYP_SOFTWARE
:
266 case AT91C_RSTC_RSTTYP_USER
:
267 /* In these cases the common_area in RAM should be ok, retain it if it's there */
268 if(common_area
.magic
== COMMON_AREA_MAGIC
&& common_area
.version
== 1) {
269 common_area_present
= 1;
272 default: /* Otherwise, initialize it from scratch */
276 if(!common_area_present
){
277 /* Common area not ok, initialize it */
278 int i
; for(i
=0; i
<sizeof(common_area
); i
++) { /* Makeshift memset, no need to drag util.c into this */
279 ((char*)&common_area
)[i
] = 0;
281 common_area
.magic
= COMMON_AREA_MAGIC
;
282 common_area
.version
= 1;
283 common_area
.flags
.bootrom_present
= 1;
286 common_area
.flags
.bootrom_present
= 1;
287 if(common_area
.command
== COMMON_AREA_COMMAND_ENTER_FLASH_MODE
) {
288 common_area
.command
= COMMON_AREA_COMMAND_NONE
;
290 } else if(BUTTON_PRESS()) {
293 // jump to Flash address of the osimage entry point (LSBit set for thumb mode)
294 asm("bx %0\n" : : "r" ( ((int)&_osimage_entry
) | 0x1 ) );