2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 * This driver was originally based on the INCA-IP driver, but due to
17 * fundamental conceptual drawbacks there has been changed a lot.
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.
22 * Copyright(C) 2006 infineon
23 * Copyright(C) 2007 John Crispin <blogic@openwrt.org>
27 #define IFAP_EEPROM_DRV_VERSION "0.0.1"
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>
38 #include <linux/fcntl.h>
39 #include <linux/ptrace.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>
47 #include <linux/irq.h>
48 #include <linux/uaccess.h>
49 #include <linux/bitops.h>
51 #include <linux/types.h>
52 #include <linux/kernel.h>
53 #include <linux/version.h>
55 #include <asm/system.h>
56 #include <asm/ifxmips/ifxmips.h>
57 #include <asm/ifxmips/ifxmips_irq.h>
58 #include <asm/ifxmips/ifx_ssc_defines.h>
59 #include <asm/ifxmips/ifx_ssc.h>
61 /* allow the user to set the major device number */
62 static int ifxmips_eeprom_maj
;
64 extern int ifx_ssc_init(void);
65 extern int ifx_ssc_open(struct inode
*inode
, struct file
*filp
);
66 extern int ifx_ssc_close(struct inode
*inode
, struct file
*filp
);
67 extern void ifx_ssc_cleanup_module(void);
68 extern int ifx_ssc_ioctl(struct inode
*inode
, struct file
*filp
,
69 unsigned int cmd
, unsigned long data
);
70 extern ssize_t
ifx_ssc_kwrite(int port
, const char *kbuf
, size_t len
);
71 extern ssize_t
ifx_ssc_kread(int port
, char *kbuf
, size_t len
);
73 extern int ifx_ssc_cs_low(unsigned int pin
);
74 extern int ifx_ssc_cs_high(unsigned int pin
);
75 extern int ifx_ssc_txrx(char *tx_buf
, unsigned int tx_len
, char *rx_buf
, unsigned int rx_len
);
76 extern int ifx_ssc_tx(char *tx_buf
, unsigned int tx_len
);
77 extern int ifx_ssc_rx(char *rx_buf
, unsigned int rx_len
);
79 #define EEPROM_CS IFX_SSC_WHBGPOSTAT_OUT0_POS
81 /* commands for EEPROM, x25160, x25140 */
82 #define EEPROM_WREN ((unsigned char)0x06)
83 #define EEPROM_WRDI ((unsigned char)0x04)
84 #define EEPROM_RDSR ((unsigned char)0x05)
85 #define EEPROM_WRSR ((unsigned char)0x01)
86 #define EEPROM_READ ((unsigned char)0x03)
87 #define EEPROM_WRITE ((unsigned char)0x02)
88 #define EEPROM_PAGE_SIZE 4
89 #define EEPROM_SIZE 512
91 static int eeprom_rdsr(void)
94 unsigned char cmd
= EEPROM_RDSR
;
100 if ((ret
= ifx_ssc_cs_low(EEPROM_CS
)) == 0)
101 if ((ret
= ifx_ssc_txrx(&cmd
, 1, &status
, 1)) >= 0)
104 ifx_ssc_cs_high(EEPROM_CS
);
105 local_irq_restore(flag
);
110 void eeprom_wip_over(void)
112 while (eeprom_rdsr())
113 printk(KERN_INFO
"waiting for eeprom\n");
116 static int eeprom_wren(void)
118 unsigned char cmd
= EEPROM_WREN
;
122 local_irq_save(flag
);
123 if ((ret
= ifx_ssc_cs_low(EEPROM_CS
)) == 0)
124 if ((ret
= ifx_ssc_tx(&cmd
, 1)) >= 0)
127 ifx_ssc_cs_high(EEPROM_CS
);
128 local_irq_restore(flag
);
136 static int eeprom_wrsr(void)
139 unsigned char cmd
[2];
142 cmd
[0] = EEPROM_WRSR
;
145 if ((ret
= eeprom_wren())) {
146 printk(KERN_ERR
"eeprom_wren fails\n");
150 local_irq_save(flag
);
152 if ((ret
= ifx_ssc_cs_low(EEPROM_CS
)))
155 if ((ret
= ifx_ssc_tx(cmd
, 2)) < 0) {
156 ifx_ssc_cs_high(EEPROM_CS
);
160 if ((ret
= ifx_ssc_cs_low(EEPROM_CS
)))
163 local_irq_restore(flag
);
169 local_irq_restore(flag
);
176 static int eeprom_read(unsigned int addr
, unsigned char *buf
, unsigned int len
)
179 unsigned char write_buf
[2];
180 unsigned int eff
= 0;
185 eff
= EEPROM_PAGE_SIZE
- (addr
% EEPROM_PAGE_SIZE
);
186 eff
= (eff
< len
) ? eff
: len
;
187 local_irq_save(flag
);
189 if ((ret
= ifx_ssc_cs_low(EEPROM_CS
)) < 0)
192 write_buf
[0] = EEPROM_READ
| ((unsigned char)((addr
& 0x100) >> 5));
193 write_buf
[1] = (addr
& 0xff);
195 ret
= ifx_ssc_txrx(write_buf
, 2, buf
, eff
);
197 printk(KERN_ERR
"ssc_txrx fails %d\n", ret
);
198 ifx_ssc_cs_high(EEPROM_CS
);
206 ret
= ifx_ssc_cs_high(EEPROM_CS
);
210 local_irq_restore(flag
);
217 local_irq_restore(flag
);
222 static int eeprom_write(unsigned int addr
, unsigned char *buf
, unsigned int len
)
225 unsigned int eff
= 0;
226 unsigned char write_buf
[2];
228 unsigned char rx_buf
[EEPROM_PAGE_SIZE
];
233 if ((ret
= eeprom_wren())) {
234 printk(KERN_ERR
"eeprom_wren fails\n");
238 write_buf
[0] = EEPROM_WRITE
| ((unsigned char)((addr
& 0x100) >> 5));
239 write_buf
[1] = (addr
& 0xff);
241 eff
= EEPROM_PAGE_SIZE
- (addr
% EEPROM_PAGE_SIZE
);
242 eff
= (eff
< len
) ? eff
: len
;
244 printk(KERN_INFO
"EEPROM Write:\n");
245 for (i
= 0; i
< eff
; i
++) {
246 printk("%2x ", buf
[i
]);
252 if ((ret
= ifx_ssc_cs_low(EEPROM_CS
)))
255 if ((ret
= ifx_ssc_tx(write_buf
, 2)) < 0) {
256 printk(KERN_ERR
"ssc_tx fails %d\n", ret
);
257 ifx_ssc_cs_high(EEPROM_CS
);
261 if ((ret
= ifx_ssc_tx(buf
, eff
)) != eff
) {
262 printk(KERN_ERR
"ssc_tx fails %d\n", ret
);
263 ifx_ssc_cs_high(EEPROM_CS
);
271 if ((ret
= ifx_ssc_cs_high(EEPROM_CS
)))
274 printk(KERN_INFO
"<==");
275 eeprom_read((addr
- eff
), rx_buf
, eff
);
276 for (i
= 0; i
< eff
; i
++)
277 printk("[%x]", rx_buf
[i
]);
288 int ifxmips_eeprom_open(struct inode
*inode
, struct file
*filp
)
294 int ifxmips_eeprom_close(struct inode
*inode
, struct file
*filp
)
299 int ifxmips_eeprom_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
, unsigned long data
)
304 ssize_t
ifxmips_eeprom_read(char *buf
, size_t len
, unsigned int addr
)
309 printk(KERN_INFO
"addr:=%d\n", addr
);
310 printk(KERN_INFO
"len:=%d\n", len
);
312 if ((addr
+ len
) > EEPROM_SIZE
) {
313 printk(KERN_ERR
"invalid len\n");
315 len
= EEPROM_SIZE
/ 2;
318 if ((ret
= ifx_ssc_open((struct inode
*)0, NULL
))) {
319 printk(KERN_ERR
"ifxmips_ssc_open fails\n");
323 data
= (unsigned int)IFX_SSC_MODE_RXTX
;
325 if ((ret
= ifx_ssc_ioctl((struct inode
*)0, NULL
, IFX_SSC_RXTX_MODE_SET
, (unsigned long) &data
))) {
326 printk(KERN_ERR
"set RXTX mode fails\n");
330 if ((ret
= eeprom_wrsr())) {
331 printk(KERN_ERR
"EEPROM reset fails\n");
335 if ((ret
= eeprom_read(addr
, buf
, len
))) {
336 printk(KERN_ERR
"eeprom read fails\n");
341 if (ifx_ssc_close((struct inode
*)0, NULL
))
342 printk(KERN_ERR
"ifxmips_ssc_close fails\n");
346 EXPORT_SYMBOL(ifxmips_eeprom_read
);
348 static ssize_t
ifxmips_eeprom_fops_read(struct file
*filp
, char *ubuf
, size_t len
, loff_t
*off
)
351 unsigned char ssc_rx_buf
[EEPROM_SIZE
];
354 if (*off
>= EEPROM_SIZE
)
357 if (*off
+ len
> EEPROM_SIZE
)
358 len
= EEPROM_SIZE
- *off
;
363 local_irq_save(flag
);
365 if ((ret
= ifxmips_eeprom_read(ssc_rx_buf
, len
, *off
)) < 0) {
366 printk(KERN_ERR
"read fails, err=%x\n", ret
);
367 local_irq_restore(flag
);
371 if (copy_to_user((void *)ubuf
, ssc_rx_buf
, ret
) != 0) {
372 local_irq_restore(flag
);
376 local_irq_restore(flag
);
382 ssize_t
ifxmips_eeprom_write(char *buf
, size_t len
, unsigned int addr
)
387 if ((ret
= ifx_ssc_open((struct inode
*)0, NULL
))) {
388 printk(KERN_ERR
"ifxmips_ssc_open fails\n");
392 data
= (unsigned int) IFX_SSC_MODE_RXTX
;
394 if ((ret
= ifx_ssc_ioctl((struct inode
*)0, NULL
, IFX_SSC_RXTX_MODE_SET
, (unsigned long) &data
))) {
395 printk(KERN_ERR
"set RXTX mode fails\n");
399 if ((ret
= eeprom_wrsr())) {
400 printk(KERN_ERR
"EEPROM reset fails\n");
404 if ((ret
= eeprom_write(addr
, buf
, len
))) {
405 printk(KERN_ERR
"eeprom write fails\n");
410 if (ifx_ssc_close((struct inode
*)0, NULL
))
411 printk(KERN_ERR
"ifxmips_ssc_close fails\n");
415 EXPORT_SYMBOL(ifxmips_eeprom_write
);
417 static ssize_t
ifxmips_eeprom_fops_write(struct file
*filp
, const char *ubuf
, size_t len
, loff_t
*off
)
420 unsigned char ssc_tx_buf
[EEPROM_SIZE
];
422 if (*off
>= EEPROM_SIZE
)
425 if (len
+ *off
> EEPROM_SIZE
)
426 len
= EEPROM_SIZE
- *off
;
428 if ((ret
= copy_from_user(ssc_tx_buf
, ubuf
, len
)))
431 ret
= ifxmips_eeprom_write(ssc_tx_buf
, len
, *off
);
439 loff_t
ifxmips_eeprom_llseek(struct file
*filp
, loff_t off
, int whence
)
448 newpos
= filp
->f_pos
+ off
;
458 filp
->f_pos
= newpos
;
463 static struct file_operations ifxmips_eeprom_fops
= {
464 .owner
= THIS_MODULE
,
465 .llseek
= ifxmips_eeprom_llseek
,
466 .read
= ifxmips_eeprom_fops_read
,
467 .write
= ifxmips_eeprom_fops_write
,
468 .ioctl
= ifxmips_eeprom_ioctl
,
469 .open
= ifxmips_eeprom_open
,
470 .release
= ifxmips_eeprom_close
,
473 int __init
ifxmips_eeprom_init(void)
477 ifxmips_eeprom_maj
= register_chrdev(0, "eeprom", &ifxmips_eeprom_fops
);
479 if (ifxmips_eeprom_maj
< 0) {
480 printk(KERN_ERR
"failed to register eeprom device\n");
486 printk(KERN_INFO
"ifxmips_eeprom : /dev/eeprom mayor %d\n", ifxmips_eeprom_maj
);
492 void __exit
ifxmips_eeprom_cleanup_module(void)
494 /*if (unregister_chrdev(ifxmips_eeprom_maj, "eeprom")) {
495 printk(KERN_ERR "Unable to unregister major %d for the EEPROM\n",
500 module_exit(ifxmips_eeprom_cleanup_module
);
501 module_init(ifxmips_eeprom_init
);
503 MODULE_LICENSE("GPL");
504 MODULE_AUTHOR("Peng Liu");
505 MODULE_DESCRIPTION("IFAP EEPROM driver");
506 MODULE_SUPPORTED_DEVICE("ifxmips_eeprom");