gsw_mt7620.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License as published by
  3. * the Free Software Foundation; version 2 of the License
  4. *
  5. * This program is distributed in the hope that it will be useful,
  6. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. * GNU General Public License for more details.
  9. *
  10. * Copyright (C) 2009-2015 John Crispin <[email protected]>
  11. * Copyright (C) 2009-2015 Felix Fietkau <[email protected]>
  12. * Copyright (C) 2013-2015 Michael Lee <[email protected]>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_irq.h>
  20. #include <ralink_regs.h>
  21. #include "mtk_eth_soc.h"
  22. #include "gsw_mt7620.h"
  23. void mtk_switch_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg)
  24. {
  25. iowrite32(val, gsw->base + reg);
  26. }
  27. u32 mtk_switch_r32(struct mt7620_gsw *gsw, unsigned reg)
  28. {
  29. return ioread32(gsw->base + reg);
  30. }
  31. static irqreturn_t gsw_interrupt_mt7620(int irq, void *_priv)
  32. {
  33. struct fe_priv *priv = (struct fe_priv *)_priv;
  34. struct mt7620_gsw *gsw = (struct mt7620_gsw *)priv->soc->swpriv;
  35. u32 status;
  36. int i, max = (gsw->port4 == PORT4_EPHY) ? (4) : (3);
  37. status = mtk_switch_r32(gsw, GSW_REG_ISR);
  38. if (status & PORT_IRQ_ST_CHG)
  39. for (i = 0; i <= max; i++) {
  40. u32 status = mtk_switch_r32(gsw, GSW_REG_PORT_STATUS(i));
  41. int link = status & 0x1;
  42. if (link != priv->link[i])
  43. mt7620_print_link_state(priv, i, link,
  44. (status >> 2) & 3,
  45. (status & 0x2));
  46. priv->link[i] = link;
  47. }
  48. mt7620_handle_carrier(priv);
  49. mtk_switch_w32(gsw, status, GSW_REG_ISR);
  50. return IRQ_HANDLED;
  51. }
  52. static void mt7620_hw_init(struct mt7620_gsw *gsw, struct device_node *np)
  53. {
  54. u32 is_BGA = (rt_sysc_r32(0x0c) >> 16) & 1;
  55. rt_sysc_w32(rt_sysc_r32(SYSC_REG_CFG1) | BIT(8), SYSC_REG_CFG1);
  56. mtk_switch_w32(gsw, mtk_switch_r32(gsw, GSW_REG_CKGCR) & ~(0x3 << 4), GSW_REG_CKGCR);
  57. /* Enable MIB stats */
  58. mtk_switch_w32(gsw, mtk_switch_r32(gsw, GSW_REG_MIB_CNT_EN) | (1 << 1), GSW_REG_MIB_CNT_EN);
  59. if (of_property_read_bool(np, "mediatek,mt7530")) {
  60. u32 val;
  61. /* turn off ephy and set phy base addr to 12 */
  62. mtk_switch_w32(gsw, mtk_switch_r32(gsw, GSW_REG_GPC1) |
  63. (0x1f << 24) | (0xc << 16),
  64. GSW_REG_GPC1);
  65. /* set MT7530 central align */
  66. val = mt7530_mdio_r32(gsw, 0x7830);
  67. val &= ~BIT(0);
  68. val |= BIT(1);
  69. mt7530_mdio_w32(gsw, 0x7830, val);
  70. val = mt7530_mdio_r32(gsw, 0x7a40);
  71. val &= ~BIT(30);
  72. mt7530_mdio_w32(gsw, 0x7a40, val);
  73. mt7530_mdio_w32(gsw, 0x7a78, 0x855);
  74. } else {
  75. /* global page 4 */
  76. _mt7620_mii_write(gsw, 1, 31, 0x4000);
  77. _mt7620_mii_write(gsw, 1, 17, 0x7444);
  78. if (is_BGA)
  79. _mt7620_mii_write(gsw, 1, 19, 0x0114);
  80. else
  81. _mt7620_mii_write(gsw, 1, 19, 0x0117);
  82. _mt7620_mii_write(gsw, 1, 22, 0x10cf);
  83. _mt7620_mii_write(gsw, 1, 25, 0x6212);
  84. _mt7620_mii_write(gsw, 1, 26, 0x0777);
  85. _mt7620_mii_write(gsw, 1, 29, 0x4000);
  86. _mt7620_mii_write(gsw, 1, 28, 0xc077);
  87. _mt7620_mii_write(gsw, 1, 24, 0x0000);
  88. /* global page 3 */
  89. _mt7620_mii_write(gsw, 1, 31, 0x3000);
  90. _mt7620_mii_write(gsw, 1, 17, 0x4838);
  91. /* global page 2 */
  92. _mt7620_mii_write(gsw, 1, 31, 0x2000);
  93. if (is_BGA) {
  94. _mt7620_mii_write(gsw, 1, 21, 0x0515);
  95. _mt7620_mii_write(gsw, 1, 22, 0x0053);
  96. _mt7620_mii_write(gsw, 1, 23, 0x00bf);
  97. _mt7620_mii_write(gsw, 1, 24, 0x0aaf);
  98. _mt7620_mii_write(gsw, 1, 25, 0x0fad);
  99. _mt7620_mii_write(gsw, 1, 26, 0x0fc1);
  100. } else {
  101. _mt7620_mii_write(gsw, 1, 21, 0x0517);
  102. _mt7620_mii_write(gsw, 1, 22, 0x0fd2);
  103. _mt7620_mii_write(gsw, 1, 23, 0x00bf);
  104. _mt7620_mii_write(gsw, 1, 24, 0x0aab);
  105. _mt7620_mii_write(gsw, 1, 25, 0x00ae);
  106. _mt7620_mii_write(gsw, 1, 26, 0x0fff);
  107. }
  108. /* global page 1 */
  109. _mt7620_mii_write(gsw, 1, 31, 0x1000);
  110. _mt7620_mii_write(gsw, 1, 17, 0xe7f8);
  111. }
  112. /* global page 0 */
  113. _mt7620_mii_write(gsw, 1, 31, 0x8000);
  114. _mt7620_mii_write(gsw, 0, 30, 0xa000);
  115. _mt7620_mii_write(gsw, 1, 30, 0xa000);
  116. _mt7620_mii_write(gsw, 2, 30, 0xa000);
  117. _mt7620_mii_write(gsw, 3, 30, 0xa000);
  118. _mt7620_mii_write(gsw, 0, 4, 0x05e1);
  119. _mt7620_mii_write(gsw, 1, 4, 0x05e1);
  120. _mt7620_mii_write(gsw, 2, 4, 0x05e1);
  121. _mt7620_mii_write(gsw, 3, 4, 0x05e1);
  122. /* global page 2 */
  123. _mt7620_mii_write(gsw, 1, 31, 0xa000);
  124. _mt7620_mii_write(gsw, 0, 16, 0x1111);
  125. _mt7620_mii_write(gsw, 1, 16, 0x1010);
  126. _mt7620_mii_write(gsw, 2, 16, 0x1515);
  127. _mt7620_mii_write(gsw, 3, 16, 0x0f0f);
  128. /* CPU Port6 Force Link 1G, FC ON */
  129. mtk_switch_w32(gsw, 0x5e33b, GSW_REG_PORT_PMCR(6));
  130. /* Set Port 6 as CPU Port */
  131. mtk_switch_w32(gsw, 0x7f7f7fe0, 0x0010);
  132. /* setup port 4 */
  133. if (gsw->port4 == PORT4_EPHY) {
  134. u32 val = rt_sysc_r32(SYSC_REG_CFG1);
  135. val |= 3 << 14;
  136. rt_sysc_w32(val, SYSC_REG_CFG1);
  137. _mt7620_mii_write(gsw, 4, 30, 0xa000);
  138. _mt7620_mii_write(gsw, 4, 4, 0x05e1);
  139. _mt7620_mii_write(gsw, 4, 16, 0x1313);
  140. _mt7620_mii_write(gsw, 4, 0, 0x3100);
  141. pr_info("gsw: setting port4 to ephy mode\n");
  142. }
  143. }
  144. static const struct of_device_id mediatek_gsw_match[] = {
  145. { .compatible = "mediatek,mt7620-gsw" },
  146. {},
  147. };
  148. MODULE_DEVICE_TABLE(of, mediatek_gsw_match);
  149. int mtk_gsw_init(struct fe_priv *priv)
  150. {
  151. struct device_node *np = priv->switch_np;
  152. struct platform_device *pdev = of_find_device_by_node(np);
  153. struct mt7620_gsw *gsw;
  154. if (!pdev)
  155. return -ENODEV;
  156. if (!of_device_is_compatible(np, mediatek_gsw_match->compatible))
  157. return -EINVAL;
  158. gsw = platform_get_drvdata(pdev);
  159. priv->soc->swpriv = gsw;
  160. mt7620_hw_init(gsw, np);
  161. if (gsw->irq) {
  162. request_irq(gsw->irq, gsw_interrupt_mt7620, 0,
  163. "gsw", priv);
  164. mtk_switch_w32(gsw, ~PORT_IRQ_ST_CHG, GSW_REG_IMR);
  165. }
  166. return 0;
  167. }
  168. static int mt7620_gsw_probe(struct platform_device *pdev)
  169. {
  170. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  171. const char *port4 = NULL;
  172. struct mt7620_gsw *gsw;
  173. struct device_node *np = pdev->dev.of_node;
  174. gsw = devm_kzalloc(&pdev->dev, sizeof(struct mt7620_gsw), GFP_KERNEL);
  175. if (!gsw)
  176. return -ENOMEM;
  177. gsw->base = devm_ioremap_resource(&pdev->dev, res);
  178. if (!gsw->base)
  179. return -EADDRNOTAVAIL;
  180. gsw->dev = &pdev->dev;
  181. of_property_read_string(np, "mediatek,port4", &port4);
  182. if (port4 && !strcmp(port4, "ephy"))
  183. gsw->port4 = PORT4_EPHY;
  184. else if (port4 && !strcmp(port4, "gmac"))
  185. gsw->port4 = PORT4_EXT;
  186. else
  187. gsw->port4 = PORT4_EPHY;
  188. gsw->irq = platform_get_irq(pdev, 0);
  189. platform_set_drvdata(pdev, gsw);
  190. return 0;
  191. }
  192. static int mt7620_gsw_remove(struct platform_device *pdev)
  193. {
  194. platform_set_drvdata(pdev, NULL);
  195. return 0;
  196. }
  197. static struct platform_driver gsw_driver = {
  198. .probe = mt7620_gsw_probe,
  199. .remove = mt7620_gsw_remove,
  200. .driver = {
  201. .name = "mt7620-gsw",
  202. .owner = THIS_MODULE,
  203. .of_match_table = mediatek_gsw_match,
  204. },
  205. };
  206. module_platform_driver(gsw_driver);
  207. MODULE_LICENSE("GPL");
  208. MODULE_AUTHOR("John Crispin <[email protected]>");
  209. MODULE_DESCRIPTION("GBit switch driver for Mediatek MT7620 SoC");
  210. MODULE_VERSION(MTK_FE_DRV_VERSION);