blob: c838ad6155f7863cbed177f35c4416fad1c0ddb5 [file] [log] [blame]
David Daney25d967b2009-10-14 12:04:38 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
David Daney775ae9b2013-04-03 09:25:32 +00006 * Copyright (C) 2009-2012 Cavium, Inc.
David Daney25d967b2009-10-14 12:04:38 -07007 */
8
David Daney25d967b2009-10-14 12:04:38 -07009#include <linux/platform_device.h>
David Daney2fd46f42012-07-05 18:12:39 +020010#include <linux/of_mdio.h>
11#include <linux/delay.h>
12#include <linux/module.h>
David Daney2fd46f42012-07-05 18:12:39 +020013#include <linux/gfp.h>
David Daney25d967b2009-10-14 12:04:38 -070014#include <linux/phy.h>
David Daney2fd46f42012-07-05 18:12:39 +020015#include <linux/io.h>
David Daney25d967b2009-10-14 12:04:38 -070016
17#include <asm/octeon/octeon.h>
18#include <asm/octeon/cvmx-smix-defs.h>
19
20#define DRV_VERSION "1.0"
21#define DRV_DESCRIPTION "Cavium Networks Octeon SMI/MDIO driver"
22
David Daney2fd46f42012-07-05 18:12:39 +020023#define SMI_CMD 0x0
24#define SMI_WR_DAT 0x8
25#define SMI_RD_DAT 0x10
26#define SMI_CLK 0x18
27#define SMI_EN 0x20
28
David Daney775ae9b2013-04-03 09:25:32 +000029enum octeon_mdiobus_mode {
30 UNINIT = 0,
31 C22,
32 C45
33};
34
David Daney25d967b2009-10-14 12:04:38 -070035struct octeon_mdiobus {
36 struct mii_bus *mii_bus;
David Daney2fd46f42012-07-05 18:12:39 +020037 u64 register_base;
38 resource_size_t mdio_phys;
39 resource_size_t regsize;
David Daney775ae9b2013-04-03 09:25:32 +000040 enum octeon_mdiobus_mode mode;
David Daney25d967b2009-10-14 12:04:38 -070041 int phy_irq[PHY_MAX_ADDR];
42};
43
David Daney775ae9b2013-04-03 09:25:32 +000044static void octeon_mdiobus_set_mode(struct octeon_mdiobus *p,
45 enum octeon_mdiobus_mode m)
46{
47 union cvmx_smix_clk smi_clk;
48
49 if (m == p->mode)
50 return;
51
52 smi_clk.u64 = cvmx_read_csr(p->register_base + SMI_CLK);
53 smi_clk.s.mode = (m == C45) ? 1 : 0;
54 smi_clk.s.preamble = 1;
55 cvmx_write_csr(p->register_base + SMI_CLK, smi_clk.u64);
56 p->mode = m;
57}
58
59static int octeon_mdiobus_c45_addr(struct octeon_mdiobus *p,
60 int phy_id, int regnum)
61{
62 union cvmx_smix_cmd smi_cmd;
63 union cvmx_smix_wr_dat smi_wr;
64 int timeout = 1000;
65
66 octeon_mdiobus_set_mode(p, C45);
67
68 smi_wr.u64 = 0;
69 smi_wr.s.dat = regnum & 0xffff;
70 cvmx_write_csr(p->register_base + SMI_WR_DAT, smi_wr.u64);
71
72 regnum = (regnum >> 16) & 0x1f;
73
74 smi_cmd.u64 = 0;
75 smi_cmd.s.phy_op = 0; /* MDIO_CLAUSE_45_ADDRESS */
76 smi_cmd.s.phy_adr = phy_id;
77 smi_cmd.s.reg_adr = regnum;
78 cvmx_write_csr(p->register_base + SMI_CMD, smi_cmd.u64);
79
80 do {
81 /* Wait 1000 clocks so we don't saturate the RSL bus
82 * doing reads.
83 */
84 __delay(1000);
85 smi_wr.u64 = cvmx_read_csr(p->register_base + SMI_WR_DAT);
86 } while (smi_wr.s.pending && --timeout);
87
88 if (timeout <= 0)
89 return -EIO;
90 return 0;
91}
92
David Daney25d967b2009-10-14 12:04:38 -070093static int octeon_mdiobus_read(struct mii_bus *bus, int phy_id, int regnum)
94{
95 struct octeon_mdiobus *p = bus->priv;
96 union cvmx_smix_cmd smi_cmd;
97 union cvmx_smix_rd_dat smi_rd;
David Daney775ae9b2013-04-03 09:25:32 +000098 unsigned int op = 1; /* MDIO_CLAUSE_22_READ */
David Daney25d967b2009-10-14 12:04:38 -070099 int timeout = 1000;
100
David Daney775ae9b2013-04-03 09:25:32 +0000101 if (regnum & MII_ADDR_C45) {
102 int r = octeon_mdiobus_c45_addr(p, phy_id, regnum);
103 if (r < 0)
104 return r;
105
106 regnum = (regnum >> 16) & 0x1f;
107 op = 3; /* MDIO_CLAUSE_45_READ */
108 } else {
109 octeon_mdiobus_set_mode(p, C22);
110 }
111
112
David Daney25d967b2009-10-14 12:04:38 -0700113 smi_cmd.u64 = 0;
David Daney775ae9b2013-04-03 09:25:32 +0000114 smi_cmd.s.phy_op = op;
David Daney25d967b2009-10-14 12:04:38 -0700115 smi_cmd.s.phy_adr = phy_id;
116 smi_cmd.s.reg_adr = regnum;
David Daney2fd46f42012-07-05 18:12:39 +0200117 cvmx_write_csr(p->register_base + SMI_CMD, smi_cmd.u64);
David Daney25d967b2009-10-14 12:04:38 -0700118
119 do {
David Daney775ae9b2013-04-03 09:25:32 +0000120 /* Wait 1000 clocks so we don't saturate the RSL bus
David Daney25d967b2009-10-14 12:04:38 -0700121 * doing reads.
122 */
David Daney2fd46f42012-07-05 18:12:39 +0200123 __delay(1000);
124 smi_rd.u64 = cvmx_read_csr(p->register_base + SMI_RD_DAT);
David Daney25d967b2009-10-14 12:04:38 -0700125 } while (smi_rd.s.pending && --timeout);
126
127 if (smi_rd.s.val)
128 return smi_rd.s.dat;
129 else
130 return -EIO;
131}
132
133static int octeon_mdiobus_write(struct mii_bus *bus, int phy_id,
134 int regnum, u16 val)
135{
136 struct octeon_mdiobus *p = bus->priv;
137 union cvmx_smix_cmd smi_cmd;
138 union cvmx_smix_wr_dat smi_wr;
David Daney775ae9b2013-04-03 09:25:32 +0000139 unsigned int op = 0; /* MDIO_CLAUSE_22_WRITE */
David Daney25d967b2009-10-14 12:04:38 -0700140 int timeout = 1000;
141
David Daney775ae9b2013-04-03 09:25:32 +0000142
143 if (regnum & MII_ADDR_C45) {
144 int r = octeon_mdiobus_c45_addr(p, phy_id, regnum);
145 if (r < 0)
146 return r;
147
148 regnum = (regnum >> 16) & 0x1f;
149 op = 1; /* MDIO_CLAUSE_45_WRITE */
150 } else {
151 octeon_mdiobus_set_mode(p, C22);
152 }
153
David Daney25d967b2009-10-14 12:04:38 -0700154 smi_wr.u64 = 0;
155 smi_wr.s.dat = val;
David Daney2fd46f42012-07-05 18:12:39 +0200156 cvmx_write_csr(p->register_base + SMI_WR_DAT, smi_wr.u64);
David Daney25d967b2009-10-14 12:04:38 -0700157
158 smi_cmd.u64 = 0;
David Daney775ae9b2013-04-03 09:25:32 +0000159 smi_cmd.s.phy_op = op;
David Daney25d967b2009-10-14 12:04:38 -0700160 smi_cmd.s.phy_adr = phy_id;
161 smi_cmd.s.reg_adr = regnum;
David Daney2fd46f42012-07-05 18:12:39 +0200162 cvmx_write_csr(p->register_base + SMI_CMD, smi_cmd.u64);
David Daney25d967b2009-10-14 12:04:38 -0700163
164 do {
David Daney775ae9b2013-04-03 09:25:32 +0000165 /* Wait 1000 clocks so we don't saturate the RSL bus
David Daney25d967b2009-10-14 12:04:38 -0700166 * doing reads.
167 */
David Daney2fd46f42012-07-05 18:12:39 +0200168 __delay(1000);
169 smi_wr.u64 = cvmx_read_csr(p->register_base + SMI_WR_DAT);
David Daney25d967b2009-10-14 12:04:38 -0700170 } while (smi_wr.s.pending && --timeout);
171
172 if (timeout <= 0)
173 return -EIO;
174
175 return 0;
176}
177
Bill Pemberton633d1592012-12-03 09:24:14 -0500178static int octeon_mdiobus_probe(struct platform_device *pdev)
David Daney25d967b2009-10-14 12:04:38 -0700179{
180 struct octeon_mdiobus *bus;
David Daney2fd46f42012-07-05 18:12:39 +0200181 struct resource *res_mem;
David Daney6c178122010-04-01 18:17:54 -0700182 union cvmx_smix_en smi_en;
David Daney25d967b2009-10-14 12:04:38 -0700183 int err = -ENOENT;
184
185 bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
186 if (!bus)
187 return -ENOMEM;
188
David Daney2fd46f42012-07-05 18:12:39 +0200189 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
190
191 if (res_mem == NULL) {
192 dev_err(&pdev->dev, "found no memory resource\n");
193 err = -ENXIO;
194 goto fail;
195 }
196 bus->mdio_phys = res_mem->start;
197 bus->regsize = resource_size(res_mem);
198 if (!devm_request_mem_region(&pdev->dev, bus->mdio_phys, bus->regsize,
199 res_mem->name)) {
200 dev_err(&pdev->dev, "request_mem_region failed\n");
201 goto fail;
202 }
203 bus->register_base =
204 (u64)devm_ioremap(&pdev->dev, bus->mdio_phys, bus->regsize);
David Daney25d967b2009-10-14 12:04:38 -0700205
206 bus->mii_bus = mdiobus_alloc();
207
208 if (!bus->mii_bus)
David Daney2fd46f42012-07-05 18:12:39 +0200209 goto fail;
David Daney25d967b2009-10-14 12:04:38 -0700210
David Daney6c178122010-04-01 18:17:54 -0700211 smi_en.u64 = 0;
212 smi_en.s.en = 1;
David Daney2fd46f42012-07-05 18:12:39 +0200213 cvmx_write_csr(bus->register_base + SMI_EN, smi_en.u64);
David Daney25d967b2009-10-14 12:04:38 -0700214
215 bus->mii_bus->priv = bus;
216 bus->mii_bus->irq = bus->phy_irq;
217 bus->mii_bus->name = "mdio-octeon";
David Daney2fd46f42012-07-05 18:12:39 +0200218 snprintf(bus->mii_bus->id, MII_BUS_ID_SIZE, "%llx", bus->register_base);
David Daney25d967b2009-10-14 12:04:38 -0700219 bus->mii_bus->parent = &pdev->dev;
220
221 bus->mii_bus->read = octeon_mdiobus_read;
222 bus->mii_bus->write = octeon_mdiobus_write;
223
Libo Chenf8825662013-08-21 18:15:15 +0800224 platform_set_drvdata(pdev, bus);
David Daney25d967b2009-10-14 12:04:38 -0700225
David Daney2fd46f42012-07-05 18:12:39 +0200226 err = of_mdiobus_register(bus->mii_bus, pdev->dev.of_node);
David Daney25d967b2009-10-14 12:04:38 -0700227 if (err)
David Daney2fd46f42012-07-05 18:12:39 +0200228 goto fail_register;
David Daney25d967b2009-10-14 12:04:38 -0700229
230 dev_info(&pdev->dev, "Version " DRV_VERSION "\n");
231
232 return 0;
David Daney2fd46f42012-07-05 18:12:39 +0200233fail_register:
David Daney25d967b2009-10-14 12:04:38 -0700234 mdiobus_free(bus->mii_bus);
David Daney2fd46f42012-07-05 18:12:39 +0200235fail:
David Daney6c178122010-04-01 18:17:54 -0700236 smi_en.u64 = 0;
David Daney2fd46f42012-07-05 18:12:39 +0200237 cvmx_write_csr(bus->register_base + SMI_EN, smi_en.u64);
David Daney25d967b2009-10-14 12:04:38 -0700238 return err;
239}
240
Bill Pemberton633d1592012-12-03 09:24:14 -0500241static int octeon_mdiobus_remove(struct platform_device *pdev)
David Daney25d967b2009-10-14 12:04:38 -0700242{
243 struct octeon_mdiobus *bus;
David Daney6c178122010-04-01 18:17:54 -0700244 union cvmx_smix_en smi_en;
David Daney25d967b2009-10-14 12:04:38 -0700245
Jingoo Han2c0c4fb2013-09-02 17:10:09 +0900246 bus = platform_get_drvdata(pdev);
David Daney25d967b2009-10-14 12:04:38 -0700247
248 mdiobus_unregister(bus->mii_bus);
249 mdiobus_free(bus->mii_bus);
David Daney6c178122010-04-01 18:17:54 -0700250 smi_en.u64 = 0;
David Daney2fd46f42012-07-05 18:12:39 +0200251 cvmx_write_csr(bus->register_base + SMI_EN, smi_en.u64);
David Daney25d967b2009-10-14 12:04:38 -0700252 return 0;
253}
254
Fabian Frederickd8a7dad2015-03-17 19:40:23 +0100255static const struct of_device_id octeon_mdiobus_match[] = {
David Daney2fd46f42012-07-05 18:12:39 +0200256 {
257 .compatible = "cavium,octeon-3860-mdio",
258 },
259 {},
260};
261MODULE_DEVICE_TABLE(of, octeon_mdiobus_match);
262
David Daney25d967b2009-10-14 12:04:38 -0700263static struct platform_driver octeon_mdiobus_driver = {
264 .driver = {
265 .name = "mdio-octeon",
David Daney2fd46f42012-07-05 18:12:39 +0200266 .of_match_table = octeon_mdiobus_match,
David Daney25d967b2009-10-14 12:04:38 -0700267 },
268 .probe = octeon_mdiobus_probe,
Bill Pemberton633d1592012-12-03 09:24:14 -0500269 .remove = octeon_mdiobus_remove,
David Daney25d967b2009-10-14 12:04:38 -0700270};
271
272void octeon_mdiobus_force_mod_depencency(void)
273{
274 /* Let ethernet drivers force us to be loaded. */
275}
276EXPORT_SYMBOL(octeon_mdiobus_force_mod_depencency);
277
Sachin Kamat9fad0c92013-03-20 01:41:32 +0000278module_platform_driver(octeon_mdiobus_driver);
David Daney25d967b2009-10-14 12:04:38 -0700279
280MODULE_DESCRIPTION(DRV_DESCRIPTION);
281MODULE_VERSION(DRV_VERSION);
282MODULE_AUTHOR("David Daney");
283MODULE_LICENSE("GPL");