blob: 0b2fcd250e6a57b59a3cb7f23d9d1c318273bbc7 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Werner Zeh63693dc2015-02-13 12:18:58 +01002
3#include "i210.h"
4#include <device/device.h>
5#include <console/console.h>
6#include <device/pci.h>
7#include <device/pci_ids.h>
8#include <device/pci_ops.h>
Werner Zehbd316422017-10-16 08:53:34 +02009#include <device/pci_def.h>
Werner Zeh63693dc2015-02-13 12:18:58 +010010#include <string.h>
11#include <types.h>
12#include <delay.h>
13
Werner Zeh63693dc2015-02-13 12:18:58 +010014/* This is a private function to wait for a bit mask in a given register */
15/* To avoid endless loops, a time-out is implemented here. */
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +020016static int wait_done(uint32_t *reg, uint32_t mask)
Werner Zeh63693dc2015-02-13 12:18:58 +010017{
Werner Zeh608d9912016-04-26 09:26:14 +020018 uint32_t timeout = I210_POLL_TIMEOUT_US;
Werner Zeh63693dc2015-02-13 12:18:58 +010019
20 while (!(*reg & mask)) {
21 udelay(1);
22 if (!--timeout)
23 return I210_NOT_READY;
24 }
25 return I210_SUCCESS;
26}
27
28/** \brief This function can read the configuration space of the MACPHY
29 * For this purpose, the EEPROM interface is used. No direct access
30 * to the flash memory will be done.
31 * @param *dev Pointer to the PCI device of this MACPHY
32 * @param address Address inside the flash where reading will start
33 * @param count Number of words (16 bit values) to read
34 * @param *buffer Pointer to the buffer where to store read data
35 * @return void I210_NO_ERROR or an error code
36 */
Werner Zeh608d9912016-04-26 09:26:14 +020037static uint32_t read_flash(struct device *dev, uint32_t address,
38 uint32_t count, uint16_t *buffer)
Werner Zeh63693dc2015-02-13 12:18:58 +010039{
Werner Zeh608d9912016-04-26 09:26:14 +020040 uint32_t bar;
41 uint32_t *eeprd;
42 uint32_t i;
Werner Zeh63693dc2015-02-13 12:18:58 +010043
44 /* Get the BAR to memory mapped space*/
45 bar = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
46 if ((!bar) || ((address + count) > 0x40))
47 return I210_INVALID_PARAM;
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +020048 eeprd = (uint32_t *)(bar + I210_REG_EEREAD);
Werner Zeh63693dc2015-02-13 12:18:58 +010049 /* Prior to start ensure flash interface is ready by checking DONE-bit */
50 if (wait_done(eeprd, I210_DONE))
51 return I210_NOT_READY;
52
53 /*OK, interface is ready, we can use it now */
54 for (i = 0; i < count; i++) {
55 /* To start a read cycle write desired address in bits 12..2 */
56 *eeprd = ((address + i) << 2) & 0x1FFC;
57 /* Wait until read is done */
58 if (wait_done(eeprd, I210_DONE))
59 return I210_READ_ERROR;
60 /* Here, we can read back desired word in bits 31..16 */
61 buffer[i] = (*eeprd & 0xffff0000) >> 16;
62 }
63 return I210_SUCCESS;
64}
65
66/** \brief This function computes the checksum for the configuration space.
67 * The address range for the checksum is 0x00..0x3e.
68 * @param *dev Pointer to the PCI device of this MACPHY
69 * @param *checksum Pointer to the buffer where to store the checksum
70 * @return void I210_NO_ERROR or an error code
71 */
Werner Zeh608d9912016-04-26 09:26:14 +020072static uint32_t compute_checksum(struct device *dev, uint16_t *checksum)
Werner Zeh63693dc2015-02-13 12:18:58 +010073{
Werner Zeh608d9912016-04-26 09:26:14 +020074 uint16_t eep_data[0x40];
75 uint32_t i;
Werner Zeh63693dc2015-02-13 12:18:58 +010076
77 /* First read back data to compute the checksum for */
78 if (read_flash(dev, 0, 0x3f, eep_data))
79 return I210_READ_ERROR;
80 /* The checksum is computed in that way that after summarize all the */
81 /* data from word address 0 to 0x3f the result is 0xBABA. */
82 *checksum = 0;
83 for (i = 0; i < 0x3f; i++)
84 *checksum += eep_data[i];
85 *checksum = I210_TARGET_CHECKSUM - *checksum;
86 return I210_SUCCESS;
87}
88
89/** \brief This function can write the configuration space of the MACPHY
90 * For this purpose, the EEPROM interface is used. No direct access
91 * to the flash memory will be done. This function will update
92 * the checksum after a value was changed.
93 * @param *dev Pointer to the PCI device of this MACPHY
94 * @param address Address inside the flash where writing will start
95 * @param count Number of words (16 bit values) to write
96 * @param *buffer Pointer to the buffer where data to write is stored in
97 * @return void I210_NO_ERROR or an error code
98 */
Werner Zeh608d9912016-04-26 09:26:14 +020099static uint32_t write_flash(struct device *dev, uint32_t address,
100 uint32_t count, uint16_t *buffer)
Werner Zeh63693dc2015-02-13 12:18:58 +0100101{
Werner Zeh608d9912016-04-26 09:26:14 +0200102 uint32_t bar;
103 uint32_t *eepwr;
104 uint32_t *eectrl;
105 uint16_t checksum;
106 uint32_t i;
Werner Zeh63693dc2015-02-13 12:18:58 +0100107
108 /* Get the BAR to memory mapped space */
109 bar = pci_read_config32(dev, 0x10);
110 if ((!bar) || ((address + count) > 0x40))
111 return I210_INVALID_PARAM;
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +0200112 eepwr = (uint32_t *)(bar + I210_REG_EEWRITE);
113 eectrl = (uint32_t *)(bar + I210_REG_EECTRL);
Werner Zeh63693dc2015-02-13 12:18:58 +0100114 /* Prior to start ensure flash interface is ready by checking DONE-bit */
115 if (wait_done(eepwr, I210_DONE))
116 return I210_NOT_READY;
117
118 /* OK, interface is ready, we can use it now */
119 for (i = 0; i < count; i++) {
120 /* To start a write cycle write desired address in bits 12..2 */
121 /* and data to write in bits 31..16 into EEWRITE-register */
122 *eepwr = ((((address + i) << 2) & 0x1FFC) | (buffer[i] << 16));
123 /* Wait until write is done */
124 if (wait_done(eepwr, I210_DONE))
125 return I210_WRITE_ERROR;
126 }
127 /* Since we have modified data, we need to update the checksum */
128 if (compute_checksum(dev, &checksum))
129 return I210_CHECKSUM_ERROR;
130 *eepwr = (0x3f << 2) | checksum << 16;
131 if (wait_done(eepwr, I210_DONE))
132 return I210_WRITE_ERROR;
133 /* Up to now, desired data was written into shadowed RAM. We now need */
Werner Zeh608d9912016-04-26 09:26:14 +0200134 /* to perform a flash cycle to bring the shadowed RAM into flash. */
135 /* To start a flash cycle we need to set FLUPD and wait for FLDONE. */
Werner Zeh63693dc2015-02-13 12:18:58 +0100136 *eectrl = *eectrl | I210_FLUPD;
137 if (wait_done(eectrl, I210_FLUDONE))
138 return I210_FLASH_UPDATE_ERROR;
139 return I210_SUCCESS;
140}
141
142/** \brief This function can read the MAC address out of the MACPHY
143 * @param *dev Pointer to the PCI device of this MACPHY
144 * @param *MACAdr Pointer to the buffer where to store read MAC address
145 * @return void I210_NO_ERROR or an error code
146 */
Werner Zeh608d9912016-04-26 09:26:14 +0200147static uint32_t read_mac_adr(struct device *dev, uint8_t *mac_adr)
Werner Zeh63693dc2015-02-13 12:18:58 +0100148{
Werner Zeh608d9912016-04-26 09:26:14 +0200149 uint16_t adr[3];
Werner Zeh63693dc2015-02-13 12:18:58 +0100150 if (!dev || !mac_adr)
151 return I210_INVALID_PARAM;
152 if (read_flash(dev, 0, 3, adr))
153 return I210_READ_ERROR;
Werner Zeh608d9912016-04-26 09:26:14 +0200154 /* Copy the address into destination. This is done because of possible */
155 /* not matching alignment for destination to uint16_t boundary. */
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +0200156 memcpy(mac_adr, (uint8_t *)adr, 6);
Werner Zeh63693dc2015-02-13 12:18:58 +0100157 return I210_SUCCESS;
158}
159
160/** \brief This function can write the MAC address to the MACPHY
161 * @param *dev Pointer to the PCI device of this MACPHY
162 * @param *MACAdr Pointer to the buffer where the desired MAC address is
163 * @return void I210_NO_ERROR or an error code
164 */
Werner Zeh608d9912016-04-26 09:26:14 +0200165static uint32_t write_mac_adr(struct device *dev, uint8_t *mac_adr)
Werner Zeh63693dc2015-02-13 12:18:58 +0100166{
Werner Zeh608d9912016-04-26 09:26:14 +0200167 uint16_t adr[3];
Werner Zeh63693dc2015-02-13 12:18:58 +0100168 if (!dev || !mac_adr)
169 return I210_INVALID_PARAM;
170 /* Copy desired address into a local buffer to avoid alignment issues */
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +0200171 memcpy((uint8_t *)adr, mac_adr, 6);
Werner Zeh63693dc2015-02-13 12:18:58 +0100172 return write_flash(dev, 0, 3, adr);
173}
174
175/** \brief This function is the driver entry point for the init phase
176 * of the PCI bus allocator. It will program a MAC address
177 * into the MACPHY.
178 * @param *dev Pointer to the used PCI device
179 * @return void Nothing is given back
180 */
181static void init(struct device *dev)
182{
Werner Zeh608d9912016-04-26 09:26:14 +0200183 uint8_t cur_adr[6];
184 uint8_t adr_to_set[6];
Werner Zeh63693dc2015-02-13 12:18:58 +0100185 enum cb_err status;
186
187 /*Check first whether there is a valid MAC address available */
Werner Zehe22d96c2016-06-29 07:53:47 +0200188 status = mainboard_get_mac_address(dev, adr_to_set);
Werner Zeh63693dc2015-02-13 12:18:58 +0100189 if (status != CB_SUCCESS) {
190 printk(BIOS_ERR, "I210: No valid MAC address found\n");
191 return;
192 }
193 /* Before we will write a new address, check the existing one */
194 if (read_mac_adr(dev, cur_adr)) {
195 printk(BIOS_ERR, "I210: Not able to read MAC address.\n");
196 return;
197 }
198 if (memcmp(cur_adr, adr_to_set, 6)) {
199 if (write_mac_adr(dev, adr_to_set))
200 printk(BIOS_ERR, "I210: Error setting MAC address\n");
201 else
202 printk(BIOS_INFO, "I210: MAC address changed.\n");
203 } else {
204 printk(BIOS_INFO, "I210: MAC address is up to date.\n");
205 }
206 return;
207}
208
Angel Pons5ad42062020-11-10 16:25:47 +0100209static void enable_bus_master(struct device *dev)
Werner Zehbd316422017-10-16 08:53:34 +0200210{
Werner Zeh35e1fca2021-07-20 06:45:50 +0200211 if (CONFIG(PCI_ALLOW_BUS_MASTER_ANY_DEVICE))
212 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Werner Zehbd316422017-10-16 08:53:34 +0200213}
214
Werner Zeh63693dc2015-02-13 12:18:58 +0100215static struct device_operations i210_ops = {
216 .read_resources = pci_dev_read_resources,
Angel Pons5ad42062020-11-10 16:25:47 +0100217 .set_resources = pci_dev_set_resources,
Werner Zeh63693dc2015-02-13 12:18:58 +0100218 .enable_resources = pci_dev_enable_resources,
219 .init = init,
Angel Pons5ad42062020-11-10 16:25:47 +0100220 .final = enable_bus_master,
Werner Zeh63693dc2015-02-13 12:18:58 +0100221};
222
Werner Zeh77319322018-05-30 07:05:26 +0200223static const unsigned short i210_device_ids[] = { 0x1537, 0x1538, 0x1533, 0 };
Werner Zeh63693dc2015-02-13 12:18:58 +0100224
225static const struct pci_driver i210_driver __pci_driver = {
226 .ops = &i210_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100227 .vendor = PCI_VID_INTEL,
Werner Zeh63693dc2015-02-13 12:18:58 +0100228 .devices = i210_device_ids,
229};