summaryrefslogtreecommitdiff
path: root/snxxx/libese-spi/p73/pal/spi/EseSpiTransport.cpp
blob: fa2965673018c37fee7a6c48e0de26144dd215a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/******************************************************************************
 *
 *  Copyright 2018-2020, 2023 NXP
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

/*
 * DAL spi port implementation for linux
 *
 * Project: Trusted ESE Linux
 *
 */

#include "EseSpiTransport.h"

#define LOG_TAG "NxpEseHal"
#include <errno.h>
#include <ese_config.h>
#include <ese_logs.h>
#include <fcntl.h>
#include <hardware/nfc.h>
#include <log/log.h>
#include <phEseStatus.h>
#include <phNxpEsePal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include "NfcAdaptation.h"
#include "hal_nxpese.h"
#include "phNxpEse_Api.h"

#define MAX_RETRY_CNT 10
#define HAL_NFC_SPI_DWP_SYNC 21
#define USE_COLD_RESET 0x00

extern int omapi_status;

static int rf_status;
#if (NFC_NXP_ESE_VER == JCOP_VER_5_x)
eseIoctlData_t eseioctldata;
#endif
// Default max retry count for SPI CLT write blocked in secs
static unsigned long int gsMaxSpiWriteRetryCnt = 10;
#if (NFC_NXP_ESE_VER == JCOP_VER_4_0)
static ESESTATUS phNxpEse_spiIoctl_legacy(uint64_t ioctlType, void* p_data);
#endif

/*******************************************************************************
**
** Function         phPalEse_spi_close
**
** Description      Closes PN547 device
**
** Parameters       pDevHandle - device handle
**
** Returns          None
**
*******************************************************************************/
void EseSpiTransport::Close(void* pDevHandle) {
  if (NULL != pDevHandle) {
    close((intptr_t)pDevHandle);
  }
  return;
}

#ifdef NXP_BOOTTIME_UPDATE
/*******************************************************************************
**
** Function         phNxpEse_spiIoctl
**
** Description      Perform cross HAL IOCTL functionality
**
** Parameters       ioctlType, input data
**
** Returns          SUCCESS/FAIL
**
*******************************************************************************/
ESESTATUS phNxpEse_spiIoctl(uint64_t ioctlType, void* p_data) {
  ESESTATUS status = ESESTATUS_SUCCESS;
  if (!p_data) {
    NXP_LOG_ESE_E("halimpl phNxpEse_spiIoctl p_data is null ioctltyp: %ld",
                  (long)ioctlType);
    return ESESTATUS_FAILED;
  }
#if (NFC_NXP_ESE_VER == JCOP_VER_5_x)
  ese_nxp_IoctlInOutData_t* inpOutData = (ese_nxp_IoctlInOutData_t*)p_data;
  switch (ioctlType) {
    case HAL_ESE_IOCTL_RF_STATUS_UPDATE:
      rf_status = inpOutData->inp.data.nxpCmd.p_cmd[0];
      if (rf_status == 1) {
        NXP_LOG_ESE_D(
            "******************RF IS ON*************************************");
      } else {
        NXP_LOG_ESE_D(
            "******************RF IS OFF*************************************");
      }
      break;
    default:
      NXP_LOG_ESE_D("Invalid IOCTL type");
      break;
  }
#endif
#if (NFC_NXP_ESE_VER == JCOP_VER_4_0)
  status = phNxpEse_spiIoctl_legacy(ioctlType, p_data);
#endif
  return status;
}
#endif

#if (NFC_NXP_ESE_VER == JCOP_VER_4_0)
/*******************************************************************************
**
** Function         phNxpEse_spiIoctl_legacy
**
** Description      Perform cross HAL IOCTL functionality
**
** Parameters       ioctlType, input data
**
** Returns          SUCCESS/FAIL
**
*******************************************************************************/
static ESESTATUS phNxpEse_spiIoctl_legacy(uint64_t ioctlType, void* p_data) {
  ese_nxp_IoctlInOutData_t* inpOutData = (ese_nxp_IoctlInOutData_t*)p_data;
  switch (ioctlType) {
    case HAL_ESE_IOCTL_RF_STATUS_UPDATE:

      rf_status = inpOutData->inp.data.nxpCmd.p_cmd[0];
      if (rf_status == 1) {
        NXP_LOG_ESE_D(
            "******************RF IS ON*************************************");
      } else {
        NXP_LOG_ESE_D(
            "******************RF IS OFF*************************************");
      }
      break;
    default:
      NXP_LOG_ESE_D("Invalid IOCTL type");
      break;
  }
  return ESESTATUS_SUCCESS;
}
#endif

