r8152.c 20.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright 2018 Google Inc.
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but without any warranty; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <usb/usb.h>

#include "base/init_funcs.h"
#include "drivers/net/mii.h"
#include "drivers/net/r8152.h"
#include "drivers/net/usb_eth.h"
21
#include "net/net.h"
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

static R8152Dev r8152_dev;

/*
 * The lower-level utilities
 */

static int read_registers(usbdev_t *dev, uint16_t value, uint16_t index,
			  uint16_t size, void *data)
{
	dev_req_t dev_req;
	dev_req.bmRequestType = Rtl8152ReqtRead;
	dev_req.bRequest = Rtl8152ReqGetRegs;
	dev_req.wValue = value;
	dev_req.wIndex = index;
	dev_req.wLength = size;

	return (dev->controller->control(dev, IN, sizeof(dev_req), &dev_req,
					 size, (uint8_t *)data)
		< 0);
}

static int write_registers(usbdev_t *dev, uint16_t value, uint16_t index,
			   uint16_t size, void *data)
{
	dev_req_t dev_req;
	dev_req.bmRequestType = Rtl8152ReqtWrite;
	dev_req.bRequest = Rtl8152ReqSetRegs;
	dev_req.wValue = value;
	dev_req.wIndex = index;
	dev_req.wLength = size;

	return (dev->controller->control(dev, OUT, sizeof(dev_req), &dev_req,
					 size, (uint8_t *)data)
		< 0);
}

static int ocp_read(usbdev_t *dev, uint16_t index, uint16_t size, void *data,
		    uint16_t type)
{
	uint16_t limit = 64;
	while (size) {
		if (size < limit)
			limit = size;

		if (read_registers(dev, index, type, limit, data))
			return 1;

		index += limit;
		data += limit;
		size -= limit;
	}
	return 0;
}

static int ocp_write(usbdev_t *dev, uint16_t index, uint16_t byteen,
		     uint16_t size, void *data, uint16_t type)
{
	uint16_t limit = 512;
	uint16_t byteen_start, byteen_end;

	byteen_start = byteen & ByteEnStartMask;
	byteen_end = byteen & ByteEnEndMask;

	if (write_registers(dev, index,
			    type | byteen_start | (byteen_start << 4), 4, data))
		return 1;

	index += 4;
	data += 4;
	size -= 4;

	if (size) {
		size -= 4;
		while (size) {
			if (size < limit)
				limit = size;

			if (write_registers(dev, index, type | ByteEnDword,
					    limit, data))
				return 1;

			index += limit;
			data += limit;
			size -= limit;
		}
		if (write_registers(dev, index,
				    type | byteen_end | (byteen_end >> 4), 4,
				    data))
			return 1;
	}
	return 0;
}

static int ocp_read_dword(usbdev_t *dev, uint16_t type, uint16_t index,
			  uint32_t *val)
{
	if (ocp_read(dev, index, sizeof(*val), val, type))
		return 1;
	*val = le32toh(*val);
	return 0;
}

