Skip to contentSkip to conversation

hw/arm: add ethernet support to Allwinner A10

PATCH Applied Beniamino Galvani 3 participants 8 messages Jan 30, 2014 – Feb 8, 2014 Arm/Allwinner sunXi SoC support

lore
Share & tools
lore
#0 lore raw

v5 cover hw/arm: add ethernet support to Allwinner A10

This patch series adds support for the EMAC Fast Ethernet controller found on Allwinner SoCs to the Allwinner A10.

Changelog:
v5: Address comments from Peter Crosthwaite and Peter Maydell:
* Move reinitialization of fifo head to a new patch
* Remove fifo8_has_space() and add fifo8_num_free(),fifo8_num_used()
* Improve documentation of new fifo8 functions
* Add restriction on fifo8_pop_buf() 'max' argument as originally suggested by Peter C.: it must not be greater than the number of bytes in the fifo
* Change license of EMAC model to GPLv2
* Check if tx length register is greater than data in fifo

v4: Address comments from Peter Crosthwaite and Andreas Färber:
* Add functions for multiple bytes push/pop to Fifo8
* Get rid of custom fifo implementation and use generic one for both tx and rx
* Rename aw_emac_mdio_read -> RTL8201CP_mdio_read
* Allow reception of new packets from net layer only when there is space for a full size frame
* Change TYPE_AW_EMAC from "allwinner_emac" to "allwinner-emac"
* Property "phyaddr" -> "phy-addr"
* Remove trailing newlines from error_report() messages

v3: Address comments from Peter Crosthwaite and Stefan Hajnoczi:
* Use a single, big rx fifo instead of per-packet fifo
* Remove link_ok field from PHY state
* Call set_link() on post_load
* Add missing PHY registers
* Implement reset() method
* Rename AwEmacMii -> RTL8201CPState
* Rename mii_{read,write} -> aw_emac_mdio_{read,write}

v2: Address Peter Crosthwaite's comments:
* Make phy address customizable through a property
* Call qemu_flush_queued_packets() when rx becomes possible
* Always create EMAC instance in SoC
* Use uint8 arrays for fifos
* Minor cleanups

Beniamino Galvani (4):
util/fifo8: implement push/pop of multiple bytes util/fifo8: clear fifo head upon reset
hw/net: add support for Allwinner EMAC Fast Ethernet controller hw/arm/allwinner-a10: initialize EMAC

 default-configs/arm-softmmu.mak |    1 +
 hw/arm/allwinner-a10.c          |   16 ++
 hw/arm/cubieboard.c             |   11 +-
 hw/net/Makefile.objs            |    1 +
 hw/net/allwinner_emac.c         |  539 +++++++++++++++++++++++++++++++++++++++
 include/hw/arm/allwinner-a10.h  |    3 +
 include/hw/net/allwinner_emac.h |  210 +++++++++++++++
 include/qemu/fifo8.h            |   61 +++++
 util/fifo8.c                    |   47 ++++
 9 files changed, 887 insertions(+), 2 deletions(-)
 create mode 100644 hw/net/allwinner_emac.c
 create mode 100644 include/hw/net/allwinner_emac.h
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v5 1/4 util/fifo8: implement push/pop of multiple bytes

The patch adds functions fifo8_push_all() and fifo8_pop_buf() which can be used respectively to push the content of a memory buffer to the fifo and to pop multiple bytes obtaining a pointer to the fifo backing buffer.

In addition, it implements fifo8_num_free() and fifo8_num_used() which allow to check if a multi-byte operation can be performed.

---
 include/qemu/fifo8.h |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++
 util/fifo8.c         |   46 +++++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)
