1 /*******************************************************************************
2 Copyright (C) Marvell International Ltd. and its affiliates
4 This software file (the "File") is owned and distributed by Marvell
5 International Ltd. and/or its affiliates ("Marvell") under the following
6 alternative licensing terms. Once you have made an election to distribute the
7 File under one of the following license alternatives, please (i) delete this
8 introductory statement regarding license alternatives, (ii) delete the two
9 license alternatives that you have not elected to use and (iii) preserve the
10 Marvell copyright notice above.
12 ********************************************************************************
13 Marvell Commercial License Option
15 If you received this File from Marvell and you have entered into a commercial
16 license agreement (a "Commercial License") with Marvell, the File is licensed
17 to you under the terms of the applicable Commercial License.
19 ********************************************************************************
20 Marvell GPL License Option
22 If you received this File from Marvell, you may opt to use, redistribute and/or
23 modify this File in accordance with the terms and conditions of the General
24 Public License Version 2, June 1991 (the "GPL License"), a copy of which is
25 available along with the File in the license.txt file or by writing to the Free
26 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or
27 on the worldwide web at http://www.gnu.org/licenses/gpl.txt.
29 THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED
30 WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY
31 DISCLAIMED. The GPL License provides additional details about this warranty
33 ********************************************************************************
34 Marvell BSD License Option
36 If you received this File from Marvell, you may opt to use, redistribute and/or
37 modify this File under the following licensing terms.
38 Redistribution and use in source and binary forms, with or without modification,
39 are permitted provided that the following conditions are met:
41 * Redistributions of source code must retain the above copyright notice,
42 this list of conditions and the following disclaimer.
44 * Redistributions in binary form must reproduce the above copyright
45 notice, this list of conditions and the following disclaimer in the
46 documentation and/or other materials provided with the distribution.
48 * Neither the name of Marvell nor the names of its contributors may be
49 used to endorse or promote products derived from this software without
50 specific prior written permission.
52 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
53 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
54 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
56 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
58 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
59 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *******************************************************************************/
66 #include "ctrlEnv/sys/mvSysGbe.h"
70 typedef struct _mvEthDecWin
73 MV_ADDR_WIN addrWin
; /* An address window*/
74 MV_BOOL enable
; /* Address decode window is enabled/disabled */
78 MV_TARGET ethAddrDecPrioTap
[] =
80 #if defined(MV_INCLUDE_SDRAM_CS0)
83 #if defined(MV_INCLUDE_SDRAM_CS1)
86 #if defined(MV_INCLUDE_SDRAM_CS2)
89 #if defined(MV_INCLUDE_SDRAM_CS3)
92 #if defined(MV_INCLUDE_DEVICE_CS0)
95 #if defined(MV_INCLUDE_DEVICE_CS1)
98 #if defined(MV_INCLUDE_DEVICE_CS2)
101 #if defined(MV_INCLUDE_DEVICE_CS3)
104 #if defined(MV_INCLUDE_PEX)
110 static MV_STATUS
ethWinOverlapDetect(int port
, MV_U32 winNum
, MV_ADDR_WIN
*pAddrWin
);
111 static MV_STATUS
mvEthWinSet(int port
, MV_U32 winNum
, MV_ETH_DEC_WIN
*pAddrDecWin
);
112 static MV_STATUS
mvEthWinGet(int port
, MV_U32 winNum
, MV_ETH_DEC_WIN
*pAddrDecWin
);
115 /*******************************************************************************
116 * mvEthWinInit - Initialize ETH address decode windows
119 * This function initialize ETH window decode unit. It set the
120 * default address decode windows of the unit.
129 * MV_ERROR if setting fail.
130 *******************************************************************************/
131 /* Configure EthDrv memory map registes. */
132 MV_STATUS
mvEthWinInit (int port
)
134 MV_U32 winNum
, status
, winPrioIndex
=0, i
, regVal
=0;
135 MV_ETH_DEC_WIN ethWin
;
136 MV_CPU_DEC_WIN cpuAddrDecWin
;
137 static MV_U32 accessProtReg
= 0;
139 #if (MV_ETH_VERSION <= 1)
140 static MV_BOOL isFirst
= MV_TRUE
;
142 if(isFirst
== MV_FALSE
)
144 MV_REG_WRITE(ETH_ACCESS_PROTECT_REG(port
), accessProtReg
);
148 #endif /* MV_GIGA_ETH_VERSION */
150 /* Initiate Ethernet address decode */
152 /* First disable all address decode windows */
153 for(winNum
=0; winNum
<ETH_MAX_DECODE_WIN
; winNum
++)
155 regVal
|= MV_BIT_MASK(winNum
);
157 MV_REG_WRITE(ETH_BASE_ADDR_ENABLE_REG(port
), regVal
);
159 /* Go through all windows in user table until table terminator */
160 for (winNum
=0; ((ethAddrDecPrioTap
[winPrioIndex
] != TBL_TERM
) &&
161 (winNum
< ETH_MAX_DECODE_WIN
)); )
163 /* first get attributes from CPU If */
164 status
= mvCpuIfTargetWinGet(ethAddrDecPrioTap
[winPrioIndex
],
167 if(MV_NO_SUCH
== status
)
174 mvOsPrintf("mvEthWinInit: ERR. mvCpuIfTargetWinGet failed\n");
178 if (cpuAddrDecWin
.enable
== MV_TRUE
)
180 ethWin
.addrWin
.baseHigh
= cpuAddrDecWin
.addrWin
.baseHigh
;
181 ethWin
.addrWin
.baseLow
= cpuAddrDecWin
.addrWin
.baseLow
;
182 ethWin
.addrWin
.size
= cpuAddrDecWin
.addrWin
.size
;
183 ethWin
.enable
= MV_TRUE
;
184 ethWin
.target
= ethAddrDecPrioTap
[winPrioIndex
];
186 if(MV_OK
!= mvEthWinSet(port
, winNum
, ðWin
))
188 mvOsPrintf("mvEthWinInit: ERR. mvEthWinSet failed winNum=%d\n",
197 /* set full access to all windows. */
198 for(i
=0; i
<winNum
; i
++)
200 accessProtReg
|= (FULL_ACCESS
<< (i
*2));
202 MV_REG_WRITE(ETH_ACCESS_PROTECT_REG(port
), accessProtReg
);
207 /*******************************************************************************
208 * mvEthWinSet - Set ETH target address window
211 * This function sets a peripheral target (e.g. SDRAM bank0, PCI_MEM0)
212 * address window, also known as address decode window.
213 * After setting this target window, the ETH will be able to access the
214 * target within the address window.
217 * winNum - ETH to target address decode window number.
218 * pAddrDecWin - ETH target window data structure.
224 * MV_ERROR if address window overlapps with other address decode windows.
225 * MV_BAD_PARAM if base address is invalid parameter or target is
228 *******************************************************************************/
229 MV_STATUS
mvEthWinSet(int port
, MV_U32 winNum
, MV_ETH_DEC_WIN
*pAddrDecWin
)
231 MV_TARGET_ATTRIB targetAttribs
;
234 /* Parameter checking */
235 if (winNum
>= ETH_MAX_DECODE_WIN
)
237 mvOsPrintf("mvEthWinSet: ERR. Invalid win num %d\n",winNum
);
241 /* Check if the requested window overlapps with current windows */
242 if (MV_TRUE
== ethWinOverlapDetect(port
, winNum
, &pAddrDecWin
->addrWin
))
244 mvOsPrintf("mvEthWinSet: ERR. Window %d overlap\n", winNum
);
248 /* check if address is aligned to the size */
249 if(MV_IS_NOT_ALIGN(pAddrDecWin
->addrWin
.baseLow
, pAddrDecWin
->addrWin
.size
))
251 mvOsPrintf("mvEthWinSet: Error setting Ethernet window %d to "\
252 "target %s.\nAddress 0x%08x is unaligned to size 0x%x.\n",
254 mvCtrlTargetNameGet(pAddrDecWin
->target
),
255 pAddrDecWin
->addrWin
.baseLow
,
256 pAddrDecWin
->addrWin
.size
);
261 decRegs
.baseReg
= MV_REG_READ(ETH_WIN_BASE_REG(port
, winNum
));
262 decRegs
.sizeReg
= MV_REG_READ(ETH_WIN_SIZE_REG(port
, winNum
));
264 if (MV_OK
!= mvCtrlAddrDecToReg(&(pAddrDecWin
->addrWin
),&decRegs
))
266 mvOsPrintf("mvEthWinSet:mvCtrlAddrDecToReg Failed\n");
270 mvCtrlAttribGet(pAddrDecWin
->target
,&targetAttribs
);
273 decRegs
.baseReg
&= ~ETH_WIN_ATTR_MASK
;
274 decRegs
.baseReg
|= targetAttribs
.attrib
<< ETH_WIN_ATTR_OFFS
;
276 decRegs
.baseReg
&= ~ETH_WIN_TARGET_MASK
;
277 decRegs
.baseReg
|= targetAttribs
.targetId
<< ETH_WIN_TARGET_OFFS
;
279 /* for the safe side we disable the window before writing the new
281 mvEthWinEnable(port
, winNum
, MV_FALSE
);
282 MV_REG_WRITE(ETH_WIN_BASE_REG(port
, winNum
), decRegs
.baseReg
);
284 /* Write to address decode Size Register */
285 MV_REG_WRITE(ETH_WIN_SIZE_REG(port
, winNum
), decRegs
.sizeReg
);
287 /* Enable address decode target window */
288 if (pAddrDecWin
->enable
== MV_TRUE
)
290 mvEthWinEnable(port
, winNum
, MV_TRUE
);
296 /*******************************************************************************
297 * mvETHWinGet - Get dma peripheral target address window.
300 * Get ETH peripheral target address window.
303 * winNum - ETH to target address decode window number.
306 * pAddrDecWin - ETH target window data structure.
309 * MV_ERROR if register parameters are invalid.
311 *******************************************************************************/
312 MV_STATUS
mvEthWinGet(int port
, MV_U32 winNum
, MV_ETH_DEC_WIN
*pAddrDecWin
)
315 MV_TARGET_ATTRIB targetAttrib
;
317 /* Parameter checking */
318 if (winNum
>= ETH_MAX_DECODE_WIN
)
320 mvOsPrintf("mvEthWinGet: ERR. Invalid winNum %d\n", winNum
);
321 return MV_NOT_SUPPORTED
;
324 decRegs
.baseReg
= MV_REG_READ(ETH_WIN_BASE_REG(port
, winNum
));
325 decRegs
.sizeReg
= MV_REG_READ(ETH_WIN_SIZE_REG(port
, winNum
));
327 if (MV_OK
!= mvCtrlRegToAddrDec(&decRegs
,&(pAddrDecWin
->addrWin
)))
329 mvOsPrintf("mvAhbToMbusWinGet: mvCtrlRegToAddrDec Failed \n");
333 /* attrib and targetId */
334 targetAttrib
.attrib
=
335 (decRegs
.baseReg
& ETH_WIN_ATTR_MASK
) >> ETH_WIN_ATTR_OFFS
;
336 targetAttrib
.targetId
=
337 (decRegs
.baseReg
& ETH_WIN_TARGET_MASK
) >> ETH_WIN_TARGET_OFFS
;
339 pAddrDecWin
->target
= mvCtrlTargetGet(&targetAttrib
);
341 /* Check if window is enabled */
342 if (~(MV_REG_READ(ETH_BASE_ADDR_ENABLE_REG(port
))) & (1 << winNum
) )
344 pAddrDecWin
->enable
= MV_TRUE
;
348 pAddrDecWin
->enable
= MV_FALSE
;
354 /*******************************************************************************
355 * mvEthWinEnable - Enable/disable a ETH to target address window
358 * This function enable/disable a ETH to target address window.
359 * According to parameter 'enable' the routine will enable the
360 * window, thus enabling ETH accesses (before enabling the window it is
361 * tested for overlapping). Otherwise, the window will be disabled.
364 * winNum - ETH to target address decode window number.
365 * enable - Enable/disable parameter.
371 * MV_ERROR if decode window number was wrong or enabled window overlapps.
373 *******************************************************************************/
374 MV_STATUS
mvEthWinEnable(int port
, MV_U32 winNum
,MV_BOOL enable
)
376 MV_ETH_DEC_WIN addrDecWin
;
378 /* Parameter checking */
379 if (winNum
>= ETH_MAX_DECODE_WIN
)
381 mvOsPrintf("mvEthTargetWinEnable:ERR. Invalid winNum%d\n",winNum
);
385 if (enable
== MV_TRUE
)
386 { /* First check for overlap with other enabled windows */
387 /* Get current window */
388 if (MV_OK
!= mvEthWinGet(port
, winNum
, &addrDecWin
))
390 mvOsPrintf("mvEthTargetWinEnable:ERR. targetWinGet fail\n");
393 /* Check for overlapping */
394 if (MV_FALSE
== ethWinOverlapDetect(port
, winNum
, &(addrDecWin
.addrWin
)))
396 /* No Overlap. Enable address decode target window */
397 MV_REG_BIT_RESET(ETH_BASE_ADDR_ENABLE_REG(port
), (1 << winNum
));
400 { /* Overlap detected */
401 mvOsPrintf("mvEthTargetWinEnable:ERR. Overlap detected\n");
406 { /* Disable address decode target window */
407 MV_REG_BIT_SET(ETH_BASE_ADDR_ENABLE_REG(port
), (1 << winNum
));
412 /*******************************************************************************
413 * mvEthWinTargetGet - Get Window number associated with target
424 *******************************************************************************/
425 MV_U32
mvEthWinTargetGet(int port
, MV_TARGET target
)
427 MV_ETH_DEC_WIN decWin
;
430 /* Check parameters */
431 if (target
>= MAX_TARGETS
)
433 mvOsPrintf("mvAhbToMbusWinTargetGet: target %d is Illigal\n", target
);
437 for (winNum
=0; winNum
<ETH_MAX_DECODE_WIN
; winNum
++)
439 if (mvEthWinGet(port
, winNum
,&decWin
) != MV_OK
)
441 mvOsPrintf("mvAhbToMbusWinTargetGet: window returned error\n");
445 if (decWin
.enable
== MV_TRUE
)
447 if (decWin
.target
== target
)
456 /*******************************************************************************
457 * mvEthProtWinSet - Set access protection of Ethernet to target window.
460 * Each Ethernet port can be configured with access attributes for each
461 * of the Ethenret to target windows (address decode windows). This
462 * function sets access attributes to a given window for the given channel.
465 * ethPort - ETH channel number. See MV_ETH_CHANNEL enumerator.
466 * winNum - IETH to target address decode window number.
467 * access - IETH access rights. See MV_ACCESS_RIGHTS enumerator.
473 * MV_ERROR in case window number is invalid or access right reserved.
475 *******************************************************************************/
476 MV_STATUS
mvEthProtWinSet(MV_U32 portNo
, MV_U32 winNum
, MV_ACCESS_RIGHTS access
)
480 /* Parameter checking */
481 if(portNo
>= mvCtrlEthMaxPortGet())
483 mvOsPrintf("mvEthProtWinSet:ERR. Invalid port number %d\n", portNo
);
487 if (winNum
>= ETH_MAX_DECODE_WIN
)
489 mvOsPrintf("mvEthProtWinSet:ERR. Invalid winNum%d\n",winNum
);
493 if((access
== ACC_RESERVED
) || (access
>= MAX_ACC_RIGHTS
))
495 mvOsPrintf("mvEthProtWinSet:ERR. Inv access param %d\n", access
);
498 /* Read current protection register */
499 protReg
= MV_REG_READ(ETH_ACCESS_PROTECT_REG(portNo
));
501 /* Clear protection window field */
502 protReg
&= ~(ETH_PROT_WIN_MASK(winNum
));
504 /* Set new protection field value */
505 protReg
|= (access
<< (ETH_PROT_WIN_OFFS(winNum
)));
507 /* Write protection register back */
508 MV_REG_WRITE(ETH_ACCESS_PROTECT_REG(portNo
), protReg
);
513 /*******************************************************************************
514 * ethWinOverlapDetect - Detect ETH address windows overlapping
517 * An unpredicted behaviur is expected in case ETH address decode
519 * This function detects ETH address decode windows overlapping of a
520 * specified window. The function does not check the window itself for
521 * overlapping. The function also skipps disabled address decode windows.
524 * winNum - address decode window number.
525 * pAddrDecWin - An address decode window struct.
531 * MV_TRUE if the given address window overlap current address
532 * decode map, MV_FALSE otherwise, MV_ERROR if reading invalid data
535 *******************************************************************************/
536 static MV_STATUS
ethWinOverlapDetect(int port
, MV_U32 winNum
, MV_ADDR_WIN
*pAddrWin
)
538 MV_U32 baseAddrEnableReg
;
540 MV_ETH_DEC_WIN addrDecWin
;
542 /* Read base address enable register. Do not check disabled windows */
543 baseAddrEnableReg
= MV_REG_READ(ETH_BASE_ADDR_ENABLE_REG(port
));
545 for (winNumIndex
=0; winNumIndex
<ETH_MAX_DECODE_WIN
; winNumIndex
++)
547 /* Do not check window itself */
548 if (winNumIndex
== winNum
)
553 /* Do not check disabled windows */
554 if (baseAddrEnableReg
& (1 << winNumIndex
))
559 /* Get window parameters */
560 if (MV_OK
!= mvEthWinGet(port
, winNumIndex
, &addrDecWin
))
562 mvOsPrintf("ethWinOverlapDetect: ERR. TargetWinGet failed\n");
566 mvOsPrintf("ethWinOverlapDetect:\n
567 winNumIndex =%d baseHigh =0x%x baseLow=0x%x size=0x%x enable=0x%x\n",
569 addrDecWin.addrWin.baseHigh,
570 addrDecWin.addrWin.baseLow,
571 addrDecWin.addrWin.size,
574 if (MV_TRUE
== ctrlWinOverlapTest(pAddrWin
, &(addrDecWin
.addrWin
)))
582 /*******************************************************************************
583 * mvEthAddrDecShow - Print the Etherent address decode map.
586 * This function print the Etherent address decode map.
597 *******************************************************************************/
598 void mvEthPortAddrDecShow(int port
)
604 mvOsOutput( "ETH %d:\n", port
);
605 mvOsOutput( "----\n" );
607 for( i
= 0; i
< ETH_MAX_DECODE_WIN
; i
++ )
609 memset( &win
, 0, sizeof(ETH_MAX_DECODE_WIN
) );
611 mvOsOutput( "win%d - ", i
);
613 if( mvEthWinGet(port
, i
, &win
) == MV_OK
)
617 mvOsOutput( "%s base %08x, ",
618 mvCtrlTargetNameGet(win
.target
), win
.addrWin
.baseLow
);
619 mvOsOutput( "...." );
620 mvSizePrint( win
.addrWin
.size
);
625 mvOsOutput( "disable\n" );
631 void mvEthAddrDecShow(void)
635 for(port
=0; port
<mvCtrlEthMaxPortGet(); port
++)
637 if (MV_FALSE
== mvCtrlPwrClckGet(ETH_GIG_UNIT_ID
, port
)) continue;
639 mvEthPortAddrDecShow(port
);
648 /* Power down all existing ports */
649 for(port
=0; port
<mvCtrlEthMaxPortGet(); port
++)
651 if (MV_FALSE
== mvCtrlPwrClckGet(ETH_GIG_UNIT_ID
, port
))
654 mvEthPortPowerUp(port
);