blob: 5ce2c4c5bb3231a164b1ae905ef37e5811f4c174 [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
Samuel Iglesias Gonsalveza3882b72012-12-10 11:50:03 +010063 /*
64 * Enable RX. TX will be enabled when
65 * there is something to send
66 */
Jens Taprogge459e6d72012-09-12 14:55:27 +020067 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020068 return 0;
69}
70
71static int ipoctal_open(struct tty_struct *tty, struct file *file)
72{
Jens Taprogge70a32812012-09-12 14:55:26 +020073 struct ipoctal_channel *channel;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020074
Jens Taprogge9c1d7842012-09-12 14:55:42 +020075 channel = dev_get_drvdata(tty->dev);
Jens Taproggeef79de02012-09-12 14:55:28 +020076 tty->driver_data = channel;
Samuel Iglesias Gonsálvez1337b072012-07-13 13:33:13 +020077
Samuel Iglesias Gonsalvez7e5730d2012-12-10 11:49:59 +010078 return tty_port_open(&channel->tty_port, tty, file);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020079}
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
Jens Taproggeef79de02012-09-12 14:55:28 +020091static void ipoctal_free_channel(struct ipoctal_channel *channel)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020092{
Jens Taprogge70a32812012-09-12 14:55:26 +020093 ipoctal_reset_stats(&channel->stats);
94 channel->pointer_read = 0;
95 channel->pointer_write = 0;
96 channel->nb_bytes = 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020097}
98
99static void ipoctal_close(struct tty_struct *tty, struct file *filp)
100{
Jens Taproggeef79de02012-09-12 14:55:28 +0200101 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200102
Jens Taprogge70a32812012-09-12 14:55:26 +0200103 tty_port_close(&channel->tty_port, tty, filp);
Samuel Iglesias Gonsalvez7e5730d2012-12-10 11:49:59 +0100104 ipoctal_free_channel(channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200105}
106
107static int ipoctal_get_icount(struct tty_struct *tty,
108 struct serial_icounter_struct *icount)
109{
Jens Taproggeef79de02012-09-12 14:55:28 +0200110 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200111
112 icount->cts = 0;
113 icount->dsr = 0;
114 icount->rng = 0;
115 icount->dcd = 0;
Jens Taprogge70a32812012-09-12 14:55:26 +0200116 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;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200121 return 0;
122}
123
Jens Taprogge87cfb952012-09-12 14:55:30 +0200124static void ipoctal_irq_rx(struct ipoctal_channel *channel,
125 struct tty_struct *tty, u8 sr)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200126{
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200127 unsigned char value;
Samuel Iglesias Gonsalvezb5071f22012-12-10 11:50:01 +0100128 unsigned char flag;
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200129 u8 isr;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200130
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200131 do {
132 value = ioread8(&channel->regs->r.rhr);
Samuel Iglesias Gonsalvezb5071f22012-12-10 11:50:01 +0100133 flag = TTY_NORMAL;
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200134 /* Error: count statistics */
135 if (sr & SR_ERROR) {
136 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200137
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200138 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) {
Samuel Iglesias Gonsalvez45141082012-09-13 12:32:22 +0200152 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200153 channel->stats.rcv_break++;
154 flag = TTY_BREAK;
155 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200156 }
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200157 tty_insert_flip_char(tty, value, flag);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200158
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200159 /* 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);
166
167 tty_flip_buffer_push(tty);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200168}
169
170static void ipoctal_irq_tx(struct ipoctal_channel *channel)
171{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200172 unsigned char value;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200173 unsigned int *pointer_write = &channel->pointer_write;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200174
Alberto Garcia69a6b9b2012-12-10 11:49:58 +0100175 if (channel->nb_bytes == 0)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200176 return;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200177
Jens Taprogge87cfb952012-09-12 14:55:30 +0200178 value = channel->tty_port.xmit_buf[*pointer_write];
179 iowrite8(value, &channel->regs->w.thr);
180 channel->stats.tx++;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200181 (*pointer_write)++;
182 *pointer_write = *pointer_write % PAGE_SIZE;
183 channel->nb_bytes--;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200184}
185
186static void ipoctal_irq_channel(struct ipoctal_channel *channel)
187{
188 u8 isr, sr;
189 struct tty_struct *tty;
190
Jens Taprogge87cfb952012-09-12 14:55:30 +0200191 tty = tty_port_tty_get(&channel->tty_port);
192 if (!tty)
193 return;
Samuel Iglesias Gonsalveze7e664f2012-12-10 11:50:05 +0100194
195 spin_lock(&channel->lock);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200196 /* The HW is organized in pair of channels. See which register we need
197 * to read from */
198 isr = ioread8(&channel->block_regs->r.isr);
199 sr = ioread8(&channel->regs->r.sr);
200
Samuel Iglesias Gonsalvez9d01b6f2012-12-10 11:50:02 +0100201 if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
Jens Taprogge87cfb952012-09-12 14:55:30 +0200202 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvez9d01b6f2012-12-10 11:50:02 +0100203 /* 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 iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
207 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
208 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200209 }
210
211 /* RX data */
212 if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
213 ipoctal_irq_rx(channel, tty, sr);
214
215 /* TX of each character */
216 if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
217 ipoctal_irq_tx(channel);
218
219 tty_flip_buffer_push(tty);
220 tty_kref_put(tty);
Samuel Iglesias Gonsalveze7e664f2012-12-10 11:50:05 +0100221 spin_unlock(&channel->lock);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200222}
223
Jens Taproggefaa75c42012-09-12 14:55:38 +0200224static irqreturn_t ipoctal_irq_handler(void *arg)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200225{
226 unsigned int i;
227 struct ipoctal *ipoctal = (struct ipoctal *) arg;
228
Jens Taproggeea991142012-09-13 12:32:20 +0200229 /* Clear the IPack device interrupt */
Jens Taprogge402228d2012-09-27 12:37:34 +0200230 readw(ipoctal->int_space + ACK_INT_REQ0);
231 readw(ipoctal->int_space + ACK_INT_REQ1);
Jens Taproggeea991142012-09-13 12:32:20 +0200232
Samuel Iglesias Gonsalvez21d27ed2012-12-10 11:50:04 +0100233 /* Check all channels */
234 for (i = 0; i < NR_CHANNELS; i++)
235 ipoctal_irq_channel(&ipoctal->channel[i]);
236
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200237 return IRQ_HANDLED;
238}
239
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200240static const struct tty_port_operations ipoctal_tty_port_ops = {
241 .dtr_rts = NULL,
242 .activate = ipoctal_port_activate,
243};
244
245static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200246 unsigned int slot)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200247{
Jens Taprogge402228d2012-09-27 12:37:34 +0200248 int res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200249 int i;
250 struct tty_driver *tty;
251 char name[20];
Jens Taprogge70a32812012-09-12 14:55:26 +0200252 struct ipoctal_channel *channel;
Jens Taprogge402228d2012-09-27 12:37:34 +0200253 struct ipack_region *region;
254 void __iomem *addr;
Jens Taprogge70a32812012-09-12 14:55:26 +0200255 union scc2698_channel __iomem *chan_regs;
256 union scc2698_block __iomem *block_regs;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200257
Jens Taprogge2ec678d2012-09-27 12:37:33 +0200258 ipoctal->board_id = ipoctal->dev->id_device;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200259
Jens Taprogge402228d2012-09-27 12:37:34 +0200260 region = &ipoctal->dev->region[IPACK_IO_SPACE];
261 addr = devm_ioremap_nocache(&ipoctal->dev->dev,
262 region->start, region->size);
263 if (!addr) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200264 dev_err(&ipoctal->dev->dev,
265 "Unable to map slot [%d:%d] IO space!\n",
266 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200267 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200268 }
Jens Taprogge402228d2012-09-27 12:37:34 +0200269 /* Save the virtual address to access the registers easily */
270 chan_regs =
271 (union scc2698_channel __iomem *) addr;
272 block_regs =
273 (union scc2698_block __iomem *) addr;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200274
Jens Taprogge402228d2012-09-27 12:37:34 +0200275 region = &ipoctal->dev->region[IPACK_INT_SPACE];
276 ipoctal->int_space =
277 devm_ioremap_nocache(&ipoctal->dev->dev,
278 region->start, region->size);
279 if (!ipoctal->int_space) {
Jens Taproggeea991142012-09-13 12:32:20 +0200280 dev_err(&ipoctal->dev->dev,
281 "Unable to map slot [%d:%d] INT space!\n",
282 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200283 return -EADDRNOTAVAIL;
Jens Taproggeea991142012-09-13 12:32:20 +0200284 }
285
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200286 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
287 ipoctal->mem8_space =
Jens Taprogge402228d2012-09-27 12:37:34 +0200288 devm_ioremap_nocache(&ipoctal->dev->dev,
289 region->start, 0x8000);
290 if (!addr) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200291 dev_err(&ipoctal->dev->dev,
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200292 "Unable to map slot [%d:%d] MEM8 space!\n",
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200293 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200294 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200295 }
296
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200297
298 /* Disable RX and TX before touching anything */
299 for (i = 0; i < NR_CHANNELS ; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200300 struct ipoctal_channel *channel = &ipoctal->channel[i];
301 channel->regs = chan_regs + i;
302 channel->block_regs = block_regs + (i >> 1);
Jens Taproggeef79de02012-09-12 14:55:28 +0200303 channel->board_id = ipoctal->board_id;
Jens Taprogge4e4732a2012-09-12 14:55:29 +0200304 if (i & 1) {
305 channel->isr_tx_rdy_mask = ISR_TxRDY_B;
306 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
307 } else {
308 channel->isr_tx_rdy_mask = ISR_TxRDY_A;
309 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
310 }
Jens Taprogge70a32812012-09-12 14:55:26 +0200311
Jens Taprogge459e6d72012-09-12 14:55:27 +0200312 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
313 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
314 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
315 iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
316 &channel->regs->w.mr); /* mr1 */
317 iowrite8(0, &channel->regs->w.mr); /* mr2 */
318 iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200319 }
320
321 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200322 iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
323 iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
324 &block_regs[i].w.opcr);
325 iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
326 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
327 &block_regs[i].w.imr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200328 }
329
330 /*
331 * IP-OCTAL has different addresses to copy its IRQ vector.
332 * Depending of the carrier these addresses are accesible or not.
333 * More info in the datasheet.
334 */
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200335 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200336 ipoctal_irq_handler, ipoctal);
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200337 /* Dummy write */
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200338 iowrite8(1, ipoctal->mem8_space + 1);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200339
340 /* Register the TTY device */
341
342 /* Each IP-OCTAL channel is a TTY port */
343 tty = alloc_tty_driver(NR_CHANNELS);
344
Jens Taprogge402228d2012-09-27 12:37:34 +0200345 if (!tty)
346 return -ENOMEM;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200347
348 /* Fill struct tty_driver with ipoctal data */
349 tty->owner = THIS_MODULE;
Jens Taprogge3f3a5922012-09-12 14:55:41 +0200350 tty->driver_name = KBUILD_MODNAME;
351 sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200352 tty->name = name;
353 tty->major = 0;
354
355 tty->minor_start = 0;
356 tty->type = TTY_DRIVER_TYPE_SERIAL;
357 tty->subtype = SERIAL_TYPE_NORMAL;
358 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
359 tty->init_termios = tty_std_termios;
360 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
361 tty->init_termios.c_ispeed = 9600;
362 tty->init_termios.c_ospeed = 9600;
363
364 tty_set_operations(tty, &ipoctal_fops);
365 res = tty_register_driver(tty);
366 if (res) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200367 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200368 put_tty_driver(tty);
Jens Taprogge402228d2012-09-27 12:37:34 +0200369 return res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200370 }
371
372 /* Save struct tty_driver for use it when uninstalling the device */
373 ipoctal->tty_drv = tty;
374
375 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200376 struct device *tty_dev;
377
Jens Taprogge70a32812012-09-12 14:55:26 +0200378 channel = &ipoctal->channel[i];
379 tty_port_init(&channel->tty_port);
380 tty_port_alloc_xmit_buf(&channel->tty_port);
381 channel->tty_port.ops = &ipoctal_tty_port_ops;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200382
Jens Taprogge70a32812012-09-12 14:55:26 +0200383 ipoctal_reset_stats(&channel->stats);
384 channel->nb_bytes = 0;
Jens Taprogge70a32812012-09-12 14:55:26 +0200385 spin_lock_init(&channel->lock);
386 channel->pointer_read = 0;
387 channel->pointer_write = 0;
Linus Torvalds3498d132012-10-01 12:26:52 -0700388 tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200389 if (IS_ERR(tty_dev)) {
390 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
Jiri Slaby191c5f12012-11-15 09:49:56 +0100391 tty_port_destroy(&channel->tty_port);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200392 continue;
393 }
Jens Taprogge9c1d7842012-09-12 14:55:42 +0200394 dev_set_drvdata(tty_dev, channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200395 }
396
397 return 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200398}
399
Jens Taprogge70a32812012-09-12 14:55:26 +0200400static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200401 const unsigned char *buf,
402 int count)
403{
404 unsigned long flags;
405 int i;
Jens Taprogge70a32812012-09-12 14:55:26 +0200406 unsigned int *pointer_read = &channel->pointer_read;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200407
408 /* Copy the bytes from the user buffer to the internal one */
409 for (i = 0; i < count; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200410 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
411 spin_lock_irqsave(&channel->lock, flags);
412 channel->tty_port.xmit_buf[*pointer_read] = buf[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200413 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
Jens Taprogge70a32812012-09-12 14:55:26 +0200414 channel->nb_bytes++;
415 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200416 } else {
417 break;
418 }
419 }
420 return i;
421}
422
Jens Taprogge699a89f2012-09-12 14:55:31 +0200423static int ipoctal_write_tty(struct tty_struct *tty,
424 const unsigned char *buf, int count)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200425{
Jens Taprogge699a89f2012-09-12 14:55:31 +0200426 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200427 unsigned int char_copied;
Jens Taprogge699a89f2012-09-12 14:55:31 +0200428
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200429 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200430
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200431 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
Jens Taproggeef79de02012-09-12 14:55:28 +0200432 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200433 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
434 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200435 }
436
437 /*
438 * Send a packet and then disable TX to avoid failure after several send
439 * operations
440 */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200441 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200442 return char_copied;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200443}
444
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200445static int ipoctal_write_room(struct tty_struct *tty)
446{
Jens Taproggeef79de02012-09-12 14:55:28 +0200447 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200448
Jens Taprogge70a32812012-09-12 14:55:26 +0200449 return PAGE_SIZE - channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200450}
451
452static int ipoctal_chars_in_buffer(struct tty_struct *tty)
453{
Jens Taproggeef79de02012-09-12 14:55:28 +0200454 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200455
Jens Taprogge70a32812012-09-12 14:55:26 +0200456 return channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200457}
458
459static void ipoctal_set_termios(struct tty_struct *tty,
460 struct ktermios *old_termios)
461{
462 unsigned int cflag;
463 unsigned char mr1 = 0;
464 unsigned char mr2 = 0;
465 unsigned char csr = 0;
Jens Taproggeef79de02012-09-12 14:55:28 +0200466 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200467 speed_t baud;
468
Alan Cox857196e2012-07-26 19:00:51 +0100469 cflag = tty->termios.c_cflag;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200470
471 /* Disable and reset everything before change the setup */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200472 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
473 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
474 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
475 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
476 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200477
478 /* Set Bits per chars */
479 switch (cflag & CSIZE) {
480 case CS6:
481 mr1 |= MR1_CHRL_6_BITS;
482 break;
483 case CS7:
484 mr1 |= MR1_CHRL_7_BITS;
485 break;
486 case CS8:
487 default:
488 mr1 |= MR1_CHRL_8_BITS;
489 /* By default, select CS8 */
Alan Cox857196e2012-07-26 19:00:51 +0100490 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200491 break;
492 }
493
494 /* Set Parity */
495 if (cflag & PARENB)
496 if (cflag & PARODD)
497 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
498 else
499 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
500 else
501 mr1 |= MR1_PARITY_OFF;
502
503 /* Mark or space parity is not supported */
Alan Cox857196e2012-07-26 19:00:51 +0100504 tty->termios.c_cflag &= ~CMSPAR;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200505
506 /* Set stop bits */
507 if (cflag & CSTOPB)
508 mr2 |= MR2_STOP_BITS_LENGTH_2;
509 else
510 mr2 |= MR2_STOP_BITS_LENGTH_1;
511
512 /* Set the flow control */
Jens Taproggeef79de02012-09-12 14:55:28 +0200513 switch (channel->board_id) {
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200514 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200515 if (cflag & CRTSCTS) {
516 mr1 |= MR1_RxRTS_CONTROL_ON;
517 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200518 } else {
519 mr1 |= MR1_RxRTS_CONTROL_OFF;
520 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200521 }
522 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200523 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200524 mr1 |= MR1_RxRTS_CONTROL_OFF;
525 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200526 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200527 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200528 mr1 |= MR1_RxRTS_CONTROL_OFF;
529 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200530 break;
531 default:
532 return;
533 break;
534 }
535
536 baud = tty_get_baud_rate(tty);
Alan Cox857196e2012-07-26 19:00:51 +0100537 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200538
539 /* Set baud rate */
Alan Cox857196e2012-07-26 19:00:51 +0100540 switch (baud) {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200541 case 75:
542 csr |= TX_CLK_75 | RX_CLK_75;
543 break;
544 case 110:
545 csr |= TX_CLK_110 | RX_CLK_110;
546 break;
547 case 150:
548 csr |= TX_CLK_150 | RX_CLK_150;
549 break;
550 case 300:
551 csr |= TX_CLK_300 | RX_CLK_300;
552 break;
553 case 600:
554 csr |= TX_CLK_600 | RX_CLK_600;
555 break;
556 case 1200:
557 csr |= TX_CLK_1200 | RX_CLK_1200;
558 break;
559 case 1800:
560 csr |= TX_CLK_1800 | RX_CLK_1800;
561 break;
562 case 2000:
563 csr |= TX_CLK_2000 | RX_CLK_2000;
564 break;
565 case 2400:
566 csr |= TX_CLK_2400 | RX_CLK_2400;
567 break;
568 case 4800:
569 csr |= TX_CLK_4800 | RX_CLK_4800;
570 break;
571 case 9600:
572 csr |= TX_CLK_9600 | RX_CLK_9600;
573 break;
574 case 19200:
575 csr |= TX_CLK_19200 | RX_CLK_19200;
576 break;
577 case 38400:
578 default:
579 csr |= TX_CLK_38400 | RX_CLK_38400;
580 /* In case of default, we establish 38400 bps */
Alan Cox857196e2012-07-26 19:00:51 +0100581 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200582 break;
583 }
584
585 mr1 |= MR1_ERROR_CHAR;
586 mr1 |= MR1_RxINT_RxRDY;
587
588 /* Write the control registers */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200589 iowrite8(mr1, &channel->regs->w.mr);
590 iowrite8(mr2, &channel->regs->w.mr);
591 iowrite8(csr, &channel->regs->w.csr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200592
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200593 /* Enable again the RX */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200594 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200595}
596
597static void ipoctal_hangup(struct tty_struct *tty)
598{
599 unsigned long flags;
Jens Taproggeef79de02012-09-12 14:55:28 +0200600 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200601
Jens Taproggeef79de02012-09-12 14:55:28 +0200602 if (channel == NULL)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200603 return;
604
Jens Taprogge70a32812012-09-12 14:55:26 +0200605 spin_lock_irqsave(&channel->lock, flags);
606 channel->nb_bytes = 0;
607 channel->pointer_read = 0;
608 channel->pointer_write = 0;
609 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200610
Jens Taprogge70a32812012-09-12 14:55:26 +0200611 tty_port_hangup(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200612
Jens Taprogge459e6d72012-09-12 14:55:27 +0200613 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
614 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
615 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
616 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
617 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200618
Jens Taprogge70a32812012-09-12 14:55:26 +0200619 clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
620 wake_up_interruptible(&channel->tty_port.open_wait);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200621}
622
623static const struct tty_operations ipoctal_fops = {
624 .ioctl = NULL,
625 .open = ipoctal_open,
626 .close = ipoctal_close,
627 .write = ipoctal_write_tty,
628 .set_termios = ipoctal_set_termios,
629 .write_room = ipoctal_write_room,
630 .chars_in_buffer = ipoctal_chars_in_buffer,
631 .get_icount = ipoctal_get_icount,
632 .hangup = ipoctal_hangup,
633};
634
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200635static int ipoctal_probe(struct ipack_device *dev)
636{
637 int res;
638 struct ipoctal *ipoctal;
639
640 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
641 if (ipoctal == NULL)
642 return -ENOMEM;
643
644 ipoctal->dev = dev;
Jens Taproggef9e314d2012-09-27 12:37:25 +0200645 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200646 if (res)
647 goto out_uninst;
648
Jens Taprogge1adda492012-09-12 14:55:39 +0200649 dev_set_drvdata(&dev->dev, ipoctal);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200650 return 0;
651
652out_uninst:
653 kfree(ipoctal);
654 return res;
655}
656
657static void __ipoctal_remove(struct ipoctal *ipoctal)
658{
659 int i;
660
Samuel Iglesias Gonsálvez690949e2012-09-11 13:35:08 +0200661 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
662
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200663 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200664 struct ipoctal_channel *channel = &ipoctal->channel[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200665 tty_unregister_device(ipoctal->tty_drv, i);
Jens Taprogge70a32812012-09-12 14:55:26 +0200666 tty_port_free_xmit_buf(&channel->tty_port);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100667 tty_port_destroy(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200668 }
669
670 tty_unregister_driver(ipoctal->tty_drv);
671 put_tty_driver(ipoctal->tty_drv);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200672 kfree(ipoctal);
673}
674
Jens Taprogge1adda492012-09-12 14:55:39 +0200675static void ipoctal_remove(struct ipack_device *idev)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200676{
Jens Taprogge1adda492012-09-12 14:55:39 +0200677 __ipoctal_remove(dev_get_drvdata(&idev->dev));
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200678}
679
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200680static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
681 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
682 IPACK1_DEVICE_ID_SBS_OCTAL_232) },
683 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
684 IPACK1_DEVICE_ID_SBS_OCTAL_422) },
685 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
686 IPACK1_DEVICE_ID_SBS_OCTAL_485) },
687 { 0, },
688};
689
690MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
691
Jens Taproggee8011132012-09-04 17:01:17 +0200692static const struct ipack_driver_ops ipoctal_drv_ops = {
Jens Taprogge4aa09d42012-09-04 17:01:19 +0200693 .probe = ipoctal_probe,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200694 .remove = ipoctal_remove,
695};
696
Jens Taproggee8011132012-09-04 17:01:17 +0200697static struct ipack_driver driver = {
698 .ops = &ipoctal_drv_ops,
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200699 .id_table = ipoctal_ids,
Jens Taproggee8011132012-09-04 17:01:17 +0200700};
701
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200702static int __init ipoctal_init(void)
703{
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200704 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200705}
706
707static void __exit ipoctal_exit(void)
708{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200709 ipack_driver_unregister(&driver);
710}
711
712MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
713MODULE_LICENSE("GPL");
714
715module_init(ipoctal_init);
716module_exit(ipoctal_exit);