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;
38 union scc2698_channel __iomem *regs;
39 union scc2698_block __iomem *block_regs;
40 unsigned int board_id;
43 unsigned int rx_enable;
47 struct ipack_device *dev;
48 unsigned int board_id;
49 struct ipoctal_channel channel[NR_CHANNELS];
50 struct tty_driver *tty_drv;
51 u8 __iomem *mem8_space;
52 u8 __iomem *int_space;
55 static inline struct ipoctal *chan_to_ipoctal(struct ipoctal_channel *chan,
58 return container_of(chan, struct ipoctal, channel[index]);
61 static void ipoctal_reset_channel(struct ipoctal_channel *channel)
63 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
64 channel->rx_enable = 0;
65 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
66 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
67 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
68 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
71 static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
73 struct ipoctal_channel *channel;
75 channel = dev_get_drvdata(tty->dev);
78 * Enable RX. TX will be enabled when
79 * there is something to send
81 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
82 channel->rx_enable = 1;
86 static int ipoctal_open(struct tty_struct *tty, struct file *file)
88 struct ipoctal_channel *channel = dev_get_drvdata(tty->dev);
89 struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
92 tty->driver_data = channel;
94 if (!ipack_get_carrier(ipoctal->dev))
97 err = tty_port_open(&channel->tty_port, tty, file);
99 ipack_put_carrier(ipoctal->dev);
104 static void ipoctal_reset_stats(struct ipoctal_stats *stats)
108 stats->rcv_break = 0;
109 stats->framing_err = 0;
110 stats->overrun_err = 0;
111 stats->parity_err = 0;
114 static void ipoctal_free_channel(struct ipoctal_channel *channel)
116 ipoctal_reset_stats(&channel->stats);
117 channel->pointer_read = 0;
118 channel->pointer_write = 0;
119 channel->nb_bytes = 0;
122 static void ipoctal_close(struct tty_struct *tty, struct file *filp)
124 struct ipoctal_channel *channel = tty->driver_data;
126 tty_port_close(&channel->tty_port, tty, filp);
127 ipoctal_free_channel(channel);
130 static int ipoctal_get_icount(struct tty_struct *tty,
131 struct serial_icounter_struct *icount)
133 struct ipoctal_channel *channel = tty->driver_data;
139 icount->rx = channel->stats.rx;
140 icount->tx = channel->stats.tx;
141 icount->frame = channel->stats.framing_err;
142 icount->parity = channel->stats.parity_err;
143 icount->brk = channel->stats.rcv_break;
147 static void ipoctal_irq_rx(struct ipoctal_channel *channel, u8 sr)
149 struct tty_port *port = &channel->tty_port;
155 value = ioread8(&channel->regs->r.rhr);
157 /* Error: count statistics */
159 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
161 if (sr & SR_OVERRUN_ERROR) {
162 channel->stats.overrun_err++;
163 /* Overrun doesn't affect the current character*/
164 tty_insert_flip_char(port, 0, TTY_OVERRUN);
166 if (sr & SR_PARITY_ERROR) {
167 channel->stats.parity_err++;
170 if (sr & SR_FRAMING_ERROR) {
171 channel->stats.framing_err++;
174 if (sr & SR_RECEIVED_BREAK) {
175 channel->stats.rcv_break++;
179 tty_insert_flip_char(port, value, flag);
181 /* Check if there are more characters in RX FIFO
182 * If there are more, the isr register for this channel
183 * has enabled the RxRDY|FFULL bit.
185 isr = ioread8(&channel->block_regs->r.isr);
186 sr = ioread8(&channel->regs->r.sr);
187 } while (isr & channel->isr_rx_rdy_mask);
189 tty_flip_buffer_push(port);
192 static void ipoctal_irq_tx(struct ipoctal_channel *channel)
195 unsigned int *pointer_write = &channel->pointer_write;
197 if (channel->nb_bytes == 0)
200 spin_lock(&channel->lock);
201 value = channel->tty_port.xmit_buf[*pointer_write];
202 iowrite8(value, &channel->regs->w.thr);
205 *pointer_write = *pointer_write % PAGE_SIZE;
207 spin_unlock(&channel->lock);
210 static void ipoctal_irq_channel(struct ipoctal_channel *channel)
214 /* The HW is organized in pair of channels. See which register we need
216 isr = ioread8(&channel->block_regs->r.isr);
217 sr = ioread8(&channel->regs->r.sr);
219 if (isr & (IMR_DELTA_BREAK_A | IMR_DELTA_BREAK_B))
220 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
222 if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
223 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
224 /* In case of RS-485, change from TX to RX when finishing TX.
226 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
227 iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
228 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
229 channel->rx_enable = 1;
234 if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
235 ipoctal_irq_rx(channel, sr);
237 /* TX of each character */
238 if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
239 ipoctal_irq_tx(channel);
242 static irqreturn_t ipoctal_irq_handler(void *arg)
245 struct ipoctal *ipoctal = (struct ipoctal *) arg;
247 /* Clear the IPack device interrupt */
248 readw(ipoctal->int_space + ACK_INT_REQ0);
249 readw(ipoctal->int_space + ACK_INT_REQ1);
251 /* Check all channels */
252 for (i = 0; i < NR_CHANNELS; i++)
253 ipoctal_irq_channel(&ipoctal->channel[i]);
258 static const struct tty_port_operations ipoctal_tty_port_ops = {
260 .activate = ipoctal_port_activate,
263 static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
268 struct tty_driver *tty;
270 struct ipoctal_channel *channel;
271 struct ipack_region *region;
273 union scc2698_channel __iomem *chan_regs;
274 union scc2698_block __iomem *block_regs;
276 ipoctal->board_id = ipoctal->dev->id_device;
278 region = &ipoctal->dev->region[IPACK_IO_SPACE];
279 addr = devm_ioremap_nocache(&ipoctal->dev->dev,
280 region->start, region->size);
282 dev_err(&ipoctal->dev->dev,
283 "Unable to map slot [%d:%d] IO space!\n",
285 return -EADDRNOTAVAIL;
287 /* Save the virtual address to access the registers easily */
289 (union scc2698_channel __iomem *) addr;
291 (union scc2698_block __iomem *) addr;
293 region = &ipoctal->dev->region[IPACK_INT_SPACE];
295 devm_ioremap_nocache(&ipoctal->dev->dev,
296 region->start, region->size);
297 if (!ipoctal->int_space) {
298 dev_err(&ipoctal->dev->dev,
299 "Unable to map slot [%d:%d] INT space!\n",
301 return -EADDRNOTAVAIL;
304 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
305 ipoctal->mem8_space =
306 devm_ioremap_nocache(&ipoctal->dev->dev,
307 region->start, 0x8000);
308 if (!ipoctal->mem8_space) {
309 dev_err(&ipoctal->dev->dev,
310 "Unable to map slot [%d:%d] MEM8 space!\n",
312 return -EADDRNOTAVAIL;
316 /* Disable RX and TX before touching anything */
317 for (i = 0; i < NR_CHANNELS ; i++) {
318 struct ipoctal_channel *channel = &ipoctal->channel[i];
319 channel->regs = chan_regs + i;
320 channel->block_regs = block_regs + (i >> 1);
321 channel->board_id = ipoctal->board_id;
323 channel->isr_tx_rdy_mask = ISR_TxRDY_B;
324 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
326 channel->isr_tx_rdy_mask = ISR_TxRDY_A;
327 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
330 ipoctal_reset_channel(channel);
331 iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
332 &channel->regs->w.mr); /* mr1 */
333 iowrite8(0, &channel->regs->w.mr); /* mr2 */
334 iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr);
337 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
338 iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
339 iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
340 &block_regs[i].w.opcr);
341 iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
342 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
343 &block_regs[i].w.imr);
347 iowrite8(1, ipoctal->mem8_space + 1);
349 /* Register the TTY device */
351 /* Each IP-OCTAL channel is a TTY port */
352 tty = alloc_tty_driver(NR_CHANNELS);
357 /* Fill struct tty_driver with ipoctal data */
358 tty->owner = THIS_MODULE;
359 tty->driver_name = KBUILD_MODNAME;
360 sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
364 tty->minor_start = 0;
365 tty->type = TTY_DRIVER_TYPE_SERIAL;
366 tty->subtype = SERIAL_TYPE_NORMAL;
367 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
368 tty->init_termios = tty_std_termios;
369 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
370 tty->init_termios.c_ispeed = 9600;
371 tty->init_termios.c_ospeed = 9600;
373 tty_set_operations(tty, &ipoctal_fops);
374 res = tty_register_driver(tty);
376 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
381 /* Save struct tty_driver for use it when uninstalling the device */
382 ipoctal->tty_drv = tty;
384 for (i = 0; i < NR_CHANNELS; i++) {
385 struct device *tty_dev;
387 channel = &ipoctal->channel[i];
388 tty_port_init(&channel->tty_port);
389 tty_port_alloc_xmit_buf(&channel->tty_port);
390 channel->tty_port.ops = &ipoctal_tty_port_ops;
392 ipoctal_reset_stats(&channel->stats);
393 channel->nb_bytes = 0;
394 spin_lock_init(&channel->lock);
395 channel->pointer_read = 0;
396 channel->pointer_write = 0;
397 tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
398 if (IS_ERR(tty_dev)) {
399 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
400 tty_port_destroy(&channel->tty_port);
403 dev_set_drvdata(tty_dev, channel);
407 * IP-OCTAL has different addresses to copy its IRQ vector.
408 * Depending of the carrier these addresses are accesible or not.
409 * More info in the datasheet.
411 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
412 ipoctal_irq_handler, ipoctal);
417 static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
418 const unsigned char *buf,
423 unsigned int *pointer_read = &channel->pointer_read;
425 /* Copy the bytes from the user buffer to the internal one */
426 for (i = 0; i < count; i++) {
427 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
428 spin_lock_irqsave(&channel->lock, flags);
429 channel->tty_port.xmit_buf[*pointer_read] = buf[i];
430 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
432 spin_unlock_irqrestore(&channel->lock, flags);
440 static int ipoctal_write_tty(struct tty_struct *tty,
441 const unsigned char *buf, int count)
443 struct ipoctal_channel *channel = tty->driver_data;
444 unsigned int char_copied;
446 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
448 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
449 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
450 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
451 channel->rx_enable = 0;
452 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
456 * Send a packet and then disable TX to avoid failure after several send
459 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
463 static int ipoctal_write_room(struct tty_struct *tty)
465 struct ipoctal_channel *channel = tty->driver_data;
467 return PAGE_SIZE - channel->nb_bytes;
470 static int ipoctal_chars_in_buffer(struct tty_struct *tty)
472 struct ipoctal_channel *channel = tty->driver_data;
474 return channel->nb_bytes;
477 static void ipoctal_set_termios(struct tty_struct *tty,
478 struct ktermios *old_termios)
481 unsigned char mr1 = 0;
482 unsigned char mr2 = 0;
483 unsigned char csr = 0;
484 struct ipoctal_channel *channel = tty->driver_data;
487 cflag = tty->termios.c_cflag;
489 /* Disable and reset everything before change the setup */
490 ipoctal_reset_channel(channel);
492 /* Set Bits per chars */
493 switch (cflag & CSIZE) {
495 mr1 |= MR1_CHRL_6_BITS;
498 mr1 |= MR1_CHRL_7_BITS;
502 mr1 |= MR1_CHRL_8_BITS;
503 /* By default, select CS8 */
504 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
511 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
513 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
515 mr1 |= MR1_PARITY_OFF;
517 /* Mark or space parity is not supported */
518 tty->termios.c_cflag &= ~CMSPAR;
522 mr2 |= MR2_STOP_BITS_LENGTH_2;
524 mr2 |= MR2_STOP_BITS_LENGTH_1;
526 /* Set the flow control */
527 switch (channel->board_id) {
528 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
529 if (cflag & CRTSCTS) {
530 mr1 |= MR1_RxRTS_CONTROL_ON;
531 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
533 mr1 |= MR1_RxRTS_CONTROL_OFF;
534 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
537 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
538 mr1 |= MR1_RxRTS_CONTROL_OFF;
539 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
541 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
542 mr1 |= MR1_RxRTS_CONTROL_OFF;
543 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
550 baud = tty_get_baud_rate(tty);
551 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
556 csr |= TX_CLK_75 | RX_CLK_75;
559 csr |= TX_CLK_110 | RX_CLK_110;
562 csr |= TX_CLK_150 | RX_CLK_150;
565 csr |= TX_CLK_300 | RX_CLK_300;
568 csr |= TX_CLK_600 | RX_CLK_600;
571 csr |= TX_CLK_1200 | RX_CLK_1200;
574 csr |= TX_CLK_1800 | RX_CLK_1800;
577 csr |= TX_CLK_2000 | RX_CLK_2000;
580 csr |= TX_CLK_2400 | RX_CLK_2400;
583 csr |= TX_CLK_4800 | RX_CLK_4800;
586 csr |= TX_CLK_9600 | RX_CLK_9600;
589 csr |= TX_CLK_19200 | RX_CLK_19200;
593 csr |= TX_CLK_38400 | RX_CLK_38400;
594 /* In case of default, we establish 38400 bps */
595 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
599 mr1 |= MR1_ERROR_CHAR;
600 mr1 |= MR1_RxINT_RxRDY;
602 /* Write the control registers */
603 iowrite8(mr1, &channel->regs->w.mr);
604 iowrite8(mr2, &channel->regs->w.mr);
605 iowrite8(csr, &channel->regs->w.csr);
607 /* Enable again the RX, if it was before */
608 if (channel->rx_enable)
609 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
612 static void ipoctal_hangup(struct tty_struct *tty)
615 struct ipoctal_channel *channel = tty->driver_data;
620 spin_lock_irqsave(&channel->lock, flags);
621 channel->nb_bytes = 0;
622 channel->pointer_read = 0;
623 channel->pointer_write = 0;
624 spin_unlock_irqrestore(&channel->lock, flags);
626 tty_port_hangup(&channel->tty_port);
628 ipoctal_reset_channel(channel);
629 tty_port_set_initialized(&channel->tty_port, 0);
630 wake_up_interruptible(&channel->tty_port.open_wait);
633 static void ipoctal_shutdown(struct tty_struct *tty)
635 struct ipoctal_channel *channel = tty->driver_data;
640 ipoctal_reset_channel(channel);
641 tty_port_set_initialized(&channel->tty_port, 0);
644 static void ipoctal_cleanup(struct tty_struct *tty)
646 struct ipoctal_channel *channel = tty->driver_data;
647 struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
649 /* release the carrier driver */
650 ipack_put_carrier(ipoctal->dev);
653 static const struct tty_operations ipoctal_fops = {
655 .open = ipoctal_open,
656 .close = ipoctal_close,
657 .write = ipoctal_write_tty,
658 .set_termios = ipoctal_set_termios,
659 .write_room = ipoctal_write_room,
660 .chars_in_buffer = ipoctal_chars_in_buffer,
661 .get_icount = ipoctal_get_icount,
662 .hangup = ipoctal_hangup,
663 .shutdown = ipoctal_shutdown,
664 .cleanup = ipoctal_cleanup,
667 static int ipoctal_probe(struct ipack_device *dev)
670 struct ipoctal *ipoctal;
672 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
677 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
681 dev_set_drvdata(&dev->dev, ipoctal);
689 static void __ipoctal_remove(struct ipoctal *ipoctal)
693 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
695 for (i = 0; i < NR_CHANNELS; i++) {
696 struct ipoctal_channel *channel = &ipoctal->channel[i];
697 tty_unregister_device(ipoctal->tty_drv, i);
698 tty_port_free_xmit_buf(&channel->tty_port);
699 tty_port_destroy(&channel->tty_port);
702 tty_unregister_driver(ipoctal->tty_drv);
703 put_tty_driver(ipoctal->tty_drv);
707 static void ipoctal_remove(struct ipack_device *idev)
709 __ipoctal_remove(dev_get_drvdata(&idev->dev));
712 static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
713 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
714 IPACK1_DEVICE_ID_SBS_OCTAL_232) },
715 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
716 IPACK1_DEVICE_ID_SBS_OCTAL_422) },
717 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
718 IPACK1_DEVICE_ID_SBS_OCTAL_485) },
722 MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
724 static const struct ipack_driver_ops ipoctal_drv_ops = {
725 .probe = ipoctal_probe,
726 .remove = ipoctal_remove,
729 static struct ipack_driver driver = {
730 .ops = &ipoctal_drv_ops,
731 .id_table = ipoctal_ids,
734 static int __init ipoctal_init(void)
736 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
739 static void __exit ipoctal_exit(void)
741 ipack_driver_unregister(&driver);
744 MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
745 MODULE_LICENSE("GPL");
747 module_init(ipoctal_init);
748 module_exit(ipoctal_exit);