Skip to contentSkip to conversation

device model bringup of cpsw on am335x bone black

PATCH Superseded Mugunthan V N 4 participants 17 messages Sep 2, 2015 – Sep 6, 2015

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover device model bringup of cpsw on am335x bone black

This patch seires enables cpsw to adopt driver model. This has been tested on AM335x beagle bone black (logs [1] with am335x_boneblack_defconfig and am335x_boneblack_vboot_defconfig. Also pushed a branch for testing [2]

[1]: http://pastebin.ubuntu.com/12252448/
[2]: git://git.ti.com/~mugunthanvnm/ti-u-boot/mugunth-ti-u-boot.git cpsw-dt-conversion

Mugunthan V N (6):
drivers: of: add support for get device address based on index
am335x_evm: prepare for eth driver model support
am335x_evm: do not define usb ether gadget when Eth DM is defined
drivers: net: cpsw: prepare driver for device model migration
drivers: net: cpsw: convert driver to adopt device driver model
defconfig: am335x: bbb: enable ethernet driver model

 board/ti/am335x/board.c                  |   7 +
 configs/am335x_boneblack_vboot_defconfig |   1 +
 drivers/core/device.c                    |  18 ++
 drivers/net/cpsw.c                       | 379 +++++++++++++++++++++++++++----
 include/configs/am335x_evm.h             |   3 +
 include/cpsw.h                           |   2 +
 include/dm/device.h                      |  10 +
 include/fdtdec.h                         |  14 ++
 lib/fdtdec.c                             |  35 +++
 9 files changed, 424 insertions(+), 45 deletions(-)
Signature · 1 line
2.5.1.522.g7aa67f6
#1 lore raw

PATCH 1/6 drivers: of: add support for get device address based on index

When a device has multiple memory regions, api "dev_get_addr" doesn't return the address and returns error. So implementing a new api to get device address based on index.

---
 drivers/core/device.c | 18 ++++++++++++++++++
 include/dm/device.h   | 10 ++++++++++
 include/fdtdec.h      | 14 ++++++++++++++
 lib/fdtdec.c          | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+)
drivers/core/device.c +18 -0
index a6cd936..398aad0 100644
@@ -591,6 +591,24 @@ fdt_addr_t dev_get_addr(struct udevice *dev)
 #endif
 }
 
+fdt_addr_t dev_get_addr_index(struct udevice *dev, int index)
+{
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+	fdt_addr_t addr;
+
+	addr = fdtdec_get_addr_size_index(gd->fdt_blob, dev->of_offset,
+					  "reg", NULL, index);
+	if (addr != FDT_ADDR_T_NONE) {
+		if (device_get_uclass_id(dev->parent) == UCLASS_SIMPLE_BUS)
+			addr = simple_bus_translate(dev->parent, addr);
+	}
+
+	return addr;
+#else
+	return FDT_ADDR_T_NONE;
+#endif
+}
+
 bool device_has_children(struct udevice *dev)
 {
 	return !list_empty(&dev->child_head);
include/dm/device.h +10 -0
index a239be6..dea04f8 100644
@@ -441,6 +441,16 @@ int device_find_next_child(struct udevice **devp);
 fdt_addr_t dev_get_addr(struct udevice *dev);
 
 /**
+ * dev_get_addr_index() - Get the index reg property of a device
+ *
+ * @dev: Pointer to a device
+ * @index: reg address index
+ *
+ * @return addr
+ */
+fdt_addr_t dev_get_addr_index(struct udevice *dev, int index);
+
+/**
  * device_has_children() - check if a device has any children
  *
  * @dev:	Device to check
include/fdtdec.h +14 -0
index 3e23731..b9e4ac6 100644
@@ -321,6 +321,20 @@ fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
 		const char *prop_name, fdt_size_t *sizep);
 
 /**
+ * Look up an address property in a node and return it as an address.
+ * The property must hold one address with a length. This is only tested
+ * on 32-bit machines.
+ *
+ * @param blob	FDT blob
+ * @param node	node to examine
+ * @param prop_name	name of property to find
+ * @param index		index of the address
+ * @return address, if found, or FDT_ADDR_T_NONE if not
+ */
+fdt_addr_t fdtdec_get_addr_size_index(const void *blob, int node,
+		const char *prop_name, fdt_size_t *sizep, int index);
+
+/**
  * Look at an address property in a node and return the pci address which
  * corresponds to the given type in the form of fdt_pci_addr.
  * The property must hold one fdt_pci_addr with a lengh.
lib/fdtdec.c +35 -0
index 29c5ccb..3afcf07 100644
@@ -119,6 +119,41 @@ fdt_addr_t fdtdec_get_addr(const void *blob, int node,
 	return fdtdec_get_addr_size(blob, node, prop_name, NULL);
 }
 
+fdt_addr_t fdtdec_get_addr_size_index(const void *blob, int node,
+		const char *prop_name, fdt_size_t *sizep, int index)
+{
+	const fdt_addr_t *cell;
+	int len;
+
+	debug("%s: %s: ", __func__, prop_name);
+	cell = fdt_getprop(blob, node, prop_name, &len);
+	if (len < sizeof(fdt_addr_t) * 2 * (index + 1)) {
+		debug("(not found)\n");
+		return FDT_ADDR_T_NONE;
+	}
+
+	len -= sizeof(fdt_addr_t) * 2 * index;
+	cell += index * 2;
+	if (cell && ((!sizep && len >= sizeof(fdt_addr_t)) ||
+		     len >= sizeof(fdt_addr_t) * 2)) {
+		fdt_addr_t addr = fdt_addr_to_cpu(*cell);
+		if (sizep) {
+			const fdt_size_t *size;
+
+			size = (fdt_size_t *)((char *)cell +
+					sizeof(fdt_addr_t));
+			*sizep = fdt_size_to_cpu(*size);
+			debug("addr=%08lx, size=%llx\n",
+			      (ulong)addr, (u64)*sizep);
+		} else {
+			debug("%08lx\n", (ulong)addr);
+		}
+		return addr;
+	}
+	debug("(not found)\n");
+	return FDT_ADDR_T_NONE;
+}
+
 #ifdef CONFIG_PCI
 int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
 		const char *prop_name, struct fdt_pci_addr *addr)
Signature · 1 line
2.5.1.522.g7aa67f6
#2 lore raw

PATCH 1/6 drivers: of: add support for get device address based on index

Hi,

Quoted 11 lines · Mugunthan V N

When a device has multiple memory regions, api "dev_get_addr" doesn't return the address and returns error. So implementing a new api to get device address based on index.

---
 drivers/core/device.c | 18 ++++++++++++++++++
 include/dm/device.h   | 10 ++++++++++
 include/fdtdec.h      | 14 ++++++++++++++
 lib/fdtdec.c          | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+)

I'd like to figure out Stephen Warren's patch before we look too hard at this one.

http://patchwork.ozlabs.org/patch/504918/

Regards,
Simon

#3 lore raw

PATCH 1/6 drivers: of: add support for get device address based on index

Quoted 15 lines · Simon Glass

Hi,

Quoted 11 lines · Mugunthan V N

When a device has multiple memory regions, api "dev_get_addr" doesn't return the address and returns error. So implementing a new api to get device address based on index.

---
 drivers/core/device.c | 18 ++++++++++++++++++
 include/dm/device.h   | 10 ++++++++++
 include/fdtdec.h      | 14 ++++++++++++++
 lib/fdtdec.c          | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+)
Quoted 5 lines

I'd like to figure out Stephen Warren's patch before we look too hard at this one.

http://patchwork.ozlabs.org/patch/504918/

The patch looks reasonable. Will test it tomorrow and repost the series again.

Regards
Mugunthan V N

#4 lore raw

PATCH 2/6 am335x_evm: prepare for eth driver model support

In reply to Mugunthan V N

Prepare board file so that ethernet registration are commented for DM conversion

---
 board/ti/am335x/board.c | 7 +++++++
 1 file changed, 7 insertions(+)
board/ti/am335x/board.c +7 -0
index 1dc2ed0..f0cb1e2 100644
@@ -38,7 +38,10 @@ DECLARE_GLOBAL_DATA_PTR;
 /* GPIO that controls power to DDR on EVM-SK */
 #define GPIO_DDR_VTT_EN		7
 
+#if defined(CONFIG_SPL_BUILD) || \
+	(defined(CONFIG_DRIVER_TI_CPSW) && !defined(CONFIG_DM_ETH))
 static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
+#endif
 
 /*
  * Read header information from EEPROM into global structure.
@@ -513,6 +516,8 @@ int board_late_init(void)
 }
 #endif
 
+#ifndef CONFIG_DM_ETH
+
 #if (defined(CONFIG_DRIVER_TI_CPSW) && !defined(CONFIG_SPL_BUILD)) || \
 	(defined(CONFIG_SPL_ETH_SUPPORT) && defined(CONFIG_SPL_BUILD))
 static void cpsw_control(int enabled)
@@ -670,3 +675,5 @@ int board_eth_init(bd_t *bis)
 	return n;
 }
 #endif
+
+#endif /* CONFIG_DM_ETH */
Signature · 1 line
2.5.1.522.g7aa67f6
#5 lore raw

PATCH 2/6 am335x_evm: prepare for eth driver model support

Quoted 4 lines · Mugunthan V N

Prepare board file so that ethernet registration are commented for DM conversion

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20150902/57b102a9/attachment.sig>
#7 lore raw

PATCH 3/6 am335x_evm: do not define usb ether gadget when Eth DM is defined

In reply to Mugunthan V N

Since usb ether gadget doesn't have support for driver model, so not defining usb ether gadget when ethernet driver model is defined.

---
 include/configs/am335x_evm.h | 3 +++
 1 file changed, 3 insertions(+)
include/configs/am335x_evm.h +3 -0
index e89c49e..56de3d4 100644
@@ -315,9 +315,12 @@
 #endif
 
 #ifdef CONFIG_USB_MUSB_GADGET
+/* Removing USB gadget and can be enabled adter adding support usb DM */
+#ifndef CONFIG_DM_ETH
 #define CONFIG_USB_ETHER
 #define CONFIG_USB_ETH_RNDIS
 #define CONFIG_USBNET_HOST_ADDR	"de:ad:be:af:00:00"
+#endif /* CONFIG_DM_ETH */
 
 /* USB TI's IDs */
 #define CONFIG_G_DNL_VENDOR_NUM 0x0451
Signature · 1 line
2.5.1.522.g7aa67f6
#8 lore raw

PATCH 3/6 am335x_evm: do not define usb ether gadget when Eth DM is defined

Quoted 5 lines · Mugunthan V N

Since usb ether gadget doesn't have support for driver model, so not defining usb ether gadget when ethernet driver model is defined.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20150902/4f9d4980/attachment.sig>
#9 lore raw

PATCH 4/6 drivers: net: cpsw: prepare driver for device model migration

In reply to Mugunthan V N

prepare driver for device model migration

---
 drivers/net/cpsw.c | 133 +++++++++++++++++++++++++++++++++++------------------
 1 file changed, 89 insertions(+), 44 deletions(-)
drivers/net/cpsw.c +89 -44
index fb4d621..a114d4d 100644
@@ -745,9 +745,8 @@ static int cpdma_process(struct cpsw_priv *priv, struct cpdma_chan *chan,
 	return 0;
 }
 
-static int cpsw_init(struct eth_device *dev, bd_t *bis)
+static int _cpsw_init(struct cpsw_priv *priv, u8 *enetaddr)
 {
-	struct cpsw_priv	*priv = dev->priv;
 	struct cpsw_slave	*slave;
 	int i, ret;
 
@@ -772,8 +771,7 @@ static int cpsw_init(struct eth_device *dev, bd_t *bis)
 
 	cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD);
 
-	cpsw_ale_add_ucast(priv, priv->dev->enetaddr, priv->host_port,
-			   ALE_SECURE);
+	cpsw_ale_add_ucast(priv, enetaddr, priv->host_port, ALE_SECURE);
 	cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << priv->host_port);
 
 	for_active_slave(slave, priv)
