memac.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. * Roy Zang <tie-fei.zang@freescale.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. */
  20. /* MAXFRM - maximum frame length */
  21. #define MAXFRM_MASK 0x0000ffff
  22. #include <common.h>
  23. #include <phy.h>
  24. #include <asm/types.h>
  25. #include <asm/io.h>
  26. #include <asm/fsl_enet.h>
  27. #include <asm/fsl_memac.h>
  28. #include "fm.h"
  29. static void memac_init_mac(struct fsl_enet_mac *mac)
  30. {
  31. struct memac *regs = mac->base;
  32. /* mask all interrupt */
  33. out_be32(&regs->imask, IMASK_MASK_ALL);
  34. /* clear all events */
  35. out_be32(&regs->ievent, IEVENT_CLEAR_ALL);
  36. /* set the max receive length */
  37. out_be32(&regs->maxfrm, mac->max_rx_len & MAXFRM_MASK);
  38. /* multicast frame reception for the hash entry disable */
  39. out_be32(&regs->hashtable_ctrl, 0);
  40. }
  41. static void memac_enable_mac(struct fsl_enet_mac *mac)
  42. {
  43. struct memac *regs = mac->base;
  44. setbits_be32(&regs->command_config, MEMAC_CMD_CFG_RXTX_EN);
  45. }
  46. static void memac_disable_mac(struct fsl_enet_mac *mac)
  47. {
  48. struct memac *regs = mac->base;
  49. clrbits_be32(&regs->command_config, MEMAC_CMD_CFG_RXTX_EN);
  50. }
  51. static void memac_set_mac_addr(struct fsl_enet_mac *mac, u8 *mac_addr)
  52. {
  53. struct memac *regs = mac->base;
  54. u32 mac_addr0, mac_addr1;
  55. /*
  56. * if a station address of 0x12345678ABCD, perform a write to
  57. * MAC_ADDR0 of 0x78563412, MAC_ADDR1 of 0x0000CDAB
  58. */
  59. mac_addr0 = (mac_addr[3] << 24) | (mac_addr[2] << 16) | \
  60. (mac_addr[1] << 8) | (mac_addr[0]);
  61. out_be32(&regs->mac_addr_0, mac_addr0);
  62. mac_addr1 = ((mac_addr[5] << 8) | mac_addr[4]) & 0x0000ffff;
  63. out_be32(&regs->mac_addr_1, mac_addr1);
  64. }
  65. static void memac_set_interface_mode(struct fsl_enet_mac *mac,
  66. phy_interface_t type, int speed)
  67. {
  68. /* Roy need more work here */
  69. struct memac *regs = mac->base;
  70. u32 if_mode, if_status;
  71. /* clear all bits relative with interface mode */
  72. if_mode = in_be32(&regs->if_mode);
  73. if_status = in_be32(&regs->if_status);
  74. /* set interface mode */
  75. switch (type) {
  76. case PHY_INTERFACE_MODE_GMII:
  77. if_mode &= ~IF_MODE_MASK;
  78. if_mode |= IF_MODE_GMII;
  79. break;
  80. case PHY_INTERFACE_MODE_RGMII:
  81. if_mode |= (IF_MODE_GMII | IF_MODE_RG);
  82. break;
  83. case PHY_INTERFACE_MODE_RMII:
  84. if_mode |= (IF_MODE_GMII | IF_MODE_RM);
  85. break;
  86. case PHY_INTERFACE_MODE_SGMII:
  87. if_mode &= ~IF_MODE_MASK;
  88. if_mode |= (IF_MODE_GMII);
  89. break;
  90. default:
  91. break;
  92. }
  93. /* Enable automatic speed selection */
  94. if_mode |= IF_MODE_EN_AUTO;
  95. if (type == PHY_INTERFACE_MODE_RGMII) {
  96. if_mode &= ~IF_MODE_EN_AUTO;
  97. if_mode &= ~IF_MODE_SETSP_MASK;
  98. switch (speed) {
  99. case SPEED_1000:
  100. if_mode |= IF_MODE_SETSP_1000M;
  101. break;
  102. case SPEED_100:
  103. if_mode |= IF_MODE_SETSP_100M;
  104. break;
  105. case SPEED_10:
  106. if_mode |= IF_MODE_SETSP_10M;
  107. default:
  108. break;
  109. }
  110. }
  111. debug(" %s, if_mode = %x\n", __func__, if_mode);
  112. debug(" %s, if_status = %x\n", __func__, if_status);
  113. out_be32(&regs->if_mode, if_mode);
  114. return;
  115. }
  116. void init_memac(struct fsl_enet_mac *mac, void *base,
  117. void *phyregs, int max_rx_len)
  118. {
  119. mac->base = base;
  120. mac->phyregs = phyregs;
  121. mac->max_rx_len = max_rx_len;
  122. mac->init_mac = memac_init_mac;
  123. mac->enable_mac = memac_enable_mac;
  124. mac->disable_mac = memac_disable_mac;
  125. mac->set_mac_addr = memac_set_mac_addr;
  126. mac->set_if_mode = memac_set_interface_mode;
  127. }