serial.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * U-boot - serial.c Blackfin Serial Driver
  3. *
  4. * Copyright (c) 2005-2008 Analog Devices Inc.
  5. *
  6. * Copyright (c) 2003 Bas Vermeulen <bas@buyways.nl>,
  7. * BuyWays B.V. (www.buyways.nl)
  8. *
  9. * Based heavily on:
  10. * blkfinserial.c: Serial driver for BlackFin DSP internal USRTs.
  11. * Copyright(c) 2003 Metrowerks <mwaddel@metrowerks.com>
  12. * Copyright(c) 2001 Tony Z. Kou <tonyko@arcturusnetworks.com>
  13. * Copyright(c) 2001-2002 Arcturus Networks Inc. <www.arcturusnetworks.com>
  14. *
  15. * Based on code from 68328 version serial driver imlpementation which was:
  16. * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
  17. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
  18. * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
  19. * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
  20. *
  21. * (C) Copyright 2000-2004
  22. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  23. *
  24. * Licensed under the GPL-2 or later.
  25. */
  26. #include <common.h>
  27. #include <watchdog.h>
  28. #include <asm/blackfin.h>
  29. #include <asm/mach-common/bits/uart.h>
  30. #if defined(UART_LSR) && (CONFIG_UART_CONSOLE != 0)
  31. # error CONFIG_UART_CONSOLE must be 0 on parts with only one UART
  32. #endif
  33. #include "serial.h"
  34. /* Symbol for our assembly to call. */
  35. void serial_set_baud(uint32_t baud)
  36. {
  37. serial_early_set_baud(baud);
  38. }
  39. /* Symbol for common u-boot code to call.
  40. * Setup the baudrate (brg: baudrate generator).
  41. */
  42. void serial_setbrg(void)
  43. {
  44. DECLARE_GLOBAL_DATA_PTR;
  45. serial_set_baud(gd->baudrate);
  46. }
  47. /* Symbol for our assembly to call. */
  48. void serial_initialize(void)
  49. {
  50. serial_early_init();
  51. }
  52. /* Symbol for common u-boot code to call. */
  53. int serial_init(void)
  54. {
  55. serial_initialize();
  56. serial_setbrg();
  57. return 0;
  58. }
  59. void serial_putc(const char c)
  60. {
  61. /* send a \r for compatibility */
  62. if (c == '\n')
  63. serial_putc('\r');
  64. WATCHDOG_RESET();
  65. /* wait for the hardware fifo to clear up */
  66. while (!(*pUART_LSR & THRE))
  67. continue;
  68. /* queue the character for transmission */
  69. *pUART_THR = c;
  70. SSYNC();
  71. WATCHDOG_RESET();
  72. /* wait for the byte to be shifted over the line */
  73. while (!(*pUART_LSR & TEMT))
  74. continue;
  75. }
  76. int serial_tstc(void)
  77. {
  78. WATCHDOG_RESET();
  79. return (*pUART_LSR & DR) ? 1 : 0;
  80. }
  81. int serial_getc(void)
  82. {
  83. uint16_t uart_lsr_val, uart_rbr_val;
  84. /* wait for data ! */
  85. while (!serial_tstc())
  86. continue;
  87. /* clear the status and grab the new byte */
  88. uart_lsr_val = *pUART_LSR;
  89. uart_rbr_val = *pUART_RBR;
  90. if (uart_lsr_val & (OE|PE|FE|BI)) {
  91. /* Some parts are read-to-clear while others are
  92. * write-to-clear. Just do the write for everyone
  93. * since it cant hurt (other than code size).
  94. */
  95. *pUART_LSR = (OE|PE|FE|BI);
  96. return -1;
  97. }
  98. return uart_rbr_val & 0xFF;
  99. }
  100. void serial_puts(const char *s)
  101. {
  102. while (*s)
  103. serial_putc(*s++);
  104. }