atmel_usart.c 5.5 KB

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