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