aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/ixl/ixl_pf_i2c.c
blob: 55062f7f7e2a49fc745bda319e81be6f708b5aa1 (plain) (blame)
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
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
487
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
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
/******************************************************************************

  Copyright (c) 2013-2018, Intel Corporation
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are met:
  
   1. Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright 
      notice, this list of conditions and the following disclaimer in the 
      documentation and/or other materials provided with the distribution.
  
   3. Neither the name of the Intel Corporation nor the names of its 
      contributors may be used to endorse or promote products derived from 
      this software without specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.

******************************************************************************/
/*$FreeBSD$*/

#include "ixl_pf.h"

#define IXL_I2C_T_RISE		1
#define IXL_I2C_T_FALL		1
#define IXL_I2C_T_SU_DATA	1
#define IXL_I2C_T_SU_STA	5
#define IXL_I2C_T_SU_STO	4
#define IXL_I2C_T_HD_STA	4
#define IXL_I2C_T_LOW		5
#define IXL_I2C_T_HIGH		4
#define IXL_I2C_T_BUF		5
#define IXL_I2C_CLOCK_STRETCHING_TIMEOUT 500

#define IXL_I2C_REG(_hw)	\
    I40E_GLGEN_I2CPARAMS(_hw->func_caps.mdio_port_num)

/* I2C bit-banging functions */
static s32	ixl_set_i2c_data(struct ixl_pf *pf, u32 *i2cctl, bool data);
static bool	ixl_get_i2c_data(struct ixl_pf *pf, u32 *i2cctl);
static void	ixl_raise_i2c_clk(struct ixl_pf *pf, u32 *i2cctl);
static void	ixl_lower_i2c_clk(struct ixl_pf *pf, u32 *i2cctl);
static s32	ixl_clock_out_i2c_bit(struct ixl_pf *pf, bool data);
static s32	ixl_get_i2c_ack(struct ixl_pf *pf);
static s32	ixl_clock_out_i2c_byte(struct ixl_pf *pf, u8 data);
static s32	ixl_clock_in_i2c_bit(struct ixl_pf *pf, bool *data);
static s32	ixl_clock_in_i2c_byte(struct ixl_pf *pf, u8 *data);
static void 	ixl_i2c_bus_clear(struct ixl_pf *pf);
static void	ixl_i2c_start(struct ixl_pf *pf);
static void	ixl_i2c_stop(struct ixl_pf *pf);

static s32	ixl_wait_for_i2c_completion(struct i40e_hw *hw, u8 portnum);

/**
 *  ixl_i2c_bus_clear - Clears the I2C bus
 *  @hw: pointer to hardware structure
 *
 *  Clears the I2C bus by sending nine clock pulses.
 *  Used when data line is stuck low.
 **/
static void
ixl_i2c_bus_clear(struct ixl_pf *pf)
{
	struct i40e_hw *hw = &pf->hw;
	u32 i2cctl = rd32(hw, IXL_I2C_REG(hw));
	u32 i;

	DEBUGFUNC("ixl_i2c_bus_clear");

	ixl_i2c_start(pf);

	ixl_set_i2c_data(pf, &i2cctl, 1);

	for (i = 0; i < 9; i++) {
		ixl_raise_i2c_clk(pf, &i2cctl);

		/* Min high period of clock is 4us */
		i40e_usec_delay(IXL_I2C_T_HIGH);

		ixl_lower_i2c_clk(pf, &i2cctl);

		/* Min low period of clock is 4.7us*/
		i40e_usec_delay(IXL_I2C_T_LOW);
	}

	ixl_i2c_start(pf);

	/* Put the i2c bus back to default state */
	ixl_i2c_stop(pf);
}

/**
 *  ixl_i2c_stop - Sets I2C stop condition
 *  @hw: pointer to hardware structure
 *
 *  Sets I2C stop condition (Low -> High on SDA while SCL is High)
 **/
static void
ixl_i2c_stop(struct ixl_pf *pf)
{
	struct i40e_hw *hw = &pf->hw;
	u32 i2cctl = rd32(hw, IXL_I2C_REG(hw));

	DEBUGFUNC("ixl_i2c_stop");

	/* Stop condition must begin with data low and clock high */
	ixl_set_i2c_data(pf, &i2cctl, 0);
	ixl_raise_i2c_clk(pf, &i2cctl);

	/* Setup time for stop condition (4us) */
	i40e_usec_delay(IXL_I2C_T_SU_STO);

	ixl_set_i2c_data(pf, &i2cctl, 1);

	/* bus free time between stop and start (4.7us)*/
	i40e_usec_delay(IXL_I2C_T_BUF);
}

