at24co2n.c revision 216390
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: head/sys/mips/rmi/dev/iic/at24co2n.c 216390 2010-12-12 06:00:26Z jchandra $");
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
60struct at24co2n_softc {
61	uint32_t	sc_addr;
62	device_t	sc_dev;
63	struct mtx	sc_mtx;
64	uint8_t		sc_mac_addr[6];
65};
66
67static void at24co2n_read_mac(struct at24co2n_softc *);
68
69static int
70at24co2n_probe(device_t dev)
71{
72	device_set_desc(dev, "AT24Co2N-10SE-2.7 EEPROM for mac address");
73	return (0);
74}
75
76static int
77at24co2n_mac_sysctl(SYSCTL_HANDLER_ARGS)
78{
79	struct at24co2n_softc *sc = arg1;
80	char buf[24];
81	int len;
82	uint8_t *p;
83
84	at24co2n_read_mac(sc);
85	p = sc->sc_mac_addr;
86	len = snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
87	    p[0], p[1], p[2], p[3], p[4], p[5]);
88	return SYSCTL_OUT(req, buf, len);
89}
90
91
92static int
93at24co2n_attach(device_t dev)
94{
95	struct at24co2n_softc *sc = device_get_softc(dev);
96	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
97	struct sysctl_oid *tree = device_get_sysctl_tree(dev);
98
99	if(sc == NULL) {
100		printf("at24co2n_attach device_get_softc failed\n");
101		return (0);
102	}
103	sc->sc_dev = dev;
104	sc->sc_addr = iicbus_get_addr(dev);
105
106	mtx_init(&sc->sc_mtx, "eeprom", "eeprom", MTX_DEF);
107
108	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
109		"eeprom-mac", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
110		at24co2n_mac_sysctl, "A", "mac address");
111
112	return (0);
113}
114
115static void
116at24co2n_read_mac(struct at24co2n_softc *sc)
117{
118	uint8_t addr = AT24CO_EEPROM_ETH_MACADDR;
119	struct iic_msg msgs[2] = {
120	     { sc->sc_addr, IIC_M_WR, 1, &addr },
121	     { sc->sc_addr, IIC_M_RD, 6, sc->sc_mac_addr},
122	};
123
124	mtx_lock(&sc->sc_mtx);
125	iicbus_transfer(sc->sc_dev, msgs, 2);
126	mtx_unlock(&sc->sc_mtx);
127}
128
129static device_method_t at24co2n_methods[] = {
130	DEVMETHOD(device_probe,		at24co2n_probe),
131	DEVMETHOD(device_attach,	at24co2n_attach),
132
133	{0, 0},
134};
135
136static driver_t at24co2n_driver = {
137	"at24co2n",
138	at24co2n_methods,
139	sizeof(struct at24co2n_softc),
140};
141static devclass_t at24co2n_devclass;
142
143DRIVER_MODULE(at24co2n, iicbus, at24co2n_driver, at24co2n_devclass, 0, 0);
144MODULE_VERSION(at24co2n, 1);
145MODULE_DEPEND(at24co2n, iicbus, 1, 1, 1);
146