serial_ks8695.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * serial.c -- KS8695 serial driver
  3. *
  4. * (C) Copyright 2004, Greg Ungerer <greg.ungerer@opengear.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <common.h>
  21. #include <asm/arch/platform.h>
  22. #include <serial.h>
  23. #include <linux/compiler.h>
  24. #ifndef CONFIG_SERIAL1
  25. #error "Bad: you didn't configure serial ..."
  26. #endif
  27. DECLARE_GLOBAL_DATA_PTR;
  28. /*
  29. * Define the UART hardware register access structure.
  30. */
  31. struct ks8695uart {
  32. unsigned int RX; /* 0x00 - Receive data (r) */
  33. unsigned int TX; /* 0x04 - Transmit data (w) */
  34. unsigned int FCR; /* 0x08 - Fifo Control (r/w) */
  35. unsigned int LCR; /* 0x0c - Line Control (r/w) */
  36. unsigned int MCR; /* 0x10 - Modem Control (r/w) */
  37. unsigned int LSR; /* 0x14 - Line Status (r/w) */
  38. unsigned int MSR; /* 0x18 - Modem Status (r/w) */
  39. unsigned int BD; /* 0x1c - Baud Rate (r/w) */
  40. unsigned int SR; /* 0x20 - Status (r/w) */
  41. };
  42. #define KS8695_UART_ADDR ((void *) (KS8695_IO_BASE + KS8695_UART_RX_BUFFER))
  43. #define KS8695_UART_CLK 25000000
  44. /*
  45. * Under some circumstances we want to be "quiet" and not issue any
  46. * serial output - though we want u-boot to otherwise work and behave
  47. * the same. By default be noisy.
  48. */
  49. int serial_console = 1;
  50. static void ks8695_serial_setbrg(void)
  51. {
  52. volatile struct ks8695uart *uartp = KS8695_UART_ADDR;
  53. /* Set to global baud rate and 8 data bits, no parity, 1 stop bit*/
  54. uartp->BD = KS8695_UART_CLK / gd->baudrate;
  55. uartp->LCR = KS8695_UART_LINEC_WLEN8;
  56. }
  57. static int ks8695_serial_init(void)
  58. {
  59. serial_console = 1;
  60. serial_setbrg();
  61. return 0;
  62. }
  63. static void ks8695_serial_raw_putc(const char c)
  64. {
  65. volatile struct ks8695uart *uartp = KS8695_UART_ADDR;
  66. int i;
  67. for (i = 0; (i < 0x100000); i++) {
  68. if (uartp->LSR & KS8695_UART_LINES_TXFE)
  69. break;
  70. }
  71. uartp->TX = c;
  72. }
  73. static void ks8695_serial_putc(const char c)
  74. {
  75. if (serial_console) {
  76. ks8695_serial_raw_putc(c);
  77. if (c == '\n')
  78. ks8695_serial_raw_putc('\r');
  79. }
  80. }
  81. static int ks8695_serial_tstc(void)
  82. {
  83. volatile struct ks8695uart *uartp = KS8695_UART_ADDR;
  84. if (serial_console)
  85. return ((uartp->LSR & KS8695_UART_LINES_RXFE) ? 1 : 0);
  86. return 0;
  87. }
  88. static void ks8695_serial_puts(const char *s)
  89. {
  90. char c;
  91. while ((c = *s++) != 0)
  92. serial_putc(c);
  93. }
  94. static int ks8695_serial_getc(void)
  95. {
  96. volatile struct ks8695uart *uartp = KS8695_UART_ADDR;
  97. while ((uartp->LSR & KS8695_UART_LINES_RXFE) == 0)
  98. ;
  99. return (uartp->RX);
  100. }
  101. #ifdef CONFIG_SERIAL_MULTI
  102. static struct serial_device ks8695_serial_drv = {
  103. .name = "ks8695_serial",
  104. .start = ks8695_serial_init,
  105. .stop = NULL,
  106. .setbrg = ks8695_serial_setbrg,
  107. .putc = ks8695_serial_putc,
  108. .puts = ks8695_serial_puts,
  109. .getc = ks8695_serial_getc,
  110. .tstc = ks8695_serial_tstc,
  111. };
  112. void ks8695_serial_initialize(void)
  113. {
  114. serial_register(&ks8695_serial_drv);
  115. }
  116. __weak struct serial_device *default_serial_console(void)
  117. {
  118. return &ks8695_serial_drv;
  119. }
  120. #else
  121. int serial_init(void)
  122. {
  123. return ks8695_serial_init();
  124. }
  125. void serial_setbrg(void)
  126. {
  127. ks8695_serial_setbrg();
  128. }
  129. void serial_putc(const char c)
  130. {
  131. ks8695_serial_putc(c);
  132. }
  133. void serial_puts(const char *s)
  134. {
  135. ks8695_serial_puts(s);
  136. }
  137. int serial_getc(void)
  138. {
  139. return ks8695_serial_getc();
  140. }
  141. int serial_tstc(void)
  142. {
  143. return ks8695_serial_tstc();
  144. }
  145. #endif