sn_console.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. * @tty_stop: Set to 1 if called via uart_stop
  230. *
  231. */
  232. static void snp_stop_tx(struct uart_port *port, unsigned int tty_stop)
  233. {
  234. }
  235. /**
  236. * snp_release_port - Free i/o and resources for port - no-op for us
  237. * @port: Port to operate on - we ignore - no-op function
  238. *
  239. */
  240. static void snp_release_port(struct uart_port *port)
  241. {
  242. }
  243. /**
  244. * snp_enable_ms - Force modem status interrupts on - no-op for us
  245. * @port: Port to operate on - we ignore - no-op function
  246. *
  247. */
  248. static void snp_enable_ms(struct uart_port *port)
  249. {
  250. }
  251. /**
  252. * snp_shutdown - shut down the port - free irq and disable - no-op for us
  253. * @port: Port to shut down - we ignore
  254. *
  255. */
  256. static void snp_shutdown(struct uart_port *port)
  257. {
  258. }
  259. /**
  260. * snp_set_mctrl - set control lines (dtr, rts, etc) - no-op for our console
  261. * @port: Port to operate on - we ignore
  262. * @mctrl: Lines to set/unset - we ignore
  263. *
  264. */
  265. static void snp_set_mctrl(struct uart_port *port, unsigned int mctrl)
  266. {
  267. }
  268. /**
  269. * snp_get_mctrl - get contorl line info, we just return a static value
  270. * @port: port to operate on - we only have one port so we ignore this
  271. *
  272. */
  273. static unsigned int snp_get_mctrl(struct uart_port *port)
  274. {
  275. return TIOCM_CAR | TIOCM_RNG | TIOCM_DSR | TIOCM_CTS;
  276. }
  277. /**
  278. * snp_stop_rx - Stop the receiver - we ignor ethis
  279. * @port: Port to operate on - we ignore
  280. *
  281. */
  282. static void snp_stop_rx(struct uart_port *port)
  283. {
  284. }
  285. /**
  286. * snp_start_tx - Start transmitter
  287. * @port: Port to operate on
  288. * @tty_stop: Set to 1 if called via uart_start
  289. *
  290. */
  291. static void snp_start_tx(struct uart_port *port, unsigned int tty_stop)
  292. {
  293. if (sal_console_port.sc_ops->sal_wakeup_transmit)
  294. sal_console_port.sc_ops->sal_wakeup_transmit(&sal_console_port,
  295. TRANSMIT_BUFFERED);
  296. }
  297. /**
  298. * snp_break_ctl - handle breaks - ignored by us
  299. * @port: Port to operate on
  300. * @break_state: Break state
  301. *
  302. */
  303. static void snp_break_ctl(struct uart_port *port, int break_state)
  304. {
  305. }
  306. /**
  307. * snp_startup - Start up the serial port - always return 0 (We're always on)
  308. * @port: Port to operate on
  309. *
  310. */
  311. static int snp_startup(struct uart_port *port)
  312. {
  313. return 0;
  314. }
  315. /**
  316. * snp_set_termios - set termios stuff - we ignore these
  317. * @port: port to operate on
  318. * @termios: New settings
  319. * @termios: Old
  320. *
  321. */
  322. static void
  323. snp_set_termios(struct uart_port *port, struct termios *termios,
  324. struct termios *old)
  325. {
  326. }
  327. /**
  328. * snp_request_port - allocate resources for port - ignored by us
  329. * @port: port to operate on
  330. *
  331. */
  332. static int snp_request_port(struct uart_port *port)
  333. {
  334. return 0;
  335. }
  336. /**
  337. * snp_config_port - allocate resources, set up - we ignore, we're always on
  338. * @port: Port to operate on
  339. * @flags: flags used for port setup
  340. *
  341. */
  342. static void snp_config_port(struct uart_port *port, int flags)
  343. {
  344. }
  345. /* Associate the uart functions above - given to serial core */
  346. static struct uart_ops sn_console_ops = {
  347. .tx_empty = snp_tx_empty,
  348. .set_mctrl = snp_set_mctrl,
  349. .get_mctrl = snp_get_mctrl,
  350. .stop_tx = snp_stop_tx,
  351. .start_tx = snp_start_tx,
  352. .stop_rx = snp_stop_rx,
  353. .enable_ms = snp_enable_ms,
  354. .break_ctl = snp_break_ctl,
  355. .startup = snp_startup,
  356. .shutdown = snp_shutdown,
  357. .set_termios = snp_set_termios,
  358. .pm = NULL,
  359. .type = snp_type,
  360. .release_port = snp_release_port,
  361. .request_port = snp_request_port,
  362. .config_port = snp_config_port,
  363. .verify_port = NULL,
  364. };
  365. /* End of uart struct functions and defines */
  366. #ifdef DEBUG
  367. /**
  368. * sn_debug_printf - close to hardware debugging printf
  369. * @fmt: printf format
  370. *
  371. * This is as "close to the metal" as we can get, used when the driver
  372. * itself may be broken.
  373. *
  374. */
  375. static int sn_debug_printf(const char *fmt, ...)
  376. {
  377. static char printk_buf[1024];
  378. int printed_len;
  379. va_list args;
  380. va_start(args, fmt);
  381. printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
  382. if (!sal_console_port.sc_ops) {
  383. sal_console_port.sc_ops = &poll_ops;
  384. early_sn_setup();
  385. }
  386. sal_console_port.sc_ops->sal_puts_raw(printk_buf, printed_len);
  387. va_end(args);
  388. return printed_len;
  389. }
  390. #endif /* DEBUG */
  391. /*
  392. * Interrupt handling routines.
  393. */
  394. /**
  395. * sn_receive_chars - Grab characters, pass them to tty layer
  396. * @port: Port to operate on
  397. * @regs: Saved registers (needed by uart_handle_sysrq_char)
  398. * @flags: irq flags
  399. *
  400. * Note: If we're not registered with the serial core infrastructure yet,
  401. * we don't try to send characters to it...
  402. *
  403. */
  404. static void
  405. sn_receive_chars(struct sn_cons_port *port, struct pt_regs *regs,
  406. unsigned long flags)
  407. {
  408. int ch;
  409. struct tty_struct *tty;
  410. if (!port) {
  411. printk(KERN_ERR "sn_receive_chars - port NULL so can't receieve\n");
  412. return;
  413. }
  414. if (!port->sc_ops) {
  415. printk(KERN_ERR "sn_receive_chars - port->sc_ops NULL so can't receieve\n");
  416. return;
  417. }
  418. if (port->sc_port.info) {
  419. /* The serial_core stuffs are initilized, use them */
  420. tty = port->sc_port.info->tty;
  421. }
  422. else {
  423. /* Not registered yet - can't pass to tty layer. */
  424. tty = NULL;
  425. }
  426. while (port->sc_ops->sal_input_pending()) {
  427. ch = port->sc_ops->sal_getc();
  428. if (ch < 0) {
  429. printk(KERN_ERR "sn_console: An error occured while "
  430. "obtaining data from the console (0x%0x)\n", ch);
  431. break;
  432. }
  433. #ifdef CONFIG_MAGIC_SYSRQ
  434. if (sysrq_requested) {
  435. unsigned long sysrq_timeout = sysrq_requested + HZ*5;
  436. sysrq_requested = 0;
  437. if (ch && time_before(jiffies, sysrq_timeout)) {
  438. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  439. handle_sysrq(ch, regs, NULL);
  440. spin_lock_irqsave(&port->sc_port.lock, flags);
  441. /* ignore actual sysrq command char */
  442. continue;
  443. }
  444. }
  445. if (ch == *sysrq_serial_ptr) {
  446. if (!(*++sysrq_serial_ptr)) {
  447. sysrq_requested = jiffies;
  448. sysrq_serial_ptr = sysrq_serial_str;
  449. }
  450. /*
  451. * ignore the whole sysrq string except for the
  452. * leading escape
  453. */
  454. if (ch != '\e')
  455. continue;
  456. }
  457. else
  458. sysrq_serial_ptr = sysrq_serial_str;
  459. #endif /* CONFIG_MAGIC_SYSRQ */
  460. /* record the character to pass up to the tty layer */
  461. if (tty) {
  462. *tty->flip.char_buf_ptr = ch;
  463. *tty->flip.flag_buf_ptr = TTY_NORMAL;
  464. tty->flip.char_buf_ptr++;
  465. tty->flip.count++;
  466. if (tty->flip.count == TTY_FLIPBUF_SIZE)
  467. break;
  468. }
  469. port->sc_port.icount.rx++;
  470. }
  471. if (tty)
  472. tty_flip_buffer_push(tty);
  473. }
  474. /**
  475. * sn_transmit_chars - grab characters from serial core, send off
  476. * @port: Port to operate on
  477. * @raw: Transmit raw or buffered
  478. *
  479. * Note: If we're early, before we're registered with serial core, the
  480. * writes are going through sn_sal_console_write because that's how
  481. * register_console has been set up. We currently could have asynch
  482. * polls calling this function due to sn_sal_switch_to_asynch but we can
  483. * ignore them until we register with the serial core stuffs.
  484. *
  485. */
  486. static void sn_transmit_chars(struct sn_cons_port *port, int raw)
  487. {
  488. int xmit_count, tail, head, loops, ii;
  489. int result;
  490. char *start;
  491. struct circ_buf *xmit;
  492. if (!port)
  493. return;
  494. BUG_ON(!port->sc_is_asynch);
  495. if (port->sc_port.info) {
  496. /* We're initilized, using serial core infrastructure */
  497. xmit = &port->sc_port.info->xmit;
  498. } else {
  499. /* Probably sn_sal_switch_to_asynch has been run but serial core isn't
  500. * initilized yet. Just return. Writes are going through
  501. * sn_sal_console_write (due to register_console) at this time.
  502. */
  503. return;
  504. }
  505. if (uart_circ_empty(xmit) || uart_tx_stopped(&port->sc_port)) {
  506. /* Nothing to do. */
  507. return;
  508. }
  509. head = xmit->head;
  510. tail = xmit->tail;
  511. start = &xmit->buf[tail];
  512. /* twice around gets the tail to the end of the buffer and
  513. * then to the head, if needed */
  514. loops = (head < tail) ? 2 : 1;
  515. for (ii = 0; ii < loops; ii++) {
  516. xmit_count = (head < tail) ?
  517. (UART_XMIT_SIZE - tail) : (head - tail);
  518. if (xmit_count > 0) {
  519. if (raw == TRANSMIT_RAW)
  520. result =
  521. port->sc_ops->sal_puts_raw(start,
  522. xmit_count);
  523. else
  524. result =
  525. port->sc_ops->sal_puts(start, xmit_count);
  526. #ifdef DEBUG
  527. if (!result)
  528. DPRINTF("`");
  529. #endif
  530. if (result > 0) {
  531. xmit_count -= result;
  532. port->sc_port.icount.tx += result;
  533. tail += result;
  534. tail &= UART_XMIT_SIZE - 1;
  535. xmit->tail = tail;
  536. start = &xmit->buf[tail];
  537. }
  538. }
  539. }
  540. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  541. uart_write_wakeup(&port->sc_port);
  542. if (uart_circ_empty(xmit))
  543. snp_stop_tx(&port->sc_port, 0); /* no-op for us */
  544. }
  545. /**
  546. * sn_sal_interrupt - Handle console interrupts
  547. * @irq: irq #, useful for debug statements
  548. * @dev_id: our pointer to our port (sn_cons_port which contains the uart port)
  549. * @regs: Saved registers, used by sn_receive_chars for uart_handle_sysrq_char
  550. *
  551. */
  552. static irqreturn_t sn_sal_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  553. {
  554. struct sn_cons_port *port = (struct sn_cons_port *)dev_id;
  555. unsigned long flags;
  556. int status = ia64_sn_console_intr_status();
  557. if (!port)
  558. return IRQ_NONE;
  559. spin_lock_irqsave(&port->sc_port.lock, flags);
  560. if (status & SAL_CONSOLE_INTR_RECV) {
  561. sn_receive_chars(port, regs, flags);
  562. }
  563. if (status & SAL_CONSOLE_INTR_XMIT) {
  564. sn_transmit_chars(port, TRANSMIT_BUFFERED);
  565. }
  566. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  567. return IRQ_HANDLED;
  568. }
  569. /**
  570. * sn_sal_connect_interrupt - Request interrupt, handled by sn_sal_interrupt
  571. * @port: Our sn_cons_port (which contains the uart port)
  572. *
  573. * returns the console irq if interrupt is successfully registered, else 0
  574. *
  575. */
  576. static int sn_sal_connect_interrupt(struct sn_cons_port *port)
  577. {
  578. if (request_irq(SGI_UART_VECTOR, sn_sal_interrupt,
  579. SA_INTERRUPT | SA_SHIRQ,
  580. "SAL console driver", port) >= 0) {
  581. return SGI_UART_VECTOR;
  582. }
  583. printk(KERN_INFO "sn_console: console proceeding in polled mode\n");
  584. return 0;
  585. }
  586. /**
  587. * sn_sal_timer_poll - this function handles polled console mode
  588. * @data: A pointer to our sn_cons_port (which contains the uart port)
  589. *
  590. * data is the pointer that init_timer will store for us. This function is
  591. * associated with init_timer to see if there is any console traffic.
  592. * Obviously not used in interrupt mode
  593. *
  594. */
  595. static void sn_sal_timer_poll(unsigned long data)
  596. {
  597. struct sn_cons_port *port = (struct sn_cons_port *)data;
  598. unsigned long flags;
  599. if (!port)
  600. return;
  601. if (!port->sc_port.irq) {
  602. spin_lock_irqsave(&port->sc_port.lock, flags);
  603. if (sn_process_input)
  604. sn_receive_chars(port, NULL, flags);
  605. sn_transmit_chars(port, TRANSMIT_RAW);
  606. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  607. mod_timer(&port->sc_timer,
  608. jiffies + port->sc_interrupt_timeout);
  609. }
  610. }
  611. /*
  612. * Boot-time initialization code
  613. */
  614. /**
  615. * sn_sal_switch_to_asynch - Switch to async mode (as opposed to synch)
  616. * @port: Our sn_cons_port (which contains the uart port)
  617. *
  618. * So this is used by sn_sal_serial_console_init (early on, before we're
  619. * registered with serial core). It's also used by sn_sal_module_init
  620. * right after we've registered with serial core. The later only happens
  621. * if we didn't already come through here via sn_sal_serial_console_init.
  622. *
  623. */
  624. static void __init sn_sal_switch_to_asynch(struct sn_cons_port *port)
  625. {
  626. unsigned long flags;
  627. if (!port)
  628. return;
  629. DPRINTF("sn_console: about to switch to asynchronous console\n");
  630. /* without early_printk, we may be invoked late enough to race
  631. * with other cpus doing console IO at this point, however
  632. * console interrupts will never be enabled */
  633. spin_lock_irqsave(&port->sc_port.lock, flags);
  634. /* early_printk invocation may have done this for us */
  635. if (!port->sc_ops)
  636. port->sc_ops = &poll_ops;
  637. /* we can't turn on the console interrupt (as request_irq
  638. * calls kmalloc, which isn't set up yet), so we rely on a
  639. * timer to poll for input and push data from the console
  640. * buffer.
  641. */
  642. init_timer(&port->sc_timer);
  643. port->sc_timer.function = sn_sal_timer_poll;
  644. port->sc_timer.data = (unsigned long)port;
  645. if (IS_RUNNING_ON_SIMULATOR())
  646. port->sc_interrupt_timeout = 6;
  647. else {
  648. /* 960cps / 16 char FIFO = 60HZ
  649. * HZ / (SN_SAL_FIFO_SPEED_CPS / SN_SAL_FIFO_DEPTH) */
  650. port->sc_interrupt_timeout =
  651. HZ * SN_SAL_UART_FIFO_DEPTH / SN_SAL_UART_FIFO_SPEED_CPS;
  652. }
  653. mod_timer(&port->sc_timer, jiffies + port->sc_interrupt_timeout);
  654. port->sc_is_asynch = 1;
  655. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  656. }
  657. /**
  658. * sn_sal_switch_to_interrupts - Switch to interrupt driven mode
  659. * @port: Our sn_cons_port (which contains the uart port)
  660. *
  661. * In sn_sal_module_init, after we're registered with serial core and
  662. * the port is added, this function is called to switch us to interrupt
  663. * mode. We were previously in asynch/polling mode (using init_timer).
  664. *
  665. * We attempt to switch to interrupt mode here by calling
  666. * sn_sal_connect_interrupt. If that works out, we enable receive interrupts.
  667. */
  668. static void __init sn_sal_switch_to_interrupts(struct sn_cons_port *port)
  669. {
  670. int irq;
  671. unsigned long flags;
  672. if (!port)
  673. return;
  674. DPRINTF("sn_console: switching to interrupt driven console\n");
  675. spin_lock_irqsave(&port->sc_port.lock, flags);
  676. irq = sn_sal_connect_interrupt(port);
  677. if (irq) {
  678. port->sc_port.irq = irq;
  679. port->sc_ops = &intr_ops;
  680. /* turn on receive interrupts */
  681. ia64_sn_console_intr_enable(SAL_CONSOLE_INTR_RECV);
  682. }
  683. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  684. }
  685. /*
  686. * Kernel console definitions
  687. */
  688. static void sn_sal_console_write(struct console *, const char *, unsigned);
  689. static int __init sn_sal_console_setup(struct console *, char *);
  690. static struct uart_driver sal_console_uart;
  691. extern struct tty_driver *uart_console_device(struct console *, int *);
  692. static struct console sal_console = {
  693. .name = DEVICE_NAME,
  694. .write = sn_sal_console_write,
  695. .device = uart_console_device,
  696. .setup = sn_sal_console_setup,
  697. .index = -1, /* unspecified */
  698. .data = &sal_console_uart,
  699. };
  700. #define SAL_CONSOLE &sal_console
  701. static struct uart_driver sal_console_uart = {
  702. .owner = THIS_MODULE,
  703. .driver_name = "sn_console",
  704. .dev_name = DEVICE_NAME,
  705. .major = 0, /* major/minor set at registration time per USE_DYNAMIC_MINOR */
  706. .minor = 0,
  707. .nr = 1, /* one port */
  708. .cons = SAL_CONSOLE,
  709. };
  710. /**
  711. * sn_sal_module_init - When the kernel loads us, get us rolling w/ serial core
  712. *
  713. * Before this is called, we've been printing kernel messages in a special
  714. * early mode not making use of the serial core infrastructure. When our
  715. * driver is loaded for real, we register the driver and port with serial
  716. * core and try to enable interrupt driven mode.
  717. *
  718. */
  719. static int __init sn_sal_module_init(void)
  720. {
  721. int retval;
  722. if (!ia64_platform_is("sn2"))
  723. return -ENODEV;
  724. printk(KERN_INFO "sn_console: Console driver init\n");
  725. if (USE_DYNAMIC_MINOR == 1) {
  726. misc.minor = MISC_DYNAMIC_MINOR;
  727. misc.name = DEVICE_NAME_DYNAMIC;
  728. retval = misc_register(&misc);
  729. if (retval != 0) {
  730. printk
  731. ("Failed to register console device using misc_register.\n");
  732. return -ENODEV;
  733. }
  734. sal_console_uart.major = MISC_MAJOR;
  735. sal_console_uart.minor = misc.minor;
  736. } else {
  737. sal_console_uart.major = DEVICE_MAJOR;
  738. sal_console_uart.minor = DEVICE_MINOR;
  739. }
  740. /* We register the driver and the port before switching to interrupts
  741. * or async above so the proper uart structures are populated */
  742. if (uart_register_driver(&sal_console_uart) < 0) {
  743. printk
  744. ("ERROR sn_sal_module_init failed uart_register_driver, line %d\n",
  745. __LINE__);
  746. return -ENODEV;
  747. }
  748. spin_lock_init(&sal_console_port.sc_port.lock);
  749. /* Setup the port struct with the minimum needed */
  750. sal_console_port.sc_port.membase = (char *)1; /* just needs to be non-zero */
  751. sal_console_port.sc_port.type = PORT_16550A;
  752. sal_console_port.sc_port.fifosize = SN_SAL_MAX_CHARS;
  753. sal_console_port.sc_port.ops = &sn_console_ops;
  754. sal_console_port.sc_port.line = 0;
  755. if (uart_add_one_port(&sal_console_uart, &sal_console_port.sc_port) < 0) {
  756. /* error - not sure what I'd do - so I'll do nothing */
  757. printk(KERN_ERR "%s: unable to add port\n", __FUNCTION__);
  758. }
  759. /* when this driver is compiled in, the console initialization
  760. * will have already switched us into asynchronous operation
  761. * before we get here through the module initcalls */
  762. if (!sal_console_port.sc_is_asynch) {
  763. sn_sal_switch_to_asynch(&sal_console_port);
  764. }
  765. /* at this point (module_init) we can try to turn on interrupts */
  766. if (!IS_RUNNING_ON_SIMULATOR()) {
  767. sn_sal_switch_to_interrupts(&sal_console_port);
  768. }
  769. sn_process_input = 1;
  770. return 0;
  771. }
  772. /**
  773. * sn_sal_module_exit - When we're unloaded, remove the driver/port
  774. *
  775. */
  776. static void __exit sn_sal_module_exit(void)
  777. {
  778. del_timer_sync(&sal_console_port.sc_timer);
  779. uart_remove_one_port(&sal_console_uart, &sal_console_port.sc_port);
  780. uart_unregister_driver(&sal_console_uart);
  781. misc_deregister(&misc);
  782. }
  783. module_init(sn_sal_module_init);
  784. module_exit(sn_sal_module_exit);
  785. /**
  786. * puts_raw_fixed - sn_sal_console_write helper for adding \r's as required
  787. * @puts_raw : puts function to do the writing
  788. * @s: input string
  789. * @count: length
  790. *
  791. * We need a \r ahead of every \n for direct writes through
  792. * ia64_sn_console_putb (what sal_puts_raw below actually does).
  793. *
  794. */
  795. static void puts_raw_fixed(int (*puts_raw) (const char *s, int len),
  796. const char *s, int count)
  797. {
  798. const char *s1;
  799. /* Output '\r' before each '\n' */
  800. while ((s1 = memchr(s, '\n', count)) != NULL) {
  801. puts_raw(s, s1 - s);
  802. puts_raw("\r\n", 2);
  803. count -= s1 + 1 - s;
  804. s = s1 + 1;
  805. }
  806. puts_raw(s, count);
  807. }
  808. /**
  809. * sn_sal_console_write - Print statements before serial core available
  810. * @console: Console to operate on - we ignore since we have just one
  811. * @s: String to send
  812. * @count: length
  813. *
  814. * This is referenced in the console struct. It is used for early
  815. * console printing before we register with serial core and for things
  816. * such as kdb. The console_lock must be held when we get here.
  817. *
  818. * This function has some code for trying to print output even if the lock
  819. * is held. We try to cover the case where a lock holder could have died.
  820. * We don't use this special case code if we're not registered with serial
  821. * core yet. After we're registered with serial core, the only time this
  822. * function would be used is for high level kernel output like magic sys req,
  823. * kdb, and printk's.
  824. */
  825. static void
  826. sn_sal_console_write(struct console *co, const char *s, unsigned count)
  827. {
  828. unsigned long flags = 0;
  829. struct sn_cons_port *port = &sal_console_port;
  830. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  831. static int stole_lock = 0;
  832. #endif
  833. BUG_ON(!port->sc_is_asynch);
  834. /* We can't look at the xmit buffer if we're not registered with serial core
  835. * yet. So only do the fancy recovery after registering
  836. */
  837. if (port->sc_port.info) {
  838. /* somebody really wants this output, might be an
  839. * oops, kdb, panic, etc. make sure they get it. */
  840. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  841. if (spin_is_locked(&port->sc_port.lock)) {
  842. int lhead = port->sc_port.info->xmit.head;
  843. int ltail = port->sc_port.info->xmit.tail;
  844. int counter, got_lock = 0;
  845. /*
  846. * We attempt to determine if someone has died with the
  847. * lock. We wait ~20 secs after the head and tail ptrs
  848. * stop moving and assume the lock holder is not functional
  849. * and plow ahead. If the lock is freed within the time out
  850. * period we re-get the lock and go ahead normally. We also
  851. * remember if we have plowed ahead so that we don't have
  852. * to wait out the time out period again - the asumption
  853. * is that we will time out again.
  854. */
  855. for (counter = 0; counter < 150; mdelay(125), counter++) {
  856. if (!spin_is_locked(&port->sc_port.lock)
  857. || stole_lock) {
  858. if (!stole_lock) {
  859. spin_lock_irqsave(&port->
  860. sc_port.lock,
  861. flags);
  862. got_lock = 1;
  863. }
  864. break;
  865. } else {
  866. /* still locked */
  867. if ((lhead !=
  868. port->sc_port.info->xmit.head)
  869. || (ltail !=
  870. port->sc_port.info->xmit.
  871. tail)) {
  872. lhead =
  873. port->sc_port.info->xmit.
  874. head;
  875. ltail =
  876. port->sc_port.info->xmit.
  877. tail;
  878. counter = 0;
  879. }
  880. }
  881. }
  882. /* flush anything in the serial core xmit buffer, raw */
  883. sn_transmit_chars(port, 1);
  884. if (got_lock) {
  885. spin_unlock_irqrestore(&port->sc_port.lock,
  886. flags);
  887. stole_lock = 0;
  888. } else {
  889. /* fell thru */
  890. stole_lock = 1;
  891. }
  892. puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
  893. } else {
  894. stole_lock = 0;
  895. #endif
  896. spin_lock_irqsave(&port->sc_port.lock, flags);
  897. sn_transmit_chars(port, 1);
  898. spin_unlock_irqrestore(&port->sc_port.lock, flags);
  899. puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
  900. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  901. }
  902. #endif
  903. }
  904. else {
  905. /* Not yet registered with serial core - simple case */
  906. puts_raw_fixed(port->sc_ops->sal_puts_raw, s, count);
  907. }
  908. }
  909. /**
  910. * sn_sal_console_setup - Set up console for early printing
  911. * @co: Console to work with
  912. * @options: Options to set
  913. *
  914. * Altix console doesn't do anything with baud rates, etc, anyway.
  915. *
  916. * This isn't required since not providing the setup function in the
  917. * console struct is ok. However, other patches like KDB plop something
  918. * here so providing it is easier.
  919. *
  920. */
  921. static int __init sn_sal_console_setup(struct console *co, char *options)
  922. {
  923. return 0;
  924. }
  925. /**
  926. * sn_sal_console_write_early - simple early output routine
  927. * @co - console struct
  928. * @s - string to print
  929. * @count - count
  930. *
  931. * Simple function to provide early output, before even
  932. * sn_sal_serial_console_init is called. Referenced in the
  933. * console struct registerd in sn_serial_console_early_setup.
  934. *
  935. */
  936. static void __init
  937. sn_sal_console_write_early(struct console *co, const char *s, unsigned count)
  938. {
  939. puts_raw_fixed(sal_console_port.sc_ops->sal_puts_raw, s, count);
  940. }
  941. /* Used for very early console printing - again, before
  942. * sn_sal_serial_console_init is run */
  943. static struct console sal_console_early __initdata = {
  944. .name = "sn_sal",
  945. .write = sn_sal_console_write_early,
  946. .flags = CON_PRINTBUFFER,
  947. .index = -1,
  948. };
  949. /**
  950. * sn_serial_console_early_setup - Sets up early console output support
  951. *
  952. * Register a console early on... This is for output before even
  953. * sn_sal_serial_cosnole_init is called. This function is called from
  954. * setup.c. This allows us to do really early polled writes. When
  955. * sn_sal_serial_console_init is called, this console is unregistered
  956. * and a new one registered.
  957. */
  958. int __init sn_serial_console_early_setup(void)
  959. {
  960. if (!ia64_platform_is("sn2"))
  961. return -1;
  962. sal_console_port.sc_ops = &poll_ops;
  963. early_sn_setup(); /* Find SAL entry points */
  964. register_console(&sal_console_early);
  965. return 0;
  966. }
  967. /**
  968. * sn_sal_serial_console_init - Early console output - set up for register
  969. *
  970. * This function is called when regular console init happens. Because we
  971. * support even earlier console output with sn_serial_console_early_setup
  972. * (called from setup.c directly), this function unregisters the really
  973. * early console.
  974. *
  975. * Note: Even if setup.c doesn't register sal_console_early, unregistering
  976. * it here doesn't hurt anything.
  977. *
  978. */
  979. static int __init sn_sal_serial_console_init(void)
  980. {
  981. if (ia64_platform_is("sn2")) {
  982. sn_sal_switch_to_asynch(&sal_console_port);
  983. DPRINTF("sn_sal_serial_console_init : register console\n");
  984. register_console(&sal_console);
  985. unregister_console(&sal_console_early);
  986. }
  987. return 0;
  988. }
  989. console_initcall(sn_sal_serial_console_init);