• Zynq-Linux移植学习笔记之66- 国产ZYNQ通过裕太PHY8521连接国产交换芯片


    1、背景介绍

    ZYNQ通过裕太PHY 8521主要连接两种国产交换芯片,一种为盛科的CTC8096,另一种为32所的JEM5396。框图示意如下:

    2、硬件状态确认

    首先检查phy的模式,确认为SGMII_MAC-RGMII_PHY

    可通过读出A001寄存器确认状态

    读出来应该是0x8155

    这样确保状态对

    3、PHY软件配置

    PHY软件需要配置为强制千兆

    ./phyreg.elf eth1 0x0 0x340

    设置phy的tx_delay

    一般设置a003寄存器为0xf8或者0xfa

    Phyreg代码如下

    1. /*
    2. ============================================================================
    3. Name : main.c
    4. Author : 123
    5. Version :
    6. Copyright : Your copyright notice
    7. Description : Hello World in C
    8. ============================================================================
    9. */
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include
    17. #include
    18. #include
    19. #include
    20. #include
    21. #define reteck(ret) \
    22. if(ret < 0){ \
    23. printf("%m! \"%s\" : line: %d\n", __func__, __LINE__); \
    24. goto lab; \
    25. }
    26. #define help() \
    27. printf("mdio:\n"); \
    28. printf("read operation: mdio reg_addr\n"); \
    29. printf("write operation: mdio reg_addr value\n"); \
    30. printf("For example:\n"); \
    31. printf("mdio eth0 1\n"); \
    32. printf("mdio eth0 0 0x12\n\n"); \
    33. exit(0);
    34. int sockfd;
    35. int main(int argc, char *argv[]) {
    36. if (argc == 1 || !strcmp(argv[1], "-h")) {
    37. help()
    38. ;
    39. }
    40. struct mii_ioctl_data *mii = NULL;
    41. struct ifreq ifr;
    42. int ret;
    43. memset(&ifr, 0, sizeof(ifr));
    44. strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
    45. sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
    46. reteck(sockfd);
    47. //get phy address in smi bus
    48. ret = ioctl(sockfd, SIOCGMIIPHY, &ifr);
    49. reteck(ret);
    50. mii = (struct mii_ioctl_data*) &ifr.ifr_data;
    51. if (argc == 3) {
    52. mii->reg_num = (uint16_t) strtoul(argv[2], NULL, 0);
    53. ret = ioctl(sockfd, SIOCGMIIREG, &ifr);
    54. reteck(ret);
    55. printf("read phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id,
    56. mii->reg_num, mii->val_out);
    57. } else if (argc == 4) {
    58. mii->reg_num = (uint16_t) strtoul(argv[2], NULL, 0);
    59. mii->val_in = (uint16_t) strtoul(argv[3], NULL, 0);
    60. ret = ioctl(sockfd, SIOCSMIIREG, &ifr);
    61. reteck(ret);
    62. printf("write phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id,
    63. mii->reg_num, mii->val_in);
    64. }
    65. lab: close(sockfd);
    66. return 0;
    67. }

    4、交换芯片端配置

    交换芯片端与PHY相连的端口需要配置强制千兆模式

    针对CTC8096(一般SDK应用内配置

    ctc8096端将对应端口强制千兆

    1. port 0x0007 auto-neg disable
    2. port 0x0007 property auto-neg-mode sgmii-slaver

    针对JEM5396(一般UBOOT阶段配置

    1. printf("Set Port 5 CONNECT TO ZYNQ SPEED 1000M\n");
    2. workBuf[0] = 0x8b;
    3. workBuf[1] = 0x0;
    4. writeBCM5396_2(&spi, 0, 0x65, workBuf);  
    5. workBuf[0] = 0x40;
    6. workBuf[1] = 0x1;  
    7. Status = writeBCM5396(&spi, 0x15, 0x0, workBuf );  

    JEM5396配置的详细代码如下:

    1. // SPDX-License-Identifier: GPL-2.0+
    2. /*
    3. * (C) Copyright 2001-2015
    4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
    5. * Joe Hershberger, National Instruments
    6. */
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #define XGpioPs_ReadReg(BaseAddr, RegOffset) \
    14. (*(volatile u32 * )((BaseAddr) + (RegOffset)))
    15. #define XGpioPs_WriteReg(BaseAddr, RegOffset, Data) \
    16. *(volatile u32 *) ((BaseAddr) + RegOffset) = Data
    17. #define AD9520READ (0x80)
    18. #define AD9520WRITE (0x00)
    19. /*-----------------------国产ZYNQ GPIO 通用代码--------------------------------------------*/
    20. #define XGPIOPS_DATA_LSW_OFFSET 0x00000000U /* Mask and Data Register LSW, WO */
    21. #define XGPIOPS_DATA_MSW_OFFSET 0x00000004U /* Mask and Data Register MSW, WO */
    22. #define XGPIOPS_DATA_OFFSET 0x00000040U /* Data Register, RW */
    23. #define XGPIOPS_DATA_RO_OFFSET 0x00000060U /* Data Register - Input, RO */
    24. #define XGPIOPS_DIRM_OFFSET 0x00000004U //0x00000204U /* Direction Mode Register, RW */
    25. #define XGPIOPS_OUTEN_OFFSET 0x00000208U /* Output Enable Register, RW */
    26. #define XGPIOPS_INTMASK_OFFSET 0x0000020CU /* Interrupt Mask Register, RO */
    27. #define XGPIOPS_INTEN_OFFSET 0x00000210U /* Interrupt Enable Register, WO */
    28. #define XGPIOPS_INTDIS_OFFSET 0x00000214U /* Interrupt Disable Register, WO*/
    29. #define XGPIOPS_INTSTS_OFFSET 0x00000218U /* Interrupt Status Register, RO */
    30. #define XGPIOPS_INTTYPE_OFFSET 0x0000021CU /* Interrupt Type Register, RW */
    31. #define XGPIOPS_INTPOL_OFFSET 0x00000220U /* Interrupt Polarity Register, RW */
    32. #define XGPIOPS_INTANY_OFFSET 0x00000224U /* Interrupt On Any Register, RW */
    33. #define XGPIOPS_DATA_MASK_OFFSET 0x00000008U /* Data/Mask Registers offset */
    34. #define XGPIOPS_DATA_BANK_OFFSET 0x00000004U /* Data Registers offset */
    35. #define XGPIOPS_REG_MASK_OFFSET 0x00000100U /* Registers offset */ //0x00000040U
    36. /* For backwards compatibility */
    37. #define XGPIOPS_BYPM_MASK_OFFSET (u32)0x40
    38. #define XGPIOPS_INTTYPE_BANK0_RESET 0xFFFFFFFFU
    39. #define XGPIOPS_INTTYPE_BANK1_RESET 0x3FFFFFFFU
    40. #define XGPIOPS_INTTYPE_BANK2_RESET 0xFFFFFFFFU
    41. #define XGPIOPS_INTTYPE_BANK3_RESET 0xFFFFFFFFU
    42. #define XGPIOPS_IRQ_TYPE_EDGE_RISING 0x00U /**< Interrupt on Rising edge */
    43. #define XGPIOPS_IRQ_TYPE_EDGE_FALLING 0x01U /**< Interrupt Falling edge */
    44. #define XGPIOPS_IRQ_TYPE_EDGE_BOTH 0x02U /**< Interrupt on both edges */
    45. #define XGPIOPS_IRQ_TYPE_LEVEL_HIGH 0x03U /**< Interrupt on high level */
    46. #define XGPIOPS_IRQ_TYPE_LEVEL_LOW 0x04U /**< Interrupt on low level */
    47. /*@}*/
    48. #define XGPIOPS_BANK0 0x00U /**< GPIO Bank 0 */
    49. #define XGPIOPS_BANK1 0x01U /**< GPIO Bank 1 */
    50. #define XGPIOPS_BANK2 0x02U /**< GPIO Bank 2 */
    51. #define XGPIOPS_BANK3 0x03U /**< GPIO Bank 3 */
    52. #define XGPIOPS_MAX_BANKS 0x04U /**< Max banks in a GPIO device */
    53. #define XGPIOPS_BANK_MAX_PINS (u32)32 /**< Max pins in a GPIO bank */
    54. #define XGPIOPS_DEVICE_MAX_PIN_NUM (u32)118 /*< Max pins in the GPIO device*/
    55. #define LED_DELAY 10000000
    56. #define FLASH_RESET_GPIO 0x41220000
    57. #define FLASH_RST_PERIOD 600000
    58. #define FIFO_CFG_GPIO 0x41210000
    59. #define FIFO_DONE_GPIO 0x41220000
    60. #define UBOOT_CFG_GPIO 0x41250000
    61. #define GGF_REST_GPIO 0x41260000
    62. #define DBFGKJ_REST_GPIO 0x412A0000
    63. #define FIRST_V_GPIO 0x41260000
    64. #define SECOND_V_GPIO 0x41270000
    65. #define THIRD_V_GPIO 0x41280000
    66. #define FORTH_V_GPIO 0x41290000
    67. typedef void (*XGpioPs_Handler) (void *CallBackRef, u32 Bank, u32 Status);
    68. typedef struct {
    69. u16 DeviceId; /**< Unique ID of device */
    70. u32 BaseAddr; /**< Register base address */
    71. } XGpioPs_Config;
    72. typedef struct {
    73. XGpioPs_Config GpioConfig; /**< Device configuration */
    74. u32 IsReady; /**< Device is initialized and ready */
    75. XGpioPs_Handler Handler; /**< Status handlers for all banks */
    76. void *CallBackRef; /**< Callback ref for bank handlers */
    77. } XGpioPs;
    78. XGpioPs_Config XGpioPs_ConfigTable[] =
    79. {
    80. {
    81. 0,
    82. 0xE0003000
    83. }
    84. };
    85. XGpioPs_Config *g_GpioConfig;
    86. void XGpioPs_GetBankPin(u8 PinNumber, u8 *BankNumber, u8 *PinNumberInBank)
    87. {
    88. /*
    89. * This structure defines the mapping of the pin numbers to the banks when
    90. * the driver APIs are used for working on the individual pins.
    91. */
    92. u32 XGpioPsPinTable[] = {
    93. (u32)31, /* 0 - 31, Bank 0 */
    94. (u32)63, /* 32 - 53, Bank 1 */
    95. (u32)95, /* 54 - 85, Bank 2 */
    96. (u32)127 /* 86 - 117 Bank 3 */
    97. };
    98. *BankNumber = 0U;
    99. while (*BankNumber < 4U) {
    100. if (PinNumber <= XGpioPsPinTable[*BankNumber]) {
    101. break;
    102. }
    103. (*BankNumber)++;
    104. }
    105. if (*BankNumber == (u8)0) {
    106. *PinNumberInBank = PinNumber;
    107. } else {
    108. *PinNumberInBank = (u8)((u32)PinNumber %
    109. (XGpioPsPinTable[*BankNumber - (u8)1] + (u32)1));
    110. }
    111. }
    112. void XGpioPs_SetDirectionPin(XGpioPs *InstancePtr, u32 Pin, u32 Direction)
    113. {
    114. u8 Bank;
    115. u8 PinNumber;
    116. u32 DirModeReg;
    117. /*
    118. * Get the Bank number and Pin number within the bank.
    119. */
    120. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
    121. DirModeReg = XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
    122. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    123. XGPIOPS_DIRM_OFFSET);
    124. // DirModeReg = XGpioPs_ReadReg(0xE000A244,0);
    125. if (Direction!=(u32)0) { /* input Direction */
    126. DirModeReg |= ((u32)1 << (u32)PinNumber);
    127. } else { /* output Direction */
    128. DirModeReg &= ~ ((u32)1 << (u32)PinNumber);
    129. }
    130. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    131. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    132. XGPIOPS_DIRM_OFFSET, DirModeReg);
    133. // XGpioPs_WriteReg(0xE000A244, 0,DirModeReg);
    134. }
    135. void XGpioPs_SetOutputEnablePin(XGpioPs *InstancePtr, u32 Pin, u32 OpEnable)
    136. {
    137. u8 Bank;
    138. u8 PinNumber;
    139. u32 OpEnableReg;
    140. /*
    141. * Get the Bank number and Pin number within the bank.
    142. */
    143. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
    144. OpEnableReg = XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
    145. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    146. XGPIOPS_OUTEN_OFFSET);
    147. // OpEnableReg = XGpioPs_ReadReg(0xE000A248,0);
    148. if (OpEnable != (u32)0) { /* Enable Output Enable */
    149. OpEnableReg |= ((u32)1 << (u32)PinNumber);
    150. } else { /* Disable Output Enable */
    151. OpEnableReg &= ~ ((u32)1 << (u32)PinNumber);
    152. }
    153. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    154. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    155. XGPIOPS_OUTEN_OFFSET, OpEnableReg);
    156. // XGpioPs_WriteReg(0xE000A248,0, OpEnableReg);
    157. }
    158. void XGpioPs_WritePin(XGpioPs *InstancePtr, u32 Pin, u32 Data)
    159. {
    160. u32 Value;
    161. u8 Bank;
    162. u8 PinNumber;
    163. u32 DataVar = Data;
    164. /*
    165. * Get the Bank number and Pin number within the bank.
    166. */
    167. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
    168. //
    169. // if (PinNumber > 15U) {
    170. /*
    171. * There are only 16 data bits in bit maskable register.
    172. */
    173. // PinNumber -= (u8)16;
    174. // RegOffset = XGPIOPS_DATA_MSW_OFFSET;
    175. // } else {
    176. // RegOffset = XGPIOPS_DATA_LSW_OFFSET;
    177. // }
    178. /*
    179. * Get the 32 bit value to be written to the Mask/Data register where
    180. * the upper 16 bits is the mask and lower 16 bits is the data.
    181. */
    182. DataVar &= (u32)0x01;
    183. Value = ((DataVar << PinNumber) | 0x00000000U);
    184. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    185. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    186. XGPIOPS_DATA_LSW_OFFSET, Value);
    187. }
    188. u32 XGpioPs_ReadPin(XGpioPs *InstancePtr, u32 Pin)
    189. {
    190. u8 Bank;
    191. u8 PinNumber;
    192. /*
    193. * Get the Bank number and Pin number within the bank.
    194. */
    195. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
    196. return (XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
    197. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    198. XGPIOPS_DATA_LSW_OFFSET) >> (u32)PinNumber) & (u32)1;
    199. }
    200. void mygpio_init(int mioPinNum)
    201. {
    202. XGpioPs Gpio;
    203. g_GpioConfig = &XGpioPs_ConfigTable[0];
    204. //Initialize the GPIO device.
    205. Gpio.IsReady = 0U;
    206. Gpio.GpioConfig.BaseAddr = g_GpioConfig->BaseAddr;
    207. Gpio.GpioConfig.DeviceId = 0;
    208. /*
    209. * Indicate the component is now ready to use.
    210. */
    211. Gpio.IsReady = 0x11111111U;
    212. //Initialize the GPIO device. end
    213. XGpioPs_SetDirectionPin(&Gpio, mioPinNum, 1);
    214. // MIO0
    215. XGpioPs_WritePin(&Gpio, mioPinNum, 0x1);
    216. if(XGpioPs_ReadPin(&Gpio,mioPinNum) != 1)
    217. printf("mio reset phy failed \n\r");
    218. udelay(2000);
    219. XGpioPs_WritePin(&Gpio, mioPinNum, 0x0);
    220. if(XGpioPs_ReadPin(&Gpio,mioPinNum) != 0)
    221. printf("mio reset phy failed \n\r");
    222. udelay(2000);
    223. XGpioPs_WritePin(&Gpio, mioPinNum, 0x1);
    224. if(XGpioPs_ReadPin(&Gpio,mioPinNum) != 1)
    225. printf("mio reset phy failed \n\r");
    226. }
    227. //
    228. /* JEM5396 读写通用代码 */
    229. //
    230. #ifndef TRUE
    231. #define TRUE 1U
    232. #endif
    233. #ifndef FALSE
    234. #define FALSE 0U
    235. #endif
    236. #ifndef NULL
    237. #define NULL 0U
    238. #endif
    239. #ifndef FPAR_SPIPS_0_DEVICE_ID
    240. #define FPS_SPI0_DEVICE_ID (0)
    241. #else
    242. #define FPS_SPI0_DEVICE_ID FPAR_SPIPS_0_DEVICE_ID
    243. #endif
    244. #ifndef FPS_SPI0_BASEADDR
    245. #define FPS_SPI0_BASEADDR (0xe0001000)
    246. #endif
    247. #ifndef FPAR_SPIPS_1_DEVICE_ID
    248. #define FPS_SPI1_DEVICE_ID (1)
    249. #else
    250. #define FPS_SPI1_DEVICE_ID FPAR_SPIPS_1_DEVICE_ID
    251. #endif
    252. #ifndef FPS_SPI1_BASEADDR
    253. #define FPS_SPI1_BASEADDR (0xe0021000)
    254. #endif
    255. #define SLCR_SPI0_CTRL (0x300)
    256. #define SLCR_SPI1_CTRL (0x300)
    257. #define SPI0_APB_RST (0x0)
    258. #define SPI1_APB_RST (0x1)
    259. #define SPI0_REF_RST (0x2)
    260. #define SPI1_REF_RST (0x3)
    261. #define SLCR_LOCK (0x004)
    262. #define SLCR_UNLOCK (0x008)
    263. #define FPS_SLCR_BASEADDR (0xE0026000)
    264. register
    265. #define SPIPS_MSTR_OFFSET (0x100)
    266. #define SPIPS_CTRLR0_OFFSET (0x00)
    267. #define SPIPS_CTRLR1_OFFSET (0x04)
    268. #define SPIPS_SSIENR_OFFSET (0x08)
    269. #define SPIPS_MVCR_OFFSET (0x0C)
    270. #define SPIPS_SER_OFFSET (0x10)
    271. #define SPIPS_BAUDR_OFFSET (0x14)
    272. #define SPIPS_TXFTLR_OFFSET (0x18)
    273. #define SPIPS_RXFTLR_OFFSET (0x1C)
    274. #define SPIPS_TXFLR_OFFSET (0x20)
    275. #define SPIPS_RXFLR_OFFSET (0x24)
    276. #define SPIPS_SR_OFFSET (0x28)
    277. #define SPIPS_IMR_OFFSET (0x2C)
    278. #define SPIPS_ISR_OFFSET (0x30)
    279. #define SPIPS_RISR_OFFSET (0x34)
    280. #define SPIPS_TXOICR_OFFSET (0x38)
    281. #define SPIPS_RXOICR_OFFSET (0x3C)
    282. #define SPIPS_RXUICR_OFFSET (0x40)
    283. #define SPIPS_MSTICR_OFFSET (0x44)
    284. #define SPIPS_ICR_OFFSET (0x48)
    285. #define SPIPS_DMACR_OFFSET (0x4C)
    286. #define SPIPS_DMATDLR_OFFSET (0x50)
    287. #define SPIPS_DMARDLR_OFFSET (0x54)
    288. #define SPIPS_IDR_OFFSET (0x58)
    289. #define SPIPS_VERSION_OFFSET (0x5C)
    290. #define SPIPS_DR_OFFSET (0x60)
    291. #define SPIPS_RX_SAMPLE_OFFSET (0xf0)
    292. #define SPIPS_SCTRLR0_OFFSET (0xf4)
    293. #define SPIPS_RSVD1_OFFSET (0xf8)
    294. #define SPIPS_RSVD2_OFFSET (0xfc)
    295. #define FMSH_ReadReg(baseAddr, offSet) *((volatile unsigned int *)(baseAddr + offSet))
    296. #define FMSH_WriteReg(baseAddr, offSet, data) *((volatile unsigned int *)(baseAddr + offSet)) = data
    297. typedef struct {
    298. u16 deviceId; /**< Unique ID of device */
    299. u32 baseAddress; /**< APB Base address of the device */
    300. } FSpiPs_Config_T;
    301. #define FPS_SPI_NUM_INSTANCES (1)
    302. FSpiPs_Config_T FSpiPs_ConfigTable[] =
    303. {
    304. {
    305. FPS_SPI0_DEVICE_ID,
    306. FPS_SPI0_BASEADDR
    307. },
    308. {
    309. FPS_SPI1_DEVICE_ID,
    310. FPS_SPI1_BASEADDR
    311. }
    312. };
    313. FSpiPs_Config_T* FSpiPs_LookupConfig(u16 deviceId)
    314. {
    315. int index;
    316. FSpiPs_Config_T* cfgPtr = NULL;
    317. for (index = 0; index < FPS_SPI_NUM_INSTANCES; index++) {
    318. if (FSpiPs_ConfigTable[index].deviceId == deviceId) {
    319. cfgPtr = &FSpiPs_ConfigTable[index];
    320. break;
    321. }
    322. }
    323. return cfgPtr;
    324. }
    325. typedef unsigned char BOOL;
    326. typedef void (*FSpiPs_StatusHandler) (void *CallBackRef, u32 StatusEvent,
    327. u32 ByteCount);
    328. typedef struct FSpiPs_Tag{
    329. FSpiPs_Config_T config; /**< Configuration structure */
    330. u32 flag;
    331. BOOL isEnable;
    332. BOOL isBusy;
    333. BOOL isMaster; /**< Master/Slave */
    334. u8 frameSize;
    335. u32 totalBytes;
    336. u32 remainingBytes;
    337. u32 requestedBytes;
    338. u8* sendBufferPtr; /**< Buffer to send (state) */
    339. u8* recvBufferPtr; /**< Buffer to receive (state) */
    340. int (*Transfer)(struct FSpiPs_Tag spi, void* sendBuffer, void* recvBuffer, u32 byteCount);
    341. FSpiPs_StatusHandler statusHandler;
    342. void* statusRef;
    343. } FSpiPs_T;
    344. static void StubStatusHandler(void *callBackRef, u32 statusEvent,
    345. u32 byteCount)
    346. {
    347. (void) callBackRef;
    348. (void) statusEvent;
    349. (void) byteCount;
    350. }
    351. #define FMSH_SUCCESS 0L
    352. #define FMSH_FAILURE 1L
    353. int FSpiPs_CfgInitialize(FSpiPs_T* spi, FSpiPs_Config_T* configPtr)
    354. {
    355. spi->config = *configPtr;
    356. spi->isBusy = FALSE;
    357. spi->remainingBytes = 0;
    358. spi->requestedBytes = 0;
    359. spi->sendBufferPtr = NULL;
    360. spi->recvBufferPtr = NULL;
    361. spi->flag = 0;
    362. spi->isEnable = FALSE;
    363. spi->isMaster = FALSE;
    364. spi->totalBytes = 0;
    365. spi->statusHandler = StubStatusHandler;
    366. return FMSH_SUCCESS;
    367. }
    368. void FSpiPs_Enable(FSpiPs_T* spi)
    369. {
    370. FMSH_WriteReg(spi->config.baseAddress, SPIPS_SSIENR_OFFSET, 0x1);
    371. spi->isEnable = TRUE;
    372. #if 0
    373. u32 temp=0;
    374. //add by fzx
    375. printf("#########FMSH SPI REG IS BELOW\n");
    376. temp=FMSH_ReadReg(spi->config.baseAddress, SPIPS_MSTR_OFFSET);
    377. printf("SPIPS_MSTR_OFFSET is 0x%x\n",temp);
    378. temp=FMSH_ReadReg(spi->config.baseAddress, SPIPS_CTRLR0_OFFSET);
    379. printf("SPIPS_CTRLR0_OFFSET is 0x%x\n",temp);
    380. temp=FMSH_ReadReg(spi->config.baseAddress, SPIPS_CTRLR1_OFFSET);
    381. printf("SPIPS_CTRLR1_OFFSET is 0x%x\n",temp);
    382. temp=FMSH_ReadReg(spi->config.baseAddress, SPIPS_SSIENR_OFFSET);
    383. printf("SPIPS_SSIENR_OFFSET is 0x%x\n",temp);
    384. temp=FMSH_ReadReg(spi->config.baseAddress, SPIPS_MVCR_OFFSET);
    385. printf("SPIPS_MVCR_OFFSET is 0x%x\n",temp);
    386. temp=FMSH_ReadReg(spi->config.baseAddress, SPIPS_SER_OFFSET);
    387. printf("SPIPS_SER_OFFSET is 0x%x\n",temp);
    388. temp=FMSH_ReadReg(spi->config.baseAddress, SPIPS_BAUDR_OFFSET);
    389. printf("SPIPS_BAUDR_OFFSET is 0x%x\n",temp);
    390. temp=FMSH_ReadReg(spi->config.baseAddress, SPIPS_SR_OFFSET);
    391. printf("SPIPS_SR_OFFSET is 0x%x\n",temp);
    392. printf("############################\n");
    393. printf("############################\n");
    394. #endif
    395. }
    396. void FSpiPs_Disable(FSpiPs_T* spi)
    397. {
    398. FMSH_WriteReg(spi->config.baseAddress, SPIPS_SSIENR_OFFSET, 0x0);
    399. spi->isEnable = FALSE;
    400. }
    401. void FSpiPs_Mst(FSpiPs_T *spi)
    402. {
    403. FMSH_WriteReg(spi->config.baseAddress, SPIPS_MSTR_OFFSET, 0x1);
    404. spi->isMaster = TRUE;
    405. }
    406. #define SPIPS_CTRL0_SCPH_MASK (0x1 << 6)
    407. #define SPIPS_CTRL0_SCPOL_MASK (0x1 << 7)
    408. #define SPIPS_CTRL0_TMOD_MASK (0x3 << 8)
    409. #define SPIPS_CTRL0_SLVOE_MASK (0x1 << 10)
    410. #define SPIPS_CTRL0_SRL_MASK (0x1 << 11)
    411. #define SPIPS_CTRL0_DFS32_MASK (0x1f << 16)
    412. #define SPIPS_CTRL0_SCPH_SHIFT (6)
    413. #define SPIPS_CTRL0_TMOD_SHIFT (8)
    414. #define SPIPS_CTRL0_DFS32_SHIFT (16)
    415. #define SPIPS_TRANSFER_STATE (0x0)
    416. #define SPIPS_TRANSMIT_ONLY_STATE (0x1)
    417. #define SPIPS_RECEIVE_ONLY_STATE (0x2)
    418. #define SPIPS_EEPROM_STATE (0x3)
    419. int FSpiPs_SetTMod(FSpiPs_T* spi, u32 tmod)
    420. {
    421. u32 configReg;
    422. if(spi->isEnable == TRUE)
    423. {
    424. return FMSH_FAILURE;
    425. }
    426. if(tmod > 3)
    427. {
    428. return FMSH_FAILURE;
    429. }
    430. configReg = FMSH_ReadReg(spi->config.baseAddress, SPIPS_CTRLR0_OFFSET);
    431. configReg &= ~SPIPS_CTRL0_TMOD_MASK;
    432. configReg |= (tmod << SPIPS_CTRL0_TMOD_SHIFT);
    433. FMSH_WriteReg(spi->config.baseAddress, SPIPS_CTRLR0_OFFSET, configReg);
    434. return FMSH_SUCCESS;
    435. }
    436. int FSpiPs_SetSckMode(FSpiPs_T* spi, u32 sckMode)
    437. {
    438. u32 configReg;
    439. if(spi->isEnable == TRUE)
    440. {
    441. return FMSH_FAILURE;
    442. }
    443. if(sckMode > 3)
    444. {
    445. return FMSH_FAILURE;
    446. }
    447. configReg = FMSH_ReadReg(spi->config.baseAddress, SPIPS_CTRLR0_OFFSET);
    448. configReg &= ~(SPIPS_CTRL0_SCPH_MASK | SPIPS_CTRL0_SCPOL_MASK);
    449. configReg |= (sckMode << SPIPS_CTRL0_SCPH_SHIFT);
    450. FMSH_WriteReg(spi->config.baseAddress, SPIPS_CTRLR0_OFFSET, configReg);
    451. return FMSH_SUCCESS;
    452. }
    453. int FSpiPs_SetSckDv(FSpiPs_T* spi, u32 sckdv)
    454. {
    455. if(spi->isEnable == TRUE || spi->isMaster == FALSE)
    456. {
    457. return FMSH_FAILURE;
    458. }
    459. FMSH_WriteReg(spi->config.baseAddress, SPIPS_BAUDR_OFFSET, sckdv);
    460. return FMSH_SUCCESS;
    461. }
    462. int FSpiPs_SetDFS32(FSpiPs_T* spi, u32 dfs32)
    463. {
    464. u32 configReg;
    465. if(spi->isEnable == TRUE)
    466. {
    467. return FMSH_FAILURE;
    468. }
    469. if(dfs32<4 || dfs32 > 0x20)
    470. {
    471. return FMSH_FAILURE;
    472. }
    473. configReg = FMSH_ReadReg(spi->config.baseAddress, SPIPS_CTRLR0_OFFSET);
    474. configReg &= ~SPIPS_CTRL0_DFS32_MASK;
    475. configReg |= ((dfs32-1) << SPIPS_CTRL0_DFS32_SHIFT);
    476. FMSH_WriteReg(spi->config.baseAddress, SPIPS_CTRLR0_OFFSET, configReg);
    477. spi->frameSize = dfs32;
    478. return FMSH_SUCCESS;
    479. }
    480. int FSpiPs_SetLoopBack(FSpiPs_T* spi, BOOL enable)
    481. {
    482. u32 configReg;
    483. if(spi->isEnable == TRUE)
    484. {
    485. return FMSH_FAILURE;
    486. }
    487. configReg = FMSH_ReadReg(spi->config.baseAddress, SPIPS_CTRLR0_OFFSET);
    488. if(enable)
    489. {
    490. configReg |= SPIPS_CTRL0_SRL_MASK;
    491. }
    492. else
    493. {
    494. configReg &= ~SPIPS_CTRL0_SRL_MASK;
    495. }
    496. FMSH_WriteReg(spi->config.baseAddress, SPIPS_CTRLR0_OFFSET, configReg);
    497. return FMSH_SUCCESS;
    498. }
    499. int FSpiPs_SetDFNum(FSpiPs_T* spi, u32 dfNum)
    500. {
    501. if(spi->isEnable == TRUE || spi->isMaster == FALSE)
    502. {
    503. return FMSH_FAILURE;
    504. }
    505. FMSH_WriteReg(spi->config.baseAddress, SPIPS_CTRLR1_OFFSET, dfNum-1);
    506. return FMSH_SUCCESS;
    507. }
    508. int FSpiPs_SetTxEmptyLvl(FSpiPs_T* spi, u8 tlvl)
    509. {
    510. if(spi->isEnable == TRUE)
    511. {
    512. return FMSH_FAILURE;
    513. }
    514. FMSH_WriteReg(spi->config.baseAddress, SPIPS_TXFTLR_OFFSET, tlvl);
    515. return FMSH_SUCCESS;
    516. }
    517. int FSpiPs_SetRxFullLvl(FSpiPs_T* spi, u8 tlvl)
    518. {
    519. if(spi->isEnable == TRUE)
    520. {
    521. return FMSH_FAILURE;
    522. }
    523. FMSH_WriteReg(spi->config.baseAddress, SPIPS_RXFTLR_OFFSET, tlvl);
    524. return FMSH_SUCCESS;
    525. }
    526. void FSpiPs_SetDMATLvl(FSpiPs_T* spi, u32 tlvl)
    527. {
    528. FMSH_WriteReg(spi->config.baseAddress, SPIPS_DMATDLR_OFFSET, tlvl);
    529. }
    530. void FSpiPs_SetDMARLvl(FSpiPs_T* spi, u32 tlvl)
    531. {
    532. FMSH_WriteReg(spi->config.baseAddress, SPIPS_DMARDLR_OFFSET, tlvl);
    533. }
    534. void FSpiPs_DisableIntr( FSpiPs_T* spi, u32 mask)
    535. {
    536. u32 configReg;
    537. configReg = FMSH_ReadReg(spi->config.baseAddress, SPIPS_IMR_OFFSET);
    538. configReg &= ~mask;
    539. FMSH_WriteReg(spi->config.baseAddress, SPIPS_IMR_OFFSET, configReg);
    540. }
    541. int FSpiPs_SetSlave(FSpiPs_T* spi, u32 slaveNo)
    542. {
    543. if(spi->isMaster == FALSE)
    544. {
    545. return FMSH_FAILURE;
    546. }
    547. if(slaveNo == 0)
    548. {
    549. FMSH_WriteReg(spi->config.baseAddress, SPIPS_SER_OFFSET, 0x0);
    550. }
    551. else
    552. {
    553. FMSH_WriteReg(spi->config.baseAddress, SPIPS_SER_OFFSET, 0x1 << (slaveNo - 1));
    554. }
    555. return FMSH_SUCCESS;
    556. }
    557. #define SPIPS_INTR_ALL (0x3f)
    558. int FSpiPs_Initialize_Master(FSpiPs_T* spi)
    559. {
    560. int err = 0;
    561. // Check whether there is another transfer in progress. Not thread-safe
    562. if(spi->isBusy == TRUE)
    563. {
    564. return FMSH_FAILURE;
    565. }
    566. // Disable device
    567. FSpiPs_Disable(spi);
    568. // Select device as Master
    569. FSpiPs_Mst(spi);
    570. // CTRL (TMode, CkMode, BaudRate, DFSize, DFNum, isLoopBack)
    571. err |= FSpiPs_SetTMod(spi, SPIPS_TRANSFER_STATE);
    572. err |= FSpiPs_SetSckMode(spi, 3);
    573. err |= FSpiPs_SetSckDv(spi, 120);
    574. err |= FSpiPs_SetDFS32(spi, 8);
    575. err |= FSpiPs_SetLoopBack(spi, FALSE);
    576. err |= FSpiPs_SetDFNum(spi, 128);
    577. // Config Tx/Rx Threshold
    578. err |= FSpiPs_SetTxEmptyLvl(spi, 20);
    579. err |= FSpiPs_SetRxFullLvl(spi, 12);
    580. FSpiPs_SetDMATLvl(spi, 12);
    581. FSpiPs_SetDMARLvl(spi, 20);
    582. // Config IMR
    583. FSpiPs_DisableIntr(spi, SPIPS_INTR_ALL);
    584. // SlaveSelect
    585. err |= FSpiPs_SetSlave(spi, 1);
    586. if(err)
    587. {
    588. return FMSH_FAILURE;
    589. }
    590. // Enable device
    591. FSpiPs_Enable(spi);
    592. return FMSH_SUCCESS;
    593. }
    594. #define NREAD (0x60)
    595. #define NWRITE (0x61)
    596. #define SIO (0xF0)
    597. #define STS (0xFE)
    598. #define SPG (0xFF)
    599. #define SPIPS_SR_DCOL (0x40)
    600. #define SPIPS_SR_TXE (0x20)
    601. #define SPIPS_SR_RFF (0x10)
    602. #define SPIPS_SR_RFNE (0x08)
    603. #define SPIPS_SR_TFE (0x04)
    604. #define SPIPS_SR_TFNF (0x02)
    605. #define SPIPS_SR_BUSY (0x01)
    606. void delay_us(u32 time_us)
    607. {
    608. for(u32 i = 0; i < 100000; i++);
    609. }
    610. void delay_ms(u32 time_ms)
    611. {
    612. for(u32 i = 0; i < 1000000; i++);
    613. }
    614. void FSpiPs_Send(FSpiPs_T* spi, u32 Data )
    615. {
    616. u32 count = 0;
    617. u8 status;
    618. status = FMSH_ReadReg(spi->config.baseAddress, SPIPS_SR_OFFSET);
    619. while((status & SPIPS_SR_TFNF) == 0) /* loop if TX fifo full */
    620. {
    621. delay_us(1);
    622. count++;
    623. if(count > 10000)
    624. {
    625. break;
    626. }
    627. status = FMSH_ReadReg(spi->config.baseAddress, SPIPS_SR_OFFSET);
    628. }
    629. FMSH_WriteReg(spi->config.baseAddress, SPIPS_DR_OFFSET, Data);
    630. }
    631. u32 FSpiPs_Recv(FSpiPs_T* spi )
    632. {
    633. u32 count = 0;
    634. u8 status;
    635. status = FMSH_ReadReg(spi->config.baseAddress, SPIPS_SR_OFFSET);
    636. while((status & SPIPS_SR_RFNE) == 0) /* loop if RX fifo empty */
    637. {
    638. delay_us(1);
    639. count++;
    640. if(count > 10000)
    641. {
    642. break;
    643. }
    644. status = FMSH_ReadReg(spi->config.baseAddress, SPIPS_SR_OFFSET);
    645. }
    646. return FMSH_ReadReg(spi->config.baseAddress, SPIPS_DR_OFFSET);
    647. }
    648. #define SPIPS_FIFO_DEPTH (0x20)
    649. int FSpiPs_PolledTransfer(FSpiPs_T* spi, u8* sendBuffer, u8* recvBuffer, u32 byteCount)
    650. {
    651. u8 tmod;
    652. // Check whether there is another transfer in progress. Not thread-safe
    653. if(spi->isBusy == TRUE)
    654. {
    655. return FMSH_FAILURE;
    656. }
    657. // Get transfer mode
    658. tmod = (FMSH_ReadReg(spi->config.baseAddress, SPIPS_CTRLR0_OFFSET) & SPIPS_CTRL0_TMOD_MASK)
    659. >> SPIPS_CTRL0_TMOD_SHIFT;
    660. if(tmod == SPIPS_TRANSFER_STATE)
    661. {
    662. if(sendBuffer == NULL || recvBuffer == NULL)
    663. {
    664. return FMSH_FAILURE;
    665. }
    666. spi->totalBytes = byteCount;
    667. spi->remainingBytes = byteCount;
    668. spi->requestedBytes = byteCount;
    669. spi->sendBufferPtr = sendBuffer;
    670. spi->recvBufferPtr = recvBuffer;
    671. }
    672. else if(tmod == SPIPS_TRANSMIT_ONLY_STATE)
    673. {
    674. if(sendBuffer == NULL)
    675. {
    676. return FMSH_FAILURE;
    677. }
    678. spi->totalBytes = byteCount;
    679. spi->requestedBytes = 0;
    680. spi->remainingBytes = byteCount;
    681. }
    682. else if(tmod == SPIPS_RECEIVE_ONLY_STATE)
    683. {
    684. if(recvBuffer == NULL)
    685. {
    686. return FMSH_FAILURE;
    687. }
    688. spi->totalBytes = byteCount;
    689. spi->requestedBytes = byteCount;
    690. spi->remainingBytes = 0;
    691. /* Write one dummy data word to Tx FIFO */
    692. FSpiPs_Send(spi, 0xff);
    693. }
    694. else if(tmod == SPIPS_EEPROM_STATE)
    695. {
    696. if(sendBuffer == NULL || recvBuffer == NULL)
    697. {
    698. return FMSH_FAILURE;
    699. }
    700. spi->totalBytes = byteCount;
    701. spi->requestedBytes = 0;
    702. spi->remainingBytes = byteCount;
    703. }
    704. // Set the busy flag, cleared when transfer is done
    705. spi->isBusy = TRUE;
    706. //disable interrupt
    707. FSpiPs_DisableIntr(spi, SPIPS_INTR_ALL);
    708. //enable spi
    709. FSpiPs_Enable(spi);
    710. //polling tx fifo level until transfer complete
    711. int cnt;
    712. u32 txLvl;
    713. u32 txEmptyLvl = 10;
    714. while(spi->remainingBytes !=0 || spi->requestedBytes != 0)
    715. {
    716. txLvl = FMSH_ReadReg(spi->config.baseAddress, SPIPS_TXFLR_OFFSET);
    717. if(txLvl <= txEmptyLvl)
    718. {
    719. cnt = FMSH_ReadReg(spi->config.baseAddress, SPIPS_RXFLR_OFFSET);
    720. while(cnt > 0 && spi->requestedBytes != 0)
    721. {
    722. if(spi->frameSize == 8)
    723. {
    724. *(u8*)(spi->recvBufferPtr) = (u8)FSpiPs_Recv(spi);
    725. }
    726. else if(spi->frameSize == 16)
    727. {
    728. *(u16*)(spi->recvBufferPtr) = (u16)FSpiPs_Recv(spi);
    729. }
    730. else if(spi->frameSize == 32)
    731. {
    732. *(u32*)(spi->recvBufferPtr) = (u32)FSpiPs_Recv(spi);
    733. }
    734. spi->recvBufferPtr += spi->frameSize >> 3;
    735. spi->requestedBytes -= spi->frameSize >> 3;
    736. cnt--;
    737. }
    738. cnt = SPIPS_FIFO_DEPTH - txEmptyLvl;
    739. while(cnt > 0 && spi->remainingBytes != 0)
    740. {
    741. if(spi->frameSize == 8)
    742. {
    743. FSpiPs_Send(spi, *(u8*)spi->sendBufferPtr);
    744. }
    745. else if(spi->frameSize == 16)
    746. {
    747. FSpiPs_Send(spi, *(u16*)spi->sendBufferPtr);
    748. }
    749. else if(spi->frameSize == 32)
    750. {
    751. FSpiPs_Send(spi, *(u32*)spi->sendBufferPtr);
    752. }
    753. spi->sendBufferPtr += spi->frameSize >> 3;
    754. spi->remainingBytes -= spi->frameSize >> 3;
    755. cnt--;
    756. }
    757. }
    758. }
    759. //Clear the busy flag
    760. spi->isBusy = FALSE;
    761. //disable spi
    762. FSpiPs_Disable(spi);
    763. return FMSH_SUCCESS;
    764. }
    765. #define SPIF (0x80)
    766. #define RACK (0x20)
    767. #define RXRDY (0x02)
    768. #define TXRDY (0x01
    769. #define TIMEOUT_HANDLER (retVal= FMSH_FAILURE);
    770. int writeBCM5396( FSpiPs_T *Spi_ptr,u8 page, u8 offset, u8 *pBuffer )
    771. {
    772. u8 data[20];
    773. u8 data_recv[20];
    774. s32 retVal;
    775. u32 u32SendNum, u32ReqRetNum;
    776. int i;
    777. for(i=0;i<20;i++)
    778. data[i]=i;
    779. // Set Page
    780. data[0] = NWRITE;
    781. data[1] = SPG;
    782. data[2] = page;
    783. u32SendNum = 3;
    784. u32ReqRetNum = 0;
    785. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum);//pBuffer
    786. if( retVal != FMSH_SUCCESS )
    787. {
    788. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    789. }
    790. // Read STS
    791. READ_STS_1:
    792. data[0] = NREAD;
    793. data[1] = STS;
    794. u32SendNum = 2;
    795. u32ReqRetNum = 1;
    796. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    797. if( retVal == FMSH_SUCCESS )
    798. {
    799. if((data_recv[2] & SPIF)==0)//( workBuf[2] & SPIF )
    800. {
    801. // Set Page
    802. data[0] = NWRITE;
    803. data[1] = SPG;
    804. data[2] = page;
    805. u32SendNum = 3;
    806. u32ReqRetNum = 0;
    807. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    808. if( retVal != FMSH_SUCCESS )
    809. {
    810. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    811. }
    812. // Write Data
    813. data[0] = NWRITE;
    814. data[1] = offset;
    815. data[2] = pBuffer[0];
    816. data[3] = pBuffer[1];
    817. u32SendNum = 4;
    818. u32ReqRetNum = 0;//1;
    819. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    820. if( retVal != FMSH_SUCCESS )
    821. {
    822. printf("Call XSpiPs_PT_SL 2 Failed\n\r");
    823. }
    824. }
    825. else
    826. {
    827. TIMEOUT_HANDLER;
    828. printf( "Timeout 1 Occured!\n\r" );
    829. delay_ms(100);
    830. // Set Page
    831. data[0] = NWRITE;
    832. data[1] = SPG;
    833. data[2] = page;
    834. u32SendNum = 3;
    835. u32ReqRetNum = 0;
    836. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    837. if( retVal != FMSH_SUCCESS )
    838. {
    839. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    840. }
    841. goto READ_STS_1;
    842. }
    843. }
    844. else
    845. printf("Call XSpiPs_PT_SL 4 Failed\n\r");
    846. return retVal;
    847. }
    848. int readBCM5396(FSpiPs_T *Spi_ptr, u8 page, u8 offset, u8 *pBuffer )
    849. {
    850. // _BCM_CMD_ bcmCmd;
    851. u8 data[8];//u8Idx;
    852. u8 data_recv[8] = {0};
    853. s32 retVal;
    854. u32 u32SendNum, u32ReqRetNum;
    855. // Set Page
    856. data[0] = NWRITE;
    857. data[1] = SPG;
    858. data[2] = page;
    859. u32SendNum = 3;
    860. u32ReqRetNum = 0;
    861. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum);
    862. if( retVal != FMSH_SUCCESS )
    863. {
    864. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    865. }
    866. delay_ms(10);
    867. // Read STS
    868. READ_STS_1:
    869. data[0] = NREAD;
    870. data[1] = STS;
    871. u32SendNum = 2;
    872. u32ReqRetNum = 1;
    873. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    874. delay_ms(10);
    875. if( retVal == FMSH_SUCCESS )
    876. {
    877. if( (data_recv[2] & SPIF)==0)
    878. {
    879. // Set Page
    880. data[0] = NWRITE;
    881. data[1] = SPG;
    882. data[2] = page;
    883. u32SendNum = 3;
    884. u32ReqRetNum = 0;
    885. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    886. if( retVal != FMSH_SUCCESS )
    887. {
    888. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    889. }
    890. // Set Offset in Page, "Null" operation :)
    891. data[0] = NREAD;
    892. data[1] = offset;
    893. u32SendNum = 2;
    894. u32ReqRetNum = 1;
    895. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    896. if( retVal != FMSH_SUCCESS )
    897. {
    898. printf("Call XSpiPs_PT_SL 2 Failed\n\r");
    899. }
    900. // Read STS
    901. READ_STS_2:
    902. data[0] = NREAD;
    903. data[1] = STS;
    904. u32SendNum = 2;
    905. u32ReqRetNum = 1;
    906. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    907. if( retVal == FMSH_SUCCESS )
    908. {
    909. if( data_recv[2] & RACK )
    910. {
    911. /*
    912. bcmCmd.cmd = NREAD;
    913. bcmCmd.regAdrs = SIO;
    914. */
    915. data[0] = NREAD;
    916. data[1] = SIO;
    917. u32SendNum = 2;
    918. u32ReqRetNum = 4;
    919. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,pBuffer,u32SendNum+u32ReqRetNum);
    920. }
    921. else
    922. {
    923. TIMEOUT_HANDLER;
    924. printf( "Timeout 2 Occured!\n\r" );
    925. delay_ms(10);
    926. // Set Page
    927. data[0] = NWRITE;
    928. data[1] = SPG;
    929. data[2] = page;
    930. u32SendNum = 3;
    931. u32ReqRetNum = 0;
    932. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    933. if( retVal != FMSH_SUCCESS )
    934. {
    935. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    936. }
    937. goto READ_STS_2;
    938. }
    939. }
    940. else
    941. printf("Call XSpiPs_PT_SL 3 Failed\n\r");
    942. }
    943. else
    944. {
    945. TIMEOUT_HANDLER;
    946. printf( "Timeout 1 Occured!\n\r" );
    947. delay_ms(100);
    948. // Set Page
    949. data[0] = NWRITE;
    950. data[1] = SPG;
    951. data[2] = page;
    952. u32SendNum = 3;
    953. u32ReqRetNum = 0;
    954. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,pBuffer,u32SendNum+u32ReqRetNum);
    955. if( retVal != FMSH_SUCCESS )
    956. {
    957. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    958. }
    959. goto READ_STS_1;
    960. }
    961. }
    962. else
    963. printf("Call XSpiPs_PT_SL 4 Failed\n\r");
    964. return retVal;
    965. }
    966. int writeBCM5396_2( FSpiPs_T *Spi_ptr,u8 page, u8 offset, u8 *pBuffer )
    967. {
    968. u8 data[20];
    969. u8 data_recv[20];
    970. s32 retVal;
    971. u32 u32SendNum, u32ReqRetNum;
    972. int i;
    973. for(i=0;i<20;i++)
    974. data[i]=i;
    975. // Set Page
    976. data[0] = NWRITE;
    977. data[1] = SPG;
    978. data[2] = page;
    979. u32SendNum = 3;
    980. u32ReqRetNum = 0;
    981. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum);//pBuffer
    982. if( retVal != FMSH_SUCCESS )
    983. {
    984. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    985. }
    986. // Read STS
    987. READ_STS_1:
    988. data[0] = NREAD;
    989. data[1] = STS;
    990. u32SendNum = 2;
    991. u32ReqRetNum = 1;
    992. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    993. //
    994. if( retVal == FMSH_SUCCESS )
    995. {
    996. if((data_recv[2] & SPIF)==0)//( workBuf[2] & SPIF )
    997. {
    998. // Set Page
    999. data[0] = NWRITE;
    1000. data[1] = SPG;
    1001. data[2] = page;
    1002. u32SendNum = 3;
    1003. u32ReqRetNum = 0;
    1004. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    1005. if( retVal != FMSH_SUCCESS )
    1006. {
    1007. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    1008. }
    1009. // Write Data
    1010. data[0] = NWRITE;
    1011. data[1] = offset;
    1012. data[2] = pBuffer[0];
    1013. u32SendNum = 3;
    1014. u32ReqRetNum = 0;//1;
    1015. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    1016. if( retVal != FMSH_SUCCESS )
    1017. {
    1018. printf("Call XSpiPs_PT_SL 2 Failed\n\r");
    1019. }
    1020. }
    1021. else
    1022. {
    1023. TIMEOUT_HANDLER;
    1024. printf( "Timeout 1 Occured!\n\r" );
    1025. delay_ms(50);
    1026. // Set Page
    1027. data[0] = NWRITE;
    1028. data[1] = SPG;
    1029. data[2] = page;
    1030. u32SendNum = 3;
    1031. u32ReqRetNum = 0;
    1032. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    1033. if( retVal != FMSH_SUCCESS )
    1034. {
    1035. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    1036. }
    1037. goto READ_STS_1;
    1038. }
    1039. }
    1040. else
    1041. printf("Call XSpiPs_PT_SL 4 Failed\n\r");
    1042. return retVal;
    1043. }
    1044. int writeBCM5396_4( FSpiPs_T *Spi_ptr,u8 page, u8 offset, u8 *pBuffer )
    1045. {
    1046. u8 data[20];
    1047. u8 data_recv[20];
    1048. s32 retVal;
    1049. u32 u32SendNum, u32ReqRetNum;
    1050. int i;
    1051. for(i=0;i<20;i++)
    1052. data[i]=i;
    1053. // Set Page
    1054. data[0] = NWRITE;
    1055. data[1] = SPG;
    1056. data[2] = page;
    1057. u32SendNum = 3;
    1058. u32ReqRetNum = 0;
    1059. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum);//pBuffer
    1060. if( retVal != FMSH_SUCCESS )
    1061. {
    1062. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    1063. }
    1064. // Read STS
    1065. READ_STS_1:
    1066. data[0] = NREAD;
    1067. data[1] = STS;
    1068. u32SendNum = 2;
    1069. u32ReqRetNum = 1;
    1070. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    1071. //
    1072. if( retVal == FMSH_SUCCESS )
    1073. {
    1074. if((data_recv[2] & SPIF)==0)//( workBuf[2] & SPIF )
    1075. {
    1076. // Set Page
    1077. data[0] = NWRITE;
    1078. data[1] = SPG;
    1079. data[2] = page;
    1080. u32SendNum = 3;
    1081. u32ReqRetNum = 0;
    1082. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    1083. if( retVal != FMSH_SUCCESS )
    1084. {
    1085. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    1086. }
    1087. // Write Data
    1088. data[0] = NWRITE;
    1089. data[1] = offset;
    1090. data[2] = pBuffer[0];
    1091. u32SendNum = 6;
    1092. u32ReqRetNum = 0;//1;
    1093. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    1094. if( retVal != FMSH_SUCCESS )
    1095. {
    1096. printf("Call XSpiPs_PT_SL 2 Failed\n\r");
    1097. }
    1098. }
    1099. else
    1100. {
    1101. TIMEOUT_HANDLER;
    1102. printf( "Timeout 1 Occured!\n\r" );
    1103. delay_ms(50);
    1104. // Set Page
    1105. data[0] = NWRITE;
    1106. data[1] = SPG;
    1107. data[2] = page;
    1108. u32SendNum = 3;
    1109. u32ReqRetNum = 0;
    1110. retVal = FSpiPs_PolledTransfer(Spi_ptr,data,data_recv,u32SendNum+u32ReqRetNum);
    1111. if( retVal != FMSH_SUCCESS )
    1112. {
    1113. printf("Call XSpiPs_PT_SL 1 Failed\n\r");
    1114. }
    1115. goto READ_STS_1;
    1116. }
    1117. }
    1118. else
    1119. printf("Call XSpiPs_PT_SL 4 Failed\n\r");
    1120. return retVal;
    1121. }
    1122. FSpiPs_T spi;
    1123. FSpiPs_T Spi[2];
    1124. int port_vlan_set(unsigned int port, unsigned int mask)
    1125. {
    1126. u8 workBuf[10];
    1127. int Status = 0;
    1128. int offset = 0;
    1129. offset = port*4;
    1130. // printf("--------Set Port Vlan:port:%d-offset:%0x-mask:%0x---------\n\r", port, mask);
    1131. workBuf[0] = (mask&0x000000ff);
    1132. workBuf[1] = (mask&0x0000ff00)>>8;
    1133. workBuf[2] = (mask&0x00010000)>>16;
    1134. Status = writeBCM5396_4(&spi, 0x31, offset, workBuf);
    1135. return Status;
    1136. }
    1137. /***************************************BOARD SW1706 JEM5396 REG READ/WRITE*****************************/
    1138. void testwrite5396(int port, unsigned int offset ,unsigned int value)
    1139. {
    1140. u8 workBuf[10];
    1141. workBuf[0]=value & 0xff;
    1142. workBuf[1]=(value>>8) & 0xff;
    1143. writeBCM5396(&spi, port, offset, workBuf);
    1144. udelay(1000);
    1145. readBCM5396(&spi, port, offset, workBuf );
    1146. printf("port=%d,offset=%xh,data=0x%x\n\r",port,offset,workBuf[2]);
    1147. printf("port=%d,offset=%xh,data=0x%x\n\r",port,offset,workBuf[3]);
    1148. }
    1149. void testread5396()
    1150. {
    1151. u8 workBuf[10];
    1152. readBCM5396(&spi, 2, 0x30, workBuf);
    1153. printf("5396 Mode_ID=0x%x\n\r", workBuf[2]);
    1154. }
    1155. int ckoff_spi_5396()
    1156. {
    1157. int ret = 0;
    1158. int Status = 0;
    1159. u8 workBuf[10];
    1160. int i=0;
    1161. /*init spi and read bcm5396 device id*/
    1162. FSpiPs_Config_T *spi_cfgptr;
    1163. spi_cfgptr = FSpiPs_LookupConfig(0);
    1164. ret = FSpiPs_CfgInitialize(&spi, spi_cfgptr);
    1165. if(ret == FMSH_SUCCESS)
    1166. {
    1167. printf("5396 Spi Init Success!\n\r");
    1168. }
    1169. else
    1170. {
    1171. printf("5396 Spi Init Failed!\n\r");
    1172. return -1;
    1173. }
    1174. /*Init master*/
    1175. ret = FSpiPs_Initialize_Master(&spi);
    1176. if(ret == FMSH_SUCCESS)
    1177. {
    1178. printf("spi Init Master Success!\n\r");
    1179. }
    1180. else
    1181. {
    1182. printf("spi Init Master Failed!\n\r");
    1183. return -1;
    1184. }
    1185. workBuf[0]=0xf1;
    1186. workBuf[1]=0x28;
    1187. workBuf[2]=0;
    1188. for(i=0x10;i<=0x1f;i++)
    1189. {
    1190. Status = writeBCM5396(&spi, i, 0x12, workBuf );
    1191. }
    1192. workBuf[0]=0xf0;
    1193. workBuf[1]=0x28;
    1194. workBuf[2]=0;
    1195. for(i=0x10;i<=0x1f;i++)
    1196. {
    1197. Status = writeBCM5396(&spi, i, 0x12, workBuf );
    1198. }
    1199. printf("JEM5396 CHECK OFF DONW\n");
    1200. return 0;
    1201. }
    1202. /***************************************BOARD SW1706 JEM5396 REG READ/WRITE*****************************/
    1203. int init_spi_5396()
    1204. {
    1205. int ret = 0;
    1206. int Status = 0;
    1207. u8 workBuf[10];
    1208. int i=0;
    1209. /*init spi and read bcm5396 device id*/
    1210. #if 0
    1211. FSpiPs_Config_T *spi_cfgptr;
    1212. spi_cfgptr = FSpiPs_LookupConfig(0);
    1213. ret = FSpiPs_CfgInitialize(&spi, spi_cfgptr);
    1214. if(ret == FMSH_SUCCESS)
    1215. {
    1216. printf("5396 Spi Init Success!\n\r");
    1217. }
    1218. else
    1219. {
    1220. printf("5396 Spi Init Failed!\n\r");
    1221. return -1;
    1222. }
    1223. /*Init master*/
    1224. ret = FSpiPs_Initialize_Master(&spi);
    1225. if(ret == FMSH_SUCCESS)
    1226. {
    1227. printf("spi Init Master Success!\n\r");
    1228. }
    1229. else
    1230. {
    1231. printf("spi Init Master Failed!\n\r");
    1232. return -1;
    1233. }
    1234. #endif
    1235. Status=readBCM5396(&spi, 2, 0x30, workBuf);
    1236. printf("5396 Mode_ID=0x%x\n\r", workBuf[2]);
    1237. Status=readBCM5396(&spi, 2, 0x40, workBuf);
    1238. printf("5396 Reversion_ID=0x%x\n\r", workBuf[2]);
    1239. /***************************************JEM5396 CONFIG*****************************/
    1240. /**PORT N STATE OVERRIDE REGISTER [0:15] (PAGE 00H: ADDRESS 60H–6FH) **/
    1241. /**强制千兆全双工模式**/
    1242. for(i=0x60;i<=0x6f;i++)
    1243. {
    1244. workBuf[0] = 0x8b;
    1245. workBuf[1] = 0x0;
    1246. if(i==0x65)
    1247. {
    1248. continue;
    1249. }
    1250. writeBCM5396_2(&spi, 0, i, workBuf);
    1251. }
    1252. workBuf[0]=0xf0;
    1253. workBuf[1]=0x1;
    1254. workBuf[2]=0;
    1255. for(i=0x10;i<=0x1f;i++)
    1256. {
    1257. if(i==0x15)
    1258. {
    1259. continue;
    1260. }
    1261. Status = writeBCM5396(&spi, i, 0x20, workBuf );
    1262. }
    1263. workBuf[0]=0x40;
    1264. workBuf[1]=0x13;
    1265. workBuf[2]=0;
    1266. for(i=0x10;i<=0x1f;i++)
    1267. {
    1268. if(i==0x15)
    1269. {
    1270. continue;
    1271. }
    1272. Status = writeBCM5396(&spi, i, 0x0, workBuf );
    1273. }
    1274. printf("Set Port 5 CONNECT TO ZYNQ SPEED FIXED 1000M\n");
    1275. workBuf[0] = 0x8b;
    1276. workBuf[1] = 0x0;
    1277. writeBCM5396_2(&spi, 0, 0x65, workBuf);
    1278. workBuf[0] = 0x40;
    1279. workBuf[1] = 0x1;
    1280. Status = writeBCM5396(&spi, 0x15, 0x0, workBuf );
    1281. printf("*****************Read SPI Reg of 5396******************\r\n");
    1282. printf("---BMC Status Registers(PAGE 0x10-0x1F)---\n");
    1283. for(i=0x10;i<=0x1f;i++)
    1284. {
    1285. Status = readBCM5396(&spi, i, 0x28, workBuf );
    1286. printf("port=%d,offset=0x28,data=0x%x\n",(i-0x10),workBuf[2]);
    1287. printf("port=%d,offset=0x29,data=0x%x\n",(i-0x10),workBuf[3]);
    1288. }
    1289. printf("---BMC Status Registers(PAGE 0x10-0x1F)---\n\r");
    1290. for(i=0x10;i<=0x1f;i++)
    1291. {
    1292. Status = readBCM5396(&spi, i, 0x20, workBuf );
    1293. printf("port=%d,offset=0x20,data=0x%x\n\r",(i-0x10),workBuf[2]);
    1294. printf("port=%d,offset=0x21,data=0x%x\n\r",(i-0x10),workBuf[3]);
    1295. }
    1296. #if 0
    1297. workBuf[0]=0x00;
    1298. workBuf[1]=0x00;
    1299. workBuf[2]=0x00;
    1300. Status =writeBCM5396(&spi, 0x31, 0x00, workBuf);//Port 0
    1301. Status =writeBCM5396(&spi, 0x31, 0x04, workBuf);//Port 1
    1302. Status =writeBCM5396(&spi, 0x31, 0x08, workBuf);//Port 2
    1303. Status =writeBCM5396(&spi, 0x31, 0x0c, workBuf);//Port 3
    1304. Status =writeBCM5396(&spi, 0x31, 0x10, workBuf);//Port 4
    1305. Status =writeBCM5396(&spi, 0x31, 0x14, workBuf);//Port 5
    1306. Status =writeBCM5396(&spi, 0x31, 0x18, workBuf);//Port 6
    1307. Status =writeBCM5396(&spi, 0x31, 0x1c, workBuf);//Port 7
    1308. Status =writeBCM5396(&spi, 0x31, 0x20, workBuf);//Port 8
    1309. Status =writeBCM5396(&spi, 0x31, 0x24, workBuf);//Port 9
    1310. Status =writeBCM5396(&spi, 0x31, 0x28, workBuf);//Port 10
    1311. Status =writeBCM5396(&spi, 0x31, 0x2c, workBuf);//Port 11
    1312. Status =writeBCM5396(&spi, 0x31, 0x30, workBuf);//Port 12
    1313. Status =writeBCM5396(&spi, 0x31, 0x34, workBuf);//Port 13
    1314. Status =writeBCM5396(&spi, 0x31, 0x38, workBuf);//Port 14
    1315. Status =writeBCM5396(&spi, 0x31, 0x3c, workBuf);//Port 15
    1316. Status =writeBCM5396(&spi, 0x31, 0x40, workBuf);//Port 15
    1317. printf("5396 ALL Port Disconnect\n\r");
    1318. #endif
    1319. return 0;
    1320. }
    1321. void FSlcrPs_setBitTo1(u32 baseAddr, u32 offSet,u32 bit_num)
    1322. {
    1323. u32 value = 0;
    1324. //First get the current value of the register
    1325. value = FMSH_ReadReg(baseAddr, offSet);
    1326. //Then write the given bit of data as 1
    1327. value |= (1 << bit_num);
    1328. //Finally, write the modified data to the register
    1329. FMSH_WriteReg(baseAddr, offSet, value);
    1330. }
    1331. void FSlcrPs_setBitTo0(u32 baseAddr, u32 offSet,u32 bit_num)
    1332. {
    1333. u32 value = 0;
    1334. //First get the current value of the register
    1335. value = FMSH_ReadReg(baseAddr, offSet);
    1336. //Then write the given bit of data as 0
    1337. value &= ~(1 << bit_num);
    1338. //Finally, write the modified data to the register
    1339. FMSH_WriteReg(baseAddr, offSet, value);
    1340. }
    1341. void FSlcrPs_unlock(void)
    1342. {
    1343. FMSH_WriteReg(FPS_SLCR_BASEADDR,SLCR_UNLOCK,0xDF0D767B); //SLCR UNLOCK
    1344. }
    1345. void FSlcrPs_lock(void)
    1346. {
    1347. FMSH_WriteReg(FPS_SLCR_BASEADDR,SLCR_LOCK,0xDF0D767B); //SLCR LOCK
    1348. }
    1349. void FSlcrPs_ipSetRst(u32 rst_id, u32 rst_mode)
    1350. {
    1351. FSlcrPs_unlock();
    1352. FSlcrPs_setBitTo1(FPS_SLCR_BASEADDR,rst_id,rst_mode);
    1353. FSlcrPs_lock();
    1354. }
    1355. void FSlcrPs_ipReleaseRst(u32 rst_id, u32 rst_mode)
    1356. {
    1357. FSlcrPs_unlock();
    1358. FSlcrPs_setBitTo0(FPS_SLCR_BASEADDR,rst_id,rst_mode);
    1359. FSlcrPs_lock();
    1360. }
    1361. void FSpiPs_Reset(FSpiPs_T* spi)
    1362. {
    1363. if(spi->config.deviceId == FPS_SPI0_DEVICE_ID)
    1364. {
    1365. FSlcrPs_ipSetRst(SLCR_SPI0_CTRL, SPI0_APB_RST);
    1366. FSlcrPs_ipSetRst(SLCR_SPI0_CTRL, SPI0_REF_RST);
    1367. FSlcrPs_ipReleaseRst(SLCR_SPI0_CTRL, SPI0_APB_RST);
    1368. FSlcrPs_ipReleaseRst(SLCR_SPI0_CTRL, SPI0_REF_RST);
    1369. }
    1370. else if(spi->config.deviceId == FPS_SPI1_DEVICE_ID)
    1371. {
    1372. FSlcrPs_ipSetRst(SLCR_SPI1_CTRL, SPI1_APB_RST);
    1373. FSlcrPs_ipSetRst(SLCR_SPI1_CTRL, SPI1_REF_RST);
    1374. FSlcrPs_ipReleaseRst(SLCR_SPI1_CTRL, SPI1_APB_RST);
    1375. FSlcrPs_ipReleaseRst(SLCR_SPI1_CTRL, SPI1_REF_RST);
    1376. }
    1377. }

    配置完成后,网络就能ping通了

  • 相关阅读:
    Servlet的基础详细笔记
    01科技文献
    html学习,html书写规范,骨架标签,图片标签,相对路径,html常用标签
    mybatis配置entity下不同文件夹同类型名称的多个类型时启动springboot项目出现TypeException源码分析
    CentOS7卸载Nginx、最后有命令总结
    PHP-字符串函数
    Flutter 小技巧之 3.16 升级最坑 M3 默认适配技巧
    【Netty】一、高性能NIO通信框架Netty-快速入门
    企业子网划分详解
    使用gtest和lcov测试代码覆盖率
  • 原文地址:https://blog.csdn.net/jj12345jj198999/article/details/134525159