flashkw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * sheevaplug nand flash driver
  3. *
  4. * for now separate from (inferno's) os/port/flashnand.c because the flash
  5. * seems newer, and has different commands, but that is nand-chip specific,
  6. * not sheevaplug-specific. they should be merged in future.
  7. *
  8. * the sheevaplug has a hynix 4gbit flash chip: hy27uf084g2m.
  9. * 2048 byte pages, with 64 spare bytes each; erase block size is 128k.
  10. *
  11. * it has a "glueless" interface, at 0xf9000000. that's the address
  12. * of the data register. the command and address registers are those
  13. * or'ed with 1 and 2 respectively.
  14. *
  15. * linux uses this layout for the nand flash (from address 0 onwards):
  16. * 1mb for u-boot
  17. * 4mb for kernel
  18. * 507mb for file system
  19. *
  20. * this is not so relevant here except for ecc. the first two areas
  21. * (u-boot and kernel) are expected to have 4-bit ecc per 512 bytes
  22. * (but calculated from last byte to first), bad erase blocks skipped.
  23. * the file system area has 1-bit ecc per 256 bytes.
  24. */
  25. #include "u.h"
  26. #include "../port/lib.h"
  27. #include "mem.h"
  28. #include "dat.h"
  29. #include "fns.h"
  30. #include "io.h"
  31. #include "../port/error.h"
  32. #include "../port/flashif.h"
  33. #include "../port/nandecc.h"
  34. #define NANDFREG ((Nandreg*)AddrNandf)
  35. enum {
  36. Debug = 0,
  37. Nopage = ~0ul, /* cache is empty */
  38. /* vendors */
  39. Hynix = 0xad,
  40. Samsung = 0xec,
  41. /* chips */
  42. Hy27UF084G2M = 0xdc,
  43. NandActCEBoot = 1<<1,
  44. };
  45. typedef struct Nandreg Nandreg;
  46. typedef struct Nandtab Nandtab;
  47. typedef struct Cache Cache;
  48. struct Nandreg { /* hw registers */
  49. ulong rdparms;
  50. ulong wrparms;
  51. uchar _pad0[0x70 - 0x20];
  52. ulong ctl;
  53. };
  54. struct Nandtab {
  55. int vid;
  56. int did;
  57. vlong size;
  58. char* name;
  59. };
  60. struct Cache {
  61. Flash *flif;
  62. ulong pageno;
  63. ulong pgsize; /* r->pagesize */
  64. char *page; /* of pgsize bytes */
  65. };
  66. enum {
  67. /* commands */
  68. Readstatus = 0x70,
  69. Readid = 0x90, /* needs 1 0-address write */
  70. Resetf = 0xff,
  71. /*
  72. * needs 5 address writes followed by Readstart,
  73. * Readstartcache or Restartcopy.
  74. */
  75. Read = 0x00,
  76. Readstart = 0x30,
  77. Readstartcache = 0x31,
  78. Readstartcopy = 0x35,
  79. /* after Readstartcache, to stop reading next pages */
  80. Readstopcache = 0x34,
  81. /* needs 5 address writes, the data, and -start or -cache */
  82. Program = 0x80,
  83. Programstart = 0x10,
  84. Programcache = 0x15,
  85. Copyback = 0x85, /* followed by Programstart */
  86. /* 3 address writes for block followed by Erasestart */
  87. Erase = 0x60,
  88. Erasestart = 0xd0,
  89. Randomread = 0x85,
  90. Randomwrite = 0x05,
  91. Randomwritestart= 0xe0,
  92. /* status bits */
  93. SFail = 1<<0,
  94. SCachefail = 1<<1,
  95. SIdle = 1<<5, /* doesn't seem to come on ever */
  96. SReady = 1<<6,
  97. SNotprotected = 1<<7,
  98. Srdymask = SReady, /* was SIdle|SReady */
  99. };
  100. Nandtab nandtab[] = {
  101. {Hynix, Hy27UF084G2M, 512*MB, "Hy27UF084G2M"},
  102. {Samsung, 0xdc, 512*MB, "Samsung 2Gb"},
  103. };
  104. static Cache cache;
  105. static void
  106. nandcmd(Flash *f, uchar b)
  107. {
  108. uchar *p = (uchar *)((ulong)f->addr|1);
  109. *p = b;
  110. coherence();
  111. }
  112. static void
  113. nandaddr(Flash *f, uchar b)
  114. {
  115. uchar *p = (uchar *)((ulong)f->addr|2);
  116. *p = b;
  117. coherence();
  118. }
  119. static uchar
  120. nandread(Flash *f)
  121. {
  122. return *(uchar *)f->addr;
  123. }
  124. static void
  125. nandreadn(Flash *f, uchar *buf, long n)
  126. {
  127. uchar *p = f->addr;
  128. while(n-- > 0)
  129. *buf++ = *p;
  130. }
  131. static void
  132. nandwrite(Flash *f, uchar b)
  133. {
  134. *(uchar *)f->addr = b;
  135. coherence();
  136. }
  137. static void
  138. nandwriten(Flash *f, uchar *buf, long n)
  139. {
  140. uchar *p = f->addr;
  141. while(n-- > 0)
  142. *p = *buf++;
  143. coherence();
  144. }
  145. static void
  146. nandclaim(Flash*)
  147. {
  148. NANDFREG->ctl |= NandActCEBoot;
  149. coherence();
  150. }
  151. static void
  152. nandunclaim(Flash*)
  153. {
  154. NANDFREG->ctl &= ~NandActCEBoot;
  155. coherence();
  156. }
  157. void mmuidmap(uintptr phys, int mbs);
  158. Nandtab *
  159. findflash(Flash *f, uintptr pa, uchar *id4p)
  160. {
  161. int i;
  162. ulong sts;
  163. uchar maker, device, id3, id4;
  164. Nandtab *chip;
  165. mmuidmap(pa, 16);
  166. f->addr = (void *)pa;
  167. /* make sure controller is idle */
  168. nandclaim(f);
  169. nandcmd(f, Resetf);
  170. nandunclaim(f);
  171. nandclaim(f);
  172. nandcmd(f, Readstatus);
  173. sts = nandread(f);
  174. nandunclaim(f);
  175. for (i = 10; i > 0 && !(sts & SReady); i--) {
  176. delay(50);
  177. nandclaim(f);
  178. nandcmd(f, Readstatus);
  179. sts = nandread(f);
  180. nandunclaim(f);
  181. }
  182. if(!(sts & SReady))
  183. return nil;
  184. nandclaim(f);
  185. nandcmd(f, Readid);
  186. nandaddr(f, 0);
  187. maker = nandread(f);
  188. device = nandread(f);
  189. id3 = nandread(f);
  190. USED(id3);
  191. id4 = nandread(f);
  192. nandunclaim(f);
  193. if (id4p)
  194. *id4p = id4;
  195. for(i = 0; i < nelem(nandtab); i++) {
  196. chip = &nandtab[i];
  197. if(chip->vid == maker && chip->did == device)
  198. return chip;
  199. }
  200. return nil;
  201. }
  202. int
  203. flashat(Flash *f, uintptr pa)
  204. {
  205. return findflash(f, pa, nil) != nil;
  206. }
  207. static int
  208. idchip(Flash *f)
  209. {
  210. uchar id4;
  211. Flashregion *r;
  212. Nandtab *chip;
  213. static int blocksizes[4] = { 64*1024, 128*1024, 256*1024, 0 };
  214. static int pagesizes[4] = { 1024, 2*1024, 0, 0 };
  215. static int spares[2] = { 8, 16 }; /* per 512 bytes */
  216. f->id = 0;
  217. f->devid = 0;
  218. f->width = 1;
  219. chip = findflash(f, (uintptr)f->addr, &id4);
  220. if (chip == nil)
  221. return -1;
  222. f->id = chip->vid;
  223. f->devid = chip->did;
  224. f->size = chip->size;
  225. f->width = 1;
  226. f->nr = 1;
  227. r = &f->regions[0];
  228. r->pagesize = pagesizes[id4 & MASK(2)];
  229. r->erasesize = blocksizes[(id4 >> 4) & MASK(2)];
  230. if (r->pagesize == 0 || r->erasesize == 0) {
  231. iprint("flashkw: bogus flash sizes\n");
  232. return -1;
  233. }
  234. r->n = f->size / r->erasesize;
  235. r->start = 0;
  236. r->end = f->size;
  237. assert(ispow2(r->pagesize));
  238. r->pageshift = log2(r->pagesize);
  239. assert(ispow2(r->erasesize));
  240. r->eraseshift = log2(r->erasesize);
  241. assert(r->eraseshift >= r->pageshift);
  242. if (cache.page == nil) {
  243. cache.pgsize = r->pagesize;
  244. cache.page = smalloc(r->pagesize);
  245. }
  246. r->spares = r->pagesize / 512 * spares[(id4 >> 2) & 1];
  247. print("#F0: kwnand: %s %,lud bytes pagesize %lud erasesize %,lud"
  248. " spares per page %lud\n", chip->name, f->size, r->pagesize,
  249. r->erasesize, r->spares);
  250. return 0;
  251. }
  252. static int
  253. ctlrwait(Flash *f)
  254. {
  255. int sts, cnt;
  256. nandclaim(f);
  257. for (;;) {
  258. nandcmd(f, Readstatus);
  259. for(cnt = 100; cnt > 0 && (nandread(f) & Srdymask) != Srdymask;
  260. cnt--)
  261. microdelay(50);
  262. nandcmd(f, Readstatus);
  263. sts = nandread(f);
  264. if((sts & Srdymask) == Srdymask)
  265. break;
  266. print("flashkw: flash ctlr busy, sts %#ux: resetting\n", sts);
  267. nandcmd(f, Resetf);
  268. }
  269. nandunclaim(f);
  270. return 0;
  271. }
  272. static int
  273. erasezone(Flash *f, Flashregion *r, ulong offset)
  274. {
  275. int i;
  276. ulong page, block;
  277. uchar s;
  278. if (Debug) {
  279. print("flashkw: erasezone: offset %#lux, region nblocks %d,"
  280. " start %#lux, end %#lux\n", offset, r->n, r->start,
  281. r->end);
  282. print(" erasesize %lud, pagesize %lud\n",
  283. r->erasesize, r->pagesize);
  284. }
  285. assert(r->erasesize != 0);
  286. if(offset & (r->erasesize - 1)) {
  287. print("flashkw: erase offset %lud not block aligned\n", offset);
  288. return -1;
  289. }
  290. page = offset >> r->pageshift;
  291. block = page >> (r->eraseshift - r->pageshift);
  292. if (Debug)
  293. print("flashkw: erase: block %#lux\n", block);
  294. /* make sure controller is idle */
  295. if(ctlrwait(f) < 0) {
  296. print("flashkw: erase: flash busy\n");
  297. return -1;
  298. }
  299. /* start erasing */
  300. nandclaim(f);
  301. nandcmd(f, Erase);
  302. nandaddr(f, page>>0);
  303. nandaddr(f, page>>8);
  304. nandaddr(f, page>>16);
  305. nandcmd(f, Erasestart);
  306. /* invalidate cache on any erasure (slight overkill) */
  307. cache.pageno = Nopage;
  308. /* have to wait until flash is done. typically ~2ms */
  309. delay(1);
  310. nandcmd(f, Readstatus);
  311. for(i = 0; i < 100; i++) {
  312. s = nandread(f);
  313. if(s & SReady) {
  314. nandunclaim(f);
  315. if(s & SFail) {
  316. print("flashkw: erase: failed, block %#lux\n",
  317. block);
  318. return -1;
  319. }
  320. return 0;
  321. }
  322. microdelay(50);
  323. }
  324. print("flashkw: erase timeout, block %#lux\n", block);
  325. nandunclaim(f);
  326. return -1;
  327. }
  328. static void
  329. flcachepage(Flash *f, ulong page, uchar *buf)
  330. {
  331. Flashregion *r = &f->regions[0];
  332. assert(cache.pgsize == r->pagesize);
  333. cache.flif = f;
  334. cache.pageno = page;
  335. /* permit i/o directly to or from the cache */
  336. if (buf != (uchar *)cache.page)
  337. memmove(cache.page, buf, cache.pgsize);
  338. }
  339. static int
  340. write1page(Flash *f, ulong offset, void *buf)
  341. {
  342. int i;
  343. ulong page, v;
  344. uchar s;
  345. uchar *eccp, *p;
  346. Flashregion *r = &f->regions[0];
  347. static uchar *oob;
  348. if (oob == nil)
  349. oob = smalloc(r->spares);
  350. page = offset >> r->pageshift;
  351. if (Debug)
  352. print("flashkw: write nand offset %#lux page %#lux\n",
  353. offset, page);
  354. if(offset & (r->pagesize - 1)) {
  355. print("flashkw: write offset %lud not page aligned\n", offset);
  356. return -1;
  357. }
  358. p = buf;
  359. memset(oob, 0xff, r->spares);
  360. assert(r->spares >= 24);
  361. eccp = oob + r->spares - 24;
  362. for(i = 0; i < r->pagesize / 256; i++) {
  363. v = nandecc(p);
  364. *eccp++ = v>>8;
  365. *eccp++ = v>>0;
  366. *eccp++ = v>>16;
  367. p += 256;
  368. }
  369. if(ctlrwait(f) < 0) {
  370. print("flashkw: write: nand not ready & idle\n");
  371. return -1;
  372. }
  373. /* write, only whole pages for now, no sub-pages */
  374. nandclaim(f);
  375. nandcmd(f, Program);
  376. nandaddr(f, 0);
  377. nandaddr(f, 0);
  378. nandaddr(f, page>>0);
  379. nandaddr(f, page>>8);
  380. nandaddr(f, page>>16);
  381. nandwriten(f, buf, r->pagesize);
  382. nandwriten(f, oob, r->spares);
  383. nandcmd(f, Programstart);
  384. microdelay(100);
  385. nandcmd(f, Readstatus);
  386. for(i = 0; i < 100; i++) {
  387. s = nandread(f);
  388. if(s & SReady) {
  389. nandunclaim(f);
  390. if(s & SFail) {
  391. print("flashkw: write failed, page %#lux\n",
  392. page);
  393. return -1;
  394. }
  395. return 0;
  396. }
  397. microdelay(10);
  398. }
  399. nandunclaim(f);
  400. flcachepage(f, page, buf);
  401. print("flashkw: write timeout for page %#lux\n", page);
  402. return -1;
  403. }
  404. static int
  405. read1page(Flash *f, ulong offset, void *buf)
  406. {
  407. int i;
  408. ulong addr, page, w;
  409. uchar *eccp, *p;
  410. Flashregion *r = &f->regions[0];
  411. static uchar *oob;
  412. if (oob == nil)
  413. oob = smalloc(r->spares);
  414. assert(r->pagesize != 0);
  415. addr = offset & (r->pagesize - 1);
  416. page = offset >> r->pageshift;
  417. if(addr != 0) {
  418. print("flashkw: read1page: read addr %#lux:"
  419. " must read aligned page\n", addr);
  420. return -1;
  421. }
  422. /* satisfy request from cache if possible */
  423. if (f == cache.flif && page == cache.pageno &&
  424. r->pagesize == cache.pgsize) {
  425. memmove(buf, cache.page, r->pagesize);
  426. return 0;
  427. }
  428. if (Debug)
  429. print("flashkw: read offset %#lux addr %#lux page %#lux\n",
  430. offset, addr, page);
  431. nandclaim(f);
  432. nandcmd(f, Read);
  433. nandaddr(f, addr>>0);
  434. nandaddr(f, addr>>8);
  435. nandaddr(f, page>>0);
  436. nandaddr(f, page>>8);
  437. nandaddr(f, page>>16);
  438. nandcmd(f, Readstart);
  439. microdelay(50);
  440. nandreadn(f, buf, r->pagesize);
  441. nandreadn(f, oob, r->spares);
  442. nandunclaim(f);
  443. /* verify/correct data. last 8*3 bytes is ecc, per 256 bytes. */
  444. p = buf;
  445. assert(r->spares >= 24);
  446. eccp = oob + r->spares - 24;
  447. for(i = 0; i < r->pagesize / 256; i++) {
  448. w = eccp[0] << 8 | eccp[1] << 0 | eccp[2] << 16;
  449. eccp += 3;
  450. switch(nandecccorrect(p, nandecc(p), &w, 1)) {
  451. case NandEccErrorBad:
  452. print("(page %d)\n", i);
  453. return -1;
  454. case NandEccErrorOneBit:
  455. case NandEccErrorOneBitInEcc:
  456. print("(page %d)\n", i);
  457. /* fall through */
  458. case NandEccErrorGood:
  459. break;
  460. }
  461. p += 256;
  462. }
  463. flcachepage(f, page, buf);
  464. return 0;
  465. }
  466. /*
  467. * read a page at offset into cache, copy fragment from buf into it
  468. * at pagoff, and rewrite that page.
  469. */
  470. static int
  471. rewrite(Flash *f, ulong offset, ulong pagoff, void *buf, ulong size)
  472. {
  473. if (read1page(f, offset, cache.page) < 0)
  474. return -1;
  475. memmove(&cache.page[pagoff], buf, size);
  476. return write1page(f, offset, cache.page);
  477. }
  478. /* there are no alignment constraints on offset, buf, nor n */
  479. static int
  480. write(Flash *f, ulong offset, void *buf, long n)
  481. {
  482. uint un, frag, pagoff;
  483. ulong pgsize;
  484. uchar *p;
  485. Flashregion *r = &f->regions[0];
  486. if(n <= 0)
  487. panic("flashkw: write: non-positive count %ld", n);
  488. un = n;
  489. assert(r->pagesize != 0);
  490. pgsize = r->pagesize;
  491. /* if a partial first page exists, update the first page with it. */
  492. p = buf;
  493. pagoff = offset % pgsize;
  494. if (pagoff != 0) {
  495. frag = pgsize - pagoff;
  496. if (frag > un) /* might not extend to end of page */
  497. frag = un;
  498. if (rewrite(f, offset - pagoff, pagoff, p, frag) < 0)
  499. return -1;
  500. offset += frag;
  501. p += frag;
  502. un -= frag;
  503. }
  504. /* copy whole pages */
  505. while (un >= pgsize) {
  506. if (write1page(f, offset, p) < 0)
  507. return -1;
  508. offset += pgsize;
  509. p += pgsize;
  510. un -= pgsize;
  511. }
  512. /* if a partial last page exists, update the last page with it. */
  513. if (un > 0)
  514. return rewrite(f, offset, 0, p, un);
  515. return 0;
  516. }
  517. /* there are no alignment constraints on offset, buf, nor n */
  518. static int
  519. read(Flash *f, ulong offset, void *buf, long n)
  520. {
  521. uint un, frag, pagoff;
  522. ulong pgsize;
  523. uchar *p;
  524. Flashregion *r = &f->regions[0];
  525. if(n <= 0)
  526. panic("flashkw: read: non-positive count %ld", n);
  527. un = n;
  528. assert(r->pagesize != 0);
  529. pgsize = r->pagesize;
  530. /* if partial 1st page, read it into cache & copy fragment to buf */
  531. p = buf;
  532. pagoff = offset % pgsize;
  533. if (pagoff != 0) {
  534. frag = pgsize - pagoff;
  535. if (frag > un) /* might not extend to end of page */
  536. frag = un;
  537. if (read1page(f, offset - pagoff, cache.page) < 0)
  538. return -1;
  539. offset += frag;
  540. memmove(p, &cache.page[pagoff], frag);
  541. p += frag;
  542. un -= frag;
  543. }
  544. /* copy whole pages */
  545. while (un >= pgsize) {
  546. if (read1page(f, offset, p) < 0)
  547. return -1;
  548. offset += pgsize;
  549. p += pgsize;
  550. un -= pgsize;
  551. }
  552. /* if partial last page, read into cache & copy initial fragment to buf */
  553. if (un > 0) {
  554. if (read1page(f, offset, cache.page) < 0)
  555. return -1;
  556. memmove(p, cache.page, un);
  557. }
  558. return 0;
  559. }
  560. static int
  561. reset(Flash *f)
  562. {
  563. if(f->data != nil)
  564. return 1;
  565. f->write = write;
  566. f->read = read;
  567. f->eraseall = nil;
  568. f->erasezone = erasezone;
  569. f->suspend = nil;
  570. f->resume = nil;
  571. f->sort = "nand";
  572. cache.pageno = Nopage;
  573. return idchip(f);
  574. }
  575. void
  576. flashkwlink(void)
  577. {
  578. addflashcard("nand", reset);
  579. }