1 /* $NetBSD: pcmciavar.h,v 1.33 2008/07/03 19:07:43 drochner 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 void *ihandle; /* opaque i/o handle */ 49 }; 50 51 #define PCMCIA_IO_ALLOCATED 0x01 /* i/o space was allocated */ 52 53 /* 54 * Contains information about allocated memory space. 55 */ 56 struct pcmcia_mem_handle { 57 bus_space_tag_t memt; /* bus space tag (from chipset) */ 58 bus_space_handle_t memh; /* mapped space handle */ 59 bus_addr_t addr; /* resulting address in bus space */ 60 bus_size_t size; /* size of mem space */ 61 pcmcia_mem_handle_t mhandle; /* opaque memory handle */ 62 bus_size_t realsize; /* how much we really allocated */ 63 }; 64 65 /* pcmcia itself */ 66 67 #define PCMCIA_CFE_MWAIT_REQUIRED 0x0001 68 #define PCMCIA_CFE_RDYBSY_ACTIVE 0x0002 69 #define PCMCIA_CFE_WP_ACTIVE 0x0004 70 #define PCMCIA_CFE_BVD_ACTIVE 0x0008 71 #define PCMCIA_CFE_IO8 0x0010 72 #define PCMCIA_CFE_IO16 0x0020 73 #define PCMCIA_CFE_IRQSHARE 0x0040 74 #define PCMCIA_CFE_IRQPULSE 0x0080 75 #define PCMCIA_CFE_IRQLEVEL 0x0100 76 #define PCMCIA_CFE_POWERDOWN 0x0200 77 #define PCMCIA_CFE_READONLY 0x0400 78 #define PCMCIA_CFE_AUDIO 0x0800 79 80 struct pcmcia_config_entry { 81 int number; 82 u_int32_t flags; 83 int iftype; 84 int num_iospace; 85 86 /* 87 * The card will only decode this mask in any case, so we can 88 * do dynamic allocation with this in mind, in case the suggestions 89 * below are no good. 90 */ 91 u_long iomask; 92 struct { 93 u_long length; 94 u_long start; 95 96 struct pcmcia_io_handle handle; 97 int window; 98 } iospace[4]; /* XXX this could be as high as 16 */ 99 u_int16_t irqmask; 100 int num_memspace; 101 struct { 102 u_long length; 103 u_long cardaddr; 104 u_long hostaddr; 105 106 struct pcmcia_mem_handle handle; 107 bus_size_t offset; 108 int window; 109 110 } memspace[2]; /* XXX this could be as high as 8 */ 111 int maxtwins; 112 SIMPLEQ_ENTRY(pcmcia_config_entry) cfe_list; 113 }; 114 115 116 struct pcmcia_funce_disk { 117 int pfd_interface; 118 }; 119 120 struct pcmcia_funce_lan { 121 int pfl_nidlen; 122 u_int8_t pfl_nid[8]; 123 }; 124 125 union pcmcia_funce { 126 struct pcmcia_funce_disk pfv_disk; 127 struct pcmcia_funce_lan pfv_lan; 128 }; 129 130 131 struct pcmcia_function { 132 /* read off the card */ 133 int number; 134 int function; 135 int last_config_index; 136 u_long ccr_base; 137 u_long ccr_mask; 138 SIMPLEQ_HEAD(, pcmcia_config_entry) cfe_head; 139 SIMPLEQ_ENTRY(pcmcia_function) pf_list; 140 /* run-time state */ 141 struct pcmcia_softc *sc; 142 struct device *child; 143 struct pcmcia_config_entry *cfe; 144 struct pcmcia_mem_handle pf_pcmh; 145 #define pf_ccrt pf_pcmh.memt 146 #define pf_ccrh pf_pcmh.memh 147 #define pf_ccr_mhandle pf_pcmh.mhandle 148 #define pf_ccr_realsize pf_pcmh.realsize 149 bus_size_t pf_ccr_offset; 150 int pf_ccr_window; 151 bus_addr_t pf_mfc_iobase; 152 bus_addr_t pf_mfc_iomax; 153 void *pf_ih; 154 int pf_flags; 155 156 union pcmcia_funce pf_funce; /* CISTPL_FUNCE */ 157 #define pf_funce_disk_interface pf_funce.pfv_disk.pfd_interface 158 #define pf_funce_lan_nid pf_funce.pfv_lan.pfl_nid 159 #define pf_funce_lan_nidlen pf_funce.pfv_lan.pfl_nidlen 160 }; 161 162 /* pf_flags */ 163 #define PFF_ENABLED 0x0001 /* function is enabled */ 164 #define PFF_DETACHED 0x0002 /* card is detached */ 165 166 SIMPLEQ_HEAD(pcmcia_function_head, pcmcia_function); 167 168 struct pcmcia_card { 169 int cis1_major; 170 int cis1_minor; 171 /* XXX waste of space? */ 172 char cis1_info_buf[256]; 173 char *cis1_info[4]; 174 /* 175 * Use int32_t for manufacturer and product so that they can 176 * hold the id value found in card CIS and special value that 177 * indicates no id was found. 178 */ 179 int32_t manufacturer; 180 #define PCMCIA_VENDOR_INVALID -1 181 int32_t product; 182 #define PCMCIA_PRODUCT_INVALID -1 183 u_int16_t error; 184 #define PCMCIA_CIS_INVALID { NULL, NULL, NULL, NULL } 185 struct pcmcia_function_head pf_head; 186 }; 187 188 struct pcmcia_softc { 189 device_t dev; 190 191 /* this stuff is for the socket */ 192 pcmcia_chipset_tag_t pct; 193 pcmcia_chipset_handle_t pch; 194 195 /* this stuff is for the card */ 196 struct pcmcia_card card; 197 void *ih; 198 int sc_enabled_count; /* how many functions are 199 enabled */ 200 201 /* 202 * These are passed down from the PCMCIA chip, and exist only 203 * so that cards with Very Special address allocation needs 204 * know what range they should be dealing with. 205 */ 206 bus_addr_t iobase; /* start i/o space allocation here */ 207 bus_size_t iosize; /* size of the i/o space range */ 208 }; 209 210 struct pcmcia_cis_quirk { 211 int32_t manufacturer; 212 int32_t product; 213 const char *cis1_info[4]; 214 const struct pcmcia_function *pf; 215 const struct pcmcia_config_entry *cfe; 216 }; 217 218 struct pcmcia_attach_args { 219 int32_t manufacturer; 220 int32_t product; 221 struct pcmcia_card *card; 222 struct pcmcia_function *pf; 223 }; 224 225 struct pcmcia_tuple { 226 unsigned int code; 227 unsigned int length; 228 u_long mult; 229 bus_size_t ptr; 230 bus_space_tag_t memt; 231 bus_space_handle_t memh; 232 }; 233 234 struct pcmcia_product { 235 u_int32_t pp_vendor; 236 u_int32_t pp_product; 237 const char *pp_cisinfo[4]; 238 }; 239 240 typedef int (*pcmcia_product_match_fn)(struct pcmcia_attach_args *, 241 const struct pcmcia_product *, int); 242 243 const void *pcmcia_product_lookup(struct pcmcia_attach_args *, const void *, 244 size_t, size_t, pcmcia_product_match_fn); 245 246 void pcmcia_devinfo(struct pcmcia_card *, int, char *, size_t); 247 248 void pcmcia_read_cis(struct pcmcia_softc *); 249 void pcmcia_check_cis_quirks(struct pcmcia_softc *); 250 void pcmcia_print_cis(struct pcmcia_softc *); 251 int pcmcia_scan_cis(struct device *, 252 int (*) (struct pcmcia_tuple *, void *), void *); 253 254 #define pcmcia_cis_read_1(tuple, idx0) \ 255 (bus_space_read_1((tuple)->memt, (tuple)->memh, (tuple)->mult*(idx0))) 256 257 #define pcmcia_tuple_read_1(tuple, idx1) \ 258 (pcmcia_cis_read_1((tuple), ((tuple)->ptr+(2+(idx1))))) 259 260 #define pcmcia_tuple_read_2(tuple, idx2) \ 261 (pcmcia_tuple_read_1((tuple), (idx2)) | \ 262 (pcmcia_tuple_read_1((tuple), (idx2)+1)<<8)) 263 264 #define pcmcia_tuple_read_3(tuple, idx3) \ 265 (pcmcia_tuple_read_1((tuple), (idx3)) | \ 266 (pcmcia_tuple_read_1((tuple), (idx3)+1)<<8) | \ 267 (pcmcia_tuple_read_1((tuple), (idx3)+2)<<16)) 268 269 #define pcmcia_tuple_read_4(tuple, idx4) \ 270 (pcmcia_tuple_read_1((tuple), (idx4)) | \ 271 (pcmcia_tuple_read_1((tuple), (idx4)+1)<<8) | \ 272 (pcmcia_tuple_read_1((tuple), (idx4)+2)<<16) | \ 273 (pcmcia_tuple_read_1((tuple), (idx4)+3)<<24)) 274 275 #define pcmcia_tuple_read_n(tuple, n, idxn) \ 276 (((n)==1)?pcmcia_tuple_read_1((tuple), (idxn)) : \ 277 (((n)==2)?pcmcia_tuple_read_2((tuple), (idxn)) : \ 278 (((n)==3)?pcmcia_tuple_read_3((tuple), (idxn)) : \ 279 /* n == 4 */ pcmcia_tuple_read_4((tuple), (idxn))))) 280 281 #define PCMCIA_SPACE_MEMORY 1 282 #define PCMCIA_SPACE_IO 2 283 284 int pcmcia_ccr_read(struct pcmcia_function *, int); 285 void pcmcia_ccr_write(struct pcmcia_function *, int, int); 286 287 #define pcmcia_mfc(sc) (! SIMPLEQ_EMPTY(&(sc)->card.pf_head) && \ 288 SIMPLEQ_NEXT(SIMPLEQ_FIRST(&(sc)->card.pf_head), pf_list)) 289 290 void pcmcia_socket_enable(struct device *); 291 void pcmcia_socket_disable(struct device *); 292 void pcmcia_socket_settype(struct device *, int); 293 294 int pcmcia_config_alloc(struct pcmcia_function *, 295 struct pcmcia_config_entry *); 296 void pcmcia_config_free(struct pcmcia_function *); 297 int pcmcia_config_map(struct pcmcia_function *); 298 void pcmcia_config_unmap(struct pcmcia_function *); 299 300 301 int pcmcia_function_configure(struct pcmcia_function *, 302 int (*validator)(struct pcmcia_config_entry *)); 303 void pcmcia_function_unconfigure(struct pcmcia_function *); 304 void pcmcia_function_init(struct pcmcia_function *, 305 struct pcmcia_config_entry *); 306 int pcmcia_function_enable(struct pcmcia_function *); 307 void pcmcia_function_disable(struct pcmcia_function *); 308 309 #define pcmcia_io_alloc(pf, start, size, align, pciop) \ 310 (pcmcia_chip_io_alloc((pf)->sc->pct, pf->sc->pch, (start), \ 311 (size), (align), (pciop))) 312 313 #define pcmcia_io_free(pf, pciohp) \ 314 (pcmcia_chip_io_free((pf)->sc->pct, (pf)->sc->pch, (pciohp))) 315 316 int pcmcia_io_map(struct pcmcia_function *, int, 317 struct pcmcia_io_handle *, int *); 318 void pcmcia_io_unmap(struct pcmcia_function *, int); 319 320 void pcmcia_free_pf(struct pcmcia_function_head *); 321 322 #define pcmcia_mem_alloc(pf, size, pcmhp) \ 323 (pcmcia_chip_mem_alloc((pf)->sc->pct, (pf)->sc->pch, (size), (pcmhp))) 324 325 #define pcmcia_mem_free(pf, pcmhp) \ 326 (pcmcia_chip_mem_free((pf)->sc->pct, (pf)->sc->pch, (pcmhp))) 327 328 #define pcmcia_mem_map(pf, kind, card_addr, size, pcmhp, offsetp, windowp) \ 329 (pcmcia_chip_mem_map((pf)->sc->pct, (pf)->sc->pch, (kind), \ 330 (card_addr), (size), (pcmhp), (offsetp), (windowp))) 331 332 #define pcmcia_mem_unmap(pf, window) \ 333 (pcmcia_chip_mem_unmap((pf)->sc->pct, (pf)->sc->pch, (window))) 334 335 void *pcmcia_intr_establish(struct pcmcia_function *, int, 336 int (*) (void *), void *); 337 void pcmcia_intr_disestablish(struct pcmcia_function *, void *); 338