dp83848.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * National Semiconductor DP83848 PHY Driver for TI DaVinci
  3. * (TMS320DM644x) based boards.
  4. *
  5. * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
  6. *
  7. * --------------------------------------------------------
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <net.h>
  29. #include <dp83848.h>
  30. #include <asm/arch/emac_defs.h>
  31. #include "../../../../../drivers/net/davinci_emac.h"
  32. #ifdef CONFIG_DRIVER_TI_EMAC
  33. #ifdef CONFIG_CMD_NET
  34. int dp83848_is_phy_connected(int phy_addr)
  35. {
  36. u_int16_t id1, id2;
  37. if (!davinci_eth_phy_read(phy_addr, DP83848_PHYID1_REG, &id1))
  38. return(0);
  39. if (!davinci_eth_phy_read(phy_addr, DP83848_PHYID2_REG, &id2))
  40. return(0);
  41. if ((id1 == DP83848_PHYID1_OUI) && (id2 == DP83848_PHYID2_OUI))
  42. return(1);
  43. return(0);
  44. }
  45. int dp83848_get_link_speed(int phy_addr)
  46. {
  47. u_int16_t tmp;
  48. volatile emac_regs* emac = (emac_regs *)EMAC_BASE_ADDR;
  49. if (!davinci_eth_phy_read(phy_addr, DP83848_STAT_REG, &tmp))
  50. return(0);
  51. if (!(tmp & DP83848_LINK_STATUS)) /* link up? */
  52. return(0);
  53. if (!davinci_eth_phy_read(phy_addr, DP83848_PHY_STAT_REG, &tmp))
  54. return(0);
  55. /* Speed doesn't matter, there is no setting for it in EMAC... */
  56. if (tmp & DP83848_DUPLEX) {
  57. /* set DM644x EMAC for Full Duplex */
  58. emac->MACCONTROL = EMAC_MACCONTROL_MIIEN_ENABLE |
  59. EMAC_MACCONTROL_FULLDUPLEX_ENABLE;
  60. } else {
  61. /*set DM644x EMAC for Half Duplex */
  62. emac->MACCONTROL = EMAC_MACCONTROL_MIIEN_ENABLE;
  63. }
  64. return(1);
  65. }
  66. int dp83848_init_phy(int phy_addr)
  67. {
  68. int ret = 1;
  69. if (!dp83848_get_link_speed(phy_addr)) {
  70. /* Try another time */
  71. udelay(100000);
  72. ret = dp83848_get_link_speed(phy_addr);
  73. }
  74. /* Disable PHY Interrupts */
  75. davinci_eth_phy_write(phy_addr, DP83848_PHY_INTR_CTRL_REG, 0);
  76. return(ret);
  77. }
  78. int dp83848_auto_negotiate(int phy_addr)
  79. {
  80. u_int16_t tmp;
  81. if (!davinci_eth_phy_read(phy_addr, DP83848_CTL_REG, &tmp))
  82. return(0);
  83. /* Restart Auto_negotiation */
  84. tmp &= ~DP83848_AUTONEG; /* remove autonegotiation enable */
  85. tmp |= DP83848_ISOLATE; /* Electrically isolate PHY */
  86. davinci_eth_phy_write(phy_addr, DP83848_CTL_REG, tmp);
  87. /* Set the Auto_negotiation Advertisement Register
  88. * MII advertising for Next page, 100BaseTxFD and HD,
  89. * 10BaseTFD and HD, IEEE 802.3
  90. */
  91. tmp = DP83848_NP | DP83848_TX_FDX | DP83848_TX_HDX |
  92. DP83848_10_FDX | DP83848_10_HDX | DP83848_AN_IEEE_802_3;
  93. davinci_eth_phy_write(phy_addr, DP83848_ANA_REG, tmp);
  94. /* Read Control Register */
  95. if (!davinci_eth_phy_read(phy_addr, DP83848_CTL_REG, &tmp))
  96. return(0);
  97. tmp |= DP83848_SPEED_SELECT | DP83848_AUTONEG | DP83848_DUPLEX_MODE;
  98. davinci_eth_phy_write(phy_addr, DP83848_CTL_REG, tmp);
  99. /* Restart Auto_negotiation */
  100. tmp |= DP83848_RESTART_AUTONEG;
  101. davinci_eth_phy_write(phy_addr, DP83848_CTL_REG, tmp);
  102. /*check AutoNegotiate complete */
  103. udelay(10000);
  104. if (!davinci_eth_phy_read(phy_addr, DP83848_STAT_REG, &tmp))
  105. return(0);
  106. if (!(tmp & DP83848_AUTONEG_COMP))
  107. return(0);
  108. return (dp83848_get_link_speed(phy_addr));
  109. }
  110. #endif /* CONFIG_CMD_NET */
  111. #endif /* CONFIG_DRIVER_ETHER */