uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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-07-06 Bernard the first version
  9. * 2014-01-11 RTsien support UART0 to UART5 straightly
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include <am33xx.h>
  15. #include <interrupt.h>
  16. #include "uart.h"
  17. #include "uart_reg.h"
  18. struct am33xx_uart
  19. {
  20. unsigned long base;
  21. int irq;
  22. };
  23. static void am33xx_uart_isr(int irqno, void* param)
  24. {
  25. rt_uint32_t iir;
  26. struct am33xx_uart* uart;
  27. struct rt_serial_device *serial;
  28. serial = (struct rt_serial_device*)param;
  29. uart = (struct am33xx_uart *)serial->parent.user_data;
  30. iir = UART_IIR_REG(uart->base);
  31. if ((iir & (0x02 << 1)) || (iir & (0x6 << 1)))
  32. {
  33. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  34. }
  35. }
  36. #define NOT_IMPLEMENTED() RT_ASSERT(0)
  37. static rt_err_t am33xx_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  38. {
  39. struct am33xx_uart* uart;
  40. unsigned long base;
  41. RT_ASSERT(serial != RT_NULL);
  42. uart = (struct am33xx_uart *)serial->parent.user_data;
  43. RT_ASSERT(uart);
  44. base = uart->base;
  45. #define __LCR UART_LCR_REG(base)
  46. if (cfg->data_bits == DATA_BITS_8)
  47. __LCR |= 3;
  48. else
  49. NOT_IMPLEMENTED();
  50. if (cfg->stop_bits == STOP_BITS_1)
  51. __LCR &= ~(1<<2);
  52. else
  53. __LCR |= (1<<2);
  54. if (cfg->parity == PARITY_NONE)
  55. __LCR &= ~(1<<3);
  56. else
  57. __LCR |= (1<<3);
  58. __LCR |= (1<<7);
  59. if (cfg->baud_rate == BAUD_RATE_115200)
  60. {
  61. UART_DLL_REG(base) = 26;
  62. UART_DLH_REG(base) = 0;
  63. }
  64. else if (cfg->baud_rate == BAUD_RATE_9600)
  65. {
  66. UART_DLL_REG(base) = 0x38;
  67. UART_DLH_REG(base) = 1;
  68. }
  69. else
  70. {
  71. NOT_IMPLEMENTED();
  72. }
  73. __LCR &= ~(1<<7);
  74. UART_MDR1_REG(base) = 0;
  75. UART_MDR2_REG(base) = 0;
  76. #undef __LCR
  77. return RT_EOK;
  78. }
  79. static rt_err_t am33xx_control(struct rt_serial_device *serial, int cmd, void *arg)
  80. {
  81. struct am33xx_uart* uart;
  82. RT_ASSERT(serial != RT_NULL);
  83. uart = (struct am33xx_uart *)serial->parent.user_data;
  84. switch (cmd)
  85. {
  86. case RT_DEVICE_CTRL_CLR_INT:
  87. /* disable rx irq */
  88. rt_hw_interrupt_mask(uart->irq);
  89. break;
  90. case RT_DEVICE_CTRL_SET_INT:
  91. /* enable rx irq */
  92. rt_hw_interrupt_umask(uart->irq);
  93. break;
  94. }
  95. return RT_EOK;
  96. }
  97. int printkc(char c)
  98. {
  99. int base = 0xf9e09000;
  100. while (!(UART_LSR_REG(base) & 0x20));
  101. UART_THR_REG(base) = c;
  102. return 1;
  103. }
  104. static int am33xx_putc(struct rt_serial_device *serial, char c)
  105. {
  106. struct am33xx_uart* uart;
  107. RT_ASSERT(serial != RT_NULL);
  108. uart = (struct am33xx_uart *)serial->parent.user_data;
  109. while (!(UART_LSR_REG(uart->base) & 0x20));
  110. UART_THR_REG(uart->base) = c;
  111. return 1;
  112. }
  113. static int am33xx_getc(struct rt_serial_device *serial)
  114. {
  115. int ch;
  116. struct am33xx_uart* uart;
  117. RT_ASSERT(serial != RT_NULL);
  118. uart = (struct am33xx_uart *)serial->parent.user_data;
  119. ch = -1;
  120. if (UART_LSR_REG(uart->base) & 0x01)
  121. {
  122. ch = UART_RHR_REG(uart->base) & 0xff;
  123. }
  124. return ch;
  125. }
  126. static const struct rt_uart_ops am33xx_uart_ops =
  127. {
  128. am33xx_configure,
  129. am33xx_control,
  130. am33xx_putc,
  131. am33xx_getc,
  132. };
  133. /* UART device driver structure */
  134. #ifdef RT_USING_UART0
  135. struct am33xx_uart uart0 =
  136. {
  137. UART0_BASE,
  138. UART0_INT,
  139. };
  140. struct rt_serial_device serial0;
  141. #endif
  142. #ifdef RT_USING_UART1
  143. struct am33xx_uart uart1 =
  144. {
  145. UART1_BASE,
  146. UART1_INT,
  147. };
  148. struct rt_serial_device serial1;
  149. #endif
  150. #ifdef RT_USING_UART2
  151. struct am33xx_uart uart2 =
  152. {
  153. UART2_BASE,
  154. UART2_INT,
  155. };
  156. struct rt_serial_device serial2;
  157. #endif
  158. #ifdef RT_USING_UART3
  159. struct am33xx_uart uart3 =
  160. {
  161. UART3_BASE,
  162. UART3_INT,
  163. };
  164. struct rt_serial_device serial3;
  165. #endif
  166. #ifdef RT_USING_UART4
  167. struct am33xx_uart uart4 =
  168. {
  169. UART4_BASE,
  170. UART4_INT,
  171. };
  172. struct rt_serial_device serial4;
  173. #endif
  174. #ifdef RT_USING_UART5
  175. struct am33xx_uart uart5 =
  176. {
  177. UART5_BASE,
  178. UART5_INT,
  179. };
  180. struct rt_serial_device serial5;
  181. #endif
  182. #define write_reg(base, value) *(int*)(base) = value
  183. #define read_reg(base) *(int*)(base)
  184. #define PRM_PER_INTRANSLATION (1 << 20)
  185. #define PRM_PER_POWSTATEOFF (0)
  186. #define PRM_PER_PERMEMSTATEOFF (0)
  187. static void poweron_per_domain(void)
  188. {
  189. unsigned long prcm_base;
  190. unsigned long prm_state;
  191. prcm_base = AM33XX_PRCM_REGS;
  192. /* wait for ongoing translations */
  193. for (prm_state = PRM_PER_PWRSTST_REG(prcm_base);
  194. prm_state & PRM_PER_INTRANSLATION;
  195. prm_state = PRM_PER_PWRSTST_REG(prcm_base))
  196. ;
  197. /* check power state */
  198. if ((prm_state & 0x03) == PRM_PER_POWSTATEOFF)
  199. /* power on PER domain */
  200. PRM_PER_PWRSTCTRL_REG(prcm_base) |= 0x3;
  201. /* check per mem state */
  202. if ((prm_state & 0x03) == PRM_PER_PERMEMSTATEOFF)
  203. /* power on PER domain */
  204. PRM_PER_PWRSTCTRL_REG(prcm_base) |= 0x3 << 25;
  205. while (PRM_PER_PWRSTST_REG(prcm_base) & PRM_PER_INTRANSLATION)
  206. ;
  207. }
  208. static void start_uart_clk(void)
  209. {
  210. unsigned long prcm_base;
  211. prcm_base = AM33XX_PRCM_REGS;
  212. #if defined(RT_USING_UART1) || defined(RT_USING_UART2) || defined(RT_USING_UART3) || defined(RT_USING_UART4) || defined(RT_USING_UART5)
  213. /* software forced wakeup */
  214. CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) |= 0x2;
  215. /* Waiting for the L4LS clock */
  216. while (!(CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) & (1<<8)))
  217. ;
  218. /* enable uart1 */
  219. #ifdef RT_USING_UART1
  220. CM_PER_UART1_CLKCTRL_REG(prcm_base) |= 0x2;
  221. /* wait for uart1 clk */
  222. while ((CM_PER_UART1_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  223. ;
  224. #endif
  225. #ifdef RT_USING_UART2
  226. CM_PER_UART2_CLKCTRL_REG(prcm_base) |= 0x2;
  227. /* wait for uart2 clk */
  228. while ((CM_PER_UART2_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  229. ;
  230. #endif
  231. #ifdef RT_USING_UART3
  232. CM_PER_UART3_CLKCTRL_REG(prcm_base) |= 0x2;
  233. /* wait for uart3 clk */
  234. while ((CM_PER_UART3_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  235. ;
  236. #endif
  237. #ifdef RT_USING_UART4
  238. CM_PER_UART4_CLKCTRL_REG(prcm_base) |= 0x2;
  239. /* wait for uart4 clk */
  240. while ((CM_PER_UART4_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  241. ;
  242. #endif
  243. #ifdef RT_USING_UART5
  244. CM_PER_UART5_CLKCTRL_REG(prcm_base) |= 0x2;
  245. /* wait for uart5 clk */
  246. while ((CM_PER_UART5_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  247. ;
  248. #endif
  249. /* Waiting for the L4LS UART clock */
  250. while (!(CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) & (1<<10)))
  251. ;
  252. #endif
  253. #ifdef RT_USING_UART0
  254. /* software forced wakeup */
  255. CM_WKUP_CLKSTCTRL_REG(prcm_base) |= 0x2;
  256. /* Waiting for the L4_WKUP clock */
  257. while (!(CM_WKUP_CLKSTCTRL_REG(prcm_base) & (1<<2)))
  258. ;
  259. /* enable uart0 */
  260. CM_WKUP_UART0_CLKCTRL_REG(prcm_base) |= 0x2;
  261. /* wait for uart0 clk */
  262. while ((CM_WKUP_UART0_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  263. ;
  264. /* Waiting for the L4_WKUP UART0 clock */
  265. while (!(CM_WKUP_CLKSTCTRL_REG(prcm_base) & (1<<12)))
  266. ;
  267. #endif
  268. }
  269. static void config_pinmux(void)
  270. {
  271. unsigned long ctlm_base;
  272. ctlm_base = AM33XX_CTLM_REGS;
  273. /* make sure the pin mux is OK for uart */
  274. #ifdef RT_USING_UART0
  275. REG32(ctlm_base + 0x800 + 0x170) = 0x20;
  276. REG32(ctlm_base + 0x800 + 0x174) = 0x00;
  277. #endif
  278. #ifdef RT_USING_UART1
  279. REG32(ctlm_base + 0x800 + 0x180) = 0x20;
  280. REG32(ctlm_base + 0x800 + 0x184) = 0x00;
  281. #endif
  282. #ifdef RT_USING_UART2
  283. REG32(ctlm_base + 0x800 + 0x150) = 0x20;
  284. REG32(ctlm_base + 0x800 + 0x154) = 0x00;
  285. #endif
  286. #ifdef RT_USING_UART3
  287. REG32(ctlm_base + 0x800 + 0x164) = 0x01;
  288. #endif
  289. #ifdef RT_USING_UART4
  290. REG32(ctlm_base + 0x800 + 0x070) = 0x26;
  291. REG32(ctlm_base + 0x800 + 0x074) = 0x06;
  292. #endif
  293. #ifdef RT_USING_UART5
  294. REG32(ctlm_base + 0x800 + 0x0C4) = 0x24;
  295. REG32(ctlm_base + 0x800 + 0x0C0) = 0x04;
  296. #endif
  297. }
  298. int rt_hw_serial_init(void)
  299. {
  300. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  301. poweron_per_domain();
  302. start_uart_clk();
  303. config_pinmux();
  304. #ifdef RT_USING_UART0
  305. serial0.ops = &am33xx_uart_ops;
  306. serial0.config = config;
  307. /* enable RX interrupt */
  308. UART_IER_REG(uart0.base) = 0x01;
  309. /* install ISR */
  310. rt_hw_interrupt_install(uart0.irq, am33xx_uart_isr, &serial0, "uart0");
  311. rt_hw_interrupt_control(uart0.irq, 0, 0);
  312. rt_hw_interrupt_mask(uart0.irq);
  313. /* register UART0 device */
  314. rt_hw_serial_register(&serial0, "uart0",
  315. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  316. &uart0);
  317. #endif
  318. #ifdef RT_USING_UART1
  319. serial1.ops = &am33xx_uart_ops;
  320. serial1.config = config;
  321. /* enable RX interrupt */
  322. UART_IER_REG(uart1.base) = 0x01;
  323. /* install ISR */
  324. rt_hw_interrupt_install(uart1.irq, am33xx_uart_isr, &serial1, "uart1");
  325. rt_hw_interrupt_control(uart1.irq, 0, 0);
  326. rt_hw_interrupt_mask(uart1.irq);
  327. /* register UART0 device */
  328. rt_hw_serial_register(&serial1, "uart1",
  329. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  330. &uart1);
  331. #endif
  332. #ifdef RT_USING_UART2
  333. serial2.ops = &am33xx_uart_ops;
  334. serial2.config = config;
  335. /* enable RX interrupt */
  336. UART_IER_REG(uart2.base) = 0x01;
  337. /* install ISR */
  338. rt_hw_interrupt_install(uart2.irq, am33xx_uart_isr, &serial2, "uart2");
  339. rt_hw_interrupt_control(uart2.irq, 0, 0);
  340. rt_hw_interrupt_mask(uart2.irq);
  341. /* register UART2 device */
  342. rt_hw_serial_register(&serial2, "uart2",
  343. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  344. &uart2);
  345. #endif
  346. #ifdef RT_USING_UART3
  347. serial3.ops = &am33xx_uart_ops;
  348. serial3.config = config;
  349. /* enable RX interrupt */
  350. UART_IER_REG(uart3.base) = 0x01;
  351. /* install ISR */
  352. rt_hw_interrupt_install(uart3.irq, am33xx_uart_isr, &serial3, "uart3");
  353. rt_hw_interrupt_control(uart3.irq, 0, 0);
  354. rt_hw_interrupt_mask(uart3.irq);
  355. /* register UART3 device */
  356. rt_hw_serial_register(&serial3, "uart3",
  357. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  358. &uart3);
  359. #endif
  360. #ifdef RT_USING_UART4
  361. /* use 9600bps for GDB stub. */
  362. config.baud_rate = BAUD_RATE_9600;
  363. serial4.ops = &am33xx_uart_ops;
  364. serial4.config = config;
  365. /* enable RX interrupt */
  366. UART_IER_REG(uart4.base) = 0x00;
  367. /* install ISR */
  368. rt_hw_interrupt_install(uart4.irq, am33xx_uart_isr, &serial4, "uart4");
  369. rt_hw_interrupt_control(uart4.irq, 0, 0);
  370. rt_hw_interrupt_mask(uart4.irq);
  371. /* register UART4 device */
  372. rt_hw_serial_register(&serial4, "uart4",
  373. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  374. &uart4);
  375. #endif
  376. #ifdef RT_USING_UART5
  377. config.baud_rate = BAUD_RATE_115200;
  378. serial5.ops = &am33xx_uart_ops;
  379. serial5.config = config;
  380. /* enable RX interrupt */
  381. UART_IER_REG(uart5.base) = 0x01;
  382. /* install ISR */
  383. rt_hw_interrupt_install(uart5.irq, am33xx_uart_isr, &serial5, "uart5");
  384. rt_hw_interrupt_control(uart5.irq, 0, 0);
  385. rt_hw_interrupt_mask(uart5.irq);
  386. /* register UART4 device */
  387. rt_hw_serial_register(&serial5, "uart5",
  388. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  389. &uart5);
  390. #endif
  391. return 0;
  392. }
  393. INIT_BOARD_EXPORT(rt_hw_serial_init);