stmmac_mdio.c 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*******************************************************************************
  STMMAC Ethernet Driver -- MDIO bus implementation
  Provides Bus interface for MII registers

  Copyright (C) 2007-2009  STMicroelectronics Ltd

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Author: Carl Shaw <carl.shaw@st.com>
  Maintainer: Giuseppe Cavallaro <peppe.cavallaro@st.com>
*******************************************************************************/

23
#include <linux/io.h>
24
#include <linux/iopoll.h>
25
#include <linux/mii.h>
26 27
#include <linux/of.h>
#include <linux/of_gpio.h>
28
#include <linux/of_mdio.h>
29 30
#include <linux/phy.h>
#include <linux/slab.h>
31

32
#include "dwxgmac2.h"
33 34 35 36 37
#include "stmmac.h"

#define MII_BUSY 0x00000001
#define MII_WRITE 0x00000002

38 39 40 41 42
/* GMAC4 defines */
#define MII_GMAC4_GOC_SHIFT		2
#define MII_GMAC4_WRITE			(1 << MII_GMAC4_GOC_SHIFT)
#define MII_GMAC4_READ			(3 << MII_GMAC4_GOC_SHIFT)

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/* XGMAC defines */
#define MII_XGMAC_SADDR			BIT(18)
#define MII_XGMAC_CMD_SHIFT		16
#define MII_XGMAC_WRITE			(1 << MII_XGMAC_CMD_SHIFT)
#define MII_XGMAC_READ			(3 << MII_XGMAC_CMD_SHIFT)
#define MII_XGMAC_BUSY			BIT(22)
#define MII_XGMAC_MAX_C22ADDR		3
#define MII_XGMAC_C22P_MASK		GENMASK(MII_XGMAC_MAX_C22ADDR, 0)

static int stmmac_xgmac2_c22_format(struct stmmac_priv *priv, int phyaddr,
				    int phyreg, u32 *hw_addr)
{
	unsigned int mii_data = priv->hw->mii.data;
	u32 tmp;

	/* HW does not support C22 addr >= 4 */
	if (phyaddr > MII_XGMAC_MAX_C22ADDR)
		return -ENODEV;
	/* Wait until any existing MII operation is complete */
	if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
			       !(tmp & MII_XGMAC_BUSY), 100, 10000))
		return -EBUSY;

	/* Set port as Clause 22 */
	tmp = readl(priv->ioaddr + XGMAC_MDIO_C22P);
	tmp &= ~MII_XGMAC_C22P_MASK;
	tmp |= BIT(phyaddr);
	writel(tmp, priv->ioaddr + XGMAC_MDIO_C22P);

	*hw_addr = (phyaddr << 16) | (phyreg & 0x1f);
	return 0;
}

static int stmmac_xgmac2_mdio_read(struct mii_bus *bus, int phyaddr, int phyreg)
{
	struct net_device *ndev = bus->priv;
	struct stmmac_priv *priv = netdev_priv(ndev);
	unsigned int mii_address = priv->hw->mii.addr;
	unsigned int mii_data = priv->hw->mii.data;
	u32 tmp, addr, value = MII_XGMAC_BUSY;
	int ret;

	if (phyreg & MII_ADDR_C45) {
		return -EOPNOTSUPP;
	} else {
		ret = stmmac_xgmac2_c22_format(priv, phyaddr, phyreg, &addr);
		if (ret)
			return ret;
	}

	value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
		& priv->hw->mii.clk_csr_mask;
	value |= MII_XGMAC_SADDR | MII_XGMAC_READ;

	/* Wait until any existing MII operation is complete */
	if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
			       !(tmp & MII_XGMAC_BUSY), 100, 10000))
		return -EBUSY;

	/* Set the MII address register to read */
	writel(addr, priv->ioaddr + mii_address);
	writel(value, priv->ioaddr + mii_data);

	/* Wait until any existing MII operation is complete */
	if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
			       !(tmp & MII_XGMAC_BUSY), 100, 10000))
		return -EBUSY;

	/* Read the data from the MII data register */
	return readl(priv->ioaddr + mii_data) & GENMASK(15, 0);
}

