1 /* $NetBSD: tcic2.c,v 1.32 2009/03/14 15:36:17 dsl Exp $ */
4 * Copyright (c) 1998, 1999 Christoph Badura. All rights reserved.
5 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Marc Horowitz.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: tcic2.c,v 1.32 2009/03/14 15:36:17 dsl Exp $");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/extent.h>
40 #include <sys/malloc.h>
41 #include <sys/kthread.h>
46 #include <dev/pcmcia/pcmciareg.h>
47 #include <dev/pcmcia/pcmciavar.h>
49 #include <dev/ic/tcic2reg.h>
50 #include <dev/ic/tcic2var.h>
56 #define DPRINTF(arg) if (tcic_debug) printf arg;
62 * Individual drivers will allocate their own memory and io regions. Memory
63 * regions must be a multiple of 4k, aligned on a 4k boundary.
66 #define TCIC_MEM_ALIGN TCIC_MEM_PAGESIZE
68 void tcic_attach_socket(struct tcic_handle
*);
69 void tcic_init_socket(struct tcic_handle
*);
71 int tcic_print(void *arg
, const char *pnp
);
72 int tcic_intr_socket(struct tcic_handle
*);
74 void tcic_attach_card(struct tcic_handle
*);
75 void tcic_detach_card(struct tcic_handle
*, int);
76 void tcic_deactivate_card(struct tcic_handle
*);
78 void tcic_chip_do_mem_map(struct tcic_handle
*, int);
79 void tcic_chip_do_io_map(struct tcic_handle
*, int);
81 void tcic_create_event_thread(void *);
82 void tcic_event_thread(void *);
84 void tcic_queue_event(struct tcic_handle
*, int);
86 /* Map between irq numbers and internal representation */
89 { 0, 0, 0, 3, 4, 5, 6, 7, 0, 0, 10, 1, 0, 0, 14, 0 };
90 int tcic_valid_irqs
= 0x4cf8;
92 int tcic_irqmap
[] = /* irqs 9 and 6 switched, some ISA cards */
93 { 0, 0, 0, 3, 4, 5, 0, 7, 0, 6, 10, 1, 0, 0, 14, 0 };
94 int tcic_valid_irqs
= 0x4eb8;
97 int tcic_mem_speed
= 250; /* memory access time in nanoseconds */
98 int tcic_io_speed
= 165; /* io access time in nanoseconds */
101 * Check various reserved and otherwise in their value restricted bits.
104 tcic_check_reserved_bits(bus_space_tag_t iot
, bus_space_handle_t ioh
)
108 DPRINTF(("tcic: chkrsvd 1\n"));
109 /* R_ADDR bit 30:28 have a restricted range. */
110 val
= (bus_space_read_2(iot
, ioh
, TCIC_R_ADDR2
) & TCIC_SS_MASK
)
115 DPRINTF(("tcic: chkrsvd 2\n"));
116 /* R_SCTRL bits 6,2,1 are reserved. */
117 val
= bus_space_read_1(iot
, ioh
, TCIC_R_SCTRL
);
118 if (val
& TCIC_SCTRL_RSVD
)
121 DPRINTF(("tcic: chkrsvd 3\n"));
122 /* R_ICSR bit 2 must be same as bit 3. */
123 val
= bus_space_read_1(iot
, ioh
, TCIC_R_ICSR
);
124 if (((val
>> 1) & 1) != ((val
>> 2) & 1))
127 DPRINTF(("tcic: chkrsvd 4\n"));
128 /* R_IENA bits 7,2 are reserverd. */
129 val
= bus_space_read_1(iot
, ioh
, TCIC_R_IENA
);
130 if (val
& TCIC_IENA_RSVD
)
133 DPRINTF(("tcic: chkrsvd 5\n"));
134 /* Some aux registers have reserved bits. */
135 /* Which are we looking at? */
136 auxreg
= bus_space_read_1(iot
, ioh
, TCIC_R_MODE
)
138 val
= bus_space_read_2(iot
, ioh
, TCIC_R_AUX
);
139 DPRINTF(("tcic: auxreg 0x%02x val 0x%04x\n", auxreg
, val
));
142 if (INVALID_AR_SYSCFG(val
))
146 if (INVALID_AR_ILOCK(val
))
150 if (INVALID_AR_TEST(val
))
155 DPRINTF(("tcic: chkrsvd 6\n"));
156 /* XXX fails if pcmcia bios is enabled. */
157 /* Various bits set or not depending if in RESET mode. */
158 val
= bus_space_read_1(iot
, ioh
, TCIC_R_SCTRL
);
159 if (val
& TCIC_SCTRL_RESET
) {
160 DPRINTF(("tcic: chkrsvd 7\n"));
161 /* Address bits must be 0 */
162 val
= bus_space_read_2(iot
, ioh
, TCIC_R_ADDR
);
165 val
= bus_space_read_2(iot
, ioh
, TCIC_R_ADDR2
);
168 DPRINTF(("tcic: chkrsvd 8\n"));
169 /* EDC bits must be 0 */
170 val
= bus_space_read_2(iot
, ioh
, TCIC_R_EDC
);
173 /* We're OK, so take it out of reset. XXX -chb */
174 bus_space_write_1(iot
, ioh
, TCIC_R_SCTRL
, 0);
176 else { /* not in RESET mode */
179 DPRINTF(("tcic: chkrsvd 9\n"));
180 /* Programming timers must have expired. */
181 val
= bus_space_read_1(iot
, ioh
, TCIC_R_SSTAT
);
182 if ((val
& (TCIC_SSTAT_6US
|TCIC_SSTAT_10US
|TCIC_SSTAT_PROGTIME
))
183 != (TCIC_SSTAT_6US
|TCIC_SSTAT_10US
|TCIC_SSTAT_PROGTIME
))
185 DPRINTF(("tcic: chkrsvd 10\n"));
187 * EDC bits should change on read from data space
188 * as long as either EDC or the data are nonzero.
190 if ((bus_space_read_2(iot
, ioh
, TCIC_R_ADDR2
)
191 & TCIC_ADDR2_INDREG
) != 0) {
192 val1
= bus_space_read_2(iot
, ioh
, TCIC_R_EDC
);
193 val2
= bus_space_read_2(iot
, ioh
, TCIC_R_DATA
);
195 val1
= bus_space_read_2(iot
, ioh
, TCIC_R_EDC
);
200 DPRINTF(("tcic: chkrsvd 11\n"));
201 /* XXX what does this check? -chb */
202 omode
= bus_space_read_1(iot
, ioh
, TCIC_R_MODE
);
203 val1
= omode
^ TCIC_AR_MASK
;
204 bus_space_write_1(iot
, ioh
, TCIC_R_MODE
, val1
);
205 val2
= bus_space_read_1(iot
, ioh
, TCIC_R_MODE
);
206 bus_space_write_1(iot
, ioh
, TCIC_R_MODE
, omode
);
210 /* All tests passed */
215 * Read chip ID from AR_ILOCK in test mode.
218 tcic_chipid(bus_space_tag_t iot
, bus_space_handle_t ioh
)
222 otest
= tcic_read_aux_2(iot
, ioh
, TCIC_AR_TEST
);
223 tcic_write_aux_2(iot
, ioh
, TCIC_AR_TEST
, TCIC_TEST_DIAG
);
224 id
= tcic_read_aux_2(iot
, ioh
, TCIC_AR_ILOCK
);
225 tcic_write_aux_2(iot
, ioh
, TCIC_AR_TEST
, otest
);
226 id
&= TCIC_ILOCKTEST_ID_MASK
;
227 id
>>= TCIC_ILOCKTEST_ID_SHFT
;
229 /* clear up IRQs inside tcic. XXX -chb */
230 while (bus_space_read_1(iot
, ioh
, TCIC_R_ICSR
))
231 bus_space_write_1(iot
, ioh
, TCIC_R_ICSR
, TCIC_ICSR_JAM
);
236 * Indicate whether the driver can handle the chip.
239 tcic_chipid_known(int id
)
241 /* XXX only know how to handle DB86082 -chb */
243 case TCIC_CHIPID_DB86082_1
:
244 case TCIC_CHIPID_DB86082A
:
245 case TCIC_CHIPID_DB86082B_ES
:
246 case TCIC_CHIPID_DB86082B
:
247 case TCIC_CHIPID_DB86084_1
:
248 case TCIC_CHIPID_DB86084A
:
249 case TCIC_CHIPID_DB86184_1
:
250 case TCIC_CHIPID_DB86072_1_ES
:
251 case TCIC_CHIPID_DB86072_1
:
259 tcic_chipid_to_string(int id
)
262 case TCIC_CHIPID_DB86082_1
:
263 return ("Databook DB86082");
264 case TCIC_CHIPID_DB86082A
:
265 return ("Databook DB86082A");
266 case TCIC_CHIPID_DB86082B_ES
:
267 return ("Databook DB86082B-es");
268 case TCIC_CHIPID_DB86082B
:
269 return ("Databook DB86082B");
270 case TCIC_CHIPID_DB86084_1
:
271 return ("Databook DB86084");
272 case TCIC_CHIPID_DB86084A
:
273 return ("Databook DB86084A");
274 case TCIC_CHIPID_DB86184_1
:
275 return ("Databook DB86184");
276 case TCIC_CHIPID_DB86072_1_ES
:
277 return ("Databook DB86072-es");
278 case TCIC_CHIPID_DB86072_1
:
279 return ("Databook DB86072");
282 return ("Unknown controller");
285 * Return bitmask of IRQs that the chip can handle.
286 * XXX should be table driven.
289 tcic_validirqs(int chipid
)
292 case TCIC_CHIPID_DB86082_1
:
293 case TCIC_CHIPID_DB86082A
:
294 case TCIC_CHIPID_DB86082B_ES
:
295 case TCIC_CHIPID_DB86082B
:
296 case TCIC_CHIPID_DB86084_1
:
297 case TCIC_CHIPID_DB86084A
:
298 case TCIC_CHIPID_DB86184_1
:
299 case TCIC_CHIPID_DB86072_1_ES
:
300 case TCIC_CHIPID_DB86072_1
:
301 return tcic_valid_irqs
;
307 tcic_attach(struct tcic_softc
*sc
)
311 /* set more chipset dependent parameters in the softc. */
312 switch (sc
->chipid
) {
313 case TCIC_CHIPID_DB86084_1
:
314 case TCIC_CHIPID_DB86084A
:
315 case TCIC_CHIPID_DB86184_1
:
316 sc
->pwrena
= TCIC_PWR_ENA
;
323 /* set up global config registers */
324 reg
= TCIC_WAIT_SYNC
| TCIC_WAIT_CCLK
| TCIC_WAIT_RISING
;
325 reg
|= (tcic_ns2wscnt(250) & TCIC_WAIT_COUNT_MASK
);
326 tcic_write_aux_1(sc
->iot
, sc
->ioh
, TCIC_AR_WCTL
, TCIC_R_WCTL_WAIT
, reg
);
327 reg
= TCIC_SYSCFG_MPSEL_RI
| TCIC_SYSCFG_MCSFULL
;
328 tcic_write_aux_2(sc
->iot
, sc
->ioh
, TCIC_AR_SYSCFG
, reg
);
329 reg
= tcic_read_aux_2(sc
->iot
, sc
->ioh
, TCIC_AR_ILOCK
);
330 reg
|= TCIC_ILOCK_HOLD_CCLK
;
331 tcic_write_aux_2(sc
->iot
, sc
->ioh
, TCIC_AR_ILOCK
, reg
);
333 /* the TCIC has two sockets */
334 /* XXX should i check for actual presence of sockets? -chb */
335 for (i
= 0; i
< TCIC_NSLOTS
; i
++) {
336 sc
->handle
[i
].sc
= sc
;
337 sc
->handle
[i
].sock
= i
;
338 sc
->handle
[i
].flags
= TCIC_FLAG_SOCKETP
;
339 sc
->handle
[i
].memwins
340 = sc
->chipid
== TCIC_CHIPID_DB86082_1
? 4 : 5;
343 /* establish the interrupt */
344 reg
= tcic_read_1(&sc
->handle
[0], TCIC_R_IENA
);
345 tcic_write_1(&sc
->handle
[0], TCIC_R_IENA
,
346 (reg
& ~TCIC_IENA_CFG_MASK
) | TCIC_IENA_CFG_HIGH
);
347 reg
= tcic_read_aux_2(sc
->iot
, sc
->ioh
, TCIC_AR_SYSCFG
);
348 tcic_write_aux_2(sc
->iot
, sc
->ioh
, TCIC_AR_SYSCFG
,
349 (reg
& ~TCIC_SYSCFG_IRQ_MASK
) | tcic_irqmap
[sc
->irq
]);
351 /* XXX block interrupts? */
353 for (i
= 0; i
< TCIC_NSLOTS
; i
++) {
354 /* XXX make more clear what happens here -chb */
355 tcic_sel_sock(&sc
->handle
[i
]);
356 tcic_write_ind_2(&sc
->handle
[i
], TCIC_IR_SCF1_N(i
), 0);
357 tcic_write_ind_2(&sc
->handle
[i
], TCIC_IR_SCF2_N(i
),
358 (TCIC_SCF2_MCD
|TCIC_SCF2_MWP
|TCIC_SCF2_MRDY
359 #if 1 /* XXX explain byte routing issue */
360 |TCIC_SCF2_MLBAT2
|TCIC_SCF2_MLBAT1
|TCIC_SCF2_IDBR
));
362 |TCIC_SCF2_MLBAT2
|TCIC_SCF2_MLBAT1
));
364 tcic_write_1(&sc
->handle
[i
], TCIC_R_MODE
, 0);
365 reg
= tcic_read_aux_2(sc
->iot
, sc
->ioh
, TCIC_AR_SYSCFG
);
366 reg
&= ~TCIC_SYSCFG_AUTOBUSY
;
367 tcic_write_aux_2(sc
->iot
, sc
->ioh
, TCIC_AR_SYSCFG
, reg
);
368 SIMPLEQ_INIT(&sc
->handle
[i
].events
);
371 if ((sc
->handle
[0].flags
& TCIC_FLAG_SOCKETP
) ||
372 (sc
->handle
[1].flags
& TCIC_FLAG_SOCKETP
)) {
373 printf("%s: %s has ", device_xname(&sc
->dev
),
374 tcic_chipid_to_string(sc
->chipid
));
376 if ((sc
->handle
[0].flags
& TCIC_FLAG_SOCKETP
) &&
377 (sc
->handle
[1].flags
& TCIC_FLAG_SOCKETP
))
378 printf("sockets A and B\n");
379 else if (sc
->handle
[0].flags
& TCIC_FLAG_SOCKETP
)
380 printf("socket A only\n");
382 printf("socket B only\n");
388 tcic_attach_sockets(struct tcic_softc
*sc
)
392 for (i
= 0; i
< TCIC_NSLOTS
; i
++)
393 if (sc
->handle
[i
].flags
& TCIC_FLAG_SOCKETP
)
394 tcic_attach_socket(&sc
->handle
[i
]);
398 tcic_attach_socket(struct tcic_handle
*h
)
400 struct pcmciabus_attach_args paa
;
401 int locs
[PCMCIABUSCF_NLOCS
];
403 /* initialize the rest of the handle */
410 /* now, config one pcmcia device per socket */
412 paa
.paa_busname
= "pcmcia";
413 paa
.pct
= (pcmcia_chipset_tag_t
) h
->sc
->pct
;
414 paa
.pch
= (pcmcia_chipset_handle_t
) h
;
415 paa
.iobase
= h
->sc
->iobase
;
416 paa
.iosize
= h
->sc
->iosize
;
418 locs
[PCMCIABUSCF_CONTROLLER
] = 0;
419 locs
[PCMCIABUSCF_SOCKET
] = h
->sock
;
421 h
->pcmcia
= config_found_sm_loc(&h
->sc
->dev
, "pcmciabus", locs
, &paa
,
422 tcic_print
, config_stdsubmatch
);
424 /* if there's actually a pcmcia device attached, initialize the slot */
431 tcic_create_event_thread(void *arg
)
433 struct tcic_handle
*h
= arg
;
444 panic("tcic_create_event_thread: unknown tcic socket");
447 if (kthread_create(PRI_NONE
, 0, NULL
, tcic_event_thread
, h
,
448 &h
->event_thread
, "%s,%s", device_xname(&h
->sc
->dev
), cs
)) {
449 aprint_error_dev(&h
->sc
->dev
, "unable to create event thread for sock 0x%02x\n", h
->sock
);
450 panic("tcic_create_event_thread");
455 tcic_event_thread(void *arg
)
457 struct tcic_handle
*h
= arg
;
458 struct tcic_event
*pe
;
461 while (h
->shutdown
== 0) {
463 if ((pe
= SIMPLEQ_FIRST(&h
->events
)) == NULL
) {
465 (void) tsleep(&h
->events
, PWAIT
, "tcicev", 0);
468 SIMPLEQ_REMOVE_HEAD(&h
->events
, pe_q
);
471 switch (pe
->pe_type
) {
472 case TCIC_EVENT_INSERTION
:
473 DPRINTF(("%s: insertion event\n", device_xname(&h
->sc
->dev
)));
477 case TCIC_EVENT_REMOVAL
:
478 DPRINTF(("%s: removal event\n", device_xname(&h
->sc
->dev
)));
479 tcic_detach_card(h
, DETACH_FORCE
);
483 panic("tcic_event_thread: unknown event %d",
489 h
->event_thread
= NULL
;
491 /* In case parent is waiting for us to exit. */
499 tcic_init_socket(struct tcic_handle
*h
)
503 /* select this socket's config registers */
506 /* set up the socket to interrupt on card detect */
507 reg
= tcic_read_ind_2(h
, TCIC_IR_SCF2_N(h
->sock
));
508 tcic_write_ind_2(h
, TCIC_IR_SCF2_N(h
->sock
), reg
& ~TCIC_SCF2_MCD
);
510 /* enable CD irq in R_IENA */
511 reg
= tcic_read_2(h
, TCIC_R_IENA
);
512 tcic_write_2(h
, TCIC_R_IENA
, reg
|= TCIC_IENA_CDCHG
);
514 /* if there's a card there, then attach it. also save sstat */
515 h
->sstat
= reg
= tcic_read_1(h
, TCIC_R_SSTAT
) & TCIC_SSTAT_STAT_MASK
;
516 if (reg
& TCIC_SSTAT_CD
)
521 tcic_print(void *arg
, const char *pnp
)
523 struct pcmciabus_attach_args
*paa
= arg
;
524 struct tcic_handle
*h
= (struct tcic_handle
*) paa
->pch
;
526 /* Only "pcmcia"s can attach to "tcic"s... easy. */
528 aprint_normal("pcmcia at %s", pnp
);
530 aprint_normal(" socket %d", h
->sock
);
538 struct tcic_softc
*sc
= arg
;
541 DPRINTF(("%s: intr\n", device_xname(&sc
->dev
)));
543 for (i
= 0; i
< TCIC_NSLOTS
; i
++)
544 if (sc
->handle
[i
].flags
& TCIC_FLAG_SOCKETP
)
545 ret
+= tcic_intr_socket(&sc
->handle
[i
]);
547 return (ret
? 1 : 0);
551 tcic_intr_socket(struct tcic_handle
*h
)
557 icsr
= tcic_read_1(h
, TCIC_R_ICSR
);
559 DPRINTF(("%s: %d icsr: 0x%02x \n", device_xname(&h
->sc
->dev
), h
->sock
, icsr
));
561 /* XXX or should the next three be handled in tcic_intr? -chb */
562 if (icsr
& TCIC_ICSR_PROGTIME
) {
563 DPRINTF(("%s: %02x PROGTIME\n", device_xname(&h
->sc
->dev
), h
->sock
));
566 if (icsr
& TCIC_ICSR_ILOCK
) {
567 DPRINTF(("%s: %02x ILOCK\n", device_xname(&h
->sc
->dev
), h
->sock
));
570 if (icsr
& TCIC_ICSR_ERR
) {
571 DPRINTF(("%s: %02x ERR\n", device_xname(&h
->sc
->dev
), h
->sock
));
574 if (icsr
& TCIC_ICSR_CDCHG
) {
577 /* compute what changed since last interrupt */
578 sstat
= tcic_read_aux_1(h
->sc
->iot
, h
->sc
->ioh
,
579 TCIC_AR_WCTL
, TCIC_R_WCTL_XCSR
) & TCIC_XCSR_STAT_MASK
;
580 delta
= h
->sstat
^ sstat
;
586 DPRINTF(("%s: %02x CDCHG %x\n", device_xname(&h
->sc
->dev
), h
->sock
,
590 * XXX This should probably schedule something to happen
591 * after the interrupt handler completes
594 if (delta
& TCIC_SSTAT_CD
) {
595 if (sstat
& TCIC_SSTAT_CD
) {
596 if (!(h
->flags
& TCIC_FLAG_CARDP
)) {
597 DPRINTF(("%s: enqueing INSERTION event\n",
598 device_xname(&h
->sc
->dev
)));
599 tcic_queue_event(h
, TCIC_EVENT_INSERTION
);
602 if (h
->flags
& TCIC_FLAG_CARDP
) {
603 /* Deactivate the card now. */
604 DPRINTF(("%s: deactivating card\n",
605 device_xname(&h
->sc
->dev
)));
606 tcic_deactivate_card(h
);
608 DPRINTF(("%s: enqueing REMOVAL event\n",
609 device_xname(&h
->sc
->dev
)));
610 tcic_queue_event(h
, TCIC_EVENT_REMOVAL
);
614 if (delta
& TCIC_SSTAT_RDY
) {
615 DPRINTF(("%s: %02x READY\n", device_xname(&h
->sc
->dev
), h
->sock
));
616 /* shouldn't happen */
618 if (delta
& TCIC_SSTAT_LBAT1
) {
619 DPRINTF(("%s: %02x LBAT1\n", device_xname(&h
->sc
->dev
), h
->sock
));
621 if (delta
& TCIC_SSTAT_LBAT2
) {
622 DPRINTF(("%s: %02x LBAT2\n", device_xname(&h
->sc
->dev
), h
->sock
));
624 if (delta
& TCIC_SSTAT_WP
) {
625 DPRINTF(("%s: %02x WP\n", device_xname(&h
->sc
->dev
), h
->sock
));
632 tcic_queue_event(struct tcic_handle
*h
, int event
)
634 struct tcic_event
*pe
;
637 pe
= malloc(sizeof(*pe
), M_TEMP
, M_NOWAIT
);
639 panic("tcic_queue_event: can't allocate event");
643 SIMPLEQ_INSERT_TAIL(&h
->events
, pe
, pe_q
);
648 tcic_attach_card(struct tcic_handle
*h
)
650 DPRINTF(("tcic_attach_card\n"));
652 if (h
->flags
& TCIC_FLAG_CARDP
)
653 panic("tcic_attach_card: already attached");
655 /* call the MI attach function */
657 pcmcia_card_attach(h
->pcmcia
);
659 h
->flags
|= TCIC_FLAG_CARDP
;
663 tcic_detach_card(struct tcic_handle
*h
, int flags
)
664 /* flags: DETACH_* */
666 DPRINTF(("tcic_detach_card\n"));
668 if (!(h
->flags
& TCIC_FLAG_CARDP
))
669 panic("tcic_detach_card: already detached");
671 h
->flags
&= ~TCIC_FLAG_CARDP
;
673 /* call the MI detach function */
675 pcmcia_card_detach(h
->pcmcia
, flags
);
680 tcic_deactivate_card(struct tcic_handle
*h
)
684 if (!(h
->flags
& TCIC_FLAG_CARDP
))
685 panic("tcic_deactivate_card: already detached");
687 /* call the MI deactivate function */
688 pcmcia_card_deactivate(h
->pcmcia
);
692 /* XXX disable card detect resume and configuration reset??? */
694 /* power down the socket */
695 tcic_write_1(h
, TCIC_R_PWR
, 0);
697 /* reset the card XXX ? -chb */
699 /* turn off irq's for this socket */
700 reg
= TCIC_IR_SCF1_N(h
->sock
);
701 val
= tcic_read_ind_2(h
, reg
);
702 tcic_write_ind_2(h
, reg
, (val
& ~TCIC_SCF1_IRQ_MASK
)|TCIC_SCF1_IRQOFF
);
703 reg
= TCIC_IR_SCF2_N(h
->sock
);
704 val
= tcic_read_ind_2(h
, reg
);
705 tcic_write_ind_2(h
, reg
,
706 (val
| (TCIC_SCF2_MLBAT1
|TCIC_SCF2_MLBAT2
|TCIC_SCF2_MRDY
707 |TCIC_SCF2_MWP
|TCIC_SCF2_MCD
)));
710 /* XXX the following routine may need to be rewritten. -chb */
712 tcic_chip_mem_alloc(pcmcia_chipset_handle_t pch
, bus_size_t size
, struct pcmcia_mem_handle
*pcmhp
)
714 struct tcic_handle
*h
= (struct tcic_handle
*) pch
;
715 bus_space_handle_t memh
;
718 int i
, mask
, mhandle
, got
= 0;
720 /* out of sc->memh, allocate as many pages as necessary */
723 * The TCIC can map memory only in sizes that are
724 * powers of two, aligned at the natural boundary for the size.
726 i
= tcic_log2((u_int
)size
);
729 sizepg
= max(i
, TCIC_MEM_SHIFT
) - (TCIC_MEM_SHIFT
-1);
731 DPRINTF(("tcic_chip_mem_alloc: size %ld sizepg %ld\n", (u_long
)size
,
734 /* can't allocate that much anyway */
735 if (sizepg
> TCIC_MEM_PAGES
) /* XXX -chb */
738 mask
= (1 << sizepg
) - 1;
740 addr
= 0; /* XXX gcc -Wuninitialized */
741 mhandle
= 0; /* XXX gcc -Wuninitialized */
743 /* XXX i should be initialised to always lay on boundary. -chb */
744 for (i
= 0; i
< (TCIC_MEM_PAGES
+ 1 - sizepg
); i
+= sizepg
) {
745 if ((h
->sc
->subregionmask
& (mask
<< i
)) == (mask
<< i
)) {
746 if (bus_space_subregion(h
->sc
->memt
, h
->sc
->memh
,
747 i
* TCIC_MEM_PAGESIZE
,
748 sizepg
* TCIC_MEM_PAGESIZE
, &memh
))
751 addr
= h
->sc
->membase
+ (i
* TCIC_MEM_PAGESIZE
);
752 h
->sc
->subregionmask
&= ~(mhandle
);
761 DPRINTF(("tcic_chip_mem_alloc bus addr 0x%lx+0x%lx\n", (u_long
) addr
,
764 pcmhp
->memt
= h
->sc
->memt
;
768 pcmhp
->mhandle
= mhandle
;
769 pcmhp
->realsize
= sizepg
* TCIC_MEM_PAGESIZE
;
774 /* XXX the following routine may need to be rewritten. -chb */
776 tcic_chip_mem_free(pcmcia_chipset_handle_t pch
, struct pcmcia_mem_handle
*pcmhp
)
778 struct tcic_handle
*h
= (struct tcic_handle
*) pch
;
780 h
->sc
->subregionmask
|= pcmhp
->mhandle
;
784 tcic_chip_do_mem_map(struct tcic_handle
*h
, int win
)
786 int reg
, hwwin
, wscnt
;
788 int kind
= h
->mem
[win
].kind
& ~PCMCIA_WIDTH_MEM_MASK
;
789 int mem8
= (h
->mem
[win
].kind
& PCMCIA_WIDTH_MEM_MASK
) == PCMCIA_WIDTH_MEM8
;
790 DPRINTF(("tcic_chip_do_mem_map window %d: 0x%lx+0x%lx 0x%lx\n",
791 win
, (u_long
)h
->mem
[win
].addr
, (u_long
)h
->mem
[win
].size
,
792 (u_long
)h
->mem
[win
].offset
));
794 * the even windows are used for socket 0,
795 * the odd ones for socket 1.
797 hwwin
= (win
<< 1) + h
->sock
;
799 /* the WR_MEXT register is MBZ */
800 tcic_write_ind_2(h
, TCIC_WR_MEXT_N(hwwin
), 0);
802 /* set the host base address and window size */
803 if (h
->mem
[win
].size2
<= 1) {
804 reg
= ((h
->mem
[win
].addr
>> TCIC_MEM_SHIFT
) &
805 TCIC_MBASE_ADDR_MASK
) | TCIC_MBASE_4K
;
807 reg
= ((h
->mem
[win
].addr
>> TCIC_MEM_SHIFT
) &
808 TCIC_MBASE_ADDR_MASK
) | (h
->mem
[win
].size2
>> 1);
810 tcic_write_ind_2(h
, TCIC_WR_MBASE_N(hwwin
), reg
);
812 /* set the card address and address space */
814 reg
= ((h
->mem
[win
].offset
>> TCIC_MEM_SHIFT
) & TCIC_MMAP_ADDR_MASK
);
815 reg
|= (kind
== PCMCIA_MEM_ATTR
) ? TCIC_MMAP_ATTR
: 0;
816 DPRINTF(("tcic_chip_do_map_mem window %d(%d) mmap 0x%04x\n",
818 tcic_write_ind_2(h
, TCIC_WR_MMAP_N(hwwin
), reg
);
820 /* set the MCTL register */
821 /* must save WSCNT field in case this is a DB86082 rev 0 */
822 /* XXX why can't I do the following two in one statement? */
823 reg
= tcic_read_ind_2(h
, TCIC_WR_MCTL_N(hwwin
)) & TCIC_MCTL_WSCNT_MASK
;
824 reg
|= TCIC_MCTL_ENA
|TCIC_MCTL_QUIET
;
825 reg
|= mem8
? TCIC_MCTL_B8
: 0;
826 reg
|= (h
->sock
<< TCIC_MCTL_SS_SHIFT
) & TCIC_MCTL_SS_MASK
;
827 #ifdef notyet /* XXX must get speed from CIS somehow. -chb */
828 wscnt
= tcic_ns2wscnt(h
->mem
[win
].speed
);
830 wscnt
= tcic_ns2wscnt(tcic_mem_speed
); /* 300 is "save" default for CIS memory */
832 if (h
->sc
->chipid
== TCIC_CHIPID_DB86082_1
) {
834 * this chip has the wait state count in window
835 * register 7 - hwwin.
838 reg2
= tcic_read_ind_2(h
, TCIC_WR_MCTL_N(7-hwwin
));
839 reg2
&= ~TCIC_MCTL_WSCNT_MASK
;
840 reg2
|= wscnt
& TCIC_MCTL_WSCNT_MASK
;
841 tcic_write_ind_2(h
, TCIC_WR_MCTL_N(7-hwwin
), reg2
);
843 reg
|= wscnt
& TCIC_MCTL_WSCNT_MASK
;
845 tcic_write_ind_2(h
, TCIC_WR_MCTL_N(hwwin
), reg
);
851 r1
= tcic_read_ind_2(h
, TCIC_WR_MBASE_N(hwwin
));
852 r2
= tcic_read_ind_2(h
, TCIC_WR_MMAP_N(hwwin
));
853 r3
= tcic_read_ind_2(h
, TCIC_WR_MCTL_N(hwwin
));
855 DPRINTF(("tcic_chip_do_mem_map window %d(%d): %04x %04x %04x\n",
856 win
, hwwin
, r1
, r2
, r3
));
863 tcic_chip_mem_map(pcmcia_chipset_handle_t pch
, int kind
, bus_addr_t card_addr
, bus_size_t size
, struct pcmcia_mem_handle
*pcmhp
, bus_size_t
*offsetp
, int *windowp
)
865 struct tcic_handle
*h
= (struct tcic_handle
*) pch
;
871 for (i
= 0; i
< h
->memwins
; i
++) {
872 if ((h
->memalloc
& (1 << i
)) == 0) {
874 h
->memalloc
|= (1 << i
);
884 /* XXX this is pretty gross */
886 if (h
->sc
->memt
!= pcmhp
->memt
)
887 panic("tcic_chip_mem_map memt is bogus");
889 busaddr
= pcmhp
->addr
;
892 * compute the address offset to the pcmcia address space for the
893 * tcic. this is intentionally signed. The masks and shifts below
894 * will cause TRT to happen in the tcic registers. Deal with making
895 * sure the address is aligned, and return the alignment offset.
898 *offsetp
= card_addr
% TCIC_MEM_ALIGN
;
899 card_addr
-= *offsetp
;
901 DPRINTF(("tcic_chip_mem_map window %d bus %lx+%lx+%lx at card addr "
902 "%lx\n", win
, (u_long
) busaddr
, (u_long
) * offsetp
, (u_long
) size
,
903 (u_long
) card_addr
));
905 /* XXX we can't use size. -chb */
907 * include the offset in the size, and decrement size by one, since
908 * the hw wants start/stop
910 size
+= *offsetp
- 1;
912 card_offset
= (((long) card_addr
) - ((long) busaddr
));
914 DPRINTF(("tcic_chip_mem_map window %d card_offset 0x%lx\n",
915 win
, (u_long
)card_offset
));
917 h
->mem
[win
].addr
= busaddr
;
918 h
->mem
[win
].size
= size
;
919 h
->mem
[win
].size2
= tcic_log2((u_int
)pcmhp
->realsize
) - TCIC_MEM_SHIFT
;
920 h
->mem
[win
].offset
= card_offset
;
921 h
->mem
[win
].kind
= kind
;
923 tcic_chip_do_mem_map(h
, win
);
929 tcic_chip_mem_unmap(pcmcia_chipset_handle_t pch
, int window
)
931 struct tcic_handle
*h
= (struct tcic_handle
*) pch
;
934 if (window
>= h
->memwins
)
935 panic("tcic_chip_mem_unmap: window out of range");
937 hwwin
= (window
<< 1) + h
->sock
;
938 tcic_write_ind_2(h
, TCIC_WR_MCTL_N(hwwin
), 0);
940 h
->memalloc
&= ~(1 << window
);
944 tcic_chip_io_alloc(pcmcia_chipset_handle_t pch
, bus_addr_t start
, bus_size_t size
, bus_size_t align
, struct pcmcia_io_handle
*pcihp
)
946 struct tcic_handle
*h
= (struct tcic_handle
*) pch
;
948 bus_space_handle_t ioh
;
950 int size2
, flags
= 0;
953 * Allocate some arbitrary I/O space.
956 DPRINTF(("tcic_chip_io_alloc req 0x%lx %ld %ld\n",
957 (u_long
) start
, (u_long
) size
, (u_long
) align
));
959 * The TCIC can map I/O space only in sizes that are
960 * powers of two, aligned at the natural boundary for the size.
962 size2
= tcic_log2((u_int
)size
);
963 if ((1 << size2
) < size
)
965 /* can't allocate that much anyway */
966 if (size2
> 16) /* XXX 64K -chb */
969 if ((1 << size2
) != align
)
970 return 1; /* not suitably aligned */
972 align
= 1 << size2
; /* no alignment given, make it natural */
974 if (start
& (align
- 1))
975 return 1; /* not suitably aligned */
981 if (bus_space_map(iot
, start
, size
, 0, &ioh
))
983 DPRINTF(("tcic_chip_io_alloc map port %lx+%lx\n",
984 (u_long
) ioaddr
, (u_long
) size
));
986 flags
|= PCMCIA_IO_ALLOCATED
;
987 if (bus_space_alloc(iot
, h
->sc
->iobase
,
988 h
->sc
->iobase
+ h
->sc
->iosize
, size
, align
, 0, 0,
991 DPRINTF(("tcic_chip_io_alloc alloc port %lx+%lx\n",
992 (u_long
) ioaddr
, (u_long
) size
));
997 pcihp
->addr
= ioaddr
;
999 pcihp
->flags
= flags
;
1005 tcic_chip_io_free(pcmcia_chipset_handle_t pch
,
1006 struct pcmcia_io_handle
*pcihp
)
1008 bus_space_tag_t iot
= pcihp
->iot
;
1009 bus_space_handle_t ioh
= pcihp
->ioh
;
1010 bus_size_t size
= pcihp
->size
;
1012 if (pcihp
->flags
& PCMCIA_IO_ALLOCATED
)
1013 bus_space_free(iot
, ioh
, size
);
1015 bus_space_unmap(iot
, ioh
, size
);
1018 static int tcic_iowidth_map
[] =
1019 { TCIC_ICTL_AUTOSZ
, TCIC_ICTL_B8
, TCIC_ICTL_B16
};
1022 tcic_chip_do_io_map(struct tcic_handle
*h
, int win
)
1024 int reg
, size2
, iotiny
, wbase
, hwwin
, wscnt
;
1026 DPRINTF(("tcic_chip_do_io_map win %d addr %lx size %lx width %d\n",
1027 win
, (long) h
->io
[win
].addr
, (long) h
->io
[win
].size
,
1028 h
->io
[win
].width
* 8));
1031 * the even windows are used for socket 0,
1032 * the odd ones for socket 1.
1034 hwwin
= (win
<< 1) + h
->sock
;
1036 /* set the WR_BASE register */
1037 /* XXX what if size isn't power of 2? -chb */
1038 size2
= tcic_log2((u_int
)h
->io
[win
].size
);
1039 DPRINTF(("tcic_chip_do_io_map win %d size2 %d\n", win
, size2
));
1041 iotiny
= TCIC_ICTL_TINY
;
1042 wbase
= h
->io
[win
].addr
;
1045 /* XXX we should do better -chb */
1046 wbase
= h
->io
[win
].addr
| (1 << (size2
- 1));
1048 tcic_write_ind_2(h
, TCIC_WR_IBASE_N(hwwin
), wbase
);
1050 /* set the WR_ICTL register */
1051 reg
= TCIC_ICTL_ENA
| TCIC_ICTL_QUIET
;
1052 reg
|= (h
->sock
<< TCIC_ICTL_SS_SHIFT
) & TCIC_ICTL_SS_MASK
;
1053 reg
|= iotiny
| tcic_iowidth_map
[h
->io
[win
].width
];
1054 if (h
->sc
->chipid
!= TCIC_CHIPID_DB86082_1
)
1055 reg
|= TCIC_ICTL_PASS16
;
1056 #ifdef notyet /* XXX must get speed from CIS somehow. -chb */
1057 wscnt
= tcic_ns2wscnt(h
->io
[win
].speed
);
1059 wscnt
= tcic_ns2wscnt(tcic_io_speed
); /* linux uses 0 as default */
1061 reg
|= wscnt
& TCIC_ICTL_WSCNT_MASK
;
1062 tcic_write_ind_2(h
, TCIC_WR_ICTL_N(hwwin
), reg
);
1068 r1
= tcic_read_ind_2(h
, TCIC_WR_IBASE_N(hwwin
));
1069 r2
= tcic_read_ind_2(h
, TCIC_WR_ICTL_N(hwwin
));
1071 DPRINTF(("tcic_chip_do_io_map window %d(%d): %04x %04x\n",
1072 win
, hwwin
, r1
, r2
));
1078 tcic_chip_io_map(pcmcia_chipset_handle_t pch
, int width
, bus_addr_t offset
, bus_size_t size
, struct pcmcia_io_handle
*pcihp
, int *windowp
)
1080 struct tcic_handle
*h
= (struct tcic_handle
*) pch
;
1081 bus_addr_t ioaddr
= pcihp
->addr
+ offset
;
1084 static const char *width_names
[] = { "auto", "io8", "io16" };
1087 /* XXX Sanity check offset/size. */
1090 for (i
= 0; i
< TCIC_IO_WINS
; i
++) {
1091 if ((h
->ioalloc
& (1 << i
)) == 0) {
1093 h
->ioalloc
|= (1 << i
);
1103 /* XXX this is pretty gross */
1105 if (h
->sc
->iot
!= pcihp
->iot
)
1106 panic("tcic_chip_io_map iot is bogus");
1108 DPRINTF(("tcic_chip_io_map window %d %s port %lx+%lx\n",
1109 win
, width_names
[width
], (u_long
) ioaddr
, (u_long
) size
));
1111 /* XXX wtf is this doing here? */
1113 printf("%s: port 0x%lx", device_xname(&h
->sc
->dev
), (u_long
) ioaddr
);
1115 printf("-0x%lx", (u_long
) ioaddr
+ (u_long
) size
- 1);
1118 h
->io
[win
].addr
= ioaddr
;
1119 h
->io
[win
].size
= size
;
1120 h
->io
[win
].width
= width
;
1122 tcic_chip_do_io_map(h
, win
);
1128 tcic_chip_io_unmap(pcmcia_chipset_handle_t pch
, int window
)
1130 struct tcic_handle
*h
= (struct tcic_handle
*) pch
;
1133 if (window
>= TCIC_IO_WINS
)
1134 panic("tcic_chip_io_unmap: window out of range");
1136 hwwin
= (window
<< 1) + h
->sock
;
1137 tcic_write_ind_2(h
, TCIC_WR_ICTL_N(hwwin
), 0);
1139 h
->ioalloc
&= ~(1 << window
);
1143 tcic_chip_socket_enable(pcmcia_chipset_handle_t pch
)
1145 struct tcic_handle
*h
= (struct tcic_handle
*) pch
;
1151 * power down the socket to reset it.
1152 * put card reset into high-z, put chip outputs to card into high-z
1155 tcic_write_1(h
, TCIC_R_PWR
, 0);
1156 reg
= tcic_read_aux_2(h
->sc
->iot
, h
->sc
->ioh
, TCIC_AR_ILOCK
);
1157 reg
|= TCIC_ILOCK_CWAIT
;
1158 reg
&= ~(TCIC_ILOCK_CRESET
|TCIC_ILOCK_CRESENA
);
1159 tcic_write_aux_2(h
->sc
->iot
, h
->sc
->ioh
, TCIC_AR_ILOCK
, reg
);
1160 tcic_write_1(h
, TCIC_R_SCTRL
, 0); /* clear TCIC_SCTRL_ENA */
1162 /* zero out the address windows */
1164 tcic_write_ind_2(h
, TCIC_IR_SCF1_N(h
->sock
), 0);
1165 /* writing to WR_MBASE_N disables the window */
1166 for (win
= 0; win
< h
->memwins
; win
++) {
1167 tcic_write_ind_2(h
, TCIC_WR_MBASE_N((win
<< 1) + h
->sock
), 0);
1169 /* writing to WR_IBASE_N disables the window */
1170 for (win
= 0; win
< TCIC_IO_WINS
; win
++) {
1171 tcic_write_ind_2(h
, TCIC_WR_IBASE_N((win
<< 1) + h
->sock
), 0);
1174 /* power up the socket */
1176 /* turn on VCC, turn of VPP */
1177 reg
= TCIC_PWR_VCC_N(h
->sock
) | TCIC_PWR_VPP_N(h
->sock
) | h
->sc
->pwrena
;
1178 if (h
->sc
->pwrena
) /* this is a '84 type chip */
1179 reg
|= TCIC_PWR_VCC5V
;
1180 tcic_write_1(h
, TCIC_R_PWR
, reg
);
1183 /* enable reset and wiggle it to reset the card */
1184 reg
= tcic_read_aux_2(h
->sc
->iot
, h
->sc
->ioh
, TCIC_AR_ILOCK
);
1185 reg
|= TCIC_ILOCK_CRESENA
;
1186 tcic_write_aux_2(h
->sc
->iot
, h
->sc
->ioh
, TCIC_AR_ILOCK
, reg
);
1187 /* XXX need bus_space_barrier here */
1188 reg
|= TCIC_ILOCK_CRESET
;
1189 tcic_write_aux_2(h
->sc
->iot
, h
->sc
->ioh
, TCIC_AR_ILOCK
, reg
);
1190 /* enable card signals */
1191 tcic_write_1(h
, TCIC_R_SCTRL
, TCIC_SCTRL_ENA
);
1192 delay(10); /* wait 10 us */
1194 /* clear the reset flag */
1195 reg
= tcic_read_aux_2(h
->sc
->iot
, h
->sc
->ioh
, TCIC_AR_ILOCK
);
1196 reg
&= ~(TCIC_ILOCK_CRESET
);
1197 tcic_write_aux_2(h
->sc
->iot
, h
->sc
->ioh
, TCIC_AR_ILOCK
, reg
);
1199 /* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
1202 /* wait for the chip to finish initializing */
1207 /* reinstall all the memory and io mappings */
1209 for (win
= 0; win
< h
->memwins
; win
++)
1210 if (h
->memalloc
& (1 << win
))
1211 tcic_chip_do_mem_map(h
, win
);
1213 for (win
= 0; win
< TCIC_IO_WINS
; win
++)
1214 if (h
->ioalloc
& (1 << win
))
1215 tcic_chip_do_io_map(h
, win
);
1219 tcic_chip_socket_settype(pcmcia_chipset_handle_t pch
, int type
)
1221 struct tcic_handle
*h
= (struct tcic_handle
*) pch
;
1226 /* set the card type */
1229 if (type
== PCMCIA_IFTYPE_IO
) {
1230 reg
|= TCIC_SCF1_IOSTS
;
1231 reg
|= tcic_irqmap
[h
->ih_irq
]; /* enable interrupts */
1233 tcic_write_ind_2(h
, TCIC_IR_SCF1_N(h
->sock
), reg
);
1235 DPRINTF(("%s: tcic_chip_socket_enable %d cardtype %s 0x%02x\n",
1236 device_xname(&h
->sc
->dev
), h
->sock
,
1237 ((type
== PCMCIA_IFTYPE_IO
) ? "io" : "mem"), reg
));
1241 tcic_chip_socket_disable(pcmcia_chipset_handle_t pch
)
1243 struct tcic_handle
*h
= (struct tcic_handle
*) pch
;
1246 DPRINTF(("tcic_chip_socket_disable\n"));
1250 /* disable interrupts */
1251 val
= tcic_read_ind_2(h
, TCIC_IR_SCF1_N(h
->sock
));
1252 val
&= TCIC_SCF1_IRQ_MASK
;
1253 tcic_write_ind_2(h
, TCIC_IR_SCF1_N(h
->sock
), val
);
1255 /* disable the output signals */
1256 tcic_write_1(h
, TCIC_R_SCTRL
, 0);
1257 val
= tcic_read_aux_2(h
->sc
->iot
, h
->sc
->ioh
, TCIC_AR_ILOCK
);
1258 val
&= ~TCIC_ILOCK_CRESENA
;
1259 tcic_write_aux_2(h
->sc
->iot
, h
->sc
->ioh
, TCIC_AR_ILOCK
, val
);
1261 /* power down the socket */
1262 tcic_write_1(h
, TCIC_R_PWR
, 0);
1266 * XXX The following is Linux driver but doesn't match the table
1270 tcic_ns2wscnt(int ns
)
1275 return (2*(ns
-14))/70; /* XXX assumes 14.31818 MHz clock. */
1280 tcic_log2(u_int val
)