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