/**
 *  ixl_clock_in_i2c_byte - Clocks in one byte via I2C
 *  @hw: pointer to hardware structure
 *  @data: data byte to clock in
 *
 *  Clocks in one byte data via I2C data/clock
 **/
static s32
ixl_clock_in_i2c_byte(struct ixl_pf *pf, u8 *data)
{
	s32 i;
	bool bit = 0;

	DEBUGFUNC("ixl_clock_in_i2c_byte");

	for (i = 7; i >= 0; i--) {
		ixl_clock_in_i2c_bit(pf, &bit);
		*data |= bit << i;
	}

	return I40E_SUCCESS;
}

/**
 *  ixl_clock_in_i2c_bit - Clocks in one bit via I2C data/clock
 *  @hw: pointer to hardware structure
 *  @data: read data value
 *
 *  Clocks in one bit via I2C data/clock
 **/
static s32
ixl_clock_in_i2c_bit(struct ixl_pf *pf, bool *data)
{
	struct i40e_hw *hw = &pf->hw;
	u32 i2cctl = rd32(hw, IXL_I2C_REG(hw));

	DEBUGFUNC("ixl_clock_in_i2c_bit");

	ixl_raise_i2c_clk(pf, &i2cctl);

	/* Minimum high period of clock is 4us */
	i40e_usec_delay(IXL_I2C_T_HIGH);

	i2cctl = rd32(hw, IXL_I2C_REG(hw));
	i2cctl |= I40E_GLGEN_I2CPARAMS_DATA_OE_N_MASK;
	wr32(hw, IXL_I2C_REG(hw), i2cctl);
	ixl_flush(hw);

	i2cctl = rd32(hw, IXL_I2C_REG(hw));
	*data = ixl_get_i2c_data(pf, &i2cctl);

	ixl_lower_i2c_clk(pf, &i2cctl);

	/* Minimum low period of clock is 4.7 us */
	i40e_usec_delay(IXL_I2C_T_LOW);

	return I40E_SUCCESS;
}

/**
 *  ixl_get_i2c_ack - Polls for I2C ACK
 *  @hw: pointer to hardware structure
 *
 *  Clocks in/out one bit via I2C data/clock
 **/
static s32
ixl_get_i2c_ack(struct ixl_pf *pf)
{
	struct i40e_hw *hw = &pf->hw;
	s32 status = I40E_SUCCESS;
	u32 i = 0;
	u32 i2cctl = rd32(hw, IXL_I2C_REG(hw));
	u32 timeout = 10;
	bool ack = 1;

	ixl_raise_i2c_clk(pf, &i2cctl);

	/* Minimum high period of clock is 4us */
	i40e_usec_delay(IXL_I2C_T_HIGH);

	i2cctl = rd32(hw, IXL_I2C_REG(hw));
	i2cctl |= I40E_GLGEN_I2CPARAMS_DATA_OE_N_MASK;
	wr32(hw, IXL_I2C_REG(hw), i2cctl);
	ixl_flush(hw);

	/* Poll for ACK.  Note that ACK in I2C spec is
	 * transition from 1 to 0 */
	for (i = 0; i < timeout; i++) {
		i2cctl = rd32(hw, IXL_I2C_REG(hw));
		ack = ixl_get_i2c_data(pf, &i2cctl);

		i40e_usec_delay(1);
		if (!ack)
			break;
	}

	if (ack) {
		ixl_dbg(pf, IXL_DBG_I2C, "I2C ack was not received.\n");
		status = I40E_ERR_PHY;
	}

	ixl_lower_i2c_clk(pf, &i2cctl);

	/* Minimum low period of clock is 4.7 us */
	i40e_usec_delay(IXL_I2C_T_LOW);

	return status;
}

/**
 *  ixl_clock_out_i2c_bit - Clocks in/out one bit via I2C data/clock
 *  @hw: pointer to hardware structure
 *  @data: data value to write
 *
 *  Clocks out one bit via I2C data/clock
 **/
