emac-sgmii.c 9.9 KB

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