xref: /OK3568_Linux_fs/u-boot/drivers/net/fm/memac_phy.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2012 Freescale Semiconductor, Inc.
3*4882a593Smuzhiyun  *	Andy Fleming <afleming@gmail.com>
4*4882a593Smuzhiyun  *	Roy Zang <tie-fei.zang@freescale.com>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
7*4882a593Smuzhiyun  * Some part is taken from tsec.c
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun #include <common.h>
10*4882a593Smuzhiyun #include <miiphy.h>
11*4882a593Smuzhiyun #include <phy.h>
12*4882a593Smuzhiyun #include <asm/io.h>
13*4882a593Smuzhiyun #include <fsl_memac.h>
14*4882a593Smuzhiyun #include <fm_eth.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #ifdef CONFIG_SYS_MEMAC_LITTLE_ENDIAN
17*4882a593Smuzhiyun #define memac_out_32(a, v)	out_le32(a, v)
18*4882a593Smuzhiyun #define memac_clrbits_32(a, v)	clrbits_le32(a, v)
19*4882a593Smuzhiyun #define memac_setbits_32(a, v)	setbits_le32(a, v)
20*4882a593Smuzhiyun #else
21*4882a593Smuzhiyun #define memac_out_32(a, v)	out_be32(a, v)
22*4882a593Smuzhiyun #define memac_clrbits_32(a, v)	clrbits_be32(a, v)
23*4882a593Smuzhiyun #define memac_setbits_32(a, v)	setbits_be32(a, v)
24*4882a593Smuzhiyun #endif
25*4882a593Smuzhiyun 
memac_in_32(u32 * reg)26*4882a593Smuzhiyun static u32 memac_in_32(u32 *reg)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun #ifdef CONFIG_SYS_MEMAC_LITTLE_ENDIAN
29*4882a593Smuzhiyun 	return in_le32(reg);
30*4882a593Smuzhiyun #else
31*4882a593Smuzhiyun 	return in_be32(reg);
32*4882a593Smuzhiyun #endif
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun  * Write value to the PHY for this device to the register at regnum, waiting
37*4882a593Smuzhiyun  * until the write is done before it returns.  All PHY configuration has to be
38*4882a593Smuzhiyun  * done through the TSEC1 MIIM regs
39*4882a593Smuzhiyun  */
memac_mdio_write(struct mii_dev * bus,int port_addr,int dev_addr,int regnum,u16 value)40*4882a593Smuzhiyun int memac_mdio_write(struct mii_dev *bus, int port_addr, int dev_addr,
41*4882a593Smuzhiyun 			int regnum, u16 value)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	u32 mdio_ctl;
44*4882a593Smuzhiyun 	struct memac_mdio_controller *regs = bus->priv;
45*4882a593Smuzhiyun 	u32 c45 = 1; /* Default to 10G interface */
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	if (dev_addr == MDIO_DEVAD_NONE) {
48*4882a593Smuzhiyun 		c45 = 0; /* clause 22 */
49*4882a593Smuzhiyun 		dev_addr = regnum & 0x1f;
50*4882a593Smuzhiyun 		memac_clrbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
51*4882a593Smuzhiyun 	} else
52*4882a593Smuzhiyun 		memac_setbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	/* Wait till the bus is free */
55*4882a593Smuzhiyun 	while ((memac_in_32(&regs->mdio_stat)) & MDIO_STAT_BSY)
56*4882a593Smuzhiyun 		;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	/* Set the port and dev addr */
59*4882a593Smuzhiyun 	mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
60*4882a593Smuzhiyun 	memac_out_32(&regs->mdio_ctl, mdio_ctl);
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	/* Set the register address */
63*4882a593Smuzhiyun 	if (c45)
64*4882a593Smuzhiyun 		memac_out_32(&regs->mdio_addr, regnum & 0xffff);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	/* Wait till the bus is free */
67*4882a593Smuzhiyun 	while ((memac_in_32(&regs->mdio_stat)) & MDIO_STAT_BSY)
68*4882a593Smuzhiyun 		;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	/* Write the value to the register */
71*4882a593Smuzhiyun 	memac_out_32(&regs->mdio_data, MDIO_DATA(value));
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	/* Wait till the MDIO write is complete */
74*4882a593Smuzhiyun 	while ((memac_in_32(&regs->mdio_data)) & MDIO_DATA_BSY)
75*4882a593Smuzhiyun 		;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	return 0;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun  * Reads from register regnum in the PHY for device dev, returning the value.
82*4882a593Smuzhiyun  * Clears miimcom first.  All PHY configuration has to be done through the
83*4882a593Smuzhiyun  * TSEC1 MIIM regs
84*4882a593Smuzhiyun  */
memac_mdio_read(struct mii_dev * bus,int port_addr,int dev_addr,int regnum)85*4882a593Smuzhiyun int memac_mdio_read(struct mii_dev *bus, int port_addr, int dev_addr,
86*4882a593Smuzhiyun 			int regnum)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	u32 mdio_ctl;
89*4882a593Smuzhiyun 	struct memac_mdio_controller *regs = bus->priv;
90*4882a593Smuzhiyun 	u32 c45 = 1;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	if (dev_addr == MDIO_DEVAD_NONE) {
93*4882a593Smuzhiyun 		if (!strcmp(bus->name, DEFAULT_FM_TGEC_MDIO_NAME))
94*4882a593Smuzhiyun 			return 0xffff;
95*4882a593Smuzhiyun 		c45 = 0; /* clause 22 */
96*4882a593Smuzhiyun 		dev_addr = regnum & 0x1f;
97*4882a593Smuzhiyun 		memac_clrbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
98*4882a593Smuzhiyun 	} else
99*4882a593Smuzhiyun 		memac_setbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	/* Wait till the bus is free */
102*4882a593Smuzhiyun 	while ((memac_in_32(&regs->mdio_stat)) & MDIO_STAT_BSY)
103*4882a593Smuzhiyun 		;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/* Set the Port and Device Addrs */
106*4882a593Smuzhiyun 	mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
107*4882a593Smuzhiyun 	memac_out_32(&regs->mdio_ctl, mdio_ctl);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	/* Set the register address */
110*4882a593Smuzhiyun 	if (c45)
111*4882a593Smuzhiyun 		memac_out_32(&regs->mdio_addr, regnum & 0xffff);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/* Wait till the bus is free */
114*4882a593Smuzhiyun 	while ((memac_in_32(&regs->mdio_stat)) & MDIO_STAT_BSY)
115*4882a593Smuzhiyun 		;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	/* Initiate the read */
118*4882a593Smuzhiyun 	mdio_ctl |= MDIO_CTL_READ;
119*4882a593Smuzhiyun 	memac_out_32(&regs->mdio_ctl, mdio_ctl);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/* Wait till the MDIO write is complete */
122*4882a593Smuzhiyun 	while ((memac_in_32(&regs->mdio_data)) & MDIO_DATA_BSY)
123*4882a593Smuzhiyun 		;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	/* Return all Fs if nothing was there */
126*4882a593Smuzhiyun 	if (memac_in_32(&regs->mdio_stat) & MDIO_STAT_RD_ER)
127*4882a593Smuzhiyun 		return 0xffff;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	return memac_in_32(&regs->mdio_data) & 0xffff;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
memac_mdio_reset(struct mii_dev * bus)132*4882a593Smuzhiyun int memac_mdio_reset(struct mii_dev *bus)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	return 0;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
fm_memac_mdio_init(bd_t * bis,struct memac_mdio_info * info)137*4882a593Smuzhiyun int fm_memac_mdio_init(bd_t *bis, struct memac_mdio_info *info)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	struct mii_dev *bus = mdio_alloc();
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	if (!bus) {
142*4882a593Smuzhiyun 		printf("Failed to allocate FM TGEC MDIO bus\n");
143*4882a593Smuzhiyun 		return -1;
144*4882a593Smuzhiyun 	}
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	bus->read = memac_mdio_read;
147*4882a593Smuzhiyun 	bus->write = memac_mdio_write;
148*4882a593Smuzhiyun 	bus->reset = memac_mdio_reset;
149*4882a593Smuzhiyun 	strcpy(bus->name, info->name);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	bus->priv = info->regs;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	/*
154*4882a593Smuzhiyun 	 * On some platforms like B4860, default value of MDIO_CLK_DIV bits
155*4882a593Smuzhiyun 	 * in mdio_stat(mdio_cfg) register generates MDIO clock too high
156*4882a593Smuzhiyun 	 * (much higher than 2.5MHz), violating the IEEE specs.
157*4882a593Smuzhiyun 	 * On other platforms like T1040, default value of MDIO_CLK_DIV bits
158*4882a593Smuzhiyun 	 * is zero, so MDIO clock is disabled.
159*4882a593Smuzhiyun 	 * So, for proper functioning of MDIO, MDIO_CLK_DIV bits needs to
160*4882a593Smuzhiyun 	 * be properly initialized.
161*4882a593Smuzhiyun 	 * NEG bit default should be '1' as per FMAN-v3 RM, but on platform
162*4882a593Smuzhiyun 	 * like T2080QDS, this bit default is '0', which leads to MDIO failure
163*4882a593Smuzhiyun 	 * on XAUI PHY, so set this bit definitely.
164*4882a593Smuzhiyun 	 */
165*4882a593Smuzhiyun 	memac_setbits_32(
166*4882a593Smuzhiyun 		&((struct memac_mdio_controller *)info->regs)->mdio_stat,
167*4882a593Smuzhiyun 		MDIO_STAT_CLKDIV(258) | MDIO_STAT_NEG);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	return mdio_register(bus);
170*4882a593Smuzhiyun }
171