161e115a5SMichael Buesch /* 261e115a5SMichael Buesch * Sonics Silicon Backplane 361e115a5SMichael Buesch * Broadcom ChipCommon core driver 461e115a5SMichael Buesch * 561e115a5SMichael Buesch * Copyright 2005, Broadcom Corporation 6eb032b98SMichael Büsch * Copyright 2006, 2007, Michael Buesch <m@bues.ch> 761e115a5SMichael Buesch * 861e115a5SMichael Buesch * Licensed under the GNU/GPL. See COPYING for details. 961e115a5SMichael Buesch */ 1061e115a5SMichael Buesch 1161e115a5SMichael Buesch #include <linux/ssb/ssb.h> 1261e115a5SMichael Buesch #include <linux/ssb/ssb_regs.h> 131014c22eSPaul Gortmaker #include <linux/export.h> 1461e115a5SMichael Buesch #include <linux/pci.h> 1561e115a5SMichael Buesch 1661e115a5SMichael Buesch #include "ssb_private.h" 1761e115a5SMichael Buesch 1861e115a5SMichael Buesch 1961e115a5SMichael Buesch /* Clock sources */ 2061e115a5SMichael Buesch enum ssb_clksrc { 2161e115a5SMichael Buesch /* PCI clock */ 2261e115a5SMichael Buesch SSB_CHIPCO_CLKSRC_PCI, 2361e115a5SMichael Buesch /* Crystal slow clock oscillator */ 2461e115a5SMichael Buesch SSB_CHIPCO_CLKSRC_XTALOS, 2561e115a5SMichael Buesch /* Low power oscillator */ 2661e115a5SMichael Buesch SSB_CHIPCO_CLKSRC_LOPWROS, 2761e115a5SMichael Buesch }; 2861e115a5SMichael Buesch 2961e115a5SMichael Buesch 30c2bcbe65SMichael Buesch static inline u32 chipco_write32_masked(struct ssb_chipcommon *cc, u16 offset, 3161e115a5SMichael Buesch u32 mask, u32 value) 3261e115a5SMichael Buesch { 3361e115a5SMichael Buesch value &= mask; 3461e115a5SMichael Buesch value |= chipco_read32(cc, offset) & ~mask; 3561e115a5SMichael Buesch chipco_write32(cc, offset, value); 36c2bcbe65SMichael Buesch 37c2bcbe65SMichael Buesch return value; 3861e115a5SMichael Buesch } 3961e115a5SMichael Buesch 4061e115a5SMichael Buesch void ssb_chipco_set_clockmode(struct ssb_chipcommon *cc, 4161e115a5SMichael Buesch enum ssb_clkmode mode) 4261e115a5SMichael Buesch { 4361e115a5SMichael Buesch struct ssb_device *ccdev = cc->dev; 4461e115a5SMichael Buesch struct ssb_bus *bus; 4561e115a5SMichael Buesch u32 tmp; 4661e115a5SMichael Buesch 4761e115a5SMichael Buesch if (!ccdev) 4861e115a5SMichael Buesch return; 4961e115a5SMichael Buesch bus = ccdev->bus; 500ca69955SRafał Miłecki 510ca69955SRafał Miłecki /* We support SLOW only on 6..9 */ 520ca69955SRafał Miłecki if (ccdev->id.revision >= 10 && mode == SSB_CLKMODE_SLOW) 530ca69955SRafał Miłecki mode = SSB_CLKMODE_DYNAMIC; 540ca69955SRafał Miłecki 550ca69955SRafał Miłecki if (cc->capabilities & SSB_CHIPCO_CAP_PMU) 560ca69955SRafał Miłecki return; /* PMU controls clockmode, separated function needed */ 570ca69955SRafał Miłecki SSB_WARN_ON(ccdev->id.revision >= 20); 580ca69955SRafał Miłecki 5961e115a5SMichael Buesch /* chipcommon cores prior to rev6 don't support dynamic clock control */ 6061e115a5SMichael Buesch if (ccdev->id.revision < 6) 6161e115a5SMichael Buesch return; 620ca69955SRafał Miłecki 630ca69955SRafał Miłecki /* ChipCommon cores rev10+ need testing */ 6461e115a5SMichael Buesch if (ccdev->id.revision >= 10) 6561e115a5SMichael Buesch return; 660ca69955SRafał Miłecki 6761e115a5SMichael Buesch if (!(cc->capabilities & SSB_CHIPCO_CAP_PCTL)) 6861e115a5SMichael Buesch return; 6961e115a5SMichael Buesch 7061e115a5SMichael Buesch switch (mode) { 710ca69955SRafał Miłecki case SSB_CLKMODE_SLOW: /* For revs 6..9 only */ 7261e115a5SMichael Buesch tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL); 7361e115a5SMichael Buesch tmp |= SSB_CHIPCO_SLOWCLKCTL_FSLOW; 7461e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_SLOWCLKCTL, tmp); 7561e115a5SMichael Buesch break; 7661e115a5SMichael Buesch case SSB_CLKMODE_FAST: 770ca69955SRafał Miłecki if (ccdev->id.revision < 10) { 7861e115a5SMichael Buesch ssb_pci_xtal(bus, SSB_GPIO_XTAL, 1); /* Force crystal on */ 7961e115a5SMichael Buesch tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL); 8061e115a5SMichael Buesch tmp &= ~SSB_CHIPCO_SLOWCLKCTL_FSLOW; 8161e115a5SMichael Buesch tmp |= SSB_CHIPCO_SLOWCLKCTL_IPLL; 8261e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_SLOWCLKCTL, tmp); 830ca69955SRafał Miłecki } else { 840ca69955SRafał Miłecki chipco_write32(cc, SSB_CHIPCO_SYSCLKCTL, 850ca69955SRafał Miłecki (chipco_read32(cc, SSB_CHIPCO_SYSCLKCTL) | 860ca69955SRafał Miłecki SSB_CHIPCO_SYSCLKCTL_FORCEHT)); 870ca69955SRafał Miłecki /* udelay(150); TODO: not available in early init */ 880ca69955SRafał Miłecki } 8961e115a5SMichael Buesch break; 9061e115a5SMichael Buesch case SSB_CLKMODE_DYNAMIC: 910ca69955SRafał Miłecki if (ccdev->id.revision < 10) { 9261e115a5SMichael Buesch tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL); 9361e115a5SMichael Buesch tmp &= ~SSB_CHIPCO_SLOWCLKCTL_FSLOW; 9461e115a5SMichael Buesch tmp &= ~SSB_CHIPCO_SLOWCLKCTL_IPLL; 9561e115a5SMichael Buesch tmp &= ~SSB_CHIPCO_SLOWCLKCTL_ENXTAL; 960ca69955SRafał Miłecki if ((tmp & SSB_CHIPCO_SLOWCLKCTL_SRC) != 970ca69955SRafał Miłecki SSB_CHIPCO_SLOWCLKCTL_SRC_XTAL) 9861e115a5SMichael Buesch tmp |= SSB_CHIPCO_SLOWCLKCTL_ENXTAL; 9961e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_SLOWCLKCTL, tmp); 10061e115a5SMichael Buesch 1010ca69955SRafał Miłecki /* For dynamic control, we have to release our xtal_pu 1020ca69955SRafał Miłecki * "force on" */ 10361e115a5SMichael Buesch if (tmp & SSB_CHIPCO_SLOWCLKCTL_ENXTAL) 10461e115a5SMichael Buesch ssb_pci_xtal(bus, SSB_GPIO_XTAL, 0); 1050ca69955SRafał Miłecki } else { 1060ca69955SRafał Miłecki chipco_write32(cc, SSB_CHIPCO_SYSCLKCTL, 1070ca69955SRafał Miłecki (chipco_read32(cc, SSB_CHIPCO_SYSCLKCTL) & 1080ca69955SRafał Miłecki ~SSB_CHIPCO_SYSCLKCTL_FORCEHT)); 1090ca69955SRafał Miłecki } 11061e115a5SMichael Buesch break; 11161e115a5SMichael Buesch default: 11261e115a5SMichael Buesch SSB_WARN_ON(1); 11361e115a5SMichael Buesch } 11461e115a5SMichael Buesch } 11561e115a5SMichael Buesch 11661e115a5SMichael Buesch /* Get the Slow Clock Source */ 11761e115a5SMichael Buesch static enum ssb_clksrc chipco_pctl_get_slowclksrc(struct ssb_chipcommon *cc) 11861e115a5SMichael Buesch { 11961e115a5SMichael Buesch struct ssb_bus *bus = cc->dev->bus; 12061e115a5SMichael Buesch u32 uninitialized_var(tmp); 12161e115a5SMichael Buesch 12261e115a5SMichael Buesch if (cc->dev->id.revision < 6) { 12361e115a5SMichael Buesch if (bus->bustype == SSB_BUSTYPE_SSB || 12461e115a5SMichael Buesch bus->bustype == SSB_BUSTYPE_PCMCIA) 12561e115a5SMichael Buesch return SSB_CHIPCO_CLKSRC_XTALOS; 12661e115a5SMichael Buesch if (bus->bustype == SSB_BUSTYPE_PCI) { 12761e115a5SMichael Buesch pci_read_config_dword(bus->host_pci, SSB_GPIO_OUT, &tmp); 12861e115a5SMichael Buesch if (tmp & 0x10) 12961e115a5SMichael Buesch return SSB_CHIPCO_CLKSRC_PCI; 13061e115a5SMichael Buesch return SSB_CHIPCO_CLKSRC_XTALOS; 13161e115a5SMichael Buesch } 13261e115a5SMichael Buesch } 13361e115a5SMichael Buesch if (cc->dev->id.revision < 10) { 13461e115a5SMichael Buesch tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL); 13561e115a5SMichael Buesch tmp &= 0x7; 13661e115a5SMichael Buesch if (tmp == 0) 13761e115a5SMichael Buesch return SSB_CHIPCO_CLKSRC_LOPWROS; 13861e115a5SMichael Buesch if (tmp == 1) 13961e115a5SMichael Buesch return SSB_CHIPCO_CLKSRC_XTALOS; 14061e115a5SMichael Buesch if (tmp == 2) 14161e115a5SMichael Buesch return SSB_CHIPCO_CLKSRC_PCI; 14261e115a5SMichael Buesch } 14361e115a5SMichael Buesch 14461e115a5SMichael Buesch return SSB_CHIPCO_CLKSRC_XTALOS; 14561e115a5SMichael Buesch } 14661e115a5SMichael Buesch 14761e115a5SMichael Buesch /* Get maximum or minimum (depending on get_max flag) slowclock frequency. */ 14861e115a5SMichael Buesch static int chipco_pctl_clockfreqlimit(struct ssb_chipcommon *cc, int get_max) 14961e115a5SMichael Buesch { 15061e115a5SMichael Buesch int uninitialized_var(limit); 15161e115a5SMichael Buesch enum ssb_clksrc clocksrc; 15261e115a5SMichael Buesch int divisor = 1; 15361e115a5SMichael Buesch u32 tmp; 15461e115a5SMichael Buesch 15561e115a5SMichael Buesch clocksrc = chipco_pctl_get_slowclksrc(cc); 15661e115a5SMichael Buesch if (cc->dev->id.revision < 6) { 15761e115a5SMichael Buesch switch (clocksrc) { 15861e115a5SMichael Buesch case SSB_CHIPCO_CLKSRC_PCI: 15961e115a5SMichael Buesch divisor = 64; 16061e115a5SMichael Buesch break; 16161e115a5SMichael Buesch case SSB_CHIPCO_CLKSRC_XTALOS: 16261e115a5SMichael Buesch divisor = 32; 16361e115a5SMichael Buesch break; 16461e115a5SMichael Buesch default: 16561e115a5SMichael Buesch SSB_WARN_ON(1); 16661e115a5SMichael Buesch } 16761e115a5SMichael Buesch } else if (cc->dev->id.revision < 10) { 16861e115a5SMichael Buesch switch (clocksrc) { 16961e115a5SMichael Buesch case SSB_CHIPCO_CLKSRC_LOPWROS: 17061e115a5SMichael Buesch break; 17161e115a5SMichael Buesch case SSB_CHIPCO_CLKSRC_XTALOS: 17261e115a5SMichael Buesch case SSB_CHIPCO_CLKSRC_PCI: 17361e115a5SMichael Buesch tmp = chipco_read32(cc, SSB_CHIPCO_SLOWCLKCTL); 17461e115a5SMichael Buesch divisor = (tmp >> 16) + 1; 17561e115a5SMichael Buesch divisor *= 4; 17661e115a5SMichael Buesch break; 17761e115a5SMichael Buesch } 17861e115a5SMichael Buesch } else { 17961e115a5SMichael Buesch tmp = chipco_read32(cc, SSB_CHIPCO_SYSCLKCTL); 18061e115a5SMichael Buesch divisor = (tmp >> 16) + 1; 18161e115a5SMichael Buesch divisor *= 4; 18261e115a5SMichael Buesch } 18361e115a5SMichael Buesch 18461e115a5SMichael Buesch switch (clocksrc) { 18561e115a5SMichael Buesch case SSB_CHIPCO_CLKSRC_LOPWROS: 18661e115a5SMichael Buesch if (get_max) 18761e115a5SMichael Buesch limit = 43000; 18861e115a5SMichael Buesch else 18961e115a5SMichael Buesch limit = 25000; 19061e115a5SMichael Buesch break; 19161e115a5SMichael Buesch case SSB_CHIPCO_CLKSRC_XTALOS: 19261e115a5SMichael Buesch if (get_max) 19361e115a5SMichael Buesch limit = 20200000; 19461e115a5SMichael Buesch else 19561e115a5SMichael Buesch limit = 19800000; 19661e115a5SMichael Buesch break; 19761e115a5SMichael Buesch case SSB_CHIPCO_CLKSRC_PCI: 19861e115a5SMichael Buesch if (get_max) 19961e115a5SMichael Buesch limit = 34000000; 20061e115a5SMichael Buesch else 20161e115a5SMichael Buesch limit = 25000000; 20261e115a5SMichael Buesch break; 20361e115a5SMichael Buesch } 20461e115a5SMichael Buesch limit /= divisor; 20561e115a5SMichael Buesch 20661e115a5SMichael Buesch return limit; 20761e115a5SMichael Buesch } 20861e115a5SMichael Buesch 20961e115a5SMichael Buesch static void chipco_powercontrol_init(struct ssb_chipcommon *cc) 21061e115a5SMichael Buesch { 21161e115a5SMichael Buesch struct ssb_bus *bus = cc->dev->bus; 21261e115a5SMichael Buesch 21361e115a5SMichael Buesch if (bus->chip_id == 0x4321) { 21461e115a5SMichael Buesch if (bus->chip_rev == 0) 21561e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_CHIPCTL, 0x3A4); 21661e115a5SMichael Buesch else if (bus->chip_rev == 1) 21761e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_CHIPCTL, 0xA4); 21861e115a5SMichael Buesch } 21961e115a5SMichael Buesch 22061e115a5SMichael Buesch if (!(cc->capabilities & SSB_CHIPCO_CAP_PCTL)) 22161e115a5SMichael Buesch return; 22261e115a5SMichael Buesch 22361e115a5SMichael Buesch if (cc->dev->id.revision >= 10) { 22461e115a5SMichael Buesch /* Set Idle Power clock rate to 1Mhz */ 22561e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_SYSCLKCTL, 22661e115a5SMichael Buesch (chipco_read32(cc, SSB_CHIPCO_SYSCLKCTL) & 22761e115a5SMichael Buesch 0x0000FFFF) | 0x00040000); 22861e115a5SMichael Buesch } else { 22961e115a5SMichael Buesch int maxfreq; 23061e115a5SMichael Buesch 23161e115a5SMichael Buesch maxfreq = chipco_pctl_clockfreqlimit(cc, 1); 23261e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_PLLONDELAY, 23361e115a5SMichael Buesch (maxfreq * 150 + 999999) / 1000000); 23461e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_FREFSELDELAY, 23561e115a5SMichael Buesch (maxfreq * 15 + 999999) / 1000000); 23661e115a5SMichael Buesch } 23761e115a5SMichael Buesch } 23861e115a5SMichael Buesch 239fd515941SRafał Miłecki /* http://bcm-v4.sipsolutions.net/802.11/PmuFastPwrupDelay */ 240fd515941SRafał Miłecki static u16 pmu_fast_powerup_delay(struct ssb_chipcommon *cc) 241fd515941SRafał Miłecki { 242fd515941SRafał Miłecki struct ssb_bus *bus = cc->dev->bus; 243fd515941SRafał Miłecki 244fd515941SRafał Miłecki switch (bus->chip_id) { 245fd515941SRafał Miłecki case 0x4312: 246fd515941SRafał Miłecki case 0x4322: 247fd515941SRafał Miłecki case 0x4328: 248fd515941SRafał Miłecki return 7000; 249fd515941SRafał Miłecki case 0x4325: 250fd515941SRafał Miłecki /* TODO: */ 251fd515941SRafał Miłecki default: 252fd515941SRafał Miłecki return 15000; 253fd515941SRafał Miłecki } 254fd515941SRafał Miłecki } 255fd515941SRafał Miłecki 256fd515941SRafał Miłecki /* http://bcm-v4.sipsolutions.net/802.11/ClkctlFastPwrupDelay */ 25761e115a5SMichael Buesch static void calc_fast_powerup_delay(struct ssb_chipcommon *cc) 25861e115a5SMichael Buesch { 25961e115a5SMichael Buesch struct ssb_bus *bus = cc->dev->bus; 26061e115a5SMichael Buesch int minfreq; 26161e115a5SMichael Buesch unsigned int tmp; 26261e115a5SMichael Buesch u32 pll_on_delay; 26361e115a5SMichael Buesch 26461e115a5SMichael Buesch if (bus->bustype != SSB_BUSTYPE_PCI) 26561e115a5SMichael Buesch return; 266fd515941SRafał Miłecki 267fd515941SRafał Miłecki if (cc->capabilities & SSB_CHIPCO_CAP_PMU) { 268fd515941SRafał Miłecki cc->fast_pwrup_delay = pmu_fast_powerup_delay(cc); 269fd515941SRafał Miłecki return; 270fd515941SRafał Miłecki } 271fd515941SRafał Miłecki 27261e115a5SMichael Buesch if (!(cc->capabilities & SSB_CHIPCO_CAP_PCTL)) 27361e115a5SMichael Buesch return; 27461e115a5SMichael Buesch 27561e115a5SMichael Buesch minfreq = chipco_pctl_clockfreqlimit(cc, 0); 27661e115a5SMichael Buesch pll_on_delay = chipco_read32(cc, SSB_CHIPCO_PLLONDELAY); 27761e115a5SMichael Buesch tmp = (((pll_on_delay + 2) * 1000000) + (minfreq - 1)) / minfreq; 27861e115a5SMichael Buesch SSB_WARN_ON(tmp & ~0xFFFF); 27961e115a5SMichael Buesch 28061e115a5SMichael Buesch cc->fast_pwrup_delay = tmp; 28161e115a5SMichael Buesch } 28261e115a5SMichael Buesch 28361e115a5SMichael Buesch void ssb_chipcommon_init(struct ssb_chipcommon *cc) 28461e115a5SMichael Buesch { 28561e115a5SMichael Buesch if (!cc->dev) 28661e115a5SMichael Buesch return; /* We don't have a ChipCommon */ 287*394bc7e3SHauke Mehrtens 288*394bc7e3SHauke Mehrtens spin_lock_init(&cc->gpio_lock); 289*394bc7e3SHauke Mehrtens 290d53cdbb9SJohn W. Linville if (cc->dev->id.revision >= 11) 291d53cdbb9SJohn W. Linville cc->status = chipco_read32(cc, SSB_CHIPCO_CHIPSTAT); 2929d1ac34eSLarry Finger ssb_dprintk(KERN_INFO PFX "chipcommon status is 0x%x\n", cc->status); 2939835a30eSRafał Miłecki 2949835a30eSRafał Miłecki if (cc->dev->id.revision >= 20) { 2959835a30eSRafał Miłecki chipco_write32(cc, SSB_CHIPCO_GPIOPULLUP, 0); 2969835a30eSRafał Miłecki chipco_write32(cc, SSB_CHIPCO_GPIOPULLDOWN, 0); 2979835a30eSRafał Miłecki } 2989835a30eSRafał Miłecki 299c9703146SMichael Buesch ssb_pmu_init(cc); 30061e115a5SMichael Buesch chipco_powercontrol_init(cc); 30161e115a5SMichael Buesch ssb_chipco_set_clockmode(cc, SSB_CLKMODE_FAST); 30261e115a5SMichael Buesch calc_fast_powerup_delay(cc); 30361e115a5SMichael Buesch } 30461e115a5SMichael Buesch 3058fe2b65aSMichael Buesch void ssb_chipco_suspend(struct ssb_chipcommon *cc) 30661e115a5SMichael Buesch { 30761e115a5SMichael Buesch if (!cc->dev) 30861e115a5SMichael Buesch return; 30961e115a5SMichael Buesch ssb_chipco_set_clockmode(cc, SSB_CLKMODE_SLOW); 31061e115a5SMichael Buesch } 31161e115a5SMichael Buesch 31261e115a5SMichael Buesch void ssb_chipco_resume(struct ssb_chipcommon *cc) 31361e115a5SMichael Buesch { 31461e115a5SMichael Buesch if (!cc->dev) 31561e115a5SMichael Buesch return; 31661e115a5SMichael Buesch chipco_powercontrol_init(cc); 31761e115a5SMichael Buesch ssb_chipco_set_clockmode(cc, SSB_CLKMODE_FAST); 31861e115a5SMichael Buesch } 31961e115a5SMichael Buesch 32061e115a5SMichael Buesch /* Get the processor clock */ 32161e115a5SMichael Buesch void ssb_chipco_get_clockcpu(struct ssb_chipcommon *cc, 32261e115a5SMichael Buesch u32 *plltype, u32 *n, u32 *m) 32361e115a5SMichael Buesch { 32461e115a5SMichael Buesch *n = chipco_read32(cc, SSB_CHIPCO_CLOCK_N); 32561e115a5SMichael Buesch *plltype = (cc->capabilities & SSB_CHIPCO_CAP_PLLT); 32661e115a5SMichael Buesch switch (*plltype) { 32761e115a5SMichael Buesch case SSB_PLLTYPE_2: 32861e115a5SMichael Buesch case SSB_PLLTYPE_4: 32961e115a5SMichael Buesch case SSB_PLLTYPE_6: 33061e115a5SMichael Buesch case SSB_PLLTYPE_7: 33161e115a5SMichael Buesch *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_MIPS); 33261e115a5SMichael Buesch break; 33361e115a5SMichael Buesch case SSB_PLLTYPE_3: 33461e115a5SMichael Buesch /* 5350 uses m2 to control mips */ 33561e115a5SMichael Buesch *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_M2); 33661e115a5SMichael Buesch break; 33761e115a5SMichael Buesch default: 33861e115a5SMichael Buesch *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_SB); 33961e115a5SMichael Buesch break; 34061e115a5SMichael Buesch } 34161e115a5SMichael Buesch } 34261e115a5SMichael Buesch 34361e115a5SMichael Buesch /* Get the bus clock */ 34461e115a5SMichael Buesch void ssb_chipco_get_clockcontrol(struct ssb_chipcommon *cc, 34561e115a5SMichael Buesch u32 *plltype, u32 *n, u32 *m) 34661e115a5SMichael Buesch { 34761e115a5SMichael Buesch *n = chipco_read32(cc, SSB_CHIPCO_CLOCK_N); 34861e115a5SMichael Buesch *plltype = (cc->capabilities & SSB_CHIPCO_CAP_PLLT); 34961e115a5SMichael Buesch switch (*plltype) { 35061e115a5SMichael Buesch case SSB_PLLTYPE_6: /* 100/200 or 120/240 only */ 35161e115a5SMichael Buesch *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_MIPS); 35261e115a5SMichael Buesch break; 35361e115a5SMichael Buesch case SSB_PLLTYPE_3: /* 25Mhz, 2 dividers */ 35461e115a5SMichael Buesch if (cc->dev->bus->chip_id != 0x5365) { 35561e115a5SMichael Buesch *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_M2); 35661e115a5SMichael Buesch break; 35761e115a5SMichael Buesch } 35861e115a5SMichael Buesch /* Fallthough */ 35961e115a5SMichael Buesch default: 36061e115a5SMichael Buesch *m = chipco_read32(cc, SSB_CHIPCO_CLOCK_SB); 36161e115a5SMichael Buesch } 36261e115a5SMichael Buesch } 36361e115a5SMichael Buesch 36461e115a5SMichael Buesch void ssb_chipco_timing_init(struct ssb_chipcommon *cc, 36561e115a5SMichael Buesch unsigned long ns) 36661e115a5SMichael Buesch { 36761e115a5SMichael Buesch struct ssb_device *dev = cc->dev; 36861e115a5SMichael Buesch struct ssb_bus *bus = dev->bus; 36961e115a5SMichael Buesch u32 tmp; 37061e115a5SMichael Buesch 37161e115a5SMichael Buesch /* set register for external IO to control LED. */ 37261e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_PROG_CFG, 0x11); 37361e115a5SMichael Buesch tmp = DIV_ROUND_UP(10, ns) << SSB_PROG_WCNT_3_SHIFT; /* Waitcount-3 = 10ns */ 37461e115a5SMichael Buesch tmp |= DIV_ROUND_UP(40, ns) << SSB_PROG_WCNT_1_SHIFT; /* Waitcount-1 = 40ns */ 37561e115a5SMichael Buesch tmp |= DIV_ROUND_UP(240, ns); /* Waitcount-0 = 240ns */ 37661e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_PROG_WAITCNT, tmp); /* 0x01020a0c for a 100Mhz clock */ 37761e115a5SMichael Buesch 37861e115a5SMichael Buesch /* Set timing for the flash */ 37961e115a5SMichael Buesch tmp = DIV_ROUND_UP(10, ns) << SSB_FLASH_WCNT_3_SHIFT; /* Waitcount-3 = 10nS */ 38061e115a5SMichael Buesch tmp |= DIV_ROUND_UP(10, ns) << SSB_FLASH_WCNT_1_SHIFT; /* Waitcount-1 = 10nS */ 38161e115a5SMichael Buesch tmp |= DIV_ROUND_UP(120, ns); /* Waitcount-0 = 120nS */ 38261e115a5SMichael Buesch if ((bus->chip_id == 0x5365) || 38361e115a5SMichael Buesch (dev->id.revision < 9)) 38461e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_FLASH_WAITCNT, tmp); 38561e115a5SMichael Buesch if ((bus->chip_id == 0x5365) || 38661e115a5SMichael Buesch (dev->id.revision < 9) || 38761e115a5SMichael Buesch ((bus->chip_id == 0x5350) && (bus->chip_rev == 0))) 38861e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_PCMCIA_MEMWAIT, tmp); 38961e115a5SMichael Buesch 39061e115a5SMichael Buesch if (bus->chip_id == 0x5350) { 39161e115a5SMichael Buesch /* Enable EXTIF */ 39261e115a5SMichael Buesch tmp = DIV_ROUND_UP(10, ns) << SSB_PROG_WCNT_3_SHIFT; /* Waitcount-3 = 10ns */ 39361e115a5SMichael Buesch tmp |= DIV_ROUND_UP(20, ns) << SSB_PROG_WCNT_2_SHIFT; /* Waitcount-2 = 20ns */ 39461e115a5SMichael Buesch tmp |= DIV_ROUND_UP(100, ns) << SSB_PROG_WCNT_1_SHIFT; /* Waitcount-1 = 100ns */ 39561e115a5SMichael Buesch tmp |= DIV_ROUND_UP(120, ns); /* Waitcount-0 = 120ns */ 39661e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_PROG_WAITCNT, tmp); /* 0x01020a0c for a 100Mhz clock */ 39761e115a5SMichael Buesch } 39861e115a5SMichael Buesch } 39961e115a5SMichael Buesch 40061e115a5SMichael Buesch /* Set chip watchdog reset timer to fire in 'ticks' backplane cycles */ 4016b9bafecSMichael Buesch void ssb_chipco_watchdog_timer_set(struct ssb_chipcommon *cc, u32 ticks) 40261e115a5SMichael Buesch { 40361e115a5SMichael Buesch /* instant NMI */ 40461e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_WATCHDOG, ticks); 40561e115a5SMichael Buesch } 40661e115a5SMichael Buesch 40728de57d1SAurelien Jarno void ssb_chipco_irq_mask(struct ssb_chipcommon *cc, u32 mask, u32 value) 40828de57d1SAurelien Jarno { 40928de57d1SAurelien Jarno chipco_write32_masked(cc, SSB_CHIPCO_IRQMASK, mask, value); 41028de57d1SAurelien Jarno } 41128de57d1SAurelien Jarno 41228de57d1SAurelien Jarno u32 ssb_chipco_irq_status(struct ssb_chipcommon *cc, u32 mask) 41328de57d1SAurelien Jarno { 41428de57d1SAurelien Jarno return chipco_read32(cc, SSB_CHIPCO_IRQSTAT) & mask; 41528de57d1SAurelien Jarno } 41628de57d1SAurelien Jarno 41761e115a5SMichael Buesch u32 ssb_chipco_gpio_in(struct ssb_chipcommon *cc, u32 mask) 41861e115a5SMichael Buesch { 41961e115a5SMichael Buesch return chipco_read32(cc, SSB_CHIPCO_GPIOIN) & mask; 42061e115a5SMichael Buesch } 42161e115a5SMichael Buesch 422c2bcbe65SMichael Buesch u32 ssb_chipco_gpio_out(struct ssb_chipcommon *cc, u32 mask, u32 value) 42361e115a5SMichael Buesch { 424*394bc7e3SHauke Mehrtens unsigned long flags; 425*394bc7e3SHauke Mehrtens u32 res = 0; 426*394bc7e3SHauke Mehrtens 427*394bc7e3SHauke Mehrtens spin_lock_irqsave(&cc->gpio_lock, flags); 428*394bc7e3SHauke Mehrtens res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOOUT, mask, value); 429*394bc7e3SHauke Mehrtens spin_unlock_irqrestore(&cc->gpio_lock, flags); 430*394bc7e3SHauke Mehrtens 431*394bc7e3SHauke Mehrtens return res; 43261e115a5SMichael Buesch } 43361e115a5SMichael Buesch 434c2bcbe65SMichael Buesch u32 ssb_chipco_gpio_outen(struct ssb_chipcommon *cc, u32 mask, u32 value) 43561e115a5SMichael Buesch { 436*394bc7e3SHauke Mehrtens unsigned long flags; 437*394bc7e3SHauke Mehrtens u32 res = 0; 438*394bc7e3SHauke Mehrtens 439*394bc7e3SHauke Mehrtens spin_lock_irqsave(&cc->gpio_lock, flags); 440*394bc7e3SHauke Mehrtens res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOOUTEN, mask, value); 441*394bc7e3SHauke Mehrtens spin_unlock_irqrestore(&cc->gpio_lock, flags); 442*394bc7e3SHauke Mehrtens 443*394bc7e3SHauke Mehrtens return res; 44461e115a5SMichael Buesch } 445c2bcbe65SMichael Buesch 446c2bcbe65SMichael Buesch u32 ssb_chipco_gpio_control(struct ssb_chipcommon *cc, u32 mask, u32 value) 447c2bcbe65SMichael Buesch { 448*394bc7e3SHauke Mehrtens unsigned long flags; 449*394bc7e3SHauke Mehrtens u32 res = 0; 450*394bc7e3SHauke Mehrtens 451*394bc7e3SHauke Mehrtens spin_lock_irqsave(&cc->gpio_lock, flags); 452*394bc7e3SHauke Mehrtens res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOCTL, mask, value); 453*394bc7e3SHauke Mehrtens spin_unlock_irqrestore(&cc->gpio_lock, flags); 454*394bc7e3SHauke Mehrtens 455*394bc7e3SHauke Mehrtens return res; 456c2bcbe65SMichael Buesch } 45785373ee8SLarry Finger EXPORT_SYMBOL(ssb_chipco_gpio_control); 458c2bcbe65SMichael Buesch 459c2bcbe65SMichael Buesch u32 ssb_chipco_gpio_intmask(struct ssb_chipcommon *cc, u32 mask, u32 value) 460c2bcbe65SMichael Buesch { 461*394bc7e3SHauke Mehrtens unsigned long flags; 462*394bc7e3SHauke Mehrtens u32 res = 0; 463*394bc7e3SHauke Mehrtens 464*394bc7e3SHauke Mehrtens spin_lock_irqsave(&cc->gpio_lock, flags); 465*394bc7e3SHauke Mehrtens res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOIRQ, mask, value); 466*394bc7e3SHauke Mehrtens spin_unlock_irqrestore(&cc->gpio_lock, flags); 467*394bc7e3SHauke Mehrtens 468*394bc7e3SHauke Mehrtens return res; 469c2bcbe65SMichael Buesch } 470c2bcbe65SMichael Buesch 471c2bcbe65SMichael Buesch u32 ssb_chipco_gpio_polarity(struct ssb_chipcommon *cc, u32 mask, u32 value) 472c2bcbe65SMichael Buesch { 473*394bc7e3SHauke Mehrtens unsigned long flags; 474*394bc7e3SHauke Mehrtens u32 res = 0; 475*394bc7e3SHauke Mehrtens 476*394bc7e3SHauke Mehrtens spin_lock_irqsave(&cc->gpio_lock, flags); 477*394bc7e3SHauke Mehrtens res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOPOL, mask, value); 478*394bc7e3SHauke Mehrtens spin_unlock_irqrestore(&cc->gpio_lock, flags); 479*394bc7e3SHauke Mehrtens 480*394bc7e3SHauke Mehrtens return res; 481c2bcbe65SMichael Buesch } 48261e115a5SMichael Buesch 483da22f22eSHauke Mehrtens u32 ssb_chipco_gpio_pullup(struct ssb_chipcommon *cc, u32 mask, u32 value) 484da22f22eSHauke Mehrtens { 485*394bc7e3SHauke Mehrtens unsigned long flags; 486*394bc7e3SHauke Mehrtens u32 res = 0; 487*394bc7e3SHauke Mehrtens 488da22f22eSHauke Mehrtens if (cc->dev->id.revision < 20) 489da22f22eSHauke Mehrtens return 0xffffffff; 490da22f22eSHauke Mehrtens 491*394bc7e3SHauke Mehrtens spin_lock_irqsave(&cc->gpio_lock, flags); 492*394bc7e3SHauke Mehrtens res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOPULLUP, mask, value); 493*394bc7e3SHauke Mehrtens spin_unlock_irqrestore(&cc->gpio_lock, flags); 494*394bc7e3SHauke Mehrtens 495*394bc7e3SHauke Mehrtens return res; 496da22f22eSHauke Mehrtens } 497da22f22eSHauke Mehrtens 498da22f22eSHauke Mehrtens u32 ssb_chipco_gpio_pulldown(struct ssb_chipcommon *cc, u32 mask, u32 value) 499da22f22eSHauke Mehrtens { 500*394bc7e3SHauke Mehrtens unsigned long flags; 501*394bc7e3SHauke Mehrtens u32 res = 0; 502*394bc7e3SHauke Mehrtens 503da22f22eSHauke Mehrtens if (cc->dev->id.revision < 20) 504da22f22eSHauke Mehrtens return 0xffffffff; 505da22f22eSHauke Mehrtens 506*394bc7e3SHauke Mehrtens spin_lock_irqsave(&cc->gpio_lock, flags); 507*394bc7e3SHauke Mehrtens res = chipco_write32_masked(cc, SSB_CHIPCO_GPIOPULLDOWN, mask, value); 508*394bc7e3SHauke Mehrtens spin_unlock_irqrestore(&cc->gpio_lock, flags); 509*394bc7e3SHauke Mehrtens 510*394bc7e3SHauke Mehrtens return res; 511da22f22eSHauke Mehrtens } 512da22f22eSHauke Mehrtens 51361e115a5SMichael Buesch #ifdef CONFIG_SSB_SERIAL 51461e115a5SMichael Buesch int ssb_chipco_serial_init(struct ssb_chipcommon *cc, 51561e115a5SMichael Buesch struct ssb_serial_port *ports) 51661e115a5SMichael Buesch { 51761e115a5SMichael Buesch struct ssb_bus *bus = cc->dev->bus; 51861e115a5SMichael Buesch int nr_ports = 0; 51961e115a5SMichael Buesch u32 plltype; 52061e115a5SMichael Buesch unsigned int irq; 52161e115a5SMichael Buesch u32 baud_base, div; 52261e115a5SMichael Buesch u32 i, n; 52358ff70d4SMichael Buesch unsigned int ccrev = cc->dev->id.revision; 52461e115a5SMichael Buesch 52561e115a5SMichael Buesch plltype = (cc->capabilities & SSB_CHIPCO_CAP_PLLT); 52661e115a5SMichael Buesch irq = ssb_mips_irq(cc->dev); 52761e115a5SMichael Buesch 52861e115a5SMichael Buesch if (plltype == SSB_PLLTYPE_1) { 52961e115a5SMichael Buesch /* PLL clock */ 53061e115a5SMichael Buesch baud_base = ssb_calc_clock_rate(plltype, 53161e115a5SMichael Buesch chipco_read32(cc, SSB_CHIPCO_CLOCK_N), 53261e115a5SMichael Buesch chipco_read32(cc, SSB_CHIPCO_CLOCK_M2)); 53361e115a5SMichael Buesch div = 1; 53461e115a5SMichael Buesch } else { 53558ff70d4SMichael Buesch if (ccrev == 20) { 53658ff70d4SMichael Buesch /* BCM5354 uses constant 25MHz clock */ 53758ff70d4SMichael Buesch baud_base = 25000000; 53858ff70d4SMichael Buesch div = 48; 53961e115a5SMichael Buesch /* Set the override bit so we don't divide it */ 54061e115a5SMichael Buesch chipco_write32(cc, SSB_CHIPCO_CORECTL, 54158ff70d4SMichael Buesch chipco_read32(cc, SSB_CHIPCO_CORECTL) 54258ff70d4SMichael Buesch | SSB_CHIPCO_CORECTL_UARTCLK0); 54358ff70d4SMichael Buesch } else if ((ccrev >= 11) && (ccrev != 15)) { 54458ff70d4SMichael Buesch /* Fixed ALP clock */ 54558ff70d4SMichael Buesch baud_base = 20000000; 54658ff70d4SMichael Buesch if (cc->capabilities & SSB_CHIPCO_CAP_PMU) { 54758ff70d4SMichael Buesch /* FIXME: baud_base is different for devices with a PMU */ 54858ff70d4SMichael Buesch SSB_WARN_ON(1); 54958ff70d4SMichael Buesch } 55058ff70d4SMichael Buesch div = 1; 55158ff70d4SMichael Buesch if (ccrev >= 21) { 55258ff70d4SMichael Buesch /* Turn off UART clock before switching clocksource. */ 55358ff70d4SMichael Buesch chipco_write32(cc, SSB_CHIPCO_CORECTL, 55458ff70d4SMichael Buesch chipco_read32(cc, SSB_CHIPCO_CORECTL) 55558ff70d4SMichael Buesch & ~SSB_CHIPCO_CORECTL_UARTCLKEN); 55658ff70d4SMichael Buesch } 55758ff70d4SMichael Buesch /* Set the override bit so we don't divide it */ 55858ff70d4SMichael Buesch chipco_write32(cc, SSB_CHIPCO_CORECTL, 55958ff70d4SMichael Buesch chipco_read32(cc, SSB_CHIPCO_CORECTL) 56058ff70d4SMichael Buesch | SSB_CHIPCO_CORECTL_UARTCLK0); 56158ff70d4SMichael Buesch if (ccrev >= 21) { 56258ff70d4SMichael Buesch /* Re-enable the UART clock. */ 56358ff70d4SMichael Buesch chipco_write32(cc, SSB_CHIPCO_CORECTL, 56458ff70d4SMichael Buesch chipco_read32(cc, SSB_CHIPCO_CORECTL) 56558ff70d4SMichael Buesch | SSB_CHIPCO_CORECTL_UARTCLKEN); 56658ff70d4SMichael Buesch } 56758ff70d4SMichael Buesch } else if (ccrev >= 3) { 56861e115a5SMichael Buesch /* Internal backplane clock */ 56961e115a5SMichael Buesch baud_base = ssb_clockspeed(bus); 57061e115a5SMichael Buesch div = chipco_read32(cc, SSB_CHIPCO_CLKDIV) 57161e115a5SMichael Buesch & SSB_CHIPCO_CLKDIV_UART; 57261e115a5SMichael Buesch } else { 57361e115a5SMichael Buesch /* Fixed internal backplane clock */ 57461e115a5SMichael Buesch baud_base = 88000000; 57561e115a5SMichael Buesch div = 48; 57661e115a5SMichael Buesch } 57761e115a5SMichael Buesch 57861e115a5SMichael Buesch /* Clock source depends on strapping if UartClkOverride is unset */ 57958ff70d4SMichael Buesch if ((ccrev > 0) && 58061e115a5SMichael Buesch !(chipco_read32(cc, SSB_CHIPCO_CORECTL) & SSB_CHIPCO_CORECTL_UARTCLK0)) { 58161e115a5SMichael Buesch if ((cc->capabilities & SSB_CHIPCO_CAP_UARTCLK) == 58261e115a5SMichael Buesch SSB_CHIPCO_CAP_UARTCLK_INT) { 58361e115a5SMichael Buesch /* Internal divided backplane clock */ 58461e115a5SMichael Buesch baud_base /= div; 58561e115a5SMichael Buesch } else { 58661e115a5SMichael Buesch /* Assume external clock of 1.8432 MHz */ 58761e115a5SMichael Buesch baud_base = 1843200; 58861e115a5SMichael Buesch } 58961e115a5SMichael Buesch } 59061e115a5SMichael Buesch } 59161e115a5SMichael Buesch 59261e115a5SMichael Buesch /* Determine the registers of the UARTs */ 59361e115a5SMichael Buesch n = (cc->capabilities & SSB_CHIPCO_CAP_NRUART); 59461e115a5SMichael Buesch for (i = 0; i < n; i++) { 59561e115a5SMichael Buesch void __iomem *cc_mmio; 59661e115a5SMichael Buesch void __iomem *uart_regs; 59761e115a5SMichael Buesch 59861e115a5SMichael Buesch cc_mmio = cc->dev->bus->mmio + (cc->dev->core_index * SSB_CORE_SIZE); 59961e115a5SMichael Buesch uart_regs = cc_mmio + SSB_CHIPCO_UART0_DATA; 60061e115a5SMichael Buesch /* Offset changed at after rev 0 */ 60158ff70d4SMichael Buesch if (ccrev == 0) 60261e115a5SMichael Buesch uart_regs += (i * 8); 60361e115a5SMichael Buesch else 60461e115a5SMichael Buesch uart_regs += (i * 256); 60561e115a5SMichael Buesch 60661e115a5SMichael Buesch nr_ports++; 60761e115a5SMichael Buesch ports[i].regs = uart_regs; 60861e115a5SMichael Buesch ports[i].irq = irq; 60961e115a5SMichael Buesch ports[i].baud_base = baud_base; 61061e115a5SMichael Buesch ports[i].reg_shift = 0; 61161e115a5SMichael Buesch } 61261e115a5SMichael Buesch 61361e115a5SMichael Buesch return nr_ports; 61461e115a5SMichael Buesch } 61561e115a5SMichael Buesch #endif /* CONFIG_SSB_SERIAL */ 616