blob: f3c63dce1e30223813ce9637cc89f703e53d7a35 [file] [log] [blame]
Andy Fleming1577ece2009-02-04 16:42:12 -08001/*
2 * Freescale PowerQUICC Ethernet Driver -- MIIM bus implementation
3 * Provides Bus interface for MIIM regs
4 *
5 * Author: Andy Fleming <afleming@freescale.com>
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +00006 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
Andy Fleming1577ece2009-02-04 16:42:12 -08007 *
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +00008 * Copyright 2002-2004, 2008-2009 Freescale Semiconductor, Inc.
Andy Fleming1577ece2009-02-04 16:42:12 -08009 *
10 * Based on gianfar_mii.c and ucc_geth_mii.c (Li Yang, Kim Phillips)
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/string.h>
21#include <linux/errno.h>
Andy Fleming1577ece2009-02-04 16:42:12 -080022#include <linux/slab.h>
Andy Fleming1577ece2009-02-04 16:42:12 -080023#include <linux/delay.h>
Andy Fleming1577ece2009-02-04 16:42:12 -080024#include <linux/module.h>
Andy Fleming1577ece2009-02-04 16:42:12 -080025#include <linux/mii.h>
Grant Likely22ae7822010-07-29 11:49:01 -060026#include <linux/of_address.h>
Grant Likely324931b2009-04-25 12:53:07 +000027#include <linux/of_mdio.h>
Timur Tabiafae5ad2012-08-29 08:08:01 +000028#include <linux/of_device.h>
Andy Fleming1577ece2009-02-04 16:42:12 -080029
30#include <asm/io.h>
Claudiu Manoil9a4cbd52014-10-07 10:44:28 +030031#if IS_ENABLED(CONFIG_UCC_GETH)
Zhao Qiang7aa1aa62015-11-30 10:48:57 +080032#include <soc/fsl/qe/ucc.h>
Claudiu Manoil9a4cbd52014-10-07 10:44:28 +030033#endif
Andy Fleming1577ece2009-02-04 16:42:12 -080034
35#include "gianfar.h"
Timur Tabi19bcd6c2012-08-29 08:07:57 +000036
37#define MIIMIND_BUSY 0x00000001
38#define MIIMIND_NOTVALID 0x00000004
39#define MIIMCFG_INIT_VALUE 0x00000007
40#define MIIMCFG_RESET 0x80000000
41
42#define MII_READ_COMMAND 0x00000001
43
Timur Tabiafae5ad2012-08-29 08:08:01 +000044struct fsl_pq_mii {
45 u32 miimcfg; /* MII management configuration reg */
46 u32 miimcom; /* MII management command reg */
47 u32 miimadd; /* MII management address reg */
48 u32 miimcon; /* MII management control reg */
49 u32 miimstat; /* MII management status reg */
50 u32 miimind; /* MII management indication reg */
51};
52
Timur Tabi19bcd6c2012-08-29 08:07:57 +000053struct fsl_pq_mdio {
54 u8 res1[16];
55 u32 ieventm; /* MDIO Interrupt event register (for etsec2)*/
56 u32 imaskm; /* MDIO Interrupt mask register (for etsec2)*/
57 u8 res2[4];
58 u32 emapm; /* MDIO Event mapping register (for etsec2)*/
59 u8 res3[1280];
Timur Tabiafae5ad2012-08-29 08:08:01 +000060 struct fsl_pq_mii mii;
Timur Tabi19bcd6c2012-08-29 08:07:57 +000061 u8 res4[28];
62 u32 utbipar; /* TBI phy address reg (only on UCC) */
63 u8 res5[2728];
64} __packed;
Andy Fleming1577ece2009-02-04 16:42:12 -080065
Timur Tabi59399c52012-07-09 16:57:36 -050066/* Number of microseconds to wait for an MII register to respond */
67#define MII_TIMEOUT 1000
68
Anton Vorontsovb3319b12009-12-30 08:23:34 +000069struct fsl_pq_mdio_priv {
70 void __iomem *map;
Timur Tabiafae5ad2012-08-29 08:08:01 +000071 struct fsl_pq_mii __iomem *regs;
72};
73
74/*
75 * Per-device-type data. Each type of device tree node that we support gets
76 * one of these.
77 *
78 * @mii_offset: the offset of the MII registers within the memory map of the
79 * node. Some nodes define only the MII registers, and some define the whole
80 * MAC (which includes the MII registers).
81 *
82 * @get_tbipa: determines the address of the TBIPA register
83 *
84 * @ucc_configure: a special function for extra QE configuration
85 */
86struct fsl_pq_mdio_data {
87 unsigned int mii_offset; /* offset of the MII registers */
88 uint32_t __iomem * (*get_tbipa)(void __iomem *p);
89 void (*ucc_configure)(phys_addr_t start, phys_addr_t end);
Anton Vorontsovb3319b12009-12-30 08:23:34 +000090};
91
Andy Fleming1577ece2009-02-04 16:42:12 -080092/*
Timur Tabi69cfb412012-08-29 08:07:59 +000093 * Write value to the PHY at mii_id at register regnum, on the bus attached
94 * to the local interface, which may be different from the generic mdio bus
95 * (tied to a single interface), waiting until the write is done before
96 * returning. This is helpful in programming interfaces like the TBI which
97 * control interfaces like onchip SERDES and are always tied to the local
98 * mdio pins, which may not be the same as system mdio bus, used for
Andy Fleming1577ece2009-02-04 16:42:12 -080099 * controlling the external PHYs, for example.
100 */
Timur Tabi69cfb412012-08-29 08:07:59 +0000101static int fsl_pq_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
102 u16 value)
Andy Fleming1577ece2009-02-04 16:42:12 -0800103{
Timur Tabi69cfb412012-08-29 08:07:59 +0000104 struct fsl_pq_mdio_priv *priv = bus->priv;
Timur Tabiafae5ad2012-08-29 08:08:01 +0000105 struct fsl_pq_mii __iomem *regs = priv->regs;
Claudiu Manoile4b081f2014-10-07 10:44:30 +0300106 unsigned int timeout;
Timur Tabi59399c52012-07-09 16:57:36 -0500107
Andy Fleming1577ece2009-02-04 16:42:12 -0800108 /* Set the PHY address and the register address we want to write */
Claudiu Manoilf5bbd262014-10-07 10:44:29 +0300109 iowrite32be((mii_id << 8) | regnum, &regs->miimadd);
Andy Fleming1577ece2009-02-04 16:42:12 -0800110
111 /* Write out the value we want */
Claudiu Manoilf5bbd262014-10-07 10:44:29 +0300112 iowrite32be(value, &regs->miimcon);
Andy Fleming1577ece2009-02-04 16:42:12 -0800113
114 /* Wait for the transaction to finish */
Claudiu Manoile4b081f2014-10-07 10:44:30 +0300115 timeout = MII_TIMEOUT;
116 while ((ioread32be(&regs->miimind) & MIIMIND_BUSY) && timeout) {
117 cpu_relax();
118 timeout--;
119 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800120
Claudiu Manoile4b081f2014-10-07 10:44:30 +0300121 return timeout ? 0 : -ETIMEDOUT;
Andy Fleming1577ece2009-02-04 16:42:12 -0800122}
123
124/*
Timur Tabi69cfb412012-08-29 08:07:59 +0000125 * Read the bus for PHY at addr mii_id, register regnum, and return the value.
126 * Clears miimcom first.
127 *
128 * All PHY operation done on the bus attached to the local interface, which
129 * may be different from the generic mdio bus. This is helpful in programming
130 * interfaces like the TBI which, in turn, control interfaces like on-chip
131 * SERDES and are always tied to the local mdio pins, which may not be the
Andy Fleming1577ece2009-02-04 16:42:12 -0800132 * same as system mdio bus, used for controlling the external PHYs, for eg.
133 */
Timur Tabi69cfb412012-08-29 08:07:59 +0000134static int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Andy Fleming1577ece2009-02-04 16:42:12 -0800135{
Timur Tabi69cfb412012-08-29 08:07:59 +0000136 struct fsl_pq_mdio_priv *priv = bus->priv;
Timur Tabiafae5ad2012-08-29 08:08:01 +0000137 struct fsl_pq_mii __iomem *regs = priv->regs;
Claudiu Manoile4b081f2014-10-07 10:44:30 +0300138 unsigned int timeout;
Timur Tabi69cfb412012-08-29 08:07:59 +0000139 u16 value;
Andy Fleming1577ece2009-02-04 16:42:12 -0800140
141 /* Set the PHY address and the register address we want to read */
Claudiu Manoilf5bbd262014-10-07 10:44:29 +0300142 iowrite32be((mii_id << 8) | regnum, &regs->miimadd);
Andy Fleming1577ece2009-02-04 16:42:12 -0800143
144 /* Clear miimcom, and then initiate a read */
Claudiu Manoilf5bbd262014-10-07 10:44:29 +0300145 iowrite32be(0, &regs->miimcom);
146 iowrite32be(MII_READ_COMMAND, &regs->miimcom);
Andy Fleming1577ece2009-02-04 16:42:12 -0800147
Timur Tabi59399c52012-07-09 16:57:36 -0500148 /* Wait for the transaction to finish, normally less than 100us */
Claudiu Manoile4b081f2014-10-07 10:44:30 +0300149 timeout = MII_TIMEOUT;
150 while ((ioread32be(&regs->miimind) &
151 (MIIMIND_NOTVALID | MIIMIND_BUSY)) && timeout) {
152 cpu_relax();
153 timeout--;
154 }
155
156 if (!timeout)
Timur Tabi59399c52012-07-09 16:57:36 -0500157 return -ETIMEDOUT;
Andy Fleming1577ece2009-02-04 16:42:12 -0800158
159 /* Grab the value of the register from miimstat */
Claudiu Manoilf5bbd262014-10-07 10:44:29 +0300160 value = ioread32be(&regs->miimstat);
Andy Fleming1577ece2009-02-04 16:42:12 -0800161
Timur Tabiafae5ad2012-08-29 08:08:01 +0000162 dev_dbg(&bus->dev, "read %04x from address %x/%x\n", value, mii_id, regnum);
Andy Fleming1577ece2009-02-04 16:42:12 -0800163 return value;
164}
165
Andy Fleming1577ece2009-02-04 16:42:12 -0800166/* Reset the MIIM registers, and wait for the bus to free */
167static int fsl_pq_mdio_reset(struct mii_bus *bus)
168{
Timur Tabi69cfb412012-08-29 08:07:59 +0000169 struct fsl_pq_mdio_priv *priv = bus->priv;
Timur Tabiafae5ad2012-08-29 08:08:01 +0000170 struct fsl_pq_mii __iomem *regs = priv->regs;
Claudiu Manoile4b081f2014-10-07 10:44:30 +0300171 unsigned int timeout;
Andy Fleming1577ece2009-02-04 16:42:12 -0800172
173 mutex_lock(&bus->mdio_lock);
174
175 /* Reset the management interface */
Claudiu Manoilf5bbd262014-10-07 10:44:29 +0300176 iowrite32be(MIIMCFG_RESET, &regs->miimcfg);
Andy Fleming1577ece2009-02-04 16:42:12 -0800177
178 /* Setup the MII Mgmt clock speed */
Claudiu Manoilf5bbd262014-10-07 10:44:29 +0300179 iowrite32be(MIIMCFG_INIT_VALUE, &regs->miimcfg);
Andy Fleming1577ece2009-02-04 16:42:12 -0800180
181 /* Wait until the bus is free */
Claudiu Manoile4b081f2014-10-07 10:44:30 +0300182 timeout = MII_TIMEOUT;
183 while ((ioread32be(&regs->miimind) & MIIMIND_BUSY) && timeout) {
184 cpu_relax();
185 timeout--;
186 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800187
188 mutex_unlock(&bus->mdio_lock);
189
Claudiu Manoile4b081f2014-10-07 10:44:30 +0300190 if (!timeout) {
Timur Tabi5078ac72012-08-29 08:08:00 +0000191 dev_err(&bus->dev, "timeout waiting for MII bus\n");
Andy Fleming1577ece2009-02-04 16:42:12 -0800192 return -EBUSY;
193 }
194
195 return 0;
196}
197
Andy Fleming952c5ca2011-11-11 05:10:39 +0000198#if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
Timur Tabiafae5ad2012-08-29 08:08:01 +0000199/*
Gerlando Falauto3bb35ac2015-10-12 09:18:41 +0200200 * Return the TBIPA address, starting from the address
201 * of the mapped GFAR MDIO registers (struct gfar)
Timur Tabiafae5ad2012-08-29 08:08:01 +0000202 * This is mildly evil, but so is our hardware for doing this.
203 * Also, we have to cast back to struct gfar because of
204 * definition weirdness done in gianfar.h.
205 */
Gerlando Falauto3bb35ac2015-10-12 09:18:41 +0200206static uint32_t __iomem *get_gfar_tbipa_from_mdio(void __iomem *p)
Timur Tabiafae5ad2012-08-29 08:08:01 +0000207{
208 struct gfar __iomem *enet_regs = p;
Andy Fleming1577ece2009-02-04 16:42:12 -0800209
Timur Tabiafae5ad2012-08-29 08:08:01 +0000210 return &enet_regs->tbipa;
Andy Fleming952c5ca2011-11-11 05:10:39 +0000211}
Andy Fleming1577ece2009-02-04 16:42:12 -0800212
Timur Tabiafae5ad2012-08-29 08:08:01 +0000213/*
Gerlando Falauto3bb35ac2015-10-12 09:18:41 +0200214 * Return the TBIPA address, starting from the address
215 * of the mapped GFAR MII registers (gfar_mii_regs[] within struct gfar)
216 */
217static uint32_t __iomem *get_gfar_tbipa_from_mii(void __iomem *p)
218{
219 return get_gfar_tbipa_from_mdio(container_of(p, struct gfar, gfar_mii_regs));
220}
221
222/*
Timur Tabiafae5ad2012-08-29 08:08:01 +0000223 * Return the TBIPAR address for an eTSEC2 node
224 */
225static uint32_t __iomem *get_etsec_tbipa(void __iomem *p)
Andy Fleming1577ece2009-02-04 16:42:12 -0800226{
Timur Tabiafae5ad2012-08-29 08:08:01 +0000227 return p;
228}
229#endif
230
Andy Fleming952c5ca2011-11-11 05:10:39 +0000231#if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
Timur Tabiafae5ad2012-08-29 08:08:01 +0000232/*
Gerlando Falauto3bb35ac2015-10-12 09:18:41 +0200233 * Return the TBIPAR address for a QE MDIO node, starting from the address
234 * of the mapped MII registers (struct fsl_pq_mii)
Timur Tabiafae5ad2012-08-29 08:08:01 +0000235 */
236static uint32_t __iomem *get_ucc_tbipa(void __iomem *p)
237{
Gerlando Falauto3bb35ac2015-10-12 09:18:41 +0200238 struct fsl_pq_mdio __iomem *mdio = container_of(p, struct fsl_pq_mdio, mii);
Timur Tabiafae5ad2012-08-29 08:08:01 +0000239
240 return &mdio->utbipar;
241}
242
243/*
244 * Find the UCC node that controls the given MDIO node
245 *
246 * For some reason, the QE MDIO nodes are not children of the UCC devices
247 * that control them. Therefore, we need to scan all UCC nodes looking for
248 * the one that encompases the given MDIO node. We do this by comparing
249 * physical addresses. The 'start' and 'end' addresses of the MDIO node are
250 * passed, and the correct UCC node will cover the entire address range.
251 *
252 * This assumes that there is only one QE MDIO node in the entire device tree.
253 */
254static void ucc_configure(phys_addr_t start, phys_addr_t end)
255{
256 static bool found_mii_master;
Andy Fleming1577ece2009-02-04 16:42:12 -0800257 struct device_node *np = NULL;
Timur Tabiafae5ad2012-08-29 08:08:01 +0000258
259 if (found_mii_master)
260 return;
Andy Fleming1577ece2009-02-04 16:42:12 -0800261
262 for_each_compatible_node(np, NULL, "ucc_geth") {
Timur Tabiafae5ad2012-08-29 08:08:01 +0000263 struct resource res;
264 const uint32_t *iprop;
265 uint32_t id;
266 int ret;
Andy Fleming1577ece2009-02-04 16:42:12 -0800267
Timur Tabiafae5ad2012-08-29 08:08:01 +0000268 ret = of_address_to_resource(np, 0, &res);
269 if (ret < 0) {
270 pr_debug("fsl-pq-mdio: no address range in node %s\n",
271 np->full_name);
Andy Fleming1577ece2009-02-04 16:42:12 -0800272 continue;
Timur Tabiafae5ad2012-08-29 08:08:01 +0000273 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800274
275 /* if our mdio regs fall within this UCC regs range */
Timur Tabiafae5ad2012-08-29 08:08:01 +0000276 if ((start < res.start) || (end > res.end))
277 continue;
Andy Fleming1577ece2009-02-04 16:42:12 -0800278
Timur Tabiafae5ad2012-08-29 08:08:01 +0000279 iprop = of_get_property(np, "cell-index", NULL);
280 if (!iprop) {
281 iprop = of_get_property(np, "device-id", NULL);
282 if (!iprop) {
283 pr_debug("fsl-pq-mdio: no UCC ID in node %s\n",
284 np->full_name);
285 continue;
Andy Fleming1577ece2009-02-04 16:42:12 -0800286 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800287 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800288
Timur Tabiafae5ad2012-08-29 08:08:01 +0000289 id = be32_to_cpup(iprop);
290
291 /*
292 * cell-index and device-id for QE nodes are
293 * numbered from 1, not 0.
294 */
295 if (ucc_set_qe_mux_mii_mng(id - 1) < 0) {
296 pr_debug("fsl-pq-mdio: invalid UCC ID in node %s\n",
297 np->full_name);
298 continue;
299 }
300
301 pr_debug("fsl-pq-mdio: setting node UCC%u to MII master\n", id);
302 found_mii_master = true;
303 }
Andy Fleming952c5ca2011-11-11 05:10:39 +0000304}
Andy Fleming1577ece2009-02-04 16:42:12 -0800305
Timur Tabiafae5ad2012-08-29 08:08:01 +0000306#endif
307
Fabian Frederick94e5a2a2015-03-17 19:37:34 +0100308static const struct of_device_id fsl_pq_mdio_match[] = {
Timur Tabiafae5ad2012-08-29 08:08:01 +0000309#if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
310 {
311 .compatible = "fsl,gianfar-tbi",
312 .data = &(struct fsl_pq_mdio_data) {
313 .mii_offset = 0,
Gerlando Falauto3bb35ac2015-10-12 09:18:41 +0200314 .get_tbipa = get_gfar_tbipa_from_mii,
Timur Tabiafae5ad2012-08-29 08:08:01 +0000315 },
316 },
317 {
318 .compatible = "fsl,gianfar-mdio",
319 .data = &(struct fsl_pq_mdio_data) {
320 .mii_offset = 0,
Gerlando Falauto3bb35ac2015-10-12 09:18:41 +0200321 .get_tbipa = get_gfar_tbipa_from_mii,
Timur Tabiafae5ad2012-08-29 08:08:01 +0000322 },
323 },
324 {
325 .type = "mdio",
326 .compatible = "gianfar",
327 .data = &(struct fsl_pq_mdio_data) {
328 .mii_offset = offsetof(struct fsl_pq_mdio, mii),
Gerlando Falauto3bb35ac2015-10-12 09:18:41 +0200329 .get_tbipa = get_gfar_tbipa_from_mdio,
Timur Tabiafae5ad2012-08-29 08:08:01 +0000330 },
331 },
332 {
333 .compatible = "fsl,etsec2-tbi",
334 .data = &(struct fsl_pq_mdio_data) {
335 .mii_offset = offsetof(struct fsl_pq_mdio, mii),
336 .get_tbipa = get_etsec_tbipa,
337 },
338 },
339 {
340 .compatible = "fsl,etsec2-mdio",
341 .data = &(struct fsl_pq_mdio_data) {
342 .mii_offset = offsetof(struct fsl_pq_mdio, mii),
343 .get_tbipa = get_etsec_tbipa,
344 },
345 },
346#endif
347#if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
348 {
349 .compatible = "fsl,ucc-mdio",
350 .data = &(struct fsl_pq_mdio_data) {
351 .mii_offset = 0,
352 .get_tbipa = get_ucc_tbipa,
353 .ucc_configure = ucc_configure,
354 },
355 },
356 {
357 /* Legacy UCC MDIO node */
358 .type = "mdio",
359 .compatible = "ucc_geth_phy",
360 .data = &(struct fsl_pq_mdio_data) {
361 .mii_offset = 0,
362 .get_tbipa = get_ucc_tbipa,
363 .ucc_configure = ucc_configure,
364 },
365 },
366#endif
Timur Tabi761743e2012-08-29 08:08:03 +0000367 /* No Kconfig option for Fman support yet */
368 {
369 .compatible = "fsl,fman-mdio",
370 .data = &(struct fsl_pq_mdio_data) {
371 .mii_offset = 0,
372 /* Fman TBI operations are handled elsewhere */
373 },
374 },
375
Timur Tabiafae5ad2012-08-29 08:08:01 +0000376 {},
377};
378MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
379
Timur Tabi5078ac72012-08-29 08:08:00 +0000380static int fsl_pq_mdio_probe(struct platform_device *pdev)
Andy Fleming1577ece2009-02-04 16:42:12 -0800381{
Timur Tabiafae5ad2012-08-29 08:08:01 +0000382 const struct of_device_id *id =
383 of_match_device(fsl_pq_mdio_match, &pdev->dev);
384 const struct fsl_pq_mdio_data *data = id->data;
Timur Tabi5078ac72012-08-29 08:08:00 +0000385 struct device_node *np = pdev->dev.of_node;
Timur Tabiafae5ad2012-08-29 08:08:01 +0000386 struct resource res;
Andy Fleming1577ece2009-02-04 16:42:12 -0800387 struct device_node *tbi;
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000388 struct fsl_pq_mdio_priv *priv;
Andy Fleming1577ece2009-02-04 16:42:12 -0800389 struct mii_bus *new_bus;
Anton Vorontsov08d18f32010-05-14 04:27:30 +0000390 int err;
Andy Fleming1577ece2009-02-04 16:42:12 -0800391
Timur Tabiafae5ad2012-08-29 08:08:01 +0000392 dev_dbg(&pdev->dev, "found %s compatible node\n", id->compatible);
393
Timur Tabidd3b8a32012-08-29 08:08:02 +0000394 new_bus = mdiobus_alloc_size(sizeof(*priv));
395 if (!new_bus)
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000396 return -ENOMEM;
397
Timur Tabidd3b8a32012-08-29 08:08:02 +0000398 priv = new_bus->priv;
Andy Fleming1577ece2009-02-04 16:42:12 -0800399 new_bus->name = "Freescale PowerQUICC MII Bus",
Timur Tabi5078ac72012-08-29 08:08:00 +0000400 new_bus->read = &fsl_pq_mdio_read;
401 new_bus->write = &fsl_pq_mdio_write;
402 new_bus->reset = &fsl_pq_mdio_reset;
Andy Fleming1577ece2009-02-04 16:42:12 -0800403
Timur Tabiafae5ad2012-08-29 08:08:01 +0000404 err = of_address_to_resource(np, 0, &res);
405 if (err < 0) {
406 dev_err(&pdev->dev, "could not obtain address information\n");
Timur Tabidd3b8a32012-08-29 08:08:02 +0000407 goto error;
Anton Vorontsov3b1fd3e2010-04-23 07:12:35 +0000408 }
409
Timur Tabi69cfb412012-08-29 08:07:59 +0000410 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s@%llx", np->name,
Timur Tabiafae5ad2012-08-29 08:08:01 +0000411 (unsigned long long)res.start);
Timur Tabi69cfb412012-08-29 08:07:59 +0000412
Timur Tabiafae5ad2012-08-29 08:08:01 +0000413 priv->map = of_iomap(np, 0);
414 if (!priv->map) {
Andy Fleming1577ece2009-02-04 16:42:12 -0800415 err = -ENOMEM;
Timur Tabidd3b8a32012-08-29 08:08:02 +0000416 goto error;
Andy Fleming1577ece2009-02-04 16:42:12 -0800417 }
418
Timur Tabiafae5ad2012-08-29 08:08:01 +0000419 /*
420 * Some device tree nodes represent only the MII registers, and
421 * others represent the MAC and MII registers. The 'mii_offset' field
422 * contains the offset of the MII registers inside the mapped register
423 * space.
424 */
425 if (data->mii_offset > resource_size(&res)) {
426 dev_err(&pdev->dev, "invalid register map\n");
427 err = -EINVAL;
Timur Tabidd3b8a32012-08-29 08:08:02 +0000428 goto error;
Timur Tabiafae5ad2012-08-29 08:08:01 +0000429 }
430 priv->regs = priv->map + data->mii_offset;
Andy Fleming1577ece2009-02-04 16:42:12 -0800431
Timur Tabi5078ac72012-08-29 08:08:00 +0000432 new_bus->parent = &pdev->dev;
Libo Chena0e18602013-08-19 19:58:40 +0800433 platform_set_drvdata(pdev, new_bus);
Andy Fleming1577ece2009-02-04 16:42:12 -0800434
Timur Tabiafae5ad2012-08-29 08:08:01 +0000435 if (data->get_tbipa) {
436 for_each_child_of_node(np, tbi) {
437 if (strcmp(tbi->type, "tbi-phy") == 0) {
438 dev_dbg(&pdev->dev, "found TBI PHY node %s\n",
439 strrchr(tbi->full_name, '/') + 1);
440 break;
441 }
Sandeep Gopalpet1d2397d2009-11-02 07:03:22 +0000442 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800443
Timur Tabiafae5ad2012-08-29 08:08:01 +0000444 if (tbi) {
445 const u32 *prop = of_get_property(tbi, "reg", NULL);
446 uint32_t __iomem *tbipa;
Andy Fleming1577ece2009-02-04 16:42:12 -0800447
Timur Tabiafae5ad2012-08-29 08:08:01 +0000448 if (!prop) {
449 dev_err(&pdev->dev,
450 "missing 'reg' property in node %s\n",
451 tbi->full_name);
452 err = -EBUSY;
453 goto error;
454 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800455
Timur Tabiafae5ad2012-08-29 08:08:01 +0000456 tbipa = data->get_tbipa(priv->map);
Andy Fleming1577ece2009-02-04 16:42:12 -0800457
Gerlando Falauto3dd03e52015-10-12 09:18:40 +0200458 /*
459 * Add consistency check to make sure TBI is contained
460 * within the mapped range (not because we would get a
461 * segfault, rather to catch bugs in computing TBI
462 * address). Print error message but continue anyway.
463 */
464 if ((void *)tbipa > priv->map + resource_size(&res) - 4)
Arnd Bergmann8cde3e42015-12-08 16:17:29 +0100465 dev_err(&pdev->dev, "invalid register map (should be at least 0x%04zx to contain TBI address)\n",
Gerlando Falauto3dd03e52015-10-12 09:18:40 +0200466 ((void *)tbipa - priv->map) + 4);
467
Claudiu Manoilf5bbd262014-10-07 10:44:29 +0300468 iowrite32be(be32_to_cpup(prop), tbipa);
Kenth Eriksson464b57d2012-03-27 22:05:54 +0000469 }
Andy Fleming1577ece2009-02-04 16:42:12 -0800470 }
471
Timur Tabiafae5ad2012-08-29 08:08:01 +0000472 if (data->ucc_configure)
473 data->ucc_configure(res.start, res.end);
474
Grant Likely324931b2009-04-25 12:53:07 +0000475 err = of_mdiobus_register(new_bus, np);
Andy Fleming1577ece2009-02-04 16:42:12 -0800476 if (err) {
Timur Tabi5078ac72012-08-29 08:08:00 +0000477 dev_err(&pdev->dev, "cannot register %s as MDIO bus\n",
478 new_bus->name);
Timur Tabidd3b8a32012-08-29 08:08:02 +0000479 goto error;
Andy Fleming1577ece2009-02-04 16:42:12 -0800480 }
481
482 return 0;
483
Timur Tabidd3b8a32012-08-29 08:08:02 +0000484error:
485 if (priv->map)
486 iounmap(priv->map);
487
Andy Fleming1577ece2009-02-04 16:42:12 -0800488 kfree(new_bus);
Timur Tabidd3b8a32012-08-29 08:08:02 +0000489
Andy Fleming1577ece2009-02-04 16:42:12 -0800490 return err;
491}
492
493
Timur Tabi5078ac72012-08-29 08:08:00 +0000494static int fsl_pq_mdio_remove(struct platform_device *pdev)
Andy Fleming1577ece2009-02-04 16:42:12 -0800495{
Timur Tabi5078ac72012-08-29 08:08:00 +0000496 struct device *device = &pdev->dev;
Andy Fleming1577ece2009-02-04 16:42:12 -0800497 struct mii_bus *bus = dev_get_drvdata(device);
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000498 struct fsl_pq_mdio_priv *priv = bus->priv;
Andy Fleming1577ece2009-02-04 16:42:12 -0800499
500 mdiobus_unregister(bus);
501
Anton Vorontsovb3319b12009-12-30 08:23:34 +0000502 iounmap(priv->map);
Andy Fleming1577ece2009-02-04 16:42:12 -0800503 mdiobus_free(bus);
504
505 return 0;
506}
507
Grant Likely74888762011-02-22 21:05:51 -0700508static struct platform_driver fsl_pq_mdio_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700509 .driver = {
510 .name = "fsl-pq_mdio",
Grant Likely40182942010-04-13 16:13:02 -0700511 .of_match_table = fsl_pq_mdio_match,
512 },
Andy Fleming1577ece2009-02-04 16:42:12 -0800513 .probe = fsl_pq_mdio_probe,
514 .remove = fsl_pq_mdio_remove,
Andy Fleming1577ece2009-02-04 16:42:12 -0800515};
516
Axel Lindb62f682011-11-27 16:44:17 +0000517module_platform_driver(fsl_pq_mdio_driver);
Andy Fleming1577ece2009-02-04 16:42:12 -0800518
Sebastian Siewior26062892009-11-06 08:50:28 +0000519MODULE_LICENSE("GPL");