1 //-----------------------------------------------------------------------------
2 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
3 // at your option, any later version. See the LICENSE.txt file for the text of
5 //-----------------------------------------------------------------------------
6 // Main code for the bootloader
7 //-----------------------------------------------------------------------------
11 struct common_area common_area
__attribute__((section(".commonarea")));
12 unsigned int start_addr
, end_addr
, bootrom_unlocked
;
13 extern char _bootrom_start
, _bootrom_end
, _flash_start
, _flash_end
;
15 static void ConfigClocks(void)
17 // we are using a 16 MHz crystal as the basis for everything
18 // slow clock runs at 32Khz typical regardless of crystal
20 // enable system clock and USB clock
21 AT91C_BASE_PMC
->PMC_SCER
= AT91C_PMC_PCK
| AT91C_PMC_UDP
;
23 // enable the clock to the following peripherals
24 AT91C_BASE_PMC
->PMC_PCER
=
32 // worst case scenario, with MAINCK = 16Mhz xtal, startup delay is 1.4ms
33 // if SLCK slow clock runs at its worst case (max) frequency of 42khz
34 // max startup delay = (1.4ms*42k)/8 = 7.356 so round up to 8
36 // enable main oscillator and set startup delay
37 AT91C_BASE_PMC
->PMC_MOR
=
39 PMC_MAIN_OSC_STARTUP_DELAY(8);
41 // wait for main oscillator to stabilize
42 while ( !(AT91C_BASE_PMC
->PMC_SR
& AT91C_PMC_MOSCS
) )
45 // PLL output clock frequency in range 80 - 160 MHz needs CKGR_PLL = 00
46 // PLL output clock frequency in range 150 - 180 MHz needs CKGR_PLL = 10
47 // PLL output is MAINCK * multiplier / divisor = 16Mhz * 12 / 2 = 96Mhz
48 AT91C_BASE_PMC
->PMC_PLLR
=
50 PMC_PLL_COUNT_BEFORE_LOCK(0x50) |
51 PMC_PLL_FREQUENCY_RANGE(0) |
52 PMC_PLL_MULTIPLIER(12) |
53 PMC_PLL_USB_DIVISOR(1);
55 // wait for PLL to lock
56 while ( !(AT91C_BASE_PMC
->PMC_SR
& AT91C_PMC_LOCK
) )
59 // we want a master clock (MCK) to be PLL clock / 2 = 96Mhz / 2 = 48Mhz
60 // datasheet recommends that this register is programmed in two operations
61 // when changing to PLL, program the prescaler first then the source
62 AT91C_BASE_PMC
->PMC_MCKR
= AT91C_PMC_PRES_CLK_2
;
64 // wait for main clock ready signal
65 while ( !(AT91C_BASE_PMC
->PMC_SR
& AT91C_PMC_MCKRDY
) )
68 // set the source to PLL
69 AT91C_BASE_PMC
->PMC_MCKR
= AT91C_PMC_PRES_CLK_2
| AT91C_PMC_CSS_PLL_CLK
;
71 // wait for main clock ready signal
72 while ( !(AT91C_BASE_PMC
->PMC_SR
& AT91C_PMC_MCKRDY
) )
76 static void Fatal(void)
81 void UsbPacketReceived(uint8_t *packet
, int len
)
84 UsbCommand
*c
= (UsbCommand
*)packet
;
87 if(len
!= sizeof(*c
)) {
94 c
->cmd
= CMD_DEVICE_INFO
;
95 c
->arg
[0] = DEVICE_INFO_FLAG_BOOTROM_PRESENT
| DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
|
96 DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH
;
97 if(common_area
.flags
.osimage_present
) c
->arg
[0] |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT
;
98 UsbSendPacket(packet
, len
);
101 case CMD_SETUP_WRITE
:
102 /* The temporary write buffer of the embedded flash controller is mapped to the
103 * whole memory region, only the last 8 bits are decoded.
105 p
= (volatile uint32_t *)&_flash_start
;
106 for(i
= 0; i
< 12; i
++) {
107 p
[i
+c
->arg
[0]] = c
->d
.asDwords
[i
];
111 case CMD_FINISH_WRITE
:
112 p
= (volatile uint32_t *)&_flash_start
;
113 for(i
= 0; i
< 4; i
++) {
114 p
[i
+60] = c
->d
.asDwords
[i
];
117 /* Check that the address that we are supposed to write to is within our allowed region */
118 if( ((c
->arg
[0]+AT91C_IFLASH_PAGE_SIZE
-1) >= end_addr
) || (c
->arg
[0] < start_addr
) ) {
122 UsbSendPacket(packet
, len
);
124 /* Translate address to flash page and do flash, update here for the 512k part */
125 AT91C_BASE_EFC0
->EFC_FCR
= MC_FLASH_COMMAND_KEY
|
126 MC_FLASH_COMMAND_PAGEN((c
->arg
[0]-(int)&_flash_start
)/AT91C_IFLASH_PAGE_SIZE
) |
127 AT91C_MC_FCMD_START_PROG
;
132 while(!((sr
= AT91C_BASE_EFC0
->EFC_FSR
) & AT91C_MC_FRDY
))
134 if(sr
& (AT91C_MC_LOCKE
| AT91C_MC_PROGE
)) {
137 UsbSendPacket(packet
, len
);
141 case CMD_HARDWARE_RESET
:
142 USB_D_PLUS_PULLUP_OFF();
143 AT91C_BASE_RSTC
->RSTC_RCR
= RST_CONTROL_KEY
| AT91C_RSTC_PROCRST
;
146 case CMD_START_FLASH
:
147 if(c
->arg
[2] == START_FLASH_MAGIC
) bootrom_unlocked
= 1;
148 else bootrom_unlocked
= 0;
150 int prot_start
= (int)&_bootrom_start
;
151 int prot_end
= (int)&_bootrom_end
;
152 int allow_start
= (int)&_flash_start
;
153 int allow_end
= (int)&_flash_end
;
154 int cmd_start
= c
->arg
[0];
155 int cmd_end
= c
->arg
[1];
157 /* Only allow command if the bootrom is unlocked, or the parameters are outside of the protected
158 * bootrom area. In any case they must be within the flash area.
160 if( (bootrom_unlocked
|| ((cmd_start
>= prot_end
) || (cmd_end
< prot_start
)))
161 && (cmd_start
>= allow_start
) && (cmd_end
<= allow_end
) ) {
162 start_addr
= cmd_start
;
165 start_addr
= end_addr
= 0;
168 UsbSendPacket(packet
, len
);
180 UsbSendPacket(packet
, len
);
184 static void flash_mode(int externally_entered
)
188 bootrom_unlocked
= 0;
196 if(!externally_entered
&& !BUTTON_PRESS()) {
197 /* Perform a reset to leave flash mode */
198 USB_D_PLUS_PULLUP_OFF();
200 AT91C_BASE_RSTC
->RSTC_RCR
= RST_CONTROL_KEY
| AT91C_RSTC_PROCRST
;
203 if(externally_entered
&& BUTTON_PRESS()) {
204 /* Let the user's button press override the automatic leave */
205 externally_entered
= 0;
210 extern char _osimage_entry
;
214 // First set up all the I/O pins; GPIOs configured directly, other ones
215 // just need to be assigned to the appropriate peripheral.
217 // Kill all the pullups, especially the one on USB D+; leave them for
218 // the unused pins, though.
219 AT91C_BASE_PIOA
->PIO_PPUDR
=
237 // (and add GPIO_FPGA_ON)
238 // These pins are outputs
239 AT91C_BASE_PIOA
->PIO_OER
=
246 // PIO controls the following pins
247 AT91C_BASE_PIOA
->PIO_PER
=
254 USB_D_PLUS_PULLUP_OFF();
260 AT91C_BASE_EFC0
->EFC_FMR
=
262 MC_FLASH_MODE_MASTER_CLK_IN_MHZ(48);
264 // Initialize all system clocks
269 int common_area_present
= 0;
270 switch(AT91C_BASE_RSTC
->RSTC_RSR
& AT91C_RSTC_RSTTYP
) {
271 case AT91C_RSTC_RSTTYP_WATCHDOG
:
272 case AT91C_RSTC_RSTTYP_SOFTWARE
:
273 case AT91C_RSTC_RSTTYP_USER
:
274 /* In these cases the common_area in RAM should be ok, retain it if it's there */
275 if(common_area
.magic
== COMMON_AREA_MAGIC
&& common_area
.version
== 1) {
276 common_area_present
= 1;
279 default: /* Otherwise, initialize it from scratch */
283 if(!common_area_present
){
284 /* Common area not ok, initialize it */
285 int i
; for(i
=0; i
<sizeof(common_area
); i
++) { /* Makeshift memset, no need to drag util.c into this */
286 ((char*)&common_area
)[i
] = 0;
288 common_area
.magic
= COMMON_AREA_MAGIC
;
289 common_area
.version
= 1;
290 common_area
.flags
.bootrom_present
= 1;
293 common_area
.flags
.bootrom_present
= 1;
294 if(common_area
.command
== COMMON_AREA_COMMAND_ENTER_FLASH_MODE
) {
295 common_area
.command
= COMMON_AREA_COMMAND_NONE
;
297 } else if(BUTTON_PRESS()) {
299 } else if(*(uint32_t*)&_osimage_entry
== 0xffffffffU
) {
302 // jump to Flash address of the osimage entry point (LSBit set for thumb mode)
303 asm("bx %0\n" : : "r" ( ((int)&_osimage_entry
) | 0x1 ) );