inphi.c 13.5 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
/*
 * Copyright 2018 NXP
 * Copyright 2018 INPHI
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 2. 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.
 *
 * 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.
 *
 * Inphi is a registered trademark of Inphi Corporation
 *
 */

#include <linux/module.h>
#include <linux/phy.h>
#include <linux/mdio.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/of_irq.h>
#include <linux/workqueue.h>
#include <linux/i2c.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/uaccess.h>

#define PHY_ID_IN112525  0x02107440

#define INPHI_S03_DEVICE_ID_MSB 0x2
#define INPHI_S03_DEVICE_ID_LSB 0x3

#define ALL_LANES		4
#define INPHI_POLL_DELAY	2500

#define PHYCTRL_REG1	0x0012
#define PHYCTRL_REG2	0x0014
#define PHYCTRL_REG3	0x0120
#define PHYCTRL_REG4	0x0121
#define PHYCTRL_REG5	0x0180
#define PHYCTRL_REG6	0x0580
#define PHYCTRL_REG7	0x05C4
#define PHYCTRL_REG8	0x01C8
#define PHYCTRL_REG9	0x0521

#define PHYSTAT_REG1	0x0021
#define PHYSTAT_REG2	0x0022
#define PHYSTAT_REG3	0x0123

#define PHYMISC_REG1	0x0025
#define PHYMISC_REG2	0x002c
#define PHYMISC_REG3	0x00b3
#define PHYMISC_REG4	0x0181
#define PHYMISC_REG5	0x019D
#define PHYMISC_REG6	0x0198
#define PHYMISC_REG7	0x0199
#define PHYMISC_REG8	0x0581
#define PHYMISC_REG9	0x0598
#define PHYMISC_REG10	0x059c
#define PHYMISC_REG20	0x01B0
#define PHYMISC_REG21	0x01BC
#define PHYMISC_REG22	0x01C0

#define RX_VCO_CODE_OFFSET	5
#define VCO_CODE		390

int vco_codes[ALL_LANES] = {
	VCO_CODE,
	VCO_CODE,
	VCO_CODE,
	VCO_CODE
};

static void mykmod_work_handler(struct work_struct *w);

static struct workqueue_struct *wq;
static DECLARE_DELAYED_WORK(mykmod_work, mykmod_work_handler);
static unsigned long onesec;
struct phy_device *inphi_phydev;

static int mdio_wr(u32 regnum, u16 val)
{
	regnum = MII_ADDR_C45 | (MDIO_MMD_VEND1 << 16) | (regnum & 0xffff);

	return mdiobus_write(inphi_phydev->mdio.bus, inphi_phydev->mdio.addr,
				regnum, val);
}

static int mdio_rd(u32 regnum)
{
	regnum = MII_ADDR_C45 | (MDIO_MMD_VEND1 << 16) | (regnum & 0xffff);

	return mdiobus_read(inphi_phydev->mdio.bus, inphi_phydev->mdio.addr,
				regnum);
}


int bit_test(int value, int bit_field)
{
	int result;
	int bit_mask = (1 << bit_field);

	result = ((value & bit_mask) == bit_mask);
	return result;
}

int tx_pll_lock_test(int lane)
{
	int i, val, locked = 1;

	if (lane == ALL_LANES) {
		for (i = 0; i < ALL_LANES; i++) {
			val = mdio_rd(i * 0x100 + PHYSTAT_REG3);
			locked = locked & bit_test(val, 15);
		}
	} else {
		val = mdio_rd(lane * 0x100 + PHYSTAT_REG3);
		locked = locked & bit_test(val, 15);
	}

	return locked;
}

void rx_reset_assert(int lane)
{
	int mask, val;

	if (lane == ALL_LANES) {
		val = mdio_rd(PHYMISC_REG2);
		mask = (1 << 15);
		mdio_wr(PHYMISC_REG2, val + mask);
	} else {
		val = mdio_rd(lane * 0x100 + PHYCTRL_REG8);
		mask = (1 << 6);
		mdio_wr(lane * 0x100 + PHYCTRL_REG8, val + mask);
	}
}

