1/******************************************************************************* 2Copyright (C) Marvell International Ltd. and its affiliates 3 4This software file (the "File") is owned and distributed by Marvell 5International Ltd. and/or its affiliates ("Marvell") under the following 6alternative licensing terms. Once you have made an election to distribute the 7File under one of the following license alternatives, please (i) delete this 8introductory statement regarding license alternatives, (ii) delete the two 9license alternatives that you have not elected to use and (iii) preserve the 10Marvell copyright notice above. 11 12******************************************************************************** 13Marvell Commercial License Option 14 15If you received this File from Marvell and you have entered into a commercial 16license agreement (a "Commercial License") with Marvell, the File is licensed 17to you under the terms of the applicable Commercial License. 18 19******************************************************************************** 20Marvell GPL License Option 21 22If you received this File from Marvell, you may opt to use, redistribute and/or 23modify this File in accordance with the terms and conditions of the General 24Public License Version 2, June 1991 (the "GPL License"), a copy of which is 25available along with the File in the license.txt file or by writing to the Free 26Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or 27on the worldwide web at http://www.gnu.org/licenses/gpl.txt. 28 29THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED 30WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY 31DISCLAIMED. The GPL License provides additional details about this warranty 32disclaimer. 33******************************************************************************** 34Marvell BSD License Option 35 36If you received this File from Marvell, you may opt to use, redistribute and/or 37modify this File under the following licensing terms. 38Redistribution and use in source and binary forms, with or without modification, 39are permitted provided that the following conditions are met: 40 41 * Redistributions of source code must retain the above copyright notice, 42 this list of conditions and the following disclaimer. 43 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. 47 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. 51 52THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 53ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 54WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 55DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 56ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 57(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 58LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 59ANY 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 61SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62 63*******************************************************************************/ 64 65#include "ctrlEnv/sys/mvSysUsb.h" 66 67MV_TARGET usbAddrDecPrioTab[] = 68{ 69#if defined(MV_INCLUDE_SDRAM_CS0) 70 SDRAM_CS0, 71#endif 72#if defined(MV_INCLUDE_SDRAM_CS1) 73 SDRAM_CS1, 74#endif 75#if defined(MV_INCLUDE_SDRAM_CS2) 76 SDRAM_CS2, 77#endif 78#if defined(MV_INCLUDE_SDRAM_CS3) 79 SDRAM_CS3, 80#endif 81#if defined(MV_INCLUDE_CESA) && defined(USB_UNDERRUN_WA) 82 CRYPT_ENG, 83#endif 84#if defined(MV_INCLUDE_PEX) 85 PEX0_MEM, 86#endif 87 TBL_TERM 88}; 89 90 91 92MV_STATUS mvUsbInit(int dev, MV_BOOL isHost) 93{ 94 MV_STATUS status; 95 96 status = mvUsbWinInit(dev); 97 if(status != MV_OK) 98 return status; 99 100 return mvUsbHalInit(dev, isHost); 101} 102 103 104/******************************************************************************* 105* usbWinOverlapDetect - Detect USB address windows overlapping 106* 107* DESCRIPTION: 108* An unpredicted behaviur is expected in case USB address decode 109* windows overlapps. 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. 113* 114* INPUT: 115* winNum - address decode window number. 116* pAddrDecWin - An address decode window struct. 117* 118* OUTPUT: 119* None. 120* 121* RETURN: 122* MV_TRUE if the given address window overlap current address 123* decode map, MV_FALSE otherwise, MV_ERROR if reading invalid data 124* from registers. 125* 126*******************************************************************************/ 127static MV_STATUS usbWinOverlapDetect(int dev, MV_U32 winNum, 128 MV_ADDR_WIN *pAddrWin) 129{ 130 MV_U32 winNumIndex; 131 MV_DEC_WIN addrDecWin; 132 133 for(winNumIndex=0; winNumIndex<MV_USB_MAX_ADDR_DECODE_WIN; winNumIndex++) 134 { 135 /* Do not check window itself */ 136 if (winNumIndex == winNum) 137 { 138 continue; 139 } 140 141 /* Get window parameters */ 142 if (MV_OK != mvUsbWinGet(dev, winNumIndex, &addrDecWin)) 143 { 144 mvOsPrintf("%s: ERR. TargetWinGet failed\n", __FUNCTION__); 145 return MV_ERROR; 146 } 147 148 /* Do not check disabled windows */ 149 if(addrDecWin.enable == MV_FALSE) 150 { 151 continue; 152 } 153 154 if (MV_TRUE == ctrlWinOverlapTest(pAddrWin, &(addrDecWin.addrWin))) 155 { 156 return MV_TRUE; 157 } 158 } 159 return MV_FALSE; 160} 161 162/******************************************************************************* 163* mvUsbWinSet - Set USB target address window 164* 165* DESCRIPTION: 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. 170* 171* INPUT: 172* winNum - USB target address decode window number. 173* pAddrDecWin - USB target window data structure. 174* 175* OUTPUT: 176* None. 177* 178* RETURN: 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 181* unknown. 182* 183*******************************************************************************/ 184MV_STATUS mvUsbWinSet(int dev, MV_U32 winNum, MV_DEC_WIN *pDecWin) 185{ 186 MV_DEC_WIN_PARAMS winParams; 187 MV_U32 sizeReg, baseReg; 188 189 /* Parameter checking */ 190 if (winNum >= MV_USB_MAX_ADDR_DECODE_WIN) 191 { 192 mvOsPrintf("%s: ERR. Invalid win num %d\n",__FUNCTION__, winNum); 193 return MV_BAD_PARAM; 194 } 195 196 /* Check if the requested window overlapps with current windows */ 197 if (MV_TRUE == usbWinOverlapDetect(dev, winNum, &pDecWin->addrWin)) 198 { 199 mvOsPrintf("%s: ERR. Window %d overlap\n", __FUNCTION__, winNum); 200 return MV_ERROR; 201 } 202 203 /* check if address is aligned to the size */ 204 if(MV_IS_NOT_ALIGN(pDecWin->addrWin.baseLow, pDecWin->addrWin.size)) 205 { 206 mvOsPrintf("mvUsbWinSet:Error setting USB window %d to "\ 207 "target %s.\nAddress 0x%08x is unaligned to size 0x%x.\n", 208 winNum, 209 mvCtrlTargetNameGet(pDecWin->target), 210 pDecWin->addrWin.baseLow, 211 pDecWin->addrWin.size); 212 return MV_ERROR; 213 } 214 215 if(MV_OK != mvCtrlAddrDecToParams(pDecWin, &winParams)) 216 { 217 mvOsPrintf("%s: mvCtrlAddrDecToParams Failed\n", __FUNCTION__); 218 return MV_ERROR; 219 } 220 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)); 225 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; 229 230 if((MV_TARGET_IS_DRAM(pDecWin->target)) && 231 (pDecWin->addrWinAttr.cachePolicy != NO_COHERENCY)) 232 { 233 sizeReg |= MV_USB_WIN_BURST_WR_32BIT_LIMIT; 234 } 235 else 236 { 237 sizeReg |= MV_USB_WIN_BURST_WR_NO_LIMIT; 238 } 239#endif /* MV645xx || MV646xx */ 240 241 if (pDecWin->enable == MV_TRUE) 242 { 243 sizeReg |= MV_USB_WIN_ENABLE_MASK; 244 } 245 else 246 { 247 sizeReg &= ~MV_USB_WIN_ENABLE_MASK; 248 } 249 250 /* Update Base value */ 251 baseReg = (winParams.baseAddr & MV_USB_WIN_BASE_MASK); 252 253 MV_REG_WRITE( MV_USB_WIN_CTRL_REG(dev, winNum), sizeReg); 254 MV_REG_WRITE( MV_USB_WIN_BASE_REG(dev, winNum), baseReg); 255 256 return MV_OK; 257} 258 259/******************************************************************************* 260* mvUsbWinGet - Get USB peripheral target address window. 261* 262* DESCRIPTION: 263* Get USB peripheral target address window. 264* 265* INPUT: 266* winNum - USB target address decode window number. 267* 268* OUTPUT: 269* pDecWin - USB target window data structure. 270* 271* RETURN: 272* MV_ERROR if register parameters are invalid. 273* 274*******************************************************************************/ 275MV_STATUS mvUsbWinGet(int dev, MV_U32 winNum, MV_DEC_WIN *pDecWin) 276{ 277 MV_DEC_WIN_PARAMS winParam; 278 MV_U32 sizeReg, baseReg; 279 280 /* Parameter checking */ 281 if (winNum >= MV_USB_MAX_ADDR_DECODE_WIN) 282 { 283 mvOsPrintf("%s (dev=%d): ERR. Invalid winNum %d\n", 284 __FUNCTION__, dev, winNum); 285 return MV_NOT_SUPPORTED; 286 } 287 288 baseReg = MV_REG_READ( MV_USB_WIN_BASE_REG(dev, winNum) ); 289 sizeReg = MV_REG_READ( MV_USB_WIN_CTRL_REG(dev, winNum) ); 290 291 /* Check if window is enabled */ 292 if(sizeReg & MV_USB_WIN_ENABLE_MASK) 293 { 294 pDecWin->enable = MV_TRUE; 295 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); 301 302 /* Translate the decode window parameters to address decode struct */ 303 if (MV_OK != mvCtrlParamsToAddrDec(&winParam, pDecWin)) 304 { 305 mvOsPrintf("Failed to translate register parameters to USB address" \ 306 " decode window structure\n"); 307 return MV_ERROR; 308 } 309 } 310 else 311 { 312 pDecWin->enable = MV_FALSE; 313 } 314 return MV_OK; 315} 316 317/******************************************************************************* 318* mvUsbWinInit - 319* 320* INPUT: 321* 322* OUTPUT: 323* 324* RETURN: 325* MV_ERROR if register parameters are invalid. 326* 327*******************************************************************************/ 328MV_STATUS mvUsbWinInit(int dev) 329{ 330 MV_STATUS status; 331 MV_DEC_WIN usbWin; 332 MV_CPU_DEC_WIN cpuAddrDecWin; 333 int winNum; 334 MV_U32 winPrioIndex = 0; 335 336 /* First disable all address decode windows */ 337 for(winNum = 0; winNum < MV_USB_MAX_ADDR_DECODE_WIN; winNum++) 338 { 339 MV_REG_BIT_RESET(MV_USB_WIN_CTRL_REG(dev, winNum), MV_USB_WIN_ENABLE_MASK); 340 } 341 342 /* Go through all windows in user table until table terminator */ 343 winNum = 0; 344 while( (usbAddrDecPrioTab[winPrioIndex] != TBL_TERM) && 345 (winNum < MV_USB_MAX_ADDR_DECODE_WIN) ) 346 { 347 /* first get attributes from CPU If */ 348 status = mvCpuIfTargetWinGet(usbAddrDecPrioTab[winPrioIndex], 349 &cpuAddrDecWin); 350 351 if(MV_NO_SUCH == status) 352 { 353 winPrioIndex++; 354 continue; 355 } 356 if (MV_OK != status) 357 { 358 mvOsPrintf("%s: ERR. mvCpuIfTargetWinGet failed\n", __FUNCTION__); 359 return MV_ERROR; 360 } 361 362 if (cpuAddrDecWin.enable == MV_TRUE) 363 { 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]; 369 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 */ 374 375 if(MV_OK != mvUsbWinSet(dev, winNum, &usbWin)) 376 { 377 return MV_ERROR; 378 } 379 winNum++; 380 } 381 winPrioIndex++; 382 } 383 return MV_OK; 384} 385 386/******************************************************************************* 387* mvUsbAddrDecShow - Print the USB address decode map. 388* 389* DESCRIPTION: 390* This function print the USB address decode map. 391* 392* INPUT: 393* None. 394* 395* OUTPUT: 396* None. 397* 398* RETURN: 399* None. 400* 401*******************************************************************************/ 402MV_VOID mvUsbAddrDecShow(MV_VOID) 403{ 404 MV_DEC_WIN addrDecWin; 405 int i, winNum; 406 407 mvOsOutput( "\n" ); 408 mvOsOutput( "USB:\n" ); 409 mvOsOutput( "----\n" ); 410 411 for(i=0; i<mvCtrlUsbMaxGet(); i++) 412 { 413 mvOsOutput( "Device %d:\n", i); 414 415 for(winNum = 0; winNum < MV_USB_MAX_ADDR_DECODE_WIN; winNum++) 416 { 417 memset(&addrDecWin, 0, sizeof(MV_DEC_WIN) ); 418 419 mvOsOutput( "win%d - ", winNum ); 420 421 if( mvUsbWinGet(i, winNum, &addrDecWin ) == MV_OK ) 422 { 423 if( addrDecWin.enable ) 424 { 425 mvOsOutput( "%s base %08x, ", 426 mvCtrlTargetNameGet(addrDecWin.target), addrDecWin.addrWin.baseLow ); 427 428 mvSizePrint( addrDecWin.addrWin.size ); 429 430#if defined(MV645xx) || defined(MV646xx) 431 switch( addrDecWin.addrWinAttr.swapType) 432 { 433 case MV_BYTE_SWAP: 434 mvOsOutput( "BYTE_SWAP, " ); 435 break; 436 case MV_NO_SWAP: 437 mvOsOutput( "NO_SWAP , " ); 438 break; 439 case MV_BYTE_WORD_SWAP: 440 mvOsOutput( "BYTE_WORD_SWAP, " ); 441 break; 442 case MV_WORD_SWAP: 443 mvOsOutput( "WORD_SWAP, " ); 444 break; 445 default: 446 mvOsOutput( "SWAP N/A , " ); 447 } 448 449 switch( addrDecWin.addrWinAttr.cachePolicy ) 450 { 451 case NO_COHERENCY: 452 mvOsOutput( "NO_COHERENCY , " ); 453 break; 454 case WT_COHERENCY: 455 mvOsOutput( "WT_COHERENCY , " ); 456 break; 457 case WB_COHERENCY: 458 mvOsOutput( "WB_COHERENCY , " ); 459 break; 460 default: 461 mvOsOutput( "COHERENCY N/A, " ); 462 } 463 464 switch( addrDecWin.addrWinAttr.pcixNoSnoop ) 465 { 466 case 0: 467 mvOsOutput( "PCI-X NS inactive, " ); 468 break; 469 case 1: 470 mvOsOutput( "PCI-X NS active , " ); 471 break; 472 default: 473 mvOsOutput( "PCI-X NS N/A , " ); 474 } 475 476 switch( addrDecWin.addrWinAttr.p2pReq64 ) 477 { 478 case 0: 479 mvOsOutput( "REQ64 force" ); 480 break; 481 case 1: 482 mvOsOutput( "REQ64 detect" ); 483 break; 484 default: 485 mvOsOutput( "REQ64 N/A" ); 486 } 487#endif /* MV645xx || MV646xx */ 488 mvOsOutput( "\n" ); 489 } 490 else 491 mvOsOutput( "disable\n" ); 492 } 493 } 494 } 495} 496 497 498