Re: [RFC 5/7] net: ethernet: bgmac: Add platform device support

2016-06-30 Thread Jon Mason
On Thu, Jun 30, 2016 at 1:58 PM, Ray Jui  wrote:
> Hi Jon,
>
>
> On 6/28/2016 12:34 PM, Jon Mason wrote:
>>
>> The bcma portion of the driver has been split off into a bcma specific
>> driver.  This has been mirrored for the platform driver.  The last
>> references to the bcma core struct have been changed into a generic
>> function call.  These function calls are wrappers to either the original
>> bcma code or new platform functions that access the same areas via MMIO.
>> This necessitated adding function pointers for both platform and bcma to
>> hide which backend is being used from the generic bgmac code.
>>
>> Signed-off-by: Jon Mason 
>> ---
>>  drivers/net/ethernet/broadcom/Kconfig  |  23 +-
>>  drivers/net/ethernet/broadcom/Makefile |   4 +-
>>  drivers/net/ethernet/broadcom/bgmac-bcma.c | 315
>> 
>>  drivers/net/ethernet/broadcom/bgmac-platform.c | 208 
>>  drivers/net/ethernet/broadcom/bgmac.c  | 327
>> -
>>  drivers/net/ethernet/broadcom/bgmac.h  |  73 +-
>>  6 files changed, 666 insertions(+), 284 deletions(-)
>>  create mode 100644 drivers/net/ethernet/broadcom/bgmac-bcma.c
>>  create mode 100644 drivers/net/ethernet/broadcom/bgmac-platform.c
>>
>> diff --git a/drivers/net/ethernet/broadcom/Kconfig
>> b/drivers/net/ethernet/broadcom/Kconfig
>> index d74a92e..bd8c80c 100644
>> --- a/drivers/net/ethernet/broadcom/Kconfig
>> +++ b/drivers/net/ethernet/broadcom/Kconfig
>> @@ -140,10 +140,18 @@ config BNX2X_SRIOV
>>   allows for virtual function acceleration in virtual
>> environments.
>>
>>  config BGMAC
>> -   tristate "BCMA bus GBit core support"
>> +   tristate
>> +   help
>> + This enables the integrated ethernet controller support for many
>> + Broadcom (mostly iProc) SoCs. An appropriate bus interface
>> driver
>> + needs to be enabled to select this.
>> +
>> +config BGMAC_BCMA
>> +   tristate "Broadcom iProc GBit BCMA support"
>> depends on BCMA && BCMA_HOST_SOC
>> depends on HAS_DMA
>> depends on BCM47XX || ARCH_BCM_5301X || COMPILE_TEST
>> +   select BGMAC
>> select PHYLIB
>> select FIXED_PHY
>> ---help---
>> @@ -152,6 +160,19 @@ config BGMAC
>>   In case of using this driver on BCM4706 it's also requires to
>> enable
>>   BCMA_DRIVER_GMAC_CMN to make it work.
>>
>> +config BGMAC_PLATFORM
>> +   tristate "Broadcom iProc GBit platform support"
>> +   depends on HAS_DMA
>> +   depends on ARCH_BCM_IPROC || COMPILE_TEST
>> +   depends on OF
>> +   select BGMAC
>> +   select PHYLIB
>> +   select FIXED_PHY
>> +   default ARCH_BCM_IPROC
>> +   ---help---
>> + Say Y here if you want to use the Broadcom iProc Gigabit
>> Ethernet
>> + controller through the generic platform interface
>> +
>>  config SYSTEMPORT
>> tristate "Broadcom SYSTEMPORT internal MAC support"
>> depends on OF
>> diff --git a/drivers/net/ethernet/broadcom/Makefile
>> b/drivers/net/ethernet/broadcom/Makefile
>> index f559794..79f2372 100644
>> --- a/drivers/net/ethernet/broadcom/Makefile
>> +++ b/drivers/net/ethernet/broadcom/Makefile
>> @@ -10,6 +10,8 @@ obj-$(CONFIG_CNIC) += cnic.o
>>  obj-$(CONFIG_BNX2X) += bnx2x/
>>  obj-$(CONFIG_SB1250_MAC) += sb1250-mac.o
>>  obj-$(CONFIG_TIGON3) += tg3.o
>> -obj-$(CONFIG_BGMAC) += bgmac.o bgmac-bcma-mdio.o
>> +obj-$(CONFIG_BGMAC) += bgmac.o
>> +obj-$(CONFIG_BGMAC_BCMA) += bgmac-bcma.o bgmac-bcma-mdio.o
>> +obj-$(CONFIG_BGMAC_PLATFORM) += bgmac-platform.o
>>  obj-$(CONFIG_SYSTEMPORT) += bcmsysport.o
>>  obj-$(CONFIG_BNXT) += bnxt/
>> diff --git a/drivers/net/ethernet/broadcom/bgmac-bcma.c
>> b/drivers/net/ethernet/broadcom/bgmac-bcma.c
>> new file mode 100644
>> index 000..9a9745c4
>> --- /dev/null
>> +++ b/drivers/net/ethernet/broadcom/bgmac-bcma.c
>> @@ -0,0 +1,315 @@
>> +/*
>> + * Driver for (BCM4706)? GBit MAC core on BCMA bus.
>> + *
>> + * Copyright (C) 2012 Rafał Miłecki 
>> + *
>> + * Licensed under the GNU/GPL. See COPYING for details.
>> + */
>> +
>> +#define pr_fmt(fmt)KBUILD_MODNAME ": " fmt
>> +
>> +#include 
>> +#include 
>> +#include 
>> +#include "bgmac.h"
>> +
>> +static inline bool bgmac_is_bcm4707_family(struct bcma_device *core)
>> +{
>> +   switch (core->bus->chipinfo.id) {
>> +   case BCMA_CHIP_ID_BCM4707:
>> +   case BCMA_CHIP_ID_BCM47094:
>> +   case BCMA_CHIP_ID_BCM53018:
>> +   return true;
>> +   default:
>> +   return false;
>> +   }
>> +}
>> +
>> +/**
>> + * BCMA bus ops
>> + **/
>> +
>> +static u32 bcma_bgmac_read(struct bgmac *bgmac, u16 offset)
>> +{
>> +   return bcma_read32(bgmac->bcma.core, offset);
>> +}
>> +
>> +static void bcma_bgmac_write(struct 

Re: [RFC 5/7] net: ethernet: bgmac: Add platform device support

2016-06-30 Thread Ray Jui

Hi Jon,

On 6/28/2016 12:34 PM, Jon Mason wrote:

The bcma portion of the driver has been split off into a bcma specific
driver.  This has been mirrored for the platform driver.  The last
references to the bcma core struct have been changed into a generic
function call.  These function calls are wrappers to either the original
bcma code or new platform functions that access the same areas via MMIO.
This necessitated adding function pointers for both platform and bcma to
hide which backend is being used from the generic bgmac code.

Signed-off-by: Jon Mason 
---
 drivers/net/ethernet/broadcom/Kconfig  |  23 +-
 drivers/net/ethernet/broadcom/Makefile |   4 +-
 drivers/net/ethernet/broadcom/bgmac-bcma.c | 315 
 drivers/net/ethernet/broadcom/bgmac-platform.c | 208 
 drivers/net/ethernet/broadcom/bgmac.c  | 327 -
 drivers/net/ethernet/broadcom/bgmac.h  |  73 +-
 6 files changed, 666 insertions(+), 284 deletions(-)
 create mode 100644 drivers/net/ethernet/broadcom/bgmac-bcma.c
 create mode 100644 drivers/net/ethernet/broadcom/bgmac-platform.c

diff --git a/drivers/net/ethernet/broadcom/Kconfig 
b/drivers/net/ethernet/broadcom/Kconfig
index d74a92e..bd8c80c 100644
--- a/drivers/net/ethernet/broadcom/Kconfig
+++ b/drivers/net/ethernet/broadcom/Kconfig
@@ -140,10 +140,18 @@ config BNX2X_SRIOV
  allows for virtual function acceleration in virtual environments.

 config BGMAC
-   tristate "BCMA bus GBit core support"
+   tristate
+   help
+ This enables the integrated ethernet controller support for many
+ Broadcom (mostly iProc) SoCs. An appropriate bus interface driver
+ needs to be enabled to select this.
+
+config BGMAC_BCMA
+   tristate "Broadcom iProc GBit BCMA support"
depends on BCMA && BCMA_HOST_SOC
depends on HAS_DMA
depends on BCM47XX || ARCH_BCM_5301X || COMPILE_TEST
+   select BGMAC
select PHYLIB
select FIXED_PHY
---help---
@@ -152,6 +160,19 @@ config BGMAC
  In case of using this driver on BCM4706 it's also requires to enable
  BCMA_DRIVER_GMAC_CMN to make it work.

+config BGMAC_PLATFORM
+   tristate "Broadcom iProc GBit platform support"
+   depends on HAS_DMA
+   depends on ARCH_BCM_IPROC || COMPILE_TEST
+   depends on OF
+   select BGMAC
+   select PHYLIB
+   select FIXED_PHY
+   default ARCH_BCM_IPROC
+   ---help---
+ Say Y here if you want to use the Broadcom iProc Gigabit Ethernet
+ controller through the generic platform interface
+
 config SYSTEMPORT
tristate "Broadcom SYSTEMPORT internal MAC support"
depends on OF
diff --git a/drivers/net/ethernet/broadcom/Makefile 
b/drivers/net/ethernet/broadcom/Makefile
index f559794..79f2372 100644
--- a/drivers/net/ethernet/broadcom/Makefile
+++ b/drivers/net/ethernet/broadcom/Makefile
@@ -10,6 +10,8 @@ obj-$(CONFIG_CNIC) += cnic.o
 obj-$(CONFIG_BNX2X) += bnx2x/
 obj-$(CONFIG_SB1250_MAC) += sb1250-mac.o
 obj-$(CONFIG_TIGON3) += tg3.o
-obj-$(CONFIG_BGMAC) += bgmac.o bgmac-bcma-mdio.o
+obj-$(CONFIG_BGMAC) += bgmac.o
+obj-$(CONFIG_BGMAC_BCMA) += bgmac-bcma.o bgmac-bcma-mdio.o
+obj-$(CONFIG_BGMAC_PLATFORM) += bgmac-platform.o
 obj-$(CONFIG_SYSTEMPORT) += bcmsysport.o
 obj-$(CONFIG_BNXT) += bnxt/
diff --git a/drivers/net/ethernet/broadcom/bgmac-bcma.c 
b/drivers/net/ethernet/broadcom/bgmac-bcma.c
new file mode 100644
index 000..9a9745c4
--- /dev/null
+++ b/drivers/net/ethernet/broadcom/bgmac-bcma.c
@@ -0,0 +1,315 @@
+/*
+ * Driver for (BCM4706)? GBit MAC core on BCMA bus.
+ *
+ * Copyright (C) 2012 Rafał Miłecki 
+ *
+ * Licensed under the GNU/GPL. See COPYING for details.
+ */
+
+#define pr_fmt(fmt)KBUILD_MODNAME ": " fmt
+
+#include 
+#include 
+#include 
+#include "bgmac.h"
+
+static inline bool bgmac_is_bcm4707_family(struct bcma_device *core)
+{
+   switch (core->bus->chipinfo.id) {
+   case BCMA_CHIP_ID_BCM4707:
+   case BCMA_CHIP_ID_BCM47094:
+   case BCMA_CHIP_ID_BCM53018:
+   return true;
+   default:
+   return false;
+   }
+}
+
+/**
+ * BCMA bus ops
+ **/
+
+static u32 bcma_bgmac_read(struct bgmac *bgmac, u16 offset)
+{
+   return bcma_read32(bgmac->bcma.core, offset);
+}
+
+static void bcma_bgmac_write(struct bgmac *bgmac, u16 offset, u32 value)
+{
+   bcma_write32(bgmac->bcma.core, offset, value);
+}
+
+static u32 bcma_bgmac_idm_read(struct bgmac *bgmac, u16 offset)
+{
+   return bcma_aread32(bgmac->bcma.core, offset);
+}
+
+static void bcma_bgmac_idm_write(struct bgmac *bgmac, u16 offset, u32 value)
+{
+   return bcma_awrite32(bgmac->bcma.core, offset, value);
+}
+
+static bool bcma_bgmac_clk_enabled(struct bgmac *bgmac)
+{
+   return 

Re: [RFC 5/7] net: ethernet: bgmac: Add platform device support

2016-06-29 Thread Florian Fainelli
On 06/28/2016 12:34 PM, Jon Mason wrote:
> The bcma portion of the driver has been split off into a bcma specific
> driver.  This has been mirrored for the platform driver.  The last
> references to the bcma core struct have been changed into a generic
> function call.  These function calls are wrappers to either the original
> bcma code or new platform functions that access the same areas via MMIO.
> This necessitated adding function pointers for both platform and bcma to
> hide which backend is being used from the generic bgmac code.
> 
> Signed-off-by: Jon Mason 
> ---

[snip]

> +static int bgmac_probe(struct platform_device *pdev)
> +{
> + struct device_node *np = pdev->dev.of_node;
> + struct bgmac *bgmac;
> + struct resource regs;
> + const u8 *mac_addr;
> + int rc;
> +
> + bgmac = kzalloc(sizeof(*bgmac), GFP_KERNEL);

You could utilize devm_kzalloc() here which simplifies the error path.

> + if (!bgmac)
> + return -ENOMEM;
> +
> + platform_set_drvdata(pdev, bgmac);
> +
> + /* Set the features of the 4707 family */
> + bgmac->feature_flags |= BGMAC_FEAT_CLKCTLST;
> + bgmac->feature_flags |= BGMAC_FEAT_NO_RESET;
> + bgmac->feature_flags |= BGMAC_FEAT_FORCE_SPEED_2500;
> + bgmac->feature_flags |= BGMAC_FEAT_CMDCFG_SR_REV4;
> + bgmac->feature_flags |= BGMAC_FEAT_TX_MASK_SETUP;
> + bgmac->feature_flags |= BGMAC_FEAT_RX_MASK_SETUP;
> +
> + bgmac->dev = >dev;
> + bgmac->dma_dev = >dev;
> +
> + mac_addr = of_get_mac_address(np);
> + if (mac_addr)
> + ether_addr_copy(bgmac->mac_addr, mac_addr);
> + else
> + dev_warn(>dev, "MAC address not present in device 
> tree\n");
> +
> + bgmac->irq = platform_get_irq(pdev, 0);
> + if (bgmac->irq < 0) {
> + rc = bgmac->irq;
> + dev_err(>dev, "Unable to obtain IRQ\n");
> + goto err;
> + }
> +
> + rc = of_address_to_resource(np, 0, );
> + if (rc < 0) {
> + dev_err(>dev, "Unable to obtain base resource\n");
> + goto err;
> + }

Here you could fetch the resource using a traditional
platform_get_resource(pdev, IORESOURCE_MEM, 0) and...

> +
> + bgmac->plat.base = ioremap(regs.start, resource_size());
> + if (!bgmac->plat.base) {
> + dev_err(>dev, "Unable to map base resource\n");
> + rc = -ENOMEM;
> + goto err;
> + }

... here do a devm_ioremap_resource(), which also does a
request_mem_region, so this shows up nicely in /proc/iomem.

> +
> + rc = of_address_to_resource(np, 1, );
> + if (rc < 0) {
> + dev_err(>dev, "Unable to obtain idm resource\n");
> + goto err1;
> + }
> +
> + bgmac->plat.idm_base = ioremap(regs.start, resource_size());
> + if (!bgmac->plat.base) {
> + dev_err(>dev, "Unable to map idm resource\n");
> + rc = -ENOMEM;
> + goto err1;
> + }

Same here.

> +
> + bgmac->read = platform_bgmac_read;
> + bgmac->write = platform_bgmac_write;
> + bgmac->idm_read = platform_bgmac_idm_read;
> + bgmac->idm_write = platform_bgmac_idm_write;
> + bgmac->clk_enabled = platform_bgmac_clk_enabled;
> + bgmac->clk_enable = platform_bgmac_clk_enable;
> + bgmac->cco_ctl_maskset = platform_bgmac_cco_ctl_maskset;
> + bgmac->get_bus_clock = platform_bgmac_get_bus_clock;
> + bgmac->cmn_maskset32 = platform_bgmac_cmn_maskset32;
> +
> + rc = bgmac_enet_probe(bgmac);
> + if (rc)
> + goto err2;
> +
> + return 0;
> +
> +err2:
> + iounmap(bgmac->plat.idm_base);
> +err1:
> + iounmap(bgmac->plat.base);
> +err:
> + kfree(bgmac);

And with devm_* helpers none of that is needed now.
-- 
Florian


[RFC 5/7] net: ethernet: bgmac: Add platform device support

2016-06-28 Thread Jon Mason
The bcma portion of the driver has been split off into a bcma specific
driver.  This has been mirrored for the platform driver.  The last
references to the bcma core struct have been changed into a generic
function call.  These function calls are wrappers to either the original
bcma code or new platform functions that access the same areas via MMIO.
This necessitated adding function pointers for both platform and bcma to
hide which backend is being used from the generic bgmac code.

Signed-off-by: Jon Mason 
---
 drivers/net/ethernet/broadcom/Kconfig  |  23 +-
 drivers/net/ethernet/broadcom/Makefile |   4 +-
 drivers/net/ethernet/broadcom/bgmac-bcma.c | 315 
 drivers/net/ethernet/broadcom/bgmac-platform.c | 208 
 drivers/net/ethernet/broadcom/bgmac.c  | 327 -
 drivers/net/ethernet/broadcom/bgmac.h  |  73 +-
 6 files changed, 666 insertions(+), 284 deletions(-)
 create mode 100644 drivers/net/ethernet/broadcom/bgmac-bcma.c
 create mode 100644 drivers/net/ethernet/broadcom/bgmac-platform.c

diff --git a/drivers/net/ethernet/broadcom/Kconfig 
b/drivers/net/ethernet/broadcom/Kconfig
index d74a92e..bd8c80c 100644
--- a/drivers/net/ethernet/broadcom/Kconfig
+++ b/drivers/net/ethernet/broadcom/Kconfig
@@ -140,10 +140,18 @@ config BNX2X_SRIOV
  allows for virtual function acceleration in virtual environments.
 
 config BGMAC
-   tristate "BCMA bus GBit core support"
+   tristate
+   help
+ This enables the integrated ethernet controller support for many
+ Broadcom (mostly iProc) SoCs. An appropriate bus interface driver
+ needs to be enabled to select this.
+
+config BGMAC_BCMA
+   tristate "Broadcom iProc GBit BCMA support"
depends on BCMA && BCMA_HOST_SOC
depends on HAS_DMA
depends on BCM47XX || ARCH_BCM_5301X || COMPILE_TEST
+   select BGMAC
select PHYLIB
select FIXED_PHY
---help---
@@ -152,6 +160,19 @@ config BGMAC
  In case of using this driver on BCM4706 it's also requires to enable
  BCMA_DRIVER_GMAC_CMN to make it work.
 
+config BGMAC_PLATFORM
+   tristate "Broadcom iProc GBit platform support"
+   depends on HAS_DMA
+   depends on ARCH_BCM_IPROC || COMPILE_TEST
+   depends on OF
+   select BGMAC
+   select PHYLIB
+   select FIXED_PHY
+   default ARCH_BCM_IPROC
+   ---help---
+ Say Y here if you want to use the Broadcom iProc Gigabit Ethernet
+ controller through the generic platform interface
+
 config SYSTEMPORT
tristate "Broadcom SYSTEMPORT internal MAC support"
depends on OF
diff --git a/drivers/net/ethernet/broadcom/Makefile 
b/drivers/net/ethernet/broadcom/Makefile
index f559794..79f2372 100644
--- a/drivers/net/ethernet/broadcom/Makefile
+++ b/drivers/net/ethernet/broadcom/Makefile
@@ -10,6 +10,8 @@ obj-$(CONFIG_CNIC) += cnic.o
 obj-$(CONFIG_BNX2X) += bnx2x/
 obj-$(CONFIG_SB1250_MAC) += sb1250-mac.o
 obj-$(CONFIG_TIGON3) += tg3.o
-obj-$(CONFIG_BGMAC) += bgmac.o bgmac-bcma-mdio.o
+obj-$(CONFIG_BGMAC) += bgmac.o
+obj-$(CONFIG_BGMAC_BCMA) += bgmac-bcma.o bgmac-bcma-mdio.o
+obj-$(CONFIG_BGMAC_PLATFORM) += bgmac-platform.o
 obj-$(CONFIG_SYSTEMPORT) += bcmsysport.o
 obj-$(CONFIG_BNXT) += bnxt/
diff --git a/drivers/net/ethernet/broadcom/bgmac-bcma.c 
b/drivers/net/ethernet/broadcom/bgmac-bcma.c
new file mode 100644
index 000..9a9745c4
--- /dev/null
+++ b/drivers/net/ethernet/broadcom/bgmac-bcma.c
@@ -0,0 +1,315 @@
+/*
+ * Driver for (BCM4706)? GBit MAC core on BCMA bus.
+ *
+ * Copyright (C) 2012 Rafał Miłecki 
+ *
+ * Licensed under the GNU/GPL. See COPYING for details.
+ */
+
+#define pr_fmt(fmt)KBUILD_MODNAME ": " fmt
+
+#include 
+#include 
+#include 
+#include "bgmac.h"
+
+static inline bool bgmac_is_bcm4707_family(struct bcma_device *core)
+{
+   switch (core->bus->chipinfo.id) {
+   case BCMA_CHIP_ID_BCM4707:
+   case BCMA_CHIP_ID_BCM47094:
+   case BCMA_CHIP_ID_BCM53018:
+   return true;
+   default:
+   return false;
+   }
+}
+
+/**
+ * BCMA bus ops
+ **/
+
+static u32 bcma_bgmac_read(struct bgmac *bgmac, u16 offset)
+{
+   return bcma_read32(bgmac->bcma.core, offset);
+}
+
+static void bcma_bgmac_write(struct bgmac *bgmac, u16 offset, u32 value)
+{
+   bcma_write32(bgmac->bcma.core, offset, value);
+}
+
+static u32 bcma_bgmac_idm_read(struct bgmac *bgmac, u16 offset)
+{
+   return bcma_aread32(bgmac->bcma.core, offset);
+}
+
+static void bcma_bgmac_idm_write(struct bgmac *bgmac, u16 offset, u32 value)
+{
+   return bcma_awrite32(bgmac->bcma.core, offset, value);
+}
+
+static bool bcma_bgmac_clk_enabled(struct bgmac *bgmac)
+{
+   return bcma_core_is_enabled(bgmac->bcma.core);
+}
+
+static void