fman_memac.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /*
  2. * Copyright 2008-2015 Freescale Semiconductor Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of Freescale Semiconductor nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. *
  16. * ALTERNATIVELY, this software may be distributed under the terms of the
  17. * GNU General Public License ("GPL") as published by the Free Software
  18. * Foundation, either version 2 of that License or (at your option) any
  19. * later version.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
  22. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
  25. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include "fman_memac.h"
  34. #include "fman.h"
  35. #include <linux/slab.h>
  36. #include <linux/io.h>
  37. #include <linux/phy.h>
  38. #include <linux/of_mdio.h>
  39. /* PCS registers */
  40. #define MDIO_SGMII_CR 0x00
  41. #define MDIO_SGMII_DEV_ABIL_SGMII 0x04
  42. #define MDIO_SGMII_LINK_TMR_L 0x12
  43. #define MDIO_SGMII_LINK_TMR_H 0x13
  44. #define MDIO_SGMII_IF_MODE 0x14
  45. /* SGMII Control defines */
  46. #define SGMII_CR_AN_EN 0x1000
  47. #define SGMII_CR_RESTART_AN 0x0200
  48. #define SGMII_CR_FD 0x0100
  49. #define SGMII_CR_SPEED_SEL1_1G 0x0040
  50. #define SGMII_CR_DEF_VAL (SGMII_CR_AN_EN | SGMII_CR_FD | \
  51. SGMII_CR_SPEED_SEL1_1G)
  52. /* SGMII Device Ability for SGMII defines */
  53. #define MDIO_SGMII_DEV_ABIL_SGMII_MODE 0x4001
  54. #define MDIO_SGMII_DEV_ABIL_BASEX_MODE 0x01A0
  55. /* Link timer define */
  56. #define LINK_TMR_L 0xa120
  57. #define LINK_TMR_H 0x0007
  58. #define LINK_TMR_L_BASEX 0xaf08
  59. #define LINK_TMR_H_BASEX 0x002f
  60. /* SGMII IF Mode defines */
  61. #define IF_MODE_USE_SGMII_AN 0x0002
  62. #define IF_MODE_SGMII_EN 0x0001
  63. #define IF_MODE_SGMII_SPEED_100M 0x0004
  64. #define IF_MODE_SGMII_SPEED_1G 0x0008
  65. #define IF_MODE_SGMII_DUPLEX_HALF 0x0010
  66. /* Num of additional exact match MAC adr regs */
  67. #define MEMAC_NUM_OF_PADDRS 7
  68. /* Control and Configuration Register (COMMAND_CONFIG) */
  69. #define CMD_CFG_REG_LOWP_RXETY 0x01000000 /* 07 Rx low power indication */
  70. #define CMD_CFG_TX_LOWP_ENA 0x00800000 /* 08 Tx Low Power Idle Enable */
  71. #define CMD_CFG_PFC_MODE 0x00080000 /* 12 Enable PFC */
  72. #define CMD_CFG_NO_LEN_CHK 0x00020000 /* 14 Payload length check disable */
  73. #define CMD_CFG_SW_RESET 0x00001000 /* 19 S/W Reset, self clearing bit */
  74. #define CMD_CFG_TX_PAD_EN 0x00000800 /* 20 Enable Tx padding of frames */
  75. #define CMD_CFG_PAUSE_IGNORE 0x00000100 /* 23 Ignore Pause frame quanta */
  76. #define CMD_CFG_CRC_FWD 0x00000040 /* 25 Terminate/frwd CRC of frames */
  77. #define CMD_CFG_PAD_EN 0x00000020 /* 26 Frame padding removal */
  78. #define CMD_CFG_PROMIS_EN 0x00000010 /* 27 Promiscuous operation enable */
  79. #define CMD_CFG_RX_EN 0x00000002 /* 30 MAC receive path enable */
  80. #define CMD_CFG_TX_EN 0x00000001 /* 31 MAC transmit path enable */
  81. /* Transmit FIFO Sections Register (TX_FIFO_SECTIONS) */
  82. #define TX_FIFO_SECTIONS_TX_EMPTY_MASK 0xFFFF0000
  83. #define TX_FIFO_SECTIONS_TX_AVAIL_MASK 0x0000FFFF
  84. #define TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_10G 0x00400000
  85. #define TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_1G 0x00100000
  86. #define TX_FIFO_SECTIONS_TX_AVAIL_10G 0x00000019
  87. #define TX_FIFO_SECTIONS_TX_AVAIL_1G 0x00000020
  88. #define TX_FIFO_SECTIONS_TX_AVAIL_SLOW_10G 0x00000060
  89. #define GET_TX_EMPTY_DEFAULT_VALUE(_val) \
  90. do { \
  91. _val &= ~TX_FIFO_SECTIONS_TX_EMPTY_MASK; \
  92. ((_val == TX_FIFO_SECTIONS_TX_AVAIL_10G) ? \
  93. (_val |= TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_10G) :\
  94. (_val |= TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_1G));\
  95. } while (0)
  96. /* Interface Mode Register (IF_MODE) */
  97. #define IF_MODE_MASK 0x00000003 /* 30-31 Mask on i/f mode bits */
  98. #define IF_MODE_XGMII 0x00000000 /* 30-31 XGMII (10G) interface */
  99. #define IF_MODE_GMII 0x00000002 /* 30-31 GMII (1G) interface */
  100. #define IF_MODE_RGMII 0x00000004
  101. #define IF_MODE_RGMII_AUTO 0x00008000
  102. #define IF_MODE_RGMII_1000 0x00004000 /* 10 - 1000Mbps RGMII */
  103. #define IF_MODE_RGMII_100 0x00000000 /* 00 - 100Mbps RGMII */
  104. #define IF_MODE_RGMII_10 0x00002000 /* 01 - 10Mbps RGMII */
  105. #define IF_MODE_RGMII_SP_MASK 0x00006000 /* Setsp mask bits */
  106. #define IF_MODE_RGMII_FD 0x00001000 /* Full duplex RGMII */
  107. #define IF_MODE_HD 0x00000040 /* Half duplex operation */
  108. /* Hash table Control Register (HASHTABLE_CTRL) */
  109. #define HASH_CTRL_MCAST_EN 0x00000100
  110. /* 26-31 Hash table address code */
  111. #define HASH_CTRL_ADDR_MASK 0x0000003F
  112. /* MAC mcast indication */
  113. #define GROUP_ADDRESS 0x0000010000000000LL
  114. #define HASH_TABLE_SIZE 64 /* Hash tbl size */
  115. /* Interrupt Mask Register (IMASK) */
  116. #define MEMAC_IMASK_MGI 0x40000000 /* 1 Magic pkt detect indication */
  117. #define MEMAC_IMASK_TSECC_ER 0x20000000 /* 2 Timestamp FIFO ECC error evnt */
  118. #define MEMAC_IMASK_TECC_ER 0x02000000 /* 6 Transmit frame ECC error evnt */
  119. #define MEMAC_IMASK_RECC_ER 0x01000000 /* 7 Receive frame ECC error evnt */
  120. #define MEMAC_ALL_ERRS_IMASK \
  121. ((u32)(MEMAC_IMASK_TSECC_ER | \
  122. MEMAC_IMASK_TECC_ER | \
  123. MEMAC_IMASK_RECC_ER | \
  124. MEMAC_IMASK_MGI))
  125. #define MEMAC_IEVNT_PCS 0x80000000 /* PCS (XG). Link sync (G) */
  126. #define MEMAC_IEVNT_AN 0x40000000 /* Auto-negotiation */
  127. #define MEMAC_IEVNT_LT 0x20000000 /* Link Training/New page */
  128. #define MEMAC_IEVNT_MGI 0x00004000 /* Magic pkt detection */
  129. #define MEMAC_IEVNT_TS_ECC_ER 0x00002000 /* Timestamp FIFO ECC error*/
  130. #define MEMAC_IEVNT_RX_FIFO_OVFL 0x00001000 /* Rx FIFO overflow */
  131. #define MEMAC_IEVNT_TX_FIFO_UNFL 0x00000800 /* Tx FIFO underflow */
  132. #define MEMAC_IEVNT_TX_FIFO_OVFL 0x00000400 /* Tx FIFO overflow */
  133. #define MEMAC_IEVNT_TX_ECC_ER 0x00000200 /* Tx frame ECC error */
  134. #define MEMAC_IEVNT_RX_ECC_ER 0x00000100 /* Rx frame ECC error */
  135. #define MEMAC_IEVNT_LI_FAULT 0x00000080 /* Link Interruption flt */
  136. #define MEMAC_IEVNT_RX_EMPTY 0x00000040 /* Rx FIFO empty */
  137. #define MEMAC_IEVNT_TX_EMPTY 0x00000020 /* Tx FIFO empty */
  138. #define MEMAC_IEVNT_RX_LOWP 0x00000010 /* Low Power Idle */
  139. #define MEMAC_IEVNT_PHY_LOS 0x00000004 /* Phy loss of signal */
  140. #define MEMAC_IEVNT_REM_FAULT 0x00000002 /* Remote fault (XGMII) */
  141. #define MEMAC_IEVNT_LOC_FAULT 0x00000001 /* Local fault (XGMII) */
  142. #define DEFAULT_PAUSE_QUANTA 0xf000
  143. #define DEFAULT_FRAME_LENGTH 0x600
  144. #define DEFAULT_TX_IPG_LENGTH 12
  145. #define CLXY_PAUSE_QUANTA_CLX_PQNT 0x0000FFFF
  146. #define CLXY_PAUSE_QUANTA_CLY_PQNT 0xFFFF0000
  147. #define CLXY_PAUSE_THRESH_CLX_QTH 0x0000FFFF
  148. #define CLXY_PAUSE_THRESH_CLY_QTH 0xFFFF0000
  149. struct mac_addr {
  150. /* Lower 32 bits of 48-bit MAC address */
  151. u32 mac_addr_l;
  152. /* Upper 16 bits of 48-bit MAC address */
  153. u32 mac_addr_u;
  154. };
  155. /* memory map */
  156. struct memac_regs {
  157. u32 res0000[2]; /* General Control and Status */
  158. u32 command_config; /* 0x008 Ctrl and cfg */
  159. struct mac_addr mac_addr0; /* 0x00C-0x010 MAC_ADDR_0...1 */
  160. u32 maxfrm; /* 0x014 Max frame length */
  161. u32 res0018[1];
  162. u32 rx_fifo_sections; /* Receive FIFO configuration reg */
  163. u32 tx_fifo_sections; /* Transmit FIFO configuration reg */
  164. u32 res0024[2];
  165. u32 hashtable_ctrl; /* 0x02C Hash table control */
  166. u32 res0030[4];
  167. u32 ievent; /* 0x040 Interrupt event */
  168. u32 tx_ipg_length; /* 0x044 Transmitter inter-packet-gap */
  169. u32 res0048;
  170. u32 imask; /* 0x04C Interrupt mask */
  171. u32 res0050;
  172. u32 pause_quanta[4]; /* 0x054 Pause quanta */
  173. u32 pause_thresh[4]; /* 0x064 Pause quanta threshold */
  174. u32 rx_pause_status; /* 0x074 Receive pause status */
  175. u32 res0078[2];
  176. struct mac_addr mac_addr[MEMAC_NUM_OF_PADDRS];/* 0x80-0x0B4 mac padr */
  177. u32 lpwake_timer; /* 0x0B8 Low Power Wakeup Timer */
  178. u32 sleep_timer; /* 0x0BC Transmit EEE Low Power Timer */
  179. u32 res00c0[8];
  180. u32 statn_config; /* 0x0E0 Statistics configuration */
  181. u32 res00e4[7];
  182. /* Rx Statistics Counter */
  183. u32 reoct_l;
  184. u32 reoct_u;
  185. u32 roct_l;
  186. u32 roct_u;
  187. u32 raln_l;
  188. u32 raln_u;
  189. u32 rxpf_l;
  190. u32 rxpf_u;
  191. u32 rfrm_l;
  192. u32 rfrm_u;
  193. u32 rfcs_l;
  194. u32 rfcs_u;
  195. u32 rvlan_l;
  196. u32 rvlan_u;
  197. u32 rerr_l;
  198. u32 rerr_u;
  199. u32 ruca_l;
  200. u32 ruca_u;
  201. u32 rmca_l;
  202. u32 rmca_u;
  203. u32 rbca_l;
  204. u32 rbca_u;
  205. u32 rdrp_l;
  206. u32 rdrp_u;
  207. u32 rpkt_l;
  208. u32 rpkt_u;
  209. u32 rund_l;
  210. u32 rund_u;
  211. u32 r64_l;
  212. u32 r64_u;
  213. u32 r127_l;
  214. u32 r127_u;
  215. u32 r255_l;
  216. u32 r255_u;
  217. u32 r511_l;
  218. u32 r511_u;
  219. u32 r1023_l;
  220. u32 r1023_u;
  221. u32 r1518_l;
  222. u32 r1518_u;
  223. u32 r1519x_l;
  224. u32 r1519x_u;
  225. u32 rovr_l;
  226. u32 rovr_u;
  227. u32 rjbr_l;
  228. u32 rjbr_u;
  229. u32 rfrg_l;
  230. u32 rfrg_u;
  231. u32 rcnp_l;
  232. u32 rcnp_u;
  233. u32 rdrntp_l;
  234. u32 rdrntp_u;
  235. u32 res01d0[12];
  236. /* Tx Statistics Counter */
  237. u32 teoct_l;
  238. u32 teoct_u;
  239. u32 toct_l;
  240. u32 toct_u;
  241. u32 res0210[2];
  242. u32 txpf_l;
  243. u32 txpf_u;
  244. u32 tfrm_l;
  245. u32 tfrm_u;
  246. u32 tfcs_l;
  247. u32 tfcs_u;
  248. u32 tvlan_l;
  249. u32 tvlan_u;
  250. u32 terr_l;
  251. u32 terr_u;
  252. u32 tuca_l;
  253. u32 tuca_u;
  254. u32 tmca_l;
  255. u32 tmca_u;
  256. u32 tbca_l;
  257. u32 tbca_u;
  258. u32 res0258[2];
  259. u32 tpkt_l;
  260. u32 tpkt_u;
  261. u32 tund_l;
  262. u32 tund_u;
  263. u32 t64_l;
  264. u32 t64_u;
  265. u32 t127_l;
  266. u32 t127_u;
  267. u32 t255_l;
  268. u32 t255_u;
  269. u32 t511_l;
  270. u32 t511_u;
  271. u32 t1023_l;
  272. u32 t1023_u;
  273. u32 t1518_l;
  274. u32 t1518_u;
  275. u32 t1519x_l;
  276. u32 t1519x_u;
  277. u32 res02a8[6];
  278. u32 tcnp_l;
  279. u32 tcnp_u;
  280. u32 res02c8[14];
  281. /* Line Interface Control */
  282. u32 if_mode; /* 0x300 Interface Mode Control */
  283. u32 if_status; /* 0x304 Interface Status */
  284. u32 res0308[14];
  285. /* HiGig/2 */
  286. u32 hg_config; /* 0x340 Control and cfg */
  287. u32 res0344[3];
  288. u32 hg_pause_quanta; /* 0x350 Pause quanta */
  289. u32 res0354[3];
  290. u32 hg_pause_thresh; /* 0x360 Pause quanta threshold */
  291. u32 res0364[3];
  292. u32 hgrx_pause_status; /* 0x370 Receive pause status */
  293. u32 hg_fifos_status; /* 0x374 fifos status */
  294. u32 rhm; /* 0x378 rx messages counter */
  295. u32 thm; /* 0x37C tx messages counter */
  296. };
  297. struct memac_cfg {
  298. bool reset_on_init;
  299. bool pause_ignore;
  300. bool promiscuous_mode_enable;
  301. struct fixed_phy_status *fixed_link;
  302. u16 max_frame_length;
  303. u16 pause_quanta;
  304. u32 tx_ipg_length;
  305. };
  306. struct fman_mac {
  307. /* Pointer to MAC memory mapped registers */
  308. struct memac_regs __iomem *regs;
  309. /* MAC address of device */
  310. u64 addr;
  311. /* Ethernet physical interface */
  312. phy_interface_t phy_if;
  313. u16 max_speed;
  314. void *dev_id; /* device cookie used by the exception cbs */
  315. fman_mac_exception_cb *exception_cb;
  316. fman_mac_exception_cb *event_cb;
  317. /* Pointer to driver's global address hash table */
  318. struct eth_hash_t *multicast_addr_hash;
  319. /* Pointer to driver's individual address hash table */
  320. struct eth_hash_t *unicast_addr_hash;
  321. u8 mac_id;
  322. u32 exceptions;
  323. struct memac_cfg *memac_drv_param;
  324. void *fm;
  325. struct fman_rev_info fm_rev_info;
  326. bool basex_if;
  327. struct phy_device *pcsphy;
  328. };
  329. static void add_addr_in_paddr(struct memac_regs __iomem *regs, u8 *adr,
  330. u8 paddr_num)
  331. {
  332. u32 tmp0, tmp1;
  333. tmp0 = (u32)(adr[0] | adr[1] << 8 | adr[2] << 16 | adr[3] << 24);
  334. tmp1 = (u32)(adr[4] | adr[5] << 8);
  335. if (paddr_num == 0) {
  336. iowrite32be(tmp0, &regs->mac_addr0.mac_addr_l);
  337. iowrite32be(tmp1, &regs->mac_addr0.mac_addr_u);
  338. } else {
  339. iowrite32be(tmp0, &regs->mac_addr[paddr_num - 1].mac_addr_l);
  340. iowrite32be(tmp1, &regs->mac_addr[paddr_num - 1].mac_addr_u);
  341. }
  342. }
  343. static int reset(struct memac_regs __iomem *regs)
  344. {
  345. u32 tmp;
  346. int count;
  347. tmp = ioread32be(&regs->command_config);
  348. tmp |= CMD_CFG_SW_RESET;
  349. iowrite32be(tmp, &regs->command_config);
  350. count = 100;
  351. do {
  352. udelay(1);
  353. } while ((ioread32be(&regs->command_config) & CMD_CFG_SW_RESET) &&
  354. --count);
  355. if (count == 0)
  356. return -EBUSY;
  357. return 0;
  358. }
  359. static void set_exception(struct memac_regs __iomem *regs, u32 val,
  360. bool enable)
  361. {
  362. u32 tmp;
  363. tmp = ioread32be(&regs->imask);
  364. if (enable)
  365. tmp |= val;
  366. else
  367. tmp &= ~val;
  368. iowrite32be(tmp, &regs->imask);
  369. }
  370. static int init(struct memac_regs __iomem *regs, struct memac_cfg *cfg,
  371. phy_interface_t phy_if, u16 speed, bool slow_10g_if,
  372. u32 exceptions)
  373. {
  374. u32 tmp;
  375. /* Config */
  376. tmp = 0;
  377. if (cfg->promiscuous_mode_enable)
  378. tmp |= CMD_CFG_PROMIS_EN;
  379. if (cfg->pause_ignore)
  380. tmp |= CMD_CFG_PAUSE_IGNORE;
  381. /* Payload length check disable */
  382. tmp |= CMD_CFG_NO_LEN_CHK;
  383. /* Enable padding of frames in transmit direction */
  384. tmp |= CMD_CFG_TX_PAD_EN;
  385. tmp |= CMD_CFG_CRC_FWD;
  386. iowrite32be(tmp, &regs->command_config);
  387. /* Max Frame Length */
  388. iowrite32be((u32)cfg->max_frame_length, &regs->maxfrm);
  389. /* Pause Time */
  390. iowrite32be((u32)cfg->pause_quanta, &regs->pause_quanta[0]);
  391. iowrite32be((u32)0, &regs->pause_thresh[0]);
  392. /* IF_MODE */
  393. tmp = 0;
  394. switch (phy_if) {
  395. case PHY_INTERFACE_MODE_XGMII:
  396. tmp |= IF_MODE_XGMII;
  397. break;
  398. default:
  399. tmp |= IF_MODE_GMII;
  400. if (phy_if == PHY_INTERFACE_MODE_RGMII)
  401. tmp |= IF_MODE_RGMII | IF_MODE_RGMII_AUTO;
  402. }
  403. iowrite32be(tmp, &regs->if_mode);
  404. /* TX_FIFO_SECTIONS */
  405. tmp = 0;
  406. if (phy_if == PHY_INTERFACE_MODE_XGMII) {
  407. if (slow_10g_if) {
  408. tmp |= (TX_FIFO_SECTIONS_TX_AVAIL_SLOW_10G |
  409. TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_10G);
  410. } else {
  411. tmp |= (TX_FIFO_SECTIONS_TX_AVAIL_10G |
  412. TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_10G);
  413. }
  414. } else {
  415. tmp |= (TX_FIFO_SECTIONS_TX_AVAIL_1G |
  416. TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_1G);
  417. }
  418. iowrite32be(tmp, &regs->tx_fifo_sections);
  419. /* clear all pending events and set-up interrupts */
  420. iowrite32be(0xffffffff, &regs->ievent);
  421. set_exception(regs, exceptions, true);
  422. return 0;
  423. }
  424. static void set_dflts(struct memac_cfg *cfg)
  425. {
  426. cfg->reset_on_init = false;
  427. cfg->promiscuous_mode_enable = false;
  428. cfg->pause_ignore = false;
  429. cfg->tx_ipg_length = DEFAULT_TX_IPG_LENGTH;
  430. cfg->max_frame_length = DEFAULT_FRAME_LENGTH;
  431. cfg->pause_quanta = DEFAULT_PAUSE_QUANTA;
  432. }
  433. static u32 get_mac_addr_hash_code(u64 eth_addr)
  434. {
  435. u64 mask1, mask2;
  436. u32 xor_val = 0;
  437. u8 i, j;
  438. for (i = 0; i < 6; i++) {
  439. mask1 = eth_addr & (u64)0x01;
  440. eth_addr >>= 1;
  441. for (j = 0; j < 7; j++) {
  442. mask2 = eth_addr & (u64)0x01;
  443. mask1 ^= mask2;
  444. eth_addr >>= 1;
  445. }
  446. xor_val |= (mask1 << (5 - i));
  447. }
  448. return xor_val;
  449. }
  450. static void setup_sgmii_internal_phy(struct fman_mac *memac,
  451. struct fixed_phy_status *fixed_link)
  452. {
  453. u16 tmp_reg16;
  454. if (WARN_ON(!memac->pcsphy))
  455. return;
  456. /* SGMII mode */
  457. tmp_reg16 = IF_MODE_SGMII_EN;
  458. if (!fixed_link)
  459. /* AN enable */
  460. tmp_reg16 |= IF_MODE_USE_SGMII_AN;
  461. else {
  462. switch (fixed_link->speed) {
  463. case 10:
  464. /* For 10M: IF_MODE[SPEED_10M] = 0 */
  465. break;
  466. case 100:
  467. tmp_reg16 |= IF_MODE_SGMII_SPEED_100M;
  468. break;
  469. case 1000: /* fallthrough */
  470. default:
  471. tmp_reg16 |= IF_MODE_SGMII_SPEED_1G;
  472. break;
  473. }
  474. if (!fixed_link->duplex)
  475. tmp_reg16 |= IF_MODE_SGMII_DUPLEX_HALF;
  476. }
  477. phy_write(memac->pcsphy, MDIO_SGMII_IF_MODE, tmp_reg16);
  478. /* Device ability according to SGMII specification */
  479. tmp_reg16 = MDIO_SGMII_DEV_ABIL_SGMII_MODE;
  480. phy_write(memac->pcsphy, MDIO_SGMII_DEV_ABIL_SGMII, tmp_reg16);
  481. /* Adjust link timer for SGMII -
  482. * According to Cisco SGMII specification the timer should be 1.6 ms.
  483. * The link_timer register is configured in units of the clock.
  484. * - When running as 1G SGMII, Serdes clock is 125 MHz, so
  485. * unit = 1 / (125*10^6 Hz) = 8 ns.
  486. * 1.6 ms in units of 8 ns = 1.6ms / 8ns = 2*10^5 = 0x30d40
  487. * - When running as 2.5G SGMII, Serdes clock is 312.5 MHz, so
  488. * unit = 1 / (312.5*10^6 Hz) = 3.2 ns.
  489. * 1.6 ms in units of 3.2 ns = 1.6ms / 3.2ns = 5*10^5 = 0x7a120.
  490. * Since link_timer value of 1G SGMII will be too short for 2.5 SGMII,
  491. * we always set up here a value of 2.5 SGMII.
  492. */
  493. phy_write(memac->pcsphy, MDIO_SGMII_LINK_TMR_H, LINK_TMR_H);
  494. phy_write(memac->pcsphy, MDIO_SGMII_LINK_TMR_L, LINK_TMR_L);
  495. if (!fixed_link)
  496. /* Restart AN */
  497. tmp_reg16 = SGMII_CR_DEF_VAL | SGMII_CR_RESTART_AN;
  498. else
  499. /* AN disabled */
  500. tmp_reg16 = SGMII_CR_DEF_VAL & ~SGMII_CR_AN_EN;
  501. phy_write(memac->pcsphy, 0x0, tmp_reg16);
  502. }
  503. static void setup_sgmii_internal_phy_base_x(struct fman_mac *memac)
  504. {
  505. u16 tmp_reg16;
  506. /* AN Device capability */
  507. tmp_reg16 = MDIO_SGMII_DEV_ABIL_BASEX_MODE;
  508. phy_write(memac->pcsphy, MDIO_SGMII_DEV_ABIL_SGMII, tmp_reg16);
  509. /* Adjust link timer for SGMII -
  510. * For Serdes 1000BaseX auto-negotiation the timer should be 10 ms.
  511. * The link_timer register is configured in units of the clock.
  512. * - When running as 1G SGMII, Serdes clock is 125 MHz, so
  513. * unit = 1 / (125*10^6 Hz) = 8 ns.
  514. * 10 ms in units of 8 ns = 10ms / 8ns = 1250000 = 0x1312d0
  515. * - When running as 2.5G SGMII, Serdes clock is 312.5 MHz, so
  516. * unit = 1 / (312.5*10^6 Hz) = 3.2 ns.
  517. * 10 ms in units of 3.2 ns = 10ms / 3.2ns = 3125000 = 0x2faf08.
  518. * Since link_timer value of 1G SGMII will be too short for 2.5 SGMII,
  519. * we always set up here a value of 2.5 SGMII.
  520. */
  521. phy_write(memac->pcsphy, MDIO_SGMII_LINK_TMR_H, LINK_TMR_H_BASEX);
  522. phy_write(memac->pcsphy, MDIO_SGMII_LINK_TMR_L, LINK_TMR_L_BASEX);
  523. /* Restart AN */
  524. tmp_reg16 = SGMII_CR_DEF_VAL | SGMII_CR_RESTART_AN;
  525. phy_write(memac->pcsphy, 0x0, tmp_reg16);
  526. }
  527. static int check_init_parameters(struct fman_mac *memac)
  528. {
  529. if (memac->addr == 0) {
  530. pr_err("Ethernet MAC must have a valid MAC address\n");
  531. return -EINVAL;
  532. }
  533. if (!memac->exception_cb) {
  534. pr_err("Uninitialized exception handler\n");
  535. return -EINVAL;
  536. }
  537. if (!memac->event_cb) {
  538. pr_warn("Uninitialize event handler\n");
  539. return -EINVAL;
  540. }
  541. return 0;
  542. }
  543. static int get_exception_flag(enum fman_mac_exceptions exception)
  544. {
  545. u32 bit_mask;
  546. switch (exception) {
  547. case FM_MAC_EX_10G_TX_ECC_ER:
  548. bit_mask = MEMAC_IMASK_TECC_ER;
  549. break;
  550. case FM_MAC_EX_10G_RX_ECC_ER:
  551. bit_mask = MEMAC_IMASK_RECC_ER;
  552. break;
  553. case FM_MAC_EX_TS_FIFO_ECC_ERR:
  554. bit_mask = MEMAC_IMASK_TSECC_ER;
  555. break;
  556. case FM_MAC_EX_MAGIC_PACKET_INDICATION:
  557. bit_mask = MEMAC_IMASK_MGI;
  558. break;
  559. default:
  560. bit_mask = 0;
  561. break;
  562. }
  563. return bit_mask;
  564. }
  565. static void memac_err_exception(void *handle)
  566. {
  567. struct fman_mac *memac = (struct fman_mac *)handle;
  568. struct memac_regs __iomem *regs = memac->regs;
  569. u32 event, imask;
  570. event = ioread32be(&regs->ievent);
  571. imask = ioread32be(&regs->imask);
  572. /* Imask include both error and notification/event bits.
  573. * Leaving only error bits enabled by imask.
  574. * The imask error bits are shifted by 16 bits offset from
  575. * their corresponding location in the ievent - hence the >> 16
  576. */
  577. event &= ((imask & MEMAC_ALL_ERRS_IMASK) >> 16);
  578. iowrite32be(event, &regs->ievent);
  579. if (event & MEMAC_IEVNT_TS_ECC_ER)
  580. memac->exception_cb(memac->dev_id, FM_MAC_EX_TS_FIFO_ECC_ERR);
  581. if (event & MEMAC_IEVNT_TX_ECC_ER)
  582. memac->exception_cb(memac->dev_id, FM_MAC_EX_10G_TX_ECC_ER);
  583. if (event & MEMAC_IEVNT_RX_ECC_ER)
  584. memac->exception_cb(memac->dev_id, FM_MAC_EX_10G_RX_ECC_ER);
  585. }
  586. static void memac_exception(void *handle)
  587. {
  588. struct fman_mac *memac = (struct fman_mac *)handle;
  589. struct memac_regs __iomem *regs = memac->regs;
  590. u32 event, imask;
  591. event = ioread32be(&regs->ievent);
  592. imask = ioread32be(&regs->imask);
  593. /* Imask include both error and notification/event bits.
  594. * Leaving only error bits enabled by imask.
  595. * The imask error bits are shifted by 16 bits offset from
  596. * their corresponding location in the ievent - hence the >> 16
  597. */
  598. event &= ((imask & MEMAC_ALL_ERRS_IMASK) >> 16);
  599. iowrite32be(event, &regs->ievent);
  600. if (event & MEMAC_IEVNT_MGI)
  601. memac->exception_cb(memac->dev_id,
  602. FM_MAC_EX_MAGIC_PACKET_INDICATION);
  603. }
  604. static void free_init_resources(struct fman_mac *memac)
  605. {
  606. fman_unregister_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
  607. FMAN_INTR_TYPE_ERR);
  608. fman_unregister_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
  609. FMAN_INTR_TYPE_NORMAL);
  610. /* release the driver's group hash table */
  611. free_hash_table(memac->multicast_addr_hash);
  612. memac->multicast_addr_hash = NULL;
  613. /* release the driver's individual hash table */
  614. free_hash_table(memac->unicast_addr_hash);
  615. memac->unicast_addr_hash = NULL;
  616. }
  617. static bool is_init_done(struct memac_cfg *memac_drv_params)
  618. {
  619. /* Checks if mEMAC driver parameters were initialized */
  620. if (!memac_drv_params)
  621. return true;
  622. return false;
  623. }
  624. int memac_enable(struct fman_mac *memac, enum comm_mode mode)
  625. {
  626. struct memac_regs __iomem *regs = memac->regs;
  627. u32 tmp;
  628. if (!is_init_done(memac->memac_drv_param))
  629. return -EINVAL;
  630. tmp = ioread32be(&regs->command_config);
  631. if (mode & COMM_MODE_RX)
  632. tmp |= CMD_CFG_RX_EN;
  633. if (mode & COMM_MODE_TX)
  634. tmp |= CMD_CFG_TX_EN;
  635. iowrite32be(tmp, &regs->command_config);
  636. return 0;
  637. }
  638. int memac_disable(struct fman_mac *memac, enum comm_mode mode)
  639. {
  640. struct memac_regs __iomem *regs = memac->regs;
  641. u32 tmp;
  642. if (!is_init_done(memac->memac_drv_param))
  643. return -EINVAL;
  644. tmp = ioread32be(&regs->command_config);
  645. if (mode & COMM_MODE_RX)
  646. tmp &= ~CMD_CFG_RX_EN;
  647. if (mode & COMM_MODE_TX)
  648. tmp &= ~CMD_CFG_TX_EN;
  649. iowrite32be(tmp, &regs->command_config);
  650. return 0;
  651. }
  652. int memac_set_promiscuous(struct fman_mac *memac, bool new_val)
  653. {
  654. struct memac_regs __iomem *regs = memac->regs;
  655. u32 tmp;
  656. if (!is_init_done(memac->memac_drv_param))
  657. return -EINVAL;
  658. tmp = ioread32be(&regs->command_config);
  659. if (new_val)
  660. tmp |= CMD_CFG_PROMIS_EN;
  661. else
  662. tmp &= ~CMD_CFG_PROMIS_EN;
  663. iowrite32be(tmp, &regs->command_config);
  664. return 0;
  665. }
  666. int memac_adjust_link(struct fman_mac *memac, u16 speed)
  667. {
  668. struct memac_regs __iomem *regs = memac->regs;
  669. u32 tmp;
  670. if (!is_init_done(memac->memac_drv_param))
  671. return -EINVAL;
  672. tmp = ioread32be(&regs->if_mode);
  673. /* Set full duplex */
  674. tmp &= ~IF_MODE_HD;
  675. if (memac->phy_if == PHY_INTERFACE_MODE_RGMII) {
  676. /* Configure RGMII in manual mode */
  677. tmp &= ~IF_MODE_RGMII_AUTO;
  678. tmp &= ~IF_MODE_RGMII_SP_MASK;
  679. /* Full duplex */
  680. tmp |= IF_MODE_RGMII_FD;
  681. switch (speed) {
  682. case SPEED_1000:
  683. tmp |= IF_MODE_RGMII_1000;
  684. break;
  685. case SPEED_100:
  686. tmp |= IF_MODE_RGMII_100;
  687. break;
  688. case SPEED_10:
  689. tmp |= IF_MODE_RGMII_10;
  690. break;
  691. default:
  692. break;
  693. }
  694. }
  695. iowrite32be(tmp, &regs->if_mode);
  696. return 0;
  697. }
  698. int memac_cfg_max_frame_len(struct fman_mac *memac, u16 new_val)
  699. {
  700. if (is_init_done(memac->memac_drv_param))
  701. return -EINVAL;
  702. memac->memac_drv_param->max_frame_length = new_val;
  703. return 0;
  704. }
  705. int memac_cfg_reset_on_init(struct fman_mac *memac, bool enable)
  706. {
  707. if (is_init_done(memac->memac_drv_param))
  708. return -EINVAL;
  709. memac->memac_drv_param->reset_on_init = enable;
  710. return 0;
  711. }
  712. int memac_cfg_fixed_link(struct fman_mac *memac,
  713. struct fixed_phy_status *fixed_link)
  714. {
  715. if (is_init_done(memac->memac_drv_param))
  716. return -EINVAL;
  717. memac->memac_drv_param->fixed_link = fixed_link;
  718. return 0;
  719. }
  720. int memac_set_tx_pause_frames(struct fman_mac *memac, u8 priority,
  721. u16 pause_time, u16 thresh_time)
  722. {
  723. struct memac_regs __iomem *regs = memac->regs;
  724. u32 tmp;
  725. if (!is_init_done(memac->memac_drv_param))
  726. return -EINVAL;
  727. tmp = ioread32be(&regs->tx_fifo_sections);
  728. GET_TX_EMPTY_DEFAULT_VALUE(tmp);
  729. iowrite32be(tmp, &regs->tx_fifo_sections);
  730. tmp = ioread32be(&regs->command_config);
  731. tmp &= ~CMD_CFG_PFC_MODE;
  732. priority = 0;
  733. iowrite32be(tmp, &regs->command_config);
  734. tmp = ioread32be(&regs->pause_quanta[priority / 2]);
  735. if (priority % 2)
  736. tmp &= CLXY_PAUSE_QUANTA_CLX_PQNT;
  737. else
  738. tmp &= CLXY_PAUSE_QUANTA_CLY_PQNT;
  739. tmp |= ((u32)pause_time << (16 * (priority % 2)));
  740. iowrite32be(tmp, &regs->pause_quanta[priority / 2]);
  741. tmp = ioread32be(&regs->pause_thresh[priority / 2]);
  742. if (priority % 2)
  743. tmp &= CLXY_PAUSE_THRESH_CLX_QTH;
  744. else
  745. tmp &= CLXY_PAUSE_THRESH_CLY_QTH;
  746. tmp |= ((u32)thresh_time << (16 * (priority % 2)));
  747. iowrite32be(tmp, &regs->pause_thresh[priority / 2]);
  748. return 0;
  749. }
  750. int memac_accept_rx_pause_frames(struct fman_mac *memac, bool en)
  751. {
  752. struct memac_regs __iomem *regs = memac->regs;
  753. u32 tmp;
  754. if (!is_init_done(memac->memac_drv_param))
  755. return -EINVAL;
  756. tmp = ioread32be(&regs->command_config);
  757. if (en)
  758. tmp &= ~CMD_CFG_PAUSE_IGNORE;
  759. else
  760. tmp |= CMD_CFG_PAUSE_IGNORE;
  761. iowrite32be(tmp, &regs->command_config);
  762. return 0;
  763. }
  764. int memac_modify_mac_address(struct fman_mac *memac, enet_addr_t *enet_addr)
  765. {
  766. if (!is_init_done(memac->memac_drv_param))
  767. return -EINVAL;
  768. add_addr_in_paddr(memac->regs, (u8 *)(*enet_addr), 0);
  769. return 0;
  770. }
  771. int memac_add_hash_mac_address(struct fman_mac *memac, enet_addr_t *eth_addr)
  772. {
  773. struct memac_regs __iomem *regs = memac->regs;
  774. struct eth_hash_entry *hash_entry;
  775. u32 hash;
  776. u64 addr;
  777. if (!is_init_done(memac->memac_drv_param))
  778. return -EINVAL;
  779. addr = ENET_ADDR_TO_UINT64(*eth_addr);
  780. if (!(addr & GROUP_ADDRESS)) {
  781. /* Unicast addresses not supported in hash */
  782. pr_err("Unicast Address\n");
  783. return -EINVAL;
  784. }
  785. hash = get_mac_addr_hash_code(addr) & HASH_CTRL_ADDR_MASK;
  786. /* Create element to be added to the driver hash table */
  787. hash_entry = kmalloc(sizeof(*hash_entry), GFP_KERNEL);
  788. if (!hash_entry)
  789. return -ENOMEM;
  790. hash_entry->addr = addr;
  791. INIT_LIST_HEAD(&hash_entry->node);
  792. list_add_tail(&hash_entry->node,
  793. &memac->multicast_addr_hash->lsts[hash]);
  794. iowrite32be(hash | HASH_CTRL_MCAST_EN, &regs->hashtable_ctrl);
  795. return 0;
  796. }
  797. int memac_del_hash_mac_address(struct fman_mac *memac, enet_addr_t *eth_addr)
  798. {
  799. struct memac_regs __iomem *regs = memac->regs;
  800. struct eth_hash_entry *hash_entry = NULL;
  801. struct list_head *pos;
  802. u32 hash;
  803. u64 addr;
  804. if (!is_init_done(memac->memac_drv_param))
  805. return -EINVAL;
  806. addr = ENET_ADDR_TO_UINT64(*eth_addr);
  807. hash = get_mac_addr_hash_code(addr) & HASH_CTRL_ADDR_MASK;
  808. list_for_each(pos, &memac->multicast_addr_hash->lsts[hash]) {
  809. hash_entry = ETH_HASH_ENTRY_OBJ(pos);
  810. if (hash_entry->addr == addr) {
  811. list_del_init(&hash_entry->node);
  812. kfree(hash_entry);
  813. break;
  814. }
  815. }
  816. if (list_empty(&memac->multicast_addr_hash->lsts[hash]))
  817. iowrite32be(hash & ~HASH_CTRL_MCAST_EN, &regs->hashtable_ctrl);
  818. return 0;
  819. }
  820. int memac_set_exception(struct fman_mac *memac,
  821. enum fman_mac_exceptions exception, bool enable)
  822. {
  823. u32 bit_mask = 0;
  824. if (!is_init_done(memac->memac_drv_param))
  825. return -EINVAL;
  826. bit_mask = get_exception_flag(exception);
  827. if (bit_mask) {
  828. if (enable)
  829. memac->exceptions |= bit_mask;
  830. else
  831. memac->exceptions &= ~bit_mask;
  832. } else {
  833. pr_err("Undefined exception\n");
  834. return -EINVAL;
  835. }
  836. set_exception(memac->regs, bit_mask, enable);
  837. return 0;
  838. }
  839. int memac_init(struct fman_mac *memac)
  840. {
  841. struct memac_cfg *memac_drv_param;
  842. u8 i;
  843. enet_addr_t eth_addr;
  844. bool slow_10g_if = false;
  845. struct fixed_phy_status *fixed_link;
  846. int err;
  847. u32 reg32 = 0;
  848. if (is_init_done(memac->memac_drv_param))
  849. return -EINVAL;
  850. err = check_init_parameters(memac);
  851. if (err)
  852. return err;
  853. memac_drv_param = memac->memac_drv_param;
  854. if (memac->fm_rev_info.major == 6 && memac->fm_rev_info.minor == 4)
  855. slow_10g_if = true;
  856. /* First, reset the MAC if desired. */
  857. if (memac_drv_param->reset_on_init) {
  858. err = reset(memac->regs);
  859. if (err) {
  860. pr_err("mEMAC reset failed\n");
  861. return err;
  862. }
  863. }
  864. /* MAC Address */
  865. MAKE_ENET_ADDR_FROM_UINT64(memac->addr, eth_addr);
  866. add_addr_in_paddr(memac->regs, (u8 *)eth_addr, 0);
  867. fixed_link = memac_drv_param->fixed_link;
  868. init(memac->regs, memac->memac_drv_param, memac->phy_if,
  869. memac->max_speed, slow_10g_if, memac->exceptions);
  870. /* FM_RX_FIFO_CORRUPT_ERRATA_10GMAC_A006320 errata workaround
  871. * Exists only in FMan 6.0 and 6.3.
  872. */
  873. if ((memac->fm_rev_info.major == 6) &&
  874. ((memac->fm_rev_info.minor == 0) ||
  875. (memac->fm_rev_info.minor == 3))) {
  876. /* MAC strips CRC from received frames - this workaround
  877. * should decrease the likelihood of bug appearance
  878. */
  879. reg32 = ioread32be(&memac->regs->command_config);
  880. reg32 &= ~CMD_CFG_CRC_FWD;
  881. iowrite32be(reg32, &memac->regs->command_config);
  882. }
  883. if (memac->phy_if == PHY_INTERFACE_MODE_SGMII) {
  884. /* Configure internal SGMII PHY */
  885. if (memac->basex_if)
  886. setup_sgmii_internal_phy_base_x(memac);
  887. else
  888. setup_sgmii_internal_phy(memac, fixed_link);
  889. } else if (memac->phy_if == PHY_INTERFACE_MODE_QSGMII) {
  890. /* Configure 4 internal SGMII PHYs */
  891. for (i = 0; i < 4; i++) {
  892. u8 qsmgii_phy_addr, phy_addr;
  893. /* QSGMII PHY address occupies 3 upper bits of 5-bit
  894. * phy_address; the lower 2 bits are used to extend
  895. * register address space and access each one of 4
  896. * ports inside QSGMII.
  897. */
  898. phy_addr = memac->pcsphy->mdio.addr;
  899. qsmgii_phy_addr = (u8)((phy_addr << 2) | i);
  900. memac->pcsphy->mdio.addr = qsmgii_phy_addr;
  901. if (memac->basex_if)
  902. setup_sgmii_internal_phy_base_x(memac);
  903. else
  904. setup_sgmii_internal_phy(memac, fixed_link);
  905. memac->pcsphy->mdio.addr = phy_addr;
  906. }
  907. }
  908. /* Max Frame Length */
  909. err = fman_set_mac_max_frame(memac->fm, memac->mac_id,
  910. memac_drv_param->max_frame_length);
  911. if (err) {
  912. pr_err("settings Mac max frame length is FAILED\n");
  913. return err;
  914. }
  915. memac->multicast_addr_hash = alloc_hash_table(HASH_TABLE_SIZE);
  916. if (!memac->multicast_addr_hash) {
  917. free_init_resources(memac);
  918. pr_err("allocation hash table is FAILED\n");
  919. return -ENOMEM;
  920. }
  921. memac->unicast_addr_hash = alloc_hash_table(HASH_TABLE_SIZE);
  922. if (!memac->unicast_addr_hash) {
  923. free_init_resources(memac);
  924. pr_err("allocation hash table is FAILED\n");
  925. return -ENOMEM;
  926. }
  927. fman_register_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
  928. FMAN_INTR_TYPE_ERR, memac_err_exception, memac);
  929. fman_register_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
  930. FMAN_INTR_TYPE_NORMAL, memac_exception, memac);
  931. kfree(memac_drv_param);
  932. memac->memac_drv_param = NULL;
  933. return 0;
  934. }
  935. int memac_free(struct fman_mac *memac)
  936. {
  937. free_init_resources(memac);
  938. if (memac->pcsphy)
  939. put_device(&memac->pcsphy->mdio.dev);
  940. kfree(memac->memac_drv_param);
  941. kfree(memac);
  942. return 0;
  943. }
  944. struct fman_mac *memac_config(struct fman_mac_params *params)
  945. {
  946. struct fman_mac *memac;
  947. struct memac_cfg *memac_drv_param;
  948. void __iomem *base_addr;
  949. base_addr = params->base_addr;
  950. /* allocate memory for the m_emac data structure */
  951. memac = kzalloc(sizeof(*memac), GFP_KERNEL);
  952. if (!memac)
  953. return NULL;
  954. /* allocate memory for the m_emac driver parameters data structure */
  955. memac_drv_param = kzalloc(sizeof(*memac_drv_param), GFP_KERNEL);
  956. if (!memac_drv_param) {
  957. memac_free(memac);
  958. return NULL;
  959. }
  960. /* Plant parameter structure pointer */
  961. memac->memac_drv_param = memac_drv_param;
  962. set_dflts(memac_drv_param);
  963. memac->addr = ENET_ADDR_TO_UINT64(params->addr);
  964. memac->regs = base_addr;
  965. memac->max_speed = params->max_speed;
  966. memac->phy_if = params->phy_if;
  967. memac->mac_id = params->mac_id;
  968. memac->exceptions = (MEMAC_IMASK_TSECC_ER | MEMAC_IMASK_TECC_ER |
  969. MEMAC_IMASK_RECC_ER | MEMAC_IMASK_MGI);
  970. memac->exception_cb = params->exception_cb;
  971. memac->event_cb = params->event_cb;
  972. memac->dev_id = params->dev_id;
  973. memac->fm = params->fm;
  974. memac->basex_if = params->basex_if;
  975. /* Save FMan revision */
  976. fman_get_revision(memac->fm, &memac->fm_rev_info);
  977. if (memac->phy_if == PHY_INTERFACE_MODE_SGMII ||
  978. memac->phy_if == PHY_INTERFACE_MODE_QSGMII) {
  979. if (!params->internal_phy_node) {
  980. pr_err("PCS PHY node is not available\n");
  981. memac_free(memac);
  982. return NULL;
  983. }
  984. memac->pcsphy = of_phy_find_device(params->internal_phy_node);
  985. if (!memac->pcsphy) {
  986. pr_err("of_phy_find_device (PCS PHY) failed\n");
  987. memac_free(memac);
  988. return NULL;
  989. }
  990. }
  991. return memac;
  992. }