Commit | Line | Data |
---|---|---|
ba4dc61f SIG |
1 | /** |
2 | * ipoctal.c | |
3 | * | |
4 | * driver for the GE IP-OCTAL boards | |
76859725 SIG |
5 | * |
6 | * Copyright (C) 2009-2012 CERN (www.cern.ch) | |
7 | * Author: Nicolas Serafini, EIC2 SA | |
8 | * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com> | |
ba4dc61f SIG |
9 | * |
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 | |
32254363 | 12 | * Software Foundation; version 2 of the License. |
ba4dc61f SIG |
13 | */ |
14 | ||
ba4dc61f SIG |
15 | #include <linux/device.h> |
16 | #include <linux/module.h> | |
ba4dc61f | 17 | #include <linux/interrupt.h> |
ba4dc61f SIG |
18 | #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> | |
64802dc8 | 23 | #include <linux/io.h> |
7dbce021 | 24 | #include <linux/ipack.h> |
ba4dc61f SIG |
25 | #include "ipoctal.h" |
26 | #include "scc2698.h" | |
27 | ||
ba4dc61f SIG |
28 | #define IP_OCTAL_ID_SPACE_VECTOR 0x41 |
29 | #define IP_OCTAL_NB_BLOCKS 4 | |
30 | ||
ba4dc61f SIG |
31 | static const struct tty_operations ipoctal_fops; |
32 | ||
70a32811 JT |
33 | struct ipoctal_channel { |
34 | struct ipoctal_stats stats; | |
35 | unsigned int nb_bytes; | |
70a32811 JT |
36 | wait_queue_head_t queue; |
37 | spinlock_t lock; | |
38 | unsigned int pointer_read; | |
39 | unsigned int pointer_write; | |
70a32811 JT |
40 | struct tty_port tty_port; |
41 | union scc2698_channel __iomem *regs; | |
42 | union scc2698_block __iomem *block_regs; | |
ef79de03 | 43 | unsigned int board_id; |
4e4732ac JT |
44 | u8 isr_rx_rdy_mask; |
45 | u8 isr_tx_rdy_mask; | |
b0d17fbd | 46 | unsigned int rx_enable; |
70a32811 JT |
47 | }; |
48 | ||
ba4dc61f | 49 | struct ipoctal { |
ba4dc61f SIG |
50 | struct ipack_device *dev; |
51 | unsigned int board_id; | |
70a32811 | 52 | struct ipoctal_channel channel[NR_CHANNELS]; |
ba4dc61f | 53 | struct tty_driver *tty_drv; |
fe4a3ed0 | 54 | u8 __iomem *mem8_space; |
402228db | 55 | u8 __iomem *int_space; |
ba4dc61f SIG |
56 | }; |
57 | ||
78f22bc2 FV |
58 | static void ipoctal_reset_channel(struct ipoctal_channel *channel) |
59 | { | |
60 | iowrite8(CR_DISABLE_RX | CR_DISABLE_TX, &channel->regs->w.cr); | |
61 | channel->rx_enable = 0; | |
62 | iowrite8(CR_CMD_RESET_RX, &channel->regs->w.cr); | |
63 | iowrite8(CR_CMD_RESET_TX, &channel->regs->w.cr); | |
64 | iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr); | |
65 | iowrite8(CR_CMD_RESET_MR, &channel->regs->w.cr); | |
66 | } | |
67 | ||
ba4dc61f SIG |
68 | static int ipoctal_port_activate(struct tty_port *port, struct tty_struct *tty) |
69 | { | |
70a32811 | 70 | struct ipoctal_channel *channel; |
ba4dc61f | 71 | |
9c1d784a | 72 | channel = dev_get_drvdata(tty->dev); |
ba4dc61f | 73 | |
a3882b78 SIG |
74 | /* |
75 | * Enable RX. TX will be enabled when | |
76 | * there is something to send | |
77 | */ | |
459e6d7c | 78 | iowrite8(CR_ENABLE_RX, &channel->regs->w.cr); |
b0d17fbd | 79 | channel->rx_enable = 1; |
ba4dc61f SIG |
80 | return 0; |
81 | } | |
82 | ||
83 | static int ipoctal_open(struct tty_struct *tty, struct file *file) | |
84 | { | |
70a32811 | 85 | struct ipoctal_channel *channel; |
ba4dc61f | 86 | |
9c1d784a | 87 | channel = dev_get_drvdata(tty->dev); |
ef79de03 | 88 | tty->driver_data = channel; |
1337b07e | 89 | |
7e5730d7 | 90 | return tty_port_open(&channel->tty_port, tty, file); |
ba4dc61f SIG |
91 | } |
92 | ||
93 | static void ipoctal_reset_stats(struct ipoctal_stats *stats) | |
94 | { | |
95 | stats->tx = 0; | |
96 | stats->rx = 0; | |
97 | stats->rcv_break = 0; | |
98 | stats->framing_err = 0; | |
99 | stats->overrun_err = 0; | |
100 | stats->parity_err = 0; | |
101 | } | |
102 | ||
ef79de03 | 103 | static void ipoctal_free_channel(struct ipoctal_channel *channel) |
ba4dc61f | 104 | { |
70a32811 JT |
105 | ipoctal_reset_stats(&channel->stats); |
106 | channel->pointer_read = 0; | |
107 | channel->pointer_write = 0; | |
108 | channel->nb_bytes = 0; | |
ba4dc61f SIG |
109 | } |
110 | ||
111 | static void ipoctal_close(struct tty_struct *tty, struct file *filp) | |
112 | { | |
ef79de03 | 113 | struct ipoctal_channel *channel = tty->driver_data; |
ba4dc61f | 114 | |
70a32811 | 115 | tty_port_close(&channel->tty_port, tty, filp); |
7e5730d7 | 116 | ipoctal_free_channel(channel); |
ba4dc61f SIG |
117 | } |
118 | ||
119 | static int ipoctal_get_icount(struct tty_struct *tty, | |
120 | struct serial_icounter_struct *icount) | |
121 | { | |
ef79de03 | 122 | struct ipoctal_channel *channel = tty->driver_data; |
ba4dc61f SIG |
123 | |
124 | icount->cts = 0; | |
125 | icount->dsr = 0; | |
126 | icount->rng = 0; | |
127 | icount->dcd = 0; | |
70a32811 JT |
128 | 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; | |
ba4dc61f SIG |
133 | return 0; |
134 | } | |
135 | ||
2e124b4a | 136 | static void ipoctal_irq_rx(struct ipoctal_channel *channel, u8 sr) |
ba4dc61f | 137 | { |
92a19f9c | 138 | struct tty_port *port = &channel->tty_port; |
ab0a71f0 | 139 | unsigned char value; |
b5071f2c | 140 | unsigned char flag; |
ab0a71f0 SIG |
141 | u8 isr; |
142 | ||
143 | do { | |
144 | value = ioread8(&channel->regs->r.rhr); | |
b5071f2c | 145 | flag = TTY_NORMAL; |
ab0a71f0 SIG |
146 | /* Error: count statistics */ |
147 | if (sr & SR_ERROR) { | |
148 | iowrite8(CR_CMD_RESET_ERR_STATUS, &channel->regs->w.cr); | |
149 | ||
150 | if (sr & SR_OVERRUN_ERROR) { | |
151 | channel->stats.overrun_err++; | |
152 | /* Overrun doesn't affect the current character*/ | |
92a19f9c | 153 | tty_insert_flip_char(port, 0, TTY_OVERRUN); |
ab0a71f0 SIG |
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) { | |
4514108c | 164 | iowrite8(CR_CMD_RESET_BREAK_CHANGE, &channel->regs->w.cr); |
ab0a71f0 SIG |
165 | channel->stats.rcv_break++; |
166 | flag = TTY_BREAK; | |
167 | } | |
87cfb955 | 168 | } |
92a19f9c | 169 | tty_insert_flip_char(port, value, flag); |
ab0a71f0 SIG |
170 | |
171 | /* 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); | |
87cfb955 | 178 | |
2e124b4a | 179 | tty_flip_buffer_push(port); |
87cfb955 JT |
180 | } |
181 | ||
182 | static void ipoctal_irq_tx(struct ipoctal_channel *channel) | |
183 | { | |
184 | unsigned char value; | |
185 | unsigned int *pointer_write = &channel->pointer_write; | |
186 | ||
69a6b9b1 | 187 | if (channel->nb_bytes == 0) |
87cfb955 | 188 | return; |
ba4dc61f | 189 | |
968d04e8 | 190 | spin_lock(&channel->lock); |
87cfb955 JT |
191 | value = channel->tty_port.xmit_buf[*pointer_write]; |
192 | iowrite8(value, &channel->regs->w.thr); | |
193 | channel->stats.tx++; | |
87cfb955 JT |
194 | (*pointer_write)++; |
195 | *pointer_write = *pointer_write % PAGE_SIZE; | |
196 | channel->nb_bytes--; | |
968d04e8 | 197 | spin_unlock(&channel->lock); |
87cfb955 | 198 | } |
ba4dc61f | 199 | |
87cfb955 JT |
200 | static void ipoctal_irq_channel(struct ipoctal_channel *channel) |
201 | { | |
202 | u8 isr, sr; | |
87cfb955 | 203 | |
87cfb955 JT |
204 | /* The HW is organized in pair of channels. See which register we need |
205 | * to read from */ | |
206 | isr = ioread8(&channel->block_regs->r.isr); | |
207 | sr = ioread8(&channel->regs->r.sr); | |
208 | ||
9d01b6f0 | 209 | if ((sr & SR_TX_EMPTY) && (channel->nb_bytes == 0)) { |
87cfb955 | 210 | iowrite8(CR_DISABLE_TX, &channel->regs->w.cr); |
9d01b6f0 SIG |
211 | /* In case of RS-485, change from TX to RX when finishing TX. |
212 | * Half-duplex. */ | |
213 | if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) { | |
214 | iowrite8(CR_CMD_NEGATE_RTSN, &channel->regs->w.cr); | |
215 | iowrite8(CR_ENABLE_RX, &channel->regs->w.cr); | |
2910fe2a | 216 | channel->rx_enable = 1; |
9d01b6f0 | 217 | } |
ba4dc61f | 218 | } |
87cfb955 JT |
219 | |
220 | /* RX data */ | |
221 | if ((isr & channel->isr_rx_rdy_mask) && (sr & SR_RX_READY)) | |
2e124b4a | 222 | ipoctal_irq_rx(channel, sr); |
87cfb955 JT |
223 | |
224 | /* TX of each character */ | |
225 | if ((isr & channel->isr_tx_rdy_mask) && (sr & SR_TX_READY)) | |
226 | ipoctal_irq_tx(channel); | |
87cfb955 JT |
227 | } |
228 | ||
faa75c40 | 229 | static irqreturn_t ipoctal_irq_handler(void *arg) |
87cfb955 JT |
230 | { |
231 | unsigned int i; | |
232 | struct ipoctal *ipoctal = (struct ipoctal *) arg; | |
233 | ||
ea991147 | 234 | /* Clear the IPack device interrupt */ |
402228db JT |
235 | readw(ipoctal->int_space + ACK_INT_REQ0); |
236 | readw(ipoctal->int_space + ACK_INT_REQ1); | |
ea991147 | 237 | |
21d27ed4 SIG |
238 | /* Check all channels */ |
239 | for (i = 0; i < NR_CHANNELS; i++) | |
240 | ipoctal_irq_channel(&ipoctal->channel[i]); | |
241 | ||
ba4dc61f SIG |
242 | return IRQ_HANDLED; |
243 | } | |
244 | ||
ba4dc61f SIG |
245 | static const struct tty_port_operations ipoctal_tty_port_ops = { |
246 | .dtr_rts = NULL, | |
247 | .activate = ipoctal_port_activate, | |
248 | }; | |
249 | ||
250 | static int ipoctal_inst_slot(struct ipoctal *ipoctal, unsigned int bus_nr, | |
c6e2dfaa | 251 | unsigned int slot) |
ba4dc61f | 252 | { |
402228db | 253 | int res; |
ba4dc61f SIG |
254 | int i; |
255 | struct tty_driver *tty; | |
256 | char name[20]; | |
70a32811 | 257 | struct ipoctal_channel *channel; |
402228db JT |
258 | struct ipack_region *region; |
259 | void __iomem *addr; | |
70a32811 JT |
260 | union scc2698_channel __iomem *chan_regs; |
261 | union scc2698_block __iomem *block_regs; | |
ba4dc61f | 262 | |
2ec678d1 | 263 | ipoctal->board_id = ipoctal->dev->id_device; |
ba4dc61f | 264 | |
402228db JT |
265 | region = &ipoctal->dev->region[IPACK_IO_SPACE]; |
266 | addr = devm_ioremap_nocache(&ipoctal->dev->dev, | |
267 | region->start, region->size); | |
268 | if (!addr) { | |
42b38207 SIG |
269 | dev_err(&ipoctal->dev->dev, |
270 | "Unable to map slot [%d:%d] IO space!\n", | |
271 | bus_nr, slot); | |
402228db | 272 | return -EADDRNOTAVAIL; |
ba4dc61f | 273 | } |
402228db JT |
274 | /* Save the virtual address to access the registers easily */ |
275 | chan_regs = | |
276 | (union scc2698_channel __iomem *) addr; | |
277 | block_regs = | |
278 | (union scc2698_block __iomem *) addr; | |
ba4dc61f | 279 | |
402228db JT |
280 | region = &ipoctal->dev->region[IPACK_INT_SPACE]; |
281 | ipoctal->int_space = | |
282 | devm_ioremap_nocache(&ipoctal->dev->dev, | |
283 | region->start, region->size); | |
284 | if (!ipoctal->int_space) { | |
ea991147 JT |
285 | dev_err(&ipoctal->dev->dev, |
286 | "Unable to map slot [%d:%d] INT space!\n", | |
287 | bus_nr, slot); | |
402228db | 288 | return -EADDRNOTAVAIL; |
ea991147 JT |
289 | } |
290 | ||
fe4a3ed0 JT |
291 | region = &ipoctal->dev->region[IPACK_MEM8_SPACE]; |
292 | ipoctal->mem8_space = | |
402228db JT |
293 | devm_ioremap_nocache(&ipoctal->dev->dev, |
294 | region->start, 0x8000); | |
fc8d713e | 295 | if (!ipoctal->mem8_space) { |
42b38207 | 296 | dev_err(&ipoctal->dev->dev, |
fe4a3ed0 | 297 | "Unable to map slot [%d:%d] MEM8 space!\n", |
42b38207 | 298 | bus_nr, slot); |
402228db | 299 | return -EADDRNOTAVAIL; |
ba4dc61f SIG |
300 | } |
301 | ||
ba4dc61f SIG |
302 | |
303 | /* Disable RX and TX before touching anything */ | |
304 | for (i = 0; i < NR_CHANNELS ; i++) { | |
70a32811 JT |
305 | struct ipoctal_channel *channel = &ipoctal->channel[i]; |
306 | channel->regs = chan_regs + i; | |
307 | channel->block_regs = block_regs + (i >> 1); | |
ef79de03 | 308 | channel->board_id = ipoctal->board_id; |
4e4732ac JT |
309 | if (i & 1) { |
310 | channel->isr_tx_rdy_mask = ISR_TxRDY_B; | |
311 | channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_B; | |
312 | } else { | |
313 | channel->isr_tx_rdy_mask = ISR_TxRDY_A; | |
314 | channel->isr_rx_rdy_mask = ISR_RxRDY_FFULL_A; | |
315 | } | |
70a32811 | 316 | |
78f22bc2 | 317 | ipoctal_reset_channel(channel); |
459e6d7c JT |
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); | |
ba4dc61f SIG |
322 | } |
323 | ||
324 | for (i = 0; i < IP_OCTAL_NB_BLOCKS; i++) { | |
459e6d7c JT |
325 | 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); | |
ba4dc61f SIG |
331 | } |
332 | ||
c6e2dfaa | 333 | /* Dummy write */ |
fe4a3ed0 | 334 | iowrite8(1, ipoctal->mem8_space + 1); |
ba4dc61f SIG |
335 | |
336 | /* Register the TTY device */ | |
337 | ||
338 | /* Each IP-OCTAL channel is a TTY port */ | |
339 | tty = alloc_tty_driver(NR_CHANNELS); | |
340 | ||
402228db JT |
341 | if (!tty) |
342 | return -ENOMEM; | |
ba4dc61f SIG |
343 | |
344 | /* Fill struct tty_driver with ipoctal data */ | |
345 | tty->owner = THIS_MODULE; | |
3f3a5927 JT |
346 | tty->driver_name = KBUILD_MODNAME; |
347 | sprintf(name, KBUILD_MODNAME ".%d.%d.", bus_nr, slot); | |
ba4dc61f SIG |
348 | tty->name = name; |
349 | tty->major = 0; | |
350 | ||
351 | tty->minor_start = 0; | |
352 | tty->type = TTY_DRIVER_TYPE_SERIAL; | |
353 | tty->subtype = SERIAL_TYPE_NORMAL; | |
354 | tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; | |
355 | tty->init_termios = tty_std_termios; | |
356 | tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; | |
357 | tty->init_termios.c_ispeed = 9600; | |
358 | tty->init_termios.c_ospeed = 9600; | |
359 | ||
360 | tty_set_operations(tty, &ipoctal_fops); | |
361 | res = tty_register_driver(tty); | |
362 | if (res) { | |
42b38207 | 363 | dev_err(&ipoctal->dev->dev, "Can't register tty driver.\n"); |
ba4dc61f | 364 | put_tty_driver(tty); |
402228db | 365 | return res; |
ba4dc61f SIG |
366 | } |
367 | ||
368 | /* Save struct tty_driver for use it when uninstalling the device */ | |
369 | ipoctal->tty_drv = tty; | |
370 | ||
371 | for (i = 0; i < NR_CHANNELS; i++) { | |
2afb41d9 JT |
372 | struct device *tty_dev; |
373 | ||
70a32811 JT |
374 | channel = &ipoctal->channel[i]; |
375 | tty_port_init(&channel->tty_port); | |
376 | tty_port_alloc_xmit_buf(&channel->tty_port); | |
377 | channel->tty_port.ops = &ipoctal_tty_port_ops; | |
378 | ||
379 | ipoctal_reset_stats(&channel->stats); | |
380 | channel->nb_bytes = 0; | |
70a32811 JT |
381 | spin_lock_init(&channel->lock); |
382 | channel->pointer_read = 0; | |
383 | channel->pointer_write = 0; | |
3498d13b | 384 | tty_dev = tty_port_register_device(&channel->tty_port, tty, i, NULL); |
2afb41d9 JT |
385 | if (IS_ERR(tty_dev)) { |
386 | dev_err(&ipoctal->dev->dev, "Failed to register tty device.\n"); | |
191c5f10 | 387 | tty_port_destroy(&channel->tty_port); |
2afb41d9 JT |
388 | continue; |
389 | } | |
9c1d784a | 390 | dev_set_drvdata(tty_dev, channel); |
ba4dc61f SIG |
391 | } |
392 | ||
4847cc07 FV |
393 | /* |
394 | * IP-OCTAL has different addresses to copy its IRQ vector. | |
395 | * Depending of the carrier these addresses are accesible or not. | |
396 | * More info in the datasheet. | |
397 | */ | |
398 | ipoctal->dev->bus->ops->request_irq(ipoctal->dev, | |
399 | ipoctal_irq_handler, ipoctal); | |
400 | ||
ba4dc61f | 401 | return 0; |
ba4dc61f SIG |
402 | } |
403 | ||
70a32811 | 404 | static inline int ipoctal_copy_write_buffer(struct ipoctal_channel *channel, |
ba4dc61f SIG |
405 | const unsigned char *buf, |
406 | int count) | |
407 | { | |
408 | unsigned long flags; | |
409 | int i; | |
70a32811 | 410 | unsigned int *pointer_read = &channel->pointer_read; |
ba4dc61f SIG |
411 | |
412 | /* Copy the bytes from the user buffer to the internal one */ | |
413 | for (i = 0; i < count; i++) { | |
70a32811 JT |
414 | if (i <= (PAGE_SIZE - channel->nb_bytes)) { |
415 | spin_lock_irqsave(&channel->lock, flags); | |
416 | channel->tty_port.xmit_buf[*pointer_read] = buf[i]; | |
ba4dc61f | 417 | *pointer_read = (*pointer_read + 1) % PAGE_SIZE; |
70a32811 JT |
418 | channel->nb_bytes++; |
419 | spin_unlock_irqrestore(&channel->lock, flags); | |
ba4dc61f SIG |
420 | } else { |
421 | break; | |
422 | } | |
423 | } | |
424 | return i; | |
425 | } | |
426 | ||
699a89f1 JT |
427 | static int ipoctal_write_tty(struct tty_struct *tty, |
428 | const unsigned char *buf, int count) | |
ba4dc61f | 429 | { |
699a89f1 | 430 | struct ipoctal_channel *channel = tty->driver_data; |
d0460067 | 431 | unsigned int char_copied; |
699a89f1 | 432 | |
d0460067 | 433 | char_copied = ipoctal_copy_write_buffer(channel, buf, count); |
ba4dc61f | 434 | |
ba4dc61f | 435 | /* As the IP-OCTAL 485 only supports half duplex, do it manually */ |
ef79de03 | 436 | if (channel->board_id == IPACK1_DEVICE_ID_SBS_OCTAL_485) { |
459e6d7c | 437 | iowrite8(CR_DISABLE_RX, &channel->regs->w.cr); |
b0d17fbd | 438 | channel->rx_enable = 0; |
459e6d7c | 439 | iowrite8(CR_CMD_ASSERT_RTSN, &channel->regs->w.cr); |
ba4dc61f SIG |
440 | } |
441 | ||
442 | /* | |
443 | * Send a packet and then disable TX to avoid failure after several send | |
444 | * operations | |
445 | */ | |
459e6d7c | 446 | iowrite8(CR_ENABLE_TX, &channel->regs->w.cr); |
d0460067 | 447 | return char_copied; |
ba4dc61f SIG |
448 | } |
449 | ||
ba4dc61f SIG |
450 | static int ipoctal_write_room(struct tty_struct *tty) |
451 | { | |
ef79de03 | 452 | struct ipoctal_channel *channel = tty->driver_data; |
ba4dc61f | 453 | |
70a32811 | 454 | return PAGE_SIZE - channel->nb_bytes; |
ba4dc61f SIG |
455 | } |
456 | ||
457 | static int ipoctal_chars_in_buffer(struct tty_struct *tty) | |
458 | { | |
ef79de03 | 459 | struct ipoctal_channel *channel = tty->driver_data; |
ba4dc61f | 460 | |
70a32811 | 461 | return channel->nb_bytes; |
ba4dc61f SIG |
462 | } |
463 | ||
464 | static void ipoctal_set_termios(struct tty_struct *tty, | |
465 | struct ktermios *old_termios) | |
466 | { | |
467 | unsigned int cflag; | |
468 | unsigned char mr1 = 0; | |
469 | unsigned char mr2 = 0; | |
470 | unsigned char csr = 0; | |
ef79de03 | 471 | struct ipoctal_channel *channel = tty->driver_data; |
ba4dc61f SIG |
472 | speed_t baud; |
473 | ||
857196e2 | 474 | cflag = tty->termios.c_cflag; |
ba4dc61f SIG |
475 | |
476 | /* Disable and reset everything before change the setup */ | |
78f22bc2 | 477 | ipoctal_reset_channel(channel); |
ba4dc61f SIG |
478 | |
479 | /* Set Bits per chars */ | |
480 | switch (cflag & CSIZE) { | |
481 | case CS6: | |
482 | mr1 |= MR1_CHRL_6_BITS; | |
483 | break; | |
484 | case CS7: | |
485 | mr1 |= MR1_CHRL_7_BITS; | |
486 | break; | |
487 | case CS8: | |
488 | default: | |
489 | mr1 |= MR1_CHRL_8_BITS; | |
490 | /* By default, select CS8 */ | |
857196e2 | 491 | tty->termios.c_cflag = (cflag & ~CSIZE) | CS8; |
ba4dc61f SIG |
492 | break; |
493 | } | |
494 | ||
495 | /* Set Parity */ | |
496 | if (cflag & PARENB) | |
497 | if (cflag & PARODD) | |
498 | mr1 |= MR1_PARITY_ON | MR1_PARITY_ODD; | |
499 | else | |
500 | mr1 |= MR1_PARITY_ON | MR1_PARITY_EVEN; | |
501 | else | |
502 | mr1 |= MR1_PARITY_OFF; | |
503 | ||
504 | /* Mark or space parity is not supported */ | |
857196e2 | 505 | tty->termios.c_cflag &= ~CMSPAR; |
ba4dc61f SIG |
506 | |
507 | /* Set stop bits */ | |
508 | if (cflag & CSTOPB) | |
509 | mr2 |= MR2_STOP_BITS_LENGTH_2; | |
510 | else | |
511 | mr2 |= MR2_STOP_BITS_LENGTH_1; | |
512 | ||
513 | /* Set the flow control */ | |
ef79de03 | 514 | switch (channel->board_id) { |
7db5e3cb | 515 | case IPACK1_DEVICE_ID_SBS_OCTAL_232: |
ba4dc61f SIG |
516 | if (cflag & CRTSCTS) { |
517 | mr1 |= MR1_RxRTS_CONTROL_ON; | |
518 | mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_ON; | |
ba4dc61f SIG |
519 | } else { |
520 | mr1 |= MR1_RxRTS_CONTROL_OFF; | |
521 | mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF; | |
ba4dc61f SIG |
522 | } |
523 | break; | |
7db5e3cb | 524 | case IPACK1_DEVICE_ID_SBS_OCTAL_422: |
ba4dc61f SIG |
525 | mr1 |= MR1_RxRTS_CONTROL_OFF; |
526 | mr2 |= MR2_TxRTS_CONTROL_OFF | MR2_CTS_ENABLE_TX_OFF; | |
ba4dc61f | 527 | break; |
7db5e3cb | 528 | case IPACK1_DEVICE_ID_SBS_OCTAL_485: |
ba4dc61f SIG |
529 | mr1 |= MR1_RxRTS_CONTROL_OFF; |
530 | mr2 |= MR2_TxRTS_CONTROL_ON | MR2_CTS_ENABLE_TX_OFF; | |
ba4dc61f SIG |
531 | break; |
532 | default: | |
533 | return; | |
534 | break; | |
535 | } | |
536 | ||
537 | baud = tty_get_baud_rate(tty); | |
857196e2 | 538 | tty_termios_encode_baud_rate(&tty->termios, baud, baud); |
ba4dc61f SIG |
539 | |
540 | /* Set baud rate */ | |
857196e2 | 541 | switch (baud) { |
ba4dc61f SIG |
542 | case 75: |
543 | csr |= TX_CLK_75 | RX_CLK_75; | |
544 | break; | |
545 | case 110: | |
546 | csr |= TX_CLK_110 | RX_CLK_110; | |
547 | break; | |
548 | case 150: | |
549 | csr |= TX_CLK_150 | RX_CLK_150; | |
550 | break; | |
551 | case 300: | |
552 | csr |= TX_CLK_300 | RX_CLK_300; | |
553 | break; | |
554 | case 600: | |
555 | csr |= TX_CLK_600 | RX_CLK_600; | |
556 | break; | |
557 | case 1200: | |
558 | csr |= TX_CLK_1200 | RX_CLK_1200; | |
559 | break; | |
560 | case 1800: | |
561 | csr |= TX_CLK_1800 | RX_CLK_1800; | |
562 | break; | |
563 | case 2000: | |
564 | csr |= TX_CLK_2000 | RX_CLK_2000; | |
565 | break; | |
566 | case 2400: | |
567 | csr |= TX_CLK_2400 | RX_CLK_2400; | |
568 | break; | |
569 | case 4800: | |
570 | csr |= TX_CLK_4800 | RX_CLK_4800; | |
571 | break; | |
572 | case 9600: | |
573 | csr |= TX_CLK_9600 | RX_CLK_9600; | |
574 | break; | |
575 | case 19200: | |
576 | csr |= TX_CLK_19200 | RX_CLK_19200; | |
577 | break; | |
578 | case 38400: | |
579 | default: | |
580 | csr |= TX_CLK_38400 | RX_CLK_38400; | |
581 | /* In case of default, we establish 38400 bps */ | |
857196e2 | 582 | tty_termios_encode_baud_rate(&tty->termios, 38400, 38400); |
ba4dc61f SIG |
583 | break; |
584 | } | |
585 | ||
586 | mr1 |= MR1_ERROR_CHAR; | |
587 | mr1 |= MR1_RxINT_RxRDY; | |
588 | ||
589 | /* Write the control registers */ | |
459e6d7c JT |
590 | iowrite8(mr1, &channel->regs->w.mr); |
591 | iowrite8(mr2, &channel->regs->w.mr); | |
592 | iowrite8(csr, &channel->regs->w.csr); | |
ba4dc61f | 593 | |
b0d17fbd SIG |
594 | /* Enable again the RX, if it was before */ |
595 | if (channel->rx_enable) | |
596 | iowrite8(CR_ENABLE_RX, &channel->regs->w.cr); | |
ba4dc61f SIG |
597 | } |
598 | ||
599 | static void ipoctal_hangup(struct tty_struct *tty) | |
600 | { | |
601 | unsigned long flags; | |
ef79de03 | 602 | struct ipoctal_channel *channel = tty->driver_data; |
ba4dc61f | 603 | |
ef79de03 | 604 | if (channel == NULL) |
ba4dc61f SIG |
605 | return; |
606 | ||
70a32811 JT |
607 | spin_lock_irqsave(&channel->lock, flags); |
608 | channel->nb_bytes = 0; | |
609 | channel->pointer_read = 0; | |
610 | channel->pointer_write = 0; | |
611 | spin_unlock_irqrestore(&channel->lock, flags); | |
ba4dc61f | 612 | |
70a32811 | 613 | tty_port_hangup(&channel->tty_port); |
ba4dc61f | 614 | |
78f22bc2 | 615 | ipoctal_reset_channel(channel); |
ba4dc61f | 616 | |
70a32811 JT |
617 | clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags); |
618 | wake_up_interruptible(&channel->tty_port.open_wait); | |
ba4dc61f SIG |
619 | } |
620 | ||
e0f8d323 SIG |
621 | static void ipoctal_shutdown(struct tty_struct *tty) |
622 | { | |
623 | struct ipoctal_channel *channel = tty->driver_data; | |
624 | ||
625 | if (channel == NULL) | |
626 | return; | |
627 | ||
78f22bc2 | 628 | ipoctal_reset_channel(channel); |
e0f8d323 SIG |
629 | clear_bit(ASYNCB_INITIALIZED, &channel->tty_port.flags); |
630 | } | |
631 | ||
ba4dc61f SIG |
632 | static const struct tty_operations ipoctal_fops = { |
633 | .ioctl = NULL, | |
634 | .open = ipoctal_open, | |
635 | .close = ipoctal_close, | |
636 | .write = ipoctal_write_tty, | |
637 | .set_termios = ipoctal_set_termios, | |
638 | .write_room = ipoctal_write_room, | |
639 | .chars_in_buffer = ipoctal_chars_in_buffer, | |
640 | .get_icount = ipoctal_get_icount, | |
641 | .hangup = ipoctal_hangup, | |
e0f8d323 | 642 | .shutdown = ipoctal_shutdown, |
ba4dc61f SIG |
643 | }; |
644 | ||
ba4dc61f SIG |
645 | static int ipoctal_probe(struct ipack_device *dev) |
646 | { | |
647 | int res; | |
648 | struct ipoctal *ipoctal; | |
649 | ||
650 | ipoctal = kzalloc(sizeof(struct ipoctal), GFP_KERNEL); | |
651 | if (ipoctal == NULL) | |
652 | return -ENOMEM; | |
653 | ||
654 | ipoctal->dev = dev; | |
f9e314d2 | 655 | res = ipoctal_inst_slot(ipoctal, dev->bus->bus_nr, dev->slot); |
ba4dc61f SIG |
656 | if (res) |
657 | goto out_uninst; | |
658 | ||
1adda497 | 659 | dev_set_drvdata(&dev->dev, ipoctal); |
ba4dc61f SIG |
660 | return 0; |
661 | ||
662 | out_uninst: | |
663 | kfree(ipoctal); | |
664 | return res; | |
665 | } | |
666 | ||
667 | static void __ipoctal_remove(struct ipoctal *ipoctal) | |
668 | { | |
669 | int i; | |
670 | ||
690949e7 SIG |
671 | ipoctal->dev->bus->ops->free_irq(ipoctal->dev); |
672 | ||
ba4dc61f | 673 | for (i = 0; i < NR_CHANNELS; i++) { |
70a32811 | 674 | struct ipoctal_channel *channel = &ipoctal->channel[i]; |
ba4dc61f | 675 | tty_unregister_device(ipoctal->tty_drv, i); |
70a32811 | 676 | tty_port_free_xmit_buf(&channel->tty_port); |
191c5f10 | 677 | tty_port_destroy(&channel->tty_port); |
ba4dc61f SIG |
678 | } |
679 | ||
680 | tty_unregister_driver(ipoctal->tty_drv); | |
681 | put_tty_driver(ipoctal->tty_drv); | |
ba4dc61f SIG |
682 | kfree(ipoctal); |
683 | } | |
684 | ||
1adda497 | 685 | static void ipoctal_remove(struct ipack_device *idev) |
ba4dc61f | 686 | { |
1adda497 | 687 | __ipoctal_remove(dev_get_drvdata(&idev->dev)); |
ba4dc61f SIG |
688 | } |
689 | ||
fdfc8cf5 JT |
690 | static DEFINE_IPACK_DEVICE_TABLE(ipoctal_ids) = { |
691 | { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS, | |
692 | IPACK1_DEVICE_ID_SBS_OCTAL_232) }, | |
693 | { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS, | |
694 | IPACK1_DEVICE_ID_SBS_OCTAL_422) }, | |
695 | { IPACK_DEVICE(IPACK_ID_VERSION_1, IPACK1_VENDOR_ID_SBS, | |
696 | IPACK1_DEVICE_ID_SBS_OCTAL_485) }, | |
697 | { 0, }, | |
698 | }; | |
699 | ||
700 | MODULE_DEVICE_TABLE(ipack, ipoctal_ids); | |
701 | ||
e8011135 | 702 | static const struct ipack_driver_ops ipoctal_drv_ops = { |
4aa09d47 | 703 | .probe = ipoctal_probe, |
ba4dc61f SIG |
704 | .remove = ipoctal_remove, |
705 | }; | |
706 | ||
e8011135 JT |
707 | static struct ipack_driver driver = { |
708 | .ops = &ipoctal_drv_ops, | |
fdfc8cf5 | 709 | .id_table = ipoctal_ids, |
e8011135 JT |
710 | }; |
711 | ||
ba4dc61f SIG |
712 | static int __init ipoctal_init(void) |
713 | { | |
ec440335 | 714 | return ipack_driver_register(&driver, THIS_MODULE, KBUILD_MODNAME); |
ba4dc61f SIG |
715 | } |
716 | ||
717 | static void __exit ipoctal_exit(void) | |
718 | { | |
ba4dc61f SIG |
719 | ipack_driver_unregister(&driver); |
720 | } | |
721 | ||
722 | MODULE_DESCRIPTION("IP-Octal 232, 422 and 485 device driver"); | |
723 | MODULE_LICENSE("GPL"); | |
724 | ||
725 | module_init(ipoctal_init); | |
726 | module_exit(ipoctal_exit); |