core.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * drivers/net/ibm_newemac/core.h
  3. *
  4. * Driver for PowerPC 4xx on-chip ethernet controller.
  5. *
  6. * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
  7. * <benh@kernel.crashing.org>
  8. *
  9. * Based on the arch/ppc version of the driver:
  10. *
  11. * Copyright (c) 2004, 2005 Zultys Technologies.
  12. * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
  13. *
  14. * Based on original work by
  15. * Armin Kuster <akuster@mvista.com>
  16. * Johnnie Peters <jpeters@mvista.com>
  17. * Copyright 2000, 2001 MontaVista Softare Inc.
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the
  21. * Free Software Foundation; either version 2 of the License, or (at your
  22. * option) any later version.
  23. *
  24. */
  25. #ifndef __IBM_NEWEMAC_CORE_H
  26. #define __IBM_NEWEMAC_CORE_H
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/list.h>
  30. #include <linux/kernel.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/of_platform.h>
  36. #include <linux/slab.h>
  37. #include <asm/io.h>
  38. #include <asm/dcr.h>
  39. #include "emac.h"
  40. #include "phy.h"
  41. #include "zmii.h"
  42. #include "rgmii.h"
  43. #include "mal.h"
  44. #include "tah.h"
  45. #include "debug.h"
  46. #define NUM_TX_BUFF CONFIG_IBM_NEW_EMAC_TXB
  47. #define NUM_RX_BUFF CONFIG_IBM_NEW_EMAC_RXB
  48. /* Simple sanity check */
  49. #if NUM_TX_BUFF > 256 || NUM_RX_BUFF > 256
  50. #error Invalid number of buffer descriptors (greater than 256)
  51. #endif
  52. #define EMAC_MIN_MTU 46
  53. /* Maximum L2 header length (VLAN tagged, no FCS) */
  54. #define EMAC_MTU_OVERHEAD (6 * 2 + 2 + 4)
  55. /* RX BD size for the given MTU */
  56. static inline int emac_rx_size(int mtu)
  57. {
  58. if (mtu > ETH_DATA_LEN)
  59. return MAL_MAX_RX_SIZE;
  60. else
  61. return mal_rx_size(ETH_DATA_LEN + EMAC_MTU_OVERHEAD);
  62. }
  63. #define EMAC_DMA_ALIGN(x) ALIGN((x), dma_get_cache_alignment())
  64. #define EMAC_RX_SKB_HEADROOM \
  65. EMAC_DMA_ALIGN(CONFIG_IBM_NEW_EMAC_RX_SKB_HEADROOM)
  66. /* Size of RX skb for the given MTU */
  67. static inline int emac_rx_skb_size(int mtu)
  68. {
  69. int size = max(mtu + EMAC_MTU_OVERHEAD, emac_rx_size(mtu));
  70. return EMAC_DMA_ALIGN(size + 2) + EMAC_RX_SKB_HEADROOM;
  71. }
  72. /* RX DMA sync size */
  73. static inline int emac_rx_sync_size(int mtu)
  74. {
  75. return EMAC_DMA_ALIGN(emac_rx_size(mtu) + 2);
  76. }
  77. /* Driver statistcs is split into two parts to make it more cache friendly:
  78. * - normal statistics (packet count, etc)
  79. * - error statistics
  80. *
  81. * When statistics is requested by ethtool, these parts are concatenated,
  82. * normal one goes first.
  83. *
  84. * Please, keep these structures in sync with emac_stats_keys.
  85. */
  86. /* Normal TX/RX Statistics */
  87. struct emac_stats {
  88. u64 rx_packets;
  89. u64 rx_bytes;
  90. u64 tx_packets;
  91. u64 tx_bytes;
  92. u64 rx_packets_csum;
  93. u64 tx_packets_csum;
  94. };
  95. /* Error statistics */
  96. struct emac_error_stats {
  97. u64 tx_undo;
  98. /* Software RX Errors */
  99. u64 rx_dropped_stack;
  100. u64 rx_dropped_oom;
  101. u64 rx_dropped_error;
  102. u64 rx_dropped_resize;
  103. u64 rx_dropped_mtu;
  104. u64 rx_stopped;
  105. /* BD reported RX errors */
  106. u64 rx_bd_errors;
  107. u64 rx_bd_overrun;
  108. u64 rx_bd_bad_packet;
  109. u64 rx_bd_runt_packet;
  110. u64 rx_bd_short_event;
  111. u64 rx_bd_alignment_error;
  112. u64 rx_bd_bad_fcs;
  113. u64 rx_bd_packet_too_long;
  114. u64 rx_bd_out_of_range;
  115. u64 rx_bd_in_range;
  116. /* EMAC IRQ reported RX errors */
  117. u64 rx_parity;
  118. u64 rx_fifo_overrun;
  119. u64 rx_overrun;
  120. u64 rx_bad_packet;
  121. u64 rx_runt_packet;
  122. u64 rx_short_event;
  123. u64 rx_alignment_error;
  124. u64 rx_bad_fcs;
  125. u64 rx_packet_too_long;
  126. u64 rx_out_of_range;
  127. u64 rx_in_range;
  128. /* Software TX Errors */
  129. u64 tx_dropped;
  130. /* BD reported TX errors */
  131. u64 tx_bd_errors;
  132. u64 tx_bd_bad_fcs;
  133. u64 tx_bd_carrier_loss;
  134. u64 tx_bd_excessive_deferral;
  135. u64 tx_bd_excessive_collisions;
  136. u64 tx_bd_late_collision;
  137. u64 tx_bd_multple_collisions;
  138. u64 tx_bd_single_collision;
  139. u64 tx_bd_underrun;
  140. u64 tx_bd_sqe;
  141. /* EMAC IRQ reported TX errors */
  142. u64 tx_parity;
  143. u64 tx_underrun;
  144. u64 tx_sqe;
  145. u64 tx_errors;
  146. };
  147. #define EMAC_ETHTOOL_STATS_COUNT ((sizeof(struct emac_stats) + \
  148. sizeof(struct emac_error_stats)) \
  149. / sizeof(u64))
  150. struct emac_instance {
  151. struct net_device *ndev;
  152. struct resource rsrc_regs;
  153. struct emac_regs __iomem *emacp;
  154. struct platform_device *ofdev;
  155. struct device_node **blist; /* bootlist entry */
  156. /* MAL linkage */
  157. u32 mal_ph;
  158. struct platform_device *mal_dev;
  159. u32 mal_rx_chan;
  160. u32 mal_tx_chan;
  161. struct mal_instance *mal;
  162. struct mal_commac commac;
  163. /* PHY infos */
  164. u32 phy_mode;
  165. u32 phy_map;
  166. u32 phy_address;
  167. u32 phy_feat_exc;
  168. struct mii_phy phy;
  169. struct mutex link_lock;
  170. struct delayed_work link_work;
  171. int link_polling;
  172. /* GPCS PHY infos */
  173. u32 gpcs_address;
  174. /* Shared MDIO if any */
  175. u32 mdio_ph;
  176. struct platform_device *mdio_dev;
  177. struct emac_instance *mdio_instance;
  178. struct mutex mdio_lock;
  179. /* ZMII infos if any */
  180. u32 zmii_ph;
  181. u32 zmii_port;
  182. struct platform_device *zmii_dev;
  183. /* RGMII infos if any */
  184. u32 rgmii_ph;
  185. u32 rgmii_port;
  186. struct platform_device *rgmii_dev;
  187. /* TAH infos if any */
  188. u32 tah_ph;
  189. u32 tah_port;
  190. struct platform_device *tah_dev;
  191. /* IRQs */
  192. int wol_irq;
  193. int emac_irq;
  194. /* OPB bus frequency in Mhz */
  195. u32 opb_bus_freq;
  196. /* Cell index within an ASIC (for clk mgmnt) */
  197. u32 cell_index;
  198. /* Max supported MTU */
  199. u32 max_mtu;
  200. /* Feature bits (from probe table) */
  201. unsigned int features;
  202. /* Tx and Rx fifo sizes & other infos in bytes */
  203. u32 tx_fifo_size;
  204. u32 tx_fifo_size_gige;
  205. u32 rx_fifo_size;
  206. u32 rx_fifo_size_gige;
  207. u32 fifo_entry_size;
  208. u32 mal_burst_size; /* move to MAL ? */
  209. /* IAHT and GAHT filter parameterization */
  210. u32 xaht_slots_shift;
  211. u32 xaht_width_shift;
  212. /* Descriptor management
  213. */
  214. struct mal_descriptor *tx_desc;
  215. int tx_cnt;
  216. int tx_slot;
  217. int ack_slot;
  218. struct mal_descriptor *rx_desc;
  219. int rx_slot;
  220. struct sk_buff *rx_sg_skb; /* 1 */
  221. int rx_skb_size;
  222. int rx_sync_size;
  223. struct sk_buff *tx_skb[NUM_TX_BUFF];
  224. struct sk_buff *rx_skb[NUM_RX_BUFF];
  225. /* Stats
  226. */
  227. struct emac_error_stats estats;
  228. struct net_device_stats nstats;
  229. struct emac_stats stats;
  230. /* Misc
  231. */
  232. int reset_failed;
  233. int stop_timeout; /* in us */
  234. int no_mcast;
  235. int mcast_pending;
  236. int opened;
  237. struct work_struct reset_work;
  238. spinlock_t lock;
  239. };
  240. /*
  241. * Features of various EMAC implementations
  242. */
  243. /*
  244. * No flow control on 40x according to the original driver
  245. */
  246. #define EMAC_FTR_NO_FLOW_CONTROL_40x 0x00000001
  247. /*
  248. * Cell is an EMAC4
  249. */
  250. #define EMAC_FTR_EMAC4 0x00000002
  251. /*
  252. * For the 440SPe, AMCC inexplicably changed the polarity of
  253. * the "operation complete" bit in the MII control register.
  254. */
  255. #define EMAC_FTR_STACR_OC_INVERT 0x00000004
  256. /*
  257. * Set if we have a TAH.
  258. */
  259. #define EMAC_FTR_HAS_TAH 0x00000008
  260. /*
  261. * Set if we have a ZMII.
  262. */
  263. #define EMAC_FTR_HAS_ZMII 0x00000010
  264. /*
  265. * Set if we have a RGMII.
  266. */
  267. #define EMAC_FTR_HAS_RGMII 0x00000020
  268. /*
  269. * Set if we have new type STACR with STAOPC
  270. */
  271. #define EMAC_FTR_HAS_NEW_STACR 0x00000040
  272. /*
  273. * Set if we need phy clock workaround for 440gx
  274. */
  275. #define EMAC_FTR_440GX_PHY_CLK_FIX 0x00000080
  276. /*
  277. * Set if we need phy clock workaround for 440ep or 440gr
  278. */
  279. #define EMAC_FTR_440EP_PHY_CLK_FIX 0x00000100
  280. /*
  281. * The 405EX and 460EX contain the EMAC4SYNC core
  282. */
  283. #define EMAC_FTR_EMAC4SYNC 0x00000200
  284. /*
  285. * Set if we need phy clock workaround for 460ex or 460gt
  286. */
  287. #define EMAC_FTR_460EX_PHY_CLK_FIX 0x00000400
  288. /* Right now, we don't quite handle the always/possible masks on the
  289. * most optimal way as we don't have a way to say something like
  290. * always EMAC4. Patches welcome.
  291. */
  292. enum {
  293. EMAC_FTRS_ALWAYS = 0,
  294. EMAC_FTRS_POSSIBLE =
  295. #ifdef CONFIG_IBM_NEW_EMAC_EMAC4
  296. EMAC_FTR_EMAC4 | EMAC_FTR_EMAC4SYNC |
  297. EMAC_FTR_HAS_NEW_STACR |
  298. EMAC_FTR_STACR_OC_INVERT | EMAC_FTR_440GX_PHY_CLK_FIX |
  299. #endif
  300. #ifdef CONFIG_IBM_NEW_EMAC_TAH
  301. EMAC_FTR_HAS_TAH |
  302. #endif
  303. #ifdef CONFIG_IBM_NEW_EMAC_ZMII
  304. EMAC_FTR_HAS_ZMII |
  305. #endif
  306. #ifdef CONFIG_IBM_NEW_EMAC_RGMII
  307. EMAC_FTR_HAS_RGMII |
  308. #endif
  309. #ifdef CONFIG_IBM_NEW_EMAC_NO_FLOW_CTRL
  310. EMAC_FTR_NO_FLOW_CONTROL_40x |
  311. #endif
  312. EMAC_FTR_460EX_PHY_CLK_FIX |
  313. EMAC_FTR_440EP_PHY_CLK_FIX,
  314. };
  315. static inline int emac_has_feature(struct emac_instance *dev,
  316. unsigned long feature)
  317. {
  318. return (EMAC_FTRS_ALWAYS & feature) ||
  319. (EMAC_FTRS_POSSIBLE & dev->features & feature);
  320. }
  321. /*
  322. * Various instances of the EMAC core have varying 1) number of
  323. * address match slots, 2) width of the registers for handling address
  324. * match slots, 3) number of registers for handling address match
  325. * slots and 4) base offset for those registers.
  326. *
  327. * These macros and inlines handle these differences based on
  328. * parameters supplied by the device structure which are, in turn,
  329. * initialized based on the "compatible" entry in the device tree.
  330. */
  331. #define EMAC4_XAHT_SLOTS_SHIFT 6
  332. #define EMAC4_XAHT_WIDTH_SHIFT 4
  333. #define EMAC4SYNC_XAHT_SLOTS_SHIFT 8
  334. #define EMAC4SYNC_XAHT_WIDTH_SHIFT 5
  335. #define EMAC_XAHT_SLOTS(dev) (1 << (dev)->xaht_slots_shift)
  336. #define EMAC_XAHT_WIDTH(dev) (1 << (dev)->xaht_width_shift)
  337. #define EMAC_XAHT_REGS(dev) (1 << ((dev)->xaht_slots_shift - \
  338. (dev)->xaht_width_shift))
  339. #define EMAC_XAHT_CRC_TO_SLOT(dev, crc) \
  340. ((EMAC_XAHT_SLOTS(dev) - 1) - \
  341. ((crc) >> ((sizeof (u32) * BITS_PER_BYTE) - \
  342. (dev)->xaht_slots_shift)))
  343. #define EMAC_XAHT_SLOT_TO_REG(dev, slot) \
  344. ((slot) >> (dev)->xaht_width_shift)
  345. #define EMAC_XAHT_SLOT_TO_MASK(dev, slot) \
  346. ((u32)(1 << (EMAC_XAHT_WIDTH(dev) - 1)) >> \
  347. ((slot) & (u32)(EMAC_XAHT_WIDTH(dev) - 1)))
  348. static inline u32 *emac_xaht_base(struct emac_instance *dev)
  349. {
  350. struct emac_regs __iomem *p = dev->emacp;
  351. int offset;
  352. /* The first IAHT entry always is the base of the block of
  353. * IAHT and GAHT registers.
  354. */
  355. if (emac_has_feature(dev, EMAC_FTR_EMAC4SYNC))
  356. offset = offsetof(struct emac_regs, u1.emac4sync.iaht1);
  357. else
  358. offset = offsetof(struct emac_regs, u0.emac4.iaht1);
  359. return ((u32 *)((ptrdiff_t)p + offset));
  360. }
  361. static inline u32 *emac_gaht_base(struct emac_instance *dev)
  362. {
  363. /* GAHT registers always come after an identical number of
  364. * IAHT registers.
  365. */
  366. return (emac_xaht_base(dev) + EMAC_XAHT_REGS(dev));
  367. }
  368. static inline u32 *emac_iaht_base(struct emac_instance *dev)
  369. {
  370. /* IAHT registers always come before an identical number of
  371. * GAHT registers.
  372. */
  373. return (emac_xaht_base(dev));
  374. }
  375. /* Ethtool get_regs complex data.
  376. * We want to get not just EMAC registers, but also MAL, ZMII, RGMII, TAH
  377. * when available.
  378. *
  379. * Returned BLOB consists of the ibm_emac_ethtool_regs_hdr,
  380. * MAL registers, EMAC registers and optional ZMII, RGMII, TAH registers.
  381. * Each register component is preceded with emac_ethtool_regs_subhdr.
  382. * Order of the optional headers follows their relative bit posititions
  383. * in emac_ethtool_regs_hdr.components
  384. */
  385. #define EMAC_ETHTOOL_REGS_ZMII 0x00000001
  386. #define EMAC_ETHTOOL_REGS_RGMII 0x00000002
  387. #define EMAC_ETHTOOL_REGS_TAH 0x00000004
  388. struct emac_ethtool_regs_hdr {
  389. u32 components;
  390. };
  391. struct emac_ethtool_regs_subhdr {
  392. u32 version;
  393. u32 index;
  394. };
  395. #define EMAC_ETHTOOL_REGS_VER 0
  396. #define EMAC_ETHTOOL_REGS_SIZE(dev) ((dev)->rsrc_regs.end - \
  397. (dev)->rsrc_regs.start + 1)
  398. #define EMAC4_ETHTOOL_REGS_VER 1
  399. #define EMAC4_ETHTOOL_REGS_SIZE(dev) ((dev)->rsrc_regs.end - \
  400. (dev)->rsrc_regs.start + 1)
  401. #endif /* __IBM_NEWEMAC_CORE_H */