1 /*
2  * Copyright (c) 2019 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 
9 #include <target.h>
10 #include <arch/arch_ops.h>
11 #include <platform/sifive.h>
12 #include <dev/gpio.h>
13 
14 static volatile unsigned int *const prci_base = (unsigned int *)PRCI_BASE;
15 
16 #define GPIO_LED_GREEN 19
17 #define GPIO_LED_BLUE  21
18 #define GPIO_LED_RED   22
19 
target_early_init(void)20 void target_early_init(void) {
21     // enable the external 16Mhz crystal
22     prci_base[1] = (1<<30); // hfxosc enable
23     while ((prci_base[1] & (1<<31)) == 0) // wait for hfxosc ready
24         ;
25 
26     // program the pll bypass, we should be running at 16Mhz now
27     prci_base[2] = 0x00070df1;
28 
29     // lfclock is a 32768Hz crystal, strapped externally
30 
31     // set up all the gpios
32     for (uint i = 0; i < 32; i++) {
33         switch (i) {
34             // default to input
35             default: gpio_config(i, GPIO_INPUT); break;
36 
37             // uart0
38             case 16: gpio_config(i, GPIO_AF0); break;
39             case 17: gpio_config(i, GPIO_AF0); break;
40 
41             // set the led gpios to output and default to off
42             case GPIO_LED_GREEN: gpio_set(i, 0); gpio_config(i, GPIO_OUTPUT); break;
43             case GPIO_LED_RED: gpio_set(i, 0); gpio_config(i, GPIO_OUTPUT); break;
44             case GPIO_LED_BLUE: gpio_set(i, 0); gpio_config(i, GPIO_OUTPUT); break;
45         }
46     }
47 }
48 
target_set_debug_led(unsigned int led,bool on)49 void target_set_debug_led(unsigned int led, bool on) {
50     unsigned int gpio;
51 
52     switch (led) {
53         default:
54         case 0: gpio = GPIO_LED_GREEN; break;
55         case 1: gpio = GPIO_LED_RED; break;
56         case 2: gpio = GPIO_LED_BLUE; break;
57     }
58 
59     gpio_set(gpio, on ? 1 : 0);
60 }
61 
target_init(void)62 void target_init(void) {
63 }
64 
65 
66