altr_tse_pcs.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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(unsigned long data)
  105. {
  106. u16 val = 0;
  107. struct tse_pcs *pcs = (struct tse_pcs *)data;
  108. void __iomem *tse_pcs_base = pcs->tse_pcs_base;
  109. void __iomem *sgmii_adapter_base = pcs->sgmii_adapter_base;
  110. val = readw(tse_pcs_base + TSE_PCS_STATUS_REG);
  111. val &= TSE_PCS_STATUS_LINK_MASK;
  112. if (val != 0) {
  113. dev_dbg(pcs->dev, "Adapter: Link is established\n");
  114. writew(SGMII_ADAPTER_ENABLE,
  115. sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
  116. } else {
  117. mod_timer(&pcs->aneg_link_timer, jiffies +
  118. msecs_to_jiffies(AUTONEGO_LINK_TIMER));
  119. }
  120. }
  121. static void auto_nego_timer_callback(unsigned long data)
  122. {
  123. u16 val = 0;
  124. u16 speed = 0;
  125. u16 duplex = 0;
  126. struct tse_pcs *pcs = (struct tse_pcs *)data;
  127. void __iomem *tse_pcs_base = pcs->tse_pcs_base;
  128. void __iomem *sgmii_adapter_base = pcs->sgmii_adapter_base;
  129. val = readw(tse_pcs_base + TSE_PCS_STATUS_REG);
  130. val &= TSE_PCS_STATUS_AN_COMPLETED_MASK;
  131. if (val != 0) {
  132. dev_dbg(pcs->dev, "Adapter: Auto Negotiation is completed\n");
  133. val = readw(tse_pcs_base + TSE_PCS_PARTNER_ABILITY_REG);
  134. speed = val & TSE_PCS_PARTNER_SPEED_MASK;
  135. duplex = val & TSE_PCS_PARTNER_DUPLEX_MASK;
  136. if (speed == TSE_PCS_PARTNER_SPEED_10 &&
  137. duplex == TSE_PCS_PARTNER_DUPLEX_FULL)
  138. dev_dbg(pcs->dev,
  139. "Adapter: Link Partner is Up - 10/Full\n");
  140. else if (speed == TSE_PCS_PARTNER_SPEED_100 &&
  141. duplex == TSE_PCS_PARTNER_DUPLEX_FULL)
  142. dev_dbg(pcs->dev,
  143. "Adapter: Link Partner is Up - 100/Full\n");
  144. else if (speed == TSE_PCS_PARTNER_SPEED_1000 &&
  145. duplex == TSE_PCS_PARTNER_DUPLEX_FULL)
  146. dev_dbg(pcs->dev,
  147. "Adapter: Link Partner is Up - 1000/Full\n");
  148. else if (speed == TSE_PCS_PARTNER_SPEED_10 &&
  149. duplex == TSE_PCS_PARTNER_DUPLEX_HALF)
  150. dev_err(pcs->dev,
  151. "Adapter does not support Half Duplex\n");
  152. else if (speed == TSE_PCS_PARTNER_SPEED_100 &&
  153. duplex == TSE_PCS_PARTNER_DUPLEX_HALF)
  154. dev_err(pcs->dev,
  155. "Adapter does not support Half Duplex\n");
  156. else if (speed == TSE_PCS_PARTNER_SPEED_1000 &&
  157. duplex == TSE_PCS_PARTNER_DUPLEX_HALF)
  158. dev_err(pcs->dev,
  159. "Adapter does not support Half Duplex\n");
  160. else
  161. dev_err(pcs->dev,
  162. "Adapter: Invalid Partner Speed and Duplex\n");
  163. if (duplex == TSE_PCS_PARTNER_DUPLEX_FULL &&
  164. (speed == TSE_PCS_PARTNER_SPEED_10 ||
  165. speed == TSE_PCS_PARTNER_SPEED_100 ||
  166. speed == TSE_PCS_PARTNER_SPEED_1000))
  167. writew(SGMII_ADAPTER_ENABLE,
  168. sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
  169. } else {
  170. val = readw(tse_pcs_base + TSE_PCS_CONTROL_REG);
  171. val |= TSE_PCS_CONTROL_RESTART_AN_MASK;
  172. writew(val, tse_pcs_base + TSE_PCS_CONTROL_REG);
  173. tse_pcs_reset(tse_pcs_base, pcs);
  174. mod_timer(&pcs->aneg_link_timer, jiffies +
  175. msecs_to_jiffies(AUTONEGO_LINK_TIMER));
  176. }
  177. }
  178. static void aneg_link_timer_callback(unsigned long data)
  179. {
  180. struct tse_pcs *pcs = (struct tse_pcs *)data;
  181. if (pcs->autoneg == AUTONEG_ENABLE)
  182. auto_nego_timer_callback(data);
  183. else if (pcs->autoneg == AUTONEG_DISABLE)
  184. pcs_link_timer_callback(data);
  185. }
  186. void tse_pcs_fix_mac_speed(struct tse_pcs *pcs, struct phy_device *phy_dev,
  187. unsigned int speed)
  188. {
  189. void __iomem *tse_pcs_base = pcs->tse_pcs_base;
  190. void __iomem *sgmii_adapter_base = pcs->sgmii_adapter_base;
  191. u32 val;
  192. writew(SGMII_ADAPTER_ENABLE,
  193. sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG);
  194. pcs->autoneg = phy_dev->autoneg;
  195. if (phy_dev->autoneg == AUTONEG_ENABLE) {
  196. val = readw(tse_pcs_base + TSE_PCS_CONTROL_REG);
  197. val |= TSE_PCS_CONTROL_AN_EN_MASK;
  198. writew(val, tse_pcs_base + TSE_PCS_CONTROL_REG);
  199. val = readw(tse_pcs_base + TSE_PCS_IF_MODE_REG);
  200. val |= TSE_PCS_USE_SGMII_AN_MASK;
  201. writew(val, tse_pcs_base + TSE_PCS_IF_MODE_REG);
  202. val = readw(tse_pcs_base + TSE_PCS_CONTROL_REG);
  203. val |= TSE_PCS_CONTROL_RESTART_AN_MASK;
  204. tse_pcs_reset(tse_pcs_base, pcs);
  205. setup_timer(&pcs->aneg_link_timer,
  206. aneg_link_timer_callback, (unsigned long)pcs);
  207. mod_timer(&pcs->aneg_link_timer, jiffies +
  208. msecs_to_jiffies(AUTONEGO_LINK_TIMER));
  209. } else if (phy_dev->autoneg == AUTONEG_DISABLE) {
  210. val = readw(tse_pcs_base + TSE_PCS_CONTROL_REG);
  211. val &= ~TSE_PCS_CONTROL_AN_EN_MASK;
  212. writew(val, tse_pcs_base + TSE_PCS_CONTROL_REG);
  213. val = readw(tse_pcs_base + TSE_PCS_IF_MODE_REG);
  214. val &= ~TSE_PCS_USE_SGMII_AN_MASK;
  215. writew(val, tse_pcs_base + TSE_PCS_IF_MODE_REG);
  216. val = readw(tse_pcs_base + TSE_PCS_IF_MODE_REG);
  217. val &= ~TSE_PCS_SGMII_SPEED_MASK;
  218. switch (speed) {
  219. case 1000:
  220. val |= TSE_PCS_SGMII_SPEED_1000;
  221. break;
  222. case 100:
  223. val |= TSE_PCS_SGMII_SPEED_100;
  224. break;
  225. case 10:
  226. val |= TSE_PCS_SGMII_SPEED_10;
  227. break;
  228. default:
  229. return;
  230. }
  231. writew(val, tse_pcs_base + TSE_PCS_IF_MODE_REG);
  232. tse_pcs_reset(tse_pcs_base, pcs);
  233. setup_timer(&pcs->aneg_link_timer,
  234. aneg_link_timer_callback, (unsigned long)pcs);
  235. mod_timer(&pcs->aneg_link_timer, jiffies +
  236. msecs_to_jiffies(AUTONEGO_LINK_TIMER));
  237. }
  238. }