blob: b4d80941150a559b6c96f1f970cd3296311df71b [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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 <reg.h>
26#include <kernel/thread.h>
27#include <lib/cbuf.h>
28
29#include <arch/arm/cm.h>
30#include <platform/lpc43xx-uart.h>
31#include <platform/lpc43xx-clocks.h>
32
33static cbuf_t console_rx_buf;
34
35#ifndef TARGET_DEBUG_BAUDRATE
36#define TARGET_DEBUG_BAUDRATE 115200
37#endif
38
39#if TARGET_DEBUG_UART == 1
40#define UART_BASE UART0_BASE
41#define UART_IRQ lpc43xx_USART0_IRQ
42#define UART_IRQn USART0_IRQn
43#elif TARGET_DEBUG_UART == 2
44#define UART_BASE UART1_BASE
45#define UART_IRQ lpc43xx_UART1_IRQ
46#define UART_IRQn UART1_IRQn
47#elif TARGET_DEBUG_UART == 3
48#define UART_BASE UART2_BASE
49#define UART_IRQ lpc43xx_USART2_IRQ
50#define UART_IRQn USART2_IRQn
51#elif TARGET_DEBUG_UART == 4
52#define UART_BASE UART3_BASE
53#define UART_IRQ lpc43xx_USART3_IRQ
54#define UART_IRQn USART3_IRQn
55#else
56#warning TARGET_DEBUG_UART unspecified
57#endif
58
59static u32 base_uart_clk[4] = {
60 BASE_UART0_CLK,
61 BASE_UART1_CLK,
62 BASE_UART2_CLK,
63 BASE_UART3_CLK
64};
65
66extern uint8_t __lpc43xx_main_clock_sel;
67extern uint32_t __lpc43xx_main_clock_mhz;
68
69#define ITM_STIM0 0xE0000000
70#define ITM_TER 0xE0000E00
71#define ITM_TCR 0xE0000E80
72#define ITM_LAR 0xE0000FB0
73
74#define TPI_ACPR 0xE0040010
75#define TPI_SPPR 0xE00400F0
76#define TPI_FFCR 0xE0040304
77
78#define DEMCR 0xE000EDFC
79#define DEMCR_TRCENA (1 << 24)
80
81void lpc43xx_debug_early_init(void)
82{
83 // ensure ITM and DWT are enabled
84 writel(readl(DEMCR) | DEMCR_TRCENA, DEMCR);
85
86 writel((1 << 9) | (1 << 16) | (2 << 10), DWT_CTRL);
87
88 // configure TPIU for one-wire, nrz, 6mbps
89 writel((__lpc43xx_main_clock_mhz / 6000000) - 1, TPI_ACPR);
90 writel(2, TPI_SPPR);
91 writel(0x100, TPI_FFCR);
92
93 // configure ITM
94 writel(0xC5ACCE55, ITM_LAR); // unlock regs
95 writel(0x0001000D, ITM_TCR); // ID=1, enable ITM, SYNC, DWT events
96 writel(0xFFFFFFFF, ITM_TER); // enable all trace ports
97
98#ifdef UART_BASE
99#if TARGET_DEBUG_BAUDRATE == 115200
100 // config for 115200-n-8-1 from 12MHz clock
101 writel(BASE_CLK_SEL(CLK_IRC), base_uart_clk[TARGET_DEBUG_UART - 1]);
102 writel(LCR_DLAB, UART_BASE + REG_LCR);
103 writel(4, UART_BASE + REG_DLL);
104 writel(0, UART_BASE + REG_DLM);
105 writel(FDR_DIVADDVAL(5) | FDR_MULVAL(8), UART_BASE + REG_FDR);
106#else
107 uint32_t div = __lpc43xx_main_clock_mhz / 16 / TARGET_DEBUG_BAUDRATE;
108 writel(BASE_CLK_SEL(__lpc43xx_main_clock_sel),
109 base_uart_clk[TARGET_DEBUG_UART - 1]);
110 writel(LCR_DLAB, UART_BASE + REG_LCR);
111 writel(div & 0xFF, UART_BASE + REG_DLL);
112 writel((div >> 8) & 0xFF, UART_BASE + REG_DLM);
113#endif
114 writel(LCR_WLS_8 | LCR_SBS_1, UART_BASE + REG_LCR);
115 writel(FCR_FIFOEN | FCR_RX_TRIG_1, UART_BASE + REG_FCR);
116 writel(IER_RBRIE, UART_BASE + REG_IER);
117 NVIC_EnableIRQ(UART_IRQn);
118#endif
119}
120
121void lpc43xx_debug_init(void)
122{
123 cbuf_initialize(&console_rx_buf, 64);
124}
125
126#ifdef UART_BASE
127void UART_IRQ (void) {
128 arm_cm_irq_entry();
129 while (readl(UART_BASE + REG_LSR) & LSR_RDR) {
130 unsigned c = readl(UART_BASE + REG_RBR);
131 if (cbuf_space_avail(&console_rx_buf)) {
132 cbuf_write_char(&console_rx_buf, c, false);
133 }
134 }
135 arm_cm_irq_exit(1);
136}
137#endif
138
139void platform_dputc(char c)
140{
141 // if ITM is enabled, send character to STIM0
142 if (readl(ITM_TCR) & 1) {
143 while (!readl(ITM_STIM0)) ;
144 writeb(c, ITM_STIM0);
145 }
146#ifdef UART_BASE
147 while (!(readl(UART_BASE + REG_LSR) & LSR_THRE)) ;
148 writel(c, UART_BASE + REG_THR);
149#endif
150}
151
152int platform_dgetc(char *c, bool wait)
153{
154 if (cbuf_read_char(&console_rx_buf, c, wait) == 0)
155 return -1;
156 return 0;
157}
158
159#define DCRDR 0xE000EDF8
160
161void _debugmonitor(void) {
162 u32 n;
163 arm_cm_irq_entry();
164 n = readl(DCRDR);
165 if (n & 0x80000000) {
166 switch (n >> 24) {
167 case 0x80: // write to console
168 if (cbuf_space_avail(&console_rx_buf)) {
169 cbuf_write_char(&console_rx_buf, n & 0xFF, false);
170 }
171 n = 0;
172 break;
173 default:
174 n = 0x01000000;
175 }
176 writel(n, DCRDR);
177 }
178 arm_cm_irq_exit(1);
179}