summaryrefslogtreecommitdiff
path: root/target/linux/ifxmips/files/drivers/char/ifxmips_eeprom.c
blob: ea1303cd66e9065b804bac6e41511a5e574e42f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 *   This driver was originally based on the INCA-IP driver, but due to
 *   fundamental conceptual drawbacks there has been changed a lot.
 *
 *   Based on INCA-IP driver Copyright (c) 2003 Gary Jennejohn <gj@denx.de>
 *   Based on the VxWorks drivers Copyright (c) 2002, Infineon Technologies.
 *
 *   Copyright (C) 2006 infineon
 *   Copyright (C) 2007 John Crispin <blogic@openwrt.org> 
 *
 */

#define IFAP_EEPROM_DRV_VERSION "0.0.1"

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/ptrace.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <asm/bitops.h>

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/version.h>

#include <asm/ifxmips/ifxmips.h>
#include <asm/ifxmips/ifxmips_irq.h>
#include <asm/ifxmips/ifx_ssc_defines.h>
#include <asm/ifxmips/ifx_ssc.h>

/* allow the user to set the major device number */
static int ifxmips_eeprom_maj = 0;

extern int ifx_ssc_init (void);
extern int ifx_ssc_open (struct inode *inode, struct file *filp);
extern int ifx_ssc_close (struct inode *inode, struct file *filp);
extern void ifx_ssc_cleanup_module (void);
extern int ifx_ssc_ioctl (struct inode *inode, struct file *filp,
			  unsigned int cmd, unsigned long data);
extern ssize_t ifx_ssc_kwrite (int port, const char *kbuf, size_t len);
extern ssize_t ifx_ssc_kread (int port, char *kbuf, size_t len);

extern int ifx_ssc_cs_low (unsigned int pin);
extern int ifx_ssc_cs_high (unsigned int pin);
extern int ifx_ssc_txrx (char *tx_buf, unsigned int tx_len, char *rx_buf, unsigned int rx_len);
extern int ifx_ssc_tx (char *tx_buf, unsigned int tx_len);
extern int ifx_ssc_rx (char *rx_buf, unsigned int rx_len);

#define EEPROM_CS IFX_SSC_WHBGPOSTAT_OUT0_POS

/* commands for EEPROM, x25160, x25140 */
#define EEPROM_WREN			((unsigned char)0x06)
#define EEPROM_WRDI			((unsigned char)0x04)
#define EEPROM_RDSR			((unsigned char)0x05)
#define EEPROM_WRSR			((unsigned char)0x01)
#define EEPROM_READ			((unsigned char)0x03)
#define EEPROM_WRITE			((unsigned char)0x02)
#define EEPROM_PAGE_SIZE		4
#define EEPROM_SIZE			512

static int
eeprom_rdsr (void)
{
	int ret = 0;
	unsigned char cmd = EEPROM_RDSR;
	unsigned long flag;
	char status;

	local_irq_save(flag);

	if ((ret = ifx_ssc_cs_low(EEPROM_CS)) == 0)
		if ((ret = ifx_ssc_txrx(&cmd, 1, &status, 1)) >= 0)
			ret = status & 1;

	ifx_ssc_cs_high(EEPROM_CS);
	local_irq_restore(flag);

	return ret;
}

void
eeprom_wip_over (void)
{
	while (eeprom_rdsr())
		printk("waiting for eeprom\n");
}

static int
eeprom_wren (void)
{
	unsigned char cmd = EEPROM_WREN;
	int ret = 0;
	unsigned long flag;

	local_irq_save(flag);
	if ((ret = ifx_ssc_cs_low(EEPROM_CS)) == 0)
		if ((ret = ifx_ssc_tx(&cmd, 1)) >= 0)
			ret = 0;

	ifx_ssc_cs_high(EEPROM_CS);
	local_irq_restore(flag);

	if (!ret)
		eeprom_wip_over();

	return ret;
}

static int
eeprom_wrsr (void)
{
	int ret = 0;
	unsigned char cmd[2];
	unsigned long flag;

	cmd[0] = EEPROM_WRSR;
	cmd[1] = 0;

	if ((ret = eeprom_wren()))
	{
		printk ("eeprom_wren fails\n");
		goto out1;
	}

	local_irq_save(flag);

	if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
		goto out;

	if ((ret = ifx_ssc_tx(cmd, 2)) < 0) {
		ifx_ssc_cs_high(EEPROM_CS);
		goto out;
	}

	if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
		goto out;

	local_irq_restore(flag);
	eeprom_wip_over();

	return ret;

out:
	local_irq_restore (flag);
	eeprom_wip_over ();

out1:
	return ret;
}