/*******************************************************************************
**
** Function         OpenAndConfigure
**
** Description      Open and configure pn547 device
**
** Parameters       pConfig     - hardware information
**                  pLinkHandle - device handle
**
** Returns          ESE status:
**                  ESESTATUS_SUCCESS            - open_and_configure operation
*success
**                  ESESTATUS_INVALID_DEVICE     - device open operation failure
**
*******************************************************************************/
ESESTATUS EseSpiTransport::OpenAndConfigure(pphPalEse_Config_t pConfig) {
  int nHandle;
  int retryCnt = 0;
  ALOGD("NxpEse EseSpiTransport::OpenAndConfigure 1");
  if (EseConfig::hasKey(NAME_NXP_SOF_WRITE)) {
    mConfigSofWrite = EseConfig::getUnsigned(NAME_NXP_SOF_WRITE);
    NXP_LOG_ESE_D("NXP_SOF_WRITE value from config file = %ld",
                  mConfigSofWrite);
  }
  if (EseConfig::hasKey(NAME_NXP_SPI_WRITE_TIMEOUT)) {
    mConfigSpiWriteTimeout = EseConfig::getUnsigned(NAME_NXP_SPI_WRITE_TIMEOUT);
    NXP_LOG_ESE_D("NXP_SPI_WRITE_TIMEOUT value from config file = %ld",
                  mConfigSpiWriteTimeout);
  }
  /* Read eSE cold reset interface from ese config file */
  if (EseConfig::hasKey(NAME_NXP_P61_COLD_RESET_INTERFACE)) {
    mConfigColdResetIntf =
        EseConfig::getUnsigned(NAME_NXP_P61_COLD_RESET_INTERFACE);
    NXP_LOG_ESE_D("mConfigColdResetIntf value from config file = %ld",
                  mConfigColdResetIntf);
  } else {
    mConfigColdResetIntf = 0x01; /* Default interface is NFC HAL */
    NXP_LOG_ESE_D("mConfigColdResetIntf: Default value ");
  }
  /* Read eSE GPIO reset config */
  if (EseConfig::hasKey(NAME_NXP_ESE_GPIO_RESET)) {
    mConfigGpioReset = EseConfig::getUnsigned(NAME_NXP_ESE_GPIO_RESET);
    NXP_LOG_ESE_D("mConfigGpioReset value from config file = %ld",
                  mConfigGpioReset);
  } else {
    mConfigGpioReset = USE_COLD_RESET;
    NXP_LOG_ESE_D("mConfigGpioReset: Default value ");
  }
  NXP_LOG_ESE_D("Opening port=%s\n", pConfig->pDevName);
/* open port */
retry:
  nHandle = open((char const*)pConfig->pDevName, O_RDWR);
  if (nHandle < 0) {
    NXP_LOG_ESE_E("%s : failed errno = 0x%x, retval %x", __FUNCTION__, errno,
                  nHandle);

    if ((errno == -EBUSY) || (errno == EBUSY)) {
      if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
        phPalEse_sleep(100 * 1000);  // 100ms delay
        return ESESTATUS_DRIVER_BUSY;
      } else {
        retryCnt++;
        NXP_LOG_ESE_E("Retry open eSE driver, retry cnt : %d", retryCnt);
        if (retryCnt < MAX_RETRY_CNT) {
          phPalEse_sleep(1000000);
          goto retry;
        }
      }
    }
    NXP_LOG_ESE_E("_spi_open() Failed: retval %x", nHandle);
    pConfig->pDevHandle = NULL;
    return ESESTATUS_INVALID_DEVICE;
  }
  NXP_LOG_ESE_D("eSE driver opened :: fd = [%d]", nHandle);
  pConfig->pDevHandle = (void*)((intptr_t)nHandle);
  return ESESTATUS_SUCCESS;
}

