1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Marvell International Ltd.
14 #include <linux/ctype.h>
15 #include <linux/delay.h>
17 #define PCI_DEVICE_ID_OCTEONTX_SMI 0xA02B
19 DECLARE_GLOBAL_DATA_PTR;
21 enum octeontx_smi_mode {
64 struct smi_x_wr_dat_s {
73 struct smi_x_rd_dat_s {
87 #define SMI_X_RD_DAT 0x10ull
88 #define SMI_X_WR_DAT 0x08ull
89 #define SMI_X_CMD 0x00ull
90 #define SMI_X_CLK 0x18ull
91 #define SMI_X_EN 0x20ull
93 struct octeontx_smi_priv {
94 void __iomem *baseaddr;
95 enum octeontx_smi_mode mode;
98 #define MDIO_TIMEOUT 10000
100 void octeontx_smi_setmode(struct mii_dev *bus, enum octeontx_smi_mode mode)
102 struct octeontx_smi_priv *priv = bus->priv;
103 union smi_x_clk smix_clk;
105 smix_clk.u = readq(priv->baseaddr + SMI_X_CLK);
106 smix_clk.s.mode = mode;
107 smix_clk.s.preamble = mode == CLAUSE45;
108 writeq(smix_clk.u, priv->baseaddr + SMI_X_CLK);
113 int octeontx_c45_addr(struct mii_dev *bus, int addr, int devad, int regnum)
115 struct octeontx_smi_priv *priv = bus->priv;
117 union smi_x_cmd smix_cmd;
118 union smi_x_wr_dat smix_wr_dat;
119 unsigned long timeout = MDIO_TIMEOUT;
122 smix_wr_dat.s.dat = regnum;
124 writeq(smix_wr_dat.u, priv->baseaddr + SMI_X_WR_DAT);
127 smix_cmd.s.phy_op = SMI_OP_C45_ADDR;
128 smix_cmd.s.phy_adr = addr;
129 smix_cmd.s.reg_adr = devad;
131 writeq(smix_cmd.u, priv->baseaddr + SMI_X_CMD);
134 smix_wr_dat.u = readq(priv->baseaddr + SMI_X_WR_DAT);
137 } while (smix_wr_dat.s.pending && timeout);
142 int octeontx_phy_read(struct mii_dev *bus, int addr, int devad, int regnum)
144 struct octeontx_smi_priv *priv = bus->priv;
145 union smi_x_cmd smix_cmd;
146 union smi_x_rd_dat smix_rd_dat;
147 unsigned long timeout = MDIO_TIMEOUT;
150 enum octeontx_smi_mode mode = (devad < 0) ? CLAUSE22 : CLAUSE45;
152 debug("RD: Mode: %u, baseaddr: %p, addr: %d, devad: %d, reg: %d\n",
153 mode, priv->baseaddr, addr, devad, regnum);
155 octeontx_smi_setmode(bus, mode);
157 if (mode == CLAUSE45) {
158 ret = octeontx_c45_addr(bus, addr, devad, regnum);
160 debug("RD: ret: %u\n", ret);
167 smix_cmd.s.phy_adr = addr;
169 if (mode == CLAUSE45) {
170 smix_cmd.s.reg_adr = devad;
171 smix_cmd.s.phy_op = SMI_OP_C45_READ;
173 smix_cmd.s.reg_adr = regnum;
174 smix_cmd.s.phy_op = SMI_OP_C22_READ;
177 writeq(smix_cmd.u, priv->baseaddr + SMI_X_CMD);
180 smix_rd_dat.u = readq(priv->baseaddr + SMI_X_RD_DAT);
183 } while (smix_rd_dat.s.pending && timeout);
185 debug("SMIX_RD_DAT: %lx\n", (unsigned long)smix_rd_dat.u);
187 return smix_rd_dat.s.dat;
190 int octeontx_phy_write(struct mii_dev *bus, int addr, int devad, int regnum,
193 struct octeontx_smi_priv *priv = bus->priv;
194 union smi_x_cmd smix_cmd;
195 union smi_x_wr_dat smix_wr_dat;
196 unsigned long timeout = MDIO_TIMEOUT;
199 enum octeontx_smi_mode mode = (devad < 0) ? CLAUSE22 : CLAUSE45;
201 debug("WR: Mode: %u, baseaddr: %p, addr: %d, devad: %d, reg: %d\n",
202 mode, priv->baseaddr, addr, devad, regnum);
204 if (mode == CLAUSE45) {
205 ret = octeontx_c45_addr(bus, addr, devad, regnum);
207 debug("WR: ret: %u\n", ret);
214 smix_wr_dat.s.dat = value;
216 writeq(smix_wr_dat.u, priv->baseaddr + SMI_X_WR_DAT);
219 smix_cmd.s.phy_adr = addr;
221 if (mode == CLAUSE45) {
222 smix_cmd.s.reg_adr = devad;
223 smix_cmd.s.phy_op = SMI_OP_C45_WRITE;
225 smix_cmd.s.reg_adr = regnum;
226 smix_cmd.s.phy_op = SMI_OP_C22_WRITE;
229 writeq(smix_cmd.u, priv->baseaddr + SMI_X_CMD);
232 smix_wr_dat.u = readq(priv->baseaddr + SMI_X_WR_DAT);
235 } while (smix_wr_dat.s.pending && timeout);
237 debug("SMIX_WR_DAT: %lx\n", (unsigned long)smix_wr_dat.u);
242 int octeontx_smi_reset(struct mii_dev *bus)
244 struct octeontx_smi_priv *priv = bus->priv;
246 union smi_x_en smi_en;
249 writeq(smi_en.u, priv->baseaddr + SMI_X_EN);
252 writeq(smi_en.u, priv->baseaddr + SMI_X_EN);
254 octeontx_smi_setmode(bus, CLAUSE22);
259 /* PHY XS initialization, primarily for RXAUI
262 int rxaui_phy_xs_init(struct mii_dev *bus, int phy_addr)
266 int phy_id1, phy_id2;
267 int oui, model_number;
269 phy_id1 = octeontx_phy_read(bus, phy_addr, 1, 0x2);
270 phy_id2 = octeontx_phy_read(bus, phy_addr, 1, 0x3);
271 model_number = (phy_id2 >> 4) & 0x3F;
272 debug("%s model %x\n", __func__, model_number);
275 oui |= (phy_id2 >> 10) & 0x3F;
276 debug("%s oui %x\n", __func__, oui);
279 if (model_number == 9) {
280 debug("%s +\n", __func__);
281 /* Perform hardware reset in XGXS control */
282 reg = octeontx_phy_read(bus, phy_addr, 4, 0x0);
283 if ((reg & 0xffff) < 0)
286 octeontx_phy_write(bus, phy_addr, 4, 0x0, reg);
288 start_time = get_timer(0);
290 reg = octeontx_phy_read(bus, phy_addr, 4, 0x0);
291 if ((reg & 0xffff) < 0)
293 } while ((reg & 0x8000) && get_timer(start_time) < 500);
295 printf("HW reset for M88X3120 PHY failed");
296 printf("MII_BMCR: 0x%x\n", reg);
299 /* program 4.49155 with 0x5 */
300 octeontx_phy_write(bus, phy_addr, 4, 0xc003, 0x5);
310 debug("M88X3120 PHY config read failed\n");
314 int octeontx_smi_probe(struct udevice *dev)
316 int ret, subnode, cnt = 0, node = dev->node.of_offset;
318 struct octeontx_smi_priv *priv;
319 pci_dev_t bdf = dm_pci_get_bdf(dev);
321 debug("SMI PCI device: %x\n", bdf);
322 if (!dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, PCI_REGION_MEM)) {
323 printf("Failed to map PCI region for bdf %x\n", bdf);
327 fdt_for_each_subnode(subnode, gd->fdt_blob, node) {
328 ret = fdt_node_check_compatible(gd->fdt_blob, subnode,
329 "cavium,thunder-8890-mdio");
334 priv = malloc(sizeof(*priv));
336 printf("Failed to allocate OcteonTX MDIO bus # %u\n",
341 bus->read = octeontx_phy_read;
342 bus->write = octeontx_phy_write;
343 bus->reset = octeontx_smi_reset;
346 priv->mode = CLAUSE22;
347 priv->baseaddr = (void __iomem *)fdtdec_get_addr(gd->fdt_blob,
350 debug("mdio base addr %p\n", priv->baseaddr);
352 /* use given name or generate its own unique name */
353 snprintf(bus->name, MDIO_NAME_LEN, "smi%d", cnt++);
355 ret = mdio_register(bus);
362 static const struct udevice_id octeontx_smi_ids[] = {
363 { .compatible = "cavium,thunder-8890-mdio-nexus" },
367 U_BOOT_DRIVER(octeontx_smi) = {
368 .name = "octeontx_smi",
370 .probe = octeontx_smi_probe,
371 .of_match = octeontx_smi_ids,
374 static struct pci_device_id octeontx_smi_supported[] = {
375 { PCI_VDEVICE(CAVIUM, PCI_DEVICE_ID_CAVIUM_SMI) },
379 U_BOOT_PCI_DEVICE(octeontx_smi, octeontx_smi_supported);