sharp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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.14 2004/08/09 13:19:43 dwmw2 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. #define CMD_RESET 0xffffffff
  34. #define CMD_READ_ID 0x90909090
  35. #define CMD_READ_STATUS 0x70707070
  36. #define CMD_CLEAR_STATUS 0x50505050
  37. #define CMD_BLOCK_ERASE_1 0x20202020
  38. #define CMD_BLOCK_ERASE_2 0xd0d0d0d0
  39. #define CMD_BYTE_WRITE 0x40404040
  40. #define CMD_SUSPEND 0xb0b0b0b0
  41. #define CMD_RESUME 0xd0d0d0d0
  42. #define CMD_SET_BLOCK_LOCK_1 0x60606060
  43. #define CMD_SET_BLOCK_LOCK_2 0x01010101
  44. #define CMD_SET_MASTER_LOCK_1 0x60606060
  45. #define CMD_SET_MASTER_LOCK_2 0xf1f1f1f1
  46. #define CMD_CLEAR_BLOCK_LOCKS_1 0x60606060
  47. #define CMD_CLEAR_BLOCK_LOCKS_2 0xd0d0d0d0
  48. #define SR_READY 0x80808080 // 1 = ready
  49. #define SR_ERASE_SUSPEND 0x40404040 // 1 = block erase suspended
  50. #define SR_ERROR_ERASE 0x20202020 // 1 = error in block erase or clear lock bits
  51. #define SR_ERROR_WRITE 0x10101010 // 1 = error in byte write or set lock bit
  52. #define SR_VPP 0x08080808 // 1 = Vpp is low
  53. #define SR_WRITE_SUSPEND 0x04040404 // 1 = byte write suspended
  54. #define SR_PROTECT 0x02020202 // 1 = lock bit set
  55. #define SR_RESERVED 0x01010101
  56. #define SR_ERRORS (SR_ERROR_ERASE|SR_ERROR_WRITE|SR_VPP|SR_PROTECT)
  57. /* Configuration options */
  58. #undef AUTOUNLOCK /* automatically unlocks blocks before erasing */
  59. struct mtd_info *sharp_probe(struct map_info *);
  60. static int sharp_probe_map(struct map_info *map,struct mtd_info *mtd);
  61. static int sharp_read(struct mtd_info *mtd, loff_t from, size_t len,
  62. size_t *retlen, u_char *buf);
  63. static int sharp_write(struct mtd_info *mtd, loff_t from, size_t len,
  64. size_t *retlen, const u_char *buf);
  65. static int sharp_erase(struct mtd_info *mtd, struct erase_info *instr);
  66. static void sharp_sync(struct mtd_info *mtd);
  67. static int sharp_suspend(struct mtd_info *mtd);
  68. static void sharp_resume(struct mtd_info *mtd);
  69. static void sharp_destroy(struct mtd_info *mtd);
  70. static int sharp_write_oneword(struct map_info *map, struct flchip *chip,
  71. unsigned long adr, __u32 datum);
  72. static int sharp_erase_oneblock(struct map_info *map, struct flchip *chip,
  73. unsigned long adr);
  74. #ifdef AUTOUNLOCK
  75. static void sharp_unlock_oneblock(struct map_info *map, struct flchip *chip,
  76. unsigned long adr);
  77. #endif
  78. struct sharp_info{
  79. struct flchip *chip;
  80. int bogus;
  81. int chipshift;
  82. int numchips;
  83. struct flchip chips[1];
  84. };
  85. struct mtd_info *sharp_probe(struct map_info *map);
  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. 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 int sharp_probe_map(struct map_info *map,struct mtd_info *mtd)
  138. {
  139. unsigned long tmp;
  140. unsigned long base = 0;
  141. u32 read0, read4;
  142. int width = 4;
  143. tmp = map_read32(map, base+0);
  144. map_write32(map, CMD_READ_ID, base+0);
  145. read0=map_read32(map, base+0);
  146. read4=map_read32(map, base+4);
  147. if(read0 == 0x89898989){
  148. printk("Looks like sharp flash\n");
  149. switch(read4){
  150. case 0xaaaaaaaa:
  151. case 0xa0a0a0a0:
  152. /* aa - LH28F016SCT-L95 2Mx8, 32 64k blocks*/
  153. /* a0 - LH28F016SCT-Z4 2Mx8, 32 64k blocks*/
  154. mtd->erasesize = 0x10000 * width;
  155. mtd->size = 0x200000 * width;
  156. return width;
  157. case 0xa6a6a6a6:
  158. /* a6 - LH28F008SCT-L12 1Mx8, 16 64k blocks*/
  159. /* a6 - LH28F008SCR-L85 1Mx8, 16 64k blocks*/
  160. mtd->erasesize = 0x10000 * width;
  161. mtd->size = 0x100000 * width;
  162. return width;
  163. #if 0
  164. case 0x00000000: /* unknown */
  165. /* XX - LH28F004SCT 512kx8, 8 64k blocks*/
  166. mtd->erasesize = 0x10000 * width;
  167. mtd->size = 0x80000 * width;
  168. return width;
  169. #endif
  170. default:
  171. printk("Sort-of looks like sharp flash, 0x%08x 0x%08x\n",
  172. read0,read4);
  173. }
  174. }else if((map_read32(map, base+0) == CMD_READ_ID)){
  175. /* RAM, probably */
  176. printk("Looks like RAM\n");
  177. map_write32(map, tmp, base+0);
  178. }else{
  179. printk("Doesn't look like sharp flash, 0x%08x 0x%08x\n",
  180. read0,read4);
  181. }
  182. return 0;
  183. }
  184. /* This function returns with the chip->mutex lock held. */
  185. static int sharp_wait(struct map_info *map, struct flchip *chip)
  186. {
  187. __u16 status;
  188. unsigned long timeo = jiffies + HZ;
  189. DECLARE_WAITQUEUE(wait, current);
  190. int adr = 0;
  191. retry:
  192. spin_lock_bh(chip->mutex);
  193. switch(chip->state){
  194. case FL_READY:
  195. map_write32(map,CMD_READ_STATUS,adr);
  196. chip->state = FL_STATUS;
  197. case FL_STATUS:
  198. status = map_read32(map,adr);
  199. //printk("status=%08x\n",status);
  200. udelay(100);
  201. if((status & SR_READY)!=SR_READY){
  202. //printk(".status=%08x\n",status);
  203. udelay(100);
  204. }
  205. break;
  206. default:
  207. printk("Waiting for chip\n");
  208. set_current_state(TASK_INTERRUPTIBLE);
  209. add_wait_queue(&chip->wq, &wait);
  210. spin_unlock_bh(chip->mutex);
  211. schedule();
  212. remove_wait_queue(&chip->wq, &wait);
  213. if(signal_pending(current))
  214. return -EINTR;
  215. timeo = jiffies + HZ;
  216. goto retry;
  217. }
  218. map_write32(map,CMD_RESET, adr);
  219. chip->state = FL_READY;
  220. return 0;
  221. }
  222. static void sharp_release(struct flchip *chip)
  223. {
  224. wake_up(&chip->wq);
  225. spin_unlock_bh(chip->mutex);
  226. }
  227. static int sharp_read(struct mtd_info *mtd, loff_t from, size_t len,
  228. size_t *retlen, u_char *buf)
  229. {
  230. struct map_info *map = mtd->priv;
  231. struct sharp_info *sharp = map->fldrv_priv;
  232. int chipnum;
  233. int ret = 0;
  234. int ofs = 0;
  235. chipnum = (from >> sharp->chipshift);
  236. ofs = from & ((1 << sharp->chipshift)-1);
  237. *retlen = 0;
  238. while(len){
  239. unsigned long thislen;
  240. if(chipnum>=sharp->numchips)
  241. break;
  242. thislen = len;
  243. if(ofs+thislen >= (1<<sharp->chipshift))
  244. thislen = (1<<sharp->chipshift) - ofs;
  245. ret = sharp_wait(map,&sharp->chips[chipnum]);
  246. if(ret<0)
  247. break;
  248. map_copy_from(map,buf,ofs,thislen);
  249. sharp_release(&sharp->chips[chipnum]);
  250. *retlen += thislen;
  251. len -= thislen;
  252. buf += thislen;
  253. ofs = 0;
  254. chipnum++;
  255. }
  256. return ret;
  257. }
  258. static int sharp_write(struct mtd_info *mtd, loff_t to, size_t len,
  259. size_t *retlen, const u_char *buf)
  260. {
  261. struct map_info *map = mtd->priv;
  262. struct sharp_info *sharp = map->fldrv_priv;
  263. int ret = 0;
  264. int i,j;
  265. int chipnum;
  266. unsigned long ofs;
  267. union { u32 l; unsigned char uc[4]; } tbuf;
  268. *retlen = 0;
  269. while(len){
  270. tbuf.l = 0xffffffff;
  271. chipnum = to >> sharp->chipshift;
  272. ofs = to & ((1<<sharp->chipshift)-1);
  273. j=0;
  274. for(i=ofs&3;i<4 && len;i++){
  275. tbuf.uc[i] = *buf;
  276. buf++;
  277. to++;
  278. len--;
  279. j++;
  280. }
  281. sharp_write_oneword(map, &sharp->chips[chipnum], ofs&~3, tbuf.l);
  282. if(ret<0)
  283. return ret;
  284. (*retlen)+=j;
  285. }
  286. return 0;
  287. }
  288. static int sharp_write_oneword(struct map_info *map, struct flchip *chip,
  289. unsigned long adr, __u32 datum)
  290. {
  291. int ret;
  292. int timeo;
  293. int try;
  294. int i;
  295. int status = 0;
  296. ret = sharp_wait(map,chip);
  297. for(try=0;try<10;try++){
  298. map_write32(map,CMD_BYTE_WRITE,adr);
  299. /* cpu_to_le32 -> hack to fix the writel be->le conversion */
  300. map_write32(map,cpu_to_le32(datum),adr);
  301. chip->state = FL_WRITING;
  302. timeo = jiffies + (HZ/2);
  303. map_write32(map,CMD_READ_STATUS,adr);
  304. for(i=0;i<100;i++){
  305. status = map_read32(map,adr);
  306. if((status & SR_READY)==SR_READY)
  307. break;
  308. }
  309. if(i==100){
  310. printk("sharp: timed out writing\n");
  311. }
  312. if(!(status&SR_ERRORS))
  313. break;
  314. printk("sharp: error writing byte at addr=%08lx status=%08x\n",adr,status);
  315. map_write32(map,CMD_CLEAR_STATUS,adr);
  316. }
  317. map_write32(map,CMD_RESET,adr);
  318. chip->state = FL_READY;
  319. wake_up(&chip->wq);
  320. spin_unlock_bh(chip->mutex);
  321. return 0;
  322. }
  323. static int sharp_erase(struct mtd_info *mtd, struct erase_info *instr)
  324. {
  325. struct map_info *map = mtd->priv;
  326. struct sharp_info *sharp = map->fldrv_priv;
  327. unsigned long adr,len;
  328. int chipnum, ret=0;
  329. //printk("sharp_erase()\n");
  330. if(instr->addr & (mtd->erasesize - 1))
  331. return -EINVAL;
  332. if(instr->len & (mtd->erasesize - 1))
  333. return -EINVAL;
  334. if(instr->len + instr->addr > mtd->size)
  335. return -EINVAL;
  336. chipnum = instr->addr >> sharp->chipshift;
  337. adr = instr->addr & ((1<<sharp->chipshift)-1);
  338. len = instr->len;
  339. while(len){
  340. ret = sharp_erase_oneblock(map, &sharp->chips[chipnum], adr);
  341. if(ret)return ret;
  342. adr += mtd->erasesize;
  343. len -= mtd->erasesize;
  344. if(adr >> sharp->chipshift){
  345. adr = 0;
  346. chipnum++;
  347. if(chipnum>=sharp->numchips)
  348. break;
  349. }
  350. }
  351. instr->state = MTD_ERASE_DONE;
  352. mtd_erase_callback(instr);
  353. return 0;
  354. }
  355. static int sharp_do_wait_for_ready(struct map_info *map, struct flchip *chip,
  356. unsigned long adr)
  357. {
  358. int ret;
  359. unsigned long timeo;
  360. int status;
  361. DECLARE_WAITQUEUE(wait, current);
  362. map_write32(map,CMD_READ_STATUS,adr);
  363. status = map_read32(map,adr);
  364. timeo = jiffies + HZ;
  365. while(time_before(jiffies, timeo)){
  366. map_write32(map,CMD_READ_STATUS,adr);
  367. status = map_read32(map,adr);
  368. if((status & SR_READY)==SR_READY){
  369. ret = 0;
  370. goto out;
  371. }
  372. set_current_state(TASK_INTERRUPTIBLE);
  373. add_wait_queue(&chip->wq, &wait);
  374. //spin_unlock_bh(chip->mutex);
  375. schedule_timeout(1);
  376. schedule();
  377. remove_wait_queue(&chip->wq, &wait);
  378. //spin_lock_bh(chip->mutex);
  379. if (signal_pending(current)){
  380. ret = -EINTR;
  381. goto out;
  382. }
  383. }
  384. ret = -ETIME;
  385. out:
  386. return ret;
  387. }
  388. static int sharp_erase_oneblock(struct map_info *map, struct flchip *chip,
  389. unsigned long adr)
  390. {
  391. int ret;
  392. //int timeo;
  393. int status;
  394. //int i;
  395. //printk("sharp_erase_oneblock()\n");
  396. #ifdef AUTOUNLOCK
  397. /* This seems like a good place to do an unlock */
  398. sharp_unlock_oneblock(map,chip,adr);
  399. #endif
  400. map_write32(map,CMD_BLOCK_ERASE_1,adr);
  401. map_write32(map,CMD_BLOCK_ERASE_2,adr);
  402. chip->state = FL_ERASING;
  403. ret = sharp_do_wait_for_ready(map,chip,adr);
  404. if(ret<0)return ret;
  405. map_write32(map,CMD_READ_STATUS,adr);
  406. status = map_read32(map,adr);
  407. if(!(status&SR_ERRORS)){
  408. map_write32(map,CMD_RESET,adr);
  409. chip->state = FL_READY;
  410. //spin_unlock_bh(chip->mutex);
  411. return 0;
  412. }
  413. printk("sharp: error erasing block at addr=%08lx status=%08x\n",adr,status);
  414. map_write32(map,CMD_CLEAR_STATUS,adr);
  415. //spin_unlock_bh(chip->mutex);
  416. return -EIO;
  417. }
  418. #ifdef AUTOUNLOCK
  419. static void sharp_unlock_oneblock(struct map_info *map, struct flchip *chip,
  420. unsigned long adr)
  421. {
  422. int i;
  423. int status;
  424. map_write32(map,CMD_CLEAR_BLOCK_LOCKS_1,adr);
  425. map_write32(map,CMD_CLEAR_BLOCK_LOCKS_2,adr);
  426. udelay(100);
  427. status = map_read32(map,adr);
  428. printk("status=%08x\n",status);
  429. for(i=0;i<1000;i++){
  430. //map_write32(map,CMD_READ_STATUS,adr);
  431. status = map_read32(map,adr);
  432. if((status & SR_READY)==SR_READY)
  433. break;
  434. udelay(100);
  435. }
  436. if(i==1000){
  437. printk("sharp: timed out unlocking block\n");
  438. }
  439. if(!(status&SR_ERRORS)){
  440. map_write32(map,CMD_RESET,adr);
  441. chip->state = FL_READY;
  442. return;
  443. }
  444. printk("sharp: error unlocking block at addr=%08lx status=%08x\n",adr,status);
  445. map_write32(map,CMD_CLEAR_STATUS,adr);
  446. }
  447. #endif
  448. static void sharp_sync(struct mtd_info *mtd)
  449. {
  450. //printk("sharp_sync()\n");
  451. }
  452. static int sharp_suspend(struct mtd_info *mtd)
  453. {
  454. printk("sharp_suspend()\n");
  455. return -EINVAL;
  456. }
  457. static void sharp_resume(struct mtd_info *mtd)
  458. {
  459. printk("sharp_resume()\n");
  460. }
  461. static void sharp_destroy(struct mtd_info *mtd)
  462. {
  463. printk("sharp_destroy()\n");
  464. }
  465. int __init sharp_probe_init(void)
  466. {
  467. printk("MTD Sharp chip driver <ds@lineo.com>\n");
  468. register_mtd_chip_driver(&sharp_chipdrv);
  469. return 0;
  470. }
  471. static void __exit sharp_probe_exit(void)
  472. {
  473. unregister_mtd_chip_driver(&sharp_chipdrv);
  474. }
  475. module_init(sharp_probe_init);
  476. module_exit(sharp_probe_exit);
  477. MODULE_LICENSE("GPL");
  478. MODULE_AUTHOR("David Schleef <ds@schleef.org>");
  479. MODULE_DESCRIPTION("Old MTD chip driver for pre-CFI Sharp flash chips");