xref: /openbmc/linux/drivers/ipack/devices/ipoctal.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b716f42eSAditya Srivastava /*
305e5027eSGreg Kroah-Hartman  * driver for the GE IP-OCTAL boards
405e5027eSGreg Kroah-Hartman  *
505e5027eSGreg Kroah-Hartman  * Copyright (C) 2009-2012 CERN (www.cern.ch)
605e5027eSGreg Kroah-Hartman  * Author: Nicolas Serafini, EIC2 SA
705e5027eSGreg Kroah-Hartman  * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
805e5027eSGreg Kroah-Hartman  */
905e5027eSGreg Kroah-Hartman 
1005e5027eSGreg Kroah-Hartman #include <linux/device.h>
1105e5027eSGreg Kroah-Hartman #include <linux/module.h>
1205e5027eSGreg Kroah-Hartman #include <linux/interrupt.h>
1305e5027eSGreg Kroah-Hartman #include <linux/sched.h>
1405e5027eSGreg Kroah-Hartman #include <linux/tty.h>
1505e5027eSGreg Kroah-Hartman #include <linux/serial.h>
1605e5027eSGreg Kroah-Hartman #include <linux/tty_flip.h>
1705e5027eSGreg Kroah-Hartman #include <linux/slab.h>
1805e5027eSGreg Kroah-Hartman #include <linux/io.h>
197dbce021SSamuel Iglesias Gonsalvez #include <linux/ipack.h>
2005e5027eSGreg Kroah-Hartman #include "ipoctal.h"
2105e5027eSGreg Kroah-Hartman #include "scc2698.h"
2205e5027eSGreg Kroah-Hartman 
2305e5027eSGreg Kroah-Hartman #define IP_OCTAL_ID_SPACE_VECTOR    0x41
2405e5027eSGreg Kroah-Hartman #define IP_OCTAL_NB_BLOCKS          4
2505e5027eSGreg Kroah-Hartman 
2605e5027eSGreg Kroah-Hartman static const struct tty_operations ipoctal_fops;
2705e5027eSGreg Kroah-Hartman 
2805e5027eSGreg Kroah-Hartman struct ipoctal_channel {
2905e5027eSGreg Kroah-Hartman 	struct ipoctal_stats		stats;
3005e5027eSGreg Kroah-Hartman 	unsigned int			nb_bytes;
3105e5027eSGreg Kroah-Hartman 	wait_queue_head_t		queue;
3205e5027eSGreg Kroah-Hartman 	spinlock_t			lock;
3305e5027eSGreg Kroah-Hartman 	unsigned int			pointer_read;
3405e5027eSGreg Kroah-Hartman 	unsigned int			pointer_write;
3505e5027eSGreg Kroah-Hartman 	struct tty_port			tty_port;
36cd20d592SJohan Hovold 	bool				tty_registered;
3705e5027eSGreg Kroah-Hartman 	union scc2698_channel __iomem	*regs;
3805e5027eSGreg Kroah-Hartman 	union scc2698_block __iomem	*block_regs;
3905e5027eSGreg Kroah-Hartman 	unsigned int			board_id;
4005e5027eSGreg Kroah-Hartman 	u8				isr_rx_rdy_mask;
4105e5027eSGreg Kroah-Hartman 	u8				isr_tx_rdy_mask;
42b0d17fbdSSamuel Iglesias Gonsalvez 	unsigned int			rx_enable;
4305e5027eSGreg Kroah-Hartman };
4405e5027eSGreg Kroah-Hartman 
4505e5027eSGreg Kroah-Hartman struct ipoctal {
4605e5027eSGreg Kroah-Hartman 	struct ipack_device		*dev;
4705e5027eSGreg Kroah-Hartman 	unsigned int			board_id;
4805e5027eSGreg Kroah-Hartman 	struct ipoctal_channel		channel[NR_CHANNELS];
4905e5027eSGreg Kroah-Hartman 	struct tty_driver		*tty_drv;
5005e5027eSGreg Kroah-Hartman 	u8 __iomem			*mem8_space;
5105e5027eSGreg Kroah-Hartman 	u8 __iomem			*int_space;
5205e5027eSGreg Kroah-Hartman };
5305e5027eSGreg Kroah-Hartman 
chan_to_ipoctal(struct ipoctal_channel * chan,unsigned int index)5482a82340SFederico Vaga static inline struct ipoctal *chan_to_ipoctal(struct ipoctal_channel *chan,
5582a82340SFederico Vaga 					      unsigned int index)
5682a82340SFederico Vaga {
5782a82340SFederico Vaga 	return container_of(chan, struct ipoctal, channel[index]);
5882a82340SFederico Vaga }
5982a82340SFederico Vaga 
ipoctal_reset_channel(struct ipoctal_channel * channel)6078f22bc2SFederico Vaga static void ipoctal_reset_channel(struct ipoctal_channel *channel)
6178f22bc2SFederico Vaga {
6278f22bc2SFederico Vaga 	iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
6378f22bc2SFederico Vaga 	channel->rx_enable = 0;
6478f22bc2SFederico Vaga 	iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
6578f22bc2SFederico Vaga 	iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
6678f22bc2SFederico Vaga 	iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
6778f22bc2SFederico Vaga 	iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
6878f22bc2SFederico Vaga }
6978f22bc2SFederico Vaga 
ipoctal_port_activate(struct tty_port * port,struct tty_struct * tty)7005e5027eSGreg Kroah-Hartman static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
7105e5027eSGreg Kroah-Hartman {
7205e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel;
7305e5027eSGreg Kroah-Hartman 
7405e5027eSGreg Kroah-Hartman 	channel = dev_get_drvdata(tty->dev);
7505e5027eSGreg Kroah-Hartman 
76a3882b78SSamuel Iglesias Gonsalvez 	/*
77a3882b78SSamuel Iglesias Gonsalvez 	 * Enable RX. TX will be enabled when
78a3882b78SSamuel Iglesias Gonsalvez 	 * there is something to send
79a3882b78SSamuel Iglesias Gonsalvez 	 */
8005e5027eSGreg Kroah-Hartman 	iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
81b0d17fbdSSamuel Iglesias Gonsalvez 	channel->rx_enable = 1;
8205e5027eSGreg Kroah-Hartman 	return 0;
8305e5027eSGreg Kroah-Hartman }
8405e5027eSGreg Kroah-Hartman 
ipoctal_install(struct tty_driver * driver,struct tty_struct * tty)85bb8a4fcbSJohan Hovold static int ipoctal_install(struct tty_driver *driver, struct tty_struct *tty)
8605e5027eSGreg Kroah-Hartman {
8782a82340SFederico Vaga 	struct ipoctal_channel *channel = dev_get_drvdata(tty->dev);
8882a82340SFederico Vaga 	struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
89bb8a4fcbSJohan Hovold 	int res;
9005e5027eSGreg Kroah-Hartman 
9182a82340SFederico Vaga 	if (!ipack_get_carrier(ipoctal->dev))
9282a82340SFederico Vaga 		return -EBUSY;
9382a82340SFederico Vaga 
94bb8a4fcbSJohan Hovold 	res = tty_standard_install(driver, tty);
95bb8a4fcbSJohan Hovold 	if (res)
96bb8a4fcbSJohan Hovold 		goto err_put_carrier;
97bb8a4fcbSJohan Hovold 
98bb8a4fcbSJohan Hovold 	tty->driver_data = channel;
99bb8a4fcbSJohan Hovold 
100bb8a4fcbSJohan Hovold 	return 0;
101bb8a4fcbSJohan Hovold 
102bb8a4fcbSJohan Hovold err_put_carrier:
10382a82340SFederico Vaga 	ipack_put_carrier(ipoctal->dev);
10482a82340SFederico Vaga 
105bb8a4fcbSJohan Hovold 	return res;
106bb8a4fcbSJohan Hovold }
107bb8a4fcbSJohan Hovold 
ipoctal_open(struct tty_struct * tty,struct file * file)108bb8a4fcbSJohan Hovold static int ipoctal_open(struct tty_struct *tty, struct file *file)
109bb8a4fcbSJohan Hovold {
110bb8a4fcbSJohan Hovold 	struct ipoctal_channel *channel = tty->driver_data;
111bb8a4fcbSJohan Hovold 
112bb8a4fcbSJohan Hovold 	return tty_port_open(&channel->tty_port, tty, file);
11305e5027eSGreg Kroah-Hartman }
11405e5027eSGreg Kroah-Hartman 
ipoctal_reset_stats(struct ipoctal_stats * stats)11505e5027eSGreg Kroah-Hartman static void ipoctal_reset_stats(struct ipoctal_stats *stats)
11605e5027eSGreg Kroah-Hartman {
11705e5027eSGreg Kroah-Hartman 	stats->tx = 0;
11805e5027eSGreg Kroah-Hartman 	stats->rx = 0;
11905e5027eSGreg Kroah-Hartman 	stats->rcv_break = 0;
12005e5027eSGreg Kroah-Hartman 	stats->framing_err = 0;
12105e5027eSGreg Kroah-Hartman 	stats->overrun_err = 0;
12205e5027eSGreg Kroah-Hartman 	stats->parity_err = 0;
12305e5027eSGreg Kroah-Hartman }
12405e5027eSGreg Kroah-Hartman 
ipoctal_free_channel(struct ipoctal_channel * channel)12505e5027eSGreg Kroah-Hartman static void ipoctal_free_channel(struct ipoctal_channel *channel)
12605e5027eSGreg Kroah-Hartman {
12705e5027eSGreg Kroah-Hartman 	ipoctal_reset_stats(&channel->stats);
12805e5027eSGreg Kroah-Hartman 	channel->pointer_read = 0;
12905e5027eSGreg Kroah-Hartman 	channel->pointer_write = 0;
13005e5027eSGreg Kroah-Hartman 	channel->nb_bytes = 0;
13105e5027eSGreg Kroah-Hartman }
13205e5027eSGreg Kroah-Hartman 
ipoctal_close(struct tty_struct * tty,struct file * filp)13305e5027eSGreg Kroah-Hartman static void ipoctal_close(struct tty_struct *tty, struct file *filp)
13405e5027eSGreg Kroah-Hartman {
13505e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
13605e5027eSGreg Kroah-Hartman 
13705e5027eSGreg Kroah-Hartman 	tty_port_close(&channel->tty_port, tty, filp);
13805e5027eSGreg Kroah-Hartman 	ipoctal_free_channel(channel);
13905e5027eSGreg Kroah-Hartman }
14005e5027eSGreg Kroah-Hartman 
ipoctal_get_icount(struct tty_struct * tty,struct serial_icounter_struct * icount)14105e5027eSGreg Kroah-Hartman static int ipoctal_get_icount(struct tty_struct *tty,
14205e5027eSGreg Kroah-Hartman 			      struct serial_icounter_struct *icount)
14305e5027eSGreg Kroah-Hartman {
14405e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
14505e5027eSGreg Kroah-Hartman 
14605e5027eSGreg Kroah-Hartman 	icount->cts = 0;
14705e5027eSGreg Kroah-Hartman 	icount->dsr = 0;
14805e5027eSGreg Kroah-Hartman 	icount->rng = 0;
14905e5027eSGreg Kroah-Hartman 	icount->dcd = 0;
15005e5027eSGreg Kroah-Hartman 	icount->rx = channel->stats.rx;
15105e5027eSGreg Kroah-Hartman 	icount->tx = channel->stats.tx;
15205e5027eSGreg Kroah-Hartman 	icount->frame = channel->stats.framing_err;
15305e5027eSGreg Kroah-Hartman 	icount->parity = channel->stats.parity_err;
15405e5027eSGreg Kroah-Hartman 	icount->brk = channel->stats.rcv_break;
15505e5027eSGreg Kroah-Hartman 	return 0;
15605e5027eSGreg Kroah-Hartman }
15705e5027eSGreg Kroah-Hartman 
ipoctal_irq_rx(struct ipoctal_channel * channel,u8 sr)1582e124b4aSJiri Slaby static void ipoctal_irq_rx(struct ipoctal_channel *channel, u8 sr)
15905e5027eSGreg Kroah-Hartman {
16092a19f9cSJiri Slaby 	struct tty_port *port = &channel->tty_port;
16105e5027eSGreg Kroah-Hartman 	unsigned char value;
162b5071f2cSSamuel Iglesias Gonsalvez 	unsigned char flag;
16305e5027eSGreg Kroah-Hartman 	u8 isr;
16405e5027eSGreg Kroah-Hartman 
16505e5027eSGreg Kroah-Hartman 	do {
16605e5027eSGreg Kroah-Hartman 		value = ioread8(&channel->regs->r.rhr);
167b5071f2cSSamuel Iglesias Gonsalvez 		flag = TTY_NORMAL;
16805e5027eSGreg Kroah-Hartman 		/* Error: count statistics */
16905e5027eSGreg Kroah-Hartman 		if (sr & SR_ERROR) {
17005e5027eSGreg Kroah-Hartman 			iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
17105e5027eSGreg Kroah-Hartman 
17205e5027eSGreg Kroah-Hartman 			if (sr & SR_OVERRUN_ERROR) {
17305e5027eSGreg Kroah-Hartman 				channel->stats.overrun_err++;
17405e5027eSGreg Kroah-Hartman 				/* Overrun doesn't affect the current character*/
17592a19f9cSJiri Slaby 				tty_insert_flip_char(port, 0, TTY_OVERRUN);
17605e5027eSGreg Kroah-Hartman 			}
17705e5027eSGreg Kroah-Hartman 			if (sr & SR_PARITY_ERROR) {
17805e5027eSGreg Kroah-Hartman 				channel->stats.parity_err++;
17905e5027eSGreg Kroah-Hartman 				flag = TTY_PARITY;
18005e5027eSGreg Kroah-Hartman 			}
18105e5027eSGreg Kroah-Hartman 			if (sr & SR_FRAMING_ERROR) {
18205e5027eSGreg Kroah-Hartman 				channel->stats.framing_err++;
18305e5027eSGreg Kroah-Hartman 				flag = TTY_FRAME;
18405e5027eSGreg Kroah-Hartman 			}
18505e5027eSGreg Kroah-Hartman 			if (sr & SR_RECEIVED_BREAK) {
18605e5027eSGreg Kroah-Hartman 				channel->stats.rcv_break++;
18705e5027eSGreg Kroah-Hartman 				flag = TTY_BREAK;
18805e5027eSGreg Kroah-Hartman 			}
18905e5027eSGreg Kroah-Hartman 		}
19092a19f9cSJiri Slaby 		tty_insert_flip_char(port, value, flag);
19105e5027eSGreg Kroah-Hartman 
19205e5027eSGreg Kroah-Hartman 		/* Check if there are more characters in RX FIFO
19305e5027eSGreg Kroah-Hartman 		 * If there are more, the isr register for this channel
19405e5027eSGreg Kroah-Hartman 		 * has enabled the RxRDY|FFULL bit.
19505e5027eSGreg Kroah-Hartman 		 */
19605e5027eSGreg Kroah-Hartman 		isr = ioread8(&channel->block_regs->r.isr);
19705e5027eSGreg Kroah-Hartman 		sr = ioread8(&channel->regs->r.sr);
19805e5027eSGreg Kroah-Hartman 	} while (isr & channel->isr_rx_rdy_mask);
19905e5027eSGreg Kroah-Hartman 
2002e124b4aSJiri Slaby 	tty_flip_buffer_push(port);
20105e5027eSGreg Kroah-Hartman }
20205e5027eSGreg Kroah-Hartman 
ipoctal_irq_tx(struct ipoctal_channel * channel)20305e5027eSGreg Kroah-Hartman static void ipoctal_irq_tx(struct ipoctal_channel *channel)
20405e5027eSGreg Kroah-Hartman {
20505e5027eSGreg Kroah-Hartman 	unsigned char value;
20605e5027eSGreg Kroah-Hartman 	unsigned int *pointer_write = &channel->pointer_write;
20705e5027eSGreg Kroah-Hartman 
20869a6b9b1SAlberto Garcia 	if (channel->nb_bytes == 0)
20905e5027eSGreg Kroah-Hartman 		return;
21005e5027eSGreg Kroah-Hartman 
211968d04e8SFederico Vaga 	spin_lock(&channel->lock);
21205e5027eSGreg Kroah-Hartman 	value = channel->tty_port.xmit_buf[*pointer_write];
21305e5027eSGreg Kroah-Hartman 	iowrite8(value, &channel->regs->w.thr);
21405e5027eSGreg Kroah-Hartman 	channel->stats.tx++;
21505e5027eSGreg Kroah-Hartman 	(*pointer_write)++;
21605e5027eSGreg Kroah-Hartman 	*pointer_write = *pointer_write % PAGE_SIZE;
21705e5027eSGreg Kroah-Hartman 	channel->nb_bytes--;
218968d04e8SFederico Vaga 	spin_unlock(&channel->lock);
21905e5027eSGreg Kroah-Hartman }
22005e5027eSGreg Kroah-Hartman 
ipoctal_irq_channel(struct ipoctal_channel * channel)22105e5027eSGreg Kroah-Hartman static void ipoctal_irq_channel(struct ipoctal_channel *channel)
22205e5027eSGreg Kroah-Hartman {
22305e5027eSGreg Kroah-Hartman 	u8 isr, sr;
22405e5027eSGreg Kroah-Hartman 
22505e5027eSGreg Kroah-Hartman 	/* The HW is organized in pair of channels.  See which register we need
22605e5027eSGreg Kroah-Hartman 	 * to read from */
22705e5027eSGreg Kroah-Hartman 	isr = ioread8(&channel->block_regs->r.isr);
22805e5027eSGreg Kroah-Hartman 	sr = ioread8(&channel->regs->r.sr);
22905e5027eSGreg Kroah-Hartman 
23095e838c9SFederico Vaga 	if (isr & (IMR_DELTA_BREAK_A | IMR_DELTA_BREAK_B))
23195e838c9SFederico Vaga 		iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
23295e838c9SFederico Vaga 
2339d01b6f0SSamuel Iglesias Gonsalvez 	if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
2349d01b6f0SSamuel Iglesias Gonsalvez 		iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
23505e5027eSGreg Kroah-Hartman 		/* In case of RS-485, change from TX to RX when finishing TX.
23605e5027eSGreg Kroah-Hartman 		 * Half-duplex. */
2379d01b6f0SSamuel Iglesias Gonsalvez 		if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
23805e5027eSGreg Kroah-Hartman 			iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
23905e5027eSGreg Kroah-Hartman 			iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
2402910fe2aSSamuel Iglesias Gonsalvez 			channel->rx_enable = 1;
2419d01b6f0SSamuel Iglesias Gonsalvez 		}
24205e5027eSGreg Kroah-Hartman 	}
24305e5027eSGreg Kroah-Hartman 
24405e5027eSGreg Kroah-Hartman 	/* RX data */
24505e5027eSGreg Kroah-Hartman 	if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
2462e124b4aSJiri Slaby 		ipoctal_irq_rx(channel, sr);
24705e5027eSGreg Kroah-Hartman 
24805e5027eSGreg Kroah-Hartman 	/* TX of each character */
24905e5027eSGreg Kroah-Hartman 	if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
25005e5027eSGreg Kroah-Hartman 		ipoctal_irq_tx(channel);
25105e5027eSGreg Kroah-Hartman }
25205e5027eSGreg Kroah-Hartman 
ipoctal_irq_handler(void * arg)25305e5027eSGreg Kroah-Hartman static irqreturn_t ipoctal_irq_handler(void *arg)
25405e5027eSGreg Kroah-Hartman {
25505e5027eSGreg Kroah-Hartman 	unsigned int i;
256c6e29fe5SXU pengfei 	struct ipoctal *ipoctal = arg;
25705e5027eSGreg Kroah-Hartman 
25805e5027eSGreg Kroah-Hartman 	/* Clear the IPack device interrupt */
25905e5027eSGreg Kroah-Hartman 	readw(ipoctal->int_space + ACK_INT_REQ0);
26005e5027eSGreg Kroah-Hartman 	readw(ipoctal->int_space + ACK_INT_REQ1);
26105e5027eSGreg Kroah-Hartman 
26221d27ed4SSamuel Iglesias Gonsalvez 	/* Check all channels */
26321d27ed4SSamuel Iglesias Gonsalvez 	for (i = 0; i < NR_CHANNELS; i++)
26421d27ed4SSamuel Iglesias Gonsalvez 		ipoctal_irq_channel(&ipoctal->channel[i]);
26521d27ed4SSamuel Iglesias Gonsalvez 
26605e5027eSGreg Kroah-Hartman 	return IRQ_HANDLED;
26705e5027eSGreg Kroah-Hartman }
26805e5027eSGreg Kroah-Hartman 
26905e5027eSGreg Kroah-Hartman static const struct tty_port_operations ipoctal_tty_port_ops = {
27005e5027eSGreg Kroah-Hartman 	.dtr_rts = NULL,
27105e5027eSGreg Kroah-Hartman 	.activate = ipoctal_port_activate,
27205e5027eSGreg Kroah-Hartman };
27305e5027eSGreg Kroah-Hartman 
ipoctal_inst_slot(struct ipoctal * ipoctal,unsigned int bus_nr,unsigned int slot)27405e5027eSGreg Kroah-Hartman static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
27505e5027eSGreg Kroah-Hartman 			     unsigned int slot)
27605e5027eSGreg Kroah-Hartman {
27705e5027eSGreg Kroah-Hartman 	int res;
27805e5027eSGreg Kroah-Hartman 	int i;
279349f2fe4SJohan Hovold 	struct tty_driver *drv;
28005e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel;
28105e5027eSGreg Kroah-Hartman 	struct ipack_region *region;
28205e5027eSGreg Kroah-Hartman 	void __iomem *addr;
28305e5027eSGreg Kroah-Hartman 	union scc2698_channel __iomem *chan_regs;
28405e5027eSGreg Kroah-Hartman 	union scc2698_block __iomem *block_regs;
28505e5027eSGreg Kroah-Hartman 
28605e5027eSGreg Kroah-Hartman 	ipoctal->board_id = ipoctal->dev->id_device;
28705e5027eSGreg Kroah-Hartman 
28805e5027eSGreg Kroah-Hartman 	region = &ipoctal->dev->region[IPACK_IO_SPACE];
2894bdc0d67SChristoph Hellwig 	addr = devm_ioremap(&ipoctal->dev->dev,
29005e5027eSGreg Kroah-Hartman 				    region->start, region->size);
29105e5027eSGreg Kroah-Hartman 	if (!addr) {
29205e5027eSGreg Kroah-Hartman 		dev_err(&ipoctal->dev->dev,
29305e5027eSGreg Kroah-Hartman 			"Unable to map slot [%d:%d] IO space!\n",
29405e5027eSGreg Kroah-Hartman 			bus_nr, slot);
29505e5027eSGreg Kroah-Hartman 		return -EADDRNOTAVAIL;
29605e5027eSGreg Kroah-Hartman 	}
29705e5027eSGreg Kroah-Hartman 	/* Save the virtual address to access the registers easily */
29805e5027eSGreg Kroah-Hartman 	chan_regs =
29905e5027eSGreg Kroah-Hartman 		(union scc2698_channel __iomem *) addr;
30005e5027eSGreg Kroah-Hartman 	block_regs =
30105e5027eSGreg Kroah-Hartman 		(union scc2698_block __iomem *) addr;
30205e5027eSGreg Kroah-Hartman 
30305e5027eSGreg Kroah-Hartman 	region = &ipoctal->dev->region[IPACK_INT_SPACE];
30405e5027eSGreg Kroah-Hartman 	ipoctal->int_space =
3054bdc0d67SChristoph Hellwig 		devm_ioremap(&ipoctal->dev->dev,
30605e5027eSGreg Kroah-Hartman 				     region->start, region->size);
30705e5027eSGreg Kroah-Hartman 	if (!ipoctal->int_space) {
30805e5027eSGreg Kroah-Hartman 		dev_err(&ipoctal->dev->dev,
30905e5027eSGreg Kroah-Hartman 			"Unable to map slot [%d:%d] INT space!\n",
31005e5027eSGreg Kroah-Hartman 			bus_nr, slot);
31105e5027eSGreg Kroah-Hartman 		return -EADDRNOTAVAIL;
31205e5027eSGreg Kroah-Hartman 	}
31305e5027eSGreg Kroah-Hartman 
31405e5027eSGreg Kroah-Hartman 	region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
31505e5027eSGreg Kroah-Hartman 	ipoctal->mem8_space =
3164bdc0d67SChristoph Hellwig 		devm_ioremap(&ipoctal->dev->dev,
31705e5027eSGreg Kroah-Hartman 				     region->start, 0x8000);
318fc8d713eSJulia Lawall 	if (!ipoctal->mem8_space) {
31905e5027eSGreg Kroah-Hartman 		dev_err(&ipoctal->dev->dev,
32005e5027eSGreg Kroah-Hartman 			"Unable to map slot [%d:%d] MEM8 space!\n",
32105e5027eSGreg Kroah-Hartman 			bus_nr, slot);
32205e5027eSGreg Kroah-Hartman 		return -EADDRNOTAVAIL;
32305e5027eSGreg Kroah-Hartman 	}
32405e5027eSGreg Kroah-Hartman 
32505e5027eSGreg Kroah-Hartman 
32605e5027eSGreg Kroah-Hartman 	/* Disable RX and TX before touching anything */
32705e5027eSGreg Kroah-Hartman 	for (i = 0; i < NR_CHANNELS ; i++) {
32805e5027eSGreg Kroah-Hartman 		struct ipoctal_channel *channel = &ipoctal->channel[i];
32905e5027eSGreg Kroah-Hartman 		channel->regs = chan_regs + i;
33005e5027eSGreg Kroah-Hartman 		channel->block_regs = block_regs + (i >> 1);
33105e5027eSGreg Kroah-Hartman 		channel->board_id = ipoctal->board_id;
33205e5027eSGreg Kroah-Hartman 		if (i & 1) {
33305e5027eSGreg Kroah-Hartman 			channel->isr_tx_rdy_mask = ISR_TxRDY_B;
33405e5027eSGreg Kroah-Hartman 			channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
33505e5027eSGreg Kroah-Hartman 		} else {
33605e5027eSGreg Kroah-Hartman 			channel->isr_tx_rdy_mask = ISR_TxRDY_A;
33705e5027eSGreg Kroah-Hartman 			channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
33805e5027eSGreg Kroah-Hartman 		}
33905e5027eSGreg Kroah-Hartman 
34078f22bc2SFederico Vaga 		ipoctal_reset_channel(channel);
34105e5027eSGreg Kroah-Hartman 		iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
34205e5027eSGreg Kroah-Hartman 			 &channel->regs->w.mr); /* mr1 */
34305e5027eSGreg Kroah-Hartman 		iowrite8(0, &channel->regs->w.mr); /* mr2 */
34405e5027eSGreg Kroah-Hartman 		iowrite8(TX_CLK_9600  | RX_CLK_9600, &channel->regs->w.csr);
34505e5027eSGreg Kroah-Hartman 	}
34605e5027eSGreg Kroah-Hartman 
34705e5027eSGreg Kroah-Hartman 	for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
34805e5027eSGreg Kroah-Hartman 		iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
34905e5027eSGreg Kroah-Hartman 		iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
35005e5027eSGreg Kroah-Hartman 			 &block_regs[i].w.opcr);
35105e5027eSGreg Kroah-Hartman 		iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
35205e5027eSGreg Kroah-Hartman 			 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
35305e5027eSGreg Kroah-Hartman 			 &block_regs[i].w.imr);
35405e5027eSGreg Kroah-Hartman 	}
35505e5027eSGreg Kroah-Hartman 
35605e5027eSGreg Kroah-Hartman 	/* Dummy write */
35705e5027eSGreg Kroah-Hartman 	iowrite8(1, ipoctal->mem8_space + 1);
35805e5027eSGreg Kroah-Hartman 
35905e5027eSGreg Kroah-Hartman 	/* Register the TTY device */
36005e5027eSGreg Kroah-Hartman 
36105e5027eSGreg Kroah-Hartman 	/* Each IP-OCTAL channel is a TTY port */
362349f2fe4SJohan Hovold 	drv = tty_alloc_driver(NR_CHANNELS, TTY_DRIVER_REAL_RAW |
36339b7b42bSJiri Slaby 			TTY_DRIVER_DYNAMIC_DEV);
364349f2fe4SJohan Hovold 	if (IS_ERR(drv))
365349f2fe4SJohan Hovold 		return PTR_ERR(drv);
36605e5027eSGreg Kroah-Hartman 
36705e5027eSGreg Kroah-Hartman 	/* Fill struct tty_driver with ipoctal data */
368349f2fe4SJohan Hovold 	drv->owner = THIS_MODULE;
369349f2fe4SJohan Hovold 	drv->driver_name = KBUILD_MODNAME;
370349f2fe4SJohan Hovold 	drv->name = kasprintf(GFP_KERNEL, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
371349f2fe4SJohan Hovold 	if (!drv->name) {
372a89936ccSJohan Hovold 		res = -ENOMEM;
373a89936ccSJohan Hovold 		goto err_put_driver;
374a89936ccSJohan Hovold 	}
375349f2fe4SJohan Hovold 	drv->major = 0;
37605e5027eSGreg Kroah-Hartman 
377349f2fe4SJohan Hovold 	drv->minor_start = 0;
378349f2fe4SJohan Hovold 	drv->type = TTY_DRIVER_TYPE_SERIAL;
379349f2fe4SJohan Hovold 	drv->subtype = SERIAL_TYPE_NORMAL;
380349f2fe4SJohan Hovold 	drv->init_termios = tty_std_termios;
381349f2fe4SJohan Hovold 	drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
382349f2fe4SJohan Hovold 	drv->init_termios.c_ispeed = 9600;
383349f2fe4SJohan Hovold 	drv->init_termios.c_ospeed = 9600;
38405e5027eSGreg Kroah-Hartman 
385349f2fe4SJohan Hovold 	tty_set_operations(drv, &ipoctal_fops);
386349f2fe4SJohan Hovold 	res = tty_register_driver(drv);
38705e5027eSGreg Kroah-Hartman 	if (res) {
38805e5027eSGreg Kroah-Hartman 		dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
389a89936ccSJohan Hovold 		goto err_free_name;
39005e5027eSGreg Kroah-Hartman 	}
39105e5027eSGreg Kroah-Hartman 
39205e5027eSGreg Kroah-Hartman 	/* Save struct tty_driver for use it when uninstalling the device */
393349f2fe4SJohan Hovold 	ipoctal->tty_drv = drv;
39405e5027eSGreg Kroah-Hartman 
39505e5027eSGreg Kroah-Hartman 	for (i = 0; i < NR_CHANNELS; i++) {
39605e5027eSGreg Kroah-Hartman 		struct device *tty_dev;
39705e5027eSGreg Kroah-Hartman 
39805e5027eSGreg Kroah-Hartman 		channel = &ipoctal->channel[i];
39905e5027eSGreg Kroah-Hartman 		tty_port_init(&channel->tty_port);
400445c8132SJohan Hovold 		res = tty_port_alloc_xmit_buf(&channel->tty_port);
401445c8132SJohan Hovold 		if (res)
402445c8132SJohan Hovold 			continue;
40305e5027eSGreg Kroah-Hartman 		channel->tty_port.ops = &ipoctal_tty_port_ops;
40405e5027eSGreg Kroah-Hartman 
40505e5027eSGreg Kroah-Hartman 		ipoctal_reset_stats(&channel->stats);
40605e5027eSGreg Kroah-Hartman 		channel->nb_bytes = 0;
40705e5027eSGreg Kroah-Hartman 		spin_lock_init(&channel->lock);
40805e5027eSGreg Kroah-Hartman 		channel->pointer_read = 0;
40905e5027eSGreg Kroah-Hartman 		channel->pointer_write = 0;
410349f2fe4SJohan Hovold 		tty_dev = tty_port_register_device_attr(&channel->tty_port, drv,
41165c001dfSJohan Hovold 							i, NULL, channel, NULL);
41205e5027eSGreg Kroah-Hartman 		if (IS_ERR(tty_dev)) {
41305e5027eSGreg Kroah-Hartman 			dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
414cd20d592SJohan Hovold 			tty_port_free_xmit_buf(&channel->tty_port);
415c6bd5bccSLinus Torvalds 			tty_port_destroy(&channel->tty_port);
41605e5027eSGreg Kroah-Hartman 			continue;
41705e5027eSGreg Kroah-Hartman 		}
418cd20d592SJohan Hovold 		channel->tty_registered = true;
41905e5027eSGreg Kroah-Hartman 	}
42005e5027eSGreg Kroah-Hartman 
4214847cc07SFederico Vaga 	/*
4224847cc07SFederico Vaga 	 * IP-OCTAL has different addresses to copy its IRQ vector.
4234847cc07SFederico Vaga 	 * Depending of the carrier these addresses are accesible or not.
4244847cc07SFederico Vaga 	 * More info in the datasheet.
4254847cc07SFederico Vaga 	 */
4264847cc07SFederico Vaga 	ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
4274847cc07SFederico Vaga 				       ipoctal_irq_handler, ipoctal);
4284847cc07SFederico Vaga 
42905e5027eSGreg Kroah-Hartman 	return 0;
430a89936ccSJohan Hovold 
431a89936ccSJohan Hovold err_free_name:
432349f2fe4SJohan Hovold 	kfree(drv->name);
433a89936ccSJohan Hovold err_put_driver:
434349f2fe4SJohan Hovold 	tty_driver_kref_put(drv);
435a89936ccSJohan Hovold 
436a89936ccSJohan Hovold 	return res;
43705e5027eSGreg Kroah-Hartman }
43805e5027eSGreg Kroah-Hartman 
ipoctal_copy_write_buffer(struct ipoctal_channel * channel,const u8 * buf,int count)43905e5027eSGreg Kroah-Hartman static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
44069851e4aSJiri Slaby (SUSE) 					    const u8 *buf, int count)
44105e5027eSGreg Kroah-Hartman {
44205e5027eSGreg Kroah-Hartman 	unsigned long flags;
44305e5027eSGreg Kroah-Hartman 	int i;
44405e5027eSGreg Kroah-Hartman 	unsigned int *pointer_read = &channel->pointer_read;
44505e5027eSGreg Kroah-Hartman 
44605e5027eSGreg Kroah-Hartman 	/* Copy the bytes from the user buffer to the internal one */
44705e5027eSGreg Kroah-Hartman 	for (i = 0; i < count; i++) {
44805e5027eSGreg Kroah-Hartman 		if (i <= (PAGE_SIZE - channel->nb_bytes)) {
44905e5027eSGreg Kroah-Hartman 			spin_lock_irqsave(&channel->lock, flags);
45005e5027eSGreg Kroah-Hartman 			channel->tty_port.xmit_buf[*pointer_read] = buf[i];
45105e5027eSGreg Kroah-Hartman 			*pointer_read = (*pointer_read + 1) % PAGE_SIZE;
45205e5027eSGreg Kroah-Hartman 			channel->nb_bytes++;
45305e5027eSGreg Kroah-Hartman 			spin_unlock_irqrestore(&channel->lock, flags);
45405e5027eSGreg Kroah-Hartman 		} else {
45505e5027eSGreg Kroah-Hartman 			break;
45605e5027eSGreg Kroah-Hartman 		}
45705e5027eSGreg Kroah-Hartman 	}
45805e5027eSGreg Kroah-Hartman 	return i;
45905e5027eSGreg Kroah-Hartman }
46005e5027eSGreg Kroah-Hartman 
ipoctal_write_tty(struct tty_struct * tty,const u8 * buf,size_t count)461*95713967SJiri Slaby (SUSE) static ssize_t ipoctal_write_tty(struct tty_struct *tty, const u8 *buf,
462*95713967SJiri Slaby (SUSE) 				 size_t count)
46305e5027eSGreg Kroah-Hartman {
46405e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
46505e5027eSGreg Kroah-Hartman 	unsigned int char_copied;
46605e5027eSGreg Kroah-Hartman 
46705e5027eSGreg Kroah-Hartman 	char_copied = ipoctal_copy_write_buffer(channel, buf, count);
46805e5027eSGreg Kroah-Hartman 
46905e5027eSGreg Kroah-Hartman 	/* As the IP-OCTAL 485 only supports half duplex, do it manually */
47005e5027eSGreg Kroah-Hartman 	if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
47105e5027eSGreg Kroah-Hartman 		iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
472b0d17fbdSSamuel Iglesias Gonsalvez 		channel->rx_enable = 0;
47305e5027eSGreg Kroah-Hartman 		iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
47405e5027eSGreg Kroah-Hartman 	}
47505e5027eSGreg Kroah-Hartman 
47605e5027eSGreg Kroah-Hartman 	/*
47705e5027eSGreg Kroah-Hartman 	 * Send a packet and then disable TX to avoid failure after several send
47805e5027eSGreg Kroah-Hartman 	 * operations
47905e5027eSGreg Kroah-Hartman 	 */
48005e5027eSGreg Kroah-Hartman 	iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
48105e5027eSGreg Kroah-Hartman 	return char_copied;
48205e5027eSGreg Kroah-Hartman }
48305e5027eSGreg Kroah-Hartman 
ipoctal_write_room(struct tty_struct * tty)48403b3b1a2SJiri Slaby static unsigned int ipoctal_write_room(struct tty_struct *tty)
48505e5027eSGreg Kroah-Hartman {
48605e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
48705e5027eSGreg Kroah-Hartman 
48805e5027eSGreg Kroah-Hartman 	return PAGE_SIZE - channel->nb_bytes;
48905e5027eSGreg Kroah-Hartman }
49005e5027eSGreg Kroah-Hartman 
ipoctal_chars_in_buffer(struct tty_struct * tty)491fff4ef17SJiri Slaby static unsigned int ipoctal_chars_in_buffer(struct tty_struct *tty)
49205e5027eSGreg Kroah-Hartman {
49305e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
49405e5027eSGreg Kroah-Hartman 
49505e5027eSGreg Kroah-Hartman 	return channel->nb_bytes;
49605e5027eSGreg Kroah-Hartman }
49705e5027eSGreg Kroah-Hartman 
ipoctal_set_termios(struct tty_struct * tty,const struct ktermios * old_termios)49805e5027eSGreg Kroah-Hartman static void ipoctal_set_termios(struct tty_struct *tty,
499a8c11c15SIlpo Järvinen 				const struct ktermios *old_termios)
50005e5027eSGreg Kroah-Hartman {
50105e5027eSGreg Kroah-Hartman 	unsigned int cflag;
50205e5027eSGreg Kroah-Hartman 	unsigned char mr1 = 0;
50305e5027eSGreg Kroah-Hartman 	unsigned char mr2 = 0;
50405e5027eSGreg Kroah-Hartman 	unsigned char csr = 0;
50505e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
50605e5027eSGreg Kroah-Hartman 	speed_t baud;
50705e5027eSGreg Kroah-Hartman 
50805e5027eSGreg Kroah-Hartman 	cflag = tty->termios.c_cflag;
50905e5027eSGreg Kroah-Hartman 
51005e5027eSGreg Kroah-Hartman 	/* Disable and reset everything before change the setup */
51178f22bc2SFederico Vaga 	ipoctal_reset_channel(channel);
51205e5027eSGreg Kroah-Hartman 
51305e5027eSGreg Kroah-Hartman 	/* Set Bits per chars */
51405e5027eSGreg Kroah-Hartman 	switch (cflag & CSIZE) {
51505e5027eSGreg Kroah-Hartman 	case CS6:
51605e5027eSGreg Kroah-Hartman 		mr1 |= MR1_CHRL_6_BITS;
51705e5027eSGreg Kroah-Hartman 		break;
51805e5027eSGreg Kroah-Hartman 	case CS7:
51905e5027eSGreg Kroah-Hartman 		mr1 |= MR1_CHRL_7_BITS;
52005e5027eSGreg Kroah-Hartman 		break;
52105e5027eSGreg Kroah-Hartman 	case CS8:
52205e5027eSGreg Kroah-Hartman 	default:
52305e5027eSGreg Kroah-Hartman 		mr1 |= MR1_CHRL_8_BITS;
52405e5027eSGreg Kroah-Hartman 		/* By default, select CS8 */
52505e5027eSGreg Kroah-Hartman 		tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
52605e5027eSGreg Kroah-Hartman 		break;
52705e5027eSGreg Kroah-Hartman 	}
52805e5027eSGreg Kroah-Hartman 
52905e5027eSGreg Kroah-Hartman 	/* Set Parity */
53005e5027eSGreg Kroah-Hartman 	if (cflag & PARENB)
53105e5027eSGreg Kroah-Hartman 		if (cflag & PARODD)
53205e5027eSGreg Kroah-Hartman 			mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
53305e5027eSGreg Kroah-Hartman 		else
53405e5027eSGreg Kroah-Hartman 			mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
53505e5027eSGreg Kroah-Hartman 	else
53605e5027eSGreg Kroah-Hartman 		mr1 |= MR1_PARITY_OFF;
53705e5027eSGreg Kroah-Hartman 
53805e5027eSGreg Kroah-Hartman 	/* Mark or space parity is not supported */
53905e5027eSGreg Kroah-Hartman 	tty->termios.c_cflag &= ~CMSPAR;
54005e5027eSGreg Kroah-Hartman 
54105e5027eSGreg Kroah-Hartman 	/* Set stop bits */
54205e5027eSGreg Kroah-Hartman 	if (cflag & CSTOPB)
54305e5027eSGreg Kroah-Hartman 		mr2 |= MR2_STOP_BITS_LENGTH_2;
54405e5027eSGreg Kroah-Hartman 	else
54505e5027eSGreg Kroah-Hartman 		mr2 |= MR2_STOP_BITS_LENGTH_1;
54605e5027eSGreg Kroah-Hartman 
54705e5027eSGreg Kroah-Hartman 	/* Set the flow control */
54805e5027eSGreg Kroah-Hartman 	switch (channel->board_id) {
54905e5027eSGreg Kroah-Hartman 	case IPACK1_DEVICE_ID_SBS_OCTAL_232:
55005e5027eSGreg Kroah-Hartman 		if (cflag & CRTSCTS) {
55105e5027eSGreg Kroah-Hartman 			mr1 |= MR1_RxRTS_CONTROL_ON;
55205e5027eSGreg Kroah-Hartman 			mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
55305e5027eSGreg Kroah-Hartman 		} else {
55405e5027eSGreg Kroah-Hartman 			mr1 |= MR1_RxRTS_CONTROL_OFF;
55505e5027eSGreg Kroah-Hartman 			mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
55605e5027eSGreg Kroah-Hartman 		}
55705e5027eSGreg Kroah-Hartman 		break;
55805e5027eSGreg Kroah-Hartman 	case IPACK1_DEVICE_ID_SBS_OCTAL_422:
55905e5027eSGreg Kroah-Hartman 		mr1 |= MR1_RxRTS_CONTROL_OFF;
56005e5027eSGreg Kroah-Hartman 		mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
56105e5027eSGreg Kroah-Hartman 		break;
56205e5027eSGreg Kroah-Hartman 	case IPACK1_DEVICE_ID_SBS_OCTAL_485:
56305e5027eSGreg Kroah-Hartman 		mr1 |= MR1_RxRTS_CONTROL_OFF;
56405e5027eSGreg Kroah-Hartman 		mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
56505e5027eSGreg Kroah-Hartman 		break;
56605e5027eSGreg Kroah-Hartman 	default:
56705e5027eSGreg Kroah-Hartman 		return;
56805e5027eSGreg Kroah-Hartman 	}
56905e5027eSGreg Kroah-Hartman 
57005e5027eSGreg Kroah-Hartman 	baud = tty_get_baud_rate(tty);
57105e5027eSGreg Kroah-Hartman 	tty_termios_encode_baud_rate(&tty->termios, baud, baud);
57205e5027eSGreg Kroah-Hartman 
57305e5027eSGreg Kroah-Hartman 	/* Set baud rate */
57405e5027eSGreg Kroah-Hartman 	switch (baud) {
57505e5027eSGreg Kroah-Hartman 	case 75:
57605e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_75 | RX_CLK_75;
57705e5027eSGreg Kroah-Hartman 		break;
57805e5027eSGreg Kroah-Hartman 	case 110:
57905e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_110 | RX_CLK_110;
58005e5027eSGreg Kroah-Hartman 		break;
58105e5027eSGreg Kroah-Hartman 	case 150:
58205e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_150 | RX_CLK_150;
58305e5027eSGreg Kroah-Hartman 		break;
58405e5027eSGreg Kroah-Hartman 	case 300:
58505e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_300 | RX_CLK_300;
58605e5027eSGreg Kroah-Hartman 		break;
58705e5027eSGreg Kroah-Hartman 	case 600:
58805e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_600 | RX_CLK_600;
58905e5027eSGreg Kroah-Hartman 		break;
59005e5027eSGreg Kroah-Hartman 	case 1200:
59105e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_1200 | RX_CLK_1200;
59205e5027eSGreg Kroah-Hartman 		break;
59305e5027eSGreg Kroah-Hartman 	case 1800:
59405e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_1800 | RX_CLK_1800;
59505e5027eSGreg Kroah-Hartman 		break;
59605e5027eSGreg Kroah-Hartman 	case 2000:
59705e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_2000 | RX_CLK_2000;
59805e5027eSGreg Kroah-Hartman 		break;
59905e5027eSGreg Kroah-Hartman 	case 2400:
60005e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_2400 | RX_CLK_2400;
60105e5027eSGreg Kroah-Hartman 		break;
60205e5027eSGreg Kroah-Hartman 	case 4800:
60305e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_4800  | RX_CLK_4800;
60405e5027eSGreg Kroah-Hartman 		break;
60505e5027eSGreg Kroah-Hartman 	case 9600:
60605e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_9600  | RX_CLK_9600;
60705e5027eSGreg Kroah-Hartman 		break;
60805e5027eSGreg Kroah-Hartman 	case 19200:
60905e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_19200 | RX_CLK_19200;
61005e5027eSGreg Kroah-Hartman 		break;
61105e5027eSGreg Kroah-Hartman 	case 38400:
61205e5027eSGreg Kroah-Hartman 	default:
61305e5027eSGreg Kroah-Hartman 		csr |= TX_CLK_38400 | RX_CLK_38400;
61405e5027eSGreg Kroah-Hartman 		/* In case of default, we establish 38400 bps */
61505e5027eSGreg Kroah-Hartman 		tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
61605e5027eSGreg Kroah-Hartman 		break;
61705e5027eSGreg Kroah-Hartman 	}
61805e5027eSGreg Kroah-Hartman 
61905e5027eSGreg Kroah-Hartman 	mr1 |= MR1_ERROR_CHAR;
62005e5027eSGreg Kroah-Hartman 	mr1 |= MR1_RxINT_RxRDY;
62105e5027eSGreg Kroah-Hartman 
62205e5027eSGreg Kroah-Hartman 	/* Write the control registers */
62305e5027eSGreg Kroah-Hartman 	iowrite8(mr1, &channel->regs->w.mr);
62405e5027eSGreg Kroah-Hartman 	iowrite8(mr2, &channel->regs->w.mr);
62505e5027eSGreg Kroah-Hartman 	iowrite8(csr, &channel->regs->w.csr);
62605e5027eSGreg Kroah-Hartman 
627b0d17fbdSSamuel Iglesias Gonsalvez 	/* Enable again the RX, if it was before */
628b0d17fbdSSamuel Iglesias Gonsalvez 	if (channel->rx_enable)
62905e5027eSGreg Kroah-Hartman 		iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
63005e5027eSGreg Kroah-Hartman }
63105e5027eSGreg Kroah-Hartman 
ipoctal_hangup(struct tty_struct * tty)63205e5027eSGreg Kroah-Hartman static void ipoctal_hangup(struct tty_struct *tty)
63305e5027eSGreg Kroah-Hartman {
63405e5027eSGreg Kroah-Hartman 	unsigned long flags;
63505e5027eSGreg Kroah-Hartman 	struct ipoctal_channel *channel = tty->driver_data;
63605e5027eSGreg Kroah-Hartman 
63705e5027eSGreg Kroah-Hartman 	if (channel == NULL)
63805e5027eSGreg Kroah-Hartman 		return;
63905e5027eSGreg Kroah-Hartman 
64005e5027eSGreg Kroah-Hartman 	spin_lock_irqsave(&channel->lock, flags);
64105e5027eSGreg Kroah-Hartman 	channel->nb_bytes = 0;
64205e5027eSGreg Kroah-Hartman 	channel->pointer_read = 0;
64305e5027eSGreg Kroah-Hartman 	channel->pointer_write = 0;
64405e5027eSGreg Kroah-Hartman 	spin_unlock_irqrestore(&channel->lock, flags);
64505e5027eSGreg Kroah-Hartman 
64605e5027eSGreg Kroah-Hartman 	tty_port_hangup(&channel->tty_port);
64705e5027eSGreg Kroah-Hartman 
64878f22bc2SFederico Vaga 	ipoctal_reset_channel(channel);
649515be7baSIlpo Järvinen 	tty_port_set_initialized(&channel->tty_port, false);
65005e5027eSGreg Kroah-Hartman 	wake_up_interruptible(&channel->tty_port.open_wait);
65105e5027eSGreg Kroah-Hartman }
65205e5027eSGreg Kroah-Hartman 
ipoctal_shutdown(struct tty_struct * tty)653e0f8d323SSamuel Iglesias Gonsalvez static void ipoctal_shutdown(struct tty_struct *tty)
654e0f8d323SSamuel Iglesias Gonsalvez {
655e0f8d323SSamuel Iglesias Gonsalvez 	struct ipoctal_channel *channel = tty->driver_data;
656e0f8d323SSamuel Iglesias Gonsalvez 
657e0f8d323SSamuel Iglesias Gonsalvez 	if (channel == NULL)
658e0f8d323SSamuel Iglesias Gonsalvez 		return;
659e0f8d323SSamuel Iglesias Gonsalvez 
66078f22bc2SFederico Vaga 	ipoctal_reset_channel(channel);
661515be7baSIlpo Järvinen 	tty_port_set_initialized(&channel->tty_port, false);
662e0f8d323SSamuel Iglesias Gonsalvez }
663e0f8d323SSamuel Iglesias Gonsalvez 
ipoctal_cleanup(struct tty_struct * tty)66482a82340SFederico Vaga static void ipoctal_cleanup(struct tty_struct *tty)
66582a82340SFederico Vaga {
66682a82340SFederico Vaga 	struct ipoctal_channel *channel = tty->driver_data;
66782a82340SFederico Vaga 	struct ipoctal *ipoctal = chan_to_ipoctal(channel, tty->index);
66882a82340SFederico Vaga 
66982a82340SFederico Vaga 	/* release the carrier driver */
67082a82340SFederico Vaga 	ipack_put_carrier(ipoctal->dev);
67182a82340SFederico Vaga }
67282a82340SFederico Vaga 
67305e5027eSGreg Kroah-Hartman static const struct tty_operations ipoctal_fops = {
67405e5027eSGreg Kroah-Hartman 	.ioctl =		NULL,
675bb8a4fcbSJohan Hovold 	.install =		ipoctal_install,
67605e5027eSGreg Kroah-Hartman 	.open =			ipoctal_open,
67705e5027eSGreg Kroah-Hartman 	.close =		ipoctal_close,
67805e5027eSGreg Kroah-Hartman 	.write =		ipoctal_write_tty,
67905e5027eSGreg Kroah-Hartman 	.set_termios =		ipoctal_set_termios,
68005e5027eSGreg Kroah-Hartman 	.write_room =		ipoctal_write_room,
68105e5027eSGreg Kroah-Hartman 	.chars_in_buffer =	ipoctal_chars_in_buffer,
68205e5027eSGreg Kroah-Hartman 	.get_icount =		ipoctal_get_icount,
68305e5027eSGreg Kroah-Hartman 	.hangup =		ipoctal_hangup,
684e0f8d323SSamuel Iglesias Gonsalvez 	.shutdown =		ipoctal_shutdown,
68582a82340SFederico Vaga 	.cleanup =              ipoctal_cleanup,
68605e5027eSGreg Kroah-Hartman };
68705e5027eSGreg Kroah-Hartman 
ipoctal_probe(struct ipack_device * dev)68805e5027eSGreg Kroah-Hartman static int ipoctal_probe(struct ipack_device *dev)
68905e5027eSGreg Kroah-Hartman {
69005e5027eSGreg Kroah-Hartman 	int res;
69105e5027eSGreg Kroah-Hartman 	struct ipoctal *ipoctal;
69205e5027eSGreg Kroah-Hartman 
69305e5027eSGreg Kroah-Hartman 	ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
69405e5027eSGreg Kroah-Hartman 	if (ipoctal == NULL)
69505e5027eSGreg Kroah-Hartman 		return -ENOMEM;
69605e5027eSGreg Kroah-Hartman 
69705e5027eSGreg Kroah-Hartman 	ipoctal->dev = dev;
69805e5027eSGreg Kroah-Hartman 	res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
69905e5027eSGreg Kroah-Hartman 	if (res)
70005e5027eSGreg Kroah-Hartman 		goto out_uninst;
70105e5027eSGreg Kroah-Hartman 
70205e5027eSGreg Kroah-Hartman 	dev_set_drvdata(&dev->dev, ipoctal);
70305e5027eSGreg Kroah-Hartman 	return 0;
70405e5027eSGreg Kroah-Hartman 
70505e5027eSGreg Kroah-Hartman out_uninst:
70605e5027eSGreg Kroah-Hartman 	kfree(ipoctal);
70705e5027eSGreg Kroah-Hartman 	return res;
70805e5027eSGreg Kroah-Hartman }
70905e5027eSGreg Kroah-Hartman 
__ipoctal_remove(struct ipoctal * ipoctal)71005e5027eSGreg Kroah-Hartman static void __ipoctal_remove(struct ipoctal *ipoctal)
71105e5027eSGreg Kroah-Hartman {
71205e5027eSGreg Kroah-Hartman 	int i;
71305e5027eSGreg Kroah-Hartman 
71405e5027eSGreg Kroah-Hartman 	ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
71505e5027eSGreg Kroah-Hartman 
71605e5027eSGreg Kroah-Hartman 	for (i = 0; i < NR_CHANNELS; i++) {
71705e5027eSGreg Kroah-Hartman 		struct ipoctal_channel *channel = &ipoctal->channel[i];
718cd20d592SJohan Hovold 
719cd20d592SJohan Hovold 		if (!channel->tty_registered)
720cd20d592SJohan Hovold 			continue;
721cd20d592SJohan Hovold 
72205e5027eSGreg Kroah-Hartman 		tty_unregister_device(ipoctal->tty_drv, i);
72305e5027eSGreg Kroah-Hartman 		tty_port_free_xmit_buf(&channel->tty_port);
724c6bd5bccSLinus Torvalds 		tty_port_destroy(&channel->tty_port);
72505e5027eSGreg Kroah-Hartman 	}
72605e5027eSGreg Kroah-Hartman 
72705e5027eSGreg Kroah-Hartman 	tty_unregister_driver(ipoctal->tty_drv);
728a89936ccSJohan Hovold 	kfree(ipoctal->tty_drv->name);
7299f90a4ddSJiri Slaby 	tty_driver_kref_put(ipoctal->tty_drv);
73005e5027eSGreg Kroah-Hartman 	kfree(ipoctal);
73105e5027eSGreg Kroah-Hartman }
73205e5027eSGreg Kroah-Hartman 
ipoctal_remove(struct ipack_device * idev)73305e5027eSGreg Kroah-Hartman static void ipoctal_remove(struct ipack_device *idev)
73405e5027eSGreg Kroah-Hartman {
73505e5027eSGreg Kroah-Hartman 	__ipoctal_remove(dev_get_drvdata(&idev->dev));
73605e5027eSGreg Kroah-Hartman }
73705e5027eSGreg Kroah-Hartman 
73805e5027eSGreg Kroah-Hartman static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
73905e5027eSGreg Kroah-Hartman 	{ IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
74005e5027eSGreg Kroah-Hartman 			IPACK1_DEVICE_ID_SBS_OCTAL_232) },
74105e5027eSGreg Kroah-Hartman 	{ IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
74205e5027eSGreg Kroah-Hartman 			IPACK1_DEVICE_ID_SBS_OCTAL_422) },
74305e5027eSGreg Kroah-Hartman 	{ IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
74405e5027eSGreg Kroah-Hartman 			IPACK1_DEVICE_ID_SBS_OCTAL_485) },
74505e5027eSGreg Kroah-Hartman 	{ 0, },
74605e5027eSGreg Kroah-Hartman };
74705e5027eSGreg Kroah-Hartman 
74805e5027eSGreg Kroah-Hartman MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
74905e5027eSGreg Kroah-Hartman 
75005e5027eSGreg Kroah-Hartman static const struct ipack_driver_ops ipoctal_drv_ops = {
75105e5027eSGreg Kroah-Hartman 	.probe  = ipoctal_probe,
75205e5027eSGreg Kroah-Hartman 	.remove = ipoctal_remove,
75305e5027eSGreg Kroah-Hartman };
75405e5027eSGreg Kroah-Hartman 
75505e5027eSGreg Kroah-Hartman static struct ipack_driver driver = {
75605e5027eSGreg Kroah-Hartman 	.ops      = &ipoctal_drv_ops,
75705e5027eSGreg Kroah-Hartman 	.id_table = ipoctal_ids,
75805e5027eSGreg Kroah-Hartman };
75905e5027eSGreg Kroah-Hartman 
ipoctal_init(void)76005e5027eSGreg Kroah-Hartman static int __init ipoctal_init(void)
76105e5027eSGreg Kroah-Hartman {
76205e5027eSGreg Kroah-Hartman 	return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
76305e5027eSGreg Kroah-Hartman }
76405e5027eSGreg Kroah-Hartman 
ipoctal_exit(void)76505e5027eSGreg Kroah-Hartman static void __exit ipoctal_exit(void)
76605e5027eSGreg Kroah-Hartman {
76705e5027eSGreg Kroah-Hartman 	ipack_driver_unregister(&driver);
76805e5027eSGreg Kroah-Hartman }
76905e5027eSGreg Kroah-Hartman 
77005e5027eSGreg Kroah-Hartman MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
77105e5027eSGreg Kroah-Hartman MODULE_LICENSE("GPL");
77205e5027eSGreg Kroah-Hartman 
77305e5027eSGreg Kroah-Hartman module_init(ipoctal_init);
77405e5027eSGreg Kroah-Hartman module_exit(ipoctal_exit);
775