kernel: fix automatic rootfs partition selection on 2.6.31
[10.03/openwrt.git] / target / linux / ifxmips / files / drivers / char / ifxmips_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 <linux/io.h>
47 #include <linux/irq.h>
48 #include <linux/uaccess.h>
49 #include <linux/bitops.h>
50
51 #include <linux/types.h>
52 #include <linux/kernel.h>
53 #include <linux/version.h>
54
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>
60
61 /* allow the user to set the major device number */
62 static int ifxmips_eeprom_maj;
63
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);
72
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);
78
79 #define EEPROM_CS IFX_SSC_WHBGPOSTAT_OUT0_POS
80
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
90
91 static int eeprom_rdsr(void)
92 {
93         int ret = 0;
94         unsigned char cmd = EEPROM_RDSR;
95         unsigned long flag;
96         char status;
97
98         local_irq_save(flag);
99
100         if ((ret = ifx_ssc_cs_low(EEPROM_CS)) == 0)
101                 if ((ret = ifx_ssc_txrx(&cmd, 1, &status, 1)) >= 0)
102                         ret = status & 1;
103
104         ifx_ssc_cs_high(EEPROM_CS);
105         local_irq_restore(flag);
106
107         return ret;
108 }
109
110 void eeprom_wip_over(void)
111 {
112         while (eeprom_rdsr())
113                 printk(KERN_INFO "waiting for eeprom\n");
114 }
115
116 static int eeprom_wren(void)
117 {
118         unsigned char cmd = EEPROM_WREN;
119         int ret = 0;
120         unsigned long flag;
121
122         local_irq_save(flag);
123         if ((ret = ifx_ssc_cs_low(EEPROM_CS)) == 0)
124                 if ((ret = ifx_ssc_tx(&cmd, 1)) >= 0)
125                         ret = 0;
126
127         ifx_ssc_cs_high(EEPROM_CS);
128         local_irq_restore(flag);
129
130         if (!ret)
131                 eeprom_wip_over();
132
133         return ret;
134 }
135
136 static int eeprom_wrsr(void)
137 {
138         int ret = 0;
139         unsigned char cmd[2];
140         unsigned long flag;
141
142         cmd[0] = EEPROM_WRSR;
143         cmd[1] = 0;
144
145         if ((ret = eeprom_wren())) {
146                 printk(KERN_ERR "eeprom_wren fails\n");
147                 goto out1;
148         }
149
150         local_irq_save(flag);
151
152         if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
153                 goto out;
154
155         if ((ret = ifx_ssc_tx(cmd, 2)) < 0) {
156                 ifx_ssc_cs_high(EEPROM_CS);
157                 goto out;
158         }
159
160         if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
161                 goto out;
162
163         local_irq_restore(flag);
164         eeprom_wip_over();
165
166         return ret;
167
168 out:
169         local_irq_restore(flag);
170         eeprom_wip_over();
171
172 out1:
173         return ret;
174 }
175
176 static int eeprom_read(unsigned int addr, unsigned char *buf, unsigned int len)
177 {
178         int ret = 0;
179         unsigned char write_buf[2];
180         unsigned int eff = 0;
181         unsigned long flag;
182
183         while (1) {
184                 eeprom_wip_over();
185                 eff = EEPROM_PAGE_SIZE - (addr % EEPROM_PAGE_SIZE);
186                 eff = (eff < len) ? eff : len;
187                 local_irq_save(flag);
188
189                 if ((ret = ifx_ssc_cs_low(EEPROM_CS)) < 0)
190                         goto out;
191
192                 write_buf[0] = EEPROM_READ | ((unsigned char)((addr & 0x100) >> 5));
193                 write_buf[1] = (addr & 0xff);
194
195                 ret = ifx_ssc_txrx(write_buf, 2, buf, eff);
196                 if (ret != eff) {
197                         printk(KERN_ERR "ssc_txrx fails %d\n", ret);
198                         ifx_ssc_cs_high(EEPROM_CS);
199                         goto out;
200                 }
201
202                 buf += ret;
203                 len -= ret;
204                 addr += ret;
205
206                 ret = ifx_ssc_cs_high(EEPROM_CS);
207                 if (ret)
208                         goto out;
209
210                 local_irq_restore(flag);
211
212                 if (len <= 0)
213                         goto out2;
214         }
215
216 out:
217         local_irq_restore(flag);
218 out2:
219         return ret;
220 }
221
222 static int eeprom_write(unsigned int addr, unsigned char *buf, unsigned int len)
223 {
224         int ret = 0;
225         unsigned int eff = 0;
226         unsigned char write_buf[2];
227         int i;
228         unsigned char rx_buf[EEPROM_PAGE_SIZE];
229
230         while (1) {
231                 eeprom_wip_over();
232
233                 if ((ret = eeprom_wren())) {
234                         printk(KERN_ERR "eeprom_wren fails\n");
235                         goto out;
236                 }
237
238                 write_buf[0] = EEPROM_WRITE | ((unsigned char)((addr & 0x100) >> 5));
239                 write_buf[1] = (addr & 0xff);
240
241                 eff = EEPROM_PAGE_SIZE - (addr % EEPROM_PAGE_SIZE);
242                 eff = (eff < len) ? eff : len;
243
244                 printk(KERN_INFO "EEPROM Write:\n");
245                 for (i = 0; i < eff; i++) {
246                         printk("%2x ", buf[i]);
247                         if ((i % 16) == 15)
248                                 printk("\n");
249                 }
250                 printk("\n");
251
252                 if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
253                         goto out;
254
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);
258                         goto out;
259                 }
260
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);
264                         goto out;
265                 }
266
267                 buf += ret;
268                 len -= ret;
269                 addr += ret;
270
271                 if ((ret = ifx_ssc_cs_high(EEPROM_CS)))
272                         goto out;
273
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]);
278                 printk("\n");
279
280                 if (len <= 0)
281                         break;
282         }
283
284 out:
285         return ret;
286 }
287
288 int ifxmips_eeprom_open(struct inode *inode, struct file *filp)
289 {
290         filp->f_pos = 0;
291         return 0;
292 }
293
294 int ifxmips_eeprom_close(struct inode *inode, struct file *filp)
295 {
296         return 0;
297 }
298
299 int ifxmips_eeprom_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long data)
300 {
301         return 0;
302 }
303
304 ssize_t ifxmips_eeprom_read(char *buf, size_t len, unsigned int addr)
305 {
306         int ret = 0;
307         unsigned int data;
308
309         printk(KERN_INFO "addr:=%d\n", addr);
310         printk(KERN_INFO "len:=%d\n", len);
311
312         if ((addr + len) > EEPROM_SIZE) {
313                 printk(KERN_ERR "invalid len\n");
314                 addr = 0;
315                 len = EEPROM_SIZE / 2;
316         }
317
318         if ((ret = ifx_ssc_open((struct inode *)0, NULL))) {
319                 printk(KERN_ERR "ifxmips_ssc_open fails\n");
320                 goto out;
321         }
322
323         data = (unsigned int)IFX_SSC_MODE_RXTX;
324
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");
327                 goto out;
328         }
329
330         if ((ret = eeprom_wrsr())) {
331                 printk(KERN_ERR "EEPROM reset fails\n");
332                 goto out;
333         }
334
335         if ((ret = eeprom_read(addr, buf, len))) {
336                 printk(KERN_ERR "eeprom read fails\n");
337                 goto out;
338         }
339
340 out:
341         if (ifx_ssc_close((struct inode *)0, NULL))
342                 printk(KERN_ERR "ifxmips_ssc_close fails\n");
343
344         return len;
345 }
346 EXPORT_SYMBOL(ifxmips_eeprom_read);
347
348 static ssize_t ifxmips_eeprom_fops_read(struct file *filp, char *ubuf, size_t len, loff_t *off)
349 {
350         int ret = 0;
351         unsigned char ssc_rx_buf[EEPROM_SIZE];
352         unsigned long flag;
353
354         if (*off >= EEPROM_SIZE)
355                 return 0;
356
357         if (*off + len > EEPROM_SIZE)
358                 len = EEPROM_SIZE - *off;
359
360         if (len == 0)
361                 return 0;
362
363         local_irq_save(flag);
364
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);
368                 return ret;
369         }
370
371         if (copy_to_user((void *)ubuf, ssc_rx_buf, ret) != 0) {
372                 local_irq_restore(flag);
373                 ret = -EFAULT;
374         }
375
376         local_irq_restore(flag);
377         *off += len;
378
379         return len;
380 }
381
382 ssize_t ifxmips_eeprom_write(char *buf, size_t len, unsigned int addr)
383 {
384         int ret = 0;
385         unsigned int data;
386
387         if ((ret = ifx_ssc_open((struct inode *)0, NULL))) {
388                 printk(KERN_ERR "ifxmips_ssc_open fails\n");
389                 goto out;
390         }
391
392         data = (unsigned int) IFX_SSC_MODE_RXTX;
393
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");
396                 goto out;
397         }
398
399         if ((ret = eeprom_wrsr())) {
400                 printk(KERN_ERR "EEPROM reset fails\n");
401                 goto out;
402         }
403
404         if ((ret = eeprom_write(addr, buf, len))) {
405                 printk(KERN_ERR "eeprom write fails\n");
406                 goto out;
407         }
408
409 out:
410         if (ifx_ssc_close((struct inode *)0, NULL))
411                 printk(KERN_ERR "ifxmips_ssc_close fails\n");
412
413         return ret;
414 }
415 EXPORT_SYMBOL(ifxmips_eeprom_write);
416
417 static ssize_t ifxmips_eeprom_fops_write(struct file *filp, const char *ubuf, size_t len, loff_t *off)
418 {
419         int ret = 0;
420         unsigned char ssc_tx_buf[EEPROM_SIZE];
421
422         if (*off >= EEPROM_SIZE)
423                 return 0;
424
425         if (len + *off > EEPROM_SIZE)
426                 len = EEPROM_SIZE - *off;
427
428         if ((ret = copy_from_user(ssc_tx_buf, ubuf, len)))
429                 return EFAULT;
430
431         ret = ifxmips_eeprom_write(ssc_tx_buf, len, *off);
432
433         if (ret > 0)
434                 *off = ret;
435
436         return ret;
437 }
438
439 loff_t ifxmips_eeprom_llseek(struct file *filp, loff_t off, int whence)
440 {
441         loff_t newpos;
442         switch (whence) {
443         case SEEK_SET:
444                 newpos = off;
445                 break;
446
447         case SEEK_CUR:
448                 newpos = filp->f_pos + off;
449                 break;
450
451         default:
452                 return -EINVAL;
453         }
454
455         if (newpos < 0)
456                 return -EINVAL;
457
458         filp->f_pos = newpos;
459
460         return newpos;
461 }
462
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,
471 };
472
473 int __init ifxmips_eeprom_init(void)
474 {
475         int ret = 0;
476
477         ifxmips_eeprom_maj = register_chrdev(0, "eeprom", &ifxmips_eeprom_fops);
478
479         if (ifxmips_eeprom_maj < 0) {
480                 printk(KERN_ERR "failed to register eeprom device\n");
481                 ret = -EINVAL;
482
483                 goto out;
484         }
485
486         printk(KERN_INFO "ifxmips_eeprom : /dev/eeprom mayor %d\n", ifxmips_eeprom_maj);
487
488 out:
489         return ret;
490 }
491
492 void __exit ifxmips_eeprom_cleanup_module(void)
493 {
494         /*if (unregister_chrdev(ifxmips_eeprom_maj, "eeprom")) {
495                 printk(KERN_ERR "Unable to unregister major %d for the EEPROM\n",
496                         maj);
497         }*/
498 }
499
500 module_exit(ifxmips_eeprom_cleanup_module);
501 module_init(ifxmips_eeprom_init);
502
503 MODULE_LICENSE("GPL");
504 MODULE_AUTHOR("Peng Liu");
505 MODULE_DESCRIPTION("IFAP EEPROM driver");
506 MODULE_SUPPORTED_DEVICE("ifxmips_eeprom");
507