0509-net-next-mediatek-add-support-for-mt7621.patch 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. From 107ff718dad1c8f6abbf6247d6796a4535b71276 Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Mon, 14 Dec 2015 23:50:53 +0100
  4. Subject: [PATCH 509/513] net-next: mediatek: add support for mt7621
  5. Add support for SoCs from the mt7620 family. This include mt7620 and mt7621.
  6. These all have one dedicated external gbit port and a builtin 5 port 100mbit
  7. switch. Additionally one of the 5 switch ports can be changed to become an
  8. additional gbit port that we can attach a phy to. This patch includes
  9. rudimentary code to power up the switch. There are a lot of magic values
  10. that get written to the switch and the internal phys. These values come
  11. straight from the SDK driver.
  12. Signed-off-by: John Crispin <blogic@openwrt.org>
  13. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  14. Signed-off-by: Michael Lee <igvtee@gmail.com>
  15. ---
  16. drivers/net/ethernet/mediatek/soc_mt7621.c | 186 ++++++++++++++++++++++++++++
  17. 1 file changed, 186 insertions(+)
  18. create mode 100644 drivers/net/ethernet/mediatek/soc_mt7621.c
  19. --- /dev/null
  20. +++ b/drivers/net/ethernet/mediatek/soc_mt7621.c
  21. @@ -0,0 +1,185 @@
  22. +/* This program is free software; you can redistribute it and/or modify
  23. + * it under the terms of the GNU General Public License as published by
  24. + * the Free Software Foundation; version 2 of the License
  25. + *
  26. + * This program is distributed in the hope that it will be useful,
  27. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. + * GNU General Public License for more details.
  30. + *
  31. + * Copyright (C) 2009-2015 John Crispin <blogic@openwrt.org>
  32. + * Copyright (C) 2009-2015 Felix Fietkau <nbd@nbd.name>
  33. + * Copyright (C) 2013-2015 Michael Lee <igvtee@gmail.com>
  34. + */
  35. +
  36. +#include <linux/module.h>
  37. +#include <linux/platform_device.h>
  38. +#include <linux/if_vlan.h>
  39. +#include <linux/of_net.h>
  40. +
  41. +#include <asm/mach-ralink/ralink_regs.h>
  42. +
  43. +#include "mtk_eth_soc.h"
  44. +#include "gsw_mt7620.h"
  45. +#include "mt7530.h"
  46. +#include "mdio.h"
  47. +
  48. +#define MT7620A_CDMA_CSG_CFG 0x400
  49. +#define MT7621_CDMP_IG_CTRL (MT7620A_CDMA_CSG_CFG + 0x00)
  50. +#define MT7621_CDMP_EG_CTRL (MT7620A_CDMA_CSG_CFG + 0x04)
  51. +#define MT7621_RESET_FE BIT(6)
  52. +#define MT7621_L4_VALID BIT(24)
  53. +
  54. +#define MT7621_TX_DMA_UDF BIT(19)
  55. +#define MT7621_TX_DMA_FPORT BIT(25)
  56. +
  57. +#define CDMA_ICS_EN BIT(2)
  58. +#define CDMA_UCS_EN BIT(1)
  59. +#define CDMA_TCS_EN BIT(0)
  60. +
  61. +#define GDMA_ICS_EN BIT(22)
  62. +#define GDMA_TCS_EN BIT(21)
  63. +#define GDMA_UCS_EN BIT(20)
  64. +
  65. +/* frame engine counters */
  66. +#define MT7621_REG_MIB_OFFSET 0x2000
  67. +#define MT7621_PPE_AC_BCNT0 (MT7621_REG_MIB_OFFSET + 0x00)
  68. +#define MT7621_GDM1_TX_GBCNT (MT7621_REG_MIB_OFFSET + 0x400)
  69. +#define MT7621_GDM2_TX_GBCNT (MT7621_GDM1_TX_GBCNT + 0x40)
  70. +
  71. +#define GSW_REG_GDMA1_MAC_ADRL 0x508
  72. +#define GSW_REG_GDMA1_MAC_ADRH 0x50C
  73. +
  74. +#define MT7621_FE_RST_GL (FE_FE_OFFSET + 0x04)
  75. +#define MT7620_FE_INT_STATUS2 (FE_FE_OFFSET + 0x08)
  76. +
  77. +/* FE_INT_STATUS reg on mt7620 define CNT_GDM1_AF at BIT(29)
  78. + * but after test it should be BIT(13).
  79. + */
  80. +#define MT7620_FE_GDM1_AF BIT(13)
  81. +#define MT7621_FE_GDM1_AF BIT(28)
  82. +#define MT7621_FE_GDM2_AF BIT(29)
  83. +
  84. +static const u16 mt7621_reg_table[FE_REG_COUNT] = {
  85. + [FE_REG_PDMA_GLO_CFG] = RT5350_PDMA_GLO_CFG,
  86. + [FE_REG_PDMA_RST_CFG] = RT5350_PDMA_RST_CFG,
  87. + [FE_REG_DLY_INT_CFG] = RT5350_DLY_INT_CFG,
  88. + [FE_REG_TX_BASE_PTR0] = RT5350_TX_BASE_PTR0,
  89. + [FE_REG_TX_MAX_CNT0] = RT5350_TX_MAX_CNT0,
  90. + [FE_REG_TX_CTX_IDX0] = RT5350_TX_CTX_IDX0,
  91. + [FE_REG_TX_DTX_IDX0] = RT5350_TX_DTX_IDX0,
  92. + [FE_REG_RX_BASE_PTR0] = RT5350_RX_BASE_PTR0,
  93. + [FE_REG_RX_MAX_CNT0] = RT5350_RX_MAX_CNT0,
  94. + [FE_REG_RX_CALC_IDX0] = RT5350_RX_CALC_IDX0,
  95. + [FE_REG_RX_DRX_IDX0] = RT5350_RX_DRX_IDX0,
  96. + [FE_REG_FE_INT_ENABLE] = RT5350_FE_INT_ENABLE,
  97. + [FE_REG_FE_INT_STATUS] = RT5350_FE_INT_STATUS,
  98. + [FE_REG_FE_DMA_VID_BASE] = 0,
  99. + [FE_REG_FE_COUNTER_BASE] = MT7621_GDM1_TX_GBCNT,
  100. + [FE_REG_FE_RST_GL] = MT7621_FE_RST_GL,
  101. + [FE_REG_FE_INT_STATUS2] = MT7620_FE_INT_STATUS2,
  102. +};
  103. +
  104. +static int mt7621_gsw_config(struct fe_priv *priv)
  105. +{
  106. + if (priv->mii_bus && priv->mii_bus->phy_map[0x1f])
  107. + mt7530_probe(priv->device, NULL, priv->mii_bus, 1);
  108. +
  109. + return 0;
  110. +}
  111. +
  112. +static void mt7621_fe_reset(void)
  113. +{
  114. + fe_reset(MT7621_RESET_FE);
  115. +}
  116. +
  117. +static void mt7621_rxcsum_config(bool enable)
  118. +{
  119. + if (enable)
  120. + fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) | (GDMA_ICS_EN |
  121. + GDMA_TCS_EN | GDMA_UCS_EN),
  122. + MT7620A_GDMA1_FWD_CFG);
  123. + else
  124. + fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) & ~(GDMA_ICS_EN |
  125. + GDMA_TCS_EN | GDMA_UCS_EN),
  126. + MT7620A_GDMA1_FWD_CFG);
  127. +}
  128. +
  129. +static void mt7621_rxvlan_config(bool enable)
  130. +{
  131. + if (enable)
  132. + fe_w32(1, MT7621_CDMP_EG_CTRL);
  133. + else
  134. + fe_w32(0, MT7621_CDMP_EG_CTRL);
  135. +}
  136. +
  137. +static int mt7621_fwd_config(struct fe_priv *priv)
  138. +{
  139. + struct net_device *dev = priv_netdev(priv);
  140. +
  141. + fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) & ~0xffff,
  142. + MT7620A_GDMA1_FWD_CFG);
  143. +
  144. + /* mt7621 doesn't have txcsum config */
  145. + mt7621_rxcsum_config((dev->features & NETIF_F_RXCSUM));
  146. + mt7621_rxvlan_config(priv->flags & FE_FLAG_RX_VLAN_CTAG);
  147. +
  148. + return 0;
  149. +}
  150. +
  151. +static void mt7621_tx_dma(struct fe_tx_dma *txd)
  152. +{
  153. + txd->txd4 = MT7621_TX_DMA_FPORT;
  154. +}
  155. +
  156. +static void mt7621_init_data(struct fe_soc_data *data,
  157. + struct net_device *netdev)
  158. +{
  159. + struct fe_priv *priv = netdev_priv(netdev);
  160. +
  161. + priv->flags = FE_FLAG_PADDING_64B | FE_FLAG_RX_2B_OFFSET |
  162. + FE_FLAG_RX_SG_DMA | FE_FLAG_NAPI_WEIGHT |
  163. + FE_FLAG_HAS_SWITCH | FE_FLAG_JUMBO_FRAME;
  164. +
  165. + netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
  166. + NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_SG | NETIF_F_TSO |
  167. + NETIF_F_TSO6 | NETIF_F_IPV6_CSUM;
  168. +}
  169. +
  170. +static void mt7621_set_mac(struct fe_priv *priv, unsigned char *mac)
  171. +{
  172. + unsigned long flags;
  173. +
  174. + spin_lock_irqsave(&priv->page_lock, flags);
  175. + fe_w32((mac[0] << 8) | mac[1], GSW_REG_GDMA1_MAC_ADRH);
  176. + fe_w32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
  177. + GSW_REG_GDMA1_MAC_ADRL);
  178. + spin_unlock_irqrestore(&priv->page_lock, flags);
  179. +}
  180. +
  181. +static struct fe_soc_data mt7621_data = {
  182. + .init_data = mt7621_init_data,
  183. + .reset_fe = mt7621_fe_reset,
  184. + .set_mac = mt7621_set_mac,
  185. + .fwd_config = mt7621_fwd_config,
  186. + .tx_dma = mt7621_tx_dma,
  187. + .switch_init = mtk_gsw_init,
  188. + .switch_config = mt7621_gsw_config,
  189. + .reg_table = mt7621_reg_table,
  190. + .pdma_glo_cfg = FE_PDMA_SIZE_16DWORDS,
  191. + .rx_int = RT5350_RX_DONE_INT,
  192. + .tx_int = RT5350_TX_DONE_INT,
  193. + .status_int = (MT7621_FE_GDM1_AF | MT7621_FE_GDM2_AF),
  194. + .checksum_bit = MT7621_L4_VALID,
  195. + .has_carrier = mt7620_has_carrier,
  196. + .mdio_read = mt7620_mdio_read,
  197. + .mdio_write = mt7620_mdio_write,
  198. + .mdio_adjust_link = mt7620_mdio_link_adjust,
  199. +};
  200. +
  201. +const struct of_device_id of_fe_match[] = {
  202. + { .compatible = "mediatek,mt7621-eth", .data = &mt7621_data },
  203. + {},
  204. +};
  205. +
  206. +MODULE_DEVICE_TABLE(of, of_fe_match);