pccardvar.h revision 75761
166200Simp/*	$NetBSD: pcmciavar.h,v 1.12 2000/02/08 12:51:31 enami Exp $	*/
252506Simp/* $FreeBSD: head/sys/dev/pccard/pccardvar.h 75761 2001-04-21 04:08:39Z imp $ */
352506Simp
452506Simp/*
552506Simp * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
652506Simp *
752506Simp * Redistribution and use in source and binary forms, with or without
852506Simp * modification, are permitted provided that the following conditions
952506Simp * are met:
1052506Simp * 1. Redistributions of source code must retain the above copyright
1152506Simp *    notice, this list of conditions and the following disclaimer.
1252506Simp * 2. Redistributions in binary form must reproduce the above copyright
1352506Simp *    notice, this list of conditions and the following disclaimer in the
1452506Simp *    documentation and/or other materials provided with the distribution.
1552506Simp * 3. All advertising materials mentioning features or use of this software
1652506Simp *    must display the following acknowledgement:
1752506Simp *	This product includes software developed by Marc Horowitz.
1852506Simp * 4. The name of the author may not be used to endorse or promote products
1952506Simp *    derived from this software without specific prior written permission.
2052506Simp *
2152506Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2252506Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2352506Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2452506Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2552506Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2652506Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2752506Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2852506Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2952506Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3052506Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3152506Simp */
3252506Simp
3352506Simp#include <sys/types.h>
3452506Simp#include <sys/queue.h>
3552506Simp
3652506Simp#include <machine/bus.h>
3774636Simp#include "card_if.h"
3852506Simp
3952506Simpextern int	pccard_verbose;
4052506Simp
4152506Simp/*
4252506Simp * Contains information about mapped/allocated i/o spaces.
4352506Simp */
4452506Simpstruct pccard_io_handle {
4552506Simp	bus_space_tag_t iot;		/* bus space tag (from chipset) */
4652506Simp	bus_space_handle_t ioh;		/* mapped space handle */
4752506Simp	bus_addr_t      addr;		/* resulting address in bus space */
4852506Simp	bus_size_t      size;		/* size of i/o space */
4952506Simp	int             flags;		/* misc. information */
5054250Simp	int		width;
5152506Simp};
5252506Simp
5352506Simp#define	PCCARD_IO_ALLOCATED	0x01	/* i/o space was allocated */
5452506Simp
5552506Simp/*
5652506Simp * Contains information about allocated memory space.
5752506Simp */
5852506Simpstruct pccard_mem_handle {
5952506Simp	bus_space_tag_t memt;		/* bus space tag (from chipset) */
6052506Simp	bus_space_handle_t memh;	/* mapped space handle */
6152506Simp	bus_addr_t      addr;		/* resulting address in bus space */
6252506Simp	bus_size_t      size;		/* size of mem space */
6352506Simp	bus_size_t      realsize;	/* how much we really allocated */
6454250Simp	long		offset;
6554250Simp	int		kind;
6652506Simp};
6752506Simp
6852506Simp/* pccard itself */
6952506Simp
7052506Simp#define PCCARD_CFE_MWAIT_REQUIRED	0x0001
7152506Simp#define PCCARD_CFE_RDYBSY_ACTIVE	0x0002
7252506Simp#define PCCARD_CFE_WP_ACTIVE		0x0004
7352506Simp#define PCCARD_CFE_BVD_ACTIVE		0x0008
7452506Simp#define PCCARD_CFE_IO8			0x0010
7552506Simp#define PCCARD_CFE_IO16			0x0020
7652506Simp#define PCCARD_CFE_IRQSHARE		0x0040
7752506Simp#define PCCARD_CFE_IRQPULSE		0x0080
7852506Simp#define PCCARD_CFE_IRQLEVEL		0x0100
7952506Simp#define PCCARD_CFE_POWERDOWN		0x0200
8052506Simp#define PCCARD_CFE_READONLY		0x0400
8152506Simp#define PCCARD_CFE_AUDIO		0x0800
8252506Simp
8352506Simpstruct pccard_config_entry {
8452506Simp	int		number;
8552506Simp	u_int32_t	flags;
8652506Simp	int		iftype;
8752506Simp	int		num_iospace;
8852506Simp
8952506Simp	/*
9052506Simp	 * The card will only decode this mask in any case, so we can
9152506Simp	 * do dynamic allocation with this in mind, in case the suggestions
9252506Simp	 * below are no good.
9352506Simp	 */
9452506Simp	u_long		iomask;
9552506Simp	struct {
9652506Simp		u_long	length;
9752506Simp		u_long	start;
9852506Simp	} iospace[4];		/* XXX this could be as high as 16 */
9952506Simp	u_int16_t	irqmask;
10052506Simp	int		num_memspace;
10152506Simp	struct {
10252506Simp		u_long	length;
10352506Simp		u_long	cardaddr;
10452506Simp		u_long	hostaddr;
10552506Simp	} memspace[2];		/* XXX this could be as high as 8 */
10652506Simp	int		maxtwins;
10767187Simp	struct resource *iores[4];
10867187Simp	int		iorid[4];
10967187Simp	struct resource *irqres;
11067187Simp	int		irqrid;
11167187Simp	struct resource *memres[2];
11267187Simp	int		memrid[2];
11360938Sjake	STAILQ_ENTRY(pccard_config_entry) cfe_list;
11452506Simp};
11552506Simp
11652506Simpstruct pccard_function {
11752506Simp	/* read off the card */
11852506Simp	int		number;
11952506Simp	int		function;
12052506Simp	int		last_config_index;
12152506Simp	u_long		ccr_base;
12252506Simp	u_long		ccr_mask;
12355720Simp	struct resource *ccr_res;
12455720Simp	int		ccr_rid;
12560938Sjake	STAILQ_HEAD(, pccard_config_entry) cfe_head;
12660938Sjake	STAILQ_ENTRY(pccard_function) pf_list;
12752506Simp	/* run-time state */
12852506Simp	struct pccard_softc *sc;
12952506Simp	struct pccard_config_entry *cfe;
13052506Simp	struct pccard_mem_handle pf_pcmh;
13164927Simp	device_t	dev;
13252506Simp#define	pf_ccrt		pf_pcmh.memt
13352506Simp#define	pf_ccrh		pf_pcmh.memh
13452506Simp#define	pf_ccr_realsize	pf_pcmh.realsize
13552506Simp	bus_addr_t	pf_ccr_offset;
13652506Simp	int		pf_ccr_window;
13752506Simp	long		pf_mfc_iobase;
13852506Simp	long		pf_mfc_iomax;
13952506Simp	int		pf_flags;
14070715Sjon	driver_intr_t	*intr_handler;
14170715Sjon	void		*intr_handler_arg;
14270715Sjon	void		*intr_handler_cookie;
14352506Simp};
14452506Simp
14552506Simp/* pf_flags */
14652506Simp#define	PFF_ENABLED	0x0001		/* function is enabled */
14752506Simp
14852506Simpstruct pccard_card {
14952506Simp	int		cis1_major;
15052506Simp	int		cis1_minor;
15152506Simp	/* XXX waste of space? */
15252506Simp	char		cis1_info_buf[256];
15352506Simp	char		*cis1_info[4];
15452506Simp	/*
15552506Simp	 * Use int32_t for manufacturer and product so that they can
15652506Simp	 * hold the id value found in card CIS and special value that
15752506Simp	 * indicates no id was found.
15852506Simp	 */
15952506Simp	int32_t		manufacturer;
16052506Simp#define	PCCARD_VENDOR_INVALID	-1
16152506Simp	int32_t		product;
16252506Simp#define	PCCARD_PRODUCT_INVALID		-1
16352506Simp	u_int16_t	error;
16452506Simp#define	PCCARD_CIS_INVALID		{ NULL, NULL, NULL, NULL }
16560938Sjake	STAILQ_HEAD(, pccard_function) pf_head;
16652506Simp};
16752506Simp
16859389Simp#define	PCCARD_MEM_ATTR		1
16959389Simp#define	PCCARD_MEM_COMMON	2
17059389Simp
17159389Simp#define	PCCARD_WIDTH_AUTO	0
17259389Simp#define	PCCARD_WIDTH_IO8	1
17359389Simp#define	PCCARD_WIDTH_IO16	2
17459389Simp
17553873Simp/* More later? */
17653873Simpstruct pccard_ivar {
17753873Simp	struct resource_list resources;
17865917Simp	struct pccard_function *fcn;
17952506Simp};
18052506Simp
18152506Simpstruct pccard_softc {
18255720Simp	device_t		dev;
18352506Simp	/* this stuff is for the socket */
18452506Simp
18552506Simp	/* this stuff is for the card */
18652506Simp	struct pccard_card card;
18755500Simp	int		sc_enabled_count;	/* num functions enabled */
18870715Sjon	int		intr_handler_count;
18952506Simp};
19052506Simp
19152506Simpvoid
19254250Simppccardbus_if_setup(struct pccard_softc*);
19352506Simp
19452506Simpstruct pccard_cis_quirk {
19552506Simp	int32_t manufacturer;
19652506Simp	int32_t product;
19752506Simp	char *cis1_info[4];
19852506Simp	struct pccard_function *pf;
19952506Simp	struct pccard_config_entry *cfe;
20052506Simp};
20152506Simp
20252506Simpstruct pccard_tuple {
20352506Simp	unsigned int	code;
20452506Simp	unsigned int	length;
20552506Simp	u_long		mult;
20652506Simp	bus_addr_t	ptr;
20752506Simp	bus_space_tag_t	memt;
20852506Simp	bus_space_handle_t memh;
20952506Simp};
21052506Simp
21166200Simpstruct pccard_product {
21266200Simp	const char	*pp_name;		/* NULL if end of table */
21366200Simp#define PCCARD_VENDOR_ANY ((u_int32_t) -1)
21466200Simp	u_int32_t	pp_vendor;
21566200Simp#define PCCARD_PRODUCT_ANY ((u_int32_t) -1)
21666200Simp	u_int32_t	pp_product;
21766200Simp	int		pp_expfunc;
21871322Simp	const char	*pp_cis[4];
21966200Simp};
22066200Simp
22166200Simptypedef int (*pccard_product_match_fn) (device_t dev,
22266200Simp    const struct pccard_product *ent, int vpfmatch);
22366200Simp
22466200Simpconst struct pccard_product
22566200Simp	*pccard_product_lookup(device_t dev, const struct pccard_product *tab,
22666200Simp	    size_t ent_size, pccard_product_match_fn matchfn);
22766200Simp
22854250Simpvoid	pccard_read_cis(struct pccard_softc *);
22952506Simpvoid	pccard_check_cis_quirks(device_t);
23052506Simpvoid	pccard_print_cis(device_t);
23159193Simpint	pccard_scan_cis(device_t,
23259193Simp		int (*) (struct pccard_tuple *, void *), void *);
23352506Simp
23452506Simp#define	pccard_cis_read_1(tuple, idx0)					\
23552506Simp	(bus_space_read_1((tuple)->memt, (tuple)->memh, (tuple)->mult*(idx0)))
23652506Simp
23752506Simp#define	pccard_tuple_read_1(tuple, idx1)				\
23852506Simp	(pccard_cis_read_1((tuple), ((tuple)->ptr+(2+(idx1)))))
23952506Simp
24052506Simp#define	pccard_tuple_read_2(tuple, idx2)				\
24152506Simp	(pccard_tuple_read_1((tuple), (idx2)) | 			\
24252506Simp	 (pccard_tuple_read_1((tuple), (idx2)+1)<<8))
24352506Simp
24452506Simp#define	pccard_tuple_read_3(tuple, idx3)				\
24552506Simp	(pccard_tuple_read_1((tuple), (idx3)) |				\
24652506Simp	 (pccard_tuple_read_1((tuple), (idx3)+1)<<8) |			\
24752506Simp	 (pccard_tuple_read_1((tuple), (idx3)+2)<<16))
24852506Simp
24952506Simp#define	pccard_tuple_read_4(tuple, idx4)				\
25052506Simp	(pccard_tuple_read_1((tuple), (idx4)) |				\
25152506Simp	 (pccard_tuple_read_1((tuple), (idx4)+1)<<8) |			\
25252506Simp	 (pccard_tuple_read_1((tuple), (idx4)+2)<<16) |			\
25352506Simp	 (pccard_tuple_read_1((tuple), (idx4)+3)<<24))
25452506Simp
25552506Simp#define	pccard_tuple_read_n(tuple, n, idxn)				\
25652506Simp	(((n)==1)?pccard_tuple_read_1((tuple), (idxn)) :		\
25752506Simp	 (((n)==2)?pccard_tuple_read_2((tuple), (idxn)) :		\
25852506Simp	  (((n)==3)?pccard_tuple_read_3((tuple), (idxn)) :		\
25952506Simp	   /* n == 4 */ pccard_tuple_read_4((tuple), (idxn)))))
26052506Simp
26152506Simp#define	PCCARD_SPACE_MEMORY	1
26252506Simp#define	PCCARD_SPACE_IO		2
26352506Simp
26454250Simpint	pccard_ccr_read(struct pccard_function *, int);
26554250Simpvoid	pccard_ccr_write(struct pccard_function *, int, int);
26652506Simp
26752506Simp#define	pccard_mfc(sc)	(STAILQ_FIRST(&(sc)->card.pf_head) &&		\
26852506Simp		 STAILQ_NEXT(STAILQ_FIRST(&(sc)->card.pf_head),pf_list))
26952506Simp
27064850Simp/* The following is the vestages of the NetBSD driver api */
27164850Simp
27265917Simpvoid	pccard_function_init(struct pccard_function *);
27354250Simpint	pccard_function_enable(struct pccard_function *);
27454250Simpvoid	pccard_function_disable(struct pccard_function *);
27552506Simp
27652506Simp#define	pccard_io_alloc(pf, start, size, align, pciop)			\
27752506Simp	(pccard_chip_io_alloc((pf)->sc->pct, pf->sc->pch, (start),	\
27852506Simp	 (size), (align), (pciop)))
27952506Simp
28052506Simp#define	pccard_io_free(pf, pciohp)					\
28152506Simp	(pccard_chip_io_free((pf)->sc->pct, (pf)->sc->pch, (pciohp)))
28252506Simp
28354250Simpint	pccard_io_map(struct pccard_function *, int, bus_addr_t,
28454250Simp	    bus_size_t, struct pccard_io_handle *, int *);
28554250Simpvoid	pccard_io_unmap(struct pccard_function *, int);
28652506Simp
28752506Simp#define pccard_mem_alloc(pf, size, pcmhp)				\
28852506Simp	(pccard_chip_mem_alloc((pf)->sc->pct, (pf)->sc->pch, (size), (pcmhp)))
28952506Simp#define pccard_mem_free(pf, pcmhp)					\
29052506Simp	(pccard_chip_mem_free((pf)->sc->pct, (pf)->sc->pch, (pcmhp)))
29152506Simp#define pccard_mem_map(pf, kind, card_addr, size, pcmhp, offsetp, windowp) \
29252506Simp	(pccard_chip_mem_map((pf)->sc->pct, (pf)->sc->pch, (kind),	\
29352506Simp	 (card_addr), (size), (pcmhp), (offsetp), (windowp)))
29452506Simp#define	pccard_mem_unmap(pf, window)					\
29552506Simp	(pccard_chip_mem_unmap((pf)->sc->pct, (pf)->sc->pch, (window)))
29656361Shosokawa
29766058Simp/* compat layer */
29874636Simpstatic __inline int
29974636Simppccard_compat_probe(device_t dev)
30074636Simp{
30174636Simp	return (CARD_COMPAT_DO_PROBE(device_get_parent(dev), dev));
30274636Simp}
30366058Simp
30474636Simpstatic __inline int
30574636Simppccard_compat_attach(device_t dev)
30674636Simp{
30774636Simp	return (CARD_COMPAT_DO_ATTACH(device_get_parent(dev), dev));
30874636Simp}
30974636Simp
31056361Shosokawa/* ivar interface */
31156361Shosokawaenum {
31256361Shosokawa	PCCARD_IVAR_ETHADDR,	/* read ethernet address from CIS tupple */
31366200Simp	PCCARD_IVAR_VENDOR,
31466200Simp	PCCARD_IVAR_PRODUCT,
31566200Simp	PCCARD_IVAR_FUNCTION_NUMBER,
31666200Simp	PCCARD_IVAR_VENDOR_STR,	/* CIS string for "Manufacturer" */
31766200Simp	PCCARD_IVAR_PRODUCT_STR,/* CIS strnig for "Product" */
31867167Simp	PCCARD_IVAR_CIS3_STR,
31975761Simp	PCCARD_IVAR_CIS4_STR,
32075761Simp	PCCARD_IVAR_FUNCTION
32156361Shosokawa};
32256361Shosokawa
32366200Simp#define PCCARD_ACCESSOR(A, B, T)					\
32466200Simp__inline static int							\
32566200Simppccard_get_ ## A(device_t dev, T *t)					\
32666200Simp{									\
32766200Simp	return BUS_READ_IVAR(device_get_parent(dev), dev, 		\
32866200Simp	    PCCARD_IVAR_ ## B, (uintptr_t *) t);			\
32956361Shosokawa}
33058581Simp
33166200SimpPCCARD_ACCESSOR(ether,		ETHADDR,		u_int8_t)
33266200SimpPCCARD_ACCESSOR(vendor,		VENDOR,			u_int32_t)
33366200SimpPCCARD_ACCESSOR(product,	PRODUCT,		u_int32_t)
33466200SimpPCCARD_ACCESSOR(function_number,FUNCTION_NUMBER,	u_int32_t)
33575761SimpPCCARD_ACCESSOR(function,	FUNCTION,		u_int32_t)
33666200SimpPCCARD_ACCESSOR(vendor_str,	VENDOR_STR,		char *)
33766200SimpPCCARD_ACCESSOR(product_str,	PRODUCT_STR,		char *)
33866200SimpPCCARD_ACCESSOR(cis3_str,	CIS3_STR,		char *)
33966200Simp
34058581Simpenum {
34159193Simp	PCCARD_A_MEM_ATTR = 0x1
34258581Simp};
34358581Simp
34464850Simp#define PCCARD_SOFTC(d) (struct pccard_softc *) device_get_softc(d)
34566847Simp#define PCCARD_IVAR(d) (struct pccard_ivar *) device_get_ivars(d)
346