mux.c 15 KB

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