atmel_usart.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/hardware.h>
  26. #include "atmel_usart.h"
  27. DECLARE_GLOBAL_DATA_PTR;
  28. void serial_setbrg(void)
  29. {
  30. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  31. unsigned long divisor;
  32. unsigned long usart_hz;
  33. /*
  34. * Master Clock
  35. * Baud Rate = --------------
  36. * 16 * CD
  37. */
  38. usart_hz = get_usart_clk_rate(CONFIG_USART_ID);
  39. divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate;
  40. writel(USART3_BF(CD, divisor), &usart->brgr);
  41. }
  42. int serial_init(void)
  43. {
  44. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  45. /*
  46. * Just in case: drain transmitter register
  47. * 1000us is enough for baudrate >= 9600
  48. */
  49. if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
  50. __udelay(1000);
  51. writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
  52. serial_setbrg();
  53. writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
  54. | USART3_BF(USCLKS, USART3_USCLKS_MCK)
  55. | USART3_BF(CHRL, USART3_CHRL_8)
  56. | USART3_BF(PAR, USART3_PAR_NONE)
  57. | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
  58. &usart->mr);
  59. writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
  60. /* 100us is enough for the new settings to be settled */
  61. __udelay(100);
  62. return 0;
  63. }
  64. void serial_putc(char c)
  65. {
  66. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  67. if (c == '\n')
  68. serial_putc('\r');
  69. while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
  70. writel(c, &usart->thr);
  71. }
  72. void serial_puts(const char *s)
  73. {
  74. while (*s)
  75. serial_putc(*s++);
  76. }
  77. int serial_getc(void)
  78. {
  79. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  80. while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
  81. WATCHDOG_RESET();
  82. return readl(&usart->rhr);
  83. }
  84. int serial_tstc(void)
  85. {
  86. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  87. return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
  88. }