fdt: add SPI controller frontend

This commit is contained in:
tnn 2019-08-13 16:46:49 +00:00
parent f5ccd4002e
commit 3bee3e25f0
3 changed files with 124 additions and 2 deletions

114
sys/dev/fdt/fdt_spi.c Normal file
View File

@ -0,0 +1,114 @@
/* $NetBSD: fdt_spi.c,v 1.1 2019/08/13 16:46:49 tnn Exp $ */
/*
* Copyright (c) 2019 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Tobias Nygren.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fdt_spi.c,v 1.1 2019/08/13 16:46:49 tnn Exp $");
#include <sys/param.h>
#include <sys/device.h>
#include <sys/bus.h>
#include <sys/kmem.h>
#include <sys/queue.h>
#include <dev/spi/spivar.h>
#include <libfdt.h>
#include <dev/fdt/fdtvar.h>
struct fdtbus_spi_controller {
device_t spi_dev;
int spi_phandle;
const struct fdtbus_spi_controller_func *spi_funcs;
LIST_ENTRY(fdtbus_spi_controller) spi_next;
};
static LIST_HEAD(, fdtbus_spi_controller) fdtbus_spi_controllers =
LIST_HEAD_INITIALIZER(fdtbus_spi_controllers);
int
fdtbus_register_spi_controller(device_t dev, int phandle,
const struct fdtbus_spi_controller_func *funcs)
{
struct fdtbus_spi_controller *spi;
spi = kmem_alloc(sizeof(*spi), KM_SLEEP);
spi->spi_dev = dev;
spi->spi_phandle = phandle;
spi->spi_funcs = funcs;
LIST_INSERT_HEAD(&fdtbus_spi_controllers, spi, spi_next);
return 0;
}
static struct spi_controller *
fdtbus_get_spi_controller(int phandle)
{
struct fdtbus_spi_controller *spi;
LIST_FOREACH(spi, &fdtbus_spi_controllers, spi_next) {
if (spi->spi_phandle == phandle) {
return spi->spi_funcs->get_controller(spi->spi_dev);
}
}
return NULL;
}
device_t
fdtbus_attach_spibus(device_t dev, int phandle, cfprint_t print)
{
struct spi_controller *spi;
struct spibus_attach_args sba;
prop_dictionary_t devs;
device_t ret;
u_int address_cells;
devs = prop_dictionary_create();
if (of_getprop_uint32(phandle, "#address-cells", &address_cells))
address_cells = 1;
of_enter_spi_devs(devs, phandle, address_cells * 4);
spi = fdtbus_get_spi_controller(phandle);
KASSERT(spi != NULL);
memset(&sba, 0, sizeof(sba));
sba.sba_controller = spi;
sba.sba_child_devices = prop_dictionary_get(devs, "spi-child-devices");
if (sba.sba_child_devices)
prop_object_retain(sba.sba_child_devices);
prop_object_release(devs);
ret = config_found_ia(dev, "spibus", &sba, print);
if (sba.sba_child_devices)
prop_object_release(sba.sba_child_devices);
return ret;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: fdtvar.h,v 1.52 2019/06/14 11:08:18 hkenken Exp $ */
/* $NetBSD: fdtvar.h,v 1.53 2019/08/13 16:46:49 tnn Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
@ -82,6 +82,10 @@ struct fdtbus_i2c_controller_func {
i2c_tag_t (*get_tag)(device_t);
};
struct fdtbus_spi_controller_func {
struct spi_controller * (*get_controller)(device_t);
};
struct fdtbus_gpio_controller;
struct fdtbus_gpio_pin {
@ -253,6 +257,8 @@ int fdtbus_register_interrupt_controller(device_t, int,
const struct fdtbus_interrupt_controller_func *);
int fdtbus_register_i2c_controller(device_t, int,
const struct fdtbus_i2c_controller_func *);
int fdtbus_register_spi_controller(device_t, int,
const struct fdtbus_spi_controller_func *);
int fdtbus_register_gpio_controller(device_t, int,
const struct fdtbus_gpio_controller_func *);
int fdtbus_register_pinctrl_config(device_t, int,
@ -370,6 +376,7 @@ void fdtbus_power_reset(void);
void fdtbus_power_poweroff(void);
device_t fdtbus_attach_i2cbus(device_t, int, i2c_tag_t, cfprint_t);
device_t fdtbus_attach_spibus(device_t, int, cfprint_t);
bool fdtbus_set_data(const void *);
const void * fdtbus_get_data(void);

View File

@ -1,4 +1,4 @@
# $NetBSD: files.fdt,v 1.44 2019/03/13 12:17:45 jmcneill Exp $
# $NetBSD: files.fdt,v 1.45 2019/08/13 16:46:49 tnn Exp $
include "external/bsd/libfdt/conf/files.libfdt"
@ -64,6 +64,7 @@ file dev/fdt/fdt_pwm.c fdt
file dev/fdt/fdt_regulator.c fdt
file dev/fdt/fdt_reset.c fdt
file dev/fdt/fdt_rtc.c fdt
file dev/fdt/fdt_spi.c fdt
file dev/fdt/fdt_syscon.c fdt
file dev/fdt/fdt_pinctrl.c fdt