include/qemu/fifo8.h +61 -0
index d318f71..8820780 100644
@@ -44,6 +44,19 @@ void fifo8_destroy(Fifo8 *fifo);
 void fifo8_push(Fifo8 *fifo, uint8_t data);
 
 /**
+ * fifo8_push_all:
+ * @fifo: FIFO to push to
+ * @data: data to push
+ * @size: number of bytes to push
+ *
+ * Push a byte array to the FIFO. Behaviour is undefined if the FIFO is full.
+ * Clients are responsible for checking the space left in the FIFO using
+ * fifo8_num_free().
+ */
+
+void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
+
+/**
  * fifo8_pop:
  * @fifo: fifo to pop from
  *
@@ -56,6 +69,32 @@ void fifo8_push(Fifo8 *fifo, uint8_t data);
 uint8_t fifo8_pop(Fifo8 *fifo);
 
 /**
+ * fifo8_pop_buf:
+ * @fifo: FIFO to pop from
+ * @max: maximum number of bytes to pop
+ * @num: actual number of returned bytes
+ *
+ * Pop a number of elements from the FIFO up to a maximum of max. The buffer
+ * containing the popped data is returned. This buffer points directly into
+ * the FIFO backing store and data is invalidated once any of the fifo8_* APIs
+ * are called on the FIFO.
+ *
+ * The function may return fewer bytes than requested when the data wraps
+ * around in the ring buffer; in this case only a contiguous part of the data
+ * is returned.
+ *
+ * The number of valid bytes returned is populated in *num; will always return
+ * at least 1 byte. max must not be 0 or greater than the number of bytes in
+ * the FIFO.
+ *
+ * Clients are responsible for checking the availability of requested data
+ * using fifo8_num_used().
+ *
+ * Returns: A pointer to popped data.
+ */
+const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *num);
+
+/**
  * fifo8_reset:
  * @fifo: FIFO to reset
  *
@@ -86,6 +125,28 @@ bool fifo8_is_empty(Fifo8 *fifo);
 
 bool fifo8_is_full(Fifo8 *fifo);
 
+/**
+ * fifo8_num_free:
+ * @fifo: FIFO to check
+ *
+ * Return the number of free bytes in the FIFO.
+ *
+ * Returns: Number of free bytes.
+ */
+
+uint32_t fifo8_num_free(Fifo8 *fifo);
+
+/**
+ * fifo8_num_used:
+ * @fifo: FIFO to check
+ *
+ * Return the number of used bytes in the FIFO.
+ *
+ * Returns: Number of used bytes.
+ */
+
+uint32_t fifo8_num_used(Fifo8 *fifo);
+
 extern const VMStateDescription vmstate_fifo8;
 
 #define VMSTATE_FIFO8(_field, _state) {                              \
util/fifo8.c +46 -0

64 lines not on this page · raw message

Signature · 1 line
1.7.10.4
Reviewed-by on v5 1/4
#2 lore raw

Re: [Qemu-devel] [PATCH v5 1/4] util/fifo8: implement push/pop of multiple bytes

Quoted 8 lines · Beniamino Galvani

The patch adds functions fifo8_push_all() and fifo8_pop_buf() which can be used respectively to push the content of a memory buffer to the fifo and to pop multiple bytes obtaining a pointer to the fifo backing buffer.

In addition, it implements fifo8_num_free() and fifo8_num_used() which allow to check if a multi-byte operation can be performed.

In an ideal world its two patches. Although its totally not worth a respin if Peter is happy to take as-is.

Quoted 1 line
Quoted 166 lines
---
 include/qemu/fifo8.h |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++
 util/fifo8.c         |   46 +++++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)
