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