serial.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * U-boot - serial.c Serial driver for BF533
  3. *
  4. * Copyright (c) 2005 blackfin.uclinux.org
  5. *
  6. * This file is based on
  7. * bf533_serial.c: Serial driver for BlackFin BF533 DSP internal UART.
  8. * Copyright (c) 2003 Bas Vermeulen <bas@buyways.nl>,
  9. * BuyWays B.V. (www.buyways.nl)
  10. *
  11. * Based heavily on blkfinserial.c
  12. * blkfinserial.c: Serial driver for BlackFin DSP internal USRTs.
  13. * Copyright(c) 2003 Metrowerks <mwaddel@metrowerks.com>
  14. * Copyright(c) 2001 Tony Z. Kou <tonyko@arcturusnetworks.com>
  15. * Copyright(c) 2001-2002 Arcturus Networks Inc. <www.arcturusnetworks.com>
  16. *
  17. * Based on code from 68328 version serial driver imlpementation which was:
  18. * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
  19. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
  20. * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
  21. * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
  22. *
  23. * (C) Copyright 2000-2004
  24. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  25. *
  26. * See file CREDITS for list of people who contributed to this
  27. * project.
  28. *
  29. * This program is free software; you can redistribute it and/or
  30. * modify it under the terms of the GNU General Public License as
  31. * published by the Free Software Foundation; either version 2 of
  32. * the License, or (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program; if not, write to the Free Software
  41. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  42. * MA 02111-1307 USA
  43. */
  44. #include <common.h>
  45. #include <asm/irq.h>
  46. #include <asm/system.h>
  47. #include <asm/segment.h>
  48. #include <asm/bitops.h>
  49. #include <asm/delay.h>
  50. #include <asm/uaccess.h>
  51. #include "bf533_serial.h"
  52. unsigned long pll_div_fact;
  53. void calc_baud(void)
  54. {
  55. unsigned char i;
  56. int temp;
  57. for(i = 0; i < sizeof(baud_table)/sizeof(int); i++) {
  58. temp = CONFIG_SCLK_HZ/(baud_table[i]*8);
  59. if ( temp && 0x1 == 1 ) {
  60. temp++;
  61. }
  62. temp = temp/2;
  63. hw_baud_table[i].dl_high = (temp >> 8)& 0xFF;
  64. hw_baud_table[i].dl_low = (temp) & 0xFF;
  65. }
  66. }
  67. void serial_setbrg(void)
  68. {
  69. int i;
  70. DECLARE_GLOBAL_DATA_PTR;
  71. calc_baud();
  72. for (i = 0; i < sizeof(baud_table) / sizeof(int); i++) {
  73. if (gd->baudrate == baud_table[i])
  74. break;
  75. }
  76. /* Enable UART */
  77. *pUART_GCTL |= UART_GCTL_UCEN;
  78. asm("ssync;");
  79. /* Set DLAB in LCR to Access DLL and DLH */
  80. ACCESS_LATCH;
  81. asm("ssync;");
  82. *pUART_DLL = hw_baud_table[i].dl_low;
  83. asm("ssync;");
  84. *pUART_DLH = hw_baud_table[i].dl_high;
  85. asm("ssync;");
  86. /* Clear DLAB in LCR to Access THR RBR IER */
  87. ACCESS_PORT_IER;
  88. asm("ssync;");
  89. /* Enable ERBFI and ELSI interrupts
  90. * to poll SIC_ISR register*/
  91. *pUART_IER = UART_IER_ELSI | UART_IER_ERBFI | UART_IER_ETBEI;
  92. asm("ssync;");
  93. /* Set LCR to Word Lengh 8-bit word select */
  94. *pUART_LCR = UART_LCR_WLS8;
  95. asm("ssync;");
  96. return;
  97. }
  98. int serial_init(void)
  99. {
  100. serial_setbrg();
  101. return (0);
  102. }
  103. void serial_putc(const char c)
  104. {
  105. if ((*pUART_LSR) & UART_LSR_TEMT)
  106. {
  107. if (c == '\n')
  108. serial_putc('\r');
  109. local_put_char(c);
  110. }
  111. while (!((*pUART_LSR) & UART_LSR_TEMT))
  112. SYNC_ALL;
  113. return;
  114. }
  115. int serial_tstc(void)
  116. {
  117. if (*pUART_LSR & UART_LSR_DR)
  118. return 1;
  119. else
  120. return 0;
  121. }
  122. int serial_getc(void)
  123. {
  124. unsigned short uart_lsr_val, uart_rbr_val;
  125. unsigned long isr_val;
  126. int ret;
  127. /* Poll for RX Interrupt */
  128. while (!((isr_val = *(volatile unsigned long *)SIC_ISR) & IRQ_UART_RX_BIT));
  129. asm("csync;");
  130. uart_lsr_val = *pUART_LSR; /* Clear status bit */
  131. uart_rbr_val = *pUART_RBR; /* getc() */
  132. if (isr_val & IRQ_UART_ERROR_BIT) {
  133. ret = -1;
  134. }
  135. else
  136. {
  137. ret = uart_rbr_val & 0xff;
  138. }
  139. return ret;
  140. }
  141. void serial_puts(const char *s)
  142. {
  143. while (*s) {
  144. serial_putc(*s++);
  145. }
  146. }
  147. static void local_put_char(char ch)
  148. {
  149. int flags = 0;
  150. unsigned long isr_val;
  151. save_and_cli(flags);
  152. /* Poll for TX Interruput */
  153. while (!((isr_val = *pSIC_ISR) & IRQ_UART_TX_BIT));
  154. asm("csync;");
  155. *pUART_THR = ch; /* putc() */
  156. if (isr_val & IRQ_UART_ERROR_BIT) {
  157. printf("?");
  158. }
  159. restore_flags(flags);
  160. return ;
  161. }