include/qemu/fifo8.h +61 -0
index d318f71..8820780 100644
@@ -44,6 +44,19 @@ void fifo8_destroy(Fifo8 *fifo);
 void fifo8_push(Fifo8 *fifo, uint8_t data);
 
 /**
+ * fifo8_push_all:
+ * @fifo: FIFO to push to
+ * @data: data to push
+ * @size: number of bytes to push
+ *
+ * Push a byte array to the FIFO. Behaviour is undefined if the FIFO is full.
+ * Clients are responsible for checking the space left in the FIFO using
+ * fifo8_num_free().
+ */
+
+void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num);
+
+/**
  * fifo8_pop:
  * @fifo: fifo to pop from
  *
@@ -56,6 +69,32 @@ void fifo8_push(Fifo8 *fifo, uint8_t data);
 uint8_t fifo8_pop(Fifo8 *fifo);
 
 /**
+ * fifo8_pop_buf:
+ * @fifo: FIFO to pop from
+ * @max: maximum number of bytes to pop
+ * @num: actual number of returned bytes
+ *
+ * Pop a number of elements from the FIFO up to a maximum of max. The buffer
+ * containing the popped data is returned. This buffer points directly into
+ * the FIFO backing store and data is invalidated once any of the fifo8_* APIs
+ * are called on the FIFO.
+ *
+ * The function may return fewer bytes than requested when the data wraps
+ * around in the ring buffer; in this case only a contiguous part of the data
+ * is returned.
+ *
+ * The number of valid bytes returned is populated in *num; will always return
+ * at least 1 byte. max must not be 0 or greater than the number of bytes in
+ * the FIFO.
+ *
+ * Clients are responsible for checking the availability of requested data
+ * using fifo8_num_used().
+ *
+ * Returns: A pointer to popped data.
+ */
+const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *num);
+
+/**
  * fifo8_reset:
  * @fifo: FIFO to reset
  *
@@ -86,6 +125,28 @@ bool fifo8_is_empty(Fifo8 *fifo);
 
 bool fifo8_is_full(Fifo8 *fifo);
 
+/**
+ * fifo8_num_free:
+ * @fifo: FIFO to check
+ *
+ * Return the number of free bytes in the FIFO.
+ *
+ * Returns: Number of free bytes.
+ */
+
+uint32_t fifo8_num_free(Fifo8 *fifo);
+
+/**
+ * fifo8_num_used:
+ * @fifo: FIFO to check
+ *
+ * Return the number of used bytes in the FIFO.
+ *
+ * Returns: Number of used bytes.
+ */
+
+uint32_t fifo8_num_used(Fifo8 *fifo);
+
 extern const VMStateDescription vmstate_fifo8;
 
 #define VMSTATE_FIFO8(_field, _state) {                              \
util/fifo8.c +46 -1
index 013e903..a7503c2 100644
@@ -37,6 +37,27 @@ void fifo8_push(Fifo8 *fifo, uint8_t data)
     fifo->num++;
 }
 
+void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num)
+{
+    uint32_t start, avail;
+
+    if (fifo->num + num > fifo->capacity) {
+        abort();
+    }
+
+    start = (fifo->head + fifo->num) % fifo->capacity;
+
+    if (start + num <= fifo->capacity) {
+        memcpy(&fifo->data[start], data, num);
+    } else {
+        avail = fifo->capacity - start;
+        memcpy(&fifo->data[start], data, avail);
+        memcpy(&fifo->data[0], &data[avail], num - avail);
+    }
+
+    fifo->num += num;
+}
+
 uint8_t fifo8_pop(Fifo8 *fifo)
 {
     uint8_t ret;
@@ -50,6 +71,21 @@ uint8_t fifo8_pop(Fifo8 *fifo)
     return ret;
 }
 
+const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *num)
+{
+    uint8_t *ret;
+
+    if (max == 0 || max > fifo->num) {
+        abort();
+    }
+    *num = MIN(fifo->capacity - fifo->head, max);
+    ret = &fifo->data[fifo->head];
+    fifo->head += *num;
+    fifo->head %= fifo->capacity;
+    fifo->num -= *num;
+    return ret;
+}
+
 void fifo8_reset(Fifo8 *fifo)
 {
     fifo->num = 0;
@@ -65,6 +101,16 @@ bool fifo8_is_full(Fifo8 *fifo)
     return (fifo->num == fifo->capacity);
 }
 
+uint32_t fifo8_num_free(Fifo8 *fifo)
+{
+    return fifo->capacity - fifo->num;
+}
+
+uint32_t fifo8_num_used(Fifo8 *fifo)
+{
+    return fifo->num;
+}
+
 const VMStateDescription vmstate_fifo8 = {
     .name = "Fifo8",
     .version_id = 1,
--

1.7.10.4

#3 lore raw

PATCH v5 2/4 util/fifo8: clear fifo head upon reset

In reply to Beniamino Galvani

To improve the predictability of fifo8_pop_buf(), the fifo head is set to start of data buffer upon a reset so that the first call to the function will be able to retrieve all data in the fifo.

---
 util/fifo8.c |    1 +
 1 file changed, 1 insertion(+)
util/fifo8.c +1 -0
index a7503c2..6a43482 100644
@@ -89,6 +89,7 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *num)
 void fifo8_reset(Fifo8 *fifo)
 {
     fifo->num = 0;
+    fifo->head = 0;
 }
 
 bool fifo8_is_empty(Fifo8 *fifo)
