serial.c 10 KB

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