18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *  Copyright (C) 2010 John Crispin <john@phrozen.org>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/types.h>
88c2ecf20Sopenharmony_ci#include <linux/pci.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/mm.h>
128c2ecf20Sopenharmony_ci#include <asm/addrspace.h>
138c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <lantiq_soc.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "pci-lantiq.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define LTQ_PCI_CFG_BUSNUM_SHF 16
208c2ecf20Sopenharmony_ci#define LTQ_PCI_CFG_DEVNUM_SHF 11
218c2ecf20Sopenharmony_ci#define LTQ_PCI_CFG_FUNNUM_SHF 8
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define PCI_ACCESS_READ	 0
248c2ecf20Sopenharmony_ci#define PCI_ACCESS_WRITE 1
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic int ltq_pci_config_access(unsigned char access_type, struct pci_bus *bus,
278c2ecf20Sopenharmony_ci	unsigned int devfn, unsigned int where, u32 *data)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	unsigned long cfg_base;
308c2ecf20Sopenharmony_ci	unsigned long flags;
318c2ecf20Sopenharmony_ci	u32 temp;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	/* we support slot from 0 to 15 dev_fn & 0x68 (AD29) is the
348c2ecf20Sopenharmony_ci	   SoC itself */
358c2ecf20Sopenharmony_ci	if ((bus->number != 0) || ((devfn & 0xf8) > 0x78)
368c2ecf20Sopenharmony_ci		|| ((devfn & 0xf8) == 0) || ((devfn & 0xf8) == 0x68))
378c2ecf20Sopenharmony_ci		return 1;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ebu_lock, flags);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	cfg_base = (unsigned long) ltq_pci_mapped_cfg;
428c2ecf20Sopenharmony_ci	cfg_base |= (bus->number << LTQ_PCI_CFG_BUSNUM_SHF) | (devfn <<
438c2ecf20Sopenharmony_ci			LTQ_PCI_CFG_FUNNUM_SHF) | (where & ~0x3);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	/* Perform access */
468c2ecf20Sopenharmony_ci	if (access_type == PCI_ACCESS_WRITE) {
478c2ecf20Sopenharmony_ci		ltq_w32(swab32(*data), ((u32 *)cfg_base));
488c2ecf20Sopenharmony_ci	} else {
498c2ecf20Sopenharmony_ci		*data = ltq_r32(((u32 *)(cfg_base)));
508c2ecf20Sopenharmony_ci		*data = swab32(*data);
518c2ecf20Sopenharmony_ci	}
528c2ecf20Sopenharmony_ci	wmb();
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	/* clean possible Master abort */
558c2ecf20Sopenharmony_ci	cfg_base = (unsigned long) ltq_pci_mapped_cfg;
568c2ecf20Sopenharmony_ci	cfg_base |= (0x0 << LTQ_PCI_CFG_FUNNUM_SHF) + 4;
578c2ecf20Sopenharmony_ci	temp = ltq_r32(((u32 *)(cfg_base)));
588c2ecf20Sopenharmony_ci	temp = swab32(temp);
598c2ecf20Sopenharmony_ci	cfg_base = (unsigned long) ltq_pci_mapped_cfg;
608c2ecf20Sopenharmony_ci	cfg_base |= (0x68 << LTQ_PCI_CFG_FUNNUM_SHF) + 4;
618c2ecf20Sopenharmony_ci	ltq_w32(temp, ((u32 *)cfg_base));
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ebu_lock, flags);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	if (((*data) == 0xffffffff) && (access_type == PCI_ACCESS_READ))
668c2ecf20Sopenharmony_ci		return 1;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	return 0;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ciint ltq_pci_read_config_dword(struct pci_bus *bus, unsigned int devfn,
728c2ecf20Sopenharmony_ci	int where, int size, u32 *val)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	u32 data = 0;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	if (ltq_pci_config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
778c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (size == 1)
808c2ecf20Sopenharmony_ci		*val = (data >> ((where & 3) << 3)) & 0xff;
818c2ecf20Sopenharmony_ci	else if (size == 2)
828c2ecf20Sopenharmony_ci		*val = (data >> ((where & 3) << 3)) & 0xffff;
838c2ecf20Sopenharmony_ci	else
848c2ecf20Sopenharmony_ci		*val = data;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return PCIBIOS_SUCCESSFUL;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciint ltq_pci_write_config_dword(struct pci_bus *bus, unsigned int devfn,
908c2ecf20Sopenharmony_ci	int where, int size, u32 val)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	u32 data = 0;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	if (size == 4) {
958c2ecf20Sopenharmony_ci		data = val;
968c2ecf20Sopenharmony_ci	} else {
978c2ecf20Sopenharmony_ci		if (ltq_pci_config_access(PCI_ACCESS_READ, bus,
988c2ecf20Sopenharmony_ci				devfn, where, &data))
998c2ecf20Sopenharmony_ci			return PCIBIOS_DEVICE_NOT_FOUND;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci		if (size == 1)
1028c2ecf20Sopenharmony_ci			data = (data & ~(0xff << ((where & 3) << 3))) |
1038c2ecf20Sopenharmony_ci				(val << ((where & 3) << 3));
1048c2ecf20Sopenharmony_ci		else if (size == 2)
1058c2ecf20Sopenharmony_ci			data = (data & ~(0xffff << ((where & 3) << 3))) |
1068c2ecf20Sopenharmony_ci				(val << ((where & 3) << 3));
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	if (ltq_pci_config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
1108c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	return PCIBIOS_SUCCESSFUL;
1138c2ecf20Sopenharmony_ci}
114