2019-09-25 17:32:27 +03:00
|
|
|
/*
|
|
|
|
* Aspeed SD Host Controller
|
|
|
|
* Eddie James <eajames@linux.ibm.com>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 IBM Corp
|
2021-02-01 23:01:47 +03:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2019-09-25 17:32:27 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu/log.h"
|
|
|
|
#include "qemu/error-report.h"
|
|
|
|
#include "hw/sd/aspeed_sdhci.h"
|
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "hw/irq.h"
|
|
|
|
#include "migration/vmstate.h"
|
2020-01-30 19:02:02 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2021-10-22 10:52:17 +03:00
|
|
|
#include "trace.h"
|
2019-09-25 17:32:27 +03:00
|
|
|
|
|
|
|
#define ASPEED_SDHCI_INFO 0x00
|
2020-09-01 15:21:50 +03:00
|
|
|
#define ASPEED_SDHCI_INFO_SLOT1 (1 << 17)
|
|
|
|
#define ASPEED_SDHCI_INFO_SLOT0 (1 << 16)
|
|
|
|
#define ASPEED_SDHCI_INFO_RESET (1 << 0)
|
2019-09-25 17:32:27 +03:00
|
|
|
#define ASPEED_SDHCI_DEBOUNCE 0x04
|
|
|
|
#define ASPEED_SDHCI_DEBOUNCE_RESET 0x00000005
|
|
|
|
#define ASPEED_SDHCI_BUS 0x08
|
|
|
|
#define ASPEED_SDHCI_SDIO_140 0x10
|
|
|
|
#define ASPEED_SDHCI_SDIO_148 0x18
|
|
|
|
#define ASPEED_SDHCI_SDIO_240 0x20
|
|
|
|
#define ASPEED_SDHCI_SDIO_248 0x28
|
|
|
|
#define ASPEED_SDHCI_WP_POL 0xec
|
|
|
|
#define ASPEED_SDHCI_CARD_DET 0xf0
|
|
|
|
#define ASPEED_SDHCI_IRQ_STAT 0xfc
|
|
|
|
|
|
|
|
#define TO_REG(addr) ((addr) / sizeof(uint32_t))
|
|
|
|
|
|
|
|
static uint64_t aspeed_sdhci_read(void *opaque, hwaddr addr, unsigned int size)
|
|
|
|
{
|
|
|
|
uint32_t val = 0;
|
|
|
|
AspeedSDHCIState *sdhci = opaque;
|
|
|
|
|
|
|
|
switch (addr) {
|
|
|
|
case ASPEED_SDHCI_SDIO_140:
|
|
|
|
val = (uint32_t)sdhci->slots[0].capareg;
|
|
|
|
break;
|
|
|
|
case ASPEED_SDHCI_SDIO_148:
|
|
|
|
val = (uint32_t)sdhci->slots[0].maxcurr;
|
|
|
|
break;
|
|
|
|
case ASPEED_SDHCI_SDIO_240:
|
|
|
|
val = (uint32_t)sdhci->slots[1].capareg;
|
|
|
|
break;
|
|
|
|
case ASPEED_SDHCI_SDIO_248:
|
|
|
|
val = (uint32_t)sdhci->slots[1].maxcurr;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (addr < ASPEED_SDHCI_REG_SIZE) {
|
|
|
|
val = sdhci->regs[TO_REG(addr)];
|
|
|
|
} else {
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"%s: Out-of-bounds read at 0x%" HWADDR_PRIx "\n",
|
|
|
|
__func__, addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 10:52:17 +03:00
|
|
|
trace_aspeed_sdhci_read(addr, size, (uint64_t) val);
|
|
|
|
|
2019-09-25 17:32:27 +03:00
|
|
|
return (uint64_t)val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void aspeed_sdhci_write(void *opaque, hwaddr addr, uint64_t val,
|
|
|
|
unsigned int size)
|
|
|
|
{
|
|
|
|
AspeedSDHCIState *sdhci = opaque;
|
|
|
|
|
2021-10-22 10:52:17 +03:00
|
|
|
trace_aspeed_sdhci_write(addr, size, val);
|
|
|
|
|
2019-09-25 17:32:27 +03:00
|
|
|
switch (addr) {
|
2020-09-01 15:21:50 +03:00
|
|
|
case ASPEED_SDHCI_INFO:
|
|
|
|
/* The RESET bit automatically clears. */
|
|
|
|
sdhci->regs[TO_REG(addr)] = (uint32_t)val & ~ASPEED_SDHCI_INFO_RESET;
|
|
|
|
break;
|
2019-09-25 17:32:27 +03:00
|
|
|
case ASPEED_SDHCI_SDIO_140:
|
|
|
|
sdhci->slots[0].capareg = (uint64_t)(uint32_t)val;
|
|
|
|
break;
|
|
|
|
case ASPEED_SDHCI_SDIO_148:
|
|
|
|
sdhci->slots[0].maxcurr = (uint64_t)(uint32_t)val;
|
|
|
|
break;
|
|
|
|
case ASPEED_SDHCI_SDIO_240:
|
|
|
|
sdhci->slots[1].capareg = (uint64_t)(uint32_t)val;
|
|
|
|
break;
|
|
|
|
case ASPEED_SDHCI_SDIO_248:
|
|
|
|
sdhci->slots[1].maxcurr = (uint64_t)(uint32_t)val;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (addr < ASPEED_SDHCI_REG_SIZE) {
|
|
|
|
sdhci->regs[TO_REG(addr)] = (uint32_t)val;
|
|
|
|
} else {
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"%s: Out-of-bounds write at 0x%" HWADDR_PRIx "\n",
|
|
|
|
__func__, addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps aspeed_sdhci_ops = {
|
|
|
|
.read = aspeed_sdhci_read,
|
|
|
|
.write = aspeed_sdhci_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
.valid.min_access_size = 4,
|
|
|
|
.valid.max_access_size = 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void aspeed_sdhci_set_irq(void *opaque, int n, int level)
|
|
|
|
{
|
|
|
|
AspeedSDHCIState *sdhci = opaque;
|
|
|
|
|
|
|
|
if (level) {
|
|
|
|
sdhci->regs[TO_REG(ASPEED_SDHCI_IRQ_STAT)] |= BIT(n);
|
|
|
|
|
|
|
|
qemu_irq_raise(sdhci->irq);
|
|
|
|
} else {
|
|
|
|
sdhci->regs[TO_REG(ASPEED_SDHCI_IRQ_STAT)] &= ~BIT(n);
|
|
|
|
|
|
|
|
qemu_irq_lower(sdhci->irq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void aspeed_sdhci_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
|
|
|
|
AspeedSDHCIState *sdhci = ASPEED_SDHCI(dev);
|
|
|
|
|
|
|
|
/* Create input irqs for the slots */
|
|
|
|
qdev_init_gpio_in_named_with_opaque(DEVICE(sbd), aspeed_sdhci_set_irq,
|
2020-01-30 19:02:02 +03:00
|
|
|
sdhci, NULL, sdhci->num_slots);
|
2019-09-25 17:32:27 +03:00
|
|
|
|
|
|
|
sysbus_init_irq(sbd, &sdhci->irq);
|
|
|
|
memory_region_init_io(&sdhci->iomem, OBJECT(sdhci), &aspeed_sdhci_ops,
|
|
|
|
sdhci, TYPE_ASPEED_SDHCI, 0x1000);
|
|
|
|
sysbus_init_mmio(sbd, &sdhci->iomem);
|
|
|
|
|
2020-01-30 19:02:02 +03:00
|
|
|
for (int i = 0; i < sdhci->num_slots; ++i) {
|
2019-09-25 17:32:27 +03:00
|
|
|
Object *sdhci_slot = OBJECT(&sdhci->slots[i]);
|
|
|
|
SysBusDevice *sbd_slot = SYS_BUS_DEVICE(&sdhci->slots[i]);
|
|
|
|
|
error: Eliminate error_propagate() with Coccinelle, part 1
When all we do with an Error we receive into a local variable is
propagating to somewhere else, we can just as well receive it there
right away. Convert
if (!foo(..., &err)) {
...
error_propagate(errp, err);
...
return ...
}
to
if (!foo(..., errp)) {
...
...
return ...
}
where nothing else needs @err. Coccinelle script:
@rule1 forall@
identifier fun, err, errp, lbl;
expression list args, args2;
binary operator op;
constant c1, c2;
symbol false;
@@
if (
(
- fun(args, &err, args2)
+ fun(args, errp, args2)
|
- !fun(args, &err, args2)
+ !fun(args, errp, args2)
|
- fun(args, &err, args2) op c1
+ fun(args, errp, args2) op c1
)
)
{
... when != err
when != lbl:
when strict
- error_propagate(errp, err);
... when != err
(
return;
|
return c2;
|
return false;
)
}
@rule2 forall@
identifier fun, err, errp, lbl;
expression list args, args2;
expression var;
binary operator op;
constant c1, c2;
symbol false;
@@
- var = fun(args, &err, args2);
+ var = fun(args, errp, args2);
... when != err
if (
(
var
|
!var
|
var op c1
)
)
{
... when != err
when != lbl:
when strict
- error_propagate(errp, err);
... when != err
(
return;
|
return c2;
|
return false;
|
return var;
)
}
@depends on rule1 || rule2@
identifier err;
@@
- Error *err = NULL;
... when != err
Not exactly elegant, I'm afraid.
The "when != lbl:" is necessary to avoid transforming
if (fun(args, &err)) {
goto out
}
...
out:
error_propagate(errp, err);
even though other paths to label out still need the error_propagate().
For an actual example, see sclp_realize().
Without the "when strict", Coccinelle transforms vfio_msix_setup(),
incorrectly. I don't know what exactly "when strict" does, only that
it helps here.
The match of return is narrower than what I want, but I can't figure
out how to express "return where the operand doesn't use @err". For
an example where it's too narrow, see vfio_intx_enable().
Silently fails to convert hw/arm/armsse.c, because Coccinelle gets
confused by ARMSSE being used both as typedef and function-like macro
there. Converted manually.
Line breaks tidied up manually. One nested declaration of @local_err
deleted manually. Preexisting unwanted blank line dropped in
hw/riscv/sifive_e.c.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-35-armbru@redhat.com>
2020-07-07 19:06:02 +03:00
|
|
|
if (!object_property_set_int(sdhci_slot, "sd-spec-version", 2, errp)) {
|
2019-09-25 17:32:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
qom: Use returned bool to check for failure, Coccinelle part
The previous commit enables conversion of
foo(..., &err);
if (err) {
...
}
to
if (!foo(..., errp)) {
...
}
for QOM functions that now return true / false on success / error.
Coccinelle script:
@@
identifier fun = {
object_apply_global_props, object_initialize_child_with_props,
object_initialize_child_with_propsv, object_property_get,
object_property_get_bool, object_property_parse, object_property_set,
object_property_set_bool, object_property_set_int,
object_property_set_link, object_property_set_qobject,
object_property_set_str, object_property_set_uint, object_set_props,
object_set_propv, user_creatable_add_dict,
user_creatable_complete, user_creatable_del
};
expression list args, args2;
typedef Error;
Error *err;
@@
- fun(args, &err, args2);
- if (err)
+ if (!fun(args, &err, args2))
{
...
}
Fails to convert hw/arm/armsse.c, because Coccinelle gets confused by
ARMSSE being used both as typedef and function-like macro there.
Convert manually.
Line breaks tidied up manually.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20200707160613.848843-29-armbru@redhat.com>
2020-07-07 19:05:56 +03:00
|
|
|
if (!object_property_set_uint(sdhci_slot, "capareg",
|
error: Eliminate error_propagate() with Coccinelle, part 1
When all we do with an Error we receive into a local variable is
propagating to somewhere else, we can just as well receive it there
right away. Convert
if (!foo(..., &err)) {
...
error_propagate(errp, err);
...
return ...
}
to
if (!foo(..., errp)) {
...
...
return ...
}
where nothing else needs @err. Coccinelle script:
@rule1 forall@
identifier fun, err, errp, lbl;
expression list args, args2;
binary operator op;
constant c1, c2;
symbol false;
@@
if (
(
- fun(args, &err, args2)
+ fun(args, errp, args2)
|
- !fun(args, &err, args2)
+ !fun(args, errp, args2)
|
- fun(args, &err, args2) op c1
+ fun(args, errp, args2) op c1
)
)
{
... when != err
when != lbl:
when strict
- error_propagate(errp, err);
... when != err
(
return;
|
return c2;
|
return false;
)
}
@rule2 forall@
identifier fun, err, errp, lbl;
expression list args, args2;
expression var;
binary operator op;
constant c1, c2;
symbol false;
@@
- var = fun(args, &err, args2);
+ var = fun(args, errp, args2);
... when != err
if (
(
var
|
!var
|
var op c1
)
)
{
... when != err
when != lbl:
when strict
- error_propagate(errp, err);
... when != err
(
return;
|
return c2;
|
return false;
|
return var;
)
}
@depends on rule1 || rule2@
identifier err;
@@
- Error *err = NULL;
... when != err
Not exactly elegant, I'm afraid.
The "when != lbl:" is necessary to avoid transforming
if (fun(args, &err)) {
goto out
}
...
out:
error_propagate(errp, err);
even though other paths to label out still need the error_propagate().
For an actual example, see sclp_realize().
Without the "when strict", Coccinelle transforms vfio_msix_setup(),
incorrectly. I don't know what exactly "when strict" does, only that
it helps here.
The match of return is narrower than what I want, but I can't figure
out how to express "return where the operand doesn't use @err". For
an example where it's too narrow, see vfio_intx_enable().
Silently fails to convert hw/arm/armsse.c, because Coccinelle gets
confused by ARMSSE being used both as typedef and function-like macro
there. Converted manually.
Line breaks tidied up manually. One nested declaration of @local_err
deleted manually. Preexisting unwanted blank line dropped in
hw/riscv/sifive_e.c.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-35-armbru@redhat.com>
2020-07-07 19:06:02 +03:00
|
|
|
ASPEED_SDHCI_CAPABILITIES, errp)) {
|
2019-09-25 17:32:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
error: Eliminate error_propagate() with Coccinelle, part 1
When all we do with an Error we receive into a local variable is
propagating to somewhere else, we can just as well receive it there
right away. Convert
if (!foo(..., &err)) {
...
error_propagate(errp, err);
...
return ...
}
to
if (!foo(..., errp)) {
...
...
return ...
}
where nothing else needs @err. Coccinelle script:
@rule1 forall@
identifier fun, err, errp, lbl;
expression list args, args2;
binary operator op;
constant c1, c2;
symbol false;
@@
if (
(
- fun(args, &err, args2)
+ fun(args, errp, args2)
|
- !fun(args, &err, args2)
+ !fun(args, errp, args2)
|
- fun(args, &err, args2) op c1
+ fun(args, errp, args2) op c1
)
)
{
... when != err
when != lbl:
when strict
- error_propagate(errp, err);
... when != err
(
return;
|
return c2;
|
return false;
)
}
@rule2 forall@
identifier fun, err, errp, lbl;
expression list args, args2;
expression var;
binary operator op;
constant c1, c2;
symbol false;
@@
- var = fun(args, &err, args2);
+ var = fun(args, errp, args2);
... when != err
if (
(
var
|
!var
|
var op c1
)
)
{
... when != err
when != lbl:
when strict
- error_propagate(errp, err);
... when != err
(
return;
|
return c2;
|
return false;
|
return var;
)
}
@depends on rule1 || rule2@
identifier err;
@@
- Error *err = NULL;
... when != err
Not exactly elegant, I'm afraid.
The "when != lbl:" is necessary to avoid transforming
if (fun(args, &err)) {
goto out
}
...
out:
error_propagate(errp, err);
even though other paths to label out still need the error_propagate().
For an actual example, see sclp_realize().
Without the "when strict", Coccinelle transforms vfio_msix_setup(),
incorrectly. I don't know what exactly "when strict" does, only that
it helps here.
The match of return is narrower than what I want, but I can't figure
out how to express "return where the operand doesn't use @err". For
an example where it's too narrow, see vfio_intx_enable().
Silently fails to convert hw/arm/armsse.c, because Coccinelle gets
confused by ARMSSE being used both as typedef and function-like macro
there. Converted manually.
Line breaks tidied up manually. One nested declaration of @local_err
deleted manually. Preexisting unwanted blank line dropped in
hw/riscv/sifive_e.c.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-35-armbru@redhat.com>
2020-07-07 19:06:02 +03:00
|
|
|
if (!sysbus_realize(sbd_slot, errp)) {
|
2019-09-25 17:32:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sysbus_connect_irq(sbd_slot, 0, qdev_get_gpio_in(DEVICE(sbd), i));
|
|
|
|
memory_region_add_subregion(&sdhci->iomem, (i + 1) * 0x100,
|
|
|
|
&sdhci->slots[i].iomem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void aspeed_sdhci_reset(DeviceState *dev)
|
|
|
|
{
|
|
|
|
AspeedSDHCIState *sdhci = ASPEED_SDHCI(dev);
|
|
|
|
|
|
|
|
memset(sdhci->regs, 0, ASPEED_SDHCI_REG_SIZE);
|
2020-09-01 15:21:50 +03:00
|
|
|
|
|
|
|
sdhci->regs[TO_REG(ASPEED_SDHCI_INFO)] = ASPEED_SDHCI_INFO_SLOT0;
|
|
|
|
if (sdhci->num_slots == 2) {
|
|
|
|
sdhci->regs[TO_REG(ASPEED_SDHCI_INFO)] |= ASPEED_SDHCI_INFO_SLOT1;
|
|
|
|
}
|
2019-09-25 17:32:27 +03:00
|
|
|
sdhci->regs[TO_REG(ASPEED_SDHCI_DEBOUNCE)] = ASPEED_SDHCI_DEBOUNCE_RESET;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_aspeed_sdhci = {
|
|
|
|
.name = TYPE_ASPEED_SDHCI,
|
|
|
|
.version_id = 1,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_UINT32_ARRAY(regs, AspeedSDHCIState, ASPEED_SDHCI_NUM_REGS),
|
|
|
|
VMSTATE_END_OF_LIST(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-01-30 19:02:02 +03:00
|
|
|
static Property aspeed_sdhci_properties[] = {
|
|
|
|
DEFINE_PROP_UINT8("num-slots", AspeedSDHCIState, num_slots, 0),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2019-09-25 17:32:27 +03:00
|
|
|
static void aspeed_sdhci_class_init(ObjectClass *classp, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(classp);
|
|
|
|
|
|
|
|
dc->realize = aspeed_sdhci_realize;
|
|
|
|
dc->reset = aspeed_sdhci_reset;
|
|
|
|
dc->vmsd = &vmstate_aspeed_sdhci;
|
2020-01-30 19:02:02 +03:00
|
|
|
device_class_set_props(dc, aspeed_sdhci_properties);
|
2019-09-25 17:32:27 +03:00
|
|
|
}
|
|
|
|
|
2022-01-17 17:58:04 +03:00
|
|
|
static const TypeInfo aspeed_sdhci_info = {
|
2019-09-25 17:32:27 +03:00
|
|
|
.name = TYPE_ASPEED_SDHCI,
|
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(AspeedSDHCIState),
|
|
|
|
.class_init = aspeed_sdhci_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void aspeed_sdhci_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&aspeed_sdhci_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(aspeed_sdhci_register_types)
|