debug.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * linux/arch/m68k/mac/debug.c
  3. *
  4. * Shamelessly stolen (SCC code and general framework) from:
  5. *
  6. * linux/arch/m68k/atari/debug.c
  7. *
  8. * Atari debugging and serial console stuff
  9. *
  10. * Assembled of parts of former atari/config.c 97-12-18 by Roman Hodek
  11. *
  12. * This file is subject to the terms and conditions of the GNU General Public
  13. * License. See the file COPYING in the main directory of this archive
  14. * for more details.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/sched.h>
  18. #include <linux/tty.h>
  19. #include <linux/console.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #define BOOTINFO_COMPAT_1_0
  23. #include <asm/setup.h>
  24. #include <asm/bootinfo.h>
  25. #include <asm/macints.h>
  26. extern unsigned long mac_videobase;
  27. extern unsigned long mac_rowbytes;
  28. extern void mac_serial_print(const char *);
  29. #define DEBUG_HEADS
  30. #undef DEBUG_SCREEN
  31. #define DEBUG_SERIAL
  32. /*
  33. * These two auxiliary debug functions should go away ASAP. Only usage:
  34. * before the console output is up (after head.S come some other crucial
  35. * setup routines :-) it permits writing 'data' to the screen as bit patterns
  36. * (good luck reading those). Helped to figure that the bootinfo contained
  37. * garbage data on the amount and size of memory chunks ...
  38. *
  39. * The 'pos' argument now simply means 'linefeed after print' ...
  40. */
  41. #ifdef DEBUG_SCREEN
  42. static int peng, line;
  43. #endif
  44. #if 0
  45. void mac_debugging_short(int pos, short num)
  46. {
  47. #ifdef DEBUG_SCREEN
  48. unsigned char *pengoffset;
  49. unsigned char *pptr;
  50. int i;
  51. #endif
  52. #ifdef DEBUG_SERIAL
  53. printk("debug: %d !\n", num);
  54. #endif
  55. #ifdef DEBUG_SCREEN
  56. if (!MACH_IS_MAC) {
  57. /* printk("debug: %d !\n", num); */
  58. return;
  59. }
  60. /* calculate current offset */
  61. pengoffset = (unsigned char *)mac_videobase +
  62. (150+line*2) * mac_rowbytes + 80 * peng;
  63. pptr = pengoffset;
  64. for (i = 0; i < 8 * sizeof(short); i++) { /* # of bits */
  65. /* value mask for bit i, reverse order */
  66. *pptr++ = (num & (1 << (8*sizeof(short)-i-1)) ? 0xFF : 0x00);
  67. }
  68. peng++;
  69. if (pos) {
  70. line++;
  71. peng = 0;
  72. }
  73. #endif
  74. }
  75. void mac_debugging_long(int pos, long addr)
  76. {
  77. #ifdef DEBUG_SCREEN
  78. unsigned char *pengoffset;
  79. unsigned char *pptr;
  80. int i;
  81. #endif
  82. #ifdef DEBUG_SERIAL
  83. printk("debug: #%ld !\n", addr);
  84. #endif
  85. #ifdef DEBUG_SCREEN
  86. if (!MACH_IS_MAC) {
  87. /* printk("debug: #%ld !\n", addr); */
  88. return;
  89. }
  90. pengoffset=(unsigned char *)(mac_videobase+(150+line*2)*mac_rowbytes)
  91. +80*peng;
  92. pptr = pengoffset;
  93. for (i = 0; i < 8 * sizeof(long); i++) { /* # of bits */
  94. *pptr++ = (addr & (1 << (8*sizeof(long)-i-1)) ? 0xFF : 0x00);
  95. }
  96. peng++;
  97. if (pos) {
  98. line++;
  99. peng = 0;
  100. }
  101. #endif
  102. }
  103. #endif /* 0 */
  104. #ifdef DEBUG_SERIAL
  105. /*
  106. * TODO: serial debug code
  107. */
  108. struct mac_SCC {
  109. u_char cha_b_ctrl;
  110. u_char char_dummy1;
  111. u_char cha_a_ctrl;
  112. u_char char_dummy2;
  113. u_char cha_b_data;
  114. u_char char_dummy3;
  115. u_char cha_a_data;
  116. };
  117. # define scc (*((volatile struct mac_SCC*)mac_bi_data.sccbase))
  118. static int scc_port = -1;
  119. static struct console mac_console_driver = {
  120. .name = "debug",
  121. .flags = CON_PRINTBUFFER,
  122. .index = -1,
  123. };
  124. /*
  125. * Crude hack to get console output to the screen before the framebuffer
  126. * is initialized (happens a lot later in 2.1!).
  127. * We just use the console routines declared in head.S, this will interfere
  128. * with regular framebuffer console output and should be used exclusively
  129. * to debug kernel problems manifesting before framebuffer init (aka WSOD)
  130. *
  131. * To keep this hack from interfering with the regular console driver, either
  132. * deregister this driver before/on framebuffer console init, or silence this
  133. * function after the fbcon driver is running (will lose console messages!?).
  134. * To debug real early bugs, need to write a 'mac_register_console_hack()'
  135. * that is called from start_kernel() before setup_arch() and just registers
  136. * this driver if Mac.
  137. */
  138. static void mac_debug_console_write(struct console *co, const char *str,
  139. unsigned int count)
  140. {
  141. mac_serial_print(str);
  142. }
  143. /* Mac: loops_per_jiffy min. 19000 ^= .5 us; MFPDELAY was 0.6 us*/
  144. #define uSEC 1
  145. static inline void mac_sccb_out(char c)
  146. {
  147. int i;
  148. do {
  149. for (i = uSEC; i > 0; --i)
  150. barrier();
  151. } while (!(scc.cha_b_ctrl & 0x04)); /* wait for tx buf empty */
  152. for (i = uSEC; i > 0; --i)
  153. barrier();
  154. scc.cha_b_data = c;
  155. }
  156. static inline void mac_scca_out(char c)
  157. {
  158. int i;
  159. do {
  160. for (i = uSEC; i > 0; --i)
  161. barrier();
  162. } while (!(scc.cha_a_ctrl & 0x04)); /* wait for tx buf empty */
  163. for (i = uSEC; i > 0; --i)
  164. barrier();
  165. scc.cha_a_data = c;
  166. }
  167. static void mac_sccb_console_write(struct console *co, const char *str,
  168. unsigned int count)
  169. {
  170. while (count--) {
  171. if (*str == '\n')
  172. mac_sccb_out('\r');
  173. mac_sccb_out(*str++);
  174. }
  175. }
  176. static void mac_scca_console_write(struct console *co, const char *str,
  177. unsigned int count)
  178. {
  179. while (count--) {
  180. if (*str == '\n')
  181. mac_scca_out('\r');
  182. mac_scca_out(*str++);
  183. }
  184. }
  185. /* The following two functions do a quick'n'dirty initialization of the MFP or
  186. * SCC serial ports. They're used by the debugging interface, kgdb, and the
  187. * serial console code. */
  188. #define SCCB_WRITE(reg,val) \
  189. do { \
  190. int i; \
  191. scc.cha_b_ctrl = (reg); \
  192. for (i = uSEC; i > 0; --i) \
  193. barrier(); \
  194. scc.cha_b_ctrl = (val); \
  195. for (i = uSEC; i > 0; --i) \
  196. barrier(); \
  197. } while(0)
  198. #define SCCA_WRITE(reg,val) \
  199. do { \
  200. int i; \
  201. scc.cha_a_ctrl = (reg); \
  202. for (i = uSEC; i > 0; --i) \
  203. barrier(); \
  204. scc.cha_a_ctrl = (val); \
  205. for (i = uSEC; i > 0; --i) \
  206. barrier(); \
  207. } while(0)
  208. /* loops_per_jiffy isn't initialized yet, so we can't use udelay(). This does a
  209. * delay of ~ 60us. */
  210. /* Mac: loops_per_jiffy min. 19000 ^= .5 us; MFPDELAY was 0.6 us*/
  211. #define LONG_DELAY() \
  212. do { \
  213. int i; \
  214. for (i = 60*uSEC; i > 0; --i) \
  215. barrier(); \
  216. } while(0)
  217. static void __init mac_init_scc_port(int cflag, int port)
  218. {
  219. /*
  220. * baud rates: 1200, 1800, 2400, 4800, 9600, 19.2k, 38.4k, 57.6k, 115.2k
  221. */
  222. static int clksrc_table[9] =
  223. /* reg 11: 0x50 = BRG, 0x00 = RTxC, 0x28 = TRxC */
  224. { 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x00, 0x00 };
  225. static int clkmode_table[9] =
  226. /* reg 4: 0x40 = x16, 0x80 = x32, 0xc0 = x64 */
  227. { 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xc0, 0x80 };
  228. static int div_table[9] =
  229. /* reg12 (BRG low) */
  230. { 94, 62, 46, 22, 10, 4, 1, 0, 0 };
  231. int baud = cflag & CBAUD;
  232. int clksrc, clkmode, div, reg3, reg5;
  233. if (cflag & CBAUDEX)
  234. baud += B38400;
  235. if (baud < B1200 || baud > B38400+2)
  236. baud = B9600; /* use default 9600bps for non-implemented rates */
  237. baud -= B1200; /* tables starts at 1200bps */
  238. clksrc = clksrc_table[baud];
  239. clkmode = clkmode_table[baud];
  240. div = div_table[baud];
  241. reg3 = (((cflag & CSIZE) == CS8) ? 0xc0 : 0x40);
  242. reg5 = (((cflag & CSIZE) == CS8) ? 0x60 : 0x20) | 0x82 /* assert DTR/RTS */;
  243. if (port == 1) {
  244. (void)scc.cha_b_ctrl; /* reset reg pointer */
  245. SCCB_WRITE(9, 0xc0); /* reset */
  246. LONG_DELAY(); /* extra delay after WR9 access */
  247. SCCB_WRITE(4, (cflag & PARENB) ? ((cflag & PARODD) ? 0x01 : 0x03) : 0 |
  248. 0x04 /* 1 stopbit */ |
  249. clkmode);
  250. SCCB_WRITE(3, reg3);
  251. SCCB_WRITE(5, reg5);
  252. SCCB_WRITE(9, 0); /* no interrupts */
  253. LONG_DELAY(); /* extra delay after WR9 access */
  254. SCCB_WRITE(10, 0); /* NRZ mode */
  255. SCCB_WRITE(11, clksrc); /* main clock source */
  256. SCCB_WRITE(12, div); /* BRG value */
  257. SCCB_WRITE(13, 0); /* BRG high byte */
  258. SCCB_WRITE(14, 1);
  259. SCCB_WRITE(3, reg3 | 1);
  260. SCCB_WRITE(5, reg5 | 8);
  261. } else if (port == 0) {
  262. (void)scc.cha_a_ctrl; /* reset reg pointer */
  263. SCCA_WRITE(9, 0xc0); /* reset */
  264. LONG_DELAY(); /* extra delay after WR9 access */
  265. SCCA_WRITE(4, (cflag & PARENB) ? ((cflag & PARODD) ? 0x01 : 0x03) : 0 |
  266. 0x04 /* 1 stopbit */ |
  267. clkmode);
  268. SCCA_WRITE(3, reg3);
  269. SCCA_WRITE(5, reg5);
  270. SCCA_WRITE(9, 0); /* no interrupts */
  271. LONG_DELAY(); /* extra delay after WR9 access */
  272. SCCA_WRITE(10, 0); /* NRZ mode */
  273. SCCA_WRITE(11, clksrc); /* main clock source */
  274. SCCA_WRITE(12, div); /* BRG value */
  275. SCCA_WRITE(13, 0); /* BRG high byte */
  276. SCCA_WRITE(14, 1);
  277. SCCA_WRITE(3, reg3 | 1);
  278. SCCA_WRITE(5, reg5 | 8);
  279. }
  280. }
  281. #endif /* DEBUG_SERIAL */
  282. static int __init mac_debug_setup(char *arg)
  283. {
  284. if (!MACH_IS_MAC)
  285. return 0;
  286. #ifdef DEBUG_SERIAL
  287. if (!strcmp(arg, "ser") || !strcmp(arg, "ser1")) {
  288. /* Mac modem port */
  289. mac_init_scc_port(B9600|CS8, 0);
  290. mac_console_driver.write = mac_scca_console_write;
  291. scc_port = 0;
  292. } else if (!strcmp(arg, "ser2")) {
  293. /* Mac printer port */
  294. mac_init_scc_port(B9600|CS8, 1);
  295. mac_console_driver.write = mac_sccb_console_write;
  296. scc_port = 1;
  297. }
  298. #endif
  299. #ifdef DEBUG_HEADS
  300. if (!strcmp(arg, "scn") || !strcmp(arg, "con")) {
  301. /* display, using head.S console routines */
  302. mac_console_driver.write = mac_debug_console_write;
  303. }
  304. #endif
  305. if (mac_console_driver.write)
  306. register_console(&mac_console_driver);
  307. return 0;
  308. }
  309. early_param("debug", mac_debug_setup);