11.20Sthorpej/* $NetBSD: sunxi_twi.c,v 1.20 2025/09/25 13:44:31 thorpej Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Sjmcneill * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.1Sjmcneill
311.20Sthorpej__KERNEL_RCSID(0, "$NetBSD: sunxi_twi.c,v 1.20 2025/09/25 13:44:31 thorpej Exp $");
321.1Sjmcneill
331.1Sjmcneill#include <sys/param.h>
341.1Sjmcneill#include <sys/bus.h>
351.1Sjmcneill#include <sys/device.h>
361.1Sjmcneill#include <sys/intr.h>
371.1Sjmcneill#include <sys/systm.h>
381.1Sjmcneill#include <sys/time.h>
391.1Sjmcneill
401.1Sjmcneill#include <dev/i2c/i2cvar.h>
411.1Sjmcneill#include <dev/i2c/gttwsivar.h>
421.6Sjmcneill#include <dev/i2c/gttwsireg.h>
431.1Sjmcneill
441.1Sjmcneill#include <dev/fdt/fdtvar.h>
451.1Sjmcneill
461.5Sjmcneill#define	TWI_CCR_REG	0x14
471.5Sjmcneill#define	 TWI_CCR_CLK_M	__BITS(6,3)
481.5Sjmcneill#define	 TWI_CCR_CLK_N	__BITS(2,0)
491.5Sjmcneill
501.11Sthorpejstatic const bus_size_t sunxi_twi_regmap[] = {
511.11Sthorpej	[TWSI_SLAVEADDR]	= TWSI_ALLWINNER_SLAVEADDR,
521.11Sthorpej	[TWSI_EXTEND_SLAVEADDR]	= TWSI_ALLWINNER_EXTEND_SLAVEADDR,
531.11Sthorpej	[TWSI_DATA]		= TWSI_ALLWINNER_DATA,
541.11Sthorpej	[TWSI_CONTROL]		= TWSI_ALLWINNER_CONTROL,
551.11Sthorpej	[TWSI_STATUS]		= TWSI_ALLWINNER_STATUS,
561.11Sthorpej	[TWSI_BAUDRATE]		= TWSI_ALLWINNER_BAUDRATE,
571.11Sthorpej	[TWSI_SOFTRESET]	= TWSI_ALLWINNER_SOFTRESET,
581.6Sjmcneill};
591.6Sjmcneill
601.1Sjmcneillstatic int sunxi_twi_match(device_t, cfdata_t, void *);
611.1Sjmcneillstatic void sunxi_twi_attach(device_t, device_t, void *);
621.1Sjmcneill
631.4Sjmcneillstruct sunxi_twi_config {
641.4Sjmcneill	bool		iflg_rwc;
651.4Sjmcneill};
661.4Sjmcneill
671.4Sjmcneillstatic const struct sunxi_twi_config sun4i_a10_i2c_config = {
681.4Sjmcneill	.iflg_rwc = false,
691.4Sjmcneill};
701.4Sjmcneill
711.4Sjmcneillstatic const struct sunxi_twi_config sun6i_a31_i2c_config = {
721.4Sjmcneill	.iflg_rwc = true,
731.4Sjmcneill};
741.4Sjmcneill
751.14Sthorpejstatic const struct device_compatible_entry compat_data[] = {
761.14Sthorpej	{ .compat = "allwinner,sun4i-a10-i2c",	.data = &sun4i_a10_i2c_config },
771.14Sthorpej	{ .compat = "allwinner,sun6i-a31-i2c",	.data = &sun6i_a31_i2c_config },
781.16Sthorpej	DEVICE_COMPAT_EOL
791.1Sjmcneill};
801.1Sjmcneill
811.1SjmcneillCFATTACH_DECL_NEW(sunxi_twi, sizeof(struct gttwsi_softc),
821.1Sjmcneill	sunxi_twi_match, sunxi_twi_attach, NULL, NULL);
831.1Sjmcneill
841.7Sjmcneillstatic u_int
851.7Sjmcneillsunxi_twi_calc_rate(u_int parent_rate, u_int n, u_int m)
861.7Sjmcneill{
871.7Sjmcneill	return parent_rate / (10 * (m + 1) * (1 << n));
881.7Sjmcneill}
891.7Sjmcneill
901.7Sjmcneillstatic void
911.7Sjmcneillsunxi_twi_set_clock(struct gttwsi_softc *sc, u_int parent_rate, u_int rate)
921.7Sjmcneill{
931.7Sjmcneill	uint32_t baud;
941.7Sjmcneill	u_int n, m, best_rate;
951.7Sjmcneill
961.11Sthorpej	baud = gttwsi_read_4(sc, TWSI_BAUDRATE);
971.7Sjmcneill
981.7Sjmcneill	for (best_rate = 0, n = 0; n < 8; n++) {
991.7Sjmcneill		for (m = 0; m < 16; m++) {
1001.11Sthorpej			const u_int tmp_rate =
1011.11Sthorpej			    sunxi_twi_calc_rate(parent_rate, n, m);
1021.7Sjmcneill			if (tmp_rate <= rate && tmp_rate > best_rate) {
1031.7Sjmcneill				best_rate = tmp_rate;
1041.7Sjmcneill				baud = __SHIFTIN(n, TWI_CCR_CLK_N) |
1051.7Sjmcneill				       __SHIFTIN(m, TWI_CCR_CLK_M);
1061.7Sjmcneill			}
1071.7Sjmcneill		}
1081.7Sjmcneill	}
1091.7Sjmcneill
1101.11Sthorpej	gttwsi_write_4(sc, TWSI_BAUDRATE, baud);
1111.7Sjmcneill	delay(10000);
1121.6Sjmcneill}
1131.6Sjmcneill
1141.1Sjmcneillstatic int
1151.1Sjmcneillsunxi_twi_match(device_t parent, cfdata_t cf, void *aux)
1161.1Sjmcneill{
1171.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1181.1Sjmcneill
1191.17Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
1201.1Sjmcneill}
1211.1Sjmcneill
1221.1Sjmcneillstatic void
1231.1Sjmcneillsunxi_twi_attach(device_t parent, device_t self, void *aux)
1241.1Sjmcneill{
1251.1Sjmcneill	struct gttwsi_softc * const sc = device_private(self);
1261.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1271.4Sjmcneill	const struct sunxi_twi_config *conf;
1281.1Sjmcneill	const int phandle = faa->faa_phandle;
1291.1Sjmcneill	bus_space_tag_t bst = faa->faa_bst;
1301.1Sjmcneill	bus_space_handle_t bsh;
1311.1Sjmcneill	struct fdtbus_reset *rst;
1321.1Sjmcneill	struct clk *clk;
1331.1Sjmcneill	char intrstr[128];
1341.1Sjmcneill	bus_addr_t addr;
1351.1Sjmcneill	bus_size_t size;
1361.1Sjmcneill	void *ih;
1371.1Sjmcneill
1381.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1391.1Sjmcneill		aprint_error(": couldn't get registers\n");
1401.1Sjmcneill		return;
1411.1Sjmcneill	}
1421.1Sjmcneill
1431.1Sjmcneill	if (bus_space_map(bst, addr, size, 0, &bsh) != 0) {
1441.1Sjmcneill		aprint_error(": couldn't map registers\n");
1451.1Sjmcneill		return;
1461.1Sjmcneill	}
1471.1Sjmcneill
1481.1Sjmcneill	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
1491.1Sjmcneill		aprint_error(": failed to decode interrupt\n");
1501.1Sjmcneill		return;
1511.1Sjmcneill	}
1521.1Sjmcneill
1531.8Sjmcneill	if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL)
1541.8Sjmcneill		if (clk_enable(clk) != 0) {
1551.8Sjmcneill			aprint_error(": couldn't enable clock\n");
1561.8Sjmcneill			return;
1571.8Sjmcneill		}
1581.1Sjmcneill	if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
1591.1Sjmcneill		if (fdtbus_reset_deassert(rst) != 0) {
1601.1Sjmcneill			aprint_error(": couldn't de-assert reset\n");
1611.1Sjmcneill			return;
1621.1Sjmcneill		}
1631.1Sjmcneill
1641.17Sthorpej	conf = of_compatible_lookup(phandle, compat_data)->data;
1651.20Sthorpej	sc->sc_iflg_rwc = conf->iflg_rwc;
1661.4Sjmcneill
1671.7Sjmcneill	/* Attach gttwsi core */
1681.11Sthorpej	gttwsi_attach_subr(self, bst, bsh, sunxi_twi_regmap);
1691.6Sjmcneill
1701.7Sjmcneill	/*
1711.7Sjmcneill	 * Set clock rate to 100kHz.
1721.7Sjmcneill	 */
1731.8Sjmcneill	if (clk != NULL)
1741.8Sjmcneill		sunxi_twi_set_clock(sc, clk_get_rate(clk), 100000);
1751.1Sjmcneill
1761.13Sjmcneill	ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0, gttwsi_intr,
1771.13Sjmcneill	    sc, device_xname(self));
1781.1Sjmcneill	if (ih == NULL) {
1791.1Sjmcneill		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
1801.1Sjmcneill		    intrstr);
1811.1Sjmcneill		return;
1821.1Sjmcneill	}
1831.1Sjmcneill	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1841.1Sjmcneill
1851.18Sthorpej	iicbus_attach(self, &sc->sc_i2c);
1861.1Sjmcneill}
187