atmel_usart.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <watchdog.h>
  13. #include <serial.h>
  14. #include <linux/compiler.h>
  15. #include <asm/io.h>
  16. #ifdef CONFIG_DM_SERIAL
  17. #include <asm/arch/atmel_serial.h>
  18. #endif
  19. #include <asm/arch/clk.h>
  20. #include <asm/arch/hardware.h>
  21. #include "atmel_usart.h"
  22. DECLARE_GLOBAL_DATA_PTR;
  23. static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
  24. int baudrate)
  25. {
  26. unsigned long divisor;
  27. unsigned long usart_hz;
  28. /*
  29. * Master Clock
  30. * Baud Rate = --------------
  31. * 16 * CD
  32. */
  33. usart_hz = get_usart_clk_rate(id);
  34. divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
  35. writel(USART3_BF(CD, divisor), &usart->brgr);
  36. }
  37. static void atmel_serial_init_internal(atmel_usart3_t *usart)
  38. {
  39. /*
  40. * Just in case: drain transmitter register
  41. * 1000us is enough for baudrate >= 9600
  42. */
  43. if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
  44. __udelay(1000);
  45. writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
  46. }
  47. static void atmel_serial_activate(atmel_usart3_t *usart)
  48. {
  49. writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
  50. | USART3_BF(USCLKS, USART3_USCLKS_MCK)
  51. | USART3_BF(CHRL, USART3_CHRL_8)
  52. | USART3_BF(PAR, USART3_PAR_NONE)
  53. | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
  54. &usart->mr);
  55. writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
  56. /* 100us is enough for the new settings to be settled */
  57. __udelay(100);
  58. }
  59. #ifndef CONFIG_DM_SERIAL
  60. static void atmel_serial_setbrg(void)
  61. {
  62. atmel_serial_setbrg_internal((atmel_usart3_t *)CONFIG_USART_BASE,
  63. CONFIG_USART_ID, gd->baudrate);
  64. }
  65. static int atmel_serial_init(void)
  66. {
  67. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  68. atmel_serial_init_internal(usart);
  69. serial_setbrg();
  70. atmel_serial_activate(usart);
  71. return 0;
  72. }
  73. static void atmel_serial_putc(char c)
  74. {
  75. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  76. if (c == '\n')
  77. serial_putc('\r');
  78. while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
  79. writel(c, &usart->thr);
  80. }
  81. static int atmel_serial_getc(void)
  82. {
  83. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  84. while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
  85. WATCHDOG_RESET();
  86. return readl(&usart->rhr);
  87. }
  88. static int atmel_serial_tstc(void)
  89. {
  90. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_USART_BASE;
  91. return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
  92. }
  93. static struct serial_device atmel_serial_drv = {
  94. .name = "atmel_serial",
  95. .start = atmel_serial_init,
  96. .stop = NULL,
  97. .setbrg = atmel_serial_setbrg,
  98. .putc = atmel_serial_putc,
  99. .puts = default_serial_puts,
  100. .getc = atmel_serial_getc,
  101. .tstc = atmel_serial_tstc,
  102. };
  103. void atmel_serial_initialize(void)
  104. {
  105. serial_register(&atmel_serial_drv);
  106. }
  107. __weak struct serial_device *default_serial_console(void)
  108. {
  109. return &atmel_serial_drv;
  110. }
  111. #endif
  112. #ifdef CONFIG_DM_SERIAL
  113. struct atmel_serial_priv {
  114. atmel_usart3_t *usart;
  115. };
  116. int atmel_serial_setbrg(struct udevice *dev, int baudrate)
  117. {
  118. struct atmel_serial_priv *priv = dev_get_priv(dev);
  119. atmel_serial_setbrg_internal(priv->usart, 0 /* ignored */, baudrate);
  120. atmel_serial_activate(priv->usart);
  121. return 0;
  122. }
  123. static int atmel_serial_getc(struct udevice *dev)
  124. {
  125. struct atmel_serial_priv *priv = dev_get_priv(dev);
  126. if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY)))
  127. return -EAGAIN;
  128. return readl(&priv->usart->rhr);
  129. }
  130. static int atmel_serial_putc(struct udevice *dev, const char ch)
  131. {
  132. struct atmel_serial_priv *priv = dev_get_priv(dev);
  133. if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY)))
  134. return -EAGAIN;
  135. writel(ch, &priv->usart->thr);
  136. return 0;
  137. }
  138. static int atmel_serial_pending(struct udevice *dev, bool input)
  139. {
  140. struct atmel_serial_priv *priv = dev_get_priv(dev);
  141. uint32_t csr = readl(&priv->usart->csr);
  142. if (input)
  143. return csr & USART3_BIT(RXRDY) ? 1 : 0;
  144. else
  145. return csr & USART3_BIT(TXEMPTY) ? 0 : 1;
  146. }
  147. static const struct dm_serial_ops atmel_serial_ops = {
  148. .putc = atmel_serial_putc,
  149. .pending = atmel_serial_pending,
  150. .getc = atmel_serial_getc,
  151. .setbrg = atmel_serial_setbrg,
  152. };
  153. static int atmel_serial_probe(struct udevice *dev)
  154. {
  155. struct atmel_serial_platdata *plat = dev->platdata;
  156. struct atmel_serial_priv *priv = dev_get_priv(dev);
  157. #if CONFIG_IS_ENABLED(OF_CONTROL)
  158. fdt_addr_t addr_base;
  159. addr_base = dev_get_addr(dev);
  160. if (addr_base == FDT_ADDR_T_NONE)
  161. return -ENODEV;
  162. plat->base_addr = (uint32_t)addr_base;
  163. #endif
  164. priv->usart = (atmel_usart3_t *)plat->base_addr;
  165. atmel_serial_init_internal(priv->usart);
  166. return 0;
  167. }
  168. #if CONFIG_IS_ENABLED(OF_CONTROL)
  169. static const struct udevice_id atmel_serial_ids[] = {
  170. { .compatible = "atmel,at91sam9260-usart" },
  171. { }
  172. };
  173. #endif
  174. U_BOOT_DRIVER(serial_atmel) = {
  175. .name = "serial_atmel",
  176. .id = UCLASS_SERIAL,
  177. #if CONFIG_IS_ENABLED(OF_CONTROL)
  178. .of_match = atmel_serial_ids,
  179. .platdata_auto_alloc_size = sizeof(struct atmel_serial_platdata),
  180. #endif
  181. .probe = atmel_serial_probe,
  182. .ops = &atmel_serial_ops,
  183. .flags = DM_FLAG_PRE_RELOC,
  184. .priv_auto_alloc_size = sizeof(struct atmel_serial_priv),
  185. };
  186. #endif