i2c1.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /*************************************************************
  2. *
  3. * Copyright @ Motorola, 1999
  4. *
  5. ************************************************************/
  6. #include <common.h>
  7. #ifdef CONFIG_HARD_I2C
  8. #include <i2c.h>
  9. #include "i2c_export.h"
  10. #include "i2c.h"
  11. #undef I2CDBG0
  12. #undef DEBUG
  13. /* Define a macro to use an optional application-layer print function, if
  14. * one was passed to the I2C library during initialization. If there was
  15. * no function pointer passed, this protects against calling it. Also define
  16. * the global variable that holds the passed pointer.
  17. */
  18. #define TIMEOUT (CFG_HZ/4)
  19. #define PRINT if ( app_print ) app_print
  20. static int (*app_print) (char *, ...);
  21. /******************* Internal to I2C Driver *****************/
  22. static unsigned int ByteToXmit = 0;
  23. static unsigned int XmitByte = 0;
  24. static unsigned char *XmitBuf = 0;
  25. static unsigned int XmitBufEmptyStop = 0;
  26. static unsigned int ByteToRcv = 0;
  27. static unsigned int RcvByte = 0;
  28. static unsigned char *RcvBuf = 0;
  29. static unsigned int RcvBufFulStop = 0;
  30. static unsigned int MasterRcvAddress = 0;
  31. /* Set by call to get_eumbbar during I2C_Initialize.
  32. * This could be globally available to the I2C library, but there is
  33. * an advantage to passing it as a parameter: it is already in a register
  34. * and doesn't have to be loaded from memory. Also, that is the way the
  35. * I2C library was already implemented and I don't want to change it without
  36. * a more detailed analysis.
  37. * It is being set as a global variable in I2C_Initialize to hide it from
  38. * the DINK application layer, because it is Kahlua-specific. I think that
  39. * get_eumbbar, load_runtime_reg, and store_runtime_reg should be defined in
  40. * a Kahlua-specific library dealing with the embedded utilities memory block.
  41. * Right now, get_eumbbar is defined in dink32/kahlua.s. The other two are
  42. * defined in dink32/drivers/i2c/i2c2.s.
  43. */
  44. static unsigned int Global_eumbbar = 0;
  45. extern unsigned int load_runtime_reg (unsigned int eumbbar,
  46. unsigned int reg);
  47. extern unsigned int store_runtime_reg (unsigned int eumbbar,
  48. unsigned int reg, unsigned int val);
  49. /************************** API *****************/
  50. /* Application Program Interface (API) are the calls provided by the I2C
  51. * library to upper layer applications (i.e., DINK) to access the Kahlua
  52. * I2C bus interface. The functions and values that are part of this API
  53. * are declared in i2c_export.h.
  54. */
  55. /* Initialize I2C unit with the following:
  56. * driver's slave address
  57. * interrupt enabled
  58. * optional pointer to application layer print function
  59. *
  60. * These parameters may be added:
  61. * desired clock rate
  62. * digital filter frequency sampling rate
  63. *
  64. * This function must be called before I2C unit can be used.
  65. */
  66. I2C_Status I2C_Initialize (unsigned char addr,
  67. I2C_INTERRUPT_MODE en_int,
  68. int (*p) (char *, ...))
  69. {
  70. I2CStatus status;
  71. /* establish the pointer, if there is one, to the application's "printf" */
  72. app_print = p;
  73. /* If this is the first call, get the embedded utilities memory block
  74. * base address. I'm not sure what to do about error handling here:
  75. * if a non-zero value is returned, accept it.
  76. */
  77. if (Global_eumbbar == 0)
  78. Global_eumbbar = get_eumbbar ();
  79. if (Global_eumbbar == 0) {
  80. PRINT ("I2C_Initialize: can't find EUMBBAR\n");
  81. return I2C_ERROR;
  82. }
  83. /* validate the I2C address */
  84. if (addr & 0x80) {
  85. PRINT ("I2C_Initialize, I2C address invalid: %d 0x%x\n",
  86. (unsigned int) addr, (unsigned int) addr);
  87. return I2C_ERROR;
  88. }
  89. /* Call the internal I2C library function to perform work.
  90. * Accept the default frequency sampling rate (no way to set it currently,
  91. * via I2C_Init) and set the clock frequency to something reasonable.
  92. */
  93. status = I2C_Init (Global_eumbbar, (unsigned char) 0x31, addr, en_int);
  94. if (status != I2CSUCCESS) {
  95. PRINT ("I2C_Initialize: error in initiation\n");
  96. return I2C_ERROR;
  97. }
  98. /* all is well */
  99. return I2C_SUCCESS;
  100. }
  101. /* Perform the given I2C transaction, only MASTER_XMIT and MASTER_RCV
  102. * are implemented. Both are only in polling mode.
  103. *
  104. * en_int controls interrupt/polling mode
  105. * act is the type of transaction
  106. * i2c_addr is the I2C address of the slave device
  107. * data_addr is the address of the data on the slave device
  108. * len is the length of data to send or receive
  109. * buffer is the address of the data buffer
  110. * stop = I2C_NO_STOP, don't signal STOP at end of transaction
  111. * I2C_STOP, signal STOP at end of transaction
  112. * retry is the timeout retry value, currently ignored
  113. * rsta = I2C_NO_RESTART, this is not continuation of existing transaction
  114. * I2C_RESTART, this is a continuation of existing transaction
  115. */
  116. I2C_Status I2C_do_transaction ( I2C_INTERRUPT_MODE en_int,
  117. I2C_TRANSACTION_MODE act,
  118. unsigned char i2c_addr,
  119. unsigned char data_addr,
  120. int len,
  121. char *buffer,
  122. I2C_STOP_MODE stop,
  123. int retry, I2C_RESTART_MODE rsta)
  124. {
  125. I2C_Status status;
  126. unsigned char data_addr_buffer[1];
  127. #if 1
  128. /* This is a temporary work-around. The I2C library breaks the protocol
  129. * if it attempts to handle a data transmission in more than one
  130. * transaction, so the data address and the actual data bytes are put
  131. * into a single buffer before sending it to the library internal functions.
  132. * The problem is related to being able to restart a transaction without
  133. * sending the I2C device address or repeating the data address. It may take
  134. * a day or two to sort it all out, so I'll have to get back to it later.
  135. * Look at I2C_Start to see about using some status flags (I'm not sure that
  136. * "stop" and "rsta" are enough to reflect the states, maybe so; but the logic
  137. * in the library is insufficient) to control correct handling of the protocol.
  138. */
  139. unsigned char dummy_buffer[257];
  140. if (act == I2C_MASTER_XMIT) {
  141. int i;
  142. if (len > 256)
  143. return I2C_ERROR;
  144. for (i = 1; i <= len; i++)
  145. dummy_buffer[i] = buffer[i - 1];
  146. dummy_buffer[0] = data_addr;
  147. status = I2C_do_buffer (en_int, act, i2c_addr, 1 + len,
  148. dummy_buffer, stop, retry, rsta);
  149. if (status != I2C_SUCCESS) {
  150. PRINT ("I2C_do_transaction: can't perform data transfer\n");
  151. return I2C_ERROR;
  152. }
  153. return I2C_SUCCESS;
  154. }
  155. #endif /* end of temp work-around */
  156. /* validate requested transaction type */
  157. if ((act != I2C_MASTER_XMIT) && (act != I2C_MASTER_RCV)) {
  158. PRINT ("I2C_do_transaction, invalid transaction request: %d\n",
  159. act);
  160. return I2C_ERROR;
  161. }
  162. /* range check the I2C address */
  163. if (i2c_addr & 0x80) {
  164. PRINT ("I2C_do_transaction, I2C address out of range: %d 0x%x\n",
  165. (unsigned int) i2c_addr, (unsigned int) i2c_addr);
  166. return I2C_ERROR;
  167. } else {
  168. data_addr_buffer[0] = data_addr;
  169. }
  170. /*
  171. * We first have to contact the slave device and transmit the
  172. * data address. Be careful about the STOP and restart stuff.
  173. * We don't want to signal STOP after sending the data
  174. * address, but this could be a continuation if the
  175. * application didn't release the bus after the previous
  176. * transaction, by not sending a STOP after it.
  177. */
  178. status = I2C_do_buffer (en_int, I2C_MASTER_XMIT, i2c_addr, 1,
  179. data_addr_buffer, I2C_NO_STOP, retry, rsta);
  180. if (status != I2C_SUCCESS) {
  181. PRINT ("I2C_do_transaction: can't send data address for read\n");
  182. return I2C_ERROR;
  183. }
  184. /* The data transfer will be a continuation. */
  185. rsta = I2C_RESTART;
  186. /* now handle the user data */
  187. status = I2C_do_buffer (en_int, act, i2c_addr, len,
  188. buffer, stop, retry, rsta);
  189. if (status != I2C_SUCCESS) {
  190. PRINT ("I2C_do_transaction: can't perform data transfer\n");
  191. return I2C_ERROR;
  192. }
  193. /* all is well */
  194. return I2C_SUCCESS;
  195. }
  196. /* This function performs the work for I2C_do_transaction. The work is
  197. * split into this function to enable I2C_do_transaction to first transmit
  198. * the data address to the I2C slave device without putting the data address
  199. * into the first byte of the buffer.
  200. *
  201. * en_int controls interrupt/polling mode
  202. * act is the type of transaction
  203. * i2c_addr is the I2C address of the slave device
  204. * len is the length of data to send or receive
  205. * buffer is the address of the data buffer
  206. * stop = I2C_NO_STOP, don't signal STOP at end of transaction
  207. * I2C_STOP, signal STOP at end of transaction
  208. * retry is the timeout retry value, currently ignored
  209. * rsta = I2C_NO_RESTART, this is not continuation of existing transaction
  210. * I2C_RESTART, this is a continuation of existing transaction
  211. */
  212. static I2C_Status I2C_do_buffer (I2C_INTERRUPT_MODE en_int,
  213. I2C_TRANSACTION_MODE act,
  214. unsigned char i2c_addr,
  215. int len,
  216. unsigned char *buffer,
  217. I2C_STOP_MODE stop,
  218. int retry, I2C_RESTART_MODE rsta)
  219. {
  220. I2CStatus rval;
  221. unsigned int dev_stat;
  222. if (act == I2C_MASTER_RCV) {
  223. /* set up for master-receive transaction */
  224. rval = I2C_get (Global_eumbbar, i2c_addr, buffer, len, stop, rsta);
  225. } else {
  226. /* set up for master-transmit transaction */
  227. rval = I2C_put (Global_eumbbar, i2c_addr, buffer, len, stop, rsta);
  228. }
  229. /* validate the setup */
  230. if (rval != I2CSUCCESS) {
  231. dev_stat = load_runtime_reg (Global_eumbbar, I2CSR);
  232. PRINT ("Error(I2C_do_buffer): control phase, code(0x%08x), status(0x%08x)\n", rval, dev_stat);
  233. I2C_Stop (Global_eumbbar);
  234. return I2C_ERROR;
  235. }
  236. if (en_int == 1) {
  237. /* this should not happen, no interrupt handling yet */
  238. return I2C_SUCCESS;
  239. }
  240. /* this performs the polling action, when the transfer is completed,
  241. * the status returned from I2C_Timer_Event will be I2CBUFFFULL or
  242. * I2CBUFFEMPTY (rcv or xmit), I2CSUCCESS or I2CADDRESS indicates the
  243. * transaction is not yet complete, anything else is an error.
  244. */
  245. while (rval == I2CSUCCESS || rval == I2CADDRESS) {
  246. int timeval = get_timer (0);
  247. /* poll the device until something happens */
  248. do {
  249. rval = I2C_Timer_Event (Global_eumbbar, 0);
  250. }
  251. while (rval == I2CNOEVENT && get_timer (timeval) < TIMEOUT);
  252. /* check for error condition */
  253. if (rval == I2CSUCCESS ||
  254. rval == I2CBUFFFULL ||
  255. rval == I2CBUFFEMPTY ||
  256. rval == I2CADDRESS) {
  257. ; /* do nothing */
  258. } else {
  259. /* report the error condition */
  260. dev_stat = load_runtime_reg (Global_eumbbar, I2CSR);
  261. PRINT ("Error(I2C_do_buffer): code(0x%08x), status(0x%08x)\n",
  262. rval, dev_stat);
  263. return I2C_ERROR;
  264. }
  265. }
  266. /* all is well */
  267. return I2C_SUCCESS;
  268. }
  269. /**
  270. * Note:
  271. *
  272. * In all following functions,
  273. * the caller shall pass the configured embedded utility memory
  274. * block base, EUMBBAR.
  275. **/
  276. /***********************************************************
  277. * function: I2C_put
  278. *
  279. * description:
  280. Send a buffer of data to the intended rcv_addr.
  281. * If stop_flag is set, after the whole buffer
  282. * is sent, generate a STOP signal provided that the
  283. * receiver doesn't signal the STOP in the middle.
  284. * I2C is the master performing transmitting. If
  285. * no STOP signal is generated at the end of current
  286. * transaction, the master can generate a START signal
  287. * to another slave addr.
  288. *
  289. * note: this is master xmit API
  290. *********************************************************/
  291. static I2CStatus I2C_put (unsigned int eumbbar, unsigned char rcv_addr, /* receiver's address */
  292. unsigned char *buffer_ptr, /* pointer of data to be sent */
  293. unsigned int length, /* number of byte of in the buffer */
  294. unsigned int stop_flag, /* 1 - signal STOP when buffer is empty
  295. * 0 - no STOP signal when buffer is empty
  296. */
  297. unsigned int is_cnt)
  298. { /* 1 - this is a restart, don't check MBB
  299. * 0 - this is a new start, check MBB
  300. */
  301. if (buffer_ptr == 0 || length == 0) {
  302. return I2CERROR;
  303. }
  304. #ifdef I2CDBG0
  305. PRINT ("%s(%d): I2C_put\n", __FILE__, __LINE__);
  306. #endif
  307. XmitByte = 0;
  308. ByteToXmit = length;
  309. XmitBuf = buffer_ptr;
  310. XmitBufEmptyStop = stop_flag;
  311. RcvByte = 0;
  312. ByteToRcv = 0;
  313. RcvBuf = 0;
  314. /* we are the master, start transaction */
  315. return I2C_Start (eumbbar, rcv_addr, XMIT, is_cnt);
  316. }
  317. /***********************************************************
  318. * function: I2C_get
  319. *
  320. * description:
  321. * Receive a buffer of data from the desired sender_addr
  322. * If stop_flag is set, when the buffer is full and the
  323. * sender does not signal STOP, generate a STOP signal.
  324. * I2C is the master performing receiving. If no STOP signal
  325. * is generated, the master can generate a START signal
  326. * to another slave addr.
  327. *
  328. * note: this is master receive API
  329. **********************************************************/
  330. static I2CStatus I2C_get (unsigned int eumbbar, unsigned char rcv_from, /* sender's address */
  331. unsigned char *buffer_ptr, /* pointer of receiving buffer */
  332. unsigned int length, /* length of the receiving buffer */
  333. unsigned int stop_flag, /* 1 - signal STOP when buffer is full
  334. * 0 - no STOP signal when buffer is full
  335. */
  336. unsigned int is_cnt)
  337. { /* 1 - this is a restart, don't check MBB
  338. * 0 - this is a new start, check MBB
  339. */
  340. if (buffer_ptr == 0 || length == 0) {
  341. return I2CERROR;
  342. }
  343. #ifdef I2CDBG0
  344. PRINT ("%s(%d): I2C_get\n", __FILE__, __LINE__);
  345. #endif
  346. RcvByte = 0;
  347. ByteToRcv = length;
  348. RcvBuf = buffer_ptr;
  349. RcvBufFulStop = stop_flag;
  350. XmitByte = 0;
  351. ByteToXmit = 0;
  352. XmitBuf = 0;
  353. /* we are the master, start the transaction */
  354. return I2C_Start (eumbbar, rcv_from, RCV, is_cnt);
  355. }
  356. #if 0 /* turn off dead code */
  357. /*********************************************************
  358. * function: I2C_write
  359. *
  360. * description:
  361. * Send a buffer of data to the requiring master.
  362. * If stop_flag is set, after the whole buffer is sent,
  363. * generate a STOP signal provided that the requiring
  364. * receiver doesn't signal the STOP in the middle.
  365. * I2C is the slave performing transmitting.
  366. *
  367. * Note: this is slave xmit API.
  368. *
  369. * due to the current Kahlua design, slave transmitter
  370. * shall not signal STOP since there is no way
  371. * for master to detect it, causing I2C bus hung.
  372. *
  373. * For the above reason, the stop_flag is always
  374. * set, i.e., 0.
  375. *
  376. * programmer shall use the timer on Kahlua to
  377. * control the interval of data byte at the
  378. * master side.
  379. *******************************************************/
  380. static I2CStatus I2C_write (unsigned int eumbbar, unsigned char *buffer_ptr, /* pointer of data to be sent */
  381. unsigned int length, /* number of byte of in the buffer */
  382. unsigned int stop_flag)
  383. { /* 1 - signal STOP when buffer is empty
  384. * 0 - no STOP signal when buffer is empty
  385. */
  386. if (buffer_ptr == 0 || length == 0) {
  387. return I2CERROR;
  388. }
  389. XmitByte = 0;
  390. ByteToXmit = length;
  391. XmitBuf = buffer_ptr;
  392. XmitBufEmptyStop = 0; /* in order to avoid bus hung, ignored the user's stop_flag */
  393. RcvByte = 0;
  394. ByteToRcv = 0;
  395. RcvBuf = 0;
  396. /* we are the slave, just wait for being called, or pull */
  397. /* I2C_Timer_Event( eumbbar ); */
  398. }
  399. /******************************************************
  400. * function: I2C_read
  401. *
  402. * description:
  403. * Receive a buffer of data from the sending master.
  404. * If stop_flag is set, when the buffer is full and the
  405. * sender does not signal STOP, generate a STOP signal.
  406. * I2C is the slave performing receiving.
  407. *
  408. * note: this is slave receive API
  409. ****************************************************/
  410. static I2CStatus I2C_read (unsigned int eumbbar, unsigned char *buffer_ptr, /* pointer of receiving buffer */
  411. unsigned int length, /* length of the receiving buffer */
  412. unsigned int stop_flag)
  413. { /* 1 - signal STOP when buffer is full
  414. * 0 - no STOP signal when buffer is full
  415. */
  416. if (buffer_ptr == 0 || length == 0) {
  417. return I2CERROR;
  418. }
  419. RcvByte = 0;
  420. ByteToRcv = length;
  421. RcvBuf = buffer_ptr;
  422. RcvBufFulStop = stop_flag;
  423. XmitByte = 0;
  424. ByteToXmit = 0;
  425. XmitBuf = 0;
  426. /* wait for master to call us, or poll */
  427. /* I2C_Timer_Event( eumbbar ); */
  428. }
  429. #endif /* turn off dead code */
  430. /*********************************************************
  431. * function: I2c_Timer_Event
  432. *
  433. * description:
  434. * if interrupt is not used, this is the timer event handler.
  435. * After each fixed time interval, this function can be called
  436. * to check the I2C status and call appropriate function to
  437. * handle the status event.
  438. ********************************************************/
  439. static I2CStatus I2C_Timer_Event (unsigned int eumbbar,
  440. I2CStatus (*handler) (unsigned int))
  441. {
  442. I2C_STAT stat;
  443. #ifdef I2CDBG0
  444. PRINT ("%s(%d): I2C_Timer_Event\n", __FILE__, __LINE__);
  445. #endif
  446. stat = I2C_Get_Stat (eumbbar);
  447. if (stat.mif == 1) {
  448. if (handler == 0) {
  449. return I2C_ISR (eumbbar);
  450. } else {
  451. return (*handler) (eumbbar);
  452. }
  453. }
  454. return I2CNOEVENT;
  455. }
  456. /****************** Device I/O function *****************/
  457. /******************************************************
  458. * function: I2C_Start
  459. *
  460. * description: Generate a START signal in the desired mode.
  461. * I2C is the master.
  462. *
  463. * Return I2CSUCCESS if no error.
  464. *
  465. * note:
  466. ****************************************************/
  467. static I2CStatus I2C_Start (unsigned int eumbbar, unsigned char slave_addr, /* address of the receiver */
  468. I2C_MODE mode, /* XMIT(1) - put (write)
  469. * RCV(0) - get (read)
  470. */
  471. unsigned int is_cnt)
  472. { /* 1 - this is a restart, don't check MBB
  473. * 0 - this is a new start
  474. */
  475. unsigned int tmp = 0;
  476. I2C_STAT stat;
  477. I2C_CTRL ctrl;
  478. #ifdef I2CDBG0
  479. PRINT ("%s(%d): I2C_Start addr 0x%x mode %d cnt %d\n", __FILE__,
  480. __LINE__, slave_addr, mode, is_cnt);
  481. #endif
  482. ctrl = I2C_Get_Ctrl (eumbbar);
  483. /* first make sure I2C has been initialized */
  484. if (ctrl.men == 0) {
  485. return I2CERROR;
  486. }
  487. /* next make sure bus is idle */
  488. stat = I2C_Get_Stat (eumbbar);
  489. if (is_cnt == 0 && stat.mbb == 1) {
  490. /* sorry, we lost */
  491. return I2CBUSBUSY;
  492. } else if (is_cnt == 1 && stat.mif == 1 && stat.mal == 0) {
  493. /* sorry, we lost the bus */
  494. return I2CALOSS;
  495. }
  496. /* OK, I2C is enabled and we have the bus */
  497. /* prepare to write the slave address */
  498. ctrl.msta = 1;
  499. ctrl.mtx = 1;
  500. ctrl.txak = 0;
  501. ctrl.rsta = is_cnt; /* set the repeat start bit */
  502. I2C_Set_Ctrl (eumbbar, ctrl);
  503. /* write the slave address and xmit/rcv mode bit */
  504. tmp = load_runtime_reg (eumbbar, I2CDR);
  505. tmp = (tmp & 0xffffff00) |
  506. ((slave_addr & 0x007f) << 1) |
  507. (mode == XMIT ? 0x0 : 0x1);
  508. store_runtime_reg (eumbbar, I2CDR, tmp);
  509. if (mode == RCV) {
  510. MasterRcvAddress = 1;
  511. } else {
  512. MasterRcvAddress = 0;
  513. }
  514. #ifdef I2CDBG0
  515. PRINT ("%s(%d): I2C_Start exit\n", __FILE__, __LINE__);
  516. #endif
  517. /* wait for the interrupt or poll */
  518. return I2CSUCCESS;
  519. }
  520. /***********************************************************
  521. * function: I2c_Stop
  522. *
  523. * description: Generate a STOP signal to terminate the master
  524. * transaction.
  525. * return I2CSUCCESS
  526. *
  527. **********************************************************/
  528. static I2CStatus I2C_Stop (unsigned int eumbbar)
  529. {
  530. I2C_CTRL ctrl;
  531. #ifdef I2CDBG0
  532. PRINT ("%s(%d): I2C_Stop enter\n", __FILE__, __LINE__);
  533. #endif
  534. ctrl = I2C_Get_Ctrl (eumbbar);
  535. ctrl.msta = 0;
  536. I2C_Set_Ctrl (eumbbar, ctrl);
  537. #ifdef I2CDBG0
  538. PRINT ("%s(%d): I2C_Stop exit\n", __FILE__, __LINE__);
  539. #endif
  540. return I2CSUCCESS;
  541. }
  542. /****************************************************
  543. * function: I2C_Master_Xmit
  544. *
  545. * description: Master sends one byte of data to
  546. * slave target
  547. *
  548. * return I2CSUCCESS if the byte transmitted.
  549. * Otherwise no-zero
  550. *
  551. * Note: condition must meet when this function is called:
  552. * I2CSR(MIF) == 1 && I2CSR(MCF) == 1 && I2CSR(RXAK) == 0
  553. * I2CCR(MSTA) == 1 && I2CCR(MTX) == 1
  554. *
  555. ***************************************************/
  556. static I2CStatus I2C_Master_Xmit (unsigned int eumbbar)
  557. {
  558. unsigned int val;
  559. if (ByteToXmit > 0) {
  560. if (ByteToXmit == XmitByte) {
  561. /* all xmitted */
  562. ByteToXmit = 0;
  563. if (XmitBufEmptyStop == 1) {
  564. I2C_Stop (eumbbar);
  565. }
  566. return I2CBUFFEMPTY;
  567. }
  568. #ifdef I2CDBG0
  569. PRINT ("%s(%d): xmit 0x%02x\n", __FILE__, __LINE__,
  570. *(XmitBuf + XmitByte));
  571. #endif
  572. val = *(XmitBuf + XmitByte);
  573. val &= 0x000000ff;
  574. store_runtime_reg (eumbbar, I2CDR, val);
  575. XmitByte++;
  576. return I2CSUCCESS;
  577. }
  578. return I2CBUFFEMPTY;
  579. }
  580. /***********************************************
  581. * function: I2C_Master_Rcv
  582. *
  583. * description: master reads one byte data
  584. * from slave source
  585. *
  586. * return I2CSUCCESS if no error
  587. *
  588. * Note: condition must meet when this function is called:
  589. * I2CSR(MIF) == 1 && I2CSR(MCF) == 1 &&
  590. * I2CCR(MSTA) == 1 && I2CCR(MTX) == 0
  591. *
  592. ***********************************************/
  593. static I2CStatus I2C_Master_Rcv (unsigned int eumbbar)
  594. {
  595. I2C_CTRL ctrl;
  596. unsigned int val;
  597. if (ByteToRcv > 0) {
  598. if (ByteToRcv - RcvByte == 2 && RcvBufFulStop == 1) {
  599. /* master requests more than or equal to 2 bytes
  600. * we are reading 2nd to last byte
  601. */
  602. /* we need to set I2CCR(TXAK) to generate a STOP */
  603. ctrl = I2C_Get_Ctrl (eumbbar);
  604. ctrl.txak = 1;
  605. I2C_Set_Ctrl (eumbbar, ctrl);
  606. /* Kahlua will automatically generate a STOP
  607. * next time a transaction happens
  608. */
  609. /* note: the case of master requesting one byte is
  610. * handled in I2C_ISR
  611. */
  612. }
  613. /* generat a STOP before reading the last byte */
  614. if (RcvByte + 1 == ByteToRcv && RcvBufFulStop == 1) {
  615. I2C_Stop (eumbbar);
  616. }
  617. val = load_runtime_reg (eumbbar, I2CDR);
  618. *(RcvBuf + RcvByte) = val & 0xFF;
  619. #ifdef I2CDBG0
  620. PRINT ("%s(%d): rcv 0x%02x\n", __FILE__, __LINE__,
  621. *(RcvBuf + RcvByte));
  622. #endif
  623. RcvByte++;
  624. if (ByteToRcv == RcvByte) {
  625. ByteToRcv = 0;
  626. return I2CBUFFFULL;
  627. }
  628. return I2CSUCCESS;
  629. }
  630. return I2CBUFFFULL;
  631. }
  632. /****************************************************
  633. * function: I2C_Slave_Xmit
  634. *
  635. * description: Slave sends one byte of data to
  636. * requesting destination
  637. *
  638. * return SUCCESS if the byte transmitted. Otherwise
  639. * No-zero
  640. *
  641. * Note: condition must meet when this function is called:
  642. * I2CSR(MIF) == 1 && I2CSR(MCF) == 1 && I2CSR(RXAK) = 0
  643. * I2CCR(MSTA) == 0 && I2CCR(MTX) == 1
  644. *
  645. ***************************************************/
  646. static I2CStatus I2C_Slave_Xmit (unsigned int eumbbar)
  647. {
  648. unsigned int val;
  649. if (ByteToXmit > 0) {
  650. if (ByteToXmit == XmitByte) {
  651. /* no more data to send */
  652. ByteToXmit = 0;
  653. /*
  654. * do not toggle I2CCR(MTX). Doing so will
  655. * cause bus-hung since current Kahlua design
  656. * does not give master a way to detect slave
  657. * stop. It is always a good idea for master
  658. * to use timer to prevent the long long
  659. * delays
  660. */
  661. return I2CBUFFEMPTY;
  662. }
  663. #ifdef I2CDBG
  664. PRINT ("%s(%d): xmit 0x%02x\n", __FILE__, __LINE__,
  665. *(XmitBuf + XmitByte));
  666. #endif
  667. val = *(XmitBuf + XmitByte);
  668. val &= 0x000000ff;
  669. store_runtime_reg (eumbbar, I2CDR, val);
  670. XmitByte++;
  671. return I2CSUCCESS;
  672. }
  673. return I2CBUFFEMPTY;
  674. }
  675. /***********************************************
  676. * function: I2C_Slave_Rcv
  677. *
  678. * description: slave reads one byte data
  679. * from master source
  680. *
  681. * return I2CSUCCESS if no error otherwise non-zero
  682. *
  683. * Note: condition must meet when this function is called:
  684. * I2CSR(MIF) == 1 && I2CSR(MCF) == 1 &&
  685. * I2CCR(MSTA) == 0 && I2CCR(MTX) = 0
  686. *
  687. ***********************************************/
  688. static I2CStatus I2C_Slave_Rcv (unsigned int eumbbar)
  689. {
  690. unsigned int val;
  691. I2C_CTRL ctrl;
  692. if (ByteToRcv > 0) {
  693. val = load_runtime_reg (eumbbar, I2CDR);
  694. *(RcvBuf + RcvByte) = val & 0xff;
  695. #ifdef I2CDBG
  696. PRINT ("%s(%d): rcv 0x%02x\n", __FILE__, __LINE__,
  697. *(RcvBuf + RcvByte));
  698. #endif
  699. RcvByte++;
  700. if (ByteToRcv == RcvByte) {
  701. if (RcvBufFulStop == 1) {
  702. /* all done */
  703. ctrl = I2C_Get_Ctrl (eumbbar);
  704. ctrl.txak = 1;
  705. I2C_Set_Ctrl (eumbbar, ctrl);
  706. }
  707. ByteToRcv = 0;
  708. return I2CBUFFFULL;
  709. }
  710. return I2CSUCCESS;
  711. }
  712. return I2CBUFFFULL;
  713. }
  714. /****************** Device Control Function *************/
  715. /*********************************************************
  716. * function: I2C_Init
  717. *
  718. * description: Initialize I2C unit with desired frequency divider,
  719. * master's listening address, with interrupt enabled
  720. * or disabled.
  721. *
  722. * note:
  723. ********************************************************/
  724. static I2CStatus I2C_Init (unsigned int eumbbar, unsigned char fdr, /* frequency divider */
  725. unsigned char slave_addr, /* driver's address used for receiving */
  726. unsigned int en_int)
  727. { /* 1 - enable I2C interrupt
  728. * 0 - disable I2C interrup
  729. */
  730. I2C_CTRL ctrl;
  731. unsigned int tmp;
  732. #ifdef I2CDBG0
  733. PRINT ("%s(%d): I2C_Init enter\n", __FILE__, __LINE__);
  734. #endif
  735. ctrl = I2C_Get_Ctrl (eumbbar);
  736. /* disable the I2C module before we change everything */
  737. ctrl.men = 0;
  738. I2C_Set_Ctrl (eumbbar, ctrl);
  739. /* set the frequency diver */
  740. tmp = load_runtime_reg (eumbbar, I2CFDR);
  741. tmp = (tmp & 0xffffffc0) | (fdr & 0x3f);
  742. store_runtime_reg (eumbbar, I2CFDR, tmp);
  743. /* Set our listening (slave) address */
  744. tmp = load_runtime_reg (eumbbar, I2CADR);
  745. tmp = (tmp & 0xffffff01) | ((slave_addr & 0x7f) << 1);
  746. store_runtime_reg (eumbbar, I2CADR, tmp);
  747. /* enable I2C with desired interrupt setting */
  748. ctrl.men = 1;
  749. ctrl.mien = en_int & 0x1;
  750. I2C_Set_Ctrl (eumbbar, ctrl);
  751. #ifdef I2CDBG0
  752. PRINT ("%s(%d): I2C_Init exit\n", __FILE__, __LINE__);
  753. #endif
  754. return I2CSUCCESS;
  755. }
  756. /*****************************************
  757. * function I2c_Get_Stat
  758. *
  759. * description: Query I2C Status, i.e., read I2CSR
  760. *
  761. ****************************************/
  762. static I2C_STAT I2C_Get_Stat (unsigned int eumbbar)
  763. {
  764. unsigned int temp;
  765. I2C_STAT stat;
  766. temp = load_runtime_reg (eumbbar, I2CSR);
  767. #ifdef I2CDBG0
  768. PRINT ("%s(%d): get stat = 0x%08x\n", __FILE__, __LINE__, temp);
  769. #endif
  770. stat.rsrv0 = (temp & 0xffffff00) >> 8;
  771. stat.mcf = (temp & 0x00000080) >> 7;
  772. stat.maas = (temp & 0x00000040) >> 6;
  773. stat.mbb = (temp & 0x00000020) >> 5;
  774. stat.mal = (temp & 0x00000010) >> 4;
  775. stat.rsrv1 = (temp & 0x00000008) >> 3;
  776. stat.srw = (temp & 0x00000004) >> 2;
  777. stat.mif = (temp & 0x00000002) >> 1;
  778. stat.rxak = (temp & 0x00000001);
  779. return stat;
  780. }
  781. /*********************************************
  782. * function: I2c_Set_Ctrl
  783. *
  784. * description: Change I2C Control bits,
  785. * i.e., write to I2CCR
  786. *
  787. ********************************************/
  788. static void I2C_Set_Ctrl (unsigned int eumbbar, I2C_CTRL ctrl)
  789. { /* new control value */
  790. unsigned int temp = load_runtime_reg (eumbbar, I2CCR);
  791. temp &= 0xffffff03;
  792. temp |= ((ctrl.men & 0x1) << 7);
  793. temp |= ((ctrl.mien & 0x1) << 6);
  794. temp |= ((ctrl.msta & 0x1) << 5);
  795. temp |= ((ctrl.mtx & 0x1) << 4);
  796. temp |= ((ctrl.txak & 0x1) << 3);
  797. temp |= ((ctrl.rsta & 0x1) << 2);
  798. #ifdef I2CDBG0
  799. PRINT ("%s(%d): set ctrl = 0x%08x\n", __FILE__, __LINE__, temp);
  800. #endif
  801. store_runtime_reg (eumbbar, I2CCR, temp);
  802. }
  803. /*****************************************
  804. * function: I2C_Get_Ctrl
  805. *
  806. * description: Query I2C Control bits,
  807. * i.e., read I2CCR
  808. *****************************************/
  809. static I2C_CTRL I2C_Get_Ctrl (unsigned int eumbbar)
  810. {
  811. union {
  812. I2C_CTRL ctrl;
  813. unsigned int temp;
  814. } s;
  815. s.temp = load_runtime_reg (eumbbar, I2CCR);
  816. #ifdef I2CDBG0
  817. PRINT ("%s(%d): get ctrl = 0x%08x\n", __FILE__, __LINE__, s.temp);
  818. #endif
  819. return s.ctrl;
  820. }
  821. /****************************************
  822. * function: I2C_Slave_Addr
  823. *
  824. * description: Process slave address phase.
  825. * return I2CSUCCESS if no error
  826. *
  827. * note: Precondition for calling this function:
  828. * I2CSR(MIF) == 1 &&
  829. * I2CSR(MAAS) == 1
  830. ****************************************/
  831. static I2CStatus I2C_Slave_Addr (unsigned int eumbbar)
  832. {
  833. I2C_STAT stat = I2C_Get_Stat (eumbbar);
  834. I2C_CTRL ctrl = I2C_Get_Ctrl (eumbbar);
  835. if (stat.srw == 1) {
  836. /* we are asked to xmit */
  837. ctrl.mtx = 1;
  838. I2C_Set_Ctrl (eumbbar, ctrl); /* set MTX */
  839. return I2C_Slave_Xmit (eumbbar);
  840. }
  841. /* we are asked to receive data */
  842. ctrl.mtx = 0;
  843. I2C_Set_Ctrl (eumbbar, ctrl);
  844. (void) load_runtime_reg (eumbbar, I2CDR); /* do a fake read to start */
  845. return I2CADDRESS;
  846. }
  847. /***********************************************
  848. * function: I2C_ISR
  849. *
  850. * description: I2C Interrupt service routine
  851. *
  852. * note: Precondition:
  853. * I2CSR(MIF) == 1
  854. **********************************************/
  855. static I2CStatus I2C_ISR (unsigned int eumbbar)
  856. {
  857. I2C_STAT stat;
  858. I2C_CTRL ctrl;
  859. #ifdef I2CDBG0
  860. PRINT ("%s(%d): I2C_ISR\n", __FILE__, __LINE__);
  861. #endif
  862. stat = I2C_Get_Stat (eumbbar);
  863. ctrl = I2C_Get_Ctrl (eumbbar);
  864. /* clear MIF */
  865. stat.mif = 0;
  866. /* Now let see what kind of event this is */
  867. if (stat.mcf == 1) {
  868. /* transfer compete */
  869. /* clear the MIF bit */
  870. I2C_Set_Stat (eumbbar, stat);
  871. if (ctrl.msta == 1) {
  872. /* master */
  873. if (ctrl.mtx == 1) {
  874. /* check if this is the address phase for master receive */
  875. if (MasterRcvAddress == 1) {
  876. /* Yes, it is the address phase of master receive */
  877. ctrl.mtx = 0;
  878. /* now check how much we want to receive */
  879. if (ByteToRcv == 1 && RcvBufFulStop == 1) {
  880. ctrl.txak = 1;
  881. }
  882. I2C_Set_Ctrl (eumbbar, ctrl);
  883. (void) load_runtime_reg (eumbbar, I2CDR); /* fake read first */
  884. MasterRcvAddress = 0;
  885. return I2CADDRESS;
  886. }
  887. /* master xmit */
  888. if (stat.rxak == 0) {
  889. /* slave has acknowledged */
  890. return I2C_Master_Xmit (eumbbar);
  891. }
  892. /* slave has not acknowledged yet, generate a STOP */
  893. if (XmitBufEmptyStop == 1) {
  894. ctrl.msta = 0;
  895. I2C_Set_Ctrl (eumbbar, ctrl);
  896. }
  897. return I2CSUCCESS;
  898. }
  899. /* master receive */
  900. return I2C_Master_Rcv (eumbbar);
  901. }
  902. /* slave */
  903. if (ctrl.mtx == 1) {
  904. /* slave xmit */
  905. if (stat.rxak == 0) {
  906. /* master has acknowledged */
  907. return I2C_Slave_Xmit (eumbbar);
  908. }
  909. /* master has not acknowledged, wait for STOP */
  910. /* do nothing for preventing bus from hung */
  911. return I2CSUCCESS;
  912. }
  913. /* slave rcv */
  914. return I2C_Slave_Rcv (eumbbar);
  915. } else if (stat.maas == 1) {
  916. /* received a call from master */
  917. /* clear the MIF bit */
  918. I2C_Set_Stat (eumbbar, stat);
  919. /* master is calling us, process the address phase */
  920. return I2C_Slave_Addr (eumbbar);
  921. } else {
  922. /* has to be arbitration lost */
  923. stat.mal = 0;
  924. I2C_Set_Stat (eumbbar, stat);
  925. ctrl.msta = 0; /* return to receive mode */
  926. I2C_Set_Ctrl (eumbbar, ctrl);
  927. }
  928. return I2CSUCCESS;
  929. }
  930. /******************************************************
  931. * function: I2C_Set_Stat
  932. *
  933. * description: modify the I2CSR
  934. *
  935. *****************************************************/
  936. static void I2C_Set_Stat (unsigned int eumbbar, I2C_STAT stat)
  937. {
  938. union {
  939. unsigned int val;
  940. I2C_STAT stat;
  941. } s_tmp;
  942. union {
  943. unsigned int val;
  944. I2C_STAT stat;
  945. } s;
  946. s.val = load_runtime_reg (eumbbar, I2CSR);
  947. s.val &= 0xffffff08;
  948. s_tmp.stat = stat;
  949. s.val |= (s_tmp.val & 0xf7);
  950. #ifdef I2CDBG0
  951. PRINT ("%s(%d): set stat = 0x%08x\n", __FILE__, __LINE__, s.val);
  952. #endif
  953. store_runtime_reg (eumbbar, I2CSR, s.val);
  954. }
  955. /******************************************************
  956. * The following are routines to glue the rest of
  957. * U-Boot to the Sandpoint I2C driver.
  958. *****************************************************/
  959. void i2c_init (int speed, int slaveadd)
  960. {
  961. #ifdef CFG_I2C_INIT_BOARD
  962. /*
  963. * call board specific i2c bus reset routine before accessing the
  964. * environment, which might be in a chip on that bus. For details
  965. * about this problem see doc/I2C_Edge_Conditions.
  966. */
  967. i2c_init_board();
  968. #endif
  969. #ifdef DEBUG
  970. I2C_Initialize (0x7f, 0, (void *) printf);
  971. #else
  972. I2C_Initialize (0x7f, 0, 0);
  973. #endif
  974. }
  975. int i2c_probe (uchar chip)
  976. {
  977. int tmp;
  978. /*
  979. * Try to read the first location of the chip. The underlying
  980. * driver doesn't appear to support sending just the chip address
  981. * and looking for an <ACK> back.
  982. */
  983. udelay(10000);
  984. return i2c_read (chip, 0, 1, (char *)&tmp, 1);
  985. }
  986. int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
  987. {
  988. I2CStatus status;
  989. uchar xaddr[4];
  990. if (alen > 0) {
  991. xaddr[0] = (addr >> 24) & 0xFF;
  992. xaddr[1] = (addr >> 16) & 0xFF;
  993. xaddr[2] = (addr >> 8) & 0xFF;
  994. xaddr[3] = addr & 0xFF;
  995. status = I2C_do_buffer (0, I2C_MASTER_XMIT, chip, alen,
  996. &xaddr[4 - alen], I2C_NO_STOP, 1,
  997. I2C_NO_RESTART);
  998. if (status != I2C_SUCCESS) {
  999. PRINT ("i2c_read: can't send data address for read\n");
  1000. return 1;
  1001. }
  1002. }
  1003. /* The data transfer will be a continuation. */
  1004. status = I2C_do_buffer (0, I2C_MASTER_RCV, chip, len,
  1005. buffer, I2C_STOP, 1, (alen > 0 ? I2C_RESTART :
  1006. I2C_NO_RESTART));
  1007. if (status != I2C_SUCCESS) {
  1008. PRINT ("i2c_read: can't perform data transfer\n");
  1009. return 1;
  1010. }
  1011. return 0;
  1012. }
  1013. int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
  1014. {
  1015. I2CStatus status;
  1016. uchar dummy_buffer[I2C_RXTX_LEN + 2];
  1017. uchar *p;
  1018. int i;
  1019. /* fill in address in big endian order */
  1020. for (i=alen-1; i>=0; --i) {
  1021. buffer[i] = addr & 0xFF;
  1022. addr >>= 8;
  1023. }
  1024. /* fill in data */
  1025. p = dummy_buffer + alen;
  1026. for (i=0; i<len; ++i)
  1027. *p++ = *buffer++;
  1028. status = I2C_do_buffer (0, I2C_MASTER_XMIT, chip, alen + len,
  1029. dummy_buffer, I2C_STOP, 1, I2C_NO_RESTART);
  1030. #ifdef CFG_EEPROM_PAGE_WRITE_DELAY_MS
  1031. udelay(CFG_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  1032. #endif
  1033. if (status != I2C_SUCCESS) {
  1034. PRINT ("i2c_write: can't perform data transfer\n");
  1035. return 1;
  1036. }
  1037. return 0;
  1038. }
  1039. uchar i2c_reg_read (uchar i2c_addr, uchar reg)
  1040. {
  1041. char buf[1];
  1042. i2c_init (0, 0);
  1043. i2c_read (i2c_addr, reg, 1, buf, 1);
  1044. return (buf[0]);
  1045. }
  1046. void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
  1047. {
  1048. i2c_init (0, 0);
  1049. i2c_write (i2c_addr, reg, 1, &val, 1);
  1050. }
  1051. #endif /* CONFIG_HARD_I2C */