Home | History | Annotate | Line # | Download | only in maple
maple.c revision 1.16
      1  1.16  thorpej /*	$NetBSD: maple.c,v 1.16 2002/09/27 03:17:47 thorpej Exp $	*/
      2   1.1   marcus 
      3   1.3   marcus /*-
      4   1.1   marcus  * Copyright (c) 2001 Marcus Comstedt
      5   1.1   marcus  * All rights reserved.
      6   1.1   marcus  *
      7   1.1   marcus  * Redistribution and use in source and binary forms, with or without
      8   1.1   marcus  * modification, are permitted provided that the following conditions
      9   1.1   marcus  * are met:
     10   1.1   marcus  * 1. Redistributions of source code must retain the above copyright
     11   1.1   marcus  *    notice, this list of conditions and the following disclaimer.
     12   1.1   marcus  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   marcus  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   marcus  *    documentation and/or other materials provided with the distribution.
     15   1.1   marcus  * 3. All advertising materials mentioning features or use of this software
     16   1.1   marcus  *    must display the following acknowledgement:
     17   1.3   marcus  *	This product includes software developed by Marcus Comstedt.
     18   1.3   marcus  * 4. Neither the name of The NetBSD Foundation nor the names of its
     19   1.3   marcus  *    contributors may be used to endorse or promote products derived
     20   1.3   marcus  *    from this software without specific prior written permission.
     21   1.1   marcus  *
     22   1.3   marcus  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23   1.3   marcus  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24   1.3   marcus  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25   1.3   marcus  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26   1.3   marcus  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27   1.3   marcus  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28   1.3   marcus  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29   1.3   marcus  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30   1.3   marcus  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31   1.3   marcus  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32   1.3   marcus  * POSSIBILITY OF SUCH DAMAGE.
     33   1.1   marcus  */
     34   1.1   marcus 
     35   1.1   marcus #include <sys/param.h>
     36   1.1   marcus #include <sys/device.h>
     37   1.1   marcus #include <sys/fcntl.h>
     38   1.1   marcus #include <sys/poll.h>
     39   1.1   marcus #include <sys/select.h>
     40   1.1   marcus #include <sys/proc.h>
     41   1.1   marcus #include <sys/signalvar.h>
     42   1.1   marcus #include <sys/systm.h>
     43  1.14  gehenna #include <sys/conf.h>
     44   1.1   marcus 
     45   1.1   marcus #include <uvm/uvm_extern.h>
     46   1.1   marcus 
     47   1.1   marcus #include <machine/cpu.h>
     48   1.1   marcus #include <machine/bus.h>
     49   1.1   marcus #include <sh3/pmap.h>
     50   1.1   marcus 
     51   1.1   marcus #include <dreamcast/dev/maple/maple.h>
     52   1.1   marcus #include <dreamcast/dev/maple/mapleconf.h>
     53   1.1   marcus #include <dreamcast/dev/maple/maplevar.h>
     54   1.1   marcus #include <dreamcast/dev/maple/maplereg.h>
     55   1.7   marcus #include <dreamcast/dev/maple/mapleio.h>
     56   1.1   marcus 
     57  1.11      uch #include "locators.h"
     58   1.1   marcus 
     59   1.7   marcus /* Internal macros, functions, and variables. */
     60   1.7   marcus 
     61   1.5   marcus #define MAPLE_CALLOUT_TICKS 2
     62   1.1   marcus 
     63   1.7   marcus #define MAPLEBUSUNIT(dev)  (minor(dev)>>5)
     64   1.7   marcus #define MAPLEPORT(dev)     ((minor(dev) & 0x18) >> 3)
     65   1.7   marcus #define MAPLESUBUNIT(dev)  (minor(dev) & 0x7)
     66   1.7   marcus 
     67   1.1   marcus /*
     68   1.1   marcus  * Function declarations.
     69   1.1   marcus  */
     70  1.12      uch static int	maplematch(struct device *, struct cfdata *, void *);
     71  1.12      uch static void	mapleattach(struct device *, struct device *, void *);
     72  1.12      uch static int	mapleprint(void *, const char *);
     73  1.12      uch static int	maplesubmatch(struct device *, struct cfdata *, void *);
     74  1.12      uch static void	maple_attach_dev(struct maple_softc *, int, int);
     75  1.12      uch static void	maple_begin_txbuf(struct maple_softc *);
     76  1.12      uch static int	maple_end_txbuf(struct maple_softc *);
     77  1.12      uch static void	maple_write_command(struct maple_softc *, int, int,
     78  1.12      uch 		    int, int, void *);
     79  1.12      uch static void	maple_scanbus(struct maple_softc *);
     80  1.12      uch static void	maple_callout(void *);
     81  1.12      uch static void	maple_send_commands(struct maple_softc *);
     82  1.12      uch static void	maple_check_responses(struct maple_softc *);
     83  1.12      uch 
     84  1.12      uch int	maple_alloc_dma(size_t, vaddr_t *, paddr_t *);
     85  1.12      uch void	maple_free_dma(paddr_t, size_t);
     86  1.12      uch 
     87  1.12      uch int	maple_internal_ioctl(struct maple_softc *,  int, int, u_long, caddr_t,
     88  1.12      uch 	    int, struct proc *);
     89   1.1   marcus 
     90   1.1   marcus /*
     91   1.1   marcus  * Global variables.
     92   1.1   marcus  */
     93   1.1   marcus int	maple_polling = 0;	/* Are we polling?  (Debugger mode) */
     94   1.1   marcus 
     95   1.1   marcus /*
     96   1.1   marcus  * Driver definition.
     97   1.1   marcus  */
     98   1.1   marcus struct cfattach maple_ca = {
     99   1.1   marcus 	sizeof(struct maple_softc), maplematch, mapleattach
    100   1.1   marcus };
    101   1.1   marcus 
    102   1.7   marcus extern struct cfdriver maple_cd;
    103  1.14  gehenna 
    104  1.14  gehenna dev_type_open(mapleopen);
    105  1.14  gehenna dev_type_close(mapleclose);
    106  1.14  gehenna dev_type_ioctl(mapleioctl);
    107  1.14  gehenna 
    108  1.14  gehenna const struct cdevsw maple_cdevsw = {
    109  1.14  gehenna 	mapleopen, mapleclose, noread, nowrite, mapleioctl,
    110  1.14  gehenna 	nostop, notty, nopoll, nommap,
    111  1.14  gehenna };
    112   1.7   marcus 
    113   1.1   marcus static int
    114  1.12      uch maplematch(struct device *parent, struct cfdata *cf, void *aux)
    115   1.1   marcus {
    116   1.1   marcus 
    117   1.1   marcus 	return (1);
    118   1.1   marcus }
    119   1.1   marcus 
    120   1.1   marcus static void
    121  1.12      uch maple_attach_dev(struct maple_softc *sc, int port, int subunit)
    122   1.1   marcus {
    123   1.1   marcus 	struct maple_attach_args ma;
    124   1.7   marcus 	u_int32_t func;
    125   1.7   marcus 	int f;
    126   1.7   marcus 	char oldxname[16];
    127   1.7   marcus 
    128   1.1   marcus 	ma.ma_port = port;
    129   1.1   marcus 	ma.ma_subunit = subunit;
    130   1.1   marcus 	ma.ma_devinfo = &sc->sc_unit[port][subunit].devinfo;
    131   1.7   marcus 	ma.ma_function = 1;
    132   1.7   marcus 	func = ma.ma_devinfo->di_func;
    133   1.4  thorpej 
    134   1.7   marcus 	mapleprint(&ma, sc->sc_dev.dv_xname);
    135   1.7   marcus 	printf("\n");
    136   1.7   marcus 	strcpy(oldxname, sc->sc_dev.dv_xname);
    137   1.7   marcus 	sprintf(sc->sc_dev.dv_xname, "maple%c", port+'A');
    138  1.12      uch 	if (subunit)
    139  1.12      uch 		sprintf(sc->sc_dev.dv_xname+6, "%d", subunit);
    140  1.12      uch 
    141  1.12      uch 	for (f = 0; f < 32; f++, ma.ma_function <<= 1)
    142  1.12      uch 		if (func & ma.ma_function)
    143  1.12      uch 			(void)config_found_sm(&sc->sc_dev, &ma,
    144  1.12      uch 			    NULL, maplesubmatch);
    145   1.7   marcus 	strcpy(sc->sc_dev.dv_xname, oldxname);
    146   1.1   marcus }
    147   1.1   marcus 
    148   1.1   marcus static void
    149  1.12      uch maple_begin_txbuf(struct maple_softc *sc)
    150   1.1   marcus {
    151  1.12      uch 
    152   1.1   marcus 	sc->sc_txlink = sc->sc_txpos = sc->sc_txbuf;
    153   1.1   marcus }
    154   1.1   marcus 
    155   1.1   marcus static int
    156  1.12      uch maple_end_txbuf(struct maple_softc *sc)
    157   1.1   marcus {
    158   1.1   marcus 	/* if no frame have been written, we can't mark the
    159   1.1   marcus 	   list end, and so the DMA must not be activated   */
    160   1.1   marcus   	if (sc->sc_txpos == sc->sc_txbuf)
    161  1.12      uch 		return (0);
    162   1.1   marcus 
    163   1.1   marcus 	*sc->sc_txlink |= 0x80000000;
    164   1.1   marcus 
    165   1.1   marcus 	return (1);
    166   1.1   marcus }
    167   1.1   marcus 
    168   1.1   marcus static int8_t subunit_code[] = { 0x20, 0x01, 0x02, 0x04, 0x08, 0x10 };
    169   1.1   marcus 
    170   1.1   marcus static void
    171  1.12      uch maple_write_command(struct maple_softc *sc, int port, int subunit, int command,
    172  1.12      uch     int datalen, void *dataaddr)
    173   1.1   marcus {
    174   1.1   marcus 	int to, from;
    175   1.1   marcus 	u_int32_t *p = sc->sc_txpos;
    176   1.1   marcus 
    177   1.1   marcus 	if ((port & ~(MAPLE_PORTS-1)) != 0 ||
    178   1.1   marcus 	    subunit < 0 || subunit >= MAPLE_SUBUNITS)
    179  1.12      uch 		return;
    180   1.1   marcus 
    181   1.1   marcus 	/* Compute sender and recipient address */
    182   1.1   marcus 	from = port << 6;
    183   1.1   marcus 	to = from | subunit_code[subunit];
    184   1.1   marcus 
    185   1.1   marcus 	/* Max data length = 255 longs = 1020 bytes */
    186  1.12      uch 	if (datalen > 255)
    187  1.12      uch 		datalen = 255;
    188  1.12      uch 	else if (datalen < 0)
    189  1.12      uch 		datalen = 0;
    190   1.1   marcus 
    191   1.1   marcus 	sc->sc_txlink = p;
    192   1.1   marcus 
    193   1.1   marcus 	/* Set length of packet and destination port (A-D) */
    194   1.1   marcus 	*p++ = datalen | (port << 16);
    195   1.1   marcus 
    196   1.1   marcus 	/* Write address to receive buffer where the response
    197   1.1   marcus 	   frame should be put */
    198   1.1   marcus 	*p++ = sc->sc_rxbuf_phys[port][subunit];
    199   1.1   marcus 
    200   1.1   marcus 	/* Create the frame header.  The fields are assembled "backwards"
    201   1.1   marcus 	   because of the Maple Bus big-endianness.                       */
    202   1.1   marcus 	*p++ = (command & 0xff) | (to << 8) | (from << 16) | (datalen << 24);
    203   1.1   marcus 
    204   1.1   marcus 	/* Copy parameter data, if any */
    205   1.1   marcus 	if (datalen > 0) {
    206  1.12      uch 		u_int32_t *param = dataaddr;
    207  1.12      uch 		int i;
    208  1.12      uch 		for (i = 0; i < datalen; i++)
    209  1.12      uch 			*p++ = *param++;
    210   1.1   marcus 	}
    211   1.1   marcus 
    212   1.1   marcus 	sc->sc_txpos = p;
    213   1.1   marcus }
    214   1.1   marcus 
    215   1.1   marcus static void
    216  1.12      uch maple_scanbus(struct maple_softc *sc)
    217   1.1   marcus {
    218   1.5   marcus 	int p, s;
    219   1.1   marcus 
    220   1.1   marcus 	maple_polling = 1;
    221   1.1   marcus 
    222   1.1   marcus 	maple_begin_txbuf(sc);
    223   1.1   marcus 
    224   1.1   marcus 	for (p = 0; p < MAPLE_PORTS; p++) {
    225  1.12      uch 		maple_write_command(sc, p, 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
    226   1.1   marcus 	}
    227   1.1   marcus 
    228   1.1   marcus 	if (maple_end_txbuf(sc)) {
    229   1.1   marcus 
    230  1.12      uch 		MAPLE_DMAADDR = sc->sc_txbuf_phys;
    231  1.12      uch 		MAPLE_STATE = 1;
    232  1.12      uch 		while (MAPLE_STATE != 0)
    233  1.12      uch 			;
    234  1.12      uch 
    235  1.12      uch 		for (p = 0; p < MAPLE_PORTS; p++)
    236  1.12      uch 			if ((sc->sc_rxbuf[p][0][0] & 0xff) ==
    237  1.12      uch 			    MAPLE_RESPONSE_DEVINFO)
    238  1.12      uch 				sc->sc_port_units[p] =
    239  1.12      uch 				    ((sc->sc_rxbuf[p][0][0] >> 15) & 0x3e) | 1;
    240  1.12      uch 			else
    241  1.12      uch 				sc->sc_port_units[p] = 0;
    242  1.12      uch 
    243  1.12      uch 
    244  1.12      uch 		maple_begin_txbuf(sc);
    245  1.12      uch 
    246  1.12      uch 		for (p = 0; p < MAPLE_PORTS; p++) {
    247  1.12      uch 			for (s = 0; s < MAPLE_SUBUNITS; s++) {
    248  1.12      uch 				if (sc->sc_port_units[p] & (1 << s))
    249  1.12      uch 					maple_write_command(sc, p, s,
    250  1.12      uch 					    MAPLE_COMMAND_DEVINFO, 0, NULL);
    251  1.12      uch 			}
    252  1.12      uch 		}
    253   1.1   marcus 
    254  1.12      uch 		if (maple_end_txbuf(sc)) {
    255  1.12      uch 			MAPLE_DMAADDR = sc->sc_txbuf_phys;
    256  1.12      uch 			MAPLE_STATE = 1;
    257  1.12      uch 			while (MAPLE_STATE != 0)
    258  1.12      uch 				;
    259  1.12      uch 
    260  1.12      uch 			for (p = 0; p < MAPLE_PORTS; p++)
    261  1.12      uch 				for (s = 0; s < MAPLE_SUBUNITS; s++)
    262  1.12      uch 					if (sc->sc_port_units[p] & (1 << s)) {
    263  1.12      uch 
    264  1.12      uch 						if ((sc->sc_rxbuf[p][s][0] &
    265  1.12      uch 						    0xff) ==
    266  1.12      uch 						    MAPLE_RESPONSE_DEVINFO) {
    267   1.1   marcus 
    268  1.12      uch 							u_int32_t *to_swap;
    269  1.12      uch 							int i;
    270   1.5   marcus 
    271  1.12      uch 							memcpy((to_swap =
    272  1.12      uch 							    &sc->sc_unit[p][s].
    273  1.12      uch 							    devinfo.di_func),
    274  1.12      uch 							    sc->sc_rxbuf[p][s] +
    275  1.12      uch 							    1,
    276  1.12      uch 							    sizeof(struct
    277  1.12      uch 								maple_devinfo));
    278   1.5   marcus 
    279  1.12      uch 							for (i = 0; i < 4; i++,
    280  1.12      uch 							    to_swap++)
    281  1.12      uch 								*to_swap =
    282  1.12      uch 								    ntohl
    283  1.12      uch 								    (*to_swap);
    284  1.12      uch 							maple_attach_dev(sc, p,
    285  1.12      uch 							    s);
    286  1.12      uch 						} else {
    287  1.12      uch 
    288  1.12      uch 							printf("%s: no response"
    289  1.12      uch 							    " from port %d "
    290  1.12      uch 							    " subunit %d\n",
    291  1.12      uch 							    sc->sc_dev.dv_xname,
    292  1.12      uch 							    p, s);
    293  1.12      uch 							sc->sc_port_units[p] &=
    294  1.12      uch 							    ~(1 << s);
    295  1.12      uch 						}
    296  1.12      uch 					}
    297  1.12      uch 
    298   1.5   marcus 		}
    299   1.1   marcus 
    300   1.1   marcus 	}
    301   1.1   marcus 
    302   1.1   marcus 	maple_polling = 0;
    303   1.1   marcus 
    304   1.1   marcus }
    305   1.1   marcus 
    306   1.1   marcus static void
    307  1.12      uch maple_send_commands(struct maple_softc *sc)
    308   1.1   marcus {
    309   1.5   marcus 	int p, s;
    310   1.1   marcus 
    311   1.1   marcus 	if (sc->maple_commands_pending || MAPLE_STATE != 0)
    312  1.12      uch 		return;
    313   1.1   marcus 
    314   1.1   marcus 	maple_begin_txbuf(sc);
    315   1.1   marcus 
    316   1.1   marcus 	for (p = 0; p < MAPLE_PORTS; p++) {
    317  1.12      uch 		for (s = 0; s < MAPLE_SUBUNITS; s++) {
    318  1.12      uch 			if (sc->sc_unit[p][s].getcond_callback != NULL) {
    319  1.12      uch 				u_int32_t func =
    320  1.12      uch 				    ntohl(sc->sc_unit[p][s].getcond_func);
    321   1.5   marcus 
    322  1.12      uch 				maple_write_command(sc, p, s,
    323  1.12      uch 				    MAPLE_COMMAND_GETCOND, 1, &func);
    324  1.12      uch 			}
    325  1.12      uch 		}
    326   1.1   marcus 	}
    327   1.1   marcus 
    328   1.1   marcus 	if (!maple_end_txbuf(sc))
    329  1.12      uch 		return;
    330   1.1   marcus 
    331   1.1   marcus 	MAPLE_DMAADDR = sc->sc_txbuf_phys;
    332   1.1   marcus 	MAPLE_STATE = 1;
    333   1.1   marcus 
    334   1.1   marcus 	sc->maple_commands_pending = 1;
    335   1.1   marcus }
    336   1.1   marcus 
    337   1.1   marcus static void
    338  1.12      uch maple_check_responses(struct maple_softc *sc)
    339   1.1   marcus {
    340   1.5   marcus 	int p, s;
    341   1.1   marcus 
    342   1.1   marcus 	if (!sc->maple_commands_pending || MAPLE_STATE != 0)
    343  1.12      uch 		return;
    344   1.1   marcus 
    345   1.1   marcus 	for (p = 0; p < MAPLE_PORTS; p++) {
    346  1.12      uch 		for (s = 0; s < MAPLE_SUBUNITS; s++) {
    347  1.12      uch 			struct maple_unit *u = &sc->sc_unit[p][s];
    348  1.12      uch 			if (u->getcond_callback != NULL &&
    349  1.12      uch 			    (sc->sc_rxbuf[p][s][0] & 0xff) ==
    350  1.12      uch 			    MAPLE_RESPONSE_DATATRF &&
    351  1.12      uch 			    (sc->sc_rxbuf[p][s][0] >> 24) >= 1 &&
    352  1.12      uch 			    htonl(sc->sc_rxbuf[p][s][1]) == u->getcond_func) {
    353  1.12      uch 				(*u->getcond_callback)(u->getcond_data,
    354  1.12      uch 				    (void *)(sc->sc_rxbuf[p][s] + 2),
    355  1.12      uch 				    ((sc->sc_rxbuf[p][s][0] >> 22) & 1020) - 4);
    356  1.12      uch 			}
    357  1.12      uch 		}
    358   1.1   marcus 	}
    359   1.1   marcus 
    360   1.1   marcus 	sc->maple_commands_pending = 0;
    361   1.1   marcus }
    362   1.1   marcus 
    363   1.1   marcus void
    364  1.12      uch maple_run_polling(struct device *dev)
    365   1.1   marcus {
    366   1.1   marcus 	struct maple_softc *sc;
    367   1.1   marcus 
    368   1.1   marcus 	sc = (struct maple_softc *)dev;
    369   1.1   marcus 
    370   1.1   marcus 	if (MAPLE_STATE != 0)
    371  1.12      uch 		return;
    372   1.1   marcus 
    373   1.1   marcus 	maple_send_commands(sc);
    374   1.1   marcus 
    375   1.1   marcus 	while (MAPLE_STATE != 0)
    376  1.12      uch 		;
    377   1.1   marcus 
    378   1.1   marcus 	maple_check_responses(sc);
    379   1.1   marcus }
    380   1.1   marcus 
    381   1.1   marcus void
    382  1.12      uch maple_set_condition_callback(struct device *dev, int port, int subunit,
    383  1.12      uch     u_int32_t func, void (*callback)(void *, void *, int), void *data)
    384   1.1   marcus {
    385   1.1   marcus 	struct maple_softc *sc;
    386   1.1   marcus 
    387   1.1   marcus 	sc = (struct maple_softc *)dev;
    388   1.1   marcus 
    389   1.1   marcus 	if ((port & ~(MAPLE_PORTS-1)) != 0 ||
    390   1.1   marcus 	    subunit < 0 || subunit >= MAPLE_SUBUNITS)
    391  1.12      uch 		return;
    392   1.1   marcus 
    393   1.1   marcus 	sc->sc_unit[port][subunit].getcond_func = func;
    394   1.1   marcus 	sc->sc_unit[port][subunit].getcond_callback = callback;
    395   1.1   marcus 	sc->sc_unit[port][subunit].getcond_data = data;
    396   1.1   marcus }
    397   1.1   marcus 
    398   1.1   marcus static void
    399  1.12      uch maple_callout(void *ctx)
    400   1.1   marcus {
    401   1.1   marcus 	struct maple_softc *sc = ctx;
    402   1.1   marcus 
    403  1.12      uch 	if (!maple_polling) {
    404  1.12      uch 		maple_check_responses(sc);
    405  1.12      uch 		maple_send_commands(sc);
    406   1.1   marcus 	}
    407   1.1   marcus 
    408   1.1   marcus 	callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS,
    409  1.12      uch 	    (void *)maple_callout, sc);
    410   1.1   marcus }
    411   1.1   marcus 
    412   1.2   marcus int
    413  1.12      uch maple_alloc_dma(size_t size, vaddr_t *vap, paddr_t *pap)
    414   1.2   marcus {
    415   1.2   marcus 	extern paddr_t avail_start, avail_end;	/* from pmap.c */
    416   1.2   marcus 	struct pglist mlist;
    417   1.8      chs 	struct vm_page *m;
    418   1.2   marcus 	int error;
    419   1.2   marcus 
    420   1.2   marcus 	size = round_page(size);
    421   1.2   marcus 
    422   1.2   marcus 	error = uvm_pglistalloc(size, avail_start, avail_end - PAGE_SIZE,
    423   1.2   marcus 	    0, 0, &mlist, 1, 0);
    424   1.2   marcus 	if (error)
    425   1.2   marcus 		return (error);
    426   1.2   marcus 
    427   1.2   marcus 	m = TAILQ_FIRST(&mlist);
    428   1.2   marcus 	*pap = VM_PAGE_TO_PHYS(m);
    429   1.2   marcus 	*vap = SH3_PHYS_TO_P2SEG(VM_PAGE_TO_PHYS(m));
    430   1.2   marcus 
    431   1.2   marcus 	return (0);
    432   1.2   marcus }
    433   1.2   marcus 
    434   1.2   marcus void
    435  1.12      uch maple_free_dma(paddr_t paddr, size_t size)
    436   1.2   marcus {
    437   1.2   marcus 	struct pglist mlist;
    438   1.8      chs 	struct vm_page *m;
    439   1.2   marcus 	bus_addr_t addr;
    440   1.2   marcus 
    441   1.2   marcus 	TAILQ_INIT(&mlist);
    442   1.2   marcus 	for (addr = paddr; addr < paddr + size; addr += PAGE_SIZE) {
    443   1.2   marcus 		m = PHYS_TO_VM_PAGE(addr);
    444   1.2   marcus 		TAILQ_INSERT_TAIL(&mlist, m, pageq);
    445   1.2   marcus 	}
    446   1.2   marcus 	uvm_pglistfree(&mlist);
    447   1.2   marcus }
    448   1.2   marcus 
    449   1.1   marcus static void
    450  1.12      uch mapleattach(struct device *parent, struct device *self, void *aux)
    451   1.1   marcus {
    452   1.1   marcus 	struct maple_softc *sc;
    453   1.1   marcus 	vaddr_t dmabuffer;
    454   1.2   marcus 	paddr_t dmabuffer_phys;
    455   1.1   marcus 	u_int32_t *p;
    456   1.1   marcus 	int i, j;
    457   1.1   marcus 
    458   1.1   marcus 	sc = (struct maple_softc *)self;
    459   1.1   marcus 
    460   1.1   marcus 	printf("\n");
    461   1.1   marcus 
    462   1.2   marcus 	if (maple_alloc_dma(MAPLE_DMABUF_SIZE, &dmabuffer, &dmabuffer_phys)) {
    463  1.12      uch 		printf("%s: unable to allocate DMA buffers.\n",
    464  1.12      uch 		    sc->sc_dev.dv_xname);
    465  1.12      uch 		return;
    466   1.2   marcus 	}
    467   1.1   marcus 
    468  1.12      uch 	p = (u_int32_t *)dmabuffer;
    469   1.1   marcus 
    470   1.1   marcus 	for (i = 0; i < MAPLE_PORTS; i++)
    471  1.12      uch 		for (j = 0; j < MAPLE_SUBUNITS; j++) {
    472  1.12      uch 			sc->sc_rxbuf[i][j] = p;
    473  1.12      uch 			sc->sc_rxbuf_phys[i][j] = SH3_P2SEG_TO_PHYS(p);
    474  1.12      uch 			p += 256;
    475  1.12      uch 		}
    476   1.1   marcus 
    477   1.1   marcus 	sc->sc_txbuf = p;
    478   1.1   marcus 	sc->sc_txbuf_phys = SH3_P2SEG_TO_PHYS(p);
    479   1.1   marcus 
    480   1.1   marcus 	sc->maple_commands_pending = 0;
    481   1.1   marcus 
    482   1.1   marcus 	MAPLE_RESET = RESET_MAGIC;
    483   1.1   marcus 	MAPLE_RESET2 = 0;
    484   1.1   marcus 
    485   1.1   marcus 	MAPLE_SPEED = SPEED_2MBPS | TIMEOUT(50000);
    486   1.1   marcus 
    487   1.1   marcus 	MAPLE_ENABLE = 1;
    488   1.1   marcus 
    489   1.1   marcus 	maple_scanbus(sc);
    490   1.1   marcus 
    491   1.9      wiz 	memset(&sc->maple_callout_ch, 0, sizeof(sc->maple_callout_ch));
    492   1.1   marcus 
    493   1.1   marcus 	callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS,
    494  1.12      uch 	    (void *)maple_callout, sc);
    495   1.1   marcus }
    496   1.1   marcus 
    497   1.1   marcus int
    498  1.12      uch mapleprint(void *aux, const char *pnp)
    499   1.1   marcus {
    500   1.1   marcus 	struct maple_attach_args *ma = aux;
    501   1.1   marcus 
    502   1.7   marcus 	if (pnp != NULL) {
    503   1.7   marcus 		printf("maple%c", 'A'+ma->ma_port);
    504   1.7   marcus 		if (ma->ma_subunit != 0)
    505   1.7   marcus 			printf("%d", ma->ma_subunit);
    506   1.7   marcus 		printf(" at %s", pnp);
    507   1.7   marcus 	}
    508   1.1   marcus 
    509   1.4  thorpej 	printf(" port %d", ma->ma_port);
    510   1.1   marcus 
    511   1.1   marcus 	if (ma->ma_subunit != 0)
    512   1.4  thorpej 		printf(" subunit %d", ma->ma_subunit);
    513   1.1   marcus 
    514   1.7   marcus 	printf(": %.*s",
    515  1.12      uch 	    (int)sizeof(ma->ma_devinfo->di_product_name),
    516  1.12      uch 	    ma->ma_devinfo->di_product_name);
    517   1.7   marcus 
    518   1.7   marcus 	return (0);
    519   1.1   marcus }
    520   1.1   marcus 
    521   1.4  thorpej static int
    522  1.12      uch maplesubmatch(struct device *parent, struct cfdata *match, void *aux)
    523   1.1   marcus {
    524   1.4  thorpej 	struct maple_attach_args *ma = aux;
    525   1.1   marcus 
    526   1.4  thorpej 	if (match->cf_loc[MAPLECF_PORT] != MAPLECF_PORT_DEFAULT &&
    527   1.4  thorpej 	    match->cf_loc[MAPLECF_PORT] != ma->ma_port)
    528   1.4  thorpej 		return (0);
    529   1.4  thorpej 
    530   1.4  thorpej 	if (match->cf_loc[MAPLECF_SUBUNIT] != MAPLECF_SUBUNIT_DEFAULT &&
    531   1.4  thorpej 	    match->cf_loc[MAPLECF_SUBUNIT] != ma->ma_subunit)
    532   1.4  thorpej 		return (0);
    533   1.4  thorpej 
    534  1.16  thorpej 	return (config_match(parent, match, aux));
    535   1.1   marcus }
    536   1.1   marcus 
    537   1.1   marcus u_int32_t
    538  1.12      uch maple_get_function_data(struct maple_devinfo *devinfo, u_int32_t function_code)
    539   1.1   marcus {
    540   1.1   marcus 	int i, p = 0;
    541   1.1   marcus 
    542   1.1   marcus 	for (i = 31; i >= 0; --i)
    543  1.12      uch 		if (devinfo->di_func & (1U << i)) {
    544  1.12      uch 			if (function_code & (1U << i))
    545  1.12      uch 				return devinfo->di_function_data[p];
    546  1.12      uch 			else
    547  1.12      uch 				if (++p >= 3)
    548  1.12      uch 					break;
    549  1.12      uch 		}
    550  1.12      uch 
    551   1.1   marcus 	return (0);
    552   1.7   marcus }
    553   1.7   marcus 
    554   1.7   marcus /* Generic maple device interface */
    555   1.7   marcus 
    556   1.7   marcus int
    557  1.12      uch mapleopen(dev_t dev, int flag, int mode, struct proc *p)
    558   1.7   marcus {
    559   1.7   marcus 	struct maple_softc *sc;
    560   1.7   marcus 
    561   1.7   marcus 	sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
    562   1.7   marcus 	if (sc == NULL)			/* make sure it was attached */
    563   1.7   marcus 		return (ENXIO);
    564   1.7   marcus 
    565   1.7   marcus 	if (MAPLEPORT(dev) >= MAPLE_PORTS)
    566   1.7   marcus 		return (ENXIO);
    567   1.7   marcus 
    568   1.7   marcus 	if (MAPLESUBUNIT(dev) >= MAPLE_SUBUNITS)
    569   1.7   marcus 		return (ENXIO);
    570   1.7   marcus 
    571  1.12      uch 	if (!(sc->sc_port_units[MAPLEPORT(dev)] & (1 << MAPLESUBUNIT(dev))))
    572   1.7   marcus 		return (ENXIO);
    573   1.7   marcus 
    574  1.12      uch 	sc->sc_port_units_open[MAPLEPORT(dev)] |= 1 << MAPLESUBUNIT(dev);
    575   1.7   marcus 
    576   1.7   marcus 	return (0);
    577   1.7   marcus }
    578   1.7   marcus 
    579   1.7   marcus int
    580  1.12      uch mapleclose(dev_t dev, int flag, int mode, struct proc *p)
    581   1.7   marcus {
    582   1.7   marcus 	struct maple_softc *sc;
    583   1.7   marcus 
    584   1.7   marcus 	sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
    585   1.7   marcus 
    586  1.12      uch 	sc->sc_port_units_open[MAPLEPORT(dev)] &= ~(1 << MAPLESUBUNIT(dev));
    587   1.7   marcus 
    588   1.7   marcus 	return (0);
    589   1.7   marcus }
    590   1.7   marcus 
    591   1.7   marcus int
    592  1.12      uch maple_internal_ioctl(struct maple_softc *sc, int port, int subunit, u_long cmd,
    593  1.12      uch     caddr_t data, int flag, struct proc *p)
    594   1.7   marcus {
    595   1.7   marcus 	struct maple_unit *u = &sc->sc_unit[port][subunit];
    596   1.7   marcus 
    597  1.12      uch 	if (!(sc->sc_port_units[port] & (1 << subunit)))
    598   1.7   marcus 		return (ENXIO);
    599   1.7   marcus 
    600   1.7   marcus 	switch(cmd) {
    601   1.7   marcus 	case MAPLEIO_GDEVINFO:
    602   1.9      wiz 		memcpy(data, &u->devinfo, sizeof(struct maple_devinfo));
    603   1.7   marcus 		return (0);
    604   1.7   marcus 	default:
    605   1.7   marcus 		return (EINVAL);
    606   1.7   marcus 	}
    607   1.7   marcus }
    608   1.7   marcus 
    609   1.7   marcus int
    610  1.12      uch mapleioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    611   1.7   marcus {
    612   1.7   marcus 	struct maple_softc *sc;
    613   1.7   marcus 
    614   1.7   marcus 	sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
    615   1.7   marcus 
    616  1.12      uch 	return (maple_internal_ioctl(sc, MAPLEPORT(dev), MAPLESUBUNIT(dev),
    617  1.12      uch 	    cmd, data, flag, p));
    618   1.1   marcus }
    619