remove /proc from ifxmips_ssc
[openwrt.git] / target / linux / ifxmips / files / drivers / char / ifxmips_ssc.c
1 /*
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.
6  *
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.
11  *
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.
15  *
16  *   Copyright (C) 2006 infineon
17  *   Copyright (C) 2007 John Crispin <blogic@openwrt.org> 
18  *
19  */
20
21 // ### TO DO: general issues:
22 //            - power management
23 //            - interrupt handling (direct/indirect)
24 //            - pin/mux-handling (just overall concept due to project dependency)
25 //            - multiple instances capability
26 //            - slave functionality
27
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>
36 #include <linux/fs.h>
37 #include <linux/fcntl.h>
38 #include <linux/ptrace.h>
39 #include <linux/mm.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>
45
46 #include <asm/system.h>
47 #include <asm/io.h>
48 #include <asm/irq.h>
49 #include <asm/uaccess.h>
50 #include <asm/bitops.h>
51
52 #include <linux/types.h>
53 #include <linux/kernel.h>
54 #include <linux/version.h>
55
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>
60
61 #ifdef SSC_FRAME_INT_ENABLE
62 #undef SSC_FRAME_INT_ENABLE
63 #endif
64
65 #define not_yet
66
67 #define SPI_VINETIC
68
69
70
71 /* allow the user to set the major device number */
72 static int maj = 0;
73
74 /*
75  * This is the per-channel data structure containing pointers, flags
76  * and variables for the port. This driver supports a maximum of PORT_CNT.
77  * isp is allocated in ifx_ssc_init() based on the chip version.
78  */
79 static struct ifx_ssc_port *isp;
80
81 /* prototypes for fops */
82 static ssize_t ifx_ssc_read (struct file *, char *, size_t, loff_t *);
83 static ssize_t ifx_ssc_write (struct file *, const char *, size_t, loff_t *);
84 //static unsigned int ifx_ssc_poll(struct file *, struct poll_table_struct *);
85 int ifx_ssc_ioctl (struct inode *, struct file *, unsigned int,
86                    unsigned long);
87 int ifx_ssc_open (struct inode *, struct file *);
88 int ifx_ssc_close (struct inode *, struct file *);
89
90 /* other forward declarations */
91 static unsigned int ifx_ssc_get_kernel_clk (struct ifx_ssc_port *info);
92 static void tx_int (struct ifx_ssc_port *);
93
94 extern unsigned int ifxmips_get_fpi_hz (void);
95 extern void mask_and_ack_ifxmips_irq (unsigned int irq_nr);
96
97 static struct file_operations ifx_ssc_fops = {
98       .owner = THIS_MODULE,
99       .read = ifx_ssc_read,
100       .write = ifx_ssc_write,
101       .ioctl = ifx_ssc_ioctl,
102       .open = ifx_ssc_open,
103       .release = ifx_ssc_close,
104 };
105
106 static inline unsigned int
107 ifx_ssc_get_kernel_clk (struct ifx_ssc_port *info)
108 {
109         unsigned int rmc;
110
111         rmc = (READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_CLC) & IFX_CLC_RUN_DIVIDER_MASK) >> IFX_CLC_RUN_DIVIDER_OFFSET;
112         if (rmc == 0)
113         {
114                 printk ("ifx_ssc_get_kernel_clk rmc==0 \n");
115                 return 0;
116         }
117         return ifxmips_get_fpi_hz () / rmc;
118 }
119
120 #ifndef not_yet
121 #ifdef IFX_SSC_INT_USE_BH
122 /*
123  * This routine is used by the interrupt handler to schedule
124  * processing in the software interrupt portion of the driver
125  * (also known as the "bottom half").  This can be called any
126  * number of times for any channel without harm.
127  */
128 static inline void
129 ifx_ssc_sched_event (struct ifx_ssc_port *info, int event)
130 {
131         info->event |= 1 << event;      /* remember what kind of event and who */
132         queue_task (&info->tqueue, &tq_cyclades);       /* it belongs to */
133         mark_bh (CYCLADES_BH);  /* then trigger event */
134 }
135
136 static void
137 do_softint (void *private_)
138 {
139         struct ifx_ssc_port *info = (struct ifx_ssc_port *) private_;
140
141         if (test_and_clear_bit (Cy_EVENT_HANGUP, &info->event))
142         {
143                 wake_up_interruptible (&info->open_wait);
144                 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
145         }
146
147         if (test_and_clear_bit (Cy_EVENT_OPEN_WAKEUP, &info->event))
148                 wake_up_interruptible (&info->open_wait);
149
150         if (test_and_clear_bit (Cy_EVENT_DELTA_WAKEUP, &info->event))
151                 wake_up_interruptible (&info->delta_msr_wait);
152
153         if (test_and_clear_bit (Cy_EVENT_WRITE_WAKEUP, &info->event))
154                 wake_up_interruptible (&tty->write_wait);
155 #ifdef Z_WAKE
156         if (test_and_clear_bit (Cy_EVENT_SHUTDOWN_WAKEUP, &info->event))
157                 wake_up_interruptible (&info->shutdown_wait);
158 #endif
159 }
160 #endif
161 #endif
162
163 inline static void
164 rx_int (struct ifx_ssc_port *info)
165 {
166         int fifo_fill_lev, bytes_in_buf, i;
167         unsigned long tmp_val;
168         unsigned long *tmp_ptr;
169         unsigned int rx_valid_cnt;
170         /* number of words waiting in the RX FIFO */
171         fifo_fill_lev = (READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_FSTAT) & IFX_SSC_FSTAT_RECEIVED_WORDS_MASK) >> IFX_SSC_FSTAT_RECEIVED_WORDS_OFFSET;
172         // Note: There are always 32 bits in a fifo-entry except for the last 
173         // word of a contigous transfer block and except for not in rx-only 
174         // mode and CON.ENBV set. But for this case it should be a convention 
175         // in software which helps:
176         // In tx or rx/tx mode all transfers from the buffer to the FIFO are 
177         // 32-bit wide, except for the last three bytes, which could be a 
178         // combination of 16- and 8-bit access.
179         // => The whole block is received as 32-bit words as a contigous stream, 
180         // even if there was a gap in tx which has the fifo run out of data! 
181         // Just the last fifo entry *may* be partially filled (0, 1, 2 or 3 bytes)!
182
183         /* free space in the RX buffer */
184         bytes_in_buf = info->rxbuf_end - info->rxbuf_ptr;
185         // transfer with 32 bits per entry
186         while ((bytes_in_buf >= 4) && (fifo_fill_lev > 0)) {
187                 tmp_ptr = (unsigned long *) info->rxbuf_ptr;
188                 *tmp_ptr = READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_RB);
189                 info->rxbuf_ptr += 4;
190                 info->stats.rxBytes += 4;
191                 fifo_fill_lev--;
192                 bytes_in_buf -= 4;
193         }
194
195         // now do the rest as mentioned in STATE.RXBV
196         while ((bytes_in_buf > 0) && (fifo_fill_lev > 0)) {
197                 rx_valid_cnt = (READ_PERIPHERAL_REGISTER(info->mapbase + IFX_SSC_STATE) & IFX_SSC_STATE_RX_BYTE_VALID_MASK) >> IFX_SSC_STATE_RX_BYTE_VALID_OFFSET;
198                 if (rx_valid_cnt == 0)
199                         break;
200
201                 if (rx_valid_cnt > bytes_in_buf)
202                         rx_valid_cnt = bytes_in_buf;
203
204                 tmp_val = READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_RB);
205
206                 for (i = 0; i < rx_valid_cnt; i++)
207                 {
208                         *info->rxbuf_ptr = (tmp_val >> (8 * (rx_valid_cnt - i - 1))) & 0xff;
209                         bytes_in_buf--;
210                         info->rxbuf_ptr++;
211                 }
212                 info->stats.rxBytes += rx_valid_cnt;
213         }
214
215         // check if transfer is complete
216         if (info->rxbuf_ptr >= info->rxbuf_end)
217         {
218                 disable_irq(info->rxirq);
219                 wake_up_interruptible (&info->rwait);
220         } else if ((info->opts.modeRxTx == IFX_SSC_MODE_RX) && (READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_RXCNT) == 0))
221         {
222                 if (info->rxbuf_end - info->rxbuf_ptr < IFX_SSC_RXREQ_BLOCK_SIZE)
223                         WRITE_PERIPHERAL_REGISTER ((info->rxbuf_end - info->rxbuf_ptr) << IFX_SSC_RXREQ_RXCOUNT_OFFSET, info->mapbase + IFX_SSC_RXREQ);
224                 else
225                         WRITE_PERIPHERAL_REGISTER (IFX_SSC_RXREQ_BLOCK_SIZE << IFX_SSC_RXREQ_RXCOUNT_OFFSET,  info->mapbase + IFX_SSC_RXREQ);
226         }
227 }
228
229 inline static void
230 tx_int (struct ifx_ssc_port *info)
231 {
232
233         int fifo_space, fill, i;
234         fifo_space = ((READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_ID) & IFX_SSC_PERID_TXFS_MASK) >> IFX_SSC_PERID_TXFS_OFFSET)
235                 - ((READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_FSTAT) & IFX_SSC_FSTAT_TRANSMIT_WORDS_MASK) >> IFX_SSC_FSTAT_TRANSMIT_WORDS_OFFSET);
236
237         if (fifo_space == 0)
238                 return;
239
240         fill = info->txbuf_end - info->txbuf_ptr;
241
242         if (fill > fifo_space * 4)
243                 fill = fifo_space * 4;
244
245         for (i = 0; i < fill / 4; i++)
246         {
247                 // at first 32 bit access
248                 WRITE_PERIPHERAL_REGISTER (*(UINT32 *) info->txbuf_ptr, info->mapbase + IFX_SSC_TB);
249                 info->txbuf_ptr += 4;
250         }
251
252         fifo_space -= fill / 4;
253         info->stats.txBytes += fill & ~0x3;
254         fill &= 0x3;
255         if ((fifo_space > 0) & (fill > 1))
256         {
257                 // trailing 16 bit access
258                 WRITE_PERIPHERAL_REGISTER_16 (*(UINT16 *) info->txbuf_ptr, info->mapbase + IFX_SSC_TB);
259                 info->txbuf_ptr += 2;
260                 info->stats.txBytes += 2;
261                 fifo_space--;
262                 fill -= 2;
263         }
264
265         if ((fifo_space > 0) & (fill > 0))
266         {
267                 // trailing 8 bit access
268                 WRITE_PERIPHERAL_REGISTER_8 (*(UINT8 *) info->txbuf_ptr, info->mapbase + IFX_SSC_TB);
269                 info->txbuf_ptr++;
270                 info->stats.txBytes++;
271         }
272
273         // check if transmission complete
274         if (info->txbuf_ptr >= info->txbuf_end)
275         {
276                 disable_irq(info->txirq);
277                 kfree (info->txbuf);
278                 info->txbuf = NULL;
279         }
280
281 }
282
283 irqreturn_t
284 ifx_ssc_rx_int (int irq, void *dev_id)
285 {
286         struct ifx_ssc_port *info = (struct ifx_ssc_port *) dev_id;
287         rx_int (info);
288
289         return IRQ_HANDLED;
290 }
291
292 irqreturn_t
293 ifx_ssc_tx_int (int irq, void *dev_id)
294 {
295         struct ifx_ssc_port *info = (struct ifx_ssc_port *) dev_id;
296         tx_int (info);
297
298         return IRQ_HANDLED;
299 }
300
301 irqreturn_t
302 ifx_ssc_err_int (int irq, void *dev_id)
303 {
304         struct ifx_ssc_port *info = (struct ifx_ssc_port *) dev_id;
305         unsigned int state;
306         unsigned int write_back = 0;
307         unsigned long flags;
308
309         local_irq_save (flags);
310         state = READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_STATE);
311
312         if ((state & IFX_SSC_STATE_RX_UFL) != 0) {
313                 info->stats.rxUnErr++;
314                 write_back |= IFX_SSC_WHBSTATE_CLR_RX_UFL_ERROR;
315         }
316
317         if ((state & IFX_SSC_STATE_RX_OFL) != 0) {
318                 info->stats.rxOvErr++;
319                 write_back |= IFX_SSC_WHBSTATE_CLR_RX_OFL_ERROR;
320         }
321
322         if ((state & IFX_SSC_STATE_TX_OFL) != 0) {
323                 info->stats.txOvErr++;
324                 write_back |= IFX_SSC_WHBSTATE_CLR_TX_OFL_ERROR;
325         }
326
327         if ((state & IFX_SSC_STATE_TX_UFL) != 0) {
328                 info->stats.txUnErr++;
329                 write_back |= IFX_SSC_WHBSTATE_CLR_TX_UFL_ERROR;
330         }
331
332         if ((state & IFX_SSC_STATE_MODE_ERR) != 0) {
333                 info->stats.modeErr++;
334                 write_back |= IFX_SSC_WHBSTATE_CLR_MODE_ERROR;
335         }
336
337         if (write_back)
338                 WRITE_PERIPHERAL_REGISTER (write_back, info->mapbase + IFX_SSC_WHBSTATE);
339
340         local_irq_restore (flags);
341
342         return IRQ_HANDLED;
343 }
344
345 static void
346 ifx_ssc_abort (struct ifx_ssc_port *info)
347 {
348         unsigned long flags;
349         bool enabled;
350
351         local_irq_save (flags);
352
353         disable_irq(info->rxirq);
354         disable_irq(info->txirq);
355         disable_irq(info->errirq);
356
357         local_irq_restore (flags);
358
359         // disable SSC (also aborts a receive request!)
360         // ### TO DO: Perhaps it's better to abort after the receiption of a 
361         // complete word. The disable cuts the transmission immediatly and 
362         // releases the chip selects. This could result in unpredictable 
363         // behavior of connected external devices!
364         enabled = (READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_STATE) & IFX_SSC_STATE_IS_ENABLED) != 0;
365         WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ENABLE, info->mapbase + IFX_SSC_WHBSTATE);
366
367         // flush fifos
368         WRITE_PERIPHERAL_REGISTER (IFX_SSC_XFCON_FIFO_FLUSH, info->mapbase + IFX_SSC_TXFCON);
369         WRITE_PERIPHERAL_REGISTER (IFX_SSC_XFCON_FIFO_FLUSH, info->mapbase + IFX_SSC_RXFCON);
370
371         // free txbuf
372         if (info->txbuf != NULL)
373         {
374                 kfree (info->txbuf);
375                 info->txbuf = NULL;
376         }
377
378         // wakeup read process
379         if (info->rxbuf != NULL)
380                 wake_up_interruptible (&info->rwait);
381
382         // clear pending int's 
383         mask_and_ack_ifxmips_irq(info->rxirq);
384         mask_and_ack_ifxmips_irq(info->txirq);
385         mask_and_ack_ifxmips_irq(info->errirq);
386
387         // clear error flags
388         WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ALL_ERROR, info->mapbase + IFX_SSC_WHBSTATE);
389
390         if (enabled)
391                 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_SET_ENABLE, info->mapbase + IFX_SSC_WHBSTATE);
392
393 }
394
395 /*
396  * This routine is called whenever a port is opened.  It enforces
397  * exclusive opening of a port and enables interrupts, etc.
398  */
399 int
400 ifx_ssc_open (struct inode *inode, struct file *filp)
401 {
402         struct ifx_ssc_port *info;
403         int line;
404         int from_kernel = 0;
405
406         if ((inode == (struct inode *) 0) || (inode == (struct inode *) 1)) {
407                 from_kernel = 1;
408                 line = (int) inode;
409         } else {
410                 line = MINOR (filp->f_dentry->d_inode->i_rdev);
411                 filp->f_op = &ifx_ssc_fops;
412         }
413
414         /* don't open more minor devices than we can support */
415         if (line < 0 || line >= PORT_CNT)
416                 return -ENXIO;
417
418         info = &isp[line];
419
420         /* exclusive open */
421         if (info->port_is_open != 0)
422                 return -EBUSY;
423         info->port_is_open++;
424
425         disable_irq(info->rxirq);
426         disable_irq(info->txirq);
427         disable_irq(info->errirq);
428
429         /* Flush and enable TX/RX FIFO */
430         WRITE_PERIPHERAL_REGISTER ((IFX_SSC_DEF_TXFIFO_FL << IFX_SSC_XFCON_ITL_OFFSET) | IFX_SSC_XFCON_FIFO_FLUSH | IFX_SSC_XFCON_FIFO_ENABLE, info->mapbase + IFX_SSC_TXFCON);
431         WRITE_PERIPHERAL_REGISTER ((IFX_SSC_DEF_RXFIFO_FL << IFX_SSC_XFCON_ITL_OFFSET) | IFX_SSC_XFCON_FIFO_FLUSH | IFX_SSC_XFCON_FIFO_ENABLE, info->mapbase + IFX_SSC_RXFCON);
432
433         /* logically flush the software FIFOs */
434         info->rxbuf_ptr = 0;
435         info->txbuf_ptr = 0;
436
437         /* clear all error bits */
438         WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ALL_ERROR, info->mapbase + IFX_SSC_WHBSTATE);
439
440         // clear pending interrupts
441         mask_and_ack_ifxmips_irq(info->rxirq);
442         mask_and_ack_ifxmips_irq(info->txirq);
443         mask_and_ack_ifxmips_irq(info->errirq);
444
445         WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_SET_ENABLE, info->mapbase + IFX_SSC_WHBSTATE);
446
447         return 0;
448 }
449 EXPORT_SYMBOL(ifx_ssc_open);
450
451 int
452 ifx_ssc_close (struct inode *inode, struct file *filp)
453 {
454         struct ifx_ssc_port *info;
455         int idx;
456
457         if ((inode == (struct inode *) 0) || (inode == (struct inode *) 1))
458                 idx = (int) inode;
459         else
460                 idx = MINOR (filp->f_dentry->d_inode->i_rdev);
461
462         if (idx < 0 || idx >= PORT_CNT)
463                 return -ENXIO;
464
465         info = &isp[idx];
466         if (!info)
467                 return -ENXIO;
468
469         WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ENABLE, info->mapbase + IFX_SSC_WHBSTATE);
470
471         ifx_ssc_abort(info);
472
473         info->port_is_open--;
474
475         return 0;
476 }
477 EXPORT_SYMBOL(ifx_ssc_close);
478
479 static ssize_t
480 ifx_ssc_read_helper_poll (struct ifx_ssc_port *info, char *buf, size_t len, int from_kernel)
481 {
482         ssize_t ret_val;
483         unsigned long flags;
484
485         if (info->opts.modeRxTx == IFX_SSC_MODE_TX)
486                 return -EFAULT;
487         local_irq_save (flags);
488         info->rxbuf_ptr = info->rxbuf;
489         info->rxbuf_end = info->rxbuf + len;
490         local_irq_restore (flags);
491         /* Vinetic driver always works in IFX_SSC_MODE_RXTX */
492         /* TXRX in poll mode */
493         while (info->rxbuf_ptr < info->rxbuf_end)
494         {
495                 if (info->txbuf_ptr < info->txbuf_end)
496                         tx_int (info);
497
498                 rx_int (info);
499         };
500
501         ret_val = info->rxbuf_ptr - info->rxbuf;
502
503         return ret_val;
504 }
505
506 static ssize_t
507 ifx_ssc_read_helper (struct ifx_ssc_port *info, char *buf, size_t len, int from_kernel)
508 {
509         ssize_t ret_val;
510         unsigned long flags;
511         DECLARE_WAITQUEUE (wait, current);
512
513         if (info->opts.modeRxTx == IFX_SSC_MODE_TX)
514                 return -EFAULT;
515
516         local_irq_save (flags);
517         info->rxbuf_ptr = info->rxbuf;
518         info->rxbuf_end = info->rxbuf + len;
519
520         if (info->opts.modeRxTx == IFX_SSC_MODE_RXTX)
521         {
522                 if ((info->txbuf == NULL) || (info->txbuf != info->txbuf_ptr) || (info->txbuf_end != len + info->txbuf))
523                 {
524                         local_irq_restore (flags);
525                         printk ("IFX SSC - %s: write must be called before calling " "read in combined RX/TX!\n", __func__);
526                         return -EFAULT;
527                 }
528
529                 local_irq_restore(flags);
530                 tx_int (info);
531
532                 if (info->txbuf_ptr < info->txbuf_end)
533                         enable_irq(info->txirq);
534
535                 enable_irq(info->rxirq);
536         } else {
537                 local_irq_restore(flags);
538                 if (READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_RXCNT) & IFX_SSC_RXCNT_TODO_MASK)
539                         return -EBUSY;
540                 enable_irq(info->rxirq);
541                 if (len < IFX_SSC_RXREQ_BLOCK_SIZE)
542                         WRITE_PERIPHERAL_REGISTER (len << IFX_SSC_RXREQ_RXCOUNT_OFFSET, info->mapbase + IFX_SSC_RXREQ);
543                 else
544                         WRITE_PERIPHERAL_REGISTER (IFX_SSC_RXREQ_BLOCK_SIZE << IFX_SSC_RXREQ_RXCOUNT_OFFSET, info->mapbase + IFX_SSC_RXREQ);
545         }
546
547         __add_wait_queue (&info->rwait, &wait);
548         set_current_state (TASK_INTERRUPTIBLE);
549
550         do {
551                 local_irq_save (flags);
552                 if (info->rxbuf_ptr >= info->rxbuf_end)
553                         break;
554
555                 local_irq_restore (flags);
556
557                 if (signal_pending (current))
558                 {
559                         ret_val = -ERESTARTSYS;
560                         goto out;
561                 }
562                 schedule();
563         } while (1);
564
565         ret_val = info->rxbuf_ptr - info->rxbuf;
566         local_irq_restore (flags);
567
568 out:
569         current->state = TASK_RUNNING;
570         __remove_wait_queue (&info->rwait, &wait);
571
572         return (ret_val);
573 }
574
575 static ssize_t
576 ifx_ssc_write_helper (struct ifx_ssc_port *info, const char *buf,
577                       size_t len, int from_kernel)
578 {
579         if (info->opts.modeRxTx == IFX_SSC_MODE_RX)
580                 return -EFAULT;
581
582         info->txbuf_ptr = info->txbuf;
583         info->txbuf_end = len + info->txbuf;
584         if (info->opts.modeRxTx == IFX_SSC_MODE_TX)
585         {
586                 tx_int (info);
587                 if (info->txbuf_ptr < info->txbuf_end)
588                 {
589                         enable_irq(info->txirq);
590                 }
591         }
592
593         return len;
594 }
595
596 ssize_t
597 ifx_ssc_kread (int port, char *kbuf, size_t len)
598 {
599         struct ifx_ssc_port *info;
600         ssize_t ret_val;
601
602         if (port < 0 || port >= PORT_CNT)
603                 return -ENXIO;
604
605         if (len == 0)
606                 return 0;
607
608         info = &isp[port];
609
610         if (info->rxbuf != NULL)
611         {
612                 printk ("SSC device busy\n");
613                 return -EBUSY;
614         }
615
616         info->rxbuf = kbuf;
617         if (info->rxbuf == NULL)
618         {
619                 printk ("SSC device error\n");
620                 return -EINVAL;
621         }
622
623         ret_val = ifx_ssc_read_helper_poll (info, kbuf, len, 1);
624         info->rxbuf = NULL;
625
626         disable_irq(info->rxirq);
627
628         return ret_val;
629 }
630 EXPORT_SYMBOL(ifx_ssc_kread);
631
632 ssize_t
633 ifx_ssc_kwrite (int port, const char *kbuf, size_t len)
634 {
635         struct ifx_ssc_port *info;
636         ssize_t ret_val;
637
638         if (port < 0 || port >= PORT_CNT)
639                 return -ENXIO;
640
641         if (len == 0)
642                 return 0;
643
644         info = &isp[port];
645
646         // check if transmission in progress
647         if (info->txbuf != NULL)
648                 return -EBUSY;
649
650         info->txbuf = (char *) kbuf;
651
652         ret_val = ifx_ssc_write_helper (info, info->txbuf, len, 1);
653
654         if (ret_val < 0)
655                 info->txbuf = NULL;
656
657         return ret_val;
658 }
659 EXPORT_SYMBOL(ifx_ssc_kwrite);
660
661 static ssize_t
662 ifx_ssc_read (struct file *filp, char *ubuf, size_t len, loff_t * off)
663 {
664         ssize_t ret_val;
665         int idx;
666         struct ifx_ssc_port *info;
667
668         idx = MINOR (filp->f_dentry->d_inode->i_rdev);
669         info = &isp[idx];
670
671         if (info->rxbuf != NULL)
672                 return -EBUSY;
673
674         info->rxbuf = kmalloc (len + 3, GFP_KERNEL);
675         if (info->rxbuf == NULL)
676                 return -ENOMEM;
677
678         ret_val = ifx_ssc_read_helper (info, info->rxbuf, len, 0);
679         if (copy_to_user ((void *) ubuf, info->rxbuf, ret_val) != 0)
680                 ret_val = -EFAULT;
681
682         disable_irq(info->rxirq);
683
684         kfree (info->rxbuf);
685         info->rxbuf = NULL;
686
687         return (ret_val);
688 }
689
690 static ssize_t
691 ifx_ssc_write (struct file *filp, const char *ubuf, size_t len, loff_t * off)
692 {
693         int idx;
694         struct ifx_ssc_port *info;
695         int ret_val;
696
697         if (len == 0)
698                 return (0);
699
700         idx = MINOR (filp->f_dentry->d_inode->i_rdev);
701         info = &isp[idx];
702
703         if (info->txbuf != NULL)
704                 return -EBUSY;
705
706         info->txbuf = kmalloc (len + 3, GFP_KERNEL);
707         if (info->txbuf == NULL)
708                 return -ENOMEM;
709
710         ret_val = copy_from_user (info->txbuf, ubuf, len);
711         if (ret_val == 0)
712                 ret_val = ifx_ssc_write_helper (info, info->txbuf, len, 0);
713         else
714                 ret_val = -EFAULT;
715
716         if (ret_val < 0)
717         {
718                 kfree (info->txbuf);
719                 info->txbuf = NULL;
720         }
721
722         return (ret_val);
723 }
724
725 static struct ifx_ssc_frm_status *
726 ifx_ssc_frm_status_get (struct ifx_ssc_port *info)
727 {
728         unsigned long tmp;
729
730         tmp = READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_SFSTAT);
731         info->frm_status.DataBusy = (tmp & IFX_SSC_SFSTAT_IN_DATA) > 0;
732         info->frm_status.PauseBusy = (tmp & IFX_SSC_SFSTAT_IN_PAUSE) > 0;
733         info->frm_status.DataCount = (tmp & IFX_SSC_SFSTAT_DATA_COUNT_MASK) >> IFX_SSC_SFSTAT_DATA_COUNT_OFFSET;
734         info->frm_status.PauseCount = (tmp & IFX_SSC_SFSTAT_PAUSE_COUNT_MASK) >> IFX_SSC_SFSTAT_PAUSE_COUNT_OFFSET;
735         tmp = READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_SFCON);
736         info->frm_status.EnIntAfterData = (tmp & IFX_SSC_SFCON_FIR_ENABLE_BEFORE_PAUSE) > 0;
737         info->frm_status.EnIntAfterPause = (tmp & IFX_SSC_SFCON_FIR_ENABLE_AFTER_PAUSE) > 0;
738
739         return &info->frm_status;
740 }
741
742
743 static struct ifx_ssc_frm_opts *
744 ifx_ssc_frm_control_get (struct ifx_ssc_port *info)
745 {
746         unsigned long tmp;
747
748         tmp = READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_SFCON);
749         info->frm_opts.FrameEnable = (tmp & IFX_SSC_SFCON_SF_ENABLE) > 0;
750         info->frm_opts.DataLength = (tmp & IFX_SSC_SFCON_DATA_LENGTH_MASK) >> IFX_SSC_SFCON_DATA_LENGTH_OFFSET;
751         info->frm_opts.PauseLength = (tmp & IFX_SSC_SFCON_PAUSE_LENGTH_MASK) >> IFX_SSC_SFCON_PAUSE_LENGTH_OFFSET;
752         info->frm_opts.IdleData = (tmp & IFX_SSC_SFCON_PAUSE_DATA_MASK) >> IFX_SSC_SFCON_PAUSE_DATA_OFFSET;
753         info->frm_opts.IdleClock = (tmp & IFX_SSC_SFCON_PAUSE_CLOCK_MASK) >> IFX_SSC_SFCON_PAUSE_CLOCK_OFFSET;
754         info->frm_opts.StopAfterPause = (tmp & IFX_SSC_SFCON_STOP_AFTER_PAUSE) > 0;
755
756         return &info->frm_opts;
757 }
758
759 static int
760 ifx_ssc_frm_control_set (struct ifx_ssc_port *info)
761 {
762         unsigned long tmp;
763
764         // check parameters
765         if ((info->frm_opts.DataLength > IFX_SSC_SFCON_DATA_LENGTH_MAX)
766             || (info->frm_opts.DataLength < 1)
767             || (info->frm_opts.PauseLength > IFX_SSC_SFCON_PAUSE_LENGTH_MAX)
768             || (info->frm_opts.PauseLength < 1)
769             || (info->frm_opts.IdleData & ~(IFX_SSC_SFCON_PAUSE_DATA_MASK >> IFX_SSC_SFCON_PAUSE_DATA_OFFSET))
770             || (info->frm_opts.IdleClock & ~(IFX_SSC_SFCON_PAUSE_CLOCK_MASK >> IFX_SSC_SFCON_PAUSE_CLOCK_OFFSET)))
771                 return -EINVAL;
772
773         // read interrupt bits (they're not changed here)
774         tmp = READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_SFCON) &
775                 (IFX_SSC_SFCON_FIR_ENABLE_BEFORE_PAUSE | IFX_SSC_SFCON_FIR_ENABLE_AFTER_PAUSE);
776
777         // set all values with respect to it's bit position (for data and pause 
778         // length set N-1)
779         tmp = (info->frm_opts.DataLength - 1) << IFX_SSC_SFCON_DATA_LENGTH_OFFSET;
780         tmp |= (info->frm_opts.PauseLength - 1) << IFX_SSC_SFCON_PAUSE_LENGTH_OFFSET;
781         tmp |= info->frm_opts.IdleData << IFX_SSC_SFCON_PAUSE_DATA_OFFSET;
782         tmp |= info->frm_opts.IdleClock << IFX_SSC_SFCON_PAUSE_CLOCK_OFFSET;
783         tmp |= info->frm_opts.FrameEnable * IFX_SSC_SFCON_SF_ENABLE;
784         tmp |= info->frm_opts.StopAfterPause * IFX_SSC_SFCON_STOP_AFTER_PAUSE;
785
786         WRITE_PERIPHERAL_REGISTER(tmp, info->mapbase + IFX_SSC_SFCON);
787
788         return 0;
789 }
790
791 static int
792 ifx_ssc_rxtx_mode_set (struct ifx_ssc_port *info, unsigned int val)
793 {
794         unsigned long tmp;
795
796         if (!(info) || (val & ~(IFX_SSC_MODE_MASK)))
797                 return -EINVAL;
798
799         if ((READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_STATE) & IFX_SSC_STATE_BUSY)
800             || (READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_RXCNT) & IFX_SSC_RXCNT_TODO_MASK))
801                 return -EBUSY;
802
803         tmp = (READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_CON) & ~(IFX_SSC_CON_RX_OFF | IFX_SSC_CON_TX_OFF)) | (val);
804         WRITE_PERIPHERAL_REGISTER (tmp, info->mapbase + IFX_SSC_CON);
805         info->opts.modeRxTx = val;
806
807         return 0;
808 }
809
810 static int
811 ifx_ssc_sethwopts (struct ifx_ssc_port *info)
812 {
813         unsigned long flags, bits;
814         struct ifx_ssc_hwopts *opts = &info->opts;
815
816         if ((opts->dataWidth < IFX_SSC_MIN_DATA_WIDTH)
817             || (opts->dataWidth > IFX_SSC_MAX_DATA_WIDTH))
818                 return -EINVAL;
819
820         bits = (opts->dataWidth - 1) << IFX_SSC_CON_DATA_WIDTH_OFFSET;
821         bits |= IFX_SSC_CON_ENABLE_BYTE_VALID;
822
823         if (opts->rxOvErrDetect)
824                 bits |= IFX_SSC_CON_RX_OFL_CHECK;
825         if (opts->rxUndErrDetect)
826                 bits |= IFX_SSC_CON_RX_UFL_CHECK;
827         if (opts->txOvErrDetect)
828                 bits |= IFX_SSC_CON_TX_OFL_CHECK;
829         if (opts->txUndErrDetect)
830                 bits |= IFX_SSC_CON_TX_UFL_CHECK;
831         if (opts->loopBack)
832                 bits |= IFX_SSC_CON_LOOPBACK_MODE;
833         if (opts->echoMode)
834                 bits |= IFX_SSC_CON_ECHO_MODE_ON;
835         if (opts->headingControl)
836                 bits |= IFX_SSC_CON_MSB_FIRST;
837         if (opts->clockPhase)
838                 bits |= IFX_SSC_CON_LATCH_THEN_SHIFT;
839         if (opts->clockPolarity)
840                 bits |= IFX_SSC_CON_CLOCK_FALL;
841
842         switch (opts->modeRxTx)
843         {
844         case IFX_SSC_MODE_TX:
845                 bits |= IFX_SSC_CON_RX_OFF;
846                 break;
847         case IFX_SSC_MODE_RX:
848                 bits |= IFX_SSC_CON_TX_OFF;
849                 break;
850         }
851
852         local_irq_save (flags);
853
854         WRITE_PERIPHERAL_REGISTER (bits, info->mapbase + IFX_SSC_CON);
855         WRITE_PERIPHERAL_REGISTER ((info->opts.gpoCs << IFX_SSC_GPOCON_ISCSB0_POS) |
856                                    (info->opts.gpoInv << IFX_SSC_GPOCON_INVOUT0_POS), info->mapbase + IFX_SSC_GPOCON);
857
858         WRITE_PERIPHERAL_REGISTER (info->opts.gpoCs << IFX_SSC_WHBGPOSTAT_SETOUT0_POS, info->mapbase + IFX_SSC_WHBGPOSTAT);
859
860         //master mode
861         if (opts->masterSelect)
862                 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_SET_MASTER_SELECT, info->mapbase + IFX_SSC_WHBSTATE);
863         else
864                 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_MASTER_SELECT, info->mapbase + IFX_SSC_WHBSTATE);
865
866         // init serial framing
867         WRITE_PERIPHERAL_REGISTER (0, info->mapbase + IFX_SSC_SFCON);
868         /* set up the port pins */
869         //check for general requirements to switch (external) pad/pin characteristics
870         /* TODO: P0.9 SPI_CS4, P0.10 SPI_CS5, P 0.11 SPI_CS6, because of ASC0 */
871         /* p0.15 SPI_CS1(EEPROM), P0.13 SPI_CS3, */
872         /* Set p0.15 to alternative 01, others to 00 (In/OUT) */
873         *(IFXMIPS_GPIO_P0_DIR) = (*IFXMIPS_GPIO_P0_DIR) | (0xA000);
874         *(IFXMIPS_GPIO_P0_ALTSEL0) = (((*IFXMIPS_GPIO_P0_ALTSEL0) | (0x8000)) & (~(0x2000)));
875         *(IFXMIPS_GPIO_P0_ALTSEL1) = (((*IFXMIPS_GPIO_P0_ALTSEL1) & (~0x8000)) & (~(0x2000)));
876         *(IFXMIPS_GPIO_P0_OD) = (*IFXMIPS_GPIO_P0_OD) | 0xA000;
877
878         /* p1.6 SPI_CS2(SFLASH), p1.0 SPI_DIN, p1.1 SPI_DOUT, p1.2 SPI_CLK */
879         *(IFXMIPS_GPIO_P1_DIR) = ((*IFXMIPS_GPIO_P1_DIR) | (0x46)) & (~1);
880         *(IFXMIPS_GPIO_P1_ALTSEL0) = ((*IFXMIPS_GPIO_P1_ALTSEL0) | (0x47));
881         *(IFXMIPS_GPIO_P1_ALTSEL1) = (*IFXMIPS_GPIO_P1_ALTSEL1) & (~0x47);
882         *(IFXMIPS_GPIO_P1_OD) = (*IFXMIPS_GPIO_P1_OD) | 0x0046;
883
884         /*CS3 */
885         /*TODO: CS4 CS5 CS6 */
886         *IFXMIPS_GPIO_P0_OUT = ((*IFXMIPS_GPIO_P0_OUT) | 0x2000);
887
888         local_irq_restore (flags);
889
890         return 0;
891 }
892
893 static int
894 ifx_ssc_set_baud (struct ifx_ssc_port *info, unsigned int baud)
895 {
896         unsigned int ifx_ssc_clock;
897         unsigned int br;
898         unsigned long flags;
899         bool enabled;
900         int retval = 0;
901
902         ifx_ssc_clock = ifx_ssc_get_kernel_clk(info);
903         if (ifx_ssc_clock == 0)
904         {
905                 retval = -EINVAL;
906                 goto out;
907         }
908
909         local_irq_save (flags);
910
911         enabled = (READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_STATE) & IFX_SSC_STATE_IS_ENABLED);
912         WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ENABLE, info->mapbase + IFX_SSC_WHBSTATE);
913
914         br = (((ifx_ssc_clock >> 1) + baud / 2) / baud) - 1;
915         wmb();
916
917         if (br > 0xffff || ((br == 0) &&
918                         ((READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_STATE) & IFX_SSC_STATE_IS_MASTER) == 0))) {
919                 local_irq_restore (flags);
920                 printk ("%s: invalid baudrate %u\n", __func__, baud);
921                 return -EINVAL;
922         }
923
924         WRITE_PERIPHERAL_REGISTER (br, info->mapbase + IFX_SSC_BR);
925
926         if (enabled)
927                 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_SET_ENABLE, info->mapbase + IFX_SSC_WHBSTATE);
928
929         local_irq_restore(flags);
930
931 out:
932         return retval;
933 }
934
935 static int
936 ifx_ssc_hwinit (struct ifx_ssc_port *info)
937 {
938         unsigned long flags;
939         bool enabled;
940
941         enabled = (READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_STATE) & IFX_SSC_STATE_IS_ENABLED);
942         WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ENABLE, info->mapbase + IFX_SSC_WHBSTATE);
943
944         if (ifx_ssc_sethwopts (info) < 0)
945         {
946                 printk ("%s: setting the hardware options failed\n", __func__);
947                 return -EINVAL;
948         }
949
950         if (ifx_ssc_set_baud (info, info->baud) < 0)
951         {
952                 printk ("%s: setting the baud rate failed\n", __func__);
953                 return -EINVAL;
954         }
955
956         local_irq_save (flags);
957
958         /* TX FIFO */
959         WRITE_PERIPHERAL_REGISTER ((IFX_SSC_DEF_TXFIFO_FL << IFX_SSC_XFCON_ITL_OFFSET) | IFX_SSC_XFCON_FIFO_ENABLE,
960                                    info->mapbase + IFX_SSC_TXFCON);
961         /* RX FIFO */
962         WRITE_PERIPHERAL_REGISTER ((IFX_SSC_DEF_RXFIFO_FL << IFX_SSC_XFCON_ITL_OFFSET) | IFX_SSC_XFCON_FIFO_ENABLE, 
963                                    info->mapbase + IFX_SSC_RXFCON);
964
965         local_irq_restore (flags);
966
967         if (enabled)
968                 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_SET_ENABLE, info->mapbase + IFX_SSC_WHBSTATE);
969
970         return 0;
971 }
972
973 int
974 ifx_ssc_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long data)
975 {
976         struct ifx_ssc_port *info;
977         int line, ret_val = 0;
978         unsigned long flags;
979         unsigned long tmp;
980         int from_kernel = 0;
981
982         if ((inode == (struct inode *) 0) || (inode == (struct inode *) 1))
983         {
984                 from_kernel = 1;
985                 line = (int) inode;
986         } else {
987                 line = MINOR (filp->f_dentry->d_inode->i_rdev);
988         }
989
990         if (line < 0 || line >= PORT_CNT)
991                 return -ENXIO;
992
993         info = &isp[line];
994
995         switch (cmd)
996         {
997         case IFX_SSC_STATS_READ:
998                 /* data must be a pointer to a struct ifx_ssc_statistics */
999                 if (from_kernel)
1000                         memcpy ((void *) data, (void *) &info->stats,
1001                                 sizeof (struct ifx_ssc_statistics));
1002                 else if (copy_to_user ((void *) data,
1003                                        (void *) &info->stats,
1004                                        sizeof (struct ifx_ssc_statistics)))
1005                         ret_val = -EFAULT;
1006                 break;
1007         case IFX_SSC_STATS_RESET:
1008                 /* just resets the statistics counters */
1009                 memset ((void *) &info->stats, 0,
1010                         sizeof (struct ifx_ssc_statistics));
1011                 break;
1012         case IFX_SSC_BAUD_SET:
1013                 /* if the buffers are not empty then the port is */
1014                 /* busy and we shouldn't change things on-the-fly! */
1015                 if (!info->txbuf || !info->rxbuf ||
1016                     (READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_STATE)
1017                      & IFX_SSC_STATE_BUSY)) {
1018                         ret_val = -EBUSY;
1019                         break;
1020                 }
1021                 /* misuse flags */
1022                 if (from_kernel)
1023                         flags = *((unsigned long *) data);
1024                 else if (copy_from_user ((void *) &flags,
1025                                          (void *) data, sizeof (flags))) {
1026                         ret_val = -EFAULT;
1027                         break;
1028                 }
1029                 if (flags == 0) {
1030                         ret_val = -EINVAL;
1031                         break;
1032                 }
1033                 if (ifx_ssc_set_baud (info, flags) < 0) {
1034                         ret_val = -EINVAL;
1035                         break;
1036                 }
1037                 info->baud = flags;
1038                 break;
1039         case IFX_SSC_BAUD_GET:
1040                 if (from_kernel)
1041                         *((unsigned int *) data) = info->baud;
1042                 else if (copy_to_user ((void *) data,
1043                                        (void *) &info->baud,
1044                                        sizeof (unsigned long)))
1045                         ret_val = -EFAULT;
1046                 break;
1047         case IFX_SSC_RXTX_MODE_SET:
1048                 if (from_kernel)
1049                         tmp = *((unsigned long *) data);
1050                 else if (copy_from_user ((void *) &tmp,
1051                                          (void *) data, sizeof (tmp))) {
1052                         ret_val = -EFAULT;
1053                         break;
1054                 }
1055                 ret_val = ifx_ssc_rxtx_mode_set (info, tmp);
1056                 break;
1057         case IFX_SSC_RXTX_MODE_GET:
1058                 tmp = READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_CON) &
1059                         (~(IFX_SSC_CON_RX_OFF | IFX_SSC_CON_TX_OFF));
1060                 if (from_kernel)
1061                         *((unsigned int *) data) = tmp;
1062                 else if (copy_to_user ((void *) data,
1063                                        (void *) &tmp, sizeof (tmp)))
1064                         ret_val = -EFAULT;
1065                 break;
1066
1067         case IFX_SSC_ABORT:
1068                 ifx_ssc_abort (info);
1069                 break;
1070
1071         case IFX_SSC_GPO_OUT_SET:
1072                 if (from_kernel)
1073                         tmp = *((unsigned long *) data);
1074                 else if (copy_from_user ((void *) &tmp,
1075                                          (void *) data, sizeof (tmp))) {
1076                         ret_val = -EFAULT;
1077                         break;
1078                 }
1079                 if (tmp > IFX_SSC_MAX_GPO_OUT)
1080                         ret_val = -EINVAL;
1081                 else
1082                         WRITE_PERIPHERAL_REGISTER
1083                                 (1 << (tmp + IFX_SSC_WHBGPOSTAT_SETOUT0_POS),
1084                                  info->mapbase + IFX_SSC_WHBGPOSTAT);
1085                 break;
1086         case IFX_SSC_GPO_OUT_CLR:
1087                 if (from_kernel)
1088                         tmp = *((unsigned long *) data);
1089                 else if (copy_from_user ((void *) &tmp,
1090                                          (void *) data, sizeof (tmp))) {
1091                         ret_val = -EFAULT;
1092                         break;
1093                 }
1094                 if (tmp > IFX_SSC_MAX_GPO_OUT)
1095                         ret_val = -EINVAL;
1096                 else {
1097                         WRITE_PERIPHERAL_REGISTER
1098                                 (1 << (tmp + IFX_SSC_WHBGPOSTAT_CLROUT0_POS),
1099                                  info->mapbase + IFX_SSC_WHBGPOSTAT);
1100                 }
1101                 break;
1102         case IFX_SSC_GPO_OUT_GET:
1103                 tmp = READ_PERIPHERAL_REGISTER
1104                         (info->mapbase + IFX_SSC_GPOSTAT);
1105                 if (from_kernel)
1106                         *((unsigned int *) data) = tmp;
1107                 else if (copy_to_user ((void *) data,
1108                                        (void *) &tmp, sizeof (tmp)))
1109                         ret_val = -EFAULT;
1110                 break;
1111         case IFX_SSC_FRM_STATUS_GET:
1112                 ifx_ssc_frm_status_get (info);
1113                 if (from_kernel)
1114                         memcpy ((void *) data, (void *) &info->frm_status,
1115                                 sizeof (struct ifx_ssc_frm_status));
1116                 else if (copy_to_user ((void *) data,
1117                                        (void *) &info->frm_status,
1118                                        sizeof (struct ifx_ssc_frm_status)))
1119                         ret_val = -EFAULT;
1120                 break;
1121         case IFX_SSC_FRM_CONTROL_GET:
1122                 ifx_ssc_frm_control_get (info);
1123                 if (from_kernel)
1124                         memcpy ((void *) data, (void *) &info->frm_opts,
1125                                 sizeof (struct ifx_ssc_frm_opts));
1126                 else if (copy_to_user ((void *) data,
1127                                        (void *) &info->frm_opts,
1128                                        sizeof (struct ifx_ssc_frm_opts)))
1129                         ret_val = -EFAULT;
1130                 break;
1131         case IFX_SSC_FRM_CONTROL_SET:
1132                 if (from_kernel)
1133                         memcpy ((void *) &info->frm_opts, (void *) data,
1134                                 sizeof (struct ifx_ssc_frm_opts));
1135                 else if (copy_to_user ((void *) &info->frm_opts,
1136                                        (void *) data,
1137                                        sizeof (struct ifx_ssc_frm_opts))) {
1138                         ret_val = -EFAULT;
1139                         break;
1140                 }
1141                 ret_val = ifx_ssc_frm_control_set (info);
1142                 break;
1143         case IFX_SSC_HWOPTS_SET:
1144                 /* data must be a pointer to a struct ifx_ssc_hwopts */
1145                 /* if the buffers are not empty then the port is */
1146                 /* busy and we shouldn't change things on-the-fly! */
1147                 if (!info->txbuf || !info->rxbuf ||
1148                     (READ_PERIPHERAL_REGISTER (info->mapbase + IFX_SSC_STATE)
1149                      & IFX_SSC_STATE_BUSY)) {
1150                         ret_val = -EBUSY;
1151                         break;
1152                 }
1153                 if (from_kernel)
1154                         memcpy ((void *) &info->opts, (void *) data,
1155                                 sizeof (struct ifx_ssc_hwopts));
1156                 else if (copy_from_user ((void *) &info->opts,
1157                                          (void *) data,
1158                                          sizeof (struct ifx_ssc_hwopts))) {
1159                         ret_val = -EFAULT;
1160                         break;
1161                 }
1162                 if (ifx_ssc_hwinit (info) < 0) {
1163                         ret_val = -EIO;
1164                 }
1165                 break;
1166         case IFX_SSC_HWOPTS_GET:
1167                 /* data must be a pointer to a struct ifx_ssc_hwopts */
1168                 if (from_kernel)
1169                         memcpy ((void *) data, (void *) &info->opts,
1170                                 sizeof (struct ifx_ssc_hwopts));
1171                 else if (copy_to_user ((void *) data,
1172                                        (void *) &info->opts,
1173                                        sizeof (struct ifx_ssc_hwopts)))
1174                         ret_val = -EFAULT;
1175                 break;
1176         default:
1177                 ret_val = -ENOIOCTLCMD;
1178         }
1179
1180         return ret_val;
1181 }
1182 EXPORT_SYMBOL(ifx_ssc_ioctl);
1183
1184 int __init
1185 ifx_ssc_init (void)
1186 {
1187         struct ifx_ssc_port *info;
1188         int i, nbytes;
1189         unsigned long flags;
1190         int ret_val;
1191
1192         ret_val = -ENOMEM;
1193         nbytes = PORT_CNT * sizeof(struct ifx_ssc_port);
1194         isp = (struct ifx_ssc_port*)kmalloc(nbytes, GFP_KERNEL);
1195
1196         if (isp == NULL)
1197         {
1198                 printk("%s: no memory for isp\n", __func__);
1199                 return (ret_val);
1200         }
1201         memset(isp, 0, nbytes);
1202
1203         ret_val = -ENXIO;
1204         if ((i = register_chrdev (maj, "ssc", &ifx_ssc_fops)) < 0)
1205         {
1206                 printk ("Unable to register major %d for the Infineon SSC\n", maj);
1207                 if (maj == 0)
1208                 {
1209                         goto errout;
1210                 } else {
1211                         maj = 0;
1212                         if ((i = register_chrdev (maj, "ssc", &ifx_ssc_fops)) < 0)
1213                         {
1214                                 printk ("Unable to register major %d for the Infineon SSC\n", maj);
1215                                 goto errout;
1216                         }
1217                 }
1218         }
1219
1220         if (maj == 0)
1221                 maj = i;
1222
1223         /* set default values in ifx_ssc_port */
1224         for (i = 0; i < PORT_CNT; i++) {
1225                 info = &isp[i];
1226                 info->port_nr = i;
1227                 /* default values for the HwOpts */
1228                 info->opts.AbortErrDetect = IFX_SSC_DEF_ABRT_ERR_DETECT;
1229                 info->opts.rxOvErrDetect = IFX_SSC_DEF_RO_ERR_DETECT;
1230                 info->opts.rxUndErrDetect = IFX_SSC_DEF_RU_ERR_DETECT;
1231                 info->opts.txOvErrDetect = IFX_SSC_DEF_TO_ERR_DETECT;
1232                 info->opts.txUndErrDetect = IFX_SSC_DEF_TU_ERR_DETECT;
1233                 info->opts.loopBack = IFX_SSC_DEF_LOOP_BACK;
1234                 info->opts.echoMode = IFX_SSC_DEF_ECHO_MODE;
1235                 info->opts.idleValue = IFX_SSC_DEF_IDLE_DATA;
1236                 info->opts.clockPolarity = IFX_SSC_DEF_CLOCK_POLARITY;
1237                 info->opts.clockPhase = IFX_SSC_DEF_CLOCK_PHASE;
1238                 info->opts.headingControl = IFX_SSC_DEF_HEADING_CONTROL;
1239                 info->opts.dataWidth = IFX_SSC_DEF_DATA_WIDTH;
1240                 info->opts.modeRxTx = IFX_SSC_DEF_MODE_RXTX;
1241                 info->opts.gpoCs = IFX_SSC_DEF_GPO_CS;
1242                 info->opts.gpoInv = IFX_SSC_DEF_GPO_INV;
1243                 info->opts.masterSelect = IFX_SSC_DEF_MASTERSLAVE;
1244                 info->baud = IFX_SSC_DEF_BAUDRATE;
1245                 info->rxbuf = NULL;
1246                 info->txbuf = NULL;
1247                 /* values specific to SSC1 */
1248                 if (i == 0) {
1249                         info->mapbase = IFXMIPS_SSC1_BASE_ADDR;
1250                         info->txirq = IFXMIPS_SSC_TIR;
1251                         info->rxirq = IFXMIPS_SSC_RIR;
1252                         info->errirq = IFXMIPS_SSC_EIR;
1253                 }
1254
1255                 WRITE_PERIPHERAL_REGISTER (IFX_SSC_DEF_RMC << IFX_CLC_RUN_DIVIDER_OFFSET, info->mapbase + IFX_SSC_CLC);
1256
1257                 init_waitqueue_head (&info->rwait);
1258
1259                 local_irq_save (flags);
1260
1261                 // init serial framing register
1262                 WRITE_PERIPHERAL_REGISTER (IFX_SSC_DEF_SFCON, info->mapbase + IFX_SSC_SFCON);
1263
1264                 ret_val = request_irq(info->txirq, ifx_ssc_tx_int, SA_INTERRUPT, "ifx_ssc_tx", info);
1265                 if (ret_val)
1266                 {
1267                         printk("%s: unable to get irq %d\n", __func__, info->txirq);
1268                         local_irq_restore(flags);
1269                         goto errout;
1270                 }
1271
1272                 ret_val = request_irq(info->rxirq, ifx_ssc_rx_int, SA_INTERRUPT, "ifx_ssc_rx", info);
1273                 if (ret_val)
1274                 {
1275                         printk ("%s: unable to get irq %d\n", __func__, info->rxirq);
1276                         local_irq_restore (flags);
1277                         goto irqerr;
1278                 }
1279
1280                 ret_val = request_irq(info->errirq, ifx_ssc_err_int, SA_INTERRUPT,"ifx_ssc_err", info);
1281                 if (ret_val)
1282                 {
1283                         printk ("%s: unable to get irq %d\n", __func__, info->errirq);
1284                         local_irq_restore (flags);
1285                         goto irqerr;
1286                 }
1287                 WRITE_PERIPHERAL_REGISTER (IFX_SSC_DEF_IRNEN, info->mapbase + IFX_SSC_IRN_EN);
1288
1289                 enable_irq(info->txirq);
1290                 enable_irq(info->rxirq);
1291                 enable_irq(info->errirq);
1292
1293                 local_irq_restore (flags);
1294         }
1295
1296         for (i = 0; i < PORT_CNT; i++) {
1297                 info = &isp[i];
1298                 if (ifx_ssc_hwinit (info) < 0)
1299                 {
1300                         printk ("%s: hardware init failed for port %d\n", __func__, i);
1301                         goto irqerr;
1302                 }
1303         }
1304
1305
1306         return 0;
1307
1308 irqerr:
1309         free_irq(isp[0].txirq, &isp[0]);
1310         free_irq(isp[0].rxirq, &isp[0]);
1311         free_irq(isp[0].errirq, &isp[0]);
1312 errout:
1313         kfree (isp);
1314         return (ret_val);
1315 }
1316
1317 void
1318 ifx_ssc_cleanup_module (void)
1319 {
1320         int i;
1321
1322         for (i = 0; i < PORT_CNT; i++) {
1323                 WRITE_PERIPHERAL_REGISTER (IFX_SSC_WHBSTATE_CLR_ENABLE, isp[i].mapbase + IFX_SSC_WHBSTATE);
1324                 free_irq(isp[i].txirq, &isp[i]);
1325                 free_irq(isp[i].rxirq, &isp[i]);
1326                 free_irq(isp[i].errirq, &isp[i]);
1327         }
1328         kfree (isp);
1329 }
1330
1331 module_init(ifx_ssc_init);
1332 module_exit(ifx_ssc_cleanup_module);
1333
1334
1335 inline int
1336 ifx_ssc_cs_low (u32 pin)
1337 {
1338         int ret = 0;
1339         if ((ret = ifx_ssc_ioctl ((struct inode *) 0, NULL, IFX_SSC_GPO_OUT_CLR, (unsigned long) &pin)))
1340                 printk ("clear CS %d fails\n", pin);
1341         wmb ();
1342
1343         return ret;
1344 }
1345 EXPORT_SYMBOL(ifx_ssc_cs_low);
1346
1347 inline int
1348 ifx_ssc_cs_high (u32 pin)
1349 {
1350         int ret = 0;
1351         if ((ret = ifx_ssc_ioctl((struct inode *) 0, NULL, IFX_SSC_GPO_OUT_SET, (unsigned long) &pin)))
1352                 printk ("set CS %d fails\n", pin);
1353         wmb ();
1354
1355         return ret;
1356 }
1357 EXPORT_SYMBOL(ifx_ssc_cs_high);
1358
1359 static int
1360 ssc_session (char *tx_buf, u32 tx_len, char *rx_buf, u32 rx_len)
1361 {
1362         int ret = 0;
1363
1364         char *ssc_tx_buf = NULL;
1365         char *ssc_rx_buf = NULL;
1366         int eff_size = 0;
1367         u8 mode = 0;
1368
1369         if (tx_buf == NULL && tx_len == 0 && rx_buf == NULL && rx_len == 0) {
1370                 printk ("invalid parameters\n");
1371                 ret = -EINVAL;
1372                 goto ssc_session_exit;
1373         }
1374         else if (tx_buf == NULL || tx_len == 0) {
1375                 if (rx_buf != NULL && rx_len != 0) {
1376                         mode = IFX_SSC_MODE_RX;
1377                 }
1378                 else {
1379                         printk ("invalid parameters\n");
1380                         ret = -EINVAL;
1381                         goto ssc_session_exit;
1382                 }
1383         }
1384         else if (rx_buf == NULL || rx_len == 0) {
1385                 if (tx_buf != NULL && tx_len != 0) {
1386                         mode = IFX_SSC_MODE_TX;
1387                 }
1388                 else {
1389                         printk ("invalid parameters\n");
1390                         ret = -EINVAL;
1391                         goto ssc_session_exit;
1392                 }
1393         }
1394         else {
1395                 mode = IFX_SSC_MODE_RXTX;
1396         }
1397
1398         if (mode == IFX_SSC_MODE_RXTX) {
1399                 eff_size = tx_len + rx_len;
1400         }
1401         else if (mode == IFX_SSC_MODE_RX) {
1402                 eff_size = rx_len;
1403         }
1404         else {
1405                 eff_size = tx_len;
1406         }
1407
1408         //4 bytes alignment,  required by driver
1409         /* change by TaiCheng */
1410         //if (in_irq()){
1411         if (1) {
1412                 ssc_tx_buf =
1413                         (char *) kmalloc (sizeof (char) *
1414                                           ((eff_size + 3) & (~3)),
1415                                           GFP_ATOMIC);
1416                 ssc_rx_buf =
1417                         (char *) kmalloc (sizeof (char) *
1418                                           ((eff_size + 3) & (~3)),
1419                                           GFP_ATOMIC);
1420         }
1421         else {
1422                 ssc_tx_buf =
1423                         (char *) kmalloc (sizeof (char) *
1424                                           ((eff_size + 3) & (~3)),
1425                                           GFP_KERNEL);
1426                 ssc_rx_buf =
1427                         (char *) kmalloc (sizeof (char) *
1428                                           ((eff_size + 3) & (~3)),
1429                                           GFP_KERNEL);
1430         }
1431         if (ssc_tx_buf == NULL || ssc_rx_buf == NULL) {
1432                 printk ("no memory for size of %d\n", eff_size);
1433                 ret = -ENOMEM;
1434                 goto ssc_session_exit;
1435         }
1436         memset ((void *) ssc_tx_buf, 0, eff_size);
1437         memset ((void *) ssc_rx_buf, 0, eff_size);
1438
1439         if (tx_len > 0) {
1440                 memcpy (ssc_tx_buf, tx_buf, tx_len);
1441         }
1442
1443         ret = ifx_ssc_kwrite (0, ssc_tx_buf, eff_size);
1444
1445         if (ret > 0) {
1446                 ssc_tx_buf = NULL;      //should be freed by ifx_ssc_kwrite
1447         }
1448
1449         if (ret != eff_size) {
1450                 printk ("ifx_ssc_write return %d\n", ret);
1451                 goto ssc_session_exit;
1452         }
1453         ret = ifx_ssc_kread (0, ssc_rx_buf, eff_size);
1454         if (ret != eff_size) {
1455                 printk ("ifx_ssc_read return %d\n", ret);
1456                 goto ssc_session_exit;
1457         }
1458
1459         memcpy (rx_buf, ssc_rx_buf + tx_len, rx_len);
1460
1461         if (mode == IFX_SSC_MODE_TX) {
1462                 ret = tx_len;
1463         }
1464         else {
1465                 ret = rx_len;
1466         }
1467       ssc_session_exit:
1468
1469         if (ssc_tx_buf != NULL)
1470                 kfree (ssc_tx_buf);
1471         if (ssc_rx_buf != NULL)
1472                 kfree (ssc_rx_buf);
1473
1474         if (ret < 0) {
1475                 printk ("ssc session fails\n");
1476         }
1477         return ret;
1478 }
1479
1480 int
1481 ifx_ssc_txrx (char *tx_buf, u32 tx_len, char *rx_buf, u32 rx_len)
1482 {
1483         return ssc_session(tx_buf, tx_len, rx_buf, rx_len);
1484 }
1485 EXPORT_SYMBOL(ifx_ssc_txrx);
1486
1487 int
1488 ifx_ssc_tx (char *tx_buf, u32 tx_len)
1489 {
1490         return ssc_session(tx_buf, tx_len, NULL, 0);
1491 }
1492 EXPORT_SYMBOL(ifx_ssc_tx);
1493
1494 int
1495 ifx_ssc_rx (char *rx_buf, u32 rx_len)
1496 {
1497         return ssc_session(NULL, 0, rx_buf, rx_len);
1498 }
1499 EXPORT_SYMBOL(ifx_ssc_rx);
1500
1501 MODULE_LICENSE("GPL");
1502 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
1503 MODULE_DESCRIPTION("ifxmips ssc driver");
1504