0504-net-next-mediatek-add-switch-driver-for-mt7621.patch 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. From 9fc19d5f7354709298dcb15b3a4c7cd9a18acebf Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Mon, 14 Dec 2015 21:24:46 +0100
  4. Subject: [PATCH 504/513] net-next: mediatek: add switch driver for mt7621
  5. This driver is very basic and only provides basic init and irq support.
  6. Switchdev support for this device will follow.
  7. Signed-off-by: John Crispin <blogic@openwrt.org>
  8. ---
  9. drivers/net/ethernet/mediatek/gsw_mt7621.c | 284 ++++++++++++++++++++++++++++
  10. 1 file changed, 284 insertions(+)
  11. create mode 100644 drivers/net/ethernet/mediatek/gsw_mt7621.c
  12. --- /dev/null
  13. +++ b/drivers/net/ethernet/mediatek/gsw_mt7621.c
  14. @@ -0,0 +1,284 @@
  15. +/* This program is free software; you can redistribute it and/or modify
  16. + * it under the terms of the GNU General Public License as published by
  17. + * the Free Software Foundation; version 2 of the License
  18. + *
  19. + * This program is distributed in the hope that it will be useful,
  20. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. + * GNU General Public License for more details.
  23. + *
  24. + * Copyright (C) 2009-2015 John Crispin <blogic@openwrt.org>
  25. + * Copyright (C) 2009-2015 Felix Fietkau <nbd@nbd.name>
  26. + * Copyright (C) 2013-2015 Michael Lee <igvtee@gmail.com>
  27. + */
  28. +
  29. +#include <linux/module.h>
  30. +#include <linux/kernel.h>
  31. +#include <linux/types.h>
  32. +#include <linux/platform_device.h>
  33. +#include <linux/of_device.h>
  34. +#include <linux/of_irq.h>
  35. +
  36. +#include <ralink_regs.h>
  37. +
  38. +#include "mtk_eth_soc.h"
  39. +#include "gsw_mt7620.h"
  40. +
  41. +void mtk_switch_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg)
  42. +{
  43. + iowrite32(val, gsw->base + reg);
  44. +}
  45. +
  46. +u32 mtk_switch_r32(struct mt7620_gsw *gsw, unsigned reg)
  47. +{
  48. + return ioread32(gsw->base + reg);
  49. +}
  50. +
  51. +static irqreturn_t gsw_interrupt_mt7621(int irq, void *_priv)
  52. +{
  53. + struct fe_priv *priv = (struct fe_priv *)_priv;
  54. + struct mt7620_gsw *gsw = (struct mt7620_gsw *)priv->soc->swpriv;
  55. + u32 reg, i;
  56. +
  57. + reg = mt7530_mdio_r32(gsw, 0x700c);
  58. +
  59. + for (i = 0; i < 5; i++)
  60. + if (reg & BIT(i)) {
  61. + unsigned int link;
  62. +
  63. + link = mt7530_mdio_r32(gsw,
  64. + 0x3008 + (i * 0x100)) & 0x1;
  65. +
  66. + if (link != priv->link[i]) {
  67. + priv->link[i] = link;
  68. + if (link)
  69. + netdev_info(priv->netdev,
  70. + "port %d link up\n", i);
  71. + else
  72. + netdev_info(priv->netdev,
  73. + "port %d link down\n", i);
  74. + }
  75. + }
  76. +
  77. + mt7530_mdio_w32(gsw, 0x700c, 0x1f);
  78. +
  79. + return IRQ_HANDLED;
  80. +}
  81. +
  82. +static void mt7621_hw_init(struct mt7620_gsw *gsw, struct device_node *np)
  83. +{
  84. + u32 i;
  85. + u32 val;
  86. +
  87. + /* wardware reset the switch */
  88. + fe_reset(RST_CTRL_MCM);
  89. + mdelay(10);
  90. +
  91. + /* reduce RGMII2 PAD driving strength */
  92. + rt_sysc_m32(3 << 4, 0, SYSC_PAD_RGMII2_MDIO);
  93. +
  94. + /* gpio mux - RGMII1=Normal mode */
  95. + rt_sysc_m32(BIT(14), 0, SYSC_GPIO_MODE);
  96. +
  97. + /* set GMAC1 RGMII mode */
  98. + rt_sysc_m32(3 << 12, 0, SYSC_REG_CFG1);
  99. +
  100. + /* enable MDIO to control MT7530 */
  101. + rt_sysc_m32(3 << 12, 0, SYSC_GPIO_MODE);
  102. +
  103. + /* turn off all PHYs */
  104. + for (i = 0; i <= 4; i++) {
  105. + val = _mt7620_mii_read(gsw, i, 0x0);
  106. + val |= BIT(11);
  107. + _mt7620_mii_write(gsw, i, 0x0, val);
  108. + }
  109. +
  110. + /* reset the switch */
  111. + mt7530_mdio_w32(gsw, 0x7000, 0x3);
  112. + usleep_range(10, 20);
  113. +
  114. + if ((rt_sysc_r32(SYSC_REG_CHIP_REV_ID) & 0xFFFF) == 0x0101) {
  115. + /* (GE1, Force 1000M/FD, FC ON, MAX_RX_LENGTH 1536) */
  116. + mtk_switch_w32(gsw, 0x2105e30b, 0x100);
  117. + mt7530_mdio_w32(gsw, 0x3600, 0x5e30b);
  118. + } else {
  119. + /* (GE1, Force 1000M/FD, FC ON, MAX_RX_LENGTH 1536) */
  120. + mtk_switch_w32(gsw, 0x2105e33b, 0x100);
  121. + mt7530_mdio_w32(gsw, 0x3600, 0x5e33b);
  122. + }
  123. +
  124. + /* (GE2, Link down) */
  125. + mtk_switch_w32(gsw, 0x8000, 0x200);
  126. +
  127. + /* Enable Port 6, P5 as GMAC5, P5 disable */
  128. + val = mt7530_mdio_r32(gsw, 0x7804);
  129. + val &= ~BIT(8);
  130. + val |= BIT(6) | BIT(13) | BIT(16);
  131. + mt7530_mdio_w32(gsw, 0x7804, val);
  132. +
  133. + val = rt_sysc_r32(0x10);
  134. + val = (val >> 6) & 0x7;
  135. + if (val >= 6) {
  136. + /* 25Mhz Xtal - do nothing */
  137. + } else if (val >= 3) {
  138. + /* 40Mhz */
  139. +
  140. + /* disable MT7530 core clock */
  141. + _mt7620_mii_write(gsw, 0, 13, 0x1f);
  142. + _mt7620_mii_write(gsw, 0, 14, 0x410);
  143. + _mt7620_mii_write(gsw, 0, 13, 0x401f);
  144. + _mt7620_mii_write(gsw, 0, 14, 0x0);
  145. +
  146. + /* disable MT7530 PLL */
  147. + _mt7620_mii_write(gsw, 0, 13, 0x1f);
  148. + _mt7620_mii_write(gsw, 0, 14, 0x40d);
  149. + _mt7620_mii_write(gsw, 0, 13, 0x401f);
  150. + _mt7620_mii_write(gsw, 0, 14, 0x2020);
  151. +
  152. + /* for MT7530 core clock = 500Mhz */
  153. + _mt7620_mii_write(gsw, 0, 13, 0x1f);
  154. + _mt7620_mii_write(gsw, 0, 14, 0x40e);
  155. + _mt7620_mii_write(gsw, 0, 13, 0x401f);
  156. + _mt7620_mii_write(gsw, 0, 14, 0x119);
  157. +
  158. + /* enable MT7530 PLL */
  159. + _mt7620_mii_write(gsw, 0, 13, 0x1f);
  160. + _mt7620_mii_write(gsw, 0, 14, 0x40d);
  161. + _mt7620_mii_write(gsw, 0, 13, 0x401f);
  162. + _mt7620_mii_write(gsw, 0, 14, 0x2820);
  163. +
  164. + usleep_range(20, 40);
  165. +
  166. + /* enable MT7530 core clock */
  167. + _mt7620_mii_write(gsw, 0, 13, 0x1f);
  168. + _mt7620_mii_write(gsw, 0, 14, 0x410);
  169. + _mt7620_mii_write(gsw, 0, 13, 0x401f);
  170. + } else {
  171. + /* 20Mhz Xtal - TODO */
  172. + }
  173. +
  174. + /* RGMII */
  175. + _mt7620_mii_write(gsw, 0, 14, 0x1);
  176. +
  177. + /* set MT7530 central align */
  178. + val = mt7530_mdio_r32(gsw, 0x7830);
  179. + val &= ~BIT(0);
  180. + val |= BIT(1);
  181. + mt7530_mdio_w32(gsw, 0x7830, val);
  182. + val = mt7530_mdio_r32(gsw, 0x7a40);
  183. + val &= ~BIT(30);
  184. + mt7530_mdio_w32(gsw, 0x7a40, val);
  185. + mt7530_mdio_w32(gsw, 0x7a78, 0x855);
  186. +
  187. + /* delay setting for 10/1000M */
  188. + mt7530_mdio_w32(gsw, 0x7b00, 0x102);
  189. + mt7530_mdio_w32(gsw, 0x7b04, 0x14);
  190. +
  191. + /* lower Tx Driving*/
  192. + mt7530_mdio_w32(gsw, 0x7a54, 0x44);
  193. + mt7530_mdio_w32(gsw, 0x7a5c, 0x44);
  194. + mt7530_mdio_w32(gsw, 0x7a64, 0x44);
  195. + mt7530_mdio_w32(gsw, 0x7a6c, 0x44);
  196. + mt7530_mdio_w32(gsw, 0x7a74, 0x44);
  197. + mt7530_mdio_w32(gsw, 0x7a7c, 0x44);
  198. +
  199. + /* turn on all PHYs */
  200. + for (i = 0; i <= 4; i++) {
  201. + val = _mt7620_mii_read(gsw, i, 0);
  202. + val &= ~BIT(11);
  203. + _mt7620_mii_write(gsw, i, 0, val);
  204. + }
  205. +
  206. + /* enable irq */
  207. + val = mt7530_mdio_r32(gsw, 0x7808);
  208. + val |= 3 << 16;
  209. + mt7530_mdio_w32(gsw, 0x7808, val);
  210. +}
  211. +
  212. +static const struct of_device_id mediatek_gsw_match[] = {
  213. + { .compatible = "mediatek,mt7621-gsw" },
  214. + {},
  215. +};
  216. +MODULE_DEVICE_TABLE(of, mediatek_gsw_match);
  217. +
  218. +int mtk_gsw_init(struct fe_priv *priv)
  219. +{
  220. + struct device_node *np = priv->switch_np;
  221. + struct platform_device *pdev = of_find_device_by_node(np);
  222. + struct mt7620_gsw *gsw;
  223. +
  224. + if (!pdev)
  225. + return -ENODEV;
  226. +
  227. + if (!of_device_is_compatible(np, mediatek_gsw_match->compatible))
  228. + return -EINVAL;
  229. +
  230. + gsw = platform_get_drvdata(pdev);
  231. + priv->soc->swpriv = gsw;
  232. +
  233. + mt7621_hw_init(gsw, np);
  234. +
  235. + if (gsw->irq) {
  236. + request_irq(gsw->irq, gsw_interrupt_mt7621, 0,
  237. + "gsw", priv);
  238. + mt7530_mdio_w32(gsw, 0x7008, 0x1f);
  239. + }
  240. +
  241. + return 0;
  242. +}
  243. +
  244. +static int mt7621_gsw_probe(struct platform_device *pdev)
  245. +{
  246. + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  247. + const char *port4 = NULL;
  248. + struct mt7620_gsw *gsw;
  249. + struct device_node *np;
  250. +
  251. + gsw = devm_kzalloc(&pdev->dev, sizeof(struct mt7620_gsw), GFP_KERNEL);
  252. + if (!gsw)
  253. + return -ENOMEM;
  254. +
  255. + gsw->base = devm_ioremap_resource(&pdev->dev, res);
  256. + if (!gsw->base)
  257. + return -EADDRNOTAVAIL;
  258. +
  259. + gsw->dev = &pdev->dev;
  260. +
  261. + of_property_read_string(np, "mediatek,port4", &port4);
  262. + if (port4 && !strcmp(port4, "ephy"))
  263. + gsw->port4 = PORT4_EPHY;
  264. + else if (port4 && !strcmp(port4, "gmac"))
  265. + gsw->port4 = PORT4_EXT;
  266. + else
  267. + gsw->port4 = PORT4_EPHY;
  268. +
  269. + gsw->irq = platform_get_irq(pdev, 0);
  270. +
  271. + platform_set_drvdata(pdev, gsw);
  272. +
  273. + return 0;
  274. +}
  275. +
  276. +static int mt7621_gsw_remove(struct platform_device *pdev)
  277. +{
  278. + platform_set_drvdata(pdev, NULL);
  279. +
  280. + return 0;
  281. +}
  282. +
  283. +static struct platform_driver gsw_driver = {
  284. + .probe = mt7621_gsw_probe,
  285. + .remove = mt7621_gsw_remove,
  286. + .driver = {
  287. + .name = "mt7621-gsw",
  288. + .owner = THIS_MODULE,
  289. + .of_match_table = mediatek_gsw_match,
  290. + },
  291. +};
  292. +
  293. +module_platform_driver(gsw_driver);
  294. +
  295. +MODULE_LICENSE("GPL");
  296. +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
  297. +MODULE_DESCRIPTION("GBit switch driver for Mediatek MT7621 SoC");
  298. +MODULE_VERSION(MTK_FE_DRV_VERSION);