serial_omap.c 4.4 KB

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