162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#include <linux/kernel.h>
362306a36Sopenharmony_ci#include <linux/types.h>
462306a36Sopenharmony_ci#include <linux/init.h>
562306a36Sopenharmony_ci#include <linux/delay.h>
662306a36Sopenharmony_ci#include <linux/pci.h>
762306a36Sopenharmony_ci#include <linux/io.h>
862306a36Sopenharmony_ci#include <linux/sh_intc.h>
962306a36Sopenharmony_ci#include "pci-sh4.h"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciint pcibios_map_platform_irq(const struct pci_dev *, u8 slot, u8 pin)
1262306a36Sopenharmony_ci{
1362306a36Sopenharmony_ci        switch (slot) {
1462306a36Sopenharmony_ci        case 0: return evt2irq(0x3a0);
1562306a36Sopenharmony_ci        case 1: return evt2irq(0x3a0);	/* AMD Ethernet controller */
1662306a36Sopenharmony_ci        case 2: return -1;
1762306a36Sopenharmony_ci        case 3: return -1;
1862306a36Sopenharmony_ci        case 4: return -1;
1962306a36Sopenharmony_ci        default:
2062306a36Sopenharmony_ci                printk("PCI: Bad IRQ mapping request for slot %d\n", slot);
2162306a36Sopenharmony_ci                return -1;
2262306a36Sopenharmony_ci        }
2362306a36Sopenharmony_ci}
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#define PCIMCR_MRSET_OFF	0xBFFFFFFF
2662306a36Sopenharmony_ci#define PCIMCR_RFSH_OFF		0xFFFFFFFB
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/*
2962306a36Sopenharmony_ci * Only long word accesses of the PCIC's internal local registers and the
3062306a36Sopenharmony_ci * configuration registers from the CPU is supported.
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_ci#define PCIC_WRITE(x,v) writel((v), PCI_REG(x))
3362306a36Sopenharmony_ci#define PCIC_READ(x) readl(PCI_REG(x))
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/*
3662306a36Sopenharmony_ci * Description:  This function sets up and initializes the pcic, sets
3762306a36Sopenharmony_ci * up the BARS, maps the DRAM into the address space etc, etc.
3862306a36Sopenharmony_ci */
3962306a36Sopenharmony_ciint pci_fixup_pcic(struct pci_channel *chan)
4062306a36Sopenharmony_ci{
4162306a36Sopenharmony_ci	unsigned long bcr1, wcr1, wcr2, wcr3, mcr;
4262306a36Sopenharmony_ci	unsigned short bcr2;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	/*
4562306a36Sopenharmony_ci	* Initialize the slave bus controller on the pcic.  The values used
4662306a36Sopenharmony_ci	* here should not be hardcoded, but they should be taken from the bsc
4762306a36Sopenharmony_ci	* on the processor, to make this function as generic as possible.
4862306a36Sopenharmony_ci	* (i.e. Another sbc may usr different SDRAM timing settings -- in order
4962306a36Sopenharmony_ci	* for the pcic to work, its settings need to be exactly the same.)
5062306a36Sopenharmony_ci	*/
5162306a36Sopenharmony_ci	bcr1 = (*(volatile unsigned long*)(SH7751_BCR1));
5262306a36Sopenharmony_ci	bcr2 = (*(volatile unsigned short*)(SH7751_BCR2));
5362306a36Sopenharmony_ci	wcr1 = (*(volatile unsigned long*)(SH7751_WCR1));
5462306a36Sopenharmony_ci	wcr2 = (*(volatile unsigned long*)(SH7751_WCR2));
5562306a36Sopenharmony_ci	wcr3 = (*(volatile unsigned long*)(SH7751_WCR3));
5662306a36Sopenharmony_ci	mcr = (*(volatile unsigned long*)(SH7751_MCR));
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	bcr1 = bcr1 | 0x00080000;  /* Enable Bit 19, BREQEN */
5962306a36Sopenharmony_ci	(*(volatile unsigned long*)(SH7751_BCR1)) = bcr1;
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	bcr1 = bcr1 | 0x40080000;  /* Enable Bit 19 BREQEN, set PCIC to slave */
6262306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCIBCR1, bcr1);	 /* PCIC BCR1 */
6362306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCIBCR2, bcr2);     /* PCIC BCR2 */
6462306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCIWCR1, wcr1);     /* PCIC WCR1 */
6562306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCIWCR2, wcr2);     /* PCIC WCR2 */
6662306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCIWCR3, wcr3);     /* PCIC WCR3 */
6762306a36Sopenharmony_ci	mcr = (mcr & PCIMCR_MRSET_OFF) & PCIMCR_RFSH_OFF;
6862306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCIMCR, mcr);      /* PCIC MCR */
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	/* Enable all interrupts, so we know what to fix */
7262306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCIINTM, 0x0000c3ff);
7362306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCIAINTM, 0x0000380f);
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	/* Set up standard PCI config registers */
7662306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCICONF1,	0xF39000C7); /* Bus Master, Mem & I/O access */
7762306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCICONF2,	0x00000000); /* PCI Class code & Revision ID */
7862306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCICONF4,	0xab000001); /* PCI I/O address (local regs) */
7962306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCICONF5,	0x0c000000); /* PCI MEM address (local RAM)  */
8062306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCICONF6,	0xd0000000); /* PCI MEM address (unused)     */
8162306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCICONF11, 0x35051054); /* PCI Subsystem ID & Vendor ID */
8262306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCILSR0, 0x03f00000);   /* MEM (full 64M exposed)       */
8362306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCILSR1, 0x00000000);   /* MEM (unused)                 */
8462306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCILAR0, 0x0c000000);   /* MEM (direct map from PCI)    */
8562306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCILAR1, 0x00000000);   /* MEM (unused)                 */
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	/* Now turn it on... */
8862306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCICR, 0xa5000001);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	/*
9162306a36Sopenharmony_ci	* Set PCIMBR and PCIIOBR here, assuming a single window
9262306a36Sopenharmony_ci	* (16M MEM, 256K IO) is enough.  If a larger space is
9362306a36Sopenharmony_ci	* needed, the readx/writex and inx/outx functions will
9462306a36Sopenharmony_ci	* have to do more (e.g. setting registers for each call).
9562306a36Sopenharmony_ci	*/
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	/*
9862306a36Sopenharmony_ci	* Set the MBR so PCI address is one-to-one with window,
9962306a36Sopenharmony_ci	* meaning all calls go straight through... use BUG_ON to
10062306a36Sopenharmony_ci	* catch erroneous assumption.
10162306a36Sopenharmony_ci	*/
10262306a36Sopenharmony_ci	BUG_ON(chan->resources[1].start != SH7751_PCI_MEMORY_BASE);
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCIMBR, chan->resources[1].start);
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	/* Set IOBR for window containing area specified in pci.h */
10762306a36Sopenharmony_ci	PCIC_WRITE(SH7751_PCIIOBR, (chan->resources[0].start & SH7751_PCIIOBR_MASK));
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	/* All done, may as well say so... */
11062306a36Sopenharmony_ci	printk("SH7751 PCI: Finished initialization of the PCI controller\n");
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	return 1;
11362306a36Sopenharmony_ci}
114