jsflash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 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. #include <linux/module.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/types.h>
  31. #include <linux/errno.h>
  32. #include <linux/miscdevice.h>
  33. #include <linux/slab.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>
  45. #include <asm/jsflash.h> /* ioctl arguments. <linux/> ?? */
  46. #define JSFIDSZ (sizeof(struct jsflash_ident_arg))
  47. #define JSFPRGSZ (sizeof(struct jsflash_program_arg))
  48. /*
  49. * Our device numbers have no business in system headers.
  50. * The only thing a user knows is the device name /dev/jsflash.
  51. *
  52. * Block devices are laid out like this:
  53. * minor+0 - Bootstrap, for 8MB SIMM 0x20400000[0x800000]
  54. * minor+1 - Filesystem to mount, normally 0x20400400[0x7ffc00]
  55. * minor+2 - Whole flash area for any case... 0x20000000[0x01000000]
  56. * Total 3 minors per flash device.
  57. *
  58. * It is easier to have static size vectors, so we define
  59. * a total minor range JSF_MAX, which must cover all minors.
  60. */
  61. /* character device */
  62. #define JSF_MINOR 178 /* 178 is registered with hpa */
  63. /* block device */
  64. #define JSF_MAX 3 /* 3 minors wasted total so far. */
  65. #define JSF_NPART 3 /* 3 minors per flash device */
  66. #define JSF_PART_BITS 2 /* 2 bits of minors to cover JSF_NPART */
  67. #define JSF_PART_MASK 0x3 /* 2 bits mask */
  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. while ((req = elv_next_request(q)) != NULL) {
  167. struct jsfd_part *jdp = req->rq_disk->private_data;
  168. unsigned long offset = req->sector << 9;
  169. size_t len = req->current_nr_sectors << 9;
  170. if ((offset + len) > jdp->dsize) {
  171. end_request(req, 0);
  172. continue;
  173. }
  174. if (rq_data_dir(req) != READ) {
  175. printk(KERN_ERR "jsfd: write\n");
  176. end_request(req, 0);
  177. continue;
  178. }
  179. if ((jdp->dbase & 0xff000000) != 0x20000000) {
  180. printk(KERN_ERR "jsfd: bad base %x\n", (int)jdp->dbase);
  181. end_request(req, 0);
  182. continue;
  183. }
  184. jsfd_read(req->buffer, jdp->dbase + offset, len);
  185. end_request(req, 1);
  186. }
  187. }
  188. /*
  189. * The memory devices use the full 32/64 bits of the offset, and so we cannot
  190. * check against negative addresses: they are ok. The return value is weird,
  191. * though, in that case (0).
  192. *
  193. * also note that seeking relative to the "end of file" isn't supported:
  194. * it has no meaning, so it returns -EINVAL.
  195. */
  196. static loff_t jsf_lseek(struct file * file, loff_t offset, int orig)
  197. {
  198. loff_t ret;
  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. * OS SIMM Cannot be read in other size but a 32bits word.
  217. */
  218. static ssize_t jsf_read(struct file * file, char __user * buf,
  219. size_t togo, loff_t *ppos)
  220. {
  221. unsigned long p = *ppos;
  222. char __user *tmp = buf;
  223. union byte4 {
  224. char s[4];
  225. unsigned int n;
  226. } b;
  227. if (p < JSF_BASE_ALL || p >= JSF_BASE_TOP) {
  228. return 0;
  229. }
  230. if ((p + togo) < p /* wrap */
  231. || (p + togo) >= JSF_BASE_TOP) {
  232. togo = JSF_BASE_TOP - p;
  233. }
  234. if (p < JSF_BASE_ALL && togo != 0) {
  235. #if 0 /* __bzero XXX */
  236. size_t x = JSF_BASE_ALL - p;
  237. if (x > togo) x = togo;
  238. clear_user(tmp, x);
  239. tmp += x;
  240. p += x;
  241. togo -= x;
  242. #else
  243. /*
  244. * Implementation of clear_user() calls __bzero
  245. * without regard to modversions,
  246. * so we cannot build a module.
  247. */
  248. return 0;
  249. #endif
  250. }
  251. while (togo >= 4) {
  252. togo -= 4;
  253. b.n = jsf_inl(p);
  254. if (copy_to_user(tmp, b.s, 4))
  255. return -EFAULT;
  256. tmp += 4;
  257. p += 4;
  258. }
  259. /*
  260. * XXX Small togo may remain if 1 byte is ordered.
  261. * It would be nice if we did a word size read and unpacked it.
  262. */
  263. *ppos = p;
  264. return tmp-buf;
  265. }
  266. static ssize_t jsf_write(struct file * file, const char __user * buf,
  267. size_t count, loff_t *ppos)
  268. {
  269. return -ENOSPC;
  270. }
  271. /*
  272. */
  273. static int jsf_ioctl_erase(unsigned long arg)
  274. {
  275. unsigned long p;
  276. /* p = jsf0.base; hits wrong bank */
  277. p = 0x20400000;
  278. jsf_outl(p, 0xAAAAAAAA); /* Unlock 1 Write 1 */
  279. jsf_outl(p, 0x55555555); /* Unlock 1 Write 2 */
  280. jsf_outl(p, 0x80808080); /* Erase setup */
  281. jsf_outl(p, 0xAAAAAAAA); /* Unlock 2 Write 1 */
  282. jsf_outl(p, 0x55555555); /* Unlock 2 Write 2 */
  283. jsf_outl(p, 0x10101010); /* Chip erase */
  284. #if 0
  285. /*
  286. * This code is ok, except that counter based timeout
  287. * has no place in this world. Let's just drop timeouts...
  288. */
  289. {
  290. int i;
  291. __u32 x;
  292. for (i = 0; i < 1000000; i++) {
  293. x = jsf_inl(p);
  294. if ((x & 0x80808080) == 0x80808080) break;
  295. }
  296. if ((x & 0x80808080) != 0x80808080) {
  297. printk("jsf0: erase timeout with 0x%08x\n", x);
  298. } else {
  299. printk("jsf0: erase done with 0x%08x\n", x);
  300. }
  301. }
  302. #else
  303. jsf_wait(p);
  304. #endif
  305. return 0;
  306. }
  307. /*
  308. * Program a block of flash.
  309. * Very simple because we can do it byte by byte anyway.
  310. */
  311. static int jsf_ioctl_program(void __user *arg)
  312. {
  313. struct jsflash_program_arg abuf;
  314. char __user *uptr;
  315. unsigned long p;
  316. unsigned int togo;
  317. union {
  318. unsigned int n;
  319. char s[4];
  320. } b;
  321. if (copy_from_user(&abuf, arg, JSFPRGSZ))
  322. return -EFAULT;
  323. p = abuf.off;
  324. togo = abuf.size;
  325. if ((togo & 3) || (p & 3)) return -EINVAL;
  326. uptr = (char __user *) (unsigned long) abuf.data;
  327. while (togo != 0) {
  328. togo -= 4;
  329. if (copy_from_user(&b.s[0], uptr, 4))
  330. return -EFAULT;
  331. jsf_write4(p, b.n);
  332. p += 4;
  333. uptr += 4;
  334. }
  335. return 0;
  336. }
  337. static long jsf_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  338. {
  339. lock_kernel();
  340. int error = -ENOTTY;
  341. void __user *argp = (void __user *)arg;
  342. if (!capable(CAP_SYS_ADMIN)) {
  343. unlock_kernel();
  344. return -EPERM;
  345. }
  346. switch (cmd) {
  347. case JSFLASH_IDENT:
  348. if (copy_to_user(argp, &jsf0.id, JSFIDSZ)) {
  349. unlock_kernel();
  350. return -EFAULT;
  351. }
  352. break;
  353. case JSFLASH_ERASE:
  354. error = jsf_ioctl_erase(arg);
  355. break;
  356. case JSFLASH_PROGRAM:
  357. error = jsf_ioctl_program(argp);
  358. break;
  359. }
  360. unlock_kernel();
  361. return error;
  362. }
  363. static int jsf_mmap(struct file * file, struct vm_area_struct * vma)
  364. {
  365. return -ENXIO;
  366. }
  367. static int jsf_open(struct inode * inode, struct file * filp)
  368. {
  369. lock_kernel();
  370. if (jsf0.base == 0) {
  371. unlock_kernel();
  372. return -ENXIO;
  373. }
  374. if (test_and_set_bit(0, (void *)&jsf0.busy) != 0) {
  375. unlock_kernel();
  376. return -EBUSY;
  377. }
  378. unlock_kernel();
  379. return 0; /* XXX What security? */
  380. }
  381. static int jsf_release(struct inode *inode, struct file *file)
  382. {
  383. jsf0.busy = 0;
  384. return 0;
  385. }
  386. static const struct file_operations jsf_fops = {
  387. .owner = THIS_MODULE,
  388. .llseek = jsf_lseek,
  389. .read = jsf_read,
  390. .write = jsf_write,
  391. .unlocked_ioctl = jsf_ioctl,
  392. .mmap = jsf_mmap,
  393. .open = jsf_open,
  394. .release = jsf_release,
  395. };
  396. static struct miscdevice jsf_dev = { JSF_MINOR, "jsflash", &jsf_fops };
  397. static struct block_device_operations jsfd_fops = {
  398. .owner = THIS_MODULE,
  399. };
  400. static int jsflash_init(void)
  401. {
  402. int rc;
  403. struct jsflash *jsf;
  404. int node;
  405. char banner[128];
  406. struct linux_prom_registers reg0;
  407. node = prom_getchild(prom_root_node);
  408. node = prom_searchsiblings(node, "flash-memory");
  409. if (node != 0 && node != -1) {
  410. if (prom_getproperty(node, "reg",
  411. (char *)&reg0, sizeof(reg0)) == -1) {
  412. printk("jsflash: no \"reg\" property\n");
  413. return -ENXIO;
  414. }
  415. if (reg0.which_io != 0) {
  416. printk("jsflash: bus number nonzero: 0x%x:%x\n",
  417. reg0.which_io, reg0.phys_addr);
  418. return -ENXIO;
  419. }
  420. /*
  421. * Flash may be somewhere else, for instance on Ebus.
  422. * So, don't do the following check for IIep flash space.
  423. */
  424. #if 0
  425. if ((reg0.phys_addr >> 24) != 0x20) {
  426. printk("jsflash: suspicious address: 0x%x:%x\n",
  427. reg0.which_io, reg0.phys_addr);
  428. return -ENXIO;
  429. }
  430. #endif
  431. if ((int)reg0.reg_size <= 0) {
  432. printk("jsflash: bad size 0x%x\n", (int)reg0.reg_size);
  433. return -ENXIO;
  434. }
  435. } else {
  436. /* XXX Remove this code once PROLL ID12 got widespread */
  437. printk("jsflash: no /flash-memory node, use PROLL >= 12\n");
  438. prom_getproperty(prom_root_node, "banner-name", banner, 128);
  439. if (strcmp (banner, "JavaStation-NC") != 0 &&
  440. strcmp (banner, "JavaStation-E") != 0) {
  441. return -ENXIO;
  442. }
  443. reg0.which_io = 0;
  444. reg0.phys_addr = 0x20400000;
  445. reg0.reg_size = 0x00800000;
  446. }
  447. /* Let us be really paranoid for modifications to probing code. */
  448. /* extern enum sparc_cpu sparc_cpu_model; */ /* in <asm/system.h> */
  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);