jsflash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 int jsf_ioctl(struct inode *inode, struct file *f, unsigned int cmd,
  338. unsigned long arg)
  339. {
  340. int error = -ENOTTY;
  341. void __user *argp = (void __user *)arg;
  342. if (!capable(CAP_SYS_ADMIN))
  343. return -EPERM;
  344. switch (cmd) {
  345. case JSFLASH_IDENT:
  346. if (copy_to_user(argp, &jsf0.id, JSFIDSZ))
  347. return -EFAULT;
  348. break;
  349. case JSFLASH_ERASE:
  350. error = jsf_ioctl_erase(arg);
  351. break;
  352. case JSFLASH_PROGRAM:
  353. error = jsf_ioctl_program(argp);
  354. break;
  355. }
  356. return error;
  357. }
  358. static int jsf_mmap(struct file * file, struct vm_area_struct * vma)
  359. {
  360. return -ENXIO;
  361. }
  362. static int jsf_open(struct inode * inode, struct file * filp)
  363. {
  364. lock_kernel();
  365. if (jsf0.base == 0) {
  366. unlock_kernel();
  367. return -ENXIO;
  368. }
  369. if (test_and_set_bit(0, (void *)&jsf0.busy) != 0) {
  370. unlock_kernel();
  371. return -EBUSY;
  372. }
  373. unlock_kernel();
  374. return 0; /* XXX What security? */
  375. }
  376. static int jsf_release(struct inode *inode, struct file *file)
  377. {
  378. jsf0.busy = 0;
  379. return 0;
  380. }
  381. static const struct file_operations jsf_fops = {
  382. .owner = THIS_MODULE,
  383. .llseek = jsf_lseek,
  384. .read = jsf_read,
  385. .write = jsf_write,
  386. .ioctl = jsf_ioctl,
  387. .mmap = jsf_mmap,
  388. .open = jsf_open,
  389. .release = jsf_release,
  390. };
  391. static struct miscdevice jsf_dev = { JSF_MINOR, "jsflash", &jsf_fops };
  392. static struct block_device_operations jsfd_fops = {
  393. .owner = THIS_MODULE,
  394. };
  395. static int jsflash_init(void)
  396. {
  397. int rc;
  398. struct jsflash *jsf;
  399. int node;
  400. char banner[128];
  401. struct linux_prom_registers reg0;
  402. node = prom_getchild(prom_root_node);
  403. node = prom_searchsiblings(node, "flash-memory");
  404. if (node != 0 && node != -1) {
  405. if (prom_getproperty(node, "reg",
  406. (char *)&reg0, sizeof(reg0)) == -1) {
  407. printk("jsflash: no \"reg\" property\n");
  408. return -ENXIO;
  409. }
  410. if (reg0.which_io != 0) {
  411. printk("jsflash: bus number nonzero: 0x%x:%x\n",
  412. reg0.which_io, reg0.phys_addr);
  413. return -ENXIO;
  414. }
  415. /*
  416. * Flash may be somewhere else, for instance on Ebus.
  417. * So, don't do the following check for IIep flash space.
  418. */
  419. #if 0
  420. if ((reg0.phys_addr >> 24) != 0x20) {
  421. printk("jsflash: suspicious address: 0x%x:%x\n",
  422. reg0.which_io, reg0.phys_addr);
  423. return -ENXIO;
  424. }
  425. #endif
  426. if ((int)reg0.reg_size <= 0) {
  427. printk("jsflash: bad size 0x%x\n", (int)reg0.reg_size);
  428. return -ENXIO;
  429. }
  430. } else {
  431. /* XXX Remove this code once PROLL ID12 got widespread */
  432. printk("jsflash: no /flash-memory node, use PROLL >= 12\n");
  433. prom_getproperty(prom_root_node, "banner-name", banner, 128);
  434. if (strcmp (banner, "JavaStation-NC") != 0 &&
  435. strcmp (banner, "JavaStation-E") != 0) {
  436. return -ENXIO;
  437. }
  438. reg0.which_io = 0;
  439. reg0.phys_addr = 0x20400000;
  440. reg0.reg_size = 0x00800000;
  441. }
  442. /* Let us be really paranoid for modifications to probing code. */
  443. /* extern enum sparc_cpu sparc_cpu_model; */ /* in <asm/system.h> */
  444. if (sparc_cpu_model != sun4m) {
  445. /* We must be on sun4m because we use MMU Bypass ASI. */
  446. return -ENXIO;
  447. }
  448. if (jsf0.base == 0) {
  449. jsf = &jsf0;
  450. jsf->base = reg0.phys_addr;
  451. jsf->size = reg0.reg_size;
  452. /* XXX Redo the userland interface. */
  453. jsf->id.off = JSF_BASE_ALL;
  454. jsf->id.size = 0x01000000; /* 16M - all segments */
  455. strcpy(jsf->id.name, "Krups_all");
  456. jsf->dv[0].dbase = jsf->base;
  457. jsf->dv[0].dsize = jsf->size;
  458. jsf->dv[1].dbase = jsf->base + 1024;
  459. jsf->dv[1].dsize = jsf->size - 1024;
  460. jsf->dv[2].dbase = JSF_BASE_ALL;
  461. jsf->dv[2].dsize = 0x01000000;
  462. printk("Espresso Flash @0x%lx [%d MB]\n", jsf->base,
  463. (int) (jsf->size / (1024*1024)));
  464. }
  465. if ((rc = misc_register(&jsf_dev)) != 0) {
  466. printk(KERN_ERR "jsf: unable to get misc minor %d\n",
  467. JSF_MINOR);
  468. jsf0.base = 0;
  469. return rc;
  470. }
  471. return 0;
  472. }
  473. static struct request_queue *jsf_queue;
  474. static int jsfd_init(void)
  475. {
  476. static DEFINE_SPINLOCK(lock);
  477. struct jsflash *jsf;
  478. struct jsfd_part *jdp;
  479. int err;
  480. int i;
  481. if (jsf0.base == 0)
  482. return -ENXIO;
  483. err = -ENOMEM;
  484. for (i = 0; i < JSF_MAX; i++) {
  485. struct gendisk *disk = alloc_disk(1);
  486. if (!disk)
  487. goto out;
  488. jsfd_disk[i] = disk;
  489. }
  490. if (register_blkdev(JSFD_MAJOR, "jsfd")) {
  491. err = -EIO;
  492. goto out;
  493. }
  494. jsf_queue = blk_init_queue(jsfd_do_request, &lock);
  495. if (!jsf_queue) {
  496. err = -ENOMEM;
  497. unregister_blkdev(JSFD_MAJOR, "jsfd");
  498. goto out;
  499. }
  500. for (i = 0; i < JSF_MAX; i++) {
  501. struct gendisk *disk = jsfd_disk[i];
  502. if ((i & JSF_PART_MASK) >= JSF_NPART) continue;
  503. jsf = &jsf0; /* actually, &jsfv[i >> JSF_PART_BITS] */
  504. jdp = &jsf->dv[i&JSF_PART_MASK];
  505. disk->major = JSFD_MAJOR;
  506. disk->first_minor = i;
  507. sprintf(disk->disk_name, "jsfd%d", i);
  508. disk->fops = &jsfd_fops;
  509. set_capacity(disk, jdp->dsize >> 9);
  510. disk->private_data = jdp;
  511. disk->queue = jsf_queue;
  512. add_disk(disk);
  513. set_disk_ro(disk, 1);
  514. }
  515. return 0;
  516. out:
  517. while (i--)
  518. put_disk(jsfd_disk[i]);
  519. return err;
  520. }
  521. MODULE_LICENSE("GPL");
  522. static int __init jsflash_init_module(void) {
  523. int rc;
  524. if ((rc = jsflash_init()) == 0) {
  525. jsfd_init();
  526. return 0;
  527. }
  528. return rc;
  529. }
  530. static void __exit jsflash_cleanup_module(void)
  531. {
  532. int i;
  533. for (i = 0; i < JSF_MAX; i++) {
  534. if ((i & JSF_PART_MASK) >= JSF_NPART) continue;
  535. del_gendisk(jsfd_disk[i]);
  536. put_disk(jsfd_disk[i]);
  537. }
  538. if (jsf0.busy)
  539. printk("jsf0: cleaning busy unit\n");
  540. jsf0.base = 0;
  541. jsf0.busy = 0;
  542. misc_deregister(&jsf_dev);
  543. unregister_blkdev(JSFD_MAJOR, "jsfd");
  544. blk_cleanup_queue(jsf_queue);
  545. }
  546. module_init(jsflash_init_module);
  547. module_exit(jsflash_cleanup_module);