nwflash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * Flash memory interface rev.5 driver for the Intel
  3. * Flash chips used on the NetWinder.
  4. *
  5. * 20/08/2000 RMK use __ioremap to map flash into virtual memory
  6. * make a few more places use "volatile"
  7. * 22/05/2001 RMK - Lock read against write
  8. * - merge printk level changes (with mods) from Alan Cox.
  9. * - use *ppos as the file position, not file->f_pos.
  10. * - fix check for out of range pos and r/w size
  11. *
  12. * Please note that we are tampering with the only flash chip in the
  13. * machine, which contains the bootup code. We therefore have the
  14. * power to convert these machines into doorstops...
  15. */
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/fs.h>
  19. #include <linux/errno.h>
  20. #include <linux/mm.h>
  21. #include <linux/delay.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/miscdevice.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/rwsem.h>
  26. #include <linux/init.h>
  27. #include <linux/mutex.h>
  28. #include <linux/jiffies.h>
  29. #include <asm/hardware/dec21285.h>
  30. #include <asm/io.h>
  31. #include <asm/mach-types.h>
  32. #include <asm/uaccess.h>
  33. /*****************************************************************************/
  34. #include <asm/nwflash.h>
  35. #define NWFLASH_VERSION "6.4"
  36. static DEFINE_MUTEX(flash_mutex);
  37. static void kick_open(void);
  38. static int get_flash_id(void);
  39. static int erase_block(int nBlock);
  40. static int write_block(unsigned long p, const char __user *buf, int count);
  41. #define KFLASH_SIZE 1024*1024 //1 Meg
  42. #define KFLASH_SIZE4 4*1024*1024 //4 Meg
  43. #define KFLASH_ID 0x89A6 //Intel flash
  44. #define KFLASH_ID4 0xB0D4 //Intel flash 4Meg
  45. static bool flashdebug; //if set - we will display progress msgs
  46. static int gbWriteEnable;
  47. static int gbWriteBase64Enable;
  48. static volatile unsigned char *FLASH_BASE;
  49. static int gbFlashSize = KFLASH_SIZE;
  50. static DEFINE_MUTEX(nwflash_mutex);
  51. static int get_flash_id(void)
  52. {
  53. volatile unsigned int c1, c2;
  54. /*
  55. * try to get flash chip ID
  56. */
  57. kick_open();
  58. c2 = inb(0x80);
  59. *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x90;
  60. udelay(15);
  61. c1 = *(volatile unsigned char *) FLASH_BASE;
  62. c2 = inb(0x80);
  63. /*
  64. * on 4 Meg flash the second byte is actually at offset 2...
  65. */
  66. if (c1 == 0xB0)
  67. c2 = *(volatile unsigned char *) (FLASH_BASE + 2);
  68. else
  69. c2 = *(volatile unsigned char *) (FLASH_BASE + 1);
  70. c2 += (c1 << 8);
  71. /*
  72. * set it back to read mode
  73. */
  74. *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0xFF;
  75. if (c2 == KFLASH_ID4)
  76. gbFlashSize = KFLASH_SIZE4;
  77. return c2;
  78. }
  79. static long flash_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  80. {
  81. mutex_lock(&flash_mutex);
  82. switch (cmd) {
  83. case CMD_WRITE_DISABLE:
  84. gbWriteBase64Enable = 0;
  85. gbWriteEnable = 0;
  86. break;
  87. case CMD_WRITE_ENABLE:
  88. gbWriteEnable = 1;
  89. break;
  90. case CMD_WRITE_BASE64K_ENABLE:
  91. gbWriteBase64Enable = 1;
  92. break;
  93. default:
  94. gbWriteBase64Enable = 0;
  95. gbWriteEnable = 0;
  96. mutex_unlock(&flash_mutex);
  97. return -EINVAL;
  98. }
  99. mutex_unlock(&flash_mutex);
  100. return 0;
  101. }
  102. static ssize_t flash_read(struct file *file, char __user *buf, size_t size,
  103. loff_t *ppos)
  104. {
  105. ssize_t ret;
  106. if (flashdebug)
  107. printk(KERN_DEBUG "flash_read: flash_read: offset=0x%llx, "
  108. "buffer=%p, count=0x%zx.\n", *ppos, buf, size);
  109. /*
  110. * We now lock against reads and writes. --rmk
  111. */
  112. if (mutex_lock_interruptible(&nwflash_mutex))
  113. return -ERESTARTSYS;
  114. ret = simple_read_from_buffer(buf, size, ppos, (void *)FLASH_BASE, gbFlashSize);
  115. mutex_unlock(&nwflash_mutex);
  116. return ret;
  117. }
  118. static ssize_t flash_write(struct file *file, const char __user *buf,
  119. size_t size, loff_t * ppos)
  120. {
  121. unsigned long p = *ppos;
  122. unsigned int count = size;
  123. int written;
  124. int nBlock, temp, rc;
  125. int i, j;
  126. if (flashdebug)
  127. printk("flash_write: offset=0x%lX, buffer=0x%p, count=0x%X.\n",
  128. p, buf, count);
  129. if (!gbWriteEnable)
  130. return -EINVAL;
  131. if (p < 64 * 1024 && (!gbWriteBase64Enable))
  132. return -EINVAL;
  133. /*
  134. * check for out of range pos or count
  135. */
  136. if (p >= gbFlashSize)
  137. return count ? -ENXIO : 0;
  138. if (count > gbFlashSize - p)
  139. count = gbFlashSize - p;
  140. if (!access_ok(VERIFY_READ, buf, count))
  141. return -EFAULT;
  142. /*
  143. * We now lock against reads and writes. --rmk
  144. */
  145. if (mutex_lock_interruptible(&nwflash_mutex))
  146. return -ERESTARTSYS;
  147. written = 0;
  148. nBlock = (int) p >> 16; //block # of 64K bytes
  149. /*
  150. * # of 64K blocks to erase and write
  151. */
  152. temp = ((int) (p + count) >> 16) - nBlock + 1;
  153. /*
  154. * write ends at exactly 64k boundary?
  155. */
  156. if (((int) (p + count) & 0xFFFF) == 0)
  157. temp -= 1;
  158. if (flashdebug)
  159. printk(KERN_DEBUG "flash_write: writing %d block(s) "
  160. "starting at %d.\n", temp, nBlock);
  161. for (; temp; temp--, nBlock++) {
  162. if (flashdebug)
  163. printk(KERN_DEBUG "flash_write: erasing block %d.\n", nBlock);
  164. /*
  165. * first we have to erase the block(s), where we will write...
  166. */
  167. i = 0;
  168. j = 0;
  169. RetryBlock:
  170. do {
  171. rc = erase_block(nBlock);
  172. i++;
  173. } while (rc && i < 10);
  174. if (rc) {
  175. printk(KERN_ERR "flash_write: erase error %x\n", rc);
  176. break;
  177. }
  178. if (flashdebug)
  179. printk(KERN_DEBUG "flash_write: writing offset %lX, "
  180. "from buf %p, bytes left %X.\n", p, buf,
  181. count - written);
  182. /*
  183. * write_block will limit write to space left in this block
  184. */
  185. rc = write_block(p, buf, count - written);
  186. j++;
  187. /*
  188. * if somehow write verify failed? Can't happen??
  189. */
  190. if (!rc) {
  191. /*
  192. * retry up to 10 times
  193. */
  194. if (j < 10)
  195. goto RetryBlock;
  196. else
  197. /*
  198. * else quit with error...
  199. */
  200. rc = -1;
  201. }
  202. if (rc < 0) {
  203. printk(KERN_ERR "flash_write: write error %X\n", rc);
  204. break;
  205. }
  206. p += rc;
  207. buf += rc;
  208. written += rc;
  209. *ppos += rc;
  210. if (flashdebug)
  211. printk(KERN_DEBUG "flash_write: written 0x%X bytes OK.\n", written);
  212. }
  213. mutex_unlock(&nwflash_mutex);
  214. return written;
  215. }
  216. /*
  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).
  220. *
  221. * also note that seeking relative to the "end of file" isn't supported:
  222. * it has no meaning, so it returns -EINVAL.
  223. */
  224. static loff_t flash_llseek(struct file *file, loff_t offset, int orig)
  225. {
  226. loff_t ret;
  227. mutex_lock(&flash_mutex);
  228. if (flashdebug)
  229. printk(KERN_DEBUG "flash_llseek: offset=0x%X, orig=0x%X.\n",
  230. (unsigned int) offset, orig);
  231. switch (orig) {
  232. case 0:
  233. if (offset < 0) {
  234. ret = -EINVAL;
  235. break;
  236. }
  237. if ((unsigned int) offset > gbFlashSize) {
  238. ret = -EINVAL;
  239. break;
  240. }
  241. file->f_pos = (unsigned int) offset;
  242. ret = file->f_pos;
  243. break;
  244. case 1:
  245. if ((file->f_pos + offset) > gbFlashSize) {
  246. ret = -EINVAL;
  247. break;
  248. }
  249. if ((file->f_pos + offset) < 0) {
  250. ret = -EINVAL;
  251. break;
  252. }
  253. file->f_pos += offset;
  254. ret = file->f_pos;
  255. break;
  256. default:
  257. ret = -EINVAL;
  258. }
  259. mutex_unlock(&flash_mutex);
  260. return ret;
  261. }
  262. /*
  263. * assume that main Write routine did the parameter checking...
  264. * so just go ahead and erase, what requested!
  265. */
  266. static int erase_block(int nBlock)
  267. {
  268. volatile unsigned int c1;
  269. volatile unsigned char *pWritePtr;
  270. unsigned long timeout;
  271. int temp, temp1;
  272. /*
  273. * reset footbridge to the correct offset 0 (...0..3)
  274. */
  275. *CSR_ROMWRITEREG = 0;
  276. /*
  277. * dummy ROM read
  278. */
  279. c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
  280. kick_open();
  281. /*
  282. * reset status if old errors
  283. */
  284. *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
  285. /*
  286. * erase a block...
  287. * aim at the middle of a current block...
  288. */
  289. pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + 0x8000 + (nBlock << 16)));
  290. /*
  291. * dummy read
  292. */
  293. c1 = *pWritePtr;
  294. kick_open();
  295. /*
  296. * erase
  297. */
  298. *(volatile unsigned char *) pWritePtr = 0x20;
  299. /*
  300. * confirm
  301. */
  302. *(volatile unsigned char *) pWritePtr = 0xD0;
  303. /*
  304. * wait 10 ms
  305. */
  306. msleep(10);
  307. /*
  308. * wait while erasing in process (up to 10 sec)
  309. */
  310. timeout = jiffies + 10 * HZ;
  311. c1 = 0;
  312. while (!(c1 & 0x80) && time_before(jiffies, timeout)) {
  313. msleep(10);
  314. /*
  315. * read any address
  316. */
  317. c1 = *(volatile unsigned char *) (pWritePtr);
  318. // printk("Flash_erase: status=%X.\n",c1);
  319. }
  320. /*
  321. * set flash for normal read access
  322. */
  323. kick_open();
  324. // *(volatile unsigned char*)(FLASH_BASE+0x8000) = 0xFF;
  325. *(volatile unsigned char *) pWritePtr = 0xFF; //back to normal operation
  326. /*
  327. * check if erase errors were reported
  328. */
  329. if (c1 & 0x20) {
  330. printk(KERN_ERR "flash_erase: err at %p\n", pWritePtr);
  331. /*
  332. * reset error
  333. */
  334. *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
  335. return -2;
  336. }
  337. /*
  338. * just to make sure - verify if erased OK...
  339. */
  340. msleep(10);
  341. pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + (nBlock << 16)));
  342. for (temp = 0; temp < 16 * 1024; temp++, pWritePtr += 4) {
  343. if ((temp1 = *(volatile unsigned int *) pWritePtr) != 0xFFFFFFFF) {
  344. printk(KERN_ERR "flash_erase: verify err at %p = %X\n",
  345. pWritePtr, temp1);
  346. return -1;
  347. }
  348. }
  349. return 0;
  350. }
  351. /*
  352. * write_block will limit number of bytes written to the space in this block
  353. */
  354. static int write_block(unsigned long p, const char __user *buf, int count)
  355. {
  356. volatile unsigned int c1;
  357. volatile unsigned int c2;
  358. unsigned char *pWritePtr;
  359. unsigned int uAddress;
  360. unsigned int offset;
  361. unsigned long timeout;
  362. unsigned long timeout1;
  363. pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + p));
  364. /*
  365. * check if write will end in this block....
  366. */
  367. offset = p & 0xFFFF;
  368. if (offset + count > 0x10000)
  369. count = 0x10000 - offset;
  370. /*
  371. * wait up to 30 sec for this block
  372. */
  373. timeout = jiffies + 30 * HZ;
  374. for (offset = 0; offset < count; offset++, pWritePtr++) {
  375. uAddress = (unsigned int) pWritePtr;
  376. uAddress &= 0xFFFFFFFC;
  377. if (__get_user(c2, buf + offset))
  378. return -EFAULT;
  379. WriteRetry:
  380. /*
  381. * dummy read
  382. */
  383. c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
  384. /*
  385. * kick open the write gate
  386. */
  387. kick_open();
  388. /*
  389. * program footbridge to the correct offset...0..3
  390. */
  391. *CSR_ROMWRITEREG = (unsigned int) pWritePtr & 3;
  392. /*
  393. * write cmd
  394. */
  395. *(volatile unsigned char *) (uAddress) = 0x40;
  396. /*
  397. * data to write
  398. */
  399. *(volatile unsigned char *) (uAddress) = c2;
  400. /*
  401. * get status
  402. */
  403. *(volatile unsigned char *) (FLASH_BASE + 0x10000) = 0x70;
  404. c1 = 0;
  405. /*
  406. * wait up to 1 sec for this byte
  407. */
  408. timeout1 = jiffies + 1 * HZ;
  409. /*
  410. * while not ready...
  411. */
  412. while (!(c1 & 0x80) && time_before(jiffies, timeout1))
  413. c1 = *(volatile unsigned char *) (FLASH_BASE + 0x8000);
  414. /*
  415. * if timeout getting status
  416. */
  417. if (time_after_eq(jiffies, timeout1)) {
  418. kick_open();
  419. /*
  420. * reset err
  421. */
  422. *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
  423. goto WriteRetry;
  424. }
  425. /*
  426. * switch on read access, as a default flash operation mode
  427. */
  428. kick_open();
  429. /*
  430. * read access
  431. */
  432. *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0xFF;
  433. /*
  434. * if hardware reports an error writing, and not timeout -
  435. * reset the chip and retry
  436. */
  437. if (c1 & 0x10) {
  438. kick_open();
  439. /*
  440. * reset err
  441. */
  442. *(volatile unsigned char *) (FLASH_BASE + 0x8000) = 0x50;
  443. /*
  444. * before timeout?
  445. */
  446. if (time_before(jiffies, timeout)) {
  447. if (flashdebug)
  448. printk(KERN_DEBUG "write_block: Retrying write at 0x%X)n",
  449. pWritePtr - FLASH_BASE);
  450. /*
  451. * wait couple ms
  452. */
  453. msleep(10);
  454. goto WriteRetry;
  455. } else {
  456. printk(KERN_ERR "write_block: timeout at 0x%X\n",
  457. pWritePtr - FLASH_BASE);
  458. /*
  459. * return error -2
  460. */
  461. return -2;
  462. }
  463. }
  464. }
  465. msleep(10);
  466. pWritePtr = (unsigned char *) ((unsigned int) (FLASH_BASE + p));
  467. for (offset = 0; offset < count; offset++) {
  468. char c, c1;
  469. if (__get_user(c, buf))
  470. return -EFAULT;
  471. buf++;
  472. if ((c1 = *pWritePtr++) != c) {
  473. printk(KERN_ERR "write_block: verify error at 0x%X (%02X!=%02X)\n",
  474. pWritePtr - FLASH_BASE, c1, c);
  475. return 0;
  476. }
  477. }
  478. return count;
  479. }
  480. static void kick_open(void)
  481. {
  482. unsigned long flags;
  483. /*
  484. * we want to write a bit pattern XXX1 to Xilinx to enable
  485. * the write gate, which will be open for about the next 2ms.
  486. */
  487. raw_spin_lock_irqsave(&nw_gpio_lock, flags);
  488. nw_cpld_modify(CPLD_FLASH_WR_ENABLE, CPLD_FLASH_WR_ENABLE);
  489. raw_spin_unlock_irqrestore(&nw_gpio_lock, flags);
  490. /*
  491. * let the ISA bus to catch on...
  492. */
  493. udelay(25);
  494. }
  495. static const struct file_operations flash_fops =
  496. {
  497. .owner = THIS_MODULE,
  498. .llseek = flash_llseek,
  499. .read = flash_read,
  500. .write = flash_write,
  501. .unlocked_ioctl = flash_ioctl,
  502. };
  503. static struct miscdevice flash_miscdev =
  504. {
  505. FLASH_MINOR,
  506. "nwflash",
  507. &flash_fops
  508. };
  509. static int __init nwflash_init(void)
  510. {
  511. int ret = -ENODEV;
  512. if (machine_is_netwinder()) {
  513. int id;
  514. FLASH_BASE = ioremap(DC21285_FLASH, KFLASH_SIZE4);
  515. if (!FLASH_BASE)
  516. goto out;
  517. id = get_flash_id();
  518. if ((id != KFLASH_ID) && (id != KFLASH_ID4)) {
  519. ret = -ENXIO;
  520. iounmap((void *)FLASH_BASE);
  521. printk("Flash: incorrect ID 0x%04X.\n", id);
  522. goto out;
  523. }
  524. printk("Flash ROM driver v.%s, flash device ID 0x%04X, size %d Mb.\n",
  525. NWFLASH_VERSION, id, gbFlashSize / (1024 * 1024));
  526. ret = misc_register(&flash_miscdev);
  527. if (ret < 0) {
  528. iounmap((void *)FLASH_BASE);
  529. }
  530. }
  531. out:
  532. return ret;
  533. }
  534. static void __exit nwflash_exit(void)
  535. {
  536. misc_deregister(&flash_miscdev);
  537. iounmap((void *)FLASH_BASE);
  538. }
  539. MODULE_LICENSE("GPL");
  540. module_param(flashdebug, bool, 0644);
  541. module_init(nwflash_init);
  542. module_exit(nwflash_exit);