1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
219580e66SDave Liu /*
39993e196SKim Phillips  * Copyright (C) 2006-2009 Freescale Semiconductor, Inc.
419580e66SDave Liu  */
519580e66SDave Liu 
619580e66SDave Liu #include <asm/mmu.h>
719580e66SDave Liu #include <asm/io.h>
819580e66SDave Liu #include <common.h>
919580e66SDave Liu #include <mpc83xx.h>
1019580e66SDave Liu #include <pci.h>
1119580e66SDave Liu #include <i2c.h>
128b34557cSAnton Vorontsov #include <fdt_support.h>
1319580e66SDave Liu #include <asm/fsl_i2c.h>
147e1afb62SKumar Gala #include <asm/fsl_mpc83xx_serdes.h>
1519580e66SDave Liu 
1619580e66SDave Liu static struct pci_region pci_regions[] = {
1719580e66SDave Liu 	{
186d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		bus_start: CONFIG_SYS_PCI_MEM_BASE,
196d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		phys_start: CONFIG_SYS_PCI_MEM_PHYS,
206d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		size: CONFIG_SYS_PCI_MEM_SIZE,
2119580e66SDave Liu 		flags: PCI_REGION_MEM | PCI_REGION_PREFETCH
2219580e66SDave Liu 	},
2319580e66SDave Liu 	{
246d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		bus_start: CONFIG_SYS_PCI_MMIO_BASE,
256d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		phys_start: CONFIG_SYS_PCI_MMIO_PHYS,
266d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		size: CONFIG_SYS_PCI_MMIO_SIZE,
2719580e66SDave Liu 		flags: PCI_REGION_MEM
2819580e66SDave Liu 	},
2919580e66SDave Liu 	{
306d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		bus_start: CONFIG_SYS_PCI_IO_BASE,
316d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		phys_start: CONFIG_SYS_PCI_IO_PHYS,
326d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 		size: CONFIG_SYS_PCI_IO_SIZE,
3319580e66SDave Liu 		flags: PCI_REGION_IO
3419580e66SDave Liu 	}
3519580e66SDave Liu };
3619580e66SDave Liu 
378b34557cSAnton Vorontsov static struct pci_region pcie_regions_0[] = {
388b34557cSAnton Vorontsov 	{
398b34557cSAnton Vorontsov 		.bus_start = CONFIG_SYS_PCIE1_MEM_BASE,
408b34557cSAnton Vorontsov 		.phys_start = CONFIG_SYS_PCIE1_MEM_PHYS,
418b34557cSAnton Vorontsov 		.size = CONFIG_SYS_PCIE1_MEM_SIZE,
428b34557cSAnton Vorontsov 		.flags = PCI_REGION_MEM,
438b34557cSAnton Vorontsov 	},
448b34557cSAnton Vorontsov 	{
458b34557cSAnton Vorontsov 		.bus_start = CONFIG_SYS_PCIE1_IO_BASE,
468b34557cSAnton Vorontsov 		.phys_start = CONFIG_SYS_PCIE1_IO_PHYS,
478b34557cSAnton Vorontsov 		.size = CONFIG_SYS_PCIE1_IO_SIZE,
488b34557cSAnton Vorontsov 		.flags = PCI_REGION_IO,
498b34557cSAnton Vorontsov 	},
508b34557cSAnton Vorontsov };
518b34557cSAnton Vorontsov 
528b34557cSAnton Vorontsov static struct pci_region pcie_regions_1[] = {
538b34557cSAnton Vorontsov 	{
548b34557cSAnton Vorontsov 		.bus_start = CONFIG_SYS_PCIE2_MEM_BASE,
558b34557cSAnton Vorontsov 		.phys_start = CONFIG_SYS_PCIE2_MEM_PHYS,
568b34557cSAnton Vorontsov 		.size = CONFIG_SYS_PCIE2_MEM_SIZE,
578b34557cSAnton Vorontsov 		.flags = PCI_REGION_MEM,
588b34557cSAnton Vorontsov 	},
598b34557cSAnton Vorontsov 	{
608b34557cSAnton Vorontsov 		.bus_start = CONFIG_SYS_PCIE2_IO_BASE,
618b34557cSAnton Vorontsov 		.phys_start = CONFIG_SYS_PCIE2_IO_PHYS,
628b34557cSAnton Vorontsov 		.size = CONFIG_SYS_PCIE2_IO_SIZE,
638b34557cSAnton Vorontsov 		.flags = PCI_REGION_IO,
648b34557cSAnton Vorontsov 	},
658b34557cSAnton Vorontsov };
668b34557cSAnton Vorontsov 
is_pex_x2(void)678b34557cSAnton Vorontsov static int is_pex_x2(void)
688b34557cSAnton Vorontsov {
6900caae6dSSimon Glass 	const char *pex_x2 = env_get("pex_x2");
708b34557cSAnton Vorontsov 
718b34557cSAnton Vorontsov 	if (pex_x2 && !strcmp(pex_x2, "yes"))
728b34557cSAnton Vorontsov 		return 1;
738b34557cSAnton Vorontsov 	return 0;
748b34557cSAnton Vorontsov }
758b34557cSAnton Vorontsov 
pci_init_board(void)7619580e66SDave Liu void pci_init_board(void)
7719580e66SDave Liu {
786d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	volatile immap_t *immr = (volatile immap_t *)CONFIG_SYS_IMMR;
798b34557cSAnton Vorontsov 	volatile sysconf83xx_t *sysconf = &immr->sysconf;
8019580e66SDave Liu 	volatile clk83xx_t *clk = (volatile clk83xx_t *)&immr->clk;
8119580e66SDave Liu 	volatile law83xx_t *pci_law = immr->sysconf.pcilaw;
828b34557cSAnton Vorontsov 	volatile law83xx_t *pcie_law = sysconf->pcielaw;
8319580e66SDave Liu 	struct pci_region *reg[] = { pci_regions };
848b34557cSAnton Vorontsov 	struct pci_region *pcie_reg[] = { pcie_regions_0, pcie_regions_1, };
858b34557cSAnton Vorontsov 	u32 spridr = in_be32(&immr->sysconf.spridr);
868b34557cSAnton Vorontsov 	int pex2 = is_pex_x2();
8719580e66SDave Liu 
8800f7bbaeSAnton Vorontsov 	if (board_pci_host_broken())
898b34557cSAnton Vorontsov 		goto skip_pci;
9000f7bbaeSAnton Vorontsov 
9119580e66SDave Liu 	/* Enable all 5 PCI_CLK_OUTPUTS */
9219580e66SDave Liu 	clk->occr |= 0xf8000000;
9319580e66SDave Liu 	udelay(2000);
9419580e66SDave Liu 
9519580e66SDave Liu 	/* Configure PCI Local Access Windows */
966d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	pci_law[0].bar = CONFIG_SYS_PCI_MEM_PHYS & LAWBAR_BAR;
9719580e66SDave Liu 	pci_law[0].ar = LBLAWAR_EN | LBLAWAR_512MB;
9819580e66SDave Liu 
996d0f6bcfSJean-Christophe PLAGNIOL-VILLARD 	pci_law[1].bar = CONFIG_SYS_PCI_IO_PHYS & LAWBAR_BAR;
10019580e66SDave Liu 	pci_law[1].ar = LBLAWAR_EN | LBLAWAR_1MB;
10119580e66SDave Liu 
10219580e66SDave Liu 	udelay(2000);
10319580e66SDave Liu 
1046aa3d3bfSPeter Tyser 	mpc83xx_pci_init(1, reg);
1058b34557cSAnton Vorontsov skip_pci:
1068b34557cSAnton Vorontsov 	/* There is no PEX in MPC8379 parts. */
1078b34557cSAnton Vorontsov 	if (PARTID_NO_E(spridr) == SPR_8379)
1088b34557cSAnton Vorontsov 		return;
1098b34557cSAnton Vorontsov 
1107e2ec1deSAnton Vorontsov 	if (pex2)
1117e2ec1deSAnton Vorontsov 		fsl_setup_serdes(CONFIG_FSL_SERDES2, FSL_SERDES_PROTO_PEX_X2,
1127e2ec1deSAnton Vorontsov 				 FSL_SERDES_CLK_100, FSL_SERDES_VDD_1V);
1137e2ec1deSAnton Vorontsov 	else
1147e2ec1deSAnton Vorontsov 		fsl_setup_serdes(CONFIG_FSL_SERDES2, FSL_SERDES_PROTO_PEX,
1157e2ec1deSAnton Vorontsov 				 FSL_SERDES_CLK_100, FSL_SERDES_VDD_1V);
1167e2ec1deSAnton Vorontsov 
1178b34557cSAnton Vorontsov 	/* Configure the clock for PCIE controller */
1188b34557cSAnton Vorontsov 	clrsetbits_be32(&clk->sccr, SCCR_PCIEXP1CM | SCCR_PCIEXP2CM,
1198b34557cSAnton Vorontsov 				    SCCR_PCIEXP1CM_1 | SCCR_PCIEXP2CM_1);
1208b34557cSAnton Vorontsov 
1218b34557cSAnton Vorontsov 	/* Deassert the resets in the control register */
1228b34557cSAnton Vorontsov 	out_be32(&sysconf->pecr1, 0xE0008000);
1238b34557cSAnton Vorontsov 	if (!pex2)
1248b34557cSAnton Vorontsov 		out_be32(&sysconf->pecr2, 0xE0008000);
1258b34557cSAnton Vorontsov 	udelay(2000);
1268b34557cSAnton Vorontsov 
1278b34557cSAnton Vorontsov 	/* Configure PCI Express Local Access Windows */
1288b34557cSAnton Vorontsov 	out_be32(&pcie_law[0].bar, CONFIG_SYS_PCIE1_BASE & LAWBAR_BAR);
1298b34557cSAnton Vorontsov 	out_be32(&pcie_law[0].ar, LBLAWAR_EN | LBLAWAR_512MB);
1308b34557cSAnton Vorontsov 
1318b34557cSAnton Vorontsov 	out_be32(&pcie_law[1].bar, CONFIG_SYS_PCIE2_BASE & LAWBAR_BAR);
1328b34557cSAnton Vorontsov 	out_be32(&pcie_law[1].ar, LBLAWAR_EN | LBLAWAR_512MB);
1338b34557cSAnton Vorontsov 
134e2229352SKim Phillips 	mpc83xx_pcie_init(pex2 ? 1 : 2, pcie_reg);
1358b34557cSAnton Vorontsov }
1368b34557cSAnton Vorontsov 
ft_pcie_fixup(void * blob,bd_t * bd)1378b34557cSAnton Vorontsov void ft_pcie_fixup(void *blob, bd_t *bd)
1388b34557cSAnton Vorontsov {
1398b34557cSAnton Vorontsov 	const char *status = "disabled (PCIE1 is x2)";
1408b34557cSAnton Vorontsov 
1418b34557cSAnton Vorontsov 	if (!is_pex_x2())
1428b34557cSAnton Vorontsov 		return;
1438b34557cSAnton Vorontsov 
1448b34557cSAnton Vorontsov 	do_fixup_by_path(blob, "pci2", "status", status,
1458b34557cSAnton Vorontsov 			 strlen(status) + 1, 1);
14619580e66SDave Liu }
147