uart.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <asm/ppc4xx.h>
  13. #include <ns16550.h>
  14. #include <asm/io.h>
  15. #include <serial.h>
  16. /*
  17. * UART test
  18. *
  19. * The controllers are configured to loopback mode and several
  20. * characters are transmitted.
  21. */
  22. #include <post.h>
  23. #if CONFIG_POST & CONFIG_SYS_POST_UART
  24. /*
  25. * This table defines the UART's that should be tested and can
  26. * be overridden in the board config file
  27. */
  28. #ifndef CONFIG_SYS_POST_UART_TABLE
  29. #define CONFIG_SYS_POST_UART_TABLE { CONFIG_SYS_NS16550_COM1, \
  30. CONFIG_SYS_NS16550_COM2, CONFIG_SYS_NS16550_COM3, \
  31. CONFIG_SYS_NS16550_COM4 }
  32. #endif
  33. DECLARE_GLOBAL_DATA_PTR;
  34. static int test_ctlr (struct NS16550 *com_port, int index)
  35. {
  36. int res = -1;
  37. char test_str[] = "*** UART Test String ***\r\n";
  38. int i;
  39. int divisor;
  40. divisor = (get_serial_clock() + (gd->baudrate * (16 / 2))) /
  41. (16 * gd->baudrate);
  42. NS16550_init(com_port, divisor);
  43. /*
  44. * Set internal loopback mode in UART
  45. */
  46. out_8(&com_port->mcr, in_8(&com_port->mcr) | UART_MCR_LOOP);
  47. /* Reset FIFOs */
  48. out_8(&com_port->fcr, UART_FCR_RXSR | UART_FCR_TXSR);
  49. udelay(100);
  50. /* Flush RX-FIFO */
  51. while (NS16550_tstc(com_port))
  52. NS16550_getc(com_port);
  53. for (i = 0; i < sizeof (test_str) - 1; i++) {
  54. NS16550_putc(com_port, test_str[i]);
  55. if (NS16550_getc(com_port) != test_str[i])
  56. goto done;
  57. }
  58. res = 0;
  59. done:
  60. if (res)
  61. post_log ("uart%d test failed\n", index);
  62. return res;
  63. }
  64. int uart_post_test (int flags)
  65. {
  66. int i, res = 0;
  67. static unsigned long base[] = CONFIG_SYS_POST_UART_TABLE;
  68. for (i = 0; i < ARRAY_SIZE(base); i++) {
  69. if (test_ctlr((struct NS16550 *)base[i], i))
  70. res = -1;
  71. }
  72. serial_reinit_all ();
  73. return res;
  74. }
  75. #endif /* CONFIG_POST & CONFIG_SYS_POST_UART */