uart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. /* software forced wakeup */
  213. CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) |= 0x2;
  214. /* Waiting for the L4LS clock */
  215. while (!(CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) & (1<<8)))
  216. ;
  217. /* enable uart1 */
  218. #ifdef RT_USING_UART1
  219. CM_PER_UART1_CLKCTRL_REG(prcm_base) |= 0x2;
  220. /* wait for uart1 clk */
  221. while ((CM_PER_UART1_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  222. ;
  223. #endif
  224. #ifdef RT_USING_UART2
  225. CM_PER_UART2_CLKCTRL_REG(prcm_base) |= 0x2;
  226. /* wait for uart2 clk */
  227. while ((CM_PER_UART2_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  228. ;
  229. #endif
  230. #ifdef RT_USING_UART3
  231. CM_PER_UART3_CLKCTRL_REG(prcm_base) |= 0x2;
  232. /* wait for uart3 clk */
  233. while ((CM_PER_UART3_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  234. ;
  235. #endif
  236. #ifdef RT_USING_UART4
  237. CM_PER_UART4_CLKCTRL_REG(prcm_base) |= 0x2;
  238. /* wait for uart4 clk */
  239. while ((CM_PER_UART4_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  240. ;
  241. #endif
  242. #ifdef RT_USING_UART5
  243. CM_PER_UART5_CLKCTRL_REG(prcm_base) |= 0x2;
  244. /* wait for uart5 clk */
  245. while ((CM_PER_UART5_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  246. ;
  247. #endif
  248. /* Waiting for the L4LS UART clock */
  249. while (!(CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) & (1<<10)))
  250. ;
  251. }
  252. static void config_pinmux(void)
  253. {
  254. unsigned long ctlm_base;
  255. ctlm_base = AM33XX_CTLM_REGS;
  256. /* make sure the pin mux is OK for uart */
  257. #ifdef RT_USING_UART1
  258. REG32(ctlm_base + 0x800 + 0x180) = 0x20;
  259. REG32(ctlm_base + 0x800 + 0x184) = 0x00;
  260. #endif
  261. #ifdef RT_USING_UART2
  262. REG32(ctlm_base + 0x800 + 0x150) = 0x20;
  263. REG32(ctlm_base + 0x800 + 0x154) = 0x00;
  264. #endif
  265. #ifdef RT_USING_UART3
  266. REG32(ctlm_base + 0x800 + 0x164) = 0x01;
  267. #endif
  268. #ifdef RT_USING_UART4
  269. REG32(ctlm_base + 0x800 + 0x070) = 0x26;
  270. REG32(ctlm_base + 0x800 + 0x074) = 0x06;
  271. #endif
  272. #ifdef RT_USING_UART5
  273. REG32(ctlm_base + 0x800 + 0x0C4) = 0x24;
  274. REG32(ctlm_base + 0x800 + 0x0C0) = 0x04;
  275. #endif
  276. }
  277. int rt_hw_serial_init(void)
  278. {
  279. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  280. poweron_per_domain();
  281. start_uart_clk();
  282. config_pinmux();
  283. #ifdef RT_USING_UART0
  284. serial0.ops = &am33xx_uart_ops;
  285. serial0.config = config;
  286. /* enable RX interrupt */
  287. UART_IER_REG(uart0.base) = 0x01;
  288. /* install ISR */
  289. rt_hw_interrupt_install(uart0.irq, am33xx_uart_isr, &serial0, "uart0");
  290. rt_hw_interrupt_control(uart0.irq, 0, 0);
  291. rt_hw_interrupt_mask(uart0.irq);
  292. /* register UART0 device */
  293. rt_hw_serial_register(&serial0, "uart0",
  294. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  295. &uart0);
  296. #endif
  297. #ifdef RT_USING_UART1
  298. serial1.ops = &am33xx_uart_ops;
  299. serial1.config = config;
  300. /* enable RX interrupt */
  301. UART_IER_REG(uart1.base) = 0x01;
  302. /* install ISR */
  303. rt_hw_interrupt_install(uart1.irq, am33xx_uart_isr, &serial1, "uart1");
  304. rt_hw_interrupt_control(uart1.irq, 0, 0);
  305. rt_hw_interrupt_mask(uart1.irq);
  306. /* register UART0 device */
  307. rt_hw_serial_register(&serial1, "uart1",
  308. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  309. &uart1);
  310. #endif
  311. #ifdef RT_USING_UART2
  312. serial2.ops = &am33xx_uart_ops;
  313. serial2.config = config;
  314. /* enable RX interrupt */
  315. UART_IER_REG(uart2.base) = 0x01;
  316. /* install ISR */
  317. rt_hw_interrupt_install(uart2.irq, am33xx_uart_isr, &serial2, "uart2");
  318. rt_hw_interrupt_control(uart2.irq, 0, 0);
  319. rt_hw_interrupt_mask(uart2.irq);
  320. /* register UART2 device */
  321. rt_hw_serial_register(&serial2, "uart2",
  322. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  323. &uart2);
  324. #endif
  325. #ifdef RT_USING_UART3
  326. serial3.ops = &am33xx_uart_ops;
  327. serial3.config = config;
  328. /* enable RX interrupt */
  329. UART_IER_REG(uart3.base) = 0x01;
  330. /* install ISR */
  331. rt_hw_interrupt_install(uart3.irq, am33xx_uart_isr, &serial3, "uart3");
  332. rt_hw_interrupt_control(uart3.irq, 0, 0);
  333. rt_hw_interrupt_mask(uart3.irq);
  334. /* register UART3 device */
  335. rt_hw_serial_register(&serial3, "uart3",
  336. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  337. &uart3);
  338. #endif
  339. #ifdef RT_USING_UART4
  340. /* use 9600bps for GDB stub. */
  341. config.baud_rate = BAUD_RATE_9600;
  342. serial4.ops = &am33xx_uart_ops;
  343. serial4.config = config;
  344. /* enable RX interrupt */
  345. UART_IER_REG(uart4.base) = 0x00;
  346. /* install ISR */
  347. rt_hw_interrupt_install(uart4.irq, am33xx_uart_isr, &serial4, "uart4");
  348. rt_hw_interrupt_control(uart4.irq, 0, 0);
  349. rt_hw_interrupt_mask(uart4.irq);
  350. /* register UART4 device */
  351. rt_hw_serial_register(&serial4, "uart4",
  352. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  353. &uart4);
  354. #endif
  355. #ifdef RT_USING_UART5
  356. config.baud_rate = BAUD_RATE_115200;
  357. serial5.ops = &am33xx_uart_ops;
  358. serial5.config = config;
  359. /* enable RX interrupt */
  360. UART_IER_REG(uart5.base) = 0x01;
  361. /* install ISR */
  362. rt_hw_interrupt_install(uart5.irq, am33xx_uart_isr, &serial5, "uart5");
  363. rt_hw_interrupt_control(uart5.irq, 0, 0);
  364. rt_hw_interrupt_mask(uart5.irq);
  365. /* register UART4 device */
  366. rt_hw_serial_register(&serial5, "uart5",
  367. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  368. &uart5);
  369. #endif
  370. return 0;
  371. }
  372. INIT_BOARD_EXPORT(rt_hw_serial_init);