cpuclock 7.75 KB
Newer Older
Jeffrey Lee's avatar
Jeffrey Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/*
*Copyright(c)2016, RISC OS Open Ltd
*Allrightsreserved.
*
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * 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.
 *     * Neither the name of RISC OS Open Ltd nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 THE COPYRIGHT HOLDER 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.
*/
#include "swis.h"
#include <stddef.h>

#include "Global/HALEntries.h"
#include "Interface/BCMSupport.h"

#include "DebugLib/DebugLib.h"

#include "cpuclock.h"
#include "BCMSupportHdr.h"

myclkdev_t myclkdev =
{
	.dev.dev.dev.dev.type = HALDeviceType_SysPeri + HALDeviceSysPeri_CPUClk,
	.dev.dev.dev.dev.id = HALDeviceID_CPUClk_BCM283x,
	.dev.dev.dev.dev.location = HALDeviceBus_Sys + HALDeviceSysBus_AHB,
	.dev.dev.dev.dev.version = 2,
	.dev.dev.dev.dev.description = "BCM283x CPU clock controller",
	.dev.dev.dev.dev.Activate = cpuclock_asm_Activate,
	.dev.dev.dev.dev.Deactivate = cpuclock_asm_Deactivate,
	.dev.dev.dev.dev.Reset = cpuclock_asm_Reset,
	.dev.dev.dev.dev.Sleep = cpuclock_asm_Sleep,
	.dev.dev.dev.dev.devicenumber = -1,
	.dev.dev.dev.NumSpeeds = cpuclock_asm_NumSpeeds,
	.dev.dev.dev.Info = cpuclock_asm_Info,
	.dev.dev.dev.Get = cpuclock_asm_Get,
	.dev.dev.dev.Set = cpuclock_asm_Set,
	.dev.GetDieTemperature = cpuclock_asm_GetDieTemperature,
};

static bool init;

static uint32_t speeds[2];

#define CLOCK_CPU 3
#define CLOCK_SCALE 1000000

#define TEMP_CPU 0 /* ??? */

typedef struct
{
	uint32_t bufsize;
	uint32_t code;
} bufheader;

typedef struct
{
	uint32_t tag;
	uint32_t bufsize;
	uint32_t valsize;
} tagheader;

void cpuclock_init(void)
{
	/* Read min, max CPU speeds */
	struct {
		bufheader hdr;
		struct {
			tagheader tag;
			uint32_t data[2];
		} min, max;
		uint32_t end;
	} block = {
		.hdr.bufsize = sizeof(block),
		.hdr.code = 0,
		.min.tag.tag = ARM2VC_Tag_GetMinClockRate,
		.min.tag.bufsize = 8,
		.min.tag.valsize = 4,
		.min.data[0] = CLOCK_CPU,
		.max.tag.tag = ARM2VC_Tag_GetMaxClockRate,
		.max.tag.bufsize = 8,
		.max.tag.valsize = 4,
		.max.data[0] = CLOCK_CPU,
		.end = ARM2VC_Tag_End,
	};
	if (_swix(BCMSupport_SendTempPropertyBuffer,_INR(0,2),&block,&block,0))
	{
		return;
	}
	if ((block.hdr.code != 0x80000000) || (block.min.tag.valsize < 0x80000008) || (block.max.tag.valsize < 0x80000008))
	{
		dprintf(("clk","Failed getting clock speeds\n"));
		return;
	}
	speeds[0] = block.min.data[1];
	speeds[1] = block.max.data[1];
	dprintf(("clk","Min, max clock rates = %u %u\n",speeds[0],speeds[1]));

	/* Fill in the relocation offsets */
	uint32_t *reloc = (uint32_t *) (_kernel_current_stack_chunk() + 1);
	myclkdev.reloc[0] = reloc[0];
	myclkdev.reloc[1] = reloc[1];

	/* Register device */
	init = !_swix(OS_Hardware,_IN(0)|_IN(8),&myclkdev.dev,OSHW_DeviceAdd);
}

_kernel_oserror *cpuclock_shutdown(void)
{
	if (!init)
	{
		return NULL;
	}
	/* Try to deregister device */
	_kernel_oserror *e = _swix(OS_Hardware,_IN(0)|_IN(8),&myclkdev.dev,OSHW_DeviceRemove);
	init = (e != NULL);
	return e;
}

bool cpuclock_Activate(struct device *dev)
{
	(void) dev;
	dprintf(("clk","Activate\n"));
	return true;
}

void cpuclock_Deactivate(struct device *dev)
{
	(void) dev;
	dprintf(("clk","Deactivate\n"));
}

void cpuclock_Reset(struct device *dev)
{
	(void) dev;
	dprintf(("clk","Reset\n"));
}

int32_t cpuclock_Sleep(struct device *dev, int32_t state)
{
	(void) dev;
	(void) state;
	dprintf(("clk","Sleep\n"));
	return 0;
}

