2013-01-07 20:19:19 +02:00
|
|
|
/*
|
|
|
|
* libubb/mmcclk.c - Calculate MMC bus clock speed
|
|
|
|
*
|
|
|
|
* Written 2013 by Werner Almesberger
|
|
|
|
* Copyright 2013 Werner Almesberger
|
|
|
|
*
|
|
|
|
* 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 <ubb/regs4740.h>
|
|
|
|
#include <ubb/mmcclk.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define MSCCDR_MAX 32
|
|
|
|
#define CLKRT_MAX 8
|
|
|
|
|
2013-01-15 23:01:27 +02:00
|
|
|
/*
|
|
|
|
* Nominally, the limit is 20 MHz. A clock can be obtained until 84 MHz,
|
|
|
|
* possibly even higher. Beyond 56 MHz, the MMC controller in some Bens
|
|
|
|
* has been observed to fail to complete the operation when attempting
|
|
|
|
* data transmission.
|
|
|
|
*
|
|
|
|
* Some Bens still work fine at 84 MHz.
|
|
|
|
*/
|
|
|
|
|
2013-01-29 01:49:17 +02:00
|
|
|
#define BUS_SAFE_MHZ 56 /* always works */
|
|
|
|
#define BUS_UNSAFE_MHZ 84 /* some devices don't like this */
|
2013-01-07 20:19:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int calculate_clock(struct mmcclk *dsc)
|
|
|
|
{
|
|
|
|
dsc->bus_clk_hz = dsc->sys_clk_hz/(dsc->clkdiv+1.0)/(1 << dsc->clkrt);
|
2013-01-29 01:49:17 +02:00
|
|
|
if (dsc->flags & MMCCLK_FLAG_ALL)
|
|
|
|
return 1;
|
|
|
|
if ((dsc->flags & MMCCLK_FLAG_UNSAFE) &&
|
|
|
|
dsc->bus_clk_hz <= BUS_UNSAFE_MHZ*1000000)
|
|
|
|
return 1;
|
|
|
|
return dsc->bus_clk_hz <= BUS_SAFE_MHZ*1000000;
|
2013-01-07 20:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-29 01:49:17 +02:00
|
|
|
void mmcclk_first(struct mmcclk *dsc, int sys_clk_hz, unsigned flags)
|
2013-01-07 20:19:19 +02:00
|
|
|
{
|
2013-01-07 21:16:31 +02:00
|
|
|
if (sys_clk_hz)
|
|
|
|
dsc->sys_clk_hz = sys_clk_hz;
|
|
|
|
else
|
|
|
|
dsc->sys_clk_hz =
|
|
|
|
(CPCCR >> 21) & 1 ? BEN_PLL_CLK_HZ : BEN_PLL_CLK_HZ/2;
|
2013-01-29 01:49:17 +02:00
|
|
|
dsc->flags = flags;
|
2013-01-07 20:19:19 +02:00
|
|
|
dsc->clkdiv = dsc->clkrt = 0;
|
|
|
|
if (calculate_clock(dsc))
|
|
|
|
return;
|
|
|
|
mmcclk_next(dsc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int mmcclk_next(struct mmcclk *dsc)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
if (++dsc->clkdiv == MSCCDR_MAX) {
|
|
|
|
dsc->clkdiv = 0;
|
|
|
|
if (++dsc->clkrt == CLKRT_MAX)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (calculate_clock(dsc))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void mmcclk_start(struct mmcclk *dsc)
|
|
|
|
{
|
|
|
|
MSCCDR = dsc->clkdiv; /* set controller clock */
|
|
|
|
CLKGR &= ~(1 << 7); /* enable MSC clock */
|
2013-01-15 19:07:31 +02:00
|
|
|
|
2013-01-20 22:34:17 +02:00
|
|
|
MSC_STRPCL = MSC_STRPCRL_RESET; /* reset the MSC */
|
|
|
|
while (MSC_STAT & MSC_STAT_IS_RESETTING);
|
|
|
|
/* wait until reset finishes */
|
2013-01-15 19:07:31 +02:00
|
|
|
|
2013-01-07 20:19:19 +02:00
|
|
|
MSC_CLKRT = dsc->clkrt; /* set bus clock */
|
2013-01-20 22:34:17 +02:00
|
|
|
MSC_STRPCL = MSC_STRPCRL_START_CLOCK;
|
2013-01-07 20:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void mmcclk_stop(void)
|
|
|
|
{
|
2013-01-20 22:34:17 +02:00
|
|
|
MSC_STRPCL = MSC_STRPCRL_STOP_CLOCK;
|
2013-01-07 20:19:19 +02:00
|
|
|
}
|