1*c7fb772bSthorpej /* $NetBSD: tsciic.c,v 1.4 2021/08/07 16:18:41 thorpej Exp $ */
25c7ee0a0Sjdc
35c7ee0a0Sjdc /*
45c7ee0a0Sjdc * Copyright (c) 2013 The NetBSD Foundation, Inc.
55c7ee0a0Sjdc * All rights reserved.
65c7ee0a0Sjdc *
75c7ee0a0Sjdc * This code is derived from software contributed to The NetBSD Foundation
85c7ee0a0Sjdc * by Julian Coleman.
95c7ee0a0Sjdc *
105c7ee0a0Sjdc * Redistribution and use in source and binary forms, with or without
115c7ee0a0Sjdc * modification, are permitted provided that the following conditions
125c7ee0a0Sjdc * are met:
135c7ee0a0Sjdc * 1. Redistributions of source code must retain the above copyright
145c7ee0a0Sjdc * notice, this list of conditions and the following disclaimer.
155c7ee0a0Sjdc * 2. Redistributions in binary form must reproduce the above copyright
165c7ee0a0Sjdc * notice, this list of conditions and the following disclaimer in the
175c7ee0a0Sjdc * documentation and/or other materials provided with the distribution.
185c7ee0a0Sjdc *
195c7ee0a0Sjdc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
205c7ee0a0Sjdc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
215c7ee0a0Sjdc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
225c7ee0a0Sjdc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
235c7ee0a0Sjdc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
245c7ee0a0Sjdc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
255c7ee0a0Sjdc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
265c7ee0a0Sjdc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
275c7ee0a0Sjdc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
285c7ee0a0Sjdc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
295c7ee0a0Sjdc * POSSIBILITY OF SUCH DAMAGE.
305c7ee0a0Sjdc */
315c7ee0a0Sjdc
325c7ee0a0Sjdc #include <sys/cdefs.h>
335c7ee0a0Sjdc
34*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: tsciic.c,v 1.4 2021/08/07 16:18:41 thorpej Exp $");
355c7ee0a0Sjdc
365c7ee0a0Sjdc #include <sys/param.h>
375c7ee0a0Sjdc #include <sys/systm.h>
385c7ee0a0Sjdc #include <sys/device.h>
395c7ee0a0Sjdc
405c7ee0a0Sjdc #include <alpha/pci/tsreg.h>
415c7ee0a0Sjdc #include <alpha/pci/tsvar.h>
425c7ee0a0Sjdc
435c7ee0a0Sjdc #include <dev/i2c/i2cvar.h>
445c7ee0a0Sjdc #include <dev/i2c/i2c_bitbang.h>
455c7ee0a0Sjdc #include <dev/i2c/ddcvar.h>
465c7ee0a0Sjdc
475c7ee0a0Sjdc /* I2C glue */
485c7ee0a0Sjdc static int tsciic_send_start(void *, int);
495c7ee0a0Sjdc static int tsciic_send_stop(void *, int);
505c7ee0a0Sjdc static int tsciic_initiate_xfer(void *, i2c_addr_t, int);
515c7ee0a0Sjdc static int tsciic_read_byte(void *, uint8_t *, int);
525c7ee0a0Sjdc static int tsciic_write_byte(void *, uint8_t, int);
535c7ee0a0Sjdc
545c7ee0a0Sjdc /* I2C bitbang glue */
555c7ee0a0Sjdc static void tsciicbb_set_bits(void *, uint32_t);
565c7ee0a0Sjdc static void tsciicbb_set_dir(void *, uint32_t);
575c7ee0a0Sjdc static uint32_t tsciicbb_read(void *);
585c7ee0a0Sjdc
595c7ee0a0Sjdc #define MPD_BIT_SDA 0x01
605c7ee0a0Sjdc #define MPD_BIT_SCL 0x02
615c7ee0a0Sjdc static const struct i2c_bitbang_ops tsciicbb_ops = {
625c7ee0a0Sjdc tsciicbb_set_bits,
635c7ee0a0Sjdc tsciicbb_set_dir,
645c7ee0a0Sjdc tsciicbb_read,
655c7ee0a0Sjdc {
665c7ee0a0Sjdc MPD_BIT_SDA,
675c7ee0a0Sjdc MPD_BIT_SCL,
685c7ee0a0Sjdc 0,
695c7ee0a0Sjdc 0
705c7ee0a0Sjdc }
715c7ee0a0Sjdc };
725c7ee0a0Sjdc
735c7ee0a0Sjdc void
tsciic_init(device_t self)742685996bSthorpej tsciic_init(device_t self)
752685996bSthorpej {
765c7ee0a0Sjdc struct tsciic_softc *sc = device_private(self);
775c7ee0a0Sjdc struct i2cbus_attach_args iba;
785c7ee0a0Sjdc
79601e1783Sthorpej iic_tag_init(&sc->sc_i2c);
805c7ee0a0Sjdc sc->sc_i2c.ic_cookie = sc;
815c7ee0a0Sjdc sc->sc_i2c.ic_send_start = tsciic_send_start;
825c7ee0a0Sjdc sc->sc_i2c.ic_send_stop = tsciic_send_stop;
835c7ee0a0Sjdc sc->sc_i2c.ic_initiate_xfer = tsciic_initiate_xfer;
845c7ee0a0Sjdc sc->sc_i2c.ic_read_byte = tsciic_read_byte;
855c7ee0a0Sjdc sc->sc_i2c.ic_write_byte = tsciic_write_byte;
865c7ee0a0Sjdc
875c7ee0a0Sjdc memset(&iba, 0, sizeof(iba));
885c7ee0a0Sjdc iba.iba_tag = &sc->sc_i2c;
895c7ee0a0Sjdc
90*c7fb772bSthorpej config_found(self, &iba, iicbus_print, CFARGS_NONE);
915c7ee0a0Sjdc }
925c7ee0a0Sjdc
935c7ee0a0Sjdc /* I2C bitbanging */
945c7ee0a0Sjdc static void
tsciicbb_set_bits(void * cookie,uint32_t bits)955c7ee0a0Sjdc tsciicbb_set_bits(void *cookie, uint32_t bits)
965c7ee0a0Sjdc {
975c7ee0a0Sjdc uint64_t val;
985c7ee0a0Sjdc
995c7ee0a0Sjdc val = (bits & MPD_BIT_SDA ? MPD_DS : 0) |
1005c7ee0a0Sjdc (bits & MPD_BIT_SCL ? MPD_CKS : 0);
1015c7ee0a0Sjdc alpha_mb();
1025c7ee0a0Sjdc STQP(TS_C_MPD) = val;
1035c7ee0a0Sjdc alpha_mb();
1045c7ee0a0Sjdc }
1055c7ee0a0Sjdc
1065c7ee0a0Sjdc static void
tsciicbb_set_dir(void * cookie,uint32_t dir)1075c7ee0a0Sjdc tsciicbb_set_dir(void *cookie, uint32_t dir)
1085c7ee0a0Sjdc {
1095c7ee0a0Sjdc /* Nothing to do */
1105c7ee0a0Sjdc }
1115c7ee0a0Sjdc
1125c7ee0a0Sjdc static uint32_t
tsciicbb_read(void * cookie)1135c7ee0a0Sjdc tsciicbb_read(void *cookie)
1145c7ee0a0Sjdc {
1155c7ee0a0Sjdc uint64_t val;
1165c7ee0a0Sjdc uint32_t bits;
1175c7ee0a0Sjdc
1185c7ee0a0Sjdc val = LDQP(TS_C_MPD);
1195c7ee0a0Sjdc bits = (val & MPD_DR ? MPD_BIT_SDA : 0) |
1205c7ee0a0Sjdc (val & MPD_CKR ? MPD_BIT_SCL : 0);
1215c7ee0a0Sjdc return bits;
1225c7ee0a0Sjdc }
1235c7ee0a0Sjdc
1245c7ee0a0Sjdc /* higher level I2C stuff */
1255c7ee0a0Sjdc static int
tsciic_send_start(void * cookie,int flags)1265c7ee0a0Sjdc tsciic_send_start(void *cookie, int flags)
1275c7ee0a0Sjdc {
1285c7ee0a0Sjdc return (i2c_bitbang_send_start(cookie, flags, &tsciicbb_ops));
1295c7ee0a0Sjdc }
1305c7ee0a0Sjdc
1315c7ee0a0Sjdc static int
tsciic_send_stop(void * cookie,int flags)1325c7ee0a0Sjdc tsciic_send_stop(void *cookie, int flags)
1335c7ee0a0Sjdc {
1345c7ee0a0Sjdc return (i2c_bitbang_send_stop(cookie, flags, &tsciicbb_ops));
1355c7ee0a0Sjdc }
1365c7ee0a0Sjdc
1375c7ee0a0Sjdc static int
tsciic_initiate_xfer(void * cookie,i2c_addr_t addr,int flags)1385c7ee0a0Sjdc tsciic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
1395c7ee0a0Sjdc {
1405c7ee0a0Sjdc return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
1415c7ee0a0Sjdc &tsciicbb_ops));
1425c7ee0a0Sjdc }
1435c7ee0a0Sjdc
1445c7ee0a0Sjdc static int
tsciic_read_byte(void * cookie,uint8_t * valp,int flags)1455c7ee0a0Sjdc tsciic_read_byte(void *cookie, uint8_t *valp, int flags)
1465c7ee0a0Sjdc {
1475c7ee0a0Sjdc return (i2c_bitbang_read_byte(cookie, valp, flags, &tsciicbb_ops));
1485c7ee0a0Sjdc }
1495c7ee0a0Sjdc
1505c7ee0a0Sjdc static int
tsciic_write_byte(void * cookie,uint8_t val,int flags)1515c7ee0a0Sjdc tsciic_write_byte(void *cookie, uint8_t val, int flags)
1525c7ee0a0Sjdc {
1535c7ee0a0Sjdc return (i2c_bitbang_write_byte(cookie, val, flags, &tsciicbb_ops));
1545c7ee0a0Sjdc }
155