1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the NXP SAA7164 PCIe bridge
5 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
10 /* The message bus to/from the firmware is a ring buffer in PCI address
11 * space. Establish the defaults.
13 int saa7164_bus_setup(struct saa7164_dev
*dev
)
15 struct tmComResBusInfo
*b
= &dev
->bus
;
19 b
->Type
= TYPE_BUS_PCIe
;
20 b
->m_wMaxReqSize
= SAA_DEVICE_MAXREQUESTSIZE
;
22 b
->m_pdwSetRing
= (u8 __iomem
*)(dev
->bmmio
+
23 ((u32
)dev
->busdesc
.CommandRing
));
25 b
->m_dwSizeSetRing
= SAA_DEVICE_BUFFERBLOCKSIZE
;
27 b
->m_pdwGetRing
= (u8 __iomem
*)(dev
->bmmio
+
28 ((u32
)dev
->busdesc
.ResponseRing
));
30 b
->m_dwSizeGetRing
= SAA_DEVICE_BUFFERBLOCKSIZE
;
32 b
->m_dwSetWritePos
= ((u32
)dev
->intfdesc
.BARLocation
) +
34 b
->m_dwSetReadPos
= b
->m_dwSetWritePos
+ (1 * sizeof(u32
));
36 b
->m_dwGetWritePos
= b
->m_dwSetWritePos
+ (2 * sizeof(u32
));
37 b
->m_dwGetReadPos
= b
->m_dwSetWritePos
+ (3 * sizeof(u32
));
42 void saa7164_bus_dump(struct saa7164_dev
*dev
)
44 struct tmComResBusInfo
*b
= &dev
->bus
;
46 dprintk(DBGLVL_BUS
, "Dumping the bus structure:\n");
47 dprintk(DBGLVL_BUS
, " .type = %d\n", b
->Type
);
48 dprintk(DBGLVL_BUS
, " .dev->bmmio = 0x%p\n", dev
->bmmio
);
49 dprintk(DBGLVL_BUS
, " .m_wMaxReqSize = 0x%x\n", b
->m_wMaxReqSize
);
50 dprintk(DBGLVL_BUS
, " .m_pdwSetRing = 0x%p\n", b
->m_pdwSetRing
);
51 dprintk(DBGLVL_BUS
, " .m_dwSizeSetRing = 0x%x\n", b
->m_dwSizeSetRing
);
52 dprintk(DBGLVL_BUS
, " .m_pdwGetRing = 0x%p\n", b
->m_pdwGetRing
);
53 dprintk(DBGLVL_BUS
, " .m_dwSizeGetRing = 0x%x\n", b
->m_dwSizeGetRing
);
55 dprintk(DBGLVL_BUS
, " .m_dwSetReadPos = 0x%x (0x%08x)\n",
56 b
->m_dwSetReadPos
, saa7164_readl(b
->m_dwSetReadPos
));
58 dprintk(DBGLVL_BUS
, " .m_dwSetWritePos = 0x%x (0x%08x)\n",
59 b
->m_dwSetWritePos
, saa7164_readl(b
->m_dwSetWritePos
));
61 dprintk(DBGLVL_BUS
, " .m_dwGetReadPos = 0x%x (0x%08x)\n",
62 b
->m_dwGetReadPos
, saa7164_readl(b
->m_dwGetReadPos
));
64 dprintk(DBGLVL_BUS
, " .m_dwGetWritePos = 0x%x (0x%08x)\n",
65 b
->m_dwGetWritePos
, saa7164_readl(b
->m_dwGetWritePos
));
69 /* Intensionally throw a BUG() if the state of the message bus looks corrupt */
70 static void saa7164_bus_verify(struct saa7164_dev
*dev
)
72 struct tmComResBusInfo
*b
= &dev
->bus
;
75 if (saa7164_readl(b
->m_dwSetReadPos
) > b
->m_dwSizeSetRing
)
78 if (saa7164_readl(b
->m_dwSetWritePos
) > b
->m_dwSizeSetRing
)
81 if (saa7164_readl(b
->m_dwGetReadPos
) > b
->m_dwSizeGetRing
)
84 if (saa7164_readl(b
->m_dwGetWritePos
) > b
->m_dwSizeGetRing
)
88 saa_debug
= 0xffff; /* Ensure we get the bus dump */
89 saa7164_bus_dump(dev
);
90 saa_debug
= 1024; /* Ensure we get the bus dump */
95 static void saa7164_bus_dumpmsg(struct saa7164_dev
*dev
, struct tmComResInfo
*m
,
98 dprintk(DBGLVL_BUS
, "Dumping msg structure:\n");
99 dprintk(DBGLVL_BUS
, " .id = %d\n", m
->id
);
100 dprintk(DBGLVL_BUS
, " .flags = 0x%x\n", m
->flags
);
101 dprintk(DBGLVL_BUS
, " .size = 0x%x\n", m
->size
);
102 dprintk(DBGLVL_BUS
, " .command = 0x%x\n", m
->command
);
103 dprintk(DBGLVL_BUS
, " .controlselector = 0x%x\n", m
->controlselector
);
104 dprintk(DBGLVL_BUS
, " .seqno = %d\n", m
->seqno
);
106 dprintk(DBGLVL_BUS
, " .buffer (ignored)\n");
110 * Places a command or a response on the bus. The implementation does not
111 * know if it is a command or a response it just places the data on the
112 * bus depending on the bus information given in the struct tmComResBusInfo
113 * structure. If the command or response does not fit into the bus ring
114 * buffer it will be refused.
117 * SAA_OK The function executed successfully.
118 * < 0 One or more members are not initialized.
120 int saa7164_bus_set(struct saa7164_dev
*dev
, struct tmComResInfo
* msg
,
123 struct tmComResBusInfo
*bus
= &dev
->bus
;
124 u32 bytes_to_write
, free_write_space
, timeout
, curr_srp
, curr_swp
;
125 u32 new_swp
, space_rem
;
126 int ret
= SAA_ERR_BAD_PARAMETER
;
130 printk(KERN_ERR
"%s() !msg\n", __func__
);
131 return SAA_ERR_BAD_PARAMETER
;
134 dprintk(DBGLVL_BUS
, "%s()\n", __func__
);
136 saa7164_bus_verify(dev
);
138 if (msg
->size
> dev
->bus
.m_wMaxReqSize
) {
139 printk(KERN_ERR
"%s() Exceeded dev->bus.m_wMaxReqSize\n",
141 return SAA_ERR_BAD_PARAMETER
;
144 if ((msg
->size
> 0) && (buf
== NULL
)) {
145 printk(KERN_ERR
"%s() Missing message buffer\n", __func__
);
146 return SAA_ERR_BAD_PARAMETER
;
149 /* Lock the bus from any other access */
150 mutex_lock(&bus
->lock
);
152 bytes_to_write
= sizeof(*msg
) + msg
->size
;
153 free_write_space
= 0;
154 timeout
= SAA_BUS_TIMEOUT
;
155 curr_srp
= saa7164_readl(bus
->m_dwSetReadPos
);
156 curr_swp
= saa7164_readl(bus
->m_dwSetWritePos
);
158 /* Deal with ring wrapping issues */
159 if (curr_srp
> curr_swp
)
160 /* Deal with the wrapped ring */
161 free_write_space
= curr_srp
- curr_swp
;
163 /* The ring has not wrapped yet */
164 free_write_space
= (curr_srp
+ bus
->m_dwSizeSetRing
) - curr_swp
;
166 dprintk(DBGLVL_BUS
, "%s() bytes_to_write = %d\n", __func__
,
169 dprintk(DBGLVL_BUS
, "%s() free_write_space = %d\n", __func__
,
172 dprintk(DBGLVL_BUS
, "%s() curr_srp = %x\n", __func__
, curr_srp
);
173 dprintk(DBGLVL_BUS
, "%s() curr_swp = %x\n", __func__
, curr_swp
);
175 /* Process the msg and write the content onto the bus */
176 while (bytes_to_write
>= free_write_space
) {
178 if (timeout
-- == 0) {
179 printk(KERN_ERR
"%s() bus timeout\n", __func__
);
180 ret
= SAA_ERR_NO_RESOURCES
;
184 /* TODO: Review this delay, efficient? */
185 /* Wait, allowing the hardware fetch time */
188 /* Check the space usage again */
189 curr_srp
= saa7164_readl(bus
->m_dwSetReadPos
);
191 /* Deal with ring wrapping issues */
192 if (curr_srp
> curr_swp
)
193 /* Deal with the wrapped ring */
194 free_write_space
= curr_srp
- curr_swp
;
196 /* Read didn't wrap around the buffer */
197 free_write_space
= (curr_srp
+ bus
->m_dwSizeSetRing
) -
202 /* Calculate the new write position */
203 new_swp
= curr_swp
+ bytes_to_write
;
205 dprintk(DBGLVL_BUS
, "%s() new_swp = %x\n", __func__
, new_swp
);
206 dprintk(DBGLVL_BUS
, "%s() bus->m_dwSizeSetRing = %x\n", __func__
,
207 bus
->m_dwSizeSetRing
);
210 * Make a copy of msg->size before it is converted to le16 since it is
211 * used in the code below.
214 /* Convert to le16/le32 */
215 msg
->size
= (__force u16
)cpu_to_le16(msg
->size
);
216 msg
->command
= (__force u32
)cpu_to_le32(msg
->command
);
217 msg
->controlselector
= (__force u16
)cpu_to_le16(msg
->controlselector
);
219 /* Mental Note: line 462 tmmhComResBusPCIe.cpp */
221 /* Check if we're going to wrap again */
222 if (new_swp
> bus
->m_dwSizeSetRing
) {
225 new_swp
-= bus
->m_dwSizeSetRing
;
227 space_rem
= bus
->m_dwSizeSetRing
- curr_swp
;
229 dprintk(DBGLVL_BUS
, "%s() space_rem = %x\n", __func__
,
232 dprintk(DBGLVL_BUS
, "%s() sizeof(*msg) = %d\n", __func__
,
235 if (space_rem
< sizeof(*msg
)) {
236 dprintk(DBGLVL_BUS
, "%s() tr4\n", __func__
);
238 /* Split the msg into pieces as the ring wraps */
239 memcpy_toio(bus
->m_pdwSetRing
+ curr_swp
, msg
, space_rem
);
240 memcpy_toio(bus
->m_pdwSetRing
, (u8
*)msg
+ space_rem
,
241 sizeof(*msg
) - space_rem
);
243 memcpy_toio(bus
->m_pdwSetRing
+ sizeof(*msg
) - space_rem
,
246 } else if (space_rem
== sizeof(*msg
)) {
247 dprintk(DBGLVL_BUS
, "%s() tr5\n", __func__
);
249 /* Additional data at the beginning of the ring */
250 memcpy_toio(bus
->m_pdwSetRing
+ curr_swp
, msg
, sizeof(*msg
));
251 memcpy_toio(bus
->m_pdwSetRing
, buf
, size
);
254 /* Additional data wraps around the ring */
255 memcpy_toio(bus
->m_pdwSetRing
+ curr_swp
, msg
, sizeof(*msg
));
257 memcpy_toio(bus
->m_pdwSetRing
+ curr_swp
+
258 sizeof(*msg
), buf
, space_rem
-
260 memcpy_toio(bus
->m_pdwSetRing
, (u8
*)buf
+
261 space_rem
- sizeof(*msg
),
262 bytes_to_write
- space_rem
);
267 } /* (new_swp > bus->m_dwSizeSetRing) */
269 dprintk(DBGLVL_BUS
, "%s() tr6\n", __func__
);
271 /* The ring buffer doesn't wrap, two simple copies */
272 memcpy_toio(bus
->m_pdwSetRing
+ curr_swp
, msg
, sizeof(*msg
));
273 memcpy_toio(bus
->m_pdwSetRing
+ curr_swp
+ sizeof(*msg
), buf
,
277 dprintk(DBGLVL_BUS
, "%s() new_swp = %x\n", __func__
, new_swp
);
279 /* Update the bus write position */
280 saa7164_writel(bus
->m_dwSetWritePos
, new_swp
);
282 /* Convert back to cpu after writing the msg to the ringbuffer. */
283 msg
->size
= le16_to_cpu((__force __le16
)msg
->size
);
284 msg
->command
= le32_to_cpu((__force __le32
)msg
->command
);
285 msg
->controlselector
= le16_to_cpu((__force __le16
)msg
->controlselector
);
289 saa7164_bus_dump(dev
);
290 mutex_unlock(&bus
->lock
);
291 saa7164_bus_verify(dev
);
296 * Receive a command or a response from the bus. The implementation does not
297 * know if it is a command or a response it simply dequeues the data,
298 * depending on the bus information given in the struct tmComResBusInfo
302 * 0 The function executed successfully.
303 * < 0 One or more members are not initialized.
305 int saa7164_bus_get(struct saa7164_dev
*dev
, struct tmComResInfo
* msg
,
306 void *buf
, int peekonly
)
308 struct tmComResBusInfo
*bus
= &dev
->bus
;
309 u32 bytes_to_read
, write_distance
, curr_grp
, curr_gwp
,
310 new_grp
, buf_size
, space_rem
;
311 struct tmComResInfo msg_tmp
;
312 int ret
= SAA_ERR_BAD_PARAMETER
;
314 saa7164_bus_verify(dev
);
319 if (msg
->size
> dev
->bus
.m_wMaxReqSize
) {
320 printk(KERN_ERR
"%s() Exceeded dev->bus.m_wMaxReqSize\n",
325 if ((peekonly
== 0) && (msg
->size
> 0) && (buf
== NULL
)) {
327 "%s() Missing msg buf, size should be %d bytes\n",
328 __func__
, msg
->size
);
332 mutex_lock(&bus
->lock
);
334 /* Peek the bus to see if a msg exists, if it's not what we're expecting
335 * then return cleanly else read the message from the bus.
337 curr_gwp
= saa7164_readl(bus
->m_dwGetWritePos
);
338 curr_grp
= saa7164_readl(bus
->m_dwGetReadPos
);
340 if (curr_gwp
== curr_grp
) {
345 bytes_to_read
= sizeof(*msg
);
347 /* Calculate write distance to current read position */
349 if (curr_gwp
>= curr_grp
)
350 /* Write doesn't wrap around the ring */
351 write_distance
= curr_gwp
- curr_grp
;
353 /* Write wraps around the ring */
354 write_distance
= curr_gwp
+ bus
->m_dwSizeGetRing
- curr_grp
;
356 if (bytes_to_read
> write_distance
) {
357 printk(KERN_ERR
"%s() No message/response found\n", __func__
);
358 ret
= SAA_ERR_INVALID_COMMAND
;
362 /* Calculate the new read position */
363 new_grp
= curr_grp
+ bytes_to_read
;
364 if (new_grp
> bus
->m_dwSizeGetRing
) {
367 new_grp
-= bus
->m_dwSizeGetRing
;
368 space_rem
= bus
->m_dwSizeGetRing
- curr_grp
;
370 memcpy_fromio(&msg_tmp
, bus
->m_pdwGetRing
+ curr_grp
, space_rem
);
371 memcpy_fromio((u8
*)&msg_tmp
+ space_rem
, bus
->m_pdwGetRing
,
372 bytes_to_read
- space_rem
);
376 memcpy_fromio(&msg_tmp
, bus
->m_pdwGetRing
+ curr_grp
, bytes_to_read
);
378 /* Convert from little endian to CPU */
379 msg_tmp
.size
= le16_to_cpu((__force __le16
)msg_tmp
.size
);
380 msg_tmp
.command
= le32_to_cpu((__force __le32
)msg_tmp
.command
);
381 msg_tmp
.controlselector
= le16_to_cpu((__force __le16
)msg_tmp
.controlselector
);
382 memcpy(msg
, &msg_tmp
, sizeof(*msg
));
384 /* No need to update the read positions, because this was a peek */
385 /* If the caller specifically want to peek, return */
390 /* Check if the command/response matches what is expected */
391 if ((msg_tmp
.id
!= msg
->id
) || (msg_tmp
.command
!= msg
->command
) ||
392 (msg_tmp
.controlselector
!= msg
->controlselector
) ||
393 (msg_tmp
.seqno
!= msg
->seqno
) || (msg_tmp
.size
!= msg
->size
)) {
395 printk(KERN_ERR
"%s() Unexpected msg miss-match\n", __func__
);
396 saa7164_bus_dumpmsg(dev
, msg
, buf
);
397 saa7164_bus_dumpmsg(dev
, &msg_tmp
, NULL
);
398 ret
= SAA_ERR_INVALID_COMMAND
;
402 /* Get the actual command and response from the bus */
403 buf_size
= msg
->size
;
405 bytes_to_read
= sizeof(*msg
) + msg
->size
;
406 /* Calculate write distance to current read position */
408 if (curr_gwp
>= curr_grp
)
409 /* Write doesn't wrap around the ring */
410 write_distance
= curr_gwp
- curr_grp
;
412 /* Write wraps around the ring */
413 write_distance
= curr_gwp
+ bus
->m_dwSizeGetRing
- curr_grp
;
415 if (bytes_to_read
> write_distance
) {
416 printk(KERN_ERR
"%s() Invalid bus state, missing msg or mangled ring, faulty H/W / bad code?\n",
418 ret
= SAA_ERR_INVALID_COMMAND
;
422 /* Calculate the new read position */
423 new_grp
= curr_grp
+ bytes_to_read
;
424 if (new_grp
> bus
->m_dwSizeGetRing
) {
427 new_grp
-= bus
->m_dwSizeGetRing
;
428 space_rem
= bus
->m_dwSizeGetRing
- curr_grp
;
430 if (space_rem
< sizeof(*msg
)) {
432 memcpy_fromio(buf
, bus
->m_pdwGetRing
+ sizeof(*msg
) -
433 space_rem
, buf_size
);
435 } else if (space_rem
== sizeof(*msg
)) {
437 memcpy_fromio(buf
, bus
->m_pdwGetRing
, buf_size
);
439 /* Additional data wraps around the ring */
441 memcpy_fromio(buf
, bus
->m_pdwGetRing
+ curr_grp
+
442 sizeof(*msg
), space_rem
- sizeof(*msg
));
443 memcpy_fromio(buf
+ space_rem
- sizeof(*msg
),
444 bus
->m_pdwGetRing
, bytes_to_read
-
453 memcpy_fromio(buf
, bus
->m_pdwGetRing
+ curr_grp
+ sizeof(*msg
),
457 /* Update the read positions, adjusting the ring */
458 saa7164_writel(bus
->m_dwGetReadPos
, new_grp
);
463 mutex_unlock(&bus
->lock
);
464 saa7164_bus_verify(dev
);