void rx_reset_de_assert(int lane)
{
	int mask, val;

	if (lane == ALL_LANES) {
		val = mdio_rd(PHYMISC_REG2);
		mask = 0xffff - (1 << 15);
		mdio_wr(PHYMISC_REG2, val & mask);
	} else {
		val = mdio_rd(lane * 0x100 + PHYCTRL_REG8);
		mask = 0xffff - (1 << 6);
		mdio_wr(lane * 0x100 + PHYCTRL_REG8, val & mask);
	}
}

void rx_powerdown_assert(int lane)
{
	int mask, val;

	val = mdio_rd(lane * 0x100 + PHYCTRL_REG8);
	mask = (1 << 5);
	mdio_wr(lane * 0x100 + PHYCTRL_REG8, val + mask);
}

void rx_powerdown_de_assert(int lane)
{
	int mask, val;

	val = mdio_rd(lane * 0x100 + PHYCTRL_REG8);
	mask = 0xffff - (1 << 5);
	mdio_wr(lane * 0x100 + PHYCTRL_REG8, val & mask);
}

void tx_pll_assert(int lane)
{
	int val, recal;

	if (lane == ALL_LANES) {
		val = mdio_rd(PHYMISC_REG2);
		recal = (1 << 12);
		mdio_wr(PHYMISC_REG2, val | recal);
	} else {
		val = mdio_rd(lane * 0x100 + PHYCTRL_REG4);
		recal = (1 << 15);
		mdio_wr(lane * 0x100 + PHYCTRL_REG4, val | recal);
	}
}

void tx_pll_de_assert(int lane)
{
	int recal, val;

	if (lane == ALL_LANES) {
		val = mdio_rd(PHYMISC_REG2);
		recal = 0xefff;
		mdio_wr(PHYMISC_REG2, val & recal);
	} else {
		val = mdio_rd(lane * 0x100 + PHYCTRL_REG4);
		recal = 0x7fff;
		mdio_wr(lane * 0x100 + PHYCTRL_REG4, val & recal);
	}
}

void tx_core_assert(int lane)
{
	int recal, val, val2, core_reset;

	if (lane == 4) {
		val = mdio_rd(PHYMISC_REG2);
		recal = 1 << 10;
		mdio_wr(PHYMISC_REG2, val | recal);
	} else {
		val2 = mdio_rd(PHYMISC_REG3);
		core_reset = (1 << (lane + 8));
		mdio_wr(PHYMISC_REG3, val2 | core_reset);
	}
}

void lol_disable(int lane)
{
	int val, mask;

	val = mdio_rd(PHYMISC_REG3);
	mask = 1 << (lane + 4);
	mdio_wr(PHYMISC_REG3, val | mask);
}

void tx_core_de_assert(int lane)
{
	int val, recal, val2, core_reset;

	if (lane == ALL_LANES) {
		val = mdio_rd(PHYMISC_REG2);
		recal = 0xffff - (1 << 10);
		mdio_wr(PHYMISC_REG2, val & recal);
	} else {
		val2 = mdio_rd(PHYMISC_REG3);
		core_reset = 0xffff - (1 << (lane + 8));
		mdio_wr(PHYMISC_REG3, val2 & core_reset);
	}
}

void tx_restart(int lane)
{
	tx_core_assert(lane);
	tx_pll_assert(lane);
	tx_pll_de_assert(lane);
	usleep_range(1500, 1600);
	tx_core_de_assert(lane);
}

void disable_lane(int lane)
{
	rx_reset_assert(lane);
	rx_powerdown_assert(lane);
	tx_core_assert(lane);
	lol_disable(lane);
}

