📄 clock.c
字号:
/* * linux/arch/arm/mach-at91/clock.c * * Copyright (C) 2005 David Brownell * Copyright (C) 2005 Ivan Kokshaysky * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. */#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/debugfs.h>#include <linux/seq_file.h>#include <linux/list.h>#include <linux/errno.h>#include <linux/err.h>#include <linux/spinlock.h>#include <linux/delay.h>#include <linux/clk.h>#include <asm/semaphore.h>#include <asm/io.h>#include <asm/mach-types.h>#include <asm/hardware.h>#include <asm/arch/at91_pmc.h>#include <asm/arch/cpu.h>#include "clock.h"/* * There's a lot more which can be done with clocks, including cpufreq * integration, slow clock mode support (for system suspend), letting * PLLB be used at other rates (on boards that don't need USB), etc. */#define clk_is_primary(x) ((x)->type & CLK_TYPE_PRIMARY)#define clk_is_programmable(x) ((x)->type & CLK_TYPE_PROGRAMMABLE)#define clk_is_peripheral(x) ((x)->type & CLK_TYPE_PERIPHERAL)#define clk_is_sys(x) ((x)->type & CLK_TYPE_SYSTEM)static LIST_HEAD(clocks);static DEFINE_SPINLOCK(clk_lock);static u32 at91_pllb_usb_init;/* * Four primary clock sources: two crystal oscillators (32K, main), and * two PLLs. PLLA usually runs the master clock; and PLLB must run at * 48 MHz (unless no USB function clocks are needed). The main clock and * both PLLs are turned off to run in "slow clock mode" (system suspend). */static struct clk clk32k = { .name = "clk32k", .rate_hz = AT91_SLOW_CLOCK, .users = 1, /* always on */ .id = 0, .type = CLK_TYPE_PRIMARY,};static struct clk main_clk = { .name = "main", .pmc_mask = AT91_PMC_MOSCS, /* in PMC_SR */ .id = 1, .type = CLK_TYPE_PRIMARY,};static struct clk plla = { .name = "plla", .parent = &main_clk, .pmc_mask = AT91_PMC_LOCKA, /* in PMC_SR */ .id = 2, .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,};static void pllb_mode(struct clk *clk, int is_on){ u32 value; if (is_on) { is_on = AT91_PMC_LOCKB; value = at91_pllb_usb_init; } else value = 0; // REVISIT: Add work-around for AT91RM9200 Errata #26 ? at91_sys_write(AT91_CKGR_PLLBR, value); do { cpu_relax(); } while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKB) != is_on);}static struct clk pllb = { .name = "pllb", .parent = &main_clk, .pmc_mask = AT91_PMC_LOCKB, /* in PMC_SR */ .mode = pllb_mode, .id = 3, .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,};static void pmc_sys_mode(struct clk *clk, int is_on){ if (is_on) at91_sys_write(AT91_PMC_SCER, clk->pmc_mask); else at91_sys_write(AT91_PMC_SCDR, clk->pmc_mask);}/* USB function clocks (PLLB must be 48 MHz) */static struct clk udpck = { .name = "udpck", .parent = &pllb, .mode = pmc_sys_mode,};static struct clk uhpck = { .name = "uhpck", .parent = &pllb, .mode = pmc_sys_mode,};/* * The master clock is divided from the CPU clock (by 1-4). It's used for * memory, interfaces to on-chip peripherals, the AIC, and sometimes more * (e.g baud rate generation). It's sourced from one of the primary clocks. */static struct clk mck = { .name = "mck", .pmc_mask = AT91_PMC_MCKRDY, /* in PMC_SR */};static void pmc_periph_mode(struct clk *clk, int is_on){ if (is_on) at91_sys_write(AT91_PMC_PCER, clk->pmc_mask); else at91_sys_write(AT91_PMC_PCDR, clk->pmc_mask);}static struct clk __init *at91_css_to_clk(unsigned long css){ switch (css) { case AT91_PMC_CSS_SLOW: return &clk32k; case AT91_PMC_CSS_MAIN: return &main_clk; case AT91_PMC_CSS_PLLA: return &plla; case AT91_PMC_CSS_PLLB: return &pllb; } return NULL;}/* * Associate a particular clock with a function (eg, "uart") and device. * The drivers can then request the same 'function' with several different * devices and not care about which clock name to use. */void __init at91_clock_associate(const char *id, struct device *dev, const char *func){ struct clk *clk = clk_get(NULL, id); if (!dev || !clk || !IS_ERR(clk_get(dev, func))) return; clk->function = func; clk->dev = dev;}/* clocks cannot be de-registered no refcounting necessary */struct clk *clk_get(struct device *dev, const char *id){ struct clk *clk; list_for_each_entry(clk, &clocks, node) { if (strcmp(id, clk->name) == 0) return clk; if (clk->function && (dev == clk->dev) && strcmp(id, clk->function) == 0) return clk; } return ERR_PTR(-ENOENT);}EXPORT_SYMBOL(clk_get);void clk_put(struct clk *clk){}EXPORT_SYMBOL(clk_put);static void __clk_enable(struct clk *clk){ if (clk->parent) __clk_enable(clk->parent); if (clk->users++ == 0 && clk->mode) clk->mode(clk, 1);}int clk_enable(struct clk *clk){ unsigned long flags; spin_lock_irqsave(&clk_lock, flags); __clk_enable(clk); spin_unlock_irqrestore(&clk_lock, flags); return 0;}EXPORT_SYMBOL(clk_enable);static void __clk_disable(struct clk *clk){ BUG_ON(clk->users == 0); if (--clk->users == 0 && clk->mode) clk->mode(clk, 0); if (clk->parent) __clk_disable(clk->parent);}void clk_disable(struct clk *clk){ unsigned long flags; spin_lock_irqsave(&clk_lock, flags); __clk_disable(clk); spin_unlock_irqrestore(&clk_lock, flags);}EXPORT_SYMBOL(clk_disable);unsigned long clk_get_rate(struct clk *clk){ unsigned long flags; unsigned long rate; spin_lock_irqsave(&clk_lock, flags); for (;;) { rate = clk->rate_hz; if (rate || !clk->parent) break; clk = clk->parent; } spin_unlock_irqrestore(&clk_lock, flags); return rate;}EXPORT_SYMBOL(clk_get_rate);/*------------------------------------------------------------------------*/#ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS/* * For now, only the programmable clocks support reparenting (MCK could * do this too, with care) or rate changing (the PLLs could do this too, * ditto MCK but that's more for cpufreq). Drivers may reparent to get * a better rate match; we don't. */long clk_round_rate(struct clk *clk, unsigned long rate){ unsigned long flags; unsigned prescale; unsigned long actual; if (!clk_is_programmable(clk)) return -EINVAL; spin_lock_irqsave(&clk_lock, flags); actual = clk->parent->rate_hz; for (prescale = 0; prescale < 7; prescale++) { if (actual && actual <= rate) break; actual >>= 1; } spin_unlock_irqrestore(&clk_lock, flags); return (prescale < 7) ? actual : -ENOENT;}EXPORT_SYMBOL(clk_round_rate);int clk_set_rate(struct clk *clk, unsigned long rate){ unsigned long flags; unsigned prescale; unsigned long actual; if (!clk_is_programmable(clk)) return -EINVAL; if (clk->users) return -EBUSY; spin_lock_irqsave(&clk_lock, flags); actual = clk->parent->rate_hz; for (prescale = 0; prescale < 7; prescale++) { if (actual && actual <= rate) { u32 pckr; pckr = at91_sys_read(AT91_PMC_PCKR(clk->id)); pckr &= AT91_PMC_CSS_PLLB; /* clock selection */ pckr |= prescale << 2; at91_sys_write(AT91_PMC_PCKR(clk->id), pckr); clk->rate_hz = actual; break; } actual >>= 1; } spin_unlock_irqrestore(&clk_lock, flags); return (prescale < 7) ? actual : -ENOENT;}EXPORT_SYMBOL(clk_set_rate);struct clk *clk_get_parent(struct clk *clk)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -