10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/errno.h>
15 #include <linux/sched.h>
18 #include <linux/poll.h>
30 #include <hwregs/intr_vect_defs.h>
31 #include <hwregs/intr_vect.h>
32 #include <hwregs/reg_map.h>
61 #define SYNC_SERIAL_MAJOR 125
65 #define IN_BUFFER_SIZE 12288
66 #define IN_DESCR_SIZE 256
67 #define NBR_IN_DESCR (IN_BUFFER_SIZE/IN_DESCR_SIZE)
69 #define OUT_BUFFER_SIZE 1024*8
70 #define NBR_OUT_DESCR 8
72 #define DEFAULT_FRAME_RATE 0
73 #define DEFAULT_WORD_RATE 7
85 #define DEBUGOUTBUF(x)
106 volatile unsigned char *
volatile readp;
108 volatile unsigned char *
volatile writep;
149 static int etrax_sync_serial_init(
void);
150 static void initialize_port(
int portnbr);
153 static int sync_serial_open(
struct inode *,
struct file*);
154 static int sync_serial_release(
struct inode*,
struct file*);
157 static int sync_serial_ioctl(
struct file *,
158 unsigned int cmd,
unsigned long arg);
160 size_t count, loff_t *ppos);
162 size_t count, loff_t *ppos);
164 #if (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0) && \
165 defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)) || \
166 (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1) && \
167 defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA))
179 #if (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0) && \
180 !defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)) || \
181 (defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1) && \
182 !defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA))
183 #define SYNC_SER_MANUAL
185 #ifdef SYNC_SER_MANUAL
189 #ifdef CONFIG_ETRAXFS
190 #define OUT_DMA_NBR 4
192 #define PINMUX_SSER pinmux_sser0
193 #define SYNCSER_INST regi_sser0
194 #define SYNCSER_INTR_VECT SSER0_INTR_VECT
195 #define OUT_DMA_INST regi_dma4
196 #define IN_DMA_INST regi_dma5
197 #define DMA_OUT_INTR_VECT DMA4_INTR_VECT
198 #define DMA_IN_INTR_VECT DMA5_INTR_VECT
199 #define REQ_DMA_SYNCSER dma_sser0
201 #define OUT_DMA_NBR 6
203 #define PINMUX_SSER pinmux_sser
204 #define SYNCSER_INST regi_sser
205 #define SYNCSER_INTR_VECT SSER_INTR_VECT
206 #define OUT_DMA_INST regi_dma6
207 #define IN_DMA_INST regi_dma7
208 #define DMA_OUT_INTR_VECT DMA6_INTR_VECT
209 #define DMA_IN_INTR_VECT DMA7_INTR_VECT
210 #define REQ_DMA_SYNCSER dma_sser
220 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL0_DMA)
226 #ifdef CONFIG_ETRAXFS
233 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL1_DMA)
242 #define NBR_PORTS ARRAY_SIZE(ports)
246 .write = sync_serial_write,
247 .read = sync_serial_read,
248 .poll = sync_serial_poll,
249 .unlocked_ioctl = sync_serial_ioctl,
250 .open = sync_serial_open,
251 .release = sync_serial_release,
255 static int __init etrax_sync_serial_init(
void)
258 #ifdef CONFIG_ETRAXFS
262 &sync_serial_fops) < 0) {
264 "Unable to get major for synchronous serial port\n");
269 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT0)
272 "Unable to alloc pins for synchronous serial port 0\n");
279 #if defined(CONFIG_ETRAX_SYNCHRONOUS_SERIAL_PORT1)
282 "Unable to alloc pins for synchronous serial port 0\n");
289 #ifdef CONFIG_ETRAXFS
297 static void __init initialize_port(
int portnbr)
308 port->port_nbr = portnbr;
311 port->out_rd_ptr =
port->out_buffer;
312 port->out_buf_count = 0;
321 port->next_rx_desc = &
port->in_descr[0];
323 port->prev_rx_desc->eol = 1;
347 REG_WR(sser,
port->regi_sser, rw_frm_cfg, frm_cfg);
361 REG_WR(sser,
port->regi_sser, rw_tr_cfg, tr_cfg);
367 REG_WR(sser,
port->regi_sser, rw_rec_cfg, rec_cfg);
372 port->out_descr[
i].wait = 0;
373 port->out_descr[
i].intr = 1;
374 port->out_descr[
i].eol = 0;
375 port->out_descr[
i].out_eop = 0;
376 port->out_descr[
i].next =
381 port->out_descr[NBR_OUT_DESCR-1].next =
385 port->active_tr_descr = &
port->out_descr[0];
386 port->prev_tr_descr = &
port->out_descr[NBR_OUT_DESCR-1];
387 port->catch_tr_descr = &
port->out_descr[0];
394 unsigned char *
start;
397 start = (
unsigned char*)port->
readp;
398 end = (
unsigned char*)port->
writep;
411 static inline int sync_data_avail_to_end(
struct sync_port *port)
414 unsigned char *
start;
417 start = (
unsigned char*)port->
readp;
418 end = (
unsigned char*)port->
writep;
433 int dev = iminor(inode);
459 if (port == &ports[0]) {
464 "synchronous serial 0 dma tr",
471 "synchronous serial 1 dma rx",
477 "synchronous serial 0 dma tr",
486 "synchronous serial 0 dma rec",
498 #ifdef CONFIG_ETRAXFS
499 else if (port == &ports[1]) {
504 "synchronous serial 1 dma tr",
511 "synchronous serial 1 dma rx",
518 "synchronous serial 1 dma tr",
528 "synchronous serial 3 dma rec",
554 #ifdef SYNC_SER_MANUAL
555 if (port == &ports[0]) {
559 "synchronous serial manual irq",
561 printk(
"Can't allocate sync serial manual irq");
565 #ifdef CONFIG_ETRAXFS
566 else if (port == &ports[1]) {
570 "synchronous serial manual irq",
579 panic(
"sync_serial: Manual mode not supported.\n");
592 static int sync_serial_release(
struct inode *inode,
struct file *file)
594 int dev = iminor(inode);
597 if (dev < 0 || dev >=
NBR_PORTS || !ports[dev].enabled)
610 static unsigned int sync_serial_poll(
struct file *file,
poll_table *
wait)
612 int dev = iminor(file->
f_path.dentry->d_inode);
613 unsigned int mask = 0;
615 DEBUGPOLL(
static unsigned int prev_mask = 0; );
619 if (!port->started) {
622 REG_RD(sser, port->regi_sser, rw_rec_cfg);
624 rec_cfg.
rec_en = port->input;
625 REG_WR(sser, port->regi_sser, rw_cfg, cfg);
626 REG_WR(sser, port->regi_sser, rw_rec_cfg, rec_cfg);
630 poll_wait(file, &port->out_wait_q, wait);
631 poll_wait(file, &port->in_wait_q, wait);
634 if (port->output && !port->tr_running)
639 port->active_tr_descr != port->catch_tr_descr &&
644 if (port->input && sync_data_avail(port) >= port->inbufchunk)
648 printk(
"sync_serial_poll: mask 0x%08X %s %s\n", mask,
655 static int sync_serial_ioctl(
struct file *file,
656 unsigned int cmd,
unsigned long arg)
660 int dev = iminor(file->
f_path.dentry->d_inode);
668 if (dev < 0 || dev >=
NBR_PORTS || !ports[dev].enabled)
674 spin_lock_irq(&port->
lock);
717 gen_cfg.
clk_div = 29493000 / (150 * 8) - 1;
720 gen_cfg.
clk_div = 29493000 / (300 * 8) - 1;
723 gen_cfg.
clk_div = 29493000 / (600 * 8) - 1;
726 gen_cfg.
clk_div = 29493000 / (1200 * 8) - 1;
729 gen_cfg.
clk_div = 29493000 / (2400 * 8) - 1;
732 gen_cfg.
clk_div = 29493000 / (4800 * 8) - 1;
735 gen_cfg.
clk_div = 29493000 / (9600 * 8) - 1;
738 gen_cfg.
clk_div = 29493000 / (19200 * 8) - 1;
741 gen_cfg.
clk_div = 29493000 / (28800 * 8) - 1;
744 gen_cfg.
clk_div = 29493000 / (57600 * 8) - 1;
747 gen_cfg.
clk_div = 29493000 / (115200 * 8) - 1;
750 gen_cfg.
clk_div = 29493000 / (230400 * 8) - 1;
753 gen_cfg.
clk_div = 29493000 / (460800 * 8) - 1;
756 gen_cfg.
clk_div = 29493000 / (921600 * 8) - 1;
760 gen_cfg.
clk_div = 100000000 / (3125000 * 8) - 1;
811 spin_unlock_irq(&port->
lock);
893 if (arg & CLOCK_NORMAL)
895 else if (arg & CLOCK_INVERT)
898 if (arg & FRAME_NORMAL)
900 else if (arg & FRAME_INVERT)
903 if (arg & STATUS_NORMAL)
905 else if (arg & STATUS_INVERT)
951 WORD_SIZE_16 | WORD_SIZE_24 | WORD_SIZE_32))) {
962 spin_unlock_irq(&port->
lock);
966 static long sync_serial_ioctl(
struct file *file,
967 unsigned int cmd,
unsigned long arg)
972 ret = sync_serial_ioctl_unlocked(file, cmd, arg);
979 static ssize_t sync_serial_write(
struct file *file,
const char *
buf,
980 size_t count, loff_t *ppos)
982 int dev = iminor(file->
f_path.dentry->d_inode);
990 unsigned char *rd_ptr;
991 unsigned char *wr_ptr;
992 unsigned char *buf_stop_ptr;
994 if (dev < 0 || dev >=
NBR_PORTS || !ports[dev].enabled) {
1016 spin_unlock_irqrestore(&port->
lock, flags);
1030 if (wr_ptr >= buf_stop_ptr)
1038 if (wr_ptr + bytes_free > buf_stop_ptr)
1039 bytes_free = buf_stop_ptr - wr_ptr;
1040 trunc_count = (count < bytes_free) ? count : bytes_free;
1046 out_buf_count, trunc_count,
1048 wr_ptr, buf_stop_ptr));
1070 start_dma_out(port, wr_ptr, trunc_count);
1080 spin_unlock_irqrestore(&port->
lock, flags);
1102 static ssize_t sync_serial_read(
struct file * file,
char * buf,
1103 size_t count, loff_t *ppos)
1105 int dev = iminor(file->
f_path.dentry->d_inode);
1108 unsigned char*
start;
1110 unsigned long flags;
1112 if (dev < 0 || dev >=
NBR_PORTS || !ports[dev].enabled)
1138 start = (
unsigned char*)port->
readp;
1139 end = (
unsigned char*)port->
writep;
1140 spin_unlock_irqrestore(&port->
lock, flags);
1141 while ((start == end) && !port->
full)
1152 start = (
unsigned char*)port->
readp;
1153 end = (
unsigned char*)port->
writep;
1154 spin_unlock_irqrestore(&port->
lock, flags);
1160 else if (end > start)
1161 avail = end -
start;
1165 count = count > avail ? avail :
count;
1172 port->
readp = port->flip;
1174 spin_unlock_irqrestore(&port->
lock, flags);
1236 static void start_dma_out(
struct sync_port *port,
1237 const char *data,
int count)
1255 port->out_context.next = 0;
1256 port->out_context.saved_data =
1258 port->out_context.saved_data_buf = port->
prev_tr_descr->buf;
1274 static void start_dma_in(
sync_port *port)
1278 port->
writep = port->flip;
1281 panic(
"Offset too large in sync serial driver\n");
1286 port->in_descr[
i].buf =
buf;
1288 port->in_descr[
i].intr = 1;
1290 port->in_descr[
i].buf =
buf;
1297 port->
prev_rx_desc = &port->in_descr[NBR_IN_DESCR - 1];
1299 port->in_context.saved_data_buf = port->in_descr[0].buf;
1341 "in descr %p (ac: %p)\n",
1365 "traversing descr %p -%d (%d)\n",
1373 if (i >= NBR_OUT_DESCR) {
1375 panic(
"sync_serial: missing eol");
1381 "eol at descr %p -%d (%d)\n",
1399 "tr_int DMA stop %d, set catch @ %p\n",
1404 "empty after eol.\n");
1416 static irqreturn_t rx_interrupt(
int irq,
void *dev_id)
1450 port->
writep = port->flip;
1477 #ifdef SYNC_SER_MANUAL
1478 static irqreturn_t manual_interrupt(
int irq,
void *dev_id)
1525 port->
writep = port->flip;
1531 port->
readp = port->flip;
1533 if (sync_data_avail(port) >= port->
inbufchunk)