📄 clock.c
字号:
{ return clk->parent;}EXPORT_SYMBOL(clk_get_parent);int clk_set_parent(struct clk *clk, struct clk *parent){ unsigned long flags; if (clk->users) return -EBUSY; if (!clk_is_primary(parent) || !clk_is_programmable(clk)) return -EINVAL; spin_lock_irqsave(&clk_lock, flags); clk->rate_hz = parent->rate_hz; clk->parent = parent; at91_sys_write(AT91_PMC_PCKR(clk->id), parent->id); spin_unlock_irqrestore(&clk_lock, flags); return 0;}EXPORT_SYMBOL(clk_set_parent);/* establish PCK0..PCK3 parentage and rate */static void init_programmable_clock(struct clk *clk){ struct clk *parent; u32 pckr; pckr = at91_sys_read(AT91_PMC_PCKR(clk->id)); parent = at91_css_to_clk(pckr & AT91_PMC_CSS); clk->parent = parent; clk->rate_hz = parent->rate_hz / (1 << ((pckr & AT91_PMC_PRES) >> 2));}#endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS *//*------------------------------------------------------------------------*/#ifdef CONFIG_DEBUG_FSstatic int at91_clk_show(struct seq_file *s, void *unused){ u32 scsr, pcsr, sr; struct clk *clk; seq_printf(s, "SCSR = %8x\n", scsr = at91_sys_read(AT91_PMC_SCSR)); seq_printf(s, "PCSR = %8x\n", pcsr = at91_sys_read(AT91_PMC_PCSR)); seq_printf(s, "MOR = %8x\n", at91_sys_read(AT91_CKGR_MOR)); seq_printf(s, "MCFR = %8x\n", at91_sys_read(AT91_CKGR_MCFR)); seq_printf(s, "PLLA = %8x\n", at91_sys_read(AT91_CKGR_PLLAR)); seq_printf(s, "PLLB = %8x\n", at91_sys_read(AT91_CKGR_PLLBR)); seq_printf(s, "MCKR = %8x\n", at91_sys_read(AT91_PMC_MCKR)); seq_printf(s, "SR = %8x\n", sr = at91_sys_read(AT91_PMC_SR)); seq_printf(s, "\n"); list_for_each_entry(clk, &clocks, node) { char *state; if (clk->mode == pmc_sys_mode) state = (scsr & clk->pmc_mask) ? "on" : "off"; else if (clk->mode == pmc_periph_mode) state = (pcsr & clk->pmc_mask) ? "on" : "off"; else if (clk->pmc_mask) state = (sr & clk->pmc_mask) ? "on" : "off"; else if (clk == &clk32k || clk == &main_clk) state = "on"; else state = ""; seq_printf(s, "%-10s users=%2d %-3s %9ld Hz %s\n", clk->name, clk->users, state, clk_get_rate(clk), clk->parent ? clk->parent->name : ""); } return 0;}static int at91_clk_open(struct inode *inode, struct file *file){ return single_open(file, at91_clk_show, NULL);}static const struct file_operations at91_clk_operations = { .open = at91_clk_open, .read = seq_read, .llseek = seq_lseek, .release = single_release,};static int __init at91_clk_debugfs_init(void){ /* /sys/kernel/debug/at91_clk */ (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations); return 0;}postcore_initcall(at91_clk_debugfs_init);#endif/*------------------------------------------------------------------------*//* Register a new clock */int __init clk_register(struct clk *clk){ if (clk_is_peripheral(clk)) { clk->parent = &mck; clk->mode = pmc_periph_mode; list_add_tail(&clk->node, &clocks); } else if (clk_is_sys(clk)) { clk->parent = &mck; clk->mode = pmc_sys_mode; list_add_tail(&clk->node, &clocks); }#ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS else if (clk_is_programmable(clk)) { clk->mode = pmc_sys_mode; init_programmable_clock(clk); list_add_tail(&clk->node, &clocks); }#endif return 0;}/*------------------------------------------------------------------------*/static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg){ unsigned mul, div; div = reg & 0xff; mul = (reg >> 16) & 0x7ff; if (div && mul) { freq /= div; freq *= mul + 1; } else freq = 0; return freq;}static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg){ if (pll == &pllb && (reg & AT91_PMC_USB96M)) return freq / 2; else return freq;}static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq){ unsigned i, div = 0, mul = 0, diff = 1 << 30; unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00; /* PLL output max 240 MHz (or 180 MHz per errata) */ if (out_freq > 240000000) goto fail; for (i = 1; i < 256; i++) { int diff1; unsigned input, mul1; /* * PLL input between 1MHz and 32MHz per spec, but lower * frequences seem necessary in some cases so allow 100K. */ input = main_freq / i; if (input < 100000) continue; if (input > 32000000) continue; mul1 = out_freq / input; if (mul1 > 2048) continue; if (mul1 < 2) goto fail; diff1 = out_freq - input * mul1; if (diff1 < 0) diff1 = -diff1; if (diff > diff1) { diff = diff1; div = i; mul = mul1; if (diff == 0) break; } } if (i == 256 && diff > (out_freq >> 5)) goto fail; return ret | ((mul - 1) << 16) | div;fail: return 0;}static struct clk *const standard_pmc_clocks[] __initdata = { /* four primary clocks */ &clk32k, &main_clk, &plla, &pllb, /* PLLB children (USB) */ &udpck, &uhpck, /* MCK */ &mck};int __init at91_clock_init(unsigned long main_clock){ unsigned tmp, freq, mckr; int i; /* * When the bootloader initialized the main oscillator correctly, * there's no problem using the cycle counter. But if it didn't, * or when using oscillator bypass mode, we must be told the speed * of the main clock. */ if (!main_clock) { do { tmp = at91_sys_read(AT91_CKGR_MCFR); } while (!(tmp & AT91_PMC_MAINRDY)); main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16); } main_clk.rate_hz = main_clock; /* report if PLLA is more than mildly overclocked */ plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_sys_read(AT91_CKGR_PLLAR)); if (plla.rate_hz > 209000000) pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000); /* * USB clock init: choose 48 MHz PLLB value, * disable 48MHz clock during usb peripheral suspend. * * REVISIT: assumes MCK doesn't derive from PLLB! */ at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M; pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init); if (cpu_is_at91rm9200()) { uhpck.pmc_mask = AT91RM9200_PMC_UHP; udpck.pmc_mask = AT91RM9200_PMC_UDP; at91_sys_write(AT91_PMC_SCER, AT91RM9200_PMC_MCKUDP); } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() || cpu_is_at91sam9263()) { uhpck.pmc_mask = AT91SAM926x_PMC_UHP; udpck.pmc_mask = AT91SAM926x_PMC_UDP; } at91_sys_write(AT91_CKGR_PLLBR, 0); udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init); uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init); /* * MCK and CPU derive from one of those primary clocks. * For now, assume this parentage won't change. */ mckr = at91_sys_read(AT91_PMC_MCKR); mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS); freq = mck.parent->rate_hz; freq /= (1 << ((mckr & AT91_PMC_PRES) >> 2)); /* prescale */ if (cpu_is_at91rm9200()) mck.rate_hz = freq / (1 + ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */ else mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */ /* Register the PMC's standard clocks */ for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++) list_add_tail(&standard_pmc_clocks[i]->node, &clocks); /* MCK and CPU clock are "always on" */ clk_enable(&mck); printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n", freq / 1000000, (unsigned) mck.rate_hz / 1000000, (unsigned) main_clock / 1000000, ((unsigned) main_clock % 1000000) / 1000); return 0;}/* * Several unused clocks may be active. Turn them off. */static int __init at91_clock_reset(void){ unsigned long pcdr = 0; unsigned long scdr = 0; struct clk *clk; list_for_each_entry(clk, &clocks, node) { if (clk->users > 0) continue; if (clk->mode == pmc_periph_mode) pcdr |= clk->pmc_mask; if (clk->mode == pmc_sys_mode) scdr |= clk->pmc_mask; pr_debug("Clocks: disable unused %s\n", clk->name); } at91_sys_write(AT91_PMC_PCDR, pcdr); at91_sys_write(AT91_PMC_SCDR, scdr); return 0;}late_initcall(at91_clock_reset);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -