opencores_yanu.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. static void oc_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. static void oc_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. static int oc_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. static void oc_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. static void oc_serial_puts(const char *s)
  135. {
  136. while (*s != 0) {
  137. serial_putc (*s++);
  138. }
  139. }
  140. static int oc_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. statoc int oc_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. }
  155. #ifdef CONFIG_SERIAL_MULTI
  156. static struct serial_device oc_serial_drv = {
  157. .name = "oc_serial",
  158. .start = oc_serial_init,
  159. .stop = NULL,
  160. .setbrg = oc_serial_setbrg,
  161. .putc = oc_serial_putc,
  162. .puts = oc_serial_puts,
  163. .getc = oc_serial_getc,
  164. .tstc = oc_serial_tstc,
  165. };
  166. void oc_serial_initialize(void)
  167. {
  168. serial_register(&oc_serial_drv);
  169. }
  170. __weak struct serial_device *default_serial_console(void)
  171. {
  172. return &oc_serial_drv;
  173. }
  174. #else
  175. int serial_init(void)
  176. {
  177. return oc_serial_init();
  178. }
  179. void serial_setbrg(void)
  180. {
  181. oc_serial_setbrg();
  182. }
  183. void serial_putc(const char c)
  184. {
  185. oc_serial_putc(c);
  186. }
  187. void serial_puts(const char *s)
  188. {
  189. oc_serial_puts(s);
  190. }
  191. int serial_getc(void)
  192. {
  193. return oc_serial_getc();
  194. }
  195. int serial_tstc(void)
  196. {
  197. return oc_serial_tstc();
  198. }
  199. #endif