opencores_yanu.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Altera NiosII YANU serial interface by Imagos
  3. * please see http://www.opencores.org/project,yanu for
  4. * information/downloads
  5. *
  6. * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <watchdog.h>
  12. #include <asm/io.h>
  13. #include <serial.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. /*-----------------------------------------------------------------*/
  16. /* YANU Imagos serial port */
  17. /*-----------------------------------------------------------------*/
  18. #define YANU_MAX_PRESCALER_N ((1 << 4) - 1) /* 15 */
  19. #define YANU_MAX_PRESCALER_M ((1 << 11) -1) /* 2047 */
  20. #define YANU_FIFO_SIZE (16)
  21. #define YANU_RXFIFO_SIZE (YANU_FIFO_SIZE)
  22. #define YANU_TXFIFO_SIZE (YANU_FIFO_SIZE)
  23. #define YANU_RXFIFO_DLY (10*11)
  24. #define YANU_TXFIFO_THR (10)
  25. #define YANU_DATA_CHAR_MASK (0xFF)
  26. /* data register */
  27. #define YANU_DATA_OFFSET (0) /* data register offset */
  28. #define YANU_CONTROL_OFFSET (4) /* control register offset */
  29. /* interrupt enable */
  30. #define YANU_CONTROL_IE_RRDY (1<<0) /* ie on received character ready */
  31. #define YANU_CONTROL_IE_OE (1<<1) /* ie on rx overrun */
  32. #define YANU_CONTROL_IE_BRK (1<<2) /* ie on break detect */
  33. #define YANU_CONTROL_IE_FE (1<<3) /* ie on framing error */
  34. #define YANU_CONTROL_IE_PE (1<<4) /* ie on parity error */
  35. #define YANU_CONTROL_IE_TRDY (1<<5) /* ie interrupt on tranmitter ready */
  36. /* control bits */
  37. #define YANU_CONTROL_BITS_POS (6) /* bits number pos */
  38. #define YANU_CONTROL_BITS (1<<YANU_CONTROL_BITS_POS) /* number of rx/tx bits per word. 3 bit unsigned integer */
  39. #define YANU_CONTROL_BITS_N (3) /* ... its bit filed length */
  40. #define YANU_CONTROL_PARENA (1<<9) /* enable parity bit transmission/reception */
  41. #define YANU_CONTROL_PAREVEN (1<<10) /* parity even */
  42. #define YANU_CONTROL_STOPS (1<<11) /* number of stop bits */
  43. #define YANU_CONTROL_HHENA (1<<12) /* Harware Handshake enable... */
  44. #define YANU_CONTROL_FORCEBRK (1<<13) /* if set than txd = active (0) */
  45. /* tuning part */
  46. #define YANU_CONTROL_RDYDLY (1<<14) /* delay from "first" before setting rrdy (in bit) */
  47. #define YANU_CONTROL_RDYDLY_N (8) /* ... its bit filed length */
  48. #define YANU_CONTROL_TXTHR (1<<22) /* tx interrupt threshold: the trdy set if txfifo_chars<= txthr (chars) */
  49. #define YANU_CONTROL_TXTHR_N (4) /* ... its bit field length */
  50. #define YANU_BAUD_OFFSET (8) /* baud register offset */
  51. #define YANU_BAUDM (1<<0) /* baud mantissa lsb */
  52. #define YANU_BAUDM_N (12) /* ...its bit filed length */
  53. #define YANU_BAUDE (1<<12) /* baud exponent lsb */
  54. #define YANU_BAUDE_N (4) /* ...its bit field length */
  55. #define YANU_ACTION_OFFSET (12) /* action register... write only */
  56. #define YANU_ACTION_RRRDY (1<<0) /* reset rrdy */
  57. #define YANU_ACTION_ROE (1<<1) /* reset oe */
  58. #define YANU_ACTION_RBRK (1<<2) /* reset brk */
  59. #define YANU_ACTION_RFE (1<<3) /* reset fe */
  60. #define YANU_ACTION_RPE (1<<4) /* reset pe */
  61. #define YANU_ACTION_SRRDY (1<<5) /* set rrdy */
  62. #define YANU_ACTION_SOE (1<<6) /* set oe */
  63. #define YANU_ACTION_SBRK (1<<7) /* set brk */
  64. #define YANU_ACTION_SFE (1<<8) /* set fe */
  65. #define YANU_ACTION_SPE (1<<9) /* set pe */
  66. #define YANU_ACTION_RFIFO_PULL (1<<10) /* pull a char from rx fifo we MUST do it before taking a char */
  67. #define YANU_ACTION_RFIFO_CLEAR (1<<11) /* clear rx fifo */
  68. #define YANU_ACTION_TFIFO_CLEAR (1<<12) /* clear tx fifo */
  69. #define YANU_ACTION_RTRDY (1<<13) /* clear trdy */
  70. #define YANU_ACTION_STRDY (1<<14) /* set trdy */
  71. #define YANU_STATUS_OFFSET (16)
  72. #define YANU_STATUS_RRDY (1<<0) /* rxrdy flag */
  73. #define YANU_STATUS_TRDY (1<<1) /* txrdy flag */
  74. #define YANU_STATUS_OE (1<<2) /* rx overrun error */
  75. #define YANU_STATUS_BRK (1<<3) /* rx break detect flag */
  76. #define YANU_STATUS_FE (1<<4) /* rx framing error flag */
  77. #define YANU_STATUS_PE (1<<5) /* rx parity erro flag */
  78. #define YANU_RFIFO_CHARS_POS (6)
  79. #define YANU_RFIFO_CHARS (1<<RFIFO_CHAR_POS) /* number of chars into rx fifo */
  80. #define YANU_RFIFO_CHARS_N (5) /* ...its bit field length: 32 chars */
  81. #define YANU_TFIFO_CHARS_POS (11)
  82. #define YANU_TFIFO_CHARS (1<<TFIFO_CHAR_POS) /* number of chars into tx fifo */
  83. #define YANU_TFIFO_CHARS_N (5) /* ...its bit field length: 32 chars */
  84. typedef volatile struct {
  85. volatile unsigned data;
  86. volatile unsigned control; /* control register (RW) 32-bit */
  87. volatile unsigned baud; /* baud/prescaler register (RW) 32-bit */
  88. volatile unsigned action; /* action register (W) 32-bit */
  89. volatile unsigned status; /* status register (R) 32-bit */
  90. volatile unsigned magic; /* magic register (R) 32-bit */
  91. } yanu_uart_t;
  92. static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
  93. static void oc_serial_setbrg(void)
  94. {
  95. int n, k;
  96. const unsigned max_uns = 0xFFFFFFFF;
  97. unsigned best_n, best_m, baud;
  98. unsigned baudrate;
  99. #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
  100. /* Everything's already setup for fixed-baud PTF assignment */
  101. baudrate = CONFIG_BAUDRATE;
  102. #else
  103. baudrate = gd->baudrate;
  104. #endif
  105. /* compute best N and M couple */
  106. best_n = YANU_MAX_PRESCALER_N;
  107. for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
  108. if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
  109. baudrate) {
  110. best_n = n;
  111. break;
  112. }
  113. }
  114. for (k = 0;; k++) {
  115. if (baudrate <= (max_uns >> (15+n-k)))
  116. break;
  117. }
  118. best_m =
  119. (baudrate * (1 << (15 + n - k))) /
  120. ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
  121. baud = best_m + best_n * YANU_BAUDE;
  122. writel(baud, &uart->baud);
  123. return;
  124. }
  125. static int oc_serial_init(void)
  126. {
  127. unsigned action,control;
  128. /* status register cleanup */
  129. action = YANU_ACTION_RRRDY |
  130. YANU_ACTION_RTRDY |
  131. YANU_ACTION_ROE |
  132. YANU_ACTION_RBRK |
  133. YANU_ACTION_RFE |
  134. YANU_ACTION_RPE |
  135. YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
  136. writel(action, &uart->action);
  137. /*
  138. * control register cleanup
  139. * no interrupts enabled
  140. * one stop bit
  141. * hardware flow control disabled
  142. * 8 bits
  143. */
  144. control = (0x7 << YANU_CONTROL_BITS_POS);
  145. /* enven parity just to be clean */
  146. control |= YANU_CONTROL_PAREVEN;
  147. /* we set threshold for fifo */
  148. control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY;
  149. control |= YANU_CONTROL_TXTHR * YANU_TXFIFO_THR;
  150. writel(control, &uart->control);
  151. /* to set baud rate */
  152. serial_setbrg();
  153. return (0);
  154. }
  155. /*-----------------------------------------------------------------------
  156. * YANU CONSOLE
  157. *---------------------------------------------------------------------*/
  158. static void oc_serial_putc(char c)
  159. {
  160. int tx_chars;
  161. unsigned status;
  162. if (c == '\n')
  163. serial_putc ('\r');
  164. while (1) {
  165. status = readl(&uart->status);
  166. tx_chars = (status>>YANU_TFIFO_CHARS_POS)
  167. & ((1<<YANU_TFIFO_CHARS_N)-1);
  168. if (tx_chars < YANU_TXFIFO_SIZE-1)
  169. break;
  170. WATCHDOG_RESET ();
  171. }
  172. writel((unsigned char)c, &uart->data);
  173. }
  174. static int oc_serial_tstc(void)
  175. {
  176. unsigned status ;
  177. status = readl(&uart->status);
  178. return (((status >> YANU_RFIFO_CHARS_POS) &
  179. ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
  180. }
  181. static int oc_serial_getc(void)
  182. {
  183. while (serial_tstc() == 0)
  184. WATCHDOG_RESET ();
  185. /* first we pull the char */
  186. writel(YANU_ACTION_RFIFO_PULL, &uart->action);
  187. return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
  188. }
  189. static struct serial_device oc_serial_drv = {
  190. .name = "oc_serial",
  191. .start = oc_serial_init,
  192. .stop = NULL,
  193. .setbrg = oc_serial_setbrg,
  194. .putc = oc_serial_putc,
  195. .puts = default_serial_puts,
  196. .getc = oc_serial_getc,
  197. .tstc = oc_serial_tstc,
  198. };
  199. void oc_serial_initialize(void)
  200. {
  201. serial_register(&oc_serial_drv);
  202. }
  203. __weak struct serial_device *default_serial_console(void)
  204. {
  205. return &oc_serial_drv;
  206. }