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