Contiki-NG
Loading...
Searching...
No Matches
pka.c
Go to the documentation of this file.
1/*
2 * Original file:
3 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
4 * All rights reserved.
5 *
6 * Port to Contiki:
7 * Copyright (c) 2014 Andreas Dröscher <contiki@anticat.ch>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35/**
36 * \addtogroup cc2538-pka
37 * @{
38 *
39 * \file
40 * Implementation of the cc2538 PKA engine driver
41 */
42#include "contiki.h"
43#include "dev/pka.h"
44#include "dev/sys-ctrl.h"
45#include "dev/nvic.h"
46#include "lpm.h"
47#include "reg.h"
48
49#include <stdbool.h>
50#include <stdint.h>
51
52static volatile struct process *notification_process;
53/*---------------------------------------------------------------------------*/
54/** \brief The PKA engine ISR
55 *
56 * This is the interrupt service routine for the PKA engine.
57 *
58 * This ISR is called at worst from PM0, so lpm_exit() does not need
59 * to be called.
60 */
61void
63{
64 NVIC_ClearPendingIRQ(PKA_IRQn);
65 NVIC_DisableIRQ(PKA_IRQn);
66
67 if(notification_process != NULL) {
68 process_poll((struct process *)notification_process);
69 notification_process = NULL;
70 }
71}
72/*---------------------------------------------------------------------------*/
73static bool
74permit_pm1(void)
75{
76 return (REG(SYS_CTRL_RCGCSEC) & SYS_CTRL_RCGCSEC_PKA) == 0;
77}
78/*---------------------------------------------------------------------------*/
79void
81{
82 lpm_register_peripheral(permit_pm1);
83}
84/*---------------------------------------------------------------------------*/
85void
87{
88 /* Enable the clock for the PKA engine */
92}
93/*---------------------------------------------------------------------------*/
94void
96{
97 /* Gate the clock for the PKA engine */
98 REG(SYS_CTRL_RCGCSEC) &= ~SYS_CTRL_RCGCSEC_PKA;
99 REG(SYS_CTRL_SCGCSEC) &= ~SYS_CTRL_SCGCSEC_PKA;
100 REG(SYS_CTRL_DCGCSEC) &= ~SYS_CTRL_DCGCSEC_PKA;
101}
102/*---------------------------------------------------------------------------*/
103bool
105{
106 return (REG(PKA_FUNCTION) & PKA_FUNCTION_RUN) == 0;
107}
108/*---------------------------------------------------------------------------*/
109void
111{
112 notification_process = p;
113}
114/*---------------------------------------------------------------------------*/
115void
116pka_run_function(uint32_t function)
117{
118 pka_register_process_notification(process_current);
119 REG(PKA_FUNCTION) = PKA_FUNCTION_RUN | function;
120 NVIC_ClearPendingIRQ(PKA_IRQn);
121 NVIC_EnableIRQ(PKA_IRQn);
122}
123/*---------------------------------------------------------------------------*/
124void
125pka_little_endian_to_pka_ram(const uint32_t *words,
126 size_t num_words,
127 uintptr_t offset)
128{
129 offset *= sizeof(uint32_t);
130 offset += PKA_RAM_BASE;
131 for(size_t i = 0; i < num_words; i++) {
132 REG(offset + sizeof(uint32_t) * i) = words[i];
133 }
134}
135/*---------------------------------------------------------------------------*/
136void
137pka_word_to_pka_ram(uint32_t word, uintptr_t offset)
138{
139 offset *= sizeof(uint32_t);
140 offset += PKA_RAM_BASE;
141 REG(offset) = word;
142}
143/*---------------------------------------------------------------------------*/
144uint32_t
145pka_word_from_pka_ram(uintptr_t offset)
146{
147 offset *= sizeof(uint32_t);
148 offset += PKA_RAM_BASE;
149 return REG(offset);
150}
151/*---------------------------------------------------------------------------*/
152void
153pka_big_endian_to_pka_ram(const uint8_t *bytes,
154 size_t num_bytes,
155 uintptr_t offset)
156{
157 offset *= sizeof(uint32_t);
158 offset += PKA_RAM_BASE;
159 while(num_bytes) {
160 uint32_t word = bytes[--num_bytes];
161 word |= bytes[--num_bytes] << 8;
162 word |= bytes[--num_bytes] << 16;
163 word |= bytes[--num_bytes] << 24;
164 REG(offset) = word;
165 offset += sizeof(uint32_t);
166 }
167}
168/*---------------------------------------------------------------------------*/
169void
171 size_t num_words,
172 uintptr_t offset)
173{
174 offset *= sizeof(uint32_t);
175 offset += PKA_RAM_BASE;
176 size_t remaining_bytes = num_words * sizeof(uint32_t);
177 while(remaining_bytes) {
178 uint32_t word = REG(offset);
179 bytes[--remaining_bytes] = word;
180 bytes[--remaining_bytes] = word >> 8;
181 bytes[--remaining_bytes] = word >> 16;
182 bytes[--remaining_bytes] = word >> 24;
183 offset += sizeof(uint32_t);
184 }
185}
186/*---------------------------------------------------------------------------*/
187
188/** @} */
@ PKA_IRQn
PKA Interrupt.
Definition cc2538_cm3.h:112
#define PKA_FUNCTION_RUN
The host sets this bit to instruct the PKA module to begin processing the basic PKCP or complex seque...
Definition pka.h:439
uint32_t pka_word_from_pka_ram(uintptr_t offset)
Retrieves a word from the PKA RAM.
Definition pka.c:145
void pka_run_function(uint32_t function)
Initiates the given PKA function.
Definition pka.c:116
void pka_word_to_pka_ram(uint32_t word, uintptr_t offset)
Copies a word to the PKA RAM.
Definition pka.c:137
#define PKA_FUNCTION
PKA function This register contains the control bits to start basic PKCP as well as complex sequencer...
Definition pka.h:174
void pka_register_process_notification(struct process *p)
Registers a process to be notified of the completion of a PKA operation.
Definition pka.c:110
#define PKA_RAM_BASE
PKA Memory Address.
Definition pka.h:58
void pka_big_endian_from_pka_ram(uint8_t *bytes, size_t num_words, uintptr_t offset)
Retrieves a big-endian sequence of bytes from the PKA RAM.
Definition pka.c:170
void pka_isr(void)
The PKA engine ISR.
Definition pka.c:62
void pka_disable(void)
Disables the PKA engine.
Definition pka.c:95
void pka_init(void)
Enables and resets the PKA engine.
Definition pka.c:80
void pka_enable(void)
Enables the PKA engine.
Definition pka.c:86
void pka_little_endian_to_pka_ram(const uint32_t *words, size_t num_words, uintptr_t offset)
Copies a little-endian sequence of words to the PKA RAM.
Definition pka.c:125
void pka_big_endian_to_pka_ram(const uint8_t *bytes, size_t num_bytes, uintptr_t offset)
Copies a big-endian sequence of bytes to the PKA RAM.
Definition pka.c:153
bool pka_check_status(void)
Checks the status of the PKA engine operation.
Definition pka.c:104
#define SYS_CTRL_RCGCSEC
Sec Mod clocks - active mode.
Definition sys-ctrl.h:83
#define SYS_CTRL_SCGCSEC_PKA
PKA clock enable, CPU IDLE.
Definition sys-ctrl.h:180
#define SYS_CTRL_SCGCSEC
Sec Mod clocks - sleep mode.
Definition sys-ctrl.h:84
#define SYS_CTRL_RCGCSEC_PKA
PKA clock enable, CPU running.
Definition sys-ctrl.h:173
#define SYS_CTRL_DCGCSEC
Sec Mod clocks - PM0.
Definition sys-ctrl.h:85
#define SYS_CTRL_DCGCSEC_PKA
PKA clock enable, PM0.
Definition sys-ctrl.h:187
void process_poll(struct process *p)
Request a process to be polled.
Definition process.c:366
Header file for the ARM Nested Vectored Interrupt Controller.
Header file for the cc2538 PKA engine driver.
Header file with register manipulation macro definitions.
Header file for the cc2538 System Control driver.