blob: 7f568e268a1eef50bda0d1c6de6479ca203ae514 [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>
26#include "../ipack_ids.h"
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020027#include "ipoctal.h"
28#include "scc2698.h"
29
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020030#define IP_OCTAL_ID_SPACE_VECTOR 0x41
31#define IP_OCTAL_NB_BLOCKS 4
32
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020033static const struct tty_operations ipoctal_fops;
34
Jens Taprogge70a32812012-09-12 14:55:26 +020035struct ipoctal_channel {
36 struct ipoctal_stats stats;
37 unsigned int nb_bytes;
Jens Taprogge70a32812012-09-12 14:55:26 +020038 wait_queue_head_t queue;
39 spinlock_t lock;
40 unsigned int pointer_read;
41 unsigned int pointer_write;
42 atomic_t open;
43 struct tty_port tty_port;
44 union scc2698_channel __iomem *regs;
45 union scc2698_block __iomem *block_regs;
Jens Taproggeef79de02012-09-12 14:55:28 +020046 unsigned int board_id;
47 unsigned char *board_write;
Jens Taprogge4e4732a2012-09-12 14:55:29 +020048 u8 isr_rx_rdy_mask;
49 u8 isr_tx_rdy_mask;
Jens Taprogge70a32812012-09-12 14:55:26 +020050};
51
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020052struct ipoctal {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020053 struct ipack_device *dev;
54 unsigned int board_id;
Jens Taprogge70a32812012-09-12 14:55:26 +020055 struct ipoctal_channel channel[NR_CHANNELS];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020056 unsigned char write;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020057 struct tty_driver *tty_drv;
Jens Taproggefe4a3ed2012-09-27 12:37:36 +020058 u8 __iomem *mem8_space;
Jens Taprogge402228d2012-09-27 12:37:34 +020059 u8 __iomem *int_space;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020060};
61
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020062static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
63{
Jens Taprogge70a32812012-09-12 14:55:26 +020064 struct ipoctal_channel *channel;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020065
Jens Taprogge9c1d7842012-09-12 14:55:42 +020066 channel = dev_get_drvdata(tty->dev);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020067
Jens Taprogge459e6d72012-09-12 14:55:27 +020068 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020069 return 0;
70}
71
72static int ipoctal_open(struct tty_struct *tty, struct file *file)
73{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020074 int res;
Jens Taprogge70a32812012-09-12 14:55:26 +020075 struct ipoctal_channel *channel;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020076
Jens Taprogge9c1d7842012-09-12 14:55:42 +020077 channel = dev_get_drvdata(tty->dev);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020078
Jens Taprogge70a32812012-09-12 14:55:26 +020079 if (atomic_read(&channel->open))
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020080 return -EBUSY;
81
Jens Taproggeef79de02012-09-12 14:55:28 +020082 tty->driver_data = channel;
Samuel Iglesias Gonsálvez1337b072012-07-13 13:33:13 +020083
Jens Taprogge70a32812012-09-12 14:55:26 +020084 res = tty_port_open(&channel->tty_port, tty, file);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020085 if (res)
86 return res;
87
Jens Taprogge70a32812012-09-12 14:55:26 +020088 atomic_inc(&channel->open);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020089 return 0;
90}
91
92static void ipoctal_reset_stats(struct ipoctal_stats *stats)
93{
94 stats->tx = 0;
95 stats->rx = 0;
96 stats->rcv_break = 0;
97 stats->framing_err = 0;
98 stats->overrun_err = 0;
99 stats->parity_err = 0;
100}
101
Jens Taproggeef79de02012-09-12 14:55:28 +0200102static void ipoctal_free_channel(struct ipoctal_channel *channel)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200103{
Jens Taprogge70a32812012-09-12 14:55:26 +0200104 ipoctal_reset_stats(&channel->stats);
105 channel->pointer_read = 0;
106 channel->pointer_write = 0;
107 channel->nb_bytes = 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200108}
109
110static void ipoctal_close(struct tty_struct *tty, struct file *filp)
111{
Jens Taproggeef79de02012-09-12 14:55:28 +0200112 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200113
Jens Taprogge70a32812012-09-12 14:55:26 +0200114 tty_port_close(&channel->tty_port, tty, filp);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200115
Jens Taprogge70a32812012-09-12 14:55:26 +0200116 if (atomic_dec_and_test(&channel->open))
Jens Taproggeef79de02012-09-12 14:55:28 +0200117 ipoctal_free_channel(channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200118}
119
120static int ipoctal_get_icount(struct tty_struct *tty,
121 struct serial_icounter_struct *icount)
122{
Jens Taproggeef79de02012-09-12 14:55:28 +0200123 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200124
125 icount->cts = 0;
126 icount->dsr = 0;
127 icount->rng = 0;
128 icount->dcd = 0;
Jens Taprogge70a32812012-09-12 14:55:26 +0200129 icount->rx = channel->stats.rx;
130 icount->tx = channel->stats.tx;
131 icount->frame = channel->stats.framing_err;
132 icount->parity = channel->stats.parity_err;
133 icount->brk = channel->stats.rcv_break;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200134 return 0;
135}
136
Jens Taprogge87cfb952012-09-12 14:55:30 +0200137static void ipoctal_irq_rx(struct ipoctal_channel *channel,
138 struct tty_struct *tty, u8 sr)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200139{
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200140 unsigned char value;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200141 unsigned char flag = TTY_NORMAL;
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200142 u8 isr;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200143
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200144 do {
145 value = ioread8(&channel->regs->r.rhr);
146 /* Error: count statistics */
147 if (sr & SR_ERROR) {
148 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200149
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200150 if (sr & SR_OVERRUN_ERROR) {
151 channel->stats.overrun_err++;
152 /* Overrun doesn't affect the current character*/
153 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
154 }
155 if (sr & SR_PARITY_ERROR) {
156 channel->stats.parity_err++;
157 flag = TTY_PARITY;
158 }
159 if (sr & SR_FRAMING_ERROR) {
160 channel->stats.framing_err++;
161 flag = TTY_FRAME;
162 }
163 if (sr & SR_RECEIVED_BREAK) {
Samuel Iglesias Gonsalvez45141082012-09-13 12:32:22 +0200164 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200165 channel->stats.rcv_break++;
166 flag = TTY_BREAK;
167 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200168 }
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200169 tty_insert_flip_char(tty, value, flag);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200170
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200171 /* Check if there are more characters in RX FIFO
172 * If there are more, the isr register for this channel
173 * has enabled the RxRDY|FFULL bit.
174 */
175 isr = ioread8(&channel->block_regs->r.isr);
176 sr = ioread8(&channel->regs->r.sr);
177 } while (isr & channel->isr_rx_rdy_mask);
178
179 tty_flip_buffer_push(tty);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200180}
181
182static void ipoctal_irq_tx(struct ipoctal_channel *channel)
183{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200184 unsigned char value;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200185 unsigned int *pointer_write = &channel->pointer_write;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200186
Jens Taprogge87cfb952012-09-12 14:55:30 +0200187 if (channel->nb_bytes <= 0) {
188 channel->nb_bytes = 0;
189 return;
190 }
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200191
Jens Taprogge87cfb952012-09-12 14:55:30 +0200192 value = channel->tty_port.xmit_buf[*pointer_write];
193 iowrite8(value, &channel->regs->w.thr);
194 channel->stats.tx++;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200195 (*pointer_write)++;
196 *pointer_write = *pointer_write % PAGE_SIZE;
197 channel->nb_bytes--;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200198
Jens Taprogge87cfb952012-09-12 14:55:30 +0200199 if ((channel->nb_bytes == 0) &&
200 (waitqueue_active(&channel->queue))) {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200201
Jens Taprogge87cfb952012-09-12 14:55:30 +0200202 if (channel->board_id != IPACK1_DEVICE_ID_SBS_OCTAL_485) {
203 *channel->board_write = 1;
Jens Taprogge70a32812012-09-12 14:55:26 +0200204 wake_up_interruptible(&channel->queue);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200205 }
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200206 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200207}
208
209static void ipoctal_irq_channel(struct ipoctal_channel *channel)
210{
211 u8 isr, sr;
212 struct tty_struct *tty;
213
214 /* If there is no client, skip the check */
215 if (!atomic_read(&channel->open))
216 return;
217
218 tty = tty_port_tty_get(&channel->tty_port);
219 if (!tty)
220 return;
221 /* The HW is organized in pair of channels. See which register we need
222 * to read from */
223 isr = ioread8(&channel->block_regs->r.isr);
224 sr = ioread8(&channel->regs->r.sr);
225
226 /* In case of RS-485, change from TX to RX when finishing TX.
227 * Half-duplex. */
228 if ((channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) &&
229 (sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
230 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
231 iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
232 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
233 *channel->board_write = 1;
234 wake_up_interruptible(&channel->queue);
235 }
236
237 /* RX data */
238 if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
239 ipoctal_irq_rx(channel, tty, sr);
240
241 /* TX of each character */
242 if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
243 ipoctal_irq_tx(channel);
244
245 tty_flip_buffer_push(tty);
246 tty_kref_put(tty);
247}
248
Jens Taproggefaa75c42012-09-12 14:55:38 +0200249static irqreturn_t ipoctal_irq_handler(void *arg)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200250{
251 unsigned int i;
252 struct ipoctal *ipoctal = (struct ipoctal *) arg;
253
254 /* Check all channels */
255 for (i = 0; i < NR_CHANNELS; i++)
256 ipoctal_irq_channel(&ipoctal->channel[i]);
257
Jens Taproggeea991142012-09-13 12:32:20 +0200258 /* Clear the IPack device interrupt */
Jens Taprogge402228d2012-09-27 12:37:34 +0200259 readw(ipoctal->int_space + ACK_INT_REQ0);
260 readw(ipoctal->int_space + ACK_INT_REQ1);
Jens Taproggeea991142012-09-13 12:32:20 +0200261
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200262 return IRQ_HANDLED;
263}
264
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200265static const struct tty_port_operations ipoctal_tty_port_ops = {
266 .dtr_rts = NULL,
267 .activate = ipoctal_port_activate,
268};
269
270static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200271 unsigned int slot)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200272{
Jens Taprogge402228d2012-09-27 12:37:34 +0200273 int res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200274 int i;
275 struct tty_driver *tty;
276 char name[20];
Jens Taprogge70a32812012-09-12 14:55:26 +0200277 struct ipoctal_channel *channel;
Jens Taprogge402228d2012-09-27 12:37:34 +0200278 struct ipack_region *region;
279 void __iomem *addr;
Jens Taprogge70a32812012-09-12 14:55:26 +0200280 union scc2698_channel __iomem *chan_regs;
281 union scc2698_block __iomem *block_regs;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200282
Jens Taprogge2ec678d2012-09-27 12:37:33 +0200283 ipoctal->board_id = ipoctal->dev->id_device;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200284
Jens Taprogge402228d2012-09-27 12:37:34 +0200285 region = &ipoctal->dev->region[IPACK_IO_SPACE];
286 addr = devm_ioremap_nocache(&ipoctal->dev->dev,
287 region->start, region->size);
288 if (!addr) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200289 dev_err(&ipoctal->dev->dev,
290 "Unable to map slot [%d:%d] IO space!\n",
291 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200292 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200293 }
Jens Taprogge402228d2012-09-27 12:37:34 +0200294 /* Save the virtual address to access the registers easily */
295 chan_regs =
296 (union scc2698_channel __iomem *) addr;
297 block_regs =
298 (union scc2698_block __iomem *) addr;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200299
Jens Taprogge402228d2012-09-27 12:37:34 +0200300 region = &ipoctal->dev->region[IPACK_INT_SPACE];
301 ipoctal->int_space =
302 devm_ioremap_nocache(&ipoctal->dev->dev,
303 region->start, region->size);
304 if (!ipoctal->int_space) {
Jens Taproggeea991142012-09-13 12:32:20 +0200305 dev_err(&ipoctal->dev->dev,
306 "Unable to map slot [%d:%d] INT space!\n",
307 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200308 return -EADDRNOTAVAIL;
Jens Taproggeea991142012-09-13 12:32:20 +0200309 }
310
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200311 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
312 ipoctal->mem8_space =
Jens Taprogge402228d2012-09-27 12:37:34 +0200313 devm_ioremap_nocache(&ipoctal->dev->dev,
314 region->start, 0x8000);
315 if (!addr) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200316 dev_err(&ipoctal->dev->dev,
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200317 "Unable to map slot [%d:%d] MEM8 space!\n",
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200318 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200319 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200320 }
321
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200322
323 /* Disable RX and TX before touching anything */
324 for (i = 0; i < NR_CHANNELS ; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200325 struct ipoctal_channel *channel = &ipoctal->channel[i];
326 channel->regs = chan_regs + i;
327 channel->block_regs = block_regs + (i >> 1);
Jens Taproggeef79de02012-09-12 14:55:28 +0200328 channel->board_write = &ipoctal->write;
329 channel->board_id = ipoctal->board_id;
Jens Taprogge4e4732a2012-09-12 14:55:29 +0200330 if (i & 1) {
331 channel->isr_tx_rdy_mask = ISR_TxRDY_B;
332 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
333 } else {
334 channel->isr_tx_rdy_mask = ISR_TxRDY_A;
335 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
336 }
Jens Taprogge70a32812012-09-12 14:55:26 +0200337
Jens Taprogge459e6d72012-09-12 14:55:27 +0200338 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
339 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
340 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
341 iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
342 &channel->regs->w.mr); /* mr1 */
343 iowrite8(0, &channel->regs->w.mr); /* mr2 */
344 iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200345 }
346
347 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200348 iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
349 iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
350 &block_regs[i].w.opcr);
351 iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
352 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
353 &block_regs[i].w.imr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200354 }
355
356 /*
357 * IP-OCTAL has different addresses to copy its IRQ vector.
358 * Depending of the carrier these addresses are accesible or not.
359 * More info in the datasheet.
360 */
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200361 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200362 ipoctal_irq_handler, ipoctal);
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200363 /* Dummy write */
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200364 iowrite8(1, ipoctal->mem8_space + 1);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200365
366 /* Register the TTY device */
367
368 /* Each IP-OCTAL channel is a TTY port */
369 tty = alloc_tty_driver(NR_CHANNELS);
370
Jens Taprogge402228d2012-09-27 12:37:34 +0200371 if (!tty)
372 return -ENOMEM;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200373
374 /* Fill struct tty_driver with ipoctal data */
375 tty->owner = THIS_MODULE;
Jens Taprogge3f3a5922012-09-12 14:55:41 +0200376 tty->driver_name = KBUILD_MODNAME;
377 sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200378 tty->name = name;
379 tty->major = 0;
380
381 tty->minor_start = 0;
382 tty->type = TTY_DRIVER_TYPE_SERIAL;
383 tty->subtype = SERIAL_TYPE_NORMAL;
384 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
385 tty->init_termios = tty_std_termios;
386 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
387 tty->init_termios.c_ispeed = 9600;
388 tty->init_termios.c_ospeed = 9600;
389
390 tty_set_operations(tty, &ipoctal_fops);
391 res = tty_register_driver(tty);
392 if (res) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200393 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200394 put_tty_driver(tty);
Jens Taprogge402228d2012-09-27 12:37:34 +0200395 return res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200396 }
397
398 /* Save struct tty_driver for use it when uninstalling the device */
399 ipoctal->tty_drv = tty;
400
401 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200402 struct device *tty_dev;
403
Jens Taprogge70a32812012-09-12 14:55:26 +0200404 channel = &ipoctal->channel[i];
405 tty_port_init(&channel->tty_port);
406 tty_port_alloc_xmit_buf(&channel->tty_port);
407 channel->tty_port.ops = &ipoctal_tty_port_ops;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200408
Jens Taprogge70a32812012-09-12 14:55:26 +0200409 ipoctal_reset_stats(&channel->stats);
410 channel->nb_bytes = 0;
411 init_waitqueue_head(&channel->queue);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200412
Jens Taprogge70a32812012-09-12 14:55:26 +0200413 spin_lock_init(&channel->lock);
414 channel->pointer_read = 0;
415 channel->pointer_write = 0;
Linus Torvalds3498d132012-10-01 12:26:52 -0700416 tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200417 if (IS_ERR(tty_dev)) {
418 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
419 continue;
420 }
Jens Taprogge9c1d7842012-09-12 14:55:42 +0200421 dev_set_drvdata(tty_dev, channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200422
423 /*
424 * Enable again the RX. TX will be enabled when
425 * there is something to send
426 */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200427 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200428 }
429
430 return 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200431}
432
Jens Taprogge70a32812012-09-12 14:55:26 +0200433static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200434 const unsigned char *buf,
435 int count)
436{
437 unsigned long flags;
438 int i;
Jens Taprogge70a32812012-09-12 14:55:26 +0200439 unsigned int *pointer_read = &channel->pointer_read;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200440
441 /* Copy the bytes from the user buffer to the internal one */
442 for (i = 0; i < count; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200443 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
444 spin_lock_irqsave(&channel->lock, flags);
445 channel->tty_port.xmit_buf[*pointer_read] = buf[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200446 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
Jens Taprogge70a32812012-09-12 14:55:26 +0200447 channel->nb_bytes++;
448 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200449 } else {
450 break;
451 }
452 }
453 return i;
454}
455
Jens Taprogge699a89f2012-09-12 14:55:31 +0200456static int ipoctal_write_tty(struct tty_struct *tty,
457 const unsigned char *buf, int count)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200458{
Jens Taprogge699a89f2012-09-12 14:55:31 +0200459 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200460 unsigned int char_copied;
Jens Taprogge699a89f2012-09-12 14:55:31 +0200461
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200462 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200463
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200464 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
Jens Taproggeef79de02012-09-12 14:55:28 +0200465 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200466 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
467 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200468 }
469
470 /*
471 * Send a packet and then disable TX to avoid failure after several send
472 * operations
473 */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200474 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
Jens Taproggeef79de02012-09-12 14:55:28 +0200475 wait_event_interruptible(channel->queue, *channel->board_write);
Jens Taprogge459e6d72012-09-12 14:55:27 +0200476 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200477
Jens Taproggeef79de02012-09-12 14:55:28 +0200478 *channel->board_write = 0;
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200479 return char_copied;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200480}
481
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200482static int ipoctal_write_room(struct tty_struct *tty)
483{
Jens Taproggeef79de02012-09-12 14:55:28 +0200484 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200485
Jens Taprogge70a32812012-09-12 14:55:26 +0200486 return PAGE_SIZE - channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200487}
488
489static int ipoctal_chars_in_buffer(struct tty_struct *tty)
490{
Jens Taproggeef79de02012-09-12 14:55:28 +0200491 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200492
Jens Taprogge70a32812012-09-12 14:55:26 +0200493 return channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200494}
495
496static void ipoctal_set_termios(struct tty_struct *tty,
497 struct ktermios *old_termios)
498{
499 unsigned int cflag;
500 unsigned char mr1 = 0;
501 unsigned char mr2 = 0;
502 unsigned char csr = 0;
Jens Taproggeef79de02012-09-12 14:55:28 +0200503 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200504 speed_t baud;
505
Alan Cox857196e2012-07-26 19:00:51 +0100506 cflag = tty->termios.c_cflag;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200507
508 /* Disable and reset everything before change the setup */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200509 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
510 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
511 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
512 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
513 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200514
515 /* Set Bits per chars */
516 switch (cflag & CSIZE) {
517 case CS6:
518 mr1 |= MR1_CHRL_6_BITS;
519 break;
520 case CS7:
521 mr1 |= MR1_CHRL_7_BITS;
522 break;
523 case CS8:
524 default:
525 mr1 |= MR1_CHRL_8_BITS;
526 /* By default, select CS8 */
Alan Cox857196e2012-07-26 19:00:51 +0100527 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200528 break;
529 }
530
531 /* Set Parity */
532 if (cflag & PARENB)
533 if (cflag & PARODD)
534 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
535 else
536 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
537 else
538 mr1 |= MR1_PARITY_OFF;
539
540 /* Mark or space parity is not supported */
Alan Cox857196e2012-07-26 19:00:51 +0100541 tty->termios.c_cflag &= ~CMSPAR;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200542
543 /* Set stop bits */
544 if (cflag & CSTOPB)
545 mr2 |= MR2_STOP_BITS_LENGTH_2;
546 else
547 mr2 |= MR2_STOP_BITS_LENGTH_1;
548
549 /* Set the flow control */
Jens Taproggeef79de02012-09-12 14:55:28 +0200550 switch (channel->board_id) {
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200551 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200552 if (cflag & CRTSCTS) {
553 mr1 |= MR1_RxRTS_CONTROL_ON;
554 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200555 } else {
556 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 }
559 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200560 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200561 mr1 |= MR1_RxRTS_CONTROL_OFF;
562 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200563 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200564 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200565 mr1 |= MR1_RxRTS_CONTROL_OFF;
566 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200567 break;
568 default:
569 return;
570 break;
571 }
572
573 baud = tty_get_baud_rate(tty);
Alan Cox857196e2012-07-26 19:00:51 +0100574 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200575
576 /* Set baud rate */
Alan Cox857196e2012-07-26 19:00:51 +0100577 switch (baud) {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200578 case 75:
579 csr |= TX_CLK_75 | RX_CLK_75;
580 break;
581 case 110:
582 csr |= TX_CLK_110 | RX_CLK_110;
583 break;
584 case 150:
585 csr |= TX_CLK_150 | RX_CLK_150;
586 break;
587 case 300:
588 csr |= TX_CLK_300 | RX_CLK_300;
589 break;
590 case 600:
591 csr |= TX_CLK_600 | RX_CLK_600;
592 break;
593 case 1200:
594 csr |= TX_CLK_1200 | RX_CLK_1200;
595 break;
596 case 1800:
597 csr |= TX_CLK_1800 | RX_CLK_1800;
598 break;
599 case 2000:
600 csr |= TX_CLK_2000 | RX_CLK_2000;
601 break;
602 case 2400:
603 csr |= TX_CLK_2400 | RX_CLK_2400;
604 break;
605 case 4800:
606 csr |= TX_CLK_4800 | RX_CLK_4800;
607 break;
608 case 9600:
609 csr |= TX_CLK_9600 | RX_CLK_9600;
610 break;
611 case 19200:
612 csr |= TX_CLK_19200 | RX_CLK_19200;
613 break;
614 case 38400:
615 default:
616 csr |= TX_CLK_38400 | RX_CLK_38400;
617 /* In case of default, we establish 38400 bps */
Alan Cox857196e2012-07-26 19:00:51 +0100618 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200619 break;
620 }
621
622 mr1 |= MR1_ERROR_CHAR;
623 mr1 |= MR1_RxINT_RxRDY;
624
625 /* Write the control registers */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200626 iowrite8(mr1, &channel->regs->w.mr);
627 iowrite8(mr2, &channel->regs->w.mr);
628 iowrite8(csr, &channel->regs->w.csr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200629
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200630 /* Enable again the RX */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200631 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200632}
633
634static void ipoctal_hangup(struct tty_struct *tty)
635{
636 unsigned long flags;
Jens Taproggeef79de02012-09-12 14:55:28 +0200637 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200638
Jens Taproggeef79de02012-09-12 14:55:28 +0200639 if (channel == NULL)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200640 return;
641
Jens Taprogge70a32812012-09-12 14:55:26 +0200642 spin_lock_irqsave(&channel->lock, flags);
643 channel->nb_bytes = 0;
644 channel->pointer_read = 0;
645 channel->pointer_write = 0;
646 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200647
Jens Taprogge70a32812012-09-12 14:55:26 +0200648 tty_port_hangup(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200649
Jens Taprogge459e6d72012-09-12 14:55:27 +0200650 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
651 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
652 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
653 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
654 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200655
Jens Taprogge70a32812012-09-12 14:55:26 +0200656 clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
657 wake_up_interruptible(&channel->tty_port.open_wait);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200658}
659
660static const struct tty_operations ipoctal_fops = {
661 .ioctl = NULL,
662 .open = ipoctal_open,
663 .close = ipoctal_close,
664 .write = ipoctal_write_tty,
665 .set_termios = ipoctal_set_termios,
666 .write_room = ipoctal_write_room,
667 .chars_in_buffer = ipoctal_chars_in_buffer,
668 .get_icount = ipoctal_get_icount,
669 .hangup = ipoctal_hangup,
670};
671
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200672static int ipoctal_probe(struct ipack_device *dev)
673{
674 int res;
675 struct ipoctal *ipoctal;
676
677 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
678 if (ipoctal == NULL)
679 return -ENOMEM;
680
681 ipoctal->dev = dev;
Jens Taproggef9e314d2012-09-27 12:37:25 +0200682 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200683 if (res)
684 goto out_uninst;
685
Jens Taprogge1adda492012-09-12 14:55:39 +0200686 dev_set_drvdata(&dev->dev, ipoctal);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200687 return 0;
688
689out_uninst:
690 kfree(ipoctal);
691 return res;
692}
693
694static void __ipoctal_remove(struct ipoctal *ipoctal)
695{
696 int i;
697
Samuel Iglesias Gonsálvez690949e2012-09-11 13:35:08 +0200698 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
699
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200700 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200701 struct ipoctal_channel *channel = &ipoctal->channel[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200702 tty_unregister_device(ipoctal->tty_drv, i);
Jens Taprogge70a32812012-09-12 14:55:26 +0200703 tty_port_free_xmit_buf(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200704 }
705
706 tty_unregister_driver(ipoctal->tty_drv);
707 put_tty_driver(ipoctal->tty_drv);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200708 kfree(ipoctal);
709}
710
Jens Taprogge1adda492012-09-12 14:55:39 +0200711static void ipoctal_remove(struct ipack_device *idev)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200712{
Jens Taprogge1adda492012-09-12 14:55:39 +0200713 __ipoctal_remove(dev_get_drvdata(&idev->dev));
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200714}
715
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200716static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
717 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
718 IPACK1_DEVICE_ID_SBS_OCTAL_232) },
719 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
720 IPACK1_DEVICE_ID_SBS_OCTAL_422) },
721 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
722 IPACK1_DEVICE_ID_SBS_OCTAL_485) },
723 { 0, },
724};
725
726MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
727
Jens Taproggee8011132012-09-04 17:01:17 +0200728static const struct ipack_driver_ops ipoctal_drv_ops = {
Jens Taprogge4aa09d42012-09-04 17:01:19 +0200729 .probe = ipoctal_probe,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200730 .remove = ipoctal_remove,
731};
732
Jens Taproggee8011132012-09-04 17:01:17 +0200733static struct ipack_driver driver = {
734 .ops = &ipoctal_drv_ops,
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200735 .id_table = ipoctal_ids,
Jens Taproggee8011132012-09-04 17:01:17 +0200736};
737
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200738static int __init ipoctal_init(void)
739{
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200740 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200741}
742
743static void __exit ipoctal_exit(void)
744{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200745 ipack_driver_unregister(&driver);
746}
747
748MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
749MODULE_LICENSE("GPL");
750
751module_init(ipoctal_init);
752module_exit(ipoctal_exit);