summaryrefslogtreecommitdiffstats
path: root/drivers/media/dvb/mantis/mantis_pci.c
blob: 68ff1b2a0b3d3667ec05e4bbcfbdb3b0c7a18bd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
	Mantis PCI bridge driver

	Copyright (C) 2005, 2006 Manu Abraham (abraham.manu@gmail.com)

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <asm/io.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include <linux/kmod.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/device.h>
#include "mantis_common.h"
#include "mantis_core.h"

#include <asm/irq.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/interrupt.h>

unsigned int verbose = 1;
module_param(verbose, int, 0644);
MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");

unsigned int devs;

#define PCI_VENDOR_ID_MANTIS			0x1822
#define PCI_DEVICE_ID_MANTIS_R11		0x4e35
#define DRIVER_NAME				"Mantis"

static struct pci_device_id mantis_pci_table[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11) },
	{ 0 },
};

MODULE_DEVICE_TABLE(pci, mantis_pci_table);

static irqreturn_t mantis_pci_irq(int irq, void *dev_id)
{
	int i = 0, interrupts = 0;
	u32 stat = 0, mask = 0, lstat = 0, mstat = 0;
	struct mantis_pci *mantis;

	mantis = (struct mantis_pci *) dev_id;
	if (unlikely(mantis == NULL)) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis == NULL");
		return IRQ_NONE;
	}
	stat = mmread(MANTIS_INT_STAT);
	mask = mmread(MANTIS_INT_MASK);
	mstat = lstat = stat & ~MANTIS_INT_RISCSTAT;

	if (!(stat & mask)) {
		dprintk(verbose, MANTIS_DEBUG, 1, "Not ours !");
		return IRQ_NONE;
	}
	mmwrite(lstat, MANTIS_INT_STAT);
	interrupts = hweight32(stat);
	dprintk(verbose, MANTIS_DEBUG, 0, "=== Interrupts[%04x/%04x]=%d [", stat, mask, interrupts);

	while (lstat) {
		if (lstat & MANTIS_INT_RISCEN) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* DMA enabl *");
			lstat &= ~MANTIS_INT_RISCEN;

		} else if (lstat & MANTIS_INT_I2CRACK) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* I2C R-ACK *");
			mantis->mantis_int_stat = stat;
			mantis->mantis_int_mask = mask;
			wake_up(&mantis->i2c_wq);
			lstat &= ~MANTIS_INT_I2CRACK;

		} else if (lstat & MANTIS_INT_PCMCIA7) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* PCMCIA-07 *");
			lstat &= ~MANTIS_INT_PCMCIA7;

		} else if (lstat & MANTIS_INT_PCMCIA6) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* PCMCIA-06 *");
			lstat &= ~MANTIS_INT_PCMCIA6;

		} else if (lstat & MANTIS_INT_PCMCIA5) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* PCMCIA-05 *");
			lstat &= ~MANTIS_INT_PCMCIA5;

		} else if (lstat & MANTIS_INT_PCMCIA4) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* PCMCIA-04 *");
			lstat &= ~MANTIS_INT_PCMCIA4;

		} else if (lstat & MANTIS_INT_PCMCIA3) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* PCMCIA-03 *");
			lstat &= ~MANTIS_INT_PCMCIA3;

		} else if (lstat & MANTIS_INT_PCMCIA2) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* PCMCIA-02 *");
			lstat &= ~MANTIS_INT_PCMCIA2;

		} else if (lstat & MANTIS_INT_PCMCIA1) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* PCMCIA-01 *");
			lstat &= ~MANTIS_INT_PCMCIA1;

		} else if (lstat & MANTIS_INT_PCMCIA0) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* PCMCIA-00 *");
			lstat &= ~MANTIS_INT_PCMCIA0;

		} else if (lstat & MANTIS_INT_IRQ0) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* INT IRQ-0 *");
			lstat &= ~MANTIS_INT_IRQ0;

		} else if (lstat & MANTIS_INT_IRQ1) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* INT IRQ-1 *");
			lstat &= ~MANTIS_INT_IRQ1;

		} else if (lstat & MANTIS_INT_OCERR) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* INT OCERR *");
			lstat &= ~MANTIS_INT_OCERR;

		} else if (lstat & MANTIS_INT_PABORT) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* INT PABRT *");
			lstat &= ~MANTIS_INT_PABORT;

		} else if (lstat & MANTIS_INT_RIPERR) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* INT RIPRR *");
			lstat &= ~MANTIS_INT_RIPERR;

		} else if (lstat & MANTIS_INT_PPERR) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* INT PPERR *");
			lstat &= ~MANTIS_INT_PPERR;

		} else if (lstat & MANTIS_INT_FTRGT) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* INT FTRGT *");
			lstat &= ~MANTIS_INT_FTRGT;

		} else if (lstat & MANTIS_INT_RISCI) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* INT RISCI *");
			mantis->finished_block = (stat & MANTIS_INT_RISCSTAT) >> 28;
			tasklet_schedule(&mantis->tasklet);
			lstat &= ~MANTIS_INT_RISCI;

		} else if (lstat & MANTIS_INT_I2CDONE) {
			dprintk(verbose, MANTIS_DEBUG, 0, "* I2C DONE  *");
			mantis->mantis_int_stat = stat;
			mantis->mantis_int_mask = mask;
			lstat &= ~MANTIS_INT_I2CDONE;
		} else {
			dprintk(verbose, MANTIS_DEBUG, 0,
				"* Unknown [%04x/%04x] *", stat, mask);
			break;
		}
		i++;
		if (i > interrupts) {
			dprintk(verbose, MANTIS_ERROR, 1, "going Loopy ! -- BREAK --");
			break;
		}
	}
	dprintk(verbose, MANTIS_DEBUG, 0, "] ===\n");

	return IRQ_HANDLED;
}


