xref: /netbsd-src/sys/arch/macppc/macppc/memory.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: memory.c,v 1.9 2021/08/07 16:18:58 thorpej Exp $	*/
2 /*	$OpenBSD: mem.c,v 1.15 2007/10/14 17:29:04 kettenis Exp $	*/
3 
4 /*-
5  * Copyright (c) 1988 University of Utah.
6  * Copyright (c) 1982, 1986, 1990, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * the Systems Programming Group of the University of Utah Computer
11  * Science Department.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)mem.c	8.3 (Berkeley) 1/12/94
38  */
39 
40 /*
41  *	The EEPROMs for Serial Presence Detect don't show up in the
42  * 	OpenFirmware tree, but their contents are available through the
43  * 	"dimm-info" property of the "/memory" node.  To make the
44  * 	information available, we fake up an I2C bus with EEPROMs
45  * 	containing the appropriate slices of the "dimm-info" property.
46  */
47 
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: memory.c,v 1.9 2021/08/07 16:18:58 thorpej Exp $");
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/types.h>
54 #include <sys/malloc.h>
55 #include <sys/device.h>
56 
57 #include <machine/autoconf.h>
58 
59 #include <dev/i2c/i2cvar.h>
60 #include <dev/ofw/openfirm.h>
61 
62 struct memory_softc {
63 	device_t	 sc_dev;
64 
65 	u_char		*sc_buf;
66 	int		 sc_len;
67 };
68 
69 /* Size of a single SPD entry in "dimm-info" property. */
70 #define SPD_SIZE	128
71 
72 int	memory_match(device_t, cfdata_t, void *);
73 void	memory_attach(device_t, device_t, void *);
74 
75 CFATTACH_DECL_NEW(memory, sizeof(struct memory_softc), memory_match, memory_attach,
76               NULL, NULL);
77 
78 int	memory_i2c_exec(void *, i2c_op_t, i2c_addr_t,
79    	                const void *, size_t, void *, size_t, int);
80 
81 int
memory_match(device_t parent,cfdata_t cf,void * aux)82 memory_match(device_t parent, cfdata_t cf, void *aux)
83 {
84 	struct confargs *ca = aux;
85 
86 	if (strcmp(ca->ca_name, "memory") == 0)
87 		return (1);
88 	return (0);
89 }
90 
91 void
memory_attach(device_t parent,device_t self,void * aux)92 memory_attach(device_t parent, device_t self, void *aux)
93 {
94 	struct memory_softc *sc = device_private(self);
95 	struct confargs *ca = aux;
96 	struct i2c_controller ic;
97 	struct i2c_attach_args ia;
98 
99 	sc->sc_dev = self;
100 	sc->sc_len = OF_getproplen(ca->ca_node, "dimm-info");
101 	if (sc->sc_len > 0) {
102 		sc->sc_buf = malloc(sc->sc_len, M_DEVBUF, M_WAITOK);
103 		printf(": len=%d", sc->sc_len);
104 	}
105 
106 	printf("\n");
107 
108 	if (sc->sc_len > 0) {
109 		int	addr;
110 
111 		OF_getprop(ca->ca_node, "dimm-info", sc->sc_buf, sc->sc_len);
112 
113 		iic_tag_init(&ic);
114 		ic.ic_cookie = sc;
115 		ic.ic_exec = memory_i2c_exec;
116 
117 		memset(&ia, 0, sizeof ia);
118 		ia.ia_tag = &ic;
119 #ifdef notyet
120 		ia.ia_name = "spd";
121 #endif
122 		addr = 0;
123 		while ((addr * SPD_SIZE) < sc->sc_len) {
124 			/* Skip entries that have not been filled in. */
125 			if (sc->sc_buf[addr * SPD_SIZE] != 0) {
126 				ia.ia_addr = 0x50 + addr;
127 				config_found(self, &ia, NULL, CFARGS_NONE);
128 			}
129 			addr++;
130 		}
131 
132 		/* No need to keep the "dimm-info" contents around. */
133 		free(sc->sc_buf, M_DEVBUF);
134 		sc->sc_len = -1;
135 		iic_tag_fini(&ic);
136 	}
137 }
138 
139 int
memory_i2c_exec(void * cookie,i2c_op_t op,i2c_addr_t addr,const void * cmdbuf,size_t cmdlen,void * buf,size_t len,int flags)140 memory_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
141                 const void *cmdbuf, size_t cmdlen,
142 		void *buf, size_t len, int flags)
143 {
144 	struct memory_softc *sc = cookie;
145 	size_t off;
146 
147 	if (op != I2C_OP_READ_WITH_STOP || cmdlen != 1)
148 		return (EINVAL);
149 
150 	off = (addr - 0x50) * SPD_SIZE + *(const u_char *)cmdbuf;
151 	if ((off + len) > sc->sc_len)
152 		return (EIO);
153 
154 	memcpy(buf, &sc->sc_buf[off], len);
155 	return (0);
156 }
157