drv_uart.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-04-16 bigmagic first version
  9. * 2020-05-26 bigmagic add other uart
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include "board.h"
  15. #include "drv_uart.h"
  16. #include "drv_gpio.h"
  17. #include <mmu.h>
  18. size_t uart0_addr = 0;
  19. size_t uart3_addr = 0;
  20. size_t uart4_addr = 0;
  21. size_t uart5_addr = 0;
  22. #ifdef RT_USING_UART0
  23. static struct rt_serial_device _serial0;
  24. #endif
  25. #ifdef RT_USING_UART1
  26. static struct rt_serial_device _serial1;
  27. #endif
  28. #ifdef RT_USING_UART3
  29. static struct rt_serial_device _serial3;
  30. #endif
  31. #ifdef RT_USING_UART4
  32. static struct rt_serial_device _serial4;
  33. #endif
  34. #ifdef RT_USING_UART5
  35. static struct rt_serial_device _serial5;
  36. #endif
  37. struct hw_uart_device
  38. {
  39. rt_ubase_t hw_base;
  40. rt_uint32_t irqno;
  41. };
  42. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  43. {
  44. struct hw_uart_device *uart;
  45. uint32_t bauddiv = (UART_REFERENCE_CLOCK / cfg->baud_rate)* 1000 / 16;
  46. uint32_t ibrd = bauddiv / 1000;
  47. RT_ASSERT(serial != RT_NULL);
  48. uart = (struct hw_uart_device *)serial->parent.user_data;
  49. if(uart->hw_base == AUX_BASE)
  50. {
  51. prev_raspi_pin_mode(GPIO_PIN_14, ALT5);
  52. prev_raspi_pin_mode(GPIO_PIN_15, ALT5);
  53. AUX_ENABLES(uart->hw_base) = 1; /* Enable UART1 */
  54. AUX_MU_IER_REG(uart->hw_base) = 0; /* Disable interrupt */
  55. AUX_MU_CNTL_REG(uart->hw_base) = 0; /* Disable Transmitter and Receiver */
  56. AUX_MU_LCR_REG(uart->hw_base) = 3; /* Works in 8-bit mode */
  57. AUX_MU_MCR_REG(uart->hw_base) = 0; /* Disable RTS */
  58. AUX_MU_IIR_REG(uart->hw_base) = 0xC6; /* Enable FIFO, Clear FIFO */
  59. AUX_MU_BAUD_REG(uart->hw_base) = 270; /* 115200 = system clock 250MHz / (8 * (baud + 1)), baud = 270 */
  60. AUX_MU_CNTL_REG(uart->hw_base) = 3; /* Enable Transmitter and Receiver */
  61. return RT_EOK;
  62. }
  63. if(uart->hw_base == uart0_addr)
  64. {
  65. prev_raspi_pin_mode(GPIO_PIN_14, ALT0);
  66. prev_raspi_pin_mode(GPIO_PIN_15, ALT0);
  67. }
  68. if(uart->hw_base == uart3_addr)
  69. {
  70. prev_raspi_pin_mode(GPIO_PIN_4, ALT4);
  71. prev_raspi_pin_mode(GPIO_PIN_5, ALT4);
  72. }
  73. if(uart->hw_base == uart4_addr)
  74. {
  75. prev_raspi_pin_mode(GPIO_PIN_8, ALT4);
  76. prev_raspi_pin_mode(GPIO_PIN_9, ALT4);
  77. }
  78. if(uart->hw_base == uart5_addr)
  79. {
  80. prev_raspi_pin_mode(GPIO_PIN_12, ALT4);
  81. prev_raspi_pin_mode(GPIO_PIN_13, ALT4);
  82. }
  83. PL011_REG_CR(uart->hw_base) = 0;/*Clear UART setting*/
  84. PL011_REG_LCRH(uart->hw_base) = 0;/*disable FIFO*/
  85. PL011_REG_IBRD(uart->hw_base) = ibrd;
  86. PL011_REG_FBRD(uart->hw_base) = (((bauddiv - ibrd * 1000) * 64 + 500) / 1000);
  87. PL011_REG_LCRH(uart->hw_base) = PL011_LCRH_WLEN_8;/*FIFO*/
  88. PL011_REG_CR(uart->hw_base) = PL011_CR_UARTEN | PL011_CR_TXE | PL011_CR_RXE;/*art enable, TX/RX enable*/
  89. return RT_EOK;
  90. }
  91. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  92. {
  93. struct hw_uart_device *uart;
  94. RT_ASSERT(serial != RT_NULL);
  95. uart = (struct hw_uart_device *)serial->parent.user_data;
  96. switch (cmd)
  97. {
  98. case RT_DEVICE_CTRL_CLR_INT:
  99. break;
  100. case RT_DEVICE_CTRL_SET_INT:
  101. /* enable rx irq */
  102. if(uart->hw_base == AUX_BASE)
  103. {
  104. AUX_MU_IER_REG(uart->hw_base) = 0x1;
  105. }
  106. else
  107. {
  108. PL011_REG_IMSC(uart->hw_base) |= PL011_IMSC_RXIM;
  109. }
  110. rt_hw_interrupt_umask(uart->irqno);
  111. break;
  112. }
  113. return RT_EOK;
  114. }
  115. static int uart_putc(struct rt_serial_device *serial, char c)
  116. {
  117. struct hw_uart_device *uart;
  118. RT_ASSERT(serial != RT_NULL);
  119. uart = (struct hw_uart_device *)serial->parent.user_data;
  120. if(uart->hw_base == AUX_BASE)
  121. {
  122. while (!(AUX_MU_LSR_REG(uart->hw_base) & 0x20));
  123. AUX_MU_IO_REG(uart->hw_base) = c;
  124. }
  125. else
  126. {
  127. while ((PL011_REG_FR(uart->hw_base) & PL011_FR_TXFF));
  128. PL011_REG_DR(uart->hw_base) = (uint8_t)c;
  129. }
  130. return 1;
  131. }
  132. static int uart_getc(struct rt_serial_device *serial)
  133. {
  134. int ch = -1;
  135. struct hw_uart_device *uart;
  136. RT_ASSERT(serial != RT_NULL);
  137. uart = (struct hw_uart_device *)serial->parent.user_data;
  138. if(uart->hw_base == AUX_BASE)
  139. {
  140. if ((AUX_MU_LSR_REG(uart->hw_base) & 0x01))
  141. {
  142. ch = AUX_MU_IO_REG(uart->hw_base) & 0xff;
  143. }
  144. }
  145. else
  146. {
  147. if((PL011_REG_FR(uart->hw_base) & PL011_FR_RXFE) == 0)
  148. {
  149. ch = PL011_REG_DR(uart->hw_base) & 0xff;
  150. }
  151. }
  152. return ch;
  153. }
  154. static const struct rt_uart_ops _uart_ops =
  155. {
  156. uart_configure,
  157. uart_control,
  158. uart_putc,
  159. uart_getc,
  160. };
  161. void *earlycon_base = (void *)AUX_BASE;
  162. size_t earlycon_size = 0x1000;
  163. extern void early_putc(int c)
  164. {
  165. if (c == '\n')
  166. {
  167. early_putc('\r');
  168. }
  169. while (!(AUX_MU_LSR_REG(earlycon_base) & 0x20));
  170. AUX_MU_IO_REG(earlycon_base) = c;
  171. }
  172. void rt_hw_console_output(const char *str)
  173. {
  174. if (earlycon_base)
  175. {
  176. while (*str)
  177. {
  178. early_putc(*str++);
  179. }
  180. }
  181. }
  182. void early_printhex(rt_ubase_t number)
  183. {
  184. char str[sizeof("0123456789abcdef")];
  185. str[16] = 0;
  186. for (int i = 15; i >= 0; --i)
  187. {
  188. str[i] = "0123456789abcdef"[(number & 0xf)];
  189. number >>= 4;
  190. }
  191. rt_kputs(str);
  192. }
  193. #ifdef RT_USING_UART1
  194. static void rt_hw_aux_uart_isr(int irqno, void *param)
  195. {
  196. struct rt_serial_device *serial = (struct rt_serial_device*)param;
  197. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  198. }
  199. #endif
  200. static void rt_hw_uart_isr(int irqno, void *param)
  201. {
  202. #ifdef RT_USING_UART0
  203. if((PACTL_CS & IRQ_UART0) == IRQ_UART0)
  204. {
  205. PACTL_CS &= ~(IRQ_UART0);
  206. rt_hw_serial_isr(&_serial0, RT_SERIAL_EVENT_RX_IND);
  207. PL011_REG_ICR(uart0_addr) = PL011_INTERRUPT_RECEIVE;
  208. }
  209. #endif
  210. #ifdef RT_USING_UART3
  211. if((PACTL_CS & IRQ_UART3) == IRQ_UART3)
  212. {
  213. PACTL_CS &= ~(IRQ_UART3);
  214. rt_hw_serial_isr(&_serial3, RT_SERIAL_EVENT_RX_IND);
  215. PL011_REG_ICR(uart3_addr) = PL011_INTERRUPT_RECEIVE;
  216. }
  217. #endif
  218. #ifdef RT_USING_UART4
  219. if((PACTL_CS & IRQ_UART4) == IRQ_UART4)
  220. {
  221. PACTL_CS &= ~(IRQ_UART4);
  222. rt_hw_serial_isr(&_serial4, RT_SERIAL_EVENT_RX_IND);
  223. PL011_REG_ICR(uart4_addr) = PL011_INTERRUPT_RECEIVE;
  224. }
  225. #endif
  226. #ifdef RT_USING_UART5
  227. if((PACTL_CS & IRQ_UART5) == IRQ_UART5)
  228. {
  229. PACTL_CS &= ~(IRQ_UART5);
  230. rt_hw_serial_isr(&_serial5, RT_SERIAL_EVENT_RX_IND);
  231. PL011_REG_ICR(uart5_addr) = PL011_INTERRUPT_RECEIVE;
  232. }
  233. #endif
  234. }
  235. #ifdef RT_USING_UART0
  236. /* UART device driver structure */
  237. static struct hw_uart_device _uart0_device =
  238. {
  239. UART0_BASE,
  240. IRQ_PL011,
  241. };
  242. #endif
  243. #ifdef RT_USING_UART1
  244. /* UART device driver structure */
  245. static struct hw_uart_device _uart1_device =
  246. {
  247. AUX_BASE,
  248. IRQ_AUX_UART,
  249. };
  250. #endif
  251. #ifdef RT_USING_UART3
  252. static struct hw_uart_device _uart3_device =
  253. {
  254. UART3_BASE,
  255. IRQ_PL011,
  256. };
  257. #endif
  258. #ifdef RT_USING_UART4
  259. static struct hw_uart_device _uart4_device =
  260. {
  261. UART4_BASE,
  262. IRQ_PL011,
  263. };
  264. #endif
  265. #ifdef RT_USING_UART5
  266. static struct hw_uart_device _uart5_device =
  267. {
  268. UART5_BASE,
  269. IRQ_PL011,
  270. };
  271. #endif
  272. int rt_hw_uart_init(void)
  273. {
  274. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  275. #ifdef RT_USING_UART0
  276. struct hw_uart_device *uart0;
  277. uart0 = &_uart0_device;
  278. _serial0.ops = &_uart_ops;
  279. _serial0.config = config;
  280. uart0_addr = UART0_BASE;
  281. #ifdef RT_USING_SMART
  282. uart0_addr = (size_t)rt_ioremap((void*)UART0_BASE, 0x1000);
  283. #endif
  284. earlycon_base = (void *)uart0_addr;
  285. uart0->hw_base = uart0_addr;
  286. /* register UART0 device */
  287. rt_hw_serial_register(&_serial0, "uart0",
  288. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  289. uart0);
  290. rt_hw_interrupt_install(uart0->irqno, rt_hw_uart_isr, &_serial0, "uart0");
  291. #endif
  292. #ifdef RT_USING_UART1
  293. struct hw_uart_device *uart1;
  294. uart1 = &_uart1_device;
  295. _serial1.ops = &_uart_ops;
  296. _serial1.config = config;
  297. uart1->hw_base = (size_t)rt_ioremap((void*)AUX_BASE, 0x1000);
  298. /* register UART1 device */
  299. rt_hw_serial_register(&_serial1, "uart1",
  300. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  301. uart1);
  302. rt_hw_interrupt_install(uart1->irqno, rt_hw_aux_uart_isr, &_serial1, "uart1");
  303. #endif
  304. #ifdef RT_USING_UART3
  305. struct hw_uart_device *uart3;
  306. uart3 = &_uart3_device;
  307. _serial3.ops = &_uart_ops;
  308. _serial3.config = config;
  309. uart3_addr = (size_t)rt_ioremap((void*)UART3_BASE, 0x1000);
  310. uart3->hw_base = uart3_addr;
  311. /* register UART3 device */
  312. rt_hw_serial_register(&_serial3, "uart3",
  313. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  314. uart3);
  315. rt_hw_interrupt_install(uart3->irqno, rt_hw_uart_isr, &_serial3, "uart3");
  316. #endif
  317. #ifdef RT_USING_UART4
  318. struct hw_uart_device *uart4;
  319. uart4 = &_uart4_device;
  320. _serial4.ops = &_uart_ops;
  321. _serial4.config = config;
  322. uart4_addr = (size_t)rt_ioremap((void*)UART4_BASE, 0x1000);
  323. uart4->hw_base = uart4_addr;
  324. /* register UART4 device */
  325. rt_hw_serial_register(&_serial4, "uart4",
  326. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  327. uart4);
  328. rt_hw_interrupt_install(uart4->irqno, rt_hw_uart_isr, &_serial4, "uart4");
  329. #endif
  330. #ifdef RT_USING_UART5
  331. struct hw_uart_device *uart5;
  332. uart5 = &_uart5_device;
  333. _serial5.ops = &_uart_ops;
  334. _serial5.config = config;
  335. uart5_addr = (size_t)rt_ioremap((void*)UART5_BASE, 0x1000);
  336. uart5->hw_base = uart5_addr;
  337. /* register UART5 device */
  338. rt_hw_serial_register(&_serial5, "uart5",
  339. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  340. uart5);
  341. rt_hw_interrupt_install(uart5->irqno, rt_hw_uart_isr, &_serial5, "uart5");
  342. #endif
  343. return 0;
  344. }