uint32_t cpuclock_NumSpeeds(struct cpuclkdevice *cpuclk)
{
	(void) cpuclk;
	dprintf(("clk","NumSpeeds\n"));
	/* The Raspberry Pi 1 only reports one speed by default.
	   This makes our speed switching code redundant, but it's still useful to register the HAL device so that we can report the CPU speed and temperature.
	   If we make sure to report that there's only one speed to the Portable module, it will be smart enough to avoid calling the Set entry when a request is made to switch from fast to slow speeds and vice-versa */
	return (speeds[0] == speeds[1]) ? 1 : 2;
}

uint32_t cpuclock_Info(struct cpuclkdevice *cpuclk, uint32_t idx)
{
	(void) cpuclk;
	dprintf(("clk","Info %u\n",idx));
	return speeds[idx] / CLOCK_SCALE;
}

uint32_t cpuclock_Get(struct cpuclkdevice *cpuclk)
{
	(void) cpuclk;
	dprintf(("clk","Get\n"));
185 186 187 188 189
	/* Avoid getting confused if only one speed is supported but the current speed doesn't match it for some reason */
	if (speeds[0] == speeds[1])
	{
		return 0;
	}
Jeffrey Lee's avatar
Jeffrey Lee committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	struct {
		bufheader hdr;
		tagheader tag;
		uint32_t data[2];
		uint32_t end;
	} block = {
		.hdr.bufsize = sizeof(block),
		.hdr.code = 0,
		.tag.tag = ARM2VC_Tag_GetClockRate,
		.tag.bufsize = 8,
		.tag.valsize = 4,
		.data[0] = CLOCK_CPU,
		.end = ARM2VC_Tag_End,
	};
	/* Enable IRQs for slow mailbox op */
	int irqs = _kernel_irqs_disabled();
	_kernel_irqs_on();
	uint32_t ret = 0;
	if (!_swix(BCMSupport_SendTempPropertyBuffer,_INR(0,2),&block,&block,0))
	{
210
		ret = (block.data[1] > speeds[0]) ? 1 : 0;
Jeffrey Lee's avatar
Jeffrey Lee committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	}
	if (irqs)
	{
		_kernel_irqs_off();
	}
	dprintf(("clk","-> %u\n",ret));
	return ret;
}

int cpuclock_Set(struct cpuclkdevice *cpuclk, uint32_t idx)
{
	(void) cpuclk;
	dprintf(("clk","Set %u\n",idx));
	struct {
		bufheader hdr;
		tagheader tag;
		uint32_t data[3];
		uint32_t end;
	} block = {
		.hdr.bufsize = sizeof(block),
		.hdr.code = 0,
		.tag.tag = ARM2VC_Tag_SetClockRate,
		.tag.bufsize = 12,
		.tag.valsize = 12,
		.data[0] = CLOCK_CPU,
		.data[1] = speeds[idx],
		.data[2] = 0,
		.end = ARM2VC_Tag_End,
	};
	/* Ensure IRQs for slow mailbox op */
	int irqs = _kernel_irqs_disabled();
	_kernel_irqs_on();
	int ret = -1;
	if (!_swix(BCMSupport_SendTempPropertyBuffer,_INR(0,2),&block,&block,0))
	{
		ret = (block.hdr.code == 0x80000000) && (block.tag.valsize >= 0x80000008);
	}
	if (irqs)
	{
		_kernel_irqs_off();
	}
	dprintf(("clk","-> %d\n",ret));
	return ret;
}

unsigned int cpuclock_GetDieTemperature(struct cpuclkdevice *cpuclk)
{
	(void) cpuclk;
	dprintf(("clk","GetDieTemperature\n"));
	struct {
		bufheader hdr;
		tagheader tag;
		uint32_t data[2];
		uint32_t end;
	} block = {
		.hdr.bufsize = sizeof(block),
		.hdr.code = 0,
		.tag.tag = ARM2VC_Tag_GetTemperature,
		.tag.bufsize = 8,
		.tag.valsize = 4,
		.data[0] = TEMP_CPU,
		.end = ARM2VC_Tag_End,
	};
	/* Ensure IRQs for slow mailbox op */
	int irqs = _kernel_irqs_disabled();
	_kernel_irqs_on();
	unsigned int ret = 0;
	if (!_swix(BCMSupport_SendTempPropertyBuffer,_INR(0,2),&block,&block,0))
	{
		if ((block.hdr.code == 0x80000000) && (block.tag.valsize >= 0x80000008))
		{
			/* Convert from thousands of a degree C to tenths of a degree K */
			ret = (block.data[1] + 273150) / 100;
		}
	}
	if (irqs)
	{
		_kernel_irqs_off();
	}
	dprintf(("clk","-> %u\n",ret));
	return ret;
}