1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2019 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_xbarb.h"
10 
11 /*******************************************************************************
12  * Definitions
13  ******************************************************************************/
14 
15 /* Component ID definition, used by tools. */
16 #ifndef FSL_COMPONENT_ID
17 #define FSL_COMPONENT_ID "platform.drivers.xbarb"
18 #endif
19 
20 typedef union
21 {
22     uint8_t _u8[2];
23     uint16_t _u16;
24 } xbarb_u8_u16_t;
25 
26 /*******************************************************************************
27  * Prototypes
28  ******************************************************************************/
29 
30 /*!
31  * @brief Get the XBARB instance from peripheral base address.
32  *
33  * @param base XBARB peripheral base address.
34  * @return XBARB instance.
35  */
36 static uint32_t XBARB_GetInstance(XBARB_Type *base);
37 
38 /*******************************************************************************
39  * Variables
40  ******************************************************************************/
41 
42 /* Array of XBARB peripheral base address. */
43 static XBARB_Type *const s_xbarbBases[] = XBARB_BASE_PTRS;
44 
45 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
46 /* Array of XBARB clock name. */
47 static const clock_ip_name_t s_xbarbClock[] = XBARB_CLOCKS;
48 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
49 
50 /*******************************************************************************
51  * Code
52  ******************************************************************************/
53 
XBARB_GetInstance(XBARB_Type * base)54 static uint32_t XBARB_GetInstance(XBARB_Type *base)
55 {
56     uint32_t instance;
57 
58     /* Find the instance index from base address mappings. */
59     for (instance = 0; instance < ARRAY_SIZE(s_xbarbBases); instance++)
60     {
61         if (s_xbarbBases[instance] == base)
62         {
63             break;
64         }
65     }
66 
67     assert(instance < ARRAY_SIZE(s_xbarbBases));
68 
69     return instance;
70 }
71 
72 /*!
73  * brief Initializes the XBARB module.
74  *
75  * This function un-gates the XBARB clock.
76  *
77  * param base XBARB peripheral address.
78  */
XBARB_Init(XBARB_Type * base)79 void XBARB_Init(XBARB_Type *base)
80 {
81 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
82     /* Enable XBARB module clock. */
83     CLOCK_EnableClock(s_xbarbClock[XBARB_GetInstance(base)]);
84 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
85 }
86 
87 /*!
88  * brief Shuts down the XBARB module.
89  *
90  * This function disables XBARB clock.
91  *
92  * param base XBARB peripheral address.
93  */
XBARB_Deinit(XBARB_Type * base)94 void XBARB_Deinit(XBARB_Type *base)
95 {
96 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
97     /* Disable XBARB module clock. */
98     CLOCK_DisableClock(s_xbarbClock[XBARB_GetInstance(base)]);
99 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
100 }
101 
102 /*!
103  * brief Configures a connection between the selected XBARB_IN[*] input and the XBARB_OUT[*] output signal.
104  *
105  * This function configures which XBARB input is connected to the selected XBARB output.
106  * If more than one XBARB module is available, only the inputs and outputs from the same module
107  * can be connected.
108  *
109  * param base XBARB peripheral address.
110  * param input XBARB input signal.
111  * param output XBARB output signal.
112  */
XBARB_SetSignalsConnection(XBARB_Type * base,xbar_input_signal_t input,xbar_output_signal_t output)113 void XBARB_SetSignalsConnection(XBARB_Type *base, xbar_input_signal_t input, xbar_output_signal_t output)
114 {
115     xbarb_u8_u16_t regVal;
116     uint8_t byteInReg;
117     uint8_t outputIndex = (uint8_t)output;
118 
119     byteInReg = outputIndex % 2U;
120 
121     regVal._u16 = XBARB_SELx(base, outputIndex);
122 
123     regVal._u8[byteInReg] = (uint8_t)input;
124 
125     XBARB_SELx(base, outputIndex) = regVal._u16;
126 }
127