void toggle_reset(int lane)
{
	int reg, val, orig;

	if (lane == ALL_LANES) {
		mdio_wr(PHYMISC_REG2, 0x8000);
		udelay(100);
		mdio_wr(PHYMISC_REG2, 0x0000);
	} else {
		reg = lane * 0x100 + PHYCTRL_REG8;
		val = (1 << 6);
		orig = mdio_rd(reg);
		mdio_wr(reg, orig + val);
		udelay(100);
		mdio_wr(reg, orig);
	}
}

int az_complete_test(int lane)
{
	int success = 1, value;

	if (lane == 0 || lane == ALL_LANES) {
		value = mdio_rd(PHYCTRL_REG5);
		success = success & bit_test(value, 2);
	}
	if (lane == 1 || lane == ALL_LANES) {
		value = mdio_rd(PHYCTRL_REG5 + 0x100);
		success = success & bit_test(value, 2);
	}
	if (lane == 2 || lane == ALL_LANES) {
		value = mdio_rd(PHYCTRL_REG5 + 0x200);
		success = success & bit_test(value, 2);
	}
	if (lane == 3 || lane == ALL_LANES) {
		value = mdio_rd(PHYCTRL_REG5 + 0x300);
		success = success & bit_test(value, 2);
	}

	return success;
}

void save_az_offsets(int lane)
{
	int i;

#define AZ_OFFSET_LANE_UPDATE(reg, lane) \
	mdio_wr((reg) + (lane) * 0x100,  \
		(mdio_rd((reg) + (lane) * 0x100) >> 8))

	if (lane == ALL_LANES) {
		for (i = 0; i < ALL_LANES; i++) {
			AZ_OFFSET_LANE_UPDATE(PHYMISC_REG20, i);
			AZ_OFFSET_LANE_UPDATE(PHYMISC_REG20 + 1, i);
			AZ_OFFSET_LANE_UPDATE(PHYMISC_REG20 + 2, i);
			AZ_OFFSET_LANE_UPDATE(PHYMISC_REG20 + 3, i);
			AZ_OFFSET_LANE_UPDATE(PHYMISC_REG21, i);
			AZ_OFFSET_LANE_UPDATE(PHYMISC_REG21 + 1, i);
			AZ_OFFSET_LANE_UPDATE(PHYMISC_REG21 + 2, i);
			AZ_OFFSET_LANE_UPDATE(PHYMISC_REG21 + 3, i);
			AZ_OFFSET_LANE_UPDATE(PHYMISC_REG22, i);
		}
	} else {
		AZ_OFFSET_LANE_UPDATE(PHYMISC_REG20, lane);
		AZ_OFFSET_LANE_UPDATE(PHYMISC_REG20 + 1, lane);
		AZ_OFFSET_LANE_UPDATE(PHYMISC_REG20 + 2, lane);
		AZ_OFFSET_LANE_UPDATE(PHYMISC_REG20 + 3, lane);
		AZ_OFFSET_LANE_UPDATE(PHYMISC_REG21, lane);
		AZ_OFFSET_LANE_UPDATE(PHYMISC_REG21 + 1, lane);
		AZ_OFFSET_LANE_UPDATE(PHYMISC_REG21 + 2, lane);
		AZ_OFFSET_LANE_UPDATE(PHYMISC_REG21 + 3, lane);
		AZ_OFFSET_LANE_UPDATE(PHYMISC_REG22, lane);
	}

	mdio_wr(PHYCTRL_REG7, 0x0001);
}

void save_vco_codes(int lane)
{
	int i;

	if (lane == ALL_LANES) {
		for (i = 0; i < ALL_LANES; i++) {
			vco_codes[i] = mdio_rd(PHYMISC_REG5 + i * 0x100);
			mdio_wr(PHYMISC_REG5 + i * 0x100,
				vco_codes[i] + RX_VCO_CODE_OFFSET);
		}
	} else {
		vco_codes[lane] = mdio_rd(PHYMISC_REG5 + lane * 0x100);
		mdio_wr(PHYMISC_REG5 + lane * 0x100,
			vco_codes[lane] + RX_VCO_CODE_OFFSET);
	}
}

