1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2013-7-14      Peng Fan     sep6200 implementation
9  */
10 
11 /**
12  * @addtogroup sep6200
13  */
14 
15 /*@{*/
16 #include <rthw.h>
17 #include <rtthread.h>
18 
19 #include <drivers/dev_serial.h>
20 
21 #include <sep6200.h>
22 
23 void rt_hw_serial_putc(const char c);
24 
25 #define UART0   ((struct uartport *)SEP6200_UART0_BASE)
26 
27 struct rt_device uart0_device;
28 struct serial_int_rx uart0_int_rx;
29 struct serial_device uart0 =
30 {
31     UART0,
32     &uart0_int_rx,
33   RT_NULL
34 };
35 
36 /*
37  * This function will handle rtos timer
38  */
rt_timer_handler(int vector,void * param)39 void rt_timer_handler(int vector, void *param)
40 {
41     rt_uint32_t clear_int;
42 
43     /* clear timer interrupt */
44     if (read_reg(SEP6200_TIMER_T2IMSR) & 0x1)
45         clear_int = read_reg(SEP6200_TIMER_T2ISCR);
46 
47     rt_tick_increase();
48 }
49 
50 /*
51  * This function will handle serial interrupt
52  */
rt_serial_handler(int vector,void * param)53 void rt_serial_handler(int vector, void *param)
54 {
55     rt_uint32_t num;
56     switch (vector) {
57         case INTSRC_UART0:
58 
59       /*No interrupt*/
60             if ((*(RP)SEP6200_UART0_IIR & 0x1))
61                 return;
62 
63       /*Get the serial interrupt num*/
64             num = (*(RP)SEP6200_UART0_IIR >> 1) & 0x7;
65 
66       /*Receive or timeout*/
67       if ((num == 6) || (num == 2))
68               rt_hw_serial_isr(&uart0_device);
69             break;
70         /*1,2,3 not implemented now, do in future*/
71         case INTSRC_UART1:
72             break;
73         case INTSRC_UART2:
74             break;
75         case INTSRC_UART3:
76             break;
77     }
78 }
79 
80 /*
81  * This function will init timer2 for system ticks
82  */
83 #define BUS4_FREQ   320000000UL
84 #define TIMER_CLK   BUS4_FREQ
85 #define HZ 100
rt_hw_timer_init(void)86 void rt_hw_timer_init(void)
87 {
88     *(RP)SEP6200_TIMER_T2LCR = (TIMER_CLK + HZ / 2) / HZ;
89     *(RP)SEP6200_TIMER_T2CR  = 0x6;
90 
91     rt_hw_interrupt_install(INTSRC_TIMER1, rt_timer_handler, RT_NULL, "timer");
92     rt_hw_interrupt_umask(INTSRC_TIMER1);
93 
94     /* start the timer */
95     *(RP)SEP6200_TIMER_T2CR |= 0x1;
96 }
97 
98 /*
99  * This function will init uart
100  */
101 #define UART_CLK 60000000UL
rt_hw_uart_init(void)102 void rt_hw_uart_init(void)
103 {
104     const rt_uint32_t uartclk = UART_CLK;
105 
106     *(RP)(SEP6200_UART0_LCR) = 0x83;
107     *(RP)(SEP6200_UART0_DLBH) = (uartclk/16/115200) >> 8;
108     *(RP)(SEP6200_UART0_DLBL) = (uartclk/16/115200) & 0xff;
109     *(RP)(SEP6200_UART0_LCR) = 0x83 & (~(0x1 << 7));
110 
111     *(RP)(SEP6200_UART0_FCR) = 0x0;
112     *(RP)(SEP6200_UART0_MCR) = 0x0;
113 
114     *(RP)(SEP6200_UART0_IER) = 0x0;
115     /* Enable rx interrupt*/
116     *(RP)(SEP6200_UART0_IER) |= 0x1;
117     /* Disable tx interrupt*/
118     *(RP)(SEP6200_UART0_IER) &= ~(0x1 << 1);
119 
120     rt_hw_interrupt_install(INTSRC_UART0, rt_serial_handler, RT_NULL, "uart0");
121     rt_hw_interrupt_umask(INTSRC_UART0);
122 
123     rt_hw_serial_register(&uart0_device, "uart0",
124             RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
125             &uart0);
126 }
127 
rt_hw_board_init(void)128 void rt_hw_board_init(void)
129 {
130   int i = 0;
131     rt_hw_uart_init();
132     rt_hw_timer_init();
133 }
134 
135 /*
136  * Write one char to serial, must not trigger interrupt
137  */
rt_hw_serial_putc(const char c)138 void rt_hw_serial_putc(const char c)
139 {
140     if (c == '\n')
141         rt_hw_serial_putc('\r');
142 
143     while (!((*(RP)SEP6200_UART0_LSR) & 0x40));
144 
145     *(RP)(SEP6200_UART0_TXFIFO) = c;
146 }
147 
148 /**
149 * This function is used by rt_kprintf to display a string on console.
150 *
151 * @param str the displayed string^M
152 */
rt_hw_console_output(const char * str)153 void rt_hw_console_output(const char *str)
154 {
155   while (*str) {
156     rt_hw_serial_putc(*str++);
157   }
158 }
159 
160 /*@}*/
161