gsw_mt7621.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_mt7621(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 reg, i;
  36. reg = mt7530_mdio_r32(gsw, 0x700c);
  37. for (i = 0; i < 5; i++)
  38. if (reg & BIT(i)) {
  39. unsigned int link;
  40. link = mt7530_mdio_r32(gsw,
  41. 0x3008 + (i * 0x100)) & 0x1;
  42. if (link != priv->link[i]) {
  43. priv->link[i] = link;
  44. if (link)
  45. netdev_info(priv->netdev,
  46. "port %d link up\n", i);
  47. else
  48. netdev_info(priv->netdev,
  49. "port %d link down\n", i);
  50. }
  51. }
  52. mt7620_handle_carrier(priv);
  53. mt7530_mdio_w32(gsw, 0x700c, 0x1f);
  54. return IRQ_HANDLED;
  55. }
  56. static void mt7621_hw_init(struct mt7620_gsw *gsw, struct device_node *np)
  57. {
  58. u32 i;
  59. u32 val;
  60. /* wardware reset the switch */
  61. fe_reset(RST_CTRL_MCM);
  62. mdelay(10);
  63. /* reduce RGMII2 PAD driving strength */
  64. rt_sysc_m32(3 << 4, 0, SYSC_PAD_RGMII2_MDIO);
  65. /* gpio mux - RGMII1=Normal mode */
  66. rt_sysc_m32(BIT(14), 0, SYSC_GPIO_MODE);
  67. /* set GMAC1 RGMII mode */
  68. rt_sysc_m32(3 << 12, 0, SYSC_REG_CFG1);
  69. /* enable MDIO to control MT7530 */
  70. rt_sysc_m32(3 << 12, 0, SYSC_GPIO_MODE);
  71. /* turn off all PHYs */
  72. for (i = 0; i <= 4; i++) {
  73. val = _mt7620_mii_read(gsw, i, 0x0);
  74. val |= BIT(11);
  75. _mt7620_mii_write(gsw, i, 0x0, val);
  76. }
  77. /* reset the switch */
  78. mt7530_mdio_w32(gsw, 0x7000, 0x3);
  79. usleep_range(10, 20);
  80. if ((rt_sysc_r32(SYSC_REG_CHIP_REV_ID) & 0xFFFF) == 0x0101) {
  81. /* (GE1, Force 1000M/FD, FC ON, MAX_RX_LENGTH 1536) */
  82. mtk_switch_w32(gsw, 0x2305e30b, GSW_REG_MAC_P0_MCR);
  83. mt7530_mdio_w32(gsw, 0x3600, 0x5e30b);
  84. } else {
  85. /* (GE1, Force 1000M/FD, FC ON, MAX_RX_LENGTH 1536) */
  86. mtk_switch_w32(gsw, 0x2305e33b, GSW_REG_MAC_P0_MCR);
  87. mt7530_mdio_w32(gsw, 0x3600, 0x5e33b);
  88. }
  89. /* (GE2, Link down) */
  90. mtk_switch_w32(gsw, 0x8000, GSW_REG_MAC_P1_MCR);
  91. /* Set switch max RX frame length to 2k */
  92. mt7530_mdio_w32(gsw, GSW_REG_GMACCR, 0x3F0B);
  93. /* Enable Port 6, P5 as GMAC5, P5 disable */
  94. val = mt7530_mdio_r32(gsw, 0x7804);
  95. val &= ~BIT(8);
  96. val |= BIT(6) | BIT(13) | BIT(16);
  97. mt7530_mdio_w32(gsw, 0x7804, val);
  98. val = rt_sysc_r32(0x10);
  99. val = (val >> 6) & 0x7;
  100. if (val >= 6) {
  101. /* 25Mhz Xtal - do nothing */
  102. } else if (val >= 3) {
  103. /* 40Mhz */
  104. /* disable MT7530 core clock */
  105. _mt7620_mii_write(gsw, 0, 13, 0x1f);
  106. _mt7620_mii_write(gsw, 0, 14, 0x410);
  107. _mt7620_mii_write(gsw, 0, 13, 0x401f);
  108. _mt7620_mii_write(gsw, 0, 14, 0x0);
  109. /* disable MT7530 PLL */
  110. _mt7620_mii_write(gsw, 0, 13, 0x1f);
  111. _mt7620_mii_write(gsw, 0, 14, 0x40d);
  112. _mt7620_mii_write(gsw, 0, 13, 0x401f);
  113. _mt7620_mii_write(gsw, 0, 14, 0x2020);
  114. /* for MT7530 core clock = 500Mhz */
  115. _mt7620_mii_write(gsw, 0, 13, 0x1f);
  116. _mt7620_mii_write(gsw, 0, 14, 0x40e);
  117. _mt7620_mii_write(gsw, 0, 13, 0x401f);
  118. _mt7620_mii_write(gsw, 0, 14, 0x119);
  119. /* enable MT7530 PLL */
  120. _mt7620_mii_write(gsw, 0, 13, 0x1f);
  121. _mt7620_mii_write(gsw, 0, 14, 0x40d);
  122. _mt7620_mii_write(gsw, 0, 13, 0x401f);
  123. _mt7620_mii_write(gsw, 0, 14, 0x2820);
  124. usleep_range(20, 40);
  125. /* enable MT7530 core clock */
  126. _mt7620_mii_write(gsw, 0, 13, 0x1f);
  127. _mt7620_mii_write(gsw, 0, 14, 0x410);
  128. _mt7620_mii_write(gsw, 0, 13, 0x401f);
  129. } else {
  130. /* 20Mhz Xtal - TODO */
  131. }
  132. /* RGMII */
  133. _mt7620_mii_write(gsw, 0, 14, 0x1);
  134. /* set MT7530 central align */
  135. val = mt7530_mdio_r32(gsw, 0x7830);
  136. val &= ~BIT(0);
  137. val |= BIT(1);
  138. mt7530_mdio_w32(gsw, 0x7830, val);
  139. val = mt7530_mdio_r32(gsw, 0x7a40);
  140. val &= ~BIT(30);
  141. mt7530_mdio_w32(gsw, 0x7a40, val);
  142. mt7530_mdio_w32(gsw, 0x7a78, 0x855);
  143. /* delay setting for 10/1000M */
  144. mt7530_mdio_w32(gsw, 0x7b00, 0x102);
  145. mt7530_mdio_w32(gsw, 0x7b04, 0x14);
  146. /* lower Tx Driving*/
  147. mt7530_mdio_w32(gsw, 0x7a54, 0x44);
  148. mt7530_mdio_w32(gsw, 0x7a5c, 0x44);
  149. mt7530_mdio_w32(gsw, 0x7a64, 0x44);
  150. mt7530_mdio_w32(gsw, 0x7a6c, 0x44);
  151. mt7530_mdio_w32(gsw, 0x7a74, 0x44);
  152. mt7530_mdio_w32(gsw, 0x7a7c, 0x44);
  153. /* turn on all PHYs */
  154. for (i = 0; i <= 4; i++) {
  155. val = _mt7620_mii_read(gsw, i, 0);
  156. val &= ~BIT(11);
  157. _mt7620_mii_write(gsw, i, 0, val);
  158. }
  159. /* enable irq */
  160. val = mt7530_mdio_r32(gsw, 0x7808);
  161. val |= 3 << 16;
  162. mt7530_mdio_w32(gsw, 0x7808, val);
  163. }
  164. static const struct of_device_id mediatek_gsw_match[] = {
  165. { .compatible = "mediatek,mt7621-gsw" },
  166. {},
  167. };
  168. MODULE_DEVICE_TABLE(of, mediatek_gsw_match);
  169. int mtk_gsw_init(struct fe_priv *priv)
  170. {
  171. struct device_node *np = priv->switch_np;
  172. struct platform_device *pdev = of_find_device_by_node(np);
  173. struct mt7620_gsw *gsw;
  174. if (!pdev)
  175. return -ENODEV;
  176. if (!of_device_is_compatible(np, mediatek_gsw_match->compatible))
  177. return -EINVAL;
  178. gsw = platform_get_drvdata(pdev);
  179. priv->soc->swpriv = gsw;
  180. mt7621_hw_init(gsw, np);
  181. if (gsw->irq) {
  182. request_irq(gsw->irq, gsw_interrupt_mt7621, 0,
  183. "gsw", priv);
  184. mt7530_mdio_w32(gsw, 0x7008, 0x1f);
  185. }
  186. return 0;
  187. }
  188. static int mt7621_gsw_probe(struct platform_device *pdev)
  189. {
  190. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  191. const char *port4 = NULL;
  192. struct mt7620_gsw *gsw;
  193. struct device_node *np;
  194. gsw = devm_kzalloc(&pdev->dev, sizeof(struct mt7620_gsw), GFP_KERNEL);
  195. if (!gsw)
  196. return -ENOMEM;
  197. gsw->base = devm_ioremap_resource(&pdev->dev, res);
  198. if (!gsw->base)
  199. return -EADDRNOTAVAIL;
  200. gsw->dev = &pdev->dev;
  201. of_property_read_string(np, "mediatek,port4", &port4);
  202. if (port4 && !strcmp(port4, "ephy"))
  203. gsw->port4 = PORT4_EPHY;
  204. else if (port4 && !strcmp(port4, "gmac"))
  205. gsw->port4 = PORT4_EXT;
  206. else
  207. gsw->port4 = PORT4_EPHY;
  208. gsw->irq = platform_get_irq(pdev, 0);
  209. platform_set_drvdata(pdev, gsw);
  210. return 0;
  211. }
  212. static int mt7621_gsw_remove(struct platform_device *pdev)
  213. {
  214. platform_set_drvdata(pdev, NULL);
  215. return 0;
  216. }
  217. static struct platform_driver gsw_driver = {
  218. .probe = mt7621_gsw_probe,
  219. .remove = mt7621_gsw_remove,
  220. .driver = {
  221. .name = "mt7621-gsw",
  222. .owner = THIS_MODULE,
  223. .of_match_table = mediatek_gsw_match,
  224. },
  225. };
  226. module_platform_driver(gsw_driver);
  227. MODULE_LICENSE("GPL");
  228. MODULE_AUTHOR("John Crispin <[email protected]>");
  229. MODULE_DESCRIPTION("GBit switch driver for Mediatek MT7621 SoC");
  230. MODULE_VERSION(MTK_FE_DRV_VERSION);