spdmem_i2c.c revision 1.1
11.1Spgoyette/* $NetBSD: spdmem_i2c.c,v 1.1 2010/03/24 00:31:41 pgoyette 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.1Spgoyette__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.1 2010/03/24 00:31:41 pgoyette Exp $");
391.1Spgoyette
401.1Spgoyette#include <sys/param.h>
411.1Spgoyette#include <sys/device.h>
421.1Spgoyette#include <sys/endian.h>
431.1Spgoyette#include <sys/sysctl.h>
441.1Spgoyette#include <machine/bswap.h>
451.1Spgoyette
461.1Spgoyette#include <dev/i2c/i2cvar.h>
471.1Spgoyette#include <dev/ic/spdmemreg.h>
481.1Spgoyette#include <dev/ic/spdmemvar.h>
491.1Spgoyette
501.1Spgoyette/* Constants for matching i2c bus address */
511.1Spgoyette#define SPDMEM_I2C_ADDRMASK 0x78
521.1Spgoyette#define SPDMEM_I2C_ADDR     0x50
531.1Spgoyette
541.1Spgoyettestruct spdmem_i2c_softc {
551.1Spgoyette	struct spdmem_softc sc_base;
561.1Spgoyette	i2c_tag_t sc_tag;
571.1Spgoyette	i2c_addr_t sc_addr;
581.1Spgoyette};
591.1Spgoyette
601.1Spgoyettestatic int spdmem_i2c_match(device_t, cfdata_t, void *);
611.1Spgoyettestatic void spdmem_i2c_attach(device_t, device_t, void *);
621.1SpgoyetteSYSCTL_SETUP_PROTO(sysctl_spdmem_setup);
631.1Spgoyette
641.1Spgoyettestatic uint8_t spdmem_i2c_read(struct spdmem_softc *, uint8_t);
651.1Spgoyette
661.1SpgoyetteCFATTACH_DECL_NEW(spdmem_iic, sizeof(struct spdmem_i2c_softc),
671.1Spgoyette    spdmem_i2c_match, spdmem_i2c_attach, NULL, NULL);
681.1Spgoyette
691.1Spgoyettestatic int
701.1Spgoyettespdmem_i2c_match(device_t parent, cfdata_t match, void *aux)
711.1Spgoyette{
721.1Spgoyette	struct i2c_attach_args *ia = aux;
731.1Spgoyette	struct spdmem_i2c_softc sc;
741.1Spgoyette
751.1Spgoyette	if (ia->ia_name) {
761.1Spgoyette		/* add other names as we find more firmware variations */
771.1Spgoyette		if (strcmp(ia->ia_name, "dimm-spd"))
781.1Spgoyette			return 0;
791.1Spgoyette	}
801.1Spgoyette
811.1Spgoyette	/* only do this lame test when not using direct config */
821.1Spgoyette	if (ia->ia_name == NULL) {
831.1Spgoyette		if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) != SPDMEM_I2C_ADDR)
841.1Spgoyette			return 0;
851.1Spgoyette	}
861.1Spgoyette
871.1Spgoyette	sc.sc_tag = ia->ia_tag;
881.1Spgoyette	sc.sc_addr = ia->ia_addr;
891.1Spgoyette	sc.sc_base.sc_read = spdmem_i2c_read;
901.1Spgoyette
911.1Spgoyette	return spdmem_common_probe(&sc.sc_base);
921.1Spgoyette}
931.1Spgoyette
941.1Spgoyettestatic void
951.1Spgoyettespdmem_i2c_attach(device_t parent, device_t self, void *aux)
961.1Spgoyette{
971.1Spgoyette	struct spdmem_i2c_softc *sc = device_private(self);
981.1Spgoyette	struct i2c_attach_args *ia = aux;
991.1Spgoyette
1001.1Spgoyette	sc->sc_tag = ia->ia_tag;
1011.1Spgoyette	sc->sc_addr = ia->ia_addr;
1021.1Spgoyette	sc->sc_base.sc_read = spdmem_i2c_read;
1031.1Spgoyette
1041.1Spgoyette	if (!pmf_device_register(self, NULL, NULL))
1051.1Spgoyette		aprint_error_dev(self, "couldn't establish power handler\n");
1061.1Spgoyette
1071.1Spgoyette	spdmem_common_attach(&sc->sc_base, self);
1081.1Spgoyette}
1091.1Spgoyette
1101.1Spgoyettestatic uint8_t
1111.1Spgoyettespdmem_i2c_read(struct spdmem_softc *softc, uint8_t reg)
1121.1Spgoyette{
1131.1Spgoyette	uint8_t val;
1141.1Spgoyette	struct spdmem_i2c_softc *sc = (struct spdmem_i2c_softc *)softc;
1151.1Spgoyette
1161.1Spgoyette	iic_acquire_bus(sc->sc_tag, 0);
1171.1Spgoyette	iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, &reg, 1,
1181.1Spgoyette		 &val, 1, 0);
1191.1Spgoyette	iic_release_bus(sc->sc_tag, 0);
1201.1Spgoyette
1211.1Spgoyette	return val;
1221.1Spgoyette}
123