altr_tse_pcs.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* Copyright Altera Corporation (C) 2016. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License, version 2,
  5. * as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * Author: Tien Hock Loh <thloh@altera.com>
  16. */
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_net.h>
  21. #include <linux/phy.h>
  22. #include <linux/regmap.h>
  23. #include <linux/reset.h>
  24. #include <linux/stmmac.h>
  25. #include "stmmac.h"
  26. #include "stmmac_platform.h"
  27. #include "altr_tse_pcs.h"
  28. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII 0
  29. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII BIT(1)
  30. #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII BIT(2)
  31. #define SYSMGR_EMACGRP_CTRL_PHYSEL_WIDTH 2
  32. #define SYSMGR_EMACGRP_CTRL_PHYSEL_MASK GENMASK(1, 0)
  33. #define TSE_PCS_CONTROL_AN_EN_MASK BIT(12)
  34. #define TSE_PCS_CONTROL_REG 0x00
  35. #define TSE_PCS_CONTROL_RESTART_AN_MASK BIT(9)
  36. #define TSE_PCS_IF_MODE_REG 0x28
  37. #define TSE_PCS_LINK_TIMER_0_REG 0x24
  38. #define TSE_PCS_LINK_TIMER_1_REG 0x26
  39. #define TSE_PCS_SIZE 0x40
  40. #define TSE_PCS_STATUS_AN_COMPLETED_MASK BIT(5)
  41. #define TSE_PCS_STATUS_LINK_MASK 0x0004
  42. #define TSE_PCS_STATUS_REG 0x02
  43. #define TSE_PCS_SGMII_SPEED_1000 BIT(3)
  44. #define TSE_PCS_SGMII_SPEED_100 BIT(2)
  45. #define TSE_PCS_SGMII_SPEED_10 0x0
  46. #define TSE_PCS_SW_RST_MASK 0x8000
  47. #define TSE_PCS_PARTNER_ABILITY_REG 0x0A
  48. #define TSE_PCS_PARTNER_DUPLEX_FULL 0x1000
  49. #define TSE_PCS_PARTNER_DUPLEX_HALF 0x0000
  50. #define TSE_PCS_PARTNER_DUPLEX_MASK 0x1000
  51. #define TSE_PCS_PARTNER_SPEED_MASK GENMASK(11, 10)
  52. #define TSE_PCS_PARTNER_SPEED_1000 BIT(11)
  53. #define TSE_PCS_PARTNER_SPEED_100 BIT(10)
  54. #define TSE_PCS_PARTNER_SPEED_10 0x0000
  55. #define TSE_PCS_PARTNER_SPEED_1000 BIT(11)
  56. #define TSE_PCS_PARTNER_SPEED_100 BIT(10)
  57. #define TSE_PCS_PARTNER_SPEED_10 0x0000
  58. #define TSE_PCS_SGMII_SPEED_MASK GENMASK(3, 2)
  59. #define TSE_PCS_SGMII_LINK_TIMER_0 0x0D40
  60. #define TSE_PCS_SGMII_LINK_TIMER_1 0x0003
  61. #define TSE_PCS_SW_RESET_TIMEOUT 100
  62. #define TSE_PCS_USE_SGMII_AN_MASK BIT(1)
  63. #define TSE_PCS_USE_SGMII_ENA BIT(0)
  64. #define SGMII_ADAPTER_CTRL_REG 0x00
  65. #define SGMII_ADAPTER_DISABLE 0x0001
  66. #define SGMII_ADAPTER_ENABLE 0x0000
  67. #define AUTONEGO_LINK_TIMER 20
  68. static int tse_pcs_reset(void __iomem *base, struct tse_pcs *pcs)
  69. {
  70. int counter = 0;
  71. u16 val;
  72. val = readw(base + TSE_PCS_CONTROL_REG);
  73. val |= TSE_PCS_SW_RST_MASK;
  74. writew(val, base + TSE_PCS_CONTROL_REG);
  75. while (counter < TSE_PCS_SW_RESET_TIMEOUT) {
  76. val = readw(base + TSE_PCS_CONTROL_REG);
  77. val &= TSE_PCS_SW_RST_MASK;
  78. if (val == 0)
  79. break;
  80. counter++;
  81. udelay(1);
  82. }
  83. if (counter >= TSE_PCS_SW_RESET_TIMEOUT) {
  84. dev_err(pcs->dev, "PCS could not get out of sw reset\n");
  85. return -ETIMEDOUT;
  86. }
  87. return 0;
  88. }
  89. int tse_pcs_init(void __iomem *base, struct tse_pcs *pcs)
  90. {
  91. int ret = 0;
  92. writew(TSE_PCS_USE_SGMII_ENA, base + TSE_PCS_IF_MODE_REG);
  93. writew(TSE_PCS_SGMII_LINK_TIMER_0, base + TSE_PCS_LINK_TIMER_0_REG);
  94. writew(TSE_PCS_SGMII_LINK_TIMER_1, base + TSE_PCS_LINK_TIMER_1_REG);
  95. ret = tse_pcs_reset(base, pcs);
  96. if (ret == 0)
  97. writew(SGMII_ADAPTER_ENABLE,
  98. pcs->sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
  99. return ret;
  100. }
  101. static void pcs_link_timer_callback(unsigned long data)
  102. {
  103. u16 val = 0;
  104. struct tse_pcs *pcs = (struct tse_pcs *)data;
  105. void __iomem *tse_pcs_base = pcs->tse_pcs_base;
  106. void __iomem *sgmii_adapter_base = pcs->sgmii_adapter_base;
  107. val = readw(tse_pcs_base + TSE_PCS_STATUS_REG);
  108. val &= TSE_PCS_STATUS_LINK_MASK;
  109. if (val != 0) {
  110. dev_dbg(pcs->dev, "Adapter: Link is established\n");
  111. writew(SGMII_ADAPTER_ENABLE,
  112. sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
  113. } else {
  114. mod_timer(&pcs->aneg_link_timer, jiffies +
  115. msecs_to_jiffies(AUTONEGO_LINK_TIMER));
  116. }
  117. }
  118. static void auto_nego_timer_callback(unsigned long data)
  119. {
  120. u16 val = 0;
  121. u16 speed = 0;
  122. u16 duplex = 0;
  123. struct tse_pcs *pcs = (struct tse_pcs *)data;
  124. void __iomem *tse_pcs_base = pcs->tse_pcs_base;
  125. void __iomem *sgmii_adapter_base = pcs->sgmii_adapter_base;
  126. val = readw(tse_pcs_base + TSE_PCS_STATUS_REG);
  127. val &= TSE_PCS_STATUS_AN_COMPLETED_MASK;
  128. if (val != 0) {
  129. dev_dbg(pcs->dev, "Adapter: Auto Negotiation is completed\n");
  130. val = readw(tse_pcs_base + TSE_PCS_PARTNER_ABILITY_REG);
  131. speed = val & TSE_PCS_PARTNER_SPEED_MASK;
  132. duplex = val & TSE_PCS_PARTNER_DUPLEX_MASK;
  133. if (speed == TSE_PCS_PARTNER_SPEED_10 &&
  134. duplex == TSE_PCS_PARTNER_DUPLEX_FULL)
  135. dev_dbg(pcs->dev,
  136. "Adapter: Link Partner is Up - 10/Full\n");
  137. else if (speed == TSE_PCS_PARTNER_SPEED_100 &&
  138. duplex == TSE_PCS_PARTNER_DUPLEX_FULL)
  139. dev_dbg(pcs->dev,
  140. "Adapter: Link Partner is Up - 100/Full\n");
  141. else if (speed == TSE_PCS_PARTNER_SPEED_1000 &&
  142. duplex == TSE_PCS_PARTNER_DUPLEX_FULL)
  143. dev_dbg(pcs->dev,
  144. "Adapter: Link Partner is Up - 1000/Full\n");
  145. else if (speed == TSE_PCS_PARTNER_SPEED_10 &&
  146. duplex == TSE_PCS_PARTNER_DUPLEX_HALF)
  147. dev_err(pcs->dev,
  148. "Adapter does not support Half Duplex\n");
  149. else if (speed == TSE_PCS_PARTNER_SPEED_100 &&
  150. duplex == TSE_PCS_PARTNER_DUPLEX_HALF)
  151. dev_err(pcs->dev,
  152. "Adapter does not support Half Duplex\n");
  153. else if (speed == TSE_PCS_PARTNER_SPEED_1000 &&
  154. duplex == TSE_PCS_PARTNER_DUPLEX_HALF)
  155. dev_err(pcs->dev,
  156. "Adapter does not support Half Duplex\n");
  157. else
  158. dev_err(pcs->dev,
  159. "Adapter: Invalid Partner Speed and Duplex\n");
  160. if (duplex == TSE_PCS_PARTNER_DUPLEX_FULL &&
  161. (speed == TSE_PCS_PARTNER_SPEED_10 ||
  162. speed == TSE_PCS_PARTNER_SPEED_100 ||
  163. speed == TSE_PCS_PARTNER_SPEED_1000))
  164. writew(SGMII_ADAPTER_ENABLE,
  165. sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
  166. } else {
  167. val = readw(tse_pcs_base + TSE_PCS_CONTROL_REG);
  168. val |= TSE_PCS_CONTROL_RESTART_AN_MASK;
  169. writew(val, tse_pcs_base + TSE_PCS_CONTROL_REG);
  170. tse_pcs_reset(tse_pcs_base, pcs);
  171. mod_timer(&pcs->aneg_link_timer, jiffies +
  172. msecs_to_jiffies(AUTONEGO_LINK_TIMER));
  173. }
  174. }
  175. static void aneg_link_timer_callback(unsigned long data)
  176. {
  177. struct tse_pcs *pcs = (struct tse_pcs *)data;
  178. if (pcs->autoneg == AUTONEG_ENABLE)
  179. auto_nego_timer_callback(data);
  180. else if (pcs->autoneg == AUTONEG_DISABLE)
  181. pcs_link_timer_callback(data);
  182. }
  183. void tse_pcs_fix_mac_speed(struct tse_pcs *pcs, struct phy_device *phy_dev,
  184. unsigned int speed)
  185. {
  186. void __iomem *tse_pcs_base = pcs->tse_pcs_base;
  187. void __iomem *sgmii_adapter_base = pcs->sgmii_adapter_base;
  188. u32 val;
  189. writew(SGMII_ADAPTER_ENABLE,
  190. sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
  191. pcs->autoneg = phy_dev->autoneg;
  192. if (phy_dev->autoneg == AUTONEG_ENABLE) {
  193. val = readw(tse_pcs_base + TSE_PCS_CONTROL_REG);
  194. val |= TSE_PCS_CONTROL_AN_EN_MASK;
  195. writew(val, tse_pcs_base + TSE_PCS_CONTROL_REG);
  196. val = readw(tse_pcs_base + TSE_PCS_IF_MODE_REG);
  197. val |= TSE_PCS_USE_SGMII_AN_MASK;
  198. writew(val, tse_pcs_base + TSE_PCS_IF_MODE_REG);
  199. val = readw(tse_pcs_base + TSE_PCS_CONTROL_REG);
  200. val |= TSE_PCS_CONTROL_RESTART_AN_MASK;
  201. tse_pcs_reset(tse_pcs_base, pcs);
  202. setup_timer(&pcs->aneg_link_timer,
  203. aneg_link_timer_callback, (unsigned long)pcs);
  204. mod_timer(&pcs->aneg_link_timer, jiffies +
  205. msecs_to_jiffies(AUTONEGO_LINK_TIMER));
  206. } else if (phy_dev->autoneg == AUTONEG_DISABLE) {
  207. val = readw(tse_pcs_base + TSE_PCS_CONTROL_REG);
  208. val &= ~TSE_PCS_CONTROL_AN_EN_MASK;
  209. writew(val, tse_pcs_base + TSE_PCS_CONTROL_REG);
  210. val = readw(tse_pcs_base + TSE_PCS_IF_MODE_REG);
  211. val &= ~TSE_PCS_USE_SGMII_AN_MASK;
  212. writew(val, tse_pcs_base + TSE_PCS_IF_MODE_REG);
  213. val = readw(tse_pcs_base + TSE_PCS_IF_MODE_REG);
  214. val &= ~TSE_PCS_SGMII_SPEED_MASK;
  215. switch (speed) {
  216. case 1000:
  217. val |= TSE_PCS_SGMII_SPEED_1000;
  218. break;
  219. case 100:
  220. val |= TSE_PCS_SGMII_SPEED_100;
  221. break;
  222. case 10:
  223. val |= TSE_PCS_SGMII_SPEED_10;
  224. break;
  225. default:
  226. return;
  227. }
  228. writew(val, tse_pcs_base + TSE_PCS_IF_MODE_REG);
  229. tse_pcs_reset(tse_pcs_base, pcs);
  230. setup_timer(&pcs->aneg_link_timer,
  231. aneg_link_timer_callback, (unsigned long)pcs);
  232. mod_timer(&pcs->aneg_link_timer, jiffies +
  233. msecs_to_jiffies(AUTONEGO_LINK_TIMER));
  234. }
  235. }