ALSA: hda - Add quirk for Dell Vostro 1220
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / sbus / char / jsflash.c
blob4942050dc5b6f0ea795d62e992368669ab1f6f6a
1 /*
2 * drivers/sbus/char/jsflash.c
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
9 * This driver is used to program OS into a Flash SIMM on
10 * Krups and Espresso platforms.
12 * TODO: do not allow erase/programming if file systems are mounted.
13 * TODO: Erase/program both banks of a 8MB SIMM.
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.
20 * This driver uses an interface different from Eddie's flash.c
21 * as a silly safeguard.
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.
29 #include <linux/module.h>
30 #include <linux/smp_lock.h>
31 #include <linux/types.h>
32 #include <linux/errno.h>
33 #include <linux/miscdevice.h>
34 #include <linux/fcntl.h>
35 #include <linux/poll.h>
36 #include <linux/init.h>
37 #include <linux/string.h>
38 #include <linux/genhd.h>
39 #include <linux/blkdev.h>
40 #include <asm/uaccess.h>
41 #include <asm/pgtable.h>
42 #include <asm/io.h>
43 #include <asm/pcic.h>
44 #include <asm/oplib.h>
46 #include <asm/jsflash.h> /* ioctl arguments. <linux/> ?? */
47 #define JSFIDSZ (sizeof(struct jsflash_ident_arg))
48 #define JSFPRGSZ (sizeof(struct jsflash_program_arg))
51 * Our device numbers have no business in system headers.
52 * The only thing a user knows is the device name /dev/jsflash.
54 * Block devices are laid out like this:
55 * minor+0 - Bootstrap, for 8MB SIMM 0x20400000[0x800000]
56 * minor+1 - Filesystem to mount, normally 0x20400400[0x7ffc00]
57 * minor+2 - Whole flash area for any case... 0x20000000[0x01000000]
58 * Total 3 minors per flash device.
60 * It is easier to have static size vectors, so we define
61 * a total minor range JSF_MAX, which must cover all minors.
63 /* character device */
64 #define JSF_MINOR 178 /* 178 is registered with hpa */
65 /* block device */
66 #define JSF_MAX 3 /* 3 minors wasted total so far. */
67 #define JSF_NPART 3 /* 3 minors per flash device */
68 #define JSF_PART_BITS 2 /* 2 bits of minors to cover JSF_NPART */
69 #define JSF_PART_MASK 0x3 /* 2 bits mask */
72 * Access functions.
73 * We could ioremap(), but it's easier this way.
75 static unsigned int jsf_inl(unsigned long addr)
77 unsigned long retval;
79 __asm__ __volatile__("lda [%1] %2, %0\n\t" :
80 "=r" (retval) :
81 "r" (addr), "i" (ASI_M_BYPASS));
82 return retval;
85 static void jsf_outl(unsigned long addr, __u32 data)
88 __asm__ __volatile__("sta %0, [%1] %2\n\t" : :
89 "r" (data), "r" (addr), "i" (ASI_M_BYPASS) :
90 "memory");
94 * soft carrier
97 struct jsfd_part {
98 unsigned long dbase;
99 unsigned long dsize;
102 struct jsflash {
103 unsigned long base;
104 unsigned long size;
105 unsigned long busy; /* In use? */
106 struct jsflash_ident_arg id;
107 /* int mbase; */ /* Minor base, typically zero */
108 struct jsfd_part dv[JSF_NPART];
112 * We do not map normal memory or obio as a safety precaution.
113 * But offsets are real, for ease of userland programming.
115 #define JSF_BASE_TOP 0x30000000
116 #define JSF_BASE_ALL 0x20000000
118 #define JSF_BASE_JK 0x20400000
122 static struct gendisk *jsfd_disk[JSF_MAX];
125 * Let's pretend we may have several of these...
127 static struct jsflash jsf0;
130 * Wait for AMD to finish its embedded algorithm.
131 * We use the Toggle bit DQ6 (0x40) because it does not
132 * depend on the data value as /DATA bit DQ7 does.
134 * XXX Do we need any timeout here? So far it never hanged, beware broken hw.
136 static void jsf_wait(unsigned long p) {
137 unsigned int x1, x2;
139 for (;;) {
140 x1 = jsf_inl(p);
141 x2 = jsf_inl(p);
142 if ((x1 & 0x40404040) == (x2 & 0x40404040)) return;
147 * Programming will only work if Flash is clean,
148 * we leave it to the programmer application.
150 * AMD must be programmed one byte at a time;
151 * thus, Simple Tech SIMM must be written 4 bytes at a time.
153 * Write waits for the chip to become ready after the write
154 * was finished. This is done so that application would read
155 * consistent data after the write is done.
157 static void jsf_write4(unsigned long fa, u32 data) {
159 jsf_outl(fa, 0xAAAAAAAA); /* Unlock 1 Write 1 */
160 jsf_outl(fa, 0x55555555); /* Unlock 1 Write 2 */
161 jsf_outl(fa, 0xA0A0A0A0); /* Byte Program */
162 jsf_outl(fa, data);
164 jsf_wait(fa);
169 static void jsfd_read(char *buf, unsigned long p, size_t togo) {
170 union byte4 {
171 char s[4];
172 unsigned int n;
173 } b;
175 while (togo >= 4) {
176 togo -= 4;
177 b.n = jsf_inl(p);
178 memcpy(buf, b.s, 4);
179 p += 4;
180 buf += 4;
184 static void jsfd_do_request(struct request_queue *q)
186 struct request *req;
188 req = blk_fetch_request(q);
189 while (req) {
190 struct jsfd_part *jdp = req->rq_disk->private_data;
191 unsigned long offset = blk_rq_pos(req) << 9;
192 size_t len = blk_rq_cur_bytes(req);
193 int err = -EIO;
195 if ((offset + len) > jdp->dsize)
196 goto end;
198 if (rq_data_dir(req) != READ) {
199 printk(KERN_ERR "jsfd: write\n");
200 goto end;
203 if ((jdp->dbase & 0xff000000) != 0x20000000) {
204 printk(KERN_ERR "jsfd: bad base %x\n", (int)jdp->dbase);
205 goto end;
208 jsfd_read(req->buffer, jdp->dbase + offset, len);
209 err = 0;
210 end:
211 if (!__blk_end_request_cur(req, err))
212 req = blk_fetch_request(q);
217 * The memory devices use the full 32/64 bits of the offset, and so we cannot
218 * check against negative addresses: they are ok. The return value is weird,
219 * though, in that case (0).
221 * also note that seeking relative to the "end of file" isn't supported:
222 * it has no meaning, so it returns -EINVAL.
224 static loff_t jsf_lseek(struct file * file, loff_t offset, int orig)
226 loff_t ret;
228 lock_kernel();
229 switch (orig) {
230 case 0:
231 file->f_pos = offset;
232 ret = file->f_pos;
233 break;
234 case 1:
235 file->f_pos += offset;
236 ret = file->f_pos;
237 break;
238 default:
239 ret = -EINVAL;
241 unlock_kernel();
242 return ret;
246 * OS SIMM Cannot be read in other size but a 32bits word.
248 static ssize_t jsf_read(struct file * file, char __user * buf,
249 size_t togo, loff_t *ppos)
251 unsigned long p = *ppos;
252 char __user *tmp = buf;
254 union byte4 {
255 char s[4];
256 unsigned int n;
257 } b;
259 if (p < JSF_BASE_ALL || p >= JSF_BASE_TOP) {
260 return 0;
263 if ((p + togo) < p /* wrap */
264 || (p + togo) >= JSF_BASE_TOP) {
265 togo = JSF_BASE_TOP - p;
268 if (p < JSF_BASE_ALL && togo != 0) {
269 #if 0 /* __bzero XXX */
270 size_t x = JSF_BASE_ALL - p;
271 if (x > togo) x = togo;
272 clear_user(tmp, x);
273 tmp += x;
274 p += x;
275 togo -= x;
276 #else
278 * Implementation of clear_user() calls __bzero
279 * without regard to modversions,
280 * so we cannot build a module.
282 return 0;
283 #endif
286 while (togo >= 4) {
287 togo -= 4;
288 b.n = jsf_inl(p);
289 if (copy_to_user(tmp, b.s, 4))
290 return -EFAULT;
291 tmp += 4;
292 p += 4;
296 * XXX Small togo may remain if 1 byte is ordered.
297 * It would be nice if we did a word size read and unpacked it.
300 *ppos = p;
301 return tmp-buf;
304 static ssize_t jsf_write(struct file * file, const char __user * buf,
305 size_t count, loff_t *ppos)
307 return -ENOSPC;
312 static int jsf_ioctl_erase(unsigned long arg)
314 unsigned long p;
316 /* p = jsf0.base; hits wrong bank */
317 p = 0x20400000;
319 jsf_outl(p, 0xAAAAAAAA); /* Unlock 1 Write 1 */
320 jsf_outl(p, 0x55555555); /* Unlock 1 Write 2 */
321 jsf_outl(p, 0x80808080); /* Erase setup */
322 jsf_outl(p, 0xAAAAAAAA); /* Unlock 2 Write 1 */
323 jsf_outl(p, 0x55555555); /* Unlock 2 Write 2 */
324 jsf_outl(p, 0x10101010); /* Chip erase */
326 #if 0
328 * This code is ok, except that counter based timeout
329 * has no place in this world. Let's just drop timeouts...
332 int i;
333 __u32 x;
334 for (i = 0; i < 1000000; i++) {
335 x = jsf_inl(p);
336 if ((x & 0x80808080) == 0x80808080) break;
338 if ((x & 0x80808080) != 0x80808080) {
339 printk("jsf0: erase timeout with 0x%08x\n", x);
340 } else {
341 printk("jsf0: erase done with 0x%08x\n", x);
344 #else
345 jsf_wait(p);
346 #endif
348 return 0;
352 * Program a block of flash.
353 * Very simple because we can do it byte by byte anyway.
355 static int jsf_ioctl_program(void __user *arg)
357 struct jsflash_program_arg abuf;
358 char __user *uptr;
359 unsigned long p;
360 unsigned int togo;
361 union {
362 unsigned int n;
363 char s[4];
364 } b;
366 if (copy_from_user(&abuf, arg, JSFPRGSZ))
367 return -EFAULT;
368 p = abuf.off;
369 togo = abuf.size;
370 if ((togo & 3) || (p & 3)) return -EINVAL;
372 uptr = (char __user *) (unsigned long) abuf.data;
373 while (togo != 0) {
374 togo -= 4;
375 if (copy_from_user(&b.s[0], uptr, 4))
376 return -EFAULT;
377 jsf_write4(p, b.n);
378 p += 4;
379 uptr += 4;
382 return 0;
385 static long jsf_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
387 lock_kernel();
388 int error = -ENOTTY;
389 void __user *argp = (void __user *)arg;
391 if (!capable(CAP_SYS_ADMIN)) {
392 unlock_kernel();
393 return -EPERM;
395 switch (cmd) {
396 case JSFLASH_IDENT:
397 if (copy_to_user(argp, &jsf0.id, JSFIDSZ)) {
398 unlock_kernel();
399 return -EFAULT;
401 break;
402 case JSFLASH_ERASE:
403 error = jsf_ioctl_erase(arg);
404 break;
405 case JSFLASH_PROGRAM:
406 error = jsf_ioctl_program(argp);
407 break;
410 unlock_kernel();
411 return error;
414 static int jsf_mmap(struct file * file, struct vm_area_struct * vma)
416 return -ENXIO;
419 static int jsf_open(struct inode * inode, struct file * filp)
421 lock_kernel();
422 if (jsf0.base == 0) {
423 unlock_kernel();
424 return -ENXIO;
426 if (test_and_set_bit(0, (void *)&jsf0.busy) != 0) {
427 unlock_kernel();
428 return -EBUSY;
431 unlock_kernel();
432 return 0; /* XXX What security? */
435 static int jsf_release(struct inode *inode, struct file *file)
437 jsf0.busy = 0;
438 return 0;
441 static const struct file_operations jsf_fops = {
442 .owner = THIS_MODULE,
443 .llseek = jsf_lseek,
444 .read = jsf_read,
445 .write = jsf_write,
446 .unlocked_ioctl = jsf_ioctl,
447 .mmap = jsf_mmap,
448 .open = jsf_open,
449 .release = jsf_release,
452 static struct miscdevice jsf_dev = { JSF_MINOR, "jsflash", &jsf_fops };
454 static const struct block_device_operations jsfd_fops = {
455 .owner = THIS_MODULE,
458 static int jsflash_init(void)
460 int rc;
461 struct jsflash *jsf;
462 int node;
463 char banner[128];
464 struct linux_prom_registers reg0;
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;
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;
480 * Flash may be somewhere else, for instance on Ebus.
481 * So, don't do the following check for IIep flash space.
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;
489 #endif
490 if ((int)reg0.reg_size <= 0) {
491 printk("jsflash: bad size 0x%x\n", (int)reg0.reg_size);
492 return -ENXIO;
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;
502 reg0.which_io = 0;
503 reg0.phys_addr = 0x20400000;
504 reg0.reg_size = 0x00800000;
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;
514 if (jsf0.base == 0) {
515 jsf = &jsf0;
517 jsf->base = reg0.phys_addr;
518 jsf->size = reg0.reg_size;
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");
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;
532 printk("Espresso Flash @0x%lx [%d MB]\n", jsf->base,
533 (int) (jsf->size / (1024*1024)));
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;
543 return 0;
546 static struct request_queue *jsf_queue;
548 static int jsfd_init(void)
550 static DEFINE_SPINLOCK(lock);
551 struct jsflash *jsf;
552 struct jsfd_part *jdp;
553 int err;
554 int i;
556 if (jsf0.base == 0)
557 return -ENXIO;
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;
567 if (register_blkdev(JSFD_MAJOR, "jsfd")) {
568 err = -EIO;
569 goto out;
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;
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];
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);
595 return 0;
596 out:
597 while (i--)
598 put_disk(jsfd_disk[i]);
599 return err;
602 MODULE_LICENSE("GPL");
604 static int __init jsflash_init_module(void) {
605 int rc;
607 if ((rc = jsflash_init()) == 0) {
608 jsfd_init();
609 return 0;
611 return rc;
614 static void __exit jsflash_cleanup_module(void)
616 int i;
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]);
623 if (jsf0.busy)
624 printk("jsf0: cleaning busy unit\n");
625 jsf0.base = 0;
626 jsf0.busy = 0;
628 misc_deregister(&jsf_dev);
629 unregister_blkdev(JSFD_MAJOR, "jsfd");
630 blk_cleanup_queue(jsf_queue);
633 module_init(jsflash_init_module);
634 module_exit(jsflash_cleanup_module);