Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Allwinner EMAC MDIO interface driver |
| 3 | * |
| 4 | * Copyright 2012-2013 Stefan Roese <sr@denx.de> |
| 5 | * Copyright 2013 Maxime Ripard <maxime.ripard@free-electrons.com> |
| 6 | * |
| 7 | * Based on the Linux driver provided by Allwinner: |
| 8 | * Copyright (C) 1997 Sten Wang |
| 9 | * |
| 10 | * This file is licensed under the terms of the GNU General Public |
| 11 | * License version 2. This program is licensed "as is" without any |
| 12 | * warranty of any kind, whether express or implied. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/delay.h> |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/of_address.h> |
| 20 | #include <linux/of_mdio.h> |
| 21 | #include <linux/phy.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/regulator/consumer.h> |
| 24 | |
| 25 | #define EMAC_MAC_MCMD_REG (0x00) |
| 26 | #define EMAC_MAC_MADR_REG (0x04) |
| 27 | #define EMAC_MAC_MWTD_REG (0x08) |
| 28 | #define EMAC_MAC_MRDD_REG (0x0c) |
| 29 | #define EMAC_MAC_MIND_REG (0x10) |
| 30 | #define EMAC_MAC_SSRR_REG (0x14) |
| 31 | |
| 32 | #define MDIO_TIMEOUT (msecs_to_jiffies(100)) |
| 33 | |
| 34 | struct sun4i_mdio_data { |
| 35 | void __iomem *membase; |
| 36 | struct regulator *regulator; |
| 37 | }; |
| 38 | |
| 39 | static int sun4i_mdio_read(struct mii_bus *bus, int mii_id, int regnum) |
| 40 | { |
| 41 | struct sun4i_mdio_data *data = bus->priv; |
Emilio López | 2bf420a | 2013-07-22 22:09:18 -0300 | [diff] [blame] | 42 | unsigned long timeout_jiffies; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 43 | int value; |
| 44 | |
| 45 | /* issue the phy address and reg */ |
| 46 | writel((mii_id << 8) | regnum, data->membase + EMAC_MAC_MADR_REG); |
| 47 | /* pull up the phy io line */ |
| 48 | writel(0x1, data->membase + EMAC_MAC_MCMD_REG); |
| 49 | |
| 50 | /* Wait read complete */ |
Emilio López | 2bf420a | 2013-07-22 22:09:18 -0300 | [diff] [blame] | 51 | timeout_jiffies = jiffies + MDIO_TIMEOUT; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 52 | while (readl(data->membase + EMAC_MAC_MIND_REG) & 0x1) { |
Emilio López | 2bf420a | 2013-07-22 22:09:18 -0300 | [diff] [blame] | 53 | if (time_is_before_jiffies(timeout_jiffies)) |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 54 | return -ETIMEDOUT; |
| 55 | msleep(1); |
| 56 | } |
| 57 | |
| 58 | /* push down the phy io line */ |
| 59 | writel(0x0, data->membase + EMAC_MAC_MCMD_REG); |
| 60 | /* and read data */ |
| 61 | value = readl(data->membase + EMAC_MAC_MRDD_REG); |
| 62 | |
| 63 | return value; |
| 64 | } |
| 65 | |
| 66 | static int sun4i_mdio_write(struct mii_bus *bus, int mii_id, int regnum, |
| 67 | u16 value) |
| 68 | { |
| 69 | struct sun4i_mdio_data *data = bus->priv; |
Emilio López | 2bf420a | 2013-07-22 22:09:18 -0300 | [diff] [blame] | 70 | unsigned long timeout_jiffies; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 71 | |
| 72 | /* issue the phy address and reg */ |
| 73 | writel((mii_id << 8) | regnum, data->membase + EMAC_MAC_MADR_REG); |
| 74 | /* pull up the phy io line */ |
| 75 | writel(0x1, data->membase + EMAC_MAC_MCMD_REG); |
| 76 | |
| 77 | /* Wait read complete */ |
Emilio López | 2bf420a | 2013-07-22 22:09:18 -0300 | [diff] [blame] | 78 | timeout_jiffies = jiffies + MDIO_TIMEOUT; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 79 | while (readl(data->membase + EMAC_MAC_MIND_REG) & 0x1) { |
Emilio López | 2bf420a | 2013-07-22 22:09:18 -0300 | [diff] [blame] | 80 | if (time_is_before_jiffies(timeout_jiffies)) |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 81 | return -ETIMEDOUT; |
| 82 | msleep(1); |
| 83 | } |
| 84 | |
| 85 | /* push down the phy io line */ |
| 86 | writel(0x0, data->membase + EMAC_MAC_MCMD_REG); |
| 87 | /* and write data */ |
| 88 | writel(value, data->membase + EMAC_MAC_MWTD_REG); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 93 | static int sun4i_mdio_probe(struct platform_device *pdev) |
| 94 | { |
| 95 | struct device_node *np = pdev->dev.of_node; |
| 96 | struct mii_bus *bus; |
| 97 | struct sun4i_mdio_data *data; |
Jisheng Zhang | 03536cc | 2013-08-26 21:11:57 +0800 | [diff] [blame] | 98 | struct resource *res; |
Andrew Lunn | e7f4dc3 | 2016-01-06 20:11:15 +0100 | [diff] [blame] | 99 | int ret; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 100 | |
| 101 | bus = mdiobus_alloc_size(sizeof(*data)); |
| 102 | if (!bus) |
| 103 | return -ENOMEM; |
| 104 | |
| 105 | bus->name = "sun4i_mii_bus"; |
| 106 | bus->read = &sun4i_mdio_read; |
| 107 | bus->write = &sun4i_mdio_write; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 108 | snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(&pdev->dev)); |
| 109 | bus->parent = &pdev->dev; |
| 110 | |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 111 | data = bus->priv; |
Jisheng Zhang | 03536cc | 2013-08-26 21:11:57 +0800 | [diff] [blame] | 112 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 113 | data->membase = devm_ioremap_resource(&pdev->dev, res); |
| 114 | if (IS_ERR(data->membase)) { |
| 115 | ret = PTR_ERR(data->membase); |
| 116 | goto err_out_free_mdiobus; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | data->regulator = devm_regulator_get(&pdev->dev, "phy"); |
| 120 | if (IS_ERR(data->regulator)) { |
| 121 | if (PTR_ERR(data->regulator) == -EPROBE_DEFER) |
| 122 | return -EPROBE_DEFER; |
| 123 | |
| 124 | dev_info(&pdev->dev, "no regulator found\n"); |
Dan Carpenter | 227f33b | 2016-03-21 12:02:31 +0300 | [diff] [blame] | 125 | data->regulator = NULL; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 126 | } else { |
| 127 | ret = regulator_enable(data->regulator); |
| 128 | if (ret) |
Jisheng Zhang | 03536cc | 2013-08-26 21:11:57 +0800 | [diff] [blame] | 129 | goto err_out_free_mdiobus; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | ret = of_mdiobus_register(bus, np); |
| 133 | if (ret < 0) |
| 134 | goto err_out_disable_regulator; |
| 135 | |
| 136 | platform_set_drvdata(pdev, bus); |
| 137 | |
| 138 | return 0; |
| 139 | |
| 140 | err_out_disable_regulator: |
Dan Carpenter | 227f33b | 2016-03-21 12:02:31 +0300 | [diff] [blame] | 141 | if (data->regulator) |
| 142 | regulator_disable(data->regulator); |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 143 | err_out_free_mdiobus: |
| 144 | mdiobus_free(bus); |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | static int sun4i_mdio_remove(struct platform_device *pdev) |
| 149 | { |
| 150 | struct mii_bus *bus = platform_get_drvdata(pdev); |
| 151 | |
| 152 | mdiobus_unregister(bus); |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 153 | mdiobus_free(bus); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static const struct of_device_id sun4i_mdio_dt_ids[] = { |
Maxime Ripard | efe2057 | 2014-02-02 14:49:12 +0100 | [diff] [blame] | 159 | { .compatible = "allwinner,sun4i-a10-mdio" }, |
| 160 | |
| 161 | /* Deprecated */ |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 162 | { .compatible = "allwinner,sun4i-mdio" }, |
| 163 | { } |
| 164 | }; |
| 165 | MODULE_DEVICE_TABLE(of, sun4i_mdio_dt_ids); |
| 166 | |
| 167 | static struct platform_driver sun4i_mdio_driver = { |
| 168 | .probe = sun4i_mdio_probe, |
| 169 | .remove = sun4i_mdio_remove, |
| 170 | .driver = { |
| 171 | .name = "sun4i-mdio", |
| 172 | .of_match_table = sun4i_mdio_dt_ids, |
| 173 | }, |
| 174 | }; |
| 175 | |
| 176 | module_platform_driver(sun4i_mdio_driver); |
| 177 | |
| 178 | MODULE_DESCRIPTION("Allwinner EMAC MDIO interface driver"); |
| 179 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); |
| 180 | MODULE_LICENSE("GPL"); |