]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/at91/at91_pmc.c
Update tcpdump to 4.9.2
[FreeBSD/FreeBSD.git] / sys / arm / at91 / at91_pmc.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
5  * Copyright (c) 2010 Greg Ansley.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include "opt_platform.h"
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/time.h>
40 #include <sys/bus.h>
41 #include <sys/resource.h>
42 #include <sys/rman.h>
43 #include <sys/timetc.h>
44
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 #include <machine/intr.h>
48 #include <arm/at91/at91reg.h>
49 #include <arm/at91/at91var.h>
50
51 #include <arm/at91/at91_pmcreg.h>
52 #include <arm/at91/at91_pmcvar.h>
53
54 #ifdef FDT
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
57 #endif
58
59 static struct at91_pmc_softc {
60         bus_space_tag_t         sc_st;
61         bus_space_handle_t      sc_sh;
62         struct resource *mem_res;       /* Memory resource */
63         device_t                dev;
64 } *pmc_softc;
65
66 static uint32_t pllb_init;
67
68 MALLOC_DECLARE(M_PMC_CLK);
69 MALLOC_DEFINE(M_PMC_CLK, "at91_pmc_clocks", "AT91 PMC Clock descriptors");
70
71 #define AT91_PMC_BASE 0xffffc00
72
73 static void at91_pmc_set_pllb_mode(struct at91_pmc_clock *, int);
74 static void at91_pmc_set_upll_mode(struct at91_pmc_clock *, int);
75 static void at91_pmc_set_sys_mode(struct at91_pmc_clock *, int);
76 static void at91_pmc_set_periph_mode(struct at91_pmc_clock *, int);
77 static void at91_pmc_clock_alias(const char *name, const char *alias);
78
79 static struct at91_pmc_clock slck = {
80         .name = "slck",         /* 32,768 Hz slow clock */
81         .hz = 32768,
82         .refcnt = 1,
83         .id = 0,
84         .primary = 1,
85 };
86
87 /*
88  * NOTE: Clocks for "ordinary peripheral" devices e.g. spi0, udp0, uhp0 etc.
89  * are now created automatically. Only "system" clocks need be defined here.
90  */
91 static struct at91_pmc_clock main_ck = {
92         .name = "main",         /* Main clock */
93         .refcnt = 0,
94         .id = 1,
95         .primary = 1,
96         .pmc_mask = PMC_IER_MOSCS,
97 };
98
99 static struct at91_pmc_clock plla = {
100         .name = "plla",         /* PLLA Clock, used for CPU clocking */
101         .parent = &main_ck,
102         .refcnt = 1,
103         .id = 0,
104         .primary = 1,
105         .pll = 1,
106         .pmc_mask = PMC_IER_LOCKA,
107 };
108
109 static struct at91_pmc_clock pllb = {
110         .name = "pllb",         /* PLLB Clock, used for USB functions */
111         .parent = &main_ck,
112         .refcnt = 0,
113         .id = 0,
114         .primary = 1,
115         .pll = 1,
116         .pmc_mask = PMC_IER_LOCKB,
117         .set_mode = &at91_pmc_set_pllb_mode,
118 };
119
120 /* Used by USB on at91sam9g45 */
121 static struct at91_pmc_clock upll = {
122         .name = "upll",         /* UTMI PLL, used for USB functions on 9G45 family */
123         .parent = &main_ck,
124         .refcnt = 0,
125         .id = 0,
126         .primary = 1,
127         .pll = 1,
128         .pmc_mask = (1 << 6),
129         .set_mode = &at91_pmc_set_upll_mode,
130 };
131
132 static struct at91_pmc_clock udpck = {
133         .name = "udpck",
134         .parent = &pllb,
135         .pmc_mask = PMC_SCER_UDP,
136         .set_mode = at91_pmc_set_sys_mode
137 };
138
139 static struct at91_pmc_clock uhpck = {
140         .name = "uhpck",
141         .parent = &pllb,
142         .pmc_mask = PMC_SCER_UHP,
143         .set_mode = at91_pmc_set_sys_mode
144 };
145
146 static struct at91_pmc_clock mck = {
147         .name = "mck",          /* Master (Peripheral) Clock */
148         .pmc_mask = PMC_IER_MCKRDY,
149         .refcnt = 0,
150 };
151
152 static struct at91_pmc_clock cpu = {
153         .name = "cpu",          /* CPU Clock */
154         .parent = &plla,
155         .pmc_mask = PMC_SCER_PCK,
156         .refcnt = 0,
157 };
158
159 /* "+32" or the automatic peripheral clocks */
160 static struct at91_pmc_clock *clock_list[16+32] = {
161         &slck,
162         &main_ck,
163         &plla,
164         &pllb,
165         &upll,
166         &udpck,
167         &uhpck,
168         &mck,
169         &cpu
170 };
171
172 static inline uint32_t
173 RD4(struct at91_pmc_softc *sc, bus_size_t off)
174 {
175
176         if (sc == NULL) {
177                 uint32_t *p = (uint32_t *)(AT91_BASE + AT91_PMC_BASE + off);
178
179                 return *p;
180         }
181         return (bus_read_4(sc->mem_res, off));
182 }
183
184 static inline void
185 WR4(struct at91_pmc_softc *sc, bus_size_t off, uint32_t val)
186 {
187
188         if (sc == NULL) {
189                 uint32_t *p = (uint32_t *)(AT91_BASE + AT91_PMC_BASE + off);
190
191                 *p = val;
192         } else
193                 bus_write_4(sc->mem_res, off, val);
194 }
195
196 /*
197  * The following is unused currently since we don't ever set the PLLA
198  * frequency of the device.  If we did, we'd have to also pay attention
199  * to the ICPLLA bit in the PMC_PLLICPR register for frequencies lower
200  * than ~600MHz, which the PMC code doesn't do right now.
201  */
202 uint32_t
203 at91_pmc_800mhz_plla_outb(int freq)
204 {
205         uint32_t outa;
206
207         /*
208          * Set OUTA, per the data sheet.  See Table 46-16 titled
209          * PLLA Frequency Regarding ICPLLA and OUTA in the SAM9X25 doc,
210          * Table 46-17 in the SAM9G20 doc, or Table 46-16 in the SAM9G45 doc.
211          * Note: the frequencies overlap by 5MHz, so we add 3 here to
212          * center shoot the transition.
213          */
214
215         freq /= 1000000;                /* MHz */
216         if (freq >= 800)
217                 freq = 800;
218         freq += 3;                      /* Allow for overlap. */
219         outa = 3 - ((freq / 50) & 3);   /* 750 / 50 = 7, see table */
220         return (1 << 29)| (outa << 14);
221 }
222
223 uint32_t
224 at91_pmc_800mhz_pllb_outb(int freq)
225 {
226
227         return (0);
228 }
229
230 void
231 at91_pmc_set_pllb_mode(struct at91_pmc_clock *clk, int on)
232 {
233         struct at91_pmc_softc *sc = pmc_softc;
234         uint32_t value;
235
236         value = on ? pllb_init : 0;
237
238         /*
239          * Only write to the register if the value is changing.  Besides being
240          * good common sense, this works around RM9200 Errata #26 (CKGR_PLL[AB]R
241          * must not be written with the same value currently in the register).
242          */
243         if (RD4(sc, CKGR_PLLBR) != value) {
244                 WR4(sc, CKGR_PLLBR, value);
245                 while (on && (RD4(sc, PMC_SR) & PMC_IER_LOCKB) != PMC_IER_LOCKB)
246                         continue;
247         }
248 }
249
250 static void
251 at91_pmc_set_upll_mode(struct at91_pmc_clock *clk, int on)
252 {
253         struct at91_pmc_softc *sc = pmc_softc;
254         uint32_t value;
255
256         if (on) {
257                 on = PMC_IER_LOCKU;
258                 value = CKGR_UCKR_UPLLEN | CKGR_UCKR_BIASEN;
259         } else
260                 value = 0;
261
262         WR4(sc, CKGR_UCKR, RD4(sc, CKGR_UCKR) | value);
263         while ((RD4(sc, PMC_SR) & PMC_IER_LOCKU) != on)
264                 continue;
265
266         WR4(sc, PMC_USB, PMC_USB_USBDIV(9) | PMC_USB_USBS);
267         WR4(sc, PMC_SCER, PMC_SCER_UHP_SAM9);
268 }
269
270 static void
271 at91_pmc_set_sys_mode(struct at91_pmc_clock *clk, int on)
272 {
273         struct at91_pmc_softc *sc = pmc_softc;
274
275         WR4(sc, on ? PMC_SCER : PMC_SCDR, clk->pmc_mask);
276         if (on)
277                 while ((RD4(sc, PMC_SCSR) & clk->pmc_mask) != clk->pmc_mask)
278                         continue;
279         else
280                 while ((RD4(sc, PMC_SCSR) & clk->pmc_mask) == clk->pmc_mask)
281                         continue;
282 }
283
284 static void
285 at91_pmc_set_periph_mode(struct at91_pmc_clock *clk, int on)
286 {
287         struct at91_pmc_softc *sc = pmc_softc;
288
289         WR4(sc, on ? PMC_PCER : PMC_PCDR, clk->pmc_mask);
290         if (on)
291                 while ((RD4(sc, PMC_PCSR) & clk->pmc_mask) != clk->pmc_mask)
292                         continue;
293         else
294                 while ((RD4(sc, PMC_PCSR) & clk->pmc_mask) == clk->pmc_mask)
295                         continue;
296 }
297
298 struct at91_pmc_clock *
299 at91_pmc_clock_add(const char *name, uint32_t irq,
300     struct at91_pmc_clock *parent)
301 {
302         struct at91_pmc_clock *clk;
303         int i, buflen;
304
305         clk = malloc(sizeof(*clk), M_PMC_CLK, M_NOWAIT | M_ZERO);
306         if (clk == NULL)
307                 goto err;
308
309         buflen = strlen(name) + 1;
310         clk->name = malloc(buflen, M_PMC_CLK, M_NOWAIT);
311         if (clk->name == NULL)
312                 goto err;
313
314         strlcpy(clk->name, name, buflen);
315         clk->pmc_mask = 1 << irq;
316         clk->set_mode = &at91_pmc_set_periph_mode;
317         if (parent == NULL)
318                 clk->parent = &mck;
319         else
320                 clk->parent = parent;
321
322         for (i = 0; i < nitems(clock_list); i++) {
323                 if (clock_list[i] == NULL) {
324                         clock_list[i] = clk;
325                         return (clk);
326                 }
327         }
328 err:
329         if (clk != NULL) {
330                 if (clk->name != NULL)
331                         free(clk->name, M_PMC_CLK);
332                 free(clk, M_PMC_CLK);
333         }
334
335         panic("could not allocate pmc clock '%s'", name);
336         return (NULL);
337 }
338
339 static void
340 at91_pmc_clock_alias(const char *name, const char *alias)
341 {
342         struct at91_pmc_clock *clk, *alias_clk;
343
344         clk = at91_pmc_clock_ref(name);
345         if (clk)
346                 alias_clk = at91_pmc_clock_add(alias, 0, clk->parent);
347
348         if (clk && alias_clk) {
349                 alias_clk->hz = clk->hz;
350                 alias_clk->pmc_mask = clk->pmc_mask;
351                 alias_clk->set_mode = clk->set_mode;
352         }
353 }
354
355 struct at91_pmc_clock *
356 at91_pmc_clock_ref(const char *name)
357 {
358         int i;
359
360         for (i = 0; i < nitems(clock_list); i++) {
361                 if (clock_list[i] == NULL)
362                     break;
363                 if (strcmp(name, clock_list[i]->name) == 0)
364                         return (clock_list[i]);
365         }
366
367         return (NULL);
368 }
369
370 void
371 at91_pmc_clock_deref(struct at91_pmc_clock *clk)
372 {
373         if (clk == NULL)
374                 return;
375 }
376
377 void
378 at91_pmc_clock_enable(struct at91_pmc_clock *clk)
379 {
380         if (clk == NULL)
381                 return;
382
383         /* XXX LOCKING? XXX */
384         if (clk->parent)
385                 at91_pmc_clock_enable(clk->parent);
386         if (clk->refcnt++ == 0 && clk->set_mode)
387                 clk->set_mode(clk, 1);
388 }
389
390 void
391 at91_pmc_clock_disable(struct at91_pmc_clock *clk)
392 {
393         if (clk == NULL)
394                 return;
395
396         /* XXX LOCKING? XXX */
397         if (--clk->refcnt == 0 && clk->set_mode)
398                 clk->set_mode(clk, 0);
399         if (clk->parent)
400                 at91_pmc_clock_disable(clk->parent);
401 }
402
403 static int
404 at91_pmc_pll_rate(struct at91_pmc_clock *clk, uint32_t reg)
405 {
406         uint32_t mul, div, freq;
407
408         freq = clk->parent->hz;
409         div = (reg >> clk->pll_div_shift) & clk->pll_div_mask;
410         mul = (reg >> clk->pll_mul_shift) & clk->pll_mul_mask;
411
412 #if 0
413         printf("pll = (%d /  %d) * %d = %d\n",
414             freq, div, mul + 1, (freq/div) * (mul+1));
415 #endif
416
417         if (div != 0 && mul != 0) {
418                 freq /= div;
419                 freq *= mul + 1;
420         } else
421                 freq = 0;
422         clk->hz = freq;
423
424         return (freq);
425 }
426
427 static uint32_t
428 at91_pmc_pll_calc(struct at91_pmc_clock *clk, uint32_t out_freq)
429 {
430         uint32_t i, div = 0, mul = 0, diff = 1 << 30;
431
432         unsigned ret = 0x3e00;
433
434         if (out_freq > clk->pll_max_out)
435                 goto fail;
436
437         for (i = 1; i < 256; i++) {
438                 int32_t diff1;
439                 uint32_t input, mul1;
440
441                 input = clk->parent->hz / i;
442                 if (input < clk->pll_min_in)
443                         break;
444                 if (input > clk->pll_max_in)
445                         continue;
446
447                 mul1 = out_freq / input;
448                 if (mul1 > (clk->pll_mul_mask + 1))
449                         continue;
450                 if (mul1 == 0)
451                         break;
452
453                 diff1 = out_freq - input * mul1;
454                 if (diff1 < 0)
455                         diff1 = -diff1;
456                 if (diff > diff1) {
457                         diff = diff1;
458                         div = i;
459                         mul = mul1;
460                         if (diff == 0)
461                                 break;
462                 }
463         }
464         if (diff > (out_freq >> PMC_PLL_SHIFT_TOL))
465                 goto fail;
466
467         if (clk->set_outb != NULL)
468                 ret |= clk->set_outb(out_freq);
469
470         return (ret |
471                 ((mul - 1) << clk->pll_mul_shift) |
472                 (div << clk->pll_div_shift));
473 fail:
474         return (0);
475 }
476
477 #if !defined(AT91C_MAIN_CLOCK)
478 static const unsigned int at91_main_clock_tbl[] = {
479         3000000, 3276800, 3686400, 3840000, 4000000,
480         4433619, 4915200, 5000000, 5242880, 6000000,
481         6144000, 6400000, 6553600, 7159090, 7372800,
482         7864320, 8000000, 9830400, 10000000, 11059200,
483         12000000, 12288000, 13560000, 14318180, 14745600,
484         16000000, 17344700, 18432000, 20000000
485 };
486 #define MAIN_CLOCK_TBL_LEN      nitems(at91_main_clock_tbl)
487 #endif
488
489 static unsigned int
490 at91_pmc_sense_main_clock(void)
491 {
492 #if !defined(AT91C_MAIN_CLOCK)
493         unsigned int ckgr_val;
494         unsigned int diff, matchdiff, freq;
495         int i;
496
497         ckgr_val = (RD4(NULL, CKGR_MCFR) & CKGR_MCFR_MAINF_MASK) << 11;
498
499         /*
500          * Clocks up to 50MHz can be connected to some models.  If
501          * the frequency is >= 21MHz, assume that the slow clock can
502          * measure it correctly, and that any error can be adequately
503          * compensated for by roudning to the nearest 500Hz.  Users
504          * with fast, or odd-ball clocks will need to set
505          * AT91C_MAIN_CLOCK in the kernel config file.
506          */
507         if (ckgr_val >= 21000000)
508                 return (rounddown(ckgr_val + 250, 500));
509
510         /*
511          * Try to find the standard frequency that match best.
512          */
513         freq = at91_main_clock_tbl[0];
514         matchdiff = abs(ckgr_val - at91_main_clock_tbl[0]);
515         for (i = 1; i < MAIN_CLOCK_TBL_LEN; i++) {
516                 diff = abs(ckgr_val - at91_main_clock_tbl[i]);
517                 if (diff < matchdiff) {
518                         freq = at91_main_clock_tbl[i];
519                         matchdiff = diff;
520                 }
521         }
522         return (freq);
523 #else
524         return (AT91C_MAIN_CLOCK);
525 #endif
526 }
527
528 void
529 at91_pmc_init_clock(void)
530 {
531         struct at91_pmc_softc *sc = NULL;
532         unsigned int main_clock;
533         uint32_t mckr;
534         uint32_t mdiv;
535
536         soc_info.soc_data->soc_clock_init();
537
538         main_clock = at91_pmc_sense_main_clock();
539
540         if (at91_is_sam9() || at91_is_sam9xe()) {
541                 uhpck.pmc_mask = PMC_SCER_UHP_SAM9;
542                 udpck.pmc_mask = PMC_SCER_UDP_SAM9;
543         }
544
545         /* There is no pllb on AT91SAM9G45 */
546         if (at91_cpu_is(AT91_T_SAM9G45)) {
547                 uhpck.parent = &upll;
548                 uhpck.pmc_mask = PMC_SCER_UHP_SAM9;
549         }
550
551         mckr = RD4(sc, PMC_MCKR);
552         main_ck.hz = main_clock;
553
554         /*
555          * Note: this means outa calc code for plla never used since
556          * we never change it.  If we did, we'd also have to mind
557          * ICPLLA to get the charge pump current right.
558          */
559         at91_pmc_pll_rate(&plla, RD4(sc, CKGR_PLLAR));
560
561         if (at91_cpu_is(AT91_T_SAM9G45) && (mckr & PMC_MCKR_PLLADIV2))
562                 plla.hz /= 2;
563
564         /*
565          * Initialize the usb clock.  This sets up pllb, but disables the
566          * actual clock. XXX except for the if 0 :(
567          */
568         if (!at91_cpu_is(AT91_T_SAM9G45)) {
569                 pllb_init = at91_pmc_pll_calc(&pllb, 48000000 * 2) | 0x10000000;
570                 at91_pmc_pll_rate(&pllb, pllb_init);
571 #if 0
572                 /* Turn off USB clocks */
573                 at91_pmc_set_periph_mode(&ohci_clk, 0);
574                 at91_pmc_set_periph_mode(&udc_clk, 0);
575 #endif
576         }
577
578         if (at91_is_rm92()) {
579                 WR4(sc, PMC_SCDR, PMC_SCER_UHP | PMC_SCER_UDP);
580                 WR4(sc, PMC_SCER, PMC_SCER_MCKUDP);
581         } else
582                 WR4(sc, PMC_SCDR, PMC_SCER_UHP_SAM9 | PMC_SCER_UDP_SAM9);
583
584         /*
585          * MCK and PCU derive from one of the primary clocks.  Initialize
586          * this relationship.
587          */
588         mck.parent = clock_list[mckr & 0x3];
589         mck.parent->refcnt++;
590
591         cpu.hz = mck.hz = mck.parent->hz /
592             (1 << ((mckr & PMC_MCKR_PRES_MASK) >> 2));
593
594         mdiv = (mckr & PMC_MCKR_MDIV_MASK) >> 8;
595         if (at91_is_sam9() || at91_is_sam9xe()) {
596                 /*
597                  * On AT91SAM9G45 when mdiv == 3 we need to divide
598                  * MCK by 3 but not, for example, on 9g20.
599                  */
600                 if (!at91_cpu_is(AT91_T_SAM9G45) || mdiv <= 2)
601                         mdiv *= 2;
602                 if (mdiv > 0)
603                         mck.hz /= mdiv;
604         } else
605                 mck.hz /= (1 + mdiv);
606
607         /* Only found on SAM9G20 */
608         if (at91_cpu_is(AT91_T_SAM9G20))
609                 cpu.hz /= (mckr & PMC_MCKR_PDIV) ?  2 : 1;
610
611         at91_master_clock = mck.hz;
612
613         /* These clocks refrenced by "special" names */
614         at91_pmc_clock_alias("ohci0", "ohci_clk");
615         at91_pmc_clock_alias("udp0",  "udp_clk");
616
617         /* Turn off "Progamable" clocks */
618         WR4(sc, PMC_SCDR, PMC_SCER_PCK0 | PMC_SCER_PCK1 | PMC_SCER_PCK2 |
619             PMC_SCER_PCK3);
620
621         /* XXX kludge, turn on all peripherals */
622         WR4(sc, PMC_PCER, 0xffffffff);
623
624         /* Disable all interrupts for PMC */
625         WR4(sc, PMC_IDR, 0xffffffff);
626 }
627
628 static void
629 at91_pmc_deactivate(device_t dev)
630 {
631         struct at91_pmc_softc *sc;
632
633         sc = device_get_softc(dev);
634         bus_generic_detach(sc->dev);
635         if (sc->mem_res)
636                 bus_release_resource(dev, SYS_RES_IOPORT,
637                     rman_get_rid(sc->mem_res), sc->mem_res);
638         sc->mem_res = NULL;
639 }
640
641 static int
642 at91_pmc_activate(device_t dev)
643 {
644         struct at91_pmc_softc *sc;
645         int rid;
646
647         sc = device_get_softc(dev);
648         rid = 0;
649         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
650             RF_ACTIVE);
651         if (sc->mem_res == NULL)
652                 goto errout;
653         return (0);
654 errout:
655         at91_pmc_deactivate(dev);
656         return (ENOMEM);
657 }
658
659 static int
660 at91_pmc_probe(device_t dev)
661 {
662 #ifdef FDT
663         if (!ofw_bus_is_compatible(dev, "atmel,at91rm9200-pmc") &&
664                 !ofw_bus_is_compatible(dev, "atmel,at91sam9260-pmc") &&
665                 !ofw_bus_is_compatible(dev, "atmel,at91sam9g45-pmc") &&
666                 !ofw_bus_is_compatible(dev, "atmel,at91sam9x5-pmc"))
667                 return (ENXIO);
668 #endif
669         device_set_desc(dev, "PMC");
670         return (0);
671 }
672
673 static int
674 at91_pmc_attach(device_t dev)
675 {
676         int err;
677
678         pmc_softc = device_get_softc(dev);
679         pmc_softc->dev = dev;
680         if ((err = at91_pmc_activate(dev)) != 0)
681                 return (err);
682
683         /*
684          * Configure main clock frequency.
685          */
686         at91_pmc_init_clock();
687
688         /*
689          * Display info about clocks previously computed
690          */
691         device_printf(dev,
692             "Primary: %d Hz PLLA: %d MHz CPU: %d MHz MCK: %d MHz\n",
693             main_ck.hz,
694             plla.hz / 1000000,
695             cpu.hz / 1000000, mck.hz / 1000000);
696
697         return (0);
698 }
699
700 static device_method_t at91_pmc_methods[] = {
701         DEVMETHOD(device_probe, at91_pmc_probe),
702         DEVMETHOD(device_attach, at91_pmc_attach),
703         DEVMETHOD_END
704 };
705
706 static driver_t at91_pmc_driver = {
707         "at91_pmc",
708         at91_pmc_methods,
709         sizeof(struct at91_pmc_softc),
710 };
711 static devclass_t at91_pmc_devclass;
712
713 #ifdef FDT
714 EARLY_DRIVER_MODULE(at91_pmc, simplebus, at91_pmc_driver, at91_pmc_devclass,
715     NULL, NULL, BUS_PASS_CPU);
716 #else
717 EARLY_DRIVER_MODULE(at91_pmc, atmelarm, at91_pmc_driver, at91_pmc_devclass,
718     NULL, NULL, BUS_PASS_CPU);
719 #endif