Chinaunix首页 | 论坛 | 博客
  • 博客访问: 191705
  • 博文数量: 60
  • 博客积分: 652
  • 博客等级: 上士
  • 技术积分: 460
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-15 14:26
文章分类
文章存档

2015年(4)

2014年(16)

2013年(16)

2011年(20)

2010年(4)

分类: LINUX

2010-06-30 14:08:02

 

  1. //sfp_types.h


  2. /******************************************************************************
  3. **
  4. ** sfp_types.h -- Include file for the SFP type.
  5. **
  6. ** 03-18-2010 -- Rudolf Chang : Initial Creation
  7. ** 04-29-2010 -- Rudolf Chang : Modify
  8. **
  9. ******************************************************************************/
  10. #ifndef _SFP_TYPES_H_
  11. #define _SFP_TYPES_H_
  12. #include <common.h>
  13. #define I2C_SW_CTL_CHIP 0x73
  14. #define I2C_ETH_SFP_CHIP 0x50
  15. #define I2C_IO_EXPA_CHIP 0x26
  16. #define I2C_CHANNEL_LM75_1_EN 1 << 0
  17. #define I2C_CHANNEL_LM75_2_EN 1 << 1
  18. #define I2C_CHANNEL_SFP_EN 1 << 2
  19. #define I2C_CHANNEL_IO_EXPA_EN 1 << 3
  20. /*-------------------------------------------------------------------------------
  21. ** Definitions -- SFP Type Defines
  22. **-------------------------------------------------------------------------------*/
  23. #define SFP_ID_ADDR 0x0
  24. #define SFP_TYPE_ETH_CODE_ADDR 0x6
  25. #define SFP_VENDOR_ADDR 0x14

  26. #define SFP_ID 0x3
  27. #define SFP_TYPE_ETH_NONE 0xff
  28. #define SFP_TYPE_ETH_100FX 0
  29. #define SFP_TYPE_ETH_1000BASE_SX 1
  30. #define SFP_TYPE_ETH_1000BASE_LX 2
  31. #define SFP_TYPE_ETH_1000BASE_CX 4
  32. #define SFP_TYPE_ETH_1000BASE_T 8
  33. #define SFP_TYPE_ETH_100BASE_LX 0x10
  34. #define SFP_TYPE_ETH_100BASE_FX 0x20
  35. #define SFP_TYPE_ETH_100BASE_LX_2 0x40
  36. /* I2C_IO_EXPA_CHIP IO_0 bits defined */
  37. #define GBIC_PRSNT 0x1
  38. #define GBIC_RATE 0x2
  39. #define GBIC_TX_FAULT 0x4
  40. #define GBIC_RX_LOST 0x8
  41. #define GBIC_TX_DIS 0x10
  42. /* I2C_IO_EXPA_CHIP IO_1 bits defined */
  43. #define RF_MODE_SEL_0 0x1
  44. #define RF_MODE_SEL_1 0x2
  45. #define RF_MODE_SEL_2 0x4
  46. #define RF_VER_SEL_0 0x8
  47. #define RF_VER_SEL_1 0x10
  48. /* I2C_IO_EXPA_CHIP registers command defined */
  49. #define REG_INPUT_0 0x1
  50. #define REG_INPUT_1 0x2
  51. #define REG_OUTPUT_0 0x4
  52. #define REG_OUTPUT_1 0x8
  53. #define REG_POLARITY_INV_0 0x10
  54. #define REG_POLARITY_INV_1 0x20
  55. #define REG_CFG_0 0x40
  56. #define REG_CFG_1 0x80
  57. /* I2C_IO_EXPA_CHIP registers value defined */
  58. #define VAL_NULL 0x0

  59. /*----------------------------------------------------------------------------
  60. ** Global Data Structure for Geting I2C Device Information
  61. **---------------------------------------------------------------------------*/
  62. typedef struct SFP_I2C_S
  63. {
  64.   uint8_t current_eth_code;
  65.   uint8_t loopback_mode; /* loopback_mode enabled: 1, loopback_mode disabled: 0*/
  66.   uint8_t trx_id; /* Present:1, Non-Present:0 for SFP */
  67.   uint8_t sfp_eth_code; /* SFP Transceiver code */
  68.   uint8_t sfp_vendor[16]; /* SFP vendor */
  69. } SFP_I2C_T;
  70. /*-------------------------------------------------------------------------
  71.  * FUNCTION NAME: SFP_I2C_Discover()
  72.  *-------------------------------------------------------------------------
  73.  * PURPOSE: This function provides SFP module discovery and device type
  74.  * detected through I2C bus.
  75.  * INPUT : sfp_info.
  76.  *
  77.  * OUTPUT : sfp_info.trx_id
  78.  * sfp_info.sfp_eth_code
  79.  * RETURN : True/False.
  80.  * NOTES :
  81.  * History:
  82.  * 2010/03/18 -- Rudolf Chang, Added.
  83.  *-------------------------------------------------------------------------
  84.  */
  85. uint8_t SFP_I2C_Discover(SFP_I2C_T *sfp_info);
  86. /*-------------------------------------------------------------------------
  87.  * FUNCTION NAME: I2C_IO_Expand()
  88.  *-------------------------------------------------------------------------
  89.  * PURPOSE: This function provides I2C IO Expander read/write through I2C bus.
  90.  * INPUT : port interface [0 | 1]
  91.  * val: value for register write
  92.  * OUTPUT :
  93.  * val: value for register read
  94.  *
  95.  * RETURN : True/False.
  96.  * NOTES :
  97.  * History:
  98.  * 2010/04/29 -- Rudolf Chang, Added.
  99.  *-------------------------------------------------------------------------
  100.  */
  101. uint8_t I2C_IO_Expand(uint8_t port, uint8_t *val);
  102. /*-------------------------------------------------------------------------
  103.  * FUNCTION NAME: I2C_Get_HWVersion()
  104.  *-------------------------------------------------------------------------
  105.  * PURPOSE: This function provides I2C IO Expander read/write through I2C bus to get hardware version.
  106.  * INPUT : NULL
  107.  * OUTPUT : NULL
  108.  *
  109.  * RETURN :normal: hardware version (eg: 2: ROB; 0: ROA; 1: ROC), abnoraml: -1
  110.  * NOTES :
  111.  * History:
  112.  * 2010/06/10 -- adam xu, Added.
  113.  *-------------------------------------------------------------------------
  114.  */
  115. uint8_t I2C_Get_HWVersion();
  116. /*-------------------------------------------------------------------------
  117.  * FUNCTION NAME: I2C_GBIC_Tx_Dis()
  118.  *-------------------------------------------------------------------------
  119.  * PURPOSE: This function provides I2C GBIC Tx Disabled for SFP module.
  120.  * INPUT :
  121.  * val: [1: disabled | 0: enabled]
  122.  * OUTPUT :
  123.  * none
  124.  *
  125.  * RETURN : True/False.
  126.  * NOTES :
  127.  * History:
  128.  * 2010/04/30 -- Rudolf Chang, Added.
  129.  *-------------------------------------------------------------------------
  130.  */
  131. uint8_t I2C_GBIC_Tx_Dis(uint8_t *val);
  132. #endif /* _SFP_TYPES_H_ */

  133. //sfp_types.c


  134. /******************************************************************************
  135. **
  136. ** sfp_types.c -- SFP types source file for 11noap
  137. **
  138. ** 3-18-2010 -- Rudolf Chang : Initial Creation
  139. **
  140. ******************************************************************************/
  141. #include "sfp_types.h"
  142. #include <common.h>
  143. #include <malloc.h>
  144. #define DISP_LINE_LEN 16
  145. uint8_t SFP_I2C_Discover(SFP_I2C_T *sfp_info)
  146. {
  147.  uint8_t dev_addr;
  148.  uint8_t internal_addr;
  149.  uint8_t data;
  150.  /* 1. I2C switch control probe: iprobe */
  151.  dev_addr = I2C_SW_CTL_CHIP;
  152.  if (0 != i2c_probe(dev_addr))
  153.  {
  154.   printf("(%s[%d]) in sfp_type.c:I2C switch control not detected!\n", __FUNCTION__, __LINE__);
  155.   return 1;
  156.  }
  157.  /* 2. Enable 3rd I2C chanel for CPU to access SFP Module: imw 73 0 4 */
  158.  dev_addr = I2C_SW_CTL_CHIP;
  159.  internal_addr = 0;
  160.  data = I2C_CHANNEL_SFP_EN;
  161.  if (0 != octeon_twsix_write_ia8(dev_addr, internal_addr, data))
  162.  {
  163.   printf("(%s[%d]) in sfp_type.c: Writing the chip of I2C switch control error!\n");
  164.   return 1;
  165.  }
  166.  udelay(11000);
  167.  /* 3. Probe if the SFP module device is found.: iprobe */
  168.  dev_addr = I2C_ETH_SFP_CHIP;
  169.  if (0 != i2c_probe(dev_addr))
  170.  {
  171.   //printf("(%s[%d]) in sfp_type.c:I2C Ethernet SFP module not detected!\n", __FUNCTION__, __LINE__);

  172.   return 1;
  173.  }
  174.  /* 4. Read SFP data : trx_id = imd 50 0 1 , eth_code = imd 50 6 1*/
  175.  dev_addr = I2C_ETH_SFP_CHIP;
  176.  internal_addr = SFP_ID_ADDR;
  177.  if(0 != (octeon_twsix_read_ia8(dev_addr, internal_addr, &data)))
  178.  {
  179.   printf("(%s[%d]) in sfp_type.c: Reading the SFP chip error!\n", __FUNCTION__, __LINE__);
  180.   return 1;
  181.  }
  182.  sfp_info->trx_id = data;
  183.  dev_addr = I2C_ETH_SFP_CHIP;
  184.  internal_addr = SFP_TYPE_ETH_CODE_ADDR;
  185.  if(0 != (octeon_twsix_read_ia8(dev_addr, internal_addr, &data)))
  186.  {
  187.   printf("(%s[%d]) in sfp_type.c: Reading the SFP chip error!\n", __FUNCTION__, __LINE__);
  188.   return 1;
  189.  }
  190.  sfp_info->sfp_eth_code = data;
  191.  return 0;
  192. }
  193. /*-------------------------------------------------------------------------
  194.  * FUNCTION NAME: I2C_IO_Expand()
  195.  *-------------------------------------------------------------------------
  196.  * PURPOSE: This function provides I2C IO Expander read/write through I2C bus.
  197.  * INPUT : port interface [0 | 1]
  198.  * val: value for register write
  199.  * OUTPUT :
  200.  * val: value for register read
  201.  *
  202.  * RETURN : True/False.
  203.  * NOTES :
  204.  * History:
  205.  * 2010/04/29 -- Rudolf Chang, Added.
  206.  *-------------------------------------------------------------------------
  207.  */
  208. uint8_t I2C_IO_Expand(uint8_t port, uint8_t *val)
  209. {
  210.     uint8_t chip = 0;
  211.     uint8_t byte = 0;
  212.     uint16_t alen = 1;
  213.     uint16_t addr = 0;
  214.     uint16_t linebytes = 1;
  215.     uint8_t data;
  216.  uint8_t dev_addr;
  217.  uint8_t internal_addr;
  218.  uint8_t linebuf[DISP_LINE_LEN];

  219.     /* 1. I2C switch control probe: iprobe */
  220.     dev_addr = I2C_SW_CTL_CHIP;
  221.     if (i2c_probe(dev_addr))
  222.     {
  223.         printf("I2C switch control not detected!\n");
  224.         return 1;
  225.     }
  226.     printf("I2C switch control detected!\n");
  227.     /* 2. Enable 4th I2C chanel for CPU to access I2C IO Expander chip: imw 73 0 8 */
  228.     
  229.  internal_addr = 0;
  230.  data = I2C_CHANNEL_IO_EXPA_EN;
  231.  if (0 != octeon_twsix_write_ia8(dev_addr, internal_addr, data))
  232.  {
  233.   printf("[%s]%d: Writing the chip of I2C switch control error!\n", __FUNCTION__, __LINE__);
  234.   return 1;
  235.  }
  236.  udelay(11000);
  237.     printf("Enable 4th I2C chanel for CPU to access I2C IO Expander chip successfully!\n");
  238.     
  239.     /* 3. unlock I2C IO Expander, Read register_0 to unlock this chip = imd 26 0 1*/
  240.     /*
  241.      * We buffer all read data, so we can make sure data is read only once.
  242.      */
  243.     dev_addr = I2C_IO_EXPA_CHIP;
  244.     internal_addr = 0;
  245.      if(octeon_twsix_read_ia8(dev_addr, internal_addr, linebuf) != 0) {
  246.         printf ("[%s]%d: Error reading the IO Expander chip.\n", __FUNCTION__, __LINE__);
  247.         return 1;
  248.     }
  249.     //printf("line buf read is %s\n", linebuf[0]);

  250.     
  251.     /* 4. Probe if the IO Expander chip is found.: iprobe */
  252.     dev_addr = I2C_IO_EXPA_CHIP;
  253.     if ( i2c_probe( dev_addr ) )
  254.     {
  255.         printf("[%s]%d: I2C IO Expander chip not detected!\n", __FUNCTION__, __LINE__);
  256.         return 1;
  257.     }
  258.     printf("I2C IO Expander chip detected successfully!\n");
  259.     /* 5. IO Expander read : imd 26 port 1 */
  260.     dev_addr = I2C_IO_EXPA_CHIP;
  261.     internal_addr = port;
  262.     if ( *val == VAL_NULL ) {
  263.         if(octeon_twsix_read_ia8(dev_addr, internal_addr, linebuf) != 0) {
  264.             printf ("[%s]%d: Error reading the IO Expander chip.\n", __FUNCTION__, __LINE__);
  265.         }
  266.         
  267.         val = linebuf[0];
  268.         printf("I2C_IO_EXP[%x]=0x%x\n", port, val);
  269.         return 0;
  270.     }
  271.     /* 6. IO Expander write */
  272.     /* 6.1 IO Expander Output enable : imw 26 2 0; imw 26 3 0 */
  273.     /*
  274.         byte = 0;
  275.         if(i2c_write(chip, (2+port), alen, &byte, 1) != 0) {
  276.             puts ("Error writing the chip of I2C IO Expander chip.\n");
  277.         }
  278.         udelay(11000);
  279.         */
  280. #if 0
  281.         while (1) {
  282.             i2c_read(chip, (2+port), alen, linebuf, linebytes);
  283.             if (linebuf[0] == byte)
  284.              break;
  285.         }
  286. #endif
  287.     /* 6.2 IO Expander write : imw 26 6 val | imw 26 7 val */
  288.     /*
  289.         byte = *val;
  290.         if(i2c_write(chip, (6+port), alen, &byte, 1) != 0) {
  291.             puts ("Error writing the chip of I2C IO Expander chip.\n");
  292.         }
  293.         udelay(11000);
  294.         */
  295. #if 0
  296.         while (1) {
  297.             i2c_read(chip, (6+port), alen, linebuf, linebytes);
  298.             if (linebuf[0] == *val)
  299.              break;
  300.         }
  301. #endif
  302. #if 0
  303.     /* 6.3 IO Expander Output disabled : imw 26 2 ff; imw 26 3 ff */
  304.         byte = 0xff;
  305.         if(i2c_write(chip, (2+port), alen, &byte, 1) != 0) {
  306.             puts ("Error writing the chip of I2C IO Expander chip.\n");
  307.         }
  308.         udelay(11000);
  309. #endif
  310.     return 0; /* access IO Expander sucess. */
  311. }

  312. /*-------------------------------------------------------------------------
  313.  * FUNCTION NAME: I2C_Get_HWVersion()
  314.  *-------------------------------------------------------------------------
  315.  * PURPOSE: This function provides I2C IO Expander read/write through I2C bus to get hardware version.
  316.  * INPUT : NULL
  317.  * OUTPUT : NULL
  318.  *
  319.  * RETURN :normal: hardware version (eg: 2: ROB; 0: ROA; 1: ROC), abnoraml: -1
  320.  * NOTES :
  321.  * History:
  322.  * 2010/06/10 -- adam xu, Added.
  323.  *-------------------------------------------------------------------------
  324.  */
  325. uint8_t I2C_Get_HWVersion()
  326. {
  327.     uint8_t data;
  328.     uint8_t val;
  329.  uint8_t dev_addr;
  330.  uint8_t internal_addr;
  331.  uint8_t linebuf[DISP_LINE_LEN];

  332.     /* 1. I2C switch control probe: iprobe */
  333.     dev_addr = I2C_SW_CTL_CHIP;
  334.     if (i2c_probe(dev_addr))
  335.     {
  336.         printf("I2C switch control not detected!\n");
  337.         return -1;
  338.     }
  339.     /* 2. Enable 4th I2C chanel for CPU to access I2C IO Expander chip: imw 73 0 8 */
  340.     
  341.  internal_addr = 0;
  342.  data = I2C_CHANNEL_IO_EXPA_EN;
  343.  if (0 != octeon_twsix_write_ia8(dev_addr, internal_addr, data))
  344.  {
  345.   printf("[%s]%d: Writing the chip of I2C switch control error!\n", __FUNCTION__, __LINE__);
  346.   return 1;
  347.  }
  348.  udelay(11000);
  349.     
  350.     /* 3. unlock I2C IO Expander, Read register_0 to unlock this chip = imd 26 0 1*/
  351.     /*
  352.      * We buffer all read data, so we can make sure data is read only once.
  353.      */
  354.     dev_addr = I2C_IO_EXPA_CHIP;
  355.     internal_addr = 0;
  356.      if(octeon_twsix_read_ia8(dev_addr, internal_addr, linebuf) != 0) {
  357.         printf ("[%s]%d: Error reading the IO Expander chip.\n", __FUNCTION__, __LINE__);
  358.         return 1;
  359.     }
  360.     
  361.     /* 4. Probe if the IO Expander chip is found.: iprobe */
  362.     dev_addr = I2C_IO_EXPA_CHIP;
  363.     if ( i2c_probe( dev_addr ) )
  364.     {
  365.         printf("[%s]%d: I2C IO Expander chip not detected!\n", __FUNCTION__, __LINE__);
  366.         return 1;
  367.     }
  368.     /* 5. IO Expander read : imd 26 port 1 */
  369.     dev_addr = I2C_IO_EXPA_CHIP;
  370.     internal_addr = 1;
  371.     if(octeon_twsix_read_ia8(dev_addr, internal_addr, linebuf) != 0) {
  372.         printf ("[%s]%d: Error reading the IO Expander chip.\n", __FUNCTION__, __LINE__);
  373.         return 1;
  374.     }
  375.         
  376.     val = (linebuf[0]>>3) & 0x03;
  377.     return val; /* access IO Expander sucess. */
  378. }

  379. uint8_t I2C_GBIC_Tx_Dis(uint8_t *val)
  380. {
  381.  uint8_t dev_addr;
  382.  uint8_t internal_addr;
  383.  uint8_t data;
  384.  /* 1. I2C switch control probe: iprobe */
  385.  dev_addr = I2C_SW_CTL_CHIP;
  386.  if (0 != i2c_probe(dev_addr))
  387.  {
  388.   printf("(%s[%d]) in sfp_type.c:I2C switch control not detected!\n", __FUNCTION__, __LINE__);
  389.   return 1;
  390.  }
  391.  /* 2. Enable 4th I2C chanel for CPU to access I2C IO Expander chip: imw 73 0 8 */
  392.  internal_addr = 0;
  393.  data = I2C_CHANNEL_IO_EXPA_EN;
  394.  if (0 != octeon_twsix_write_ia8(dev_addr, internal_addr, data))
  395.  {
  396.   printf("(%s[%d]) in sfp_type.c: Writing the chip of I2C switch control error!\n", __FUNCTION__, __LINE__);
  397.   return 1;
  398.  }
  399.  udelay(11000);

  400.  /* 3. IO Expander Output enable : imw 26 2 0; imw 26 3 0 */
  401.  dev_addr = I2C_IO_EXPA_CHIP;
  402.  internal_addr = 2;
  403.  data = 0x04;
  404.  if (0 != octeon_twsix_write_ia8(dev_addr, internal_addr, data))
  405.  {
  406.   printf("(%s[%d]) in sfp_type.c: Writing the chip of I2C IO Expander error!\n", __FUNCTION__, __LINE__);
  407.   return 1;
  408.  }
  409.  udelay(11000);
  410.  /* 4. IO Expander write : imw 26 6 val | imw 26 7 val */
  411.  dev_addr = I2C_IO_EXPA_CHIP;
  412.  internal_addr = 6;
  413.  data = 0x04;
  414.  if (0 != octeon_twsix_write_ia8(dev_addr, internal_addr, data))
  415.  {
  416.   printf("(%s[%d]) in sfp_type.c: Writing the chip of I2C IO Expander error!\n", __FUNCTION__, __LINE__);
  417.   return 1;
  418.  }
  419.  udelay(11000);
  420.  return 0;
  421. }
阅读(3576) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~