1216390Sjchandra/*-
2216390Sjchandra * Copyright (c) 2003-2009 RMI Corporation
3216390Sjchandra * All rights reserved.
4216390Sjchandra *
5216390Sjchandra * Redistribution and use in source and binary forms, with or without
6216390Sjchandra * modification, are permitted provided that the following conditions
7216390Sjchandra * are met:
8216390Sjchandra * 1. Redistributions of source code must retain the above copyright
9216390Sjchandra *    notice, this list of conditions and the following disclaimer.
10216390Sjchandra * 2. Redistributions in binary form must reproduce the above copyright
11216390Sjchandra *    notice, this list of conditions and the following disclaimer in the
12216390Sjchandra *    documentation and/or other materials provided with the distribution.
13216390Sjchandra * 3. Neither the name of RMI Corporation, nor the names of its contributors,
14216390Sjchandra *    may be used to endorse or promote products derived from this software
15216390Sjchandra *    without specific prior written permission.
16216390Sjchandra *
17216390Sjchandra * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18216390Sjchandra * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19216390Sjchandra * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20216390Sjchandra * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21216390Sjchandra * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22216390Sjchandra * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23216390Sjchandra * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24216390Sjchandra * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25216390Sjchandra * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26216390Sjchandra * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27216390Sjchandra * SUCH DAMAGE.
28216390Sjchandra *
29216390Sjchandra * RMI_BSD */
30216390Sjchandra
31216390Sjchandra#include <sys/cdefs.h>
32216390Sjchandra__FBSDID("$FreeBSD$");
33216390Sjchandra/*
34216390Sjchandra * reading eeprom for the mac address .
35216390Sjchandra */
36216390Sjchandra#include <sys/param.h>
37216390Sjchandra#include <sys/systm.h>
38216390Sjchandra#include <sys/kernel.h>
39216390Sjchandra#include <sys/lock.h>
40216390Sjchandra#include <sys/module.h>
41216390Sjchandra#include <sys/mutex.h>
42216390Sjchandra#include <sys/bus.h>
43216390Sjchandra#include <sys/resource.h>
44216390Sjchandra#include <sys/rman.h>
45216390Sjchandra#include <sys/sysctl.h>
46216390Sjchandra
47216390Sjchandra#include <machine/bus.h>
48216390Sjchandra#include <machine/cpu.h>
49216390Sjchandra#include <machine/cpufunc.h>
50216390Sjchandra#include <machine/frame.h>
51216390Sjchandra#include <machine/resource.h>
52216390Sjchandra
53216390Sjchandra#include <dev/iicbus/iiconf.h>
54216390Sjchandra#include <dev/iicbus/iicbus.h>
55216390Sjchandra
56216390Sjchandra#include "iicbus_if.h"
57216390Sjchandra
58216390Sjchandra#define	AT24CO_EEPROM_ETH_MACADDR	0x20
59216390Sjchandra
60216390Sjchandrastruct at24co2n_softc {
61216390Sjchandra	uint32_t	sc_addr;
62216390Sjchandra	device_t	sc_dev;
63216390Sjchandra	uint8_t		sc_mac_addr[6];
64216390Sjchandra};
65216390Sjchandra
66216390Sjchandrastatic void at24co2n_read_mac(struct at24co2n_softc *);
67216390Sjchandra
68216390Sjchandrastatic int
69216390Sjchandraat24co2n_probe(device_t dev)
70216390Sjchandra{
71216390Sjchandra	device_set_desc(dev, "AT24Co2N-10SE-2.7 EEPROM for mac address");
72216390Sjchandra	return (0);
73216390Sjchandra}
74216390Sjchandra
75216390Sjchandrastatic int
76216390Sjchandraat24co2n_mac_sysctl(SYSCTL_HANDLER_ARGS)
77216390Sjchandra{
78216390Sjchandra	struct at24co2n_softc *sc = arg1;
79216390Sjchandra	char buf[24];
80216390Sjchandra	int len;
81216390Sjchandra	uint8_t *p;
82216390Sjchandra
83216390Sjchandra	at24co2n_read_mac(sc);
84216390Sjchandra	p = sc->sc_mac_addr;
85216390Sjchandra	len = snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
86216390Sjchandra	    p[0], p[1], p[2], p[3], p[4], p[5]);
87216390Sjchandra	return SYSCTL_OUT(req, buf, len);
88216390Sjchandra}
89216390Sjchandra
90216390Sjchandra
91216390Sjchandrastatic int
92216390Sjchandraat24co2n_attach(device_t dev)
93216390Sjchandra{
94216390Sjchandra	struct at24co2n_softc *sc = device_get_softc(dev);
95216390Sjchandra	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
96216390Sjchandra	struct sysctl_oid *tree = device_get_sysctl_tree(dev);
97216390Sjchandra
98216390Sjchandra	if(sc == NULL) {
99216390Sjchandra		printf("at24co2n_attach device_get_softc failed\n");
100216390Sjchandra		return (0);
101216390Sjchandra	}
102216390Sjchandra	sc->sc_dev = dev;
103216390Sjchandra	sc->sc_addr = iicbus_get_addr(dev);
104216390Sjchandra
105216390Sjchandra	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
106216390Sjchandra		"eeprom-mac", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
107216390Sjchandra		at24co2n_mac_sysctl, "A", "mac address");
108216390Sjchandra
109216390Sjchandra	return (0);
110216390Sjchandra}
111216390Sjchandra
112216390Sjchandrastatic void
113216390Sjchandraat24co2n_read_mac(struct at24co2n_softc *sc)
114216390Sjchandra{
115216390Sjchandra	uint8_t addr = AT24CO_EEPROM_ETH_MACADDR;
116216390Sjchandra	struct iic_msg msgs[2] = {
117216390Sjchandra	     { sc->sc_addr, IIC_M_WR, 1, &addr },
118216390Sjchandra	     { sc->sc_addr, IIC_M_RD, 6, sc->sc_mac_addr},
119216390Sjchandra	};
120216390Sjchandra
121216390Sjchandra	iicbus_transfer(sc->sc_dev, msgs, 2);
122216390Sjchandra}
123216390Sjchandra
124216390Sjchandrastatic device_method_t at24co2n_methods[] = {
125216390Sjchandra	DEVMETHOD(device_probe,		at24co2n_probe),
126216390Sjchandra	DEVMETHOD(device_attach,	at24co2n_attach),
127216390Sjchandra
128216390Sjchandra	{0, 0},
129216390Sjchandra};
130216390Sjchandra
131216390Sjchandrastatic driver_t at24co2n_driver = {
132216390Sjchandra	"at24co2n",
133216390Sjchandra	at24co2n_methods,
134216390Sjchandra	sizeof(struct at24co2n_softc),
135216390Sjchandra};
136216390Sjchandrastatic devclass_t at24co2n_devclass;
137216390Sjchandra
138216390SjchandraDRIVER_MODULE(at24co2n, iicbus, at24co2n_driver, at24co2n_devclass, 0, 0);
139216390SjchandraMODULE_VERSION(at24co2n, 1);
140216390SjchandraMODULE_DEPEND(at24co2n, iicbus, 1, 1, 1);
141