2006-03-24 07:37:56 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2006 M. Warner Losh. All rights reserved.
|
2010-10-06 22:25:21 +00:00
|
|
|
* Copyright (c) 2010 Greg Ansley. All rights reserved.
|
2006-03-24 07:37:56 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2008-11-25 00:13:26 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``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 AUTHOR OR CONTRIBUTORS 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.
|
2006-03-24 07:37:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
2010-10-06 22:25:21 +00:00
|
|
|
#include <sys/malloc.h>
|
2006-03-24 07:37:56 +00:00
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/rman.h>
|
|
|
|
#include <sys/timetc.h>
|
|
|
|
|
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
#include <machine/cpufunc.h>
|
|
|
|
#include <machine/resource.h>
|
|
|
|
#include <machine/frame.h>
|
|
|
|
#include <machine/intr.h>
|
2010-10-06 22:25:21 +00:00
|
|
|
#include <arm/at91/at91reg.h>
|
|
|
|
#include <arm/at91/at91var.h>
|
2006-03-24 07:37:56 +00:00
|
|
|
|
|
|
|
#include <arm/at91/at91_pmcreg.h>
|
|
|
|
#include <arm/at91/at91_pmcvar.h>
|
|
|
|
|
|
|
|
static struct at91_pmc_softc {
|
|
|
|
bus_space_tag_t sc_st;
|
|
|
|
bus_space_handle_t sc_sh;
|
|
|
|
struct resource *mem_res; /* Memory resource */
|
|
|
|
device_t dev;
|
|
|
|
} *pmc_softc;
|
|
|
|
|
2012-05-29 03:23:18 +00:00
|
|
|
static uint32_t pllb_init;
|
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
MALLOC_DECLARE(M_PMC);
|
|
|
|
MALLOC_DEFINE(M_PMC, "at91_pmc_clocks", "AT91 PMC Clock descriptors");
|
|
|
|
|
2012-05-29 03:23:18 +00:00
|
|
|
#define AT91_PMC_BASE 0xffffc00
|
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
static void at91_pmc_set_pllb_mode(struct at91_pmc_clock *, int);
|
2012-07-26 08:01:25 +00:00
|
|
|
static void at91_pmc_set_upll_mode(struct at91_pmc_clock *, int);
|
2006-03-24 07:37:56 +00:00
|
|
|
static void at91_pmc_set_sys_mode(struct at91_pmc_clock *, int);
|
|
|
|
static void at91_pmc_set_periph_mode(struct at91_pmc_clock *, int);
|
2010-10-06 22:25:21 +00:00
|
|
|
static void at91_pmc_clock_alias(const char *name, const char *alias);
|
2006-03-24 07:37:56 +00:00
|
|
|
|
|
|
|
static struct at91_pmc_clock slck = {
|
2012-08-11 05:03:30 +00:00
|
|
|
.name = "slck", /* 32,768 Hz slow clock */
|
2006-03-24 07:37:56 +00:00
|
|
|
.hz = 32768,
|
|
|
|
.refcnt = 1,
|
|
|
|
.id = 0,
|
|
|
|
.primary = 1,
|
|
|
|
};
|
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
/*
|
|
|
|
* NOTE: Clocks for "ordinary peripheral" devices e.g. spi0, udp0, uhp0 etc.
|
|
|
|
* are now created automatically. Only "system" clocks need be defined here.
|
|
|
|
*/
|
2006-03-24 07:37:56 +00:00
|
|
|
static struct at91_pmc_clock main_ck = {
|
2012-08-11 05:03:30 +00:00
|
|
|
.name = "main", /* Main clock */
|
2006-03-24 07:37:56 +00:00
|
|
|
.refcnt = 0,
|
|
|
|
.id = 1,
|
|
|
|
.primary = 1,
|
|
|
|
.pmc_mask = PMC_IER_MOSCS,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct at91_pmc_clock plla = {
|
2012-08-11 05:03:30 +00:00
|
|
|
.name = "plla", /* PLLA Clock, used for CPU clocking */
|
2006-03-24 07:37:56 +00:00
|
|
|
.parent = &main_ck,
|
|
|
|
.refcnt = 1,
|
|
|
|
.id = 0,
|
|
|
|
.primary = 1,
|
|
|
|
.pll = 1,
|
|
|
|
.pmc_mask = PMC_IER_LOCKA,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct at91_pmc_clock pllb = {
|
2012-08-11 05:03:30 +00:00
|
|
|
.name = "pllb", /* PLLB Clock, used for USB functions */
|
2006-03-24 07:37:56 +00:00
|
|
|
.parent = &main_ck,
|
|
|
|
.refcnt = 0,
|
|
|
|
.id = 0,
|
|
|
|
.primary = 1,
|
|
|
|
.pll = 1,
|
|
|
|
.pmc_mask = PMC_IER_LOCKB,
|
|
|
|
.set_mode = &at91_pmc_set_pllb_mode,
|
|
|
|
};
|
|
|
|
|
2012-07-26 08:01:25 +00:00
|
|
|
/* Used by USB on at91sam9g45 */
|
|
|
|
static struct at91_pmc_clock upll = {
|
2012-08-11 05:03:30 +00:00
|
|
|
.name = "upll", /* UTMI PLL, used for USB functions on 9G45 family */
|
2012-07-26 08:01:25 +00:00
|
|
|
.parent = &main_ck,
|
|
|
|
.refcnt = 0,
|
|
|
|
.id = 0,
|
|
|
|
.primary = 1,
|
|
|
|
.pll = 1,
|
|
|
|
.pmc_mask = (1 << 6),
|
|
|
|
.set_mode = &at91_pmc_set_upll_mode,
|
|
|
|
};
|
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
static struct at91_pmc_clock udpck = {
|
|
|
|
.name = "udpck",
|
|
|
|
.parent = &pllb,
|
|
|
|
.pmc_mask = PMC_SCER_UDP,
|
|
|
|
.set_mode = at91_pmc_set_sys_mode
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct at91_pmc_clock uhpck = {
|
|
|
|
.name = "uhpck",
|
|
|
|
.parent = &pllb,
|
|
|
|
.pmc_mask = PMC_SCER_UHP,
|
|
|
|
.set_mode = at91_pmc_set_sys_mode
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct at91_pmc_clock mck = {
|
2012-08-11 05:03:30 +00:00
|
|
|
.name = "mck", /* Master (Peripheral) Clock */
|
2006-03-24 07:37:56 +00:00
|
|
|
.pmc_mask = PMC_IER_MCKRDY,
|
|
|
|
.refcnt = 0,
|
|
|
|
};
|
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
static struct at91_pmc_clock cpu = {
|
2012-08-11 05:03:30 +00:00
|
|
|
.name = "cpu", /* CPU Clock */
|
2010-10-06 22:25:21 +00:00
|
|
|
.parent = &plla,
|
|
|
|
.pmc_mask = PMC_SCER_PCK,
|
|
|
|
.refcnt = 0,
|
2006-03-24 07:37:56 +00:00
|
|
|
};
|
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
/* "+32" or the automatic peripheral clocks */
|
|
|
|
static struct at91_pmc_clock *clock_list[16+32] = {
|
2006-03-24 07:37:56 +00:00
|
|
|
&slck,
|
|
|
|
&main_ck,
|
|
|
|
&plla,
|
|
|
|
&pllb,
|
2012-07-26 08:01:25 +00:00
|
|
|
&upll,
|
2006-03-24 07:37:56 +00:00
|
|
|
&udpck,
|
|
|
|
&uhpck,
|
|
|
|
&mck,
|
2010-10-06 22:25:21 +00:00
|
|
|
&cpu
|
2006-03-24 07:37:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline uint32_t
|
|
|
|
RD4(struct at91_pmc_softc *sc, bus_size_t off)
|
|
|
|
{
|
2012-04-14 17:17:55 +00:00
|
|
|
|
2012-05-29 03:23:18 +00:00
|
|
|
if (sc == NULL) {
|
|
|
|
uint32_t *p = (uint32_t *)(AT91_BASE + AT91_PMC_BASE + off);
|
|
|
|
|
|
|
|
return *p;
|
|
|
|
}
|
2010-10-06 22:25:21 +00:00
|
|
|
return (bus_read_4(sc->mem_res, off));
|
2006-03-24 07:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
WR4(struct at91_pmc_softc *sc, bus_size_t off, uint32_t val)
|
|
|
|
{
|
2012-04-14 17:17:55 +00:00
|
|
|
|
2012-05-29 03:23:18 +00:00
|
|
|
if (sc == NULL) {
|
|
|
|
uint32_t *p = (uint32_t *)(AT91_BASE + AT91_PMC_BASE + off);
|
|
|
|
|
|
|
|
*p = val;
|
|
|
|
} else
|
|
|
|
bus_write_4(sc->mem_res, off, val);
|
2006-03-24 07:37:56 +00:00
|
|
|
}
|
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
void
|
2006-03-24 07:37:56 +00:00
|
|
|
at91_pmc_set_pllb_mode(struct at91_pmc_clock *clk, int on)
|
|
|
|
{
|
|
|
|
struct at91_pmc_softc *sc = pmc_softc;
|
|
|
|
uint32_t value;
|
|
|
|
|
|
|
|
if (on) {
|
|
|
|
on = PMC_IER_LOCKB;
|
2012-05-29 03:23:18 +00:00
|
|
|
value = pllb_init;
|
2012-04-14 11:29:32 +00:00
|
|
|
} else
|
2006-03-24 07:37:56 +00:00
|
|
|
value = 0;
|
2010-10-06 22:25:21 +00:00
|
|
|
|
|
|
|
/* Workaround RM9200 Errata #26 */
|
|
|
|
if (at91_is_rm92() &&
|
|
|
|
((value ^ RD4(sc, CKGR_PLLBR)) & 0x03f0ff) != 0) {
|
|
|
|
WR4(sc, CKGR_PLLBR, value ^ 1);
|
|
|
|
while ((RD4(sc, PMC_SR) & PMC_IER_LOCKB) != on)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
WR4(sc, CKGR_PLLBR, value);
|
|
|
|
while ((RD4(sc, PMC_SR) & PMC_IER_LOCKB) != on)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-07-26 08:01:25 +00:00
|
|
|
static void
|
|
|
|
at91_pmc_set_upll_mode(struct at91_pmc_clock *clk, int on)
|
|
|
|
{
|
|
|
|
struct at91_pmc_softc *sc = pmc_softc;
|
|
|
|
uint32_t value;
|
|
|
|
|
|
|
|
if (on) {
|
|
|
|
on = PMC_IER_LOCKU;
|
|
|
|
value = CKGR_UCKR_UPLLEN | CKGR_UCKR_BIASEN;
|
|
|
|
} else
|
|
|
|
value = 0;
|
|
|
|
|
|
|
|
WR4(sc, CKGR_UCKR, RD4(sc, CKGR_UCKR) | value);
|
|
|
|
while ((RD4(sc, PMC_SR) & PMC_IER_LOCKU) != on)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
WR4(sc, PMC_USB, PMC_USB_USBDIV(9) | PMC_USB_USBS);
|
|
|
|
WR4(sc, PMC_SCER, PMC_SCER_UHP_SAM9);
|
|
|
|
}
|
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
static void
|
|
|
|
at91_pmc_set_sys_mode(struct at91_pmc_clock *clk, int on)
|
|
|
|
{
|
|
|
|
struct at91_pmc_softc *sc = pmc_softc;
|
|
|
|
|
|
|
|
WR4(sc, on ? PMC_SCER : PMC_SCDR, clk->pmc_mask);
|
|
|
|
if (on)
|
|
|
|
while ((RD4(sc, PMC_SCSR) & clk->pmc_mask) != clk->pmc_mask)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
while ((RD4(sc, PMC_SCSR) & clk->pmc_mask) == clk->pmc_mask)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
at91_pmc_set_periph_mode(struct at91_pmc_clock *clk, int on)
|
|
|
|
{
|
|
|
|
struct at91_pmc_softc *sc = pmc_softc;
|
|
|
|
|
|
|
|
WR4(sc, on ? PMC_PCER : PMC_PCDR, clk->pmc_mask);
|
|
|
|
if (on)
|
|
|
|
while ((RD4(sc, PMC_PCSR) & clk->pmc_mask) != clk->pmc_mask)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
while ((RD4(sc, PMC_PCSR) & clk->pmc_mask) == clk->pmc_mask)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
struct at91_pmc_clock *
|
2012-04-14 17:17:55 +00:00
|
|
|
at91_pmc_clock_add(const char *name, uint32_t irq,
|
|
|
|
struct at91_pmc_clock *parent)
|
2010-10-06 22:25:21 +00:00
|
|
|
{
|
|
|
|
struct at91_pmc_clock *clk;
|
|
|
|
int i, buflen;
|
|
|
|
|
|
|
|
clk = malloc(sizeof(*clk), M_PMC, M_NOWAIT | M_ZERO);
|
2012-04-14 11:29:32 +00:00
|
|
|
if (clk == NULL)
|
2010-10-06 22:25:21 +00:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
buflen = strlen(name) + 1;
|
|
|
|
clk->name = malloc(buflen, M_PMC, M_NOWAIT);
|
2012-04-14 11:29:32 +00:00
|
|
|
if (clk->name == NULL)
|
2010-10-06 22:25:21 +00:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
strlcpy(clk->name, name, buflen);
|
|
|
|
clk->pmc_mask = 1 << irq;
|
|
|
|
clk->set_mode = &at91_pmc_set_periph_mode;
|
|
|
|
if (parent == NULL)
|
|
|
|
clk->parent = &mck;
|
|
|
|
else
|
|
|
|
clk->parent = parent;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(clock_list) / sizeof(clock_list[0]); i++) {
|
|
|
|
if (clock_list[i] == NULL) {
|
|
|
|
clock_list[i] = clk;
|
|
|
|
return (clk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err:
|
|
|
|
if (clk != NULL) {
|
2012-04-14 11:29:32 +00:00
|
|
|
if (clk->name != NULL)
|
2010-10-06 22:25:21 +00:00
|
|
|
free(clk->name, M_PMC);
|
|
|
|
free(clk, M_PMC);
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("could not allocate pmc clock '%s'", name);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
at91_pmc_clock_alias(const char *name, const char *alias)
|
|
|
|
{
|
|
|
|
struct at91_pmc_clock *clk, *alias_clk;
|
|
|
|
|
|
|
|
clk = at91_pmc_clock_ref(name);
|
|
|
|
if (clk)
|
|
|
|
alias_clk = at91_pmc_clock_add(alias, 0, clk->parent);
|
|
|
|
|
|
|
|
if (clk && alias_clk) {
|
|
|
|
alias_clk->hz = clk->hz;
|
|
|
|
alias_clk->pmc_mask = clk->pmc_mask;
|
|
|
|
alias_clk->set_mode = clk->set_mode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
struct at91_pmc_clock *
|
|
|
|
at91_pmc_clock_ref(const char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
for (i = 0; i < sizeof(clock_list) / sizeof(clock_list[0]); i++) {
|
|
|
|
if (clock_list[i] == NULL)
|
|
|
|
break;
|
2006-03-24 07:37:56 +00:00
|
|
|
if (strcmp(name, clock_list[i]->name) == 0)
|
|
|
|
return (clock_list[i]);
|
2010-10-06 22:25:21 +00:00
|
|
|
}
|
2006-03-24 07:37:56 +00:00
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
at91_pmc_clock_deref(struct at91_pmc_clock *clk)
|
|
|
|
{
|
2012-04-14 17:17:55 +00:00
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
at91_pmc_clock_enable(struct at91_pmc_clock *clk)
|
|
|
|
{
|
2012-04-14 17:17:55 +00:00
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
/* XXX LOCKING? XXX */
|
|
|
|
if (clk->parent)
|
|
|
|
at91_pmc_clock_enable(clk->parent);
|
|
|
|
if (clk->refcnt++ == 0 && clk->set_mode)
|
|
|
|
clk->set_mode(clk, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
at91_pmc_clock_disable(struct at91_pmc_clock *clk)
|
|
|
|
{
|
2012-04-14 17:17:55 +00:00
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
/* XXX LOCKING? XXX */
|
|
|
|
if (--clk->refcnt == 0 && clk->set_mode)
|
|
|
|
clk->set_mode(clk, 0);
|
|
|
|
if (clk->parent)
|
|
|
|
at91_pmc_clock_disable(clk->parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-10-06 22:25:21 +00:00
|
|
|
at91_pmc_pll_rate(struct at91_pmc_clock *clk, uint32_t reg)
|
2006-03-24 07:37:56 +00:00
|
|
|
{
|
2010-12-06 10:24:06 +00:00
|
|
|
uint32_t mul, div, freq;
|
2010-10-06 22:25:21 +00:00
|
|
|
|
|
|
|
freq = clk->parent->hz;
|
|
|
|
div = (reg >> clk->pll_div_shift) & clk->pll_div_mask;
|
|
|
|
mul = (reg >> clk->pll_mul_shift) & clk->pll_mul_mask;
|
2006-03-24 07:37:56 +00:00
|
|
|
|
2012-04-14 11:29:32 +00:00
|
|
|
#if 0
|
|
|
|
printf("pll = (%d / %d) * %d = %d\n",
|
|
|
|
freq, div, mul + 1, (freq/div) * (mul+1));
|
|
|
|
#endif
|
2010-10-06 22:40:27 +00:00
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
if (div != 0 && mul != 0) {
|
|
|
|
freq /= div;
|
|
|
|
freq *= mul + 1;
|
2012-04-14 11:29:32 +00:00
|
|
|
} else
|
2006-03-24 07:37:56 +00:00
|
|
|
freq = 0;
|
2010-10-06 22:25:21 +00:00
|
|
|
clk->hz = freq;
|
2010-10-06 22:40:27 +00:00
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
return (freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
2010-10-06 22:25:21 +00:00
|
|
|
at91_pmc_pll_calc(struct at91_pmc_clock *clk, uint32_t out_freq)
|
2006-03-24 07:37:56 +00:00
|
|
|
{
|
2008-07-22 14:04:06 +00:00
|
|
|
uint32_t i, div = 0, mul = 0, diff = 1 << 30;
|
2006-03-24 07:37:56 +00:00
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
unsigned ret = 0x3e00;
|
|
|
|
|
|
|
|
if (out_freq > clk->pll_max_out)
|
2008-07-22 14:04:06 +00:00
|
|
|
goto fail;
|
2006-03-24 07:37:56 +00:00
|
|
|
|
2008-07-22 14:04:06 +00:00
|
|
|
for (i = 1; i < 256; i++) {
|
|
|
|
int32_t diff1;
|
|
|
|
uint32_t input, mul1;
|
2006-03-24 07:37:56 +00:00
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
input = clk->parent->hz / i;
|
|
|
|
if (input < clk->pll_min_in)
|
2008-07-22 14:04:06 +00:00
|
|
|
break;
|
2010-10-06 22:25:21 +00:00
|
|
|
if (input > clk->pll_max_in)
|
2008-07-22 14:04:06 +00:00
|
|
|
continue;
|
2006-03-24 07:37:56 +00:00
|
|
|
|
2008-07-22 14:04:06 +00:00
|
|
|
mul1 = out_freq / input;
|
2010-10-06 22:25:21 +00:00
|
|
|
if (mul1 > (clk->pll_mul_mask + 1))
|
2008-07-22 14:04:06 +00:00
|
|
|
continue;
|
2010-10-06 22:25:21 +00:00
|
|
|
if (mul1 == 0)
|
2008-07-22 14:04:06 +00:00
|
|
|
break;
|
2006-03-24 07:37:56 +00:00
|
|
|
|
2008-07-22 14:04:06 +00:00
|
|
|
diff1 = out_freq - input * mul1;
|
2006-03-24 07:37:56 +00:00
|
|
|
if (diff1 < 0)
|
2008-07-22 14:04:06 +00:00
|
|
|
diff1 = -diff1;
|
|
|
|
if (diff > diff1) {
|
|
|
|
diff = diff1;
|
|
|
|
div = i;
|
|
|
|
mul = mul1;
|
|
|
|
if (diff == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (diff > (out_freq >> PMC_PLL_SHIFT_TOL))
|
2006-03-24 07:37:56 +00:00
|
|
|
goto fail;
|
2010-10-06 22:25:21 +00:00
|
|
|
|
|
|
|
if (clk->set_outb != NULL)
|
|
|
|
ret |= clk->set_outb(out_freq);
|
|
|
|
|
|
|
|
return (ret |
|
|
|
|
((mul - 1) << clk->pll_mul_shift) |
|
|
|
|
(div << clk->pll_div_shift));
|
2006-03-24 07:37:56 +00:00
|
|
|
fail:
|
2010-10-06 22:25:21 +00:00
|
|
|
return (0);
|
2006-03-24 07:37:56 +00:00
|
|
|
}
|
|
|
|
|
2012-05-21 04:24:58 +00:00
|
|
|
#if !defined(AT91C_MAIN_CLOCK)
|
|
|
|
static const unsigned int at91_main_clock_tbl[] = {
|
|
|
|
3000000, 3276800, 3686400, 3840000, 4000000,
|
|
|
|
4433619, 4915200, 5000000, 5242880, 6000000,
|
|
|
|
6144000, 6400000, 6553600, 7159090, 7372800,
|
|
|
|
7864320, 8000000, 9830400, 10000000, 11059200,
|
|
|
|
12000000, 12288000, 13560000, 14318180, 14745600,
|
|
|
|
16000000, 17344700, 18432000, 20000000
|
|
|
|
};
|
|
|
|
#define MAIN_CLOCK_TBL_LEN (sizeof(at91_main_clock_tbl) / sizeof(*at91_main_clock_tbl))
|
2012-05-29 03:23:18 +00:00
|
|
|
#endif
|
2012-05-21 04:24:58 +00:00
|
|
|
|
|
|
|
static unsigned int
|
2012-05-29 03:23:18 +00:00
|
|
|
at91_pmc_sense_main_clock(void)
|
2012-05-21 04:24:58 +00:00
|
|
|
{
|
2012-05-29 03:23:18 +00:00
|
|
|
#if !defined(AT91C_MAIN_CLOCK)
|
2012-05-21 04:24:58 +00:00
|
|
|
unsigned int ckgr_val;
|
|
|
|
unsigned int diff, matchdiff, freq;
|
|
|
|
int i;
|
|
|
|
|
2012-05-29 03:23:18 +00:00
|
|
|
ckgr_val = (RD4(NULL, CKGR_MCFR) & CKGR_MCFR_MAINF_MASK) << 11;
|
2012-05-21 04:24:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Clocks up to 50MHz can be connected to some models. If
|
|
|
|
* the frequency is >= 21MHz, assume that the slow clock can
|
|
|
|
* measure it correctly, and that any error can be adequately
|
|
|
|
* compensated for by roudning to the nearest 500Hz. Users
|
|
|
|
* with fast, or odd-ball clocks will need to set
|
2012-06-04 04:24:59 +00:00
|
|
|
* AT91C_MAIN_CLOCK in the kernel config file.
|
2012-05-21 04:24:58 +00:00
|
|
|
*/
|
|
|
|
if (ckgr_val >= 21000000)
|
|
|
|
return ((ckgr_val + 250) / 500 * 500);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to find the standard frequency that match best.
|
|
|
|
*/
|
|
|
|
freq = at91_main_clock_tbl[0];
|
|
|
|
matchdiff = abs(ckgr_val - at91_main_clock_tbl[0]);
|
|
|
|
for (i = 1; i < MAIN_CLOCK_TBL_LEN; i++) {
|
|
|
|
diff = abs(ckgr_val - at91_main_clock_tbl[i]);
|
|
|
|
if (diff < matchdiff) {
|
|
|
|
freq = at91_main_clock_tbl[i];
|
|
|
|
matchdiff = diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (freq);
|
2012-05-29 03:23:18 +00:00
|
|
|
#else
|
|
|
|
return (AT91C_MAIN_CLOCK);
|
2012-05-21 04:24:58 +00:00
|
|
|
#endif
|
2012-05-29 03:23:18 +00:00
|
|
|
}
|
2012-05-21 04:24:58 +00:00
|
|
|
|
2012-05-29 03:23:18 +00:00
|
|
|
void
|
|
|
|
at91_pmc_init_clock(void)
|
2006-03-24 07:37:56 +00:00
|
|
|
{
|
2012-05-29 03:23:18 +00:00
|
|
|
struct at91_pmc_softc *sc = NULL;
|
2012-05-21 04:24:58 +00:00
|
|
|
unsigned int main_clock;
|
2006-03-24 07:37:56 +00:00
|
|
|
uint32_t mckr;
|
2010-10-06 22:25:21 +00:00
|
|
|
uint32_t mdiv;
|
2006-03-24 07:37:56 +00:00
|
|
|
|
2012-05-29 03:23:18 +00:00
|
|
|
main_clock = at91_pmc_sense_main_clock();
|
2012-05-21 04:24:58 +00:00
|
|
|
|
2012-04-14 17:17:55 +00:00
|
|
|
if (at91_is_sam9() || at91_is_sam9xe()) {
|
2010-10-06 22:25:21 +00:00
|
|
|
uhpck.pmc_mask = PMC_SCER_UHP_SAM9;
|
|
|
|
udpck.pmc_mask = PMC_SCER_UDP_SAM9;
|
|
|
|
}
|
2012-08-10 04:48:06 +00:00
|
|
|
|
2012-07-26 08:01:25 +00:00
|
|
|
/* There is no pllb on AT91SAM9G45 */
|
|
|
|
if (at91_cpu_is(AT91_T_SAM9G45)) {
|
|
|
|
uhpck.parent = &upll;
|
|
|
|
uhpck.pmc_mask = PMC_SCER_UHP_SAM9;
|
|
|
|
}
|
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
mckr = RD4(sc, PMC_MCKR);
|
2006-03-24 07:37:56 +00:00
|
|
|
main_ck.hz = main_clock;
|
2010-10-06 22:25:21 +00:00
|
|
|
|
2012-08-11 05:03:30 +00:00
|
|
|
/*
|
|
|
|
* Note: this means outa calc code for plla never used since
|
|
|
|
* we never change it. If we did, we'd also have to mind
|
|
|
|
* ICPLLA to get the charge pump current right.
|
|
|
|
*/
|
2010-10-06 22:25:21 +00:00
|
|
|
at91_pmc_pll_rate(&plla, RD4(sc, CKGR_PLLAR));
|
|
|
|
|
Enhance the Atmel SoC chip identification routines to account for more
SoC variants. Fold the AT91SAM9XE chips into the AT91SAM9260
handling, where appropriate. The following SoCs/SoC families are recognized:
at91cap9, at91rm9200, at91sam9260, at91sam9261, at91sam9263,
at91sam9g10, at91sam9g20, at91sam9g45, at91sam9n12, at91sam9rl,
at91sam9x5
and the following variations are also recognized:
at91rm9200_bga, at91rm9200_pqfp, at91sam9xe, at91sam9g45, at91sam9m10,
at91sam9g46, at91sam9m11, at91sam9g15, at91sam9g25, at91sam9g35,
at91sam9x25, at91sam9x35
This is only the identification routine: no additional Atmel devices
are supported at this time.
# With these changes, I'm able to boot to the point of identification
# on a few different Atmel SoCs that we don't yet support using the
# KB920X config file -- someday tht will be an ATMEL config file...
2012-06-06 06:19:52 +00:00
|
|
|
if (at91_cpu_is(AT91_T_SAM9G45) && (mckr & PMC_MCKR_PLLADIV2))
|
2010-10-06 22:25:21 +00:00
|
|
|
plla.hz /= 2;
|
2006-03-24 07:37:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the usb clock. This sets up pllb, but disables the
|
2012-08-10 04:48:06 +00:00
|
|
|
* actual clock. XXX except for the if 0 :(
|
2006-03-24 07:37:56 +00:00
|
|
|
*/
|
2012-08-10 04:48:06 +00:00
|
|
|
if (!at91_cpu_is(AT91_T_SAM9G45)) {
|
|
|
|
pllb_init = at91_pmc_pll_calc(&pllb, 48000000 * 2) | 0x10000000;
|
|
|
|
at91_pmc_pll_rate(&pllb, pllb_init);
|
2010-10-06 22:25:21 +00:00
|
|
|
#if 0
|
2012-08-10 04:48:06 +00:00
|
|
|
/* Turn off USB clocks */
|
|
|
|
at91_pmc_set_periph_mode(&ohci_clk, 0);
|
|
|
|
at91_pmc_set_periph_mode(&udc_clk, 0);
|
2010-07-14 00:48:53 +00:00
|
|
|
#endif
|
2012-08-10 04:48:06 +00:00
|
|
|
}
|
2006-03-24 07:37:56 +00:00
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
if (at91_is_rm92()) {
|
|
|
|
WR4(sc, PMC_SCDR, PMC_SCER_UHP | PMC_SCER_UDP);
|
|
|
|
WR4(sc, PMC_SCER, PMC_SCER_MCKUDP);
|
2012-04-14 11:29:32 +00:00
|
|
|
} else
|
2010-10-06 22:25:21 +00:00
|
|
|
WR4(sc, PMC_SCDR, PMC_SCER_UHP_SAM9 | PMC_SCER_UDP_SAM9);
|
|
|
|
WR4(sc, CKGR_PLLBR, 0);
|
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
/*
|
|
|
|
* MCK and PCU derive from one of the primary clocks. Initialize
|
|
|
|
* this relationship.
|
|
|
|
*/
|
|
|
|
mck.parent = clock_list[mckr & 0x3];
|
|
|
|
mck.parent->refcnt++;
|
2010-10-06 22:25:21 +00:00
|
|
|
|
2012-04-14 11:29:32 +00:00
|
|
|
cpu.hz = mck.hz = mck.parent->hz /
|
|
|
|
(1 << ((mckr & PMC_MCKR_PRES_MASK) >> 2));
|
2010-10-06 22:25:21 +00:00
|
|
|
|
|
|
|
mdiv = (mckr & PMC_MCKR_MDIV_MASK) >> 8;
|
2012-04-14 17:17:55 +00:00
|
|
|
if (at91_is_sam9() || at91_is_sam9xe()) {
|
2012-07-26 08:01:25 +00:00
|
|
|
/*
|
|
|
|
* On AT91SAM9G45 when mdiv == 3 we need to divide
|
|
|
|
* MCK by 3 but not, for example, on 9g20.
|
|
|
|
*/
|
|
|
|
if (!at91_cpu_is(AT91_T_SAM9G45) || mdiv <= 2)
|
|
|
|
mdiv *= 2;
|
2010-10-06 22:40:27 +00:00
|
|
|
if (mdiv > 0)
|
2012-07-26 08:01:25 +00:00
|
|
|
mck.hz /= mdiv;
|
2012-04-14 11:29:32 +00:00
|
|
|
} else
|
2010-10-06 22:25:21 +00:00
|
|
|
mck.hz /= (1 + mdiv);
|
|
|
|
|
|
|
|
/* Only found on SAM9G20 */
|
Enhance the Atmel SoC chip identification routines to account for more
SoC variants. Fold the AT91SAM9XE chips into the AT91SAM9260
handling, where appropriate. The following SoCs/SoC families are recognized:
at91cap9, at91rm9200, at91sam9260, at91sam9261, at91sam9263,
at91sam9g10, at91sam9g20, at91sam9g45, at91sam9n12, at91sam9rl,
at91sam9x5
and the following variations are also recognized:
at91rm9200_bga, at91rm9200_pqfp, at91sam9xe, at91sam9g45, at91sam9m10,
at91sam9g46, at91sam9m11, at91sam9g15, at91sam9g25, at91sam9g35,
at91sam9x25, at91sam9x35
This is only the identification routine: no additional Atmel devices
are supported at this time.
# With these changes, I'm able to boot to the point of identification
# on a few different Atmel SoCs that we don't yet support using the
# KB920X config file -- someday tht will be an ATMEL config file...
2012-06-06 06:19:52 +00:00
|
|
|
if (at91_cpu_is(AT91_T_SAM9G20))
|
2010-10-06 22:25:21 +00:00
|
|
|
cpu.hz /= (mckr & PMC_MCKR_PDIV) ? 2 : 1;
|
|
|
|
|
|
|
|
at91_master_clock = mck.hz;
|
2006-03-24 07:37:56 +00:00
|
|
|
|
2012-05-21 04:24:58 +00:00
|
|
|
/* These clocks refrenced by "special" names */
|
|
|
|
at91_pmc_clock_alias("ohci0", "ohci_clk");
|
|
|
|
at91_pmc_clock_alias("udp0", "udp_clk");
|
2010-10-06 22:25:21 +00:00
|
|
|
|
|
|
|
/* Turn off "Progamable" clocks */
|
2006-03-24 07:37:56 +00:00
|
|
|
WR4(sc, PMC_SCDR, PMC_SCER_PCK0 | PMC_SCER_PCK1 | PMC_SCER_PCK2 |
|
|
|
|
PMC_SCER_PCK3);
|
2010-10-06 22:25:21 +00:00
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
/* XXX kludge, turn on all peripherals */
|
|
|
|
WR4(sc, PMC_PCER, 0xffffffff);
|
2010-10-06 22:25:21 +00:00
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
/* Disable all interrupts for PMC */
|
|
|
|
WR4(sc, PMC_IDR, 0xffffffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
at91_pmc_deactivate(device_t dev)
|
|
|
|
{
|
|
|
|
struct at91_pmc_softc *sc;
|
|
|
|
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
bus_generic_detach(sc->dev);
|
|
|
|
if (sc->mem_res)
|
|
|
|
bus_release_resource(dev, SYS_RES_IOPORT,
|
|
|
|
rman_get_rid(sc->mem_res), sc->mem_res);
|
|
|
|
sc->mem_res = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
at91_pmc_activate(device_t dev)
|
|
|
|
{
|
|
|
|
struct at91_pmc_softc *sc;
|
|
|
|
int rid;
|
|
|
|
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
rid = 0;
|
|
|
|
sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
|
|
|
|
RF_ACTIVE);
|
|
|
|
if (sc->mem_res == NULL)
|
|
|
|
goto errout;
|
|
|
|
return (0);
|
|
|
|
errout:
|
|
|
|
at91_pmc_deactivate(dev);
|
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
at91_pmc_probe(device_t dev)
|
|
|
|
{
|
|
|
|
|
|
|
|
device_set_desc(dev, "PMC");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
at91_pmc_attach(device_t dev)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
pmc_softc = device_get_softc(dev);
|
|
|
|
pmc_softc->dev = dev;
|
|
|
|
if ((err = at91_pmc_activate(dev)) != 0)
|
2010-10-06 22:25:21 +00:00
|
|
|
return (err);
|
2008-11-30 22:33:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Configure main clock frequency.
|
|
|
|
*/
|
2012-05-29 03:23:18 +00:00
|
|
|
at91_pmc_init_clock();
|
2010-10-06 22:25:21 +00:00
|
|
|
|
2012-05-29 03:23:18 +00:00
|
|
|
/*
|
|
|
|
* Display info about clocks previously computed
|
|
|
|
*/
|
2012-05-21 04:24:58 +00:00
|
|
|
device_printf(dev,
|
|
|
|
"Primary: %d Hz PLLA: %d MHz CPU: %d MHz MCK: %d MHz\n",
|
|
|
|
main_ck.hz,
|
|
|
|
plla.hz / 1000000,
|
|
|
|
cpu.hz / 1000000, mck.hz / 1000000);
|
2010-10-06 22:25:21 +00:00
|
|
|
|
2006-03-24 07:37:56 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static device_method_t at91_pmc_methods[] = {
|
|
|
|
DEVMETHOD(device_probe, at91_pmc_probe),
|
|
|
|
DEVMETHOD(device_attach, at91_pmc_attach),
|
2012-04-14 11:29:32 +00:00
|
|
|
DEVMETHOD_END
|
2006-03-24 07:37:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static driver_t at91_pmc_driver = {
|
|
|
|
"at91_pmc",
|
|
|
|
at91_pmc_methods,
|
|
|
|
sizeof(struct at91_pmc_softc),
|
|
|
|
};
|
|
|
|
static devclass_t at91_pmc_devclass;
|
|
|
|
|
2012-04-14 11:29:32 +00:00
|
|
|
DRIVER_MODULE(at91_pmc, atmelarm, at91_pmc_driver, at91_pmc_devclass, NULL,
|
|
|
|
NULL);
|