blob: c5e0c4ef698059b84e0a890d31573164470140ad [file] [log] [blame]
Werner Zeh63693dc2015-02-13 12:18:58 +01001/*
2 * This file is part of the coreboot project.
3 *
Werner Zeh608d9912016-04-26 09:26:14 +02004 * Copyright (C) 2014-2016 Siemens AG.
Werner Zeh63693dc2015-02-13 12:18:58 +01005 *
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.
Werner Zeh63693dc2015-02-13 12:18:58 +010014 */
15
16#include "i210.h"
17#include <device/device.h>
18#include <console/console.h>
19#include <device/pci.h>
20#include <device/pci_ids.h>
21#include <device/pci_ops.h>
22#include <string.h>
23#include <types.h>
24#include <delay.h>
25
26/* We need one function we can call to get a MAC address to use */
27/* This function can be coded somewhere else but must exist. */
Werner Zeh608d9912016-04-26 09:26:14 +020028extern enum cb_err mainboard_get_mac_address(uint16_t bus, uint8_t devfn,
29 uint8_t mac[6]);
Werner Zeh63693dc2015-02-13 12:18:58 +010030
31/* This is a private function to wait for a bit mask in a given register */
32/* To avoid endless loops, a time-out is implemented here. */
Werner Zeh608d9912016-04-26 09:26:14 +020033static int wait_done(uint32_t* reg, uint32_t mask)
Werner Zeh63693dc2015-02-13 12:18:58 +010034{
Werner Zeh608d9912016-04-26 09:26:14 +020035 uint32_t timeout = I210_POLL_TIMEOUT_US;
Werner Zeh63693dc2015-02-13 12:18:58 +010036
37 while (!(*reg & mask)) {
38 udelay(1);
39 if (!--timeout)
40 return I210_NOT_READY;
41 }
42 return I210_SUCCESS;
43}
44
45/** \brief This function can read the configuration space of the MACPHY
46 * For this purpose, the EEPROM interface is used. No direct access
47 * to the flash memory will be done.
48 * @param *dev Pointer to the PCI device of this MACPHY
49 * @param address Address inside the flash where reading will start
50 * @param count Number of words (16 bit values) to read
51 * @param *buffer Pointer to the buffer where to store read data
52 * @return void I210_NO_ERROR or an error code
53 */
Werner Zeh608d9912016-04-26 09:26:14 +020054static uint32_t read_flash(struct device *dev, uint32_t address,
55 uint32_t count, uint16_t *buffer)
Werner Zeh63693dc2015-02-13 12:18:58 +010056{
Werner Zeh608d9912016-04-26 09:26:14 +020057 uint32_t bar;
58 uint32_t *eeprd;
59 uint32_t i;
Werner Zeh63693dc2015-02-13 12:18:58 +010060
61 /* Get the BAR to memory mapped space*/
62 bar = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
63 if ((!bar) || ((address + count) > 0x40))
64 return I210_INVALID_PARAM;
Werner Zeh608d9912016-04-26 09:26:14 +020065 eeprd = (uint32_t*)(bar + I210_REG_EEREAD);
Werner Zeh63693dc2015-02-13 12:18:58 +010066 /* Prior to start ensure flash interface is ready by checking DONE-bit */
67 if (wait_done(eeprd, I210_DONE))
68 return I210_NOT_READY;
69
70 /*OK, interface is ready, we can use it now */
71 for (i = 0; i < count; i++) {
72 /* To start a read cycle write desired address in bits 12..2 */
73 *eeprd = ((address + i) << 2) & 0x1FFC;
74 /* Wait until read is done */
75 if (wait_done(eeprd, I210_DONE))
76 return I210_READ_ERROR;
77 /* Here, we can read back desired word in bits 31..16 */
78 buffer[i] = (*eeprd & 0xffff0000) >> 16;
79 }
80 return I210_SUCCESS;
81}
82
83/** \brief This function computes the checksum for the configuration space.
84 * The address range for the checksum is 0x00..0x3e.
85 * @param *dev Pointer to the PCI device of this MACPHY
86 * @param *checksum Pointer to the buffer where to store the checksum
87 * @return void I210_NO_ERROR or an error code
88 */
Werner Zeh608d9912016-04-26 09:26:14 +020089static uint32_t compute_checksum(struct device *dev, uint16_t *checksum)
Werner Zeh63693dc2015-02-13 12:18:58 +010090{
Werner Zeh608d9912016-04-26 09:26:14 +020091 uint16_t eep_data[0x40];
92 uint32_t i;
Werner Zeh63693dc2015-02-13 12:18:58 +010093
94 /* First read back data to compute the checksum for */
95 if (read_flash(dev, 0, 0x3f, eep_data))
96 return I210_READ_ERROR;
97 /* The checksum is computed in that way that after summarize all the */
98 /* data from word address 0 to 0x3f the result is 0xBABA. */
99 *checksum = 0;
100 for (i = 0; i < 0x3f; i++)
101 *checksum += eep_data[i];
102 *checksum = I210_TARGET_CHECKSUM - *checksum;
103 return I210_SUCCESS;
104}
105
106/** \brief This function can write the configuration space of the MACPHY
107 * For this purpose, the EEPROM interface is used. No direct access
108 * to the flash memory will be done. This function will update
109 * the checksum after a value was changed.
110 * @param *dev Pointer to the PCI device of this MACPHY
111 * @param address Address inside the flash where writing will start
112 * @param count Number of words (16 bit values) to write
113 * @param *buffer Pointer to the buffer where data to write is stored in
114 * @return void I210_NO_ERROR or an error code
115 */
Werner Zeh608d9912016-04-26 09:26:14 +0200116static uint32_t write_flash(struct device *dev, uint32_t address,
117 uint32_t count, uint16_t *buffer)
Werner Zeh63693dc2015-02-13 12:18:58 +0100118{
Werner Zeh608d9912016-04-26 09:26:14 +0200119 uint32_t bar;
120 uint32_t *eepwr;
121 uint32_t *eectrl;
122 uint16_t checksum;
123 uint32_t i;
Werner Zeh63693dc2015-02-13 12:18:58 +0100124
125 /* Get the BAR to memory mapped space */
126 bar = pci_read_config32(dev, 0x10);
127 if ((!bar) || ((address + count) > 0x40))
128 return I210_INVALID_PARAM;
Werner Zeh608d9912016-04-26 09:26:14 +0200129 eepwr = (uint32_t*)(bar + I210_REG_EEWRITE);
130 eectrl = (uint32_t*)(bar + I210_REG_EECTRL);
Werner Zeh63693dc2015-02-13 12:18:58 +0100131 /* Prior to start ensure flash interface is ready by checking DONE-bit */
132 if (wait_done(eepwr, I210_DONE))
133 return I210_NOT_READY;
134
135 /* OK, interface is ready, we can use it now */
136 for (i = 0; i < count; i++) {
137 /* To start a write cycle write desired address in bits 12..2 */
138 /* and data to write in bits 31..16 into EEWRITE-register */
139 *eepwr = ((((address + i) << 2) & 0x1FFC) | (buffer[i] << 16));
140 /* Wait until write is done */
141 if (wait_done(eepwr, I210_DONE))
142 return I210_WRITE_ERROR;
143 }
144 /* Since we have modified data, we need to update the checksum */
145 if (compute_checksum(dev, &checksum))
146 return I210_CHECKSUM_ERROR;
147 *eepwr = (0x3f << 2) | checksum << 16;
148 if (wait_done(eepwr, I210_DONE))
149 return I210_WRITE_ERROR;
150 /* Up to now, desired data was written into shadowed RAM. We now need */
Werner Zeh608d9912016-04-26 09:26:14 +0200151 /* to perform a flash cycle to bring the shadowed RAM into flash. */
152 /* To start a flash cycle we need to set FLUPD and wait for FLDONE. */
Werner Zeh63693dc2015-02-13 12:18:58 +0100153 *eectrl = *eectrl | I210_FLUPD;
154 if (wait_done(eectrl, I210_FLUDONE))
155 return I210_FLASH_UPDATE_ERROR;
156 return I210_SUCCESS;
157}
158
159/** \brief This function can read the MAC address out of the MACPHY
160 * @param *dev Pointer to the PCI device of this MACPHY
161 * @param *MACAdr Pointer to the buffer where to store read MAC address
162 * @return void I210_NO_ERROR or an error code
163 */
Werner Zeh608d9912016-04-26 09:26:14 +0200164static uint32_t read_mac_adr(struct device *dev, uint8_t *mac_adr)
Werner Zeh63693dc2015-02-13 12:18:58 +0100165{
Werner Zeh608d9912016-04-26 09:26:14 +0200166 uint16_t adr[3];
Werner Zeh63693dc2015-02-13 12:18:58 +0100167 if (!dev || !mac_adr)
168 return I210_INVALID_PARAM;
169 if (read_flash(dev, 0, 3, adr))
170 return I210_READ_ERROR;
Werner Zeh608d9912016-04-26 09:26:14 +0200171 /* Copy the address into destination. This is done because of possible */
172 /* not matching alignment for destination to uint16_t boundary. */
173 memcpy(mac_adr, (uint8_t*)adr, 6);
Werner Zeh63693dc2015-02-13 12:18:58 +0100174 return I210_SUCCESS;
175}
176
177/** \brief This function can write the MAC address to the MACPHY
178 * @param *dev Pointer to the PCI device of this MACPHY
179 * @param *MACAdr Pointer to the buffer where the desired MAC address is
180 * @return void I210_NO_ERROR or an error code
181 */
Werner Zeh608d9912016-04-26 09:26:14 +0200182static uint32_t write_mac_adr(struct device *dev, uint8_t *mac_adr)
Werner Zeh63693dc2015-02-13 12:18:58 +0100183{
Werner Zeh608d9912016-04-26 09:26:14 +0200184 uint16_t adr[3];
Werner Zeh63693dc2015-02-13 12:18:58 +0100185 if (!dev || !mac_adr)
186 return I210_INVALID_PARAM;
187 /* Copy desired address into a local buffer to avoid alignment issues */
Werner Zeh608d9912016-04-26 09:26:14 +0200188 memcpy((uint8_t*)adr, mac_adr, 6);
Werner Zeh63693dc2015-02-13 12:18:58 +0100189 return write_flash(dev, 0, 3, adr);
190}
191
192/** \brief This function is the driver entry point for the init phase
193 * of the PCI bus allocator. It will program a MAC address
194 * into the MACPHY.
195 * @param *dev Pointer to the used PCI device
196 * @return void Nothing is given back
197 */
198static void init(struct device *dev)
199{
Werner Zeh608d9912016-04-26 09:26:14 +0200200 uint8_t cur_adr[6];
201 uint8_t adr_to_set[6];
Werner Zeh63693dc2015-02-13 12:18:58 +0100202 enum cb_err status;
203
204 /*Check first whether there is a valid MAC address available */
205 status = mainboard_get_mac_address(dev->bus->subordinate,
206 dev->path.pci.devfn, adr_to_set);
207 if (status != CB_SUCCESS) {
208 printk(BIOS_ERR, "I210: No valid MAC address found\n");
209 return;
210 }
211 /* Before we will write a new address, check the existing one */
212 if (read_mac_adr(dev, cur_adr)) {
213 printk(BIOS_ERR, "I210: Not able to read MAC address.\n");
214 return;
215 }
216 if (memcmp(cur_adr, adr_to_set, 6)) {
217 if (write_mac_adr(dev, adr_to_set))
218 printk(BIOS_ERR, "I210: Error setting MAC address\n");
219 else
220 printk(BIOS_INFO, "I210: MAC address changed.\n");
221 } else {
222 printk(BIOS_INFO, "I210: MAC address is up to date.\n");
223 }
224 return;
225}
226
227static struct device_operations i210_ops = {
228 .read_resources = pci_dev_read_resources,
229 .set_resources = pci_dev_set_resources,
230 .enable_resources = pci_dev_enable_resources,
231 .init = init,
232 .scan_bus = 0,
233 .ops_pci = 0,
234};
235
236static const unsigned short i210_device_ids[] = { 0x1538, 0x1533, 0 };
237
238static const struct pci_driver i210_driver __pci_driver = {
239 .ops = &i210_ops,
240 .vendor = PCI_VENDOR_ID_INTEL,
241 .devices = i210_device_ids,
242};