altr_tse_pcs.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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_CTRL_AUTONEG_SGMII 0x1140
  37. #define TSE_PCS_IF_MODE_REG 0x28
  38. #define TSE_PCS_LINK_TIMER_0_REG 0x24
  39. #define TSE_PCS_LINK_TIMER_1_REG 0x26
  40. #define TSE_PCS_SIZE 0x40
  41. #define TSE_PCS_STATUS_AN_COMPLETED_MASK BIT(5)
  42. #define TSE_PCS_STATUS_LINK_MASK 0x0004
  43. #define TSE_PCS_STATUS_REG 0x02
  44. #define TSE_PCS_SGMII_SPEED_1000 BIT(3)
  45. #define TSE_PCS_SGMII_SPEED_100 BIT(2)
  46. #define TSE_PCS_SGMII_SPEED_10 0x0
  47. #define TSE_PCS_SW_RST_MASK 0x8000
  48. #define TSE_PCS_PARTNER_ABILITY_REG 0x0A
  49. #define TSE_PCS_PARTNER_DUPLEX_FULL 0x1000
  50. #define TSE_PCS_PARTNER_DUPLEX_HALF 0x0000
  51. #define TSE_PCS_PARTNER_DUPLEX_MASK 0x1000
  52. #define TSE_PCS_PARTNER_SPEED_MASK GENMASK(11, 10)
  53. #define TSE_PCS_PARTNER_SPEED_1000 BIT(11)
  54. #define TSE_PCS_PARTNER_SPEED_100 BIT(10)
  55. #define TSE_PCS_PARTNER_SPEED_10 0x0000
  56. #define TSE_PCS_PARTNER_SPEED_1000 BIT(11)
  57. #define TSE_PCS_PARTNER_SPEED_100 BIT(10)
  58. #define TSE_PCS_PARTNER_SPEED_10 0x0000
  59. #define TSE_PCS_SGMII_SPEED_MASK GENMASK(3, 2)
  60. #define TSE_PCS_SGMII_LINK_TIMER_0 0x0D40
  61. #define TSE_PCS_SGMII_LINK_TIMER_1 0x0003
  62. #define TSE_PCS_SW_RESET_TIMEOUT 100
  63. #define TSE_PCS_USE_SGMII_AN_MASK BIT(1)
  64. #define TSE_PCS_USE_SGMII_ENA BIT(0)
  65. #define TSE_PCS_IF_USE_SGMII 0x03
  66. #define SGMII_ADAPTER_CTRL_REG 0x00
  67. #define SGMII_ADAPTER_DISABLE 0x0001
  68. #define SGMII_ADAPTER_ENABLE 0x0000
  69. #define AUTONEGO_LINK_TIMER 20
  70. static int tse_pcs_reset(void __iomem *base, struct tse_pcs *pcs)
  71. {
  72. int counter = 0;
  73. u16 val;
  74. val = readw(base + TSE_PCS_CONTROL_REG);
  75. val |= TSE_PCS_SW_RST_MASK;
  76. writew(val, base + TSE_PCS_CONTROL_REG);
  77. while (counter < TSE_PCS_SW_RESET_TIMEOUT) {
  78. val = readw(base + TSE_PCS_CONTROL_REG);
  79. val &= TSE_PCS_SW_RST_MASK;
  80. if (val == 0)
  81. break;
  82. counter++;
  83. udelay(1);
  84. }
  85. if (counter >= TSE_PCS_SW_RESET_TIMEOUT) {
  86. dev_err(pcs->dev, "PCS could not get out of sw reset\n");
  87. return -ETIMEDOUT;
  88. }
  89. return 0;
  90. }
  91. int tse_pcs_init(void __iomem *base, struct tse_pcs *pcs)
  92. {
  93. int ret = 0;
  94. writew(TSE_PCS_IF_USE_SGMII, base + TSE_PCS_IF_MODE_REG);
  95. writew(TSE_PCS_CTRL_AUTONEG_SGMII, base + TSE_PCS_CONTROL_REG);
  96. writew(TSE_PCS_SGMII_LINK_TIMER_0, base + TSE_PCS_LINK_TIMER_0_REG);
  97. writew(TSE_PCS_SGMII_LINK_TIMER_1, base + TSE_PCS_LINK_TIMER_1_REG);
  98. ret = tse_pcs_reset(base, pcs);
  99. if (ret == 0)
  100. writew(SGMII_ADAPTER_ENABLE,
  101. pcs->sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
  102. return ret;
  103. }
  104. static void pcs_link_timer_callback(struct tse_pcs *pcs)
  105. {
  106. u16 val = 0;
  107. void __iomem *tse_pcs_base = pcs->tse_pcs_base;
  108. void __iomem *sgmii_adapter_base = pcs->sgmii_adapter_base;
  109. val = readw(tse_pcs_base + TSE_PCS_STATUS_REG);
  110. val &= TSE_PCS_STATUS_LINK_MASK;
  111. if (val != 0) {
  112. dev_dbg(pcs->dev, "Adapter: Link is established\n");
  113. writew(SGMII_ADAPTER_ENABLE,
  114. sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
  115. } else {
  116. mod_timer(&pcs->aneg_link_timer, jiffies +
  117. msecs_to_jiffies(AUTONEGO_LINK_TIMER));
  118. }
  119. }
  120. static void auto_nego_timer_callback(struct tse_pcs *pcs)
  121. {
  122. u16 val = 0;
  123. u16 speed = 0;
  124. u16 duplex = 0;
  125. void __iomem *tse_pcs_base = pcs->tse_pcs_base;
  126. void __iomem *sgmii_adapter_base = pcs->sgmii_adapter_base;
  127. val = readw(tse_pcs_base + TSE_PCS_STATUS_REG);
  128. val &= TSE_PCS_STATUS_AN_COMPLETED_MASK;
  129. if (val != 0) {
  130. dev_dbg(pcs->dev, "Adapter: Auto Negotiation is completed\n");
  131. val = readw(tse_pcs_base + TSE_PCS_PARTNER_ABILITY_REG);
  132. speed = val & TSE_PCS_PARTNER_SPEED_MASK;
  133. duplex = val & TSE_PCS_PARTNER_DUPLEX_MASK;
  134. if (speed == TSE_PCS_PARTNER_SPEED_10 &&
  135. duplex == TSE_PCS_PARTNER_DUPLEX_FULL)
  136. dev_dbg(pcs->dev,
  137. "Adapter: Link Partner is Up - 10/Full\n");
  138. else if (speed == TSE_PCS_PARTNER_SPEED_100 &&
  139. duplex == TSE_PCS_PARTNER_DUPLEX_FULL)
  140. dev_dbg(pcs->dev,
  141. "Adapter: Link Partner is Up - 100/Full\n");
  142. else if (speed == TSE_PCS_PARTNER_SPEED_1000 &&
  143. duplex == TSE_PCS_PARTNER_DUPLEX_FULL)
  144. dev_dbg(pcs->dev,
  145. "Adapter: Link Partner is Up - 1000/Full\n");
  146. else if (speed == TSE_PCS_PARTNER_SPEED_10 &&
  147. duplex == TSE_PCS_PARTNER_DUPLEX_HALF)
  148. dev_err(pcs->dev,
  149. "Adapter does not support Half Duplex\n");
  150. else if (speed == TSE_PCS_PARTNER_SPEED_100 &&
  151. duplex == TSE_PCS_PARTNER_DUPLEX_HALF)
  152. dev_err(pcs->dev,
  153. "Adapter does not support Half Duplex\n");
  154. else if (speed == TSE_PCS_PARTNER_SPEED_1000 &&
  155. duplex == TSE_PCS_PARTNER_DUPLEX_HALF)
  156. dev_err(pcs->dev,
  157. "Adapter does not support Half Duplex\n");
  158. else
  159. dev_err(pcs->dev,
  160. "Adapter: Invalid Partner Speed and Duplex\n");
  161. if (duplex == TSE_PCS_PARTNER_DUPLEX_FULL &&
  162. (speed == TSE_PCS_PARTNER_SPEED_10 ||
  163. speed == TSE_PCS_PARTNER_SPEED_100 ||
  164. speed == TSE_PCS_PARTNER_SPEED_1000))
  165. writew(SGMII_ADAPTER_ENABLE,
  166. sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
  167. } else {
  168. val = readw(tse_pcs_base + TSE_PCS_CONTROL_REG);
  169. val |= TSE_PCS_CONTROL_RESTART_AN_MASK;
  170. writew(val, tse_pcs_base + TSE_PCS_CONTROL_REG);
  171. tse_pcs_reset(tse_pcs_base, pcs);
  172. mod_timer(&pcs->aneg_link_timer, jiffies +
  173. msecs_to_jiffies(AUTONEGO_LINK_TIMER));
  174. }
  175. }
  176. static void aneg_link_timer_callback(struct timer_list *t)
  177. {
  178. struct tse_pcs *pcs = from_timer(pcs, t, aneg_link_timer);
  179. if (pcs->autoneg == AUTONEG_ENABLE)
  180. auto_nego_timer_callback(pcs);
  181. else if (pcs->autoneg == AUTONEG_DISABLE)
  182. pcs_link_timer_callback(pcs);
  183. }
  184. void tse_pcs_fix_mac_speed(struct tse_pcs *pcs, struct phy_device *phy_dev,
  185. unsigned int speed)
  186. {
  187. void __iomem *tse_pcs_base = pcs->tse_pcs_base;
  188. void __iomem *sgmii_adapter_base = pcs->sgmii_adapter_base;
  189. u32 val;
  190. writew(SGMII_ADAPTER_ENABLE,
  191. sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
  192. pcs->autoneg = phy_dev->autoneg;
  193. if (phy_dev->autoneg == AUTONEG_ENABLE) {
  194. val = readw(tse_pcs_base + TSE_PCS_CONTROL_REG);
  195. val |= TSE_PCS_CONTROL_AN_EN_MASK;
  196. writew(val, tse_pcs_base + TSE_PCS_CONTROL_REG);
  197. val = readw(tse_pcs_base + TSE_PCS_IF_MODE_REG);
  198. val |= TSE_PCS_USE_SGMII_AN_MASK;
  199. writew(val, tse_pcs_base + TSE_PCS_IF_MODE_REG);
  200. val = readw(tse_pcs_base + TSE_PCS_CONTROL_REG);
  201. val |= TSE_PCS_CONTROL_RESTART_AN_MASK;
  202. tse_pcs_reset(tse_pcs_base, pcs);
  203. timer_setup(&pcs->aneg_link_timer, aneg_link_timer_callback,
  204. 0);
  205. mod_timer(&pcs->aneg_link_timer, jiffies +
  206. msecs_to_jiffies(AUTONEGO_LINK_TIMER));
  207. } else if (phy_dev->autoneg == AUTONEG_DISABLE) {
  208. val = readw(tse_pcs_base + TSE_PCS_CONTROL_REG);
  209. val &= ~TSE_PCS_CONTROL_AN_EN_MASK;
  210. writew(val, tse_pcs_base + TSE_PCS_CONTROL_REG);
  211. val = readw(tse_pcs_base + TSE_PCS_IF_MODE_REG);
  212. val &= ~TSE_PCS_USE_SGMII_AN_MASK;
  213. writew(val, tse_pcs_base + TSE_PCS_IF_MODE_REG);
  214. val = readw(tse_pcs_base + TSE_PCS_IF_MODE_REG);
  215. val &= ~TSE_PCS_SGMII_SPEED_MASK;
  216. switch (speed) {
  217. case 1000:
  218. val |= TSE_PCS_SGMII_SPEED_1000;
  219. break;
  220. case 100:
  221. val |= TSE_PCS_SGMII_SPEED_100;
  222. break;
  223. case 10:
  224. val |= TSE_PCS_SGMII_SPEED_10;
  225. break;
  226. default:
  227. return;
  228. }
  229. writew(val, tse_pcs_base + TSE_PCS_IF_MODE_REG);
  230. tse_pcs_reset(tse_pcs_base, pcs);
  231. timer_setup(&pcs->aneg_link_timer, aneg_link_timer_callback,
  232. 0);
  233. mod_timer(&pcs->aneg_link_timer, jiffies +
  234. msecs_to_jiffies(AUTONEGO_LINK_TIMER));
  235. }
  236. }