openbcm

Git mirror of https://github.com/Broadcom-Network-Switching-Software/OpenBCM
git clone git://git.finwo.net/mirror/broadcom/openbcm
Log | Files | Refs | README

soc.c (9758B)


      1 /* 
      2  * 
      3  * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file.
      4  * 
      5  * Copyright 2007-2019 Broadcom Inc. All rights reserved.
      6  *
      7  * File:        soc.c
      8  * Purpose:     Board driver SOC interfaces
      9  *
     10  * SOC interfaces are defined here; all other functions needed SOC
     11  * intefaces should use these functions.
     12  */
     13 
     14 #include <shared/bsl.h>
     15 
     16 #include <soc/drv.h>
     17 #include <soc/i2c.h>
     18 #include <soc/cmic.h>
     19 
     20 #include <bcm/error.h>
     21 #include <bcm_int/control.h>
     22 #include <board_int/support.h>
     23 
     24 /* device interfaces */
     25 
     26 /* return on error except if BCM_E_UNAVAIL */
     27 #define UOK(__rv__) \
     28   if (BCM_FAILURE(__rv__) && ((__rv__) != BCM_E_UNAVAIL)) { \
     29       return (__rv__); \
     30   }
     31 
     32 /*
     33  * Function:
     34  *     board_num_devices
     35  * Purpose:
     36  *     Returns the number of local devices on the board.
     37  * Parameters:
     38  *     none
     39  * Returns:
     40  *     number of devices
     41  */
     42 int
     43 board_num_devices(void)
     44 {
     45     return soc_ndev;
     46 }
     47 
     48 /*
     49  * Function:
     50  *     board_device_name
     51  * Purpose:
     52  *     Returns a string representing the name of the device
     53  * Parameters:
     54  *     unit - (IN) BCM device number
     55  * Returns:
     56  *     string
     57  */
     58 const char *
     59 board_device_name(int unit)
     60 {
     61     return soc_dev_name(unit);
     62 }
     63 
     64 /*
     65  * Function:
     66  *     board_device_reset
     67  * Purpose:
     68  *     Architecture independent device reset
     69  * Parameters:
     70  *     unit - (IN) BCM device number
     71  * Returns:
     72  *     BCM_E_NONE - success
     73  *     BCM_E_XXX - failed
     74  */
     75 int
     76 board_device_reset(int unit)
     77 {
     78     
     79     UOK(soc_reset_init(unit));
     80     UOK(soc_misc_init(unit));
     81 #if defined(BCM_ESW_SUPPORT)
     82     if (SOC_IS_ESW(unit)) {
     83         UOK(soc_mmu_init(unit));
     84     }
     85 #endif
     86     return BCM_E_NONE;
     87 }
     88 
     89 /*
     90  * Function:
     91  *     board_device_modid_max
     92  * Purpose:
     93  *     Return the largest modid a device supports
     94  * Parameters:
     95  *      unit - (IN) BCM device number
     96  * Returns:
     97  *     modid
     98  */
     99 int
    100 board_device_modid_max(int unit)
    101 {
    102     return SOC_MODID_MAX(unit);
    103 }
    104 
    105 /*
    106  * Function:
    107  *     board_device_info
    108  * Purpose:
    109  *     Returns device PCI info
    110  * Parameters:
    111  *     unit - (IN)  BCM device number
    112  *     info - (OUT) Device PCI info
    113  * Returns:
    114  *     BCM_E_NONE - success
    115  *     BCM_E_XXX - failed
    116  * Notes:
    117  *     The function uses a BCM interface, but does not make any BCM
    118  *     calls.
    119  */
    120 int
    121 board_device_info(int unit, bcm_info_t *info)
    122 {
    123     uint16 dev_id = 0;
    124     uint8 rev_id = 0;
    125 
    126     if (!SOC_UNIT_VALID(unit)) {
    127 	return BCM_E_UNIT;
    128     }
    129     if (info == NULL) {
    130 	return BCM_E_PARAM;
    131     }
    132     soc_cm_get_id(unit, &dev_id, &rev_id);
    133     info->vendor = SOC_PCI_VENDOR(unit);
    134     info->device = dev_id;
    135     info->revision = rev_id;
    136     info->capability = 0;
    137     
    138     return BCM_E_NONE;
    139 }
    140 
    141 /* LED processor */
    142 
    143 
    144 /*
    145  * Function:
    146  *     board_led_program_write
    147  * Purpose:
    148  *     Write device LED program memory
    149  * Parameters:
    150  *     unit - (IN) BCM device number
    151  *     len  - (IN) program length
    152  *     pgm  - (IN) LED program
    153  * Returns:
    154  *     BCM_E_NONE - success
    155  *     BCM_E_XXX - failed
    156  */
    157 int
    158 board_led_program_write(int unit, int len, uint8 *pgm)
    159 {
    160     int i;
    161 
    162     if (len > CMIC_LED_PROGRAM_RAM_SIZE) {
    163         return BCM_E_PARAM;
    164     }
    165 
    166     for (i=0; i<len; i++) {
    167         soc_pci_write(unit, CMIC_LED_PROGRAM_RAM(i), pgm[i]);
    168     }
    169 
    170     return BCM_E_NONE;
    171 }
    172 
    173 /*
    174  * Function:
    175  *     board_led_program_read
    176  * Purpose:
    177  *     Read device LED program memory
    178  * Parameters:
    179  *     unit   - (IN)  BCM device number
    180  *     maxlen - (IN)  maximum program length
    181  *     pgm    - (OUT) LED program
    182  *     actual - (OUT) actual program length
    183  * Returns:
    184  *     BCM_E_NONE - success
    185  *     BCM_E_XXX - failed
    186  */
    187 int
    188 board_led_program_read(int unit, int maxlen, uint8 *pgm, int *actual)
    189 {
    190     int i, rv;
    191 
    192     rv = BCM_E_PARAM;
    193 
    194     if (maxlen > CMIC_LED_PROGRAM_RAM_SIZE) {
    195         goto fail;
    196     }
    197 
    198     for (i=0; i<maxlen; i++) {
    199         pgm[i] = soc_pci_read(unit, CMIC_LED_PROGRAM_RAM(i));
    200     }
    201 
    202     if (maxlen > 0) {
    203         if (actual) {
    204             *actual = maxlen;
    205         }
    206         rv = BCM_E_NONE;
    207     } else {
    208         if (actual) {
    209             rv = BCM_E_NONE;
    210             *actual = CMIC_LED_PROGRAM_RAM_SIZE;
    211         }
    212     }
    213 
    214  fail:
    215     return rv;
    216 }
    217 
    218 
    219 /*
    220  * Function:
    221  *     board_led_data_write
    222  * Purpose:
    223  *     Write device LED data memory
    224  * Parameters:
    225  *     unit - (IN) BCM device number
    226  *     len  - (IN) program length
    227  *     data - (IN) LED data
    228  * Returns:
    229  *     BCM_E_NONE - success
    230  *     BCM_E_XXX - failed
    231  */
    232 int
    233 board_led_data_write(int unit, int len, uint8 *data)
    234 {
    235     int i;
    236 
    237     if (len > CMIC_LED_DATA_RAM_SIZE) {
    238         return BCM_E_PARAM;
    239     }
    240 
    241     for (i=0; i<len; i++) {
    242         soc_pci_write(unit, CMIC_LED_DATA_RAM(i), data[i]);
    243     }
    244 
    245     return BCM_E_NONE;
    246 }
    247 
    248 /*
    249  * Function:
    250  *     board_led_data_read
    251  * Purpose:
    252  *     Read device LED data memory
    253  * Parameters:
    254  *     unit   - (IN)  BCM device number
    255  *     maxlen - (IN)  maximum data length
    256  *     pgm    - (OUT) LED data
    257  *     actual - (OUT) actual data length
    258  * Returns:
    259  *     BCM_E_NONE - success
    260  *     BCM_E_XXX - failed
    261  */
    262 int
    263 board_led_data_read(int unit, int maxlen, uint8 *data, int *actual)
    264 {
    265     int i, rv;
    266 
    267     rv = BCM_E_PARAM;
    268 
    269     if (maxlen > CMIC_LED_DATA_RAM_SIZE || !actual) {
    270         goto fail;
    271     }
    272 
    273     for (i=0; i<maxlen; i++) {
    274         data[i] = soc_pci_read(unit, CMIC_LED_DATA_RAM(i));
    275     }
    276 
    277     if (maxlen > 0) {
    278         if (actual) {
    279             *actual = maxlen;
    280         }
    281         rv = BCM_E_NONE;
    282     } else {
    283         if (actual) {
    284             rv = BCM_E_NONE;
    285             *actual = CMIC_LED_DATA_RAM_SIZE;
    286         }
    287     }
    288 
    289  fail:
    290     return rv;
    291 }
    292 
    293 /*
    294  * Function:
    295  *     board_led_enable_set
    296  * Purpose:
    297  *     Enable LED processor
    298  * Parameters:
    299  *     unit  - (IN) BCM device number
    300  *     value - (IN) TRUE to enable
    301  * Returns:
    302  *     BCM_E_NONE - success
    303  *     BCM_E_XXX - failed
    304  */
    305 int
    306 board_led_enable_set(int unit, int value)
    307 {
    308     uint32 ctrl;
    309 
    310     ctrl = soc_pci_read(unit, CMIC_LED_CTRL);
    311 
    312     if (value) {
    313         ctrl |= LC_LED_ENABLE;
    314     } else {
    315         ctrl &= ~LC_LED_ENABLE;
    316     }
    317 
    318     return BCM_E_NONE;
    319 }
    320 
    321 /*
    322  * Function:
    323  *     board_led_enable_get
    324  * Purpose:
    325  *     Get LED processor enable state
    326  * Parameters:
    327  *     unit  - (IN)  BCM device number
    328  *     value - (OUT) enable state
    329  * Returns:
    330  *     BCM_E_NONE - success
    331  *     BCM_E_XXX - failed
    332  */
    333 int
    334 board_led_enable_get(int unit, int *value)
    335 {
    336     uint32 ctrl;
    337 
    338     if (!value) {
    339         return BCM_E_PARAM;
    340     }
    341 
    342     ctrl = soc_pci_read(unit, CMIC_LED_CTRL);
    343 
    344     *value = ((ctrl & LC_LED_ENABLE) != 0);
    345 
    346     return BCM_E_NONE;
    347 }
    348 
    349 
    350 int
    351 board_led_status_get(int unit, int ctl, int *value)
    352 {
    353     if (!value) {
    354         return BCM_E_PARAM;
    355     }
    356 
    357     *value = soc_pci_read(unit, CMIC_LED_STATUS);
    358 
    359     return BCM_E_NONE;
    360 }
    361 
    362 /* I2C */
    363 #if defined(INCLUDE_I2C)
    364 
    365 #if defined(BROADCOM_DEBUG)
    366 
    367 /*
    368  * Function:
    369  *     board_i2c_device_show_f
    370  * Purpose:
    371  *     board_i2c_device_show traverse callback
    372  * Parameters:
    373  *     unit      - (IN) BCM device number
    374  *     idx       - (IN) I2C device ordinal
    375  *     id        - (IN) I2C device ID
    376  *     name      - (IN) I2C device name
    377  *     desc      - (IN) I2C device description
    378  *     user_data - (IN) callback user data
    379  * Returns:
    380  *     void
    381  */
    382 STATIC void
    383 board_i2c_device_show_f(int unit,
    384                         int idx, int id, char *name, char *desc,
    385                         void *user_data)
    386 {
    387     int *first = (int *)user_data;
    388 
    389     if (!*first) {
    390         LOG_VERBOSE(BSL_LS_BOARD_COMMON,
    391                     (BSL_META_U(unit,
    392                     "Unit %d i2c devices:\n"),
    393                      unit));
    394         LOG_VERBOSE(BSL_LS_BOARD_COMMON,
    395                     (BSL_META_U(unit,
    396                     "idx id  name     description\n")));
    397         LOG_VERBOSE(BSL_LS_BOARD_COMMON,
    398                     (BSL_META_U(unit,
    399                                 "--- --- -------- ---------------------------------\n")));
    400         *first = !*first;
    401     }
    402     LOG_VERBOSE(BSL_LS_BOARD_COMMON,
    403                 (BSL_META_U(unit,
    404                 "%3d %3d %8s %s\n"),
    405                  idx, id, name, desc));
    406 }
    407 
    408 /*
    409  * Function:
    410  *     board_i2c_device_show
    411  * Purpose:
    412  *     Print all I2C devices on unit
    413  * Parameters:
    414  *     unit - (IN) BCM device number
    415  * Returns:
    416  *     BCM_E_NONE - success
    417  *     BCM_E_XXX - failed
    418  */
    419 void
    420 board_i2c_device_show(int unit)
    421 {
    422     int first;
    423 
    424     first = 0;
    425     
    426     board_i2c_device_traverse(unit, board_i2c_device_show_f, (void *)&first);
    427 }
    428 #endif /* BROADCOM_DEBUG */
    429 
    430 /*
    431  * Function:
    432  *     board_i2c_device_init
    433  * Purpose:
    434  *     Attach and probe I2C devices on unit
    435  * Parameters:
    436  *     unit - (IN) BCM device number
    437  * Returns:
    438  *     BCM_E_NONE - success
    439  *     BCM_E_XXX - failed
    440  */
    441 int
    442 board_i2c_device_init(int unit)
    443 {
    444     if (!soc_i2c_is_attached(unit)) {
    445         BCM_IF_ERROR_RETURN(soc_i2c_attach(unit, 0, 0));
    446     }
    447     
    448     BCM_IF_ERROR_RETURN(soc_i2c_probe(unit));
    449 #if defined(BROADCOM_DEBUG)
    450     board_i2c_device_show(unit);
    451 #endif
    452     return BCM_E_NONE;
    453 }
    454 
    455 /*
    456  * Function:
    457  *     board_i2c_device_traverse
    458  * Purpose:
    459  *     Traverse all I2C devices on unit
    460  * Parameters:
    461  *     unit      - (IN) BCM device number
    462  *     func      - (IN) traverse callback function
    463  *     user_data - (IN) calback user data
    464  * Returns:
    465  *     BCM_E_NONE - success
    466  *     BCM_E_XXX - failed
    467  */
    468 int
    469 board_i2c_device_traverse(int unit, board_i2c_device_cb func, void *user_data)
    470 {
    471     int idx;
    472     i2c_device_t* i2c_dev = NULL;
    473 
    474     for (idx = 0; idx < soc_i2c_device_count(unit); idx++) {
    475         i2c_dev = soc_i2c_device(unit, idx) ;
    476         if (i2c_dev) {
    477             func(unit, idx,
    478                  i2c_dev->driver->id, i2c_dev->devname, i2c_dev->desc,
    479                  user_data);
    480         }
    481     }
    482 
    483     return BCM_E_NONE;
    484 }
    485 
    486 #endif