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