Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
aux.c
Go to the documentation of this file.
1 /*
2  * Copyright 2009 Red Hat Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Authors: Ben Skeggs
23  */
24 
25 #include <subdev/i2c.h>
26 
27 /******************************************************************************
28  * aux channel util functions
29  *****************************************************************************/
30 #define AUX_DBG(fmt, args...) nv_debug(aux, "AUXCH(%d): " fmt, ch, ##args)
31 #define AUX_ERR(fmt, args...) nv_error(aux, "AUXCH(%d): " fmt, ch, ##args)
32 
33 static void
34 auxch_fini(struct nouveau_i2c *aux, int ch)
35 {
36  nv_mask(aux, 0x00e4e4 + (ch * 0x50), 0x00310000, 0x00000000);
37 }
38 
39 static int
40 auxch_init(struct nouveau_i2c *aux, int ch)
41 {
42  const u32 unksel = 1; /* nfi which to use, or if it matters.. */
43  const u32 ureq = unksel ? 0x00100000 : 0x00200000;
44  const u32 urep = unksel ? 0x01000000 : 0x02000000;
45  u32 ctrl, timeout;
46 
47  /* wait up to 1ms for any previous transaction to be done... */
48  timeout = 1000;
49  do {
50  ctrl = nv_rd32(aux, 0x00e4e4 + (ch * 0x50));
51  udelay(1);
52  if (!timeout--) {
53  AUX_ERR("begin idle timeout 0x%08x", ctrl);
54  return -EBUSY;
55  }
56  } while (ctrl & 0x03010000);
57 
58  /* set some magic, and wait up to 1ms for it to appear */
59  nv_mask(aux, 0x00e4e4 + (ch * 0x50), 0x00300000, ureq);
60  timeout = 1000;
61  do {
62  ctrl = nv_rd32(aux, 0x00e4e4 + (ch * 0x50));
63  udelay(1);
64  if (!timeout--) {
65  AUX_ERR("magic wait 0x%08x\n", ctrl);
66  auxch_fini(aux, ch);
67  return -EBUSY;
68  }
69  } while ((ctrl & 0x03000000) != urep);
70 
71  return 0;
72 }
73 
74 static int
75 auxch_tx(struct nouveau_i2c *aux, int ch, u8 type, u32 addr, u8 *data, u8 size)
76 {
77  u32 ctrl, stat, timeout, retries;
78  u32 xbuf[4] = {};
79  int ret, i;
80 
81  AUX_DBG("%d: 0x%08x %d\n", type, addr, size);
82 
83  ret = auxch_init(aux, ch);
84  if (ret)
85  goto out;
86 
87  stat = nv_rd32(aux, 0x00e4e8 + (ch * 0x50));
88  if (!(stat & 0x10000000)) {
89  AUX_DBG("sink not detected\n");
90  ret = -ENXIO;
91  goto out;
92  }
93 
94  if (!(type & 1)) {
95  memcpy(xbuf, data, size);
96  for (i = 0; i < 16; i += 4) {
97  AUX_DBG("wr 0x%08x\n", xbuf[i / 4]);
98  nv_wr32(aux, 0x00e4c0 + (ch * 0x50) + i, xbuf[i / 4]);
99  }
100  }
101 
102  ctrl = nv_rd32(aux, 0x00e4e4 + (ch * 0x50));
103  ctrl &= ~0x0001f0ff;
104  ctrl |= type << 12;
105  ctrl |= size - 1;
106  nv_wr32(aux, 0x00e4e0 + (ch * 0x50), addr);
107 
108  /* retry transaction a number of times on failure... */
109  ret = -EREMOTEIO;
110  for (retries = 0; retries < 32; retries++) {
111  /* reset, and delay a while if this is a retry */
112  nv_wr32(aux, 0x00e4e4 + (ch * 0x50), 0x80000000 | ctrl);
113  nv_wr32(aux, 0x00e4e4 + (ch * 0x50), 0x00000000 | ctrl);
114  if (retries)
115  udelay(400);
116 
117  /* transaction request, wait up to 1ms for it to complete */
118  nv_wr32(aux, 0x00e4e4 + (ch * 0x50), 0x00010000 | ctrl);
119 
120  timeout = 1000;
121  do {
122  ctrl = nv_rd32(aux, 0x00e4e4 + (ch * 0x50));
123  udelay(1);
124  if (!timeout--) {
125  AUX_ERR("tx req timeout 0x%08x\n", ctrl);
126  goto out;
127  }
128  } while (ctrl & 0x00010000);
129 
130  /* read status, and check if transaction completed ok */
131  stat = nv_mask(aux, 0x00e4e8 + (ch * 0x50), 0, 0);
132  if (!(stat & 0x000f0f00)) {
133  ret = 0;
134  break;
135  }
136 
137  AUX_DBG("%02d 0x%08x 0x%08x\n", retries, ctrl, stat);
138  }
139 
140  if (type & 1) {
141  for (i = 0; i < 16; i += 4) {
142  xbuf[i / 4] = nv_rd32(aux, 0x00e4d0 + (ch * 0x50) + i);
143  AUX_DBG("rd 0x%08x\n", xbuf[i / 4]);
144  }
145  memcpy(data, xbuf, size);
146  }
147 
148 out:
149  auxch_fini(aux, ch);
150  return ret;
151 }
152 
153 int
154 nv_rdaux(struct nouveau_i2c_port *auxch, u32 addr, u8 *data, u8 size)
155 {
156  return auxch_tx(auxch->i2c, auxch->drive, 9, addr, data, size);
157 }
158 
159 int
160 nv_wraux(struct nouveau_i2c_port *auxch, u32 addr, u8 *data, u8 size)
161 {
162  return auxch_tx(auxch->i2c, auxch->drive, 8, addr, data, size);
163 }
164 
165 static int
166 aux_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
167 {
168  struct nouveau_i2c_port *auxch = (struct nouveau_i2c_port *)adap;
169  struct i2c_msg *msg = msgs;
170  int ret, mcnt = num;
171 
172  while (mcnt--) {
173  u8 remaining = msg->len;
174  u8 *ptr = msg->buf;
175 
176  while (remaining) {
177  u8 cnt = (remaining > 16) ? 16 : remaining;
178  u8 cmd;
179 
180  if (msg->flags & I2C_M_RD)
181  cmd = 1;
182  else
183  cmd = 0;
184 
185  if (mcnt || remaining > 16)
186  cmd |= 4; /* MOT */
187 
188  ret = auxch_tx(auxch->i2c, auxch->drive, cmd,
189  msg->addr, ptr, cnt);
190  if (ret < 0)
191  return ret;
192 
193  ptr += cnt;
194  remaining -= cnt;
195  }
196 
197  msg++;
198  }
199 
200  return num;
201 }
202 
203 static u32
204 aux_func(struct i2c_adapter *adap)
205 {
207 }
208 
210  .master_xfer = aux_xfer,
211  .functionality = aux_func
212 };