1/* $NetBSD: spdmem_i2c.c,v 1.3 2011/10/02 19:03:56 jmcneill Exp $ */
2
3/*
4 * Copyright (c) 2007 Nicolas Joly
5 * Copyright (c) 2007 Paul Goyette
6 * Copyright (c) 2007 Tobias Nygren
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Serial Presence Detect (SPD) memory identification
35 */
36
37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.3 2011/10/02 19:03:56 jmcneill Exp $");
39
40#include <sys/param.h>
41#include <sys/device.h>
42#include <sys/endian.h>
43#include <sys/module.h>
44#include <sys/sysctl.h>
45#include <machine/bswap.h>
46
47#include <dev/i2c/i2cvar.h>
48#include <dev/ic/spdmemreg.h>
49#include <dev/ic/spdmemvar.h>
50
51/* Constants for matching i2c bus address */
52#define SPDMEM_I2C_ADDRMASK 0x78
53#define SPDMEM_I2C_ADDR     0x50
54
55struct spdmem_i2c_softc {
56	struct spdmem_softc sc_base;
57	i2c_tag_t sc_tag;
58	i2c_addr_t sc_addr;
59};
60
61static int  spdmem_i2c_match(device_t, cfdata_t, void *);
62static void spdmem_i2c_attach(device_t, device_t, void *);
63static int  spdmem_i2c_detach(device_t, int);
64
65CFATTACH_DECL_NEW(spdmem_iic, sizeof(struct spdmem_i2c_softc),
66    spdmem_i2c_match, spdmem_i2c_attach, spdmem_i2c_detach, NULL);
67
68static uint8_t spdmem_i2c_read(struct spdmem_softc *, uint8_t);
69
70SYSCTL_SETUP_PROTO(sysctl_spdmem_setup);
71
72static int
73spdmem_i2c_match(device_t parent, cfdata_t match, void *aux)
74{
75	struct i2c_attach_args *ia = aux;
76	struct spdmem_i2c_softc sc;
77
78	if (ia->ia_name) {
79		/* add other names as we find more firmware variations */
80		if (strcmp(ia->ia_name, "dimm-spd") &&
81		    strcmp(ia->ia_name, "dimm"))
82			return 0;
83	}
84
85	/* only do this lame test when not using direct config */
86	if (ia->ia_name == NULL) {
87		if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) != SPDMEM_I2C_ADDR)
88			return 0;
89	}
90
91	sc.sc_tag = ia->ia_tag;
92	sc.sc_addr = ia->ia_addr;
93	sc.sc_base.sc_read = spdmem_i2c_read;
94
95	return spdmem_common_probe(&sc.sc_base);
96}
97
98static void
99spdmem_i2c_attach(device_t parent, device_t self, void *aux)
100{
101	struct spdmem_i2c_softc *sc = device_private(self);
102	struct i2c_attach_args *ia = aux;
103
104	sc->sc_tag = ia->ia_tag;
105	sc->sc_addr = ia->ia_addr;
106	sc->sc_base.sc_read = spdmem_i2c_read;
107
108	if (!pmf_device_register(self, NULL, NULL))
109		aprint_error_dev(self, "couldn't establish power handler\n");
110
111	spdmem_common_attach(&sc->sc_base, self);
112}
113
114static int
115spdmem_i2c_detach(device_t self, int flags)
116{
117	struct spdmem_i2c_softc *sc = device_private(self);
118
119	pmf_device_deregister(self);
120
121	return spdmem_common_detach(&sc->sc_base, self);
122}
123
124static uint8_t
125spdmem_i2c_read(struct spdmem_softc *softc, uint8_t reg)
126{
127	uint8_t val;
128	struct spdmem_i2c_softc *sc = (struct spdmem_i2c_softc *)softc;
129
130	iic_acquire_bus(sc->sc_tag, 0);
131	iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, &reg, 1,
132		 &val, 1, 0);
133	iic_release_bus(sc->sc_tag, 0);
134
135	return val;
136}
137
138MODULE(MODULE_CLASS_DRIVER, spdmem, "iic");
139
140#ifdef _MODULE
141#include "ioconf.c"
142#endif
143
144static int
145spdmem_modcmd(modcmd_t cmd, void *opaque)
146{
147	int error = 0;
148
149	switch (cmd) {
150	case MODULE_CMD_INIT:
151#ifdef _MODULE
152		error = config_init_component(cfdriver_ioconf_spdmem,
153		    cfattach_ioconf_spdmem, cfdata_ioconf_spdmem);
154#endif
155		return error;
156	case MODULE_CMD_FINI:
157#ifdef _MODULE
158		error = config_fini_component(cfdriver_ioconf_spdmem,
159		    cfattach_ioconf_spdmem, cfdata_ioconf_spdmem);
160#endif
161		return error;
162	default:
163		return ENOTTY;
164	}
165}
166