summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/amlclock.c
blob: 9bf7978c280815339febcbbd929b3ef1a2f4988f (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*	$OpenBSD: amlclock.c,v 1.11 2020/05/18 10:40:38 kettenis Exp $	*/
/*
 * Copyright (c) 2019 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 <machine/intr.h>
#include <machine/bus.h>
#include <machine/fdt.h>

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

/* Clock IDs */
#define G12A_SYS_PLL		0
#define G12A_FCLK_DIV2		2
#define G12A_FCLK_DIV3		3
#define G12A_FCLK_DIV4		4
#define G12A_FCLK_DIV5		5
#define G12A_FCLK_DIV7		6
#define G12A_MPLL1		12
#define G12A_MPLL2		13
#define G12A_I2C		24
#define G12A_SD_EMMC_A		33
#define G12A_SD_EMMC_B		34
#define G12A_SD_EMMC_C		35
#define G12A_SD_EMMC_A_CLK0	60
#define G12A_SD_EMMC_B_CLK0	61
#define G12A_SD_EMMC_C_CLK0	62
#define G12A_USB		47
#define G12A_FCLK_DIV2P5	99
#define G12A_CPU_CLK		187
#define G12A_PCIE_PLL		201
#define G12A_TS			212
#define G12B_SYS1_PLL		214
#define G12B_CPUB_CLK		224

/* Registers */
#define HHI_PCIE_PLL_CNTL0	0x26
#define HHI_PCIE_PLL_CNTL1	0x27
#define HHI_PCIE_PLL_CNTL2	0x28
#define HHI_PCIE_PLL_CNTL3	0x29
#define HHI_PCIE_PLL_CNTL4	0x2a
#define HHI_PCIE_PLL_CNTL5	0x2b
#define HHI_GCLK_MPEG0		0x50
#define HHI_GCLK_MPEG1		0x51
#define HHI_MPEG_CLK_CNTL	0x5d
#define HHI_TS_CLK_CNTL		0x64
#define HHI_SYS_CPU_CLK_CNTL0	0x67
#define  HHI_SYS_CPU_CLK_DYN_ENABLE		(1 << 26)
#define  HHI_SYS_CPU_CLK_MUX1_DIVN_TCNT(x)	(((x) >> 20) & 0x3f)
#define  HHI_SYS_CPU_CLK_MUX1_DIVN_TCNT_MASK	(0x3f << 20)
#define  HHI_SYS_CPU_CLK_MUX1_DIVN_TCNT_SHIFT	20
#define  HHI_SYS_CPU_CLK_POSTMUX1		(1 << 18)
#define  HHI_SYS_CPU_CLK_PREMUX1(x)		(((x) >> 16) & 0x3)
#define  HHI_SYS_CPU_CLK_PREMUX1_MASK		(0x3 << 16)
#define  HHI_SYS_CPU_CLK_PREMUX1_FCLK_DIV2	(0x1 << 16)
#define  HHI_SYS_CPU_CLK_PREMUX1_FCLK_DIV3	(0x2 << 16)
#define  HHI_SYS_CPU_CLK_FINAL_MUX_SEL		(1 << 11)
#define  HHI_SYS_CPU_CLK_FINAL_DYN_MUX_SEL	(1 << 10)
#define  HHI_SYS_CPU_CLK_MUX0_DIVN_TCNT(x)	(((x) >> 4) & 0x3f)
#define  HHI_SYS_CPU_CLK_MUX0_DIVN_TCNT_MASK	(0x3f << 4)
#define  HHI_SYS_CPU_CLK_MUX0_DIVN_TCNT_SHIFT	4
#define  HHI_SYS_CPU_CLK_POSTMUX0		(1 << 2)
#define  HHI_SYS_CPU_CLK_PREMUX0(x)		(((x) >> 0) & 0x3)
#define  HHI_SYS_CPU_CLK_PREMUX0_MASK		(0x3 << 0)
#define  HHI_SYS_CPU_CLK_PREMUX0_FCLK_DIV2	(0x1 << 0)
#define  HHI_SYS_CPU_CLK_PREMUX0_FCLK_DIV3	(0x2 << 0)
#define HHI_SYS_CPUB_CLK_CNTL	0x82
#define HHI_NAND_CLK_CNTL	0x97
#define HHI_SD_EMMC_CLK_CNTL	0x99
#define HHI_SYS_PLL_CNTL0	0xbd
#define  HHI_SYS_DPLL_LOCK	(1 << 31)
#define  HHI_SYS_DPLL_RESET	(1 << 29)
#define  HHI_SYS_DPLL_EN	(1 << 28)
#define  HHI_SYS_DPLL_OD(x)	(((x) >> 16) & 0x7)
#define  HHI_SYS_DPLL_OD_MASK	(0x7 << 16)
#define  HHI_SYS_DPLL_OD_SHIFT	16
#define  HHI_SYS_DPLL_N(x)	(((x) >> 10) & 0x1f)
#define  HHI_SYS_DPLL_N_MASK	(0x1f << 10)
#define  HHI_SYS_DPLL_N_SHIFT	10
#define  HHI_SYS_DPLL_M(x)	(((x) >> 0) & 0xff)
#define  HHI_SYS_DPLL_M_MASK	(0xff << 0)
#define  HHI_SYS_DPLL_M_SHIFT	0
#define HHI_SYS1_PLL_CNTL0	0xe0

#define HREAD4(sc, reg)							\
	(regmap_read_4((sc)->sc_rm, (reg) << 2))
#define HWRITE4(sc, reg, val)						\
	regmap_write_4((sc)->sc_rm, (reg) << 2, (val))
#define HSET4(sc, reg, bits)						\
	HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
#define HCLR4(sc, reg, bits)						\
	HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))

