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