1 /******************************************************************************
3 ** FILE NAME : danube_eeprom.c
9 ** DESCRIPTION : X25040 EEPROM Driver
10 ** COPYRIGHT : Copyright (c) 2006
11 ** Infineon Technologies AG
12 ** Am Campeon 1-12, 85579 Neubiberg, Germany
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.
20 ** $Date $Author $Comment
21 ** 31 DEC 2004 Liu Peng Initiate Version
22 ** 25 OCT 2006 Xu Liang Add GPL header.
23 *******************************************************************************/
25 #define IFAP_EEPROM_DRV_VERSION "0.0.1"
28 **************************************************
30 * This driver was originally based on the INCA-IP driver, but due to
31 * fundamental conceptual drawbacks there has been changed a lot.
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.
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.
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.
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
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>
64 #include <linux/proc_fs.h>
65 #include <linux/fcntl.h>
66 #include <linux/ptrace.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>
76 #include <asm/uaccess.h>
77 #include <asm/bitops.h>
79 #include <linux/types.h>
80 #include <linux/kernel.h>
81 #include <linux/version.h>
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>
91 #define AMAZON_EEPROM_EMSG(fmt,args...) printk("%s:" fmt, __FUNCTION__, ##args)
94 #define AMAZON_EEPROM_DMSG(fmt,args...) printk("%s:" fmt, __FUNCTION__, ##args)
96 #define AMAZON_EEPROM_DMSG(fmt,args...)
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");
106 /* allow the user to set the major device number */
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,
112 static int danube_eeprom_ioctl (struct inode
*, struct file
*, unsigned int,
114 static int danube_eeprom_open (struct inode
*, struct file
*);
115 static int danube_eeprom_close (struct inode
*, struct file
*);
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
);
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
);
133 /* CS: board dependant */
134 #define EEPROM_CS IFX_SSC_WHBGPOSTAT_OUT0_POS
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
146 /* Brief: EEPROM_RDSR command
151 eeprom_rdsr (char *status
)
154 unsigned char cmd
= EEPROM_RDSR
;
157 local_irq_save (flag
);
158 if ((ret
= ifx_ssc_cs_low (EEPROM_CS
))) {
159 local_irq_restore (flag
);
162 if ((ret
= ifx_ssc_txrx (&cmd
, 1, status
, 1)) < 0) {
163 ifx_ssc_cs_high (EEPROM_CS
);
164 local_irq_restore (flag
);
168 if ((ret
= ifx_ssc_cs_high (EEPROM_CS
))) {
169 local_irq_restore (flag
);
172 local_irq_restore (flag
);
176 /* Brief: wait for WIP is over
182 eeprom_wip_over (void)
187 ret
= eeprom_rdsr (&status
);
188 AMAZON_EEPROM_DMSG ("status %x \n", status
);
190 AMAZON_EEPROM_EMSG ("read back status fails %d\n",
194 else if (((status
) & 1) != 0) {
195 AMAZON_EEPROM_DMSG ("read back status not zero %x\n",
205 /* Brief: EEPROM_WREN command
212 unsigned char cmd
= EEPROM_WREN
;
216 local_irq_save (flag
);
217 if ((ret
= ifx_ssc_cs_low (EEPROM_CS
))) {
218 local_irq_restore (flag
);
222 if ((ret
= ifx_ssc_tx (&cmd
, 1)) < 0) {
223 ifx_ssc_cs_high (EEPROM_CS
);
224 local_irq_restore (flag
);
228 if ((ret
= ifx_ssc_cs_high (EEPROM_CS
))) {
229 local_irq_restore (flag
);
232 local_irq_restore (flag
);
238 /* Brief: EEPROM_WRSR command
246 unsigned char cmd
[2];
247 cmd
[0] = EEPROM_WRSR
;
249 if ((ret
= eeprom_wren ())) {
250 AMAZON_EEPROM_EMSG ("eeprom_wren fails\n");
254 local_irq_save (flag
);
255 if ((ret
= ifx_ssc_cs_low (EEPROM_CS
))) {
256 goto eeprom_wrsr_err_out
;
259 if ((ret
= ifx_ssc_tx (cmd
, 2)) < 0) {
260 ifx_ssc_cs_high (EEPROM_CS
);
261 goto eeprom_wrsr_err_out
;
264 if ((ret
= ifx_ssc_cs_low (EEPROM_CS
))) {
265 goto eeprom_wrsr_err_out
;
267 local_irq_restore (flag
);
271 local_irq_restore (flag
);
276 /* Brief: read EEPROM
285 eeprom_read (u32 addr
, unsigned char *buf
, u32 len
)
288 unsigned char write_buf
[2];
295 eff
= EEPROM_PAGE_SIZE
- (addr
% EEPROM_PAGE_SIZE
);
296 eff
= (eff
< len
) ? eff
: len
;
299 local_irq_save (flag
);
300 if ((ret
= ifx_ssc_cs_low (EEPROM_CS
)) < 0) {
301 goto eeprom_read_err_out
;
304 write_buf
[0] = EEPROM_READ
| ((u8
) ((addr
& 0x100) >> 5));
305 write_buf
[1] = (addr
& 0xff);
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
;
315 if ((ret
= ifx_ssc_cs_high (EEPROM_CS
))) {
316 goto eeprom_read_err_out
;
318 local_irq_restore (flag
);
326 local_irq_restore (flag
);
330 /* Brief: write EEPROm
339 eeprom_write (u32 addr
, unsigned char *buf
, u32 len
)
343 unsigned char write_buf
[2];
345 //unsigned char rx_buf[EEPROM_PAGE_SIZE];
347 //CMD + ADDR + DATA (up to EEPROM_PAGE_SIZE)
350 if ((ret
= eeprom_wren ())) {
351 AMAZON_EEPROM_EMSG ("eeprom_wren fails\n");
354 write_buf
[0] = EEPROM_WRITE
| ((u8
) ((addr
& 0x100) >> 5));
355 write_buf
[1] = (addr
& 0xff);
357 eff
= EEPROM_PAGE_SIZE
- (addr
% EEPROM_PAGE_SIZE
);
358 eff
= (eff
< len
) ? eff
: len
;
359 AMAZON_EEPROM_DMSG ("EEPROM Write:\n");
362 for (i
= 0; i
< eff
; i
++) {
363 printk ("%2x ", buf
[i
]);
370 if ((ret
= ifx_ssc_cs_low (EEPROM_CS
))) {
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
);
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
);
390 if ((ret
= ifx_ssc_cs_high (EEPROM_CS
))) {
395 eeprom_read ((addr
- eff
), rx_buf
, eff
);
396 for (i
= 0; i
< eff
; i
++) {
397 printk ("[%x]", rx_buf
[i
]);
409 danube_eeprom_open (struct inode
*inode
, struct file
*filp
)
416 danube_eeprom_close (struct inode
*inode
, struct file
*filp
)
422 danube_eeprom_ioctl (struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
429 danube_eeprom_kread (char *buf
, size_t len
, u32 addr
)
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");
438 len
= EEPROM_SIZE
/ 2;
441 if ((ret
= ifx_ssc_open ((struct inode
*) 0, NULL
))) {
442 AMAZON_EEPROM_EMSG ("danube_eeprom_open fails\n");
446 data
= (u32
) IFX_SSC_MODE_RXTX
;
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");
454 if ((ret
= eeprom_wrsr ())) {
455 AMAZON_EEPROM_EMSG ("EEPROM reset fails\n");
459 if ((ret
= eeprom_read (addr
, buf
, len
))) {
460 AMAZON_EEPROM_EMSG ("eeprom read fails\n");
465 if (ifx_ssc_close ((struct inode
*) 0, NULL
)) {
466 AMAZON_EEPROM_EMSG ("danube_eeprom_close fails\n");
472 EXPORT_SYMBOL (danube_eeprom_kread
);
475 danube_eeprom_read (struct file
*filp
, char *ubuf
, size_t len
, loff_t
* off
)
478 unsigned char ssc_rx_buf
[EEPROM_SIZE
];
480 if (*off
>= EEPROM_SIZE
)
482 if (*off
+ len
> EEPROM_SIZE
)
483 len
= EEPROM_SIZE
- *off
;
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
);
492 if (copy_to_user ((void *) ubuf
, ssc_rx_buf
, ret
) != 0) {
493 local_irq_restore (flag
);
496 local_irq_restore (flag
);
502 danube_eeprom_kwrite (char *buf
, size_t len
, u32 addr
)
506 if ((ret
= ifx_ssc_open ((struct inode
*) 0, NULL
))) {
507 AMAZON_EEPROM_EMSG ("danube_eeprom_open fails\n");
511 data
= (u32
) IFX_SSC_MODE_RXTX
;
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");
519 if ((ret
= eeprom_wrsr ())) {
520 AMAZON_EEPROM_EMSG ("EEPROM reset fails\n");
524 if ((ret
= eeprom_write (addr
, buf
, len
))) {
525 AMAZON_EEPROM_EMSG ("eeprom write fails\n");
530 if (ifx_ssc_close ((struct inode
*) 0, NULL
)) {
531 AMAZON_EEPROM_EMSG ("danube_eeprom_close fails\n");
536 EXPORT_SYMBOL (danube_eeprom_kwrite
);
539 danube_eeprom_write (struct file
*filp
, const char *ubuf
, size_t len
,
543 unsigned char ssc_tx_buf
[EEPROM_SIZE
];
544 if (*off
>= EEPROM_SIZE
)
546 if (len
+ *off
> EEPROM_SIZE
)
547 len
= EEPROM_SIZE
- *off
;
548 if ((ret
= copy_from_user (ssc_tx_buf
, ubuf
, len
))) {
551 ret
= danube_eeprom_kwrite (ssc_tx_buf
, len
, *off
);
559 danube_eeprom_llseek (struct file
* filp
, loff_t off
, int whence
)
563 case 0: /*SEEK_SET */
566 case 1: /*SEEK_CUR */
567 newpos
= filp
->f_pos
+ off
;
574 filp
->f_pos
= newpos
;
578 static struct file_operations danube_eeprom_fops
= {
580 llseek
:danube_eeprom_llseek
,
582 read
:danube_eeprom_read
, /* read */
583 write
:danube_eeprom_write
,
585 ioctl
:danube_eeprom_ioctl
,
587 open
:danube_eeprom_open
, /* open */
588 release
:danube_eeprom_close
,
593 danube_eeprom_init (void)
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
);
605 register_chrdev (maj
, "ssc",
606 &danube_eeprom_fops
)) < 0) {
607 printk ("Unable to register major 0 for the Infineon Amazon EEPROM\n");
619 danube_eeprom_cleanup_module (void)
621 if (unregister_chrdev (maj
, "eeprom")) {
622 printk ("Unable to unregister major %d for the EEPROM\n",
627 module_exit (danube_eeprom_cleanup_module
);
628 module_init (danube_eeprom_init
);
632 danube_eeprom_set_maj (char *str
)
634 maj
= simple_strtol (str
, NULL
, 0);
638 __setup ("eeprom_maj=", danube_eeprom_set_maj
);