xref: /openbmc/linux/arch/mips/pci/ops-lantiq.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e47d4889SJohn Crispin /*
3e47d4889SJohn Crispin  *
497b92108SJohn Crispin  *  Copyright (C) 2010 John Crispin <john@phrozen.org>
5e47d4889SJohn Crispin  */
6e47d4889SJohn Crispin 
7e47d4889SJohn Crispin #include <linux/types.h>
8e47d4889SJohn Crispin #include <linux/pci.h>
9e47d4889SJohn Crispin #include <linux/kernel.h>
10e47d4889SJohn Crispin #include <linux/delay.h>
11e47d4889SJohn Crispin #include <linux/mm.h>
12e47d4889SJohn Crispin #include <asm/addrspace.h>
13e47d4889SJohn Crispin #include <linux/vmalloc.h>
14e47d4889SJohn Crispin 
15e47d4889SJohn Crispin #include <lantiq_soc.h>
16e47d4889SJohn Crispin 
17e47d4889SJohn Crispin #include "pci-lantiq.h"
18e47d4889SJohn Crispin 
19e47d4889SJohn Crispin #define LTQ_PCI_CFG_BUSNUM_SHF 16
20e47d4889SJohn Crispin #define LTQ_PCI_CFG_DEVNUM_SHF 11
21e47d4889SJohn Crispin #define LTQ_PCI_CFG_FUNNUM_SHF 8
22e47d4889SJohn Crispin 
23e47d4889SJohn Crispin #define PCI_ACCESS_READ	 0
24e47d4889SJohn Crispin #define PCI_ACCESS_WRITE 1
25e47d4889SJohn Crispin 
ltq_pci_config_access(unsigned char access_type,struct pci_bus * bus,unsigned int devfn,unsigned int where,u32 * data)26e47d4889SJohn Crispin static int ltq_pci_config_access(unsigned char access_type, struct pci_bus *bus,
27e47d4889SJohn Crispin 	unsigned int devfn, unsigned int where, u32 *data)
28e47d4889SJohn Crispin {
29e47d4889SJohn Crispin 	unsigned long cfg_base;
30e47d4889SJohn Crispin 	unsigned long flags;
31e47d4889SJohn Crispin 	u32 temp;
32e47d4889SJohn Crispin 
33e47d4889SJohn Crispin 	/* we support slot from 0 to 15 dev_fn & 0x68 (AD29) is the
34e47d4889SJohn Crispin 	   SoC itself */
35e47d4889SJohn Crispin 	if ((bus->number != 0) || ((devfn & 0xf8) > 0x78)
36e47d4889SJohn Crispin 		|| ((devfn & 0xf8) == 0) || ((devfn & 0xf8) == 0x68))
37e47d4889SJohn Crispin 		return 1;
38e47d4889SJohn Crispin 
39e47d4889SJohn Crispin 	spin_lock_irqsave(&ebu_lock, flags);
40e47d4889SJohn Crispin 
41e47d4889SJohn Crispin 	cfg_base = (unsigned long) ltq_pci_mapped_cfg;
42e47d4889SJohn Crispin 	cfg_base |= (bus->number << LTQ_PCI_CFG_BUSNUM_SHF) | (devfn <<
43e47d4889SJohn Crispin 			LTQ_PCI_CFG_FUNNUM_SHF) | (where & ~0x3);
44e47d4889SJohn Crispin 
45e47d4889SJohn Crispin 	/* Perform access */
46e47d4889SJohn Crispin 	if (access_type == PCI_ACCESS_WRITE) {
47e47d4889SJohn Crispin 		ltq_w32(swab32(*data), ((u32 *)cfg_base));
48e47d4889SJohn Crispin 	} else {
49e47d4889SJohn Crispin 		*data = ltq_r32(((u32 *)(cfg_base)));
50e47d4889SJohn Crispin 		*data = swab32(*data);
51e47d4889SJohn Crispin 	}
52e47d4889SJohn Crispin 	wmb();
53e47d4889SJohn Crispin 
54e47d4889SJohn Crispin 	/* clean possible Master abort */
55e47d4889SJohn Crispin 	cfg_base = (unsigned long) ltq_pci_mapped_cfg;
56e47d4889SJohn Crispin 	cfg_base |= (0x0 << LTQ_PCI_CFG_FUNNUM_SHF) + 4;
57e47d4889SJohn Crispin 	temp = ltq_r32(((u32 *)(cfg_base)));
58e47d4889SJohn Crispin 	temp = swab32(temp);
59e47d4889SJohn Crispin 	cfg_base = (unsigned long) ltq_pci_mapped_cfg;
60e47d4889SJohn Crispin 	cfg_base |= (0x68 << LTQ_PCI_CFG_FUNNUM_SHF) + 4;
61e47d4889SJohn Crispin 	ltq_w32(temp, ((u32 *)cfg_base));
62e47d4889SJohn Crispin 
63e47d4889SJohn Crispin 	spin_unlock_irqrestore(&ebu_lock, flags);
64e47d4889SJohn Crispin 
65e47d4889SJohn Crispin 	if (((*data) == 0xffffffff) && (access_type == PCI_ACCESS_READ))
66e47d4889SJohn Crispin 		return 1;
67e47d4889SJohn Crispin 
68e47d4889SJohn Crispin 	return 0;
69e47d4889SJohn Crispin }
70e47d4889SJohn Crispin 
ltq_pci_read_config_dword(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)71e47d4889SJohn Crispin int ltq_pci_read_config_dword(struct pci_bus *bus, unsigned int devfn,
72e47d4889SJohn Crispin 	int where, int size, u32 *val)
73e47d4889SJohn Crispin {
74e47d4889SJohn Crispin 	u32 data = 0;
75e47d4889SJohn Crispin 
76e47d4889SJohn Crispin 	if (ltq_pci_config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
77e47d4889SJohn Crispin 		return PCIBIOS_DEVICE_NOT_FOUND;
78e47d4889SJohn Crispin 
79e47d4889SJohn Crispin 	if (size == 1)
80e47d4889SJohn Crispin 		*val = (data >> ((where & 3) << 3)) & 0xff;
81e47d4889SJohn Crispin 	else if (size == 2)
82e47d4889SJohn Crispin 		*val = (data >> ((where & 3) << 3)) & 0xffff;
83e47d4889SJohn Crispin 	else
84e47d4889SJohn Crispin 		*val = data;
85e47d4889SJohn Crispin 
86e47d4889SJohn Crispin 	return PCIBIOS_SUCCESSFUL;
87e47d4889SJohn Crispin }
88e47d4889SJohn Crispin 
ltq_pci_write_config_dword(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)89e47d4889SJohn Crispin int ltq_pci_write_config_dword(struct pci_bus *bus, unsigned int devfn,
90e47d4889SJohn Crispin 	int where, int size, u32 val)
91e47d4889SJohn Crispin {
92e47d4889SJohn Crispin 	u32 data = 0;
93e47d4889SJohn Crispin 
94e47d4889SJohn Crispin 	if (size == 4) {
95e47d4889SJohn Crispin 		data = val;
96e47d4889SJohn Crispin 	} else {
97e47d4889SJohn Crispin 		if (ltq_pci_config_access(PCI_ACCESS_READ, bus,
98e47d4889SJohn Crispin 				devfn, where, &data))
99e47d4889SJohn Crispin 			return PCIBIOS_DEVICE_NOT_FOUND;
100e47d4889SJohn Crispin 
101e47d4889SJohn Crispin 		if (size == 1)
102e47d4889SJohn Crispin 			data = (data & ~(0xff << ((where & 3) << 3))) |
103e47d4889SJohn Crispin 				(val << ((where & 3) << 3));
104e47d4889SJohn Crispin 		else if (size == 2)
105e47d4889SJohn Crispin 			data = (data & ~(0xffff << ((where & 3) << 3))) |
106e47d4889SJohn Crispin 				(val << ((where & 3) << 3));
107e47d4889SJohn Crispin 	}
108e47d4889SJohn Crispin 
109e47d4889SJohn Crispin 	if (ltq_pci_config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
110e47d4889SJohn Crispin 		return PCIBIOS_DEVICE_NOT_FOUND;
111e47d4889SJohn Crispin 
112e47d4889SJohn Crispin 	return PCIBIOS_SUCCESSFUL;
113e47d4889SJohn Crispin }
114