Home | History | Annotate | Line # | Download | only in pci
      1 /*	$NetBSD: tsciic.c,v 1.5 2025/09/15 13:23:00 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Julian Coleman.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 
     34 __KERNEL_RCSID(0, "$NetBSD: tsciic.c,v 1.5 2025/09/15 13:23:00 thorpej Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/device.h>
     39 
     40 #include <alpha/pci/tsreg.h>
     41 #include <alpha/pci/tsvar.h>
     42 
     43 #include <dev/i2c/i2cvar.h>
     44 #include <dev/i2c/i2c_bitbang.h>
     45 #include <dev/i2c/ddcvar.h>
     46 
     47 /* I2C glue */
     48 static int tsciic_send_start(void *, int);
     49 static int tsciic_send_stop(void *, int);
     50 static int tsciic_initiate_xfer(void *, i2c_addr_t, int);
     51 static int tsciic_read_byte(void *, uint8_t *, int);
     52 static int tsciic_write_byte(void *, uint8_t, int);
     53 
     54 /* I2C bitbang glue */
     55 static void tsciicbb_set_bits(void *, uint32_t);
     56 static void tsciicbb_set_dir(void *, uint32_t);
     57 static uint32_t tsciicbb_read(void *);
     58 
     59 #define MPD_BIT_SDA 0x01
     60 #define MPD_BIT_SCL 0x02
     61 static const struct i2c_bitbang_ops tsciicbb_ops = {
     62 	tsciicbb_set_bits,
     63 	tsciicbb_set_dir,
     64 	tsciicbb_read,
     65 	{
     66 		MPD_BIT_SDA,
     67 		MPD_BIT_SCL,
     68 		0,
     69 		0
     70 	}
     71 };
     72 
     73 void
     74 tsciic_init(device_t self)
     75 {
     76 	struct tsciic_softc *sc = device_private(self);
     77 
     78 	iic_tag_init(&sc->sc_i2c);
     79 	sc->sc_i2c.ic_cookie = sc;
     80 	sc->sc_i2c.ic_send_start = tsciic_send_start;
     81 	sc->sc_i2c.ic_send_stop = tsciic_send_stop;
     82 	sc->sc_i2c.ic_initiate_xfer = tsciic_initiate_xfer;
     83 	sc->sc_i2c.ic_read_byte = tsciic_read_byte;
     84 	sc->sc_i2c.ic_write_byte = tsciic_write_byte;
     85 
     86 	iicbus_attach(self, &sc->sc_i2c);
     87 }
     88 
     89 /* I2C bitbanging */
     90 static void
     91 tsciicbb_set_bits(void *cookie, uint32_t bits)
     92 {
     93 	uint64_t val;
     94 
     95 	val = (bits & MPD_BIT_SDA ? MPD_DS : 0) |
     96 	    (bits & MPD_BIT_SCL ? MPD_CKS : 0);
     97 	alpha_mb();
     98 	STQP(TS_C_MPD) = val;
     99 	alpha_mb();
    100 }
    101 
    102 static void
    103 tsciicbb_set_dir(void *cookie, uint32_t dir)
    104 {
    105 	/* Nothing to do */
    106 }
    107 
    108 static uint32_t
    109 tsciicbb_read(void *cookie)
    110 {
    111 	uint64_t val;
    112 	uint32_t bits;
    113 
    114 	val = LDQP(TS_C_MPD);
    115 	bits = (val & MPD_DR ? MPD_BIT_SDA : 0) |
    116 	    (val & MPD_CKR ? MPD_BIT_SCL : 0);
    117 	return bits;
    118 }
    119 
    120 /* higher level I2C stuff */
    121 static int
    122 tsciic_send_start(void *cookie, int flags)
    123 {
    124 	return (i2c_bitbang_send_start(cookie, flags, &tsciicbb_ops));
    125 }
    126 
    127 static int
    128 tsciic_send_stop(void *cookie, int flags)
    129 {
    130 	return (i2c_bitbang_send_stop(cookie, flags, &tsciicbb_ops));
    131 }
    132 
    133 static int
    134 tsciic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    135 {
    136 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
    137 	    &tsciicbb_ops));
    138 }
    139 
    140 static int
    141 tsciic_read_byte(void *cookie, uint8_t *valp, int flags)
    142 {
    143 	return (i2c_bitbang_read_byte(cookie, valp, flags, &tsciicbb_ops));
    144 }
    145 
    146 static int
    147 tsciic_write_byte(void *cookie, uint8_t val, int flags)
    148 {
    149 	return (i2c_bitbang_write_byte(cookie, val, flags, &tsciicbb_ops));
    150 }
    151