1*605f564fSmsaitoh /* $NetBSD: bonito_pci.c,v 1.12 2015/10/02 05:22:51 msaitoh Exp $ */
24b23ee5dSthorpej
34b23ee5dSthorpej /*-
44b23ee5dSthorpej * Copyright (c) 2001 The NetBSD Foundation, Inc.
54b23ee5dSthorpej * All rights reserved.
64b23ee5dSthorpej *
74b23ee5dSthorpej * This code is derived from software contributed to The NetBSD Foundation
84b23ee5dSthorpej * by Jason R. Thorpe.
94b23ee5dSthorpej *
104b23ee5dSthorpej * Redistribution and use in source and binary forms, with or without
114b23ee5dSthorpej * modification, are permitted provided that the following conditions
124b23ee5dSthorpej * are met:
134b23ee5dSthorpej * 1. Redistributions of source code must retain the above copyright
144b23ee5dSthorpej * notice, this list of conditions and the following disclaimer.
154b23ee5dSthorpej * 2. Redistributions in binary form must reproduce the above copyright
164b23ee5dSthorpej * notice, this list of conditions and the following disclaimer in the
174b23ee5dSthorpej * documentation and/or other materials provided with the distribution.
184b23ee5dSthorpej *
194b23ee5dSthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204b23ee5dSthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214b23ee5dSthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224b23ee5dSthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
234b23ee5dSthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244b23ee5dSthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254b23ee5dSthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264b23ee5dSthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274b23ee5dSthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284b23ee5dSthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294b23ee5dSthorpej * POSSIBILITY OF SUCH DAMAGE.
304b23ee5dSthorpej */
314b23ee5dSthorpej
324b23ee5dSthorpej /*
334b23ee5dSthorpej * PCI configuration space support for the Algorithmics BONITO
344b23ee5dSthorpej * MIPS PCI and memory controller.
354b23ee5dSthorpej */
364b23ee5dSthorpej
374b2744bfSlukem #include <sys/cdefs.h>
38*605f564fSmsaitoh __KERNEL_RCSID(0, "$NetBSD: bonito_pci.c,v 1.12 2015/10/02 05:22:51 msaitoh Exp $");
394b2744bfSlukem
404b23ee5dSthorpej #include <sys/param.h>
41a0a8f335Sdyoung #include <sys/bus.h>
42b6185cbdSmatt #include <sys/device.h>
43b6185cbdSmatt #include <sys/intr.h>
44b6185cbdSmatt #include <sys/systm.h>
45b6185cbdSmatt
46b6185cbdSmatt #include <mips/locore.h>
474b23ee5dSthorpej
484b23ee5dSthorpej #include <dev/pci/pcireg.h>
494b23ee5dSthorpej #include <dev/pci/pcivar.h>
504b23ee5dSthorpej
514b23ee5dSthorpej #include <mips/bonito/bonitoreg.h>
524b23ee5dSthorpej #include <mips/bonito/bonitovar.h>
534b23ee5dSthorpej
544b23ee5dSthorpej /*
554b23ee5dSthorpej * Bonito systems are always single-processor, so this is sufficient.
564b23ee5dSthorpej */
574b23ee5dSthorpej #define PCI_CONF_LOCK(s) (s) = splhigh()
584b23ee5dSthorpej #define PCI_CONF_UNLOCK(s) splx((s))
594b23ee5dSthorpej
60ead615ccSmatt void bonito_attach_hook(device_t, device_t,
614b23ee5dSthorpej struct pcibus_attach_args *);
624b23ee5dSthorpej int bonito_bus_maxdevs(void *, int);
634b23ee5dSthorpej pcitag_t bonito_make_tag(void *, int, int, int);
644b23ee5dSthorpej void bonito_decompose_tag(void *, pcitag_t, int *, int *, int *);
654b23ee5dSthorpej pcireg_t bonito_conf_read(void *, pcitag_t, int);
664b23ee5dSthorpej void bonito_conf_write(void *, pcitag_t, int, pcireg_t);
674b23ee5dSthorpej
684b23ee5dSthorpej void
bonito_pci_init(pci_chipset_tag_t pc,const struct bonito_config * bc)69ad05e3fbSbouyer bonito_pci_init(pci_chipset_tag_t pc, const struct bonito_config *bc)
704b23ee5dSthorpej {
714b23ee5dSthorpej
72ad05e3fbSbouyer pc->pc_conf_v = __UNCONST(bc);
73ad05e3fbSbouyer if (bc->bc_attach_hook != NULL)
74ad05e3fbSbouyer pc->pc_attach_hook = bc->bc_attach_hook;
75ad05e3fbSbouyer else
764b23ee5dSthorpej pc->pc_attach_hook = bonito_attach_hook;
774b23ee5dSthorpej pc->pc_bus_maxdevs = bonito_bus_maxdevs;
784b23ee5dSthorpej pc->pc_make_tag = bonito_make_tag;
794b23ee5dSthorpej pc->pc_decompose_tag = bonito_decompose_tag;
804b23ee5dSthorpej pc->pc_conf_read = bonito_conf_read;
814b23ee5dSthorpej pc->pc_conf_write = bonito_conf_write;
824b23ee5dSthorpej }
834b23ee5dSthorpej
844b23ee5dSthorpej void
bonito_attach_hook(device_t parent,device_t self,struct pcibus_attach_args * pba)85ead615ccSmatt bonito_attach_hook(device_t parent, device_t self,
864b23ee5dSthorpej struct pcibus_attach_args *pba)
874b23ee5dSthorpej {
884b23ee5dSthorpej }
894b23ee5dSthorpej
904b23ee5dSthorpej int
bonito_bus_maxdevs(void * v,int busno)914b23ee5dSthorpej bonito_bus_maxdevs(void *v, int busno)
924b23ee5dSthorpej {
934b23ee5dSthorpej
944b23ee5dSthorpej return (32);
954b23ee5dSthorpej }
964b23ee5dSthorpej
974b23ee5dSthorpej pcitag_t
bonito_make_tag(void * v,int b,int d,int f)984b23ee5dSthorpej bonito_make_tag(void *v, int b, int d, int f)
994b23ee5dSthorpej {
1004b23ee5dSthorpej
1014b23ee5dSthorpej return ((b << 16) | (d << 11) | (f << 8));
1024b23ee5dSthorpej }
1034b23ee5dSthorpej
1044b23ee5dSthorpej void
bonito_decompose_tag(void * v,pcitag_t tag,int * bp,int * dp,int * fp)1054b23ee5dSthorpej bonito_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
1064b23ee5dSthorpej {
1074b23ee5dSthorpej
1084b23ee5dSthorpej if (bp != NULL)
1094b23ee5dSthorpej *bp = (tag >> 16) & 0xff;
1104b23ee5dSthorpej if (dp != NULL)
1114b23ee5dSthorpej *dp = (tag >> 11) & 0x1f;
1124b23ee5dSthorpej if (fp != NULL)
1134b23ee5dSthorpej *fp = (tag >> 8) & 0x7;
1144b23ee5dSthorpej }
1154b23ee5dSthorpej
116d9fa6bc2Smatt static bool
bonito_conf_addr(struct bonito_config * bc,pcitag_t tag,int offset,u_int32_t * cfgoff,u_int32_t * pcimap_cfg)1174b23ee5dSthorpej bonito_conf_addr(struct bonito_config *bc, pcitag_t tag, int offset,
1184b23ee5dSthorpej u_int32_t *cfgoff, u_int32_t *pcimap_cfg)
1194b23ee5dSthorpej {
1204b23ee5dSthorpej int b, d, f;
1214b23ee5dSthorpej
122*605f564fSmsaitoh if ((unsigned int)offset >= PCI_CONF_SIZE)
123*605f564fSmsaitoh return true;
124*605f564fSmsaitoh
1254b23ee5dSthorpej bonito_decompose_tag(bc, tag, &b, &d, &f);
1264b23ee5dSthorpej
1274b23ee5dSthorpej if (b == 0) {
1284b23ee5dSthorpej if (d > (31 - bc->bc_adbase))
129d9fa6bc2Smatt return true;
130d9fa6bc2Smatt *cfgoff = (1UL << (d + bc->bc_adbase)) | (f << 8) | offset;
1314b23ee5dSthorpej *pcimap_cfg = 0;
1324b23ee5dSthorpej } else {
1334b23ee5dSthorpej *cfgoff = tag | offset;
1344b23ee5dSthorpej *pcimap_cfg = BONITO_PCIMAPCFG_TYPE1;
1354b23ee5dSthorpej }
1364b23ee5dSthorpej
137d9fa6bc2Smatt return false;
1384b23ee5dSthorpej }
1394b23ee5dSthorpej
1404b23ee5dSthorpej pcireg_t
bonito_conf_read(void * v,pcitag_t tag,int offset)1414b23ee5dSthorpej bonito_conf_read(void *v, pcitag_t tag, int offset)
1424b23ee5dSthorpej {
1434b23ee5dSthorpej struct bonito_config *bc = v;
1444b23ee5dSthorpej pcireg_t data;
14556d29395Smrg u_int32_t cfgoff, pcimap_cfg;
1464b23ee5dSthorpej int s;
1474b23ee5dSthorpej
1484b23ee5dSthorpej if (bonito_conf_addr(bc, tag, offset, &cfgoff, &pcimap_cfg))
1494b23ee5dSthorpej return ((pcireg_t) -1);
1504b23ee5dSthorpej
1514b23ee5dSthorpej PCI_CONF_LOCK(s);
1524b23ee5dSthorpej
1534b23ee5dSthorpej /* clear aborts */
1544b23ee5dSthorpej REGVAL(BONITO_PCICMD) |=
1554b23ee5dSthorpej PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
1564b23ee5dSthorpej
1574b23ee5dSthorpej /* high 16 bits of address go into PciMapCfg register */
1584b23ee5dSthorpej REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
1594b23ee5dSthorpej
1604b23ee5dSthorpej wbflush();
161fd2320f3Ssimonb /* Issue a read to make sure the write is posted */
16256d29395Smrg (void)REGVAL(BONITO_PCIMAP_CFG);
1634b23ee5dSthorpej
1644b23ee5dSthorpej /* low 16 bits of address are offset into config space */
1654b23ee5dSthorpej data = REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc));
1664b23ee5dSthorpej
1674b23ee5dSthorpej /* check for error */
1684b23ee5dSthorpej if (REGVAL(BONITO_PCICMD) &
1694b23ee5dSthorpej (PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT))
1704b23ee5dSthorpej data = (pcireg_t) -1;
1714b23ee5dSthorpej
1724b23ee5dSthorpej PCI_CONF_UNLOCK(s);
1734b23ee5dSthorpej
1744b23ee5dSthorpej return (data);
1754b23ee5dSthorpej }
1764b23ee5dSthorpej
1774b23ee5dSthorpej void
bonito_conf_write(void * v,pcitag_t tag,int offset,pcireg_t data)1784b23ee5dSthorpej bonito_conf_write(void *v, pcitag_t tag, int offset, pcireg_t data)
1794b23ee5dSthorpej {
1804b23ee5dSthorpej struct bonito_config *vt = v;
18156d29395Smrg u_int32_t cfgoff, pcimap_cfg;
1824b23ee5dSthorpej int s;
1834b23ee5dSthorpej
1844b23ee5dSthorpej if (bonito_conf_addr(vt, tag, offset, &cfgoff, &pcimap_cfg))
1854b23ee5dSthorpej panic("bonito_conf_write");
1864b23ee5dSthorpej
1874b23ee5dSthorpej PCI_CONF_LOCK(s);
1884b23ee5dSthorpej
1894b23ee5dSthorpej /* clear aborts */
1904b23ee5dSthorpej REGVAL(BONITO_PCICMD) |=
1914b23ee5dSthorpej PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
1924b23ee5dSthorpej
1934b23ee5dSthorpej /* high 16 bits of address go into PciMapCfg register */
1944b23ee5dSthorpej REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
1954b23ee5dSthorpej
1964b23ee5dSthorpej wbflush();
197fd2320f3Ssimonb /* Issue a read to make sure the write is posted */
19856d29395Smrg (void)REGVAL(BONITO_PCIMAP_CFG);
1994b23ee5dSthorpej
2004b23ee5dSthorpej /* low 16 bits of address are offset into config space */
2014b23ee5dSthorpej REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc)) = data;
2024b23ee5dSthorpej
2034b23ee5dSthorpej PCI_CONF_UNLOCK(s);
2044b23ee5dSthorpej }
205