Annotation of margi2/margi.c, revision 1.4
1.1 cvs 1: /*
2: margi.c
3:
4: Copyright (C) Marcus Metzler for convergence integrated media.
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 "margi.h"
22:
23: #include <pcmcia/version.h>
24: #include <pcmcia/cs_types.h>
25: #include <pcmcia/cs.h>
26: #include <pcmcia/cistpl.h>
27: #include <pcmcia/cisreg.h>
28: #include <pcmcia/bus_ops.h>
29: #include <pcmcia/ds.h>
30:
31:
32:
33: #include "l64014.h"
34: #include "l64021.h"
35: #include "i2c.h"
36: #include "decoder.h"
37: #include "dram.h"
38: #include "video.h"
39: #include "cvdv.h"
40:
41: //#define USE_BH 1
42: #ifdef USE_BH
43: #define MARGI_BH 31
44: // shouldn't be a number, but then MARGI_BH must be entered into interrupt.h
45: #endif
46:
47: MODULE_AUTHOR(AUTHOR);
48: MODULE_DESCRIPTION(MEDDEVNAME " Driver V." DVERSION);
49:
50: #ifdef PCMCIA_DEBUG
51: static int pc_debug = PCMCIA_DEBUG;
52: MODULE_PARM(pc_debug, "i");
53: #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
54: static char *version = "margi_cs.c 0.42 11/1/2000 (Marcus Metzler)";
55: #else
56: #define DEBUG(n, args...)
57: #endif
58:
59: #define MAX_DEV 4
60: #define DEVICE_NR(minor) ((minor)>>4)
61:
62: /*====================================================================*/
63:
64: /* Parameters that can be set with 'insmod' */
65:
66: /* Release IO ports after configuration? */
67: static int free_ports = 0;
68:
69: /* The old way: bit map of interrupts to choose from */
70: /* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, and 3 */
71: static u_int irq_mask = 0xdeb8;
72: /* Newer, simpler way of listing specific interrupts */
73: static int irq_list[4] = { -1 };
74:
75: MODULE_PARM(free_ports, "i");
76: MODULE_PARM(irq_mask, "i");
77: MODULE_PARM(irq_list, "1-4i");
78:
79: extern unsigned int major_device_number;
80: extern struct file_operations cvdv_fileops;
81:
82:
83: typedef struct margi_info_t {
84: dev_link_t link;
85: dev_node_t node;
86: struct cvdv_cards card;
87: int stop;
88: } margi_info_t;
89:
90:
91:
92: /*
93: The event() function is this driver's Card Services event handler.
94: It will be called by Card Services when an appropriate card status
95: event is received. The config() and release() entry points are
96: used to configure or release a socket, in response to card
97: insertion and ejection events. They are invoked from the margi
98: event handler.
99: */
100:
101: static void margi_config(dev_link_t * link);
102: static void margi_release(u_long arg);
103: static int margi_event(event_t event, int priority,
104: event_callback_args_t * args);
105: /*
106: The attach() and detach() entry points are used to create and destroy
107: "instances" of the driver, where each instance represents everything
108: needed to manage one actual PCMCIA card.
109: */
110:
111: static dev_link_t *margi_attach(void);
112: static void margi_detach(dev_link_t *);
113: static u_char read_lsi_status(struct cvdv_cards *card);
114:
115: /*
116: You'll also need to prototype all the functions that will actually
117: be used to talk to your device. See 'memory_cs' for a good example
118: of a fully self-sufficient driver; the other drivers rely more or
119: less on other parts of the kernel.
120: */
121:
122: /*
123: The dev_info variable is the "key" that is used to match up this
124: device driver with appropriate cards, through the card configuration
125: database.
126: */
127:
128: static dev_link_t *dev_table[MAX_DEV] = { NULL, /* ... */ };
129:
130: static dev_info_t dev_info = "margi_cs";
131:
132: /*
133: A linked list of "instances" of the margi device. Each actual
134: PCMCIA card corresponds to one device instance, and is described
135: by one dev_link_t structure (defined in ds.h).
136:
137: You may not want to use a linked list for this -- for example, the
138: memory card driver uses an array of dev_link_t pointers, where minor
139: device numbers are used to derive the corresponding array index.
140: */
141:
142: static dev_link_t *dev_list = NULL;
143:
144: /*
145: A dev_link_t structure has fields for most things that are needed
146: to keep track of a socket, but there will usually be some device
147: specific information that also needs to be kept track of. The
148: 'priv' pointer in a dev_link_t structure can be used to point to
149: a device-specific private data structure, like this.
150:
151: To simplify the data structure handling, we actually include the
152: dev_link_t structure in the device's private data structure.
153:
154: A driver needs to provide a dev_node_t structure for each device
155: on a card. In some cases, there is only one device per card (for
156: example, ethernet cards, modems). In other cases, there may be
157: many actual or logical devices (SCSI adapters, memory cards with
158: multiple partitions). The dev_node_t structures need to be kept
159: in a linked list starting at the 'dev' field of a dev_link_t
160: structure. We allocate them in the card's private data structure,
161: because they generally shouldn't be allocated dynamically.
162:
163: In this case, we also provide a flag to indicate if a device is
164: "stopped" due to a power management event, or card ejection. The
165: device IO routines can use a flag like this to throttle IO to a
166: card that is not ready to accept it.
167:
168: The bus_operations pointer is used on platforms for which we need
169: to use special socket-specific versions of normal IO primitives
170: (inb, outb, readb, writeb, etc) for card IO.
171: */
172:
173:
174: void DACSetFrequency(struct cvdv_cards *card, int khz, int multiple) {
175: u8 b = read_indexed_register(card, IIO_OSC_AUD);
176:
177: b &= 0xf8;
178:
179: switch (khz){
1.2 rjkm 180: case 32:
181: b |= 0x04;
182: break;
1.1 cvs 183: case 48:
184: b |= 0x00;
185: break;
186: case 44:
187: b |= 0x01;
188: break;
189: case 96:
190: b |= 0x02;
191: break;
192: default:
193: b |= 0x00;
194: break;
195: }
196: write_indexed_register(card, IIO_OSC_AUD, b);
197:
198: }
199:
200: int MargiFreeBuffers(struct cvdv_cards *card)
201: {
202: printk(KERN_DEBUG LOGNAME ": -- MargiFreeBuffers\n");
203:
204: ring_destroy(&(card->rbuf));
205: card->use_ring = 0;
206: return 0;
207: }
208:
209:
210: int MargiSetBuffers(struct cvdv_cards *card, u32 size)
211: {
212: MargiFreeBuffers(card);
213: printk(KERN_DEBUG LOGNAME ": -- MargiSetBuffers(%d)\n",
214: size);
215:
216: ring_init(&(card->rbuf),size);
217: card->use_ring = 1;
218: return 0;
219:
220: }
221:
222: int MargiFlush (struct cvdv_cards *card)
223: {
224: int co = 0;
225: int i;
226: for (i=0;i<100;i++)
227: MargiPush(card, 32, FlushPacket);
228: while (ring_write_rest(&(card->rbuf)) && co<100) co++;
229: VideoSetBackground(card, 1, 0, 0, 0); // black
230:
231: ring_flush(&(card->rbuf));
232: card->DMAABusy = 0;
233:
234:
235: DecoderStopChannel(card);
236: DecoderStreamReset(card);
237: DecoderSetupReset(card);
238: card->channelrun = 0;
239:
240: printk(KERN_DEBUG LOGNAME ": Margi Flush \n");
241: return 0;
242: }
243:
244:
245:
246:
247: int MargiPush(struct cvdv_cards *card, int count, const char *data)
248: {
249: // printk(KERN_DEBUG LOGNAME ": -- MargiPush(%d,0x%08lX)\n",count,(long)data);
250: if (!card->use_ring)
251: return 0;
252:
253: if (!card->channelrun){
254: DecoderStartChannel(card);
255: }
256:
257: card->DMAABusy = 1;
258: /*
259: while (ring_write_rest(&(card->rbuf)) < count && co < 100){
260: printk("buffer waiting to write\n");
261: co++;
262: // interruptible_sleep_on(&card->wqA);
263: // if (signal_pending(current))
264: // return 0;
265: }
266: */
267: count = ring_write(&(card->rbuf),data,count);
268: return count;
269: }
270:
271: int DecoderStartChannel(struct cvdv_cards *card)
272: {
273: //DecoderSetByte(card,0x007,0x01); // channel start
274: DecoderMaskByte(card, 0x007, 0xC3, 0xC3); // channel start
275: //DecoderSetByte(card,0x005,0x01); // invert channel clock on
276: //DecoderDelByte(card,0x005,0x02); // channel request mode off
277: //DecoderDelByte(card,0x005,0x04); // channel pause off
278: //DecoderDelByte(card,0x005,0x08); // channel bypass enable off
279: #ifdef BYPASS
280: DecoderMaskByte(card,0x005,0x0F,0x08);
281: #else
282: DecoderMaskByte(card,0x005,0x0F,0x01);
283: #endif
284: card->channelrun = 1;
285: return 0;
286: }
287:
288: int DecoderStopChannel(struct cvdv_cards *card)
289: {
290: // DecoderDelByte(card,0x007,0x01); // channel reset
291: DecoderMaskByte(card, 0x007, 0xC3, 0xC2); // channel reset
292: DecoderSetByte(card, 0x005, 0x04); // channel pause
293: card->channelrun = 0;
294: return 0;
295: }
296:
297: u32 DecoderGetAudioBufferSpace(struct cvdv_cards *card)
298: {
299:
300: u32 MaxSize, Size;
301:
302: MaxSize = card->AudioESSize;
303: Size = DecoderGetAudioESLevel(card);
304:
305: if (Size>MaxSize)
306: return 0;
307: return (MaxSize - Size);
308:
309: }
310:
311: u32 DecoderGetVideoBufferSpace(struct cvdv_cards *card)
312: {
313:
314: u32 MaxSize, Size;
315:
316: MaxSize = card->VideoESSize;
317: Size = DecoderGetVideoESLevel(card);
318:
319: if (Size>MaxSize)
320: return 0;
321: return (MaxSize - Size);
322:
323: }
324:
325: u32 DecoderGetBufferSpace(struct cvdv_cards *card)
326: {
327: u32 audio,video;
328:
329: audio = DecoderGetAudioBufferSpace(card);
330: video = DecoderGetVideoBufferSpace(card);
331:
1.2 rjkm 332: if (audio > 2048) audio -= 2048;
333: if (video > 16384) video -= 16384;
1.1 cvs 334:
335: if (audio < video) return audio;
336: return video;
337: }
338:
339: static int ringDMA (struct cvdv_cards *card){
340:
341: u32 size = 0;
342: u_char stat;
343: dev_link_t *link = &(((margi_info_t *) card->margi)->link);
344: u32 count=0;
345: u8 data;
346:
347: count = ring_read_rest(&(card->rbuf));
1.4 ! rjkm 348: if (count < 2048) {
1.1 cvs 349: card->DMAABusy = 0;
350: wake_up_interruptible(&(card->wqA));
351: return 0;
1.4 ! rjkm 352: }
1.1 cvs 353: stat = read_lsi_status(card);
354: /*
355: printk(KERN_DEBUG LOGNAME
356: ": -- stat: %d readpos: %d writepos: %d \n",
357: stat,card->rbuf.read_pos,card->rbuf.write_pos);
358: */
359: if (stat & LSI_ARQ) {
360: stat = read_lsi_status(card);
361: }
362:
363: if (stat & LSI_READY){
364: data = read_indexed_register(card, IIO_LSI_CONTROL);
365: data |= RR;
366: write_indexed_register(card, IIO_LSI_CONTROL, data);
367: return 0;
368: }
369:
370: if ((stat & LSI_ARQ) == 0) {
371: size = DecoderGetBufferSpace(card);
372: if (count > size) count = size & 0xfffffffc;
373: if (count>=2048) count &=0xfffff800;
374: count &=0xfffffffc;
375:
1.4 ! rjkm 376: if (!size &&
! 377: !card->DecoderOpen){
! 378: printk(KERN_DEBUG LOGNAME
! 379: ": -- PREPARE IT ALREADY\n");
! 380: Prepare(card);
! 381: }
! 382:
! 383: if (count > size) count = size & 0xfffffffc;
1.1 cvs 384: /*
385: printk(KERN_DEBUG LOGNAME
1.4 ! rjkm 386: ": -- stat: %d length: %d size: %d startV: %d startA: %d\n",
! 387: stat,count,size, card->startingV, card->startingA);
1.1 cvs 388: */
389: if (count) {
390: ring_read_direct(&(card->rbuf),
391: link->io.BasePort1+DIO_LSI_STATUS,
392: count);
393: }
394: } else {
395: count = 0;
396: //card->DMAABusy = 0;
397: }
398:
399: return count;
400: }
401:
402:
403: u_char read_indexed_register(struct cvdv_cards * card, int addr)
404: {
405: dev_link_t *link = &(((margi_info_t *) card->margi)->link);
406:
407: outb(addr, link->io.BasePort1 + DIO_CONTROL_INDEX);
408: return (inb(link->io.BasePort1 + DIO_CONTROL_DATA));
409: }
410:
411:
412: void write_indexed_register(struct cvdv_cards *card, int addr, u_char data)
413: {
414: dev_link_t *link = &(((margi_info_t *) card->margi)->link);
415:
416: outb(addr, link->io.BasePort1 + DIO_CONTROL_INDEX);
417: outb(data, link->io.BasePort1 + DIO_CONTROL_DATA);
418: }
419:
420: void WriteByte(struct cvdv_cards *card, int addr, u_char data)
421: {
422: dev_link_t *link = &(((margi_info_t *) card->margi)->link);
423:
424: outb((u_char) (addr & 255),
425: link->io.BasePort1 + DIO_LSI_INDEX_LOW);
426: outb(((addr & 256) ? 1 : 0),
427: link->io.BasePort1 + DIO_LSI_INDEX_HIGH);
428: outb(data, link->io.BasePort1 + DIO_LSI_DATA);
429: }
430:
431: u_char ReadByte(struct cvdv_cards *card, int addr)
432: {
433: dev_link_t *link = &(((margi_info_t *) card->margi)->link);
434:
435: outb((u_char) (addr & 255),
436: link->io.BasePort1 + DIO_LSI_INDEX_LOW);
437: outb(((addr & 256) ? 1 : 0),
438: link->io.BasePort1 + DIO_LSI_INDEX_HIGH);
439: return inb(link->io.BasePort1 + DIO_LSI_DATA);
440: }
441:
442: void MaskByte(struct cvdv_cards *card, int addr, u_char mask, u_char bits)
443: {
444: WriteByte(card, addr, (ReadByte(card, addr) & ~(mask)) | (bits));
445: }
446:
447:
448:
449: #define MAXWRITE 49000
450: #define MAX_COUNT 10
451:
452: #ifdef USE_BH
453: struct cvdv_cards *bh_card;
454:
455: static void do_margi_bh(void)
456: {
457: struct cvdv_cards *card = bh_card;
458: #else
459:
460: static void do_margi(struct cvdv_cards *card)
461: {
462:
463: #endif
464: int countA, countB;
465: int try;
466:
467: countA = 0;
468: countB = 0;
469:
470: card->currentType = 0;
471: for ( try = 0; try < MAX_COUNT ;try++)
472: if (countA < MAXWRITE){
473: int count = 0;
474: count = ringDMA(card);
475: countA += count;
476: if (!count)
477: try=MAX_COUNT;
478: } else break;
479:
480:
481: }
482:
483: void L64014Intr(int irq, void *dev_id, struct pt_regs *regs)
484: {
485: margi_info_t *margi = dev_id;
486: struct cvdv_cards *card = &(margi->card);
487: u_char dio_index, lsi_index_low, lsi_index_high;
488: u8 control,mask,stat;
489: int try;
490:
491: //save registers
492: dio_index = inb(margi->link.io.BasePort1 + DIO_CONTROL_INDEX);
493: lsi_index_low = inb(margi->link.io.BasePort1 + DIO_LSI_INDEX_LOW);
494: lsi_index_high =
495: inb(margi->link.io.BasePort1 + DIO_LSI_INDEX_HIGH);
496:
497: control= read_indexed_register(card, IIO_IRQ_CONTROL);
498: if (control & IRQ_EN){
499: mask = 0;
500: if ( control & DEC_EN ) mask |= DEC_INT;
501: if ( control & VSYNC_EN ) mask |= VSYNC_INT;
502: stat = read_indexed_register(card, IIO_IRQ_STATUS);
503: try = 0;
504: while ( (try++ < 100) && (stat & mask) ){
505:
506: if (stat & DEC_INT) {
507: write_indexed_register(card,IIO_IRQ_CONTROL,
508: control & (~DEC_EN));
509: write_indexed_register(card,IIO_IRQ_CONTROL,
510: control);
511:
512: if(card->use_ring){
513: L64021Intr(card);
514: }
515: }
516: if (stat & VSYNC_INT) {
517:
518: write_indexed_register(card,IIO_IRQ_CONTROL,
519: control & (~VSYNC_EN));
520: write_indexed_register(card,IIO_IRQ_CONTROL,
521: control);
522: if(card->use_ring){
523: L64021Intr(card);
524: }
525: if (card->DMAABusy){
526:
527: #ifdef USE_BH
528: bh_card = card;
529: mark_bh(MARGI_BH);
530: #else
531: do_margi(card);
532: #endif
533: }
534: }
535:
536: stat = read_indexed_register(card, IIO_IRQ_STATUS);
537: }
538: }
539:
540: //load registers
541: outb(dio_index, margi->link.io.BasePort1 + DIO_CONTROL_INDEX);
542: outb(lsi_index_low, margi->link.io.BasePort1 + DIO_LSI_INDEX_LOW);
543: outb(lsi_index_high,margi->link.io.BasePort1 + DIO_LSI_INDEX_HIGH);
544: }
545:
546: int L64014RemoveIntr(struct cvdv_cards *card)
547: {
548: printk(KERN_DEBUG LOGNAME ": -- L64014RemoveIntr\n");
549: // Disable the IRQ's
550: write_indexed_register(card, IIO_IRQ_CONTROL, 0x00);
551: if (!card->IntInstalled)
552: return 1;
553: L64021RemoveIntr(card);
554: return 0;
555: }
556:
557: void l64020Reset(struct cvdv_cards *card){
558: u8 data;
559:
560:
561: data = read_indexed_register(card, IIO_LSI_CONTROL);
562: data &= ~(RR | DR);
563: write_indexed_register(card, IIO_LSI_CONTROL, data);
564: mdelay(100);
565: data = read_indexed_register(card, IIO_LSI_CONTROL);
566: data |= DR;
567: write_indexed_register(card, IIO_LSI_CONTROL, data);
568:
569: data = read_indexed_register(card,IIO_GPIO_PINS);
570: data &= ~0x01;
571: write_indexed_register(card,IIO_GPIO_PINS,data);
572: data |= 0x01;
573: write_indexed_register(card,IIO_GPIO_PINS,data);
574:
575: //write_indexed_register(card, IIO_LSI_CONTROL, DR);
576: }
577:
578: int L64014Init(struct cvdv_cards *card)
579: {
580: u16 testram[16];
581: int i, err;
582:
583: printk(KERN_DEBUG LOGNAME ": -- L64014Init\n");
584: card->videomode = VIDEO_MODE;
585:
586: /* Reset 64020 */
587: write_indexed_register(card, IIO_GPIO_CONTROL, 0x01);
588: l64020Reset(card);
589: /* init GPIO */
590: write_indexed_register(card, IIO_GPIO_CONTROL, 0x01);
591: write_indexed_register(card, IIO_GPIO_PINS, 0xff);
592:
593: /* Set to PAL */
594: write_indexed_register(card, IIO_VIDEO_CONTROL0, 0);
595: write_indexed_register(card, IIO_VIDEO_CONTROL1, VMS_PAL);
596:
597: /* Set Audio freq */
598: write_indexed_register(card, IIO_OSC_AUD, 0x12);
599:
600: write_indexed_register(card, CSS_COMMAND, 0x01);
601:
602:
603: printk("CSID: %02x\n", I2CRead(card, 0, 0x3d));
604: card->i2c_addr = I2CRead(card, 0, 0x0f);
605: printk("I2CADDR: %02x\n", card->i2c_addr);
606:
607: I2CWrite(card, card->i2c_addr, CS_CONTROL0, 0x4a);
608: I2CWrite(card, card->i2c_addr, CS_CONTROL1, 0x04);
609: I2CWrite(card, card->i2c_addr, CS_SC_AMP, 0x15);
610: I2CWrite(card, card->i2c_addr, CS_SC_SYNTH0, 0x96);
611: I2CWrite(card, card->i2c_addr, CS_SC_SYNTH1, 0x15);
612: I2CWrite(card, card->i2c_addr, CS_SC_SYNTH2, 0x13);
613: I2CWrite(card, card->i2c_addr, CS_SC_SYNTH3, 0x54);
614:
615: I2CWrite(card, card->i2c_addr, CS_DAC, 0x87);
616: I2CWrite(card, card->i2c_addr, CS_BKG_COL, 0x03);
617:
618: printk("Decoder Status: %d\n", read_lsi_status(card));
619: printk("lsi stat %d\n", DecoderReadByte(card, 0x005));
620:
621: L64021Init(card);
622:
623: // Find out how much DRAM we have
624: card->DRAMSize = 0x00200000; // maximum size
625: do {
626: printk(KERN_DEBUG LOGNAME
627: ": Probing DRAM Size: 0x%08X (%d kByte) ... ",
628: card->DRAMSize, card->DRAMSize / 512);
629: for (i = 0; i < 8; i++)
630: testram[i] = rnd(0x100) | (rnd(0x100) << 8);
631: if (DRAMWriteWord(card, 0, 4, &testram[0], 0))
632: printk(KERN_DEBUG LOGNAME ": DRAM Write error.\n");
633: if (DRAMWriteWord
634: (card, card->DRAMSize - 4, 4, &testram[4],
635: 0)) printk(KERN_DEBUG LOGNAME
636: ": DRAM Write error.\n");
637: if (DRAMReadWord(card, 0, 4, &testram[8], 0))
638: printk(KERN_DEBUG LOGNAME ": DRAM Read error.\n");
639: if (DRAMReadWord
640: (card, card->DRAMSize - 4, 4, &testram[12],
641: 0)) printk(KERN_DEBUG LOGNAME ": DRAM Read error.\n");
642: err = 0;
643: for (i = 0; (!err) && (i < 8); i++)
644: if (testram[i] != testram[i + 8])
645: err = i + 1;
646: if (err) printk(" failed\n");
647: else printk(" ok\n");
648: /*
649: if (err)
650: printk(KERN_DEBUG LOGNAME
651: ": DRAM compare error at cell %d: 0x%04X %04X %04X %04X->0x%04X %04X %04X %04X / 0x%04X %04X %04X %04X->0x%04X %04X %04X %04X\n",
652: err, testram[0], testram[1], testram[2],
653: testram[3], testram[8], testram[9],
654: testram[10], testram[11], testram[4],
655: testram[5], testram[6], testram[7],
656: testram[12], testram[13], testram[14],
657: testram[15]);
658: */
659: if (err)
660: card->DRAMSize >>= 1;
661: } while (err && (card->DRAMSize >= 0x00100000));
662: printk(KERN_INFO LOGNAME ": DRAM Size: 0x%08X (%d kByte)\n",
663: card->DRAMSize, card->DRAMSize / 512);
664: if (card->DRAMSize < 0x00100000) { // minimum size
665: printk(KERN_INFO LOGNAME
666: ": DRAM ERROR: Not enough memory on card!\n");
667: return 1;
668: }
669: return 0;
670: }
671:
672:
673: void CardDeInit(struct cvdv_cards *card)
674: {
675: CloseCard(card);
676: MargiFlush(card);
677: MargiFreeBuffers(card);
678: L64014RemoveIntr(card);
1.4 ! rjkm 679: card_init(card, 0);
1.1 cvs 680: }
681:
682:
683: static u_char read_lsi_status(struct cvdv_cards *card)
684: {
685: margi_info_t *margi = (margi_info_t *) card->margi;
686: return (inb(margi->link.io.BasePort1 + DIO_LSI_STATUS) & 15);
687:
688: }
689:
690: /*====================================================================*/
691:
692: static void cs_error(client_handle_t handle, int func, int ret)
693: {
694: error_info_t err = { func, ret };
695: CardServices(ReportError, handle, &err);
696: }
697:
698: /*======================================================================
699:
700: margi_attach() creates an "instance" of the driver, allocating
701: local data structures for one device. The device is registered
702: with Card Services.
703:
704: The dev_link structure is initialized, but we don't actually
705: configure the card at this point -- we wait until we receive a
706: card insertion event.
707:
708: ======================================================================*/
709:
710: static dev_link_t *margi_attach(void)
711: {
712: margi_info_t *local;
713: dev_link_t *link;
714: client_reg_t client_reg;
715: int ret, i;
716:
717: DEBUG(0, "margi_attach()\n");
718:
719: for (i = 0; i < MAX_DEV; i++)
720: if (dev_table[i] == NULL)
721: break;
722: if (i == MAX_DEV) {
723: printk(KERN_NOTICE "margi_cs: no devices available\n");
724: return NULL;
725: }
726:
727: /* Allocate space for private device-specific data */
728: local = kmalloc(sizeof(margi_info_t), GFP_KERNEL);
729: if (!local)
730: return NULL;
731: memset(local, 0, sizeof(margi_info_t));
732: link = &local->link;
733: link->priv = local;
734: local->card.margi = (void *) local;
735: dev_table[i] = link;
736:
737: /* Initialize the dev_link_t structure */
738: link->release.function = &margi_release;
739: link->release.data = (u_long) link;
740:
741: /* Interrupt setup */
742: link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
743: link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
744: if (irq_list[0] == -1)
745: link->irq.IRQInfo2 = irq_mask;
746: else
747: for (i = 0; i < 4; i++)
748: link->irq.IRQInfo2 |= 1 << irq_list[i];
749: link->irq.Handler = NULL;
750:
751: /*
752: General socket configuration defaults can go here. In this
753: client, we assume very little, and rely on the CIS for almost
754: everything. In most clients, many details (i.e., number, sizes,
755: and attributes of IO windows) are fixed by the nature of the
756: device, and can be hard-wired here.
757: */
758: link->conf.Attributes = 0;
759: link->conf.Vcc = 50;
760: link->conf.IntType = INT_MEMORY_AND_IO;
761:
762: /* Register with Card Services */
763: link->next = dev_list;
764: dev_list = link;
765: client_reg.dev_info = &dev_info;
766: client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
767: client_reg.EventMask =
768: CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
769: CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
770: CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
771: client_reg.event_handler = &margi_event;
772: client_reg.Version = 0x0210;
773: client_reg.event_callback_args.client_data = link;
774: ret = CardServices(RegisterClient, &link->handle, &client_reg);
775: if (ret != CS_SUCCESS) {
776: cs_error(link->handle, RegisterClient, ret);
777: margi_detach(link);
778: return NULL;
779: }
780:
781: return link;
782: } /* margi_attach */
783:
784: /*======================================================================
785:
786: This deletes a driver "instance". The device is de-registered
787: with Card Services. If it has been released, all local data
788: structures are freed. Otherwise, the structures will be freed
789: when the device is released.
790:
791: ======================================================================*/
792:
793: static void margi_detach(dev_link_t * link)
794: {
795: dev_link_t **linkp;
796:
797: int nd;
798:
799: DEBUG(0, "margi_detach(0x%p)\n", link);
800:
801: for (nd = 0; nd < MAX_DEV; nd++)
802: if (dev_table[nd] == link)
803: break;
804: if (nd == MAX_DEV)
805: return;
806:
807: /* Locate device structure */
808: for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
809: if (*linkp == link)
810: break;
811: if (*linkp == NULL)
812: return;
813:
814: /*
815: If the device is currently configured and active, we won't
816: actually delete it yet. Instead, it is marked so that when
817: the release() function is called, that will trigger a proper
818: detach().
819: */
820: if (link->state & DEV_CONFIG) {
821: #ifdef PCMCIA_DEBUG
822: printk(KERN_DEBUG "margi_cs: detach postponed, '%s' "
823: "still locked\n", link->dev->dev_name);
824: #endif
825: link->state |= DEV_STALE_LINK;
826: return;
827: }
828:
829: /* Break the link with Card Services */
830: if (link->handle)
831: CardServices(DeregisterClient, link->handle);
832:
833: /* Unlink device structure, and free it */
834: *linkp = link->next;
835: /* This points to the parent struct cvdv_cards struct */
836: dev_table[nd] = NULL;
837:
838: kfree(link->priv);
839:
840: } /* margi_detach */
841:
842: /*======================================================================
843:
844: margi_config() is scheduled to run after a CARD_INSERTION event
845: is received, to configure the PCMCIA socket, and to make the
846: device available to the system.
847:
848: ======================================================================*/
849:
850: #define CS_CHECK(fn, args...) \
851: while ((last_ret=CardServices(last_fn=(fn),args))!=0) goto cs_failed
852:
853: #define CFG_CHECK(fn, args...) \
854: if (CardServices(fn, args) != 0) goto next_entry
855:
856: static void margi_config(dev_link_t * link)
857: {
858: client_handle_t handle = link->handle;
859: margi_info_t *dev = link->priv;
860: struct cvdv_cards *card = &(dev->card);
861: tuple_t tuple;
862: cisparse_t parse;
863: int last_fn, last_ret, i;
864: u_char buf[64];
865: config_info_t conf;
866: win_req_t req;
867: memreq_t map;
868: int minor = 0;
869:
870: DEBUG(0, "margi_config(0x%p)\n", link);
871:
872: /*
873: This reads the card's CONFIG tuple to find its configuration
874: registers.
875: */
876: tuple.DesiredTuple = CISTPL_CONFIG;
877: tuple.Attributes = 0;
878: tuple.TupleData = buf;
879: tuple.TupleDataMax = sizeof(buf);
880: tuple.TupleOffset = 0;
881: CS_CHECK(GetFirstTuple, handle, &tuple);
882: CS_CHECK(GetTupleData, handle, &tuple);
883: CS_CHECK(ParseTuple, handle, &tuple, &parse);
884: link->conf.ConfigBase = parse.config.base;
885: link->conf.Present = parse.config.rmask[0];
886:
887: /* Configure card */
888: link->state |= DEV_CONFIG;
889:
890: /* Look up the current Vcc */
891: CS_CHECK(GetConfigurationInfo, handle, &conf);
892: link->conf.Vcc = conf.Vcc;
893:
894: /*
895: In this loop, we scan the CIS for configuration table entries,
896: each of which describes a valid card configuration, including
897: voltage, IO window, memory window, and interrupt settings.
898:
899: We make no assumptions about the card to be configured: we use
900: just the information available in the CIS. In an ideal world,
901: this would work for any PCMCIA card, but it requires a complete
902: and accurate CIS. In practice, a driver usually "knows" most of
903: these things without consulting the CIS, and most client drivers
904: will only use the CIS to fill in implementation-defined details.
905: */
906: tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
907: CS_CHECK(GetFirstTuple, handle, &tuple);
908: while (1) {
909: cistpl_cftable_entry_t dflt = { 0 };
910: cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
911: CFG_CHECK(GetTupleData, handle, &tuple);
912: CFG_CHECK(ParseTuple, handle, &tuple, &parse);
913:
914: if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
915: dflt = *cfg;
916: if (cfg->index == 0)
917: goto next_entry;
918: link->conf.ConfigIndex = cfg->index;
919:
920: /* Does this card need audio output? */
921: if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
922: link->conf.Attributes |= CONF_ENABLE_SPKR;
923: link->conf.Status = CCSR_AUDIO_ENA;
924: }
925:
926: /* Use power settings for Vcc and Vpp if present */
927: /* Note that the CIS values need to be rescaled */
928: if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
929: if (conf.Vcc !=
930: cfg->vcc.param[CISTPL_POWER_VNOM] /
931: 10000) goto next_entry;
932: } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
933: if (conf.Vcc !=
934: dflt.vcc.param[CISTPL_POWER_VNOM] /
935: 10000) goto next_entry;
936: }
937:
938: if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
939: link->conf.Vpp1 = link->conf.Vpp2 =
940: cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
941: else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
942: link->conf.Vpp1 = link->conf.Vpp2 =
943: dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
944:
945: /* Do we need to allocate an interrupt? */
946: // if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
947: //link->conf.Attributes |= CONF_ENABLE_IRQ;
948: /*
949: Allocate an interrupt line. Note that this does not assign a
950: handler to the interrupt, unless the 'Handler' member of the
951: irq structure is initialized.
952: */
953:
954: link->irq.Attributes =
955: IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
956: link->irq.Handler = &L64014Intr;
957: link->irq.Instance = link;
958: link->conf.Attributes |= CONF_ENABLE_IRQ;
959:
960: #ifdef USE_BH
961: init_bh(MARGI_BH, do_margi_bh);
962: #endif
963: if (link->conf.Attributes & CONF_ENABLE_IRQ)
964: CS_CHECK(RequestIRQ, link->handle, &link->irq);
965:
966: /* IO window settings */
967: link->io.NumPorts1 = link->io.NumPorts2 = 0;
968: if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
969: cistpl_io_t *io =
970: (cfg->io.nwin) ? &cfg->io : &dflt.io;
971: link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
972: if (!(io->flags & CISTPL_IO_8BIT))
973: link->io.Attributes1 =
974: IO_DATA_PATH_WIDTH_16;
975: if (!(io->flags & CISTPL_IO_16BIT))
976: link->io.Attributes1 =
977: IO_DATA_PATH_WIDTH_8;
978: link->io.IOAddrLines =
979: io->flags & CISTPL_IO_LINES_MASK;
980: link->io.BasePort1 = io->win[0].base;
981: link->io.NumPorts1 = io->win[0].len;
982: if (io->nwin > 1) {
983: link->io.Attributes2 =
984: link->io.Attributes1;
985: link->io.BasePort2 = io->win[1].base;
986: link->io.NumPorts2 = io->win[1].len;
987: }
988: }
989:
990: /* This reserves IO space but doesn't actually enable it */
991: CFG_CHECK(RequestIO, link->handle, &link->io);
992:
993: /*
994: Now set up a common memory window, if needed. There is room
995: in the dev_link_t structure for one memory window handle,
996: but if the base addresses need to be saved, or if multiple
997: windows are needed, the info should go in the private data
998: structure for this device.
999:
1000: Note that the memory window base is a physical address, and
1001: needs to be mapped to virtual space with ioremap() before it
1002: is used.
1003: */
1004: if ((cfg->mem.nwin > 0) || (dflt.mem.nwin > 0)) {
1005: cistpl_mem_t *mem =
1006: (cfg->mem.nwin) ? &cfg->mem : &dflt.mem;
1007: req.Attributes =
1008: WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM;
1009: req.Attributes |= WIN_ENABLE;
1010: req.Base = mem->win[0].host_addr;
1011: req.Size = mem->win[0].len;
1012: req.AccessSpeed = 0;
1013: link->win = (window_handle_t) link->handle;
1014: CFG_CHECK(RequestWindow, &link->win, &req);
1015: map.Page = 0;
1016: map.CardOffset = mem->win[0].card_addr;
1017: CFG_CHECK(MapMemPage, link->win, &map);
1018: }
1019: /* If we got this far, we're cool! */
1020: break;
1021:
1022: next_entry:
1023: CS_CHECK(GetNextTuple, handle, &tuple);
1024: }
1025:
1026: /*
1027: This actually configures the PCMCIA socket -- setting up
1028: the I/O windows and the interrupt mapping, and putting the
1029: card and host interface into "Memory and IO" mode.
1030: */
1031: CS_CHECK(RequestConfiguration, link->handle, &link->conf);
1032:
1033: /*
1034: We can release the IO port allocations here, if some other
1035: driver for the card is going to loaded, and will expect the
1036: ports to be available.
1037: */
1038: if (free_ports) {
1039: if (link->io.BasePort1)
1040: release_region(link->io.BasePort1,
1041: link->io.NumPorts1);
1042: if (link->io.BasePort2)
1043: release_region(link->io.BasePort2,
1044: link->io.NumPorts2);
1045: }
1046:
1047: /*
1048: At this point, the dev_node_t structure(s) need to be
1049: initialized and arranged in a linked list at link->dev.
1050: */
1051:
1052: first_card = card;
1053: minor=0;
1054: card->next = NULL;
1055: card_init(card, minor);
1056: if ((i = register_chrdev(CVDV_MAJOR, CVDV_PROCNAME, &cvdv_fileops))
1057: >= 0) {
1058: major_device_number = ((i) ? i : CVDV_MAJOR);
1059: printk(KERN_INFO LOGNAME
1060: ": Char-device with major number %d installed\n",
1061: major_device_number);
1062: } else {
1063: printk(KERN_ERR LOGNAME
1064: ": ERROR: Failed to install Char-device %d, error %d\n",
1065: CVDV_MAJOR, i);
1066: }
1067: sprintf(dev->node.dev_name, "margi");
1068: dev->node.major = major_device_number;
1069: dev->node.minor = minor;
1070: link->dev = &dev->node;
1071:
1072: /* Finally, report what we've done */
1073: printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
1074: dev->node.dev_name, link->conf.ConfigIndex,
1075: link->conf.Vcc / 10, link->conf.Vcc % 10);
1076: if (link->conf.Vpp1)
1077: printk(", Vpp %d.%d", link->conf.Vpp1 / 10,
1078: link->conf.Vpp1 % 10);
1079: if (link->conf.Attributes & CONF_ENABLE_IRQ)
1080: printk(", irq %d", link->irq.AssignedIRQ);
1081: if (link->io.NumPorts1)
1082: printk(", io 0x%04x-0x%04x", link->io.BasePort1,
1083: link->io.BasePort1 + link->io.NumPorts1 - 1);
1084: if (link->io.NumPorts2)
1085: printk(" & 0x%04x-0x%04x", link->io.BasePort2,
1086: link->io.BasePort2 + link->io.NumPorts2 - 1);
1087: if (link->win)
1088: printk(", mem 0x%06lx-0x%06lx", req.Base,
1089: req.Base + req.Size - 1);
1090: printk("\n");
1091:
1092: link->state &= ~DEV_CONFIG_PENDING;
1093: if (0xdd == read_indexed_register(card, IIO_ID)) {
1094: printk("L64014 Version %d in mode %d detected\n",
1095: (read_indexed_register(card, IIO_MODE) & 248) >> 3,
1096: read_indexed_register(card, IIO_MODE) & 7);
1097: write_indexed_register(card, IIO_GPIO_CONTROL, 0x07);
1098:
1099: L64014Init(card);
1100:
1101: // default: color bars
1102: VideoSetBackground(card, 1, 0, 0, 0); // black
1103: SetVideoSystem(card);
1104: minorlist[minor] = card; // fast access for the char driver
1105:
1106:
1107: /*enable L64014 IRQ */
1108: write_indexed_register(card, IIO_IRQ_CONTROL,
1109: IRQ_POL | IRQ_EN | VSYNC_EN);
1110: // write_indexed_register(card, IIO_IRQ_CONTROL, 0x24);
1111: }
1112: return;
1113:
1114: cs_failed:
1115: cs_error(link->handle, last_fn, last_ret);
1116: margi_release((u_long) link);
1117:
1118: } /* margi_config */
1119:
1120: /*======================================================================
1121:
1122: After a card is removed, margi_release() will unregister the
1123: device, and release the PCMCIA configuration. If the device is
1124: still open, this will be postponed until it is closed.
1125:
1126: ======================================================================*/
1127:
1128: static void margi_release(u_long arg)
1129: {
1130: dev_link_t *link = (dev_link_t *) arg;
1131: margi_info_t *dev = link->priv;
1132: struct cvdv_cards *card = &(dev->card);
1133:
1134: DEBUG(0, "margi_release(0x%p)\n", link);
1135: /*
1136: If the device is currently in use, we won't release until it
1137: is actually closed, because until then, we can't be sure that
1138: no one will try to access the device or its data structures.
1139: */
1140: if (link->open) {
1141: DEBUG(1, "margi_cs: release postponed, '%s' still open\n",
1142: link->dev->dev_name);
1143: link->state |= DEV_STALE_CONFIG;
1144: return;
1145: }
1146:
1147: /* Unlink the device chain */
1148: link->dev = NULL;
1149:
1150: /*
1151: In a normal driver, additional code may be needed to release
1152: other kernel data structures associated with this device.
1153: */
1154:
1155: printk(KERN_INFO LOGNAME ": Unloading device driver\n");
1156: if (major_device_number)
1157: unregister_chrdev(major_device_number, CVDV_PROCNAME);
1158: CardDeInit(card);
1159:
1160: #ifdef USE_BH
1161: remove_bh(MARGI_BH);
1162: #endif
1163: mdelay(100);
1164: /* Don't bother checking to see if these succeed or not */
1165: if (link->win)
1166: CardServices(ReleaseWindow, link->win);
1167: CardServices(ReleaseConfiguration, link->handle);
1168: if (link->io.NumPorts1)
1169: CardServices(ReleaseIO, link->handle, &link->io);
1170: if (link->irq.AssignedIRQ)
1171: CardServices(ReleaseIRQ, link->handle, &link->irq);
1172: link->state &= ~DEV_CONFIG;
1173:
1174: if (link->state & DEV_STALE_LINK)
1175: margi_detach(link);
1176:
1177: } /* margi_release */
1178:
1179: /*======================================================================
1180:
1181: The card status event handler. Mostly, this schedules other
1182: stuff to run after an event is received.
1183:
1184: When a CARD_REMOVAL event is received, we immediately set a
1185: private flag to block future accesses to this device. All the
1186: functions that actually access the device should check this flag
1187: to make sure the card is still present.
1188:
1189: ======================================================================*/
1190:
1191: static int margi_event(event_t event, int priority,
1192: event_callback_args_t * args)
1193: {
1194: dev_link_t *link = args->client_data;
1195: margi_info_t *dev = link->priv;
1196:
1197: DEBUG(1, "margi_event(0x%06x)\n", event);
1198:
1199: switch (event) {
1200: case CS_EVENT_CARD_REMOVAL:
1201: link->state &= ~DEV_PRESENT;
1202: if (link->state & DEV_CONFIG) {
1203: ((margi_info_t *) link->priv)->stop = 1;
1204: link->release.expires = jiffies + HZ / 20;
1205: add_timer(&link->release);
1206: }
1207: break;
1208: case CS_EVENT_CARD_INSERTION:
1209: link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1210: dev->card.bus = args->bus;
1211: margi_config(link);
1212: break;
1213: case CS_EVENT_PM_SUSPEND:
1214: link->state |= DEV_SUSPEND;
1215: /* Fall through... */
1216: case CS_EVENT_RESET_PHYSICAL:
1217: /* Mark the device as stopped, to block IO until later */
1218: dev->stop = 1;
1219: if (link->state & DEV_CONFIG)
1220: CardServices(ReleaseConfiguration, link->handle);
1221: break;
1222: case CS_EVENT_PM_RESUME:
1223: link->state &= ~DEV_SUSPEND;
1224: /* Fall through... */
1225: case CS_EVENT_CARD_RESET:
1226: if (link->state & DEV_CONFIG)
1227: CardServices(RequestConfiguration, link->handle,
1228: &link->conf);
1229: dev->stop = 0;
1230: /*
1231: In a normal driver, additional code may go here to restore
1232: the device state and restart IO.
1233: */
1234: break;
1235: }
1236: return 0;
1237: } /* margi_event */
1238:
1239: /*====================================================================*/
1240:
1241: static int __init init_margi_cs(void)
1242: {
1243: servinfo_t serv;
1244: DEBUG(0, "%s\n", version);
1245: CardServices(GetCardServicesInfo, &serv);
1246: if (serv.Revision != CS_RELEASE_CODE) {
1247: printk(KERN_NOTICE "margi_cs: Card Services release "
1248: "does not match!\n");
1249: return -1;
1250: }
1251: register_pccard_driver(&dev_info, &margi_attach, &margi_detach);
1252: return 0;
1253: }
1254:
1255: static void __exit exit_margi_cs(void)
1256: {
1257: DEBUG(0, "margi_cs: unloading\n");
1258: unregister_pccard_driver(&dev_info);
1259: while (dev_list != NULL) {
1260: if (dev_list->state & DEV_CONFIG)
1261: margi_release((u_long) dev_list);
1262: margi_detach(dev_list);
1263: }
1264: }
1265:
1266: module_init(init_margi_cs);
1267: module_exit(exit_margi_cs);
LinuxTV legacy CVS <linuxtv.org/cvs>