serial_stm32x7.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. static const struct dm_serial_ops stm32_serial_ops = {
  67. .putc = stm32_serial_putc,
  68. .pending = stm32_serial_pending,
  69. .getc = stm32_serial_getc,
  70. .setbrg = stm32_serial_setbrg,
  71. };
  72. U_BOOT_DRIVER(serial_stm32) = {
  73. .name = "serial_stm32x7",
  74. .id = UCLASS_SERIAL,
  75. .ops = &stm32_serial_ops,
  76. .probe = stm32_serial_probe,
  77. .flags = DM_FLAG_PRE_RELOC,
  78. };