uart.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * (C) Copyright 2007
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Author: Igor Lisitsin <igor@emcraft.com>
  6. *
  7. * Copyright 2010, Stefan Roese, DENX Software Engineering, sr@denx.de
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <asm/ppc4xx.h>
  29. #include <ns16550.h>
  30. #include <asm/io.h>
  31. #include <serial.h>
  32. /*
  33. * UART test
  34. *
  35. * The controllers are configured to loopback mode and several
  36. * characters are transmitted.
  37. */
  38. #include <post.h>
  39. #if CONFIG_POST & CONFIG_SYS_POST_UART
  40. /*
  41. * This table defines the UART's that should be tested and can
  42. * be overridden in the board config file
  43. */
  44. #ifndef CONFIG_SYS_POST_UART_TABLE
  45. #define CONFIG_SYS_POST_UART_TABLE { CONFIG_SYS_NS16550_COM1, \
  46. CONFIG_SYS_NS16550_COM2, CONFIG_SYS_NS16550_COM3, \
  47. CONFIG_SYS_NS16550_COM4 }
  48. #endif
  49. DECLARE_GLOBAL_DATA_PTR;
  50. static int test_ctlr (struct NS16550 *com_port, int index)
  51. {
  52. int res = -1;
  53. char test_str[] = "*** UART Test String ***\r\n";
  54. int i;
  55. int divisor;
  56. divisor = (get_serial_clock() + (gd->baudrate * (16 / 2))) /
  57. (16 * gd->baudrate);
  58. NS16550_init(com_port, divisor);
  59. /*
  60. * Set internal loopback mode in UART
  61. */
  62. out_8(&com_port->mcr, in_8(&com_port->mcr) | UART_MCR_LOOP);
  63. /* Reset FIFOs */
  64. out_8(&com_port->fcr, UART_FCR_RXSR | UART_FCR_TXSR);
  65. udelay(100);
  66. /* Flush RX-FIFO */
  67. while (NS16550_tstc(com_port))
  68. NS16550_getc(com_port);
  69. for (i = 0; i < sizeof (test_str) - 1; i++) {
  70. NS16550_putc(com_port, test_str[i]);
  71. if (NS16550_getc(com_port) != test_str[i])
  72. goto done;
  73. }
  74. res = 0;
  75. done:
  76. if (res)
  77. post_log ("uart%d test failed\n", index);
  78. return res;
  79. }
  80. int uart_post_test (int flags)
  81. {
  82. int i, res = 0;
  83. static unsigned long base[] = CONFIG_SYS_POST_UART_TABLE;
  84. for (i = 0; i < ARRAY_SIZE(base); i++) {
  85. if (test_ctlr((struct NS16550 *)base[i], i))
  86. res = -1;
  87. }
  88. serial_reinit_all ();
  89. return res;
  90. }
  91. #endif /* CONFIG_POST & CONFIG_SYS_POST_UART */