162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Keymile km82xx support
462306a36Sopenharmony_ci * Copyright 2008-2011 DENX Software Engineering GmbH
562306a36Sopenharmony_ci * Author: Heiko Schocher <hs@denx.de>
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * based on code from:
862306a36Sopenharmony_ci * Copyright 2007 Freescale Semiconductor, Inc.
962306a36Sopenharmony_ci * Author: Scott Wood <scottwood@freescale.com>
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/init.h>
1362306a36Sopenharmony_ci#include <linux/interrupt.h>
1462306a36Sopenharmony_ci#include <linux/fsl_devices.h>
1562306a36Sopenharmony_ci#include <linux/of_platform.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <linux/io.h>
1862306a36Sopenharmony_ci#include <asm/cpm2.h>
1962306a36Sopenharmony_ci#include <asm/udbg.h>
2062306a36Sopenharmony_ci#include <asm/machdep.h>
2162306a36Sopenharmony_ci#include <linux/time.h>
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#include <sysdev/fsl_soc.h>
2462306a36Sopenharmony_ci#include <sysdev/cpm2_pic.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#include "pq2.h"
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_cistatic void __init km82xx_pic_init(void)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	struct device_node *np = of_find_compatible_node(NULL, NULL,
3162306a36Sopenharmony_ci							"fsl,pq2-pic");
3262306a36Sopenharmony_ci	if (!np) {
3362306a36Sopenharmony_ci		pr_err("PIC init: can not find cpm-pic node\n");
3462306a36Sopenharmony_ci		return;
3562306a36Sopenharmony_ci	}
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	cpm2_pic_init(np);
3862306a36Sopenharmony_ci	of_node_put(np);
3962306a36Sopenharmony_ci}
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistruct cpm_pin {
4262306a36Sopenharmony_ci	int port, pin, flags;
4362306a36Sopenharmony_ci};
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cistatic __initdata struct cpm_pin km82xx_pins[] = {
4662306a36Sopenharmony_ci	/* SMC1 */
4762306a36Sopenharmony_ci	{2, 4, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
4862306a36Sopenharmony_ci	{2, 5, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY},
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	/* SMC2 */
5162306a36Sopenharmony_ci	{0, 8, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
5262306a36Sopenharmony_ci	{0, 9, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY},
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	/* SCC1 */
5562306a36Sopenharmony_ci	{2, 21, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
5662306a36Sopenharmony_ci	{2, 15, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
5762306a36Sopenharmony_ci	{3, 31, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
5862306a36Sopenharmony_ci	{3, 30, CPM_PIN_OUTPUT | CPM_PIN_SECONDARY},
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	/* SCC4 */
6162306a36Sopenharmony_ci	{2, 25, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
6262306a36Sopenharmony_ci	{2, 24, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
6362306a36Sopenharmony_ci	{2,  9, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
6462306a36Sopenharmony_ci	{2,  8, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
6562306a36Sopenharmony_ci	{3, 22, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
6662306a36Sopenharmony_ci	{3, 21, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY},
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	/* FCC1 */
6962306a36Sopenharmony_ci	{0, 14, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
7062306a36Sopenharmony_ci	{0, 15, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
7162306a36Sopenharmony_ci	{0, 16, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
7262306a36Sopenharmony_ci	{0, 17, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
7362306a36Sopenharmony_ci	{0, 18, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY},
7462306a36Sopenharmony_ci	{0, 19, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY},
7562306a36Sopenharmony_ci	{0, 20, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY},
7662306a36Sopenharmony_ci	{0, 21, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY},
7762306a36Sopenharmony_ci	{0, 26, CPM_PIN_INPUT | CPM_PIN_SECONDARY},
7862306a36Sopenharmony_ci	{0, 27, CPM_PIN_INPUT | CPM_PIN_SECONDARY},
7962306a36Sopenharmony_ci	{0, 28, CPM_PIN_OUTPUT | CPM_PIN_SECONDARY},
8062306a36Sopenharmony_ci	{0, 29, CPM_PIN_OUTPUT | CPM_PIN_SECONDARY},
8162306a36Sopenharmony_ci	{0, 30, CPM_PIN_INPUT | CPM_PIN_SECONDARY},
8262306a36Sopenharmony_ci	{0, 31, CPM_PIN_INPUT | CPM_PIN_SECONDARY},
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	{2, 22, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
8562306a36Sopenharmony_ci	{2, 23, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	/* FCC2 */
8862306a36Sopenharmony_ci	{1, 18, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
8962306a36Sopenharmony_ci	{1, 19, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
9062306a36Sopenharmony_ci	{1, 20, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
9162306a36Sopenharmony_ci	{1, 21, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
9262306a36Sopenharmony_ci	{1, 22, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY},
9362306a36Sopenharmony_ci	{1, 23, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY},
9462306a36Sopenharmony_ci	{1, 24, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY},
9562306a36Sopenharmony_ci	{1, 25, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY},
9662306a36Sopenharmony_ci	{1, 26, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
9762306a36Sopenharmony_ci	{1, 27, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
9862306a36Sopenharmony_ci	{1, 28, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
9962306a36Sopenharmony_ci	{1, 29, CPM_PIN_OUTPUT | CPM_PIN_SECONDARY},
10062306a36Sopenharmony_ci	{1, 30, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
10162306a36Sopenharmony_ci	{1, 31, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY},
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	{2, 18, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
10462306a36Sopenharmony_ci	{2, 19, CPM_PIN_INPUT | CPM_PIN_PRIMARY},
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	/* MDC */
10762306a36Sopenharmony_ci	{0, 13, CPM_PIN_OUTPUT | CPM_PIN_GPIO},
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci#if defined(CONFIG_I2C_CPM)
11062306a36Sopenharmony_ci	/* I2C */
11162306a36Sopenharmony_ci	{3, 14, CPM_PIN_INPUT | CPM_PIN_SECONDARY | CPM_PIN_OPENDRAIN},
11262306a36Sopenharmony_ci	{3, 15, CPM_PIN_INPUT | CPM_PIN_SECONDARY | CPM_PIN_OPENDRAIN},
11362306a36Sopenharmony_ci#endif
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	/* USB */
11662306a36Sopenharmony_ci	{0, 10, CPM_PIN_OUTPUT | CPM_PIN_GPIO},    /* FULL_SPEED */
11762306a36Sopenharmony_ci	{0, 11, CPM_PIN_OUTPUT | CPM_PIN_GPIO},    /*/SLAVE */
11862306a36Sopenharmony_ci	{2, 10, CPM_PIN_INPUT  | CPM_PIN_PRIMARY}, /* RXN */
11962306a36Sopenharmony_ci	{2, 11, CPM_PIN_INPUT  | CPM_PIN_PRIMARY}, /* RXP */
12062306a36Sopenharmony_ci	{2, 20, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY}, /* /OE */
12162306a36Sopenharmony_ci	{2, 27, CPM_PIN_INPUT  | CPM_PIN_PRIMARY}, /* RXCLK */
12262306a36Sopenharmony_ci	{3, 23, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY}, /* TXP */
12362306a36Sopenharmony_ci	{3, 24, CPM_PIN_OUTPUT | CPM_PIN_PRIMARY}, /* TXN */
12462306a36Sopenharmony_ci	{3, 25, CPM_PIN_INPUT  | CPM_PIN_PRIMARY}, /* RXD */
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	/* SPI */
12762306a36Sopenharmony_ci	{3, 16, CPM_PIN_INPUT | CPM_PIN_SECONDARY},/* SPI_MISO PD16 */
12862306a36Sopenharmony_ci	{3, 17, CPM_PIN_INPUT | CPM_PIN_SECONDARY},/* SPI_MOSI PD17 */
12962306a36Sopenharmony_ci	{3, 18, CPM_PIN_INPUT | CPM_PIN_SECONDARY},/* SPI_CLK PD18 */
13062306a36Sopenharmony_ci};
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_cistatic void __init init_ioports(void)
13362306a36Sopenharmony_ci{
13462306a36Sopenharmony_ci	int i;
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(km82xx_pins); i++) {
13762306a36Sopenharmony_ci		const struct cpm_pin *pin = &km82xx_pins[i];
13862306a36Sopenharmony_ci		cpm2_set_pin(pin->port, pin->pin, pin->flags);
13962306a36Sopenharmony_ci	}
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	cpm2_smc_clk_setup(CPM_CLK_SMC2, CPM_BRG8);
14262306a36Sopenharmony_ci	cpm2_smc_clk_setup(CPM_CLK_SMC1, CPM_BRG7);
14362306a36Sopenharmony_ci	cpm2_clk_setup(CPM_CLK_SCC1, CPM_CLK11, CPM_CLK_RX);
14462306a36Sopenharmony_ci	cpm2_clk_setup(CPM_CLK_SCC1, CPM_CLK11, CPM_CLK_TX);
14562306a36Sopenharmony_ci	cpm2_clk_setup(CPM_CLK_SCC3, CPM_CLK5, CPM_CLK_RTX);
14662306a36Sopenharmony_ci	cpm2_clk_setup(CPM_CLK_SCC4, CPM_CLK7, CPM_CLK_RX);
14762306a36Sopenharmony_ci	cpm2_clk_setup(CPM_CLK_SCC4, CPM_CLK8, CPM_CLK_TX);
14862306a36Sopenharmony_ci	cpm2_clk_setup(CPM_CLK_FCC1, CPM_CLK10, CPM_CLK_RX);
14962306a36Sopenharmony_ci	cpm2_clk_setup(CPM_CLK_FCC1, CPM_CLK9,  CPM_CLK_TX);
15062306a36Sopenharmony_ci	cpm2_clk_setup(CPM_CLK_FCC2, CPM_CLK13, CPM_CLK_RX);
15162306a36Sopenharmony_ci	cpm2_clk_setup(CPM_CLK_FCC2, CPM_CLK14, CPM_CLK_TX);
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	/* Force USB FULL SPEED bit to '1' */
15462306a36Sopenharmony_ci	setbits32(&cpm2_immr->im_ioport.iop_pdata, 1 << (31 - 10));
15562306a36Sopenharmony_ci	/* clear USB_SLAVE */
15662306a36Sopenharmony_ci	clrbits32(&cpm2_immr->im_ioport.iop_pdata, 1 << (31 - 11));
15762306a36Sopenharmony_ci}
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_cistatic void __init km82xx_setup_arch(void)
16062306a36Sopenharmony_ci{
16162306a36Sopenharmony_ci	if (ppc_md.progress)
16262306a36Sopenharmony_ci		ppc_md.progress("km82xx_setup_arch()", 0);
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	cpm2_reset();
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	/* When this is set, snooping CPM DMA from RAM causes
16762306a36Sopenharmony_ci	 * machine checks.  See erratum SIU18.
16862306a36Sopenharmony_ci	 */
16962306a36Sopenharmony_ci	clrbits32(&cpm2_immr->im_siu_conf.siu_82xx.sc_bcr, MPC82XX_BCR_PLDP);
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	init_ioports();
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	if (ppc_md.progress)
17462306a36Sopenharmony_ci		ppc_md.progress("km82xx_setup_arch(), finish", 0);
17562306a36Sopenharmony_ci}
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_cistatic const struct of_device_id of_bus_ids[] __initconst = {
17862306a36Sopenharmony_ci	{ .compatible = "simple-bus", },
17962306a36Sopenharmony_ci	{},
18062306a36Sopenharmony_ci};
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_cistatic int __init declare_of_platform_devices(void)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	of_platform_bus_probe(NULL, of_bus_ids, NULL);
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	return 0;
18762306a36Sopenharmony_ci}
18862306a36Sopenharmony_cimachine_device_initcall(km82xx, declare_of_platform_devices);
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_cidefine_machine(km82xx)
19162306a36Sopenharmony_ci{
19262306a36Sopenharmony_ci	.name = "Keymile km82xx",
19362306a36Sopenharmony_ci	.compatible = "keymile,km82xx",
19462306a36Sopenharmony_ci	.setup_arch = km82xx_setup_arch,
19562306a36Sopenharmony_ci	.init_IRQ = km82xx_pic_init,
19662306a36Sopenharmony_ci	.get_irq = cpm2_get_irq,
19762306a36Sopenharmony_ci	.restart = pq2_restart,
19862306a36Sopenharmony_ci	.progress = udbg_progress,
19962306a36Sopenharmony_ci};
200