summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/intel/e1000/gcu_if.c
blob: 3b10dfa098b8233ac0aacb25c4f394cb0cb160d5 (plain)
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
/*****************************************************************************

GPL LICENSE SUMMARY

  Copyright(c) 2007,2008,2009 Intel Corporation. All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of version 2 of the GNU General Public License as
  published by the Free Software Foundation.

  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.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  The full GNU General Public License is included in this distribution
  in the file called LICENSE.GPL.

  Contact Information:
  Intel Corporation

 version: Embedded.Release.Patch.L.1.0.7-5

  Contact Information:

  Intel Corporation, 5000 W Chandler Blvd, Chandler, AZ 85226

*****************************************************************************/

/**************************************************************************
 * @ingroup GCU_INTERFACE
 *
 * @file gcu_if.c
 *
 * @description
 *   This module contains shared functions for accessing and configuring
 *   the GCU.
 *
 **************************************************************************/

#include "gcu.h"
#include "gcu_reg.h"
#include "gcu_if.h"

/* forward declaration for write verify used in gcu_write_eth_phy */
static int32_t gcu_write_verify(uint32_t phy_num,
				uint32_t reg_addr,
				uint16_t written_data,
				const struct gcu_adapter *adapter);

/**
 * gcu_write_eth_phy
 * @phy_num: phy we want to write to, either 0, 1, or 2
 * @reg_addr: address in PHY's register space to write to
 * @phy_data: data to be written
 *
 * interface function for other modules to access the GCU
 **/
int32_t
gcu_write_eth_phy(uint32_t phy_num, uint32_t reg_addr, uint16_t phy_data)
{
	const struct gcu_adapter *adapter;
	uint32_t data = 0;
	uint32_t timeoutCounter = 0;
	const uint32_t timeoutCounterMax = GCU_MAX_ATTEMPTS;
	uint32_t pending;

	GCU_DBG("%s\n", __func__);

	if (phy_num > MDIO_COMMAND_PHY_ADDR_MAX) {
		GCU_ERR("phy_num = %d, which is greater than "
			"MDIO_COMMAND_PHY_ADDR_MAX\n", phy_num);

		return -1;
	}

	if (reg_addr > MDIO_COMMAND_PHY_REG_MAX) {
		GCU_ERR("reg_addr = %d, which is greater than "
			"MDIO_COMMAND_PHY_REG_MAX\n", phy_num);

		return -1;
	}

	/* format the data to be written to the MDIO_COMMAND_REG */
	data = phy_data;
	data |= (reg_addr << MDIO_COMMAND_PHY_REG_OFFSET);
	data |= (phy_num << MDIO_COMMAND_PHY_ADDR_OFFSET);
	data |= MDIO_COMMAND_OPER_MASK | MDIO_COMMAND_GO_MASK;

	/*
	 * get_gcu_adapter contains a spinlock, this may pause for a bit
	 */
	adapter = gcu_get_adapter();
	if (!adapter) {
		GCU_ERR("gcu_adapter not available, cannot access MMIO\n");
		return -1;
	}

	/*
	 * We write to MDIO_COMMAND_REG initially, then read that
	 * same register until its MDIO_GO bit is cleared. When cleared,
	 * the transaction is complete
	 */
	iowrite32(data, adapter->hw_addr + MDIO_COMMAND_REG);
	do {
		timeoutCounter++;
		udelay(0x32);	/* 50 microsecond delay */
		data = ioread32(adapter->hw_addr + MDIO_COMMAND_REG);
		pending =
		    (data & MDIO_COMMAND_GO_MASK) >> MDIO_COMMAND_GO_OFFSET;
	} while (pending && timeoutCounter < timeoutCounterMax);

	if (timeoutCounter == timeoutCounterMax && pending) {
		GCU_ERR("Reached maximum number of retries"
			" accessing MDIO_COMMAND_REG\n");

		gcu_release_adapter(&adapter);

		return -1;
	}

	/* validate the write during debug */
#ifdef DBG2
	if (!gcu_write_verify(phy_num, reg_addr, phy_data, adapter)) {
		GCU_ERR("Write verification failed for PHY=%d and addr=%d\n",
			phy_num, reg_addr);

		gcu_release_adapter(&adapter);

		return -1;
	}
#endif

	gcu_release_adapter(&adapter);

	return 0;
}
EXPORT_SYMBOL(gcu_write_eth_phy);

/**
 * gcu_read_eth_phy
 * @phy_num: phy we want to write to, either 0, 1, or 2
 * @reg_addr: address in PHY's register space to write to
 * @phy_data: data to be written
 *
 * interface function for other modules to access the GCU
 **/
