altera_uart.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
  3. * Scott McNutt <smcnutt@psyent.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <watchdog.h>
  25. #include <asm/io.h>
  26. #include <nios2-io.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. /*------------------------------------------------------------------
  29. * UART the serial port
  30. *-----------------------------------------------------------------*/
  31. static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
  32. #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
  33. /* Everything's already setup for fixed-baud PTF
  34. * assignment
  35. */
  36. void serial_setbrg (void){ return; }
  37. int serial_init (void) { return (0);}
  38. #else
  39. void serial_setbrg (void)
  40. {
  41. unsigned div;
  42. div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
  43. writel (div, &uart->divisor);
  44. return;
  45. }
  46. int serial_init (void)
  47. {
  48. serial_setbrg ();
  49. return (0);
  50. }
  51. #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
  52. /*-----------------------------------------------------------------------
  53. * UART CONSOLE
  54. *---------------------------------------------------------------------*/
  55. void serial_putc (char c)
  56. {
  57. if (c == '\n')
  58. serial_putc ('\r');
  59. while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
  60. WATCHDOG_RESET ();
  61. writel ((unsigned char)c, &uart->txdata);
  62. }
  63. void serial_puts (const char *s)
  64. {
  65. while (*s != 0) {
  66. serial_putc (*s++);
  67. }
  68. }
  69. int serial_tstc (void)
  70. {
  71. return (readl (&uart->status) & NIOS_UART_RRDY);
  72. }
  73. int serial_getc (void)
  74. {
  75. while (serial_tstc () == 0)
  76. WATCHDOG_RESET ();
  77. return (readl (&uart->rxdata) & 0x00ff );
  78. }