Home | History | Annotate | Line # | Download | only in maple
maple.c revision 1.14
      1  1.14  gehenna /*	$NetBSD: maple.c,v 1.14 2002/09/06 13:18:43 gehenna 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 	if (strcmp("maple", cf->cf_driver->cd_name))
    118  1.12      uch 		return (0);
    119   1.1   marcus 
    120   1.1   marcus 	return (1);
    121   1.1   marcus }
    122   1.1   marcus 
    123   1.1   marcus static void
    124  1.12      uch maple_attach_dev(struct maple_softc *sc, int port, int subunit)
    125   1.1   marcus {
    126   1.1   marcus 	struct maple_attach_args ma;
    127   1.7   marcus 	u_int32_t func;
    128   1.7   marcus 	int f;
    129   1.7   marcus 	char oldxname[16];
    130   1.7   marcus 
    131   1.1   marcus 	ma.ma_port = port;
    132   1.1   marcus 	ma.ma_subunit = subunit;
    133   1.1   marcus 	ma.ma_devinfo = &sc->sc_unit[port][subunit].devinfo;
    134   1.7   marcus 	ma.ma_function = 1;
    135   1.7   marcus 	func = ma.ma_devinfo->di_func;
    136   1.4  thorpej 
    137   1.7   marcus 	mapleprint(&ma, sc->sc_dev.dv_xname);
    138   1.7   marcus 	printf("\n");
    139   1.7   marcus 	strcpy(oldxname, sc->sc_dev.dv_xname);
    140   1.7   marcus 	sprintf(sc->sc_dev.dv_xname, "maple%c", port+'A');
    141  1.12      uch 	if (subunit)
    142  1.12      uch 		sprintf(sc->sc_dev.dv_xname+6, "%d", subunit);
    143  1.12      uch 
    144  1.12      uch 	for (f = 0; f < 32; f++, ma.ma_function <<= 1)
    145  1.12      uch 		if (func & ma.ma_function)
    146  1.12      uch 			(void)config_found_sm(&sc->sc_dev, &ma,
    147  1.12      uch 			    NULL, maplesubmatch);
    148   1.7   marcus 	strcpy(sc->sc_dev.dv_xname, oldxname);
    149   1.1   marcus }
    150   1.1   marcus 
    151   1.1   marcus static void
    152  1.12      uch maple_begin_txbuf(struct maple_softc *sc)
    153   1.1   marcus {
    154  1.12      uch 
    155   1.1   marcus 	sc->sc_txlink = sc->sc_txpos = sc->sc_txbuf;
    156   1.1   marcus }
    157   1.1   marcus 
    158   1.1   marcus static int
    159  1.12      uch maple_end_txbuf(struct maple_softc *sc)
    160   1.1   marcus {
    161   1.1   marcus 	/* if no frame have been written, we can't mark the
    162   1.1   marcus 	   list end, and so the DMA must not be activated   */
    163   1.1   marcus   	if (sc->sc_txpos == sc->sc_txbuf)
    164  1.12      uch 		return (0);
    165   1.1   marcus 
    166   1.1   marcus 	*sc->sc_txlink |= 0x80000000;
    167   1.1   marcus 
    168   1.1   marcus 	return (1);
    169   1.1   marcus }
    170   1.1   marcus 
    171   1.1   marcus static int8_t subunit_code[] = { 0x20, 0x01, 0x02, 0x04, 0x08, 0x10 };
    172   1.1   marcus 
    173   1.1   marcus static void
    174  1.12      uch maple_write_command(struct maple_softc *sc, int port, int subunit, int command,
    175  1.12      uch     int datalen, void *dataaddr)
    176   1.1   marcus {
    177   1.1   marcus 	int to, from;
    178   1.1   marcus 	u_int32_t *p = sc->sc_txpos;
    179   1.1   marcus 
    180   1.1   marcus 	if ((port & ~(MAPLE_PORTS-1)) != 0 ||
    181   1.1   marcus 	    subunit < 0 || subunit >= MAPLE_SUBUNITS)
    182  1.12      uch 		return;
    183   1.1   marcus 
    184   1.1   marcus 	/* Compute sender and recipient address */
    185   1.1   marcus 	from = port << 6;
    186   1.1   marcus 	to = from | subunit_code[subunit];
    187   1.1   marcus 
    188   1.1   marcus 	/* Max data length = 255 longs = 1020 bytes */
    189  1.12      uch 	if (datalen > 255)
    190  1.12      uch 		datalen = 255;
    191  1.12      uch 	else if (datalen < 0)
    192  1.12      uch 		datalen = 0;
    193   1.1   marcus 
    194   1.1   marcus 	sc->sc_txlink = p;
    195   1.1   marcus 
    196   1.1   marcus 	/* Set length of packet and destination port (A-D) */
    197   1.1   marcus 	*p++ = datalen | (port << 16);
    198   1.1   marcus 
    199   1.1   marcus 	/* Write address to receive buffer where the response
    200   1.1   marcus 	   frame should be put */
    201   1.1   marcus 	*p++ = sc->sc_rxbuf_phys[port][subunit];
    202   1.1   marcus 
    203   1.1   marcus 	/* Create the frame header.  The fields are assembled "backwards"
    204   1.1   marcus 	   because of the Maple Bus big-endianness.                       */
    205   1.1   marcus 	*p++ = (command & 0xff) | (to << 8) | (from << 16) | (datalen << 24);
    206   1.1   marcus 
    207   1.1   marcus 	/* Copy parameter data, if any */
    208   1.1   marcus 	if (datalen > 0) {
    209  1.12      uch 		u_int32_t *param = dataaddr;
    210  1.12      uch 		int i;
    211  1.12      uch 		for (i = 0; i < datalen; i++)
    212  1.12      uch 			*p++ = *param++;
    213   1.1   marcus 	}
    214   1.1   marcus 
    215   1.1   marcus 	sc->sc_txpos = p;
    216   1.1   marcus }
    217   1.1   marcus 
    218   1.1   marcus static void
    219  1.12      uch maple_scanbus(struct maple_softc *sc)
    220   1.1   marcus {
    221   1.5   marcus 	int p, s;
    222   1.1   marcus 
    223   1.1   marcus 	maple_polling = 1;
    224   1.1   marcus 
    225   1.1   marcus 	maple_begin_txbuf(sc);
    226   1.1   marcus 
    227   1.1   marcus 	for (p = 0; p < MAPLE_PORTS; p++) {
    228  1.12      uch 		maple_write_command(sc, p, 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
    229   1.1   marcus 	}
    230   1.1   marcus 
    231   1.1   marcus 	if (maple_end_txbuf(sc)) {
    232   1.1   marcus 
    233  1.12      uch 		MAPLE_DMAADDR = sc->sc_txbuf_phys;
    234  1.12      uch 		MAPLE_STATE = 1;
    235  1.12      uch 		while (MAPLE_STATE != 0)
    236  1.12      uch 			;
    237  1.12      uch 
    238  1.12      uch 		for (p = 0; p < MAPLE_PORTS; p++)
    239  1.12      uch 			if ((sc->sc_rxbuf[p][0][0] & 0xff) ==
    240  1.12      uch 			    MAPLE_RESPONSE_DEVINFO)
    241  1.12      uch 				sc->sc_port_units[p] =
    242  1.12      uch 				    ((sc->sc_rxbuf[p][0][0] >> 15) & 0x3e) | 1;
    243  1.12      uch 			else
    244  1.12      uch 				sc->sc_port_units[p] = 0;
    245  1.12      uch 
    246  1.12      uch 
    247  1.12      uch 		maple_begin_txbuf(sc);
    248  1.12      uch 
    249  1.12      uch 		for (p = 0; p < MAPLE_PORTS; p++) {
    250  1.12      uch 			for (s = 0; s < MAPLE_SUBUNITS; s++) {
    251  1.12      uch 				if (sc->sc_port_units[p] & (1 << s))
    252  1.12      uch 					maple_write_command(sc, p, s,
    253  1.12      uch 					    MAPLE_COMMAND_DEVINFO, 0, NULL);
    254  1.12      uch 			}
    255  1.12      uch 		}
    256   1.1   marcus 
    257  1.12      uch 		if (maple_end_txbuf(sc)) {
    258  1.12      uch 			MAPLE_DMAADDR = sc->sc_txbuf_phys;
    259  1.12      uch 			MAPLE_STATE = 1;
    260  1.12      uch 			while (MAPLE_STATE != 0)
    261  1.12      uch 				;
    262  1.12      uch 
    263  1.12      uch 			for (p = 0; p < MAPLE_PORTS; p++)
    264  1.12      uch 				for (s = 0; s < MAPLE_SUBUNITS; s++)
    265  1.12      uch 					if (sc->sc_port_units[p] & (1 << s)) {
    266  1.12      uch 
    267  1.12      uch 						if ((sc->sc_rxbuf[p][s][0] &
    268  1.12      uch 						    0xff) ==
    269  1.12      uch 						    MAPLE_RESPONSE_DEVINFO) {
    270   1.1   marcus 
    271  1.12      uch 							u_int32_t *to_swap;
    272  1.12      uch 							int i;
    273   1.5   marcus 
    274  1.12      uch 							memcpy((to_swap =
    275  1.12      uch 							    &sc->sc_unit[p][s].
    276  1.12      uch 							    devinfo.di_func),
    277  1.12      uch 							    sc->sc_rxbuf[p][s] +
    278  1.12      uch 							    1,
    279  1.12      uch 							    sizeof(struct
    280  1.12      uch 								maple_devinfo));
    281   1.5   marcus 
    282  1.12      uch 							for (i = 0; i < 4; i++,
    283  1.12      uch 							    to_swap++)
    284  1.12      uch 								*to_swap =
    285  1.12      uch 								    ntohl
    286  1.12      uch 								    (*to_swap);
    287  1.12      uch 							maple_attach_dev(sc, p,
    288  1.12      uch 							    s);
    289  1.12      uch 						} else {
    290  1.12      uch 
    291  1.12      uch 							printf("%s: no response"
    292  1.12      uch 							    " from port %d "
    293  1.12      uch 							    " subunit %d\n",
    294  1.12      uch 							    sc->sc_dev.dv_xname,
    295  1.12      uch 							    p, s);
    296  1.12      uch 							sc->sc_port_units[p] &=
    297  1.12      uch 							    ~(1 << s);
    298  1.12      uch 						}
    299  1.12      uch 					}
    300  1.12      uch 
    301   1.5   marcus 		}
    302   1.1   marcus 
    303   1.1   marcus 	}
    304   1.1   marcus 
    305   1.1   marcus 	maple_polling = 0;
    306   1.1   marcus 
    307   1.1   marcus }
    308   1.1   marcus 
    309   1.1   marcus static void
    310  1.12      uch maple_send_commands(struct maple_softc *sc)
    311   1.1   marcus {
    312   1.5   marcus 	int p, s;
    313   1.1   marcus 
    314   1.1   marcus 	if (sc->maple_commands_pending || MAPLE_STATE != 0)
    315  1.12      uch 		return;
    316   1.1   marcus 
    317   1.1   marcus 	maple_begin_txbuf(sc);
    318   1.1   marcus 
    319   1.1   marcus 	for (p = 0; p < MAPLE_PORTS; p++) {
    320  1.12      uch 		for (s = 0; s < MAPLE_SUBUNITS; s++) {
    321  1.12      uch 			if (sc->sc_unit[p][s].getcond_callback != NULL) {
    322  1.12      uch 				u_int32_t func =
    323  1.12      uch 				    ntohl(sc->sc_unit[p][s].getcond_func);
    324   1.5   marcus 
    325  1.12      uch 				maple_write_command(sc, p, s,
    326  1.12      uch 				    MAPLE_COMMAND_GETCOND, 1, &func);
    327  1.12      uch 			}
    328  1.12      uch 		}
    329   1.1   marcus 	}
    330   1.1   marcus 
    331   1.1   marcus 	if (!maple_end_txbuf(sc))
    332  1.12      uch 		return;
    333   1.1   marcus 
    334   1.1   marcus 	MAPLE_DMAADDR = sc->sc_txbuf_phys;
    335   1.1   marcus 	MAPLE_STATE = 1;
    336   1.1   marcus 
    337   1.1   marcus 	sc->maple_commands_pending = 1;
    338   1.1   marcus }
    339   1.1   marcus 
    340   1.1   marcus static void
    341  1.12      uch maple_check_responses(struct maple_softc *sc)
    342   1.1   marcus {
    343   1.5   marcus 	int p, s;
    344   1.1   marcus 
    345   1.1   marcus 	if (!sc->maple_commands_pending || MAPLE_STATE != 0)
    346  1.12      uch 		return;
    347   1.1   marcus 
    348   1.1   marcus 	for (p = 0; p < MAPLE_PORTS; p++) {
    349  1.12      uch 		for (s = 0; s < MAPLE_SUBUNITS; s++) {
    350  1.12      uch 			struct maple_unit *u = &sc->sc_unit[p][s];
    351  1.12      uch 			if (u->getcond_callback != NULL &&
    352  1.12      uch 			    (sc->sc_rxbuf[p][s][0] & 0xff) ==
    353  1.12      uch 			    MAPLE_RESPONSE_DATATRF &&
    354  1.12      uch 			    (sc->sc_rxbuf[p][s][0] >> 24) >= 1 &&
    355  1.12      uch 			    htonl(sc->sc_rxbuf[p][s][1]) == u->getcond_func) {
    356  1.12      uch 				(*u->getcond_callback)(u->getcond_data,
    357  1.12      uch 				    (void *)(sc->sc_rxbuf[p][s] + 2),
    358  1.12      uch 				    ((sc->sc_rxbuf[p][s][0] >> 22) & 1020) - 4);
    359  1.12      uch 			}
    360  1.12      uch 		}
    361   1.1   marcus 	}
    362   1.1   marcus 
    363   1.1   marcus 	sc->maple_commands_pending = 0;
    364   1.1   marcus }
    365   1.1   marcus 
    366   1.1   marcus void
    367  1.12      uch maple_run_polling(struct device *dev)
    368   1.1   marcus {
    369   1.1   marcus 	struct maple_softc *sc;
    370   1.1   marcus 
    371   1.1   marcus 	sc = (struct maple_softc *)dev;
    372   1.1   marcus 
    373   1.1   marcus 	if (MAPLE_STATE != 0)
    374  1.12      uch 		return;
    375   1.1   marcus 
    376   1.1   marcus 	maple_send_commands(sc);
    377   1.1   marcus 
    378   1.1   marcus 	while (MAPLE_STATE != 0)
    379  1.12      uch 		;
    380   1.1   marcus 
    381   1.1   marcus 	maple_check_responses(sc);
    382   1.1   marcus }
    383   1.1   marcus 
    384   1.1   marcus void
    385  1.12      uch maple_set_condition_callback(struct device *dev, int port, int subunit,
    386  1.12      uch     u_int32_t func, void (*callback)(void *, void *, int), void *data)
    387   1.1   marcus {
    388   1.1   marcus 	struct maple_softc *sc;
    389   1.1   marcus 
    390   1.1   marcus 	sc = (struct maple_softc *)dev;
    391   1.1   marcus 
    392   1.1   marcus 	if ((port & ~(MAPLE_PORTS-1)) != 0 ||
    393   1.1   marcus 	    subunit < 0 || subunit >= MAPLE_SUBUNITS)
    394  1.12      uch 		return;
    395   1.1   marcus 
    396   1.1   marcus 	sc->sc_unit[port][subunit].getcond_func = func;
    397   1.1   marcus 	sc->sc_unit[port][subunit].getcond_callback = callback;
    398   1.1   marcus 	sc->sc_unit[port][subunit].getcond_data = data;
    399   1.1   marcus }
    400   1.1   marcus 
    401   1.1   marcus static void
    402  1.12      uch maple_callout(void *ctx)
    403   1.1   marcus {
    404   1.1   marcus 	struct maple_softc *sc = ctx;
    405   1.1   marcus 
    406  1.12      uch 	if (!maple_polling) {
    407  1.12      uch 		maple_check_responses(sc);
    408  1.12      uch 		maple_send_commands(sc);
    409   1.1   marcus 	}
    410   1.1   marcus 
    411   1.1   marcus 	callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS,
    412  1.12      uch 	    (void *)maple_callout, sc);
    413   1.1   marcus }
    414   1.1   marcus 
    415   1.2   marcus int
    416  1.12      uch maple_alloc_dma(size_t size, vaddr_t *vap, paddr_t *pap)
    417   1.2   marcus {
    418   1.2   marcus 	extern paddr_t avail_start, avail_end;	/* from pmap.c */
    419   1.2   marcus 	struct pglist mlist;
    420   1.8      chs 	struct vm_page *m;
    421   1.2   marcus 	int error;
    422   1.2   marcus 
    423   1.2   marcus 	size = round_page(size);
    424   1.2   marcus 
    425   1.2   marcus 	error = uvm_pglistalloc(size, avail_start, avail_end - PAGE_SIZE,
    426   1.2   marcus 	    0, 0, &mlist, 1, 0);
    427   1.2   marcus 	if (error)
    428   1.2   marcus 		return (error);
    429   1.2   marcus 
    430   1.2   marcus 	m = TAILQ_FIRST(&mlist);
    431   1.2   marcus 	*pap = VM_PAGE_TO_PHYS(m);
    432   1.2   marcus 	*vap = SH3_PHYS_TO_P2SEG(VM_PAGE_TO_PHYS(m));
    433   1.2   marcus 
    434   1.2   marcus 	return (0);
    435   1.2   marcus }
    436   1.2   marcus 
    437   1.2   marcus void
    438  1.12      uch maple_free_dma(paddr_t paddr, size_t size)
    439   1.2   marcus {
    440   1.2   marcus 	struct pglist mlist;
    441   1.8      chs 	struct vm_page *m;
    442   1.2   marcus 	bus_addr_t addr;
    443   1.2   marcus 
    444   1.2   marcus 	TAILQ_INIT(&mlist);
    445   1.2   marcus 	for (addr = paddr; addr < paddr + size; addr += PAGE_SIZE) {
    446   1.2   marcus 		m = PHYS_TO_VM_PAGE(addr);
    447   1.2   marcus 		TAILQ_INSERT_TAIL(&mlist, m, pageq);
    448   1.2   marcus 	}
    449   1.2   marcus 	uvm_pglistfree(&mlist);
    450   1.2   marcus }
    451   1.2   marcus 
    452   1.1   marcus static void
    453  1.12      uch mapleattach(struct device *parent, struct device *self, void *aux)
    454   1.1   marcus {
    455   1.1   marcus 	struct maple_softc *sc;
    456   1.1   marcus 	vaddr_t dmabuffer;
    457   1.2   marcus 	paddr_t dmabuffer_phys;
    458   1.1   marcus 	u_int32_t *p;
    459   1.1   marcus 	int i, j;
    460   1.1   marcus 
    461   1.1   marcus 	sc = (struct maple_softc *)self;
    462   1.1   marcus 
    463   1.1   marcus 	printf("\n");
    464   1.1   marcus 
    465   1.2   marcus 	if (maple_alloc_dma(MAPLE_DMABUF_SIZE, &dmabuffer, &dmabuffer_phys)) {
    466  1.12      uch 		printf("%s: unable to allocate DMA buffers.\n",
    467  1.12      uch 		    sc->sc_dev.dv_xname);
    468  1.12      uch 		return;
    469   1.2   marcus 	}
    470   1.1   marcus 
    471  1.12      uch 	p = (u_int32_t *)dmabuffer;
    472   1.1   marcus 
    473   1.1   marcus 	for (i = 0; i < MAPLE_PORTS; i++)
    474  1.12      uch 		for (j = 0; j < MAPLE_SUBUNITS; j++) {
    475  1.12      uch 			sc->sc_rxbuf[i][j] = p;
    476  1.12      uch 			sc->sc_rxbuf_phys[i][j] = SH3_P2SEG_TO_PHYS(p);
    477  1.12      uch 			p += 256;
    478  1.12      uch 		}
    479   1.1   marcus 
    480   1.1   marcus 	sc->sc_txbuf = p;
    481   1.1   marcus 	sc->sc_txbuf_phys = SH3_P2SEG_TO_PHYS(p);
    482   1.1   marcus 
    483   1.1   marcus 	sc->maple_commands_pending = 0;
    484   1.1   marcus 
    485   1.1   marcus 	MAPLE_RESET = RESET_MAGIC;
    486   1.1   marcus 	MAPLE_RESET2 = 0;
    487   1.1   marcus 
    488   1.1   marcus 	MAPLE_SPEED = SPEED_2MBPS | TIMEOUT(50000);
    489   1.1   marcus 
    490   1.1   marcus 	MAPLE_ENABLE = 1;
    491   1.1   marcus 
    492   1.1   marcus 	maple_scanbus(sc);
    493   1.1   marcus 
    494   1.9      wiz 	memset(&sc->maple_callout_ch, 0, sizeof(sc->maple_callout_ch));
    495   1.1   marcus 
    496   1.1   marcus 	callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS,
    497  1.12      uch 	    (void *)maple_callout, sc);
    498   1.1   marcus }
    499   1.1   marcus 
    500   1.1   marcus int
    501  1.12      uch mapleprint(void *aux, const char *pnp)
    502   1.1   marcus {
    503   1.1   marcus 	struct maple_attach_args *ma = aux;
    504   1.1   marcus 
    505   1.7   marcus 	if (pnp != NULL) {
    506   1.7   marcus 		printf("maple%c", 'A'+ma->ma_port);
    507   1.7   marcus 		if (ma->ma_subunit != 0)
    508   1.7   marcus 			printf("%d", ma->ma_subunit);
    509   1.7   marcus 		printf(" at %s", pnp);
    510   1.7   marcus 	}
    511   1.1   marcus 
    512   1.4  thorpej 	printf(" port %d", ma->ma_port);
    513   1.1   marcus 
    514   1.1   marcus 	if (ma->ma_subunit != 0)
    515   1.4  thorpej 		printf(" subunit %d", ma->ma_subunit);
    516   1.1   marcus 
    517   1.7   marcus 	printf(": %.*s",
    518  1.12      uch 	    (int)sizeof(ma->ma_devinfo->di_product_name),
    519  1.12      uch 	    ma->ma_devinfo->di_product_name);
    520   1.7   marcus 
    521   1.7   marcus 	return (0);
    522   1.1   marcus }
    523   1.1   marcus 
    524   1.4  thorpej static int
    525  1.12      uch maplesubmatch(struct device *parent, struct cfdata *match, void *aux)
    526   1.1   marcus {
    527   1.4  thorpej 	struct maple_attach_args *ma = aux;
    528   1.1   marcus 
    529   1.4  thorpej 	if (match->cf_loc[MAPLECF_PORT] != MAPLECF_PORT_DEFAULT &&
    530   1.4  thorpej 	    match->cf_loc[MAPLECF_PORT] != ma->ma_port)
    531   1.4  thorpej 		return (0);
    532   1.4  thorpej 
    533   1.4  thorpej 	if (match->cf_loc[MAPLECF_SUBUNIT] != MAPLECF_SUBUNIT_DEFAULT &&
    534   1.4  thorpej 	    match->cf_loc[MAPLECF_SUBUNIT] != ma->ma_subunit)
    535   1.4  thorpej 		return (0);
    536   1.4  thorpej 
    537   1.4  thorpej 	return ((*match->cf_attach->ca_match)(parent, match, aux));
    538   1.1   marcus }
    539   1.1   marcus 
    540   1.1   marcus u_int32_t
    541  1.12      uch maple_get_function_data(struct maple_devinfo *devinfo, u_int32_t function_code)
    542   1.1   marcus {
    543   1.1   marcus 	int i, p = 0;
    544   1.1   marcus 
    545   1.1   marcus 	for (i = 31; i >= 0; --i)
    546  1.12      uch 		if (devinfo->di_func & (1U << i)) {
    547  1.12      uch 			if (function_code & (1U << i))
    548  1.12      uch 				return devinfo->di_function_data[p];
    549  1.12      uch 			else
    550  1.12      uch 				if (++p >= 3)
    551  1.12      uch 					break;
    552  1.12      uch 		}
    553  1.12      uch 
    554   1.1   marcus 	return (0);
    555   1.7   marcus }
    556   1.7   marcus 
    557   1.7   marcus /* Generic maple device interface */
    558   1.7   marcus 
    559   1.7   marcus int
    560  1.12      uch mapleopen(dev_t dev, int flag, int mode, struct proc *p)
    561   1.7   marcus {
    562   1.7   marcus 	struct maple_softc *sc;
    563   1.7   marcus 
    564   1.7   marcus 	sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
    565   1.7   marcus 	if (sc == NULL)			/* make sure it was attached */
    566   1.7   marcus 		return (ENXIO);
    567   1.7   marcus 
    568   1.7   marcus 	if (MAPLEPORT(dev) >= MAPLE_PORTS)
    569   1.7   marcus 		return (ENXIO);
    570   1.7   marcus 
    571   1.7   marcus 	if (MAPLESUBUNIT(dev) >= MAPLE_SUBUNITS)
    572   1.7   marcus 		return (ENXIO);
    573   1.7   marcus 
    574  1.12      uch 	if (!(sc->sc_port_units[MAPLEPORT(dev)] & (1 << MAPLESUBUNIT(dev))))
    575   1.7   marcus 		return (ENXIO);
    576   1.7   marcus 
    577  1.12      uch 	sc->sc_port_units_open[MAPLEPORT(dev)] |= 1 << MAPLESUBUNIT(dev);
    578   1.7   marcus 
    579   1.7   marcus 	return (0);
    580   1.7   marcus }
    581   1.7   marcus 
    582   1.7   marcus int
    583  1.12      uch mapleclose(dev_t dev, int flag, int mode, struct proc *p)
    584   1.7   marcus {
    585   1.7   marcus 	struct maple_softc *sc;
    586   1.7   marcus 
    587   1.7   marcus 	sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
    588   1.7   marcus 
    589  1.12      uch 	sc->sc_port_units_open[MAPLEPORT(dev)] &= ~(1 << MAPLESUBUNIT(dev));
    590   1.7   marcus 
    591   1.7   marcus 	return (0);
    592   1.7   marcus }
    593   1.7   marcus 
    594   1.7   marcus int
    595  1.12      uch maple_internal_ioctl(struct maple_softc *sc, int port, int subunit, u_long cmd,
    596  1.12      uch     caddr_t data, int flag, struct proc *p)
    597   1.7   marcus {
    598   1.7   marcus 	struct maple_unit *u = &sc->sc_unit[port][subunit];
    599   1.7   marcus 
    600  1.12      uch 	if (!(sc->sc_port_units[port] & (1 << subunit)))
    601   1.7   marcus 		return (ENXIO);
    602   1.7   marcus 
    603   1.7   marcus 	switch(cmd) {
    604   1.7   marcus 	case MAPLEIO_GDEVINFO:
    605   1.9      wiz 		memcpy(data, &u->devinfo, sizeof(struct maple_devinfo));
    606   1.7   marcus 		return (0);
    607   1.7   marcus 	default:
    608   1.7   marcus 		return (EINVAL);
    609   1.7   marcus 	}
    610   1.7   marcus }
    611   1.7   marcus 
    612   1.7   marcus int
    613  1.12      uch mapleioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    614   1.7   marcus {
    615   1.7   marcus 	struct maple_softc *sc;
    616   1.7   marcus 
    617   1.7   marcus 	sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
    618   1.7   marcus 
    619  1.12      uch 	return (maple_internal_ioctl(sc, MAPLEPORT(dev), MAPLESUBUNIT(dev),
    620  1.12      uch 	    cmd, data, flag, p));
    621   1.1   marcus }
    622