summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/sfp.c
blob: f0ee6bc8bc4ee070139bb872034d3c10761418b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* $OpenBSD: sfp.c,v 1.1 2019/09/07 13:32:36 patrick Exp $ */
/*
 * Copyright (c) 2019 Patrick Wildt <patrick@blueri.se>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>

#include <net/if.h>

#include <machine/bus.h>
#include <machine/fdt.h>

#include <dev/i2c/i2cvar.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_gpio.h>
#include <dev/ofw/ofw_misc.h>

struct sfp_softc {
	struct device		 sc_dev;
	i2c_tag_t		 sc_tag;
	int			 sc_node;

	struct sfp_device	 sc_sd;
};

int	 sfp_match(struct device *, void *, void *);
void	 sfp_attach(struct device *, struct device *, void *);
int	 sfp_detach(struct device *, int);

int	 sfp_i2c_get_sffpage(void *, struct if_sffpage *);

struct cfattach sfp_ca = {
	sizeof(struct sfp_softc), sfp_match, sfp_attach, sfp_detach,
};

struct cfdriver sfp_cd = {
	NULL, "sfp", DV_DULL
};

int
sfp_match(struct device *parent, void *match, void *aux)
{
	struct fdt_attach_args *faa = aux;

	return (OF_is_compatible(faa->fa_node, "sff,sfp") ||
	    OF_is_compatible(faa->fa_node, "sff,sfp+"));
}

void
sfp_attach(struct device *parent, struct device *self, void *aux)
{
	struct sfp_softc *sc = (struct sfp_softc *)self;
	struct fdt_attach_args *faa = aux;

	sc->sc_node = faa->fa_node;
	sc->sc_tag = i2c_byphandle(OF_getpropint(sc->sc_node,
	    "i2c-bus", 0));

	if (sc->sc_tag == NULL) {
		printf(": can't get i2c bus\n");
		return;
	}

	printf("\n");

	sc->sc_sd.sd_node = faa->fa_node;
	sc->sc_sd.sd_cookie = sc;
	sc->sc_sd.sd_get_sffpage = sfp_i2c_get_sffpage;
	sfp_register(&sc->sc_sd);
}

int
sfp_detach(struct device *self, int flags)
{
	return 0;
}

int
sfp_i2c_get_sffpage(void *cookie, struct if_sffpage *sff)
{
	struct sfp_softc *sc = cookie;
	uint8_t reg = sff->sff_page;

	iic_acquire_bus(sc->sc_tag, 0);
	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
	    sff->sff_addr >> 1, &reg, sizeof(reg),
	    sff->sff_data, sizeof(sff->sff_data), 0)) {
		printf("%s: cannot read register 0x%x\n",
		    sc->sc_dev.dv_xname, reg);
	}
	iic_release_bus(sc->sc_tag, 0);

	return 0;
}