blob: c2ef1b4e737ecd8dc9112ce7596b922e51a55ea7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Roy Zang111fd192012-10-08 07:44:21 +00002/*
3 * Copyright 2012 Freescale Semiconductor, Inc.
Andy Flemingb21f87a32014-07-25 17:39:08 -05004 * Andy Fleming <afleming@gmail.com>
Roy Zang111fd192012-10-08 07:44:21 +00005 * Roy Zang <tie-fei.zang@freescale.com>
Roy Zang111fd192012-10-08 07:44:21 +00006 * Some part is taken from tsec.c
7 */
8#include <common.h>
9#include <miiphy.h>
10#include <phy.h>
11#include <asm/io.h>
Shaohui Xiecd348ef2015-03-20 19:28:19 -070012#include <fsl_memac.h>
Roy Zang111fd192012-10-08 07:44:21 +000013#include <fm_eth.h>
14
Shaohui Xiecd348ef2015-03-20 19:28:19 -070015#ifdef CONFIG_SYS_MEMAC_LITTLE_ENDIAN
16#define memac_out_32(a, v) out_le32(a, v)
17#define memac_clrbits_32(a, v) clrbits_le32(a, v)
18#define memac_setbits_32(a, v) setbits_le32(a, v)
19#else
20#define memac_out_32(a, v) out_be32(a, v)
21#define memac_clrbits_32(a, v) clrbits_be32(a, v)
22#define memac_setbits_32(a, v) setbits_be32(a, v)
23#endif
24
25static u32 memac_in_32(u32 *reg)
26{
27#ifdef CONFIG_SYS_MEMAC_LITTLE_ENDIAN
28 return in_le32(reg);
29#else
30 return in_be32(reg);
31#endif
32}
33
Roy Zang111fd192012-10-08 07:44:21 +000034/*
35 * Write value to the PHY for this device to the register at regnum, waiting
36 * until the write is done before it returns. All PHY configuration has to be
37 * done through the TSEC1 MIIM regs
38 */
39int memac_mdio_write(struct mii_dev *bus, int port_addr, int dev_addr,
40 int regnum, u16 value)
41{
42 u32 mdio_ctl;
43 struct memac_mdio_controller *regs = bus->priv;
44 u32 c45 = 1; /* Default to 10G interface */
45
46 if (dev_addr == MDIO_DEVAD_NONE) {
47 c45 = 0; /* clause 22 */
48 dev_addr = regnum & 0x1f;
Shaohui Xiecd348ef2015-03-20 19:28:19 -070049 memac_clrbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
Shaohui Xie3a7ed5a2014-04-22 18:21:37 +080050 } else
Shaohui Xiecd348ef2015-03-20 19:28:19 -070051 memac_setbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
Roy Zang111fd192012-10-08 07:44:21 +000052
53 /* Wait till the bus is free */
Shaohui Xiecd348ef2015-03-20 19:28:19 -070054 while ((memac_in_32(&regs->mdio_stat)) & MDIO_STAT_BSY)
Roy Zang111fd192012-10-08 07:44:21 +000055 ;
56
57 /* Set the port and dev addr */
58 mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
Shaohui Xiecd348ef2015-03-20 19:28:19 -070059 memac_out_32(&regs->mdio_ctl, mdio_ctl);
Roy Zang111fd192012-10-08 07:44:21 +000060
61 /* Set the register address */
62 if (c45)
Shaohui Xiecd348ef2015-03-20 19:28:19 -070063 memac_out_32(&regs->mdio_addr, regnum & 0xffff);
Roy Zang111fd192012-10-08 07:44:21 +000064
65 /* Wait till the bus is free */
Shaohui Xiecd348ef2015-03-20 19:28:19 -070066 while ((memac_in_32(&regs->mdio_stat)) & MDIO_STAT_BSY)
Roy Zang111fd192012-10-08 07:44:21 +000067 ;
68
69 /* Write the value to the register */
Shaohui Xiecd348ef2015-03-20 19:28:19 -070070 memac_out_32(&regs->mdio_data, MDIO_DATA(value));
Roy Zang111fd192012-10-08 07:44:21 +000071
72 /* Wait till the MDIO write is complete */
Shaohui Xiecd348ef2015-03-20 19:28:19 -070073 while ((memac_in_32(&regs->mdio_data)) & MDIO_DATA_BSY)
Roy Zang111fd192012-10-08 07:44:21 +000074 ;
75
76 return 0;
77}
78
79/*
80 * Reads from register regnum in the PHY for device dev, returning the value.
81 * Clears miimcom first. All PHY configuration has to be done through the
82 * TSEC1 MIIM regs
83 */
84int memac_mdio_read(struct mii_dev *bus, int port_addr, int dev_addr,
85 int regnum)
86{
87 u32 mdio_ctl;
88 struct memac_mdio_controller *regs = bus->priv;
89 u32 c45 = 1;
90
91 if (dev_addr == MDIO_DEVAD_NONE) {
Shaohui Xieff5fb2a2014-08-13 18:32:19 +080092 if (!strcmp(bus->name, DEFAULT_FM_TGEC_MDIO_NAME))
93 return 0xffff;
Roy Zang111fd192012-10-08 07:44:21 +000094 c45 = 0; /* clause 22 */
95 dev_addr = regnum & 0x1f;
Shaohui Xiecd348ef2015-03-20 19:28:19 -070096 memac_clrbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
Shaohui Xie3a7ed5a2014-04-22 18:21:37 +080097 } else
Shaohui Xiecd348ef2015-03-20 19:28:19 -070098 memac_setbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
Roy Zang111fd192012-10-08 07:44:21 +000099
100 /* Wait till the bus is free */
Shaohui Xiecd348ef2015-03-20 19:28:19 -0700101 while ((memac_in_32(&regs->mdio_stat)) & MDIO_STAT_BSY)
Roy Zang111fd192012-10-08 07:44:21 +0000102 ;
103
104 /* Set the Port and Device Addrs */
105 mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
Shaohui Xiecd348ef2015-03-20 19:28:19 -0700106 memac_out_32(&regs->mdio_ctl, mdio_ctl);
Roy Zang111fd192012-10-08 07:44:21 +0000107
108 /* Set the register address */
109 if (c45)
Shaohui Xiecd348ef2015-03-20 19:28:19 -0700110 memac_out_32(&regs->mdio_addr, regnum & 0xffff);
Roy Zang111fd192012-10-08 07:44:21 +0000111
112 /* Wait till the bus is free */
Shaohui Xiecd348ef2015-03-20 19:28:19 -0700113 while ((memac_in_32(&regs->mdio_stat)) & MDIO_STAT_BSY)
Roy Zang111fd192012-10-08 07:44:21 +0000114 ;
115
116 /* Initiate the read */
117 mdio_ctl |= MDIO_CTL_READ;
Shaohui Xiecd348ef2015-03-20 19:28:19 -0700118 memac_out_32(&regs->mdio_ctl, mdio_ctl);
Roy Zang111fd192012-10-08 07:44:21 +0000119
120 /* Wait till the MDIO write is complete */
Shaohui Xiecd348ef2015-03-20 19:28:19 -0700121 while ((memac_in_32(&regs->mdio_data)) & MDIO_DATA_BSY)
Roy Zang111fd192012-10-08 07:44:21 +0000122 ;
123
124 /* Return all Fs if nothing was there */
Shaohui Xiecd348ef2015-03-20 19:28:19 -0700125 if (memac_in_32(&regs->mdio_stat) & MDIO_STAT_RD_ER)
Roy Zang111fd192012-10-08 07:44:21 +0000126 return 0xffff;
127
Shaohui Xiecd348ef2015-03-20 19:28:19 -0700128 return memac_in_32(&regs->mdio_data) & 0xffff;
Roy Zang111fd192012-10-08 07:44:21 +0000129}
130
131int memac_mdio_reset(struct mii_dev *bus)
132{
133 return 0;
134}
135
136int fm_memac_mdio_init(bd_t *bis, struct memac_mdio_info *info)
137{
138 struct mii_dev *bus = mdio_alloc();
139
140 if (!bus) {
141 printf("Failed to allocate FM TGEC MDIO bus\n");
142 return -1;
143 }
144
145 bus->read = memac_mdio_read;
146 bus->write = memac_mdio_write;
147 bus->reset = memac_mdio_reset;
Ben Whitten192bc692015-12-30 13:05:58 +0000148 strcpy(bus->name, info->name);
Roy Zang111fd192012-10-08 07:44:21 +0000149
150 bus->priv = info->regs;
151
Priyanka Jain2ee6c522014-04-08 10:55:49 +0530152 /*
153 * On some platforms like B4860, default value of MDIO_CLK_DIV bits
154 * in mdio_stat(mdio_cfg) register generates MDIO clock too high
155 * (much higher than 2.5MHz), violating the IEEE specs.
156 * On other platforms like T1040, default value of MDIO_CLK_DIV bits
157 * is zero, so MDIO clock is disabled.
158 * So, for proper functioning of MDIO, MDIO_CLK_DIV bits needs to
159 * be properly initialized.
Shaohui Xieae6b4582014-08-13 18:38:09 +0800160 * NEG bit default should be '1' as per FMAN-v3 RM, but on platform
161 * like T2080QDS, this bit default is '0', which leads to MDIO failure
162 * on XAUI PHY, so set this bit definitely.
Priyanka Jain2ee6c522014-04-08 10:55:49 +0530163 */
Shaohui Xiecd348ef2015-03-20 19:28:19 -0700164 memac_setbits_32(
165 &((struct memac_mdio_controller *)info->regs)->mdio_stat,
166 MDIO_STAT_CLKDIV(258) | MDIO_STAT_NEG);
Priyanka Jain2ee6c522014-04-08 10:55:49 +0530167
Roy Zang111fd192012-10-08 07:44:21 +0000168 return mdio_register(bus);
169}