aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/SAMA/LLD/CRYPTOv1/sama_crypto_lld.c
blob: f06ef34ed92264f5d1785c40746a0f0c49f0d770 (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
/*
    ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio

    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.
*/
#include "hal.h"

#if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__)

#include "sama_crypto_lld.h"



#if defined(SAMA_DMA_REQUIRED)
static void crypto_lld_serve_read_interrupt(CRYDriver *cryp, uint32_t flags);
static void crypto_lld_serve_write_interrupt(CRYDriver *cryp, uint32_t flags);
#endif

/**
 * @brief   Wakes up the waiting thread.
 *
 * @param[in] cryp      pointer to the @p CRYDRiver object
 *
 * @notapi
 */
#define _cry_wakeup_isr(cryp) {                                             \
  osalSysLockFromISR();                                                     \
  osalThreadResumeI(&cryp->thread, MSG_OK);                               	\
  osalSysUnlockFromISR();                                                   \
}


/**
 * @brief   Common ISR code.
 * @details This code handles the portable part of the ISR code:
 *          - Callback invocation.
 *          - Waiting thread wakeup, if any.
 *          - Driver state transitions.
 *          .
 * @note    This macro is meant to be used in the low level drivers
 *          implementation only.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 *
 * @notapi
 */
#define _cry_isr_code(cryp) {                                               \
  _cry_wakeup_isr(cryp);                                                    \
}

void samaCryptoDriverInit(CRYDriver *cryp) {
	cryp->enabledPer = 0;
	cryp->thread = NULL;
	osalMutexObjectInit(&cryp->mutex);
}

void samaCryptoDriverStart(CRYDriver *cryp) {


	if (cryp->config->transfer_mode == TRANSFER_DMA)
	{
#if defined(SAMA_DMA_REQUIRED)
		cryp->dmarx = dmaChannelAllocate(SAMA_CRY_CRYD1_DMA_IRQ_PRIORITY,
				(sama_dmaisr_t) crypto_lld_serve_read_interrupt, (void *) cryp);
		osalDbgAssert(cryp->dmarx != NULL, "no channel allocated");

		cryp->dmatx = dmaChannelAllocate(SAMA_CRY_CRYD1_DMA_IRQ_PRIORITY,
				(sama_dmaisr_t) crypto_lld_serve_write_interrupt, (void *) cryp);
		osalDbgAssert(cryp->dmatx != NULL, "no channel allocated");
#endif
	}

}

void samaCryptoDriverStop(CRYDriver *cryp) {
#if defined(SAMA_DMA_REQUIRED)
	if (cryp->config->transfer_mode == TRANSFER_DMA)
	{
		dmaChannelRelease(cryp->dmarx);
		dmaChannelRelease(cryp->dmatx);
	}
#endif
	samaCryptoDriverDisable(cryp);
}

void samaCryptoDriverDisable(CRYDriver *cryp)
{
		if ((cryp->enabledPer & AES_PER)) {
			cryp->enabledPer &= ~AES_PER;
			pmcDisableAES();
		}
		if ((cryp->enabledPer & TDES_PER)) {
			cryp->enabledPer &= ~TDES_PER;
			pmcDisableDES();
		}
		if ((cryp->enabledPer & SHA_PER)) {
			cryp->enabledPer &= ~SHA_PER;
			pmcDisableSHA();
		}
		if ((cryp->enabledPer & TRNG_PER)) {
			cryp->enabledPer &= ~TRNG_PER;
			TRNG->TRNG_CR = TRNG_CR_KEY_PASSWD;
			pmcDisableTRNG();
		}
}

#if defined(SAMA_DMA_REQUIRED)
/**
 * @brief   Shared end-of-rx service routine.
 *
 * @param[in] cryp      pointer to the @p CRYDriver object
 * @param[in] flags     pre-shifted content of the ISR register
 */
static void crypto_lld_serve_read_interrupt(CRYDriver *cryp, uint32_t flags) {

  /* DMA errors handling.*/
  #if defined(SAMA_CRY_DMA_ERROR_HOOK)
  if ((flags & (XDMAC_CIS_RBEIS | XDMAC_CIS_ROIS)) != 0) {
	  SAMA_CRY_DMA_ERROR_HOOK(cryp);
  }
#else
  (void)flags;
#endif

  /* Stop everything.*/

  dmaChannelDisable(cryp->dmatx);
  dmaChannelDisable(cryp->dmarx);
  /* Portable CRY ISR code defined in the high level driver, note, it is
     a macro.*/
  _cry_isr_code(cryp);
}

/**
 * @brief   Shared end-of-tx service routine.
 *
 * @param[in] cryp      pointer to the @p CRYDriver object
 * @param[in] flags     pre-shifted content of the ISR register
 */
static void crypto_lld_serve_write_interrupt(CRYDriver *cryp, uint32_t flags) {

  /* DMA errors handling.*/

#if defined(SAMA_CRY_DMA_ERROR_HOOK)
  (void)cryp;
  if ((flags & (XDMAC_CIS_WBEIS | XDMAC_CIS_ROIS)) != 0) {
	  SAMA_CRY_DMA_ERROR_HOOK(cryp);
  }
#else
  (void)cryp;
  (void)flags;
#endif


}

#endif

#endif /* HAL_USE_CRY */

/** @} */