Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/arch/arm/nvidia Tegra I2C driver
details: https://anonhg.NetBSD.org/src/rev/ebfa6c76b497
branches: trunk
changeset: 338113:ebfa6c76b497
user: jmcneill <jmcneill%NetBSD.org@localhost>
date: Sun May 10 23:50:21 2015 +0000
description:
Tegra I2C driver
diffstat:
sys/arch/arm/nvidia/files.tegra | 7 +-
sys/arch/arm/nvidia/tegra_car.c | 71 +++++++-
sys/arch/arm/nvidia/tegra_carreg.h | 37 +++-
sys/arch/arm/nvidia/tegra_i2c.c | 352 +++++++++++++++++++++++++++++++++++++
sys/arch/arm/nvidia/tegra_i2creg.h | 134 ++++++++++++++
sys/arch/arm/nvidia/tegra_intr.h | 8 +-
sys/arch/arm/nvidia/tegra_io.c | 16 +-
sys/arch/arm/nvidia/tegra_reg.h | 14 +-
sys/arch/arm/nvidia/tegra_var.h | 3 +-
9 files changed, 633 insertions(+), 9 deletions(-)
diffs (truncated from 786 to 300 lines):
diff -r 8acc117f64da -r ebfa6c76b497 sys/arch/arm/nvidia/files.tegra
--- a/sys/arch/arm/nvidia/files.tegra Sun May 10 22:54:06 2015 +0000
+++ b/sys/arch/arm/nvidia/files.tegra Sun May 10 23:50:21 2015 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: files.tegra,v 1.8 2015/05/07 23:55:11 jmcneill Exp $
+# $NetBSD: files.tegra,v 1.9 2015/05/10 23:50:21 jmcneill Exp $
#
# Configuration info for NVIDIA Tegra ARM Peripherals
#
@@ -50,6 +50,11 @@
attach com at tegraio with tegra_com
file arch/arm/nvidia/tegra_com.c tegra_com needs-flag
+# I2C
+device tegrai2c: i2cbus, i2cexec
+attach tegrai2c at tegraio with tegra_i2c
+file arch/arm/nvidia/tegra_i2c.c tegra_i2c
+
# RTC
device tegrartc
attach tegrartc at tegraio with tegra_rtc
diff -r 8acc117f64da -r ebfa6c76b497 sys/arch/arm/nvidia/tegra_car.c
--- a/sys/arch/arm/nvidia/tegra_car.c Sun May 10 22:54:06 2015 +0000
+++ b/sys/arch/arm/nvidia/tegra_car.c Sun May 10 23:50:21 2015 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: tegra_car.c,v 1.8 2015/05/10 15:31:48 jmcneill Exp $ */
+/* $NetBSD: tegra_car.c,v 1.9 2015/05/10 23:50:21 jmcneill Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -29,7 +29,7 @@
#include "locators.h"
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tegra_car.c,v 1.8 2015/05/10 15:31:48 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tegra_car.c,v 1.9 2015/05/10 23:50:21 jmcneill Exp $");
#include <sys/param.h>
#include <sys/bus.h>
@@ -437,3 +437,70 @@
bus_space_write_4(bst, bsh, CAR_RST_DEV_W_CLR_REG, CAR_DEV_W_SATACOLD);
bus_space_write_4(bst, bsh, CAR_RST_DEV_V_CLR_REG, CAR_DEV_V_SATA);
}
+
+int
+tegra_car_periph_i2c_enable(u_int port, u_int rate)
+{
+ bus_space_tag_t bst;
+ bus_space_handle_t bsh;
+ bus_size_t rst_reg, enb_reg, clksrc_reg;
+ uint32_t dev_bit;
+
+ tegra_car_get_bs(&bst, &bsh);
+
+ switch (port) {
+ case 0:
+ rst_reg = CAR_RST_DEV_L_SET_REG;
+ enb_reg = CAR_CLK_ENB_L_SET_REG;
+ dev_bit = CAR_DEV_L_I2C1;
+ clksrc_reg = CAR_CLKSRC_I2C1_REG;
+ break;
+ case 1:
+ rst_reg = CAR_RST_DEV_H_SET_REG;
+ enb_reg = CAR_CLK_ENB_H_SET_REG;
+ dev_bit = CAR_DEV_H_I2C2;
+ clksrc_reg = CAR_CLKSRC_I2C2_REG;
+ break;
+ case 2:
+ rst_reg = CAR_RST_DEV_U_SET_REG;
+ enb_reg = CAR_CLK_ENB_U_SET_REG;
+ dev_bit = CAR_DEV_U_I2C3;
+ clksrc_reg = CAR_CLKSRC_I2C3_REG;
+ break;
+ case 3:
+ rst_reg = CAR_RST_DEV_V_SET_REG;
+ enb_reg = CAR_CLK_ENB_V_SET_REG;
+ dev_bit = CAR_DEV_V_I2C4;
+ clksrc_reg = CAR_CLKSRC_I2C4_REG;
+ break;
+ case 4:
+ rst_reg = CAR_RST_DEV_H_SET_REG;
+ enb_reg = CAR_CLK_ENB_V_SET_REG;
+ dev_bit = CAR_DEV_H_I2C5;
+ clksrc_reg = CAR_CLKSRC_I2C5_REG;
+ break;
+ case 5:
+ rst_reg = CAR_RST_DEV_X_SET_REG;
+ enb_reg = CAR_CLK_ENB_X_SET_REG;
+ dev_bit = CAR_DEV_X_I2C6;
+ clksrc_reg = CAR_CLKSRC_I2C6_REG;
+ break;
+ default:
+ return EINVAL;
+ }
+
+ /* Enter reset, enable clock */
+ bus_space_write_4(bst, bsh, rst_reg, dev_bit);
+ bus_space_write_4(bst, bsh, enb_reg, dev_bit);
+
+ /* Set clock source to PLLP */
+ const u_int div = howmany(tegra_car_pllp0_rate() / 1000, rate / 1000);
+ bus_space_write_4(bst, bsh, clksrc_reg,
+ __SHIFTIN(CAR_CLKSRC_I2C_SRC_PLLP_OUT0, CAR_CLKSRC_I2C_SRC) |
+ __SHIFTIN(div - 1, CAR_CLKSRC_I2C_DIV));
+
+ /* Leave reset */
+ bus_space_write_4(bst, bsh, rst_reg+4, dev_bit);
+
+ return 0;
+}
diff -r 8acc117f64da -r ebfa6c76b497 sys/arch/arm/nvidia/tegra_carreg.h
--- a/sys/arch/arm/nvidia/tegra_carreg.h Sun May 10 22:54:06 2015 +0000
+++ b/sys/arch/arm/nvidia/tegra_carreg.h Sun May 10 23:50:21 2015 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: tegra_carreg.h,v 1.10 2015/05/10 15:31:48 jmcneill Exp $ */
+/* $NetBSD: tegra_carreg.h,v 1.11 2015/05/10 23:50:21 jmcneill Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -100,6 +100,22 @@
#define CAR_PLLE_MISC_REG 0xec
+#define CAR_CLKSRC_I2C1_REG 0x124
+#define CAR_CLKSRC_I2C2_REG 0x198
+#define CAR_CLKSRC_I2C3_REG 0x1b8
+#define CAR_CLKSRC_I2C4_REG 0x3c4
+#define CAR_CLKSRC_I2C5_REG 0x128
+#define CAR_CLKSRC_I2C6_REG 0x65c
+
+#define CAR_CLKSRC_I2C_SRC __BITS(31,29)
+#define CAR_CLKSRC_I2C_SRC_PLLP_OUT0 0
+#define CAR_CLKSRC_I2C_SRC_PLLC2_OUT0 1
+#define CAR_CLKSRC_I2C_SRC_PLLC_OUT0 2
+#define CAR_CLKSRC_I2C_SRC_PLLC3_OUT0 3
+#define CAR_CLKSRC_I2C_SRC_PLLM_OUT0 4
+#define CAR_CLKSRC_I2C_SRC_CLK_M 6
+#define CAR_CLKSRC_I2C_DIV __BITS(15,0)
+
#define CAR_CLKSRC_UARTA_REG 0x178
#define CAR_CLKSRC_UARTB_REG 0x17c
#define CAR_CLKSRC_UARTC_REG 0x1a0
@@ -140,6 +156,8 @@
#define CAR_RST_DEV_V_CLR_REG 0x434
#define CAR_RST_DEV_W_SET_REG 0x438
#define CAR_RST_DEV_W_CLR_REG 0x43c
+#define CAR_RST_DEV_X_SET_REG 0x290
+#define CAR_RST_DEV_X_CLR_REG 0x294
#define CAR_CLK_ENB_L_SET_REG 0x320
#define CAR_CLK_ENB_L_CLR_REG 0x324
@@ -151,6 +169,8 @@
#define CAR_CLK_ENB_V_CLR_REG 0x444
#define CAR_CLK_ENB_W_SET_REG 0x448
#define CAR_CLK_ENB_W_CLR_REG 0x44c
+#define CAR_CLK_ENB_X_SET_REG 0x284
+#define CAR_CLK_ENB_X_CLR_REG 0x288
#define CAR_DEV_L_CACHE2 __BIT(31)
#define CAR_DEV_L_I2S0 __BIT(30)
@@ -261,6 +281,21 @@
#define CAR_DEV_W_SATACOLD __BIT(1)
#define CAR_DEV_W_HDA2HDMICODEC __BIT(0)
+#define CAR_DEV_X_AMX1 __BIT(25)
+#define CAR_DEV_X_GPU __BIT(24)
+#define CAR_DEV_X_SOR0 __BIT(22)
+#define CAR_DEV_X_DPAUX __BIT(21)
+#define CAR_DEV_X_ADX1 __BIT(20)
+#define CAR_DEV_X_VIC __BIT(18)
+#define CAR_DEV_X_CLK72MHZ __BIT(17)
+#define CAR_DEV_X_HDMI_AUDIO __BIT(16)
+#define CAR_DEV_X_EMC_DLL __BIT(14)
+#define CAR_DEV_X_VIM2_CLK __BIT(11)
+#define CAR_DEV_X_I2C6 __BIT(6)
+#define CAR_DEV_X_CAM_MCLK2 __BIT(5)
+#define CAR_DEV_X_CAM_MCLK __BIT(4)
+#define CAR_DEV_X_SPARE __BIT(0)
+
#define CAR_UTMIP_PLL_CFG0_REG 0x480
#define CAR_UTMIP_PLL_CFG1_REG 0x484
diff -r 8acc117f64da -r ebfa6c76b497 sys/arch/arm/nvidia/tegra_i2c.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/arm/nvidia/tegra_i2c.c Sun May 10 23:50:21 2015 +0000
@@ -0,0 +1,352 @@
+/* $NetBSD: tegra_i2c.c,v 1.1 2015/05/10 23:50:21 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "locators.h"
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: tegra_i2c.c,v 1.1 2015/05/10 23:50:21 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/device.h>
+#include <sys/intr.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+
+#include <dev/i2c/i2cvar.h>
+
+#include <arm/nvidia/tegra_reg.h>
+#include <arm/nvidia/tegra_i2creg.h>
+#include <arm/nvidia/tegra_var.h>
+
+static int tegra_i2c_match(device_t, cfdata_t, void *);
+static void tegra_i2c_attach(device_t, device_t, void *);
+
+struct tegra_i2c_softc {
+ device_t sc_dev;
+ bus_space_tag_t sc_bst;
+ bus_space_handle_t sc_bsh;
+ void * sc_ih;
+
+ struct i2c_controller sc_ic;
+ kmutex_t sc_lock;
+ kcondvar_t sc_cv;
+ device_t sc_i2cdev;
+};
+
+static void tegra_i2c_init(struct tegra_i2c_softc *);
+static int tegra_i2c_intr(void *);
+
+static int tegra_i2c_acquire_bus(void *, int);
+static void tegra_i2c_release_bus(void *, int);
+static int tegra_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
+ size_t, void *, size_t, int);
+
+static int tegra_i2c_wait(struct tegra_i2c_softc *, int);
+static int tegra_i2c_write(struct tegra_i2c_softc *, i2c_addr_t,
+ const uint8_t *, size_t, int);
+static int tegra_i2c_read(struct tegra_i2c_softc *, i2c_addr_t, uint8_t *,
+ size_t, int);
+
+CFATTACH_DECL_NEW(tegra_i2c, sizeof(struct tegra_i2c_softc),
+ tegra_i2c_match, tegra_i2c_attach, NULL, NULL);
+
+#define I2C_WRITE(sc, reg, val) \
+ bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
+#define I2C_READ(sc, reg) \
+ bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
+#define I2C_SET_CLEAR(sc, reg, setval, clrval) \
+ tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (setval), (clrval))
+
+static int
+tegra_i2c_match(device_t parent, cfdata_t cf, void *aux)
+{
+ struct tegraio_attach_args * const tio = aux;
+ const struct tegra_locators * const loc = &tio->tio_loc;
+
+ if (loc->loc_port == TEGRAIOCF_PORT_DEFAULT)
+ return 0;
+
+ return 1;
+}
+
+static void
+tegra_i2c_attach(device_t parent, device_t self, void *aux)
+{
+ struct tegra_i2c_softc * const sc = device_private(self);
+ struct tegraio_attach_args * const tio = aux;
+ const struct tegra_locators * const loc = &tio->tio_loc;
+ struct i2cbus_attach_args iba;
+
+ sc->sc_dev = self;
+ sc->sc_bst = tio->tio_bst;
+ bus_space_subregion(tio->tio_bst, tio->tio_bsh,
+ loc->loc_offset, loc->loc_size, &sc->sc_bsh);
+ mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
+ cv_init(&sc->sc_cv, device_xname(self));
+
+ aprint_naive("\n");
+ aprint_normal(": I2C%d\n", loc->loc_port + 1);
Home |
Main Index |
Thread Index |
Old Index