debug.c 9.7 KB

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