blob: a155d8930b38761969b7dff7c12f7b1376193eea [file] [log] [blame]
Roy Zang111fd192012-10-08 07:44:21 +00001/*
2 * Copyright 2012 Freescale Semiconductor, Inc.
Andy Flemingb21f87a32014-07-25 17:39:08 -05003 * Andy Fleming <afleming@gmail.com>
Roy Zang111fd192012-10-08 07:44:21 +00004 * Roy Zang <tie-fei.zang@freescale.com>
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Roy Zang111fd192012-10-08 07:44:21 +00007 * Some part is taken from tsec.c
8 */
9#include <common.h>
10#include <miiphy.h>
11#include <phy.h>
12#include <asm/io.h>
13#include <asm/fsl_memac.h>
14#include <fm_eth.h>
15
16/*
17 * Write value to the PHY for this device to the register at regnum, waiting
18 * until the write is done before it returns. All PHY configuration has to be
19 * done through the TSEC1 MIIM regs
20 */
21int memac_mdio_write(struct mii_dev *bus, int port_addr, int dev_addr,
22 int regnum, u16 value)
23{
24 u32 mdio_ctl;
25 struct memac_mdio_controller *regs = bus->priv;
26 u32 c45 = 1; /* Default to 10G interface */
27
28 if (dev_addr == MDIO_DEVAD_NONE) {
29 c45 = 0; /* clause 22 */
30 dev_addr = regnum & 0x1f;
31 clrbits_be32(&regs->mdio_stat, MDIO_STAT_ENC);
Shaohui Xie3a7ed5a2014-04-22 18:21:37 +080032 } else
Roy Zang111fd192012-10-08 07:44:21 +000033 setbits_be32(&regs->mdio_stat, MDIO_STAT_ENC);
Roy Zang111fd192012-10-08 07:44:21 +000034
35 /* Wait till the bus is free */
36 while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
37 ;
38
39 /* Set the port and dev addr */
40 mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
41 out_be32(&regs->mdio_ctl, mdio_ctl);
42
43 /* Set the register address */
44 if (c45)
45 out_be32(&regs->mdio_addr, regnum & 0xffff);
46
47 /* Wait till the bus is free */
48 while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
49 ;
50
51 /* Write the value to the register */
52 out_be32(&regs->mdio_data, MDIO_DATA(value));
53
54 /* Wait till the MDIO write is complete */
55 while ((in_be32(&regs->mdio_data)) & MDIO_DATA_BSY)
56 ;
57
58 return 0;
59}
60
61/*
62 * Reads from register regnum in the PHY for device dev, returning the value.
63 * Clears miimcom first. All PHY configuration has to be done through the
64 * TSEC1 MIIM regs
65 */
66int memac_mdio_read(struct mii_dev *bus, int port_addr, int dev_addr,
67 int regnum)
68{
69 u32 mdio_ctl;
70 struct memac_mdio_controller *regs = bus->priv;
71 u32 c45 = 1;
72
73 if (dev_addr == MDIO_DEVAD_NONE) {
Shaohui Xieff5fb2a2014-08-13 18:32:19 +080074 if (!strcmp(bus->name, DEFAULT_FM_TGEC_MDIO_NAME))
75 return 0xffff;
Roy Zang111fd192012-10-08 07:44:21 +000076 c45 = 0; /* clause 22 */
77 dev_addr = regnum & 0x1f;
78 clrbits_be32(&regs->mdio_stat, MDIO_STAT_ENC);
Shaohui Xie3a7ed5a2014-04-22 18:21:37 +080079 } else
Roy Zang111fd192012-10-08 07:44:21 +000080 setbits_be32(&regs->mdio_stat, MDIO_STAT_ENC);
Roy Zang111fd192012-10-08 07:44:21 +000081
82 /* Wait till the bus is free */
83 while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
84 ;
85
86 /* Set the Port and Device Addrs */
87 mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
88 out_be32(&regs->mdio_ctl, mdio_ctl);
89
90 /* Set the register address */
91 if (c45)
92 out_be32(&regs->mdio_addr, regnum & 0xffff);
93
94 /* Wait till the bus is free */
95 while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
96 ;
97
98 /* Initiate the read */
99 mdio_ctl |= MDIO_CTL_READ;
100 out_be32(&regs->mdio_ctl, mdio_ctl);
101
102 /* Wait till the MDIO write is complete */
103 while ((in_be32(&regs->mdio_data)) & MDIO_DATA_BSY)
104 ;
105
106 /* Return all Fs if nothing was there */
107 if (in_be32(&regs->mdio_stat) & MDIO_STAT_RD_ER)
108 return 0xffff;
109
110 return in_be32(&regs->mdio_data) & 0xffff;
111}
112
113int memac_mdio_reset(struct mii_dev *bus)
114{
115 return 0;
116}
117
118int fm_memac_mdio_init(bd_t *bis, struct memac_mdio_info *info)
119{
120 struct mii_dev *bus = mdio_alloc();
121
122 if (!bus) {
123 printf("Failed to allocate FM TGEC MDIO bus\n");
124 return -1;
125 }
126
127 bus->read = memac_mdio_read;
128 bus->write = memac_mdio_write;
129 bus->reset = memac_mdio_reset;
130 sprintf(bus->name, info->name);
131
132 bus->priv = info->regs;
133
Priyanka Jain2ee6c522014-04-08 10:55:49 +0530134 /*
135 * On some platforms like B4860, default value of MDIO_CLK_DIV bits
136 * in mdio_stat(mdio_cfg) register generates MDIO clock too high
137 * (much higher than 2.5MHz), violating the IEEE specs.
138 * On other platforms like T1040, default value of MDIO_CLK_DIV bits
139 * is zero, so MDIO clock is disabled.
140 * So, for proper functioning of MDIO, MDIO_CLK_DIV bits needs to
141 * be properly initialized.
Shaohui Xieae6b4582014-08-13 18:38:09 +0800142 * NEG bit default should be '1' as per FMAN-v3 RM, but on platform
143 * like T2080QDS, this bit default is '0', which leads to MDIO failure
144 * on XAUI PHY, so set this bit definitely.
Priyanka Jain2ee6c522014-04-08 10:55:49 +0530145 */
146 setbits_be32(&((struct memac_mdio_controller *)info->regs)->mdio_stat,
Shaohui Xieae6b4582014-08-13 18:38:09 +0800147 MDIO_STAT_CLKDIV(258) | MDIO_STAT_NEG);
Priyanka Jain2ee6c522014-04-08 10:55:49 +0530148
Roy Zang111fd192012-10-08 07:44:21 +0000149 return mdio_register(bus);
150}