blob: 8d0a86631908ce706ad542b545630f11e63de14b [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>
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020023#include <linux/atomic.h>
Jens Taprogge64802dc2012-09-04 17:01:08 +020024#include <linux/io.h>
Samuel Iglesias Gonsalvez7dbce022012-11-16 19:33:45 +010025#include <linux/ipack.h>
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020026#include "ipoctal.h"
27#include "scc2698.h"
28
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020029#define IP_OCTAL_ID_SPACE_VECTOR 0x41
30#define IP_OCTAL_NB_BLOCKS 4
31
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020032static const struct tty_operations ipoctal_fops;
33
Jens Taprogge70a32812012-09-12 14:55:26 +020034struct ipoctal_channel {
35 struct ipoctal_stats stats;
36 unsigned int nb_bytes;
Jens Taprogge70a32812012-09-12 14:55:26 +020037 wait_queue_head_t queue;
38 spinlock_t lock;
39 unsigned int pointer_read;
40 unsigned int pointer_write;
Jens Taprogge70a32812012-09-12 14:55:26 +020041 struct tty_port tty_port;
42 union scc2698_channel __iomem *regs;
43 union scc2698_block __iomem *block_regs;
Jens Taproggeef79de02012-09-12 14:55:28 +020044 unsigned int board_id;
45 unsigned char *board_write;
Jens Taprogge4e4732a2012-09-12 14:55:29 +020046 u8 isr_rx_rdy_mask;
47 u8 isr_tx_rdy_mask;
Jens Taprogge70a32812012-09-12 14:55:26 +020048};
49
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020050struct ipoctal {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020051 struct ipack_device *dev;
52 unsigned int board_id;
Jens Taprogge70a32812012-09-12 14:55:26 +020053 struct ipoctal_channel channel[NR_CHANNELS];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020054 unsigned char write;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020055 struct tty_driver *tty_drv;
Jens Taproggefe4a3ed2012-09-27 12:37:36 +020056 u8 __iomem *mem8_space;
Jens Taprogge402228d2012-09-27 12:37:34 +020057 u8 __iomem *int_space;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020058};
59
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020060static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
61{
Jens Taprogge70a32812012-09-12 14:55:26 +020062 struct ipoctal_channel *channel;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020063
Jens Taprogge9c1d7842012-09-12 14:55:42 +020064 channel = dev_get_drvdata(tty->dev);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020065
Jens Taprogge459e6d72012-09-12 14:55:27 +020066 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020067 return 0;
68}
69
70static int ipoctal_open(struct tty_struct *tty, struct file *file)
71{
Jens Taprogge70a32812012-09-12 14:55:26 +020072 struct ipoctal_channel *channel;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020073
Jens Taprogge9c1d7842012-09-12 14:55:42 +020074 channel = dev_get_drvdata(tty->dev);
Jens Taproggeef79de02012-09-12 14:55:28 +020075 tty->driver_data = channel;
Samuel Iglesias Gonsálvez1337b072012-07-13 13:33:13 +020076
Samuel Iglesias Gonsalvez7e5730d2012-12-10 11:49:59 +010077 return tty_port_open(&channel->tty_port, tty, file);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020078}
79
80static 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 Taproggeef79de02012-09-12 14:55:28 +020090static void ipoctal_free_channel(struct ipoctal_channel *channel)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020091{
Jens Taprogge70a32812012-09-12 14:55:26 +020092 ipoctal_reset_stats(&channel->stats);
93 channel->pointer_read = 0;
94 channel->pointer_write = 0;
95 channel->nb_bytes = 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020096}
97
98static void ipoctal_close(struct tty_struct *tty, struct file *filp)
99{
Jens Taproggeef79de02012-09-12 14:55:28 +0200100 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200101
Jens Taprogge70a32812012-09-12 14:55:26 +0200102 tty_port_close(&channel->tty_port, tty, filp);
Samuel Iglesias Gonsalvez7e5730d2012-12-10 11:49:59 +0100103 ipoctal_free_channel(channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200104}
105
106static int ipoctal_get_icount(struct tty_struct *tty,
107 struct serial_icounter_struct *icount)
108{
Jens Taproggeef79de02012-09-12 14:55:28 +0200109 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200110
111 icount->cts = 0;
112 icount->dsr = 0;
113 icount->rng = 0;
114 icount->dcd = 0;
Jens Taprogge70a32812012-09-12 14:55:26 +0200115 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 Gonsalvezba4dc612012-05-09 15:27:21 +0200120 return 0;
121}
122
Jens Taprogge87cfb952012-09-12 14:55:30 +0200123static void ipoctal_irq_rx(struct ipoctal_channel *channel,
124 struct tty_struct *tty, u8 sr)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200125{
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200126 unsigned char value;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200127 unsigned char flag = TTY_NORMAL;
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200128 u8 isr;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200129
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200130 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 Taprogge87cfb952012-09-12 14:55:30 +0200135
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200136 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 Gonsalvez45141082012-09-13 12:32:22 +0200150 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200151 channel->stats.rcv_break++;
152 flag = TTY_BREAK;
153 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200154 }
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200155 tty_insert_flip_char(tty, value, flag);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200156
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200157 /* 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 Taprogge87cfb952012-09-12 14:55:30 +0200166}
167
168static void ipoctal_irq_tx(struct ipoctal_channel *channel)
169{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200170 unsigned char value;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200171 unsigned int *pointer_write = &channel->pointer_write;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200172
Alberto Garcia69a6b9b2012-12-10 11:49:58 +0100173 if (channel->nb_bytes == 0)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200174 return;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200175
Jens Taprogge87cfb952012-09-12 14:55:30 +0200176 value = channel->tty_port.xmit_buf[*pointer_write];
177 iowrite8(value, &channel->regs->w.thr);
178 channel->stats.tx++;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200179 (*pointer_write)++;
180 *pointer_write = *pointer_write % PAGE_SIZE;
181 channel->nb_bytes--;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200182
Alberto Garciacc83f832012-12-10 11:49:57 +0100183 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 Gonsalvezba4dc612012-05-09 15:27:21 +0200187 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200188}
189
190static void ipoctal_irq_channel(struct ipoctal_channel *channel)
191{
192 u8 isr, sr;
193 struct tty_struct *tty;
194
Jens Taprogge87cfb952012-09-12 14:55:30 +0200195 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 Taproggefaa75c42012-09-12 14:55:38 +0200226static irqreturn_t ipoctal_irq_handler(void *arg)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200227{
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 Taproggeea991142012-09-13 12:32:20 +0200235 /* Clear the IPack device interrupt */
Jens Taprogge402228d2012-09-27 12:37:34 +0200236 readw(ipoctal->int_space + ACK_INT_REQ0);
237 readw(ipoctal->int_space + ACK_INT_REQ1);
Jens Taproggeea991142012-09-13 12:32:20 +0200238
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200239 return IRQ_HANDLED;
240}
241
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200242static const struct tty_port_operations ipoctal_tty_port_ops = {
243 .dtr_rts = NULL,
244 .activate = ipoctal_port_activate,
245};
246
247static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200248 unsigned int slot)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200249{
Jens Taprogge402228d2012-09-27 12:37:34 +0200250 int res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200251 int i;
252 struct tty_driver *tty;
253 char name[20];
Jens Taprogge70a32812012-09-12 14:55:26 +0200254 struct ipoctal_channel *channel;
Jens Taprogge402228d2012-09-27 12:37:34 +0200255 struct ipack_region *region;
256 void __iomem *addr;
Jens Taprogge70a32812012-09-12 14:55:26 +0200257 union scc2698_channel __iomem *chan_regs;
258 union scc2698_block __iomem *block_regs;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200259
Jens Taprogge2ec678d2012-09-27 12:37:33 +0200260 ipoctal->board_id = ipoctal->dev->id_device;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200261
Jens Taprogge402228d2012-09-27 12:37:34 +0200262 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 Gonsalvez42b38202012-05-25 13:08:13 +0200266 dev_err(&ipoctal->dev->dev,
267 "Unable to map slot [%d:%d] IO space!\n",
268 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200269 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200270 }
Jens Taprogge402228d2012-09-27 12:37:34 +0200271 /* 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 Gonsalvezba4dc612012-05-09 15:27:21 +0200276
Jens Taprogge402228d2012-09-27 12:37:34 +0200277 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 Taproggeea991142012-09-13 12:32:20 +0200282 dev_err(&ipoctal->dev->dev,
283 "Unable to map slot [%d:%d] INT space!\n",
284 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200285 return -EADDRNOTAVAIL;
Jens Taproggeea991142012-09-13 12:32:20 +0200286 }
287
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200288 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
289 ipoctal->mem8_space =
Jens Taprogge402228d2012-09-27 12:37:34 +0200290 devm_ioremap_nocache(&ipoctal->dev->dev,
291 region->start, 0x8000);
292 if (!addr) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200293 dev_err(&ipoctal->dev->dev,
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200294 "Unable to map slot [%d:%d] MEM8 space!\n",
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200295 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200296 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200297 }
298
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200299
300 /* Disable RX and TX before touching anything */
301 for (i = 0; i < NR_CHANNELS ; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200302 struct ipoctal_channel *channel = &ipoctal->channel[i];
303 channel->regs = chan_regs + i;
304 channel->block_regs = block_regs + (i >> 1);
Jens Taproggeef79de02012-09-12 14:55:28 +0200305 channel->board_write = &ipoctal->write;
306 channel->board_id = ipoctal->board_id;
Jens Taprogge4e4732a2012-09-12 14:55:29 +0200307 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 Taprogge70a32812012-09-12 14:55:26 +0200314
Jens Taprogge459e6d72012-09-12 14:55:27 +0200315 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 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
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 Taproggec6e2dfa2012-09-13 12:32:21 +0200338 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200339 ipoctal_irq_handler, ipoctal);
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200340 /* Dummy write */
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200341 iowrite8(1, ipoctal->mem8_space + 1);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200342
343 /* Register the TTY device */
344
345 /* Each IP-OCTAL channel is a TTY port */
346 tty = alloc_tty_driver(NR_CHANNELS);
347
Jens Taprogge402228d2012-09-27 12:37:34 +0200348 if (!tty)
349 return -ENOMEM;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200350
351 /* Fill struct tty_driver with ipoctal data */
352 tty->owner = THIS_MODULE;
Jens Taprogge3f3a5922012-09-12 14:55:41 +0200353 tty->driver_name = KBUILD_MODNAME;
354 sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200355 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 Gonsalvez42b38202012-05-25 13:08:13 +0200370 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200371 put_tty_driver(tty);
Jens Taprogge402228d2012-09-27 12:37:34 +0200372 return res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200373 }
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 Taprogge2afb41d2012-09-12 14:55:40 +0200379 struct device *tty_dev;
380
Jens Taprogge70a32812012-09-12 14:55:26 +0200381 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 Gonsalvezba4dc612012-05-09 15:27:21 +0200385
Jens Taprogge70a32812012-09-12 14:55:26 +0200386 ipoctal_reset_stats(&channel->stats);
387 channel->nb_bytes = 0;
388 init_waitqueue_head(&channel->queue);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200389
Jens Taprogge70a32812012-09-12 14:55:26 +0200390 spin_lock_init(&channel->lock);
391 channel->pointer_read = 0;
392 channel->pointer_write = 0;
Linus Torvalds3498d132012-10-01 12:26:52 -0700393 tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200394 if (IS_ERR(tty_dev)) {
395 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
Jiri Slaby191c5f12012-11-15 09:49:56 +0100396 tty_port_destroy(&channel->tty_port);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200397 continue;
398 }
Jens Taprogge9c1d7842012-09-12 14:55:42 +0200399 dev_set_drvdata(tty_dev, channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200400
401 /*
402 * Enable again the RX. TX will be enabled when
403 * there is something to send
404 */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200405 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200406 }
407
408 return 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200409}
410
Jens Taprogge70a32812012-09-12 14:55:26 +0200411static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200412 const unsigned char *buf,
413 int count)
414{
415 unsigned long flags;
416 int i;
Jens Taprogge70a32812012-09-12 14:55:26 +0200417 unsigned int *pointer_read = &channel->pointer_read;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200418
419 /* Copy the bytes from the user buffer to the internal one */
420 for (i = 0; i < count; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200421 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 Gonsalvezba4dc612012-05-09 15:27:21 +0200424 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
Jens Taprogge70a32812012-09-12 14:55:26 +0200425 channel->nb_bytes++;
426 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200427 } else {
428 break;
429 }
430 }
431 return i;
432}
433
Jens Taprogge699a89f2012-09-12 14:55:31 +0200434static int ipoctal_write_tty(struct tty_struct *tty,
435 const unsigned char *buf, int count)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200436{
Jens Taprogge699a89f2012-09-12 14:55:31 +0200437 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200438 unsigned int char_copied;
Jens Taprogge699a89f2012-09-12 14:55:31 +0200439
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200440 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200441
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200442 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
Jens Taproggeef79de02012-09-12 14:55:28 +0200443 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200444 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
445 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200446 }
447
448 /*
449 * Send a packet and then disable TX to avoid failure after several send
450 * operations
451 */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200452 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
Jens Taproggeef79de02012-09-12 14:55:28 +0200453 wait_event_interruptible(channel->queue, *channel->board_write);
Jens Taprogge459e6d72012-09-12 14:55:27 +0200454 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200455
Jens Taproggeef79de02012-09-12 14:55:28 +0200456 *channel->board_write = 0;
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200457 return char_copied;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200458}
459
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200460static int ipoctal_write_room(struct tty_struct *tty)
461{
Jens Taproggeef79de02012-09-12 14:55:28 +0200462 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200463
Jens Taprogge70a32812012-09-12 14:55:26 +0200464 return PAGE_SIZE - channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200465}
466
467static int ipoctal_chars_in_buffer(struct tty_struct *tty)
468{
Jens Taproggeef79de02012-09-12 14:55:28 +0200469 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200470
Jens Taprogge70a32812012-09-12 14:55:26 +0200471 return channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200472}
473
474static 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 Taproggeef79de02012-09-12 14:55:28 +0200481 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200482 speed_t baud;
483
Alan Cox857196e2012-07-26 19:00:51 +0100484 cflag = tty->termios.c_cflag;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200485
486 /* Disable and reset everything before change the setup */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200487 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 Gonsalvezba4dc612012-05-09 15:27:21 +0200492
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 Cox857196e2012-07-26 19:00:51 +0100505 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200506 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 Cox857196e2012-07-26 19:00:51 +0100519 tty->termios.c_cflag &= ~CMSPAR;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200520
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 Taproggeef79de02012-09-12 14:55:28 +0200528 switch (channel->board_id) {
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200529 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200530 if (cflag & CRTSCTS) {
531 mr1 |= MR1_RxRTS_CONTROL_ON;
532 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200533 } else {
534 mr1 |= MR1_RxRTS_CONTROL_OFF;
535 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200536 }
537 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200538 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200539 mr1 |= MR1_RxRTS_CONTROL_OFF;
540 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200541 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200542 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200543 mr1 |= MR1_RxRTS_CONTROL_OFF;
544 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200545 break;
546 default:
547 return;
548 break;
549 }
550
551 baud = tty_get_baud_rate(tty);
Alan Cox857196e2012-07-26 19:00:51 +0100552 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200553
554 /* Set baud rate */
Alan Cox857196e2012-07-26 19:00:51 +0100555 switch (baud) {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200556 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 Cox857196e2012-07-26 19:00:51 +0100596 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200597 break;
598 }
599
600 mr1 |= MR1_ERROR_CHAR;
601 mr1 |= MR1_RxINT_RxRDY;
602
603 /* Write the control registers */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200604 iowrite8(mr1, &channel->regs->w.mr);
605 iowrite8(mr2, &channel->regs->w.mr);
606 iowrite8(csr, &channel->regs->w.csr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200607
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200608 /* Enable again the RX */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200609 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200610}
611
612static void ipoctal_hangup(struct tty_struct *tty)
613{
614 unsigned long flags;
Jens Taproggeef79de02012-09-12 14:55:28 +0200615 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200616
Jens Taproggeef79de02012-09-12 14:55:28 +0200617 if (channel == NULL)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200618 return;
619
Jens Taprogge70a32812012-09-12 14:55:26 +0200620 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 Gonsalvezba4dc612012-05-09 15:27:21 +0200625
Jens Taprogge70a32812012-09-12 14:55:26 +0200626 tty_port_hangup(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200627
Jens Taprogge459e6d72012-09-12 14:55:27 +0200628 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 Gonsalvezba4dc612012-05-09 15:27:21 +0200633
Jens Taprogge70a32812012-09-12 14:55:26 +0200634 clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
635 wake_up_interruptible(&channel->tty_port.open_wait);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200636}
637
638static 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 Gonsalvezba4dc612012-05-09 15:27:21 +0200650static 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 Taproggef9e314d2012-09-27 12:37:25 +0200660 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200661 if (res)
662 goto out_uninst;
663
Jens Taprogge1adda492012-09-12 14:55:39 +0200664 dev_set_drvdata(&dev->dev, ipoctal);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200665 return 0;
666
667out_uninst:
668 kfree(ipoctal);
669 return res;
670}
671
672static void __ipoctal_remove(struct ipoctal *ipoctal)
673{
674 int i;
675
Samuel Iglesias Gonsálvez690949e2012-09-11 13:35:08 +0200676 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
677
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200678 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200679 struct ipoctal_channel *channel = &ipoctal->channel[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200680 tty_unregister_device(ipoctal->tty_drv, i);
Jens Taprogge70a32812012-09-12 14:55:26 +0200681 tty_port_free_xmit_buf(&channel->tty_port);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100682 tty_port_destroy(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200683 }
684
685 tty_unregister_driver(ipoctal->tty_drv);
686 put_tty_driver(ipoctal->tty_drv);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200687 kfree(ipoctal);
688}
689
Jens Taprogge1adda492012-09-12 14:55:39 +0200690static void ipoctal_remove(struct ipack_device *idev)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200691{
Jens Taprogge1adda492012-09-12 14:55:39 +0200692 __ipoctal_remove(dev_get_drvdata(&idev->dev));
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200693}
694
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200695static 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
705MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
706
Jens Taproggee8011132012-09-04 17:01:17 +0200707static const struct ipack_driver_ops ipoctal_drv_ops = {
Jens Taprogge4aa09d42012-09-04 17:01:19 +0200708 .probe = ipoctal_probe,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200709 .remove = ipoctal_remove,
710};
711
Jens Taproggee8011132012-09-04 17:01:17 +0200712static struct ipack_driver driver = {
713 .ops = &ipoctal_drv_ops,
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200714 .id_table = ipoctal_ids,
Jens Taproggee8011132012-09-04 17:01:17 +0200715};
716
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200717static int __init ipoctal_init(void)
718{
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200719 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200720}
721
722static void __exit ipoctal_exit(void)
723{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200724 ipack_driver_unregister(&driver);
725}
726
727MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
728MODULE_LICENSE("GPL");
729
730module_init(ipoctal_init);
731module_exit(ipoctal_exit);