atmel_usart.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <common.h>
  19. #ifdef CONFIG_ATMEL_USART
  20. #include <asm/io.h>
  21. #include <asm/arch/platform.h>
  22. #include "atmel_usart.h"
  23. DECLARE_GLOBAL_DATA_PTR;
  24. void serial_setbrg(void)
  25. {
  26. unsigned long divisor;
  27. unsigned long usart_hz;
  28. /*
  29. * Master Clock
  30. * Baud Rate = --------------
  31. * 16 * CD
  32. */
  33. usart_hz = pm_get_clock_freq(gd->console_uart->resource[0].u.clock.id);
  34. divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate;
  35. usart3_writel(gd->console_uart, BRGR, USART3_BF(CD, divisor));
  36. }
  37. int serial_init(void)
  38. {
  39. usart3_writel(gd->console_uart, CR,
  40. USART3_BIT(RSTRX) | USART3_BIT(RSTTX));
  41. serial_setbrg();
  42. usart3_writel(gd->console_uart, CR,
  43. USART3_BIT(RXEN) | USART3_BIT(TXEN));
  44. usart3_writel(gd->console_uart, MR,
  45. USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
  46. | USART3_BF(USCLKS, USART3_USCLKS_MCK)
  47. | USART3_BF(CHRL, USART3_CHRL_8)
  48. | USART3_BF(PAR, USART3_PAR_NONE)
  49. | USART3_BF(NBSTOP, USART3_NBSTOP_1));
  50. return 0;
  51. }
  52. void serial_putc(char c)
  53. {
  54. if (c == '\n')
  55. serial_putc('\r');
  56. while (!(usart3_readl(gd->console_uart, CSR) & USART3_BIT(TXRDY))) ;
  57. usart3_writel(gd->console_uart, THR, c);
  58. }
  59. void serial_puts(const char *s)
  60. {
  61. while (*s)
  62. serial_putc(*s++);
  63. }
  64. int serial_getc(void)
  65. {
  66. while (!(usart3_readl(gd->console_uart, CSR) & USART3_BIT(RXRDY))) ;
  67. return usart3_readl(gd->console_uart, RHR);
  68. }
  69. int serial_tstc(void)
  70. {
  71. return (usart3_readl(gd->console_uart, CSR) & USART3_BIT(RXRDY)) != 0;
  72. }
  73. #endif /* CONFIG_ATMEL_USART */