int inphi_lane_recovery(int lane)
{
	int i, value, az_pass;

	switch (lane) {
	case 0:
	case 1:
	case 2:
	case 3:
		rx_reset_assert(lane);
		mdelay(20);
		break;
	case ALL_LANES:
		mdio_wr(PHYMISC_REG2, 0x9C00);
		mdelay(20);
		do {
			value = mdio_rd(PHYMISC_REG2);
			udelay(10);
		} while (!bit_test(value, 4));
		break;
	default:
		dev_err(&inphi_phydev->mdio.dev,
			"Incorrect usage of APIs in %s driver\n",
			inphi_phydev->drv->name);
		break;
	}

	if (lane == ALL_LANES) {
		for (i = 0; i < ALL_LANES; i++)
			mdio_wr(PHYMISC_REG7 + i * 0x100, VCO_CODE);
	} else {
		mdio_wr(PHYMISC_REG7 + lane * 0x100, VCO_CODE);
	}

	if (lane == ALL_LANES)
		for (i = 0; i < ALL_LANES; i++)
			mdio_wr(PHYCTRL_REG5 + i * 0x100, 0x0418);
	else
		mdio_wr(PHYCTRL_REG5 + lane * 0x100, 0x0418);

	mdio_wr(PHYCTRL_REG7,   0x0000);

	rx_reset_de_assert(lane);

	if (lane == ALL_LANES) {
		for (i = 0; i < ALL_LANES; i++) {
			mdio_wr(PHYCTRL_REG5 + i * 0x100, 0x0410);
			mdio_wr(PHYCTRL_REG5 + i * 0x100, 0x0412);
		}
	} else {
		mdio_wr(PHYCTRL_REG5 + lane * 0x100, 0x0410);
		mdio_wr(PHYCTRL_REG5 + lane * 0x100, 0x0412);
	}

	for (i = 0; i < 64; i++) {
		mdelay(100);
		az_pass = az_complete_test(lane);
		if (az_pass) {
			save_az_offsets(lane);
			break;
		}
	}

	if (!az_pass) {
		pr_info("in112525: AZ calibration fail @ lane=%d\n", lane);
		return -1;
	}

	if (lane == ALL_LANES) {
		mdio_wr(PHYMISC_REG8, 0x0002);
		mdio_wr(PHYMISC_REG9, 0x2028);
		mdio_wr(PHYCTRL_REG6, 0x0010);
		usleep_range(1000, 1200);
		mdio_wr(PHYCTRL_REG6, 0x0110);
		mdelay(30);
		mdio_wr(PHYMISC_REG9, 0x3020);
	} else {
		mdio_wr(PHYMISC_REG4 + lane * 0x100, 0x0002);
		mdio_wr(PHYMISC_REG6 + lane * 0x100, 0x2028);
		mdio_wr(PHYCTRL_REG5 + lane * 0x100, 0x0010);
		usleep_range(1000, 1200);
		mdio_wr(PHYCTRL_REG5 + lane * 0x100, 0x0110);
		mdelay(30);
		mdio_wr(PHYMISC_REG6 + lane * 0x100, 0x3020);
	}

	if (lane == ALL_LANES) {
		mdio_wr(PHYMISC_REG2, 0x1C00);
		mdio_wr(PHYMISC_REG2, 0x0C00);
	} else {
		tx_restart(lane);
		mdelay(11);
	}

	if (lane == ALL_LANES) {
		if (bit_test(mdio_rd(PHYMISC_REG2), 6) == 0)
			return -1;
	} else {
		if (tx_pll_lock_test(lane) == 0)
			return -1;
	}

	save_vco_codes(lane);

	if (lane == ALL_LANES) {
		mdio_wr(PHYMISC_REG2, 0x0400);
		mdio_wr(PHYMISC_REG2, 0x0000);
		value = mdio_rd(PHYCTRL_REG1);
		value = value & 0xffbf;
		mdio_wr(PHYCTRL_REG2, value);
	} else {
		tx_core_de_assert(lane);
	}

	if (lane == ALL_LANES) {
		mdio_wr(PHYMISC_REG1, 0x8000);
		mdio_wr(PHYMISC_REG1, 0x0000);
	}
	mdio_rd(PHYMISC_REG1);
	mdio_rd(PHYMISC_REG1);
	usleep_range(1000, 1200);
	mdio_rd(PHYSTAT_REG1);
	mdio_rd(PHYSTAT_REG2);

	return 0;
}

