dwmac4_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * This is the driver for the GMAC on-chip Ethernet controller for ST SoCs.
  3. * DWC Ether MAC version 4.00 has been used for developing this code.
  4. *
  5. * This only implements the mac core functions for this chip.
  6. *
  7. * Copyright (C) 2015 STMicroelectronics Ltd
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * Author: Alexandre Torgue <alexandre.torgue@st.com>
  14. */
  15. #include <linux/crc32.h>
  16. #include <linux/slab.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/io.h>
  19. #include "stmmac_pcs.h"
  20. #include "dwmac4.h"
  21. static void dwmac4_core_init(struct mac_device_info *hw, int mtu)
  22. {
  23. void __iomem *ioaddr = hw->pcsr;
  24. u32 value = readl(ioaddr + GMAC_CONFIG);
  25. value |= GMAC_CORE_INIT;
  26. if (mtu > 1500)
  27. value |= GMAC_CONFIG_2K;
  28. if (mtu > 2000)
  29. value |= GMAC_CONFIG_JE;
  30. if (hw->ps) {
  31. value |= GMAC_CONFIG_TE;
  32. if (hw->ps == SPEED_1000) {
  33. value &= ~GMAC_CONFIG_PS;
  34. } else {
  35. value |= GMAC_CONFIG_PS;
  36. if (hw->ps == SPEED_10)
  37. value &= ~GMAC_CONFIG_FES;
  38. else
  39. value |= GMAC_CONFIG_FES;
  40. }
  41. }
  42. writel(value, ioaddr + GMAC_CONFIG);
  43. /* Mask GMAC interrupts */
  44. value = GMAC_INT_DEFAULT_MASK;
  45. if (hw->pmt)
  46. value |= GMAC_INT_PMT_EN;
  47. if (hw->pcs)
  48. value |= GMAC_PCS_IRQ_DEFAULT;
  49. writel(value, ioaddr + GMAC_INT_EN);
  50. }
  51. static void dwmac4_dump_regs(struct mac_device_info *hw)
  52. {
  53. void __iomem *ioaddr = hw->pcsr;
  54. int i;
  55. pr_debug("\tDWMAC4 regs (base addr = 0x%p)\n", ioaddr);
  56. for (i = 0; i < GMAC_REG_NUM; i++) {
  57. int offset = i * 4;
  58. pr_debug("\tReg No. %d (offset 0x%x): 0x%08x\n", i,
  59. offset, readl(ioaddr + offset));
  60. }
  61. }
  62. static int dwmac4_rx_ipc_enable(struct mac_device_info *hw)
  63. {
  64. void __iomem *ioaddr = hw->pcsr;
  65. u32 value = readl(ioaddr + GMAC_CONFIG);
  66. if (hw->rx_csum)
  67. value |= GMAC_CONFIG_IPC;
  68. else
  69. value &= ~GMAC_CONFIG_IPC;
  70. writel(value, ioaddr + GMAC_CONFIG);
  71. value = readl(ioaddr + GMAC_CONFIG);
  72. return !!(value & GMAC_CONFIG_IPC);
  73. }
  74. static void dwmac4_pmt(struct mac_device_info *hw, unsigned long mode)
  75. {
  76. void __iomem *ioaddr = hw->pcsr;
  77. unsigned int pmt = 0;
  78. if (mode & WAKE_MAGIC) {
  79. pr_debug("GMAC: WOL Magic frame\n");
  80. pmt |= power_down | magic_pkt_en;
  81. }
  82. if (mode & WAKE_UCAST) {
  83. pr_debug("GMAC: WOL on global unicast\n");
  84. pmt |= power_down | global_unicast | wake_up_frame_en;
  85. }
  86. writel(pmt, ioaddr + GMAC_PMT);
  87. }
  88. static void dwmac4_set_umac_addr(struct mac_device_info *hw,
  89. unsigned char *addr, unsigned int reg_n)
  90. {
  91. void __iomem *ioaddr = hw->pcsr;
  92. stmmac_dwmac4_set_mac_addr(ioaddr, addr, GMAC_ADDR_HIGH(reg_n),
  93. GMAC_ADDR_LOW(reg_n));
  94. }
  95. static void dwmac4_get_umac_addr(struct mac_device_info *hw,
  96. unsigned char *addr, unsigned int reg_n)
  97. {
  98. void __iomem *ioaddr = hw->pcsr;
  99. stmmac_dwmac4_get_mac_addr(ioaddr, addr, GMAC_ADDR_HIGH(reg_n),
  100. GMAC_ADDR_LOW(reg_n));
  101. }
  102. static void dwmac4_set_filter(struct mac_device_info *hw,
  103. struct net_device *dev)
  104. {
  105. void __iomem *ioaddr = (void __iomem *)dev->base_addr;
  106. unsigned int value = 0;
  107. if (dev->flags & IFF_PROMISC) {
  108. value = GMAC_PACKET_FILTER_PR;
  109. } else if ((dev->flags & IFF_ALLMULTI) ||
  110. (netdev_mc_count(dev) > HASH_TABLE_SIZE)) {
  111. /* Pass all multi */
  112. value = GMAC_PACKET_FILTER_PM;
  113. /* Set the 64 bits of the HASH tab. To be updated if taller
  114. * hash table is used
  115. */
  116. writel(0xffffffff, ioaddr + GMAC_HASH_TAB_0_31);
  117. writel(0xffffffff, ioaddr + GMAC_HASH_TAB_32_63);
  118. } else if (!netdev_mc_empty(dev)) {
  119. u32 mc_filter[2];
  120. struct netdev_hw_addr *ha;
  121. /* Hash filter for multicast */
  122. value = GMAC_PACKET_FILTER_HMC;
  123. memset(mc_filter, 0, sizeof(mc_filter));
  124. netdev_for_each_mc_addr(ha, dev) {
  125. /* The upper 6 bits of the calculated CRC are used to
  126. * index the content of the Hash Table Reg 0 and 1.
  127. */
  128. int bit_nr =
  129. (bitrev32(~crc32_le(~0, ha->addr, 6)) >> 26);
  130. /* The most significant bit determines the register
  131. * to use while the other 5 bits determines the bit
  132. * within the selected register
  133. */
  134. mc_filter[bit_nr >> 5] |= (1 << (bit_nr & 0x1F));
  135. }
  136. writel(mc_filter[0], ioaddr + GMAC_HASH_TAB_0_31);
  137. writel(mc_filter[1], ioaddr + GMAC_HASH_TAB_32_63);
  138. }
  139. /* Handle multiple unicast addresses */
  140. if (netdev_uc_count(dev) > GMAC_MAX_PERFECT_ADDRESSES) {
  141. /* Switch to promiscuous mode if more than 128 addrs
  142. * are required
  143. */
  144. value |= GMAC_PACKET_FILTER_PR;
  145. } else if (!netdev_uc_empty(dev)) {
  146. int reg = 1;
  147. struct netdev_hw_addr *ha;
  148. netdev_for_each_uc_addr(ha, dev) {
  149. dwmac4_set_umac_addr(hw, ha->addr, reg);
  150. reg++;
  151. }
  152. }
  153. writel(value, ioaddr + GMAC_PACKET_FILTER);
  154. }
  155. static void dwmac4_flow_ctrl(struct mac_device_info *hw, unsigned int duplex,
  156. unsigned int fc, unsigned int pause_time)
  157. {
  158. void __iomem *ioaddr = hw->pcsr;
  159. u32 channel = STMMAC_CHAN0; /* FIXME */
  160. unsigned int flow = 0;
  161. pr_debug("GMAC Flow-Control:\n");
  162. if (fc & FLOW_RX) {
  163. pr_debug("\tReceive Flow-Control ON\n");
  164. flow |= GMAC_RX_FLOW_CTRL_RFE;
  165. writel(flow, ioaddr + GMAC_RX_FLOW_CTRL);
  166. }
  167. if (fc & FLOW_TX) {
  168. pr_debug("\tTransmit Flow-Control ON\n");
  169. flow |= GMAC_TX_FLOW_CTRL_TFE;
  170. writel(flow, ioaddr + GMAC_QX_TX_FLOW_CTRL(channel));
  171. if (duplex) {
  172. pr_debug("\tduplex mode: PAUSE %d\n", pause_time);
  173. flow |= (pause_time << GMAC_TX_FLOW_CTRL_PT_SHIFT);
  174. writel(flow, ioaddr + GMAC_QX_TX_FLOW_CTRL(channel));
  175. }
  176. }
  177. }
  178. static void dwmac4_ctrl_ane(void __iomem *ioaddr, bool ane, bool srgmi_ral,
  179. bool loopback)
  180. {
  181. dwmac_ctrl_ane(ioaddr, GMAC_PCS_BASE, ane, srgmi_ral, loopback);
  182. }
  183. static void dwmac4_rane(void __iomem *ioaddr, bool restart)
  184. {
  185. dwmac_rane(ioaddr, GMAC_PCS_BASE, restart);
  186. }
  187. static void dwmac4_get_adv_lp(void __iomem *ioaddr, struct rgmii_adv *adv)
  188. {
  189. dwmac_get_adv_lp(ioaddr, GMAC_PCS_BASE, adv);
  190. }
  191. /* RGMII or SMII interface */
  192. static void dwmac4_phystatus(void __iomem *ioaddr, struct stmmac_extra_stats *x)
  193. {
  194. u32 status;
  195. status = readl(ioaddr + GMAC_PHYIF_CONTROL_STATUS);
  196. x->irq_rgmii_n++;
  197. /* Check the link status */
  198. if (status & GMAC_PHYIF_CTRLSTATUS_LNKSTS) {
  199. int speed_value;
  200. x->pcs_link = 1;
  201. speed_value = ((status & GMAC_PHYIF_CTRLSTATUS_SPEED) >>
  202. GMAC_PHYIF_CTRLSTATUS_SPEED_SHIFT);
  203. if (speed_value == GMAC_PHYIF_CTRLSTATUS_SPEED_125)
  204. x->pcs_speed = SPEED_1000;
  205. else if (speed_value == GMAC_PHYIF_CTRLSTATUS_SPEED_25)
  206. x->pcs_speed = SPEED_100;
  207. else
  208. x->pcs_speed = SPEED_10;
  209. x->pcs_duplex = (status & GMAC_PHYIF_CTRLSTATUS_LNKMOD_MASK);
  210. pr_info("Link is Up - %d/%s\n", (int)x->pcs_speed,
  211. x->pcs_duplex ? "Full" : "Half");
  212. } else {
  213. x->pcs_link = 0;
  214. pr_info("Link is Down\n");
  215. }
  216. }
  217. static int dwmac4_irq_status(struct mac_device_info *hw,
  218. struct stmmac_extra_stats *x)
  219. {
  220. void __iomem *ioaddr = hw->pcsr;
  221. u32 mtl_int_qx_status;
  222. u32 intr_status;
  223. int ret = 0;
  224. intr_status = readl(ioaddr + GMAC_INT_STATUS);
  225. /* Not used events (e.g. MMC interrupts) are not handled. */
  226. if ((intr_status & mmc_tx_irq))
  227. x->mmc_tx_irq_n++;
  228. if (unlikely(intr_status & mmc_rx_irq))
  229. x->mmc_rx_irq_n++;
  230. if (unlikely(intr_status & mmc_rx_csum_offload_irq))
  231. x->mmc_rx_csum_offload_irq_n++;
  232. /* Clear the PMT bits 5 and 6 by reading the PMT status reg */
  233. if (unlikely(intr_status & pmt_irq)) {
  234. readl(ioaddr + GMAC_PMT);
  235. x->irq_receive_pmt_irq_n++;
  236. }
  237. mtl_int_qx_status = readl(ioaddr + MTL_INT_STATUS);
  238. /* Check MTL Interrupt: Currently only one queue is used: Q0. */
  239. if (mtl_int_qx_status & MTL_INT_Q0) {
  240. /* read Queue 0 Interrupt status */
  241. u32 status = readl(ioaddr + MTL_CHAN_INT_CTRL(STMMAC_CHAN0));
  242. if (status & MTL_RX_OVERFLOW_INT) {
  243. /* clear Interrupt */
  244. writel(status | MTL_RX_OVERFLOW_INT,
  245. ioaddr + MTL_CHAN_INT_CTRL(STMMAC_CHAN0));
  246. ret = CORE_IRQ_MTL_RX_OVERFLOW;
  247. }
  248. }
  249. dwmac_pcs_isr(ioaddr, GMAC_PCS_BASE, intr_status, x);
  250. if (intr_status & PCS_RGSMIIIS_IRQ)
  251. dwmac4_phystatus(ioaddr, x);
  252. return ret;
  253. }
  254. static void dwmac4_debug(void __iomem *ioaddr, struct stmmac_extra_stats *x)
  255. {
  256. u32 value;
  257. /* Currently only channel 0 is supported */
  258. value = readl(ioaddr + MTL_CHAN_TX_DEBUG(STMMAC_CHAN0));
  259. if (value & MTL_DEBUG_TXSTSFSTS)
  260. x->mtl_tx_status_fifo_full++;
  261. if (value & MTL_DEBUG_TXFSTS)
  262. x->mtl_tx_fifo_not_empty++;
  263. if (value & MTL_DEBUG_TWCSTS)
  264. x->mmtl_fifo_ctrl++;
  265. if (value & MTL_DEBUG_TRCSTS_MASK) {
  266. u32 trcsts = (value & MTL_DEBUG_TRCSTS_MASK)
  267. >> MTL_DEBUG_TRCSTS_SHIFT;
  268. if (trcsts == MTL_DEBUG_TRCSTS_WRITE)
  269. x->mtl_tx_fifo_read_ctrl_write++;
  270. else if (trcsts == MTL_DEBUG_TRCSTS_TXW)
  271. x->mtl_tx_fifo_read_ctrl_wait++;
  272. else if (trcsts == MTL_DEBUG_TRCSTS_READ)
  273. x->mtl_tx_fifo_read_ctrl_read++;
  274. else
  275. x->mtl_tx_fifo_read_ctrl_idle++;
  276. }
  277. if (value & MTL_DEBUG_TXPAUSED)
  278. x->mac_tx_in_pause++;
  279. value = readl(ioaddr + MTL_CHAN_RX_DEBUG(STMMAC_CHAN0));
  280. if (value & MTL_DEBUG_RXFSTS_MASK) {
  281. u32 rxfsts = (value & MTL_DEBUG_RXFSTS_MASK)
  282. >> MTL_DEBUG_RRCSTS_SHIFT;
  283. if (rxfsts == MTL_DEBUG_RXFSTS_FULL)
  284. x->mtl_rx_fifo_fill_level_full++;
  285. else if (rxfsts == MTL_DEBUG_RXFSTS_AT)
  286. x->mtl_rx_fifo_fill_above_thresh++;
  287. else if (rxfsts == MTL_DEBUG_RXFSTS_BT)
  288. x->mtl_rx_fifo_fill_below_thresh++;
  289. else
  290. x->mtl_rx_fifo_fill_level_empty++;
  291. }
  292. if (value & MTL_DEBUG_RRCSTS_MASK) {
  293. u32 rrcsts = (value & MTL_DEBUG_RRCSTS_MASK) >>
  294. MTL_DEBUG_RRCSTS_SHIFT;
  295. if (rrcsts == MTL_DEBUG_RRCSTS_FLUSH)
  296. x->mtl_rx_fifo_read_ctrl_flush++;
  297. else if (rrcsts == MTL_DEBUG_RRCSTS_RSTAT)
  298. x->mtl_rx_fifo_read_ctrl_read_data++;
  299. else if (rrcsts == MTL_DEBUG_RRCSTS_RDATA)
  300. x->mtl_rx_fifo_read_ctrl_status++;
  301. else
  302. x->mtl_rx_fifo_read_ctrl_idle++;
  303. }
  304. if (value & MTL_DEBUG_RWCSTS)
  305. x->mtl_rx_fifo_ctrl_active++;
  306. /* GMAC debug */
  307. value = readl(ioaddr + GMAC_DEBUG);
  308. if (value & GMAC_DEBUG_TFCSTS_MASK) {
  309. u32 tfcsts = (value & GMAC_DEBUG_TFCSTS_MASK)
  310. >> GMAC_DEBUG_TFCSTS_SHIFT;
  311. if (tfcsts == GMAC_DEBUG_TFCSTS_XFER)
  312. x->mac_tx_frame_ctrl_xfer++;
  313. else if (tfcsts == GMAC_DEBUG_TFCSTS_GEN_PAUSE)
  314. x->mac_tx_frame_ctrl_pause++;
  315. else if (tfcsts == GMAC_DEBUG_TFCSTS_WAIT)
  316. x->mac_tx_frame_ctrl_wait++;
  317. else
  318. x->mac_tx_frame_ctrl_idle++;
  319. }
  320. if (value & GMAC_DEBUG_TPESTS)
  321. x->mac_gmii_tx_proto_engine++;
  322. if (value & GMAC_DEBUG_RFCFCSTS_MASK)
  323. x->mac_rx_frame_ctrl_fifo = (value & GMAC_DEBUG_RFCFCSTS_MASK)
  324. >> GMAC_DEBUG_RFCFCSTS_SHIFT;
  325. if (value & GMAC_DEBUG_RPESTS)
  326. x->mac_gmii_rx_proto_engine++;
  327. }
  328. static const struct stmmac_ops dwmac4_ops = {
  329. .core_init = dwmac4_core_init,
  330. .rx_ipc = dwmac4_rx_ipc_enable,
  331. .dump_regs = dwmac4_dump_regs,
  332. .host_irq_status = dwmac4_irq_status,
  333. .flow_ctrl = dwmac4_flow_ctrl,
  334. .pmt = dwmac4_pmt,
  335. .set_umac_addr = dwmac4_set_umac_addr,
  336. .get_umac_addr = dwmac4_get_umac_addr,
  337. .pcs_ctrl_ane = dwmac4_ctrl_ane,
  338. .pcs_rane = dwmac4_rane,
  339. .pcs_get_adv_lp = dwmac4_get_adv_lp,
  340. .debug = dwmac4_debug,
  341. .set_filter = dwmac4_set_filter,
  342. };
  343. struct mac_device_info *dwmac4_setup(void __iomem *ioaddr, int mcbins,
  344. int perfect_uc_entries, int *synopsys_id)
  345. {
  346. struct mac_device_info *mac;
  347. u32 hwid = readl(ioaddr + GMAC_VERSION);
  348. mac = kzalloc(sizeof(const struct mac_device_info), GFP_KERNEL);
  349. if (!mac)
  350. return NULL;
  351. mac->pcsr = ioaddr;
  352. mac->multicast_filter_bins = mcbins;
  353. mac->unicast_filter_entries = perfect_uc_entries;
  354. mac->mcast_bits_log2 = 0;
  355. if (mac->multicast_filter_bins)
  356. mac->mcast_bits_log2 = ilog2(mac->multicast_filter_bins);
  357. mac->mac = &dwmac4_ops;
  358. mac->link.port = GMAC_CONFIG_PS;
  359. mac->link.duplex = GMAC_CONFIG_DM;
  360. mac->link.speed = GMAC_CONFIG_FES;
  361. mac->mii.addr = GMAC_MDIO_ADDR;
  362. mac->mii.data = GMAC_MDIO_DATA;
  363. /* Get and dump the chip ID */
  364. *synopsys_id = stmmac_get_synopsys_id(hwid);
  365. if (*synopsys_id > DWMAC_CORE_4_00)
  366. mac->dma = &dwmac410_dma_ops;
  367. else
  368. mac->dma = &dwmac4_dma_ops;
  369. return mac;
  370. }