fman_memac.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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. /* SGMII mode */
  455. tmp_reg16 = IF_MODE_SGMII_EN;
  456. if (!fixed_link)
  457. /* AN enable */
  458. tmp_reg16 |= IF_MODE_USE_SGMII_AN;
  459. else {
  460. switch (fixed_link->speed) {
  461. case 10:
  462. /* For 10M: IF_MODE[SPEED_10M] = 0 */
  463. break;
  464. case 100:
  465. tmp_reg16 |= IF_MODE_SGMII_SPEED_100M;
  466. break;
  467. case 1000: /* fallthrough */
  468. default:
  469. tmp_reg16 |= IF_MODE_SGMII_SPEED_1G;
  470. break;
  471. }
  472. if (!fixed_link->duplex)
  473. tmp_reg16 |= IF_MODE_SGMII_DUPLEX_HALF;
  474. }
  475. phy_write(memac->pcsphy, MDIO_SGMII_IF_MODE, tmp_reg16);
  476. /* Device ability according to SGMII specification */
  477. tmp_reg16 = MDIO_SGMII_DEV_ABIL_SGMII_MODE;
  478. phy_write(memac->pcsphy, MDIO_SGMII_DEV_ABIL_SGMII, tmp_reg16);
  479. /* Adjust link timer for SGMII -
  480. * According to Cisco SGMII specification the timer should be 1.6 ms.
  481. * The link_timer register is configured in units of the clock.
  482. * - When running as 1G SGMII, Serdes clock is 125 MHz, so
  483. * unit = 1 / (125*10^6 Hz) = 8 ns.
  484. * 1.6 ms in units of 8 ns = 1.6ms / 8ns = 2*10^5 = 0x30d40
  485. * - When running as 2.5G SGMII, Serdes clock is 312.5 MHz, so
  486. * unit = 1 / (312.5*10^6 Hz) = 3.2 ns.
  487. * 1.6 ms in units of 3.2 ns = 1.6ms / 3.2ns = 5*10^5 = 0x7a120.
  488. * Since link_timer value of 1G SGMII will be too short for 2.5 SGMII,
  489. * we always set up here a value of 2.5 SGMII.
  490. */
  491. phy_write(memac->pcsphy, MDIO_SGMII_LINK_TMR_H, LINK_TMR_H);
  492. phy_write(memac->pcsphy, MDIO_SGMII_LINK_TMR_L, LINK_TMR_L);
  493. if (!fixed_link)
  494. /* Restart AN */
  495. tmp_reg16 = SGMII_CR_DEF_VAL | SGMII_CR_RESTART_AN;
  496. else
  497. /* AN disabled */
  498. tmp_reg16 = SGMII_CR_DEF_VAL & ~SGMII_CR_AN_EN;
  499. phy_write(memac->pcsphy, 0x0, tmp_reg16);
  500. }
  501. static void setup_sgmii_internal_phy_base_x(struct fman_mac *memac)
  502. {
  503. u16 tmp_reg16;
  504. /* AN Device capability */
  505. tmp_reg16 = MDIO_SGMII_DEV_ABIL_BASEX_MODE;
  506. phy_write(memac->pcsphy, MDIO_SGMII_DEV_ABIL_SGMII, tmp_reg16);
  507. /* Adjust link timer for SGMII -
  508. * For Serdes 1000BaseX auto-negotiation the timer should be 10 ms.
  509. * The link_timer register is configured in units of the clock.
  510. * - When running as 1G SGMII, Serdes clock is 125 MHz, so
  511. * unit = 1 / (125*10^6 Hz) = 8 ns.
  512. * 10 ms in units of 8 ns = 10ms / 8ns = 1250000 = 0x1312d0
  513. * - When running as 2.5G SGMII, Serdes clock is 312.5 MHz, so
  514. * unit = 1 / (312.5*10^6 Hz) = 3.2 ns.
  515. * 10 ms in units of 3.2 ns = 10ms / 3.2ns = 3125000 = 0x2faf08.
  516. * Since link_timer value of 1G SGMII will be too short for 2.5 SGMII,
  517. * we always set up here a value of 2.5 SGMII.
  518. */
  519. phy_write(memac->pcsphy, MDIO_SGMII_LINK_TMR_H, LINK_TMR_H_BASEX);
  520. phy_write(memac->pcsphy, MDIO_SGMII_LINK_TMR_L, LINK_TMR_L_BASEX);
  521. /* Restart AN */
  522. tmp_reg16 = SGMII_CR_DEF_VAL | SGMII_CR_RESTART_AN;
  523. phy_write(memac->pcsphy, 0x0, tmp_reg16);
  524. }
  525. static int check_init_parameters(struct fman_mac *memac)
  526. {
  527. if (memac->addr == 0) {
  528. pr_err("Ethernet MAC must have a valid MAC address\n");
  529. return -EINVAL;
  530. }
  531. if (!memac->exception_cb) {
  532. pr_err("Uninitialized exception handler\n");
  533. return -EINVAL;
  534. }
  535. if (!memac->event_cb) {
  536. pr_warn("Uninitialize event handler\n");
  537. return -EINVAL;
  538. }
  539. return 0;
  540. }
  541. static int get_exception_flag(enum fman_mac_exceptions exception)
  542. {
  543. u32 bit_mask;
  544. switch (exception) {
  545. case FM_MAC_EX_10G_TX_ECC_ER:
  546. bit_mask = MEMAC_IMASK_TECC_ER;
  547. break;
  548. case FM_MAC_EX_10G_RX_ECC_ER:
  549. bit_mask = MEMAC_IMASK_RECC_ER;
  550. break;
  551. case FM_MAC_EX_TS_FIFO_ECC_ERR:
  552. bit_mask = MEMAC_IMASK_TSECC_ER;
  553. break;
  554. case FM_MAC_EX_MAGIC_PACKET_INDICATION:
  555. bit_mask = MEMAC_IMASK_MGI;
  556. break;
  557. default:
  558. bit_mask = 0;
  559. break;
  560. }
  561. return bit_mask;
  562. }
  563. static void memac_err_exception(void *handle)
  564. {
  565. struct fman_mac *memac = (struct fman_mac *)handle;
  566. struct memac_regs __iomem *regs = memac->regs;
  567. u32 event, imask;
  568. event = ioread32be(&regs->ievent);
  569. imask = ioread32be(&regs->imask);
  570. /* Imask include both error and notification/event bits.
  571. * Leaving only error bits enabled by imask.
  572. * The imask error bits are shifted by 16 bits offset from
  573. * their corresponding location in the ievent - hence the >> 16
  574. */
  575. event &= ((imask & MEMAC_ALL_ERRS_IMASK) >> 16);
  576. iowrite32be(event, &regs->ievent);
  577. if (event & MEMAC_IEVNT_TS_ECC_ER)
  578. memac->exception_cb(memac->dev_id, FM_MAC_EX_TS_FIFO_ECC_ERR);
  579. if (event & MEMAC_IEVNT_TX_ECC_ER)
  580. memac->exception_cb(memac->dev_id, FM_MAC_EX_10G_TX_ECC_ER);
  581. if (event & MEMAC_IEVNT_RX_ECC_ER)
  582. memac->exception_cb(memac->dev_id, FM_MAC_EX_10G_RX_ECC_ER);
  583. }
  584. static void memac_exception(void *handle)
  585. {
  586. struct fman_mac *memac = (struct fman_mac *)handle;
  587. struct memac_regs __iomem *regs = memac->regs;
  588. u32 event, imask;
  589. event = ioread32be(&regs->ievent);
  590. imask = ioread32be(&regs->imask);
  591. /* Imask include both error and notification/event bits.
  592. * Leaving only error bits enabled by imask.
  593. * The imask error bits are shifted by 16 bits offset from
  594. * their corresponding location in the ievent - hence the >> 16
  595. */
  596. event &= ((imask & MEMAC_ALL_ERRS_IMASK) >> 16);
  597. iowrite32be(event, &regs->ievent);
  598. if (event & MEMAC_IEVNT_MGI)
  599. memac->exception_cb(memac->dev_id,
  600. FM_MAC_EX_MAGIC_PACKET_INDICATION);
  601. }
  602. static void free_init_resources(struct fman_mac *memac)
  603. {
  604. fman_unregister_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
  605. FMAN_INTR_TYPE_ERR);
  606. fman_unregister_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
  607. FMAN_INTR_TYPE_NORMAL);
  608. /* release the driver's group hash table */
  609. free_hash_table(memac->multicast_addr_hash);
  610. memac->multicast_addr_hash = NULL;
  611. /* release the driver's individual hash table */
  612. free_hash_table(memac->unicast_addr_hash);
  613. memac->unicast_addr_hash = NULL;
  614. }
  615. static bool is_init_done(struct memac_cfg *memac_drv_params)
  616. {
  617. /* Checks if mEMAC driver parameters were initialized */
  618. if (!memac_drv_params)
  619. return true;
  620. return false;
  621. }
  622. int memac_enable(struct fman_mac *memac, enum comm_mode mode)
  623. {
  624. struct memac_regs __iomem *regs = memac->regs;
  625. u32 tmp;
  626. if (!is_init_done(memac->memac_drv_param))
  627. return -EINVAL;
  628. tmp = ioread32be(&regs->command_config);
  629. if (mode & COMM_MODE_RX)
  630. tmp |= CMD_CFG_RX_EN;
  631. if (mode & COMM_MODE_TX)
  632. tmp |= CMD_CFG_TX_EN;
  633. iowrite32be(tmp, &regs->command_config);
  634. return 0;
  635. }
  636. int memac_disable(struct fman_mac *memac, enum comm_mode mode)
  637. {
  638. struct memac_regs __iomem *regs = memac->regs;
  639. u32 tmp;
  640. if (!is_init_done(memac->memac_drv_param))
  641. return -EINVAL;
  642. tmp = ioread32be(&regs->command_config);
  643. if (mode & COMM_MODE_RX)
  644. tmp &= ~CMD_CFG_RX_EN;
  645. if (mode & COMM_MODE_TX)
  646. tmp &= ~CMD_CFG_TX_EN;
  647. iowrite32be(tmp, &regs->command_config);
  648. return 0;
  649. }
  650. int memac_set_promiscuous(struct fman_mac *memac, bool new_val)
  651. {
  652. struct memac_regs __iomem *regs = memac->regs;
  653. u32 tmp;
  654. if (!is_init_done(memac->memac_drv_param))
  655. return -EINVAL;
  656. tmp = ioread32be(&regs->command_config);
  657. if (new_val)
  658. tmp |= CMD_CFG_PROMIS_EN;
  659. else
  660. tmp &= ~CMD_CFG_PROMIS_EN;
  661. iowrite32be(tmp, &regs->command_config);
  662. return 0;
  663. }
  664. int memac_adjust_link(struct fman_mac *memac, u16 speed)
  665. {
  666. struct memac_regs __iomem *regs = memac->regs;
  667. u32 tmp;
  668. if (!is_init_done(memac->memac_drv_param))
  669. return -EINVAL;
  670. tmp = ioread32be(&regs->if_mode);
  671. /* Set full duplex */
  672. tmp &= ~IF_MODE_HD;
  673. if (memac->phy_if == PHY_INTERFACE_MODE_RGMII) {
  674. /* Configure RGMII in manual mode */
  675. tmp &= ~IF_MODE_RGMII_AUTO;
  676. tmp &= ~IF_MODE_RGMII_SP_MASK;
  677. /* Full duplex */
  678. tmp |= IF_MODE_RGMII_FD;
  679. switch (speed) {
  680. case SPEED_1000:
  681. tmp |= IF_MODE_RGMII_1000;
  682. break;
  683. case SPEED_100:
  684. tmp |= IF_MODE_RGMII_100;
  685. break;
  686. case SPEED_10:
  687. tmp |= IF_MODE_RGMII_10;
  688. break;
  689. default:
  690. break;
  691. }
  692. }
  693. iowrite32be(tmp, &regs->if_mode);
  694. return 0;
  695. }
  696. int memac_cfg_max_frame_len(struct fman_mac *memac, u16 new_val)
  697. {
  698. if (is_init_done(memac->memac_drv_param))
  699. return -EINVAL;
  700. memac->memac_drv_param->max_frame_length = new_val;
  701. return 0;
  702. }
  703. int memac_cfg_reset_on_init(struct fman_mac *memac, bool enable)
  704. {
  705. if (is_init_done(memac->memac_drv_param))
  706. return -EINVAL;
  707. memac->memac_drv_param->reset_on_init = enable;
  708. return 0;
  709. }
  710. int memac_cfg_fixed_link(struct fman_mac *memac,
  711. struct fixed_phy_status *fixed_link)
  712. {
  713. if (is_init_done(memac->memac_drv_param))
  714. return -EINVAL;
  715. memac->memac_drv_param->fixed_link = fixed_link;
  716. return 0;
  717. }
  718. int memac_set_tx_pause_frames(struct fman_mac *memac, u8 priority,
  719. u16 pause_time, u16 thresh_time)
  720. {
  721. struct memac_regs __iomem *regs = memac->regs;
  722. u32 tmp;
  723. if (!is_init_done(memac->memac_drv_param))
  724. return -EINVAL;
  725. tmp = ioread32be(&regs->tx_fifo_sections);
  726. GET_TX_EMPTY_DEFAULT_VALUE(tmp);
  727. iowrite32be(tmp, &regs->tx_fifo_sections);
  728. tmp = ioread32be(&regs->command_config);
  729. tmp &= ~CMD_CFG_PFC_MODE;
  730. priority = 0;
  731. iowrite32be(tmp, &regs->command_config);
  732. tmp = ioread32be(&regs->pause_quanta[priority / 2]);
  733. if (priority % 2)
  734. tmp &= CLXY_PAUSE_QUANTA_CLX_PQNT;
  735. else
  736. tmp &= CLXY_PAUSE_QUANTA_CLY_PQNT;
  737. tmp |= ((u32)pause_time << (16 * (priority % 2)));
  738. iowrite32be(tmp, &regs->pause_quanta[priority / 2]);
  739. tmp = ioread32be(&regs->pause_thresh[priority / 2]);
  740. if (priority % 2)
  741. tmp &= CLXY_PAUSE_THRESH_CLX_QTH;
  742. else
  743. tmp &= CLXY_PAUSE_THRESH_CLY_QTH;
  744. tmp |= ((u32)thresh_time << (16 * (priority % 2)));
  745. iowrite32be(tmp, &regs->pause_thresh[priority / 2]);
  746. return 0;
  747. }
  748. int memac_accept_rx_pause_frames(struct fman_mac *memac, bool en)
  749. {
  750. struct memac_regs __iomem *regs = memac->regs;
  751. u32 tmp;
  752. if (!is_init_done(memac->memac_drv_param))
  753. return -EINVAL;
  754. tmp = ioread32be(&regs->command_config);
  755. if (en)
  756. tmp &= ~CMD_CFG_PAUSE_IGNORE;
  757. else
  758. tmp |= CMD_CFG_PAUSE_IGNORE;
  759. iowrite32be(tmp, &regs->command_config);
  760. return 0;
  761. }
  762. int memac_modify_mac_address(struct fman_mac *memac, enet_addr_t *enet_addr)
  763. {
  764. if (!is_init_done(memac->memac_drv_param))
  765. return -EINVAL;
  766. add_addr_in_paddr(memac->regs, (u8 *)(*enet_addr), 0);
  767. return 0;
  768. }
  769. int memac_add_hash_mac_address(struct fman_mac *memac, enet_addr_t *eth_addr)
  770. {
  771. struct memac_regs __iomem *regs = memac->regs;
  772. struct eth_hash_entry *hash_entry;
  773. u32 hash;
  774. u64 addr;
  775. if (!is_init_done(memac->memac_drv_param))
  776. return -EINVAL;
  777. addr = ENET_ADDR_TO_UINT64(*eth_addr);
  778. if (!(addr & GROUP_ADDRESS)) {
  779. /* Unicast addresses not supported in hash */
  780. pr_err("Unicast Address\n");
  781. return -EINVAL;
  782. }
  783. hash = get_mac_addr_hash_code(addr) & HASH_CTRL_ADDR_MASK;
  784. /* Create element to be added to the driver hash table */
  785. hash_entry = kmalloc(sizeof(*hash_entry), GFP_KERNEL);
  786. if (!hash_entry)
  787. return -ENOMEM;
  788. hash_entry->addr = addr;
  789. INIT_LIST_HEAD(&hash_entry->node);
  790. list_add_tail(&hash_entry->node,
  791. &memac->multicast_addr_hash->lsts[hash]);
  792. iowrite32be(hash | HASH_CTRL_MCAST_EN, &regs->hashtable_ctrl);
  793. return 0;
  794. }
  795. int memac_del_hash_mac_address(struct fman_mac *memac, enet_addr_t *eth_addr)
  796. {
  797. struct memac_regs __iomem *regs = memac->regs;
  798. struct eth_hash_entry *hash_entry = NULL;
  799. struct list_head *pos;
  800. u32 hash;
  801. u64 addr;
  802. if (!is_init_done(memac->memac_drv_param))
  803. return -EINVAL;
  804. addr = ENET_ADDR_TO_UINT64(*eth_addr);
  805. hash = get_mac_addr_hash_code(addr) & HASH_CTRL_ADDR_MASK;
  806. list_for_each(pos, &memac->multicast_addr_hash->lsts[hash]) {
  807. hash_entry = ETH_HASH_ENTRY_OBJ(pos);
  808. if (hash_entry->addr == addr) {
  809. list_del_init(&hash_entry->node);
  810. kfree(hash_entry);
  811. break;
  812. }
  813. }
  814. if (list_empty(&memac->multicast_addr_hash->lsts[hash]))
  815. iowrite32be(hash & ~HASH_CTRL_MCAST_EN, &regs->hashtable_ctrl);
  816. return 0;
  817. }
  818. int memac_set_exception(struct fman_mac *memac,
  819. enum fman_mac_exceptions exception, bool enable)
  820. {
  821. u32 bit_mask = 0;
  822. if (!is_init_done(memac->memac_drv_param))
  823. return -EINVAL;
  824. bit_mask = get_exception_flag(exception);
  825. if (bit_mask) {
  826. if (enable)
  827. memac->exceptions |= bit_mask;
  828. else
  829. memac->exceptions &= ~bit_mask;
  830. } else {
  831. pr_err("Undefined exception\n");
  832. return -EINVAL;
  833. }
  834. set_exception(memac->regs, bit_mask, enable);
  835. return 0;
  836. }
  837. int memac_init(struct fman_mac *memac)
  838. {
  839. struct memac_cfg *memac_drv_param;
  840. u8 i;
  841. enet_addr_t eth_addr;
  842. bool slow_10g_if = false;
  843. struct fixed_phy_status *fixed_link;
  844. int err;
  845. u32 reg32 = 0;
  846. if (is_init_done(memac->memac_drv_param))
  847. return -EINVAL;
  848. err = check_init_parameters(memac);
  849. if (err)
  850. return err;
  851. memac_drv_param = memac->memac_drv_param;
  852. if (memac->fm_rev_info.major == 6 && memac->fm_rev_info.minor == 4)
  853. slow_10g_if = true;
  854. /* First, reset the MAC if desired. */
  855. if (memac_drv_param->reset_on_init) {
  856. err = reset(memac->regs);
  857. if (err) {
  858. pr_err("mEMAC reset failed\n");
  859. return err;
  860. }
  861. }
  862. /* MAC Address */
  863. MAKE_ENET_ADDR_FROM_UINT64(memac->addr, eth_addr);
  864. add_addr_in_paddr(memac->regs, (u8 *)eth_addr, 0);
  865. fixed_link = memac_drv_param->fixed_link;
  866. init(memac->regs, memac->memac_drv_param, memac->phy_if,
  867. memac->max_speed, slow_10g_if, memac->exceptions);
  868. /* FM_RX_FIFO_CORRUPT_ERRATA_10GMAC_A006320 errata workaround
  869. * Exists only in FMan 6.0 and 6.3.
  870. */
  871. if ((memac->fm_rev_info.major == 6) &&
  872. ((memac->fm_rev_info.minor == 0) ||
  873. (memac->fm_rev_info.minor == 3))) {
  874. /* MAC strips CRC from received frames - this workaround
  875. * should decrease the likelihood of bug appearance
  876. */
  877. reg32 = ioread32be(&memac->regs->command_config);
  878. reg32 &= ~CMD_CFG_CRC_FWD;
  879. iowrite32be(reg32, &memac->regs->command_config);
  880. }
  881. if (memac->phy_if == PHY_INTERFACE_MODE_SGMII) {
  882. /* Configure internal SGMII PHY */
  883. if (memac->basex_if)
  884. setup_sgmii_internal_phy_base_x(memac);
  885. else
  886. setup_sgmii_internal_phy(memac, fixed_link);
  887. } else if (memac->phy_if == PHY_INTERFACE_MODE_QSGMII) {
  888. /* Configure 4 internal SGMII PHYs */
  889. for (i = 0; i < 4; i++) {
  890. u8 qsmgii_phy_addr, phy_addr;
  891. /* QSGMII PHY address occupies 3 upper bits of 5-bit
  892. * phy_address; the lower 2 bits are used to extend
  893. * register address space and access each one of 4
  894. * ports inside QSGMII.
  895. */
  896. phy_addr = memac->pcsphy->mdio.addr;
  897. qsmgii_phy_addr = (u8)((phy_addr << 2) | i);
  898. memac->pcsphy->mdio.addr = qsmgii_phy_addr;
  899. if (memac->basex_if)
  900. setup_sgmii_internal_phy_base_x(memac);
  901. else
  902. setup_sgmii_internal_phy(memac, fixed_link);
  903. memac->pcsphy->mdio.addr = phy_addr;
  904. }
  905. }
  906. /* Max Frame Length */
  907. err = fman_set_mac_max_frame(memac->fm, memac->mac_id,
  908. memac_drv_param->max_frame_length);
  909. if (err) {
  910. pr_err("settings Mac max frame length is FAILED\n");
  911. return err;
  912. }
  913. memac->multicast_addr_hash = alloc_hash_table(HASH_TABLE_SIZE);
  914. if (!memac->multicast_addr_hash) {
  915. free_init_resources(memac);
  916. pr_err("allocation hash table is FAILED\n");
  917. return -ENOMEM;
  918. }
  919. memac->unicast_addr_hash = alloc_hash_table(HASH_TABLE_SIZE);
  920. if (!memac->unicast_addr_hash) {
  921. free_init_resources(memac);
  922. pr_err("allocation hash table is FAILED\n");
  923. return -ENOMEM;
  924. }
  925. fman_register_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
  926. FMAN_INTR_TYPE_ERR, memac_err_exception, memac);
  927. fman_register_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
  928. FMAN_INTR_TYPE_NORMAL, memac_exception, memac);
  929. kfree(memac_drv_param);
  930. memac->memac_drv_param = NULL;
  931. return 0;
  932. }
  933. int memac_free(struct fman_mac *memac)
  934. {
  935. free_init_resources(memac);
  936. kfree(memac->memac_drv_param);
  937. kfree(memac);
  938. return 0;
  939. }
  940. struct fman_mac *memac_config(struct fman_mac_params *params)
  941. {
  942. struct fman_mac *memac;
  943. struct memac_cfg *memac_drv_param;
  944. void __iomem *base_addr;
  945. base_addr = params->base_addr;
  946. /* allocate memory for the m_emac data structure */
  947. memac = kzalloc(sizeof(*memac), GFP_KERNEL);
  948. if (!memac)
  949. return NULL;
  950. /* allocate memory for the m_emac driver parameters data structure */
  951. memac_drv_param = kzalloc(sizeof(*memac_drv_param), GFP_KERNEL);
  952. if (!memac_drv_param) {
  953. memac_free(memac);
  954. return NULL;
  955. }
  956. /* Plant parameter structure pointer */
  957. memac->memac_drv_param = memac_drv_param;
  958. set_dflts(memac_drv_param);
  959. memac->addr = ENET_ADDR_TO_UINT64(params->addr);
  960. memac->regs = base_addr;
  961. memac->max_speed = params->max_speed;
  962. memac->phy_if = params->phy_if;
  963. memac->mac_id = params->mac_id;
  964. memac->exceptions = (MEMAC_IMASK_TSECC_ER | MEMAC_IMASK_TECC_ER |
  965. MEMAC_IMASK_RECC_ER | MEMAC_IMASK_MGI);
  966. memac->exception_cb = params->exception_cb;
  967. memac->event_cb = params->event_cb;
  968. memac->dev_id = params->dev_id;
  969. memac->fm = params->fm;
  970. memac->basex_if = params->basex_if;
  971. /* Save FMan revision */
  972. fman_get_revision(memac->fm, &memac->fm_rev_info);
  973. if (memac->phy_if == PHY_INTERFACE_MODE_SGMII) {
  974. if (!params->internal_phy_node) {
  975. pr_err("PCS PHY node is not available\n");
  976. memac_free(memac);
  977. return NULL;
  978. }
  979. memac->pcsphy = of_phy_find_device(params->internal_phy_node);
  980. if (!memac->pcsphy) {
  981. pr_err("of_phy_find_device (PCS PHY) failed\n");
  982. memac_free(memac);
  983. return NULL;
  984. }
  985. }
  986. return memac;
  987. }