drv_uart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. volatile void *earlycon_base = 0;
  162. const 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_earlycon_ioremap_early(void)
  173. {
  174. earlycon_base = rt_ioremap_early((void *)AUX_BASE, earlycon_size);
  175. }
  176. void rt_hw_console_output(const char *str)
  177. {
  178. if (earlycon_base)
  179. {
  180. while (*str)
  181. {
  182. early_putc(*str++);
  183. }
  184. }
  185. }
  186. void early_printhex(rt_ubase_t number)
  187. {
  188. char str[sizeof("0123456789abcdef")];
  189. str[16] = 0;
  190. for (int i = 15; i >= 0; --i)
  191. {
  192. str[i] = "0123456789abcdef"[(number & 0xf)];
  193. number >>= 4;
  194. }
  195. rt_kputs(str);
  196. }
  197. #ifdef RT_USING_UART1
  198. static void rt_hw_aux_uart_isr(int irqno, void *param)
  199. {
  200. struct rt_serial_device *serial = (struct rt_serial_device*)param;
  201. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  202. }
  203. #endif
  204. static void rt_hw_uart_isr(int irqno, void *param)
  205. {
  206. #ifdef RT_USING_UART0
  207. if((PACTL_CS & IRQ_UART0) == IRQ_UART0)
  208. {
  209. PACTL_CS &= ~(IRQ_UART0);
  210. rt_hw_serial_isr(&_serial0, RT_SERIAL_EVENT_RX_IND);
  211. PL011_REG_ICR(uart0_addr) = PL011_INTERRUPT_RECEIVE;
  212. }
  213. #endif
  214. #ifdef RT_USING_UART3
  215. if((PACTL_CS & IRQ_UART3) == IRQ_UART3)
  216. {
  217. PACTL_CS &= ~(IRQ_UART3);
  218. rt_hw_serial_isr(&_serial3, RT_SERIAL_EVENT_RX_IND);
  219. PL011_REG_ICR(uart3_addr) = PL011_INTERRUPT_RECEIVE;
  220. }
  221. #endif
  222. #ifdef RT_USING_UART4
  223. if((PACTL_CS & IRQ_UART4) == IRQ_UART4)
  224. {
  225. PACTL_CS &= ~(IRQ_UART4);
  226. rt_hw_serial_isr(&_serial4, RT_SERIAL_EVENT_RX_IND);
  227. PL011_REG_ICR(uart4_addr) = PL011_INTERRUPT_RECEIVE;
  228. }
  229. #endif
  230. #ifdef RT_USING_UART5
  231. if((PACTL_CS & IRQ_UART5) == IRQ_UART5)
  232. {
  233. PACTL_CS &= ~(IRQ_UART5);
  234. rt_hw_serial_isr(&_serial5, RT_SERIAL_EVENT_RX_IND);
  235. PL011_REG_ICR(uart5_addr) = PL011_INTERRUPT_RECEIVE;
  236. }
  237. #endif
  238. }
  239. #ifdef RT_USING_UART0
  240. /* UART device driver structure */
  241. static struct hw_uart_device _uart0_device =
  242. {
  243. UART0_BASE,
  244. IRQ_PL011,
  245. };
  246. #endif
  247. #ifdef RT_USING_UART1
  248. /* UART device driver structure */
  249. static struct hw_uart_device _uart1_device =
  250. {
  251. AUX_BASE,
  252. IRQ_AUX_UART,
  253. };
  254. #endif
  255. #ifdef RT_USING_UART3
  256. static struct hw_uart_device _uart3_device =
  257. {
  258. UART3_BASE,
  259. IRQ_PL011,
  260. };
  261. #endif
  262. #ifdef RT_USING_UART4
  263. static struct hw_uart_device _uart4_device =
  264. {
  265. UART4_BASE,
  266. IRQ_PL011,
  267. };
  268. #endif
  269. #ifdef RT_USING_UART5
  270. static struct hw_uart_device _uart5_device =
  271. {
  272. UART5_BASE,
  273. IRQ_PL011,
  274. };
  275. #endif
  276. int rt_hw_uart_init(void)
  277. {
  278. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  279. #ifdef RT_USING_UART0
  280. struct hw_uart_device *uart0;
  281. uart0 = &_uart0_device;
  282. _serial0.ops = &_uart_ops;
  283. _serial0.config = config;
  284. uart0_addr = UART0_BASE;
  285. #ifdef RT_USING_SMART
  286. uart0_addr = (size_t)rt_ioremap((void*)UART0_BASE, 0x1000);
  287. #endif
  288. earlycon_base = (void *)uart0_addr;
  289. uart0->hw_base = uart0_addr;
  290. /* register UART0 device */
  291. rt_hw_serial_register(&_serial0, "uart0",
  292. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  293. uart0);
  294. rt_hw_interrupt_install(uart0->irqno, rt_hw_uart_isr, &_serial0, "uart0");
  295. #endif
  296. #ifdef RT_USING_UART1
  297. struct hw_uart_device *uart1;
  298. uart1 = &_uart1_device;
  299. _serial1.ops = &_uart_ops;
  300. _serial1.config = config;
  301. uart1->hw_base = (size_t)rt_ioremap((void*)AUX_BASE, 0x1000);
  302. /* register UART1 device */
  303. rt_hw_serial_register(&_serial1, "uart1",
  304. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  305. uart1);
  306. rt_hw_interrupt_install(uart1->irqno, rt_hw_aux_uart_isr, &_serial1, "uart1");
  307. #endif
  308. #ifdef RT_USING_UART3
  309. struct hw_uart_device *uart3;
  310. uart3 = &_uart3_device;
  311. _serial3.ops = &_uart_ops;
  312. _serial3.config = config;
  313. uart3_addr = (size_t)rt_ioremap((void*)UART3_BASE, 0x1000);
  314. uart3->hw_base = uart3_addr;
  315. /* register UART3 device */
  316. rt_hw_serial_register(&_serial3, "uart3",
  317. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  318. uart3);
  319. rt_hw_interrupt_install(uart3->irqno, rt_hw_uart_isr, &_serial3, "uart3");
  320. #endif
  321. #ifdef RT_USING_UART4
  322. struct hw_uart_device *uart4;
  323. uart4 = &_uart4_device;
  324. _serial4.ops = &_uart_ops;
  325. _serial4.config = config;
  326. uart4_addr = (size_t)rt_ioremap((void*)UART4_BASE, 0x1000);
  327. uart4->hw_base = uart4_addr;
  328. /* register UART4 device */
  329. rt_hw_serial_register(&_serial4, "uart4",
  330. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  331. uart4);
  332. rt_hw_interrupt_install(uart4->irqno, rt_hw_uart_isr, &_serial4, "uart4");
  333. #endif
  334. #ifdef RT_USING_UART5
  335. struct hw_uart_device *uart5;
  336. uart5 = &_uart5_device;
  337. _serial5.ops = &_uart_ops;
  338. _serial5.config = config;
  339. uart5_addr = (size_t)rt_ioremap((void*)UART5_BASE, 0x1000);
  340. uart5->hw_base = uart5_addr;
  341. /* register UART5 device */
  342. rt_hw_serial_register(&_serial5, "uart5",
  343. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  344. uart5);
  345. rt_hw_interrupt_install(uart5->irqno, rt_hw_uart_isr, &_serial5, "uart5");
  346. #endif
  347. return 0;
  348. }