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 "../engine.h"
33 #include "../i2c_engine.h"
34 #include "../i2c_hw_engine.h"
35 #include "../i2c_generic_hw_engine.h"
40 #include "i2c_hw_engine_dce80.h"
43 * Post-requisites: headers required by this unit
46 #include "dce/dce_8_0_d.h"
47 #include "dce/dce_8_0_sh_mask.h"
53 DC_I2C_STATUS__DC_I2C_STATUS_IDLE,
54 DC_I2C_STATUS__DC_I2C_STATUS_USED_BY_SW,
55 DC_I2C_STATUS__DC_I2C_STATUS_USED_BY_HW
58 enum dc_i2c_arbitration {
59 DC_I2C_ARBITRATION__DC_I2C_SW_PRIORITY_NORMAL,
60 DC_I2C_ARBITRATION__DC_I2C_SW_PRIORITY_HIGH
65 * (timeout implemented in SW by querying status) */
66 I2C_SETUP_TIME_LIMIT = 255,
67 I2C_HW_BUFFER_SIZE = 144
72 * Cast 'struct i2c_hw_engine *'
73 * to 'struct i2c_hw_engine_dce80 *'
75 #define FROM_I2C_HW_ENGINE(ptr) \
76 container_of((ptr), struct i2c_hw_engine_dce80, base)
80 * Cast pointer to 'struct i2c_engine *'
81 * to pointer to 'struct i2c_hw_engine_dce80 *'
83 #define FROM_I2C_ENGINE(ptr) \
84 FROM_I2C_HW_ENGINE(container_of((ptr), struct i2c_hw_engine, base))
88 * Cast pointer to 'struct engine *'
89 * to 'pointer to struct i2c_hw_engine_dce80 *'
91 #define FROM_ENGINE(ptr) \
92 FROM_I2C_ENGINE(container_of((ptr), struct i2c_engine, base))
94 static void disable_i2c_hw_engine(
95 struct i2c_hw_engine_dce80 *engine)
97 const uint32_t addr = engine->addr.DC_I2C_DDCX_SETUP;
100 struct dc_context *ctx = NULL;
102 ctx = engine->base.base.base.ctx;
104 value = dm_read_reg(ctx, addr);
112 dm_write_reg(ctx, addr, value);
115 static void release_engine(
116 struct engine *engine)
118 struct i2c_hw_engine_dce80 *hw_engine = FROM_ENGINE(engine);
120 struct i2c_engine *base = NULL;
124 base = &hw_engine->base.base;
126 /* Restore original HW engine speed */
128 base->funcs->set_speed(base, hw_engine->base.original_speed);
132 value = dm_read_reg(engine->ctx, mmDC_I2C_ARBITRATION);
138 DC_I2C_SW_DONE_USING_I2C_REG);
140 dm_write_reg(engine->ctx, mmDC_I2C_ARBITRATION, value);
143 /* Reset HW engine */
145 uint32_t i2c_sw_status = 0;
147 value = dm_read_reg(engine->ctx, mmDC_I2C_SW_STATUS);
149 i2c_sw_status = get_reg_field_value(
153 /* if used by SW, safe to reset */
154 safe_to_reset = (i2c_sw_status == 1);
157 value = dm_read_reg(engine->ctx, mmDC_I2C_CONTROL);
170 DC_I2C_SW_STATUS_RESET);
172 dm_write_reg(engine->ctx, mmDC_I2C_CONTROL, value);
175 /* HW I2c engine - clock gating feature */
176 if (!hw_engine->engine_keep_power_up_count)
177 disable_i2c_hw_engine(hw_engine);
180 static void destruct(
181 struct i2c_hw_engine_dce80 *engine)
183 dal_i2c_hw_engine_destruct(&engine->base);
187 struct i2c_engine **i2c_engine)
189 struct i2c_hw_engine_dce80 *engine = FROM_I2C_ENGINE(*i2c_engine);
198 static bool setup_engine(
199 struct i2c_engine *i2c_engine)
202 struct i2c_hw_engine_dce80 *engine = FROM_I2C_ENGINE(i2c_engine);
204 /* Program pin select */
206 const uint32_t addr = mmDC_I2C_CONTROL;
208 value = dm_read_reg(i2c_engine->base.ctx, addr);
232 DC_I2C_SW_STATUS_RESET);
238 DC_I2C_TRANSACTION_COUNT);
246 dm_write_reg(i2c_engine->base.ctx, addr, value);
249 /* Program time limit */
251 const uint32_t addr = engine->addr.DC_I2C_DDCX_SETUP;
253 value = dm_read_reg(i2c_engine->base.ctx, addr);
257 I2C_SETUP_TIME_LIMIT,
259 DC_I2C_DDC1_TIME_LIMIT);
267 dm_write_reg(i2c_engine->base.ctx, addr, value);
270 /* Program HW priority
271 * set to High - interrupt software I2C at any time
272 * Enable restart of SW I2C that was interrupted by HW
273 * disable queuing of software while I2C is in use by HW */
275 value = dm_read_reg(i2c_engine->base.ctx,
276 mmDC_I2C_ARBITRATION);
282 DC_I2C_NO_QUEUED_SW_GO);
286 DC_I2C_ARBITRATION__DC_I2C_SW_PRIORITY_NORMAL,
290 dm_write_reg(i2c_engine->base.ctx,
291 mmDC_I2C_ARBITRATION, value);
297 static uint32_t get_speed(
298 const struct i2c_engine *i2c_engine)
300 const struct i2c_hw_engine_dce80 *engine = FROM_I2C_ENGINE(i2c_engine);
302 const uint32_t addr = engine->addr.DC_I2C_DDCX_SPEED;
304 uint32_t pre_scale = 0;
306 uint32_t value = dm_read_reg(i2c_engine->base.ctx, addr);
308 pre_scale = get_reg_field_value(
311 DC_I2C_DDC1_PRESCALE);
313 /* [anaumov] it seems following is unnecessary */
314 /*ASSERT(value.bits.DC_I2C_DDC1_PRESCALE);*/
317 engine->reference_frequency / pre_scale :
318 engine->base.default_speed;
321 static void set_speed(
322 struct i2c_engine *i2c_engine,
325 struct i2c_hw_engine_dce80 *engine = FROM_I2C_ENGINE(i2c_engine);
328 const uint32_t addr = engine->addr.DC_I2C_DDCX_SPEED;
330 uint32_t value = dm_read_reg(i2c_engine->base.ctx, addr);
334 engine->reference_frequency / speed,
336 DC_I2C_DDC1_PRESCALE);
342 DC_I2C_DDC1_THRESHOLD);
344 dm_write_reg(i2c_engine->base.ctx, addr, value);
348 static inline void reset_hw_engine(struct engine *engine)
350 uint32_t value = dm_read_reg(engine->ctx, mmDC_I2C_CONTROL);
362 DC_I2C_SW_STATUS_RESET);
364 dm_write_reg(engine->ctx, mmDC_I2C_CONTROL, value);
367 static bool is_hw_busy(struct engine *engine)
369 uint32_t i2c_sw_status = 0;
371 uint32_t value = dm_read_reg(engine->ctx, mmDC_I2C_SW_STATUS);
373 i2c_sw_status = get_reg_field_value(
378 if (i2c_sw_status == DC_I2C_STATUS__DC_I2C_STATUS_IDLE)
381 reset_hw_engine(engine);
383 value = dm_read_reg(engine->ctx, mmDC_I2C_SW_STATUS);
385 i2c_sw_status = get_reg_field_value(
390 return i2c_sw_status != DC_I2C_STATUS__DC_I2C_STATUS_IDLE;
395 * DC_GPIO_DDC MM register offsets
397 static const uint32_t transaction_addr[] = {
398 mmDC_I2C_TRANSACTION0,
399 mmDC_I2C_TRANSACTION1,
400 mmDC_I2C_TRANSACTION2,
401 mmDC_I2C_TRANSACTION3
404 static bool process_transaction(
405 struct i2c_hw_engine_dce80 *engine,
406 struct i2c_request_transaction_data *request)
408 uint32_t length = request->length;
409 uint8_t *buffer = request->data;
411 bool last_transaction = false;
414 struct dc_context *ctx = NULL;
416 ctx = engine->base.base.base.ctx;
419 const uint32_t addr =
420 transaction_addr[engine->transaction_count];
422 value = dm_read_reg(ctx, addr);
428 DC_I2C_STOP_ON_NACK0);
436 if ((engine->transaction_count == 3) ||
437 (request->action == I2CAUX_TRANSACTION_ACTION_I2C_WRITE) ||
438 (request->action & I2CAUX_TRANSACTION_ACTION_I2C_READ)) {
446 last_transaction = true;
456 (0 != (request->action &
457 I2CAUX_TRANSACTION_ACTION_I2C_READ)),
467 dm_write_reg(ctx, addr, value);
470 /* Write the I2C address and I2C data
471 * into the hardware circular buffer, one byte per entry.
472 * As an example, the 7-bit I2C slave address for CRT monitor
473 * for reading DDC/EDID information is 0b1010001.
474 * For an I2C send operation, the LSB must be programmed to 0;
475 * for I2C receive operation, the LSB must be programmed to 1. */
492 if (engine->transaction_count == 0) {
499 /*enable index write*/
507 dm_write_reg(ctx, mmDC_I2C_DATA, value);
509 if (!(request->action & I2CAUX_TRANSACTION_ACTION_I2C_READ)) {
525 dm_write_reg(ctx, mmDC_I2C_DATA, value);
531 ++engine->transaction_count;
532 engine->buffer_used_bytes += length + 1;
534 return last_transaction;
537 static void execute_transaction(
538 struct i2c_hw_engine_dce80 *engine)
541 struct dc_context *ctx = NULL;
543 ctx = engine->base.base.base.ctx;
546 const uint32_t addr = engine->addr.DC_I2C_DDCX_SETUP;
548 value = dm_read_reg(ctx, addr);
554 DC_I2C_DDC1_DATA_DRIVE_EN);
560 DC_I2C_DDC1_CLK_DRIVE_EN);
566 DC_I2C_DDC1_DATA_DRIVE_SEL);
572 DC_I2C_DDC1_INTRA_TRANSACTION_DELAY);
578 DC_I2C_DDC1_INTRA_BYTE_DELAY);
580 dm_write_reg(ctx, addr, value);
584 const uint32_t addr = mmDC_I2C_CONTROL;
586 value = dm_read_reg(ctx, addr);
598 DC_I2C_SW_STATUS_RESET);
614 engine->transaction_count - 1,
616 DC_I2C_TRANSACTION_COUNT);
618 dm_write_reg(ctx, addr, value);
621 /* start I2C transfer */
623 const uint32_t addr = mmDC_I2C_CONTROL;
625 value = dm_read_reg(ctx, addr);
633 dm_write_reg(ctx, addr, value);
636 /* all transactions were executed and HW buffer became empty
637 * (even though it actually happens when status becomes DONE) */
638 engine->transaction_count = 0;
639 engine->buffer_used_bytes = 0;
642 static void submit_channel_request(
643 struct i2c_engine *engine,
644 struct i2c_request_transaction_data *request)
646 request->status = I2C_CHANNEL_OPERATION_SUCCEEDED;
648 if (!process_transaction(FROM_I2C_ENGINE(engine), request))
651 if (is_hw_busy(&engine->base)) {
652 request->status = I2C_CHANNEL_OPERATION_ENGINE_BUSY;
656 execute_transaction(FROM_I2C_ENGINE(engine));
659 static void process_channel_reply(
660 struct i2c_engine *engine,
661 struct i2c_reply_transaction_data *reply)
663 uint32_t length = reply->length;
664 uint8_t *buffer = reply->data;
687 dm_write_reg(engine->base.ctx, mmDC_I2C_DATA, value);
690 /* after reading the status,
691 * if the I2C operation executed successfully
692 * (i.e. DC_I2C_STATUS_DONE = 1) then the I2C controller
693 * should read data bytes from I2C circular data buffer */
695 value = dm_read_reg(engine->base.ctx, mmDC_I2C_DATA);
697 *buffer++ = get_reg_field_value(
706 static enum i2c_channel_operation_result get_channel_status(
707 struct i2c_engine *engine,
708 uint8_t *returned_bytes)
710 uint32_t i2c_sw_status = 0;
711 uint32_t value = dm_read_reg(engine->base.ctx, mmDC_I2C_SW_STATUS);
713 i2c_sw_status = get_reg_field_value(
718 if (i2c_sw_status == DC_I2C_STATUS__DC_I2C_STATUS_USED_BY_SW)
719 return I2C_CHANNEL_OPERATION_ENGINE_BUSY;
720 else if (value & DC_I2C_SW_STATUS__DC_I2C_SW_STOPPED_ON_NACK_MASK)
721 return I2C_CHANNEL_OPERATION_NO_RESPONSE;
722 else if (value & DC_I2C_SW_STATUS__DC_I2C_SW_TIMEOUT_MASK)
723 return I2C_CHANNEL_OPERATION_TIMEOUT;
724 else if (value & DC_I2C_SW_STATUS__DC_I2C_SW_ABORTED_MASK)
725 return I2C_CHANNEL_OPERATION_FAILED;
726 else if (value & DC_I2C_SW_STATUS__DC_I2C_SW_DONE_MASK)
727 return I2C_CHANNEL_OPERATION_SUCCEEDED;
730 * this is the case when HW used for communication, I2C_SW_STATUS
733 return I2C_CHANNEL_OPERATION_SUCCEEDED;
736 static uint32_t get_hw_buffer_available_size(
737 const struct i2c_hw_engine *engine)
739 return I2C_HW_BUFFER_SIZE -
740 FROM_I2C_HW_ENGINE(engine)->buffer_used_bytes;
743 static uint32_t get_transaction_timeout(
744 const struct i2c_hw_engine *engine,
747 uint32_t speed = engine->base.funcs->get_speed(&engine->base);
749 uint32_t period_timeout;
750 uint32_t num_of_clock_stretches;
755 period_timeout = (1000 * TRANSACTION_TIMEOUT_IN_I2C_CLOCKS) / speed;
757 num_of_clock_stretches = 1 + (length << 3) + 1;
758 num_of_clock_stretches +=
759 (FROM_I2C_HW_ENGINE(engine)->buffer_used_bytes << 3) +
760 (FROM_I2C_HW_ENGINE(engine)->transaction_count << 1);
762 return period_timeout * num_of_clock_stretches;
767 * DC_I2C_DDC1_SETUP MM register offsets
770 * The indices of this offset array are DDC engine IDs
772 static const int32_t ddc_setup_offset[] = {
774 mmDC_I2C_DDC1_SETUP - mmDC_I2C_DDC1_SETUP, /* DDC Engine 1 */
775 mmDC_I2C_DDC2_SETUP - mmDC_I2C_DDC1_SETUP, /* DDC Engine 2 */
776 mmDC_I2C_DDC3_SETUP - mmDC_I2C_DDC1_SETUP, /* DDC Engine 3 */
777 mmDC_I2C_DDC4_SETUP - mmDC_I2C_DDC1_SETUP, /* DDC Engine 4 */
778 mmDC_I2C_DDC5_SETUP - mmDC_I2C_DDC1_SETUP, /* DDC Engine 5 */
779 mmDC_I2C_DDC6_SETUP - mmDC_I2C_DDC1_SETUP, /* DDC Engine 6 */
780 mmDC_I2C_DDCVGA_SETUP - mmDC_I2C_DDC1_SETUP /* DDC Engine 7 */
785 * DC_I2C_DDC1_SPEED MM register offsets
788 * The indices of this offset array are DDC engine IDs
790 static const int32_t ddc_speed_offset[] = {
791 mmDC_I2C_DDC1_SPEED - mmDC_I2C_DDC1_SPEED, /* DDC Engine 1 */
792 mmDC_I2C_DDC2_SPEED - mmDC_I2C_DDC1_SPEED, /* DDC Engine 2 */
793 mmDC_I2C_DDC3_SPEED - mmDC_I2C_DDC1_SPEED, /* DDC Engine 3 */
794 mmDC_I2C_DDC4_SPEED - mmDC_I2C_DDC1_SPEED, /* DDC Engine 4 */
795 mmDC_I2C_DDC5_SPEED - mmDC_I2C_DDC1_SPEED, /* DDC Engine 5 */
796 mmDC_I2C_DDC6_SPEED - mmDC_I2C_DDC1_SPEED, /* DDC Engine 6 */
797 mmDC_I2C_DDCVGA_SPEED - mmDC_I2C_DDC1_SPEED /* DDC Engine 7 */
800 static const struct i2c_engine_funcs i2c_engine_funcs = {
802 .get_speed = get_speed,
803 .set_speed = set_speed,
804 .setup_engine = setup_engine,
805 .submit_channel_request = submit_channel_request,
806 .process_channel_reply = process_channel_reply,
807 .get_channel_status = get_channel_status,
808 .acquire_engine = dal_i2c_hw_engine_acquire_engine,
811 static const struct engine_funcs engine_funcs = {
812 .release_engine = release_engine,
813 .get_engine_type = dal_i2c_hw_engine_get_engine_type,
814 .acquire = dal_i2c_engine_acquire,
815 .submit_request = dal_i2c_hw_engine_submit_request,
818 static const struct i2c_hw_engine_funcs i2c_hw_engine_funcs = {
819 .get_hw_buffer_available_size =
820 get_hw_buffer_available_size,
821 .get_transaction_timeout =
822 get_transaction_timeout,
823 .wait_on_operation_result =
824 dal_i2c_hw_engine_wait_on_operation_result,
827 static void construct(
828 struct i2c_hw_engine_dce80 *engine,
829 const struct i2c_hw_engine_dce80_create_arg *arg)
831 dal_i2c_hw_engine_construct(&engine->base, arg->ctx);
833 engine->base.base.base.funcs = &engine_funcs;
834 engine->base.base.funcs = &i2c_engine_funcs;
835 engine->base.funcs = &i2c_hw_engine_funcs;
836 engine->base.default_speed = arg->default_speed;
837 engine->addr.DC_I2C_DDCX_SETUP =
838 mmDC_I2C_DDC1_SETUP + ddc_setup_offset[arg->engine_id];
839 engine->addr.DC_I2C_DDCX_SPEED =
840 mmDC_I2C_DDC1_SPEED + ddc_speed_offset[arg->engine_id];
842 engine->engine_id = arg->engine_id;
843 engine->reference_frequency = arg->reference_frequency;
844 engine->buffer_used_bytes = 0;
845 engine->transaction_count = 0;
846 engine->engine_keep_power_up_count = 1;
849 struct i2c_engine *dal_i2c_hw_engine_dce80_create(
850 const struct i2c_hw_engine_dce80_create_arg *arg)
852 struct i2c_hw_engine_dce80 *engine;
859 if ((arg->engine_id >= sizeof(ddc_setup_offset) / sizeof(int32_t)) ||
860 (arg->engine_id >= sizeof(ddc_speed_offset) / sizeof(int32_t)) ||
861 !arg->reference_frequency) {
866 engine = kzalloc(sizeof(struct i2c_hw_engine_dce80), GFP_KERNEL);
873 construct(engine, arg);
874 return &engine->base.base;