fix up eeprom and add ssc driver ... this needs a lot of work
[lede.git] / target / linux / danube / files / drivers / char / danube_eeprom.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  *   This driver was originally based on the INCA-IP driver, but due to
17  *   fundamental conceptual drawbacks there has been changed a lot.
18  *
19  *   Based on INCA-IP driver Copyright (c) 2003 Gary Jennejohn <gj@denx.de>
20  *   Based on the VxWorks drivers Copyright (c) 2002, Infineon Technologies.
21  *
22  *   Copyright (C) 2006 infineon
23  *   Copyright (C) 2007 John Crispin <blogic@openwrt.org> 
24  *
25  */
26
27 #define IFAP_EEPROM_DRV_VERSION "0.0.1"
28
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/signal.h>
32 #include <linux/sched.h>
33 #include <linux/timer.h>
34 #include <linux/interrupt.h>
35 #include <linux/major.h>
36 #include <linux/string.h>
37 #include <linux/fs.h>
38 #include <linux/fcntl.h>
39 #include <linux/ptrace.h>
40 #include <linux/mm.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>
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/danube/danube.h>
57 #include <asm/danube/danube_irq.h>
58 #include <asm/danube/ifx_ssc_defines.h>
59 #include <asm/danube/ifx_ssc.h>
60
61 /* allow the user to set the major device number */
62 static int danube_eeprom_maj = 0;
63
64 static ssize_t danube_eeprom_fops_read (struct file *, char *, size_t, loff_t *);
65 static ssize_t danube_eeprom_fops_write (struct file *, const char *, size_t,
66                                     loff_t *);
67 static int danube_eeprom_ioctl (struct inode *, struct file *, unsigned int,
68                                 unsigned long);
69 static int danube_eeprom_open (struct inode *, struct file *);
70 static int danube_eeprom_close (struct inode *, struct file *);
71
72 //ifx_ssc.c
73 extern int ifx_ssc_init (void);
74 extern int ifx_ssc_open (struct inode *inode, struct file *filp);
75 extern int ifx_ssc_close (struct inode *inode, struct file *filp);
76 extern void ifx_ssc_cleanup_module (void);
77 extern int ifx_ssc_ioctl (struct inode *inode, struct file *filp,
78                           unsigned int cmd, unsigned long data);
79 extern ssize_t ifx_ssc_kwrite (int port, const char *kbuf, size_t len);
80 extern ssize_t ifx_ssc_kread (int port, char *kbuf, size_t len);
81
82 extern int ifx_ssc_cs_low (unsigned int pin);
83 extern int ifx_ssc_cs_high (unsigned int pin);
84 extern int ifx_ssc_txrx (char *tx_buf, unsigned int tx_len, char *rx_buf, unsigned int rx_len);
85 extern int ifx_ssc_tx (char *tx_buf, unsigned int tx_len);
86 extern int ifx_ssc_rx (char *rx_buf, unsigned int rx_len);
87
88 #define EEPROM_CS IFX_SSC_WHBGPOSTAT_OUT0_POS
89
90 /* commands for EEPROM, x25160, x25140 */
91 #define EEPROM_WREN                     ((unsigned char)0x06)
92 #define EEPROM_WRDI                     ((unsigned char)0x04)
93 #define EEPROM_RDSR                     ((unsigned char)0x05)
94 #define EEPROM_WRSR                     ((unsigned char)0x01)
95 #define EEPROM_READ                     ((unsigned char)0x03)
96 #define EEPROM_WRITE                    ((unsigned char)0x02)
97 #define EEPROM_PAGE_SIZE                4
98 #define EEPROM_SIZE                     512
99
100 static int
101 eeprom_rdsr (char *status)
102 {
103         int ret = 0;
104         unsigned char cmd = EEPROM_RDSR;
105         unsigned long flag;
106
107         local_irq_save(flag);
108
109         if ((ret = ifx_ssc_cs_low (EEPROM_CS)))
110         {
111                 local_irq_restore(flag);
112                 goto out;
113         }
114
115         if ((ret = ifx_ssc_txrx (&cmd, 1, status, 1)) < 0)
116         {
117                 ifx_ssc_cs_high(EEPROM_CS);
118                 local_irq_restore(flag);
119                 goto out;
120         }
121
122         if ((ret = ifx_ssc_cs_high(EEPROM_CS)))
123         {
124                 local_irq_restore(flag);
125                 goto out;
126         }
127
128         local_irq_restore(flag);
129
130 out:
131         return ret;
132 }
133
134 static inline int
135 eeprom_wip_over (void)
136 {
137         int ret = 0;
138         unsigned char status;
139
140         while (1)
141         {
142                 ret = eeprom_rdsr(&status);
143                 printk("status %x \n", status);
144
145                 if (ret)
146                 {
147                         printk("read back status fails %d\n", ret);
148                         break;
149                 }
150
151                 if (((status) & 1) != 0)
152                         printk("read back status not zero %x\n", status);
153                 else
154                         break;
155         }
156
157         return ret;
158 }
159
160 static int
161 eeprom_wren (void)
162 {
163         unsigned char cmd = EEPROM_WREN;
164         int ret = 0;
165         unsigned long flag;
166
167         local_irq_save(flag);
168         if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
169         {
170                 local_irq_restore(flag);
171                 goto out;
172         }
173
174         if ((ret = ifx_ssc_tx(&cmd, 1)) < 0)
175         {
176                 ifx_ssc_cs_high(EEPROM_CS);
177                 local_irq_restore(flag);
178                 goto out;
179         }
180
181         if ((ret = ifx_ssc_cs_high(EEPROM_CS)))
182         {
183                 local_irq_restore(flag);
184                 goto out;
185         }
186
187         local_irq_restore(flag);
188         eeprom_wip_over();
189
190 out:
191         return ret;
192 }
193
194 static int
195 eeprom_wrsr (void)
196 {
197         int ret = 0;
198         unsigned char cmd[2];
199         unsigned long flag;
200
201         cmd[0] = EEPROM_WRSR;
202         cmd[1] = 0;
203
204         if ((ret = eeprom_wren()))
205         {
206                 printk ("eeprom_wren fails\n");
207                 goto out1;
208         }
209
210         local_irq_save(flag);
211
212         if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
213                 goto out;
214
215         if ((ret = ifx_ssc_tx(cmd, 2)) < 0) {
216                 ifx_ssc_cs_high(EEPROM_CS);
217                 goto out;
218         }
219
220         if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
221                 goto out;
222
223         local_irq_restore(flag);
224         eeprom_wip_over();
225
226         return ret;
227
228 out:
229         local_irq_restore (flag);
230         eeprom_wip_over ();
231
232 out1:
233         return ret;
234 }
235
236 static int
237 eeprom_read (unsigned int addr, unsigned char *buf, unsigned int len)
238 {
239         int ret = 0;
240         unsigned char write_buf[2];
241         unsigned int eff = 0;
242         unsigned long flag;
243
244         while (1)
245         {
246                 eeprom_wip_over();
247                 eff = EEPROM_PAGE_SIZE - (addr % EEPROM_PAGE_SIZE);
248                 eff = (eff < len) ? eff : len;
249                 local_irq_save(flag);
250
251                 if ((ret = ifx_ssc_cs_low(EEPROM_CS)) < 0)
252                         goto out;
253
254                 write_buf[0] = EEPROM_READ | ((unsigned char) ((addr & 0x100) >> 5));
255                 write_buf[1] = (addr & 0xff);
256
257                 if ((ret = ifx_ssc_txrx (write_buf, 2, buf, eff)) != eff)
258                 {
259                         printk("ssc_txrx fails %d\n", ret);
260                         ifx_ssc_cs_high (EEPROM_CS);
261                         goto out;
262                 }
263
264                 buf += ret;
265                 len -= ret;
266                 addr += ret;
267
268                 if ((ret = ifx_ssc_cs_high(EEPROM_CS)))
269                         goto out;
270
271                 local_irq_restore(flag);
272
273                 if (len <= 0)
274                         goto out2;
275         }
276
277 out:
278         local_irq_restore (flag);
279 out2:
280         return ret;
281 }
282
283 static int
284 eeprom_write (unsigned int addr, unsigned char *buf, unsigned int len)
285 {
286         int ret = 0;
287         unsigned int eff = 0;
288         unsigned char write_buf[2];
289         int i;
290         unsigned char rx_buf[EEPROM_PAGE_SIZE];
291
292         while (1)
293         {
294                 eeprom_wip_over();
295
296                 if ((ret = eeprom_wren()))
297                 {
298                         printk("eeprom_wren fails\n");
299                         goto out;
300                 }
301
302                 write_buf[0] = EEPROM_WRITE | ((unsigned char) ((addr & 0x100) >> 5));
303                 write_buf[1] = (addr & 0xff);
304
305                 eff = EEPROM_PAGE_SIZE - (addr % EEPROM_PAGE_SIZE);
306                 eff = (eff < len) ? eff : len;
307
308                 printk("EEPROM Write:\n");
309                 for (i = 0; i < eff; i++) {
310                         printk("%2x ", buf[i]);
311                         if ((i % 16) == 15)
312                                 printk("\n");
313                 }
314                 printk("\n");
315
316                 if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
317                         goto out;
318
319                 if ((ret = ifx_ssc_tx (write_buf, 2)) < 0)
320                 {
321                         printk("ssc_tx fails %d\n", ret);
322                         ifx_ssc_cs_high(EEPROM_CS);
323                         goto out;
324                 }
325
326                 if ((ret = ifx_ssc_tx (buf, eff)) != eff)
327                 {
328                         printk("ssc_tx fails %d\n", ret);
329                         ifx_ssc_cs_high(EEPROM_CS);
330                         goto out;
331                 }
332
333                 buf += ret;
334                 len -= ret;
335                 addr += ret;
336
337                 if ((ret = ifx_ssc_cs_high (EEPROM_CS)))
338                         goto out;
339
340                 printk ("<==");
341                 eeprom_read((addr - eff), rx_buf, eff);
342                 for (i = 0; i < eff; i++)
343                 {
344                         printk ("[%x]", rx_buf[i]);
345                 }
346                 printk ("\n");
347
348                 if (len <= 0)
349                         break;
350         }
351
352 out:
353         return ret;
354 }
355
356 int
357 danube_eeprom_open (struct inode *inode, struct file *filp)
358 {
359         filp->f_pos = 0;
360         return 0;
361 }
362
363 int
364 danube_eeprom_close (struct inode *inode, struct file *filp)
365 {
366         return 0;
367 }
368
369 int
370 danube_eeprom_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long data)
371 {
372         return 0;
373 }
374
375 ssize_t
376 danube_eeprom_read (char *buf, size_t len, unsigned int addr)
377 {
378         int ret = 0;
379         unsigned int data;
380
381         printk("addr:=%d\n", addr);
382         printk("len:=%d\n", len);
383
384         if ((addr + len) > EEPROM_SIZE)
385         {
386                 printk("invalid len\n");
387                 addr = 0;
388                 len = EEPROM_SIZE / 2;
389         }
390
391         if ((ret = ifx_ssc_open ((struct inode *) 0, NULL)))
392         {
393                 printk("danube_eeprom_open fails\n");
394                 goto out;
395         }
396
397         data = (unsigned int)IFX_SSC_MODE_RXTX;
398
399         if ((ret = ifx_ssc_ioctl ((struct inode *) 0, NULL, IFX_SSC_RXTX_MODE_SET, (unsigned long) &data)))
400         {
401                 printk("set RXTX mode fails\n");
402                 goto out;
403         }
404
405         if ((ret = eeprom_wrsr ()))
406         {
407                 printk("EEPROM reset fails\n");
408                 goto out;
409         }
410
411         if ((ret = eeprom_read (addr, buf, len)))
412         {
413                 printk("eeprom read fails\n");
414                 goto out;
415         }
416
417 out:
418         if (ifx_ssc_close ((struct inode *) 0, NULL))
419                 printk("danube_eeprom_close fails\n");
420
421         return len;
422 }
423 EXPORT_SYMBOL(danube_eeprom_read);
424
425 static ssize_t
426 danube_eeprom_fops_read (struct file *filp, char *ubuf, size_t len, loff_t * off)
427 {
428         int ret = 0;
429         unsigned char ssc_rx_buf[EEPROM_SIZE];
430         long flag;
431
432         if (*off >= EEPROM_SIZE)
433                 return 0;
434
435         if (*off + len > EEPROM_SIZE)
436                 len = EEPROM_SIZE - *off;
437
438         if (len == 0)
439                 return 0;
440
441         local_irq_save(flag);
442
443         if ((ret = danube_eeprom_read(ssc_rx_buf, len, *off)) < 0)
444         {
445                 printk("read fails, err=%x\n", ret);
446                 local_irq_restore(flag);
447                 return ret;
448         }
449
450         if (copy_to_user((void*)ubuf, ssc_rx_buf, ret) != 0)
451         {
452                 local_irq_restore(flag);
453                 ret = -EFAULT;
454         }
455
456         local_irq_restore(flag);
457         *off += len;
458
459         return len;
460 }
461
462 ssize_t
463 danube_eeprom_write (char *buf, size_t len, unsigned int addr)
464 {
465         int ret = 0;
466         unsigned int data;
467
468         if ((ret = ifx_ssc_open ((struct inode *) 0, NULL)))
469         {
470                 printk ("danube_eeprom_open fails\n");
471                 goto out;
472         }
473
474         data = (unsigned int) IFX_SSC_MODE_RXTX;
475
476         if ((ret = ifx_ssc_ioctl ((struct inode *) 0, NULL, IFX_SSC_RXTX_MODE_SET, (unsigned long) &data)))
477         {
478                 printk ("set RXTX mode fails\n");
479                 goto out;
480         }
481
482         if ((ret = eeprom_wrsr ())) {
483                 printk ("EEPROM reset fails\n");
484                 goto out;
485         }
486
487         if ((ret = eeprom_write (addr, buf, len))) {
488                 printk ("eeprom write fails\n");
489                 goto out;
490         }
491
492 out:
493         if (ifx_ssc_close ((struct inode *) 0, NULL))
494                 printk ("danube_eeprom_close fails\n");
495
496         return ret;
497 }
498 EXPORT_SYMBOL(danube_eeprom_write);
499
500 static ssize_t
501 danube_eeprom_fops_write (struct file *filp, const char *ubuf, size_t len, loff_t * off)
502 {
503         int ret = 0;
504         unsigned char ssc_tx_buf[EEPROM_SIZE];
505
506         if (*off >= EEPROM_SIZE)
507                 return 0;
508
509         if (len + *off > EEPROM_SIZE)
510                 len = EEPROM_SIZE - *off;
511
512         if ((ret = copy_from_user (ssc_tx_buf, ubuf, len)))
513                 return EFAULT;
514
515         ret = danube_eeprom_write (ssc_tx_buf, len, *off);
516
517         if (ret > 0)
518                 *off = ret;
519
520         return ret;
521 }
522
523 loff_t
524 danube_eeprom_llseek (struct file * filp, loff_t off, int whence)
525 {
526         loff_t newpos;
527         switch (whence) {
528         case SEEK_SET:
529                 newpos = off;
530                 break;
531
532         case SEEK_CUR:
533                 newpos = filp->f_pos + off;
534                 break;
535
536         default:
537                 return -EINVAL;
538         }
539
540         if (newpos < 0)
541                 return -EINVAL;
542
543         filp->f_pos = newpos;
544
545         return newpos;
546 }
547
548 static struct file_operations danube_eeprom_fops = {
549       owner:THIS_MODULE,
550       llseek:danube_eeprom_llseek,
551       read:danube_eeprom_fops_read,
552       write:danube_eeprom_fops_write,
553       ioctl:danube_eeprom_ioctl,
554       open:danube_eeprom_open,
555       release:danube_eeprom_close,
556 };
557
558 int __init
559 danube_eeprom_init (void)
560 {
561         int ret = 0;
562
563         danube_eeprom_maj = register_chrdev(0, "eeprom", &danube_eeprom_fops);
564
565         if (danube_eeprom_maj < 0)
566         {
567                 printk("failed to register eeprom device\n");
568                 ret = -EINVAL;
569                 
570                 goto out;
571         }
572
573         printk("danube_eeprom : /dev/eeprom mayor %d\n", danube_eeprom_maj);
574
575 out:
576         return ret;
577 }
578
579 void __exit
580 danube_eeprom_cleanup_module (void)
581 {
582         /*if (unregister_chrdev (danube_eeprom_maj, "eeprom")) {
583                 printk ("Unable to unregister major %d for the EEPROM\n",
584                         maj);
585         }*/
586 }
587
588 module_exit (danube_eeprom_cleanup_module);
589 module_init (danube_eeprom_init);
590
591 MODULE_LICENSE ("GPL");
592 MODULE_AUTHOR ("Peng Liu");
593 MODULE_DESCRIPTION ("IFAP EEPROM driver");
594 MODULE_SUPPORTED_DEVICE ("danube_eeprom");
595
596