1 /* $OpenBSD: spdmem_i2c.c,v 1.2 2022/04/06 18:59:28 naddy Exp $ */
2 /* $NetBSD: spdmem.c,v 1.3 2007/09/20 23:09:59 xtraeme Exp $ */
3
4 /*
5 * Copyright (c) 2007 Jonathan Gray <jsg@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /*
21 * Copyright (c) 2007 Nicolas Joly
22 * Copyright (c) 2007 Paul Goyette
23 * Copyright (c) 2007 Tobias Nygren
24 * All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. The name of the author may not be used to endorse or promote products
35 * derived from this software without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
38 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
39 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
41 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 * POSSIBILITY OF SUCH DAMAGE.
48 */
49
50 /*
51 * Serial Presence Detect (SPD) memory identification
52 */
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/device.h>
57
58 #include <dev/spdmemvar.h>
59 #include <dev/i2c/i2cvar.h>
60
61 struct spdmem_iic_softc {
62 struct spdmem_softc sc_base;
63 i2c_tag_t sc_tag;
64 i2c_addr_t sc_addr;
65 };
66
67 int spdmem_iic_match(struct device *, void *, void *);
68 void spdmem_iic_attach(struct device *, struct device *, void *);
69 uint8_t spdmem_iic_read(struct spdmem_softc *, uint8_t);
70
71 const struct cfattach spdmem_iic_ca = {
72 sizeof(struct spdmem_iic_softc), spdmem_iic_match, spdmem_iic_attach
73 };
74
75 int
spdmem_iic_match(struct device * parent,void * match,void * aux)76 spdmem_iic_match(struct device *parent, void *match, void *aux)
77 {
78 struct i2c_attach_args *ia = aux;
79 struct spdmem_iic_softc sc;
80
81 /* clever attachments like openfirmware informed macppc */
82 if (strcmp(ia->ia_name, "spd") == 0)
83 return (1);
84
85 /* dumb, need sanity checks */
86 if (strcmp(ia->ia_name, "eeprom") != 0)
87 return (0);
88
89 sc.sc_tag = ia->ia_tag;
90 sc.sc_addr = ia->ia_addr;
91 sc.sc_base.sc_read = spdmem_iic_read;
92
93 return spdmem_probe(&sc.sc_base);
94 }
95
96 void
spdmem_iic_attach(struct device * parent,struct device * self,void * aux)97 spdmem_iic_attach(struct device *parent, struct device *self, void *aux)
98 {
99 struct spdmem_iic_softc *sc = (struct spdmem_iic_softc *)self;
100 struct i2c_attach_args *ia = aux;
101
102 sc->sc_tag = ia->ia_tag;
103 sc->sc_addr = ia->ia_addr;
104 sc->sc_base.sc_read = spdmem_iic_read;
105
106 printf(":");
107
108 spdmem_attach_common(&sc->sc_base);
109 }
110
111 uint8_t
spdmem_iic_read(struct spdmem_softc * v,uint8_t reg)112 spdmem_iic_read(struct spdmem_softc *v, uint8_t reg)
113 {
114 struct spdmem_iic_softc *sc = (struct spdmem_iic_softc *)v;
115 uint8_t val = 0xff;
116
117 iic_acquire_bus(sc->sc_tag,0);
118 iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
119 ®, sizeof reg, &val, sizeof val, 0);
120 iic_release_bus(sc->sc_tag, 0);
121
122 return val;
123 }
124