14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/errno.h>
19 #include <linux/sched.h>
21 #include <linux/poll.h>
29 #include <asm/uaccess.h>
58 #define SYNC_SERIAL_MAJOR 125
62 #define IN_BUFFER_SIZE 12288
63 #define IN_DESCR_SIZE 256
64 #define NUM_IN_DESCR (IN_BUFFER_SIZE/IN_DESCR_SIZE)
65 #define OUT_BUFFER_SIZE 4096
67 #define DEFAULT_FRAME_RATE 0
68 #define DEFAULT_WORD_RATE 7
81 #define SETF(var, reg, field, val) \
83 var = (var & ~IO_MASK_(reg##_, field##_)) | \
84 IO_FIELD_(reg##_, field##_, val); \
87 #define SETS(var, reg, field, val) \
89 var = (var & ~IO_MASK_(reg##_, field##_)) | \
90 IO_STATE_(reg##_, field##_, _##val); \
95 const volatile unsigned *
const status;
131 volatile unsigned char *
volatile readp;
152 static int etrax_sync_serial_init(
void);
153 static void initialize_port(
int portnbr);
160 static long sync_serial_ioctl(
struct file *
file,
161 unsigned int cmd,
unsigned long arg);
163 size_t count, loff_t *ppos);
165 size_t count, loff_t *ppos);
167 #if ((defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0) && \
168 defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)) || \
169 (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1) && \
170 defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)))
181 #if ((defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0) && \
182 !defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)) || \
183 (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1) && \
184 !defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)))
185 #define SYNC_SER_MANUAL
187 #ifdef SYNC_SER_MANUAL
194 .status = R_SYNC_SERIAL1_STATUS,
195 .ctrl_data = R_SYNC_SERIAL1_CTRL,
196 .output_dma_first = R_DMA_CH8_FIRST,
197 .output_dma_cmd = R_DMA_CH8_CMD,
198 .output_dma_clr_irq = R_DMA_CH8_CLR_INTR,
199 .input_dma_first = R_DMA_CH9_FIRST,
200 .input_dma_cmd = R_DMA_CH9_CMD,
201 .input_dma_descr = R_DMA_CH9_DESCR,
202 .input_dma_clr_irq = R_DMA_CH9_CLR_INTR,
203 .data_out = R_SYNC_SERIAL1_TR_DATA,
204 .data_in = R_SYNC_SERIAL1_REC_DATA,
205 .data_avail_bit =
IO_BITNR(R_IRQ_MASK1_RD, ser1_data),
206 .transmitter_ready_bit =
IO_BITNR(R_IRQ_MASK1_RD, ser1_ready),
207 .input_dma_descr_bit =
IO_BITNR(R_IRQ_MASK2_RD, dma9_descr),
208 .output_dma_bit =
IO_BITNR(R_IRQ_MASK2_RD, dma8_eop),
210 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)
217 .status = R_SYNC_SERIAL3_STATUS,
218 .ctrl_data = R_SYNC_SERIAL3_CTRL,
219 .output_dma_first = R_DMA_CH4_FIRST,
220 .output_dma_cmd = R_DMA_CH4_CMD,
221 .output_dma_clr_irq = R_DMA_CH4_CLR_INTR,
222 .input_dma_first = R_DMA_CH5_FIRST,
223 .input_dma_cmd = R_DMA_CH5_CMD,
224 .input_dma_descr = R_DMA_CH5_DESCR,
225 .input_dma_clr_irq = R_DMA_CH5_CLR_INTR,
226 .data_out = R_SYNC_SERIAL3_TR_DATA,
227 .data_in = R_SYNC_SERIAL3_REC_DATA,
228 .data_avail_bit =
IO_BITNR(R_IRQ_MASK1_RD, ser3_data),
229 .transmitter_ready_bit =
IO_BITNR(R_IRQ_MASK1_RD, ser3_ready),
230 .input_dma_descr_bit =
IO_BITNR(R_IRQ_MASK2_RD, dma5_descr),
231 .output_dma_bit =
IO_BITNR(R_IRQ_MASK2_RD, dma4_eop),
233 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)
242 static unsigned sync_serial_prescale_shadow;
244 #define NUMBER_OF_PORTS 2
248 .write = sync_serial_write,
249 .read = sync_serial_read,
250 .poll = sync_serial_poll,
251 .unlocked_ioctl = sync_serial_ioctl,
252 .open = sync_serial_open,
253 .release = sync_serial_release,
257 static int __init etrax_sync_serial_init(
void)
262 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
265 "Could not allocate IO group for port %d\n", 0);
269 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1)
271 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
275 "Could not allocate IO group for port %d\n", 1);
281 &sync_serial_fops) < 0) {
282 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1)
285 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
288 printk(
"unable to get major for synchronous serial port\n");
298 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
302 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)
310 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1)
314 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)
325 *R_SYNC_SERIAL_PRESCALE = sync_serial_prescale_shadow = (
331 IO_FIELD(R_SYNC_SERIAL_PRESCALE, frame_rate,
334 IO_STATE(R_SYNC_SERIAL_PRESCALE, warp_mode, normal));
343 static void __init initialize_port(
int portnbr)
355 port->
outp = port->out_buffer;
357 port->
readp = port->flip;
358 port->
writep = port->flip;
369 IO_STATE(R_SYNC_SERIAL1_CTRL, tr_baud, c115k2Hz) |
373 IO_STATE(R_SYNC_SERIAL1_CTRL, f_synctype, normal) |
375 IO_STATE(R_SYNC_SERIAL1_CTRL, f_sync, on) |
376 IO_STATE(R_SYNC_SERIAL1_CTRL, clk_mode, normal) |
380 IO_STATE(R_SYNC_SERIAL1_CTRL, wordsize, size8bit) |
381 IO_STATE(R_SYNC_SERIAL1_CTRL, buf_empty, lmt_8) |
382 IO_STATE(R_SYNC_SERIAL1_CTRL, buf_full, lmt_8) |
385 IO_STATE(R_SYNC_SERIAL1_CTRL, frame_polarity, normal)|
386 IO_STATE(R_SYNC_SERIAL1_CTRL, status_polarity, inverted)|
387 IO_STATE(R_SYNC_SERIAL1_CTRL, clk_driver, normal) |
388 IO_STATE(R_SYNC_SERIAL1_CTRL, frame_driver, normal) |
389 IO_STATE(R_SYNC_SERIAL1_CTRL, status_driver, normal)|
402 static inline int sync_data_avail(
struct sync_port *port)
405 unsigned char *
start;
408 start = (
unsigned char *)port->
readp;
409 end = (
unsigned char *)port->
writep;
421 static inline int sync_data_avail_to_end(
struct sync_port *port)
424 unsigned char *
start;
427 start = (
unsigned char *)port->
readp;
428 end = (
unsigned char *)port->
writep;
459 if (port->
busy == 2) {
465 if (port == &ports[0]) {
468 "synchronous serial 1 dma tr",
471 "sync serial port 1 IRQ");
474 "synchronous serial 1 dma rx",
478 "sync serial port 1 IRQ");
481 "synchronous serial 1 dma tr",
487 "sync serial port 1 "
491 "synchronous serial 1 dma rec",
498 "sync serial port 1 "
505 *R_DMA_CH8_CLR_INTR =
506 IO_STATE(R_DMA_CH8_CLR_INTR, clr_eop,
508 IO_STATE(R_DMA_CH8_CLR_INTR, clr_descr,
510 *R_DMA_CH9_CLR_INTR =
511 IO_STATE(R_DMA_CH9_CLR_INTR, clr_eop,
513 IO_STATE(R_DMA_CH9_CLR_INTR, clr_descr,
518 IO_STATE(R_IRQ_MASK2_SET, dma9_descr,
520 }
else if (port == &ports[1]) {
523 "synchronous serial 3 dma tr",
526 "sync serial port 3 IRQ");
529 "synchronous serial 3 dma rx",
533 "sync serial port 3 IRQ");
536 "synchronous serial 3 dma tr",
542 "sync serial port 3 "
546 "synchronous serial 3 dma rec",
553 "sync serial port 3 "
560 *R_DMA_CH4_CLR_INTR =
561 IO_STATE(R_DMA_CH4_CLR_INTR, clr_eop,
563 IO_STATE(R_DMA_CH4_CLR_INTR, clr_descr,
565 *R_DMA_CH5_CLR_INTR =
566 IO_STATE(R_DMA_CH5_CLR_INTR, clr_eop,
568 IO_STATE(R_DMA_CH5_CLR_INTR, clr_descr,
573 IO_STATE(R_IRQ_MASK2_SET, dma5_descr,
579 #ifdef SYNC_SER_MANUAL
580 if (port == &ports[0]) {
584 "synchronous serial manual irq",
587 "sync serial manual irq");
590 }
else if (port == &ports[1]) {
594 "synchronous serial manual irq",
597 "sync serial manual irq");
603 panic(
"sync_serial: Manual mode not supported.\n");
611 if (mode ==
IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL, mode, master_input) ||
613 mode ==
IO_STATE_VALUE(R_SYNC_SERIAL1_CTRL, mode, master_bidir) ||
634 static int sync_serial_release(
struct inode *inode,
struct file *file)
655 static unsigned int sync_serial_poll(
struct file *file,
poll_table *
wait)
657 int dev =
MINOR(file->f_dentry->d_inode->i_rdev);
658 unsigned int mask = 0;
660 DEBUGPOLL(
static unsigned int prev_mask = 0);
669 if (sync_data_avail(port) >= port->
inbufchunk)
675 mask &
POLLOUT ?
"POLLOUT" :
"",
676 mask &
POLLIN ?
"POLLIN" :
"");
682 static int sync_serial_ioctl_unlocked(
struct file *file,
683 unsigned int cmd,
unsigned long arg)
688 int dev =
MINOR(file->f_dentry->d_inode->i_rdev);
704 port->
outp = port->out_buffer;
705 *R_DMA_CH4_CLR_INTR =
706 IO_STATE(R_DMA_CH4_CLR_INTR, clr_eop,
do) |
707 IO_STATE(R_DMA_CH4_CLR_INTR, clr_descr,
do);
715 port->
outp = port->out_buffer;
716 *R_DMA_CH8_CLR_INTR =
717 IO_STATE(R_DMA_CH8_CLR_INTR, clr_eop,
do) |
718 IO_STATE(R_DMA_CH8_CLR_INTR, clr_descr,
do);
729 SETS(sync_serial_prescale_shadow,
730 R_SYNC_SERIAL_PRESCALE, clk_sel_u3,
733 SETS(sync_serial_prescale_shadow,
734 R_SYNC_SERIAL_PRESCALE, clk_sel_u1,
737 SETF(sync_serial_prescale_shadow,
738 R_SYNC_SERIAL_PRESCALE, prescaler,
740 SETF(sync_serial_prescale_shadow,
741 R_SYNC_SERIAL_PRESCALE, frame_rate,
743 SETF(sync_serial_prescale_shadow,
744 R_SYNC_SERIAL_PRESCALE, word_rate,
750 SETS(sync_serial_prescale_shadow,
751 R_SYNC_SERIAL_PRESCALE, clk_sel_u3,
754 SETS(sync_serial_prescale_shadow,
755 R_SYNC_SERIAL_PRESCALE, clk_sel_u1,
784 f_syncsize, extended);
798 wordsize, size12bit);
801 wordsize, size16bit);
804 wordsize, size24bit);
807 wordsize, size32bit);
842 frame_polarity, normal);
845 frame_polarity, inverted);
849 status_polarity, normal);
852 status_polarity, inverted);
855 if (arg & CLOCK_NORMAL)
858 else if (arg & CLOCK_INVERT)
860 clk_driver, inverted);
862 if (arg & FRAME_NORMAL)
864 frame_driver, normal);
865 else if (arg & FRAME_INVERT)
867 frame_driver, inverted);
869 if (arg & STATUS_NORMAL)
871 status_driver, normal);
872 else if (arg & STATUS_INVERT)
874 status_driver, inverted);
890 frame_polarity, inverted);
897 frame_driver, inverted);
899 clk_driver, inverted);
935 *R_SYNC_SERIAL_PRESCALE = sync_serial_prescale_shadow;
960 static long sync_serial_ioctl(
struct file *file,
961 unsigned int cmd,
unsigned long arg)
966 ret = sync_serial_ioctl_unlocked(file, cmd, arg);
973 static ssize_t sync_serial_write(
struct file *file,
const char *
buf,
974 size_t count, loff_t *ppos)
976 int dev =
MINOR(file->f_dentry->d_inode->i_rdev);
981 unsigned long free_outp;
983 unsigned long out_buffer;
1012 out_buffer = (
unsigned long)port->out_buffer;
1017 if (free_outp >= outp)
1020 c = outp - free_outp;
1025 outp, free_outp, c));
1033 free_outp-out_buffer, c, c1));
1061 *R_IRQ_MASK1_SET = 1 <<
1065 (
unsigned char *
volatile)port->
outp, c);
1084 start_dma(port, port->
outp, c);
1097 static ssize_t sync_serial_read(
struct file *file,
char *buf,
1098 size_t count, loff_t *ppos)
1100 int dev =
MINOR(file->f_dentry->d_inode->i_rdev);
1103 unsigned char *
start;
1105 unsigned long flags;
1114 dev, count, port->
readp - port->flip,
1131 start = (
unsigned char *)port->
readp;
1132 end = (
unsigned char *)port->
writep;
1134 while (start == end && !port->
full) {
1144 start = (
unsigned char *)port->
readp;
1145 end = (
unsigned char *)port->
writep;
1152 else if (end > start)
1153 avail = end -
start;
1157 count = count > avail ? avail :
count;
1164 port->
readp = port->flip;
1171 static void send_word(
struct sync_port *port)
1173 switch (
IO_EXTRACT(R_SYNC_SERIAL1_CTRL, wordsize,
1179 port->
outp = port->out_buffer;
1184 data |= *port->
outp++;
1188 port->
outp = port->out_buffer;
1196 port->
outp = port->out_buffer;
1203 port->
outp = port->out_buffer;
1210 port->
outp = port->out_buffer;
1216 static void start_dma(
struct sync_port *port,
const char *data,
int count)
1219 port->out_descr.hw_len = 0;
1220 port->out_descr.next = 0;
1222 port->out_descr.sw_len =
count;
1224 port->out_descr.
status = 0;
1229 (
unsigned long)data, count));
1232 static void start_dma_in(
struct sync_port *port)
1236 port->
writep = port->flip;
1239 panic(
"Offset too large in sync serial driver\n");
1245 port->in_descr[
i].ctrl =
d_int;
1246 port->in_descr[
i].next =
virt_to_phys(&port->in_descr[i+1]);
1247 port->in_descr[
i].buf =
buf;
1248 port->in_descr[
i].hw_len = 0;
1250 port->in_descr[
i].fifo_len = 0;
1255 port->in_descr[i-1].next =
virt_to_phys(&port->in_descr[0]);
1256 port->in_descr[i-1].ctrl |=
d_eol;
1258 port->
prev_rx_desc = &port->in_descr[NUM_IN_DESCR - 1];
1266 unsigned long ireg = *R_IRQ_MASK2_RD;
1285 IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop,
do) |
1286 IO_STATE(R_DMA_CH0_CLR_INTR, clr_descr,
do);
1288 descr = &port->out_descr;
1296 port->
outp += sentl;
1298 port->
outp = port->out_buffer;
1304 "tx_int DMAWRITE %i %i\n", sentl, c));
1305 start_dma(port, port->
outp, c);
1308 "tx_int DMA stop %i\n", sentl));
1317 static irqreturn_t rx_interrupt(
int irq,
void *dev_id)
1319 unsigned long ireg = *R_IRQ_MASK2_RD;
1338 int first_size = port->flip +
1347 port->
writep = port->flip +
1354 if (port->
writep >= port->flip
1356 port->
writep = port->flip;
1380 #ifdef SYNC_SER_MANUAL
1381 static irqreturn_t manual_interrupt(
int irq,
void *dev_id)
1397 IO_MASK(R_SYNC_SERIAL1_CTRL, wordsize)) {
1398 case IO_STATE(R_SYNC_SERIAL1_CTRL, wordsize, size8bit):
1402 case IO_STATE(R_SYNC_SERIAL1_CTRL, wordsize, size12bit):
1404 int data = *(
unsigned short *)port->
data_in;
1405 *port->
writep = (data & 0x0ff0) >> 4;
1406 *(port->
writep + 1) = data & 0x0f;
1410 case IO_STATE(R_SYNC_SERIAL1_CTRL, wordsize, size16bit):
1415 case IO_STATE(R_SYNC_SERIAL1_CTRL, wordsize, size24bit):
1419 case IO_STATE(R_SYNC_SERIAL1_CTRL, wordsize, size32bit):
1427 port->
writep = port->flip;
1432 if (port->
readp >= port->flip +
1434 port->
readp = port->flip;
1436 if (sync_data_avail(port) >= port->
inbufchunk) {
1450 *R_IRQ_MASK1_CLR = 1 <<