Signature · 1 line
1.7.10.4
Reviewed-by on v5 2/4
#4 lore raw

Re: [Qemu-devel] [PATCH v5 2/4] util/fifo8: clear fifo head upon reset

Quoted 2 lines · Beniamino Galvani

To improve the predictability of fifo8_pop_buf(), the fifo head is set to start of data buffer upon a reset so that the first call to the

A nit:

"the start of the"

Quoted 3 lines

function will be able to retrieve all data in the fifo.

Thanks for your contributions.

Regards,
Peter

Quoted 20 lines
---
 util/fifo8.c |    1 +
 1 file changed, 1 insertion(+)
util/fifo8.c +1 -1
index a7503c2..6a43482 100644
@@ -89,6 +89,7 @@ const uint8_t *fifo8_pop_buf(Fifo8 *fifo, uint32_t max, uint32_t *num)
 void fifo8_reset(Fifo8 *fifo)
 {
     fifo->num = 0;
+    fifo->head = 0;
 }
 
 bool fifo8_is_empty(Fifo8 *fifo)
--

1.7.10.4

#5 lore raw

PATCH v5 3/4 hw/net: add support for Allwinner EMAC Fast Ethernet controller

In reply to Beniamino Galvani

This patch adds support for the Fast Ethernet MAC found on Allwinner SoCs, together with a basic emulation of Realtek RTL8201CP PHY.

Since there is no public documentation of the Allwinner controller, the implementation is based on Linux kernel driver.

---
 default-configs/arm-softmmu.mak |    1 +
 hw/net/Makefile.objs            |    1 +
 hw/net/allwinner_emac.c         |  539 +++++++++++++++++++++++++++++++++++++++
 include/hw/net/allwinner_emac.h |  210 +++++++++++++++
 4 files changed, 751 insertions(+)
 create mode 100644 hw/net/allwinner_emac.c
 create mode 100644 include/hw/net/allwinner_emac.h
default-configs/arm-softmmu.mak +1 -0
index ce1d620..f3513fa 100644
@@ -27,6 +27,7 @@ CONFIG_SSI_SD=y
 CONFIG_SSI_M25P80=y
 CONFIG_LAN9118=y
 CONFIG_SMC91C111=y
+CONFIG_ALLWINNER_EMAC=y
 CONFIG_DS1338=y
 CONFIG_PFLASH_CFI01=y
 CONFIG_PFLASH_CFI02=y
hw/net/Makefile.objs +1 -0
index 951cca3..75e80c2 100644
@@ -18,6 +18,7 @@ common-obj-$(CONFIG_OPENCORES_ETH) += opencores_eth.o
 common-obj-$(CONFIG_XGMAC) += xgmac.o
 common-obj-$(CONFIG_MIPSNET) += mipsnet.o
 common-obj-$(CONFIG_XILINX_AXI) += xilinx_axienet.o
+common-obj-$(CONFIG_ALLWINNER_EMAC) += allwinner_emac.o
 
 common-obj-$(CONFIG_CADENCE) += cadence_gem.o
 common-obj-$(CONFIG_STELLARIS_ENET) += stellaris_enet.o
hw/net/allwinner_emac.c new file +539 -0

539 lines not on this page · raw message

include/hw/net/allwinner_emac.h new file +210 -0

210 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#6 lore raw

PATCH v5 4/4 hw/arm/allwinner-a10: initialize EMAC

In reply to Beniamino Galvani
---
 hw/arm/allwinner-a10.c         |   16 ++++++++++++++++
 hw/arm/cubieboard.c            |   11 +++++++++--
 include/hw/arm/allwinner-a10.h |    3 +++
 3 files changed, 28 insertions(+), 2 deletions(-)
