David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 1 | /* |
| 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 Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 6 | * Copyright (C) 2009-2015 Cavium, Inc. |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 9 | #include <linux/platform_device.h> |
Radha Mohan Chintakuntla | a6d6786 | 2015-07-28 15:12:11 -0700 | [diff] [blame] | 10 | #include <linux/of_address.h> |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 11 | #include <linux/of_mdio.h> |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 12 | #include <linux/module.h> |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 13 | #include <linux/gfp.h> |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 14 | #include <linux/phy.h> |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 15 | #include <linux/io.h> |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 16 | |
David Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 17 | #include "mdio-cavium.h" |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 18 | |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 19 | static int octeon_mdiobus_probe(struct platform_device *pdev) |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 20 | { |
David Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 21 | struct cavium_mdiobus *bus; |
Andrew Lunn | 35d2aea | 2016-01-06 20:11:14 +0100 | [diff] [blame] | 22 | struct mii_bus *mii_bus; |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 23 | struct resource *res_mem; |
David Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 24 | resource_size_t mdio_phys; |
| 25 | resource_size_t regsize; |
David Daney | 6c17812 | 2010-04-01 18:17:54 -0700 | [diff] [blame] | 26 | union cvmx_smix_en smi_en; |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 27 | int err = -ENOENT; |
| 28 | |
Andrew Lunn | 35d2aea | 2016-01-06 20:11:14 +0100 | [diff] [blame] | 29 | mii_bus = devm_mdiobus_alloc_size(&pdev->dev, sizeof(*bus)); |
| 30 | if (!mii_bus) |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 31 | return -ENOMEM; |
| 32 | |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 33 | res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 34 | if (res_mem == NULL) { |
| 35 | dev_err(&pdev->dev, "found no memory resource\n"); |
Radha Mohan Chintakuntla | de9e397 | 2015-07-28 15:12:12 -0700 | [diff] [blame] | 36 | return -ENXIO; |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 37 | } |
Radha Mohan Chintakuntla | de9e397 | 2015-07-28 15:12:12 -0700 | [diff] [blame] | 38 | |
Andrew Lunn | 35d2aea | 2016-01-06 20:11:14 +0100 | [diff] [blame] | 39 | bus = mii_bus->priv; |
| 40 | bus->mii_bus = mii_bus; |
David Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 41 | mdio_phys = res_mem->start; |
| 42 | regsize = resource_size(res_mem); |
Radha Mohan Chintakuntla | de9e397 | 2015-07-28 15:12:12 -0700 | [diff] [blame] | 43 | |
David Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 44 | if (!devm_request_mem_region(&pdev->dev, mdio_phys, regsize, |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 45 | res_mem->name)) { |
| 46 | dev_err(&pdev->dev, "request_mem_region failed\n"); |
Radha Mohan Chintakuntla | de9e397 | 2015-07-28 15:12:12 -0700 | [diff] [blame] | 47 | return -ENXIO; |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 48 | } |
Radha Mohan Chintakuntla | de9e397 | 2015-07-28 15:12:12 -0700 | [diff] [blame] | 49 | |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 50 | bus->register_base = |
David Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 51 | (u64)devm_ioremap(&pdev->dev, mdio_phys, regsize); |
Radha Mohan Chintakuntla | de9e397 | 2015-07-28 15:12:12 -0700 | [diff] [blame] | 52 | if (!bus->register_base) { |
| 53 | dev_err(&pdev->dev, "dev_ioremap failed\n"); |
| 54 | return -ENOMEM; |
| 55 | } |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 56 | |
David Daney | 6c17812 | 2010-04-01 18:17:54 -0700 | [diff] [blame] | 57 | smi_en.u64 = 0; |
| 58 | smi_en.s.en = 1; |
Radha Mohan Chintakuntla | a6d6786 | 2015-07-28 15:12:11 -0700 | [diff] [blame] | 59 | oct_mdio_writeq(smi_en.u64, bus->register_base + SMI_EN); |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 60 | |
David Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 61 | bus->mii_bus->name = KBUILD_MODNAME; |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 62 | snprintf(bus->mii_bus->id, MII_BUS_ID_SIZE, "%llx", bus->register_base); |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 63 | bus->mii_bus->parent = &pdev->dev; |
| 64 | |
David Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 65 | bus->mii_bus->read = cavium_mdiobus_read; |
| 66 | bus->mii_bus->write = cavium_mdiobus_write; |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 67 | |
Libo Chen | f882566 | 2013-08-21 18:15:15 +0800 | [diff] [blame] | 68 | platform_set_drvdata(pdev, bus); |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 69 | |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 70 | err = of_mdiobus_register(bus->mii_bus, pdev->dev.of_node); |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 71 | if (err) |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 72 | goto fail_register; |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 73 | |
David Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 74 | dev_info(&pdev->dev, "Probed\n"); |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 75 | |
| 76 | return 0; |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 77 | fail_register: |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 78 | mdiobus_free(bus->mii_bus); |
David Daney | 6c17812 | 2010-04-01 18:17:54 -0700 | [diff] [blame] | 79 | smi_en.u64 = 0; |
Radha Mohan Chintakuntla | a6d6786 | 2015-07-28 15:12:11 -0700 | [diff] [blame] | 80 | oct_mdio_writeq(smi_en.u64, bus->register_base + SMI_EN); |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 81 | return err; |
| 82 | } |
| 83 | |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 84 | static int octeon_mdiobus_remove(struct platform_device *pdev) |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 85 | { |
David Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 86 | struct cavium_mdiobus *bus; |
David Daney | 6c17812 | 2010-04-01 18:17:54 -0700 | [diff] [blame] | 87 | union cvmx_smix_en smi_en; |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 88 | |
Jingoo Han | 2c0c4fb | 2013-09-02 17:10:09 +0900 | [diff] [blame] | 89 | bus = platform_get_drvdata(pdev); |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 90 | |
| 91 | mdiobus_unregister(bus->mii_bus); |
| 92 | mdiobus_free(bus->mii_bus); |
David Daney | 6c17812 | 2010-04-01 18:17:54 -0700 | [diff] [blame] | 93 | smi_en.u64 = 0; |
Radha Mohan Chintakuntla | a6d6786 | 2015-07-28 15:12:11 -0700 | [diff] [blame] | 94 | oct_mdio_writeq(smi_en.u64, bus->register_base + SMI_EN); |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
Fabian Frederick | d8a7dad | 2015-03-17 19:40:23 +0100 | [diff] [blame] | 98 | static const struct of_device_id octeon_mdiobus_match[] = { |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 99 | { |
| 100 | .compatible = "cavium,octeon-3860-mdio", |
| 101 | }, |
| 102 | {}, |
| 103 | }; |
| 104 | MODULE_DEVICE_TABLE(of, octeon_mdiobus_match); |
| 105 | |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 106 | static struct platform_driver octeon_mdiobus_driver = { |
| 107 | .driver = { |
David Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 108 | .name = KBUILD_MODNAME, |
David Daney | 2fd46f4 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 109 | .of_match_table = octeon_mdiobus_match, |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 110 | }, |
| 111 | .probe = octeon_mdiobus_probe, |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 112 | .remove = octeon_mdiobus_remove, |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | void octeon_mdiobus_force_mod_depencency(void) |
| 116 | { |
| 117 | /* Let ethernet drivers force us to be loaded. */ |
| 118 | } |
| 119 | EXPORT_SYMBOL(octeon_mdiobus_force_mod_depencency); |
| 120 | |
Sachin Kamat | 9fad0c9 | 2013-03-20 01:41:32 +0000 | [diff] [blame] | 121 | module_platform_driver(octeon_mdiobus_driver); |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 122 | |
David Daney | 1eefee9 | 2016-03-11 09:53:10 -0800 | [diff] [blame] | 123 | MODULE_DESCRIPTION("Cavium OCTEON MDIO bus driver"); |
David Daney | 25d967b | 2009-10-14 12:04:38 -0700 | [diff] [blame] | 124 | MODULE_AUTHOR("David Daney"); |
| 125 | MODULE_LICENSE("GPL"); |