fpga_serial.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. DECLARE_GLOBAL_DATA_PTR;
  30. /* 8 data, 1 stop, no parity
  31. */
  32. #define LCRVAL 0x03
  33. /* RTS/DTR
  34. */
  35. #define MCRVAL 0x03
  36. /* Clear & enable FIFOs
  37. */
  38. #define FCRVAL 0x07
  39. static void fpga_serial_wait (void);
  40. static void fpga_serial_print (char c);
  41. void fpga_serial_init (int baudrate)
  42. {
  43. int clock_divisor = 115200 / baudrate;
  44. out8 (FPGA (INT, SERIAL_CONFIG), 0x24);
  45. iobarrier_rw ();
  46. fpga_serial_wait ();
  47. out8 (UART (IER), 0);
  48. out8 (UART (LCR), LCRVAL | 0x80);
  49. iobarrier_rw ();
  50. out8 (UART (DLL), clock_divisor & 0xff);
  51. out8 (UART (DLM), clock_divisor >> 8);
  52. iobarrier_rw ();
  53. out8 (UART (LCR), LCRVAL);
  54. iobarrier_rw ();
  55. out8 (UART (MCR), MCRVAL);
  56. out8 (UART (FCR), FCRVAL);
  57. iobarrier_rw ();
  58. }
  59. void fpga_serial_putc (char c)
  60. {
  61. if (c) {
  62. fpga_serial_print (c);
  63. }
  64. }
  65. int fpga_serial_getc (void)
  66. {
  67. while ((in8 (UART (LSR)) & 0x01) == 0);
  68. return in8 (UART (RBR));
  69. }
  70. int fpga_serial_tstc (void)
  71. {
  72. return (in8 (UART (LSR)) & 0x01) != 0;
  73. }
  74. void fpga_serial_setbrg (void)
  75. {
  76. int clock_divisor = 115200 / gd->baudrate;
  77. fpga_serial_wait ();
  78. out8 (UART (LCR), LCRVAL | 0x80);
  79. iobarrier_rw ();
  80. out8 (UART (DLL), clock_divisor & 0xff);
  81. out8 (UART (DLM), clock_divisor >> 8);
  82. iobarrier_rw ();
  83. out8 (UART (LCR), LCRVAL);
  84. iobarrier_rw ();
  85. }
  86. static void fpga_serial_wait (void)
  87. {
  88. while ((in8 (UART (LSR)) & 0x40) == 0);
  89. }
  90. static void fpga_serial_print (char c)
  91. {
  92. if (c == '\n') {
  93. while ((in8 (UART (LSR)) & 0x20) == 0);
  94. out8 (UART (THR), '\r');
  95. iobarrier_rw ();
  96. }
  97. while ((in8 (UART (LSR)) & 0x20) == 0);
  98. out8 (UART (THR), c);
  99. iobarrier_rw ();
  100. if (c == '\n') {
  101. fpga_serial_wait ();
  102. }
  103. }