atmel_usart.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * Modified to support C structur SoC access by
  5. * Andreas Bießmann <biessmann@corscience.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <common.h>
  22. #include <watchdog.h>
  23. #include <asm/io.h>
  24. #include <asm/arch/clk.h>
  25. #include <asm/arch/memory-map.h>
  26. #if defined(CONFIG_USART0)
  27. # define USART_ID 0
  28. # define USART_BASE USART0_BASE
  29. #elif defined(CONFIG_USART1)
  30. # define USART_ID 1
  31. # define USART_BASE USART1_BASE
  32. #elif defined(CONFIG_USART2)
  33. # define USART_ID 2
  34. # define USART_BASE USART2_BASE
  35. #elif defined(CONFIG_USART3)
  36. # define USART_ID 3
  37. # define USART_BASE USART3_BASE
  38. #endif
  39. #include "atmel_usart.h"
  40. DECLARE_GLOBAL_DATA_PTR;
  41. void serial_setbrg(void)
  42. {
  43. atmel_usart3_t *usart = (atmel_usart3_t*)USART_BASE;
  44. unsigned long divisor;
  45. unsigned long usart_hz;
  46. /*
  47. * Master Clock
  48. * Baud Rate = --------------
  49. * 16 * CD
  50. */
  51. usart_hz = get_usart_clk_rate(USART_ID);
  52. divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate;
  53. writel(USART3_BF(CD, divisor), &usart->brgr);
  54. }
  55. int serial_init(void)
  56. {
  57. atmel_usart3_t *usart = (atmel_usart3_t*)USART_BASE;
  58. writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
  59. serial_setbrg();
  60. writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
  61. writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
  62. | USART3_BF(USCLKS, USART3_USCLKS_MCK)
  63. | USART3_BF(CHRL, USART3_CHRL_8)
  64. | USART3_BF(PAR, USART3_PAR_NONE)
  65. | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
  66. &usart->mr);
  67. return 0;
  68. }
  69. void serial_putc(char c)
  70. {
  71. atmel_usart3_t *usart = (atmel_usart3_t*)USART_BASE;
  72. if (c == '\n')
  73. serial_putc('\r');
  74. while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
  75. writel(c, &usart->thr);
  76. }
  77. void serial_puts(const char *s)
  78. {
  79. while (*s)
  80. serial_putc(*s++);
  81. }
  82. int serial_getc(void)
  83. {
  84. atmel_usart3_t *usart = (atmel_usart3_t*)USART_BASE;
  85. while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
  86. WATCHDOG_RESET();
  87. return readl(&usart->rhr);
  88. }
  89. int serial_tstc(void)
  90. {
  91. atmel_usart3_t *usart = (atmel_usart3_t*)USART_BASE;
  92. return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
  93. }