PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/sbus/char/jsflash.c

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