Home | History | Annotate | Download | only in hal
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2012-2013 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 
     20 /******************************************************************************
     21  *
     22  *  Vendor-specific handler for DM events
     23  *
     24  ******************************************************************************/
     25 #include <string.h>
     26 #include "nfc_hal_int.h"
     27 #include "nfc_hal_post_reset.h"
     28 #include "userial.h"
     29 #include "upio.h"
     30 
     31 /*****************************************************************************
     32 ** Constants and types
     33 *****************************************************************************/
     34 
     35 #define NFC_HAL_I93_RW_CFG_LEN              (5)
     36 #define NFC_HAL_I93_RW_CFG_PARAM_LEN        (3)
     37 #define NFC_HAL_I93_AFI                     (0)
     38 #define NFC_HAL_I93_ENABLE_SMART_POLL       (1)
     39 
     40 static UINT8 nfc_hal_dm_i93_rw_cfg[NFC_HAL_I93_RW_CFG_LEN] =
     41 {
     42     NCI_PARAM_ID_I93_DATARATE,
     43     NFC_HAL_I93_RW_CFG_PARAM_LEN,
     44     NFC_HAL_I93_FLAG_DATA_RATE,    /* Bit0:Sub carrier, Bit1:Data rate, Bit4:Enable/Disable AFI */
     45     NFC_HAL_I93_AFI,               /* AFI if Bit 4 is set in the flag byte */
     46     NFC_HAL_I93_ENABLE_SMART_POLL  /* Bit0:Enable/Disable smart poll */
     47 };
     48 
     49 static UINT8 nfc_hal_dm_set_fw_fsm_cmd[NCI_MSG_HDR_SIZE + 1] =
     50 {
     51     NCI_MTS_CMD|NCI_GID_PROP,
     52     NCI_MSG_SET_FWFSM,
     53     0x01,
     54     0x00,
     55 };
     56 #define NCI_SET_FWFSM_OFFSET_ENABLE      3
     57 
     58 #define NCI_PROP_PARAM_SIZE_XTAL_INDEX      3       /* length of parameters in XTAL_INDEX CMD */
     59 
     60 const UINT8 nfc_hal_dm_get_build_info_cmd[NCI_MSG_HDR_SIZE] =
     61 {
     62     NCI_MTS_CMD|NCI_GID_PROP,
     63     NCI_MSG_GET_BUILD_INFO,
     64     0x00
     65 };
     66 #define NCI_BUILD_INFO_OFFSET_HWID  25  /* HW ID offset in build info RSP */
     67 
     68 const UINT8 nfc_hal_dm_get_patch_version_cmd [NCI_MSG_HDR_SIZE] =
     69 {
     70     NCI_MTS_CMD|NCI_GID_PROP,
     71     NCI_MSG_GET_PATCH_VERSION,
     72     0x00
     73 };
     74 #define NCI_PATCH_INFO_VERSION_LEN  16  /* Length of patch version string in PATCH_INFO */
     75 
     76 /* Version string for BCM20791B3 */
     77 const UINT8 NFC_HAL_DM_BCM20791B3_STR[]   = "20791B3";
     78 #define NFC_HAL_DM_BCM20791B3_STR_LEN     (sizeof (NFC_HAL_DM_BCM20791B3_STR)-1)
     79 
     80 /* Version string for BCM20791B4 */
     81 const UINT8 NFC_HAL_DM_BCM20791B4_STR[]   = "20791B4";
     82 #define NFC_HAL_DM_BCM20791B4_STR_LEN     (sizeof (NFC_HAL_DM_BCM20791B4_STR)-1)
     83 
     84 /* Version string for BCM43341B0 */
     85 const UINT8 NFC_HAL_DM_BCM43341B0_STR[]   = "43341B0";
     86 #define NFC_HAL_DM_BCM43341B0_STR_LEN     (sizeof (NFC_HAL_DM_BCM43341B0_STR)-1)
     87 
     88 /*****************************************************************************
     89 ** Extern function prototypes
     90 *****************************************************************************/
     91 extern UINT8 *p_nfc_hal_dm_lptd_cfg;
     92 extern UINT8 *p_nfc_hal_dm_pll_325_cfg;
     93 extern UINT8 *p_nfc_hal_dm_start_up_cfg;
     94 extern UINT8 *p_nfc_hal_dm_start_up_vsc_cfg;
     95 extern tNFC_HAL_CFG *p_nfc_hal_cfg;
     96 
     97 /*****************************************************************************
     98 ** Local function prototypes
     99 *****************************************************************************/
    100 
    101 /*******************************************************************************
    102 **
    103 ** Function         nfc_hal_dm_set_config
    104 **
    105 ** Description      Send NCI config items to NFCC
    106 **
    107 ** Returns          tHAL_NFC_STATUS
    108 **
    109 *******************************************************************************/
    110 tHAL_NFC_STATUS nfc_hal_dm_set_config (UINT8 tlv_size,
    111                                        UINT8 *p_param_tlvs,
    112                                        tNFC_HAL_NCI_CBACK *p_cback)
    113 {
    114     UINT8  *p_buff, *p;
    115     UINT8  num_param = 0, param_len, rem_len, *p_tlv;
    116     UINT16 cmd_len = NCI_MSG_HDR_SIZE + tlv_size + 1;
    117     tHAL_NFC_STATUS status = HAL_NFC_STATUS_FAILED;
    118 
    119     if ((tlv_size == 0)||(p_param_tlvs == NULL))
    120     {
    121         return status;
    122     }
    123 
    124     if ((p_buff = (UINT8 *) GKI_getbuf ((UINT16)(NCI_MSG_HDR_SIZE + tlv_size))) != NULL)
    125     {
    126         p = p_buff;
    127 
    128         NCI_MSG_BLD_HDR0 (p, NCI_MT_CMD, NCI_GID_CORE);
    129         NCI_MSG_BLD_HDR1 (p, NCI_MSG_CORE_SET_CONFIG);
    130         UINT8_TO_STREAM  (p, (UINT8) (tlv_size + 1));
    131 
    132         rem_len = tlv_size;
    133         p_tlv   = p_param_tlvs;
    134         while (rem_len > 1)
    135         {
    136             num_param++;                /* number of params */
    137 
    138             p_tlv ++;                   /* param type   */
    139             param_len = *p_tlv++;       /* param length */
    140 
    141             rem_len -= 2;               /* param type and length */
    142             if (rem_len >= param_len)
    143             {
    144                 rem_len -= param_len;
    145                 p_tlv   += param_len;   /* next param_type */
    146 
    147                 if (rem_len == 0)
    148                 {
    149                     status = HAL_NFC_STATUS_OK;
    150                     break;
    151                 }
    152             }
    153             else
    154             {
    155                 /* error found */
    156                 break;
    157             }
    158         }
    159 
    160         if (status == HAL_NFC_STATUS_OK)
    161         {
    162             UINT8_TO_STREAM (p, num_param);
    163             ARRAY_TO_STREAM (p, p_param_tlvs, tlv_size);
    164 
    165             nfc_hal_dm_send_nci_cmd (p_buff, cmd_len, p_cback);
    166         }
    167         else
    168         {
    169             HAL_TRACE_ERROR0 ("nfc_hal_dm_set_config ():Bad TLV");
    170         }
    171 
    172         GKI_freebuf (p_buff);
    173     }
    174 
    175     return status;
    176 }
    177 
    178 /*******************************************************************************
    179 **
    180 ** Function         nfc_hal_dm_set_fw_fsm
    181 **
    182 ** Description      Enable or disable FW FSM
    183 **
    184 ** Returns          void
    185 **
    186 *******************************************************************************/
    187 void nfc_hal_dm_set_fw_fsm (BOOLEAN enable, tNFC_HAL_NCI_CBACK *p_cback)
    188 {
    189     if (enable)
    190         nfc_hal_dm_set_fw_fsm_cmd[NCI_SET_FWFSM_OFFSET_ENABLE] = 0x01; /* Enable, default is disabled */
    191     else
    192         nfc_hal_dm_set_fw_fsm_cmd[NCI_SET_FWFSM_OFFSET_ENABLE] = 0x00; /* Disable */
    193 
    194     nfc_hal_dm_send_nci_cmd (nfc_hal_dm_set_fw_fsm_cmd, NCI_MSG_HDR_SIZE + 1, p_cback);
    195 }
    196 
    197 /*******************************************************************************
    198 **
    199 ** Function         nfc_hal_dm_config_nfcc_cback
    200 **
    201 ** Description      Callback for NCI vendor specific command complete
    202 **
    203 ** Returns          void
    204 **
    205 *******************************************************************************/
    206 void nfc_hal_dm_config_nfcc_cback (tNFC_HAL_NCI_EVT event, UINT16 data_len, UINT8 *p_data)
    207 {
    208     if (nfc_hal_cb.dev_cb.next_dm_config == NFC_HAL_DM_CONFIG_NONE)
    209     {
    210         nfc_hal_hci_enable ();
    211     }
    212     else
    213     {
    214         nfc_hal_dm_config_nfcc ();
    215     }
    216 }
    217 
    218 /*******************************************************************************
    219 **
    220 ** Function         nfc_hal_dm_send_startup_vsc
    221 **
    222 ** Description      Send VS command before NFA start-up
    223 **
    224 ** Returns          None
    225 **
    226 *******************************************************************************/
    227 void nfc_hal_dm_send_startup_vsc (void)
    228 {
    229     UINT8  *p, *p_end;
    230     UINT16 len;
    231 
    232     HAL_TRACE_DEBUG0 ("nfc_hal_dm_send_startup_vsc ()");
    233 
    234     /* VSC must have NCI header at least */
    235     if (nfc_hal_cb.dev_cb.next_startup_vsc + NCI_MSG_HDR_SIZE - 1 <= *p_nfc_hal_dm_start_up_vsc_cfg)
    236     {
    237         p     = p_nfc_hal_dm_start_up_vsc_cfg + nfc_hal_cb.dev_cb.next_startup_vsc;
    238         len   = *(p + 2);
    239         p_end = p + NCI_MSG_HDR_SIZE - 1 + len;
    240 
    241         if (p_end <= p_nfc_hal_dm_start_up_vsc_cfg + *p_nfc_hal_dm_start_up_vsc_cfg)
    242         {
    243             /* move to next VSC */
    244             nfc_hal_cb.dev_cb.next_startup_vsc += NCI_MSG_HDR_SIZE + len;
    245 
    246             /* if this is last VSC */
    247             if (p_end == p_nfc_hal_dm_start_up_vsc_cfg + *p_nfc_hal_dm_start_up_vsc_cfg)
    248                 nfc_hal_cb.dev_cb.next_dm_config = NFC_HAL_DM_CONFIG_NONE;
    249 
    250             nfc_hal_dm_send_nci_cmd (p, (UINT16)(NCI_MSG_HDR_SIZE + len), nfc_hal_dm_config_nfcc_cback);
    251             return;
    252         }
    253     }
    254 
    255     HAL_TRACE_ERROR0 ("nfc_hal_dm_send_startup_vsc (): Bad start-up VSC");
    256 
    257     NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_IDLE);
    258     nfc_hal_cb.p_stack_cback (HAL_NFC_POST_INIT_CPLT_EVT, HAL_NFC_STATUS_FAILED);
    259 }
    260 
    261 /*******************************************************************************
    262 **
    263 ** Function         nfc_hal_dm_config_nfcc
    264 **
    265 ** Description      Send VS config before NFA start-up
    266 **
    267 ** Returns          void
    268 **
    269 *******************************************************************************/
    270 void nfc_hal_dm_config_nfcc (void)
    271 {
    272     HAL_TRACE_DEBUG1 ("nfc_hal_dm_config_nfcc (): next_dm_config = %d", nfc_hal_cb.dev_cb.next_dm_config);
    273 
    274     if ((p_nfc_hal_dm_lptd_cfg[0]) && (nfc_hal_cb.dev_cb.next_dm_config <= NFC_HAL_DM_CONFIG_LPTD))
    275     {
    276         nfc_hal_cb.dev_cb.next_dm_config = NFC_HAL_DM_CONFIG_PLL_325;
    277 
    278         if (nfc_hal_dm_set_config (p_nfc_hal_dm_lptd_cfg[0],
    279                                    &p_nfc_hal_dm_lptd_cfg[1],
    280                                    nfc_hal_dm_config_nfcc_cback) == HAL_NFC_STATUS_OK)
    281         {
    282             return;
    283         }
    284         else
    285         {
    286             NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_IDLE);
    287             nfc_hal_cb.p_stack_cback (HAL_NFC_POST_INIT_CPLT_EVT, HAL_NFC_STATUS_FAILED);
    288             return;
    289         }
    290     }
    291 
    292     if ((p_nfc_hal_dm_pll_325_cfg) && (nfc_hal_cb.dev_cb.next_dm_config <= NFC_HAL_DM_CONFIG_PLL_325))
    293     {
    294         nfc_hal_cb.dev_cb.next_dm_config = NFC_HAL_DM_CONFIG_START_UP;
    295 
    296         if (nfc_hal_dm_set_config (NFC_HAL_PLL_325_SETCONFIG_PARAM_LEN,
    297                                    p_nfc_hal_dm_pll_325_cfg,
    298                                    nfc_hal_dm_config_nfcc_cback) == HAL_NFC_STATUS_OK)
    299         {
    300             return;
    301         }
    302         else
    303         {
    304             NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_IDLE);
    305             nfc_hal_cb.p_stack_cback (HAL_NFC_POST_INIT_CPLT_EVT, HAL_NFC_STATUS_FAILED);
    306             return;
    307         }
    308     }
    309 
    310     if ((p_nfc_hal_dm_start_up_cfg[0]) && (nfc_hal_cb.dev_cb.next_dm_config <= NFC_HAL_DM_CONFIG_START_UP))
    311     {
    312         nfc_hal_cb.dev_cb.next_dm_config = NFC_HAL_DM_CONFIG_I93_DATA_RATE;
    313         if (nfc_hal_dm_set_config (p_nfc_hal_dm_start_up_cfg[0],
    314                                    &p_nfc_hal_dm_start_up_cfg[1],
    315                                    nfc_hal_dm_config_nfcc_cback) == HAL_NFC_STATUS_OK)
    316         {
    317             return;
    318         }
    319         else
    320         {
    321             NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_IDLE);
    322             nfc_hal_cb.p_stack_cback (HAL_NFC_POST_INIT_CPLT_EVT, HAL_NFC_STATUS_FAILED);
    323             return;
    324         }
    325     }
    326 
    327 #if (NFC_HAL_I93_FLAG_DATA_RATE == NFC_HAL_I93_FLAG_DATA_RATE_HIGH)
    328     if (nfc_hal_cb.dev_cb.next_dm_config  <= NFC_HAL_DM_CONFIG_I93_DATA_RATE)
    329     {
    330         nfc_hal_cb.dev_cb.next_dm_config = NFC_HAL_DM_CONFIG_FW_FSM;
    331         if (nfc_hal_dm_set_config (NFC_HAL_I93_RW_CFG_LEN,
    332                                    nfc_hal_dm_i93_rw_cfg,
    333                                    nfc_hal_dm_config_nfcc_cback) == HAL_NFC_STATUS_OK)
    334         {
    335             return;
    336         }
    337         else
    338         {
    339             NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_IDLE);
    340             nfc_hal_cb.p_stack_cback (HAL_NFC_POST_INIT_CPLT_EVT, HAL_NFC_STATUS_FAILED);
    341             return;
    342         }
    343     }
    344 #endif
    345 
    346     /* FW FSM is disabled as default in NFCC */
    347     if (nfc_hal_cb.dev_cb.next_dm_config <= NFC_HAL_DM_CONFIG_FW_FSM)
    348     {
    349         nfc_hal_cb.dev_cb.next_dm_config = NFC_HAL_DM_CONFIG_START_UP_VSC;
    350         nfc_hal_dm_set_fw_fsm (NFC_HAL_DM_MULTI_TECH_RESP, nfc_hal_dm_config_nfcc_cback);
    351         return;
    352     }
    353 
    354     if (nfc_hal_cb.dev_cb.next_dm_config <= NFC_HAL_DM_CONFIG_START_UP_VSC)
    355     {
    356         if (p_nfc_hal_dm_start_up_vsc_cfg && *p_nfc_hal_dm_start_up_vsc_cfg)
    357         {
    358             nfc_hal_dm_send_startup_vsc ();
    359             return;
    360         }
    361     }
    362 
    363     /* nothing to config */
    364     nfc_hal_cb.dev_cb.next_dm_config = NFC_HAL_DM_CONFIG_NONE;
    365     nfc_hal_dm_config_nfcc_cback (0, 0, NULL);
    366 }
    367 
    368 /*******************************************************************************
    369 **
    370 ** Function:    nfc_hal_dm_get_xtal_index
    371 **
    372 ** Description: Return Xtal index and frequency
    373 **
    374 ** Returns:     tNFC_HAL_XTAL_INDEX
    375 **
    376 *******************************************************************************/
    377 tNFC_HAL_XTAL_INDEX nfc_hal_dm_get_xtal_index (UINT32 brcm_hw_id, UINT16 *p_xtal_freq)
    378 {
    379     UINT8 xx;
    380 
    381     HAL_TRACE_DEBUG1("nfc_hal_dm_get_xtal_index() brcm_hw_id:0x%x", brcm_hw_id);
    382 
    383     for (xx = 0; xx < nfc_post_reset_cb.dev_init_config.num_xtal_cfg; xx++)
    384     {
    385         if (brcm_hw_id == nfc_post_reset_cb.dev_init_config.xtal_cfg[xx].brcm_hw_id)
    386         {
    387             *p_xtal_freq = nfc_post_reset_cb.dev_init_config.xtal_cfg[xx].xtal_freq;
    388             return (nfc_post_reset_cb.dev_init_config.xtal_cfg[xx].xtal_index);
    389         }
    390     }
    391 
    392     /* if not found */
    393     *p_xtal_freq = 0;
    394     return (NFC_HAL_XTAL_INDEX_MAX);
    395 }
    396 
    397 /*******************************************************************************
    398 **
    399 ** Function         nfc_hal_dm_set_xtal_freq_index
    400 **
    401 ** Description      Set crystal frequency index
    402 **
    403 ** Returns          void
    404 **
    405 *******************************************************************************/
    406 void nfc_hal_dm_set_xtal_freq_index (void)
    407 {
    408     UINT8 nci_brcm_xtal_index_cmd[NCI_MSG_HDR_SIZE + NCI_PROP_PARAM_SIZE_XTAL_INDEX];
    409     UINT8 *p;
    410     tNFC_HAL_XTAL_INDEX xtal_index;
    411     UINT16              xtal_freq;
    412 
    413     HAL_TRACE_DEBUG1 ("nfc_hal_dm_set_xtal_freq_index (): brcm_hw_id = 0x%x", nfc_hal_cb.dev_cb.brcm_hw_id);
    414 
    415     xtal_index = nfc_hal_dm_get_xtal_index (nfc_hal_cb.dev_cb.brcm_hw_id, &xtal_freq);
    416 
    417     p = nci_brcm_xtal_index_cmd;
    418     UINT8_TO_STREAM  (p, (NCI_MTS_CMD|NCI_GID_PROP));
    419     UINT8_TO_STREAM  (p, NCI_MSG_GET_XTAL_INDEX_FROM_DH);
    420     UINT8_TO_STREAM  (p, NCI_PROP_PARAM_SIZE_XTAL_INDEX);
    421     UINT8_TO_STREAM  (p, xtal_index);
    422     UINT16_TO_STREAM (p, xtal_freq);
    423 
    424     NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_W4_XTAL_SET);
    425 
    426     nfc_hal_dm_send_nci_cmd (nci_brcm_xtal_index_cmd, NCI_MSG_HDR_SIZE + NCI_PROP_PARAM_SIZE_XTAL_INDEX, NULL);
    427 }
    428 
    429 /*******************************************************************************
    430 **
    431 ** Function         nfc_hal_dm_send_get_build_info_cmd
    432 **
    433 ** Description      Send NCI_MSG_GET_BUILD_INFO CMD
    434 **
    435 ** Returns          void
    436 **
    437 *******************************************************************************/
    438 void nfc_hal_dm_send_get_build_info_cmd (void)
    439 {
    440     NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_W4_BUILD_INFO);
    441 
    442     /* get build information to find out HW */
    443     nfc_hal_dm_send_nci_cmd (nfc_hal_dm_get_build_info_cmd, NCI_MSG_HDR_SIZE, NULL);
    444 }
    445 
    446 /*******************************************************************************
    447 **
    448 ** Function         nfc_hal_dm_proc_msg_during_init
    449 **
    450 ** Description      Process NCI message while initializing NFCC
    451 **
    452 ** Returns          void
    453 **
    454 *******************************************************************************/
    455 void nfc_hal_dm_proc_msg_during_init (NFC_HDR *p_msg)
    456 {
    457     UINT8 *p;
    458     UINT8 reset_reason, reset_type;
    459     UINT8 mt, pbf, gid, op_code;
    460     UINT8 *p_old, old_gid, old_oid, old_mt;
    461     UINT8 u8;
    462     tNFC_HAL_NCI_CBACK *p_cback = NULL;
    463     UINT8   chipverlen;
    464     UINT8   chipverstr[NCI_SPD_HEADER_CHIPVER_LEN];
    465     UINT16  xtal_freq;
    466 
    467     HAL_TRACE_DEBUG1 ("nfc_hal_dm_proc_msg_during_init(): init state:%d", nfc_hal_cb.dev_cb.initializing_state);
    468 
    469     p = (UINT8 *) (p_msg + 1) + p_msg->offset;
    470 
    471     NCI_MSG_PRS_HDR0 (p, mt, pbf, gid);
    472     NCI_MSG_PRS_HDR1 (p, op_code);
    473 
    474     /* check if waiting for this response */
    475     if (  (nfc_hal_cb.ncit_cb.nci_wait_rsp == NFC_HAL_WAIT_RSP_CMD)
    476         ||(nfc_hal_cb.ncit_cb.nci_wait_rsp == NFC_HAL_WAIT_RSP_VSC)  )
    477     {
    478         if (mt == NCI_MT_RSP)
    479         {
    480             p_old = nfc_hal_cb.ncit_cb.last_hdr;
    481             NCI_MSG_PRS_HDR0 (p_old, old_mt, pbf, old_gid);
    482             old_oid = ((*p_old) & NCI_OID_MASK);
    483             /* make sure this is the RSP we are waiting for before updating the command window */
    484             if ((old_gid == gid) && (old_oid == op_code))
    485             {
    486                 nfc_hal_cb.ncit_cb.nci_wait_rsp = NFC_HAL_WAIT_RSP_NONE;
    487                 p_cback = (tNFC_HAL_NCI_CBACK *)nfc_hal_cb.ncit_cb.p_vsc_cback;
    488                 nfc_hal_cb.ncit_cb.p_vsc_cback  = NULL;
    489                 nfc_hal_main_stop_quick_timer (&nfc_hal_cb.ncit_cb.nci_wait_rsp_timer);
    490             }
    491         }
    492     }
    493 
    494     if (gid == NCI_GID_CORE)
    495     {
    496         if (op_code == NCI_MSG_CORE_RESET)
    497         {
    498             if (mt == NCI_MT_NTF)
    499             {
    500                 if (  (nfc_hal_cb.dev_cb.initializing_state == NFC_HAL_INIT_STATE_W4_NFCC_ENABLE)
    501                     ||(nfc_hal_cb.dev_cb.initializing_state == NFC_HAL_INIT_STATE_POST_XTAL_SET)  )
    502                 {
    503                     /*
    504                     ** Core reset ntf in the following cases;
    505                     ** 1) after power up (raising REG_PU)
    506                     ** 2) after setting xtal index
    507                     ** Start pre-initializing NFCC
    508                     */
    509                     nfc_hal_main_stop_quick_timer (&nfc_hal_cb.timer);
    510                     nfc_hal_dm_pre_init_nfcc ();
    511                 }
    512                 else
    513                 {
    514                     /* Core reset ntf after post-patch download, Call reset notification callback */
    515                     p++;                                /* Skip over param len */
    516                     STREAM_TO_UINT8 (reset_reason, p);
    517                     STREAM_TO_UINT8 (reset_type, p);
    518                     nfc_hal_prm_spd_reset_ntf (reset_reason, reset_type);
    519                 }
    520             }
    521         }
    522         else if (p_cback)
    523         {
    524             (*p_cback) ((tNFC_HAL_NCI_EVT) (op_code),
    525                         p_msg->len,
    526                         (UINT8 *) (p_msg + 1) + p_msg->offset);
    527         }
    528     }
    529     else if (gid == NCI_GID_PROP) /* this is for download patch */
    530     {
    531         if (mt == NCI_MT_NTF)
    532             op_code |= NCI_NTF_BIT;
    533         else
    534             op_code |= NCI_RSP_BIT;
    535 
    536         if (nfc_hal_cb.dev_cb.initializing_state == NFC_HAL_INIT_STATE_W4_XTAL_SET)
    537         {
    538             if (op_code == (NCI_RSP_BIT|NCI_MSG_GET_XTAL_INDEX_FROM_DH))
    539             {
    540                 /* start timer in case that NFCC doesn't send RESET NTF after loading patch from NVM */
    541                 NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_POST_XTAL_SET);
    542 
    543                 nfc_hal_main_start_quick_timer (&nfc_hal_cb.timer, NFC_HAL_TTYPE_NFCC_ENABLE,
    544                                                 ((p_nfc_hal_cfg->nfc_hal_post_xtal_timeout)*QUICK_TIMER_TICKS_PER_SEC)/1000);
    545             }
    546         }
    547         else if (  (op_code == NFC_VS_GET_BUILD_INFO_EVT)
    548                  &&(nfc_hal_cb.dev_cb.initializing_state == NFC_HAL_INIT_STATE_W4_BUILD_INFO)  )
    549         {
    550             p += NCI_BUILD_INFO_OFFSET_HWID;
    551 
    552             STREAM_TO_UINT32 (nfc_hal_cb.dev_cb.brcm_hw_id, p);
    553 
    554             STREAM_TO_UINT8 (chipverlen, p);
    555             memset (chipverstr, 0, NCI_SPD_HEADER_CHIPVER_LEN);
    556 
    557             STREAM_TO_ARRAY (chipverstr, p, chipverlen);
    558 
    559             if ((chipverlen == NFC_HAL_DM_BCM20791B3_STR_LEN) && (memcmp (NFC_HAL_DM_BCM20791B3_STR, chipverstr, NFC_HAL_DM_BCM20791B3_STR_LEN) == 0))
    560             {
    561                 /* BCM2079B3 FW - eSE restarted for patch download */
    562                 nfc_hal_cb.hci_cb.hci_fw_workaround         = TRUE;
    563                 nfc_hal_cb.hci_cb.hci_fw_validate_netwk_cmd = TRUE;
    564             }
    565             else if (  ((chipverlen == NFC_HAL_DM_BCM20791B4_STR_LEN) && (memcmp (NFC_HAL_DM_BCM20791B4_STR, chipverstr, NFC_HAL_DM_BCM20791B4_STR_LEN) == 0))
    566                      ||((chipverlen == NFC_HAL_DM_BCM43341B0_STR_LEN) && (memcmp (NFC_HAL_DM_BCM43341B0_STR, chipverstr, NFC_HAL_DM_BCM43341B0_STR_LEN) == 0))  )
    567             {
    568                 /* BCM43341B0/BCM2079B4 FW - eSE restarted for patch download */
    569                 nfc_hal_cb.hci_cb.hci_fw_workaround         = TRUE;
    570                 nfc_hal_cb.hci_cb.hci_fw_validate_netwk_cmd = FALSE;
    571             }
    572             else
    573             {
    574                 /* BCM2079B5 FW - eSE not be restarted for patch download from UICC */
    575                 nfc_hal_cb.hci_cb.hci_fw_workaround         = FALSE;
    576                 nfc_hal_cb.hci_cb.hci_fw_validate_netwk_cmd = FALSE;
    577             }
    578 
    579             /* if NFCC needs to set Xtal frequency before getting patch version */
    580             if (nfc_hal_dm_get_xtal_index (nfc_hal_cb.dev_cb.brcm_hw_id, &xtal_freq) < NFC_HAL_XTAL_INDEX_MAX)
    581             {
    582                 {
    583                     /* set Xtal index before getting patch version */
    584                     nfc_hal_dm_set_xtal_freq_index ();
    585                     return;
    586                 }
    587             }
    588 
    589             NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_W4_PATCH_INFO);
    590 
    591             nfc_hal_dm_send_nci_cmd (nfc_hal_dm_get_patch_version_cmd, NCI_MSG_HDR_SIZE, NULL);
    592         }
    593         else if (  (op_code == NFC_VS_GET_PATCH_VERSION_EVT)
    594                  &&(nfc_hal_cb.dev_cb.initializing_state == NFC_HAL_INIT_STATE_W4_PATCH_INFO)  )
    595         {
    596             /* Store NVM info to control block */
    597 
    598             /* Skip over rsp len */
    599             p++;
    600 
    601             /* Get project id */
    602             STREAM_TO_UINT16 (nfc_hal_cb.nvm_cb.project_id, p);
    603 
    604             /* RFU */
    605             p++;
    606 
    607             /* Get chip version string */
    608             STREAM_TO_UINT8 (u8, p);
    609             p += NCI_PATCH_INFO_VERSION_LEN;
    610 
    611             /* Get major/minor version */
    612             STREAM_TO_UINT16 (nfc_hal_cb.nvm_cb.ver_major, p);
    613             STREAM_TO_UINT16 (nfc_hal_cb.nvm_cb.ver_minor, p);
    614 
    615             /* Skip over max_size and patch_max_size */
    616             p += 4;
    617 
    618             /* Get current lpm patch size */
    619             STREAM_TO_UINT16 (nfc_hal_cb.nvm_cb.lpm_size, p);
    620             STREAM_TO_UINT16 (nfc_hal_cb.nvm_cb.fpm_size, p);
    621 
    622             /* clear all flags which may be set during previous initialization */
    623             nfc_hal_cb.nvm_cb.flags = 0;
    624 
    625             /* Set patch present flag */
    626             if ((nfc_hal_cb.nvm_cb.fpm_size) || (nfc_hal_cb.nvm_cb.lpm_size))
    627                 nfc_hal_cb.nvm_cb.flags |= NFC_HAL_NVM_FLAGS_PATCH_PRESENT;
    628 
    629             /* LPMPatchCodeHasBadCRC (if not bad crc, then indicate LPM patch is present in nvm) */
    630             STREAM_TO_UINT8 (u8, p);
    631             if (u8)
    632             {
    633                 /* LPM patch in NVM fails CRC check */
    634                 nfc_hal_cb.nvm_cb.flags |= NFC_HAL_NVM_FLAGS_LPM_BAD;
    635             }
    636 
    637 
    638             /* FPMPatchCodeHasBadCRC (if not bad crc, then indicate LPM patch is present in nvm) */
    639             STREAM_TO_UINT8 (u8, p);
    640             if (u8)
    641             {
    642                 /* FPM patch in NVM fails CRC check */
    643                 nfc_hal_cb.nvm_cb.flags |= NFC_HAL_NVM_FLAGS_FPM_BAD;
    644             }
    645 
    646             /* Check if downloading patch to RAM only (no NVM) */
    647             STREAM_TO_UINT8 (nfc_hal_cb.nvm_cb.nvm_type, p);
    648             if (nfc_hal_cb.nvm_cb.nvm_type == NCI_SPD_NVM_TYPE_NONE)
    649             {
    650                 nfc_hal_cb.nvm_cb.flags |= NFC_HAL_NVM_FLAGS_NO_NVM;
    651             }
    652 
    653             /* let platform update baudrate or download patch */
    654             NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_W4_APP_COMPLETE);
    655             nfc_hal_post_reset_init (nfc_hal_cb.dev_cb.brcm_hw_id, nfc_hal_cb.nvm_cb.nvm_type);
    656         }
    657         else if (p_cback)
    658         {
    659             (*p_cback) ((tNFC_HAL_NCI_EVT) (op_code),
    660                         p_msg->len,
    661                         (UINT8 *) (p_msg + 1) + p_msg->offset);
    662         }
    663         else if (op_code == NFC_VS_SEC_PATCH_AUTH_EVT)
    664         {
    665             HAL_TRACE_DEBUG0 ("signature!!");
    666             nfc_hal_prm_nci_command_complete_cback ((tNFC_HAL_NCI_EVT) (op_code),
    667                                                     p_msg->len,
    668                                                     (UINT8 *) (p_msg + 1) + p_msg->offset);
    669         }
    670     }
    671 }
    672 
    673 /*******************************************************************************
    674 **
    675 ** Function         nfc_hal_dm_send_nci_cmd
    676 **
    677 ** Description      Send NCI command to NFCC while initializing BRCM NFCC
    678 **
    679 ** Returns          void
    680 **
    681 *******************************************************************************/
    682 void nfc_hal_dm_send_nci_cmd (const UINT8 *p_data, UINT16 len, tNFC_HAL_NCI_CBACK *p_cback)
    683 {
    684     NFC_HDR *p_buf;
    685     UINT8  *ps;
    686 
    687     HAL_TRACE_DEBUG1 ("nfc_hal_dm_send_nci_cmd (): nci_wait_rsp = 0x%x", nfc_hal_cb.ncit_cb.nci_wait_rsp);
    688 
    689     if (nfc_hal_cb.ncit_cb.nci_wait_rsp != NFC_HAL_WAIT_RSP_NONE)
    690     {
    691         HAL_TRACE_ERROR0 ("nfc_hal_dm_send_nci_cmd(): no command window");
    692         return;
    693     }
    694 
    695     if ((p_buf = (NFC_HDR *)GKI_getpoolbuf (NFC_HAL_NCI_POOL_ID)) != NULL)
    696     {
    697         nfc_hal_cb.ncit_cb.nci_wait_rsp = NFC_HAL_WAIT_RSP_VSC;
    698 
    699         p_buf->offset = NFC_HAL_NCI_MSG_OFFSET_SIZE;
    700         p_buf->event  = NFC_HAL_EVT_TO_NFC_NCI;
    701         p_buf->len    = len;
    702 
    703         memcpy ((UINT8*) (p_buf + 1) + p_buf->offset, p_data, len);
    704 
    705         /* Keep a copy of the command and send to NCI transport */
    706 
    707         /* save the message header to double check the response */
    708         ps   = (UINT8 *)(p_buf + 1) + p_buf->offset;
    709         memcpy(nfc_hal_cb.ncit_cb.last_hdr, ps, NFC_HAL_SAVED_HDR_SIZE);
    710         memcpy(nfc_hal_cb.ncit_cb.last_cmd, ps + NCI_MSG_HDR_SIZE, NFC_HAL_SAVED_CMD_SIZE);
    711 
    712         /* save the callback for NCI VSCs */
    713         nfc_hal_cb.ncit_cb.p_vsc_cback = (void *)p_cback;
    714 
    715         nfc_hal_nci_send_cmd (p_buf);
    716 
    717         /* start NFC command-timeout timer */
    718         nfc_hal_main_start_quick_timer (&nfc_hal_cb.ncit_cb.nci_wait_rsp_timer, (UINT16)(NFC_HAL_TTYPE_NCI_WAIT_RSP),
    719                                         ((UINT32) NFC_HAL_CMD_TOUT) * QUICK_TIMER_TICKS_PER_SEC / 1000);
    720     }
    721 }
    722 
    723 /*******************************************************************************
    724 **
    725 ** Function         nfc_hal_dm_send_pend_cmd
    726 **
    727 ** Description      Send a command to NFCC
    728 **
    729 ** Returns          void
    730 **
    731 *******************************************************************************/
    732 void nfc_hal_dm_send_pend_cmd (void)
    733 {
    734     NFC_HDR *p_buf = nfc_hal_cb.ncit_cb.p_pend_cmd;
    735     UINT8  *p;
    736 
    737     if (p_buf == NULL)
    738         return;
    739 
    740     /* check low power mode state */
    741     if (!nfc_hal_dm_power_mode_execute (NFC_HAL_LP_TX_DATA_EVT))
    742     {
    743         return;
    744     }
    745 
    746     if (nfc_hal_cb.ncit_cb.nci_wait_rsp == NFC_HAL_WAIT_RSP_PROP)
    747     {
    748 #if (NFC_HAL_TRACE_PROTOCOL == TRUE)
    749         DispHciCmd (p_buf);
    750 #endif
    751 
    752         /* save the message header to double check the response */
    753         p = (UINT8 *)(p_buf + 1) + p_buf->offset;
    754         memcpy(nfc_hal_cb.ncit_cb.last_hdr, p, NFC_HAL_SAVED_HDR_SIZE);
    755 
    756         /* add packet type for BT message */
    757         p_buf->offset--;
    758         p_buf->len++;
    759 
    760         p  = (UINT8 *) (p_buf + 1) + p_buf->offset;
    761         *p = HCIT_TYPE_COMMAND;
    762 
    763         USERIAL_Write (USERIAL_NFC_PORT, p, p_buf->len);
    764 
    765         GKI_freebuf (p_buf);
    766         nfc_hal_cb.ncit_cb.p_pend_cmd = NULL;
    767 
    768         /* start NFC command-timeout timer */
    769         nfc_hal_main_start_quick_timer (&nfc_hal_cb.ncit_cb.nci_wait_rsp_timer, (UINT16)(NFC_HAL_TTYPE_NCI_WAIT_RSP),
    770                                         ((UINT32) NFC_HAL_CMD_TOUT) * QUICK_TIMER_TICKS_PER_SEC / 1000);
    771 
    772     }
    773 }
    774 
    775 /*******************************************************************************
    776 **
    777 ** Function         nfc_hal_dm_send_bt_cmd
    778 **
    779 ** Description      Send BT message to NFCC while initializing BRCM NFCC
    780 **
    781 ** Returns          void
    782 **
    783 *******************************************************************************/
    784 void nfc_hal_dm_send_bt_cmd (const UINT8 *p_data, UINT16 len, tNFC_HAL_BTVSC_CPLT_CBACK *p_cback)
    785 {
    786     NFC_HDR *p_buf;
    787 
    788     HAL_TRACE_DEBUG1 ("nfc_hal_dm_send_bt_cmd (): nci_wait_rsp = 0x%x", nfc_hal_cb.ncit_cb.nci_wait_rsp);
    789 
    790     if (nfc_hal_cb.ncit_cb.nci_wait_rsp != NFC_HAL_WAIT_RSP_NONE)
    791     {
    792         HAL_TRACE_ERROR0 ("nfc_hal_dm_send_bt_cmd(): no command window");
    793         return;
    794     }
    795 
    796     if ((p_buf = (NFC_HDR *) GKI_getpoolbuf (NFC_HAL_NCI_POOL_ID)) != NULL)
    797     {
    798         nfc_hal_cb.ncit_cb.nci_wait_rsp = NFC_HAL_WAIT_RSP_PROP;
    799 
    800         p_buf->offset = NFC_HAL_NCI_MSG_OFFSET_SIZE;
    801         p_buf->len    = len;
    802 
    803         memcpy ((UINT8*) (p_buf + 1) + p_buf->offset, p_data, len);
    804 
    805         /* save the callback for NCI VSCs)  */
    806         nfc_hal_cb.ncit_cb.p_vsc_cback = (void *)p_cback;
    807 
    808         nfc_hal_cb.ncit_cb.p_pend_cmd = p_buf;
    809         if (nfc_hal_cb.dev_cb.initializing_state == NFC_HAL_INIT_STATE_IDLE)
    810         {
    811             NFC_HAL_SET_INIT_STATE(NFC_HAL_INIT_STATE_W4_CONTROL_DONE);
    812             nfc_hal_cb.p_stack_cback (HAL_NFC_REQUEST_CONTROL_EVT, HAL_NFC_STATUS_OK);
    813             return;
    814         }
    815 
    816         nfc_hal_dm_send_pend_cmd();
    817     }
    818 }
    819 
    820 /*******************************************************************************
    821 **
    822 ** Function         nfc_hal_dm_set_nfc_wake
    823 **
    824 ** Description      Set NFC_WAKE line
    825 **
    826 ** Returns          void
    827 **
    828 *******************************************************************************/
    829 void nfc_hal_dm_set_nfc_wake (UINT8 cmd)
    830 {
    831     HAL_TRACE_DEBUG1 ("nfc_hal_dm_set_nfc_wake () %s",
    832                       (cmd == NFC_HAL_ASSERT_NFC_WAKE ? "ASSERT" : "DEASSERT"));
    833 
    834     /*
    835     **  nfc_wake_active_mode             cmd              result of voltage on NFC_WAKE
    836     **
    837     **  NFC_HAL_LP_ACTIVE_LOW (0)    NFC_HAL_ASSERT_NFC_WAKE (0)    pull down NFC_WAKE (GND)
    838     **  NFC_HAL_LP_ACTIVE_LOW (0)    NFC_HAL_DEASSERT_NFC_WAKE (1)  pull up NFC_WAKE (VCC)
    839     **  NFC_HAL_LP_ACTIVE_HIGH (1)   NFC_HAL_ASSERT_NFC_WAKE (0)    pull up NFC_WAKE (VCC)
    840     **  NFC_HAL_LP_ACTIVE_HIGH (1)   NFC_HAL_DEASSERT_NFC_WAKE (1)  pull down NFC_WAKE (GND)
    841     */
    842 
    843     if (cmd == nfc_hal_cb.dev_cb.nfc_wake_active_mode)
    844         UPIO_Set (UPIO_GENERAL, NFC_HAL_LP_NFC_WAKE_GPIO, UPIO_OFF); /* pull down NFC_WAKE */
    845     else
    846         UPIO_Set (UPIO_GENERAL, NFC_HAL_LP_NFC_WAKE_GPIO, UPIO_ON);  /* pull up NFC_WAKE */
    847 }
    848 
    849 /*******************************************************************************
    850 **
    851 ** Function         nfc_hal_dm_power_mode_execute
    852 **
    853 ** Description      If snooze mode is enabled in full power mode,
    854 **                     Assert NFC_WAKE before sending data
    855 **                     Deassert NFC_WAKE when idle timer expires
    856 **
    857 ** Returns          TRUE if DH can send data to NFCC
    858 **
    859 *******************************************************************************/
    860 BOOLEAN nfc_hal_dm_power_mode_execute (tNFC_HAL_LP_EVT event)
    861 {
    862     BOOLEAN send_to_nfcc = FALSE;
    863 
    864     HAL_TRACE_DEBUG1 ("nfc_hal_dm_power_mode_execute () event = %d", event);
    865 
    866     if (nfc_hal_cb.dev_cb.power_mode == NFC_HAL_POWER_MODE_FULL)
    867     {
    868         if (nfc_hal_cb.dev_cb.snooze_mode != NFC_HAL_LP_SNOOZE_MODE_NONE)
    869         {
    870             /* if any transport activity */
    871             if (  (event == NFC_HAL_LP_TX_DATA_EVT)
    872                 ||(event == NFC_HAL_LP_RX_DATA_EVT)  )
    873             {
    874                 /* if idle timer is not running */
    875                 if (nfc_hal_cb.dev_cb.lp_timer.in_use == FALSE)
    876                 {
    877                     nfc_hal_dm_set_nfc_wake (NFC_HAL_ASSERT_NFC_WAKE);
    878                 }
    879 
    880                 /* start or extend idle timer */
    881                 nfc_hal_main_start_quick_timer (&nfc_hal_cb.dev_cb.lp_timer, 0x00,
    882                                                 ((UINT32) NFC_HAL_LP_IDLE_TIMEOUT) * QUICK_TIMER_TICKS_PER_SEC / 1000);
    883             }
    884             else if (event == NFC_HAL_LP_TIMEOUT_EVT)
    885             {
    886                 /* let NFCC go to snooze mode */
    887                 nfc_hal_dm_set_nfc_wake (NFC_HAL_DEASSERT_NFC_WAKE);
    888             }
    889         }
    890 
    891         send_to_nfcc = TRUE;
    892     }
    893 
    894     return (send_to_nfcc);
    895 }
    896 
    897 /*******************************************************************************
    898 **
    899 ** Function         nci_brcm_lp_timeout_cback
    900 **
    901 ** Description      callback function for low power timeout
    902 **
    903 ** Returns          void
    904 **
    905 *******************************************************************************/
    906 static void nci_brcm_lp_timeout_cback (void *p_tle)
    907 {
    908     HAL_TRACE_DEBUG0 ("nci_brcm_lp_timeout_cback ()");
    909 
    910     nfc_hal_dm_power_mode_execute (NFC_HAL_LP_TIMEOUT_EVT);
    911 }
    912 
    913 /*******************************************************************************
    914 **
    915 ** Function         nfc_hal_dm_pre_init_nfcc
    916 **
    917 ** Description      This function initializes Broadcom specific control blocks for
    918 **                  NCI transport
    919 **
    920 ** Returns          void
    921 **
    922 *******************************************************************************/
    923 void nfc_hal_dm_pre_init_nfcc (void)
    924 {
    925     HAL_TRACE_DEBUG0 ("nfc_hal_dm_pre_init_nfcc ()");
    926 
    927     /* if it was waiting for core reset notification after raising REG_PU */
    928     if (nfc_hal_cb.dev_cb.initializing_state == NFC_HAL_INIT_STATE_W4_NFCC_ENABLE)
    929     {
    930         nfc_hal_dm_send_get_build_info_cmd ();
    931     }
    932     /* if it was waiting for core reset notification after setting Xtal */
    933     else if (nfc_hal_cb.dev_cb.initializing_state == NFC_HAL_INIT_STATE_POST_XTAL_SET)
    934     {
    935         {
    936             /* Core reset ntf after xtal setting indicating NFCC loaded patch from NVM */
    937             NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_W4_PATCH_INFO);
    938 
    939             nfc_hal_dm_send_nci_cmd (nfc_hal_dm_get_patch_version_cmd, NCI_MSG_HDR_SIZE, NULL);
    940         }
    941     }
    942 }
    943 
    944 /*******************************************************************************
    945 **
    946 ** Function         nfc_hal_dm_shutting_down_nfcc
    947 **
    948 ** Description      This function initializes Broadcom specific control blocks for
    949 **                  NCI transport
    950 **
    951 ** Returns          void
    952 **
    953 *******************************************************************************/
    954 void nfc_hal_dm_shutting_down_nfcc (void)
    955 {
    956     HAL_TRACE_DEBUG0 ("nfc_hal_dm_shutting_down_nfcc ()");
    957 
    958     nfc_hal_cb.dev_cb.initializing_state = NFC_HAL_INIT_STATE_CLOSING;
    959 
    960     /* reset low power mode variables */
    961     if (  (nfc_hal_cb.dev_cb.power_mode  == NFC_HAL_POWER_MODE_FULL)
    962         &&(nfc_hal_cb.dev_cb.snooze_mode != NFC_HAL_LP_SNOOZE_MODE_NONE)  )
    963     {
    964         nfc_hal_dm_set_nfc_wake (NFC_HAL_ASSERT_NFC_WAKE);
    965     }
    966 
    967     nfc_hal_cb.ncit_cb.nci_wait_rsp = NFC_HAL_WAIT_RSP_NONE;
    968     nfc_hal_cb.hci_cb.hcp_conn_id = 0;
    969 
    970     nfc_hal_cb.dev_cb.power_mode  = NFC_HAL_POWER_MODE_FULL;
    971     nfc_hal_cb.dev_cb.snooze_mode = NFC_HAL_LP_SNOOZE_MODE_NONE;
    972 
    973     /* Stop all timers */
    974     nfc_hal_main_stop_quick_timer (&nfc_hal_cb.ncit_cb.nci_wait_rsp_timer);
    975     nfc_hal_main_stop_quick_timer (&nfc_hal_cb.dev_cb.lp_timer);
    976     nfc_hal_main_stop_quick_timer (&nfc_hal_cb.prm.timer);
    977     nfc_hal_main_stop_quick_timer (&nfc_hal_cb.hci_cb.hci_timer);
    978     nfc_hal_main_stop_quick_timer (&nfc_hal_cb.timer);
    979 }
    980 
    981 /*******************************************************************************
    982 **
    983 ** Function         nfc_hal_dm_init
    984 **
    985 ** Description      This function initializes Broadcom specific control blocks for
    986 **                  NCI transport
    987 **
    988 ** Returns          void
    989 **
    990 *******************************************************************************/
    991 void nfc_hal_dm_init (void)
    992 {
    993     HAL_TRACE_DEBUG0 ("nfc_hal_dm_init ()");
    994 
    995     nfc_hal_cb.dev_cb.lp_timer.p_cback = nci_brcm_lp_timeout_cback;
    996 
    997     nfc_hal_cb.ncit_cb.nci_wait_rsp_timer.p_cback = nfc_hal_nci_cmd_timeout_cback;
    998 
    999     nfc_hal_cb.hci_cb.hci_timer.p_cback = nfc_hal_hci_timeout_cback;
   1000 
   1001     nfc_hal_cb.pre_discover_done        = FALSE;
   1002 
   1003     nfc_post_reset_cb.spd_nvm_detection_cur_count = 0;
   1004     nfc_post_reset_cb.spd_skip_on_power_cycle     = FALSE;
   1005 
   1006 }
   1007 
   1008 /*******************************************************************************
   1009 **
   1010 ** Function         HAL_NfcDevInitDone
   1011 **
   1012 ** Description      Notify that pre-initialization of NFCC is complete
   1013 **
   1014 ** Returns          void
   1015 **
   1016 *******************************************************************************/
   1017 void HAL_NfcPreInitDone (tHAL_NFC_STATUS status)
   1018 {
   1019     HAL_TRACE_DEBUG1 ("HAL_NfcPreInitDone () status=%d", status);
   1020 
   1021     if (nfc_hal_cb.dev_cb.initializing_state == NFC_HAL_INIT_STATE_W4_APP_COMPLETE)
   1022     {
   1023         NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_IDLE);
   1024 
   1025         nfc_hal_main_pre_init_done (status);
   1026     }
   1027 }
   1028 
   1029 /*******************************************************************************
   1030 **
   1031 ** Function         HAL_NfcReInit
   1032 **
   1033 ** Description      This function is called to restart initialization after REG_PU
   1034 **                  toggled because of failure to detect NVM type or download patchram.
   1035 **
   1036 ** Note             This function should be called only during the HAL init process
   1037 **
   1038 ** Returns          HAL_NFC_STATUS_OK if successfully initiated
   1039 **                  HAL_NFC_STATUS_FAILED otherwise
   1040 **
   1041 *******************************************************************************/
   1042 tHAL_NFC_STATUS HAL_NfcReInit (void)
   1043 {
   1044     tHAL_NFC_STATUS status = HAL_NFC_STATUS_FAILED;
   1045 
   1046     HAL_TRACE_DEBUG1 ("HAL_NfcReInit () init st=0x%x", nfc_hal_cb.dev_cb.initializing_state);
   1047     if (nfc_hal_cb.dev_cb.initializing_state == NFC_HAL_INIT_STATE_W4_APP_COMPLETE)
   1048     {
   1049         {
   1050             /* Wait for NFCC to enable - Core reset notification */
   1051             NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_W4_NFCC_ENABLE);
   1052 
   1053             /* NFCC Enable timeout */
   1054             nfc_hal_main_start_quick_timer (&nfc_hal_cb.timer, NFC_HAL_TTYPE_NFCC_ENABLE,
   1055                                             ((p_nfc_hal_cfg->nfc_hal_nfcc_enable_timeout)*QUICK_TIMER_TICKS_PER_SEC)/1000);
   1056         }
   1057 
   1058         status = HAL_NFC_STATUS_OK;
   1059     }
   1060     return status;
   1061 }
   1062 
   1063 /*******************************************************************************
   1064 **
   1065 ** Function         nfc_hal_dm_set_snooze_mode_cback
   1066 **
   1067 ** Description      This is baud rate update complete callback.
   1068 **
   1069 ** Returns          void
   1070 **
   1071 *******************************************************************************/
   1072 static void nfc_hal_dm_set_snooze_mode_cback (tNFC_HAL_BTVSC_CPLT *pData)
   1073 {
   1074     UINT8             status = pData->p_param_buf[0];
   1075     tHAL_NFC_STATUS   hal_status;
   1076     tHAL_NFC_STATUS_CBACK *p_cback;
   1077 
   1078     /* if it is completed */
   1079     if (status == HCI_SUCCESS)
   1080     {
   1081         /* update snooze mode */
   1082         nfc_hal_cb.dev_cb.snooze_mode = nfc_hal_cb.dev_cb.new_snooze_mode;
   1083 
   1084         nfc_hal_dm_set_nfc_wake (NFC_HAL_ASSERT_NFC_WAKE);
   1085 
   1086         if ( nfc_hal_cb.dev_cb.snooze_mode != NFC_HAL_LP_SNOOZE_MODE_NONE)
   1087         {
   1088             /* start idle timer */
   1089             nfc_hal_main_start_quick_timer (&nfc_hal_cb.dev_cb.lp_timer, 0x00,
   1090                                             ((UINT32) NFC_HAL_LP_IDLE_TIMEOUT) * QUICK_TIMER_TICKS_PER_SEC / 1000);
   1091         }
   1092         else
   1093         {
   1094             nfc_hal_main_stop_quick_timer (&nfc_hal_cb.dev_cb.lp_timer);
   1095         }
   1096         hal_status = HAL_NFC_STATUS_OK;
   1097     }
   1098     else
   1099     {
   1100         hal_status = HAL_NFC_STATUS_FAILED;
   1101     }
   1102 
   1103     if (nfc_hal_cb.dev_cb.p_prop_cback)
   1104     {
   1105         p_cback = nfc_hal_cb.dev_cb.p_prop_cback;
   1106         nfc_hal_cb.dev_cb.p_prop_cback = NULL;
   1107         (*p_cback) (hal_status);
   1108     }
   1109 }
   1110 
   1111 /*******************************************************************************
   1112 **
   1113 ** Function         HAL_NfcSetSnoozeMode
   1114 **
   1115 ** Description      Set snooze mode
   1116 **                  snooze_mode
   1117 **                      NFC_HAL_LP_SNOOZE_MODE_NONE - Snooze mode disabled
   1118 **                      NFC_HAL_LP_SNOOZE_MODE_UART - Snooze mode for UART
   1119 **                      NFC_HAL_LP_SNOOZE_MODE_SPI_I2C - Snooze mode for SPI/I2C
   1120 **
   1121 **                  idle_threshold_dh/idle_threshold_nfcc
   1122 **                      Idle Threshold Host in 100ms unit
   1123 **
   1124 **                  nfc_wake_active_mode/dh_wake_active_mode
   1125 **                      NFC_HAL_LP_ACTIVE_LOW - high to low voltage is asserting
   1126 **                      NFC_HAL_LP_ACTIVE_HIGH - low to high voltage is asserting
   1127 **
   1128 **                  p_snooze_cback
   1129 **                      Notify status of operation
   1130 **
   1131 ** Returns          tHAL_NFC_STATUS
   1132 **
   1133 *******************************************************************************/
   1134 tHAL_NFC_STATUS HAL_NfcSetSnoozeMode (UINT8 snooze_mode,
   1135                                       UINT8 idle_threshold_dh,
   1136                                       UINT8 idle_threshold_nfcc,
   1137                                       UINT8 nfc_wake_active_mode,
   1138                                       UINT8 dh_wake_active_mode,
   1139                                       tHAL_NFC_STATUS_CBACK *p_snooze_cback)
   1140 {
   1141     UINT8 cmd[NFC_HAL_BT_HCI_CMD_HDR_SIZE + HCI_BRCM_WRITE_SLEEP_MODE_LENGTH];
   1142     UINT8 *p;
   1143 
   1144     HAL_TRACE_API1 ("HAL_NfcSetSnoozeMode (): snooze_mode = %d", snooze_mode);
   1145 
   1146     nfc_hal_cb.dev_cb.new_snooze_mode      = snooze_mode;
   1147     nfc_hal_cb.dev_cb.nfc_wake_active_mode = nfc_wake_active_mode;
   1148     nfc_hal_cb.dev_cb.p_prop_cback         = p_snooze_cback;
   1149 
   1150     p = cmd;
   1151 
   1152     /* Add the HCI command */
   1153     UINT16_TO_STREAM (p, HCI_BRCM_WRITE_SLEEP_MODE);
   1154     UINT8_TO_STREAM  (p, HCI_BRCM_WRITE_SLEEP_MODE_LENGTH);
   1155 
   1156     memset (p, 0x00, HCI_BRCM_WRITE_SLEEP_MODE_LENGTH);
   1157 
   1158     UINT8_TO_STREAM  (p, snooze_mode);          /* Sleep Mode               */
   1159 
   1160     UINT8_TO_STREAM  (p, idle_threshold_dh);    /* Idle Threshold Host      */
   1161     UINT8_TO_STREAM  (p, idle_threshold_nfcc);  /* Idle Threshold HC        */
   1162     UINT8_TO_STREAM  (p, nfc_wake_active_mode); /* BT Wake Active Mode      */
   1163     UINT8_TO_STREAM  (p, dh_wake_active_mode);  /* Host Wake Active Mode    */
   1164 
   1165     nfc_hal_dm_send_bt_cmd (cmd,
   1166                             NFC_HAL_BT_HCI_CMD_HDR_SIZE + HCI_BRCM_WRITE_SLEEP_MODE_LENGTH,
   1167                             nfc_hal_dm_set_snooze_mode_cback);
   1168     return (NCI_STATUS_OK);
   1169 }
   1170 
   1171 
   1172 
   1173 
   1174 
   1175 
   1176 
   1177 
   1178