lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2016, ZIXC Corporation. |
| 3 | * |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
| 8 | #include <malloc.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <i2c.h> |
| 11 | #include <drvs_gpio.h> |
| 12 | #include <asm/arch/i2c.h> |
| 13 | #include <asm/arch/hardware.h> |
| 14 | #include <asm/arch/uart.h> |
| 15 | #include <asm/arch/lsp_crpm.h> |
| 16 | #include <config.h> |
| 17 | |
| 18 | |
| 19 | |
| 20 | T_I2C_Regs *p_i2c_reg[2] = {NULL}; |
| 21 | static uint retries = 3; /* ÖØ¸´²Ù×÷´ÎÊý */ |
| 22 | |
| 23 | #define REG32(x) (*(volatile u32*)(x)) |
| 24 | |
| 25 | |
| 26 | /******************************************************************************* |
| 27 | * Function: |
| 28 | * Description: |
| 29 | * Parameters: |
| 30 | * Input: |
| 31 | * |
| 32 | * Output: |
| 33 | * |
| 34 | * Returns: |
| 35 | * |
| 36 | * |
| 37 | * Others: 1. ÔÚ100KHZƵÂÊÏ£¬Ê¹Óà zDrvI2c_DevRead_ByteStream ¶Á 32 ×Ö½Úʱ£¬(ÔÚU207ÊÖ»úÉÏ) |
| 38 | Ô¼ÏûºÄ tempTimeOut = 600 |
| 39 | 2. ÓÉÓÚCPUƵÂʲ»¶¨£¬bsp_udelayº¯ÊýÑÓʱ²»Ì«Çå³þ£¬¹ÊʹÓôó²ÎÊý |
| 40 | tempTimeOut = 0x20000; |
| 41 | ********************************************************************************/ |
| 42 | static int i2c_WaitTransferOver( T_I2C_Regs *i2c_reg ) |
| 43 | { |
| 44 | uint tempTimeOut = 0x20000; |
| 45 | uint tempStatus = 0x0; |
| 46 | |
| 47 | while( tempTimeOut ) |
| 48 | { |
| 49 | tempStatus = i2c_reg->I2C_STATUS; |
| 50 | if(tempStatus & 0x1) |
| 51 | { |
| 52 | break; |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | tempTimeOut--; |
| 57 | } |
| 58 | } |
| 59 | if( 0 == tempTimeOut ) |
| 60 | { |
| 61 | i2c_reg->I2C_STATUS |= I2C_IRQ_ACK; |
| 62 | return DRV_I2C_ERROR_TIMEOUT; /* ³¬Ê±´íÎó */ |
| 63 | } |
| 64 | |
| 65 | if (tempStatus & I2C_IRQ_ERR_DEVICE) |
| 66 | { |
| 67 | i2c_reg->I2C_STATUS |= I2C_IRQ_ACK; |
| 68 | return DRV_I2C_ERROR_DEVICE; /* Æ÷¼þµØÖ·´«ÊäÆÚ¼äÓдíÎó·¢Éú */ |
| 69 | } |
| 70 | if (tempStatus & I2C_IRQ_ERR_DATA) |
| 71 | { |
| 72 | i2c_reg->I2C_STATUS |= I2C_IRQ_ACK; |
| 73 | return DRV_I2C_ERROR_DATA; /* ×ÓµØÖ·´«Êä»òÊý¾Ý´«Êä¹ý³ÌÖÐÓдíÎó·¢Éú */ |
| 74 | } |
| 75 | |
| 76 | if (tempStatus & I2C_IRQ_TIME_OUT) |
| 77 | { |
| 78 | i2c_reg->I2C_STATUS |= I2C_IRQ_ACK; |
| 79 | return DRV_I2C_ERROR_TIMEOUTSELF; /* ³¬Ê±´íÎó */ |
| 80 | } |
| 81 | |
| 82 | if (tempStatus & I2C_IRQ_TRANS_DONE) |
| 83 | { |
| 84 | i2c_reg->I2C_STATUS |= I2C_IRQ_ACK; |
| 85 | return 0; /* Õý³£Íê³É */ |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /******************************************************************************* |
| 92 | * Function: |
| 93 | * Description: |
| 94 | * Parameters: |
| 95 | * Input: |
| 96 | * |
| 97 | * Output: |
| 98 | * |
| 99 | * Returns: |
| 100 | * |
| 101 | * |
| 102 | * Others: ----------------------------- |
| 103 | ********************************************************************************/ |
| 104 | void i2c_Start( T_I2C_Regs *i2c_reg ) |
| 105 | { |
| 106 | i2c_reg->CMD |= I2C_CMD_START; /* ²úÉú¿ªÊ¼Ìõ¼þ£¬¿ªÊ¼´«Êä */ |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /******************************************************************************* |
| 111 | * Function: |
| 112 | * Description: |
| 113 | * Parameters: |
| 114 | * Input: |
| 115 | * |
| 116 | * Output: |
| 117 | * |
| 118 | * Returns: |
| 119 | * |
| 120 | * |
| 121 | * Others: |
| 122 | ********************************************************************************/ |
| 123 | static int i2c_ReadFifoData( T_I2C_Regs *i2c_reg, uchar *read_buf, uint read_len ) |
| 124 | { |
| 125 | if( i2c_reg->RXF_STATUS & I2C_RX_FIFO_FULL ) |
| 126 | { |
| 127 | do |
| 128 | { |
| 129 | *read_buf++ = i2c_reg->DATA; |
| 130 | } while( --read_len ); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | return DRV_I2C_RECV_NOT_COMPLETE; |
| 135 | } |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | /******************************************************************************* |
| 140 | * Function: |
| 141 | * Description: |
| 142 | * Parameters: |
| 143 | * Input: |
| 144 | * |
| 145 | * Output: |
| 146 | * |
| 147 | * Returns: |
| 148 | * |
| 149 | * |
| 150 | * Others: ÕâÀïÖ±½ÓÈ϶¨ дµÄ¾ßÌåÊý¾Ý×î´ó¸öÊýΪ 30 £¬ÆäʵÔÚ 8λ¼Ä´æÆ÷ģʽÏÂ×î¶à¿ÉÒÔ 31¸ö |
| 151 | ********************************************************************************/ |
| 152 | void i2c_WriteFifoData( T_I2C_Regs *i2c_reg, uchar *writeBuf, uint writerNum ) |
| 153 | { |
| 154 | do |
| 155 | { |
| 156 | i2c_reg->DATA = *writeBuf++; /* дÊý¾Ý */ |
| 157 | }while(--writerNum != 0); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /******************************************************************************* |
| 162 | * Function: |
| 163 | * Description: |
| 164 | * Parameters: |
| 165 | * Input: |
| 166 | * |
| 167 | * Output: |
| 168 | * |
| 169 | * Returns: |
| 170 | * |
| 171 | * |
| 172 | * Others: ÔÚÐ´ÕæÕýµÄÊý¾ÝǰдÈë¼Ä´æÆ÷µØÖ·µ½ FIFO |
| 173 | 1.8λ¼Ä´æÆ÷µØÖ· |
| 174 | 2.16λ¼Ä´æÆ÷µØÖ· |
| 175 | ********************************************************************************/ |
| 176 | static void i2c_WriteFifoRegAddr( T_I2C_Regs *i2c_reg, ushort reg_addr, uint reg_len ) |
| 177 | { |
| 178 | ushort tempRegAddr = 0; |
| 179 | if( reg_len == 16 ) // Èç¹ûλ16λ¼Ä´æÆ÷µØÖ·£¬ÏÈд--¸ß8λ--; ºóд -µÍ8λ-- |
| 180 | { |
| 181 | tempRegAddr = reg_addr; |
| 182 | i2c_reg->DATA = tempRegAddr >> 8; |
| 183 | } |
| 184 | i2c_reg->DATA = reg_addr; |
| 185 | } |
| 186 | |
| 187 | /******************************************************************************* |
| 188 | * Function: |
| 189 | * Description: |
| 190 | * Parameters: |
| 191 | * Input: |
| 192 | * |
| 193 | * Output: |
| 194 | * |
| 195 | * Returns: |
| 196 | * |
| 197 | * |
| 198 | * Others: ÔÚ¼òµ¥Ð´Ä£Ê½ÏÂ( 8λ¼Ä´æÆ÷ºÍ16λ¼Ä´æÆ÷ )£¬ÉèÖà FifoDepth |
| 199 | 1.¸´Î» ½ÓÊÕFIFO ºÍ ·¢ËÍFIFO |
| 200 | 2.ÅжÏÊÇ·ñΪ16λ¼Ä´æÆ÷µØÖ·£¬ÈçÊÇ£¬Ôò writerNum +2; |
| 201 | ²»È»Îª 8 λ¼Ä´æÆ÷µØÖ·£¬ writerNum +1; |
| 202 | 3.дÈë·¢Ë͸öÊýµ½ÏàÓ¦µÄ¼Ä´æÆ÷¡£ |
| 203 | ********************************************************************************/ |
| 204 | static void i2c_SetFifoDepthSimpleWrite( T_I2C_Regs *i2c_reg, uint write_len, uint reg_len ) |
| 205 | { |
| 206 | uint tempWriterNum = write_len; |
| 207 | //¸´Î»WRITE(read)_FIFO_SOFT_RESET |
| 208 | i2c_reg->TXF_CTRL |= I2C_TX_FIFO_RST; |
| 209 | i2c_reg->RXF_CTRL |= I2C_RX_FIFO_RST; |
| 210 | |
| 211 | if( reg_len == 16 ) //ʹÓà 16λ¼Ä´æÆ÷µØÖ· |
| 212 | { |
| 213 | tempWriterNum++; //writerNum = writerNum + 2 - 1; //ÕâÀï¼õÈ¥ÁËд¼Ä´æÆ÷ʱÐèÒª½«È¥µÄ 1 |
| 214 | } |
| 215 | i2c_reg->TXF_CTRL = tempWriterNum; |
| 216 | i2c_reg->RXF_CTRL = 0; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /******************************************************************************* |
| 221 | * Function: |
| 222 | * Description: |
| 223 | * Parameters: |
| 224 | * Input: |
| 225 | * |
| 226 | * Output: |
| 227 | * |
| 228 | * Returns: |
| 229 | * |
| 230 | * |
| 231 | * Others: |
| 232 | ********************************************************************************/ |
| 233 | static void i2c_SetFifoDepthCombineRead( T_I2C_Regs *i2c_reg, uint read_len, uint reg_len ) |
| 234 | { |
| 235 | uint tempWriterNum = 0; |
| 236 | //¸´Î»WRITE(read)_FIFO_SOFT_RESET |
| 237 | i2c_reg->TXF_CTRL |= I2C_TX_FIFO_RST; |
| 238 | i2c_reg->RXF_CTRL |= I2C_RX_FIFO_RST; |
| 239 | |
| 240 | if( reg_len == 16 ) //ʹÓà 16λ¼Ä´æÆ÷µØÖ· |
| 241 | { |
| 242 | tempWriterNum++; //tempWriterNum = tempWriterNum + 2 - 1; |
| 243 | //ÕâÀï¼õÈ¥ÁËд¼Ä´æÆ÷ʱÐèÒª½«È¥µÄ 1 |
| 244 | } |
| 245 | i2c_reg->TXF_CTRL = tempWriterNum; |
| 246 | i2c_reg->RXF_CTRL = --read_len; |
| 247 | } |
| 248 | |
| 249 | /******************************************************************************* |
| 250 | * Function: i2c_SetFifoDepthSimpleRead |
| 251 | * Description: |
| 252 | * Parameters: |
| 253 | * Input: |
| 254 | * |
| 255 | * Output: |
| 256 | * |
| 257 | * Returns: |
| 258 | * |
| 259 | * |
| 260 | * Others: |
| 261 | ********************************************************************************/ |
| 262 | static void i2c_SetFifoDepthSimpleRead( T_I2C_Regs *pi2cReg, uint readNum ) |
| 263 | { |
| 264 | //¸´Î»WRITE(read)_FIFO_SOFT_RESET |
| 265 | pi2cReg->TXF_CTRL |= I2C_TX_FIFO_RST; |
| 266 | pi2cReg->RXF_CTRL |= I2C_RX_FIFO_RST; |
| 267 | |
| 268 | pi2cReg->TXF_CTRL = 0; |
| 269 | pi2cReg->RXF_CTRL = --readNum; |
| 270 | } |
| 271 | |
| 272 | /******************************************************************************* |
| 273 | * Function: i2c_SetFifoDepthSimpleWriteWithoutReg |
| 274 | * Description: |
| 275 | * Parameters: |
| 276 | * Input: |
| 277 | * |
| 278 | * Output: |
| 279 | * |
| 280 | * Returns: |
| 281 | * |
| 282 | * |
| 283 | * Others: |
| 284 | ********************************************************************************/ |
| 285 | static void i2c_SetFifoDepthSimpleWriteWithoutReg( T_I2C_Regs *pi2cReg, uint writerNum ) |
| 286 | { |
| 287 | //¸´Î»WRITE(read)_FIFO_SOFT_RESET |
| 288 | pi2cReg->TXF_CTRL |= I2C_TX_FIFO_RST; |
| 289 | pi2cReg->RXF_CTRL |= I2C_RX_FIFO_RST; |
| 290 | |
| 291 | pi2cReg->TXF_CTRL = writerNum - 1; |
| 292 | pi2cReg->RXF_CTRL = 0; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | /******************************************************************************* |
| 297 | * Function: i2c_SetFreq |
| 298 | * Description: |
| 299 | * Parameters: |
| 300 | * Input: |
| 301 | * |
| 302 | * Output: |
| 303 | * |
| 304 | * Returns: |
| 305 | * |
| 306 | * |
| 307 | * Others: ÉèÖÃi2c×ÜÏߵįµÂÊ£¬CLK = Fref_clk/( (CLK_DIV_FS+1)*4 ) |
| 308 | Fref_clk = 26M |
| 309 | ²ÎÊý: I2C_FREQ_ONE_HUNDRED_KILO_HZ 100kHz CLK_DIV_FS = 64 |
| 310 | ²ÎÊý: I2C_FREQ_THREE_HUNDRED_KILO_HZ 300kHz CLK_DIV_FS = 21 |
| 311 | ²ÎÊý: I2C_FREQ_FOUR_HUNDRED_KILO_HZ 400kHz CLK_DIV_FS = 16 |
| 312 | ********************************************************************************/ |
| 313 | static void i2c_SetFreq( T_I2C_Regs *pi2cReg, T_ZDrvI2c_BusFreq devFreq ) |
| 314 | { |
| 315 | pi2cReg->CMD &= (~I2C_CMD_SPEED_MODE); /* ʹÓÿìËÙģʽ */ |
| 316 | |
| 317 | switch( devFreq ) |
| 318 | { |
| 319 | case I2C_FREQ_ONE_HUNDRED_KILO_HZ: |
| 320 | pi2cReg->CLK_DIV_FS = 64; |
| 321 | break; |
| 322 | case I2C_FREQ_THREE_HUNDRED_KILO_HZ: |
| 323 | pi2cReg->CLK_DIV_FS = 21; |
| 324 | break; |
| 325 | case I2C_FREQ_FOUR_HUNDRED_KILO_HZ: |
| 326 | pi2cReg->CLK_DIV_FS = 16; |
| 327 | break; |
| 328 | default: |
| 329 | pi2cReg->CLK_DIV_FS = 64; |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /******************************************************************************* |
| 335 | * Function: |
| 336 | * Description: |
| 337 | * Parameters: |
| 338 | * Input: |
| 339 | * |
| 340 | * Output: |
| 341 | * |
| 342 | * Returns: |
| 343 | * |
| 344 | * |
| 345 | * Others: ÉèÖÃI2C×ÜÏߵĶÁд·½Ê½£¬ÕâÀïĬÈÏΪÖ÷»úģʽ£¬²»Ê¹ÓÃÖÐ¶Ï |
| 346 | ·ÖΪ ¼òµ¥Ð´¡¢¼òµ¥¶Á¡¢ ---×éºÏ¶Á--- ÈýÖÖģʽ |
| 347 | ********************************************************************************/ |
| 348 | static void i2c_SetMode( T_I2C_Regs *i2c_reg, T_I2C_Mode mode ) |
| 349 | { |
| 350 | uint cmd_reg = 0x0; |
| 351 | |
| 352 | cmd_reg |= I2C_IRQ_ACK; //Çå³ýÖжϼ°ÆäËû´íÎó¼°×´Ì¬ |
| 353 | /* ÕâÀï·ÀÖ¹Çå³ýÉϴδ«Êä¿ÉÄܲúÉúµÄ´íÎó״̬ */ |
| 354 | cmd_reg |= I2C_CMD_MS_SEL; //Ö÷»úģʽ |
| 355 | cmd_reg &= (~I2C_CMD_IRQ_EN); //ÖжÏÇëÇ󹨱Õ; |
| 356 | |
| 357 | switch( mode ) |
| 358 | { |
| 359 | case I2C_SIMPLE_WRITE: //²ÉÓüòµ¥Ð´Ä£Ê½ |
| 360 | cmd_reg &= (~(I2C_CMD_CMB_RW | I2C_CMD_RW)); |
| 361 | break; |
| 362 | case I2C_COMBINE_READ: |
| 363 | cmd_reg |= I2C_CMD_CMB_RW; //---²ÉÓÃ×éºÏ¶Á--- |
| 364 | cmd_reg &= (~I2C_CMD_RW); //0:w 1:r |
| 365 | break; |
| 366 | default: |
| 367 | cmd_reg &= (~I2C_CMD_CMB_RW); //²ÉÓüòµ¥¶Á |
| 368 | cmd_reg |= I2C_CMD_RW; |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | i2c_reg->CMD = cmd_reg; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /******************************************************************************* |
| 377 | * Function: |
| 378 | * Description: д´Ó»úµØÖ· |
| 379 | * Parameters: |
| 380 | * Input: |
| 381 | * |
| 382 | * Output: |
| 383 | * |
| 384 | * Returns: |
| 385 | * |
| 386 | * |
| 387 | * Others: дÈë´Ó»úµØÖ·£¬ÕâÀïµÄ´Ó»úµØÖ·Ö§³Ö --7λ--10λ--- ´Ó»úµØÖ· |
| 388 | 1.7λ´Ó»úµØÖ·£¬²»Ê¹ÄÜ10λµØÖ·£¬Ö±½ÓдÈë´Ó»úµØÖ·¼Ä´æÆ÷¡£¸üе±Ç°×ÜÏߵĴӻúµØÖ· |
| 389 | 2.10λ´Ó»úµØÖ·£¬Ê¹ÄÜ10λµØÖ·£¬·Ö±ðдÈëÏàÓ¦¼Ä´æÆ÷¡£ ¸üе±Ç°×ÜÏߵĴӻúµØÖ· |
| 390 | ********************************************************************************/ |
| 391 | static void i2c_SetSlaveAddress( T_I2C_Regs *i2c_reg, ushort slaveAddr ) |
| 392 | { |
| 393 | if( (slaveAddr & 0xf700) == 0 ) /* 7λ´Ó»úµØÖ· */ |
| 394 | { |
| 395 | i2c_reg->CMD &= (~I2C_CMD_ADDR_MODE); |
| 396 | i2c_reg->DEVICE_L = slaveAddr; |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | i2c_reg->CMD |= I2C_CMD_ADDR_MODE; |
| 401 | i2c_reg->DEVICE_L = slaveAddr; |
| 402 | i2c_reg->DEVICE_H = slaveAddr >> 7; |
| 403 | } |
| 404 | |
| 405 | } |
| 406 | |
| 407 | |
| 408 | /* ================================================================================ |
| 409 | * i2c_read: |
| 410 | */ |
| 411 | int i2c_read(uint i2c_bus, ushort slave_addr, ushort reg_addr, uint reg_len, |
| 412 | uchar *read_buf, uint read_len) |
| 413 | { |
| 414 | int ret = 0; |
| 415 | T_I2C_Regs *i2c_Reg = p_i2c_reg[i2c_bus]; |
| 416 | |
| 417 | do //¿ªÊ¼´«Êä |
| 418 | { |
| 419 | i2c_SetFreq(i2c_Reg, I2C_FREQ_ONE_HUNDRED_KILO_HZ); //ÉèÖÃ×ÜÏ߯µÂÊ |
| 420 | i2c_SetSlaveAddress(i2c_Reg, slave_addr); //д´Ó»úµØÖ· |
| 421 | i2c_SetMode(i2c_Reg, I2C_COMBINE_READ); //×éºÏ¶Áģʽ |
| 422 | i2c_SetFifoDepthCombineRead(i2c_Reg, read_len, reg_len); //ÉèÖÃFIFOµÄÉî¶È |
| 423 | i2c_WriteFifoRegAddr(i2c_Reg, reg_addr, reg_len); //ÍùFIFOÖÐдÈë¼Ä´æÆ÷µØÖ· |
| 424 | i2c_Start(i2c_Reg); //²úÉú¿ªÊ¼Ìõ¼þ£¬¿ªÊ¼´«Êä |
| 425 | ret = i2c_WaitTransferOver(i2c_Reg); //µÈ´ý´«Êä½áÊø |
| 426 | if( ret != 0 ) |
| 427 | { |
| 428 | continue; //´«Êäʧ°Ü£¬½áÊø±¾´ÎÑ»·²¢ÖØ´« |
| 429 | } |
| 430 | ret = i2c_ReadFifoData(i2c_Reg, read_buf, read_len ); //¶ÁÈ¡Êý¾Ý |
| 431 | if( ret != 0 ) |
| 432 | { |
| 433 | continue; //´«Êäʧ°Ü£¬½áÊø±¾´ÎÑ»·²¢ÖØ´« |
| 434 | } |
| 435 | break; //´«Êä³É¹¦£¬Ìø³öÑ»· |
| 436 | } while( --retries != 0 ); |
| 437 | retries = 3; |
| 438 | |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | /* ================================================================================ |
| 443 | * i2c_write: |
| 444 | */ |
| 445 | int i2c_write(uint i2c_bus, ushort slave_addr, ushort reg_addr, uint reg_len, |
| 446 | uchar *write_buf, uint write_len) |
| 447 | { |
| 448 | int ret = 0; |
| 449 | T_I2C_Regs *i2c_Reg = p_i2c_reg[i2c_bus]; |
| 450 | |
| 451 | do //¿ªÊ¼´«Êä |
| 452 | { |
| 453 | i2c_SetFreq(i2c_Reg, I2C_FREQ_ONE_HUNDRED_KILO_HZ); //ÉèÖÃ×ÜÏ߯µÂÊ |
| 454 | i2c_SetSlaveAddress(i2c_Reg, slave_addr); //д´Ó»úµØÖ· |
| 455 | i2c_SetMode(i2c_Reg, I2C_SIMPLE_WRITE); //×éºÏ¶Áģʽ |
| 456 | i2c_SetFifoDepthSimpleWrite(i2c_Reg, write_len, reg_len); //ÉèÖÃFIFOµÄÉî¶È |
| 457 | i2c_WriteFifoRegAddr(i2c_Reg, reg_addr, reg_len); //ÍùFIFOÖÐдÈë¼Ä´æÆ÷µØÖ· |
| 458 | i2c_WriteFifoData(i2c_Reg, write_buf, write_len); //ÍùFIFOдÈëÊý¾Ý |
| 459 | i2c_Start(i2c_Reg); //²úÉú¿ªÊ¼Ìõ¼þ£¬¿ªÊ¼´«Êä |
| 460 | ret = i2c_WaitTransferOver(i2c_Reg); //µÈ´ý´«Êä½áÊø |
| 461 | if( ret != 0 ) |
| 462 | { |
| 463 | continue; //´«Êäʧ°Ü£¬½áÊø±¾´ÎÑ»·²¢ÖØ´« |
| 464 | } |
| 465 | break; //´«Êä³É¹¦£¬Ìø³öÑ»· |
| 466 | } while( --retries != 0 ); |
| 467 | retries = 3; |
| 468 | |
| 469 | return ret; |
| 470 | } |
| 471 | |
| 472 | /* ================================================================================ |
| 473 | * i2c_simple_read: |
| 474 | */ |
| 475 | int i2c_simple_read(uint i2c_bus, ushort slave_addr, uchar *read_buf, uint read_len) |
| 476 | { |
| 477 | int ret = 0; |
| 478 | T_I2C_Regs *i2c_Reg = p_i2c_reg[i2c_bus]; |
| 479 | |
| 480 | if ( (i2c_Reg == NULL) || (read_buf == NULL) || (read_len > 33))//×î¶àд 32 ¸ö×Ö½Ú |
| 481 | { |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | do //¿ªÊ¼´«Êä |
| 486 | { |
| 487 | i2c_SetFreq(i2c_Reg, I2C_FREQ_FOUR_HUNDRED_KILO_HZ); //ÉèÖÃ×ÜÏ߯µÂÊ |
| 488 | i2c_SetSlaveAddress(i2c_Reg, slave_addr); //д´Ó»úµØÖ· |
| 489 | i2c_SetMode(i2c_Reg, I2C_SIMPLE_READ); //×éºÏ¶Áģʽ |
| 490 | i2c_SetFifoDepthSimpleRead(i2c_Reg, read_len); //ÉèÖÃFIFOµÄÉî¶È |
| 491 | i2c_Start(i2c_Reg); //²úÉú¿ªÊ¼Ìõ¼þ£¬¿ªÊ¼´«Êä |
| 492 | ret = i2c_WaitTransferOver(i2c_Reg); //µÈ´ý´«Êä½áÊø |
| 493 | if( ret != 0 ) |
| 494 | { |
| 495 | continue; //´«Êäʧ°Ü£¬½áÊø±¾´ÎÑ»·²¢ÖØ´« |
| 496 | } |
| 497 | ret = i2c_ReadFifoData(i2c_Reg, read_buf, read_len ); //¶ÁÈ¡Êý¾Ý |
| 498 | if( ret != 0 ) |
| 499 | { |
| 500 | continue; //´«Êäʧ°Ü£¬½áÊø±¾´ÎÑ»·²¢ÖØ´« |
| 501 | } |
| 502 | break; //´«Êä³É¹¦£¬Ìø³öÑ»· |
| 503 | } while( --retries != 0 ); |
| 504 | retries = 3; |
| 505 | |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | /* ================================================================================ |
| 510 | * i2c_simple_write: |
| 511 | */ |
| 512 | int i2c_simple_write(uint i2c_bus, ushort slave_addr,uchar *write_buf, uint write_len) |
| 513 | { |
| 514 | int ret = 0; |
| 515 | T_I2C_Regs *i2c_Reg = p_i2c_reg[i2c_bus]; |
| 516 | |
| 517 | if ( (i2c_Reg == NULL) || (write_buf == NULL) || (write_len > 33))//×î¶àд 32 ¸ö×Ö½Ú |
| 518 | { |
| 519 | return -1; |
| 520 | } |
| 521 | |
| 522 | do //¿ªÊ¼´«Êä |
| 523 | { |
| 524 | i2c_SetFreq(i2c_Reg, I2C_FREQ_FOUR_HUNDRED_KILO_HZ); //ÉèÖÃ×ÜÏ߯µÂÊ |
| 525 | i2c_SetSlaveAddress(i2c_Reg, slave_addr); //д´Ó»úµØÖ· |
| 526 | i2c_SetMode(i2c_Reg, I2C_SIMPLE_WRITE); //×éºÏ¶Áģʽ |
| 527 | i2c_SetFifoDepthSimpleWriteWithoutReg(i2c_Reg, write_len); //ÉèÖÃFIFOµÄÉî¶È |
| 528 | i2c_WriteFifoData(i2c_Reg, write_buf, write_len); //ÍùFIFOдÈëÊý¾Ý |
| 529 | i2c_Start(i2c_Reg); //²úÉú¿ªÊ¼Ìõ¼þ£¬¿ªÊ¼´«Êä |
| 530 | ret = i2c_WaitTransferOver(i2c_Reg); //µÈ´ý´«Êä½áÊø |
| 531 | if( ret != 0 ) |
| 532 | { |
| 533 | continue; //´«Êäʧ°Ü£¬½áÊø±¾´ÎÑ»·²¢ÖØ´« |
| 534 | } |
| 535 | break; //´«Êä³É¹¦£¬Ìø³öÑ»· |
| 536 | } while( --retries != 0 ); |
| 537 | retries = 3; |
| 538 | |
| 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | /* ================================================================================ |
| 543 | * i2c_init: |
| 544 | */ |
| 545 | int i2c_init(void) |
| 546 | { |
| 547 | p_i2c_reg[0] = (T_I2C_Regs *)ARM_I2C0_BASE; |
| 548 | p_i2c_reg[1] = (T_I2C_Regs *)ARM_I2C1_BASE; |
| 549 | /* p_i2c_reg[2] = (T_I2C_Regs *)ARM_PMICI2C_BASE; */ |
| 550 | |
| 551 | /* gpio config */ |
| 552 | //gpio_set_reuse(GPIO_I2C0_CLK, 0x0); |
| 553 | zDrvGpio_SetFunc(GPIO43, GPIO43_SCL0); |
| 554 | zDrvGpio_SetDirection(GPIO43, GPIO_OUT); |
| 555 | //gpio_set_reuse(GPIO_I2C0_DATA, 0x0); |
| 556 | zDrvGpio_SetFunc(GPIO44, GPIO44_SDA0); |
| 557 | zDrvGpio_SetDirection(GPIO44, GPIO_OUT); |
| 558 | //gpio_set_reuse(GPIO_I2C1_CLK, 0x0); |
| 559 | zDrvGpio_SetFunc(GPIO45, GPIO45_SCL1); |
| 560 | zDrvGpio_SetDirection(GPIO45, GPIO_OUT); |
| 561 | //gpio_set_reuse(GPIO_I2C1_DATA, 0x0); |
| 562 | zDrvGpio_SetFunc(GPIO46, GPIO46_SDA1); |
| 563 | zDrvGpio_SetDirection(GPIO46, GPIO_OUT); |
| 564 | |
| 565 | /* i2c1 new clk config */ |
| 566 | REG32(0x140002c) &= ~(0x1<<4);//i2c1 select 26M |
| 567 | REG32(0x140002c) &= ~(0x1<<8);//reset on i2c1 apb clk |
| 568 | REG32(0x140002c) |= (0x1<<8);//reset off i2c1 apb clk |
| 569 | REG32(0x140002c) &= ~(0x1<<9);//reset on i2c1 work clk |
| 570 | REG32(0x140002c) |= (0x1<<9);//reset off i2c1 work clk |
| 571 | REG32(0x140002c) |= (0x1<<0);//enable i2c1 apb clk |
| 572 | REG32(0x140002c) |= (0x1<<1);//enable i2c1 work clk |
| 573 | |
| 574 | /* pmic i2c clk config */ |
| 575 | REG32(0x13b03c) &= ~(0x1<<1);//pmici2c select 26M |
| 576 | REG32(0x13b074) &= ~(0x1<<8);//reset on pmici2c apb clk |
| 577 | REG32(0x13b074) |= (0x1<<8);//reset off pmici2c apb clk |
| 578 | REG32(0x13b074) &= ~(0x1<<9);//reset on pmici2c work clk |
| 579 | REG32(0x13b074) |= (0x1<<9);//reset off pmici2c work clk |
| 580 | REG32(0x13b054) |= (0x1<<8);//enable pmici2c apb clk |
| 581 | REG32(0x13b054) |= (0x1<<9);//enable pmici2c work clk |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |