Normalize how motoi2c controllers are attached.

This commit is contained in:
thorpej 2022-07-22 23:43:23 +00:00
parent 8306b1360a
commit 66fba52e46
9 changed files with 112 additions and 138 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: imx51_i2c.c,v 1.2 2015/03/27 05:31:23 hkenken Exp $ */
/* $NetBSD: imx51_i2c.c,v 1.3 2022/07/22 23:43:23 thorpej Exp $ */
/*
* Copyright (c) 2012, 2015 Genetec Corporation. All rights reserved.
@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: imx51_i2c.c,v 1.2 2015/03/27 05:31:23 hkenken Exp $");
__KERNEL_RCSID(0, "$NetBSD: imx51_i2c.c,v 1.3 2022/07/22 23:43:23 thorpej Exp $");
#include "opt_imx.h"
@ -65,7 +65,7 @@ imxi2c_attach(device_t parent, device_t self, void *aux)
if (aa->aa_size <= 0)
aa->aa_size = I2C_SIZE;
imxi2c_set_freq(self, imx51_get_clock(IMX51CLK_PERCLK_ROOT), 400000);
imxi2c_attach_common(parent, self,
aa->aa_iot, aa->aa_addr, aa->aa_size, aa->aa_irq, 0);
imxi2c_attach_common(self,
aa->aa_iot, aa->aa_addr, aa->aa_size,
imx51_get_clock(IMX51CLK_PERCLK_ROOT), 400000);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: imxi2c.c,v 1.3 2019/07/30 06:52:57 hkenken Exp $ */
/* $NetBSD: imxi2c.c,v 1.4 2022/07/22 23:43:23 thorpej Exp $ */
/*
* Copyright (c) 2012, 2015 Genetec Corporation. All rights reserved.
@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: imxi2c.c,v 1.3 2019/07/30 06:52:57 hkenken Exp $");
__KERNEL_RCSID(0, "$NetBSD: imxi2c.c,v 1.4 2022/07/22 23:43:23 thorpej Exp $");
#include <sys/param.h>
#include <sys/bus.h>
@ -78,40 +78,9 @@ imxi2c_iowr1(struct motoi2c_softc *sc, bus_size_t off, uint8_t data)
bus_space_write_2(sc->sc_iot, sc->sc_ioh, off, data);
}
int
imxi2c_attach_common(device_t parent, device_t self,
bus_space_tag_t iot, paddr_t iobase, size_t size, int intr, int flags)
static void
imxi2c_set_freq(struct imxi2c_softc *imxsc, long freq, unsigned int speed)
{
struct imxi2c_softc *sc = device_private(self);
struct motoi2c_softc *msc = &sc->sc_motoi2c;
int error;
aprint_naive("\n");
aprint_normal("\n");
sc->sc_dev = self;
msc->sc_iot = iot;
error = bus_space_map(msc->sc_iot, iobase, size, 0, &msc->sc_ioh);
if (error) {
aprint_error_dev(sc->sc_dev,
"failed to map registers (errno=%d)\n", error);
return 1;
}
sc->sc_motoi2c_settings.i2c_adr = MOTOI2C_ADR_DEFAULT;
sc->sc_motoi2c_settings.i2c_dfsrr = MOTOI2C_DFSRR_DEFAULT;
msc->sc_iord = imxi2c_iord1;
msc->sc_iowr = imxi2c_iowr1;
motoi2c_attach_common(self, msc, &sc->sc_motoi2c_settings);
return 0;
}
int
imxi2c_set_freq(device_t self, long freq, int speed)
{
struct imxi2c_softc *sc = device_private(self);
bool found = false;
int index;
@ -122,10 +91,40 @@ imxi2c_set_freq(device_t self, long freq, int speed)
}
}
if (found == false)
sc->sc_motoi2c_settings.i2c_fdr = 0x1f;
else
sc->sc_motoi2c_settings.i2c_fdr = imxi2c_clk_div[index].ic_val;
return 0;
if (found == false) {
imxsc->sc_motoi2c_settings.i2c_fdr = 0x1f;
} else {
imxsc->sc_motoi2c_settings.i2c_fdr =
imxi2c_clk_div[index].ic_val;
}
}
void
imxi2c_attach_common(device_t self, bus_space_tag_t iot, paddr_t iobase,
size_t size, long freq, unsigned int speed)
{
struct imxi2c_softc *imxsc = device_private(self);
struct motoi2c_softc *sc = &imxsc->sc_motoi2c;
int error;
aprint_naive("\n");
aprint_normal("\n");
sc->sc_dev = self;
sc->sc_iot = iot;
error = bus_space_map(sc->sc_iot, iobase, size, 0, &sc->sc_ioh);
if (error) {
aprint_error_dev(sc->sc_dev,
"failed to map registers (errno=%d)\n", error);
return;
}
imxsc->sc_motoi2c_settings.i2c_adr = MOTOI2C_ADR_DEFAULT;
imxsc->sc_motoi2c_settings.i2c_dfsrr = MOTOI2C_DFSRR_DEFAULT;
imxi2c_set_freq(imxsc, freq, speed);
sc->sc_iord = imxi2c_iord1;
sc->sc_iowr = imxi2c_iowr1;
motoi2c_attach(sc, &imxsc->sc_motoi2c_settings);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: imxi2cvar.h,v 1.3 2019/06/20 08:16:19 hkenken Exp $ */
/* $NetBSD: imxi2cvar.h,v 1.4 2022/07/22 23:43:23 thorpej Exp $ */
/*
* Copyright (c) 2012, 2015 Genetec Corporation. All rights reserved.
@ -33,22 +33,16 @@
#include <dev/i2c/motoi2cvar.h>
struct imxi2c_softc {
device_t sc_dev;
struct motoi2c_softc sc_motoi2c;
struct motoi2c_settings sc_motoi2c_settings;
struct clk *sc_clk;
};
int imxi2c_attach_common(device_t, device_t,
bus_space_tag_t, paddr_t, size_t, int, int);
int imxi2c_set_freq(device_t, long, int);
void imxi2c_attach_common(device_t, bus_space_tag_t, paddr_t, size_t,
long, unsigned int);
/*
* defined in imx51_i2c.c and imx31_i2c.c
*/
int imxi2c_match(device_t, cfdata_t, void *);
void imxi2c_attach(device_t, device_t, void *);
int imxi2c_match(device_t, cfdata_t, void *);
void imxi2c_attach(device_t, device_t, void *);
#endif /* _IMXI2CVAR_H_ */

