xref: /openbmc/linux/arch/mips/pci/ops-gt64xxx_pci0.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*41173abcSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2252161ecSYoichi Yuasa /*
3252161ecSYoichi Yuasa  * Copyright (C) 1999, 2000, 2004  MIPS Technologies, Inc.
4252161ecSYoichi Yuasa  *	All rights reserved.
5252161ecSYoichi Yuasa  *	Authors: Carsten Langgaard <carstenl@mips.com>
6252161ecSYoichi Yuasa  *		 Maciej W. Rozycki <macro@mips.com>
7252161ecSYoichi Yuasa  */
8252161ecSYoichi Yuasa #include <linux/types.h>
9252161ecSYoichi Yuasa #include <linux/pci.h>
10252161ecSYoichi Yuasa #include <linux/kernel.h>
11252161ecSYoichi Yuasa 
12252161ecSYoichi Yuasa #include <asm/gt64120.h>
13252161ecSYoichi Yuasa 
14252161ecSYoichi Yuasa #define PCI_ACCESS_READ	 0
15252161ecSYoichi Yuasa #define PCI_ACCESS_WRITE 1
16252161ecSYoichi Yuasa 
17252161ecSYoichi Yuasa /*
18252161ecSYoichi Yuasa  *  PCI configuration cycle AD bus definition
19252161ecSYoichi Yuasa  */
20252161ecSYoichi Yuasa /* Type 0 */
21252161ecSYoichi Yuasa #define PCI_CFG_TYPE0_REG_SHF		0
22252161ecSYoichi Yuasa #define PCI_CFG_TYPE0_FUNC_SHF		8
23252161ecSYoichi Yuasa 
24252161ecSYoichi Yuasa /* Type 1 */
25252161ecSYoichi Yuasa #define PCI_CFG_TYPE1_REG_SHF		0
26252161ecSYoichi Yuasa #define PCI_CFG_TYPE1_FUNC_SHF		8
27252161ecSYoichi Yuasa #define PCI_CFG_TYPE1_DEV_SHF		11
28252161ecSYoichi Yuasa #define PCI_CFG_TYPE1_BUS_SHF		16
29252161ecSYoichi Yuasa 
gt64xxx_pci0_pcibios_config_access(unsigned char access_type,struct pci_bus * bus,unsigned int devfn,int where,u32 * data)30252161ecSYoichi Yuasa static int gt64xxx_pci0_pcibios_config_access(unsigned char access_type,
31252161ecSYoichi Yuasa 		struct pci_bus *bus, unsigned int devfn, int where, u32 * data)
32252161ecSYoichi Yuasa {
33252161ecSYoichi Yuasa 	unsigned char busnum = bus->number;
34252161ecSYoichi Yuasa 	u32 intr;
35252161ecSYoichi Yuasa 
36252161ecSYoichi Yuasa 	if ((busnum == 0) && (devfn >= PCI_DEVFN(31, 0)))
37252161ecSYoichi Yuasa 		return -1;	/* Because of a bug in the galileo (for slot 31). */
38252161ecSYoichi Yuasa 
39252161ecSYoichi Yuasa 	/* Clear cause register bits */
40252161ecSYoichi Yuasa 	GT_WRITE(GT_INTRCAUSE_OFS, ~(GT_INTRCAUSE_MASABORT0_BIT |
41252161ecSYoichi Yuasa 				     GT_INTRCAUSE_TARABORT0_BIT));
42252161ecSYoichi Yuasa 
43252161ecSYoichi Yuasa 	/* Setup address */
44252161ecSYoichi Yuasa 	GT_WRITE(GT_PCI0_CFGADDR_OFS,
45252161ecSYoichi Yuasa 		 (busnum << GT_PCI0_CFGADDR_BUSNUM_SHF) |
46252161ecSYoichi Yuasa 		 (devfn << GT_PCI0_CFGADDR_FUNCTNUM_SHF) |
47252161ecSYoichi Yuasa 		 ((where / 4) << GT_PCI0_CFGADDR_REGNUM_SHF) |
48252161ecSYoichi Yuasa 		 GT_PCI0_CFGADDR_CONFIGEN_BIT);
49252161ecSYoichi Yuasa 
50252161ecSYoichi Yuasa 	if (access_type == PCI_ACCESS_WRITE) {
51252161ecSYoichi Yuasa 		if (busnum == 0 && PCI_SLOT(devfn) == 0) {
52252161ecSYoichi Yuasa 			/*
53252161ecSYoichi Yuasa 			 * The Galileo system controller is acting
54252161ecSYoichi Yuasa 			 * differently than other devices.
55252161ecSYoichi Yuasa 			 */
56252161ecSYoichi Yuasa 			GT_WRITE(GT_PCI0_CFGDATA_OFS, *data);
57252161ecSYoichi Yuasa 		} else
58252161ecSYoichi Yuasa 			__GT_WRITE(GT_PCI0_CFGDATA_OFS, *data);
59252161ecSYoichi Yuasa 	} else {
60252161ecSYoichi Yuasa 		if (busnum == 0 && PCI_SLOT(devfn) == 0) {
61252161ecSYoichi Yuasa 			/*
62252161ecSYoichi Yuasa 			 * The Galileo system controller is acting
63252161ecSYoichi Yuasa 			 * differently than other devices.
64252161ecSYoichi Yuasa 			 */
65252161ecSYoichi Yuasa 			*data = GT_READ(GT_PCI0_CFGDATA_OFS);
66252161ecSYoichi Yuasa 		} else
67252161ecSYoichi Yuasa 			*data = __GT_READ(GT_PCI0_CFGDATA_OFS);
68252161ecSYoichi Yuasa 	}
69252161ecSYoichi Yuasa 
70252161ecSYoichi Yuasa 	/* Check for master or target abort */
71252161ecSYoichi Yuasa 	intr = GT_READ(GT_INTRCAUSE_OFS);
72252161ecSYoichi Yuasa 
73252161ecSYoichi Yuasa 	if (intr & (GT_INTRCAUSE_MASABORT0_BIT | GT_INTRCAUSE_TARABORT0_BIT)) {
74252161ecSYoichi Yuasa 		/* Error occurred */
75252161ecSYoichi Yuasa 
76252161ecSYoichi Yuasa 		/* Clear bits */
77252161ecSYoichi Yuasa 		GT_WRITE(GT_INTRCAUSE_OFS, ~(GT_INTRCAUSE_MASABORT0_BIT |
78252161ecSYoichi Yuasa 					     GT_INTRCAUSE_TARABORT0_BIT));
79252161ecSYoichi Yuasa 
80252161ecSYoichi Yuasa 		return -1;
81252161ecSYoichi Yuasa 	}
82252161ecSYoichi Yuasa 
83252161ecSYoichi Yuasa 	return 0;
84252161ecSYoichi Yuasa }
85252161ecSYoichi Yuasa 
86252161ecSYoichi Yuasa 
87252161ecSYoichi Yuasa /*
88252161ecSYoichi Yuasa  * We can't address 8 and 16 bit words directly.  Instead we have to
89252161ecSYoichi Yuasa  * read/write a 32bit word and mask/modify the data we actually want.
90252161ecSYoichi Yuasa  */
gt64xxx_pci0_pcibios_read(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)91252161ecSYoichi Yuasa static int gt64xxx_pci0_pcibios_read(struct pci_bus *bus, unsigned int devfn,
92252161ecSYoichi Yuasa 		int where, int size, u32 * val)
93252161ecSYoichi Yuasa {
94252161ecSYoichi Yuasa 	u32 data = 0;
95252161ecSYoichi Yuasa 
96252161ecSYoichi Yuasa 	if (gt64xxx_pci0_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
97252161ecSYoichi Yuasa 					       where, &data))
98252161ecSYoichi Yuasa 		return PCIBIOS_DEVICE_NOT_FOUND;
99252161ecSYoichi Yuasa 
100252161ecSYoichi Yuasa 	if (size == 1)
101252161ecSYoichi Yuasa 		*val = (data >> ((where & 3) << 3)) & 0xff;
102252161ecSYoichi Yuasa 	else if (size == 2)
103252161ecSYoichi Yuasa 		*val = (data >> ((where & 3) << 3)) & 0xffff;
104252161ecSYoichi Yuasa 	else
105252161ecSYoichi Yuasa 		*val = data;
106252161ecSYoichi Yuasa 
107252161ecSYoichi Yuasa 	return PCIBIOS_SUCCESSFUL;
108252161ecSYoichi Yuasa }
109252161ecSYoichi Yuasa 
gt64xxx_pci0_pcibios_write(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)110252161ecSYoichi Yuasa static int gt64xxx_pci0_pcibios_write(struct pci_bus *bus, unsigned int devfn,
111252161ecSYoichi Yuasa 		int where, int size, u32 val)
112252161ecSYoichi Yuasa {
113252161ecSYoichi Yuasa 	u32 data = 0;
114252161ecSYoichi Yuasa 
115252161ecSYoichi Yuasa 	if (size == 4)
116252161ecSYoichi Yuasa 		data = val;
117252161ecSYoichi Yuasa 	else {
118252161ecSYoichi Yuasa 		if (gt64xxx_pci0_pcibios_config_access(PCI_ACCESS_READ, bus,
119252161ecSYoichi Yuasa 						       devfn, where, &data))
120252161ecSYoichi Yuasa 			return PCIBIOS_DEVICE_NOT_FOUND;
121252161ecSYoichi Yuasa 
122252161ecSYoichi Yuasa 		if (size == 1)
123252161ecSYoichi Yuasa 			data = (data & ~(0xff << ((where & 3) << 3))) |
124252161ecSYoichi Yuasa 				(val << ((where & 3) << 3));
125252161ecSYoichi Yuasa 		else if (size == 2)
126252161ecSYoichi Yuasa 			data = (data & ~(0xffff << ((where & 3) << 3))) |
127252161ecSYoichi Yuasa 				(val << ((where & 3) << 3));
128252161ecSYoichi Yuasa 	}
129252161ecSYoichi Yuasa 
130252161ecSYoichi Yuasa 	if (gt64xxx_pci0_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn,
131252161ecSYoichi Yuasa 					       where, &data))
132252161ecSYoichi Yuasa 		return PCIBIOS_DEVICE_NOT_FOUND;
133252161ecSYoichi Yuasa 
134252161ecSYoichi Yuasa 	return PCIBIOS_SUCCESSFUL;
135252161ecSYoichi Yuasa }
136252161ecSYoichi Yuasa 
137252161ecSYoichi Yuasa struct pci_ops gt64xxx_pci0_ops = {
138252161ecSYoichi Yuasa 	.read	= gt64xxx_pci0_pcibios_read,
139252161ecSYoichi Yuasa 	.write	= gt64xxx_pci0_pcibios_write
140252161ecSYoichi Yuasa };
141