atmel_usart.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 <malloc.h>
  13. #include <watchdog.h>
  14. #include <serial.h>
  15. #include <debug_uart.h>
  16. #include <asm/global_data.h>
  17. #include <linux/compiler.h>
  18. #include <linux/delay.h>
  19. #include <asm/io.h>
  20. #if CONFIG_IS_ENABLED(DM_SERIAL)
  21. #include <asm/arch/atmel_serial.h>
  22. #endif
  23. #include <asm/arch/clk.h>
  24. #include <asm/arch/hardware.h>
  25. #include "atmel_usart.h"
  26. DECLARE_GLOBAL_DATA_PTR;
  27. #if !CONFIG_IS_ENABLED(DM_SERIAL)
  28. static void atmel_serial_setbrg_internal(atmel_usart3_t *usart, int id,
  29. int baudrate)
  30. {
  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(id);
  39. divisor = (usart_hz / 16 + baudrate / 2) / baudrate;
  40. writel(USART3_BF(CD, divisor), &usart->brgr);
  41. }
  42. static void atmel_serial_init_internal(atmel_usart3_t *usart)
  43. {
  44. /*
  45. * Just in case: drain transmitter register
  46. * 1000us is enough for baudrate >= 9600
  47. */
  48. if (!(readl(&usart->csr) & USART3_BIT(TXEMPTY)))
  49. __udelay(1000);
  50. writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
  51. }
  52. static void atmel_serial_activate(atmel_usart3_t *usart)
  53. {
  54. writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL)
  55. | USART3_BF(USCLKS, USART3_USCLKS_MCK)
  56. | USART3_BF(CHRL, USART3_CHRL_8)
  57. | USART3_BF(PAR, USART3_PAR_NONE)
  58. | USART3_BF(NBSTOP, USART3_NBSTOP_1)),
  59. &usart->mr);
  60. writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
  61. /* 100us is enough for the new settings to be settled */
  62. __udelay(100);
  63. }
  64. static void atmel_serial_setbrg(void)
  65. {
  66. atmel_serial_setbrg_internal((atmel_usart3_t *)CFG_USART_BASE,
  67. CFG_USART_ID, gd->baudrate);
  68. }
  69. static int atmel_serial_init(void)
  70. {
  71. atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
  72. atmel_serial_init_internal(usart);
  73. serial_setbrg();
  74. atmel_serial_activate(usart);
  75. return 0;
  76. }
  77. static void atmel_serial_putc(char c)
  78. {
  79. atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
  80. if (c == '\n')
  81. serial_putc('\r');
  82. while (!(readl(&usart->csr) & USART3_BIT(TXRDY)));
  83. writel(c, &usart->thr);
  84. }
  85. static int atmel_serial_getc(void)
  86. {
  87. atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
  88. while (!(readl(&usart->csr) & USART3_BIT(RXRDY)))
  89. schedule();
  90. return readl(&usart->rhr);
  91. }
  92. static int atmel_serial_tstc(void)
  93. {
  94. atmel_usart3_t *usart = (atmel_usart3_t *)CFG_USART_BASE;
  95. return (readl(&usart->csr) & USART3_BIT(RXRDY)) != 0;
  96. }
  97. static struct serial_device atmel_serial_drv = {
  98. .name = "atmel_serial",
  99. .start = atmel_serial_init,
  100. .stop = NULL,
  101. .setbrg = atmel_serial_setbrg,
  102. .putc = atmel_serial_putc,
  103. .puts = default_serial_puts,
  104. .getc = atmel_serial_getc,
  105. .tstc = atmel_serial_tstc,
  106. };
  107. void atmel_serial_initialize(void)
  108. {
  109. serial_register(&atmel_serial_drv);
  110. }
  111. __weak struct serial_device *default_serial_console(void)
  112. {
  113. return &atmel_serial_drv;
  114. }
  115. #else
  116. enum serial_clk_type {
  117. CLK_TYPE_NORMAL = 0,
  118. CLK_TYPE_DBGU,
  119. };
  120. struct atmel_serial_priv {
  121. atmel_usart3_t *usart;
  122. ulong usart_clk_rate;
  123. };
  124. static void _atmel_serial_set_brg(atmel_usart3_t *usart,
  125. ulong usart_clk_rate, int baudrate)
  126. {
  127. unsigned long divisor;
  128. divisor = (usart_clk_rate / 16 + baudrate / 2) / baudrate;
  129. writel(USART3_BF(CD, divisor), &usart->brgr);
  130. }
  131. void _atmel_serial_init(atmel_usart3_t *usart,
  132. ulong usart_clk_rate, int baudrate)
  133. {
  134. writel(USART3_BIT(RXDIS) | USART3_BIT(TXDIS), &usart->cr);
  135. writel((USART3_BF(USART_MODE, USART3_USART_MODE_NORMAL) |
  136. USART3_BF(USCLKS, USART3_USCLKS_MCK) |
  137. USART3_BF(CHRL, USART3_CHRL_8) |
  138. USART3_BF(PAR, USART3_PAR_NONE) |
  139. USART3_BF(NBSTOP, USART3_NBSTOP_1)), &usart->mr);
  140. _atmel_serial_set_brg(usart, usart_clk_rate, baudrate);
  141. writel(USART3_BIT(RSTRX) | USART3_BIT(RSTTX), &usart->cr);
  142. writel(USART3_BIT(RXEN) | USART3_BIT(TXEN), &usart->cr);
  143. }
  144. int atmel_serial_setbrg(struct udevice *dev, int baudrate)
  145. {
  146. struct atmel_serial_priv *priv = dev_get_priv(dev);
  147. _atmel_serial_set_brg(priv->usart, priv->usart_clk_rate, baudrate);
  148. return 0;
  149. }
  150. static int atmel_serial_getc(struct udevice *dev)
  151. {
  152. struct atmel_serial_priv *priv = dev_get_priv(dev);
  153. if (!(readl(&priv->usart->csr) & USART3_BIT(RXRDY)))
  154. return -EAGAIN;
  155. return readl(&priv->usart->rhr);
  156. }
  157. static int atmel_serial_putc(struct udevice *dev, const char ch)
  158. {
  159. struct atmel_serial_priv *priv = dev_get_priv(dev);
  160. if (!(readl(&priv->usart->csr) & USART3_BIT(TXRDY)))
  161. return -EAGAIN;
  162. writel(ch, &priv->usart->thr);
  163. return 0;
  164. }
  165. static int atmel_serial_pending(struct udevice *dev, bool input)
  166. {
  167. struct atmel_serial_priv *priv = dev_get_priv(dev);
  168. uint32_t csr = readl(&priv->usart->csr);
  169. if (input)
  170. return csr & USART3_BIT(RXRDY) ? 1 : 0;
  171. else
  172. return csr & USART3_BIT(TXEMPTY) ? 0 : 1;
  173. }
  174. static const struct dm_serial_ops atmel_serial_ops = {
  175. .putc = atmel_serial_putc,
  176. .pending = atmel_serial_pending,
  177. .getc = atmel_serial_getc,
  178. .setbrg = atmel_serial_setbrg,
  179. };
  180. #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_SPL_CLK)
  181. static int atmel_serial_enable_clk(struct udevice *dev)
  182. {
  183. struct atmel_serial_priv *priv = dev_get_priv(dev);
  184. /* Use fixed clock value in SPL */
  185. priv->usart_clk_rate = CONFIG_SPL_UART_CLOCK;
  186. return 0;
  187. }
  188. #else
  189. static int atmel_serial_enable_clk(struct udevice *dev)
  190. {
  191. struct atmel_serial_priv *priv = dev_get_priv(dev);
  192. struct clk clk;
  193. ulong clk_rate;
  194. int ret;
  195. ret = clk_get_by_index(dev, 0, &clk);
  196. if (ret)
  197. return -EINVAL;
  198. if (dev_get_driver_data(dev) == CLK_TYPE_NORMAL) {
  199. ret = clk_enable(&clk);
  200. if (ret)
  201. return ret;
  202. }
  203. clk_rate = clk_get_rate(&clk);
  204. if (!clk_rate)
  205. return -EINVAL;
  206. priv->usart_clk_rate = clk_rate;
  207. clk_free(&clk);
  208. return 0;
  209. }
  210. #endif
  211. static int atmel_serial_probe(struct udevice *dev)
  212. {
  213. struct atmel_serial_plat *plat = dev_get_plat(dev);
  214. struct atmel_serial_priv *priv = dev_get_priv(dev);
  215. int ret;
  216. #if CONFIG_IS_ENABLED(OF_CONTROL)
  217. fdt_addr_t addr_base;
  218. addr_base = dev_read_addr(dev);
  219. if (addr_base == FDT_ADDR_T_NONE)
  220. return -ENODEV;
  221. plat->base_addr = (uint32_t)addr_base;
  222. #endif
  223. priv->usart = (atmel_usart3_t *)plat->base_addr;
  224. ret = atmel_serial_enable_clk(dev);
  225. if (ret)
  226. return ret;
  227. _atmel_serial_init(priv->usart, priv->usart_clk_rate, gd->baudrate);
  228. return 0;
  229. }
  230. #if CONFIG_IS_ENABLED(OF_CONTROL)
  231. static const struct udevice_id atmel_serial_ids[] = {
  232. {
  233. .compatible = "atmel,at91sam9260-dbgu",
  234. .data = CLK_TYPE_DBGU,
  235. },
  236. {
  237. .compatible = "atmel,at91sam9260-usart",
  238. .data = CLK_TYPE_NORMAL,
  239. },
  240. { }
  241. };
  242. #endif
  243. U_BOOT_DRIVER(serial_atmel) = {
  244. .name = "serial_atmel",
  245. .id = UCLASS_SERIAL,
  246. #if CONFIG_IS_ENABLED(OF_CONTROL)
  247. .of_match = atmel_serial_ids,
  248. .plat_auto = sizeof(struct atmel_serial_plat),
  249. #endif
  250. .probe = atmel_serial_probe,
  251. .ops = &atmel_serial_ops,
  252. #if !CONFIG_IS_ENABLED(OF_CONTROL)
  253. .flags = DM_FLAG_PRE_RELOC,
  254. #endif
  255. .priv_auto = sizeof(struct atmel_serial_priv),
  256. };
  257. #endif
  258. #ifdef CONFIG_DEBUG_UART_ATMEL
  259. static inline void _debug_uart_init(void)
  260. {
  261. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_VAL(DEBUG_UART_BASE);
  262. _atmel_serial_init(usart, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
  263. }
  264. static inline void _debug_uart_putc(int ch)
  265. {
  266. atmel_usart3_t *usart = (atmel_usart3_t *)CONFIG_VAL(DEBUG_UART_BASE);
  267. while (!(readl(&usart->csr) & USART3_BIT(TXRDY)))
  268. ;
  269. writel(ch, &usart->thr);
  270. }
  271. DEBUG_UART_FUNCS
  272. #endif