static int __devinit mantis_pci_probe(struct pci_dev *pdev,
				      const struct pci_device_id *mantis_pci_table)
{
	u8 revision, latency;
	struct mantis_pci *mantis;
	int ret = 0;

	devs++;

	mantis = kmalloc(sizeof (struct mantis_pci), GFP_KERNEL);
	if (mantis == NULL) {
		printk("%s: Out of memory\n", __func__);
		ret = -ENOMEM;
		goto err;
	}
	memset(mantis, 0, sizeof (struct mantis_pci));
	mantis->num = devs;
	if (pci_enable_device(pdev)) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed");
		ret = -ENODEV;
		goto err;
	}
	mantis->mantis_addr = pci_resource_start(pdev, 0);
	if (!request_mem_region(pci_resource_start(pdev, 0),
			pci_resource_len(pdev, 0), DRIVER_NAME)) {
		ret = -ENODEV;
		goto err0;
	}

	if ((mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000)) == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
		ret = -ENODEV;
		goto err1;
	}

	// Clear and disable all interrupts at startup
	// to avoid lockup situations
	mmwrite(0x00, MANTIS_INT_MASK);
	if (request_irq(pdev->irq, mantis_pci_irq, IRQF_SHARED | IRQF_DISABLED,
						DRIVER_NAME, mantis) < 0) {

		dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed");
		ret = -ENODEV;
		goto err2;
	}
	pci_set_master(pdev);
	pci_set_drvdata(pdev, mantis);
	pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
	pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
	mantis->latency = latency;
	mantis->revision = revision;
	mantis->pdev = pdev;
	init_waitqueue_head(&mantis->i2c_wq);

	// CAM bypass
	//mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_IRQ1, MANTIS_INT_MASK);
	dprintk(verbose, MANTIS_INFO, 1, "gpif status: %04x  irqcfg: %04x", mmread(0x9c), mmread(0x98));
	if ((mmread(0x9c) & 0x200) != 0) { //CAM inserted
		msleep_interruptible(1);
		if ((mmread(0x9c) & 0x200) != 0)
			mmwrite(((mmread(0x98) | 0x01) & ~0x02), 0x98);
		else
			mmwrite(((mmread(0x98) | 0x02) & ~0x01), 0x98);

	} else {
		mmwrite(((mmread(0x98) | 0x02) & ~0x01), 0x98);
	}
	mantis_set_direction(mantis, 0);

	// default latency if none specified
	if (!latency)
		pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
	dprintk(verbose, MANTIS_ERROR, 0, "Mantis Rev %d, ",
		mantis->revision);

	dprintk(verbose, MANTIS_ERROR, 0,
		"irq: %d, latency: %d\n memory: 0x%lx, mmio: 0x%p\n",
		pdev->irq, mantis->latency,
		mantis->mantis_addr, mantis->mantis_mmio);

	// No more PCI specific stuff !
	if (mantis_core_init(mantis) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis core init failed");
		ret = -ENODEV;
		goto err2;
	}

	return 0;

	// Error conditions ..
err2:
	dprintk(verbose, MANTIS_DEBUG, 1, "Err: IO Unmap");
	if (mantis->mantis_mmio)
		iounmap(mantis->mantis_mmio);
err1:
	dprintk(verbose, MANTIS_DEBUG, 1, "Err: Release regions");
	release_mem_region(pci_resource_start(pdev, 0),
				pci_resource_len(pdev, 0));
	pci_disable_device(pdev);
err0:
	dprintk(verbose, MANTIS_DEBUG, 1, "Err: Free");
	kfree(mantis);
err:
	dprintk(verbose, MANTIS_DEBUG, 1, "Err:");
	return ret;
}

static void __devexit mantis_pci_remove(struct pci_dev *pdev)
{
	struct mantis_pci *mantis = pci_get_drvdata(pdev);

	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Aeio, Mantis NULL ptr");
		return;
	}
	mantis_core_exit(mantis);
	dprintk(verbose, MANTIS_ERROR, 1, "Removing -->Mantis irq: %d, latency: %d\n memory: 0x%lx, mmio: 0x%p",
		pdev->irq, mantis->latency, mantis->mantis_addr,
		mantis->mantis_mmio);

	free_irq(pdev->irq, mantis);
	pci_release_regions(pdev);
	if (mantis_dma_exit(mantis) < 0)
		dprintk(verbose, MANTIS_ERROR, 1, "DMA exit failed");

	pci_set_drvdata(pdev, NULL);
	pci_disable_device(pdev);
	kfree(mantis);
}

static struct pci_driver mantis_pci_driver = {
	.name = DRIVER_NAME,
	.id_table = mantis_pci_table,
	.probe = mantis_pci_probe,
	.remove = mantis_pci_remove,
};

static int __devinit mantis_pci_init(void)
{
	return pci_register_driver(&mantis_pci_driver);
}

static void __devexit mantis_pci_exit(void)
{
	pci_unregister_driver(&mantis_pci_driver);
}

module_init(mantis_pci_init);
module_exit(mantis_pci_exit);

MODULE_DESCRIPTION("Mantis PCI DTV bridge driver");
MODULE_AUTHOR("Manu Abraham");
MODULE_LICENSE("GPL");