summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/mvtemp.c
blob: d6112f71d031f70bf620effbc55256cf06f7ae49 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*	$OpenBSD: mvtemp.c,v 1.1 2018/03/31 16:35:15 kettenis Exp $	*/
/*
 * Copyright (c) 2018 Mark Kettenis <kettenis@openbsd.org>
 *
 * 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/device.h>
#include <sys/sensors.h>

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

#include <dev/ofw/openfirm.h>
#include <dev/ofw/fdt.h>

/* Registers */
#define TEMP_STAT			0x0000
#define TEMP_CTRL0			0x0000
#define  TEMP_CTRL0_TSEN_TC_TRIM_MASK	0x7
#define  TEMP_CTRL0_TSEN_TC_TRIM_VAL	0x3
#define TEMP_CTRL1			0x0004
#define  TEMP_CTRL1_TSEN_RESET		(1 << 8)

struct mvtemp_softc {
	struct device		sc_dev;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_stat_ioh;
	bus_space_handle_t	sc_ctrl_ioh;

	uint32_t		sc_stat_valid;
	int32_t			(*sc_calc_temp)(uint32_t);

	struct ksensor		sc_sensor;
	struct ksensordev	sc_sensordev;
};

int	mvtemp_match(struct device *, void *, void *);
void	mvtemp_attach(struct device *, struct device *, void *);

struct cfattach	mvtemp_ca = {
	sizeof (struct mvtemp_softc), mvtemp_match, mvtemp_attach
};

struct cfdriver mvtemp_cd = {
	NULL, "mvtemp", DV_DULL
};

struct mvtemp_compat {
	const char *compat;
	uint32_t stat_valid;
	void	(*init)(struct mvtemp_softc *);
	int32_t	(*calc_temp)(uint32_t);
};

void	mvtemp_ap806_init(struct mvtemp_softc *);
int32_t mvtemp_ap806_calc_temp(uint32_t);
void	mvtemp_cp110_init(struct mvtemp_softc *);
int32_t mvtemp_cp110_calc_temp(uint32_t);

struct mvtemp_compat mvtemp_compat[] = {
	{
		"marvell,armada-ap806-thermal", (1 << 16),
		mvtemp_ap806_init, mvtemp_ap806_calc_temp,
	},
	{
		"marvell,armada-cp110-thermal", (1 << 10),
		mvtemp_cp110_init, mvtemp_cp110_calc_temp,
	}
};

void	mvtemp_refresh_sensors(void *);

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

	for (i = 0; i < nitems(mvtemp_compat); i++) {
		if (OF_is_compatible(faa->fa_node, mvtemp_compat[i].compat))
			return 1;
	}

	return 0;
}

void
mvtemp_attach(struct device *parent, struct device *self, void *aux)
{
	struct mvtemp_softc *sc = (struct mvtemp_softc *)self;
	struct fdt_attach_args *faa = aux;
	int i;

	if (faa->fa_nreg < 2) {
		printf(": no registers\n");
		return;
	}

	sc->sc_iot = faa->fa_iot;
	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
	    faa->fa_reg[0].size, 0, &sc->sc_stat_ioh)) {
		printf(": can't map registers\n");
		return;
	}
	if (bus_space_map(sc->sc_iot, faa->fa_reg[1].addr,
	    faa->fa_reg[1].size, 0, &sc->sc_ctrl_ioh)) {
		bus_space_unmap(sc->sc_iot, sc->sc_stat_ioh,
		    faa->fa_reg[0].size);
		printf(": can't map registers\n");
		return;
	}

	printf("\n");

	for (i = 0; i < nitems(mvtemp_compat); i++) {
		if (OF_is_compatible(faa->fa_node, mvtemp_compat[i].compat)) {
			break;
		}
	}
	KASSERT(i < nitems(mvtemp_compat));

	mvtemp_compat[i].init(sc);
	sc->sc_stat_valid = mvtemp_compat[i].stat_valid;
	sc->sc_calc_temp = mvtemp_compat[i].calc_temp;

	/* Register sensors. */
	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
	    sizeof(sc->sc_sensordev.xname));
	sc->sc_sensor.type = SENSOR_TEMP;
	sc->sc_sensor.flags = SENSOR_FINVALID;
	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
	sensordev_install(&sc->sc_sensordev);
	sensor_task_register(sc, mvtemp_refresh_sensors, 5);
}

/* AP806 */

void
mvtemp_ap806_init(struct mvtemp_softc *sc)
{
}

int32_t
mvtemp_ap806_calc_temp(uint32_t stat)
{
	stat = ((stat & 0x3ff) ^ 0x200) - 0x200;
	return (stat * 423000) + 150000000 + 273150000;
}

/* CP110 */

void
mvtemp_cp110_init(struct mvtemp_softc *sc)
{
	uint32_t ctrl;

	ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ctrl_ioh, TEMP_CTRL1);
	ctrl |= TEMP_CTRL1_TSEN_RESET;
	bus_space_write_4(sc->sc_iot, sc->sc_ctrl_ioh, TEMP_CTRL1, ctrl);

	ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ctrl_ioh, TEMP_CTRL0);
	ctrl &= ~TEMP_CTRL0_TSEN_TC_TRIM_MASK;
	ctrl |= TEMP_CTRL0_TSEN_TC_TRIM_VAL;
	bus_space_write_4(sc->sc_iot, sc->sc_ctrl_ioh, TEMP_CTRL0, ctrl);
}

int32_t
mvtemp_cp110_calc_temp(uint32_t stat)
{
	return ((stat & 0x3ff) * 476100) - 279100000 + 273150000;
}

void
mvtemp_refresh_sensors(void *arg)
{
	struct mvtemp_softc *sc = arg;
	int32_t stat, temp;

	stat = bus_space_read_4(sc->sc_iot, sc->sc_stat_ioh, TEMP_STAT);
	temp = sc->sc_calc_temp(stat);
	sc->sc_sensor.value = temp;
	if ((stat & sc->sc_stat_valid) && temp >= 0)
		sc->sc_sensor.flags &= ~SENSOR_FINVALID;
	else
		sc->sc_sensor.flags |= SENSOR_FINVALID;
}