2 * Copyright (c) 2003-2009 RMI Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 * reading eeprom for the mac address .
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
43 #include <sys/resource.h>
45 #include <sys/sysctl.h>
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>
53 #include <dev/iicbus/iiconf.h>
54 #include <dev/iicbus/iicbus.h>
56 #include "iicbus_if.h"
58 #define AT24CO_EEPROM_ETH_MACADDR 0x20
60 struct at24co2n_softc {
63 uint8_t sc_mac_addr[6];
66 static void at24co2n_read_mac(struct at24co2n_softc *);
69 at24co2n_probe(device_t dev)
71 device_set_desc(dev, "AT24Co2N-10SE-2.7 EEPROM for mac address");
76 at24co2n_mac_sysctl(SYSCTL_HANDLER_ARGS)
78 struct at24co2n_softc *sc = arg1;
83 at24co2n_read_mac(sc);
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);
92 at24co2n_attach(device_t dev)
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);
99 printf("at24co2n_attach device_get_softc failed\n");
103 sc->sc_addr = iicbus_get_addr(dev);
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");
113 at24co2n_read_mac(struct at24co2n_softc *sc)
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},
121 iicbus_transfer(sc->sc_dev, msgs, 2);
124 static device_method_t at24co2n_methods[] = {
125 DEVMETHOD(device_probe, at24co2n_probe),
126 DEVMETHOD(device_attach, at24co2n_attach),
131 static driver_t at24co2n_driver = {
134 sizeof(struct at24co2n_softc),
136 static devclass_t at24co2n_devclass;
138 DRIVER_MODULE(at24co2n, iicbus, at24co2n_driver, at24co2n_devclass, 0, 0);
139 MODULE_VERSION(at24co2n, 1);
140 MODULE_DEPEND(at24co2n, iicbus, 1, 1, 1);