dscr.c 16.2 KB
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
/*
 *  Device State Control Registers driver
 *
 *  Copyright (C) 2011 Texas Instruments Incorporated
 *  Author: Mark Salter <msalter@redhat.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 */

/*
 * The Device State Control Registers (DSCR) provide SoC level control over
 * a number of peripherals. Details vary considerably among the various SoC
 * parts. In general, the DSCR block will provide one or more configuration
 * registers often protected by a lock register. One or more key values must
 * be written to a lock register in order to unlock the configuration register.
 * The configuration register may be used to enable (and disable in some
 * cases) SoC pin drivers, peripheral clock sources (internal or pin), etc.
 * In some cases, a configuration register is write once or the individual
 * bits are write once. That is, you may be able to enable a device, but
 * will not be able to disable it.
 *
 * In addition to device configuration, the DSCR block may provide registers
 * which are used to reset SoC peripherals, provide device ID information,
 * provide MAC addresses, and other miscellaneous functions.
 */

#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <asm/soc.h>
#include <asm/dscr.h>

#define MAX_DEVSTATE_IDS   32
#define MAX_DEVCTL_REGS     8
#define MAX_DEVSTAT_REGS    8
#define MAX_LOCKED_REGS     4
#define MAX_SOC_EMACS       2

struct rmii_reset_reg {
	u32 reg;
	u32 mask;
};

/*
 * Some registerd may be locked. In order to write to these
 * registers, the key value must first be written to the lockreg.
 */
struct locked_reg {
	u32 reg;	/* offset from base */
	u32 lockreg;	/* offset from base */
	u32 key;	/* unlock key */
};

/*
 * This describes a contiguous area of like control bits used to enable/disable
 * SoC devices. Each controllable device is given an ID which is used by the
 * individual device drivers to control the device state. These IDs start at
 * zero and are assigned sequentially to the control bitfield ranges described
 * by this structure.
 */
struct devstate_ctl_reg {
	u32 reg;		/* register holding the control bits */
	u8  start_id;		/* start id of this range */
	u8  num_ids;		/* number of devices in this range */
	u8  enable_only;	/* bits are write-once to enable only */
	u8  enable;		/* value used to enable device */
	u8  disable;		/* value used to disable device */
	u8  shift;		/* starting (rightmost) bit in range */
	u8  nbits;		/* number of bits per device */
};


/*
 * This describes a region of status bits indicating the state of
 * various devices. This is used internally to wait for status
 * change completion when enabling/disabling a device. Status is
 * optional and not all device controls will have a corresponding
 * status.
 */
struct devstate_stat_reg {
	u32 reg;		/* register holding the status bits */
	u8  start_id;		/* start id of this range */
	u8  num_ids;		/* number of devices in this range */
	u8  enable;		/* value indicating enabled state */
	u8  disable;		/* value indicating disabled state */
	u8  shift;		/* starting (rightmost) bit in range */
	u8  nbits;		/* number of bits per device */
};

struct devstate_info {
	struct devstate_ctl_reg *ctl;
	struct devstate_stat_reg *stat;
};

/* These are callbacks to SOC-specific code. */
struct dscr_ops {
	void (*init)(struct device_node *node);
};

struct dscr_regs {
	spinlock_t		lock;
	void __iomem		*base;
	u32			kick_reg[2];
	u32			kick_key[2];
	struct locked_reg	locked[MAX_LOCKED_REGS];
	struct devstate_info	devstate_info[MAX_DEVSTATE_IDS];
	struct rmii_reset_reg   rmii_resets[MAX_SOC_EMACS];
	struct devstate_ctl_reg devctl[MAX_DEVCTL_REGS];
	struct devstate_stat_reg devstat[MAX_DEVSTAT_REGS];
};

static struct dscr_regs	dscr;

static struct locked_reg *find_locked_reg(u32 reg)
{
	int i;

	for (i = 0; i < MAX_LOCKED_REGS; i++)
		if (dscr.locked[i].key && reg == dscr.locked[i].reg)
			return &dscr.locked[i];
	return NULL;
}

/*
 * Write to a register with one lock
 */
static void dscr_write_locked1(u32 reg, u32 val,
			       u32 lock, u32 key)
{
	void __iomem *reg_addr = dscr.base + reg;
	void __iomem *lock_addr = dscr.base + lock;

	/*
	 * For some registers, the lock is relocked after a short number
	 * of cycles. We have to put the lock write and register write in
	 * the same fetch packet to meet this timing. The .align ensures
	 * the two stw instructions are in the same fetch packet.
	 */
	asm volatile ("b	.s2	0f\n"
		      "nop	5\n"
		      "    .align 5\n"
		      "0:\n"
		      "stw	.D1T2	%3,*%2\n"
		      "stw	.D1T2	%1,*%0\n"
		      :
		      : "a"(reg_addr), "b"(val), "a"(lock_addr), "b"(key)
		);

	/* in case the hw doesn't reset the lock */
	soc_writel(0, lock_addr);
}

