sunxi_twi.c revision 1.10
11.10Sjmcneill/* $NetBSD: sunxi_twi.c,v 1.10 2018/07/01 21:16:19 jmcneill 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.6Sjmcneill#include "opt_gttwsi.h"
301.6Sjmcneill#ifdef GTTWSI_ALLWINNER
311.6Sjmcneill# error Do not define GTTWSI_ALLWINNER when using this driver
321.6Sjmcneill#endif
331.6Sjmcneill
341.1Sjmcneill#include <sys/cdefs.h>
351.1Sjmcneill
361.10Sjmcneill__KERNEL_RCSID(0, "$NetBSD: sunxi_twi.c,v 1.10 2018/07/01 21:16:19 jmcneill Exp $");
371.1Sjmcneill
381.1Sjmcneill#include <sys/param.h>
391.1Sjmcneill#include <sys/bus.h>
401.1Sjmcneill#include <sys/device.h>
411.1Sjmcneill#include <sys/intr.h>
421.1Sjmcneill#include <sys/systm.h>
431.1Sjmcneill#include <sys/time.h>
441.1Sjmcneill
451.1Sjmcneill#include <dev/i2c/i2cvar.h>
461.1Sjmcneill#include <dev/i2c/gttwsivar.h>
471.6Sjmcneill#include <dev/i2c/gttwsireg.h>
481.1Sjmcneill
491.1Sjmcneill#include <dev/fdt/fdtvar.h>
501.1Sjmcneill
511.5Sjmcneill#define	TWI_CCR_REG	0x14
521.5Sjmcneill#define	 TWI_CCR_CLK_M	__BITS(6,3)
531.5Sjmcneill#define	 TWI_CCR_CLK_N	__BITS(2,0)
541.5Sjmcneill
551.7Sjmcneillstatic uint8_t sunxi_twi_regmap_rd[] = {
561.7Sjmcneill	[TWSI_SLAVEADDR/4]		= 0x00,
571.7Sjmcneill	[TWSI_EXTEND_SLAVEADDR/4]	= 0x04,
581.7Sjmcneill	[TWSI_DATA/4]			= 0x08,
591.7Sjmcneill	[TWSI_CONTROL/4]		= 0x0c,
601.7Sjmcneill	[TWSI_STATUS/4]			= 0x10,
611.7Sjmcneill	[TWSI_SOFTRESET/4]		= 0x18,
621.7Sjmcneill};
631.7Sjmcneill
641.7Sjmcneillstatic uint8_t sunxi_twi_regmap_wr[] = {
651.7Sjmcneill	[TWSI_SLAVEADDR/4]		= 0x00,
661.7Sjmcneill	[TWSI_EXTEND_SLAVEADDR/4]	= 0x04,
671.7Sjmcneill	[TWSI_DATA/4]			= 0x08,
681.7Sjmcneill	[TWSI_CONTROL/4]		= 0x0c,
691.7Sjmcneill	[TWSI_BAUDRATE/4]		= 0x14,
701.7Sjmcneill	[TWSI_SOFTRESET/4]		= 0x18,
711.6Sjmcneill};
721.6Sjmcneill
731.1Sjmcneillstatic int sunxi_twi_match(device_t, cfdata_t, void *);
741.1Sjmcneillstatic void sunxi_twi_attach(device_t, device_t, void *);
751.1Sjmcneill
761.4Sjmcneillstruct sunxi_twi_config {
771.4Sjmcneill	bool		iflg_rwc;
781.4Sjmcneill};
791.4Sjmcneill
801.4Sjmcneillstatic const struct sunxi_twi_config sun4i_a10_i2c_config = {
811.4Sjmcneill	.iflg_rwc = false,
821.4Sjmcneill};
831.4Sjmcneill
841.4Sjmcneillstatic const struct sunxi_twi_config sun6i_a31_i2c_config = {
851.4Sjmcneill	.iflg_rwc = true,
861.4Sjmcneill};
871.4Sjmcneill
881.4Sjmcneillstatic const struct of_compat_data compat_data[] = {
891.4Sjmcneill	{ "allwinner,sun4i-a10-i2c",	(uintptr_t)&sun4i_a10_i2c_config },
901.4Sjmcneill	{ "allwinner,sun6i-a31-i2c",	(uintptr_t)&sun6i_a31_i2c_config },
911.4Sjmcneill	{ NULL }
921.1Sjmcneill};
931.1Sjmcneill
941.1SjmcneillCFATTACH_DECL_NEW(sunxi_twi, sizeof(struct gttwsi_softc),
951.1Sjmcneill	sunxi_twi_match, sunxi_twi_attach, NULL, NULL);
961.1Sjmcneill
971.1Sjmcneillstatic i2c_tag_t
981.1Sjmcneillsunxi_twi_get_tag(device_t dev)
991.1Sjmcneill{
1001.1Sjmcneill	struct gttwsi_softc * const sc = device_private(dev);
1011.1Sjmcneill
1021.1Sjmcneill	return &sc->sc_i2c;
1031.1Sjmcneill}
1041.1Sjmcneill
1051.1Sjmcneillconst struct fdtbus_i2c_controller_func sunxi_twi_funcs = {
1061.1Sjmcneill	.get_tag = sunxi_twi_get_tag,
1071.1Sjmcneill};
1081.1Sjmcneill
1091.6Sjmcneillstatic uint32_t
1101.6Sjmcneillsunxi_twi_reg_read(struct gttwsi_softc *sc, uint32_t reg)
1111.6Sjmcneill{
1121.7Sjmcneill	return bus_space_read_4(sc->sc_bust, sc->sc_bush, sunxi_twi_regmap_rd[reg/4]);
1131.6Sjmcneill}
1141.6Sjmcneill
1151.6Sjmcneillstatic void
1161.6Sjmcneillsunxi_twi_reg_write(struct gttwsi_softc *sc, uint32_t reg, uint32_t val)
1171.6Sjmcneill{
1181.7Sjmcneill	bus_space_write_4(sc->sc_bust, sc->sc_bush, sunxi_twi_regmap_wr[reg/4], val);
1191.7Sjmcneill}
1201.7Sjmcneill
1211.7Sjmcneillstatic u_int
1221.7Sjmcneillsunxi_twi_calc_rate(u_int parent_rate, u_int n, u_int m)
1231.7Sjmcneill{
1241.7Sjmcneill	return parent_rate / (10 * (m + 1) * (1 << n));
1251.7Sjmcneill}
1261.7Sjmcneill
1271.7Sjmcneillstatic void
1281.7Sjmcneillsunxi_twi_set_clock(struct gttwsi_softc *sc, u_int parent_rate, u_int rate)
1291.7Sjmcneill{
1301.7Sjmcneill	uint32_t baud;
1311.7Sjmcneill	u_int n, m, best_rate;
1321.7Sjmcneill
1331.7Sjmcneill	baud = sunxi_twi_reg_read(sc, TWSI_BAUDRATE);
1341.7Sjmcneill
1351.7Sjmcneill	for (best_rate = 0, n = 0; n < 8; n++) {
1361.7Sjmcneill		for (m = 0; m < 16; m++) {
1371.7Sjmcneill			const u_int tmp_rate = sunxi_twi_calc_rate(parent_rate, n, m);
1381.7Sjmcneill			if (tmp_rate <= rate && tmp_rate > best_rate) {
1391.7Sjmcneill				best_rate = tmp_rate;
1401.7Sjmcneill				baud = __SHIFTIN(n, TWI_CCR_CLK_N) |
1411.7Sjmcneill				       __SHIFTIN(m, TWI_CCR_CLK_M);
1421.7Sjmcneill			}
1431.7Sjmcneill		}
1441.7Sjmcneill	}
1451.7Sjmcneill
1461.7Sjmcneill	sunxi_twi_reg_write(sc, TWSI_BAUDRATE, baud);
1471.7Sjmcneill	delay(10000);
1481.6Sjmcneill}
1491.6Sjmcneill
1501.1Sjmcneillstatic int
1511.1Sjmcneillsunxi_twi_match(device_t parent, cfdata_t cf, void *aux)
1521.1Sjmcneill{
1531.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1541.1Sjmcneill
1551.4Sjmcneill	return of_match_compat_data(faa->faa_phandle, compat_data);
1561.1Sjmcneill}
1571.1Sjmcneill
1581.1Sjmcneillstatic void
1591.1Sjmcneillsunxi_twi_attach(device_t parent, device_t self, void *aux)
1601.1Sjmcneill{
1611.1Sjmcneill	struct gttwsi_softc * const sc = device_private(self);
1621.1Sjmcneill	struct fdt_attach_args * const faa = aux;
1631.4Sjmcneill	const struct sunxi_twi_config *conf;
1641.1Sjmcneill	const int phandle = faa->faa_phandle;
1651.1Sjmcneill	bus_space_tag_t bst = faa->faa_bst;
1661.1Sjmcneill	bus_space_handle_t bsh;
1671.1Sjmcneill	struct fdtbus_reset *rst;
1681.1Sjmcneill	struct clk *clk;
1691.1Sjmcneill	char intrstr[128];
1701.1Sjmcneill	bus_addr_t addr;
1711.1Sjmcneill	bus_size_t size;
1721.1Sjmcneill	void *ih;
1731.1Sjmcneill
1741.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1751.1Sjmcneill		aprint_error(": couldn't get registers\n");
1761.1Sjmcneill		return;
1771.1Sjmcneill	}
1781.1Sjmcneill
1791.1Sjmcneill	if (bus_space_map(bst, addr, size, 0, &bsh) != 0) {
1801.1Sjmcneill		aprint_error(": couldn't map registers\n");
1811.1Sjmcneill		return;
1821.1Sjmcneill	}
1831.1Sjmcneill
1841.1Sjmcneill	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
1851.1Sjmcneill		aprint_error(": failed to decode interrupt\n");
1861.1Sjmcneill		return;
1871.1Sjmcneill	}
1881.1Sjmcneill
1891.8Sjmcneill	if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL)
1901.8Sjmcneill		if (clk_enable(clk) != 0) {
1911.8Sjmcneill			aprint_error(": couldn't enable clock\n");
1921.8Sjmcneill			return;
1931.8Sjmcneill		}
1941.1Sjmcneill	if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
1951.1Sjmcneill		if (fdtbus_reset_deassert(rst) != 0) {
1961.1Sjmcneill			aprint_error(": couldn't de-assert reset\n");
1971.1Sjmcneill			return;
1981.1Sjmcneill		}
1991.1Sjmcneill
2001.4Sjmcneill	conf = (void *)of_search_compatible(phandle, compat_data)->data;
2011.4Sjmcneill	prop_dictionary_set_bool(device_properties(self), "iflg-rwc",
2021.4Sjmcneill	    conf->iflg_rwc);
2031.4Sjmcneill
2041.7Sjmcneill	/* Attach gttwsi core */
2051.6Sjmcneill	sc->sc_reg_read = sunxi_twi_reg_read;
2061.6Sjmcneill	sc->sc_reg_write = sunxi_twi_reg_write;
2071.7Sjmcneill	gttwsi_attach_subr(self, bst, bsh);
2081.6Sjmcneill
2091.7Sjmcneill	/*
2101.7Sjmcneill	 * Set clock rate to 100kHz.
2111.7Sjmcneill	 */
2121.8Sjmcneill	if (clk != NULL)
2131.8Sjmcneill		sunxi_twi_set_clock(sc, clk_get_rate(clk), 100000);
2141.1Sjmcneill
2151.1Sjmcneill	ih = fdtbus_intr_establish(phandle, 0, IPL_VM, 0, gttwsi_intr, sc);
2161.1Sjmcneill	if (ih == NULL) {
2171.1Sjmcneill		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
2181.1Sjmcneill		    intrstr);
2191.1Sjmcneill		return;
2201.1Sjmcneill	}
2211.1Sjmcneill	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
2221.1Sjmcneill
2231.1Sjmcneill	fdtbus_register_i2c_controller(self, phandle, &sunxi_twi_funcs);
2241.1Sjmcneill
2251.10Sjmcneill	fdtbus_attach_i2cbus(self, phandle, &sc->sc_i2c, iicbus_print);
2261.1Sjmcneill}
227