atmel_usart.c 2.3 KB

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