static s32
ixl_clock_out_i2c_bit(struct ixl_pf *pf, bool data)
{
	struct i40e_hw *hw = &pf->hw;
	s32 status;
	u32 i2cctl = rd32(hw, IXL_I2C_REG(hw));

	status = ixl_set_i2c_data(pf, &i2cctl, data);
	if (status == I40E_SUCCESS) {
		ixl_raise_i2c_clk(pf, &i2cctl);

		/* Minimum high period of clock is 4us */
		i40e_usec_delay(IXL_I2C_T_HIGH);

		ixl_lower_i2c_clk(pf, &i2cctl);

		/* Minimum low period of clock is 4.7 us.
		 * This also takes care of the data hold time.
		 */
		i40e_usec_delay(IXL_I2C_T_LOW);
	} else {
		status = I40E_ERR_PHY;
		ixl_dbg(pf, IXL_DBG_I2C, "I2C data was not set to %#x\n", data);
	}

	return status;
}

/**
 *  ixl_clock_out_i2c_byte - Clocks out one byte via I2C
 *  @hw: pointer to hardware structure
 *  @data: data byte clocked out
 *
 *  Clocks out one byte data via I2C data/clock
 **/
static s32
ixl_clock_out_i2c_byte(struct ixl_pf *pf, u8 data)
{
	struct i40e_hw *hw = &pf->hw;
	s32 status = I40E_SUCCESS;
	s32 i;
	u32 i2cctl;
	bool bit;

	DEBUGFUNC("ixl_clock_out_i2c_byte");

	for (i = 7; i >= 0; i--) {
		bit = (data >> i) & 0x1;
		status = ixl_clock_out_i2c_bit(pf, bit);

		if (status != I40E_SUCCESS)
			break;
	}

	/* Release SDA line (set high) */
	i2cctl = rd32(hw, IXL_I2C_REG(hw));
	i2cctl |= I40E_GLGEN_I2CPARAMS_DATA_OUT_MASK;
	i2cctl &= ~(I40E_GLGEN_I2CPARAMS_DATA_OE_N_MASK);
	wr32(hw, IXL_I2C_REG(hw), i2cctl);
	ixl_flush(hw);

	return status;
}

/**
 *  ixl_lower_i2c_clk - Lowers the I2C SCL clock
 *  @hw: pointer to hardware structure
 *  @i2cctl: Current value of I2CCTL register
 *
 *  Lowers the I2C clock line '1'->'0'
 **/
static void
ixl_lower_i2c_clk(struct ixl_pf *pf, u32 *i2cctl)
{
	struct i40e_hw *hw = &pf->hw;

	*i2cctl &= ~(I40E_GLGEN_I2CPARAMS_CLK_MASK);
	*i2cctl &= ~(I40E_GLGEN_I2CPARAMS_CLK_OE_N_MASK);

	wr32(hw, IXL_I2C_REG(hw), *i2cctl);
	ixl_flush(hw);

	/* SCL fall time (300ns) */
	i40e_usec_delay(IXL_I2C_T_FALL);
}

/**
 *  ixl_raise_i2c_clk - Raises the I2C SCL clock
 *  @hw: pointer to hardware structure
 *  @i2cctl: Current value of I2CCTL register
 *
 *  Raises the I2C clock line '0'->'1'
 **/
static void
ixl_raise_i2c_clk(struct ixl_pf *pf, u32 *i2cctl)
{
	struct i40e_hw *hw = &pf->hw;
	u32 i = 0;
	u32 timeout = IXL_I2C_CLOCK_STRETCHING_TIMEOUT;
	u32 i2cctl_r = 0;

	for (i = 0; i < timeout; i++) {
		*i2cctl |= I40E_GLGEN_I2CPARAMS_CLK_MASK;
		*i2cctl &= ~(I40E_GLGEN_I2CPARAMS_CLK_OE_N_MASK);

		wr32(hw, IXL_I2C_REG(hw), *i2cctl);
		ixl_flush(hw);
		/* SCL rise time (1000ns) */
		i40e_usec_delay(IXL_I2C_T_RISE);

		i2cctl_r = rd32(hw, IXL_I2C_REG(hw));
		if (i2cctl_r & I40E_GLGEN_I2CPARAMS_CLK_IN_MASK)
			break;
	}
}

/**
 *  ixl_get_i2c_data - Reads the I2C SDA data bit
 *  @hw: pointer to hardware structure
 *  @i2cctl: Current value of I2CCTL register
 *
 *  Returns the I2C data bit value
 **/
static bool
ixl_get_i2c_data(struct ixl_pf *pf, u32 *i2cctl)
{
	bool data;

	if (*i2cctl & I40E_GLGEN_I2CPARAMS_DATA_IN_MASK)
		data = 1;
	else
		data = 0;

	return data;
}

