blob: f2875f0f14d4297c3d48df45a7c7fedb98af4ca2 [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;
Jens Taprogge70a32812012-09-12 14:55:26 +020046};
47
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020048struct ipoctal {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020049 struct ipack_device *dev;
50 unsigned int board_id;
Jens Taprogge70a32812012-09-12 14:55:26 +020051 struct ipoctal_channel channel[NR_CHANNELS];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020052 struct tty_driver *tty_drv;
Jens Taproggefe4a3ed2012-09-27 12:37:36 +020053 u8 __iomem *mem8_space;
Jens Taprogge402228d2012-09-27 12:37:34 +020054 u8 __iomem *int_space;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020055};
56
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020057static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
58{
Jens Taprogge70a32812012-09-12 14:55:26 +020059 struct ipoctal_channel *channel;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020060
Jens Taprogge9c1d7842012-09-12 14:55:42 +020061 channel = dev_get_drvdata(tty->dev);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020062
Jens Taprogge459e6d72012-09-12 14:55:27 +020063 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020064 return 0;
65}
66
67static int ipoctal_open(struct tty_struct *tty, struct file *file)
68{
Jens Taprogge70a32812012-09-12 14:55:26 +020069 struct ipoctal_channel *channel;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020070
Jens Taprogge9c1d7842012-09-12 14:55:42 +020071 channel = dev_get_drvdata(tty->dev);
Jens Taproggeef79de02012-09-12 14:55:28 +020072 tty->driver_data = channel;
Samuel Iglesias Gonsálvez1337b072012-07-13 13:33:13 +020073
Samuel Iglesias Gonsalvez7e5730d2012-12-10 11:49:59 +010074 return tty_port_open(&channel->tty_port, tty, file);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020075}
76
77static void ipoctal_reset_stats(struct ipoctal_stats *stats)
78{
79 stats->tx = 0;
80 stats->rx = 0;
81 stats->rcv_break = 0;
82 stats->framing_err = 0;
83 stats->overrun_err = 0;
84 stats->parity_err = 0;
85}
86
Jens Taproggeef79de02012-09-12 14:55:28 +020087static void ipoctal_free_channel(struct ipoctal_channel *channel)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020088{
Jens Taprogge70a32812012-09-12 14:55:26 +020089 ipoctal_reset_stats(&channel->stats);
90 channel->pointer_read = 0;
91 channel->pointer_write = 0;
92 channel->nb_bytes = 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020093}
94
95static void ipoctal_close(struct tty_struct *tty, struct file *filp)
96{
Jens Taproggeef79de02012-09-12 14:55:28 +020097 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020098
Jens Taprogge70a32812012-09-12 14:55:26 +020099 tty_port_close(&channel->tty_port, tty, filp);
Samuel Iglesias Gonsalvez7e5730d2012-12-10 11:49:59 +0100100 ipoctal_free_channel(channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200101}
102
103static int ipoctal_get_icount(struct tty_struct *tty,
104 struct serial_icounter_struct *icount)
105{
Jens Taproggeef79de02012-09-12 14:55:28 +0200106 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200107
108 icount->cts = 0;
109 icount->dsr = 0;
110 icount->rng = 0;
111 icount->dcd = 0;
Jens Taprogge70a32812012-09-12 14:55:26 +0200112 icount->rx = channel->stats.rx;
113 icount->tx = channel->stats.tx;
114 icount->frame = channel->stats.framing_err;
115 icount->parity = channel->stats.parity_err;
116 icount->brk = channel->stats.rcv_break;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200117 return 0;
118}
119
Jens Taprogge87cfb952012-09-12 14:55:30 +0200120static void ipoctal_irq_rx(struct ipoctal_channel *channel,
121 struct tty_struct *tty, u8 sr)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200122{
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200123 unsigned char value;
Samuel Iglesias Gonsalvezb5071f22012-12-10 11:50:01 +0100124 unsigned char flag;
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200125 u8 isr;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200126
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200127 do {
128 value = ioread8(&channel->regs->r.rhr);
Samuel Iglesias Gonsalvezb5071f22012-12-10 11:50:01 +0100129 flag = TTY_NORMAL;
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200130 /* Error: count statistics */
131 if (sr & SR_ERROR) {
132 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200133
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200134 if (sr & SR_OVERRUN_ERROR) {
135 channel->stats.overrun_err++;
136 /* Overrun doesn't affect the current character*/
137 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
138 }
139 if (sr & SR_PARITY_ERROR) {
140 channel->stats.parity_err++;
141 flag = TTY_PARITY;
142 }
143 if (sr & SR_FRAMING_ERROR) {
144 channel->stats.framing_err++;
145 flag = TTY_FRAME;
146 }
147 if (sr & SR_RECEIVED_BREAK) {
Samuel Iglesias Gonsalvez45141082012-09-13 12:32:22 +0200148 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200149 channel->stats.rcv_break++;
150 flag = TTY_BREAK;
151 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200152 }
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200153 tty_insert_flip_char(tty, value, flag);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200154
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200155 /* Check if there are more characters in RX FIFO
156 * If there are more, the isr register for this channel
157 * has enabled the RxRDY|FFULL bit.
158 */
159 isr = ioread8(&channel->block_regs->r.isr);
160 sr = ioread8(&channel->regs->r.sr);
161 } while (isr & channel->isr_rx_rdy_mask);
162
163 tty_flip_buffer_push(tty);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200164}
165
166static void ipoctal_irq_tx(struct ipoctal_channel *channel)
167{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200168 unsigned char value;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200169 unsigned int *pointer_write = &channel->pointer_write;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200170
Alberto Garcia69a6b9b2012-12-10 11:49:58 +0100171 if (channel->nb_bytes == 0)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200172 return;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200173
Jens Taprogge87cfb952012-09-12 14:55:30 +0200174 value = channel->tty_port.xmit_buf[*pointer_write];
175 iowrite8(value, &channel->regs->w.thr);
176 channel->stats.tx++;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200177 (*pointer_write)++;
178 *pointer_write = *pointer_write % PAGE_SIZE;
179 channel->nb_bytes--;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200180}
181
182static void ipoctal_irq_channel(struct ipoctal_channel *channel)
183{
184 u8 isr, sr;
185 struct tty_struct *tty;
186
Jens Taprogge87cfb952012-09-12 14:55:30 +0200187 tty = tty_port_tty_get(&channel->tty_port);
188 if (!tty)
189 return;
190 /* The HW is organized in pair of channels. See which register we need
191 * to read from */
192 isr = ioread8(&channel->block_regs->r.isr);
193 sr = ioread8(&channel->regs->r.sr);
194
Samuel Iglesias Gonsalvez9d01b6f2012-12-10 11:50:02 +0100195 if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
Jens Taprogge87cfb952012-09-12 14:55:30 +0200196 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvez9d01b6f2012-12-10 11:50:02 +0100197 /* In case of RS-485, change from TX to RX when finishing TX.
198 * Half-duplex. */
199 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
200 iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
201 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
202 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200203 }
204
205 /* RX data */
206 if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
207 ipoctal_irq_rx(channel, tty, sr);
208
209 /* TX of each character */
210 if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
211 ipoctal_irq_tx(channel);
212
213 tty_flip_buffer_push(tty);
214 tty_kref_put(tty);
215}
216
Jens Taproggefaa75c42012-09-12 14:55:38 +0200217static irqreturn_t ipoctal_irq_handler(void *arg)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200218{
219 unsigned int i;
220 struct ipoctal *ipoctal = (struct ipoctal *) arg;
221
222 /* Check all channels */
223 for (i = 0; i < NR_CHANNELS; i++)
224 ipoctal_irq_channel(&ipoctal->channel[i]);
225
Jens Taproggeea991142012-09-13 12:32:20 +0200226 /* Clear the IPack device interrupt */
Jens Taprogge402228d2012-09-27 12:37:34 +0200227 readw(ipoctal->int_space + ACK_INT_REQ0);
228 readw(ipoctal->int_space + ACK_INT_REQ1);
Jens Taproggeea991142012-09-13 12:32:20 +0200229
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200230 return IRQ_HANDLED;
231}
232
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200233static const struct tty_port_operations ipoctal_tty_port_ops = {
234 .dtr_rts = NULL,
235 .activate = ipoctal_port_activate,
236};
237
238static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200239 unsigned int slot)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200240{
Jens Taprogge402228d2012-09-27 12:37:34 +0200241 int res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200242 int i;
243 struct tty_driver *tty;
244 char name[20];
Jens Taprogge70a32812012-09-12 14:55:26 +0200245 struct ipoctal_channel *channel;
Jens Taprogge402228d2012-09-27 12:37:34 +0200246 struct ipack_region *region;
247 void __iomem *addr;
Jens Taprogge70a32812012-09-12 14:55:26 +0200248 union scc2698_channel __iomem *chan_regs;
249 union scc2698_block __iomem *block_regs;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200250
Jens Taprogge2ec678d2012-09-27 12:37:33 +0200251 ipoctal->board_id = ipoctal->dev->id_device;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200252
Jens Taprogge402228d2012-09-27 12:37:34 +0200253 region = &ipoctal->dev->region[IPACK_IO_SPACE];
254 addr = devm_ioremap_nocache(&ipoctal->dev->dev,
255 region->start, region->size);
256 if (!addr) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200257 dev_err(&ipoctal->dev->dev,
258 "Unable to map slot [%d:%d] IO space!\n",
259 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200260 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200261 }
Jens Taprogge402228d2012-09-27 12:37:34 +0200262 /* Save the virtual address to access the registers easily */
263 chan_regs =
264 (union scc2698_channel __iomem *) addr;
265 block_regs =
266 (union scc2698_block __iomem *) addr;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200267
Jens Taprogge402228d2012-09-27 12:37:34 +0200268 region = &ipoctal->dev->region[IPACK_INT_SPACE];
269 ipoctal->int_space =
270 devm_ioremap_nocache(&ipoctal->dev->dev,
271 region->start, region->size);
272 if (!ipoctal->int_space) {
Jens Taproggeea991142012-09-13 12:32:20 +0200273 dev_err(&ipoctal->dev->dev,
274 "Unable to map slot [%d:%d] INT space!\n",
275 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200276 return -EADDRNOTAVAIL;
Jens Taproggeea991142012-09-13 12:32:20 +0200277 }
278
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200279 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
280 ipoctal->mem8_space =
Jens Taprogge402228d2012-09-27 12:37:34 +0200281 devm_ioremap_nocache(&ipoctal->dev->dev,
282 region->start, 0x8000);
283 if (!addr) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200284 dev_err(&ipoctal->dev->dev,
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200285 "Unable to map slot [%d:%d] MEM8 space!\n",
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200286 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200287 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200288 }
289
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200290
291 /* Disable RX and TX before touching anything */
292 for (i = 0; i < NR_CHANNELS ; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200293 struct ipoctal_channel *channel = &ipoctal->channel[i];
294 channel->regs = chan_regs + i;
295 channel->block_regs = block_regs + (i >> 1);
Jens Taproggeef79de02012-09-12 14:55:28 +0200296 channel->board_id = ipoctal->board_id;
Jens Taprogge4e4732a2012-09-12 14:55:29 +0200297 if (i & 1) {
298 channel->isr_tx_rdy_mask = ISR_TxRDY_B;
299 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
300 } else {
301 channel->isr_tx_rdy_mask = ISR_TxRDY_A;
302 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
303 }
Jens Taprogge70a32812012-09-12 14:55:26 +0200304
Jens Taprogge459e6d72012-09-12 14:55:27 +0200305 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
306 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
307 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
308 iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
309 &channel->regs->w.mr); /* mr1 */
310 iowrite8(0, &channel->regs->w.mr); /* mr2 */
311 iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200312 }
313
314 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200315 iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
316 iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
317 &block_regs[i].w.opcr);
318 iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
319 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
320 &block_regs[i].w.imr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200321 }
322
323 /*
324 * IP-OCTAL has different addresses to copy its IRQ vector.
325 * Depending of the carrier these addresses are accesible or not.
326 * More info in the datasheet.
327 */
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200328 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200329 ipoctal_irq_handler, ipoctal);
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200330 /* Dummy write */
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200331 iowrite8(1, ipoctal->mem8_space + 1);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200332
333 /* Register the TTY device */
334
335 /* Each IP-OCTAL channel is a TTY port */
336 tty = alloc_tty_driver(NR_CHANNELS);
337
Jens Taprogge402228d2012-09-27 12:37:34 +0200338 if (!tty)
339 return -ENOMEM;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200340
341 /* Fill struct tty_driver with ipoctal data */
342 tty->owner = THIS_MODULE;
Jens Taprogge3f3a5922012-09-12 14:55:41 +0200343 tty->driver_name = KBUILD_MODNAME;
344 sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200345 tty->name = name;
346 tty->major = 0;
347
348 tty->minor_start = 0;
349 tty->type = TTY_DRIVER_TYPE_SERIAL;
350 tty->subtype = SERIAL_TYPE_NORMAL;
351 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
352 tty->init_termios = tty_std_termios;
353 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
354 tty->init_termios.c_ispeed = 9600;
355 tty->init_termios.c_ospeed = 9600;
356
357 tty_set_operations(tty, &ipoctal_fops);
358 res = tty_register_driver(tty);
359 if (res) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200360 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200361 put_tty_driver(tty);
Jens Taprogge402228d2012-09-27 12:37:34 +0200362 return res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200363 }
364
365 /* Save struct tty_driver for use it when uninstalling the device */
366 ipoctal->tty_drv = tty;
367
368 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200369 struct device *tty_dev;
370
Jens Taprogge70a32812012-09-12 14:55:26 +0200371 channel = &ipoctal->channel[i];
372 tty_port_init(&channel->tty_port);
373 tty_port_alloc_xmit_buf(&channel->tty_port);
374 channel->tty_port.ops = &ipoctal_tty_port_ops;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200375
Jens Taprogge70a32812012-09-12 14:55:26 +0200376 ipoctal_reset_stats(&channel->stats);
377 channel->nb_bytes = 0;
Jens Taprogge70a32812012-09-12 14:55:26 +0200378 spin_lock_init(&channel->lock);
379 channel->pointer_read = 0;
380 channel->pointer_write = 0;
Linus Torvalds3498d132012-10-01 12:26:52 -0700381 tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200382 if (IS_ERR(tty_dev)) {
383 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
Jiri Slaby191c5f12012-11-15 09:49:56 +0100384 tty_port_destroy(&channel->tty_port);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200385 continue;
386 }
Jens Taprogge9c1d7842012-09-12 14:55:42 +0200387 dev_set_drvdata(tty_dev, channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200388
389 /*
390 * Enable again the RX. TX will be enabled when
391 * there is something to send
392 */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200393 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200394 }
395
396 return 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200397}
398
Jens Taprogge70a32812012-09-12 14:55:26 +0200399static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200400 const unsigned char *buf,
401 int count)
402{
403 unsigned long flags;
404 int i;
Jens Taprogge70a32812012-09-12 14:55:26 +0200405 unsigned int *pointer_read = &channel->pointer_read;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200406
407 /* Copy the bytes from the user buffer to the internal one */
408 for (i = 0; i < count; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200409 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
410 spin_lock_irqsave(&channel->lock, flags);
411 channel->tty_port.xmit_buf[*pointer_read] = buf[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200412 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
Jens Taprogge70a32812012-09-12 14:55:26 +0200413 channel->nb_bytes++;
414 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200415 } else {
416 break;
417 }
418 }
419 return i;
420}
421
Jens Taprogge699a89f2012-09-12 14:55:31 +0200422static int ipoctal_write_tty(struct tty_struct *tty,
423 const unsigned char *buf, int count)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200424{
Jens Taprogge699a89f2012-09-12 14:55:31 +0200425 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200426 unsigned int char_copied;
Jens Taprogge699a89f2012-09-12 14:55:31 +0200427
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200428 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200429
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200430 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
Jens Taproggeef79de02012-09-12 14:55:28 +0200431 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200432 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
433 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200434 }
435
436 /*
437 * Send a packet and then disable TX to avoid failure after several send
438 * operations
439 */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200440 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200441 return char_copied;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200442}
443
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200444static int ipoctal_write_room(struct tty_struct *tty)
445{
Jens Taproggeef79de02012-09-12 14:55:28 +0200446 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200447
Jens Taprogge70a32812012-09-12 14:55:26 +0200448 return PAGE_SIZE - channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200449}
450
451static int ipoctal_chars_in_buffer(struct tty_struct *tty)
452{
Jens Taproggeef79de02012-09-12 14:55:28 +0200453 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200454
Jens Taprogge70a32812012-09-12 14:55:26 +0200455 return channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200456}
457
458static void ipoctal_set_termios(struct tty_struct *tty,
459 struct ktermios *old_termios)
460{
461 unsigned int cflag;
462 unsigned char mr1 = 0;
463 unsigned char mr2 = 0;
464 unsigned char csr = 0;
Jens Taproggeef79de02012-09-12 14:55:28 +0200465 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200466 speed_t baud;
467
Alan Cox857196e2012-07-26 19:00:51 +0100468 cflag = tty->termios.c_cflag;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200469
470 /* Disable and reset everything before change the setup */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200471 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
472 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
473 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
474 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
475 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200476
477 /* Set Bits per chars */
478 switch (cflag & CSIZE) {
479 case CS6:
480 mr1 |= MR1_CHRL_6_BITS;
481 break;
482 case CS7:
483 mr1 |= MR1_CHRL_7_BITS;
484 break;
485 case CS8:
486 default:
487 mr1 |= MR1_CHRL_8_BITS;
488 /* By default, select CS8 */
Alan Cox857196e2012-07-26 19:00:51 +0100489 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200490 break;
491 }
492
493 /* Set Parity */
494 if (cflag & PARENB)
495 if (cflag & PARODD)
496 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
497 else
498 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
499 else
500 mr1 |= MR1_PARITY_OFF;
501
502 /* Mark or space parity is not supported */
Alan Cox857196e2012-07-26 19:00:51 +0100503 tty->termios.c_cflag &= ~CMSPAR;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200504
505 /* Set stop bits */
506 if (cflag & CSTOPB)
507 mr2 |= MR2_STOP_BITS_LENGTH_2;
508 else
509 mr2 |= MR2_STOP_BITS_LENGTH_1;
510
511 /* Set the flow control */
Jens Taproggeef79de02012-09-12 14:55:28 +0200512 switch (channel->board_id) {
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200513 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200514 if (cflag & CRTSCTS) {
515 mr1 |= MR1_RxRTS_CONTROL_ON;
516 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200517 } else {
518 mr1 |= MR1_RxRTS_CONTROL_OFF;
519 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200520 }
521 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200522 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200523 mr1 |= MR1_RxRTS_CONTROL_OFF;
524 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200525 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200526 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200527 mr1 |= MR1_RxRTS_CONTROL_OFF;
528 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200529 break;
530 default:
531 return;
532 break;
533 }
534
535 baud = tty_get_baud_rate(tty);
Alan Cox857196e2012-07-26 19:00:51 +0100536 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200537
538 /* Set baud rate */
Alan Cox857196e2012-07-26 19:00:51 +0100539 switch (baud) {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200540 case 75:
541 csr |= TX_CLK_75 | RX_CLK_75;
542 break;
543 case 110:
544 csr |= TX_CLK_110 | RX_CLK_110;
545 break;
546 case 150:
547 csr |= TX_CLK_150 | RX_CLK_150;
548 break;
549 case 300:
550 csr |= TX_CLK_300 | RX_CLK_300;
551 break;
552 case 600:
553 csr |= TX_CLK_600 | RX_CLK_600;
554 break;
555 case 1200:
556 csr |= TX_CLK_1200 | RX_CLK_1200;
557 break;
558 case 1800:
559 csr |= TX_CLK_1800 | RX_CLK_1800;
560 break;
561 case 2000:
562 csr |= TX_CLK_2000 | RX_CLK_2000;
563 break;
564 case 2400:
565 csr |= TX_CLK_2400 | RX_CLK_2400;
566 break;
567 case 4800:
568 csr |= TX_CLK_4800 | RX_CLK_4800;
569 break;
570 case 9600:
571 csr |= TX_CLK_9600 | RX_CLK_9600;
572 break;
573 case 19200:
574 csr |= TX_CLK_19200 | RX_CLK_19200;
575 break;
576 case 38400:
577 default:
578 csr |= TX_CLK_38400 | RX_CLK_38400;
579 /* In case of default, we establish 38400 bps */
Alan Cox857196e2012-07-26 19:00:51 +0100580 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200581 break;
582 }
583
584 mr1 |= MR1_ERROR_CHAR;
585 mr1 |= MR1_RxINT_RxRDY;
586
587 /* Write the control registers */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200588 iowrite8(mr1, &channel->regs->w.mr);
589 iowrite8(mr2, &channel->regs->w.mr);
590 iowrite8(csr, &channel->regs->w.csr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200591
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200592 /* Enable again the RX */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200593 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200594}
595
596static void ipoctal_hangup(struct tty_struct *tty)
597{
598 unsigned long flags;
Jens Taproggeef79de02012-09-12 14:55:28 +0200599 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200600
Jens Taproggeef79de02012-09-12 14:55:28 +0200601 if (channel == NULL)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200602 return;
603
Jens Taprogge70a32812012-09-12 14:55:26 +0200604 spin_lock_irqsave(&channel->lock, flags);
605 channel->nb_bytes = 0;
606 channel->pointer_read = 0;
607 channel->pointer_write = 0;
608 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200609
Jens Taprogge70a32812012-09-12 14:55:26 +0200610 tty_port_hangup(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200611
Jens Taprogge459e6d72012-09-12 14:55:27 +0200612 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
613 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
614 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
615 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
616 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200617
Jens Taprogge70a32812012-09-12 14:55:26 +0200618 clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
619 wake_up_interruptible(&channel->tty_port.open_wait);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200620}
621
622static const struct tty_operations ipoctal_fops = {
623 .ioctl = NULL,
624 .open = ipoctal_open,
625 .close = ipoctal_close,
626 .write = ipoctal_write_tty,
627 .set_termios = ipoctal_set_termios,
628 .write_room = ipoctal_write_room,
629 .chars_in_buffer = ipoctal_chars_in_buffer,
630 .get_icount = ipoctal_get_icount,
631 .hangup = ipoctal_hangup,
632};
633
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200634static int ipoctal_probe(struct ipack_device *dev)
635{
636 int res;
637 struct ipoctal *ipoctal;
638
639 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
640 if (ipoctal == NULL)
641 return -ENOMEM;
642
643 ipoctal->dev = dev;
Jens Taproggef9e314d2012-09-27 12:37:25 +0200644 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200645 if (res)
646 goto out_uninst;
647
Jens Taprogge1adda492012-09-12 14:55:39 +0200648 dev_set_drvdata(&dev->dev, ipoctal);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200649 return 0;
650
651out_uninst:
652 kfree(ipoctal);
653 return res;
654}
655
656static void __ipoctal_remove(struct ipoctal *ipoctal)
657{
658 int i;
659
Samuel Iglesias Gonsálvez690949e2012-09-11 13:35:08 +0200660 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
661
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200662 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200663 struct ipoctal_channel *channel = &ipoctal->channel[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200664 tty_unregister_device(ipoctal->tty_drv, i);
Jens Taprogge70a32812012-09-12 14:55:26 +0200665 tty_port_free_xmit_buf(&channel->tty_port);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100666 tty_port_destroy(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200667 }
668
669 tty_unregister_driver(ipoctal->tty_drv);
670 put_tty_driver(ipoctal->tty_drv);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200671 kfree(ipoctal);
672}
673
Jens Taprogge1adda492012-09-12 14:55:39 +0200674static void ipoctal_remove(struct ipack_device *idev)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200675{
Jens Taprogge1adda492012-09-12 14:55:39 +0200676 __ipoctal_remove(dev_get_drvdata(&idev->dev));
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200677}
678
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200679static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
680 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
681 IPACK1_DEVICE_ID_SBS_OCTAL_232) },
682 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
683 IPACK1_DEVICE_ID_SBS_OCTAL_422) },
684 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
685 IPACK1_DEVICE_ID_SBS_OCTAL_485) },
686 { 0, },
687};
688
689MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
690
Jens Taproggee8011132012-09-04 17:01:17 +0200691static const struct ipack_driver_ops ipoctal_drv_ops = {
Jens Taprogge4aa09d42012-09-04 17:01:19 +0200692 .probe = ipoctal_probe,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200693 .remove = ipoctal_remove,
694};
695
Jens Taproggee8011132012-09-04 17:01:17 +0200696static struct ipack_driver driver = {
697 .ops = &ipoctal_drv_ops,
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200698 .id_table = ipoctal_ids,
Jens Taproggee8011132012-09-04 17:01:17 +0200699};
700
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200701static int __init ipoctal_init(void)
702{
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200703 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200704}
705
706static void __exit ipoctal_exit(void)
707{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200708 ipack_driver_unregister(&driver);
709}
710
711MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
712MODULE_LICENSE("GPL");
713
714module_init(ipoctal_init);
715module_exit(ipoctal_exit);