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>
46 #include <asm/system.h>
49 #include <asm/uaccess.h>
50 #include <asm/bitops.h>
52 #include <linux/types.h>
53 #include <linux/kernel.h>
54 #include <linux/version.h>
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>
61 /* allow the user to set the major device number */
62 static int danube_eeprom_maj = 0;
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,
67 static int danube_eeprom_ioctl (struct inode *, struct file *, unsigned int,
69 static int danube_eeprom_open (struct inode *, struct file *);
70 static int danube_eeprom_close (struct inode *, struct file *);
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);
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);
88 #define EEPROM_CS IFX_SSC_WHBGPOSTAT_OUT0_POS
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
101 eeprom_rdsr (char *status)
104 unsigned char cmd = EEPROM_RDSR;
107 local_irq_save(flag);
109 if ((ret = ifx_ssc_cs_low (EEPROM_CS)))
111 local_irq_restore(flag);
115 if ((ret = ifx_ssc_txrx (&cmd, 1, status, 1)) < 0)
117 ifx_ssc_cs_high(EEPROM_CS);
118 local_irq_restore(flag);
122 if ((ret = ifx_ssc_cs_high(EEPROM_CS)))
124 local_irq_restore(flag);
128 local_irq_restore(flag);
135 eeprom_wip_over (void)
138 unsigned char status;
142 ret = eeprom_rdsr(&status);
143 printk("status %x \n", status);
147 printk("read back status fails %d\n", ret);
151 if (((status) & 1) != 0)
152 printk("read back status not zero %x\n", status);
163 unsigned char cmd = EEPROM_WREN;
167 local_irq_save(flag);
168 if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
170 local_irq_restore(flag);
174 if ((ret = ifx_ssc_tx(&cmd, 1)) < 0)
176 ifx_ssc_cs_high(EEPROM_CS);
177 local_irq_restore(flag);
181 if ((ret = ifx_ssc_cs_high(EEPROM_CS)))
183 local_irq_restore(flag);
187 local_irq_restore(flag);
198 unsigned char cmd[2];
201 cmd[0] = EEPROM_WRSR;
204 if ((ret = eeprom_wren()))
206 printk ("eeprom_wren fails\n");
210 local_irq_save(flag);
212 if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
215 if ((ret = ifx_ssc_tx(cmd, 2)) < 0) {
216 ifx_ssc_cs_high(EEPROM_CS);
220 if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
223 local_irq_restore(flag);
229 local_irq_restore (flag);
237 eeprom_read (unsigned int addr, unsigned char *buf, unsigned int len)
240 unsigned char write_buf[2];
241 unsigned int eff = 0;
247 eff = EEPROM_PAGE_SIZE - (addr % EEPROM_PAGE_SIZE);
248 eff = (eff < len) ? eff : len;
249 local_irq_save(flag);
251 if ((ret = ifx_ssc_cs_low(EEPROM_CS)) < 0)
254 write_buf[0] = EEPROM_READ | ((unsigned char) ((addr & 0x100) >> 5));
255 write_buf[1] = (addr & 0xff);
257 if ((ret = ifx_ssc_txrx (write_buf, 2, buf, eff)) != eff)
259 printk("ssc_txrx fails %d\n", ret);
260 ifx_ssc_cs_high (EEPROM_CS);
268 if ((ret = ifx_ssc_cs_high(EEPROM_CS)))
271 local_irq_restore(flag);
278 local_irq_restore (flag);
284 eeprom_write (unsigned int addr, unsigned char *buf, unsigned int len)
287 unsigned int eff = 0;
288 unsigned char write_buf[2];
290 unsigned char rx_buf[EEPROM_PAGE_SIZE];
296 if ((ret = eeprom_wren()))
298 printk("eeprom_wren fails\n");
302 write_buf[0] = EEPROM_WRITE | ((unsigned char) ((addr & 0x100) >> 5));
303 write_buf[1] = (addr & 0xff);
305 eff = EEPROM_PAGE_SIZE - (addr % EEPROM_PAGE_SIZE);
306 eff = (eff < len) ? eff : len;
308 printk("EEPROM Write:\n");
309 for (i = 0; i < eff; i++) {
310 printk("%2x ", buf[i]);
316 if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
319 if ((ret = ifx_ssc_tx (write_buf, 2)) < 0)
321 printk("ssc_tx fails %d\n", ret);
322 ifx_ssc_cs_high(EEPROM_CS);
326 if ((ret = ifx_ssc_tx (buf, eff)) != eff)
328 printk("ssc_tx fails %d\n", ret);
329 ifx_ssc_cs_high(EEPROM_CS);
337 if ((ret = ifx_ssc_cs_high (EEPROM_CS)))
341 eeprom_read((addr - eff), rx_buf, eff);
342 for (i = 0; i < eff; i++)
344 printk ("[%x]", rx_buf[i]);
357 danube_eeprom_open (struct inode *inode, struct file *filp)
364 danube_eeprom_close (struct inode *inode, struct file *filp)
370 danube_eeprom_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long data)
376 danube_eeprom_read (char *buf, size_t len, unsigned int addr)
381 printk("addr:=%d\n", addr);
382 printk("len:=%d\n", len);
384 if ((addr + len) > EEPROM_SIZE)
386 printk("invalid len\n");
388 len = EEPROM_SIZE / 2;
391 if ((ret = ifx_ssc_open ((struct inode *) 0, NULL)))
393 printk("danube_eeprom_open fails\n");
397 data = (unsigned int)IFX_SSC_MODE_RXTX;
399 if ((ret = ifx_ssc_ioctl ((struct inode *) 0, NULL, IFX_SSC_RXTX_MODE_SET, (unsigned long) &data)))
401 printk("set RXTX mode fails\n");
405 if ((ret = eeprom_wrsr ()))
407 printk("EEPROM reset fails\n");
411 if ((ret = eeprom_read (addr, buf, len)))
413 printk("eeprom read fails\n");
418 if (ifx_ssc_close ((struct inode *) 0, NULL))
419 printk("danube_eeprom_close fails\n");
423 EXPORT_SYMBOL(danube_eeprom_read);
426 danube_eeprom_fops_read (struct file *filp, char *ubuf, size_t len, loff_t * off)
429 unsigned char ssc_rx_buf[EEPROM_SIZE];
432 if (*off >= EEPROM_SIZE)
435 if (*off + len > EEPROM_SIZE)
436 len = EEPROM_SIZE - *off;
441 local_irq_save(flag);
443 if ((ret = danube_eeprom_read(ssc_rx_buf, len, *off)) < 0)
445 printk("read fails, err=%x\n", ret);
446 local_irq_restore(flag);
450 if (copy_to_user((void*)ubuf, ssc_rx_buf, ret) != 0)
452 local_irq_restore(flag);
456 local_irq_restore(flag);
463 danube_eeprom_write (char *buf, size_t len, unsigned int addr)
468 if ((ret = ifx_ssc_open ((struct inode *) 0, NULL)))
470 printk ("danube_eeprom_open fails\n");
474 data = (unsigned int) IFX_SSC_MODE_RXTX;
476 if ((ret = ifx_ssc_ioctl ((struct inode *) 0, NULL, IFX_SSC_RXTX_MODE_SET, (unsigned long) &data)))
478 printk ("set RXTX mode fails\n");
482 if ((ret = eeprom_wrsr ())) {
483 printk ("EEPROM reset fails\n");
487 if ((ret = eeprom_write (addr, buf, len))) {
488 printk ("eeprom write fails\n");
493 if (ifx_ssc_close ((struct inode *) 0, NULL))
494 printk ("danube_eeprom_close fails\n");
498 EXPORT_SYMBOL(danube_eeprom_write);
501 danube_eeprom_fops_write (struct file *filp, const char *ubuf, size_t len, loff_t * off)
504 unsigned char ssc_tx_buf[EEPROM_SIZE];
506 if (*off >= EEPROM_SIZE)
509 if (len + *off > EEPROM_SIZE)
510 len = EEPROM_SIZE - *off;
512 if ((ret = copy_from_user (ssc_tx_buf, ubuf, len)))
515 ret = danube_eeprom_write (ssc_tx_buf, len, *off);
524 danube_eeprom_llseek (struct file * filp, loff_t off, int whence)
533 newpos = filp->f_pos + off;
543 filp->f_pos = newpos;
548 static struct file_operations danube_eeprom_fops = {
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,
559 danube_eeprom_init (void)
563 danube_eeprom_maj = register_chrdev(0, "eeprom", &danube_eeprom_fops);
565 if (danube_eeprom_maj < 0)
567 printk("failed to register eeprom device\n");
573 printk("danube_eeprom : /dev/eeprom mayor %d\n", danube_eeprom_maj);
580 danube_eeprom_cleanup_module (void)
582 /*if (unregister_chrdev (danube_eeprom_maj, "eeprom")) {
583 printk ("Unable to unregister major %d for the EEPROM\n",
588 module_exit (danube_eeprom_cleanup_module);
589 module_init (danube_eeprom_init);
591 MODULE_LICENSE ("GPL");
592 MODULE_AUTHOR ("Peng Liu");
593 MODULE_DESCRIPTION ("IFAP EEPROM driver");
594 MODULE_SUPPORTED_DEVICE ("danube_eeprom");