struct amlclock_gate {
	uint8_t reg;
	uint8_t bit;
};

struct amlclock_gate aml_g12a_gates[] = {
	[G12A_I2C] = { HHI_GCLK_MPEG0, 9 },
	[G12A_SD_EMMC_A] = { HHI_GCLK_MPEG0, 24 },
	[G12A_SD_EMMC_B] = { HHI_GCLK_MPEG0, 25 },
	[G12A_SD_EMMC_C] = { HHI_GCLK_MPEG0, 26 },
	[G12A_USB] = { HHI_GCLK_MPEG1, 26 },

	[G12A_SD_EMMC_A_CLK0] = { HHI_SD_EMMC_CLK_CNTL, 7 },
	[G12A_SD_EMMC_B_CLK0] = { HHI_SD_EMMC_CLK_CNTL, 23 },
	[G12A_SD_EMMC_C_CLK0] = { HHI_NAND_CLK_CNTL, 7 },

	[G12A_TS] = { HHI_TS_CLK_CNTL, 8 },
};

struct amlclock_softc {
	struct device		sc_dev;
	struct regmap		*sc_rm;
	int			sc_node;
	uint32_t		sc_g12b;

	struct amlclock_gate	*sc_gates;
	int			sc_ngates;

	struct clock_device	sc_cd;
	uint32_t		sc_xtal;
};

int amlclock_match(struct device *, void *, void *);
void amlclock_attach(struct device *, struct device *, void *);

struct cfattach	amlclock_ca = {
	sizeof (struct amlclock_softc), amlclock_match, amlclock_attach
};

struct cfdriver amlclock_cd = {
	NULL, "amlclock", DV_DULL
};

uint32_t amlclock_get_frequency(void *, uint32_t *);
int	amlclock_set_frequency(void *, uint32_t *, uint32_t);
void	amlclock_enable(void *, uint32_t *, int);

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

	return (OF_is_compatible(faa->fa_node, "amlogic,g12a-clkc") ||
	    OF_is_compatible(faa->fa_node, "amlogic,g12b-clkc") ||
	    OF_is_compatible(faa->fa_node, "amlogic,sm1-clkc"));
}

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

	sc->sc_rm = regmap_bynode(OF_parent(faa->fa_node));
	if (sc->sc_rm == NULL) {
		printf(": no registers\n");
		return;
	}

	sc->sc_node = faa->fa_node;
	printf("\n");

	if (OF_is_compatible(faa->fa_node, "amlogic,g12b-clkc"))
		sc->sc_g12b = 1;

	sc->sc_gates = aml_g12a_gates;
	sc->sc_ngates = nitems(aml_g12a_gates);

	sc->sc_xtal = clock_get_frequency(sc->sc_node, "xtal");

	sc->sc_cd.cd_node = faa->fa_node;
	sc->sc_cd.cd_cookie = sc;
	sc->sc_cd.cd_get_frequency = amlclock_get_frequency;
	sc->sc_cd.cd_set_frequency = amlclock_set_frequency;
	sc->sc_cd.cd_enable = amlclock_enable;
	clock_register(&sc->sc_cd);
}

