mux.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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/slab.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. #define GET_MUX_PORTS(iodc_data) ((((iodc_data)[4] & 0xf0) >> 4) * 8) + 8
  63. /**
  64. * mux_tx_empty - Check if the transmitter fifo is empty.
  65. * @port: Ptr to the uart_port.
  66. *
  67. * This function test if the transmitter fifo for the port
  68. * described by 'port' is empty. If it is empty, this function
  69. * should return TIOCSER_TEMT, otherwise return 0.
  70. */
  71. static unsigned int mux_tx_empty(struct uart_port *port)
  72. {
  73. return UART_GET_FIFO_CNT(port) ? 0 : TIOCSER_TEMT;
  74. }
  75. /**
  76. * mux_set_mctrl - Set the current state of the modem control inputs.
  77. * @ports: Ptr to the uart_port.
  78. * @mctrl: Modem control bits.
  79. *
  80. * The Serial MUX does not support CTS, DCD or DSR so this function
  81. * is ignored.
  82. */
  83. static void mux_set_mctrl(struct uart_port *port, unsigned int mctrl)
  84. {
  85. }
  86. /**
  87. * mux_get_mctrl - Returns the current state of modem control inputs.
  88. * @port: Ptr to the uart_port.
  89. *
  90. * The Serial MUX does not support CTS, DCD or DSR so these lines are
  91. * treated as permanently active.
  92. */
  93. static unsigned int mux_get_mctrl(struct uart_port *port)
  94. {
  95. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  96. }
  97. /**
  98. * mux_stop_tx - Stop transmitting characters.
  99. * @port: Ptr to the uart_port.
  100. *
  101. * The Serial MUX does not support this function.
  102. */
  103. static void mux_stop_tx(struct uart_port *port)
  104. {
  105. }
  106. /**
  107. * mux_start_tx - Start transmitting characters.
  108. * @port: Ptr to the uart_port.
  109. *
  110. * The Serial Mux does not support this function.
  111. */
  112. static void mux_start_tx(struct uart_port *port)
  113. {
  114. }
  115. /**
  116. * mux_stop_rx - Stop receiving characters.
  117. * @port: Ptr to the uart_port.
  118. *
  119. * The Serial Mux does not support this function.
  120. */
  121. static void mux_stop_rx(struct uart_port *port)
  122. {
  123. }
  124. /**
  125. * mux_enable_ms - Enable modum status interrupts.
  126. * @port: Ptr to the uart_port.
  127. *
  128. * The Serial Mux does not support this function.
  129. */
  130. static void mux_enable_ms(struct uart_port *port)
  131. {
  132. }
  133. /**
  134. * mux_break_ctl - Control the transmitssion of a break signal.
  135. * @port: Ptr to the uart_port.
  136. * @break_state: Raise/Lower the break signal.
  137. *
  138. * The Serial Mux does not support this function.
  139. */
  140. static void mux_break_ctl(struct uart_port *port, int break_state)
  141. {
  142. }
  143. /**
  144. * mux_write - Write chars to the mux fifo.
  145. * @port: Ptr to the uart_port.
  146. *
  147. * This function writes all the data from the uart buffer to
  148. * the mux fifo.
  149. */
  150. static void mux_write(struct uart_port *port)
  151. {
  152. int count;
  153. struct circ_buf *xmit = &port->info->xmit;
  154. if(port->x_char) {
  155. UART_PUT_CHAR(port, port->x_char);
  156. port->icount.tx++;
  157. port->x_char = 0;
  158. return;
  159. }
  160. if(uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  161. mux_stop_tx(port);
  162. return;
  163. }
  164. count = (port->fifosize) - UART_GET_FIFO_CNT(port);
  165. do {
  166. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  167. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  168. port->icount.tx++;
  169. if(uart_circ_empty(xmit))
  170. break;
  171. } while(--count > 0);
  172. while(UART_GET_FIFO_CNT(port))
  173. udelay(1);
  174. if(uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  175. uart_write_wakeup(port);
  176. if (uart_circ_empty(xmit))
  177. mux_stop_tx(port);
  178. }
  179. /**
  180. * mux_read - Read chars from the mux fifo.
  181. * @port: Ptr to the uart_port.
  182. *
  183. * This reads all available data from the mux's fifo and pushes
  184. * the data to the tty layer.
  185. */
  186. static void mux_read(struct uart_port *port)
  187. {
  188. int data;
  189. struct tty_struct *tty = port->info->tty;
  190. __u32 start_count = port->icount.rx;
  191. while(1) {
  192. data = __raw_readl(port->membase + IO_DATA_REG_OFFSET);
  193. if (MUX_STATUS(data))
  194. continue;
  195. if (MUX_EOFIFO(data))
  196. break;
  197. port->icount.rx++;
  198. if (MUX_BREAK(data)) {
  199. port->icount.brk++;
  200. if(uart_handle_break(port))
  201. continue;
  202. }
  203. if (uart_handle_sysrq_char(port, data & 0xffu))
  204. continue;
  205. tty_insert_flip_char(tty, data & 0xFF, TTY_NORMAL);
  206. }
  207. if (start_count != port->icount.rx) {
  208. tty_flip_buffer_push(tty);
  209. }
  210. }
  211. /**
  212. * mux_startup - Initialize the port.
  213. * @port: Ptr to the uart_port.
  214. *
  215. * Grab any resources needed for this port and start the
  216. * mux timer.
  217. */
  218. static int mux_startup(struct uart_port *port)
  219. {
  220. mux_ports[port->line].enabled = 1;
  221. return 0;
  222. }
  223. /**
  224. * mux_shutdown - Disable the port.
  225. * @port: Ptr to the uart_port.
  226. *
  227. * Release any resources needed for the port.
  228. */
  229. static void mux_shutdown(struct uart_port *port)
  230. {
  231. mux_ports[port->line].enabled = 0;
  232. }
  233. /**
  234. * mux_set_termios - Chane port parameters.
  235. * @port: Ptr to the uart_port.
  236. * @termios: new termios settings.
  237. * @old: old termios settings.
  238. *
  239. * The Serial Mux does not support this function.
  240. */
  241. static void
  242. mux_set_termios(struct uart_port *port, struct termios *termios,
  243. struct termios *old)
  244. {
  245. }
  246. /**
  247. * mux_type - Describe the port.
  248. * @port: Ptr to the uart_port.
  249. *
  250. * Return a pointer to a string constant describing the
  251. * specified port.
  252. */
  253. static const char *mux_type(struct uart_port *port)
  254. {
  255. return "Mux";
  256. }
  257. /**
  258. * mux_release_port - Release memory and IO regions.
  259. * @port: Ptr to the uart_port.
  260. *
  261. * Release any memory and IO region resources currently in use by
  262. * the port.
  263. */
  264. static void mux_release_port(struct uart_port *port)
  265. {
  266. }
  267. /**
  268. * mux_request_port - Request memory and IO regions.
  269. * @port: Ptr to the uart_port.
  270. *
  271. * Request any memory and IO region resources required by the port.
  272. * If any fail, no resources should be registered when this function
  273. * returns, and it should return -EBUSY on failure.
  274. */
  275. static int mux_request_port(struct uart_port *port)
  276. {
  277. return 0;
  278. }
  279. /**
  280. * mux_config_port - Perform port autoconfiguration.
  281. * @port: Ptr to the uart_port.
  282. * @type: Bitmask of required configurations.
  283. *
  284. * Perform any autoconfiguration steps for the port. This function is
  285. * called if the UPF_BOOT_AUTOCONF flag is specified for the port.
  286. * [Note: This is required for now because of a bug in the Serial core.
  287. * rmk has already submitted a patch to linus, should be available for
  288. * 2.5.47.]
  289. */
  290. static void mux_config_port(struct uart_port *port, int type)
  291. {
  292. port->type = PORT_MUX;
  293. }
  294. /**
  295. * mux_verify_port - Verify the port information.
  296. * @port: Ptr to the uart_port.
  297. * @ser: Ptr to the serial information.
  298. *
  299. * Verify the new serial port information contained within serinfo is
  300. * suitable for this port type.
  301. */
  302. static int mux_verify_port(struct uart_port *port, struct serial_struct *ser)
  303. {
  304. if(port->membase == NULL)
  305. return -EINVAL;
  306. return 0;
  307. }
  308. /**
  309. * mux_drv_poll - Mux poll function.
  310. * @unused: Unused variable
  311. *
  312. * This function periodically polls the Serial MUX to check for new data.
  313. */
  314. static void mux_poll(unsigned long unused)
  315. {
  316. int i;
  317. for(i = 0; i < port_cnt; ++i) {
  318. if(!mux_ports[i].enabled)
  319. continue;
  320. mux_read(&mux_ports[i].port);
  321. mux_write(&mux_ports[i].port);
  322. }
  323. mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
  324. }
  325. #ifdef CONFIG_SERIAL_MUX_CONSOLE
  326. static void mux_console_write(struct console *co, const char *s, unsigned count)
  327. {
  328. /* Wait until the FIFO drains. */
  329. while(UART_GET_FIFO_CNT(&mux_ports[0].port))
  330. udelay(1);
  331. while(count--) {
  332. if(*s == '\n') {
  333. UART_PUT_CHAR(&mux_ports[0].port, '\r');
  334. }
  335. UART_PUT_CHAR(&mux_ports[0].port, *s++);
  336. }
  337. }
  338. static int mux_console_setup(struct console *co, char *options)
  339. {
  340. return 0;
  341. }
  342. struct tty_driver *mux_console_device(struct console *co, int *index)
  343. {
  344. *index = co->index;
  345. return mux_driver.tty_driver;
  346. }
  347. static struct console mux_console = {
  348. .name = "ttyB",
  349. .write = mux_console_write,
  350. .device = mux_console_device,
  351. .setup = mux_console_setup,
  352. .flags = CON_ENABLED | CON_PRINTBUFFER,
  353. .index = 0,
  354. };
  355. #define MUX_CONSOLE &mux_console
  356. #else
  357. #define MUX_CONSOLE NULL
  358. #endif
  359. static struct uart_ops mux_pops = {
  360. .tx_empty = mux_tx_empty,
  361. .set_mctrl = mux_set_mctrl,
  362. .get_mctrl = mux_get_mctrl,
  363. .stop_tx = mux_stop_tx,
  364. .start_tx = mux_start_tx,
  365. .stop_rx = mux_stop_rx,
  366. .enable_ms = mux_enable_ms,
  367. .break_ctl = mux_break_ctl,
  368. .startup = mux_startup,
  369. .shutdown = mux_shutdown,
  370. .set_termios = mux_set_termios,
  371. .type = mux_type,
  372. .release_port = mux_release_port,
  373. .request_port = mux_request_port,
  374. .config_port = mux_config_port,
  375. .verify_port = mux_verify_port,
  376. };
  377. /**
  378. * mux_probe - Determine if the Serial Mux should claim this device.
  379. * @dev: The parisc device.
  380. *
  381. * Deterimine if the Serial Mux should claim this chip (return 0)
  382. * or not (return 1).
  383. */
  384. static int __init mux_probe(struct parisc_device *dev)
  385. {
  386. int i, status, ports;
  387. u8 iodc_data[32];
  388. unsigned long bytecnt;
  389. struct uart_port *port;
  390. status = pdc_iodc_read(&bytecnt, dev->hpa.start, 0, iodc_data, 32);
  391. if(status != PDC_OK) {
  392. printk(KERN_ERR "Serial mux: Unable to read IODC.\n");
  393. return 1;
  394. }
  395. ports = GET_MUX_PORTS(iodc_data);
  396. printk(KERN_INFO "Serial mux driver (%d ports) Revision: 0.3\n", ports);
  397. if(!port_cnt) {
  398. mux_driver.cons = MUX_CONSOLE;
  399. status = uart_register_driver(&mux_driver);
  400. if(status) {
  401. printk(KERN_ERR "Serial mux: Unable to register driver.\n");
  402. return 1;
  403. }
  404. init_timer(&mux_timer);
  405. mux_timer.function = mux_poll;
  406. }
  407. for(i = 0; i < ports; ++i, ++port_cnt) {
  408. port = &mux_ports[port_cnt].port;
  409. port->iobase = 0;
  410. port->mapbase = dev->hpa.start + MUX_OFFSET +
  411. (i * MUX_LINE_OFFSET);
  412. port->membase = ioremap_nocache(port->mapbase, MUX_LINE_OFFSET);
  413. port->iotype = UPIO_MEM;
  414. port->type = PORT_MUX;
  415. port->irq = NO_IRQ;
  416. port->uartclk = 0;
  417. port->fifosize = MUX_FIFO_SIZE;
  418. port->ops = &mux_pops;
  419. port->flags = UPF_BOOT_AUTOCONF;
  420. port->line = port_cnt;
  421. /* The port->timeout needs to match what is present in
  422. * uart_wait_until_sent in serial_core.c. Otherwise
  423. * the time spent in msleep_interruptable will be very
  424. * long, causing the appearance of a console hang.
  425. */
  426. port->timeout = HZ / 50;
  427. spin_lock_init(&port->lock);
  428. status = uart_add_one_port(&mux_driver, port);
  429. BUG_ON(status);
  430. }
  431. #ifdef CONFIG_SERIAL_MUX_CONSOLE
  432. register_console(&mux_console);
  433. #endif
  434. mod_timer(&mux_timer, jiffies + MUX_POLL_DELAY);
  435. return 0;
  436. }
  437. static struct parisc_device_id mux_tbl[] = {
  438. { HPHW_A_DIRECT, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0000D },
  439. { 0, }
  440. };
  441. MODULE_DEVICE_TABLE(parisc, mux_tbl);
  442. static struct parisc_driver serial_mux_driver = {
  443. .name = "serial_mux",
  444. .id_table = mux_tbl,
  445. .probe = mux_probe,
  446. };
  447. /**
  448. * mux_init - Serial MUX initalization procedure.
  449. *
  450. * Register the Serial MUX driver.
  451. */
  452. static int __init mux_init(void)
  453. {
  454. return register_parisc_driver(&serial_mux_driver);
  455. }
  456. /**
  457. * mux_exit - Serial MUX cleanup procedure.
  458. *
  459. * Unregister the Serial MUX driver from the tty layer.
  460. */
  461. static void __exit mux_exit(void)
  462. {
  463. int i;
  464. for (i = 0; i < port_cnt; i++) {
  465. uart_remove_one_port(&mux_driver, &mux_ports[i].port);
  466. if (mux_ports[i].port.membase)
  467. iounmap(mux_ports[i].port.membase);
  468. }
  469. uart_unregister_driver(&mux_driver);
  470. }
  471. module_init(mux_init);
  472. module_exit(mux_exit);
  473. MODULE_AUTHOR("Ryan Bradetich");
  474. MODULE_DESCRIPTION("Serial MUX driver");
  475. MODULE_LICENSE("GPL");
  476. MODULE_ALIAS_CHARDEV_MAJOR(MUX_MAJOR);