blob: 2bec9ccc02938d25ae9b366ad4c05e1889fe52c5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/sbus/char/jsflash.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds (drivers/char/mem.c)
5 * Copyright (C) 1997 Eddie C. Dost (drivers/sbus/char/flash.c)
6 * Copyright (C) 1997-2000 Pavel Machek <pavel@ucw.cz> (drivers/block/nbd.c)
7 * Copyright (C) 1999-2000 Pete Zaitcev
8 *
9 * This driver is used to program OS into a Flash SIMM on
10 * Krups and Espresso platforms.
11 *
12 * TODO: do not allow erase/programming if file systems are mounted.
13 * TODO: Erase/program both banks of a 8MB SIMM.
14 *
15 * It is anticipated that programming an OS Flash will be a routine
16 * procedure. In the same time it is exeedingly dangerous because
17 * a user can program its OBP flash with OS image and effectively
18 * kill the machine.
19 *
20 * This driver uses an interface different from Eddie's flash.c
21 * as a silly safeguard.
22 *
23 * XXX The flash.c manipulates page caching characteristics in a certain
24 * dubious way; also it assumes that remap_pfn_range() can remap
25 * PCI bus locations, which may be false. ioremap() must be used
26 * instead. We should discuss this.
27 */
28
29#include <linux/module.h>
Arnd Bergmann28fbbf42008-05-20 19:16:08 +020030#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/types.h>
32#include <linux/errno.h>
33#include <linux/miscdevice.h>
34#include <linux/slab.h>
35#include <linux/fcntl.h>
36#include <linux/poll.h>
37#include <linux/init.h>
38#include <linux/string.h>
39#include <linux/smp_lock.h>
40#include <linux/genhd.h>
41#include <linux/blkdev.h>
42
43#define MAJOR_NR JSFD_MAJOR
44
45#include <asm/uaccess.h>
46#include <asm/pgtable.h>
47#include <asm/io.h>
48#include <asm/pcic.h>
49#include <asm/oplib.h>
50
51#include <asm/jsflash.h> /* ioctl arguments. <linux/> ?? */
52#define JSFIDSZ (sizeof(struct jsflash_ident_arg))
53#define JSFPRGSZ (sizeof(struct jsflash_program_arg))
54
55/*
56 * Our device numbers have no business in system headers.
57 * The only thing a user knows is the device name /dev/jsflash.
58 *
59 * Block devices are laid out like this:
60 * minor+0 - Bootstrap, for 8MB SIMM 0x20400000[0x800000]
61 * minor+1 - Filesystem to mount, normally 0x20400400[0x7ffc00]
62 * minor+2 - Whole flash area for any case... 0x20000000[0x01000000]
63 * Total 3 minors per flash device.
64 *
65 * It is easier to have static size vectors, so we define
66 * a total minor range JSF_MAX, which must cover all minors.
67 */
68/* character device */
69#define JSF_MINOR 178 /* 178 is registered with hpa */
70/* block device */
71#define JSF_MAX 3 /* 3 minors wasted total so far. */
72#define JSF_NPART 3 /* 3 minors per flash device */
73#define JSF_PART_BITS 2 /* 2 bits of minors to cover JSF_NPART */
74#define JSF_PART_MASK 0x3 /* 2 bits mask */
75
76/*
77 * Access functions.
78 * We could ioremap(), but it's easier this way.
79 */
80static unsigned int jsf_inl(unsigned long addr)
81{
82 unsigned long retval;
83
84 __asm__ __volatile__("lda [%1] %2, %0\n\t" :
85 "=r" (retval) :
86 "r" (addr), "i" (ASI_M_BYPASS));
87 return retval;
88}
89
90static void jsf_outl(unsigned long addr, __u32 data)
91{
92
93 __asm__ __volatile__("sta %0, [%1] %2\n\t" : :
94 "r" (data), "r" (addr), "i" (ASI_M_BYPASS) :
95 "memory");
96}
97
98/*
99 * soft carrier
100 */
101
102struct jsfd_part {
103 unsigned long dbase;
104 unsigned long dsize;
105};
106
107struct jsflash {
108 unsigned long base;
109 unsigned long size;
110 unsigned long busy; /* In use? */
111 struct jsflash_ident_arg id;
112 /* int mbase; */ /* Minor base, typically zero */
113 struct jsfd_part dv[JSF_NPART];
114};
115
116/*
117 * We do not map normal memory or obio as a safety precaution.
118 * But offsets are real, for ease of userland programming.
119 */
120#define JSF_BASE_TOP 0x30000000
121#define JSF_BASE_ALL 0x20000000
122
123#define JSF_BASE_JK 0x20400000
124
125/*
126 */
127static struct gendisk *jsfd_disk[JSF_MAX];
128
129/*
130 * Let's pretend we may have several of these...
131 */
132static struct jsflash jsf0;
133
134/*
135 * Wait for AMD to finish its embedded algorithm.
136 * We use the Toggle bit DQ6 (0x40) because it does not
137 * depend on the data value as /DATA bit DQ7 does.
138 *
139 * XXX Do we need any timeout here? So far it never hanged, beware broken hw.
140 */
141static void jsf_wait(unsigned long p) {
142 unsigned int x1, x2;
143
144 for (;;) {
145 x1 = jsf_inl(p);
146 x2 = jsf_inl(p);
147 if ((x1 & 0x40404040) == (x2 & 0x40404040)) return;
148 }
149}
150
151/*
152 * Programming will only work if Flash is clean,
153 * we leave it to the programmer application.
154 *
155 * AMD must be programmed one byte at a time;
156 * thus, Simple Tech SIMM must be written 4 bytes at a time.
157 *
158 * Write waits for the chip to become ready after the write
159 * was finished. This is done so that application would read
160 * consistent data after the write is done.
161 */
162static void jsf_write4(unsigned long fa, u32 data) {
163
164 jsf_outl(fa, 0xAAAAAAAA); /* Unlock 1 Write 1 */
165 jsf_outl(fa, 0x55555555); /* Unlock 1 Write 2 */
166 jsf_outl(fa, 0xA0A0A0A0); /* Byte Program */
167 jsf_outl(fa, data);
168
169 jsf_wait(fa);
170}
171
172/*
173 */
174static void jsfd_read(char *buf, unsigned long p, size_t togo) {
175 union byte4 {
176 char s[4];
177 unsigned int n;
178 } b;
179
180 while (togo >= 4) {
181 togo -= 4;
182 b.n = jsf_inl(p);
183 memcpy(buf, b.s, 4);
184 p += 4;
185 buf += 4;
186 }
187}
188
Jens Axboe165125e2007-07-24 09:28:11 +0200189static void jsfd_do_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct request *req;
192
193 while ((req = elv_next_request(q)) != NULL) {
194 struct jsfd_part *jdp = req->rq_disk->private_data;
195 unsigned long offset = req->sector << 9;
196 size_t len = req->current_nr_sectors << 9;
197
198 if ((offset + len) > jdp->dsize) {
199 end_request(req, 0);
200 continue;
201 }
202
203 if (rq_data_dir(req) != READ) {
204 printk(KERN_ERR "jsfd: write\n");
205 end_request(req, 0);
206 continue;
207 }
208
209 if ((jdp->dbase & 0xff000000) != 0x20000000) {
210 printk(KERN_ERR "jsfd: bad base %x\n", (int)jdp->dbase);
211 end_request(req, 0);
212 continue;
213 }
214
215 jsfd_read(req->buffer, jdp->dbase + offset, len);
216
217 end_request(req, 1);
218 }
219}
220
221/*
222 * The memory devices use the full 32/64 bits of the offset, and so we cannot
223 * check against negative addresses: they are ok. The return value is weird,
224 * though, in that case (0).
225 *
226 * also note that seeking relative to the "end of file" isn't supported:
227 * it has no meaning, so it returns -EINVAL.
228 */
229static loff_t jsf_lseek(struct file * file, loff_t offset, int orig)
230{
231 loff_t ret;
232
233 lock_kernel();
234 switch (orig) {
235 case 0:
236 file->f_pos = offset;
237 ret = file->f_pos;
238 break;
239 case 1:
240 file->f_pos += offset;
241 ret = file->f_pos;
242 break;
243 default:
244 ret = -EINVAL;
245 }
246 unlock_kernel();
247 return ret;
248}
249
250/*
251 * OS SIMM Cannot be read in other size but a 32bits word.
252 */
Al Virobc05d832005-12-06 05:51:43 -0500253static ssize_t jsf_read(struct file * file, char __user * buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 size_t togo, loff_t *ppos)
255{
256 unsigned long p = *ppos;
Al Virobc05d832005-12-06 05:51:43 -0500257 char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 union byte4 {
260 char s[4];
261 unsigned int n;
262 } b;
263
264 if (p < JSF_BASE_ALL || p >= JSF_BASE_TOP) {
265 return 0;
266 }
267
268 if ((p + togo) < p /* wrap */
269 || (p + togo) >= JSF_BASE_TOP) {
270 togo = JSF_BASE_TOP - p;
271 }
272
273 if (p < JSF_BASE_ALL && togo != 0) {
274#if 0 /* __bzero XXX */
275 size_t x = JSF_BASE_ALL - p;
276 if (x > togo) x = togo;
277 clear_user(tmp, x);
278 tmp += x;
279 p += x;
280 togo -= x;
281#else
282 /*
283 * Implementation of clear_user() calls __bzero
284 * without regard to modversions,
285 * so we cannot build a module.
286 */
287 return 0;
288#endif
289 }
290
291 while (togo >= 4) {
292 togo -= 4;
293 b.n = jsf_inl(p);
294 if (copy_to_user(tmp, b.s, 4))
295 return -EFAULT;
296 tmp += 4;
297 p += 4;
298 }
299
300 /*
301 * XXX Small togo may remain if 1 byte is ordered.
302 * It would be nice if we did a word size read and unpacked it.
303 */
304
305 *ppos = p;
306 return tmp-buf;
307}
308
Al Virobc05d832005-12-06 05:51:43 -0500309static ssize_t jsf_write(struct file * file, const char __user * buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 size_t count, loff_t *ppos)
311{
312 return -ENOSPC;
313}
314
315/*
316 */
317static int jsf_ioctl_erase(unsigned long arg)
318{
319 unsigned long p;
320
321 /* p = jsf0.base; hits wrong bank */
322 p = 0x20400000;
323
324 jsf_outl(p, 0xAAAAAAAA); /* Unlock 1 Write 1 */
325 jsf_outl(p, 0x55555555); /* Unlock 1 Write 2 */
326 jsf_outl(p, 0x80808080); /* Erase setup */
327 jsf_outl(p, 0xAAAAAAAA); /* Unlock 2 Write 1 */
328 jsf_outl(p, 0x55555555); /* Unlock 2 Write 2 */
329 jsf_outl(p, 0x10101010); /* Chip erase */
330
331#if 0
332 /*
333 * This code is ok, except that counter based timeout
334 * has no place in this world. Let's just drop timeouts...
335 */
336 {
337 int i;
338 __u32 x;
339 for (i = 0; i < 1000000; i++) {
340 x = jsf_inl(p);
341 if ((x & 0x80808080) == 0x80808080) break;
342 }
343 if ((x & 0x80808080) != 0x80808080) {
344 printk("jsf0: erase timeout with 0x%08x\n", x);
345 } else {
346 printk("jsf0: erase done with 0x%08x\n", x);
347 }
348 }
349#else
350 jsf_wait(p);
351#endif
352
353 return 0;
354}
355
356/*
357 * Program a block of flash.
358 * Very simple because we can do it byte by byte anyway.
359 */
Al Virobc05d832005-12-06 05:51:43 -0500360static int jsf_ioctl_program(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct jsflash_program_arg abuf;
Al Virobc05d832005-12-06 05:51:43 -0500363 char __user *uptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 unsigned long p;
365 unsigned int togo;
366 union {
367 unsigned int n;
368 char s[4];
369 } b;
370
Al Virobc05d832005-12-06 05:51:43 -0500371 if (copy_from_user(&abuf, arg, JSFPRGSZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return -EFAULT;
373 p = abuf.off;
374 togo = abuf.size;
375 if ((togo & 3) || (p & 3)) return -EINVAL;
376
Al Virobc05d832005-12-06 05:51:43 -0500377 uptr = (char __user *) (unsigned long) abuf.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 while (togo != 0) {
379 togo -= 4;
380 if (copy_from_user(&b.s[0], uptr, 4))
381 return -EFAULT;
382 jsf_write4(p, b.n);
383 p += 4;
384 uptr += 4;
385 }
386
387 return 0;
388}
389
390static int jsf_ioctl(struct inode *inode, struct file *f, unsigned int cmd,
391 unsigned long arg)
392{
393 int error = -ENOTTY;
Al Virobc05d832005-12-06 05:51:43 -0500394 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (!capable(CAP_SYS_ADMIN))
397 return -EPERM;
398 switch (cmd) {
399 case JSFLASH_IDENT:
Al Virobc05d832005-12-06 05:51:43 -0500400 if (copy_to_user(argp, &jsf0.id, JSFIDSZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return -EFAULT;
402 break;
403 case JSFLASH_ERASE:
404 error = jsf_ioctl_erase(arg);
405 break;
406 case JSFLASH_PROGRAM:
Al Virobc05d832005-12-06 05:51:43 -0500407 error = jsf_ioctl_program(argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 break;
409 }
410
411 return error;
412}
413
414static int jsf_mmap(struct file * file, struct vm_area_struct * vma)
415{
416 return -ENXIO;
417}
418
419static int jsf_open(struct inode * inode, struct file * filp)
420{
Arnd Bergmann28fbbf42008-05-20 19:16:08 +0200421 lock_kernel();
422 if (jsf0.base == 0) {
423 unlock_kernel();
424 return -ENXIO;
425 }
426 if (test_and_set_bit(0, (void *)&jsf0.busy) != 0) {
427 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -EBUSY;
Arnd Bergmann28fbbf42008-05-20 19:16:08 +0200429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Arnd Bergmann28fbbf42008-05-20 19:16:08 +0200431 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return 0; /* XXX What security? */
433}
434
435static int jsf_release(struct inode *inode, struct file *file)
436{
437 jsf0.busy = 0;
438 return 0;
439}
440
Arjan van de Ven00977a52007-02-12 00:55:34 -0800441static const struct file_operations jsf_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 .owner = THIS_MODULE,
443 .llseek = jsf_lseek,
444 .read = jsf_read,
445 .write = jsf_write,
446 .ioctl = jsf_ioctl,
447 .mmap = jsf_mmap,
448 .open = jsf_open,
449 .release = jsf_release,
450};
451
452static struct miscdevice jsf_dev = { JSF_MINOR, "jsflash", &jsf_fops };
453
454static struct block_device_operations jsfd_fops = {
455 .owner = THIS_MODULE,
456};
457
458static int jsflash_init(void)
459{
460 int rc;
461 struct jsflash *jsf;
462 int node;
463 char banner[128];
464 struct linux_prom_registers reg0;
465
466 node = prom_getchild(prom_root_node);
467 node = prom_searchsiblings(node, "flash-memory");
468 if (node != 0 && node != -1) {
469 if (prom_getproperty(node, "reg",
470 (char *)&reg0, sizeof(reg0)) == -1) {
471 printk("jsflash: no \"reg\" property\n");
472 return -ENXIO;
473 }
474 if (reg0.which_io != 0) {
475 printk("jsflash: bus number nonzero: 0x%x:%x\n",
476 reg0.which_io, reg0.phys_addr);
477 return -ENXIO;
478 }
479 /*
480 * Flash may be somewhere else, for instance on Ebus.
481 * So, don't do the following check for IIep flash space.
482 */
483#if 0
484 if ((reg0.phys_addr >> 24) != 0x20) {
485 printk("jsflash: suspicious address: 0x%x:%x\n",
486 reg0.which_io, reg0.phys_addr);
487 return -ENXIO;
488 }
489#endif
490 if ((int)reg0.reg_size <= 0) {
491 printk("jsflash: bad size 0x%x\n", (int)reg0.reg_size);
492 return -ENXIO;
493 }
494 } else {
495 /* XXX Remove this code once PROLL ID12 got widespread */
496 printk("jsflash: no /flash-memory node, use PROLL >= 12\n");
497 prom_getproperty(prom_root_node, "banner-name", banner, 128);
498 if (strcmp (banner, "JavaStation-NC") != 0 &&
499 strcmp (banner, "JavaStation-E") != 0) {
500 return -ENXIO;
501 }
502 reg0.which_io = 0;
503 reg0.phys_addr = 0x20400000;
504 reg0.reg_size = 0x00800000;
505 }
506
507 /* Let us be really paranoid for modifications to probing code. */
508 /* extern enum sparc_cpu sparc_cpu_model; */ /* in <asm/system.h> */
509 if (sparc_cpu_model != sun4m) {
510 /* We must be on sun4m because we use MMU Bypass ASI. */
511 return -ENXIO;
512 }
513
514 if (jsf0.base == 0) {
515 jsf = &jsf0;
516
517 jsf->base = reg0.phys_addr;
518 jsf->size = reg0.reg_size;
519
520 /* XXX Redo the userland interface. */
521 jsf->id.off = JSF_BASE_ALL;
522 jsf->id.size = 0x01000000; /* 16M - all segments */
523 strcpy(jsf->id.name, "Krups_all");
524
525 jsf->dv[0].dbase = jsf->base;
526 jsf->dv[0].dsize = jsf->size;
527 jsf->dv[1].dbase = jsf->base + 1024;
528 jsf->dv[1].dsize = jsf->size - 1024;
529 jsf->dv[2].dbase = JSF_BASE_ALL;
530 jsf->dv[2].dsize = 0x01000000;
531
532 printk("Espresso Flash @0x%lx [%d MB]\n", jsf->base,
533 (int) (jsf->size / (1024*1024)));
534 }
535
536 if ((rc = misc_register(&jsf_dev)) != 0) {
537 printk(KERN_ERR "jsf: unable to get misc minor %d\n",
538 JSF_MINOR);
539 jsf0.base = 0;
540 return rc;
541 }
542
543 return 0;
544}
545
546static struct request_queue *jsf_queue;
547
548static int jsfd_init(void)
549{
550 static DEFINE_SPINLOCK(lock);
551 struct jsflash *jsf;
552 struct jsfd_part *jdp;
553 int err;
554 int i;
555
556 if (jsf0.base == 0)
557 return -ENXIO;
558
559 err = -ENOMEM;
560 for (i = 0; i < JSF_MAX; i++) {
561 struct gendisk *disk = alloc_disk(1);
562 if (!disk)
563 goto out;
564 jsfd_disk[i] = disk;
565 }
566
567 if (register_blkdev(JSFD_MAJOR, "jsfd")) {
568 err = -EIO;
569 goto out;
570 }
571
572 jsf_queue = blk_init_queue(jsfd_do_request, &lock);
573 if (!jsf_queue) {
574 err = -ENOMEM;
575 unregister_blkdev(JSFD_MAJOR, "jsfd");
576 goto out;
577 }
578
579 for (i = 0; i < JSF_MAX; i++) {
580 struct gendisk *disk = jsfd_disk[i];
581 if ((i & JSF_PART_MASK) >= JSF_NPART) continue;
582 jsf = &jsf0; /* actually, &jsfv[i >> JSF_PART_BITS] */
583 jdp = &jsf->dv[i&JSF_PART_MASK];
584
585 disk->major = JSFD_MAJOR;
586 disk->first_minor = i;
587 sprintf(disk->disk_name, "jsfd%d", i);
588 disk->fops = &jsfd_fops;
589 set_capacity(disk, jdp->dsize >> 9);
590 disk->private_data = jdp;
591 disk->queue = jsf_queue;
592 add_disk(disk);
593 set_disk_ro(disk, 1);
594 }
595 return 0;
596out:
597 while (i--)
598 put_disk(jsfd_disk[i]);
599 return err;
600}
601
602MODULE_LICENSE("GPL");
603
604static int __init jsflash_init_module(void) {
605 int rc;
606
607 if ((rc = jsflash_init()) == 0) {
608 jsfd_init();
609 return 0;
610 }
611 return rc;
612}
613
614static void __exit jsflash_cleanup_module(void)
615{
616 int i;
617
618 for (i = 0; i < JSF_MAX; i++) {
619 if ((i & JSF_PART_MASK) >= JSF_NPART) continue;
620 del_gendisk(jsfd_disk[i]);
621 put_disk(jsfd_disk[i]);
622 }
623 if (jsf0.busy)
624 printk("jsf0: cleaning busy unit\n");
625 jsf0.base = 0;
626 jsf0.busy = 0;
627
628 misc_deregister(&jsf_dev);
Akinobu Mita00d59402007-07-17 04:03:46 -0700629 unregister_blkdev(JSFD_MAJOR, "jsfd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 blk_cleanup_queue(jsf_queue);
631}
632
633module_init(jsflash_init_module);
634module_exit(jsflash_cleanup_module);