uart.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * serial.c UART driver
  3. *
  4. * COPYRIGHT (C) 2013, Shanghai Real-Thread Technology Co., Ltd
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * Change Logs:
  23. * Date Author Notes
  24. * 2013-03-30 Bernard the first verion
  25. */
  26. #include <rthw.h>
  27. #include <rtdevice.h>
  28. #include "board.h"
  29. #include "gic.h"
  30. #include "cp15.h"
  31. #include "uart_hw.h"
  32. #define Zynq7000_UART_INT_DISABLE(UART) \
  33. (UART->IER &= ~(UART_IXR_RXOVR | UART_IXR_RXFULL))
  34. #define Zynq7000_UART_INT_ENABLE(UART) \
  35. (UART->IER |= (UART_IXR_RXOVR | UART_IXR_RXFULL))
  36. #define Zynq7000_UART_SENDCHAR(UART, ch) \
  37. do { \
  38. while ((UART->SR) & UART_SR_TXFULL); \
  39. UART->FIFO = ch; \
  40. } while(0)
  41. #define Zynq7000_UART_GETCHAR(UART, ch) \
  42. do { \
  43. if (UART->ISR & UART_SR_RXOVR) \
  44. { \
  45. ch = UART->FIFO & 0xff; \
  46. UART->ISR = (UART_IXR_RXOVR | UART_IXR_RXFULL); \
  47. } \
  48. } while(0)
  49. static void UartEnable(UART_Registers* uart)
  50. {
  51. uint32_t tmp = uart->CR;
  52. tmp &= ~UART_CR_EN_DIS_MASK;
  53. tmp |= (UART_CR_TX_EN | UART_CR_RX_EN);
  54. uart->CR = tmp;
  55. }
  56. static void UartDisable(UART_Registers* uart)
  57. {
  58. uint32_t tmp = uart->CR;
  59. tmp &= ~UART_CR_EN_DIS_MASK;
  60. tmp |= (UART_CR_TX_DIS | UART_CR_RX_DIS);
  61. uart->CR = tmp;
  62. }
  63. static void UartResetTXRXLogic(UART_Registers* uart)
  64. {
  65. uart->CR |= 0x03;
  66. while (uart->CR & 0x03)
  67. ;
  68. }
  69. /* PULLUP | LVCMOS18 | Fast CMOS | UART */
  70. #define RX_MIO_PIN_MODE ((0x1UL << 12) | (0x1UL << 9) | (0x01UL << 8) | (0x7UL << 5))
  71. #define TX_MIO_PIN_MODE ( (0x1UL << 9) | (0x01UL << 8) | (0x7UL << 5))
  72. struct hw_uart_device
  73. {
  74. UART_Registers * uart;
  75. rt_uint32_t irqno;
  76. /* MIO pin mode address */
  77. rt_uint32_t *rxmio;
  78. rt_uint32_t *txmio;
  79. };
  80. /* RT-Thread UART interface */
  81. static void rt_hw_uart_isr(int irqno, void *param)
  82. {
  83. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  84. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  85. }
  86. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  87. {
  88. uint32_t mr;
  89. struct hw_uart_device *pdev = serial->parent.user_data;
  90. UART_Registers *uart = pdev->uart;
  91. /* unlock SLCR */
  92. __REG32(Zynq7000_SLCR_BASE+Zynq7000_SLCR_UNLOCK) = 0xDF0D;
  93. /* no loopback */
  94. __REG32(Zynq7000_SLCR_BASE+Zynq7000_SLCR_MIO_LOOPBACK) &= ~(1 << 1);
  95. if (uart == (void*)Zynq7000_UART0_BASE)
  96. {
  97. /* enable the coresponding AMBA Peripheral Clock */
  98. __REG32(Zynq7000_SLCR_BASE+Zynq7000_SLCR_APER_CLK_CTRL) |= 1 << 20;
  99. /* enable uart clock. Divider 0x14 gives 50MHZ ref clock on IO PLL input. */
  100. __REG32(Zynq7000_SLCR_BASE+Zynq7000_SLCR_UART_CLK_CTRL) |= (0x14 << 8) | 0x01;
  101. /* deassert the AMBA clock and software reset */
  102. __REG32(Zynq7000_SLCR_BASE+Zynq7000_SLCR_UART_RST_CTRL) &= ~((0x01 << 2)|(0x01 << 0));
  103. }
  104. else if (uart == (void*)Zynq7000_UART1_BASE)
  105. {
  106. __REG32(Zynq7000_SLCR_BASE+Zynq7000_SLCR_APER_CLK_CTRL) |= 1 << 21;
  107. __REG32(Zynq7000_SLCR_BASE+Zynq7000_SLCR_UART_CLK_CTRL) |= (0x14 << 8) | 0x02;
  108. __REG32(Zynq7000_SLCR_BASE+Zynq7000_SLCR_UART_RST_CTRL) &= ~((0x01 << 3)|(0x01 << 1));
  109. }
  110. else
  111. return -RT_ERROR;
  112. UartDisable(uart);
  113. UartResetTXRXLogic(uart);
  114. UartEnable(uart);
  115. mr = uart->MR & ~(UART_MR_CHARLEN_MASK |
  116. UART_MR_STOPMODE_MASK |
  117. UART_MR_PARITY_MASK);
  118. if (cfg->stop_bits == STOP_BITS_2)
  119. mr |= UART_MR_STOPMODE_2_BIT;
  120. else if (cfg->stop_bits == STOP_BITS_1)
  121. mr |= UART_MR_STOPMODE_1_BIT;
  122. else
  123. return -RT_ERROR;
  124. if (cfg->parity == PARITY_EVEN)
  125. mr |= UART_MR_PARITY_EVEN;
  126. else if (cfg->parity == PARITY_ODD)
  127. mr |= UART_MR_PARITY_ODD;
  128. else if (cfg->parity == PARITY_NONE)
  129. mr |= UART_MR_PARITY_NONE;
  130. else
  131. return -1;
  132. if (cfg->data_bits == DATA_BITS_8)
  133. mr |= UART_MR_CHARLEN_8_BIT;
  134. else if (cfg->data_bits == DATA_BITS_7)
  135. mr |= UART_MR_CHARLEN_7_BIT;
  136. else if (cfg->data_bits == DATA_BITS_6)
  137. mr |= UART_MR_CHARLEN_6_BIT;
  138. else
  139. return -RT_ERROR;
  140. uart->MR = mr;
  141. uart->TXWM = 8;
  142. uart->RXWM = 1;
  143. if (cfg->baud_rate == BAUD_RATE_115200)
  144. {
  145. uart->BAUDGEN = UART_BAUDGEN_115200;
  146. uart->BAUDDIV = UART_BAUDDIV_115200;
  147. }
  148. else
  149. {
  150. rt_kprintf("baudrate %d not implemented yet\n", cfg->baud_rate);
  151. }
  152. /* disable all interrupts */
  153. uart->IDR = UART_IXR_MASK;
  154. /* configure the pin */
  155. *(pdev->txmio) = TX_MIO_PIN_MODE;
  156. *(pdev->rxmio) = RX_MIO_PIN_MODE;
  157. return RT_EOK;
  158. }
  159. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  160. {
  161. struct hw_uart_device *pdev;
  162. RT_ASSERT(serial != RT_NULL);
  163. pdev = serial->parent.user_data;
  164. switch (cmd)
  165. {
  166. case RT_DEVICE_CTRL_CLR_INT:
  167. /* disable rx irq */
  168. Zynq7000_UART_INT_DISABLE(pdev->uart);
  169. break;
  170. case RT_DEVICE_CTRL_SET_INT:
  171. /* enable rx irq */
  172. Zynq7000_UART_INT_ENABLE(pdev->uart);
  173. rt_hw_interrupt_install(pdev->irqno, rt_hw_uart_isr, serial, "uart");
  174. /* set the interrupt to this cpu */
  175. arm_gic_set_cpu(0, pdev->irqno, 1 << rt_cpu_get_smp_id());
  176. rt_hw_interrupt_umask(pdev->irqno);
  177. break;
  178. }
  179. return RT_EOK;
  180. }
  181. static int uart_putc(struct rt_serial_device *serial, char c)
  182. {
  183. struct hw_uart_device *dev;
  184. RT_ASSERT(serial != RT_NULL);
  185. dev = (struct hw_uart_device *)serial->parent.user_data;
  186. Zynq7000_UART_SENDCHAR(dev->uart, c);
  187. return 1;
  188. }
  189. static int uart_getc(struct rt_serial_device *serial)
  190. {
  191. int ch;
  192. struct hw_uart_device *dev;
  193. RT_ASSERT(serial != RT_NULL);
  194. dev = (struct hw_uart_device *)serial->parent.user_data;
  195. ch = -1;
  196. Zynq7000_UART_GETCHAR(dev->uart, ch);
  197. return ch;
  198. }
  199. static const struct rt_uart_ops _uart_ops =
  200. {
  201. uart_configure,
  202. uart_control,
  203. uart_putc,
  204. uart_getc,
  205. };
  206. /* UART device driver structure */
  207. static struct hw_uart_device _uart_device0 =
  208. {
  209. .uart = (UART_Registers*)Zynq7000_UART0_BASE,
  210. .irqno = IRQ_Zynq7000_UART0,
  211. .rxmio = (rt_uint32_t*)(Zynq7000_SLCR_BASE+0x0728), /* MIO10 */
  212. .txmio = (rt_uint32_t*)(Zynq7000_SLCR_BASE+0x072C), /* MIO11 */
  213. };
  214. static struct hw_uart_device _uart_device1 =
  215. {
  216. .uart = (UART_Registers*)Zynq7000_UART1_BASE,
  217. .irqno = IRQ_Zynq7000_UART1,
  218. .rxmio = (rt_uint32_t*)(Zynq7000_SLCR_BASE+0x07C4), /* MIO49 */
  219. .txmio = (rt_uint32_t*)(Zynq7000_SLCR_BASE+0x07C0), /* MIO48 */
  220. };
  221. static struct rt_serial_device _serial0;
  222. static struct rt_serial_device _serial1;
  223. int rt_hw_uart_init(void)
  224. {
  225. struct serial_configure config;
  226. config.baud_rate = BAUD_RATE_115200;
  227. config.bit_order = BIT_ORDER_LSB;
  228. config.data_bits = DATA_BITS_8;
  229. config.parity = PARITY_NONE;
  230. config.stop_bits = STOP_BITS_1;
  231. config.invert = NRZ_NORMAL;
  232. config.bufsz = RT_SERIAL_RB_BUFSZ;
  233. _serial0.ops = &_uart_ops;
  234. _serial0.config = config;
  235. _serial1.ops = &_uart_ops;
  236. _serial1.config = config;
  237. /* register uart device */
  238. rt_hw_serial_register(&_serial0, "uart0",
  239. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  240. &_uart_device0);
  241. rt_hw_serial_register(&_serial1, "uart1",
  242. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  243. &_uart_device1);
  244. return 0;
  245. }
  246. INIT_BOARD_EXPORT(rt_hw_uart_init);