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/fcntl.h>
38 #include <linux/ptrace.h>
40 #include <linux/ioport.h>
41 #include <linux/init.h>
42 #include <linux/delay.h>
43 #include <linux/spinlock.h>
44 #include <linux/slab.h>
46 #include <asm/system.h>
49 #include <asm/uaccess.h>
50 #include <asm/bitops.h>
52 #include <linux/types.h>
53 #include <linux/kernel.h>
54 #include <linux/version.h>
56 #include <asm/ifxmips/ifxmips.h>
57 #include <asm/ifxmips/ifxmips_irq.h>
58 #include <asm/ifxmips/ifx_ssc_defines.h>
59 #include <asm/ifxmips/ifx_ssc.h>
61 /* allow the user to set the major device number */
65 * This is the per-channel data structure containing pointers, flags
66 * and variables for the port. This driver supports a maximum of PORT_CNT.
67 * isp is allocated in ifx_ssc_init() based on the chip version.
69 static struct ifx_ssc_port *isp;
71 /* other forward declarations */
72 static unsigned int ifx_ssc_get_kernel_clk (struct ifx_ssc_port *info);
73 static void tx_int (struct ifx_ssc_port *);
75 extern unsigned int ifxmips_get_fpi_hz (void);
76 extern void mask_and_ack_ifxmips_irq (unsigned int irq_nr);
78 static inline unsigned int
79 ifx_ssc_get_kernel_clk (struct ifx_ssc_port *info)
83 rmc = (ifxmips_r32(IFXMIPS_SSC_CLC) & IFX_CLC_RUN_DIVIDER_MASK) >> IFX_CLC_RUN_DIVIDER_OFFSET;
86 printk ("ifx_ssc_get_kernel_clk rmc==0 \n");
89 return ifxmips_get_fpi_hz () / rmc;
93 rx_int (struct ifx_ssc_port *info)
95 int fifo_fill_lev, bytes_in_buf, i;
96 unsigned long tmp_val;
97 unsigned long *tmp_ptr;
98 unsigned int rx_valid_cnt;
99 /* number of words waiting in the RX FIFO */
100 fifo_fill_lev = (ifxmips_r32(IFXMIPS_SSC_FSTAT) & IFX_SSC_FSTAT_RECEIVED_WORDS_MASK) >> IFX_SSC_FSTAT_RECEIVED_WORDS_OFFSET;
101 bytes_in_buf = info->rxbuf_end - info->rxbuf_ptr;
102 // transfer with 32 bits per entry
103 while ((bytes_in_buf >= 4) && (fifo_fill_lev > 0)) {
104 tmp_ptr = (unsigned long *) info->rxbuf_ptr;
105 *tmp_ptr = ifxmips_r32(IFXMIPS_SSC_RB);
106 info->rxbuf_ptr += 4;
107 info->stats.rxBytes += 4;
112 // now do the rest as mentioned in STATE.RXBV
113 while ((bytes_in_buf > 0) && (fifo_fill_lev > 0)) {
114 rx_valid_cnt = (ifxmips_r32(IFXMIPS_SSC_STATE) & IFX_SSC_STATE_RX_BYTE_VALID_MASK) >> IFX_SSC_STATE_RX_BYTE_VALID_OFFSET;
115 if (rx_valid_cnt == 0)
118 if (rx_valid_cnt > bytes_in_buf)
119 rx_valid_cnt = bytes_in_buf;
121 tmp_val = ifxmips_r32(IFXMIPS_SSC_RB);
123 for (i = 0; i < rx_valid_cnt; i++)
125 *info->rxbuf_ptr = (tmp_val >> (8 * (rx_valid_cnt - i - 1))) & 0xff;
129 info->stats.rxBytes += rx_valid_cnt;
132 // check if transfer is complete
133 if (info->rxbuf_ptr >= info->rxbuf_end)
135 disable_irq(IFXMIPS_SSC_RIR);
136 wake_up_interruptible (&info->rwait);
137 } else if ((info->opts.modeRxTx == IFX_SSC_MODE_RX) && (ifxmips_r32(IFXMIPS_SSC_RXCNT) == 0))
139 if (info->rxbuf_end - info->rxbuf_ptr < IFX_SSC_RXREQ_BLOCK_SIZE)
140 ifxmips_w32((info->rxbuf_end - info->rxbuf_ptr) << IFX_SSC_RXREQ_RXCOUNT_OFFSET, IFXMIPS_SSC_RXREQ);
142 ifxmips_w32(IFX_SSC_RXREQ_BLOCK_SIZE << IFX_SSC_RXREQ_RXCOUNT_OFFSET, IFXMIPS_SSC_RXREQ);
147 tx_int (struct ifx_ssc_port *info)
150 int fifo_space, fill, i;
151 fifo_space = ((ifxmips_r32(IFXMIPS_SSC_ID) & IFX_SSC_PERID_TXFS_MASK) >> IFX_SSC_PERID_TXFS_OFFSET)
152 - ((ifxmips_r32(IFXMIPS_SSC_FSTAT) & IFX_SSC_FSTAT_TRANSMIT_WORDS_MASK) >> IFX_SSC_FSTAT_TRANSMIT_WORDS_OFFSET);
157 fill = info->txbuf_end - info->txbuf_ptr;
159 if (fill > fifo_space * 4)
160 fill = fifo_space * 4;
162 for (i = 0; i < fill / 4; i++)
164 // at first 32 bit access
165 ifxmips_w32(*(UINT32 *) info->txbuf_ptr, IFXMIPS_SSC_TB);
166 info->txbuf_ptr += 4;
169 fifo_space -= fill / 4;
170 info->stats.txBytes += fill & ~0x3;
172 if ((fifo_space > 0) & (fill > 1))
174 // trailing 16 bit access
175 WRITE_PERIPHERAL_REGISTER_16 (*(UINT16 *) info->txbuf_ptr, info->mapbase + IFX_SSC_TB);
176 info->txbuf_ptr += 2;
177 info->stats.txBytes += 2;
182 if ((fifo_space > 0) & (fill > 0))
184 // trailing 8 bit access
185 WRITE_PERIPHERAL_REGISTER_8 (*(UINT8 *) info->txbuf_ptr, info->mapbase + IFX_SSC_TB);
187 info->stats.txBytes++;
190 // check if transmission complete
191 if (info->txbuf_ptr >= info->txbuf_end)
193 disable_irq(IFXMIPS_SSC_TIR);
201 ifx_ssc_rx_int (int irq, void *dev_id)
203 struct ifx_ssc_port *info = (struct ifx_ssc_port *) dev_id;
210 ifx_ssc_tx_int (int irq, void *dev_id)
212 struct ifx_ssc_port *info = (struct ifx_ssc_port *) dev_id;
219 ifx_ssc_err_int (int irq, void *dev_id)
221 struct ifx_ssc_port *info = (struct ifx_ssc_port *) dev_id;
223 unsigned int write_back = 0;
226 local_irq_save (flags);
227 state = ifxmips_r32(IFXMIPS_SSC_STATE);
229 if ((state & IFX_SSC_STATE_RX_UFL) != 0) {
230 info->stats.rxUnErr++;
231 write_back |= IFX_SSC_WHBSTATE_CLR_RX_UFL_ERROR;
234 if ((state & IFX_SSC_STATE_RX_OFL) != 0) {
235 info->stats.rxOvErr++;
236 write_back |= IFX_SSC_WHBSTATE_CLR_RX_OFL_ERROR;
239 if ((state & IFX_SSC_STATE_TX_OFL) != 0) {
240 info->stats.txOvErr++;
241 write_back |= IFX_SSC_WHBSTATE_CLR_TX_OFL_ERROR;
244 if ((state & IFX_SSC_STATE_TX_UFL) != 0) {
245 info->stats.txUnErr++;
246 write_back |= IFX_SSC_WHBSTATE_CLR_TX_UFL_ERROR;
249 if ((state & IFX_SSC_STATE_MODE_ERR) != 0) {
250 info->stats.modeErr++;
251 write_back |= IFX_SSC_WHBSTATE_CLR_MODE_ERROR;
255 ifxmips_w32(write_back, IFXMIPS_SSC_WHBSTATE);
257 local_irq_restore (flags);
263 ifx_ssc_abort (struct ifx_ssc_port *info)
268 local_irq_save (flags);
270 disable_irq(IFXMIPS_SSC_RIR);
271 disable_irq(IFXMIPS_SSC_TIR);
272 disable_irq(IFXMIPS_SSC_EIR);
274 local_irq_restore (flags);
276 // disable SSC (also aborts a receive request!)
277 // ### TO DO: Perhaps it's better to abort after the receiption of a
278 // complete word. The disable cuts the transmission immediatly and
279 // releases the chip selects. This could result in unpredictable
280 // behavior of connected external devices!
281 enabled = (ifxmips_r32(IFXMIPS_SSC_STATE) & IFX_SSC_STATE_IS_ENABLED) != 0;
282 ifxmips_w32(IFX_SSC_WHBSTATE_CLR_ENABLE, IFXMIPS_SSC_WHBSTATE);
285 ifxmips_w32(IFX_SSC_XFCON_FIFO_FLUSH, IFXMIPS_SSC_TXFCON);
286 ifxmips_w32(IFX_SSC_XFCON_FIFO_FLUSH, IFXMIPS_SSC_RXFCON);
289 if (info->txbuf != NULL)
295 // wakeup read process
296 if (info->rxbuf != NULL)
297 wake_up_interruptible (&info->rwait);
299 // clear pending int's
300 mask_and_ack_ifxmips_irq(IFXMIPS_SSC_RIR);
301 mask_and_ack_ifxmips_irq(IFXMIPS_SSC_TIR);
302 mask_and_ack_ifxmips_irq(IFXMIPS_SSC_EIR);
305 ifxmips_w32(IFX_SSC_WHBSTATE_CLR_ALL_ERROR, IFXMIPS_SSC_WHBSTATE);
308 ifxmips_w32(IFX_SSC_WHBSTATE_SET_ENABLE, IFXMIPS_SSC_WHBSTATE);
313 * This routine is called whenever a port is opened. It enforces
314 * exclusive opening of a port and enables interrupts, etc.
317 ifx_ssc_open (struct inode *inode, struct file *filp)
319 struct ifx_ssc_port *info;
323 if ((inode == (struct inode *) 0) || (inode == (struct inode *) 1)) {
327 line = MINOR (filp->f_dentry->d_inode->i_rdev);
330 /* don't open more minor devices than we can support */
331 if (line < 0 || line >= PORT_CNT)
337 if (info->port_is_open != 0)
339 info->port_is_open++;
341 disable_irq(IFXMIPS_SSC_RIR);
342 disable_irq(IFXMIPS_SSC_TIR);
343 disable_irq(IFXMIPS_SSC_EIR);
345 /* Flush and enable TX/RX FIFO */
346 ifxmips_w32((IFX_SSC_DEF_TXFIFO_FL << IFX_SSC_XFCON_ITL_OFFSET) | IFX_SSC_XFCON_FIFO_FLUSH | IFX_SSC_XFCON_FIFO_ENABLE, IFXMIPS_SSC_TXFCON);
347 ifxmips_w32((IFX_SSC_DEF_RXFIFO_FL << IFX_SSC_XFCON_ITL_OFFSET) | IFX_SSC_XFCON_FIFO_FLUSH | IFX_SSC_XFCON_FIFO_ENABLE, IFXMIPS_SSC_RXFCON);
349 /* logically flush the software FIFOs */
353 /* clear all error bits */
354 ifxmips_w32(IFX_SSC_WHBSTATE_CLR_ALL_ERROR, IFXMIPS_SSC_WHBSTATE);
356 // clear pending interrupts
357 mask_and_ack_ifxmips_irq(IFXMIPS_SSC_RIR);
358 mask_and_ack_ifxmips_irq(IFXMIPS_SSC_TIR);
359 mask_and_ack_ifxmips_irq(IFXMIPS_SSC_EIR);
361 ifxmips_w32(IFX_SSC_WHBSTATE_SET_ENABLE, IFXMIPS_SSC_WHBSTATE);
365 EXPORT_SYMBOL(ifx_ssc_open);
368 ifx_ssc_close (struct inode *inode, struct file *filp)
370 struct ifx_ssc_port *info;
373 if ((inode == (struct inode *) 0) || (inode == (struct inode *) 1))
376 idx = MINOR (filp->f_dentry->d_inode->i_rdev);
378 if (idx < 0 || idx >= PORT_CNT)
385 ifxmips_w32(IFX_SSC_WHBSTATE_CLR_ENABLE, IFXMIPS_SSC_WHBSTATE);
389 info->port_is_open--;
393 EXPORT_SYMBOL(ifx_ssc_close);
396 ifx_ssc_read_helper_poll (struct ifx_ssc_port *info, char *buf, size_t len, int from_kernel)
401 if (info->opts.modeRxTx == IFX_SSC_MODE_TX)
403 local_irq_save (flags);
404 info->rxbuf_ptr = info->rxbuf;
405 info->rxbuf_end = info->rxbuf + len;
406 local_irq_restore (flags);
407 /* Vinetic driver always works in IFX_SSC_MODE_RXTX */
408 /* TXRX in poll mode */
409 while (info->rxbuf_ptr < info->rxbuf_end)
411 if (info->txbuf_ptr < info->txbuf_end)
417 ret_val = info->rxbuf_ptr - info->rxbuf;
423 ifx_ssc_read_helper (struct ifx_ssc_port *info, char *buf, size_t len, int from_kernel)
427 DECLARE_WAITQUEUE (wait, current);
429 if (info->opts.modeRxTx == IFX_SSC_MODE_TX)
432 local_irq_save (flags);
433 info->rxbuf_ptr = info->rxbuf;
434 info->rxbuf_end = info->rxbuf + len;
436 if (info->opts.modeRxTx == IFX_SSC_MODE_RXTX)
438 if ((info->txbuf == NULL) || (info->txbuf != info->txbuf_ptr) || (info->txbuf_end != len + info->txbuf))
440 local_irq_restore (flags);
441 printk ("IFX SSC - %s: write must be called before calling " "read in combined RX/TX!\n", __func__);
445 local_irq_restore(flags);
448 if (info->txbuf_ptr < info->txbuf_end)
449 enable_irq(IFXMIPS_SSC_TIR);
451 enable_irq(IFXMIPS_SSC_RIR);
453 local_irq_restore(flags);
454 if (ifxmips_r32(IFXMIPS_SSC_RXCNT) & IFX_SSC_RXCNT_TODO_MASK)
456 enable_irq(IFXMIPS_SSC_RIR);
457 if (len < IFX_SSC_RXREQ_BLOCK_SIZE)
458 ifxmips_w32(len << IFX_SSC_RXREQ_RXCOUNT_OFFSET, IFXMIPS_SSC_RXREQ);
460 ifxmips_w32(IFX_SSC_RXREQ_BLOCK_SIZE << IFX_SSC_RXREQ_RXCOUNT_OFFSET, IFXMIPS_SSC_RXREQ);
463 __add_wait_queue (&info->rwait, &wait);
464 set_current_state (TASK_INTERRUPTIBLE);
467 local_irq_save (flags);
468 if (info->rxbuf_ptr >= info->rxbuf_end)
471 local_irq_restore (flags);
473 if (signal_pending (current))
475 ret_val = -ERESTARTSYS;
481 ret_val = info->rxbuf_ptr - info->rxbuf;
482 local_irq_restore (flags);
485 current->state = TASK_RUNNING;
486 __remove_wait_queue (&info->rwait, &wait);
492 ifx_ssc_write_helper (struct ifx_ssc_port *info, const char *buf,
493 size_t len, int from_kernel)
495 if (info->opts.modeRxTx == IFX_SSC_MODE_RX)
498 info->txbuf_ptr = info->txbuf;
499 info->txbuf_end = len + info->txbuf;
500 if (info->opts.modeRxTx == IFX_SSC_MODE_TX)
503 if (info->txbuf_ptr < info->txbuf_end)
505 enable_irq(IFXMIPS_SSC_TIR);
513 ifx_ssc_kread (int port, char *kbuf, size_t len)
515 struct ifx_ssc_port *info;
518 if (port < 0 || port >= PORT_CNT)
526 if (info->rxbuf != NULL)
528 printk ("SSC device busy\n");
533 if (info->rxbuf == NULL)
535 printk ("SSC device error\n");
539 ret_val = ifx_ssc_read_helper_poll (info, kbuf, len, 1);
542 disable_irq(IFXMIPS_SSC_RIR);
546 EXPORT_SYMBOL(ifx_ssc_kread);
549 ifx_ssc_kwrite (int port, const char *kbuf, size_t len)
551 struct ifx_ssc_port *info;
554 if (port < 0 || port >= PORT_CNT)
562 // check if transmission in progress
563 if (info->txbuf != NULL)
566 info->txbuf = (char *) kbuf;
568 ret_val = ifx_ssc_write_helper (info, info->txbuf, len, 1);
575 EXPORT_SYMBOL(ifx_ssc_kwrite);
578 ifx_ssc_read (struct file *filp, char *ubuf, size_t len, loff_t * off)
582 struct ifx_ssc_port *info;
584 idx = MINOR (filp->f_dentry->d_inode->i_rdev);
587 if (info->rxbuf != NULL)
590 info->rxbuf = kmalloc (len + 3, GFP_KERNEL);
591 if (info->rxbuf == NULL)
594 ret_val = ifx_ssc_read_helper (info, info->rxbuf, len, 0);
595 if (copy_to_user ((void *) ubuf, info->rxbuf, ret_val) != 0)
598 disable_irq(IFXMIPS_SSC_RIR);
607 ifx_ssc_write (struct file *filp, const char *ubuf, size_t len, loff_t * off)
610 struct ifx_ssc_port *info;
616 idx = MINOR (filp->f_dentry->d_inode->i_rdev);
619 if (info->txbuf != NULL)
622 info->txbuf = kmalloc (len + 3, GFP_KERNEL);
623 if (info->txbuf == NULL)
626 ret_val = copy_from_user (info->txbuf, ubuf, len);
628 ret_val = ifx_ssc_write_helper (info, info->txbuf, len, 0);
641 static struct ifx_ssc_frm_status *
642 ifx_ssc_frm_status_get (struct ifx_ssc_port *info)
646 tmp = ifxmips_r32(IFXMIPS_SSC_SFSTAT);
647 info->frm_status.DataBusy = (tmp & IFX_SSC_SFSTAT_IN_DATA) > 0;
648 info->frm_status.PauseBusy = (tmp & IFX_SSC_SFSTAT_IN_PAUSE) > 0;
649 info->frm_status.DataCount = (tmp & IFX_SSC_SFSTAT_DATA_COUNT_MASK) >> IFX_SSC_SFSTAT_DATA_COUNT_OFFSET;
650 info->frm_status.PauseCount = (tmp & IFX_SSC_SFSTAT_PAUSE_COUNT_MASK) >> IFX_SSC_SFSTAT_PAUSE_COUNT_OFFSET;
651 tmp = ifxmips_r32(IFXMIPS_SSC_SFCON);
652 info->frm_status.EnIntAfterData = (tmp & IFX_SSC_SFCON_FIR_ENABLE_BEFORE_PAUSE) > 0;
653 info->frm_status.EnIntAfterPause = (tmp & IFX_SSC_SFCON_FIR_ENABLE_AFTER_PAUSE) > 0;
655 return &info->frm_status;
659 static struct ifx_ssc_frm_opts *
660 ifx_ssc_frm_control_get (struct ifx_ssc_port *info)
664 tmp = ifxmips_r32(IFXMIPS_SSC_SFCON);
665 info->frm_opts.FrameEnable = (tmp & IFX_SSC_SFCON_SF_ENABLE) > 0;
666 info->frm_opts.DataLength = (tmp & IFX_SSC_SFCON_DATA_LENGTH_MASK) >> IFX_SSC_SFCON_DATA_LENGTH_OFFSET;
667 info->frm_opts.PauseLength = (tmp & IFX_SSC_SFCON_PAUSE_LENGTH_MASK) >> IFX_SSC_SFCON_PAUSE_LENGTH_OFFSET;
668 info->frm_opts.IdleData = (tmp & IFX_SSC_SFCON_PAUSE_DATA_MASK) >> IFX_SSC_SFCON_PAUSE_DATA_OFFSET;
669 info->frm_opts.IdleClock = (tmp & IFX_SSC_SFCON_PAUSE_CLOCK_MASK) >> IFX_SSC_SFCON_PAUSE_CLOCK_OFFSET;
670 info->frm_opts.StopAfterPause = (tmp & IFX_SSC_SFCON_STOP_AFTER_PAUSE) > 0;
672 return &info->frm_opts;
676 ifx_ssc_frm_control_set (struct ifx_ssc_port *info)
680 if ((info->frm_opts.DataLength > IFX_SSC_SFCON_DATA_LENGTH_MAX)
681 || (info->frm_opts.DataLength < 1)
682 || (info->frm_opts.PauseLength > IFX_SSC_SFCON_PAUSE_LENGTH_MAX)
683 || (info->frm_opts.PauseLength < 1)
684 || (info->frm_opts.IdleData & ~(IFX_SSC_SFCON_PAUSE_DATA_MASK >> IFX_SSC_SFCON_PAUSE_DATA_OFFSET))
685 || (info->frm_opts.IdleClock & ~(IFX_SSC_SFCON_PAUSE_CLOCK_MASK >> IFX_SSC_SFCON_PAUSE_CLOCK_OFFSET)))
688 // read interrupt bits (they're not changed here)
689 tmp = ifxmips_r32(IFXMIPS_SSC_SFCON) &
690 (IFX_SSC_SFCON_FIR_ENABLE_BEFORE_PAUSE | IFX_SSC_SFCON_FIR_ENABLE_AFTER_PAUSE);
692 // set all values with respect to it's bit position (for data and pause
694 tmp = (info->frm_opts.DataLength - 1) << IFX_SSC_SFCON_DATA_LENGTH_OFFSET;
695 tmp |= (info->frm_opts.PauseLength - 1) << IFX_SSC_SFCON_PAUSE_LENGTH_OFFSET;
696 tmp |= info->frm_opts.IdleData << IFX_SSC_SFCON_PAUSE_DATA_OFFSET;
697 tmp |= info->frm_opts.IdleClock << IFX_SSC_SFCON_PAUSE_CLOCK_OFFSET;
698 tmp |= info->frm_opts.FrameEnable * IFX_SSC_SFCON_SF_ENABLE;
699 tmp |= info->frm_opts.StopAfterPause * IFX_SSC_SFCON_STOP_AFTER_PAUSE;
701 ifxmips_w32(tmp, IFXMIPS_SSC_SFCON);
707 ifx_ssc_rxtx_mode_set (struct ifx_ssc_port *info, unsigned int val)
711 if (!(info) || (val & ~(IFX_SSC_MODE_MASK)))
714 if ((ifxmips_r32(IFXMIPS_SSC_STATE) & IFX_SSC_STATE_BUSY)
715 || (ifxmips_r32(IFXMIPS_SSC_RXCNT) & IFX_SSC_RXCNT_TODO_MASK))
718 tmp = (ifxmips_r32(IFXMIPS_SSC_CON) & ~(IFX_SSC_CON_RX_OFF | IFX_SSC_CON_TX_OFF)) | (val);
719 ifxmips_w32(tmp, IFXMIPS_SSC_SFCON);
720 info->opts.modeRxTx = val;
726 ifx_ssc_sethwopts (struct ifx_ssc_port *info)
728 unsigned long flags, bits;
729 struct ifx_ssc_hwopts *opts = &info->opts;
731 if ((opts->dataWidth < IFX_SSC_MIN_DATA_WIDTH)
732 || (opts->dataWidth > IFX_SSC_MAX_DATA_WIDTH))
735 bits = (opts->dataWidth - 1) << IFX_SSC_CON_DATA_WIDTH_OFFSET;
736 bits |= IFX_SSC_CON_ENABLE_BYTE_VALID;
738 if (opts->rxOvErrDetect)
739 bits |= IFX_SSC_CON_RX_OFL_CHECK;
740 if (opts->rxUndErrDetect)
741 bits |= IFX_SSC_CON_RX_UFL_CHECK;
742 if (opts->txOvErrDetect)
743 bits |= IFX_SSC_CON_TX_OFL_CHECK;
744 if (opts->txUndErrDetect)
745 bits |= IFX_SSC_CON_TX_UFL_CHECK;
747 bits |= IFX_SSC_CON_LOOPBACK_MODE;
749 bits |= IFX_SSC_CON_ECHO_MODE_ON;
750 if (opts->headingControl)
751 bits |= IFX_SSC_CON_MSB_FIRST;
752 if (opts->clockPhase)
753 bits |= IFX_SSC_CON_LATCH_THEN_SHIFT;
754 if (opts->clockPolarity)
755 bits |= IFX_SSC_CON_CLOCK_FALL;
757 switch (opts->modeRxTx)
759 case IFX_SSC_MODE_TX:
760 bits |= IFX_SSC_CON_RX_OFF;
762 case IFX_SSC_MODE_RX:
763 bits |= IFX_SSC_CON_TX_OFF;
767 local_irq_save (flags);
769 ifxmips_w32(bits, IFXMIPS_SSC_CON);
770 ifxmips_w32((info->opts.gpoCs << IFX_SSC_GPOCON_ISCSB0_POS) |
771 (info->opts.gpoInv << IFX_SSC_GPOCON_INVOUT0_POS), IFXMIPS_SSC_GPOCON);
773 ifxmips_w32(info->opts.gpoCs << IFX_SSC_WHBGPOSTAT_SETOUT0_POS, IFXMIPS_SSC_WHBGPOSTAT);
776 if (opts->masterSelect)
777 ifxmips_w32(IFX_SSC_WHBSTATE_SET_MASTER_SELECT, IFXMIPS_SSC_WHBSTATE);
779 ifxmips_w32(IFX_SSC_WHBSTATE_CLR_MASTER_SELECT, IFXMIPS_SSC_WHBSTATE);
781 // init serial framing
782 ifxmips_w32(0, IFXMIPS_SSC_SFCON);
783 /* set up the port pins */
784 //check for general requirements to switch (external) pad/pin characteristics
785 /* TODO: P0.9 SPI_CS4, P0.10 SPI_CS5, P 0.11 SPI_CS6, because of ASC0 */
786 /* p0.15 SPI_CS1(EEPROM), P0.13 SPI_CS3, */
787 /* Set p0.15 to alternative 01, others to 00 (In/OUT) */
788 *(IFXMIPS_GPIO_P0_DIR) = (*IFXMIPS_GPIO_P0_DIR) | (0xA000);
789 *(IFXMIPS_GPIO_P0_ALTSEL0) = (((*IFXMIPS_GPIO_P0_ALTSEL0) | (0x8000)) & (~(0x2000)));
790 *(IFXMIPS_GPIO_P0_ALTSEL1) = (((*IFXMIPS_GPIO_P0_ALTSEL1) & (~0x8000)) & (~(0x2000)));
791 *(IFXMIPS_GPIO_P0_OD) = (*IFXMIPS_GPIO_P0_OD) | 0xA000;
793 /* p1.6 SPI_CS2(SFLASH), p1.0 SPI_DIN, p1.1 SPI_DOUT, p1.2 SPI_CLK */
794 *(IFXMIPS_GPIO_P1_DIR) = ((*IFXMIPS_GPIO_P1_DIR) | (0x46)) & (~1);
795 *(IFXMIPS_GPIO_P1_ALTSEL0) = ((*IFXMIPS_GPIO_P1_ALTSEL0) | (0x47));
796 *(IFXMIPS_GPIO_P1_ALTSEL1) = (*IFXMIPS_GPIO_P1_ALTSEL1) & (~0x47);
797 *(IFXMIPS_GPIO_P1_OD) = (*IFXMIPS_GPIO_P1_OD) | 0x0046;
800 /*TODO: CS4 CS5 CS6 */
801 *IFXMIPS_GPIO_P0_OUT = ((*IFXMIPS_GPIO_P0_OUT) | 0x2000);
803 local_irq_restore (flags);
809 ifx_ssc_set_baud (struct ifx_ssc_port *info, unsigned int baud)
811 unsigned int ifx_ssc_clock;
817 ifx_ssc_clock = ifx_ssc_get_kernel_clk(info);
818 if (ifx_ssc_clock == 0)
824 local_irq_save (flags);
826 enabled = (ifxmips_r32(IFXMIPS_SSC_STATE) & IFX_SSC_STATE_IS_ENABLED);
827 ifxmips_w32(IFX_SSC_WHBSTATE_CLR_ENABLE, IFXMIPS_SSC_WHBSTATE);
829 br = (((ifx_ssc_clock >> 1) + baud / 2) / baud) - 1;
832 if (br > 0xffff || ((br == 0) &&
833 ((ifxmips_r32(IFXMIPS_SSC_STATE) & IFX_SSC_STATE_IS_MASTER) == 0))) {
834 local_irq_restore (flags);
835 printk ("%s: invalid baudrate %u\n", __func__, baud);
839 ifxmips_w32(br, IFXMIPS_SSC_BR);
842 ifxmips_w32(IFX_SSC_WHBSTATE_SET_ENABLE, IFXMIPS_SSC_WHBSTATE);
844 local_irq_restore(flags);
851 ifx_ssc_hwinit (struct ifx_ssc_port *info)
856 enabled = (ifxmips_r32(IFXMIPS_SSC_STATE) & IFX_SSC_STATE_IS_ENABLED);
857 ifxmips_w32(IFX_SSC_WHBSTATE_CLR_ENABLE, IFXMIPS_SSC_WHBSTATE);
859 if (ifx_ssc_sethwopts (info) < 0)
861 printk ("%s: setting the hardware options failed\n", __func__);
865 if (ifx_ssc_set_baud (info, info->baud) < 0)
867 printk ("%s: setting the baud rate failed\n", __func__);
871 local_irq_save (flags);
874 ifxmips_w32((IFX_SSC_DEF_TXFIFO_FL << IFX_SSC_XFCON_ITL_OFFSET) | IFX_SSC_XFCON_FIFO_ENABLE, IFXMIPS_SSC_TXFCON);
876 ifxmips_w32((IFX_SSC_DEF_RXFIFO_FL << IFX_SSC_XFCON_ITL_OFFSET) | IFX_SSC_XFCON_FIFO_ENABLE, IFXMIPS_SSC_RXFCON);
878 local_irq_restore (flags);
881 ifxmips_w32(IFX_SSC_WHBSTATE_SET_ENABLE, IFXMIPS_SSC_WHBSTATE);
887 ifx_ssc_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long data)
889 struct ifx_ssc_port *info;
890 int line, ret_val = 0;
895 if ((inode == (struct inode *) 0) || (inode == (struct inode *) 1))
900 line = MINOR (filp->f_dentry->d_inode->i_rdev);
903 if (line < 0 || line >= PORT_CNT)
910 case IFX_SSC_STATS_READ:
911 /* data must be a pointer to a struct ifx_ssc_statistics */
913 memcpy ((void *) data, (void *) &info->stats,
914 sizeof (struct ifx_ssc_statistics));
915 else if (copy_to_user ((void *) data,
916 (void *) &info->stats,
917 sizeof (struct ifx_ssc_statistics)))
920 case IFX_SSC_STATS_RESET:
921 /* just resets the statistics counters */
922 memset ((void *) &info->stats, 0,
923 sizeof (struct ifx_ssc_statistics));
925 case IFX_SSC_BAUD_SET:
926 /* if the buffers are not empty then the port is */
927 /* busy and we shouldn't change things on-the-fly! */
928 if (!info->txbuf || !info->rxbuf ||
929 (ifxmips_r32(IFXMIPS_SSC_STATE) & IFX_SSC_STATE_BUSY)) {
935 flags = *((unsigned long *) data);
936 else if (copy_from_user ((void *) &flags,
937 (void *) data, sizeof (flags))) {
945 if (ifx_ssc_set_baud (info, flags) < 0) {
951 case IFX_SSC_BAUD_GET:
953 *((unsigned int *) data) = info->baud;
954 else if (copy_to_user ((void *) data,
955 (void *) &info->baud,
956 sizeof (unsigned long)))
959 case IFX_SSC_RXTX_MODE_SET:
961 tmp = *((unsigned long *) data);
962 else if (copy_from_user ((void *) &tmp,
963 (void *) data, sizeof (tmp))) {
967 ret_val = ifx_ssc_rxtx_mode_set (info, tmp);
969 case IFX_SSC_RXTX_MODE_GET:
970 tmp = ifxmips_r32(IFXMIPS_SSC_CON) &
971 (~(IFX_SSC_CON_RX_OFF | IFX_SSC_CON_TX_OFF));
973 *((unsigned int *) data) = tmp;
974 else if (copy_to_user ((void *) data,
975 (void *) &tmp, sizeof (tmp)))
980 ifx_ssc_abort (info);
983 case IFX_SSC_GPO_OUT_SET:
985 tmp = *((unsigned long *) data);
986 else if (copy_from_user ((void *) &tmp,
987 (void *) data, sizeof (tmp))) {
991 if (tmp > IFX_SSC_MAX_GPO_OUT)
994 ifxmips_w32(1 << (tmp + IFX_SSC_WHBGPOSTAT_SETOUT0_POS),
995 IFXMIPS_SSC_WHBGPOSTAT);
997 case IFX_SSC_GPO_OUT_CLR:
999 tmp = *((unsigned long *) data);
1000 else if (copy_from_user ((void *) &tmp, (void *) data, sizeof (tmp))) {
1004 if (tmp > IFX_SSC_MAX_GPO_OUT)
1007 ifxmips_w32(1 << (tmp + IFX_SSC_WHBGPOSTAT_CLROUT0_POS),
1008 IFXMIPS_SSC_WHBGPOSTAT);
1011 case IFX_SSC_GPO_OUT_GET:
1012 tmp = ifxmips_r32(IFXMIPS_SSC_GPOSTAT);
1014 *((unsigned int *) data) = tmp;
1015 else if (copy_to_user ((void *) data,
1016 (void *) &tmp, sizeof (tmp)))
1019 case IFX_SSC_FRM_STATUS_GET:
1020 ifx_ssc_frm_status_get (info);
1022 memcpy ((void *) data, (void *) &info->frm_status,
1023 sizeof (struct ifx_ssc_frm_status));
1024 else if (copy_to_user ((void *) data,
1025 (void *) &info->frm_status,
1026 sizeof (struct ifx_ssc_frm_status)))
1029 case IFX_SSC_FRM_CONTROL_GET:
1030 ifx_ssc_frm_control_get (info);
1032 memcpy ((void *) data, (void *) &info->frm_opts,
1033 sizeof (struct ifx_ssc_frm_opts));
1034 else if (copy_to_user ((void *) data,
1035 (void *) &info->frm_opts,
1036 sizeof (struct ifx_ssc_frm_opts)))
1039 case IFX_SSC_FRM_CONTROL_SET:
1041 memcpy ((void *) &info->frm_opts, (void *) data,
1042 sizeof (struct ifx_ssc_frm_opts));
1043 else if (copy_to_user ((void *) &info->frm_opts,
1045 sizeof (struct ifx_ssc_frm_opts))) {
1049 ret_val = ifx_ssc_frm_control_set (info);
1051 case IFX_SSC_HWOPTS_SET:
1052 /* data must be a pointer to a struct ifx_ssc_hwopts */
1053 /* if the buffers are not empty then the port is */
1054 /* busy and we shouldn't change things on-the-fly! */
1055 if (!info->txbuf || !info->rxbuf ||
1056 (ifxmips_r32(IFXMIPS_SSC_STATE)
1057 & IFX_SSC_STATE_BUSY)) {
1062 memcpy ((void *) &info->opts, (void *) data,
1063 sizeof (struct ifx_ssc_hwopts));
1064 else if (copy_from_user ((void *) &info->opts,
1065 (void *) data, sizeof(struct ifx_ssc_hwopts))) {
1069 if (ifx_ssc_hwinit (info) < 0) {
1073 case IFX_SSC_HWOPTS_GET:
1074 /* data must be a pointer to a struct ifx_ssc_hwopts */
1076 memcpy ((void *) data, (void *) &info->opts,
1077 sizeof (struct ifx_ssc_hwopts));
1078 else if (copy_to_user ((void *) data,
1079 (void *) &info->opts,
1080 sizeof (struct ifx_ssc_hwopts)))
1084 ret_val = -ENOIOCTLCMD;
1089 EXPORT_SYMBOL(ifx_ssc_ioctl);
1091 static struct file_operations ifx_ssc_fops = {
1092 .owner = THIS_MODULE,
1093 .read = ifx_ssc_read,
1094 .write = ifx_ssc_write,
1095 .ioctl = ifx_ssc_ioctl,
1096 .open = ifx_ssc_open,
1097 .release = ifx_ssc_close,
1103 struct ifx_ssc_port *info;
1105 unsigned long flags;
1109 nbytes = PORT_CNT * sizeof(struct ifx_ssc_port);
1110 isp = (struct ifx_ssc_port*)kmalloc(nbytes, GFP_KERNEL);
1114 printk("%s: no memory for isp\n", __func__);
1117 memset(isp, 0, nbytes);
1120 if ((i = register_chrdev (maj, "ssc", &ifx_ssc_fops)) < 0)
1122 printk ("Unable to register major %d for the Infineon SSC\n", maj);
1128 if ((i = register_chrdev (maj, "ssc", &ifx_ssc_fops)) < 0)
1130 printk ("Unable to register major %d for the Infineon SSC\n", maj);
1139 /* set default values in ifx_ssc_port */
1140 for (i = 0; i < PORT_CNT; i++) {
1143 /* default values for the HwOpts */
1144 info->opts.AbortErrDetect = IFX_SSC_DEF_ABRT_ERR_DETECT;
1145 info->opts.rxOvErrDetect = IFX_SSC_DEF_RO_ERR_DETECT;
1146 info->opts.rxUndErrDetect = IFX_SSC_DEF_RU_ERR_DETECT;
1147 info->opts.txOvErrDetect = IFX_SSC_DEF_TO_ERR_DETECT;
1148 info->opts.txUndErrDetect = IFX_SSC_DEF_TU_ERR_DETECT;
1149 info->opts.loopBack = IFX_SSC_DEF_LOOP_BACK;
1150 info->opts.echoMode = IFX_SSC_DEF_ECHO_MODE;
1151 info->opts.idleValue = IFX_SSC_DEF_IDLE_DATA;
1152 info->opts.clockPolarity = IFX_SSC_DEF_CLOCK_POLARITY;
1153 info->opts.clockPhase = IFX_SSC_DEF_CLOCK_PHASE;
1154 info->opts.headingControl = IFX_SSC_DEF_HEADING_CONTROL;
1155 info->opts.dataWidth = IFX_SSC_DEF_DATA_WIDTH;
1156 info->opts.modeRxTx = IFX_SSC_DEF_MODE_RXTX;
1157 info->opts.gpoCs = IFX_SSC_DEF_GPO_CS;
1158 info->opts.gpoInv = IFX_SSC_DEF_GPO_INV;
1159 info->opts.masterSelect = IFX_SSC_DEF_MASTERSLAVE;
1160 info->baud = IFX_SSC_DEF_BAUDRATE;
1163 /* values specific to SSC1 */
1165 info->mapbase = IFXMIPS_SSC_BASE_ADDR;
1168 ifxmips_w32(IFX_SSC_DEF_RMC << IFX_CLC_RUN_DIVIDER_OFFSET, IFXMIPS_SSC_CLC);
1170 init_waitqueue_head (&info->rwait);
1172 local_irq_save (flags);
1174 // init serial framing register
1175 ifxmips_w32(IFX_SSC_DEF_SFCON, IFXMIPS_SSC_SFCON);
1177 ret_val = request_irq(IFXMIPS_SSC_TIR, ifx_ssc_tx_int, IRQF_DISABLED, "ifx_ssc_tx", info);
1180 printk("%s: unable to get irq %d\n", __func__, IFXMIPS_SSC_TIR);
1181 local_irq_restore(flags);
1185 ret_val = request_irq(IFXMIPS_SSC_RIR, ifx_ssc_rx_int, IRQF_DISABLED, "ifx_ssc_rx", info);
1188 printk ("%s: unable to get irq %d\n", __func__, IFXMIPS_SSC_RIR);
1189 local_irq_restore (flags);
1193 ret_val = request_irq(IFXMIPS_SSC_EIR, ifx_ssc_err_int, IRQF_DISABLED, "ifx_ssc_err", info);
1196 printk ("%s: unable to get irq %d\n", __func__, IFXMIPS_SSC_EIR);
1197 local_irq_restore (flags);
1200 ifxmips_w32(IFX_SSC_DEF_IRNEN, IFXMIPS_SSC_IRN);
1202 //enable_irq(IFXMIPS_SSC_TIR);
1203 //enable_irq(IFXMIPS_SSC_RIR);
1204 //enable_irq(IFXMIPS_SSC_EIR);
1206 local_irq_restore (flags);
1209 for (i = 0; i < PORT_CNT; i++) {
1211 if (ifx_ssc_hwinit (info) < 0)
1213 printk ("%s: hardware init failed for port %d\n", __func__, i);
1222 free_irq(IFXMIPS_SSC_TIR, &isp[0]);
1223 free_irq(IFXMIPS_SSC_RIR, &isp[0]);
1224 free_irq(IFXMIPS_SSC_EIR, &isp[0]);
1231 ifx_ssc_cleanup_module (void)
1235 for (i = 0; i < PORT_CNT; i++) {
1236 ifxmips_w32(IFX_SSC_WHBSTATE_CLR_ENABLE, IFXMIPS_SSC_WHBSTATE);
1237 free_irq(IFXMIPS_SSC_TIR, &isp[i]);
1238 free_irq(IFXMIPS_SSC_RIR, &isp[i]);
1239 free_irq(IFXMIPS_SSC_EIR, &isp[i]);
1244 module_init(ifx_ssc_init);
1245 module_exit(ifx_ssc_cleanup_module);
1249 ifx_ssc_cs_low (u32 pin)
1252 if ((ret = ifx_ssc_ioctl ((struct inode *) 0, NULL, IFX_SSC_GPO_OUT_CLR, (unsigned long) &pin)))
1253 printk ("clear CS %d fails\n", pin);
1258 EXPORT_SYMBOL(ifx_ssc_cs_low);
1261 ifx_ssc_cs_high (u32 pin)
1264 if ((ret = ifx_ssc_ioctl((struct inode *) 0, NULL, IFX_SSC_GPO_OUT_SET, (unsigned long) &pin)))
1265 printk ("set CS %d fails\n", pin);
1270 EXPORT_SYMBOL(ifx_ssc_cs_high);
1273 ssc_session (char *tx_buf, u32 tx_len, char *rx_buf, u32 rx_len)
1277 char *ssc_tx_buf = NULL;
1278 char *ssc_rx_buf = NULL;
1282 if (tx_buf == NULL && tx_len == 0 && rx_buf == NULL && rx_len == 0) {
1283 printk ("invalid parameters\n");
1285 goto ssc_session_exit;
1287 else if (tx_buf == NULL || tx_len == 0) {
1288 if (rx_buf != NULL && rx_len != 0) {
1289 mode = IFX_SSC_MODE_RX;
1292 printk ("invalid parameters\n");
1294 goto ssc_session_exit;
1297 else if (rx_buf == NULL || rx_len == 0) {
1298 if (tx_buf != NULL && tx_len != 0) {
1299 mode = IFX_SSC_MODE_TX;
1302 printk ("invalid parameters\n");
1304 goto ssc_session_exit;
1308 mode = IFX_SSC_MODE_RXTX;
1311 if (mode == IFX_SSC_MODE_RXTX) {
1312 eff_size = tx_len + rx_len;
1314 else if (mode == IFX_SSC_MODE_RX) {
1321 //4 bytes alignment, required by driver
1322 /* change by TaiCheng */
1326 (char *) kmalloc (sizeof (char) *
1327 ((eff_size + 3) & (~3)),
1330 (char *) kmalloc (sizeof (char) *
1331 ((eff_size + 3) & (~3)),
1336 (char *) kmalloc (sizeof (char) *
1337 ((eff_size + 3) & (~3)),
1340 (char *) kmalloc (sizeof (char) *
1341 ((eff_size + 3) & (~3)),
1344 if (ssc_tx_buf == NULL || ssc_rx_buf == NULL) {
1345 printk ("no memory for size of %d\n", eff_size);
1347 goto ssc_session_exit;
1349 memset ((void *) ssc_tx_buf, 0, eff_size);
1350 memset ((void *) ssc_rx_buf, 0, eff_size);
1353 memcpy (ssc_tx_buf, tx_buf, tx_len);
1356 ret = ifx_ssc_kwrite (0, ssc_tx_buf, eff_size);
1359 ssc_tx_buf = NULL; //should be freed by ifx_ssc_kwrite
1362 if (ret != eff_size) {
1363 printk ("ifx_ssc_write return %d\n", ret);
1364 goto ssc_session_exit;
1366 ret = ifx_ssc_kread (0, ssc_rx_buf, eff_size);
1367 if (ret != eff_size) {
1368 printk ("ifx_ssc_read return %d\n", ret);
1369 goto ssc_session_exit;
1372 memcpy (rx_buf, ssc_rx_buf + tx_len, rx_len);
1374 if (mode == IFX_SSC_MODE_TX) {
1382 if (ssc_tx_buf != NULL)
1384 if (ssc_rx_buf != NULL)
1388 printk ("ssc session fails\n");
1394 ifx_ssc_txrx (char *tx_buf, u32 tx_len, char *rx_buf, u32 rx_len)
1396 return ssc_session(tx_buf, tx_len, rx_buf, rx_len);
1398 EXPORT_SYMBOL(ifx_ssc_txrx);
1401 ifx_ssc_tx (char *tx_buf, u32 tx_len)
1403 return ssc_session(tx_buf, tx_len, NULL, 0);
1405 EXPORT_SYMBOL(ifx_ssc_tx);
1408 ifx_ssc_rx (char *rx_buf, u32 rx_len)
1410 return ssc_session(NULL, 0, rx_buf, rx_len);
1412 EXPORT_SYMBOL(ifx_ssc_rx);
1414 MODULE_LICENSE("GPL");
1415 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
1416 MODULE_DESCRIPTION("ifxmips ssc driver");