opencores_yanu.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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(baud, &uart->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(baud, &uart->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(action, &uart->action);
  97. /*
  98. * control register cleanup
  99. * no interrupts enabled
  100. * one stop bit
  101. * hardware flow control disabled
  102. * 8 bits
  103. */
  104. control = (0x7 << YANU_CONTROL_BITS_POS);
  105. /* enven parity just to be clean */
  106. control |= YANU_CONTROL_PAREVEN;
  107. /* we set threshold for fifo */
  108. control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY;
  109. control |= YANU_CONTROL_TXTHR * YANU_TXFIFO_THR;
  110. writel(control, &uart->control);
  111. /* to set baud rate */
  112. serial_setbrg();
  113. return (0);
  114. }
  115. /*-----------------------------------------------------------------------
  116. * YANU CONSOLE
  117. *---------------------------------------------------------------------*/
  118. void serial_putc (char c)
  119. {
  120. int tx_chars;
  121. unsigned status;
  122. if (c == '\n')
  123. serial_putc ('\r');
  124. while (1) {
  125. status = readl(&uart->status);
  126. tx_chars = (status>>YANU_TFIFO_CHARS_POS)
  127. & ((1<<YANU_TFIFO_CHARS_N)-1);
  128. if (tx_chars < YANU_TXFIFO_SIZE-1)
  129. break;
  130. WATCHDOG_RESET ();
  131. }
  132. writel((unsigned char)c, &uart->data);
  133. }
  134. void serial_puts (const char *s)
  135. {
  136. while (*s != 0) {
  137. serial_putc (*s++);
  138. }
  139. }
  140. int serial_tstc(void)
  141. {
  142. unsigned status ;
  143. status = readl(&uart->status);
  144. return (((status >> YANU_RFIFO_CHARS_POS) &
  145. ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
  146. }
  147. int serial_getc (void)
  148. {
  149. while (serial_tstc() == 0)
  150. WATCHDOG_RESET ();
  151. /* first we pull the char */
  152. writel(YANU_ACTION_RFIFO_PULL, &uart->action);
  153. return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
  154. }