mux.c 14 KB

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