View File

@ -1,4 +1,4 @@
/* $NetBSD: imx_i2c.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $ */
/* $NetBSD: imx_i2c.c,v 1.3 2022/07/22 23:43:24 thorpej Exp $ */
/*-
* Copyright (c) 2019 Genetec Corporation. All rights reserved.
@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: imx_i2c.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: imx_i2c.c,v 1.3 2022/07/22 23:43:24 thorpej Exp $");
#include <sys/bus.h>
@ -51,7 +51,7 @@ imxi2c_match(device_t parent, cfdata_t cf, void *aux)
void
imxi2c_attach(device_t parent __unused, device_t self, void *aux)
{
struct imxi2c_softc *sc = device_private(self);
struct imxi2c_softc *imxsc = device_private(self);
struct fdt_attach_args * const faa = aux;
const int phandle = faa->faa_phandle;
bus_space_tag_t bst = faa->faa_bst;
@ -64,15 +64,15 @@ imxi2c_attach(device_t parent __unused, device_t self, void *aux)
return;
}
sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
if (sc->sc_clk == NULL) {
imxsc->sc_clk = fdtbus_clock_get_index(phandle, 0);
if (imxsc->sc_clk == NULL) {
aprint_error(": couldn't get clock\n");
return;
}
error = clk_enable(sc->sc_clk);
error = clk_enable(imxsc->sc_clk);
if (error) {
aprint_error_dev(sc->sc_dev, "couldn't enable: %d\n", error);
aprint_error_dev(self, "couldn't enable: %d\n", error);
return;
}
@ -80,10 +80,8 @@ imxi2c_attach(device_t parent __unused, device_t self, void *aux)
error = of_getprop_uint32(phandle, "clock-frequency", &freq);
if (error)
freq = 100000;
imxi2c_set_freq(self, clk_get_rate(sc->sc_clk), freq);
sc->sc_motoi2c.sc_phandle = phandle;
imxi2c_attach_common(parent, self, bst, addr, size, -1, 0);
imxsc->sc_motoi2c.sc_phandle = phandle;
imxi2c_attach_common(self, bst, addr, size,
clk_get_rate(imxsc->sc_clk), freq);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: pq3diic.c,v 1.6 2022/07/22 20:09:47 thorpej Exp $ */
/* $NetBSD: pq3diic.c,v 1.7 2022/07/22 23:43:24 thorpej Exp $ */
/*-
* Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
* All rights reserved.
@ -35,7 +35,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pq3diic.c,v 1.6 2022/07/22 20:09:47 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: pq3diic.c,v 1.7 2022/07/22 23:43:24 thorpej Exp $");
#include "ioconf.h"
@ -56,15 +56,10 @@ __KERNEL_RCSID(0, "$NetBSD: pq3diic.c,v 1.6 2022/07/22 20:09:47 thorpej Exp $");
#include <powerpc/booke/e500var.h>
#include <powerpc/booke/e500reg.h>
struct pq3diic_softc {
device_t sc_dev;
struct motoi2c_softc sc_motoi2c;
};
static int pq3diic_match(device_t, cfdata_t, void *);
static void pq3diic_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(pq3diic, sizeof(struct pq3diic_softc),
CFATTACH_DECL_NEW(pq3diic, sizeof(struct motoi2c_softc),
pq3diic_match, pq3diic_attach, NULL, NULL);
static int
@ -91,8 +86,7 @@ static void
pq3diic_attach(device_t parent, device_t self, void *aux)
{
struct cpunode_softc * const psc = device_private(parent);
struct pq3diic_softc * const sc = device_private(self);
struct motoi2c_softc * const msc = &sc->sc_motoi2c;
struct motoi2c_softc * const sc = device_private(self);
struct cpunode_attach_args * const cna = aux;
struct cpunode_locators * const cnl = &cna->cna_locs;
int error;
@ -102,16 +96,16 @@ pq3diic_attach(device_t parent, device_t self, void *aux)
aprint_normal("\n");
msc->sc_iot = cna->cna_memt;
error = bus_space_map(msc->sc_iot, cnl->cnl_addr, I2C_SIZE,
0, &msc->sc_ioh);
sc->sc_iot = cna->cna_memt;
error = bus_space_map(sc->sc_iot, cnl->cnl_addr, I2C_SIZE,
0, &sc->sc_ioh);
if (error) {
aprint_error_dev(self,
"can't map registers (error = %d)\n", error);
return;
}
motoi2c_attach_common(self, msc, NULL);
motoi2c_attach(sc, NULL);
#if 0
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: iic_eumb.c,v 1.19 2012/05/12 13:13:24 nisimura Exp $ */
/* $NetBSD: iic_eumb.c,v 1.20 2022/07/22 23:43:24 thorpej Exp $ */
/*-
* Copyright (c) 2010,2011 Frank Wille.
@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: iic_eumb.c,v 1.19 2012/05/12 13:13:24 nisimura Exp $");
__KERNEL_RCSID(0, "$NetBSD: iic_eumb.c,v 1.20 2022/07/22 23:43:24 thorpej Exp $");
#include <sys/param.h>
#include <sys/device.h>
@ -38,15 +38,10 @@ __KERNEL_RCSID(0, "$NetBSD: iic_eumb.c,v 1.19 2012/05/12 13:13:24 nisimura Exp $
#include <dev/i2c/motoi2cvar.h>
#include <sandpoint/sandpoint/eumbvar.h>
struct iic_eumb_softc {
device_t sc_dev;
struct motoi2c_softc sc_motoi2c;
};
static int iic_eumb_match(device_t, cfdata_t, void *);
static void iic_eumb_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(iic_eumb, sizeof(struct iic_eumb_softc),
CFATTACH_DECL_NEW(iic_eumb, sizeof(struct motoi2c_softc),
iic_eumb_match, iic_eumb_attach, NULL, NULL);
static int found;
@ -61,13 +56,11 @@ iic_eumb_match(device_t parent, cfdata_t cf, void *aux)
static void
iic_eumb_attach(device_t parent, device_t self, void *aux)
{
struct iic_eumb_softc *sc;
struct eumb_attach_args *eaa;
struct motoi2c_softc *sc = device_private(self);
struct eumb_attach_args *eaa = aux;
bus_space_handle_t ioh;
sc = device_private(self);
sc->sc_dev = self;
eaa = aux;
found = 1;
aprint_naive("\n");
@ -77,7 +70,7 @@ iic_eumb_attach(device_t parent, device_t self, void *aux)
* map EUMB registers and attach MI motoi2c with default settings
*/
bus_space_map(eaa->eumb_bt, 0x3000, 0x20, 0, &ioh);
sc->sc_motoi2c.sc_iot = eaa->eumb_bt;
sc->sc_motoi2c.sc_ioh = ioh;
motoi2c_attach_common(self, &sc->sc_motoi2c, NULL);
sc->sc_iot = eaa->eumb_bt;
sc->sc_ioh = ioh;
motoi2c_attach(sc, NULL);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: nxpiic_acpi.c,v 1.4 2021/01/29 02:26:58 thorpej Exp $ */
/* $NetBSD: nxpiic_acpi.c,v 1.5 2022/07/22 23:43:23 thorpej Exp $ */
/*-
* Copyright (c) 2021 The NetBSD Foundation, Inc.
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nxpiic_acpi.c,v 1.4 2021/01/29 02:26:58 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: nxpiic_acpi.c,v 1.5 2022/07/22 23:43:23 thorpej Exp $");
#include <sys/param.h>
#include <sys/bus.h>
@ -68,18 +68,13 @@ static const struct clk_div {
{ 3840, 0x3f }, { 4096, 0x7B }, { 5120, 0x7d }, { 6144, 0x7e },
};
struct nxpiic_softc {
device_t sc_dev;
struct motoi2c_softc sc_motoi2c;
};
static int nxpiic_acpi_match(device_t, cfdata_t, void *);
static void nxpiic_acpi_attach(device_t, device_t, void *);
static uint8_t nxpiic_acpi_iord(struct motoi2c_softc *, bus_size_t);
static void nxpiic_acpi_iowr(struct motoi2c_softc *, bus_size_t, uint8_t);
CFATTACH_DECL_NEW(nxpiic_acpi, sizeof(struct nxpiic_softc),
CFATTACH_DECL_NEW(nxpiic_acpi, sizeof(struct motoi2c_softc),
nxpiic_acpi_match, nxpiic_acpi_attach, NULL, NULL);
static const struct device_compatible_entry compat_data[] = {
@ -98,8 +93,7 @@ nxpiic_acpi_match(device_t parent, cfdata_t cf, void *aux)
static void
nxpiic_acpi_attach(device_t parent, device_t self, void *aux)
{
struct nxpiic_softc * const sc = device_private(self);
struct motoi2c_softc * const msc = &sc->sc_motoi2c;
struct motoi2c_softc * const sc = device_private(self);
struct motoi2c_settings settings;
struct acpi_attach_args *aa = aux;
struct acpi_resources res;
@ -109,7 +103,7 @@ nxpiic_acpi_attach(device_t parent, device_t self, void *aux)
int error, n;
sc->sc_dev = self;
msc->sc_iot = aa->aa_memt;
sc->sc_iot = aa->aa_memt;
rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
&res, &acpi_resource_parse_ops_default);
@ -130,8 +124,8 @@ nxpiic_acpi_attach(device_t parent, device_t self, void *aux)
}
aprint_debug_dev(self, "bus clock %u Hz\n", (u_int)clock_freq);
error = bus_space_map(msc->sc_iot, mem->ar_base, mem->ar_length, 0,
&msc->sc_ioh);
error = bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0,
&sc->sc_ioh);
if (error) {
aprint_error_dev(self, "couldn't map registers\n");
return;
@ -145,12 +139,12 @@ nxpiic_acpi_attach(device_t parent, device_t self, void *aux)
}
settings.i2c_fdr = nxpiic_clk_div[n].ibc;
msc->sc_flags |= MOTOI2C_F_ENABLE_INV | MOTOI2C_F_STATUS_W1C;
msc->sc_iord = nxpiic_acpi_iord;
msc->sc_iowr = nxpiic_acpi_iowr;
msc->sc_child_devices = acpi_enter_i2c_devs(self, aa->aa_node);
sc->sc_flags |= MOTOI2C_F_ENABLE_INV | MOTOI2C_F_STATUS_W1C;
sc->sc_iord = nxpiic_acpi_iord;
sc->sc_iowr = nxpiic_acpi_iowr;
sc->sc_child_devices = acpi_enter_i2c_devs(self, aa->aa_node);
motoi2c_attach_common(self, msc, &settings);
motoi2c_attach(sc, &settings);
done:
acpi_resource_cleanup(&res);

View File

@ -1,4 +1,4 @@
/* $NetBSD: motoi2c.c,v 1.12 2021/08/07 16:19:11 thorpej Exp $ */
/* $NetBSD: motoi2c.c,v 1.13 2022/07/22 23:43:23 thorpej Exp $ */
/*-
* Copyright (c) 2007, 2010 The NetBSD Foundation, Inc.
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: motoi2c.c,v 1.12 2021/08/07 16:19:11 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: motoi2c.c,v 1.13 2022/07/22 23:43:23 thorpej Exp $");
#if defined(__arm__) || defined(__aarch64__)
#include "opt_fdt.h"
@ -62,6 +62,7 @@ static int motoi2c_acquire_bus(void *, int);
static void motoi2c_release_bus(void *, int);
static int motoi2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
void *, size_t, int);
static void motoi2c_clear_status(struct motoi2c_softc *, uint8_t);
static int motoi2c_busy_wait(struct motoi2c_softc *, uint8_t);
static const struct motoi2c_settings motoi2c_default_settings = {
@ -88,23 +89,26 @@ motoi2c_iowr1(struct motoi2c_softc *sc, bus_size_t off, uint8_t data)
}
void
motoi2c_attach_common(device_t self, struct motoi2c_softc *sc,
const struct motoi2c_settings *i2c)
motoi2c_attach(struct motoi2c_softc *sc,
const struct motoi2c_settings *settings)
{
struct i2cbus_attach_args iba;
if (i2c == NULL)
i2c = &motoi2c_default_settings;
if (settings == NULL) {
sc->sc_settings = motoi2c_default_settings;
} else {
sc->sc_settings = *settings;
}
if (sc->sc_iord == NULL)
sc->sc_iord = motoi2c_iord1;
if (sc->sc_iowr == NULL)
sc->sc_iowr = motoi2c_iowr1;
iic_tag_init(&sc->sc_i2c);
sc->sc_i2c.ic_cookie = sc;
sc->sc_i2c.ic_acquire_bus = motoi2c_acquire_bus;
sc->sc_i2c.ic_release_bus = motoi2c_release_bus;
sc->sc_i2c.ic_exec = motoi2c_exec;
if (sc->sc_iord == NULL)
sc->sc_iord = motoi2c_iord1;
if (sc->sc_iowr == NULL)
sc->sc_iowr = motoi2c_iowr1;
memset(&iba, 0, sizeof(iba));
iba.iba_tag = &sc->sc_i2c;
iba.iba_child_devices = sc->sc_child_devices;
@ -118,23 +122,19 @@ motoi2c_attach_common(device_t self, struct motoi2c_softc *sc,
}
I2C_WRITE(I2CCR, sc->sc_disable_mask); /* reset before config */
I2C_WRITE(I2CDFSRR, i2c->i2c_dfsrr); /* sampling units */
I2C_WRITE(I2CFDR, i2c->i2c_fdr); /* divider 3072 (0x31) */
I2C_WRITE(I2CADR, i2c->i2c_adr); /* our slave address is 0x7f */
if ((sc->sc_flags & MOTOI2C_F_STATUS_W1C) != 0) {
I2C_WRITE(I2CSR, I2C_READ(I2CSR)); /* clear status flags */
} else {
I2C_WRITE(I2CSR, 0); /* clear status flags */
}
I2C_WRITE(I2CDFSRR, sc->sc_settings.i2c_dfsrr); /* sampling units */
I2C_WRITE(I2CFDR, sc->sc_settings.i2c_fdr); /* divider 3072 */
I2C_WRITE(I2CADR, sc->sc_settings.i2c_adr); /* our slave address */
motoi2c_clear_status(sc, I2C_READ(I2CSR));
#ifdef FDT
if (sc->sc_phandle != 0) {
fdtbus_register_i2c_controller(&sc->sc_i2c, sc->sc_phandle);
fdtbus_attach_i2cbus(self, sc->sc_phandle, &sc->sc_i2c,
fdtbus_attach_i2cbus(sc->sc_dev, sc->sc_phandle, &sc->sc_i2c,
iicbus_print);
} else
#endif
config_found(self, &iba, iicbus_print,
config_found(sc->sc_dev, &iba, iicbus_print,
CFARGS(.iattr = "i2cbus"));
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: motoi2cvar.h,v 1.8 2021/01/25 12:08:47 jmcneill Exp $ */
/* $NetBSD: motoi2cvar.h,v 1.9 2022/07/22 23:43:23 thorpej Exp $ */
/*-
* Copyright (c) 2007, 2010 The NetBSD Foundation, Inc.
@ -45,6 +45,7 @@ struct motoi2c_settings {
};
struct motoi2c_softc {
device_t sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
struct i2c_controller sc_i2c;
@ -52,6 +53,7 @@ struct motoi2c_softc {
motoi2c_iowr_t sc_iowr;
int sc_phandle;
prop_array_t sc_child_devices;
struct motoi2c_settings sc_settings;
int sc_flags;
#define MOTOI2C_F_ENABLE_INV __BIT(0)
#define MOTOI2C_F_STATUS_W1C __BIT(1)
@ -64,7 +66,7 @@ struct motoi2c_softc {
#define MOTOI2C_DFSRR_DEFAULT 0x10
#ifdef _KERNEL
void motoi2c_attach_common(device_t, struct motoi2c_softc *,
void motoi2c_attach(struct motoi2c_softc *,
const struct motoi2c_settings *);
int motoi2c_intr(void *);
#endif