serial_zynq.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2012 Michal Simek <monstr@monstr.eu>
  3. * Copyright (C) 2011-2012 Xilinx, Inc. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <fdtdec.h>
  9. #include <watchdog.h>
  10. #include <asm/io.h>
  11. #include <linux/compiler.h>
  12. #include <serial.h>
  13. #include <asm/arch/clk.h>
  14. #include <asm/arch/hardware.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. #define ZYNQ_UART_SR_TXFULL 0x00000010 /* TX FIFO full */
  17. #define ZYNQ_UART_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
  18. #define ZYNQ_UART_CR_TX_EN 0x00000010 /* TX enabled */
  19. #define ZYNQ_UART_CR_RX_EN 0x00000004 /* RX enabled */
  20. #define ZYNQ_UART_CR_TXRST 0x00000002 /* TX logic reset */
  21. #define ZYNQ_UART_CR_RXRST 0x00000001 /* RX logic reset */
  22. #define ZYNQ_UART_MR_PARITY_NONE 0x00000020 /* No parity mode */
  23. struct uart_zynq {
  24. u32 control; /* 0x0 - Control Register [8:0] */
  25. u32 mode; /* 0x4 - Mode Register [10:0] */
  26. u32 reserved1[4];
  27. u32 baud_rate_gen; /* 0x18 - Baud Rate Generator [15:0] */
  28. u32 reserved2[4];
  29. u32 channel_sts; /* 0x2c - Channel Status [11:0] */
  30. u32 tx_rx_fifo; /* 0x30 - FIFO [15:0] or [7:0] */
  31. u32 baud_rate_divider; /* 0x34 - Baud Rate Divider [7:0] */
  32. };
  33. static struct uart_zynq *uart_zynq_ports[2] = {
  34. [0] = (struct uart_zynq *)ZYNQ_SERIAL_BASEADDR0,
  35. [1] = (struct uart_zynq *)ZYNQ_SERIAL_BASEADDR1,
  36. };
  37. /* Set up the baud rate in gd struct */
  38. static void uart_zynq_serial_setbrg(const int port)
  39. {
  40. /* Calculation results. */
  41. unsigned int calc_bauderror, bdiv, bgen;
  42. unsigned long calc_baud = 0;
  43. unsigned long baud = gd->baudrate;
  44. unsigned long clock = get_uart_clk(port);
  45. struct uart_zynq *regs = uart_zynq_ports[port];
  46. /* master clock
  47. * Baud rate = ------------------
  48. * bgen * (bdiv + 1)
  49. *
  50. * Find acceptable values for baud generation.
  51. */
  52. for (bdiv = 4; bdiv < 255; bdiv++) {
  53. bgen = clock / (baud * (bdiv + 1));
  54. if (bgen < 2 || bgen > 65535)
  55. continue;
  56. calc_baud = clock / (bgen * (bdiv + 1));
  57. /*
  58. * Use first calculated baudrate with
  59. * an acceptable (<3%) error
  60. */
  61. if (baud > calc_baud)
  62. calc_bauderror = baud - calc_baud;
  63. else
  64. calc_bauderror = calc_baud - baud;
  65. if (((calc_bauderror * 100) / baud) < 3)
  66. break;
  67. }
  68. writel(bdiv, &regs->baud_rate_divider);
  69. writel(bgen, &regs->baud_rate_gen);
  70. }
  71. /* Initialize the UART, with...some settings. */
  72. static int uart_zynq_serial_init(const int port)
  73. {
  74. struct uart_zynq *regs = uart_zynq_ports[port];
  75. if (!regs)
  76. return -1;
  77. /* RX/TX enabled & reset */
  78. writel(ZYNQ_UART_CR_TX_EN | ZYNQ_UART_CR_RX_EN | ZYNQ_UART_CR_TXRST | \
  79. ZYNQ_UART_CR_RXRST, &regs->control);
  80. writel(ZYNQ_UART_MR_PARITY_NONE, &regs->mode); /* 8 bit, no parity */
  81. uart_zynq_serial_setbrg(port);
  82. return 0;
  83. }
  84. static void uart_zynq_serial_putc(const char c, const int port)
  85. {
  86. struct uart_zynq *regs = uart_zynq_ports[port];
  87. while ((readl(&regs->channel_sts) & ZYNQ_UART_SR_TXFULL) != 0)
  88. WATCHDOG_RESET();
  89. if (c == '\n') {
  90. writel('\r', &regs->tx_rx_fifo);
  91. while ((readl(&regs->channel_sts) & ZYNQ_UART_SR_TXFULL) != 0)
  92. WATCHDOG_RESET();
  93. }
  94. writel(c, &regs->tx_rx_fifo);
  95. }
  96. static void uart_zynq_serial_puts(const char *s, const int port)
  97. {
  98. while (*s)
  99. uart_zynq_serial_putc(*s++, port);
  100. }
  101. static int uart_zynq_serial_tstc(const int port)
  102. {
  103. struct uart_zynq *regs = uart_zynq_ports[port];
  104. return (readl(&regs->channel_sts) & ZYNQ_UART_SR_RXEMPTY) == 0;
  105. }
  106. static int uart_zynq_serial_getc(const int port)
  107. {
  108. struct uart_zynq *regs = uart_zynq_ports[port];
  109. while (!uart_zynq_serial_tstc(port))
  110. WATCHDOG_RESET();
  111. return readl(&regs->tx_rx_fifo);
  112. }
  113. /* Multi serial device functions */
  114. #define DECLARE_PSSERIAL_FUNCTIONS(port) \
  115. static int uart_zynq##port##_init(void) \
  116. { return uart_zynq_serial_init(port); } \
  117. static void uart_zynq##port##_setbrg(void) \
  118. { return uart_zynq_serial_setbrg(port); } \
  119. static int uart_zynq##port##_getc(void) \
  120. { return uart_zynq_serial_getc(port); } \
  121. static int uart_zynq##port##_tstc(void) \
  122. { return uart_zynq_serial_tstc(port); } \
  123. static void uart_zynq##port##_putc(const char c) \
  124. { uart_zynq_serial_putc(c, port); } \
  125. static void uart_zynq##port##_puts(const char *s) \
  126. { uart_zynq_serial_puts(s, port); }
  127. /* Serial device descriptor */
  128. #define INIT_PSSERIAL_STRUCTURE(port, __name) { \
  129. .name = __name, \
  130. .start = uart_zynq##port##_init, \
  131. .stop = NULL, \
  132. .setbrg = uart_zynq##port##_setbrg, \
  133. .getc = uart_zynq##port##_getc, \
  134. .tstc = uart_zynq##port##_tstc, \
  135. .putc = uart_zynq##port##_putc, \
  136. .puts = uart_zynq##port##_puts, \
  137. }
  138. DECLARE_PSSERIAL_FUNCTIONS(0);
  139. static struct serial_device uart_zynq_serial0_device =
  140. INIT_PSSERIAL_STRUCTURE(0, "ttyPS0");
  141. DECLARE_PSSERIAL_FUNCTIONS(1);
  142. static struct serial_device uart_zynq_serial1_device =
  143. INIT_PSSERIAL_STRUCTURE(1, "ttyPS1");
  144. #ifdef CONFIG_OF_CONTROL
  145. __weak struct serial_device *default_serial_console(void)
  146. {
  147. const void *blob = gd->fdt_blob;
  148. int node;
  149. unsigned int base_addr;
  150. node = fdt_path_offset(blob, "serial0");
  151. if (node < 0)
  152. return NULL;
  153. base_addr = fdtdec_get_addr(blob, node, "reg");
  154. if (base_addr == FDT_ADDR_T_NONE)
  155. return NULL;
  156. if (base_addr == ZYNQ_SERIAL_BASEADDR0)
  157. return &uart_zynq_serial0_device;
  158. if (base_addr == ZYNQ_SERIAL_BASEADDR1)
  159. return &uart_zynq_serial1_device;
  160. return NULL;
  161. }
  162. #else
  163. __weak struct serial_device *default_serial_console(void)
  164. {
  165. #if defined(CONFIG_ZYNQ_SERIAL_UART0)
  166. if (uart_zynq_ports[0])
  167. return &uart_zynq_serial0_device;
  168. #endif
  169. #if defined(CONFIG_ZYNQ_SERIAL_UART1)
  170. if (uart_zynq_ports[1])
  171. return &uart_zynq_serial1_device;
  172. #endif
  173. return NULL;
  174. }
  175. #endif
  176. void zynq_serial_initialize(void)
  177. {
  178. serial_register(&uart_zynq_serial0_device);
  179. serial_register(&uart_zynq_serial1_device);
  180. }