2 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
5 * Ben Collins <bcollins@ubuntu.com>
8 * John Brooks <john.brooks@bluecherry.net>
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.
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.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
28 module_param(multi_p2m, uint, 0644);
29 MODULE_PARM_DESC(multi_p2m,
30 "Use multiple P2M DMA channels (default: no, 6010-only)");
33 module_param(desc_mode, uint, 0644);
34 MODULE_PARM_DESC(desc_mode,
35 "Allow use of descriptor mode DMA (default: no, 6010-only)");
37 int solo_p2m_dma(struct solo_dev *solo_dev, int wr,
38 void *sys_addr, u32 ext_addr, u32 size,
39 int repeat, u32 ext_size)
44 if (WARN_ON_ONCE((unsigned long)sys_addr & 0x03))
46 if (WARN_ON_ONCE(!size))
49 dma_addr = pci_map_single(solo_dev->pdev, sys_addr, size,
50 wr ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
51 if (pci_dma_mapping_error(solo_dev->pdev, dma_addr))
54 ret = solo_p2m_dma_t(solo_dev, wr, dma_addr, ext_addr, size,
57 pci_unmap_single(solo_dev->pdev, dma_addr, size,
58 wr ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
63 /* Mutex must be held for p2m_id before calling this!! */
64 int solo_p2m_dma_desc(struct solo_dev *solo_dev,
65 struct solo_p2m_desc *desc, dma_addr_t desc_dma,
68 struct solo_p2m_dev *p2m_dev;
70 unsigned int config = 0;
74 /* Get next ID. According to Softlogic, 6110 has problems on !=0 P2M */
75 if (solo_dev->type != SOLO_DEV_6110 && multi_p2m) {
76 p2m_id = atomic_inc_return(&solo_dev->p2m_count) % SOLO_NR_P2M;
81 p2m_dev = &solo_dev->p2m_dev[p2m_id];
83 if (mutex_lock_interruptible(&p2m_dev->mutex))
86 reinit_completion(&p2m_dev->completion);
89 if (desc_cnt > 1 && solo_dev->type != SOLO_DEV_6110 && desc_mode) {
90 /* For 6010 with more than one desc, we can do a one-shot */
91 p2m_dev->desc_count = p2m_dev->desc_idx = 0;
92 config = solo_reg_read(solo_dev, SOLO_P2M_CONFIG(p2m_id));
94 solo_reg_write(solo_dev, SOLO_P2M_DES_ADR(p2m_id), desc_dma);
95 solo_reg_write(solo_dev, SOLO_P2M_DESC_ID(p2m_id), desc_cnt);
96 solo_reg_write(solo_dev, SOLO_P2M_CONFIG(p2m_id), config |
99 /* For single descriptors and 6110, we need to run each desc */
100 p2m_dev->desc_count = desc_cnt;
101 p2m_dev->desc_idx = 1;
102 p2m_dev->descs = desc;
104 solo_reg_write(solo_dev, SOLO_P2M_TAR_ADR(p2m_id),
106 solo_reg_write(solo_dev, SOLO_P2M_EXT_ADR(p2m_id),
108 solo_reg_write(solo_dev, SOLO_P2M_EXT_CFG(p2m_id),
110 solo_reg_write(solo_dev, SOLO_P2M_CONTROL(p2m_id),
114 timeout = wait_for_completion_timeout(&p2m_dev->completion,
115 solo_dev->p2m_jiffies);
117 if (WARN_ON_ONCE(p2m_dev->error))
119 else if (timeout == 0) {
120 solo_dev->p2m_timeouts++;
124 solo_reg_write(solo_dev, SOLO_P2M_CONTROL(p2m_id), 0);
126 /* Don't write here for the no_desc_mode case, because config is 0.
127 * We can't test no_desc_mode again, it might race. */
128 if (desc_cnt > 1 && solo_dev->type != SOLO_DEV_6110 && config)
129 solo_reg_write(solo_dev, SOLO_P2M_CONFIG(p2m_id), config);
131 mutex_unlock(&p2m_dev->mutex);
136 void solo_p2m_fill_desc(struct solo_p2m_desc *desc, int wr,
137 dma_addr_t dma_addr, u32 ext_addr, u32 size,
138 int repeat, u32 ext_size)
140 WARN_ON_ONCE(dma_addr & 0x03);
143 desc->cfg = SOLO_P2M_COPY_SIZE(size >> 2);
144 desc->ctrl = SOLO_P2M_BURST_SIZE(SOLO_P2M_BURST_256) |
145 (wr ? SOLO_P2M_WRITE : 0) | SOLO_P2M_TRANS_ON;
148 desc->cfg |= SOLO_P2M_EXT_INC(ext_size >> 2);
149 desc->ctrl |= SOLO_P2M_PCI_INC(size >> 2) |
150 SOLO_P2M_REPEAT(repeat);
153 desc->dma_addr = dma_addr;
154 desc->ext_addr = ext_addr;
157 int solo_p2m_dma_t(struct solo_dev *solo_dev, int wr,
158 dma_addr_t dma_addr, u32 ext_addr, u32 size,
159 int repeat, u32 ext_size)
161 struct solo_p2m_desc desc[2];
163 solo_p2m_fill_desc(&desc[1], wr, dma_addr, ext_addr, size, repeat,
166 /* No need for desc_dma since we know it is a single-shot */
167 return solo_p2m_dma_desc(solo_dev, desc, 0, 1);
170 void solo_p2m_isr(struct solo_dev *solo_dev, int id)
172 struct solo_p2m_dev *p2m_dev = &solo_dev->p2m_dev[id];
173 struct solo_p2m_desc *desc;
175 if (p2m_dev->desc_count <= p2m_dev->desc_idx) {
176 complete(&p2m_dev->completion);
180 /* Setup next descriptor */
182 desc = &p2m_dev->descs[p2m_dev->desc_idx];
184 solo_reg_write(solo_dev, SOLO_P2M_CONTROL(id), 0);
185 solo_reg_write(solo_dev, SOLO_P2M_TAR_ADR(id), desc->dma_addr);
186 solo_reg_write(solo_dev, SOLO_P2M_EXT_ADR(id), desc->ext_addr);
187 solo_reg_write(solo_dev, SOLO_P2M_EXT_CFG(id), desc->cfg);
188 solo_reg_write(solo_dev, SOLO_P2M_CONTROL(id), desc->ctrl);
191 void solo_p2m_error_isr(struct solo_dev *solo_dev)
193 unsigned int err = solo_reg_read(solo_dev, SOLO_PCI_ERR);
194 struct solo_p2m_dev *p2m_dev;
197 if (!(err & (SOLO_PCI_ERR_P2M | SOLO_PCI_ERR_P2M_DESC)))
200 for (i = 0; i < SOLO_NR_P2M; i++) {
201 p2m_dev = &solo_dev->p2m_dev[i];
203 solo_reg_write(solo_dev, SOLO_P2M_CONTROL(i), 0);
204 complete(&p2m_dev->completion);
208 void solo_p2m_exit(struct solo_dev *solo_dev)
212 for (i = 0; i < SOLO_NR_P2M; i++)
213 solo_irq_off(solo_dev, SOLO_IRQ_P2M(i));
216 static int solo_p2m_test(struct solo_dev *solo_dev, int base, int size)
222 int order = get_order(size);
224 wr_buf = (u32 *)__get_free_pages(GFP_KERNEL, order);
228 rd_buf = (u32 *)__get_free_pages(GFP_KERNEL, order);
229 if (rd_buf == NULL) {
230 free_pages((unsigned long)wr_buf, order);
234 for (i = 0; i < (size >> 3); i++)
235 *(wr_buf + i) = (i << 16) | (i + 1);
237 for (i = (size >> 3); i < (size >> 2); i++)
238 *(wr_buf + i) = ~((i << 16) | (i + 1));
240 memset(rd_buf, 0x55, size);
242 if (solo_p2m_dma(solo_dev, 1, wr_buf, base, size, 0, 0))
245 if (solo_p2m_dma(solo_dev, 0, rd_buf, base, size, 0, 0))
248 for (i = 0; i < (size >> 2); i++) {
249 if (*(wr_buf + i) != *(rd_buf + i))
256 free_pages((unsigned long)wr_buf, order);
257 free_pages((unsigned long)rd_buf, order);
262 int solo_p2m_init(struct solo_dev *solo_dev)
264 struct solo_p2m_dev *p2m_dev;
267 for (i = 0; i < SOLO_NR_P2M; i++) {
268 p2m_dev = &solo_dev->p2m_dev[i];
270 mutex_init(&p2m_dev->mutex);
271 init_completion(&p2m_dev->completion);
273 solo_reg_write(solo_dev, SOLO_P2M_CONTROL(i), 0);
274 solo_reg_write(solo_dev, SOLO_P2M_CONFIG(i),
275 SOLO_P2M_CSC_16BIT_565 |
276 SOLO_P2M_DESC_INTR_OPT |
277 SOLO_P2M_DMA_INTERVAL(0) |
278 SOLO_P2M_PCI_MASTER_MODE);
279 solo_irq_on(solo_dev, SOLO_IRQ_P2M(i));
282 /* Find correct SDRAM size */
283 for (solo_dev->sdram_size = 0, i = 2; i >= 0; i--) {
284 solo_reg_write(solo_dev, SOLO_DMA_CTRL,
285 SOLO_DMA_CTRL_REFRESH_CYCLE(1) |
286 SOLO_DMA_CTRL_SDRAM_SIZE(i) |
287 SOLO_DMA_CTRL_SDRAM_CLK_INVERT |
288 SOLO_DMA_CTRL_READ_CLK_SELECT |
289 SOLO_DMA_CTRL_LATENCY(1));
291 solo_reg_write(solo_dev, SOLO_SYS_CFG, solo_dev->sys_config |
293 solo_reg_write(solo_dev, SOLO_SYS_CFG, solo_dev->sys_config);
297 if (solo_p2m_test(solo_dev, 0x07ff0000, 0x00010000) ||
298 solo_p2m_test(solo_dev, 0x05ff0000, 0x00010000))
303 if (solo_p2m_test(solo_dev, 0x03ff0000, 0x00010000))
308 if (solo_p2m_test(solo_dev, 0x01ff0000, 0x00010000))
312 solo_dev->sdram_size = (32 << 20) << i;
316 if (!solo_dev->sdram_size) {
317 dev_err(&solo_dev->pdev->dev, "Error detecting SDRAM size\n");
321 if (SOLO_SDRAM_END(solo_dev) > solo_dev->sdram_size) {
322 dev_err(&solo_dev->pdev->dev,
323 "SDRAM is not large enough (%u < %u)\n",
324 solo_dev->sdram_size, SOLO_SDRAM_END(solo_dev));