Skip to contentSkip to conversation

nios2: Add Altera JTAG UART emulation

PATCH In review Juro Bystricky 2 participants 3 messages Feb 12, 2017 – Feb 13, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nios2: Add Altera JTAG UART emulation

Add the Altera JTAG UART model.

Hardware emulation based on:
https://www.altera.com/en_US/pdfs/literature/ug/ug_embedded_ip.pdf
(Please see "Register Map" on page 65)

---
V2: - Removed link to hw specs(checkpatch: line over 90 characters)
V3: - New link to do hw specs
V4: - Modified check for correct fifo size
- Made fifo_size a property
- Use register offsets instead of register indeces
- Removed redundant test for fifo overflow
- Modified commit message
V5: - definine ALTERA_JTAG_UART_REGS_MEM_SIZE
- Modified commit message
- Replaced DEFAULT_FIFO_SIZE by ALTERA_JUART_DEFAULT_FIFO_SIZE
- Modified test for fifo size
- Simplified error message in altera_juart_create().

---
 default-configs/nios2-softmmu.mak |   1 +
 hw/char/Makefile.objs             |   1 +
 hw/char/altera_juart.c            | 282 ++++++++++++++++++++++++++++++++++++++
 include/hw/char/altera_juart.h    |  46 +++++++
 4 files changed, 330 insertions(+)
 create mode 100644 hw/char/altera_juart.c
 create mode 100644 include/hw/char/altera_juart.h
default-configs/nios2-softmmu.mak +1 -0
index 74dc70c..6159846 100644
@@ -4,3 +4,4 @@ CONFIG_NIOS2=y
 CONFIG_SERIAL=y
 CONFIG_PTIMER=y
 CONFIG_ALTERA_TIMER=y
+CONFIG_ALTERA_JUART=y
hw/char/Makefile.objs +1 -0
index 6ea76fe..f0d0e25 100644
@@ -27,5 +27,6 @@ common-obj-$(CONFIG_LM32) += lm32_juart.o
 common-obj-$(CONFIG_LM32) += lm32_uart.o
 common-obj-$(CONFIG_MILKYMIST) += milkymist-uart.o
 common-obj-$(CONFIG_SCLPCONSOLE) += sclpconsole.o sclpconsole-lm.o
+common-obj-$(CONFIG_ALTERA_JUART) += altera_juart.o
 
 obj-$(CONFIG_VIRTIO) += virtio-serial-bus.o
hw/char/altera_juart.c new file +282 -0

282 lines not on this page · raw message

include/hw/char/altera_juart.h new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw

On 02/12/2017 07:55 PM, Juro Bystricky wrote:

Subject should contain [PATCH v5] ...

Quoted 5 lines

Add the Altera JTAG UART model.

Hardware emulation based on:
https://www.altera.com/en_US/pdfs/literature/ug/ug_embedded_ip.pdf
(Please see "Register Map" on page 65)

[...]

hw/char/altera_juart.c line 100

Quoted 12 lines
hw/char/altera_juart.c new file +12 -0
+static void altera_juart_write(void *opaque, hwaddr addr,
+                       uint64_t value, unsigned int size)
+{
+    AlteraJUARTState *s = opaque;
+    unsigned char c;
+
+    switch (addr) {
+    case OFFSET_R_DATA:
+        c = value & 0xFF;
+        /*
+         * We do not decrement the write fifo,
+         * we "tranmsmit" instanteniously, CONTROL_WI always asserted

Nit, fix this comment (transmit, instantly) and it's alignment.

I don't quite understand what this comment is trying to tell me though.

Quoted 21 lines
hw/char/altera_juart.c new file +21 -0
+         */
+        s->jcontrol |= CONTROL_WI;
+        s->jdata = c;
+        qemu_chr_fe_write(&s->chr, &c, 1);
+        altera_juart_update_irq(s);
+        break;
+
+    case OFFSET_R_CONTROL:
+        /* Only RE and WE are writable */
+        value &= CONTROL_WMASK;
+        s->jcontrol &= ~CONTROL_WMASK;
+        s->jcontrol |= value;
+
+        /* Writing 1 to AC clears it to 0 */
+        if (value & CONTROL_AC) {
+            s->jcontrol &= ~CONTROL_AC;
+        }
+        altera_juart_update_irq(s);
+        break;
+    }
+}

[...]

