2 *-----------------------------------------------------------------------------
3 * Filename: i2c_bitbash_tnc.c
5 *-----------------------------------------------------------------------------
6 * Copyright (c) 2002-2010, Intel Corporation.
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 *-----------------------------------------------------------------------------
28 * Bitbash to read GPIO pins are done on LPC device 0:31:0. So do not
29 * use EMGD_READ32 and EMGD_WRITE32 macros for read/write to device 31. These
30 * macros do mmio only on device2.
31 * To properly read/write mmio on device 31, use
32 * READ_MMIO_REG_TNC(port_type, reg) and
33 * WRITE_MMIO_REG_TNC(port_type, reg, data).
34 * These macros properly set to work for all OSes including VBIOS, but
35 * generate more code compared to EMGD_READ32 and EMGD_WRITE32. So use as
37 *-----------------------------------------------------------------------------
40 #define MODULE_NAME hal.dpd
57 #include "../cmn/i2c_dispatch.h"
60 * @addtogroup display_group
64 static int i2c_error_recovery_tnc(
65 unsigned long hold_time);
67 static int i2c_write_byte_tnc(
69 unsigned long hold_time);
71 static int i2c_read_byte_tnc(
74 unsigned long hold_time);
76 int i2c_read_regs_gpio(
77 igd_context_t *context,
78 unsigned long i2c_bus,
79 unsigned long i2c_speed,
82 unsigned char FAR *buffer,
83 unsigned long num_bytes,
86 int i2c_write_reg_list_gpio(
87 igd_context_t *context,
88 unsigned long i2c_bus,
89 unsigned long i2c_speed,
94 /* The LVDS GPIO clock lines are GPIOSUS[3]
95 * The LVDS GPIO data lines are GPIOSUS[4]
97 #define GPIO_CLOCK 0x08
98 #define GPIO_DATA 0x10
107 static void enable_gpio_tnc(igd_context_t *context)
110 * NOTE: This really should be a system BIOS job.
111 * The driver would not touch these register anymore since
112 * it would cause the 13x7 panel fail to start.
115 /* Enabling LVDS Data and LVDS Clock */
118 temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGEN);
119 temp |= (GPIO_DATA | GPIO_CLOCK);
120 WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGEN, temp);
133 static void i2c_get(unsigned long *clock,
137 /* Set Data as Input */
138 temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGIO);
140 WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGIO, temp);
142 *data = (READ_MMIO_REG_TNC(IGD_PORT_LPC, RGLVL) & GPIO_DATA) ? 1:0;
143 *clock = (READ_MMIO_REG_TNC(IGD_PORT_LPC, RGLVL) & GPIO_CLOCK) ? 1:0;
146 EMGD_WRITE32(0, EMGD_MMIO(mmio) + i2c_bus);
147 c = EMGD_READ32(EMGD_MMIO(mmio) + i2c_bus)>>4;
160 static void i2c_set_data(int data,
161 unsigned long hold_time)
164 /* The LVDS GPIO data lines are GPIOSUS[4] */
166 temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGIO);
168 WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGIO, temp);
169 /* Read status register */
170 temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGLVL);
173 /* Set level to High */
176 /* Set level to low */
179 WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGLVL, temp);
184 /* Implementation using Display GPIO
185 * For alm, the default data value "could" be 0
188 * Simplified definition for the bits
189 * 11: GPIO data Value
191 * 9: GPIO Data Direction Value
192 * 8: GPIO Data Direction Mask
194 EMGD_WRITE32(data ? 0x500 : 0x700, EMGD_MMIO(mmio) + i2c_bus);
195 EMGD_WRITE32(data ? 0x400 : 0x600, EMGD_MMIO(mmio) + i2c_bus);
207 static void i2c_set_clock(int clock,
208 unsigned long hold_time)
211 /* The LVDS GPIO clock lines are GPIOSUS[3] */
213 temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGIO);
215 WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGIO, temp);
216 /* Read Status Register */
217 temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGLVL);
220 /* Set level to High */
224 /* Set level to low */
227 WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGLVL, temp);
233 * Simplified definition for the bits
234 * 3: GPIO Clock Value
236 * 1: GPIO Clock Direction Value
237 * 0: GPIO Clock Direction Mask
240 EMGD_WRITE32(clock ? 0x5 : 0x7, EMGD_MMIO(mmio) + i2c_bus);
241 EMGD_WRITE32(clock ? 0x4 : 0x6, EMGD_MMIO(mmio) + i2c_bus);
250 * @return 0 on success
251 * @return 1 on failure
253 static int i2c_start_tnc(unsigned long hold_time)
255 unsigned long sc, sd;
258 i2c_set_data(1, hold_time);
261 i2c_set_clock(1, hold_time);
263 /* Start condition happens when sd goes high to low when sc is high */
268 i2c_error_recovery_tnc(hold_time);
272 i2c_set_data(0, hold_time);
273 i2c_set_clock(0, hold_time);
276 } /* end i2c_start */
284 static int i2c_stop_tnc(unsigned long hold_time)
286 /* Stop condition happens when sd goes low to high when sc is high */
289 i2c_set_clock(0, hold_time);
290 i2c_set_data(0, hold_time);
292 i2c_set_clock(1, hold_time);
295 /* Try another time */
297 i2c_set_clock(1, hold_time);
299 i2c_set_data(1, hold_time);
308 * @return 0 on success
309 * @return 1 on failure
311 static int i2c_error_recovery_tnc(unsigned long hold_time)
313 unsigned char max_retries = 9;
314 unsigned long sc, sd;
316 while (max_retries--) {
318 if (sd == 1 && sc == 1) {
321 i2c_stop_tnc(hold_time);
324 EMGD_ERROR("Cannot recover I2C error.");
334 * @return 0 on success
335 * @return 1 on failure
337 static int i2c_write_byte_tnc(unsigned char value,
338 unsigned long hold_time)
343 /* I2C_DEBUG("i2c_write_byte"); */
344 for(i=7; i>=0; i--) {
345 i2c_set_clock(0, hold_time);
346 i2c_set_data(value>>i & 1, hold_time);
348 i2c_set_clock(1, hold_time);
352 i2c_set_clock(0, hold_time);
353 /* Set data low. Possible inteference in some lvds panel */
354 i2c_set_data(0, hold_time);
355 i2c_set_clock(1, hold_time);
360 i2c_set_clock(0, hold_time);
363 EMGD_ERROR("No ACK for byte 0x%x", value);
364 i2c_error_recovery_tnc(hold_time);
370 } /* end i2c_write_byte */
378 * @return 0 on success
379 * @return 1 on failure
381 static int i2c_read_byte_tnc(unsigned char *value,
383 unsigned long hold_time)
386 unsigned long sc, sd, temp;
389 for(i=7; i>=0; i--) {
390 i2c_set_clock(1, hold_time);
394 EMGD_DEBUG("Clock low on read %d", i);
395 i2c_error_recovery_tnc(hold_time);
398 *value |= (sd & 1)<<i;
399 i2c_set_clock(0, hold_time);
403 i2c_set_data(0, hold_time);
406 /* Master does not ACK */
407 i2c_set_clock(1, hold_time);
408 i2c_set_clock(0, hold_time);
411 /* Set data as input as we continue to read */
412 temp = READ_MMIO_REG_TNC(IGD_PORT_LPC, RGIO);
414 WRITE_MMIO_REG_TNC(IGD_PORT_LPC, RGIO, temp);
418 } /* end i2c_read_byte */
430 * @return 0 on success
431 * @return 1 on failure
433 int i2c_read_regs_gpio(igd_context_t *context,
434 unsigned long i2c_bus,
435 unsigned long i2c_speed,
438 unsigned char FAR *buffer,
439 unsigned long num_bytes,
443 unsigned long hold_time;
447 EMGD_DEBUG("i2c Speed failed.");
452 * We are holding the clock LOW for "hold_time" and then HIGH for
453 * "hold_time". Therefore, we double the clock speed in this calculation.
455 if (flags & IGD_I2C_WRITE_FW){
458 hold_time = 1000/(i2c_speed * 2);
461 /* enable_gpio_tnc(context); */
463 if (i2c_start_tnc(hold_time)) {
464 EMGD_DEBUG("i2c Start failed.");
468 if (i2c_write_byte_tnc((unsigned char)dab & 0xFE,
470 EMGD_DEBUG("i2c DAB(W) failed.");
474 if (i2c_write_byte_tnc(reg, hold_time)) {
475 EMGD_DEBUG("RAB failed.");
479 if (i2c_start_tnc(hold_time)) {
480 EMGD_DEBUG("i2c ReStart failed");
484 if (i2c_write_byte_tnc((unsigned char)dab | 0x01,
486 EMGD_ERROR("i2c DAB(R) failed");
491 /* Read the requested number of bytes */
492 for(i=0; i<(int)(num_bytes-1); i++) {
494 * Use a local temp so that the FAR pointer doesn't have to
497 if (i2c_read_byte_tnc(&temp, 1, hold_time)) {
498 EMGD_DEBUG("Read data byte %d failed", i);
499 EMGD_DEBUG("Exit i2c_read_regs_tnc with error");
505 /* No ACK on the last read */
506 if(i2c_read_byte_tnc(&temp, 0, hold_time)) {
507 EMGD_DEBUG("Read Data %d Failed", i);
508 EMGD_DEBUG("Exit i2c_read_regs_tnc with error");
513 i2c_stop_tnc(hold_time);
514 i2c_stop_tnc(hold_time);
528 * @return 0 on success
529 * @return 1 on failure
531 int i2c_write_reg_list_gpio(igd_context_t *context,
532 unsigned long i2c_bus,
533 unsigned long i2c_speed,
538 unsigned long hold_time;
545 * We are holding the clock LOW for "hold_time" and then HIGH for
546 * "hold_time". Therefore, we double the clock speed in this calculation.
548 if (flags & IGD_I2C_WRITE_FW){
551 hold_time = 1000/(i2c_speed * 2);
554 /* enable_gpio_tnc(context); */
556 while(reg_list->reg != PD_REG_LIST_END) {
557 if (i2c_start_tnc(hold_time)) {
558 EMGD_DEBUG("Start failed");
562 if (i2c_write_byte_tnc((unsigned char)dab & 0xFE,
564 EMGD_DEBUG("DAB(W) failed");
568 /* Register Address */
569 if (i2c_write_byte_tnc((unsigned char)reg_list->reg, hold_time)) {
570 EMGD_DEBUG("RAB failed");
576 if (i2c_write_byte_tnc((unsigned char)reg_list->value, hold_time)) {
577 EMGD_DEBUG("Data failed");
581 if(reg_list[1].reg != (reg_list[0].reg + 1)) {
586 EMGD_DEBUG("I2C Multi-Write Reg[%x] = 0x%x",
587 (unsigned short)reg_list->reg,
588 (unsigned short)reg_list->value);
590 } while(flags & IGD_I2C_SERIAL_WRITE);
593 i2c_stop_tnc(hold_time);
594 i2c_stop_tnc(hold_time);