4 * driver for the GE IP-OCTAL boards
6 * Copyright (C) 2009-2012 CERN (www.cern.ch)
7 * Author: Nicolas Serafini, EIC2 SA
8 * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; version 2 of the License.
15 #include <linux/device.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/sched.h>
19 #include <linux/tty.h>
20 #include <linux/serial.h>
21 #include <linux/tty_flip.h>
22 #include <linux/slab.h>
24 #include <linux/ipack.h>
28 #define IP_OCTAL_ID_SPACE_VECTOR 0x41
29 #define IP_OCTAL_NB_BLOCKS 4
31 static const struct tty_operations ipoctal_fops;
33 struct ipoctal_channel {
34 struct ipoctal_stats stats;
35 unsigned int nb_bytes;
36 wait_queue_head_t queue;
38 unsigned int pointer_read;
39 unsigned int pointer_write;
40 struct tty_port tty_port;
41 union scc2698_channel __iomem *regs;
42 union scc2698_block __iomem *block_regs;
43 unsigned int board_id;
46 unsigned int rx_enable;
50 struct ipack_device *dev;
51 unsigned int board_id;
52 struct ipoctal_channel channel[NR_CHANNELS];
53 struct tty_driver *tty_drv;
54 u8 __iomem *mem8_space;
55 u8 __iomem *int_space;
58 static inline struct ipoctal *chan_to_ipoctal(struct ipoctal_channel *chan,
61 return container_of(chan, struct ipoctal, channel[index]);
64 static void ipoctal_reset_channel(struct ipoctal_channel *channel)
66 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
67 channel->rx_enable = 0;
68 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
69 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
70 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
71 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
74 static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
76 struct ipoctal_channel *channel;
78 channel = dev_get_drvdata(tty->dev);
81 * Enable RX. TX will be enabled when
82 * there is something to send
84 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
85 channel->rx_enable = 1;
89 static int ipoctal_open(struct tty_struct *tty, struct file *file)
91 struct ipoctal_channel *channel = dev_get_drvdata(tty->dev);
92 struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
95 tty->driver_data = channel;
97 if (!ipack_get_carrier(ipoctal->dev))
100 err = tty_port_open(&channel->tty_port, tty, file);
102 ipack_put_carrier(ipoctal->dev);
107 static void ipoctal_reset_stats(struct ipoctal_stats *stats)
111 stats->rcv_break = 0;
112 stats->framing_err = 0;
113 stats->overrun_err = 0;
114 stats->parity_err = 0;
117 static void ipoctal_free_channel(struct ipoctal_channel *channel)
119 ipoctal_reset_stats(&channel->stats);
120 channel->pointer_read = 0;
121 channel->pointer_write = 0;
122 channel->nb_bytes = 0;
125 static void ipoctal_close(struct tty_struct *tty, struct file *filp)
127 struct ipoctal_channel *channel = tty->driver_data;
129 tty_port_close(&channel->tty_port, tty, filp);
130 ipoctal_free_channel(channel);
133 static int ipoctal_get_icount(struct tty_struct *tty,
134 struct serial_icounter_struct *icount)
136 struct ipoctal_channel *channel = tty->driver_data;
142 icount->rx = channel->stats.rx;
143 icount->tx = channel->stats.tx;
144 icount->frame = channel->stats.framing_err;
145 icount->parity = channel->stats.parity_err;
146 icount->brk = channel->stats.rcv_break;
150 static void ipoctal_irq_rx(struct ipoctal_channel *channel, u8 sr)
152 struct tty_port *port = &channel->tty_port;
158 value = ioread8(&channel->regs->r.rhr);
160 /* Error: count statistics */
162 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
164 if (sr & SR_OVERRUN_ERROR) {
165 channel->stats.overrun_err++;
166 /* Overrun doesn't affect the current character*/
167 tty_insert_flip_char(port, 0, TTY_OVERRUN);
169 if (sr & SR_PARITY_ERROR) {
170 channel->stats.parity_err++;
173 if (sr & SR_FRAMING_ERROR) {
174 channel->stats.framing_err++;
177 if (sr & SR_RECEIVED_BREAK) {
178 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
179 channel->stats.rcv_break++;
183 tty_insert_flip_char(port, value, flag);
185 /* Check if there are more characters in RX FIFO
186 * If there are more, the isr register for this channel
187 * has enabled the RxRDY|FFULL bit.
189 isr = ioread8(&channel->block_regs->r.isr);
190 sr = ioread8(&channel->regs->r.sr);
191 } while (isr & channel->isr_rx_rdy_mask);
193 tty_flip_buffer_push(port);
196 static void ipoctal_irq_tx(struct ipoctal_channel *channel)
199 unsigned int *pointer_write = &channel->pointer_write;
201 if (channel->nb_bytes == 0)
204 spin_lock(&channel->lock);
205 value = channel->tty_port.xmit_buf[*pointer_write];
206 iowrite8(value, &channel->regs->w.thr);
209 *pointer_write = *pointer_write % PAGE_SIZE;
211 spin_unlock(&channel->lock);
214 static void ipoctal_irq_channel(struct ipoctal_channel *channel)
218 /* The HW is organized in pair of channels. See which register we need
220 isr = ioread8(&channel->block_regs->r.isr);
221 sr = ioread8(&channel->regs->r.sr);
223 if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
224 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
225 /* In case of RS-485, change from TX to RX when finishing TX.
227 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
228 iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
229 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
230 channel->rx_enable = 1;
235 if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
236 ipoctal_irq_rx(channel, sr);
238 /* TX of each character */
239 if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
240 ipoctal_irq_tx(channel);
243 static irqreturn_t ipoctal_irq_handler(void *arg)
246 struct ipoctal *ipoctal = (struct ipoctal *) arg;
248 /* Clear the IPack device interrupt */
249 readw(ipoctal->int_space + ACK_INT_REQ0);
250 readw(ipoctal->int_space + ACK_INT_REQ1);
252 /* Check all channels */
253 for (i = 0; i < NR_CHANNELS; i++)
254 ipoctal_irq_channel(&ipoctal->channel[i]);
259 static const struct tty_port_operations ipoctal_tty_port_ops = {
261 .activate = ipoctal_port_activate,
264 static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
269 struct tty_driver *tty;
271 struct ipoctal_channel *channel;
272 struct ipack_region *region;
274 union scc2698_channel __iomem *chan_regs;
275 union scc2698_block __iomem *block_regs;
277 ipoctal->board_id = ipoctal->dev->id_device;
279 region = &ipoctal->dev->region[IPACK_IO_SPACE];
280 addr = devm_ioremap_nocache(&ipoctal->dev->dev,
281 region->start, region->size);
283 dev_err(&ipoctal->dev->dev,
284 "Unable to map slot [%d:%d] IO space!\n",
286 return -EADDRNOTAVAIL;
288 /* Save the virtual address to access the registers easily */
290 (union scc2698_channel __iomem *) addr;
292 (union scc2698_block __iomem *) addr;
294 region = &ipoctal->dev->region[IPACK_INT_SPACE];
296 devm_ioremap_nocache(&ipoctal->dev->dev,
297 region->start, region->size);
298 if (!ipoctal->int_space) {
299 dev_err(&ipoctal->dev->dev,
300 "Unable to map slot [%d:%d] INT space!\n",
302 return -EADDRNOTAVAIL;
305 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
306 ipoctal->mem8_space =
307 devm_ioremap_nocache(&ipoctal->dev->dev,
308 region->start, 0x8000);
309 if (!ipoctal->mem8_space) {
310 dev_err(&ipoctal->dev->dev,
311 "Unable to map slot [%d:%d] MEM8 space!\n",
313 return -EADDRNOTAVAIL;
317 /* Disable RX and TX before touching anything */
318 for (i = 0; i < NR_CHANNELS ; i++) {
319 struct ipoctal_channel *channel = &ipoctal->channel[i];
320 channel->regs = chan_regs + i;
321 channel->block_regs = block_regs + (i >> 1);
322 channel->board_id = ipoctal->board_id;
324 channel->isr_tx_rdy_mask = ISR_TxRDY_B;
325 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
327 channel->isr_tx_rdy_mask = ISR_TxRDY_A;
328 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
331 ipoctal_reset_channel(channel);
332 iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
333 &channel->regs->w.mr); /* mr1 */
334 iowrite8(0, &channel->regs->w.mr); /* mr2 */
335 iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr);
338 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
339 iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
340 iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
341 &block_regs[i].w.opcr);
342 iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
343 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
344 &block_regs[i].w.imr);
348 iowrite8(1, ipoctal->mem8_space + 1);
350 /* Register the TTY device */
352 /* Each IP-OCTAL channel is a TTY port */
353 tty = alloc_tty_driver(NR_CHANNELS);
358 /* Fill struct tty_driver with ipoctal data */
359 tty->owner = THIS_MODULE;
360 tty->driver_name = KBUILD_MODNAME;
361 sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
365 tty->minor_start = 0;
366 tty->type = TTY_DRIVER_TYPE_SERIAL;
367 tty->subtype = SERIAL_TYPE_NORMAL;
368 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
369 tty->init_termios = tty_std_termios;
370 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
371 tty->init_termios.c_ispeed = 9600;
372 tty->init_termios.c_ospeed = 9600;
374 tty_set_operations(tty, &ipoctal_fops);
375 res = tty_register_driver(tty);
377 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
382 /* Save struct tty_driver for use it when uninstalling the device */
383 ipoctal->tty_drv = tty;
385 for (i = 0; i < NR_CHANNELS; i++) {
386 struct device *tty_dev;
388 channel = &ipoctal->channel[i];
389 tty_port_init(&channel->tty_port);
390 tty_port_alloc_xmit_buf(&channel->tty_port);
391 channel->tty_port.ops = &ipoctal_tty_port_ops;
393 ipoctal_reset_stats(&channel->stats);
394 channel->nb_bytes = 0;
395 spin_lock_init(&channel->lock);
396 channel->pointer_read = 0;
397 channel->pointer_write = 0;
398 tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
399 if (IS_ERR(tty_dev)) {
400 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
401 tty_port_destroy(&channel->tty_port);
404 dev_set_drvdata(tty_dev, channel);
408 * IP-OCTAL has different addresses to copy its IRQ vector.
409 * Depending of the carrier these addresses are accesible or not.
410 * More info in the datasheet.
412 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
413 ipoctal_irq_handler, ipoctal);
418 static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
419 const unsigned char *buf,
424 unsigned int *pointer_read = &channel->pointer_read;
426 /* Copy the bytes from the user buffer to the internal one */
427 for (i = 0; i < count; i++) {
428 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
429 spin_lock_irqsave(&channel->lock, flags);
430 channel->tty_port.xmit_buf[*pointer_read] = buf[i];
431 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
433 spin_unlock_irqrestore(&channel->lock, flags);
441 static int ipoctal_write_tty(struct tty_struct *tty,
442 const unsigned char *buf, int count)
444 struct ipoctal_channel *channel = tty->driver_data;
445 unsigned int char_copied;
447 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
449 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
450 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
451 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
452 channel->rx_enable = 0;
453 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
457 * Send a packet and then disable TX to avoid failure after several send
460 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
464 static int ipoctal_write_room(struct tty_struct *tty)
466 struct ipoctal_channel *channel = tty->driver_data;
468 return PAGE_SIZE - channel->nb_bytes;
471 static int ipoctal_chars_in_buffer(struct tty_struct *tty)
473 struct ipoctal_channel *channel = tty->driver_data;
475 return channel->nb_bytes;
478 static void ipoctal_set_termios(struct tty_struct *tty,
479 struct ktermios *old_termios)
482 unsigned char mr1 = 0;
483 unsigned char mr2 = 0;
484 unsigned char csr = 0;
485 struct ipoctal_channel *channel = tty->driver_data;
488 cflag = tty->termios.c_cflag;
490 /* Disable and reset everything before change the setup */
491 ipoctal_reset_channel(channel);
493 /* Set Bits per chars */
494 switch (cflag & CSIZE) {
496 mr1 |= MR1_CHRL_6_BITS;
499 mr1 |= MR1_CHRL_7_BITS;
503 mr1 |= MR1_CHRL_8_BITS;
504 /* By default, select CS8 */
505 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
512 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
514 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
516 mr1 |= MR1_PARITY_OFF;
518 /* Mark or space parity is not supported */
519 tty->termios.c_cflag &= ~CMSPAR;
523 mr2 |= MR2_STOP_BITS_LENGTH_2;
525 mr2 |= MR2_STOP_BITS_LENGTH_1;
527 /* Set the flow control */
528 switch (channel->board_id) {
529 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
530 if (cflag & CRTSCTS) {
531 mr1 |= MR1_RxRTS_CONTROL_ON;
532 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
534 mr1 |= MR1_RxRTS_CONTROL_OFF;
535 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
538 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
539 mr1 |= MR1_RxRTS_CONTROL_OFF;
540 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
542 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
543 mr1 |= MR1_RxRTS_CONTROL_OFF;
544 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
551 baud = tty_get_baud_rate(tty);
552 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
557 csr |= TX_CLK_75 | RX_CLK_75;
560 csr |= TX_CLK_110 | RX_CLK_110;
563 csr |= TX_CLK_150 | RX_CLK_150;
566 csr |= TX_CLK_300 | RX_CLK_300;
569 csr |= TX_CLK_600 | RX_CLK_600;
572 csr |= TX_CLK_1200 | RX_CLK_1200;
575 csr |= TX_CLK_1800 | RX_CLK_1800;
578 csr |= TX_CLK_2000 | RX_CLK_2000;
581 csr |= TX_CLK_2400 | RX_CLK_2400;
584 csr |= TX_CLK_4800 | RX_CLK_4800;
587 csr |= TX_CLK_9600 | RX_CLK_9600;
590 csr |= TX_CLK_19200 | RX_CLK_19200;
594 csr |= TX_CLK_38400 | RX_CLK_38400;
595 /* In case of default, we establish 38400 bps */
596 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
600 mr1 |= MR1_ERROR_CHAR;
601 mr1 |= MR1_RxINT_RxRDY;
603 /* Write the control registers */
604 iowrite8(mr1, &channel->regs->w.mr);
605 iowrite8(mr2, &channel->regs->w.mr);
606 iowrite8(csr, &channel->regs->w.csr);
608 /* Enable again the RX, if it was before */
609 if (channel->rx_enable)
610 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
613 static void ipoctal_hangup(struct tty_struct *tty)
616 struct ipoctal_channel *channel = tty->driver_data;
621 spin_lock_irqsave(&channel->lock, flags);
622 channel->nb_bytes = 0;
623 channel->pointer_read = 0;
624 channel->pointer_write = 0;
625 spin_unlock_irqrestore(&channel->lock, flags);
627 tty_port_hangup(&channel->tty_port);
629 ipoctal_reset_channel(channel);
631 clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
632 wake_up_interruptible(&channel->tty_port.open_wait);
635 static void ipoctal_shutdown(struct tty_struct *tty)
637 struct ipoctal_channel *channel = tty->driver_data;
642 ipoctal_reset_channel(channel);
643 clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
646 static void ipoctal_cleanup(struct tty_struct *tty)
648 struct ipoctal_channel *channel = tty->driver_data;
649 struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
651 /* release the carrier driver */
652 ipack_put_carrier(ipoctal->dev);
655 static const struct tty_operations ipoctal_fops = {
657 .open = ipoctal_open,
658 .close = ipoctal_close,
659 .write = ipoctal_write_tty,
660 .set_termios = ipoctal_set_termios,
661 .write_room = ipoctal_write_room,
662 .chars_in_buffer = ipoctal_chars_in_buffer,
663 .get_icount = ipoctal_get_icount,
664 .hangup = ipoctal_hangup,
665 .shutdown = ipoctal_shutdown,
666 .cleanup = ipoctal_cleanup,
669 static int ipoctal_probe(struct ipack_device *dev)
672 struct ipoctal *ipoctal;
674 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
679 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
683 dev_set_drvdata(&dev->dev, ipoctal);
691 static void __ipoctal_remove(struct ipoctal *ipoctal)
695 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
697 for (i = 0; i < NR_CHANNELS; i++) {
698 struct ipoctal_channel *channel = &ipoctal->channel[i];
699 tty_unregister_device(ipoctal->tty_drv, i);
700 tty_port_free_xmit_buf(&channel->tty_port);
701 tty_port_destroy(&channel->tty_port);
704 tty_unregister_driver(ipoctal->tty_drv);
705 put_tty_driver(ipoctal->tty_drv);
709 static void ipoctal_remove(struct ipack_device *idev)
711 __ipoctal_remove(dev_get_drvdata(&idev->dev));
714 static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
715 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
716 IPACK1_DEVICE_ID_SBS_OCTAL_232) },
717 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
718 IPACK1_DEVICE_ID_SBS_OCTAL_422) },
719 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
720 IPACK1_DEVICE_ID_SBS_OCTAL_485) },
724 MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
726 static const struct ipack_driver_ops ipoctal_drv_ops = {
727 .probe = ipoctal_probe,
728 .remove = ipoctal_remove,
731 static struct ipack_driver driver = {
732 .ops = &ipoctal_drv_ops,
733 .id_table = ipoctal_ids,
736 static int __init ipoctal_init(void)
738 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
741 static void __exit ipoctal_exit(void)
743 ipack_driver_unregister(&driver);
746 MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
747 MODULE_LICENSE("GPL");
749 module_init(ipoctal_init);
750 module_exit(ipoctal_exit);