sunxi_twi.c revision 1.4
1/* $NetBSD: sunxi_twi.c,v 1.4 2017/10/02 22:41:25 jmcneill Exp $ */
2
3/*-
4 * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30
31__KERNEL_RCSID(0, "$NetBSD: sunxi_twi.c,v 1.4 2017/10/02 22:41:25 jmcneill Exp $");
32
33#include <sys/param.h>
34#include <sys/bus.h>
35#include <sys/device.h>
36#include <sys/intr.h>
37#include <sys/systm.h>
38#include <sys/time.h>
39
40#include <dev/i2c/i2cvar.h>
41#include <dev/i2c/gttwsivar.h>
42
43#include <dev/fdt/fdtvar.h>
44
45static int sunxi_twi_match(device_t, cfdata_t, void *);
46static void sunxi_twi_attach(device_t, device_t, void *);
47
48struct sunxi_twi_config {
49	bool		iflg_rwc;
50};
51
52static const struct sunxi_twi_config sun4i_a10_i2c_config = {
53	.iflg_rwc = false,
54};
55
56static const struct sunxi_twi_config sun6i_a31_i2c_config = {
57	.iflg_rwc = true,
58};
59
60static const struct of_compat_data compat_data[] = {
61	{ "allwinner,sun4i-a10-i2c",	(uintptr_t)&sun4i_a10_i2c_config },
62	{ "allwinner,sun6i-a31-i2c",	(uintptr_t)&sun6i_a31_i2c_config },
63	{ NULL }
64};
65
66CFATTACH_DECL_NEW(sunxi_twi, sizeof(struct gttwsi_softc),
67	sunxi_twi_match, sunxi_twi_attach, NULL, NULL);
68
69static i2c_tag_t
70sunxi_twi_get_tag(device_t dev)
71{
72	struct gttwsi_softc * const sc = device_private(dev);
73
74	return &sc->sc_i2c;
75}
76
77const struct fdtbus_i2c_controller_func sunxi_twi_funcs = {
78	.get_tag = sunxi_twi_get_tag,
79};
80
81static int
82sunxi_twi_match(device_t parent, cfdata_t cf, void *aux)
83{
84	struct fdt_attach_args * const faa = aux;
85
86	return of_match_compat_data(faa->faa_phandle, compat_data);
87}
88
89static void
90sunxi_twi_attach(device_t parent, device_t self, void *aux)
91{
92	struct gttwsi_softc * const sc = device_private(self);
93	struct fdt_attach_args * const faa = aux;
94	const struct sunxi_twi_config *conf;
95	struct i2cbus_attach_args iba;
96	const int phandle = faa->faa_phandle;
97	bus_space_tag_t bst = faa->faa_bst;
98	bus_space_handle_t bsh;
99	prop_dictionary_t devs;
100	uint32_t address_cells;
101	struct fdtbus_reset *rst;
102	struct clk *clk;
103	char intrstr[128];
104	bus_addr_t addr;
105	bus_size_t size;
106	void *ih;
107
108	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
109		aprint_error(": couldn't get registers\n");
110		return;
111	}
112
113	if (bus_space_map(bst, addr, size, 0, &bsh) != 0) {
114		aprint_error(": couldn't map registers\n");
115		return;
116	}
117
118	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
119		aprint_error(": failed to decode interrupt\n");
120		return;
121	}
122
123	if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL)
124		if (clk_enable(clk) != 0) {
125			aprint_error(": couldn't enable clock\n");
126			return;
127		}
128	if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
129		if (fdtbus_reset_deassert(rst) != 0) {
130			aprint_error(": couldn't de-assert reset\n");
131			return;
132		}
133
134	conf = (void *)of_search_compatible(phandle, compat_data)->data;
135	prop_dictionary_set_bool(device_properties(self), "iflg-rwc",
136	    conf->iflg_rwc);
137
138	gttwsi_attach_subr(self, bst, bsh);
139
140	ih = fdtbus_intr_establish(phandle, 0, IPL_VM, 0, gttwsi_intr, sc);
141	if (ih == NULL) {
142		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
143		    intrstr);
144		return;
145	}
146	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
147
148	fdtbus_register_i2c_controller(self, phandle, &sunxi_twi_funcs);
149
150	devs = prop_dictionary_create();
151	if (of_getprop_uint32(phandle, "#address-cells", &address_cells))
152		address_cells = 1;
153
154	of_enter_i2c_devs(devs, phandle, address_cells * 4, 0);
155
156	memset(&iba, 0, sizeof(iba));
157	iba.iba_tag = &sc->sc_i2c;
158	iba.iba_child_devices = prop_dictionary_get(devs, "i2c-child-devices");
159	if (iba.iba_child_devices)
160		prop_object_retain(iba.iba_child_devices);
161	else
162		iba.iba_child_devices = prop_array_create();
163	prop_object_release(devs);
164
165	config_found_ia(self, "i2cbus", &iba, iicbus_print);
166}
167