2 * Copyright 2012-15 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include "dm_services.h"
29 * Pre-requisites: headers required by header of this unit
31 #include "include/i2caux_interface.h"
32 #include "dc_bios_types.h"
41 * Post-requisites: headers required by this unit
45 #include "i2c_engine.h"
46 #include "aux_engine.h"
52 #include "dce80/i2caux_dce80.h"
54 #include "dce100/i2caux_dce100.h"
56 #include "dce110/i2caux_dce110.h"
58 #include "dce112/i2caux_dce112.h"
60 #include "dce120/i2caux_dce120.h"
62 #include "diagnostics/i2caux_diag.h"
66 * Plain API, available publicly
69 struct i2caux
*dal_i2caux_create(
70 struct dc_context
*ctx
)
72 if (IS_FPGA_MAXIMUS_DC(ctx
->dce_environment
)) {
73 return dal_i2caux_diag_fpga_create(ctx
);
76 switch (ctx
->dce_version
) {
78 return dal_i2caux_dce80_create(ctx
);
79 case DCE_VERSION_11_2
:
80 return dal_i2caux_dce112_create(ctx
);
81 case DCE_VERSION_11_0
:
82 return dal_i2caux_dce110_create(ctx
);
83 case DCE_VERSION_10_0
:
84 return dal_i2caux_dce100_create(ctx
);
85 case DCE_VERSION_12_0
:
86 return dal_i2caux_dce120_create(ctx
);
93 bool dal_i2caux_submit_i2c_command(
94 struct i2caux
*i2caux
,
96 struct i2c_command
*cmd
)
98 struct i2c_engine
*engine
;
99 uint8_t index_of_payload
= 0;
113 * default will be SW, however there is a feature flag in adapter
114 * service that determines whether SW i2c_engine will be available or
115 * not, if sw i2c is not available we will fallback to hw. This feature
116 * flag is set to not creating sw i2c engine for every dce except dce80
119 switch (cmd
->engine
) {
120 case I2C_COMMAND_ENGINE_DEFAULT
:
121 case I2C_COMMAND_ENGINE_SW
:
122 /* try to acquire SW engine first,
123 * acquire HW engine if SW engine not available */
124 engine
= i2caux
->funcs
->acquire_i2c_sw_engine(i2caux
, ddc
);
127 engine
= i2caux
->funcs
->acquire_i2c_hw_engine(
130 case I2C_COMMAND_ENGINE_HW
:
132 /* try to acquire HW engine first,
133 * acquire SW engine if HW engine not available */
134 engine
= i2caux
->funcs
->acquire_i2c_hw_engine(i2caux
, ddc
);
137 engine
= i2caux
->funcs
->acquire_i2c_sw_engine(
144 engine
->funcs
->set_speed(engine
, cmd
->speed
);
148 while (index_of_payload
< cmd
->number_of_payloads
) {
149 bool mot
= (index_of_payload
!= cmd
->number_of_payloads
- 1);
151 struct i2c_payload
*payload
= cmd
->payloads
+ index_of_payload
;
153 struct i2caux_transaction_request request
= { 0 };
155 request
.operation
= payload
->write
?
156 I2CAUX_TRANSACTION_WRITE
:
157 I2CAUX_TRANSACTION_READ
;
159 request
.payload
.address_space
=
160 I2CAUX_TRANSACTION_ADDRESS_SPACE_I2C
;
161 request
.payload
.address
= (payload
->address
<< 1) |
163 request
.payload
.length
= payload
->length
;
164 request
.payload
.data
= payload
->data
;
166 if (!engine
->base
.funcs
->submit_request(
167 &engine
->base
, &request
, mot
)) {
175 i2caux
->funcs
->release_engine(i2caux
, &engine
->base
);
180 bool dal_i2caux_submit_aux_command(
181 struct i2caux
*i2caux
,
183 struct aux_command
*cmd
)
185 struct aux_engine
*engine
;
186 uint8_t index_of_payload
= 0;
200 engine
= i2caux
->funcs
->acquire_aux_engine(i2caux
, ddc
);
205 engine
->delay
= cmd
->defer_delay
;
206 engine
->max_defer_write_retry
= cmd
->max_defer_write_retry
;
210 while (index_of_payload
< cmd
->number_of_payloads
) {
211 struct aux_payload
*payload
= cmd
->payloads
+ index_of_payload
;
212 struct i2caux_transaction_request request
= { 0 };
214 if (cmd
->mot
== I2C_MOT_UNDEF
)
215 mot
= (index_of_payload
!= cmd
->number_of_payloads
- 1);
217 mot
= (cmd
->mot
== I2C_MOT_TRUE
);
219 request
.operation
= payload
->write
?
220 I2CAUX_TRANSACTION_WRITE
:
221 I2CAUX_TRANSACTION_READ
;
223 if (payload
->i2c_over_aux
) {
224 request
.payload
.address_space
=
225 I2CAUX_TRANSACTION_ADDRESS_SPACE_I2C
;
227 request
.payload
.address
= (payload
->address
<< 1) |
230 request
.payload
.address_space
=
231 I2CAUX_TRANSACTION_ADDRESS_SPACE_DPCD
;
233 request
.payload
.address
= payload
->address
;
236 request
.payload
.length
= payload
->length
;
237 request
.payload
.data
= payload
->data
;
239 if (!engine
->base
.funcs
->submit_request(
240 &engine
->base
, &request
, mot
)) {
248 i2caux
->funcs
->release_engine(i2caux
, &engine
->base
);
253 static bool get_hw_supported_ddc_line(
255 enum gpio_ddc_line
*line
)
257 enum gpio_ddc_line line_found
;
264 if (!ddc
->hw_info
.hw_supported
)
267 line_found
= dal_ddc_get_line(ddc
);
269 if (line_found
>= GPIO_DDC_LINE_COUNT
)
277 void dal_i2caux_configure_aux(
278 struct i2caux
*i2caux
,
280 union aux_config cfg
)
282 struct aux_engine
*engine
=
283 i2caux
->funcs
->acquire_aux_engine(i2caux
, ddc
);
288 engine
->funcs
->configure(engine
, cfg
);
290 i2caux
->funcs
->release_engine(i2caux
, &engine
->base
);
293 void dal_i2caux_destroy(
294 struct i2caux
**i2caux
)
296 if (!i2caux
|| !*i2caux
) {
301 (*i2caux
)->funcs
->destroy(i2caux
);
308 * An utility function used by 'struct i2caux' and its descendants
311 uint32_t dal_i2caux_get_reference_clock(
312 struct dc_bios
*bios
)
314 struct firmware_info info
= { { 0 } };
316 if (bios
->funcs
->get_firmware_info(bios
, &info
) != BP_RESULT_OK
)
319 return info
.pll_info
.crystal_frequency
;
328 /* following are expressed in KHz */
329 DEFAULT_I2C_SW_SPEED
= 50,
330 DEFAULT_I2C_HW_SPEED
= 50,
332 /* This is the timeout as defined in DP 1.2a,
333 * 2.3.4 "Detailed uPacket TX AUX CH State Description". */
334 AUX_TIMEOUT_PERIOD
= 400,
336 /* Ideally, the SW timeout should be just above 550usec
337 * which is programmed in HW.
338 * But the SW timeout of 600usec is not reliable,
339 * because on some systems, delay_in_microseconds()
340 * returns faster than it should.
341 * EPR #379763: by trial-and-error on different systems,
342 * 700usec is the minimum reliable SW timeout for polling
343 * the AUX_SW_STATUS.AUX_SW_DONE bit.
344 * This timeout expires *only* when there is
345 * AUX Error or AUX Timeout conditions - not during normal operation.
346 * During normal operation, AUX_SW_STATUS.AUX_SW_DONE bit is set
347 * at most within ~240usec. That means,
348 * increasing this timeout will not affect normal operation,
349 * and we'll timeout after
350 * SW_AUX_TIMEOUT_PERIOD_MULTIPLIER * AUX_TIMEOUT_PERIOD = 1600usec.
351 * This timeout is especially important for
352 * resume from S3 and CTS. */
353 SW_AUX_TIMEOUT_PERIOD_MULTIPLIER
= 4
356 struct i2c_engine
*dal_i2caux_acquire_i2c_sw_engine(
357 struct i2caux
*i2caux
,
360 enum gpio_ddc_line line
;
361 struct i2c_engine
*engine
= NULL
;
363 if (get_hw_supported_ddc_line(ddc
, &line
))
364 engine
= i2caux
->i2c_sw_engines
[line
];
367 engine
= i2caux
->i2c_generic_sw_engine
;
372 if (!engine
->base
.funcs
->acquire(&engine
->base
, ddc
))
378 struct aux_engine
*dal_i2caux_acquire_aux_engine(
379 struct i2caux
*i2caux
,
382 enum gpio_ddc_line line
;
383 struct aux_engine
*engine
;
385 if (!get_hw_supported_ddc_line(ddc
, &line
))
388 engine
= i2caux
->aux_engines
[line
];
393 if (!engine
->base
.funcs
->acquire(&engine
->base
, ddc
))
399 void dal_i2caux_release_engine(
400 struct i2caux
*i2caux
,
401 struct engine
*engine
)
403 engine
->funcs
->release_engine(engine
);
405 dal_ddc_close(engine
->ddc
);
410 bool dal_i2caux_construct(
411 struct i2caux
*i2caux
,
412 struct dc_context
*ctx
)
418 i2caux
->i2c_sw_engines
[i
] = NULL
;
419 i2caux
->i2c_hw_engines
[i
] = NULL
;
420 i2caux
->aux_engines
[i
] = NULL
;
423 } while (i
< GPIO_DDC_LINE_COUNT
);
425 i2caux
->i2c_generic_sw_engine
= NULL
;
426 i2caux
->i2c_generic_hw_engine
= NULL
;
428 i2caux
->aux_timeout_period
=
429 SW_AUX_TIMEOUT_PERIOD_MULTIPLIER
* AUX_TIMEOUT_PERIOD
;
431 i2caux
->default_i2c_sw_speed
= DEFAULT_I2C_SW_SPEED
;
432 i2caux
->default_i2c_hw_speed
= DEFAULT_I2C_HW_SPEED
;
437 void dal_i2caux_destruct(
438 struct i2caux
*i2caux
)
442 if (i2caux
->i2c_generic_hw_engine
)
443 i2caux
->i2c_generic_hw_engine
->funcs
->destroy(
444 &i2caux
->i2c_generic_hw_engine
);
446 if (i2caux
->i2c_generic_sw_engine
)
447 i2caux
->i2c_generic_sw_engine
->funcs
->destroy(
448 &i2caux
->i2c_generic_sw_engine
);
451 if (i2caux
->aux_engines
[i
])
452 i2caux
->aux_engines
[i
]->funcs
->destroy(
453 &i2caux
->aux_engines
[i
]);
455 if (i2caux
->i2c_hw_engines
[i
])
456 i2caux
->i2c_hw_engines
[i
]->funcs
->destroy(
457 &i2caux
->i2c_hw_engines
[i
]);
459 if (i2caux
->i2c_sw_engines
[i
])
460 i2caux
->i2c_sw_engines
[i
]->funcs
->destroy(
461 &i2caux
->i2c_sw_engines
[i
]);
464 } while (i
< GPIO_DDC_LINE_COUNT
);