serial_omap.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Texas Instruments' OMAP serial driver
  4. *
  5. * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
  6. * Lokesh Vutla <lokeshvutla@ti.com>
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <dt-structs.h>
  11. #include <ns16550.h>
  12. #include <serial.h>
  13. #include <clk.h>
  14. #include <linux/err.h>
  15. #ifndef CONFIG_SYS_NS16550_CLK
  16. #define CONFIG_SYS_NS16550_CLK 0
  17. #endif
  18. #ifdef CONFIG_DEBUG_UART_OMAP
  19. #ifndef CONFIG_SYS_NS16550_IER
  20. #define CONFIG_SYS_NS16550_IER 0x00
  21. #endif
  22. #define UART_MCRVAL 0x00
  23. #define UART_LCRVAL UART_LCR_8N1
  24. static inline void serial_out_shift(void *addr, int shift, int value)
  25. {
  26. #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
  27. outb(value, (ulong)addr);
  28. #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
  29. out_le32(addr, value);
  30. #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
  31. out_be32(addr, value);
  32. #elif defined(CONFIG_SYS_NS16550_MEM32)
  33. writel(value, addr);
  34. #elif defined(CONFIG_SYS_BIG_ENDIAN)
  35. writeb(value, addr + (1 << shift) - 1);
  36. #else
  37. writeb(value, addr);
  38. #endif
  39. }
  40. static inline int serial_in_shift(void *addr, int shift)
  41. {
  42. #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
  43. return inb((ulong)addr);
  44. #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
  45. return in_le32(addr);
  46. #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
  47. return in_be32(addr);
  48. #elif defined(CONFIG_SYS_NS16550_MEM32)
  49. return readl(addr);
  50. #elif defined(CONFIG_SYS_BIG_ENDIAN)
  51. return readb(addr + (1 << shift) - 1);
  52. #else
  53. return readb(addr);
  54. #endif
  55. }
  56. #include <debug_uart.h>
  57. static inline void _debug_uart_init(void)
  58. {
  59. struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
  60. int baud_divisor;
  61. baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
  62. CONFIG_BAUDRATE);
  63. serial_dout(&com_port->ier, CONFIG_SYS_NS16550_IER);
  64. serial_dout(&com_port->mdr1, 0x7);
  65. serial_dout(&com_port->mcr, UART_MCRVAL);
  66. serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
  67. serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
  68. serial_dout(&com_port->dll, baud_divisor & 0xff);
  69. serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
  70. serial_dout(&com_port->lcr, UART_LCRVAL);
  71. serial_dout(&com_port->mdr1, 0x0);
  72. }
  73. static inline void _debug_uart_putc(int ch)
  74. {
  75. struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
  76. while (!(serial_din(&com_port->lsr) & UART_LSR_THRE))
  77. ;
  78. serial_dout(&com_port->thr, ch);
  79. }
  80. DEBUG_UART_FUNCS
  81. #endif
  82. #if CONFIG_IS_ENABLED(DM_SERIAL)
  83. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  84. static int omap_serial_ofdata_to_platdata(struct udevice *dev)
  85. {
  86. struct ns16550_platdata *plat = dev->platdata;
  87. fdt_addr_t addr;
  88. struct clk clk;
  89. int err;
  90. /* try Processor Local Bus device first */
  91. addr = dev_read_addr(dev);
  92. if (addr == FDT_ADDR_T_NONE)
  93. return -EINVAL;
  94. plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
  95. plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
  96. plat->reg_shift = 2;
  97. err = clk_get_by_index(dev, 0, &clk);
  98. if (!err) {
  99. err = clk_get_rate(&clk);
  100. if (!IS_ERR_VALUE(err))
  101. plat->clock = err;
  102. } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
  103. debug("omap serial failed to get clock\n");
  104. return err;
  105. }
  106. if (!plat->clock)
  107. plat->clock = dev_read_u32_default(dev, "clock-frequency",
  108. CONFIG_SYS_NS16550_CLK);
  109. if (!plat->clock) {
  110. debug("omap serial clock not defined\n");
  111. return -EINVAL;
  112. }
  113. plat->fcr = UART_FCR_DEFVAL;
  114. return 0;
  115. }
  116. static const struct udevice_id omap_serial_ids[] = {
  117. { .compatible = "ti,omap2-uart", },
  118. { .compatible = "ti,omap3-uart", },
  119. { .compatible = "ti,omap4-uart", },
  120. { .compatible = "ti,am3352-uart", },
  121. { .compatible = "ti,am4372-uart", },
  122. { .compatible = "ti,dra742-uart", },
  123. { .compatible = "ti,am654-uart", },
  124. {}
  125. };
  126. #endif /* OF_CONTROL && !OF_PLATDATA */
  127. #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
  128. U_BOOT_DRIVER(omap_serial) = {
  129. .name = "omap_serial",
  130. .id = UCLASS_SERIAL,
  131. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  132. .of_match = omap_serial_ids,
  133. .ofdata_to_platdata = omap_serial_ofdata_to_platdata,
  134. .platdata_auto_alloc_size = sizeof(struct ns16550_platdata),
  135. #endif
  136. .priv_auto_alloc_size = sizeof(struct NS16550),
  137. .probe = ns16550_serial_probe,
  138. .ops = &ns16550_serial_ops,
  139. #if !CONFIG_IS_ENABLED(OF_CONTROL)
  140. .flags = DM_FLAG_PRE_RELOC,
  141. #endif
  142. };
  143. #endif
  144. #endif /* DM_SERIAL */