spdmem_i2c.c revision 1.4
11.4Snakayama/* $NetBSD: spdmem_i2c.c,v 1.4 2012/01/22 10:36:52 nakayama Exp $ */
21.1Spgoyette
31.1Spgoyette/*
41.1Spgoyette * Copyright (c) 2007 Nicolas Joly
51.1Spgoyette * Copyright (c) 2007 Paul Goyette
61.1Spgoyette * Copyright (c) 2007 Tobias Nygren
71.1Spgoyette * All rights reserved.
81.1Spgoyette *
91.1Spgoyette * Redistribution and use in source and binary forms, with or without
101.1Spgoyette * modification, are permitted provided that the following conditions
111.1Spgoyette * are met:
121.1Spgoyette * 1. Redistributions of source code must retain the above copyright
131.1Spgoyette *    notice, this list of conditions and the following disclaimer.
141.1Spgoyette * 2. Redistributions in binary form must reproduce the above copyright
151.1Spgoyette *    notice, this list of conditions and the following disclaimer in the
161.1Spgoyette *    documentation and/or other materials provided with the distribution.
171.1Spgoyette * 3. The name of the author may not be used to endorse or promote products
181.1Spgoyette *    derived from this software without specific prior written permission.
191.1Spgoyette *
201.1Spgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
211.1Spgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
221.1Spgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
231.1Spgoyette * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
241.1Spgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
251.1Spgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
261.1Spgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
271.1Spgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
281.1Spgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
291.1Spgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
301.1Spgoyette * POSSIBILITY OF SUCH DAMAGE.
311.1Spgoyette */
321.1Spgoyette
331.1Spgoyette/*
341.1Spgoyette * Serial Presence Detect (SPD) memory identification
351.1Spgoyette */
361.1Spgoyette
371.1Spgoyette#include <sys/cdefs.h>
381.4Snakayama__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.4 2012/01/22 10:36:52 nakayama Exp $");
391.1Spgoyette
401.1Spgoyette#include <sys/param.h>
411.1Spgoyette#include <sys/device.h>
421.1Spgoyette#include <sys/endian.h>
431.2Spgoyette#include <sys/module.h>
441.1Spgoyette#include <sys/sysctl.h>
451.1Spgoyette#include <machine/bswap.h>
461.1Spgoyette
471.1Spgoyette#include <dev/i2c/i2cvar.h>
481.1Spgoyette#include <dev/ic/spdmemreg.h>
491.1Spgoyette#include <dev/ic/spdmemvar.h>
501.1Spgoyette
511.1Spgoyette/* Constants for matching i2c bus address */
521.1Spgoyette#define SPDMEM_I2C_ADDRMASK 0x78
531.1Spgoyette#define SPDMEM_I2C_ADDR     0x50
541.1Spgoyette
551.1Spgoyettestruct spdmem_i2c_softc {
561.1Spgoyette	struct spdmem_softc sc_base;
571.1Spgoyette	i2c_tag_t sc_tag;
581.1Spgoyette	i2c_addr_t sc_addr;
591.1Spgoyette};
601.1Spgoyette
611.2Spgoyettestatic int  spdmem_i2c_match(device_t, cfdata_t, void *);
621.1Spgoyettestatic void spdmem_i2c_attach(device_t, device_t, void *);
631.2Spgoyettestatic int  spdmem_i2c_detach(device_t, int);
641.2Spgoyette
651.2SpgoyetteCFATTACH_DECL_NEW(spdmem_iic, sizeof(struct spdmem_i2c_softc),
661.2Spgoyette    spdmem_i2c_match, spdmem_i2c_attach, spdmem_i2c_detach, NULL);
671.1Spgoyette
681.1Spgoyettestatic uint8_t spdmem_i2c_read(struct spdmem_softc *, uint8_t);
691.1Spgoyette
701.2SpgoyetteSYSCTL_SETUP_PROTO(sysctl_spdmem_setup);
711.1Spgoyette
721.1Spgoyettestatic int
731.1Spgoyettespdmem_i2c_match(device_t parent, cfdata_t match, void *aux)
741.1Spgoyette{
751.1Spgoyette	struct i2c_attach_args *ia = aux;
761.1Spgoyette	struct spdmem_i2c_softc sc;
771.1Spgoyette
781.1Spgoyette	if (ia->ia_name) {
791.1Spgoyette		/* add other names as we find more firmware variations */
801.4Snakayama		if (strcmp(ia->ia_name, "dimm-spd") &&
811.4Snakayama		    strcmp(ia->ia_name, "dimm"))
821.1Spgoyette			return 0;
831.1Spgoyette	}
841.1Spgoyette
851.1Spgoyette	/* only do this lame test when not using direct config */
861.1Spgoyette	if (ia->ia_name == NULL) {
871.1Spgoyette		if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) != SPDMEM_I2C_ADDR)
881.1Spgoyette			return 0;
891.1Spgoyette	}
901.1Spgoyette
911.1Spgoyette	sc.sc_tag = ia->ia_tag;
921.1Spgoyette	sc.sc_addr = ia->ia_addr;
931.1Spgoyette	sc.sc_base.sc_read = spdmem_i2c_read;
941.1Spgoyette
951.1Spgoyette	return spdmem_common_probe(&sc.sc_base);
961.1Spgoyette}
971.1Spgoyette
981.1Spgoyettestatic void
991.1Spgoyettespdmem_i2c_attach(device_t parent, device_t self, void *aux)
1001.1Spgoyette{
1011.1Spgoyette	struct spdmem_i2c_softc *sc = device_private(self);
1021.1Spgoyette	struct i2c_attach_args *ia = aux;
1031.1Spgoyette
1041.1Spgoyette	sc->sc_tag = ia->ia_tag;
1051.1Spgoyette	sc->sc_addr = ia->ia_addr;
1061.1Spgoyette	sc->sc_base.sc_read = spdmem_i2c_read;
1071.1Spgoyette
1081.1Spgoyette	if (!pmf_device_register(self, NULL, NULL))
1091.1Spgoyette		aprint_error_dev(self, "couldn't establish power handler\n");
1101.1Spgoyette
1111.1Spgoyette	spdmem_common_attach(&sc->sc_base, self);
1121.1Spgoyette}
1131.1Spgoyette
1141.2Spgoyettestatic int
1151.2Spgoyettespdmem_i2c_detach(device_t self, int flags)
1161.2Spgoyette{
1171.2Spgoyette	struct spdmem_i2c_softc *sc = device_private(self);
1181.2Spgoyette
1191.2Spgoyette	pmf_device_deregister(self);
1201.2Spgoyette
1211.2Spgoyette	return spdmem_common_detach(&sc->sc_base, self);
1221.2Spgoyette}
1231.2Spgoyette
1241.1Spgoyettestatic uint8_t
1251.1Spgoyettespdmem_i2c_read(struct spdmem_softc *softc, uint8_t reg)
1261.1Spgoyette{
1271.1Spgoyette	uint8_t val;
1281.1Spgoyette	struct spdmem_i2c_softc *sc = (struct spdmem_i2c_softc *)softc;
1291.1Spgoyette
1301.1Spgoyette	iic_acquire_bus(sc->sc_tag, 0);
1311.1Spgoyette	iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, &reg, 1,
1321.1Spgoyette		 &val, 1, 0);
1331.1Spgoyette	iic_release_bus(sc->sc_tag, 0);
1341.1Spgoyette
1351.1Spgoyette	return val;
1361.1Spgoyette}
1371.2Spgoyette
1381.3SjmcneillMODULE(MODULE_CLASS_DRIVER, spdmem, "iic");
1391.2Spgoyette
1401.2Spgoyette#ifdef _MODULE
1411.2Spgoyette#include "ioconf.c"
1421.2Spgoyette#endif
1431.2Spgoyette
1441.2Spgoyettestatic int
1451.2Spgoyettespdmem_modcmd(modcmd_t cmd, void *opaque)
1461.2Spgoyette{
1471.2Spgoyette	int error = 0;
1481.2Spgoyette
1491.2Spgoyette	switch (cmd) {
1501.2Spgoyette	case MODULE_CMD_INIT:
1511.2Spgoyette#ifdef _MODULE
1521.2Spgoyette		error = config_init_component(cfdriver_ioconf_spdmem,
1531.2Spgoyette		    cfattach_ioconf_spdmem, cfdata_ioconf_spdmem);
1541.2Spgoyette#endif
1551.2Spgoyette		return error;
1561.2Spgoyette	case MODULE_CMD_FINI:
1571.2Spgoyette#ifdef _MODULE
1581.2Spgoyette		error = config_fini_component(cfdriver_ioconf_spdmem,
1591.2Spgoyette		    cfattach_ioconf_spdmem, cfdata_ioconf_spdmem);
1601.2Spgoyette#endif
1611.2Spgoyette		return error;
1621.2Spgoyette	default:
1631.2Spgoyette		return ENOTTY;
1641.2Spgoyette	}
1651.2Spgoyette}
166