mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by Umar Naeem

Committer:
ranaumarnaeem
Date:
Tue May 23 12:54:50 2017 +0000
Revision:
165:2dd56e6daeec
Parent:
150:02e0a0aed4ec
jhjg

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 150:02e0a0aed4ec 1 /**
<> 150:02e0a0aed4ec 2 * @file maa.c
<> 150:02e0a0aed4ec 3 * @brief Modular Arithmetic Accelerator (MAA) API Function Implementations.
<> 150:02e0a0aed4ec 4 */
<> 150:02e0a0aed4ec 5 /* *****************************************************************************
<> 150:02e0a0aed4ec 6 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
<> 150:02e0a0aed4ec 7 *
<> 150:02e0a0aed4ec 8 * Permission is hereby granted, free of charge, to any person obtaining a
<> 150:02e0a0aed4ec 9 * copy of this software and associated documentation files (the "Software"),
<> 150:02e0a0aed4ec 10 * to deal in the Software without restriction, including without limitation
<> 150:02e0a0aed4ec 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
<> 150:02e0a0aed4ec 12 * and/or sell copies of the Software, and to permit persons to whom the
<> 150:02e0a0aed4ec 13 * Software is furnished to do so, subject to the following conditions:
<> 150:02e0a0aed4ec 14 *
<> 150:02e0a0aed4ec 15 * The above copyright notice and this permission notice shall be included
<> 150:02e0a0aed4ec 16 * in all copies or substantial portions of the Software.
<> 150:02e0a0aed4ec 17 *
<> 150:02e0a0aed4ec 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
<> 150:02e0a0aed4ec 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
<> 150:02e0a0aed4ec 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
<> 150:02e0a0aed4ec 21 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
<> 150:02e0a0aed4ec 22 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
<> 150:02e0a0aed4ec 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
<> 150:02e0a0aed4ec 24 * OTHER DEALINGS IN THE SOFTWARE.
<> 150:02e0a0aed4ec 25 *
<> 150:02e0a0aed4ec 26 * Except as contained in this notice, the name of Maxim Integrated
<> 150:02e0a0aed4ec 27 * Products, Inc. shall not be used except as stated in the Maxim Integrated
<> 150:02e0a0aed4ec 28 * Products, Inc. Branding Policy.
<> 150:02e0a0aed4ec 29 *
<> 150:02e0a0aed4ec 30 * The mere transfer of this software does not imply any licenses
<> 150:02e0a0aed4ec 31 * of trade secrets, proprietary technology, copyrights, patents,
<> 150:02e0a0aed4ec 32 * trademarks, maskwork rights, or any other form of intellectual
<> 150:02e0a0aed4ec 33 * property whatsoever. Maxim Integrated Products, Inc. retains all
<> 150:02e0a0aed4ec 34 * ownership rights.
<> 150:02e0a0aed4ec 35 *
<> 150:02e0a0aed4ec 36 **************************************************************************** */
<> 150:02e0a0aed4ec 37
<> 150:02e0a0aed4ec 38 /* **** Includes **** */
<> 150:02e0a0aed4ec 39 #include <string.h>
<> 150:02e0a0aed4ec 40 #include "mxc_assert.h"
<> 150:02e0a0aed4ec 41 #include "mxc_lock.h"
<> 150:02e0a0aed4ec 42 #include "mxc_sys.h"
<> 150:02e0a0aed4ec 43 #include "maa.h"
<> 150:02e0a0aed4ec 44
<> 150:02e0a0aed4ec 45 /**
<> 150:02e0a0aed4ec 46 * @ingroup maa
<> 150:02e0a0aed4ec 47 * @{
<> 150:02e0a0aed4ec 48 */
<> 150:02e0a0aed4ec 49
<> 150:02e0a0aed4ec 50 ///@cond
<> 150:02e0a0aed4ec 51 #define maa_is_running() (MXC_MAA->ctrl & MXC_F_MAA_CTRL_START ? 1 : 0)
<> 150:02e0a0aed4ec 52
<> 150:02e0a0aed4ec 53 /**
<> 150:02e0a0aed4ec 54 * Macro that translates from #mxc_maa_reg_select_t to a pointer for MAA memory operations.
<> 150:02e0a0aed4ec 55 */
<> 150:02e0a0aed4ec 56 #define UMAA_REGFILE_TO_ADDR(base, x) ((base + (MXC_MAA_HALF_SIZE * x)))
<> 150:02e0a0aed4ec 57
<> 150:02e0a0aed4ec 58 /**
<> 150:02e0a0aed4ec 59 * Macro that adjusts the pointer so that it is pointing to the last 32-bit word in the maa (sub-)segment
<> 150:02e0a0aed4ec 60 */
<> 150:02e0a0aed4ec 61 #define UMAA_ADDR_INDEX_LAST_32BIT(x) ((uint8_t *)x + (MXC_MAA_HALF_SIZE-4))
<> 150:02e0a0aed4ec 62 ///@endcond
<> 150:02e0a0aed4ec 63
<> 150:02e0a0aed4ec 64
<> 150:02e0a0aed4ec 65 /* ************************************************************************* */
<> 150:02e0a0aed4ec 66 mxc_maa_ret_t MAA_Init(void)
<> 150:02e0a0aed4ec 67 {
<> 150:02e0a0aed4ec 68 SYS_MAA_Init();
<> 150:02e0a0aed4ec 69
<> 150:02e0a0aed4ec 70 return MXC_E_MAA_OK;
<> 150:02e0a0aed4ec 71 }
<> 150:02e0a0aed4ec 72
<> 150:02e0a0aed4ec 73 /* ************************************************************************* */
<> 150:02e0a0aed4ec 74 mxc_maa_ret_t MAA_WipeRAM(void)
<> 150:02e0a0aed4ec 75 {
<> 150:02e0a0aed4ec 76 /* Check for running MAA */
<> 150:02e0a0aed4ec 77 if (maa_is_running()) {
<> 150:02e0a0aed4ec 78 return MXC_E_MAA_ERR;
<> 150:02e0a0aed4ec 79 }
<> 150:02e0a0aed4ec 80
<> 150:02e0a0aed4ec 81 /* Clear register files */
<> 150:02e0a0aed4ec 82 memset((void *)MXC_MAA_MEM->seg0, 0, sizeof(MXC_MAA_MEM->seg0));
<> 150:02e0a0aed4ec 83 memset((void *)MXC_MAA_MEM->seg1, 0, sizeof(MXC_MAA_MEM->seg1));
<> 150:02e0a0aed4ec 84 memset((void *)MXC_MAA_MEM->seg2, 0, sizeof(MXC_MAA_MEM->seg2));
<> 150:02e0a0aed4ec 85 memset((void *)MXC_MAA_MEM->seg3, 0, sizeof(MXC_MAA_MEM->seg3));
<> 150:02e0a0aed4ec 86 memset((void *)MXC_MAA_MEM->seg4, 0, sizeof(MXC_MAA_MEM->seg4));
<> 150:02e0a0aed4ec 87 memset((void *)MXC_MAA_MEM->seg5, 0, sizeof(MXC_MAA_MEM->seg5));
<> 150:02e0a0aed4ec 88
<> 150:02e0a0aed4ec 89 return MXC_E_MAA_OK;
<> 150:02e0a0aed4ec 90 }
<> 150:02e0a0aed4ec 91
<> 150:02e0a0aed4ec 92 /* ************************************************************************* */
<> 150:02e0a0aed4ec 93 mxc_maa_ret_t MAA_Load(mxc_maa_reg_select_t regfile, const uint8_t *data, unsigned int size, mxc_maa_endian_select_t flag)
<> 150:02e0a0aed4ec 94 {
<> 150:02e0a0aed4ec 95 uint32_t *maaptr;
<> 150:02e0a0aed4ec 96 uint32_t fill;
<> 150:02e0a0aed4ec 97 unsigned int zerotmp;
<> 150:02e0a0aed4ec 98
<> 150:02e0a0aed4ec 99 if ((regfile > MXC_E_REG_51) || (size > MXC_MAA_REG_SIZE)) {
<> 150:02e0a0aed4ec 100 /* Out of range */
<> 150:02e0a0aed4ec 101 return MXC_E_MAA_ERR;
<> 150:02e0a0aed4ec 102 }
<> 150:02e0a0aed4ec 103
<> 150:02e0a0aed4ec 104 if (flag == MXC_MAA_F_MEM_REVERSE) {
<> 150:02e0a0aed4ec 105 /* This is not currently implemented */
<> 150:02e0a0aed4ec 106 return MXC_E_MAA_ERR;
<> 150:02e0a0aed4ec 107 }
<> 150:02e0a0aed4ec 108
<> 150:02e0a0aed4ec 109 maaptr = (uint32_t *)UMAA_REGFILE_TO_ADDR(MXC_BASE_MAA_MEM, regfile);
<> 150:02e0a0aed4ec 110
<> 150:02e0a0aed4ec 111 /*
<> 150:02e0a0aed4ec 112 * MAA (sub-)segments must be loaded with zero pad to a 64-bit boundary, or the "garbage bits"
<> 150:02e0a0aed4ec 113 * will case erroneous results.
<> 150:02e0a0aed4ec 114 */
<> 150:02e0a0aed4ec 115 /* Find the ceiling for the closest 64-bit boundary based on the selected MAWS */
<> 150:02e0a0aed4ec 116 zerotmp = (((MXC_MAA->maws & MXC_F_MAA_MAWS_MODLEN) >> MXC_F_MAA_MAWS_MODLEN_POS) + 63) & 0xfc0;
<> 150:02e0a0aed4ec 117 /* Convert to bytes */
<> 150:02e0a0aed4ec 118 zerotmp /= 8;
<> 150:02e0a0aed4ec 119
<> 150:02e0a0aed4ec 120 /* Fill uMAA memory in long word sized chunks */
<> 150:02e0a0aed4ec 121 while (size > 3) {
<> 150:02e0a0aed4ec 122 *maaptr++ = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
<> 150:02e0a0aed4ec 123 data += 4;
<> 150:02e0a0aed4ec 124 size -= 4;
<> 150:02e0a0aed4ec 125 zerotmp = (zerotmp > 4) ? (zerotmp - 4) : 0;
<> 150:02e0a0aed4ec 126 }
<> 150:02e0a0aed4ec 127
<> 150:02e0a0aed4ec 128 /* Remainder */
<> 150:02e0a0aed4ec 129 if (size) {
<> 150:02e0a0aed4ec 130 fill = data[0];
<> 150:02e0a0aed4ec 131 fill |= ((size > 1) ? (data[1] << 8) : 0);
<> 150:02e0a0aed4ec 132 fill |= ((size > 2) ? (data[2] << 16) : 0);
<> 150:02e0a0aed4ec 133 *maaptr++ = fill;
<> 150:02e0a0aed4ec 134
<> 150:02e0a0aed4ec 135 /* We just filled 4 bytes in this section */
<> 150:02e0a0aed4ec 136 zerotmp = (zerotmp > 4) ? (zerotmp - 4) : 0;
<> 150:02e0a0aed4ec 137 }
<> 150:02e0a0aed4ec 138
<> 150:02e0a0aed4ec 139 /* Wipe the remaining "garbage bits" */
<> 150:02e0a0aed4ec 140 while (zerotmp) {
<> 150:02e0a0aed4ec 141 *maaptr++ = 0;
<> 150:02e0a0aed4ec 142 zerotmp = (zerotmp > 4) ? (zerotmp - 4) : 0;
<> 150:02e0a0aed4ec 143 }
<> 150:02e0a0aed4ec 144
<> 150:02e0a0aed4ec 145 return MXC_E_MAA_OK;
<> 150:02e0a0aed4ec 146 }
<> 150:02e0a0aed4ec 147
<> 150:02e0a0aed4ec 148 /* ************************************************************************* */
<> 150:02e0a0aed4ec 149 mxc_maa_ret_t MAA_Unload(mxc_maa_reg_select_t regfile, uint8_t *data, unsigned int size, mxc_maa_endian_select_t flag)
<> 150:02e0a0aed4ec 150 {
<> 150:02e0a0aed4ec 151 uint32_t *maaptr;
<> 150:02e0a0aed4ec 152 uint32_t fill;
<> 150:02e0a0aed4ec 153
<> 150:02e0a0aed4ec 154 if ((regfile > MXC_E_REG_51) || (size > MXC_MAA_REG_SIZE)) {
<> 150:02e0a0aed4ec 155 /* Out of range */
<> 150:02e0a0aed4ec 156 return MXC_E_MAA_ERR;
<> 150:02e0a0aed4ec 157 }
<> 150:02e0a0aed4ec 158
<> 150:02e0a0aed4ec 159 if (flag == MXC_MAA_F_MEM_REVERSE) {
<> 150:02e0a0aed4ec 160 /* This is not currently implemented */
<> 150:02e0a0aed4ec 161 return MXC_E_MAA_ERR;
<> 150:02e0a0aed4ec 162 }
<> 150:02e0a0aed4ec 163
<> 150:02e0a0aed4ec 164 maaptr = (uint32_t *)UMAA_REGFILE_TO_ADDR(MXC_BASE_MAA_MEM, regfile);
<> 150:02e0a0aed4ec 165
<> 150:02e0a0aed4ec 166 /* Unload uMAA memory in long word sized chunks */
<> 150:02e0a0aed4ec 167 while (size > 3) {
<> 150:02e0a0aed4ec 168 fill = *maaptr++;
<> 150:02e0a0aed4ec 169 data[0] = fill & 0xff;
<> 150:02e0a0aed4ec 170 data[1] = (fill >> 8) & 0xff;
<> 150:02e0a0aed4ec 171 data[2] = (fill >> 16) & 0xff;
<> 150:02e0a0aed4ec 172 data[3] = (fill >> 24) & 0xff;
<> 150:02e0a0aed4ec 173 data += 4;
<> 150:02e0a0aed4ec 174 size -= 4;
<> 150:02e0a0aed4ec 175 }
<> 150:02e0a0aed4ec 176
<> 150:02e0a0aed4ec 177 /* Remainder */
<> 150:02e0a0aed4ec 178 if (size) {
<> 150:02e0a0aed4ec 179 fill = *maaptr;
<> 150:02e0a0aed4ec 180 data[0] = fill & 0xff;
<> 150:02e0a0aed4ec 181 if (size > 1) {
<> 150:02e0a0aed4ec 182 data[1] = (fill >> 8) & 0xff;
<> 150:02e0a0aed4ec 183 }
<> 150:02e0a0aed4ec 184 if (size > 2) {
<> 150:02e0a0aed4ec 185 data[2] = (fill >> 16) & 0xff;
<> 150:02e0a0aed4ec 186 }
<> 150:02e0a0aed4ec 187 }
<> 150:02e0a0aed4ec 188
<> 150:02e0a0aed4ec 189 return MXC_E_MAA_OK;
<> 150:02e0a0aed4ec 190 }
<> 150:02e0a0aed4ec 191
<> 150:02e0a0aed4ec 192 /* ************************************************************************* */
<> 150:02e0a0aed4ec 193 mxc_maa_ret_t MAA_Run(mxc_maa_operation_t op,
<> 150:02e0a0aed4ec 194 mxc_maa_reg_select_t al, mxc_maa_reg_select_t bl,
<> 150:02e0a0aed4ec 195 mxc_maa_reg_select_t rl, mxc_maa_reg_select_t tl)
<> 150:02e0a0aed4ec 196 {
<> 150:02e0a0aed4ec 197 if (maa_is_running()) {
<> 150:02e0a0aed4ec 198 /* Attempt to start the MAA while already running */
<> 150:02e0a0aed4ec 199 return MXC_E_MAA_ERR;
<> 150:02e0a0aed4ec 200 }
<> 150:02e0a0aed4ec 201
<> 150:02e0a0aed4ec 202 /* Clear out any previous flags */
<> 150:02e0a0aed4ec 203 MXC_MAA->ctrl = 0x00000020;
<> 150:02e0a0aed4ec 204
<> 150:02e0a0aed4ec 205 /* Construct memory segment selections, select operation, and start the uMAA */
<> 150:02e0a0aed4ec 206 MXC_MAA->ctrl = (((al << MXC_F_MAA_CTRL_SEG_A_POS) & MXC_F_MAA_CTRL_SEG_A) |
<> 150:02e0a0aed4ec 207 ((bl << MXC_F_MAA_CTRL_SEG_B_POS) & MXC_F_MAA_CTRL_SEG_B) |
<> 150:02e0a0aed4ec 208 ((rl << MXC_F_MAA_CTRL_SEG_RES_POS) & MXC_F_MAA_CTRL_SEG_RES) |
<> 150:02e0a0aed4ec 209 ((tl << MXC_F_MAA_CTRL_SEG_TMP_POS) & MXC_F_MAA_CTRL_SEG_TMP) |
<> 150:02e0a0aed4ec 210 ((op << MXC_F_MAA_CTRL_OPSEL_POS) & MXC_F_MAA_CTRL_OPSEL) |
<> 150:02e0a0aed4ec 211 MXC_F_MAA_CTRL_START);
<> 150:02e0a0aed4ec 212
<> 150:02e0a0aed4ec 213 /* Blocking wait for uMAA to complete. */
<> 150:02e0a0aed4ec 214 while ((MXC_MAA->ctrl & MXC_F_MAA_CTRL_IF_DONE) == 0);
<> 150:02e0a0aed4ec 215
<> 150:02e0a0aed4ec 216 if (MXC_MAA->ctrl & MXC_F_MAA_CTRL_IF_ERROR) {
<> 150:02e0a0aed4ec 217 /* MAA signaled error */
<> 150:02e0a0aed4ec 218 return MXC_E_MAA_ERR;
<> 150:02e0a0aed4ec 219 }
<> 150:02e0a0aed4ec 220
<> 150:02e0a0aed4ec 221 return MXC_E_MAA_OK;
<> 150:02e0a0aed4ec 222 }
<> 150:02e0a0aed4ec 223
<> 150:02e0a0aed4ec 224 /* ************************************************************************* */
<> 150:02e0a0aed4ec 225 mxc_maa_ret_t MAA_SetWordSize(unsigned int len)
<> 150:02e0a0aed4ec 226 {
<> 150:02e0a0aed4ec 227 if ((len > MXC_MAA_REG_SIZE_BITS) || maa_is_running()) {
<> 150:02e0a0aed4ec 228 return MXC_E_MAA_ERR;
<> 150:02e0a0aed4ec 229 }
<> 150:02e0a0aed4ec 230
<> 150:02e0a0aed4ec 231 /* Set bit length for calculation, and disable endian swap */
<> 150:02e0a0aed4ec 232 MXC_MAA->maws = ((len << MXC_F_MAA_MAWS_MODLEN_POS) & MXC_F_MAA_MAWS_MODLEN);
<> 150:02e0a0aed4ec 233
<> 150:02e0a0aed4ec 234 return MXC_E_MAA_OK;
<> 150:02e0a0aed4ec 235 }
<> 150:02e0a0aed4ec 236 /**@} end of ingroup maa */