debug.c 9.7 KB

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