1 /*-
2 * Copyright (c) 2003-2009 RMI Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of RMI Corporation, nor the names of its contributors,
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * RMI_BSD */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 /*
34 * reading eeprom for the mac address .
35 */
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/bus.h>
43 #include <sys/resource.h>
44 #include <sys/rman.h>
45 #include <sys/sysctl.h>
46
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
49 #include <machine/cpufunc.h>
50 #include <machine/frame.h>
51 #include <machine/resource.h>
52
53 #include <dev/iicbus/iiconf.h>
54 #include <dev/iicbus/iicbus.h>
55
56 #include "iicbus_if.h"
57
58 #define AT24CO_EEPROM_ETH_MACADDR 0x20
59
60 struct at24co2n_softc {
61 uint32_t sc_addr;
62 device_t sc_dev;
63 uint8_t sc_mac_addr[6];
64 };
65
66 static void at24co2n_read_mac(struct at24co2n_softc *);
67
68 static int
at24co2n_probe(device_t dev)69 at24co2n_probe(device_t dev)
70 {
71 device_set_desc(dev, "AT24Co2N-10SE-2.7 EEPROM for mac address");
72 return (0);
73 }
74
75 static int
at24co2n_mac_sysctl(SYSCTL_HANDLER_ARGS)76 at24co2n_mac_sysctl(SYSCTL_HANDLER_ARGS)
77 {
78 struct at24co2n_softc *sc = arg1;
79 char buf[24];
80 int len;
81 uint8_t *p;
82
83 at24co2n_read_mac(sc);
84 p = sc->sc_mac_addr;
85 len = snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
86 p[0], p[1], p[2], p[3], p[4], p[5]);
87 return SYSCTL_OUT(req, buf, len);
88 }
89
90
91 static int
at24co2n_attach(device_t dev)92 at24co2n_attach(device_t dev)
93 {
94 struct at24co2n_softc *sc = device_get_softc(dev);
95 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
96 struct sysctl_oid *tree = device_get_sysctl_tree(dev);
97
98 if(sc == NULL) {
99 printf("at24co2n_attach device_get_softc failed\n");
100 return (0);
101 }
102 sc->sc_dev = dev;
103 sc->sc_addr = iicbus_get_addr(dev);
104
105 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
106 "eeprom-mac", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
107 at24co2n_mac_sysctl, "A", "mac address");
108
109 return (0);
110 }
111
112 static void
at24co2n_read_mac(struct at24co2n_softc * sc)113 at24co2n_read_mac(struct at24co2n_softc *sc)
114 {
115 uint8_t addr = AT24CO_EEPROM_ETH_MACADDR;
116 struct iic_msg msgs[2] = {
117 { sc->sc_addr, IIC_M_WR, 1, &addr },
118 { sc->sc_addr, IIC_M_RD, 6, sc->sc_mac_addr},
119 };
120
121 iicbus_transfer(sc->sc_dev, msgs, 2);
122 }
123
124 static device_method_t at24co2n_methods[] = {
125 DEVMETHOD(device_probe, at24co2n_probe),
126 DEVMETHOD(device_attach, at24co2n_attach),
127
128 {0, 0},
129 };
130
131 static driver_t at24co2n_driver = {
132 "at24co2n",
133 at24co2n_methods,
134 sizeof(struct at24co2n_softc),
135 };
136 static devclass_t at24co2n_devclass;
137
138 DRIVER_MODULE(at24co2n, iicbus, at24co2n_driver, at24co2n_devclass, 0, 0);
139 MODULE_VERSION(at24co2n, 1);
140 MODULE_DEPEND(at24co2n, iicbus, 1, 1, 1);
141