blob: f6c8f80c835e0c1e5514c334c7f0db7b71cb6691 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Gala2683c532011-04-13 08:37:44 -05002/*
3 * Copyright 2009-2011 Freescale Semiconductor, Inc.
Andy Flemingb36a4d42014-07-25 17:39:08 -05004 * Andy Fleming <afleming@gmail.com>
Kumar Gala2683c532011-04-13 08:37:44 -05005 * Some part is taken from tsec.c
6 */
Kumar Gala2683c532011-04-13 08:37:44 -05007#include <miiphy.h>
8#include <phy.h>
9#include <asm/io.h>
Shaohui Xie513eaf22015-10-26 19:47:47 +080010#include <fsl_tgec.h>
Kumar Gala2683c532011-04-13 08:37:44 -050011#include <fm_eth.h>
12
13/*
14 * Write value to the PHY for this device to the register at regnum, waiting
15 * until the write is done before it returns. All PHY configuration has to be
16 * done through the TSEC1 MIIM regs
17 */
Kim Phillips914b0782012-10-29 13:34:34 +000018static int tgec_mdio_write(struct mii_dev *bus, int port_addr, int dev_addr,
19 int regnum, u16 value)
Kumar Gala2683c532011-04-13 08:37:44 -050020{
21 u32 mdio_ctl;
22 u32 stat_val;
23 struct tgec_mdio_controller *regs = bus->priv;
24
25 if (dev_addr == MDIO_DEVAD_NONE)
26 return 0;
27
28 /* Wait till the bus is free */
29 stat_val = MDIO_STAT_CLKDIV(100);
30 out_be32(&regs->mdio_stat, stat_val);
31 while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
32 ;
33
34 /* Set the port and dev addr */
35 mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
36 out_be32(&regs->mdio_ctl, mdio_ctl);
37
38 /* Set the register address */
39 out_be32(&regs->mdio_addr, regnum & 0xffff);
40
41 /* Wait till the bus is free */
42 while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
43 ;
44
45 /* Write the value to the register */
46 out_be32(&regs->mdio_data, MDIO_DATA(value));
47
48 /* Wait till the MDIO write is complete */
49 while ((in_be32(&regs->mdio_data)) & MDIO_DATA_BSY)
50 ;
51
52 return 0;
53}
54
55/*
56 * Reads from register regnum in the PHY for device dev, returning the value.
57 * Clears miimcom first. All PHY configuration has to be done through the
58 * TSEC1 MIIM regs
59 */
Kim Phillips914b0782012-10-29 13:34:34 +000060static int tgec_mdio_read(struct mii_dev *bus, int port_addr, int dev_addr,
61 int regnum)
Kumar Gala2683c532011-04-13 08:37:44 -050062{
63 u32 mdio_ctl;
64 u32 stat_val;
65 struct tgec_mdio_controller *regs = bus->priv;
66
67 if (dev_addr == MDIO_DEVAD_NONE)
68 return 0xffff;
69
70 stat_val = MDIO_STAT_CLKDIV(100);
71 out_be32(&regs->mdio_stat, stat_val);
72 /* Wait till the bus is free */
73 while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
74 ;
75
76 /* Set the Port and Device Addrs */
77 mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
78 out_be32(&regs->mdio_ctl, mdio_ctl);
79
80 /* Set the register address */
81 out_be32(&regs->mdio_addr, regnum & 0xffff);
82
83 /* Wait till the bus is free */
84 while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
85 ;
86
87 /* Initiate the read */
88 mdio_ctl |= MDIO_CTL_READ;
89 out_be32(&regs->mdio_ctl, mdio_ctl);
90
91 /* Wait till the MDIO write is complete */
92 while ((in_be32(&regs->mdio_data)) & MDIO_DATA_BSY)
93 ;
94
95 /* Return all Fs if nothing was there */
96 if (in_be32(&regs->mdio_stat) & MDIO_STAT_RD_ER)
97 return 0xffff;
98
99 return in_be32(&regs->mdio_data) & 0xffff;
100}
101
Kim Phillips914b0782012-10-29 13:34:34 +0000102static int tgec_mdio_reset(struct mii_dev *bus)
Kumar Gala2683c532011-04-13 08:37:44 -0500103{
104 return 0;
105}
106
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900107int fm_tgec_mdio_init(struct bd_info *bis, struct tgec_mdio_info *info)
Kumar Gala2683c532011-04-13 08:37:44 -0500108{
109 struct mii_dev *bus = mdio_alloc();
110
111 if (!bus) {
112 printf("Failed to allocate FM TGEC MDIO bus\n");
113 return -1;
114 }
115
116 bus->read = tgec_mdio_read;
117 bus->write = tgec_mdio_write;
118 bus->reset = tgec_mdio_reset;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000119 strcpy(bus->name, info->name);
Kumar Gala2683c532011-04-13 08:37:44 -0500120
121 bus->priv = info->regs;
122
123 return mdio_register(bus);
124}