uint32_t
amlclock_get_cpu_freq(struct amlclock_softc *sc, bus_size_t offset)
{
	uint32_t reg, mux, div;
	uint32_t idx;

	reg = HREAD4(sc, offset);
	if (reg & HHI_SYS_CPU_CLK_FINAL_MUX_SEL) {
		if (sc->sc_g12b && offset == HHI_SYS_CPU_CLK_CNTL0)
			idx = G12B_SYS1_PLL;
		else
			idx = G12A_SYS_PLL;
		return amlclock_get_frequency(sc, &idx);
	}
	if (reg & HHI_SYS_CPU_CLK_FINAL_DYN_MUX_SEL) {
		div = (reg & HHI_SYS_CPU_CLK_POSTMUX1) ?
		    (HHI_SYS_CPU_CLK_MUX1_DIVN_TCNT(reg) + 1) : 1;
		mux = HHI_SYS_CPU_CLK_PREMUX1(reg);
	} else {
		div = (reg & HHI_SYS_CPU_CLK_POSTMUX0) ?
		    (HHI_SYS_CPU_CLK_MUX0_DIVN_TCNT(reg) + 1) : 1;
		mux = HHI_SYS_CPU_CLK_PREMUX0(reg);
	}
	switch (mux) {
	case 0:
		return sc->sc_xtal / div;
	case 1:
		idx = G12A_FCLK_DIV2;
		break;
	case 2:
		idx = G12A_FCLK_DIV3;
		break;
	case 3:
		return 0;
	}
	return amlclock_get_frequency(sc, &idx) / div;
}

