memac.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. * Roy Zang <tie-fei.zang@freescale.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /* MAXFRM - maximum frame length */
  8. #define MAXFRM_MASK 0x0000ffff
  9. #include <common.h>
  10. #include <phy.h>
  11. #include <asm/types.h>
  12. #include <asm/io.h>
  13. #include <fsl_memac.h>
  14. #include "fm.h"
  15. static void memac_init_mac(struct fsl_enet_mac *mac)
  16. {
  17. struct memac *regs = mac->base;
  18. /* mask all interrupt */
  19. out_be32(&regs->imask, IMASK_MASK_ALL);
  20. /* clear all events */
  21. out_be32(&regs->ievent, IEVENT_CLEAR_ALL);
  22. /* set the max receive length */
  23. out_be32(&regs->maxfrm, mac->max_rx_len & MAXFRM_MASK);
  24. /* multicast frame reception for the hash entry disable */
  25. out_be32(&regs->hashtable_ctrl, 0);
  26. }
  27. static void memac_enable_mac(struct fsl_enet_mac *mac)
  28. {
  29. struct memac *regs = mac->base;
  30. setbits_be32(&regs->command_config,
  31. MEMAC_CMD_CFG_RXTX_EN | MEMAC_CMD_CFG_NO_LEN_CHK);
  32. }
  33. static void memac_disable_mac(struct fsl_enet_mac *mac)
  34. {
  35. struct memac *regs = mac->base;
  36. clrbits_be32(&regs->command_config, MEMAC_CMD_CFG_RXTX_EN);
  37. }
  38. static void memac_set_mac_addr(struct fsl_enet_mac *mac, u8 *mac_addr)
  39. {
  40. struct memac *regs = mac->base;
  41. u32 mac_addr0, mac_addr1;
  42. /*
  43. * if a station address of 0x12345678ABCD, perform a write to
  44. * MAC_ADDR0 of 0x78563412, MAC_ADDR1 of 0x0000CDAB
  45. */
  46. mac_addr0 = (mac_addr[3] << 24) | (mac_addr[2] << 16) | \
  47. (mac_addr[1] << 8) | (mac_addr[0]);
  48. out_be32(&regs->mac_addr_0, mac_addr0);
  49. mac_addr1 = ((mac_addr[5] << 8) | mac_addr[4]) & 0x0000ffff;
  50. out_be32(&regs->mac_addr_1, mac_addr1);
  51. }
  52. static void memac_set_interface_mode(struct fsl_enet_mac *mac,
  53. phy_interface_t type, int speed)
  54. {
  55. /* Roy need more work here */
  56. struct memac *regs = mac->base;
  57. u32 if_mode, if_status;
  58. /* clear all bits relative with interface mode */
  59. if_mode = in_be32(&regs->if_mode);
  60. if_status = in_be32(&regs->if_status);
  61. /* set interface mode */
  62. switch (type) {
  63. case PHY_INTERFACE_MODE_GMII:
  64. if_mode &= ~IF_MODE_MASK;
  65. if_mode |= IF_MODE_GMII;
  66. break;
  67. case PHY_INTERFACE_MODE_RGMII:
  68. if_mode |= (IF_MODE_GMII | IF_MODE_RG);
  69. break;
  70. case PHY_INTERFACE_MODE_RMII:
  71. if_mode |= (IF_MODE_GMII | IF_MODE_RM);
  72. break;
  73. case PHY_INTERFACE_MODE_SGMII:
  74. case PHY_INTERFACE_MODE_SGMII_2500:
  75. case PHY_INTERFACE_MODE_QSGMII:
  76. if_mode &= ~IF_MODE_MASK;
  77. if_mode |= (IF_MODE_GMII);
  78. break;
  79. case PHY_INTERFACE_MODE_XGMII:
  80. if_mode &= ~IF_MODE_MASK;
  81. if_mode |= IF_MODE_XGMII;
  82. break;
  83. default:
  84. break;
  85. }
  86. /* Enable automatic speed selection for Non-XGMII */
  87. if (type != PHY_INTERFACE_MODE_XGMII)
  88. if_mode |= IF_MODE_EN_AUTO;
  89. if (type == PHY_INTERFACE_MODE_RGMII) {
  90. if_mode &= ~IF_MODE_EN_AUTO;
  91. if_mode &= ~IF_MODE_SETSP_MASK;
  92. switch (speed) {
  93. case SPEED_1000:
  94. if_mode |= IF_MODE_SETSP_1000M;
  95. break;
  96. case SPEED_100:
  97. if_mode |= IF_MODE_SETSP_100M;
  98. break;
  99. case SPEED_10:
  100. if_mode |= IF_MODE_SETSP_10M;
  101. default:
  102. break;
  103. }
  104. }
  105. debug(" %s, if_mode = %x\n", __func__, if_mode);
  106. debug(" %s, if_status = %x\n", __func__, if_status);
  107. out_be32(&regs->if_mode, if_mode);
  108. return;
  109. }
  110. void init_memac(struct fsl_enet_mac *mac, void *base,
  111. void *phyregs, int max_rx_len)
  112. {
  113. mac->base = base;
  114. mac->phyregs = phyregs;
  115. mac->max_rx_len = max_rx_len;
  116. mac->init_mac = memac_init_mac;
  117. mac->enable_mac = memac_enable_mac;
  118. mac->disable_mac = memac_disable_mac;
  119. mac->set_mac_addr = memac_set_mac_addr;
  120. mac->set_if_mode = memac_set_interface_mode;
  121. }