opencores_yanu.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <watchdog.h>
  24. #include <asm/io.h>
  25. #include <nios2-yanu.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. /*-----------------------------------------------------------------*/
  28. /* YANU Imagos serial port */
  29. /*-----------------------------------------------------------------*/
  30. static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
  31. #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
  32. /* Everything's already setup for fixed-baud PTF assignment*/
  33. void serial_setbrg (void)
  34. {
  35. int n, k;
  36. const unsigned max_uns = 0xFFFFFFFF;
  37. unsigned best_n, best_m, baud;
  38. /* compute best N and M couple */
  39. best_n = YANU_MAX_PRESCALER_N;
  40. for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
  41. if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
  42. (unsigned)CONFIG_BAUDRATE) {
  43. best_n = n;
  44. break;
  45. }
  46. }
  47. for (k = 0;; k++) {
  48. if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k)))
  49. break;
  50. }
  51. best_m =
  52. ((unsigned)CONFIG_BAUDRATE * (1 << (15 + n - k))) /
  53. ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
  54. baud = best_m + best_n * YANU_BAUDE;
  55. writel(&uart->baud, baud);
  56. return;
  57. }
  58. #else
  59. void serial_setbrg (void)
  60. {
  61. int n, k;
  62. const unsigned max_uns = 0xFFFFFFFF;
  63. unsigned best_n, best_m, baud;
  64. /* compute best N and M couple */
  65. best_n = YANU_MAX_PRESCALER_N;
  66. for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
  67. if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
  68. gd->baudrate) {
  69. best_n = n;
  70. break;
  71. }
  72. }
  73. for (k = 0;; k++) {
  74. if (gd->baudrate <= (max_uns >> (15+n-k)))
  75. break;
  76. }
  77. best_m =
  78. (gd->baudrate * (1 << (15 + n - k))) /
  79. ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
  80. baud = best_m + best_n * YANU_BAUDE;
  81. writel(&uart->baud, baud);
  82. return;
  83. }
  84. #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
  85. int serial_init (void)
  86. {
  87. unsigned action,control;
  88. /* status register cleanup */
  89. action = YANU_ACTION_RRRDY |
  90. YANU_ACTION_RTRDY |
  91. YANU_ACTION_ROE |
  92. YANU_ACTION_RBRK |
  93. YANU_ACTION_RFE |
  94. YANU_ACTION_RPE |
  95. YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
  96. writel(&uart->action, action);
  97. /* control register cleanup */
  98. /* no interrupts enabled */
  99. /* one stop bit */
  100. /* hardware flow control disabled */
  101. /* 8 bits */
  102. control = (0x7 << YANU_CONTROL_BITS_POS);
  103. /* enven parity just to be clean */
  104. control |= YANU_CONTROL_PAREVEN;
  105. /* we set threshold for fifo */
  106. control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY;
  107. control |= YANU_CONTROL_TXTHR * YANU_TXFIFO_THR;
  108. writel(&uart->control, control);
  109. /* to set baud rate */
  110. serial_setbrg();
  111. return (0);
  112. }
  113. /*-----------------------------------------------------------------------
  114. * YANU CONSOLE
  115. *---------------------------------------------------------------------*/
  116. void serial_putc (char c)
  117. {
  118. int tx_chars;
  119. unsigned status;
  120. if (c == '\n')
  121. serial_putc ('\r');
  122. while (1) {
  123. status = readl(&uart->status);
  124. tx_chars = (status>>YANU_TFIFO_CHARS_POS)
  125. & ((1<<YANU_TFIFO_CHARS_N)-1);
  126. if (tx_chars < YANU_TXFIFO_SIZE-1)
  127. break;
  128. WATCHDOG_RESET ();
  129. }
  130. writel(&uart->data, (unsigned char)c);
  131. }
  132. void serial_puts (const char *s)
  133. {
  134. while (*s != 0) {
  135. serial_putc (*s++);
  136. }
  137. }
  138. int serial_tstc(void)
  139. {
  140. unsigned status ;
  141. status = readl(&uart->status);
  142. return (((status >> YANU_RFIFO_CHARS_POS) &
  143. ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
  144. }
  145. int serial_getc (void)
  146. {
  147. while (serial_tstc() == 0)
  148. WATCHDOG_RESET ();
  149. /* first we pull the char */
  150. writel(&uart->action, YANU_ACTION_RFIFO_PULL);
  151. return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
  152. }