1 /*
2 	Mantis PCI bridge driver
3 
4 	Copyright (C) Manu Abraham (abraham.manu@gmail.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 	GNU General Public License for more details.
15 
16 	You should have received a copy of the GNU General Public License
17 	along with this program; if not, write to the Free Software
18 	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/kernel.h>
24 #include <linux/pci.h>
25 #include <linux/slab.h>
26 #include <asm/irq.h>
27 #include <linux/interrupt.h>
28 
29 #include "dmxdev.h"
30 #include "dvbdev.h"
31 #include "dvb_demux.h"
32 #include "dvb_frontend.h"
33 #include "dvb_net.h"
34 
35 #include "mantis_common.h"
36 
37 #include "mantis_vp1033.h"
38 #include "mantis_vp1034.h"
39 #include "mantis_vp1041.h"
40 #include "mantis_vp2033.h"
41 #include "mantis_vp2040.h"
42 #include "mantis_vp3030.h"
43 
44 #include "mantis_dma.h"
45 #include "mantis_ca.h"
46 #include "mantis_dvb.h"
47 #include "mantis_uart.h"
48 #include "mantis_ioc.h"
49 #include "mantis_pci.h"
50 #include "mantis_i2c.h"
51 #include "mantis_reg.h"
52 
53 static unsigned int verbose;
54 module_param(verbose, int, 0644);
55 MODULE_PARM_DESC(verbose, "verbose startup messages, default is 0 (no)");
56 
57 static int devs;
58 
59 #define DRIVER_NAME	"Mantis"
60 
61 static char *label[10] = {
62 	"DMA",
63 	"IRQ-0",
64 	"IRQ-1",
65 	"OCERR",
66 	"PABRT",
67 	"RIPRR",
68 	"PPERR",
69 	"FTRGT",
70 	"RISCI",
71 	"RACK"
72 };
73 
mantis_irq_handler(int irq,void * dev_id)74 static irqreturn_t mantis_irq_handler(int irq, void *dev_id)
75 {
76 	u32 stat = 0, mask = 0, lstat = 0;
77 	u32 rst_stat = 0, rst_mask = 0;
78 
79 	struct mantis_pci *mantis;
80 	struct mantis_ca *ca;
81 
82 	mantis = (struct mantis_pci *) dev_id;
83 	if (unlikely(mantis == NULL)) {
84 		dprintk(MANTIS_ERROR, 1, "Mantis == NULL");
85 		return IRQ_NONE;
86 	}
87 	ca = mantis->mantis_ca;
88 
89 	stat = mmread(MANTIS_INT_STAT);
90 	mask = mmread(MANTIS_INT_MASK);
91 	lstat = stat & ~MANTIS_INT_RISCSTAT;
92 	if (!(stat & mask))
93 		return IRQ_NONE;
94 
95 	rst_mask  = MANTIS_GPIF_WRACK  |
96 		    MANTIS_GPIF_OTHERR |
97 		    MANTIS_SBUF_WSTO   |
98 		    MANTIS_GPIF_EXTIRQ;
99 
100 	rst_stat  = mmread(MANTIS_GPIF_STATUS);
101 	rst_stat &= rst_mask;
102 	mmwrite(rst_stat, MANTIS_GPIF_STATUS);
103 
104 	mantis->mantis_int_stat = stat;
105 	mantis->mantis_int_mask = mask;
106 	dprintk(MANTIS_DEBUG, 0, "\n-- Stat=<%02x> Mask=<%02x> --", stat, mask);
107 	if (stat & MANTIS_INT_RISCEN) {
108 		dprintk(MANTIS_DEBUG, 0, "<%s>", label[0]);
109 	}
110 	if (stat & MANTIS_INT_IRQ0) {
111 		dprintk(MANTIS_DEBUG, 0, "<%s>", label[1]);
112 		mantis->gpif_status = rst_stat;
113 		wake_up(&ca->hif_write_wq);
114 		schedule_work(&ca->hif_evm_work);
115 	}
116 	if (stat & MANTIS_INT_IRQ1) {
117 		dprintk(MANTIS_DEBUG, 0, "<%s>", label[2]);
118 		schedule_work(&mantis->uart_work);
119 	}
120 	if (stat & MANTIS_INT_OCERR) {
121 		dprintk(MANTIS_DEBUG, 0, "<%s>", label[3]);
122 	}
123 	if (stat & MANTIS_INT_PABORT) {
124 		dprintk(MANTIS_DEBUG, 0, "<%s>", label[4]);
125 	}
126 	if (stat & MANTIS_INT_RIPERR) {
127 		dprintk(MANTIS_DEBUG, 0, "<%s>", label[5]);
128 	}
129 	if (stat & MANTIS_INT_PPERR) {
130 		dprintk(MANTIS_DEBUG, 0, "<%s>", label[6]);
131 	}
132 	if (stat & MANTIS_INT_FTRGT) {
133 		dprintk(MANTIS_DEBUG, 0, "<%s>", label[7]);
134 	}
135 	if (stat & MANTIS_INT_RISCI) {
136 		dprintk(MANTIS_DEBUG, 0, "<%s>", label[8]);
137 		mantis->busy_block = (stat & MANTIS_INT_RISCSTAT) >> 28;
138 		tasklet_schedule(&mantis->tasklet);
139 	}
140 	if (stat & MANTIS_INT_I2CDONE) {
141 		dprintk(MANTIS_DEBUG, 0, "<%s>", label[9]);
142 		wake_up(&mantis->i2c_wq);
143 	}
144 	mmwrite(stat, MANTIS_INT_STAT);
145 	stat &= ~(MANTIS_INT_RISCEN   | MANTIS_INT_I2CDONE |
146 		  MANTIS_INT_I2CRACK  | MANTIS_INT_PCMCIA7 |
147 		  MANTIS_INT_PCMCIA6  | MANTIS_INT_PCMCIA5 |
148 		  MANTIS_INT_PCMCIA4  | MANTIS_INT_PCMCIA3 |
149 		  MANTIS_INT_PCMCIA2  | MANTIS_INT_PCMCIA1 |
150 		  MANTIS_INT_PCMCIA0  | MANTIS_INT_IRQ1	   |
151 		  MANTIS_INT_IRQ0     | MANTIS_INT_OCERR   |
152 		  MANTIS_INT_PABORT   | MANTIS_INT_RIPERR  |
153 		  MANTIS_INT_PPERR    | MANTIS_INT_FTRGT   |
154 		  MANTIS_INT_RISCI);
155 
156 	if (stat)
157 		dprintk(MANTIS_DEBUG, 0, "<Unknown> Stat=<%02x> Mask=<%02x>", stat, mask);
158 
159 	dprintk(MANTIS_DEBUG, 0, "\n");
160 	return IRQ_HANDLED;
161 }
162 
mantis_pci_probe(struct pci_dev * pdev,const struct pci_device_id * pci_id)163 static int __devinit mantis_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
164 {
165 	struct mantis_pci *mantis;
166 	struct mantis_hwconfig *config;
167 	int err = 0;
168 
169 	mantis = kzalloc(sizeof(struct mantis_pci), GFP_KERNEL);
170 	if (mantis == NULL) {
171 		printk(KERN_ERR "%s ERROR: Out of memory\n", __func__);
172 		err = -ENOMEM;
173 		goto fail0;
174 	}
175 
176 	mantis->num		= devs;
177 	mantis->verbose		= verbose;
178 	mantis->pdev		= pdev;
179 	config			= (struct mantis_hwconfig *) pci_id->driver_data;
180 	config->irq_handler	= &mantis_irq_handler;
181 	mantis->hwconfig	= config;
182 
183 	err = mantis_pci_init(mantis);
184 	if (err) {
185 		dprintk(MANTIS_ERROR, 1, "ERROR: Mantis PCI initialization failed <%d>", err);
186 		goto fail1;
187 	}
188 
189 	err = mantis_stream_control(mantis, STREAM_TO_HIF);
190 	if (err < 0) {
191 		dprintk(MANTIS_ERROR, 1, "ERROR: Mantis stream control failed <%d>", err);
192 		goto fail1;
193 	}
194 
195 	err = mantis_i2c_init(mantis);
196 	if (err < 0) {
197 		dprintk(MANTIS_ERROR, 1, "ERROR: Mantis I2C initialization failed <%d>", err);
198 		goto fail2;
199 	}
200 
201 	err = mantis_get_mac(mantis);
202 	if (err < 0) {
203 		dprintk(MANTIS_ERROR, 1, "ERROR: Mantis MAC address read failed <%d>", err);
204 		goto fail2;
205 	}
206 
207 	err = mantis_dma_init(mantis);
208 	if (err < 0) {
209 		dprintk(MANTIS_ERROR, 1, "ERROR: Mantis DMA initialization failed <%d>", err);
210 		goto fail3;
211 	}
212 
213 	err = mantis_dvb_init(mantis);
214 	if (err < 0) {
215 		dprintk(MANTIS_ERROR, 1, "ERROR: Mantis DVB initialization failed <%d>", err);
216 		goto fail4;
217 	}
218 	err = mantis_uart_init(mantis);
219 	if (err < 0) {
220 		dprintk(MANTIS_ERROR, 1, "ERROR: Mantis UART initialization failed <%d>", err);
221 		goto fail6;
222 	}
223 
224 	devs++;
225 
226 	return err;
227 
228 
229 	dprintk(MANTIS_ERROR, 1, "ERROR: Mantis UART exit! <%d>", err);
230 	mantis_uart_exit(mantis);
231 
232 fail6:
233 fail4:
234 	dprintk(MANTIS_ERROR, 1, "ERROR: Mantis DMA exit! <%d>", err);
235 	mantis_dma_exit(mantis);
236 
237 fail3:
238 	dprintk(MANTIS_ERROR, 1, "ERROR: Mantis I2C exit! <%d>", err);
239 	mantis_i2c_exit(mantis);
240 
241 fail2:
242 	dprintk(MANTIS_ERROR, 1, "ERROR: Mantis PCI exit! <%d>", err);
243 	mantis_pci_exit(mantis);
244 
245 fail1:
246 	dprintk(MANTIS_ERROR, 1, "ERROR: Mantis free! <%d>", err);
247 	kfree(mantis);
248 
249 fail0:
250 	return err;
251 }
252 
mantis_pci_remove(struct pci_dev * pdev)253 static void __devexit mantis_pci_remove(struct pci_dev *pdev)
254 {
255 	struct mantis_pci *mantis = pci_get_drvdata(pdev);
256 
257 	if (mantis) {
258 
259 		mantis_uart_exit(mantis);
260 		mantis_dvb_exit(mantis);
261 		mantis_dma_exit(mantis);
262 		mantis_i2c_exit(mantis);
263 		mantis_pci_exit(mantis);
264 		kfree(mantis);
265 	}
266 	return;
267 }
268 
269 static struct pci_device_id mantis_pci_table[] = {
270 	MAKE_ENTRY(TWINHAN_TECHNOLOGIES, MANTIS_VP_1033_DVB_S, &vp1033_config),
271 	MAKE_ENTRY(TWINHAN_TECHNOLOGIES, MANTIS_VP_1034_DVB_S, &vp1034_config),
272 	MAKE_ENTRY(TWINHAN_TECHNOLOGIES, MANTIS_VP_1041_DVB_S2, &vp1041_config),
273 	MAKE_ENTRY(TECHNISAT, SKYSTAR_HD2_10, &vp1041_config),
274 	MAKE_ENTRY(TECHNISAT, SKYSTAR_HD2_20, &vp1041_config),
275 	MAKE_ENTRY(TERRATEC, CINERGY_S2_PCI_HD, &vp1041_config),
276 	MAKE_ENTRY(TWINHAN_TECHNOLOGIES, MANTIS_VP_2033_DVB_C, &vp2033_config),
277 	MAKE_ENTRY(TWINHAN_TECHNOLOGIES, MANTIS_VP_2040_DVB_C, &vp2040_config),
278 	MAKE_ENTRY(TECHNISAT, CABLESTAR_HD2, &vp2040_config),
279 	MAKE_ENTRY(TERRATEC, CINERGY_C, &vp2033_config),
280 	MAKE_ENTRY(TWINHAN_TECHNOLOGIES, MANTIS_VP_3030_DVB_T, &vp3030_config),
281 	{ }
282 };
283 
284 MODULE_DEVICE_TABLE(pci, mantis_pci_table);
285 
286 static struct pci_driver mantis_pci_driver = {
287 	.name		= DRIVER_NAME,
288 	.id_table	= mantis_pci_table,
289 	.probe		= mantis_pci_probe,
290 	.remove		= mantis_pci_remove,
291 };
292 
mantis_init(void)293 static int __devinit mantis_init(void)
294 {
295 	return pci_register_driver(&mantis_pci_driver);
296 }
297 
mantis_exit(void)298 static void __devexit mantis_exit(void)
299 {
300 	return pci_unregister_driver(&mantis_pci_driver);
301 }
302 
303 module_init(mantis_init);
304 module_exit(mantis_exit);
305 
306 MODULE_DESCRIPTION("MANTIS driver");
307 MODULE_AUTHOR("Manu Abraham");
308 MODULE_LICENSE("GPL");
309