serial.c 12 KB

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