/*
 * Write to a register protected by two lock registers
 */
static void dscr_write_locked2(u32 reg, u32 val,
			       u32 lock0, u32 key0,
			       u32 lock1, u32 key1)
{
	soc_writel(key0, dscr.base + lock0);
	soc_writel(key1, dscr.base + lock1);
	soc_writel(val, dscr.base + reg);
	soc_writel(0, dscr.base + lock0);
	soc_writel(0, dscr.base + lock1);
}

static void dscr_write(u32 reg, u32 val)
{
	struct locked_reg *lock;

	lock = find_locked_reg(reg);
	if (lock)
		dscr_write_locked1(reg, val, lock->lockreg, lock->key);
	else if (dscr.kick_key[0])
		dscr_write_locked2(reg, val, dscr.kick_reg[0], dscr.kick_key[0],
				   dscr.kick_reg[1], dscr.kick_key[1]);
	else
		soc_writel(val, dscr.base + reg);
}


/*
 * Drivers can use this interface to enable/disable SoC IP blocks.
 */
void dscr_set_devstate(int id, enum dscr_devstate_t state)
{
	struct devstate_ctl_reg *ctl;
	struct devstate_stat_reg *stat;
	struct devstate_info *info;
	u32 ctl_val, val;
	int ctl_shift, ctl_mask;
	unsigned long flags;

	if (!dscr.base)
		return;

	if (id < 0 || id >= MAX_DEVSTATE_IDS)
		return;

	info = &dscr.devstate_info[id];
	ctl = info->ctl;
	stat = info->stat;

	if (ctl == NULL)
		return;

	ctl_shift = ctl->shift + ctl->nbits * (id - ctl->start_id);
	ctl_mask = ((1 << ctl->nbits) - 1) << ctl_shift;

	switch (state) {
	case DSCR_DEVSTATE_ENABLED:
		ctl_val = ctl->enable << ctl_shift;
		break;
	case DSCR_DEVSTATE_DISABLED:
		if (ctl->enable_only)
			return;
		ctl_val = ctl->disable << ctl_shift;
		break;
	default:
		return;
	}

	spin_lock_irqsave(&dscr.lock, flags);

	val = soc_readl(dscr.base + ctl->reg);
	val &= ~ctl_mask;
	val |= ctl_val;

	dscr_write(ctl->reg, val);

	spin_unlock_irqrestore(&dscr.lock, flags);

	if (!stat)
		return;

	ctl_shift = stat->shift + stat->nbits * (id - stat->start_id);

	if (state == DSCR_DEVSTATE_ENABLED)
		ctl_val = stat->enable;
	else
		ctl_val = stat->disable;

	do {
		val = soc_readl(dscr.base + stat->reg);
		val >>= ctl_shift;
		val &= ((1 << stat->nbits) - 1);
	} while (val != ctl_val);
}
EXPORT_SYMBOL(dscr_set_devstate);

/*
 * Drivers can use this to reset RMII module.
 */
void dscr_rmii_reset(int id, int assert)
{
	struct rmii_reset_reg *r;
	unsigned long flags;
	u32 val;

	if (id < 0 || id >= MAX_SOC_EMACS)
		return;

	r = &dscr.rmii_resets[id];
	if (r->mask == 0)
		return;

	spin_lock_irqsave(&dscr.lock, flags);

	val = soc_readl(dscr.base + r->reg);
	if (assert)
		dscr_write(r->reg, val | r->mask);
	else
		dscr_write(r->reg, val & ~(r->mask));

	spin_unlock_irqrestore(&dscr.lock, flags);
}
EXPORT_SYMBOL(dscr_rmii_reset);

static void __init dscr_parse_devstat(struct device_node *node,
				      void __iomem *base)
{
	u32 val;
	int err;

	err = of_property_read_u32_array(node, "ti,dscr-devstat", &val, 1);
	if (!err)
		c6x_devstat = soc_readl(base + val);
	printk(KERN_INFO "DEVSTAT: %08x\n", c6x_devstat);
}

static void __init dscr_parse_silicon_rev(struct device_node *node,
					 void __iomem *base)
{
	u32 vals[3];
	int err;

	err = of_property_read_u32_array(node, "ti,dscr-silicon-rev", vals, 3);
	if (!err) {
		c6x_silicon_rev = soc_readl(base + vals[0]);
		c6x_silicon_rev >>= vals[1];
		c6x_silicon_rev &= vals[2];
	}
}

