blob: 43016935e6a9f7f99eb3f78f15b6467e789f044c [file] [log] [blame]
Damien Zammite983f0c2016-05-21 02:24:19 +10001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16/*
17 * This driver forces the 10ec:8168 device to reset so that it goes
18 * into a proper power state, also programs a default MAC address
19 * so that if the EEPROM/efuse is unconfigured it still has a default MAC.
20 */
21
22#include <arch/io.h>
23#include <device/device.h>
24#include <device/pci.h>
25#include <device/pci_ops.h>
26#include <device/pci_def.h>
27#include <delay.h>
28#include <console/console.h>
29
30#define NIC_TIMEOUT 1000
31
32#define CMD_REG 0x37
33#define CMD_REG_RESET 0x10
34
35#define CFG_9346 0x50
36#define CFG_9346_LOCK 0x00
37#define CFG_9346_UNLOCK 0xc0
38
39static void r8168_init(struct device *dev)
40{
41 u32 i;
42 const u8 mac[6] = { 0x00, 0xe0, 0x4c, 0x00, 0xc0, 0xb0 };
43
44 /* Get the resource of the NIC mmio */
45 struct resource *nic_res = find_resource(dev, PCI_BASE_ADDRESS_0);
46 u16 nic_port = (u16)nic_res->base;
47
48 /* Set bus master */
49 pci_write_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER
50 | PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
51
52 /* Reset NIC */
53 printk(BIOS_DEBUG, "r8168: Resetting NIC...");
54 outb(CMD_REG_RESET, nic_port + CMD_REG);
55
56 i = 0;
57 /* Poll for reset, with 1s timeout */
58 while (i < NIC_TIMEOUT && (inb(nic_port + CMD_REG) & CMD_REG_RESET)) {
59 udelay(1000);
60 if (++i >= NIC_TIMEOUT)
61 printk(BIOS_DEBUG, "timeout waiting for nic to reset\n");
62 }
63 if (i < NIC_TIMEOUT)
64 printk(BIOS_DEBUG, "done\n");
65
66 /* Unlock config regs */
67 outb(CFG_9346_UNLOCK, nic_port + CFG_9346);
68
69 /* Set MAC address 00:e0:4c:00:c0:b0
70 * NB: only 4-byte write accesses allowed
71 */
72 outl(mac[4] | mac[5] << 8, nic_port + 4);
73 inl(nic_port + 4);
74
75 outl(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24, nic_port);
76 inl(nic_port);
77
78 /* Lock config regs */
79 outb(CFG_9346_LOCK, nic_port + CFG_9346);
80}
81
82static struct device_operations r8168_ops = {
83 .read_resources = pci_dev_read_resources,
84 .set_resources = pci_dev_set_resources,
85 .enable_resources = pci_dev_enable_resources,
86 .init = r8168_init,
87 .scan_bus = 0,
88};
89
90static const struct pci_driver r8168_driver __pci_driver = {
91 .ops = &r8168_ops,
92 .vendor = 0x10ec,
93 .device = 0x8168,
94};