emac-sgmii.c 11 KB

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