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