sharp.c 13 KB

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