1 /*************************************************************
3 * Copyright @ Motorola, 1999
5 ************************************************************/
10 #include "i2c_export.h"
16 /* Define a macro to use an optional application-layer print function, if
17 * one was passed to the I2C library during initialization. If there was
18 * no function pointer passed, this protects against calling it. Also define
19 * the global variable that holds the passed pointer.
21 #define TIMEOUT (CFG_HZ/4)
22 #define PRINT if ( app_print ) app_print
23 static int (*app_print) (char *, ...);
25 /******************* Internal to I2C Driver *****************/
26 static unsigned int ByteToXmit = 0;
27 static unsigned int XmitByte = 0;
28 static unsigned char *XmitBuf = 0;
29 static unsigned int XmitBufEmptyStop = 0;
30 static unsigned int ByteToRcv = 0;
31 static unsigned int RcvByte = 0;
32 static unsigned char *RcvBuf = 0;
33 static unsigned int RcvBufFulStop = 0;
34 static unsigned int MasterRcvAddress = 0;
36 /* Set by call to get_eumbbar during I2C_Initialize.
37 * This could be globally available to the I2C library, but there is
38 * an advantage to passing it as a parameter: it is already in a register
39 * and doesn't have to be loaded from memory. Also, that is the way the
40 * I2C library was already implemented and I don't want to change it without
41 * a more detailed analysis.
42 * It is being set as a global variable in I2C_Initialize to hide it from
43 * the DINK application layer, because it is Kahlua-specific. I think that
44 * get_eumbbar, load_runtime_reg, and store_runtime_reg should be defined in
45 * a Kahlua-specific library dealing with the embedded utilities memory block.
46 * Right now, get_eumbbar is defined in dink32/kahlua.s. The other two are
47 * defined in dink32/drivers/i2c/i2c2.s.
49 static unsigned int Global_eumbbar = 0;
51 extern unsigned int load_runtime_reg (unsigned int eumbbar,
54 extern unsigned int store_runtime_reg (unsigned int eumbbar,
55 unsigned int reg, unsigned int val);
57 /************************** API *****************/
59 /* Application Program Interface (API) are the calls provided by the I2C
60 * library to upper layer applications (i.e., DINK) to access the Kahlua
61 * I2C bus interface. The functions and values that are part of this API
62 * are declared in i2c_export.h.
65 /* Initialize I2C unit with the following:
66 * driver's slave address
68 * optional pointer to application layer print function
70 * These parameters may be added:
72 * digital filter frequency sampling rate
74 * This function must be called before I2C unit can be used.
76 I2C_Status I2C_Initialize (unsigned char addr,
77 I2C_INTERRUPT_MODE en_int,
78 int (*p) (char *, ...))
82 /* establish the pointer, if there is one, to the application's "printf" */
85 /* If this is the first call, get the embedded utilities memory block
86 * base address. I'm not sure what to do about error handling here:
87 * if a non-zero value is returned, accept it.
89 if (Global_eumbbar == 0)
90 Global_eumbbar = get_eumbbar ();
91 if (Global_eumbbar == 0) {
92 PRINT ("I2C_Initialize: can't find EUMBBAR\n");
96 /* validate the I2C address */
98 PRINT ("I2C_Initialize, I2C address invalid: %d 0x%x\n",
99 (unsigned int) addr, (unsigned int) addr);
103 /* Call the internal I2C library function to perform work.
104 * Accept the default frequency sampling rate (no way to set it currently,
105 * via I2C_Init) and set the clock frequency to something reasonable.
107 status = I2C_Init (Global_eumbbar, (unsigned char) 0x31, addr, en_int);
108 if (status != I2CSUCCESS) {
109 PRINT ("I2C_Initialize: error in initiation\n");
118 /* Perform the given I2C transaction, only MASTER_XMIT and MASTER_RCV
119 * are implemented. Both are only in polling mode.
121 * en_int controls interrupt/polling mode
122 * act is the type of transaction
123 * i2c_addr is the I2C address of the slave device
124 * data_addr is the address of the data on the slave device
125 * len is the length of data to send or receive
126 * buffer is the address of the data buffer
127 * stop = I2C_NO_STOP, don't signal STOP at end of transaction
128 * I2C_STOP, signal STOP at end of transaction
129 * retry is the timeout retry value, currently ignored
130 * rsta = I2C_NO_RESTART, this is not continuation of existing transaction
131 * I2C_RESTART, this is a continuation of existing transaction
133 I2C_Status I2C_do_transaction ( I2C_INTERRUPT_MODE en_int,
134 I2C_TRANSACTION_MODE act,
135 unsigned char i2c_addr,
136 unsigned char data_addr,
140 int retry, I2C_RESTART_MODE rsta)
143 unsigned char data_addr_buffer[1];
146 /* This is a temporary work-around. The I2C library breaks the protocol
147 * if it attempts to handle a data transmission in more than one
148 * transaction, so the data address and the actual data bytes are put
149 * into a single buffer before sending it to the library internal functions.
150 * The problem is related to being able to restart a transaction without
151 * sending the I2C device address or repeating the data address. It may take
152 * a day or two to sort it all out, so I'll have to get back to it later.
153 * Look at I2C_Start to see about using some status flags (I'm not sure that
154 * "stop" and "rsta" are enough to reflect the states, maybe so; but the logic
155 * in the library is insufficient) to control correct handling of the protocol.
157 unsigned char dummy_buffer[257];
159 if (act == I2C_MASTER_XMIT) {
164 for (i = 1; i <= len; i++)
165 dummy_buffer[i] = buffer[i - 1];
166 dummy_buffer[0] = data_addr;
167 status = I2C_do_buffer (en_int, act, i2c_addr, 1 + len,
168 dummy_buffer, stop, retry, rsta);
169 if (status != I2C_SUCCESS) {
170 PRINT ("I2C_do_transaction: can't perform data transfer\n");
175 #endif /* end of temp work-around */
177 /* validate requested transaction type */
178 if ((act != I2C_MASTER_XMIT) && (act != I2C_MASTER_RCV)) {
179 PRINT ("I2C_do_transaction, invalid transaction request: %d\n",
184 /* range check the I2C address */
185 if (i2c_addr & 0x80) {
186 PRINT ("I2C_do_transaction, I2C address out of range: %d 0x%x\n",
187 (unsigned int) i2c_addr, (unsigned int) i2c_addr);
190 data_addr_buffer[0] = data_addr;
194 * We first have to contact the slave device and transmit the
195 * data address. Be careful about the STOP and restart stuff.
196 * We don't want to signal STOP after sending the data
197 * address, but this could be a continuation if the
198 * application didn't release the bus after the previous
199 * transaction, by not sending a STOP after it.
201 status = I2C_do_buffer (en_int, I2C_MASTER_XMIT, i2c_addr, 1,
202 data_addr_buffer, I2C_NO_STOP, retry, rsta);
203 if (status != I2C_SUCCESS) {
204 PRINT ("I2C_do_transaction: can't send data address for read\n");
208 /* The data transfer will be a continuation. */
211 /* now handle the user data */
212 status = I2C_do_buffer (en_int, act, i2c_addr, len,
213 buffer, stop, retry, rsta);
214 if (status != I2C_SUCCESS) {
215 PRINT ("I2C_do_transaction: can't perform data transfer\n");
223 /* This function performs the work for I2C_do_transaction. The work is
224 * split into this function to enable I2C_do_transaction to first transmit
225 * the data address to the I2C slave device without putting the data address
226 * into the first byte of the buffer.
228 * en_int controls interrupt/polling mode
229 * act is the type of transaction
230 * i2c_addr is the I2C address of the slave device
231 * len is the length of data to send or receive
232 * buffer is the address of the data buffer
233 * stop = I2C_NO_STOP, don't signal STOP at end of transaction
234 * I2C_STOP, signal STOP at end of transaction
235 * retry is the timeout retry value, currently ignored
236 * rsta = I2C_NO_RESTART, this is not continuation of existing transaction
237 * I2C_RESTART, this is a continuation of existing transaction
239 static I2C_Status I2C_do_buffer (I2C_INTERRUPT_MODE en_int,
240 I2C_TRANSACTION_MODE act,
241 unsigned char i2c_addr,
243 unsigned char *buffer,
245 int retry, I2C_RESTART_MODE rsta)
248 unsigned int dev_stat;
250 if (act == I2C_MASTER_RCV) {
251 /* set up for master-receive transaction */
252 rval = I2C_get (Global_eumbbar, i2c_addr, buffer, len, stop, rsta);
254 /* set up for master-transmit transaction */
255 rval = I2C_put (Global_eumbbar, i2c_addr, buffer, len, stop, rsta);
258 /* validate the setup */
259 if (rval != I2CSUCCESS) {
260 dev_stat = load_runtime_reg (Global_eumbbar, I2CSR);
261 PRINT ("Error(I2C_do_buffer): control phase, code(0x%08x), status(0x%08x)\n", rval, dev_stat);
262 I2C_Stop (Global_eumbbar);
267 /* this should not happen, no interrupt handling yet */
271 /* this performs the polling action, when the transfer is completed,
272 * the status returned from I2C_Timer_Event will be I2CBUFFFULL or
273 * I2CBUFFEMPTY (rcv or xmit), I2CSUCCESS or I2CADDRESS indicates the
274 * transaction is not yet complete, anything else is an error.
276 while (rval == I2CSUCCESS || rval == I2CADDRESS) {
277 int timeval = get_timer (0);
279 /* poll the device until something happens */
281 rval = I2C_Timer_Event (Global_eumbbar, 0);
283 while (rval == I2CNOEVENT && get_timer (timeval) < TIMEOUT);
285 /* check for error condition */
286 if (rval == I2CSUCCESS ||
287 rval == I2CBUFFFULL ||
288 rval == I2CBUFFEMPTY ||
289 rval == I2CADDRESS) {
292 /* report the error condition */
293 dev_stat = load_runtime_reg (Global_eumbbar, I2CSR);
294 PRINT ("Error(I2C_do_buffer): code(0x%08x), status(0x%08x)\n",
307 * In all following functions,
308 * the caller shall pass the configured embedded utility memory
309 * block base, EUMBBAR.
312 /***********************************************************
316 Send a buffer of data to the intended rcv_addr.
317 * If stop_flag is set, after the whole buffer
318 * is sent, generate a STOP signal provided that the
319 * receiver doesn't signal the STOP in the middle.
320 * I2C is the master performing transmitting. If
321 * no STOP signal is generated at the end of current
322 * transaction, the master can generate a START signal
323 * to another slave addr.
325 * note: this is master xmit API
326 *********************************************************/
327 static I2CStatus I2C_put (unsigned int eumbbar, unsigned char rcv_addr, /* receiver's address */
328 unsigned char *buffer_ptr, /* pointer of data to be sent */
329 unsigned int length, /* number of byte of in the buffer */
330 unsigned int stop_flag, /* 1 - signal STOP when buffer is empty
331 * 0 - no STOP signal when buffer is empty
334 { /* 1 - this is a restart, don't check MBB
335 * 0 - this is a new start, check MBB
337 if (buffer_ptr == 0 || length == 0) {
341 PRINT ("%s(%d): I2C_put\n", __FILE__, __LINE__);
346 XmitBuf = buffer_ptr;
347 XmitBufEmptyStop = stop_flag;
353 /* we are the master, start transaction */
354 return I2C_Start (eumbbar, rcv_addr, XMIT, is_cnt);
357 /***********************************************************
361 * Receive a buffer of data from the desired sender_addr
362 * If stop_flag is set, when the buffer is full and the
363 * sender does not signal STOP, generate a STOP signal.
364 * I2C is the master performing receiving. If no STOP signal
365 * is generated, the master can generate a START signal
366 * to another slave addr.
368 * note: this is master receive API
369 **********************************************************/
370 static I2CStatus I2C_get (unsigned int eumbbar, unsigned char rcv_from, /* sender's address */
371 unsigned char *buffer_ptr, /* pointer of receiving buffer */
372 unsigned int length, /* length of the receiving buffer */
373 unsigned int stop_flag, /* 1 - signal STOP when buffer is full
374 * 0 - no STOP signal when buffer is full
377 { /* 1 - this is a restart, don't check MBB
378 * 0 - this is a new start, check MBB
380 if (buffer_ptr == 0 || length == 0) {
384 PRINT ("%s(%d): I2C_get\n", __FILE__, __LINE__);
390 RcvBufFulStop = stop_flag;
396 /* we are the master, start the transaction */
397 return I2C_Start (eumbbar, rcv_from, RCV, is_cnt);
401 #if 0 /* turn off dead code */
402 /*********************************************************
403 * function: I2C_write
406 * Send a buffer of data to the requiring master.
407 * If stop_flag is set, after the whole buffer is sent,
408 * generate a STOP signal provided that the requiring
409 * receiver doesn't signal the STOP in the middle.
410 * I2C is the slave performing transmitting.
412 * Note: this is slave xmit API.
414 * due to the current Kahlua design, slave transmitter
415 * shall not signal STOP since there is no way
416 * for master to detect it, causing I2C bus hung.
418 * For the above reason, the stop_flag is always
421 * programmer shall use the timer on Kahlua to
422 * control the interval of data byte at the
424 *******************************************************/
425 static I2CStatus I2C_write (unsigned int eumbbar, unsigned char *buffer_ptr, /* pointer of data to be sent */
426 unsigned int length, /* number of byte of in the buffer */
427 unsigned int stop_flag)
428 { /* 1 - signal STOP when buffer is empty
429 * 0 - no STOP signal when buffer is empty
431 if (buffer_ptr == 0 || length == 0) {
437 XmitBuf = buffer_ptr;
438 XmitBufEmptyStop = 0; /* in order to avoid bus hung, ignored the user's stop_flag */
444 /* we are the slave, just wait for being called, or pull */
445 /* I2C_Timer_Event( eumbbar ); */
448 /******************************************************
452 * Receive a buffer of data from the sending master.
453 * If stop_flag is set, when the buffer is full and the
454 * sender does not signal STOP, generate a STOP signal.
455 * I2C is the slave performing receiving.
457 * note: this is slave receive API
458 ****************************************************/
459 static I2CStatus I2C_read (unsigned int eumbbar, unsigned char *buffer_ptr, /* pointer of receiving buffer */
460 unsigned int length, /* length of the receiving buffer */
461 unsigned int stop_flag)
462 { /* 1 - signal STOP when buffer is full
463 * 0 - no STOP signal when buffer is full
465 if (buffer_ptr == 0 || length == 0) {
472 RcvBufFulStop = stop_flag;
478 /* wait for master to call us, or poll */
479 /* I2C_Timer_Event( eumbbar ); */
481 #endif /* turn off dead code */
483 /*********************************************************
484 * function: I2c_Timer_Event
487 * if interrupt is not used, this is the timer event handler.
488 * After each fixed time interval, this function can be called
489 * to check the I2C status and call appropriate function to
490 * handle the status event.
491 ********************************************************/
492 static I2CStatus I2C_Timer_Event (unsigned int eumbbar,
493 I2CStatus (*handler) (unsigned int))
498 PRINT ("%s(%d): I2C_Timer_Event\n", __FILE__, __LINE__);
501 stat = I2C_Get_Stat (eumbbar);
505 return I2C_ISR (eumbbar);
507 return (*handler) (eumbbar);
515 /****************** Device I/O function *****************/
517 /******************************************************
518 * function: I2C_Start
520 * description: Generate a START signal in the desired mode.
523 * Return I2CSUCCESS if no error.
526 ****************************************************/
527 static I2CStatus I2C_Start (unsigned int eumbbar, unsigned char slave_addr, /* address of the receiver */
528 I2C_MODE mode, /* XMIT(1) - put (write)
529 * RCV(0) - get (read)
532 { /* 1 - this is a restart, don't check MBB
533 * 0 - this is a new start
535 unsigned int tmp = 0;
540 PRINT ("%s(%d): I2C_Start addr 0x%x mode %d cnt %d\n", __FILE__,
541 __LINE__, slave_addr, mode, is_cnt);
544 ctrl = I2C_Get_Ctrl (eumbbar);
546 /* first make sure I2C has been initialized */
551 /* next make sure bus is idle */
552 stat = I2C_Get_Stat (eumbbar);
554 if (is_cnt == 0 && stat.mbb == 1) {
557 } else if (is_cnt == 1 && stat.mif == 1 && stat.mal == 0) {
558 /* sorry, we lost the bus */
563 /* OK, I2C is enabled and we have the bus */
565 /* prepare to write the slave address */
569 ctrl.rsta = is_cnt; /* set the repeat start bit */
570 I2C_Set_Ctrl (eumbbar, ctrl);
572 /* write the slave address and xmit/rcv mode bit */
573 tmp = load_runtime_reg (eumbbar, I2CDR);
574 tmp = (tmp & 0xffffff00) |
575 ((slave_addr & 0x007f) << 1) |
576 (mode == XMIT ? 0x0 : 0x1);
577 store_runtime_reg (eumbbar, I2CDR, tmp);
580 MasterRcvAddress = 1;
582 MasterRcvAddress = 0;
586 PRINT ("%s(%d): I2C_Start exit\n", __FILE__, __LINE__);
589 /* wait for the interrupt or poll */
593 /***********************************************************
596 * description: Generate a STOP signal to terminate the master
600 **********************************************************/
601 static I2CStatus I2C_Stop (unsigned int eumbbar)
606 PRINT ("%s(%d): I2C_Stop enter\n", __FILE__, __LINE__);
609 ctrl = I2C_Get_Ctrl (eumbbar);
611 I2C_Set_Ctrl (eumbbar, ctrl);
614 PRINT ("%s(%d): I2C_Stop exit\n", __FILE__, __LINE__);
620 /****************************************************
621 * function: I2C_Master_Xmit
623 * description: Master sends one byte of data to
626 * return I2CSUCCESS if the byte transmitted.
629 * Note: condition must meet when this function is called:
630 * I2CSR(MIF) == 1 && I2CSR(MCF) == 1 && I2CSR(RXAK) == 0
631 * I2CCR(MSTA) == 1 && I2CCR(MTX) == 1
633 ***************************************************/
634 static I2CStatus I2C_Master_Xmit (unsigned int eumbbar)
638 if (ByteToXmit > 0) {
640 if (ByteToXmit == XmitByte) {
644 if (XmitBufEmptyStop == 1) {
652 PRINT ("%s(%d): xmit 0x%02x\n", __FILE__, __LINE__,
653 *(XmitBuf + XmitByte));
656 val = *(XmitBuf + XmitByte);
658 store_runtime_reg (eumbbar, I2CDR, val);
668 /***********************************************
669 * function: I2C_Master_Rcv
671 * description: master reads one byte data
674 * return I2CSUCCESS if no error
676 * Note: condition must meet when this function is called:
677 * I2CSR(MIF) == 1 && I2CSR(MCF) == 1 &&
678 * I2CCR(MSTA) == 1 && I2CCR(MTX) == 0
680 ***********************************************/
681 static I2CStatus I2C_Master_Rcv (unsigned int eumbbar)
688 if (ByteToRcv - RcvByte == 2 && RcvBufFulStop == 1) {
689 /* master requests more than or equal to 2 bytes
690 * we are reading 2nd to last byte
693 /* we need to set I2CCR(TXAK) to generate a STOP */
694 ctrl = I2C_Get_Ctrl (eumbbar);
696 I2C_Set_Ctrl (eumbbar, ctrl);
698 /* Kahlua will automatically generate a STOP
699 * next time a transaction happens
702 /* note: the case of master requesting one byte is
707 /* generat a STOP before reading the last byte */
708 if (RcvByte + 1 == ByteToRcv && RcvBufFulStop == 1) {
712 val = load_runtime_reg (eumbbar, I2CDR);
713 *(RcvBuf + RcvByte) = val & 0xFF;
716 PRINT ("%s(%d): rcv 0x%02x\n", __FILE__, __LINE__,
717 *(RcvBuf + RcvByte));
722 if (ByteToRcv == RcvByte) {
735 /****************************************************
736 * function: I2C_Slave_Xmit
738 * description: Slave sends one byte of data to
739 * requesting destination
741 * return SUCCESS if the byte transmitted. Otherwise
744 * Note: condition must meet when this function is called:
745 * I2CSR(MIF) == 1 && I2CSR(MCF) == 1 && I2CSR(RXAK) = 0
746 * I2CCR(MSTA) == 0 && I2CCR(MTX) == 1
748 ***************************************************/
749 static I2CStatus I2C_Slave_Xmit (unsigned int eumbbar)
753 if (ByteToXmit > 0) {
755 if (ByteToXmit == XmitByte) {
756 /* no more data to send */
760 * do not toggle I2CCR(MTX). Doing so will
761 * cause bus-hung since current Kahlua design
762 * does not give master a way to detect slave
763 * stop. It is always a good idea for master
764 * to use timer to prevent the long long
771 PRINT ("%s(%d): xmit 0x%02x\n", __FILE__, __LINE__,
772 *(XmitBuf + XmitByte));
775 val = *(XmitBuf + XmitByte);
777 store_runtime_reg (eumbbar, I2CDR, val);
786 /***********************************************
787 * function: I2C_Slave_Rcv
789 * description: slave reads one byte data
792 * return I2CSUCCESS if no error otherwise non-zero
794 * Note: condition must meet when this function is called:
795 * I2CSR(MIF) == 1 && I2CSR(MCF) == 1 &&
796 * I2CCR(MSTA) == 0 && I2CCR(MTX) = 0
798 ***********************************************/
799 static I2CStatus I2C_Slave_Rcv (unsigned int eumbbar)
805 val = load_runtime_reg (eumbbar, I2CDR);
806 *(RcvBuf + RcvByte) = val & 0xff;
808 PRINT ("%s(%d): rcv 0x%02x\n", __FILE__, __LINE__,
809 *(RcvBuf + RcvByte));
813 if (ByteToRcv == RcvByte) {
814 if (RcvBufFulStop == 1) {
816 ctrl = I2C_Get_Ctrl (eumbbar);
818 I2C_Set_Ctrl (eumbbar, ctrl);
831 /****************** Device Control Function *************/
833 /*********************************************************
836 * description: Initialize I2C unit with desired frequency divider,
837 * master's listening address, with interrupt enabled
841 ********************************************************/
842 static I2CStatus I2C_Init (unsigned int eumbbar, unsigned char fdr, /* frequency divider */
843 unsigned char slave_addr, /* driver's address used for receiving */
845 { /* 1 - enable I2C interrupt
846 * 0 - disable I2C interrup
852 PRINT ("%s(%d): I2C_Init enter\n", __FILE__, __LINE__);
855 ctrl = I2C_Get_Ctrl (eumbbar);
856 /* disable the I2C module before we change everything */
858 I2C_Set_Ctrl (eumbbar, ctrl);
860 /* set the frequency diver */
861 tmp = load_runtime_reg (eumbbar, I2CFDR);
862 tmp = (tmp & 0xffffffc0) | (fdr & 0x3f);
863 store_runtime_reg (eumbbar, I2CFDR, tmp);
865 /* Set our listening (slave) address */
866 tmp = load_runtime_reg (eumbbar, I2CADR);
867 tmp = (tmp & 0xffffff01) | ((slave_addr & 0x7f) << 1);
868 store_runtime_reg (eumbbar, I2CADR, tmp);
870 /* enable I2C with desired interrupt setting */
872 ctrl.mien = en_int & 0x1;
873 I2C_Set_Ctrl (eumbbar, ctrl);
875 PRINT ("%s(%d): I2C_Init exit\n", __FILE__, __LINE__);
882 /*****************************************
883 * function I2c_Get_Stat
885 * description: Query I2C Status, i.e., read I2CSR
887 ****************************************/
888 static I2C_STAT I2C_Get_Stat (unsigned int eumbbar)
893 temp = load_runtime_reg (eumbbar, I2CSR);
896 PRINT ("%s(%d): get stat = 0x%08x\n", __FILE__, __LINE__, temp);
899 stat.rsrv0 = (temp & 0xffffff00) >> 8;
900 stat.mcf = (temp & 0x00000080) >> 7;
901 stat.maas = (temp & 0x00000040) >> 6;
902 stat.mbb = (temp & 0x00000020) >> 5;
903 stat.mal = (temp & 0x00000010) >> 4;
904 stat.rsrv1 = (temp & 0x00000008) >> 3;
905 stat.srw = (temp & 0x00000004) >> 2;
906 stat.mif = (temp & 0x00000002) >> 1;
907 stat.rxak = (temp & 0x00000001);
911 /*********************************************
912 * function: I2c_Set_Ctrl
914 * description: Change I2C Control bits,
915 * i.e., write to I2CCR
917 ********************************************/
918 static void I2C_Set_Ctrl (unsigned int eumbbar, I2C_CTRL ctrl)
919 { /* new control value */
920 unsigned int temp = load_runtime_reg (eumbbar, I2CCR);
923 temp |= ((ctrl.men & 0x1) << 7);
924 temp |= ((ctrl.mien & 0x1) << 6);
925 temp |= ((ctrl.msta & 0x1) << 5);
926 temp |= ((ctrl.mtx & 0x1) << 4);
927 temp |= ((ctrl.txak & 0x1) << 3);
928 temp |= ((ctrl.rsta & 0x1) << 2);
930 PRINT ("%s(%d): set ctrl = 0x%08x\n", __FILE__, __LINE__, temp);
932 store_runtime_reg (eumbbar, I2CCR, temp);
936 /*****************************************
937 * function: I2C_Get_Ctrl
939 * description: Query I2C Control bits,
941 *****************************************/
942 static I2C_CTRL I2C_Get_Ctrl (unsigned int eumbbar)
949 s.temp = load_runtime_reg (eumbbar, I2CCR);
951 PRINT ("%s(%d): get ctrl = 0x%08x\n", __FILE__, __LINE__, s.temp);
958 /****************************************
959 * function: I2C_Slave_Addr
961 * description: Process slave address phase.
962 * return I2CSUCCESS if no error
964 * note: Precondition for calling this function:
967 ****************************************/
968 static I2CStatus I2C_Slave_Addr (unsigned int eumbbar)
970 I2C_STAT stat = I2C_Get_Stat (eumbbar);
971 I2C_CTRL ctrl = I2C_Get_Ctrl (eumbbar);
974 /* we are asked to xmit */
976 I2C_Set_Ctrl (eumbbar, ctrl); /* set MTX */
977 return I2C_Slave_Xmit (eumbbar);
980 /* we are asked to receive data */
982 I2C_Set_Ctrl (eumbbar, ctrl);
983 (void) load_runtime_reg (eumbbar, I2CDR); /* do a fake read to start */
988 /***********************************************
991 * description: I2C Interrupt service routine
993 * note: Precondition:
995 **********************************************/
996 static I2CStatus I2C_ISR (unsigned int eumbbar)
1002 PRINT ("%s(%d): I2C_ISR\n", __FILE__, __LINE__);
1005 stat = I2C_Get_Stat (eumbbar);
1006 ctrl = I2C_Get_Ctrl (eumbbar);
1011 /* Now let see what kind of event this is */
1012 if (stat.mcf == 1) {
1013 /* transfer compete */
1015 /* clear the MIF bit */
1016 I2C_Set_Stat (eumbbar, stat);
1018 if (ctrl.msta == 1) {
1020 if (ctrl.mtx == 1) {
1021 /* check if this is the address phase for master receive */
1022 if (MasterRcvAddress == 1) {
1023 /* Yes, it is the address phase of master receive */
1025 /* now check how much we want to receive */
1026 if (ByteToRcv == 1 && RcvBufFulStop == 1) {
1030 I2C_Set_Ctrl (eumbbar, ctrl);
1031 (void) load_runtime_reg (eumbbar, I2CDR); /* fake read first */
1033 MasterRcvAddress = 0;
1039 if (stat.rxak == 0) {
1040 /* slave has acknowledged */
1041 return I2C_Master_Xmit (eumbbar);
1044 /* slave has not acknowledged yet, generate a STOP */
1045 if (XmitBufEmptyStop == 1) {
1047 I2C_Set_Ctrl (eumbbar, ctrl);
1053 /* master receive */
1054 return I2C_Master_Rcv (eumbbar);
1058 if (ctrl.mtx == 1) {
1060 if (stat.rxak == 0) {
1061 /* master has acknowledged */
1062 return I2C_Slave_Xmit (eumbbar);
1065 /* master has not acknowledged, wait for STOP */
1066 /* do nothing for preventing bus from hung */
1071 return I2C_Slave_Rcv (eumbbar);
1073 } else if (stat.maas == 1) {
1074 /* received a call from master */
1076 /* clear the MIF bit */
1077 I2C_Set_Stat (eumbbar, stat);
1079 /* master is calling us, process the address phase */
1080 return I2C_Slave_Addr (eumbbar);
1082 /* has to be arbitration lost */
1084 I2C_Set_Stat (eumbbar, stat);
1086 ctrl.msta = 0; /* return to receive mode */
1087 I2C_Set_Ctrl (eumbbar, ctrl);
1094 /******************************************************
1095 * function: I2C_Set_Stat
1097 * description: modify the I2CSR
1099 *****************************************************/
1100 static void I2C_Set_Stat (unsigned int eumbbar, I2C_STAT stat)
1111 s.val = load_runtime_reg (eumbbar, I2CSR);
1112 s.val &= 0xffffff08;
1114 s.val |= (s_tmp.val & 0xf7);
1117 PRINT ("%s(%d): set stat = 0x%08x\n", __FILE__, __LINE__, s.val);
1120 store_runtime_reg (eumbbar, I2CSR, s.val);
1124 /******************************************************
1125 * The following are routines to glue the rest of
1126 * U-Boot to the Sandpoint I2C driver.
1127 *****************************************************/
1129 void i2c_init (int speed, int slaveadd)
1131 #ifdef CFG_I2C_INIT_BOARD
1133 * call board specific i2c bus reset routine before accessing the
1134 * environment, which might be in a chip on that bus. For details
1135 * about this problem see doc/I2C_Edge_Conditions.
1141 I2C_Initialize (0x7f, 0, (void *) printf);
1143 I2C_Initialize (0x7f, 0, 0);
1147 int i2c_probe (uchar chip)
1152 * Try to read the first location of the chip. The underlying
1153 * driver doesn't appear to support sending just the chip address
1154 * and looking for an <ACK> back.
1157 return i2c_read (chip, 0, 1, (char *)&tmp, 1);
1160 int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
1166 xaddr[0] = (addr >> 24) & 0xFF;
1167 xaddr[1] = (addr >> 16) & 0xFF;
1168 xaddr[2] = (addr >> 8) & 0xFF;
1169 xaddr[3] = addr & 0xFF;
1171 status = I2C_do_buffer (0, I2C_MASTER_XMIT, chip, alen,
1172 &xaddr[4 - alen], I2C_NO_STOP, 1,
1174 if (status != I2C_SUCCESS) {
1175 PRINT ("i2c_read: can't send data address for read\n");
1180 /* The data transfer will be a continuation. */
1181 status = I2C_do_buffer (0, I2C_MASTER_RCV, chip, len,
1182 buffer, I2C_STOP, 1, (alen > 0 ? I2C_RESTART :
1185 if (status != I2C_SUCCESS) {
1186 PRINT ("i2c_read: can't perform data transfer\n");
1193 int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
1196 uchar dummy_buffer[I2C_RXTX_LEN + 2];
1201 /* fill in address in big endian order */
1202 for (i=0; i<alen; ++i)
1203 *p++ = (addr >> (i * 8)) & 0xFF;
1205 for (i=0; i<len; ++i)
1208 status = I2C_do_buffer (0, I2C_MASTER_XMIT, chip, alen + len,
1209 dummy_buffer, I2C_STOP, 1, I2C_NO_RESTART);
1211 #ifdef CFG_EEPROM_PAGE_WRITE_DELAY_MS
1212 udelay(CFG_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
1214 if (status != I2C_SUCCESS) {
1215 PRINT ("i2c_write: can't perform data transfer\n");
1222 uchar i2c_reg_read (uchar i2c_addr, uchar reg)
1228 i2c_read (i2c_addr, reg, 1, buf, 1);
1233 void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
1237 i2c_write (i2c_addr, reg, 1, &val, 1);
1240 #endif /* CONFIG_HARD_I2C */