sharp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. 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. struct mtd_info *sharp_probe(struct map_info *map);
  87. static void sharp_destroy(struct mtd_info *mtd);
  88. static struct mtd_chip_driver sharp_chipdrv = {
  89. .probe = sharp_probe,
  90. .destroy = sharp_destroy,
  91. .name = "sharp",
  92. .module = THIS_MODULE
  93. };
  94. struct mtd_info *sharp_probe(struct map_info *map)
  95. {
  96. struct mtd_info *mtd = NULL;
  97. struct sharp_info *sharp = NULL;
  98. int width;
  99. mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
  100. if(!mtd)
  101. return NULL;
  102. sharp = kmalloc(sizeof(*sharp), GFP_KERNEL);
  103. if(!sharp) {
  104. kfree(mtd);
  105. return NULL;
  106. }
  107. memset(mtd, 0, sizeof(*mtd));
  108. width = sharp_probe_map(map,mtd);
  109. if(!width){
  110. kfree(mtd);
  111. kfree(sharp);
  112. return NULL;
  113. }
  114. mtd->priv = map;
  115. mtd->type = MTD_NORFLASH;
  116. mtd->erase = sharp_erase;
  117. mtd->read = sharp_read;
  118. mtd->write = sharp_write;
  119. mtd->sync = sharp_sync;
  120. mtd->suspend = sharp_suspend;
  121. mtd->resume = sharp_resume;
  122. mtd->flags = MTD_CAP_NORFLASH;
  123. mtd->name = map->name;
  124. memset(sharp, 0, sizeof(*sharp));
  125. sharp->chipshift = 23;
  126. sharp->numchips = 1;
  127. sharp->chips[0].start = 0;
  128. sharp->chips[0].state = FL_READY;
  129. sharp->chips[0].mutex = &sharp->chips[0]._spinlock;
  130. sharp->chips[0].word_write_time = 0;
  131. init_waitqueue_head(&sharp->chips[0].wq);
  132. spin_lock_init(&sharp->chips[0]._spinlock);
  133. map->fldrv = &sharp_chipdrv;
  134. map->fldrv_priv = sharp;
  135. __module_get(THIS_MODULE);
  136. return mtd;
  137. }
  138. static inline void sharp_send_cmd(struct map_info *map, unsigned long cmd, unsigned long adr)
  139. {
  140. map_word map_cmd;
  141. map_cmd.x[0] = cmd;
  142. map_write(map, map_cmd, adr);
  143. }
  144. static int sharp_probe_map(struct map_info *map,struct mtd_info *mtd)
  145. {
  146. map_word tmp, read0, read4;
  147. unsigned long base = 0;
  148. int width = 4;
  149. tmp = map_read(map, base+0);
  150. sharp_send_cmd(map, CMD_READ_ID, base+0);
  151. read0 = map_read(map, base+0);
  152. read4 = map_read(map, base+4);
  153. if(read0.x[0] == 0x89898989){
  154. printk("Looks like sharp flash\n");
  155. switch(read4.x[0]){
  156. case 0xaaaaaaaa:
  157. case 0xa0a0a0a0:
  158. /* aa - LH28F016SCT-L95 2Mx8, 32 64k blocks*/
  159. /* a0 - LH28F016SCT-Z4 2Mx8, 32 64k blocks*/
  160. mtd->erasesize = 0x10000 * width;
  161. mtd->size = 0x200000 * width;
  162. return width;
  163. case 0xa6a6a6a6:
  164. /* a6 - LH28F008SCT-L12 1Mx8, 16 64k blocks*/
  165. /* a6 - LH28F008SCR-L85 1Mx8, 16 64k blocks*/
  166. mtd->erasesize = 0x10000 * width;
  167. mtd->size = 0x100000 * width;
  168. return width;
  169. #if 0
  170. case 0x00000000: /* unknown */
  171. /* XX - LH28F004SCT 512kx8, 8 64k blocks*/
  172. mtd->erasesize = 0x10000 * width;
  173. mtd->size = 0x80000 * width;
  174. return width;
  175. #endif
  176. default:
  177. printk("Sort-of looks like sharp flash, 0x%08lx 0x%08lx\n",
  178. read0.x[0], read4.x[0]);
  179. }
  180. }else if((map_read(map, base+0).x[0] == CMD_READ_ID)){
  181. /* RAM, probably */
  182. printk("Looks like RAM\n");
  183. map_write(map, tmp, base+0);
  184. }else{
  185. printk("Doesn't look like sharp flash, 0x%08lx 0x%08lx\n",
  186. read0.x[0], read4.x[0]);
  187. }
  188. return 0;
  189. }
  190. /* This function returns with the chip->mutex lock held. */
  191. static int sharp_wait(struct map_info *map, struct flchip *chip)
  192. {
  193. int i;
  194. map_word status;
  195. unsigned long timeo = jiffies + HZ;
  196. DECLARE_WAITQUEUE(wait, current);
  197. int adr = 0;
  198. retry:
  199. spin_lock_bh(chip->mutex);
  200. switch(chip->state){
  201. case FL_READY:
  202. sharp_send_cmd(map, CMD_READ_STATUS, adr);
  203. chip->state = FL_STATUS;
  204. case FL_STATUS:
  205. for(i=0;i<100;i++){
  206. status = map_read(map, adr);
  207. if((status.x[0] & SR_READY)==SR_READY)
  208. break;
  209. udelay(1);
  210. }
  211. break;
  212. default:
  213. printk("Waiting for chip\n");
  214. set_current_state(TASK_INTERRUPTIBLE);
  215. add_wait_queue(&chip->wq, &wait);
  216. spin_unlock_bh(chip->mutex);
  217. schedule();
  218. remove_wait_queue(&chip->wq, &wait);
  219. if(signal_pending(current))
  220. return -EINTR;
  221. timeo = jiffies + HZ;
  222. goto retry;
  223. }
  224. sharp_send_cmd(map, CMD_RESET, adr);
  225. chip->state = FL_READY;
  226. return 0;
  227. }
  228. static void sharp_release(struct flchip *chip)
  229. {
  230. wake_up(&chip->wq);
  231. spin_unlock_bh(chip->mutex);
  232. }
  233. static int sharp_read(struct mtd_info *mtd, loff_t from, size_t len,
  234. size_t *retlen, u_char *buf)
  235. {
  236. struct map_info *map = mtd->priv;
  237. struct sharp_info *sharp = map->fldrv_priv;
  238. int chipnum;
  239. int ret = 0;
  240. int ofs = 0;
  241. chipnum = (from >> sharp->chipshift);
  242. ofs = from & ((1 << sharp->chipshift)-1);
  243. *retlen = 0;
  244. while(len){
  245. unsigned long thislen;
  246. if(chipnum>=sharp->numchips)
  247. break;
  248. thislen = len;
  249. if(ofs+thislen >= (1<<sharp->chipshift))
  250. thislen = (1<<sharp->chipshift) - ofs;
  251. ret = sharp_wait(map,&sharp->chips[chipnum]);
  252. if(ret<0)
  253. break;
  254. map_copy_from(map,buf,ofs,thislen);
  255. sharp_release(&sharp->chips[chipnum]);
  256. *retlen += thislen;
  257. len -= thislen;
  258. buf += thislen;
  259. ofs = 0;
  260. chipnum++;
  261. }
  262. return ret;
  263. }
  264. static int sharp_write(struct mtd_info *mtd, loff_t to, size_t len,
  265. size_t *retlen, const u_char *buf)
  266. {
  267. struct map_info *map = mtd->priv;
  268. struct sharp_info *sharp = map->fldrv_priv;
  269. int ret = 0;
  270. int i,j;
  271. int chipnum;
  272. unsigned long ofs;
  273. union { u32 l; unsigned char uc[4]; } tbuf;
  274. *retlen = 0;
  275. while(len){
  276. tbuf.l = 0xffffffff;
  277. chipnum = to >> sharp->chipshift;
  278. ofs = to & ((1<<sharp->chipshift)-1);
  279. j=0;
  280. for(i=ofs&3;i<4 && len;i++){
  281. tbuf.uc[i] = *buf;
  282. buf++;
  283. to++;
  284. len--;
  285. j++;
  286. }
  287. sharp_write_oneword(map, &sharp->chips[chipnum], ofs&~3, tbuf.l);
  288. if(ret<0)
  289. return ret;
  290. (*retlen)+=j;
  291. }
  292. return 0;
  293. }
  294. static int sharp_write_oneword(struct map_info *map, struct flchip *chip,
  295. unsigned long adr, __u32 datum)
  296. {
  297. int ret;
  298. int timeo;
  299. int try;
  300. int i;
  301. map_word data, status;
  302. status.x[0] = 0;
  303. ret = sharp_wait(map,chip);
  304. for(try=0;try<10;try++){
  305. sharp_send_cmd(map, CMD_BYTE_WRITE, adr);
  306. /* cpu_to_le32 -> hack to fix the writel be->le conversion */
  307. data.x[0] = cpu_to_le32(datum);
  308. map_write(map, data, adr);
  309. chip->state = FL_WRITING;
  310. timeo = jiffies + (HZ/2);
  311. sharp_send_cmd(map, CMD_READ_STATUS, adr);
  312. for(i=0;i<100;i++){
  313. status = map_read(map, adr);
  314. if((status.x[0] & SR_READY) == SR_READY)
  315. break;
  316. }
  317. if(i==100){
  318. printk("sharp: timed out writing\n");
  319. }
  320. if(!(status.x[0] & SR_ERRORS))
  321. break;
  322. printk("sharp: error writing byte at addr=%08lx status=%08lx\n", adr, status.x[0]);
  323. sharp_send_cmd(map, CMD_CLEAR_STATUS, adr);
  324. }
  325. sharp_send_cmd(map, CMD_RESET, adr);
  326. chip->state = FL_READY;
  327. wake_up(&chip->wq);
  328. spin_unlock_bh(chip->mutex);
  329. return 0;
  330. }
  331. static int sharp_erase(struct mtd_info *mtd, struct erase_info *instr)
  332. {
  333. struct map_info *map = mtd->priv;
  334. struct sharp_info *sharp = map->fldrv_priv;
  335. unsigned long adr,len;
  336. int chipnum, ret=0;
  337. //printk("sharp_erase()\n");
  338. if(instr->addr & (mtd->erasesize - 1))
  339. return -EINVAL;
  340. if(instr->len & (mtd->erasesize - 1))
  341. return -EINVAL;
  342. if(instr->len + instr->addr > mtd->size)
  343. return -EINVAL;
  344. chipnum = instr->addr >> sharp->chipshift;
  345. adr = instr->addr & ((1<<sharp->chipshift)-1);
  346. len = instr->len;
  347. while(len){
  348. ret = sharp_erase_oneblock(map, &sharp->chips[chipnum], adr);
  349. if(ret)return ret;
  350. adr += mtd->erasesize;
  351. len -= mtd->erasesize;
  352. if(adr >> sharp->chipshift){
  353. adr = 0;
  354. chipnum++;
  355. if(chipnum>=sharp->numchips)
  356. break;
  357. }
  358. }
  359. instr->state = MTD_ERASE_DONE;
  360. mtd_erase_callback(instr);
  361. return 0;
  362. }
  363. static int sharp_do_wait_for_ready(struct map_info *map, struct flchip *chip,
  364. unsigned long adr)
  365. {
  366. int ret;
  367. unsigned long timeo;
  368. map_word status;
  369. DECLARE_WAITQUEUE(wait, current);
  370. sharp_send_cmd(map, CMD_READ_STATUS, adr);
  371. status = map_read(map, adr);
  372. timeo = jiffies + HZ;
  373. while(time_before(jiffies, timeo)){
  374. sharp_send_cmd(map, CMD_READ_STATUS, adr);
  375. status = map_read(map, adr);
  376. if((status.x[0] & SR_READY)==SR_READY){
  377. ret = 0;
  378. goto out;
  379. }
  380. set_current_state(TASK_INTERRUPTIBLE);
  381. add_wait_queue(&chip->wq, &wait);
  382. //spin_unlock_bh(chip->mutex);
  383. schedule_timeout(1);
  384. schedule();
  385. remove_wait_queue(&chip->wq, &wait);
  386. //spin_lock_bh(chip->mutex);
  387. if (signal_pending(current)){
  388. ret = -EINTR;
  389. goto out;
  390. }
  391. }
  392. ret = -ETIME;
  393. out:
  394. return ret;
  395. }
  396. static int sharp_erase_oneblock(struct map_info *map, struct flchip *chip,
  397. unsigned long adr)
  398. {
  399. int ret;
  400. //int timeo;
  401. map_word status;
  402. //int i;
  403. //printk("sharp_erase_oneblock()\n");
  404. #ifdef AUTOUNLOCK
  405. /* This seems like a good place to do an unlock */
  406. sharp_unlock_oneblock(map,chip,adr);
  407. #endif
  408. sharp_send_cmd(map, CMD_BLOCK_ERASE_1, adr);
  409. sharp_send_cmd(map, CMD_BLOCK_ERASE_2, adr);
  410. chip->state = FL_ERASING;
  411. ret = sharp_do_wait_for_ready(map,chip,adr);
  412. if(ret<0)return ret;
  413. sharp_send_cmd(map, CMD_READ_STATUS, adr);
  414. status = map_read(map, adr);
  415. if(!(status.x[0] & SR_ERRORS)){
  416. sharp_send_cmd(map, CMD_RESET, adr);
  417. chip->state = FL_READY;
  418. //spin_unlock_bh(chip->mutex);
  419. return 0;
  420. }
  421. printk("sharp: error erasing block at addr=%08lx status=%08lx\n", adr, status.x[0]);
  422. sharp_send_cmd(map, CMD_CLEAR_STATUS, adr);
  423. //spin_unlock_bh(chip->mutex);
  424. return -EIO;
  425. }
  426. #ifdef AUTOUNLOCK
  427. static void sharp_unlock_oneblock(struct map_info *map, struct flchip *chip,
  428. unsigned long adr)
  429. {
  430. int i;
  431. map_word status;
  432. sharp_send_cmd(map, CMD_CLEAR_BLOCK_LOCKS_1, adr);
  433. sharp_send_cmd(map, CMD_CLEAR_BLOCK_LOCKS_2, adr);
  434. udelay(100);
  435. status = map_read(map, adr);
  436. printk("status=%08lx\n", status.x[0]);
  437. for(i=0;i<1000;i++){
  438. //sharp_send_cmd(map, CMD_READ_STATUS, adr);
  439. status = map_read(map, adr);
  440. if((status.x[0] & SR_READY) == SR_READY)
  441. break;
  442. udelay(100);
  443. }
  444. if(i==1000){
  445. printk("sharp: timed out unlocking block\n");
  446. }
  447. if(!(status.x[0] & SR_ERRORS)){
  448. sharp_send_cmd(map, CMD_RESET, adr);
  449. chip->state = FL_READY;
  450. return;
  451. }
  452. printk("sharp: error unlocking block at addr=%08lx status=%08lx\n", adr, status.x[0]);
  453. sharp_send_cmd(map, CMD_CLEAR_STATUS, adr);
  454. }
  455. #endif
  456. static void sharp_sync(struct mtd_info *mtd)
  457. {
  458. //printk("sharp_sync()\n");
  459. }
  460. static int sharp_suspend(struct mtd_info *mtd)
  461. {
  462. printk("sharp_suspend()\n");
  463. return -EINVAL;
  464. }
  465. static void sharp_resume(struct mtd_info *mtd)
  466. {
  467. printk("sharp_resume()\n");
  468. }
  469. static void sharp_destroy(struct mtd_info *mtd)
  470. {
  471. printk("sharp_destroy()\n");
  472. }
  473. int __init sharp_probe_init(void)
  474. {
  475. printk("MTD Sharp chip driver <ds@lineo.com>\n");
  476. register_mtd_chip_driver(&sharp_chipdrv);
  477. return 0;
  478. }
  479. static void __exit sharp_probe_exit(void)
  480. {
  481. unregister_mtd_chip_driver(&sharp_chipdrv);
  482. }
  483. module_init(sharp_probe_init);
  484. module_exit(sharp_probe_exit);
  485. MODULE_LICENSE("GPL");
  486. MODULE_AUTHOR("David Schleef <ds@schleef.org>");
  487. MODULE_DESCRIPTION("Old MTD chip driver for pre-CFI Sharp flash chips");