int
amlclock_set_cpu_freq(struct amlclock_softc *sc, bus_size_t offset,
    uint32_t freq)
{
	uint32_t reg, div;
	uint32_t parent_freq;
	uint32_t idx;

	/*
	 * For clock frequencies above 1GHz we have to use
	 * SYS_PLL/SYS1_PLL.
	 */
	reg = HREAD4(sc, offset);
	if (freq > 1000000000) {
		/* 
		 * Switch to a fixed clock if we're currently using
		 * SYS_PLL/SYS1_PLL.  Doesn't really matter which one.
		 */
		if (reg & HHI_SYS_CPU_CLK_FINAL_MUX_SEL) {
			reg &= ~HHI_SYS_CPU_CLK_FINAL_MUX_SEL;
			HWRITE4(sc, offset, reg);
			delay(100);
		}

		if (sc->sc_g12b && offset == HHI_SYS_CPU_CLK_CNTL0)
			idx = G12B_SYS1_PLL;
		else
			idx = G12A_SYS_PLL;
		amlclock_set_frequency(sc, &idx, freq);

		/* Switch to SYS_PLL/SYS1_PLL. */
		reg |= HHI_SYS_CPU_CLK_FINAL_MUX_SEL;
		HWRITE4(sc, offset, reg);
		delay(100);

		return 0;
	}

	/*
	 * There are two signal paths for frequencies up to 1GHz.  If
	 * we're using one, we can program the dividers for the other
	 * one and switch to it.  The pre-divider can be either 2 or 3
	 * and can't be bypassed, so take this into account and only
	 * allow frequencies that include such a divider.
	 */
	div = 2;
	parent_freq = 2000000000;
	while (parent_freq / div > freq)
		div++;
	while ((div % 2) != 0 && (div % 3) != 0)
		div++;
	if (div > 32)
		return EINVAL;
	if ((div % 2) == 0) {
		parent_freq /= 2;
		div /= 2;
	} else {	
		parent_freq /= 3;
		div /= 3;
	}

	if (reg & HHI_SYS_CPU_CLK_FINAL_DYN_MUX_SEL) {
		/* premux0 */
		reg = HREAD4(sc, offset);
		reg &= ~HHI_SYS_CPU_CLK_PREMUX0_MASK;
		if (parent_freq == 1000000000)
			reg |= HHI_SYS_CPU_CLK_PREMUX0_FCLK_DIV2;
		else
			reg |= HHI_SYS_CPU_CLK_PREMUX0_FCLK_DIV3;
		HWRITE4(sc, offset, reg);
		delay(100);

		/* mux0 divider */
		HSET4(sc, offset, HHI_SYS_CPU_CLK_DYN_ENABLE);
		reg = HREAD4(sc, offset);
		reg &= ~HHI_SYS_CPU_CLK_DYN_ENABLE;
		reg &= ~HHI_SYS_CPU_CLK_MUX0_DIVN_TCNT_MASK;
		reg |= ((div - 1) << HHI_SYS_CPU_CLK_MUX0_DIVN_TCNT_SHIFT);
		HWRITE4(sc, offset, reg);

		/* postmux0 */
		if (div != 1)
			HSET4(sc, offset, HHI_SYS_CPU_CLK_POSTMUX0);
		else
			HCLR4(sc, offset, HHI_SYS_CPU_CLK_POSTMUX0);

		/* final_dyn_mux_sel and final_mux_sel */
		reg = HREAD4(sc, offset);
		reg &= ~HHI_SYS_CPU_CLK_FINAL_DYN_MUX_SEL;
		reg &= ~HHI_SYS_CPU_CLK_FINAL_MUX_SEL;
		HWRITE4(sc, offset, reg);
		delay(100);
	} else {
		/* premux1 */
		reg = HREAD4(sc, offset);
		reg &= ~HHI_SYS_CPU_CLK_PREMUX1_MASK;
		if (parent_freq == 1000000000)
			reg |= HHI_SYS_CPU_CLK_PREMUX1_FCLK_DIV2;
		else
			reg |= HHI_SYS_CPU_CLK_PREMUX1_FCLK_DIV3;
		HWRITE4(sc, offset, reg);
		delay(100);

		/* mux1 divider */
		HSET4(sc, offset, HHI_SYS_CPU_CLK_DYN_ENABLE);
		reg = HREAD4(sc, offset);
		reg &= ~HHI_SYS_CPU_CLK_DYN_ENABLE;
		reg &= ~HHI_SYS_CPU_CLK_MUX1_DIVN_TCNT_MASK;
		reg |= ((div - 1) << HHI_SYS_CPU_CLK_MUX1_DIVN_TCNT_SHIFT);
		HWRITE4(sc, offset, reg);

		/* postmux1 */
		if (div != 1)
			HSET4(sc, offset, HHI_SYS_CPU_CLK_POSTMUX1);
		else
			HCLR4(sc, offset, HHI_SYS_CPU_CLK_POSTMUX1);

		/* final_dyn_mux_sel and final_mux_sel */
		reg = HREAD4(sc, offset);
		reg |= HHI_SYS_CPU_CLK_FINAL_DYN_MUX_SEL;
		reg &= ~HHI_SYS_CPU_CLK_FINAL_MUX_SEL;
		HWRITE4(sc, offset, reg);
		delay(100);
	}

	return 0;
}

int
amlclock_set_pll_freq(struct amlclock_softc *sc, bus_size_t offset,
    uint32_t freq)
{
	uint32_t reg, div;
	uint32_t m, n = 1;
	int timo;

	/*
	 * The multiplier should be between 128 and 255.  If
	 * necessary, adjust the divider to achieve this.
	 */
	div = 1;
	while ((div * (uint64_t)freq) / sc->sc_xtal < 128)
		div *= 2;
	if (div > 128)
		return EINVAL;
	m = (div * (uint64_t)freq) / sc->sc_xtal;
	if (m > 255)
		return EINVAL;

	HSET4(sc, offset, HHI_SYS_DPLL_RESET);
	HCLR4(sc, offset, HHI_SYS_DPLL_EN);

	reg = HREAD4(sc, offset);
	reg &= ~HHI_SYS_DPLL_OD_MASK;
	reg |= ((fls(div) - 1) << HHI_SYS_DPLL_OD_SHIFT);
	reg &= ~(HHI_SYS_DPLL_M_MASK | HHI_SYS_DPLL_N_MASK);
	reg |= (m << HHI_SYS_DPLL_M_SHIFT);
	reg |= (n << HHI_SYS_DPLL_N_SHIFT);
	HWRITE4(sc, offset, reg);

	HSET4(sc, offset, HHI_SYS_DPLL_RESET);
	HSET4(sc, offset, HHI_SYS_DPLL_EN);
	HCLR4(sc, offset, HHI_SYS_DPLL_RESET);

	for (timo = 24000000; timo > 0; timo--) {
		if (HREAD4(sc, offset) & HHI_SYS_DPLL_LOCK)
			return 0;
	}

	return ETIMEDOUT;
}