/*******************************************************************************
**
** Function         Read
**
** Description      Reads requested number of bytes from pn547 device into given
*buffer
**
** Parameters       pDevHandle       - valid device handle
**                  pBuffer          - buffer for read data
**                  nNbBytesToRead   - number of bytes requested to be read
**
** Returns          numRead   - number of successfully read bytes
**                  -1        - read operation failure
**
*******************************************************************************/
int EseSpiTransport::Read(void* pDevHandle, uint8_t* pBuffer,
                          int nNbBytesToRead) {
  int ret = -1;
  ret = read((intptr_t)pDevHandle, (void*)pBuffer, (nNbBytesToRead));
  return ret;
}

/*******************************************************************************
**
** Function         Write
**
** Description      Writes requested number of bytes from given buffer into
*pn547 device
**
** Parameters       pDevHandle       - valid device handle
**                  pBuffer          - buffer for read data
**                  nNbBytesToWrite  - number of bytes requested to be written
**
** Returns          numWrote   - number of successfully written bytes
**                  -1         - write operation failure
**
*******************************************************************************/
int EseSpiTransport::Write(void* pDevHandle, uint8_t* pBuffer,
                           int nNbBytesToWrite) {
  int ret = -1;
  int numWrote = 0;
  unsigned long int retryCount = 0;
  if (NULL == pDevHandle) {
    NXP_LOG_ESE_E("phPalEse_spi_write: received pDevHandle=NULL");
    return -1;
  }
  if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
    if (mConfigSofWrite == 1) {
      /* Appending SOF for SPI write */
      pBuffer[0] = SEND_PACKET_SOF;
    } else {
      /* Do Nothing */
    }
  }
  NXP_LOG_ESE_D("NXP_SPI_WRITE_TIMEOUT value is... : %ld secs",
                mConfigSpiWriteTimeout);
  if (mConfigSpiWriteTimeout > 0) {
    gsMaxSpiWriteRetryCnt = mConfigSpiWriteTimeout;
  } else {
    /* Do Nothing */
  }

  while (numWrote < nNbBytesToWrite) {
    // usleep(5000);
    if (rf_status == 0) {
      ret = write((intptr_t)pDevHandle, pBuffer + numWrote,
                  nNbBytesToWrite - numWrote);
    } else {
      ret = -1;
    }
    if (ret > 0) {
      numWrote += ret;
    } else if (ret == 0) {
      NXP_LOG_ESE_E("_spi_write() EOF");
      return -1;
    } else {
      NXP_LOG_ESE_E("_spi_write() errno : %x", errno);
      NXP_LOG_ESE_D("rf_status value is %d", rf_status);
      if ((errno == EINTR || errno == EAGAIN || rf_status == 1) &&
          (retryCount < gsMaxSpiWriteRetryCnt)) {
        /*Configure retry count or timeout here,now its configured for 2*10
         * secs*/
        if (retryCount > gsMaxSpiWriteRetryCnt) {
          ret = -1;
          break;
        }

        retryCount++;
        /* 5ms delay to give ESE wake up delay */
        phPalEse_sleep(1000 * (GET_WAKE_UP_DELAY()));
        NXP_LOG_ESE_E("_spi_write() failed. Going to retry, counter:%ld !",
                      retryCount);
        continue;
      }
      return -1;
    }
  }
  return numWrote;
}

