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