static int
eeprom_read (unsigned int addr, unsigned char *buf, unsigned int len)
{
	int ret = 0;
	unsigned char write_buf[2];
	unsigned int eff = 0;
	unsigned long flag;

	while (1)
	{
		eeprom_wip_over();
		eff = EEPROM_PAGE_SIZE - (addr % EEPROM_PAGE_SIZE);
		eff = (eff < len) ? eff : len;
		local_irq_save(flag);

		if ((ret = ifx_ssc_cs_low(EEPROM_CS)) < 0)
			goto out;

		write_buf[0] = EEPROM_READ | ((unsigned char) ((addr & 0x100) >> 5));
		write_buf[1] = (addr & 0xff);

		if ((ret = ifx_ssc_txrx (write_buf, 2, buf, eff)) != eff)
		{
			printk("ssc_txrx fails %d\n", ret);
			ifx_ssc_cs_high (EEPROM_CS);
			goto out;
		}

		buf += ret;
		len -= ret;
		addr += ret;

		if ((ret = ifx_ssc_cs_high(EEPROM_CS)))
			goto out;

		local_irq_restore(flag);

		if (len <= 0)
			goto out2;
	}

out:
	local_irq_restore (flag);
out2:
	return ret;
}

static int
eeprom_write (unsigned int addr, unsigned char *buf, unsigned int len)
{
	int ret = 0;
	unsigned int eff = 0;
	unsigned char write_buf[2];
	int i;
	unsigned char rx_buf[EEPROM_PAGE_SIZE];

	while (1)
	{
		eeprom_wip_over();

		if ((ret = eeprom_wren()))
		{
			printk("eeprom_wren fails\n");
			goto out;
		}

		write_buf[0] = EEPROM_WRITE | ((unsigned char) ((addr & 0x100) >> 5));
		write_buf[1] = (addr & 0xff);

		eff = EEPROM_PAGE_SIZE - (addr % EEPROM_PAGE_SIZE);
		eff = (eff < len) ? eff : len;

		printk("EEPROM Write:\n");
		for (i = 0; i < eff; i++) {
			printk("%2x ", buf[i]);
			if ((i % 16) == 15)
				printk("\n");
		}
		printk("\n");

		if ((ret = ifx_ssc_cs_low(EEPROM_CS)))
			goto out;

		if ((ret = ifx_ssc_tx (write_buf, 2)) < 0)
		{
			printk("ssc_tx fails %d\n", ret);
			ifx_ssc_cs_high(EEPROM_CS);
			goto out;
		}

		if ((ret = ifx_ssc_tx (buf, eff)) != eff)
		{
			printk("ssc_tx fails %d\n", ret);
			ifx_ssc_cs_high(EEPROM_CS);
			goto out;
		}

		buf += ret;
		len -= ret;
		addr += ret;

		if ((ret = ifx_ssc_cs_high (EEPROM_CS)))
			goto out;

		printk ("<==");
		eeprom_read((addr - eff), rx_buf, eff);
		for (i = 0; i < eff; i++)
		{
			printk ("[%x]", rx_buf[i]);
		}
		printk ("\n");

		if (len <= 0)
			break;
	}

out:
	return ret;
}

int
ifxmips_eeprom_open (struct inode *inode, struct file *filp)
{
	filp->f_pos = 0;
	return 0;
}

int
ifxmips_eeprom_close (struct inode *inode, struct file *filp)
{
	return 0;
}

int
ifxmips_eeprom_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long data)
{
	return 0;
}

ssize_t
ifxmips_eeprom_read (char *buf, size_t len, unsigned int addr)
{
	int ret = 0;
	unsigned int data;

	printk("addr:=%d\n", addr);
	printk("len:=%d\n", len);

	if ((addr + len) > EEPROM_SIZE)
	{
		printk("invalid len\n");
		addr = 0;
		len = EEPROM_SIZE / 2;
	}

	if ((ret = ifx_ssc_open((struct inode *) 0, NULL)))
	{
		printk("ifxmips_eeprom_open fails\n");
		goto out;
	}

	data = (unsigned int)IFX_SSC_MODE_RXTX;

	if ((ret = ifx_ssc_ioctl((struct inode *) 0, NULL, IFX_SSC_RXTX_MODE_SET, (unsigned long) &data)))
	{
		printk("set RXTX mode fails\n");
		goto out;
	}

	if ((ret = eeprom_wrsr()))
	{
		printk("EEPROM reset fails\n");
		goto out;
	}

	if ((ret = eeprom_read(addr, buf, len)))
	{
		printk("eeprom read fails\n");
		goto out;
	}

out:
	if (ifx_ssc_close((struct inode *) 0, NULL))
		printk("ifxmips_eeprom_close fails\n");

	return len;
}
EXPORT_SYMBOL(ifxmips_eeprom_read);