hw/arm/allwinner-a10.c +16 -0
index 4658e19..01206f2 100644
@@ -31,6 +31,13 @@ static void aw_a10_init(Object *obj)
 
     object_initialize(&s->timer, sizeof(s->timer), TYPE_AW_A10_PIT);
     qdev_set_parent_bus(DEVICE(&s->timer), sysbus_get_default());
+
+    object_initialize(&s->emac, sizeof(s->emac), TYPE_AW_EMAC);
+    qdev_set_parent_bus(DEVICE(&s->emac), sysbus_get_default());
+    if (nd_table[0].used) {
+        qemu_check_nic_model(&nd_table[0], TYPE_AW_EMAC);
+        qdev_set_nic_properties(DEVICE(&s->emac), &nd_table[0]);
+    }
 }
 
 static void aw_a10_realize(DeviceState *dev, Error **errp)
@@ -76,6 +83,15 @@ static void aw_a10_realize(DeviceState *dev, Error **errp)
     sysbus_connect_irq(sysbusdev, 4, s->irq[67]);
     sysbus_connect_irq(sysbusdev, 5, s->irq[68]);
 
+    object_property_set_bool(OBJECT(&s->emac), true, "realized", &err);
+    if (err != NULL) {
+        error_propagate(errp, err);
+        return;
+    }
+    sysbusdev = SYS_BUS_DEVICE(&s->emac);
+    sysbus_mmio_map(sysbusdev, 0, AW_A10_EMAC_BASE);
+    sysbus_connect_irq(sysbusdev, 0, s->irq[55]);
+
     serial_mm_init(get_system_memory(), AW_A10_UART0_REG_BASE, 2, s->irq[1],
                    115200, serial_hds[0], DEVICE_NATIVE_ENDIAN);
 }
hw/arm/cubieboard.c +9 -2
index 3fcb6d2..d95a7f3 100644
@@ -36,10 +36,17 @@ static void cubieboard_init(QEMUMachineInitArgs *args)
     Error *err = NULL;
 
     s->a10 = AW_A10(object_new(TYPE_AW_A10));
+
+    object_property_set_int(OBJECT(&s->a10->emac), 1, "phy-addr", &err);
+    if (err != NULL) {
+        error_report("Couldn't set phy address: %s", error_get_pretty(err));
+        exit(1);
+    }
+
     object_property_set_bool(OBJECT(s->a10), true, "realized", &err);
     if (err != NULL) {
-        error_report("Couldn't realize Allwinner A10: %s\n",
-                error_get_pretty(err));
+        error_report("Couldn't realize Allwinner A10: %s",
+                     error_get_pretty(err));
         exit(1);
     }
 
include/hw/arm/allwinner-a10.h +3 -0
index da36647..01a189b 100644
@@ -6,6 +6,7 @@
 #include "hw/arm/arm.h"
 #include "hw/timer/allwinner-a10-pit.h"
 #include "hw/intc/allwinner-a10-pic.h"
+#include "hw/net/allwinner_emac.h"
 
 #include "sysemu/sysemu.h"
 #include "exec/address-spaces.h"
@@ -14,6 +15,7 @@
 #define AW_A10_PIC_REG_BASE     0x01c20400
 #define AW_A10_PIT_REG_BASE     0x01c20c00
 #define AW_A10_UART0_REG_BASE   0x01c28000
+#define AW_A10_EMAC_BASE        0x01c0b000
 
 #define AW_A10_SDRAM_BASE       0x40000000
 
@@ -29,6 +31,7 @@ typedef struct AwA10State {
     qemu_irq irq[AW_A10_PIC_INT_NR];
     AwA10PITState timer;
     AwA10PICState intc;
+    AwEmacState emac;
 } AwA10State;
 
 #define ALLWINNER_H_
Signature · 1 line
1.7.10.4
#7 lore raw
In reply to Beniamino Galvani
Quoted 2 lines · Beniamino Galvani

This patch series adds support for the EMAC Fast Ethernet controller found on Allwinner SoCs to the Allwinner A10.

Applied to target-arm.next; thanks for your patience in getting this through code review.

-- PMM