uint32_t
amlclock_get_frequency(void *cookie, uint32_t *cells)
{
	struct amlclock_softc *sc = cookie;
	uint32_t idx = cells[0];
	uint32_t reg, mux, div;
	uint32_t m, n;

	switch (idx) {
	case G12A_SYS_PLL:
		reg = HREAD4(sc, HHI_SYS_PLL_CNTL0);
		div = 1 << HHI_SYS_DPLL_OD(reg);
		m = HHI_SYS_DPLL_M(reg);
		n = HHI_SYS_DPLL_N(reg);
		return (((uint64_t)sc->sc_xtal * m) / n) / div;
	case G12B_SYS1_PLL:
		reg = HREAD4(sc, HHI_SYS1_PLL_CNTL0);
		div = 1 << HHI_SYS_DPLL_OD(reg);
		m = HHI_SYS_DPLL_M(reg);
		n = HHI_SYS_DPLL_N(reg);
		return (((uint64_t)sc->sc_xtal * m) / n) / div;
	case G12A_FCLK_DIV2:
		return 1000000000;
	case G12A_FCLK_DIV3:
		return 666666666;
	case G12A_FCLK_DIV4:
		return 500000000;
	case G12A_FCLK_DIV5:
		return 400000000;
	case G12A_FCLK_DIV7:
		return 285714285;
	case G12A_FCLK_DIV2P5:
		return 800000000;

	case G12A_I2C:
		reg = HREAD4(sc, HHI_MPEG_CLK_CNTL);
		mux = (reg >> 12) & 0x7;
		div = ((reg >> 0) & 0x7f) + 1;
		switch (mux) {
		case 0:
			return sc->sc_xtal / div;
		case 2:
			idx = G12A_FCLK_DIV7;
			break;
		case 3:
			idx = G12A_MPLL1;
			break;
		case 4:
			idx = G12A_MPLL2;
			break;
		case 5:
			idx = G12A_FCLK_DIV4;
			break;
		case 6:
			idx = G12A_FCLK_DIV3;
			break;
		case 7:
			idx = G12A_FCLK_DIV5;
			break;
		default:
			goto fail;
		}
		return amlclock_get_frequency(sc, &idx) / div;
	case G12A_SD_EMMC_A_CLK0:
		reg = HREAD4(sc, HHI_SD_EMMC_CLK_CNTL);
		mux = (reg >> 9) & 0x7;
		div = ((reg >> 0) & 0x7f) + 1;
		switch (mux) {
		case 0:
			return sc->sc_xtal / div;
		case 1:
			idx = G12A_FCLK_DIV2;
			break;
		case 2:
			idx = G12A_FCLK_DIV3;
			break;
		case 3:
			idx = G12A_FCLK_DIV5;
			break;
		case 4:
			idx = G12A_FCLK_DIV7;
			break;
		default:
			goto fail;
		}
		return amlclock_get_frequency(sc, &idx) / div;
	case G12A_SD_EMMC_B_CLK0:
		reg = HREAD4(sc, HHI_SD_EMMC_CLK_CNTL);
		mux = (reg >> 25) & 0x7;
		div = ((reg >> 16) & 0x7f) + 1;
		switch (mux) {
		case 0:
			return sc->sc_xtal / div;
		case 1:
			idx = G12A_FCLK_DIV2;
			break;
		case 2:
			idx = G12A_FCLK_DIV3;
			break;
		case 3:
			idx = G12A_FCLK_DIV5;
			break;
		case 4:
			idx = G12A_FCLK_DIV7;
			break;
		default:
			goto fail;
		}
		return amlclock_get_frequency(sc, &idx) / div;
	case G12A_SD_EMMC_C_CLK0:
		reg = HREAD4(sc, HHI_NAND_CLK_CNTL);
		mux = (reg >> 9) & 0x7;
		div = ((reg >> 0) & 0x7f) + 1;
		switch (mux) {
		case 0:
			return sc->sc_xtal / div;
		case 1:
			idx = G12A_FCLK_DIV2;
			break;
		case 2:
			idx = G12A_FCLK_DIV3;
			break;
		case 3:
			idx = G12A_FCLK_DIV5;
			break;
		case 4:
			idx = G12A_FCLK_DIV7;
			break;
		default:
			goto fail;
		}
		return amlclock_get_frequency(sc, &idx) / div;
	case G12A_CPU_CLK:
		return amlclock_get_cpu_freq(sc, HHI_SYS_CPU_CLK_CNTL0);
	case G12B_CPUB_CLK:
		return amlclock_get_cpu_freq(sc, HHI_SYS_CPUB_CLK_CNTL);
	}

fail:
	printf("%s: 0x%08x\n", __func__, idx);
	return 0;
}