Quoted 22 lines
hw/char/altera_juart.c new file +22 -0
+void altera_juart_create(int channel, const hwaddr addr, qemu_irq irq, uint32_t fifo_size)
+{
+    DeviceState *dev;
+    SysBusDevice *bus;
+    Chardev *chr;
+    const char chr_name[] = "juart";
+    char label[ARRAY_SIZE(chr_name) + 1];
+
+    dev = qdev_create(NULL, TYPE_ALTERA_JUART);
+
+    if (channel >= MAX_SERIAL_PORTS) {
+        error_report("Only %d serial ports are supported by QEMU",
+                     MAX_SERIAL_PORTS);
+        exit(1);
+    }
+
+    chr = serial_hds[channel];
+    if (!chr) {
+        snprintf(label, ARRAY_SIZE(label), "%s%d", chr_name, channel);
+        chr = qemu_chr_new(label, "null");
+        if (!chr) {
+            error_report("Failed to assign serial port to altera %s", label);

hw/char/altera_juart.c line 208

Quoted 10 lines
hw/char/altera_juart.c new file +10 -0
+            exit(1);
+        }
+    }
+    qdev_prop_set_chr(dev, "chardev", chr);
+
+    /*
+     * FIFO size can be set from 8 to 32,768 bytes.
+     * Only powers of two are allowed.
+     */
+    if (fifo_size < 8 || fifo_size > 3276 || (fifo_size & ~(1 << ctz32(fifo_size)))) {

32768 , not 3276 ...

Quoted 14 lines
hw/char/altera_juart.c new file +14 -0
+        error_report("juart%d: Invalid FIFO size. [%u]", channel, fifo_size);
+        exit(1);
+    }
+
+    qdev_prop_set_uint32(dev, "fifo-size", fifo_size);
+    bus = SYS_BUS_DEVICE(dev);
+    qdev_init_nofail(dev);
+
+    if (addr != (hwaddr)-1) {
+        sysbus_mmio_map(bus, 0, addr);
+    }
+
+    sysbus_connect_irq(bus, 0, irq);
+}

[...]

Quoted 6 lines
include/hw/char/altera_juart.h new file +6 -0
+typedef struct AlteraJUARTState {
+    SysBusDevice busdev;
+    MemoryRegion mmio;
+    CharBackend chr;
+    qemu_irq irq;
+

include/hw/char/altera_juart.h line 44

Quoted 10 lines
include/hw/char/altera_juart.h new file +10 -0
+    unsigned int rx_fifo_size;
+    unsigned int rx_fifo_pos;
+    unsigned int rx_fifo_len;
+    uint32_t jdata;
+    uint32_t jcontrol;
+    uint8_t *rx_fifo;
+} AlteraJUARTState;
+
+void altera_juart_create(int channel, const hwaddr addr, qemu_irq irq,
+                                                        uint32_t fifo_size);

Fix the alignment here so it doesn't look so braindead, align under the first open parenthesis.

Quoted 2 lines

+#endif /* ALTERA_JUART_H */

Signature · 2 lines
Best regards,
Marek Vasut
#2 lore raw
Quoted 3 lines

Subject should contain [PATCH v5] ...

mea culpa, to my defense I did specify --subject-prefix="PATCH v5" but did not realize "git send-email file.patch" seems to ignore it

Normally I do something like this (which works fine):
"git send-email -1 --subject-prefix="PATCH v5"

[...]

Quoted 7 lines

hw/char/altera_juart.c line 100

Quoted 2 lines
hw/char/altera_juart.c new file +2 -0
+         * We do not decrement the write fifo,
+         * we "tranmsmit" instanteniously, CONTROL_WI always asserted

Nit, fix this comment (transmit, instantly) and it's alignment.

I don't quite understand what this comment is trying to tell me though.

comment removed as too confusing

Quoted 8 lines

hw/char/altera_juart.c

Quoted 5 lines
hw/char/altera_juart.c new file +5 -0
+    /*
+     * FIFO size can be set from 8 to 32,768 bytes.
+     * Only powers of two are allowed.
+     */
+    if (fifo_size < 8 || fifo_size > 3276 || (fifo_size & ~(1 <<

ctz32(fifo_size)))) {

32768 , not 3276 ...

Thanks, good catch. Fixed.

[...]

Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+void altera_juart_create(int channel, const hwaddr addr, qemu_irq irq,
+                                                        uint32_t

fifo_size);

Fix the alignment here so it doesn't look so braindead, align under the first open parenthesis.

Fixed

Thanks

Juro