Remove building with NOCRYPTO option
[minix.git] / minix / drivers / bus / ti1225 / ti1225.c
blobfb7b68849176658c57fbf9eefccb1d5d5cf5a088
1 /*
2 ti1225.c
4 Created: Dec 2005 by Philip Homburg
5 */
7 #include <minix/drivers.h>
8 #include <minix/driver.h>
9 #include <machine/pci.h>
10 #include <machine/vm.h>
11 #include <machine/vmparam.h>
12 #include <sys/mman.h>
14 #include "ti1225.h"
15 #include "i82365.h"
17 /* The use of interrupts is not yet ready for prime time */
18 #define USE_INTS 0
20 static struct port
22 int p_devind;
23 u8_t p_cb_busnr;
24 u16_t p_exca_port;
25 #if USE_INTS
26 int p_irq;
27 int p_hook;
28 #endif
29 volatile struct csr *csr_ptr;
30 } port;
32 static int instance;
33 static int debug;
35 static int hw_probe(int skip);
36 static void hw_init(struct port *pp, int devind);
37 static void do_int(struct port *pp);
39 /* SEF functions and variables. */
40 static void sef_local_startup(void);
41 static int sef_cb_init_fresh(int type, sef_init_info_t *info);
43 /*===========================================================================*
44 * main *
45 *===========================================================================*/
46 int main(int argc, char *argv[])
48 int r;
49 message m;
50 int ipc_status;
52 /* SEF local startup. */
53 env_setargs(argc, argv);
54 sef_local_startup();
56 for (;;)
58 r= driver_receive(ANY, &m, &ipc_status);
59 if (r != OK)
60 panic("driver_receive failed: %d", r);
61 printf("ti1225: got message %u from %d\n",
62 m.m_type, m.m_source);
64 return 0;
67 /*===========================================================================*
68 * sef_local_startup *
69 *===========================================================================*/
70 static void sef_local_startup()
72 /* Register init callbacks. */
73 sef_setcb_init_fresh(sef_cb_init_fresh);
74 sef_setcb_init_lu(sef_cb_init_fresh);
75 sef_setcb_init_restart(sef_cb_init_fresh);
77 /* Let SEF perform startup. */
78 sef_startup();
81 /*===========================================================================*
82 * sef_cb_init_fresh *
83 *===========================================================================*/
84 static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
86 /* Initialize the ti1225 driver. */
87 int r, devind;
88 long v;
90 if((r=tsc_calibrate()) != OK)
91 panic("tsc_calibrate failed: %d", r);
93 v = 0;
94 (void) env_parse("instance", "d", 0, &v, 0, 255);
95 instance = (int) v;
97 v = 0;
98 (void) env_parse("debug", "d", 0, &v, 0, 1);
99 debug = (int) v;
101 devind = hw_probe(instance);
103 if (devind < 0)
104 return(ENODEV);
106 hw_init(&port, devind);
108 return(OK);
111 /*===========================================================================*
112 * hw_probe *
113 *===========================================================================*/
114 static int hw_probe(int skip)
116 u16_t vid, did;
117 int devind;
119 pci_init();
121 if (pci_first_dev(&devind, &vid, &did) != 1)
122 return(-1);
124 while (skip--)
125 if (pci_next_dev(&devind, &vid, &did) != 1)
126 return(-1);
128 pci_reserve(devind);
130 if (debug)
131 printf("ti1225: found device %04x/%04x\n", vid, did);
133 return(devind);
136 /*===========================================================================*
137 * hw_init *
138 *===========================================================================*/
139 static void hw_init(struct port *pp, int devind)
141 u8_t v8;
142 u16_t v16;
143 u32_t v32;
144 #if USE_INTS
145 int r, irq;
146 #endif
148 pp->p_devind= devind;
149 if (debug)
150 printf("hw_init: devind = %d\n", devind);
152 if (debug)
154 v16= pci_attr_r16(devind, PCI_CR);
155 printf("ti1225: command register 0x%x\n", v16);
158 v32= pci_attr_r32(devind, TI_CB_BASEADDR);
159 if (debug)
160 printf("ti1225: Cardbus/ExCA base address 0x%x\n", v32);
161 v32 &= PCI_BAR_MEM_MASK; /* Clear low order bits in base */
163 pp->csr_ptr=
164 (struct csr *) vm_map_phys(SELF, (void *) v32, PAGE_SIZE);
165 if (pp->csr_ptr == MAP_FAILED)
166 panic("hw_init: vm_map_phys failed");
168 if (debug)
170 v8= pci_attr_r8(devind, TI_PCI_BUS_NR);
171 printf("ti1225: PCI bus number %d\n", v8);
173 v8= pci_attr_r8(devind, TI_CB_BUS_NR);
174 pp->p_cb_busnr= v8;
175 if (debug)
177 printf("ti1225: CardBus bus number %d\n", v8);
178 v8= pci_attr_r8(devind, TI_SO_BUS_NR);
179 printf("ti1225: Subordinate bus number %d\n", v8);
182 #if USE_INTS
183 irq= pci_attr_r8(devind, PCI_ILR);
184 pp->p_irq= irq;
185 printf("ti1225 using IRQ %d\n", irq);
186 #endif
188 v32= pci_attr_r32(devind, TI_LEGACY_BA);
189 v32 &= ~1;
190 if (debug)
192 printf("ti1225: PC Card 16-bit legacy-mode base address 0x%x\n",
193 v32);
196 if (v32 == 0)
197 panic("bad legacy-mode base address: %d", v32);
198 pp->p_exca_port= v32;
200 if (debug)
202 v32= pci_attr_r32(devind, TI_MF_ROUTE);
203 printf("ti1225: Multifunction routing 0x%08x\n", v32);
206 #if USE_INTS
207 pp->p_hook = pp->p_irq;
208 r= sys_irqsetpolicy(pp->p_irq, 0, &pp->p_hook);
209 if (r != OK)
210 panic("sys_irqsetpolicy failed: %d", r);
211 #endif
213 /* Clear CBB_BC_INTEXCA */
214 v16= pci_attr_r16(devind, CBB_BRIDGECTRL);
215 if (debug)
216 printf("ti1225: Bridge control 0x%04x\n", v16);
217 v16 &= ~CBB_BC_INTEXCA;
218 pci_attr_w16(devind, CBB_BRIDGECTRL, v16);
220 if (debug)
222 v32= pci_attr_r32(devind, TI_SYSCTRL);
223 printf("ti1225: System Control Register 0x%08x\n", v32);
225 v8= pci_attr_r8(devind, TI_CARD_CTRL);
226 printf("ti1225: Card Control 0x%02x\n", v8);
228 v8= pci_attr_r8(devind, TI_DEV_CTRL);
229 printf("ti1225: Device Control 0x%02x\n", v8);
232 /* Enable socket interrupts */
233 pp->csr_ptr->csr_mask |= CM_PWRMASK | CM_CDMASK | CM_CSTSMASK;
235 do_int(pp);
237 #if USE_INTS
238 r= sys_irqenable(&pp->p_hook);
239 if (r != OK)
240 panic("unable enable interrupts: %d", r);
241 #endif
244 /*===========================================================================*
245 * do_int *
246 *===========================================================================*/
247 static void do_int(struct port *pp)
249 int devind, vcc_5v, vcc_3v, vcc_Xv, vcc_Yv,
250 socket_5v, socket_3v, socket_Xv, socket_Yv;
251 spin_t spin;
252 u32_t csr_event, csr_present, csr_control;
253 u8_t v8;
254 u16_t v16;
255 #if USE_INTS
256 int r;
257 #endif
259 devind= pp->p_devind;
260 v8= pci_attr_r8(devind, TI_CARD_CTRL);
261 if (v8 & TI_CCR_IFG)
263 printf("ti1225: got functional interrupt\n");
264 pci_attr_w8(devind, TI_CARD_CTRL, v8);
267 if (debug)
269 printf("Socket event: 0x%x\n", pp->csr_ptr->csr_event);
270 printf("Socket mask: 0x%x\n", pp->csr_ptr->csr_mask);
273 csr_present= pp->csr_ptr->csr_present;
274 csr_control= pp->csr_ptr->csr_control;
276 if ((csr_present & (CP_CDETECT1|CP_CDETECT2)) != 0)
278 if (debug)
279 printf("do_int: no card present\n");
280 return;
282 if (csr_present & CP_BADVCCREQ)
284 printf("do_int: Bad Vcc request\n");
285 /* return; */
287 if (csr_present & CP_DATALOST)
289 /* Do we care? */
290 if (debug)
291 printf("do_int: Data lost\n");
292 /* return; */
294 if (csr_present & CP_NOTACARD)
296 printf("do_int: Not a card\n");
297 return;
299 if (debug)
301 if (csr_present & CP_CBCARD)
302 printf("do_int: Cardbus card detected\n");
303 if (csr_present & CP_16BITCARD)
304 printf("do_int: 16-bit card detected\n");
306 if (csr_present & CP_PWRCYCLE)
308 if (debug)
309 printf("do_int: powered up\n");
310 return;
312 vcc_5v= !!(csr_present & CP_5VCARD);
313 vcc_3v= !!(csr_present & CP_3VCARD);
314 vcc_Xv= !!(csr_present & CP_XVCARD);
315 vcc_Yv= !!(csr_present & CP_YVCARD);
316 if (debug)
318 printf("do_int: card supports:%s%s%s%s\n",
319 vcc_5v ? " 5V" : "", vcc_3v ? " 3V" : "",
320 vcc_Xv ? " X.X V" : "", vcc_Yv ? " Y.Y V" : "");
322 socket_5v= !!(csr_present & CP_5VSOCKET);
323 socket_3v= !!(csr_present & CP_3VSOCKET);
324 socket_Xv= !!(csr_present & CP_XVSOCKET);
325 socket_Yv= !!(csr_present & CP_YVSOCKET);
326 if (debug)
328 printf("do_int: socket supports:%s%s%s%s\n",
329 socket_5v ? " 5V" : "", socket_3v ? " 3V" : "",
330 socket_Xv ? " X.X V" : "", socket_Yv ? " Y.Y V" : "");
332 if (vcc_5v && socket_5v)
334 csr_control= (csr_control & ~CC_VCCCTRL) | CC_VCC_5V;
335 pp->csr_ptr->csr_control= csr_control;
336 if (debug)
337 printf("do_int: applying 5V\n");
339 else if (vcc_3v && socket_3v)
341 csr_control= (csr_control & ~CC_VCCCTRL) | CC_VCC_3V;
342 pp->csr_ptr->csr_control= csr_control;
343 if (debug)
344 printf("do_int: applying 3V\n");
346 else if (vcc_Xv && socket_Xv)
348 csr_control= (csr_control & ~CC_VCCCTRL) | CC_VCC_XV;
349 pp->csr_ptr->csr_control= csr_control;
350 printf("do_int: applying X.X V\n");
352 else if (vcc_Yv && socket_Yv)
354 csr_control= (csr_control & ~CC_VCCCTRL) | CC_VCC_YV;
355 pp->csr_ptr->csr_control= csr_control;
356 printf("do_int: applying Y.Y V\n");
358 else
360 printf("do_int: socket and card are not compatible\n");
361 return;
364 csr_event= pp->csr_ptr->csr_event;
365 if (csr_event)
367 if (debug)
368 printf("clearing socket event\n");
369 pp->csr_ptr->csr_event= csr_event;
370 if (debug)
372 printf("Socket event (cleared): 0x%x\n",
373 pp->csr_ptr->csr_event);
377 devind= pp->p_devind;
378 v8= pci_attr_r8(devind, TI_CARD_CTRL);
379 if (v8 & TI_CCR_IFG)
381 printf("ti1225: got functional interrupt\n");
382 pci_attr_w8(devind, TI_CARD_CTRL, v8);
385 if (debug)
387 v8= pci_attr_r8(devind, TI_CARD_CTRL);
388 printf("TI_CARD_CTRL: 0x%02x\n", v8);
391 spin_init(&spin, 100000);
392 do {
393 csr_present= pp->csr_ptr->csr_present;
394 if (csr_present & CP_PWRCYCLE)
395 break;
396 } while (spin_check(&spin));
398 if (!(csr_present & CP_PWRCYCLE))
400 printf("do_int: not powered up?\n");
401 return;
404 /* Reset device */
405 v16= pci_attr_r16(devind, CBB_BRIDGECTRL);
406 v16 |= CBB_BC_CRST;
407 pci_attr_w16(devind, CBB_BRIDGECTRL, v16);
409 /* Wait one microsecond. Is this correct? What are the specs? */
410 micro_delay(1);
412 /* Clear CBB_BC_CRST */
413 v16= pci_attr_r16(devind, CBB_BRIDGECTRL);
414 v16 &= ~CBB_BC_CRST;
415 pci_attr_w16(devind, CBB_BRIDGECTRL, v16);
417 /* Wait one microsecond after clearing the reset line. Is this
418 * correct? What are the specs?
420 micro_delay(1);
422 pci_rescan_bus(pp->p_cb_busnr);
424 #if USE_INTS
425 r= sys_irqenable(&pp->p_hook);
426 if (r != OK)
427 panic("unable enable interrupts: %d", r);
428 #endif