serial_stm32x7.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <dm/platform_data/serial_stm32x7.h>
  14. #include "serial_stm32x7.h"
  15. DECLARE_GLOBAL_DATA_PTR;
  16. static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
  17. {
  18. struct stm32x7_serial_platdata *plat = dev->platdata;
  19. struct stm32_usart *const usart = plat->base;
  20. u32 clock, int_div, frac_div, tmp;
  21. if (((u32)usart & STM32_BUS_MASK) == APB1_PERIPH_BASE)
  22. clock = clock_get(CLOCK_APB1);
  23. else if (((u32)usart & STM32_BUS_MASK) == APB2_PERIPH_BASE)
  24. clock = clock_get(CLOCK_APB2);
  25. else
  26. return -EINVAL;
  27. int_div = (25 * clock) / (4 * baudrate);
  28. tmp = ((int_div / 100) << USART_BRR_M_SHIFT) & USART_BRR_M_MASK;
  29. frac_div = int_div - (100 * (tmp >> USART_BRR_M_SHIFT));
  30. tmp |= (((frac_div * 16) + 50) / 100) & USART_BRR_F_MASK;
  31. writel(tmp, &usart->brr);
  32. return 0;
  33. }
  34. static int stm32_serial_getc(struct udevice *dev)
  35. {
  36. struct stm32x7_serial_platdata *plat = dev->platdata;
  37. struct stm32_usart *const usart = plat->base;
  38. if ((readl(&usart->sr) & USART_SR_FLAG_RXNE) == 0)
  39. return -EAGAIN;
  40. return readl(&usart->rd_dr);
  41. }
  42. static int stm32_serial_putc(struct udevice *dev, const char c)
  43. {
  44. struct stm32x7_serial_platdata *plat = dev->platdata;
  45. struct stm32_usart *const usart = plat->base;
  46. if ((readl(&usart->sr) & USART_SR_FLAG_TXE) == 0)
  47. return -EAGAIN;
  48. writel(c, &usart->tx_dr);
  49. return 0;
  50. }
  51. static int stm32_serial_pending(struct udevice *dev, bool input)
  52. {
  53. struct stm32x7_serial_platdata *plat = dev->platdata;
  54. struct stm32_usart *const usart = plat->base;
  55. if (input)
  56. return readl(&usart->sr) & USART_SR_FLAG_RXNE ? 1 : 0;
  57. else
  58. return readl(&usart->sr) & USART_SR_FLAG_TXE ? 0 : 1;
  59. }
  60. static int stm32_serial_probe(struct udevice *dev)
  61. {
  62. struct stm32x7_serial_platdata *plat = dev->platdata;
  63. struct stm32_usart *const usart = plat->base;
  64. #ifdef CONFIG_CLK
  65. int ret;
  66. struct clk clk;
  67. ret = clk_get_by_index(dev, 0, &clk);
  68. if (ret < 0)
  69. return ret;
  70. ret = clk_enable(&clk);
  71. if (ret) {
  72. dev_err(dev, "failed to enable clock\n");
  73. return ret;
  74. }
  75. #endif
  76. setbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
  77. return 0;
  78. }
  79. #if CONFIG_IS_ENABLED(OF_CONTROL)
  80. static const struct udevice_id stm32_serial_id[] = {
  81. {.compatible = "st,stm32-usart"},
  82. {.compatible = "st,stm32-uart"},
  83. {}
  84. };
  85. static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
  86. {
  87. struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
  88. fdt_addr_t addr;
  89. addr = dev_get_addr(dev);
  90. if (addr == FDT_ADDR_T_NONE)
  91. return -EINVAL;
  92. plat->base = (struct stm32_usart *)addr;
  93. return 0;
  94. }
  95. #endif
  96. static const struct dm_serial_ops stm32_serial_ops = {
  97. .putc = stm32_serial_putc,
  98. .pending = stm32_serial_pending,
  99. .getc = stm32_serial_getc,
  100. .setbrg = stm32_serial_setbrg,
  101. };
  102. U_BOOT_DRIVER(serial_stm32) = {
  103. .name = "serial_stm32x7",
  104. .id = UCLASS_SERIAL,
  105. .of_match = of_match_ptr(stm32_serial_id),
  106. .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
  107. .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
  108. .ops = &stm32_serial_ops,
  109. .probe = stm32_serial_probe,
  110. .flags = DM_FLAG_PRE_RELOC,
  111. };