@@ -857,10 +855,8 @@ static int cpsw_init(struct eth_device *dev, bd_t *bis)
 	return 0;
 }
 
-static void cpsw_halt(struct eth_device *dev)
+static void _cpsw_halt(struct cpsw_priv *priv)
 {
-	struct cpsw_priv	*priv = dev->priv;
-
 	writel(0, priv->dma_regs + CPDMA_TXCONTROL);
 	writel(0, priv->dma_regs + CPDMA_RXCONTROL);
 
@@ -870,12 +866,10 @@ static void cpsw_halt(struct eth_device *dev)
 	/* clear dma state */
 	setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
 
-	priv->data.control(0);
 }
 
-static int cpsw_send(struct eth_device *dev, void *packet, int length)
+static int _cpsw_send(struct cpsw_priv *priv, void *packet, int length)
 {
-	struct cpsw_priv	*priv = dev->priv;
 	void *buffer;
 	int len;
 	int timeout = CPDMA_TIMEOUT;
@@ -896,20 +890,21 @@ static int cpsw_send(struct eth_device *dev, void *packet, int length)
 	return cpdma_submit(priv, &priv->tx_chan, packet, length);
 }
 
-static int cpsw_recv(struct eth_device *dev)
+static int _cpsw_recv(struct cpsw_priv *priv, uchar **pkt)
 {
-	struct cpsw_priv	*priv = dev->priv;
 	void *buffer;
 	int len;
+	int ret = -EAGAIN;
 
-	while (cpdma_process(priv, &priv->rx_chan, &buffer, &len) >= 0) {
-		invalidate_dcache_range((unsigned long)buffer,
-					(unsigned long)buffer + PKTSIZE_ALIGN);
-		net_process_received_packet(buffer, len);
-		cpdma_submit(priv, &priv->rx_chan, buffer, PKTSIZE);
-	}
+	ret = cpdma_process(priv, &priv->rx_chan, &buffer, &len);
+	if (ret < 0)
+		return ret;
 
-	return 0;
+	invalidate_dcache_range((unsigned long)buffer,
+				(unsigned long)buffer + PKTSIZE_ALIGN);
+	*pkt = buffer;
+
+	return len;
 }
 
 static void cpsw_slave_setup(struct cpsw_slave *slave, int slave_num,
@@ -923,15 +918,14 @@ static void cpsw_slave_setup(struct cpsw_slave *slave, int slave_num,
 	slave->sliver	= regs + data->sliver_reg_ofs;
 }
 
-static int cpsw_phy_init(struct eth_device *dev, struct cpsw_slave *slave)
+static int cpsw_phy_init(struct cpsw_priv *priv, struct cpsw_slave *slave)
 {
-	struct cpsw_priv *priv = (struct cpsw_priv *)dev->priv;
 	struct phy_device *phydev;
 	u32 supported = PHY_GBIT_FEATURES;
 
 	phydev = phy_connect(priv->bus,
 			slave->data->phy_addr,
-			dev,
+			priv->dev,
 			slave->data->phy_if);
 
 	if (!phydev)
@@ -946,30 +940,14 @@ static int cpsw_phy_init(struct eth_device *dev, struct cpsw_slave *slave)
 	return 1;
 }
 
-int cpsw_register(struct cpsw_platform_data *data)
+int _cpsw_register(struct cpsw_priv *priv)
 {
-	struct cpsw_priv	*priv;
 	struct cpsw_slave	*slave;
+	struct cpsw_platform_data *data = &priv->data;
 	void			*regs = (void *)data->cpsw_base;
-	struct eth_device	*dev;
-
-	dev = calloc(sizeof(*dev), 1);
-	if (!dev)
-		return -ENOMEM;
-
-	priv = calloc(sizeof(*priv), 1);
-	if (!priv) {
-		free(dev);
-		return -ENOMEM;
-	}
-
-	priv->data = *data;
-	priv->dev = dev;
 
 	priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves);
 	if (!priv->slaves) {
-		free(dev);
-		free(priv);
 		return -ENOMEM;
 	}
 
@@ -987,6 +965,70 @@ int cpsw_register(struct cpsw_platform_data *data)
 		idx = idx + 1;
 	}
 
