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> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/of_address.h> |
| 21 | #include <linux/of_mdio.h> |
| 22 | #include <linux/phy.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/regulator/consumer.h> |
| 25 | |
| 26 | #define EMAC_MAC_MCMD_REG (0x00) |
| 27 | #define EMAC_MAC_MADR_REG (0x04) |
| 28 | #define EMAC_MAC_MWTD_REG (0x08) |
| 29 | #define EMAC_MAC_MRDD_REG (0x0c) |
| 30 | #define EMAC_MAC_MIND_REG (0x10) |
| 31 | #define EMAC_MAC_SSRR_REG (0x14) |
| 32 | |
| 33 | #define MDIO_TIMEOUT (msecs_to_jiffies(100)) |
| 34 | |
| 35 | struct sun4i_mdio_data { |
| 36 | void __iomem *membase; |
| 37 | struct regulator *regulator; |
| 38 | }; |
| 39 | |
| 40 | static int sun4i_mdio_read(struct mii_bus *bus, int mii_id, int regnum) |
| 41 | { |
| 42 | struct sun4i_mdio_data *data = bus->priv; |
Emilio López | 2bf420a | 2013-07-22 22:09:18 -0300 | [diff] [blame] | 43 | unsigned long timeout_jiffies; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 44 | int value; |
| 45 | |
| 46 | /* issue the phy address and reg */ |
| 47 | writel((mii_id << 8) | regnum, data->membase + EMAC_MAC_MADR_REG); |
| 48 | /* pull up the phy io line */ |
| 49 | writel(0x1, data->membase + EMAC_MAC_MCMD_REG); |
| 50 | |
| 51 | /* Wait read complete */ |
Emilio López | 2bf420a | 2013-07-22 22:09:18 -0300 | [diff] [blame] | 52 | timeout_jiffies = jiffies + MDIO_TIMEOUT; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 53 | while (readl(data->membase + EMAC_MAC_MIND_REG) & 0x1) { |
Emilio López | 2bf420a | 2013-07-22 22:09:18 -0300 | [diff] [blame] | 54 | if (time_is_before_jiffies(timeout_jiffies)) |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 55 | return -ETIMEDOUT; |
| 56 | msleep(1); |
| 57 | } |
| 58 | |
| 59 | /* push down the phy io line */ |
| 60 | writel(0x0, data->membase + EMAC_MAC_MCMD_REG); |
| 61 | /* and read data */ |
| 62 | value = readl(data->membase + EMAC_MAC_MRDD_REG); |
| 63 | |
| 64 | return value; |
| 65 | } |
| 66 | |
| 67 | static int sun4i_mdio_write(struct mii_bus *bus, int mii_id, int regnum, |
| 68 | u16 value) |
| 69 | { |
| 70 | struct sun4i_mdio_data *data = bus->priv; |
Emilio López | 2bf420a | 2013-07-22 22:09:18 -0300 | [diff] [blame] | 71 | unsigned long timeout_jiffies; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 72 | |
| 73 | /* issue the phy address and reg */ |
| 74 | writel((mii_id << 8) | regnum, data->membase + EMAC_MAC_MADR_REG); |
| 75 | /* pull up the phy io line */ |
| 76 | writel(0x1, data->membase + EMAC_MAC_MCMD_REG); |
| 77 | |
| 78 | /* Wait read complete */ |
Emilio López | 2bf420a | 2013-07-22 22:09:18 -0300 | [diff] [blame] | 79 | timeout_jiffies = jiffies + MDIO_TIMEOUT; |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 80 | while (readl(data->membase + EMAC_MAC_MIND_REG) & 0x1) { |
Emilio López | 2bf420a | 2013-07-22 22:09:18 -0300 | [diff] [blame] | 81 | if (time_is_before_jiffies(timeout_jiffies)) |
Maxime Ripard | 4bdcb1d | 2013-05-30 03:49:21 +0000 | [diff] [blame] | 82 | return -ETIMEDOUT; |
| 83 | msleep(1); |
| 84 | } |
| 85 | |
| 86 | /* push down the phy io line */ |
| 87 | writel(0x0, data->membase + EMAC_MAC_MCMD_REG); |
| 88 | /* and write data */ |
| 89 | writel(value, data->membase + EMAC_MAC_MWTD_REG); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int sun4i_mdio_reset(struct mii_bus *bus) |
| 95 | { |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int sun4i_mdio_probe(struct platform_device *pdev) |
| 100 | { |
| 101 | struct device_node *np = pdev->dev.of_node; |
| 102 | struct mii_bus *bus; |
| 103 | struct sun4i_mdio_data *data; |
| 104 | int ret, i; |
| 105 | |
| 106 | bus = mdiobus_alloc_size(sizeof(*data)); |
| 107 | if (!bus) |
| 108 | return -ENOMEM; |
| 109 | |
| 110 | bus->name = "sun4i_mii_bus"; |
| 111 | bus->read = &sun4i_mdio_read; |
| 112 | bus->write = &sun4i_mdio_write; |
| 113 | bus->reset = &sun4i_mdio_reset; |
| 114 | snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(&pdev->dev)); |
| 115 | bus->parent = &pdev->dev; |
| 116 | |
| 117 | bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); |
| 118 | if (!bus->irq) { |
| 119 | ret = -ENOMEM; |
| 120 | goto err_out_free_mdiobus; |
| 121 | } |
| 122 | |
| 123 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 124 | bus->irq[i] = PHY_POLL; |
| 125 | |
| 126 | data = bus->priv; |
| 127 | data->membase = of_iomap(np, 0); |
| 128 | if (!data->membase) { |
| 129 | ret = -ENOMEM; |
| 130 | goto err_out_free_mdio_irq; |
| 131 | } |
| 132 | |
| 133 | data->regulator = devm_regulator_get(&pdev->dev, "phy"); |
| 134 | if (IS_ERR(data->regulator)) { |
| 135 | if (PTR_ERR(data->regulator) == -EPROBE_DEFER) |
| 136 | return -EPROBE_DEFER; |
| 137 | |
| 138 | dev_info(&pdev->dev, "no regulator found\n"); |
| 139 | } else { |
| 140 | ret = regulator_enable(data->regulator); |
| 141 | if (ret) |
| 142 | goto err_out_free_mdio_irq; |
| 143 | } |
| 144 | |
| 145 | ret = of_mdiobus_register(bus, np); |
| 146 | if (ret < 0) |
| 147 | goto err_out_disable_regulator; |
| 148 | |
| 149 | platform_set_drvdata(pdev, bus); |
| 150 | |
| 151 | return 0; |
| 152 | |
| 153 | err_out_disable_regulator: |
| 154 | regulator_disable(data->regulator); |
| 155 | err_out_free_mdio_irq: |
| 156 | kfree(bus->irq); |
| 157 | err_out_free_mdiobus: |
| 158 | mdiobus_free(bus); |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | static int sun4i_mdio_remove(struct platform_device *pdev) |
| 163 | { |
| 164 | struct mii_bus *bus = platform_get_drvdata(pdev); |
| 165 | |
| 166 | mdiobus_unregister(bus); |
| 167 | kfree(bus->irq); |
| 168 | mdiobus_free(bus); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static const struct of_device_id sun4i_mdio_dt_ids[] = { |
| 174 | { .compatible = "allwinner,sun4i-mdio" }, |
| 175 | { } |
| 176 | }; |
| 177 | MODULE_DEVICE_TABLE(of, sun4i_mdio_dt_ids); |
| 178 | |
| 179 | static struct platform_driver sun4i_mdio_driver = { |
| 180 | .probe = sun4i_mdio_probe, |
| 181 | .remove = sun4i_mdio_remove, |
| 182 | .driver = { |
| 183 | .name = "sun4i-mdio", |
| 184 | .of_match_table = sun4i_mdio_dt_ids, |
| 185 | }, |
| 186 | }; |
| 187 | |
| 188 | module_platform_driver(sun4i_mdio_driver); |
| 189 | |
| 190 | MODULE_DESCRIPTION("Allwinner EMAC MDIO interface driver"); |
| 191 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); |
| 192 | MODULE_LICENSE("GPL"); |