devflash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. enum {
  8. Nflash = 2,
  9. Maxwchunk= 1024, /* maximum chunk written by one call to falg->write */
  10. };
  11. /*
  12. * Flashes are either 8 or 16 bits wide. On some installations (e.g., the
  13. * bitsy, they are interleaved: address 0 is in the first chip, address 2
  14. * on the second, address 4 on the first, etc.
  15. * We define Funit as the unit that matches the width of a single flash chip,
  16. * so Funit is either `uchar' or `ushort' (I haven't seen 32-bit wide flashes),
  17. * and we define Fword as the unit that matches a set of interleaved Funits.
  18. * We access interleaved flashes simultaneously, by doing single reads and
  19. * writes to both. The macro `mirror' takes a command and replicates it for
  20. * this purpose.
  21. * The Blast board has a non-interleaved 16-bit wide flash. When doing
  22. * writes to it, we must swap bytes.
  23. */
  24. typedef struct FlashAlg FlashAlg;
  25. typedef struct Flash Flash;
  26. typedef struct FlashRegion FlashRegion;
  27. #ifdef WIDTH8
  28. typedef uchar Funit; /* Width of the flash (uchar or ushort) */
  29. # define toendian(x) (x) /* Little or big endianness */
  30. # define fromendian(x) (x)
  31. # define reg(x) ((x)<<1)
  32. # ifdef INTERLEAVED
  33. # define mirror(x) ((x)<<8|(x)) /* Double query for interleaved flashes */
  34. typedef ushort Fword; /* Width after interleaving */
  35. # define Wshift 1
  36. # else
  37. # define mirror(x) (x)
  38. typedef uchar Fword;
  39. # define Wshift 0
  40. # endif
  41. #else
  42. typedef ushort Funit;
  43. # define toendian(x) ((x)<<8)
  44. # define fromendian(x) ((x)>>8)
  45. # define reg(x) (x)
  46. # ifdef INTERLEAVED
  47. # define mirror(x) (toendian(x)<<16|toendian(x))
  48. typedef ulong Fword;
  49. # define Wshift 2
  50. # else
  51. # define mirror(x) toendian(x)
  52. typedef ushort Fword;
  53. # define Wshift 1
  54. # endif
  55. #endif
  56. /* this defines a contiguous set of erase blocks of one size */
  57. struct FlashRegion
  58. {
  59. ulong addr; /* start of region */
  60. ulong end; /* end of region + 1 */
  61. ulong n; /* number of blocks */
  62. ulong size; /* size of each block */
  63. };
  64. struct Flash
  65. {
  66. ISAConf; /* contains size */
  67. RWlock;
  68. Fword *p;
  69. ushort algid; /* access algorithm */
  70. FlashAlg *alg;
  71. ushort manid; /* manufacturer id */
  72. ushort devid; /* device id */
  73. int wbsize; /* size of write buffer */
  74. ulong nr; /* number of regions */
  75. uchar bootprotect;
  76. ulong offset; /* beginning offset of this flash */
  77. FlashRegion r[32];
  78. };
  79. /* this defines a particular access algorithm */
  80. struct FlashAlg
  81. {
  82. int id;
  83. char *name;
  84. void (*identify)(Flash*); /* identify device */
  85. void (*erase)(Flash*, ulong); /* erase a region */
  86. void (*write)(Flash*, void*, long, ulong); /* write a region */
  87. };
  88. static void ise_id(Flash*);
  89. static void ise_erase(Flash*, ulong);
  90. static void ise_write(Flash*, void*, long, ulong);
  91. static void afs_id(Flash*);
  92. static void afs_erase(Flash*, ulong);
  93. static void afs_write(Flash*, void*, long, ulong);
  94. static ulong blockstart(Flash*, ulong);
  95. static ulong blockend(Flash*, ulong);
  96. FlashAlg falg[] =
  97. {
  98. { 1, "Intel/Sharp Extended", ise_id, ise_erase, ise_write },
  99. { 2, "AMD/Fujitsu Standard", afs_id, afs_erase, afs_write },
  100. };
  101. Flash flashes[Nflash];
  102. /*
  103. * common flash interface
  104. */
  105. static uchar
  106. cfigetc(Flash *flash, int off)
  107. {
  108. uchar rv;
  109. flash->p[reg(0x55)] = mirror(0x98);
  110. rv = fromendian(flash->p[reg(off)]);
  111. flash->p[reg(0x55)] = mirror(0xFF);
  112. return rv;
  113. }
  114. static ushort
  115. cfigets(Flash *flash, int off)
  116. {
  117. return (cfigetc(flash, off+1)<<8)|cfigetc(flash, off);
  118. }
  119. static ulong
  120. cfigetl(Flash *flash, int off)
  121. {
  122. return (cfigetc(flash, off+3)<<24)|(cfigetc(flash, off+2)<<16)|
  123. (cfigetc(flash, off+1)<<8)|cfigetc(flash, off);
  124. }
  125. static void
  126. cfiquery(Flash *flash)
  127. {
  128. uchar q, r, y;
  129. ulong x, addr;
  130. q = cfigetc(flash, 0x10);
  131. r = cfigetc(flash, 0x11);
  132. y = cfigetc(flash, 0x12);
  133. if(q != 'Q' || r != 'R' || y != 'Y'){
  134. print("cfi query failed: %ux %ux %ux\n", q, r, y);
  135. return;
  136. }
  137. flash->algid = cfigetc(flash, 0x13);
  138. flash->size = (sizeof(Fword)/sizeof(Funit)) * (1<<(cfigetc(flash, 0x27)));
  139. flash->wbsize = (sizeof(Fword)/sizeof(Funit)) * (1<<(cfigetc(flash, 0x2a)));
  140. flash->nr = cfigetc(flash, 0x2c);
  141. if(flash->nr > nelem(flash->r)){
  142. print("cfi reports > %d regions\n", nelem(flash->r));
  143. flash->nr = nelem(flash->r);
  144. }
  145. addr = 0;
  146. for(q = 0; q < flash->nr; q++){
  147. x = cfigetl(flash, q+0x2d);
  148. flash->r[q].size = (sizeof(Fword)/sizeof(Funit)) * 256 * (x>>16);
  149. flash->r[q].n = (x&0xffff)+1;
  150. flash->r[q].addr = addr;
  151. addr += flash->r[q].size*flash->r[q].n;
  152. flash->r[q].end = addr;
  153. }
  154. }
  155. /*
  156. * flash device interface
  157. */
  158. enum
  159. {
  160. Qtopdir,
  161. Q2nddir,
  162. Qfctl,
  163. Qfdata,
  164. Maxpart= 8,
  165. };
  166. typedef struct FPart FPart;
  167. struct FPart
  168. {
  169. Flash *flash;
  170. char *name;
  171. char *ctlname;
  172. ulong start;
  173. ulong end;
  174. };
  175. static FPart part[Maxpart];
  176. #define FQID(p,q) ((p)<<8|(q))
  177. #define FTYPE(q) ((q) & 0xff)
  178. #define FPART(q) (&part[(q) >>8])
  179. static int
  180. gen(Chan *c, char*, Dirtab*, int, int i, Dir *dp)
  181. {
  182. Qid q;
  183. FPart *fp;
  184. q.vers = 0;
  185. /* top level directory contains the name of the network */
  186. if(c->qid.path == Qtopdir){
  187. switch(i){
  188. case DEVDOTDOT:
  189. q.path = Qtopdir;
  190. q.type = QTDIR;
  191. devdir(c, q, "#F", 0, eve, DMDIR|0555, dp);
  192. break;
  193. case 0:
  194. q.path = Q2nddir;
  195. q.type = QTDIR;
  196. devdir(c, q, "flash", 0, eve, DMDIR|0555, dp);
  197. break;
  198. default:
  199. return -1;
  200. }
  201. return 1;
  202. }
  203. /* second level contains all partitions and their control files */
  204. switch(i) {
  205. case DEVDOTDOT:
  206. q.path = Qtopdir;
  207. q.type = QTDIR;
  208. devdir(c, q, "#F", 0, eve, DMDIR|0555, dp);
  209. break;
  210. default:
  211. if(i >= 2*Maxpart)
  212. return -1;
  213. fp = &part[i>>1];
  214. if(fp->name == nil)
  215. return 0;
  216. if(i & 1){
  217. q.path = FQID(i>>1, Qfdata);
  218. q.type = QTFILE;
  219. devdir(c, q, fp->name, fp->end-fp->start, eve, 0660, dp);
  220. } else {
  221. q.path = FQID(i>>1, Qfctl);
  222. q.type = QTFILE;
  223. devdir(c, q, fp->ctlname, 0, eve, 0660, dp);
  224. }
  225. break;
  226. }
  227. return 1;
  228. }
  229. static Flash *
  230. findflash(ulong addr)
  231. {
  232. Flash *flash;
  233. for (flash = flashes; flash < flashes + Nflash; flash++)
  234. if(addr >= flash->offset && addr < flash->offset + flash->size)
  235. return flash;
  236. return nil;
  237. }
  238. static FPart*
  239. findpart(char *name)
  240. {
  241. int i;
  242. for(i = 0; i < Maxpart; i++)
  243. if(part[i].name != nil && strcmp(name, part[i].name) == 0)
  244. break;
  245. if(i >= Maxpart)
  246. return nil;
  247. return &part[i];
  248. }
  249. static void
  250. addpart(FPart *fp, char *name, ulong start, ulong end)
  251. {
  252. int i;
  253. char ctlname[64];
  254. Flash *flash;
  255. if (start > end)
  256. error(Ebadarg);
  257. if(fp == nil){
  258. flash = findflash(start);
  259. if (flash == nil || end > flash->offset + flash->size)
  260. error(Ebadarg);
  261. start -= flash->offset;
  262. end -= flash->offset;
  263. } else {
  264. start += fp->start;
  265. end += fp->start;
  266. if(start >= fp->end || end > fp->end){
  267. error(Ebadarg);
  268. }
  269. flash = fp->flash;
  270. }
  271. if(blockstart(flash, start) != start)
  272. error("must start on erase boundary");
  273. if(blockstart(flash, end) != end && end != flash->size)
  274. error("must end on erase boundary");
  275. fp = findpart(name);
  276. if(fp != nil)
  277. error(Eexist);
  278. for(i = 0; i < Maxpart; i++)
  279. if(part[i].name == nil)
  280. break;
  281. if(i == Maxpart)
  282. error("no more partitions");
  283. fp = &part[i];
  284. kstrdup(&fp->name, name);
  285. snprint(ctlname, sizeof ctlname, "%sctl", name);
  286. kstrdup(&fp->ctlname, ctlname);
  287. fp->flash = flash;
  288. fp->start = start;
  289. fp->end = end;
  290. }
  291. static void
  292. rempart(FPart *fp)
  293. {
  294. char *p, *cp;
  295. p = fp->name;
  296. fp->name = nil;
  297. cp = fp->ctlname;
  298. fp->ctlname = nil;
  299. free(p);
  300. free(cp);
  301. }
  302. void
  303. flashinit(void)
  304. {
  305. int i, ctlrno;
  306. char *fname;
  307. ulong offset;
  308. Flash *flash;
  309. offset = 0;
  310. for (ctlrno = 0; ctlrno < Nflash; ctlrno++){
  311. flash = flashes + ctlrno;
  312. if(isaconfig("flash", ctlrno, flash) == 0)
  313. continue;
  314. flash->p = (Fword*)flash->mem;
  315. cfiquery(flash);
  316. for(i = 0; i < nelem(falg); i++)
  317. if(flash->algid == falg[i].id){
  318. flash->alg = &falg[i];
  319. (*flash->alg->identify)(flash);
  320. break;
  321. }
  322. flash->bootprotect = 1;
  323. flash->offset = offset;
  324. fname = malloc(8);
  325. sprint(fname, "flash%d", ctlrno);
  326. addpart(nil, fname, offset, offset + flash->size);
  327. offset += flash->size;
  328. }
  329. }
  330. static Chan*
  331. flashattach(char* spec)
  332. {
  333. return devattach('F', spec);
  334. }
  335. static Walkqid*
  336. flashwalk(Chan *c, Chan *nc, char **name, int nname)
  337. {
  338. return devwalk(c, nc, name, nname, nil, 0, gen);
  339. }
  340. static int
  341. flashstat(Chan *c, uchar *db, int n)
  342. {
  343. return devstat(c, db, n, nil, 0, gen);
  344. }
  345. static Chan*
  346. flashopen(Chan* c, int omode)
  347. {
  348. omode = openmode(omode);
  349. if(strcmp(up->user, eve)!=0)
  350. error(Eperm);
  351. return devopen(c, omode, nil, 0, gen);
  352. }
  353. static void
  354. flashclose(Chan*)
  355. {
  356. }
  357. static long
  358. flashctlread(FPart *fp, void* a, long n, vlong off)
  359. {
  360. char *buf, *p, *e;
  361. int i;
  362. ulong addr, end;
  363. Flash *flash;
  364. flash = fp->flash;
  365. buf = smalloc(1024);
  366. e = buf + 1024;
  367. p = seprint(buf, e, "0x%-9lux 0x%-9lux 0x%-9lux 0x%-9x 0x%-9ux 0x%-9ux\n",
  368. flash->offset, fp->start, fp->end-fp->start, flash->wbsize, flash->manid, flash->devid);
  369. addr = fp->start;
  370. for(i = 0; i < flash->nr && addr < fp->end; i++)
  371. if(flash->r[i].addr <= addr && flash->r[i].end > addr){
  372. if(fp->end <= flash->r[i].end)
  373. end = fp->end;
  374. else
  375. end = flash->r[i].end;
  376. p = seprint(p, e, "0x%-9lux 0x%-9lux 0x%-9lux\n", addr,
  377. (end-addr)/flash->r[i].size, flash->r[i].size);
  378. addr = end;
  379. }
  380. n = readstr(off, a, n, buf);
  381. free(buf);
  382. return n;
  383. }
  384. static long
  385. flashdataread(FPart *fp, void* a, long n, vlong off)
  386. {
  387. Flash *flash;
  388. flash = fp->flash;
  389. rlock(flash);
  390. if(waserror()){
  391. runlock(flash);
  392. nexterror();
  393. }
  394. if(fp->name == nil)
  395. error("partition vanished");
  396. if(!iseve())
  397. error(Eperm);
  398. off += fp->start;
  399. if(off >= fp->end)
  400. n = 0;
  401. if(off+n >= fp->end)
  402. n = fp->end - off;
  403. if(n > 0)
  404. memmove(a, ((uchar*)flash->mem)+off, n);
  405. runlock(flash);
  406. poperror();
  407. return n;
  408. }
  409. static long
  410. flashread(Chan* c, void* a, long n, vlong off)
  411. {
  412. int t;
  413. if(c->qid.type == QTDIR)
  414. return devdirread(c, a, n, nil, 0, gen);
  415. t = FTYPE(c->qid.path);
  416. switch(t){
  417. default:
  418. error(Eperm);
  419. case Qfctl:
  420. n = flashctlread(FPART(c->qid.path), a, n, off);
  421. break;
  422. case Qfdata:
  423. n = flashdataread(FPART(c->qid.path), a, n, off);
  424. break;
  425. }
  426. return n;
  427. }
  428. static void
  429. bootprotect(ulong addr)
  430. {
  431. FlashRegion *r;
  432. Flash *flash;
  433. flash = findflash(addr);
  434. if (flash == nil)
  435. error(Ebadarg);
  436. if(flash->bootprotect == 0)
  437. return;
  438. if(flash->nr == 0)
  439. error("writing over boot loader disallowed");
  440. r = flash->r;
  441. if(addr >= r->addr && addr < r->addr + r->size)
  442. error("writing over boot loader disallowed");
  443. }
  444. static ulong
  445. blockstart(Flash *flash, ulong addr)
  446. {
  447. FlashRegion *r, *e;
  448. ulong x;
  449. r = flash->r;
  450. for(e = &flash->r[flash->nr]; r < e; r++){
  451. if(addr >= r->addr && addr < r->end){
  452. x = addr - r->addr;
  453. x /= r->size;
  454. return r->addr + x*r->size;
  455. }
  456. }
  457. return (ulong)-1;
  458. }
  459. static ulong
  460. blockend(Flash *flash, ulong addr)
  461. {
  462. FlashRegion *r, *e;
  463. ulong x;
  464. r = flash->r;
  465. for(e = &flash->r[flash->nr]; r < e; r++)
  466. if(addr >= r->addr && addr < r->end){
  467. x = addr - r->addr;
  468. x /= r->size;
  469. return r->addr + (x+1)*r->size;
  470. }
  471. return (ulong)-1;
  472. }
  473. static long
  474. flashctlwrite(FPart *fp, char *p, long n)
  475. {
  476. Cmdbuf *cmd;
  477. ulong off;
  478. Flash *flash;
  479. if(fp == nil)
  480. panic("flashctlwrite");
  481. flash = fp->flash;
  482. cmd = parsecmd(p, n);
  483. wlock(flash);
  484. if(waserror()){
  485. wunlock(flash);
  486. nexterror();
  487. }
  488. if(strcmp(cmd->f[0], "erase") == 0){
  489. switch(cmd->nf){
  490. case 2:
  491. /* erase a single block in the partition */
  492. off = atoi(cmd->f[1]);
  493. off += fp->start;
  494. if(off >= fp->end)
  495. error("region not in partition");
  496. if(off != blockstart(flash, off))
  497. error("erase must be a block boundary");
  498. bootprotect(off);
  499. (*flash->alg->erase)(flash, off);
  500. break;
  501. case 1:
  502. /* erase the whole partition */
  503. bootprotect(fp->start);
  504. for(off = fp->start; off < fp->end; off = blockend(flash, off))
  505. (*flash->alg->erase)(flash, off);
  506. break;
  507. default:
  508. error(Ebadarg);
  509. }
  510. } else if(strcmp(cmd->f[0], "add") == 0){
  511. if(cmd->nf != 4)
  512. error(Ebadarg);
  513. addpart(fp, cmd->f[1], strtoul(cmd->f[2], nil, 0), strtoul(cmd->f[3], nil, 0));
  514. } else if(strcmp(cmd->f[0], "remove") == 0){
  515. rempart(fp);
  516. } else if(strcmp(cmd->f[0], "protectboot") == 0){
  517. if(cmd->nf == 0 || strcmp(cmd->f[1], "off") != 0)
  518. flash->bootprotect = 1;
  519. else
  520. flash->bootprotect = 0;
  521. } else
  522. error(Ebadarg);
  523. poperror();
  524. wunlock(flash);
  525. free(cmd);
  526. return n;
  527. }
  528. static long
  529. flashdatawrite(FPart *fp, uchar *p, long n, long off)
  530. {
  531. uchar *end;
  532. int m;
  533. int on;
  534. long ooff;
  535. uchar *buf;
  536. Flash *flash;
  537. if(fp == nil)
  538. panic("flashdatawrite");
  539. flash = fp->flash;
  540. buf = nil;
  541. wlock(flash);
  542. if(waserror()){
  543. wunlock(flash);
  544. if(buf != nil)
  545. free(buf);
  546. nexterror();
  547. }
  548. if(fp->name == nil)
  549. error("partition vanished");
  550. if(!iseve())
  551. error(Eperm);
  552. /* can't cross partition boundaries */
  553. off += fp->start;
  554. if(off >= fp->end || off+n > fp->end || n <= 0)
  555. error(Ebadarg);
  556. /* make sure we're not writing the boot sector */
  557. bootprotect(off);
  558. on = n;
  559. /*
  560. * get the data into kernel memory to avoid faults during writing.
  561. * if write is not on a quad boundary or not a multiple of 4 bytes,
  562. * extend with data already in flash.
  563. */
  564. buf = smalloc(n+8);
  565. m = off & 3;
  566. if(m){
  567. *(ulong*)buf = flash->p[off>>Wshift];
  568. n += m;
  569. off -= m;
  570. }
  571. if(n & 3){
  572. n -= n & 3;
  573. *(ulong*)(&buf[n]) = flash->p[(off+n)>>Wshift];
  574. n += 4;
  575. }
  576. memmove(&buf[m], p, on);
  577. /* (*flash->alg->write) can't cross blocks */
  578. ooff = off;
  579. p = buf;
  580. for(end = p + n; p < end; p += m){
  581. m = blockend(flash, off) - off;
  582. if(m > end - p)
  583. m = end - p;
  584. if(m > Maxwchunk)
  585. m = Maxwchunk;
  586. (*flash->alg->write)(flash, p, m, off);
  587. off += m;
  588. }
  589. /* make sure write succeeded */
  590. if(memcmp(buf, &flash->p[ooff>>Wshift], n) != 0)
  591. error("written bytes don't match");
  592. wunlock(flash);
  593. free(buf);
  594. poperror();
  595. return on;
  596. }
  597. static long
  598. flashwrite(Chan* c, void* a, long n, vlong off)
  599. {
  600. int t;
  601. if(c->qid.type == QTDIR)
  602. error(Eperm);
  603. if(!iseve())
  604. error(Eperm);
  605. t = FTYPE(c->qid.path);
  606. switch(t){
  607. default:
  608. panic("flashwrite");
  609. case Qfctl:
  610. n = flashctlwrite(FPART(c->qid.path), a, n);
  611. break;
  612. case Qfdata:
  613. n = flashdatawrite(FPART(c->qid.path), a, n, off);
  614. break;
  615. }
  616. return n;
  617. }
  618. Dev flashdevtab = {
  619. 'F',
  620. "flash",
  621. devreset,
  622. flashinit,
  623. devshutdown,
  624. flashattach,
  625. flashwalk,
  626. flashstat,
  627. flashopen,
  628. devcreate,
  629. flashclose,
  630. flashread,
  631. devbread,
  632. flashwrite,
  633. devbwrite,
  634. devremove,
  635. devwstat,
  636. };
  637. enum
  638. {
  639. /* status register */
  640. ISEs_lockerr= 1<<1,
  641. ISEs_powererr= 1<<3,
  642. ISEs_progerr= 1<<4,
  643. ISEs_eraseerr= 1<<5,
  644. ISEs_ready= 1<<7,
  645. ISEs_err= (ISEs_lockerr|ISEs_powererr|ISEs_progerr|ISEs_eraseerr),
  646. /* extended status register */
  647. ISExs_bufavail= 1<<7,
  648. };
  649. /* intel/sharp extended command set */
  650. static void
  651. ise_reset(Flash* flash)
  652. {
  653. flash->p[reg(0xaa)] = mirror(0xff); /* reset */
  654. }
  655. static void
  656. ise_id(Flash* flash)
  657. {
  658. ise_reset(flash);
  659. flash->p[reg(0xaaa)] = mirror(0x90); /* uncover vendor info */
  660. flash->manid = fromendian(flash->p[reg(0x0)]);
  661. flash->devid = fromendian(flash->p[reg(0x1)]);
  662. ise_reset(flash);
  663. }
  664. static void
  665. ise_clearerror(Flash* flash)
  666. {
  667. flash->p[reg(0x200)] = mirror(0x50);
  668. }
  669. static void
  670. ise_error(int bank, ulong status)
  671. {
  672. char err[64];
  673. if(status & (ISEs_lockerr)){
  674. sprint(err, "flash%d: block locked %lux", bank, status);
  675. error(err);
  676. }
  677. if(status & (ISEs_powererr)){
  678. sprint(err, "flash%d: low prog voltage %lux", bank, status);
  679. error(err);
  680. }
  681. if(status & (ISEs_progerr|ISEs_eraseerr)){
  682. sprint(err, "flash%d: i/o error %lux", bank, status);
  683. error(err);
  684. }
  685. }
  686. static void
  687. ise_erase(Flash *flash, ulong addr)
  688. {
  689. ulong start;
  690. ulong x;
  691. addr >>= Wshift;
  692. flashprogpower(1);
  693. flash->p[addr] = mirror(0x20);
  694. flash->p[addr] = mirror(0xd0);
  695. start = m->ticks;
  696. do {
  697. x = fromendian(flash->p[addr]);
  698. if((x & mirror(ISEs_ready)) == mirror(ISEs_ready))
  699. break;
  700. } while(TK2MS(m->ticks-start) < 1500);
  701. flashprogpower(0);
  702. ise_clearerror(flash);
  703. ise_error(0, x);
  704. ise_error(1, x>>16);
  705. ise_reset(flash);
  706. }
  707. /*
  708. * the flash spec claimes writing goes faster if we use
  709. * the write buffer. We fill the write buffer and then
  710. * issue the write request. After the write request,
  711. * subsequent reads will yield the status register.
  712. *
  713. * returns the status, even on timeouts.
  714. *
  715. * NOTE: I tried starting back to back buffered writes
  716. * without reading the status in between, as the
  717. * flowchart in the intel data sheet suggests.
  718. * However, it always responded with an illegal
  719. * command sequence, so I must be missing something.
  720. * If someone learns better, please email me, though
  721. * I doubt it will be much faster. - presotto@bell-labs.com
  722. */
  723. static long
  724. ise_wbwrite(Flash *flash, Fword *p, int n, ulong off, ulong baddr, ulong *status)
  725. {
  726. Fword x;
  727. ulong start;
  728. int i;
  729. int s;
  730. /* put flash into write buffer mode */
  731. start = m->ticks;
  732. for(;;) {
  733. s = splhi();
  734. /* request write buffer mode */
  735. flash->p[baddr] = mirror(0xe8);
  736. /* look at extended status reg for status */
  737. if((flash->p[baddr] & mirror(1<<7)) == mirror(1<<7))
  738. break;
  739. splx(s);
  740. /* didn't work, keep trying for 2 secs */
  741. if(TK2MS(m->ticks-start) > 2000){
  742. /* set up to read status */
  743. flash->p[baddr] = mirror(0x70);
  744. *status = fromendian(flash->p[baddr]);
  745. pprint("write buffered cmd timed out\n");
  746. return -1;
  747. }
  748. }
  749. /* fill write buffer */
  750. flash->p[baddr] = mirror(n-1);
  751. for(i = 0; i < n; i++)
  752. flash->p[off+i] = *p++;
  753. /* program from buffer */
  754. flash->p[baddr] = mirror(0xd0);
  755. splx(s);
  756. /* wait till the programming is done */
  757. start = m->ticks;
  758. for(;;) {
  759. x = flash->p[baddr]; /* read status register */
  760. *status = fromendian(x);
  761. if((x & mirror(ISEs_ready)) == mirror(ISEs_ready))
  762. break;
  763. if(TK2MS(m->ticks-start) > 2000){
  764. pprint("read status timed out\n");
  765. return -1;
  766. }
  767. }
  768. if(x & mirror(ISEs_err))
  769. return -1;
  770. return n;
  771. }
  772. static void
  773. ise_write(Flash *flash, void *a, long n, ulong off)
  774. {
  775. Fword *p, *end;
  776. int i, wbsize;
  777. ulong x, baddr;
  778. /* everything in terms of Fwords */
  779. wbsize = flash->wbsize >> Wshift;
  780. baddr = blockstart(flash, off) >> Wshift;
  781. off >>= Wshift;
  782. n >>= Wshift;
  783. p = a;
  784. /* first see if write will succeed */
  785. for(i = 0; i < n; i++)
  786. if((p[i] & flash->p[off+i]) != p[i])
  787. error("flash needs erase");
  788. if(waserror()){
  789. ise_reset(flash);
  790. flashprogpower(0);
  791. nexterror();
  792. }
  793. flashprogpower(1);
  794. /*
  795. * use the first write to reach
  796. * a write buffer boundary. the intel maunal
  797. * says writes starting at wb boundaries
  798. * maximize speed.
  799. */
  800. i = wbsize - (off & (wbsize-1));
  801. for(end = p + n; p < end;){
  802. if(i > end - p)
  803. i = end - p;
  804. if(ise_wbwrite(flash, p, i, off, baddr, &x) < 0)
  805. break;
  806. off += i;
  807. p += i;
  808. i = wbsize;
  809. }
  810. ise_clearerror(flash);
  811. ise_error(0, x);
  812. ise_error(1, x>>16);
  813. ise_reset(flash);
  814. flashprogpower(0);
  815. poperror();
  816. }
  817. /* amd/fujitsu standard command set
  818. * I don't have an amd chipset to work with
  819. * so I'm loathe to write this yet. If someone
  820. * else does, please send it to me and I'll
  821. * incorporate it -- presotto@bell-labs.com
  822. */
  823. static void
  824. afs_reset(Flash *flash)
  825. {
  826. flash->p[reg(0xaa)] = mirror(0xf0); /* reset */
  827. }
  828. static void
  829. afs_id(Flash *flash)
  830. {
  831. afs_reset(flash);
  832. flash->p[reg(0xaa)] = mirror(0xf0); /* reset */
  833. flash->p[reg(0xaaa)] = mirror(0xaa); /* query vendor block */
  834. flash->p[reg(0x554)] = mirror(0x55);
  835. flash->p[reg(0xaaa)] = mirror(0x90);
  836. flash->manid = fromendian(flash->p[reg(0x00)]);
  837. afs_reset(flash);
  838. flash->p[reg(0xaaa)] = mirror(0xaa); /* query vendor block */
  839. flash->p[reg(0x554)] = mirror(0x55);
  840. flash->p[reg(0xaaa)] = mirror(0x90);
  841. flash->devid = fromendian(flash->p[reg(0x02)]);
  842. afs_reset(flash);
  843. }
  844. static void
  845. afs_erase(Flash*, ulong)
  846. {
  847. error("amd/fujistsu erase not implemented");
  848. }
  849. static void
  850. afs_write(Flash*, void*, long, ulong)
  851. {
  852. error("amd/fujistsu write not implemented");
  853. }