blob: 93cbf9dda1fec1bca4cdaa2d6864e421c0d069b6 [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;
Samuel Iglesias Gonsalvezb0d17fb2012-12-10 11:50:07 +010046 unsigned int rx_enable;
Jens Taprogge70a32812012-09-12 14:55:26 +020047};
48
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020049struct ipoctal {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020050 struct ipack_device *dev;
51 unsigned int board_id;
Jens Taprogge70a32812012-09-12 14:55:26 +020052 struct ipoctal_channel channel[NR_CHANNELS];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020053 struct tty_driver *tty_drv;
Jens Taproggefe4a3ed2012-09-27 12:37:36 +020054 u8 __iomem *mem8_space;
Jens Taprogge402228d2012-09-27 12:37:34 +020055 u8 __iomem *int_space;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020056};
57
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020058static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty)
59{
Jens Taprogge70a32812012-09-12 14:55:26 +020060 struct ipoctal_channel *channel;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020061
Jens Taprogge9c1d7842012-09-12 14:55:42 +020062 channel = dev_get_drvdata(tty->dev);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020063
Samuel Iglesias Gonsalveza3882b72012-12-10 11:50:03 +010064 /*
65 * Enable RX. TX will be enabled when
66 * there is something to send
67 */
Jens Taprogge459e6d72012-09-12 14:55:27 +020068 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezb0d17fb2012-12-10 11:50:07 +010069 channel->rx_enable = 1;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020070 return 0;
71}
72
73static int ipoctal_open(struct tty_struct *tty, struct file *file)
74{
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);
Jens Taproggeef79de02012-09-12 14:55:28 +020078 tty->driver_data = channel;
Samuel Iglesias Gonsálvez1337b072012-07-13 13:33:13 +020079
Samuel Iglesias Gonsalvez7e5730d2012-12-10 11:49:59 +010080 return tty_port_open(&channel->tty_port, tty, file);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020081}
82
83static void ipoctal_reset_stats(struct ipoctal_stats *stats)
84{
85 stats->tx = 0;
86 stats->rx = 0;
87 stats->rcv_break = 0;
88 stats->framing_err = 0;
89 stats->overrun_err = 0;
90 stats->parity_err = 0;
91}
92
Jens Taproggeef79de02012-09-12 14:55:28 +020093static void ipoctal_free_channel(struct ipoctal_channel *channel)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020094{
Jens Taprogge70a32812012-09-12 14:55:26 +020095 ipoctal_reset_stats(&channel->stats);
96 channel->pointer_read = 0;
97 channel->pointer_write = 0;
98 channel->nb_bytes = 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +020099}
100
101static void ipoctal_close(struct tty_struct *tty, struct file *filp)
102{
Jens Taproggeef79de02012-09-12 14:55:28 +0200103 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200104
Jens Taprogge70a32812012-09-12 14:55:26 +0200105 tty_port_close(&channel->tty_port, tty, filp);
Samuel Iglesias Gonsalvez7e5730d2012-12-10 11:49:59 +0100106 ipoctal_free_channel(channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200107}
108
109static int ipoctal_get_icount(struct tty_struct *tty,
110 struct serial_icounter_struct *icount)
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
114 icount->cts = 0;
115 icount->dsr = 0;
116 icount->rng = 0;
117 icount->dcd = 0;
Jens Taprogge70a32812012-09-12 14:55:26 +0200118 icount->rx = channel->stats.rx;
119 icount->tx = channel->stats.tx;
120 icount->frame = channel->stats.framing_err;
121 icount->parity = channel->stats.parity_err;
122 icount->brk = channel->stats.rcv_break;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200123 return 0;
124}
125
Jens Taprogge87cfb952012-09-12 14:55:30 +0200126static void ipoctal_irq_rx(struct ipoctal_channel *channel,
127 struct tty_struct *tty, u8 sr)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200128{
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200129 unsigned char value;
Samuel Iglesias Gonsalvezb5071f22012-12-10 11:50:01 +0100130 unsigned char flag;
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200131 u8 isr;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200132
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200133 do {
134 value = ioread8(&channel->regs->r.rhr);
Samuel Iglesias Gonsalvezb5071f22012-12-10 11:50:01 +0100135 flag = TTY_NORMAL;
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200136 /* Error: count statistics */
137 if (sr & SR_ERROR) {
138 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200139
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200140 if (sr & SR_OVERRUN_ERROR) {
141 channel->stats.overrun_err++;
142 /* Overrun doesn't affect the current character*/
143 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
144 }
145 if (sr & SR_PARITY_ERROR) {
146 channel->stats.parity_err++;
147 flag = TTY_PARITY;
148 }
149 if (sr & SR_FRAMING_ERROR) {
150 channel->stats.framing_err++;
151 flag = TTY_FRAME;
152 }
153 if (sr & SR_RECEIVED_BREAK) {
Samuel Iglesias Gonsalvez45141082012-09-13 12:32:22 +0200154 iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200155 channel->stats.rcv_break++;
156 flag = TTY_BREAK;
157 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200158 }
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200159 tty_insert_flip_char(tty, value, flag);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200160
Samuel Iglesias Gonsalvezab0a71f2012-09-12 14:55:43 +0200161 /* Check if there are more characters in RX FIFO
162 * If there are more, the isr register for this channel
163 * has enabled the RxRDY|FFULL bit.
164 */
165 isr = ioread8(&channel->block_regs->r.isr);
166 sr = ioread8(&channel->regs->r.sr);
167 } while (isr & channel->isr_rx_rdy_mask);
168
169 tty_flip_buffer_push(tty);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200170}
171
172static void ipoctal_irq_tx(struct ipoctal_channel *channel)
173{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200174 unsigned char value;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200175 unsigned int *pointer_write = &channel->pointer_write;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200176
Alberto Garcia69a6b9b2012-12-10 11:49:58 +0100177 if (channel->nb_bytes == 0)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200178 return;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200179
Jens Taprogge87cfb952012-09-12 14:55:30 +0200180 value = channel->tty_port.xmit_buf[*pointer_write];
181 iowrite8(value, &channel->regs->w.thr);
182 channel->stats.tx++;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200183 (*pointer_write)++;
184 *pointer_write = *pointer_write % PAGE_SIZE;
185 channel->nb_bytes--;
Jens Taprogge87cfb952012-09-12 14:55:30 +0200186}
187
188static void ipoctal_irq_channel(struct ipoctal_channel *channel)
189{
190 u8 isr, sr;
191 struct tty_struct *tty;
192
Jens Taprogge87cfb952012-09-12 14:55:30 +0200193 tty = tty_port_tty_get(&channel->tty_port);
194 if (!tty)
195 return;
Samuel Iglesias Gonsalveze7e664f2012-12-10 11:50:05 +0100196
197 spin_lock(&channel->lock);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200198 /* The HW is organized in pair of channels. See which register we need
199 * to read from */
200 isr = ioread8(&channel->block_regs->r.isr);
201 sr = ioread8(&channel->regs->r.sr);
202
Samuel Iglesias Gonsalvez9d01b6f2012-12-10 11:50:02 +0100203 if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) {
Jens Taprogge87cfb952012-09-12 14:55:30 +0200204 iowrite8(CR_DISABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvez9d01b6f2012-12-10 11:50:02 +0100205 /* In case of RS-485, change from TX to RX when finishing TX.
206 * Half-duplex. */
207 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
208 iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr);
209 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvez2910fe22013-01-18 08:57:21 +0100210 channel->rx_enable = 1;
Samuel Iglesias Gonsalvez9d01b6f2012-12-10 11:50:02 +0100211 }
Jens Taprogge87cfb952012-09-12 14:55:30 +0200212 }
213
214 /* RX data */
215 if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY))
216 ipoctal_irq_rx(channel, tty, sr);
217
218 /* TX of each character */
219 if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY))
220 ipoctal_irq_tx(channel);
221
Jens Taprogge87cfb952012-09-12 14:55:30 +0200222 tty_kref_put(tty);
Samuel Iglesias Gonsalveze7e664f2012-12-10 11:50:05 +0100223 spin_unlock(&channel->lock);
Jens Taprogge87cfb952012-09-12 14:55:30 +0200224}
225
Jens Taproggefaa75c42012-09-12 14:55:38 +0200226static irqreturn_t ipoctal_irq_handler(void *arg)
Jens Taprogge87cfb952012-09-12 14:55:30 +0200227{
228 unsigned int i;
229 struct ipoctal *ipoctal = (struct ipoctal *) arg;
230
Jens Taproggeea991142012-09-13 12:32:20 +0200231 /* Clear the IPack device interrupt */
Jens Taprogge402228d2012-09-27 12:37:34 +0200232 readw(ipoctal->int_space + ACK_INT_REQ0);
233 readw(ipoctal->int_space + ACK_INT_REQ1);
Jens Taproggeea991142012-09-13 12:32:20 +0200234
Samuel Iglesias Gonsalvez21d27ed2012-12-10 11:50:04 +0100235 /* Check all channels */
236 for (i = 0; i < NR_CHANNELS; i++)
237 ipoctal_irq_channel(&ipoctal->channel[i]);
238
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200239 return IRQ_HANDLED;
240}
241
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200242static const struct tty_port_operations ipoctal_tty_port_ops = {
243 .dtr_rts = NULL,
244 .activate = ipoctal_port_activate,
245};
246
247static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr,
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200248 unsigned int slot)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200249{
Jens Taprogge402228d2012-09-27 12:37:34 +0200250 int res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200251 int i;
252 struct tty_driver *tty;
253 char name[20];
Jens Taprogge70a32812012-09-12 14:55:26 +0200254 struct ipoctal_channel *channel;
Jens Taprogge402228d2012-09-27 12:37:34 +0200255 struct ipack_region *region;
256 void __iomem *addr;
Jens Taprogge70a32812012-09-12 14:55:26 +0200257 union scc2698_channel __iomem *chan_regs;
258 union scc2698_block __iomem *block_regs;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200259
Jens Taprogge2ec678d2012-09-27 12:37:33 +0200260 ipoctal->board_id = ipoctal->dev->id_device;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200261
Jens Taprogge402228d2012-09-27 12:37:34 +0200262 region = &ipoctal->dev->region[IPACK_IO_SPACE];
263 addr = devm_ioremap_nocache(&ipoctal->dev->dev,
264 region->start, region->size);
265 if (!addr) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200266 dev_err(&ipoctal->dev->dev,
267 "Unable to map slot [%d:%d] IO space!\n",
268 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200269 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200270 }
Jens Taprogge402228d2012-09-27 12:37:34 +0200271 /* Save the virtual address to access the registers easily */
272 chan_regs =
273 (union scc2698_channel __iomem *) addr;
274 block_regs =
275 (union scc2698_block __iomem *) addr;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200276
Jens Taprogge402228d2012-09-27 12:37:34 +0200277 region = &ipoctal->dev->region[IPACK_INT_SPACE];
278 ipoctal->int_space =
279 devm_ioremap_nocache(&ipoctal->dev->dev,
280 region->start, region->size);
281 if (!ipoctal->int_space) {
Jens Taproggeea991142012-09-13 12:32:20 +0200282 dev_err(&ipoctal->dev->dev,
283 "Unable to map slot [%d:%d] INT space!\n",
284 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200285 return -EADDRNOTAVAIL;
Jens Taproggeea991142012-09-13 12:32:20 +0200286 }
287
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200288 region = &ipoctal->dev->region[IPACK_MEM8_SPACE];
289 ipoctal->mem8_space =
Jens Taprogge402228d2012-09-27 12:37:34 +0200290 devm_ioremap_nocache(&ipoctal->dev->dev,
291 region->start, 0x8000);
292 if (!addr) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200293 dev_err(&ipoctal->dev->dev,
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200294 "Unable to map slot [%d:%d] MEM8 space!\n",
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200295 bus_nr, slot);
Jens Taprogge402228d2012-09-27 12:37:34 +0200296 return -EADDRNOTAVAIL;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200297 }
298
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200299
300 /* Disable RX and TX before touching anything */
301 for (i = 0; i < NR_CHANNELS ; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200302 struct ipoctal_channel *channel = &ipoctal->channel[i];
303 channel->regs = chan_regs + i;
304 channel->block_regs = block_regs + (i >> 1);
Jens Taproggeef79de02012-09-12 14:55:28 +0200305 channel->board_id = ipoctal->board_id;
Jens Taprogge4e4732a2012-09-12 14:55:29 +0200306 if (i & 1) {
307 channel->isr_tx_rdy_mask = ISR_TxRDY_B;
308 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B;
309 } else {
310 channel->isr_tx_rdy_mask = ISR_TxRDY_A;
311 channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A;
312 }
Jens Taprogge70a32812012-09-12 14:55:26 +0200313
Jens Taprogge459e6d72012-09-12 14:55:27 +0200314 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezb0d17fb2012-12-10 11:50:07 +0100315 channel->rx_enable = 0;
Jens Taprogge459e6d72012-09-12 14:55:27 +0200316 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
317 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
318 iowrite8(MR1_CHRL_8_BITS | MR1_ERROR_CHAR | MR1_RxINT_RxRDY,
319 &channel->regs->w.mr); /* mr1 */
320 iowrite8(0, &channel->regs->w.mr); /* mr2 */
321 iowrite8(TX_CLK_9600 | RX_CLK_9600, &channel->regs->w.csr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200322 }
323
324 for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200325 iowrite8(ACR_BRG_SET2, &block_regs[i].w.acr);
326 iowrite8(OPCR_MPP_OUTPUT | OPCR_MPOa_RTSN | OPCR_MPOb_RTSN,
327 &block_regs[i].w.opcr);
328 iowrite8(IMR_TxRDY_A | IMR_RxRDY_FFULL_A | IMR_DELTA_BREAK_A |
329 IMR_TxRDY_B | IMR_RxRDY_FFULL_B | IMR_DELTA_BREAK_B,
330 &block_regs[i].w.imr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200331 }
332
333 /*
334 * IP-OCTAL has different addresses to copy its IRQ vector.
335 * Depending of the carrier these addresses are accesible or not.
336 * More info in the datasheet.
337 */
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200338 ipoctal->dev->bus->ops->request_irq(ipoctal->dev,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200339 ipoctal_irq_handler, ipoctal);
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200340 /* Dummy write */
Jens Taproggefe4a3ed2012-09-27 12:37:36 +0200341 iowrite8(1, ipoctal->mem8_space + 1);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200342
343 /* Register the TTY device */
344
345 /* Each IP-OCTAL channel is a TTY port */
346 tty = alloc_tty_driver(NR_CHANNELS);
347
Jens Taprogge402228d2012-09-27 12:37:34 +0200348 if (!tty)
349 return -ENOMEM;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200350
351 /* Fill struct tty_driver with ipoctal data */
352 tty->owner = THIS_MODULE;
Jens Taprogge3f3a5922012-09-12 14:55:41 +0200353 tty->driver_name = KBUILD_MODNAME;
354 sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200355 tty->name = name;
356 tty->major = 0;
357
358 tty->minor_start = 0;
359 tty->type = TTY_DRIVER_TYPE_SERIAL;
360 tty->subtype = SERIAL_TYPE_NORMAL;
361 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
362 tty->init_termios = tty_std_termios;
363 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
364 tty->init_termios.c_ispeed = 9600;
365 tty->init_termios.c_ospeed = 9600;
366
367 tty_set_operations(tty, &ipoctal_fops);
368 res = tty_register_driver(tty);
369 if (res) {
Samuel Iglesias Gonsalvez42b38202012-05-25 13:08:13 +0200370 dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n");
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200371 put_tty_driver(tty);
Jens Taprogge402228d2012-09-27 12:37:34 +0200372 return res;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200373 }
374
375 /* Save struct tty_driver for use it when uninstalling the device */
376 ipoctal->tty_drv = tty;
377
378 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200379 struct device *tty_dev;
380
Jens Taprogge70a32812012-09-12 14:55:26 +0200381 channel = &ipoctal->channel[i];
382 tty_port_init(&channel->tty_port);
383 tty_port_alloc_xmit_buf(&channel->tty_port);
384 channel->tty_port.ops = &ipoctal_tty_port_ops;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200385
Jens Taprogge70a32812012-09-12 14:55:26 +0200386 ipoctal_reset_stats(&channel->stats);
387 channel->nb_bytes = 0;
Jens Taprogge70a32812012-09-12 14:55:26 +0200388 spin_lock_init(&channel->lock);
389 channel->pointer_read = 0;
390 channel->pointer_write = 0;
Linus Torvalds3498d132012-10-01 12:26:52 -0700391 tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200392 if (IS_ERR(tty_dev)) {
393 dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n");
Jiri Slaby191c5f12012-11-15 09:49:56 +0100394 tty_port_destroy(&channel->tty_port);
Jens Taprogge2afb41d2012-09-12 14:55:40 +0200395 continue;
396 }
Jens Taprogge9c1d7842012-09-12 14:55:42 +0200397 dev_set_drvdata(tty_dev, channel);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200398 }
399
400 return 0;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200401}
402
Jens Taprogge70a32812012-09-12 14:55:26 +0200403static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200404 const unsigned char *buf,
405 int count)
406{
407 unsigned long flags;
408 int i;
Jens Taprogge70a32812012-09-12 14:55:26 +0200409 unsigned int *pointer_read = &channel->pointer_read;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200410
411 /* Copy the bytes from the user buffer to the internal one */
412 for (i = 0; i < count; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200413 if (i <= (PAGE_SIZE - channel->nb_bytes)) {
414 spin_lock_irqsave(&channel->lock, flags);
415 channel->tty_port.xmit_buf[*pointer_read] = buf[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200416 *pointer_read = (*pointer_read + 1) % PAGE_SIZE;
Jens Taprogge70a32812012-09-12 14:55:26 +0200417 channel->nb_bytes++;
418 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200419 } else {
420 break;
421 }
422 }
423 return i;
424}
425
Jens Taprogge699a89f2012-09-12 14:55:31 +0200426static int ipoctal_write_tty(struct tty_struct *tty,
427 const unsigned char *buf, int count)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200428{
Jens Taprogge699a89f2012-09-12 14:55:31 +0200429 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200430 unsigned int char_copied;
Jens Taprogge699a89f2012-09-12 14:55:31 +0200431
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200432 char_copied = ipoctal_copy_write_buffer(channel, buf, count);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200433
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200434 /* As the IP-OCTAL 485 only supports half duplex, do it manually */
Jens Taproggeef79de02012-09-12 14:55:28 +0200435 if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) {
Jens Taprogge459e6d72012-09-12 14:55:27 +0200436 iowrite8(CR_DISABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezb0d17fb2012-12-10 11:50:07 +0100437 channel->rx_enable = 0;
Jens Taprogge459e6d72012-09-12 14:55:27 +0200438 iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200439 }
440
441 /*
442 * Send a packet and then disable TX to avoid failure after several send
443 * operations
444 */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200445 iowrite8(CR_ENABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezd0460062012-09-13 12:32:23 +0200446 return char_copied;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200447}
448
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200449static int ipoctal_write_room(struct tty_struct *tty)
450{
Jens Taproggeef79de02012-09-12 14:55:28 +0200451 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200452
Jens Taprogge70a32812012-09-12 14:55:26 +0200453 return PAGE_SIZE - channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200454}
455
456static int ipoctal_chars_in_buffer(struct tty_struct *tty)
457{
Jens Taproggeef79de02012-09-12 14:55:28 +0200458 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200459
Jens Taprogge70a32812012-09-12 14:55:26 +0200460 return channel->nb_bytes;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200461}
462
463static void ipoctal_set_termios(struct tty_struct *tty,
464 struct ktermios *old_termios)
465{
466 unsigned int cflag;
467 unsigned char mr1 = 0;
468 unsigned char mr2 = 0;
469 unsigned char csr = 0;
Jens Taproggeef79de02012-09-12 14:55:28 +0200470 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200471 speed_t baud;
472
Alan Cox857196e2012-07-26 19:00:51 +0100473 cflag = tty->termios.c_cflag;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200474
475 /* Disable and reset everything before change the setup */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200476 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
477 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
478 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
479 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
480 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200481
482 /* Set Bits per chars */
483 switch (cflag & CSIZE) {
484 case CS6:
485 mr1 |= MR1_CHRL_6_BITS;
486 break;
487 case CS7:
488 mr1 |= MR1_CHRL_7_BITS;
489 break;
490 case CS8:
491 default:
492 mr1 |= MR1_CHRL_8_BITS;
493 /* By default, select CS8 */
Alan Cox857196e2012-07-26 19:00:51 +0100494 tty->termios.c_cflag = (cflag & ~CSIZE) | CS8;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200495 break;
496 }
497
498 /* Set Parity */
499 if (cflag & PARENB)
500 if (cflag & PARODD)
501 mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD;
502 else
503 mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN;
504 else
505 mr1 |= MR1_PARITY_OFF;
506
507 /* Mark or space parity is not supported */
Alan Cox857196e2012-07-26 19:00:51 +0100508 tty->termios.c_cflag &= ~CMSPAR;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200509
510 /* Set stop bits */
511 if (cflag & CSTOPB)
512 mr2 |= MR2_STOP_BITS_LENGTH_2;
513 else
514 mr2 |= MR2_STOP_BITS_LENGTH_1;
515
516 /* Set the flow control */
Jens Taproggeef79de02012-09-12 14:55:28 +0200517 switch (channel->board_id) {
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200518 case IPACK1_DEVICE_ID_SBS_OCTAL_232:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200519 if (cflag & CRTSCTS) {
520 mr1 |= MR1_RxRTS_CONTROL_ON;
521 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200522 } else {
523 mr1 |= MR1_RxRTS_CONTROL_OFF;
524 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200525 }
526 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200527 case IPACK1_DEVICE_ID_SBS_OCTAL_422:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200528 mr1 |= MR1_RxRTS_CONTROL_OFF;
529 mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200530 break;
Jens Taprogge7db5e3c2012-09-04 17:01:16 +0200531 case IPACK1_DEVICE_ID_SBS_OCTAL_485:
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200532 mr1 |= MR1_RxRTS_CONTROL_OFF;
533 mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200534 break;
535 default:
536 return;
537 break;
538 }
539
540 baud = tty_get_baud_rate(tty);
Alan Cox857196e2012-07-26 19:00:51 +0100541 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200542
543 /* Set baud rate */
Alan Cox857196e2012-07-26 19:00:51 +0100544 switch (baud) {
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200545 case 75:
546 csr |= TX_CLK_75 | RX_CLK_75;
547 break;
548 case 110:
549 csr |= TX_CLK_110 | RX_CLK_110;
550 break;
551 case 150:
552 csr |= TX_CLK_150 | RX_CLK_150;
553 break;
554 case 300:
555 csr |= TX_CLK_300 | RX_CLK_300;
556 break;
557 case 600:
558 csr |= TX_CLK_600 | RX_CLK_600;
559 break;
560 case 1200:
561 csr |= TX_CLK_1200 | RX_CLK_1200;
562 break;
563 case 1800:
564 csr |= TX_CLK_1800 | RX_CLK_1800;
565 break;
566 case 2000:
567 csr |= TX_CLK_2000 | RX_CLK_2000;
568 break;
569 case 2400:
570 csr |= TX_CLK_2400 | RX_CLK_2400;
571 break;
572 case 4800:
573 csr |= TX_CLK_4800 | RX_CLK_4800;
574 break;
575 case 9600:
576 csr |= TX_CLK_9600 | RX_CLK_9600;
577 break;
578 case 19200:
579 csr |= TX_CLK_19200 | RX_CLK_19200;
580 break;
581 case 38400:
582 default:
583 csr |= TX_CLK_38400 | RX_CLK_38400;
584 /* In case of default, we establish 38400 bps */
Alan Cox857196e2012-07-26 19:00:51 +0100585 tty_termios_encode_baud_rate(&tty->termios, 38400, 38400);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200586 break;
587 }
588
589 mr1 |= MR1_ERROR_CHAR;
590 mr1 |= MR1_RxINT_RxRDY;
591
592 /* Write the control registers */
Jens Taprogge459e6d72012-09-12 14:55:27 +0200593 iowrite8(mr1, &channel->regs->w.mr);
594 iowrite8(mr2, &channel->regs->w.mr);
595 iowrite8(csr, &channel->regs->w.csr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200596
Samuel Iglesias Gonsalvezb0d17fb2012-12-10 11:50:07 +0100597 /* Enable again the RX, if it was before */
598 if (channel->rx_enable)
599 iowrite8(CR_ENABLE_RX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200600}
601
602static void ipoctal_hangup(struct tty_struct *tty)
603{
604 unsigned long flags;
Jens Taproggeef79de02012-09-12 14:55:28 +0200605 struct ipoctal_channel *channel = tty->driver_data;
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200606
Jens Taproggeef79de02012-09-12 14:55:28 +0200607 if (channel == NULL)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200608 return;
609
Jens Taprogge70a32812012-09-12 14:55:26 +0200610 spin_lock_irqsave(&channel->lock, flags);
611 channel->nb_bytes = 0;
612 channel->pointer_read = 0;
613 channel->pointer_write = 0;
614 spin_unlock_irqrestore(&channel->lock, flags);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200615
Jens Taprogge70a32812012-09-12 14:55:26 +0200616 tty_port_hangup(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200617
Jens Taprogge459e6d72012-09-12 14:55:27 +0200618 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezb0d17fb2012-12-10 11:50:07 +0100619 channel->rx_enable = 0;
Jens Taprogge459e6d72012-09-12 14:55:27 +0200620 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
621 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
622 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
623 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200624
Jens Taprogge70a32812012-09-12 14:55:26 +0200625 clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
626 wake_up_interruptible(&channel->tty_port.open_wait);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200627}
628
Samuel Iglesias Gonsalveze0f8d322012-12-10 11:50:08 +0100629static void ipoctal_shutdown(struct tty_struct *tty)
630{
631 struct ipoctal_channel *channel = tty->driver_data;
632
633 if (channel == NULL)
634 return;
635
636 iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr);
637 channel->rx_enable = 0;
638 iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr);
639 iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr);
640 iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr);
641 iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr);
642 clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags);
643}
644
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200645static const struct tty_operations ipoctal_fops = {
646 .ioctl = NULL,
647 .open = ipoctal_open,
648 .close = ipoctal_close,
649 .write = ipoctal_write_tty,
650 .set_termios = ipoctal_set_termios,
651 .write_room = ipoctal_write_room,
652 .chars_in_buffer = ipoctal_chars_in_buffer,
653 .get_icount = ipoctal_get_icount,
654 .hangup = ipoctal_hangup,
Samuel Iglesias Gonsalveze0f8d322012-12-10 11:50:08 +0100655 .shutdown = ipoctal_shutdown,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200656};
657
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200658static int ipoctal_probe(struct ipack_device *dev)
659{
660 int res;
661 struct ipoctal *ipoctal;
662
663 ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL);
664 if (ipoctal == NULL)
665 return -ENOMEM;
666
667 ipoctal->dev = dev;
Jens Taproggef9e314d2012-09-27 12:37:25 +0200668 res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200669 if (res)
670 goto out_uninst;
671
Jens Taprogge1adda492012-09-12 14:55:39 +0200672 dev_set_drvdata(&dev->dev, ipoctal);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200673 return 0;
674
675out_uninst:
676 kfree(ipoctal);
677 return res;
678}
679
680static void __ipoctal_remove(struct ipoctal *ipoctal)
681{
682 int i;
683
Samuel Iglesias Gonsálvez690949e2012-09-11 13:35:08 +0200684 ipoctal->dev->bus->ops->free_irq(ipoctal->dev);
685
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200686 for (i = 0; i < NR_CHANNELS; i++) {
Jens Taprogge70a32812012-09-12 14:55:26 +0200687 struct ipoctal_channel *channel = &ipoctal->channel[i];
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200688 tty_unregister_device(ipoctal->tty_drv, i);
Jens Taprogge70a32812012-09-12 14:55:26 +0200689 tty_port_free_xmit_buf(&channel->tty_port);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100690 tty_port_destroy(&channel->tty_port);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200691 }
692
693 tty_unregister_driver(ipoctal->tty_drv);
694 put_tty_driver(ipoctal->tty_drv);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200695 kfree(ipoctal);
696}
697
Jens Taprogge1adda492012-09-12 14:55:39 +0200698static void ipoctal_remove(struct ipack_device *idev)
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200699{
Jens Taprogge1adda492012-09-12 14:55:39 +0200700 __ipoctal_remove(dev_get_drvdata(&idev->dev));
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200701}
702
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200703static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = {
704 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
705 IPACK1_DEVICE_ID_SBS_OCTAL_232) },
706 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
707 IPACK1_DEVICE_ID_SBS_OCTAL_422) },
708 { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS,
709 IPACK1_DEVICE_ID_SBS_OCTAL_485) },
710 { 0, },
711};
712
713MODULE_DEVICE_TABLE(ipack, ipoctal_ids);
714
Jens Taproggee8011132012-09-04 17:01:17 +0200715static const struct ipack_driver_ops ipoctal_drv_ops = {
Jens Taprogge4aa09d42012-09-04 17:01:19 +0200716 .probe = ipoctal_probe,
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200717 .remove = ipoctal_remove,
718};
719
Jens Taproggee8011132012-09-04 17:01:17 +0200720static struct ipack_driver driver = {
721 .ops = &ipoctal_drv_ops,
Jens Taproggefdfc8cf2012-09-04 17:01:18 +0200722 .id_table = ipoctal_ids,
Jens Taproggee8011132012-09-04 17:01:17 +0200723};
724
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200725static int __init ipoctal_init(void)
726{
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200727 return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME);
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200728}
729
730static void __exit ipoctal_exit(void)
731{
Samuel Iglesias Gonsalvezba4dc612012-05-09 15:27:21 +0200732 ipack_driver_unregister(&driver);
733}
734
735MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver");
736MODULE_LICENSE("GPL");
737
738module_init(ipoctal_init);
739module_exit(ipoctal_exit);