Home | History | Annotate | Line # | Download | only in alpha
alpha_pci_io.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: alpha_pci_io.c,v 1.1 2000/02/26 18:59:36 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Jason R. Thorpe.
      9  1.1  thorpej  *
     10  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1  thorpej  * modification, are permitted provided that the following conditions
     12  1.1  thorpej  * are met:
     13  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.1  thorpej  *    must display the following acknowledgement:
     20  1.1  thorpej  *	This product includes software developed by the NetBSD
     21  1.1  thorpej  *	Foundation, Inc. and its contributors.
     22  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     24  1.1  thorpej  *    from this software without specific prior written permission.
     25  1.1  thorpej  *
     26  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  thorpej  */
     38  1.1  thorpej 
     39  1.1  thorpej /*
     40  1.1  thorpej  * Support for x86-style programmed I/O to PCI/EISA/ISA I/O space.  This
     41  1.1  thorpej  * is currently used to provide such support for XFree86.  In a perfect
     42  1.1  thorpej  * world, this would go away in favor of a real bus space mapping framework.
     43  1.1  thorpej  */
     44  1.1  thorpej 
     45  1.1  thorpej #include <sys/param.h>
     46  1.1  thorpej 
     47  1.1  thorpej #include <machine/bwx.h>
     48  1.1  thorpej #include <machine/sysarch.h>
     49  1.1  thorpej #include <machine/pio.h>
     50  1.1  thorpej 
     51  1.1  thorpej #include <err.h>
     52  1.1  thorpej #include <stdlib.h>
     53  1.1  thorpej 
     54  1.1  thorpej struct alpha_bus_window *alpha_pci_io_windows;
     55  1.1  thorpej int alpha_pci_io_window_count;
     56  1.1  thorpej 
     57  1.1  thorpej __inline struct alpha_bus_window *alpha_pci_io_findwindow __P((bus_addr_t));
     58  1.1  thorpej __inline u_int32_t *alpha_pci_io_swiz __P((bus_addr_t, int));
     59  1.1  thorpej 
     60  1.1  thorpej u_int8_t	alpha_pci_io_swiz_inb __P((bus_addr_t));
     61  1.1  thorpej u_int16_t	alpha_pci_io_swiz_inw __P((bus_addr_t));
     62  1.1  thorpej u_int32_t	alpha_pci_io_swiz_inl __P((bus_addr_t));
     63  1.1  thorpej void		alpha_pci_io_swiz_outb __P((bus_addr_t, u_int8_t));
     64  1.1  thorpej void		alpha_pci_io_swiz_outw __P((bus_addr_t, u_int16_t));
     65  1.1  thorpej void		alpha_pci_io_swiz_outl __P((bus_addr_t, u_int32_t));
     66  1.1  thorpej 
     67  1.1  thorpej const struct alpha_pci_io_ops alpha_pci_io_swiz_ops = {
     68  1.1  thorpej 	alpha_pci_io_swiz_inb,
     69  1.1  thorpej 	alpha_pci_io_swiz_inw,
     70  1.1  thorpej 	alpha_pci_io_swiz_inl,
     71  1.1  thorpej 	alpha_pci_io_swiz_outb,
     72  1.1  thorpej 	alpha_pci_io_swiz_outw,
     73  1.1  thorpej 	alpha_pci_io_swiz_outl,
     74  1.1  thorpej };
     75  1.1  thorpej 
     76  1.1  thorpej u_int8_t	alpha_pci_io_bwx_inb __P((bus_addr_t));
     77  1.1  thorpej u_int16_t	alpha_pci_io_bwx_inw __P((bus_addr_t));
     78  1.1  thorpej u_int32_t	alpha_pci_io_bwx_inl __P((bus_addr_t));
     79  1.1  thorpej void		alpha_pci_io_bwx_outb __P((bus_addr_t, u_int8_t));
     80  1.1  thorpej void		alpha_pci_io_bwx_outw __P((bus_addr_t, u_int16_t));
     81  1.1  thorpej void		alpha_pci_io_bwx_outl __P((bus_addr_t, u_int32_t));
     82  1.1  thorpej 
     83  1.1  thorpej const struct alpha_pci_io_ops alpha_pci_io_bwx_ops = {
     84  1.1  thorpej 	alpha_pci_io_bwx_inb,
     85  1.1  thorpej 	alpha_pci_io_bwx_inw,
     86  1.1  thorpej 	alpha_pci_io_bwx_inl,
     87  1.1  thorpej 	alpha_pci_io_bwx_outb,
     88  1.1  thorpej 	alpha_pci_io_bwx_outw,
     89  1.1  thorpej 	alpha_pci_io_bwx_outl,
     90  1.1  thorpej };
     91  1.1  thorpej 
     92  1.1  thorpej const struct alpha_pci_io_ops *alpha_pci_io_switch;
     93  1.1  thorpej 
     94  1.1  thorpej int
     95  1.1  thorpej alpha_pci_io_enable(onoff)
     96  1.1  thorpej 	int onoff;
     97  1.1  thorpej {
     98  1.1  thorpej 	struct alpha_bus_window *abw;
     99  1.1  thorpej 	int i, count;
    100  1.1  thorpej 
    101  1.1  thorpej 	if (onoff == 0 && alpha_pci_io_windows != NULL) {
    102  1.1  thorpej 		for (i = 0; i < alpha_pci_io_window_count; i++)
    103  1.1  thorpej 			alpha_bus_unmapwindow(&alpha_pci_io_windows[i]);
    104  1.1  thorpej 		free(alpha_pci_io_windows);
    105  1.1  thorpej 		alpha_pci_io_windows = NULL;
    106  1.1  thorpej 		alpha_pci_io_window_count = 0;
    107  1.1  thorpej 		alpha_pci_io_switch = NULL;
    108  1.1  thorpej 		return (0);
    109  1.1  thorpej 	} else if (onoff == 0)
    110  1.1  thorpej 		return (0);
    111  1.1  thorpej 	else if (alpha_pci_io_windows != NULL)
    112  1.1  thorpej 		return (0);
    113  1.1  thorpej 
    114  1.1  thorpej 	count = alpha_bus_getwindows(ALPHA_BUS_TYPE_PCI_IO, &abw);
    115  1.1  thorpej 	if (count <= 0)
    116  1.1  thorpej 		return (-1);
    117  1.1  thorpej 
    118  1.1  thorpej 	for (i = 0; i < count; i++) {
    119  1.1  thorpej 		if (alpha_bus_mapwindow(&abw[i]) == -1) {
    120  1.1  thorpej 			free(abw);
    121  1.1  thorpej 			return (-1);
    122  1.1  thorpej 		}
    123  1.1  thorpej 	}
    124  1.1  thorpej 
    125  1.1  thorpej 	alpha_pci_io_windows = abw;
    126  1.1  thorpej 	alpha_pci_io_window_count = count;
    127  1.1  thorpej 
    128  1.1  thorpej 	if (abw->abw_abst.abst_flags & ABST_BWX)
    129  1.1  thorpej 		alpha_pci_io_switch = &alpha_pci_io_bwx_ops;
    130  1.1  thorpej 	else
    131  1.1  thorpej 		alpha_pci_io_switch = &alpha_pci_io_swiz_ops;
    132  1.1  thorpej 
    133  1.1  thorpej 	return (0);
    134  1.1  thorpej }
    135  1.1  thorpej 
    136  1.1  thorpej __inline struct alpha_bus_window *
    137  1.1  thorpej alpha_pci_io_findwindow(ioaddr)
    138  1.1  thorpej 	bus_addr_t ioaddr;
    139  1.1  thorpej {
    140  1.1  thorpej 	struct alpha_bus_window *abw;
    141  1.1  thorpej 	int i;
    142  1.1  thorpej 
    143  1.1  thorpej 	/* XXX Cache the last hit? */
    144  1.1  thorpej 
    145  1.1  thorpej 	for (i = 0; i < alpha_pci_io_window_count; i++) {
    146  1.1  thorpej 		abw = &alpha_pci_io_windows[i];
    147  1.1  thorpej 		if (ioaddr >= abw->abw_abst.abst_bus_start &&
    148  1.1  thorpej 		    ioaddr <= abw->abw_abst.abst_bus_end)
    149  1.1  thorpej 			return (abw);
    150  1.1  thorpej 	}
    151  1.1  thorpej 
    152  1.1  thorpej 	warnx("alpha_pci_io_findwindow: no window for 0x%lx, ABORTING!",
    153  1.1  thorpej 	    (u_long) ioaddr);
    154  1.1  thorpej 	abort();
    155  1.1  thorpej }
    156  1.1  thorpej 
    157  1.1  thorpej __inline u_int32_t *
    158  1.1  thorpej alpha_pci_io_swiz(ioaddr, size)
    159  1.1  thorpej 	bus_addr_t ioaddr;
    160  1.1  thorpej 	int size;
    161  1.1  thorpej {
    162  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    163  1.1  thorpej 	u_int32_t *port;
    164  1.1  thorpej 
    165  1.1  thorpej 	port = (u_int32_t *) (abw->abw_addr +
    166  1.1  thorpej 	    (((ioaddr - abw->abw_abst.abst_bus_start) <<
    167  1.1  thorpej 	      abw->abw_abst.abst_addr_shift) |
    168  1.1  thorpej 	     (size << abw->abw_abst.abst_size_shift)));
    169  1.1  thorpej 
    170  1.1  thorpej 	return (port);
    171  1.1  thorpej }
    172  1.1  thorpej 
    173  1.1  thorpej u_int8_t
    174  1.1  thorpej alpha_pci_io_swiz_inb(ioaddr)
    175  1.1  thorpej 	bus_addr_t ioaddr;
    176  1.1  thorpej {
    177  1.1  thorpej 	u_int32_t *port = alpha_pci_io_swiz(ioaddr, 0);
    178  1.1  thorpej 	int offset = ioaddr & 3;
    179  1.1  thorpej 
    180  1.1  thorpej 	alpha_mb();
    181  1.1  thorpej 
    182  1.1  thorpej 	return ((*port >> (8 * offset)) & 0xff);
    183  1.1  thorpej }
    184  1.1  thorpej 
    185  1.1  thorpej u_int16_t
    186  1.1  thorpej alpha_pci_io_swiz_inw(ioaddr)
    187  1.1  thorpej 	bus_addr_t ioaddr;
    188  1.1  thorpej {
    189  1.1  thorpej 	u_int32_t *port = alpha_pci_io_swiz(ioaddr, 1);
    190  1.1  thorpej 	int offset = ioaddr & 3;
    191  1.1  thorpej 
    192  1.1  thorpej 	alpha_mb();
    193  1.1  thorpej 
    194  1.1  thorpej 	return ((*port >> (8 * offset)) & 0xffff);
    195  1.1  thorpej }
    196  1.1  thorpej 
    197  1.1  thorpej u_int32_t
    198  1.1  thorpej alpha_pci_io_swiz_inl(ioaddr)
    199  1.1  thorpej 	bus_addr_t ioaddr;
    200  1.1  thorpej {
    201  1.1  thorpej 	u_int32_t *port = alpha_pci_io_swiz(ioaddr, 3);
    202  1.1  thorpej 
    203  1.1  thorpej 	alpha_mb();
    204  1.1  thorpej 
    205  1.1  thorpej 	return (*port);
    206  1.1  thorpej }
    207  1.1  thorpej 
    208  1.1  thorpej void
    209  1.1  thorpej alpha_pci_io_swiz_outb(ioaddr, val)
    210  1.1  thorpej 	bus_addr_t ioaddr;
    211  1.1  thorpej 	u_int8_t val;
    212  1.1  thorpej {
    213  1.1  thorpej 	u_int32_t *port = alpha_pci_io_swiz(ioaddr, 0);
    214  1.1  thorpej 	int offset = ioaddr & 3;
    215  1.1  thorpej 	u_int32_t nval = val << (8 * offset);
    216  1.1  thorpej 
    217  1.1  thorpej 	*port = nval;
    218  1.1  thorpej 	alpha_mb();
    219  1.1  thorpej }
    220  1.1  thorpej 
    221  1.1  thorpej void
    222  1.1  thorpej alpha_pci_io_swiz_outw(ioaddr, val)
    223  1.1  thorpej 	bus_addr_t ioaddr;
    224  1.1  thorpej 	u_int16_t val;
    225  1.1  thorpej {
    226  1.1  thorpej 	u_int32_t *port = alpha_pci_io_swiz(ioaddr, 1);
    227  1.1  thorpej 	int offset = ioaddr & 3;
    228  1.1  thorpej 	u_int32_t nval = val << (8 * offset);
    229  1.1  thorpej 
    230  1.1  thorpej 	*port = nval;
    231  1.1  thorpej 	alpha_mb();
    232  1.1  thorpej }
    233  1.1  thorpej 
    234  1.1  thorpej void
    235  1.1  thorpej alpha_pci_io_swiz_outl(ioaddr, val)
    236  1.1  thorpej 	bus_addr_t ioaddr;
    237  1.1  thorpej 	u_int32_t val;
    238  1.1  thorpej {
    239  1.1  thorpej 	u_int32_t *port = alpha_pci_io_swiz(ioaddr, 3);
    240  1.1  thorpej 
    241  1.1  thorpej 	*port = val;
    242  1.1  thorpej 	alpha_mb();
    243  1.1  thorpej }
    244  1.1  thorpej 
    245  1.1  thorpej /*
    246  1.1  thorpej  * The following functions are used only on EV56 and greater CPUs,
    247  1.1  thorpej  * and the assembler requires going to EV56 mode in order to emit
    248  1.1  thorpej  * these instructions.
    249  1.1  thorpej  */
    250  1.1  thorpej __asm(".arch ev56");
    251  1.1  thorpej 
    252  1.1  thorpej u_int8_t
    253  1.1  thorpej alpha_pci_io_bwx_inb(ioaddr)
    254  1.1  thorpej 	bus_addr_t ioaddr;
    255  1.1  thorpej {
    256  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    257  1.1  thorpej 	u_int8_t *port = (u_int8_t *) (abw->abw_addr +
    258  1.1  thorpej 	    (ioaddr - abw->abw_abst.abst_bus_start));
    259  1.1  thorpej 
    260  1.1  thorpej 	alpha_mb();
    261  1.1  thorpej 
    262  1.1  thorpej 	return (alpha_ldbu(port));
    263  1.1  thorpej }
    264  1.1  thorpej 
    265  1.1  thorpej u_int16_t
    266  1.1  thorpej alpha_pci_io_bwx_inw(ioaddr)
    267  1.1  thorpej 	bus_addr_t ioaddr;
    268  1.1  thorpej {
    269  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    270  1.1  thorpej 	u_int16_t *port = (u_int16_t *) (abw->abw_addr +
    271  1.1  thorpej 	    (ioaddr - abw->abw_abst.abst_bus_start));
    272  1.1  thorpej 
    273  1.1  thorpej 	alpha_mb();
    274  1.1  thorpej 
    275  1.1  thorpej 	return (alpha_ldwu(port));
    276  1.1  thorpej }
    277  1.1  thorpej 
    278  1.1  thorpej u_int32_t
    279  1.1  thorpej alpha_pci_io_bwx_inl(ioaddr)
    280  1.1  thorpej 	bus_addr_t ioaddr;
    281  1.1  thorpej {
    282  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    283  1.1  thorpej 	u_int32_t *port = (u_int32_t *) (abw->abw_addr +
    284  1.1  thorpej 	    (ioaddr - abw->abw_abst.abst_bus_start));
    285  1.1  thorpej 
    286  1.1  thorpej 	alpha_mb();
    287  1.1  thorpej 
    288  1.1  thorpej 	return (*port);
    289  1.1  thorpej }
    290  1.1  thorpej 
    291  1.1  thorpej void
    292  1.1  thorpej alpha_pci_io_bwx_outb(ioaddr, val)
    293  1.1  thorpej 	bus_addr_t ioaddr;
    294  1.1  thorpej 	u_int8_t val;
    295  1.1  thorpej {
    296  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    297  1.1  thorpej 	u_int8_t *port = (u_int8_t *) (abw->abw_addr +
    298  1.1  thorpej 	    (ioaddr - abw->abw_abst.abst_bus_start));
    299  1.1  thorpej 
    300  1.1  thorpej 	alpha_stb(port, val);
    301  1.1  thorpej 	alpha_mb();
    302  1.1  thorpej }
    303  1.1  thorpej 
    304  1.1  thorpej void
    305  1.1  thorpej alpha_pci_io_bwx_outw(ioaddr, val)
    306  1.1  thorpej 	bus_addr_t ioaddr;
    307  1.1  thorpej 	u_int16_t val;
    308  1.1  thorpej {
    309  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    310  1.1  thorpej 	u_int16_t *port = (u_int16_t *) (abw->abw_addr +
    311  1.1  thorpej 	    (ioaddr - abw->abw_abst.abst_bus_start));
    312  1.1  thorpej 
    313  1.1  thorpej 	alpha_stw(port, val);
    314  1.1  thorpej 	alpha_mb();
    315  1.1  thorpej }
    316  1.1  thorpej 
    317  1.1  thorpej void
    318  1.1  thorpej alpha_pci_io_bwx_outl(ioaddr, val)
    319  1.1  thorpej 	bus_addr_t ioaddr;
    320  1.1  thorpej 	u_int32_t val;
    321  1.1  thorpej {
    322  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    323  1.1  thorpej 	u_int32_t *port = (u_int32_t *) (abw->abw_addr +
    324  1.1  thorpej 	    (ioaddr - abw->abw_abst.abst_bus_start));
    325  1.1  thorpej 
    326  1.1  thorpej 	*port = val;
    327  1.1  thorpej 	alpha_mb();
    328  1.1  thorpej }
    329