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 //-----------------------------------------------------------------------------
12 struct common_area common_area
__attribute__((section(".commonarea")));
13 unsigned int start_addr
, end_addr
, bootrom_unlocked
;
14 extern char _bootrom_start
, _bootrom_end
, _flash_start
, _flash_end
;
16 static void ConfigClocks(void)
18 // we are using a 16 MHz crystal as the basis for everything
19 // slow clock runs at 32Khz typical regardless of crystal
21 // enable system clock and USB clock
22 AT91C_BASE_PMC
->PMC_SCER
= AT91C_PMC_PCK
| AT91C_PMC_UDP
;
24 // enable the clock to the following peripherals
25 AT91C_BASE_PMC
->PMC_PCER
=
33 // worst case scenario, with MAINCK = 16Mhz xtal, startup delay is 1.4ms
34 // if SLCK slow clock runs at its worst case (max) frequency of 42khz
35 // max startup delay = (1.4ms*42k)/8 = 7.356 so round up to 8
37 // enable main oscillator and set startup delay
38 AT91C_BASE_PMC
->PMC_MOR
=
40 PMC_MAIN_OSC_STARTUP_DELAY(8);
42 // wait for main oscillator to stabilize
43 while ( !(AT91C_BASE_PMC
->PMC_SR
& AT91C_PMC_MOSCS
) )
46 // PLL output clock frequency in range 80 - 160 MHz needs CKGR_PLL = 00
47 // PLL output clock frequency in range 150 - 180 MHz needs CKGR_PLL = 10
48 // PLL output is MAINCK * multiplier / divisor = 16Mhz * 12 / 2 = 96Mhz
49 AT91C_BASE_PMC
->PMC_PLLR
=
51 PMC_PLL_COUNT_BEFORE_LOCK(0x50) |
52 PMC_PLL_FREQUENCY_RANGE(0) |
53 PMC_PLL_MULTIPLIER(12) |
54 PMC_PLL_USB_DIVISOR(1);
56 // wait for PLL to lock
57 while ( !(AT91C_BASE_PMC
->PMC_SR
& AT91C_PMC_LOCK
) )
60 // we want a master clock (MCK) to be PLL clock / 2 = 96Mhz / 2 = 48Mhz
61 // datasheet recommends that this register is programmed in two operations
62 // when changing to PLL, program the prescaler first then the source
63 AT91C_BASE_PMC
->PMC_MCKR
= AT91C_PMC_PRES_CLK_2
;
65 // wait for main clock ready signal
66 while ( !(AT91C_BASE_PMC
->PMC_SR
& AT91C_PMC_MCKRDY
) )
69 // set the source to PLL
70 AT91C_BASE_PMC
->PMC_MCKR
= AT91C_PMC_PRES_CLK_2
| AT91C_PMC_CSS_PLL_CLK
;
72 // wait for main clock ready signal
73 while ( !(AT91C_BASE_PMC
->PMC_SR
& AT91C_PMC_MCKRDY
) )
77 static void Fatal(void)
82 void UsbPacketReceived(uint8_t *packet
, int len
)
85 UsbCommand
*c
= (UsbCommand
*)packet
;
88 if(len
!= sizeof(*c
)) {
95 c
->cmd
= CMD_DEVICE_INFO
;
96 c
->arg
[0] = DEVICE_INFO_FLAG_BOOTROM_PRESENT
| DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
|
97 DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH
;
98 if(common_area
.flags
.osimage_present
) c
->arg
[0] |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT
;
99 UsbSendPacket(packet
, len
);
102 case CMD_SETUP_WRITE
:
103 /* The temporary write buffer of the embedded flash controller is mapped to the
104 * whole memory region, only the last 8 bits are decoded.
106 p
= (volatile uint32_t *)&_flash_start
;
107 for(i
= 0; i
< 12; i
++) {
108 p
[i
+c
->arg
[0]] = c
->d
.asDwords
[i
];
112 case CMD_FINISH_WRITE
:
113 p
= (volatile uint32_t *)&_flash_start
;
114 for(i
= 0; i
< 4; i
++) {
115 p
[i
+60] = c
->d
.asDwords
[i
];
118 /* Check that the address that we are supposed to write to is within our allowed region */
119 if( ((c
->arg
[0]+AT91C_IFLASH_PAGE_SIZE
-1) >= end_addr
) || (c
->arg
[0] < start_addr
) ) {
123 UsbSendPacket(packet
, len
);
125 /* Translate address to flash page and do flash, update here for the 512k part */
126 AT91C_BASE_EFC0
->EFC_FCR
= MC_FLASH_COMMAND_KEY
|
127 MC_FLASH_COMMAND_PAGEN((c
->arg
[0]-(int)&_flash_start
)/AT91C_IFLASH_PAGE_SIZE
) |
128 AT91C_MC_FCMD_START_PROG
;
133 while(!((sr
= AT91C_BASE_EFC0
->EFC_FSR
) & AT91C_MC_FRDY
))
135 if(sr
& (AT91C_MC_LOCKE
| AT91C_MC_PROGE
)) {
138 UsbSendPacket(packet
, len
);
142 case CMD_HARDWARE_RESET
:
143 USB_D_PLUS_PULLUP_OFF();
144 AT91C_BASE_RSTC
->RSTC_RCR
= RST_CONTROL_KEY
| AT91C_RSTC_PROCRST
;
147 case CMD_START_FLASH
:
148 if(c
->arg
[2] == START_FLASH_MAGIC
) bootrom_unlocked
= 1;
149 else bootrom_unlocked
= 0;
151 int prot_start
= (int)&_bootrom_start
;
152 int prot_end
= (int)&_bootrom_end
;
153 int allow_start
= (int)&_flash_start
;
154 int allow_end
= (int)&_flash_end
;
155 int cmd_start
= c
->arg
[0];
156 int cmd_end
= c
->arg
[1];
158 /* Only allow command if the bootrom is unlocked, or the parameters are outside of the protected
159 * bootrom area. In any case they must be within the flash area.
161 if( (bootrom_unlocked
|| ((cmd_start
>= prot_end
) || (cmd_end
< prot_start
)))
162 && (cmd_start
>= allow_start
) && (cmd_end
<= allow_end
) ) {
163 start_addr
= cmd_start
;
166 start_addr
= end_addr
= 0;
169 UsbSendPacket(packet
, len
);
181 UsbSendPacket(packet
, len
);
185 static void flash_mode(int externally_entered
)
189 bootrom_unlocked
= 0;
197 if(!externally_entered
&& !BUTTON_PRESS()) {
198 /* Perform a reset to leave flash mode */
199 USB_D_PLUS_PULLUP_OFF();
201 AT91C_BASE_RSTC
->RSTC_RCR
= RST_CONTROL_KEY
| AT91C_RSTC_PROCRST
;
204 if(externally_entered
&& BUTTON_PRESS()) {
205 /* Let the user's button press override the automatic leave */
206 externally_entered
= 0;
211 extern char _osimage_entry
;
215 // First set up all the I/O pins; GPIOs configured directly, other ones
216 // just need to be assigned to the appropriate peripheral.
218 // Kill all the pullups, especially the one on USB D+; leave them for
219 // the unused pins, though.
220 AT91C_BASE_PIOA
->PIO_PPUDR
=
238 // (and add GPIO_FPGA_ON)
239 // These pins are outputs
240 AT91C_BASE_PIOA
->PIO_OER
=
247 // PIO controls the following pins
248 AT91C_BASE_PIOA
->PIO_PER
=
255 USB_D_PLUS_PULLUP_OFF();
261 AT91C_BASE_EFC0
->EFC_FMR
=
263 MC_FLASH_MODE_MASTER_CLK_IN_MHZ(48);
265 // Initialize all system clocks
270 int common_area_present
= 0;
271 switch(AT91C_BASE_RSTC
->RSTC_RSR
& AT91C_RSTC_RSTTYP
) {
272 case AT91C_RSTC_RSTTYP_WATCHDOG
:
273 case AT91C_RSTC_RSTTYP_SOFTWARE
:
274 case AT91C_RSTC_RSTTYP_USER
:
275 /* In these cases the common_area in RAM should be ok, retain it if it's there */
276 if(common_area
.magic
== COMMON_AREA_MAGIC
&& common_area
.version
== 1) {
277 common_area_present
= 1;
280 default: /* Otherwise, initialize it from scratch */
284 if(!common_area_present
){
285 /* Common area not ok, initialize it */
286 int i
; for(i
=0; i
<sizeof(common_area
); i
++) { /* Makeshift memset, no need to drag util.c into this */
287 ((char*)&common_area
)[i
] = 0;
289 common_area
.magic
= COMMON_AREA_MAGIC
;
290 common_area
.version
= 1;
291 common_area
.flags
.bootrom_present
= 1;
294 common_area
.flags
.bootrom_present
= 1;
295 if(common_area
.command
== COMMON_AREA_COMMAND_ENTER_FLASH_MODE
) {
296 common_area
.command
= COMMON_AREA_COMMAND_NONE
;
298 } else if(BUTTON_PRESS()) {
300 } else if(*(uint32_t*)&_osimage_entry
== 0xffffffffU
) {
303 // jump to Flash address of the osimage entry point (LSBit set for thumb mode)
304 __asm("bx %0\n" : : "r" ( ((int)&_osimage_entry
) | 0x1 ) );