+	cpsw_mdio_init(priv->dev->name, data->mdio_base, data->mdio_div);
+	priv->bus = miiphy_get_dev_by_name(priv->dev->name);
+	for_active_slave(slave, priv)
+		cpsw_phy_init(priv, slave);
+
+	return 0;
+}
+
+static int cpsw_init(struct eth_device *dev, bd_t *bis)
+{
+	struct cpsw_priv	*priv = dev->priv;
+
+	return _cpsw_init(priv, dev->enetaddr);
+}
+
+static void cpsw_halt(struct eth_device *dev)
+{
+	struct cpsw_priv *priv = dev->priv;
+
+	return _cpsw_halt(priv);
+}
+
+static int cpsw_send(struct eth_device *dev, void *packet, int length)
+{
+	struct cpsw_priv	*priv = dev->priv;
+
+	return _cpsw_send(priv, packet, length);
+}
+
+static int cpsw_recv(struct eth_device *dev)
+{
+	struct cpsw_priv *priv = dev->priv;
+	uchar *pkt = NULL;
+	int len;
+
+	len = _cpsw_recv(priv, &pkt);
+
+	if (len > 0) {
+		net_process_received_packet(pkt, len);
+		cpdma_submit(priv, &priv->rx_chan, pkt, PKTSIZE);
+	}
+
+	return len;
+}
+
+int cpsw_register(struct cpsw_platform_data *data)
+{
+	struct cpsw_priv	*priv;
+	struct eth_device	*dev;
+	int ret;
+
+	dev = calloc(sizeof(*dev), 1);
+	if (!dev)
+		return -ENOMEM;
+
+	priv = calloc(sizeof(*priv), 1);
+	if (!priv) {
+		free(dev);
+		return -ENOMEM;
+	}
+
+	priv->dev = dev;
+	priv->data = *data;
+
 	strcpy(dev->name, "cpsw");
 	dev->iobase	= 0;
 	dev->init	= cpsw_init;
@@ -997,10 +1039,13 @@ int cpsw_register(struct cpsw_platform_data *data)
 
 	eth_register(dev);
 
-	cpsw_mdio_init(dev->name, data->mdio_base, data->mdio_div);
-	priv->bus = miiphy_get_dev_by_name(dev->name);
-	for_active_slave(slave, priv)
-		cpsw_phy_init(dev, slave);
+	ret = _cpsw_register(priv);
+	if (ret < 0) {
+		eth_unregister(dev);
+		free(dev);
+		free(priv);
+		return ret;
+	}
 
 	return 1;
 }
Signature · 1 line
2.5.1.522.g7aa67f6
#11 lore raw

PATCH 5/6 drivers: net: cpsw: convert driver to adopt device driver model

In reply to Mugunthan V N

adopt cpsw driver to device driver model

---
 drivers/net/cpsw.c | 246 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 include/cpsw.h     |   2 +
 2 files changed, 247 insertions(+), 1 deletion(-)
drivers/net/cpsw.c +245 -1
index a114d4d..f982eb5 100644
@@ -25,6 +25,9 @@
 #include <asm/io.h>
 #include <phy.h>
 #include <asm/arch/cpu.h>
+#include <dm.h>
+
+DECLARE_GLOBAL_DATA_PTR;
 
 #define BITMASK(bits)		(BIT(bits) - 1)
 #define PHY_REG_MASK		0x1f
@@ -37,6 +40,23 @@
 #define FULLDUPLEXEN		BIT(0)
 #define MIIEN			BIT(15)
 
+/* reg offset */
+#define CPSW_HOST_PORT_OFFSET	0x108
+#define CPSW_SLAVE0_OFFSET	0x208
+#define CPSW_SLAVE1_OFFSET	0x308
+#define CPSW_SLAVE_SIZE		0x100
+#define CPSW_CPDMA_OFFSET	0x800
+#define CPSW_HW_STATS		0x900
+#define CPSW_STATERAM_OFFSET	0xa00
+#define CPSW_CPTS_OFFSET	0xc00
+#define CPSW_ALE_OFFSET		0xd00
+#define CPSW_SLIVER0_OFFSET	0xd80
+#define CPSW_SLIVER1_OFFSET	0xdc0
+#define CPSW_BD_OFFSET		0x2000
+#define CPSW_MDIO_DIV		0xff
+
+#define AM335X_GMII_SEL_OFFSET	0x630
+
 /* DMA Registers */
 #define CPDMA_TXCONTROL		0x004
 #define CPDMA_RXCONTROL		0x014
@@ -218,7 +238,11 @@ struct cpdma_chan {
 				(priv)->data.slaves; slave++)
 
 struct cpsw_priv {
+#ifdef CONFIG_DM_ETH
+	struct udevice			*dev;
+#else
 	struct eth_device		*dev;
+#endif
 	struct cpsw_platform_data	data;
 	int				host_port;
 
@@ -522,7 +546,7 @@ static int cpsw_mdio_write(struct mii_dev *bus, int phy_id, int dev_addr,
 	return 0;
 }
 
-static void cpsw_mdio_init(char *name, u32 mdio_base, u32 div)
+static void cpsw_mdio_init(const char *name, u32 mdio_base, u32 div)
 {
 	struct mii_dev *bus = mdio_alloc();
 
@@ -563,8 +587,15 @@ static inline void setbit_and_wait_for_clear32(void *addr)
 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
 			       struct cpsw_priv *priv)
 {
+#ifdef CONFIG_DM_ETH
+	struct eth_pdata *pdata = dev_get_platdata(priv->dev);
+
+	writel(mac_hi(pdata->enetaddr), &slave->regs->sa_hi);
+	writel(mac_lo(pdata->enetaddr), &slave->regs->sa_lo);
+#else
 	__raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi);
 	__raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo);