/**
 *  ixl_set_i2c_data - Sets the I2C data bit
 *  @hw: pointer to hardware structure
 *  @i2cctl: Current value of I2CCTL register
 *  @data: I2C data value (0 or 1) to set
 *
 *  Sets the I2C data bit
 **/
static s32
ixl_set_i2c_data(struct ixl_pf *pf, u32 *i2cctl, bool data)
{
	struct i40e_hw *hw = &pf->hw;
	s32 status = I40E_SUCCESS;

	DEBUGFUNC("ixl_set_i2c_data");

	if (data)
		*i2cctl |= I40E_GLGEN_I2CPARAMS_DATA_OUT_MASK;
	else
		*i2cctl &= ~(I40E_GLGEN_I2CPARAMS_DATA_OUT_MASK);
	*i2cctl &= ~(I40E_GLGEN_I2CPARAMS_DATA_OE_N_MASK);

	wr32(hw, IXL_I2C_REG(hw), *i2cctl);
	ixl_flush(hw);

	/* Data rise/fall (1000ns/300ns) and set-up time (250ns) */
	i40e_usec_delay(IXL_I2C_T_RISE + IXL_I2C_T_FALL + IXL_I2C_T_SU_DATA);

	/* Verify data was set correctly */
	*i2cctl = rd32(hw, IXL_I2C_REG(hw));
	if (data != ixl_get_i2c_data(pf, i2cctl)) {
		status = I40E_ERR_PHY;
		ixl_dbg(pf, IXL_DBG_I2C, "Error - I2C data was not set to %X.\n", data);
	}

	return status;
}

/**
 *  ixl_i2c_start - Sets I2C start condition
 *  Sets I2C start condition (High -> Low on SDA while SCL is High)
 **/
static void
ixl_i2c_start(struct ixl_pf *pf)
{
	struct i40e_hw *hw = &pf->hw;
	u32 i2cctl = rd32(hw, IXL_I2C_REG(hw));

	DEBUGFUNC("ixl_i2c_start");

	/* Start condition must begin with data and clock high */
	ixl_set_i2c_data(pf, &i2cctl, 1);
	ixl_raise_i2c_clk(pf, &i2cctl);

	/* Setup time for start condition (4.7us) */
	i40e_usec_delay(IXL_I2C_T_SU_STA);

	ixl_set_i2c_data(pf, &i2cctl, 0);

	/* Hold time for start condition (4us) */
	i40e_usec_delay(IXL_I2C_T_HD_STA);

	ixl_lower_i2c_clk(pf, &i2cctl);

	/* Minimum low period of clock is 4.7 us */
	i40e_usec_delay(IXL_I2C_T_LOW);

}

/**
 *  ixl_read_i2c_byte_bb - Reads 8 bit word over I2C
 **/
s32
ixl_read_i2c_byte_bb(struct ixl_pf *pf, u8 byte_offset,
		  u8 dev_addr, u8 *data)
{
	struct i40e_hw *hw = &pf->hw;
	u32 max_retry = 10;
	u32 retry = 0;
	bool nack = 1;
	s32 status;
	*data = 0;

	u32 i2cctl = rd32(hw, IXL_I2C_REG(hw));
	i2cctl |= I40E_GLGEN_I2CPARAMS_I2CBB_EN_MASK;
	wr32(hw, IXL_I2C_REG(hw), i2cctl);
	ixl_flush(hw);

	do {
		ixl_i2c_start(pf);

		/* Device Address and write indication */
		status = ixl_clock_out_i2c_byte(pf, dev_addr);
		if (status != I40E_SUCCESS) {
			ixl_dbg(pf, IXL_DBG_I2C, "dev_addr clock out error\n");
			goto fail;
		}

		status = ixl_get_i2c_ack(pf);
		if (status != I40E_SUCCESS) {
			ixl_dbg(pf, IXL_DBG_I2C, "dev_addr i2c ack error\n");
			goto fail;
		}

		status = ixl_clock_out_i2c_byte(pf, byte_offset);
		if (status != I40E_SUCCESS) {
			ixl_dbg(pf, IXL_DBG_I2C, "byte_offset clock out error\n");
			goto fail;
		}

		status = ixl_get_i2c_ack(pf);
		if (status != I40E_SUCCESS) {
			ixl_dbg(pf, IXL_DBG_I2C, "byte_offset i2c ack error\n");
			goto fail;
		}

		ixl_i2c_start(pf);

		/* Device Address and read indication */
		status = ixl_clock_out_i2c_byte(pf, (dev_addr | 0x1));
		if (status != I40E_SUCCESS)
			goto fail;

		status = ixl_get_i2c_ack(pf);
		if (status != I40E_SUCCESS)
			goto fail;

		status = ixl_clock_in_i2c_byte(pf, data);
		if (status != I40E_SUCCESS)
			goto fail;

		status = ixl_clock_out_i2c_bit(pf, nack);
		if (status != I40E_SUCCESS)
			goto fail;

		ixl_i2c_stop(pf);
		status = I40E_SUCCESS;
		goto done;

fail:
		ixl_i2c_bus_clear(pf);
		i40e_msec_delay(100);
		retry++;
		if (retry < max_retry)
			ixl_dbg(pf, IXL_DBG_I2C, "I2C byte read error - Retrying\n");
		else
			ixl_dbg(pf, IXL_DBG_I2C, "I2C byte read error\n");

	} while (retry < max_retry);
done:
	i2cctl = rd32(hw, IXL_I2C_REG(hw));
	i2cctl &= ~I40E_GLGEN_I2CPARAMS_I2CBB_EN_MASK;
	wr32(hw, IXL_I2C_REG(hw), i2cctl);
	ixl_flush(hw);

	return status;
}