/*
 * Some SoCs will have a pair of fuse registers which hold
 * an ethernet MAC address. The "ti,dscr-mac-fuse-regs"
 * property is a mapping from fuse register bytes to MAC
 * address bytes. The expected format is:
 *
 *	ti,dscr-mac-fuse-regs = <reg0 b3 b2 b1 b0
 *				 reg1 b3 b2 b1 b0>
 *
 * reg0 and reg1 are the offsets of the two fuse registers.
 * b3-b0 positionally represent bytes within the fuse register.
 * b3 is the most significant byte and b0 is the least.
 * Allowable values for b3-b0 are:
 *
 *	  0 = fuse register byte not used in MAC address
 *      1-6 = index+1 into c6x_fuse_mac[]
 */
static void __init dscr_parse_mac_fuse(struct device_node *node,
				       void __iomem *base)
{
	u32 vals[10], fuse;
	int f, i, j, err;

	err = of_property_read_u32_array(node, "ti,dscr-mac-fuse-regs",
					 vals, 10);
	if (err)
		return;

	for (f = 0; f < 2; f++) {
		fuse = soc_readl(base + vals[f * 5]);
		for (j = (f * 5) + 1, i = 24; i >= 0; i -= 8, j++)
			if (vals[j] && vals[j] <= 6)
				c6x_fuse_mac[vals[j] - 1] = fuse >> i;
	}
}

static void __init dscr_parse_rmii_resets(struct device_node *node,
					  void __iomem *base)
{
	const __be32 *p;
	int i, size;

	/* look for RMII reset registers */
	p = of_get_property(node, "ti,dscr-rmii-resets", &size);
	if (p) {
		/* parse all the reg/mask pairs we can handle */
		size /= (sizeof(*p) * 2);
		if (size > MAX_SOC_EMACS)
			size = MAX_SOC_EMACS;

		for (i = 0; i < size; i++) {
			dscr.rmii_resets[i].reg = be32_to_cpup(p++);
			dscr.rmii_resets[i].mask = be32_to_cpup(p++);
		}
	}
}


static void __init dscr_parse_privperm(struct device_node *node,
				       void __iomem *base)
{
	u32 vals[2];
	int err;

	err = of_property_read_u32_array(node, "ti,dscr-privperm", vals, 2);
	if (err)
		return;
	dscr_write(vals[0], vals[1]);
}

/*
 * SoCs may have "locked" DSCR registers which can only be written
 * to only after writing a key value to a lock registers. These
 * regisers can be described with the "ti,dscr-locked-regs" property.
 * This property provides a list of register descriptions with each
 * description consisting of three values.
 *
 *	ti,dscr-locked-regs = <reg0 lockreg0 key0
 *                               ...
 *                             regN lockregN keyN>;
 *
 * reg is the offset of the locked register
 * lockreg is the offset of the lock register
 * key is the unlock key written to lockreg
 *
 */
static void __init dscr_parse_locked_regs(struct device_node *node,
					  void __iomem *base)
{
	struct locked_reg *r;
	const __be32 *p;
	int i, size;

	p = of_get_property(node, "ti,dscr-locked-regs", &size);
	if (p) {
		/* parse all the register descriptions we can handle */
		size /= (sizeof(*p) * 3);
		if (size > MAX_LOCKED_REGS)
			size = MAX_LOCKED_REGS;

		for (i = 0; i < size; i++) {
			r = &dscr.locked[i];

			r->reg = be32_to_cpup(p++);
			r->lockreg = be32_to_cpup(p++);
			r->key = be32_to_cpup(p++);
		}
	}
}

/*
 * SoCs may have DSCR registers which are only write enabled after
 * writing specific key values to two registers. The two key registers
 * and the key values can be parsed from a "ti,dscr-kick-regs"
 * propety with the following layout:
 *
 *	ti,dscr-kick-regs = <kickreg0 key0 kickreg1 key1>
 *
 * kickreg is the offset of the "kick" register
 * key is the value which unlocks writing for protected regs
 */
static void __init dscr_parse_kick_regs(struct device_node *node,
					void __iomem *base)
{
	u32 vals[4];
	int err;

	err = of_property_read_u32_array(node, "ti,dscr-kick-regs", vals, 4);
	if (!err) {
		dscr.kick_reg[0] = vals[0];
		dscr.kick_key[0] = vals[1];
		dscr.kick_reg[1] = vals[2];
		dscr.kick_key[1] = vals[3];
	}
}


