1 /*	$OpenBSD: cardbusvar.h,v 1.8 2005/06/27 21:38:27 deraadt Exp $	*/
2 /*	$NetBSD: cardbusvar.h,v 1.17 2000/04/02 19:11:37 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1998, 1999 and 2000
6  *       HAYAKAWA Koichi.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the author.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef _DEV_CARDBUS_CARDBUSVAR_H_
37 #define _DEV_CARDBUS_CARDBUSVAR_H_
38 
39 #include <dev/pci/pcivar.h>	/* for pcitag_t */
40 
41 #if 1
42 #include <dev/cardbus/rbus.h>
43 #endif
44 
45 
46 
47 typedef void *cardbus_chipset_tag_t;
48 typedef int cardbus_intr_handle_t;
49 
50 
51 /* XXX they must be in cardbusreg.h */
52 typedef u_int32_t cardbusreg_t;
53 typedef pcitag_t cardbustag_t;
54 typedef int cardbus_intr_line_t;
55 
56 #define CARDBUS_ID_REG          0x00
57 
58 typedef u_int16_t cardbus_vendor_id_t;
59 typedef u_int16_t cardbus_product_id_t;
60 
61 #  define CARDBUS_VENDOR_SHIFT  0
62 #  define CARDBUS_VENDOR_MASK   0xffff
63 #  define CARDBUS_VENDOR(id) \
64 	    (((id) >> CARDBUS_VENDOR_SHIFT) & CARDBUS_VENDOR_MASK)
65 
66 #  define CARDBUS_PRODUCT_SHIFT  16
67 #  define CARDBUS_PRODUCT_MASK   0xffff
68 #  define CARDBUS_PRODUCT(id) \
69 	    (((id) >> CARDBUS_PRODUCT_SHIFT) & CARDBUS_PRODUCT_MASK)
70 
71 
72 #define	CARDBUS_COMMAND_STATUS_REG  0x04
73 
74 #  define CARDBUS_COMMAND_IO_ENABLE     0x00000001
75 #  define CARDBUS_COMMAND_MEM_ENABLE    0x00000002
76 #  define CARDBUS_COMMAND_MASTER_ENABLE 0x00000004
77 
78 
79 #define CARDBUS_CLASS_REG       0x08
80 
81 #define	CARDBUS_CLASS_SHIFT				24
82 #define	CARDBUS_CLASS_MASK				0xff
83 #define	CARDBUS_CLASS(cr) \
84 	    (((cr) >> CARDBUS_CLASS_SHIFT) & CARDBUS_CLASS_MASK)
85 
86 #define	CARDBUS_SUBCLASS_SHIFT			16
87 #define	CARDBUS_SUBCLASS_MASK			0xff
88 #define	CARDBUS_SUBCLASS(cr) \
89 	    (((cr) >> CARDBUS_SUBCLASS_SHIFT) & CARDBUS_SUBCLASS_MASK)
90 
91 #define	CARDBUS_INTERFACE_SHIFT			8
92 #define	CARDBUS_INTERFACE_MASK			0xff
93 #define	CARDBUS_INTERFACE(cr) \
94 	    (((cr) >> CARDBUS_INTERFACE_SHIFT) & CARDBUS_INTERFACE_MASK)
95 
96 #define	CARDBUS_REVISION_SHIFT			0
97 #define	CARDBUS_REVISION_MASK			0xff
98 #define	CARDBUS_REVISION(cr) \
99 	    (((cr) >> CARDBUS_REVISION_SHIFT) & CARDBUS_REVISION_MASK)
100 
101 /* base classes */
102 #define	CARDBUS_CLASS_PREHISTORIC		0x00
103 #define	CARDBUS_CLASS_MASS_STORAGE		0x01
104 #define	CARDBUS_CLASS_NETWORK			0x02
105 #define	CARDBUS_CLASS_DISPLAY			0x03
106 #define	CARDBUS_CLASS_MULTIMEDIA		0x04
107 #define	CARDBUS_CLASS_MEMORY			0x05
108 #define	CARDBUS_CLASS_BRIDGE			0x06
109 #define	CARDBUS_CLASS_COMMUNICATIONS		0x07
110 #define	CARDBUS_CLASS_SYSTEM			0x08
111 #define	CARDBUS_CLASS_INPUT			0x09
112 #define	CARDBUS_CLASS_DOCK			0x0a
113 #define	CARDBUS_CLASS_PROCESSOR			0x0b
114 #define	CARDBUS_CLASS_SERIALBUS			0x0c
115 #define	CARDBUS_CLASS_UNDEFINED			0xff
116 
117 /* 0x0c serial bus subclasses */
118 #define	CARDBUS_SUBCLASS_SERIALBUS_FIREWIRE	0x00
119 #define	CARDBUS_SUBCLASS_SERIALBUS_ACCESS	0x01
120 #define	CARDBUS_SUBCLASS_SERIALBUS_SSA		0x02
121 #define	CARDBUS_SUBCLASS_SERIALBUS_USB		0x03
122 #define	CARDBUS_SUBCLASS_SERIALBUS_FIBER	0x04
123 
124 /* BIST, Header Type, Latency Timer, Cache Line Size */
125 #define CARDBUS_BHLC_REG        0x0c
126 
127 #define	CARDBUS_BIST_SHIFT        24
128 #define	CARDBUS_BIST_MASK       0xff
129 #define	CARDBUS_BIST(bhlcr) \
130 	    (((bhlcr) >> CARDBUS_BIST_SHIFT) & CARDBUS_BIST_MASK)
131 
132 #define	CARDBUS_HDRTYPE_SHIFT     16
133 #define	CARDBUS_HDRTYPE_MASK    0xff
134 #define	CARDBUS_HDRTYPE(bhlcr) \
135 	    (((bhlcr) >> CARDBUS_HDRTYPE_SHIFT) & CARDBUS_HDRTYPE_MASK)
136 
137 #define	CARDBUS_HDRTYPE_TYPE(bhlcr) \
138 	    (CARDBUS_HDRTYPE(bhlcr) & 0x7f)
139 #define	CARDBUS_HDRTYPE_MULTIFN(bhlcr) \
140 	    ((CARDBUS_HDRTYPE(bhlcr) & 0x80) != 0)
141 
142 #define	CARDBUS_LATTIMER_SHIFT      8
143 #define	CARDBUS_LATTIMER_MASK    0xff
144 #define	CARDBUS_LATTIMER(bhlcr) \
145 	    (((bhlcr) >> CARDBUS_LATTIMER_SHIFT) & CARDBUS_LATTIMER_MASK)
146 
147 #define	CARDBUS_CACHELINE_SHIFT     0
148 #define	CARDBUS_CACHELINE_MASK   0xff
149 #define	CARDBUS_CACHELINE(bhlcr) \
150 	    (((bhlcr) >> CARDBUS_CACHELINE_SHIFT) & CARDBUS_CACHELINE_MASK)
151 
152 
153 /* Base Registers */
154 #define CARDBUS_BASE0_REG  0x10
155 #define CARDBUS_BASE1_REG  0x14
156 #define CARDBUS_BASE2_REG  0x18
157 #define CARDBUS_BASE3_REG  0x1C
158 #define CARDBUS_BASE4_REG  0x20
159 #define CARDBUS_BASE5_REG  0x24
160 #define CARDBUS_CIS_REG    0x28
161 #define CARDBUS_ROM_REG	   0x30
162 #  define CARDBUS_CIS_ASIMASK 0x07
163 #    define CARDBUS_CIS_ASI(x) (CARDBUS_CIS_ASIMASK & (x))
164 #  define CARDBUS_CIS_ASI_TUPLE 0x00
165 #  define CARDBUS_CIS_ASI_BAR0  0x01
166 #  define CARDBUS_CIS_ASI_BAR1  0x02
167 #  define CARDBUS_CIS_ASI_BAR2  0x03
168 #  define CARDBUS_CIS_ASI_BAR3  0x04
169 #  define CARDBUS_CIS_ASI_BAR4  0x05
170 #  define CARDBUS_CIS_ASI_BAR5  0x06
171 #  define CARDBUS_CIS_ASI_ROM   0x07
172 #  define CARDBUS_CIS_ADDRMASK 0x0ffffff8
173 #    define CARDBUS_CIS_ADDR(x) (CARDBUS_CIS_ADDRMASK & (x))
174 #    define CARDBUS_CIS_ASI_BAR(x) (((CARDBUS_CIS_ASIMASK & (x))-1)*4+0x10)
175 #    define CARDBUS_CIS_ASI_ROM_IMAGE(x) (((x) >> 28) & 0xf)
176 
177 #define	CARDBUS_INTERRUPT_REG   0x3c
178 
179 #define CARDBUS_MAPREG_TYPE_MEM		0x00000000
180 #define CARDBUS_MAPREG_TYPE_IO		0x00000001
181 
182 /* XXX end */
183 
184 #if rbus
185 
186 typedef struct cardbus_functions {
187   int (*cardbus_space_alloc)(cardbus_chipset_tag_t, rbus_tag_t,
188 				  bus_addr_t addr, bus_size_t size,
189 				  bus_addr_t mask, bus_size_t align,
190 				  int flags, bus_addr_t *addrp,
191 				  bus_space_handle_t *bshp);
192   int (*cardbus_space_free)(cardbus_chipset_tag_t, rbus_tag_t,
193 				 bus_space_handle_t, bus_size_t);
194   void *(*cardbus_intr_establish)(cardbus_chipset_tag_t, int irq, int level, int (*ih)(void *), void *sc);
195   void (*cardbus_intr_disestablish)(cardbus_chipset_tag_t ct, void *ih);
196   int (*cardbus_ctrl)(cardbus_chipset_tag_t, int);
197   int (*cardbus_power)(cardbus_chipset_tag_t, int);
198 
199   cardbustag_t (*cardbus_make_tag)(cardbus_chipset_tag_t, int, int, int);
200   void (*cardbus_free_tag)(cardbus_chipset_tag_t, cardbustag_t);
201   cardbusreg_t (*cardbus_conf_read)(cardbus_chipset_tag_t, cardbustag_t, int);
202   void (*cardbus_conf_write)(cardbus_chipset_tag_t, cardbustag_t, int, cardbusreg_t);
203 } cardbus_function_t, *cardbus_function_tag_t;
204 
205 #else
206 
207 typedef struct cardbus_functions {
208   int (*cardbus_ctrl)(cardbus_chipset_tag_t, int);
209   int (*cardbus_power)(cardbus_chipset_tag_t, int);
210   int (*cardbus_mem_open)(cardbus_chipset_tag_t, int, u_int32_t, u_int32_t);
211   int (*cardbus_mem_close)(cardbus_chipset_tag_t, int);
212   int (*cardbus_io_open)(cardbus_chipset_tag_t, int, u_int32_t, u_int32_t);
213   int (*cardbus_io_close)(cardbus_chipset_tag_t, int);
214   void *(*cardbus_intr_establish)(cardbus_chipset_tag_t, int irq, int level, int (*ih)(void *), void *sc);
215   void (*cardbus_intr_disestablish)(cardbus_chipset_tag_t ct, void *ih);
216 
217   cardbustag_t (*cardbus_make_tag)(cardbus_chipset_tag_t, int, int, int);  cardbusreg_t (*cardbus_conf_read)(cardbus_chipset_tag_t, cardbustag_t, int);
218   void (*cardbus_conf_write)(cardbus_chipset_tag_t, cardbustag_t, int, cardbusreg_t);
219 } cardbus_function_t, *cardbus_function_tag_t;
220 #endif /* rbus */
221 
222 /*
223  * struct cbslot_attach_args is the attach argument for cardbus card.
224  */
225 struct cbslot_attach_args {
226   char *cba_busname;
227   bus_space_tag_t cba_iot;	/* cardbus i/o space tag */
228   bus_space_tag_t cba_memt;	/* cardbus mem space tag */
229   bus_dma_tag_t cba_dmat;	/* DMA tag */
230 
231   int cba_bus;			/* cardbus bus number */
232 
233   cardbus_chipset_tag_t cba_cc;	/* cardbus chipset */
234   cardbus_function_tag_t cba_cf; /* cardbus functions */
235   int cba_intrline;		/* interrupt line */
236 
237 #if rbus
238   rbus_tag_t cba_rbus_iot;	/* CardBus i/o rbus tag */
239   rbus_tag_t cba_rbus_memt;	/* CardBus mem rbus tag */
240 #endif
241 
242   int cba_cacheline;		/* cache line size */
243   int cba_lattimer;		/* latency timer */
244 };
245 
246 
247 #define cbslotcf_dev  cf_loc[0]
248 #define cbslotcf_func cf_loc[1]
249 #define CBSLOT_UNK_DEV -1
250 #define CBSLOT_UNK_FUNC -1
251 
252 
253 struct cardbus_devfunc;
254 
255 /*
256  * struct cardbus_softc is the softc for cardbus card.
257  */
258 struct cardbus_softc {
259   struct device sc_dev;		/* fundamental device structure */
260 
261   int sc_bus;			/* cardbus bus number */
262   int sc_device;		/* cardbus device number */
263   int sc_intrline;		/* CardBus intrline */
264 
265   bus_space_tag_t sc_iot;	/* CardBus I/O space tag */
266   bus_space_tag_t sc_memt;	/* CardBus MEM space tag */
267   bus_dma_tag_t sc_dmat;	/* DMA tag */
268 
269   cardbus_chipset_tag_t sc_cc;	/* CardBus chipset */
270   cardbus_function_tag_t sc_cf;	/* CardBus function */
271 
272 #if rbus
273   rbus_tag_t sc_rbus_iot;	/* CardBus i/o rbus tag */
274   rbus_tag_t sc_rbus_memt;	/* CardBus mem rbus tag */
275 #endif
276 
277   int sc_cacheline;		/* cache line size */
278   int sc_lattimer;		/* latency timer */
279   int sc_volt;			/* applied Vcc voltage */
280 #define PCCARD_33V  0x02
281 #define PCCARD_XXV  0x04
282 #define PCCARD_YYV  0x08
283   int sc_poweron_func;
284   struct cardbus_devfunc *sc_funcs;	/* list of cardbus device functions */
285 };
286 
287 
288 /*
289  * struct cardbus_devfunc:
290  *
291  *   This is the data deposit for each function of a CardBus device.
292  *   This structure is used for memory or i/o space allocation and
293  *   disallocation.
294  */
295 typedef struct cardbus_devfunc {
296   cardbus_chipset_tag_t ct_cc;
297   cardbus_function_tag_t ct_cf;
298   struct cardbus_softc *ct_sc;	/* pointer to the parent */
299   int ct_bus;			/* bus number */
300   int ct_dev;			/* device number */
301   int ct_func;			/* function number */
302 
303 #if rbus
304   rbus_tag_t ct_rbus_iot;	/* CardBus i/o rbus tag */
305   rbus_tag_t ct_rbus_memt;	/* CardBus mem rbus tag */
306 #endif
307 
308   u_int32_t ct_bar[6];		/* Base Address Regs 0 to 6 */
309   u_int32_t ct_lc;		/* Latency timer and cache line size */
310   /* u_int32_t ct_cisreg; */    /* CIS reg: is it needed??? */
311 
312   struct device *ct_device;	/* pointer to the device */
313 
314   struct cardbus_devfunc *ct_next;
315 
316   /* some data structure needed for tuple??? */
317 } *cardbus_devfunc_t;
318 
319 
320 /* XXX various things extracted from CIS */
321 struct cardbus_cis_info {
322     int32_t		manufacturer;
323     int32_t		product;
324     char		cis1_info_buf[256];
325     char	       *cis1_info[4];
326     struct cb_bar_info {
327 	unsigned int flags;
328 	unsigned int size;
329     } bar[7];
330     unsigned int	funcid;
331     union {
332 	struct {
333 	    int uart_type;
334 	    int uart_present;
335 	} serial;
336 	struct {
337 	    char netid[6];
338 	    char netid_present;
339 	    char __filler;
340 	} network;
341     } funce;
342 };
343 
344 struct cardbus_attach_args {
345   int ca_unit;
346   cardbus_devfunc_t ca_ct;
347 
348   bus_space_tag_t ca_iot;	/* CardBus I/O space tag */
349   bus_space_tag_t ca_memt;	/* CardBus MEM space tag */
350   bus_dma_tag_t ca_dmat;	/* DMA tag */
351 
352   u_int ca_bus;
353   u_int ca_device;
354   u_int ca_function;
355   cardbustag_t ca_tag;
356   cardbusreg_t ca_id;
357   cardbusreg_t ca_class;
358 
359   /* interrupt information */
360   cardbus_intr_line_t ca_intrline;
361 
362 #if rbus
363   rbus_tag_t ca_rbus_iot;	/* CardBus i/o rbus tag */
364   rbus_tag_t ca_rbus_memt;	/* CardBus mem rbus tag */
365 #endif
366 
367   struct cardbus_cis_info ca_cis;
368 };
369 
370 
371 #define CARDBUS_ENABLE  1	/* enable the channel */
372 #define CARDBUS_DISABLE 2	/* disable the channel */
373 #define CARDBUS_RESET   4
374 #define CARDBUS_CD      7
375 #  define CARDBUS_NOCARD 0
376 #  define CARDBUS_5V_CARD 0x01	/* XXX: It must not exist */
377 #  define CARDBUS_3V_CARD 0x02
378 #  define CARDBUS_XV_CARD 0x04
379 #  define CARDBUS_YV_CARD 0x08
380 #define CARDBUS_IO_ENABLE    100
381 #define CARDBUS_IO_DISABLE   101
382 #define CARDBUS_MEM_ENABLE   102
383 #define CARDBUS_MEM_DISABLE  103
384 #define CARDBUS_BM_ENABLE    104 /* bus master */
385 #define CARDBUS_BM_DISABLE   105
386 
387 #define CARDBUS_VCC_UC  0x0000
388 #define CARDBUS_VCC_3V  0x0001
389 #define CARDBUS_VCC_XV  0x0002
390 #define CARDBUS_VCC_YV  0x0003
391 #define CARDBUS_VCC_0V  0x0004
392 #define CARDBUS_VCC_5V  0x0005	/* ??? */
393 #define CARDBUS_VCCMASK 0x000f
394 #define CARDBUS_VPP_UC  0x0000
395 #define CARDBUS_VPP_VCC 0x0010
396 #define CARDBUS_VPP_12V 0x0030
397 #define CARDBUS_VPP_0V  0x0040
398 #define CARDBUS_VPPMASK 0x00f0
399 
400 #define CARDBUSCF_DEV			0
401 #define CARDBUSCF_DEV_DEFAULT		-1
402 #define CARDBUSCF_FUNCTION		1
403 #define CARDBUSCF_FUNCTION_DEFAULT	-1
404 
405 /*
406  * Locators devies that attach to 'cardbus', as specified to config.
407  */
408 #define cardbuscf_dev cf_loc[CARDBUSCF_DEV]
409 #define CARDBUS_UNK_DEV CARDBUSCF_DEV_DEFAULT
410 
411 #define cardbuscf_function cf_loc[CARDBUSCF_FUNCTION]
412 #define CARDBUS_UNK_FUNCTION CARDBUSCF_FUNCTION_DEFAULT
413 
414 int cardbus_attach_card(struct cardbus_softc *);
415 void cardbus_detach_card(struct cardbus_softc *);
416 void *cardbus_intr_establish(cardbus_chipset_tag_t, cardbus_function_tag_t, cardbus_intr_handle_t irq, int level, int (*func) (void *), void *arg);
417 void cardbus_intr_disestablish(cardbus_chipset_tag_t, cardbus_function_tag_t, void *handler);
418 
419 int cardbus_mapreg_map(struct cardbus_softc *, int, int, cardbusreg_t,
420     int, bus_space_tag_t *, bus_space_handle_t *, bus_addr_t *, bus_size_t *);
421 int cardbus_mapreg_unmap(struct cardbus_softc *, int, int,
422     bus_space_tag_t, bus_space_handle_t, bus_size_t);
423 
424 int cardbus_save_bar(cardbus_devfunc_t);
425 int cardbus_restore_bar(cardbus_devfunc_t);
426 
427 int cardbus_function_enable(struct cardbus_softc *, int function);
428 int cardbus_function_disable(struct cardbus_softc *, int function);
429 
430 int cardbus_get_capability(cardbus_chipset_tag_t, cardbus_function_tag_t,
431     cardbustag_t, int, int *, cardbusreg_t *);
432 
433 struct cardbus_matchid {
434 	cardbus_vendor_id_t	cm_vid;
435 	cardbus_product_id_t	cm_pid;
436 };
437 
438 int cardbus_matchbyid(struct cardbus_attach_args *, const struct cardbus_matchid *, int);
439 
440 #define Cardbus_function_enable(ct) cardbus_function_enable((ct)->ct_sc, (ct)->ct_func)
441 #define Cardbus_function_disable(ct) cardbus_function_disable((ct)->ct_sc, (ct)->ct_func)
442 
443 
444 
445 #define Cardbus_mapreg_map(ct, reg, type, busflags, tagp, handlep, basep, sizep) \
446 	cardbus_mapreg_map((ct)->ct_sc, (ct->ct_func), (reg), (type),\
447 			   (busflags), (tagp), (handlep), (basep), (sizep))
448 #define Cardbus_mapreg_unmap(ct, reg, tag, handle, size) \
449 	cardbus_mapreg_unmap((ct)->ct_sc, (ct->ct_func), (reg), (tag), (handle), (size))
450 
451 #define Cardbus_make_tag(ct) (*(ct)->ct_cf->cardbus_make_tag)((ct)->ct_cc, (ct)->ct_bus, (ct)->ct_dev, (ct)->ct_func)
452 #define cardbus_make_tag(cc, cf, bus, device, function) ((cf)->cardbus_make_tag)((cc), (bus), (device), (function))
453 
454 #define Cardbus_free_tag(ct, tag) (*(ct)->ct_cf->cardbus_free_tag)((ct)->ct_cc, (tag))
455 #define cardbus_free_tag(cc, cf, tag) (*(cf)->cardbus_free_tag)(cc, (tag))
456 
457 #define Cardbus_conf_read(ct, tag, offs) (*(ct)->ct_cf->cardbus_conf_read)((ct)->ct_cf, (tag), (offs))
458 #define cardbus_conf_read(cc, cf, tag, offs) ((cf)->cardbus_conf_read)((cc), (tag), (offs))
459 #define Cardbus_conf_write(ct, tag, offs, val) (*(ct)->ct_cf->cardbus_conf_write)((ct)->ct_cf, (tag), (offs), (val))
460 #define cardbus_conf_write(cc, cf, tag, offs, val) ((cf)->cardbus_conf_write)((cc), (tag), (offs), (val))
461 
462 #endif /* !_DEV_CARDBUS_CARDBUSVAR_H_ */
463