static int stmmac_xgmac2_mdio_write(struct mii_bus *bus, int phyaddr,
				    int phyreg, u16 phydata)
{
	struct net_device *ndev = bus->priv;
	struct stmmac_priv *priv = netdev_priv(ndev);
	unsigned int mii_address = priv->hw->mii.addr;
	unsigned int mii_data = priv->hw->mii.data;
	u32 addr, tmp, value = MII_XGMAC_BUSY;
	int ret;

	if (phyreg & MII_ADDR_C45) {
		return -EOPNOTSUPP;
	} else {
		ret = stmmac_xgmac2_c22_format(priv, phyaddr, phyreg, &addr);
		if (ret)
			return ret;
	}

	value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
		& priv->hw->mii.clk_csr_mask;
	value |= phydata | MII_XGMAC_SADDR;
	value |= MII_XGMAC_WRITE;

	/* Wait until any existing MII operation is complete */
	if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
			       !(tmp & MII_XGMAC_BUSY), 100, 10000))
		return -EBUSY;

	/* Set the MII address register to write */
	writel(addr, priv->ioaddr + mii_address);
	writel(value, priv->ioaddr + mii_data);

	/* Wait until any existing MII operation is complete */
	return readl_poll_timeout(priv->ioaddr + mii_data, tmp,
				  !(tmp & MII_XGMAC_BUSY), 100, 10000);
}

152 153 154
/**
 * stmmac_mdio_read
 * @bus: points to the mii_bus structure
L
LABBE Corentin 已提交
155 156
 * @phyaddr: MII addr
 * @phyreg: MII reg
157 158 159 160 161 162 163 164 165
 * Description: it reads data from the MII register from within the phy device.
 * For the 7111 GMAC, we must set the bit 0 in the MII address register while
 * accessing the PHY registers.
 * Fortunately, it seems this has no drawback for the 7109 MAC.
 */
