xref: /netbsd-src/sys/dev/i2c/spdmem_i2c.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /* $NetBSD: spdmem_i2c.c,v 1.1 2010/03/24 00:31:41 pgoyette 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.1 2010/03/24 00:31:41 pgoyette Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/device.h>
42 #include <sys/endian.h>
43 #include <sys/sysctl.h>
44 #include <machine/bswap.h>
45 
46 #include <dev/i2c/i2cvar.h>
47 #include <dev/ic/spdmemreg.h>
48 #include <dev/ic/spdmemvar.h>
49 
50 /* Constants for matching i2c bus address */
51 #define SPDMEM_I2C_ADDRMASK 0x78
52 #define SPDMEM_I2C_ADDR     0x50
53 
54 struct spdmem_i2c_softc {
55 	struct spdmem_softc sc_base;
56 	i2c_tag_t sc_tag;
57 	i2c_addr_t sc_addr;
58 };
59 
60 static int spdmem_i2c_match(device_t, cfdata_t, void *);
61 static void spdmem_i2c_attach(device_t, device_t, void *);
62 SYSCTL_SETUP_PROTO(sysctl_spdmem_setup);
63 
64 static uint8_t spdmem_i2c_read(struct spdmem_softc *, uint8_t);
65 
66 CFATTACH_DECL_NEW(spdmem_iic, sizeof(struct spdmem_i2c_softc),
67     spdmem_i2c_match, spdmem_i2c_attach, NULL, NULL);
68 
69 static int
70 spdmem_i2c_match(device_t parent, cfdata_t match, void *aux)
71 {
72 	struct i2c_attach_args *ia = aux;
73 	struct spdmem_i2c_softc sc;
74 
75 	if (ia->ia_name) {
76 		/* add other names as we find more firmware variations */
77 		if (strcmp(ia->ia_name, "dimm-spd"))
78 			return 0;
79 	}
80 
81 	/* only do this lame test when not using direct config */
82 	if (ia->ia_name == NULL) {
83 		if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) != SPDMEM_I2C_ADDR)
84 			return 0;
85 	}
86 
87 	sc.sc_tag = ia->ia_tag;
88 	sc.sc_addr = ia->ia_addr;
89 	sc.sc_base.sc_read = spdmem_i2c_read;
90 
91 	return spdmem_common_probe(&sc.sc_base);
92 }
93 
94 static void
95 spdmem_i2c_attach(device_t parent, device_t self, void *aux)
96 {
97 	struct spdmem_i2c_softc *sc = device_private(self);
98 	struct i2c_attach_args *ia = aux;
99 
100 	sc->sc_tag = ia->ia_tag;
101 	sc->sc_addr = ia->ia_addr;
102 	sc->sc_base.sc_read = spdmem_i2c_read;
103 
104 	if (!pmf_device_register(self, NULL, NULL))
105 		aprint_error_dev(self, "couldn't establish power handler\n");
106 
107 	spdmem_common_attach(&sc->sc_base, self);
108 }
109 
110 static uint8_t
111 spdmem_i2c_read(struct spdmem_softc *softc, uint8_t reg)
112 {
113 	uint8_t val;
114 	struct spdmem_i2c_softc *sc = (struct spdmem_i2c_softc *)softc;
115 
116 	iic_acquire_bus(sc->sc_tag, 0);
117 	iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, &reg, 1,
118 		 &val, 1, 0);
119 	iic_release_bus(sc->sc_tag, 0);
120 
121 	return val;
122 }
123