rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2015 Brian Swetland |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <debug.h> |
| 25 | #include <arch/arm/cm.h> |
| 26 | #include <kernel/thread.h> |
| 27 | #include <platform.h> |
| 28 | |
| 29 | #include <platform/lpc43xx-clocks.h> |
| 30 | |
| 31 | void lpc43xx_debug_early_init(void); |
| 32 | void lpc43xx_debug_init(void); |
| 33 | |
| 34 | uint8_t __lpc43xx_main_clock_sel; |
| 35 | uint32_t __lpc43xx_main_clock_mhz; |
| 36 | |
| 37 | void platform_early_init(void) |
| 38 | { |
| 39 | #ifndef WITH_NO_CLOCK_INIT |
| 40 | unsigned cfg; |
| 41 | // Different boot modes will enable different sets of clocks. |
| 42 | // To keep it simple, we drop back to the 12MHz internal osc, |
| 43 | // power down the other clocks, and bring things back up in an |
| 44 | // orderly fashion. This costs a few hundred microseconds. |
| 45 | |
| 46 | // switch CPU clock to 12MHz internal osc |
| 47 | writel(readl(BASE_M4_CLK) | BASE_AUTOBLOCK, BASE_M4_CLK); |
| 48 | writel(BASE_CLK_SEL(CLK_IRC) | BASE_AUTOBLOCK, BASE_M4_CLK); |
| 49 | |
| 50 | // Disable PLL1, if it was already running |
| 51 | writel(PLL1_CTRL_PD, PLL1_CTRL); |
| 52 | |
| 53 | // Disable PLL0USB, if it was already running |
| 54 | writel(PLL0_CTRL_PD, PLL0USB_CTRL); |
| 55 | |
| 56 | // Disable XTAL osc if it was already running |
| 57 | writel(readl(XTAL_OSC_CTRL) | 1, XTAL_OSC_CTRL); |
| 58 | // Disable BYPASS or HF modes: |
| 59 | writel(1, XTAL_OSC_CTRL); |
| 60 | // Enable, HF=0 BYPASS=0 |
| 61 | writel(0, XTAL_OSC_CTRL); |
| 62 | // Wait |
| 63 | spin_cycles(3000); // 250uS @ 12MHz |
| 64 | |
| 65 | // PLL1: 12MHz -> N=(/2) -> M=(x32) -> P=(/2) 96MHz |
| 66 | cfg = PLL1_CTRL_NSEL_2 | PLL1_CTRL_PSEL_1 | PLL1_CTRL_MSEL(32) | |
| 67 | PLL1_CTRL_CLK_SEL(CLK_XTAL) | PLL1_CTRL_AUTOBLOCK; |
| 68 | writel(cfg, PLL1_CTRL); |
| 69 | while (!(readl(PLL1_STAT) & PLL1_STAT_LOCK)) ; |
| 70 | |
| 71 | writel(BASE_CLK_SEL(CLK_PLL1) | BASE_AUTOBLOCK, BASE_M4_CLK); |
| 72 | |
| 73 | // when moving from < 90 MHz to > 110MHz, must spend 50uS |
| 74 | // at 90-110MHz before shifting to high speeds |
| 75 | spin_cycles(4800); // 50uS @ 96MHz |
| 76 | |
| 77 | // disable P divider 192MHz |
| 78 | writel(cfg | PLL1_CTRL_DIRECT, PLL1_CTRL); |
| 79 | |
| 80 | // 12MHz -> 480MHz settings, per boot rom |
| 81 | writel(0x01967FFA, PLL0USB_MDIV); |
| 82 | writel(0x00302062, PLL0USB_NP_DIV); |
| 83 | // Enable PLL, wait for lock |
| 84 | cfg = PLL0_CTRL_CLK_SEL(CLK_XTAL) | PLL0_CTRL_DIRECTO | PLL0_CTRL_AUTOBLOCK; |
| 85 | writel(cfg, PLL0USB_CTRL); |
| 86 | while (!(readl(PLL0USB_STAT) & PLL0_STAT_LOCK)) ; |
| 87 | // Enable clock output |
| 88 | writel(cfg | PLL0_CTRL_CLKEN, PLL0USB_CTRL); |
| 89 | |
| 90 | #if 0 |
| 91 | // route PLL1 / 2 to CLK0 pin for verification |
| 92 | writel(0x11, 0x40086C00); // CLK0 = CLK_OUT, no PU/PD |
| 93 | writel(IDIV_CLK_SEL(CLK_PLL1) | IDIV_N(2), IDIVE_CTRL); |
| 94 | writel(BASE_CLK_SEL(CLK_IDIVE), BASE_OUT_CLK); |
| 95 | #endif |
| 96 | #if 0 |
| 97 | // route PLL0USB / 4 to CLK0 pin for verification |
| 98 | writel(0x11, 0x40086C00); // CLK0 = CLK_OUT, no PU/PD |
| 99 | writel(IDIV_CLK_SEL(CLK_PLL0USB) | IDIV_N(4), IDIVA_CTRL); |
| 100 | writel(BASE_CLK_SEL(CLK_IDIVA), BASE_OUT_CLK); |
| 101 | #endif |
| 102 | __lpc43xx_main_clock_mhz = 192000000; |
| 103 | __lpc43xx_main_clock_sel = CLK_PLL1; |
| 104 | #else |
| 105 | __lpc43xx_main_clock_mhz = 96000000; |
| 106 | __lpc43xx_main_clock_sel = CLK_IDIVC; |
| 107 | #endif |
| 108 | arm_cm_systick_init(__lpc43xx_main_clock_mhz); |
| 109 | lpc43xx_debug_early_init(); |
| 110 | } |
| 111 | |
| 112 | void lpc43xx_usb_init(u32 dmabase, size_t dmasize); |
| 113 | |
| 114 | void platform_init(void) |
| 115 | { |
| 116 | lpc43xx_debug_init(); |
| 117 | lpc43xx_usb_init(0x20000000, 4096); |
| 118 | } |
| 119 | |
| 120 | void platform_halt(platform_halt_action suggested_action, |
| 121 | platform_halt_reason reason) |
| 122 | { |
| 123 | arch_disable_ints(); |
| 124 | if (suggested_action == HALT_ACTION_REBOOT) { |
| 125 | // CORE reset |
| 126 | writel(1, 0x40053100); |
| 127 | } else { |
| 128 | dprintf(ALWAYS, "HALT: spinning forever... (reason = %d)\n", reason); |
| 129 | } |
| 130 | for(;;); |
| 131 | } |