dwmac1000_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*******************************************************************************
  2. This is the driver for the GMAC on-chip Ethernet controller for ST SoCs.
  3. DWC Ether MAC 10/100/1000 Universal version 3.41a has been used for
  4. developing this code.
  5. This only implements the mac core functions for this chip.
  6. Copyright (C) 2007-2009 STMicroelectronics Ltd
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms and conditions of the GNU General Public License,
  9. version 2, as published by the Free Software Foundation.
  10. This program is distributed in the hope it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. more details.
  14. You should have received a copy of the GNU General Public License along with
  15. this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. The full GNU General Public License is included in this distribution in
  18. the file called "COPYING".
  19. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  20. *******************************************************************************/
  21. #include <linux/crc32.h>
  22. #include <linux/slab.h>
  23. #include <linux/ethtool.h>
  24. #include <asm/io.h>
  25. #include "dwmac1000.h"
  26. static void dwmac1000_core_init(struct mac_device_info *hw, int mtu)
  27. {
  28. void __iomem *ioaddr = hw->pcsr;
  29. u32 value = readl(ioaddr + GMAC_CONTROL);
  30. value |= GMAC_CORE_INIT;
  31. if (mtu > 1500)
  32. value |= GMAC_CONTROL_2K;
  33. if (mtu > 2000)
  34. value |= GMAC_CONTROL_JE;
  35. writel(value, ioaddr + GMAC_CONTROL);
  36. /* Mask GMAC interrupts */
  37. writel(0x207, ioaddr + GMAC_INT_MASK);
  38. #ifdef STMMAC_VLAN_TAG_USED
  39. /* Tag detection without filtering */
  40. writel(0x0, ioaddr + GMAC_VLAN_TAG);
  41. #endif
  42. }
  43. static int dwmac1000_rx_ipc_enable(struct mac_device_info *hw)
  44. {
  45. void __iomem *ioaddr = hw->pcsr;
  46. u32 value = readl(ioaddr + GMAC_CONTROL);
  47. if (hw->rx_csum)
  48. value |= GMAC_CONTROL_IPC;
  49. else
  50. value &= ~GMAC_CONTROL_IPC;
  51. writel(value, ioaddr + GMAC_CONTROL);
  52. value = readl(ioaddr + GMAC_CONTROL);
  53. return !!(value & GMAC_CONTROL_IPC);
  54. }
  55. static void dwmac1000_dump_regs(struct mac_device_info *hw)
  56. {
  57. void __iomem *ioaddr = hw->pcsr;
  58. int i;
  59. pr_info("\tDWMAC1000 regs (base addr = 0x%p)\n", ioaddr);
  60. for (i = 0; i < 55; i++) {
  61. int offset = i * 4;
  62. pr_info("\tReg No. %d (offset 0x%x): 0x%08x\n", i,
  63. offset, readl(ioaddr + offset));
  64. }
  65. }
  66. static void dwmac1000_set_umac_addr(struct mac_device_info *hw,
  67. unsigned char *addr,
  68. unsigned int reg_n)
  69. {
  70. void __iomem *ioaddr = hw->pcsr;
  71. stmmac_set_mac_addr(ioaddr, addr, GMAC_ADDR_HIGH(reg_n),
  72. GMAC_ADDR_LOW(reg_n));
  73. }
  74. static void dwmac1000_get_umac_addr(struct mac_device_info *hw,
  75. unsigned char *addr,
  76. unsigned int reg_n)
  77. {
  78. void __iomem *ioaddr = hw->pcsr;
  79. stmmac_get_mac_addr(ioaddr, addr, GMAC_ADDR_HIGH(reg_n),
  80. GMAC_ADDR_LOW(reg_n));
  81. }
  82. static void dwmac1000_set_mchash(void __iomem *ioaddr, u32 *mcfilterbits,
  83. int mcbitslog2)
  84. {
  85. int numhashregs, regs;
  86. switch (mcbitslog2) {
  87. case 6:
  88. writel(mcfilterbits[0], ioaddr + GMAC_HASH_LOW);
  89. writel(mcfilterbits[1], ioaddr + GMAC_HASH_HIGH);
  90. return;
  91. break;
  92. case 7:
  93. numhashregs = 4;
  94. break;
  95. case 8:
  96. numhashregs = 8;
  97. break;
  98. default:
  99. pr_debug("STMMAC: err in setting mulitcast filter\n");
  100. return;
  101. break;
  102. }
  103. for (regs = 0; regs < numhashregs; regs++)
  104. writel(mcfilterbits[regs],
  105. ioaddr + GMAC_EXTHASH_BASE + regs * 4);
  106. }
  107. static void dwmac1000_set_filter(struct mac_device_info *hw,
  108. struct net_device *dev)
  109. {
  110. void __iomem *ioaddr = (void __iomem *)dev->base_addr;
  111. unsigned int value = 0;
  112. unsigned int perfect_addr_number = hw->unicast_filter_entries;
  113. u32 mc_filter[8];
  114. int mcbitslog2 = hw->mcast_bits_log2;
  115. pr_debug("%s: # mcasts %d, # unicast %d\n", __func__,
  116. netdev_mc_count(dev), netdev_uc_count(dev));
  117. memset(mc_filter, 0, sizeof(mc_filter));
  118. if (dev->flags & IFF_PROMISC) {
  119. value = GMAC_FRAME_FILTER_PR;
  120. } else if (dev->flags & IFF_ALLMULTI) {
  121. value = GMAC_FRAME_FILTER_PM; /* pass all multi */
  122. } else if (!netdev_mc_empty(dev)) {
  123. struct netdev_hw_addr *ha;
  124. /* Hash filter for multicast */
  125. value = GMAC_FRAME_FILTER_HMC;
  126. netdev_for_each_mc_addr(ha, dev) {
  127. /* The upper n bits of the calculated CRC are used to
  128. * index the contents of the hash table. The number of
  129. * bits used depends on the hardware configuration
  130. * selected at core configuration time.
  131. */
  132. int bit_nr = bitrev32(~crc32_le(~0, ha->addr,
  133. ETH_ALEN)) >>
  134. (32 - mcbitslog2);
  135. /* The most significant bit determines the register to
  136. * use (H/L) while the other 5 bits determine the bit
  137. * within the register.
  138. */
  139. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  140. }
  141. }
  142. dwmac1000_set_mchash(ioaddr, mc_filter, mcbitslog2);
  143. /* Handle multiple unicast addresses (perfect filtering) */
  144. if (netdev_uc_count(dev) > perfect_addr_number)
  145. /* Switch to promiscuous mode if more than unicast
  146. * addresses are requested than supported by hardware.
  147. */
  148. value |= GMAC_FRAME_FILTER_PR;
  149. else {
  150. int reg = 1;
  151. struct netdev_hw_addr *ha;
  152. netdev_for_each_uc_addr(ha, dev) {
  153. stmmac_set_mac_addr(ioaddr, ha->addr,
  154. GMAC_ADDR_HIGH(reg),
  155. GMAC_ADDR_LOW(reg));
  156. reg++;
  157. }
  158. }
  159. #ifdef FRAME_FILTER_DEBUG
  160. /* Enable Receive all mode (to debug filtering_fail errors) */
  161. value |= GMAC_FRAME_FILTER_RA;
  162. #endif
  163. writel(value, ioaddr + GMAC_FRAME_FILTER);
  164. }
  165. static void dwmac1000_flow_ctrl(struct mac_device_info *hw, unsigned int duplex,
  166. unsigned int fc, unsigned int pause_time)
  167. {
  168. void __iomem *ioaddr = hw->pcsr;
  169. /* Set flow such that DZPQ in Mac Register 6 is 0,
  170. * and unicast pause detect is enabled.
  171. */
  172. unsigned int flow = GMAC_FLOW_CTRL_UP;
  173. pr_debug("GMAC Flow-Control:\n");
  174. if (fc & FLOW_RX) {
  175. pr_debug("\tReceive Flow-Control ON\n");
  176. flow |= GMAC_FLOW_CTRL_RFE;
  177. }
  178. if (fc & FLOW_TX) {
  179. pr_debug("\tTransmit Flow-Control ON\n");
  180. flow |= GMAC_FLOW_CTRL_TFE;
  181. }
  182. if (duplex) {
  183. pr_debug("\tduplex mode: PAUSE %d\n", pause_time);
  184. flow |= (pause_time << GMAC_FLOW_CTRL_PT_SHIFT);
  185. }
  186. writel(flow, ioaddr + GMAC_FLOW_CTRL);
  187. }
  188. static void dwmac1000_pmt(struct mac_device_info *hw, unsigned long mode)
  189. {
  190. void __iomem *ioaddr = hw->pcsr;
  191. unsigned int pmt = 0;
  192. if (mode & WAKE_MAGIC) {
  193. pr_debug("GMAC: WOL Magic frame\n");
  194. pmt |= power_down | magic_pkt_en;
  195. }
  196. if (mode & WAKE_UCAST) {
  197. pr_debug("GMAC: WOL on global unicast\n");
  198. pmt |= global_unicast;
  199. }
  200. writel(pmt, ioaddr + GMAC_PMT);
  201. }
  202. static int dwmac1000_irq_status(struct mac_device_info *hw,
  203. struct stmmac_extra_stats *x)
  204. {
  205. void __iomem *ioaddr = hw->pcsr;
  206. u32 intr_status = readl(ioaddr + GMAC_INT_STATUS);
  207. int ret = 0;
  208. /* Not used events (e.g. MMC interrupts) are not handled. */
  209. if ((intr_status & mmc_tx_irq))
  210. x->mmc_tx_irq_n++;
  211. if (unlikely(intr_status & mmc_rx_irq))
  212. x->mmc_rx_irq_n++;
  213. if (unlikely(intr_status & mmc_rx_csum_offload_irq))
  214. x->mmc_rx_csum_offload_irq_n++;
  215. if (unlikely(intr_status & pmt_irq)) {
  216. /* clear the PMT bits 5 and 6 by reading the PMT status reg */
  217. readl(ioaddr + GMAC_PMT);
  218. x->irq_receive_pmt_irq_n++;
  219. }
  220. /* MAC trx/rx EEE LPI entry/exit interrupts */
  221. if (intr_status & lpiis_irq) {
  222. /* Clean LPI interrupt by reading the Reg 12 */
  223. ret = readl(ioaddr + LPI_CTRL_STATUS);
  224. if (ret & LPI_CTRL_STATUS_TLPIEN)
  225. x->irq_tx_path_in_lpi_mode_n++;
  226. if (ret & LPI_CTRL_STATUS_TLPIEX)
  227. x->irq_tx_path_exit_lpi_mode_n++;
  228. if (ret & LPI_CTRL_STATUS_RLPIEN)
  229. x->irq_rx_path_in_lpi_mode_n++;
  230. if (ret & LPI_CTRL_STATUS_RLPIEX)
  231. x->irq_rx_path_exit_lpi_mode_n++;
  232. }
  233. if ((intr_status & pcs_ane_irq) || (intr_status & pcs_link_irq)) {
  234. readl(ioaddr + GMAC_AN_STATUS);
  235. x->irq_pcs_ane_n++;
  236. }
  237. if (intr_status & rgmii_irq) {
  238. u32 status = readl(ioaddr + GMAC_S_R_GMII);
  239. x->irq_rgmii_n++;
  240. /* Save and dump the link status. */
  241. if (status & GMAC_S_R_GMII_LINK) {
  242. int speed_value = (status & GMAC_S_R_GMII_SPEED) >>
  243. GMAC_S_R_GMII_SPEED_SHIFT;
  244. x->pcs_duplex = (status & GMAC_S_R_GMII_MODE);
  245. if (speed_value == GMAC_S_R_GMII_SPEED_125)
  246. x->pcs_speed = SPEED_1000;
  247. else if (speed_value == GMAC_S_R_GMII_SPEED_25)
  248. x->pcs_speed = SPEED_100;
  249. else
  250. x->pcs_speed = SPEED_10;
  251. x->pcs_link = 1;
  252. pr_debug("%s: Link is Up - %d/%s\n", __func__,
  253. (int)x->pcs_speed,
  254. x->pcs_duplex ? "Full" : "Half");
  255. } else {
  256. x->pcs_link = 0;
  257. pr_debug("%s: Link is Down\n", __func__);
  258. }
  259. }
  260. return ret;
  261. }
  262. static void dwmac1000_set_eee_mode(struct mac_device_info *hw)
  263. {
  264. void __iomem *ioaddr = hw->pcsr;
  265. u32 value;
  266. /* Enable the link status receive on RGMII, SGMII ore SMII
  267. * receive path and instruct the transmit to enter in LPI
  268. * state.
  269. */
  270. value = readl(ioaddr + LPI_CTRL_STATUS);
  271. value |= LPI_CTRL_STATUS_LPIEN | LPI_CTRL_STATUS_LPITXA;
  272. writel(value, ioaddr + LPI_CTRL_STATUS);
  273. }
  274. static void dwmac1000_reset_eee_mode(struct mac_device_info *hw)
  275. {
  276. void __iomem *ioaddr = hw->pcsr;
  277. u32 value;
  278. value = readl(ioaddr + LPI_CTRL_STATUS);
  279. value &= ~(LPI_CTRL_STATUS_LPIEN | LPI_CTRL_STATUS_LPITXA);
  280. writel(value, ioaddr + LPI_CTRL_STATUS);
  281. }
  282. static void dwmac1000_set_eee_pls(struct mac_device_info *hw, int link)
  283. {
  284. void __iomem *ioaddr = hw->pcsr;
  285. u32 value;
  286. value = readl(ioaddr + LPI_CTRL_STATUS);
  287. if (link)
  288. value |= LPI_CTRL_STATUS_PLS;
  289. else
  290. value &= ~LPI_CTRL_STATUS_PLS;
  291. writel(value, ioaddr + LPI_CTRL_STATUS);
  292. }
  293. static void dwmac1000_set_eee_timer(struct mac_device_info *hw, int ls, int tw)
  294. {
  295. void __iomem *ioaddr = hw->pcsr;
  296. int value = ((tw & 0xffff)) | ((ls & 0x7ff) << 16);
  297. /* Program the timers in the LPI timer control register:
  298. * LS: minimum time (ms) for which the link
  299. * status from PHY should be ok before transmitting
  300. * the LPI pattern.
  301. * TW: minimum time (us) for which the core waits
  302. * after it has stopped transmitting the LPI pattern.
  303. */
  304. writel(value, ioaddr + LPI_TIMER_CTRL);
  305. }
  306. static void dwmac1000_ctrl_ane(struct mac_device_info *hw, bool restart)
  307. {
  308. void __iomem *ioaddr = hw->pcsr;
  309. /* auto negotiation enable and External Loopback enable */
  310. u32 value = GMAC_AN_CTRL_ANE | GMAC_AN_CTRL_ELE;
  311. if (restart)
  312. value |= GMAC_AN_CTRL_RAN;
  313. writel(value, ioaddr + GMAC_AN_CTRL);
  314. }
  315. static void dwmac1000_get_adv(struct mac_device_info *hw, struct rgmii_adv *adv)
  316. {
  317. void __iomem *ioaddr = hw->pcsr;
  318. u32 value = readl(ioaddr + GMAC_ANE_ADV);
  319. if (value & GMAC_ANE_FD)
  320. adv->duplex = DUPLEX_FULL;
  321. if (value & GMAC_ANE_HD)
  322. adv->duplex |= DUPLEX_HALF;
  323. adv->pause = (value & GMAC_ANE_PSE) >> GMAC_ANE_PSE_SHIFT;
  324. value = readl(ioaddr + GMAC_ANE_LPA);
  325. if (value & GMAC_ANE_FD)
  326. adv->lp_duplex = DUPLEX_FULL;
  327. if (value & GMAC_ANE_HD)
  328. adv->lp_duplex = DUPLEX_HALF;
  329. adv->lp_pause = (value & GMAC_ANE_PSE) >> GMAC_ANE_PSE_SHIFT;
  330. }
  331. static const struct stmmac_ops dwmac1000_ops = {
  332. .core_init = dwmac1000_core_init,
  333. .rx_ipc = dwmac1000_rx_ipc_enable,
  334. .dump_regs = dwmac1000_dump_regs,
  335. .host_irq_status = dwmac1000_irq_status,
  336. .set_filter = dwmac1000_set_filter,
  337. .flow_ctrl = dwmac1000_flow_ctrl,
  338. .pmt = dwmac1000_pmt,
  339. .set_umac_addr = dwmac1000_set_umac_addr,
  340. .get_umac_addr = dwmac1000_get_umac_addr,
  341. .set_eee_mode = dwmac1000_set_eee_mode,
  342. .reset_eee_mode = dwmac1000_reset_eee_mode,
  343. .set_eee_timer = dwmac1000_set_eee_timer,
  344. .set_eee_pls = dwmac1000_set_eee_pls,
  345. .ctrl_ane = dwmac1000_ctrl_ane,
  346. .get_adv = dwmac1000_get_adv,
  347. };
  348. struct mac_device_info *dwmac1000_setup(void __iomem *ioaddr, int mcbins,
  349. int perfect_uc_entries)
  350. {
  351. struct mac_device_info *mac;
  352. u32 hwid = readl(ioaddr + GMAC_VERSION);
  353. mac = kzalloc(sizeof(const struct mac_device_info), GFP_KERNEL);
  354. if (!mac)
  355. return NULL;
  356. mac->pcsr = ioaddr;
  357. mac->multicast_filter_bins = mcbins;
  358. mac->unicast_filter_entries = perfect_uc_entries;
  359. mac->mcast_bits_log2 = 0;
  360. if (mac->multicast_filter_bins)
  361. mac->mcast_bits_log2 = ilog2(mac->multicast_filter_bins);
  362. mac->mac = &dwmac1000_ops;
  363. mac->dma = &dwmac1000_dma_ops;
  364. mac->link.port = GMAC_CONTROL_PS;
  365. mac->link.duplex = GMAC_CONTROL_DM;
  366. mac->link.speed = GMAC_CONTROL_FES;
  367. mac->mii.addr = GMAC_MII_ADDR;
  368. mac->mii.data = GMAC_MII_DATA;
  369. mac->synopsys_uid = hwid;
  370. return mac;
  371. }