/**
 *  ixl_write_i2c_byte_bb - Writes 8 bit word over I2C
 **/
s32
ixl_write_i2c_byte_bb(struct ixl_pf *pf, u8 byte_offset,
		       u8 dev_addr, u8 data)
{
	struct i40e_hw *hw = &pf->hw;
	s32 status = I40E_SUCCESS;
	u32 max_retry = 1;
	u32 retry = 0;

	u32 i2cctl = rd32(hw, IXL_I2C_REG(hw));
	i2cctl |= I40E_GLGEN_I2CPARAMS_I2CBB_EN_MASK;
	wr32(hw, IXL_I2C_REG(hw), i2cctl);
	ixl_flush(hw);

	do {
		ixl_i2c_start(pf);

		status = ixl_clock_out_i2c_byte(pf, dev_addr);
		if (status != I40E_SUCCESS)
			goto fail;

		status = ixl_get_i2c_ack(pf);
		if (status != I40E_SUCCESS)
			goto fail;

		status = ixl_clock_out_i2c_byte(pf, byte_offset);
		if (status != I40E_SUCCESS)
			goto fail;

		status = ixl_get_i2c_ack(pf);
		if (status != I40E_SUCCESS)
			goto fail;

		status = ixl_clock_out_i2c_byte(pf, data);
		if (status != I40E_SUCCESS)
			goto fail;

		status = ixl_get_i2c_ack(pf);
		if (status != I40E_SUCCESS)
			goto fail;

		ixl_i2c_stop(pf);
		goto write_byte_out;

fail:
		ixl_i2c_bus_clear(pf);
		i40e_msec_delay(100);
		retry++;
		if (retry < max_retry)
			ixl_dbg(pf, IXL_DBG_I2C, "I2C byte write error - Retrying\n");
		else
			ixl_dbg(pf, IXL_DBG_I2C, "I2C byte write error\n");
	} while (retry < max_retry);

write_byte_out:
	i2cctl = rd32(hw, IXL_I2C_REG(hw));
	i2cctl &= ~I40E_GLGEN_I2CPARAMS_I2CBB_EN_MASK;
	wr32(hw, IXL_I2C_REG(hw), i2cctl);
	ixl_flush(hw);

	return status;
}

/**
 *  ixl_read_i2c_byte - Reads 8 bit word over I2C using a hardware register
 **/
s32
ixl_read_i2c_byte_reg(struct ixl_pf *pf, u8 byte_offset,
		  u8 dev_addr, u8 *data)
{
	struct i40e_hw *hw = &pf->hw;
	u32 reg = 0;
	s32 status;
	*data = 0;

	reg |= (byte_offset << I40E_GLGEN_I2CCMD_REGADD_SHIFT);
	reg |= (((dev_addr >> 1) & 0x7) << I40E_GLGEN_I2CCMD_PHYADD_SHIFT);
	reg |= I40E_GLGEN_I2CCMD_OP_MASK;
	wr32(hw, I40E_GLGEN_I2CCMD(hw->func_caps.mdio_port_num), reg);

	status = ixl_wait_for_i2c_completion(hw, hw->func_caps.mdio_port_num);

	/* Get data from I2C register */
	reg = rd32(hw, I40E_GLGEN_I2CCMD(hw->func_caps.mdio_port_num));

	/* Retrieve data readed from EEPROM */
	*data = (u8)(reg & 0xff);

	if (status)
		ixl_dbg(pf, IXL_DBG_I2C, "I2C byte read error\n");
	return status;
}

