1 // SPDX-License-Identifier: GPL-2.0-only
5 * driver for the GE IP-OCTAL boards
7 * Copyright (C) 2009-2012 CERN (www.cern.ch)
8 * Author: Nicolas Serafini, EIC2 SA
9 * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/sched.h>
16 #include <linux/tty.h>
17 #include <linux/serial.h>
18 #include <linux/tty_flip.h>
19 #include <linux/slab.h>
21 #include <linux/ipack.h>
25 #define IP_OCTAL_ID_SPACE_VECTOR 0x41
26 #define IP_OCTAL_NB_BLOCKS 4
28 static const struct tty_operations ipoctal_fops;
30 struct ipoctal_channel {
31 struct ipoctal_stats stats;
32 unsigned int nb_bytes;
33 wait_queue_head_t queue;
35 unsigned int pointer_read;
36 unsigned int pointer_write;
37 struct tty_port tty_port;
39 union scc2698_channel __iomem *regs;
40 union scc2698_block __iomem *block_regs;
41 unsigned int board_id;
44 unsigned int rx_enable;
48 struct ipack_device *dev;
49 unsigned int board_id;
50 struct ipoctal_channel channel[NR_CHANNELS];
51 struct tty_driver *tty_drv;
52 u8 __iomem *mem8_space;
53 u8 __iomem *int_space;
56 static inline struct ipoctal *chan_to_ipoctal(struct ipoctal_channel *chan,
59 return container_of(chan, struct ipoctal, channel[index]);
62 static void ipoctal_reset_channel(struct ipoctal_channel *channel)
64 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
65 channel->rx_enable = 0;
66 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
67 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
68 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
69 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
72 static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
74 struct ipoctal_channel *channel;
76 channel = dev_get_drvdata(tty->dev);
79 * Enable RX. TX will be enabled when
80 * there is something to send
82 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
83 channel->rx_enable = 1;
87 static int ipoctal_install(struct tty_driver *driver, struct tty_struct *tty)
89 struct ipoctal_channel *channel = dev_get_drvdata(tty->dev);
90 struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
93 if (!ipack_get_carrier(ipoctal->dev))
96 res = tty_standard_install(driver, tty);
100 tty->driver_data = channel;
105 ipack_put_carrier(ipoctal->dev);
110 static int ipoctal_open(struct tty_struct *tty, struct file *file)
112 struct ipoctal_channel *channel = tty->driver_data;
114 return tty_port_open(&channel->tty_port, tty, file);
117 static void ipoctal_reset_stats(struct ipoctal_stats *stats)
121 stats->rcv_break = 0;
122 stats->framing_err = 0;
123 stats->overrun_err = 0;
124 stats->parity_err = 0;
127 static void ipoctal_free_channel(struct ipoctal_channel *channel)
129 ipoctal_reset_stats(&channel->stats);
130 channel->pointer_read = 0;
131 channel->pointer_write = 0;
132 channel->nb_bytes = 0;
135 static void ipoctal_close(struct tty_struct *tty, struct file *filp)
137 struct ipoctal_channel *channel = tty->driver_data;
139 tty_port_close(&channel->tty_port, tty, filp);
140 ipoctal_free_channel(channel);
143 static int ipoctal_get_icount(struct tty_struct *tty,
144 struct serial_icounter_struct *icount)
146 struct ipoctal_channel *channel = tty->driver_data;
152 icount->rx = channel->stats.rx;
153 icount->tx = channel->stats.tx;
154 icount->frame = channel->stats.framing_err;
155 icount->parity = channel->stats.parity_err;
156 icount->brk = channel->stats.rcv_break;
160 static void ipoctal_irq_rx(struct ipoctal_channel *channel, u8 sr)
162 struct tty_port *port = &channel->tty_port;
168 value = ioread8(&channel->regs->r.rhr);
170 /* Error: count statistics */
172 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
174 if (sr & SR_OVERRUN_ERROR) {
175 channel->stats.overrun_err++;
176 /* Overrun doesn't affect the current character*/
177 tty_insert_flip_char(port, 0, TTY_OVERRUN);
179 if (sr & SR_PARITY_ERROR) {
180 channel->stats.parity_err++;
183 if (sr & SR_FRAMING_ERROR) {
184 channel->stats.framing_err++;
187 if (sr & SR_RECEIVED_BREAK) {
188 channel->stats.rcv_break++;
192 tty_insert_flip_char(port, value, flag);
194 /* Check if there are more characters in RX FIFO
195 * If there are more, the isr register for this channel
196 * has enabled the RxRDY|FFULL bit.
198 isr = ioread8(&channel->block_regs->r.isr);
199 sr = ioread8(&channel->regs->r.sr);
200 } while (isr & channel->isr_rx_rdy_mask);
202 tty_flip_buffer_push(port);
205 static void ipoctal_irq_tx(struct ipoctal_channel *channel)
208 unsigned int *pointer_write = &channel->pointer_write;
210 if (channel->nb_bytes == 0)
213 spin_lock(&channel->lock);
214 value = channel->tty_port.xmit_buf[*pointer_write];
215 iowrite8(value, &channel->regs->w.thr);
218 *pointer_write = *pointer_write % PAGE_SIZE;
220 spin_unlock(&channel->lock);
223 static void ipoctal_irq_channel(struct ipoctal_channel *channel)
227 /* The HW is organized in pair of channels. See which register we need
229 isr = ioread8(&channel->block_regs->r.isr);
230 sr = ioread8(&channel->regs->r.sr);
232 if (isr & (IMR_DELTA_BREAK_A | IMR_DELTA_BREAK_B))
233 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
235 if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
236 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
237 /* In case of RS-485, change from TX to RX when finishing TX.
239 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
240 iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
241 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
242 channel->rx_enable = 1;
247 if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
248 ipoctal_irq_rx(channel, sr);
250 /* TX of each character */
251 if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
252 ipoctal_irq_tx(channel);
255 static irqreturn_t ipoctal_irq_handler(void *arg)
258 struct ipoctal *ipoctal = (struct ipoctal *) arg;
260 /* Clear the IPack device interrupt */
261 readw(ipoctal->int_space + ACK_INT_REQ0);
262 readw(ipoctal->int_space + ACK_INT_REQ1);
264 /* Check all channels */
265 for (i = 0; i < NR_CHANNELS; i++)
266 ipoctal_irq_channel(&ipoctal->channel[i]);
271 static const struct tty_port_operations ipoctal_tty_port_ops = {
273 .activate = ipoctal_port_activate,
276 static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
281 struct tty_driver *tty;
282 struct ipoctal_channel *channel;
283 struct ipack_region *region;
285 union scc2698_channel __iomem *chan_regs;
286 union scc2698_block __iomem *block_regs;
288 ipoctal->board_id = ipoctal->dev->id_device;
290 region = &ipoctal->dev->region[IPACK_IO_SPACE];
291 addr = devm_ioremap(&ipoctal->dev->dev,
292 region->start, region->size);
294 dev_err(&ipoctal->dev->dev,
295 "Unable to map slot [%d:%d] IO space!\n",
297 return -EADDRNOTAVAIL;
299 /* Save the virtual address to access the registers easily */
301 (union scc2698_channel __iomem *) addr;
303 (union scc2698_block __iomem *) addr;
305 region = &ipoctal->dev->region[IPACK_INT_SPACE];
307 devm_ioremap(&ipoctal->dev->dev,
308 region->start, region->size);
309 if (!ipoctal->int_space) {
310 dev_err(&ipoctal->dev->dev,
311 "Unable to map slot [%d:%d] INT space!\n",
313 return -EADDRNOTAVAIL;
316 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
317 ipoctal->mem8_space =
318 devm_ioremap(&ipoctal->dev->dev,
319 region->start, 0x8000);
320 if (!ipoctal->mem8_space) {
321 dev_err(&ipoctal->dev->dev,
322 "Unable to map slot [%d:%d] MEM8 space!\n",
324 return -EADDRNOTAVAIL;
328 /* Disable RX and TX before touching anything */
329 for (i = 0; i < NR_CHANNELS ; i++) {
330 struct ipoctal_channel *channel = &ipoctal->channel[i];
331 channel->regs = chan_regs + i;
332 channel->block_regs = block_regs + (i >> 1);
333 channel->board_id = ipoctal->board_id;
335 channel->isr_tx_rdy_mask = ISR_TxRDY_B;
336 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
338 channel->isr_tx_rdy_mask = ISR_TxRDY_A;
339 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
342 ipoctal_reset_channel(channel);
343 iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
344 &channel->regs->w.mr); /* mr1 */
345 iowrite8(0, &channel->regs->w.mr); /* mr2 */
346 iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr);
349 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
350 iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
351 iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
352 &block_regs[i].w.opcr);
353 iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
354 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
355 &block_regs[i].w.imr);
359 iowrite8(1, ipoctal->mem8_space + 1);
361 /* Register the TTY device */
363 /* Each IP-OCTAL channel is a TTY port */
364 tty = alloc_tty_driver(NR_CHANNELS);
369 /* Fill struct tty_driver with ipoctal data */
370 tty->owner = THIS_MODULE;
371 tty->driver_name = KBUILD_MODNAME;
372 tty->name = kasprintf(GFP_KERNEL, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
379 tty->minor_start = 0;
380 tty->type = TTY_DRIVER_TYPE_SERIAL;
381 tty->subtype = SERIAL_TYPE_NORMAL;
382 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
383 tty->init_termios = tty_std_termios;
384 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
385 tty->init_termios.c_ispeed = 9600;
386 tty->init_termios.c_ospeed = 9600;
388 tty_set_operations(tty, &ipoctal_fops);
389 res = tty_register_driver(tty);
391 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
395 /* Save struct tty_driver for use it when uninstalling the device */
396 ipoctal->tty_drv = tty;
398 for (i = 0; i < NR_CHANNELS; i++) {
399 struct device *tty_dev;
401 channel = &ipoctal->channel[i];
402 tty_port_init(&channel->tty_port);
403 res = tty_port_alloc_xmit_buf(&channel->tty_port);
406 channel->tty_port.ops = &ipoctal_tty_port_ops;
408 ipoctal_reset_stats(&channel->stats);
409 channel->nb_bytes = 0;
410 spin_lock_init(&channel->lock);
411 channel->pointer_read = 0;
412 channel->pointer_write = 0;
413 tty_dev = tty_port_register_device_attr(&channel->tty_port, tty,
414 i, NULL, channel, NULL);
415 if (IS_ERR(tty_dev)) {
416 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
417 tty_port_free_xmit_buf(&channel->tty_port);
418 tty_port_destroy(&channel->tty_port);
421 channel->tty_registered = true;
425 * IP-OCTAL has different addresses to copy its IRQ vector.
426 * Depending of the carrier these addresses are accesible or not.
427 * More info in the datasheet.
429 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
430 ipoctal_irq_handler, ipoctal);
442 static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
443 const unsigned char *buf,
448 unsigned int *pointer_read = &channel->pointer_read;
450 /* Copy the bytes from the user buffer to the internal one */
451 for (i = 0; i < count; i++) {
452 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
453 spin_lock_irqsave(&channel->lock, flags);
454 channel->tty_port.xmit_buf[*pointer_read] = buf[i];
455 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
457 spin_unlock_irqrestore(&channel->lock, flags);
465 static int ipoctal_write_tty(struct tty_struct *tty,
466 const unsigned char *buf, int count)
468 struct ipoctal_channel *channel = tty->driver_data;
469 unsigned int char_copied;
471 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
473 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
474 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
475 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
476 channel->rx_enable = 0;
477 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
481 * Send a packet and then disable TX to avoid failure after several send
484 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
488 static int ipoctal_write_room(struct tty_struct *tty)
490 struct ipoctal_channel *channel = tty->driver_data;
492 return PAGE_SIZE - channel->nb_bytes;
495 static int ipoctal_chars_in_buffer(struct tty_struct *tty)
497 struct ipoctal_channel *channel = tty->driver_data;
499 return channel->nb_bytes;
502 static void ipoctal_set_termios(struct tty_struct *tty,
503 struct ktermios *old_termios)
506 unsigned char mr1 = 0;
507 unsigned char mr2 = 0;
508 unsigned char csr = 0;
509 struct ipoctal_channel *channel = tty->driver_data;
512 cflag = tty->termios.c_cflag;
514 /* Disable and reset everything before change the setup */
515 ipoctal_reset_channel(channel);
517 /* Set Bits per chars */
518 switch (cflag & CSIZE) {
520 mr1 |= MR1_CHRL_6_BITS;
523 mr1 |= MR1_CHRL_7_BITS;
527 mr1 |= MR1_CHRL_8_BITS;
528 /* By default, select CS8 */
529 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
536 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
538 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
540 mr1 |= MR1_PARITY_OFF;
542 /* Mark or space parity is not supported */
543 tty->termios.c_cflag &= ~CMSPAR;
547 mr2 |= MR2_STOP_BITS_LENGTH_2;
549 mr2 |= MR2_STOP_BITS_LENGTH_1;
551 /* Set the flow control */
552 switch (channel->board_id) {
553 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
554 if (cflag & CRTSCTS) {
555 mr1 |= MR1_RxRTS_CONTROL_ON;
556 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
558 mr1 |= MR1_RxRTS_CONTROL_OFF;
559 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
562 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
563 mr1 |= MR1_RxRTS_CONTROL_OFF;
564 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
566 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
567 mr1 |= MR1_RxRTS_CONTROL_OFF;
568 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
575 baud = tty_get_baud_rate(tty);
576 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
581 csr |= TX_CLK_75 | RX_CLK_75;
584 csr |= TX_CLK_110 | RX_CLK_110;
587 csr |= TX_CLK_150 | RX_CLK_150;
590 csr |= TX_CLK_300 | RX_CLK_300;
593 csr |= TX_CLK_600 | RX_CLK_600;
596 csr |= TX_CLK_1200 | RX_CLK_1200;
599 csr |= TX_CLK_1800 | RX_CLK_1800;
602 csr |= TX_CLK_2000 | RX_CLK_2000;
605 csr |= TX_CLK_2400 | RX_CLK_2400;
608 csr |= TX_CLK_4800 | RX_CLK_4800;
611 csr |= TX_CLK_9600 | RX_CLK_9600;
614 csr |= TX_CLK_19200 | RX_CLK_19200;
618 csr |= TX_CLK_38400 | RX_CLK_38400;
619 /* In case of default, we establish 38400 bps */
620 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
624 mr1 |= MR1_ERROR_CHAR;
625 mr1 |= MR1_RxINT_RxRDY;
627 /* Write the control registers */
628 iowrite8(mr1, &channel->regs->w.mr);
629 iowrite8(mr2, &channel->regs->w.mr);
630 iowrite8(csr, &channel->regs->w.csr);
632 /* Enable again the RX, if it was before */
633 if (channel->rx_enable)
634 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
637 static void ipoctal_hangup(struct tty_struct *tty)
640 struct ipoctal_channel *channel = tty->driver_data;
645 spin_lock_irqsave(&channel->lock, flags);
646 channel->nb_bytes = 0;
647 channel->pointer_read = 0;
648 channel->pointer_write = 0;
649 spin_unlock_irqrestore(&channel->lock, flags);
651 tty_port_hangup(&channel->tty_port);
653 ipoctal_reset_channel(channel);
654 tty_port_set_initialized(&channel->tty_port, 0);
655 wake_up_interruptible(&channel->tty_port.open_wait);
658 static void ipoctal_shutdown(struct tty_struct *tty)
660 struct ipoctal_channel *channel = tty->driver_data;
665 ipoctal_reset_channel(channel);
666 tty_port_set_initialized(&channel->tty_port, 0);
669 static void ipoctal_cleanup(struct tty_struct *tty)
671 struct ipoctal_channel *channel = tty->driver_data;
672 struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
674 /* release the carrier driver */
675 ipack_put_carrier(ipoctal->dev);
678 static const struct tty_operations ipoctal_fops = {
680 .install = ipoctal_install,
681 .open = ipoctal_open,
682 .close = ipoctal_close,
683 .write = ipoctal_write_tty,
684 .set_termios = ipoctal_set_termios,
685 .write_room = ipoctal_write_room,
686 .chars_in_buffer = ipoctal_chars_in_buffer,
687 .get_icount = ipoctal_get_icount,
688 .hangup = ipoctal_hangup,
689 .shutdown = ipoctal_shutdown,
690 .cleanup = ipoctal_cleanup,
693 static int ipoctal_probe(struct ipack_device *dev)
696 struct ipoctal *ipoctal;
698 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
703 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
707 dev_set_drvdata(&dev->dev, ipoctal);
715 static void __ipoctal_remove(struct ipoctal *ipoctal)
719 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
721 for (i = 0; i < NR_CHANNELS; i++) {
722 struct ipoctal_channel *channel = &ipoctal->channel[i];
724 if (!channel->tty_registered)
727 tty_unregister_device(ipoctal->tty_drv, i);
728 tty_port_free_xmit_buf(&channel->tty_port);
729 tty_port_destroy(&channel->tty_port);
732 tty_unregister_driver(ipoctal->tty_drv);
733 kfree(ipoctal->tty_drv->name);
734 put_tty_driver(ipoctal->tty_drv);
738 static void ipoctal_remove(struct ipack_device *idev)
740 __ipoctal_remove(dev_get_drvdata(&idev->dev));
743 static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
744 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
745 IPACK1_DEVICE_ID_SBS_OCTAL_232) },
746 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
747 IPACK1_DEVICE_ID_SBS_OCTAL_422) },
748 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
749 IPACK1_DEVICE_ID_SBS_OCTAL_485) },
753 MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
755 static const struct ipack_driver_ops ipoctal_drv_ops = {
756 .probe = ipoctal_probe,
757 .remove = ipoctal_remove,
760 static struct ipack_driver driver = {
761 .ops = &ipoctal_drv_ops,
762 .id_table = ipoctal_ids,
765 static int __init ipoctal_init(void)
767 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
770 static void __exit ipoctal_exit(void)
772 ipack_driver_unregister(&driver);
775 MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
776 MODULE_LICENSE("GPL");
778 module_init(ipoctal_init);
779 module_exit(ipoctal_exit);