mcfuart.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * (C) Copyright 2004-2007 Freescale Semiconductor, Inc.
  3. * TsiChung Liew, Tsi-Chung.Liew@freescale.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (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
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * Minimal serial functions needed to use one of the uart ports
  26. * as serial console interface.
  27. */
  28. #include <common.h>
  29. #include <serial.h>
  30. #include <linux/compiler.h>
  31. #include <asm/immap.h>
  32. #include <asm/uart.h>
  33. DECLARE_GLOBAL_DATA_PTR;
  34. extern void uart_port_conf(int port);
  35. static int mcf_serial_init(void)
  36. {
  37. volatile uart_t *uart;
  38. u32 counter;
  39. uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE);
  40. uart_port_conf(CONFIG_SYS_UART_PORT);
  41. /* write to SICR: SIM2 = uart mode,dcd does not affect rx */
  42. uart->ucr = UART_UCR_RESET_RX;
  43. uart->ucr = UART_UCR_RESET_TX;
  44. uart->ucr = UART_UCR_RESET_ERROR;
  45. uart->ucr = UART_UCR_RESET_MR;
  46. __asm__("nop");
  47. uart->uimr = 0;
  48. /* write to CSR: RX/TX baud rate from timers */
  49. uart->ucsr = (UART_UCSR_RCS_SYS_CLK | UART_UCSR_TCS_SYS_CLK);
  50. uart->umr = (UART_UMR_BC_8 | UART_UMR_PM_NONE);
  51. uart->umr = UART_UMR_SB_STOP_BITS_1;
  52. /* Setting up BaudRate */
  53. counter = (u32) ((gd->bus_clk / 32) + (gd->baudrate / 2));
  54. counter = counter / gd->baudrate;
  55. /* write to CTUR: divide counter upper byte */
  56. uart->ubg1 = (u8) ((counter & 0xff00) >> 8);
  57. /* write to CTLR: divide counter lower byte */
  58. uart->ubg2 = (u8) (counter & 0x00ff);
  59. uart->ucr = (UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED);
  60. return (0);
  61. }
  62. static void mcf_serial_putc(const char c)
  63. {
  64. volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE);
  65. if (c == '\n')
  66. serial_putc('\r');
  67. /* Wait for last character to go. */
  68. while (!(uart->usr & UART_USR_TXRDY)) ;
  69. uart->utb = c;
  70. }
  71. static int mcf_serial_getc(void)
  72. {
  73. volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE);
  74. /* Wait for a character to arrive. */
  75. while (!(uart->usr & UART_USR_RXRDY)) ;
  76. return uart->urb;
  77. }
  78. static int mcf_serial_tstc(void)
  79. {
  80. volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE);
  81. return (uart->usr & UART_USR_RXRDY);
  82. }
  83. static void mcf_serial_setbrg(void)
  84. {
  85. volatile uart_t *uart = (volatile uart_t *)(CONFIG_SYS_UART_BASE);
  86. u32 counter;
  87. /* Setting up BaudRate */
  88. counter = (u32) ((gd->bus_clk / 32) + (gd->baudrate / 2));
  89. counter = counter / gd->baudrate;
  90. /* write to CTUR: divide counter upper byte */
  91. uart->ubg1 = ((counter & 0xff00) >> 8);
  92. /* write to CTLR: divide counter lower byte */
  93. uart->ubg2 = (counter & 0x00ff);
  94. uart->ucr = UART_UCR_RESET_RX;
  95. uart->ucr = UART_UCR_RESET_TX;
  96. uart->ucr = UART_UCR_RX_ENABLED | UART_UCR_TX_ENABLED;
  97. }
  98. static struct serial_device mcf_serial_drv = {
  99. .name = "mcf_serial",
  100. .start = mcf_serial_init,
  101. .stop = NULL,
  102. .setbrg = mcf_serial_setbrg,
  103. .putc = mcf_serial_putc,
  104. .puts = default_serial_puts,
  105. .getc = mcf_serial_getc,
  106. .tstc = mcf_serial_tstc,
  107. };
  108. void mcf_serial_initialize(void)
  109. {
  110. serial_register(&mcf_serial_drv);
  111. }
  112. __weak struct serial_device *default_serial_console(void)
  113. {
  114. return &mcf_serial_drv;
  115. }