mcfuart.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * (C) Copyright 2004-2007 Freescale Semiconductor, Inc.
  3. * TsiChung Liew, Tsi-Chung.Liew@freescale.com.
  4. *
  5. * Modified to add device model (DM) support
  6. * (C) Copyright 2015 Angelo Dureghello <angelo@sysam.it>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. /*
  11. * Minimal serial functions needed to use one of the uart ports
  12. * as serial console interface.
  13. */
  14. #include <common.h>
  15. #include <dm.h>
  16. #include <dm/platform_data/serial_coldfire.h>
  17. #include <serial.h>
  18. #include <linux/compiler.h>
  19. #include <asm/immap.h>
  20. #include <asm/uart.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. extern void uart_port_conf(int port);
  23. static int mcf_serial_init_common(uart_t *uart, int port_idx, int baudrate)
  24. {
  25. u32 counter;
  26. uart_port_conf(port_idx);
  27. /* write to SICR: SIM2 = uart mode,dcd does not affect rx */
  28. writeb(UART_UCR_RESET_RX, &uart->ucr);
  29. writeb(UART_UCR_RESET_TX, &uart->ucr);
  30. writeb(UART_UCR_RESET_ERROR, &uart->ucr);
  31. writeb(UART_UCR_RESET_MR, &uart->ucr);
  32. __asm__("nop");
  33. writeb(0, &uart->uimr);
  34. /* write to CSR: RX/TX baud rate from timers */
  35. writeb(UART_UCSR_RCS_SYS_CLK | UART_UCSR_TCS_SYS_CLK, &uart->ucsr);
  36. writeb(UART_UMR_BC_8 | UART_UMR_PM_NONE, &uart->umr);
  37. writeb(UART_UMR_SB_STOP_BITS_1, &uart->umr);
  38. /* Setting up BaudRate */
  39. counter = (u32) ((gd->bus_clk / 32) + (baudrate / 2));
  40. counter = counter / baudrate;
  41. /* write to CTUR: divide counter upper byte */
  42. writeb((u8)((counter & 0xff00) >> 8), &uart->ubg1);
  43. /* write to CTLR: divide counter lower byte */
  44. writeb((u8)(counter & 0x00ff), &uart->ubg2);
  45. writeb(UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED, &uart->ucr);
  46. return (0);
  47. }
  48. static void mcf_serial_setbrg_common(uart_t *uart, int baudrate)
  49. {
  50. u32 counter;
  51. /* Setting up BaudRate */
  52. counter = (u32) ((gd->bus_clk / 32) + (baudrate / 2));
  53. counter = counter / baudrate;
  54. /* write to CTUR: divide counter upper byte */
  55. writeb(((counter & 0xff00) >> 8), &uart->ubg1);
  56. /* write to CTLR: divide counter lower byte */
  57. writeb((counter & 0x00ff), &uart->ubg2);
  58. writeb(UART_UCR_RESET_RX, &uart->ucr);
  59. writeb(UART_UCR_RESET_TX, &uart->ucr);
  60. writeb(UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED, &uart->ucr);
  61. }
  62. #ifndef CONFIG_DM_SERIAL
  63. static int mcf_serial_init(void)
  64. {
  65. uart_t *uart_base;
  66. int port_idx;
  67. uart_base = (uart_t *)CONFIG_SYS_UART_BASE;
  68. port_idx = CONFIG_SYS_UART_PORT;
  69. return mcf_serial_init_common(uart_base, port_idx, gd->baudrate);
  70. }
  71. static void mcf_serial_putc(const char c)
  72. {
  73. uart_t *uart = (uart_t *)CONFIG_SYS_UART_BASE;
  74. if (c == '\n')
  75. serial_putc('\r');
  76. /* Wait for last character to go. */
  77. while (!(readb(&uart->usr) & UART_USR_TXRDY))
  78. ;
  79. writeb(c, &uart->utb);
  80. }
  81. static int mcf_serial_getc(void)
  82. {
  83. uart_t *uart = (uart_t *)CONFIG_SYS_UART_BASE;
  84. /* Wait for a character to arrive. */
  85. while (!(readb(&uart->usr) & UART_USR_RXRDY))
  86. ;
  87. return readb(&uart->urb);
  88. }
  89. static void mcf_serial_setbrg(void)
  90. {
  91. uart_t *uart = (uart_t *)CONFIG_SYS_UART_BASE;
  92. mcf_serial_setbrg_common(uart, gd->baudrate);
  93. }
  94. static int mcf_serial_tstc(void)
  95. {
  96. uart_t *uart = (uart_t *)CONFIG_SYS_UART_BASE;
  97. return readb(&uart->usr) & UART_USR_RXRDY;
  98. }
  99. static struct serial_device mcf_serial_drv = {
  100. .name = "mcf_serial",
  101. .start = mcf_serial_init,
  102. .stop = NULL,
  103. .setbrg = mcf_serial_setbrg,
  104. .putc = mcf_serial_putc,
  105. .puts = default_serial_puts,
  106. .getc = mcf_serial_getc,
  107. .tstc = mcf_serial_tstc,
  108. };
  109. void mcf_serial_initialize(void)
  110. {
  111. serial_register(&mcf_serial_drv);
  112. }
  113. __weak struct serial_device *default_serial_console(void)
  114. {
  115. return &mcf_serial_drv;
  116. }
  117. #endif
  118. #ifdef CONFIG_DM_SERIAL
  119. static int coldfire_serial_probe(struct udevice *dev)
  120. {
  121. struct coldfire_serial_platdata *plat = dev->platdata;
  122. return mcf_serial_init_common((uart_t *)plat->base,
  123. plat->port, plat->baudrate);
  124. }
  125. static int coldfire_serial_putc(struct udevice *dev, const char ch)
  126. {
  127. struct coldfire_serial_platdata *plat = dev->platdata;
  128. uart_t *uart = (uart_t *)plat->base;
  129. /* Wait for last character to go. */
  130. if (!(readb(&uart->usr) & UART_USR_TXRDY))
  131. return -EAGAIN;
  132. writeb(ch, &uart->utb);
  133. return 0;
  134. }
  135. static int coldfire_serial_getc(struct udevice *dev)
  136. {
  137. struct coldfire_serial_platdata *plat = dev->platdata;
  138. uart_t *uart = (uart_t *)(plat->base);
  139. /* Wait for a character to arrive. */
  140. if (!(readb(&uart->usr) & UART_USR_RXRDY))
  141. return -EAGAIN;
  142. return readb(&uart->urb);
  143. }
  144. int coldfire_serial_setbrg(struct udevice *dev, int baudrate)
  145. {
  146. struct coldfire_serial_platdata *plat = dev->platdata;
  147. uart_t *uart = (uart_t *)(plat->base);
  148. mcf_serial_setbrg_common(uart, baudrate);
  149. return 0;
  150. }
  151. static int coldfire_serial_pending(struct udevice *dev, bool input)
  152. {
  153. struct coldfire_serial_platdata *plat = dev->platdata;
  154. uart_t *uart = (uart_t *)(plat->base);
  155. if (input)
  156. return readb(&uart->usr) & UART_USR_RXRDY ? 1 : 0;
  157. else
  158. return readb(&uart->usr) & UART_USR_TXRDY ? 0 : 1;
  159. return 0;
  160. }
  161. static const struct dm_serial_ops coldfire_serial_ops = {
  162. .putc = coldfire_serial_putc,
  163. .pending = coldfire_serial_pending,
  164. .getc = coldfire_serial_getc,
  165. .setbrg = coldfire_serial_setbrg,
  166. };
  167. U_BOOT_DRIVER(serial_coldfire) = {
  168. .name = "serial_coldfire",
  169. .id = UCLASS_SERIAL,
  170. .probe = coldfire_serial_probe,
  171. .ops = &coldfire_serial_ops,
  172. .flags = DM_FLAG_PRE_RELOC,
  173. };
  174. #endif