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