Commit | Line | Data |
---|---|---|
e4f2379d AB |
1 | /* |
2 | * Copyright (C) 2004-2013 Synopsys, Inc. (www.synopsys.com) | |
3 | * | |
4 | * MDIO implementation for ARC EMAC | |
5 | */ | |
6 | ||
7 | #include <linux/delay.h> | |
8 | #include <linux/of_mdio.h> | |
9 | #include <linux/platform_device.h> | |
10 | ||
11 | #include "emac.h" | |
12 | ||
13 | /* Number of seconds we wait for "MDIO complete" flag to appear */ | |
14 | #define ARC_MDIO_COMPLETE_POLL_COUNT 1 | |
15 | ||
16 | /** | |
17 | * arc_mdio_complete_wait - Waits until MDIO transaction is completed. | |
18 | * @priv: Pointer to ARC EMAC private data structure. | |
19 | * | |
20 | * returns: 0 on success, -ETIMEDOUT on a timeout. | |
21 | */ | |
22 | static int arc_mdio_complete_wait(struct arc_emac_priv *priv) | |
23 | { | |
24 | unsigned int i; | |
25 | ||
26 | for (i = 0; i < ARC_MDIO_COMPLETE_POLL_COUNT * 40; i++) { | |
27 | unsigned int status = arc_reg_get(priv, R_STATUS); | |
28 | ||
29 | status &= MDIO_MASK; | |
30 | ||
31 | if (status) { | |
32 | /* Reset "MDIO complete" flag */ | |
33 | arc_reg_set(priv, R_STATUS, status); | |
34 | return 0; | |
35 | } | |
36 | ||
37 | msleep(25); | |
38 | } | |
39 | ||
40 | return -ETIMEDOUT; | |
41 | } | |
42 | ||
43 | /** | |
44 | * arc_mdio_read - MDIO interface read function. | |
45 | * @bus: Pointer to MII bus structure. | |
46 | * @phy_addr: Address of the PHY device. | |
47 | * @reg_num: PHY register to read. | |
48 | * | |
49 | * returns: The register contents on success, -ETIMEDOUT on a timeout. | |
50 | * | |
51 | * Reads the contents of the requested register from the requested PHY | |
52 | * address. | |
53 | */ | |
54 | static int arc_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num) | |
55 | { | |
56 | struct arc_emac_priv *priv = bus->priv; | |
57 | unsigned int value; | |
58 | int error; | |
59 | ||
60 | arc_reg_set(priv, R_MDIO, | |
61 | 0x60020000 | (phy_addr << 23) | (reg_num << 18)); | |
62 | ||
63 | error = arc_mdio_complete_wait(priv); | |
64 | if (error < 0) | |
65 | return error; | |
66 | ||
67 | value = arc_reg_get(priv, R_MDIO) & 0xffff; | |
68 | ||
69 | dev_dbg(priv->dev, "arc_mdio_read(phy_addr=%i, reg_num=%x) = %x\n", | |
70 | phy_addr, reg_num, value); | |
71 | ||
72 | return value; | |
73 | } | |
74 | ||
75 | /** | |
76 | * arc_mdio_write - MDIO interface write function. | |
77 | * @bus: Pointer to MII bus structure. | |
78 | * @phy_addr: Address of the PHY device. | |
79 | * @reg_num: PHY register to write to. | |
80 | * @value: Value to be written into the register. | |
81 | * | |
82 | * returns: 0 on success, -ETIMEDOUT on a timeout. | |
83 | * | |
84 | * Writes the value to the requested register. | |
85 | */ | |
86 | static int arc_mdio_write(struct mii_bus *bus, int phy_addr, | |
87 | int reg_num, u16 value) | |
88 | { | |
89 | struct arc_emac_priv *priv = bus->priv; | |
90 | ||
91 | dev_dbg(priv->dev, | |
92 | "arc_mdio_write(phy_addr=%i, reg_num=%x, value=%x)\n", | |
93 | phy_addr, reg_num, value); | |
94 | ||
95 | arc_reg_set(priv, R_MDIO, | |
96 | 0x50020000 | (phy_addr << 23) | (reg_num << 18) | value); | |
97 | ||
98 | return arc_mdio_complete_wait(priv); | |
99 | } | |
100 | ||
101 | /** | |
102 | * arc_mdio_probe - MDIO probe function. | |
e4f2379d AB |
103 | * @priv: Pointer to ARC EMAC private data structure. |
104 | * | |
105 | * returns: 0 on success, -ENOMEM when mdiobus_alloc | |
106 | * (to allocate memory for MII bus structure) fails. | |
107 | * | |
108 | * Sets up and registers the MDIO interface. | |
109 | */ | |
93e91b3d | 110 | int arc_mdio_probe(struct arc_emac_priv *priv) |
e4f2379d AB |
111 | { |
112 | struct mii_bus *bus; | |
113 | int error; | |
114 | ||
115 | bus = mdiobus_alloc(); | |
116 | if (!bus) | |
117 | return -ENOMEM; | |
118 | ||
119 | priv->bus = bus; | |
120 | bus->priv = priv; | |
121 | bus->parent = priv->dev; | |
122 | bus->name = "Synopsys MII Bus", | |
123 | bus->read = &arc_mdio_read; | |
124 | bus->write = &arc_mdio_write; | |
125 | ||
93e91b3d | 126 | snprintf(bus->id, MII_BUS_ID_SIZE, "%s", bus->name); |
e4f2379d | 127 | |
93e91b3d | 128 | error = of_mdiobus_register(bus, priv->dev->of_node); |
e4f2379d AB |
129 | if (error) { |
130 | dev_err(priv->dev, "cannot register MDIO bus %s\n", bus->name); | |
131 | mdiobus_free(bus); | |
132 | return error; | |
133 | } | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
138 | /** | |
139 | * arc_mdio_remove - MDIO remove function. | |
140 | * @priv: Pointer to ARC EMAC private data structure. | |
141 | * | |
142 | * Unregisters the MDIO and frees any associate memory for MII bus. | |
143 | */ | |
144 | int arc_mdio_remove(struct arc_emac_priv *priv) | |
145 | { | |
146 | mdiobus_unregister(priv->bus); | |
147 | mdiobus_free(priv->bus); | |
148 | priv->bus = NULL; | |
149 | ||
150 | return 0; | |
151 | } |