1 // SPDX-License-Identifier: GPL-2.0
3 * Simple synchronous serial port driver for ETRAX 100LX.
5 * Synchronous serial ports are used for continuous streamed data like audio.
6 * The default setting for this driver is compatible with the STA 013 MP3
7 * decoder. The driver can easily be tuned to fit other audio encoder/decoders
10 * Copyright (c) 2001-2008 Axis Communications AB
12 * Author: Mikael Starvik, Johan Adolfsson
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/major.h>
20 #include <linux/sched/signal.h>
21 #include <linux/interrupt.h>
22 #include <linux/poll.h>
23 #include <linux/init.h>
24 #include <linux/mutex.h>
25 #include <linux/timer.h>
26 #include <linux/wait.h>
30 #include <arch/svinto.h>
31 #include <linux/uaccess.h>
32 #include <asm/sync_serial.h>
33 #include <arch/io_interface_mux.h>
35 /* The receiver is a bit tricky because of the continuous stream of data.*/
37 /* Three DMA descriptors are linked together. Each DMA descriptor is */
38 /* responsible for port->bufchunk of a common buffer. */
40 /* +---------------------------------------------+ */
41 /* | +----------+ +----------+ +----------+ | */
42 /* +-> | Descr[0] |-->| Descr[1] |-->| Descr[2] |-+ */
43 /* +----------+ +----------+ +----------+ */
46 /* +-------------------------------------+ */
48 /* +-------------------------------------+ */
49 /* |<- data_avail ->| */
52 /* If the application keeps up the pace readp will be right after writep.*/
53 /* If the application can't keep the pace we have to throw away data. */
54 /* The idea is that readp should be ready with the data pointed out by */
55 /* Descr[i] when the DMA has filled in Descr[i+1]. */
56 /* Otherwise we will discard */
57 /* the rest of the data pointed out by Descr1 and set readp to the start */
60 #define SYNC_SERIAL_MAJOR 125
62 /* IN_BUFFER_SIZE should be a multiple of 6 to make sure that 24 bit */
63 /* words can be handled */
64 #define IN_BUFFER_SIZE 12288
65 #define IN_DESCR_SIZE 256
66 #define NUM_IN_DESCR (IN_BUFFER_SIZE/IN_DESCR_SIZE)
67 #define OUT_BUFFER_SIZE 4096
69 #define DEFAULT_FRAME_RATE 0
70 #define DEFAULT_WORD_RATE 7
72 /* NOTE: Enabling some debug will likely cause overrun or underrun,
73 * especially if manual mode is use.
82 /* Define some macros to access ETRAX 100 registers */
83 #define SETF(var, reg, field, val) \
85 var = (var & ~IO_MASK_(reg##_, field##_)) | \
86 IO_FIELD_(reg##_, field##_, val); \
89 #define SETS(var, reg, field, val) \
91 var = (var & ~IO_MASK_(reg##_, field##_)) | \
92 IO_STATE_(reg##_, field##_, _##val); \
96 /* Etrax registers and bits*/
97 const volatile unsigned *const status
;
98 volatile unsigned *const ctrl_data
;
99 volatile unsigned *const output_dma_first
;
100 volatile unsigned char *const output_dma_cmd
;
101 volatile unsigned char *const output_dma_clr_irq
;
102 volatile unsigned *const input_dma_first
;
103 volatile unsigned char *const input_dma_cmd
;
104 volatile unsigned *const input_dma_descr
;
106 volatile unsigned char *const input_dma_clr_irq
;
107 volatile unsigned *const data_out
;
108 const volatile unsigned *const data_in
;
109 char data_avail_bit
; /* In R_IRQ_MASK1_RD/SET/CLR */
110 char transmitter_ready_bit
; /* In R_IRQ_MASK1_RD/SET/CLR */
111 char input_dma_descr_bit
; /* In R_IRQ_MASK2_RD */
113 char output_dma_bit
; /* In R_IRQ_MASK2_RD */
114 /* End of fields initialised in array */
115 char started
; /* 1 if port has been started */
116 char port_nbr
; /* Port 0 or 1 */
117 char busy
; /* 1 if port is busy */
119 char enabled
; /* 1 if port is enabled */
120 char use_dma
; /* 1 if port uses dma */
125 /* Register shadow */
126 unsigned int ctrl_data_shadow
;
127 /* Remaining bytes for current transfer */
128 volatile unsigned int out_count
;
129 /* Current position in out_buffer */
132 /* Next byte to be read by application */
133 volatile unsigned char *volatile readp
;
134 /* Next byte to be written by etrax */
135 volatile unsigned char *volatile writep
;
137 unsigned int in_buffer_size
;
138 unsigned int inbufchunk
;
139 struct etrax_dma_descr out_descr
__attribute__ ((aligned(32)));
140 struct etrax_dma_descr in_descr
[NUM_IN_DESCR
] __attribute__ ((aligned(32)));
141 unsigned char out_buffer
[OUT_BUFFER_SIZE
] __attribute__ ((aligned(32)));
142 unsigned char in_buffer
[IN_BUFFER_SIZE
]__attribute__ ((aligned(32)));
143 unsigned char flip
[IN_BUFFER_SIZE
] __attribute__ ((aligned(32)));
144 struct etrax_dma_descr
*next_rx_desc
;
145 struct etrax_dma_descr
*prev_rx_desc
;
148 wait_queue_head_t out_wait_q
;
149 wait_queue_head_t in_wait_q
;
153 static DEFINE_MUTEX(sync_serial_mutex
);
154 static int etrax_sync_serial_init(void);
155 static void initialize_port(int portnbr
);
156 static inline int sync_data_avail(struct sync_port
*port
);
158 static int sync_serial_open(struct inode
*inode
, struct file
*file
);
159 static int sync_serial_release(struct inode
*inode
, struct file
*file
);
160 static unsigned int sync_serial_poll(struct file
*filp
, poll_table
*wait
);
162 static long sync_serial_ioctl(struct file
*file
,
163 unsigned int cmd
, unsigned long arg
);
164 static ssize_t
sync_serial_write(struct file
*file
, const char *buf
,
165 size_t count
, loff_t
*ppos
);
166 static ssize_t
sync_serial_read(struct file
*file
, char *buf
,
167 size_t count
, loff_t
*ppos
);
169 #if ((defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0) && \
170 defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)) || \
171 (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1) && \
172 defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)))
176 static void send_word(struct sync_port
*port
);
177 static void start_dma(struct sync_port
*port
, const char *data
, int count
);
178 static void start_dma_in(struct sync_port
*port
);
180 static irqreturn_t
tr_interrupt(int irq
, void *dev_id
);
181 static irqreturn_t
rx_interrupt(int irq
, void *dev_id
);
183 #if ((defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0) && \
184 !defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)) || \
185 (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1) && \
186 !defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)))
187 #define SYNC_SER_MANUAL
189 #ifdef SYNC_SER_MANUAL
190 static irqreturn_t
manual_interrupt(int irq
, void *dev_id
);
194 static struct sync_port ports
[] = {
196 .status
= R_SYNC_SERIAL1_STATUS
,
197 .ctrl_data
= R_SYNC_SERIAL1_CTRL
,
198 .output_dma_first
= R_DMA_CH8_FIRST
,
199 .output_dma_cmd
= R_DMA_CH8_CMD
,
200 .output_dma_clr_irq
= R_DMA_CH8_CLR_INTR
,
201 .input_dma_first
= R_DMA_CH9_FIRST
,
202 .input_dma_cmd
= R_DMA_CH9_CMD
,
203 .input_dma_descr
= R_DMA_CH9_DESCR
,
204 .input_dma_clr_irq
= R_DMA_CH9_CLR_INTR
,
205 .data_out
= R_SYNC_SERIAL1_TR_DATA
,
206 .data_in
= R_SYNC_SERIAL1_REC_DATA
,
207 .data_avail_bit
= IO_BITNR(R_IRQ_MASK1_RD
, ser1_data
),
208 .transmitter_ready_bit
= IO_BITNR(R_IRQ_MASK1_RD
, ser1_ready
),
209 .input_dma_descr_bit
= IO_BITNR(R_IRQ_MASK2_RD
, dma9_descr
),
210 .output_dma_bit
= IO_BITNR(R_IRQ_MASK2_RD
, dma8_eop
),
212 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)
219 .status
= R_SYNC_SERIAL3_STATUS
,
220 .ctrl_data
= R_SYNC_SERIAL3_CTRL
,
221 .output_dma_first
= R_DMA_CH4_FIRST
,
222 .output_dma_cmd
= R_DMA_CH4_CMD
,
223 .output_dma_clr_irq
= R_DMA_CH4_CLR_INTR
,
224 .input_dma_first
= R_DMA_CH5_FIRST
,
225 .input_dma_cmd
= R_DMA_CH5_CMD
,
226 .input_dma_descr
= R_DMA_CH5_DESCR
,
227 .input_dma_clr_irq
= R_DMA_CH5_CLR_INTR
,
228 .data_out
= R_SYNC_SERIAL3_TR_DATA
,
229 .data_in
= R_SYNC_SERIAL3_REC_DATA
,
230 .data_avail_bit
= IO_BITNR(R_IRQ_MASK1_RD
, ser3_data
),
231 .transmitter_ready_bit
= IO_BITNR(R_IRQ_MASK1_RD
, ser3_ready
),
232 .input_dma_descr_bit
= IO_BITNR(R_IRQ_MASK2_RD
, dma5_descr
),
233 .output_dma_bit
= IO_BITNR(R_IRQ_MASK2_RD
, dma4_eop
),
235 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)
243 /* Register shadows */
244 static unsigned sync_serial_prescale_shadow
;
246 #define NUMBER_OF_PORTS 2
248 static const struct file_operations sync_serial_fops
= {
249 .owner
= THIS_MODULE
,
250 .write
= sync_serial_write
,
251 .read
= sync_serial_read
,
252 .poll
= sync_serial_poll
,
253 .unlocked_ioctl
= sync_serial_ioctl
,
254 .open
= sync_serial_open
,
255 .release
= sync_serial_release
,
256 .llseek
= noop_llseek
,
259 static int __init
etrax_sync_serial_init(void)
261 ports
[0].enabled
= 0;
262 ports
[1].enabled
= 0;
264 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
265 if (cris_request_io_interface(if_sync_serial_1
, "sync_ser1")) {
266 printk(KERN_CRIT
"ETRAX100LX sync_serial: "
267 "Could not allocate IO group for port %d\n", 0);
271 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1)
272 if (cris_request_io_interface(if_sync_serial_3
, "sync_ser3")) {
273 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
274 cris_free_io_interface(if_sync_serial_1
);
276 printk(KERN_CRIT
"ETRAX100LX sync_serial: "
277 "Could not allocate IO group for port %d\n", 1);
282 if (register_chrdev(SYNC_SERIAL_MAJOR
, "sync serial",
283 &sync_serial_fops
) < 0) {
284 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1)
285 cris_free_io_interface(if_sync_serial_3
);
287 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
288 cris_free_io_interface(if_sync_serial_1
);
290 printk("unable to get major for synchronous serial port\n");
294 /* Deselect synchronous serial ports while configuring. */
295 SETS(gen_config_ii_shadow
, R_GEN_CONFIG_II
, sermode1
, async
);
296 SETS(gen_config_ii_shadow
, R_GEN_CONFIG_II
, sermode3
, async
);
297 *R_GEN_CONFIG_II
= gen_config_ii_shadow
;
299 /* Initialize Ports */
300 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
301 ports
[0].enabled
= 1;
302 SETS(port_pb_i2c_shadow
, R_PORT_PB_I2C
, syncser1
, ss1extra
);
303 SETS(gen_config_ii_shadow
, R_GEN_CONFIG_II
, sermode1
, sync
);
304 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)
305 ports
[0].use_dma
= 1;
307 ports
[0].use_dma
= 0;
312 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1)
313 ports
[1].enabled
= 1;
314 SETS(port_pb_i2c_shadow
, R_PORT_PB_I2C
, syncser3
, ss3extra
);
315 SETS(gen_config_ii_shadow
, R_GEN_CONFIG_II
, sermode3
, sync
);
316 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)
317 ports
[1].use_dma
= 1;
319 ports
[1].use_dma
= 0;
324 *R_PORT_PB_I2C
= port_pb_i2c_shadow
; /* Use PB4/PB7 */
327 *R_SYNC_SERIAL_PRESCALE
= sync_serial_prescale_shadow
= (
328 IO_STATE(R_SYNC_SERIAL_PRESCALE
, clk_sel_u1
, codec
) |
329 IO_STATE(R_SYNC_SERIAL_PRESCALE
, word_stb_sel_u1
, external
) |
330 IO_STATE(R_SYNC_SERIAL_PRESCALE
, clk_sel_u3
, codec
) |
331 IO_STATE(R_SYNC_SERIAL_PRESCALE
, word_stb_sel_u3
, external
) |
332 IO_STATE(R_SYNC_SERIAL_PRESCALE
, prescaler
, div4
) |
333 IO_FIELD(R_SYNC_SERIAL_PRESCALE
, frame_rate
,
334 DEFAULT_FRAME_RATE
) |
335 IO_FIELD(R_SYNC_SERIAL_PRESCALE
, word_rate
, DEFAULT_WORD_RATE
) |
336 IO_STATE(R_SYNC_SERIAL_PRESCALE
, warp_mode
, normal
));
338 /* Select synchronous ports */
339 *R_GEN_CONFIG_II
= gen_config_ii_shadow
;
341 printk(KERN_INFO
"ETRAX 100LX synchronous serial port driver\n");
345 static void __init
initialize_port(int portnbr
)
347 struct sync_port
*port
= &ports
[portnbr
];
349 DEBUG(printk(KERN_DEBUG
"Init sync serial port %d\n", portnbr
));
352 port
->port_nbr
= portnbr
;
354 port
->tr_running
= 0;
357 port
->outp
= port
->out_buffer
;
359 port
->readp
= port
->flip
;
360 port
->writep
= port
->flip
;
361 port
->in_buffer_size
= IN_BUFFER_SIZE
;
362 port
->inbufchunk
= IN_DESCR_SIZE
;
363 port
->next_rx_desc
= &port
->in_descr
[0];
364 port
->prev_rx_desc
= &port
->in_descr
[NUM_IN_DESCR
-1];
365 port
->prev_rx_desc
->ctrl
= d_eol
;
367 init_waitqueue_head(&port
->out_wait_q
);
368 init_waitqueue_head(&port
->in_wait_q
);
370 port
->ctrl_data_shadow
=
371 IO_STATE(R_SYNC_SERIAL1_CTRL
, tr_baud
, c115k2Hz
) |
372 IO_STATE(R_SYNC_SERIAL1_CTRL
, mode
, master_output
) |
373 IO_STATE(R_SYNC_SERIAL1_CTRL
, error
, ignore
) |
374 IO_STATE(R_SYNC_SERIAL1_CTRL
, rec_enable
, disable
) |
375 IO_STATE(R_SYNC_SERIAL1_CTRL
, f_synctype
, normal
) |
376 IO_STATE(R_SYNC_SERIAL1_CTRL
, f_syncsize
, word
) |
377 IO_STATE(R_SYNC_SERIAL1_CTRL
, f_sync
, on
) |
378 IO_STATE(R_SYNC_SERIAL1_CTRL
, clk_mode
, normal
) |
379 IO_STATE(R_SYNC_SERIAL1_CTRL
, clk_halt
, stopped
) |
380 IO_STATE(R_SYNC_SERIAL1_CTRL
, bitorder
, msb
) |
381 IO_STATE(R_SYNC_SERIAL1_CTRL
, tr_enable
, disable
) |
382 IO_STATE(R_SYNC_SERIAL1_CTRL
, wordsize
, size8bit
) |
383 IO_STATE(R_SYNC_SERIAL1_CTRL
, buf_empty
, lmt_8
) |
384 IO_STATE(R_SYNC_SERIAL1_CTRL
, buf_full
, lmt_8
) |
385 IO_STATE(R_SYNC_SERIAL1_CTRL
, flow_ctrl
, enabled
) |
386 IO_STATE(R_SYNC_SERIAL1_CTRL
, clk_polarity
, neg
) |
387 IO_STATE(R_SYNC_SERIAL1_CTRL
, frame_polarity
, normal
)|
388 IO_STATE(R_SYNC_SERIAL1_CTRL
, status_polarity
, inverted
)|
389 IO_STATE(R_SYNC_SERIAL1_CTRL
, clk_driver
, normal
) |
390 IO_STATE(R_SYNC_SERIAL1_CTRL
, frame_driver
, normal
) |
391 IO_STATE(R_SYNC_SERIAL1_CTRL
, status_driver
, normal
)|
392 IO_STATE(R_SYNC_SERIAL1_CTRL
, def_out0
, high
);
395 port
->ctrl_data_shadow
|= IO_STATE(R_SYNC_SERIAL1_CTRL
,
398 port
->ctrl_data_shadow
|= IO_STATE(R_SYNC_SERIAL1_CTRL
,
401 *port
->ctrl_data
= port
->ctrl_data_shadow
;
404 static inline int sync_data_avail(struct sync_port
*port
)
407 unsigned char *start
;
410 start
= (unsigned char *)port
->readp
; /* cast away volatile */
411 end
= (unsigned char *)port
->writep
; /* cast away volatile */
412 /* 0123456789 0123456789
419 avail
= port
->in_buffer_size
- (start
- end
);
423 static inline int sync_data_avail_to_end(struct sync_port
*port
)
426 unsigned char *start
;
429 start
= (unsigned char *)port
->readp
; /* cast away volatile */
430 end
= (unsigned char *)port
->writep
; /* cast away volatile */
431 /* 0123456789 0123456789
439 avail
= port
->flip
+ port
->in_buffer_size
- start
;
444 static int sync_serial_open(struct inode
*inode
, struct file
*file
)
446 int dev
= MINOR(inode
->i_rdev
);
447 struct sync_port
*port
;
451 mutex_lock(&sync_serial_mutex
);
452 DEBUG(printk(KERN_DEBUG
"Open sync serial port %d\n", dev
));
454 if (dev
< 0 || dev
>= NUMBER_OF_PORTS
|| !ports
[dev
].enabled
) {
455 DEBUG(printk(KERN_DEBUG
"Invalid minor %d\n", dev
));
460 /* Allow open this device twice (assuming one reader and one writer) */
461 if (port
->busy
== 2) {
462 DEBUG(printk(KERN_DEBUG
"Device is busy.. \n"));
465 if (port
->init_irqs
) {
467 if (port
== &ports
[0]) {
469 if (request_irq(24, tr_interrupt
, 0,
470 "synchronous serial 1 dma tr",
472 printk(KERN_CRIT
"Can't alloc "
473 "sync serial port 1 IRQ");
475 } else if (request_irq(25, rx_interrupt
, 0,
476 "synchronous serial 1 dma rx",
478 free_irq(24, &port
[0]);
479 printk(KERN_CRIT
"Can't alloc "
480 "sync serial port 1 IRQ");
482 } else if (cris_request_dma(8,
483 "synchronous serial 1 dma tr",
484 DMA_VERBOSE_ON_ERROR
,
486 free_irq(24, &port
[0]);
487 free_irq(25, &port
[0]);
488 printk(KERN_CRIT
"Can't alloc "
489 "sync serial port 1 "
492 } else if (cris_request_dma(9,
493 "synchronous serial 1 dma rec",
494 DMA_VERBOSE_ON_ERROR
,
496 cris_free_dma(8, NULL
);
497 free_irq(24, &port
[0]);
498 free_irq(25, &port
[0]);
499 printk(KERN_CRIT
"Can't alloc "
500 "sync serial port 1 "
505 RESET_DMA(8); WAIT_DMA(8);
506 RESET_DMA(9); WAIT_DMA(9);
507 *R_DMA_CH8_CLR_INTR
=
508 IO_STATE(R_DMA_CH8_CLR_INTR
, clr_eop
,
510 IO_STATE(R_DMA_CH8_CLR_INTR
, clr_descr
,
512 *R_DMA_CH9_CLR_INTR
=
513 IO_STATE(R_DMA_CH9_CLR_INTR
, clr_eop
,
515 IO_STATE(R_DMA_CH9_CLR_INTR
, clr_descr
,
518 IO_STATE(R_IRQ_MASK2_SET
, dma8_eop
,
520 IO_STATE(R_IRQ_MASK2_SET
, dma9_descr
,
522 } else if (port
== &ports
[1]) {
524 if (request_irq(20, tr_interrupt
, 0,
525 "synchronous serial 3 dma tr",
527 printk(KERN_CRIT
"Can't alloc "
528 "sync serial port 3 IRQ");
530 } else if (request_irq(21, rx_interrupt
, 0,
531 "synchronous serial 3 dma rx",
533 free_irq(20, &ports
[1]);
534 printk(KERN_CRIT
"Can't alloc "
535 "sync serial port 3 IRQ");
537 } else if (cris_request_dma(4,
538 "synchronous serial 3 dma tr",
539 DMA_VERBOSE_ON_ERROR
,
541 free_irq(21, &ports
[1]);
542 free_irq(20, &ports
[1]);
543 printk(KERN_CRIT
"Can't alloc "
544 "sync serial port 3 "
547 } else if (cris_request_dma(5,
548 "synchronous serial 3 dma rec",
549 DMA_VERBOSE_ON_ERROR
,
551 cris_free_dma(4, NULL
);
552 free_irq(21, &ports
[1]);
553 free_irq(20, &ports
[1]);
554 printk(KERN_CRIT
"Can't alloc "
555 "sync serial port 3 "
560 RESET_DMA(4); WAIT_DMA(4);
561 RESET_DMA(5); WAIT_DMA(5);
562 *R_DMA_CH4_CLR_INTR
=
563 IO_STATE(R_DMA_CH4_CLR_INTR
, clr_eop
,
565 IO_STATE(R_DMA_CH4_CLR_INTR
, clr_descr
,
567 *R_DMA_CH5_CLR_INTR
=
568 IO_STATE(R_DMA_CH5_CLR_INTR
, clr_eop
,
570 IO_STATE(R_DMA_CH5_CLR_INTR
, clr_descr
,
573 IO_STATE(R_IRQ_MASK2_SET
, dma4_eop
,
575 IO_STATE(R_IRQ_MASK2_SET
, dma5_descr
,
580 } else { /* !port->use_dma */
581 #ifdef SYNC_SER_MANUAL
582 if (port
== &ports
[0]) {
586 "synchronous serial manual irq",
588 printk(KERN_CRIT
"Can't alloc "
589 "sync serial manual irq");
592 } else if (port
== &ports
[1]) {
596 "synchronous serial manual irq",
598 printk(KERN_CRIT
"Can't alloc "
599 "sync serial manual irq");
605 panic("sync_serial: Manual mode not supported.\n");
606 #endif /* SYNC_SER_MANUAL */
608 } /* port->init_irqs */
611 /* Start port if we use it as input */
612 mode
= IO_EXTRACT(R_SYNC_SERIAL1_CTRL
, mode
, port
->ctrl_data_shadow
);
613 if (mode
== IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL
, mode
, master_input
) ||
614 mode
== IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL
, mode
, slave_input
) ||
615 mode
== IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL
, mode
, master_bidir
) ||
616 mode
== IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL
, mode
, slave_bidir
)) {
617 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, clk_halt
,
619 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, tr_enable
,
621 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, rec_enable
,
624 *port
->ctrl_data
= port
->ctrl_data_shadow
;
626 *R_IRQ_MASK1_SET
= 1 << port
->data_avail_bit
;
627 DEBUG(printk(KERN_DEBUG
"sser%d rec started\n", dev
));
632 mutex_unlock(&sync_serial_mutex
);
636 static int sync_serial_release(struct inode
*inode
, struct file
*file
)
638 int dev
= MINOR(inode
->i_rdev
);
639 struct sync_port
*port
;
641 if (dev
< 0 || dev
>= NUMBER_OF_PORTS
|| !ports
[dev
].enabled
) {
642 DEBUG(printk(KERN_DEBUG
"Invalid minor %d\n", dev
));
649 *R_IRQ_MASK1_CLR
= ((1 << port
->data_avail_bit
) |
650 (1 << port
->transmitter_ready_bit
));
657 static unsigned int sync_serial_poll(struct file
*file
, poll_table
*wait
)
659 int dev
= MINOR(file_inode(file
)->i_rdev
);
660 unsigned int mask
= 0;
661 struct sync_port
*port
;
662 DEBUGPOLL(static unsigned int prev_mask
= 0);
665 poll_wait(file
, &port
->out_wait_q
, wait
);
666 poll_wait(file
, &port
->in_wait_q
, wait
);
667 /* Some room to write */
668 if (port
->out_count
< OUT_BUFFER_SIZE
)
669 mask
|= POLLOUT
| POLLWRNORM
;
670 /* At least an inbufchunk of data */
671 if (sync_data_avail(port
) >= port
->inbufchunk
)
672 mask
|= POLLIN
| POLLRDNORM
;
674 DEBUGPOLL(if (mask
!= prev_mask
)
675 printk(KERN_DEBUG
"sync_serial_poll: mask 0x%08X %s %s\n",
677 mask
& POLLOUT
? "POLLOUT" : "",
678 mask
& POLLIN
? "POLLIN" : "");
684 static int sync_serial_ioctl_unlocked(struct file
*file
,
685 unsigned int cmd
, unsigned long arg
)
690 int dev
= MINOR(file_inode(file
)->i_rdev
);
691 struct sync_port
*port
;
693 if (dev
< 0 || dev
>= NUMBER_OF_PORTS
|| !ports
[dev
].enabled
) {
694 DEBUG(printk(KERN_DEBUG
"Invalid minor %d\n", dev
));
699 local_irq_save(flags
);
700 /* Disable port while changing config */
703 RESET_DMA(4); WAIT_DMA(4);
704 port
->tr_running
= 0;
706 port
->outp
= port
->out_buffer
;
707 *R_DMA_CH4_CLR_INTR
=
708 IO_STATE(R_DMA_CH4_CLR_INTR
, clr_eop
, do) |
709 IO_STATE(R_DMA_CH4_CLR_INTR
, clr_descr
, do);
711 SETS(gen_config_ii_shadow
, R_GEN_CONFIG_II
, sermode3
, async
);
714 RESET_DMA(8); WAIT_DMA(8);
715 port
->tr_running
= 0;
717 port
->outp
= port
->out_buffer
;
718 *R_DMA_CH8_CLR_INTR
=
719 IO_STATE(R_DMA_CH8_CLR_INTR
, clr_eop
, do) |
720 IO_STATE(R_DMA_CH8_CLR_INTR
, clr_descr
, do);
722 SETS(gen_config_ii_shadow
, R_GEN_CONFIG_II
, sermode1
, async
);
724 *R_GEN_CONFIG_II
= gen_config_ii_shadow
;
725 local_irq_restore(flags
);
729 if (GET_SPEED(arg
) == CODEC
) {
731 SETS(sync_serial_prescale_shadow
,
732 R_SYNC_SERIAL_PRESCALE
, clk_sel_u3
,
735 SETS(sync_serial_prescale_shadow
,
736 R_SYNC_SERIAL_PRESCALE
, clk_sel_u1
,
739 SETF(sync_serial_prescale_shadow
,
740 R_SYNC_SERIAL_PRESCALE
, prescaler
,
742 SETF(sync_serial_prescale_shadow
,
743 R_SYNC_SERIAL_PRESCALE
, frame_rate
,
744 GET_FRAME_RATE(arg
));
745 SETF(sync_serial_prescale_shadow
,
746 R_SYNC_SERIAL_PRESCALE
, word_rate
,
749 SETF(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
750 tr_baud
, GET_SPEED(arg
));
752 SETS(sync_serial_prescale_shadow
,
753 R_SYNC_SERIAL_PRESCALE
, clk_sel_u3
,
756 SETS(sync_serial_prescale_shadow
,
757 R_SYNC_SERIAL_PRESCALE
, clk_sel_u1
,
764 if (arg
== MASTER_OUTPUT
|| arg
== SLAVE_OUTPUT
)
765 *R_IRQ_MASK1_CLR
= 1 << port
->data_avail_bit
;
766 else if (!port
->use_dma
)
767 *R_IRQ_MASK1_SET
= 1 << port
->data_avail_bit
;
768 SETF(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, mode
, arg
);
771 if (arg
& NORMAL_SYNC
)
772 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
774 else if (arg
& EARLY_SYNC
)
775 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
779 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
781 else if (arg
& WORD_SYNC
)
782 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
784 else if (arg
& EXTENDED_SYNC
)
785 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
786 f_syncsize
, extended
);
789 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
791 else if (arg
& SYNC_OFF
)
792 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
795 if (arg
& WORD_SIZE_8
)
796 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
798 else if (arg
& WORD_SIZE_12
)
799 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
800 wordsize
, size12bit
);
801 else if (arg
& WORD_SIZE_16
)
802 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
803 wordsize
, size16bit
);
804 else if (arg
& WORD_SIZE_24
)
805 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
806 wordsize
, size24bit
);
807 else if (arg
& WORD_SIZE_32
)
808 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
809 wordsize
, size32bit
);
811 if (arg
& BIT_ORDER_MSB
)
812 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
814 else if (arg
& BIT_ORDER_LSB
)
815 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
818 if (arg
& FLOW_CONTROL_ENABLE
)
819 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
821 else if (arg
& FLOW_CONTROL_DISABLE
)
822 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
823 flow_ctrl
, disabled
);
825 if (arg
& CLOCK_NOT_GATED
)
826 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
828 else if (arg
& CLOCK_GATED
)
829 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
834 /* NOTE!! negedge is considered NORMAL */
835 if (arg
& CLOCK_NORMAL
)
836 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
838 else if (arg
& CLOCK_INVERT
)
839 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
842 if (arg
& FRAME_NORMAL
)
843 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
844 frame_polarity
, normal
);
845 else if (arg
& FRAME_INVERT
)
846 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
847 frame_polarity
, inverted
);
849 if (arg
& STATUS_NORMAL
)
850 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
851 status_polarity
, normal
);
852 else if (arg
& STATUS_INVERT
)
853 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
854 status_polarity
, inverted
);
857 if (arg
& CLOCK_NORMAL
)
858 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
860 else if (arg
& CLOCK_INVERT
)
861 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
862 clk_driver
, inverted
);
864 if (arg
& FRAME_NORMAL
)
865 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
866 frame_driver
, normal
);
867 else if (arg
& FRAME_INVERT
)
868 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
869 frame_driver
, inverted
);
871 if (arg
& STATUS_NORMAL
)
872 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
873 status_driver
, normal
);
874 else if (arg
& STATUS_INVERT
)
875 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
876 status_driver
, inverted
);
879 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, flow_ctrl
,
881 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, bitorder
,
883 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, wordsize
,
885 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, f_sync
, on
);
886 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, f_syncsize
,
888 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, f_synctype
,
890 if (arg
& SPI_SLAVE
) {
891 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
892 frame_polarity
, inverted
);
893 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
895 SETF(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
898 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
899 frame_driver
, inverted
);
900 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
901 clk_driver
, inverted
);
902 SETF(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
,
903 mode
, MASTER_OUTPUT
);
908 if (arg
> port
->in_buffer_size
/NUM_IN_DESCR
)
910 port
->inbufchunk
= arg
;
911 /* Make sure in_buffer_size is a multiple of inbufchunk */
912 port
->in_buffer_size
=
913 (port
->in_buffer_size
/port
->inbufchunk
) *
915 DEBUG(printk(KERN_DEBUG
"inbufchunk %i in_buffer_size: %i\n",
916 port
->inbufchunk
, port
->in_buffer_size
));
918 if (port
->port_nbr
== 0) {
932 /* Make sure we write the config without interruption */
933 local_irq_save(flags
);
934 /* Set config and enable port */
935 *port
->ctrl_data
= port
->ctrl_data_shadow
;
936 nop(); nop(); nop(); nop();
937 *R_SYNC_SERIAL_PRESCALE
= sync_serial_prescale_shadow
;
938 nop(); nop(); nop(); nop();
940 SETS(gen_config_ii_shadow
, R_GEN_CONFIG_II
, sermode3
, sync
);
942 SETS(gen_config_ii_shadow
, R_GEN_CONFIG_II
, sermode1
, sync
);
944 *R_GEN_CONFIG_II
= gen_config_ii_shadow
;
945 /* Reset DMA. At readout from serial port the data could be shifted
946 * one byte if not resetting DMA.
949 if (port
->port_nbr
== 0) {
958 local_irq_restore(flags
);
962 static long sync_serial_ioctl(struct file
*file
,
963 unsigned int cmd
, unsigned long arg
)
967 mutex_lock(&sync_serial_mutex
);
968 ret
= sync_serial_ioctl_unlocked(file
, cmd
, arg
);
969 mutex_unlock(&sync_serial_mutex
);
975 static ssize_t
sync_serial_write(struct file
*file
, const char *buf
,
976 size_t count
, loff_t
*ppos
)
978 int dev
= MINOR(file_inode(file
)->i_rdev
);
979 DECLARE_WAITQUEUE(wait
, current
);
980 struct sync_port
*port
;
983 unsigned long free_outp
;
985 unsigned long out_buffer
;
987 if (dev
< 0 || dev
>= NUMBER_OF_PORTS
|| !ports
[dev
].enabled
) {
988 DEBUG(printk(KERN_DEBUG
"Invalid minor %d\n", dev
));
993 DEBUGWRITE(printk(KERN_DEBUG
"W d%d c %lu (%d/%d)\n",
994 port
->port_nbr
, count
, port
->out_count
, OUT_BUFFER_SIZE
));
995 /* Space to end of buffer */
997 * out_buffer <c1>012345<- c ->OUT_BUFFER_SIZE
1000 * out_buffer 45<- c ->0123OUT_BUFFER_SIZE
1006 /* Read variables that may be updated by interrupts */
1007 local_irq_save(flags
);
1008 if (count
> OUT_BUFFER_SIZE
- port
->out_count
)
1009 count
= OUT_BUFFER_SIZE
- port
->out_count
;
1011 outp
= (unsigned long)port
->outp
;
1012 free_outp
= outp
+ port
->out_count
;
1013 local_irq_restore(flags
);
1014 out_buffer
= (unsigned long)port
->out_buffer
;
1016 /* Find out where and how much to write */
1017 if (free_outp
>= out_buffer
+ OUT_BUFFER_SIZE
)
1018 free_outp
-= OUT_BUFFER_SIZE
;
1019 if (free_outp
>= outp
)
1020 c
= out_buffer
+ OUT_BUFFER_SIZE
- free_outp
;
1022 c
= outp
- free_outp
;
1026 DEBUGWRITE(printk(KERN_DEBUG
"w op %08lX fop %08lX c %lu\n",
1027 outp
, free_outp
, c
));
1028 if (copy_from_user((void *)free_outp
, buf
, c
))
1034 DEBUGWRITE(printk(KERN_DEBUG
"w2 fi %lu c %lu c1 %lu\n",
1035 free_outp
-out_buffer
, c
, c1
));
1036 if (copy_from_user((void *)out_buffer
, buf
, c1
))
1039 local_irq_save(flags
);
1040 port
->out_count
+= count
;
1041 local_irq_restore(flags
);
1043 /* Make sure transmitter/receiver is running */
1044 if (!port
->started
) {
1045 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, clk_halt
,
1047 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, tr_enable
,
1049 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, rec_enable
,
1054 *port
->ctrl_data
= port
->ctrl_data_shadow
;
1056 if (file
->f_flags
& O_NONBLOCK
) {
1057 local_irq_save(flags
);
1058 if (!port
->tr_running
) {
1059 if (!port
->use_dma
) {
1060 /* Start sender by writing data */
1062 /* and enable transmitter ready IRQ */
1063 *R_IRQ_MASK1_SET
= 1 <<
1064 port
->transmitter_ready_bit
;
1067 (unsigned char *volatile)port
->outp
, c
);
1069 local_irq_restore(flags
);
1070 DEBUGWRITE(printk(KERN_DEBUG
"w d%d c %lu NB\n",
1071 port
->port_nbr
, count
));
1075 /* Sleep until all sent */
1076 add_wait_queue(&port
->out_wait_q
, &wait
);
1077 set_current_state(TASK_INTERRUPTIBLE
);
1078 local_irq_save(flags
);
1079 if (!port
->tr_running
) {
1080 if (!port
->use_dma
) {
1081 /* Start sender by writing data */
1083 /* and enable transmitter ready IRQ */
1084 *R_IRQ_MASK1_SET
= 1 << port
->transmitter_ready_bit
;
1086 start_dma(port
, port
->outp
, c
);
1088 local_irq_restore(flags
);
1090 remove_wait_queue(&port
->out_wait_q
, &wait
);
1091 if (signal_pending(current
))
1094 DEBUGWRITE(printk(KERN_DEBUG
"w d%d c %lu\n", port
->port_nbr
, count
));
1098 static ssize_t
sync_serial_read(struct file
*file
, char *buf
,
1099 size_t count
, loff_t
*ppos
)
1101 int dev
= MINOR(file_inode(file
)->i_rdev
);
1103 struct sync_port
*port
;
1104 unsigned char *start
;
1106 unsigned long flags
;
1108 if (dev
< 0 || dev
>= NUMBER_OF_PORTS
|| !ports
[dev
].enabled
) {
1109 DEBUG(printk(KERN_DEBUG
"Invalid minor %d\n", dev
));
1114 DEBUGREAD(printk(KERN_DEBUG
"R%d c %d ri %lu wi %lu /%lu\n",
1115 dev
, count
, port
->readp
- port
->flip
,
1116 port
->writep
- port
->flip
, port
->in_buffer_size
));
1118 if (!port
->started
) {
1119 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, clk_halt
,
1121 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, tr_enable
,
1123 SETS(port
->ctrl_data_shadow
, R_SYNC_SERIAL1_CTRL
, rec_enable
,
1127 *port
->ctrl_data
= port
->ctrl_data_shadow
;
1129 /* Calculate number of available bytes */
1130 /* Save pointers to avoid that they are modified by interrupt */
1131 local_irq_save(flags
);
1132 start
= (unsigned char *)port
->readp
; /* cast away volatile */
1133 end
= (unsigned char *)port
->writep
; /* cast away volatile */
1134 local_irq_restore(flags
);
1135 while (start
== end
&& !port
->full
) {
1137 if (file
->f_flags
& O_NONBLOCK
)
1140 wait_event_interruptible(port
->in_wait_q
,
1141 !(start
== end
&& !port
->full
));
1142 if (signal_pending(current
))
1145 local_irq_save(flags
);
1146 start
= (unsigned char *)port
->readp
; /* cast away volatile */
1147 end
= (unsigned char *)port
->writep
; /* cast away volatile */
1148 local_irq_restore(flags
);
1151 /* Lazy read, never return wrapped data. */
1153 avail
= port
->in_buffer_size
;
1154 else if (end
> start
)
1155 avail
= end
- start
;
1157 avail
= port
->flip
+ port
->in_buffer_size
- start
;
1159 count
= count
> avail
? avail
: count
;
1160 if (copy_to_user(buf
, start
, count
))
1162 /* Disable interrupts while updating readp */
1163 local_irq_save(flags
);
1164 port
->readp
+= count
;
1165 if (port
->readp
>= port
->flip
+ port
->in_buffer_size
) /* Wrap? */
1166 port
->readp
= port
->flip
;
1168 local_irq_restore(flags
);
1169 DEBUGREAD(printk(KERN_DEBUG
"r %d\n", count
));
1173 static void send_word(struct sync_port
*port
)
1175 switch (IO_EXTRACT(R_SYNC_SERIAL1_CTRL
, wordsize
,
1176 port
->ctrl_data_shadow
)) {
1177 case IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL
, wordsize
, size8bit
):
1179 *port
->data_out
= *port
->outp
++;
1180 if (port
->outp
>= port
->out_buffer
+ OUT_BUFFER_SIZE
)
1181 port
->outp
= port
->out_buffer
;
1183 case IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL
, wordsize
, size12bit
):
1185 int data
= (*port
->outp
++) << 8;
1186 data
|= *port
->outp
++;
1187 port
->out_count
-= 2;
1188 *port
->data_out
= data
;
1189 if (port
->outp
>= port
->out_buffer
+ OUT_BUFFER_SIZE
)
1190 port
->outp
= port
->out_buffer
;
1193 case IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL
, wordsize
, size16bit
):
1194 port
->out_count
-= 2;
1195 *port
->data_out
= *(unsigned short *)port
->outp
;
1197 if (port
->outp
>= port
->out_buffer
+ OUT_BUFFER_SIZE
)
1198 port
->outp
= port
->out_buffer
;
1200 case IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL
, wordsize
, size24bit
):
1201 port
->out_count
-= 3;
1202 *port
->data_out
= *(unsigned int *)port
->outp
;
1204 if (port
->outp
>= port
->out_buffer
+ OUT_BUFFER_SIZE
)
1205 port
->outp
= port
->out_buffer
;
1207 case IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL
, wordsize
, size32bit
):
1208 port
->out_count
-= 4;
1209 *port
->data_out
= *(unsigned int *)port
->outp
;
1211 if (port
->outp
>= port
->out_buffer
+ OUT_BUFFER_SIZE
)
1212 port
->outp
= port
->out_buffer
;
1218 static void start_dma(struct sync_port
*port
, const char *data
, int count
)
1220 port
->tr_running
= 1;
1221 port
->out_descr
.hw_len
= 0;
1222 port
->out_descr
.next
= 0;
1223 port
->out_descr
.ctrl
= d_eol
| d_eop
; /* No d_wait to avoid glitches */
1224 port
->out_descr
.sw_len
= count
;
1225 port
->out_descr
.buf
= virt_to_phys(data
);
1226 port
->out_descr
.status
= 0;
1228 *port
->output_dma_first
= virt_to_phys(&port
->out_descr
);
1229 *port
->output_dma_cmd
= IO_STATE(R_DMA_CH0_CMD
, cmd
, start
);
1230 DEBUGTXINT(printk(KERN_DEBUG
"dma %08lX c %d\n",
1231 (unsigned long)data
, count
));
1234 static void start_dma_in(struct sync_port
*port
)
1238 port
->writep
= port
->flip
;
1240 if (port
->writep
> port
->flip
+ port
->in_buffer_size
) {
1241 panic("Offset too large in sync serial driver\n");
1244 buf
= virt_to_phys(port
->in_buffer
);
1245 for (i
= 0; i
< NUM_IN_DESCR
; i
++) {
1246 port
->in_descr
[i
].sw_len
= port
->inbufchunk
;
1247 port
->in_descr
[i
].ctrl
= d_int
;
1248 port
->in_descr
[i
].next
= virt_to_phys(&port
->in_descr
[i
+1]);
1249 port
->in_descr
[i
].buf
= buf
;
1250 port
->in_descr
[i
].hw_len
= 0;
1251 port
->in_descr
[i
].status
= 0;
1252 port
->in_descr
[i
].fifo_len
= 0;
1253 buf
+= port
->inbufchunk
;
1254 prepare_rx_descriptor(&port
->in_descr
[i
]);
1256 /* Link the last descriptor to the first */
1257 port
->in_descr
[i
-1].next
= virt_to_phys(&port
->in_descr
[0]);
1258 port
->in_descr
[i
-1].ctrl
|= d_eol
;
1259 port
->next_rx_desc
= &port
->in_descr
[0];
1260 port
->prev_rx_desc
= &port
->in_descr
[NUM_IN_DESCR
- 1];
1261 *port
->input_dma_first
= virt_to_phys(port
->next_rx_desc
);
1262 *port
->input_dma_cmd
= IO_STATE(R_DMA_CH0_CMD
, cmd
, start
);
1266 static irqreturn_t
tr_interrupt(int irq
, void *dev_id
)
1268 unsigned long ireg
= *R_IRQ_MASK2_RD
;
1269 struct etrax_dma_descr
*descr
;
1274 for (i
= 0; i
< NUMBER_OF_PORTS
; i
++) {
1275 struct sync_port
*port
= &ports
[i
];
1276 if (!port
->enabled
|| !port
->use_dma
)
1279 /* IRQ active for the port? */
1280 if (!(ireg
& (1 << port
->output_dma_bit
)))
1286 *port
->output_dma_clr_irq
=
1287 IO_STATE(R_DMA_CH0_CLR_INTR
, clr_eop
, do) |
1288 IO_STATE(R_DMA_CH0_CLR_INTR
, clr_descr
, do);
1290 descr
= &port
->out_descr
;
1291 if (!(descr
->status
& d_stop
))
1292 sentl
= descr
->sw_len
;
1294 /* Otherwise find amount of data sent here */
1295 sentl
= descr
->hw_len
;
1297 port
->out_count
-= sentl
;
1298 port
->outp
+= sentl
;
1299 if (port
->outp
>= port
->out_buffer
+ OUT_BUFFER_SIZE
)
1300 port
->outp
= port
->out_buffer
;
1301 if (port
->out_count
) {
1302 int c
= port
->out_buffer
+ OUT_BUFFER_SIZE
- port
->outp
;
1303 if (c
> port
->out_count
)
1304 c
= port
->out_count
;
1305 DEBUGTXINT(printk(KERN_DEBUG
1306 "tx_int DMAWRITE %i %i\n", sentl
, c
));
1307 start_dma(port
, port
->outp
, c
);
1309 DEBUGTXINT(printk(KERN_DEBUG
1310 "tx_int DMA stop %i\n", sentl
));
1311 port
->tr_running
= 0;
1313 /* wake up the waiting process */
1314 wake_up_interruptible(&port
->out_wait_q
);
1316 return IRQ_RETVAL(handled
);
1317 } /* tr_interrupt */
1319 static irqreturn_t
rx_interrupt(int irq
, void *dev_id
)
1321 unsigned long ireg
= *R_IRQ_MASK2_RD
;
1325 for (i
= 0; i
< NUMBER_OF_PORTS
; i
++) {
1326 struct sync_port
*port
= &ports
[i
];
1328 if (!port
->enabled
|| !port
->use_dma
)
1331 if (!(ireg
& (1 << port
->input_dma_descr_bit
)))
1334 /* Descriptor interrupt */
1336 while (*port
->input_dma_descr
!=
1337 virt_to_phys(port
->next_rx_desc
)) {
1338 if (port
->writep
+ port
->inbufchunk
> port
->flip
+
1339 port
->in_buffer_size
) {
1340 int first_size
= port
->flip
+
1341 port
->in_buffer_size
- port
->writep
;
1342 memcpy(port
->writep
,
1343 phys_to_virt(port
->next_rx_desc
->buf
),
1346 phys_to_virt(port
->next_rx_desc
->buf
+
1348 port
->inbufchunk
- first_size
);
1349 port
->writep
= port
->flip
+
1350 port
->inbufchunk
- first_size
;
1352 memcpy(port
->writep
,
1353 phys_to_virt(port
->next_rx_desc
->buf
),
1355 port
->writep
+= port
->inbufchunk
;
1356 if (port
->writep
>= port
->flip
1357 + port
->in_buffer_size
)
1358 port
->writep
= port
->flip
;
1360 if (port
->writep
== port
->readp
)
1362 prepare_rx_descriptor(port
->next_rx_desc
);
1363 port
->next_rx_desc
->ctrl
|= d_eol
;
1364 port
->prev_rx_desc
->ctrl
&= ~d_eol
;
1365 port
->prev_rx_desc
= phys_to_virt((unsigned)
1366 port
->next_rx_desc
);
1367 port
->next_rx_desc
= phys_to_virt((unsigned)
1368 port
->next_rx_desc
->next
);
1369 /* Wake up the waiting process */
1370 wake_up_interruptible(&port
->in_wait_q
);
1371 *port
->input_dma_cmd
= IO_STATE(R_DMA_CH1_CMD
,
1373 /* DMA has reached end of descriptor */
1374 *port
->input_dma_clr_irq
= IO_STATE(R_DMA_CH0_CLR_INTR
,
1378 return IRQ_RETVAL(handled
);
1379 } /* rx_interrupt */
1380 #endif /* SYNC_SER_DMA */
1382 #ifdef SYNC_SER_MANUAL
1383 static irqreturn_t
manual_interrupt(int irq
, void *dev_id
)
1388 for (i
= 0; i
< NUMBER_OF_PORTS
; i
++) {
1389 struct sync_port
*port
= &ports
[i
];
1391 if (!port
->enabled
|| port
->use_dma
)
1394 /* Data received? */
1395 if (*R_IRQ_MASK1_RD
& (1 << port
->data_avail_bit
)) {
1398 switch (port
->ctrl_data_shadow
&
1399 IO_MASK(R_SYNC_SERIAL1_CTRL
, wordsize
)) {
1400 case IO_STATE(R_SYNC_SERIAL1_CTRL
, wordsize
, size8bit
):
1402 *(volatile char *)port
->data_in
;
1404 case IO_STATE(R_SYNC_SERIAL1_CTRL
, wordsize
, size12bit
):
1406 int data
= *(unsigned short *)port
->data_in
;
1407 *port
->writep
= (data
& 0x0ff0) >> 4;
1408 *(port
->writep
+ 1) = data
& 0x0f;
1412 case IO_STATE(R_SYNC_SERIAL1_CTRL
, wordsize
, size16bit
):
1413 *(unsigned short *)port
->writep
=
1414 *(volatile unsigned short *)port
->data_in
;
1417 case IO_STATE(R_SYNC_SERIAL1_CTRL
, wordsize
, size24bit
):
1418 *(unsigned int *)port
->writep
= *port
->data_in
;
1421 case IO_STATE(R_SYNC_SERIAL1_CTRL
, wordsize
, size32bit
):
1422 *(unsigned int *)port
->writep
= *port
->data_in
;
1428 if (port
->writep
>= port
->flip
+ port
->in_buffer_size
)
1429 port
->writep
= port
->flip
;
1430 if (port
->writep
== port
->readp
) {
1431 /* Receive buffer overrun, discard oldest */
1434 if (port
->readp
>= port
->flip
+
1435 port
->in_buffer_size
)
1436 port
->readp
= port
->flip
;
1438 if (sync_data_avail(port
) >= port
->inbufchunk
) {
1439 /* Wake up application */
1440 wake_up_interruptible(&port
->in_wait_q
);
1444 /* Transmitter ready? */
1445 if (*R_IRQ_MASK1_RD
& (1 << port
->transmitter_ready_bit
)) {
1446 if (port
->out_count
> 0) {
1447 /* More data to send */
1450 /* Transmission finished */
1452 *R_IRQ_MASK1_CLR
= 1 <<
1453 port
->transmitter_ready_bit
;
1454 /* Wake up application */
1455 wake_up_interruptible(&port
->out_wait_q
);
1459 return IRQ_RETVAL(handled
);
1463 module_init(etrax_sync_serial_init
);