saa7164-bus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Driver for the NXP SAA7164 PCIe bridge
  3. *
  4. * Copyright (c) 2010-2015 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 __iomem *)(dev->bmmio +
  32. ((u32)dev->busdesc.CommandRing));
  33. b->m_dwSizeSetRing = SAA_DEVICE_BUFFERBLOCKSIZE;
  34. b->m_pdwGetRing = (u8 __iomem *)(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. static 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. static 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. u16 size;
  116. if (!msg) {
  117. printk(KERN_ERR "%s() !msg\n", __func__);
  118. return SAA_ERR_BAD_PARAMETER;
  119. }
  120. dprintk(DBGLVL_BUS, "%s()\n", __func__);
  121. saa7164_bus_verify(dev);
  122. if (msg->size > dev->bus.m_wMaxReqSize) {
  123. printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
  124. __func__);
  125. return SAA_ERR_BAD_PARAMETER;
  126. }
  127. if ((msg->size > 0) && (buf == NULL)) {
  128. printk(KERN_ERR "%s() Missing message buffer\n", __func__);
  129. return SAA_ERR_BAD_PARAMETER;
  130. }
  131. /* Lock the bus from any other access */
  132. mutex_lock(&bus->lock);
  133. bytes_to_write = sizeof(*msg) + msg->size;
  134. free_write_space = 0;
  135. timeout = SAA_BUS_TIMEOUT;
  136. curr_srp = saa7164_readl(bus->m_dwSetReadPos);
  137. curr_swp = saa7164_readl(bus->m_dwSetWritePos);
  138. /* Deal with ring wrapping issues */
  139. if (curr_srp > curr_swp)
  140. /* Deal with the wrapped ring */
  141. free_write_space = curr_srp - curr_swp;
  142. else
  143. /* The ring has not wrapped yet */
  144. free_write_space = (curr_srp + bus->m_dwSizeSetRing) - curr_swp;
  145. dprintk(DBGLVL_BUS, "%s() bytes_to_write = %d\n", __func__,
  146. bytes_to_write);
  147. dprintk(DBGLVL_BUS, "%s() free_write_space = %d\n", __func__,
  148. free_write_space);
  149. dprintk(DBGLVL_BUS, "%s() curr_srp = %x\n", __func__, curr_srp);
  150. dprintk(DBGLVL_BUS, "%s() curr_swp = %x\n", __func__, curr_swp);
  151. /* Process the msg and write the content onto the bus */
  152. while (bytes_to_write >= free_write_space) {
  153. if (timeout-- == 0) {
  154. printk(KERN_ERR "%s() bus timeout\n", __func__);
  155. ret = SAA_ERR_NO_RESOURCES;
  156. goto out;
  157. }
  158. /* TODO: Review this delay, efficient? */
  159. /* Wait, allowing the hardware fetch time */
  160. mdelay(1);
  161. /* Check the space usage again */
  162. curr_srp = saa7164_readl(bus->m_dwSetReadPos);
  163. /* Deal with ring wrapping issues */
  164. if (curr_srp > curr_swp)
  165. /* Deal with the wrapped ring */
  166. free_write_space = curr_srp - curr_swp;
  167. else
  168. /* Read didn't wrap around the buffer */
  169. free_write_space = (curr_srp + bus->m_dwSizeSetRing) -
  170. curr_swp;
  171. }
  172. /* Calculate the new write position */
  173. new_swp = curr_swp + bytes_to_write;
  174. dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
  175. dprintk(DBGLVL_BUS, "%s() bus->m_dwSizeSetRing = %x\n", __func__,
  176. bus->m_dwSizeSetRing);
  177. /*
  178. * Make a copy of msg->size before it is converted to le16 since it is
  179. * used in the code below.
  180. */
  181. size = msg->size;
  182. /* Convert to le16/le32 */
  183. msg->size = (__force u16)cpu_to_le16(msg->size);
  184. msg->command = (__force u32)cpu_to_le32(msg->command);
  185. msg->controlselector = (__force u16)cpu_to_le16(msg->controlselector);
  186. /* Mental Note: line 462 tmmhComResBusPCIe.cpp */
  187. /* Check if we're going to wrap again */
  188. if (new_swp > bus->m_dwSizeSetRing) {
  189. /* Ring wraps */
  190. new_swp -= bus->m_dwSizeSetRing;
  191. space_rem = bus->m_dwSizeSetRing - curr_swp;
  192. dprintk(DBGLVL_BUS, "%s() space_rem = %x\n", __func__,
  193. space_rem);
  194. dprintk(DBGLVL_BUS, "%s() sizeof(*msg) = %d\n", __func__,
  195. (u32)sizeof(*msg));
  196. if (space_rem < sizeof(*msg)) {
  197. dprintk(DBGLVL_BUS, "%s() tr4\n", __func__);
  198. /* Split the msg into pieces as the ring wraps */
  199. memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, space_rem);
  200. memcpy_toio(bus->m_pdwSetRing, (u8 *)msg + space_rem,
  201. sizeof(*msg) - space_rem);
  202. memcpy_toio(bus->m_pdwSetRing + sizeof(*msg) - space_rem,
  203. buf, size);
  204. } else if (space_rem == sizeof(*msg)) {
  205. dprintk(DBGLVL_BUS, "%s() tr5\n", __func__);
  206. /* Additional data at the beginning of the ring */
  207. memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
  208. memcpy_toio(bus->m_pdwSetRing, buf, size);
  209. } else {
  210. /* Additional data wraps around the ring */
  211. memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
  212. if (size > 0) {
  213. memcpy_toio(bus->m_pdwSetRing + curr_swp +
  214. sizeof(*msg), buf, space_rem -
  215. sizeof(*msg));
  216. memcpy_toio(bus->m_pdwSetRing, (u8 *)buf +
  217. space_rem - sizeof(*msg),
  218. bytes_to_write - space_rem);
  219. }
  220. }
  221. } /* (new_swp > bus->m_dwSizeSetRing) */
  222. else {
  223. dprintk(DBGLVL_BUS, "%s() tr6\n", __func__);
  224. /* The ring buffer doesn't wrap, two simple copies */
  225. memcpy_toio(bus->m_pdwSetRing + curr_swp, msg, sizeof(*msg));
  226. memcpy_toio(bus->m_pdwSetRing + curr_swp + sizeof(*msg), buf,
  227. size);
  228. }
  229. dprintk(DBGLVL_BUS, "%s() new_swp = %x\n", __func__, new_swp);
  230. /* Update the bus write position */
  231. saa7164_writel(bus->m_dwSetWritePos, new_swp);
  232. /* Convert back to cpu after writing the msg to the ringbuffer. */
  233. msg->size = le16_to_cpu((__force __le16)msg->size);
  234. msg->command = le32_to_cpu((__force __le32)msg->command);
  235. msg->controlselector = le16_to_cpu((__force __le16)msg->controlselector);
  236. ret = SAA_OK;
  237. out:
  238. saa7164_bus_dump(dev);
  239. mutex_unlock(&bus->lock);
  240. saa7164_bus_verify(dev);
  241. return ret;
  242. }
  243. /*
  244. * Receive a command or a response from the bus. The implementation does not
  245. * know if it is a command or a response it simply dequeues the data,
  246. * depending on the bus information given in the struct tmComResBusInfo
  247. * structure.
  248. *
  249. * Return Value:
  250. * 0 The function executed successfully.
  251. * < 0 One or more members are not initialized.
  252. */
  253. int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
  254. void *buf, int peekonly)
  255. {
  256. struct tmComResBusInfo *bus = &dev->bus;
  257. u32 bytes_to_read, write_distance, curr_grp, curr_gwp,
  258. new_grp, buf_size, space_rem;
  259. struct tmComResInfo msg_tmp;
  260. int ret = SAA_ERR_BAD_PARAMETER;
  261. saa7164_bus_verify(dev);
  262. if (msg == NULL)
  263. return ret;
  264. if (msg->size > dev->bus.m_wMaxReqSize) {
  265. printk(KERN_ERR "%s() Exceeded dev->bus.m_wMaxReqSize\n",
  266. __func__);
  267. return ret;
  268. }
  269. if ((peekonly == 0) && (msg->size > 0) && (buf == NULL)) {
  270. printk(KERN_ERR
  271. "%s() Missing msg buf, size should be %d bytes\n",
  272. __func__, msg->size);
  273. return ret;
  274. }
  275. mutex_lock(&bus->lock);
  276. /* Peek the bus to see if a msg exists, if it's not what we're expecting
  277. * then return cleanly else read the message from the bus.
  278. */
  279. curr_gwp = saa7164_readl(bus->m_dwGetWritePos);
  280. curr_grp = saa7164_readl(bus->m_dwGetReadPos);
  281. if (curr_gwp == curr_grp) {
  282. ret = SAA_ERR_EMPTY;
  283. goto out;
  284. }
  285. bytes_to_read = sizeof(*msg);
  286. /* Calculate write distance to current read position */
  287. write_distance = 0;
  288. if (curr_gwp >= curr_grp)
  289. /* Write doesn't wrap around the ring */
  290. write_distance = curr_gwp - curr_grp;
  291. else
  292. /* Write wraps around the ring */
  293. write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
  294. if (bytes_to_read > write_distance) {
  295. printk(KERN_ERR "%s() No message/response found\n", __func__);
  296. ret = SAA_ERR_INVALID_COMMAND;
  297. goto out;
  298. }
  299. /* Calculate the new read position */
  300. new_grp = curr_grp + bytes_to_read;
  301. if (new_grp > bus->m_dwSizeGetRing) {
  302. /* Ring wraps */
  303. new_grp -= bus->m_dwSizeGetRing;
  304. space_rem = bus->m_dwSizeGetRing - curr_grp;
  305. memcpy_fromio(&msg_tmp, bus->m_pdwGetRing + curr_grp, space_rem);
  306. memcpy_fromio((u8 *)&msg_tmp + space_rem, bus->m_pdwGetRing,
  307. bytes_to_read - space_rem);
  308. } else {
  309. /* No wrapping */
  310. memcpy_fromio(&msg_tmp, bus->m_pdwGetRing + curr_grp, bytes_to_read);
  311. }
  312. /* Convert from little endian to CPU */
  313. msg_tmp.size = le16_to_cpu((__force __le16)msg_tmp.size);
  314. msg_tmp.command = le32_to_cpu((__force __le32)msg_tmp.command);
  315. msg_tmp.controlselector = le16_to_cpu((__force __le16)msg_tmp.controlselector);
  316. memcpy(msg, &msg_tmp, sizeof(*msg));
  317. /* No need to update the read positions, because this was a peek */
  318. /* If the caller specifically want to peek, return */
  319. if (peekonly) {
  320. goto peekout;
  321. }
  322. /* Check if the command/response matches what is expected */
  323. if ((msg_tmp.id != msg->id) || (msg_tmp.command != msg->command) ||
  324. (msg_tmp.controlselector != msg->controlselector) ||
  325. (msg_tmp.seqno != msg->seqno) || (msg_tmp.size != msg->size)) {
  326. printk(KERN_ERR "%s() Unexpected msg miss-match\n", __func__);
  327. saa7164_bus_dumpmsg(dev, msg, buf);
  328. saa7164_bus_dumpmsg(dev, &msg_tmp, NULL);
  329. ret = SAA_ERR_INVALID_COMMAND;
  330. goto out;
  331. }
  332. /* Get the actual command and response from the bus */
  333. buf_size = msg->size;
  334. bytes_to_read = sizeof(*msg) + msg->size;
  335. /* Calculate write distance to current read position */
  336. write_distance = 0;
  337. if (curr_gwp >= curr_grp)
  338. /* Write doesn't wrap around the ring */
  339. write_distance = curr_gwp - curr_grp;
  340. else
  341. /* Write wraps around the ring */
  342. write_distance = curr_gwp + bus->m_dwSizeGetRing - curr_grp;
  343. if (bytes_to_read > write_distance) {
  344. printk(KERN_ERR "%s() Invalid bus state, missing msg "
  345. "or mangled ring, faulty H/W / bad code?\n", __func__);
  346. ret = SAA_ERR_INVALID_COMMAND;
  347. goto out;
  348. }
  349. /* Calculate the new read position */
  350. new_grp = curr_grp + bytes_to_read;
  351. if (new_grp > bus->m_dwSizeGetRing) {
  352. /* Ring wraps */
  353. new_grp -= bus->m_dwSizeGetRing;
  354. space_rem = bus->m_dwSizeGetRing - curr_grp;
  355. if (space_rem < sizeof(*msg)) {
  356. if (buf)
  357. memcpy_fromio(buf, bus->m_pdwGetRing + sizeof(*msg) -
  358. space_rem, buf_size);
  359. } else if (space_rem == sizeof(*msg)) {
  360. if (buf)
  361. memcpy_fromio(buf, bus->m_pdwGetRing, buf_size);
  362. } else {
  363. /* Additional data wraps around the ring */
  364. if (buf) {
  365. memcpy_fromio(buf, bus->m_pdwGetRing + curr_grp +
  366. sizeof(*msg), space_rem - sizeof(*msg));
  367. memcpy_fromio(buf + space_rem - sizeof(*msg),
  368. bus->m_pdwGetRing, bytes_to_read -
  369. space_rem);
  370. }
  371. }
  372. } else {
  373. /* No wrapping */
  374. if (buf)
  375. memcpy_fromio(buf, bus->m_pdwGetRing + curr_grp + sizeof(*msg),
  376. buf_size);
  377. }
  378. /* Update the read positions, adjusting the ring */
  379. saa7164_writel(bus->m_dwGetReadPos, new_grp);
  380. peekout:
  381. ret = SAA_OK;
  382. out:
  383. mutex_unlock(&bus->lock);
  384. saa7164_bus_verify(dev);
  385. return ret;
  386. }