int
amlclock_set_frequency(void *cookie, uint32_t *cells, uint32_t freq)
{
	struct amlclock_softc *sc = cookie;
	uint32_t idx = cells[0];

	switch (idx) {
	case G12A_SYS_PLL:
		return amlclock_set_pll_freq(sc, HHI_SYS_PLL_CNTL0, freq);
	case G12B_SYS1_PLL:
		return amlclock_set_pll_freq(sc, HHI_SYS1_PLL_CNTL0, freq);
	case G12A_PCIE_PLL:
		/* Fixed at 100 MHz. */
		if (freq != 100000000)
			return -1;
		HWRITE4(sc, HHI_PCIE_PLL_CNTL0, 0x20090496);
		HWRITE4(sc, HHI_PCIE_PLL_CNTL0, 0x30090496);
		HWRITE4(sc, HHI_PCIE_PLL_CNTL1, 0x00000000);
		HWRITE4(sc, HHI_PCIE_PLL_CNTL2, 0x00001100);
		HWRITE4(sc, HHI_PCIE_PLL_CNTL3, 0x10058e00);
		HWRITE4(sc, HHI_PCIE_PLL_CNTL4, 0x000100c0);
		HWRITE4(sc, HHI_PCIE_PLL_CNTL5, 0x68000048);
		HWRITE4(sc, HHI_PCIE_PLL_CNTL5, 0x68000068);
		delay(20);
		HWRITE4(sc, HHI_PCIE_PLL_CNTL4, 0x008100c0);
		delay(10);
		HWRITE4(sc, HHI_PCIE_PLL_CNTL0, 0x34090496);
		HWRITE4(sc, HHI_PCIE_PLL_CNTL0, 0x14090496);
		delay(10);
		HWRITE4(sc, HHI_PCIE_PLL_CNTL2, 0x00001000);
		return 0;
	case G12A_CPU_CLK:
		return amlclock_set_cpu_freq(sc, HHI_SYS_CPU_CLK_CNTL0, freq);
	case G12B_CPUB_CLK:
		return amlclock_set_cpu_freq(sc, HHI_SYS_CPUB_CLK_CNTL, freq);
	}

	printf("%s: 0x%08x\n", __func__, idx);
	return -1;
}

void
amlclock_enable(void *cookie, uint32_t *cells, int on)
{
	struct amlclock_softc *sc = cookie;
	uint32_t idx = cells[0];

	if (idx < sc->sc_ngates && sc->sc_gates[idx].reg != 0) {
		if (on)
			HSET4(sc, sc->sc_gates[idx].reg,
			    (1U << sc->sc_gates[idx].bit));
		else
			HCLR4(sc, sc->sc_gates[idx].reg,
			    (1U << sc->sc_gates[idx].bit));
		return;
	}

	switch (idx) {
	case G12A_FCLK_DIV2:
	case G12A_PCIE_PLL:
		/* Already enabled. */
		return;
	}

	printf("%s: 0x%08x\n", __func__, idx);
}