jsflash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 <linux/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 int jsfd_queue;
  164. static struct request *jsfd_next_request(void)
  165. {
  166. struct request_queue *q;
  167. struct request *rq;
  168. int old_pos = jsfd_queue;
  169. do {
  170. q = jsfd_disk[jsfd_queue]->queue;
  171. if (++jsfd_queue == JSF_MAX)
  172. jsfd_queue = 0;
  173. if (q) {
  174. rq = blk_fetch_request(q);
  175. if (rq)
  176. return rq;
  177. }
  178. } while (jsfd_queue != old_pos);
  179. return NULL;
  180. }
  181. static void jsfd_request(void)
  182. {
  183. struct request *req;
  184. req = jsfd_next_request();
  185. while (req) {
  186. struct jsfd_part *jdp = req->rq_disk->private_data;
  187. unsigned long offset = blk_rq_pos(req) << 9;
  188. size_t len = blk_rq_cur_bytes(req);
  189. int err = -EIO;
  190. if ((offset + len) > jdp->dsize)
  191. goto end;
  192. if (rq_data_dir(req) != READ) {
  193. printk(KERN_ERR "jsfd: write\n");
  194. goto end;
  195. }
  196. if ((jdp->dbase & 0xff000000) != 0x20000000) {
  197. printk(KERN_ERR "jsfd: bad base %x\n", (int)jdp->dbase);
  198. goto end;
  199. }
  200. jsfd_read(bio_data(req->bio), jdp->dbase + offset, len);
  201. err = 0;
  202. end:
  203. if (!__blk_end_request_cur(req, err))
  204. req = jsfd_next_request();
  205. }
  206. }
  207. static void jsfd_do_request(struct request_queue *q)
  208. {
  209. jsfd_request();
  210. }
  211. /*
  212. * The memory devices use the full 32/64 bits of the offset, and so we cannot
  213. * check against negative addresses: they are ok. The return value is weird,
  214. * though, in that case (0).
  215. *
  216. * also note that seeking relative to the "end of file" isn't supported:
  217. * it has no meaning, so it returns -EINVAL.
  218. */
  219. static loff_t jsf_lseek(struct file * file, loff_t offset, int orig)
  220. {
  221. loff_t ret;
  222. mutex_lock(&jsf_mutex);
  223. switch (orig) {
  224. case 0:
  225. file->f_pos = offset;
  226. ret = file->f_pos;
  227. break;
  228. case 1:
  229. file->f_pos += offset;
  230. ret = file->f_pos;
  231. break;
  232. default:
  233. ret = -EINVAL;
  234. }
  235. mutex_unlock(&jsf_mutex);
  236. return ret;
  237. }
  238. /*
  239. * OS SIMM Cannot be read in other size but a 32bits word.
  240. */
  241. static ssize_t jsf_read(struct file * file, char __user * buf,
  242. size_t togo, loff_t *ppos)
  243. {
  244. unsigned long p = *ppos;
  245. char __user *tmp = buf;
  246. union byte4 {
  247. char s[4];
  248. unsigned int n;
  249. } b;
  250. if (p < JSF_BASE_ALL || p >= JSF_BASE_TOP) {
  251. return 0;
  252. }
  253. if ((p + togo) < p /* wrap */
  254. || (p + togo) >= JSF_BASE_TOP) {
  255. togo = JSF_BASE_TOP - p;
  256. }
  257. if (p < JSF_BASE_ALL && togo != 0) {
  258. #if 0 /* __bzero XXX */
  259. size_t x = JSF_BASE_ALL - p;
  260. if (x > togo) x = togo;
  261. clear_user(tmp, x);
  262. tmp += x;
  263. p += x;
  264. togo -= x;
  265. #else
  266. /*
  267. * Implementation of clear_user() calls __bzero
  268. * without regard to modversions,
  269. * so we cannot build a module.
  270. */
  271. return 0;
  272. #endif
  273. }
  274. while (togo >= 4) {
  275. togo -= 4;
  276. b.n = jsf_inl(p);
  277. if (copy_to_user(tmp, b.s, 4))
  278. return -EFAULT;
  279. tmp += 4;
  280. p += 4;
  281. }
  282. /*
  283. * XXX Small togo may remain if 1 byte is ordered.
  284. * It would be nice if we did a word size read and unpacked it.
  285. */
  286. *ppos = p;
  287. return tmp-buf;
  288. }
  289. static ssize_t jsf_write(struct file * file, const char __user * buf,
  290. size_t count, loff_t *ppos)
  291. {
  292. return -ENOSPC;
  293. }
  294. /*
  295. */
  296. static int jsf_ioctl_erase(unsigned long arg)
  297. {
  298. unsigned long p;
  299. /* p = jsf0.base; hits wrong bank */
  300. p = 0x20400000;
  301. jsf_outl(p, 0xAAAAAAAA); /* Unlock 1 Write 1 */
  302. jsf_outl(p, 0x55555555); /* Unlock 1 Write 2 */
  303. jsf_outl(p, 0x80808080); /* Erase setup */
  304. jsf_outl(p, 0xAAAAAAAA); /* Unlock 2 Write 1 */
  305. jsf_outl(p, 0x55555555); /* Unlock 2 Write 2 */
  306. jsf_outl(p, 0x10101010); /* Chip erase */
  307. #if 0
  308. /*
  309. * This code is ok, except that counter based timeout
  310. * has no place in this world. Let's just drop timeouts...
  311. */
  312. {
  313. int i;
  314. __u32 x;
  315. for (i = 0; i < 1000000; i++) {
  316. x = jsf_inl(p);
  317. if ((x & 0x80808080) == 0x80808080) break;
  318. }
  319. if ((x & 0x80808080) != 0x80808080) {
  320. printk("jsf0: erase timeout with 0x%08x\n", x);
  321. } else {
  322. printk("jsf0: erase done with 0x%08x\n", x);
  323. }
  324. }
  325. #else
  326. jsf_wait(p);
  327. #endif
  328. return 0;
  329. }
  330. /*
  331. * Program a block of flash.
  332. * Very simple because we can do it byte by byte anyway.
  333. */
  334. static int jsf_ioctl_program(void __user *arg)
  335. {
  336. struct jsflash_program_arg abuf;
  337. char __user *uptr;
  338. unsigned long p;
  339. unsigned int togo;
  340. union {
  341. unsigned int n;
  342. char s[4];
  343. } b;
  344. if (copy_from_user(&abuf, arg, JSFPRGSZ))
  345. return -EFAULT;
  346. p = abuf.off;
  347. togo = abuf.size;
  348. if ((togo & 3) || (p & 3)) return -EINVAL;
  349. uptr = (char __user *) (unsigned long) abuf.data;
  350. while (togo != 0) {
  351. togo -= 4;
  352. if (copy_from_user(&b.s[0], uptr, 4))
  353. return -EFAULT;
  354. jsf_write4(p, b.n);
  355. p += 4;
  356. uptr += 4;
  357. }
  358. return 0;
  359. }
  360. static long jsf_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  361. {
  362. mutex_lock(&jsf_mutex);
  363. int error = -ENOTTY;
  364. void __user *argp = (void __user *)arg;
  365. if (!capable(CAP_SYS_ADMIN)) {
  366. mutex_unlock(&jsf_mutex);
  367. return -EPERM;
  368. }
  369. switch (cmd) {
  370. case JSFLASH_IDENT:
  371. if (copy_to_user(argp, &jsf0.id, JSFIDSZ)) {
  372. mutex_unlock(&jsf_mutex);
  373. return -EFAULT;
  374. }
  375. break;
  376. case JSFLASH_ERASE:
  377. error = jsf_ioctl_erase(arg);
  378. break;
  379. case JSFLASH_PROGRAM:
  380. error = jsf_ioctl_program(argp);
  381. break;
  382. }
  383. mutex_unlock(&jsf_mutex);
  384. return error;
  385. }
  386. static int jsf_mmap(struct file * file, struct vm_area_struct * vma)
  387. {
  388. return -ENXIO;
  389. }
  390. static int jsf_open(struct inode * inode, struct file * filp)
  391. {
  392. mutex_lock(&jsf_mutex);
  393. if (jsf0.base == 0) {
  394. mutex_unlock(&jsf_mutex);
  395. return -ENXIO;
  396. }
  397. if (test_and_set_bit(0, (void *)&jsf0.busy) != 0) {
  398. mutex_unlock(&jsf_mutex);
  399. return -EBUSY;
  400. }
  401. mutex_unlock(&jsf_mutex);
  402. return 0; /* XXX What security? */
  403. }
  404. static int jsf_release(struct inode *inode, struct file *file)
  405. {
  406. jsf0.busy = 0;
  407. return 0;
  408. }
  409. static const struct file_operations jsf_fops = {
  410. .owner = THIS_MODULE,
  411. .llseek = jsf_lseek,
  412. .read = jsf_read,
  413. .write = jsf_write,
  414. .unlocked_ioctl = jsf_ioctl,
  415. .mmap = jsf_mmap,
  416. .open = jsf_open,
  417. .release = jsf_release,
  418. };
  419. static struct miscdevice jsf_dev = { JSF_MINOR, "jsflash", &jsf_fops };
  420. static const struct block_device_operations jsfd_fops = {
  421. .owner = THIS_MODULE,
  422. };
  423. static int jsflash_init(void)
  424. {
  425. int rc;
  426. struct jsflash *jsf;
  427. phandle node;
  428. char banner[128];
  429. struct linux_prom_registers reg0;
  430. node = prom_getchild(prom_root_node);
  431. node = prom_searchsiblings(node, "flash-memory");
  432. if (node != 0 && (s32)node != -1) {
  433. if (prom_getproperty(node, "reg",
  434. (char *)&reg0, sizeof(reg0)) == -1) {
  435. printk("jsflash: no \"reg\" property\n");
  436. return -ENXIO;
  437. }
  438. if (reg0.which_io != 0) {
  439. printk("jsflash: bus number nonzero: 0x%x:%x\n",
  440. reg0.which_io, reg0.phys_addr);
  441. return -ENXIO;
  442. }
  443. /*
  444. * Flash may be somewhere else, for instance on Ebus.
  445. * So, don't do the following check for IIep flash space.
  446. */
  447. #if 0
  448. if ((reg0.phys_addr >> 24) != 0x20) {
  449. printk("jsflash: suspicious address: 0x%x:%x\n",
  450. reg0.which_io, reg0.phys_addr);
  451. return -ENXIO;
  452. }
  453. #endif
  454. if ((int)reg0.reg_size <= 0) {
  455. printk("jsflash: bad size 0x%x\n", (int)reg0.reg_size);
  456. return -ENXIO;
  457. }
  458. } else {
  459. /* XXX Remove this code once PROLL ID12 got widespread */
  460. printk("jsflash: no /flash-memory node, use PROLL >= 12\n");
  461. prom_getproperty(prom_root_node, "banner-name", banner, 128);
  462. if (strcmp (banner, "JavaStation-NC") != 0 &&
  463. strcmp (banner, "JavaStation-E") != 0) {
  464. return -ENXIO;
  465. }
  466. reg0.which_io = 0;
  467. reg0.phys_addr = 0x20400000;
  468. reg0.reg_size = 0x00800000;
  469. }
  470. /* Let us be really paranoid for modifications to probing code. */
  471. if (sparc_cpu_model != sun4m) {
  472. /* We must be on sun4m because we use MMU Bypass ASI. */
  473. return -ENXIO;
  474. }
  475. if (jsf0.base == 0) {
  476. jsf = &jsf0;
  477. jsf->base = reg0.phys_addr;
  478. jsf->size = reg0.reg_size;
  479. /* XXX Redo the userland interface. */
  480. jsf->id.off = JSF_BASE_ALL;
  481. jsf->id.size = 0x01000000; /* 16M - all segments */
  482. strcpy(jsf->id.name, "Krups_all");
  483. jsf->dv[0].dbase = jsf->base;
  484. jsf->dv[0].dsize = jsf->size;
  485. jsf->dv[1].dbase = jsf->base + 1024;
  486. jsf->dv[1].dsize = jsf->size - 1024;
  487. jsf->dv[2].dbase = JSF_BASE_ALL;
  488. jsf->dv[2].dsize = 0x01000000;
  489. printk("Espresso Flash @0x%lx [%d MB]\n", jsf->base,
  490. (int) (jsf->size / (1024*1024)));
  491. }
  492. if ((rc = misc_register(&jsf_dev)) != 0) {
  493. printk(KERN_ERR "jsf: unable to get misc minor %d\n",
  494. JSF_MINOR);
  495. jsf0.base = 0;
  496. return rc;
  497. }
  498. return 0;
  499. }
  500. static int jsfd_init(void)
  501. {
  502. static DEFINE_SPINLOCK(lock);
  503. struct jsflash *jsf;
  504. struct jsfd_part *jdp;
  505. int err;
  506. int i;
  507. if (jsf0.base == 0)
  508. return -ENXIO;
  509. err = -ENOMEM;
  510. for (i = 0; i < JSF_MAX; i++) {
  511. struct gendisk *disk = alloc_disk(1);
  512. if (!disk)
  513. goto out;
  514. disk->queue = blk_init_queue(jsfd_do_request, &lock);
  515. if (!disk->queue) {
  516. put_disk(disk);
  517. goto out;
  518. }
  519. jsfd_disk[i] = disk;
  520. }
  521. if (register_blkdev(JSFD_MAJOR, "jsfd")) {
  522. err = -EIO;
  523. goto out;
  524. }
  525. for (i = 0; i < JSF_MAX; i++) {
  526. struct gendisk *disk = jsfd_disk[i];
  527. if ((i & JSF_PART_MASK) >= JSF_NPART) continue;
  528. jsf = &jsf0; /* actually, &jsfv[i >> JSF_PART_BITS] */
  529. jdp = &jsf->dv[i&JSF_PART_MASK];
  530. disk->major = JSFD_MAJOR;
  531. disk->first_minor = i;
  532. sprintf(disk->disk_name, "jsfd%d", i);
  533. disk->fops = &jsfd_fops;
  534. set_capacity(disk, jdp->dsize >> 9);
  535. disk->private_data = jdp;
  536. add_disk(disk);
  537. set_disk_ro(disk, 1);
  538. }
  539. return 0;
  540. out:
  541. while (i--)
  542. put_disk(jsfd_disk[i]);
  543. return err;
  544. }
  545. MODULE_LICENSE("GPL");
  546. static int __init jsflash_init_module(void) {
  547. int rc;
  548. if ((rc = jsflash_init()) == 0) {
  549. jsfd_init();
  550. return 0;
  551. }
  552. return rc;
  553. }
  554. static void __exit jsflash_cleanup_module(void)
  555. {
  556. int i;
  557. for (i = 0; i < JSF_MAX; i++) {
  558. if ((i & JSF_PART_MASK) >= JSF_NPART) continue;
  559. del_gendisk(jsfd_disk[i]);
  560. blk_cleanup_queue(jsfd_disk[i]->queue);
  561. put_disk(jsfd_disk[i]);
  562. }
  563. if (jsf0.busy)
  564. printk("jsf0: cleaning busy unit\n");
  565. jsf0.base = 0;
  566. jsf0.busy = 0;
  567. misc_deregister(&jsf_dev);
  568. unregister_blkdev(JSFD_MAJOR, "jsfd");
  569. }
  570. module_init(jsflash_init_module);
  571. module_exit(jsflash_cleanup_module);