Home | History | Annotate | Line # | Download | only in pcmcia
pcmciavar.h revision 1.26
      1 /*	$NetBSD: pcmciavar.h,v 1.26 2004/08/12 16:04:20 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Marc Horowitz.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/types.h>
     33 #include <sys/queue.h>
     34 
     35 #include <dev/pcmcia/pcmciachip.h>
     36 
     37 extern int	pcmcia_verbose;
     38 
     39 /*
     40  * Contains information about mapped/allocated i/o spaces.
     41  */
     42 struct pcmcia_io_handle {
     43 	bus_space_tag_t iot;		/* bus space tag (from chipset) */
     44 	bus_space_handle_t ioh;		/* mapped space handle */
     45 	bus_addr_t      addr;		/* resulting address in bus space */
     46 	bus_size_t      size;		/* size of i/o space */
     47 	int             flags;		/* misc. information */
     48 };
     49 
     50 #define	PCMCIA_IO_ALLOCATED	0x01	/* i/o space was allocated */
     51 
     52 /*
     53  * Contains information about allocated memory space.
     54  */
     55 struct pcmcia_mem_handle {
     56 	bus_space_tag_t memt;		/* bus space tag (from chipset) */
     57 	bus_space_handle_t memh;	/* mapped space handle */
     58 	bus_addr_t      addr;		/* resulting address in bus space */
     59 	bus_size_t      size;		/* size of mem space */
     60 	pcmcia_mem_handle_t mhandle;	/* opaque memory handle */
     61 	bus_size_t      realsize;	/* how much we really allocated */
     62 };
     63 
     64 /* pcmcia itself */
     65 
     66 #define PCMCIA_CFE_MWAIT_REQUIRED	0x0001
     67 #define PCMCIA_CFE_RDYBSY_ACTIVE	0x0002
     68 #define PCMCIA_CFE_WP_ACTIVE		0x0004
     69 #define PCMCIA_CFE_BVD_ACTIVE		0x0008
     70 #define PCMCIA_CFE_IO8			0x0010
     71 #define PCMCIA_CFE_IO16			0x0020
     72 #define PCMCIA_CFE_IRQSHARE		0x0040
     73 #define PCMCIA_CFE_IRQPULSE		0x0080
     74 #define PCMCIA_CFE_IRQLEVEL		0x0100
     75 #define PCMCIA_CFE_POWERDOWN		0x0200
     76 #define PCMCIA_CFE_READONLY		0x0400
     77 #define PCMCIA_CFE_AUDIO		0x0800
     78 
     79 struct pcmcia_config_entry {
     80 	int		number;
     81 	u_int32_t	flags;
     82 	int		iftype;
     83 	int		num_iospace;
     84 
     85 	/*
     86 	 * The card will only decode this mask in any case, so we can
     87 	 * do dynamic allocation with this in mind, in case the suggestions
     88 	 * below are no good.
     89 	 */
     90 	u_long		iomask;
     91 	struct {
     92 		u_long	length;
     93 		u_long	start;
     94 
     95 		struct	pcmcia_io_handle handle;
     96 		int	window;
     97 	} iospace[4];		/* XXX this could be as high as 16 */
     98 	u_int16_t	irqmask;
     99 	int		num_memspace;
    100 	struct {
    101 		u_long	length;
    102 		u_long	cardaddr;
    103 		u_long	hostaddr;
    104 
    105 		struct	pcmcia_mem_handle handle;
    106 		bus_size_t offset;
    107 		int	window;
    108 
    109 	} memspace[2];		/* XXX this could be as high as 8 */
    110 	int		maxtwins;
    111 	SIMPLEQ_ENTRY(pcmcia_config_entry) cfe_list;
    112 };
    113 
    114 
    115 struct pcmcia_funce_disk {
    116 	int pfd_interface;
    117 };
    118 
    119 struct pcmcia_funce_lan {
    120 	int pfl_nidlen;
    121 	u_int8_t pfl_nid[8];
    122 };
    123 
    124 union pcmcia_funce {
    125 	struct pcmcia_funce_disk pfv_disk;
    126 	struct pcmcia_funce_lan pfv_lan;
    127 };
    128 
    129 
    130 struct pcmcia_function {
    131 	/* read off the card */
    132 	int		number;
    133 	int		function;
    134 	int		last_config_index;
    135 	u_long		ccr_base;
    136 	u_long		ccr_mask;
    137 	SIMPLEQ_HEAD(, pcmcia_config_entry) cfe_head;
    138 	SIMPLEQ_ENTRY(pcmcia_function) pf_list;
    139 	/* run-time state */
    140 	struct pcmcia_softc *sc;
    141 	struct device *child;
    142 	struct pcmcia_config_entry *cfe;
    143 	struct pcmcia_mem_handle pf_pcmh;
    144 #define	pf_ccrt		pf_pcmh.memt
    145 #define	pf_ccrh		pf_pcmh.memh
    146 #define	pf_ccr_mhandle	pf_pcmh.mhandle
    147 #define	pf_ccr_realsize	pf_pcmh.realsize
    148 	bus_size_t	pf_ccr_offset;
    149 	int		pf_ccr_window;
    150 	bus_addr_t	pf_mfc_iobase;
    151 	bus_addr_t	pf_mfc_iomax;
    152 	void		*pf_ih;
    153 	int		pf_flags;
    154 
    155 	union pcmcia_funce pf_funce; /* CISTPL_FUNCE */
    156 #define pf_funce_disk_interface pf_funce.pfv_disk.pfd_interface
    157 #define pf_funce_lan_nid pf_funce.pfv_lan.pfl_nid
    158 #define pf_funce_lan_nidlen pf_funce.pfv_lan.pfl_nidlen
    159 };
    160 
    161 /* pf_flags */
    162 #define	PFF_ENABLED	0x0001		/* function is enabled */
    163 
    164 SIMPLEQ_HEAD(pcmcia_function_head, pcmcia_function);
    165 
    166 struct pcmcia_card {
    167 	int		cis1_major;
    168 	int		cis1_minor;
    169 	/* XXX waste of space? */
    170 	char		cis1_info_buf[256];
    171 	char		*cis1_info[4];
    172 	/*
    173 	 * Use int32_t for manufacturer and product so that they can
    174 	 * hold the id value found in card CIS and special value that
    175 	 * indicates no id was found.
    176 	 */
    177 	int32_t		manufacturer;
    178 #define	PCMCIA_VENDOR_INVALID	-1
    179 	int32_t		product;
    180 #define	PCMCIA_PRODUCT_INVALID		-1
    181 	u_int16_t	error;
    182 #define	PCMCIA_CIS_INVALID		{ NULL, NULL, NULL, NULL }
    183 	struct pcmcia_function_head	pf_head;
    184 };
    185 
    186 struct pcmcia_softc {
    187 	struct device	dev;
    188 
    189 	/* this stuff is for the socket */
    190 	pcmcia_chipset_tag_t pct;
    191 	pcmcia_chipset_handle_t pch;
    192 
    193 	/* this stuff is for the card */
    194 	struct pcmcia_card card;
    195 	void		*ih;
    196 	int		sc_enabled_count;	/* how many functions are
    197 						   enabled */
    198 
    199 	/*
    200 	 * These are passed down from the PCMCIA chip, and exist only
    201 	 * so that cards with Very Special address allocation needs
    202 	 * know what range they should be dealing with.
    203 	 */
    204 	bus_addr_t iobase;		/* start i/o space allocation here */
    205 	bus_size_t iosize;		/* size of the i/o space range */
    206 };
    207 
    208 struct pcmcia_cis_quirk {
    209 	int32_t manufacturer;
    210 	int32_t product;
    211 	const char *cis1_info[4];
    212 	const struct pcmcia_function *pf;
    213 	const struct pcmcia_config_entry *cfe;
    214 };
    215 
    216 struct pcmcia_attach_args {
    217 	int32_t manufacturer;
    218 	int32_t product;
    219 	struct pcmcia_card *card;
    220 	struct pcmcia_function *pf;
    221 };
    222 
    223 struct pcmcia_tuple {
    224 	unsigned int	code;
    225 	unsigned int	length;
    226 	u_long		mult;
    227 	bus_size_t	ptr;
    228 	bus_space_tag_t	memt;
    229 	bus_space_handle_t memh;
    230 };
    231 
    232 struct pcmcia_product {
    233 	u_int32_t	pp_vendor;
    234 	u_int32_t	pp_product;
    235 	const char	*pp_cisinfo[4];
    236 };
    237 
    238 typedef int (*pcmcia_product_match_fn) __P((struct pcmcia_attach_args *,
    239     const struct pcmcia_product *, int));
    240 
    241 const void *
    242 	pcmcia_product_lookup __P((struct pcmcia_attach_args *, const void *,
    243 	    size_t, size_t, pcmcia_product_match_fn));
    244 
    245 void	pcmcia_devinfo __P((struct pcmcia_card *, int, char *, size_t));
    246 
    247 void	pcmcia_read_cis __P((struct pcmcia_softc *));
    248 void	pcmcia_check_cis_quirks __P((struct pcmcia_softc *));
    249 void	pcmcia_print_cis __P((struct pcmcia_softc *));
    250 int	pcmcia_scan_cis __P((struct device *,
    251 	    int (*) (struct pcmcia_tuple *, void *), void *));
    252 
    253 #define	pcmcia_cis_read_1(tuple, idx0)					\
    254 	(bus_space_read_1((tuple)->memt, (tuple)->memh, (tuple)->mult*(idx0)))
    255 
    256 #define	pcmcia_tuple_read_1(tuple, idx1)				\
    257 	(pcmcia_cis_read_1((tuple), ((tuple)->ptr+(2+(idx1)))))
    258 
    259 #define	pcmcia_tuple_read_2(tuple, idx2)				\
    260 	(pcmcia_tuple_read_1((tuple), (idx2)) | 			\
    261 	 (pcmcia_tuple_read_1((tuple), (idx2)+1)<<8))
    262 
    263 #define	pcmcia_tuple_read_3(tuple, idx3)				\
    264 	(pcmcia_tuple_read_1((tuple), (idx3)) |				\
    265 	 (pcmcia_tuple_read_1((tuple), (idx3)+1)<<8) |			\
    266 	 (pcmcia_tuple_read_1((tuple), (idx3)+2)<<16))
    267 
    268 #define	pcmcia_tuple_read_4(tuple, idx4)				\
    269 	(pcmcia_tuple_read_1((tuple), (idx4)) |				\
    270 	 (pcmcia_tuple_read_1((tuple), (idx4)+1)<<8) |			\
    271 	 (pcmcia_tuple_read_1((tuple), (idx4)+2)<<16) |			\
    272 	 (pcmcia_tuple_read_1((tuple), (idx4)+3)<<24))
    273 
    274 #define	pcmcia_tuple_read_n(tuple, n, idxn)				\
    275 	(((n)==1)?pcmcia_tuple_read_1((tuple), (idxn)) :		\
    276 	 (((n)==2)?pcmcia_tuple_read_2((tuple), (idxn)) :		\
    277 	  (((n)==3)?pcmcia_tuple_read_3((tuple), (idxn)) :		\
    278 	   /* n == 4 */ pcmcia_tuple_read_4((tuple), (idxn)))))
    279 
    280 #define	PCMCIA_SPACE_MEMORY	1
    281 #define	PCMCIA_SPACE_IO		2
    282 
    283 int	pcmcia_ccr_read __P((struct pcmcia_function *, int));
    284 void	pcmcia_ccr_write __P((struct pcmcia_function *, int, int));
    285 
    286 #define	pcmcia_mfc(sc)	(! SIMPLEQ_EMPTY(&(sc)->card.pf_head) &&	\
    287 		 SIMPLEQ_NEXT(SIMPLEQ_FIRST(&(sc)->card.pf_head), pf_list))
    288 
    289 void	pcmcia_socket_enable __P((struct device *));
    290 void	pcmcia_socket_disable __P((struct device *));
    291 void	pcmcia_socket_settype __P((struct device *));
    292 
    293 int	pcmcia_config_alloc __P((struct pcmcia_function *,
    294 	    struct pcmcia_config_entry *));
    295 void	pcmcia_config_free __P((struct pcmcia_function *));
    296 int	pcmcia_config_map __P((struct pcmcia_function *));
    297 void	pcmcia_config_unmap __P((struct pcmcia_function *));
    298 
    299 
    300 int	pcmcia_function_configure __P((struct pcmcia_function *,
    301 	    int (*validator)(struct pcmcia_config_entry *)));
    302 void	pcmcia_function_unconfigure __P((struct pcmcia_function *));
    303 void	pcmcia_function_init __P((struct pcmcia_function *,
    304 	    struct pcmcia_config_entry *));
    305 int	pcmcia_function_enable __P((struct pcmcia_function *));
    306 void	pcmcia_function_disable __P((struct pcmcia_function *));
    307 
    308 #define	pcmcia_io_alloc(pf, start, size, align, pciop)			\
    309 	(pcmcia_chip_io_alloc((pf)->sc->pct, pf->sc->pch, (start),	\
    310 	 (size), (align), (pciop)))
    311 
    312 #define	pcmcia_io_free(pf, pciohp)					\
    313 	(pcmcia_chip_io_free((pf)->sc->pct, (pf)->sc->pch, (pciohp)))
    314 
    315 int	pcmcia_io_map __P((struct pcmcia_function *, int,
    316 	    struct pcmcia_io_handle *, int *));
    317 void	pcmcia_io_unmap __P((struct pcmcia_function *, int));
    318 
    319 void	pcmcia_free_pf __P((struct pcmcia_function_head *));
    320 
    321 #define pcmcia_mem_alloc(pf, size, pcmhp)				\
    322 	(pcmcia_chip_mem_alloc((pf)->sc->pct, (pf)->sc->pch, (size), (pcmhp)))
    323 
    324 #define pcmcia_mem_free(pf, pcmhp)					\
    325 	(pcmcia_chip_mem_free((pf)->sc->pct, (pf)->sc->pch, (pcmhp)))
    326 
    327 #define pcmcia_mem_map(pf, kind, card_addr, size, pcmhp, offsetp, windowp) \
    328 	(pcmcia_chip_mem_map((pf)->sc->pct, (pf)->sc->pch, (kind),	\
    329 	 (card_addr), (size), (pcmhp), (offsetp), (windowp)))
    330 
    331 #define	pcmcia_mem_unmap(pf, window)					\
    332 	(pcmcia_chip_mem_unmap((pf)->sc->pct, (pf)->sc->pch, (window)))
    333 
    334 void	*pcmcia_intr_establish __P((struct pcmcia_function *, int,
    335 	    int (*) (void *), void *));
    336 void 	pcmcia_intr_disestablish __P((struct pcmcia_function *, void *));
    337