udbg_16550.c 7.0 KB

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