/**
 *  ixl_write_i2c_byte - Writes 8 bit word over I2C using a hardware register
 **/
s32
ixl_write_i2c_byte_reg(struct ixl_pf *pf, u8 byte_offset,
		       u8 dev_addr, u8 data)
{
	struct i40e_hw *hw = &pf->hw;
	s32 status = I40E_SUCCESS;
	u32 reg = 0;
	u8 upperbyte = 0;
	u16 datai2c = 0;

	status = ixl_read_i2c_byte_reg(pf, byte_offset + 1, dev_addr, &upperbyte);
	datai2c = ((u16)upperbyte << 8) | (u16)data;
	reg = rd32(hw, I40E_GLGEN_I2CCMD(hw->func_caps.mdio_port_num));

	/* Form write command */
	reg &= ~I40E_GLGEN_I2CCMD_PHYADD_MASK;
	reg |= (((dev_addr >> 1) & 0x7) << I40E_GLGEN_I2CCMD_PHYADD_SHIFT);
	reg &= ~I40E_GLGEN_I2CCMD_REGADD_MASK;
	reg |= (byte_offset << I40E_GLGEN_I2CCMD_REGADD_SHIFT);
	reg &= ~I40E_GLGEN_I2CCMD_DATA_MASK;
	reg |= (datai2c << I40E_GLGEN_I2CCMD_DATA_SHIFT);
	reg &= ~I40E_GLGEN_I2CCMD_OP_MASK;

	/* Write command to registers controling I2C - data and address. */
	wr32(hw, I40E_GLGEN_I2CCMD(hw->func_caps.mdio_port_num), reg);

	status = ixl_wait_for_i2c_completion(hw, hw->func_caps.mdio_port_num);

	if (status)
		ixl_dbg(pf, IXL_DBG_I2C, "I2C byte write error\n");
	return status;
}

/**
 *  ixl_wait_for_i2c_completion
 **/
static s32
ixl_wait_for_i2c_completion(struct i40e_hw *hw, u8 portnum)
{
	s32 status = 0;
	u32 timeout = 100;
	u32 reg;
	do {
		reg = rd32(hw, I40E_GLGEN_I2CCMD(portnum));
		if ((reg & I40E_GLGEN_I2CCMD_R_MASK) != 0)
			break;
		i40e_usec_delay(10);
	} while (timeout-- > 0);

	if (timeout == 0)
		return I40E_ERR_TIMEOUT;
	else
		return status;
}

/**
 *  ixl_read_i2c_byte - Reads 8 bit word over I2C using a hardware register
 **/
s32
ixl_read_i2c_byte_aq(struct ixl_pf *pf, u8 byte_offset,
		  u8 dev_addr, u8 *data)
{
	struct i40e_hw *hw = &pf->hw;
	s32 status = I40E_SUCCESS;
	u32 reg;

	status = i40e_aq_get_phy_register(hw,
					I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
					dev_addr,
					byte_offset,
					&reg, NULL);

	if (status)
		ixl_dbg(pf, IXL_DBG_I2C, "I2C byte read status %s, error %s\n",
		    i40e_stat_str(hw, status), i40e_aq_str(hw, hw->aq.asq_last_status));
	else
		*data = (u8)reg;

	return status;
}

/**
 *  ixl_write_i2c_byte - Writes 8 bit word over I2C using a hardware register
 **/
s32
ixl_write_i2c_byte_aq(struct ixl_pf *pf, u8 byte_offset,
		       u8 dev_addr, u8 data)
{
	struct i40e_hw *hw = &pf->hw;
	s32 status = I40E_SUCCESS;

	status = i40e_aq_set_phy_register(hw,
					I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
					dev_addr,
					byte_offset,
					data, NULL);

	if (status)
		ixl_dbg(pf, IXL_DBG_I2C, "I2C byte write status %s, error %s\n",
		    i40e_stat_str(hw, status), i40e_aq_str(hw, hw->aq.asq_last_status));

	return status;
}