mux.c 15 KB

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