emac-sgmii.c 9.9 KB

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