spdmem_i2c.c revision 1.12
1/* $NetBSD: spdmem_i2c.c,v 1.12 2016/01/05 11:49:32 msaitoh Exp $ */
2
3/*
4 * Copyright (c) 2007 Nicolas Joly
5 * Copyright (c) 2007 Paul Goyette
6 * Copyright (c) 2007 Tobias Nygren
7 * Copyright (c) 2015 Michael van Elst
8 * All rights reserved.
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 * 3. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/*
35 * Serial Presence Detect (SPD) memory identification
36 *
37 * JEDEC standard No. 21-C
38 * JEDEC document 4_01_06R24
39 * - Definitions of the EE1004-v 4 Kbit Serial Presence Detect EEPROM [...]
40 */
41
42#include <sys/cdefs.h>
43__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.12 2016/01/05 11:49:32 msaitoh Exp $");
44
45#include <sys/param.h>
46#include <sys/device.h>
47#include <sys/endian.h>
48#include <sys/module.h>
49#include <sys/sysctl.h>
50#include <machine/bswap.h>
51
52#include <dev/i2c/i2cvar.h>
53#include <dev/ic/spdmemreg.h>
54#include <dev/ic/spdmemvar.h>
55
56/* Constants for matching i2c bus address */
57#define SPDMEM_I2C_ADDRMASK 0xfff8
58#define SPDMEM_I2C_ADDR     0x50
59#define SPDCTL_I2C_ADDR     0x30
60
61/* set write protection */
62#define SPDCTL_SWP0         (SPDCTL_I2C_ADDR + 1)
63#define SPDCTL_SWP1         (SPDCTL_I2C_ADDR + 4)
64#define SPDCTL_SWP2         (SPDCTL_I2C_ADDR + 5)
65#define SPDCTL_SWP3         (SPDCTL_I2C_ADDR + 0)
66
67/* clear write protections */
68#define SPDCTL_CWP          (SPDCTL_I2C_ADDR + 3)
69
70/* read protection status */
71#define SPDCTL_RPS0         (SPDCTL_I2C_ADDR + 1)
72#define SPDCTL_RPS1         (SPDCTL_I2C_ADDR + 4)
73#define SPDCTL_RPS2         (SPDCTL_I2C_ADDR + 5)
74#define SPDCTL_RPS3         (SPDCTL_I2C_ADDR + 0)
75
76/* select page address */
77#define SPDCTL_SPA0         (SPDCTL_I2C_ADDR + 6)
78#define SPDCTL_SPA1         (SPDCTL_I2C_ADDR + 7)
79
80/* read page address */
81#define SPDCTL_RPA          (SPDCTL_I2C_ADDR + 6)
82
83struct spdmem_i2c_softc {
84	struct spdmem_softc sc_base;
85	i2c_tag_t sc_tag;
86	i2c_addr_t sc_addr; /* EEPROM */
87	i2c_addr_t sc_page0;
88	i2c_addr_t sc_page1;
89};
90
91static int  spdmem_i2c_match(device_t, cfdata_t, void *);
92static void spdmem_i2c_attach(device_t, device_t, void *);
93static int  spdmem_i2c_detach(device_t, int);
94
95CFATTACH_DECL_NEW(spdmem_iic, sizeof(struct spdmem_i2c_softc),
96    spdmem_i2c_match, spdmem_i2c_attach, spdmem_i2c_detach, NULL);
97
98static int spdmem_i2c_read(struct spdmem_softc *, uint16_t, uint8_t *);
99
100static int
101spdmem_i2c_match(device_t parent, cfdata_t match, void *aux)
102{
103	struct i2c_attach_args *ia = aux;
104	struct spdmem_i2c_softc sc;
105
106	if (ia->ia_name) {
107		/* add other names as we find more firmware variations */
108		if (strcmp(ia->ia_name, "dimm-spd") &&
109		    strcmp(ia->ia_name, "dimm"))
110			return 0;
111	}
112
113	/* only do this lame test when not using direct config */
114	if (ia->ia_name == NULL) {
115		if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) != SPDMEM_I2C_ADDR)
116			return 0;
117	}
118
119	sc.sc_tag = ia->ia_tag;
120	sc.sc_addr = ia->ia_addr;
121	sc.sc_page0 = SPDCTL_SPA0;
122	sc.sc_page1 = SPDCTL_SPA1;
123	sc.sc_base.sc_read = spdmem_i2c_read;
124
125	return spdmem_common_probe(&sc.sc_base);
126}
127
128static void
129spdmem_i2c_attach(device_t parent, device_t self, void *aux)
130{
131	struct spdmem_i2c_softc *sc = device_private(self);
132	struct i2c_attach_args *ia = aux;
133
134	sc->sc_tag = ia->ia_tag;
135	sc->sc_addr = ia->ia_addr;
136	sc->sc_page0 = SPDCTL_SPA0;
137	sc->sc_page1 = SPDCTL_SPA1;
138	sc->sc_base.sc_read = spdmem_i2c_read;
139
140	if (!pmf_device_register(self, NULL, NULL))
141		aprint_error_dev(self, "couldn't establish power handler\n");
142
143	spdmem_common_attach(&sc->sc_base, self);
144}
145
146static int
147spdmem_i2c_detach(device_t self, int flags)
148{
149	struct spdmem_i2c_softc *sc = device_private(self);
150
151	pmf_device_deregister(self);
152
153	return spdmem_common_detach(&sc->sc_base, self);
154}
155
156static int
157spdmem_i2c_read(struct spdmem_softc *softc, uint16_t addr, uint8_t *val)
158{
159	uint8_t reg;
160	struct spdmem_i2c_softc *sc = (struct spdmem_i2c_softc *)softc;
161	static uint8_t dummy = 0;
162	int rv;
163
164	reg = addr & 0xff;
165
166	iic_acquire_bus(sc->sc_tag, 0);
167
168	if (addr & 0x100) {
169		rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_page1,
170		    &dummy, 1, NULL, 0, I2C_F_POLL);
171		rv |= iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
172		    &reg, 1, val, 1, I2C_F_POLL);
173		rv |= iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
174		    sc->sc_page0, &dummy, 1, NULL, 0, I2C_F_POLL);
175	} else {
176		rv = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
177		    &reg, 1, val, 1, I2C_F_POLL);
178	}
179
180	iic_release_bus(sc->sc_tag, 0);
181
182	return rv;
183}
184
185MODULE(MODULE_CLASS_DRIVER, spdmem, "i2cexec");
186
187#ifdef _MODULE
188#include "ioconf.c"
189#endif
190
191static int
192spdmem_modcmd(modcmd_t cmd, void *opaque)
193{
194	int error = 0;
195#ifdef _MODULE
196	static struct sysctllog *spdmem_sysctl_clog;
197#endif
198
199	switch (cmd) {
200	case MODULE_CMD_INIT:
201#ifdef _MODULE
202		error = config_init_component(cfdriver_ioconf_spdmem,
203		    cfattach_ioconf_spdmem, cfdata_ioconf_spdmem);
204#endif
205		return error;
206	case MODULE_CMD_FINI:
207#ifdef _MODULE
208		error = config_fini_component(cfdriver_ioconf_spdmem,
209		    cfattach_ioconf_spdmem, cfdata_ioconf_spdmem);
210		sysctl_teardown(&spdmem_sysctl_clog);
211#endif
212		return error;
213	default:
214		return ENOTTY;
215	}
216}
217