/*
 * SoCs may provide controls to enable/disable individual IP blocks. These
 * controls in the DSCR usually control pin drivers but also may control
 * clocking and or resets. The device tree is used to describe the bitfields
 * in registers used to control device state. The number of bits and their
 * values may vary even within the same register.
 *
 * The layout of these bitfields is described by the ti,dscr-devstate-ctl-regs
 * property. This property is a list where each element describes a contiguous
 * range of control fields with like properties. Each element of the list
 * consists of 7 cells with the following values:
 *
 *   start_id num_ids reg enable disable start_bit nbits
 *
 * start_id is device id for the first device control in the range
 * num_ids is the number of device controls in the range
 * reg is the offset of the register holding the control bits
 * enable is the value to enable a device
 * disable is the value to disable a device (0xffffffff if cannot disable)
 * start_bit is the bit number of the first bit in the range
 * nbits is the number of bits per device control
 */
static void __init dscr_parse_devstate_ctl_regs(struct device_node *node,
						void __iomem *base)
{
	struct devstate_ctl_reg *r;
	const __be32 *p;
	int i, j, size;

	p = of_get_property(node, "ti,dscr-devstate-ctl-regs", &size);
	if (p) {
		/* parse all the ranges we can handle */
		size /= (sizeof(*p) * 7);
		if (size > MAX_DEVCTL_REGS)
			size = MAX_DEVCTL_REGS;

		for (i = 0; i < size; i++) {
			r = &dscr.devctl[i];

			r->start_id = be32_to_cpup(p++);
			r->num_ids = be32_to_cpup(p++);
			r->reg = be32_to_cpup(p++);
			r->enable = be32_to_cpup(p++);
			r->disable = be32_to_cpup(p++);
			if (r->disable == 0xffffffff)
				r->enable_only = 1;
			r->shift = be32_to_cpup(p++);
			r->nbits = be32_to_cpup(p++);

			for (j = r->start_id;
			     j < (r->start_id + r->num_ids);
			     j++)
				dscr.devstate_info[j].ctl = r;
		}
	}
}

/*
 * SoCs may provide status registers indicating the state (enabled/disabled) of
 * devices on the SoC. The device tree is used to describe the bitfields in
 * registers used to provide device status. The number of bits and their
 * values used to provide status may vary even within the same register.
 *
 * The layout of these bitfields is described by the ti,dscr-devstate-stat-regs
 * property. This property is a list where each element describes a contiguous
 * range of status fields with like properties. Each element of the list
 * consists of 7 cells with the following values:
 *
 *   start_id num_ids reg enable disable start_bit nbits
 *
 * start_id is device id for the first device status in the range
 * num_ids is the number of devices covered by the range
 * reg is the offset of the register holding the status bits
 * enable is the value indicating device is enabled
 * disable is the value indicating device is disabled
 * start_bit is the bit number of the first bit in the range
 * nbits is the number of bits per device status
 */
static void __init dscr_parse_devstate_stat_regs(struct device_node *node,
						 void __iomem *base)
{
	struct devstate_stat_reg *r;
	const __be32 *p;
	int i, j, size;

	p = of_get_property(node, "ti,dscr-devstate-stat-regs", &size);
	if (p) {
		/* parse all the ranges we can handle */
		size /= (sizeof(*p) * 7);
		if (size > MAX_DEVSTAT_REGS)
			size = MAX_DEVSTAT_REGS;

		for (i = 0; i < size; i++) {
			r = &dscr.devstat[i];

			r->start_id = be32_to_cpup(p++);
			r->num_ids = be32_to_cpup(p++);
			r->reg = be32_to_cpup(p++);
			r->enable = be32_to_cpup(p++);
			r->disable = be32_to_cpup(p++);
			r->shift = be32_to_cpup(p++);
			r->nbits = be32_to_cpup(p++);

			for (j = r->start_id;
			     j < (r->start_id + r->num_ids);
			     j++)
				dscr.devstate_info[j].stat = r;
		}
	}
}

static struct of_device_id dscr_ids[] __initdata = {
	{ .compatible = "ti,c64x+dscr" },
	{}
};

/*
 * Probe for DSCR area.
 *
 * This has to be done early on in case timer or interrupt controller
 * needs something. e.g. On C6455 SoC, timer must be enabled through
 * DSCR before it is functional.
 */
void __init dscr_probe(void)
{
	struct device_node *node;
	void __iomem *base;

	spin_lock_init(&dscr.lock);

	node = of_find_matching_node(NULL, dscr_ids);
	if (!node)
		return;

	base = of_iomap(node, 0);
	if (!base) {
		of_node_put(node);
		return;
	}

	dscr.base = base;

	dscr_parse_devstat(node, base);
	dscr_parse_silicon_rev(node, base);
	dscr_parse_mac_fuse(node, base);
	dscr_parse_rmii_resets(node, base);
	dscr_parse_locked_regs(node, base);
	dscr_parse_kick_regs(node, base);
	dscr_parse_devstate_ctl_regs(node, base);
	dscr_parse_devstate_stat_regs(node, base);
	dscr_parse_privperm(node, base);
}