head	1.1;
access;
symbols;
locks; strict;
comment	@ * @;


1.1
date	2026.12.02.11.24.48;	author hkenken;	state Exp;
branches;
next	;
commitid	UuYtdjTm5a0xyJ5H;


desc
@@


1.1
log
@Add ntxec, a driver for the Netronix MSP430 EC.

This fixes the problem that the system stops after about 13 seconds.

imx51reg.h: The value of I2C3_BASE was incorrect.

Tested: KOBO and NETWALKER kernels build with no warnings.
On a Kobo Glo (N613), imxi2c2 attaches at 0x53fec000 and
"ntxec0 at iic2 addr 0x43: PMIC/RTC VER 0xC851" is printed.
Before this change, the system stopped about 13 seconds after
power-on.  With this change, the console still responds 100 seconds
after power-on.  The RTC is not tested.  NETWALKER is build-tested
only.
@
text
@/*	$NetBSD$	*/

/*-
 * Copyright (c) 2026 Hashimoto Kenichi <hkenken@@NetBSD.org>
 * All rights reserved.
 *
 * 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 "opt_fdt.h"

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/kmem.h>

#include <dev/clock_subr.h>

#include <dev/i2c/i2cvar.h>

#ifdef FDT
#include <dev/fdt/fdtvar.h>
#endif

#define NTXEC_ADDR		0x43

#define NTXEC_CHIP_VER		0x00

#define NTXEC_RTC_W_YEA		0x10
#define NTXEC_RTC_W_MON		0x11
#define NTXEC_RTC_W_DAY		0x12
#define NTXEC_RTC_W_HOU		0x13
#define NTXEC_RTC_W_MIN		0x14
#define NTXEC_RTC_W_SEC		0x15

#define NTXEC_RTC_R_YEA_MON	0x20
#define NTXEC_RTC_R_DAY_HOU	0x21
#define NTXEC_RTC_R_MIN_SEC	0x23

#define NTXEC_RTC_BASE_YEAR	2000

#define NTXEC_ADC_CTRL		0x30
#define NTXEC_ADC_VAL		0x41
#define NTXEC_POWER_DOWN	0x50
#define NTXEC_POWER_KEEP	0x70
#define NTXEC_RESET_CTRL	0x90

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "netronix,ntxec" },
	DEVICE_COMPAT_EOL
};

struct ntxec_softc {
	device_t	sc_dev;

	i2c_tag_t	sc_i2c;
	i2c_addr_t	sc_addr;

	struct todr_chip_handle sc_todr;
};

static int	ntxec_match(device_t, cfdata_t, void *);
static void	ntxec_attach(device_t, device_t, void *);

static int	ntxec_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
static int	ntxec_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);

static int	ntxec_read(struct ntxec_softc *, uint8_t, uint16_t *);
static int	ntxec_write(struct ntxec_softc *, uint8_t, uint16_t);

CFATTACH_DECL_NEW(ntxec, sizeof(struct ntxec_softc),
    ntxec_match, ntxec_attach, NULL, NULL);

static int
ntxec_match(device_t parent, cfdata_t match, void *aux)
{
	struct i2c_attach_args *ia = aux;
	int match_result;

	if (iic_use_direct_match(ia, match, compat_data, &match_result))
		return match_result;

	if (ia->ia_addr == NTXEC_ADDR)
		return I2C_MATCH_ADDRESS_ONLY;

	return 0;
}

static void
ntxec_attach(device_t parent, device_t self, void *aux)
{
	struct ntxec_softc *sc = device_private(self);
	struct i2c_attach_args *ia = aux;
	uint16_t ver;

	sc->sc_dev = self;
	sc->sc_i2c = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;

	iic_acquire_bus(sc->sc_i2c, 0);
	ntxec_read(sc, NTXEC_CHIP_VER, &ver);
	iic_release_bus(sc->sc_i2c, 0);

	aprint_naive("\n");
	aprint_normal(": PMIC/RTC VER 0x%04X\n", ver);

	iic_acquire_bus(sc->sc_i2c, 0);
	ntxec_write(sc, NTXEC_POWER_KEEP, 0x0800);
	iic_release_bus(sc->sc_i2c, 0);

	sc->sc_todr.todr_gettime_ymdhms = ntxec_rtc_gettime;
	sc->sc_todr.todr_settime_ymdhms = ntxec_rtc_settime;
	sc->sc_todr.todr_dev = self;

	todr_attach(&sc->sc_todr);

#ifdef FDT
	if (devhandle_type(device_handle(self)) == DEVHANDLE_TYPE_OF) {
		int phandle = devhandle_to_of(device_handle(self));
		fdt_add_bus(self, phandle, NULL);
	}
#endif
}

static int
ntxec_read(struct ntxec_softc *sc, uint8_t reg, uint16_t *val)
{
	uint16_t tmp;
	int ret;

	ret = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr, &reg, 1,
	    &tmp, 2, 0);
	*val = be16toh(tmp);
	return ret;
}

static int
ntxec_write(struct ntxec_softc *sc, uint8_t reg, uint16_t val)
{
	uint16_t tmp = htobe16(val);

	return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, &reg, 1,
	    &tmp, 2, 0);
}

static int
ntxec_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
{
	struct ntxec_softc *sc = device_private(tch->todr_dev);
	uint16_t min_sec, day_hou, yea_mon;

	iic_acquire_bus(sc->sc_i2c, 0);
	ntxec_read(sc, NTXEC_RTC_R_MIN_SEC, &min_sec);
	ntxec_read(sc, NTXEC_RTC_R_DAY_HOU, &day_hou);
	ntxec_read(sc, NTXEC_RTC_R_YEA_MON, &yea_mon);
	iic_release_bus(sc->sc_i2c, 0);

	dt->dt_year = NTXEC_RTC_BASE_YEAR + ((yea_mon >> 8) & 0xff);
	dt->dt_mon = yea_mon & 0x1f;
	dt->dt_day = (day_hou >> 8) & 0x3f;
	dt->dt_hour = day_hou & 0x3f;
	dt->dt_min = (min_sec >> 8) & 0x7f;
	dt->dt_sec = min_sec & 0x7f;

	return 0;
}

static int
ntxec_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
{
	struct ntxec_softc *sc = device_private(tch->todr_dev);

	iic_acquire_bus(sc->sc_i2c, 0);
	ntxec_write(sc, NTXEC_RTC_W_SEC, (dt->dt_sec & 0x7f) << 8);
	ntxec_write(sc, NTXEC_RTC_W_MIN, (dt->dt_min & 0x7f) << 8);
	ntxec_write(sc, NTXEC_RTC_W_HOU, (dt->dt_hour & 0x3f) << 8);
	ntxec_write(sc, NTXEC_RTC_W_DAY, (dt->dt_day & 0x3f) << 8);
	ntxec_write(sc, NTXEC_RTC_W_MON, (dt->dt_mon & 0x1f) << 8);
	ntxec_write(sc, NTXEC_RTC_W_YEA,
	    ((dt->dt_year - NTXEC_RTC_BASE_YEAR) & 0xff) << 8);
	iic_release_bus(sc->sc_i2c, 0);

	return 0;
}
@
