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 *******************************************************************************/
65 #include "ctrlEnv/sys/mvSysUsb.h"
67 MV_TARGET usbAddrDecPrioTab[] =
69 #if defined(MV_INCLUDE_SDRAM_CS0)
72 #if defined(MV_INCLUDE_SDRAM_CS1)
75 #if defined(MV_INCLUDE_SDRAM_CS2)
78 #if defined(MV_INCLUDE_SDRAM_CS3)
81 #if defined(MV_INCLUDE_CESA) && defined(USB_UNDERRUN_WA)
84 #if defined(MV_INCLUDE_PEX)
92 MV_STATUS mvUsbInit(int dev, MV_BOOL isHost)
96 status = mvUsbWinInit(dev);
100 return mvUsbHalInit(dev, isHost);
104 /*******************************************************************************
105 * usbWinOverlapDetect - Detect USB address windows overlapping
108 * An unpredicted behaviur is expected in case USB address decode
110 * This function detects USB address decode windows overlapping of a
111 * specified window. The function does not check the window itself for
112 * overlapping. The function also skipps disabled address decode windows.
115 * winNum - address decode window number.
116 * pAddrDecWin - An address decode window struct.
122 * MV_TRUE if the given address window overlap current address
123 * decode map, MV_FALSE otherwise, MV_ERROR if reading invalid data
126 *******************************************************************************/
127 static MV_STATUS usbWinOverlapDetect(int dev, MV_U32 winNum,
128 MV_ADDR_WIN *pAddrWin)
131 MV_DEC_WIN addrDecWin;
133 for(winNumIndex=0; winNumIndex<MV_USB_MAX_ADDR_DECODE_WIN; winNumIndex++)
135 /* Do not check window itself */
136 if (winNumIndex == winNum)
141 /* Get window parameters */
142 if (MV_OK != mvUsbWinGet(dev, winNumIndex, &addrDecWin))
144 mvOsPrintf("%s: ERR. TargetWinGet failed\n", __FUNCTION__);
148 /* Do not check disabled windows */
149 if(addrDecWin.enable == MV_FALSE)
154 if (MV_TRUE == ctrlWinOverlapTest(pAddrWin, &(addrDecWin.addrWin)))
162 /*******************************************************************************
163 * mvUsbWinSet - Set USB target address window
166 * This function sets a peripheral target (e.g. SDRAM bank0, PCI_MEM0)
167 * address window, also known as address decode window.
168 * After setting this target window, the USB will be able to access the
169 * target within the address window.
172 * winNum - USB target address decode window number.
173 * pAddrDecWin - USB target window data structure.
179 * MV_ERROR if address window overlapps with other address decode windows.
180 * MV_BAD_PARAM if base address is invalid parameter or target is
183 *******************************************************************************/
184 MV_STATUS mvUsbWinSet(int dev, MV_U32 winNum, MV_DEC_WIN *pDecWin)
186 MV_DEC_WIN_PARAMS winParams;
187 MV_U32 sizeReg, baseReg;
189 /* Parameter checking */
190 if (winNum >= MV_USB_MAX_ADDR_DECODE_WIN)
192 mvOsPrintf("%s: ERR. Invalid win num %d\n",__FUNCTION__, winNum);
196 /* Check if the requested window overlapps with current windows */
197 if (MV_TRUE == usbWinOverlapDetect(dev, winNum, &pDecWin->addrWin))
199 mvOsPrintf("%s: ERR. Window %d overlap\n", __FUNCTION__, winNum);
203 /* check if address is aligned to the size */
204 if(MV_IS_NOT_ALIGN(pDecWin->addrWin.baseLow, pDecWin->addrWin.size))
206 mvOsPrintf("mvUsbWinSet:Error setting USB window %d to "\
207 "target %s.\nAddress 0x%08x is unaligned to size 0x%x.\n",
209 mvCtrlTargetNameGet(pDecWin->target),
210 pDecWin->addrWin.baseLow,
211 pDecWin->addrWin.size);
215 if(MV_OK != mvCtrlAddrDecToParams(pDecWin, &winParams))
217 mvOsPrintf("%s: mvCtrlAddrDecToParams Failed\n", __FUNCTION__);
221 /* set Size, Attributes and TargetID */
222 sizeReg = (((winParams.targetId << MV_USB_WIN_TARGET_OFFSET) & MV_USB_WIN_TARGET_MASK) |
223 ((winParams.attrib << MV_USB_WIN_ATTR_OFFSET) & MV_USB_WIN_ATTR_MASK) |
224 ((winParams.size << MV_USB_WIN_SIZE_OFFSET) & MV_USB_WIN_SIZE_MASK));
226 #if defined(MV645xx) || defined(MV646xx)
227 /* If window is DRAM with HW cache coherency, make sure bit2 is set */
228 sizeReg &= ~MV_USB_WIN_BURST_WR_LIMIT_MASK;
230 if((MV_TARGET_IS_DRAM(pDecWin->target)) &&
231 (pDecWin->addrWinAttr.cachePolicy != NO_COHERENCY))
233 sizeReg |= MV_USB_WIN_BURST_WR_32BIT_LIMIT;
237 sizeReg |= MV_USB_WIN_BURST_WR_NO_LIMIT;
239 #endif /* MV645xx || MV646xx */
241 if (pDecWin->enable == MV_TRUE)
243 sizeReg |= MV_USB_WIN_ENABLE_MASK;
247 sizeReg &= ~MV_USB_WIN_ENABLE_MASK;
250 /* Update Base value */
251 baseReg = (winParams.baseAddr & MV_USB_WIN_BASE_MASK);
253 MV_REG_WRITE( MV_USB_WIN_CTRL_REG(dev, winNum), sizeReg);
254 MV_REG_WRITE( MV_USB_WIN_BASE_REG(dev, winNum), baseReg);
259 /*******************************************************************************
260 * mvUsbWinGet - Get USB peripheral target address window.
263 * Get USB peripheral target address window.
266 * winNum - USB target address decode window number.
269 * pDecWin - USB target window data structure.
272 * MV_ERROR if register parameters are invalid.
274 *******************************************************************************/
275 MV_STATUS mvUsbWinGet(int dev, MV_U32 winNum, MV_DEC_WIN *pDecWin)
277 MV_DEC_WIN_PARAMS winParam;
278 MV_U32 sizeReg, baseReg;
280 /* Parameter checking */
281 if (winNum >= MV_USB_MAX_ADDR_DECODE_WIN)
283 mvOsPrintf("%s (dev=%d): ERR. Invalid winNum %d\n",
284 __FUNCTION__, dev, winNum);
285 return MV_NOT_SUPPORTED;
288 baseReg = MV_REG_READ( MV_USB_WIN_BASE_REG(dev, winNum) );
289 sizeReg = MV_REG_READ( MV_USB_WIN_CTRL_REG(dev, winNum) );
291 /* Check if window is enabled */
292 if(sizeReg & MV_USB_WIN_ENABLE_MASK)
294 pDecWin->enable = MV_TRUE;
296 /* Extract window parameters from registers */
297 winParam.targetId = (sizeReg & MV_USB_WIN_TARGET_MASK) >> MV_USB_WIN_TARGET_OFFSET;
298 winParam.attrib = (sizeReg & MV_USB_WIN_ATTR_MASK) >> MV_USB_WIN_ATTR_OFFSET;
299 winParam.size = (sizeReg & MV_USB_WIN_SIZE_MASK) >> MV_USB_WIN_SIZE_OFFSET;
300 winParam.baseAddr = (baseReg & MV_USB_WIN_BASE_MASK);
302 /* Translate the decode window parameters to address decode struct */
303 if (MV_OK != mvCtrlParamsToAddrDec(&winParam, pDecWin))
305 mvOsPrintf("Failed to translate register parameters to USB address" \
306 " decode window structure\n");
312 pDecWin->enable = MV_FALSE;
317 /*******************************************************************************
325 * MV_ERROR if register parameters are invalid.
327 *******************************************************************************/
328 MV_STATUS mvUsbWinInit(int dev)
332 MV_CPU_DEC_WIN cpuAddrDecWin;
334 MV_U32 winPrioIndex = 0;
336 /* First disable all address decode windows */
337 for(winNum = 0; winNum < MV_USB_MAX_ADDR_DECODE_WIN; winNum++)
339 MV_REG_BIT_RESET(MV_USB_WIN_CTRL_REG(dev, winNum), MV_USB_WIN_ENABLE_MASK);
342 /* Go through all windows in user table until table terminator */
344 while( (usbAddrDecPrioTab[winPrioIndex] != TBL_TERM) &&
345 (winNum < MV_USB_MAX_ADDR_DECODE_WIN) )
347 /* first get attributes from CPU If */
348 status = mvCpuIfTargetWinGet(usbAddrDecPrioTab[winPrioIndex],
351 if(MV_NO_SUCH == status)
358 mvOsPrintf("%s: ERR. mvCpuIfTargetWinGet failed\n", __FUNCTION__);
362 if (cpuAddrDecWin.enable == MV_TRUE)
364 usbWin.addrWin.baseHigh = cpuAddrDecWin.addrWin.baseHigh;
365 usbWin.addrWin.baseLow = cpuAddrDecWin.addrWin.baseLow;
366 usbWin.addrWin.size = cpuAddrDecWin.addrWin.size;
367 usbWin.enable = MV_TRUE;
368 usbWin.target = usbAddrDecPrioTab[winPrioIndex];
370 #if defined(MV645xx) || defined(MV646xx)
371 /* Get the default attributes for that target window */
372 mvCtrlDefAttribGet(usbWin.target, &usbWin.addrWinAttr);
373 #endif /* MV645xx || MV646xx */
375 if(MV_OK != mvUsbWinSet(dev, winNum, &usbWin))
386 /*******************************************************************************
387 * mvUsbAddrDecShow - Print the USB address decode map.
390 * This function print the USB address decode map.
401 *******************************************************************************/
402 MV_VOID mvUsbAddrDecShow(MV_VOID)
404 MV_DEC_WIN addrDecWin;
408 mvOsOutput( "USB:\n" );
409 mvOsOutput( "----\n" );
411 for(i=0; i<mvCtrlUsbMaxGet(); i++)
413 mvOsOutput( "Device %d:\n", i);
415 for(winNum = 0; winNum < MV_USB_MAX_ADDR_DECODE_WIN; winNum++)
417 memset(&addrDecWin, 0, sizeof(MV_DEC_WIN) );
419 mvOsOutput( "win%d - ", winNum );
421 if( mvUsbWinGet(i, winNum, &addrDecWin ) == MV_OK )
423 if( addrDecWin.enable )
425 mvOsOutput( "%s base %08x, ",
426 mvCtrlTargetNameGet(addrDecWin.target), addrDecWin.addrWin.baseLow );
428 mvSizePrint( addrDecWin.addrWin.size );
430 #if defined(MV645xx) || defined(MV646xx)
431 switch( addrDecWin.addrWinAttr.swapType)
434 mvOsOutput( "BYTE_SWAP, " );
437 mvOsOutput( "NO_SWAP , " );
439 case MV_BYTE_WORD_SWAP:
440 mvOsOutput( "BYTE_WORD_SWAP, " );
443 mvOsOutput( "WORD_SWAP, " );
446 mvOsOutput( "SWAP N/A , " );
449 switch( addrDecWin.addrWinAttr.cachePolicy )
452 mvOsOutput( "NO_COHERENCY , " );
455 mvOsOutput( "WT_COHERENCY , " );
458 mvOsOutput( "WB_COHERENCY , " );
461 mvOsOutput( "COHERENCY N/A, " );
464 switch( addrDecWin.addrWinAttr.pcixNoSnoop )
467 mvOsOutput( "PCI-X NS inactive, " );
470 mvOsOutput( "PCI-X NS active , " );
473 mvOsOutput( "PCI-X NS N/A , " );
476 switch( addrDecWin.addrWinAttr.p2pReq64 )
479 mvOsOutput( "REQ64 force" );
482 mvOsOutput( "REQ64 detect" );
485 mvOsOutput( "REQ64 N/A" );
487 #endif /* MV645xx || MV646xx */
491 mvOsOutput( "disable\n" );