sn_console.c 28 KB

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