mux.c 12 KB

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