explanations with common test3 errors.
[minix3.git] / drivers / ti1225 / ti1225.c
blob225169c95e57f3243af5fdfa1ee1ab327df7156b
1 /*
2 ti1225.c
4 Created: Dec 2005 by Philip Homburg
5 */
7 #include "../drivers.h"
8 #include <ibm/pci.h>
9 #include <sys/vm.h>
11 #include "ti1225.h"
12 #include "i82365.h"
14 /* The use of interrupts is not yet ready for prime time */
15 #define USE_INTS 0
17 #define MICROS_TO_TICKS(m) (((m)*HZ/1000000)+1)
19 #define NR_PORTS 2
21 PRIVATE struct port
23 unsigned p_flags;
24 int p_devind;
25 u8_t p_cb_busnr;
26 u16_t p_exca_port;
27 #if USE_INTS
28 int p_irq;
29 int p_hook;
30 #endif
31 char *base_ptr;
32 volatile struct csr *csr_ptr;
34 char buffer[2*PAGE_SIZE];
35 } ports[NR_PORTS];
37 #define PF_PRESENT 1
39 struct pcitab
41 u16_t vid;
42 u16_t did;
43 int checkclass;
46 PRIVATE struct pcitab pcitab_ti[]=
48 { 0x104C, 0xAC1C, 0 }, /* TI PCI1225 */
50 { 0x0000, 0x0000, 0 }
52 PRIVATE char *progname;
53 PRIVATE int debug;
55 FORWARD _PROTOTYPE( void init, (void) );
56 FORWARD _PROTOTYPE( void hw_init, (struct port *pp) );
57 FORWARD _PROTOTYPE( void map_regs, (struct port *pp, u32_t base) );
58 FORWARD _PROTOTYPE( void do_int, (struct port *pp) );
59 FORWARD _PROTOTYPE( u8_t read_exca, (struct port *pp, int socket, int reg) );
60 FORWARD _PROTOTYPE( void do_outb, (port_t port, u8_t value) );
61 FORWARD _PROTOTYPE( u8_t do_inb, (port_t port) );
62 FORWARD _PROTOTYPE( void micro_delay, (unsigned long usecs) );
64 int main(int argc, char *argv[])
66 int c, r;
67 message m;
69 (progname=strrchr(argv[0],'/')) ? progname++ : (progname=argv[0]);
71 debug= 0;
72 while (c= getopt(argc, argv, "d?"), c != -1)
74 switch(c)
76 case '?': panic("ti1225", "Usage: ti1225 [-d]", NO_NUM);
77 case 'd': debug++; break;
78 default: panic("ti1225", "getopt failed", NO_NUM);
82 init();
84 for (;;)
86 r= receive(ANY, &m);
87 if (r != OK)
88 panic("ti1225", "receive failed", r);
89 printf("ti1225: got message %u from %d\n",
90 m.m_type, m.m_source);
92 return 0;
95 PRIVATE void init()
97 int i, r, first, devind, port;
98 u16_t vid, did;
100 pci_init1(progname);
102 first= 1;
103 port= 0;
104 for (;;)
106 if (first)
108 first= 0;
109 r= pci_first_dev(&devind, &vid, &did);
111 else
112 r= pci_next_dev(&devind, &vid, &did);
113 if (r != 1)
114 break;
116 for (i= 0; pcitab_ti[i].vid != 0; i++)
118 if (pcitab_ti[i].vid != vid)
119 continue;
120 if (pcitab_ti[i].did != did)
121 continue;
122 if (pcitab_ti[i].checkclass)
124 panic("ti1225",
125 "fxp_probe: class check not implemented",
126 NO_NUM);
128 break;
130 if (pcitab_ti[i].vid == 0)
131 continue;
133 pci_reserve(devind);
135 if (debug)
136 printf("ti1225: found device %04x/%04x\n", vid, did);
137 ports[port].p_devind= devind;
138 ports[port].p_flags |= PF_PRESENT;
139 port++;
140 if (port >= NR_PORTS)
141 break;
144 for (i= 0; i<NR_PORTS; i++)
146 if (!(ports[i].p_flags & PF_PRESENT))
147 continue;
148 hw_init(&ports[i]);
152 PRIVATE void hw_init(pp)
153 struct port *pp;
155 int i, r, devind, irq, socket;
156 u8_t v8;
157 u16_t v16;
158 u32_t v32;
160 devind= pp->p_devind;
161 if (debug)
162 printf("hw_init: devind = %d\n", devind);
164 if (debug)
166 v16= pci_attr_r16(devind, PCI_CR);
167 printf("ti1225: command register 0x%x\n", v16);
170 v32= pci_attr_r32(devind, TI_CB_BASEADDR);
171 if (debug)
172 printf("ti1225: Cardbus/ExCA base address 0x%x\n", v32);
173 map_regs(pp, v32);
174 pp->csr_ptr= (struct csr *)pp->base_ptr;
176 if (debug)
178 v8= pci_attr_r8(devind, TI_PCI_BUS_NR);
179 printf("ti1225: PCI bus number %d\n", v8);
181 v8= pci_attr_r8(devind, TI_CB_BUS_NR);
182 pp->p_cb_busnr= v8;
183 if (debug)
185 printf("ti1225: CardBus bus number %d\n", v8);
186 v8= pci_attr_r8(devind, TI_SO_BUS_NR);
187 printf("ti1225: Subordinate bus number %d\n", v8);
190 #if USE_INTS
191 irq= pci_attr_r8(devind, PCI_ILR);
192 pp->p_irq= irq;
193 printf("ti1225 using IRQ %d\n", irq);
194 #endif
196 v32= pci_attr_r32(devind, TI_LEGACY_BA);
197 v32 &= ~1;
198 if (debug)
200 printf("ti1225: PC Card 16-bit legacy-mode base address 0x%x\n",
201 v32);
204 if (v32 == 0)
205 panic("ti1225", "bad lagacy-mode base address 0x%x\n", v32);
206 pp->p_exca_port= v32;
208 if (debug)
210 v32= pci_attr_r32(devind, TI_MF_ROUTE);
211 printf("ti1225: Multifunction routing 0x%08x\n", v32);
214 #if USE_INTS
215 pp->p_hook = pp->p_irq;
216 r= sys_irqsetpolicy(pp->p_irq, 0, &pp->p_hook);
217 if (r != OK)
218 panic("ti1225","sys_irqsetpolicy failed", r);
219 #endif
221 /* Clear CBB_BC_INTEXCA */
222 v16= pci_attr_r16(devind, CBB_BRIDGECTRL);
223 if (debug)
224 printf("ti1225: Bridge control 0x%04x\n", v16);
225 v16 &= ~CBB_BC_INTEXCA;
226 pci_attr_w16(devind, CBB_BRIDGECTRL, v16);
228 if (debug)
230 v32= pci_attr_r32(devind, TI_SYSCTRL);
231 printf("ti1225: System Control Register 0x%08x\n", v32);
233 v8= pci_attr_r8(devind, TI_CARD_CTRL);
234 printf("ti1225: Card Control 0x%02x\n", v8);
236 v8= pci_attr_r8(devind, TI_DEV_CTRL);
237 printf("ti1225: Device Control 0x%02x\n", v8);
240 /* Enable socket interrupts */
241 pp->csr_ptr->csr_mask |= CM_PWRMASK | CM_CDMASK | CM_CSTSMASK;
243 do_int(pp);
245 #if USE_INTS
246 r= sys_irqenable(&pp->p_hook);
247 if (r != OK)
248 panic("ti1225","unable enable interrupts", r);
249 #endif
252 PRIVATE void map_regs(pp, base)
253 struct port *pp;
254 u32_t base;
256 int r;
257 vir_bytes buf_base;
259 buf_base= (vir_bytes)pp->buffer;
260 if (buf_base % PAGE_SIZE)
261 buf_base += PAGE_SIZE-(buf_base % PAGE_SIZE);
262 pp->base_ptr= (char *)buf_base;
263 if (debug)
265 printf("ti1225: map_regs: using %p for %p\n",
266 pp->base_ptr, pp->buffer);
269 /* Clear low order bits in base */
270 base &= ~(u32_t)0xF;
272 r= sys_vm_map(SELF, 1 /* map */, (vir_bytes)pp->base_ptr,
273 PAGE_SIZE, (phys_bytes)base);
274 if (r != OK)
275 panic("ti1225", "map_regs: sys_vm_map failed", r);
278 PRIVATE void do_int(pp)
279 struct port *pp;
281 int i, r, devind, vcc_5v, vcc_3v, vcc_Xv, vcc_Yv,
282 socket_5v, socket_3v, socket_Xv, socket_Yv;
283 clock_t t0, t1;
284 u32_t csr_event, csr_present, csr_control;
285 u8_t v8;
286 u16_t v16;
288 devind= pp->p_devind;
289 v8= pci_attr_r8(devind, TI_CARD_CTRL);
290 if (v8 & TI_CCR_IFG)
292 printf("ti1225: got functional interrupt\n", v8);
293 pci_attr_w8(devind, TI_CARD_CTRL, v8);
296 if (debug)
298 printf("Socket event: 0x%x\n", pp->csr_ptr->csr_event);
299 printf("Socket mask: 0x%x\n", pp->csr_ptr->csr_mask);
302 csr_present= pp->csr_ptr->csr_present;
303 csr_control= pp->csr_ptr->csr_control;
305 if ((csr_present & (CP_CDETECT1|CP_CDETECT2)) != 0)
307 if (debug)
308 printf("do_int: no card present\n");
309 return;
311 if (csr_present & CP_BADVCCREQ)
313 printf("do_int: Bad Vcc request\n");
314 /* return; */
316 if (csr_present & CP_DATALOST)
318 /* Do we care? */
319 if (debug)
320 printf("do_int: Data lost\n");
321 /* return; */
323 if (csr_present & CP_NOTACARD)
325 printf("do_int: Not a card\n");
326 return;
328 if (debug)
330 if (csr_present & CP_CBCARD)
331 printf("do_int: Cardbus card detected\n");
332 if (csr_present & CP_16BITCARD)
333 printf("do_int: 16-bit card detected\n");
335 if (csr_present & CP_PWRCYCLE)
337 if (debug)
338 printf("do_int: powered up\n");
339 return;
341 vcc_5v= !!(csr_present & CP_5VCARD);
342 vcc_3v= !!(csr_present & CP_3VCARD);
343 vcc_Xv= !!(csr_present & CP_XVCARD);
344 vcc_Yv= !!(csr_present & CP_YVCARD);
345 if (debug)
347 printf("do_int: card supports:%s%s%s%s\n",
348 vcc_5v ? " 5V" : "", vcc_3v ? " 3V" : "",
349 vcc_Xv ? " X.X V" : "", vcc_Yv ? " Y.Y V" : "");
351 socket_5v= !!(csr_present & CP_5VSOCKET);
352 socket_3v= !!(csr_present & CP_3VSOCKET);
353 socket_Xv= !!(csr_present & CP_XVSOCKET);
354 socket_Yv= !!(csr_present & CP_YVSOCKET);
355 if (debug)
357 printf("do_int: socket supports:%s%s%s%s\n",
358 socket_5v ? " 5V" : "", socket_3v ? " 3V" : "",
359 socket_Xv ? " X.X V" : "", socket_Yv ? " Y.Y V" : "");
361 if (vcc_5v && socket_5v)
363 csr_control= (csr_control & ~CC_VCCCTRL) | CC_VCC_5V;
364 pp->csr_ptr->csr_control= csr_control;
365 if (debug)
366 printf("do_int: applying 5V\n");
368 else if (vcc_3v && socket_3v)
370 csr_control= (csr_control & ~CC_VCCCTRL) | CC_VCC_3V;
371 pp->csr_ptr->csr_control= csr_control;
372 if (debug)
373 printf("do_int: applying 3V\n");
375 else if (vcc_Xv && socket_Xv)
377 csr_control= (csr_control & ~CC_VCCCTRL) | CC_VCC_XV;
378 pp->csr_ptr->csr_control= csr_control;
379 printf("do_int: applying X.X V\n");
381 else if (vcc_Yv && socket_Yv)
383 csr_control= (csr_control & ~CC_VCCCTRL) | CC_VCC_YV;
384 pp->csr_ptr->csr_control= csr_control;
385 printf("do_int: applying Y.Y V\n");
387 else
389 printf("do_int: socket and card are not compatible\n");
390 return;
393 csr_event= pp->csr_ptr->csr_event;
394 if (csr_event)
396 if (debug)
397 printf("clearing socket event\n");
398 pp->csr_ptr->csr_event= csr_event;
399 if (debug)
401 printf("Socket event (cleared): 0x%x\n",
402 pp->csr_ptr->csr_event);
406 devind= pp->p_devind;
407 v8= pci_attr_r8(devind, TI_CARD_CTRL);
408 if (v8 & TI_CCR_IFG)
410 printf("ti1225: got functional interrupt\n", v8);
411 pci_attr_w8(devind, TI_CARD_CTRL, v8);
414 if (debug)
416 v8= pci_attr_r8(devind, TI_CARD_CTRL);
417 printf("TI_CARD_CTRL: 0x%02x\n", v8);
420 getuptime(&t0);
421 do {
422 csr_present= pp->csr_ptr->csr_present;
423 if (csr_present & CP_PWRCYCLE)
424 break;
425 } while (getuptime(&t1)==OK && (t1-t0) < MICROS_TO_TICKS(100000));
427 if (!(csr_present & CP_PWRCYCLE))
429 printf("do_int: not powered up?\n");
430 return;
433 /* Reset device */
434 v16= pci_attr_r16(devind, CBB_BRIDGECTRL);
435 v16 |= CBB_BC_CRST;
436 pci_attr_w16(devind, CBB_BRIDGECTRL, v16);
438 /* Wait one microsecond. Is this correct? What are the specs? */
439 micro_delay(1);
441 /* Clear CBB_BC_CRST */
442 v16= pci_attr_r16(devind, CBB_BRIDGECTRL);
443 v16 &= ~CBB_BC_CRST;
444 pci_attr_w16(devind, CBB_BRIDGECTRL, v16);
446 /* Wait one microsecond after clearing the reset line. Is this
447 * correct? What are the specs?
449 micro_delay(1);
451 pci_rescan_bus(pp->p_cb_busnr);
453 #if USE_INTS
454 r= sys_irqenable(&pp->p_hook);
455 if (r != OK)
456 panic("ti1225","unable enable interrupts", r);
457 #endif
461 PRIVATE u8_t read_exca(pp, socket, reg)
462 struct port *pp;
463 int socket;
464 int reg;
466 u16_t port;
468 port= pp->p_exca_port;
469 if (port == 0)
470 panic("ti1225", "read_exca: bad port", NO_NUM);
471 do_outb(port, socket * 0x40 + reg);
472 return do_inb(port+1);
475 PRIVATE u8_t do_inb(port_t port)
477 int r;
478 u32_t value;
480 r= sys_inb(port, &value);
481 if (r != OK)
482 panic("ti1225","sys_inb failed", r);
483 return value;
486 PRIVATE void do_outb(port_t port, u8_t value)
488 int r;
490 r= sys_outb(port, value);
491 if (r != OK)
492 panic("ti1225","sys_outb failed", r);
495 PRIVATE void micro_delay(unsigned long usecs)
497 tickdelay(MICROS_TO_TICKS(usecs));