saa7164-bus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Driver for the NXP SAA7164 PCIe bridge
  3. *
  4. * Copyright (c) 2010 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. struct tmComResBusInfo *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_dwSetWritePos = ((u32)dev->intfdesc.BARLocation) +
  38. (2 * sizeof(u64));
  39. b->m_dwSetReadPos = b->m_dwSetWritePos + (1 * sizeof(u32));
  40. b->m_dwGetWritePos = b->m_dwSetWritePos + (2 * sizeof(u32));
  41. b->m_dwGetReadPos = b->m_dwSetWritePos + (3 * sizeof(u32));
  42. return 0;
  43. }
  44. void saa7164_bus_dump(struct saa7164_dev *dev)
  45. {
  46. struct tmComResBusInfo *b = &dev->bus;
  47. dprintk(DBGLVL_BUS, "Dumping the bus structure:\n");
  48. dprintk(DBGLVL_BUS, " .type = %d\n", b->Type);
  49. dprintk(DBGLVL_BUS, " .dev->bmmio = 0x%p\n", dev->bmmio);
  50. dprintk(DBGLVL_BUS, " .m_wMaxReqSize = 0x%x\n", b->m_wMaxReqSize);
  51. dprintk(DBGLVL_BUS, " .m_pdwSetRing = 0x%p\n", b->m_pdwSetRing);
  52. dprintk(DBGLVL_BUS, " .m_dwSizeSetRing = 0x%x\n", b->m_dwSizeSetRing);
  53. dprintk(DBGLVL_BUS, " .m_pdwGetRing = 0x%p\n", b->m_pdwGetRing);
  54. 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));
  57. dprintk(DBGLVL_BUS, " .m_dwSetWritePos = 0x%x (0x%08x)\n",
  58. b->m_dwSetWritePos, saa7164_readl(b->m_dwSetWritePos));
  59. dprintk(DBGLVL_BUS, " .m_dwGetReadPos = 0x%x (0x%08x)\n",
  60. b->m_dwGetReadPos, saa7164_readl(b->m_dwGetReadPos));
  61. dprintk(DBGLVL_BUS, " .m_dwGetWritePos = 0x%x (0x%08x)\n",
  62. b->m_dwGetWritePos, saa7164_readl(b->m_dwGetWritePos));
  63. }
  64. /* Intensionally throw a BUG() if the state of the message bus looks corrupt */
  65. void saa7164_bus_verify(struct saa7164_dev *dev)
  66. {
  67. struct tmComResBusInfo *b = &dev->bus;
  68. int bug = 0;
  69. if (saa7164_readl(b->m_dwSetReadPos) > b->m_dwSizeSetRing)
  70. bug++;
  71. if (saa7164_readl(b->m_dwSetWritePos) > b->m_dwSizeSetRing)
  72. bug++;
  73. if (saa7164_readl(b->m_dwGetReadPos) > b->m_dwSizeGetRing)
  74. bug++;
  75. if (saa7164_readl(b->m_dwGetWritePos) > b->m_dwSizeGetRing)
  76. bug++;
  77. if (bug) {
  78. saa_debug = 0xffff; /* Ensure we get the bus dump */
  79. saa7164_bus_dump(dev);
  80. saa_debug = 1024; /* Ensure we get the bus dump */
  81. BUG();
  82. }
  83. }
  84. void saa7164_bus_dumpmsg(struct saa7164_dev *dev, struct tmComResInfo* m,
  85. void *buf)
  86. {
  87. dprintk(DBGLVL_BUS, "Dumping msg structure:\n");
  88. dprintk(DBGLVL_BUS, " .id = %d\n", m->id);
  89. dprintk(DBGLVL_BUS, " .flags = 0x%x\n", m->flags);
  90. dprintk(DBGLVL_BUS, " .size = 0x%x\n", m->size);
  91. dprintk(DBGLVL_BUS, " .command = 0x%x\n", m->command);
  92. dprintk(DBGLVL_BUS, " .controlselector = 0x%x\n", m->controlselector);
  93. dprintk(DBGLVL_BUS, " .seqno = %d\n", m->seqno);
  94. if (buf)
  95. dprintk(DBGLVL_BUS, " .buffer (ignored)\n");
  96. }
  97. /*
  98. * Places a command or a response on the bus. The implementation does not
  99. * know if it is a command or a response it just places the data on the
  100. * bus depending on the bus information given in the struct tmComResBusInfo
  101. * structure. If the command or response does not fit into the bus ring
  102. * buffer it will be refused.
  103. *
  104. * Return Value:
  105. * SAA_OK The function executed successfully.
  106. * < 0 One or more members are not initialized.
  107. */
  108. int saa7164_bus_set(struct saa7164_dev *dev, struct tmComResInfo* msg,
  109. void *buf)
  110. {
  111. struct tmComResBusInfo *bus = &dev->bus;
  112. u32 bytes_to_write, free_write_space, timeout, curr_srp, curr_swp;
  113. u32 new_swp, space_rem;
  114. int ret = SAA_ERR_BAD_PARAMETER;
  115. if (!msg) {
  116. printk(KERN_ERR "%s() !msg\n", __func__);
  117. return SAA_ERR_BAD_PARAMETER;
  118. }
  119. dprintk(DBGLVL_BUS, "%s()\n", __func__);
  120. saa7164_bus_verify(dev);
  121. msg->size = cpu_to_le16(msg->size);
  122. msg->command = cpu_to_le32(msg->command);
  123. msg->controlselector = cpu_to_le16(msg->controlselector);
  124. if (msg->size > dev->bus.m_wMaxReqSize) {
  125. printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
  126. __func__);
  127. return SAA_ERR_BAD_PARAMETER;
  128. }
  129. if ((msg->size > 0) && (buf == NULL)) {
  130. printk(KERN_ERR "%s() Missing message buffer\n", __func__);
  131. return SAA_ERR_BAD_PARAMETER;
  132. }
  133. /* Lock the bus from any other access */
  134. mutex_lock(&bus->lock);
  135. bytes_to_write = sizeof(*msg) + msg->size;
  136. free_write_space = 0;
  137. timeout = SAA_BUS_TIMEOUT;
  138. curr_srp = le32_to_cpu(saa7164_readl(bus->m_dwSetReadPos));
  139. curr_swp = le32_to_cpu(saa7164_readl(bus->m_dwSetWritePos));
  140. /* Deal with ring wrapping issues */
  141. if (curr_srp > curr_swp)
  142. /* Deal with the wrapped ring */
  143. free_write_space = curr_srp - curr_swp;
  144. else
  145. /* The ring has not wrapped yet */
  146. free_write_space = (curr_srp + bus->m_dwSizeSetRing) - curr_swp;
  147. dprintk(DBGLVL_BUS, "%s() bytes_to_write = %d\n", __func__,
  148. bytes_to_write);
  149. dprintk(DBGLVL_BUS, "%s() free_write_space = %d\n", __func__,
  150. free_write_space);
  151. dprintk(DBGLVL_BUS, "%s() curr_srp = %x\n", __func__, curr_srp);
  152. dprintk(DBGLVL_BUS, "%s() curr_swp = %x\n", __func__, curr_swp);
  153. /* Process the msg and write the content onto the bus */
  154. while (bytes_to_write >= free_write_space) {
  155. if (timeout-- == 0) {
  156. printk(KERN_ERR "%s() bus timeout\n", __func__);
  157. ret = SAA_ERR_NO_RESOURCES;
  158. goto out;
  159. }
  160. /* TODO: Review this delay, efficient? */
  161. /* Wait, allowing the hardware fetch time */
  162. mdelay(1);
  163. /* Check the space usage again */
  164. curr_srp = le32_to_cpu(saa7164_readl(bus->m_dwSetReadPos));
  165. /* Deal with ring wrapping issues */
  166. if (curr_srp > curr_swp)
  167. /* Deal with the wrapped ring */
  168. free_write_space = curr_srp - curr_swp;
  169. else
  170. /* Read didn't wrap around the buffer */
  171. free_write_space = (curr_srp + bus->m_dwSizeSetRing) -
  172. curr_swp;
  173. }
  174. /* Calculate the new write position */
  175. new_swp = curr_swp + bytes_to_write;
  176. dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
  177. dprintk(DBGLVL_BUS, "%s() bus->m_dwSizeSetRing = %x\n", __func__,
  178. bus->m_dwSizeSetRing);
  179. /* Mental Note: line 462 tmmhComResBusPCIe.cpp */
  180. /* Check if we're going to wrap again */
  181. if (new_swp > bus->m_dwSizeSetRing) {
  182. /* Ring wraps */
  183. new_swp -= bus->m_dwSizeSetRing;
  184. space_rem = bus->m_dwSizeSetRing - curr_swp;
  185. dprintk(DBGLVL_BUS, "%s() space_rem = %x\n", __func__,
  186. space_rem);
  187. dprintk(DBGLVL_BUS, "%s() sizeof(*msg) = %d\n", __func__,
  188. (u32)sizeof(*msg));
  189. if (space_rem < sizeof(*msg)) {
  190. dprintk(DBGLVL_BUS, "%s() tr4\n", __func__);
  191. /* Split the msg into pieces as the ring wraps */
  192. memcpy(bus->m_pdwSetRing + curr_swp, msg, space_rem);
  193. memcpy(bus->m_pdwSetRing, (u8 *)msg + space_rem,
  194. sizeof(*msg) - space_rem);
  195. memcpy(bus->m_pdwSetRing + sizeof(*msg) - space_rem,
  196. buf, msg->size);
  197. } else if (space_rem == sizeof(*msg)) {
  198. dprintk(DBGLVL_BUS, "%s() tr5\n", __func__);
  199. /* Additional data at the beginning of the ring */
  200. memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
  201. memcpy(bus->m_pdwSetRing, buf, msg->size);
  202. } else {
  203. /* Additional data wraps around the ring */
  204. memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
  205. if (msg->size > 0) {
  206. memcpy(bus->m_pdwSetRing + curr_swp +
  207. sizeof(*msg), buf, space_rem -
  208. sizeof(*msg));
  209. memcpy(bus->m_pdwSetRing, (u8 *)buf +
  210. space_rem - sizeof(*msg),
  211. bytes_to_write - space_rem);
  212. }
  213. }
  214. } /* (new_swp > bus->m_dwSizeSetRing) */
  215. else {
  216. dprintk(DBGLVL_BUS, "%s() tr6\n", __func__);
  217. /* The ring buffer doesn't wrap, two simple copies */
  218. memcpy(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
  219. memcpy(bus->m_pdwSetRing + curr_swp + sizeof(*msg), buf,
  220. msg->size);
  221. }
  222. dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
  223. /* Update the bus write position */
  224. saa7164_writel(bus->m_dwSetWritePos, cpu_to_le32(new_swp));
  225. ret = SAA_OK;
  226. out:
  227. saa7164_bus_dump(dev);
  228. mutex_unlock(&bus->lock);
  229. saa7164_bus_verify(dev);
  230. return ret;
  231. }
  232. /*
  233. * Receive a command or a response from the bus. The implementation does not
  234. * know if it is a command or a response it simply dequeues the data,
  235. * depending on the bus information given in the struct tmComResBusInfo
  236. * structure.
  237. *
  238. * Return Value:
  239. * 0 The function executed successfully.
  240. * < 0 One or more members are not initialized.
  241. */
  242. int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
  243. void *buf, int peekonly)
  244. {
  245. struct tmComResBusInfo *bus = &dev->bus;
  246. u32 bytes_to_read, write_distance, curr_grp, curr_gwp,
  247. new_grp, buf_size, space_rem;
  248. struct tmComResInfo msg_tmp;
  249. int ret = SAA_ERR_BAD_PARAMETER;
  250. saa7164_bus_verify(dev);
  251. if (msg == NULL)
  252. return ret;
  253. if (msg->size > dev->bus.m_wMaxReqSize) {
  254. printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
  255. __func__);
  256. return ret;
  257. }
  258. if ((peekonly == 0) && (msg->size > 0) && (buf == NULL)) {
  259. printk(KERN_ERR
  260. "%s() Missing msg buf, size should be %d bytes\n",
  261. __func__, msg->size);
  262. return ret;
  263. }
  264. mutex_lock(&bus->lock);
  265. /* Peek the bus to see if a msg exists, if it's not what we're expecting
  266. * then return cleanly else read the message from the bus.
  267. */
  268. curr_gwp = le32_to_cpu(saa7164_readl(bus->m_dwGetWritePos));
  269. curr_grp = le32_to_cpu(saa7164_readl(bus->m_dwGetReadPos));
  270. if (curr_gwp == curr_grp) {
  271. ret = SAA_ERR_EMPTY;
  272. goto out;
  273. }
  274. bytes_to_read = sizeof(*msg);
  275. /* Calculate write distance to current read position */
  276. write_distance = 0;
  277. if (curr_gwp >= curr_grp)
  278. /* Write doesn't wrap around the ring */
  279. write_distance = curr_gwp - curr_grp;
  280. else
  281. /* Write wraps around the ring */
  282. write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
  283. if (bytes_to_read > write_distance) {
  284. printk(KERN_ERR "%s() No message/response found\n", __func__);
  285. ret = SAA_ERR_INVALID_COMMAND;
  286. goto out;
  287. }
  288. /* Calculate the new read position */
  289. new_grp = curr_grp + bytes_to_read;
  290. if (new_grp > bus->m_dwSizeGetRing) {
  291. /* Ring wraps */
  292. new_grp -= bus->m_dwSizeGetRing;
  293. space_rem = bus->m_dwSizeGetRing - curr_grp;
  294. memcpy(&msg_tmp, bus->m_pdwGetRing + curr_grp, space_rem);
  295. memcpy((u8 *)&msg_tmp + space_rem, bus->m_pdwGetRing,
  296. bytes_to_read - space_rem);
  297. } else {
  298. /* No wrapping */
  299. memcpy(&msg_tmp, bus->m_pdwGetRing + curr_grp, bytes_to_read);
  300. }
  301. /* No need to update the read positions, because this was a peek */
  302. /* If the caller specifically want to peek, return */
  303. if (peekonly) {
  304. memcpy(msg, &msg_tmp, sizeof(*msg));
  305. goto peekout;
  306. }
  307. /* Check if the command/response matches what is expected */
  308. if ((msg_tmp.id != msg->id) || (msg_tmp.command != msg->command) ||
  309. (msg_tmp.controlselector != msg->controlselector) ||
  310. (msg_tmp.seqno != msg->seqno) || (msg_tmp.size != msg->size)) {
  311. printk(KERN_ERR "%s() Unexpected msg miss-match\n", __func__);
  312. saa7164_bus_dumpmsg(dev, msg, buf);
  313. saa7164_bus_dumpmsg(dev, &msg_tmp, NULL);
  314. ret = SAA_ERR_INVALID_COMMAND;
  315. goto out;
  316. }
  317. /* Get the actual command and response from the bus */
  318. buf_size = msg->size;
  319. bytes_to_read = sizeof(*msg) + msg->size;
  320. /* Calculate write distance to current read position */
  321. write_distance = 0;
  322. if (curr_gwp >= curr_grp)
  323. /* Write doesn't wrap around the ring */
  324. write_distance = curr_gwp - curr_grp;
  325. else
  326. /* Write wraps around the ring */
  327. write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
  328. if (bytes_to_read > write_distance) {
  329. printk(KERN_ERR "%s() Invalid bus state, missing msg "
  330. "or mangled ring, faulty H/W / bad code?\n", __func__);
  331. ret = SAA_ERR_INVALID_COMMAND;
  332. goto out;
  333. }
  334. /* Calculate the new read position */
  335. new_grp = curr_grp + bytes_to_read;
  336. if (new_grp > bus->m_dwSizeGetRing) {
  337. /* Ring wraps */
  338. new_grp -= bus->m_dwSizeGetRing;
  339. space_rem = bus->m_dwSizeGetRing - curr_grp;
  340. if (space_rem < sizeof(*msg)) {
  341. /* msg wraps around the ring */
  342. memcpy(msg, bus->m_pdwGetRing + curr_grp, space_rem);
  343. memcpy((u8 *)msg + space_rem, bus->m_pdwGetRing,
  344. sizeof(*msg) - space_rem);
  345. if (buf)
  346. memcpy(buf, bus->m_pdwGetRing + sizeof(*msg) -
  347. space_rem, buf_size);
  348. } else if (space_rem == sizeof(*msg)) {
  349. memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
  350. if (buf)
  351. memcpy(buf, bus->m_pdwGetRing, buf_size);
  352. } else {
  353. /* Additional data wraps around the ring */
  354. memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
  355. if (buf) {
  356. memcpy(buf, bus->m_pdwGetRing + curr_grp +
  357. sizeof(*msg), space_rem - sizeof(*msg));
  358. memcpy(buf + space_rem - sizeof(*msg),
  359. bus->m_pdwGetRing, bytes_to_read -
  360. space_rem);
  361. }
  362. }
  363. } else {
  364. /* No wrapping */
  365. memcpy(msg, bus->m_pdwGetRing + curr_grp, sizeof(*msg));
  366. if (buf)
  367. memcpy(buf, bus->m_pdwGetRing + curr_grp + sizeof(*msg),
  368. buf_size);
  369. }
  370. /* Update the read positions, adjusting the ring */
  371. saa7164_writel(bus->m_dwGetReadPos, cpu_to_le32(new_grp));
  372. peekout:
  373. msg->size = le16_to_cpu(msg->size);
  374. msg->command = le32_to_cpu(msg->command);
  375. msg->controlselector = le16_to_cpu(msg->controlselector);
  376. ret = SAA_OK;
  377. out:
  378. mutex_unlock(&bus->lock);
  379. saa7164_bus_verify(dev);
  380. return ret;
  381. }