fman_memac.c 32 KB

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