i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * basic read/write interface to PXA25x I⁲C bus (master mode)
  3. * 7 bit addressing only.
  4. * TO DO:
  5. * - enable unit clock
  6. */
  7. #include "u.h"
  8. #include "../port/lib.h"
  9. #include "mem.h"
  10. #include "dat.h"
  11. #include "fns.h"
  12. #include "../port/error.h"
  13. #include "io.h"
  14. typedef struct Ctlr Ctlr;
  15. typedef struct I2Cregs I2Cregs;
  16. struct I2Cregs {
  17. ulong ibmr; /* bus monitor */
  18. ulong pad0;
  19. ulong idbr; /* data buffer */
  20. ulong pad1;
  21. ulong icr; /* control */
  22. ulong pad2;
  23. ulong isr; /* status */
  24. ulong pad3;
  25. ulong isar; /* slave address */
  26. };
  27. enum {
  28. /* ibmr */
  29. Scls= 1<<1, /* SCL pin status */
  30. Sdas= 1<<0, /* SDA pin status */
  31. /* icr */
  32. Fm= 1<<15, /* =0, 100 kb/sec; =1, 400 kb/sec */
  33. Ur= 1<<14, /* reset the i2c unit only */
  34. Sadie= 1<<13, /* slave address detected interrupt enable */
  35. Aldie= 1<<12, /* arbitration loss detected interrupt enable (master mode) */
  36. Ssdie= 1<<11, /* stop detected interrupt enable (slave mode) */
  37. Beie= 1<<10, /* bus error interrupt enable */
  38. Irfie= 1<<9, /* idbr receive full, interrupt enable */
  39. Iteie= 1<<8, /* idbr transmit empty interrupt enable */
  40. Gcd= 1<<7, /* disable response to general call message (slave); must be set if master uses g.c. */
  41. Scle= 1<<6, /* SCL enable: enable clock output for master mode */
  42. Iue= 1<<5, /* enable i2c (default: slave) */
  43. Ma= 1<<4, /* master abort (send STOP without data) */
  44. Tb= 1<<3, /* transfer byte on i2c bus */
  45. Ack= 0<<2,
  46. Nak= 1<<2,
  47. Stop= 1<<1, /* send a stop */
  48. Start= 1<<0, /* send a stop */
  49. /* isr */
  50. Bed= 1<<10, /* bus error detected */
  51. Sad= 1<<9, /* slave address detected */
  52. Gcad= 1<<8, /* general call address detected */
  53. Irf= 1<<7, /* idbr receive full */
  54. Ite= 1<<6, /* idbr transmit empty */
  55. Ald= 1<<5, /* arbitration loss detected (multi-master) */
  56. Ssd= 1<<4, /* slave stop detected */
  57. Ibb= 1<<3, /* i2c bus is busy */
  58. Ub= 1<<2, /* unit is busy (between start and stop) */
  59. Nakrcv= 1<<1, /* nak received or sent a NAK */
  60. Rwm= 1<<0, /* =0, master transmit (or slave receive); =1, master receive (or slave transmit) */
  61. Err= Bed | Ssd,
  62. /* isar address (0x7F bits) */
  63. /* others */
  64. Rbit = 1<<0, /* bit in address byte denoting read */
  65. Wbit= 0<<0,
  66. MaxIO = 8192, /* largest transfer done at once (can change) */
  67. MaxSA= 2, /* largest subaddress; could be FIFOsize */
  68. Bufsize = MaxIO, /* subaddress bytes don't go in buffer */
  69. Freq = 0, /* set to Fm for high-speed */
  70. // I2Ctimeout = 125, /* msec (can change) */
  71. I2Ctimeout = 10000, /* msec when Chatty */
  72. Chatty = 0,
  73. };
  74. #define DPRINT if(Chatty)print
  75. /*
  76. * I2C software structures
  77. */
  78. struct Ctlr {
  79. Lock;
  80. QLock io;
  81. int init;
  82. int polling; /* eg, when running before system set up */
  83. I2Cregs* regs; /* hardware registers */
  84. /* controller state (see below) */
  85. int status;
  86. int phase;
  87. Rendez r;
  88. /* transfer parameters */
  89. int addr;
  90. int salen; /* bytes remaining of subaddress */
  91. int offset; /* sub-addressed offset */
  92. int cntl; /* everything but transfer length */
  93. int rdcount; /* requested read transfer size */
  94. Block* b;
  95. };
  96. enum {
  97. /* Ctlr.state */
  98. Idle,
  99. Done,
  100. Failed,
  101. Busy,
  102. Address,
  103. Subaddress,
  104. Read,
  105. Write,
  106. Halting,
  107. };
  108. static Ctlr i2cctlr[1];
  109. static void interrupt(Ureg*, void*);
  110. static int readyxfer(Ctlr*, int);
  111. static void rxstart(Ctlr*);
  112. static void txstart(Ctlr*);
  113. static void stopxfer(Ctlr*);
  114. static void txoffset(Ctlr*, ulong, int);
  115. static int idlectlr(Ctlr*);
  116. static void
  117. i2cdump(char *t, I2Cregs *i2c)
  118. {
  119. iprint("i2c %s: ibmr=%.4lux icr=%.4lux isr=%.4lux\n", t, i2c->ibmr, i2c->icr, i2c->isr);
  120. }
  121. static void
  122. initialise(I2Cregs *i2c, int eintr)
  123. {
  124. int ctl;
  125. /* initialisation (see p. 9-11 on) */
  126. i2c->isar = 0;
  127. ctl = Freq | Gcd | Scle | Iue;
  128. if(eintr)
  129. ctl |= Beie | Irfie; /* Iteie set by txstart */
  130. i2c->icr = ctl;
  131. if(Chatty)
  132. iprint("ctl=%4.4ux icr=%4.4lux\n", ctl, i2c->icr);
  133. }
  134. /*
  135. * called by the reset routine of any driver using the IIC
  136. */
  137. void
  138. i2csetup(int polling)
  139. {
  140. I2Cregs *i2c;
  141. Ctlr *ctlr;
  142. ctlr = i2cctlr;
  143. ctlr->polling = polling;
  144. i2c = KADDR(PHYSI2C);
  145. ctlr->regs = i2c;
  146. if(!polling){
  147. if(ctlr->init == 0){
  148. initialise(i2c, 1);
  149. ctlr->init = 1;
  150. intrenable(IRQ, IRQi2c, interrupt, i2cctlr, "i2c");
  151. if(Chatty)
  152. i2cdump("init", i2c);
  153. }
  154. }else
  155. initialise(i2c, 0);
  156. }
  157. static void
  158. done(Ctlr *ctlr)
  159. {
  160. ctlr->phase = Done;
  161. wakeup(&ctlr->r);
  162. }
  163. static void
  164. failed(Ctlr *ctlr)
  165. {
  166. ctlr->phase = Failed;
  167. wakeup(&ctlr->r);
  168. }
  169. static void
  170. interrupt(Ureg*, void *arg)
  171. {
  172. int sts, idl;
  173. Ctlr *ctlr;
  174. Block *b;
  175. I2Cregs *i2c;
  176. char xx[12];
  177. ctlr = arg;
  178. i2c = ctlr->regs;
  179. idl = (i2c->ibmr & 3) == 3;
  180. if(Chatty && ctlr->phase != Read && ctlr->phase != Write){
  181. snprint(xx, sizeof(xx), "intr %d", ctlr->phase);
  182. i2cdump(xx, i2c);
  183. }
  184. sts = i2c->isr;
  185. if(sts & (Bed | Sad | Gcad | Ald))
  186. iprint("i2c: unexpected status: %.4ux", sts);
  187. i2c->isr = sts;
  188. ctlr->status = sts;
  189. i2c->icr &= ~(Start | Stop | Nak | Ma | Iteie);
  190. if(sts & Err){
  191. failed(ctlr);
  192. return;
  193. }
  194. switch(ctlr->phase){
  195. default:
  196. iprint("i2c: unexpected interrupt: p-%d s=%.4ux\n", ctlr->phase, sts);
  197. break;
  198. case Halting:
  199. ctlr->phase = Idle;
  200. break;
  201. case Subaddress:
  202. if(ctlr->salen){
  203. /* push out next byte of subaddress */
  204. ctlr->salen -= 8;
  205. i2c->idbr = ctlr->offset >> ctlr->salen;
  206. i2c->icr |= Aldie | Tb | Iteie;
  207. break;
  208. }
  209. /* subaddress finished */
  210. if(ctlr->cntl & Rbit){
  211. /* must readdress if reading to change mode */
  212. i2c->idbr = (ctlr->addr << 1) | Rbit;
  213. i2c->icr |= Start | Tb | Iteie;
  214. ctlr->phase = Address; /* readdress */
  215. break;
  216. }
  217. /* FALL THROUGH if writing */
  218. case Address:
  219. /* if not sub-addressed, rxstart/txstart */
  220. if(ctlr->cntl & Rbit)
  221. rxstart(ctlr);
  222. else
  223. txstart(ctlr);
  224. break;
  225. case Read:
  226. b = ctlr->b;
  227. if(b == nil)
  228. panic("i2c: no buffer");
  229. /* master receive: next byte */
  230. if(sts & Irf){
  231. ctlr->rdcount--;
  232. if(b->wp < b->lim)
  233. *b->wp++ = i2c->idbr;
  234. }
  235. if(ctlr->rdcount <= 0 || sts & Nakrcv || idl){
  236. if(Chatty)
  237. iprint("done: %.4ux\n", sts);
  238. done(ctlr);
  239. break;
  240. }
  241. rxstart(ctlr);
  242. break;
  243. case Write:
  244. b = ctlr->b;
  245. if(b == nil)
  246. panic("i2c: no buffer");
  247. /* account for data transmitted */
  248. if(BLEN(b) <= 0 || sts & Nakrcv){
  249. done(ctlr);
  250. break;
  251. }
  252. txstart(ctlr);
  253. break;
  254. }
  255. }
  256. static int
  257. isdone(void *a)
  258. {
  259. return ((Ctlr*)a)->phase < Busy;
  260. }
  261. static int
  262. i2cerror(char *s)
  263. {
  264. DPRINT("i2c error: %s\n", s);
  265. if(up)
  266. error(s);
  267. /* no current process, don't call error */
  268. return -1;
  269. }
  270. static char*
  271. startxfer(I2Cdev *d, int op, Block *b, int n, ulong offset)
  272. {
  273. I2Cregs *i2c;
  274. Ctlr *ctlr;
  275. int i, p, s;
  276. ctlr = i2cctlr;
  277. if(up){
  278. qlock(&ctlr->io);
  279. if(waserror()){
  280. qunlock(&ctlr->io);
  281. nexterror();
  282. }
  283. }
  284. ilock(ctlr);
  285. if(!idlectlr(ctlr)){
  286. iunlock(ctlr);
  287. if(up)
  288. error("bus confused");
  289. return "bus confused";
  290. }
  291. if(ctlr->phase >= Busy)
  292. panic("i2c: ctlr busy");
  293. ctlr->cntl = op;
  294. ctlr->b = b;
  295. ctlr->rdcount = n;
  296. ctlr->addr = d->addr;
  297. i2c = ctlr->regs;
  298. ctlr->salen = d->salen*8;
  299. ctlr->offset = offset;
  300. if(ctlr->salen){
  301. ctlr->phase = Subaddress;
  302. op = Wbit;
  303. }else
  304. ctlr->phase = Address;
  305. i2c->idbr = (d->addr<<1) | op; /* 7-bit address + R/nW */
  306. i2c->icr |= Start | Tb | Iteie;
  307. if(Chatty)
  308. i2cdump("start", i2c);
  309. iunlock(ctlr);
  310. /* wait for it */
  311. if(ctlr->polling){
  312. for(i=0; !isdone(ctlr); i++){
  313. delay(2);
  314. interrupt(nil, ctlr);
  315. }
  316. }else
  317. tsleep(&ctlr->r, isdone, ctlr, I2Ctimeout);
  318. ilock(ctlr);
  319. p = ctlr->phase;
  320. s = ctlr->status;
  321. ctlr->b = nil;
  322. if(ctlr->phase != Done && ctlr->phase != Idle)
  323. stopxfer(ctlr);
  324. iunlock(ctlr);
  325. if(up){
  326. poperror();
  327. qunlock(&ctlr->io);
  328. }
  329. if(p != Done || s & (Bed|Ald)){ /* CHECK; time out */
  330. if(s & Ald)
  331. return "i2c lost arbitration";
  332. if(s & Bed)
  333. return "i2c bus error";
  334. if(s & Ssd)
  335. return "i2c transfer aborted"; /* ?? */
  336. if(0 && p != Done)
  337. return "i2c timed out";
  338. sprint(up->genbuf, "i2c error: phase=%d status=%.4ux", p, s);
  339. return up->genbuf;
  340. }
  341. return nil;
  342. }
  343. long
  344. i2csend(I2Cdev *d, void *buf, long n, ulong offset)
  345. {
  346. Block *b;
  347. char *e;
  348. if(n <= 0)
  349. return 0;
  350. if(n > MaxIO)
  351. n = MaxIO;
  352. if(up){
  353. b = allocb(n);
  354. if(b == nil)
  355. error(Enomem);
  356. if(waserror()){
  357. freeb(b);
  358. nexterror();
  359. }
  360. }else{
  361. b = iallocb(n);
  362. if(b == nil)
  363. return -1;
  364. }
  365. memmove(b->wp, buf, n);
  366. b->wp += n;
  367. e = startxfer(d, 0, b, 0, offset);
  368. if(up)
  369. poperror();
  370. n -= BLEN(b); /* residue */
  371. freeb(b);
  372. if(e)
  373. return i2cerror(e);
  374. return n;
  375. }
  376. long
  377. i2crecv(I2Cdev *d, void *buf, long n, ulong offset)
  378. {
  379. Block *b;
  380. long nr;
  381. char *e;
  382. if(n <= 0)
  383. return 0;
  384. if(n > MaxIO)
  385. n = MaxIO;
  386. if(up){
  387. b = allocb(n);
  388. if(b == nil)
  389. error(Enomem);
  390. if(waserror()){
  391. freeb(b);
  392. nexterror();
  393. }
  394. }else{
  395. b = iallocb(n);
  396. if(b == nil)
  397. return -1;
  398. }
  399. e = startxfer(d, Rbit, b, n, offset);
  400. nr = BLEN(b);
  401. if(nr > 0)
  402. memmove(buf, b->rp, nr);
  403. if(up)
  404. poperror();
  405. freeb(b);
  406. if(e)
  407. return i2cerror(e);
  408. return nr;
  409. }
  410. /*
  411. * the controller must be locked for the following functions
  412. */
  413. static int
  414. readyxfer(Ctlr *ctlr, int phase)
  415. {
  416. I2Cregs *i2c;
  417. i2c = ctlr->regs;
  418. if((i2c->isr & Bed) != 0){
  419. failed(ctlr);
  420. return 0;
  421. }
  422. ctlr->phase = phase;
  423. return 1;
  424. }
  425. /*
  426. * start a master transfer to receive the next byte of data
  427. */
  428. static void
  429. rxstart(Ctlr *ctlr)
  430. {
  431. Block *b;
  432. int cntl;
  433. b = ctlr->b;
  434. if(b == nil || ctlr->rdcount<= 0){
  435. done(ctlr);
  436. return;
  437. }
  438. if(!readyxfer(ctlr, Read))
  439. return;
  440. cntl = Aldie | Tb;
  441. if(ctlr->rdcount == 1)
  442. cntl |= Stop | Nak | Iteie; /* last byte of transfer */
  443. ctlr->regs->icr |= cntl;
  444. }
  445. /*
  446. * start a master transfer to send the next chunk of data
  447. */
  448. static void
  449. txstart(Ctlr *ctlr)
  450. {
  451. Block *b;
  452. int cntl;
  453. long nb;
  454. I2Cregs *i2c;
  455. b = ctlr->b;
  456. if(b == nil || (nb = BLEN(b)) <= 0){
  457. done(ctlr);
  458. return;
  459. }
  460. if(!readyxfer(ctlr, Write))
  461. return;
  462. i2c = ctlr->regs;
  463. i2c->idbr = *b->rp++;
  464. cntl = Aldie | Tb | Iteie;
  465. if(nb == 1)
  466. cntl |= Stop;
  467. i2c->icr |= cntl;
  468. }
  469. /*
  470. * stop a transfer if one is in progress
  471. */
  472. static void
  473. stopxfer(Ctlr *ctlr)
  474. {
  475. I2Cregs *i2c;
  476. i2c = ctlr->regs;
  477. if((i2c->isr & Ub) == 0){
  478. ctlr->phase = Idle;
  479. return;
  480. }
  481. if((i2c->isr & Ibb) == 0 && ctlr->phase != Halting){
  482. ctlr->phase = Halting; /* interrupt will clear the state */
  483. i2c->icr |= Ma;
  484. }
  485. /* if that doesn't clear it by the next operation, idlectlr will do so below */
  486. }
  487. static int
  488. idlectlr(Ctlr *ctlr)
  489. {
  490. I2Cregs *i2c;
  491. i2c = ctlr->regs;
  492. if((i2c->isr & Ibb) == 0){
  493. if((i2c->isr & Ub) == 0){
  494. ctlr->phase = Idle;
  495. return 1;
  496. }
  497. iprint("i2c: bus free, ctlr busy: isr=%.4lux icr=%.4lux\n", i2c->isr, i2c->icr);
  498. }
  499. /* hit it with the hammer, soft reset */
  500. iprint("i2c: soft reset\n");
  501. i2c->icr = Ur;
  502. iunlock(ctlr);
  503. delay(1);
  504. ilock(ctlr);
  505. initialise(i2c, !ctlr->polling);
  506. ctlr->phase = Idle;
  507. return (i2c->isr & (Ibb | Ub)) == 0;
  508. }