udbg_16550.c 6.4 KB

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