udbg_16550.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * udbg for NS16550 compatible serial ports
  4. *
  5. * Copyright (C) 2001-2005 PPC 64 Team, IBM Corp
  6. */
  7. #include <linux/types.h>
  8. #include <asm/udbg.h>
  9. #include <asm/io.h>
  10. #include <asm/early_ioremap.h>
  11. extern u8 real_readb(volatile u8 __iomem *addr);
  12. extern void real_writeb(u8 data, volatile u8 __iomem *addr);
  13. extern u8 real_205_readb(volatile u8 __iomem *addr);
  14. extern void real_205_writeb(u8 data, volatile u8 __iomem *addr);
  15. #define UART_RBR 0
  16. #define UART_IER 1
  17. #define UART_FCR 2
  18. #define UART_LCR 3
  19. #define UART_MCR 4
  20. #define UART_LSR 5
  21. #define UART_MSR 6
  22. #define UART_SCR 7
  23. #define UART_THR UART_RBR
  24. #define UART_IIR UART_FCR
  25. #define UART_DLL UART_RBR
  26. #define UART_DLM UART_IER
  27. #define UART_DLAB UART_LCR
  28. #define LSR_DR 0x01 /* Data ready */
  29. #define LSR_OE 0x02 /* Overrun */
  30. #define LSR_PE 0x04 /* Parity error */
  31. #define LSR_FE 0x08 /* Framing error */
  32. #define LSR_BI 0x10 /* Break */
  33. #define LSR_THRE 0x20 /* Xmit holding register empty */
  34. #define LSR_TEMT 0x40 /* Xmitter empty */
  35. #define LSR_ERR 0x80 /* Error */
  36. #define LCR_DLAB 0x80
  37. static u8 (*udbg_uart_in)(unsigned int reg);
  38. static void (*udbg_uart_out)(unsigned int reg, u8 data);
  39. static void udbg_uart_flush(void)
  40. {
  41. if (!udbg_uart_in)
  42. return;
  43. /* wait for idle */
  44. while ((udbg_uart_in(UART_LSR) & LSR_THRE) == 0)
  45. cpu_relax();
  46. }
  47. static void udbg_uart_putc(char c)
  48. {
  49. if (!udbg_uart_out)
  50. return;
  51. if (c == '\n')
  52. udbg_uart_putc('\r');
  53. udbg_uart_flush();
  54. udbg_uart_out(UART_THR, c);
  55. }
  56. static int udbg_uart_getc_poll(void)
  57. {
  58. if (!udbg_uart_in)
  59. return -1;
  60. if (!(udbg_uart_in(UART_LSR) & LSR_DR))
  61. return udbg_uart_in(UART_RBR);
  62. return -1;
  63. }
  64. static int udbg_uart_getc(void)
  65. {
  66. if (!udbg_uart_in)
  67. return -1;
  68. /* wait for char */
  69. while (!(udbg_uart_in(UART_LSR) & LSR_DR))
  70. cpu_relax();
  71. return udbg_uart_in(UART_RBR);
  72. }
  73. static void __init udbg_use_uart(void)
  74. {
  75. udbg_putc = udbg_uart_putc;
  76. udbg_flush = udbg_uart_flush;
  77. udbg_getc = udbg_uart_getc;
  78. udbg_getc_poll = udbg_uart_getc_poll;
  79. }
  80. void __init udbg_uart_setup(unsigned int speed, unsigned int clock)
  81. {
  82. unsigned int dll, base_bauds;
  83. if (!udbg_uart_out)
  84. return;
  85. if (clock == 0)
  86. clock = 1843200;
  87. if (speed == 0)
  88. speed = 9600;
  89. base_bauds = clock / 16;
  90. dll = base_bauds / speed;
  91. udbg_uart_out(UART_LCR, 0x00);
  92. udbg_uart_out(UART_IER, 0xff);
  93. udbg_uart_out(UART_IER, 0x00);
  94. udbg_uart_out(UART_LCR, LCR_DLAB);
  95. udbg_uart_out(UART_DLL, dll & 0xff);
  96. udbg_uart_out(UART_DLM, dll >> 8);
  97. /* 8 data, 1 stop, no parity */
  98. udbg_uart_out(UART_LCR, 0x3);
  99. /* RTS/DTR */
  100. udbg_uart_out(UART_MCR, 0x3);
  101. /* Clear & enable FIFOs */
  102. udbg_uart_out(UART_FCR, 0x7);
  103. }
  104. unsigned int __init udbg_probe_uart_speed(unsigned int clock)
  105. {
  106. unsigned int dll, dlm, divisor, prescaler, speed;
  107. u8 old_lcr;
  108. old_lcr = udbg_uart_in(UART_LCR);
  109. /* select divisor latch registers. */
  110. udbg_uart_out(UART_LCR, old_lcr | LCR_DLAB);
  111. /* now, read the divisor */
  112. dll = udbg_uart_in(UART_DLL);
  113. dlm = udbg_uart_in(UART_DLM);
  114. divisor = dlm << 8 | dll;
  115. /* check prescaling */
  116. if (udbg_uart_in(UART_MCR) & 0x80)
  117. prescaler = 4;
  118. else
  119. prescaler = 1;
  120. /* restore the LCR */
  121. udbg_uart_out(UART_LCR, old_lcr);
  122. /* calculate speed */
  123. speed = (clock / prescaler) / (divisor * 16);
  124. /* sanity check */
  125. if (speed > (clock / 16))
  126. speed = 9600;
  127. return speed;
  128. }
  129. static union {
  130. unsigned char __iomem *mmio_base;
  131. unsigned long pio_base;
  132. } udbg_uart;
  133. static unsigned int udbg_uart_stride = 1;
  134. static u8 udbg_uart_in_pio(unsigned int reg)
  135. {
  136. return inb(udbg_uart.pio_base + (reg * udbg_uart_stride));
  137. }
  138. static void udbg_uart_out_pio(unsigned int reg, u8 data)
  139. {
  140. outb(data, udbg_uart.pio_base + (reg * udbg_uart_stride));
  141. }
  142. void __init udbg_uart_init_pio(unsigned long port, unsigned int stride)
  143. {
  144. if (!port)
  145. return;
  146. udbg_uart.pio_base = port;
  147. udbg_uart_stride = stride;
  148. udbg_uart_in = udbg_uart_in_pio;
  149. udbg_uart_out = udbg_uart_out_pio;
  150. udbg_use_uart();
  151. }
  152. static u8 udbg_uart_in_mmio(unsigned int reg)
  153. {
  154. return in_8(udbg_uart.mmio_base + (reg * udbg_uart_stride));
  155. }
  156. static void udbg_uart_out_mmio(unsigned int reg, u8 data)
  157. {
  158. out_8(udbg_uart.mmio_base + (reg * udbg_uart_stride), data);
  159. }
  160. void __init udbg_uart_init_mmio(void __iomem *addr, unsigned int stride)
  161. {
  162. if (!addr)
  163. return;
  164. udbg_uart.mmio_base = addr;
  165. udbg_uart_stride = stride;
  166. udbg_uart_in = udbg_uart_in_mmio;
  167. udbg_uart_out = udbg_uart_out_mmio;
  168. udbg_use_uart();
  169. }
  170. #ifdef CONFIG_PPC_MAPLE
  171. #define UDBG_UART_MAPLE_ADDR ((void __iomem *)0xf40003f8)
  172. static u8 udbg_uart_in_maple(unsigned int reg)
  173. {
  174. return real_readb(UDBG_UART_MAPLE_ADDR + reg);
  175. }
  176. static void udbg_uart_out_maple(unsigned int reg, u8 val)
  177. {
  178. real_writeb(val, UDBG_UART_MAPLE_ADDR + reg);
  179. }
  180. void __init udbg_init_maple_realmode(void)
  181. {
  182. udbg_uart_in = udbg_uart_in_maple;
  183. udbg_uart_out = udbg_uart_out_maple;
  184. udbg_use_uart();
  185. }
  186. #endif /* CONFIG_PPC_MAPLE */
  187. #ifdef CONFIG_PPC_PASEMI
  188. #define UDBG_UART_PAS_ADDR ((void __iomem *)0xfcff03f8UL)
  189. static u8 udbg_uart_in_pas(unsigned int reg)
  190. {
  191. return real_205_readb(UDBG_UART_PAS_ADDR + reg);
  192. }
  193. static void udbg_uart_out_pas(unsigned int reg, u8 val)
  194. {
  195. real_205_writeb(val, UDBG_UART_PAS_ADDR + reg);
  196. }
  197. void __init udbg_init_pas_realmode(void)
  198. {
  199. udbg_uart_in = udbg_uart_in_pas;
  200. udbg_uart_out = udbg_uart_out_pas;
  201. udbg_use_uart();
  202. }
  203. #endif /* CONFIG_PPC_PASEMI */
  204. #ifdef CONFIG_PPC_EARLY_DEBUG_44x
  205. #include <platforms/44x/44x.h>
  206. static u8 udbg_uart_in_44x_as1(unsigned int reg)
  207. {
  208. return as1_readb((void __iomem *)PPC44x_EARLY_DEBUG_VIRTADDR + reg);
  209. }
  210. static void udbg_uart_out_44x_as1(unsigned int reg, u8 val)
  211. {
  212. as1_writeb(val, (void __iomem *)PPC44x_EARLY_DEBUG_VIRTADDR + reg);
  213. }
  214. void __init udbg_init_44x_as1(void)
  215. {
  216. udbg_uart_in = udbg_uart_in_44x_as1;
  217. udbg_uart_out = udbg_uart_out_44x_as1;
  218. udbg_use_uart();
  219. }
  220. #endif /* CONFIG_PPC_EARLY_DEBUG_44x */
  221. #ifdef CONFIG_PPC_EARLY_DEBUG_16550
  222. static void __iomem *udbg_uart_early_addr;
  223. void __init udbg_init_debug_16550(void)
  224. {
  225. udbg_uart_early_addr = early_ioremap(CONFIG_PPC_EARLY_DEBUG_16550_PHYSADDR, 0x1000);
  226. udbg_uart_init_mmio(udbg_uart_early_addr, CONFIG_PPC_EARLY_DEBUG_16550_STRIDE);
  227. }
  228. static int __init udbg_init_debug_16550_ioremap(void)
  229. {
  230. void __iomem *addr;
  231. if (!udbg_uart_early_addr)
  232. return 0;
  233. addr = ioremap(CONFIG_PPC_EARLY_DEBUG_16550_PHYSADDR, 0x1000);
  234. if (WARN_ON(!addr))
  235. return -ENOMEM;
  236. udbg_uart_init_mmio(addr, CONFIG_PPC_EARLY_DEBUG_16550_STRIDE);
  237. early_iounmap(udbg_uart_early_addr, 0x1000);
  238. udbg_uart_early_addr = NULL;
  239. return 0;
  240. }
  241. early_initcall(udbg_init_debug_16550_ioremap);
  242. #endif /* CONFIG_PPC_EARLY_DEBUG_16550 */