sn_console.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * C-Brick Serial Port (and console) driver for SGI Altix machines.
  3. *
  4. * This driver is NOT suitable for talking to the l1-controller for
  5. * anything other than 'console activities' --- please use the l1
  6. * driver for that.
  7. *
  8. *
  9. * Copyright (c) 2004-2005 Silicon Graphics, Inc. All Rights Reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of version 2 of the GNU General Public License
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it would be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. *
  19. * Further, this software is distributed without any warranty that it is
  20. * free of the rightful claim of any third person regarding infringement
  21. * or the like. Any license provided herein, whether implied or
  22. * otherwise, applies only to this software file. Patent licenses, if
  23. * any, provided herein do not apply to combinations of this program with
  24. * other software, or any other product whatsoever.
  25. *
  26. * You should have received a copy of the GNU General Public
  27. * License along with this program; if not, write the Free Software
  28. * Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  29. *
  30. * Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
  31. * Mountain View, CA 94043, or:
  32. *
  33. * http://www.sgi.com
  34. *
  35. * For further information regarding this notice, see:
  36. *
  37. * http://oss.sgi.com/projects/GenInfo/NoticeExplan
  38. */
  39. #include <linux/config.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/tty.h>
  42. #include <linux/serial.h>
  43. #include <linux/console.h>
  44. #include <linux/module.h>
  45. #include <linux/sysrq.h>
  46. #include <linux/circ_buf.h>
  47. #include <linux/serial_reg.h>
  48. #include <linux/delay.h> /* for mdelay */
  49. #include <linux/miscdevice.h>
  50. #include <linux/serial_core.h>
  51. #include <asm/io.h>
  52. #include <asm/sn/simulator.h>
  53. #include <asm/sn/sn_sal.h>
  54. /* number of characters we can transmit to the SAL console at a time */
  55. #define SN_SAL_MAX_CHARS 120
  56. /* 64K, when we're asynch, it must be at least printk's LOG_BUF_LEN to
  57. * avoid losing chars, (always has to be a power of 2) */
  58. #define SN_SAL_BUFFER_SIZE (64 * (1 << 10))
  59. #define SN_SAL_UART_FIFO_DEPTH 16
  60. #define SN_SAL_UART_FIFO_SPEED_CPS 9600/10
  61. /* sn_transmit_chars() calling args */
  62. #define TRANSMIT_BUFFERED 0
  63. #define TRANSMIT_RAW 1
  64. /* To use dynamic numbers only and not use the assigned major and minor,
  65. * define the following.. */
  66. /* #define USE_DYNAMIC_MINOR 1 *//* use dynamic minor number */
  67. #define USE_DYNAMIC_MINOR 0 /* Don't rely on misc_register dynamic minor */
  68. /* Device name we're using */
  69. #define DEVICE_NAME "ttySG"
  70. #define DEVICE_NAME_DYNAMIC "ttySG0" /* need full name for misc_register */
  71. /* The major/minor we are using, ignored for USE_DYNAMIC_MINOR */
  72. #define DEVICE_MAJOR 204
  73. #define DEVICE_MINOR 40
  74. #ifdef CONFIG_MAGIC_SYSRQ
  75. static char sysrq_serial_str[] = "\eSYS";
  76. static char *sysrq_serial_ptr = sysrq_serial_str;
  77. static unsigned long sysrq_requested;
  78. #endif /* CONFIG_MAGIC_SYSRQ */
  79. /*
  80. * Port definition - this kinda drives it all
  81. */
  82. struct sn_cons_port {
  83. struct timer_list sc_timer;
  84. struct uart_port sc_port;
  85. struct sn_sal_ops {
  86. int (*sal_puts_raw) (const char *s, int len);
  87. int (*sal_puts) (const char *s, int len);
  88. int (*sal_getc) (void);
  89. int (*sal_input_pending) (void);
  90. void (*sal_wakeup_transmit) (struct sn_cons_port *, int);
  91. } *sc_ops;
  92. unsigned long sc_interrupt_timeout;
  93. int sc_is_asynch;
  94. };
  95. static struct sn_cons_port sal_console_port;
  96. static int sn_process_input;
  97. /* Only used if USE_DYNAMIC_MINOR is set to 1 */
  98. static struct miscdevice misc; /* used with misc_register for dynamic */
  99. extern void early_sn_setup(void);
  100. #undef DEBUG
  101. #ifdef DEBUG
  102. static int sn_debug_printf(const char *fmt, ...);
  103. #define DPRINTF(x...) sn_debug_printf(x)
  104. #else
  105. #define DPRINTF(x...) do { } while (0)
  106. #endif
  107. /* Prototypes */
  108. static int snt_hw_puts_raw(const char *, int);
  109. static int snt_hw_puts_buffered(const char *, int);
  110. static int snt_poll_getc(void);
  111. static int snt_poll_input_pending(void);
  112. static int snt_intr_getc(void);
  113. static int snt_intr_input_pending(void);
  114. static void sn_transmit_chars(struct sn_cons_port *, int);
  115. /* A table for polling:
  116. */
  117. static struct sn_sal_ops poll_ops = {
  118. .sal_puts_raw = snt_hw_puts_raw,
  119. .sal_puts = snt_hw_puts_raw,
  120. .sal_getc = snt_poll_getc,
  121. .sal_input_pending = snt_poll_input_pending
  122. };
  123. /* A table for interrupts enabled */
  124. static struct sn_sal_ops intr_ops = {
  125. .sal_puts_raw = snt_hw_puts_raw,
  126. .sal_puts = snt_hw_puts_buffered,
  127. .sal_getc = snt_intr_getc,
  128. .sal_input_pending = snt_intr_input_pending,
  129. .sal_wakeup_transmit = sn_transmit_chars
  130. };
  131. /* the console does output in two distinctly different ways:
  132. * synchronous (raw) and asynchronous (buffered). initally, early_printk
  133. * does synchronous output. any data written goes directly to the SAL
  134. * to be output (incidentally, it is internally buffered by the SAL)
  135. * after interrupts and timers are initialized and available for use,
  136. * the console init code switches to asynchronous output. this is
  137. * also the earliest opportunity to begin polling for console input.
  138. * after console initialization, console output and tty (serial port)
  139. * output is buffered and sent to the SAL asynchronously (either by
  140. * timer callback or by UART interrupt) */
  141. /* routines for running the console in polling mode */
  142. /**
  143. * snt_poll_getc - Get a character from the console in polling mode
  144. *
  145. */
  146. static int snt_poll_getc(void)
  147. {
  148. int ch;
  149. ia64_sn_console_getc(&ch);
  150. return ch;
  151. }
  152. /**
  153. * snt_poll_input_pending - Check if any input is waiting - polling mode.
  154. *
  155. */
  156. static int snt_poll_input_pending(void)
  157. {
  158. int status, input;
  159. status = ia64_sn_console_check(&input);
  160. return !status && input;
  161. }
  162. /* routines for an interrupt driven console (normal) */
  163. /**
  164. * snt_intr_getc - Get a character from the console, interrupt mode
  165. *
  166. */
  167. static int snt_intr_getc(void)
  168. {
  169. return ia64_sn_console_readc();
  170. }
  171. /**
  172. * snt_intr_input_pending - Check if input is pending, interrupt mode
  173. *
  174. */
  175. static int snt_intr_input_pending(void)
  176. {
  177. return ia64_sn_console_intr_status() & SAL_CONSOLE_INTR_RECV;
  178. }
  179. /* these functions are polled and interrupt */
  180. /**
  181. * snt_hw_puts_raw - Send raw string to the console, polled or interrupt mode
  182. * @s: String
  183. * @len: Length
  184. *
  185. */
  186. static int snt_hw_puts_raw(const char *s, int len)
  187. {
  188. /* this will call the PROM and not return until this is done */
  189. return ia64_sn_console_putb(s, len);
  190. }
  191. /**
  192. * snt_hw_puts_buffered - Send string to console, polled or interrupt mode
  193. * @s: String
  194. * @len: Length
  195. *
  196. */
  197. static int snt_hw_puts_buffered(const char *s, int len)
  198. {
  199. /* queue data to the PROM */
  200. return ia64_sn_console_xmit_chars((char *)s, len);
  201. }
  202. /* uart interface structs
  203. * These functions are associated with the uart_port that the serial core
  204. * infrastructure calls.
  205. *
  206. * Note: Due to how the console works, many routines are no-ops.
  207. */
  208. /**
  209. * snp_type - What type of console are we?
  210. * @port: Port to operate with (we ignore since we only have one port)
  211. *
  212. */
  213. static const char *snp_type(struct uart_port *port)
  214. {
  215. return ("SGI SN L1");
  216. }
  217. /**
  218. * snp_tx_empty - Is the transmitter empty? We pretend we're always empty
  219. * @port: Port to operate on (we ignore since we only have one port)
  220. *
  221. */
  222. static unsigned int snp_tx_empty(struct uart_port *port)
  223. {
  224. return 1;
  225. }
  226. /**
  227. * snp_stop_tx - stop the transmitter - no-op for us
  228. * @port: Port to operat eon - we ignore - no-op function
  229. *
  230. */
  231. static void snp_stop_tx(struct uart_port *port)
  232. {
  233. }
  234. /**
  235. * snp_release_port - Free i/o and resources for port - no-op for us
  236. * @port: Port to operate on - we ignore - no-op function
  237. *
  238. */
  239. static void snp_release_port(struct uart_port *port)
  240. {
  241. }
  242. /**
  243. * snp_enable_ms - Force modem status interrupts on - no-op for us
  244. * @port: Port to operate on - we ignore - no-op function
  245. *
  246. */
  247. static void snp_enable_ms(struct uart_port *port)
  248. {
  249. }
  250. /**
  251. * snp_shutdown - shut down the port - free irq and disable - no-op for us
  252. * @port: Port to shut down - we ignore
  253. *
  254. */
  255. static void snp_shutdown(struct uart_port *port)
  256. {
  257. }
  258. /**
  259. * snp_set_mctrl - set control lines (dtr, rts, etc) - no-op for our console
  260. * @port: Port to operate on - we ignore
  261. * @mctrl: Lines to set/unset - we ignore
  262. *
  263. */
  264. static void snp_set_mctrl(struct uart_port *port, unsigned int mctrl)
  265. {
  266. }
  267. /**
  268. * snp_get_mctrl - get contorl line info, we just return a static value
  269. * @port: port to operate on - we only have one port so we ignore this
  270. *
  271. */
  272. static unsigned int snp_get_mctrl(struct uart_port *port)
  273. {
  274. return TIOCM_CAR | TIOCM_RNG | TIOCM_DSR | TIOCM_CTS;
  275. }
  276. /**
  277. * snp_stop_rx - Stop the receiver - we ignor ethis
  278. * @port: Port to operate on - we ignore
  279. *
  280. */
  281. static void snp_stop_rx(struct uart_port *port)
  282. {
  283. }
  284. /**
  285. * snp_start_tx - Start transmitter
  286. * @port: Port to operate on
  287. *
  288. */
  289. static void snp_start_tx(struct uart_port *port)
  290. {
  291. if (sal_console_port.sc_ops->sal_wakeup_transmit)
  292. sal_console_port.sc_ops->sal_wakeup_transmit(&sal_console_port,
  293. TRANSMIT_BUFFERED);
  294. }
  295. /**
  296. * snp_break_ctl - handle breaks - ignored by us
  297. * @port: Port to operate on
  298. * @break_state: Break state
  299. *
  300. */
  301. static void snp_break_ctl(struct uart_port *port, int break_state)
  302. {
  303. }
  304. /**
  305. * snp_startup - Start up the serial port - always return 0 (We're always on)
  306. * @port: Port to operate on
  307. *
  308. */
  309. static int snp_startup(struct uart_port *port)
  310. {
  311. return 0;
  312. }
  313. /**
  314. * snp_set_termios - set termios stuff - we ignore these
  315. * @port: port to operate on
  316. * @termios: New settings
  317. * @termios: Old
  318. *
  319. */
  320. static void
  321. snp_set_termios(struct uart_port *port, struct termios *termios,
  322. struct termios *old)
  323. {
  324. }
  325. /**
  326. * snp_request_port - allocate resources for port - ignored by us
  327. * @port: port to operate on
  328. *
  329. */
  330. static int snp_request_port(struct uart_port *port)
  331. {
  332. return 0;
  333. }
  334. /**
  335. * snp_config_port - allocate resources, set up - we ignore, we're always on
  336. * @port: Port to operate on
  337. * @flags: flags used for port setup
  338. *
  339. */
  340. static void snp_config_port(struct uart_port *port, int flags)
  341. {
  342. }
  343. /* Associate the uart functions above - given to serial core */
  344. static struct uart_ops sn_console_ops = {
  345. .tx_empty = snp_tx_empty,
  346. .set_mctrl = snp_set_mctrl,
  347. .get_mctrl = snp_get_mctrl,
  348. .stop_tx = snp_stop_tx,
  349. .start_tx = snp_start_tx,
  350. .stop_rx = snp_stop_rx,
  351. .enable_ms = snp_enable_ms,
  352. .break_ctl = snp_break_ctl,
  353. .startup = snp_startup,
  354. .shutdown = snp_shutdown,
  355. .set_termios = snp_set_termios,
  356. .pm = NULL,
  357. .type = snp_type,
  358. .release_port = snp_release_port,
  359. .request_port = snp_request_port,
  360. .config_port = snp_config_port,
  361. .verify_port = NULL,
  362. };
  363. /* End of uart struct functions and defines */
  364. #ifdef DEBUG
  365. /**
  366. * sn_debug_printf - close to hardware debugging printf
  367. * @fmt: printf format
  368. *
  369. * This is as "close to the metal" as we can get, used when the driver
  370. * itself may be broken.
  371. *
  372. */
  373. static int sn_debug_printf(const char *fmt, ...)
  374. {
  375. static char printk_buf[1024];
  376. int printed_len;
  377. va_list args;
  378. va_start(args, fmt);
  379. printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
  380. if (!sal_console_port.sc_ops) {
  381. sal_console_port.sc_ops = &poll_ops;
  382. early_sn_setup();
  383. }
  384. sal_console_port.sc_ops->sal_puts_raw(printk_buf, printed_len);
  385. va_end(args);
  386. return printed_len;
  387. }
  388. #endif /* DEBUG */
  389. /*
  390. * Interrupt handling routines.
  391. */
  392. /**
  393. * sn_receive_chars - Grab characters, pass them to tty layer
  394. * @port: Port to operate on
  395. * @regs: Saved registers (needed by uart_handle_sysrq_char)
  396. * @flags: irq flags
  397. *
  398. * Note: If we're not registered with the serial core infrastructure yet,
  399. * we don't try to send characters to it...
  400. *
  401. */
  402. static void
  403. sn_receive_chars(struct sn_cons_port *port, struct pt_regs *regs,
  404. unsigned long flags)
  405. {
  406. int ch;
  407. struct tty_struct *tty;
  408. if (!port) {
  409. printk(KERN_ERR "sn_receive_chars - port NULL so can't receieve\n");
  410. return;
  411. }
  412. if (!port->sc_ops) {
  413. printk(KERN_ERR "sn_receive_chars - port->sc_ops NULL so can't receieve\n");
  414. return;
  415. }
  416. if (port->sc_port.info) {
  417. /* The serial_core stuffs are initilized, use them */
  418. tty = port->sc_port.info->tty;
  419. }
  420. else {
  421. /* Not registered yet - can't pass to tty layer. */
  422. tty = NULL;
  423. }
  424. while (port->sc_ops->sal_input_pending()) {
  425. ch = port->sc_ops->sal_getc();
  426. if (ch < 0) {
  427. printk(KERN_ERR "sn_console: An error occured while "
  428. "obtaining data from the console (0x%0x)\n", ch);
  429. break;
  430. }
  431. #ifdef CONFIG_MAGIC_SYSRQ
  432. if (sysrq_requested) {
  433. unsigned long sysrq_timeout = sysrq_requested + HZ*5;
  434. sysrq_requested = 0;
  435. if (ch && time_before(jiffies, sysrq_timeout)) {
  436. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  437. handle_sysrq(ch, regs, NULL);
  438. spin_lock_irqsave(&port->sc_port.lock, flags);
  439. /* ignore actual sysrq command char */
  440. continue;
  441. }
  442. }
  443. if (ch == *sysrq_serial_ptr) {
  444. if (!(*++sysrq_serial_ptr)) {
  445. sysrq_requested = jiffies;
  446. sysrq_serial_ptr = sysrq_serial_str;
  447. }
  448. /*
  449. * ignore the whole sysrq string except for the
  450. * leading escape
  451. */
  452. if (ch != '\e')
  453. continue;
  454. }
  455. else
  456. sysrq_serial_ptr = sysrq_serial_str;
  457. #endif /* CONFIG_MAGIC_SYSRQ */
  458. /* record the character to pass up to the tty layer */
  459. if (tty) {
  460. if(tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
  461. break;
  462. }
  463. port->sc_port.icount.rx++;
  464. }
  465. if (tty)
  466. tty_flip_buffer_push(tty);
  467. }
  468. /**
  469. * sn_transmit_chars - grab characters from serial core, send off
  470. * @port: Port to operate on
  471. * @raw: Transmit raw or buffered
  472. *
  473. * Note: If we're early, before we're registered with serial core, the
  474. * writes are going through sn_sal_console_write because that's how
  475. * register_console has been set up. We currently could have asynch
  476. * polls calling this function due to sn_sal_switch_to_asynch but we can
  477. * ignore them until we register with the serial core stuffs.
  478. *
  479. */
  480. static void sn_transmit_chars(struct sn_cons_port *port, int raw)
  481. {
  482. int xmit_count, tail, head, loops, ii;
  483. int result;
  484. char *start;
  485. struct circ_buf *xmit;
  486. if (!port)
  487. return;
  488. BUG_ON(!port->sc_is_asynch);
  489. if (port->sc_port.info) {
  490. /* We're initilized, using serial core infrastructure */
  491. xmit = &port->sc_port.info->xmit;
  492. } else {
  493. /* Probably sn_sal_switch_to_asynch has been run but serial core isn't
  494. * initilized yet. Just return. Writes are going through
  495. * sn_sal_console_write (due to register_console) at this time.
  496. */
  497. return;
  498. }
  499. if (uart_circ_empty(xmit) || uart_tx_stopped(&port->sc_port)) {
  500. /* Nothing to do. */
  501. ia64_sn_console_intr_disable(SAL_CONSOLE_INTR_XMIT);
  502. return;
  503. }
  504. head = xmit->head;
  505. tail = xmit->tail;
  506. start = &xmit->buf[tail];
  507. /* twice around gets the tail to the end of the buffer and
  508. * then to the head, if needed */
  509. loops = (head < tail) ? 2 : 1;
  510. for (ii = 0; ii < loops; ii++) {
  511. xmit_count = (head < tail) ?
  512. (UART_XMIT_SIZE - tail) : (head - tail);
  513. if (xmit_count > 0) {
  514. if (raw == TRANSMIT_RAW)
  515. result =
  516. port->sc_ops->sal_puts_raw(start,
  517. xmit_count);
  518. else
  519. result =
  520. port->sc_ops->sal_puts(start, xmit_count);
  521. #ifdef DEBUG
  522. if (!result)
  523. DPRINTF("`");
  524. #endif
  525. if (result > 0) {
  526. xmit_count -= result;
  527. port->sc_port.icount.tx += result;
  528. tail += result;
  529. tail &= UART_XMIT_SIZE - 1;
  530. xmit->tail = tail;
  531. start = &xmit->buf[tail];
  532. }
  533. }
  534. }
  535. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  536. uart_write_wakeup(&port->sc_port);
  537. if (uart_circ_empty(xmit))
  538. snp_stop_tx(&port->sc_port); /* no-op for us */
  539. }
  540. /**
  541. * sn_sal_interrupt - Handle console interrupts
  542. * @irq: irq #, useful for debug statements
  543. * @dev_id: our pointer to our port (sn_cons_port which contains the uart port)
  544. * @regs: Saved registers, used by sn_receive_chars for uart_handle_sysrq_char
  545. *
  546. */
  547. static irqreturn_t sn_sal_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  548. {
  549. struct sn_cons_port *port = (struct sn_cons_port *)dev_id;
  550. unsigned long flags;
  551. int status = ia64_sn_console_intr_status();
  552. if (!port)
  553. return IRQ_NONE;
  554. spin_lock_irqsave(&port->sc_port.lock, flags);
  555. if (status & SAL_CONSOLE_INTR_RECV) {
  556. sn_receive_chars(port, regs, flags);
  557. }
  558. if (status & SAL_CONSOLE_INTR_XMIT) {
  559. sn_transmit_chars(port, TRANSMIT_BUFFERED);
  560. }
  561. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  562. return IRQ_HANDLED;
  563. }
  564. /**
  565. * sn_sal_connect_interrupt - Request interrupt, handled by sn_sal_interrupt
  566. * @port: Our sn_cons_port (which contains the uart port)
  567. *
  568. * returns the console irq if interrupt is successfully registered, else 0
  569. *
  570. */
  571. static int sn_sal_connect_interrupt(struct sn_cons_port *port)
  572. {
  573. if (request_irq(SGI_UART_VECTOR, sn_sal_interrupt,
  574. SA_INTERRUPT | SA_SHIRQ,
  575. "SAL console driver", port) >= 0) {
  576. return SGI_UART_VECTOR;
  577. }
  578. printk(KERN_INFO "sn_console: console proceeding in polled mode\n");
  579. return 0;
  580. }
  581. /**
  582. * sn_sal_timer_poll - this function handles polled console mode
  583. * @data: A pointer to our sn_cons_port (which contains the uart port)
  584. *
  585. * data is the pointer that init_timer will store for us. This function is
  586. * associated with init_timer to see if there is any console traffic.
  587. * Obviously not used in interrupt mode
  588. *
  589. */
  590. static void sn_sal_timer_poll(unsigned long data)
  591. {
  592. struct sn_cons_port *port = (struct sn_cons_port *)data;
  593. unsigned long flags;
  594. if (!port)
  595. return;
  596. if (!port->sc_port.irq) {
  597. spin_lock_irqsave(&port->sc_port.lock, flags);
  598. if (sn_process_input)
  599. sn_receive_chars(port, NULL, flags);
  600. sn_transmit_chars(port, TRANSMIT_RAW);
  601. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  602. mod_timer(&port->sc_timer,
  603. jiffies + port->sc_interrupt_timeout);
  604. }
  605. }
  606. /*
  607. * Boot-time initialization code
  608. */
  609. /**
  610. * sn_sal_switch_to_asynch - Switch to async mode (as opposed to synch)
  611. * @port: Our sn_cons_port (which contains the uart port)
  612. *
  613. * So this is used by sn_sal_serial_console_init (early on, before we're
  614. * registered with serial core). It's also used by sn_sal_module_init
  615. * right after we've registered with serial core. The later only happens
  616. * if we didn't already come through here via sn_sal_serial_console_init.
  617. *
  618. */
  619. static void __init sn_sal_switch_to_asynch(struct sn_cons_port *port)
  620. {
  621. unsigned long flags;
  622. if (!port)
  623. return;
  624. DPRINTF("sn_console: about to switch to asynchronous console\n");
  625. /* without early_printk, we may be invoked late enough to race
  626. * with other cpus doing console IO at this point, however
  627. * console interrupts will never be enabled */
  628. spin_lock_irqsave(&port->sc_port.lock, flags);
  629. /* early_printk invocation may have done this for us */
  630. if (!port->sc_ops)
  631. port->sc_ops = &poll_ops;
  632. /* we can't turn on the console interrupt (as request_irq
  633. * calls kmalloc, which isn't set up yet), so we rely on a
  634. * timer to poll for input and push data from the console
  635. * buffer.
  636. */
  637. init_timer(&port->sc_timer);
  638. port->sc_timer.function = sn_sal_timer_poll;
  639. port->sc_timer.data = (unsigned long)port;
  640. if (IS_RUNNING_ON_SIMULATOR())
  641. port->sc_interrupt_timeout = 6;
  642. else {
  643. /* 960cps / 16 char FIFO = 60HZ
  644. * HZ / (SN_SAL_FIFO_SPEED_CPS / SN_SAL_FIFO_DEPTH) */
  645. port->sc_interrupt_timeout =
  646. HZ * SN_SAL_UART_FIFO_DEPTH / SN_SAL_UART_FIFO_SPEED_CPS;
  647. }
  648. mod_timer(&port->sc_timer, jiffies + port->sc_interrupt_timeout);
  649. port->sc_is_asynch = 1;
  650. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  651. }
  652. /**
  653. * sn_sal_switch_to_interrupts - Switch to interrupt driven mode
  654. * @port: Our sn_cons_port (which contains the uart port)
  655. *
  656. * In sn_sal_module_init, after we're registered with serial core and
  657. * the port is added, this function is called to switch us to interrupt
  658. * mode. We were previously in asynch/polling mode (using init_timer).
  659. *
  660. * We attempt to switch to interrupt mode here by calling
  661. * sn_sal_connect_interrupt. If that works out, we enable receive interrupts.
  662. */
  663. static void __init sn_sal_switch_to_interrupts(struct sn_cons_port *port)
  664. {
  665. int irq;
  666. unsigned long flags;
  667. if (!port)
  668. return;
  669. DPRINTF("sn_console: switching to interrupt driven console\n");
  670. spin_lock_irqsave(&port->sc_port.lock, flags);
  671. irq = sn_sal_connect_interrupt(port);
  672. if (irq) {
  673. port->sc_port.irq = irq;
  674. port->sc_ops = &intr_ops;
  675. /* turn on receive interrupts */
  676. ia64_sn_console_intr_enable(SAL_CONSOLE_INTR_RECV);
  677. }
  678. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  679. }
  680. /*
  681. * Kernel console definitions
  682. */
  683. static void sn_sal_console_write(struct console *, const char *, unsigned);
  684. static int __init sn_sal_console_setup(struct console *, char *);
  685. static struct uart_driver sal_console_uart;
  686. extern struct tty_driver *uart_console_device(struct console *, int *);
  687. static struct console sal_console = {
  688. .name = DEVICE_NAME,
  689. .write = sn_sal_console_write,
  690. .device = uart_console_device,
  691. .setup = sn_sal_console_setup,
  692. .index = -1, /* unspecified */
  693. .data = &sal_console_uart,
  694. };
  695. #define SAL_CONSOLE &sal_console
  696. static struct uart_driver sal_console_uart = {
  697. .owner = THIS_MODULE,
  698. .driver_name = "sn_console",
  699. .dev_name = DEVICE_NAME,
  700. .major = 0, /* major/minor set at registration time per USE_DYNAMIC_MINOR */
  701. .minor = 0,
  702. .nr = 1, /* one port */
  703. .cons = SAL_CONSOLE,
  704. };
  705. /**
  706. * sn_sal_module_init - When the kernel loads us, get us rolling w/ serial core
  707. *
  708. * Before this is called, we've been printing kernel messages in a special
  709. * early mode not making use of the serial core infrastructure. When our
  710. * driver is loaded for real, we register the driver and port with serial
  711. * core and try to enable interrupt driven mode.
  712. *
  713. */
  714. static int __init sn_sal_module_init(void)
  715. {
  716. int retval;
  717. if (!ia64_platform_is("sn2"))
  718. return -ENODEV;
  719. printk(KERN_INFO "sn_console: Console driver init\n");
  720. if (USE_DYNAMIC_MINOR == 1) {
  721. misc.minor = MISC_DYNAMIC_MINOR;
  722. misc.name = DEVICE_NAME_DYNAMIC;
  723. retval = misc_register(&misc);
  724. if (retval != 0) {
  725. printk
  726. ("Failed to register console device using misc_register.\n");
  727. return -ENODEV;
  728. }
  729. sal_console_uart.major = MISC_MAJOR;
  730. sal_console_uart.minor = misc.minor;
  731. } else {
  732. sal_console_uart.major = DEVICE_MAJOR;
  733. sal_console_uart.minor = DEVICE_MINOR;
  734. }
  735. /* We register the driver and the port before switching to interrupts
  736. * or async above so the proper uart structures are populated */
  737. if (uart_register_driver(&sal_console_uart) < 0) {
  738. printk
  739. ("ERROR sn_sal_module_init failed uart_register_driver, line %d\n",
  740. __LINE__);
  741. return -ENODEV;
  742. }
  743. spin_lock_init(&sal_console_port.sc_port.lock);
  744. /* Setup the port struct with the minimum needed */
  745. sal_console_port.sc_port.membase = (char *)1; /* just needs to be non-zero */
  746. sal_console_port.sc_port.type = PORT_16550A;
  747. sal_console_port.sc_port.fifosize = SN_SAL_MAX_CHARS;
  748. sal_console_port.sc_port.ops = &sn_console_ops;
  749. sal_console_port.sc_port.line = 0;
  750. if (uart_add_one_port(&sal_console_uart, &sal_console_port.sc_port) < 0) {
  751. /* error - not sure what I'd do - so I'll do nothing */
  752. printk(KERN_ERR "%s: unable to add port\n", __FUNCTION__);
  753. }
  754. /* when this driver is compiled in, the console initialization
  755. * will have already switched us into asynchronous operation
  756. * before we get here through the module initcalls */
  757. if (!sal_console_port.sc_is_asynch) {
  758. sn_sal_switch_to_asynch(&sal_console_port);
  759. }
  760. /* at this point (module_init) we can try to turn on interrupts */
  761. if (!IS_RUNNING_ON_SIMULATOR()) {
  762. sn_sal_switch_to_interrupts(&sal_console_port);
  763. }
  764. sn_process_input = 1;
  765. return 0;
  766. }
  767. /**
  768. * sn_sal_module_exit - When we're unloaded, remove the driver/port
  769. *
  770. */
  771. static void __exit sn_sal_module_exit(void)
  772. {
  773. del_timer_sync(&sal_console_port.sc_timer);
  774. uart_remove_one_port(&sal_console_uart, &sal_console_port.sc_port);
  775. uart_unregister_driver(&sal_console_uart);
  776. misc_deregister(&misc);
  777. }
  778. module_init(sn_sal_module_init);
  779. module_exit(sn_sal_module_exit);
  780. /**
  781. * puts_raw_fixed - sn_sal_console_write helper for adding \r's as required
  782. * @puts_raw : puts function to do the writing
  783. * @s: input string
  784. * @count: length
  785. *
  786. * We need a \r ahead of every \n for direct writes through
  787. * ia64_sn_console_putb (what sal_puts_raw below actually does).
  788. *
  789. */
  790. static void puts_raw_fixed(int (*puts_raw) (const char *s, int len),
  791. const char *s, int count)
  792. {
  793. const char *s1;
  794. /* Output '\r' before each '\n' */
  795. while ((s1 = memchr(s, '\n', count)) != NULL) {
  796. puts_raw(s, s1 - s);
  797. puts_raw("\r\n", 2);
  798. count -= s1 + 1 - s;
  799. s = s1 + 1;
  800. }
  801. puts_raw(s, count);
  802. }
  803. /**
  804. * sn_sal_console_write - Print statements before serial core available
  805. * @console: Console to operate on - we ignore since we have just one
  806. * @s: String to send
  807. * @count: length
  808. *
  809. * This is referenced in the console struct. It is used for early
  810. * console printing before we register with serial core and for things
  811. * such as kdb. The console_lock must be held when we get here.
  812. *
  813. * This function has some code for trying to print output even if the lock
  814. * is held. We try to cover the case where a lock holder could have died.
  815. * We don't use this special case code if we're not registered with serial
  816. * core yet. After we're registered with serial core, the only time this
  817. * function would be used is for high level kernel output like magic sys req,
  818. * kdb, and printk's.
  819. */
  820. static void
  821. sn_sal_console_write(struct console *co, const char *s, unsigned count)
  822. {
  823. unsigned long flags = 0;
  824. struct sn_cons_port *port = &sal_console_port;
  825. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  826. static int stole_lock = 0;
  827. #endif
  828. BUG_ON(!port->sc_is_asynch);
  829. /* We can't look at the xmit buffer if we're not registered with serial core
  830. * yet. So only do the fancy recovery after registering
  831. */
  832. if (port->sc_port.info) {
  833. /* somebody really wants this output, might be an
  834. * oops, kdb, panic, etc. make sure they get it. */
  835. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  836. if (spin_is_locked(&port->sc_port.lock)) {
  837. int lhead = port->sc_port.info->xmit.head;
  838. int ltail = port->sc_port.info->xmit.tail;
  839. int counter, got_lock = 0;
  840. /*
  841. * We attempt to determine if someone has died with the
  842. * lock. We wait ~20 secs after the head and tail ptrs
  843. * stop moving and assume the lock holder is not functional
  844. * and plow ahead. If the lock is freed within the time out
  845. * period we re-get the lock and go ahead normally. We also
  846. * remember if we have plowed ahead so that we don't have
  847. * to wait out the time out period again - the asumption
  848. * is that we will time out again.
  849. */
  850. for (counter = 0; counter < 150; mdelay(125), counter++) {
  851. if (!spin_is_locked(&port->sc_port.lock)
  852. || stole_lock) {
  853. if (!stole_lock) {
  854. spin_lock_irqsave(&port->
  855. sc_port.lock,
  856. flags);
  857. got_lock = 1;
  858. }
  859. break;
  860. } else {
  861. /* still locked */
  862. if ((lhead !=
  863. port->sc_port.info->xmit.head)
  864. || (ltail !=
  865. port->sc_port.info->xmit.
  866. tail)) {
  867. lhead =
  868. port->sc_port.info->xmit.
  869. head;
  870. ltail =
  871. port->sc_port.info->xmit.
  872. tail;
  873. counter = 0;
  874. }
  875. }
  876. }
  877. /* flush anything in the serial core xmit buffer, raw */
  878. sn_transmit_chars(port, 1);
  879. if (got_lock) {
  880. spin_unlock_irqrestore(&port->sc_port.lock,
  881. flags);
  882. stole_lock = 0;
  883. } else {
  884. /* fell thru */
  885. stole_lock = 1;
  886. }
  887. puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
  888. } else {
  889. stole_lock = 0;
  890. #endif
  891. spin_lock_irqsave(&port->sc_port.lock, flags);
  892. sn_transmit_chars(port, 1);
  893. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  894. puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
  895. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  896. }
  897. #endif
  898. }
  899. else {
  900. /* Not yet registered with serial core - simple case */
  901. puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
  902. }
  903. }
  904. /**
  905. * sn_sal_console_setup - Set up console for early printing
  906. * @co: Console to work with
  907. * @options: Options to set
  908. *
  909. * Altix console doesn't do anything with baud rates, etc, anyway.
  910. *
  911. * This isn't required since not providing the setup function in the
  912. * console struct is ok. However, other patches like KDB plop something
  913. * here so providing it is easier.
  914. *
  915. */
  916. static int __init sn_sal_console_setup(struct console *co, char *options)
  917. {
  918. return 0;
  919. }
  920. /**
  921. * sn_sal_console_write_early - simple early output routine
  922. * @co - console struct
  923. * @s - string to print
  924. * @count - count
  925. *
  926. * Simple function to provide early output, before even
  927. * sn_sal_serial_console_init is called. Referenced in the
  928. * console struct registerd in sn_serial_console_early_setup.
  929. *
  930. */
  931. static void __init
  932. sn_sal_console_write_early(struct console *co, const char *s, unsigned count)
  933. {
  934. puts_raw_fixed(sal_console_port.sc_ops->sal_puts_raw, s, count);
  935. }
  936. /* Used for very early console printing - again, before
  937. * sn_sal_serial_console_init is run */
  938. static struct console sal_console_early __initdata = {
  939. .name = "sn_sal",
  940. .write = sn_sal_console_write_early,
  941. .flags = CON_PRINTBUFFER,
  942. .index = -1,
  943. };
  944. /**
  945. * sn_serial_console_early_setup - Sets up early console output support
  946. *
  947. * Register a console early on... This is for output before even
  948. * sn_sal_serial_cosnole_init is called. This function is called from
  949. * setup.c. This allows us to do really early polled writes. When
  950. * sn_sal_serial_console_init is called, this console is unregistered
  951. * and a new one registered.
  952. */
  953. int __init sn_serial_console_early_setup(void)
  954. {
  955. if (!ia64_platform_is("sn2"))
  956. return -1;
  957. sal_console_port.sc_ops = &poll_ops;
  958. spin_lock_init(&sal_console_port.sc_port.lock);
  959. early_sn_setup(); /* Find SAL entry points */
  960. register_console(&sal_console_early);
  961. return 0;
  962. }
  963. /**
  964. * sn_sal_serial_console_init - Early console output - set up for register
  965. *
  966. * This function is called when regular console init happens. Because we
  967. * support even earlier console output with sn_serial_console_early_setup
  968. * (called from setup.c directly), this function unregisters the really
  969. * early console.
  970. *
  971. * Note: Even if setup.c doesn't register sal_console_early, unregistering
  972. * it here doesn't hurt anything.
  973. *
  974. */
  975. static int __init sn_sal_serial_console_init(void)
  976. {
  977. if (ia64_platform_is("sn2")) {
  978. sn_sal_switch_to_asynch(&sal_console_port);
  979. DPRINTF("sn_sal_serial_console_init : register console\n");
  980. register_console(&sal_console);
  981. unregister_console(&sal_console_early);
  982. }
  983. return 0;
  984. }
  985. console_initcall(sn_sal_serial_console_init);