2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2006 infineon
17 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
21 // ### TO DO: general issues:
23 // - interrupt handling (direct/indirect)
24 // - pin/mux-handling (just overall concept due to project dependency)
25 // - multiple instances capability
26 // - slave functionality
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/timer.h>
33 #include <linux/interrupt.h>
34 #include <linux/major.h>
35 #include <linux/string.h>
37 #include <linux/proc_fs.h>
38 #include <linux/fcntl.h>
39 #include <linux/ptrace.h>
41 #include <linux/ioport.h>
42 #include <linux/init.h>
43 #include <linux/delay.h>
44 #include <linux/spinlock.h>
45 #include <linux/slab.h>
47 #include <asm/system.h>
50 #include <asm/uaccess.h>
51 #include <asm/bitops.h>
53 #include <linux/types.h>
54 #include <linux/kernel.h>
55 #include <linux/version.h>
57 #include <asm/danube/danube.h>
58 #include <asm/danube/danube_irq.h>
59 #include <asm/danube/ifx_ssc_defines.h>
60 #include <asm/danube/ifx_ssc.h>
62 #ifdef SSC_FRAME_INT_ENABLE
63 #undef SSC_FRAME_INT_ENABLE
72 /* allow the user to set the major device number */
76 * This is the per-channel data structure containing pointers, flags
77 * and variables for the port. This driver supports a maximum of PORT_CNT.
78 * isp is allocated in ifx_ssc_init() based on the chip version.
80 static struct ifx_ssc_port
*isp
;
82 /* prototypes for fops */
83 static ssize_t
ifx_ssc_read (struct file
*, char *, size_t, loff_t
*);
84 static ssize_t
ifx_ssc_write (struct file
*, const char *, size_t, loff_t
*);
85 //static unsigned int ifx_ssc_poll(struct file *, struct poll_table_struct *);
86 int ifx_ssc_ioctl (struct inode
*, struct file
*, unsigned int,
88 int ifx_ssc_open (struct inode
*, struct file
*);
89 int ifx_ssc_close (struct inode
*, struct file
*);
91 /* other forward declarations */
92 static unsigned int ifx_ssc_get_kernel_clk (struct ifx_ssc_port
*info
);
93 #ifdef SSC_FRAME_INT_ENABLE
94 static void ifx_ssc_frm_int (int, void *, struct pt_regs
*);
96 static void tx_int (struct ifx_ssc_port
*);
97 static int ifx_ssc1_read_proc (char *, char **, off_t
, int, int *, void *);
98 static void ifx_gpio_init (void);
99 /************************************************************************
100 * Function declaration
101 ************************************************************************/
103 extern unsigned int danube_get_fpi_hz (void);
104 extern void disable_danube_irq (unsigned int irq_nr
);
105 extern void enable_danube_irq (unsigned int irq_nr
);
106 extern void mask_and_ack_danube_irq (unsigned int irq_nr
);
108 /*****************************************************************/
110 int (*request
) (unsigned int, irq_handler_t handler
,
111 unsigned long, const char *, void *);
112 void (*free
) (unsigned int irq
, void *dev_id
);
113 void (*enable
) (unsigned int irq
);
114 void (*disable
) (unsigned int irq
);
115 void (*clear
) (unsigned int irq
);
118 static ifx_int_wrapper_t ifx_int_wrapper
= {
119 request
:request_irq
, // IM action: enable int
120 free
:free_irq
, // IM action: disable int
121 enable
:enable_danube_irq
,
122 disable
:disable_danube_irq
,
123 clear
:mask_and_ack_danube_irq
,
128 static struct file_operations ifx_ssc_fops
= {
130 read
:ifx_ssc_read
, /* read */
131 write
:ifx_ssc_write
, /* write */
132 // poll: ifx_ssc_poll, /* poll */
133 ioctl
:ifx_ssc_ioctl
, /* ioctl */
134 open
:ifx_ssc_open
, /* open */
135 release
:ifx_ssc_close
, /* release */
138 static inline unsigned int
139 ifx_ssc_get_kernel_clk (struct ifx_ssc_port
*info
)
140 { // ATTENTION: This function assumes that the CLC register is set with the
141 // appropriate value for RMC.
144 rmc
= (READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_CLC
) &
145 IFX_CLC_RUN_DIVIDER_MASK
) >> IFX_CLC_RUN_DIVIDER_OFFSET
;
147 printk ("ifx_ssc_get_kernel_clk rmc==0 \n");
150 return (danube_get_fpi_hz () / rmc
);
154 #ifdef IFX_SSC_INT_USE_BH
156 * This routine is used by the interrupt handler to schedule
157 * processing in the software interrupt portion of the driver
158 * (also known as the "bottom half"). This can be called any
159 * number of times for any channel without harm.
162 ifx_ssc_sched_event (struct ifx_ssc_port
*info
, int event
)
164 info
->event
|= 1 << event
; /* remember what kind of event and who */
165 queue_task (&info
->tqueue
, &tq_cyclades
); /* it belongs to */
166 mark_bh (CYCLADES_BH
); /* then trigger event */
167 } /* ifx_ssc_sched_event */
170 * This routine is used to handle the "bottom half" processing for the
171 * serial driver, known also the "software interrupt" processing.
172 * This processing is done at the kernel interrupt level, after the
173 * cy#/_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
174 * is where time-consuming activities which can not be done in the
175 * interrupt driver proper are done; the interrupt driver schedules
176 * them using ifx_ssc_sched_event(), and they get done here.
178 * This is done through one level of indirection--the task queue.
179 * When a hardware interrupt service routine wants service by the
180 * driver's bottom half, it enqueues the appropriate tq_struct (one
181 * per port) to the tq_cyclades work queue and sets a request flag
182 * via mark_bh for processing that queue. When the time is right,
183 * do_ifx_ssc_bh is called (because of the mark_bh) and it requests
184 * that the work queue be processed.
186 * Although this may seem unwieldy, it gives the system a way to
187 * pass an argument (in this case the pointer to the ifx_ssc_port
188 * structure) to the bottom half of the driver. Previous kernels
189 * had to poll every port to see if that port needed servicing.
194 run_task_queue (&tq_cyclades
);
195 } /* do_ifx_ssc_bh */
198 do_softint (void *private_
)
200 struct ifx_ssc_port
*info
= (struct ifx_ssc_port
*) private_
;
202 if (test_and_clear_bit (Cy_EVENT_HANGUP
, &info
->event
)) {
203 wake_up_interruptible (&info
->open_wait
);
204 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
| ASYNC_CALLOUT_ACTIVE
);
206 if (test_and_clear_bit (Cy_EVENT_OPEN_WAKEUP
, &info
->event
)) {
207 wake_up_interruptible (&info
->open_wait
);
209 if (test_and_clear_bit (Cy_EVENT_DELTA_WAKEUP
, &info
->event
)) {
210 wake_up_interruptible (&info
->delta_msr_wait
);
212 if (test_and_clear_bit (Cy_EVENT_WRITE_WAKEUP
, &info
->event
)) {
213 wake_up_interruptible (&tty
->write_wait
);
216 if (test_and_clear_bit (Cy_EVENT_SHUTDOWN_WAKEUP
, &info
->event
)) {
217 wake_up_interruptible (&info
->shutdown_wait
);
221 #endif /* IFX_SSC_INT_USE_BH */
225 rx_int (struct ifx_ssc_port
*info
)
227 int fifo_fill_lev
, bytes_in_buf
, i
;
228 unsigned long tmp_val
;
229 unsigned long *tmp_ptr
;
230 unsigned int rx_valid_cnt
;
231 /* number of words waiting in the RX FIFO */
232 fifo_fill_lev
= (READ_PERIPHERAL_REGISTER (info
->mapbase
+
234 IFX_SSC_FSTAT_RECEIVED_WORDS_MASK
) >>
235 IFX_SSC_FSTAT_RECEIVED_WORDS_OFFSET
;
236 // Note: There are always 32 bits in a fifo-entry except for the last
237 // word of a contigous transfer block and except for not in rx-only
238 // mode and CON.ENBV set. But for this case it should be a convention
239 // in software which helps:
240 // In tx or rx/tx mode all transfers from the buffer to the FIFO are
241 // 32-bit wide, except for the last three bytes, which could be a
242 // combination of 16- and 8-bit access.
243 // => The whole block is received as 32-bit words as a contigous stream,
244 // even if there was a gap in tx which has the fifo run out of data!
245 // Just the last fifo entry *may* be partially filled (0, 1, 2 or 3 bytes)!
247 /* free space in the RX buffer */
248 bytes_in_buf
= info
->rxbuf_end
- info
->rxbuf_ptr
;
249 // transfer with 32 bits per entry
250 while ((bytes_in_buf
>= 4) && (fifo_fill_lev
> 0)) {
251 tmp_ptr
= (unsigned long *) info
->rxbuf_ptr
;
253 READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_RB
);
254 info
->rxbuf_ptr
+= 4;
255 info
->stats
.rxBytes
+= 4;
258 } // while ((bytes_in_buf >= 4) && (fifo_fill_lev > 0))
259 // now do the rest as mentioned in STATE.RXBV
260 while ((bytes_in_buf
> 0) && (fifo_fill_lev
> 0)) {
262 (READ_PERIPHERAL_REGISTER
264 IFX_SSC_STATE
) & IFX_SSC_STATE_RX_BYTE_VALID_MASK
)
265 >> IFX_SSC_STATE_RX_BYTE_VALID_OFFSET
;
266 if (rx_valid_cnt
== 0)
268 if (rx_valid_cnt
> bytes_in_buf
) {
269 // ### TO DO: warning message: not block aligned data, other data
270 // in this entry will be lost
271 rx_valid_cnt
= bytes_in_buf
;
274 READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_RB
);
276 for (i
= 0; i
< rx_valid_cnt
; i
++) {
278 (tmp_val
>> (8 * (rx_valid_cnt
- i
- 1))) &
281 *info->rxbuf_ptr = tmp_val & 0xff;
288 info
->stats
.rxBytes
+= rx_valid_cnt
;
289 } // while ((bytes_in_buf > 0) && (fifo_fill_lev > 0))
291 // check if transfer is complete
292 if (info
->rxbuf_ptr
>= info
->rxbuf_end
) {
293 ifx_int_wrapper
.disable (info
->rxirq
);
294 /* wakeup any processes waiting in read() */
295 wake_up_interruptible (&info
->rwait
);
297 //wake_up_interruptible(&info->pwait);
299 else if ((info
->opts
.modeRxTx
== IFX_SSC_MODE_RX
) &&
300 (READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_RXCNT
) ==
302 // if buffer not filled completely and rx request done initiate new transfer
304 if (info->rxbuf_end - info->rxbuf_ptr < 65536)
306 if (info
->rxbuf_end
- info
->rxbuf_ptr
<
307 IFX_SSC_RXREQ_BLOCK_SIZE
)
308 WRITE_PERIPHERAL_REGISTER ((info
->rxbuf_end
-
311 IFX_SSC_RXREQ_RXCOUNT_OFFSET
,
315 WRITE_PERIPHERAL_REGISTER (IFX_SSC_RXREQ_BLOCK_SIZE
<<
316 IFX_SSC_RXREQ_RXCOUNT_OFFSET
,
323 tx_int (struct ifx_ssc_port
*info
)
326 int fifo_space
, fill
, i
;
327 fifo_space
= ((READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_ID
) &
328 IFX_SSC_PERID_TXFS_MASK
) >> IFX_SSC_PERID_TXFS_OFFSET
)
330 ((READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_FSTAT
) &
331 IFX_SSC_FSTAT_TRANSMIT_WORDS_MASK
) >>
332 IFX_SSC_FSTAT_TRANSMIT_WORDS_OFFSET
);
337 fill
= info
->txbuf_end
- info
->txbuf_ptr
;
339 if (fill
> fifo_space
* 4)
340 fill
= fifo_space
* 4;
342 for (i
= 0; i
< fill
/ 4; i
++) {
343 // at first 32 bit access
344 WRITE_PERIPHERAL_REGISTER (*(UINT32
*) info
->txbuf_ptr
,
345 info
->mapbase
+ IFX_SSC_TB
);
346 info
->txbuf_ptr
+= 4;
349 fifo_space
-= fill
/ 4;
350 info
->stats
.txBytes
+= fill
& ~0x3;
352 if ((fifo_space
> 0) & (fill
> 1)) {
353 // trailing 16 bit access
354 WRITE_PERIPHERAL_REGISTER_16 (*(UINT16
*) info
->txbuf_ptr
,
355 info
->mapbase
+ IFX_SSC_TB
);
356 info
->txbuf_ptr
+= 2;
357 info
->stats
.txBytes
+= 2;
359 /* added by bingtao */
362 if ((fifo_space
> 0) & (fill
> 0)) {
363 // trailing 8 bit access
364 WRITE_PERIPHERAL_REGISTER_8 (*(UINT8
*) info
->txbuf_ptr
,
365 info
->mapbase
+ IFX_SSC_TB
);
367 info
->stats
.txBytes
++;
373 // check if transmission complete
374 if (info
->txbuf_ptr
>= info
->txbuf_end
) {
375 ifx_int_wrapper
.disable (info
->txirq
);
378 /* wake up any process waiting in poll() */
379 //wake_up_interruptible(&info->pwait);
385 ifx_ssc_rx_int (int irq
, void *dev_id
)
387 struct ifx_ssc_port
*info
= (struct ifx_ssc_port
*) dev_id
;
394 ifx_ssc_tx_int (int irq
, void *dev_id
)
396 struct ifx_ssc_port
*info
= (struct ifx_ssc_port
*) dev_id
;
403 ifx_ssc_err_int (int irq
, void *dev_id
)
405 struct ifx_ssc_port
*info
= (struct ifx_ssc_port
*) dev_id
;
407 unsigned int write_back
= 0;
410 local_irq_save (flags
);
411 state
= READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_STATE
);
413 if ((state
& IFX_SSC_STATE_RX_UFL
) != 0) {
414 info
->stats
.rxUnErr
++;
415 write_back
|= IFX_SSC_WHBSTATE_CLR_RX_UFL_ERROR
;
417 if ((state
& IFX_SSC_STATE_RX_OFL
) != 0) {
418 info
->stats
.rxOvErr
++;
419 write_back
|= IFX_SSC_WHBSTATE_CLR_RX_OFL_ERROR
;
421 if ((state
& IFX_SSC_STATE_TX_OFL
) != 0) {
422 info
->stats
.txOvErr
++;
423 write_back
|= IFX_SSC_WHBSTATE_CLR_TX_OFL_ERROR
;
425 if ((state
& IFX_SSC_STATE_TX_UFL
) != 0) {
426 info
->stats
.txUnErr
++;
427 write_back
|= IFX_SSC_WHBSTATE_CLR_TX_UFL_ERROR
;
429 // if ((state & IFX_SSC_STATE_ABORT_ERR) != 0) {
430 // info->stats.abortErr++;
431 // write_back |= IFX_SSC_WHBSTATE_CLR_ABORT_ERROR;
433 if ((state
& IFX_SSC_STATE_MODE_ERR
) != 0) {
434 info
->stats
.modeErr
++;
435 write_back
|= IFX_SSC_WHBSTATE_CLR_MODE_ERROR
;
439 WRITE_PERIPHERAL_REGISTER (write_back
,
440 info
->mapbase
+ IFX_SSC_WHBSTATE
);
442 local_irq_restore (flags
);
447 #ifdef SSC_FRAME_INT_ENABLE
449 ifx_ssc_frm_int (int irq
, void *dev_id
, struct pt_regs
*regs
)
451 // ### TO DO: wake up framing wait-queue in conjunction with batch execution
456 ifx_ssc_abort (struct ifx_ssc_port
*info
)
461 local_irq_save (flags
);
464 ifx_int_wrapper
.disable (info
->rxirq
);
465 ifx_int_wrapper
.disable (info
->txirq
);
466 ifx_int_wrapper
.disable (info
->errirq
);
468 ifx_int_wrapper.disable(info->frmirq);
470 local_irq_restore (flags
);
472 // disable SSC (also aborts a receive request!)
473 // ### TO DO: Perhaps it's better to abort after the receiption of a
474 // complete word. The disable cuts the transmission immediatly and
475 // releases the chip selects. This could result in unpredictable
476 // behavior of connected external devices!
477 enabled
= (READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_STATE
)
478 & IFX_SSC_STATE_IS_ENABLED
) != 0;
479 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ENABLE
,
480 info
->mapbase
+ IFX_SSC_WHBSTATE
);
483 WRITE_PERIPHERAL_REGISTER (IFX_SSC_XFCON_FIFO_FLUSH
,
484 info
->mapbase
+ IFX_SSC_TXFCON
);
485 WRITE_PERIPHERAL_REGISTER (IFX_SSC_XFCON_FIFO_FLUSH
,
486 info
->mapbase
+ IFX_SSC_RXFCON
);
489 if (info
->txbuf
!= NULL
) {
494 // wakeup read process
495 if (info
->rxbuf
!= NULL
)
496 wake_up_interruptible (&info
->rwait
);
498 // clear pending int's
499 ifx_int_wrapper
.clear (info
->rxirq
);
500 ifx_int_wrapper
.clear (info
->txirq
);
501 ifx_int_wrapper
.clear (info
->errirq
);
503 ifx_int_wrapper.clear(info->frmirq);
507 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ALL_ERROR
,
508 info
->mapbase
+ IFX_SSC_WHBSTATE
);
510 //printk("IFX SSC%d: Transmission aborted\n", info->port_nr);
513 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_SET_ENABLE
,
514 info
->mapbase
+ IFX_SSC_WHBSTATE
);
519 * This routine is called whenever a port is opened. It enforces
520 * exclusive opening of a port and enables interrupts, etc.
523 ifx_ssc_open (struct inode
*inode
, struct file
*filp
)
525 struct ifx_ssc_port
*info
;
529 if ((inode
== (struct inode
*) 0) || (inode
== (struct inode
*) 1)) {
534 line
= MINOR (filp
->f_dentry
->d_inode
->i_rdev
);
535 filp
->f_op
= &ifx_ssc_fops
;
538 /* don't open more minor devices than we can support */
539 if (line
< 0 || line
>= PORT_CNT
)
545 if (info
->port_is_open
!= 0)
547 info
->port_is_open
++;
549 ifx_int_wrapper
.disable (info
->rxirq
);
550 ifx_int_wrapper
.disable (info
->txirq
);
551 ifx_int_wrapper
.disable (info
->errirq
);
553 ifx_int_wrapper.disable(info->frmirq);
556 /* Flush and enable TX/RX FIFO */
557 WRITE_PERIPHERAL_REGISTER ((IFX_SSC_DEF_TXFIFO_FL
<<
558 IFX_SSC_XFCON_ITL_OFFSET
) |
559 IFX_SSC_XFCON_FIFO_FLUSH
|
560 IFX_SSC_XFCON_FIFO_ENABLE
,
561 info
->mapbase
+ IFX_SSC_TXFCON
);
562 WRITE_PERIPHERAL_REGISTER ((IFX_SSC_DEF_RXFIFO_FL
<<
563 IFX_SSC_XFCON_ITL_OFFSET
) |
564 IFX_SSC_XFCON_FIFO_FLUSH
|
565 IFX_SSC_XFCON_FIFO_ENABLE
,
566 info
->mapbase
+ IFX_SSC_RXFCON
);
568 /* logically flush the software FIFOs */
572 /* clear all error bits */
573 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ALL_ERROR
,
574 info
->mapbase
+ IFX_SSC_WHBSTATE
);
576 // clear pending interrupts
577 ifx_int_wrapper
.clear (info
->rxirq
);
578 ifx_int_wrapper
.clear (info
->txirq
);
579 ifx_int_wrapper
.clear (info
->errirq
);
581 ifx_int_wrapper.clear(info->frmirq);
585 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_SET_ENABLE
,
586 info
->mapbase
+ IFX_SSC_WHBSTATE
);
591 EXPORT_SYMBOL (ifx_ssc_open
);
594 * This routine is called when a particular device is closed.
597 ifx_ssc_close (struct inode
*inode
, struct file
*filp
)
599 struct ifx_ssc_port
*info
;
602 if ((inode
== (struct inode
*) 0) || (inode
== (struct inode
*) 1))
605 idx
= MINOR (filp
->f_dentry
->d_inode
->i_rdev
);
607 if (idx
< 0 || idx
>= PORT_CNT
)
615 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ENABLE
,
616 info
->mapbase
+ IFX_SSC_WHBSTATE
);
618 // call abort function to disable int's, flush fifos...
619 ifx_ssc_abort (info
);
621 info
->port_is_open
--;
624 } /* ifx_ssc_close */
626 EXPORT_SYMBOL (ifx_ssc_close
);
628 /* added by bingtao */
629 /* helper routine to handle reads from the kernel or user-space */
630 /* info->rxbuf : never kfree and contains valid data */
631 /* should be points to NULL after copying data !!! */
633 ifx_ssc_read_helper_poll (struct ifx_ssc_port
*info
, char *buf
, size_t len
,
639 if (info
->opts
.modeRxTx
== IFX_SSC_MODE_TX
)
641 local_irq_save (flags
);
642 info
->rxbuf_ptr
= info
->rxbuf
;
643 info
->rxbuf_end
= info
->rxbuf
+ len
;
644 local_irq_restore (flags
);
645 /* Vinetic driver always works in IFX_SSC_MODE_RXTX */
646 /* TXRX in poll mode */
647 while (info
->rxbuf_ptr
< info
->rxbuf_end
) {
648 /* This is the key point, if you don't check this condition
649 kfree (NULL) will happen
650 because tx only need write into FIFO, it's much fast than rx
651 So when rx still waiting , tx already finish and release buf
653 if (info
->txbuf_ptr
< info
->txbuf_end
) {
660 ret_val
= info
->rxbuf_ptr
- info
->rxbuf
;
662 } // ifx_ssc_read_helper_poll
664 /* helper routine to handle reads from the kernel or user-space */
665 /* info->rx_buf : never kfree and contains valid data */
666 /* should be points to NULL after copying data !!! */
668 ifx_ssc_read_helper (struct ifx_ssc_port
*info
, char *buf
, size_t len
,
673 DECLARE_WAITQUEUE (wait
, current
);
675 if (info
->opts
.modeRxTx
== IFX_SSC_MODE_TX
)
677 local_irq_save (flags
);
678 info
->rxbuf_ptr
= info
->rxbuf
;
679 info
->rxbuf_end
= info
->rxbuf
+ len
;
680 if (info
->opts
.modeRxTx
== IFX_SSC_MODE_RXTX
) {
681 if ((info
->txbuf
== NULL
) ||
682 (info
->txbuf
!= info
->txbuf_ptr
) ||
683 (info
->txbuf_end
!= len
+ info
->txbuf
)) {
684 local_irq_restore (flags
);
685 printk ("IFX SSC - %s: write must be called before calling " "read in combined RX/TX!\n", __FUNCTION__
);
688 local_irq_restore (flags
);
689 /* should enable tx, right? */
691 if (info
->txbuf_ptr
< info
->txbuf_end
) {
692 ifx_int_wrapper
.enable (info
->txirq
);
695 ifx_int_wrapper
.enable (info
->rxirq
);
698 local_irq_restore (flags
);
699 if (READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_RXCNT
) &
700 IFX_SSC_RXCNT_TODO_MASK
)
702 ifx_int_wrapper
.enable (info
->rxirq
);
703 // rx request limited to ' bytes
707 if (len
< IFX_SSC_RXREQ_BLOCK_SIZE
)
708 WRITE_PERIPHERAL_REGISTER (len
<<
709 IFX_SSC_RXREQ_RXCOUNT_OFFSET
,
713 WRITE_PERIPHERAL_REGISTER (IFX_SSC_RXREQ_BLOCK_SIZE
<<
714 IFX_SSC_RXREQ_RXCOUNT_OFFSET
,
719 __add_wait_queue (&info
->rwait
, &wait
);
720 set_current_state (TASK_INTERRUPTIBLE
);
721 // wakeup done in rx_int
724 local_irq_save (flags
);
725 if (info
->rxbuf_ptr
>= info
->rxbuf_end
)
727 local_irq_restore (flags
);
729 // if (filp->f_flags & O_NONBLOCK)
734 if (signal_pending (current
)) {
735 ret_val
= -ERESTARTSYS
;
741 ret_val
= info
->rxbuf_ptr
- info
->rxbuf
; // should be equal to len
742 local_irq_restore (flags
);
745 current
->state
= TASK_RUNNING
;
746 __remove_wait_queue (&info
->rwait
, &wait
);
748 } // ifx_ssc_read_helper
751 /* helper routine to handle writes to the kernel or user-space */
752 /* info->txbuf has two cases:
753 * 1) return value < 0 (-EFAULT), not touched at all
754 * 2) kfree and points to NULL in interrupt routine (but maybe later )
757 ifx_ssc_write_helper (struct ifx_ssc_port
*info
, const char *buf
,
758 size_t len
, int from_kernel
)
760 // check if in tx or tx/rx mode
761 if (info
->opts
.modeRxTx
== IFX_SSC_MODE_RX
)
764 info
->txbuf_ptr
= info
->txbuf
;
765 info
->txbuf_end
= len
+ info
->txbuf
;
766 /* start the transmission (not in rx/tx, see read helper) */
767 if (info
->opts
.modeRxTx
== IFX_SSC_MODE_TX
) {
769 if (info
->txbuf_ptr
< info
->txbuf_end
) {
770 ifx_int_wrapper
.enable (info
->txirq
);
773 //local_irq_restore(flags);
778 * kernel interfaces for read and write.
779 * The caller must set port to: n for SSC<m> with n=m-1 (e.g. n=0 for SSC1)
782 ifx_ssc_kread (int port
, char *kbuf
, size_t len
)
784 struct ifx_ssc_port
*info
;
787 if (port
< 0 || port
>= PORT_CNT
)
795 // check if reception in progress
796 if (info
->rxbuf
!= NULL
) {
797 printk ("SSC device busy\n");
802 if (info
->rxbuf
== NULL
) {
803 printk ("SSC device error\n");
807 /* changed by bingtao */
808 /* change by TaiCheng */
811 ret_val
= ifx_ssc_read_helper (info
, kbuf
, len
, 1);
814 ret_val
= ifx_ssc_read_helper_poll (info
, kbuf
, len
, 1);
818 // ### TO DO: perhaps warn if ret_val != len
819 ifx_int_wrapper
.disable (info
->rxirq
);
824 EXPORT_SYMBOL (ifx_ssc_kread
);
827 ifx_ssc_kwrite (int port
, const char *kbuf
, size_t len
)
829 struct ifx_ssc_port
*info
;
832 if (port
< 0 || port
>= PORT_CNT
)
840 // check if transmission in progress
841 if (info
->txbuf
!= NULL
)
843 info
->txbuf
= (char *) kbuf
;
845 ret_val
= ifx_ssc_write_helper (info
, info
->txbuf
, len
, 1);
852 EXPORT_SYMBOL (ifx_ssc_kwrite
);
855 * user interfaces to read and write
858 ifx_ssc_read (struct file
*filp
, char *ubuf
, size_t len
, loff_t
* off
)
862 struct ifx_ssc_port
*info
;
868 idx
= MINOR (filp
->f_dentry
->d_inode
->i_rdev
);
871 // check if reception in progress
872 if (info
->rxbuf
!= NULL
)
875 info
->rxbuf
= kmalloc (len
+ 3, GFP_KERNEL
);
876 if (info
->rxbuf
== NULL
)
879 ret_val
= ifx_ssc_read_helper (info
, info
->rxbuf
, len
, 0);
880 // ### TO DO: perhaps warn if ret_val != len
881 if (copy_to_user ((void *) ubuf
, info
->rxbuf
, ret_val
) != 0)
884 ifx_int_wrapper
.disable (info
->rxirq
);
892 * As many bytes as we have free space for are copied from the user
893 * into txbuf and the actual byte count is returned. The transmission is
894 * always kicked off by calling the appropriate TX routine.
897 ifx_ssc_write (struct file
*filp
, const char *ubuf
, size_t len
, loff_t
* off
)
900 struct ifx_ssc_port
*info
;
906 idx
= MINOR (filp
->f_dentry
->d_inode
->i_rdev
);
909 // check if transmission in progress
910 if (info
->txbuf
!= NULL
)
913 info
->txbuf
= kmalloc (len
+ 3, GFP_KERNEL
);
914 if (info
->txbuf
== NULL
)
917 ret_val
= copy_from_user (info
->txbuf
, ubuf
, len
);
919 ret_val
= ifx_ssc_write_helper (info
, info
->txbuf
, len
, 0);
923 kfree (info
->txbuf
); // otherwise will be done in ISR
927 } /* ifx_ssc_write */
930 * ------------------------------------------------------------
931 * ifx_ssc_ioctl() and friends
932 * ------------------------------------------------------------
935 /*-----------------------------------------------------------------------------
936 FUNC-NAME : ifx_ssc_frm_status_get
937 LONG-NAME : framing status get
938 PURPOSE : Get the actual status of the framing.
940 PARAMETER : *info pointer to the port-specific structure ifx_ssc_port.
942 RESULT : pointer to a structure ifx_ssc_frm_status which holds busy and
945 REMARKS : Returns a register value independent of framing is enabled or
946 not! Changes structure inside of info, so the return value isn't
947 needed at all, but could be used for simple access.
948 -----------------------------------------------------------------------------*/
949 static struct ifx_ssc_frm_status
*
950 ifx_ssc_frm_status_get (struct ifx_ssc_port
*info
)
954 tmp
= READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_SFSTAT
);
955 info
->frm_status
.DataBusy
= (tmp
& IFX_SSC_SFSTAT_IN_DATA
) > 0;
956 info
->frm_status
.PauseBusy
= (tmp
& IFX_SSC_SFSTAT_IN_PAUSE
) > 0;
957 info
->frm_status
.DataCount
= (tmp
& IFX_SSC_SFSTAT_DATA_COUNT_MASK
)
958 >> IFX_SSC_SFSTAT_DATA_COUNT_OFFSET
;
959 info
->frm_status
.PauseCount
= (tmp
& IFX_SSC_SFSTAT_PAUSE_COUNT_MASK
)
960 >> IFX_SSC_SFSTAT_PAUSE_COUNT_OFFSET
;
961 tmp
= READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_SFCON
);
962 info
->frm_status
.EnIntAfterData
=
963 (tmp
& IFX_SSC_SFCON_FIR_ENABLE_BEFORE_PAUSE
) > 0;
964 info
->frm_status
.EnIntAfterPause
=
965 (tmp
& IFX_SSC_SFCON_FIR_ENABLE_AFTER_PAUSE
) > 0;
966 return (&info
->frm_status
);
967 } // ifx_ssc_frm_status_get
969 /*-----------------------------------------------------------------------------
970 FUNC-NAME : ifx_ssc_frm_control_get
971 LONG-NAME : framing control get
972 PURPOSE : Get the actual control values of the framing.
974 PARAMETER : *info pointer to the port-specific structure ifx_ssc_port.
976 RESULT : pointer to a structure ifx_ssc_frm_opts which holds control bits
977 and count reload values.
979 REMARKS : Changes structure inside of info, so the return value isn't
980 needed at all, but could be used for simple access.
981 -----------------------------------------------------------------------------*/
982 static struct ifx_ssc_frm_opts
*
983 ifx_ssc_frm_control_get (struct ifx_ssc_port
*info
)
987 tmp
= READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_SFCON
);
988 info
->frm_opts
.FrameEnable
= (tmp
& IFX_SSC_SFCON_SF_ENABLE
) > 0;
989 info
->frm_opts
.DataLength
= (tmp
& IFX_SSC_SFCON_DATA_LENGTH_MASK
)
990 >> IFX_SSC_SFCON_DATA_LENGTH_OFFSET
;
991 info
->frm_opts
.PauseLength
= (tmp
& IFX_SSC_SFCON_PAUSE_LENGTH_MASK
)
992 >> IFX_SSC_SFCON_PAUSE_LENGTH_OFFSET
;
993 info
->frm_opts
.IdleData
= (tmp
& IFX_SSC_SFCON_PAUSE_DATA_MASK
)
994 >> IFX_SSC_SFCON_PAUSE_DATA_OFFSET
;
995 info
->frm_opts
.IdleClock
= (tmp
& IFX_SSC_SFCON_PAUSE_CLOCK_MASK
)
996 >> IFX_SSC_SFCON_PAUSE_CLOCK_OFFSET
;
997 info
->frm_opts
.StopAfterPause
=
998 (tmp
& IFX_SSC_SFCON_STOP_AFTER_PAUSE
) > 0;
999 return (&info
->frm_opts
);
1000 } // ifx_ssc_frm_control_get
1002 /*-----------------------------------------------------------------------------
1003 FUNC-NAME : ifx_ssc_frm_control_set
1004 LONG-NAME : framing control set
1005 PURPOSE : Set the actual control values of the framing.
1007 PARAMETER : *info pointer to the port-specific structure ifx_ssc_port.
1009 RESULT : pointer to a structure ifx_ssc_frm_opts which holds control bits
1010 and count reload values.
1013 -----------------------------------------------------------------------------*/
1015 ifx_ssc_frm_control_set (struct ifx_ssc_port
*info
)
1020 if ((info
->frm_opts
.DataLength
> IFX_SSC_SFCON_DATA_LENGTH_MAX
) ||
1021 (info
->frm_opts
.DataLength
< 1) ||
1022 (info
->frm_opts
.PauseLength
> IFX_SSC_SFCON_PAUSE_LENGTH_MAX
) ||
1023 (info
->frm_opts
.PauseLength
< 1) ||
1024 ((info
->frm_opts
.IdleData
& ~(IFX_SSC_SFCON_PAUSE_DATA_MASK
>>
1025 IFX_SSC_SFCON_PAUSE_DATA_OFFSET
)) !=
1029 IdleClock
& ~(IFX_SSC_SFCON_PAUSE_CLOCK_MASK
>>
1030 IFX_SSC_SFCON_PAUSE_CLOCK_OFFSET
)) != 0))
1033 // read interrupt bits (they're not changed here)
1034 tmp
= READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_SFCON
) &
1035 (IFX_SSC_SFCON_FIR_ENABLE_BEFORE_PAUSE
|
1036 IFX_SSC_SFCON_FIR_ENABLE_AFTER_PAUSE
);
1038 // set all values with respect to it's bit position (for data and pause
1040 tmp
= (info
->frm_opts
.DataLength
-
1041 1) << IFX_SSC_SFCON_DATA_LENGTH_OFFSET
;
1042 tmp
|= (info
->frm_opts
.PauseLength
-
1043 1) << IFX_SSC_SFCON_PAUSE_LENGTH_OFFSET
;
1044 tmp
|= info
->frm_opts
.IdleData
<< IFX_SSC_SFCON_PAUSE_DATA_OFFSET
;
1045 tmp
|= info
->frm_opts
.IdleClock
<< IFX_SSC_SFCON_PAUSE_CLOCK_OFFSET
;
1046 tmp
|= info
->frm_opts
.FrameEnable
* IFX_SSC_SFCON_SF_ENABLE
;
1047 tmp
|= info
->frm_opts
.StopAfterPause
* IFX_SSC_SFCON_STOP_AFTER_PAUSE
;
1049 WRITE_PERIPHERAL_REGISTER (tmp
, info
->mapbase
+ IFX_SSC_SFCON
);
1052 } // ifx_ssc_frm_control_set
1054 /*-----------------------------------------------------------------------------
1055 FUNC-NAME : ifx_ssc_rxtx_mode_set
1056 LONG-NAME : rxtx mode set
1057 PURPOSE : Set the transmission mode.
1059 PARAMETER : *info pointer to the port-specific structure ifx_ssc_port.
1061 RESULT : Returns error code
1063 REMARKS : Assumes that SSC not used (SSC disabled, device not opened yet
1065 -----------------------------------------------------------------------------*/
1067 ifx_ssc_rxtx_mode_set (struct ifx_ssc_port
*info
, unsigned int val
)
1072 if (!(info
) || (val
& ~(IFX_SSC_MODE_MASK
)))
1074 /*check BUSY and RXCNT */
1075 if (READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_STATE
) &
1077 || READ_PERIPHERAL_REGISTER (info
->mapbase
+
1079 IFX_SSC_RXCNT_TODO_MASK
)
1082 tmp
= (READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_CON
) &
1083 ~(IFX_SSC_CON_RX_OFF
| IFX_SSC_CON_TX_OFF
)) | (val
);
1084 WRITE_PERIPHERAL_REGISTER (tmp
, info
->mapbase
+ IFX_SSC_CON
);
1085 info
->opts
.modeRxTx
= val
;
1087 printk(KERN_DEBUG "IFX SSC%d: Setting mode to %s%s\n",
1089 ((val & IFX_SSC_CON_RX_OFF) == 0) ? "rx ":"",
1090 ((val & IFX_SSC_CON_TX_OFF) == 0) ? "tx":"");
1093 } // ifx_ssc_rxtx_mode_set
1096 ifx_gpio_init (void)
1098 /* TODO: P0.9 SPI_CS4, P0.10 SPI_CS5, P 0.11 SPI_CS6, because of ASC0 */
1099 /* p0.15 SPI_CS1(EEPROM), P0.13 SPI_CS3, */
1100 /* Set p0.15 to alternative 01, others to 00 (In/OUT) */
1101 *(DANUBE_GPIO_P0_DIR
) = (*DANUBE_GPIO_P0_DIR
) | (0xA000);
1102 *(DANUBE_GPIO_P0_ALTSEL0
) =
1103 (((*DANUBE_GPIO_P0_ALTSEL0
) | (0x8000)) & (~(0x2000)));
1104 *(DANUBE_GPIO_P0_ALTSEL1
) =
1105 (((*DANUBE_GPIO_P0_ALTSEL1
) & (~0x8000)) & (~(0x2000)));
1106 *(DANUBE_GPIO_P0_OD
) = (*DANUBE_GPIO_P0_OD
) | 0xA000;
1108 /* p1.6 SPI_CS2(SFLASH), p1.0 SPI_DIN, p1.1 SPI_DOUT, p1.2 SPI_CLK */
1109 *(DANUBE_GPIO_P1_DIR
) = ((*DANUBE_GPIO_P1_DIR
) | (0x46)) & (~1);
1110 *(DANUBE_GPIO_P1_ALTSEL0
) = ((*DANUBE_GPIO_P1_ALTSEL0
) | (0x47));
1111 *(DANUBE_GPIO_P1_ALTSEL1
) = (*DANUBE_GPIO_P1_ALTSEL1
) & (~0x47);
1112 *(DANUBE_GPIO_P1_OD
) = (*DANUBE_GPIO_P1_OD
) | 0x0046;
1115 /*TODO: CS4 CS5 CS6 */
1116 *DANUBE_GPIO_P0_OUT
= ((*DANUBE_GPIO_P0_OUT
) | 0x2000);
1121 * This routine intializes the SSC appropriately depending
1122 * on slave/master and full-/half-duplex mode.
1123 * It assumes that the SSC is disabled and the fifo's and buffers
1124 * are flushes later on.
1127 ifx_ssc_sethwopts (struct ifx_ssc_port
*info
)
1129 unsigned long flags
, bits
;
1130 struct ifx_ssc_hwopts
*opts
= &info
->opts
;
1133 if ((opts
->dataWidth
< IFX_SSC_MIN_DATA_WIDTH
) ||
1134 (opts
->dataWidth
> IFX_SSC_MAX_DATA_WIDTH
)) {
1135 printk ("%s: sanity check failed\n", __FUNCTION__
);
1138 bits
= (opts
->dataWidth
- 1) << IFX_SSC_CON_DATA_WIDTH_OFFSET
;
1139 bits
|= IFX_SSC_CON_ENABLE_BYTE_VALID
;
1140 // if (opts->abortErrDetect)
1141 // bits |= IFX_SSC_CON_ABORT_ERR_CHECK;
1142 if (opts
->rxOvErrDetect
)
1143 bits
|= IFX_SSC_CON_RX_OFL_CHECK
;
1144 if (opts
->rxUndErrDetect
)
1145 bits
|= IFX_SSC_CON_RX_UFL_CHECK
;
1146 if (opts
->txOvErrDetect
)
1147 bits
|= IFX_SSC_CON_TX_OFL_CHECK
;
1148 if (opts
->txUndErrDetect
)
1149 bits
|= IFX_SSC_CON_TX_UFL_CHECK
;
1151 bits
|= IFX_SSC_CON_LOOPBACK_MODE
;
1153 bits
|= IFX_SSC_CON_ECHO_MODE_ON
;
1154 if (opts
->headingControl
)
1155 bits
|= IFX_SSC_CON_MSB_FIRST
;
1156 if (opts
->clockPhase
)
1157 bits
|= IFX_SSC_CON_LATCH_THEN_SHIFT
;
1158 if (opts
->clockPolarity
)
1159 bits
|= IFX_SSC_CON_CLOCK_FALL
;
1160 switch (opts
->modeRxTx
) {
1161 case IFX_SSC_MODE_TX
:
1162 bits
|= IFX_SSC_CON_RX_OFF
;
1164 case IFX_SSC_MODE_RX
:
1165 bits
|= IFX_SSC_CON_TX_OFF
;
1167 } // switch (opts->modeRxT)
1168 local_irq_save (flags
);
1169 WRITE_PERIPHERAL_REGISTER (bits
, info
->mapbase
+ IFX_SSC_CON
);
1170 WRITE_PERIPHERAL_REGISTER ((info
->opts
.
1171 gpoCs
<< IFX_SSC_GPOCON_ISCSB0_POS
) |
1173 gpoInv
<< IFX_SSC_GPOCON_INVOUT0_POS
),
1174 info
->mapbase
+ IFX_SSC_GPOCON
);
1175 /*TODO: disable cs */
1176 WRITE_PERIPHERAL_REGISTER (info
->opts
.
1177 gpoCs
<< IFX_SSC_WHBGPOSTAT_SETOUT0_POS
,
1178 info
->mapbase
+ IFX_SSC_WHBGPOSTAT
);
1181 if (opts
->masterSelect
) {
1182 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_SET_MASTER_SELECT
,
1183 info
->mapbase
+ IFX_SSC_WHBSTATE
);
1186 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_MASTER_SELECT
,
1187 info
->mapbase
+ IFX_SSC_WHBSTATE
);
1189 // init serial framing
1190 WRITE_PERIPHERAL_REGISTER (0, info
->mapbase
+ IFX_SSC_SFCON
);
1191 /* set up the port pins */
1192 //check for general requirements to switch (external) pad/pin characteristics
1194 local_irq_restore (flags
);
1197 } // ifx_ssc_sethwopts
1200 ifx_ssc_set_baud (struct ifx_ssc_port
*info
, unsigned int baud
)
1202 unsigned int ifx_ssc_clock
;
1204 unsigned long flags
;
1207 ifx_ssc_clock
= ifx_ssc_get_kernel_clk (info
);
1208 if (ifx_ssc_clock
== 0)
1210 local_irq_save (flags
);
1211 /* have to disable the SSC to set the baudrate */
1212 enabled
= (READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_STATE
)
1213 & IFX_SSC_STATE_IS_ENABLED
) != 0;
1214 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ENABLE
,
1215 info
->mapbase
+ IFX_SSC_WHBSTATE
);
1218 br
= (((ifx_ssc_clock
>> 1) + baud
/ 2) / baud
) - 1;
1222 ((READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_STATE
) &
1223 IFX_SSC_STATE_IS_MASTER
) == 0))) {
1224 local_irq_restore (flags
);
1225 printk ("%s: illegal baudrate %u\n", __FUNCTION__
, baud
);
1228 WRITE_PERIPHERAL_REGISTER (br
, info
->mapbase
+ IFX_SSC_BR
);
1230 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_SET_ENABLE
,
1231 info
->mapbase
+ IFX_SSC_WHBSTATE
);
1233 local_irq_restore (flags
);
1235 } // ifx_ssc_set_baud
1238 ifx_ssc_hwinit (struct ifx_ssc_port
*info
)
1240 unsigned long flags
;
1243 /* have to disable the SSC */
1244 enabled
= (READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_STATE
)
1245 & IFX_SSC_STATE_IS_ENABLED
) != 0;
1246 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ENABLE
,
1247 info
->mapbase
+ IFX_SSC_WHBSTATE
);
1249 if (ifx_ssc_sethwopts (info
) < 0) {
1250 printk ("%s: setting the hardware options failed\n",
1255 if (ifx_ssc_set_baud (info
, info
->baud
) < 0) {
1256 printk ("%s: setting the baud rate failed\n", __FUNCTION__
);
1259 local_irq_save (flags
);
1261 WRITE_PERIPHERAL_REGISTER ((IFX_SSC_DEF_TXFIFO_FL
<<
1262 IFX_SSC_XFCON_ITL_OFFSET
) |
1263 IFX_SSC_XFCON_FIFO_ENABLE
,
1264 info
->mapbase
+ IFX_SSC_TXFCON
);
1266 WRITE_PERIPHERAL_REGISTER ((IFX_SSC_DEF_RXFIFO_FL
<<
1267 IFX_SSC_XFCON_ITL_OFFSET
) |
1268 IFX_SSC_XFCON_FIFO_ENABLE
,
1269 info
->mapbase
+ IFX_SSC_RXFCON
);
1270 local_irq_restore (flags
);
1272 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_SET_ENABLE
,
1273 info
->mapbase
+ IFX_SSC_WHBSTATE
);
1277 /*-----------------------------------------------------------------------------
1278 FUNC-NAME : ifx_ssc_batch_exec
1282 PARAMETER : *info pointer to the port-specific structure ifx_ssc_port.
1284 RESULT : Returns error code
1287 -----------------------------------------------------------------------------*/
1289 ifx_ssc_batch_exec (struct ifx_ssc_port
*info
,
1290 struct ifx_ssc_batch_list
*batch_anchor
)
1292 // ### TO DO: implement user space batch execution
1293 // first, copy the whole linked list from user to kernel space
1294 // save some hardware options
1296 // restore hardware options if selected
1298 } // ifx_ssc_batch_exec
1301 * This routine allows the driver to implement device-
1302 * specific ioctl's. If the ioctl number passed in cmd is
1303 * not recognized by the driver, it should return ENOIOCTLCMD.
1306 ifx_ssc_ioctl (struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
1309 struct ifx_ssc_port
*info
;
1310 int line
, ret_val
= 0;
1311 unsigned long flags
;
1313 int from_kernel
= 0;
1315 if ((inode
== (struct inode
*) 0) || (inode
== (struct inode
*) 1)) {
1320 line
= MINOR (filp
->f_dentry
->d_inode
->i_rdev
);
1322 /* don't use more minor devices than we can support */
1323 if (line
< 0 || line
>= PORT_CNT
)
1329 case IFX_SSC_STATS_READ
:
1330 /* data must be a pointer to a struct ifx_ssc_statistics */
1332 memcpy ((void *) data
, (void *) &info
->stats
,
1333 sizeof (struct ifx_ssc_statistics
));
1334 else if (copy_to_user ((void *) data
,
1335 (void *) &info
->stats
,
1336 sizeof (struct ifx_ssc_statistics
)))
1339 case IFX_SSC_STATS_RESET
:
1340 /* just resets the statistics counters */
1341 memset ((void *) &info
->stats
, 0,
1342 sizeof (struct ifx_ssc_statistics
));
1344 case IFX_SSC_BAUD_SET
:
1345 /* if the buffers are not empty then the port is */
1346 /* busy and we shouldn't change things on-the-fly! */
1347 if (!info
->txbuf
|| !info
->rxbuf
||
1348 (READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_STATE
)
1349 & IFX_SSC_STATE_BUSY
)) {
1355 flags
= *((unsigned long *) data
);
1356 else if (copy_from_user ((void *) &flags
,
1357 (void *) data
, sizeof (flags
))) {
1365 if (ifx_ssc_set_baud (info
, flags
) < 0) {
1371 case IFX_SSC_BAUD_GET
:
1373 *((unsigned int *) data
) = info
->baud
;
1374 else if (copy_to_user ((void *) data
,
1375 (void *) &info
->baud
,
1376 sizeof (unsigned long)))
1379 case IFX_SSC_RXTX_MODE_SET
:
1381 tmp
= *((unsigned long *) data
);
1382 else if (copy_from_user ((void *) &tmp
,
1383 (void *) data
, sizeof (tmp
))) {
1387 ret_val
= ifx_ssc_rxtx_mode_set (info
, tmp
);
1389 case IFX_SSC_RXTX_MODE_GET
:
1390 tmp
= READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_CON
) &
1391 (~(IFX_SSC_CON_RX_OFF
| IFX_SSC_CON_TX_OFF
));
1393 *((unsigned int *) data
) = tmp
;
1394 else if (copy_to_user ((void *) data
,
1395 (void *) &tmp
, sizeof (tmp
)))
1400 ifx_ssc_abort (info
);
1403 case IFX_SSC_GPO_OUT_SET
:
1405 tmp
= *((unsigned long *) data
);
1406 else if (copy_from_user ((void *) &tmp
,
1407 (void *) data
, sizeof (tmp
))) {
1411 if (tmp
> IFX_SSC_MAX_GPO_OUT
)
1414 WRITE_PERIPHERAL_REGISTER
1415 (1 << (tmp
+ IFX_SSC_WHBGPOSTAT_SETOUT0_POS
),
1416 info
->mapbase
+ IFX_SSC_WHBGPOSTAT
);
1418 case IFX_SSC_GPO_OUT_CLR
:
1420 tmp
= *((unsigned long *) data
);
1421 else if (copy_from_user ((void *) &tmp
,
1422 (void *) data
, sizeof (tmp
))) {
1426 if (tmp
> IFX_SSC_MAX_GPO_OUT
)
1429 WRITE_PERIPHERAL_REGISTER
1430 (1 << (tmp
+ IFX_SSC_WHBGPOSTAT_CLROUT0_POS
),
1431 info
->mapbase
+ IFX_SSC_WHBGPOSTAT
);
1434 case IFX_SSC_GPO_OUT_GET
:
1435 tmp
= READ_PERIPHERAL_REGISTER
1436 (info
->mapbase
+ IFX_SSC_GPOSTAT
);
1438 *((unsigned int *) data
) = tmp
;
1439 else if (copy_to_user ((void *) data
,
1440 (void *) &tmp
, sizeof (tmp
)))
1443 case IFX_SSC_FRM_STATUS_GET
:
1444 ifx_ssc_frm_status_get (info
);
1446 memcpy ((void *) data
, (void *) &info
->frm_status
,
1447 sizeof (struct ifx_ssc_frm_status
));
1448 else if (copy_to_user ((void *) data
,
1449 (void *) &info
->frm_status
,
1450 sizeof (struct ifx_ssc_frm_status
)))
1453 case IFX_SSC_FRM_CONTROL_GET
:
1454 ifx_ssc_frm_control_get (info
);
1456 memcpy ((void *) data
, (void *) &info
->frm_opts
,
1457 sizeof (struct ifx_ssc_frm_opts
));
1458 else if (copy_to_user ((void *) data
,
1459 (void *) &info
->frm_opts
,
1460 sizeof (struct ifx_ssc_frm_opts
)))
1463 case IFX_SSC_FRM_CONTROL_SET
:
1465 memcpy ((void *) &info
->frm_opts
, (void *) data
,
1466 sizeof (struct ifx_ssc_frm_opts
));
1467 else if (copy_to_user ((void *) &info
->frm_opts
,
1469 sizeof (struct ifx_ssc_frm_opts
))) {
1473 ret_val
= ifx_ssc_frm_control_set (info
);
1475 case IFX_SSC_HWOPTS_SET
:
1476 /* data must be a pointer to a struct ifx_ssc_hwopts */
1477 /* if the buffers are not empty then the port is */
1478 /* busy and we shouldn't change things on-the-fly! */
1479 if (!info
->txbuf
|| !info
->rxbuf
||
1480 (READ_PERIPHERAL_REGISTER (info
->mapbase
+ IFX_SSC_STATE
)
1481 & IFX_SSC_STATE_BUSY
)) {
1486 memcpy ((void *) &info
->opts
, (void *) data
,
1487 sizeof (struct ifx_ssc_hwopts
));
1488 else if (copy_from_user ((void *) &info
->opts
,
1490 sizeof (struct ifx_ssc_hwopts
))) {
1494 if (ifx_ssc_hwinit (info
) < 0) {
1498 case IFX_SSC_HWOPTS_GET
:
1499 /* data must be a pointer to a struct ifx_ssc_hwopts */
1501 memcpy ((void *) data
, (void *) &info
->opts
,
1502 sizeof (struct ifx_ssc_hwopts
));
1503 else if (copy_to_user ((void *) data
,
1504 (void *) &info
->opts
,
1505 sizeof (struct ifx_ssc_hwopts
)))
1509 ret_val
= -ENOIOCTLCMD
;
1513 } /* ifx_ssc_ioctl */
1515 EXPORT_SYMBOL (ifx_ssc_ioctl
);
1517 ///* the poll routine */
1518 //static unsigned int
1519 //ifx_ssc_poll(struct file *filp, struct poll_table_struct *pts)
1521 // int unit = MINOR(filp->f_dentry->d_inode->i_rdev);
1522 // struct ifx_ssc_port *info;
1523 // unsigned int mask = 0;
1526 // info = &isp[unit];
1528 // /* add event to the wait queues */
1529 // /* DO NOT FORGET TO DO A WAKEUP ON THESE !!!! */
1530 // poll_wait(filp, &info->pwait, pts);
1532 // /* are there bytes in the RX SW-FIFO? */
1533 // if (info->rxrp != info->rxwp)
1534 // mask |= POLLIN | POLLRDNORM;
1536 // /* free space in the TX SW-FIFO */
1537 // spc = info->txrp - info->txwp - 1;
1539 // spc += TX_BUFSIZE;
1540 //#ifdef IFX_SSC_USEDMA
1541 // /* writing always works, except in the DMA case when all descriptors */
1542 // /* are used up */
1543 // if (unit == 1 && info->dma_freecnt == 0)
1547 // mask |= POLLOUT | POLLWRNORM;
1553 ifx_ssc1_read_proc (char *page
, char **start
, off_t offset
, int count
,
1554 int *eof
, void *data
)
1557 unsigned long flags
;
1559 /* don't want any interrupts here */
1560 local_save_flags(flags
);
1561 local_irq_disable();
1563 /* print statistics */
1564 off
+= sprintf (page
+ off
,
1565 "Statistics for Infineon Synchronous Serial Controller SSC1\n");
1566 off
+= sprintf (page
+ off
, "RX overflow errors %d\n",
1567 isp
[0].stats
.rxOvErr
);
1568 off
+= sprintf (page
+ off
, "RX underflow errors %d\n",
1569 isp
[0].stats
.rxUnErr
);
1570 off
+= sprintf (page
+ off
, "TX overflow errors %d\n",
1571 isp
[0].stats
.txOvErr
);
1572 off
+= sprintf (page
+ off
, "TX underflow errors %d\n",
1573 isp
[0].stats
.txUnErr
);
1574 off
+= sprintf (page
+ off
, "Abort errors %d\n",
1575 isp
[0].stats
.abortErr
);
1576 off
+= sprintf (page
+ off
, "Mode errors %d\n", isp
[0].stats
.modeErr
);
1577 off
+= sprintf (page
+ off
, "RX Bytes %d\n", isp
[0].stats
.rxBytes
);
1578 off
+= sprintf (page
+ off
, "TX Bytes %d\n", isp
[0].stats
.txBytes
);
1580 local_irq_restore(flags
); /* XXXXX */
1586 * This routine prints out the appropriate serial driver version number
1592 printk ("Infineon Technologies Synchronous Serial Controller (SSC) driver\n" " version %s - built %s %s\n", IFX_SSC_DRV_VERSION
, __DATE__
, __TIME__
);
1594 } /* show_version */
1597 * Due to the fact that a port can be dynamically switched between slave
1598 * and master mode using an IOCTL the hardware is not initialized here,
1599 * but in ifx_ssc_hwinit() as a result of an IOCTL.
1604 struct ifx_ssc_port
*info
;
1606 unsigned long flags
;
1609 // ### TO DO: dynamic port count evaluation due to pin multiplexing
1612 nbytes
= PORT_CNT
* sizeof (struct ifx_ssc_port
);
1613 isp
= (struct ifx_ssc_port
*) kmalloc (nbytes
, GFP_KERNEL
);
1615 printk ("%s: no memory for isp\n", __FUNCTION__
);
1618 memset (isp
, 0, nbytes
);
1622 /* register the device */
1627 if ((i
= register_chrdev (maj
, "ssc", &ifx_ssc_fops
)) < 0) {
1628 printk ("Unable to register major %d for the Infineon SSC\n",
1636 register_chrdev (maj
, "ssc",
1637 &ifx_ssc_fops
)) < 0) {
1638 printk ("Unable to register major %d for the Infineon SSC\n", maj
);
1645 //printk("registered major %d for Infineon SSC\n", maj);
1647 /* set default values in ifx_ssc_port */
1648 for (i
= 0; i
< PORT_CNT
; i
++) {
1651 /* default values for the HwOpts */
1652 info
->opts
.AbortErrDetect
= IFX_SSC_DEF_ABRT_ERR_DETECT
;
1653 info
->opts
.rxOvErrDetect
= IFX_SSC_DEF_RO_ERR_DETECT
;
1654 info
->opts
.rxUndErrDetect
= IFX_SSC_DEF_RU_ERR_DETECT
;
1655 info
->opts
.txOvErrDetect
= IFX_SSC_DEF_TO_ERR_DETECT
;
1656 info
->opts
.txUndErrDetect
= IFX_SSC_DEF_TU_ERR_DETECT
;
1657 info
->opts
.loopBack
= IFX_SSC_DEF_LOOP_BACK
;
1658 info
->opts
.echoMode
= IFX_SSC_DEF_ECHO_MODE
;
1659 info
->opts
.idleValue
= IFX_SSC_DEF_IDLE_DATA
;
1660 info
->opts
.clockPolarity
= IFX_SSC_DEF_CLOCK_POLARITY
;
1661 info
->opts
.clockPhase
= IFX_SSC_DEF_CLOCK_PHASE
;
1662 info
->opts
.headingControl
= IFX_SSC_DEF_HEADING_CONTROL
;
1663 info
->opts
.dataWidth
= IFX_SSC_DEF_DATA_WIDTH
;
1664 info
->opts
.modeRxTx
= IFX_SSC_DEF_MODE_RXTX
;
1665 info
->opts
.gpoCs
= IFX_SSC_DEF_GPO_CS
;
1666 info
->opts
.gpoInv
= IFX_SSC_DEF_GPO_INV
;
1667 info
->opts
.masterSelect
= IFX_SSC_DEF_MASTERSLAVE
;
1668 info
->baud
= IFX_SSC_DEF_BAUDRATE
;
1671 /* values specific to SSC1 */
1673 info
->mapbase
= DANUBE_SSC1_BASE_ADDR
;
1674 // ### TO DO: power management
1676 // setting interrupt vectors
1677 info
->txirq
= DANUBE_SSC_TIR
;
1678 info
->rxirq
= DANUBE_SSC_RIR
;
1679 info
->errirq
= DANUBE_SSC_EIR
;
1681 info->frmirq = IFX_SSC_FIR;
1686 WRITE_PERIPHERAL_REGISTER (IFX_SSC_DEF_RMC
<<
1687 IFX_CLC_RUN_DIVIDER_OFFSET
,
1688 info
->mapbase
+ IFX_SSC_CLC
);
1690 // ### TO DO: multiple instances
1692 init_waitqueue_head (&info
->rwait
);
1693 //init_waitqueue_head(&info->pwait);
1695 local_irq_save (flags
);
1697 // init serial framing register
1698 WRITE_PERIPHERAL_REGISTER (IFX_SSC_DEF_SFCON
,
1699 info
->mapbase
+ IFX_SSC_SFCON
);
1701 /* try to get the interrupts */
1702 // ### TO DO: interrupt handling with multiple instances
1704 ifx_int_wrapper
.request (info
->txirq
, ifx_ssc_tx_int
, SA_INTERRUPT
, "ifx_ssc_tx", info
);
1706 printk ("%s: unable to get irq %d\n", __FUNCTION__
,
1708 local_irq_restore (flags
);
1712 ifx_int_wrapper
.request (info
->rxirq
, ifx_ssc_rx_int
, SA_INTERRUPT
, "ifx_ssc_rx", info
);
1714 printk ("%s: unable to get irq %d\n", __FUNCTION__
,
1716 local_irq_restore (flags
);
1720 ifx_int_wrapper
.request (info
->errirq
, ifx_ssc_err_int
, SA_INTERRUPT
,"ifx_ssc_err", info
);
1722 printk ("%s: unable to get irq %d\n", __FUNCTION__
,
1724 local_irq_restore (flags
);
1728 ret_val = ifx_int_wrapper.request(info->frmirq, ifx_ssc_frm_int,
1729 0, "ifx_ssc_frm", info);
1731 printk("%s: unable to get irq %d\n", __FUNCTION__,
1733 local_irq_restore(flags);
1738 WRITE_PERIPHERAL_REGISTER (IFX_SSC_DEF_IRNEN
,
1739 info
->mapbase
+ IFX_SSC_IRN_EN
);
1740 ifx_int_wrapper
.enable (info
->txirq
);
1741 ifx_int_wrapper
.enable (info
->rxirq
);
1742 ifx_int_wrapper
.enable (info
->errirq
);
1744 local_irq_restore (flags
);
1745 } // for (i = 0; i < PORT_CNT; i++)
1747 /* init the SSCs with default values */
1748 for (i
= 0; i
< PORT_CNT
; i
++) {
1750 if (ifx_ssc_hwinit (info
) < 0) {
1751 printk ("%s: hardware init failed for port %d\n",
1757 /* register /proc read handler */
1758 // ### TO DO: multiple instances
1759 /* for SSC1, which is always present */
1760 create_proc_read_entry ("driver/ssc1", 0, NULL
, ifx_ssc1_read_proc
,
1765 // ### TO DO: multiple instances
1766 ifx_int_wrapper
.free (isp
[0].txirq
, &isp
[0]);
1767 ifx_int_wrapper
.free (isp
[0].rxirq
, &isp
[0]);
1768 ifx_int_wrapper
.free (isp
[0].errirq
, &isp
[0]);
1770 ifx_int_wrapper.free(isp[0].frmirq, &isp[0]);
1773 /* free up any allocated memory in the error case */
1776 } /* ifx_ssc_init */
1779 ifx_ssc_cleanup_module (void)
1783 /* free up any allocated memory */
1784 for (i
= 0; i
< PORT_CNT
; i
++) {
1785 /* disable the SSC */
1786 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ENABLE
,
1787 isp
[i
].mapbase
+ IFX_SSC_WHBSTATE
);
1788 /* free the interrupts */
1789 ifx_int_wrapper
.free (isp
[i
].txirq
, &isp
[i
]);
1790 ifx_int_wrapper
.free (isp
[i
].rxirq
, &isp
[i
]);
1791 ifx_int_wrapper
.free (isp
[i
].errirq
, &isp
[i
]);
1793 ifx_int_wrapper.free(isp[i].frmirq, &isp[i]);
1795 if (isp[i].rxbuf != NULL)
1796 kfree(isp[i].rxbuf);
1797 if (isp[i].txbuf != NULL)
1798 kfree(isp[i].txbuf);
1802 /* unregister the device */
1803 // if (unregister_chrdev (maj, "ssc")) {
1804 // printk ("Unable to unregister major %d for the SSC\n", maj);
1806 /* delete /proc read handler */
1807 remove_proc_entry ("driver/ssc1", NULL
);
1808 remove_proc_entry ("driver/ssc2", NULL
);
1809 } /* ifx_ssc_cleanup_module */
1811 module_exit (ifx_ssc_cleanup_module
);
1813 /* Module entry-points */
1814 module_init (ifx_ssc_init
);
1818 ifx_ssc_set_maj (char *str
)
1820 maj
= simple_strtol (str
, NULL
, 0);
1824 __setup ("ssc_maj=", ifx_ssc_set_maj
);
1825 #endif /* !MODULE */
1827 #define DANUBE_SSC_EMSG(fmt,arg...) printk("%s: "fmt,__FUNCTION__, ##arg)
1828 /* Brief: chip select enable
1831 ifx_ssc_cs_low (u32 pin
)
1835 ifx_ssc_ioctl ((struct inode
*) 0, NULL
, IFX_SSC_GPO_OUT_CLR
,
1836 (unsigned long) &pin
))) {
1837 DANUBE_SSC_EMSG ("clear CS %d fails\n", pin
);
1843 EXPORT_SYMBOL (ifx_ssc_cs_low
);
1844 /* Brief: chip select disable
1847 ifx_ssc_cs_high (u32 pin
)
1851 ifx_ssc_ioctl ((struct inode
*) 0, NULL
, IFX_SSC_GPO_OUT_SET
,
1852 (unsigned long) &pin
))) {
1853 DANUBE_SSC_EMSG ("set CS %d fails\n", pin
);
1859 EXPORT_SYMBOL (ifx_ssc_cs_high
);
1860 /* Brief: one SSC session
1866 * session_mode: IFX_SSC_MODE_RXTX or IFX_SSC_MODE_TX
1867 * Return: >=0 number of bytes received (if rx_buf != 0) or transmitted
1870 * 0. copy data to internal buffer
1872 * 2a. If SSC_SESSION_MODE_TXONLY, read tx_len data
1873 * 2b. If not Read back (tx_len + rx_len) data
1874 * 3. copy internal buffer to rx buf if necessary
1877 ssc_session (char *tx_buf
, u32 tx_len
, char *rx_buf
, u32 rx_len
)
1881 char *ssc_tx_buf
= NULL
;
1882 char *ssc_rx_buf
= NULL
;
1884 // volatile char ssc_tx_buf[128]={0};
1885 // volatile char ssc_rx_buf[128]={0};
1890 if (tx_buf
== NULL
&& tx_len
== 0 && rx_buf
== NULL
&& rx_len
== 0) {
1891 DANUBE_SSC_EMSG ("invalid parameters\n");
1893 goto ssc_session_exit
;
1895 else if (tx_buf
== NULL
|| tx_len
== 0) {
1896 if (rx_buf
!= NULL
&& rx_len
!= 0) {
1897 mode
= IFX_SSC_MODE_RX
;
1900 DANUBE_SSC_EMSG ("invalid parameters\n");
1902 goto ssc_session_exit
;
1905 else if (rx_buf
== NULL
|| rx_len
== 0) {
1906 if (tx_buf
!= NULL
&& tx_len
!= 0) {
1907 mode
= IFX_SSC_MODE_TX
;
1910 DANUBE_SSC_EMSG ("invalid parameters\n");
1912 goto ssc_session_exit
;
1916 mode
= IFX_SSC_MODE_RXTX
;
1919 if (mode
== IFX_SSC_MODE_RXTX
) {
1920 eff_size
= tx_len
+ rx_len
;
1922 else if (mode
== IFX_SSC_MODE_RX
) {
1929 //4 bytes alignment, required by driver
1930 /* change by TaiCheng */
1934 (char *) kmalloc (sizeof (char) *
1935 ((eff_size
+ 3) & (~3)),
1938 (char *) kmalloc (sizeof (char) *
1939 ((eff_size
+ 3) & (~3)),
1944 (char *) kmalloc (sizeof (char) *
1945 ((eff_size
+ 3) & (~3)),
1948 (char *) kmalloc (sizeof (char) *
1949 ((eff_size
+ 3) & (~3)),
1952 if (ssc_tx_buf
== NULL
|| ssc_rx_buf
== NULL
) {
1953 DANUBE_SSC_EMSG ("no memory for size of %d\n", eff_size
);
1955 goto ssc_session_exit
;
1957 memset ((void *) ssc_tx_buf
, 0, eff_size
);
1958 memset ((void *) ssc_rx_buf
, 0, eff_size
);
1961 memcpy (ssc_tx_buf
, tx_buf
, tx_len
);
1964 ret
= ifx_ssc_kwrite (0, ssc_tx_buf
, eff_size
);
1967 ssc_tx_buf
= NULL
; //should be freed by ifx_ssc_kwrite
1970 if (ret
!= eff_size
) {
1971 DANUBE_SSC_EMSG ("ifx_ssc_write return %d\n", ret
);
1972 goto ssc_session_exit
;
1974 ret
= ifx_ssc_kread (0, ssc_rx_buf
, eff_size
);
1975 if (ret
!= eff_size
) {
1976 DANUBE_SSC_EMSG ("ifx_ssc_read return %d\n", ret
);
1977 goto ssc_session_exit
;
1980 memcpy (rx_buf
, ssc_rx_buf
+ tx_len
, rx_len
);
1982 if (mode
== IFX_SSC_MODE_TX
) {
1990 if (ssc_tx_buf
!= NULL
)
1992 if (ssc_rx_buf
!= NULL
)
1996 printk ("ssc session fails\n");
2001 /* Brief: TX-RX session
2007 * Return: >=0 number of bytes received
2014 ifx_ssc_txrx (char *tx_buf
, u32 tx_len
, char *rx_buf
, u32 rx_len
)
2016 return ssc_session (tx_buf
, tx_len
, rx_buf
, rx_len
);
2019 EXPORT_SYMBOL (ifx_ssc_txrx
);
2020 /* Brief: TX only session
2024 * Return: >=0 number of bytes transmitted
2028 ifx_ssc_tx (char *tx_buf
, u32 tx_len
)
2030 return ssc_session (tx_buf
, tx_len
, NULL
, 0);
2033 EXPORT_SYMBOL (ifx_ssc_tx
);
2034 /* Brief: RX only session
2038 * Return: >=0 number of bytes received
2042 ifx_ssc_rx (char *rx_buf
, u32 rx_len
)
2044 return ssc_session (NULL
, 0, rx_buf
, rx_len
);
2047 EXPORT_SYMBOL (ifx_ssc_rx
);
2049 MODULE_LICENSE("GPL");
2050 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
2051 MODULE_DESCRIPTION("danube ssc driver");