xref: /kernel/linux/linux-5.10/drivers/ssb/embedded.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Sonics Silicon Backplane
38c2ecf20Sopenharmony_ci * Embedded systems support code
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2005-2008, Broadcom Corporation
68c2ecf20Sopenharmony_ci * Copyright 2006-2008, Michael Buesch <m@bues.ch>
78c2ecf20Sopenharmony_ci * Copyright 2012, Hauke Mehrtens <hauke@hauke-m.de>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Licensed under the GNU/GPL. See COPYING for details.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "ssb_private.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/export.h>
158c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
168c2ecf20Sopenharmony_ci#include <linux/ssb/ssb.h>
178c2ecf20Sopenharmony_ci#include <linux/ssb/ssb_embedded.h>
188c2ecf20Sopenharmony_ci#include <linux/ssb/ssb_driver_pci.h>
198c2ecf20Sopenharmony_ci#include <linux/ssb/ssb_driver_gige.h>
208c2ecf20Sopenharmony_ci#include <linux/pci.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciint ssb_watchdog_timer_set(struct ssb_bus *bus, u32 ticks)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	if (ssb_chipco_available(&bus->chipco)) {
268c2ecf20Sopenharmony_ci		ssb_chipco_watchdog_timer_set(&bus->chipco, ticks);
278c2ecf20Sopenharmony_ci		return 0;
288c2ecf20Sopenharmony_ci	}
298c2ecf20Sopenharmony_ci	if (ssb_extif_available(&bus->extif)) {
308c2ecf20Sopenharmony_ci		ssb_extif_watchdog_timer_set(&bus->extif, ticks);
318c2ecf20Sopenharmony_ci		return 0;
328c2ecf20Sopenharmony_ci	}
338c2ecf20Sopenharmony_ci	return -ENODEV;
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ssb_watchdog_timer_set);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciint ssb_watchdog_register(struct ssb_bus *bus)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	struct bcm47xx_wdt wdt = {};
408c2ecf20Sopenharmony_ci	struct platform_device *pdev;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	if (ssb_chipco_available(&bus->chipco)) {
438c2ecf20Sopenharmony_ci		wdt.driver_data = &bus->chipco;
448c2ecf20Sopenharmony_ci		wdt.timer_set = ssb_chipco_watchdog_timer_set_wdt;
458c2ecf20Sopenharmony_ci		wdt.timer_set_ms = ssb_chipco_watchdog_timer_set_ms;
468c2ecf20Sopenharmony_ci		wdt.max_timer_ms = bus->chipco.max_timer_ms;
478c2ecf20Sopenharmony_ci	} else if (ssb_extif_available(&bus->extif)) {
488c2ecf20Sopenharmony_ci		wdt.driver_data = &bus->extif;
498c2ecf20Sopenharmony_ci		wdt.timer_set = ssb_extif_watchdog_timer_set_wdt;
508c2ecf20Sopenharmony_ci		wdt.timer_set_ms = ssb_extif_watchdog_timer_set_ms;
518c2ecf20Sopenharmony_ci		wdt.max_timer_ms = SSB_EXTIF_WATCHDOG_MAX_TIMER_MS;
528c2ecf20Sopenharmony_ci	} else {
538c2ecf20Sopenharmony_ci		return -ENODEV;
548c2ecf20Sopenharmony_ci	}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	pdev = platform_device_register_data(NULL, "bcm47xx-wdt",
578c2ecf20Sopenharmony_ci					     bus->busnumber, &wdt,
588c2ecf20Sopenharmony_ci					     sizeof(wdt));
598c2ecf20Sopenharmony_ci	if (IS_ERR(pdev)) {
608c2ecf20Sopenharmony_ci		pr_debug("can not register watchdog device, err: %li\n",
618c2ecf20Sopenharmony_ci			 PTR_ERR(pdev));
628c2ecf20Sopenharmony_ci		return PTR_ERR(pdev);
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	bus->watchdog = pdev;
668c2ecf20Sopenharmony_ci	return 0;
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciu32 ssb_gpio_in(struct ssb_bus *bus, u32 mask)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	unsigned long flags;
728c2ecf20Sopenharmony_ci	u32 res = 0;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	spin_lock_irqsave(&bus->gpio_lock, flags);
758c2ecf20Sopenharmony_ci	if (ssb_chipco_available(&bus->chipco))
768c2ecf20Sopenharmony_ci		res = ssb_chipco_gpio_in(&bus->chipco, mask);
778c2ecf20Sopenharmony_ci	else if (ssb_extif_available(&bus->extif))
788c2ecf20Sopenharmony_ci		res = ssb_extif_gpio_in(&bus->extif, mask);
798c2ecf20Sopenharmony_ci	else
808c2ecf20Sopenharmony_ci		WARN_ON(1);
818c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&bus->gpio_lock, flags);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return res;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ssb_gpio_in);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciu32 ssb_gpio_out(struct ssb_bus *bus, u32 mask, u32 value)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	unsigned long flags;
908c2ecf20Sopenharmony_ci	u32 res = 0;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	spin_lock_irqsave(&bus->gpio_lock, flags);
938c2ecf20Sopenharmony_ci	if (ssb_chipco_available(&bus->chipco))
948c2ecf20Sopenharmony_ci		res = ssb_chipco_gpio_out(&bus->chipco, mask, value);
958c2ecf20Sopenharmony_ci	else if (ssb_extif_available(&bus->extif))
968c2ecf20Sopenharmony_ci		res = ssb_extif_gpio_out(&bus->extif, mask, value);
978c2ecf20Sopenharmony_ci	else
988c2ecf20Sopenharmony_ci		WARN_ON(1);
998c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&bus->gpio_lock, flags);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return res;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ssb_gpio_out);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ciu32 ssb_gpio_outen(struct ssb_bus *bus, u32 mask, u32 value)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	unsigned long flags;
1088c2ecf20Sopenharmony_ci	u32 res = 0;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	spin_lock_irqsave(&bus->gpio_lock, flags);
1118c2ecf20Sopenharmony_ci	if (ssb_chipco_available(&bus->chipco))
1128c2ecf20Sopenharmony_ci		res = ssb_chipco_gpio_outen(&bus->chipco, mask, value);
1138c2ecf20Sopenharmony_ci	else if (ssb_extif_available(&bus->extif))
1148c2ecf20Sopenharmony_ci		res = ssb_extif_gpio_outen(&bus->extif, mask, value);
1158c2ecf20Sopenharmony_ci	else
1168c2ecf20Sopenharmony_ci		WARN_ON(1);
1178c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&bus->gpio_lock, flags);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	return res;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ssb_gpio_outen);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciu32 ssb_gpio_control(struct ssb_bus *bus, u32 mask, u32 value)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	unsigned long flags;
1268c2ecf20Sopenharmony_ci	u32 res = 0;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	spin_lock_irqsave(&bus->gpio_lock, flags);
1298c2ecf20Sopenharmony_ci	if (ssb_chipco_available(&bus->chipco))
1308c2ecf20Sopenharmony_ci		res = ssb_chipco_gpio_control(&bus->chipco, mask, value);
1318c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&bus->gpio_lock, flags);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	return res;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ssb_gpio_control);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciu32 ssb_gpio_intmask(struct ssb_bus *bus, u32 mask, u32 value)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	unsigned long flags;
1408c2ecf20Sopenharmony_ci	u32 res = 0;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	spin_lock_irqsave(&bus->gpio_lock, flags);
1438c2ecf20Sopenharmony_ci	if (ssb_chipco_available(&bus->chipco))
1448c2ecf20Sopenharmony_ci		res = ssb_chipco_gpio_intmask(&bus->chipco, mask, value);
1458c2ecf20Sopenharmony_ci	else if (ssb_extif_available(&bus->extif))
1468c2ecf20Sopenharmony_ci		res = ssb_extif_gpio_intmask(&bus->extif, mask, value);
1478c2ecf20Sopenharmony_ci	else
1488c2ecf20Sopenharmony_ci		WARN_ON(1);
1498c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&bus->gpio_lock, flags);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	return res;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ssb_gpio_intmask);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ciu32 ssb_gpio_polarity(struct ssb_bus *bus, u32 mask, u32 value)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	unsigned long flags;
1588c2ecf20Sopenharmony_ci	u32 res = 0;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	spin_lock_irqsave(&bus->gpio_lock, flags);
1618c2ecf20Sopenharmony_ci	if (ssb_chipco_available(&bus->chipco))
1628c2ecf20Sopenharmony_ci		res = ssb_chipco_gpio_polarity(&bus->chipco, mask, value);
1638c2ecf20Sopenharmony_ci	else if (ssb_extif_available(&bus->extif))
1648c2ecf20Sopenharmony_ci		res = ssb_extif_gpio_polarity(&bus->extif, mask, value);
1658c2ecf20Sopenharmony_ci	else
1668c2ecf20Sopenharmony_ci		WARN_ON(1);
1678c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&bus->gpio_lock, flags);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return res;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ssb_gpio_polarity);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci#ifdef CONFIG_SSB_DRIVER_GIGE
1748c2ecf20Sopenharmony_cistatic int gige_pci_init_callback(struct ssb_bus *bus, unsigned long data)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	struct pci_dev *pdev = (struct pci_dev *)data;
1778c2ecf20Sopenharmony_ci	struct ssb_device *dev;
1788c2ecf20Sopenharmony_ci	unsigned int i;
1798c2ecf20Sopenharmony_ci	int res;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	for (i = 0; i < bus->nr_devices; i++) {
1828c2ecf20Sopenharmony_ci		dev = &(bus->devices[i]);
1838c2ecf20Sopenharmony_ci		if (dev->id.coreid != SSB_DEV_ETHERNET_GBIT)
1848c2ecf20Sopenharmony_ci			continue;
1858c2ecf20Sopenharmony_ci		if (!dev->dev ||
1868c2ecf20Sopenharmony_ci		    !dev->dev->driver ||
1878c2ecf20Sopenharmony_ci		    !device_is_registered(dev->dev))
1888c2ecf20Sopenharmony_ci			continue;
1898c2ecf20Sopenharmony_ci		res = ssb_gige_pcibios_plat_dev_init(dev, pdev);
1908c2ecf20Sopenharmony_ci		if (res >= 0)
1918c2ecf20Sopenharmony_ci			return res;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return -ENODEV;
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci#endif /* CONFIG_SSB_DRIVER_GIGE */
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ciint ssb_pcibios_plat_dev_init(struct pci_dev *dev)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	int err;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	err = ssb_pcicore_plat_dev_init(dev);
2038c2ecf20Sopenharmony_ci	if (!err)
2048c2ecf20Sopenharmony_ci		return 0;
2058c2ecf20Sopenharmony_ci#ifdef CONFIG_SSB_DRIVER_GIGE
2068c2ecf20Sopenharmony_ci	err = ssb_for_each_bus_call((unsigned long)dev, gige_pci_init_callback);
2078c2ecf20Sopenharmony_ci	if (err >= 0)
2088c2ecf20Sopenharmony_ci		return err;
2098c2ecf20Sopenharmony_ci#endif
2108c2ecf20Sopenharmony_ci	/* This is not a PCI device on any SSB device. */
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return -ENODEV;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci#ifdef CONFIG_SSB_DRIVER_GIGE
2168c2ecf20Sopenharmony_cistatic int gige_map_irq_callback(struct ssb_bus *bus, unsigned long data)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	const struct pci_dev *pdev = (const struct pci_dev *)data;
2198c2ecf20Sopenharmony_ci	struct ssb_device *dev;
2208c2ecf20Sopenharmony_ci	unsigned int i;
2218c2ecf20Sopenharmony_ci	int res;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	for (i = 0; i < bus->nr_devices; i++) {
2248c2ecf20Sopenharmony_ci		dev = &(bus->devices[i]);
2258c2ecf20Sopenharmony_ci		if (dev->id.coreid != SSB_DEV_ETHERNET_GBIT)
2268c2ecf20Sopenharmony_ci			continue;
2278c2ecf20Sopenharmony_ci		if (!dev->dev ||
2288c2ecf20Sopenharmony_ci		    !dev->dev->driver ||
2298c2ecf20Sopenharmony_ci		    !device_is_registered(dev->dev))
2308c2ecf20Sopenharmony_ci			continue;
2318c2ecf20Sopenharmony_ci		res = ssb_gige_map_irq(dev, pdev);
2328c2ecf20Sopenharmony_ci		if (res >= 0)
2338c2ecf20Sopenharmony_ci			return res;
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	return -ENODEV;
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci#endif /* CONFIG_SSB_DRIVER_GIGE */
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ciint ssb_pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	int res;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	/* Check if this PCI device is a device on a SSB bus or device
2458c2ecf20Sopenharmony_ci	 * and return the IRQ number for it. */
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	res = ssb_pcicore_pcibios_map_irq(dev, slot, pin);
2488c2ecf20Sopenharmony_ci	if (res >= 0)
2498c2ecf20Sopenharmony_ci		return res;
2508c2ecf20Sopenharmony_ci#ifdef CONFIG_SSB_DRIVER_GIGE
2518c2ecf20Sopenharmony_ci	res = ssb_for_each_bus_call((unsigned long)dev, gige_map_irq_callback);
2528c2ecf20Sopenharmony_ci	if (res >= 0)
2538c2ecf20Sopenharmony_ci		return res;
2548c2ecf20Sopenharmony_ci#endif
2558c2ecf20Sopenharmony_ci	/* This is not a PCI device on any SSB device. */
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	return -ENODEV;
2588c2ecf20Sopenharmony_ci}
259