emac-sgmii.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Copyright (c) 2015-2016, The Linux Foundation. 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 and
  5. * only version 2 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. /* Qualcomm Technologies, Inc. EMAC SGMII Controller driver.
  13. */
  14. #include <linux/iopoll.h>
  15. #include <linux/acpi.h>
  16. #include <linux/of_device.h>
  17. #include "emac.h"
  18. #include "emac-mac.h"
  19. #include "emac-sgmii.h"
  20. /* EMAC_SGMII register offsets */
  21. #define EMAC_SGMII_PHY_AUTONEG_CFG2 0x0048
  22. #define EMAC_SGMII_PHY_SPEED_CFG1 0x0074
  23. #define EMAC_SGMII_PHY_IRQ_CMD 0x00ac
  24. #define EMAC_SGMII_PHY_INTERRUPT_CLEAR 0x00b0
  25. #define EMAC_SGMII_PHY_INTERRUPT_STATUS 0x00b8
  26. #define FORCE_AN_TX_CFG BIT(5)
  27. #define FORCE_AN_RX_CFG BIT(4)
  28. #define AN_ENABLE BIT(0)
  29. #define DUPLEX_MODE BIT(4)
  30. #define SPDMODE_1000 BIT(1)
  31. #define SPDMODE_100 BIT(0)
  32. #define SPDMODE_10 0
  33. #define IRQ_GLOBAL_CLEAR BIT(0)
  34. #define DECODE_CODE_ERR BIT(7)
  35. #define DECODE_DISP_ERR BIT(6)
  36. #define SGMII_PHY_IRQ_CLR_WAIT_TIME 10
  37. #define SGMII_PHY_INTERRUPT_ERR (DECODE_CODE_ERR | DECODE_DISP_ERR)
  38. #define SERDES_START_WAIT_TIMES 100
  39. static int emac_sgmii_link_init(struct emac_adapter *adpt)
  40. {
  41. struct phy_device *phydev = adpt->phydev;
  42. struct emac_phy *phy = &adpt->phy;
  43. u32 val;
  44. val = readl(phy->base + EMAC_SGMII_PHY_AUTONEG_CFG2);
  45. if (phydev->autoneg == AUTONEG_ENABLE) {
  46. val &= ~(FORCE_AN_RX_CFG | FORCE_AN_TX_CFG);
  47. val |= AN_ENABLE;
  48. writel(val, phy->base + EMAC_SGMII_PHY_AUTONEG_CFG2);
  49. } else {
  50. u32 speed_cfg;
  51. switch (phydev->speed) {
  52. case SPEED_10:
  53. speed_cfg = SPDMODE_10;
  54. break;
  55. case SPEED_100:
  56. speed_cfg = SPDMODE_100;
  57. break;
  58. case SPEED_1000:
  59. speed_cfg = SPDMODE_1000;
  60. break;
  61. default:
  62. return -EINVAL;
  63. }
  64. if (phydev->duplex == DUPLEX_FULL)
  65. speed_cfg |= DUPLEX_MODE;
  66. val &= ~AN_ENABLE;
  67. writel(speed_cfg, phy->base + EMAC_SGMII_PHY_SPEED_CFG1);
  68. writel(val, phy->base + EMAC_SGMII_PHY_AUTONEG_CFG2);
  69. }
  70. return 0;
  71. }
  72. static int emac_sgmii_irq_clear(struct emac_adapter *adpt, u32 irq_bits)
  73. {
  74. struct emac_phy *phy = &adpt->phy;
  75. u32 status;
  76. writel_relaxed(irq_bits, phy->base + EMAC_SGMII_PHY_INTERRUPT_CLEAR);
  77. writel_relaxed(IRQ_GLOBAL_CLEAR, phy->base + EMAC_SGMII_PHY_IRQ_CMD);
  78. /* Ensure interrupt clear command is written to HW */
  79. wmb();
  80. /* After set the IRQ_GLOBAL_CLEAR bit, the status clearing must
  81. * be confirmed before clearing the bits in other registers.
  82. * It takes a few cycles for hw to clear the interrupt status.
  83. */
  84. if (readl_poll_timeout_atomic(phy->base +
  85. EMAC_SGMII_PHY_INTERRUPT_STATUS,
  86. status, !(status & irq_bits), 1,
  87. SGMII_PHY_IRQ_CLR_WAIT_TIME)) {
  88. netdev_err(adpt->netdev,
  89. "error: failed clear SGMII irq: status:0x%x bits:0x%x\n",
  90. status, irq_bits);
  91. return -EIO;
  92. }
  93. /* Finalize clearing procedure */
  94. writel_relaxed(0, phy->base + EMAC_SGMII_PHY_IRQ_CMD);
  95. writel_relaxed(0, phy->base + EMAC_SGMII_PHY_INTERRUPT_CLEAR);
  96. /* Ensure that clearing procedure finalization is written to HW */
  97. wmb();
  98. return 0;
  99. }
  100. static void emac_sgmii_reset_prepare(struct emac_adapter *adpt)
  101. {
  102. struct emac_phy *phy = &adpt->phy;
  103. u32 val;
  104. /* Reset PHY */
  105. val = readl(phy->base + EMAC_EMAC_WRAPPER_CSR2);
  106. writel(((val & ~PHY_RESET) | PHY_RESET), phy->base +
  107. EMAC_EMAC_WRAPPER_CSR2);
  108. /* Ensure phy-reset command is written to HW before the release cmd */
  109. msleep(50);
  110. val = readl(phy->base + EMAC_EMAC_WRAPPER_CSR2);
  111. writel((val & ~PHY_RESET), phy->base + EMAC_EMAC_WRAPPER_CSR2);
  112. /* Ensure phy-reset release command is written to HW before initializing
  113. * SGMII
  114. */
  115. msleep(50);
  116. }
  117. void emac_sgmii_reset(struct emac_adapter *adpt)
  118. {
  119. int ret;
  120. emac_sgmii_reset_prepare(adpt);
  121. ret = emac_sgmii_link_init(adpt);
  122. if (ret) {
  123. netdev_err(adpt->netdev, "unsupported link speed\n");
  124. return;
  125. }
  126. ret = adpt->phy.initialize(adpt);
  127. if (ret)
  128. netdev_err(adpt->netdev,
  129. "could not reinitialize internal PHY (error=%i)\n",
  130. ret);
  131. }
  132. static int emac_sgmii_acpi_match(struct device *dev, void *data)
  133. {
  134. #ifdef CONFIG_ACPI
  135. static const struct acpi_device_id match_table[] = {
  136. {
  137. .id = "QCOM8071",
  138. },
  139. {}
  140. };
  141. const struct acpi_device_id *id = acpi_match_device(match_table, dev);
  142. emac_sgmii_initialize *initialize = data;
  143. if (id) {
  144. acpi_handle handle = ACPI_HANDLE(dev);
  145. unsigned long long hrv;
  146. acpi_status status;
  147. status = acpi_evaluate_integer(handle, "_HRV", NULL, &hrv);
  148. if (status) {
  149. if (status == AE_NOT_FOUND)
  150. /* Older versions of the QDF2432 ACPI tables do
  151. * not have an _HRV property.
  152. */
  153. hrv = 1;
  154. else
  155. /* Something is wrong with the tables */
  156. return 0;
  157. }
  158. switch (hrv) {
  159. case 1:
  160. *initialize = emac_sgmii_init_qdf2432;
  161. return 1;
  162. case 2:
  163. *initialize = emac_sgmii_init_qdf2400;
  164. return 1;
  165. }
  166. }
  167. #endif
  168. return 0;
  169. }
  170. static const struct of_device_id emac_sgmii_dt_match[] = {
  171. {
  172. .compatible = "qcom,fsm9900-emac-sgmii",
  173. .data = emac_sgmii_init_fsm9900,
  174. },
  175. {
  176. .compatible = "qcom,qdf2432-emac-sgmii",
  177. .data = emac_sgmii_init_qdf2432,
  178. },
  179. {}
  180. };
  181. int emac_sgmii_config(struct platform_device *pdev, struct emac_adapter *adpt)
  182. {
  183. struct platform_device *sgmii_pdev = NULL;
  184. struct emac_phy *phy = &adpt->phy;
  185. struct resource *res;
  186. int ret;
  187. if (has_acpi_companion(&pdev->dev)) {
  188. struct device *dev;
  189. dev = device_find_child(&pdev->dev, &phy->initialize,
  190. emac_sgmii_acpi_match);
  191. if (!dev) {
  192. dev_err(&pdev->dev, "cannot find internal phy node\n");
  193. return -ENODEV;
  194. }
  195. sgmii_pdev = to_platform_device(dev);
  196. } else {
  197. const struct of_device_id *match;
  198. struct device_node *np;
  199. np = of_parse_phandle(pdev->dev.of_node, "internal-phy", 0);
  200. if (!np) {
  201. dev_err(&pdev->dev, "missing internal-phy property\n");
  202. return -ENODEV;
  203. }
  204. sgmii_pdev = of_find_device_by_node(np);
  205. if (!sgmii_pdev) {
  206. dev_err(&pdev->dev, "invalid internal-phy property\n");
  207. return -ENODEV;
  208. }
  209. match = of_match_device(emac_sgmii_dt_match, &sgmii_pdev->dev);
  210. if (!match) {
  211. dev_err(&pdev->dev, "unrecognized internal phy node\n");
  212. ret = -ENODEV;
  213. goto error_put_device;
  214. }
  215. phy->initialize = (emac_sgmii_initialize)match->data;
  216. }
  217. /* Base address is the first address */
  218. res = platform_get_resource(sgmii_pdev, IORESOURCE_MEM, 0);
  219. if (!res) {
  220. ret = -EINVAL;
  221. goto error_put_device;
  222. }
  223. phy->base = ioremap(res->start, resource_size(res));
  224. if (!phy->base) {
  225. ret = -ENOMEM;
  226. goto error_put_device;
  227. }
  228. /* v2 SGMII has a per-lane digital digital, so parse it if it exists */
  229. res = platform_get_resource(sgmii_pdev, IORESOURCE_MEM, 1);
  230. if (res) {
  231. phy->digital = ioremap(res->start, resource_size(res));
  232. if (!phy->digital) {
  233. ret = -ENOMEM;
  234. goto error_unmap_base;
  235. }
  236. }
  237. ret = phy->initialize(adpt);
  238. if (ret)
  239. goto error;
  240. emac_sgmii_irq_clear(adpt, SGMII_PHY_INTERRUPT_ERR);
  241. /* We've remapped the addresses, so we don't need the device any
  242. * more. of_find_device_by_node() says we should release it.
  243. */
  244. put_device(&sgmii_pdev->dev);
  245. return 0;
  246. error:
  247. if (phy->digital)
  248. iounmap(phy->digital);
  249. error_unmap_base:
  250. iounmap(phy->base);
  251. error_put_device:
  252. put_device(&sgmii_pdev->dev);
  253. return ret;
  254. }