emac-sgmii.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
  3. */
  4. /* Qualcomm Technologies, Inc. EMAC SGMII Controller driver.
  5. */
  6. #include <linux/interrupt.h>
  7. #include <linux/iopoll.h>
  8. #include <linux/acpi.h>
  9. #include <linux/of_device.h>
  10. #include "emac.h"
  11. #include "emac-mac.h"
  12. #include "emac-sgmii.h"
  13. /* EMAC_SGMII register offsets */
  14. #define EMAC_SGMII_PHY_AUTONEG_CFG2 0x0048
  15. #define EMAC_SGMII_PHY_SPEED_CFG1 0x0074
  16. #define EMAC_SGMII_PHY_IRQ_CMD 0x00ac
  17. #define EMAC_SGMII_PHY_INTERRUPT_CLEAR 0x00b0
  18. #define EMAC_SGMII_PHY_INTERRUPT_MASK 0x00b4
  19. #define EMAC_SGMII_PHY_INTERRUPT_STATUS 0x00b8
  20. #define EMAC_SGMII_PHY_RX_CHK_STATUS 0x00d4
  21. #define FORCE_AN_TX_CFG BIT(5)
  22. #define FORCE_AN_RX_CFG BIT(4)
  23. #define AN_ENABLE BIT(0)
  24. #define DUPLEX_MODE BIT(4)
  25. #define SPDMODE_1000 BIT(1)
  26. #define SPDMODE_100 BIT(0)
  27. #define SPDMODE_10 0
  28. #define CDR_ALIGN_DET BIT(6)
  29. #define IRQ_GLOBAL_CLEAR BIT(0)
  30. #define DECODE_CODE_ERR BIT(7)
  31. #define DECODE_DISP_ERR BIT(6)
  32. #define SGMII_PHY_IRQ_CLR_WAIT_TIME 10
  33. #define SGMII_PHY_INTERRUPT_ERR (DECODE_CODE_ERR | DECODE_DISP_ERR)
  34. #define SGMII_ISR_MASK (SGMII_PHY_INTERRUPT_ERR)
  35. #define SERDES_START_WAIT_TIMES 100
  36. int emac_sgmii_init(struct emac_adapter *adpt)
  37. {
  38. if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->init))
  39. return 0;
  40. return adpt->phy.sgmii_ops->init(adpt);
  41. }
  42. int emac_sgmii_open(struct emac_adapter *adpt)
  43. {
  44. if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->open))
  45. return 0;
  46. return adpt->phy.sgmii_ops->open(adpt);
  47. }
  48. void emac_sgmii_close(struct emac_adapter *adpt)
  49. {
  50. if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->close))
  51. return;
  52. adpt->phy.sgmii_ops->close(adpt);
  53. }
  54. int emac_sgmii_link_change(struct emac_adapter *adpt, bool link_state)
  55. {
  56. if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->link_change))
  57. return 0;
  58. return adpt->phy.sgmii_ops->link_change(adpt, link_state);
  59. }
  60. void emac_sgmii_reset(struct emac_adapter *adpt)
  61. {
  62. if (!(adpt->phy.sgmii_ops && adpt->phy.sgmii_ops->reset))
  63. return;
  64. adpt->phy.sgmii_ops->reset(adpt);
  65. }
  66. /* Initialize the SGMII link between the internal and external PHYs. */
  67. static void emac_sgmii_link_init(struct emac_adapter *adpt)
  68. {
  69. struct emac_sgmii *phy = &adpt->phy;
  70. u32 val;
  71. /* Always use autonegotiation. It works no matter how the external
  72. * PHY is configured.
  73. */
  74. val = readl(phy->base + EMAC_SGMII_PHY_AUTONEG_CFG2);
  75. val &= ~(FORCE_AN_RX_CFG | FORCE_AN_TX_CFG);
  76. val |= AN_ENABLE;
  77. writel(val, phy->base + EMAC_SGMII_PHY_AUTONEG_CFG2);
  78. }
  79. static int emac_sgmii_irq_clear(struct emac_adapter *adpt, u8 irq_bits)
  80. {
  81. struct emac_sgmii *phy = &adpt->phy;
  82. u8 status;
  83. writel_relaxed(irq_bits, phy->base + EMAC_SGMII_PHY_INTERRUPT_CLEAR);
  84. writel_relaxed(IRQ_GLOBAL_CLEAR, phy->base + EMAC_SGMII_PHY_IRQ_CMD);
  85. /* Ensure interrupt clear command is written to HW */
  86. wmb();
  87. /* After set the IRQ_GLOBAL_CLEAR bit, the status clearing must
  88. * be confirmed before clearing the bits in other registers.
  89. * It takes a few cycles for hw to clear the interrupt status.
  90. */
  91. if (readl_poll_timeout_atomic(phy->base +
  92. EMAC_SGMII_PHY_INTERRUPT_STATUS,
  93. status, !(status & irq_bits), 1,
  94. SGMII_PHY_IRQ_CLR_WAIT_TIME)) {
  95. net_err_ratelimited("%s: failed to clear SGMII irq: status:0x%x bits:0x%x\n",
  96. adpt->netdev->name, status, irq_bits);
  97. return -EIO;
  98. }
  99. /* Finalize clearing procedure */
  100. writel_relaxed(0, phy->base + EMAC_SGMII_PHY_IRQ_CMD);
  101. writel_relaxed(0, phy->base + EMAC_SGMII_PHY_INTERRUPT_CLEAR);
  102. /* Ensure that clearing procedure finalization is written to HW */
  103. wmb();
  104. return 0;
  105. }
  106. /* The number of decode errors that triggers a reset */
  107. #define DECODE_ERROR_LIMIT 2
  108. static irqreturn_t emac_sgmii_interrupt(int irq, void *data)
  109. {
  110. struct emac_adapter *adpt = data;
  111. struct emac_sgmii *phy = &adpt->phy;
  112. u8 status;
  113. status = readl(phy->base + EMAC_SGMII_PHY_INTERRUPT_STATUS);
  114. status &= SGMII_ISR_MASK;
  115. if (!status)
  116. return IRQ_HANDLED;
  117. /* If we get a decoding error and CDR is not locked, then try
  118. * resetting the internal PHY. The internal PHY uses an embedded
  119. * clock with Clock and Data Recovery (CDR) to recover the
  120. * clock and data.
  121. */
  122. if (status & SGMII_PHY_INTERRUPT_ERR) {
  123. int count;
  124. /* The SGMII is capable of recovering from some decode
  125. * errors automatically. However, if we get multiple
  126. * decode errors in a row, then assume that something
  127. * is wrong and reset the interface.
  128. */
  129. count = atomic_inc_return(&phy->decode_error_count);
  130. if (count == DECODE_ERROR_LIMIT) {
  131. schedule_work(&adpt->work_thread);
  132. atomic_set(&phy->decode_error_count, 0);
  133. }
  134. } else {
  135. /* We only care about consecutive decode errors. */
  136. atomic_set(&phy->decode_error_count, 0);
  137. }
  138. if (emac_sgmii_irq_clear(adpt, status))
  139. schedule_work(&adpt->work_thread);
  140. return IRQ_HANDLED;
  141. }
  142. static void emac_sgmii_reset_prepare(struct emac_adapter *adpt)
  143. {
  144. struct emac_sgmii *phy = &adpt->phy;
  145. u32 val;
  146. /* Reset PHY */
  147. val = readl(phy->base + EMAC_EMAC_WRAPPER_CSR2);
  148. writel(((val & ~PHY_RESET) | PHY_RESET), phy->base +
  149. EMAC_EMAC_WRAPPER_CSR2);
  150. /* Ensure phy-reset command is written to HW before the release cmd */
  151. msleep(50);
  152. val = readl(phy->base + EMAC_EMAC_WRAPPER_CSR2);
  153. writel((val & ~PHY_RESET), phy->base + EMAC_EMAC_WRAPPER_CSR2);
  154. /* Ensure phy-reset release command is written to HW before initializing
  155. * SGMII
  156. */
  157. msleep(50);
  158. }
  159. static void emac_sgmii_common_reset(struct emac_adapter *adpt)
  160. {
  161. int ret;
  162. emac_sgmii_reset_prepare(adpt);
  163. emac_sgmii_link_init(adpt);
  164. ret = emac_sgmii_init(adpt);
  165. if (ret)
  166. netdev_err(adpt->netdev,
  167. "could not reinitialize internal PHY (error=%i)\n",
  168. ret);
  169. }
  170. static int emac_sgmii_common_open(struct emac_adapter *adpt)
  171. {
  172. struct emac_sgmii *sgmii = &adpt->phy;
  173. int ret;
  174. if (sgmii->irq) {
  175. /* Make sure interrupts are cleared and disabled first */
  176. ret = emac_sgmii_irq_clear(adpt, 0xff);
  177. if (ret)
  178. return ret;
  179. writel(0, sgmii->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
  180. ret = request_irq(sgmii->irq, emac_sgmii_interrupt, 0,
  181. "emac-sgmii", adpt);
  182. if (ret) {
  183. netdev_err(adpt->netdev,
  184. "could not register handler for internal PHY\n");
  185. return ret;
  186. }
  187. }
  188. return 0;
  189. }
  190. static void emac_sgmii_common_close(struct emac_adapter *adpt)
  191. {
  192. struct emac_sgmii *sgmii = &adpt->phy;
  193. /* Make sure interrupts are disabled */
  194. writel(0, sgmii->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
  195. free_irq(sgmii->irq, adpt);
  196. }
  197. /* The error interrupts are only valid after the link is up */
  198. static int emac_sgmii_common_link_change(struct emac_adapter *adpt, bool linkup)
  199. {
  200. struct emac_sgmii *sgmii = &adpt->phy;
  201. int ret;
  202. if (linkup) {
  203. /* Clear and enable interrupts */
  204. ret = emac_sgmii_irq_clear(adpt, 0xff);
  205. if (ret)
  206. return ret;
  207. writel(SGMII_ISR_MASK,
  208. sgmii->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
  209. } else {
  210. /* Disable interrupts */
  211. writel(0, sgmii->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
  212. synchronize_irq(sgmii->irq);
  213. }
  214. return 0;
  215. }
  216. static struct sgmii_ops fsm9900_ops = {
  217. .init = emac_sgmii_init_fsm9900,
  218. .open = emac_sgmii_common_open,
  219. .close = emac_sgmii_common_close,
  220. .link_change = emac_sgmii_common_link_change,
  221. .reset = emac_sgmii_common_reset,
  222. };
  223. static struct sgmii_ops qdf2432_ops = {
  224. .init = emac_sgmii_init_qdf2432,
  225. .open = emac_sgmii_common_open,
  226. .close = emac_sgmii_common_close,
  227. .link_change = emac_sgmii_common_link_change,
  228. .reset = emac_sgmii_common_reset,
  229. };
  230. #ifdef CONFIG_ACPI
  231. static struct sgmii_ops qdf2400_ops = {
  232. .init = emac_sgmii_init_qdf2400,
  233. .open = emac_sgmii_common_open,
  234. .close = emac_sgmii_common_close,
  235. .link_change = emac_sgmii_common_link_change,
  236. .reset = emac_sgmii_common_reset,
  237. };
  238. #endif
  239. static int emac_sgmii_acpi_match(struct device *dev, void *data)
  240. {
  241. #ifdef CONFIG_ACPI
  242. static const struct acpi_device_id match_table[] = {
  243. {
  244. .id = "QCOM8071",
  245. },
  246. {}
  247. };
  248. const struct acpi_device_id *id = acpi_match_device(match_table, dev);
  249. struct sgmii_ops **ops = data;
  250. if (id) {
  251. acpi_handle handle = ACPI_HANDLE(dev);
  252. unsigned long long hrv;
  253. acpi_status status;
  254. status = acpi_evaluate_integer(handle, "_HRV", NULL, &hrv);
  255. if (status) {
  256. if (status == AE_NOT_FOUND)
  257. /* Older versions of the QDF2432 ACPI tables do
  258. * not have an _HRV property.
  259. */
  260. hrv = 1;
  261. else
  262. /* Something is wrong with the tables */
  263. return 0;
  264. }
  265. switch (hrv) {
  266. case 1:
  267. *ops = &qdf2432_ops;
  268. return 1;
  269. case 2:
  270. *ops = &qdf2400_ops;
  271. return 1;
  272. }
  273. }
  274. #endif
  275. return 0;
  276. }
  277. static const struct of_device_id emac_sgmii_dt_match[] = {
  278. {
  279. .compatible = "qcom,fsm9900-emac-sgmii",
  280. .data = &fsm9900_ops,
  281. },
  282. {
  283. .compatible = "qcom,qdf2432-emac-sgmii",
  284. .data = &qdf2432_ops,
  285. },
  286. {}
  287. };
  288. int emac_sgmii_config(struct platform_device *pdev, struct emac_adapter *adpt)
  289. {
  290. struct platform_device *sgmii_pdev = NULL;
  291. struct emac_sgmii *phy = &adpt->phy;
  292. struct resource *res;
  293. int ret;
  294. if (has_acpi_companion(&pdev->dev)) {
  295. struct device *dev;
  296. dev = device_find_child(&pdev->dev, &phy->sgmii_ops,
  297. emac_sgmii_acpi_match);
  298. if (!dev) {
  299. dev_warn(&pdev->dev, "cannot find internal phy node\n");
  300. return 0;
  301. }
  302. sgmii_pdev = to_platform_device(dev);
  303. } else {
  304. const struct of_device_id *match;
  305. struct device_node *np;
  306. np = of_parse_phandle(pdev->dev.of_node, "internal-phy", 0);
  307. if (!np) {
  308. dev_err(&pdev->dev, "missing internal-phy property\n");
  309. return -ENODEV;
  310. }
  311. sgmii_pdev = of_find_device_by_node(np);
  312. of_node_put(np);
  313. if (!sgmii_pdev) {
  314. dev_err(&pdev->dev, "invalid internal-phy property\n");
  315. return -ENODEV;
  316. }
  317. match = of_match_device(emac_sgmii_dt_match, &sgmii_pdev->dev);
  318. if (!match) {
  319. dev_err(&pdev->dev, "unrecognized internal phy node\n");
  320. ret = -ENODEV;
  321. goto error_put_device;
  322. }
  323. phy->sgmii_ops = (struct sgmii_ops *)match->data;
  324. }
  325. /* Base address is the first address */
  326. res = platform_get_resource(sgmii_pdev, IORESOURCE_MEM, 0);
  327. if (!res) {
  328. ret = -EINVAL;
  329. goto error_put_device;
  330. }
  331. phy->base = ioremap(res->start, resource_size(res));
  332. if (!phy->base) {
  333. ret = -ENOMEM;
  334. goto error_put_device;
  335. }
  336. /* v2 SGMII has a per-lane digital digital, so parse it if it exists */
  337. res = platform_get_resource(sgmii_pdev, IORESOURCE_MEM, 1);
  338. if (res) {
  339. phy->digital = ioremap(res->start, resource_size(res));
  340. if (!phy->digital) {
  341. ret = -ENOMEM;
  342. goto error_unmap_base;
  343. }
  344. }
  345. ret = emac_sgmii_init(adpt);
  346. if (ret)
  347. goto error;
  348. emac_sgmii_link_init(adpt);
  349. ret = platform_get_irq(sgmii_pdev, 0);
  350. if (ret > 0)
  351. phy->irq = ret;
  352. /* We've remapped the addresses, so we don't need the device any
  353. * more. of_find_device_by_node() says we should release it.
  354. */
  355. put_device(&sgmii_pdev->dev);
  356. return 0;
  357. error:
  358. if (phy->digital)
  359. iounmap(phy->digital);
  360. error_unmap_base:
  361. iounmap(phy->base);
  362. error_put_device:
  363. put_device(&sgmii_pdev->dev);
  364. return ret;
  365. }