flashkw.c 13 KB

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