1 // SPDX-License-Identifier: GPL-2.0-only
3 * driver for the GE IP-OCTAL boards
5 * Copyright (C) 2009-2012 CERN (www.cern.ch)
6 * Author: Nicolas Serafini, EIC2 SA
7 * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
10 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/sched.h>
14 #include <linux/tty.h>
15 #include <linux/serial.h>
16 #include <linux/tty_flip.h>
17 #include <linux/slab.h>
19 #include <linux/ipack.h>
23 #define IP_OCTAL_ID_SPACE_VECTOR 0x41
24 #define IP_OCTAL_NB_BLOCKS 4
26 static const struct tty_operations ipoctal_fops;
28 struct ipoctal_channel {
29 struct ipoctal_stats stats;
30 unsigned int nb_bytes;
31 wait_queue_head_t queue;
33 unsigned int pointer_read;
34 unsigned int pointer_write;
35 struct tty_port tty_port;
37 union scc2698_channel __iomem *regs;
38 union scc2698_block __iomem *block_regs;
39 unsigned int board_id;
42 unsigned int rx_enable;
46 struct ipack_device *dev;
47 unsigned int board_id;
48 struct ipoctal_channel channel[NR_CHANNELS];
49 struct tty_driver *tty_drv;
50 u8 __iomem *mem8_space;
51 u8 __iomem *int_space;
54 static inline struct ipoctal *chan_to_ipoctal(struct ipoctal_channel *chan,
57 return container_of(chan, struct ipoctal, channel[index]);
60 static void ipoctal_reset_channel(struct ipoctal_channel *channel)
62 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
63 channel->rx_enable = 0;
64 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
65 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
66 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
67 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
70 static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
72 struct ipoctal_channel *channel;
74 channel = dev_get_drvdata(tty->dev);
77 * Enable RX. TX will be enabled when
78 * there is something to send
80 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
81 channel->rx_enable = 1;
85 static int ipoctal_install(struct tty_driver *driver, struct tty_struct *tty)
87 struct ipoctal_channel *channel = dev_get_drvdata(tty->dev);
88 struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
91 if (!ipack_get_carrier(ipoctal->dev))
94 res = tty_standard_install(driver, tty);
98 tty->driver_data = channel;
103 ipack_put_carrier(ipoctal->dev);
108 static int ipoctal_open(struct tty_struct *tty, struct file *file)
110 struct ipoctal_channel *channel = tty->driver_data;
112 return tty_port_open(&channel->tty_port, tty, file);
115 static void ipoctal_reset_stats(struct ipoctal_stats *stats)
119 stats->rcv_break = 0;
120 stats->framing_err = 0;
121 stats->overrun_err = 0;
122 stats->parity_err = 0;
125 static void ipoctal_free_channel(struct ipoctal_channel *channel)
127 ipoctal_reset_stats(&channel->stats);
128 channel->pointer_read = 0;
129 channel->pointer_write = 0;
130 channel->nb_bytes = 0;
133 static void ipoctal_close(struct tty_struct *tty, struct file *filp)
135 struct ipoctal_channel *channel = tty->driver_data;
137 tty_port_close(&channel->tty_port, tty, filp);
138 ipoctal_free_channel(channel);
141 static int ipoctal_get_icount(struct tty_struct *tty,
142 struct serial_icounter_struct *icount)
144 struct ipoctal_channel *channel = tty->driver_data;
150 icount->rx = channel->stats.rx;
151 icount->tx = channel->stats.tx;
152 icount->frame = channel->stats.framing_err;
153 icount->parity = channel->stats.parity_err;
154 icount->brk = channel->stats.rcv_break;
158 static void ipoctal_irq_rx(struct ipoctal_channel *channel, u8 sr)
160 struct tty_port *port = &channel->tty_port;
164 value = ioread8(&channel->regs->r.rhr);
166 /* Error: count statistics */
168 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
170 if (sr & SR_OVERRUN_ERROR) {
171 channel->stats.overrun_err++;
172 /* Overrun doesn't affect the current character*/
173 tty_insert_flip_char(port, 0, TTY_OVERRUN);
175 if (sr & SR_PARITY_ERROR) {
176 channel->stats.parity_err++;
179 if (sr & SR_FRAMING_ERROR) {
180 channel->stats.framing_err++;
183 if (sr & SR_RECEIVED_BREAK) {
184 channel->stats.rcv_break++;
188 tty_insert_flip_char(port, value, flag);
190 /* Check if there are more characters in RX FIFO
191 * If there are more, the isr register for this channel
192 * has enabled the RxRDY|FFULL bit.
194 isr = ioread8(&channel->block_regs->r.isr);
195 sr = ioread8(&channel->regs->r.sr);
196 } while (isr & channel->isr_rx_rdy_mask);
198 tty_flip_buffer_push(port);
201 static void ipoctal_irq_tx(struct ipoctal_channel *channel)
203 unsigned int *pointer_write = &channel->pointer_write;
206 if (channel->nb_bytes == 0)
209 spin_lock(&channel->lock);
210 value = channel->tty_port.xmit_buf[*pointer_write];
211 iowrite8(value, &channel->regs->w.thr);
214 *pointer_write = *pointer_write % PAGE_SIZE;
216 spin_unlock(&channel->lock);
219 static void ipoctal_irq_channel(struct ipoctal_channel *channel)
223 /* The HW is organized in pair of channels. See which register we need
225 isr = ioread8(&channel->block_regs->r.isr);
226 sr = ioread8(&channel->regs->r.sr);
228 if (isr & (IMR_DELTA_BREAK_A | IMR_DELTA_BREAK_B))
229 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
231 if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
232 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
233 /* In case of RS-485, change from TX to RX when finishing TX.
235 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
236 iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
237 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
238 channel->rx_enable = 1;
243 if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
244 ipoctal_irq_rx(channel, sr);
246 /* TX of each character */
247 if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
248 ipoctal_irq_tx(channel);
251 static irqreturn_t ipoctal_irq_handler(void *arg)
254 struct ipoctal *ipoctal = arg;
256 /* Clear the IPack device interrupt */
257 readw(ipoctal->int_space + ACK_INT_REQ0);
258 readw(ipoctal->int_space + ACK_INT_REQ1);
260 /* Check all channels */
261 for (i = 0; i < NR_CHANNELS; i++)
262 ipoctal_irq_channel(&ipoctal->channel[i]);
267 static const struct tty_port_operations ipoctal_tty_port_ops = {
269 .activate = ipoctal_port_activate,
272 static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
277 struct tty_driver *drv;
278 struct ipoctal_channel *channel;
279 struct ipack_region *region;
281 union scc2698_channel __iomem *chan_regs;
282 union scc2698_block __iomem *block_regs;
284 ipoctal->board_id = ipoctal->dev->id_device;
286 region = &ipoctal->dev->region[IPACK_IO_SPACE];
287 addr = devm_ioremap(&ipoctal->dev->dev,
288 region->start, region->size);
290 dev_err(&ipoctal->dev->dev,
291 "Unable to map slot [%d:%d] IO space!\n",
293 return -EADDRNOTAVAIL;
295 /* Save the virtual address to access the registers easily */
297 (union scc2698_channel __iomem *) addr;
299 (union scc2698_block __iomem *) addr;
301 region = &ipoctal->dev->region[IPACK_INT_SPACE];
303 devm_ioremap(&ipoctal->dev->dev,
304 region->start, region->size);
305 if (!ipoctal->int_space) {
306 dev_err(&ipoctal->dev->dev,
307 "Unable to map slot [%d:%d] INT space!\n",
309 return -EADDRNOTAVAIL;
312 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
313 ipoctal->mem8_space =
314 devm_ioremap(&ipoctal->dev->dev,
315 region->start, 0x8000);
316 if (!ipoctal->mem8_space) {
317 dev_err(&ipoctal->dev->dev,
318 "Unable to map slot [%d:%d] MEM8 space!\n",
320 return -EADDRNOTAVAIL;
324 /* Disable RX and TX before touching anything */
325 for (i = 0; i < NR_CHANNELS ; i++) {
326 struct ipoctal_channel *channel = &ipoctal->channel[i];
327 channel->regs = chan_regs + i;
328 channel->block_regs = block_regs + (i >> 1);
329 channel->board_id = ipoctal->board_id;
331 channel->isr_tx_rdy_mask = ISR_TxRDY_B;
332 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
334 channel->isr_tx_rdy_mask = ISR_TxRDY_A;
335 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
338 ipoctal_reset_channel(channel);
339 iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
340 &channel->regs->w.mr); /* mr1 */
341 iowrite8(0, &channel->regs->w.mr); /* mr2 */
342 iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr);
345 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
346 iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
347 iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
348 &block_regs[i].w.opcr);
349 iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
350 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
351 &block_regs[i].w.imr);
355 iowrite8(1, ipoctal->mem8_space + 1);
357 /* Register the TTY device */
359 /* Each IP-OCTAL channel is a TTY port */
360 drv = tty_alloc_driver(NR_CHANNELS, TTY_DRIVER_REAL_RAW |
361 TTY_DRIVER_DYNAMIC_DEV);
365 /* Fill struct tty_driver with ipoctal data */
366 drv->owner = THIS_MODULE;
367 drv->driver_name = KBUILD_MODNAME;
368 drv->name = kasprintf(GFP_KERNEL, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
375 drv->minor_start = 0;
376 drv->type = TTY_DRIVER_TYPE_SERIAL;
377 drv->subtype = SERIAL_TYPE_NORMAL;
378 drv->init_termios = tty_std_termios;
379 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
380 drv->init_termios.c_ispeed = 9600;
381 drv->init_termios.c_ospeed = 9600;
383 tty_set_operations(drv, &ipoctal_fops);
384 res = tty_register_driver(drv);
386 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
390 /* Save struct tty_driver for use it when uninstalling the device */
391 ipoctal->tty_drv = drv;
393 for (i = 0; i < NR_CHANNELS; i++) {
394 struct device *tty_dev;
396 channel = &ipoctal->channel[i];
397 tty_port_init(&channel->tty_port);
398 res = tty_port_alloc_xmit_buf(&channel->tty_port);
401 channel->tty_port.ops = &ipoctal_tty_port_ops;
403 ipoctal_reset_stats(&channel->stats);
404 channel->nb_bytes = 0;
405 spin_lock_init(&channel->lock);
406 channel->pointer_read = 0;
407 channel->pointer_write = 0;
408 tty_dev = tty_port_register_device_attr(&channel->tty_port, drv,
409 i, NULL, channel, NULL);
410 if (IS_ERR(tty_dev)) {
411 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
412 tty_port_free_xmit_buf(&channel->tty_port);
413 tty_port_destroy(&channel->tty_port);
416 channel->tty_registered = true;
420 * IP-OCTAL has different addresses to copy its IRQ vector.
421 * Depending of the carrier these addresses are accesible or not.
422 * More info in the datasheet.
424 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
425 ipoctal_irq_handler, ipoctal);
432 tty_driver_kref_put(drv);
437 static inline size_t ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
438 const u8 *buf, size_t count)
442 unsigned int *pointer_read = &channel->pointer_read;
444 /* Copy the bytes from the user buffer to the internal one */
445 for (i = 0; i < count; i++) {
446 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
447 spin_lock_irqsave(&channel->lock, flags);
448 channel->tty_port.xmit_buf[*pointer_read] = buf[i];
449 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
451 spin_unlock_irqrestore(&channel->lock, flags);
459 static ssize_t ipoctal_write_tty(struct tty_struct *tty, const u8 *buf,
462 struct ipoctal_channel *channel = tty->driver_data;
465 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
467 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
468 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
469 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
470 channel->rx_enable = 0;
471 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
475 * Send a packet and then disable TX to avoid failure after several send
478 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
482 static unsigned int ipoctal_write_room(struct tty_struct *tty)
484 struct ipoctal_channel *channel = tty->driver_data;
486 return PAGE_SIZE - channel->nb_bytes;
489 static unsigned int ipoctal_chars_in_buffer(struct tty_struct *tty)
491 struct ipoctal_channel *channel = tty->driver_data;
493 return channel->nb_bytes;
496 static void ipoctal_set_termios(struct tty_struct *tty,
497 const struct ktermios *old_termios)
500 unsigned char mr1 = 0;
501 unsigned char mr2 = 0;
502 unsigned char csr = 0;
503 struct ipoctal_channel *channel = tty->driver_data;
506 cflag = tty->termios.c_cflag;
508 /* Disable and reset everything before change the setup */
509 ipoctal_reset_channel(channel);
511 /* Set Bits per chars */
512 switch (cflag & CSIZE) {
514 mr1 |= MR1_CHRL_6_BITS;
517 mr1 |= MR1_CHRL_7_BITS;
521 mr1 |= MR1_CHRL_8_BITS;
522 /* By default, select CS8 */
523 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
530 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
532 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
534 mr1 |= MR1_PARITY_OFF;
536 /* Mark or space parity is not supported */
537 tty->termios.c_cflag &= ~CMSPAR;
541 mr2 |= MR2_STOP_BITS_LENGTH_2;
543 mr2 |= MR2_STOP_BITS_LENGTH_1;
545 /* Set the flow control */
546 switch (channel->board_id) {
547 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
548 if (cflag & CRTSCTS) {
549 mr1 |= MR1_RxRTS_CONTROL_ON;
550 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
552 mr1 |= MR1_RxRTS_CONTROL_OFF;
553 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
556 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
557 mr1 |= MR1_RxRTS_CONTROL_OFF;
558 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
560 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
561 mr1 |= MR1_RxRTS_CONTROL_OFF;
562 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
568 baud = tty_get_baud_rate(tty);
569 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
574 csr |= TX_CLK_75 | RX_CLK_75;
577 csr |= TX_CLK_110 | RX_CLK_110;
580 csr |= TX_CLK_150 | RX_CLK_150;
583 csr |= TX_CLK_300 | RX_CLK_300;
586 csr |= TX_CLK_600 | RX_CLK_600;
589 csr |= TX_CLK_1200 | RX_CLK_1200;
592 csr |= TX_CLK_1800 | RX_CLK_1800;
595 csr |= TX_CLK_2000 | RX_CLK_2000;
598 csr |= TX_CLK_2400 | RX_CLK_2400;
601 csr |= TX_CLK_4800 | RX_CLK_4800;
604 csr |= TX_CLK_9600 | RX_CLK_9600;
607 csr |= TX_CLK_19200 | RX_CLK_19200;
611 csr |= TX_CLK_38400 | RX_CLK_38400;
612 /* In case of default, we establish 38400 bps */
613 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
617 mr1 |= MR1_ERROR_CHAR;
618 mr1 |= MR1_RxINT_RxRDY;
620 /* Write the control registers */
621 iowrite8(mr1, &channel->regs->w.mr);
622 iowrite8(mr2, &channel->regs->w.mr);
623 iowrite8(csr, &channel->regs->w.csr);
625 /* Enable again the RX, if it was before */
626 if (channel->rx_enable)
627 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
630 static void ipoctal_hangup(struct tty_struct *tty)
633 struct ipoctal_channel *channel = tty->driver_data;
638 spin_lock_irqsave(&channel->lock, flags);
639 channel->nb_bytes = 0;
640 channel->pointer_read = 0;
641 channel->pointer_write = 0;
642 spin_unlock_irqrestore(&channel->lock, flags);
644 tty_port_hangup(&channel->tty_port);
646 ipoctal_reset_channel(channel);
647 tty_port_set_initialized(&channel->tty_port, false);
648 wake_up_interruptible(&channel->tty_port.open_wait);
651 static void ipoctal_shutdown(struct tty_struct *tty)
653 struct ipoctal_channel *channel = tty->driver_data;
658 ipoctal_reset_channel(channel);
659 tty_port_set_initialized(&channel->tty_port, false);
662 static void ipoctal_cleanup(struct tty_struct *tty)
664 struct ipoctal_channel *channel = tty->driver_data;
665 struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
667 /* release the carrier driver */
668 ipack_put_carrier(ipoctal->dev);
671 static const struct tty_operations ipoctal_fops = {
673 .install = ipoctal_install,
674 .open = ipoctal_open,
675 .close = ipoctal_close,
676 .write = ipoctal_write_tty,
677 .set_termios = ipoctal_set_termios,
678 .write_room = ipoctal_write_room,
679 .chars_in_buffer = ipoctal_chars_in_buffer,
680 .get_icount = ipoctal_get_icount,
681 .hangup = ipoctal_hangup,
682 .shutdown = ipoctal_shutdown,
683 .cleanup = ipoctal_cleanup,
686 static int ipoctal_probe(struct ipack_device *dev)
689 struct ipoctal *ipoctal;
691 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
696 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
700 dev_set_drvdata(&dev->dev, ipoctal);
708 static void __ipoctal_remove(struct ipoctal *ipoctal)
712 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
714 for (i = 0; i < NR_CHANNELS; i++) {
715 struct ipoctal_channel *channel = &ipoctal->channel[i];
717 if (!channel->tty_registered)
720 tty_unregister_device(ipoctal->tty_drv, i);
721 tty_port_free_xmit_buf(&channel->tty_port);
722 tty_port_destroy(&channel->tty_port);
725 tty_unregister_driver(ipoctal->tty_drv);
726 kfree(ipoctal->tty_drv->name);
727 tty_driver_kref_put(ipoctal->tty_drv);
731 static void ipoctal_remove(struct ipack_device *idev)
733 __ipoctal_remove(dev_get_drvdata(&idev->dev));
736 static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
737 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
738 IPACK1_DEVICE_ID_SBS_OCTAL_232) },
739 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
740 IPACK1_DEVICE_ID_SBS_OCTAL_422) },
741 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
742 IPACK1_DEVICE_ID_SBS_OCTAL_485) },
746 MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
748 static const struct ipack_driver_ops ipoctal_drv_ops = {
749 .probe = ipoctal_probe,
750 .remove = ipoctal_remove,
753 static struct ipack_driver driver = {
754 .ops = &ipoctal_drv_ops,
755 .id_table = ipoctal_ids,
758 static int __init ipoctal_init(void)
760 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
763 static void __exit ipoctal_exit(void)
765 ipack_driver_unregister(&driver);
768 MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
769 MODULE_LICENSE("GPL");
771 module_init(ipoctal_init);
772 module_exit(ipoctal_exit);