sunxi_twi.c revision 1.14
11.14Sthorpej/* $NetBSD: sunxi_twi.c,v 1.14 2021/01/18 02:35:49 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.14Sthorpej__KERNEL_RCSID(0, "$NetBSD: sunxi_twi.c,v 1.14 2021/01/18 02:35:49 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.14Sthorpej 791.14Sthorpej { 0 } 801.1Sjmcneill}; 811.1Sjmcneill 821.1SjmcneillCFATTACH_DECL_NEW(sunxi_twi, sizeof(struct gttwsi_softc), 831.1Sjmcneill sunxi_twi_match, sunxi_twi_attach, NULL, NULL); 841.1Sjmcneill 851.7Sjmcneillstatic u_int 861.7Sjmcneillsunxi_twi_calc_rate(u_int parent_rate, u_int n, u_int m) 871.7Sjmcneill{ 881.7Sjmcneill return parent_rate / (10 * (m + 1) * (1 << n)); 891.7Sjmcneill} 901.7Sjmcneill 911.7Sjmcneillstatic void 921.7Sjmcneillsunxi_twi_set_clock(struct gttwsi_softc *sc, u_int parent_rate, u_int rate) 931.7Sjmcneill{ 941.7Sjmcneill uint32_t baud; 951.7Sjmcneill u_int n, m, best_rate; 961.7Sjmcneill 971.11Sthorpej baud = gttwsi_read_4(sc, TWSI_BAUDRATE); 981.7Sjmcneill 991.7Sjmcneill for (best_rate = 0, n = 0; n < 8; n++) { 1001.7Sjmcneill for (m = 0; m < 16; m++) { 1011.11Sthorpej const u_int tmp_rate = 1021.11Sthorpej sunxi_twi_calc_rate(parent_rate, n, m); 1031.7Sjmcneill if (tmp_rate <= rate && tmp_rate > best_rate) { 1041.7Sjmcneill best_rate = tmp_rate; 1051.7Sjmcneill baud = __SHIFTIN(n, TWI_CCR_CLK_N) | 1061.7Sjmcneill __SHIFTIN(m, TWI_CCR_CLK_M); 1071.7Sjmcneill } 1081.7Sjmcneill } 1091.7Sjmcneill } 1101.7Sjmcneill 1111.11Sthorpej gttwsi_write_4(sc, TWSI_BAUDRATE, baud); 1121.7Sjmcneill delay(10000); 1131.6Sjmcneill} 1141.6Sjmcneill 1151.1Sjmcneillstatic int 1161.1Sjmcneillsunxi_twi_match(device_t parent, cfdata_t cf, void *aux) 1171.1Sjmcneill{ 1181.1Sjmcneill struct fdt_attach_args * const faa = aux; 1191.1Sjmcneill 1201.4Sjmcneill return of_match_compat_data(faa->faa_phandle, compat_data); 1211.1Sjmcneill} 1221.1Sjmcneill 1231.1Sjmcneillstatic void 1241.1Sjmcneillsunxi_twi_attach(device_t parent, device_t self, void *aux) 1251.1Sjmcneill{ 1261.1Sjmcneill struct gttwsi_softc * const sc = device_private(self); 1271.1Sjmcneill struct fdt_attach_args * const faa = aux; 1281.4Sjmcneill const struct sunxi_twi_config *conf; 1291.1Sjmcneill const int phandle = faa->faa_phandle; 1301.1Sjmcneill bus_space_tag_t bst = faa->faa_bst; 1311.1Sjmcneill bus_space_handle_t bsh; 1321.1Sjmcneill struct fdtbus_reset *rst; 1331.1Sjmcneill struct clk *clk; 1341.1Sjmcneill char intrstr[128]; 1351.1Sjmcneill bus_addr_t addr; 1361.1Sjmcneill bus_size_t size; 1371.1Sjmcneill void *ih; 1381.1Sjmcneill 1391.1Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1401.1Sjmcneill aprint_error(": couldn't get registers\n"); 1411.1Sjmcneill return; 1421.1Sjmcneill } 1431.1Sjmcneill 1441.1Sjmcneill if (bus_space_map(bst, addr, size, 0, &bsh) != 0) { 1451.1Sjmcneill aprint_error(": couldn't map registers\n"); 1461.1Sjmcneill return; 1471.1Sjmcneill } 1481.1Sjmcneill 1491.1Sjmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 1501.1Sjmcneill aprint_error(": failed to decode interrupt\n"); 1511.1Sjmcneill return; 1521.1Sjmcneill } 1531.1Sjmcneill 1541.8Sjmcneill if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL) 1551.8Sjmcneill if (clk_enable(clk) != 0) { 1561.8Sjmcneill aprint_error(": couldn't enable clock\n"); 1571.8Sjmcneill return; 1581.8Sjmcneill } 1591.1Sjmcneill if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL) 1601.1Sjmcneill if (fdtbus_reset_deassert(rst) != 0) { 1611.1Sjmcneill aprint_error(": couldn't de-assert reset\n"); 1621.1Sjmcneill return; 1631.1Sjmcneill } 1641.1Sjmcneill 1651.14Sthorpej conf = of_search_compatible(phandle, compat_data)->data; 1661.4Sjmcneill prop_dictionary_set_bool(device_properties(self), "iflg-rwc", 1671.4Sjmcneill conf->iflg_rwc); 1681.4Sjmcneill 1691.7Sjmcneill /* Attach gttwsi core */ 1701.11Sthorpej gttwsi_attach_subr(self, bst, bsh, sunxi_twi_regmap); 1711.6Sjmcneill 1721.7Sjmcneill /* 1731.7Sjmcneill * Set clock rate to 100kHz. 1741.7Sjmcneill */ 1751.8Sjmcneill if (clk != NULL) 1761.8Sjmcneill sunxi_twi_set_clock(sc, clk_get_rate(clk), 100000); 1771.1Sjmcneill 1781.13Sjmcneill ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0, gttwsi_intr, 1791.13Sjmcneill sc, device_xname(self)); 1801.1Sjmcneill if (ih == NULL) { 1811.1Sjmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n", 1821.1Sjmcneill intrstr); 1831.1Sjmcneill return; 1841.1Sjmcneill } 1851.1Sjmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr); 1861.1Sjmcneill 1871.12Sthorpej fdtbus_register_i2c_controller(&sc->sc_i2c, phandle); 1881.1Sjmcneill 1891.10Sjmcneill fdtbus_attach_i2cbus(self, phandle, &sc->sc_i2c, iicbus_print); 1901.1Sjmcneill} 191