18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Sonics Silicon Backplane
38c2ecf20Sopenharmony_ci * Broadcom EXTIF core driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2005, Broadcom Corporation
68c2ecf20Sopenharmony_ci * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
78c2ecf20Sopenharmony_ci * Copyright 2006, 2007, Felix Fietkau <nbd@openwrt.org>
88c2ecf20Sopenharmony_ci * Copyright 2007, Aurelien Jarno <aurelien@aurel32.net>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Licensed under the GNU/GPL. See COPYING for details.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "ssb_private.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/serial.h>
168c2ecf20Sopenharmony_ci#include <linux/serial_core.h>
178c2ecf20Sopenharmony_ci#include <linux/serial_reg.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic inline u32 extif_read32(struct ssb_extif *extif, u16 offset)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	return ssb_read32(extif->dev, offset);
238c2ecf20Sopenharmony_ci}
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic inline void extif_write32(struct ssb_extif *extif, u16 offset, u32 value)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	ssb_write32(extif->dev, offset, value);
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic inline u32 extif_write32_masked(struct ssb_extif *extif, u16 offset,
318c2ecf20Sopenharmony_ci				       u32 mask, u32 value)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	value &= mask;
348c2ecf20Sopenharmony_ci	value |= extif_read32(extif, offset) & ~mask;
358c2ecf20Sopenharmony_ci	extif_write32(extif, offset, value);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	return value;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#ifdef CONFIG_SSB_SERIAL
418c2ecf20Sopenharmony_cistatic bool serial_exists(u8 *regs)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	u8 save_mcr, msr = 0;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	if (regs) {
468c2ecf20Sopenharmony_ci		save_mcr = regs[UART_MCR];
478c2ecf20Sopenharmony_ci		regs[UART_MCR] = (UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_RTS);
488c2ecf20Sopenharmony_ci		msr = regs[UART_MSR] & (UART_MSR_DCD | UART_MSR_RI
498c2ecf20Sopenharmony_ci					| UART_MSR_CTS | UART_MSR_DSR);
508c2ecf20Sopenharmony_ci		regs[UART_MCR] = save_mcr;
518c2ecf20Sopenharmony_ci	}
528c2ecf20Sopenharmony_ci	return (msr == (UART_MSR_DCD | UART_MSR_CTS));
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ciint ssb_extif_serial_init(struct ssb_extif *extif, struct ssb_serial_port *ports)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	u32 i, nr_ports = 0;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	/* Disable GPIO interrupt initially */
608c2ecf20Sopenharmony_ci	extif_write32(extif, SSB_EXTIF_GPIO_INTPOL, 0);
618c2ecf20Sopenharmony_ci	extif_write32(extif, SSB_EXTIF_GPIO_INTMASK, 0);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
648c2ecf20Sopenharmony_ci		void __iomem *uart_regs;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci		uart_regs = ioremap(SSB_EUART, 16);
678c2ecf20Sopenharmony_ci		if (uart_regs) {
688c2ecf20Sopenharmony_ci			uart_regs += (i * 8);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci			if (serial_exists(uart_regs) && ports) {
718c2ecf20Sopenharmony_ci				extif_write32(extif, SSB_EXTIF_GPIO_INTMASK, 2);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci				nr_ports++;
748c2ecf20Sopenharmony_ci				ports[i].regs = uart_regs;
758c2ecf20Sopenharmony_ci				ports[i].irq = 2;
768c2ecf20Sopenharmony_ci				ports[i].baud_base = 13500000;
778c2ecf20Sopenharmony_ci				ports[i].reg_shift = 0;
788c2ecf20Sopenharmony_ci			}
798c2ecf20Sopenharmony_ci			iounmap(uart_regs);
808c2ecf20Sopenharmony_ci		}
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci	return nr_ports;
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci#endif /* CONFIG_SSB_SERIAL */
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_civoid ssb_extif_timing_init(struct ssb_extif *extif, unsigned long ns)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	u32 tmp;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/* Initialize extif so we can get to the LEDs and external UART */
918c2ecf20Sopenharmony_ci	extif_write32(extif, SSB_EXTIF_PROG_CFG, SSB_EXTCFG_EN);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	/* Set timing for the flash */
948c2ecf20Sopenharmony_ci	tmp  = DIV_ROUND_UP(10, ns) << SSB_PROG_WCNT_3_SHIFT;
958c2ecf20Sopenharmony_ci	tmp |= DIV_ROUND_UP(40, ns) << SSB_PROG_WCNT_1_SHIFT;
968c2ecf20Sopenharmony_ci	tmp |= DIV_ROUND_UP(120, ns);
978c2ecf20Sopenharmony_ci	extif_write32(extif, SSB_EXTIF_PROG_WAITCNT, tmp);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/* Set programmable interface timing for external uart */
1008c2ecf20Sopenharmony_ci	tmp  = DIV_ROUND_UP(10, ns) << SSB_PROG_WCNT_3_SHIFT;
1018c2ecf20Sopenharmony_ci	tmp |= DIV_ROUND_UP(20, ns) << SSB_PROG_WCNT_2_SHIFT;
1028c2ecf20Sopenharmony_ci	tmp |= DIV_ROUND_UP(100, ns) << SSB_PROG_WCNT_1_SHIFT;
1038c2ecf20Sopenharmony_ci	tmp |= DIV_ROUND_UP(120, ns);
1048c2ecf20Sopenharmony_ci	extif_write32(extif, SSB_EXTIF_PROG_WAITCNT, tmp);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_civoid ssb_extif_get_clockcontrol(struct ssb_extif *extif,
1088c2ecf20Sopenharmony_ci				u32 *pll_type, u32 *n, u32 *m)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	*pll_type = SSB_PLLTYPE_1;
1118c2ecf20Sopenharmony_ci	*n = extif_read32(extif, SSB_EXTIF_CLOCK_N);
1128c2ecf20Sopenharmony_ci	*m = extif_read32(extif, SSB_EXTIF_CLOCK_SB);
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciu32 ssb_extif_watchdog_timer_set_wdt(struct bcm47xx_wdt *wdt, u32 ticks)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	struct ssb_extif *extif = bcm47xx_wdt_get_drvdata(wdt);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	return ssb_extif_watchdog_timer_set(extif, ticks);
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciu32 ssb_extif_watchdog_timer_set_ms(struct bcm47xx_wdt *wdt, u32 ms)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct ssb_extif *extif = bcm47xx_wdt_get_drvdata(wdt);
1258c2ecf20Sopenharmony_ci	u32 ticks = (SSB_EXTIF_WATCHDOG_CLK / 1000) * ms;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	ticks = ssb_extif_watchdog_timer_set(extif, ticks);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return (ticks * 1000) / SSB_EXTIF_WATCHDOG_CLK;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ciu32 ssb_extif_watchdog_timer_set(struct ssb_extif *extif, u32 ticks)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	if (ticks > SSB_EXTIF_WATCHDOG_MAX_TIMER)
1358c2ecf20Sopenharmony_ci		ticks = SSB_EXTIF_WATCHDOG_MAX_TIMER;
1368c2ecf20Sopenharmony_ci	extif_write32(extif, SSB_EXTIF_WATCHDOG, ticks);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	return ticks;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_civoid ssb_extif_init(struct ssb_extif *extif)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	if (!extif->dev)
1448c2ecf20Sopenharmony_ci		return; /* We don't have a Extif core */
1458c2ecf20Sopenharmony_ci	spin_lock_init(&extif->gpio_lock);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ciu32 ssb_extif_gpio_in(struct ssb_extif *extif, u32 mask)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	return extif_read32(extif, SSB_EXTIF_GPIO_IN) & mask;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ciu32 ssb_extif_gpio_out(struct ssb_extif *extif, u32 mask, u32 value)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	unsigned long flags;
1568c2ecf20Sopenharmony_ci	u32 res = 0;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	spin_lock_irqsave(&extif->gpio_lock, flags);
1598c2ecf20Sopenharmony_ci	res = extif_write32_masked(extif, SSB_EXTIF_GPIO_OUT(0),
1608c2ecf20Sopenharmony_ci				   mask, value);
1618c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&extif->gpio_lock, flags);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	return res;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ciu32 ssb_extif_gpio_outen(struct ssb_extif *extif, u32 mask, u32 value)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	unsigned long flags;
1698c2ecf20Sopenharmony_ci	u32 res = 0;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	spin_lock_irqsave(&extif->gpio_lock, flags);
1728c2ecf20Sopenharmony_ci	res = extif_write32_masked(extif, SSB_EXTIF_GPIO_OUTEN(0),
1738c2ecf20Sopenharmony_ci				   mask, value);
1748c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&extif->gpio_lock, flags);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	return res;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ciu32 ssb_extif_gpio_polarity(struct ssb_extif *extif, u32 mask, u32 value)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	unsigned long flags;
1828c2ecf20Sopenharmony_ci	u32 res = 0;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	spin_lock_irqsave(&extif->gpio_lock, flags);
1858c2ecf20Sopenharmony_ci	res = extif_write32_masked(extif, SSB_EXTIF_GPIO_INTPOL, mask, value);
1868c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&extif->gpio_lock, flags);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	return res;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ciu32 ssb_extif_gpio_intmask(struct ssb_extif *extif, u32 mask, u32 value)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	unsigned long flags;
1948c2ecf20Sopenharmony_ci	u32 res = 0;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	spin_lock_irqsave(&extif->gpio_lock, flags);
1978c2ecf20Sopenharmony_ci	res = extif_write32_masked(extif, SSB_EXTIF_GPIO_INTMASK, mask, value);
1988c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&extif->gpio_lock, flags);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	return res;
2018c2ecf20Sopenharmony_ci}
202