Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 1 | /** |
| 2 | * ipoctal.c |
| 3 | * |
| 4 | * driver for the GE IP-OCTAL boards |
Samuel Iglesias Gonsalvez | 7685972 | 2012-11-16 16:19:58 +0100 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 2009-2012 CERN (www.cern.ch) |
| 7 | * Author: Nicolas Serafini, EIC2 SA |
| 8 | * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com> |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
Samuel Iglesias Gonsalvez | 3225436 | 2012-05-11 10:17:15 +0200 | [diff] [blame] | 12 | * Software Foundation; version 2 of the License. |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 15 | #include <linux/device.h> |
| 16 | #include <linux/module.h> |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 18 | #include <linux/sched.h> |
| 19 | #include <linux/tty.h> |
| 20 | #include <linux/serial.h> |
| 21 | #include <linux/tty_flip.h> |
| 22 | #include <linux/slab.h> |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 23 | #include <linux/atomic.h> |
Jens Taprogge | 64802dc | 2012-09-04 17:01:08 +0200 | [diff] [blame] | 24 | #include <linux/io.h> |
Samuel Iglesias Gonsalvez | 7dbce02 | 2012-11-16 19:33:45 +0100 | [diff] [blame] | 25 | #include <linux/ipack.h> |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 26 | #include "ipoctal.h" |
| 27 | #include "scc2698.h" |
| 28 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 29 | #define IP_OCTAL_ID_SPACE_VECTOR 0x41 |
| 30 | #define IP_OCTAL_NB_BLOCKS 4 |
| 31 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 32 | static const struct tty_operations ipoctal_fops; |
| 33 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 34 | struct ipoctal_channel { |
| 35 | struct ipoctal_stats stats; |
| 36 | unsigned int nb_bytes; |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 37 | wait_queue_head_t queue; |
| 38 | spinlock_t lock; |
| 39 | unsigned int pointer_read; |
| 40 | unsigned int pointer_write; |
| 41 | atomic_t open; |
| 42 | struct tty_port tty_port; |
| 43 | union scc2698_channel __iomem *regs; |
| 44 | union scc2698_block __iomem *block_regs; |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 45 | unsigned int board_id; |
| 46 | unsigned char *board_write; |
Jens Taprogge | 4e4732a | 2012-09-12 14:55:29 +0200 | [diff] [blame] | 47 | u8 isr_rx_rdy_mask; |
| 48 | u8 isr_tx_rdy_mask; |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 51 | struct ipoctal { |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 52 | struct ipack_device *dev; |
| 53 | unsigned int board_id; |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 54 | struct ipoctal_channel channel[NR_CHANNELS]; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 55 | unsigned char write; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 56 | struct tty_driver *tty_drv; |
Jens Taprogge | fe4a3ed | 2012-09-27 12:37:36 +0200 | [diff] [blame] | 57 | u8 __iomem *mem8_space; |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 58 | u8 __iomem *int_space; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 61 | static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty) |
| 62 | { |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 63 | struct ipoctal_channel *channel; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 64 | |
Jens Taprogge | 9c1d784 | 2012-09-12 14:55:42 +0200 | [diff] [blame] | 65 | channel = dev_get_drvdata(tty->dev); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 66 | |
Jens Taprogge | 459e6d7 | 2012-09-12 14:55:27 +0200 | [diff] [blame] | 67 | iowrite8(CR_ENABLE_RX, &channel->regs->w.cr); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int ipoctal_open(struct tty_struct *tty, struct file *file) |
| 72 | { |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 73 | int res; |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 74 | struct ipoctal_channel *channel; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 75 | |
Jens Taprogge | 9c1d784 | 2012-09-12 14:55:42 +0200 | [diff] [blame] | 76 | channel = dev_get_drvdata(tty->dev); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 77 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 78 | if (atomic_read(&channel->open)) |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 79 | return -EBUSY; |
| 80 | |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 81 | tty->driver_data = channel; |
Samuel Iglesias Gonsálvez | 1337b07 | 2012-07-13 13:33:13 +0200 | [diff] [blame] | 82 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 83 | res = tty_port_open(&channel->tty_port, tty, file); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 84 | if (res) |
| 85 | return res; |
| 86 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 87 | atomic_inc(&channel->open); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static void ipoctal_reset_stats(struct ipoctal_stats *stats) |
| 92 | { |
| 93 | stats->tx = 0; |
| 94 | stats->rx = 0; |
| 95 | stats->rcv_break = 0; |
| 96 | stats->framing_err = 0; |
| 97 | stats->overrun_err = 0; |
| 98 | stats->parity_err = 0; |
| 99 | } |
| 100 | |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 101 | static void ipoctal_free_channel(struct ipoctal_channel *channel) |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 102 | { |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 103 | ipoctal_reset_stats(&channel->stats); |
| 104 | channel->pointer_read = 0; |
| 105 | channel->pointer_write = 0; |
| 106 | channel->nb_bytes = 0; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void ipoctal_close(struct tty_struct *tty, struct file *filp) |
| 110 | { |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 111 | struct ipoctal_channel *channel = tty->driver_data; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 112 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 113 | tty_port_close(&channel->tty_port, tty, filp); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 114 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 115 | if (atomic_dec_and_test(&channel->open)) |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 116 | ipoctal_free_channel(channel); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int ipoctal_get_icount(struct tty_struct *tty, |
| 120 | struct serial_icounter_struct *icount) |
| 121 | { |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 122 | struct ipoctal_channel *channel = tty->driver_data; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 123 | |
| 124 | icount->cts = 0; |
| 125 | icount->dsr = 0; |
| 126 | icount->rng = 0; |
| 127 | icount->dcd = 0; |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 128 | icount->rx = channel->stats.rx; |
| 129 | icount->tx = channel->stats.tx; |
| 130 | icount->frame = channel->stats.framing_err; |
| 131 | icount->parity = channel->stats.parity_err; |
| 132 | icount->brk = channel->stats.rcv_break; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 136 | static void ipoctal_irq_rx(struct ipoctal_channel *channel, |
| 137 | struct tty_struct *tty, u8 sr) |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 138 | { |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 139 | struct tty_port *port = &channel->tty_port; |
Samuel Iglesias Gonsalvez | ab0a71f | 2012-09-12 14:55:43 +0200 | [diff] [blame] | 140 | unsigned char value; |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 141 | unsigned char flag = TTY_NORMAL; |
Samuel Iglesias Gonsalvez | ab0a71f | 2012-09-12 14:55:43 +0200 | [diff] [blame] | 142 | u8 isr; |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 143 | |
Samuel Iglesias Gonsalvez | ab0a71f | 2012-09-12 14:55:43 +0200 | [diff] [blame] | 144 | do { |
| 145 | value = ioread8(&channel->regs->r.rhr); |
| 146 | /* Error: count statistics */ |
| 147 | if (sr & SR_ERROR) { |
| 148 | iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr); |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 149 | |
Samuel Iglesias Gonsalvez | ab0a71f | 2012-09-12 14:55:43 +0200 | [diff] [blame] | 150 | if (sr & SR_OVERRUN_ERROR) { |
| 151 | channel->stats.overrun_err++; |
| 152 | /* Overrun doesn't affect the current character*/ |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 153 | tty_insert_flip_char(port, 0, TTY_OVERRUN); |
Samuel Iglesias Gonsalvez | ab0a71f | 2012-09-12 14:55:43 +0200 | [diff] [blame] | 154 | } |
| 155 | if (sr & SR_PARITY_ERROR) { |
| 156 | channel->stats.parity_err++; |
| 157 | flag = TTY_PARITY; |
| 158 | } |
| 159 | if (sr & SR_FRAMING_ERROR) { |
| 160 | channel->stats.framing_err++; |
| 161 | flag = TTY_FRAME; |
| 162 | } |
| 163 | if (sr & SR_RECEIVED_BREAK) { |
Samuel Iglesias Gonsalvez | 4514108 | 2012-09-13 12:32:22 +0200 | [diff] [blame] | 164 | iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr); |
Samuel Iglesias Gonsalvez | ab0a71f | 2012-09-12 14:55:43 +0200 | [diff] [blame] | 165 | channel->stats.rcv_break++; |
| 166 | flag = TTY_BREAK; |
| 167 | } |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 168 | } |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 169 | tty_insert_flip_char(port, value, flag); |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 170 | |
Samuel Iglesias Gonsalvez | ab0a71f | 2012-09-12 14:55:43 +0200 | [diff] [blame] | 171 | /* Check if there are more characters in RX FIFO |
| 172 | * If there are more, the isr register for this channel |
| 173 | * has enabled the RxRDY|FFULL bit. |
| 174 | */ |
| 175 | isr = ioread8(&channel->block_regs->r.isr); |
| 176 | sr = ioread8(&channel->regs->r.sr); |
| 177 | } while (isr & channel->isr_rx_rdy_mask); |
| 178 | |
| 179 | tty_flip_buffer_push(tty); |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static void ipoctal_irq_tx(struct ipoctal_channel *channel) |
| 183 | { |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 184 | unsigned char value; |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 185 | unsigned int *pointer_write = &channel->pointer_write; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 186 | |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 187 | if (channel->nb_bytes <= 0) { |
| 188 | channel->nb_bytes = 0; |
| 189 | return; |
| 190 | } |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 191 | |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 192 | value = channel->tty_port.xmit_buf[*pointer_write]; |
| 193 | iowrite8(value, &channel->regs->w.thr); |
| 194 | channel->stats.tx++; |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 195 | (*pointer_write)++; |
| 196 | *pointer_write = *pointer_write % PAGE_SIZE; |
| 197 | channel->nb_bytes--; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 198 | |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 199 | if ((channel->nb_bytes == 0) && |
| 200 | (waitqueue_active(&channel->queue))) { |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 201 | |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 202 | if (channel->board_id != IPACK1_DEVICE_ID_SBS_OCTAL_485) { |
| 203 | *channel->board_write = 1; |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 204 | wake_up_interruptible(&channel->queue); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 205 | } |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 206 | } |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void ipoctal_irq_channel(struct ipoctal_channel *channel) |
| 210 | { |
| 211 | u8 isr, sr; |
| 212 | struct tty_struct *tty; |
| 213 | |
| 214 | /* If there is no client, skip the check */ |
| 215 | if (!atomic_read(&channel->open)) |
| 216 | return; |
| 217 | |
| 218 | tty = tty_port_tty_get(&channel->tty_port); |
| 219 | if (!tty) |
| 220 | return; |
| 221 | /* The HW is organized in pair of channels. See which register we need |
| 222 | * to read from */ |
| 223 | isr = ioread8(&channel->block_regs->r.isr); |
| 224 | sr = ioread8(&channel->regs->r.sr); |
| 225 | |
| 226 | /* In case of RS-485, change from TX to RX when finishing TX. |
| 227 | * Half-duplex. */ |
| 228 | if ((channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) && |
| 229 | (sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) { |
| 230 | iowrite8(CR_DISABLE_TX, &channel->regs->w.cr); |
| 231 | iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr); |
| 232 | iowrite8(CR_ENABLE_RX, &channel->regs->w.cr); |
| 233 | *channel->board_write = 1; |
| 234 | wake_up_interruptible(&channel->queue); |
| 235 | } |
| 236 | |
| 237 | /* RX data */ |
| 238 | if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY)) |
| 239 | ipoctal_irq_rx(channel, tty, sr); |
| 240 | |
| 241 | /* TX of each character */ |
| 242 | if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY)) |
| 243 | ipoctal_irq_tx(channel); |
| 244 | |
| 245 | tty_flip_buffer_push(tty); |
| 246 | tty_kref_put(tty); |
| 247 | } |
| 248 | |
Jens Taprogge | faa75c4 | 2012-09-12 14:55:38 +0200 | [diff] [blame] | 249 | static irqreturn_t ipoctal_irq_handler(void *arg) |
Jens Taprogge | 87cfb95 | 2012-09-12 14:55:30 +0200 | [diff] [blame] | 250 | { |
| 251 | unsigned int i; |
| 252 | struct ipoctal *ipoctal = (struct ipoctal *) arg; |
| 253 | |
| 254 | /* Check all channels */ |
| 255 | for (i = 0; i < NR_CHANNELS; i++) |
| 256 | ipoctal_irq_channel(&ipoctal->channel[i]); |
| 257 | |
Jens Taprogge | ea99114 | 2012-09-13 12:32:20 +0200 | [diff] [blame] | 258 | /* Clear the IPack device interrupt */ |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 259 | readw(ipoctal->int_space + ACK_INT_REQ0); |
| 260 | readw(ipoctal->int_space + ACK_INT_REQ1); |
Jens Taprogge | ea99114 | 2012-09-13 12:32:20 +0200 | [diff] [blame] | 261 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 262 | return IRQ_HANDLED; |
| 263 | } |
| 264 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 265 | static const struct tty_port_operations ipoctal_tty_port_ops = { |
| 266 | .dtr_rts = NULL, |
| 267 | .activate = ipoctal_port_activate, |
| 268 | }; |
| 269 | |
| 270 | static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr, |
Jens Taprogge | c6e2dfa | 2012-09-13 12:32:21 +0200 | [diff] [blame] | 271 | unsigned int slot) |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 272 | { |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 273 | int res; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 274 | int i; |
| 275 | struct tty_driver *tty; |
| 276 | char name[20]; |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 277 | struct ipoctal_channel *channel; |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 278 | struct ipack_region *region; |
| 279 | void __iomem *addr; |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 280 | union scc2698_channel __iomem *chan_regs; |
| 281 | union scc2698_block __iomem *block_regs; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 282 | |
Jens Taprogge | 2ec678d | 2012-09-27 12:37:33 +0200 | [diff] [blame] | 283 | ipoctal->board_id = ipoctal->dev->id_device; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 284 | |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 285 | region = &ipoctal->dev->region[IPACK_IO_SPACE]; |
| 286 | addr = devm_ioremap_nocache(&ipoctal->dev->dev, |
| 287 | region->start, region->size); |
| 288 | if (!addr) { |
Samuel Iglesias Gonsalvez | 42b3820 | 2012-05-25 13:08:13 +0200 | [diff] [blame] | 289 | dev_err(&ipoctal->dev->dev, |
| 290 | "Unable to map slot [%d:%d] IO space!\n", |
| 291 | bus_nr, slot); |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 292 | return -EADDRNOTAVAIL; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 293 | } |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 294 | /* Save the virtual address to access the registers easily */ |
| 295 | chan_regs = |
| 296 | (union scc2698_channel __iomem *) addr; |
| 297 | block_regs = |
| 298 | (union scc2698_block __iomem *) addr; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 299 | |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 300 | region = &ipoctal->dev->region[IPACK_INT_SPACE]; |
| 301 | ipoctal->int_space = |
| 302 | devm_ioremap_nocache(&ipoctal->dev->dev, |
| 303 | region->start, region->size); |
| 304 | if (!ipoctal->int_space) { |
Jens Taprogge | ea99114 | 2012-09-13 12:32:20 +0200 | [diff] [blame] | 305 | dev_err(&ipoctal->dev->dev, |
| 306 | "Unable to map slot [%d:%d] INT space!\n", |
| 307 | bus_nr, slot); |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 308 | return -EADDRNOTAVAIL; |
Jens Taprogge | ea99114 | 2012-09-13 12:32:20 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Jens Taprogge | fe4a3ed | 2012-09-27 12:37:36 +0200 | [diff] [blame] | 311 | region = &ipoctal->dev->region[IPACK_MEM8_SPACE]; |
| 312 | ipoctal->mem8_space = |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 313 | devm_ioremap_nocache(&ipoctal->dev->dev, |
| 314 | region->start, 0x8000); |
| 315 | if (!addr) { |
Samuel Iglesias Gonsalvez | 42b3820 | 2012-05-25 13:08:13 +0200 | [diff] [blame] | 316 | dev_err(&ipoctal->dev->dev, |
Jens Taprogge | fe4a3ed | 2012-09-27 12:37:36 +0200 | [diff] [blame] | 317 | "Unable to map slot [%d:%d] MEM8 space!\n", |
Samuel Iglesias Gonsalvez | 42b3820 | 2012-05-25 13:08:13 +0200 | [diff] [blame] | 318 | bus_nr, slot); |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 319 | return -EADDRNOTAVAIL; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 322 | |
| 323 | /* Disable RX and TX before touching anything */ |
| 324 | for (i = 0; i < NR_CHANNELS ; i++) { |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 325 | struct ipoctal_channel *channel = &ipoctal->channel[i]; |
| 326 | channel->regs = chan_regs + i; |
| 327 | channel->block_regs = block_regs + (i >> 1); |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 328 | channel->board_write = &ipoctal->write; |
| 329 | channel->board_id = ipoctal->board_id; |
Jens Taprogge | 4e4732a | 2012-09-12 14:55:29 +0200 | [diff] [blame] | 330 | if (i & 1) { |
| 331 | channel->isr_tx_rdy_mask = ISR_TxRDY_B; |
| 332 | channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B; |
| 333 | } else { |
| 334 | channel->isr_tx_rdy_mask = ISR_TxRDY_A; |
| 335 | channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A; |
| 336 | } |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 337 | |
Jens Taprogge | 459e6d7 | 2012-09-12 14:55:27 +0200 | [diff] [blame] | 338 | iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr); |
| 339 | iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr); |
| 340 | iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr); |
| 341 | iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY, |
| 342 | &channel->regs->w.mr); /* mr1 */ |
| 343 | iowrite8(0, &channel->regs->w.mr); /* mr2 */ |
| 344 | iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) { |
Jens Taprogge | 459e6d7 | 2012-09-12 14:55:27 +0200 | [diff] [blame] | 348 | iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr); |
| 349 | iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN, |
| 350 | &block_regs[i].w.opcr); |
| 351 | iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A | |
| 352 | IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B, |
| 353 | &block_regs[i].w.imr); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* |
| 357 | * IP-OCTAL has different addresses to copy its IRQ vector. |
| 358 | * Depending of the carrier these addresses are accesible or not. |
| 359 | * More info in the datasheet. |
| 360 | */ |
Jens Taprogge | c6e2dfa | 2012-09-13 12:32:21 +0200 | [diff] [blame] | 361 | ipoctal->dev->bus->ops->request_irq(ipoctal->dev, |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 362 | ipoctal_irq_handler, ipoctal); |
Jens Taprogge | c6e2dfa | 2012-09-13 12:32:21 +0200 | [diff] [blame] | 363 | /* Dummy write */ |
Jens Taprogge | fe4a3ed | 2012-09-27 12:37:36 +0200 | [diff] [blame] | 364 | iowrite8(1, ipoctal->mem8_space + 1); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 365 | |
| 366 | /* Register the TTY device */ |
| 367 | |
| 368 | /* Each IP-OCTAL channel is a TTY port */ |
| 369 | tty = alloc_tty_driver(NR_CHANNELS); |
| 370 | |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 371 | if (!tty) |
| 372 | return -ENOMEM; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 373 | |
| 374 | /* Fill struct tty_driver with ipoctal data */ |
| 375 | tty->owner = THIS_MODULE; |
Jens Taprogge | 3f3a592 | 2012-09-12 14:55:41 +0200 | [diff] [blame] | 376 | tty->driver_name = KBUILD_MODNAME; |
| 377 | sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 378 | tty->name = name; |
| 379 | tty->major = 0; |
| 380 | |
| 381 | tty->minor_start = 0; |
| 382 | tty->type = TTY_DRIVER_TYPE_SERIAL; |
| 383 | tty->subtype = SERIAL_TYPE_NORMAL; |
| 384 | tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
| 385 | tty->init_termios = tty_std_termios; |
| 386 | tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
| 387 | tty->init_termios.c_ispeed = 9600; |
| 388 | tty->init_termios.c_ospeed = 9600; |
| 389 | |
| 390 | tty_set_operations(tty, &ipoctal_fops); |
| 391 | res = tty_register_driver(tty); |
| 392 | if (res) { |
Samuel Iglesias Gonsalvez | 42b3820 | 2012-05-25 13:08:13 +0200 | [diff] [blame] | 393 | dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n"); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 394 | put_tty_driver(tty); |
Jens Taprogge | 402228d | 2012-09-27 12:37:34 +0200 | [diff] [blame] | 395 | return res; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* Save struct tty_driver for use it when uninstalling the device */ |
| 399 | ipoctal->tty_drv = tty; |
| 400 | |
| 401 | for (i = 0; i < NR_CHANNELS; i++) { |
Jens Taprogge | 2afb41d | 2012-09-12 14:55:40 +0200 | [diff] [blame] | 402 | struct device *tty_dev; |
| 403 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 404 | channel = &ipoctal->channel[i]; |
| 405 | tty_port_init(&channel->tty_port); |
| 406 | tty_port_alloc_xmit_buf(&channel->tty_port); |
| 407 | channel->tty_port.ops = &ipoctal_tty_port_ops; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 408 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 409 | ipoctal_reset_stats(&channel->stats); |
| 410 | channel->nb_bytes = 0; |
| 411 | init_waitqueue_head(&channel->queue); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 412 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 413 | spin_lock_init(&channel->lock); |
| 414 | channel->pointer_read = 0; |
| 415 | channel->pointer_write = 0; |
Linus Torvalds | 3498d13 | 2012-10-01 12:26:52 -0700 | [diff] [blame] | 416 | tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL); |
Jens Taprogge | 2afb41d | 2012-09-12 14:55:40 +0200 | [diff] [blame] | 417 | if (IS_ERR(tty_dev)) { |
| 418 | dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n"); |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 419 | tty_port_destroy(&channel->tty_port); |
Jens Taprogge | 2afb41d | 2012-09-12 14:55:40 +0200 | [diff] [blame] | 420 | continue; |
| 421 | } |
Jens Taprogge | 9c1d784 | 2012-09-12 14:55:42 +0200 | [diff] [blame] | 422 | dev_set_drvdata(tty_dev, channel); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 423 | |
| 424 | /* |
| 425 | * Enable again the RX. TX will be enabled when |
| 426 | * there is something to send |
| 427 | */ |
Jens Taprogge | 459e6d7 | 2012-09-12 14:55:27 +0200 | [diff] [blame] | 428 | iowrite8(CR_ENABLE_RX, &channel->regs->w.cr); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | return 0; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 434 | static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel, |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 435 | const unsigned char *buf, |
| 436 | int count) |
| 437 | { |
| 438 | unsigned long flags; |
| 439 | int i; |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 440 | unsigned int *pointer_read = &channel->pointer_read; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 441 | |
| 442 | /* Copy the bytes from the user buffer to the internal one */ |
| 443 | for (i = 0; i < count; i++) { |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 444 | if (i <= (PAGE_SIZE - channel->nb_bytes)) { |
| 445 | spin_lock_irqsave(&channel->lock, flags); |
| 446 | channel->tty_port.xmit_buf[*pointer_read] = buf[i]; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 447 | *pointer_read = (*pointer_read + 1) % PAGE_SIZE; |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 448 | channel->nb_bytes++; |
| 449 | spin_unlock_irqrestore(&channel->lock, flags); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 450 | } else { |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | return i; |
| 455 | } |
| 456 | |
Jens Taprogge | 699a89f | 2012-09-12 14:55:31 +0200 | [diff] [blame] | 457 | static int ipoctal_write_tty(struct tty_struct *tty, |
| 458 | const unsigned char *buf, int count) |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 459 | { |
Jens Taprogge | 699a89f | 2012-09-12 14:55:31 +0200 | [diff] [blame] | 460 | struct ipoctal_channel *channel = tty->driver_data; |
Samuel Iglesias Gonsalvez | d046006 | 2012-09-13 12:32:23 +0200 | [diff] [blame] | 461 | unsigned int char_copied; |
Jens Taprogge | 699a89f | 2012-09-12 14:55:31 +0200 | [diff] [blame] | 462 | |
Samuel Iglesias Gonsalvez | d046006 | 2012-09-13 12:32:23 +0200 | [diff] [blame] | 463 | char_copied = ipoctal_copy_write_buffer(channel, buf, count); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 464 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 465 | /* As the IP-OCTAL 485 only supports half duplex, do it manually */ |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 466 | if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) { |
Jens Taprogge | 459e6d7 | 2012-09-12 14:55:27 +0200 | [diff] [blame] | 467 | iowrite8(CR_DISABLE_RX, &channel->regs->w.cr); |
| 468 | iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* |
| 472 | * Send a packet and then disable TX to avoid failure after several send |
| 473 | * operations |
| 474 | */ |
Jens Taprogge | 459e6d7 | 2012-09-12 14:55:27 +0200 | [diff] [blame] | 475 | iowrite8(CR_ENABLE_TX, &channel->regs->w.cr); |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 476 | wait_event_interruptible(channel->queue, *channel->board_write); |
Jens Taprogge | 459e6d7 | 2012-09-12 14:55:27 +0200 | [diff] [blame] | 477 | iowrite8(CR_DISABLE_TX, &channel->regs->w.cr); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 478 | |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 479 | *channel->board_write = 0; |
Samuel Iglesias Gonsalvez | d046006 | 2012-09-13 12:32:23 +0200 | [diff] [blame] | 480 | return char_copied; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 483 | static int ipoctal_write_room(struct tty_struct *tty) |
| 484 | { |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 485 | struct ipoctal_channel *channel = tty->driver_data; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 486 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 487 | return PAGE_SIZE - channel->nb_bytes; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static int ipoctal_chars_in_buffer(struct tty_struct *tty) |
| 491 | { |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 492 | struct ipoctal_channel *channel = tty->driver_data; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 493 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 494 | return channel->nb_bytes; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static void ipoctal_set_termios(struct tty_struct *tty, |
| 498 | struct ktermios *old_termios) |
| 499 | { |
| 500 | unsigned int cflag; |
| 501 | unsigned char mr1 = 0; |
| 502 | unsigned char mr2 = 0; |
| 503 | unsigned char csr = 0; |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 504 | struct ipoctal_channel *channel = tty->driver_data; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 505 | speed_t baud; |
| 506 | |
Alan Cox | 857196e | 2012-07-26 19:00:51 +0100 | [diff] [blame] | 507 | cflag = tty->termios.c_cflag; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 508 | |
| 509 | /* Disable and reset everything before change the setup */ |
Jens Taprogge | 459e6d7 | 2012-09-12 14:55:27 +0200 | [diff] [blame] | 510 | iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr); |
| 511 | iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr); |
| 512 | iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr); |
| 513 | iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr); |
| 514 | iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 515 | |
| 516 | /* Set Bits per chars */ |
| 517 | switch (cflag & CSIZE) { |
| 518 | case CS6: |
| 519 | mr1 |= MR1_CHRL_6_BITS; |
| 520 | break; |
| 521 | case CS7: |
| 522 | mr1 |= MR1_CHRL_7_BITS; |
| 523 | break; |
| 524 | case CS8: |
| 525 | default: |
| 526 | mr1 |= MR1_CHRL_8_BITS; |
| 527 | /* By default, select CS8 */ |
Alan Cox | 857196e | 2012-07-26 19:00:51 +0100 | [diff] [blame] | 528 | tty->termios.c_cflag = (cflag & ~CSIZE) | CS8; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 529 | break; |
| 530 | } |
| 531 | |
| 532 | /* Set Parity */ |
| 533 | if (cflag & PARENB) |
| 534 | if (cflag & PARODD) |
| 535 | mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD; |
| 536 | else |
| 537 | mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN; |
| 538 | else |
| 539 | mr1 |= MR1_PARITY_OFF; |
| 540 | |
| 541 | /* Mark or space parity is not supported */ |
Alan Cox | 857196e | 2012-07-26 19:00:51 +0100 | [diff] [blame] | 542 | tty->termios.c_cflag &= ~CMSPAR; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 543 | |
| 544 | /* Set stop bits */ |
| 545 | if (cflag & CSTOPB) |
| 546 | mr2 |= MR2_STOP_BITS_LENGTH_2; |
| 547 | else |
| 548 | mr2 |= MR2_STOP_BITS_LENGTH_1; |
| 549 | |
| 550 | /* Set the flow control */ |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 551 | switch (channel->board_id) { |
Jens Taprogge | 7db5e3c | 2012-09-04 17:01:16 +0200 | [diff] [blame] | 552 | case IPACK1_DEVICE_ID_SBS_OCTAL_232: |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 553 | if (cflag & CRTSCTS) { |
| 554 | mr1 |= MR1_RxRTS_CONTROL_ON; |
| 555 | mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 556 | } else { |
| 557 | mr1 |= MR1_RxRTS_CONTROL_OFF; |
| 558 | mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 559 | } |
| 560 | break; |
Jens Taprogge | 7db5e3c | 2012-09-04 17:01:16 +0200 | [diff] [blame] | 561 | case IPACK1_DEVICE_ID_SBS_OCTAL_422: |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 562 | mr1 |= MR1_RxRTS_CONTROL_OFF; |
| 563 | mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 564 | break; |
Jens Taprogge | 7db5e3c | 2012-09-04 17:01:16 +0200 | [diff] [blame] | 565 | case IPACK1_DEVICE_ID_SBS_OCTAL_485: |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 566 | mr1 |= MR1_RxRTS_CONTROL_OFF; |
| 567 | mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 568 | break; |
| 569 | default: |
| 570 | return; |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | baud = tty_get_baud_rate(tty); |
Alan Cox | 857196e | 2012-07-26 19:00:51 +0100 | [diff] [blame] | 575 | tty_termios_encode_baud_rate(&tty->termios, baud, baud); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 576 | |
| 577 | /* Set baud rate */ |
Alan Cox | 857196e | 2012-07-26 19:00:51 +0100 | [diff] [blame] | 578 | switch (baud) { |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 579 | case 75: |
| 580 | csr |= TX_CLK_75 | RX_CLK_75; |
| 581 | break; |
| 582 | case 110: |
| 583 | csr |= TX_CLK_110 | RX_CLK_110; |
| 584 | break; |
| 585 | case 150: |
| 586 | csr |= TX_CLK_150 | RX_CLK_150; |
| 587 | break; |
| 588 | case 300: |
| 589 | csr |= TX_CLK_300 | RX_CLK_300; |
| 590 | break; |
| 591 | case 600: |
| 592 | csr |= TX_CLK_600 | RX_CLK_600; |
| 593 | break; |
| 594 | case 1200: |
| 595 | csr |= TX_CLK_1200 | RX_CLK_1200; |
| 596 | break; |
| 597 | case 1800: |
| 598 | csr |= TX_CLK_1800 | RX_CLK_1800; |
| 599 | break; |
| 600 | case 2000: |
| 601 | csr |= TX_CLK_2000 | RX_CLK_2000; |
| 602 | break; |
| 603 | case 2400: |
| 604 | csr |= TX_CLK_2400 | RX_CLK_2400; |
| 605 | break; |
| 606 | case 4800: |
| 607 | csr |= TX_CLK_4800 | RX_CLK_4800; |
| 608 | break; |
| 609 | case 9600: |
| 610 | csr |= TX_CLK_9600 | RX_CLK_9600; |
| 611 | break; |
| 612 | case 19200: |
| 613 | csr |= TX_CLK_19200 | RX_CLK_19200; |
| 614 | break; |
| 615 | case 38400: |
| 616 | default: |
| 617 | csr |= TX_CLK_38400 | RX_CLK_38400; |
| 618 | /* In case of default, we establish 38400 bps */ |
Alan Cox | 857196e | 2012-07-26 19:00:51 +0100 | [diff] [blame] | 619 | tty_termios_encode_baud_rate(&tty->termios, 38400, 38400); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 620 | break; |
| 621 | } |
| 622 | |
| 623 | mr1 |= MR1_ERROR_CHAR; |
| 624 | mr1 |= MR1_RxINT_RxRDY; |
| 625 | |
| 626 | /* Write the control registers */ |
Jens Taprogge | 459e6d7 | 2012-09-12 14:55:27 +0200 | [diff] [blame] | 627 | iowrite8(mr1, &channel->regs->w.mr); |
| 628 | iowrite8(mr2, &channel->regs->w.mr); |
| 629 | iowrite8(csr, &channel->regs->w.csr); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 630 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 631 | /* Enable again the RX */ |
Jens Taprogge | 459e6d7 | 2012-09-12 14:55:27 +0200 | [diff] [blame] | 632 | iowrite8(CR_ENABLE_RX, &channel->regs->w.cr); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static void ipoctal_hangup(struct tty_struct *tty) |
| 636 | { |
| 637 | unsigned long flags; |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 638 | struct ipoctal_channel *channel = tty->driver_data; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 639 | |
Jens Taprogge | ef79de0 | 2012-09-12 14:55:28 +0200 | [diff] [blame] | 640 | if (channel == NULL) |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 641 | return; |
| 642 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 643 | spin_lock_irqsave(&channel->lock, flags); |
| 644 | channel->nb_bytes = 0; |
| 645 | channel->pointer_read = 0; |
| 646 | channel->pointer_write = 0; |
| 647 | spin_unlock_irqrestore(&channel->lock, flags); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 648 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 649 | tty_port_hangup(&channel->tty_port); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 650 | |
Jens Taprogge | 459e6d7 | 2012-09-12 14:55:27 +0200 | [diff] [blame] | 651 | iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr); |
| 652 | iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr); |
| 653 | iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr); |
| 654 | iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr); |
| 655 | iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 656 | |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 657 | clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags); |
| 658 | wake_up_interruptible(&channel->tty_port.open_wait); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static const struct tty_operations ipoctal_fops = { |
| 662 | .ioctl = NULL, |
| 663 | .open = ipoctal_open, |
| 664 | .close = ipoctal_close, |
| 665 | .write = ipoctal_write_tty, |
| 666 | .set_termios = ipoctal_set_termios, |
| 667 | .write_room = ipoctal_write_room, |
| 668 | .chars_in_buffer = ipoctal_chars_in_buffer, |
| 669 | .get_icount = ipoctal_get_icount, |
| 670 | .hangup = ipoctal_hangup, |
| 671 | }; |
| 672 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 673 | static int ipoctal_probe(struct ipack_device *dev) |
| 674 | { |
| 675 | int res; |
| 676 | struct ipoctal *ipoctal; |
| 677 | |
| 678 | ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL); |
| 679 | if (ipoctal == NULL) |
| 680 | return -ENOMEM; |
| 681 | |
| 682 | ipoctal->dev = dev; |
Jens Taprogge | f9e314d | 2012-09-27 12:37:25 +0200 | [diff] [blame] | 683 | res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 684 | if (res) |
| 685 | goto out_uninst; |
| 686 | |
Jens Taprogge | 1adda49 | 2012-09-12 14:55:39 +0200 | [diff] [blame] | 687 | dev_set_drvdata(&dev->dev, ipoctal); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 688 | return 0; |
| 689 | |
| 690 | out_uninst: |
| 691 | kfree(ipoctal); |
| 692 | return res; |
| 693 | } |
| 694 | |
| 695 | static void __ipoctal_remove(struct ipoctal *ipoctal) |
| 696 | { |
| 697 | int i; |
| 698 | |
Samuel Iglesias Gonsálvez | 690949e | 2012-09-11 13:35:08 +0200 | [diff] [blame] | 699 | ipoctal->dev->bus->ops->free_irq(ipoctal->dev); |
| 700 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 701 | for (i = 0; i < NR_CHANNELS; i++) { |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 702 | struct ipoctal_channel *channel = &ipoctal->channel[i]; |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 703 | tty_unregister_device(ipoctal->tty_drv, i); |
Jens Taprogge | 70a3281 | 2012-09-12 14:55:26 +0200 | [diff] [blame] | 704 | tty_port_free_xmit_buf(&channel->tty_port); |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 705 | tty_port_destroy(&channel->tty_port); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | tty_unregister_driver(ipoctal->tty_drv); |
| 709 | put_tty_driver(ipoctal->tty_drv); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 710 | kfree(ipoctal); |
| 711 | } |
| 712 | |
Jens Taprogge | 1adda49 | 2012-09-12 14:55:39 +0200 | [diff] [blame] | 713 | static void ipoctal_remove(struct ipack_device *idev) |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 714 | { |
Jens Taprogge | 1adda49 | 2012-09-12 14:55:39 +0200 | [diff] [blame] | 715 | __ipoctal_remove(dev_get_drvdata(&idev->dev)); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Jens Taprogge | fdfc8cf | 2012-09-04 17:01:18 +0200 | [diff] [blame] | 718 | static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = { |
| 719 | { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS, |
| 720 | IPACK1_DEVICE_ID_SBS_OCTAL_232) }, |
| 721 | { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS, |
| 722 | IPACK1_DEVICE_ID_SBS_OCTAL_422) }, |
| 723 | { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS, |
| 724 | IPACK1_DEVICE_ID_SBS_OCTAL_485) }, |
| 725 | { 0, }, |
| 726 | }; |
| 727 | |
| 728 | MODULE_DEVICE_TABLE(ipack, ipoctal_ids); |
| 729 | |
Jens Taprogge | e801113 | 2012-09-04 17:01:17 +0200 | [diff] [blame] | 730 | static const struct ipack_driver_ops ipoctal_drv_ops = { |
Jens Taprogge | 4aa09d4 | 2012-09-04 17:01:19 +0200 | [diff] [blame] | 731 | .probe = ipoctal_probe, |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 732 | .remove = ipoctal_remove, |
| 733 | }; |
| 734 | |
Jens Taprogge | e801113 | 2012-09-04 17:01:17 +0200 | [diff] [blame] | 735 | static struct ipack_driver driver = { |
| 736 | .ops = &ipoctal_drv_ops, |
Jens Taprogge | fdfc8cf | 2012-09-04 17:01:18 +0200 | [diff] [blame] | 737 | .id_table = ipoctal_ids, |
Jens Taprogge | e801113 | 2012-09-04 17:01:17 +0200 | [diff] [blame] | 738 | }; |
| 739 | |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 740 | static int __init ipoctal_init(void) |
| 741 | { |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 742 | return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME); |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | static void __exit ipoctal_exit(void) |
| 746 | { |
Samuel Iglesias Gonsalvez | ba4dc61 | 2012-05-09 15:27:21 +0200 | [diff] [blame] | 747 | ipack_driver_unregister(&driver); |
| 748 | } |
| 749 | |
| 750 | MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver"); |
| 751 | MODULE_LICENSE("GPL"); |
| 752 | |
| 753 | module_init(ipoctal_init); |
| 754 | module_exit(ipoctal_exit); |