serial_stm32x7.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <dm.h>
  9. #include <asm/io.h>
  10. #include <serial.h>
  11. #include <asm/arch/stm32.h>
  12. #include <dm/platform_data/serial_stm32x7.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 clock, int_div, frac_div, tmp;
  20. if (((u32)usart & STM32_BUS_MASK) == APB1_PERIPH_BASE)
  21. clock = clock_get(CLOCK_APB1);
  22. else if (((u32)usart & STM32_BUS_MASK) == APB2_PERIPH_BASE)
  23. clock = clock_get(CLOCK_APB2);
  24. else
  25. return -EINVAL;
  26. int_div = (25 * clock) / (4 * baudrate);
  27. tmp = ((int_div / 100) << USART_BRR_M_SHIFT) & USART_BRR_M_MASK;
  28. frac_div = int_div - (100 * (tmp >> USART_BRR_M_SHIFT));
  29. tmp |= (((frac_div * 16) + 50) / 100) & USART_BRR_F_MASK;
  30. writel(tmp, &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. setbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
  64. return 0;
  65. }
  66. #if CONFIG_IS_ENABLED(OF_CONTROL)
  67. static const struct udevice_id stm32_serial_id[] = {
  68. {.compatible = "st,stm32-usart"},
  69. {.compatible = "st,stm32-uart"},
  70. {}
  71. };
  72. static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
  73. {
  74. struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
  75. fdt_addr_t addr;
  76. addr = dev_get_addr(dev);
  77. if (addr == FDT_ADDR_T_NONE)
  78. return -EINVAL;
  79. plat->base = (struct stm32_usart *)addr;
  80. return 0;
  81. }
  82. #endif
  83. static const struct dm_serial_ops stm32_serial_ops = {
  84. .putc = stm32_serial_putc,
  85. .pending = stm32_serial_pending,
  86. .getc = stm32_serial_getc,
  87. .setbrg = stm32_serial_setbrg,
  88. };
  89. U_BOOT_DRIVER(serial_stm32) = {
  90. .name = "serial_stm32x7",
  91. .id = UCLASS_SERIAL,
  92. .of_match = of_match_ptr(stm32_serial_id),
  93. .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
  94. .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
  95. .ops = &stm32_serial_ops,
  96. .probe = stm32_serial_probe,
  97. .flags = DM_FLAG_PRE_RELOC,
  98. };