blob: a1514a79e4870edc4310f84c805f157641e61587 [file] [log] [blame]
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +02001/**
2 * ipoctal.c
3 *
4 * driver for the GE IP-OCTAL boards
Samuel Iglesias Gonsalvez76859722012-11-16 16:19:58 +01005 *
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 Gonsalvezba4dc612012-05-09 15:27:21 +02009 *
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 Gonsalvez32254362012-05-11 10:17:15 +020012 * Software Foundation; version 2 of the License.
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020013 */
14
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020015#include <linux/device.h>
16#include <linux/module.h>
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020017#include <linux/interrupt.h>
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020018#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 Taprogge64802dc2012-09-04 17:01:08 +020023#include <linux/io.h>
Samuel Iglesias Gonsalvez7dbce022012-11-16 19:33:45 +010024#include <linux/ipack.h>
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020025#include "ipoctal.h"
26#include "scc2698.h"
27
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020028#define IP_OCTAL_ID_SPACE_VECTOR 0x41
29#define IP_OCTAL_NB_BLOCKS 4
30
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020031static const struct tty_operations ipoctal_fops;
32
Jens Taprogge70a32812012-09-12 14:55:26 +020033struct ipoctal_channel {
34 struct ipoctal_stats stats;
35 unsigned int nb_bytes;
Jens Taprogge70a32812012-09-12 14:55:26 +020036 wait_queue_head_t queue;
37 spinlock_t lock;
38 unsigned int pointer_read;
39 unsigned int pointer_write;
Jens Taprogge70a32812012-09-12 14:55:26 +020040 struct tty_port tty_port;
41 union scc2698_channel __iomem *regs;
42 union scc2698_block __iomem *block_regs;
Jens Taproggeef79de02012-09-12 14:55:28 +020043 unsigned int board_id;
Jens Taprogge4e4732a2012-09-12 14:55:29 +020044 u8 isr_rx_rdy_mask;
45 u8 isr_tx_rdy_mask;
Samuel Iglesias Gonsalvezb0d17fb2012-12-10 11:50:07 +010046 unsigned int rx_enable;
Jens Taprogge70a32812012-09-12 14:55:26 +020047};
48
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020049struct ipoctal {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020050 struct ipack_device *dev;
51 unsigned int board_id;
Jens Taprogge70a32812012-09-12 14:55:26 +020052 struct ipoctal_channel channel[NR_CHANNELS];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020053 struct tty_driver *tty_drv;
Jens Taproggefe4a3ed2012-09-27 12:37:36 +020054 u8 __iomem *mem8_space;
Jens Taprogge402228d2012-09-27 12:37:34 +020055 u8 __iomem *int_space;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020056};
57
Federico Vaga78f22bc2014-09-02 17:31:39 +020058static void ipoctal_reset_channel(struct ipoctal_channel *channel)
59{
60 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
61 channel->rx_enable = 0;
62 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
63 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
64 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
65 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
66}
67
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020068static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
69{
Jens Taprogge70a32812012-09-12 14:55:26 +020070 struct ipoctal_channel *channel;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020071
Jens Taprogge9c1d7842012-09-12 14:55:42 +020072 channel = dev_get_drvdata(tty->dev);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020073
Samuel Iglesias Gonsalveza3882b72012-12-10 11:50:03 +010074 /*
75 * Enable RX. TX will be enabled when
76 * there is something to send
77 */
Jens Taprogge459e6d72012-09-12 14:55:27 +020078 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezb0d17fb2012-12-10 11:50:07 +010079 channel->rx_enable = 1;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020080 return 0;
81}
82
83static int ipoctal_open(struct tty_struct *tty, struct file *file)
84{
Jens Taprogge70a32812012-09-12 14:55:26 +020085 struct ipoctal_channel *channel;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020086
Jens Taprogge9c1d7842012-09-12 14:55:42 +020087 channel = dev_get_drvdata(tty->dev);
Jens Taproggeef79de02012-09-12 14:55:28 +020088 tty->driver_data = channel;
Samuel Iglesias Gonsálvez1337b072012-07-13 13:33:13 +020089
Samuel Iglesias Gonsalvez7e5730d2012-12-10 11:49:59 +010090 return tty_port_open(&channel->tty_port, tty, file);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020091}
92
93static void ipoctal_reset_stats(struct ipoctal_stats *stats)
94{
95 stats->tx = 0;
96 stats->rx = 0;
97 stats->rcv_break = 0;
98 stats->framing_err = 0;
99 stats->overrun_err = 0;
100 stats->parity_err = 0;
101}
102
Jens Taproggeef79de02012-09-12 14:55:28 +0200103static void ipoctal_free_channel(struct ipoctal_channel *channel)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200104{
Jens Taprogge70a32812012-09-12 14:55:26 +0200105 ipoctal_reset_stats(&channel->stats);
106 channel->pointer_read = 0;
107 channel->pointer_write = 0;
108 channel->nb_bytes = 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200109}
110
111static void ipoctal_close(struct tty_struct *tty, struct file *filp)
112{
Jens Taproggeef79de02012-09-12 14:55:28 +0200113 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200114
Jens Taprogge70a32812012-09-12 14:55:26 +0200115 tty_port_close(&channel->tty_port, tty, filp);
Samuel Iglesias Gonsalvez7e5730d2012-12-10 11:49:59 +0100116 ipoctal_free_channel(channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200117}
118
119static int ipoctal_get_icount(struct tty_struct *tty,
120 struct serial_icounter_struct *icount)
121{
Jens Taproggeef79de02012-09-12 14:55:28 +0200122 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200123
124 icount->cts = 0;
125 icount->dsr = 0;
126 icount->rng = 0;
127 icount->dcd = 0;
Jens Taprogge70a32812012-09-12 14:55:26 +0200128 icount->rx = channel->stats.rx;
129 icount->tx = channel->stats.tx;
130 icount->frame = channel->stats.framing_err;
131 icount->parity = channel->stats.parity_err;
132 icount->brk = channel->stats.rcv_break;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200133 return 0;
134}
135
Jiri Slaby2e124b42013-01-03 15:53:06 +0100136static void ipoctal_irq_rx(struct ipoctal_channel *channel, u8 sr)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200137{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100138 struct tty_port *port = &channel->tty_port;
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200139 unsigned char value;
Samuel Iglesias Gonsalvezb5071f22012-12-10 11:50:01 +0100140 unsigned char flag;
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200141 u8 isr;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200142
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200143 do {
144 value = ioread8(&channel->regs->r.rhr);
Samuel Iglesias Gonsalvezb5071f22012-12-10 11:50:01 +0100145 flag = TTY_NORMAL;
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200146 /* Error: count statistics */
147 if (sr & SR_ERROR) {
148 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200149
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200150 if (sr & SR_OVERRUN_ERROR) {
151 channel->stats.overrun_err++;
152 /* Overrun doesn't affect the current character*/
Jiri Slaby92a19f92013-01-03 15:53:03 +0100153 tty_insert_flip_char(port, 0, TTY_OVERRUN);
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200154 }
155 if (sr & SR_PARITY_ERROR) {
156 channel->stats.parity_err++;
157 flag = TTY_PARITY;
158 }
159 if (sr & SR_FRAMING_ERROR) {
160 channel->stats.framing_err++;
161 flag = TTY_FRAME;
162 }
163 if (sr & SR_RECEIVED_BREAK) {
Samuel Iglesias Gonsalvez45141082012-09-13 12:32:22 +0200164 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200165 channel->stats.rcv_break++;
166 flag = TTY_BREAK;
167 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200168 }
Jiri Slaby92a19f92013-01-03 15:53:03 +0100169 tty_insert_flip_char(port, value, flag);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200170
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200171 /* Check if there are more characters in RX FIFO
172 * If there are more, the isr register for this channel
173 * has enabled the RxRDY|FFULL bit.
174 */
175 isr = ioread8(&channel->block_regs->r.isr);
176 sr = ioread8(&channel->regs->r.sr);
177 } while (isr & channel->isr_rx_rdy_mask);
178
Jiri Slaby2e124b42013-01-03 15:53:06 +0100179 tty_flip_buffer_push(port);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200180}
181
182static void ipoctal_irq_tx(struct ipoctal_channel *channel)
183{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200184 unsigned char value;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200185 unsigned int *pointer_write = &channel->pointer_write;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200186
Alberto Garcia69a6b9b2012-12-10 11:49:58 +0100187 if (channel->nb_bytes == 0)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200188 return;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200189
Federico Vaga968d04e2014-06-26 09:46:24 +0200190 spin_lock(&channel->lock);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200191 value = channel->tty_port.xmit_buf[*pointer_write];
192 iowrite8(value, &channel->regs->w.thr);
193 channel->stats.tx++;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200194 (*pointer_write)++;
195 *pointer_write = *pointer_write % PAGE_SIZE;
196 channel->nb_bytes--;
Federico Vaga968d04e2014-06-26 09:46:24 +0200197 spin_unlock(&channel->lock);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200198}
199
200static void ipoctal_irq_channel(struct ipoctal_channel *channel)
201{
202 u8 isr, sr;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200203
Jens Taprogge87cfb952012-09-12 14:55:30 +0200204 /* The HW is organized in pair of channels. See which register we need
205 * to read from */
206 isr = ioread8(&channel->block_regs->r.isr);
207 sr = ioread8(&channel->regs->r.sr);
208
Samuel Iglesias Gonsalvez9d01b6f2012-12-10 11:50:02 +0100209 if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
Jens Taprogge87cfb952012-09-12 14:55:30 +0200210 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvez9d01b6f2012-12-10 11:50:02 +0100211 /* In case of RS-485, change from TX to RX when finishing TX.
212 * Half-duplex. */
213 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
214 iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
215 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvez2910fe22013-01-18 08:57:21 +0100216 channel->rx_enable = 1;
Samuel Iglesias Gonsalvez9d01b6f2012-12-10 11:50:02 +0100217 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200218 }
219
220 /* RX data */
221 if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
Jiri Slaby2e124b42013-01-03 15:53:06 +0100222 ipoctal_irq_rx(channel, sr);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200223
224 /* TX of each character */
225 if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
226 ipoctal_irq_tx(channel);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200227}
228
Jens Taproggefaa75c42012-09-12 14:55:38 +0200229static irqreturn_t ipoctal_irq_handler(void *arg)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200230{
231 unsigned int i;
232 struct ipoctal *ipoctal = (struct ipoctal *) arg;
233
Jens Taproggeea991142012-09-13 12:32:20 +0200234 /* Clear the IPack device interrupt */
Jens Taprogge402228d2012-09-27 12:37:34 +0200235 readw(ipoctal->int_space + ACK_INT_REQ0);
236 readw(ipoctal->int_space + ACK_INT_REQ1);
Jens Taproggeea991142012-09-13 12:32:20 +0200237
Samuel Iglesias Gonsalvez21d27ed2012-12-10 11:50:04 +0100238 /* Check all channels */
239 for (i = 0; i < NR_CHANNELS; i++)
240 ipoctal_irq_channel(&ipoctal->channel[i]);
241
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200242 return IRQ_HANDLED;
243}
244
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200245static const struct tty_port_operations ipoctal_tty_port_ops = {
246 .dtr_rts = NULL,
247 .activate = ipoctal_port_activate,
248};
249
250static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200251 unsigned int slot)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200252{
Jens Taprogge402228d2012-09-27 12:37:34 +0200253 int res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200254 int i;
255 struct tty_driver *tty;
256 char name[20];
Jens Taprogge70a32812012-09-12 14:55:26 +0200257 struct ipoctal_channel *channel;
Jens Taprogge402228d2012-09-27 12:37:34 +0200258 struct ipack_region *region;
259 void __iomem *addr;
Jens Taprogge70a32812012-09-12 14:55:26 +0200260 union scc2698_channel __iomem *chan_regs;
261 union scc2698_block __iomem *block_regs;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200262
Jens Taprogge2ec678d2012-09-27 12:37:33 +0200263 ipoctal->board_id = ipoctal->dev->id_device;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200264
Jens Taprogge402228d2012-09-27 12:37:34 +0200265 region = &ipoctal->dev->region[IPACK_IO_SPACE];
266 addr = devm_ioremap_nocache(&ipoctal->dev->dev,
267 region->start, region->size);
268 if (!addr) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200269 dev_err(&ipoctal->dev->dev,
270 "Unable to map slot [%d:%d] IO space!\n",
271 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200272 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200273 }
Jens Taprogge402228d2012-09-27 12:37:34 +0200274 /* Save the virtual address to access the registers easily */
275 chan_regs =
276 (union scc2698_channel __iomem *) addr;
277 block_regs =
278 (union scc2698_block __iomem *) addr;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200279
Jens Taprogge402228d2012-09-27 12:37:34 +0200280 region = &ipoctal->dev->region[IPACK_INT_SPACE];
281 ipoctal->int_space =
282 devm_ioremap_nocache(&ipoctal->dev->dev,
283 region->start, region->size);
284 if (!ipoctal->int_space) {
Jens Taproggeea991142012-09-13 12:32:20 +0200285 dev_err(&ipoctal->dev->dev,
286 "Unable to map slot [%d:%d] INT space!\n",
287 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200288 return -EADDRNOTAVAIL;
Jens Taproggeea991142012-09-13 12:32:20 +0200289 }
290
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200291 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
292 ipoctal->mem8_space =
Jens Taprogge402228d2012-09-27 12:37:34 +0200293 devm_ioremap_nocache(&ipoctal->dev->dev,
294 region->start, 0x8000);
Julia Lawallfc8d7132013-01-21 14:02:53 +0100295 if (!ipoctal->mem8_space) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200296 dev_err(&ipoctal->dev->dev,
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200297 "Unable to map slot [%d:%d] MEM8 space!\n",
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200298 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200299 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200300 }
301
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200302
303 /* Disable RX and TX before touching anything */
304 for (i = 0; i < NR_CHANNELS ; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200305 struct ipoctal_channel *channel = &ipoctal->channel[i];
306 channel->regs = chan_regs + i;
307 channel->block_regs = block_regs + (i >> 1);
Jens Taproggeef79de02012-09-12 14:55:28 +0200308 channel->board_id = ipoctal->board_id;
Jens Taprogge4e4732a2012-09-12 14:55:29 +0200309 if (i & 1) {
310 channel->isr_tx_rdy_mask = ISR_TxRDY_B;
311 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
312 } else {
313 channel->isr_tx_rdy_mask = ISR_TxRDY_A;
314 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
315 }
Jens Taprogge70a32812012-09-12 14:55:26 +0200316
Federico Vaga78f22bc2014-09-02 17:31:39 +0200317 ipoctal_reset_channel(channel);
Jens Taprogge459e6d72012-09-12 14:55:27 +0200318 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 Gonsalvezba4dc612012-05-09 15:27:21 +0200322 }
323
324 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200325 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 Gonsalvezba4dc612012-05-09 15:27:21 +0200331 }
332
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200333 /* Dummy write */
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200334 iowrite8(1, ipoctal->mem8_space + 1);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200335
336 /* Register the TTY device */
337
338 /* Each IP-OCTAL channel is a TTY port */
339 tty = alloc_tty_driver(NR_CHANNELS);
340
Jens Taprogge402228d2012-09-27 12:37:34 +0200341 if (!tty)
342 return -ENOMEM;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200343
344 /* Fill struct tty_driver with ipoctal data */
345 tty->owner = THIS_MODULE;
Jens Taprogge3f3a5922012-09-12 14:55:41 +0200346 tty->driver_name = KBUILD_MODNAME;
347 sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200348 tty->name = name;
349 tty->major = 0;
350
351 tty->minor_start = 0;
352 tty->type = TTY_DRIVER_TYPE_SERIAL;
353 tty->subtype = SERIAL_TYPE_NORMAL;
354 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
355 tty->init_termios = tty_std_termios;
356 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
357 tty->init_termios.c_ispeed = 9600;
358 tty->init_termios.c_ospeed = 9600;
359
360 tty_set_operations(tty, &ipoctal_fops);
361 res = tty_register_driver(tty);
362 if (res) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200363 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200364 put_tty_driver(tty);
Jens Taprogge402228d2012-09-27 12:37:34 +0200365 return res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200366 }
367
368 /* Save struct tty_driver for use it when uninstalling the device */
369 ipoctal->tty_drv = tty;
370
371 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200372 struct device *tty_dev;
373
Jens Taprogge70a32812012-09-12 14:55:26 +0200374 channel = &ipoctal->channel[i];
375 tty_port_init(&channel->tty_port);
376 tty_port_alloc_xmit_buf(&channel->tty_port);
377 channel->tty_port.ops = &ipoctal_tty_port_ops;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200378
Jens Taprogge70a32812012-09-12 14:55:26 +0200379 ipoctal_reset_stats(&channel->stats);
380 channel->nb_bytes = 0;
Jens Taprogge70a32812012-09-12 14:55:26 +0200381 spin_lock_init(&channel->lock);
382 channel->pointer_read = 0;
383 channel->pointer_write = 0;
Linus Torvalds3498d132012-10-01 12:26:52 -0700384 tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200385 if (IS_ERR(tty_dev)) {
386 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
Jiri Slaby191c5f12012-11-15 09:49:56 +0100387 tty_port_destroy(&channel->tty_port);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200388 continue;
389 }
Jens Taprogge9c1d7842012-09-12 14:55:42 +0200390 dev_set_drvdata(tty_dev, channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200391 }
392
Federico Vaga4847cc02014-07-03 10:53:58 +0200393 /*
394 * IP-OCTAL has different addresses to copy its IRQ vector.
395 * Depending of the carrier these addresses are accesible or not.
396 * More info in the datasheet.
397 */
398 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
399 ipoctal_irq_handler, ipoctal);
400
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200401 return 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200402}
403
Jens Taprogge70a32812012-09-12 14:55:26 +0200404static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200405 const unsigned char *buf,
406 int count)
407{
408 unsigned long flags;
409 int i;
Jens Taprogge70a32812012-09-12 14:55:26 +0200410 unsigned int *pointer_read = &channel->pointer_read;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200411
412 /* Copy the bytes from the user buffer to the internal one */
413 for (i = 0; i < count; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200414 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
415 spin_lock_irqsave(&channel->lock, flags);
416 channel->tty_port.xmit_buf[*pointer_read] = buf[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200417 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
Jens Taprogge70a32812012-09-12 14:55:26 +0200418 channel->nb_bytes++;
419 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200420 } else {
421 break;
422 }
423 }
424 return i;
425}
426
Jens Taprogge699a89f2012-09-12 14:55:31 +0200427static int ipoctal_write_tty(struct tty_struct *tty,
428 const unsigned char *buf, int count)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200429{
Jens Taprogge699a89f2012-09-12 14:55:31 +0200430 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200431 unsigned int char_copied;
Jens Taprogge699a89f2012-09-12 14:55:31 +0200432
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200433 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200434
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200435 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
Jens Taproggeef79de02012-09-12 14:55:28 +0200436 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200437 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezb0d17fb2012-12-10 11:50:07 +0100438 channel->rx_enable = 0;
Jens Taprogge459e6d72012-09-12 14:55:27 +0200439 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200440 }
441
442 /*
443 * Send a packet and then disable TX to avoid failure after several send
444 * operations
445 */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200446 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200447 return char_copied;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200448}
449
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200450static int ipoctal_write_room(struct tty_struct *tty)
451{
Jens Taproggeef79de02012-09-12 14:55:28 +0200452 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200453
Jens Taprogge70a32812012-09-12 14:55:26 +0200454 return PAGE_SIZE - channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200455}
456
457static int ipoctal_chars_in_buffer(struct tty_struct *tty)
458{
Jens Taproggeef79de02012-09-12 14:55:28 +0200459 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200460
Jens Taprogge70a32812012-09-12 14:55:26 +0200461 return channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200462}
463
464static void ipoctal_set_termios(struct tty_struct *tty,
465 struct ktermios *old_termios)
466{
467 unsigned int cflag;
468 unsigned char mr1 = 0;
469 unsigned char mr2 = 0;
470 unsigned char csr = 0;
Jens Taproggeef79de02012-09-12 14:55:28 +0200471 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200472 speed_t baud;
473
Alan Cox857196e2012-07-26 19:00:51 +0100474 cflag = tty->termios.c_cflag;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200475
476 /* Disable and reset everything before change the setup */
Federico Vaga78f22bc2014-09-02 17:31:39 +0200477 ipoctal_reset_channel(channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200478
479 /* Set Bits per chars */
480 switch (cflag & CSIZE) {
481 case CS6:
482 mr1 |= MR1_CHRL_6_BITS;
483 break;
484 case CS7:
485 mr1 |= MR1_CHRL_7_BITS;
486 break;
487 case CS8:
488 default:
489 mr1 |= MR1_CHRL_8_BITS;
490 /* By default, select CS8 */
Alan Cox857196e2012-07-26 19:00:51 +0100491 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200492 break;
493 }
494
495 /* Set Parity */
496 if (cflag & PARENB)
497 if (cflag & PARODD)
498 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
499 else
500 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
501 else
502 mr1 |= MR1_PARITY_OFF;
503
504 /* Mark or space parity is not supported */
Alan Cox857196e2012-07-26 19:00:51 +0100505 tty->termios.c_cflag &= ~CMSPAR;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200506
507 /* Set stop bits */
508 if (cflag & CSTOPB)
509 mr2 |= MR2_STOP_BITS_LENGTH_2;
510 else
511 mr2 |= MR2_STOP_BITS_LENGTH_1;
512
513 /* Set the flow control */
Jens Taproggeef79de02012-09-12 14:55:28 +0200514 switch (channel->board_id) {
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200515 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200516 if (cflag & CRTSCTS) {
517 mr1 |= MR1_RxRTS_CONTROL_ON;
518 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200519 } else {
520 mr1 |= MR1_RxRTS_CONTROL_OFF;
521 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200522 }
523 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200524 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200525 mr1 |= MR1_RxRTS_CONTROL_OFF;
526 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200527 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200528 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200529 mr1 |= MR1_RxRTS_CONTROL_OFF;
530 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200531 break;
532 default:
533 return;
534 break;
535 }
536
537 baud = tty_get_baud_rate(tty);
Alan Cox857196e2012-07-26 19:00:51 +0100538 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200539
540 /* Set baud rate */
Alan Cox857196e2012-07-26 19:00:51 +0100541 switch (baud) {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200542 case 75:
543 csr |= TX_CLK_75 | RX_CLK_75;
544 break;
545 case 110:
546 csr |= TX_CLK_110 | RX_CLK_110;
547 break;
548 case 150:
549 csr |= TX_CLK_150 | RX_CLK_150;
550 break;
551 case 300:
552 csr |= TX_CLK_300 | RX_CLK_300;
553 break;
554 case 600:
555 csr |= TX_CLK_600 | RX_CLK_600;
556 break;
557 case 1200:
558 csr |= TX_CLK_1200 | RX_CLK_1200;
559 break;
560 case 1800:
561 csr |= TX_CLK_1800 | RX_CLK_1800;
562 break;
563 case 2000:
564 csr |= TX_CLK_2000 | RX_CLK_2000;
565 break;
566 case 2400:
567 csr |= TX_CLK_2400 | RX_CLK_2400;
568 break;
569 case 4800:
570 csr |= TX_CLK_4800 | RX_CLK_4800;
571 break;
572 case 9600:
573 csr |= TX_CLK_9600 | RX_CLK_9600;
574 break;
575 case 19200:
576 csr |= TX_CLK_19200 | RX_CLK_19200;
577 break;
578 case 38400:
579 default:
580 csr |= TX_CLK_38400 | RX_CLK_38400;
581 /* In case of default, we establish 38400 bps */
Alan Cox857196e2012-07-26 19:00:51 +0100582 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200583 break;
584 }
585
586 mr1 |= MR1_ERROR_CHAR;
587 mr1 |= MR1_RxINT_RxRDY;
588
589 /* Write the control registers */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200590 iowrite8(mr1, &channel->regs->w.mr);
591 iowrite8(mr2, &channel->regs->w.mr);
592 iowrite8(csr, &channel->regs->w.csr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200593
Samuel Iglesias Gonsalvezb0d17fb2012-12-10 11:50:07 +0100594 /* Enable again the RX, if it was before */
595 if (channel->rx_enable)
596 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200597}
598
599static void ipoctal_hangup(struct tty_struct *tty)
600{
601 unsigned long flags;
Jens Taproggeef79de02012-09-12 14:55:28 +0200602 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200603
Jens Taproggeef79de02012-09-12 14:55:28 +0200604 if (channel == NULL)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200605 return;
606
Jens Taprogge70a32812012-09-12 14:55:26 +0200607 spin_lock_irqsave(&channel->lock, flags);
608 channel->nb_bytes = 0;
609 channel->pointer_read = 0;
610 channel->pointer_write = 0;
611 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200612
Jens Taprogge70a32812012-09-12 14:55:26 +0200613 tty_port_hangup(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200614
Federico Vaga78f22bc2014-09-02 17:31:39 +0200615 ipoctal_reset_channel(channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200616
Jens Taprogge70a32812012-09-12 14:55:26 +0200617 clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
618 wake_up_interruptible(&channel->tty_port.open_wait);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200619}
620
Samuel Iglesias Gonsalveze0f8d322012-12-10 11:50:08 +0100621static void ipoctal_shutdown(struct tty_struct *tty)
622{
623 struct ipoctal_channel *channel = tty->driver_data;
624
625 if (channel == NULL)
626 return;
627
Federico Vaga78f22bc2014-09-02 17:31:39 +0200628 ipoctal_reset_channel(channel);
Samuel Iglesias Gonsalveze0f8d322012-12-10 11:50:08 +0100629 clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
630}
631
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200632static const struct tty_operations ipoctal_fops = {
633 .ioctl = NULL,
634 .open = ipoctal_open,
635 .close = ipoctal_close,
636 .write = ipoctal_write_tty,
637 .set_termios = ipoctal_set_termios,
638 .write_room = ipoctal_write_room,
639 .chars_in_buffer = ipoctal_chars_in_buffer,
640 .get_icount = ipoctal_get_icount,
641 .hangup = ipoctal_hangup,
Samuel Iglesias Gonsalveze0f8d322012-12-10 11:50:08 +0100642 .shutdown = ipoctal_shutdown,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200643};
644
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200645static int ipoctal_probe(struct ipack_device *dev)
646{
647 int res;
648 struct ipoctal *ipoctal;
649
650 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
651 if (ipoctal == NULL)
652 return -ENOMEM;
653
654 ipoctal->dev = dev;
Jens Taproggef9e314d2012-09-27 12:37:25 +0200655 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200656 if (res)
657 goto out_uninst;
658
Jens Taprogge1adda492012-09-12 14:55:39 +0200659 dev_set_drvdata(&dev->dev, ipoctal);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200660 return 0;
661
662out_uninst:
663 kfree(ipoctal);
664 return res;
665}
666
667static void __ipoctal_remove(struct ipoctal *ipoctal)
668{
669 int i;
670
Samuel Iglesias Gonsálvez690949e2012-09-11 13:35:08 +0200671 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
672
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200673 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200674 struct ipoctal_channel *channel = &ipoctal->channel[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200675 tty_unregister_device(ipoctal->tty_drv, i);
Jens Taprogge70a32812012-09-12 14:55:26 +0200676 tty_port_free_xmit_buf(&channel->tty_port);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100677 tty_port_destroy(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200678 }
679
680 tty_unregister_driver(ipoctal->tty_drv);
681 put_tty_driver(ipoctal->tty_drv);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200682 kfree(ipoctal);
683}
684
Jens Taprogge1adda492012-09-12 14:55:39 +0200685static void ipoctal_remove(struct ipack_device *idev)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200686{
Jens Taprogge1adda492012-09-12 14:55:39 +0200687 __ipoctal_remove(dev_get_drvdata(&idev->dev));
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200688}
689
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200690static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
691 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
692 IPACK1_DEVICE_ID_SBS_OCTAL_232) },
693 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
694 IPACK1_DEVICE_ID_SBS_OCTAL_422) },
695 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
696 IPACK1_DEVICE_ID_SBS_OCTAL_485) },
697 { 0, },
698};
699
700MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
701
Jens Taproggee8011132012-09-04 17:01:17 +0200702static const struct ipack_driver_ops ipoctal_drv_ops = {
Jens Taprogge4aa09d42012-09-04 17:01:19 +0200703 .probe = ipoctal_probe,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200704 .remove = ipoctal_remove,
705};
706
Jens Taproggee8011132012-09-04 17:01:17 +0200707static struct ipack_driver driver = {
708 .ops = &ipoctal_drv_ops,
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200709 .id_table = ipoctal_ids,
Jens Taproggee8011132012-09-04 17:01:17 +0200710};
711
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200712static int __init ipoctal_init(void)
713{
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200714 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200715}
716
717static void __exit ipoctal_exit(void)
718{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200719 ipack_driver_unregister(&driver);
720}
721
722MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
723MODULE_LICENSE("GPL");
724
725module_init(ipoctal_init);
726module_exit(ipoctal_exit);