mtd_subpagetest.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright (C) 2006-2007 Nokia Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; see the file COPYING. If not, write to the Free Software
  15. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * Test sub-page read and write on MTD device.
  18. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  19. *
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/err.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/slab.h>
  27. #include <linux/sched.h>
  28. #define PRINT_PREF KERN_INFO "mtd_subpagetest: "
  29. static int dev;
  30. module_param(dev, int, S_IRUGO);
  31. MODULE_PARM_DESC(dev, "MTD device number to use");
  32. static struct mtd_info *mtd;
  33. static unsigned char *writebuf;
  34. static unsigned char *readbuf;
  35. static unsigned char *bbt;
  36. static int subpgsize;
  37. static int bufsize;
  38. static int ebcnt;
  39. static int pgcnt;
  40. static int errcnt;
  41. static unsigned long next = 1;
  42. static inline unsigned int simple_rand(void)
  43. {
  44. next = next * 1103515245 + 12345;
  45. return (unsigned int)((next / 65536) % 32768);
  46. }
  47. static inline void simple_srand(unsigned long seed)
  48. {
  49. next = seed;
  50. }
  51. static void set_random_data(unsigned char *buf, size_t len)
  52. {
  53. size_t i;
  54. for (i = 0; i < len; ++i)
  55. buf[i] = simple_rand();
  56. }
  57. static inline void clear_data(unsigned char *buf, size_t len)
  58. {
  59. memset(buf, 0, len);
  60. }
  61. static int erase_eraseblock(int ebnum)
  62. {
  63. int err;
  64. struct erase_info ei;
  65. loff_t addr = ebnum * mtd->erasesize;
  66. memset(&ei, 0, sizeof(struct erase_info));
  67. ei.mtd = mtd;
  68. ei.addr = addr;
  69. ei.len = mtd->erasesize;
  70. err = mtd->erase(mtd, &ei);
  71. if (err) {
  72. printk(PRINT_PREF "error %d while erasing EB %d\n", err, ebnum);
  73. return err;
  74. }
  75. if (ei.state == MTD_ERASE_FAILED) {
  76. printk(PRINT_PREF "some erase error occurred at EB %d\n",
  77. ebnum);
  78. return -EIO;
  79. }
  80. return 0;
  81. }
  82. static int erase_whole_device(void)
  83. {
  84. int err;
  85. unsigned int i;
  86. printk(PRINT_PREF "erasing whole device\n");
  87. for (i = 0; i < ebcnt; ++i) {
  88. if (bbt[i])
  89. continue;
  90. err = erase_eraseblock(i);
  91. if (err)
  92. return err;
  93. cond_resched();
  94. }
  95. printk(PRINT_PREF "erased %u eraseblocks\n", i);
  96. return 0;
  97. }
  98. static int write_eraseblock(int ebnum)
  99. {
  100. size_t written = 0;
  101. int err = 0;
  102. loff_t addr = ebnum * mtd->erasesize;
  103. set_random_data(writebuf, subpgsize);
  104. err = mtd->write(mtd, addr, subpgsize, &written, writebuf);
  105. if (unlikely(err || written != subpgsize)) {
  106. printk(PRINT_PREF "error: write failed at %#llx\n",
  107. (long long)addr);
  108. if (written != subpgsize) {
  109. printk(PRINT_PREF " write size: %#x\n", subpgsize);
  110. printk(PRINT_PREF " written: %#zx\n", written);
  111. }
  112. return err ? err : -1;
  113. }
  114. addr += subpgsize;
  115. set_random_data(writebuf, subpgsize);
  116. err = mtd->write(mtd, addr, subpgsize, &written, writebuf);
  117. if (unlikely(err || written != subpgsize)) {
  118. printk(PRINT_PREF "error: write failed at %#llx\n",
  119. (long long)addr);
  120. if (written != subpgsize) {
  121. printk(PRINT_PREF " write size: %#x\n", subpgsize);
  122. printk(PRINT_PREF " written: %#zx\n", written);
  123. }
  124. return err ? err : -1;
  125. }
  126. return err;
  127. }
  128. static int write_eraseblock2(int ebnum)
  129. {
  130. size_t written = 0;
  131. int err = 0, k;
  132. loff_t addr = ebnum * mtd->erasesize;
  133. for (k = 1; k < 33; ++k) {
  134. if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize)
  135. break;
  136. set_random_data(writebuf, subpgsize * k);
  137. err = mtd->write(mtd, addr, subpgsize * k, &written, writebuf);
  138. if (unlikely(err || written != subpgsize * k)) {
  139. printk(PRINT_PREF "error: write failed at %#llx\n",
  140. (long long)addr);
  141. if (written != subpgsize) {
  142. printk(PRINT_PREF " write size: %#x\n",
  143. subpgsize * k);
  144. printk(PRINT_PREF " written: %#08zx\n",
  145. written);
  146. }
  147. return err ? err : -1;
  148. }
  149. addr += subpgsize * k;
  150. }
  151. return err;
  152. }
  153. static void print_subpage(unsigned char *p)
  154. {
  155. int i, j;
  156. for (i = 0; i < subpgsize; ) {
  157. for (j = 0; i < subpgsize && j < 32; ++i, ++j)
  158. printk("%02x", *p++);
  159. printk("\n");
  160. }
  161. }
  162. static int verify_eraseblock(int ebnum)
  163. {
  164. size_t read = 0;
  165. int err = 0;
  166. loff_t addr = ebnum * mtd->erasesize;
  167. set_random_data(writebuf, subpgsize);
  168. clear_data(readbuf, subpgsize);
  169. read = 0;
  170. err = mtd->read(mtd, addr, subpgsize, &read, readbuf);
  171. if (unlikely(err || read != subpgsize)) {
  172. if (err == -EUCLEAN && read == subpgsize) {
  173. printk(PRINT_PREF "ECC correction at %#llx\n",
  174. (long long)addr);
  175. err = 0;
  176. } else {
  177. printk(PRINT_PREF "error: read failed at %#llx\n",
  178. (long long)addr);
  179. return err ? err : -1;
  180. }
  181. }
  182. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  183. printk(PRINT_PREF "error: verify failed at %#llx\n",
  184. (long long)addr);
  185. printk(PRINT_PREF "------------- written----------------\n");
  186. print_subpage(writebuf);
  187. printk(PRINT_PREF "------------- read ------------------\n");
  188. print_subpage(readbuf);
  189. printk(PRINT_PREF "-------------------------------------\n");
  190. errcnt += 1;
  191. }
  192. addr += subpgsize;
  193. set_random_data(writebuf, subpgsize);
  194. clear_data(readbuf, subpgsize);
  195. read = 0;
  196. err = mtd->read(mtd, addr, subpgsize, &read, readbuf);
  197. if (unlikely(err || read != subpgsize)) {
  198. if (err == -EUCLEAN && read == subpgsize) {
  199. printk(PRINT_PREF "ECC correction at %#llx\n",
  200. (long long)addr);
  201. err = 0;
  202. } else {
  203. printk(PRINT_PREF "error: read failed at %#llx\n",
  204. (long long)addr);
  205. return err ? err : -1;
  206. }
  207. }
  208. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  209. printk(PRINT_PREF "error: verify failed at %#llx\n",
  210. (long long)addr);
  211. printk(PRINT_PREF "------------- written----------------\n");
  212. print_subpage(writebuf);
  213. printk(PRINT_PREF "------------- read ------------------\n");
  214. print_subpage(readbuf);
  215. printk(PRINT_PREF "-------------------------------------\n");
  216. errcnt += 1;
  217. }
  218. return err;
  219. }
  220. static int verify_eraseblock2(int ebnum)
  221. {
  222. size_t read = 0;
  223. int err = 0, k;
  224. loff_t addr = ebnum * mtd->erasesize;
  225. for (k = 1; k < 33; ++k) {
  226. if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize)
  227. break;
  228. set_random_data(writebuf, subpgsize * k);
  229. clear_data(readbuf, subpgsize * k);
  230. read = 0;
  231. err = mtd->read(mtd, addr, subpgsize * k, &read, readbuf);
  232. if (unlikely(err || read != subpgsize * k)) {
  233. if (err == -EUCLEAN && read == subpgsize * k) {
  234. printk(PRINT_PREF "ECC correction at %#llx\n",
  235. (long long)addr);
  236. err = 0;
  237. } else {
  238. printk(PRINT_PREF "error: read failed at "
  239. "%#llx\n", (long long)addr);
  240. return err ? err : -1;
  241. }
  242. }
  243. if (unlikely(memcmp(readbuf, writebuf, subpgsize * k))) {
  244. printk(PRINT_PREF "error: verify failed at %#llx\n",
  245. (long long)addr);
  246. errcnt += 1;
  247. }
  248. addr += subpgsize * k;
  249. }
  250. return err;
  251. }
  252. static int verify_eraseblock_ff(int ebnum)
  253. {
  254. uint32_t j;
  255. size_t read = 0;
  256. int err = 0;
  257. loff_t addr = ebnum * mtd->erasesize;
  258. memset(writebuf, 0xff, subpgsize);
  259. for (j = 0; j < mtd->erasesize / subpgsize; ++j) {
  260. clear_data(readbuf, subpgsize);
  261. read = 0;
  262. err = mtd->read(mtd, addr, subpgsize, &read, readbuf);
  263. if (unlikely(err || read != subpgsize)) {
  264. if (err == -EUCLEAN && read == subpgsize) {
  265. printk(PRINT_PREF "ECC correction at %#llx\n",
  266. (long long)addr);
  267. err = 0;
  268. } else {
  269. printk(PRINT_PREF "error: read failed at "
  270. "%#llx\n", (long long)addr);
  271. return err ? err : -1;
  272. }
  273. }
  274. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  275. printk(PRINT_PREF "error: verify 0xff failed at "
  276. "%#llx\n", (long long)addr);
  277. errcnt += 1;
  278. }
  279. addr += subpgsize;
  280. }
  281. return err;
  282. }
  283. static int verify_all_eraseblocks_ff(void)
  284. {
  285. int err;
  286. unsigned int i;
  287. printk(PRINT_PREF "verifying all eraseblocks for 0xff\n");
  288. for (i = 0; i < ebcnt; ++i) {
  289. if (bbt[i])
  290. continue;
  291. err = verify_eraseblock_ff(i);
  292. if (err)
  293. return err;
  294. if (i % 256 == 0)
  295. printk(PRINT_PREF "verified up to eraseblock %u\n", i);
  296. cond_resched();
  297. }
  298. printk(PRINT_PREF "verified %u eraseblocks\n", i);
  299. return 0;
  300. }
  301. static int is_block_bad(int ebnum)
  302. {
  303. loff_t addr = ebnum * mtd->erasesize;
  304. int ret;
  305. ret = mtd->block_isbad(mtd, addr);
  306. if (ret)
  307. printk(PRINT_PREF "block %d is bad\n", ebnum);
  308. return ret;
  309. }
  310. static int scan_for_bad_eraseblocks(void)
  311. {
  312. int i, bad = 0;
  313. bbt = kzalloc(ebcnt, GFP_KERNEL);
  314. if (!bbt) {
  315. printk(PRINT_PREF "error: cannot allocate memory\n");
  316. return -ENOMEM;
  317. }
  318. printk(PRINT_PREF "scanning for bad eraseblocks\n");
  319. for (i = 0; i < ebcnt; ++i) {
  320. bbt[i] = is_block_bad(i) ? 1 : 0;
  321. if (bbt[i])
  322. bad += 1;
  323. cond_resched();
  324. }
  325. printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
  326. return 0;
  327. }
  328. static int __init mtd_subpagetest_init(void)
  329. {
  330. int err = 0;
  331. uint32_t i;
  332. uint64_t tmp;
  333. printk(KERN_INFO "\n");
  334. printk(KERN_INFO "=================================================\n");
  335. printk(PRINT_PREF "MTD device: %d\n", dev);
  336. mtd = get_mtd_device(NULL, dev);
  337. if (IS_ERR(mtd)) {
  338. err = PTR_ERR(mtd);
  339. printk(PRINT_PREF "error: cannot get MTD device\n");
  340. return err;
  341. }
  342. if (mtd->type != MTD_NANDFLASH) {
  343. printk(PRINT_PREF "this test requires NAND flash\n");
  344. goto out;
  345. }
  346. subpgsize = mtd->writesize >> mtd->subpage_sft;
  347. printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
  348. "page size %u, subpage size %u, count of eraseblocks %u, "
  349. "pages per eraseblock %u, OOB size %u\n",
  350. (unsigned long long)mtd->size, mtd->erasesize,
  351. mtd->writesize, subpgsize, ebcnt, pgcnt, mtd->oobsize);
  352. err = -ENOMEM;
  353. bufsize = subpgsize * 32;
  354. writebuf = kmalloc(bufsize, GFP_KERNEL);
  355. if (!writebuf) {
  356. printk(PRINT_PREF "error: cannot allocate memory\n");
  357. goto out;
  358. }
  359. readbuf = kmalloc(bufsize, GFP_KERNEL);
  360. if (!readbuf) {
  361. printk(PRINT_PREF "error: cannot allocate memory\n");
  362. goto out;
  363. }
  364. tmp = mtd->size;
  365. do_div(tmp, mtd->erasesize);
  366. ebcnt = tmp;
  367. pgcnt = mtd->erasesize / mtd->writesize;
  368. err = scan_for_bad_eraseblocks();
  369. if (err)
  370. goto out;
  371. err = erase_whole_device();
  372. if (err)
  373. goto out;
  374. printk(PRINT_PREF "writing whole device\n");
  375. simple_srand(1);
  376. for (i = 0; i < ebcnt; ++i) {
  377. if (bbt[i])
  378. continue;
  379. err = write_eraseblock(i);
  380. if (unlikely(err))
  381. goto out;
  382. if (i % 256 == 0)
  383. printk(PRINT_PREF "written up to eraseblock %u\n", i);
  384. cond_resched();
  385. }
  386. printk(PRINT_PREF "written %u eraseblocks\n", i);
  387. simple_srand(1);
  388. printk(PRINT_PREF "verifying all eraseblocks\n");
  389. for (i = 0; i < ebcnt; ++i) {
  390. if (bbt[i])
  391. continue;
  392. err = verify_eraseblock(i);
  393. if (unlikely(err))
  394. goto out;
  395. if (i % 256 == 0)
  396. printk(PRINT_PREF "verified up to eraseblock %u\n", i);
  397. cond_resched();
  398. }
  399. printk(PRINT_PREF "verified %u eraseblocks\n", i);
  400. err = erase_whole_device();
  401. if (err)
  402. goto out;
  403. err = verify_all_eraseblocks_ff();
  404. if (err)
  405. goto out;
  406. /* Write all eraseblocks */
  407. simple_srand(3);
  408. printk(PRINT_PREF "writing whole device\n");
  409. for (i = 0; i < ebcnt; ++i) {
  410. if (bbt[i])
  411. continue;
  412. err = write_eraseblock2(i);
  413. if (unlikely(err))
  414. goto out;
  415. if (i % 256 == 0)
  416. printk(PRINT_PREF "written up to eraseblock %u\n", i);
  417. cond_resched();
  418. }
  419. printk(PRINT_PREF "written %u eraseblocks\n", i);
  420. /* Check all eraseblocks */
  421. simple_srand(3);
  422. printk(PRINT_PREF "verifying all eraseblocks\n");
  423. for (i = 0; i < ebcnt; ++i) {
  424. if (bbt[i])
  425. continue;
  426. err = verify_eraseblock2(i);
  427. if (unlikely(err))
  428. goto out;
  429. if (i % 256 == 0)
  430. printk(PRINT_PREF "verified up to eraseblock %u\n", i);
  431. cond_resched();
  432. }
  433. printk(PRINT_PREF "verified %u eraseblocks\n", i);
  434. err = erase_whole_device();
  435. if (err)
  436. goto out;
  437. err = verify_all_eraseblocks_ff();
  438. if (err)
  439. goto out;
  440. printk(PRINT_PREF "finished with %d errors\n", errcnt);
  441. out:
  442. kfree(bbt);
  443. kfree(readbuf);
  444. kfree(writebuf);
  445. put_mtd_device(mtd);
  446. if (err)
  447. printk(PRINT_PREF "error %d occurred\n", err);
  448. printk(KERN_INFO "=================================================\n");
  449. return err;
  450. }
  451. module_init(mtd_subpagetest_init);
  452. static void __exit mtd_subpagetest_exit(void)
  453. {
  454. return;
  455. }
  456. module_exit(mtd_subpagetest_exit);
  457. MODULE_DESCRIPTION("Subpage test module");
  458. MODULE_AUTHOR("Adrian Hunter");
  459. MODULE_LICENSE("GPL");