serial_ks8695.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * serial.c -- KS8695 serial driver
  3. *
  4. * (C) Copyright 2004, Greg Ungerer <greg.ungerer@opengear.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <asm/arch/platform.h>
  10. #include <serial.h>
  11. #include <linux/compiler.h>
  12. #ifndef CONFIG_SERIAL1
  13. #error "Bad: you didn't configure serial ..."
  14. #endif
  15. DECLARE_GLOBAL_DATA_PTR;
  16. /*
  17. * Define the UART hardware register access structure.
  18. */
  19. struct ks8695uart {
  20. unsigned int RX; /* 0x00 - Receive data (r) */
  21. unsigned int TX; /* 0x04 - Transmit data (w) */
  22. unsigned int FCR; /* 0x08 - Fifo Control (r/w) */
  23. unsigned int LCR; /* 0x0c - Line Control (r/w) */
  24. unsigned int MCR; /* 0x10 - Modem Control (r/w) */
  25. unsigned int LSR; /* 0x14 - Line Status (r/w) */
  26. unsigned int MSR; /* 0x18 - Modem Status (r/w) */
  27. unsigned int BD; /* 0x1c - Baud Rate (r/w) */
  28. unsigned int SR; /* 0x20 - Status (r/w) */
  29. };
  30. #define KS8695_UART_ADDR ((void *) (KS8695_IO_BASE + KS8695_UART_RX_BUFFER))
  31. #define KS8695_UART_CLK 25000000
  32. /*
  33. * Under some circumstances we want to be "quiet" and not issue any
  34. * serial output - though we want u-boot to otherwise work and behave
  35. * the same. By default be noisy.
  36. */
  37. int serial_console = 1;
  38. static void ks8695_serial_setbrg(void)
  39. {
  40. volatile struct ks8695uart *uartp = KS8695_UART_ADDR;
  41. /* Set to global baud rate and 8 data bits, no parity, 1 stop bit*/
  42. uartp->BD = KS8695_UART_CLK / gd->baudrate;
  43. uartp->LCR = KS8695_UART_LINEC_WLEN8;
  44. }
  45. static int ks8695_serial_init(void)
  46. {
  47. serial_console = 1;
  48. serial_setbrg();
  49. return 0;
  50. }
  51. static void ks8695_serial_raw_putc(const char c)
  52. {
  53. volatile struct ks8695uart *uartp = KS8695_UART_ADDR;
  54. int i;
  55. for (i = 0; (i < 0x100000); i++) {
  56. if (uartp->LSR & KS8695_UART_LINES_TXFE)
  57. break;
  58. }
  59. uartp->TX = c;
  60. }
  61. static void ks8695_serial_putc(const char c)
  62. {
  63. if (serial_console) {
  64. ks8695_serial_raw_putc(c);
  65. if (c == '\n')
  66. ks8695_serial_raw_putc('\r');
  67. }
  68. }
  69. static int ks8695_serial_tstc(void)
  70. {
  71. volatile struct ks8695uart *uartp = KS8695_UART_ADDR;
  72. if (serial_console)
  73. return ((uartp->LSR & KS8695_UART_LINES_RXFE) ? 1 : 0);
  74. return 0;
  75. }
  76. static int ks8695_serial_getc(void)
  77. {
  78. volatile struct ks8695uart *uartp = KS8695_UART_ADDR;
  79. while ((uartp->LSR & KS8695_UART_LINES_RXFE) == 0)
  80. ;
  81. return (uartp->RX);
  82. }
  83. static struct serial_device ks8695_serial_drv = {
  84. .name = "ks8695_serial",
  85. .start = ks8695_serial_init,
  86. .stop = NULL,
  87. .setbrg = ks8695_serial_setbrg,
  88. .putc = ks8695_serial_putc,
  89. .puts = default_serial_puts,
  90. .getc = ks8695_serial_getc,
  91. .tstc = ks8695_serial_tstc,
  92. };
  93. void ks8695_serial_initialize(void)
  94. {
  95. serial_register(&ks8695_serial_drv);
  96. }
  97. __weak struct serial_device *default_serial_console(void)
  98. {
  99. return &ks8695_serial_drv;
  100. }