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;
49 struct ipack_device *dev;
50 unsigned int board_id;
51 struct ipoctal_channel channel[NR_CHANNELS];
52 struct tty_driver *tty_drv;
53 u8 __iomem *mem8_space;
54 u8 __iomem *int_space;
57 static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
59 struct ipoctal_channel *channel;
61 channel = dev_get_drvdata(tty->dev);
64 * Enable RX. TX will be enabled when
65 * there is something to send
67 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
71 static int ipoctal_open(struct tty_struct *tty, struct file *file)
73 struct ipoctal_channel *channel;
75 channel = dev_get_drvdata(tty->dev);
76 tty->driver_data = channel;
78 return tty_port_open(&channel->tty_port, tty, file);
81 static void ipoctal_reset_stats(struct ipoctal_stats *stats)
86 stats->framing_err = 0;
87 stats->overrun_err = 0;
88 stats->parity_err = 0;
91 static void ipoctal_free_channel(struct ipoctal_channel *channel)
93 ipoctal_reset_stats(&channel->stats);
94 channel->pointer_read = 0;
95 channel->pointer_write = 0;
96 channel->nb_bytes = 0;
99 static void ipoctal_close(struct tty_struct *tty, struct file *filp)
101 struct ipoctal_channel *channel = tty->driver_data;
103 tty_port_close(&channel->tty_port, tty, filp);
104 ipoctal_free_channel(channel);
107 static int ipoctal_get_icount(struct tty_struct *tty,
108 struct serial_icounter_struct *icount)
110 struct ipoctal_channel *channel = tty->driver_data;
116 icount->rx = channel->stats.rx;
117 icount->tx = channel->stats.tx;
118 icount->frame = channel->stats.framing_err;
119 icount->parity = channel->stats.parity_err;
120 icount->brk = channel->stats.rcv_break;
124 static void ipoctal_irq_rx(struct ipoctal_channel *channel,
125 struct tty_struct *tty, u8 sr)
132 value = ioread8(&channel->regs->r.rhr);
134 /* Error: count statistics */
136 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
138 if (sr & SR_OVERRUN_ERROR) {
139 channel->stats.overrun_err++;
140 /* Overrun doesn't affect the current character*/
141 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
143 if (sr & SR_PARITY_ERROR) {
144 channel->stats.parity_err++;
147 if (sr & SR_FRAMING_ERROR) {
148 channel->stats.framing_err++;
151 if (sr & SR_RECEIVED_BREAK) {
152 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
153 channel->stats.rcv_break++;
157 tty_insert_flip_char(tty, value, flag);
159 /* Check if there are more characters in RX FIFO
160 * If there are more, the isr register for this channel
161 * has enabled the RxRDY|FFULL bit.
163 isr = ioread8(&channel->block_regs->r.isr);
164 sr = ioread8(&channel->regs->r.sr);
165 } while (isr & channel->isr_rx_rdy_mask);
167 tty_flip_buffer_push(tty);
170 static void ipoctal_irq_tx(struct ipoctal_channel *channel)
173 unsigned int *pointer_write = &channel->pointer_write;
175 if (channel->nb_bytes == 0)
178 value = channel->tty_port.xmit_buf[*pointer_write];
179 iowrite8(value, &channel->regs->w.thr);
182 *pointer_write = *pointer_write % PAGE_SIZE;
186 static void ipoctal_irq_channel(struct ipoctal_channel *channel)
189 struct tty_struct *tty;
191 tty = tty_port_tty_get(&channel->tty_port);
195 spin_lock(&channel->lock);
196 /* The HW is organized in pair of channels. See which register we need
198 isr = ioread8(&channel->block_regs->r.isr);
199 sr = ioread8(&channel->regs->r.sr);
201 if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
202 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
203 /* In case of RS-485, change from TX to RX when finishing TX.
205 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
206 iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
207 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
212 if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
213 ipoctal_irq_rx(channel, tty, sr);
215 /* TX of each character */
216 if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
217 ipoctal_irq_tx(channel);
220 spin_unlock(&channel->lock);
223 static irqreturn_t ipoctal_irq_handler(void *arg)
226 struct ipoctal *ipoctal = (struct ipoctal *) arg;
228 /* Clear the IPack device interrupt */
229 readw(ipoctal->int_space + ACK_INT_REQ0);
230 readw(ipoctal->int_space + ACK_INT_REQ1);
232 /* Check all channels */
233 for (i = 0; i < NR_CHANNELS; i++)
234 ipoctal_irq_channel(&ipoctal->channel[i]);
239 static const struct tty_port_operations ipoctal_tty_port_ops = {
241 .activate = ipoctal_port_activate,
244 static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
249 struct tty_driver *tty;
251 struct ipoctal_channel *channel;
252 struct ipack_region *region;
254 union scc2698_channel __iomem *chan_regs;
255 union scc2698_block __iomem *block_regs;
257 ipoctal->board_id = ipoctal->dev->id_device;
259 region = &ipoctal->dev->region[IPACK_IO_SPACE];
260 addr = devm_ioremap_nocache(&ipoctal->dev->dev,
261 region->start, region->size);
263 dev_err(&ipoctal->dev->dev,
264 "Unable to map slot [%d:%d] IO space!\n",
266 return -EADDRNOTAVAIL;
268 /* Save the virtual address to access the registers easily */
270 (union scc2698_channel __iomem *) addr;
272 (union scc2698_block __iomem *) addr;
274 region = &ipoctal->dev->region[IPACK_INT_SPACE];
276 devm_ioremap_nocache(&ipoctal->dev->dev,
277 region->start, region->size);
278 if (!ipoctal->int_space) {
279 dev_err(&ipoctal->dev->dev,
280 "Unable to map slot [%d:%d] INT space!\n",
282 return -EADDRNOTAVAIL;
285 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
286 ipoctal->mem8_space =
287 devm_ioremap_nocache(&ipoctal->dev->dev,
288 region->start, 0x8000);
290 dev_err(&ipoctal->dev->dev,
291 "Unable to map slot [%d:%d] MEM8 space!\n",
293 return -EADDRNOTAVAIL;
297 /* Disable RX and TX before touching anything */
298 for (i = 0; i < NR_CHANNELS ; i++) {
299 struct ipoctal_channel *channel = &ipoctal->channel[i];
300 channel->regs = chan_regs + i;
301 channel->block_regs = block_regs + (i >> 1);
302 channel->board_id = ipoctal->board_id;
304 channel->isr_tx_rdy_mask = ISR_TxRDY_B;
305 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
307 channel->isr_tx_rdy_mask = ISR_TxRDY_A;
308 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
311 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
312 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
313 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
314 iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
315 &channel->regs->w.mr); /* mr1 */
316 iowrite8(0, &channel->regs->w.mr); /* mr2 */
317 iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr);
320 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
321 iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
322 iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
323 &block_regs[i].w.opcr);
324 iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
325 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
326 &block_regs[i].w.imr);
330 * IP-OCTAL has different addresses to copy its IRQ vector.
331 * Depending of the carrier these addresses are accesible or not.
332 * More info in the datasheet.
334 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
335 ipoctal_irq_handler, ipoctal);
337 iowrite8(1, ipoctal->mem8_space + 1);
339 /* Register the TTY device */
341 /* Each IP-OCTAL channel is a TTY port */
342 tty = alloc_tty_driver(NR_CHANNELS);
347 /* Fill struct tty_driver with ipoctal data */
348 tty->owner = THIS_MODULE;
349 tty->driver_name = KBUILD_MODNAME;
350 sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
354 tty->minor_start = 0;
355 tty->type = TTY_DRIVER_TYPE_SERIAL;
356 tty->subtype = SERIAL_TYPE_NORMAL;
357 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
358 tty->init_termios = tty_std_termios;
359 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
360 tty->init_termios.c_ispeed = 9600;
361 tty->init_termios.c_ospeed = 9600;
363 tty_set_operations(tty, &ipoctal_fops);
364 res = tty_register_driver(tty);
366 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
371 /* Save struct tty_driver for use it when uninstalling the device */
372 ipoctal->tty_drv = tty;
374 for (i = 0; i < NR_CHANNELS; i++) {
375 struct device *tty_dev;
377 channel = &ipoctal->channel[i];
378 tty_port_init(&channel->tty_port);
379 tty_port_alloc_xmit_buf(&channel->tty_port);
380 channel->tty_port.ops = &ipoctal_tty_port_ops;
382 ipoctal_reset_stats(&channel->stats);
383 channel->nb_bytes = 0;
384 spin_lock_init(&channel->lock);
385 channel->pointer_read = 0;
386 channel->pointer_write = 0;
387 tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
388 if (IS_ERR(tty_dev)) {
389 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
390 tty_port_destroy(&channel->tty_port);
393 dev_set_drvdata(tty_dev, channel);
399 static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
400 const unsigned char *buf,
405 unsigned int *pointer_read = &channel->pointer_read;
407 /* Copy the bytes from the user buffer to the internal one */
408 for (i = 0; i < count; i++) {
409 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
410 spin_lock_irqsave(&channel->lock, flags);
411 channel->tty_port.xmit_buf[*pointer_read] = buf[i];
412 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
414 spin_unlock_irqrestore(&channel->lock, flags);
422 static int ipoctal_write_tty(struct tty_struct *tty,
423 const unsigned char *buf, int count)
425 struct ipoctal_channel *channel = tty->driver_data;
426 unsigned int char_copied;
428 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
430 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
431 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
432 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
433 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
437 * Send a packet and then disable TX to avoid failure after several send
440 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
444 static int ipoctal_write_room(struct tty_struct *tty)
446 struct ipoctal_channel *channel = tty->driver_data;
448 return PAGE_SIZE - channel->nb_bytes;
451 static int ipoctal_chars_in_buffer(struct tty_struct *tty)
453 struct ipoctal_channel *channel = tty->driver_data;
455 return channel->nb_bytes;
458 static void ipoctal_set_termios(struct tty_struct *tty,
459 struct ktermios *old_termios)
462 unsigned char mr1 = 0;
463 unsigned char mr2 = 0;
464 unsigned char csr = 0;
465 struct ipoctal_channel *channel = tty->driver_data;
468 cflag = tty->termios.c_cflag;
470 /* Disable and reset everything before change the setup */
471 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
472 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
473 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
474 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
475 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
477 /* Set Bits per chars */
478 switch (cflag & CSIZE) {
480 mr1 |= MR1_CHRL_6_BITS;
483 mr1 |= MR1_CHRL_7_BITS;
487 mr1 |= MR1_CHRL_8_BITS;
488 /* By default, select CS8 */
489 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
496 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
498 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
500 mr1 |= MR1_PARITY_OFF;
502 /* Mark or space parity is not supported */
503 tty->termios.c_cflag &= ~CMSPAR;
507 mr2 |= MR2_STOP_BITS_LENGTH_2;
509 mr2 |= MR2_STOP_BITS_LENGTH_1;
511 /* Set the flow control */
512 switch (channel->board_id) {
513 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
514 if (cflag & CRTSCTS) {
515 mr1 |= MR1_RxRTS_CONTROL_ON;
516 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
518 mr1 |= MR1_RxRTS_CONTROL_OFF;
519 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
522 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
523 mr1 |= MR1_RxRTS_CONTROL_OFF;
524 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
526 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
527 mr1 |= MR1_RxRTS_CONTROL_OFF;
528 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
535 baud = tty_get_baud_rate(tty);
536 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
541 csr |= TX_CLK_75 | RX_CLK_75;
544 csr |= TX_CLK_110 | RX_CLK_110;
547 csr |= TX_CLK_150 | RX_CLK_150;
550 csr |= TX_CLK_300 | RX_CLK_300;
553 csr |= TX_CLK_600 | RX_CLK_600;
556 csr |= TX_CLK_1200 | RX_CLK_1200;
559 csr |= TX_CLK_1800 | RX_CLK_1800;
562 csr |= TX_CLK_2000 | RX_CLK_2000;
565 csr |= TX_CLK_2400 | RX_CLK_2400;
568 csr |= TX_CLK_4800 | RX_CLK_4800;
571 csr |= TX_CLK_9600 | RX_CLK_9600;
574 csr |= TX_CLK_19200 | RX_CLK_19200;
578 csr |= TX_CLK_38400 | RX_CLK_38400;
579 /* In case of default, we establish 38400 bps */
580 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
584 mr1 |= MR1_ERROR_CHAR;
585 mr1 |= MR1_RxINT_RxRDY;
587 /* Write the control registers */
588 iowrite8(mr1, &channel->regs->w.mr);
589 iowrite8(mr2, &channel->regs->w.mr);
590 iowrite8(csr, &channel->regs->w.csr);
592 /* Enable again the RX */
593 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
596 static void ipoctal_hangup(struct tty_struct *tty)
599 struct ipoctal_channel *channel = tty->driver_data;
604 spin_lock_irqsave(&channel->lock, flags);
605 channel->nb_bytes = 0;
606 channel->pointer_read = 0;
607 channel->pointer_write = 0;
608 spin_unlock_irqrestore(&channel->lock, flags);
610 tty_port_hangup(&channel->tty_port);
612 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
613 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
614 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
615 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
616 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
618 clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
619 wake_up_interruptible(&channel->tty_port.open_wait);
622 static const struct tty_operations ipoctal_fops = {
624 .open = ipoctal_open,
625 .close = ipoctal_close,
626 .write = ipoctal_write_tty,
627 .set_termios = ipoctal_set_termios,
628 .write_room = ipoctal_write_room,
629 .chars_in_buffer = ipoctal_chars_in_buffer,
630 .get_icount = ipoctal_get_icount,
631 .hangup = ipoctal_hangup,
634 static int ipoctal_probe(struct ipack_device *dev)
637 struct ipoctal *ipoctal;
639 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
644 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
648 dev_set_drvdata(&dev->dev, ipoctal);
656 static void __ipoctal_remove(struct ipoctal *ipoctal)
660 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
662 for (i = 0; i < NR_CHANNELS; i++) {
663 struct ipoctal_channel *channel = &ipoctal->channel[i];
664 tty_unregister_device(ipoctal->tty_drv, i);
665 tty_port_free_xmit_buf(&channel->tty_port);
666 tty_port_destroy(&channel->tty_port);
669 tty_unregister_driver(ipoctal->tty_drv);
670 put_tty_driver(ipoctal->tty_drv);
674 static void ipoctal_remove(struct ipack_device *idev)
676 __ipoctal_remove(dev_get_drvdata(&idev->dev));
679 static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
680 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
681 IPACK1_DEVICE_ID_SBS_OCTAL_232) },
682 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
683 IPACK1_DEVICE_ID_SBS_OCTAL_422) },
684 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
685 IPACK1_DEVICE_ID_SBS_OCTAL_485) },
689 MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
691 static const struct ipack_driver_ops ipoctal_drv_ops = {
692 .probe = ipoctal_probe,
693 .remove = ipoctal_remove,
696 static struct ipack_driver driver = {
697 .ops = &ipoctal_drv_ops,
698 .id_table = ipoctal_ids,
701 static int __init ipoctal_init(void)
703 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
706 static void __exit ipoctal_exit(void)
708 ipack_driver_unregister(&driver);
711 MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
712 MODULE_LICENSE("GPL");
714 module_init(ipoctal_init);
715 module_exit(ipoctal_exit);