solo6x10-p2m.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
  3. *
  4. * Original author:
  5. * Ben Collins <bcollins@ubuntu.com>
  6. *
  7. * Additional work by:
  8. * John Brooks <john.brooks@bluecherry.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include "solo6x10.h"
  24. static int multi_p2m;
  25. module_param(multi_p2m, uint, 0644);
  26. MODULE_PARM_DESC(multi_p2m,
  27. "Use multiple P2M DMA channels (default: no, 6010-only)");
  28. static int desc_mode;
  29. module_param(desc_mode, uint, 0644);
  30. MODULE_PARM_DESC(desc_mode,
  31. "Allow use of descriptor mode DMA (default: no, 6010-only)");
  32. int solo_p2m_dma(struct solo_dev *solo_dev, int wr,
  33. void *sys_addr, u32 ext_addr, u32 size,
  34. int repeat, u32 ext_size)
  35. {
  36. dma_addr_t dma_addr;
  37. int ret;
  38. if (WARN_ON_ONCE((unsigned long)sys_addr & 0x03))
  39. return -EINVAL;
  40. if (WARN_ON_ONCE(!size))
  41. return -EINVAL;
  42. dma_addr = pci_map_single(solo_dev->pdev, sys_addr, size,
  43. wr ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
  44. if (pci_dma_mapping_error(solo_dev->pdev, dma_addr))
  45. return -ENOMEM;
  46. ret = solo_p2m_dma_t(solo_dev, wr, dma_addr, ext_addr, size,
  47. repeat, ext_size);
  48. pci_unmap_single(solo_dev->pdev, dma_addr, size,
  49. wr ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
  50. return ret;
  51. }
  52. /* Mutex must be held for p2m_id before calling this!! */
  53. int solo_p2m_dma_desc(struct solo_dev *solo_dev,
  54. struct solo_p2m_desc *desc, dma_addr_t desc_dma,
  55. int desc_cnt)
  56. {
  57. struct solo_p2m_dev *p2m_dev;
  58. unsigned int timeout;
  59. unsigned int config = 0;
  60. int ret = 0;
  61. unsigned int p2m_id = 0;
  62. /* Get next ID. According to Softlogic, 6110 has problems on !=0 P2M */
  63. if (solo_dev->type != SOLO_DEV_6110 && multi_p2m)
  64. p2m_id = atomic_inc_return(&solo_dev->p2m_count) % SOLO_NR_P2M;
  65. p2m_dev = &solo_dev->p2m_dev[p2m_id];
  66. if (mutex_lock_interruptible(&p2m_dev->mutex))
  67. return -EINTR;
  68. reinit_completion(&p2m_dev->completion);
  69. p2m_dev->error = 0;
  70. if (desc_cnt > 1 && solo_dev->type != SOLO_DEV_6110 && desc_mode) {
  71. /* For 6010 with more than one desc, we can do a one-shot */
  72. p2m_dev->desc_count = p2m_dev->desc_idx = 0;
  73. config = solo_reg_read(solo_dev, SOLO_P2M_CONFIG(p2m_id));
  74. solo_reg_write(solo_dev, SOLO_P2M_DES_ADR(p2m_id), desc_dma);
  75. solo_reg_write(solo_dev, SOLO_P2M_DESC_ID(p2m_id), desc_cnt);
  76. solo_reg_write(solo_dev, SOLO_P2M_CONFIG(p2m_id), config |
  77. SOLO_P2M_DESC_MODE);
  78. } else {
  79. /* For single descriptors and 6110, we need to run each desc */
  80. p2m_dev->desc_count = desc_cnt;
  81. p2m_dev->desc_idx = 1;
  82. p2m_dev->descs = desc;
  83. solo_reg_write(solo_dev, SOLO_P2M_TAR_ADR(p2m_id),
  84. desc[1].dma_addr);
  85. solo_reg_write(solo_dev, SOLO_P2M_EXT_ADR(p2m_id),
  86. desc[1].ext_addr);
  87. solo_reg_write(solo_dev, SOLO_P2M_EXT_CFG(p2m_id),
  88. desc[1].cfg);
  89. solo_reg_write(solo_dev, SOLO_P2M_CONTROL(p2m_id),
  90. desc[1].ctrl);
  91. }
  92. timeout = wait_for_completion_timeout(&p2m_dev->completion,
  93. solo_dev->p2m_jiffies);
  94. if (WARN_ON_ONCE(p2m_dev->error))
  95. ret = -EIO;
  96. else if (timeout == 0) {
  97. solo_dev->p2m_timeouts++;
  98. ret = -EAGAIN;
  99. }
  100. solo_reg_write(solo_dev, SOLO_P2M_CONTROL(p2m_id), 0);
  101. /* Don't write here for the no_desc_mode case, because config is 0.
  102. * We can't test no_desc_mode again, it might race. */
  103. if (desc_cnt > 1 && solo_dev->type != SOLO_DEV_6110 && config)
  104. solo_reg_write(solo_dev, SOLO_P2M_CONFIG(p2m_id), config);
  105. mutex_unlock(&p2m_dev->mutex);
  106. return ret;
  107. }
  108. void solo_p2m_fill_desc(struct solo_p2m_desc *desc, int wr,
  109. dma_addr_t dma_addr, u32 ext_addr, u32 size,
  110. int repeat, u32 ext_size)
  111. {
  112. WARN_ON_ONCE(dma_addr & 0x03);
  113. WARN_ON_ONCE(!size);
  114. desc->cfg = SOLO_P2M_COPY_SIZE(size >> 2);
  115. desc->ctrl = SOLO_P2M_BURST_SIZE(SOLO_P2M_BURST_256) |
  116. (wr ? SOLO_P2M_WRITE : 0) | SOLO_P2M_TRANS_ON;
  117. if (repeat) {
  118. desc->cfg |= SOLO_P2M_EXT_INC(ext_size >> 2);
  119. desc->ctrl |= SOLO_P2M_PCI_INC(size >> 2) |
  120. SOLO_P2M_REPEAT(repeat);
  121. }
  122. desc->dma_addr = dma_addr;
  123. desc->ext_addr = ext_addr;
  124. }
  125. int solo_p2m_dma_t(struct solo_dev *solo_dev, int wr,
  126. dma_addr_t dma_addr, u32 ext_addr, u32 size,
  127. int repeat, u32 ext_size)
  128. {
  129. struct solo_p2m_desc desc[2];
  130. solo_p2m_fill_desc(&desc[1], wr, dma_addr, ext_addr, size, repeat,
  131. ext_size);
  132. /* No need for desc_dma since we know it is a single-shot */
  133. return solo_p2m_dma_desc(solo_dev, desc, 0, 1);
  134. }
  135. void solo_p2m_isr(struct solo_dev *solo_dev, int id)
  136. {
  137. struct solo_p2m_dev *p2m_dev = &solo_dev->p2m_dev[id];
  138. struct solo_p2m_desc *desc;
  139. if (p2m_dev->desc_count <= p2m_dev->desc_idx) {
  140. complete(&p2m_dev->completion);
  141. return;
  142. }
  143. /* Setup next descriptor */
  144. p2m_dev->desc_idx++;
  145. desc = &p2m_dev->descs[p2m_dev->desc_idx];
  146. solo_reg_write(solo_dev, SOLO_P2M_CONTROL(id), 0);
  147. solo_reg_write(solo_dev, SOLO_P2M_TAR_ADR(id), desc->dma_addr);
  148. solo_reg_write(solo_dev, SOLO_P2M_EXT_ADR(id), desc->ext_addr);
  149. solo_reg_write(solo_dev, SOLO_P2M_EXT_CFG(id), desc->cfg);
  150. solo_reg_write(solo_dev, SOLO_P2M_CONTROL(id), desc->ctrl);
  151. }
  152. void solo_p2m_error_isr(struct solo_dev *solo_dev)
  153. {
  154. unsigned int err = solo_reg_read(solo_dev, SOLO_PCI_ERR);
  155. struct solo_p2m_dev *p2m_dev;
  156. int i;
  157. if (!(err & (SOLO_PCI_ERR_P2M | SOLO_PCI_ERR_P2M_DESC)))
  158. return;
  159. for (i = 0; i < SOLO_NR_P2M; i++) {
  160. p2m_dev = &solo_dev->p2m_dev[i];
  161. p2m_dev->error = 1;
  162. solo_reg_write(solo_dev, SOLO_P2M_CONTROL(i), 0);
  163. complete(&p2m_dev->completion);
  164. }
  165. }
  166. void solo_p2m_exit(struct solo_dev *solo_dev)
  167. {
  168. int i;
  169. for (i = 0; i < SOLO_NR_P2M; i++)
  170. solo_irq_off(solo_dev, SOLO_IRQ_P2M(i));
  171. }
  172. static int solo_p2m_test(struct solo_dev *solo_dev, int base, int size)
  173. {
  174. u32 *wr_buf;
  175. u32 *rd_buf;
  176. int i;
  177. int ret = -EIO;
  178. int order = get_order(size);
  179. wr_buf = (u32 *)__get_free_pages(GFP_KERNEL, order);
  180. if (wr_buf == NULL)
  181. return -1;
  182. rd_buf = (u32 *)__get_free_pages(GFP_KERNEL, order);
  183. if (rd_buf == NULL) {
  184. free_pages((unsigned long)wr_buf, order);
  185. return -1;
  186. }
  187. for (i = 0; i < (size >> 3); i++)
  188. *(wr_buf + i) = (i << 16) | (i + 1);
  189. for (i = (size >> 3); i < (size >> 2); i++)
  190. *(wr_buf + i) = ~((i << 16) | (i + 1));
  191. memset(rd_buf, 0x55, size);
  192. if (solo_p2m_dma(solo_dev, 1, wr_buf, base, size, 0, 0))
  193. goto test_fail;
  194. if (solo_p2m_dma(solo_dev, 0, rd_buf, base, size, 0, 0))
  195. goto test_fail;
  196. for (i = 0; i < (size >> 2); i++) {
  197. if (*(wr_buf + i) != *(rd_buf + i))
  198. goto test_fail;
  199. }
  200. ret = 0;
  201. test_fail:
  202. free_pages((unsigned long)wr_buf, order);
  203. free_pages((unsigned long)rd_buf, order);
  204. return ret;
  205. }
  206. int solo_p2m_init(struct solo_dev *solo_dev)
  207. {
  208. struct solo_p2m_dev *p2m_dev;
  209. int i;
  210. for (i = 0; i < SOLO_NR_P2M; i++) {
  211. p2m_dev = &solo_dev->p2m_dev[i];
  212. mutex_init(&p2m_dev->mutex);
  213. init_completion(&p2m_dev->completion);
  214. solo_reg_write(solo_dev, SOLO_P2M_CONTROL(i), 0);
  215. solo_reg_write(solo_dev, SOLO_P2M_CONFIG(i),
  216. SOLO_P2M_CSC_16BIT_565 |
  217. SOLO_P2M_DESC_INTR_OPT |
  218. SOLO_P2M_DMA_INTERVAL(0) |
  219. SOLO_P2M_PCI_MASTER_MODE);
  220. solo_irq_on(solo_dev, SOLO_IRQ_P2M(i));
  221. }
  222. /* Find correct SDRAM size */
  223. for (solo_dev->sdram_size = 0, i = 2; i >= 0; i--) {
  224. solo_reg_write(solo_dev, SOLO_DMA_CTRL,
  225. SOLO_DMA_CTRL_REFRESH_CYCLE(1) |
  226. SOLO_DMA_CTRL_SDRAM_SIZE(i) |
  227. SOLO_DMA_CTRL_SDRAM_CLK_INVERT |
  228. SOLO_DMA_CTRL_READ_CLK_SELECT |
  229. SOLO_DMA_CTRL_LATENCY(1));
  230. solo_reg_write(solo_dev, SOLO_SYS_CFG, solo_dev->sys_config |
  231. SOLO_SYS_CFG_RESET);
  232. solo_reg_write(solo_dev, SOLO_SYS_CFG, solo_dev->sys_config);
  233. switch (i) {
  234. case 2:
  235. if (solo_p2m_test(solo_dev, 0x07ff0000, 0x00010000) ||
  236. solo_p2m_test(solo_dev, 0x05ff0000, 0x00010000))
  237. continue;
  238. break;
  239. case 1:
  240. if (solo_p2m_test(solo_dev, 0x03ff0000, 0x00010000))
  241. continue;
  242. break;
  243. default:
  244. if (solo_p2m_test(solo_dev, 0x01ff0000, 0x00010000))
  245. continue;
  246. }
  247. solo_dev->sdram_size = (32 << 20) << i;
  248. break;
  249. }
  250. if (!solo_dev->sdram_size) {
  251. dev_err(&solo_dev->pdev->dev, "Error detecting SDRAM size\n");
  252. return -EIO;
  253. }
  254. if (SOLO_SDRAM_END(solo_dev) > solo_dev->sdram_size) {
  255. dev_err(&solo_dev->pdev->dev,
  256. "SDRAM is not large enough (%u < %u)\n",
  257. solo_dev->sdram_size, SOLO_SDRAM_END(solo_dev));
  258. return -EIO;
  259. }
  260. return 0;
  261. }