summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/bcm2835_clock.c
blob: 6e0d877c1f4cdddf29d5868c9987aa61abf30c4f (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
200
201
202
/*	$OpenBSD: bcm2835_clock.c,v 1.3 2022/04/06 18:59:28 naddy Exp $	*/

/*
 * Copyright (c) 2020 Tobias Heider <tobhe@openbsd.org>
 * Copyright (c) 2019 Neil Ashford <ashfordneil0@gmail.com>
 *
 * 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.
 */

/*-
 * Copyright (c) 2017 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

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

#include <machine/fdt.h>

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

#include <dev/ic/bcm2835_mbox.h>
#include <dev/ic/bcm2835_vcprop.h>

enum {
	BCMCLOCK_CLOCK_TIMER = 17,
	BCMCLOCK_CLOCK_UART = 19,
	BCMCLOCK_CLOCK_VPU = 20,
	BCMCLOCK_CLOCK_V3D = 21,
	BCMCLOCK_CLOCK_ISP = 22,
	BCMCLOCK_CLOCK_H264 = 23,
	BCMCLOCK_CLOCK_VEC = 24,
	BCMCLOCK_CLOCK_HSM = 25,
	BCMCLOCK_CLOCK_SDRAM = 26,
	BCMCLOCK_CLOCK_TSENS = 27,
	BCMCLOCK_CLOCK_EMMC = 28,
	BCMCLOCK_CLOCK_PERIIMAGE = 29,
	BCMCLOCK_CLOCK_PWM = 30,
	BCMCLOCK_CLOCK_PCM = 31,
	BCMCLOCK_NCLOCK
};


struct bcmclock_softc {
	struct device		sc_dev;
	struct clock_device	sc_cd;

};

int bcmclock_match(struct device *, void *, void *);
void bcmclock_attach(struct device *, struct device *, void *);

const struct cfattach bcmclock_ca = {
	sizeof(struct bcmclock_softc),
	bcmclock_match,
	bcmclock_attach,
};

uint32_t bcmclock_get_frequency(void *, uint32_t *);

struct cfdriver bcmclock_cd = { NULL, "bcmclock", DV_DULL };

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

	return (OF_is_compatible(faa->fa_node, "brcm,bcm2711-cprman") ||
	    OF_is_compatible(faa->fa_node, "brcm,bcm2835-cprman"));
}

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

	printf("\n");

	sc->sc_cd.cd_node = faa->fa_node;
	sc->sc_cd.cd_cookie = sc;
	sc->sc_cd.cd_get_frequency = bcmclock_get_frequency;

	clock_register(&sc->sc_cd);
}

uint32_t
bcmclock_get_frequency(void *cookie, uint32_t *cells)
{
	struct request {
		struct vcprop_buffer_hdr vb_hdr;
		struct vcprop_tag_clockrate vbt_clkrate;
		struct vcprop_tag end;
	} __attribute((aligned(16), packed));

	uint32_t result;
	struct request req = {
		.vb_hdr = {
			.vpb_len = sizeof(req),
			.vpb_rcode = VCPROP_PROCESS_REQUEST,
		},
		.vbt_clkrate = {
			.tag = {
				.vpt_tag = VCPROPTAG_GET_CLOCKRATE,
				.vpt_len = VCPROPTAG_LEN(req.vbt_clkrate),
				.vpt_rcode = VCPROPTAG_REQUEST
			},
		},
		.end = {
			.vpt_tag = VCPROPTAG_NULL
		}
	};

	switch (cells[0]) {
	case BCMCLOCK_CLOCK_TIMER:
		break;
	case BCMCLOCK_CLOCK_UART:
		req.vbt_clkrate.id = VCPROP_CLK_UART;
		break;
	case BCMCLOCK_CLOCK_VPU:
		req.vbt_clkrate.id = VCPROP_CLK_CORE;
		break;
	case BCMCLOCK_CLOCK_V3D:
		req.vbt_clkrate.id = VCPROP_CLK_V3D;
		break;
	case BCMCLOCK_CLOCK_ISP:
		req.vbt_clkrate.id = VCPROP_CLK_ISP;
		break;
	case BCMCLOCK_CLOCK_H264:
		req.vbt_clkrate.id = VCPROP_CLK_H264;
		break;
	case BCMCLOCK_CLOCK_VEC:
		break;
	case BCMCLOCK_CLOCK_HSM:
		break;
	case BCMCLOCK_CLOCK_SDRAM:
		req.vbt_clkrate.id = VCPROP_CLK_SDRAM;
		break;
	case BCMCLOCK_CLOCK_TSENS:
		break;
	case BCMCLOCK_CLOCK_EMMC:
		req.vbt_clkrate.id = VCPROP_CLK_EMMC;
		break;
	case BCMCLOCK_CLOCK_PERIIMAGE:
		break;
	case BCMCLOCK_CLOCK_PWM:
		req.vbt_clkrate.id = VCPROP_CLK_PWM;
		break;
	case BCMCLOCK_CLOCK_PCM:
		break;
	}

	if (req.vbt_clkrate.id == 0) {
		printf("bcmclock[unknown]: request to unknown clock type %d\n",
		       cells[0]);
		return 0;
	}

	bcmmbox_post(BCMMBOX_CHANARM2VC, &req, sizeof(req), &result);

	if (vcprop_tag_success_p(&req.vbt_clkrate.tag))
		return req.vbt_clkrate.rate;

	printf("bcmclock[unknown]: vcprop result %x:%x\n", req.vb_hdr.vpb_rcode,
	       req.vbt_clkrate.tag.vpt_rcode);

	return 0;
}