1/* $NetBSD: tsciic.c,v 1.4 2021/08/07 16:18:41 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.4 2021/08/07 16:18:41 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 */ 48static int tsciic_send_start(void *, int); 49static int tsciic_send_stop(void *, int); 50static int tsciic_initiate_xfer(void *, i2c_addr_t, int); 51static int tsciic_read_byte(void *, uint8_t *, int); 52static int tsciic_write_byte(void *, uint8_t, int); 53 54/* I2C bitbang glue */ 55static void tsciicbb_set_bits(void *, uint32_t); 56static void tsciicbb_set_dir(void *, uint32_t); 57static uint32_t tsciicbb_read(void *); 58 59#define MPD_BIT_SDA 0x01 60#define MPD_BIT_SCL 0x02 61static 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 73void 74tsciic_init(device_t self) 75{ 76 struct tsciic_softc *sc = device_private(self); 77 struct i2cbus_attach_args iba; 78 79 iic_tag_init(&sc->sc_i2c); 80 sc->sc_i2c.ic_cookie = sc; 81 sc->sc_i2c.ic_send_start = tsciic_send_start; 82 sc->sc_i2c.ic_send_stop = tsciic_send_stop; 83 sc->sc_i2c.ic_initiate_xfer = tsciic_initiate_xfer; 84 sc->sc_i2c.ic_read_byte = tsciic_read_byte; 85 sc->sc_i2c.ic_write_byte = tsciic_write_byte; 86 87 memset(&iba, 0, sizeof(iba)); 88 iba.iba_tag = &sc->sc_i2c; 89 90 config_found(self, &iba, iicbus_print, CFARGS_NONE); 91} 92 93/* I2C bitbanging */ 94static void 95tsciicbb_set_bits(void *cookie, uint32_t bits) 96{ 97 uint64_t val; 98 99 val = (bits & MPD_BIT_SDA ? MPD_DS : 0) | 100 (bits & MPD_BIT_SCL ? MPD_CKS : 0); 101 alpha_mb(); 102 STQP(TS_C_MPD) = val; 103 alpha_mb(); 104} 105 106static void 107tsciicbb_set_dir(void *cookie, uint32_t dir) 108{ 109 /* Nothing to do */ 110} 111 112static uint32_t 113tsciicbb_read(void *cookie) 114{ 115 uint64_t val; 116 uint32_t bits; 117 118 val = LDQP(TS_C_MPD); 119 bits = (val & MPD_DR ? MPD_BIT_SDA : 0) | 120 (val & MPD_CKR ? MPD_BIT_SCL : 0); 121 return bits; 122} 123 124/* higher level I2C stuff */ 125static int 126tsciic_send_start(void *cookie, int flags) 127{ 128 return (i2c_bitbang_send_start(cookie, flags, &tsciicbb_ops)); 129} 130 131static int 132tsciic_send_stop(void *cookie, int flags) 133{ 134 return (i2c_bitbang_send_stop(cookie, flags, &tsciicbb_ops)); 135} 136 137static int 138tsciic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags) 139{ 140 return (i2c_bitbang_initiate_xfer(cookie, addr, flags, 141 &tsciicbb_ops)); 142} 143 144static int 145tsciic_read_byte(void *cookie, uint8_t *valp, int flags) 146{ 147 return (i2c_bitbang_read_byte(cookie, valp, flags, &tsciicbb_ops)); 148} 149 150static int 151tsciic_write_byte(void *cookie, uint8_t val, int flags) 152{ 153 return (i2c_bitbang_write_byte(cookie, val, flags, &tsciicbb_ops)); 154} 155