1 /*
2  * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _FSL_SMARTCARD_H_
10 #define _FSL_SMARTCARD_H_
11 
12 #include "fsl_common.h"
13 
14 /*!
15  * @addtogroup smartcard
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @name Driver version */
24 /*@{*/
25 /*! @brief Smart card driver version 2.3.0.
26  */
27 #define FSL_SMARTCARD_DRIVER_VERSION (MAKE_VERSION(2, 3, 0))
28 /*@}*/
29 
30 /*! @brief Smart card global define which specify number of clock cycles until initial 'TS' character has to be received
31  */
32 #define SMARTCARD_INIT_DELAY_CLOCK_CYCLES (42000u)
33 
34 /*! @brief Smart card global define which specify number of clock cycles during which ATR string has to be received */
35 #define SMARTCARD_EMV_ATR_DURATION_ETU (20150u)
36 
37 /*! @brief Smart card specification initial TS character definition of direct convention */
38 #define SMARTCARD_TS_DIRECT_CONVENTION (0x3Bu)
39 
40 /*! @brief Smart card specification initial TS character definition of inverse convention */
41 #define SMARTCARD_TS_INVERSE_CONVENTION (0x3Fu)
42 
43 /*! @brief Smart card Error codes. */
44 enum
45 {
46     kStatus_SMARTCARD_Success              = MAKE_STATUS(kStatusGroup_SMARTCARD, 0), /*!< Transfer ends successfully */
47     kStatus_SMARTCARD_TxBusy               = MAKE_STATUS(kStatusGroup_SMARTCARD, 1), /*!< Transmit in progress */
48     kStatus_SMARTCARD_RxBusy               = MAKE_STATUS(kStatusGroup_SMARTCARD, 2), /*!< Receiving in progress */
49     kStatus_SMARTCARD_NoTransferInProgress = MAKE_STATUS(kStatusGroup_SMARTCARD, 3), /*!< No transfer in progress */
50     kStatus_SMARTCARD_Timeout              = MAKE_STATUS(kStatusGroup_SMARTCARD, 4), /*!< Transfer ends with time-out */
51     kStatus_SMARTCARD_Initialized =
52         MAKE_STATUS(kStatusGroup_SMARTCARD, 5), /*!< Smart card driver is already initialized */
53     kStatus_SMARTCARD_PhyInitialized =
54         MAKE_STATUS(kStatusGroup_SMARTCARD, 6), /*!< Smart card PHY drive is already  initialized */
55     kStatus_SMARTCARD_CardNotActivated = MAKE_STATUS(kStatusGroup_SMARTCARD, 7), /*!< Smart card is not activated */
56     kStatus_SMARTCARD_InvalidInput =
57         MAKE_STATUS(kStatusGroup_SMARTCARD, 8), /*!< Function called with invalid input arguments */
58     kStatus_SMARTCARD_OtherError = MAKE_STATUS(kStatusGroup_SMARTCARD, 9) /*!< Some other error occur */
59 };
60 
61 /*! @brief Control codes for the Smart card protocol timers and misc. */
62 typedef enum _smartcard_control
63 {
64     kSMARTCARD_EnableADT               = 0x0u,
65     kSMARTCARD_DisableADT              = 0x1u,
66     kSMARTCARD_EnableGTV               = 0x2u,
67     kSMARTCARD_DisableGTV              = 0x3u,
68     kSMARTCARD_ResetWWT                = 0x4u,
69     kSMARTCARD_EnableWWT               = 0x5u,
70     kSMARTCARD_DisableWWT              = 0x6u,
71     kSMARTCARD_ResetCWT                = 0x7u,
72     kSMARTCARD_EnableCWT               = 0x8u,
73     kSMARTCARD_DisableCWT              = 0x9u,
74     kSMARTCARD_ResetBWT                = 0xAu,
75     kSMARTCARD_EnableBWT               = 0xBu,
76     kSMARTCARD_DisableBWT              = 0xCu,
77     kSMARTCARD_EnableInitDetect        = 0xDu,
78     kSMARTCARD_EnableAnack             = 0xEu,
79     kSMARTCARD_DisableAnack            = 0xFu,
80     kSMARTCARD_ConfigureBaudrate       = 0x10u,
81     kSMARTCARD_SetupATRMode            = 0x11u,
82     kSMARTCARD_SetupT0Mode             = 0x12u,
83     kSMARTCARD_SetupT1Mode             = 0x13u,
84     kSMARTCARD_EnableReceiverMode      = 0x14u,
85     kSMARTCARD_DisableReceiverMode     = 0x15u,
86     kSMARTCARD_EnableTransmitterMode   = 0x16u,
87     kSMARTCARD_DisableTransmitterMode  = 0x17u,
88     kSMARTCARD_ResetWaitTimeMultiplier = 0x18u,
89 } smartcard_control_t;
90 
91 /*! @brief Defines Smart card interface voltage class values */
92 typedef enum _smartcard_card_voltage_class
93 {
94     kSMARTCARD_VoltageClassUnknown = 0x0u,
95     kSMARTCARD_VoltageClassA5_0V   = 0x1u,
96     kSMARTCARD_VoltageClassB3_3V   = 0x2u,
97     kSMARTCARD_VoltageClassC1_8V   = 0x3u
98 } smartcard_card_voltage_class_t;
99 
100 /*! @brief Defines Smart card I/O transfer states */
101 typedef enum _smartcard_transfer_state
102 {
103     kSMARTCARD_IdleState               = 0x0u,
104     kSMARTCARD_WaitingForTSState       = 0x1u,
105     kSMARTCARD_InvalidTSDetecetedState = 0x2u,
106     kSMARTCARD_ReceivingState          = 0x3u,
107     kSMARTCARD_TransmittingState       = 0x4u,
108 } smartcard_transfer_state_t;
109 
110 /*! @brief Defines Smart card reset types */
111 typedef enum _smartcard_reset_type
112 {
113     kSMARTCARD_ColdReset   = 0x0u,
114     kSMARTCARD_WarmReset   = 0x1u,
115     kSMARTCARD_NoColdReset = 0x2u,
116     kSMARTCARD_NoWarmReset = 0x3u,
117 } smartcard_reset_type_t;
118 
119 /*! @brief Defines Smart card transport protocol types */
120 typedef enum _smartcard_transport_type
121 {
122     kSMARTCARD_T0Transport = 0x0u,
123     kSMARTCARD_T1Transport = 0x1u
124 } smartcard_transport_type_t;
125 
126 /*! @brief Defines Smart card data parity types */
127 typedef enum _smartcard_parity_type
128 {
129     kSMARTCARD_EvenParity = 0x0u,
130     kSMARTCARD_OddParity  = 0x1u
131 } smartcard_parity_type_t;
132 
133 /*! @brief Defines data Convention format */
134 typedef enum _smartcard_card_convention
135 {
136     kSMARTCARD_DirectConvention  = 0x0u,
137     kSMARTCARD_InverseConvention = 0x1u
138 } smartcard_card_convention_t;
139 
140 /*! @brief Defines Smart card interface IC control types */
141 typedef enum _smartcard_interface_control
142 {
143     kSMARTCARD_InterfaceSetVcc               = 0x00u,
144     kSMARTCARD_InterfaceSetClockToResetDelay = 0x01u,
145     kSMARTCARD_InterfaceReadStatus           = 0x02u
146 } smartcard_interface_control_t;
147 
148 /*! @brief Defines transfer direction.*/
149 typedef enum _smartcard_direction
150 {
151     kSMARTCARD_Receive  = 0u,
152     kSMARTCARD_Transmit = 1u
153 } smartcard_direction_t;
154 
155 /*! @brief Smart card interface interrupt callback function type */
156 typedef void (*smartcard_interface_callback_t)(void *smartcardContext, void *param);
157 /*! @brief Smart card transfer interrupt callback function type */
158 typedef void (*smartcard_transfer_callback_t)(void *smartcardContext, void *param);
159 
160 /*! @brief Time Delay function used to passive waiting using RTOS [us] */
161 typedef void (*smartcard_time_delay_t)(uint32_t us);
162 
163 /*! @brief Defines card-specific parameters for Smart card driver */
164 typedef struct _smartcard_card_params
165 {
166     /* ISO7816/EMV4.3 specification variables */
167     uint16_t Fi;            /*!< 4 bits Fi - clock rate conversion integer */
168     uint8_t fMax;           /*!< Maximum Smart card frequency in MHz */
169     uint8_t WI;             /*!< 8 bits WI - work wait time integer */
170     uint8_t Di;             /*!< 4 bits DI - baud rate divisor */
171     uint8_t BWI;            /*!< 4 bits BWI - block wait time integer */
172     uint8_t CWI;            /*!< 4 bits CWI - character wait time integer */
173     uint8_t BGI;            /*!< 4 bits BGI - block guard time integer */
174     uint8_t GTN;            /*!< 8 bits GTN - extended guard time integer */
175     uint8_t IFSC;           /*!< Indicates IFSC value of the card */
176     uint8_t modeNegotiable; /*!< Indicates if the card acts in negotiable or a specific mode. */
177     uint8_t currentD;       /*!< 4 bits DI - current baud rate divisor*/
178     /* Driver-specific variables */
179     uint8_t status;                         /*!< Indicates smart card status */
180     bool t0Indicated;                       /*!< Indicates ff T=0 indicated in TD1 byte */
181     bool t1Indicated;                       /*!< Indicates if T=1 indicated in TD2 byte */
182     bool atrComplete;                       /*!< Indicates whether the ATR received from the card was complete or not */
183     bool atrValid;                          /*!< Indicates whether the ATR received from the card was valid or not */
184     bool present;                           /*!< Indicates if a smart card is present */
185     bool active;                            /*!< Indicates if the smart card is activated */
186     bool faulty;                            /*!< Indicates whether smart card/interface is faulty */
187     smartcard_card_convention_t convention; /*!< Card convention, kSMARTCARD_DirectConvention for direct convention,
188                                                  kSMARTCARD_InverseConvention for inverse convention */
189 } smartcard_card_params_t;
190 
191 /*! @brief Smart card defines the state of the EMV timers in the Smart card driver */
192 typedef struct _smartcard_timers_state
193 {
194     volatile bool adtExpired;           /*!< Indicates whether ADT timer expired */
195     volatile bool wwtExpired;           /*!< Indicates whether WWT timer expired */
196     volatile bool cwtExpired;           /*!< Indicates whether CWT timer expired */
197     volatile bool bwtExpired;           /*!< Indicates whether BWT timer expired */
198     volatile bool initCharTimerExpired; /*!< Indicates whether reception timer
199                 for initialization character (TS) after the RST has expired */
200 } smartcard_timers_state_t;
201 
202 /*! @brief Defines user specified configuration of Smart card interface */
203 typedef struct _smartcard_interface_config
204 {
205     uint32_t smartCardClock;            /*!< Smart card interface clock [Hz] */
206     uint32_t clockToResetDelay;         /*!< Indicates clock to RST apply delay [smart card clock cycles] */
207     uint8_t clockModule;                /*!< Smart card clock module number */
208     uint8_t clockModuleChannel;         /*!< Smart card clock module channel number */
209     uint8_t clockModuleSourceClock;     /*!< Smart card clock module source clock [e.g., BusClk] */
210     smartcard_card_voltage_class_t vcc; /*!< Smart card voltage class */
211     uint8_t controlPort;                /*!< Smart card PHY control port instance */
212     uint8_t controlPin;                 /*!< Smart card PHY control pin instance */
213     uint8_t irqPort;                    /*!< Smart card PHY Interrupt port instance */
214     uint8_t irqPin;                     /*!< Smart card PHY Interrupt pin instance */
215     uint8_t resetPort;                  /*!< Smart card reset port instance */
216     uint8_t resetPin;                   /*!< Smart card reset pin instance */
217     uint8_t vsel0Port;                  /*!< Smart card PHY Vsel0 control port instance */
218     uint8_t vsel0Pin;                   /*!< Smart card PHY Vsel0 control pin instance */
219     uint8_t vsel1Port;                  /*!< Smart card PHY Vsel1 control port instance */
220     uint8_t vsel1Pin;                   /*!< Smart card PHY Vsel1 control pin instance */
221     uint8_t dataPort;                   /*!< Smart card PHY data port instance */
222     uint8_t dataPin;                    /*!< Smart card PHY data pin instance */
223     uint8_t dataPinMux;                 /*!< Smart card PHY data pin mux option */
224     uint8_t tsTimerId; /*!< Numerical identifier of the External HW timer for Initial character detection */
225 } smartcard_interface_config_t;
226 
227 /*! @brief Defines user transfer structure used to initialize transfer */
228 typedef struct _smartcard_xfer
229 {
230     smartcard_direction_t direction; /*!< Direction of communication. (RX/TX) */
231     uint8_t *buff;                   /*!< The buffer of data. */
232     size_t size;                     /*!< The number of transferred units. */
233 } smartcard_xfer_t;
234 
235 /*!
236  * @brief Runtime state of the Smart card driver.
237  */
238 typedef struct _smartcard_context
239 {
240     /* Xfer part */
241     void *base;                      /*!< Smart card module base address */
242     smartcard_direction_t direction; /*!< Direction of communication. (RX/TX) */
243     uint8_t *xBuff;                  /*!< The buffer of data being transferred.*/
244     volatile size_t xSize;           /*!< The number of bytes to be transferred. */
245     volatile bool xIsBusy;           /*!< True if there is an active transfer. */
246     uint8_t txFifoEntryCount;        /*!< Number of data word entries in transmit FIFO. */
247     uint8_t rxFifoThreshold;         /*!< The max value of the receiver FIFO threshold. */
248     /* Smart card Interface part */
249     smartcard_interface_callback_t interfaceCallback; /*!< Callback to invoke after interface IC raised interrupt.*/
250     smartcard_transfer_callback_t transferCallback;   /*!< Callback to invoke after transfer event occur.*/
251     void *interfaceCallbackParam;                     /*!< Interface callback parameter pointer.*/
252     void *transferCallbackParam;                      /*!< Transfer callback parameter pointer.*/
253     smartcard_time_delay_t timeDelay;                 /*!< Function which handles time delay defined by user or RTOS. */
254     smartcard_reset_type_t resetType; /*!< Indicates whether a Cold reset or Warm reset was requested. */
255     smartcard_transport_type_t tType; /*!< Indicates current transfer protocol (T0 or T1) */
256     /* Smart card State part */
257     volatile smartcard_transfer_state_t transferState; /*!< Indicates the current transfer state */
258     smartcard_timers_state_t timersState; /*!< Indicates the state of different protocol timers used in driver */
259     smartcard_card_params_t
260         cardParams; /*!< Smart card parameters(ATR and current) and interface slots states(ATR and current) */
261     uint8_t IFSD;   /*!< Indicates the terminal IFSD */
262     smartcard_parity_type_t parity; /*!< Indicates current parity even/odd */
263     volatile bool rxtCrossed;       /*!< Indicates whether RXT thresholds has been crossed */
264     volatile bool txtCrossed;       /*!< Indicates whether TXT thresholds has been crossed */
265     volatile bool wtxRequested;     /*!< Indicates whether WTX has been requested or not*/
266     volatile bool parityError;      /*!< Indicates whether a parity error has been detected */
267     uint8_t statusBytes[2]; /*!< Used to store Status bytes SW1, SW2 of the last executed card command response */
268     /* Configuration part */
269     smartcard_interface_config_t interfaceConfig; /*!< Smart card interface configuration structure */
270     bool abortTransfer;                           /*!< Used to abort transfer. */
271 } smartcard_context_t;
272 
273 /*! @}*/
274 #endif /* _FSL_SMARTCARD_H_*/
275