static int ocp_write_dword(usbdev_t *dev, uint16_t type, uint16_t index,
			   uint32_t val)
{
	val = htole32(val);
	return ocp_write(dev, index, ByteEnDword, sizeof(val), &val, type);
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
static int ocp_dword_clrsetbits(usbdev_t *dev, uint16_t type, uint16_t index,
				uint32_t clr_mask, uint32_t set_mask)
{
	uint32_t data;
	if (ocp_read_dword(dev, type, index, &data))
		return 1;
	data = (data & ~clr_mask) | set_mask;
	return ocp_write_dword(dev, type, index, data);
}

static int ocp_dword_setbits(usbdev_t *dev, uint16_t type, uint16_t index,
			     uint32_t set_mask)
{
	return ocp_dword_clrsetbits(dev, type, index, 0, set_mask);
}

static int ocp_dword_clearbits(usbdev_t *dev, uint16_t type, uint16_t index,
			       uint32_t clr_mask)
{
	return ocp_dword_clrsetbits(dev, type, index, clr_mask, 0);
}

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
static int ocp_read_word(usbdev_t *dev, uint16_t type, uint16_t index,
			 uint16_t *val)
{
	uint32_t data;
	uint8_t shift = index & 2;

	index = ALIGN_DOWN(index, 4);
	if (ocp_read(dev, index, sizeof(data), &data,
		     type | (ByteEnWord << shift)))
		return 1;
	data = le32toh(data);
	data >>= (shift * 8);
	*val = data & 0xffff;

	return 0;
}

static int ocp_write_word(usbdev_t *dev, uint16_t type, uint16_t index,
			  uint16_t val)
{
	uint32_t tmp = val;
	uint8_t shift = index & 2;

	tmp <<= (shift * 8);
	index = ALIGN_DOWN(index, 4);
	tmp = htole32(tmp);

	return ocp_write(dev, index, ByteEnWord << shift, sizeof(tmp), &tmp,
			 type);
}

static int ocp_word_clrsetbits(usbdev_t *dev, uint16_t type, uint16_t index,
			       uint16_t clr_mask, uint16_t set_mask)
{
	uint16_t data;
	if (ocp_read_word(dev, type, index, &data))
		return 1;
	data = (data & ~clr_mask) | set_mask;
	return ocp_write_word(dev, type, index, data);
}

static int ocp_word_setbits(usbdev_t *dev, uint16_t type, uint16_t index,
			    uint16_t set_mask)
{
	return ocp_word_clrsetbits(dev, type, index, 0, set_mask);
}

static int ocp_word_clearbits(usbdev_t *dev, uint16_t type, uint16_t index,
			      uint16_t clr_mask)
{
	return ocp_word_clrsetbits(dev, type, index, clr_mask, 0);
}

static int ocp_read_byte(usbdev_t *dev, uint16_t type, uint16_t index,
			 uint8_t *val)
{
	uint32_t data;
	uint8_t shift = index & 3;

	index = ALIGN_DOWN(index, 4);
	if (ocp_read(dev, index, sizeof(data), &data, type))
		return 1;
	data = le32toh(data);
	data >>= (shift * 8);
	*val = data & 0xff;

	return 0;
}

static int ocp_write_byte(usbdev_t *dev, uint16_t type, uint16_t index,
			  uint8_t val)
{
	uint32_t tmp = val;
	uint8_t shift = index & 3;

	tmp <<= (shift * 8);
	index = ALIGN_DOWN(index, 4);
	tmp = htole32(tmp);

	return ocp_write(dev, index, ByteEnByte << shift, sizeof(tmp), &tmp,
			 type);
}

static int ocp_byte_clrsetbits(usbdev_t *dev, uint16_t type, uint16_t index,
			       uint8_t clr_mask, uint8_t set_mask)
{
	uint8_t data;
	if (ocp_read_byte(dev, type, index, &data))
		return 1;
	data = (data & ~clr_mask) | set_mask;
	return ocp_write_byte(dev, type, index, data);
}

static int ocp_byte_setbits(usbdev_t *dev, uint16_t type, uint16_t index,
			    uint8_t set_mask)
{
	return ocp_byte_clrsetbits(dev, type, index, 0, set_mask);
}

static int ocp_byte_clearbits(usbdev_t *dev, uint16_t type, uint16_t index,
			      uint8_t clr_mask)
{
	return ocp_byte_clrsetbits(dev, type, index, clr_mask, 0);
}

static int ocp_reg_read(usbdev_t *dev, uint16_t addr, uint16_t *val)
{
	uint16_t ocp_base, ocp_index;

	ocp_base = addr & 0xf000;
	if (ocp_base != r8152_dev.ocp_base) {
		if (ocp_write_word(dev, McuTypePla, PlaOcpGphyBase, ocp_base))
			return 1;
		r8152_dev.ocp_base = ocp_base;
	}

	ocp_index = (addr & 0x0fff) | 0xb000;
	return ocp_read_word(dev, McuTypePla, ocp_index, val);
}

static int ocp_reg_write(usbdev_t *dev, uint16_t addr, uint16_t val)
{
	uint16_t ocp_base, ocp_index;

	ocp_base = addr & 0xf000;
	if (ocp_base != r8152_dev.ocp_base) {
		if (ocp_write_word(dev, McuTypePla, PlaOcpGphyBase, ocp_base))
			return 1;
		r8152_dev.ocp_base = ocp_base;
	}

	ocp_index = (addr & 0x0fff) | 0xb000;
	return ocp_write_word(dev, McuTypePla, ocp_index, val);
}

static int ocp_reg_clrsetbits(usbdev_t *dev, uint16_t addr, uint16_t clr_mask,
			      uint16_t set_mask)
{
	uint16_t data;
	if (ocp_reg_read(dev, addr, &data))
		return 1;
	data = (data & ~clr_mask) | set_mask;
	return ocp_reg_write(dev, addr, data);
}

static int ocp_reg_setbits(usbdev_t *dev, uint16_t addr, uint16_t set_mask)
{
	return ocp_reg_clrsetbits(dev, addr, 0, set_mask);
}

static int ocp_reg_clearbits(usbdev_t *dev, uint16_t addr, uint16_t clr_mask)
{
	return ocp_reg_clrsetbits(dev, addr, clr_mask, 0);
}

static int get_version(usbdev_t *dev, uint8_t *version)
{
	uint32_t ocp_data;

	if (ocp_read_dword(dev, McuTypePla, PlaTcr0, &ocp_data)) {
		printf("Failed to read version.\n");
		return 1;
	}

	ocp_data = (ocp_data >> 16) & VersionMask;

	switch (ocp_data) {
	case 0x4c00:
		*version = RtlVersion01;
		break;
	case 0x4c10:
		*version = RtlVersion02;
		break;
	case 0x5c00:
		*version = RtlVersion03;
		break;
	case 0x5c10:
		*version = RtlVersion04;
		break;
	case 0x5c20:
		*version = RtlVersion05;
		break;
	case 0x5c30:
		*version = RtlVersion06;
		break;
	case 0x4800:
		*version = RtlVersion07;
		break;
	case 0x6000:
		*version = RtlVersion08;
		break;
	case 0x6010:
		*version = RtlVersion09;
		break;
	default:
		*version = RtlVersionUnknown;
		printf("Unknown version %04x\n", ocp_data);
		return 1;
	}
	printf("Version %d (ocp_data = %04x)\n", *version, ocp_data);
	return 0;
}

static int r8152_mdio_read(usbdev_t *dev, uint8_t loc, uint16_t *val)
{
	return ocp_reg_read(dev, OcpBaseMii + loc * 2, val);
}

static int r8152_mdio_write(usbdev_t *dev, uint8_t loc, uint16_t val)
{
	return ocp_reg_write(dev, OcpBaseMii + loc * 2, val);
}

static int sram_write(usbdev_t *dev, uint16_t addr, uint16_t data)
{
	if (ocp_reg_write(dev, OcpSramAddr, addr))
		return 1;
	if (ocp_reg_write(dev, OcpSramData, data))
		return 1;
	return 0;
}

static int r8153_wait_autoload_done(usbdev_t *dev)
{
	int i;
	uint16_t data;

	for (i = 0; i < 500; i++) {
		if (ocp_read_word(dev, McuTypePla, PlaBootCtrl, &data))
			return 1;
		if (data & AutoloadDone)
			return 0;
		mdelay(20);
	}

	return 1;
}

static int r8153_wait_for_phy_status(usbdev_t *dev, uint8_t desired)
{
	int i;
	uint16_t val;

	for (i = 0; i < 500; i++) {
		if (ocp_reg_read(dev, OcpPhyStatus, &val))
			return 1;
		val &= 0x07;
		if (desired) {
			if (val == desired)
				return 0;
		} else if (val == PhyStatLanOn || val == PhyStatPwrdn
			   || val == PhyStatExtInit) {
			return 0;
		}
		mdelay(20);
	}

	return 1;
}

static int r8153_power_cut_disable(usbdev_t *dev)
{
	if (ocp_word_clearbits(dev, McuTypeUsb, UsbPowerCut, PwrEn | Phase2En))
		return 1;

	if (ocp_word_clearbits(dev, McuTypeUsb, UsbMisc0, PcutStatus))
		return 1;

	return 0;
}

static int rtl_tally_reset(usbdev_t *dev)
{
	return ocp_word_setbits(dev, McuTypePla, PlaRsttally, TallyReset);
}

static int r8153_eee_disable(usbdev_t *dev)
{
	if (ocp_word_clearbits(dev, McuTypePla, PlaEeeCr, EeeRxEn | EeeTxEn))
		return 1;

	if (ocp_reg_clearbits(dev, OcpEeeCfg, Eee10En))
		return 1;

	return 0;
}

static int r8153_hw_phy_cfg(usbdev_t *dev)
{
	if (r8153_eee_disable(dev))
		return 1;

	if (ocp_reg_write(dev, OcpEeeAdv, 0))
		return 1;

	if (r8152_dev.version == RtlVersion03) {
		if (ocp_reg_clearbits(dev, OcpEeeCfg, CtapShortEn))
			return 1;
	}

	if (ocp_reg_setbits(dev, OcpPowerCfg, EeeClkdivEn))
		return 1;

	if (ocp_reg_setbits(dev, OcpDownSpeed, En10mBgoff))
		return 1;

	if (ocp_reg_setbits(dev, OcpPowerCfg, En10mPlloff))
		return 1;

	if (sram_write(dev, SramImpedance, 0x0b13))
		return 1;

	if (ocp_word_setbits(dev, McuTypePla, PlaPhyPwr, PfmPwmSwitch))
		return 1;

	if (sram_write(dev, SramLpfCfg, 0xf70f))
		return 1;

	if (sram_write(dev, Sram10mAmp1, 0x00af))
		return 1;
	if (sram_write(dev, Sram10mAmp2, 0x0208))
		return 1;

	return 0;
}

static int r8153_set_rx_mode(usbdev_t *dev)
{
	uint32_t tmp[2] = { 0xffffffff, 0xffffffff };

	if (ocp_write(dev, PlaMar, ByteEnDword, sizeof(tmp), tmp, McuTypePla))
		return 1;

487
	if (ocp_dword_setbits(dev, McuTypePla, PlaRcr, RcrAb | RcrApm | RcrAm))
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 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
		return 1;

	return 0;
}

static int r8152b_reset_packet_filter(usbdev_t *dev)
{
	if (ocp_word_clearbits(dev, McuTypePla, PlaFmc, FmcFcrMcuEn))
		return 1;
	if (ocp_word_setbits(dev, McuTypePla, PlaFmc, FmcFcrMcuEn))
		return 1;

	return 0;
}

static int rxdy_gated_disable(usbdev_t *dev)
{
	return ocp_word_clearbits(dev, McuTypePla, PlaMisc1, RxdyGatedEn);
}

static int rtl_enable(usbdev_t *dev)
{
	if (r8152b_reset_packet_filter(dev))
		return 1;

	if (ocp_byte_setbits(dev, McuTypePla, PlaCr, CrRe | CrTe))
		return 1;

	if (rxdy_gated_disable(dev))
		return 1;
	if (r8153_set_rx_mode(dev))
		return 1;

	return 0;
}

static int r8153_mac_clk_speed_disable(usbdev_t *dev)
{
	if (ocp_write_word(dev, McuTypePla, PlaMacPwrCtrl, 0))
		return 1;
	if (ocp_write_word(dev, McuTypePla, PlaMacPwrCtrl2, 0))
		return 1;
	if (ocp_write_word(dev, McuTypePla, PlaMacPwrCtrl3, 0))
		return 1;
	if (ocp_write_word(dev, McuTypePla, PlaMacPwrCtrl4, 0))
		return 1;
	return 0;
}

static int r8153_init(usbdev_t *dev)
{
	uint16_t data;

	if (r8153_wait_autoload_done(dev))
		return 1;

	if (r8153_wait_for_phy_status(dev, 0))
		return 1;

	if (r8152_dev.version == RtlVersion03
	    || r8152_dev.version == RtlVersion04
	    || r8152_dev.version == RtlVersion05) {
		if (ocp_reg_write(dev, OcpAdcCfg,
				  AdcCfgCkadselL | AdcCfgAdcEn | AdcCfgEnEmiL))
			return 1;
	}

	if (r8152_mdio_read(dev, MiiBmcr, &data))
		return 1;
	data &= ~BmcrPowerDown;
	if (r8152_mdio_write(dev, MiiBmcr, data))
		return 1;

	if (r8153_wait_for_phy_status(dev, PhyStatLanOn))
		return 1;

	if (r8152_dev.version == RtlVersion05) {
		if (ocp_byte_clearbits(dev, McuTypePla, PlaDmyReg0, EcmAldps))
			return 1;
	}
	if (r8152_dev.version == RtlVersion05
	    || r8152_dev.version == RtlVersion06) {
		if (ocp_read_word(dev, McuTypeUsb, UsbBurstSize, &data))
			return 1;
		if ((data ? ocp_byte_setbits : ocp_byte_clearbits)(
			    dev, McuTypeUsb, UsbCsrDummy1, DynamicBurst))
			return 1;
	}

	if (ocp_byte_setbits(dev, McuTypeUsb, UsbCsrDummy2, Ep4FullFc))
		return 1;

	if (ocp_word_clearbits(dev, McuTypeUsb, UsbWdt11Ctrl, Timer11En))
		return 1;

	if (ocp_word_clearbits(dev, McuTypePla, PlaLedFeature, LedModeMask))
		return 1;

	if (ocp_write_byte(dev, McuTypeUsb, UsbLpmCtrl,
			   FifoEmpty1fb | RokExitLpm | LpmTimer500us))
		return 1;

	if (ocp_read_word(dev, McuTypeUsb, UsbAfeCtrl2, &data))
		return 1;
	data &= ~SenValMask;
	data |= SenValNormal | SelRxidle;
	if (ocp_write_word(dev, McuTypeUsb, UsbAfeCtrl2, data))
		return 1;

	if (ocp_write_word(dev, McuTypeUsb, UsbConnectTimer, 0x0001))
		return 1;

	if (r8153_power_cut_disable(dev))
		return 1;

	if (r8153_mac_clk_speed_disable(dev))
		return 1;

	if (ocp_word_clearbits(dev, McuTypeUsb, UsbUsbCtrl,
			       RxAggDisable | RxZeroEn))
		return 1;

	if (rtl_tally_reset(dev))
		return 1;

	if (rtl_enable(dev))
		return 1;

	if (r8153_hw_phy_cfg(dev))
		return 1;

	return 0;
}

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
static int r8153b_power_cut_disable(usbdev_t *dev)
{
	if (ocp_word_clearbits(dev, McuTypeUsb, UsbPowerCut, PwrEn))
		return 1;

	if (ocp_word_clearbits(dev, McuTypeUsb, UsbMisc0, PcutStatus))
		return 1;

	return 0;
}

static int r8153b_ups_disable(usbdev_t *dev)
{
	if (ocp_byte_clearbits(dev, McuTypeUsb, UsbPowerCut,
			       UpsEn | UpsPrewake))
		return 1;

	if (ocp_byte_clearbits(dev, McuTypeUsb, 0xcfff, 1 << 0))
		return 1;

	if (ocp_byte_clearbits(dev, McuTypeUsb, UsbMisc0, PcutStatus))
		return 1;

	return 0;
}

static int r8153b_queue_wake_disable(usbdev_t *dev)
{
	if (ocp_byte_clearbits(dev, McuTypePla, 0xd38a, 1 << 0))
		return 1;

	if (ocp_byte_clearbits(dev, McuTypePla, 0xd38c, 1 << 0))
		return 1;

	return 0;
}

static int rtl_runtime_suspend_disable(usbdev_t *dev)
{
	if (ocp_write_byte(dev, McuTypePla, PlaCrwecr, CrwecrConfig))
		return 1;

	if (ocp_word_clearbits(dev, McuTypePla, PlaConfig34, LinkOffWakeEn))
		return 1;

	if (ocp_write_byte(dev, McuTypePla, PlaCrwecr, CrwecrNormal))
		return 1;

	return 0;
}

static int r8153b_eee_disable(usbdev_t *dev)
{
	if (r8153_eee_disable(dev))
		return 1;

	if (ocp_dword_clearbits(dev, McuTypeUsb, UsbUpsFlags, UpsFlagsEnEee))
		return 1;

	return 0;
}

static int r8153b_hw_phy_cfg(usbdev_t *dev)
{
	if (r8153b_eee_disable(dev))
		return 1;

	if (ocp_reg_write(dev, OcpEeeAdv, 0))
		return 1;

	if (ocp_reg_setbits(dev, OcpNctlCfg, PgaReturnEn))
		return 1;

	if (ocp_word_setbits(dev, McuTypePla, PlaPhyPwr, PfmPwmSwitch))
		return 1;

	return 0;
}

static int r8153b_init(usbdev_t *dev)
{
	uint16_t data;

	if (r8153_wait_autoload_done(dev))
		return 1;

	if (r8153_wait_for_phy_status(dev, 0))
		return 1;

	if (r8152_mdio_read(dev, MiiBmcr, &data))
		return 1;
	data &= ~BmcrPowerDown;
	if (r8152_mdio_write(dev, MiiBmcr, data))
		return 1;

	if (r8153_wait_for_phy_status(dev, PhyStatLanOn))
		return 1;

	if (ocp_write_word(dev, McuTypeUsb, UsbMscTimer, 0x0fff))
		return 1;

	if (r8153b_power_cut_disable(dev))
		return 1;

	if (r8153b_ups_disable(dev))
		return 1;

	if (r8153b_queue_wake_disable(dev))
		return 1;

	if (rtl_runtime_suspend_disable(dev))
		return 1;

	if (ocp_word_clearbits(dev, McuTypeUsb, UsbUsbCtrl,
			       RxAggDisable | RxZeroEn))
		return 1;

	if (rtl_tally_reset(dev))
		return 1;

	if (rtl_enable(dev))
		return 1;

	if (r8153b_hw_phy_cfg(dev))
		return 1;

	return 0;
}

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
/*
 * The higher-level commands
 */

static int rtl8152_init(GenericUsbDevice *gen_dev)
{
	usbdev_t *usb_dev = gen_dev->dev;

	printf("R8152: Initializing\n");

	if (usb_eth_init_endpoints(usb_dev, &r8152_dev.bulk_in, 1,
				   &r8152_dev.bulk_out, 2)) {
		printf("R8152: Problem with the endpoints.\n");
		return 1;
	}

	if (get_version(usb_dev, &r8152_dev.version)) {
		printf("R8152: Problem when getting version.\n");
		return 1;
	}

	// TODO(pihsun): Support RTL8152 and RTL8153b.
	switch (r8152_dev.version) {
	case RtlVersion01:
	case RtlVersion02:
	case RtlVersion07:
		printf("RTL8152 is not supported yet.");
		return 1;
	case RtlVersion03:
	case RtlVersion04:
	case RtlVersion05:
	case RtlVersion06:
		if (r8153_init(usb_dev))
			return 1;
		break;
	case RtlVersion08:
	case RtlVersion09:
788 789 790
		if (r8153b_init(usb_dev))
			return 1;
		break;
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
	}

	printf("R8152: Done initializing\n");
	return 0;
}

static int rtl8152_mdio_read(NetDevice *dev, uint8_t loc, uint16_t *val)
{
	GenericUsbDevice *gen_dev = (GenericUsbDevice *)dev->dev_data;
	usbdev_t *usb_dev = gen_dev->dev;

	return r8152_mdio_read(usb_dev, loc, val);
}

static int rtl8152_mdio_write(NetDevice *dev, uint8_t loc, uint16_t val)
{
	GenericUsbDevice *gen_dev = (GenericUsbDevice *)dev->dev_data;
	usbdev_t *usb_dev = gen_dev->dev;

	return r8152_mdio_write(usb_dev, loc, val);
}

enum { TxFs = 1u << 31, TxLs = 1u << 30 };

static int rtl8152_send(NetDevice *net_dev, void *buf, uint16_t len)
{
	GenericUsbDevice *gen_dev = (GenericUsbDevice *)net_dev->dev_data;
	usbdev_t *usb_dev = gen_dev->dev;
	uint32_t tx_desc[2];

	static uint8_t msg[CONFIG_UIP_BUFSIZE + sizeof(tx_desc)];

	if (len > sizeof(msg) - sizeof(tx_desc)) {
		printf("R8152: Packet size %u is too large.\n", len);
		return 1;
	}
	tx_desc[0] = htole32(len | TxFs | TxLs);
	tx_desc[1] = htole32(0);
	memcpy(msg, tx_desc, sizeof(tx_desc));
	memcpy(msg + sizeof(tx_desc), buf, len);

	return (usb_dev->controller->bulk(r8152_dev.bulk_out,
					  len + sizeof(tx_desc), msg, 0)
		< 0);
}

static int rtl8152_recv(NetDevice *net_dev, void *buf, uint16_t *len,
			int maxlen)
{
	GenericUsbDevice *gen_dev = (GenericUsbDevice *)net_dev->dev_data;
	usbdev_t *usb_dev = gen_dev->dev;

	uint32_t rx_desc[6];
	int32_t packet_len;
	static int32_t buf_size = 0;
846 847
	static uint8_t msg[ETHERNET_MAX_FRAME_SIZE + sizeof(rx_desc)];
	static int32_t offset, partial;
848

849
	if (partial || offset >= buf_size) {
850 851
		offset = 0;
		buf_size = usb_dev->controller->bulk(r8152_dev.bulk_in,
852
				sizeof(msg) - partial, msg + partial, 0);
853 854 855 856
		if (buf_size < 0) {
			printf("R8152: Bulk read error %#x\n", buf_size);
			return 1;
		}
857 858
		buf_size += partial;
		partial = 0;
859 860
	}

861 862
	*len = 0;
	if (buf_size < offset + sizeof(rx_desc))
863 864 865 866 867 868
		return 0;

	memcpy(&rx_desc, msg + offset, sizeof(rx_desc));
	packet_len = le32toh(rx_desc[0]) & 0x7fff;
	packet_len -= 4;

869 870
	if (packet_len > maxlen ||
	    offset + sizeof(rx_desc) + packet_len > sizeof(msg)) {
871 872 873 874 875 876
		buf_size = 0;
		offset = 0;
		printf("R8152: Packet is too large.\n");
		return 1;
	}

877 878 879 880 881 882
	if  (offset == 0 && packet_len > buf_size) {
		partial = buf_size;
		return 0;
	}

	*len = packet_len;
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
	memcpy(buf, msg + offset + sizeof(rx_desc), packet_len);
	offset += sizeof(rx_desc) + packet_len + 4;
	offset = ALIGN_UP(offset, 8);

	return 0;
}

static const uip_eth_addr *rtl8152_get_mac(NetDevice *net_dev)
{
	GenericUsbDevice *gen_dev = (GenericUsbDevice *)net_dev->dev_data;
	usbdev_t *usb_dev = gen_dev->dev;

	uint8_t data[8];

	if (ocp_read(usb_dev, PlaIdr, sizeof(data), data, McuTypePla))
		return NULL;

	memcpy(&r8152_dev.mac_addr, data, sizeof(uip_eth_addr));
	return &r8152_dev.mac_addr;
}


/*
 * Code to plug the driver into the USB and network stacks.
 */

/* Supported usb ethernet dongles. */
static const UsbEthId r8152_supported_ids[] = {
	/* Realtek */
	{ 0x0bda, 0x8050 },
	{ 0x0bda, 0x8152 },
	{ 0x0bda, 0x8153 },
	/* Microsoft */
	{ 0x045e, 0x07ab },
	{ 0x045e, 0x07c6 },
	/* Samsung */
	{ 0x04e8, 0xa101 },
	/* Lenovo */
	{ 0x17ef, 0x304f },
	{ 0x17ef, 0x3052 },
	{ 0x17ef, 0x3054 },
	{ 0x17ef, 0x3057 },
	{ 0x17ef, 0x3062 },
	{ 0x17ef, 0x3069 },
	{ 0x17ef, 0x7205 },
	{ 0x17ef, 0x720a },
	{ 0x17ef, 0x720b },
	{ 0x17ef, 0x720c },
	{ 0x17ef, 0x7214 },
	/* Linksys */
	{ 0x13b1, 0x0041 },
	/* Nvidia */
	{ 0x0955, 0x09ff },
	/* TP-Link */
	{ 0x2357, 0x0601 },
};

static R8152Dev r8152_dev = {
	.usb_eth_dev = {
		.init = &rtl8152_init,
		.net_dev = {
			.ready = &mii_ready,
			.recv = &rtl8152_recv,
			.send = &rtl8152_send,
			.get_mac = &rtl8152_get_mac,
			.mdio_read = &rtl8152_mdio_read,
			.mdio_write = &rtl8152_mdio_write,
		},
		.supported_ids = r8152_supported_ids,
		.num_supported_ids = ARRAY_SIZE(r8152_supported_ids),
	},
};

static int r8152_driver_register(void)
{
	list_insert_after(&r8152_dev.usb_eth_dev.list_node, &usb_eth_drivers);
	return 0;
}

INIT_FUNC(r8152_driver_register);