1
2
3#include <linux/module.h>
4#include <linux/types.h>
5#include <linux/errno.h>
6#include <linux/miscdevice.h>
7#include <linux/slab.h>
8#include <linux/fcntl.h>
9#include <linux/poll.h>
10#include <linux/init.h>
11#include <linux/string.h>
12#include <linux/smp_lock.h>
13#include <linux/genhd.h>
14#include <linux/blkdev.h>
15
16#define MAJOR_NR	JSFD_MAJOR
17
18#include <asm/uaccess.h>
19#include <asm/pgtable.h>
20#include <asm/io.h>
21#include <asm/pcic.h>
22#include <asm/oplib.h>
23
24#include <asm/jsflash.h>		/* ioctl arguments. <linux/> ?? */
25#define JSFIDSZ		(sizeof(struct jsflash_ident_arg))
26#define JSFPRGSZ	(sizeof(struct jsflash_program_arg))
27
28/*
29 * Our device numbers have no business in system headers.
30 * The only thing a user knows is the device name /dev/jsflash.
31 *
32 * Block devices are laid out like this:
33 *   minor+0	- Bootstrap, for 8MB SIMM 0x20400000[0x800000]
34 *   minor+1	- Filesystem to mount, normally 0x20400400[0x7ffc00]
35 *   minor+2	- Whole flash area for any case... 0x20000000[0x01000000]
36 * Total 3 minors per flash device.
37 *
38 * It is easier to have static size vectors, so we define
39 * a total minor range JSF_MAX, which must cover all minors.
40 */
41/* character device */
42#define JSF_MINOR	178	/* 178 is registered with hpa */
43/* block device */
44#define JSF_MAX		 3	/* 3 minors wasted total so far. */
45#define JSF_NPART	 3	/* 3 minors per flash device */
46#define JSF_PART_BITS	 2	/* 2 bits of minors to cover JSF_NPART */
47#define JSF_PART_MASK	 0x3	/* 2 bits mask */
48
49/*
50 * Access functions.
51 * We could ioremap(), but it's easier this way.
52 */
53static unsigned int jsf_inl(unsigned long addr)
54{
55	unsigned long retval;
56
57	__asm__ __volatile__("lda [%1] %2, %0\n\t" :
58				"=r" (retval) :
59				"r" (addr), "i" (ASI_M_BYPASS));
60        return retval;
61}
62
63static void jsf_outl(unsigned long addr, __u32 data)
64{
65
66	__asm__ __volatile__("sta %0, [%1] %2\n\t" : :
67				"r" (data), "r" (addr), "i" (ASI_M_BYPASS) :
68				"memory");
69}
70
71/*
72 * soft carrier
73 */
74
75struct jsfd_part {
76	unsigned long dbase;
77	unsigned long dsize;
78};
79
80struct jsflash {
81	unsigned long base;
82	unsigned long size;
83	unsigned long busy;		/* In use? */
84	struct jsflash_ident_arg id;
85	/* int mbase; */		/* Minor base, typically zero */
86	struct jsfd_part dv[JSF_NPART];
87};
88
89/*
90 * We do not map normal memory or obio as a safety precaution.
91 * But offsets are real, for ease of userland programming.
92 */
93#define JSF_BASE_TOP	0x30000000
94#define JSF_BASE_ALL	0x20000000
95
96#define JSF_BASE_JK	0x20400000
97
98/*
99 */
100static struct gendisk *jsfd_disk[JSF_MAX];
101
102/*
103 * Let's pretend we may have several of these...
104 */
105static struct jsflash jsf0;
106
107static void jsf_wait(unsigned long p) {
108	unsigned int x1, x2;
109
110	for (;;) {
111		x1 = jsf_inl(p);
112		x2 = jsf_inl(p);
113		if ((x1 & 0x40404040) == (x2 & 0x40404040)) return;
114	}
115}
116
117/*
118 * Programming will only work if Flash is clean,
119 * we leave it to the programmer application.
120 *
121 * AMD must be programmed one byte at a time;
122 * thus, Simple Tech SIMM must be written 4 bytes at a time.
123 *
124 * Write waits for the chip to become ready after the write
125 * was finished. This is done so that application would read
126 * consistent data after the write is done.
127 */
128static void jsf_write4(unsigned long fa, u32 data) {
129
130	jsf_outl(fa, 0xAAAAAAAA);		/* Unlock 1 Write 1 */
131	jsf_outl(fa, 0x55555555);		/* Unlock 1 Write 2 */
132	jsf_outl(fa, 0xA0A0A0A0);		/* Byte Program */
133	jsf_outl(fa, data);
134
135	jsf_wait(fa);
136}
137
138/*
139 */
140static void jsfd_read(char *buf, unsigned long p, size_t togo) {
141	union byte4 {
142		char s[4];
143		unsigned int n;
144	} b;
145
146	while (togo >= 4) {
147		togo -= 4;
148		b.n = jsf_inl(p);
149		memcpy(buf, b.s, 4);
150		p += 4;
151		buf += 4;
152	}
153}
154
155static void jsfd_do_request(request_queue_t *q)
156{
157	struct request *req;
158
159	while ((req = elv_next_request(q)) != NULL) {
160		struct jsfd_part *jdp = req->rq_disk->private_data;
161		unsigned long offset = req->sector << 9;
162		size_t len = req->current_nr_sectors << 9;
163
164		if ((offset + len) > jdp->dsize) {
165               		end_request(req, 0);
166			continue;
167		}
168
169		if (rq_data_dir(req) != READ) {
170			printk(KERN_ERR "jsfd: write\n");
171			end_request(req, 0);
172			continue;
173		}
174
175		if ((jdp->dbase & 0xff000000) != 0x20000000) {
176			printk(KERN_ERR "jsfd: bad base %x\n", (int)jdp->dbase);
177			end_request(req, 0);
178			continue;
179		}
180
181		jsfd_read(req->buffer, jdp->dbase + offset, len);
182
183		end_request(req, 1);
184	}
185}
186
187/*
188 * The memory devices use the full 32/64 bits of the offset, and so we cannot
189 * check against negative addresses: they are ok. The return value is weird,
190 * though, in that case (0).
191 *
192 * also note that seeking relative to the "end of file" isn't supported:
193 * it has no meaning, so it returns -EINVAL.
194 */
195static loff_t jsf_lseek(struct file * file, loff_t offset, int orig)
196{
197	loff_t ret;
198
199	lock_kernel();
200	switch (orig) {
201		case 0:
202			file->f_pos = offset;
203			ret = file->f_pos;
204			break;
205		case 1:
206			file->f_pos += offset;
207			ret = file->f_pos;
208			break;
209		default:
210			ret = -EINVAL;
211	}
212	unlock_kernel();
213	return ret;
214}
215
216/*
217 * OS SIMM Cannot be read in other size but a 32bits word.
218 */
219static ssize_t jsf_read(struct file * file, char __user * buf,
220    size_t togo, loff_t *ppos)
221{
222	unsigned long p = *ppos;
223	char __user *tmp = buf;
224
225	union byte4 {
226		char s[4];
227		unsigned int n;
228	} b;
229
230	if (p < JSF_BASE_ALL || p >= JSF_BASE_TOP) {
231		return 0;
232	}
233
234	if ((p + togo) < p	/* wrap */
235	   || (p + togo) >= JSF_BASE_TOP) {
236		togo = JSF_BASE_TOP - p;
237	}
238
239	if (p < JSF_BASE_ALL && togo != 0) {
240		/*
241		 * Implementation of clear_user() calls __bzero
242		 * without regard to modversions,
243		 * so we cannot build a module.
244		 */
245		return 0;
246	}
247
248	while (togo >= 4) {
249		togo -= 4;
250		b.n = jsf_inl(p);
251		if (copy_to_user(tmp, b.s, 4))
252			return -EFAULT;
253		tmp += 4;
254		p += 4;
255	}
256
257
258	*ppos = p;
259	return tmp-buf;
260}
261
262static ssize_t jsf_write(struct file * file, const char __user * buf,
263    size_t count, loff_t *ppos)
264{
265	return -ENOSPC;
266}
267
268/*
269 */
270static int jsf_ioctl_erase(unsigned long arg)
271{
272	unsigned long p;
273
274	/* p = jsf0.base;	hits wrong bank */
275	p = 0x20400000;
276
277	jsf_outl(p, 0xAAAAAAAA);		/* Unlock 1 Write 1 */
278	jsf_outl(p, 0x55555555);		/* Unlock 1 Write 2 */
279	jsf_outl(p, 0x80808080);		/* Erase setup */
280	jsf_outl(p, 0xAAAAAAAA);		/* Unlock 2 Write 1 */
281	jsf_outl(p, 0x55555555);		/* Unlock 2 Write 2 */
282	jsf_outl(p, 0x10101010);		/* Chip erase */
283
284	jsf_wait(p);
285
286	return 0;
287}
288
289/*
290 * Program a block of flash.
291 * Very simple because we can do it byte by byte anyway.
292 */
293static int jsf_ioctl_program(void __user *arg)
294{
295	struct jsflash_program_arg abuf;
296	char __user *uptr;
297	unsigned long p;
298	unsigned int togo;
299	union {
300		unsigned int n;
301		char s[4];
302	} b;
303
304	if (copy_from_user(&abuf, arg, JSFPRGSZ))
305		return -EFAULT;
306	p = abuf.off;
307	togo = abuf.size;
308	if ((togo & 3) || (p & 3)) return -EINVAL;
309
310	uptr = (char __user *) (unsigned long) abuf.data;
311	while (togo != 0) {
312		togo -= 4;
313		if (copy_from_user(&b.s[0], uptr, 4))
314			return -EFAULT;
315		jsf_write4(p, b.n);
316		p += 4;
317		uptr += 4;
318	}
319
320	return 0;
321}
322
323static int jsf_ioctl(struct inode *inode, struct file *f, unsigned int cmd,
324    unsigned long arg)
325{
326	int error = -ENOTTY;
327	void __user *argp = (void __user *)arg;
328
329	if (!capable(CAP_SYS_ADMIN))
330		return -EPERM;
331	switch (cmd) {
332	case JSFLASH_IDENT:
333		if (copy_to_user(argp, &jsf0.id, JSFIDSZ))
334			return -EFAULT;
335		break;
336	case JSFLASH_ERASE:
337		error = jsf_ioctl_erase(arg);
338		break;
339	case JSFLASH_PROGRAM:
340		error = jsf_ioctl_program(argp);
341		break;
342	}
343
344	return error;
345}
346
347static int jsf_mmap(struct file * file, struct vm_area_struct * vma)
348{
349	return -ENXIO;
350}
351
352static int jsf_open(struct inode * inode, struct file * filp)
353{
354
355	if (jsf0.base == 0) return -ENXIO;
356	if (test_and_set_bit(0, (void *)&jsf0.busy) != 0)
357		return -EBUSY;
358
359	return 0;
360}
361
362static int jsf_release(struct inode *inode, struct file *file)
363{
364	jsf0.busy = 0;
365	return 0;
366}
367
368static const struct file_operations jsf_fops = {
369	.owner =	THIS_MODULE,
370	.llseek =	jsf_lseek,
371	.read =		jsf_read,
372	.write =	jsf_write,
373	.ioctl =	jsf_ioctl,
374	.mmap =		jsf_mmap,
375	.open =		jsf_open,
376	.release =	jsf_release,
377};
378
379static struct miscdevice jsf_dev = { JSF_MINOR, "jsflash", &jsf_fops };
380
381static struct block_device_operations jsfd_fops = {
382	.owner =	THIS_MODULE,
383};
384
385static int jsflash_init(void)
386{
387	int rc;
388	struct jsflash *jsf;
389	int node;
390	char banner[128];
391	struct linux_prom_registers reg0;
392
393	node = prom_getchild(prom_root_node);
394	node = prom_searchsiblings(node, "flash-memory");
395	if (node != 0 && node != -1) {
396		if (prom_getproperty(node, "reg",
397		    (char *)&reg0, sizeof(reg0)) == -1) {
398			printk("jsflash: no \"reg\" property\n");
399			return -ENXIO;
400		}
401		if (reg0.which_io != 0) {
402			printk("jsflash: bus number nonzero: 0x%x:%x\n",
403			    reg0.which_io, reg0.phys_addr);
404			return -ENXIO;
405		}
406		/*
407		 * Flash may be somewhere else, for instance on Ebus.
408		 * So, don't do the following check for IIep flash space.
409		 */
410		if ((int)reg0.reg_size <= 0) {
411			printk("jsflash: bad size 0x%x\n", (int)reg0.reg_size);
412			return -ENXIO;
413		}
414	} else {
415		printk("jsflash: no /flash-memory node, use PROLL >= 12\n");
416		prom_getproperty(prom_root_node, "banner-name", banner, 128);
417		if (strcmp (banner, "JavaStation-NC") != 0 &&
418		    strcmp (banner, "JavaStation-E") != 0) {
419			return -ENXIO;
420		}
421		reg0.which_io = 0;
422		reg0.phys_addr = 0x20400000;
423		reg0.reg_size  = 0x00800000;
424	}
425
426	/* Let us be really paranoid for modifications to probing code. */
427	/* extern enum sparc_cpu sparc_cpu_model; */ /* in <asm/system.h> */
428	if (sparc_cpu_model != sun4m) {
429		/* We must be on sun4m because we use MMU Bypass ASI. */
430		return -ENXIO;
431	}
432
433	if (jsf0.base == 0) {
434		jsf = &jsf0;
435
436		jsf->base = reg0.phys_addr;
437		jsf->size = reg0.reg_size;
438
439		jsf->id.off = JSF_BASE_ALL;
440		jsf->id.size = 0x01000000;	/* 16M - all segments */
441		strcpy(jsf->id.name, "Krups_all");
442
443		jsf->dv[0].dbase = jsf->base;
444		jsf->dv[0].dsize = jsf->size;
445		jsf->dv[1].dbase = jsf->base + 1024;
446		jsf->dv[1].dsize = jsf->size - 1024;
447		jsf->dv[2].dbase = JSF_BASE_ALL;
448		jsf->dv[2].dsize = 0x01000000;
449
450		printk("Espresso Flash @0x%lx [%d MB]\n", jsf->base,
451		    (int) (jsf->size / (1024*1024)));
452	}
453
454	if ((rc = misc_register(&jsf_dev)) != 0) {
455		printk(KERN_ERR "jsf: unable to get misc minor %d\n",
456		    JSF_MINOR);
457		jsf0.base = 0;
458		return rc;
459	}
460
461	return 0;
462}
463
464static struct request_queue *jsf_queue;
465
466static int jsfd_init(void)
467{
468	static DEFINE_SPINLOCK(lock);
469	struct jsflash *jsf;
470	struct jsfd_part *jdp;
471	int err;
472	int i;
473
474	if (jsf0.base == 0)
475		return -ENXIO;
476
477	err = -ENOMEM;
478	for (i = 0; i < JSF_MAX; i++) {
479		struct gendisk *disk = alloc_disk(1);
480		if (!disk)
481			goto out;
482		jsfd_disk[i] = disk;
483	}
484
485	if (register_blkdev(JSFD_MAJOR, "jsfd")) {
486		err = -EIO;
487		goto out;
488	}
489
490	jsf_queue = blk_init_queue(jsfd_do_request, &lock);
491	if (!jsf_queue) {
492		err = -ENOMEM;
493		unregister_blkdev(JSFD_MAJOR, "jsfd");
494		goto out;
495	}
496
497	for (i = 0; i < JSF_MAX; i++) {
498		struct gendisk *disk = jsfd_disk[i];
499		if ((i & JSF_PART_MASK) >= JSF_NPART) continue;
500		jsf = &jsf0;	/* actually, &jsfv[i >> JSF_PART_BITS] */
501		jdp = &jsf->dv[i&JSF_PART_MASK];
502
503		disk->major = JSFD_MAJOR;
504		disk->first_minor = i;
505		sprintf(disk->disk_name, "jsfd%d", i);
506		disk->fops = &jsfd_fops;
507		set_capacity(disk, jdp->dsize >> 9);
508		disk->private_data = jdp;
509		disk->queue = jsf_queue;
510		add_disk(disk);
511		set_disk_ro(disk, 1);
512	}
513	return 0;
514out:
515	while (i--)
516		put_disk(jsfd_disk[i]);
517	return err;
518}
519
520MODULE_LICENSE("GPL");
521
522static int __init jsflash_init_module(void) {
523	int rc;
524
525	if ((rc = jsflash_init()) == 0) {
526		jsfd_init();
527		return 0;
528	}
529	return rc;
530}
531
532static void __exit jsflash_cleanup_module(void)
533{
534	int i;
535
536	for (i = 0; i < JSF_MAX; i++) {
537		if ((i & JSF_PART_MASK) >= JSF_NPART) continue;
538		del_gendisk(jsfd_disk[i]);
539		put_disk(jsfd_disk[i]);
540	}
541	if (jsf0.busy)
542		printk("jsf0: cleaning busy unit\n");
543	jsf0.base = 0;
544	jsf0.busy = 0;
545
546	misc_deregister(&jsf_dev);
547	if (unregister_blkdev(JSFD_MAJOR, "jsfd") != 0)
548		printk("jsfd: cleanup_module failed\n");
549	blk_cleanup_queue(jsf_queue);
550}
551
552module_init(jsflash_init_module);
553module_exit(jsflash_cleanup_module);
554