udbg_16550.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 || !(udbg_uart_in(UART_LSR) & LSR_DR))
  63. return udbg_uart_in(UART_RBR);
  64. return -1;
  65. }
  66. static int udbg_uart_getc(void)
  67. {
  68. if (!udbg_uart_in)
  69. return -1;
  70. /* wait for char */
  71. while (!(udbg_uart_in(UART_LSR) & LSR_DR))
  72. cpu_relax();
  73. return udbg_uart_in(UART_RBR);
  74. }
  75. static void udbg_use_uart(void)
  76. {
  77. udbg_putc = udbg_uart_putc;
  78. udbg_flush = udbg_uart_flush;
  79. udbg_getc = udbg_uart_getc;
  80. udbg_getc_poll = udbg_uart_getc_poll;
  81. }
  82. void udbg_uart_setup(unsigned int speed, unsigned int clock)
  83. {
  84. unsigned int dll, base_bauds;
  85. if (!udbg_uart_out)
  86. return;
  87. if (clock == 0)
  88. clock = 1843200;
  89. if (speed == 0)
  90. speed = 9600;
  91. base_bauds = clock / 16;
  92. dll = base_bauds / speed;
  93. udbg_uart_out(UART_LCR, 0x00);
  94. udbg_uart_out(UART_IER, 0xff);
  95. udbg_uart_out(UART_IER, 0x00);
  96. udbg_uart_out(UART_LCR, LCR_DLAB);
  97. udbg_uart_out(UART_DLL, dll & 0xff);
  98. udbg_uart_out(UART_DLM, dll >> 8);
  99. /* 8 data, 1 stop, no parity */
  100. udbg_uart_out(UART_LCR, 0x3);
  101. /* RTS/DTR */
  102. udbg_uart_out(UART_MCR, 0x3);
  103. /* Clear & enable FIFOs */
  104. udbg_uart_out(UART_FCR, 0x7);
  105. }
  106. unsigned int udbg_probe_uart_speed(unsigned int clock)
  107. {
  108. unsigned int dll, dlm, divisor, prescaler, speed;
  109. u8 old_lcr;
  110. old_lcr = udbg_uart_in(UART_LCR);
  111. /* select divisor latch registers. */
  112. udbg_uart_out(UART_LCR, old_lcr | LCR_DLAB);
  113. /* now, read the divisor */
  114. dll = udbg_uart_in(UART_DLL);
  115. dlm = udbg_uart_in(UART_DLM);
  116. divisor = dlm << 8 | dll;
  117. /* check prescaling */
  118. if (udbg_uart_in(UART_MCR) & 0x80)
  119. prescaler = 4;
  120. else
  121. prescaler = 1;
  122. /* restore the LCR */
  123. udbg_uart_out(UART_LCR, old_lcr);
  124. /* calculate speed */
  125. speed = (clock / prescaler) / (divisor * 16);
  126. /* sanity check */
  127. if (speed > (clock / 16))
  128. speed = 9600;
  129. return speed;
  130. }
  131. static union {
  132. unsigned char __iomem *mmio_base;
  133. unsigned long pio_base;
  134. } udbg_uart;
  135. static unsigned int udbg_uart_stride = 1;
  136. static u8 udbg_uart_in_pio(unsigned int reg)
  137. {
  138. return inb(udbg_uart.pio_base + (reg * udbg_uart_stride));
  139. }
  140. static void udbg_uart_out_pio(unsigned int reg, u8 data)
  141. {
  142. outb(data, udbg_uart.pio_base + (reg * udbg_uart_stride));
  143. }
  144. void udbg_uart_init_pio(unsigned long port, unsigned int stride)
  145. {
  146. if (!port)
  147. return;
  148. udbg_uart.pio_base = port;
  149. udbg_uart_stride = stride;
  150. udbg_uart_in = udbg_uart_in_pio;
  151. udbg_uart_out = udbg_uart_out_pio;
  152. udbg_use_uart();
  153. }
  154. static u8 udbg_uart_in_mmio(unsigned int reg)
  155. {
  156. return in_8(udbg_uart.mmio_base + (reg * udbg_uart_stride));
  157. }
  158. static void udbg_uart_out_mmio(unsigned int reg, u8 data)
  159. {
  160. out_8(udbg_uart.mmio_base + (reg * udbg_uart_stride), data);
  161. }
  162. void udbg_uart_init_mmio(void __iomem *addr, unsigned int stride)
  163. {
  164. if (!addr)
  165. return;
  166. udbg_uart.mmio_base = addr;
  167. udbg_uart_stride = stride;
  168. udbg_uart_in = udbg_uart_in_mmio;
  169. udbg_uart_out = udbg_uart_out_mmio;
  170. udbg_use_uart();
  171. }
  172. #ifdef CONFIG_PPC_MAPLE
  173. #define UDBG_UART_MAPLE_ADDR ((void __iomem *)0xf40003f8)
  174. static u8 udbg_uart_in_maple(unsigned int reg)
  175. {
  176. return real_readb(UDBG_UART_MAPLE_ADDR + reg);
  177. }
  178. static void udbg_uart_out_maple(unsigned int reg, u8 val)
  179. {
  180. real_writeb(val, UDBG_UART_MAPLE_ADDR + reg);
  181. }
  182. void __init udbg_init_maple_realmode(void)
  183. {
  184. udbg_uart_in = udbg_uart_in_maple;
  185. udbg_uart_out = udbg_uart_out_maple;
  186. udbg_use_uart();
  187. }
  188. #endif /* CONFIG_PPC_MAPLE */
  189. #ifdef CONFIG_PPC_PASEMI
  190. #define UDBG_UART_PAS_ADDR ((void __iomem *)0xfcff03f8UL)
  191. static u8 udbg_uart_in_pas(unsigned int reg)
  192. {
  193. return real_205_readb(UDBG_UART_PAS_ADDR + reg);
  194. }
  195. static void udbg_uart_out_pas(unsigned int reg, u8 val)
  196. {
  197. real_205_writeb(val, UDBG_UART_PAS_ADDR + reg);
  198. }
  199. void __init udbg_init_pas_realmode(void)
  200. {
  201. udbg_uart_in = udbg_uart_in_pas;
  202. udbg_uart_out = udbg_uart_out_pas;
  203. udbg_use_uart();
  204. }
  205. #endif /* CONFIG_PPC_PASEMI */
  206. #ifdef CONFIG_PPC_EARLY_DEBUG_44x
  207. #include <platforms/44x/44x.h>
  208. static u8 udbg_uart_in_44x_as1(unsigned int reg)
  209. {
  210. return as1_readb((void __iomem *)PPC44x_EARLY_DEBUG_VIRTADDR + reg);
  211. }
  212. static void udbg_uart_out_44x_as1(unsigned int reg, u8 val)
  213. {
  214. as1_writeb(val, (void __iomem *)PPC44x_EARLY_DEBUG_VIRTADDR + reg);
  215. }
  216. void __init udbg_init_44x_as1(void)
  217. {
  218. udbg_uart_in = udbg_uart_in_44x_as1;
  219. udbg_uart_out = udbg_uart_out_44x_as1;
  220. udbg_use_uart();
  221. }
  222. #endif /* CONFIG_PPC_EARLY_DEBUG_44x */
  223. #ifdef CONFIG_PPC_EARLY_DEBUG_40x
  224. static u8 udbg_uart_in_40x(unsigned int reg)
  225. {
  226. return real_readb((void __iomem *)CONFIG_PPC_EARLY_DEBUG_40x_PHYSADDR
  227. + reg);
  228. }
  229. static void udbg_uart_out_40x(unsigned int reg, u8 val)
  230. {
  231. real_writeb(val, (void __iomem *)CONFIG_PPC_EARLY_DEBUG_40x_PHYSADDR
  232. + reg);
  233. }
  234. void __init udbg_init_40x_realmode(void)
  235. {
  236. udbg_uart_in = udbg_uart_in_40x;
  237. udbg_uart_out = udbg_uart_out_40x;
  238. udbg_use_uart();
  239. }
  240. #endif /* CONFIG_PPC_EARLY_DEBUG_40x */
  241. #ifdef CONFIG_PPC_EARLY_DEBUG_WSP
  242. void __init udbg_init_wsp(void)
  243. {
  244. udbg_uart_init_mmio((void *)WSP_UART_VIRT, 1);
  245. udbg_uart_setup(57600, 50000000);
  246. }
  247. #endif /* CONFIG_PPC_EARLY_DEBUG_WSP */