atmel_usart.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <serial.h>
  24. #include <linux/compiler.h>
  25. #include <asm/io.h>
  26. #include <asm/arch/clk.h>
  27. #include <asm/arch/hardware.h>
  28. #include "atmel_usart.h"
  29. DECLARE_GLOBAL_DATA_PTR;
  30. static void atmel_serial_setbrg(void)
  31. {
  32. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  33. unsigned long divisor;
  34. unsigned long usart_hz;
  35. /*
  36. * Master Clock
  37. * Baud Rate = --------------
  38. * 16 * CD
  39. */
  40. usart_hz = get_usart_clk_rate(CONFIG_USART_ID);
  41. divisor = (usart_hz / 16 + gd->baudrate / 2) / gd->baudrate;
  42. writel(USART3_BF(CD, divisor), &usart->brgr);
  43. }
  44. static int atmel_serial_init(void)
  45. {
  46. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  47. /*
  48. * Just in case: drain transmitter register
  49. * 1000us is enough for baudrate >= 9600
  50. */
  51. if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
  52. __udelay(1000);
  53. writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
  54. serial_setbrg();
  55. writel((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. &usart->mr);
  61. writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
  62. /* 100us is enough for the new settings to be settled */
  63. __udelay(100);
  64. return 0;
  65. }
  66. static void atmel_serial_putc(char c)
  67. {
  68. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  69. if (c == '\n')
  70. serial_putc('\r');
  71. while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
  72. writel(c, &usart->thr);
  73. }
  74. static int atmel_serial_getc(void)
  75. {
  76. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  77. while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
  78. WATCHDOG_RESET();
  79. return readl(&usart->rhr);
  80. }
  81. static int atmel_serial_tstc(void)
  82. {
  83. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  84. return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
  85. }
  86. static struct serial_device atmel_serial_drv = {
  87. .name = "atmel_serial",
  88. .start = atmel_serial_init,
  89. .stop = NULL,
  90. .setbrg = atmel_serial_setbrg,
  91. .putc = atmel_serial_putc,
  92. .puts = default_serial_puts,
  93. .getc = atmel_serial_getc,
  94. .tstc = atmel_serial_tstc,
  95. };
  96. void atmel_serial_initialize(void)
  97. {
  98. serial_register(&atmel_serial_drv);
  99. }
  100. __weak struct serial_device *default_serial_console(void)
  101. {
  102. return &atmel_serial_drv;
  103. }