xref: /openbmc/linux/drivers/ipack/devices/ipoctal.c (revision fff4ef17a9400fcd276b5c3a00ce5793f6c465e6)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
205e5027eSGreg Kroah-Hartman /**
305e5027eSGreg Kroah-Hartman  * ipoctal.c
405e5027eSGreg Kroah-Hartman  *
505e5027eSGreg Kroah-Hartman  * driver for the GE IP-OCTAL boards
605e5027eSGreg Kroah-Hartman  *
705e5027eSGreg Kroah-Hartman  * Copyright (C) 2009-2012 CERN (www.cern.ch)
805e5027eSGreg Kroah-Hartman  * Author: Nicolas Serafini, EIC2 SA
905e5027eSGreg Kroah-Hartman  * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
1005e5027eSGreg Kroah-Hartman  */
1105e5027eSGreg Kroah-Hartman 
1205e5027eSGreg Kroah-Hartman #include <linux/device.h>
1305e5027eSGreg Kroah-Hartman #include <linux/module.h>
1405e5027eSGreg Kroah-Hartman #include <linux/interrupt.h>
1505e5027eSGreg Kroah-Hartman #include <linux/sched.h>
1605e5027eSGreg Kroah-Hartman #include <linux/tty.h>
1705e5027eSGreg Kroah-Hartman #include <linux/serial.h>
1805e5027eSGreg Kroah-Hartman #include <linux/tty_flip.h>
1905e5027eSGreg Kroah-Hartman #include <linux/slab.h>
2005e5027eSGreg Kroah-Hartman #include <linux/io.h>
217dbce021SSamuel Iglesias Gonsalvez #include <linux/ipack.h>
2205e5027eSGreg Kroah-Hartman #include "ipoctal.h"
2305e5027eSGreg Kroah-Hartman #include "scc2698.h"
2405e5027eSGreg Kroah-Hartman 
2505e5027eSGreg Kroah-Hartman #define IP_OCTAL_ID_SPACE_VECTOR    0x41
2605e5027eSGreg Kroah-Hartman #define IP_OCTAL_NB_BLOCKS          4
2705e5027eSGreg Kroah-Hartman 
2805e5027eSGreg Kroah-Hartman static const struct tty_operations ipoctal_fops;
2905e5027eSGreg Kroah-Hartman 
3005e5027eSGreg Kroah-Hartman struct ipoctal_channel {
3105e5027eSGreg Kroah-Hartman 	struct ipoctal_stats		stats;
3205e5027eSGreg Kroah-Hartman 	unsigned int			nb_bytes;
3305e5027eSGreg Kroah-Hartman 	wait_queue_head_t		queue;
3405e5027eSGreg Kroah-Hartman 	spinlock_t			lock;
3505e5027eSGreg Kroah-Hartman 	unsigned int			pointer_read;
3605e5027eSGreg Kroah-Hartman 	unsigned int			pointer_write;
3705e5027eSGreg Kroah-Hartman 	struct tty_port			tty_port;
3805e5027eSGreg Kroah-Hartman 	union scc2698_channel __iomem	*regs;
3905e5027eSGreg Kroah-Hartman 	union scc2698_block __iomem	*block_regs;
4005e5027eSGreg Kroah-Hartman 	unsigned int			board_id;
4105e5027eSGreg Kroah-Hartman 	u8				isr_rx_rdy_mask;
4205e5027eSGreg Kroah-Hartman 	u8				isr_tx_rdy_mask;
43b0d17fbdSSamuel Iglesias Gonsalvez 	unsigned int			rx_enable;
4405e5027eSGreg Kroah-Hartman };
4505e5027eSGreg Kroah-Hartman 
4605e5027eSGreg Kroah-Hartman struct ipoctal {
4705e5027eSGreg Kroah-Hartman 	struct ipack_device		*dev;
4805e5027eSGreg Kroah-Hartman 	unsigned int			board_id;
4905e5027eSGreg Kroah-Hartman 	struct ipoctal_channel		channel[NR_CHANNELS];
5005e5027eSGreg Kroah-Hartman 	struct tty_driver		*tty_drv;
5105e5027eSGreg Kroah-Hartman 	u8 __iomem			*mem8_space;
5205e5027eSGreg Kroah-Hartman 	u8 __iomem			*int_space;
5305e5027eSGreg Kroah-Hartman };
5405e5027eSGreg Kroah-Hartman 
5582a82340SFederico Vaga static inline struct ipoctal *chan_to_ipoctal(struct ipoctal_channel *chan,
5682a82340SFederico Vaga 					      unsigned int index)
5782a82340SFederico Vaga {
5882a82340SFederico Vaga 	return container_of(chan, struct ipoctal, channel[index]);
5982a82340SFederico Vaga }
6082a82340SFederico Vaga 
6178f22bc2SFederico Vaga static void ipoctal_reset_channel(struct ipoctal_channel *channel)
6278f22bc2SFederico Vaga {
6378f22bc2SFederico Vaga 	iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
6478f22bc2SFederico Vaga 	channel->rx_enable = 0;
6578f22bc2SFederico Vaga 	iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
6678f22bc2SFederico Vaga 	iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
6778f22bc2SFederico Vaga 	iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
6878f22bc2SFederico Vaga 	iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
6978f22bc2SFederico Vaga }
7078f22bc2SFederico Vaga 
7105e5027eSGreg Kroah-Hartman static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
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 
77a3882b78SSamuel Iglesias Gonsalvez 	/*
78a3882b78SSamuel Iglesias Gonsalvez 	 * Enable RX. TX will be enabled when
79a3882b78SSamuel Iglesias Gonsalvez 	 * there is something to send
80a3882b78SSamuel Iglesias Gonsalvez 	 */
8105e5027eSGreg Kroah-Hartman 	iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
82b0d17fbdSSamuel Iglesias Gonsalvez 	channel->rx_enable = 1;
8305e5027eSGreg Kroah-Hartman 	return 0;
8405e5027eSGreg Kroah-Hartman }
8505e5027eSGreg Kroah-Hartman 
8605e5027eSGreg Kroah-Hartman static int ipoctal_open(struct tty_struct *tty, struct file *file)
8705e5027eSGreg Kroah-Hartman {
8882a82340SFederico Vaga 	struct ipoctal_channel *channel = dev_get_drvdata(tty->dev);
8982a82340SFederico Vaga 	struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
9082a82340SFederico Vaga 	int err;
9105e5027eSGreg Kroah-Hartman 
9205e5027eSGreg Kroah-Hartman 	tty->driver_data = channel;
9305e5027eSGreg Kroah-Hartman 
9482a82340SFederico Vaga 	if (!ipack_get_carrier(ipoctal->dev))
9582a82340SFederico Vaga 		return -EBUSY;
9682a82340SFederico Vaga 
9782a82340SFederico Vaga 	err = tty_port_open(&channel->tty_port, tty, file);
9882a82340SFederico Vaga 	if (err)
9982a82340SFederico Vaga 		ipack_put_carrier(ipoctal->dev);
10082a82340SFederico Vaga 
10182a82340SFederico Vaga 	return err;
10205e5027eSGreg Kroah-Hartman }
10305e5027eSGreg Kroah-Hartman 
10405e5027eSGreg Kroah-Hartman static void ipoctal_reset_stats(struct ipoctal_stats *stats)
10505e5027eSGreg Kroah-Hartman {
10605e5027eSGreg Kroah-Hartman 	stats->tx = 0;
10705e5027eSGreg Kroah-Hartman 	stats->rx = 0;
10805e5027eSGreg Kroah-Hartman 	stats->rcv_break = 0;
10905e5027eSGreg Kroah-Hartman 	stats->framing_err = 0;
11005e5027eSGreg Kroah-Hartman 	stats->overrun_err = 0;
11105e5027eSGreg Kroah-Hartman 	stats->parity_err = 0;
11205e5027eSGreg Kroah-Hartman }
11305e5027eSGreg Kroah-Hartman 
11405e5027eSGreg Kroah-Hartman static void ipoctal_free_channel(struct ipoctal_channel *channel)
11505e5027eSGreg Kroah-Hartman {
11605e5027eSGreg Kroah-Hartman 	ipoctal_reset_stats(&channel->stats);
11705e5027eSGreg Kroah-Hartman 	channel->pointer_read = 0;
11805e5027eSGreg Kroah-Hartman 	channel->pointer_write = 0;
11905e5027eSGreg Kroah-Hartman 	channel->nb_bytes = 0;
12005e5027eSGreg Kroah-Hartman }
12105e5027eSGreg Kroah-Hartman 
12205e5027eSGreg Kroah-Hartman static void ipoctal_close(struct tty_struct *tty, struct file *filp)
12305e5027eSGreg Kroah-Hartman {
12405e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
12505e5027eSGreg Kroah-Hartman 
12605e5027eSGreg Kroah-Hartman 	tty_port_close(&channel->tty_port, tty, filp);
12705e5027eSGreg Kroah-Hartman 	ipoctal_free_channel(channel);
12805e5027eSGreg Kroah-Hartman }
12905e5027eSGreg Kroah-Hartman 
13005e5027eSGreg Kroah-Hartman static int ipoctal_get_icount(struct tty_struct *tty,
13105e5027eSGreg Kroah-Hartman 			      struct serial_icounter_struct *icount)
13205e5027eSGreg Kroah-Hartman {
13305e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
13405e5027eSGreg Kroah-Hartman 
13505e5027eSGreg Kroah-Hartman 	icount->cts = 0;
13605e5027eSGreg Kroah-Hartman 	icount->dsr = 0;
13705e5027eSGreg Kroah-Hartman 	icount->rng = 0;
13805e5027eSGreg Kroah-Hartman 	icount->dcd = 0;
13905e5027eSGreg Kroah-Hartman 	icount->rx = channel->stats.rx;
14005e5027eSGreg Kroah-Hartman 	icount->tx = channel->stats.tx;
14105e5027eSGreg Kroah-Hartman 	icount->frame = channel->stats.framing_err;
14205e5027eSGreg Kroah-Hartman 	icount->parity = channel->stats.parity_err;
14305e5027eSGreg Kroah-Hartman 	icount->brk = channel->stats.rcv_break;
14405e5027eSGreg Kroah-Hartman 	return 0;
14505e5027eSGreg Kroah-Hartman }
14605e5027eSGreg Kroah-Hartman 
1472e124b4aSJiri Slaby static void ipoctal_irq_rx(struct ipoctal_channel *channel, u8 sr)
14805e5027eSGreg Kroah-Hartman {
14992a19f9cSJiri Slaby 	struct tty_port *port = &channel->tty_port;
15005e5027eSGreg Kroah-Hartman 	unsigned char value;
151b5071f2cSSamuel Iglesias Gonsalvez 	unsigned char flag;
15205e5027eSGreg Kroah-Hartman 	u8 isr;
15305e5027eSGreg Kroah-Hartman 
15405e5027eSGreg Kroah-Hartman 	do {
15505e5027eSGreg Kroah-Hartman 		value = ioread8(&channel->regs->r.rhr);
156b5071f2cSSamuel Iglesias Gonsalvez 		flag = TTY_NORMAL;
15705e5027eSGreg Kroah-Hartman 		/* Error: count statistics */
15805e5027eSGreg Kroah-Hartman 		if (sr & SR_ERROR) {
15905e5027eSGreg Kroah-Hartman 			iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
16005e5027eSGreg Kroah-Hartman 
16105e5027eSGreg Kroah-Hartman 			if (sr & SR_OVERRUN_ERROR) {
16205e5027eSGreg Kroah-Hartman 				channel->stats.overrun_err++;
16305e5027eSGreg Kroah-Hartman 				/* Overrun doesn't affect the current character*/
16492a19f9cSJiri Slaby 				tty_insert_flip_char(port, 0, TTY_OVERRUN);
16505e5027eSGreg Kroah-Hartman 			}
16605e5027eSGreg Kroah-Hartman 			if (sr & SR_PARITY_ERROR) {
16705e5027eSGreg Kroah-Hartman 				channel->stats.parity_err++;
16805e5027eSGreg Kroah-Hartman 				flag = TTY_PARITY;
16905e5027eSGreg Kroah-Hartman 			}
17005e5027eSGreg Kroah-Hartman 			if (sr & SR_FRAMING_ERROR) {
17105e5027eSGreg Kroah-Hartman 				channel->stats.framing_err++;
17205e5027eSGreg Kroah-Hartman 				flag = TTY_FRAME;
17305e5027eSGreg Kroah-Hartman 			}
17405e5027eSGreg Kroah-Hartman 			if (sr & SR_RECEIVED_BREAK) {
17505e5027eSGreg Kroah-Hartman 				channel->stats.rcv_break++;
17605e5027eSGreg Kroah-Hartman 				flag = TTY_BREAK;
17705e5027eSGreg Kroah-Hartman 			}
17805e5027eSGreg Kroah-Hartman 		}
17992a19f9cSJiri Slaby 		tty_insert_flip_char(port, value, flag);
18005e5027eSGreg Kroah-Hartman 
18105e5027eSGreg Kroah-Hartman 		/* Check if there are more characters in RX FIFO
18205e5027eSGreg Kroah-Hartman 		 * If there are more, the isr register for this channel
18305e5027eSGreg Kroah-Hartman 		 * has enabled the RxRDY|FFULL bit.
18405e5027eSGreg Kroah-Hartman 		 */
18505e5027eSGreg Kroah-Hartman 		isr = ioread8(&channel->block_regs->r.isr);
18605e5027eSGreg Kroah-Hartman 		sr = ioread8(&channel->regs->r.sr);
18705e5027eSGreg Kroah-Hartman 	} while (isr & channel->isr_rx_rdy_mask);
18805e5027eSGreg Kroah-Hartman 
1892e124b4aSJiri Slaby 	tty_flip_buffer_push(port);
19005e5027eSGreg Kroah-Hartman }
19105e5027eSGreg Kroah-Hartman 
19205e5027eSGreg Kroah-Hartman static void ipoctal_irq_tx(struct ipoctal_channel *channel)
19305e5027eSGreg Kroah-Hartman {
19405e5027eSGreg Kroah-Hartman 	unsigned char value;
19505e5027eSGreg Kroah-Hartman 	unsigned int *pointer_write = &channel->pointer_write;
19605e5027eSGreg Kroah-Hartman 
19769a6b9b1SAlberto Garcia 	if (channel->nb_bytes == 0)
19805e5027eSGreg Kroah-Hartman 		return;
19905e5027eSGreg Kroah-Hartman 
200968d04e8SFederico Vaga 	spin_lock(&channel->lock);
20105e5027eSGreg Kroah-Hartman 	value = channel->tty_port.xmit_buf[*pointer_write];
20205e5027eSGreg Kroah-Hartman 	iowrite8(value, &channel->regs->w.thr);
20305e5027eSGreg Kroah-Hartman 	channel->stats.tx++;
20405e5027eSGreg Kroah-Hartman 	(*pointer_write)++;
20505e5027eSGreg Kroah-Hartman 	*pointer_write = *pointer_write % PAGE_SIZE;
20605e5027eSGreg Kroah-Hartman 	channel->nb_bytes--;
207968d04e8SFederico Vaga 	spin_unlock(&channel->lock);
20805e5027eSGreg Kroah-Hartman }
20905e5027eSGreg Kroah-Hartman 
21005e5027eSGreg Kroah-Hartman static void ipoctal_irq_channel(struct ipoctal_channel *channel)
21105e5027eSGreg Kroah-Hartman {
21205e5027eSGreg Kroah-Hartman 	u8 isr, sr;
21305e5027eSGreg Kroah-Hartman 
21405e5027eSGreg Kroah-Hartman 	/* The HW is organized in pair of channels.  See which register we need
21505e5027eSGreg Kroah-Hartman 	 * to read from */
21605e5027eSGreg Kroah-Hartman 	isr = ioread8(&channel->block_regs->r.isr);
21705e5027eSGreg Kroah-Hartman 	sr = ioread8(&channel->regs->r.sr);
21805e5027eSGreg Kroah-Hartman 
21995e838c9SFederico Vaga 	if (isr & (IMR_DELTA_BREAK_A | IMR_DELTA_BREAK_B))
22095e838c9SFederico Vaga 		iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
22195e838c9SFederico Vaga 
2229d01b6f0SSamuel Iglesias Gonsalvez 	if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
2239d01b6f0SSamuel Iglesias Gonsalvez 		iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
22405e5027eSGreg Kroah-Hartman 		/* In case of RS-485, change from TX to RX when finishing TX.
22505e5027eSGreg Kroah-Hartman 		 * Half-duplex. */
2269d01b6f0SSamuel Iglesias Gonsalvez 		if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
22705e5027eSGreg Kroah-Hartman 			iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
22805e5027eSGreg Kroah-Hartman 			iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
2292910fe2aSSamuel Iglesias Gonsalvez 			channel->rx_enable = 1;
2309d01b6f0SSamuel Iglesias Gonsalvez 		}
23105e5027eSGreg Kroah-Hartman 	}
23205e5027eSGreg Kroah-Hartman 
23305e5027eSGreg Kroah-Hartman 	/* RX data */
23405e5027eSGreg Kroah-Hartman 	if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
2352e124b4aSJiri Slaby 		ipoctal_irq_rx(channel, sr);
23605e5027eSGreg Kroah-Hartman 
23705e5027eSGreg Kroah-Hartman 	/* TX of each character */
23805e5027eSGreg Kroah-Hartman 	if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
23905e5027eSGreg Kroah-Hartman 		ipoctal_irq_tx(channel);
24005e5027eSGreg Kroah-Hartman }
24105e5027eSGreg Kroah-Hartman 
24205e5027eSGreg Kroah-Hartman static irqreturn_t ipoctal_irq_handler(void *arg)
24305e5027eSGreg Kroah-Hartman {
24405e5027eSGreg Kroah-Hartman 	unsigned int i;
24505e5027eSGreg Kroah-Hartman 	struct ipoctal *ipoctal = (struct ipoctal *) arg;
24605e5027eSGreg Kroah-Hartman 
24705e5027eSGreg Kroah-Hartman 	/* Clear the IPack device interrupt */
24805e5027eSGreg Kroah-Hartman 	readw(ipoctal->int_space + ACK_INT_REQ0);
24905e5027eSGreg Kroah-Hartman 	readw(ipoctal->int_space + ACK_INT_REQ1);
25005e5027eSGreg Kroah-Hartman 
25121d27ed4SSamuel Iglesias Gonsalvez 	/* Check all channels */
25221d27ed4SSamuel Iglesias Gonsalvez 	for (i = 0; i < NR_CHANNELS; i++)
25321d27ed4SSamuel Iglesias Gonsalvez 		ipoctal_irq_channel(&ipoctal->channel[i]);
25421d27ed4SSamuel Iglesias Gonsalvez 
25505e5027eSGreg Kroah-Hartman 	return IRQ_HANDLED;
25605e5027eSGreg Kroah-Hartman }
25705e5027eSGreg Kroah-Hartman 
25805e5027eSGreg Kroah-Hartman static const struct tty_port_operations ipoctal_tty_port_ops = {
25905e5027eSGreg Kroah-Hartman 	.dtr_rts = NULL,
26005e5027eSGreg Kroah-Hartman 	.activate = ipoctal_port_activate,
26105e5027eSGreg Kroah-Hartman };
26205e5027eSGreg Kroah-Hartman 
26305e5027eSGreg Kroah-Hartman static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
26405e5027eSGreg Kroah-Hartman 			     unsigned int slot)
26505e5027eSGreg Kroah-Hartman {
26605e5027eSGreg Kroah-Hartman 	int res;
26705e5027eSGreg Kroah-Hartman 	int i;
26805e5027eSGreg Kroah-Hartman 	struct tty_driver *tty;
26905e5027eSGreg Kroah-Hartman 	char name[20];
27005e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel;
27105e5027eSGreg Kroah-Hartman 	struct ipack_region *region;
27205e5027eSGreg Kroah-Hartman 	void __iomem *addr;
27305e5027eSGreg Kroah-Hartman 	union scc2698_channel __iomem *chan_regs;
27405e5027eSGreg Kroah-Hartman 	union scc2698_block __iomem *block_regs;
27505e5027eSGreg Kroah-Hartman 
27605e5027eSGreg Kroah-Hartman 	ipoctal->board_id = ipoctal->dev->id_device;
27705e5027eSGreg Kroah-Hartman 
27805e5027eSGreg Kroah-Hartman 	region = &ipoctal->dev->region[IPACK_IO_SPACE];
2794bdc0d67SChristoph Hellwig 	addr = devm_ioremap(&ipoctal->dev->dev,
28005e5027eSGreg Kroah-Hartman 				    region->start, region->size);
28105e5027eSGreg Kroah-Hartman 	if (!addr) {
28205e5027eSGreg Kroah-Hartman 		dev_err(&ipoctal->dev->dev,
28305e5027eSGreg Kroah-Hartman 			"Unable to map slot [%d:%d] IO space!\n",
28405e5027eSGreg Kroah-Hartman 			bus_nr, slot);
28505e5027eSGreg Kroah-Hartman 		return -EADDRNOTAVAIL;
28605e5027eSGreg Kroah-Hartman 	}
28705e5027eSGreg Kroah-Hartman 	/* Save the virtual address to access the registers easily */
28805e5027eSGreg Kroah-Hartman 	chan_regs =
28905e5027eSGreg Kroah-Hartman 		(union scc2698_channel __iomem *) addr;
29005e5027eSGreg Kroah-Hartman 	block_regs =
29105e5027eSGreg Kroah-Hartman 		(union scc2698_block __iomem *) addr;
29205e5027eSGreg Kroah-Hartman 
29305e5027eSGreg Kroah-Hartman 	region = &ipoctal->dev->region[IPACK_INT_SPACE];
29405e5027eSGreg Kroah-Hartman 	ipoctal->int_space =
2954bdc0d67SChristoph Hellwig 		devm_ioremap(&ipoctal->dev->dev,
29605e5027eSGreg Kroah-Hartman 				     region->start, region->size);
29705e5027eSGreg Kroah-Hartman 	if (!ipoctal->int_space) {
29805e5027eSGreg Kroah-Hartman 		dev_err(&ipoctal->dev->dev,
29905e5027eSGreg Kroah-Hartman 			"Unable to map slot [%d:%d] INT space!\n",
30005e5027eSGreg Kroah-Hartman 			bus_nr, slot);
30105e5027eSGreg Kroah-Hartman 		return -EADDRNOTAVAIL;
30205e5027eSGreg Kroah-Hartman 	}
30305e5027eSGreg Kroah-Hartman 
30405e5027eSGreg Kroah-Hartman 	region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
30505e5027eSGreg Kroah-Hartman 	ipoctal->mem8_space =
3064bdc0d67SChristoph Hellwig 		devm_ioremap(&ipoctal->dev->dev,
30705e5027eSGreg Kroah-Hartman 				     region->start, 0x8000);
308fc8d713eSJulia Lawall 	if (!ipoctal->mem8_space) {
30905e5027eSGreg Kroah-Hartman 		dev_err(&ipoctal->dev->dev,
31005e5027eSGreg Kroah-Hartman 			"Unable to map slot [%d:%d] MEM8 space!\n",
31105e5027eSGreg Kroah-Hartman 			bus_nr, slot);
31205e5027eSGreg Kroah-Hartman 		return -EADDRNOTAVAIL;
31305e5027eSGreg Kroah-Hartman 	}
31405e5027eSGreg Kroah-Hartman 
31505e5027eSGreg Kroah-Hartman 
31605e5027eSGreg Kroah-Hartman 	/* Disable RX and TX before touching anything */
31705e5027eSGreg Kroah-Hartman 	for (i = 0; i < NR_CHANNELS ; i++) {
31805e5027eSGreg Kroah-Hartman 		struct ipoctal_channel *channel = &ipoctal->channel[i];
31905e5027eSGreg Kroah-Hartman 		channel->regs = chan_regs + i;
32005e5027eSGreg Kroah-Hartman 		channel->block_regs = block_regs + (i >> 1);
32105e5027eSGreg Kroah-Hartman 		channel->board_id = ipoctal->board_id;
32205e5027eSGreg Kroah-Hartman 		if (i & 1) {
32305e5027eSGreg Kroah-Hartman 			channel->isr_tx_rdy_mask = ISR_TxRDY_B;
32405e5027eSGreg Kroah-Hartman 			channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
32505e5027eSGreg Kroah-Hartman 		} else {
32605e5027eSGreg Kroah-Hartman 			channel->isr_tx_rdy_mask = ISR_TxRDY_A;
32705e5027eSGreg Kroah-Hartman 			channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
32805e5027eSGreg Kroah-Hartman 		}
32905e5027eSGreg Kroah-Hartman 
33078f22bc2SFederico Vaga 		ipoctal_reset_channel(channel);
33105e5027eSGreg Kroah-Hartman 		iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
33205e5027eSGreg Kroah-Hartman 			 &channel->regs->w.mr); /* mr1 */
33305e5027eSGreg Kroah-Hartman 		iowrite8(0, &channel->regs->w.mr); /* mr2 */
33405e5027eSGreg Kroah-Hartman 		iowrite8(TX_CLK_9600  | RX_CLK_9600, &channel->regs->w.csr);
33505e5027eSGreg Kroah-Hartman 	}
33605e5027eSGreg Kroah-Hartman 
33705e5027eSGreg Kroah-Hartman 	for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
33805e5027eSGreg Kroah-Hartman 		iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
33905e5027eSGreg Kroah-Hartman 		iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
34005e5027eSGreg Kroah-Hartman 			 &block_regs[i].w.opcr);
34105e5027eSGreg Kroah-Hartman 		iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
34205e5027eSGreg Kroah-Hartman 			 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
34305e5027eSGreg Kroah-Hartman 			 &block_regs[i].w.imr);
34405e5027eSGreg Kroah-Hartman 	}
34505e5027eSGreg Kroah-Hartman 
34605e5027eSGreg Kroah-Hartman 	/* Dummy write */
34705e5027eSGreg Kroah-Hartman 	iowrite8(1, ipoctal->mem8_space + 1);
34805e5027eSGreg Kroah-Hartman 
34905e5027eSGreg Kroah-Hartman 	/* Register the TTY device */
35005e5027eSGreg Kroah-Hartman 
35105e5027eSGreg Kroah-Hartman 	/* Each IP-OCTAL channel is a TTY port */
35205e5027eSGreg Kroah-Hartman 	tty = alloc_tty_driver(NR_CHANNELS);
35305e5027eSGreg Kroah-Hartman 
35405e5027eSGreg Kroah-Hartman 	if (!tty)
35505e5027eSGreg Kroah-Hartman 		return -ENOMEM;
35605e5027eSGreg Kroah-Hartman 
35705e5027eSGreg Kroah-Hartman 	/* Fill struct tty_driver with ipoctal data */
35805e5027eSGreg Kroah-Hartman 	tty->owner = THIS_MODULE;
35905e5027eSGreg Kroah-Hartman 	tty->driver_name = KBUILD_MODNAME;
36005e5027eSGreg Kroah-Hartman 	sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
36105e5027eSGreg Kroah-Hartman 	tty->name = name;
36205e5027eSGreg Kroah-Hartman 	tty->major = 0;
36305e5027eSGreg Kroah-Hartman 
36405e5027eSGreg Kroah-Hartman 	tty->minor_start = 0;
36505e5027eSGreg Kroah-Hartman 	tty->type = TTY_DRIVER_TYPE_SERIAL;
36605e5027eSGreg Kroah-Hartman 	tty->subtype = SERIAL_TYPE_NORMAL;
36705e5027eSGreg Kroah-Hartman 	tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
36805e5027eSGreg Kroah-Hartman 	tty->init_termios = tty_std_termios;
36905e5027eSGreg Kroah-Hartman 	tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
37005e5027eSGreg Kroah-Hartman 	tty->init_termios.c_ispeed = 9600;
37105e5027eSGreg Kroah-Hartman 	tty->init_termios.c_ospeed = 9600;
37205e5027eSGreg Kroah-Hartman 
37305e5027eSGreg Kroah-Hartman 	tty_set_operations(tty, &ipoctal_fops);
37405e5027eSGreg Kroah-Hartman 	res = tty_register_driver(tty);
37505e5027eSGreg Kroah-Hartman 	if (res) {
37605e5027eSGreg Kroah-Hartman 		dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
37705e5027eSGreg Kroah-Hartman 		put_tty_driver(tty);
37805e5027eSGreg Kroah-Hartman 		return res;
37905e5027eSGreg Kroah-Hartman 	}
38005e5027eSGreg Kroah-Hartman 
38105e5027eSGreg Kroah-Hartman 	/* Save struct tty_driver for use it when uninstalling the device */
38205e5027eSGreg Kroah-Hartman 	ipoctal->tty_drv = tty;
38305e5027eSGreg Kroah-Hartman 
38405e5027eSGreg Kroah-Hartman 	for (i = 0; i < NR_CHANNELS; i++) {
38505e5027eSGreg Kroah-Hartman 		struct device *tty_dev;
38605e5027eSGreg Kroah-Hartman 
38705e5027eSGreg Kroah-Hartman 		channel = &ipoctal->channel[i];
38805e5027eSGreg Kroah-Hartman 		tty_port_init(&channel->tty_port);
38905e5027eSGreg Kroah-Hartman 		tty_port_alloc_xmit_buf(&channel->tty_port);
39005e5027eSGreg Kroah-Hartman 		channel->tty_port.ops = &ipoctal_tty_port_ops;
39105e5027eSGreg Kroah-Hartman 
39205e5027eSGreg Kroah-Hartman 		ipoctal_reset_stats(&channel->stats);
39305e5027eSGreg Kroah-Hartman 		channel->nb_bytes = 0;
39405e5027eSGreg Kroah-Hartman 		spin_lock_init(&channel->lock);
39505e5027eSGreg Kroah-Hartman 		channel->pointer_read = 0;
39605e5027eSGreg Kroah-Hartman 		channel->pointer_write = 0;
39705e5027eSGreg Kroah-Hartman 		tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
39805e5027eSGreg Kroah-Hartman 		if (IS_ERR(tty_dev)) {
39905e5027eSGreg Kroah-Hartman 			dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
400c6bd5bccSLinus Torvalds 			tty_port_destroy(&channel->tty_port);
40105e5027eSGreg Kroah-Hartman 			continue;
40205e5027eSGreg Kroah-Hartman 		}
40305e5027eSGreg Kroah-Hartman 		dev_set_drvdata(tty_dev, channel);
40405e5027eSGreg Kroah-Hartman 	}
40505e5027eSGreg Kroah-Hartman 
4064847cc07SFederico Vaga 	/*
4074847cc07SFederico Vaga 	 * IP-OCTAL has different addresses to copy its IRQ vector.
4084847cc07SFederico Vaga 	 * Depending of the carrier these addresses are accesible or not.
4094847cc07SFederico Vaga 	 * More info in the datasheet.
4104847cc07SFederico Vaga 	 */
4114847cc07SFederico Vaga 	ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
4124847cc07SFederico Vaga 				       ipoctal_irq_handler, ipoctal);
4134847cc07SFederico Vaga 
41405e5027eSGreg Kroah-Hartman 	return 0;
41505e5027eSGreg Kroah-Hartman }
41605e5027eSGreg Kroah-Hartman 
41705e5027eSGreg Kroah-Hartman static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
41805e5027eSGreg Kroah-Hartman 					    const unsigned char *buf,
41905e5027eSGreg Kroah-Hartman 					    int count)
42005e5027eSGreg Kroah-Hartman {
42105e5027eSGreg Kroah-Hartman 	unsigned long flags;
42205e5027eSGreg Kroah-Hartman 	int i;
42305e5027eSGreg Kroah-Hartman 	unsigned int *pointer_read = &channel->pointer_read;
42405e5027eSGreg Kroah-Hartman 
42505e5027eSGreg Kroah-Hartman 	/* Copy the bytes from the user buffer to the internal one */
42605e5027eSGreg Kroah-Hartman 	for (i = 0; i < count; i++) {
42705e5027eSGreg Kroah-Hartman 		if (i <= (PAGE_SIZE - channel->nb_bytes)) {
42805e5027eSGreg Kroah-Hartman 			spin_lock_irqsave(&channel->lock, flags);
42905e5027eSGreg Kroah-Hartman 			channel->tty_port.xmit_buf[*pointer_read] = buf[i];
43005e5027eSGreg Kroah-Hartman 			*pointer_read = (*pointer_read + 1) % PAGE_SIZE;
43105e5027eSGreg Kroah-Hartman 			channel->nb_bytes++;
43205e5027eSGreg Kroah-Hartman 			spin_unlock_irqrestore(&channel->lock, flags);
43305e5027eSGreg Kroah-Hartman 		} else {
43405e5027eSGreg Kroah-Hartman 			break;
43505e5027eSGreg Kroah-Hartman 		}
43605e5027eSGreg Kroah-Hartman 	}
43705e5027eSGreg Kroah-Hartman 	return i;
43805e5027eSGreg Kroah-Hartman }
43905e5027eSGreg Kroah-Hartman 
44005e5027eSGreg Kroah-Hartman static int ipoctal_write_tty(struct tty_struct *tty,
44105e5027eSGreg Kroah-Hartman 			     const unsigned char *buf, int count)
44205e5027eSGreg Kroah-Hartman {
44305e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
44405e5027eSGreg Kroah-Hartman 	unsigned int char_copied;
44505e5027eSGreg Kroah-Hartman 
44605e5027eSGreg Kroah-Hartman 	char_copied = ipoctal_copy_write_buffer(channel, buf, count);
44705e5027eSGreg Kroah-Hartman 
44805e5027eSGreg Kroah-Hartman 	/* As the IP-OCTAL 485 only supports half duplex, do it manually */
44905e5027eSGreg Kroah-Hartman 	if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
45005e5027eSGreg Kroah-Hartman 		iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
451b0d17fbdSSamuel Iglesias Gonsalvez 		channel->rx_enable = 0;
45205e5027eSGreg Kroah-Hartman 		iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
45305e5027eSGreg Kroah-Hartman 	}
45405e5027eSGreg Kroah-Hartman 
45505e5027eSGreg Kroah-Hartman 	/*
45605e5027eSGreg Kroah-Hartman 	 * Send a packet and then disable TX to avoid failure after several send
45705e5027eSGreg Kroah-Hartman 	 * operations
45805e5027eSGreg Kroah-Hartman 	 */
45905e5027eSGreg Kroah-Hartman 	iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
46005e5027eSGreg Kroah-Hartman 	return char_copied;
46105e5027eSGreg Kroah-Hartman }
46205e5027eSGreg Kroah-Hartman 
46303b3b1a2SJiri Slaby static unsigned int ipoctal_write_room(struct tty_struct *tty)
46405e5027eSGreg Kroah-Hartman {
46505e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
46605e5027eSGreg Kroah-Hartman 
46705e5027eSGreg Kroah-Hartman 	return PAGE_SIZE - channel->nb_bytes;
46805e5027eSGreg Kroah-Hartman }
46905e5027eSGreg Kroah-Hartman 
470*fff4ef17SJiri Slaby static unsigned int ipoctal_chars_in_buffer(struct tty_struct *tty)
47105e5027eSGreg Kroah-Hartman {
47205e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
47305e5027eSGreg Kroah-Hartman 
47405e5027eSGreg Kroah-Hartman 	return channel->nb_bytes;
47505e5027eSGreg Kroah-Hartman }
47605e5027eSGreg Kroah-Hartman 
47705e5027eSGreg Kroah-Hartman static void ipoctal_set_termios(struct tty_struct *tty,
47805e5027eSGreg Kroah-Hartman 				struct ktermios *old_termios)
47905e5027eSGreg Kroah-Hartman {
48005e5027eSGreg Kroah-Hartman 	unsigned int cflag;
48105e5027eSGreg Kroah-Hartman 	unsigned char mr1 = 0;
48205e5027eSGreg Kroah-Hartman 	unsigned char mr2 = 0;
48305e5027eSGreg Kroah-Hartman 	unsigned char csr = 0;
48405e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
48505e5027eSGreg Kroah-Hartman 	speed_t baud;
48605e5027eSGreg Kroah-Hartman 
48705e5027eSGreg Kroah-Hartman 	cflag = tty->termios.c_cflag;
48805e5027eSGreg Kroah-Hartman 
48905e5027eSGreg Kroah-Hartman 	/* Disable and reset everything before change the setup */
49078f22bc2SFederico Vaga 	ipoctal_reset_channel(channel);
49105e5027eSGreg Kroah-Hartman 
49205e5027eSGreg Kroah-Hartman 	/* Set Bits per chars */
49305e5027eSGreg Kroah-Hartman 	switch (cflag & CSIZE) {
49405e5027eSGreg Kroah-Hartman 	case CS6:
49505e5027eSGreg Kroah-Hartman 		mr1 |= MR1_CHRL_6_BITS;
49605e5027eSGreg Kroah-Hartman 		break;
49705e5027eSGreg Kroah-Hartman 	case CS7:
49805e5027eSGreg Kroah-Hartman 		mr1 |= MR1_CHRL_7_BITS;
49905e5027eSGreg Kroah-Hartman 		break;
50005e5027eSGreg Kroah-Hartman 	case CS8:
50105e5027eSGreg Kroah-Hartman 	default:
50205e5027eSGreg Kroah-Hartman 		mr1 |= MR1_CHRL_8_BITS;
50305e5027eSGreg Kroah-Hartman 		/* By default, select CS8 */
50405e5027eSGreg Kroah-Hartman 		tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
50505e5027eSGreg Kroah-Hartman 		break;
50605e5027eSGreg Kroah-Hartman 	}
50705e5027eSGreg Kroah-Hartman 
50805e5027eSGreg Kroah-Hartman 	/* Set Parity */
50905e5027eSGreg Kroah-Hartman 	if (cflag & PARENB)
51005e5027eSGreg Kroah-Hartman 		if (cflag & PARODD)
51105e5027eSGreg Kroah-Hartman 			mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
51205e5027eSGreg Kroah-Hartman 		else
51305e5027eSGreg Kroah-Hartman 			mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
51405e5027eSGreg Kroah-Hartman 	else
51505e5027eSGreg Kroah-Hartman 		mr1 |= MR1_PARITY_OFF;
51605e5027eSGreg Kroah-Hartman 
51705e5027eSGreg Kroah-Hartman 	/* Mark or space parity is not supported */
51805e5027eSGreg Kroah-Hartman 	tty->termios.c_cflag &= ~CMSPAR;
51905e5027eSGreg Kroah-Hartman 
52005e5027eSGreg Kroah-Hartman 	/* Set stop bits */
52105e5027eSGreg Kroah-Hartman 	if (cflag & CSTOPB)
52205e5027eSGreg Kroah-Hartman 		mr2 |= MR2_STOP_BITS_LENGTH_2;
52305e5027eSGreg Kroah-Hartman 	else
52405e5027eSGreg Kroah-Hartman 		mr2 |= MR2_STOP_BITS_LENGTH_1;
52505e5027eSGreg Kroah-Hartman 
52605e5027eSGreg Kroah-Hartman 	/* Set the flow control */
52705e5027eSGreg Kroah-Hartman 	switch (channel->board_id) {
52805e5027eSGreg Kroah-Hartman 	case IPACK1_DEVICE_ID_SBS_OCTAL_232:
52905e5027eSGreg Kroah-Hartman 		if (cflag & CRTSCTS) {
53005e5027eSGreg Kroah-Hartman 			mr1 |= MR1_RxRTS_CONTROL_ON;
53105e5027eSGreg Kroah-Hartman 			mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
53205e5027eSGreg Kroah-Hartman 		} else {
53305e5027eSGreg Kroah-Hartman 			mr1 |= MR1_RxRTS_CONTROL_OFF;
53405e5027eSGreg Kroah-Hartman 			mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
53505e5027eSGreg Kroah-Hartman 		}
53605e5027eSGreg Kroah-Hartman 		break;
53705e5027eSGreg Kroah-Hartman 	case IPACK1_DEVICE_ID_SBS_OCTAL_422:
53805e5027eSGreg Kroah-Hartman 		mr1 |= MR1_RxRTS_CONTROL_OFF;
53905e5027eSGreg Kroah-Hartman 		mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
54005e5027eSGreg Kroah-Hartman 		break;
54105e5027eSGreg Kroah-Hartman 	case IPACK1_DEVICE_ID_SBS_OCTAL_485:
54205e5027eSGreg Kroah-Hartman 		mr1 |= MR1_RxRTS_CONTROL_OFF;
54305e5027eSGreg Kroah-Hartman 		mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
54405e5027eSGreg Kroah-Hartman 		break;
54505e5027eSGreg Kroah-Hartman 	default:
54605e5027eSGreg Kroah-Hartman 		return;
54705e5027eSGreg Kroah-Hartman 	}
54805e5027eSGreg Kroah-Hartman 
54905e5027eSGreg Kroah-Hartman 	baud = tty_get_baud_rate(tty);
55005e5027eSGreg Kroah-Hartman 	tty_termios_encode_baud_rate(&tty->termios, baud, baud);
55105e5027eSGreg Kroah-Hartman 
55205e5027eSGreg Kroah-Hartman 	/* Set baud rate */
55305e5027eSGreg Kroah-Hartman 	switch (baud) {
55405e5027eSGreg Kroah-Hartman 	case 75:
55505e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_75 | RX_CLK_75;
55605e5027eSGreg Kroah-Hartman 		break;
55705e5027eSGreg Kroah-Hartman 	case 110:
55805e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_110 | RX_CLK_110;
55905e5027eSGreg Kroah-Hartman 		break;
56005e5027eSGreg Kroah-Hartman 	case 150:
56105e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_150 | RX_CLK_150;
56205e5027eSGreg Kroah-Hartman 		break;
56305e5027eSGreg Kroah-Hartman 	case 300:
56405e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_300 | RX_CLK_300;
56505e5027eSGreg Kroah-Hartman 		break;
56605e5027eSGreg Kroah-Hartman 	case 600:
56705e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_600 | RX_CLK_600;
56805e5027eSGreg Kroah-Hartman 		break;
56905e5027eSGreg Kroah-Hartman 	case 1200:
57005e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_1200 | RX_CLK_1200;
57105e5027eSGreg Kroah-Hartman 		break;
57205e5027eSGreg Kroah-Hartman 	case 1800:
57305e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_1800 | RX_CLK_1800;
57405e5027eSGreg Kroah-Hartman 		break;
57505e5027eSGreg Kroah-Hartman 	case 2000:
57605e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_2000 | RX_CLK_2000;
57705e5027eSGreg Kroah-Hartman 		break;
57805e5027eSGreg Kroah-Hartman 	case 2400:
57905e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_2400 | RX_CLK_2400;
58005e5027eSGreg Kroah-Hartman 		break;
58105e5027eSGreg Kroah-Hartman 	case 4800:
58205e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_4800  | RX_CLK_4800;
58305e5027eSGreg Kroah-Hartman 		break;
58405e5027eSGreg Kroah-Hartman 	case 9600:
58505e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_9600  | RX_CLK_9600;
58605e5027eSGreg Kroah-Hartman 		break;
58705e5027eSGreg Kroah-Hartman 	case 19200:
58805e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_19200 | RX_CLK_19200;
58905e5027eSGreg Kroah-Hartman 		break;
59005e5027eSGreg Kroah-Hartman 	case 38400:
59105e5027eSGreg Kroah-Hartman 	default:
59205e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_38400 | RX_CLK_38400;
59305e5027eSGreg Kroah-Hartman 		/* In case of default, we establish 38400 bps */
59405e5027eSGreg Kroah-Hartman 		tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
59505e5027eSGreg Kroah-Hartman 		break;
59605e5027eSGreg Kroah-Hartman 	}
59705e5027eSGreg Kroah-Hartman 
59805e5027eSGreg Kroah-Hartman 	mr1 |= MR1_ERROR_CHAR;
59905e5027eSGreg Kroah-Hartman 	mr1 |= MR1_RxINT_RxRDY;
60005e5027eSGreg Kroah-Hartman 
60105e5027eSGreg Kroah-Hartman 	/* Write the control registers */
60205e5027eSGreg Kroah-Hartman 	iowrite8(mr1, &channel->regs->w.mr);
60305e5027eSGreg Kroah-Hartman 	iowrite8(mr2, &channel->regs->w.mr);
60405e5027eSGreg Kroah-Hartman 	iowrite8(csr, &channel->regs->w.csr);
60505e5027eSGreg Kroah-Hartman 
606b0d17fbdSSamuel Iglesias Gonsalvez 	/* Enable again the RX, if it was before */
607b0d17fbdSSamuel Iglesias Gonsalvez 	if (channel->rx_enable)
60805e5027eSGreg Kroah-Hartman 		iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
60905e5027eSGreg Kroah-Hartman }
61005e5027eSGreg Kroah-Hartman 
61105e5027eSGreg Kroah-Hartman static void ipoctal_hangup(struct tty_struct *tty)
61205e5027eSGreg Kroah-Hartman {
61305e5027eSGreg Kroah-Hartman 	unsigned long flags;
61405e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
61505e5027eSGreg Kroah-Hartman 
61605e5027eSGreg Kroah-Hartman 	if (channel == NULL)
61705e5027eSGreg Kroah-Hartman 		return;
61805e5027eSGreg Kroah-Hartman 
61905e5027eSGreg Kroah-Hartman 	spin_lock_irqsave(&channel->lock, flags);
62005e5027eSGreg Kroah-Hartman 	channel->nb_bytes = 0;
62105e5027eSGreg Kroah-Hartman 	channel->pointer_read = 0;
62205e5027eSGreg Kroah-Hartman 	channel->pointer_write = 0;
62305e5027eSGreg Kroah-Hartman 	spin_unlock_irqrestore(&channel->lock, flags);
62405e5027eSGreg Kroah-Hartman 
62505e5027eSGreg Kroah-Hartman 	tty_port_hangup(&channel->tty_port);
62605e5027eSGreg Kroah-Hartman 
62778f22bc2SFederico Vaga 	ipoctal_reset_channel(channel);
628d41861caSPeter Hurley 	tty_port_set_initialized(&channel->tty_port, 0);
62905e5027eSGreg Kroah-Hartman 	wake_up_interruptible(&channel->tty_port.open_wait);
63005e5027eSGreg Kroah-Hartman }
63105e5027eSGreg Kroah-Hartman 
632e0f8d323SSamuel Iglesias Gonsalvez static void ipoctal_shutdown(struct tty_struct *tty)
633e0f8d323SSamuel Iglesias Gonsalvez {
634e0f8d323SSamuel Iglesias Gonsalvez 	struct ipoctal_channel *channel = tty->driver_data;
635e0f8d323SSamuel Iglesias Gonsalvez 
636e0f8d323SSamuel Iglesias Gonsalvez 	if (channel == NULL)
637e0f8d323SSamuel Iglesias Gonsalvez 		return;
638e0f8d323SSamuel Iglesias Gonsalvez 
63978f22bc2SFederico Vaga 	ipoctal_reset_channel(channel);
640d41861caSPeter Hurley 	tty_port_set_initialized(&channel->tty_port, 0);
641e0f8d323SSamuel Iglesias Gonsalvez }
642e0f8d323SSamuel Iglesias Gonsalvez 
64382a82340SFederico Vaga static void ipoctal_cleanup(struct tty_struct *tty)
64482a82340SFederico Vaga {
64582a82340SFederico Vaga 	struct ipoctal_channel *channel = tty->driver_data;
64682a82340SFederico Vaga 	struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
64782a82340SFederico Vaga 
64882a82340SFederico Vaga 	/* release the carrier driver */
64982a82340SFederico Vaga 	ipack_put_carrier(ipoctal->dev);
65082a82340SFederico Vaga }
65182a82340SFederico Vaga 
65205e5027eSGreg Kroah-Hartman static const struct tty_operations ipoctal_fops = {
65305e5027eSGreg Kroah-Hartman 	.ioctl =		NULL,
65405e5027eSGreg Kroah-Hartman 	.open =			ipoctal_open,
65505e5027eSGreg Kroah-Hartman 	.close =		ipoctal_close,
65605e5027eSGreg Kroah-Hartman 	.write =		ipoctal_write_tty,
65705e5027eSGreg Kroah-Hartman 	.set_termios =		ipoctal_set_termios,
65805e5027eSGreg Kroah-Hartman 	.write_room =		ipoctal_write_room,
65905e5027eSGreg Kroah-Hartman 	.chars_in_buffer =	ipoctal_chars_in_buffer,
66005e5027eSGreg Kroah-Hartman 	.get_icount =		ipoctal_get_icount,
66105e5027eSGreg Kroah-Hartman 	.hangup =		ipoctal_hangup,
662e0f8d323SSamuel Iglesias Gonsalvez 	.shutdown =		ipoctal_shutdown,
66382a82340SFederico Vaga 	.cleanup =              ipoctal_cleanup,
66405e5027eSGreg Kroah-Hartman };
66505e5027eSGreg Kroah-Hartman 
66605e5027eSGreg Kroah-Hartman static int ipoctal_probe(struct ipack_device *dev)
66705e5027eSGreg Kroah-Hartman {
66805e5027eSGreg Kroah-Hartman 	int res;
66905e5027eSGreg Kroah-Hartman 	struct ipoctal *ipoctal;
67005e5027eSGreg Kroah-Hartman 
67105e5027eSGreg Kroah-Hartman 	ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
67205e5027eSGreg Kroah-Hartman 	if (ipoctal == NULL)
67305e5027eSGreg Kroah-Hartman 		return -ENOMEM;
67405e5027eSGreg Kroah-Hartman 
67505e5027eSGreg Kroah-Hartman 	ipoctal->dev = dev;
67605e5027eSGreg Kroah-Hartman 	res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
67705e5027eSGreg Kroah-Hartman 	if (res)
67805e5027eSGreg Kroah-Hartman 		goto out_uninst;
67905e5027eSGreg Kroah-Hartman 
68005e5027eSGreg Kroah-Hartman 	dev_set_drvdata(&dev->dev, ipoctal);
68105e5027eSGreg Kroah-Hartman 	return 0;
68205e5027eSGreg Kroah-Hartman 
68305e5027eSGreg Kroah-Hartman out_uninst:
68405e5027eSGreg Kroah-Hartman 	kfree(ipoctal);
68505e5027eSGreg Kroah-Hartman 	return res;
68605e5027eSGreg Kroah-Hartman }
68705e5027eSGreg Kroah-Hartman 
68805e5027eSGreg Kroah-Hartman static void __ipoctal_remove(struct ipoctal *ipoctal)
68905e5027eSGreg Kroah-Hartman {
69005e5027eSGreg Kroah-Hartman 	int i;
69105e5027eSGreg Kroah-Hartman 
69205e5027eSGreg Kroah-Hartman 	ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
69305e5027eSGreg Kroah-Hartman 
69405e5027eSGreg Kroah-Hartman 	for (i = 0; i < NR_CHANNELS; i++) {
69505e5027eSGreg Kroah-Hartman 		struct ipoctal_channel *channel = &ipoctal->channel[i];
69605e5027eSGreg Kroah-Hartman 		tty_unregister_device(ipoctal->tty_drv, i);
69705e5027eSGreg Kroah-Hartman 		tty_port_free_xmit_buf(&channel->tty_port);
698c6bd5bccSLinus Torvalds 		tty_port_destroy(&channel->tty_port);
69905e5027eSGreg Kroah-Hartman 	}
70005e5027eSGreg Kroah-Hartman 
70105e5027eSGreg Kroah-Hartman 	tty_unregister_driver(ipoctal->tty_drv);
70205e5027eSGreg Kroah-Hartman 	put_tty_driver(ipoctal->tty_drv);
70305e5027eSGreg Kroah-Hartman 	kfree(ipoctal);
70405e5027eSGreg Kroah-Hartman }
70505e5027eSGreg Kroah-Hartman 
70605e5027eSGreg Kroah-Hartman static void ipoctal_remove(struct ipack_device *idev)
70705e5027eSGreg Kroah-Hartman {
70805e5027eSGreg Kroah-Hartman 	__ipoctal_remove(dev_get_drvdata(&idev->dev));
70905e5027eSGreg Kroah-Hartman }
71005e5027eSGreg Kroah-Hartman 
71105e5027eSGreg Kroah-Hartman static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
71205e5027eSGreg Kroah-Hartman 	{ IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
71305e5027eSGreg Kroah-Hartman 			IPACK1_DEVICE_ID_SBS_OCTAL_232) },
71405e5027eSGreg Kroah-Hartman 	{ IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
71505e5027eSGreg Kroah-Hartman 			IPACK1_DEVICE_ID_SBS_OCTAL_422) },
71605e5027eSGreg Kroah-Hartman 	{ IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
71705e5027eSGreg Kroah-Hartman 			IPACK1_DEVICE_ID_SBS_OCTAL_485) },
71805e5027eSGreg Kroah-Hartman 	{ 0, },
71905e5027eSGreg Kroah-Hartman };
72005e5027eSGreg Kroah-Hartman 
72105e5027eSGreg Kroah-Hartman MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
72205e5027eSGreg Kroah-Hartman 
72305e5027eSGreg Kroah-Hartman static const struct ipack_driver_ops ipoctal_drv_ops = {
72405e5027eSGreg Kroah-Hartman 	.probe  = ipoctal_probe,
72505e5027eSGreg Kroah-Hartman 	.remove = ipoctal_remove,
72605e5027eSGreg Kroah-Hartman };
72705e5027eSGreg Kroah-Hartman 
72805e5027eSGreg Kroah-Hartman static struct ipack_driver driver = {
72905e5027eSGreg Kroah-Hartman 	.ops      = &ipoctal_drv_ops,
73005e5027eSGreg Kroah-Hartman 	.id_table = ipoctal_ids,
73105e5027eSGreg Kroah-Hartman };
73205e5027eSGreg Kroah-Hartman 
73305e5027eSGreg Kroah-Hartman static int __init ipoctal_init(void)
73405e5027eSGreg Kroah-Hartman {
73505e5027eSGreg Kroah-Hartman 	return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
73605e5027eSGreg Kroah-Hartman }
73705e5027eSGreg Kroah-Hartman 
73805e5027eSGreg Kroah-Hartman static void __exit ipoctal_exit(void)
73905e5027eSGreg Kroah-Hartman {
74005e5027eSGreg Kroah-Hartman 	ipack_driver_unregister(&driver);
74105e5027eSGreg Kroah-Hartman }
74205e5027eSGreg Kroah-Hartman 
74305e5027eSGreg Kroah-Hartman MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
74405e5027eSGreg Kroah-Hartman MODULE_LICENSE("GPL");
74505e5027eSGreg Kroah-Hartman 
74605e5027eSGreg Kroah-Hartman module_init(ipoctal_init);
74705e5027eSGreg Kroah-Hartman module_exit(ipoctal_exit);
748