18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * PCI operations for the Sega Dreamcast
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2001, 2002  M. R. Brown
68c2ecf20Sopenharmony_ci * Copyright (C) 2002, 2003  Paul Mundt
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/sched.h>
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/param.h>
128c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/irq.h>
158c2ecf20Sopenharmony_ci#include <linux/pci.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/io.h>
188c2ecf20Sopenharmony_ci#include <mach/pci.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * The !gapspci_config_access case really shouldn't happen, ever, unless
228c2ecf20Sopenharmony_ci * someone implicitly messes around with the last devfn value.. otherwise we
238c2ecf20Sopenharmony_ci * only support a single device anyways, and if we didn't have a BBA, we
248c2ecf20Sopenharmony_ci * wouldn't make it terribly far through the PCI setup anyways.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * Also, we could very easily support both Type 0 and Type 1 configurations
278c2ecf20Sopenharmony_ci * here, but since it doesn't seem that there is any such implementation in
288c2ecf20Sopenharmony_ci * existence, we don't bother.
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * I suppose if someone actually gets around to ripping the chip out of
318c2ecf20Sopenharmony_ci * the BBA and hanging some more devices off of it, then this might be
328c2ecf20Sopenharmony_ci * something to take into consideration. However, due to the cost of the BBA,
338c2ecf20Sopenharmony_ci * and the general lack of activity by DC hardware hackers, this doesn't seem
348c2ecf20Sopenharmony_ci * likely to happen anytime soon.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_cistatic int gapspci_config_access(unsigned char bus, unsigned int devfn)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	return (bus == 0) && (devfn == 0);
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/*
428c2ecf20Sopenharmony_ci * We can also actually read and write in b/w/l sizes! Thankfully this part
438c2ecf20Sopenharmony_ci * was at least done right, and we don't have to do the stupid masking and
448c2ecf20Sopenharmony_ci * shifting that we do on the 7751! Small wonders never cease to amaze.
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_cistatic int gapspci_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *val)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	*val = 0xffffffff;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if (!gapspci_config_access(bus->number, devfn))
518c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	switch (size) {
548c2ecf20Sopenharmony_ci	case 1: *val = inb(GAPSPCI_BBA_CONFIG+where); break;
558c2ecf20Sopenharmony_ci	case 2: *val = inw(GAPSPCI_BBA_CONFIG+where); break;
568c2ecf20Sopenharmony_ci	case 4: *val = inl(GAPSPCI_BBA_CONFIG+where); break;
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci        return PCIBIOS_SUCCESSFUL;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic int gapspci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 val)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	if (!gapspci_config_access(bus->number, devfn))
658c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	switch (size) {
688c2ecf20Sopenharmony_ci	case 1: outb(( u8)val, GAPSPCI_BBA_CONFIG+where); break;
698c2ecf20Sopenharmony_ci	case 2: outw((u16)val, GAPSPCI_BBA_CONFIG+where); break;
708c2ecf20Sopenharmony_ci	case 4: outl((u32)val, GAPSPCI_BBA_CONFIG+where); break;
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci        return PCIBIOS_SUCCESSFUL;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistruct pci_ops gapspci_pci_ops = {
778c2ecf20Sopenharmony_ci	.read	= gapspci_read,
788c2ecf20Sopenharmony_ci	.write	= gapspci_write,
798c2ecf20Sopenharmony_ci};
80