mux.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. ** mux.c:
  3. ** serial driver for the Mux console found in some PA-RISC servers.
  4. **
  5. ** (c) Copyright 2002 Ryan Bradetich
  6. ** (c) Copyright 2002 Hewlett-Packard Company
  7. **
  8. ** This program is free software; you can redistribute it and/or modify
  9. ** it under the terms of the GNU General Public License as published by
  10. ** the Free Software Foundation; either version 2 of the License, or
  11. ** (at your option) any later version.
  12. **
  13. ** This Driver currently only supports the console (port 0) on the MUX.
  14. ** Additional work will be needed on this driver to enable the full
  15. ** functionality of the MUX.
  16. **
  17. */
  18. #include <linux/module.h>
  19. #include <linux/ioport.h>
  20. #include <linux/init.h>
  21. #include <linux/serial.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/console.h>
  25. #include <linux/delay.h> /* for udelay */
  26. #include <linux/device.h>
  27. #include <asm/io.h>
  28. #include <asm/irq.h>
  29. #include <asm/parisc-device.h>
  30. #ifdef CONFIG_MAGIC_SYSRQ
  31. #include <linux/sysrq.h>
  32. #define SUPPORT_SYSRQ
  33. #endif
  34. #include <linux/serial_core.h>
  35. #define MUX_OFFSET 0x800
  36. #define MUX_LINE_OFFSET 0x80
  37. #define MUX_FIFO_SIZE 255
  38. #define MUX_POLL_DELAY (30 * HZ / 1000)
  39. #define IO_DATA_REG_OFFSET 0x3c
  40. #define IO_DCOUNT_REG_OFFSET 0x40
  41. #define MUX_EOFIFO(status) ((status & 0xF000) == 0xF000)
  42. #define MUX_STATUS(status) ((status & 0xF000) == 0x8000)
  43. #define MUX_BREAK(status) ((status & 0xF000) == 0x2000)
  44. #define MUX_NR 256
  45. static unsigned int port_cnt __read_mostly;
  46. struct mux_port {
  47. struct uart_port port;
  48. int enabled;
  49. };
  50. static struct mux_port mux_ports[MUX_NR];
  51. static struct uart_driver mux_driver = {
  52. .owner = THIS_MODULE,
  53. .driver_name = "ttyB",
  54. .dev_name = "ttyB",
  55. .major = MUX_MAJOR,
  56. .minor = 0,
  57. .nr = MUX_NR,
  58. };
  59. static struct timer_list mux_timer;
  60. #define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + IO_DATA_REG_OFFSET)
  61. #define UART_GET_FIFO_CNT(p) __raw_readl((p)->membase + IO_DCOUNT_REG_OFFSET)
  62. /**
  63. * get_mux_port_count - Get the number of available ports on the Mux.
  64. * @dev: The parisc device.
  65. *
  66. * This function is used to determine the number of ports the Mux
  67. * supports. The IODC data reports the number of ports the Mux
  68. * can support, but there are cases where not all the Mux ports
  69. * are connected. This function can override the IODC and
  70. * return the true port count.
  71. */
  72. static int __init get_mux_port_count(struct parisc_device *dev)
  73. {
  74. int status;
  75. u8 iodc_data[32];
  76. unsigned long bytecnt;
  77. /* If this is the built-in Mux for the K-Class (Eole CAP/MUX),
  78. * we only need to allocate resources for 1 port since the
  79. * other 7 ports are not connected.
  80. */
  81. if(dev->id.hversion == 0x15)
  82. return 1;
  83. status = pdc_iodc_read(&bytecnt, dev->hpa.start, 0, iodc_data, 32);
  84. BUG_ON(status != PDC_OK);
  85. /* Return the number of ports specified in the iodc data. */
  86. return ((((iodc_data)[4] & 0xf0) >> 4) * 8) + 8;
  87. }
  88. /**
  89. * mux_tx_empty - Check if the transmitter fifo is empty.
  90. * @port: Ptr to the uart_port.
  91. *
  92. * This function test if the transmitter fifo for the port
  93. * described by 'port' is empty. If it is empty, this function
  94. * should return TIOCSER_TEMT, otherwise return 0.
  95. */
  96. static unsigned int mux_tx_empty(struct uart_port *port)
  97. {
  98. return UART_GET_FIFO_CNT(port) ? 0 : TIOCSER_TEMT;
  99. }
  100. /**
  101. * mux_set_mctrl - Set the current state of the modem control inputs.
  102. * @ports: Ptr to the uart_port.
  103. * @mctrl: Modem control bits.
  104. *
  105. * The Serial MUX does not support CTS, DCD or DSR so this function
  106. * is ignored.
  107. */
  108. static void mux_set_mctrl(struct uart_port *port, unsigned int mctrl)
  109. {
  110. }
  111. /**
  112. * mux_get_mctrl - Returns the current state of modem control inputs.
  113. * @port: Ptr to the uart_port.
  114. *
  115. * The Serial MUX does not support CTS, DCD or DSR so these lines are
  116. * treated as permanently active.
  117. */
  118. static unsigned int mux_get_mctrl(struct uart_port *port)
  119. {
  120. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  121. }
  122. /**
  123. * mux_stop_tx - Stop transmitting characters.
  124. * @port: Ptr to the uart_port.
  125. *
  126. * The Serial MUX does not support this function.
  127. */
  128. static void mux_stop_tx(struct uart_port *port)
  129. {
  130. }
  131. /**
  132. * mux_start_tx - Start transmitting characters.
  133. * @port: Ptr to the uart_port.
  134. *
  135. * The Serial Mux does not support this function.
  136. */
  137. static void mux_start_tx(struct uart_port *port)
  138. {
  139. }
  140. /**
  141. * mux_stop_rx - Stop receiving characters.
  142. * @port: Ptr to the uart_port.
  143. *
  144. * The Serial Mux does not support this function.
  145. */
  146. static void mux_stop_rx(struct uart_port *port)
  147. {
  148. }
  149. /**
  150. * mux_enable_ms - Enable modum status interrupts.
  151. * @port: Ptr to the uart_port.
  152. *
  153. * The Serial Mux does not support this function.
  154. */
  155. static void mux_enable_ms(struct uart_port *port)
  156. {
  157. }
  158. /**
  159. * mux_break_ctl - Control the transmitssion of a break signal.
  160. * @port: Ptr to the uart_port.
  161. * @break_state: Raise/Lower the break signal.
  162. *
  163. * The Serial Mux does not support this function.
  164. */
  165. static void mux_break_ctl(struct uart_port *port, int break_state)
  166. {
  167. }
  168. /**
  169. * mux_write - Write chars to the mux fifo.
  170. * @port: Ptr to the uart_port.
  171. *
  172. * This function writes all the data from the uart buffer to
  173. * the mux fifo.
  174. */
  175. static void mux_write(struct uart_port *port)
  176. {
  177. int count;
  178. struct circ_buf *xmit = &port->state->xmit;
  179. if(port->x_char) {
  180. UART_PUT_CHAR(port, port->x_char);
  181. port->icount.tx++;
  182. port->x_char = 0;
  183. return;
  184. }
  185. if(uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  186. mux_stop_tx(port);
  187. return;
  188. }
  189. count = (port->fifosize) - UART_GET_FIFO_CNT(port);
  190. do {
  191. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  192. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  193. port->icount.tx++;
  194. if(uart_circ_empty(xmit))
  195. break;
  196. } while(--count > 0);
  197. while(UART_GET_FIFO_CNT(port))
  198. udelay(1);
  199. if(uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  200. uart_write_wakeup(port);
  201. if (uart_circ_empty(xmit))
  202. mux_stop_tx(port);
  203. }
  204. /**
  205. * mux_read - Read chars from the mux fifo.
  206. * @port: Ptr to the uart_port.
  207. *
  208. * This reads all available data from the mux's fifo and pushes
  209. * the data to the tty layer.
  210. */
  211. static void mux_read(struct uart_port *port)
  212. {
  213. int data;
  214. struct tty_struct *tty = port->state->port.tty;
  215. __u32 start_count = port->icount.rx;
  216. while(1) {
  217. data = __raw_readl(port->membase + IO_DATA_REG_OFFSET);
  218. if (MUX_STATUS(data))
  219. continue;
  220. if (MUX_EOFIFO(data))
  221. break;
  222. port->icount.rx++;
  223. if (MUX_BREAK(data)) {
  224. port->icount.brk++;
  225. if(uart_handle_break(port))
  226. continue;
  227. }
  228. if (uart_handle_sysrq_char(port, data & 0xffu))
  229. continue;
  230. tty_insert_flip_char(tty, data & 0xFF, TTY_NORMAL);
  231. }
  232. if (start_count != port->icount.rx) {
  233. tty_flip_buffer_push(tty);
  234. }
  235. }
  236. /**
  237. * mux_startup - Initialize the port.
  238. * @port: Ptr to the uart_port.
  239. *
  240. * Grab any resources needed for this port and start the
  241. * mux timer.
  242. */
  243. static int mux_startup(struct uart_port *port)
  244. {
  245. mux_ports[port->line].enabled = 1;
  246. return 0;
  247. }
  248. /**
  249. * mux_shutdown - Disable the port.
  250. * @port: Ptr to the uart_port.
  251. *
  252. * Release any resources needed for the port.
  253. */
  254. static void mux_shutdown(struct uart_port *port)
  255. {
  256. mux_ports[port->line].enabled = 0;
  257. }
  258. /**
  259. * mux_set_termios - Chane port parameters.
  260. * @port: Ptr to the uart_port.
  261. * @termios: new termios settings.
  262. * @old: old termios settings.
  263. *
  264. * The Serial Mux does not support this function.
  265. */
  266. static void
  267. mux_set_termios(struct uart_port *port, struct ktermios *termios,
  268. struct ktermios *old)
  269. {
  270. }
  271. /**
  272. * mux_type - Describe the port.
  273. * @port: Ptr to the uart_port.
  274. *
  275. * Return a pointer to a string constant describing the
  276. * specified port.
  277. */
  278. static const char *mux_type(struct uart_port *port)
  279. {
  280. return "Mux";
  281. }
  282. /**
  283. * mux_release_port - Release memory and IO regions.
  284. * @port: Ptr to the uart_port.
  285. *
  286. * Release any memory and IO region resources currently in use by
  287. * the port.
  288. */
  289. static void mux_release_port(struct uart_port *port)
  290. {
  291. }
  292. /**
  293. * mux_request_port - Request memory and IO regions.
  294. * @port: Ptr to the uart_port.
  295. *
  296. * Request any memory and IO region resources required by the port.
  297. * If any fail, no resources should be registered when this function
  298. * returns, and it should return -EBUSY on failure.
  299. */
  300. static int mux_request_port(struct uart_port *port)
  301. {
  302. return 0;
  303. }
  304. /**
  305. * mux_config_port - Perform port autoconfiguration.
  306. * @port: Ptr to the uart_port.
  307. * @type: Bitmask of required configurations.
  308. *
  309. * Perform any autoconfiguration steps for the port. This function is
  310. * called if the UPF_BOOT_AUTOCONF flag is specified for the port.
  311. * [Note: This is required for now because of a bug in the Serial core.
  312. * rmk has already submitted a patch to linus, should be available for
  313. * 2.5.47.]
  314. */
  315. static void mux_config_port(struct uart_port *port, int type)
  316. {
  317. port->type = PORT_MUX;
  318. }
  319. /**
  320. * mux_verify_port - Verify the port information.
  321. * @port: Ptr to the uart_port.
  322. * @ser: Ptr to the serial information.
  323. *
  324. * Verify the new serial port information contained within serinfo is
  325. * suitable for this port type.
  326. */
  327. static int mux_verify_port(struct uart_port *port, struct serial_struct *ser)
  328. {
  329. if(port->membase == NULL)
  330. return -EINVAL;
  331. return 0;
  332. }
  333. /**
  334. * mux_drv_poll - Mux poll function.
  335. * @unused: Unused variable
  336. *
  337. * This function periodically polls the Serial MUX to check for new data.
  338. */
  339. static void mux_poll(unsigned long unused)
  340. {
  341. int i;
  342. for(i = 0; i < port_cnt; ++i) {
  343. if(!mux_ports[i].enabled)
  344. continue;
  345. mux_read(&mux_ports[i].port);
  346. mux_write(&mux_ports[i].port);
  347. }
  348. mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
  349. }
  350. #ifdef CONFIG_SERIAL_MUX_CONSOLE
  351. static void mux_console_write(struct console *co, const char *s, unsigned count)
  352. {
  353. /* Wait until the FIFO drains. */
  354. while(UART_GET_FIFO_CNT(&mux_ports[0].port))
  355. udelay(1);
  356. while(count--) {
  357. if(*s == '\n') {
  358. UART_PUT_CHAR(&mux_ports[0].port, '\r');
  359. }
  360. UART_PUT_CHAR(&mux_ports[0].port, *s++);
  361. }
  362. }
  363. static int mux_console_setup(struct console *co, char *options)
  364. {
  365. return 0;
  366. }
  367. struct tty_driver *mux_console_device(struct console *co, int *index)
  368. {
  369. *index = co->index;
  370. return mux_driver.tty_driver;
  371. }
  372. static struct console mux_console = {
  373. .name = "ttyB",
  374. .write = mux_console_write,
  375. .device = mux_console_device,
  376. .setup = mux_console_setup,
  377. .flags = CON_ENABLED | CON_PRINTBUFFER,
  378. .index = 0,
  379. };
  380. #define MUX_CONSOLE &mux_console
  381. #else
  382. #define MUX_CONSOLE NULL
  383. #endif
  384. static struct uart_ops mux_pops = {
  385. .tx_empty = mux_tx_empty,
  386. .set_mctrl = mux_set_mctrl,
  387. .get_mctrl = mux_get_mctrl,
  388. .stop_tx = mux_stop_tx,
  389. .start_tx = mux_start_tx,
  390. .stop_rx = mux_stop_rx,
  391. .enable_ms = mux_enable_ms,
  392. .break_ctl = mux_break_ctl,
  393. .startup = mux_startup,
  394. .shutdown = mux_shutdown,
  395. .set_termios = mux_set_termios,
  396. .type = mux_type,
  397. .release_port = mux_release_port,
  398. .request_port = mux_request_port,
  399. .config_port = mux_config_port,
  400. .verify_port = mux_verify_port,
  401. };
  402. /**
  403. * mux_probe - Determine if the Serial Mux should claim this device.
  404. * @dev: The parisc device.
  405. *
  406. * Deterimine if the Serial Mux should claim this chip (return 0)
  407. * or not (return 1).
  408. */
  409. static int __init mux_probe(struct parisc_device *dev)
  410. {
  411. int i, status;
  412. int port_count = get_mux_port_count(dev);
  413. printk(KERN_INFO "Serial mux driver (%d ports) Revision: 0.6\n", port_count);
  414. dev_set_drvdata(&dev->dev, (void *)(long)port_count);
  415. request_mem_region(dev->hpa.start + MUX_OFFSET,
  416. port_count * MUX_LINE_OFFSET, "Mux");
  417. if(!port_cnt) {
  418. mux_driver.cons = MUX_CONSOLE;
  419. status = uart_register_driver(&mux_driver);
  420. if(status) {
  421. printk(KERN_ERR "Serial mux: Unable to register driver.\n");
  422. return 1;
  423. }
  424. }
  425. for(i = 0; i < port_count; ++i, ++port_cnt) {
  426. struct uart_port *port = &mux_ports[port_cnt].port;
  427. port->iobase = 0;
  428. port->mapbase = dev->hpa.start + MUX_OFFSET +
  429. (i * MUX_LINE_OFFSET);
  430. port->membase = ioremap_nocache(port->mapbase, MUX_LINE_OFFSET);
  431. port->iotype = UPIO_MEM;
  432. port->type = PORT_MUX;
  433. port->irq = 0;
  434. port->uartclk = 0;
  435. port->fifosize = MUX_FIFO_SIZE;
  436. port->ops = &mux_pops;
  437. port->flags = UPF_BOOT_AUTOCONF;
  438. port->line = port_cnt;
  439. /* The port->timeout needs to match what is present in
  440. * uart_wait_until_sent in serial_core.c. Otherwise
  441. * the time spent in msleep_interruptable will be very
  442. * long, causing the appearance of a console hang.
  443. */
  444. port->timeout = HZ / 50;
  445. spin_lock_init(&port->lock);
  446. status = uart_add_one_port(&mux_driver, port);
  447. BUG_ON(status);
  448. }
  449. return 0;
  450. }
  451. static int __devexit mux_remove(struct parisc_device *dev)
  452. {
  453. int i, j;
  454. int port_count = (long)dev_get_drvdata(&dev->dev);
  455. /* Find Port 0 for this card in the mux_ports list. */
  456. for(i = 0; i < port_cnt; ++i) {
  457. if(mux_ports[i].port.mapbase == dev->hpa.start + MUX_OFFSET)
  458. break;
  459. }
  460. BUG_ON(i + port_count > port_cnt);
  461. /* Release the resources associated with each port on the device. */
  462. for(j = 0; j < port_count; ++j, ++i) {
  463. struct uart_port *port = &mux_ports[i].port;
  464. uart_remove_one_port(&mux_driver, port);
  465. if(port->membase)
  466. iounmap(port->membase);
  467. }
  468. release_mem_region(dev->hpa.start + MUX_OFFSET, port_count * MUX_LINE_OFFSET);
  469. return 0;
  470. }
  471. /* Hack. This idea was taken from the 8250_gsc.c on how to properly order
  472. * the serial port detection in the proper order. The idea is we always
  473. * want the builtin mux to be detected before addin mux cards, so we
  474. * specifically probe for the builtin mux cards first.
  475. *
  476. * This table only contains the parisc_device_id of known builtin mux
  477. * devices. All other mux cards will be detected by the generic mux_tbl.
  478. */
  479. static struct parisc_device_id builtin_mux_tbl[] = {
  480. { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x15, 0x0000D }, /* All K-class */
  481. { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, 0x44, 0x0000D }, /* E35, E45, and E55 */
  482. { 0, }
  483. };
  484. static struct parisc_device_id mux_tbl[] = {
  485. { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0000D },
  486. { 0, }
  487. };
  488. MODULE_DEVICE_TABLE(parisc, builtin_mux_tbl);
  489. MODULE_DEVICE_TABLE(parisc, mux_tbl);
  490. static struct parisc_driver builtin_serial_mux_driver = {
  491. .name = "builtin_serial_mux",
  492. .id_table = builtin_mux_tbl,
  493. .probe = mux_probe,
  494. .remove = __devexit_p(mux_remove),
  495. };
  496. static struct parisc_driver serial_mux_driver = {
  497. .name = "serial_mux",
  498. .id_table = mux_tbl,
  499. .probe = mux_probe,
  500. .remove = __devexit_p(mux_remove),
  501. };
  502. /**
  503. * mux_init - Serial MUX initialization procedure.
  504. *
  505. * Register the Serial MUX driver.
  506. */
  507. static int __init mux_init(void)
  508. {
  509. register_parisc_driver(&builtin_serial_mux_driver);
  510. register_parisc_driver(&serial_mux_driver);
  511. if(port_cnt > 0) {
  512. /* Start the Mux timer */
  513. init_timer(&mux_timer);
  514. mux_timer.function = mux_poll;
  515. mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
  516. #ifdef CONFIG_SERIAL_MUX_CONSOLE
  517. register_console(&mux_console);
  518. #endif
  519. }
  520. return 0;
  521. }
  522. /**
  523. * mux_exit - Serial MUX cleanup procedure.
  524. *
  525. * Unregister the Serial MUX driver from the tty layer.
  526. */
  527. static void __exit mux_exit(void)
  528. {
  529. /* Delete the Mux timer. */
  530. if(port_cnt > 0) {
  531. del_timer(&mux_timer);
  532. #ifdef CONFIG_SERIAL_MUX_CONSOLE
  533. unregister_console(&mux_console);
  534. #endif
  535. }
  536. unregister_parisc_driver(&builtin_serial_mux_driver);
  537. unregister_parisc_driver(&serial_mux_driver);
  538. uart_unregister_driver(&mux_driver);
  539. }
  540. module_init(mux_init);
  541. module_exit(mux_exit);
  542. MODULE_AUTHOR("Ryan Bradetich");
  543. MODULE_DESCRIPTION("Serial MUX driver");
  544. MODULE_LICENSE("GPL");
  545. MODULE_ALIAS_CHARDEV_MAJOR(MUX_MAJOR);