serial_stm32x7.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * (C) Copyright 2016
  3. * Vikas Manocha, <vikas.manocha@st.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <clk.h>
  9. #include <dm.h>
  10. #include <asm/io.h>
  11. #include <serial.h>
  12. #include <asm/arch/stm32.h>
  13. #include "serial_stm32x7.h"
  14. DECLARE_GLOBAL_DATA_PTR;
  15. static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
  16. {
  17. struct stm32x7_serial_platdata *plat = dev->platdata;
  18. struct stm32_usart *const usart = plat->base;
  19. u32 int_div, mantissa, fraction, oversampling;
  20. int_div = DIV_ROUND_CLOSEST(plat->clock_rate, baudrate);
  21. if (int_div < 16) {
  22. oversampling = 8;
  23. setbits_le32(&usart->cr1, USART_CR1_OVER8);
  24. } else {
  25. oversampling = 16;
  26. clrbits_le32(&usart->cr1, USART_CR1_OVER8);
  27. }
  28. mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
  29. fraction = int_div % oversampling;
  30. writel(mantissa | fraction, &usart->brr);
  31. return 0;
  32. }
  33. static int stm32_serial_getc(struct udevice *dev)
  34. {
  35. struct stm32x7_serial_platdata *plat = dev->platdata;
  36. struct stm32_usart *const usart = plat->base;
  37. if ((readl(&usart->sr) & USART_SR_FLAG_RXNE) == 0)
  38. return -EAGAIN;
  39. return readl(&usart->rd_dr);
  40. }
  41. static int stm32_serial_putc(struct udevice *dev, const char c)
  42. {
  43. struct stm32x7_serial_platdata *plat = dev->platdata;
  44. struct stm32_usart *const usart = plat->base;
  45. if ((readl(&usart->sr) & USART_SR_FLAG_TXE) == 0)
  46. return -EAGAIN;
  47. writel(c, &usart->tx_dr);
  48. return 0;
  49. }
  50. static int stm32_serial_pending(struct udevice *dev, bool input)
  51. {
  52. struct stm32x7_serial_platdata *plat = dev->platdata;
  53. struct stm32_usart *const usart = plat->base;
  54. if (input)
  55. return readl(&usart->sr) & USART_SR_FLAG_RXNE ? 1 : 0;
  56. else
  57. return readl(&usart->sr) & USART_SR_FLAG_TXE ? 0 : 1;
  58. }
  59. static int stm32_serial_probe(struct udevice *dev)
  60. {
  61. struct stm32x7_serial_platdata *plat = dev->platdata;
  62. struct stm32_usart *const usart = plat->base;
  63. #ifdef CONFIG_CLK
  64. int ret;
  65. struct clk clk;
  66. ret = clk_get_by_index(dev, 0, &clk);
  67. if (ret < 0)
  68. return ret;
  69. ret = clk_enable(&clk);
  70. if (ret) {
  71. dev_err(dev, "failed to enable clock\n");
  72. return ret;
  73. }
  74. #endif
  75. plat->clock_rate = clk_get_rate(&clk);
  76. if (plat->clock_rate < 0) {
  77. clk_disable(&clk);
  78. return plat->clock_rate;
  79. };
  80. /* Disable usart-> disable overrun-> enable usart */
  81. clrbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
  82. setbits_le32(&usart->cr3, USART_CR3_OVRDIS);
  83. setbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
  84. return 0;
  85. }
  86. #if CONFIG_IS_ENABLED(OF_CONTROL)
  87. static const struct udevice_id stm32_serial_id[] = {
  88. {.compatible = "st,stm32f7-usart"},
  89. {.compatible = "st,stm32f7-uart"},
  90. {.compatible = "st,stm32h7-usart"},
  91. {.compatible = "st,stm32h7-uart"},
  92. {}
  93. };
  94. static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
  95. {
  96. struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
  97. fdt_addr_t addr;
  98. addr = devfdt_get_addr(dev);
  99. if (addr == FDT_ADDR_T_NONE)
  100. return -EINVAL;
  101. plat->base = (struct stm32_usart *)addr;
  102. return 0;
  103. }
  104. #endif
  105. static const struct dm_serial_ops stm32_serial_ops = {
  106. .putc = stm32_serial_putc,
  107. .pending = stm32_serial_pending,
  108. .getc = stm32_serial_getc,
  109. .setbrg = stm32_serial_setbrg,
  110. };
  111. U_BOOT_DRIVER(serial_stm32) = {
  112. .name = "serial_stm32x7",
  113. .id = UCLASS_SERIAL,
  114. .of_match = of_match_ptr(stm32_serial_id),
  115. .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
  116. .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
  117. .ops = &stm32_serial_ops,
  118. .probe = stm32_serial_probe,
  119. .flags = DM_FLAG_PRE_RELOC,
  120. };