summaryrefslogtreecommitdiffstats
path: root/src/import/chips/ocmb/common/procedures/hwp/pmic/lib/utils/pmic_enable_utils.H
blob: 835eb907cbe5b90d75503638a9980abe51fe1adc (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
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/ocmb/common/procedures/hwp/pmic/lib/utils/pmic_enable_utils.H $ */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2019                             */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

///
/// @file pmic_enable_utils.H
/// @brief Utility functions for PMIC enable operation
///
// *HWP HWP Owner: Mark Pizzutillo <mark.pizzutillo@ibm.com>
// *HWP HWP Backup: Louis Stermole <stermole@us.ibm.com>
// *HWP Team: Memory
// *HWP Level: 2
// *HWP Consumed by: FSP:HB

#ifndef __PMIC_ENABLE_UTILS_H__
#define __PMIC_ENABLE_UTILS_H__

#include <fapi2.H>
#include <pmic_regs.H>
#include <pmic_regs_fld.H>
#include <lib/i2c/i2c_pmic.H>
#include <lib/utils/pmic_common_utils.H>
#include <lib/utils/pmic_consts.H>
#include <generic/memory/lib/utils/c_str.H>
#include <generic/memory/lib/utils/index.H>
#include <mss_pmic_attribute_getters.H>
#include <mss_pmic_attribute_setters.H>

namespace mss
{
namespace pmic
{

///
/// @brief Different enable operations
///
enum enable_mode
{
    SPD = 0, // Use values from the SPD (default). Overwrite the vendor region with SPD settings
    MANUAL = 1, // Use voltage settings currently in the vendor region. (Changed via pmic_update, or factory defaults)
};

/// @brief pointer to PMIC attribute getters for DIMM target
typedef fapi2::ReturnCode (*pmic_attr_ptr)(const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_target, uint8_t& o_value);
typedef fapi2::ReturnCode (*pmic_attr_ptr_signed)(const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_target,
        int8_t& o_value);
// Pointers below allow for run-time attribute getter selection by PMIC ID (0,1)

// Voltage Setting
static constexpr pmic_attr_ptr get_swa_voltage_setting[] =
{
    mss::attr::get_pmic0_swa_voltage_setting,
    mss::attr::get_pmic1_swa_voltage_setting
};
static constexpr pmic_attr_ptr get_swb_voltage_setting[] =
{
    mss::attr::get_pmic0_swb_voltage_setting,
    mss::attr::get_pmic1_swb_voltage_setting
};
static constexpr pmic_attr_ptr get_swc_voltage_setting[] =
{
    mss::attr::get_pmic0_swc_voltage_setting,
    mss::attr::get_pmic1_swc_voltage_setting
};
static constexpr pmic_attr_ptr get_swd_voltage_setting[] =
{
    mss::attr::get_pmic0_swd_voltage_setting,
    mss::attr::get_pmic1_swd_voltage_setting
};

// Voltage Range Select
static constexpr pmic_attr_ptr get_swa_voltage_range_select[] =
{
    mss::attr::get_pmic0_swa_voltage_range_select,
    mss::attr::get_pmic1_swa_voltage_range_select
};
static constexpr pmic_attr_ptr get_swb_voltage_range_select[] =
{
    mss::attr::get_pmic0_swb_voltage_range_select,
    mss::attr::get_pmic1_swb_voltage_range_select
};
static constexpr pmic_attr_ptr get_swc_voltage_range_select[] =
{
    mss::attr::get_pmic0_swc_voltage_range_select,
    mss::attr::get_pmic1_swc_voltage_range_select
};
static constexpr pmic_attr_ptr get_swd_voltage_range_select[] =
{
    mss::attr::get_pmic0_swd_voltage_range_select,
    mss::attr::get_pmic1_swd_voltage_range_select
};

// Voltage Offset
static constexpr pmic_attr_ptr_signed get_swa_voltage_offset[] =
{
    mss::attr::get_pmic0_swa_voltage_offset,
    mss::attr::get_pmic1_swa_voltage_offset
};
static constexpr pmic_attr_ptr_signed get_swb_voltage_offset[] =
{
    mss::attr::get_pmic0_swb_voltage_offset,
    mss::attr::get_pmic1_swb_voltage_offset
};
static constexpr pmic_attr_ptr_signed get_swc_voltage_offset[] =
{
    mss::attr::get_pmic0_swc_voltage_offset,
    mss::attr::get_pmic1_swc_voltage_offset
};
static constexpr pmic_attr_ptr_signed get_swd_voltage_offset[] =
{
    mss::attr::get_pmic0_swd_voltage_offset,
    mss::attr::get_pmic1_swd_voltage_offset
};

// Sequence Delay
static constexpr pmic_attr_ptr get_swa_sequence_delay[] =
{
    mss::attr::get_pmic0_swa_sequence_delay,
    mss::attr::get_pmic1_swa_sequence_delay
};
static constexpr pmic_attr_ptr get_swb_sequence_delay[] =
{
    mss::attr::get_pmic0_swb_sequence_delay,
    mss::attr::get_pmic1_swb_sequence_delay
};
static constexpr pmic_attr_ptr get_swc_sequence_delay[] =
{
    mss::attr::get_pmic0_swc_sequence_delay,
    mss::attr::get_pmic1_swc_sequence_delay
};
static constexpr pmic_attr_ptr get_swd_sequence_delay[] =
{
    mss::attr::get_pmic0_swd_sequence_delay,
    mss::attr::get_pmic1_swd_sequence_delay
};

// Sequence Order
static constexpr pmic_attr_ptr get_swa_sequence_order[] =
{
    mss::attr::get_pmic0_swa_sequence_order,
    mss::attr::get_pmic1_swa_sequence_order
};
static constexpr pmic_attr_ptr get_swb_sequence_order[] =
{
    mss::attr::get_pmic0_swb_sequence_order,
    mss::attr::get_pmic1_swb_sequence_order
};
static constexpr pmic_attr_ptr get_swc_sequence_order[] =
{
    mss::attr::get_pmic0_swc_sequence_order,
    mss::attr::get_pmic1_swc_sequence_order
};
static constexpr pmic_attr_ptr get_swd_sequence_order[] =
{
    mss::attr::get_pmic0_swd_sequence_order,
    mss::attr::get_pmic1_swd_sequence_order
};

// Phase Combination
static constexpr pmic_attr_ptr get_phase_comb[] =
{
    mss::attr::get_pmic0_phase_comb,
    mss::attr::get_pmic1_phase_comb
};

// EFD Fields

// Offset
static constexpr pmic_attr_ptr_signed get_efd_swa_voltage_offset[] =
{
    mss::attr::get_efd_pmic0_swa_voltage_offset,
    mss::attr::get_efd_pmic1_swa_voltage_offset
};

static constexpr pmic_attr_ptr_signed get_efd_swb_voltage_offset[] =
{
    mss::attr::get_efd_pmic0_swb_voltage_offset,
    mss::attr::get_efd_pmic1_swb_voltage_offset
};

static constexpr pmic_attr_ptr_signed get_efd_swc_voltage_offset[] =
{
    mss::attr::get_efd_pmic0_swc_voltage_offset,
    mss::attr::get_efd_pmic1_swc_voltage_offset
};

static constexpr pmic_attr_ptr_signed get_efd_swd_voltage_offset[] =
{
    mss::attr::get_efd_pmic0_swd_voltage_offset,
    mss::attr::get_efd_pmic1_swd_voltage_offset
};

// These arrays allow us to dynamically choose the right attribute getter at runtime based on the rail and mss::pmic::id
static const pmic_attr_ptr* get_volt_setting[] =
{
    mss::pmic::get_swa_voltage_setting,
    mss::pmic::get_swb_voltage_setting,
    mss::pmic::get_swc_voltage_setting,
    mss::pmic::get_swd_voltage_setting
};

static const pmic_attr_ptr* get_volt_range_select[] =
{
    mss::pmic::get_swa_voltage_range_select,
    mss::pmic::get_swb_voltage_range_select,
    mss::pmic::get_swc_voltage_range_select,
    mss::pmic::get_swd_voltage_range_select
};

static const pmic_attr_ptr_signed* get_volt_offset[] =
{
    mss::pmic::get_swa_voltage_offset,
    mss::pmic::get_swb_voltage_offset,
    mss::pmic::get_swc_voltage_offset,
    mss::pmic::get_swd_voltage_offset
};

// EFD Offset + Direction functions
static const pmic_attr_ptr_signed* get_efd_volt_offset[] =
{
    mss::pmic::get_efd_swa_voltage_offset,
    mss::pmic::get_efd_swb_voltage_offset,
    mss::pmic::get_efd_swc_voltage_offset,
    mss::pmic::get_efd_swd_voltage_offset
};

// For output traces
static const std::vector<const char*> PMIC_RAIL_NAMES = {"SWA", "SWB", "SWC", "SWD"};

// Attribute setter FP type
typedef fapi2::ReturnCode (*pmic_attr_setter_ptr)(const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_target,
        uint8_t i_value);
typedef fapi2::ReturnCode (*pmic_attr_setter_ptr_signed)(const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_target,
        int8_t i_value);

// Voltage Setting
static constexpr pmic_attr_setter_ptr set_swa_voltage_setting[] =
{
    mss::attr::set_pmic0_swa_voltage_setting,
    mss::attr::set_pmic1_swa_voltage_setting
};
static constexpr pmic_attr_setter_ptr set_swb_voltage_setting[] =
{
    mss::attr::set_pmic0_swb_voltage_setting,
    mss::attr::set_pmic1_swb_voltage_setting
};
static constexpr pmic_attr_setter_ptr set_swc_voltage_setting[] =
{
    mss::attr::set_pmic0_swc_voltage_setting,
    mss::attr::set_pmic1_swc_voltage_setting
};
static constexpr pmic_attr_setter_ptr set_swd_voltage_setting[] =
{
    mss::attr::set_pmic0_swd_voltage_setting,
    mss::attr::set_pmic1_swd_voltage_setting
};

// Voltage Range Select
static constexpr pmic_attr_setter_ptr set_swa_voltage_range_select[] =
{
    mss::attr::set_pmic0_swa_voltage_range_select,
    mss::attr::set_pmic1_swa_voltage_range_select
};
static constexpr pmic_attr_setter_ptr set_swb_voltage_range_select[] =
{
    mss::attr::set_pmic0_swb_voltage_range_select,
    mss::attr::set_pmic1_swb_voltage_range_select
};
static constexpr pmic_attr_setter_ptr set_swc_voltage_range_select[] =
{
    mss::attr::set_pmic0_swc_voltage_range_select,
    mss::attr::set_pmic1_swc_voltage_range_select
};
static constexpr pmic_attr_setter_ptr set_swd_voltage_range_select[] =
{
    mss::attr::set_pmic0_swd_voltage_range_select,
    mss::attr::set_pmic1_swd_voltage_range_select
};

// Voltage Offset
static constexpr pmic_attr_setter_ptr_signed set_swa_voltage_offset[] =
{
    mss::attr::set_pmic0_swa_voltage_offset,
    mss::attr::set_pmic1_swa_voltage_offset
};
static constexpr pmic_attr_setter_ptr_signed set_swb_voltage_offset[] =
{
    mss::attr::set_pmic0_swb_voltage_offset,
    mss::attr::set_pmic1_swb_voltage_offset
};
static constexpr pmic_attr_setter_ptr_signed set_swc_voltage_offset[] =
{
    mss::attr::set_pmic0_swc_voltage_offset,
    mss::attr::set_pmic1_swc_voltage_offset
};
static constexpr pmic_attr_setter_ptr_signed set_swd_voltage_offset[] =
{
    mss::attr::set_pmic0_swd_voltage_offset,
    mss::attr::set_pmic1_swd_voltage_offset
};

// Sequence Delay
static constexpr pmic_attr_setter_ptr set_swa_sequence_delay[] =
{
    mss::attr::set_pmic0_swa_sequence_delay,
    mss::attr::set_pmic1_swa_sequence_delay
};
static constexpr pmic_attr_setter_ptr set_swb_sequence_delay[] =
{
    mss::attr::set_pmic0_swb_sequence_delay,
    mss::attr::set_pmic1_swb_sequence_delay
};
static constexpr pmic_attr_setter_ptr set_swc_sequence_delay[] =
{
    mss::attr::set_pmic0_swc_sequence_delay,
    mss::attr::set_pmic1_swc_sequence_delay
};
static constexpr pmic_attr_setter_ptr set_swd_sequence_delay[] =
{
    mss::attr::set_pmic0_swd_sequence_delay,
    mss::attr::set_pmic1_swd_sequence_delay
};

// Sequence Order
static constexpr pmic_attr_setter_ptr set_swa_sequence_order[] =
{
    mss::attr::set_pmic0_swa_sequence_order,
    mss::attr::set_pmic1_swa_sequence_order
};
static constexpr pmic_attr_setter_ptr set_swb_sequence_order[] =
{
    mss::attr::set_pmic0_swb_sequence_order,
    mss::attr::set_pmic1_swb_sequence_order
};
static constexpr pmic_attr_setter_ptr set_swc_sequence_order[] =
{
    mss::attr::set_pmic0_swc_sequence_order,
    mss::attr::set_pmic1_swc_sequence_order
};
static constexpr pmic_attr_setter_ptr set_swd_sequence_order[] =
{
    mss::attr::set_pmic0_swd_sequence_order,
    mss::attr::set_pmic1_swd_sequence_order
};

// Phase Combination
static constexpr pmic_attr_setter_ptr set_phase_comb[] =
{
    mss::attr::set_pmic0_phase_comb,
    mss::attr::set_pmic1_phase_comb
};

// Offset
static constexpr pmic_attr_setter_ptr_signed set_efd_swa_voltage_offset[] =
{
    mss::attr::set_efd_pmic0_swa_voltage_offset,
    mss::attr::set_efd_pmic1_swa_voltage_offset
};

static constexpr pmic_attr_setter_ptr_signed set_efd_swb_voltage_offset[] =
{
    mss::attr::set_efd_pmic0_swb_voltage_offset,
    mss::attr::set_efd_pmic1_swb_voltage_offset
};

static constexpr pmic_attr_setter_ptr_signed set_efd_swc_voltage_offset[] =
{
    mss::attr::set_efd_pmic0_swc_voltage_offset,
    mss::attr::set_efd_pmic1_swc_voltage_offset
};

static constexpr pmic_attr_setter_ptr_signed set_efd_swd_voltage_offset[] =
{
    mss::attr::set_efd_pmic0_swd_voltage_offset,
    mss::attr::set_efd_pmic1_swd_voltage_offset
};

//-----------------------------------
// SPD Biasing functions
//-----------------------------------

///
/// @breif set VR enable bit for system startup via R32 (not broadcast)
///
/// @param[in] i_pmic_target PMIC target
/// @return fapi2::FAPI2_RC_SUCCESS iff success
///
fapi2::ReturnCode start_vr_enable(
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_PMIC>& i_pmic_target);

///
/// @brief bias PMIC0 with spd settings for phase combination (SWA, SWB or SWA+SWB)
///
/// @param[in] i_pmic_target PMIC target
/// @param[in] i_port - port target of PMIC
/// @param[in] i_dimm_index - DIMM index for PMIC (0,1)
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS iff no error
///
fapi2::ReturnCode bias_with_spd_phase_comb(
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_PMIC>& i_pmic_target,
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_DIMM>& i_dimm,
    const mss::pmic::id i_id);

///
/// @brief bias with SPD settings for voltage ranges
///
/// @param[in] i_pmic_target PMIC target
/// @param[in] i_port port target of PMIC
/// @param[in] i_dimm_index DIMM index for PMIC (0,1)
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS iff no error
///
fapi2::ReturnCode bias_with_spd_volt_ranges(
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_PMIC>& i_pmic_target,
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_DIMM>& i_dimm,
    const mss::pmic::id i_id);

///
/// @brief bias with SPD settings for startup sequence
///
/// @param[in] i_pmic_target PMIC target
/// @param[in] i_port port target of PMIC
/// @param[in] i_dimm_index DIMM index for PMIC (0,1)
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS iff no error
///
fapi2::ReturnCode bias_with_spd_startup_seq(
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_PMIC>& i_pmic_target,
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_DIMM>& i_dimm,
    const mss::pmic::id i_id);

///
/// @brief Set the startup seq register with the given parameters
///
/// @param[in] i_pmic_target PMIC target
/// @param[in] i_rail rail to
/// @param[in] i_round sequence round 1-4
/// @param[in] i_delay delay after round
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS iff no error
///
fapi2::ReturnCode set_startup_seq_register(
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_PMIC>& i_pmic_target,
    const uint8_t i_rail,
    const uint8_t i_round,
    const uint8_t i_delay);

//-----------------------------------
// Templated SPD Biasing functions
//-----------------------------------

///
/// @brief bias with spd settings for voltages
///
/// @tparam V mss::pmic::vendor (TI/IDT)
/// @param[in] i_pmic_target PMIC target
/// @param[in] i_dimm_target DIMM target of PMIC
/// @param[in] i_dimm_index - DIMM index for PMIC (0,1)
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS iff no error
///
template <mss::pmic::vendor V>
fapi2::ReturnCode bias_with_spd_voltages(
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_PMIC>& i_pmic_target,
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_DIMM>& i_dimm_target,
    const mss::pmic::id i_id);

///
/// @brief Calcuate target voltage for PMIC
///
/// @param[in] i_dimm_target DIMM target of PMIC (holds the attributes)
/// @param[in] i_id ID of pmic (0,1)
/// @param[in] i_rail RAIL to calculate voltage for
/// @param[out] o_volt_buffer output buffer
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS iff success
///
inline fapi2::ReturnCode calculate_voltage_write_buffer(
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_DIMM>& i_dimm_target,
    const mss::pmic::id i_id,
    const uint8_t i_rail,
    fapi2::buffer<uint8_t>& o_volt_buffer)
{
    uint8_t l_volt = 0;
    int8_t l_volt_offset = 0;
    int8_t l_efd_volt_offset = 0;

    // Get the attributes corresponding to the rail and PMIC indices
    FAPI_TRY(get_volt_setting[i_rail][i_id](i_dimm_target, l_volt));
    FAPI_TRY(get_volt_offset[i_rail][i_id](i_dimm_target, l_volt_offset));
    FAPI_TRY(get_efd_volt_offset[i_rail][i_id](i_dimm_target, l_efd_volt_offset));

    // Set output buffer
    o_volt_buffer = l_volt + l_volt_offset + l_efd_volt_offset;

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Bias with spd voltages for IDT pmic
///
/// @param[in] i_pmic_target PMIC target
/// @param[in] i_dimm_target DIMM target
/// @param[in] i_id relative ID of PMIC (0/1)
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS iff success
///
template <>
inline fapi2::ReturnCode bias_with_spd_voltages<mss::pmic::vendor::IDT>(
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_PMIC>& i_pmic_target,
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_DIMM>& i_dimm_target,
    const mss::pmic::id i_id)
{
    using CONSTS = mss::pmic::consts<mss::pmic::product::JEDEC_COMPLIANT>;

    for (uint8_t l_rail_index = mss::pmic::rail::SWA; l_rail_index <= mss::pmic::rail::SWD; ++l_rail_index)
    {
        fapi2::buffer<uint8_t> l_volt_buffer;
        FAPI_TRY(calculate_voltage_write_buffer(i_dimm_target, i_id, l_rail_index, l_volt_buffer));

        // Since we have unsigned integers, this should check both underflow and overflow
        FAPI_ASSERT(l_volt_buffer <= CONSTS::MAX_VOLT_BITMAP,
                    fapi2::PMIC_VOLTAGE_OUT_OF_RANGE()
                    .set_TARGET(i_pmic_target)
                    .set_VOLTAGE_BITMAP(l_volt_buffer)
                    .set_RAIL(mss::pmic::VOLT_SETTING_REGS[l_rail_index]),
                    "Voltage out of range as determined by SPD voltage +/- offset for %s of %s",
                    PMIC_RAIL_NAMES[l_rail_index], mss::c_str(i_pmic_target) );

        l_volt_buffer = l_volt_buffer << CONSTS::SHIFT_VOLTAGE_FOR_REG;
        FAPI_TRY(mss::pmic::i2c::reg_write(i_pmic_target, mss::pmic::VOLT_SETTING_REGS[l_rail_index], l_volt_buffer),
                 "Error writing address 0x%02hhX of PMIC %s", mss::pmic::VOLT_SETTING_REGS[l_rail_index], mss::c_str(i_pmic_target));

    }

    return fapi2::FAPI2_RC_SUCCESS;

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Bias with spd voltages for TI pmic
///
/// @param[in] i_pmic_target PMIC target
/// @param[in] i_dimm_target DIMM target
/// @param[in] i_id relative ID of PMIC (0/1)
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS iff success
///
template <>
inline fapi2::ReturnCode bias_with_spd_voltages<mss::pmic::vendor::TI>(
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_PMIC>& i_pmic_target,
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_DIMM>& i_dimm_target,
    const mss::pmic::id i_id)
{
    using REGS = pmicRegs<mss::pmic::product::JEDEC_COMPLIANT>;
    using CONSTS = mss::pmic::consts<mss::pmic::product::JEDEC_COMPLIANT>;

    for (uint8_t l_rail_index = mss::pmic::rail::SWA; l_rail_index <= mss::pmic::rail::SWD; ++l_rail_index)
    {
        fapi2::buffer<uint8_t> l_volt_buffer;
        FAPI_TRY(calculate_voltage_write_buffer(i_dimm_target, i_id, l_rail_index, l_volt_buffer));

        bool l_overflow = false;

        uint8_t l_volt_range_select = 0;
        FAPI_TRY(get_volt_range_select[l_rail_index][i_id](i_dimm_target, l_volt_range_select));

        // SWD supports a RANGE 1, but NOT SWA-C
        if (l_rail_index == mss::pmic::rail::SWD)
        {
            // Can set range and voltage directly
            fapi2::buffer<uint8_t> l_volt_range_buffer;

            // Read in what the register has, as to not overwrite any default values
            FAPI_TRY(mss::pmic::i2c::reg_read_reverse_buffer(i_pmic_target, REGS::R2B, l_volt_range_buffer));

            l_volt_range_buffer.writeBit<FIELDS::SWD_VOLTAGE_RANGE>(l_volt_range_select);

            // Write to PMIC
            FAPI_TRY(mss::pmic::i2c::reg_write_reverse_buffer(i_pmic_target, REGS::R2B, l_volt_range_buffer));
        }
        else
        {
            // Check if the range is range 1, in which case we will need to convert to range 0 (thanks TI)
            if (l_volt_range_select == CONSTS::RANGE_1)
            {
                // Convert from RANGE1 -> RANGE0

                // Since both ranges are 5mV (at least they're supposed to be)
                // we can just subtract the difference between range 1 and 0
                // which is 600mV -> 800mV
                // 200mV / 5 = 40
                uint8_t l_old_voltage = uint8_t(l_volt_buffer);
                l_volt_buffer = l_volt_buffer - CONSTS::CONVERT_RANGE1_TO_RANGE0;

                // Check for overflow (the old voltage should be larger unless we rolled over)
                if (l_old_voltage < l_volt_buffer)
                {
                    // Not using an extra xml error for this as overflow implies PMIC_VOLTAGE_OUT_OF_RANGE anyway.
                    FAPI_ERR("Overflow ocurred when converting SPD Range 1 voltage to TI Range 0");
                    l_overflow = true;
                }
            }
        }

        // Check for overflow due to range conversion (SWA-C), but also due to overflow due to offset attributes
        FAPI_ASSERT( (!l_overflow) && (l_volt_buffer <= CONSTS::MAX_VOLT_BITMAP),
                     fapi2::PMIC_VOLTAGE_OUT_OF_RANGE()
                     .set_TARGET(i_pmic_target)
                     .set_VOLTAGE_BITMAP(l_volt_buffer)
                     .set_RAIL(mss::pmic::VOLT_SETTING_REGS[l_rail_index]),
                     "Voltage out of range as determined by SPD voltage +/- offset for %s of %s",
                     PMIC_RAIL_NAMES[l_rail_index], mss::c_str(i_pmic_target) );

        l_volt_buffer = l_volt_buffer << CONSTS::SHIFT_VOLTAGE_FOR_REG;
        FAPI_TRY(mss::pmic::i2c::reg_write(i_pmic_target, mss::pmic::VOLT_SETTING_REGS[l_rail_index], l_volt_buffer),
                 "Error writing address 0x%02hhX of PMIC %s", mss::pmic::VOLT_SETTING_REGS[l_rail_index], mss::c_str(i_pmic_target));

    }

    return fapi2::FAPI2_RC_SUCCESS;

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Bias PMIC from SPD settings per vendor
///
/// @tparam V mss::pmic::vendor (IDT/TI)
/// @param[in] i_pmic_target PMIC target
/// @param[in] i_dimm_target DIMM target associated with PMIC
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS iff success
///
template <mss::pmic::vendor V>
fapi2::ReturnCode bias_with_spd_settings(
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_PMIC>& i_pmic_target,
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_DIMM>& i_dimm_target);

///
/// @brief Bias IDT PMIC from SPD settings
///
/// @param[in] i_pmic_target PMIC target
/// @param[in] i_dimm_target DIMM target
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS iff success
///
template<>
inline fapi2::ReturnCode bias_with_spd_settings<mss::pmic::vendor::IDT>(
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_PMIC>& i_pmic_target,
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_DIMM>& i_dimm_target)
{
    using CONSTS = mss::pmic::consts<mss::pmic::product::JEDEC_COMPLIANT>;
    // Unlock Vendor Region
    FAPI_TRY(mss::pmic::unlock_vendor_region(i_pmic_target),
             "Error unlocking vendor region on PMIC %s", mss::c_str(i_pmic_target));

    {
        // PMIC position/ID of the OCMB target. There could be 4 total, but we care about whether its PMIC0(2) or PMIC1(3)
        const mss::pmic::id l_relative_pmic_id = static_cast<mss::pmic::id>(
                    mss::index(i_pmic_target) % CONSTS::NUM_UNIQUE_PMICS);

        // Phase combination
        FAPI_TRY(mss::pmic::bias_with_spd_phase_comb(i_pmic_target, i_dimm_target, l_relative_pmic_id));

        // Voltage ranges
        FAPI_TRY(mss::pmic::bias_with_spd_volt_ranges(i_pmic_target, i_dimm_target, l_relative_pmic_id));

        // Voltages
        FAPI_TRY(mss::pmic::bias_with_spd_voltages<mss::pmic::IDT>(i_pmic_target, i_dimm_target, l_relative_pmic_id));

        // Startup sequence
        FAPI_TRY(mss::pmic::bias_with_spd_startup_seq(i_pmic_target, i_dimm_target, l_relative_pmic_id));
    }

fapi_try_exit:
    // Try to lock vendor region even in the case of an error in this function
    return mss::pmic::lock_vendor_region(i_pmic_target, fapi2::current_err);
}

///
/// @brief Bias TI PMIC from SPD settings
///
/// @param[in] i_pmic_target PMIC target
/// @param[in] i_dimm_target DIMM target
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS iff success
///
template<>
inline fapi2::ReturnCode bias_with_spd_settings<mss::pmic::vendor::TI>(
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_PMIC>& i_pmic_target,
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_DIMM>& i_dimm_target)
{
    using CONSTS = mss::pmic::consts<mss::pmic::product::JEDEC_COMPLIANT>;
    // Unlock Vendor Region
    FAPI_TRY(mss::pmic::unlock_vendor_region(i_pmic_target),
             "Error unlocking vendor region on PMIC %s", mss::c_str(i_pmic_target));

    {
        // PMIC position/ID of the OCMB target. There could be 4 total, but we care about whether its PMIC0(2) or PMIC1(3)
        const mss::pmic::id l_relative_pmic_id = static_cast<mss::pmic::id>(
                    mss::index(i_pmic_target) % CONSTS::NUM_UNIQUE_PMICS);
        // Phase combination
        FAPI_TRY(mss::pmic::bias_with_spd_phase_comb(i_pmic_target, i_dimm_target, l_relative_pmic_id));

        // Voltages
        // TI pmic only has range 0 for SWA-C. We need to convert anything SPD that says range 1 --> range 0
        FAPI_TRY(mss::pmic::bias_with_spd_voltages<mss::pmic::TI>(i_pmic_target, i_dimm_target, l_relative_pmic_id));

        // Startup sequence
        FAPI_TRY(mss::pmic::bias_with_spd_startup_seq(i_pmic_target, i_dimm_target, l_relative_pmic_id));
    }

fapi_try_exit:
    // Try to lock vendor region even in the case of an error in this function
    return mss::pmic::lock_vendor_region(i_pmic_target, fapi2::current_err);
}

//------------------- ENABLE FUNCTIONS-----------------//

///
/// @brief template function for the chip-specific enable functions
///
/// @tparam H module_height
/// @param[in] i_pmic_target - the pmic target
/// @param[in] i_dimm_target - the dimm target that the PMIC resides on
/// @param[in] i_vendor_id - the vendor ID of the PMIC to bias
/// @param[in] i_mode enable mode operation
/// @return fapi2::ReturnCode - FAPI2_RC_SUCCESS if successful
///
template <mss::pmic::module_height H>
fapi2::ReturnCode enable_chip(const fapi2::Target<fapi2::TargetType::TARGET_TYPE_PMIC>& i_pmic_target,
                              const fapi2::Target<fapi2::TargetType::TARGET_TYPE_DIMM>& i_dimm_target,
                              const uint16_t i_vendor_id,
                              const mss::pmic::enable_mode i_mode);

///
/// @brief enable procedure for IDT PMIC and 1U or 2U DIMM
///
/// @param[in] i_pmic_target - the pmic_target
/// @param[in] i_dimm_target - the dimm target that the PMIC resides on
/// @param[in] i_vendor_id - the vendor ID of the PMIC to bias
/// @param[in] i_mode enable mode operation
/// @return fapi2::ReturnCode - FAPI2_RC_SUCCESS if successful
///
template <>
inline fapi2::ReturnCode enable_chip<mss::pmic::module_height::HEIGHT_1U>(
    const fapi2::Target<fapi2::TARGET_TYPE_PMIC>& i_pmic_target,
    const fapi2::Target<fapi2::TargetType::TARGET_TYPE_DIMM>& i_dimm_target,
    const uint16_t i_vendor_id,
    const mss::pmic::enable_mode i_mode)
{
    if (i_mode == mss::pmic::enable_mode::SPD)
    {
        FAPI_INF("Setting PMIC settings from SPD");

        // Make sure it is TI or IDT
        FAPI_ASSERT((i_vendor_id == mss::pmic::vendor::IDT ||
                     i_vendor_id == mss::pmic::vendor::TI),
                    fapi2::PMIC_CHIP_NOT_RECOGNIZED()
                    .set_TARGET(i_pmic_target)
                    .set_VENDOR_ID(i_vendor_id),
                    "Unknown PMIC: %s with vendor ID 0x%04hhX",
                    mss::c_str(i_pmic_target),
                    uint8_t(i_vendor_id) );

        if (i_vendor_id == mss::pmic::vendor::IDT)
        {
            FAPI_TRY(mss::pmic::bias_with_spd_settings<mss::pmic::vendor::IDT>(i_pmic_target, i_dimm_target),
                     "enable_chip<IDT, 1U/2U>: Error biasing PMIC %s with SPD settings",
                     mss::c_str(i_pmic_target));
        }
        else // assert done in pmic_enable.C that vendor is IDT or TI
        {
            FAPI_TRY(mss::pmic::bias_with_spd_settings<mss::pmic::vendor::TI>(i_pmic_target, i_dimm_target),
                     "enable_chip<TI, 1U/2U>: Error biasing PMIC %s with SPD settings",
                     mss::c_str(i_pmic_target));
        }
    }
    else // manual mode
    {
        FAPI_INF("Using built-in PMIC settings (defaults or from pmic_update)");
    }

    // Execute VR Enable command
    FAPI_TRY((mss::pmic::start_vr_enable(i_pmic_target)),
             "enable_chip: Failed to start VR Enable for %s", mss::c_str(i_pmic_target));

    return fapi2::FAPI2_RC_SUCCESS;

fapi_try_exit:
    return fapi2::current_err;
}

}
} // mss

#endif
OpenPOWER on IntegriCloud