atmel_usart.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
  46. serial_setbrg();
  47. writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
  48. writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
  49. | USART3_BF(USCLKS, USART3_USCLKS_MCK)
  50. | USART3_BF(CHRL, USART3_CHRL_8)
  51. | USART3_BF(PAR, USART3_PAR_NONE)
  52. | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
  53. &usart->mr);
  54. return 0;
  55. }
  56. void serial_putc(char c)
  57. {
  58. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  59. if (c == '\n')
  60. serial_putc('\r');
  61. while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
  62. writel(c, &usart->thr);
  63. }
  64. void serial_puts(const char *s)
  65. {
  66. while (*s)
  67. serial_putc(*s++);
  68. }
  69. int serial_getc(void)
  70. {
  71. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  72. while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
  73. WATCHDOG_RESET();
  74. return readl(&usart->rhr);
  75. }
  76. int serial_tstc(void)
  77. {
  78. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  79. return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
  80. }