serial.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * (C) Copyright 2002
  3. * Lineo, Inc <www.lineo.com>
  4. * Bernhard Kuhn <bkuhn@lineo.com>
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Marius Groeger <mgroeger@sysgo.de>
  9. *
  10. * (C) Copyright 2002
  11. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  12. * Alex Zuepke <azu@sysgo.de>
  13. *
  14. * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. *
  30. */
  31. #include <common.h>
  32. #include <asm/io.h>
  33. #include <asm/arch/hardware.h>
  34. #if !defined(CONFIG_DBGU) && !defined(CONFIG_USART0) && !defined(CONFIG_USART1)
  35. #error must define one of CONFIG_DBGU or CONFIG_USART0 or CONFIG_USART1
  36. #endif
  37. /* ggi thunder */
  38. #ifdef CONFIG_DBGU
  39. AT91PS_USART us = (AT91PS_USART) AT91C_BASE_DBGU;
  40. #endif
  41. #ifdef CONFIG_USART0
  42. AT91PS_USART us = (AT91PS_USART) AT91C_BASE_US0;
  43. #endif
  44. #ifdef CONFIG_USART1
  45. AT91PS_USART us = (AT91PS_USART) AT91C_BASE_US1;
  46. #endif
  47. void serial_setbrg (void)
  48. {
  49. DECLARE_GLOBAL_DATA_PTR;
  50. int baudrate;
  51. if ((baudrate = gd->baudrate) <= 0)
  52. baudrate = CONFIG_BAUDRATE;
  53. if (baudrate == 0 || baudrate == CONFIG_BAUDRATE)
  54. us->US_BRGR = CFG_AT91C_BRGR_DIVISOR; /* hardcode so no __divsi3 */
  55. else
  56. /* MASTER_CLOCK/(16 * baudrate) */
  57. us->US_BRGR = (AT91C_MASTER_CLOCK >> 4)/baudrate;
  58. }
  59. int serial_init (void)
  60. {
  61. /* make any port initializations specific to this port */
  62. #ifdef CONFIG_DBGU
  63. *AT91C_PIOA_PDR = AT91C_PA31_DTXD | AT91C_PA30_DRXD; /* PA 31 & 30 */
  64. *AT91C_PMC_PCER = 1 << AT91C_ID_SYS; /* enable clock */
  65. #endif
  66. #ifdef CONFIG_USART0
  67. *AT91C_PIOA_PDR = AT91C_PA17_TXD0 | AT91C_PA18_RXD0;
  68. *AT91C_PMC_PCER |= 1 << AT91C_ID_USART0; /* enable clock */
  69. #endif
  70. #ifdef CONFIG_USART1
  71. *AT91C_PIOB_PDR = AT91C_PB21_TXD1 | AT91C_PB20_RXD1;
  72. *AT91C_PMC_PCER |= 1 << AT91C_ID_USART1; /* enable clock */
  73. #endif
  74. serial_setbrg ();
  75. us->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX;
  76. us->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
  77. us->US_MR =
  78. (AT91C_US_CLKS_CLOCK | AT91C_US_CHRL_8_BITS |
  79. AT91C_US_PAR_NONE | AT91C_US_NBSTOP_1_BIT);
  80. us->US_IMR = ~0ul;
  81. return (0);
  82. }
  83. void serial_putc (const char c)
  84. {
  85. if (c == '\n')
  86. serial_putc ('\r');
  87. while ((us->US_CSR & AT91C_US_TXRDY) == 0);
  88. us->US_THR = c;
  89. }
  90. void serial_puts (const char *s)
  91. {
  92. while (*s) {
  93. serial_putc (*s++);
  94. }
  95. }
  96. int serial_getc (void)
  97. {
  98. while ((us->US_CSR & AT91C_US_RXRDY) == 0);
  99. return us->US_RHR;
  100. }
  101. int serial_tstc (void)
  102. {
  103. return ((us->US_CSR & AT91C_US_RXRDY) == AT91C_US_RXRDY);
  104. }