blob: 8fac6f8910eb1c1097353652ffbfbe4ceb1efec9 [file] [log] [blame]
Ronald G. Minnich2a66d6b2013-03-28 17:01:43 -07001/*
2 * Copyright 2013 Google Inc.
3 * Copyright © 2008 Intel Corporation
4 *
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 * Authors:
26 * Keith Packard <keithp@keithp.com>
27 *
28 */
29
30#include <console/console.h>
31#include <stdint.h>
32#include <delay.h>
33#include "i915io.h"
34
35u32
36pack_aux(u32 *src32, int src_bytes)
37{
38 u8 *src = (u8 *)src32;
39 int i;
40 u32 v = 0;
41
42 if (src_bytes > 4)
43 src_bytes = 4;
44 for (i = 0; i < src_bytes; i++)
45 v |= ((u32) src[i]) << ((3-i) * 8);
46 return v;
47}
48
49void
Ronald G. Minnich4f78b182013-04-17 16:57:30 -070050unpack_aux(u32 src, u8 *dst, int dst_bytes)
Ronald G. Minnich2a66d6b2013-03-28 17:01:43 -070051{
Ronald G. Minnich2a66d6b2013-03-28 17:01:43 -070052
53 int i;
54 if (dst_bytes > 4)
55 dst_bytes = 4;
56 for (i = 0; i < dst_bytes; i++)
57 dst[i] = src >> ((3-i) * 8);
58}
59
60int
61intel_dp_aux_ch(u32 ch_ctl, u32 ch_data, u32 *send, int send_bytes,
Ronald G. Minnich4f78b182013-04-17 16:57:30 -070062 u8 *recv, int recv_size)
Ronald G. Minnich2a66d6b2013-03-28 17:01:43 -070063{
64 int i;
65 int recv_bytes;
66 u32 status;
67 u32 aux_clock_divider;
68 int try, precharge = 5;
69
70 /* The clock divider is based off the hrawclk,
71 * and would like to run at 2MHz. So, take the
72 * hrawclk value and divide by 2 and use that
73 *
74 * Note that PCH attached eDP panels should use a 125MHz input
75 * clock divider.
76 */
77 /* 200 on link */
78 aux_clock_divider = 200; /* SNB & IVB eDP input clock at 400Mhz */
79
80 /* Try to wait for any previous AUX channel activity */
81 for (try = 0; try < 3; try++) {
82 status = io_i915_READ32(ch_ctl);
83 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
84 break;
85 udelay(1000);
86 }
87
88 if (try == 3) {
89 printk(BIOS_SPEW, "[000000.0] [drm:%s], ", __func__);
90 printk(BIOS_SPEW, "dp_aux_ch not started status 0x%08lx\n",
91 io_i915_READ32(ch_ctl));
92 return -1;
93 }
94
95 /* Must try at least 3 times according to DP spec */
96 for (try = 0; try < 5; try++) {
97 /* Load the send data into the aux channel data registers */
98 for (i = 0; i < send_bytes; i += 4)
99 io_i915_WRITE32(send[i], ch_data + i);
100
101 /* Send the command and wait for it to complete */
102 io_i915_WRITE32(
103 DP_AUX_CH_CTL_SEND_BUSY |
104 DP_AUX_CH_CTL_TIME_OUT_400us |
105 (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
106 (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
107 (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
108 DP_AUX_CH_CTL_DONE |
109 DP_AUX_CH_CTL_TIME_OUT_ERROR |
110 DP_AUX_CH_CTL_RECEIVE_ERROR, ch_ctl);
111 for (;;) {
112 status = io_i915_READ32(ch_ctl);
113 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
114 break;
115 udelay(100);
116 }
117
118 /* Clear done status and any errors */
119 io_i915_WRITE32(
120 status |
121 DP_AUX_CH_CTL_DONE |
122 DP_AUX_CH_CTL_TIME_OUT_ERROR |
123 DP_AUX_CH_CTL_RECEIVE_ERROR, ch_ctl);
124
125 if (status & (DP_AUX_CH_CTL_TIME_OUT_ERROR |
126 DP_AUX_CH_CTL_RECEIVE_ERROR))
127 continue;
128 if (status & DP_AUX_CH_CTL_DONE)
129 break;
130 }
131
132 if ((status & DP_AUX_CH_CTL_DONE) == 0) {
133 printk(BIOS_SPEW, "[000000.0] [drm:%s], ", __func__);
134 printk(BIOS_SPEW, "dp_aux_ch not done status 0x%08x\n", status);
135 return -1;
136 }
137
138 /* Check for timeout or receive error.
139 * Timeouts occur when the sink is not connected
140 */
141 if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
142 printk(BIOS_SPEW, "[000000.0] [drm:%s], ", __func__);
143 printk(BIOS_SPEW, "dp_aux_ch receive error status 0x%08x\n", status);
144 return -1;
145 }
146
147 /* Timeouts occur when the device isn't connected, so they're
148 * "normal" -- don't fill the kernel log with these */
149 if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
150 printk(BIOS_SPEW, "[000000.0] [drm:%s], ", __func__);
151 printk(BIOS_SPEW, "dp_aux_ch timeout status 0x%08x\n", status);
152 return -1;
153 }
154
155 /* Unload any bytes sent back from the other side */
156 recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
157 DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
158 if (recv_bytes > recv_size)
159 recv_bytes = recv_size;
160
161 for (i = 0; i < recv_bytes; i += 4)
162 unpack_aux(io_i915_READ32(ch_data + i),
163 recv + i, recv_bytes - i);
164
165 return recv_bytes;
166}
167