sharp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * MTD chip driver for pre-CFI Sharp flash chips
  3. *
  4. * Copyright 2000,2001 David A. Schleef <ds@schleef.org>
  5. * 2000,2001 Lineo, Inc.
  6. *
  7. * $Id: sharp.c,v 1.17 2005/11/29 14:28:28 gleixner Exp $
  8. *
  9. * Devices supported:
  10. * LH28F016SCT Symmetrical block flash memory, 2Mx8
  11. * LH28F008SCT Symmetrical block flash memory, 1Mx8
  12. *
  13. * Documentation:
  14. * http://www.sharpmeg.com/datasheets/memic/flashcmp/
  15. * http://www.sharpmeg.com/datasheets/memic/flashcmp/01symf/16m/016sctl9.pdf
  16. * 016sctl9.pdf
  17. *
  18. * Limitations:
  19. * This driver only supports 4x1 arrangement of chips.
  20. * Not tested on anything but PowerPC.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/sched.h>
  26. #include <linux/errno.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/mtd/map.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/cfi.h>
  31. #include <linux/delay.h>
  32. #include <linux/init.h>
  33. #include <linux/slab.h>
  34. #define CMD_RESET 0xffffffff
  35. #define CMD_READ_ID 0x90909090
  36. #define CMD_READ_STATUS 0x70707070
  37. #define CMD_CLEAR_STATUS 0x50505050
  38. #define CMD_BLOCK_ERASE_1 0x20202020
  39. #define CMD_BLOCK_ERASE_2 0xd0d0d0d0
  40. #define CMD_BYTE_WRITE 0x40404040
  41. #define CMD_SUSPEND 0xb0b0b0b0
  42. #define CMD_RESUME 0xd0d0d0d0
  43. #define CMD_SET_BLOCK_LOCK_1 0x60606060
  44. #define CMD_SET_BLOCK_LOCK_2 0x01010101
  45. #define CMD_SET_MASTER_LOCK_1 0x60606060
  46. #define CMD_SET_MASTER_LOCK_2 0xf1f1f1f1
  47. #define CMD_CLEAR_BLOCK_LOCKS_1 0x60606060
  48. #define CMD_CLEAR_BLOCK_LOCKS_2 0xd0d0d0d0
  49. #define SR_READY 0x80808080 // 1 = ready
  50. #define SR_ERASE_SUSPEND 0x40404040 // 1 = block erase suspended
  51. #define SR_ERROR_ERASE 0x20202020 // 1 = error in block erase or clear lock bits
  52. #define SR_ERROR_WRITE 0x10101010 // 1 = error in byte write or set lock bit
  53. #define SR_VPP 0x08080808 // 1 = Vpp is low
  54. #define SR_WRITE_SUSPEND 0x04040404 // 1 = byte write suspended
  55. #define SR_PROTECT 0x02020202 // 1 = lock bit set
  56. #define SR_RESERVED 0x01010101
  57. #define SR_ERRORS (SR_ERROR_ERASE|SR_ERROR_WRITE|SR_VPP|SR_PROTECT)
  58. /* Configuration options */
  59. #undef AUTOUNLOCK /* automatically unlocks blocks before erasing */
  60. static struct mtd_info *sharp_probe(struct map_info *);
  61. static int sharp_probe_map(struct map_info *map,struct mtd_info *mtd);
  62. static int sharp_read(struct mtd_info *mtd, loff_t from, size_t len,
  63. size_t *retlen, u_char *buf);
  64. static int sharp_write(struct mtd_info *mtd, loff_t from, size_t len,
  65. size_t *retlen, const u_char *buf);
  66. static int sharp_erase(struct mtd_info *mtd, struct erase_info *instr);
  67. static void sharp_sync(struct mtd_info *mtd);
  68. static int sharp_suspend(struct mtd_info *mtd);
  69. static void sharp_resume(struct mtd_info *mtd);
  70. static void sharp_destroy(struct mtd_info *mtd);
  71. static int sharp_write_oneword(struct map_info *map, struct flchip *chip,
  72. unsigned long adr, __u32 datum);
  73. static int sharp_erase_oneblock(struct map_info *map, struct flchip *chip,
  74. unsigned long adr);
  75. #ifdef AUTOUNLOCK
  76. static void sharp_unlock_oneblock(struct map_info *map, struct flchip *chip,
  77. unsigned long adr);
  78. #endif
  79. struct sharp_info{
  80. struct flchip *chip;
  81. int bogus;
  82. int chipshift;
  83. int numchips;
  84. struct flchip chips[1];
  85. };
  86. static void sharp_destroy(struct mtd_info *mtd);
  87. static struct mtd_chip_driver sharp_chipdrv = {
  88. .probe = sharp_probe,
  89. .destroy = sharp_destroy,
  90. .name = "sharp",
  91. .module = THIS_MODULE
  92. };
  93. static struct mtd_info *sharp_probe(struct map_info *map)
  94. {
  95. struct mtd_info *mtd = NULL;
  96. struct sharp_info *sharp = NULL;
  97. int width;
  98. mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
  99. if(!mtd)
  100. return NULL;
  101. sharp = kmalloc(sizeof(*sharp), GFP_KERNEL);
  102. if(!sharp) {
  103. kfree(mtd);
  104. return NULL;
  105. }
  106. memset(mtd, 0, sizeof(*mtd));
  107. width = sharp_probe_map(map,mtd);
  108. if(!width){
  109. kfree(mtd);
  110. kfree(sharp);
  111. return NULL;
  112. }
  113. mtd->priv = map;
  114. mtd->type = MTD_NORFLASH;
  115. mtd->erase = sharp_erase;
  116. mtd->read = sharp_read;
  117. mtd->write = sharp_write;
  118. mtd->sync = sharp_sync;
  119. mtd->suspend = sharp_suspend;
  120. mtd->resume = sharp_resume;
  121. mtd->flags = MTD_CAP_NORFLASH;
  122. mtd->name = map->name;
  123. memset(sharp, 0, sizeof(*sharp));
  124. sharp->chipshift = 23;
  125. sharp->numchips = 1;
  126. sharp->chips[0].start = 0;
  127. sharp->chips[0].state = FL_READY;
  128. sharp->chips[0].mutex = &sharp->chips[0]._spinlock;
  129. sharp->chips[0].word_write_time = 0;
  130. init_waitqueue_head(&sharp->chips[0].wq);
  131. spin_lock_init(&sharp->chips[0]._spinlock);
  132. map->fldrv = &sharp_chipdrv;
  133. map->fldrv_priv = sharp;
  134. __module_get(THIS_MODULE);
  135. return mtd;
  136. }
  137. static inline void sharp_send_cmd(struct map_info *map, unsigned long cmd, unsigned long adr)
  138. {
  139. map_word map_cmd;
  140. map_cmd.x[0] = cmd;
  141. map_write(map, map_cmd, adr);
  142. }
  143. static int sharp_probe_map(struct map_info *map,struct mtd_info *mtd)
  144. {
  145. map_word tmp, read0, read4;
  146. unsigned long base = 0;
  147. int width = 4;
  148. tmp = map_read(map, base+0);
  149. sharp_send_cmd(map, CMD_READ_ID, base+0);
  150. read0 = map_read(map, base+0);
  151. read4 = map_read(map, base+4);
  152. if(read0.x[0] == 0x89898989){
  153. printk("Looks like sharp flash\n");
  154. switch(read4.x[0]){
  155. case 0xaaaaaaaa:
  156. case 0xa0a0a0a0:
  157. /* aa - LH28F016SCT-L95 2Mx8, 32 64k blocks*/
  158. /* a0 - LH28F016SCT-Z4 2Mx8, 32 64k blocks*/
  159. mtd->erasesize = 0x10000 * width;
  160. mtd->size = 0x200000 * width;
  161. return width;
  162. case 0xa6a6a6a6:
  163. /* a6 - LH28F008SCT-L12 1Mx8, 16 64k blocks*/
  164. /* a6 - LH28F008SCR-L85 1Mx8, 16 64k blocks*/
  165. mtd->erasesize = 0x10000 * width;
  166. mtd->size = 0x100000 * width;
  167. return width;
  168. #if 0
  169. case 0x00000000: /* unknown */
  170. /* XX - LH28F004SCT 512kx8, 8 64k blocks*/
  171. mtd->erasesize = 0x10000 * width;
  172. mtd->size = 0x80000 * width;
  173. return width;
  174. #endif
  175. default:
  176. printk("Sort-of looks like sharp flash, 0x%08lx 0x%08lx\n",
  177. read0.x[0], read4.x[0]);
  178. }
  179. }else if((map_read(map, base+0).x[0] == CMD_READ_ID)){
  180. /* RAM, probably */
  181. printk("Looks like RAM\n");
  182. map_write(map, tmp, base+0);
  183. }else{
  184. printk("Doesn't look like sharp flash, 0x%08lx 0x%08lx\n",
  185. read0.x[0], read4.x[0]);
  186. }
  187. return 0;
  188. }
  189. /* This function returns with the chip->mutex lock held. */
  190. static int sharp_wait(struct map_info *map, struct flchip *chip)
  191. {
  192. int i;
  193. map_word status;
  194. unsigned long timeo = jiffies + HZ;
  195. DECLARE_WAITQUEUE(wait, current);
  196. int adr = 0;
  197. retry:
  198. spin_lock_bh(chip->mutex);
  199. switch(chip->state){
  200. case FL_READY:
  201. sharp_send_cmd(map, CMD_READ_STATUS, adr);
  202. chip->state = FL_STATUS;
  203. case FL_STATUS:
  204. for(i=0;i<100;i++){
  205. status = map_read(map, adr);
  206. if((status.x[0] & SR_READY)==SR_READY)
  207. break;
  208. udelay(1);
  209. }
  210. break;
  211. default:
  212. printk("Waiting for chip\n");
  213. set_current_state(TASK_INTERRUPTIBLE);
  214. add_wait_queue(&chip->wq, &wait);
  215. spin_unlock_bh(chip->mutex);
  216. schedule();
  217. remove_wait_queue(&chip->wq, &wait);
  218. if(signal_pending(current))
  219. return -EINTR;
  220. timeo = jiffies + HZ;
  221. goto retry;
  222. }
  223. sharp_send_cmd(map, CMD_RESET, adr);
  224. chip->state = FL_READY;
  225. return 0;
  226. }
  227. static void sharp_release(struct flchip *chip)
  228. {
  229. wake_up(&chip->wq);
  230. spin_unlock_bh(chip->mutex);
  231. }
  232. static int sharp_read(struct mtd_info *mtd, loff_t from, size_t len,
  233. size_t *retlen, u_char *buf)
  234. {
  235. struct map_info *map = mtd->priv;
  236. struct sharp_info *sharp = map->fldrv_priv;
  237. int chipnum;
  238. int ret = 0;
  239. int ofs = 0;
  240. chipnum = (from >> sharp->chipshift);
  241. ofs = from & ((1 << sharp->chipshift)-1);
  242. *retlen = 0;
  243. while(len){
  244. unsigned long thislen;
  245. if(chipnum>=sharp->numchips)
  246. break;
  247. thislen = len;
  248. if(ofs+thislen >= (1<<sharp->chipshift))
  249. thislen = (1<<sharp->chipshift) - ofs;
  250. ret = sharp_wait(map,&sharp->chips[chipnum]);
  251. if(ret<0)
  252. break;
  253. map_copy_from(map,buf,ofs,thislen);
  254. sharp_release(&sharp->chips[chipnum]);
  255. *retlen += thislen;
  256. len -= thislen;
  257. buf += thislen;
  258. ofs = 0;
  259. chipnum++;
  260. }
  261. return ret;
  262. }
  263. static int sharp_write(struct mtd_info *mtd, loff_t to, size_t len,
  264. size_t *retlen, const u_char *buf)
  265. {
  266. struct map_info *map = mtd->priv;
  267. struct sharp_info *sharp = map->fldrv_priv;
  268. int ret = 0;
  269. int i,j;
  270. int chipnum;
  271. unsigned long ofs;
  272. union { u32 l; unsigned char uc[4]; } tbuf;
  273. *retlen = 0;
  274. while(len){
  275. tbuf.l = 0xffffffff;
  276. chipnum = to >> sharp->chipshift;
  277. ofs = to & ((1<<sharp->chipshift)-1);
  278. j=0;
  279. for(i=ofs&3;i<4 && len;i++){
  280. tbuf.uc[i] = *buf;
  281. buf++;
  282. to++;
  283. len--;
  284. j++;
  285. }
  286. sharp_write_oneword(map, &sharp->chips[chipnum], ofs&~3, tbuf.l);
  287. if(ret<0)
  288. return ret;
  289. (*retlen)+=j;
  290. }
  291. return 0;
  292. }
  293. static int sharp_write_oneword(struct map_info *map, struct flchip *chip,
  294. unsigned long adr, __u32 datum)
  295. {
  296. int ret;
  297. int timeo;
  298. int try;
  299. int i;
  300. map_word data, status;
  301. status.x[0] = 0;
  302. ret = sharp_wait(map,chip);
  303. for(try=0;try<10;try++){
  304. sharp_send_cmd(map, CMD_BYTE_WRITE, adr);
  305. /* cpu_to_le32 -> hack to fix the writel be->le conversion */
  306. data.x[0] = cpu_to_le32(datum);
  307. map_write(map, data, adr);
  308. chip->state = FL_WRITING;
  309. timeo = jiffies + (HZ/2);
  310. sharp_send_cmd(map, CMD_READ_STATUS, adr);
  311. for(i=0;i<100;i++){
  312. status = map_read(map, adr);
  313. if((status.x[0] & SR_READY) == SR_READY)
  314. break;
  315. }
  316. if(i==100){
  317. printk("sharp: timed out writing\n");
  318. }
  319. if(!(status.x[0] & SR_ERRORS))
  320. break;
  321. printk("sharp: error writing byte at addr=%08lx status=%08lx\n", adr, status.x[0]);
  322. sharp_send_cmd(map, CMD_CLEAR_STATUS, adr);
  323. }
  324. sharp_send_cmd(map, CMD_RESET, adr);
  325. chip->state = FL_READY;
  326. wake_up(&chip->wq);
  327. spin_unlock_bh(chip->mutex);
  328. return 0;
  329. }
  330. static int sharp_erase(struct mtd_info *mtd, struct erase_info *instr)
  331. {
  332. struct map_info *map = mtd->priv;
  333. struct sharp_info *sharp = map->fldrv_priv;
  334. unsigned long adr,len;
  335. int chipnum, ret=0;
  336. //printk("sharp_erase()\n");
  337. if(instr->addr & (mtd->erasesize - 1))
  338. return -EINVAL;
  339. if(instr->len & (mtd->erasesize - 1))
  340. return -EINVAL;
  341. if(instr->len + instr->addr > mtd->size)
  342. return -EINVAL;
  343. chipnum = instr->addr >> sharp->chipshift;
  344. adr = instr->addr & ((1<<sharp->chipshift)-1);
  345. len = instr->len;
  346. while(len){
  347. ret = sharp_erase_oneblock(map, &sharp->chips[chipnum], adr);
  348. if(ret)return ret;
  349. adr += mtd->erasesize;
  350. len -= mtd->erasesize;
  351. if(adr >> sharp->chipshift){
  352. adr = 0;
  353. chipnum++;
  354. if(chipnum>=sharp->numchips)
  355. break;
  356. }
  357. }
  358. instr->state = MTD_ERASE_DONE;
  359. mtd_erase_callback(instr);
  360. return 0;
  361. }
  362. static int sharp_do_wait_for_ready(struct map_info *map, struct flchip *chip,
  363. unsigned long adr)
  364. {
  365. int ret;
  366. unsigned long timeo;
  367. map_word status;
  368. DECLARE_WAITQUEUE(wait, current);
  369. sharp_send_cmd(map, CMD_READ_STATUS, adr);
  370. status = map_read(map, adr);
  371. timeo = jiffies + HZ;
  372. while(time_before(jiffies, timeo)){
  373. sharp_send_cmd(map, CMD_READ_STATUS, adr);
  374. status = map_read(map, adr);
  375. if((status.x[0] & SR_READY)==SR_READY){
  376. ret = 0;
  377. goto out;
  378. }
  379. set_current_state(TASK_INTERRUPTIBLE);
  380. add_wait_queue(&chip->wq, &wait);
  381. //spin_unlock_bh(chip->mutex);
  382. schedule_timeout(1);
  383. schedule();
  384. remove_wait_queue(&chip->wq, &wait);
  385. //spin_lock_bh(chip->mutex);
  386. if (signal_pending(current)){
  387. ret = -EINTR;
  388. goto out;
  389. }
  390. }
  391. ret = -ETIME;
  392. out:
  393. return ret;
  394. }
  395. static int sharp_erase_oneblock(struct map_info *map, struct flchip *chip,
  396. unsigned long adr)
  397. {
  398. int ret;
  399. //int timeo;
  400. map_word status;
  401. //int i;
  402. //printk("sharp_erase_oneblock()\n");
  403. #ifdef AUTOUNLOCK
  404. /* This seems like a good place to do an unlock */
  405. sharp_unlock_oneblock(map,chip,adr);
  406. #endif
  407. sharp_send_cmd(map, CMD_BLOCK_ERASE_1, adr);
  408. sharp_send_cmd(map, CMD_BLOCK_ERASE_2, adr);
  409. chip->state = FL_ERASING;
  410. ret = sharp_do_wait_for_ready(map,chip,adr);
  411. if(ret<0)return ret;
  412. sharp_send_cmd(map, CMD_READ_STATUS, adr);
  413. status = map_read(map, adr);
  414. if(!(status.x[0] & SR_ERRORS)){
  415. sharp_send_cmd(map, CMD_RESET, adr);
  416. chip->state = FL_READY;
  417. //spin_unlock_bh(chip->mutex);
  418. return 0;
  419. }
  420. printk("sharp: error erasing block at addr=%08lx status=%08lx\n", adr, status.x[0]);
  421. sharp_send_cmd(map, CMD_CLEAR_STATUS, adr);
  422. //spin_unlock_bh(chip->mutex);
  423. return -EIO;
  424. }
  425. #ifdef AUTOUNLOCK
  426. static void sharp_unlock_oneblock(struct map_info *map, struct flchip *chip,
  427. unsigned long adr)
  428. {
  429. int i;
  430. map_word status;
  431. sharp_send_cmd(map, CMD_CLEAR_BLOCK_LOCKS_1, adr);
  432. sharp_send_cmd(map, CMD_CLEAR_BLOCK_LOCKS_2, adr);
  433. udelay(100);
  434. status = map_read(map, adr);
  435. printk("status=%08lx\n", status.x[0]);
  436. for(i=0;i<1000;i++){
  437. //sharp_send_cmd(map, CMD_READ_STATUS, adr);
  438. status = map_read(map, adr);
  439. if((status.x[0] & SR_READY) == SR_READY)
  440. break;
  441. udelay(100);
  442. }
  443. if(i==1000){
  444. printk("sharp: timed out unlocking block\n");
  445. }
  446. if(!(status.x[0] & SR_ERRORS)){
  447. sharp_send_cmd(map, CMD_RESET, adr);
  448. chip->state = FL_READY;
  449. return;
  450. }
  451. printk("sharp: error unlocking block at addr=%08lx status=%08lx\n", adr, status.x[0]);
  452. sharp_send_cmd(map, CMD_CLEAR_STATUS, adr);
  453. }
  454. #endif
  455. static void sharp_sync(struct mtd_info *mtd)
  456. {
  457. //printk("sharp_sync()\n");
  458. }
  459. static int sharp_suspend(struct mtd_info *mtd)
  460. {
  461. printk("sharp_suspend()\n");
  462. return -EINVAL;
  463. }
  464. static void sharp_resume(struct mtd_info *mtd)
  465. {
  466. printk("sharp_resume()\n");
  467. }
  468. static void sharp_destroy(struct mtd_info *mtd)
  469. {
  470. printk("sharp_destroy()\n");
  471. }
  472. static int __init sharp_probe_init(void)
  473. {
  474. printk("MTD Sharp chip driver <ds@lineo.com>\n");
  475. register_mtd_chip_driver(&sharp_chipdrv);
  476. return 0;
  477. }
  478. static void __exit sharp_probe_exit(void)
  479. {
  480. unregister_mtd_chip_driver(&sharp_chipdrv);
  481. }
  482. module_init(sharp_probe_init);
  483. module_exit(sharp_probe_exit);
  484. MODULE_LICENSE("GPL");
  485. MODULE_AUTHOR("David Schleef <ds@schleef.org>");
  486. MODULE_DESCRIPTION("Old MTD chip driver for pre-CFI Sharp flash chips");