fpga_serial.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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. #include <config.h>
  24. #include <common.h>
  25. #include <asm/io.h>
  26. #include "fpga_serial.h"
  27. #include "hardware.h"
  28. #include "pcippc2.h"
  29. /* 8 data, 1 stop, no parity
  30. */
  31. #define LCRVAL 0x03
  32. /* RTS/DTR
  33. */
  34. #define MCRVAL 0x03
  35. /* Clear & enable FIFOs
  36. */
  37. #define FCRVAL 0x07
  38. static void fpga_serial_wait (void);
  39. static void fpga_serial_print (char c);
  40. void fpga_serial_init (int baudrate)
  41. {
  42. int clock_divisor = 115200 / baudrate;
  43. out8 (FPGA (INT, SERIAL_CONFIG), 0x24);
  44. iobarrier_rw ();
  45. fpga_serial_wait ();
  46. out8 (UART (IER), 0);
  47. out8 (UART (LCR), LCRVAL | 0x80);
  48. iobarrier_rw ();
  49. out8 (UART (DLL), clock_divisor & 0xff);
  50. out8 (UART (DLM), clock_divisor >> 8);
  51. iobarrier_rw ();
  52. out8 (UART (LCR), LCRVAL);
  53. iobarrier_rw ();
  54. out8 (UART (MCR), MCRVAL);
  55. out8 (UART (FCR), FCRVAL);
  56. iobarrier_rw ();
  57. }
  58. void fpga_serial_putc (char c)
  59. {
  60. if (c) {
  61. fpga_serial_print (c);
  62. }
  63. }
  64. void fpga_serial_puts (const char *s)
  65. {
  66. while (*s) {
  67. fpga_serial_print (*s++);
  68. }
  69. }
  70. int fpga_serial_getc (void)
  71. {
  72. while ((in8 (UART (LSR)) & 0x01) == 0);
  73. return in8 (UART (RBR));
  74. }
  75. int fpga_serial_tstc (void)
  76. {
  77. return (in8 (UART (LSR)) & 0x01) != 0;
  78. }
  79. void fpga_serial_setbrg (void)
  80. {
  81. DECLARE_GLOBAL_DATA_PTR;
  82. int clock_divisor = 115200 / gd->baudrate;
  83. fpga_serial_wait ();
  84. out8 (UART (LCR), LCRVAL | 0x80);
  85. iobarrier_rw ();
  86. out8 (UART (DLL), clock_divisor & 0xff);
  87. out8 (UART (DLM), clock_divisor >> 8);
  88. iobarrier_rw ();
  89. out8 (UART (LCR), LCRVAL);
  90. iobarrier_rw ();
  91. }
  92. static void fpga_serial_wait (void)
  93. {
  94. while ((in8 (UART (LSR)) & 0x40) == 0);
  95. }
  96. static void fpga_serial_print (char c)
  97. {
  98. if (c == '\n') {
  99. while ((in8 (UART (LSR)) & 0x20) == 0);
  100. out8 (UART (THR), '\r');
  101. iobarrier_rw ();
  102. }
  103. while ((in8 (UART (LSR)) & 0x20) == 0);
  104. out8 (UART (THR), c);
  105. iobarrier_rw ();
  106. if (c == '\n') {
  107. fpga_serial_wait ();
  108. }
  109. }