int32_t
gcu_read_eth_phy(uint32_t phy_num, uint32_t reg_addr, uint16_t *phy_data)
{
	const struct gcu_adapter *adapter;
	uint32_t data = 0;
	uint32_t timeoutCounter = 0;
	const uint32_t timeoutCounterMax = GCU_MAX_ATTEMPTS;
	uint32_t pending = 0;

	GCU_DBG("%s\n", __func__);

	if (phy_num > MDIO_COMMAND_PHY_ADDR_MAX) {
		GCU_ERR("phy_num = %d, which is greater than "
			"MDIO_COMMAND_PHY_ADDR_MAX\n", phy_num);

		return -1;
	}

	if (reg_addr > MDIO_COMMAND_PHY_REG_MAX) {
		GCU_ERR("reg_addr = %d, which is greater than "
			"MDIO_COMMAND_PHY_REG_MAX\n", phy_num);

		return -1;
	}

	/* format the data to be written to MDIO_COMMAND_REG */
	data |= (reg_addr << MDIO_COMMAND_PHY_REG_OFFSET);
	data |= (phy_num << MDIO_COMMAND_PHY_ADDR_OFFSET);
	data |= MDIO_COMMAND_GO_MASK;

	/*
	 * this call contains a spinlock, so this may pause for a bit
	 */
	adapter = gcu_get_adapter();
	if (!adapter) {
		GCU_ERR("gcu_adapter not available, cannot access MMIO\n");
		return -1;
	}

	/*
	 * We write to MDIO_COMMAND_REG initially, then read that
	 * same register until its MDIO_GO bit is cleared. When cleared,
	 * the transaction is complete
	 */
	iowrite32(data, adapter->hw_addr + MDIO_COMMAND_REG);
	do {
		timeoutCounter++;
		udelay(0x32);	/* 50 microsecond delay */
		data = ioread32(adapter->hw_addr + MDIO_COMMAND_REG);
		pending =
		    (data & MDIO_COMMAND_GO_MASK) >> MDIO_COMMAND_GO_OFFSET;
	} while (pending && timeoutCounter < timeoutCounterMax);

	if (timeoutCounter == timeoutCounterMax && pending) {
		GCU_ERR("Reached maximum number of retries"
			" accessing MDIO_COMMAND_REG\n");

		gcu_release_adapter(&adapter);

		return -1;
	}

	/* we retrieve the data from the MDIO_STATUS_REGISTER */
	data = ioread32(adapter->hw_addr + MDIO_STATUS_REG);
	if ((data & MDIO_STATUS_STATUS_MASK) != 0) {
		GCU_ERR("Unable to retrieve data from MDIO_STATUS_REG\n");

		gcu_release_adapter(&adapter);

		return -1;
	}

	*phy_data = (uint16_t) (data & MDIO_STATUS_READ_DATA_MASK);

	gcu_release_adapter(&adapter);

	return 0;
}
EXPORT_SYMBOL(gcu_read_eth_phy);

/**
 * gcu_write_verify
 * @phy_num: phy we want to write to, either 0, 1, or 2
 * @reg_addr: address in PHY's register space to write to
 * @phy_data: data to be checked
 * @adapter: pointer to global adapter struct
 *
 * This f(n) assumes that the spinlock acquired for adapter is
 * still in force.
 **/
int32_t
gcu_write_verify(uint32_t phy_num, uint32_t reg_addr, uint16_t written_data,
		 const struct gcu_adapter *adapter)
{
	uint32_t data = 0;
	uint32_t timeoutCounter = 0;
	const uint32_t timeoutCounterMax = GCU_MAX_ATTEMPTS;
	uint32_t pending = 0;

	GCU_DBG("%s\n", __func__);

	if (!adapter) {
		GCU_ERR("Invalid adapter pointer\n");
		return 0;
	}

	if (phy_num > MDIO_COMMAND_PHY_ADDR_MAX) {
		GCU_ERR("phy_num = %d, which is greater than "
			"MDIO_COMMAND_PHY_ADDR_MAX\n", phy_num);

		return 0;
	}

	if (reg_addr > MDIO_COMMAND_PHY_REG_MAX) {
		GCU_ERR("reg_addr = %d, which is greater than "
			"MDIO_COMMAND_PHY_REG_MAX\n", phy_num);

		return 0;
	}

	/* format the data to be written to MDIO_COMMAND_REG */
	data |= (reg_addr << MDIO_COMMAND_PHY_REG_OFFSET);
	data |= (phy_num << MDIO_COMMAND_PHY_ADDR_OFFSET);
	data |= MDIO_COMMAND_GO_MASK;

	/*
	 * We write to MDIO_COMMAND_REG initially, then read that
	 * same register until its MDIO_GO bit is cleared. When cleared,
	 * the transaction is complete
	 */
	iowrite32(data, adapter->hw_addr + MDIO_COMMAND_REG);
	do {
		timeoutCounter++;
		udelay(0x32);	/* 50 microsecond delay */
		data = ioread32(adapter->hw_addr + MDIO_COMMAND_REG);
		pending =
		    (data & MDIO_COMMAND_GO_MASK) >> MDIO_COMMAND_GO_OFFSET;
	} while (pending && timeoutCounter < timeoutCounterMax);

	if (timeoutCounter == timeoutCounterMax && pending) {
		GCU_ERR("Reached maximum number of retries"
			" accessing MDIO_COMMAND_REG\n");

		return 0;
	}

	/* we retrieve the data from the MDIO_STATUS_REGISTER */
	data = ioread32(adapter->hw_addr + MDIO_STATUS_REG);
	if ((data & MDIO_STATUS_STATUS_MASK) != 0) {
		GCU_ERR("Unable to retrieve data from MDIO_STATUS_REG\n");

		return 0;
	}

	return written_data == (uint16_t) (data & MDIO_STATUS_READ_DATA_MASK);
}

/*
 * gcu_e1000_resume
 * @pdev: gcu pci_dev
 * purpose - exported PM resume function used by iegbe
 *           driver to enable the GCU device.
 */
void gcu_e1000_resume(struct pci_dev *pdev)
{
	GCU_DBG("%s\n", __func__);

	pci_restore_state(pdev);
	if (!pci_enable_device(pdev))
		GCU_DBG("pci_enable_device failed!\n");

	return;
}
EXPORT_SYMBOL(gcu_e1000_resume);

/*
 * gcu_e1000_suspend
 * @pdev: gcu pci_dev
 * @state: PM state
 * purpose - exported PM suspend function used by iegbe
 *           driver to disable the GCU device.
 */
int gcu_e1000_suspend(struct pci_dev *pdev, uint32_t state)
{
	GCU_DBG("%s\n", __func__);

	pci_save_state(pdev);
	pci_disable_device(pdev);
	state = (state > 0) ? 0 : 0;

	return state;
}
EXPORT_SYMBOL(gcu_e1000_suspend);