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