1 /********************************************************************************
2 Copyright (C) 2016 Marvell International Ltd.
3
4 Marvell BSD License Option
5
6 If you received this File from Marvell, you may opt to use, redistribute and/or
7 modify this File under the following licensing terms.
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
10
11 * Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
13
14 * 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 * Neither the name of Marvell nor the names of its contributors may be
19 used to endorse or promote products derived from this software without
20 specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 *******************************************************************************/
34
35 #include "ComPhyLib.h"
36
37 STATIC
38 VOID
ComPhyMuxCheckConfig(IN COMPHY_MUX_DATA * MuxData,IN COMPHY_MAP * ComPhyMapData,IN UINTN ComPhyMaxLanes)39 ComPhyMuxCheckConfig (
40 IN COMPHY_MUX_DATA *MuxData,
41 IN COMPHY_MAP *ComPhyMapData,
42 IN UINTN ComPhyMaxLanes
43 )
44 {
45 COMPHY_MUX_OPTIONS *PtrMuxOpt;
46 UINTN Lane, Opt, Valid;
47
48 for (Lane = 0; Lane < ComPhyMaxLanes; Lane++, ComPhyMapData++, MuxData++) {
49 PtrMuxOpt = MuxData->MuxValues;
50 for (Opt = 0, Valid = 0; Opt < MuxData->MaxLaneValues; Opt++, PtrMuxOpt++) {
51 if (PtrMuxOpt->Type == ComPhyMapData->Type) {
52 Valid = 1;
53 break;
54 }
55 }
56 if (Valid == 0) {
57 DEBUG((DEBUG_INFO, "Lane number %d, had invalid Type %d\n", Lane,
58 ComPhyMapData->Type));
59 DEBUG((DEBUG_INFO, "Set Lane %d as Type %d\n", Lane,
60 PHY_TYPE_UNCONNECTED));
61 ComPhyMapData->Type = PHY_TYPE_UNCONNECTED;
62 } else {
63 DEBUG((DEBUG_INFO, "Lane number %d, has Type %d\n", Lane,
64 ComPhyMapData->Type));
65 }
66 }
67 }
68
69 STATIC
70 UINT32
ComPhyMuxGetMuxValue(IN COMPHY_MUX_DATA * MuxData,IN UINT32 Type,IN UINTN Lane)71 ComPhyMuxGetMuxValue (
72 IN COMPHY_MUX_DATA *MuxData,
73 IN UINT32 Type,
74 IN UINTN Lane
75 )
76 {
77 COMPHY_MUX_OPTIONS *PtrMuxOpt;
78 UINTN Opt;
79 UINT32 Value = 0;
80
81 PtrMuxOpt = MuxData->MuxValues;
82 for (Opt = 0 ; Opt < MuxData->MaxLaneValues; Opt++, PtrMuxOpt++)
83 if (PtrMuxOpt->Type == Type) {
84 Value = PtrMuxOpt->MuxValue;
85 break;
86 }
87
88 return Value;
89 }
90
91 STATIC
92 VOID
ComPhyMuxRegWrite(IN COMPHY_MUX_DATA * MuxData,IN COMPHY_MAP * ComPhyMapData,IN UINTN ComPhyMaxLanes,IN EFI_PHYSICAL_ADDRESS SelectorBase,IN UINT32 BitCount)93 ComPhyMuxRegWrite (
94 IN COMPHY_MUX_DATA *MuxData,
95 IN COMPHY_MAP *ComPhyMapData,
96 IN UINTN ComPhyMaxLanes,
97 IN EFI_PHYSICAL_ADDRESS SelectorBase,
98 IN UINT32 BitCount
99 )
100 {
101 UINT32 Lane, Value, Offset, Mask;
102
103 for (Lane = 0; Lane < ComPhyMaxLanes; Lane++, ComPhyMapData++, MuxData++) {
104 Offset = Lane * BitCount;
105 Mask = (((1 << BitCount) - 1) << Offset);
106 Value = (ComPhyMuxGetMuxValue (MuxData, ComPhyMapData->Type, Lane) <<
107 Offset);
108 RegSet (SelectorBase, Value, Mask);
109 }
110 }
111
112 VOID
ComPhyMuxInit(IN CHIP_COMPHY_CONFIG * PtrChipCfg,IN COMPHY_MAP * ComPhyMapData,IN EFI_PHYSICAL_ADDRESS SelectorBase)113 ComPhyMuxInit (
114 IN CHIP_COMPHY_CONFIG *PtrChipCfg,
115 IN COMPHY_MAP *ComPhyMapData,
116 IN EFI_PHYSICAL_ADDRESS SelectorBase
117 )
118 {
119 COMPHY_MUX_DATA *MuxData;
120 UINT32 MuxBitCount;
121 UINT32 ComPhyMaxLanes;
122
123 ComPhyMaxLanes = PtrChipCfg->LanesCount;
124 MuxData = PtrChipCfg->MuxData;
125 MuxBitCount = PtrChipCfg->MuxBitCount;
126
127 /* Check if the configuration is valid */
128 ComPhyMuxCheckConfig (MuxData, ComPhyMapData, ComPhyMaxLanes);
129 /* Init COMPHY selectors */
130 ComPhyMuxRegWrite (MuxData, ComPhyMapData, ComPhyMaxLanes, SelectorBase,
131 MuxBitCount);
132 }
133