serial.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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);
  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 serial_ringbuffer uart0_int_rx;
  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 serial_ringbuffer uart1_int_rx;
  144. struct am33xx_uart uart1 =
  145. {
  146. UART1_BASE,
  147. UART1_INT,
  148. };
  149. struct rt_serial_device serial1;
  150. #endif
  151. #ifdef RT_USING_UART2
  152. struct serial_ringbuffer uart2_int_rx;
  153. struct am33xx_uart uart2 =
  154. {
  155. UART2_BASE,
  156. UART2_INT,
  157. };
  158. struct rt_serial_device serial2;
  159. #endif
  160. #ifdef RT_USING_UART3
  161. struct serial_ringbuffer uart3_int_rx;
  162. struct am33xx_uart uart3 =
  163. {
  164. UART3_BASE,
  165. UART3_INT,
  166. };
  167. struct rt_serial_device serial3;
  168. #endif
  169. #ifdef RT_USING_UART4
  170. struct serial_ringbuffer uart4_int_rx;
  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 serial_ringbuffer uart5_int_rx;
  180. struct am33xx_uart uart5 =
  181. {
  182. UART5_BASE,
  183. UART5_INT,
  184. };
  185. struct rt_serial_device serial5;
  186. #endif
  187. #define write_reg(base, value) *(int*)(base) = value
  188. #define read_reg(base) *(int*)(base)
  189. #define PRM_PER_INTRANSLATION (1 << 20)
  190. #define PRM_PER_POWSTATEOFF (0)
  191. #define PRM_PER_PERMEMSTATEOFF (0)
  192. static void poweron_per_domain(void)
  193. {
  194. unsigned long prcm_base;
  195. unsigned long prm_state;
  196. prcm_base = AM33XX_PRCM_REGS;
  197. /* wait for ongoing translations */
  198. for (prm_state = PRM_PER_PWRSTST_REG(prcm_base);
  199. prm_state & PRM_PER_INTRANSLATION;
  200. prm_state = PRM_PER_PWRSTST_REG(prcm_base))
  201. ;
  202. /* check power state */
  203. if ((prm_state & 0x03) == PRM_PER_POWSTATEOFF)
  204. /* power on PER domain */
  205. PRM_PER_PWRSTCTRL_REG(prcm_base) |= 0x3;
  206. /* check per mem state */
  207. if ((prm_state & 0x03) == PRM_PER_PERMEMSTATEOFF)
  208. /* power on PER domain */
  209. PRM_PER_PWRSTCTRL_REG(prcm_base) |= 0x3 << 25;
  210. while (PRM_PER_PWRSTST_REG(prcm_base) & PRM_PER_INTRANSLATION)
  211. ;
  212. }
  213. static void start_uart_clk(void)
  214. {
  215. unsigned long prcm_base;
  216. prcm_base = AM33XX_PRCM_REGS;
  217. /* software forced wakeup */
  218. CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) |= 0x2;
  219. /* Waiting for the L4LS clock */
  220. while (!(CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) & (1<<8)))
  221. ;
  222. /* enable uart1 */
  223. #ifdef RT_USING_UART1
  224. CM_PER_UART1_CLKCTRL_REG(prcm_base) |= 0x2;
  225. /* wait for uart1 clk */
  226. while ((CM_PER_UART1_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  227. ;
  228. #endif
  229. #ifdef RT_USING_UART2
  230. CM_PER_UART2_CLKCTRL_REG(prcm_base) |= 0x2;
  231. /* wait for uart2 clk */
  232. while ((CM_PER_UART2_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  233. ;
  234. #endif
  235. #ifdef RT_USING_UART3
  236. CM_PER_UART3_CLKCTRL_REG(prcm_base) |= 0x2;
  237. /* wait for uart3 clk */
  238. while ((CM_PER_UART3_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  239. ;
  240. #endif
  241. #ifdef RT_USING_UART4
  242. CM_PER_UART4_CLKCTRL_REG(prcm_base) |= 0x2;
  243. /* wait for uart4 clk */
  244. while ((CM_PER_UART4_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  245. ;
  246. #endif
  247. #ifdef RT_USING_UART5
  248. CM_PER_UART5_CLKCTRL_REG(prcm_base) |= 0x2;
  249. /* wait for uart5 clk */
  250. while ((CM_PER_UART5_CLKCTRL_REG(prcm_base) & (0x3<<16)) != 0)
  251. ;
  252. #endif
  253. /* Waiting for the L4LS UART clock */
  254. while (!(CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) & (1<<10)))
  255. ;
  256. }
  257. static void config_pinmux(void)
  258. {
  259. unsigned long ctlm_base;
  260. ctlm_base = AM33XX_CTLM_REGS;
  261. /* make sure the pin mux is OK for uart */
  262. #ifdef RT_USING_UART1
  263. REG32(ctlm_base + 0x800 + 0x180) = 0x20;
  264. REG32(ctlm_base + 0x800 + 0x184) = 0x00;
  265. #endif
  266. #ifdef RT_USING_UART2
  267. REG32(ctlm_base + 0x800 + 0x150) = 0x20;
  268. REG32(ctlm_base + 0x800 + 0x154) = 0x00;
  269. #endif
  270. #ifdef RT_USING_UART3
  271. REG32(ctlm_base + 0x800 + 0x164) = 0x01;
  272. #endif
  273. #ifdef RT_USING_UART4
  274. REG32(ctlm_base + 0x800 + 0x070) = 0x26;
  275. REG32(ctlm_base + 0x800 + 0x074) = 0x06;
  276. #endif
  277. #ifdef RT_USING_UART5
  278. REG32(ctlm_base + 0x800 + 0x0C4) = 0x24;
  279. REG32(ctlm_base + 0x800 + 0x0C0) = 0x04;
  280. #endif
  281. }
  282. int rt_hw_serial_init(void)
  283. {
  284. struct serial_configure config;
  285. poweron_per_domain();
  286. start_uart_clk();
  287. config_pinmux();
  288. #ifdef RT_USING_UART0
  289. config.baud_rate = BAUD_RATE_115200;
  290. config.bit_order = BIT_ORDER_LSB;
  291. config.data_bits = DATA_BITS_8;
  292. config.parity = PARITY_NONE;
  293. config.stop_bits = STOP_BITS_1;
  294. config.invert = NRZ_NORMAL;
  295. serial0.ops = &am33xx_uart_ops;
  296. serial0.int_rx = &uart0_int_rx;
  297. serial0.config = config;
  298. /* enable RX interrupt */
  299. UART_IER_REG(uart0.base) = 0x01;
  300. /* install ISR */
  301. rt_hw_interrupt_install(uart0.irq, am33xx_uart_isr, &serial0, "uart0");
  302. rt_hw_interrupt_control(uart0.irq, 0, 0);
  303. rt_hw_interrupt_mask(uart0.irq);
  304. /* register UART0 device */
  305. rt_hw_serial_register(&serial0, "uart0",
  306. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  307. &uart0);
  308. #endif
  309. #ifdef RT_USING_UART1
  310. config.baud_rate = BAUD_RATE_115200;
  311. config.bit_order = BIT_ORDER_LSB;
  312. config.data_bits = DATA_BITS_8;
  313. config.parity = PARITY_NONE;
  314. config.stop_bits = STOP_BITS_1;
  315. config.invert = NRZ_NORMAL;
  316. serial1.ops = &am33xx_uart_ops;
  317. serial1.int_rx = &uart1_int_rx;
  318. serial1.config = config;
  319. /* enable RX interrupt */
  320. UART_IER_REG(uart1.base) = 0x01;
  321. /* install ISR */
  322. rt_hw_interrupt_install(uart1.irq, am33xx_uart_isr, &serial1, "uart1");
  323. rt_hw_interrupt_control(uart1.irq, 0, 0);
  324. rt_hw_interrupt_mask(uart1.irq);
  325. /* register UART0 device */
  326. rt_hw_serial_register(&serial1, "uart1",
  327. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  328. &uart1);
  329. #endif
  330. #ifdef RT_USING_UART2
  331. config.baud_rate = BAUD_RATE_115200;
  332. config.bit_order = BIT_ORDER_LSB;
  333. config.data_bits = DATA_BITS_8;
  334. config.parity = PARITY_NONE;
  335. config.stop_bits = STOP_BITS_1;
  336. config.invert = NRZ_NORMAL;
  337. serial2.ops = &am33xx_uart_ops;
  338. serial2.int_rx = &uart2_int_rx;
  339. serial2.config = config;
  340. /* enable RX interrupt */
  341. UART_IER_REG(uart2.base) = 0x01;
  342. /* install ISR */
  343. rt_hw_interrupt_install(uart2.irq, am33xx_uart_isr, &serial2, "uart2");
  344. rt_hw_interrupt_control(uart2.irq, 0, 0);
  345. rt_hw_interrupt_mask(uart2.irq);
  346. /* register UART2 device */
  347. rt_hw_serial_register(&serial2, "uart2",
  348. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  349. &uart2);
  350. #endif
  351. #ifdef RT_USING_UART3
  352. config.baud_rate = BAUD_RATE_115200;
  353. config.bit_order = BIT_ORDER_LSB;
  354. config.data_bits = DATA_BITS_8;
  355. config.parity = PARITY_NONE;
  356. config.stop_bits = STOP_BITS_1;
  357. config.invert = NRZ_NORMAL;
  358. serial3.ops = &am33xx_uart_ops;
  359. serial3.int_rx = &uart_3_int_rx;
  360. serial3.config = config;
  361. /* enable RX interrupt */
  362. UART_IER_REG(uart3.base) = 0x01;
  363. /* install ISR */
  364. rt_hw_interrupt_install(uart3.irq, am33xx_uart_isr, &serial3, "uart3");
  365. rt_hw_interrupt_control(uart3.irq, 0, 0);
  366. rt_hw_interrupt_mask(uart3.irq);
  367. /* register UART3 device */
  368. rt_hw_serial_register(&serial3, "uart3",
  369. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  370. &uart3);
  371. #endif
  372. #ifdef RT_USING_UART4
  373. config.baud_rate = BAUD_RATE_115200;
  374. config.bit_order = BIT_ORDER_LSB;
  375. config.data_bits = DATA_BITS_8;
  376. config.parity = PARITY_NONE;
  377. config.stop_bits = STOP_BITS_1;
  378. config.invert = NRZ_NORMAL;
  379. serial4.ops = &am33xx_uart_ops;
  380. serial4.int_rx = &uart4_int_rx;
  381. serial4.config = config;
  382. /* enable RX interrupt */
  383. UART_IER_REG(uart4.base) = 0x01;
  384. /* install ISR */
  385. rt_hw_interrupt_install(uart4.irq, am33xx_uart_isr, &serial4, "uart4");
  386. rt_hw_interrupt_control(uart4.irq, 0, 0);
  387. rt_hw_interrupt_mask(uart4.irq);
  388. /* register UART4 device */
  389. rt_hw_serial_register(&serial4, "uart4",
  390. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  391. &uart4);
  392. #endif
  393. #ifdef RT_USING_UART5
  394. config.baud_rate = BAUD_RATE_115200;
  395. config.bit_order = BIT_ORDER_LSB;
  396. config.data_bits = DATA_BITS_8;
  397. config.parity = PARITY_NONE;
  398. config.stop_bits = STOP_BITS_1;
  399. config.invert = NRZ_NORMAL;
  400. serial5.ops = &am33xx_uart_ops;
  401. serial5.int_rx = &uart5_int_rx;
  402. serial5.config = config;
  403. /* enable RX interrupt */
  404. UART_IER_REG(uart5.base) = 0x01;
  405. /* install ISR */
  406. rt_hw_interrupt_install(uart5.irq, am33xx_uart_isr, &serial5, "uart5");
  407. rt_hw_interrupt_control(uart5.irq, 0, 0);
  408. rt_hw_interrupt_mask(uart5.irq);
  409. /* register UART4 device */
  410. rt_hw_serial_register(&serial5, "uart5",
  411. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  412. &uart5);
  413. #endif
  414. return 0;
  415. }
  416. INIT_BOARD_EXPORT(rt_hw_serial_init);