serial_zynq.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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;
  44. unsigned long clock = get_uart_clk(port);
  45. struct uart_zynq *regs = uart_zynq_ports[port];
  46. /* Covering case where input clock is so slow */
  47. if (clock < 1000000 && gd->baudrate > 4800)
  48. gd->baudrate = 4800;
  49. baud = gd->baudrate;
  50. /* master clock
  51. * Baud rate = ------------------
  52. * bgen * (bdiv + 1)
  53. *
  54. * Find acceptable values for baud generation.
  55. */
  56. for (bdiv = 4; bdiv < 255; bdiv++) {
  57. bgen = clock / (baud * (bdiv + 1));
  58. if (bgen < 2 || bgen > 65535)
  59. continue;
  60. calc_baud = clock / (bgen * (bdiv + 1));
  61. /*
  62. * Use first calculated baudrate with
  63. * an acceptable (<3%) error
  64. */
  65. if (baud > calc_baud)
  66. calc_bauderror = baud - calc_baud;
  67. else
  68. calc_bauderror = calc_baud - baud;
  69. if (((calc_bauderror * 100) / baud) < 3)
  70. break;
  71. }
  72. writel(bdiv, &regs->baud_rate_divider);
  73. writel(bgen, &regs->baud_rate_gen);
  74. }
  75. /* Initialize the UART, with...some settings. */
  76. static int uart_zynq_serial_init(const int port)
  77. {
  78. struct uart_zynq *regs = uart_zynq_ports[port];
  79. if (!regs)
  80. return -1;
  81. /* RX/TX enabled & reset */
  82. writel(ZYNQ_UART_CR_TX_EN | ZYNQ_UART_CR_RX_EN | ZYNQ_UART_CR_TXRST | \
  83. ZYNQ_UART_CR_RXRST, &regs->control);
  84. writel(ZYNQ_UART_MR_PARITY_NONE, &regs->mode); /* 8 bit, no parity */
  85. uart_zynq_serial_setbrg(port);
  86. return 0;
  87. }
  88. static void uart_zynq_serial_putc(const char c, const int port)
  89. {
  90. struct uart_zynq *regs = uart_zynq_ports[port];
  91. while ((readl(&regs->channel_sts) & ZYNQ_UART_SR_TXFULL) != 0)
  92. WATCHDOG_RESET();
  93. if (c == '\n') {
  94. writel('\r', &regs->tx_rx_fifo);
  95. while ((readl(&regs->channel_sts) & ZYNQ_UART_SR_TXFULL) != 0)
  96. WATCHDOG_RESET();
  97. }
  98. writel(c, &regs->tx_rx_fifo);
  99. }
  100. static void uart_zynq_serial_puts(const char *s, const int port)
  101. {
  102. while (*s)
  103. uart_zynq_serial_putc(*s++, port);
  104. }
  105. static int uart_zynq_serial_tstc(const int port)
  106. {
  107. struct uart_zynq *regs = uart_zynq_ports[port];
  108. return (readl(&regs->channel_sts) & ZYNQ_UART_SR_RXEMPTY) == 0;
  109. }
  110. static int uart_zynq_serial_getc(const int port)
  111. {
  112. struct uart_zynq *regs = uart_zynq_ports[port];
  113. while (!uart_zynq_serial_tstc(port))
  114. WATCHDOG_RESET();
  115. return readl(&regs->tx_rx_fifo);
  116. }
  117. /* Multi serial device functions */
  118. #define DECLARE_PSSERIAL_FUNCTIONS(port) \
  119. static int uart_zynq##port##_init(void) \
  120. { return uart_zynq_serial_init(port); } \
  121. static void uart_zynq##port##_setbrg(void) \
  122. { return uart_zynq_serial_setbrg(port); } \
  123. static int uart_zynq##port##_getc(void) \
  124. { return uart_zynq_serial_getc(port); } \
  125. static int uart_zynq##port##_tstc(void) \
  126. { return uart_zynq_serial_tstc(port); } \
  127. static void uart_zynq##port##_putc(const char c) \
  128. { uart_zynq_serial_putc(c, port); } \
  129. static void uart_zynq##port##_puts(const char *s) \
  130. { uart_zynq_serial_puts(s, port); }
  131. /* Serial device descriptor */
  132. #define INIT_PSSERIAL_STRUCTURE(port, __name) { \
  133. .name = __name, \
  134. .start = uart_zynq##port##_init, \
  135. .stop = NULL, \
  136. .setbrg = uart_zynq##port##_setbrg, \
  137. .getc = uart_zynq##port##_getc, \
  138. .tstc = uart_zynq##port##_tstc, \
  139. .putc = uart_zynq##port##_putc, \
  140. .puts = uart_zynq##port##_puts, \
  141. }
  142. DECLARE_PSSERIAL_FUNCTIONS(0);
  143. static struct serial_device uart_zynq_serial0_device =
  144. INIT_PSSERIAL_STRUCTURE(0, "ttyPS0");
  145. DECLARE_PSSERIAL_FUNCTIONS(1);
  146. static struct serial_device uart_zynq_serial1_device =
  147. INIT_PSSERIAL_STRUCTURE(1, "ttyPS1");
  148. #if CONFIG_IS_ENABLED(OF_CONTROL)
  149. __weak struct serial_device *default_serial_console(void)
  150. {
  151. const void *blob = gd->fdt_blob;
  152. int node;
  153. unsigned int base_addr;
  154. node = fdt_path_offset(blob, "serial0");
  155. if (node < 0)
  156. return NULL;
  157. base_addr = fdtdec_get_addr(blob, node, "reg");
  158. if (base_addr == FDT_ADDR_T_NONE)
  159. return NULL;
  160. if (base_addr == ZYNQ_SERIAL_BASEADDR0)
  161. return &uart_zynq_serial0_device;
  162. if (base_addr == ZYNQ_SERIAL_BASEADDR1)
  163. return &uart_zynq_serial1_device;
  164. return NULL;
  165. }
  166. #else
  167. __weak struct serial_device *default_serial_console(void)
  168. {
  169. #if defined(CONFIG_ZYNQ_SERIAL_UART0)
  170. if (uart_zynq_ports[0])
  171. return &uart_zynq_serial0_device;
  172. #endif
  173. #if defined(CONFIG_ZYNQ_SERIAL_UART1)
  174. if (uart_zynq_ports[1])
  175. return &uart_zynq_serial1_device;
  176. #endif
  177. return NULL;
  178. }
  179. #endif
  180. void zynq_serial_initialize(void)
  181. {
  182. serial_register(&uart_zynq_serial0_device);
  183. serial_register(&uart_zynq_serial1_device);
  184. }