+#endif
 }
 
 static void cpsw_slave_update_link(struct cpsw_slave *slave,
@@ -973,6 +1004,7 @@ int _cpsw_register(struct cpsw_priv *priv)
 	return 0;
 }
 
+#ifndef CONFIG_DM_ETH
 static int cpsw_init(struct eth_device *dev, bd_t *bis)
 {
 	struct cpsw_priv	*priv = dev->priv;
@@ -1049,3 +1081,215 @@ int cpsw_register(struct cpsw_platform_data *data)
 
 	return 1;
 }
+#else
+static int cpsw_eth_start(struct udevice *dev)
+{
+	struct eth_pdata *pdata = dev_get_platdata(dev);
+	struct cpsw_priv *priv = dev_get_priv(dev);
+
+	return _cpsw_init(priv, pdata->enetaddr);
+}
+
+static int cpsw_eth_send(struct udevice *dev, void *packet, int length)
+{
+	struct cpsw_priv *priv = dev_get_priv(dev);
+
+	return _cpsw_send(priv, packet, length);
+}
+
+static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp)
+{
+	struct cpsw_priv *priv = dev_get_priv(dev);
+
+	return _cpsw_recv(priv, packetp);
+}
+
+static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet,
+				   int length)
+{
+	struct cpsw_priv *priv = dev_get_priv(dev);
+
+	return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE);
+}
+
+static void cpsw_eth_stop(struct udevice *dev)
+{
+	struct cpsw_priv *priv = dev_get_priv(dev);
+
+	return _cpsw_halt(priv);
+}
+
+
+static int cpsw_eth_probe(struct udevice *dev)
+{
+	struct cpsw_priv *priv = dev_get_priv(dev);
+
+	priv->dev = dev;
+
+	return _cpsw_register(priv);
+}
+
+static const struct eth_ops cpsw_eth_ops = {
+	.start		= cpsw_eth_start,
+	.send		= cpsw_eth_send,
+	.recv		= cpsw_eth_recv,
+	.free_pkt	= cpsw_eth_free_pkt,
+	.stop		= cpsw_eth_stop,
+};
+
+static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
+{
+	struct eth_pdata *pdata = dev_get_platdata(dev);
+	struct cpsw_priv *priv = dev_get_priv(dev);
+	const char *phy_mode;
+	const void *fdt = gd->fdt_blob;
+	int node = dev->of_offset;
+	int subnode;
+	int slave_index = 0;
+	uint32_t mac_hi, mac_lo;
+	fdt32_t gmii = 0;
+	int active_slave;
+
+	pdata->iobase = dev_get_addr_index(dev, 0);
+	priv->data.version = CPSW_CTRL_VERSION_2;
+	priv->data.bd_ram_ofs = CPSW_BD_OFFSET;
+	priv->data.ale_reg_ofs = CPSW_ALE_OFFSET;
+	priv->data.cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
+	priv->data.mdio_div = CPSW_MDIO_DIV;
+	priv->data.host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
+
+	pdata->phy_interface = -1;
+
+	priv->data.cpsw_base = pdata->iobase;
+	priv->data.mac_id = dev_get_addr_index(dev, 2);
+	priv->data.channels = fdtdec_get_int(fdt, node, "cpdma_channels", -1);
+	if (priv->data.channels <= 0) {
+		printf("error: cpdma_channels not found in dt\n");
+		return -ENOENT;
+	}
+
+	priv->data.slaves = fdtdec_get_int(fdt, node, "slaves", -1);
+	if (priv->data.slaves <= 0) {
+		printf("error: slaves not found in dt\n");
+		return -ENOENT;
+	}
+	priv->data.slave_data = malloc(sizeof(struct cpsw_slave_data) *
+				       priv->data.slaves);
+
+	priv->data.ale_entries = fdtdec_get_int(fdt, node, "ale_entries", -1);
+	if (priv->data.ale_entries <= 0) {
+		printf("error: ale_entries not found in dt\n");
+		return -ENOENT;
+	}
+
+	priv->data.bd_ram_ofs = fdtdec_get_int(fdt, node, "bd_ram_size", -1);
+	if (priv->data.bd_ram_ofs <= 0) {
+		printf("error: bd_ram_size not found in dt\n");
+		return -ENOENT;
+	}
+
+	priv->data.mac_control = fdtdec_get_int(fdt, node, "mac_control", -1);
+	if (priv->data.mac_control <= 0) {
+		printf("error: ale_entries not found in dt\n");
+		return -ENOENT;
+	}
+
+	active_slave = fdtdec_get_int(fdt, node, "active_slave", 0);
+	priv->data.active_slave = active_slave;
+
+	fdt_for_each_subnode(fdt, subnode, node) {
+		int len;
+		const char *name;
+
+		name = fdt_get_name(fdt, subnode, &len);
+		if (!strncmp(name, "mdio", 4)) {
+			priv->data.mdio_base = fdtdec_get_addr(fdt, subnode,
+							       "reg");
+		}
+
+		if (!strncmp(name, "slave", 5)) {
+			u32 phy_id[2];
+
+			if (slave_index >= priv->data.slaves) {
+				printf("error: num slaves and slave nodes did not match\n");
+				return -EINVAL;
+			}
+			phy_mode = fdt_getprop(fdt, subnode, "phy-mode", NULL);
+			if (phy_mode)
+				priv->data.slave_data[slave_index].phy_if =
+					phy_get_interface_by_name(phy_mode);
+			fdtdec_get_int_array(fdt, subnode, "phy_id", phy_id, 2);
+			priv->data.slave_data[slave_index].phy_addr = phy_id[1];
+			slave_index++;
+		}
+
+		if (!strncmp(name, "cpsw-phy-sel", 12)) {
+			priv->data.gmii_sel = fdtdec_get_addr(fdt, subnode,
+							      "reg");
+		}
+	}
+
+	priv->data.slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
+	priv->data.slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
+
+	if (priv->data.slaves == 2) {
+		priv->data.slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
+		priv->data.slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
+	}
+
+	subnode = fdtdec_lookup_phandle(fdt, node, "syscon");
+	priv->data.mac_id = fdt_translate_address((void *)fdt, subnode, &gmii);
+	priv->data.mac_id += AM335X_GMII_SEL_OFFSET;
+	priv->data.mac_id += active_slave * 8;
+
+	/* try reading mac address from efuse */
+	mac_lo = readl(priv->data.mac_id);
+	mac_hi = readl(priv->data.mac_id + 4);
+	pdata->enetaddr[0] = mac_hi & 0xFF;
+	pdata->enetaddr[1] = (mac_hi & 0xFF00) >> 8;
+	pdata->enetaddr[2] = (mac_hi & 0xFF0000) >> 16;
+	pdata->enetaddr[3] = (mac_hi & 0xFF000000) >> 24;
+	pdata->enetaddr[4] = mac_lo & 0xFF;
+	pdata->enetaddr[5] = (mac_lo & 0xFF00) >> 8;
+
+	pdata->phy_interface = priv->data.slave_data[active_slave].phy_if;
+	if (pdata->phy_interface == -1) {
+		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+		return -EINVAL;
+	}
+	switch (pdata->phy_interface) {
+	case PHY_INTERFACE_MODE_MII:
+		writel(MII_MODE_ENABLE, priv->data.gmii_sel);
+		break;
+	case PHY_INTERFACE_MODE_RMII:
+		writel(RMII_MODE_ENABLE, priv->data.gmii_sel);
+		break;
+	case PHY_INTERFACE_MODE_RGMII:
+	case PHY_INTERFACE_MODE_RGMII_ID:
+	case PHY_INTERFACE_MODE_RGMII_RXID:
+	case PHY_INTERFACE_MODE_RGMII_TXID:
+		writel(RGMII_MODE_ENABLE, priv->data.gmii_sel);
+		break;
+	}
+	return 0;
+}
+
+
+static const struct udevice_id cpsw_eth_ids[] = {
+	{ .compatible = "ti,cpsw" },
+	{ .compatible = "ti,am335x-cpsw" },
+	{ }
+};
+
+U_BOOT_DRIVER(eth_cpsw) = {
+	.name	= "eth_cpsw",
+	.id	= UCLASS_ETH,
+	.of_match = cpsw_eth_ids,
+	.ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
+	.probe	= cpsw_eth_probe,
+	.ops	= &cpsw_eth_ops,
+	.priv_auto_alloc_size = sizeof(struct cpsw_priv),
+	.platdata_auto_alloc_size = sizeof(struct eth_pdata),
+	.flags = DM_FLAG_ALLOC_PRIV_DMA,
+};
+#endif /* CONFIG_DM_ETH */
include/cpsw.h +2 -0
index 547b40c..cf1d30b 100644
@@ -31,6 +31,8 @@ enum {
 struct cpsw_platform_data {
 	u32	mdio_base;
 	u32	cpsw_base;
+	u32	mac_id;
+	u32	gmii_sel;
 	int	mdio_div;
 	int	channels;	/* number of cpdma channels (symmetric)	*/
 	u32	cpdma_reg_ofs;	/* cpdma register offset		*/
Signature · 1 line
2.5.1.522.g7aa67f6
#12 lore raw

PATCH 5/6 drivers: net: cpsw: convert driver to adopt device driver model

Hi,

Quoted 7 lines · Mugunthan V N

adopt cpsw driver to device driver model

---
 drivers/net/cpsw.c | 246 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 include/cpsw.h     |   2 +
 2 files changed, 247 insertions(+), 1 deletion(-)

This looks correct. A few thoughts below.

Quoted 174 lines
drivers/net/cpsw.c +119 -1
index a114d4d..f982eb5 100644
@@ -25,6 +25,9 @@
 #include <asm/io.h>
 #include <phy.h>
 #include <asm/arch/cpu.h>
+#include <dm.h>
+
+DECLARE_GLOBAL_DATA_PTR;
 
 #define BITMASK(bits)          (BIT(bits) - 1)
 #define PHY_REG_MASK           0x1f
@@ -37,6 +40,23 @@
 #define FULLDUPLEXEN           BIT(0)
 #define MIIEN                  BIT(15)
 
+/* reg offset */
+#define CPSW_HOST_PORT_OFFSET  0x108
+#define CPSW_SLAVE0_OFFSET     0x208
+#define CPSW_SLAVE1_OFFSET     0x308
+#define CPSW_SLAVE_SIZE                0x100
+#define CPSW_CPDMA_OFFSET      0x800
+#define CPSW_HW_STATS          0x900
+#define CPSW_STATERAM_OFFSET   0xa00
+#define CPSW_CPTS_OFFSET       0xc00
+#define CPSW_ALE_OFFSET                0xd00
+#define CPSW_SLIVER0_OFFSET    0xd80
+#define CPSW_SLIVER1_OFFSET    0xdc0
+#define CPSW_BD_OFFSET         0x2000
+#define CPSW_MDIO_DIV          0xff
+
+#define AM335X_GMII_SEL_OFFSET 0x630
+
 /* DMA Registers */
 #define CPDMA_TXCONTROL                0x004
 #define CPDMA_RXCONTROL                0x014
@@ -218,7 +238,11 @@ struct cpdma_chan {
                                (priv)->data.slaves; slave++)
 
 struct cpsw_priv {
+#ifdef CONFIG_DM_ETH
+       struct udevice                  *dev;
+#else
        struct eth_device               *dev;
+#endif
        struct cpsw_platform_data       data;
        int                             host_port;
 
@@ -522,7 +546,7 @@ static int cpsw_mdio_write(struct mii_dev *bus, int phy_id, int dev_addr,
        return 0;
 }
 
-static void cpsw_mdio_init(char *name, u32 mdio_base, u32 div)
+static void cpsw_mdio_init(const char *name, u32 mdio_base, u32 div)
 {
        struct mii_dev *bus = mdio_alloc();
 
@@ -563,8 +587,15 @@ static inline void setbit_and_wait_for_clear32(void *addr)
 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
                               struct cpsw_priv *priv)
 {
+#ifdef CONFIG_DM_ETH
+       struct eth_pdata *pdata = dev_get_platdata(priv->dev);
+
+       writel(mac_hi(pdata->enetaddr), &slave->regs->sa_hi);
+       writel(mac_lo(pdata->enetaddr), &slave->regs->sa_lo);
+#else
        __raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi);
        __raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo);
+#endif
 }
 
 static void cpsw_slave_update_link(struct cpsw_slave *slave,
@@ -973,6 +1004,7 @@ int _cpsw_register(struct cpsw_priv *priv)
        return 0;
 }
 
+#ifndef CONFIG_DM_ETH
 static int cpsw_init(struct eth_device *dev, bd_t *bis)
 {
        struct cpsw_priv        *priv = dev->priv;
@@ -1049,3 +1081,215 @@ int cpsw_register(struct cpsw_platform_data *data)
 
        return 1;
 }
+#else
+static int cpsw_eth_start(struct udevice *dev)
+{
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+       struct cpsw_priv *priv = dev_get_priv(dev);
+
+       return _cpsw_init(priv, pdata->enetaddr);
+}
+
+static int cpsw_eth_send(struct udevice *dev, void *packet, int length)
+{
+       struct cpsw_priv *priv = dev_get_priv(dev);
+
+       return _cpsw_send(priv, packet, length);
+}
+
+static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp)
+{
+       struct cpsw_priv *priv = dev_get_priv(dev);
+
+       return _cpsw_recv(priv, packetp);
+}
+
+static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet,
+                                  int length)
+{
+       struct cpsw_priv *priv = dev_get_priv(dev);
+
+       return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE);
+}
+
+static void cpsw_eth_stop(struct udevice *dev)
+{
+       struct cpsw_priv *priv = dev_get_priv(dev);
+
+       return _cpsw_halt(priv);
+}
+
+
+static int cpsw_eth_probe(struct udevice *dev)
+{
+       struct cpsw_priv *priv = dev_get_priv(dev);
+
+       priv->dev = dev;
+
+       return _cpsw_register(priv);
+}
+
+static const struct eth_ops cpsw_eth_ops = {
+       .start          = cpsw_eth_start,
+       .send           = cpsw_eth_send,
+       .recv           = cpsw_eth_recv,
+       .free_pkt       = cpsw_eth_free_pkt,
+       .stop           = cpsw_eth_stop,
+};
+
+static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
+{
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+       struct cpsw_priv *priv = dev_get_priv(dev);
+       const char *phy_mode;
+       const void *fdt = gd->fdt_blob;
+       int node = dev->of_offset;
+       int subnode;
+       int slave_index = 0;
+       uint32_t mac_hi, mac_lo;
+       fdt32_t gmii = 0;
+       int active_slave;
+
+       pdata->iobase = dev_get_addr_index(dev, 0);
+       priv->data.version = CPSW_CTRL_VERSION_2;
+       priv->data.bd_ram_ofs = CPSW_BD_OFFSET;
+       priv->data.ale_reg_ofs = CPSW_ALE_OFFSET;
+       priv->data.cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
+       priv->data.mdio_div = CPSW_MDIO_DIV;
+       priv->data.host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
+
+       pdata->phy_interface = -1;
+
+       priv->data.cpsw_base = pdata->iobase;
+       priv->data.mac_id = dev_get_addr_index(dev, 2);
+       priv->data.channels = fdtdec_get_int(fdt, node, "cpdma_channels", -1);
+       if (priv->data.channels <= 0) {
+               printf("error: cpdma_channels not found in dt\n");
+               return -ENOENT;
+       }

drivers/net/cpsw.c int cpsw_register(struct cpsw_platform_data *data) · line 1179

Quoted 10 lines
drivers/net/cpsw.c +10 -0
+
+       priv->data.slaves = fdtdec_get_int(fdt, node, "slaves", -1);
+       if (priv->data.slaves <= 0) {
+               printf("error: slaves not found in dt\n");
+               return -ENOENT;
+       }
+       priv->data.slave_data = malloc(sizeof(struct cpsw_slave_data) *
+                                      priv->data.slaves);
+
+       priv->data.ale_entries = fdtdec_get_int(fdt, node, "ale_entries", -1);

Is this device tree binding documented somewhere? If not, are you creating a new one?

Quoted 25 lines
drivers/net/cpsw.c +25 -0
+       if (priv->data.ale_entries <= 0) {
+               printf("error: ale_entries not found in dt\n");
+               return -ENOENT;
+       }
+
+       priv->data.bd_ram_ofs = fdtdec_get_int(fdt, node, "bd_ram_size", -1);
+       if (priv->data.bd_ram_ofs <= 0) {
+               printf("error: bd_ram_size not found in dt\n");
+               return -ENOENT;
+       }
+
+       priv->data.mac_control = fdtdec_get_int(fdt, node, "mac_control", -1);
+       if (priv->data.mac_control <= 0) {
+               printf("error: ale_entries not found in dt\n");
+               return -ENOENT;
+       }
+
+       active_slave = fdtdec_get_int(fdt, node, "active_slave", 0);
+       priv->data.active_slave = active_slave;
+
+       fdt_for_each_subnode(fdt, subnode, node) {
+               int len;
+               const char *name;
+
+               name = fdt_get_name(fdt, subnode, &len);

drivers/net/cpsw.c int cpsw_register(struct cpsw_platform_data *data) · line 1214

Quoted 10 lines
drivers/net/cpsw.c +10 -0
+               if (!strncmp(name, "mdio", 4)) {
+                       priv->data.mdio_base = fdtdec_get_addr(fdt, subnode,
+                                                              "reg");
+               }
+
+               if (!strncmp(name, "slave", 5)) {
+                       u32 phy_id[2];
+
+                       if (slave_index >= priv->data.slaves) {
+                               printf("error: num slaves and slave nodes did not match\n");

debug() ?

drivers/net/cpsw.c int cpsw_register(struct cpsw_platform_data *data) · line 1221

Quoted 7 lines
drivers/net/cpsw.c +7 -0
+                               return -EINVAL;
+                       }
+                       phy_mode = fdt_getprop(fdt, subnode, "phy-mode", NULL);
+                       if (phy_mode)
+                               priv->data.slave_data[slave_index].phy_if =
+                                       phy_get_interface_by_name(phy_mode);
+                       fdtdec_get_int_array(fdt, subnode, "phy_id", phy_id, 2);

Error check?

Quoted 20 lines
drivers/net/cpsw.c +20 -0
+                       priv->data.slave_data[slave_index].phy_addr = phy_id[1];
+                       slave_index++;
+               }
+
+               if (!strncmp(name, "cpsw-phy-sel", 12)) {
+                       priv->data.gmii_sel = fdtdec_get_addr(fdt, subnode,
+                                                             "reg");
+               }
+       }
+
+       priv->data.slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
+       priv->data.slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
+
+       if (priv->data.slaves == 2) {
+               priv->data.slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
+               priv->data.slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
+       }
+
+       subnode = fdtdec_lookup_phandle(fdt, node, "syscon");
+       priv->data.mac_id = fdt_translate_address((void *)fdt, subnode, &gmii);

drivers/net/cpsw.c int cpsw_register(struct cpsw_platform_data *data) · line 1251

Quoted 10 lines
drivers/net/cpsw.c +10 -0
+       priv->data.mac_id += AM335X_GMII_SEL_OFFSET;
+       priv->data.mac_id += active_slave * 8;
+
+       /* try reading mac address from efuse */
+       mac_lo = readl(priv->data.mac_id);
+       mac_hi = readl(priv->data.mac_id + 4);
+       pdata->enetaddr[0] = mac_hi & 0xFF;
+       pdata->enetaddr[1] = (mac_hi & 0xFF00) >> 8;
+       pdata->enetaddr[2] = (mac_hi & 0xFF0000) >> 16;
+       pdata->enetaddr[3] = (mac_hi & 0xFF000000) >> 24;

Can you use put_unaligned() or similar?

Quoted 58 lines
drivers/net/cpsw.c +44 -0
+       pdata->enetaddr[4] = mac_lo & 0xFF;
+       pdata->enetaddr[5] = (mac_lo & 0xFF00) >> 8;
+
+       pdata->phy_interface = priv->data.slave_data[active_slave].phy_if;
+       if (pdata->phy_interface == -1) {
+               debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+               return -EINVAL;
+       }
+       switch (pdata->phy_interface) {
+       case PHY_INTERFACE_MODE_MII:
+               writel(MII_MODE_ENABLE, priv->data.gmii_sel);
+               break;
+       case PHY_INTERFACE_MODE_RMII:
+               writel(RMII_MODE_ENABLE, priv->data.gmii_sel);
+               break;
+       case PHY_INTERFACE_MODE_RGMII:
+       case PHY_INTERFACE_MODE_RGMII_ID:
+       case PHY_INTERFACE_MODE_RGMII_RXID:
+       case PHY_INTERFACE_MODE_RGMII_TXID:
+               writel(RGMII_MODE_ENABLE, priv->data.gmii_sel);
+               break;
+       }
+       return 0;
+}
+
+
+static const struct udevice_id cpsw_eth_ids[] = {
+       { .compatible = "ti,cpsw" },
+       { .compatible = "ti,am335x-cpsw" },
+       { }
+};
+
+U_BOOT_DRIVER(eth_cpsw) = {
+       .name   = "eth_cpsw",
+       .id     = UCLASS_ETH,
+       .of_match = cpsw_eth_ids,
+       .ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
+       .probe  = cpsw_eth_probe,
+       .ops    = &cpsw_eth_ops,
+       .priv_auto_alloc_size = sizeof(struct cpsw_priv),
+       .platdata_auto_alloc_size = sizeof(struct eth_pdata),
+       .flags = DM_FLAG_ALLOC_PRIV_DMA,
+};
+#endif /* CONFIG_DM_ETH */
include/cpsw.h +2 -1
index 547b40c..cf1d30b 100644
@@ -31,6 +31,8 @@ enum {
 struct cpsw_platform_data {
        u32     mdio_base;
        u32     cpsw_base;
+       u32     mac_id;
+       u32     gmii_sel;
        int     mdio_div;
        int     channels;       /* number of cpdma channels (symmetric) */
        u32     cpdma_reg_ofs;  /* cpdma register offset                */
--
Quoted 2 lines

2.5.1.522.g7aa67f6

Regards,
Simon

#13 lore raw

PATCH 5/6 drivers: net: cpsw: convert driver to adopt device driver model

Quoted 194 lines · Simon Glass

Hi,

Quoted 7 lines · Mugunthan V N

adopt cpsw driver to device driver model

---
 drivers/net/cpsw.c | 246 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 include/cpsw.h     |   2 +
 2 files changed, 247 insertions(+), 1 deletion(-)

This looks correct. A few thoughts below.

Quoted 179 lines
drivers/net/cpsw.c +124 -1
index a114d4d..f982eb5 100644
@@ -25,6 +25,9 @@
 #include <asm/io.h>
 #include <phy.h>
 #include <asm/arch/cpu.h>
+#include <dm.h>
+
+DECLARE_GLOBAL_DATA_PTR;
 
 #define BITMASK(bits)          (BIT(bits) - 1)
 #define PHY_REG_MASK           0x1f
@@ -37,6 +40,23 @@
 #define FULLDUPLEXEN           BIT(0)
 #define MIIEN                  BIT(15)
 
+/* reg offset */
+#define CPSW_HOST_PORT_OFFSET  0x108
+#define CPSW_SLAVE0_OFFSET     0x208
+#define CPSW_SLAVE1_OFFSET     0x308
+#define CPSW_SLAVE_SIZE                0x100
+#define CPSW_CPDMA_OFFSET      0x800
+#define CPSW_HW_STATS          0x900
+#define CPSW_STATERAM_OFFSET   0xa00
+#define CPSW_CPTS_OFFSET       0xc00
+#define CPSW_ALE_OFFSET                0xd00
+#define CPSW_SLIVER0_OFFSET    0xd80
+#define CPSW_SLIVER1_OFFSET    0xdc0
+#define CPSW_BD_OFFSET         0x2000
+#define CPSW_MDIO_DIV          0xff
+
+#define AM335X_GMII_SEL_OFFSET 0x630
+
 /* DMA Registers */
 #define CPDMA_TXCONTROL                0x004
 #define CPDMA_RXCONTROL                0x014
@@ -218,7 +238,11 @@ struct cpdma_chan {
                                (priv)->data.slaves; slave++)
 
 struct cpsw_priv {
+#ifdef CONFIG_DM_ETH
+       struct udevice                  *dev;
+#else
        struct eth_device               *dev;
+#endif
        struct cpsw_platform_data       data;
        int                             host_port;
 
@@ -522,7 +546,7 @@ static int cpsw_mdio_write(struct mii_dev *bus, int phy_id, int dev_addr,
        return 0;
 }
 
-static void cpsw_mdio_init(char *name, u32 mdio_base, u32 div)
+static void cpsw_mdio_init(const char *name, u32 mdio_base, u32 div)
 {
        struct mii_dev *bus = mdio_alloc();
 
@@ -563,8 +587,15 @@ static inline void setbit_and_wait_for_clear32(void *addr)
 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
                               struct cpsw_priv *priv)
 {
+#ifdef CONFIG_DM_ETH
+       struct eth_pdata *pdata = dev_get_platdata(priv->dev);
+
+       writel(mac_hi(pdata->enetaddr), &slave->regs->sa_hi);
+       writel(mac_lo(pdata->enetaddr), &slave->regs->sa_lo);
+#else
        __raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi);
        __raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo);
+#endif
 }
 
 static void cpsw_slave_update_link(struct cpsw_slave *slave,
@@ -973,6 +1004,7 @@ int _cpsw_register(struct cpsw_priv *priv)
        return 0;
 }
 
+#ifndef CONFIG_DM_ETH
 static int cpsw_init(struct eth_device *dev, bd_t *bis)
 {
        struct cpsw_priv        *priv = dev->priv;
@@ -1049,3 +1081,215 @@ int cpsw_register(struct cpsw_platform_data *data)
 
        return 1;
 }
+#else
+static int cpsw_eth_start(struct udevice *dev)
+{
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+       struct cpsw_priv *priv = dev_get_priv(dev);
+
+       return _cpsw_init(priv, pdata->enetaddr);
+}
+
+static int cpsw_eth_send(struct udevice *dev, void *packet, int length)
+{
+       struct cpsw_priv *priv = dev_get_priv(dev);
+
+       return _cpsw_send(priv, packet, length);
+}
+
+static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp)
+{
+       struct cpsw_priv *priv = dev_get_priv(dev);
+
+       return _cpsw_recv(priv, packetp);
+}
+
+static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet,
+                                  int length)
+{
+       struct cpsw_priv *priv = dev_get_priv(dev);
+
+       return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE);
+}
+
+static void cpsw_eth_stop(struct udevice *dev)
+{
+       struct cpsw_priv *priv = dev_get_priv(dev);
+
+       return _cpsw_halt(priv);
+}
+
+
+static int cpsw_eth_probe(struct udevice *dev)
+{
+       struct cpsw_priv *priv = dev_get_priv(dev);
+
+       priv->dev = dev;
+
+       return _cpsw_register(priv);
+}
+
+static const struct eth_ops cpsw_eth_ops = {
+       .start          = cpsw_eth_start,
+       .send           = cpsw_eth_send,
+       .recv           = cpsw_eth_recv,
+       .free_pkt       = cpsw_eth_free_pkt,
+       .stop           = cpsw_eth_stop,
+};
+
+static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
+{
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+       struct cpsw_priv *priv = dev_get_priv(dev);
+       const char *phy_mode;
+       const void *fdt = gd->fdt_blob;
+       int node = dev->of_offset;
+       int subnode;
+       int slave_index = 0;
+       uint32_t mac_hi, mac_lo;
+       fdt32_t gmii = 0;
+       int active_slave;
+
+       pdata->iobase = dev_get_addr_index(dev, 0);
+       priv->data.version = CPSW_CTRL_VERSION_2;
+       priv->data.bd_ram_ofs = CPSW_BD_OFFSET;
+       priv->data.ale_reg_ofs = CPSW_ALE_OFFSET;
+       priv->data.cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
+       priv->data.mdio_div = CPSW_MDIO_DIV;
+       priv->data.host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
+
+       pdata->phy_interface = -1;
+
+       priv->data.cpsw_base = pdata->iobase;
+       priv->data.mac_id = dev_get_addr_index(dev, 2);
+       priv->data.channels = fdtdec_get_int(fdt, node, "cpdma_channels", -1);
+       if (priv->data.channels <= 0) {
+               printf("error: cpdma_channels not found in dt\n");
+               return -ENOENT;
+       }
+
+       priv->data.slaves = fdtdec_get_int(fdt, node, "slaves", -1);
+       if (priv->data.slaves <= 0) {
+               printf("error: slaves not found in dt\n");
+               return -ENOENT;
Quoted 8 lines

drivers/net/cpsw.c int cpsw_register(struct cpsw_platform_data *data) · line 1179

Quoted 5 lines
drivers/net/cpsw.c +5 -0
+       }
+       priv->data.slave_data = malloc(sizeof(struct cpsw_slave_data) *
+                                      priv->data.slaves);
+
+       priv->data.ale_entries = fdtdec_get_int(fdt, node, "ale_entries", -1);

Is this device tree binding documented somewhere? If not, are you creating a new one?

It is documented in kernel binding document
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/devicetree/bindings/net/cpsw.txt?id=refs/tags/v4.2

Quoted 30 lines
Quoted 30 lines
drivers/net/cpsw.c +29 -0
+       if (priv->data.ale_entries <= 0) {
+               printf("error: ale_entries not found in dt\n");
+               return -ENOENT;
+       }
+
+       priv->data.bd_ram_ofs = fdtdec_get_int(fdt, node, "bd_ram_size", -1);
+       if (priv->data.bd_ram_ofs <= 0) {
+               printf("error: bd_ram_size not found in dt\n");
+               return -ENOENT;
+       }
+
+       priv->data.mac_control = fdtdec_get_int(fdt, node, "mac_control", -1);
+       if (priv->data.mac_control <= 0) {
+               printf("error: ale_entries not found in dt\n");
+               return -ENOENT;
+       }
+
+       active_slave = fdtdec_get_int(fdt, node, "active_slave", 0);
+       priv->data.active_slave = active_slave;
+
+       fdt_for_each_subnode(fdt, subnode, node) {
+               int len;
+               const char *name;
+
+               name = fdt_get_name(fdt, subnode, &len);
+               if (!strncmp(name, "mdio", 4)) {
+                       priv->data.mdio_base = fdtdec_get_addr(fdt, subnode,
+                                                              "reg");
+               }
Quoted 8 lines

drivers/net/cpsw.c int cpsw_register(struct cpsw_platform_data *data) · line 1214

Quoted 6 lines
drivers/net/cpsw.c +6 -0
+
+               if (!strncmp(name, "slave", 5)) {
+                       u32 phy_id[2];
+
+                       if (slave_index >= priv->data.slaves) {
+                               printf("error: num slaves and slave nodes did not match\n");

debug() ?

I wanted this to be error the debug, so used printf.

Quoted 10 lines

drivers/net/cpsw.c int cpsw_register(struct cpsw_platform_data *data) · line 1221

Quoted 8 lines
drivers/net/cpsw.c +7 -0
+                               return -EINVAL;
+                       }
+                       phy_mode = fdt_getprop(fdt, subnode, "phy-mode", NULL);
+                       if (phy_mode)
+                               priv->data.slave_data[slave_index].phy_if =
+                                       phy_get_interface_by_name(phy_mode);
+                       fdtdec_get_int_array(fdt, subnode, "phy_id", phy_id, 2);

Error check?

phy_id is optional node. It will be populated only when phy is connected on board.

Quoted 25 lines
Quoted 25 lines
drivers/net/cpsw.c +24 -0
+                       priv->data.slave_data[slave_index].phy_addr = phy_id[1];
+                       slave_index++;
+               }
+
+               if (!strncmp(name, "cpsw-phy-sel", 12)) {
+                       priv->data.gmii_sel = fdtdec_get_addr(fdt, subnode,
+                                                             "reg");
+               }
+       }
+
+       priv->data.slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
+       priv->data.slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
+
+       if (priv->data.slaves == 2) {
+               priv->data.slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
+               priv->data.slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
+       }
+
+       subnode = fdtdec_lookup_phandle(fdt, node, "syscon");
+       priv->data.mac_id = fdt_translate_address((void *)fdt, subnode, &gmii);
+       priv->data.mac_id += AM335X_GMII_SEL_OFFSET;
+       priv->data.mac_id += active_slave * 8;
+
+       /* try reading mac address from efuse */
Quoted 8 lines

drivers/net/cpsw.c int cpsw_register(struct cpsw_platform_data *data) · line 1251

Quoted 6 lines
drivers/net/cpsw.c +6 -0
+       mac_lo = readl(priv->data.mac_id);
+       mac_hi = readl(priv->data.mac_id + 4);
+       pdata->enetaddr[0] = mac_hi & 0xFF;
+       pdata->enetaddr[1] = (mac_hi & 0xFF00) >> 8;
+       pdata->enetaddr[2] = (mac_hi & 0xFF0000) >> 16;
+       pdata->enetaddr[3] = (mac_hi & 0xFF000000) >> 24;

Can you use put_unaligned() or similar?

This will vary based on platform. In DRA7 mac id combination is different.

Regards
Mugunthan V N

#14 lore raw

PATCH 6/6 defconfig: am335x: bbb: enable ethernet driver model

In reply to Mugunthan V N

enable ethernet driver model for am335x beagle bone black as cpsw supports driver model

---
 configs/am335x_boneblack_vboot_defconfig | 1 +
 1 file changed, 1 insertion(+)
configs/am335x_boneblack_vboot_defconfig +1 -0
index b52ddfd..117a146 100644
@@ -13,3 +13,4 @@ CONFIG_SYS_EXTRA_OPTIONS="EMMC_BOOT,ENABLE_VBOOT"
 # CONFIG_CMD_SETEXPR is not set
 CONFIG_OF_CONTROL=y
 CONFIG_SPI_FLASH=y
+CONFIG_DM_ETH=y
Signature · 1 line
2.5.1.522.g7aa67f6
#15 lore raw

PATCH 6/6 defconfig: am335x: bbb: enable ethernet driver model

Quoted 4 lines · Mugunthan V N

enable ethernet driver model for am335x beagle bone black as cpsw supports driver model

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20150902/8a3559e8/attachment.sig>
#16 lore raw

cover device model bringup of cpsw on am335x bone black

In reply to Mugunthan V N
Quoted 7 lines · Mugunthan V N

This patch seires enables cpsw to adopt driver model. This has been tested on AM335x beagle bone black (logs [1] with am335x_boneblack_defconfig and am335x_boneblack_vboot_defconfig. Also pushed a branch for testing [2]

[1]: http://pastebin.ubuntu.com/12252448/
[2]: git://git.ti.com/~mugunthanvnm/ti-u-boot/mugunth-ti-u-boot.git cpsw-dt-conversion

Looks OK overall, and I'll let Simon comment on the DM portions. Can you please also enable and test am335x_gp_evm? Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20150902/949a9cc2/attachment.sig>