atmel_usart.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2004-2006 Atmel Corporation
  4. *
  5. * Modified to support C structur SoC access by
  6. * Andreas Bießmann <biessmann@corscience.de>
  7. */
  8. #include <common.h>
  9. #include <clk.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. #ifndef CONFIG_DM_SERIAL
  25. static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
  26. int baudrate)
  27. {
  28. unsigned long divisor;
  29. unsigned long usart_hz;
  30. /*
  31. * Master Clock
  32. * Baud Rate = --------------
  33. * 16 * CD
  34. */
  35. usart_hz = get_usart_clk_rate(id);
  36. divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
  37. writel(USART3_BF(CD, divisor), &usart->brgr);
  38. }
  39. static void atmel_serial_init_internal(atmel_usart3_t *usart)
  40. {
  41. /*
  42. * Just in case: drain transmitter register
  43. * 1000us is enough for baudrate >= 9600
  44. */
  45. if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
  46. __udelay(1000);
  47. writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
  48. }
  49. static void atmel_serial_activate(atmel_usart3_t *usart)
  50. {
  51. writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
  52. | USART3_BF(USCLKS, USART3_USCLKS_MCK)
  53. | USART3_BF(CHRL, USART3_CHRL_8)
  54. | USART3_BF(PAR, USART3_PAR_NONE)
  55. | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
  56. &usart->mr);
  57. writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
  58. /* 100us is enough for the new settings to be settled */
  59. __udelay(100);
  60. }
  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. enum serial_clk_type {
  115. CLK_TYPE_NORMAL = 0,
  116. CLK_TYPE_DBGU,
  117. };
  118. struct atmel_serial_priv {
  119. atmel_usart3_t *usart;
  120. ulong usart_clk_rate;
  121. };
  122. static void _atmel_serial_set_brg(atmel_usart3_t *usart,
  123. ulong usart_clk_rate, int baudrate)
  124. {
  125. unsigned long divisor;
  126. divisor = (usart_clk_rate / 16 + baudrate / 2) / baudrate;
  127. writel(USART3_BF(CD, divisor), &usart->brgr);
  128. }
  129. void _atmel_serial_init(atmel_usart3_t *usart,
  130. ulong usart_clk_rate, int baudrate)
  131. {
  132. writel(USART3_BIT(RXDIS) | USART3_BIT(TXDIS), &usart->cr);
  133. writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL) |
  134. USART3_BF(USCLKS, USART3_USCLKS_MCK) |
  135. USART3_BF(CHRL, USART3_CHRL_8) |
  136. USART3_BF(PAR, USART3_PAR_NONE) |
  137. USART3_BF(NBSTOP, USART3_NBSTOP_1)), &usart->mr);
  138. _atmel_serial_set_brg(usart, usart_clk_rate, baudrate);
  139. writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
  140. writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
  141. }
  142. int atmel_serial_setbrg(struct udevice *dev, int baudrate)
  143. {
  144. struct atmel_serial_priv *priv = dev_get_priv(dev);
  145. _atmel_serial_set_brg(priv->usart, priv->usart_clk_rate, baudrate);
  146. return 0;
  147. }
  148. static int atmel_serial_getc(struct udevice *dev)
  149. {
  150. struct atmel_serial_priv *priv = dev_get_priv(dev);
  151. if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY)))
  152. return -EAGAIN;
  153. return readl(&priv->usart->rhr);
  154. }
  155. static int atmel_serial_putc(struct udevice *dev, const char ch)
  156. {
  157. struct atmel_serial_priv *priv = dev_get_priv(dev);
  158. if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY)))
  159. return -EAGAIN;
  160. writel(ch, &priv->usart->thr);
  161. return 0;
  162. }
  163. static int atmel_serial_pending(struct udevice *dev, bool input)
  164. {
  165. struct atmel_serial_priv *priv = dev_get_priv(dev);
  166. uint32_t csr = readl(&priv->usart->csr);
  167. if (input)
  168. return csr & USART3_BIT(RXRDY) ? 1 : 0;
  169. else
  170. return csr & USART3_BIT(TXEMPTY) ? 0 : 1;
  171. }
  172. static const struct dm_serial_ops atmel_serial_ops = {
  173. .putc = atmel_serial_putc,
  174. .pending = atmel_serial_pending,
  175. .getc = atmel_serial_getc,
  176. .setbrg = atmel_serial_setbrg,
  177. };
  178. static int atmel_serial_enable_clk(struct udevice *dev)
  179. {
  180. struct atmel_serial_priv *priv = dev_get_priv(dev);
  181. struct clk clk;
  182. ulong clk_rate;
  183. int ret;
  184. ret = clk_get_by_index(dev, 0, &clk);
  185. if (ret)
  186. return -EINVAL;
  187. if (dev_get_driver_data(dev) == CLK_TYPE_NORMAL) {
  188. ret = clk_enable(&clk);
  189. if (ret)
  190. return ret;
  191. }
  192. clk_rate = clk_get_rate(&clk);
  193. if (!clk_rate)
  194. return -EINVAL;
  195. priv->usart_clk_rate = clk_rate;
  196. clk_free(&clk);
  197. return 0;
  198. }
  199. static int atmel_serial_probe(struct udevice *dev)
  200. {
  201. struct atmel_serial_platdata *plat = dev->platdata;
  202. struct atmel_serial_priv *priv = dev_get_priv(dev);
  203. int ret;
  204. #if CONFIG_IS_ENABLED(OF_CONTROL)
  205. fdt_addr_t addr_base;
  206. addr_base = devfdt_get_addr(dev);
  207. if (addr_base == FDT_ADDR_T_NONE)
  208. return -ENODEV;
  209. plat->base_addr = (uint32_t)addr_base;
  210. #endif
  211. priv->usart = (atmel_usart3_t *)plat->base_addr;
  212. ret = atmel_serial_enable_clk(dev);
  213. if (ret)
  214. return ret;
  215. _atmel_serial_init(priv->usart, priv->usart_clk_rate, gd->baudrate);
  216. return 0;
  217. }
  218. #if CONFIG_IS_ENABLED(OF_CONTROL)
  219. static const struct udevice_id atmel_serial_ids[] = {
  220. {
  221. .compatible = "atmel,at91sam9260-dbgu",
  222. .data = CLK_TYPE_DBGU,
  223. },
  224. {
  225. .compatible = "atmel,at91sam9260-usart",
  226. .data = CLK_TYPE_NORMAL,
  227. },
  228. { }
  229. };
  230. #endif
  231. U_BOOT_DRIVER(serial_atmel) = {
  232. .name = "serial_atmel",
  233. .id = UCLASS_SERIAL,
  234. #if CONFIG_IS_ENABLED(OF_CONTROL)
  235. .of_match = atmel_serial_ids,
  236. .platdata_auto_alloc_size = sizeof(struct atmel_serial_platdata),
  237. #endif
  238. .probe = atmel_serial_probe,
  239. .ops = &atmel_serial_ops,
  240. .flags = DM_FLAG_PRE_RELOC,
  241. .priv_auto_alloc_size = sizeof(struct atmel_serial_priv),
  242. };
  243. #endif
  244. #ifdef CONFIG_DEBUG_UART_ATMEL
  245. static inline void _debug_uart_init(void)
  246. {
  247. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE;
  248. _atmel_serial_init(usart, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
  249. }
  250. static inline void _debug_uart_putc(int ch)
  251. {
  252. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_DEBUG_UART_BASE;
  253. while (!(readl(&usart->csr) & USART3_BIT(TXRDY)))
  254. ;
  255. writel(ch, &usart->thr);
  256. }
  257. DEBUG_UART_FUNCS
  258. #endif