blob: a33a849765c6399b283865d120b0135eab638d5f [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;
41 atomic_t open;
42 struct tty_port tty_port;
43 union scc2698_channel __iomem *regs;
44 union scc2698_block __iomem *block_regs;
Jens Taproggeef79de02012-09-12 14:55:28 +020045 unsigned int board_id;
46 unsigned char *board_write;
Jens Taprogge4e4732a2012-09-12 14:55:29 +020047 u8 isr_rx_rdy_mask;
48 u8 isr_tx_rdy_mask;
Jens Taprogge70a32812012-09-12 14:55:26 +020049};
50
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020051struct ipoctal {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020052 struct ipack_device *dev;
53 unsigned int board_id;
Jens Taprogge70a32812012-09-12 14:55:26 +020054 struct ipoctal_channel channel[NR_CHANNELS];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020055 unsigned char write;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020056 struct tty_driver *tty_drv;
Jens Taproggefe4a3ed2012-09-27 12:37:36 +020057 u8 __iomem *mem8_space;
Jens Taprogge402228d2012-09-27 12:37:34 +020058 u8 __iomem *int_space;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020059};
60
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020061static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
62{
Jens Taprogge70a32812012-09-12 14:55:26 +020063 struct ipoctal_channel *channel;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020064
Jens Taprogge9c1d7842012-09-12 14:55:42 +020065 channel = dev_get_drvdata(tty->dev);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020066
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{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020073 int res;
Jens Taprogge70a32812012-09-12 14:55:26 +020074 struct ipoctal_channel *channel;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020075
Jens Taprogge9c1d7842012-09-12 14:55:42 +020076 channel = dev_get_drvdata(tty->dev);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020077
Jens Taprogge70a32812012-09-12 14:55:26 +020078 if (atomic_read(&channel->open))
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020079 return -EBUSY;
80
Jens Taproggeef79de02012-09-12 14:55:28 +020081 tty->driver_data = channel;
Samuel Iglesias Gonsálvez1337b072012-07-13 13:33:13 +020082
Jens Taprogge70a32812012-09-12 14:55:26 +020083 res = tty_port_open(&channel->tty_port, tty, file);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020084 if (res)
85 return res;
86
Jens Taprogge70a32812012-09-12 14:55:26 +020087 atomic_inc(&channel->open);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020088 return 0;
89}
90
91static void ipoctal_reset_stats(struct ipoctal_stats *stats)
92{
93 stats->tx = 0;
94 stats->rx = 0;
95 stats->rcv_break = 0;
96 stats->framing_err = 0;
97 stats->overrun_err = 0;
98 stats->parity_err = 0;
99}
100
Jens Taproggeef79de02012-09-12 14:55:28 +0200101static void ipoctal_free_channel(struct ipoctal_channel *channel)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200102{
Jens Taprogge70a32812012-09-12 14:55:26 +0200103 ipoctal_reset_stats(&channel->stats);
104 channel->pointer_read = 0;
105 channel->pointer_write = 0;
106 channel->nb_bytes = 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200107}
108
109static void ipoctal_close(struct tty_struct *tty, struct file *filp)
110{
Jens Taproggeef79de02012-09-12 14:55:28 +0200111 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200112
Jens Taprogge70a32812012-09-12 14:55:26 +0200113 tty_port_close(&channel->tty_port, tty, filp);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200114
Jens Taprogge70a32812012-09-12 14:55:26 +0200115 if (atomic_dec_and_test(&channel->open))
Jens Taproggeef79de02012-09-12 14:55:28 +0200116 ipoctal_free_channel(channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200117}
118
119static int ipoctal_get_icount(struct tty_struct *tty,
120 struct serial_icounter_struct *icount)
121{
Jens Taproggeef79de02012-09-12 14:55:28 +0200122 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200123
124 icount->cts = 0;
125 icount->dsr = 0;
126 icount->rng = 0;
127 icount->dcd = 0;
Jens Taprogge70a32812012-09-12 14:55:26 +0200128 icount->rx = channel->stats.rx;
129 icount->tx = channel->stats.tx;
130 icount->frame = channel->stats.framing_err;
131 icount->parity = channel->stats.parity_err;
132 icount->brk = channel->stats.rcv_break;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200133 return 0;
134}
135
Jens Taprogge87cfb952012-09-12 14:55:30 +0200136static void ipoctal_irq_rx(struct ipoctal_channel *channel,
137 struct tty_struct *tty, u8 sr)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200138{
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200139 unsigned char value;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200140 unsigned char flag = TTY_NORMAL;
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200141 u8 isr;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200142
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200143 do {
144 value = ioread8(&channel->regs->r.rhr);
145 /* Error: count statistics */
146 if (sr & SR_ERROR) {
147 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200148
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200149 if (sr & SR_OVERRUN_ERROR) {
150 channel->stats.overrun_err++;
151 /* Overrun doesn't affect the current character*/
152 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
153 }
154 if (sr & SR_PARITY_ERROR) {
155 channel->stats.parity_err++;
156 flag = TTY_PARITY;
157 }
158 if (sr & SR_FRAMING_ERROR) {
159 channel->stats.framing_err++;
160 flag = TTY_FRAME;
161 }
162 if (sr & SR_RECEIVED_BREAK) {
Samuel Iglesias Gonsalvez45141082012-09-13 12:32:22 +0200163 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200164 channel->stats.rcv_break++;
165 flag = TTY_BREAK;
166 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200167 }
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200168 tty_insert_flip_char(tty, value, flag);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200169
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200170 /* Check if there are more characters in RX FIFO
171 * If there are more, the isr register for this channel
172 * has enabled the RxRDY|FFULL bit.
173 */
174 isr = ioread8(&channel->block_regs->r.isr);
175 sr = ioread8(&channel->regs->r.sr);
176 } while (isr & channel->isr_rx_rdy_mask);
177
178 tty_flip_buffer_push(tty);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200179}
180
181static void ipoctal_irq_tx(struct ipoctal_channel *channel)
182{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200183 unsigned char value;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200184 unsigned int *pointer_write = &channel->pointer_write;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200185
Alberto Garcia69a6b9b2012-12-10 11:49:58 +0100186 if (channel->nb_bytes == 0)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200187 return;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200188
Jens Taprogge87cfb952012-09-12 14:55:30 +0200189 value = channel->tty_port.xmit_buf[*pointer_write];
190 iowrite8(value, &channel->regs->w.thr);
191 channel->stats.tx++;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200192 (*pointer_write)++;
193 *pointer_write = *pointer_write % PAGE_SIZE;
194 channel->nb_bytes--;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200195
Alberto Garciacc83f832012-12-10 11:49:57 +0100196 if (channel->nb_bytes == 0 &&
197 channel->board_id != IPACK1_DEVICE_ID_SBS_OCTAL_485) {
198 *channel->board_write = 1;
199 wake_up_interruptible(&channel->queue);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200200 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200201}
202
203static void ipoctal_irq_channel(struct ipoctal_channel *channel)
204{
205 u8 isr, sr;
206 struct tty_struct *tty;
207
208 /* If there is no client, skip the check */
209 if (!atomic_read(&channel->open))
210 return;
211
212 tty = tty_port_tty_get(&channel->tty_port);
213 if (!tty)
214 return;
215 /* The HW is organized in pair of channels. See which register we need
216 * to read from */
217 isr = ioread8(&channel->block_regs->r.isr);
218 sr = ioread8(&channel->regs->r.sr);
219
220 /* In case of RS-485, change from TX to RX when finishing TX.
221 * Half-duplex. */
222 if ((channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) &&
223 (sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
224 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
225 iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
226 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
227 *channel->board_write = 1;
228 wake_up_interruptible(&channel->queue);
229 }
230
231 /* RX data */
232 if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
233 ipoctal_irq_rx(channel, tty, sr);
234
235 /* TX of each character */
236 if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
237 ipoctal_irq_tx(channel);
238
239 tty_flip_buffer_push(tty);
240 tty_kref_put(tty);
241}
242
Jens Taproggefaa75c42012-09-12 14:55:38 +0200243static irqreturn_t ipoctal_irq_handler(void *arg)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200244{
245 unsigned int i;
246 struct ipoctal *ipoctal = (struct ipoctal *) arg;
247
248 /* Check all channels */
249 for (i = 0; i < NR_CHANNELS; i++)
250 ipoctal_irq_channel(&ipoctal->channel[i]);
251
Jens Taproggeea991142012-09-13 12:32:20 +0200252 /* Clear the IPack device interrupt */
Jens Taprogge402228d2012-09-27 12:37:34 +0200253 readw(ipoctal->int_space + ACK_INT_REQ0);
254 readw(ipoctal->int_space + ACK_INT_REQ1);
Jens Taproggeea991142012-09-13 12:32:20 +0200255
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200256 return IRQ_HANDLED;
257}
258
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200259static const struct tty_port_operations ipoctal_tty_port_ops = {
260 .dtr_rts = NULL,
261 .activate = ipoctal_port_activate,
262};
263
264static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200265 unsigned int slot)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200266{
Jens Taprogge402228d2012-09-27 12:37:34 +0200267 int res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200268 int i;
269 struct tty_driver *tty;
270 char name[20];
Jens Taprogge70a32812012-09-12 14:55:26 +0200271 struct ipoctal_channel *channel;
Jens Taprogge402228d2012-09-27 12:37:34 +0200272 struct ipack_region *region;
273 void __iomem *addr;
Jens Taprogge70a32812012-09-12 14:55:26 +0200274 union scc2698_channel __iomem *chan_regs;
275 union scc2698_block __iomem *block_regs;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200276
Jens Taprogge2ec678d2012-09-27 12:37:33 +0200277 ipoctal->board_id = ipoctal->dev->id_device;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200278
Jens Taprogge402228d2012-09-27 12:37:34 +0200279 region = &ipoctal->dev->region[IPACK_IO_SPACE];
280 addr = devm_ioremap_nocache(&ipoctal->dev->dev,
281 region->start, region->size);
282 if (!addr) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200283 dev_err(&ipoctal->dev->dev,
284 "Unable to map slot [%d:%d] IO space!\n",
285 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200286 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200287 }
Jens Taprogge402228d2012-09-27 12:37:34 +0200288 /* Save the virtual address to access the registers easily */
289 chan_regs =
290 (union scc2698_channel __iomem *) addr;
291 block_regs =
292 (union scc2698_block __iomem *) addr;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200293
Jens Taprogge402228d2012-09-27 12:37:34 +0200294 region = &ipoctal->dev->region[IPACK_INT_SPACE];
295 ipoctal->int_space =
296 devm_ioremap_nocache(&ipoctal->dev->dev,
297 region->start, region->size);
298 if (!ipoctal->int_space) {
Jens Taproggeea991142012-09-13 12:32:20 +0200299 dev_err(&ipoctal->dev->dev,
300 "Unable to map slot [%d:%d] INT space!\n",
301 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200302 return -EADDRNOTAVAIL;
Jens Taproggeea991142012-09-13 12:32:20 +0200303 }
304
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200305 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
306 ipoctal->mem8_space =
Jens Taprogge402228d2012-09-27 12:37:34 +0200307 devm_ioremap_nocache(&ipoctal->dev->dev,
308 region->start, 0x8000);
309 if (!addr) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200310 dev_err(&ipoctal->dev->dev,
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200311 "Unable to map slot [%d:%d] MEM8 space!\n",
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200312 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200313 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200314 }
315
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200316
317 /* Disable RX and TX before touching anything */
318 for (i = 0; i < NR_CHANNELS ; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200319 struct ipoctal_channel *channel = &ipoctal->channel[i];
320 channel->regs = chan_regs + i;
321 channel->block_regs = block_regs + (i >> 1);
Jens Taproggeef79de02012-09-12 14:55:28 +0200322 channel->board_write = &ipoctal->write;
323 channel->board_id = ipoctal->board_id;
Jens Taprogge4e4732a2012-09-12 14:55:29 +0200324 if (i & 1) {
325 channel->isr_tx_rdy_mask = ISR_TxRDY_B;
326 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
327 } else {
328 channel->isr_tx_rdy_mask = ISR_TxRDY_A;
329 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
330 }
Jens Taprogge70a32812012-09-12 14:55:26 +0200331
Jens Taprogge459e6d72012-09-12 14:55:27 +0200332 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
333 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
334 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
335 iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
336 &channel->regs->w.mr); /* mr1 */
337 iowrite8(0, &channel->regs->w.mr); /* mr2 */
338 iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200339 }
340
341 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200342 iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
343 iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
344 &block_regs[i].w.opcr);
345 iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
346 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
347 &block_regs[i].w.imr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200348 }
349
350 /*
351 * IP-OCTAL has different addresses to copy its IRQ vector.
352 * Depending of the carrier these addresses are accesible or not.
353 * More info in the datasheet.
354 */
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200355 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200356 ipoctal_irq_handler, ipoctal);
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200357 /* Dummy write */
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200358 iowrite8(1, ipoctal->mem8_space + 1);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200359
360 /* Register the TTY device */
361
362 /* Each IP-OCTAL channel is a TTY port */
363 tty = alloc_tty_driver(NR_CHANNELS);
364
Jens Taprogge402228d2012-09-27 12:37:34 +0200365 if (!tty)
366 return -ENOMEM;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200367
368 /* Fill struct tty_driver with ipoctal data */
369 tty->owner = THIS_MODULE;
Jens Taprogge3f3a5922012-09-12 14:55:41 +0200370 tty->driver_name = KBUILD_MODNAME;
371 sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200372 tty->name = name;
373 tty->major = 0;
374
375 tty->minor_start = 0;
376 tty->type = TTY_DRIVER_TYPE_SERIAL;
377 tty->subtype = SERIAL_TYPE_NORMAL;
378 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
379 tty->init_termios = tty_std_termios;
380 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
381 tty->init_termios.c_ispeed = 9600;
382 tty->init_termios.c_ospeed = 9600;
383
384 tty_set_operations(tty, &ipoctal_fops);
385 res = tty_register_driver(tty);
386 if (res) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200387 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200388 put_tty_driver(tty);
Jens Taprogge402228d2012-09-27 12:37:34 +0200389 return res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200390 }
391
392 /* Save struct tty_driver for use it when uninstalling the device */
393 ipoctal->tty_drv = tty;
394
395 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200396 struct device *tty_dev;
397
Jens Taprogge70a32812012-09-12 14:55:26 +0200398 channel = &ipoctal->channel[i];
399 tty_port_init(&channel->tty_port);
400 tty_port_alloc_xmit_buf(&channel->tty_port);
401 channel->tty_port.ops = &ipoctal_tty_port_ops;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200402
Jens Taprogge70a32812012-09-12 14:55:26 +0200403 ipoctal_reset_stats(&channel->stats);
404 channel->nb_bytes = 0;
405 init_waitqueue_head(&channel->queue);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200406
Jens Taprogge70a32812012-09-12 14:55:26 +0200407 spin_lock_init(&channel->lock);
408 channel->pointer_read = 0;
409 channel->pointer_write = 0;
Linus Torvalds3498d132012-10-01 12:26:52 -0700410 tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200411 if (IS_ERR(tty_dev)) {
412 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
Jiri Slaby191c5f12012-11-15 09:49:56 +0100413 tty_port_destroy(&channel->tty_port);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200414 continue;
415 }
Jens Taprogge9c1d7842012-09-12 14:55:42 +0200416 dev_set_drvdata(tty_dev, channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200417
418 /*
419 * Enable again the RX. TX will be enabled when
420 * there is something to send
421 */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200422 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200423 }
424
425 return 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200426}
427
Jens Taprogge70a32812012-09-12 14:55:26 +0200428static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200429 const unsigned char *buf,
430 int count)
431{
432 unsigned long flags;
433 int i;
Jens Taprogge70a32812012-09-12 14:55:26 +0200434 unsigned int *pointer_read = &channel->pointer_read;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200435
436 /* Copy the bytes from the user buffer to the internal one */
437 for (i = 0; i < count; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200438 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
439 spin_lock_irqsave(&channel->lock, flags);
440 channel->tty_port.xmit_buf[*pointer_read] = buf[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200441 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
Jens Taprogge70a32812012-09-12 14:55:26 +0200442 channel->nb_bytes++;
443 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200444 } else {
445 break;
446 }
447 }
448 return i;
449}
450
Jens Taprogge699a89f2012-09-12 14:55:31 +0200451static int ipoctal_write_tty(struct tty_struct *tty,
452 const unsigned char *buf, int count)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200453{
Jens Taprogge699a89f2012-09-12 14:55:31 +0200454 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200455 unsigned int char_copied;
Jens Taprogge699a89f2012-09-12 14:55:31 +0200456
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200457 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200458
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200459 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
Jens Taproggeef79de02012-09-12 14:55:28 +0200460 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200461 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
462 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200463 }
464
465 /*
466 * Send a packet and then disable TX to avoid failure after several send
467 * operations
468 */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200469 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
Jens Taproggeef79de02012-09-12 14:55:28 +0200470 wait_event_interruptible(channel->queue, *channel->board_write);
Jens Taprogge459e6d72012-09-12 14:55:27 +0200471 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200472
Jens Taproggeef79de02012-09-12 14:55:28 +0200473 *channel->board_write = 0;
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200474 return char_copied;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200475}
476
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200477static int ipoctal_write_room(struct tty_struct *tty)
478{
Jens Taproggeef79de02012-09-12 14:55:28 +0200479 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200480
Jens Taprogge70a32812012-09-12 14:55:26 +0200481 return PAGE_SIZE - channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200482}
483
484static int ipoctal_chars_in_buffer(struct tty_struct *tty)
485{
Jens Taproggeef79de02012-09-12 14:55:28 +0200486 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200487
Jens Taprogge70a32812012-09-12 14:55:26 +0200488 return channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200489}
490
491static void ipoctal_set_termios(struct tty_struct *tty,
492 struct ktermios *old_termios)
493{
494 unsigned int cflag;
495 unsigned char mr1 = 0;
496 unsigned char mr2 = 0;
497 unsigned char csr = 0;
Jens Taproggeef79de02012-09-12 14:55:28 +0200498 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200499 speed_t baud;
500
Alan Cox857196e2012-07-26 19:00:51 +0100501 cflag = tty->termios.c_cflag;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200502
503 /* Disable and reset everything before change the setup */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200504 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
505 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
506 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
507 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
508 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200509
510 /* Set Bits per chars */
511 switch (cflag & CSIZE) {
512 case CS6:
513 mr1 |= MR1_CHRL_6_BITS;
514 break;
515 case CS7:
516 mr1 |= MR1_CHRL_7_BITS;
517 break;
518 case CS8:
519 default:
520 mr1 |= MR1_CHRL_8_BITS;
521 /* By default, select CS8 */
Alan Cox857196e2012-07-26 19:00:51 +0100522 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200523 break;
524 }
525
526 /* Set Parity */
527 if (cflag & PARENB)
528 if (cflag & PARODD)
529 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
530 else
531 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
532 else
533 mr1 |= MR1_PARITY_OFF;
534
535 /* Mark or space parity is not supported */
Alan Cox857196e2012-07-26 19:00:51 +0100536 tty->termios.c_cflag &= ~CMSPAR;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200537
538 /* Set stop bits */
539 if (cflag & CSTOPB)
540 mr2 |= MR2_STOP_BITS_LENGTH_2;
541 else
542 mr2 |= MR2_STOP_BITS_LENGTH_1;
543
544 /* Set the flow control */
Jens Taproggeef79de02012-09-12 14:55:28 +0200545 switch (channel->board_id) {
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200546 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200547 if (cflag & CRTSCTS) {
548 mr1 |= MR1_RxRTS_CONTROL_ON;
549 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200550 } else {
551 mr1 |= MR1_RxRTS_CONTROL_OFF;
552 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200553 }
554 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200555 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200556 mr1 |= MR1_RxRTS_CONTROL_OFF;
557 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200558 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200559 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200560 mr1 |= MR1_RxRTS_CONTROL_OFF;
561 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200562 break;
563 default:
564 return;
565 break;
566 }
567
568 baud = tty_get_baud_rate(tty);
Alan Cox857196e2012-07-26 19:00:51 +0100569 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200570
571 /* Set baud rate */
Alan Cox857196e2012-07-26 19:00:51 +0100572 switch (baud) {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200573 case 75:
574 csr |= TX_CLK_75 | RX_CLK_75;
575 break;
576 case 110:
577 csr |= TX_CLK_110 | RX_CLK_110;
578 break;
579 case 150:
580 csr |= TX_CLK_150 | RX_CLK_150;
581 break;
582 case 300:
583 csr |= TX_CLK_300 | RX_CLK_300;
584 break;
585 case 600:
586 csr |= TX_CLK_600 | RX_CLK_600;
587 break;
588 case 1200:
589 csr |= TX_CLK_1200 | RX_CLK_1200;
590 break;
591 case 1800:
592 csr |= TX_CLK_1800 | RX_CLK_1800;
593 break;
594 case 2000:
595 csr |= TX_CLK_2000 | RX_CLK_2000;
596 break;
597 case 2400:
598 csr |= TX_CLK_2400 | RX_CLK_2400;
599 break;
600 case 4800:
601 csr |= TX_CLK_4800 | RX_CLK_4800;
602 break;
603 case 9600:
604 csr |= TX_CLK_9600 | RX_CLK_9600;
605 break;
606 case 19200:
607 csr |= TX_CLK_19200 | RX_CLK_19200;
608 break;
609 case 38400:
610 default:
611 csr |= TX_CLK_38400 | RX_CLK_38400;
612 /* In case of default, we establish 38400 bps */
Alan Cox857196e2012-07-26 19:00:51 +0100613 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200614 break;
615 }
616
617 mr1 |= MR1_ERROR_CHAR;
618 mr1 |= MR1_RxINT_RxRDY;
619
620 /* Write the control registers */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200621 iowrite8(mr1, &channel->regs->w.mr);
622 iowrite8(mr2, &channel->regs->w.mr);
623 iowrite8(csr, &channel->regs->w.csr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200624
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200625 /* Enable again the RX */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200626 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200627}
628
629static void ipoctal_hangup(struct tty_struct *tty)
630{
631 unsigned long flags;
Jens Taproggeef79de02012-09-12 14:55:28 +0200632 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200633
Jens Taproggeef79de02012-09-12 14:55:28 +0200634 if (channel == NULL)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200635 return;
636
Jens Taprogge70a32812012-09-12 14:55:26 +0200637 spin_lock_irqsave(&channel->lock, flags);
638 channel->nb_bytes = 0;
639 channel->pointer_read = 0;
640 channel->pointer_write = 0;
641 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200642
Jens Taprogge70a32812012-09-12 14:55:26 +0200643 tty_port_hangup(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200644
Jens Taprogge459e6d72012-09-12 14:55:27 +0200645 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
646 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
647 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
648 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
649 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200650
Jens Taprogge70a32812012-09-12 14:55:26 +0200651 clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
652 wake_up_interruptible(&channel->tty_port.open_wait);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200653}
654
655static const struct tty_operations ipoctal_fops = {
656 .ioctl = NULL,
657 .open = ipoctal_open,
658 .close = ipoctal_close,
659 .write = ipoctal_write_tty,
660 .set_termios = ipoctal_set_termios,
661 .write_room = ipoctal_write_room,
662 .chars_in_buffer = ipoctal_chars_in_buffer,
663 .get_icount = ipoctal_get_icount,
664 .hangup = ipoctal_hangup,
665};
666
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200667static int ipoctal_probe(struct ipack_device *dev)
668{
669 int res;
670 struct ipoctal *ipoctal;
671
672 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
673 if (ipoctal == NULL)
674 return -ENOMEM;
675
676 ipoctal->dev = dev;
Jens Taproggef9e314d2012-09-27 12:37:25 +0200677 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200678 if (res)
679 goto out_uninst;
680
Jens Taprogge1adda492012-09-12 14:55:39 +0200681 dev_set_drvdata(&dev->dev, ipoctal);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200682 return 0;
683
684out_uninst:
685 kfree(ipoctal);
686 return res;
687}
688
689static void __ipoctal_remove(struct ipoctal *ipoctal)
690{
691 int i;
692
Samuel Iglesias Gonsálvez690949e2012-09-11 13:35:08 +0200693 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
694
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200695 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200696 struct ipoctal_channel *channel = &ipoctal->channel[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200697 tty_unregister_device(ipoctal->tty_drv, i);
Jens Taprogge70a32812012-09-12 14:55:26 +0200698 tty_port_free_xmit_buf(&channel->tty_port);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100699 tty_port_destroy(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200700 }
701
702 tty_unregister_driver(ipoctal->tty_drv);
703 put_tty_driver(ipoctal->tty_drv);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200704 kfree(ipoctal);
705}
706
Jens Taprogge1adda492012-09-12 14:55:39 +0200707static void ipoctal_remove(struct ipack_device *idev)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200708{
Jens Taprogge1adda492012-09-12 14:55:39 +0200709 __ipoctal_remove(dev_get_drvdata(&idev->dev));
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200710}
711
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200712static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
713 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
714 IPACK1_DEVICE_ID_SBS_OCTAL_232) },
715 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
716 IPACK1_DEVICE_ID_SBS_OCTAL_422) },
717 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
718 IPACK1_DEVICE_ID_SBS_OCTAL_485) },
719 { 0, },
720};
721
722MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
723
Jens Taproggee8011132012-09-04 17:01:17 +0200724static const struct ipack_driver_ops ipoctal_drv_ops = {
Jens Taprogge4aa09d42012-09-04 17:01:19 +0200725 .probe = ipoctal_probe,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200726 .remove = ipoctal_remove,
727};
728
Jens Taproggee8011132012-09-04 17:01:17 +0200729static struct ipack_driver driver = {
730 .ops = &ipoctal_drv_ops,
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200731 .id_table = ipoctal_ids,
Jens Taproggee8011132012-09-04 17:01:17 +0200732};
733
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200734static int __init ipoctal_init(void)
735{
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200736 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200737}
738
739static void __exit ipoctal_exit(void)
740{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200741 ipack_driver_unregister(&driver);
742}
743
744MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
745MODULE_LICENSE("GPL");
746
747module_init(ipoctal_init);
748module_exit(ipoctal_exit);