sn_console.c 30 KB

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