static void mykmod_work_handler(struct work_struct *w)
{
	int all_lanes_lock, lane0_lock, lane1_lock, lane2_lock, lane3_lock;

	lane0_lock = bit_test(mdio_rd(0x123), 15);
	lane1_lock = bit_test(mdio_rd(0x223), 15);
	lane2_lock = bit_test(mdio_rd(0x323), 15);
	lane3_lock = bit_test(mdio_rd(0x423), 15);

	/* check if the chip had any successful lane lock from the previous
	 * stage (e.g. u-boot)
	 */
	all_lanes_lock = lane0_lock | lane1_lock | lane2_lock | lane3_lock;

	if (!all_lanes_lock) {
		/* start fresh */
		inphi_lane_recovery(ALL_LANES);
	} else {
		if (!lane0_lock)
			inphi_lane_recovery(0);
		if (!lane1_lock)
			inphi_lane_recovery(1);
		if (!lane2_lock)
			inphi_lane_recovery(2);
		if (!lane3_lock)
			inphi_lane_recovery(3);
	}

	queue_delayed_work(wq, &mykmod_work, onesec);
}

int inphi_probe(struct phy_device *phydev)
{
	int phy_id = 0, id_lsb = 0, id_msb = 0;

	/* setup the inphi_phydev ptr for mdio_rd/mdio_wr APIs */
	inphi_phydev = phydev;

	/* Read device id from phy registers */
	id_lsb = mdio_rd(INPHI_S03_DEVICE_ID_MSB);
	if (id_lsb < 0)
		return -ENXIO;

	phy_id = id_lsb << 16;

	id_msb = mdio_rd(INPHI_S03_DEVICE_ID_LSB);
	if (id_msb < 0)
		return -ENXIO;

	phy_id |= id_msb;

	/* Make sure the device tree binding matched the driver with the
	 * right device.
	 */
	if (phy_id != phydev->drv->phy_id) {
		dev_err(&phydev->mdio.dev,
			"Error matching phy with %s driver\n",
			phydev->drv->name);
		return -ENODEV;
	}

	/* update the local phydev pointer, used inside all APIs */
	inphi_phydev = phydev;
	onesec = msecs_to_jiffies(INPHI_POLL_DELAY);

	wq = create_singlethread_workqueue("inphi_kmod");
	if (wq) {
		queue_delayed_work(wq, &mykmod_work, onesec);
	} else {
		dev_err(&phydev->mdio.dev,
			"Error creating kernel workqueue for %s driver\n",
			phydev->drv->name);
		return -ENOMEM;
	}

	return 0;
}

static struct phy_driver inphi_driver[] = {
{
	.phy_id		= PHY_ID_IN112525,
	.phy_id_mask	= 0x0ff0fff0,
	.name		= "Inphi 112525_S03",
	.features	= PHY_GBIT_FEATURES,
	.probe		= &inphi_probe,
},
};

module_phy_driver(inphi_driver);

static struct mdio_device_id __maybe_unused inphi_tbl[] = {
	{ PHY_ID_IN112525, 0x0ff0fff0},
	{},
};

MODULE_DEVICE_TABLE(mdio, inphi_tbl);