add original danube eeprom code
[openwrt/svn-archive/archive.git] / target / linux / danube / files / drivers / char / danube_eeprom.c
1 /******************************************************************************
2 **
3 ** FILE NAME : danube_eeprom.c
4 ** PROJECT : Danube
5 ** MODULES : EEPROM
6 **
7 ** DATE : 31 DEC 2004
8 ** AUTHOR : Liu Peng
9 ** DESCRIPTION : X25040 EEPROM Driver
10 ** COPYRIGHT : Copyright (c) 2006
11 ** Infineon Technologies AG
12 ** Am Campeon 1-12, 85579 Neubiberg, Germany
13 **
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License as published by
16 ** the Free Software Foundation; either version 2 of the License, or
17 ** (at your option) any later version.
18 **
19 ** HISTORY
20 ** $Date $Author $Comment
21 ** 31 DEC 2004 Liu Peng Initiate Version
22 ** 25 OCT 2006 Xu Liang Add GPL header.
23 *******************************************************************************/
24
25 #define IFAP_EEPROM_DRV_VERSION "0.0.1"
26
27 /*
28 **************************************************
29 *
30 * This driver was originally based on the INCA-IP driver, but due to
31 * fundamental conceptual drawbacks there has been changed a lot.
32 *
33 * Based on INCA-IP driver Copyright (c) 2003 Gary Jennejohn <gj@denx.de>
34 * Based on the VxWorks drivers Copyright (c) 2002, Infineon Technologies.
35 *
36 * This program is free software; you can redistribute it and/or modify
37 * it under the terms of the GNU General Public License as published by
38 * the Free Software Foundation; either version 2 of the License, or
39 * (at your option) any later version.
40 *
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
45 *
46 * You should have received a copy of the GNU General Public License
47 * along with this program; if not, write to the Free Software
48 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
49 *
50 */
51 #ifndef EXPORT_SYMTAB
52 #define EXPORT_SYMTAB
53 #endif
54 #include <linux/config.h>
55 #include <linux/module.h>
56 #include <linux/errno.h>
57 #include <linux/signal.h>
58 #include <linux/sched.h>
59 #include <linux/timer.h>
60 #include <linux/interrupt.h>
61 #include <linux/major.h>
62 #include <linux/string.h>
63 #include <linux/fs.h>
64 #include <linux/proc_fs.h>
65 #include <linux/fcntl.h>
66 #include <linux/ptrace.h>
67 #include <linux/mm.h>
68 #include <linux/ioport.h>
69 #include <linux/init.h>
70 #include <linux/delay.h>
71 #include <linux/spinlock.h>
72 #include <linux/slab.h>
73 #include <asm/system.h>
74 #include <asm/io.h>
75 #include <asm/irq.h>
76 #include <asm/uaccess.h>
77 #include <asm/bitops.h>
78
79 #include <linux/types.h>
80 #include <linux/kernel.h>
81 #include <linux/version.h>
82
83 #include <asm/danube/danube.h>
84 #include <asm/danube/irq.h>
85 #include <asm/danube/ifx_ssc_defines.h>
86 #include <asm/danube/ifx_ssc.h>
87 #if CONFIG_MODVERSIONS==1
88 # include <linux/modversions.h>
89 #endif
90
91 #define AMAZON_EEPROM_EMSG(fmt,args...) printk("%s:" fmt, __FUNCTION__, ##args)
92
93 #if 0
94 #define AMAZON_EEPROM_DMSG(fmt,args...) printk("%s:" fmt, __FUNCTION__, ##args)
95 #else
96 #define AMAZON_EEPROM_DMSG(fmt,args...)
97 #endif
98
99 MODULE_LICENSE ("GPL");
100 MODULE_AUTHOR ("Peng Liu");
101 MODULE_DESCRIPTION ("IFAP EEPROM driver");
102 MODULE_SUPPORTED_DEVICE ("danube_eeprom");
103 MODULE_PARM (maj, "i");
104 MODULE_PARM_DESC (maj, "Major device number");
105
106 /* allow the user to set the major device number */
107 static int maj = 0;
108
109 static ssize_t danube_eeprom_read (struct file *, char *, size_t, loff_t *);
110 static ssize_t danube_eeprom_write (struct file *, const char *, size_t,
111 loff_t *);
112 static int danube_eeprom_ioctl (struct inode *, struct file *, unsigned int,
113 unsigned long);
114 static int danube_eeprom_open (struct inode *, struct file *);
115 static int danube_eeprom_close (struct inode *, struct file *);
116
117 //ifx_ssc.c
118 extern int ifx_ssc_init (void);
119 extern int ifx_ssc_open (struct inode *inode, struct file *filp);
120 extern int ifx_ssc_close (struct inode *inode, struct file *filp);
121 extern void ifx_ssc_cleanup_module (void);
122 extern int ifx_ssc_ioctl (struct inode *inode, struct file *filp,
123 unsigned int cmd, unsigned long data);
124 extern ssize_t ifx_ssc_kwrite (int port, const char *kbuf, size_t len);
125 extern ssize_t ifx_ssc_kread (int port, char *kbuf, size_t len);
126
127 extern int ifx_ssc_cs_low (u32 pin);
128 extern int ifx_ssc_cs_high (u32 pin);
129 extern int ifx_ssc_txrx (char *tx_buf, u32 tx_len, char *rx_buf, u32 rx_len);
130 extern int ifx_ssc_tx (char *tx_buf, u32 tx_len);
131 extern int ifx_ssc_rx (char *rx_buf, u32 rx_len);
132
133 /* CS: board dependant */
134 #define EEPROM_CS IFX_SSC_WHBGPOSTAT_OUT0_POS
135
136 /* commands for EEPROM, x25160, x25140 */
137 #define EEPROM_WREN ((u8)0x06)
138 #define EEPROM_WRDI ((u8)0x04)
139 #define EEPROM_RDSR ((u8)0x05)
140 #define EEPROM_WRSR ((u8)0x01)
141 #define EEPROM_READ ((u8)0x03)
142 #define EEPROM_WRITE ((u8)0x02)
143 #define EEPROM_PAGE_SIZE 4
144 #define EEPROM_SIZE 512
145
146 /* Brief: EEPROM_RDSR command
147 * Return: 0 OK
148 * error code
149 */
150 static int
151 eeprom_rdsr (char *status)
152 {
153 int ret = 0;
154 unsigned char cmd = EEPROM_RDSR;
155
156 unsigned long flag;
157 local_irq_save (flag);
158 if ((ret = ifx_ssc_cs_low (EEPROM_CS))) {
159 local_irq_restore (flag);
160 return ret;
161 }
162 if ((ret = ifx_ssc_txrx (&cmd, 1, status, 1)) < 0) {
163 ifx_ssc_cs_high (EEPROM_CS);
164 local_irq_restore (flag);
165 return ret;
166 }
167
168 if ((ret = ifx_ssc_cs_high (EEPROM_CS))) {
169 local_irq_restore (flag);
170 return ret;
171 }
172 local_irq_restore (flag);
173 return ret;
174 }
175
176 /* Brief: wait for WIP is over
177 * Return:
178 * 0 WIP is over
179 * <0 Error code
180 */
181 static inline int
182 eeprom_wip_over (void)
183 {
184 int ret = 0;
185 u8 status;
186 while (1) {
187 ret = eeprom_rdsr (&status);
188 AMAZON_EEPROM_DMSG ("status %x \n", status);
189 if (ret) {
190 AMAZON_EEPROM_EMSG ("read back status fails %d\n",
191 ret);
192 break;
193 }
194 else if (((status) & 1) != 0) {
195 AMAZON_EEPROM_DMSG ("read back status not zero %x\n",
196 status);
197 }
198 else {
199 break;
200 }
201 }
202 return ret;
203 }
204
205 /* Brief: EEPROM_WREN command
206 * Return: 0 OK
207 * error code
208 */
209 static int
210 eeprom_wren (void)
211 {
212 unsigned char cmd = EEPROM_WREN;
213 int ret = 0;
214
215 unsigned long flag;
216 local_irq_save (flag);
217 if ((ret = ifx_ssc_cs_low (EEPROM_CS))) {
218 local_irq_restore (flag);
219 return ret;
220 }
221
222 if ((ret = ifx_ssc_tx (&cmd, 1)) < 0) {
223 ifx_ssc_cs_high (EEPROM_CS);
224 local_irq_restore (flag);
225 return ret;
226 }
227
228 if ((ret = ifx_ssc_cs_high (EEPROM_CS))) {
229 local_irq_restore (flag);
230 return ret;
231 }
232 local_irq_restore (flag);
233 eeprom_wip_over ();
234 return ret;
235
236 }
237
238 /* Brief: EEPROM_WRSR command
239 * Return: 0 OK
240 * error code
241 */
242 static int
243 eeprom_wrsr (void)
244 {
245 int ret = 0;
246 unsigned char cmd[2];
247 cmd[0] = EEPROM_WRSR;
248 cmd[1] = 0;
249 if ((ret = eeprom_wren ())) {
250 AMAZON_EEPROM_EMSG ("eeprom_wren fails\n");
251 return ret;
252 }
253 unsigned long flag;
254 local_irq_save (flag);
255 if ((ret = ifx_ssc_cs_low (EEPROM_CS))) {
256 goto eeprom_wrsr_err_out;
257 }
258
259 if ((ret = ifx_ssc_tx (cmd, 2)) < 0) {
260 ifx_ssc_cs_high (EEPROM_CS);
261 goto eeprom_wrsr_err_out;
262 }
263
264 if ((ret = ifx_ssc_cs_low (EEPROM_CS))) {
265 goto eeprom_wrsr_err_out;
266 }
267 local_irq_restore (flag);
268 eeprom_wip_over ();
269 return ret;
270 eeprom_wrsr_err_out:
271 local_irq_restore (flag);
272 eeprom_wip_over ();
273 return ret;
274 }
275
276 /* Brief: read EEPROM
277 * Parameter:
278 * addr
279 * len
280 * buf
281 * Return: ok
282 * -EFAULT
283 */
284 static int
285 eeprom_read (u32 addr, unsigned char *buf, u32 len)
286 {
287 int ret = 0;
288 unsigned char write_buf[2];
289 u32 eff = 0;
290
291 unsigned long flag;
292
293 while (1) {
294 eeprom_wip_over ();
295 eff = EEPROM_PAGE_SIZE - (addr % EEPROM_PAGE_SIZE);
296 eff = (eff < len) ? eff : len;
297 //Format:
298 //CMD + ADDR
299 local_irq_save (flag);
300 if ((ret = ifx_ssc_cs_low (EEPROM_CS)) < 0) {
301 goto eeprom_read_err_out;
302 }
303
304 write_buf[0] = EEPROM_READ | ((u8) ((addr & 0x100) >> 5));
305 write_buf[1] = (addr & 0xff);
306
307 if ((ret = ifx_ssc_txrx (write_buf, 2, buf, eff)) != eff) {
308 AMAZON_EEPROM_EMSG ("ssc_txrx fails %d\n", ret);
309 ifx_ssc_cs_high (EEPROM_CS);
310 goto eeprom_read_err_out;
311 }
312 buf += ret;
313 len -= ret;
314 addr += ret;
315 if ((ret = ifx_ssc_cs_high (EEPROM_CS))) {
316 goto eeprom_read_err_out;
317 }
318 local_irq_restore (flag);
319 if (len <= 0) {
320 break;
321 }
322 }
323
324 return ret;
325 eeprom_read_err_out:
326 local_irq_restore (flag);
327 return ret;
328 }
329
330 /* Brief: write EEPROm
331 * Parameter:
332 * addr
333 * len
334 * buf
335 * Return: 0 ok
336 * -EFAULT
337 */
338 static int
339 eeprom_write (u32 addr, unsigned char *buf, u32 len)
340 {
341 int ret = 0;
342 u32 eff = 0;
343 unsigned char write_buf[2];
344
345 //unsigned char rx_buf[EEPROM_PAGE_SIZE];
346 //Format:
347 //CMD + ADDR + DATA (up to EEPROM_PAGE_SIZE)
348 while (1) {
349 eeprom_wip_over ();
350 if ((ret = eeprom_wren ())) {
351 AMAZON_EEPROM_EMSG ("eeprom_wren fails\n");
352 return ret;
353 }
354 write_buf[0] = EEPROM_WRITE | ((u8) ((addr & 0x100) >> 5));
355 write_buf[1] = (addr & 0xff);
356
357 eff = EEPROM_PAGE_SIZE - (addr % EEPROM_PAGE_SIZE);
358 eff = (eff < len) ? eff : len;
359 AMAZON_EEPROM_DMSG ("EEPROM Write:\n");
360 #if 0
361 int i;
362 for (i = 0; i < eff; i++) {
363 printk ("%2x ", buf[i]);
364 if (i % 16 == 15)
365 printk ("\n");
366 }
367 printk ("\n");
368 #endif
369
370 if ((ret = ifx_ssc_cs_low (EEPROM_CS))) {
371 return ret;
372 }
373
374 if ((ret = ifx_ssc_tx (write_buf, 2)) < 0) {
375 AMAZON_EEPROM_EMSG ("ssc_tx fails %d\n", ret);
376 ifx_ssc_cs_high (EEPROM_CS);
377 return ret;
378 }
379
380 if ((ret = ifx_ssc_tx (buf, eff)) != eff) {
381 AMAZON_EEPROM_EMSG ("ssc_tx fails %d\n", ret);
382 ifx_ssc_cs_high (EEPROM_CS);
383 return ret;
384 }
385
386 buf += ret;
387 len -= ret;
388 addr += ret;
389
390 if ((ret = ifx_ssc_cs_high (EEPROM_CS))) {
391 return ret;
392 }
393 #if 0
394 printk ("<==");
395 eeprom_read ((addr - eff), rx_buf, eff);
396 for (i = 0; i < eff; i++) {
397 printk ("[%x]", rx_buf[i]);
398 }
399 printk ("\n");
400 #endif
401 if (len <= 0)
402 break;
403 }
404
405 return ret;
406 }
407
408 int
409 danube_eeprom_open (struct inode *inode, struct file *filp)
410 {
411 filp->f_pos = 0;
412 return 0;
413 }
414
415 int
416 danube_eeprom_close (struct inode *inode, struct file *filp)
417 {
418 return 0;
419 }
420
421 int
422 danube_eeprom_ioctl (struct inode *inode, struct file *filp, unsigned int cmd,
423 unsigned long data)
424 {
425 return 0;
426 }
427
428 ssize_t
429 danube_eeprom_kread (char *buf, size_t len, u32 addr)
430 {
431 int ret = 0;
432 u32 data;
433 printk ("addr:=%d\n", addr);
434 printk ("len:=%d\n", len);
435 if ((addr + len) > EEPROM_SIZE) {
436 AMAZON_EEPROM_EMSG ("invalid len\n");
437 addr = 0;
438 len = EEPROM_SIZE / 2;
439 }
440
441 if ((ret = ifx_ssc_open ((struct inode *) 0, NULL))) {
442 AMAZON_EEPROM_EMSG ("danube_eeprom_open fails\n");
443 goto read_err_out;
444 }
445
446 data = (u32) IFX_SSC_MODE_RXTX;
447 if ((ret =
448 ifx_ssc_ioctl ((struct inode *) 0, NULL, IFX_SSC_RXTX_MODE_SET,
449 (unsigned long) &data))) {
450 AMAZON_EEPROM_EMSG ("set RXTX mode fails\n");
451 goto read_err_out;
452 }
453
454 if ((ret = eeprom_wrsr ())) {
455 AMAZON_EEPROM_EMSG ("EEPROM reset fails\n");
456 goto read_err_out;
457 }
458
459 if ((ret = eeprom_read (addr, buf, len))) {
460 AMAZON_EEPROM_EMSG ("eeprom read fails\n");
461 goto read_err_out;
462 }
463
464 read_err_out:
465 if (ifx_ssc_close ((struct inode *) 0, NULL)) {
466 AMAZON_EEPROM_EMSG ("danube_eeprom_close fails\n");
467 }
468 return len;
469
470 }
471
472 EXPORT_SYMBOL (danube_eeprom_kread);
473
474 static ssize_t
475 danube_eeprom_read (struct file *filp, char *ubuf, size_t len, loff_t * off)
476 {
477 int ret = 0;
478 unsigned char ssc_rx_buf[EEPROM_SIZE];
479 long flag;
480 if (*off >= EEPROM_SIZE)
481 return 0;
482 if (*off + len > EEPROM_SIZE)
483 len = EEPROM_SIZE - *off;
484 if (len == 0)
485 return 0;
486 local_irq_save (flag);
487 if ((ret = danube_eeprom_kread (ssc_rx_buf, len, *off)) < 0) {
488 AMAZON_EEPROM_EMSG ("read fails, err=%x\n", ret);
489 local_irq_restore (flag);
490 return ret;
491 }
492 if (copy_to_user ((void *) ubuf, ssc_rx_buf, ret) != 0) {
493 local_irq_restore (flag);
494 ret = -EFAULT;
495 }
496 local_irq_restore (flag);
497 *off += len;
498 return len;
499 }
500
501 ssize_t
502 danube_eeprom_kwrite (char *buf, size_t len, u32 addr)
503 {
504 int ret = 0;
505 u32 data;
506 if ((ret = ifx_ssc_open ((struct inode *) 0, NULL))) {
507 AMAZON_EEPROM_EMSG ("danube_eeprom_open fails\n");
508 goto write_err_out;
509 }
510
511 data = (u32) IFX_SSC_MODE_RXTX;
512 if ((ret =
513 ifx_ssc_ioctl ((struct inode *) 0, NULL, IFX_SSC_RXTX_MODE_SET,
514 (unsigned long) &data))) {
515 AMAZON_EEPROM_EMSG ("set RXTX mode fails\n");
516 goto write_err_out;
517 }
518
519 if ((ret = eeprom_wrsr ())) {
520 AMAZON_EEPROM_EMSG ("EEPROM reset fails\n");
521 goto write_err_out;
522 }
523
524 if ((ret = eeprom_write (addr, buf, len))) {
525 AMAZON_EEPROM_EMSG ("eeprom write fails\n");
526 goto write_err_out;
527 }
528
529 write_err_out:
530 if (ifx_ssc_close ((struct inode *) 0, NULL)) {
531 AMAZON_EEPROM_EMSG ("danube_eeprom_close fails\n");
532 }
533 return ret;
534 }
535
536 EXPORT_SYMBOL (danube_eeprom_kwrite);
537
538 static ssize_t
539 danube_eeprom_write (struct file *filp, const char *ubuf, size_t len,
540 loff_t * off)
541 {
542 int ret = 0;
543 unsigned char ssc_tx_buf[EEPROM_SIZE];
544 if (*off >= EEPROM_SIZE)
545 return 0;
546 if (len + *off > EEPROM_SIZE)
547 len = EEPROM_SIZE - *off;
548 if ((ret = copy_from_user (ssc_tx_buf, ubuf, len))) {
549 return EFAULT;
550 }
551 ret = danube_eeprom_kwrite (ssc_tx_buf, len, *off);
552 if (ret > 0) {
553 *off = ret;
554 }
555 return ret;
556 }
557
558 loff_t
559 danube_eeprom_llseek (struct file * filp, loff_t off, int whence)
560 {
561 loff_t newpos;
562 switch (whence) {
563 case 0: /*SEEK_SET */
564 newpos = off;
565 break;
566 case 1: /*SEEK_CUR */
567 newpos = filp->f_pos + off;
568 break;
569 default:
570 return -EINVAL;
571 }
572 if (newpos < 0)
573 return -EINVAL;
574 filp->f_pos = newpos;
575 return newpos;
576 }
577
578 static struct file_operations danube_eeprom_fops = {
579 owner:THIS_MODULE,
580 llseek:danube_eeprom_llseek,
581 /* llseek */
582 read:danube_eeprom_read, /* read */
583 write:danube_eeprom_write,
584 /* write */
585 ioctl:danube_eeprom_ioctl,
586 /* ioctl */
587 open:danube_eeprom_open, /* open */
588 release:danube_eeprom_close,
589 /* release */
590 };
591
592 int __init
593 danube_eeprom_init (void)
594 {
595 int ret = 0;
596
597 if ((ret = register_chrdev (maj, "eeprom", &danube_eeprom_fops)) < 0) {
598 printk ("Unable to register major %d for the Infineon Amazon EEPROM\n", maj);
599 if (maj == 0) {
600 goto errout;
601 }
602 else {
603 maj = 0;
604 if ((ret =
605 register_chrdev (maj, "ssc",
606 &danube_eeprom_fops)) < 0) {
607 printk ("Unable to register major 0 for the Infineon Amazon EEPROM\n");
608 goto errout;
609 }
610 }
611 }
612 if (maj == 0)
613 maj = ret;
614 errout:
615 return ret;
616 }
617
618 void __exit
619 danube_eeprom_cleanup_module (void)
620 {
621 if (unregister_chrdev (maj, "eeprom")) {
622 printk ("Unable to unregister major %d for the EEPROM\n",
623 maj);
624 }
625 }
626
627 module_exit (danube_eeprom_cleanup_module);
628 module_init (danube_eeprom_init);
629
630 #ifndef MODULE
631 static int __init
632 danube_eeprom_set_maj (char *str)
633 {
634 maj = simple_strtol (str, NULL, 0);
635 return 1;
636 }
637
638 __setup ("eeprom_maj=", danube_eeprom_set_maj);
639 #endif /* !MODULE */