serial_stm32x7.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <dm/platform_data/serial_stm32x7.h>
  12. #include "serial_stm32x7.h"
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
  15. {
  16. struct stm32x7_serial_platdata *plat = dev->platdata;
  17. struct stm32_usart *const usart = plat->base;
  18. writel(plat->clock/baudrate, &usart->brr);
  19. return 0;
  20. }
  21. static int stm32_serial_getc(struct udevice *dev)
  22. {
  23. struct stm32x7_serial_platdata *plat = dev->platdata;
  24. struct stm32_usart *const usart = plat->base;
  25. if ((readl(&usart->sr) & USART_SR_FLAG_RXNE) == 0)
  26. return -EAGAIN;
  27. return readl(&usart->rd_dr);
  28. }
  29. static int stm32_serial_putc(struct udevice *dev, const char c)
  30. {
  31. struct stm32x7_serial_platdata *plat = dev->platdata;
  32. struct stm32_usart *const usart = plat->base;
  33. if ((readl(&usart->sr) & USART_SR_FLAG_TXE) == 0)
  34. return -EAGAIN;
  35. writel(c, &usart->tx_dr);
  36. return 0;
  37. }
  38. static int stm32_serial_pending(struct udevice *dev, bool input)
  39. {
  40. struct stm32x7_serial_platdata *plat = dev->platdata;
  41. struct stm32_usart *const usart = plat->base;
  42. if (input)
  43. return readl(&usart->sr) & USART_SR_FLAG_RXNE ? 1 : 0;
  44. else
  45. return readl(&usart->sr) & USART_SR_FLAG_TXE ? 0 : 1;
  46. }
  47. static int stm32_serial_probe(struct udevice *dev)
  48. {
  49. struct stm32x7_serial_platdata *plat = dev->platdata;
  50. struct stm32_usart *const usart = plat->base;
  51. setbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
  52. return 0;
  53. }
  54. static const struct dm_serial_ops stm32_serial_ops = {
  55. .putc = stm32_serial_putc,
  56. .pending = stm32_serial_pending,
  57. .getc = stm32_serial_getc,
  58. .setbrg = stm32_serial_setbrg,
  59. };
  60. U_BOOT_DRIVER(serial_stm32) = {
  61. .name = "serial_stm32x7",
  62. .id = UCLASS_SERIAL,
  63. .ops = &stm32_serial_ops,
  64. .probe = stm32_serial_probe,
  65. .flags = DM_FLAG_PRE_RELOC,
  66. };