sn_console.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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. * 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/interrupt.h>
  40. #include <linux/tty.h>
  41. #include <linux/tty_flip.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). initially, 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 ktermios *termios,
  322. struct ktermios *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. * @flags: irq flags
  396. *
  397. * Note: If we're not registered with the serial core infrastructure yet,
  398. * we don't try to send characters to it...
  399. *
  400. */
  401. static void
  402. sn_receive_chars(struct sn_cons_port *port, unsigned long flags)
  403. {
  404. int ch;
  405. struct tty_struct *tty;
  406. if (!port) {
  407. printk(KERN_ERR "sn_receive_chars - port NULL so can't receieve\n");
  408. return;
  409. }
  410. if (!port->sc_ops) {
  411. printk(KERN_ERR "sn_receive_chars - port->sc_ops NULL so can't receieve\n");
  412. return;
  413. }
  414. if (port->sc_port.state) {
  415. /* The serial_core stuffs are initialized, use them */
  416. tty = port->sc_port.state->port.tty;
  417. }
  418. else {
  419. /* Not registered yet - can't pass to tty layer. */
  420. tty = NULL;
  421. }
  422. while (port->sc_ops->sal_input_pending()) {
  423. ch = port->sc_ops->sal_getc();
  424. if (ch < 0) {
  425. printk(KERN_ERR "sn_console: An error occurred while "
  426. "obtaining data from the console (0x%0x)\n", ch);
  427. break;
  428. }
  429. #ifdef CONFIG_MAGIC_SYSRQ
  430. if (sysrq_requested) {
  431. unsigned long sysrq_timeout = sysrq_requested + HZ*5;
  432. sysrq_requested = 0;
  433. if (ch && time_before(jiffies, sysrq_timeout)) {
  434. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  435. handle_sysrq(ch);
  436. spin_lock_irqsave(&port->sc_port.lock, flags);
  437. /* ignore actual sysrq command char */
  438. continue;
  439. }
  440. }
  441. if (ch == *sysrq_serial_ptr) {
  442. if (!(*++sysrq_serial_ptr)) {
  443. sysrq_requested = jiffies;
  444. sysrq_serial_ptr = sysrq_serial_str;
  445. }
  446. /*
  447. * ignore the whole sysrq string except for the
  448. * leading escape
  449. */
  450. if (ch != '\e')
  451. continue;
  452. }
  453. else
  454. sysrq_serial_ptr = sysrq_serial_str;
  455. #endif /* CONFIG_MAGIC_SYSRQ */
  456. /* record the character to pass up to the tty layer */
  457. if (tty) {
  458. if(tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
  459. break;
  460. }
  461. port->sc_port.icount.rx++;
  462. }
  463. if (tty)
  464. tty_flip_buffer_push(tty);
  465. }
  466. /**
  467. * sn_transmit_chars - grab characters from serial core, send off
  468. * @port: Port to operate on
  469. * @raw: Transmit raw or buffered
  470. *
  471. * Note: If we're early, before we're registered with serial core, the
  472. * writes are going through sn_sal_console_write because that's how
  473. * register_console has been set up. We currently could have asynch
  474. * polls calling this function due to sn_sal_switch_to_asynch but we can
  475. * ignore them until we register with the serial core stuffs.
  476. *
  477. */
  478. static void sn_transmit_chars(struct sn_cons_port *port, int raw)
  479. {
  480. int xmit_count, tail, head, loops, ii;
  481. int result;
  482. char *start;
  483. struct circ_buf *xmit;
  484. if (!port)
  485. return;
  486. BUG_ON(!port->sc_is_asynch);
  487. if (port->sc_port.state) {
  488. /* We're initialized, using serial core infrastructure */
  489. xmit = &port->sc_port.state->xmit;
  490. } else {
  491. /* Probably sn_sal_switch_to_asynch has been run but serial core isn't
  492. * initialized yet. Just return. Writes are going through
  493. * sn_sal_console_write (due to register_console) at this time.
  494. */
  495. return;
  496. }
  497. if (uart_circ_empty(xmit) || uart_tx_stopped(&port->sc_port)) {
  498. /* Nothing to do. */
  499. ia64_sn_console_intr_disable(SAL_CONSOLE_INTR_XMIT);
  500. return;
  501. }
  502. head = xmit->head;
  503. tail = xmit->tail;
  504. start = &xmit->buf[tail];
  505. /* twice around gets the tail to the end of the buffer and
  506. * then to the head, if needed */
  507. loops = (head < tail) ? 2 : 1;
  508. for (ii = 0; ii < loops; ii++) {
  509. xmit_count = (head < tail) ?
  510. (UART_XMIT_SIZE - tail) : (head - tail);
  511. if (xmit_count > 0) {
  512. if (raw == TRANSMIT_RAW)
  513. result =
  514. port->sc_ops->sal_puts_raw(start,
  515. xmit_count);
  516. else
  517. result =
  518. port->sc_ops->sal_puts(start, xmit_count);
  519. #ifdef DEBUG
  520. if (!result)
  521. DPRINTF("`");
  522. #endif
  523. if (result > 0) {
  524. xmit_count -= result;
  525. port->sc_port.icount.tx += result;
  526. tail += result;
  527. tail &= UART_XMIT_SIZE - 1;
  528. xmit->tail = tail;
  529. start = &xmit->buf[tail];
  530. }
  531. }
  532. }
  533. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  534. uart_write_wakeup(&port->sc_port);
  535. if (uart_circ_empty(xmit))
  536. snp_stop_tx(&port->sc_port); /* no-op for us */
  537. }
  538. /**
  539. * sn_sal_interrupt - Handle console interrupts
  540. * @irq: irq #, useful for debug statements
  541. * @dev_id: our pointer to our port (sn_cons_port which contains the uart port)
  542. *
  543. */
  544. static irqreturn_t sn_sal_interrupt(int irq, void *dev_id)
  545. {
  546. struct sn_cons_port *port = (struct sn_cons_port *)dev_id;
  547. unsigned long flags;
  548. int status = ia64_sn_console_intr_status();
  549. if (!port)
  550. return IRQ_NONE;
  551. spin_lock_irqsave(&port->sc_port.lock, flags);
  552. if (status & SAL_CONSOLE_INTR_RECV) {
  553. sn_receive_chars(port, flags);
  554. }
  555. if (status & SAL_CONSOLE_INTR_XMIT) {
  556. sn_transmit_chars(port, TRANSMIT_BUFFERED);
  557. }
  558. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  559. return IRQ_HANDLED;
  560. }
  561. /**
  562. * sn_sal_timer_poll - this function handles polled console mode
  563. * @data: A pointer to our sn_cons_port (which contains the uart port)
  564. *
  565. * data is the pointer that init_timer will store for us. This function is
  566. * associated with init_timer to see if there is any console traffic.
  567. * Obviously not used in interrupt mode
  568. *
  569. */
  570. static void sn_sal_timer_poll(unsigned long data)
  571. {
  572. struct sn_cons_port *port = (struct sn_cons_port *)data;
  573. unsigned long flags;
  574. if (!port)
  575. return;
  576. if (!port->sc_port.irq) {
  577. spin_lock_irqsave(&port->sc_port.lock, flags);
  578. if (sn_process_input)
  579. sn_receive_chars(port, flags);
  580. sn_transmit_chars(port, TRANSMIT_RAW);
  581. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  582. mod_timer(&port->sc_timer,
  583. jiffies + port->sc_interrupt_timeout);
  584. }
  585. }
  586. /*
  587. * Boot-time initialization code
  588. */
  589. /**
  590. * sn_sal_switch_to_asynch - Switch to async mode (as opposed to synch)
  591. * @port: Our sn_cons_port (which contains the uart port)
  592. *
  593. * So this is used by sn_sal_serial_console_init (early on, before we're
  594. * registered with serial core). It's also used by sn_sal_module_init
  595. * right after we've registered with serial core. The later only happens
  596. * if we didn't already come through here via sn_sal_serial_console_init.
  597. *
  598. */
  599. static void __init sn_sal_switch_to_asynch(struct sn_cons_port *port)
  600. {
  601. unsigned long flags;
  602. if (!port)
  603. return;
  604. DPRINTF("sn_console: about to switch to asynchronous console\n");
  605. /* without early_printk, we may be invoked late enough to race
  606. * with other cpus doing console IO at this point, however
  607. * console interrupts will never be enabled */
  608. spin_lock_irqsave(&port->sc_port.lock, flags);
  609. /* early_printk invocation may have done this for us */
  610. if (!port->sc_ops)
  611. port->sc_ops = &poll_ops;
  612. /* we can't turn on the console interrupt (as request_irq
  613. * calls kmalloc, which isn't set up yet), so we rely on a
  614. * timer to poll for input and push data from the console
  615. * buffer.
  616. */
  617. init_timer(&port->sc_timer);
  618. port->sc_timer.function = sn_sal_timer_poll;
  619. port->sc_timer.data = (unsigned long)port;
  620. if (IS_RUNNING_ON_SIMULATOR())
  621. port->sc_interrupt_timeout = 6;
  622. else {
  623. /* 960cps / 16 char FIFO = 60HZ
  624. * HZ / (SN_SAL_FIFO_SPEED_CPS / SN_SAL_FIFO_DEPTH) */
  625. port->sc_interrupt_timeout =
  626. HZ * SN_SAL_UART_FIFO_DEPTH / SN_SAL_UART_FIFO_SPEED_CPS;
  627. }
  628. mod_timer(&port->sc_timer, jiffies + port->sc_interrupt_timeout);
  629. port->sc_is_asynch = 1;
  630. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  631. }
  632. /**
  633. * sn_sal_switch_to_interrupts - Switch to interrupt driven mode
  634. * @port: Our sn_cons_port (which contains the uart port)
  635. *
  636. * In sn_sal_module_init, after we're registered with serial core and
  637. * the port is added, this function is called to switch us to interrupt
  638. * mode. We were previously in asynch/polling mode (using init_timer).
  639. *
  640. * We attempt to switch to interrupt mode here by calling
  641. * request_irq. If that works out, we enable receive interrupts.
  642. */
  643. static void __init sn_sal_switch_to_interrupts(struct sn_cons_port *port)
  644. {
  645. unsigned long flags;
  646. if (port) {
  647. DPRINTF("sn_console: switching to interrupt driven console\n");
  648. if (request_irq(SGI_UART_VECTOR, sn_sal_interrupt,
  649. IRQF_SHARED,
  650. "SAL console driver", port) >= 0) {
  651. spin_lock_irqsave(&port->sc_port.lock, flags);
  652. port->sc_port.irq = SGI_UART_VECTOR;
  653. port->sc_ops = &intr_ops;
  654. /* turn on receive interrupts */
  655. ia64_sn_console_intr_enable(SAL_CONSOLE_INTR_RECV);
  656. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  657. }
  658. else {
  659. printk(KERN_INFO
  660. "sn_console: console proceeding in polled mode\n");
  661. }
  662. }
  663. }
  664. /*
  665. * Kernel console definitions
  666. */
  667. static void sn_sal_console_write(struct console *, const char *, unsigned);
  668. static int sn_sal_console_setup(struct console *, char *);
  669. static struct uart_driver sal_console_uart;
  670. extern struct tty_driver *uart_console_device(struct console *, int *);
  671. static struct console sal_console = {
  672. .name = DEVICE_NAME,
  673. .write = sn_sal_console_write,
  674. .device = uart_console_device,
  675. .setup = sn_sal_console_setup,
  676. .index = -1, /* unspecified */
  677. .data = &sal_console_uart,
  678. };
  679. #define SAL_CONSOLE &sal_console
  680. static struct uart_driver sal_console_uart = {
  681. .owner = THIS_MODULE,
  682. .driver_name = "sn_console",
  683. .dev_name = DEVICE_NAME,
  684. .major = 0, /* major/minor set at registration time per USE_DYNAMIC_MINOR */
  685. .minor = 0,
  686. .nr = 1, /* one port */
  687. .cons = SAL_CONSOLE,
  688. };
  689. /**
  690. * sn_sal_module_init - When the kernel loads us, get us rolling w/ serial core
  691. *
  692. * Before this is called, we've been printing kernel messages in a special
  693. * early mode not making use of the serial core infrastructure. When our
  694. * driver is loaded for real, we register the driver and port with serial
  695. * core and try to enable interrupt driven mode.
  696. *
  697. */
  698. static int __init sn_sal_module_init(void)
  699. {
  700. int retval;
  701. if (!ia64_platform_is("sn2"))
  702. return 0;
  703. printk(KERN_INFO "sn_console: Console driver init\n");
  704. if (USE_DYNAMIC_MINOR == 1) {
  705. misc.minor = MISC_DYNAMIC_MINOR;
  706. misc.name = DEVICE_NAME_DYNAMIC;
  707. retval = misc_register(&misc);
  708. if (retval != 0) {
  709. printk(KERN_WARNING "Failed to register console "
  710. "device using misc_register.\n");
  711. return -ENODEV;
  712. }
  713. sal_console_uart.major = MISC_MAJOR;
  714. sal_console_uart.minor = misc.minor;
  715. } else {
  716. sal_console_uart.major = DEVICE_MAJOR;
  717. sal_console_uart.minor = DEVICE_MINOR;
  718. }
  719. /* We register the driver and the port before switching to interrupts
  720. * or async above so the proper uart structures are populated */
  721. if (uart_register_driver(&sal_console_uart) < 0) {
  722. printk
  723. ("ERROR sn_sal_module_init failed uart_register_driver, line %d\n",
  724. __LINE__);
  725. return -ENODEV;
  726. }
  727. spin_lock_init(&sal_console_port.sc_port.lock);
  728. /* Setup the port struct with the minimum needed */
  729. sal_console_port.sc_port.membase = (char *)1; /* just needs to be non-zero */
  730. sal_console_port.sc_port.type = PORT_16550A;
  731. sal_console_port.sc_port.fifosize = SN_SAL_MAX_CHARS;
  732. sal_console_port.sc_port.ops = &sn_console_ops;
  733. sal_console_port.sc_port.line = 0;
  734. if (uart_add_one_port(&sal_console_uart, &sal_console_port.sc_port) < 0) {
  735. /* error - not sure what I'd do - so I'll do nothing */
  736. printk(KERN_ERR "%s: unable to add port\n", __func__);
  737. }
  738. /* when this driver is compiled in, the console initialization
  739. * will have already switched us into asynchronous operation
  740. * before we get here through the module initcalls */
  741. if (!sal_console_port.sc_is_asynch) {
  742. sn_sal_switch_to_asynch(&sal_console_port);
  743. }
  744. /* at this point (module_init) we can try to turn on interrupts */
  745. if (!IS_RUNNING_ON_SIMULATOR()) {
  746. sn_sal_switch_to_interrupts(&sal_console_port);
  747. }
  748. sn_process_input = 1;
  749. return 0;
  750. }
  751. /**
  752. * sn_sal_module_exit - When we're unloaded, remove the driver/port
  753. *
  754. */
  755. static void __exit sn_sal_module_exit(void)
  756. {
  757. del_timer_sync(&sal_console_port.sc_timer);
  758. uart_remove_one_port(&sal_console_uart, &sal_console_port.sc_port);
  759. uart_unregister_driver(&sal_console_uart);
  760. misc_deregister(&misc);
  761. }
  762. module_init(sn_sal_module_init);
  763. module_exit(sn_sal_module_exit);
  764. /**
  765. * puts_raw_fixed - sn_sal_console_write helper for adding \r's as required
  766. * @puts_raw : puts function to do the writing
  767. * @s: input string
  768. * @count: length
  769. *
  770. * We need a \r ahead of every \n for direct writes through
  771. * ia64_sn_console_putb (what sal_puts_raw below actually does).
  772. *
  773. */
  774. static void puts_raw_fixed(int (*puts_raw) (const char *s, int len),
  775. const char *s, int count)
  776. {
  777. const char *s1;
  778. /* Output '\r' before each '\n' */
  779. while ((s1 = memchr(s, '\n', count)) != NULL) {
  780. puts_raw(s, s1 - s);
  781. puts_raw("\r\n", 2);
  782. count -= s1 + 1 - s;
  783. s = s1 + 1;
  784. }
  785. puts_raw(s, count);
  786. }
  787. /**
  788. * sn_sal_console_write - Print statements before serial core available
  789. * @console: Console to operate on - we ignore since we have just one
  790. * @s: String to send
  791. * @count: length
  792. *
  793. * This is referenced in the console struct. It is used for early
  794. * console printing before we register with serial core and for things
  795. * such as kdb. The console_lock must be held when we get here.
  796. *
  797. * This function has some code for trying to print output even if the lock
  798. * is held. We try to cover the case where a lock holder could have died.
  799. * We don't use this special case code if we're not registered with serial
  800. * core yet. After we're registered with serial core, the only time this
  801. * function would be used is for high level kernel output like magic sys req,
  802. * kdb, and printk's.
  803. */
  804. static void
  805. sn_sal_console_write(struct console *co, const char *s, unsigned count)
  806. {
  807. unsigned long flags = 0;
  808. struct sn_cons_port *port = &sal_console_port;
  809. static int stole_lock = 0;
  810. BUG_ON(!port->sc_is_asynch);
  811. /* We can't look at the xmit buffer if we're not registered with serial core
  812. * yet. So only do the fancy recovery after registering
  813. */
  814. if (!port->sc_port.state) {
  815. /* Not yet registered with serial core - simple case */
  816. puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
  817. return;
  818. }
  819. /* somebody really wants this output, might be an
  820. * oops, kdb, panic, etc. make sure they get it. */
  821. if (spin_is_locked(&port->sc_port.lock)) {
  822. int lhead = port->sc_port.state->xmit.head;
  823. int ltail = port->sc_port.state->xmit.tail;
  824. int counter, got_lock = 0;
  825. /*
  826. * We attempt to determine if someone has died with the
  827. * lock. We wait ~20 secs after the head and tail ptrs
  828. * stop moving and assume the lock holder is not functional
  829. * and plow ahead. If the lock is freed within the time out
  830. * period we re-get the lock and go ahead normally. We also
  831. * remember if we have plowed ahead so that we don't have
  832. * to wait out the time out period again - the asumption
  833. * is that we will time out again.
  834. */
  835. for (counter = 0; counter < 150; mdelay(125), counter++) {
  836. if (!spin_is_locked(&port->sc_port.lock)
  837. || stole_lock) {
  838. if (!stole_lock) {
  839. spin_lock_irqsave(&port->sc_port.lock,
  840. flags);
  841. got_lock = 1;
  842. }
  843. break;
  844. } else {
  845. /* still locked */
  846. if ((lhead != port->sc_port.state->xmit.head)
  847. || (ltail !=
  848. port->sc_port.state->xmit.tail)) {
  849. lhead =
  850. port->sc_port.state->xmit.head;
  851. ltail =
  852. port->sc_port.state->xmit.tail;
  853. counter = 0;
  854. }
  855. }
  856. }
  857. /* flush anything in the serial core xmit buffer, raw */
  858. sn_transmit_chars(port, 1);
  859. if (got_lock) {
  860. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  861. stole_lock = 0;
  862. } else {
  863. /* fell thru */
  864. stole_lock = 1;
  865. }
  866. puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
  867. } else {
  868. stole_lock = 0;
  869. spin_lock_irqsave(&port->sc_port.lock, flags);
  870. sn_transmit_chars(port, 1);
  871. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  872. puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
  873. }
  874. }
  875. /**
  876. * sn_sal_console_setup - Set up console for early printing
  877. * @co: Console to work with
  878. * @options: Options to set
  879. *
  880. * Altix console doesn't do anything with baud rates, etc, anyway.
  881. *
  882. * This isn't required since not providing the setup function in the
  883. * console struct is ok. However, other patches like KDB plop something
  884. * here so providing it is easier.
  885. *
  886. */
  887. static int sn_sal_console_setup(struct console *co, char *options)
  888. {
  889. return 0;
  890. }
  891. /**
  892. * sn_sal_console_write_early - simple early output routine
  893. * @co - console struct
  894. * @s - string to print
  895. * @count - count
  896. *
  897. * Simple function to provide early output, before even
  898. * sn_sal_serial_console_init is called. Referenced in the
  899. * console struct registerd in sn_serial_console_early_setup.
  900. *
  901. */
  902. static void __init
  903. sn_sal_console_write_early(struct console *co, const char *s, unsigned count)
  904. {
  905. puts_raw_fixed(sal_console_port.sc_ops->sal_puts_raw, s, count);
  906. }
  907. /* Used for very early console printing - again, before
  908. * sn_sal_serial_console_init is run */
  909. static struct console sal_console_early __initdata = {
  910. .name = "sn_sal",
  911. .write = sn_sal_console_write_early,
  912. .flags = CON_PRINTBUFFER,
  913. .index = -1,
  914. };
  915. /**
  916. * sn_serial_console_early_setup - Sets up early console output support
  917. *
  918. * Register a console early on... This is for output before even
  919. * sn_sal_serial_cosnole_init is called. This function is called from
  920. * setup.c. This allows us to do really early polled writes. When
  921. * sn_sal_serial_console_init is called, this console is unregistered
  922. * and a new one registered.
  923. */
  924. int __init sn_serial_console_early_setup(void)
  925. {
  926. if (!ia64_platform_is("sn2"))
  927. return -1;
  928. sal_console_port.sc_ops = &poll_ops;
  929. spin_lock_init(&sal_console_port.sc_port.lock);
  930. early_sn_setup(); /* Find SAL entry points */
  931. register_console(&sal_console_early);
  932. return 0;
  933. }
  934. /**
  935. * sn_sal_serial_console_init - Early console output - set up for register
  936. *
  937. * This function is called when regular console init happens. Because we
  938. * support even earlier console output with sn_serial_console_early_setup
  939. * (called from setup.c directly), this function unregisters the really
  940. * early console.
  941. *
  942. * Note: Even if setup.c doesn't register sal_console_early, unregistering
  943. * it here doesn't hurt anything.
  944. *
  945. */
  946. static int __init sn_sal_serial_console_init(void)
  947. {
  948. if (ia64_platform_is("sn2")) {
  949. sn_sal_switch_to_asynch(&sal_console_port);
  950. DPRINTF("sn_sal_serial_console_init : register console\n");
  951. register_console(&sal_console);
  952. unregister_console(&sal_console_early);
  953. }
  954. return 0;
  955. }
  956. console_initcall(sn_sal_serial_console_init);