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