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;
36 union scc2698_channel __iomem *regs;
37 union scc2698_block __iomem *block_regs;
38 unsigned int board_id;
41 unsigned int rx_enable;
45 struct ipack_device *dev;
46 unsigned int board_id;
47 struct ipoctal_channel channel[NR_CHANNELS];
48 struct tty_driver *tty_drv;
49 u8 __iomem *mem8_space;
50 u8 __iomem *int_space;
53 static inline struct ipoctal *chan_to_ipoctal(struct ipoctal_channel *chan,
56 return container_of(chan, struct ipoctal, channel[index]);
59 static void ipoctal_reset_channel(struct ipoctal_channel *channel)
61 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
62 channel->rx_enable = 0;
63 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
64 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
65 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
66 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
69 static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
71 struct ipoctal_channel *channel;
73 channel = dev_get_drvdata(tty->dev);
76 * Enable RX. TX will be enabled when
77 * there is something to send
79 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
80 channel->rx_enable = 1;
84 static int ipoctal_open(struct tty_struct *tty, struct file *file)
86 struct ipoctal_channel *channel = dev_get_drvdata(tty->dev);
87 struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
90 tty->driver_data = channel;
92 if (!ipack_get_carrier(ipoctal->dev))
95 err = tty_port_open(&channel->tty_port, tty, file);
97 ipack_put_carrier(ipoctal->dev);
102 static void ipoctal_reset_stats(struct ipoctal_stats *stats)
106 stats->rcv_break = 0;
107 stats->framing_err = 0;
108 stats->overrun_err = 0;
109 stats->parity_err = 0;
112 static void ipoctal_free_channel(struct ipoctal_channel *channel)
114 ipoctal_reset_stats(&channel->stats);
115 channel->pointer_read = 0;
116 channel->pointer_write = 0;
117 channel->nb_bytes = 0;
120 static void ipoctal_close(struct tty_struct *tty, struct file *filp)
122 struct ipoctal_channel *channel = tty->driver_data;
124 tty_port_close(&channel->tty_port, tty, filp);
125 ipoctal_free_channel(channel);
128 static int ipoctal_get_icount(struct tty_struct *tty,
129 struct serial_icounter_struct *icount)
131 struct ipoctal_channel *channel = tty->driver_data;
137 icount->rx = channel->stats.rx;
138 icount->tx = channel->stats.tx;
139 icount->frame = channel->stats.framing_err;
140 icount->parity = channel->stats.parity_err;
141 icount->brk = channel->stats.rcv_break;
145 static void ipoctal_irq_rx(struct ipoctal_channel *channel, u8 sr)
147 struct tty_port *port = &channel->tty_port;
153 value = ioread8(&channel->regs->r.rhr);
155 /* Error: count statistics */
157 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
159 if (sr & SR_OVERRUN_ERROR) {
160 channel->stats.overrun_err++;
161 /* Overrun doesn't affect the current character*/
162 tty_insert_flip_char(port, 0, TTY_OVERRUN);
164 if (sr & SR_PARITY_ERROR) {
165 channel->stats.parity_err++;
168 if (sr & SR_FRAMING_ERROR) {
169 channel->stats.framing_err++;
172 if (sr & SR_RECEIVED_BREAK) {
173 channel->stats.rcv_break++;
177 tty_insert_flip_char(port, value, flag);
179 /* Check if there are more characters in RX FIFO
180 * If there are more, the isr register for this channel
181 * has enabled the RxRDY|FFULL bit.
183 isr = ioread8(&channel->block_regs->r.isr);
184 sr = ioread8(&channel->regs->r.sr);
185 } while (isr & channel->isr_rx_rdy_mask);
187 tty_flip_buffer_push(port);
190 static void ipoctal_irq_tx(struct ipoctal_channel *channel)
193 unsigned int *pointer_write = &channel->pointer_write;
195 if (channel->nb_bytes == 0)
198 spin_lock(&channel->lock);
199 value = channel->tty_port.xmit_buf[*pointer_write];
200 iowrite8(value, &channel->regs->w.thr);
203 *pointer_write = *pointer_write % PAGE_SIZE;
205 spin_unlock(&channel->lock);
208 static void ipoctal_irq_channel(struct ipoctal_channel *channel)
212 /* The HW is organized in pair of channels. See which register we need
214 isr = ioread8(&channel->block_regs->r.isr);
215 sr = ioread8(&channel->regs->r.sr);
217 if (isr & (IMR_DELTA_BREAK_A | IMR_DELTA_BREAK_B))
218 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
220 if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
221 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
222 /* In case of RS-485, change from TX to RX when finishing TX.
224 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
225 iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
226 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
227 channel->rx_enable = 1;
232 if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
233 ipoctal_irq_rx(channel, sr);
235 /* TX of each character */
236 if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
237 ipoctal_irq_tx(channel);
240 static irqreturn_t ipoctal_irq_handler(void *arg)
243 struct ipoctal *ipoctal = (struct ipoctal *) arg;
245 /* Clear the IPack device interrupt */
246 readw(ipoctal->int_space + ACK_INT_REQ0);
247 readw(ipoctal->int_space + ACK_INT_REQ1);
249 /* Check all channels */
250 for (i = 0; i < NR_CHANNELS; i++)
251 ipoctal_irq_channel(&ipoctal->channel[i]);
256 static const struct tty_port_operations ipoctal_tty_port_ops = {
258 .activate = ipoctal_port_activate,
261 static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
266 struct tty_driver *tty;
268 struct ipoctal_channel *channel;
269 struct ipack_region *region;
271 union scc2698_channel __iomem *chan_regs;
272 union scc2698_block __iomem *block_regs;
274 ipoctal->board_id = ipoctal->dev->id_device;
276 region = &ipoctal->dev->region[IPACK_IO_SPACE];
277 addr = devm_ioremap(&ipoctal->dev->dev,
278 region->start, region->size);
280 dev_err(&ipoctal->dev->dev,
281 "Unable to map slot [%d:%d] IO space!\n",
283 return -EADDRNOTAVAIL;
285 /* Save the virtual address to access the registers easily */
287 (union scc2698_channel __iomem *) addr;
289 (union scc2698_block __iomem *) addr;
291 region = &ipoctal->dev->region[IPACK_INT_SPACE];
293 devm_ioremap(&ipoctal->dev->dev,
294 region->start, region->size);
295 if (!ipoctal->int_space) {
296 dev_err(&ipoctal->dev->dev,
297 "Unable to map slot [%d:%d] INT space!\n",
299 return -EADDRNOTAVAIL;
302 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
303 ipoctal->mem8_space =
304 devm_ioremap(&ipoctal->dev->dev,
305 region->start, 0x8000);
306 if (!ipoctal->mem8_space) {
307 dev_err(&ipoctal->dev->dev,
308 "Unable to map slot [%d:%d] MEM8 space!\n",
310 return -EADDRNOTAVAIL;
314 /* Disable RX and TX before touching anything */
315 for (i = 0; i < NR_CHANNELS ; i++) {
316 struct ipoctal_channel *channel = &ipoctal->channel[i];
317 channel->regs = chan_regs + i;
318 channel->block_regs = block_regs + (i >> 1);
319 channel->board_id = ipoctal->board_id;
321 channel->isr_tx_rdy_mask = ISR_TxRDY_B;
322 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
324 channel->isr_tx_rdy_mask = ISR_TxRDY_A;
325 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
328 ipoctal_reset_channel(channel);
329 iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
330 &channel->regs->w.mr); /* mr1 */
331 iowrite8(0, &channel->regs->w.mr); /* mr2 */
332 iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr);
335 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
336 iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
337 iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
338 &block_regs[i].w.opcr);
339 iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
340 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
341 &block_regs[i].w.imr);
345 iowrite8(1, ipoctal->mem8_space + 1);
347 /* Register the TTY device */
349 /* Each IP-OCTAL channel is a TTY port */
350 tty = alloc_tty_driver(NR_CHANNELS);
355 /* Fill struct tty_driver with ipoctal data */
356 tty->owner = THIS_MODULE;
357 tty->driver_name = KBUILD_MODNAME;
358 sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
362 tty->minor_start = 0;
363 tty->type = TTY_DRIVER_TYPE_SERIAL;
364 tty->subtype = SERIAL_TYPE_NORMAL;
365 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
366 tty->init_termios = tty_std_termios;
367 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
368 tty->init_termios.c_ispeed = 9600;
369 tty->init_termios.c_ospeed = 9600;
371 tty_set_operations(tty, &ipoctal_fops);
372 res = tty_register_driver(tty);
374 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
379 /* Save struct tty_driver for use it when uninstalling the device */
380 ipoctal->tty_drv = tty;
382 for (i = 0; i < NR_CHANNELS; i++) {
383 struct device *tty_dev;
385 channel = &ipoctal->channel[i];
386 tty_port_init(&channel->tty_port);
387 tty_port_alloc_xmit_buf(&channel->tty_port);
388 channel->tty_port.ops = &ipoctal_tty_port_ops;
390 ipoctal_reset_stats(&channel->stats);
391 channel->nb_bytes = 0;
392 spin_lock_init(&channel->lock);
393 channel->pointer_read = 0;
394 channel->pointer_write = 0;
395 tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
396 if (IS_ERR(tty_dev)) {
397 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
398 tty_port_destroy(&channel->tty_port);
401 dev_set_drvdata(tty_dev, channel);
405 * IP-OCTAL has different addresses to copy its IRQ vector.
406 * Depending of the carrier these addresses are accesible or not.
407 * More info in the datasheet.
409 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
410 ipoctal_irq_handler, ipoctal);
415 static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
416 const unsigned char *buf,
421 unsigned int *pointer_read = &channel->pointer_read;
423 /* Copy the bytes from the user buffer to the internal one */
424 for (i = 0; i < count; i++) {
425 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
426 spin_lock_irqsave(&channel->lock, flags);
427 channel->tty_port.xmit_buf[*pointer_read] = buf[i];
428 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
430 spin_unlock_irqrestore(&channel->lock, flags);
438 static int ipoctal_write_tty(struct tty_struct *tty,
439 const unsigned char *buf, int count)
441 struct ipoctal_channel *channel = tty->driver_data;
442 unsigned int char_copied;
444 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
446 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
447 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
448 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
449 channel->rx_enable = 0;
450 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
454 * Send a packet and then disable TX to avoid failure after several send
457 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
461 static unsigned int ipoctal_write_room(struct tty_struct *tty)
463 struct ipoctal_channel *channel = tty->driver_data;
465 return PAGE_SIZE - channel->nb_bytes;
468 static unsigned int ipoctal_chars_in_buffer(struct tty_struct *tty)
470 struct ipoctal_channel *channel = tty->driver_data;
472 return channel->nb_bytes;
475 static void ipoctal_set_termios(struct tty_struct *tty,
476 struct ktermios *old_termios)
479 unsigned char mr1 = 0;
480 unsigned char mr2 = 0;
481 unsigned char csr = 0;
482 struct ipoctal_channel *channel = tty->driver_data;
485 cflag = tty->termios.c_cflag;
487 /* Disable and reset everything before change the setup */
488 ipoctal_reset_channel(channel);
490 /* Set Bits per chars */
491 switch (cflag & CSIZE) {
493 mr1 |= MR1_CHRL_6_BITS;
496 mr1 |= MR1_CHRL_7_BITS;
500 mr1 |= MR1_CHRL_8_BITS;
501 /* By default, select CS8 */
502 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
509 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
511 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
513 mr1 |= MR1_PARITY_OFF;
515 /* Mark or space parity is not supported */
516 tty->termios.c_cflag &= ~CMSPAR;
520 mr2 |= MR2_STOP_BITS_LENGTH_2;
522 mr2 |= MR2_STOP_BITS_LENGTH_1;
524 /* Set the flow control */
525 switch (channel->board_id) {
526 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
527 if (cflag & CRTSCTS) {
528 mr1 |= MR1_RxRTS_CONTROL_ON;
529 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
531 mr1 |= MR1_RxRTS_CONTROL_OFF;
532 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
535 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
536 mr1 |= MR1_RxRTS_CONTROL_OFF;
537 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
539 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
540 mr1 |= MR1_RxRTS_CONTROL_OFF;
541 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
547 baud = tty_get_baud_rate(tty);
548 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
553 csr |= TX_CLK_75 | RX_CLK_75;
556 csr |= TX_CLK_110 | RX_CLK_110;
559 csr |= TX_CLK_150 | RX_CLK_150;
562 csr |= TX_CLK_300 | RX_CLK_300;
565 csr |= TX_CLK_600 | RX_CLK_600;
568 csr |= TX_CLK_1200 | RX_CLK_1200;
571 csr |= TX_CLK_1800 | RX_CLK_1800;
574 csr |= TX_CLK_2000 | RX_CLK_2000;
577 csr |= TX_CLK_2400 | RX_CLK_2400;
580 csr |= TX_CLK_4800 | RX_CLK_4800;
583 csr |= TX_CLK_9600 | RX_CLK_9600;
586 csr |= TX_CLK_19200 | RX_CLK_19200;
590 csr |= TX_CLK_38400 | RX_CLK_38400;
591 /* In case of default, we establish 38400 bps */
592 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
596 mr1 |= MR1_ERROR_CHAR;
597 mr1 |= MR1_RxINT_RxRDY;
599 /* Write the control registers */
600 iowrite8(mr1, &channel->regs->w.mr);
601 iowrite8(mr2, &channel->regs->w.mr);
602 iowrite8(csr, &channel->regs->w.csr);
604 /* Enable again the RX, if it was before */
605 if (channel->rx_enable)
606 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
609 static void ipoctal_hangup(struct tty_struct *tty)
612 struct ipoctal_channel *channel = tty->driver_data;
617 spin_lock_irqsave(&channel->lock, flags);
618 channel->nb_bytes = 0;
619 channel->pointer_read = 0;
620 channel->pointer_write = 0;
621 spin_unlock_irqrestore(&channel->lock, flags);
623 tty_port_hangup(&channel->tty_port);
625 ipoctal_reset_channel(channel);
626 tty_port_set_initialized(&channel->tty_port, 0);
627 wake_up_interruptible(&channel->tty_port.open_wait);
630 static void ipoctal_shutdown(struct tty_struct *tty)
632 struct ipoctal_channel *channel = tty->driver_data;
637 ipoctal_reset_channel(channel);
638 tty_port_set_initialized(&channel->tty_port, 0);
641 static void ipoctal_cleanup(struct tty_struct *tty)
643 struct ipoctal_channel *channel = tty->driver_data;
644 struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
646 /* release the carrier driver */
647 ipack_put_carrier(ipoctal->dev);
650 static const struct tty_operations ipoctal_fops = {
652 .open = ipoctal_open,
653 .close = ipoctal_close,
654 .write = ipoctal_write_tty,
655 .set_termios = ipoctal_set_termios,
656 .write_room = ipoctal_write_room,
657 .chars_in_buffer = ipoctal_chars_in_buffer,
658 .get_icount = ipoctal_get_icount,
659 .hangup = ipoctal_hangup,
660 .shutdown = ipoctal_shutdown,
661 .cleanup = ipoctal_cleanup,
664 static int ipoctal_probe(struct ipack_device *dev)
667 struct ipoctal *ipoctal;
669 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
674 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
678 dev_set_drvdata(&dev->dev, ipoctal);
686 static void __ipoctal_remove(struct ipoctal *ipoctal)
690 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
692 for (i = 0; i < NR_CHANNELS; i++) {
693 struct ipoctal_channel *channel = &ipoctal->channel[i];
694 tty_unregister_device(ipoctal->tty_drv, i);
695 tty_port_free_xmit_buf(&channel->tty_port);
696 tty_port_destroy(&channel->tty_port);
699 tty_unregister_driver(ipoctal->tty_drv);
700 put_tty_driver(ipoctal->tty_drv);
704 static void ipoctal_remove(struct ipack_device *idev)
706 __ipoctal_remove(dev_get_drvdata(&idev->dev));
709 static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
710 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
711 IPACK1_DEVICE_ID_SBS_OCTAL_232) },
712 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
713 IPACK1_DEVICE_ID_SBS_OCTAL_422) },
714 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
715 IPACK1_DEVICE_ID_SBS_OCTAL_485) },
719 MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
721 static const struct ipack_driver_ops ipoctal_drv_ops = {
722 .probe = ipoctal_probe,
723 .remove = ipoctal_remove,
726 static struct ipack_driver driver = {
727 .ops = &ipoctal_drv_ops,
728 .id_table = ipoctal_ids,
731 static int __init ipoctal_init(void)
733 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
736 static void __exit ipoctal_exit(void)
738 ipack_driver_unregister(&driver);
741 MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
742 MODULE_LICENSE("GPL");
744 module_init(ipoctal_init);
745 module_exit(ipoctal_exit);