xref: /openbmc/linux/drivers/ipack/devices/ipoctal.c (revision 21d27ed4616c9a7f2886c4159b4c409f73f96e76)
105e5027eSGreg Kroah-Hartman /**
205e5027eSGreg Kroah-Hartman  * ipoctal.c
305e5027eSGreg Kroah-Hartman  *
405e5027eSGreg Kroah-Hartman  * driver for the GE IP-OCTAL boards
505e5027eSGreg Kroah-Hartman  *
605e5027eSGreg Kroah-Hartman  * Copyright (C) 2009-2012 CERN (www.cern.ch)
705e5027eSGreg Kroah-Hartman  * Author: Nicolas Serafini, EIC2 SA
805e5027eSGreg Kroah-Hartman  * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
905e5027eSGreg Kroah-Hartman  *
1005e5027eSGreg Kroah-Hartman  * This program is free software; you can redistribute it and/or modify it
1105e5027eSGreg Kroah-Hartman  * under the terms of the GNU General Public License as published by the Free
1205e5027eSGreg Kroah-Hartman  * Software Foundation; version 2 of the License.
1305e5027eSGreg Kroah-Hartman  */
1405e5027eSGreg Kroah-Hartman 
1505e5027eSGreg Kroah-Hartman #include <linux/device.h>
1605e5027eSGreg Kroah-Hartman #include <linux/module.h>
1705e5027eSGreg Kroah-Hartman #include <linux/interrupt.h>
1805e5027eSGreg Kroah-Hartman #include <linux/sched.h>
1905e5027eSGreg Kroah-Hartman #include <linux/tty.h>
2005e5027eSGreg Kroah-Hartman #include <linux/serial.h>
2105e5027eSGreg Kroah-Hartman #include <linux/tty_flip.h>
2205e5027eSGreg Kroah-Hartman #include <linux/slab.h>
2305e5027eSGreg Kroah-Hartman #include <linux/io.h>
247dbce021SSamuel Iglesias Gonsalvez #include <linux/ipack.h>
2505e5027eSGreg Kroah-Hartman #include "ipoctal.h"
2605e5027eSGreg Kroah-Hartman #include "scc2698.h"
2705e5027eSGreg Kroah-Hartman 
2805e5027eSGreg Kroah-Hartman #define IP_OCTAL_ID_SPACE_VECTOR    0x41
2905e5027eSGreg Kroah-Hartman #define IP_OCTAL_NB_BLOCKS          4
3005e5027eSGreg Kroah-Hartman 
3105e5027eSGreg Kroah-Hartman static const struct tty_operations ipoctal_fops;
3205e5027eSGreg Kroah-Hartman 
3305e5027eSGreg Kroah-Hartman struct ipoctal_channel {
3405e5027eSGreg Kroah-Hartman 	struct ipoctal_stats		stats;
3505e5027eSGreg Kroah-Hartman 	unsigned int			nb_bytes;
3605e5027eSGreg Kroah-Hartman 	wait_queue_head_t		queue;
3705e5027eSGreg Kroah-Hartman 	spinlock_t			lock;
3805e5027eSGreg Kroah-Hartman 	unsigned int			pointer_read;
3905e5027eSGreg Kroah-Hartman 	unsigned int			pointer_write;
4005e5027eSGreg Kroah-Hartman 	struct tty_port			tty_port;
4105e5027eSGreg Kroah-Hartman 	union scc2698_channel __iomem	*regs;
4205e5027eSGreg Kroah-Hartman 	union scc2698_block __iomem	*block_regs;
4305e5027eSGreg Kroah-Hartman 	unsigned int			board_id;
4405e5027eSGreg Kroah-Hartman 	u8				isr_rx_rdy_mask;
4505e5027eSGreg Kroah-Hartman 	u8				isr_tx_rdy_mask;
4605e5027eSGreg Kroah-Hartman };
4705e5027eSGreg Kroah-Hartman 
4805e5027eSGreg Kroah-Hartman struct ipoctal {
4905e5027eSGreg Kroah-Hartman 	struct ipack_device		*dev;
5005e5027eSGreg Kroah-Hartman 	unsigned int			board_id;
5105e5027eSGreg Kroah-Hartman 	struct ipoctal_channel		channel[NR_CHANNELS];
5205e5027eSGreg Kroah-Hartman 	struct tty_driver		*tty_drv;
5305e5027eSGreg Kroah-Hartman 	u8 __iomem			*mem8_space;
5405e5027eSGreg Kroah-Hartman 	u8 __iomem			*int_space;
5505e5027eSGreg Kroah-Hartman };
5605e5027eSGreg Kroah-Hartman 
5705e5027eSGreg Kroah-Hartman static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
5805e5027eSGreg Kroah-Hartman {
5905e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel;
6005e5027eSGreg Kroah-Hartman 
6105e5027eSGreg Kroah-Hartman 	channel = dev_get_drvdata(tty->dev);
6205e5027eSGreg Kroah-Hartman 
63a3882b78SSamuel Iglesias Gonsalvez 	/*
64a3882b78SSamuel Iglesias Gonsalvez 	 * Enable RX. TX will be enabled when
65a3882b78SSamuel Iglesias Gonsalvez 	 * there is something to send
66a3882b78SSamuel Iglesias Gonsalvez 	 */
6705e5027eSGreg Kroah-Hartman 	iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
6805e5027eSGreg Kroah-Hartman 	return 0;
6905e5027eSGreg Kroah-Hartman }
7005e5027eSGreg Kroah-Hartman 
7105e5027eSGreg Kroah-Hartman static int ipoctal_open(struct tty_struct *tty, struct file *file)
7205e5027eSGreg Kroah-Hartman {
7305e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel;
7405e5027eSGreg Kroah-Hartman 
7505e5027eSGreg Kroah-Hartman 	channel = dev_get_drvdata(tty->dev);
7605e5027eSGreg Kroah-Hartman 	tty->driver_data = channel;
7705e5027eSGreg Kroah-Hartman 
787e5730d7SSamuel Iglesias Gonsalvez 	return tty_port_open(&channel->tty_port, tty, file);
7905e5027eSGreg Kroah-Hartman }
8005e5027eSGreg Kroah-Hartman 
8105e5027eSGreg Kroah-Hartman static void ipoctal_reset_stats(struct ipoctal_stats *stats)
8205e5027eSGreg Kroah-Hartman {
8305e5027eSGreg Kroah-Hartman 	stats->tx = 0;
8405e5027eSGreg Kroah-Hartman 	stats->rx = 0;
8505e5027eSGreg Kroah-Hartman 	stats->rcv_break = 0;
8605e5027eSGreg Kroah-Hartman 	stats->framing_err = 0;
8705e5027eSGreg Kroah-Hartman 	stats->overrun_err = 0;
8805e5027eSGreg Kroah-Hartman 	stats->parity_err = 0;
8905e5027eSGreg Kroah-Hartman }
9005e5027eSGreg Kroah-Hartman 
9105e5027eSGreg Kroah-Hartman static void ipoctal_free_channel(struct ipoctal_channel *channel)
9205e5027eSGreg Kroah-Hartman {
9305e5027eSGreg Kroah-Hartman 	ipoctal_reset_stats(&channel->stats);
9405e5027eSGreg Kroah-Hartman 	channel->pointer_read = 0;
9505e5027eSGreg Kroah-Hartman 	channel->pointer_write = 0;
9605e5027eSGreg Kroah-Hartman 	channel->nb_bytes = 0;
9705e5027eSGreg Kroah-Hartman }
9805e5027eSGreg Kroah-Hartman 
9905e5027eSGreg Kroah-Hartman static void ipoctal_close(struct tty_struct *tty, struct file *filp)
10005e5027eSGreg Kroah-Hartman {
10105e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
10205e5027eSGreg Kroah-Hartman 
10305e5027eSGreg Kroah-Hartman 	tty_port_close(&channel->tty_port, tty, filp);
10405e5027eSGreg Kroah-Hartman 	ipoctal_free_channel(channel);
10505e5027eSGreg Kroah-Hartman }
10605e5027eSGreg Kroah-Hartman 
10705e5027eSGreg Kroah-Hartman static int ipoctal_get_icount(struct tty_struct *tty,
10805e5027eSGreg Kroah-Hartman 			      struct serial_icounter_struct *icount)
10905e5027eSGreg Kroah-Hartman {
11005e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
11105e5027eSGreg Kroah-Hartman 
11205e5027eSGreg Kroah-Hartman 	icount->cts = 0;
11305e5027eSGreg Kroah-Hartman 	icount->dsr = 0;
11405e5027eSGreg Kroah-Hartman 	icount->rng = 0;
11505e5027eSGreg Kroah-Hartman 	icount->dcd = 0;
11605e5027eSGreg Kroah-Hartman 	icount->rx = channel->stats.rx;
11705e5027eSGreg Kroah-Hartman 	icount->tx = channel->stats.tx;
11805e5027eSGreg Kroah-Hartman 	icount->frame = channel->stats.framing_err;
11905e5027eSGreg Kroah-Hartman 	icount->parity = channel->stats.parity_err;
12005e5027eSGreg Kroah-Hartman 	icount->brk = channel->stats.rcv_break;
12105e5027eSGreg Kroah-Hartman 	return 0;
12205e5027eSGreg Kroah-Hartman }
12305e5027eSGreg Kroah-Hartman 
12405e5027eSGreg Kroah-Hartman static void ipoctal_irq_rx(struct ipoctal_channel *channel,
12505e5027eSGreg Kroah-Hartman 			   struct tty_struct *tty, u8 sr)
12605e5027eSGreg Kroah-Hartman {
12705e5027eSGreg Kroah-Hartman 	unsigned char value;
128b5071f2cSSamuel Iglesias Gonsalvez 	unsigned char flag;
12905e5027eSGreg Kroah-Hartman 	u8 isr;
13005e5027eSGreg Kroah-Hartman 
13105e5027eSGreg Kroah-Hartman 	do {
13205e5027eSGreg Kroah-Hartman 		value = ioread8(&channel->regs->r.rhr);
133b5071f2cSSamuel Iglesias Gonsalvez 		flag = TTY_NORMAL;
13405e5027eSGreg Kroah-Hartman 		/* Error: count statistics */
13505e5027eSGreg Kroah-Hartman 		if (sr & SR_ERROR) {
13605e5027eSGreg Kroah-Hartman 			iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
13705e5027eSGreg Kroah-Hartman 
13805e5027eSGreg Kroah-Hartman 			if (sr & SR_OVERRUN_ERROR) {
13905e5027eSGreg Kroah-Hartman 				channel->stats.overrun_err++;
14005e5027eSGreg Kroah-Hartman 				/* Overrun doesn't affect the current character*/
14105e5027eSGreg Kroah-Hartman 				tty_insert_flip_char(tty, 0, TTY_OVERRUN);
14205e5027eSGreg Kroah-Hartman 			}
14305e5027eSGreg Kroah-Hartman 			if (sr & SR_PARITY_ERROR) {
14405e5027eSGreg Kroah-Hartman 				channel->stats.parity_err++;
14505e5027eSGreg Kroah-Hartman 				flag = TTY_PARITY;
14605e5027eSGreg Kroah-Hartman 			}
14705e5027eSGreg Kroah-Hartman 			if (sr & SR_FRAMING_ERROR) {
14805e5027eSGreg Kroah-Hartman 				channel->stats.framing_err++;
14905e5027eSGreg Kroah-Hartman 				flag = TTY_FRAME;
15005e5027eSGreg Kroah-Hartman 			}
15105e5027eSGreg Kroah-Hartman 			if (sr & SR_RECEIVED_BREAK) {
15205e5027eSGreg Kroah-Hartman 				iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
15305e5027eSGreg Kroah-Hartman 				channel->stats.rcv_break++;
15405e5027eSGreg Kroah-Hartman 				flag = TTY_BREAK;
15505e5027eSGreg Kroah-Hartman 			}
15605e5027eSGreg Kroah-Hartman 		}
15705e5027eSGreg Kroah-Hartman 		tty_insert_flip_char(tty, value, flag);
15805e5027eSGreg Kroah-Hartman 
15905e5027eSGreg Kroah-Hartman 		/* Check if there are more characters in RX FIFO
16005e5027eSGreg Kroah-Hartman 		 * If there are more, the isr register for this channel
16105e5027eSGreg Kroah-Hartman 		 * has enabled the RxRDY|FFULL bit.
16205e5027eSGreg Kroah-Hartman 		 */
16305e5027eSGreg Kroah-Hartman 		isr = ioread8(&channel->block_regs->r.isr);
16405e5027eSGreg Kroah-Hartman 		sr = ioread8(&channel->regs->r.sr);
16505e5027eSGreg Kroah-Hartman 	} while (isr & channel->isr_rx_rdy_mask);
16605e5027eSGreg Kroah-Hartman 
16705e5027eSGreg Kroah-Hartman 	tty_flip_buffer_push(tty);
16805e5027eSGreg Kroah-Hartman }
16905e5027eSGreg Kroah-Hartman 
17005e5027eSGreg Kroah-Hartman static void ipoctal_irq_tx(struct ipoctal_channel *channel)
17105e5027eSGreg Kroah-Hartman {
17205e5027eSGreg Kroah-Hartman 	unsigned char value;
17305e5027eSGreg Kroah-Hartman 	unsigned int *pointer_write = &channel->pointer_write;
17405e5027eSGreg Kroah-Hartman 
17569a6b9b1SAlberto Garcia 	if (channel->nb_bytes == 0)
17605e5027eSGreg Kroah-Hartman 		return;
17705e5027eSGreg Kroah-Hartman 
17805e5027eSGreg Kroah-Hartman 	value = channel->tty_port.xmit_buf[*pointer_write];
17905e5027eSGreg Kroah-Hartman 	iowrite8(value, &channel->regs->w.thr);
18005e5027eSGreg Kroah-Hartman 	channel->stats.tx++;
18105e5027eSGreg Kroah-Hartman 	(*pointer_write)++;
18205e5027eSGreg Kroah-Hartman 	*pointer_write = *pointer_write % PAGE_SIZE;
18305e5027eSGreg Kroah-Hartman 	channel->nb_bytes--;
18405e5027eSGreg Kroah-Hartman }
18505e5027eSGreg Kroah-Hartman 
18605e5027eSGreg Kroah-Hartman static void ipoctal_irq_channel(struct ipoctal_channel *channel)
18705e5027eSGreg Kroah-Hartman {
18805e5027eSGreg Kroah-Hartman 	u8 isr, sr;
18905e5027eSGreg Kroah-Hartman 	struct tty_struct *tty;
19005e5027eSGreg Kroah-Hartman 
19105e5027eSGreg Kroah-Hartman 	tty = tty_port_tty_get(&channel->tty_port);
19205e5027eSGreg Kroah-Hartman 	if (!tty)
19305e5027eSGreg Kroah-Hartman 		return;
19405e5027eSGreg Kroah-Hartman 	/* The HW is organized in pair of channels.  See which register we need
19505e5027eSGreg Kroah-Hartman 	 * to read from */
19605e5027eSGreg Kroah-Hartman 	isr = ioread8(&channel->block_regs->r.isr);
19705e5027eSGreg Kroah-Hartman 	sr = ioread8(&channel->regs->r.sr);
19805e5027eSGreg Kroah-Hartman 
1999d01b6f0SSamuel Iglesias Gonsalvez 	if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
2009d01b6f0SSamuel Iglesias Gonsalvez 		iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
20105e5027eSGreg Kroah-Hartman 		/* In case of RS-485, change from TX to RX when finishing TX.
20205e5027eSGreg Kroah-Hartman 		 * Half-duplex. */
2039d01b6f0SSamuel Iglesias Gonsalvez 		if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
20405e5027eSGreg Kroah-Hartman 			iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
20505e5027eSGreg Kroah-Hartman 			iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
2069d01b6f0SSamuel Iglesias Gonsalvez 		}
20705e5027eSGreg Kroah-Hartman 	}
20805e5027eSGreg Kroah-Hartman 
20905e5027eSGreg Kroah-Hartman 	/* RX data */
21005e5027eSGreg Kroah-Hartman 	if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
21105e5027eSGreg Kroah-Hartman 		ipoctal_irq_rx(channel, tty, sr);
21205e5027eSGreg Kroah-Hartman 
21305e5027eSGreg Kroah-Hartman 	/* TX of each character */
21405e5027eSGreg Kroah-Hartman 	if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
21505e5027eSGreg Kroah-Hartman 		ipoctal_irq_tx(channel);
21605e5027eSGreg Kroah-Hartman 
21705e5027eSGreg Kroah-Hartman 	tty_flip_buffer_push(tty);
21805e5027eSGreg Kroah-Hartman 	tty_kref_put(tty);
21905e5027eSGreg Kroah-Hartman }
22005e5027eSGreg Kroah-Hartman 
22105e5027eSGreg Kroah-Hartman static irqreturn_t ipoctal_irq_handler(void *arg)
22205e5027eSGreg Kroah-Hartman {
22305e5027eSGreg Kroah-Hartman 	unsigned int i;
22405e5027eSGreg Kroah-Hartman 	struct ipoctal *ipoctal = (struct ipoctal *) arg;
22505e5027eSGreg Kroah-Hartman 
22605e5027eSGreg Kroah-Hartman 	/* Clear the IPack device interrupt */
22705e5027eSGreg Kroah-Hartman 	readw(ipoctal->int_space + ACK_INT_REQ0);
22805e5027eSGreg Kroah-Hartman 	readw(ipoctal->int_space + ACK_INT_REQ1);
22905e5027eSGreg Kroah-Hartman 
230*21d27ed4SSamuel Iglesias Gonsalvez 	/* Check all channels */
231*21d27ed4SSamuel Iglesias Gonsalvez 	for (i = 0; i < NR_CHANNELS; i++)
232*21d27ed4SSamuel Iglesias Gonsalvez 		ipoctal_irq_channel(&ipoctal->channel[i]);
233*21d27ed4SSamuel Iglesias Gonsalvez 
23405e5027eSGreg Kroah-Hartman 	return IRQ_HANDLED;
23505e5027eSGreg Kroah-Hartman }
23605e5027eSGreg Kroah-Hartman 
23705e5027eSGreg Kroah-Hartman static const struct tty_port_operations ipoctal_tty_port_ops = {
23805e5027eSGreg Kroah-Hartman 	.dtr_rts = NULL,
23905e5027eSGreg Kroah-Hartman 	.activate = ipoctal_port_activate,
24005e5027eSGreg Kroah-Hartman };
24105e5027eSGreg Kroah-Hartman 
24205e5027eSGreg Kroah-Hartman static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
24305e5027eSGreg Kroah-Hartman 			     unsigned int slot)
24405e5027eSGreg Kroah-Hartman {
24505e5027eSGreg Kroah-Hartman 	int res;
24605e5027eSGreg Kroah-Hartman 	int i;
24705e5027eSGreg Kroah-Hartman 	struct tty_driver *tty;
24805e5027eSGreg Kroah-Hartman 	char name[20];
24905e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel;
25005e5027eSGreg Kroah-Hartman 	struct ipack_region *region;
25105e5027eSGreg Kroah-Hartman 	void __iomem *addr;
25205e5027eSGreg Kroah-Hartman 	union scc2698_channel __iomem *chan_regs;
25305e5027eSGreg Kroah-Hartman 	union scc2698_block __iomem *block_regs;
25405e5027eSGreg Kroah-Hartman 
25505e5027eSGreg Kroah-Hartman 	ipoctal->board_id = ipoctal->dev->id_device;
25605e5027eSGreg Kroah-Hartman 
25705e5027eSGreg Kroah-Hartman 	region = &ipoctal->dev->region[IPACK_IO_SPACE];
25805e5027eSGreg Kroah-Hartman 	addr = devm_ioremap_nocache(&ipoctal->dev->dev,
25905e5027eSGreg Kroah-Hartman 				    region->start, region->size);
26005e5027eSGreg Kroah-Hartman 	if (!addr) {
26105e5027eSGreg Kroah-Hartman 		dev_err(&ipoctal->dev->dev,
26205e5027eSGreg Kroah-Hartman 			"Unable to map slot [%d:%d] IO space!\n",
26305e5027eSGreg Kroah-Hartman 			bus_nr, slot);
26405e5027eSGreg Kroah-Hartman 		return -EADDRNOTAVAIL;
26505e5027eSGreg Kroah-Hartman 	}
26605e5027eSGreg Kroah-Hartman 	/* Save the virtual address to access the registers easily */
26705e5027eSGreg Kroah-Hartman 	chan_regs =
26805e5027eSGreg Kroah-Hartman 		(union scc2698_channel __iomem *) addr;
26905e5027eSGreg Kroah-Hartman 	block_regs =
27005e5027eSGreg Kroah-Hartman 		(union scc2698_block __iomem *) addr;
27105e5027eSGreg Kroah-Hartman 
27205e5027eSGreg Kroah-Hartman 	region = &ipoctal->dev->region[IPACK_INT_SPACE];
27305e5027eSGreg Kroah-Hartman 	ipoctal->int_space =
27405e5027eSGreg Kroah-Hartman 		devm_ioremap_nocache(&ipoctal->dev->dev,
27505e5027eSGreg Kroah-Hartman 				     region->start, region->size);
27605e5027eSGreg Kroah-Hartman 	if (!ipoctal->int_space) {
27705e5027eSGreg Kroah-Hartman 		dev_err(&ipoctal->dev->dev,
27805e5027eSGreg Kroah-Hartman 			"Unable to map slot [%d:%d] INT space!\n",
27905e5027eSGreg Kroah-Hartman 			bus_nr, slot);
28005e5027eSGreg Kroah-Hartman 		return -EADDRNOTAVAIL;
28105e5027eSGreg Kroah-Hartman 	}
28205e5027eSGreg Kroah-Hartman 
28305e5027eSGreg Kroah-Hartman 	region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
28405e5027eSGreg Kroah-Hartman 	ipoctal->mem8_space =
28505e5027eSGreg Kroah-Hartman 		devm_ioremap_nocache(&ipoctal->dev->dev,
28605e5027eSGreg Kroah-Hartman 				     region->start, 0x8000);
28705e5027eSGreg Kroah-Hartman 	if (!addr) {
28805e5027eSGreg Kroah-Hartman 		dev_err(&ipoctal->dev->dev,
28905e5027eSGreg Kroah-Hartman 			"Unable to map slot [%d:%d] MEM8 space!\n",
29005e5027eSGreg Kroah-Hartman 			bus_nr, slot);
29105e5027eSGreg Kroah-Hartman 		return -EADDRNOTAVAIL;
29205e5027eSGreg Kroah-Hartman 	}
29305e5027eSGreg Kroah-Hartman 
29405e5027eSGreg Kroah-Hartman 
29505e5027eSGreg Kroah-Hartman 	/* Disable RX and TX before touching anything */
29605e5027eSGreg Kroah-Hartman 	for (i = 0; i < NR_CHANNELS ; i++) {
29705e5027eSGreg Kroah-Hartman 		struct ipoctal_channel *channel = &ipoctal->channel[i];
29805e5027eSGreg Kroah-Hartman 		channel->regs = chan_regs + i;
29905e5027eSGreg Kroah-Hartman 		channel->block_regs = block_regs + (i >> 1);
30005e5027eSGreg Kroah-Hartman 		channel->board_id = ipoctal->board_id;
30105e5027eSGreg Kroah-Hartman 		if (i & 1) {
30205e5027eSGreg Kroah-Hartman 			channel->isr_tx_rdy_mask = ISR_TxRDY_B;
30305e5027eSGreg Kroah-Hartman 			channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
30405e5027eSGreg Kroah-Hartman 		} else {
30505e5027eSGreg Kroah-Hartman 			channel->isr_tx_rdy_mask = ISR_TxRDY_A;
30605e5027eSGreg Kroah-Hartman 			channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
30705e5027eSGreg Kroah-Hartman 		}
30805e5027eSGreg Kroah-Hartman 
30905e5027eSGreg Kroah-Hartman 		iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
31005e5027eSGreg Kroah-Hartman 		iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
31105e5027eSGreg Kroah-Hartman 		iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
31205e5027eSGreg Kroah-Hartman 		iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
31305e5027eSGreg Kroah-Hartman 			 &channel->regs->w.mr); /* mr1 */
31405e5027eSGreg Kroah-Hartman 		iowrite8(0, &channel->regs->w.mr); /* mr2 */
31505e5027eSGreg Kroah-Hartman 		iowrite8(TX_CLK_9600  | RX_CLK_9600, &channel->regs->w.csr);
31605e5027eSGreg Kroah-Hartman 	}
31705e5027eSGreg Kroah-Hartman 
31805e5027eSGreg Kroah-Hartman 	for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
31905e5027eSGreg Kroah-Hartman 		iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
32005e5027eSGreg Kroah-Hartman 		iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
32105e5027eSGreg Kroah-Hartman 			 &block_regs[i].w.opcr);
32205e5027eSGreg Kroah-Hartman 		iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
32305e5027eSGreg Kroah-Hartman 			 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
32405e5027eSGreg Kroah-Hartman 			 &block_regs[i].w.imr);
32505e5027eSGreg Kroah-Hartman 	}
32605e5027eSGreg Kroah-Hartman 
32705e5027eSGreg Kroah-Hartman 	/*
32805e5027eSGreg Kroah-Hartman 	 * IP-OCTAL has different addresses to copy its IRQ vector.
32905e5027eSGreg Kroah-Hartman 	 * Depending of the carrier these addresses are accesible or not.
33005e5027eSGreg Kroah-Hartman 	 * More info in the datasheet.
33105e5027eSGreg Kroah-Hartman 	 */
33205e5027eSGreg Kroah-Hartman 	ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
33305e5027eSGreg Kroah-Hartman 				       ipoctal_irq_handler, ipoctal);
33405e5027eSGreg Kroah-Hartman 	/* Dummy write */
33505e5027eSGreg Kroah-Hartman 	iowrite8(1, ipoctal->mem8_space + 1);
33605e5027eSGreg Kroah-Hartman 
33705e5027eSGreg Kroah-Hartman 	/* Register the TTY device */
33805e5027eSGreg Kroah-Hartman 
33905e5027eSGreg Kroah-Hartman 	/* Each IP-OCTAL channel is a TTY port */
34005e5027eSGreg Kroah-Hartman 	tty = alloc_tty_driver(NR_CHANNELS);
34105e5027eSGreg Kroah-Hartman 
34205e5027eSGreg Kroah-Hartman 	if (!tty)
34305e5027eSGreg Kroah-Hartman 		return -ENOMEM;
34405e5027eSGreg Kroah-Hartman 
34505e5027eSGreg Kroah-Hartman 	/* Fill struct tty_driver with ipoctal data */
34605e5027eSGreg Kroah-Hartman 	tty->owner = THIS_MODULE;
34705e5027eSGreg Kroah-Hartman 	tty->driver_name = KBUILD_MODNAME;
34805e5027eSGreg Kroah-Hartman 	sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
34905e5027eSGreg Kroah-Hartman 	tty->name = name;
35005e5027eSGreg Kroah-Hartman 	tty->major = 0;
35105e5027eSGreg Kroah-Hartman 
35205e5027eSGreg Kroah-Hartman 	tty->minor_start = 0;
35305e5027eSGreg Kroah-Hartman 	tty->type = TTY_DRIVER_TYPE_SERIAL;
35405e5027eSGreg Kroah-Hartman 	tty->subtype = SERIAL_TYPE_NORMAL;
35505e5027eSGreg Kroah-Hartman 	tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
35605e5027eSGreg Kroah-Hartman 	tty->init_termios = tty_std_termios;
35705e5027eSGreg Kroah-Hartman 	tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
35805e5027eSGreg Kroah-Hartman 	tty->init_termios.c_ispeed = 9600;
35905e5027eSGreg Kroah-Hartman 	tty->init_termios.c_ospeed = 9600;
36005e5027eSGreg Kroah-Hartman 
36105e5027eSGreg Kroah-Hartman 	tty_set_operations(tty, &ipoctal_fops);
36205e5027eSGreg Kroah-Hartman 	res = tty_register_driver(tty);
36305e5027eSGreg Kroah-Hartman 	if (res) {
36405e5027eSGreg Kroah-Hartman 		dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
36505e5027eSGreg Kroah-Hartman 		put_tty_driver(tty);
36605e5027eSGreg Kroah-Hartman 		return res;
36705e5027eSGreg Kroah-Hartman 	}
36805e5027eSGreg Kroah-Hartman 
36905e5027eSGreg Kroah-Hartman 	/* Save struct tty_driver for use it when uninstalling the device */
37005e5027eSGreg Kroah-Hartman 	ipoctal->tty_drv = tty;
37105e5027eSGreg Kroah-Hartman 
37205e5027eSGreg Kroah-Hartman 	for (i = 0; i < NR_CHANNELS; i++) {
37305e5027eSGreg Kroah-Hartman 		struct device *tty_dev;
37405e5027eSGreg Kroah-Hartman 
37505e5027eSGreg Kroah-Hartman 		channel = &ipoctal->channel[i];
37605e5027eSGreg Kroah-Hartman 		tty_port_init(&channel->tty_port);
37705e5027eSGreg Kroah-Hartman 		tty_port_alloc_xmit_buf(&channel->tty_port);
37805e5027eSGreg Kroah-Hartman 		channel->tty_port.ops = &ipoctal_tty_port_ops;
37905e5027eSGreg Kroah-Hartman 
38005e5027eSGreg Kroah-Hartman 		ipoctal_reset_stats(&channel->stats);
38105e5027eSGreg Kroah-Hartman 		channel->nb_bytes = 0;
38205e5027eSGreg Kroah-Hartman 		spin_lock_init(&channel->lock);
38305e5027eSGreg Kroah-Hartman 		channel->pointer_read = 0;
38405e5027eSGreg Kroah-Hartman 		channel->pointer_write = 0;
38505e5027eSGreg Kroah-Hartman 		tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
38605e5027eSGreg Kroah-Hartman 		if (IS_ERR(tty_dev)) {
38705e5027eSGreg Kroah-Hartman 			dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
388c6bd5bccSLinus Torvalds 			tty_port_destroy(&channel->tty_port);
38905e5027eSGreg Kroah-Hartman 			continue;
39005e5027eSGreg Kroah-Hartman 		}
39105e5027eSGreg Kroah-Hartman 		dev_set_drvdata(tty_dev, channel);
39205e5027eSGreg Kroah-Hartman 	}
39305e5027eSGreg Kroah-Hartman 
39405e5027eSGreg Kroah-Hartman 	return 0;
39505e5027eSGreg Kroah-Hartman }
39605e5027eSGreg Kroah-Hartman 
39705e5027eSGreg Kroah-Hartman static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
39805e5027eSGreg Kroah-Hartman 					    const unsigned char *buf,
39905e5027eSGreg Kroah-Hartman 					    int count)
40005e5027eSGreg Kroah-Hartman {
40105e5027eSGreg Kroah-Hartman 	unsigned long flags;
40205e5027eSGreg Kroah-Hartman 	int i;
40305e5027eSGreg Kroah-Hartman 	unsigned int *pointer_read = &channel->pointer_read;
40405e5027eSGreg Kroah-Hartman 
40505e5027eSGreg Kroah-Hartman 	/* Copy the bytes from the user buffer to the internal one */
40605e5027eSGreg Kroah-Hartman 	for (i = 0; i < count; i++) {
40705e5027eSGreg Kroah-Hartman 		if (i <= (PAGE_SIZE - channel->nb_bytes)) {
40805e5027eSGreg Kroah-Hartman 			spin_lock_irqsave(&channel->lock, flags);
40905e5027eSGreg Kroah-Hartman 			channel->tty_port.xmit_buf[*pointer_read] = buf[i];
41005e5027eSGreg Kroah-Hartman 			*pointer_read = (*pointer_read + 1) % PAGE_SIZE;
41105e5027eSGreg Kroah-Hartman 			channel->nb_bytes++;
41205e5027eSGreg Kroah-Hartman 			spin_unlock_irqrestore(&channel->lock, flags);
41305e5027eSGreg Kroah-Hartman 		} else {
41405e5027eSGreg Kroah-Hartman 			break;
41505e5027eSGreg Kroah-Hartman 		}
41605e5027eSGreg Kroah-Hartman 	}
41705e5027eSGreg Kroah-Hartman 	return i;
41805e5027eSGreg Kroah-Hartman }
41905e5027eSGreg Kroah-Hartman 
42005e5027eSGreg Kroah-Hartman static int ipoctal_write_tty(struct tty_struct *tty,
42105e5027eSGreg Kroah-Hartman 			     const unsigned char *buf, int count)
42205e5027eSGreg Kroah-Hartman {
42305e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
42405e5027eSGreg Kroah-Hartman 	unsigned int char_copied;
42505e5027eSGreg Kroah-Hartman 
42605e5027eSGreg Kroah-Hartman 	char_copied = ipoctal_copy_write_buffer(channel, buf, count);
42705e5027eSGreg Kroah-Hartman 
42805e5027eSGreg Kroah-Hartman 	/* As the IP-OCTAL 485 only supports half duplex, do it manually */
42905e5027eSGreg Kroah-Hartman 	if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
43005e5027eSGreg Kroah-Hartman 		iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
43105e5027eSGreg Kroah-Hartman 		iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
43205e5027eSGreg Kroah-Hartman 	}
43305e5027eSGreg Kroah-Hartman 
43405e5027eSGreg Kroah-Hartman 	/*
43505e5027eSGreg Kroah-Hartman 	 * Send a packet and then disable TX to avoid failure after several send
43605e5027eSGreg Kroah-Hartman 	 * operations
43705e5027eSGreg Kroah-Hartman 	 */
43805e5027eSGreg Kroah-Hartman 	iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
43905e5027eSGreg Kroah-Hartman 	return char_copied;
44005e5027eSGreg Kroah-Hartman }
44105e5027eSGreg Kroah-Hartman 
44205e5027eSGreg Kroah-Hartman static int ipoctal_write_room(struct tty_struct *tty)
44305e5027eSGreg Kroah-Hartman {
44405e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
44505e5027eSGreg Kroah-Hartman 
44605e5027eSGreg Kroah-Hartman 	return PAGE_SIZE - channel->nb_bytes;
44705e5027eSGreg Kroah-Hartman }
44805e5027eSGreg Kroah-Hartman 
44905e5027eSGreg Kroah-Hartman static int ipoctal_chars_in_buffer(struct tty_struct *tty)
45005e5027eSGreg Kroah-Hartman {
45105e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
45205e5027eSGreg Kroah-Hartman 
45305e5027eSGreg Kroah-Hartman 	return channel->nb_bytes;
45405e5027eSGreg Kroah-Hartman }
45505e5027eSGreg Kroah-Hartman 
45605e5027eSGreg Kroah-Hartman static void ipoctal_set_termios(struct tty_struct *tty,
45705e5027eSGreg Kroah-Hartman 				struct ktermios *old_termios)
45805e5027eSGreg Kroah-Hartman {
45905e5027eSGreg Kroah-Hartman 	unsigned int cflag;
46005e5027eSGreg Kroah-Hartman 	unsigned char mr1 = 0;
46105e5027eSGreg Kroah-Hartman 	unsigned char mr2 = 0;
46205e5027eSGreg Kroah-Hartman 	unsigned char csr = 0;
46305e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
46405e5027eSGreg Kroah-Hartman 	speed_t baud;
46505e5027eSGreg Kroah-Hartman 
46605e5027eSGreg Kroah-Hartman 	cflag = tty->termios.c_cflag;
46705e5027eSGreg Kroah-Hartman 
46805e5027eSGreg Kroah-Hartman 	/* Disable and reset everything before change the setup */
46905e5027eSGreg Kroah-Hartman 	iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
47005e5027eSGreg Kroah-Hartman 	iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
47105e5027eSGreg Kroah-Hartman 	iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
47205e5027eSGreg Kroah-Hartman 	iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
47305e5027eSGreg Kroah-Hartman 	iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
47405e5027eSGreg Kroah-Hartman 
47505e5027eSGreg Kroah-Hartman 	/* Set Bits per chars */
47605e5027eSGreg Kroah-Hartman 	switch (cflag & CSIZE) {
47705e5027eSGreg Kroah-Hartman 	case CS6:
47805e5027eSGreg Kroah-Hartman 		mr1 |= MR1_CHRL_6_BITS;
47905e5027eSGreg Kroah-Hartman 		break;
48005e5027eSGreg Kroah-Hartman 	case CS7:
48105e5027eSGreg Kroah-Hartman 		mr1 |= MR1_CHRL_7_BITS;
48205e5027eSGreg Kroah-Hartman 		break;
48305e5027eSGreg Kroah-Hartman 	case CS8:
48405e5027eSGreg Kroah-Hartman 	default:
48505e5027eSGreg Kroah-Hartman 		mr1 |= MR1_CHRL_8_BITS;
48605e5027eSGreg Kroah-Hartman 		/* By default, select CS8 */
48705e5027eSGreg Kroah-Hartman 		tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
48805e5027eSGreg Kroah-Hartman 		break;
48905e5027eSGreg Kroah-Hartman 	}
49005e5027eSGreg Kroah-Hartman 
49105e5027eSGreg Kroah-Hartman 	/* Set Parity */
49205e5027eSGreg Kroah-Hartman 	if (cflag & PARENB)
49305e5027eSGreg Kroah-Hartman 		if (cflag & PARODD)
49405e5027eSGreg Kroah-Hartman 			mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
49505e5027eSGreg Kroah-Hartman 		else
49605e5027eSGreg Kroah-Hartman 			mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
49705e5027eSGreg Kroah-Hartman 	else
49805e5027eSGreg Kroah-Hartman 		mr1 |= MR1_PARITY_OFF;
49905e5027eSGreg Kroah-Hartman 
50005e5027eSGreg Kroah-Hartman 	/* Mark or space parity is not supported */
50105e5027eSGreg Kroah-Hartman 	tty->termios.c_cflag &= ~CMSPAR;
50205e5027eSGreg Kroah-Hartman 
50305e5027eSGreg Kroah-Hartman 	/* Set stop bits */
50405e5027eSGreg Kroah-Hartman 	if (cflag & CSTOPB)
50505e5027eSGreg Kroah-Hartman 		mr2 |= MR2_STOP_BITS_LENGTH_2;
50605e5027eSGreg Kroah-Hartman 	else
50705e5027eSGreg Kroah-Hartman 		mr2 |= MR2_STOP_BITS_LENGTH_1;
50805e5027eSGreg Kroah-Hartman 
50905e5027eSGreg Kroah-Hartman 	/* Set the flow control */
51005e5027eSGreg Kroah-Hartman 	switch (channel->board_id) {
51105e5027eSGreg Kroah-Hartman 	case IPACK1_DEVICE_ID_SBS_OCTAL_232:
51205e5027eSGreg Kroah-Hartman 		if (cflag & CRTSCTS) {
51305e5027eSGreg Kroah-Hartman 			mr1 |= MR1_RxRTS_CONTROL_ON;
51405e5027eSGreg Kroah-Hartman 			mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
51505e5027eSGreg Kroah-Hartman 		} else {
51605e5027eSGreg Kroah-Hartman 			mr1 |= MR1_RxRTS_CONTROL_OFF;
51705e5027eSGreg Kroah-Hartman 			mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
51805e5027eSGreg Kroah-Hartman 		}
51905e5027eSGreg Kroah-Hartman 		break;
52005e5027eSGreg Kroah-Hartman 	case IPACK1_DEVICE_ID_SBS_OCTAL_422:
52105e5027eSGreg Kroah-Hartman 		mr1 |= MR1_RxRTS_CONTROL_OFF;
52205e5027eSGreg Kroah-Hartman 		mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
52305e5027eSGreg Kroah-Hartman 		break;
52405e5027eSGreg Kroah-Hartman 	case IPACK1_DEVICE_ID_SBS_OCTAL_485:
52505e5027eSGreg Kroah-Hartman 		mr1 |= MR1_RxRTS_CONTROL_OFF;
52605e5027eSGreg Kroah-Hartman 		mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
52705e5027eSGreg Kroah-Hartman 		break;
52805e5027eSGreg Kroah-Hartman 	default:
52905e5027eSGreg Kroah-Hartman 		return;
53005e5027eSGreg Kroah-Hartman 		break;
53105e5027eSGreg Kroah-Hartman 	}
53205e5027eSGreg Kroah-Hartman 
53305e5027eSGreg Kroah-Hartman 	baud = tty_get_baud_rate(tty);
53405e5027eSGreg Kroah-Hartman 	tty_termios_encode_baud_rate(&tty->termios, baud, baud);
53505e5027eSGreg Kroah-Hartman 
53605e5027eSGreg Kroah-Hartman 	/* Set baud rate */
53705e5027eSGreg Kroah-Hartman 	switch (baud) {
53805e5027eSGreg Kroah-Hartman 	case 75:
53905e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_75 | RX_CLK_75;
54005e5027eSGreg Kroah-Hartman 		break;
54105e5027eSGreg Kroah-Hartman 	case 110:
54205e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_110 | RX_CLK_110;
54305e5027eSGreg Kroah-Hartman 		break;
54405e5027eSGreg Kroah-Hartman 	case 150:
54505e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_150 | RX_CLK_150;
54605e5027eSGreg Kroah-Hartman 		break;
54705e5027eSGreg Kroah-Hartman 	case 300:
54805e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_300 | RX_CLK_300;
54905e5027eSGreg Kroah-Hartman 		break;
55005e5027eSGreg Kroah-Hartman 	case 600:
55105e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_600 | RX_CLK_600;
55205e5027eSGreg Kroah-Hartman 		break;
55305e5027eSGreg Kroah-Hartman 	case 1200:
55405e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_1200 | RX_CLK_1200;
55505e5027eSGreg Kroah-Hartman 		break;
55605e5027eSGreg Kroah-Hartman 	case 1800:
55705e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_1800 | RX_CLK_1800;
55805e5027eSGreg Kroah-Hartman 		break;
55905e5027eSGreg Kroah-Hartman 	case 2000:
56005e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_2000 | RX_CLK_2000;
56105e5027eSGreg Kroah-Hartman 		break;
56205e5027eSGreg Kroah-Hartman 	case 2400:
56305e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_2400 | RX_CLK_2400;
56405e5027eSGreg Kroah-Hartman 		break;
56505e5027eSGreg Kroah-Hartman 	case 4800:
56605e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_4800  | RX_CLK_4800;
56705e5027eSGreg Kroah-Hartman 		break;
56805e5027eSGreg Kroah-Hartman 	case 9600:
56905e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_9600  | RX_CLK_9600;
57005e5027eSGreg Kroah-Hartman 		break;
57105e5027eSGreg Kroah-Hartman 	case 19200:
57205e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_19200 | RX_CLK_19200;
57305e5027eSGreg Kroah-Hartman 		break;
57405e5027eSGreg Kroah-Hartman 	case 38400:
57505e5027eSGreg Kroah-Hartman 	default:
57605e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_38400 | RX_CLK_38400;
57705e5027eSGreg Kroah-Hartman 		/* In case of default, we establish 38400 bps */
57805e5027eSGreg Kroah-Hartman 		tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
57905e5027eSGreg Kroah-Hartman 		break;
58005e5027eSGreg Kroah-Hartman 	}
58105e5027eSGreg Kroah-Hartman 
58205e5027eSGreg Kroah-Hartman 	mr1 |= MR1_ERROR_CHAR;
58305e5027eSGreg Kroah-Hartman 	mr1 |= MR1_RxINT_RxRDY;
58405e5027eSGreg Kroah-Hartman 
58505e5027eSGreg Kroah-Hartman 	/* Write the control registers */
58605e5027eSGreg Kroah-Hartman 	iowrite8(mr1, &channel->regs->w.mr);
58705e5027eSGreg Kroah-Hartman 	iowrite8(mr2, &channel->regs->w.mr);
58805e5027eSGreg Kroah-Hartman 	iowrite8(csr, &channel->regs->w.csr);
58905e5027eSGreg Kroah-Hartman 
59005e5027eSGreg Kroah-Hartman 	/* Enable again the RX */
59105e5027eSGreg Kroah-Hartman 	iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
59205e5027eSGreg Kroah-Hartman }
59305e5027eSGreg Kroah-Hartman 
59405e5027eSGreg Kroah-Hartman static void ipoctal_hangup(struct tty_struct *tty)
59505e5027eSGreg Kroah-Hartman {
59605e5027eSGreg Kroah-Hartman 	unsigned long flags;
59705e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
59805e5027eSGreg Kroah-Hartman 
59905e5027eSGreg Kroah-Hartman 	if (channel == NULL)
60005e5027eSGreg Kroah-Hartman 		return;
60105e5027eSGreg Kroah-Hartman 
60205e5027eSGreg Kroah-Hartman 	spin_lock_irqsave(&channel->lock, flags);
60305e5027eSGreg Kroah-Hartman 	channel->nb_bytes = 0;
60405e5027eSGreg Kroah-Hartman 	channel->pointer_read = 0;
60505e5027eSGreg Kroah-Hartman 	channel->pointer_write = 0;
60605e5027eSGreg Kroah-Hartman 	spin_unlock_irqrestore(&channel->lock, flags);
60705e5027eSGreg Kroah-Hartman 
60805e5027eSGreg Kroah-Hartman 	tty_port_hangup(&channel->tty_port);
60905e5027eSGreg Kroah-Hartman 
61005e5027eSGreg Kroah-Hartman 	iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
61105e5027eSGreg Kroah-Hartman 	iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
61205e5027eSGreg Kroah-Hartman 	iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
61305e5027eSGreg Kroah-Hartman 	iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
61405e5027eSGreg Kroah-Hartman 	iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
61505e5027eSGreg Kroah-Hartman 
61605e5027eSGreg Kroah-Hartman 	clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
61705e5027eSGreg Kroah-Hartman 	wake_up_interruptible(&channel->tty_port.open_wait);
61805e5027eSGreg Kroah-Hartman }
61905e5027eSGreg Kroah-Hartman 
62005e5027eSGreg Kroah-Hartman static const struct tty_operations ipoctal_fops = {
62105e5027eSGreg Kroah-Hartman 	.ioctl =		NULL,
62205e5027eSGreg Kroah-Hartman 	.open =			ipoctal_open,
62305e5027eSGreg Kroah-Hartman 	.close =		ipoctal_close,
62405e5027eSGreg Kroah-Hartman 	.write =		ipoctal_write_tty,
62505e5027eSGreg Kroah-Hartman 	.set_termios =		ipoctal_set_termios,
62605e5027eSGreg Kroah-Hartman 	.write_room =		ipoctal_write_room,
62705e5027eSGreg Kroah-Hartman 	.chars_in_buffer =	ipoctal_chars_in_buffer,
62805e5027eSGreg Kroah-Hartman 	.get_icount =		ipoctal_get_icount,
62905e5027eSGreg Kroah-Hartman 	.hangup =		ipoctal_hangup,
63005e5027eSGreg Kroah-Hartman };
63105e5027eSGreg Kroah-Hartman 
63205e5027eSGreg Kroah-Hartman static int ipoctal_probe(struct ipack_device *dev)
63305e5027eSGreg Kroah-Hartman {
63405e5027eSGreg Kroah-Hartman 	int res;
63505e5027eSGreg Kroah-Hartman 	struct ipoctal *ipoctal;
63605e5027eSGreg Kroah-Hartman 
63705e5027eSGreg Kroah-Hartman 	ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
63805e5027eSGreg Kroah-Hartman 	if (ipoctal == NULL)
63905e5027eSGreg Kroah-Hartman 		return -ENOMEM;
64005e5027eSGreg Kroah-Hartman 
64105e5027eSGreg Kroah-Hartman 	ipoctal->dev = dev;
64205e5027eSGreg Kroah-Hartman 	res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
64305e5027eSGreg Kroah-Hartman 	if (res)
64405e5027eSGreg Kroah-Hartman 		goto out_uninst;
64505e5027eSGreg Kroah-Hartman 
64605e5027eSGreg Kroah-Hartman 	dev_set_drvdata(&dev->dev, ipoctal);
64705e5027eSGreg Kroah-Hartman 	return 0;
64805e5027eSGreg Kroah-Hartman 
64905e5027eSGreg Kroah-Hartman out_uninst:
65005e5027eSGreg Kroah-Hartman 	kfree(ipoctal);
65105e5027eSGreg Kroah-Hartman 	return res;
65205e5027eSGreg Kroah-Hartman }
65305e5027eSGreg Kroah-Hartman 
65405e5027eSGreg Kroah-Hartman static void __ipoctal_remove(struct ipoctal *ipoctal)
65505e5027eSGreg Kroah-Hartman {
65605e5027eSGreg Kroah-Hartman 	int i;
65705e5027eSGreg Kroah-Hartman 
65805e5027eSGreg Kroah-Hartman 	ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
65905e5027eSGreg Kroah-Hartman 
66005e5027eSGreg Kroah-Hartman 	for (i = 0; i < NR_CHANNELS; i++) {
66105e5027eSGreg Kroah-Hartman 		struct ipoctal_channel *channel = &ipoctal->channel[i];
66205e5027eSGreg Kroah-Hartman 		tty_unregister_device(ipoctal->tty_drv, i);
66305e5027eSGreg Kroah-Hartman 		tty_port_free_xmit_buf(&channel->tty_port);
664c6bd5bccSLinus Torvalds 		tty_port_destroy(&channel->tty_port);
66505e5027eSGreg Kroah-Hartman 	}
66605e5027eSGreg Kroah-Hartman 
66705e5027eSGreg Kroah-Hartman 	tty_unregister_driver(ipoctal->tty_drv);
66805e5027eSGreg Kroah-Hartman 	put_tty_driver(ipoctal->tty_drv);
66905e5027eSGreg Kroah-Hartman 	kfree(ipoctal);
67005e5027eSGreg Kroah-Hartman }
67105e5027eSGreg Kroah-Hartman 
67205e5027eSGreg Kroah-Hartman static void ipoctal_remove(struct ipack_device *idev)
67305e5027eSGreg Kroah-Hartman {
67405e5027eSGreg Kroah-Hartman 	__ipoctal_remove(dev_get_drvdata(&idev->dev));
67505e5027eSGreg Kroah-Hartman }
67605e5027eSGreg Kroah-Hartman 
67705e5027eSGreg Kroah-Hartman static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
67805e5027eSGreg Kroah-Hartman 	{ IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
67905e5027eSGreg Kroah-Hartman 			IPACK1_DEVICE_ID_SBS_OCTAL_232) },
68005e5027eSGreg Kroah-Hartman 	{ IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
68105e5027eSGreg Kroah-Hartman 			IPACK1_DEVICE_ID_SBS_OCTAL_422) },
68205e5027eSGreg Kroah-Hartman 	{ IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
68305e5027eSGreg Kroah-Hartman 			IPACK1_DEVICE_ID_SBS_OCTAL_485) },
68405e5027eSGreg Kroah-Hartman 	{ 0, },
68505e5027eSGreg Kroah-Hartman };
68605e5027eSGreg Kroah-Hartman 
68705e5027eSGreg Kroah-Hartman MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
68805e5027eSGreg Kroah-Hartman 
68905e5027eSGreg Kroah-Hartman static const struct ipack_driver_ops ipoctal_drv_ops = {
69005e5027eSGreg Kroah-Hartman 	.probe  = ipoctal_probe,
69105e5027eSGreg Kroah-Hartman 	.remove = ipoctal_remove,
69205e5027eSGreg Kroah-Hartman };
69305e5027eSGreg Kroah-Hartman 
69405e5027eSGreg Kroah-Hartman static struct ipack_driver driver = {
69505e5027eSGreg Kroah-Hartman 	.ops      = &ipoctal_drv_ops,
69605e5027eSGreg Kroah-Hartman 	.id_table = ipoctal_ids,
69705e5027eSGreg Kroah-Hartman };
69805e5027eSGreg Kroah-Hartman 
69905e5027eSGreg Kroah-Hartman static int __init ipoctal_init(void)
70005e5027eSGreg Kroah-Hartman {
70105e5027eSGreg Kroah-Hartman 	return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
70205e5027eSGreg Kroah-Hartman }
70305e5027eSGreg Kroah-Hartman 
70405e5027eSGreg Kroah-Hartman static void __exit ipoctal_exit(void)
70505e5027eSGreg Kroah-Hartman {
70605e5027eSGreg Kroah-Hartman 	ipack_driver_unregister(&driver);
70705e5027eSGreg Kroah-Hartman }
70805e5027eSGreg Kroah-Hartman 
70905e5027eSGreg Kroah-Hartman MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
71005e5027eSGreg Kroah-Hartman MODULE_LICENSE("GPL");
71105e5027eSGreg Kroah-Hartman 
71205e5027eSGreg Kroah-Hartman module_init(ipoctal_init);
71305e5027eSGreg Kroah-Hartman module_exit(ipoctal_exit);
714