sunxi_twi.c revision 1.1
11.1Sjmcneill/* $NetBSD: sunxi_twi.c,v 1.1 2017/06/29 19:16:08 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.1Sjmcneill#include <sys/cdefs.h> 301.1Sjmcneill 311.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: sunxi_twi.c,v 1.1 2017/06/29 19:16:08 jmcneill 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.1Sjmcneill 431.1Sjmcneill#include <dev/fdt/fdtvar.h> 441.1Sjmcneill 451.1Sjmcneillstatic int sunxi_twi_match(device_t, cfdata_t, void *); 461.1Sjmcneillstatic void sunxi_twi_attach(device_t, device_t, void *); 471.1Sjmcneill 481.1Sjmcneillstatic const char * const compatible[] = { 491.1Sjmcneill "allwinner,sun6i-a31-i2c", 501.1Sjmcneill NULL 511.1Sjmcneill}; 521.1Sjmcneill 531.1SjmcneillCFATTACH_DECL_NEW(sunxi_twi, sizeof(struct gttwsi_softc), 541.1Sjmcneill sunxi_twi_match, sunxi_twi_attach, NULL, NULL); 551.1Sjmcneill 561.1Sjmcneillstatic i2c_tag_t 571.1Sjmcneillsunxi_twi_get_tag(device_t dev) 581.1Sjmcneill{ 591.1Sjmcneill struct gttwsi_softc * const sc = device_private(dev); 601.1Sjmcneill 611.1Sjmcneill return &sc->sc_i2c; 621.1Sjmcneill} 631.1Sjmcneill 641.1Sjmcneillconst struct fdtbus_i2c_controller_func sunxi_twi_funcs = { 651.1Sjmcneill .get_tag = sunxi_twi_get_tag, 661.1Sjmcneill}; 671.1Sjmcneill 681.1Sjmcneillstatic int 691.1Sjmcneillsunxi_twi_match(device_t parent, cfdata_t cf, void *aux) 701.1Sjmcneill{ 711.1Sjmcneill struct fdt_attach_args * const faa = aux; 721.1Sjmcneill 731.1Sjmcneill return of_match_compatible(faa->faa_phandle, compatible); 741.1Sjmcneill} 751.1Sjmcneill 761.1Sjmcneillstatic void 771.1Sjmcneillsunxi_twi_attach(device_t parent, device_t self, void *aux) 781.1Sjmcneill{ 791.1Sjmcneill struct gttwsi_softc * const sc = device_private(self); 801.1Sjmcneill struct fdt_attach_args * const faa = aux; 811.1Sjmcneill struct i2cbus_attach_args iba; 821.1Sjmcneill const int phandle = faa->faa_phandle; 831.1Sjmcneill bus_space_tag_t bst = faa->faa_bst; 841.1Sjmcneill bus_space_handle_t bsh; 851.1Sjmcneill prop_dictionary_t devs; 861.1Sjmcneill uint32_t address_cells; 871.1Sjmcneill struct fdtbus_reset *rst; 881.1Sjmcneill struct clk *clk; 891.1Sjmcneill char intrstr[128]; 901.1Sjmcneill bus_addr_t addr; 911.1Sjmcneill bus_size_t size; 921.1Sjmcneill void *ih; 931.1Sjmcneill 941.1Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 951.1Sjmcneill aprint_error(": couldn't get registers\n"); 961.1Sjmcneill return; 971.1Sjmcneill } 981.1Sjmcneill 991.1Sjmcneill if (bus_space_map(bst, addr, size, 0, &bsh) != 0) { 1001.1Sjmcneill aprint_error(": couldn't map registers\n"); 1011.1Sjmcneill return; 1021.1Sjmcneill } 1031.1Sjmcneill 1041.1Sjmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 1051.1Sjmcneill aprint_error(": failed to decode interrupt\n"); 1061.1Sjmcneill return; 1071.1Sjmcneill } 1081.1Sjmcneill 1091.1Sjmcneill if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL) 1101.1Sjmcneill if (clk_enable(clk) != 0) { 1111.1Sjmcneill aprint_error(": couldn't enable clock\n"); 1121.1Sjmcneill return; 1131.1Sjmcneill } 1141.1Sjmcneill if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL) 1151.1Sjmcneill if (fdtbus_reset_deassert(rst) != 0) { 1161.1Sjmcneill aprint_error(": couldn't de-assert reset\n"); 1171.1Sjmcneill return; 1181.1Sjmcneill } 1191.1Sjmcneill 1201.1Sjmcneill gttwsi_attach_subr(self, bst, bsh); 1211.1Sjmcneill 1221.1Sjmcneill aprint_naive("\n"); 1231.1Sjmcneill aprint_normal(": TWI\n"); 1241.1Sjmcneill 1251.1Sjmcneill ih = fdtbus_intr_establish(phandle, 0, IPL_VM, 0, gttwsi_intr, sc); 1261.1Sjmcneill if (ih == NULL) { 1271.1Sjmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n", 1281.1Sjmcneill intrstr); 1291.1Sjmcneill return; 1301.1Sjmcneill } 1311.1Sjmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr); 1321.1Sjmcneill 1331.1Sjmcneill fdtbus_register_i2c_controller(self, phandle, &sunxi_twi_funcs); 1341.1Sjmcneill 1351.1Sjmcneill devs = prop_dictionary_create(); 1361.1Sjmcneill if (of_getprop_uint32(phandle, "#address-cells", &address_cells)) 1371.1Sjmcneill address_cells = 1; 1381.1Sjmcneill 1391.1Sjmcneill of_enter_i2c_devs(devs, phandle, address_cells * 4, 0); 1401.1Sjmcneill 1411.1Sjmcneill memset(&iba, 0, sizeof(iba)); 1421.1Sjmcneill iba.iba_tag = &sc->sc_i2c; 1431.1Sjmcneill iba.iba_child_devices = prop_dictionary_get(devs, "i2c-child-devices"); 1441.1Sjmcneill if (iba.iba_child_devices) 1451.1Sjmcneill prop_object_retain(iba.iba_child_devices); 1461.1Sjmcneill else 1471.1Sjmcneill iba.iba_child_devices = prop_array_create(); 1481.1Sjmcneill prop_object_release(devs); 1491.1Sjmcneill 1501.1Sjmcneill config_found_ia(self, "i2cbus", &iba, iicbus_print); 1511.1Sjmcneill} 152