saa7164-bus.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Driver for the NXP SAA7164 PCIe bridge
  3. *
  4. * Copyright (c) 2009 Steven Toth <stoth@kernellabs.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include "saa7164.h"
  22. /* The message bus to/from the firmware is a ring buffer in PCI address
  23. * space. Establish the defaults.
  24. */
  25. int saa7164_bus_setup(struct saa7164_dev *dev)
  26. {
  27. tmComResBusInfo_t *b = &dev->bus;
  28. mutex_init(&b->lock);
  29. b->Type = TYPE_BUS_PCIe;
  30. b->m_wMaxReqSize = SAA_DEVICE_MAXREQUESTSIZE;
  31. b->m_pdwSetRing = (u8 *)(dev->bmmio +
  32. ((u32)dev->busdesc.CommandRing));
  33. b->m_dwSizeSetRing = SAA_DEVICE_BUFFERBLOCKSIZE;
  34. b->m_pdwGetRing = (u8 *)(dev->bmmio +
  35. ((u32)dev->busdesc.ResponseRing));
  36. b->m_dwSizeGetRing = SAA_DEVICE_BUFFERBLOCKSIZE;
  37. b->m_pdwSetWritePos = (u32 *)((u8 *)(dev->bmmio +
  38. ((u32)dev->intfdesc.BARLocation) + (2 * sizeof(u64))));
  39. b->m_pdwSetReadPos = (u32 *)((u8 *)b->m_pdwSetWritePos +
  40. 1 * sizeof(u32));
  41. b->m_pdwGetWritePos = (u32 *)((u8 *)b->m_pdwSetWritePos +
  42. 2 * sizeof(u32));
  43. b->m_pdwGetReadPos = (u32 *)((u8 *)b->m_pdwSetWritePos +
  44. 3 * sizeof(u32));
  45. return 0;
  46. }
  47. void saa7164_bus_dump(struct saa7164_dev *dev)
  48. {
  49. tmComResBusInfo_t *b = &dev->bus;
  50. dprintk(DBGLVL_BUS, "Dumping the bus structure:\n");
  51. dprintk(DBGLVL_BUS, " .type = %d\n", b->Type);
  52. dprintk(DBGLVL_BUS, " .dev->bmmio = 0x%p\n", dev->bmmio);
  53. dprintk(DBGLVL_BUS, " .m_wMaxReqSize = 0x%x\n", b->m_wMaxReqSize);
  54. dprintk(DBGLVL_BUS, " .m_pdwSetRing = 0x%p\n", b->m_pdwSetRing);
  55. dprintk(DBGLVL_BUS, " .m_dwSizeSetRing = 0x%x\n", b->m_dwSizeSetRing);
  56. dprintk(DBGLVL_BUS, " .m_pdwGetRing = 0x%p\n", b->m_pdwGetRing);
  57. dprintk(DBGLVL_BUS, " .m_dwSizeGetRing = 0x%x\n", b->m_dwSizeGetRing);
  58. dprintk(DBGLVL_BUS, " .m_pdwSetWritePos = 0x%p (0x%08x)\n",
  59. b->m_pdwSetWritePos, *b->m_pdwSetWritePos);
  60. dprintk(DBGLVL_BUS, " .m_pdwSetReadPos = 0x%p (0x%08x)\n",
  61. b->m_pdwSetReadPos, *b->m_pdwSetReadPos);
  62. dprintk(DBGLVL_BUS, " .m_pdwGetWritePos = 0x%p (0x%08x)\n",
  63. b->m_pdwGetWritePos, *b->m_pdwGetWritePos);
  64. dprintk(DBGLVL_BUS, " .m_pdwGetReadPos = 0x%p (0x%08x)\n",
  65. b->m_pdwGetReadPos, *b->m_pdwGetReadPos);
  66. }
  67. void saa7164_bus_dumpmsg(struct saa7164_dev *dev, tmComResInfo_t* m, void *buf)
  68. {
  69. dprintk(DBGLVL_BUS, "Dumping msg structure:\n");
  70. dprintk(DBGLVL_BUS, " .id = %d\n", m->id);
  71. dprintk(DBGLVL_BUS, " .flags = 0x%x\n", m->flags);
  72. dprintk(DBGLVL_BUS, " .size = 0x%x\n", m->size);
  73. dprintk(DBGLVL_BUS, " .command = 0x%x\n", m->command);
  74. dprintk(DBGLVL_BUS, " .controlselector = 0x%x\n", m->controlselector);
  75. dprintk(DBGLVL_BUS, " .seqno = %d\n", m->seqno);
  76. if (buf)
  77. dprintk(DBGLVL_BUS, " .buffer (ignored)\n");
  78. }
  79. /*
  80. * Places a command or a response on the bus. The implementation does not
  81. * know if it is a command or a response it just places the data on the
  82. * bus depending on the bus information given in the tmComResBusInfo_t
  83. * structure. If the command or response does not fit into the bus ring
  84. * buffer it will be refused.
  85. *
  86. * Return Value:
  87. * SAA_OK The function executed successfully.
  88. * < 0 One or more members are not initialized.
  89. */
  90. int saa7164_bus_set(struct saa7164_dev *dev, tmComResInfo_t* msg, void *buf)
  91. {
  92. tmComResBusInfo_t *bus = &dev->bus;
  93. u32 bytes_to_write, read_distance, timeout, curr_srp, curr_swp;
  94. u32 new_swp, space_rem;
  95. int ret = SAA_ERR_BAD_PARAMETER;
  96. if (!msg) {
  97. printk(KERN_ERR "%s() !msg\n", __func__);
  98. return SAA_ERR_BAD_PARAMETER;
  99. }
  100. dprintk(DBGLVL_BUS, "%s()\n", __func__);
  101. msg->size = cpu_to_le16(msg->size);
  102. msg->command = cpu_to_le16(msg->command);
  103. msg->controlselector = cpu_to_le16(msg->controlselector);
  104. if (msg->size > dev->bus.m_wMaxReqSize) {
  105. printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
  106. __func__);
  107. return SAA_ERR_BAD_PARAMETER;
  108. }
  109. if ((msg->size > 0) && (buf == 0)) {
  110. printk(KERN_ERR "%s() Missing message buffer\n", __func__);
  111. return SAA_ERR_BAD_PARAMETER;
  112. }
  113. /* Lock the bus from any other access */
  114. mutex_lock(&bus->lock);
  115. bytes_to_write = sizeof(*msg) + msg->size;
  116. read_distance = 0;
  117. timeout = SAA_BUS_TIMEOUT;
  118. curr_srp = le32_to_cpu(*bus->m_pdwSetReadPos);
  119. curr_swp = le32_to_cpu(*bus->m_pdwSetWritePos);
  120. /* Deal with ring wrapping issues */
  121. if (curr_srp > curr_swp)
  122. /* The ring has not wrapped yet */
  123. read_distance = curr_srp - curr_swp;
  124. else
  125. /* Deal with the wrapped ring */
  126. read_distance = (curr_srp + bus->m_dwSizeSetRing) - curr_swp;
  127. dprintk(DBGLVL_BUS, "%s() bytes_to_write = %d\n", __func__,
  128. bytes_to_write);
  129. dprintk(DBGLVL_BUS, "%s() read_distance = %d\n", __func__,
  130. read_distance);
  131. dprintk(DBGLVL_BUS, "%s() curr_srp = %x\n", __func__, curr_srp);
  132. dprintk(DBGLVL_BUS, "%s() curr_swp = %x\n", __func__, curr_swp);
  133. /* Process the msg and write the content onto the bus */
  134. while (bytes_to_write >= read_distance) {
  135. if (timeout-- == 0) {
  136. printk(KERN_ERR "%s() bus timeout\n", __func__);
  137. ret = SAA_ERR_NO_RESOURCES;
  138. goto out;
  139. }
  140. /* TODO: Review this delay, efficient? */
  141. /* Wait, allowing the hardware fetch time */
  142. mdelay(1);
  143. /* Check the space usage again */
  144. curr_srp = le32_to_cpu(*bus->m_pdwSetReadPos);
  145. /* Deal with ring wrapping issues */
  146. if (curr_srp > curr_swp)
  147. /* Read didn't wrap around the buffer */
  148. read_distance = curr_srp - curr_swp;
  149. else
  150. /* Deal with the wrapped ring */
  151. read_distance = (curr_srp + bus->m_dwSizeSetRing) -
  152. curr_swp;
  153. }
  154. /* Calculate the new write position */
  155. new_swp = curr_swp + bytes_to_write;
  156. dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
  157. dprintk(DBGLVL_BUS, "%s() bus->m_dwSizeSetRing = %x\n", __func__,
  158. bus->m_dwSizeSetRing);
  159. /* Mental Note: line 462 tmmhComResBusPCIe.cpp */
  160. /* Check if we're going to wrap again */
  161. if (new_swp > bus->m_dwSizeSetRing) {
  162. /* Ring wraps */
  163. new_swp -= bus->m_dwSizeSetRing;
  164. space_rem = bus->m_dwSizeSetRing - curr_swp;
  165. dprintk(DBGLVL_BUS, "%s() space_rem = %x\n", __func__,
  166. space_rem);
  167. dprintk(DBGLVL_BUS, "%s() sizeof(*msg) = %d\n", __func__,
  168. (u32)sizeof(*msg));
  169. if (space_rem < sizeof(*msg)) {
  170. dprintk(DBGLVL_BUS, "%s() tr4\n", __func__);
  171. /* Split the msg into pieces as the ring wraps */
  172. memcpy(bus->m_pdwSetRing + curr_swp, msg, space_rem);
  173. memcpy(bus->m_pdwSetRing, (u8 *)msg + space_rem,
  174. sizeof(*msg) - space_rem);
  175. memcpy(bus->m_pdwSetRing + sizeof(*msg) - space_rem,
  176. buf, msg->size);
  177. } else if (space_rem == sizeof(*msg)) {
  178. dprintk(DBGLVL_BUS, "%s() tr5\n", __func__);
  179. /* Additional data at the beginning of the ring */
  180. memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
  181. memcpy(bus->m_pdwSetRing, buf, msg->size);
  182. } else {
  183. /* Additional data wraps around the ring */
  184. memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
  185. if (msg->size > 0) {
  186. memcpy(bus->m_pdwSetRing + curr_swp +
  187. sizeof(*msg), buf, space_rem -
  188. sizeof(*msg));
  189. memcpy(bus->m_pdwSetRing, (u8 *)buf +
  190. space_rem - sizeof(*msg),
  191. bytes_to_write - space_rem);
  192. }
  193. }
  194. } /* (new_swp > bus->m_dwSizeSetRing) */
  195. else {
  196. dprintk(DBGLVL_BUS, "%s() tr6\n", __func__);
  197. /* The ring buffer doesn't wrap, two simple copies */
  198. memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
  199. memcpy(bus->m_pdwSetRing + curr_swp + sizeof(*msg), buf,
  200. msg->size);
  201. }
  202. dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
  203. /* TODO: Convert all of the direct PCI writes into
  204. * saa7164_writel/b calls for consistency.
  205. */
  206. /* Update the bus write position */
  207. *bus->m_pdwSetWritePos = cpu_to_le32(new_swp);
  208. ret = SAA_OK;
  209. out:
  210. mutex_unlock(&bus->lock);
  211. return ret;
  212. }
  213. /*
  214. * Receive a command or a response from the bus. The implementation does not
  215. * know if it is a command or a response it simply dequeues the data,
  216. * depending on the bus information given in the tmComResBusInfo_t structure.
  217. *
  218. * Return Value:
  219. * 0 The function executed successfully.
  220. * < 0 One or more members are not initialized.
  221. */
  222. int saa7164_bus_get(struct saa7164_dev *dev, tmComResInfo_t* msg, void *buf,
  223. int peekonly)
  224. {
  225. tmComResBusInfo_t *bus = &dev->bus;
  226. u32 bytes_to_read, write_distance, curr_grp, curr_gwp,
  227. new_grp, buf_size, space_rem;
  228. tmComResInfo_t msg_tmp;
  229. int ret = SAA_ERR_BAD_PARAMETER;
  230. if (msg == 0)
  231. return ret;
  232. if (msg->size > dev->bus.m_wMaxReqSize) {
  233. printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
  234. __func__);
  235. return ret;
  236. }
  237. if ((peekonly == 0) && (msg->size > 0) && (buf == 0)) {
  238. printk(KERN_ERR
  239. "%s() Missing msg buf, size should be %d bytes\n",
  240. __func__, msg->size);
  241. return ret;
  242. }
  243. mutex_lock(&bus->lock);
  244. /* Peek the bus to see if a msg exists, if it's not what we're expecting
  245. * then return cleanly else read the message from the bus.
  246. */
  247. curr_gwp = le32_to_cpu(*bus->m_pdwGetWritePos);
  248. curr_grp = le32_to_cpu(*bus->m_pdwGetReadPos);
  249. if (curr_gwp == curr_grp) {
  250. dprintk(DBGLVL_BUS, "%s() No message on the bus\n", __func__);
  251. ret = SAA_ERR_EMPTY;
  252. goto out;
  253. }
  254. bytes_to_read = sizeof(*msg);
  255. /* Calculate write distance to current read position */
  256. write_distance = 0;
  257. if (curr_gwp >= curr_grp)
  258. /* Write doesn't wrap around the ring */
  259. write_distance = curr_gwp - curr_grp;
  260. else
  261. /* Write wraps around the ring */
  262. write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
  263. if (bytes_to_read > write_distance) {
  264. printk(KERN_ERR "%s() No message/response found\n", __func__);
  265. ret = SAA_ERR_INVALID_COMMAND;
  266. goto out;
  267. }
  268. /* Calculate the new read position */
  269. new_grp = curr_grp + bytes_to_read;
  270. if (new_grp > bus->m_dwSizeGetRing) {
  271. /* Ring wraps */
  272. new_grp -= bus->m_dwSizeGetRing;
  273. space_rem = bus->m_dwSizeGetRing - curr_grp;
  274. memcpy(&msg_tmp, bus->m_pdwGetRing + curr_grp, space_rem);
  275. memcpy((u8 *)&msg_tmp + space_rem, bus->m_pdwGetRing,
  276. bytes_to_read - space_rem);
  277. } else {
  278. /* No wrapping */
  279. memcpy(&msg_tmp, bus->m_pdwGetRing + curr_grp, bytes_to_read);
  280. }
  281. /* No need to update the read positions, because this was a peek */
  282. /* If the caller specifically want to peek, return */
  283. if (peekonly) {
  284. memcpy(msg, &msg_tmp, sizeof(*msg));
  285. goto peekout;
  286. }
  287. /* Check if the command/response matches what is expected */
  288. if ((msg_tmp.id != msg->id) || (msg_tmp.command != msg->command) ||
  289. (msg_tmp.controlselector != msg->controlselector) ||
  290. (msg_tmp.seqno != msg->seqno) || (msg_tmp.size != msg->size)) {
  291. printk(KERN_ERR "%s() Unexpected msg miss-match\n", __func__);
  292. saa7164_bus_dumpmsg(dev, msg, buf);
  293. saa7164_bus_dumpmsg(dev, &msg_tmp, 0);
  294. ret = SAA_ERR_INVALID_COMMAND;
  295. goto out;
  296. }
  297. /* Get the actual command and response from the bus */
  298. buf_size = msg->size;
  299. bytes_to_read = sizeof(*msg) + msg->size;
  300. /* Calculate write distance to current read position */
  301. write_distance = 0;
  302. if (curr_gwp >= curr_grp)
  303. /* Write doesn't wrap around the ring */
  304. write_distance = curr_gwp - curr_grp;
  305. else
  306. /* Write wraps around the ring */
  307. write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
  308. if (bytes_to_read > write_distance) {
  309. printk(KERN_ERR "%s() Invalid bus state, missing msg "
  310. "or mangled ring, faulty H/W / bad code?\n", __func__);
  311. ret = SAA_ERR_INVALID_COMMAND;
  312. goto out;
  313. }
  314. /* Calculate the new read position */
  315. new_grp = curr_grp + bytes_to_read;
  316. if (new_grp > bus->m_dwSizeGetRing) {
  317. /* Ring wraps */
  318. new_grp -= bus->m_dwSizeGetRing;
  319. space_rem = bus->m_dwSizeGetRing - curr_grp;
  320. if (space_rem < sizeof(*msg)) {
  321. /* msg wraps around the ring */
  322. memcpy(msg, bus->m_pdwGetRing + curr_grp, space_rem);
  323. memcpy((u8 *)msg + space_rem, bus->m_pdwGetRing,
  324. sizeof(*msg) - space_rem);
  325. if (buf)
  326. memcpy(buf, bus->m_pdwGetRing + sizeof(*msg) -
  327. space_rem, buf_size);
  328. } else if (space_rem == sizeof(*msg)) {
  329. memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
  330. if (buf)
  331. memcpy(buf, bus->m_pdwGetRing, buf_size);
  332. } else {
  333. /* Additional data wraps around the ring */
  334. memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
  335. if (buf) {
  336. memcpy(buf, bus->m_pdwGetRing + curr_grp +
  337. sizeof(*msg), space_rem - sizeof(*msg));
  338. memcpy(buf + space_rem - sizeof(*msg),
  339. bus->m_pdwGetRing, bytes_to_read -
  340. space_rem);
  341. }
  342. }
  343. } else {
  344. /* No wrapping */
  345. memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
  346. if (buf)
  347. memcpy(buf, bus->m_pdwGetRing + curr_grp + sizeof(*msg),
  348. buf_size);
  349. }
  350. /* Update the read positions, adjusting the ring */
  351. *bus->m_pdwGetReadPos = cpu_to_le32(new_grp);
  352. peekout:
  353. msg->size = le16_to_cpu(msg->size);
  354. msg->command = le16_to_cpu(msg->command);
  355. msg->controlselector = le16_to_cpu(msg->controlselector);
  356. ret = SAA_OK;
  357. out:
  358. mutex_unlock(&bus->lock);
  359. return ret;
  360. }