Home | History | Annotate | Line # | Download | only in alpha
alpha_pci_io.c revision 1.3
      1  1.3    perry /*	$NetBSD: alpha_pci_io.c,v 1.3 2005/12/24 21:11:16 perry 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.2  thorpej uint8_t		alpha_pci_io_swiz_inb(bus_addr_t);
     58  1.2  thorpej uint16_t	alpha_pci_io_swiz_inw(bus_addr_t);
     59  1.2  thorpej uint32_t	alpha_pci_io_swiz_inl(bus_addr_t);
     60  1.2  thorpej void		alpha_pci_io_swiz_outb(bus_addr_t, uint8_t);
     61  1.2  thorpej void		alpha_pci_io_swiz_outw(bus_addr_t, uint16_t);
     62  1.2  thorpej void		alpha_pci_io_swiz_outl(bus_addr_t, uint32_t);
     63  1.1  thorpej 
     64  1.1  thorpej const struct alpha_pci_io_ops alpha_pci_io_swiz_ops = {
     65  1.1  thorpej 	alpha_pci_io_swiz_inb,
     66  1.1  thorpej 	alpha_pci_io_swiz_inw,
     67  1.1  thorpej 	alpha_pci_io_swiz_inl,
     68  1.1  thorpej 	alpha_pci_io_swiz_outb,
     69  1.1  thorpej 	alpha_pci_io_swiz_outw,
     70  1.1  thorpej 	alpha_pci_io_swiz_outl,
     71  1.1  thorpej };
     72  1.1  thorpej 
     73  1.2  thorpej uint8_t		alpha_pci_io_bwx_inb(bus_addr_t);
     74  1.2  thorpej uint16_t	alpha_pci_io_bwx_inw(bus_addr_t);
     75  1.2  thorpej uint32_t	alpha_pci_io_bwx_inl(bus_addr_t);
     76  1.2  thorpej void		alpha_pci_io_bwx_outb(bus_addr_t, uint8_t);
     77  1.2  thorpej void		alpha_pci_io_bwx_outw(bus_addr_t, uint16_t);
     78  1.2  thorpej void		alpha_pci_io_bwx_outl(bus_addr_t, uint32_t);
     79  1.1  thorpej 
     80  1.1  thorpej const struct alpha_pci_io_ops alpha_pci_io_bwx_ops = {
     81  1.1  thorpej 	alpha_pci_io_bwx_inb,
     82  1.1  thorpej 	alpha_pci_io_bwx_inw,
     83  1.1  thorpej 	alpha_pci_io_bwx_inl,
     84  1.1  thorpej 	alpha_pci_io_bwx_outb,
     85  1.1  thorpej 	alpha_pci_io_bwx_outw,
     86  1.1  thorpej 	alpha_pci_io_bwx_outl,
     87  1.1  thorpej };
     88  1.1  thorpej 
     89  1.1  thorpej const struct alpha_pci_io_ops *alpha_pci_io_switch;
     90  1.1  thorpej 
     91  1.1  thorpej int
     92  1.2  thorpej alpha_pci_io_enable(int onoff)
     93  1.1  thorpej {
     94  1.1  thorpej 	struct alpha_bus_window *abw;
     95  1.1  thorpej 	int i, count;
     96  1.1  thorpej 
     97  1.1  thorpej 	if (onoff == 0 && alpha_pci_io_windows != NULL) {
     98  1.1  thorpej 		for (i = 0; i < alpha_pci_io_window_count; i++)
     99  1.1  thorpej 			alpha_bus_unmapwindow(&alpha_pci_io_windows[i]);
    100  1.1  thorpej 		free(alpha_pci_io_windows);
    101  1.1  thorpej 		alpha_pci_io_windows = NULL;
    102  1.1  thorpej 		alpha_pci_io_window_count = 0;
    103  1.1  thorpej 		alpha_pci_io_switch = NULL;
    104  1.1  thorpej 		return (0);
    105  1.1  thorpej 	} else if (onoff == 0)
    106  1.1  thorpej 		return (0);
    107  1.1  thorpej 	else if (alpha_pci_io_windows != NULL)
    108  1.1  thorpej 		return (0);
    109  1.1  thorpej 
    110  1.1  thorpej 	count = alpha_bus_getwindows(ALPHA_BUS_TYPE_PCI_IO, &abw);
    111  1.1  thorpej 	if (count <= 0)
    112  1.1  thorpej 		return (-1);
    113  1.1  thorpej 
    114  1.1  thorpej 	for (i = 0; i < count; i++) {
    115  1.1  thorpej 		if (alpha_bus_mapwindow(&abw[i]) == -1) {
    116  1.1  thorpej 			free(abw);
    117  1.1  thorpej 			return (-1);
    118  1.1  thorpej 		}
    119  1.1  thorpej 	}
    120  1.1  thorpej 
    121  1.1  thorpej 	alpha_pci_io_windows = abw;
    122  1.1  thorpej 	alpha_pci_io_window_count = count;
    123  1.1  thorpej 
    124  1.1  thorpej 	if (abw->abw_abst.abst_flags & ABST_BWX)
    125  1.1  thorpej 		alpha_pci_io_switch = &alpha_pci_io_bwx_ops;
    126  1.1  thorpej 	else
    127  1.1  thorpej 		alpha_pci_io_switch = &alpha_pci_io_swiz_ops;
    128  1.1  thorpej 
    129  1.1  thorpej 	return (0);
    130  1.1  thorpej }
    131  1.1  thorpej 
    132  1.3    perry static inline struct alpha_bus_window *
    133  1.2  thorpej alpha_pci_io_findwindow(bus_addr_t ioaddr)
    134  1.1  thorpej {
    135  1.1  thorpej 	struct alpha_bus_window *abw;
    136  1.1  thorpej 	int i;
    137  1.1  thorpej 
    138  1.1  thorpej 	/* XXX Cache the last hit? */
    139  1.1  thorpej 
    140  1.1  thorpej 	for (i = 0; i < alpha_pci_io_window_count; i++) {
    141  1.1  thorpej 		abw = &alpha_pci_io_windows[i];
    142  1.1  thorpej 		if (ioaddr >= abw->abw_abst.abst_bus_start &&
    143  1.1  thorpej 		    ioaddr <= abw->abw_abst.abst_bus_end)
    144  1.1  thorpej 			return (abw);
    145  1.1  thorpej 	}
    146  1.1  thorpej 
    147  1.1  thorpej 	warnx("alpha_pci_io_findwindow: no window for 0x%lx, ABORTING!",
    148  1.1  thorpej 	    (u_long) ioaddr);
    149  1.1  thorpej 	abort();
    150  1.2  thorpej 	/* NOTREACHED */
    151  1.1  thorpej }
    152  1.1  thorpej 
    153  1.3    perry static inline uint32_t *
    154  1.2  thorpej alpha_pci_io_swiz(bus_addr_t ioaddr, int size)
    155  1.1  thorpej {
    156  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    157  1.2  thorpej 	uint32_t *port;
    158  1.1  thorpej 
    159  1.2  thorpej 	/* LINTED */
    160  1.2  thorpej 	port = (uint32_t *) (abw->abw_addr +
    161  1.1  thorpej 	    (((ioaddr - abw->abw_abst.abst_bus_start) <<
    162  1.1  thorpej 	      abw->abw_abst.abst_addr_shift) |
    163  1.1  thorpej 	     (size << abw->abw_abst.abst_size_shift)));
    164  1.1  thorpej 
    165  1.1  thorpej 	return (port);
    166  1.1  thorpej }
    167  1.1  thorpej 
    168  1.2  thorpej uint8_t
    169  1.2  thorpej alpha_pci_io_swiz_inb(bus_addr_t ioaddr)
    170  1.1  thorpej {
    171  1.2  thorpej 	uint32_t *port = alpha_pci_io_swiz(ioaddr, 0);
    172  1.2  thorpej 	bus_addr_t offset = ioaddr & 3;
    173  1.1  thorpej 
    174  1.1  thorpej 	alpha_mb();
    175  1.1  thorpej 
    176  1.1  thorpej 	return ((*port >> (8 * offset)) & 0xff);
    177  1.1  thorpej }
    178  1.1  thorpej 
    179  1.2  thorpej uint16_t
    180  1.2  thorpej alpha_pci_io_swiz_inw(bus_addr_t ioaddr)
    181  1.1  thorpej {
    182  1.2  thorpej 	uint32_t *port = alpha_pci_io_swiz(ioaddr, 1);
    183  1.2  thorpej 	bus_addr_t offset = ioaddr & 3;
    184  1.1  thorpej 
    185  1.1  thorpej 	alpha_mb();
    186  1.1  thorpej 
    187  1.1  thorpej 	return ((*port >> (8 * offset)) & 0xffff);
    188  1.1  thorpej }
    189  1.1  thorpej 
    190  1.2  thorpej uint32_t
    191  1.2  thorpej alpha_pci_io_swiz_inl(bus_addr_t ioaddr)
    192  1.1  thorpej {
    193  1.2  thorpej 	uint32_t *port = alpha_pci_io_swiz(ioaddr, 3);
    194  1.1  thorpej 
    195  1.1  thorpej 	alpha_mb();
    196  1.1  thorpej 
    197  1.1  thorpej 	return (*port);
    198  1.1  thorpej }
    199  1.1  thorpej 
    200  1.1  thorpej void
    201  1.2  thorpej alpha_pci_io_swiz_outb(bus_addr_t ioaddr, uint8_t val)
    202  1.2  thorpej {
    203  1.2  thorpej 	uint32_t *port = alpha_pci_io_swiz(ioaddr, 0);
    204  1.2  thorpej 	bus_addr_t offset = ioaddr & 3;
    205  1.2  thorpej 	uint32_t nval = ((uint32_t)val) << (8 * offset);
    206  1.1  thorpej 
    207  1.1  thorpej 	*port = nval;
    208  1.1  thorpej 	alpha_mb();
    209  1.1  thorpej }
    210  1.1  thorpej 
    211  1.1  thorpej void
    212  1.2  thorpej alpha_pci_io_swiz_outw(bus_addr_t ioaddr, uint16_t val)
    213  1.2  thorpej {
    214  1.2  thorpej 	uint32_t *port = alpha_pci_io_swiz(ioaddr, 1);
    215  1.2  thorpej 	bus_addr_t offset = ioaddr & 3;
    216  1.2  thorpej 	uint32_t nval = ((uint32_t)val) << (8 * offset);
    217  1.1  thorpej 
    218  1.1  thorpej 	*port = nval;
    219  1.1  thorpej 	alpha_mb();
    220  1.1  thorpej }
    221  1.1  thorpej 
    222  1.1  thorpej void
    223  1.2  thorpej alpha_pci_io_swiz_outl(bus_addr_t ioaddr, uint32_t val)
    224  1.1  thorpej {
    225  1.2  thorpej 	uint32_t *port = alpha_pci_io_swiz(ioaddr, 3);
    226  1.1  thorpej 
    227  1.1  thorpej 	*port = val;
    228  1.1  thorpej 	alpha_mb();
    229  1.1  thorpej }
    230  1.1  thorpej 
    231  1.1  thorpej /*
    232  1.1  thorpej  * The following functions are used only on EV56 and greater CPUs,
    233  1.1  thorpej  * and the assembler requires going to EV56 mode in order to emit
    234  1.1  thorpej  * these instructions.
    235  1.1  thorpej  */
    236  1.1  thorpej __asm(".arch ev56");
    237  1.1  thorpej 
    238  1.2  thorpej uint8_t
    239  1.2  thorpej alpha_pci_io_bwx_inb(bus_addr_t ioaddr)
    240  1.1  thorpej {
    241  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    242  1.2  thorpej 	uint8_t *port = (uint8_t *) (abw->abw_addr +
    243  1.1  thorpej 	    (ioaddr - abw->abw_abst.abst_bus_start));
    244  1.1  thorpej 
    245  1.1  thorpej 	alpha_mb();
    246  1.1  thorpej 
    247  1.1  thorpej 	return (alpha_ldbu(port));
    248  1.1  thorpej }
    249  1.1  thorpej 
    250  1.2  thorpej uint16_t
    251  1.2  thorpej alpha_pci_io_bwx_inw(bus_addr_t ioaddr)
    252  1.1  thorpej {
    253  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    254  1.2  thorpej 	/* LINTED */
    255  1.2  thorpej 	uint16_t *port = (uint16_t *) (abw->abw_addr +
    256  1.1  thorpej 	    (ioaddr - abw->abw_abst.abst_bus_start));
    257  1.1  thorpej 
    258  1.1  thorpej 	alpha_mb();
    259  1.1  thorpej 
    260  1.1  thorpej 	return (alpha_ldwu(port));
    261  1.1  thorpej }
    262  1.1  thorpej 
    263  1.2  thorpej uint32_t
    264  1.2  thorpej alpha_pci_io_bwx_inl(bus_addr_t ioaddr)
    265  1.1  thorpej {
    266  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    267  1.2  thorpej 	/* LINTED */
    268  1.2  thorpej 	uint32_t *port = (uint32_t *) (abw->abw_addr +
    269  1.1  thorpej 	    (ioaddr - abw->abw_abst.abst_bus_start));
    270  1.1  thorpej 
    271  1.1  thorpej 	alpha_mb();
    272  1.1  thorpej 
    273  1.1  thorpej 	return (*port);
    274  1.1  thorpej }
    275  1.1  thorpej 
    276  1.1  thorpej void
    277  1.2  thorpej alpha_pci_io_bwx_outb(bus_addr_t ioaddr, uint8_t val)
    278  1.1  thorpej {
    279  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    280  1.2  thorpej 	uint8_t *port = (uint8_t *) (abw->abw_addr +
    281  1.1  thorpej 	    (ioaddr - abw->abw_abst.abst_bus_start));
    282  1.1  thorpej 
    283  1.1  thorpej 	alpha_stb(port, val);
    284  1.1  thorpej 	alpha_mb();
    285  1.1  thorpej }
    286  1.1  thorpej 
    287  1.1  thorpej void
    288  1.2  thorpej alpha_pci_io_bwx_outw(bus_addr_t ioaddr, uint16_t val)
    289  1.1  thorpej {
    290  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    291  1.2  thorpej 	/* LINTED */
    292  1.2  thorpej 	uint16_t *port = (uint16_t *) (abw->abw_addr +
    293  1.1  thorpej 	    (ioaddr - abw->abw_abst.abst_bus_start));
    294  1.1  thorpej 
    295  1.1  thorpej 	alpha_stw(port, val);
    296  1.1  thorpej 	alpha_mb();
    297  1.1  thorpej }
    298  1.1  thorpej 
    299  1.1  thorpej void
    300  1.2  thorpej alpha_pci_io_bwx_outl(bus_addr_t ioaddr, uint32_t val)
    301  1.1  thorpej {
    302  1.1  thorpej 	struct alpha_bus_window *abw = alpha_pci_io_findwindow(ioaddr);
    303  1.2  thorpej 	/* LINTED */
    304  1.2  thorpej 	uint32_t *port = (uint32_t *) (abw->abw_addr +
    305  1.1  thorpej 	    (ioaddr - abw->abw_abst.abst_bus_start));
    306  1.1  thorpej 
    307  1.1  thorpej 	*port = val;
    308  1.1  thorpej 	alpha_mb();
    309  1.1  thorpej }
    310