static int stmmac_mdio_read(struct mii_bus *bus, int phyaddr, int phyreg)
{
	struct net_device *ndev = bus->priv;
	struct stmmac_priv *priv = netdev_priv(ndev);
166 167
	unsigned int mii_address = priv->hw->mii.addr;
	unsigned int mii_data = priv->hw->mii.data;
168
	u32 v;
169
	int data;
L
LABBE Corentin 已提交
170 171 172 173 174
	u32 value = MII_BUSY;

	value |= (phyaddr << priv->hw->mii.addr_shift)
		& priv->hw->mii.addr_mask;
	value |= (phyreg << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask;
J
jpinto 已提交
175 176
	value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
		& priv->hw->mii.clk_csr_mask;
L
LABBE Corentin 已提交
177 178
	if (priv->plat->has_gmac4)
		value |= MII_GMAC4_READ;
179

180 181
	if (readl_poll_timeout(priv->ioaddr + mii_address, v, !(v & MII_BUSY),
			       100, 10000))
182 183
		return -EBUSY;

184
	writel(value, priv->ioaddr + mii_address);
185

186 187
	if (readl_poll_timeout(priv->ioaddr + mii_address, v, !(v & MII_BUSY),
			       100, 10000))
188
		return -EBUSY;
189 190

	/* Read the data from the MII data register */
191
	data = (int)readl(priv->ioaddr + mii_data);
192 193 194 195 196 197 198

	return data;
}

/**
 * stmmac_mdio_write
 * @bus: points to the mii_bus structure
L
LABBE Corentin 已提交
199 200
 * @phyaddr: MII addr
 * @phyreg: MII reg
201 202 203 204 205 206 207 208
 * @phydata: phy data
 * Description: it writes the data into the MII register from within the device.
 */
static int stmmac_mdio_write(struct mii_bus *bus, int phyaddr, int phyreg,
			     u16 phydata)
{
	struct net_device *ndev = bus->priv;
	struct stmmac_priv *priv = netdev_priv(ndev);
209 210
	unsigned int mii_address = priv->hw->mii.addr;
	unsigned int mii_data = priv->hw->mii.data;
211
	u32 v;
212
	u32 value = MII_BUSY;
213

L
LABBE Corentin 已提交
214 215 216
	value |= (phyaddr << priv->hw->mii.addr_shift)
		& priv->hw->mii.addr_mask;
	value |= (phyreg << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask;
217

J
jpinto 已提交
218 219
	value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
		& priv->hw->mii.clk_csr_mask;
L
LABBE Corentin 已提交
220 221
	if (priv->plat->has_gmac4)
		value |= MII_GMAC4_WRITE;
222 223
	else
		value |= MII_WRITE;
224 225

	/* Wait until any existing MII operation is complete */
226 227
	if (readl_poll_timeout(priv->ioaddr + mii_address, v, !(v & MII_BUSY),
			       100, 10000))
228 229 230 231 232 233 234
		return -EBUSY;

	/* Set the MII address register to write */
	writel(phydata, priv->ioaddr + mii_data);
	writel(value, priv->ioaddr + mii_address);

	/* Wait until any existing MII operation is complete */
235 236
	return readl_poll_timeout(priv->ioaddr + mii_address, v, !(v & MII_BUSY),
				  100, 10000);
237 238
}

239 240 241 242 243
/**
 * stmmac_mdio_reset
 * @bus: points to the mii_bus structure
 * Description: reset the MII bus
 */
244
int stmmac_mdio_reset(struct mii_bus *bus)
245
{
246
#if defined(CONFIG_STMMAC_PLATFORM)
247 248
	struct net_device *ndev = bus->priv;
	struct stmmac_priv *priv = netdev_priv(ndev);
249
	unsigned int mii_address = priv->hw->mii.addr;
250 251 252 253 254 255
	struct stmmac_mdio_bus_data *data = priv->plat->mdio_bus_data;

#ifdef CONFIG_OF
	if (priv->device->of_node) {
		if (data->reset_gpio < 0) {
			struct device_node *np = priv->device->of_node;
256

257 258 259 260 261 262 263 264 265 266 267 268 269
			if (!np)
				return 0;

			data->reset_gpio = of_get_named_gpio(np,
						"snps,reset-gpio", 0);
			if (data->reset_gpio < 0)
				return 0;

			data->active_low = of_property_read_bool(np,
						"snps,reset-active-low");
			of_property_read_u32_array(np,
				"snps,reset-delays-us", data->delays, 3);

270 271 272
			if (gpio_request(data->reset_gpio, "mdio-reset"))
				return 0;
		}
273

274 275 276 277
		gpio_direction_output(data->reset_gpio,
				      data->active_low ? 1 : 0);
		if (data->delays[0])
			msleep(DIV_ROUND_UP(data->delays[0], 1000));
278

279 280 281
		gpio_set_value(data->reset_gpio, data->active_low ? 0 : 1);
		if (data->delays[1])
			msleep(DIV_ROUND_UP(data->delays[1], 1000));
282

283 284 285
		gpio_set_value(data->reset_gpio, data->active_low ? 1 : 0);
		if (data->delays[2])
			msleep(DIV_ROUND_UP(data->delays[2], 1000));
286 287
	}
#endif
288

289
	if (data->phy_reset) {
290
		netdev_dbg(ndev, "stmmac_mdio_reset: calling phy_reset\n");
291
		data->phy_reset(priv->plat->bsp_priv);
292 293 294 295
	}

	/* This is a workaround for problems with the STE101P PHY.
	 * It doesn't complete its reset until at least one clock cycle
296
	 * on MDC, so perform a dummy mdio read. To be updated for GMAC4
297
	 * if needed.
298
	 */
299 300
	if (!priv->plat->has_gmac4)
		writel(0, priv->ioaddr + mii_address);
301
#endif
302 303 304 305 306 307 308 309 310 311 312 313 314
	return 0;
}

/**
 * stmmac_mdio_register
 * @ndev: net device structure
 * Description: it registers the MII bus
 */
int stmmac_mdio_register(struct net_device *ndev)
{
	int err = 0;
	struct mii_bus *new_bus;
	struct stmmac_priv *priv = netdev_priv(ndev);
315
	struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data;
G
Giuseppe CAVALLARO 已提交
316
	struct device_node *mdio_node = priv->plat->mdio_node;
317
	struct device *dev = ndev->dev.parent;
318
	int addr, found, max_addr;
319

320 321 322
	if (!mdio_bus_data)
		return 0;

323
	new_bus = mdiobus_alloc();
324
	if (!new_bus)
325 326
		return -ENOMEM;

327
	if (mdio_bus_data->irqs)
328
		memcpy(new_bus->irq, mdio_bus_data->irqs, sizeof(new_bus->irq));
329

330 331 332 333 334
#ifdef CONFIG_OF
	if (priv->device->of_node)
		mdio_bus_data->reset_gpio = -1;
#endif

335
	new_bus->name = "stmmac";
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

	if (priv->plat->has_xgmac) {
		new_bus->read = &stmmac_xgmac2_mdio_read;
		new_bus->write = &stmmac_xgmac2_mdio_write;

		/* Right now only C22 phys are supported */
		max_addr = MII_XGMAC_MAX_C22ADDR + 1;

		/* Check if DT specified an unsupported phy addr */
		if (priv->plat->phy_addr > MII_XGMAC_MAX_C22ADDR)
			dev_err(dev, "Unsupported phy_addr (max=%d)\n",
					MII_XGMAC_MAX_C22ADDR);
	} else {
		new_bus->read = &stmmac_mdio_read;
		new_bus->write = &stmmac_mdio_write;
		max_addr = PHY_MAX_ADDR;
	}
353

354
	new_bus->reset = &stmmac_mdio_reset;
355
	snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s-%x",
G
Giuseppe CAVALLARO 已提交
356
		 new_bus->name, priv->plat->bus_id);
357
	new_bus->priv = ndev;
358
	new_bus->phy_mask = mdio_bus_data->phy_mask;
359
	new_bus->parent = priv->device;
360

361
	err = of_mdiobus_register(new_bus, mdio_node);
362
	if (err != 0) {
363
		dev_err(dev, "Cannot register the MDIO bus\n");
364 365 366
		goto bus_register_fail;
	}

367 368 369
	if (priv->plat->phy_node || mdio_node)
		goto bus_register_done;

370
	found = 0;
371
	for (addr = 0; addr < max_addr; addr++) {
372
		struct phy_device *phydev = mdiobus_get_phy(new_bus, addr);
373 374 375 376 377 378 379 380 381 382 383 384 385

		if (!phydev)
			continue;

		/*
		 * If an IRQ was provided to be assigned after
		 * the bus probe, do it here.
		 */
		if (!mdio_bus_data->irqs &&
		    (mdio_bus_data->probed_phy_irq > 0)) {
			new_bus->irq[addr] = mdio_bus_data->probed_phy_irq;
			phydev->irq = mdio_bus_data->probed_phy_irq;
		}
386

387 388 389 390 391 392 393 394
		/*
		 * If we're going to bind the MAC to this PHY bus,
		 * and no PHY number was provided to the MAC,
		 * use the one probed here.
		 */
		if (priv->plat->phy_addr == -1)
			priv->plat->phy_addr = addr;

395
		phy_attached_info(phydev);
396
		found = 1;
397 398
	}

399
	if (!found && !mdio_node) {
400
		dev_warn(dev, "No PHY found\n");
401 402 403 404 405
		mdiobus_unregister(new_bus);
		mdiobus_free(new_bus);
		return -ENODEV;
	}

406
bus_register_done:
407
	priv->mii = new_bus;
408 409

	return 0;
410

411
bus_register_fail:
412
	mdiobus_free(new_bus);
413 414 415 416 417 418 419 420 421 422 423 424
	return err;
}

/**
 * stmmac_mdio_unregister
 * @ndev: net device structure
 * Description: it unregisters the MII bus
 */
int stmmac_mdio_unregister(struct net_device *ndev)
{
	struct stmmac_priv *priv = netdev_priv(ndev);

425 426 427
	if (!priv->mii)
		return 0;

428 429
	mdiobus_unregister(priv->mii);
	priv->mii->priv = NULL;
430 431
	mdiobus_free(priv->mii);
	priv->mii = NULL;
432 433 434

	return 0;
}