/*******************************************************************************
**
** Function         Ioctl
**
** Description      Exposed ioctl by p61 spi driver
**
** Parameters       pDevHandle     - valid device handle
**                  level          - reset level
**
** Returns           0   - ioctl operation success
**                  -1   - ioctl operation failure
**
*******************************************************************************/
ESESTATUS EseSpiTransport::Ioctl(phPalEse_ControlCode_t eControlCode,
                                 void* pDevHandle, long level) {
  ESESTATUS ret = ESESTATUS_IOCTL_FAILED;
  int retioctl = 0x00;
#if (NFC_NXP_ESE_VER == JCOP_VER_5_x)
  ese_nxp_IoctlInOutData_t inpOutData;
  inpOutData.inp.level = level;
  NfcAdaptation& pNfcAdapt = NfcAdaptation::GetInstance();
#endif
  NXP_LOG_ESE_D("phPalEse_spi_ioctl(), ioctl %x , level %lx", eControlCode,
                level);
  if (NULL == pDevHandle) {
    if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
      return ESESTATUS_IOCTL_FAILED;
    }
  }
  switch (eControlCode) {
    case phPalEse_e_ResetDevice:
      if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
        ret = ESESTATUS_SUCCESS;
      } else {
        ret = (ESESTATUS)ioctl((intptr_t)pDevHandle, P61_SET_PWR, level);
      }
      break;

    case phPalEse_e_EnableLog:
      if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
        ret = ESESTATUS_SUCCESS;
      } else {
        ret = (ESESTATUS)ioctl((intptr_t)pDevHandle, P61_SET_DBG, level);
      }
      break;

    case phPalEse_e_EnablePollMode:
      if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
        ret = ESESTATUS_SUCCESS;
      } else {
        ret = (ESESTATUS)ioctl((intptr_t)pDevHandle, P61_SET_POLL, level);
      }
      break;
    case phPalEse_e_SetSecureMode:
      ret =
          (ESESTATUS)ioctl((intptr_t)pDevHandle, ESE_SET_TRUSTED_ACCESS, level);
      if (0x00 <= ret) {
        ret = ESESTATUS_SUCCESS;
      }
      break;
    case phPalEse_e_ChipRst:
      if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
        if (level == 5) {              // SPI driver communication part
          if (!mConfigColdResetIntf) { // Call the driver IOCTL
            unsigned int cmd = ESE_PERFORM_COLD_RESET;
            if ((mConfigGpioReset == 0x01) &&
                ((GET_CHIP_OS_VERSION() == OS_VERSION_8_9))) {
              cmd = P61_SET_PWR;
            }
            retioctl = ioctl((intptr_t)pDevHandle, cmd, level);
            if (0x00 <= retioctl) {
              ret = ESESTATUS_SUCCESS;
            }
          } else {
            if ((NFC_NXP_ESE_VER == JCOP_VER_5_x) &&
                (GET_CHIP_OS_VERSION() != OS_VERSION_8_9)) {
              // Nfc Driver communication part
              pNfcAdapt.Initialize();
              ret = pNfcAdapt.resetEse(level);
            } else {
              NXP_LOG_ESE_E("%s: Not supported", __func__);
              ret = ESESTATUS_SUCCESS;
            }
          }
        } else {
          ret = ESESTATUS_SUCCESS;
        }
      } else {
        ret = (ESESTATUS)ioctl((intptr_t)pDevHandle, P61_SET_SPM_PWR, level);
      }
      break;
    case phPalEse_e_ResetProtection:
      if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
        retioctl = ioctl((intptr_t)pDevHandle, PERFORM_RESET_PROTECTION, level);
        if (0x00 <= retioctl) {
          ret = ESESTATUS_SUCCESS;
        } else {
          NXP_LOG_ESE_E("phPalEse_e_ResetProtection ioctl failed status :%x !",
                        retioctl);
        }
      }
      break;
    case phPalEse_e_EnableThroughputMeasurement:
      if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
        ret = ESESTATUS_SUCCESS;
      } else {
        ret = (ESESTATUS)ioctl((intptr_t)pDevHandle, P61_SET_THROUGHPUT, level);
      }
      break;

    case phPalEse_e_SetPowerScheme:
      if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
        ret = ESESTATUS_SUCCESS;
      } else {
        ret =
            (ESESTATUS)ioctl((intptr_t)pDevHandle, P61_SET_POWER_SCHEME, level);
      }
      break;

    case phPalEse_e_GetSPMStatus:
      if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
        ret = ESESTATUS_SUCCESS;
      } else {
        ret = (ESESTATUS)ioctl((intptr_t)pDevHandle, P61_GET_SPM_STATUS, level);
      }
      break;

    case phPalEse_e_GetEseAccess:
      if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
        ret = ESESTATUS_SUCCESS;
      } else {
        ret = (ESESTATUS)ioctl((intptr_t)pDevHandle, P61_GET_ESE_ACCESS, level);
      }
      break;
    case phPalEse_e_SetJcopDwnldState:
      if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
        ret = ESESTATUS_SUCCESS;
      } else {
        ret =
            (ESESTATUS)ioctl((intptr_t)pDevHandle, P61_SET_DWNLD_STATUS, level);
      }
      break;
    case phPalEse_e_DisablePwrCntrl:
      ret = ESESTATUS_SUCCESS;
      break;
    default:
      ret = ESESTATUS_IOCTL_FAILED;
      break;
  }
  NXP_LOG_ESE_D("Exit  phPalEse_spi_ioctl : ret = %d errno = %d", ret, errno);
  return (ESESTATUS)ret;
}