Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/extres/syscon/syscon_generic.c
Show All 40 Lines | |||||
#include <sys/kernel.h> | #include <sys/kernel.h> | ||||
#include <sys/lock.h> | #include <sys/lock.h> | ||||
#include <sys/module.h> | #include <sys/module.h> | ||||
#include <sys/mutex.h> | #include <sys/mutex.h> | ||||
#include <sys/rman.h> | #include <sys/rman.h> | ||||
#include <machine/bus.h> | #include <machine/bus.h> | ||||
#include <dev/fdt/simplebus.h> | |||||
#include <dev/ofw/ofw_bus.h> | #include <dev/ofw/ofw_bus.h> | ||||
#include <dev/ofw/ofw_bus_subr.h> | #include <dev/ofw/ofw_bus_subr.h> | ||||
#include "syscon_if.h" | #include "syscon_if.h" | ||||
#include "syscon.h" | #include "syscon.h" | ||||
#include "syscon_generic.h" | #include "syscon_generic.h" | ||||
#include "clkdev_if.h" | |||||
MALLOC_DECLARE(M_SYSCON); | MALLOC_DECLARE(M_SYSCON); | ||||
static uint32_t syscon_generic_read_4(struct syscon *syscon, bus_size_t offset); | static uint32_t syscon_generic_read_4(struct syscon *syscon, bus_size_t offset); | ||||
static int syscon_generic_write_4(struct syscon *syscon, bus_size_t offset, | static int syscon_generic_write_4(struct syscon *syscon, bus_size_t offset, | ||||
uint32_t val); | uint32_t val); | ||||
static int syscon_generic_modify_4(struct syscon *syscon, bus_size_t offset, | static int syscon_generic_modify_4(struct syscon *syscon, bus_size_t offset, | ||||
uint32_t clear_bits, uint32_t set_bits); | uint32_t clear_bits, uint32_t set_bits); | ||||
Show All 9 Lines | |||||
#define SYSCON_LOCK(_sc) mtx_lock_spin(&(_sc)->mtx) | #define SYSCON_LOCK(_sc) mtx_lock_spin(&(_sc)->mtx) | ||||
#define SYSCON_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->mtx) | #define SYSCON_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->mtx) | ||||
#define SYSCON_LOCK_INIT(_sc) mtx_init(&(_sc)->mtx, \ | #define SYSCON_LOCK_INIT(_sc) mtx_init(&(_sc)->mtx, \ | ||||
device_get_nameunit((_sc)->dev), "syscon", MTX_SPIN) | device_get_nameunit((_sc)->dev), "syscon", MTX_SPIN) | ||||
#define SYSCON_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx); | #define SYSCON_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx); | ||||
#define SYSCON_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_OWNED); | #define SYSCON_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_OWNED); | ||||
#define SYSCON_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_NOTOWNED); | #define SYSCON_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->mtx, MA_NOTOWNED); | ||||
static syscon_method_t syscon_generic_methods[] = { | static syscon_method_t syscon_generic_syscon_methods[] = { | ||||
SYSCONMETHOD(syscon_read_4, syscon_generic_read_4), | SYSCONMETHOD(syscon_read_4, syscon_generic_read_4), | ||||
SYSCONMETHOD(syscon_write_4, syscon_generic_write_4), | SYSCONMETHOD(syscon_write_4, syscon_generic_write_4), | ||||
SYSCONMETHOD(syscon_modify_4, syscon_generic_modify_4), | SYSCONMETHOD(syscon_modify_4, syscon_generic_modify_4), | ||||
SYSCONMETHOD_END | SYSCONMETHOD_END | ||||
}; | }; | ||||
DEFINE_CLASS_1(syscon_generic, syscon_generic_class, syscon_generic_methods, | DEFINE_CLASS_1(syscon_generic_syscon, syscon_generic_syscon_class, syscon_generic_syscon_methods, | ||||
0, syscon_class); | 0, syscon_class); | ||||
static uint32_t | static uint32_t | ||||
syscon_generic_read_4(struct syscon *syscon, bus_size_t offset) | syscon_generic_read_4(struct syscon *syscon, bus_size_t offset) | ||||
{ | { | ||||
struct syscon_generic_softc *sc; | struct syscon_generic_softc *sc; | ||||
uint32_t val; | uint32_t val; | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | if (!ofw_bus_status_okay(dev)) | ||||
return (ENXIO); | return (ENXIO); | ||||
if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) | if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) | ||||
return (ENXIO); | return (ENXIO); | ||||
device_set_desc(dev, "syscon"); | device_set_desc(dev, "syscon"); | ||||
if (!bootverbose) | if (!bootverbose) | ||||
device_quiet(dev); | device_quiet(dev); | ||||
if (ofw_bus_is_compatible(dev, "simple-bus")) | |||||
return (BUS_PROBE_DEFAULT); | |||||
return (BUS_PROBE_GENERIC); | return (BUS_PROBE_GENERIC); | ||||
} | } | ||||
static int | static int | ||||
syscon_generic_attach(device_t dev) | syscon_generic_attach(device_t dev) | ||||
{ | { | ||||
struct syscon_generic_softc *sc; | struct syscon_generic_softc *sc; | ||||
int rid; | int rid; | ||||
phandle_t node, child; | |||||
device_t cdev; | |||||
sc = device_get_softc(dev); | sc = device_get_softc(dev); | ||||
sc->dev = dev; | sc->dev = dev; | ||||
rid = 0; | rid = 0; | ||||
sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, | sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, | ||||
RF_ACTIVE); | RF_ACTIVE); | ||||
if (sc->mem_res == NULL) { | if (sc->mem_res == NULL) { | ||||
device_printf(dev, "Cannot allocate memory resource\n"); | device_printf(dev, "Cannot allocate memory resource\n"); | ||||
return (ENXIO); | return (ENXIO); | ||||
} | } | ||||
SYSCON_LOCK_INIT(sc); | SYSCON_LOCK_INIT(sc); | ||||
sc->syscon = syscon_create_ofw_node(dev, &syscon_generic_class, | node = ofw_bus_get_node(sc->dev); | ||||
ofw_bus_get_node(dev)); | sc->syscon = syscon_create_ofw_node(dev, &syscon_generic_syscon_class, | ||||
node); | |||||
if (sc->syscon == NULL) { | if (sc->syscon == NULL) { | ||||
device_printf(dev, "Failed to create/register syscon\n"); | device_printf(dev, "Failed to create/register syscon\n"); | ||||
return (ENXIO); | return (ENXIO); | ||||
} | } | ||||
if (ofw_bus_is_compatible(dev, "simple-bus")) { | |||||
simplebus_init(sc->dev, node); | |||||
mmel: simplebus_attach is public function and should be used here. | |||||
for (child = OF_child(node); child != 0; child = OF_peer(child)) { | |||||
cdev = simplebus_add_device(sc->dev, child, 0, NULL, -1, NULL); | |||||
if (cdev != NULL) | |||||
device_probe_and_attach(cdev); | |||||
} | |||||
} | |||||
return (0); | return (0); | ||||
} | } | ||||
static int | static int | ||||
syscon_generic_detach(device_t dev) | syscon_generic_detach(device_t dev) | ||||
{ | { | ||||
struct syscon_generic_softc *sc; | struct syscon_generic_softc *sc; | ||||
sc = device_get_softc(dev); | sc = device_get_softc(dev); | ||||
if (sc->syscon != NULL) { | if (sc->syscon != NULL) { | ||||
syscon_unregister(sc->syscon); | syscon_unregister(sc->syscon); | ||||
free(sc->syscon, M_SYSCON); | free(sc->syscon, M_SYSCON); | ||||
} | } | ||||
/* Memory allocated in simplebus_init */ | |||||
mmelUnsubmitted Not Done Inline Actionssimplebus_detach() should be declared public and should be called from here mmel: simplebus_detach() should be declared public and should be called from here | |||||
if (sc->sc_simplebus.ranges != NULL) | |||||
free(sc->sc_simplebus.ranges, M_DEVBUF); | |||||
SYSCON_LOCK_DESTROY(sc); | SYSCON_LOCK_DESTROY(sc); | ||||
if (sc->mem_res != NULL) | if (sc->mem_res != NULL) | ||||
bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res); | bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res); | ||||
return (0); | return (0); | ||||
} | } | ||||
/* clkdev interface */ | |||||
mmelUnsubmitted Not Done Inline ActionsThis clock interface is not common and thus cannot be added into common (universal) driver mmel: This clock interface is not common and thus cannot be added into common (universal) driver | |||||
static int | |||||
syscon_generic_clk_write_4(device_t dev, bus_addr_t offset, uint32_t val) | |||||
{ | |||||
struct syscon_generic_softc *sc; | |||||
sc = device_get_softc(dev); | |||||
bus_write_4(sc->mem_res, offset, val); | |||||
return (0); | |||||
} | |||||
static int | |||||
syscon_generic_clk_read_4(device_t dev, bus_addr_t offset, uint32_t *val) | |||||
{ | |||||
struct syscon_generic_softc *sc; | |||||
sc = device_get_softc(dev); | |||||
*val = bus_read_4(sc->mem_res, offset); | |||||
return (0); | |||||
} | |||||
static int | |||||
syscon_generic_clk_modify_4(device_t dev, bus_addr_t offset, uint32_t clear_bits, uint32_t set_bits) | |||||
{ | |||||
struct syscon_generic_softc *sc; | |||||
uint32_t val; | |||||
sc = device_get_softc(dev); | |||||
val = bus_read_4(sc->mem_res, offset); | |||||
val &= ~clear_bits; | |||||
val |= set_bits; | |||||
bus_write_4(sc->mem_res, offset, val); | |||||
return (0); | |||||
} | |||||
static void | |||||
syscon_generic_clk_device_lock(device_t dev) | |||||
{ | |||||
struct syscon_generic_softc *sc; | |||||
sc = device_get_softc(dev); | |||||
SYSCON_LOCK(sc); | |||||
} | |||||
static void | |||||
syscon_generic_clk_device_unlock(device_t dev) | |||||
{ | |||||
struct syscon_generic_softc *sc; | |||||
sc = device_get_softc(dev); | |||||
SYSCON_UNLOCK(sc); | |||||
} | |||||
static device_method_t syscon_generic_dmethods[] = { | static device_method_t syscon_generic_dmethods[] = { | ||||
/* Device interface */ | /* Device interface */ | ||||
DEVMETHOD(device_probe, syscon_generic_probe), | DEVMETHOD(device_probe, syscon_generic_probe), | ||||
DEVMETHOD(device_attach, syscon_generic_attach), | DEVMETHOD(device_attach, syscon_generic_attach), | ||||
DEVMETHOD(device_detach, syscon_generic_detach), | DEVMETHOD(device_detach, syscon_generic_detach), | ||||
/* clkdev interface */ | |||||
DEVMETHOD(clkdev_write_4, syscon_generic_clk_write_4), | |||||
DEVMETHOD(clkdev_read_4, syscon_generic_clk_read_4), | |||||
DEVMETHOD(clkdev_modify_4, syscon_generic_clk_modify_4), | |||||
DEVMETHOD(clkdev_device_lock, syscon_generic_clk_device_lock), | |||||
DEVMETHOD(clkdev_device_unlock, syscon_generic_clk_device_unlock), | |||||
DEVMETHOD_END | DEVMETHOD_END | ||||
}; | }; | ||||
DEFINE_CLASS_0(syscon_generic, syscon_generic_driver, syscon_generic_dmethods, | DEFINE_CLASS_1(syscon_generic, syscon_generic_driver, syscon_generic_dmethods, | ||||
sizeof(struct syscon_generic_softc)); | sizeof(struct syscon_generic_softc), simplebus_driver); | ||||
static devclass_t syscon_generic_devclass; | static devclass_t syscon_generic_devclass; | ||||
EARLY_DRIVER_MODULE(syscon_generic, simplebus, syscon_generic_driver, | EARLY_DRIVER_MODULE(syscon_generic, simplebus, syscon_generic_driver, | ||||
syscon_generic_devclass, 0, 0, BUS_PASS_DEFAULT); | syscon_generic_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); | ||||
MODULE_VERSION(syscon_generic, 1); | MODULE_VERSION(syscon_generic, 1); | ||||
simplebus_attach is public function and should be used here.