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