soc_mt7620.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/platform_device.h>
  16. #include <linux/if_vlan.h>
  17. #include <linux/of_net.h>
  18. #include <asm/mach-ralink/ralink_regs.h>
  19. #include <mt7620.h>
  20. #include "mtk_eth_soc.h"
  21. #include "gsw_mt7620.h"
  22. #include "mt7530.h"
  23. #include "mdio.h"
  24. #define MT7620A_CDMA_CSG_CFG 0x400
  25. #define MT7620_DMA_VID (MT7620A_CDMA_CSG_CFG | 0x30)
  26. #define MT7621_CDMP_IG_CTRL (MT7620A_CDMA_CSG_CFG + 0x00)
  27. #define MT7621_CDMP_EG_CTRL (MT7620A_CDMA_CSG_CFG + 0x04)
  28. #define MT7620A_RESET_FE BIT(21)
  29. #define MT7621_RESET_FE BIT(6)
  30. #define MT7620A_RESET_ESW BIT(23)
  31. #define MT7620_L4_VALID BIT(23)
  32. #define MT7621_L4_VALID BIT(24)
  33. #define MT7620_TX_DMA_UDF BIT(15)
  34. #define MT7621_TX_DMA_UDF BIT(19)
  35. #define TX_DMA_FP_BMAP ((0xff) << 19)
  36. #define CDMA_ICS_EN BIT(2)
  37. #define CDMA_UCS_EN BIT(1)
  38. #define CDMA_TCS_EN BIT(0)
  39. #define GDMA_ICS_EN BIT(22)
  40. #define GDMA_TCS_EN BIT(21)
  41. #define GDMA_UCS_EN BIT(20)
  42. /* frame engine counters */
  43. #define MT7620_REG_MIB_OFFSET 0x1000
  44. #define MT7620_PPE_AC_BCNT0 (MT7620_REG_MIB_OFFSET + 0x00)
  45. #define MT7620_GDM1_TX_GBCNT (MT7620_REG_MIB_OFFSET + 0x300)
  46. #define MT7620_GDM2_TX_GBCNT (MT7620_GDM1_TX_GBCNT + 0x40)
  47. #define MT7621_REG_MIB_OFFSET 0x2000
  48. #define MT7621_PPE_AC_BCNT0 (MT7621_REG_MIB_OFFSET + 0x00)
  49. #define MT7621_GDM1_TX_GBCNT (MT7621_REG_MIB_OFFSET + 0x400)
  50. #define MT7621_GDM2_TX_GBCNT (MT7621_GDM1_TX_GBCNT + 0x40)
  51. #define GSW_REG_GDMA1_MAC_ADRL 0x508
  52. #define GSW_REG_GDMA1_MAC_ADRH 0x50C
  53. #define MT7621_FE_RST_GL (FE_FE_OFFSET + 0x04)
  54. #define MT7620_FE_INT_STATUS2 (FE_FE_OFFSET + 0x08)
  55. /* FE_INT_STATUS reg on mt7620 define CNT_GDM1_AF at BIT(29)
  56. * but after test it should be BIT(13).
  57. */
  58. #define MT7620_FE_GDM1_AF BIT(13)
  59. #define MT7621_FE_GDM1_AF BIT(28)
  60. #define MT7621_FE_GDM2_AF BIT(29)
  61. static const u16 mt7620_reg_table[FE_REG_COUNT] = {
  62. [FE_REG_PDMA_GLO_CFG] = RT5350_PDMA_GLO_CFG,
  63. [FE_REG_PDMA_RST_CFG] = RT5350_PDMA_RST_CFG,
  64. [FE_REG_DLY_INT_CFG] = RT5350_DLY_INT_CFG,
  65. [FE_REG_TX_BASE_PTR0] = RT5350_TX_BASE_PTR0,
  66. [FE_REG_TX_MAX_CNT0] = RT5350_TX_MAX_CNT0,
  67. [FE_REG_TX_CTX_IDX0] = RT5350_TX_CTX_IDX0,
  68. [FE_REG_TX_DTX_IDX0] = RT5350_TX_DTX_IDX0,
  69. [FE_REG_RX_BASE_PTR0] = RT5350_RX_BASE_PTR0,
  70. [FE_REG_RX_MAX_CNT0] = RT5350_RX_MAX_CNT0,
  71. [FE_REG_RX_CALC_IDX0] = RT5350_RX_CALC_IDX0,
  72. [FE_REG_RX_DRX_IDX0] = RT5350_RX_DRX_IDX0,
  73. [FE_REG_FE_INT_ENABLE] = RT5350_FE_INT_ENABLE,
  74. [FE_REG_FE_INT_STATUS] = RT5350_FE_INT_STATUS,
  75. [FE_REG_FE_DMA_VID_BASE] = MT7620_DMA_VID,
  76. [FE_REG_FE_COUNTER_BASE] = MT7620_GDM1_TX_GBCNT,
  77. [FE_REG_FE_RST_GL] = MT7621_FE_RST_GL,
  78. [FE_REG_FE_INT_STATUS2] = MT7620_FE_INT_STATUS2,
  79. };
  80. static int mt7620_gsw_config(struct fe_priv *priv)
  81. {
  82. struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
  83. /* is the mt7530 internal or external */
  84. if (priv->mii_bus && mdiobus_get_phy(priv->mii_bus, 0x1f)) {
  85. mt7530_probe(priv->device, gsw->base, NULL, 0);
  86. mt7530_probe(priv->device, NULL, priv->mii_bus, 1);
  87. } else {
  88. mt7530_probe(priv->device, gsw->base, NULL, 1);
  89. }
  90. return 0;
  91. }
  92. static void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac)
  93. {
  94. struct mt7620_gsw *gsw = (struct mt7620_gsw *)priv->soc->swpriv;
  95. unsigned long flags;
  96. spin_lock_irqsave(&priv->page_lock, flags);
  97. mtk_switch_w32(gsw, (mac[0] << 8) | mac[1], GSW_REG_SMACCR1);
  98. mtk_switch_w32(gsw, (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
  99. GSW_REG_SMACCR0);
  100. spin_unlock_irqrestore(&priv->page_lock, flags);
  101. }
  102. static void mt7620_auto_poll(struct mt7620_gsw *gsw)
  103. {
  104. int phy;
  105. int lsb = -1, msb = 0;
  106. for_each_set_bit(phy, &gsw->autopoll, 32) {
  107. if (lsb < 0)
  108. lsb = phy;
  109. msb = phy;
  110. }
  111. if (lsb == msb)
  112. lsb--;
  113. mtk_switch_w32(gsw, PHY_AN_EN | PHY_PRE_EN | PMY_MDC_CONF(5) |
  114. (msb << 8) | lsb, ESW_PHY_POLLING);
  115. }
  116. static void mt7620_port_init(struct fe_priv *priv, struct device_node *np)
  117. {
  118. struct mt7620_gsw *gsw = (struct mt7620_gsw *)priv->soc->swpriv;
  119. const __be32 *_id = of_get_property(np, "reg", NULL);
  120. int phy_mode, size, id;
  121. int shift = 12;
  122. u32 val, mask = 0;
  123. int min = (gsw->port4 == PORT4_EPHY) ? (5) : (4);
  124. if (!_id || (be32_to_cpu(*_id) < min) || (be32_to_cpu(*_id) > 5)) {
  125. if (_id)
  126. pr_err("%s: invalid port id %d\n", np->name,
  127. be32_to_cpu(*_id));
  128. else
  129. pr_err("%s: invalid port id\n", np->name);
  130. return;
  131. }
  132. id = be32_to_cpu(*_id);
  133. if (id == 4)
  134. shift = 14;
  135. priv->phy->phy_fixed[id] = of_get_property(np, "mediatek,fixed-link",
  136. &size);
  137. if (priv->phy->phy_fixed[id] &&
  138. (size != (4 * sizeof(*priv->phy->phy_fixed[id])))) {
  139. pr_err("%s: invalid fixed link property\n", np->name);
  140. priv->phy->phy_fixed[id] = NULL;
  141. return;
  142. }
  143. phy_mode = of_get_phy_mode(np);
  144. switch (phy_mode) {
  145. case PHY_INTERFACE_MODE_RGMII:
  146. mask = 0;
  147. break;
  148. case PHY_INTERFACE_MODE_MII:
  149. mask = 1;
  150. break;
  151. case PHY_INTERFACE_MODE_RMII:
  152. mask = 2;
  153. break;
  154. default:
  155. dev_err(priv->device, "port %d - invalid phy mode\n", id);
  156. return;
  157. }
  158. priv->phy->phy_node[id] = of_parse_phandle(np, "phy-handle", 0);
  159. if (!priv->phy->phy_node[id] && !priv->phy->phy_fixed[id])
  160. return;
  161. val = rt_sysc_r32(SYSC_REG_CFG1);
  162. val &= ~(3 << shift);
  163. val |= mask << shift;
  164. rt_sysc_w32(val, SYSC_REG_CFG1);
  165. if (priv->phy->phy_fixed[id]) {
  166. const __be32 *link = priv->phy->phy_fixed[id];
  167. int tx_fc, rx_fc;
  168. u32 val = 0;
  169. priv->phy->speed[id] = be32_to_cpup(link++);
  170. tx_fc = be32_to_cpup(link++);
  171. rx_fc = be32_to_cpup(link++);
  172. priv->phy->duplex[id] = be32_to_cpup(link++);
  173. priv->link[id] = 1;
  174. switch (priv->phy->speed[id]) {
  175. case SPEED_10:
  176. val = 0;
  177. break;
  178. case SPEED_100:
  179. val = 1;
  180. break;
  181. case SPEED_1000:
  182. val = 2;
  183. break;
  184. default:
  185. dev_err(priv->device, "invalid link speed: %d\n",
  186. priv->phy->speed[id]);
  187. priv->phy->phy_fixed[id] = 0;
  188. return;
  189. }
  190. val = PMCR_SPEED(val);
  191. val |= PMCR_LINK | PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
  192. PMCR_TX_EN | PMCR_FORCE | PMCR_MAC_MODE | PMCR_IPG;
  193. if (tx_fc)
  194. val |= PMCR_TX_FC;
  195. if (rx_fc)
  196. val |= PMCR_RX_FC;
  197. if (priv->phy->duplex[id])
  198. val |= PMCR_DUPLEX;
  199. mtk_switch_w32(gsw, val, GSW_REG_PORT_PMCR(id));
  200. dev_info(priv->device, "using fixed link parameters\n");
  201. return;
  202. }
  203. if (priv->phy->phy_node[id] && mdiobus_get_phy(priv->mii_bus, id)) {
  204. u32 val = PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
  205. PMCR_TX_EN | PMCR_MAC_MODE | PMCR_IPG;
  206. mtk_switch_w32(gsw, val, GSW_REG_PORT_PMCR(id));
  207. fe_connect_phy_node(priv, priv->phy->phy_node[id]);
  208. gsw->autopoll |= BIT(id);
  209. mt7620_auto_poll(gsw);
  210. return;
  211. }
  212. }
  213. static void mt7620_fe_reset(void)
  214. {
  215. fe_reset(MT7620A_RESET_FE | MT7620A_RESET_ESW);
  216. }
  217. static void mt7620_rxcsum_config(bool enable)
  218. {
  219. if (enable)
  220. fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) | (GDMA_ICS_EN |
  221. GDMA_TCS_EN | GDMA_UCS_EN),
  222. MT7620A_GDMA1_FWD_CFG);
  223. else
  224. fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) & ~(GDMA_ICS_EN |
  225. GDMA_TCS_EN | GDMA_UCS_EN),
  226. MT7620A_GDMA1_FWD_CFG);
  227. }
  228. static void mt7620_txcsum_config(bool enable)
  229. {
  230. if (enable)
  231. fe_w32(fe_r32(MT7620A_CDMA_CSG_CFG) | (CDMA_ICS_EN |
  232. CDMA_UCS_EN | CDMA_TCS_EN),
  233. MT7620A_CDMA_CSG_CFG);
  234. else
  235. fe_w32(fe_r32(MT7620A_CDMA_CSG_CFG) & ~(CDMA_ICS_EN |
  236. CDMA_UCS_EN | CDMA_TCS_EN),
  237. MT7620A_CDMA_CSG_CFG);
  238. }
  239. static int mt7620_fwd_config(struct fe_priv *priv)
  240. {
  241. struct net_device *dev = priv_netdev(priv);
  242. fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) & ~7, MT7620A_GDMA1_FWD_CFG);
  243. mt7620_txcsum_config((dev->features & NETIF_F_IP_CSUM));
  244. mt7620_rxcsum_config((dev->features & NETIF_F_RXCSUM));
  245. return 0;
  246. }
  247. static void mt7620_tx_dma(struct fe_tx_dma *txd)
  248. {
  249. }
  250. static void mt7620_init_data(struct fe_soc_data *data,
  251. struct net_device *netdev)
  252. {
  253. struct fe_priv *priv = netdev_priv(netdev);
  254. priv->flags = FE_FLAG_PADDING_64B | FE_FLAG_RX_2B_OFFSET |
  255. FE_FLAG_RX_SG_DMA | FE_FLAG_HAS_SWITCH;
  256. netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
  257. NETIF_F_HW_VLAN_CTAG_TX;
  258. if (mt7620_get_eco() >= 5)
  259. netdev->hw_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
  260. NETIF_F_IPV6_CSUM;
  261. }
  262. static struct fe_soc_data mt7620_data = {
  263. .init_data = mt7620_init_data,
  264. .reset_fe = mt7620_fe_reset,
  265. .set_mac = mt7620_set_mac,
  266. .fwd_config = mt7620_fwd_config,
  267. .tx_dma = mt7620_tx_dma,
  268. .switch_init = mtk_gsw_init,
  269. .switch_config = mt7620_gsw_config,
  270. .port_init = mt7620_port_init,
  271. .reg_table = mt7620_reg_table,
  272. .pdma_glo_cfg = FE_PDMA_SIZE_16DWORDS,
  273. .rx_int = RT5350_RX_DONE_INT,
  274. .tx_int = RT5350_TX_DONE_INT,
  275. .status_int = MT7620_FE_GDM1_AF,
  276. .checksum_bit = MT7620_L4_VALID,
  277. .has_carrier = mt7620_has_carrier,
  278. .mdio_read = mt7620_mdio_read,
  279. .mdio_write = mt7620_mdio_write,
  280. .mdio_adjust_link = mt7620_mdio_link_adjust,
  281. };
  282. const struct of_device_id of_fe_match[] = {
  283. { .compatible = "mediatek,mt7620-eth", .data = &mt7620_data },
  284. {},
  285. };
  286. MODULE_DEVICE_TABLE(of, of_fe_match);