altera_uart.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <linux/compiler.h>
  28. #include <serial.h>
  29. DECLARE_GLOBAL_DATA_PTR;
  30. /*------------------------------------------------------------------
  31. * UART the serial port
  32. *-----------------------------------------------------------------*/
  33. static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
  34. #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
  35. /*
  36. * Everything's already setup for fixed-baud PTF
  37. * assignment
  38. */
  39. static void altera_serial_setbrg(void)
  40. {
  41. }
  42. static int altera_serial_init(void)
  43. {
  44. return 0;
  45. }
  46. #else
  47. static void altera_serial_setbrg(void)
  48. {
  49. unsigned div;
  50. div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
  51. writel (div, &uart->divisor);
  52. }
  53. static int altera_serial_init(void)
  54. {
  55. serial_setbrg();
  56. return 0;
  57. }
  58. #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
  59. /*-----------------------------------------------------------------------
  60. * UART CONSOLE
  61. *---------------------------------------------------------------------*/
  62. static void altera_serial_putc(char c)
  63. {
  64. if (c == '\n')
  65. serial_putc ('\r');
  66. while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
  67. WATCHDOG_RESET ();
  68. writel ((unsigned char)c, &uart->txdata);
  69. }
  70. static void altera_serial_puts(const char *s)
  71. {
  72. while (*s != 0) {
  73. serial_putc (*s++);
  74. }
  75. }
  76. static int altera_serial_tstc(void)
  77. {
  78. return (readl (&uart->status) & NIOS_UART_RRDY);
  79. }
  80. static int altera_serial_getc(void)
  81. {
  82. while (serial_tstc () == 0)
  83. WATCHDOG_RESET ();
  84. return (readl (&uart->rxdata) & 0x00ff );
  85. }
  86. static struct serial_device altera_serial_drv = {
  87. .name = "altera_serial",
  88. .start = altera_serial_init,
  89. .stop = NULL,
  90. .setbrg = altera_serial_setbrg,
  91. .putc = altera_serial_putc,
  92. .puts = altera_serial_puts,
  93. .getc = altera_serial_getc,
  94. .tstc = altera_serial_tstc,
  95. };
  96. void altera_serial_initialize(void)
  97. {
  98. serial_register(&altera_serial_drv);
  99. }
  100. __weak struct serial_device *default_serial_console(void)
  101. {
  102. return &altera_serial_drv;
  103. }