atmel_usart.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/clk.h>
  22. #include <asm/arch/memory-map.h>
  23. #if defined(CONFIG_USART0)
  24. # define USART_ID 0
  25. # define USART_BASE USART0_BASE
  26. #elif defined(CONFIG_USART1)
  27. # define USART_ID 1
  28. # define USART_BASE USART1_BASE
  29. #elif defined(CONFIG_USART2)
  30. # define USART_ID 2
  31. # define USART_BASE USART2_BASE
  32. #elif defined(CONFIG_USART3)
  33. # define USART_ID 3
  34. # define USART_BASE USART3_BASE
  35. #endif
  36. #include "atmel_usart.h"
  37. DECLARE_GLOBAL_DATA_PTR;
  38. void serial_setbrg(void)
  39. {
  40. unsigned long divisor;
  41. unsigned long usart_hz;
  42. /*
  43. * Master Clock
  44. * Baud Rate = --------------
  45. * 16 * CD
  46. */
  47. usart_hz = get_usart_clk_rate(USART_ID);
  48. divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate;
  49. usart3_writel(BRGR, USART3_BF(CD, divisor));
  50. }
  51. int serial_init(void)
  52. {
  53. usart3_writel(CR, USART3_BIT(RSTRX) | USART3_BIT(RSTTX));
  54. serial_setbrg();
  55. usart3_writel(CR, USART3_BIT(RXEN) | USART3_BIT(TXEN));
  56. usart3_writel(MR, (USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
  57. | USART3_BF(USCLKS, USART3_USCLKS_MCK)
  58. | USART3_BF(CHRL, USART3_CHRL_8)
  59. | USART3_BF(PAR, USART3_PAR_NONE)
  60. | USART3_BF(NBSTOP, USART3_NBSTOP_1)));
  61. return 0;
  62. }
  63. void serial_putc(char c)
  64. {
  65. if (c == '\n')
  66. serial_putc('\r');
  67. while (!(usart3_readl(CSR) & USART3_BIT(TXRDY))) ;
  68. usart3_writel(THR, c);
  69. }
  70. void serial_puts(const char *s)
  71. {
  72. while (*s)
  73. serial_putc(*s++);
  74. }
  75. int serial_getc(void)
  76. {
  77. while (!(usart3_readl(CSR) & USART3_BIT(RXRDY))) ;
  78. return usart3_readl(RHR);
  79. }
  80. int serial_tstc(void)
  81. {
  82. return (usart3_readl(CSR) & USART3_BIT(RXRDY)) != 0;
  83. }
  84. #endif /* CONFIG_ATMEL_USART */