rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2012 Corey Tabaka |
| 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 | #include <arch/ops.h> |
| 24 | #include <debug.h> |
| 25 | #include <reg.h> |
| 26 | #include <compiler.h> |
| 27 | #include <lib/cbuf.h> |
| 28 | #include <platform/interrupts.h> |
| 29 | |
| 30 | #include <kernel/thread.h> |
| 31 | |
| 32 | #include <hw_control_AM335x.h> |
| 33 | #include <soc_AM335x.h> |
| 34 | #include <hw_cm_wkup.h> |
| 35 | #include <hw_cm_per.h> |
| 36 | #include <hw_types.h> |
| 37 | #include <hw_uart_irda_cir.h> |
| 38 | #include <uart_irda_cir.h> |
| 39 | #include <interrupt.h> |
| 40 | |
| 41 | #define UART_CONSOLE_BASE (SOC_UART_0_REGS) |
| 42 | #define BAUD_RATE_115200 (115200) |
| 43 | #define UART_MODULE_INPUT_CLK (48000000) |
| 44 | #define UART_CONSOLE_INT (SYS_INT_UART0INT) |
| 45 | |
| 46 | static cbuf_t uart_rx_buf; |
| 47 | |
| 48 | static enum handler_return uart_irq_handler(void *arg) |
| 49 | { |
| 50 | unsigned char c; |
| 51 | unsigned int lcr; |
| 52 | bool resched = false; |
| 53 | |
| 54 | lcr = UARTRegConfigModeEnable(UART_CONSOLE_BASE, UART_REG_OPERATIONAL_MODE); |
| 55 | |
| 56 | while (HWREG(UART_CONSOLE_BASE + UART_LSR) & UART_LSR_RX_FIFO_E) { |
| 57 | c = (char) HWREG(UART_CONSOLE_BASE + UART_RHR); |
| 58 | cbuf_write_char(&uart_rx_buf, c, false); |
| 59 | resched = true; |
| 60 | } |
| 61 | |
| 62 | HWREG(UART_CONSOLE_BASE + UART_LCR) = lcr; |
| 63 | |
| 64 | return resched ? INT_RESCHEDULE : INT_NO_RESCHEDULE; |
| 65 | } |
| 66 | |
| 67 | void uart_init(void) |
| 68 | { |
| 69 | /* finish uart init to get rx going */ |
| 70 | cbuf_initialize(&uart_rx_buf, 16); |
| 71 | |
| 72 | register_int_handler(UART_CONSOLE_INT, uart_irq_handler, NULL); |
| 73 | unmask_interrupt(UART_CONSOLE_INT); |
| 74 | |
| 75 | /* enable RHR/CTI interrupt */ |
| 76 | UARTIntEnable(UART_CONSOLE_BASE, UART_INT_RHR_CTI); |
| 77 | } |
| 78 | |
| 79 | static void uart_putc(char c) |
| 80 | { |
| 81 | UARTCharPut(UART_CONSOLE_BASE, c); |
| 82 | } |
| 83 | |
| 84 | static int uart_getc(char *c, bool wait) |
| 85 | { |
| 86 | return cbuf_read_char(&uart_rx_buf, c, wait); |
| 87 | } |
| 88 | |
| 89 | void platform_dputc(char c) |
| 90 | { |
| 91 | if (c == '\n') |
| 92 | uart_putc('\r'); |
| 93 | uart_putc(c); |
| 94 | } |
| 95 | |
| 96 | int platform_dgetc(char *c, bool wait) |
| 97 | { |
| 98 | return uart_getc(c, wait); |
| 99 | } |
| 100 | |
| 101 | void platform_init_debug(void) |
| 102 | { |
| 103 | /* configure UART0 clock */ |
| 104 | HWREG(SOC_CM_WKUP_REGS + CM_WKUP_UART0_CLKCTRL) |= |
| 105 | CM_WKUP_UART0_CLKCTRL_MODULEMODE_ENABLE; |
| 106 | |
| 107 | while (CM_WKUP_UART0_CLKCTRL_MODULEMODE_ENABLE != |
| 108 | (HWREG(SOC_CM_WKUP_REGS + CM_WKUP_UART0_CLKCTRL) & |
| 109 | CM_WKUP_UART0_CLKCTRL_MODULEMODE)); |
| 110 | |
| 111 | while (CM_WKUP_CLKSTCTRL_CLKACTIVITY_UART0_GFCLK != |
| 112 | (HWREG(SOC_CM_WKUP_REGS + CM_WKUP_CLKSTCTRL) & |
| 113 | CM_WKUP_CLKSTCTRL_CLKACTIVITY_UART0_GFCLK)); |
| 114 | |
| 115 | while ((CM_WKUP_UART0_CLKCTRL_IDLEST_FUNC << CM_WKUP_UART0_CLKCTRL_IDLEST_SHIFT) != |
| 116 | (HWREG(SOC_CM_WKUP_REGS + CM_WKUP_UART0_CLKCTRL) & |
| 117 | CM_WKUP_UART0_CLKCTRL_IDLEST)); |
| 118 | |
| 119 | /* RXD */ |
| 120 | HWREG(SOC_CONTROL_REGS + CONTROL_CONF_UART_RXD(0)) = |
| 121 | (CONTROL_CONF_UART0_RXD_CONF_UART0_RXD_PUTYPESEL | |
| 122 | CONTROL_CONF_UART0_RXD_CONF_UART0_RXD_RXACTIVE); |
| 123 | |
| 124 | /* TXD */ |
| 125 | HWREG(SOC_CONTROL_REGS + CONTROL_CONF_UART_TXD(0)) = |
| 126 | CONTROL_CONF_UART0_TXD_CONF_UART0_TXD_PUTYPESEL; |
| 127 | |
| 128 | /* software reset */ |
| 129 | HWREG(UART_CONSOLE_BASE + UART_SYSC) |= (UART_SYSC_SOFTRESET); |
| 130 | while (!(HWREG(UART_CONSOLE_BASE + UART_SYSS) & UART_SYSS_RESETDONE)); |
| 131 | |
| 132 | /* setup fifo */ |
| 133 | UARTFIFOConfig(UART_CONSOLE_BASE, |
| 134 | UART_FIFO_CONFIG(UART_TRIG_LVL_GRANULARITY_1, |
| 135 | UART_TRIG_LVL_GRANULARITY_1, 1, 1, 1, 1, |
| 136 | UART_DMA_EN_PATH_SCR, |
| 137 | UART_DMA_MODE_0_ENABLE)); |
| 138 | |
| 139 | /* baud rate settings */ |
| 140 | unsigned int divisor = UARTDivisorValCompute(UART_MODULE_INPUT_CLK, |
| 141 | BAUD_RATE_115200, |
| 142 | UART16x_OPER_MODE, |
| 143 | UART_MIR_OVERSAMPLING_RATE_42); |
| 144 | |
| 145 | UARTDivisorLatchWrite(UART_CONSOLE_BASE, divisor); |
| 146 | UARTRegConfigModeEnable(UART_CONSOLE_BASE, UART_REG_CONFIG_MODE_B); |
| 147 | UARTLineCharacConfig(UART_CONSOLE_BASE, |
| 148 | (UART_FRAME_WORD_LENGTH_8 | UART_FRAME_NUM_STB_1), |
| 149 | UART_PARITY_NONE); |
| 150 | |
| 151 | UARTDivisorLatchDisable(UART_CONSOLE_BASE); |
| 152 | UARTBreakCtl(UART_CONSOLE_BASE, UART_BREAK_COND_DISABLE); |
| 153 | UARTOperatingModeSelect(UART_CONSOLE_BASE, UART16x_OPER_MODE); |
| 154 | } |
| 155 | |
| 156 | void debug_point(char c) |
| 157 | { |
| 158 | uart_putc('\r'); |
| 159 | uart_putc('\n'); |
| 160 | uart_putc(c); |
| 161 | } |
| 162 | |
| 163 | void debug_hex(int val) |
| 164 | { |
| 165 | unsigned int i, temp; |
| 166 | |
| 167 | uart_putc('\r'); |
| 168 | uart_putc('\n'); |
| 169 | |
| 170 | for (i=0; i < 8; i++) { |
| 171 | temp = (val >> (28 - i*4)) & 0xf; |
| 172 | uart_putc((temp < 10 ? '0' + temp : 'a' + temp - 10)); |
| 173 | } |
| 174 | } |
| 175 | |