2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
7 * 2005 (c) MontaVista Software, Inc.
8 * Vitaly Bordug <vbordug@ru.mvista.com>
10 * This file is licensed under the terms of the GNU General Public License
11 * version 2. This program is licensed "as is" without any warranty of any
12 * kind, whether express or implied.
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/ptrace.h>
20 #include <linux/errno.h>
21 #include <linux/ioport.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
30 #include <linux/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/of_platform.h>
36 #include <asm/pgtable.h>
38 #include <asm/uaccess.h>
39 #include <asm/mpc5xxx.h>
44 /* Make MII read/write commands for the FEC.
46 #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
47 #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
50 #define FEC_MII_LOOPS 10000
52 static int fs_enet_fec_mii_read(struct mii_bus
*bus
, int phy_id
, int location
)
54 struct fec_info
* fec
= bus
->priv
;
55 struct fec __iomem
*fecp
= fec
->fecp
;
58 BUG_ON((in_be32(&fecp
->fec_r_cntrl
) & FEC_RCNTRL_MII_MODE
) == 0);
60 /* Add PHY address to register command. */
61 out_be32(&fecp
->fec_mii_data
, (phy_id
<< 23) | mk_mii_read(location
));
63 for (i
= 0; i
< FEC_MII_LOOPS
; i
++)
64 if ((in_be32(&fecp
->fec_ievent
) & FEC_ENET_MII
) != 0)
67 if (i
< FEC_MII_LOOPS
) {
68 out_be32(&fecp
->fec_ievent
, FEC_ENET_MII
);
69 ret
= in_be32(&fecp
->fec_mii_data
) & 0xffff;
75 static int fs_enet_fec_mii_write(struct mii_bus
*bus
, int phy_id
, int location
, u16 val
)
77 struct fec_info
* fec
= bus
->priv
;
78 struct fec __iomem
*fecp
= fec
->fecp
;
81 /* this must never happen */
82 BUG_ON((in_be32(&fecp
->fec_r_cntrl
) & FEC_RCNTRL_MII_MODE
) == 0);
84 /* Add PHY address to register command. */
85 out_be32(&fecp
->fec_mii_data
, (phy_id
<< 23) | mk_mii_write(location
, val
));
87 for (i
= 0; i
< FEC_MII_LOOPS
; i
++)
88 if ((in_be32(&fecp
->fec_ievent
) & FEC_ENET_MII
) != 0)
91 if (i
< FEC_MII_LOOPS
)
92 out_be32(&fecp
->fec_ievent
, FEC_ENET_MII
);
98 static int fs_enet_fec_mii_reset(struct mii_bus
*bus
)
100 /* nothing here - for now */
104 static int __devinit
fs_enet_mdio_probe(struct platform_device
*ofdev
,
105 const struct of_device_id
*match
)
108 struct mii_bus
*new_bus
;
109 struct fec_info
*fec
;
110 int (*get_bus_freq
)(struct device_node
*) = match
->data
;
111 int ret
= -ENOMEM
, clock
, speed
;
113 new_bus
= mdiobus_alloc();
117 fec
= kzalloc(sizeof(struct fec_info
), GFP_KERNEL
);
122 new_bus
->name
= "FEC MII Bus";
123 new_bus
->read
= &fs_enet_fec_mii_read
;
124 new_bus
->write
= &fs_enet_fec_mii_write
;
125 new_bus
->reset
= &fs_enet_fec_mii_reset
;
127 ret
= of_address_to_resource(ofdev
->dev
.of_node
, 0, &res
);
131 snprintf(new_bus
->id
, MII_BUS_ID_SIZE
, "%x", res
.start
);
133 fec
->fecp
= ioremap(res
.start
, res
.end
- res
.start
+ 1);
138 clock
= get_bus_freq(ofdev
->dev
.of_node
);
140 /* Use maximum divider if clock is unknown */
141 dev_warn(&ofdev
->dev
, "could not determine IPS clock\n");
142 clock
= 0x3F * 5000000;
145 clock
= ppc_proc_freq
;
148 * Scale for a MII clock <= 2.5 MHz
149 * Note that only 6 bits (25:30) are available for MII speed.
151 speed
= (clock
+ 4999999) / 5000000;
155 "MII clock (%d Hz) exceeds max (2.5 MHz)\n",
159 fec
->mii_speed
= speed
<< 1;
161 setbits32(&fec
->fecp
->fec_r_cntrl
, FEC_RCNTRL_MII_MODE
);
162 setbits32(&fec
->fecp
->fec_ecntrl
, FEC_ECNTRL_PINMUX
|
163 FEC_ECNTRL_ETHER_EN
);
164 out_be32(&fec
->fecp
->fec_ievent
, FEC_ENET_MII
);
165 clrsetbits_be32(&fec
->fecp
->fec_mii_speed
, 0x7E, fec
->mii_speed
);
167 new_bus
->phy_mask
= ~0;
168 new_bus
->irq
= kmalloc(sizeof(int) * PHY_MAX_ADDR
, GFP_KERNEL
);
172 new_bus
->parent
= &ofdev
->dev
;
173 dev_set_drvdata(&ofdev
->dev
, new_bus
);
175 ret
= of_mdiobus_register(new_bus
, ofdev
->dev
.of_node
);
182 dev_set_drvdata(&ofdev
->dev
, NULL
);
190 mdiobus_free(new_bus
);
195 static int fs_enet_mdio_remove(struct platform_device
*ofdev
)
197 struct mii_bus
*bus
= dev_get_drvdata(&ofdev
->dev
);
198 struct fec_info
*fec
= bus
->priv
;
200 mdiobus_unregister(bus
);
201 dev_set_drvdata(&ofdev
->dev
, NULL
);
210 static struct of_device_id fs_enet_mdio_fec_match
[] = {
212 .compatible
= "fsl,pq1-fec-mdio",
214 #if defined(CONFIG_PPC_MPC512x)
216 .compatible
= "fsl,mpc5121-fec-mdio",
217 .data
= mpc5xxx_get_bus_frequency
,
222 MODULE_DEVICE_TABLE(of
, fs_enet_mdio_fec_match
);
224 static struct of_platform_driver fs_enet_fec_mdio_driver
= {
226 .name
= "fsl-fec-mdio",
227 .owner
= THIS_MODULE
,
228 .of_match_table
= fs_enet_mdio_fec_match
,
230 .probe
= fs_enet_mdio_probe
,
231 .remove
= fs_enet_mdio_remove
,
234 static int fs_enet_mdio_fec_init(void)
236 return of_register_platform_driver(&fs_enet_fec_mdio_driver
);
239 static void fs_enet_mdio_fec_exit(void)
241 of_unregister_platform_driver(&fs_enet_fec_mdio_driver
);
244 module_init(fs_enet_mdio_fec_init
);
245 module_exit(fs_enet_mdio_fec_exit
);