#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/spinlock.h>
+#include <linux/skbuff.h>
#include "rtl8366_smi.h"
rtl8366_smi_clk_delay(smi);
/* prepare data */
- if ( data & ( 1 << (len - 1)) )
- gpio_set_value(sda, 1);
- else
- gpio_set_value(sda, 0);
+ gpio_set_value(sda, !!(data & ( 1 << (len - 1))));
rtl8366_smi_clk_delay(smi);
/* clocking */
/* clocking */
gpio_set_value(sck, 1);
rtl8366_smi_clk_delay(smi);
- u = gpio_get_value(sda);
+ u = !!gpio_get_value(sda);
gpio_set_value(sck, 0);
*data |= (u << (len - 1));
}
EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg);
+int rtl8366_smi_rmwr(struct rtl8366_smi *smi, u32 addr, u32 mask, u32 data)
+{
+ u32 t;
+ int err;
+
+ err = rtl8366_smi_read_reg(smi, addr, &t);
+ if (err)
+ return err;
+
+ err = rtl8366_smi_write_reg(smi, addr, (t & ~mask) | data);
+ return err;
+
+}
+EXPORT_SYMBOL_GPL(rtl8366_smi_rmwr);
+
+static int rtl8366_smi_mii_init(struct rtl8366_smi *smi)
+{
+ int ret;
+ int i;
+
+ smi->mii_bus = mdiobus_alloc();
+ if (smi->mii_bus == NULL) {
+ ret = -ENOMEM;
+ goto err;
+ }
+
+ smi->mii_bus->priv = (void *) smi;
+ smi->mii_bus->name = dev_name(smi->parent);
+ smi->mii_bus->read = smi->ops->mii_read;
+ smi->mii_bus->write = smi->ops->mii_write;
+ snprintf(smi->mii_bus->id, MII_BUS_ID_SIZE, "%s",
+ dev_name(smi->parent));
+ smi->mii_bus->parent = smi->parent;
+ smi->mii_bus->phy_mask = ~(0x1f);
+ smi->mii_bus->irq = smi->mii_irq;
+ for (i = 0; i < PHY_MAX_ADDR; i++)
+ smi->mii_irq[i] = PHY_POLL;
+
+ ret = mdiobus_register(smi->mii_bus);
+ if (ret)
+ goto err_free;
+
+ return 0;
+
+ err_free:
+ mdiobus_free(smi->mii_bus);
+ err:
+ return ret;
+}
+
+static void rtl8366_smi_mii_cleanup(struct rtl8366_smi *smi)
+{
+ mdiobus_unregister(smi->mii_bus);
+ mdiobus_free(smi->mii_bus);
+}
+
int rtl8366_smi_init(struct rtl8366_smi *smi)
{
int err;
if (!smi->parent)
return -EINVAL;
+ if (!smi->ops)
+ return -EINVAL;
+
err = gpio_request(smi->gpio_sda, dev_name(smi->parent));
if (err) {
dev_err(smi->parent, "gpio_request failed for %u, err=%d\n",
dev_info(smi->parent, "using GPIO pins %u (SDA) and %u (SCK)\n",
smi->gpio_sda, smi->gpio_sck);
+ err = smi->ops->detect(smi);
+ if (err) {
+ dev_err(smi->parent, "chip detection failed, err=%d\n", err);
+ goto err_free_sck;
+ }
+
+ err = rtl8366_smi_mii_init(smi);
+ if (err)
+ goto err_free_sck;
+
return 0;
+ err_free_sck:
+ gpio_free(smi->gpio_sck);
err_free_sda:
gpio_free(smi->gpio_sda);
err_out:
void rtl8366_smi_cleanup(struct rtl8366_smi *smi)
{
+ rtl8366_smi_mii_cleanup(smi);
gpio_free(smi->gpio_sck);
gpio_free(smi->gpio_sda);
}