static ssize_t
ifxmips_eeprom_fops_read (struct file *filp, char *ubuf, size_t len, loff_t * off)
{
	int ret = 0;
	unsigned char ssc_rx_buf[EEPROM_SIZE];
	long flag;

	if (*off >= EEPROM_SIZE)
		return 0;

	if (*off + len > EEPROM_SIZE)
		len = EEPROM_SIZE - *off;

	if (len == 0)
		return 0;

	local_irq_save(flag);

	if ((ret = ifxmips_eeprom_read(ssc_rx_buf, len, *off)) < 0)
	{
		printk("read fails, err=%x\n", ret);
		local_irq_restore(flag);
		return ret;
	}

	if (copy_to_user((void*)ubuf, ssc_rx_buf, ret) != 0)
	{
		local_irq_restore(flag);
		ret = -EFAULT;
	}

	local_irq_restore(flag);
	*off += len;

	return len;
}

ssize_t
ifxmips_eeprom_write (char *buf, size_t len, unsigned int addr)
{
	int ret = 0;
	unsigned int data;

	if ((ret = ifx_ssc_open ((struct inode *) 0, NULL)))
	{
		printk ("ifxmips_eeprom_open fails\n");
		goto out;
	}

	data = (unsigned int) IFX_SSC_MODE_RXTX;

	if ((ret = ifx_ssc_ioctl ((struct inode *) 0, NULL, IFX_SSC_RXTX_MODE_SET, (unsigned long) &data)))
	{
		printk ("set RXTX mode fails\n");
		goto out;
	}

	if ((ret = eeprom_wrsr ())) {
		printk ("EEPROM reset fails\n");
		goto out;
	}

	if ((ret = eeprom_write (addr, buf, len))) {
		printk ("eeprom write fails\n");
		goto out;
	}

out:
	if (ifx_ssc_close ((struct inode *) 0, NULL))
		printk ("ifxmips_eeprom_close fails\n");

	return ret;
}
EXPORT_SYMBOL(ifxmips_eeprom_write);

static ssize_t
ifxmips_eeprom_fops_write (struct file *filp, const char *ubuf, size_t len, loff_t * off)
{
	int ret = 0;
	unsigned char ssc_tx_buf[EEPROM_SIZE];

	if (*off >= EEPROM_SIZE)
		return 0;

	if (len + *off > EEPROM_SIZE)
		len = EEPROM_SIZE - *off;

	if ((ret = copy_from_user (ssc_tx_buf, ubuf, len)))
		return EFAULT;

	ret = ifxmips_eeprom_write (ssc_tx_buf, len, *off);

	if (ret > 0)
		*off = ret;

	return ret;
}

loff_t
ifxmips_eeprom_llseek (struct file * filp, loff_t off, int whence)
{
	loff_t newpos;
	switch (whence) {
	case SEEK_SET:
		newpos = off;
		break;

	case SEEK_CUR:
		newpos = filp->f_pos + off;
		break;

	default:
		return -EINVAL;
	}

	if (newpos < 0)
		return -EINVAL;

	filp->f_pos = newpos;

	return newpos;
}

static struct file_operations ifxmips_eeprom_fops = {
      owner:THIS_MODULE,
      llseek:ifxmips_eeprom_llseek,
      read:ifxmips_eeprom_fops_read,
      write:ifxmips_eeprom_fops_write,
      ioctl:ifxmips_eeprom_ioctl,
      open:ifxmips_eeprom_open,
      release:ifxmips_eeprom_close,
};

int __init
ifxmips_eeprom_init (void)
{
	int ret = 0;

	ifxmips_eeprom_maj = register_chrdev(0, "eeprom", &ifxmips_eeprom_fops);

	if (ifxmips_eeprom_maj < 0)
	{
		printk("failed to register eeprom device\n");
		ret = -EINVAL;
		
		goto out;
	}

	printk("ifxmips_eeprom : /dev/eeprom mayor %d\n", ifxmips_eeprom_maj);

out:
	return ret;
}

void __exit
ifxmips_eeprom_cleanup_module (void)
{
	/*if (unregister_chrdev (ifxmips_eeprom_maj, "eeprom")) {
		printk ("Unable to unregister major %d for the EEPROM\n",
			maj);
	}*/
}

module_exit (ifxmips_eeprom_cleanup_module);
module_init (ifxmips_eeprom_init);

MODULE_LICENSE ("GPL");
MODULE_AUTHOR ("Peng Liu");
MODULE_DESCRIPTION ("IFAP EEPROM driver");
MODULE_SUPPORTED_DEVICE ("ifxmips_eeprom");