driver_pcicore.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * Sonics Silicon Backplane
  3. * Broadcom PCI-core driver
  4. *
  5. * Copyright 2005, Broadcom Corporation
  6. * Copyright 2006, 2007, Michael Buesch <mb@bu3sch.de>
  7. *
  8. * Licensed under the GNU/GPL. See COPYING for details.
  9. */
  10. #include <linux/ssb/ssb.h>
  11. #include <linux/pci.h>
  12. #include <linux/delay.h>
  13. #include <linux/ssb/ssb_embedded.h>
  14. #include "ssb_private.h"
  15. static u32 ssb_pcie_read(struct ssb_pcicore *pc, u32 address);
  16. static void ssb_pcie_write(struct ssb_pcicore *pc, u32 address, u32 data);
  17. static u16 ssb_pcie_mdio_read(struct ssb_pcicore *pc, u8 device, u8 address);
  18. static void ssb_pcie_mdio_write(struct ssb_pcicore *pc, u8 device,
  19. u8 address, u16 data);
  20. static inline
  21. u32 pcicore_read32(struct ssb_pcicore *pc, u16 offset)
  22. {
  23. return ssb_read32(pc->dev, offset);
  24. }
  25. static inline
  26. void pcicore_write32(struct ssb_pcicore *pc, u16 offset, u32 value)
  27. {
  28. ssb_write32(pc->dev, offset, value);
  29. }
  30. static inline
  31. u16 pcicore_read16(struct ssb_pcicore *pc, u16 offset)
  32. {
  33. return ssb_read16(pc->dev, offset);
  34. }
  35. static inline
  36. void pcicore_write16(struct ssb_pcicore *pc, u16 offset, u16 value)
  37. {
  38. ssb_write16(pc->dev, offset, value);
  39. }
  40. /**************************************************
  41. * Code for hostmode operation.
  42. **************************************************/
  43. #ifdef CONFIG_SSB_PCICORE_HOSTMODE
  44. #include <asm/paccess.h>
  45. /* Probe a 32bit value on the bus and catch bus exceptions.
  46. * Returns nonzero on a bus exception.
  47. * This is MIPS specific */
  48. #define mips_busprobe32(val, addr) get_dbe((val), ((u32 *)(addr)))
  49. /* Assume one-hot slot wiring */
  50. #define SSB_PCI_SLOT_MAX 16
  51. /* Global lock is OK, as we won't have more than one extpci anyway. */
  52. static DEFINE_SPINLOCK(cfgspace_lock);
  53. /* Core to access the external PCI config space. Can only have one. */
  54. static struct ssb_pcicore *extpci_core;
  55. static u32 get_cfgspace_addr(struct ssb_pcicore *pc,
  56. unsigned int bus, unsigned int dev,
  57. unsigned int func, unsigned int off)
  58. {
  59. u32 addr = 0;
  60. u32 tmp;
  61. /* We do only have one cardbus device behind the bridge. */
  62. if (pc->cardbusmode && (dev >= 1))
  63. goto out;
  64. if (bus == 0) {
  65. /* Type 0 transaction */
  66. if (unlikely(dev >= SSB_PCI_SLOT_MAX))
  67. goto out;
  68. /* Slide the window */
  69. tmp = SSB_PCICORE_SBTOPCI_CFG0;
  70. tmp |= ((1 << (dev + 16)) & SSB_PCICORE_SBTOPCI1_MASK);
  71. pcicore_write32(pc, SSB_PCICORE_SBTOPCI1, tmp);
  72. /* Calculate the address */
  73. addr = SSB_PCI_CFG;
  74. addr |= ((1 << (dev + 16)) & ~SSB_PCICORE_SBTOPCI1_MASK);
  75. addr |= (func << 8);
  76. addr |= (off & ~3);
  77. } else {
  78. /* Type 1 transaction */
  79. pcicore_write32(pc, SSB_PCICORE_SBTOPCI1,
  80. SSB_PCICORE_SBTOPCI_CFG1);
  81. /* Calculate the address */
  82. addr = SSB_PCI_CFG;
  83. addr |= (bus << 16);
  84. addr |= (dev << 11);
  85. addr |= (func << 8);
  86. addr |= (off & ~3);
  87. }
  88. out:
  89. return addr;
  90. }
  91. static int ssb_extpci_read_config(struct ssb_pcicore *pc,
  92. unsigned int bus, unsigned int dev,
  93. unsigned int func, unsigned int off,
  94. void *buf, int len)
  95. {
  96. int err = -EINVAL;
  97. u32 addr, val;
  98. void __iomem *mmio;
  99. SSB_WARN_ON(!pc->hostmode);
  100. if (unlikely(len != 1 && len != 2 && len != 4))
  101. goto out;
  102. addr = get_cfgspace_addr(pc, bus, dev, func, off);
  103. if (unlikely(!addr))
  104. goto out;
  105. err = -ENOMEM;
  106. mmio = ioremap_nocache(addr, len);
  107. if (!mmio)
  108. goto out;
  109. if (mips_busprobe32(val, mmio)) {
  110. val = 0xffffffff;
  111. goto unmap;
  112. }
  113. val = readl(mmio);
  114. val >>= (8 * (off & 3));
  115. switch (len) {
  116. case 1:
  117. *((u8 *)buf) = (u8)val;
  118. break;
  119. case 2:
  120. *((u16 *)buf) = (u16)val;
  121. break;
  122. case 4:
  123. *((u32 *)buf) = (u32)val;
  124. break;
  125. }
  126. err = 0;
  127. unmap:
  128. iounmap(mmio);
  129. out:
  130. return err;
  131. }
  132. static int ssb_extpci_write_config(struct ssb_pcicore *pc,
  133. unsigned int bus, unsigned int dev,
  134. unsigned int func, unsigned int off,
  135. const void *buf, int len)
  136. {
  137. int err = -EINVAL;
  138. u32 addr, val = 0;
  139. void __iomem *mmio;
  140. SSB_WARN_ON(!pc->hostmode);
  141. if (unlikely(len != 1 && len != 2 && len != 4))
  142. goto out;
  143. addr = get_cfgspace_addr(pc, bus, dev, func, off);
  144. if (unlikely(!addr))
  145. goto out;
  146. err = -ENOMEM;
  147. mmio = ioremap_nocache(addr, len);
  148. if (!mmio)
  149. goto out;
  150. if (mips_busprobe32(val, mmio)) {
  151. val = 0xffffffff;
  152. goto unmap;
  153. }
  154. switch (len) {
  155. case 1:
  156. val = readl(mmio);
  157. val &= ~(0xFF << (8 * (off & 3)));
  158. val |= *((const u8 *)buf) << (8 * (off & 3));
  159. break;
  160. case 2:
  161. val = readl(mmio);
  162. val &= ~(0xFFFF << (8 * (off & 3)));
  163. val |= *((const u16 *)buf) << (8 * (off & 3));
  164. break;
  165. case 4:
  166. val = *((const u32 *)buf);
  167. break;
  168. }
  169. writel(val, mmio);
  170. err = 0;
  171. unmap:
  172. iounmap(mmio);
  173. out:
  174. return err;
  175. }
  176. static int ssb_pcicore_read_config(struct pci_bus *bus, unsigned int devfn,
  177. int reg, int size, u32 *val)
  178. {
  179. unsigned long flags;
  180. int err;
  181. spin_lock_irqsave(&cfgspace_lock, flags);
  182. err = ssb_extpci_read_config(extpci_core, bus->number, PCI_SLOT(devfn),
  183. PCI_FUNC(devfn), reg, val, size);
  184. spin_unlock_irqrestore(&cfgspace_lock, flags);
  185. return err ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
  186. }
  187. static int ssb_pcicore_write_config(struct pci_bus *bus, unsigned int devfn,
  188. int reg, int size, u32 val)
  189. {
  190. unsigned long flags;
  191. int err;
  192. spin_lock_irqsave(&cfgspace_lock, flags);
  193. err = ssb_extpci_write_config(extpci_core, bus->number, PCI_SLOT(devfn),
  194. PCI_FUNC(devfn), reg, &val, size);
  195. spin_unlock_irqrestore(&cfgspace_lock, flags);
  196. return err ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
  197. }
  198. static struct pci_ops ssb_pcicore_pciops = {
  199. .read = ssb_pcicore_read_config,
  200. .write = ssb_pcicore_write_config,
  201. };
  202. static struct resource ssb_pcicore_mem_resource = {
  203. .name = "SSB PCIcore external memory",
  204. .start = SSB_PCI_DMA,
  205. .end = SSB_PCI_DMA + SSB_PCI_DMA_SZ - 1,
  206. .flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED,
  207. };
  208. static struct resource ssb_pcicore_io_resource = {
  209. .name = "SSB PCIcore external I/O",
  210. .start = 0x100,
  211. .end = 0x7FF,
  212. .flags = IORESOURCE_IO | IORESOURCE_PCI_FIXED,
  213. };
  214. static struct pci_controller ssb_pcicore_controller = {
  215. .pci_ops = &ssb_pcicore_pciops,
  216. .io_resource = &ssb_pcicore_io_resource,
  217. .mem_resource = &ssb_pcicore_mem_resource,
  218. };
  219. /* This function is called when doing a pci_enable_device().
  220. * We must first check if the device is a device on the PCI-core bridge. */
  221. int ssb_pcicore_plat_dev_init(struct pci_dev *d)
  222. {
  223. if (d->bus->ops != &ssb_pcicore_pciops) {
  224. /* This is not a device on the PCI-core bridge. */
  225. return -ENODEV;
  226. }
  227. ssb_printk(KERN_INFO "PCI: Fixing up device %s\n",
  228. pci_name(d));
  229. /* Fix up interrupt lines */
  230. d->irq = ssb_mips_irq(extpci_core->dev) + 2;
  231. pci_write_config_byte(d, PCI_INTERRUPT_LINE, d->irq);
  232. return 0;
  233. }
  234. /* Early PCI fixup for a device on the PCI-core bridge. */
  235. static void ssb_pcicore_fixup_pcibridge(struct pci_dev *dev)
  236. {
  237. u8 lat;
  238. if (dev->bus->ops != &ssb_pcicore_pciops) {
  239. /* This is not a device on the PCI-core bridge. */
  240. return;
  241. }
  242. if (dev->bus->number != 0 || PCI_SLOT(dev->devfn) != 0)
  243. return;
  244. ssb_printk(KERN_INFO "PCI: Fixing up bridge %s\n", pci_name(dev));
  245. /* Enable PCI bridge bus mastering and memory space */
  246. pci_set_master(dev);
  247. if (pcibios_enable_device(dev, ~0) < 0) {
  248. ssb_printk(KERN_ERR "PCI: SSB bridge enable failed\n");
  249. return;
  250. }
  251. /* Enable PCI bridge BAR1 prefetch and burst */
  252. pci_write_config_dword(dev, SSB_BAR1_CONTROL, 3);
  253. /* Make sure our latency is high enough to handle the devices behind us */
  254. lat = 168;
  255. ssb_printk(KERN_INFO "PCI: Fixing latency timer of device %s to %u\n",
  256. pci_name(dev), lat);
  257. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  258. }
  259. DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, ssb_pcicore_fixup_pcibridge);
  260. /* PCI device IRQ mapping. */
  261. int ssb_pcicore_pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  262. {
  263. if (dev->bus->ops != &ssb_pcicore_pciops) {
  264. /* This is not a device on the PCI-core bridge. */
  265. return -ENODEV;
  266. }
  267. return ssb_mips_irq(extpci_core->dev) + 2;
  268. }
  269. static void ssb_pcicore_init_hostmode(struct ssb_pcicore *pc)
  270. {
  271. u32 val;
  272. if (WARN_ON(extpci_core))
  273. return;
  274. extpci_core = pc;
  275. ssb_dprintk(KERN_INFO PFX "PCIcore in host mode found\n");
  276. /* Reset devices on the external PCI bus */
  277. val = SSB_PCICORE_CTL_RST_OE;
  278. val |= SSB_PCICORE_CTL_CLK_OE;
  279. pcicore_write32(pc, SSB_PCICORE_CTL, val);
  280. val |= SSB_PCICORE_CTL_CLK; /* Clock on */
  281. pcicore_write32(pc, SSB_PCICORE_CTL, val);
  282. udelay(150); /* Assertion time demanded by the PCI standard */
  283. val |= SSB_PCICORE_CTL_RST; /* Deassert RST# */
  284. pcicore_write32(pc, SSB_PCICORE_CTL, val);
  285. val = SSB_PCICORE_ARBCTL_INTERN;
  286. pcicore_write32(pc, SSB_PCICORE_ARBCTL, val);
  287. udelay(1); /* Assertion time demanded by the PCI standard */
  288. if (pc->dev->bus->has_cardbus_slot) {
  289. ssb_dprintk(KERN_INFO PFX "CardBus slot detected\n");
  290. pc->cardbusmode = 1;
  291. /* GPIO 1 resets the bridge */
  292. ssb_gpio_out(pc->dev->bus, 1, 1);
  293. ssb_gpio_outen(pc->dev->bus, 1, 1);
  294. pcicore_write16(pc, SSB_PCICORE_SPROM(0),
  295. pcicore_read16(pc, SSB_PCICORE_SPROM(0))
  296. | 0x0400);
  297. }
  298. /* 64MB I/O window */
  299. pcicore_write32(pc, SSB_PCICORE_SBTOPCI0,
  300. SSB_PCICORE_SBTOPCI_IO);
  301. /* 64MB config space */
  302. pcicore_write32(pc, SSB_PCICORE_SBTOPCI1,
  303. SSB_PCICORE_SBTOPCI_CFG0);
  304. /* 1GB memory window */
  305. pcicore_write32(pc, SSB_PCICORE_SBTOPCI2,
  306. SSB_PCICORE_SBTOPCI_MEM | SSB_PCI_DMA);
  307. /* Enable PCI bridge BAR0 prefetch and burst */
  308. val = PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
  309. ssb_extpci_write_config(pc, 0, 0, 0, PCI_COMMAND, &val, 2);
  310. /* Clear error conditions */
  311. val = 0;
  312. ssb_extpci_write_config(pc, 0, 0, 0, PCI_STATUS, &val, 2);
  313. /* Enable PCI interrupts */
  314. pcicore_write32(pc, SSB_PCICORE_IMASK,
  315. SSB_PCICORE_IMASK_INTA);
  316. /* Ok, ready to run, register it to the system.
  317. * The following needs change, if we want to port hostmode
  318. * to non-MIPS platform. */
  319. ssb_pcicore_controller.io_map_base = (unsigned long)ioremap_nocache(SSB_PCI_MEM, 0x04000000);
  320. set_io_port_base(ssb_pcicore_controller.io_map_base);
  321. /* Give some time to the PCI controller to configure itself with the new
  322. * values. Not waiting at this point causes crashes of the machine. */
  323. mdelay(10);
  324. register_pci_controller(&ssb_pcicore_controller);
  325. }
  326. static int pcicore_is_in_hostmode(struct ssb_pcicore *pc)
  327. {
  328. struct ssb_bus *bus = pc->dev->bus;
  329. u16 chipid_top;
  330. u32 tmp;
  331. chipid_top = (bus->chip_id & 0xFF00);
  332. if (chipid_top != 0x4700 &&
  333. chipid_top != 0x5300)
  334. return 0;
  335. if (bus->sprom.boardflags_lo & SSB_PCICORE_BFL_NOPCI)
  336. return 0;
  337. /* The 200-pin BCM4712 package does not bond out PCI. Even when
  338. * PCI is bonded out, some boards may leave the pins floating. */
  339. if (bus->chip_id == 0x4712) {
  340. if (bus->chip_package == SSB_CHIPPACK_BCM4712S)
  341. return 0;
  342. if (bus->chip_package == SSB_CHIPPACK_BCM4712M)
  343. return 0;
  344. }
  345. if (bus->chip_id == 0x5350)
  346. return 0;
  347. return !mips_busprobe32(tmp, (bus->mmio + (pc->dev->core_index * SSB_CORE_SIZE)));
  348. }
  349. #endif /* CONFIG_SSB_PCICORE_HOSTMODE */
  350. /**************************************************
  351. * Workarounds.
  352. **************************************************/
  353. static void ssb_pcicore_fix_sprom_core_index(struct ssb_pcicore *pc)
  354. {
  355. u16 tmp = pcicore_read16(pc, SSB_PCICORE_SPROM(0));
  356. if (((tmp & 0xF000) >> 12) != pc->dev->core_index) {
  357. tmp &= ~0xF000;
  358. tmp |= (pc->dev->core_index << 12);
  359. pcicore_write16(pc, SSB_PCICORE_SPROM(0), tmp);
  360. }
  361. }
  362. static u8 ssb_pcicore_polarity_workaround(struct ssb_pcicore *pc)
  363. {
  364. return (ssb_pcie_read(pc, 0x204) & 0x10) ? 0xC0 : 0x80;
  365. }
  366. static void ssb_pcicore_serdes_workaround(struct ssb_pcicore *pc)
  367. {
  368. const u8 serdes_pll_device = 0x1D;
  369. const u8 serdes_rx_device = 0x1F;
  370. u16 tmp;
  371. ssb_pcie_mdio_write(pc, serdes_rx_device, 1 /* Control */,
  372. ssb_pcicore_polarity_workaround(pc));
  373. tmp = ssb_pcie_mdio_read(pc, serdes_pll_device, 1 /* Control */);
  374. if (tmp & 0x4000)
  375. ssb_pcie_mdio_write(pc, serdes_pll_device, 1, tmp & ~0x4000);
  376. }
  377. static void ssb_pcicore_pci_setup_workarounds(struct ssb_pcicore *pc)
  378. {
  379. struct ssb_device *pdev = pc->dev;
  380. struct ssb_bus *bus = pdev->bus;
  381. u32 tmp;
  382. tmp = pcicore_read32(pc, SSB_PCICORE_SBTOPCI2);
  383. tmp |= SSB_PCICORE_SBTOPCI_PREF;
  384. tmp |= SSB_PCICORE_SBTOPCI_BURST;
  385. pcicore_write32(pc, SSB_PCICORE_SBTOPCI2, tmp);
  386. if (pdev->id.revision < 5) {
  387. tmp = ssb_read32(pdev, SSB_IMCFGLO);
  388. tmp &= ~SSB_IMCFGLO_SERTO;
  389. tmp |= 2;
  390. tmp &= ~SSB_IMCFGLO_REQTO;
  391. tmp |= 3 << SSB_IMCFGLO_REQTO_SHIFT;
  392. ssb_write32(pdev, SSB_IMCFGLO, tmp);
  393. ssb_commit_settings(bus);
  394. } else if (pdev->id.revision >= 11) {
  395. tmp = pcicore_read32(pc, SSB_PCICORE_SBTOPCI2);
  396. tmp |= SSB_PCICORE_SBTOPCI_MRM;
  397. pcicore_write32(pc, SSB_PCICORE_SBTOPCI2, tmp);
  398. }
  399. }
  400. static void ssb_pcicore_pcie_setup_workarounds(struct ssb_pcicore *pc)
  401. {
  402. u32 tmp;
  403. u8 rev = pc->dev->id.revision;
  404. if (rev == 0 || rev == 1) {
  405. /* TLP Workaround register. */
  406. tmp = ssb_pcie_read(pc, 0x4);
  407. tmp |= 0x8;
  408. ssb_pcie_write(pc, 0x4, tmp);
  409. }
  410. if (rev == 1) {
  411. /* DLLP Link Control register. */
  412. tmp = ssb_pcie_read(pc, 0x100);
  413. tmp |= 0x40;
  414. ssb_pcie_write(pc, 0x100, tmp);
  415. }
  416. if (rev == 0) {
  417. const u8 serdes_rx_device = 0x1F;
  418. ssb_pcie_mdio_write(pc, serdes_rx_device,
  419. 2 /* Timer */, 0x8128);
  420. ssb_pcie_mdio_write(pc, serdes_rx_device,
  421. 6 /* CDR */, 0x0100);
  422. ssb_pcie_mdio_write(pc, serdes_rx_device,
  423. 7 /* CDR BW */, 0x1466);
  424. } else if (rev == 3 || rev == 4 || rev == 5) {
  425. /* TODO: DLLP Power Management Threshold */
  426. ssb_pcicore_serdes_workaround(pc);
  427. /* TODO: ASPM */
  428. } else if (rev == 7) {
  429. /* TODO: No PLL down */
  430. }
  431. if (rev >= 6) {
  432. /* Miscellaneous Configuration Fixup */
  433. tmp = pcicore_read16(pc, SSB_PCICORE_SPROM(5));
  434. if (!(tmp & 0x8000))
  435. pcicore_write16(pc, SSB_PCICORE_SPROM(5),
  436. tmp | 0x8000);
  437. }
  438. }
  439. /**************************************************
  440. * Generic and Clientmode operation code.
  441. **************************************************/
  442. static void ssb_pcicore_init_clientmode(struct ssb_pcicore *pc)
  443. {
  444. struct ssb_device *pdev = pc->dev;
  445. struct ssb_bus *bus = pdev->bus;
  446. if (bus->bustype == SSB_BUSTYPE_PCI)
  447. ssb_pcicore_fix_sprom_core_index(pc);
  448. /* Disable PCI interrupts. */
  449. ssb_write32(pdev, SSB_INTVEC, 0);
  450. /* Additional PCIe always once-executed workarounds */
  451. if (pc->dev->id.coreid == SSB_DEV_PCIE) {
  452. ssb_pcicore_serdes_workaround(pc);
  453. /* TODO: ASPM */
  454. /* TODO: Clock Request Update */
  455. }
  456. }
  457. void ssb_pcicore_init(struct ssb_pcicore *pc)
  458. {
  459. struct ssb_device *dev = pc->dev;
  460. if (!dev)
  461. return;
  462. if (!ssb_device_is_enabled(dev))
  463. ssb_device_enable(dev, 0);
  464. #ifdef CONFIG_SSB_PCICORE_HOSTMODE
  465. pc->hostmode = pcicore_is_in_hostmode(pc);
  466. if (pc->hostmode)
  467. ssb_pcicore_init_hostmode(pc);
  468. #endif /* CONFIG_SSB_PCICORE_HOSTMODE */
  469. if (!pc->hostmode)
  470. ssb_pcicore_init_clientmode(pc);
  471. }
  472. static u32 ssb_pcie_read(struct ssb_pcicore *pc, u32 address)
  473. {
  474. pcicore_write32(pc, 0x130, address);
  475. return pcicore_read32(pc, 0x134);
  476. }
  477. static void ssb_pcie_write(struct ssb_pcicore *pc, u32 address, u32 data)
  478. {
  479. pcicore_write32(pc, 0x130, address);
  480. pcicore_write32(pc, 0x134, data);
  481. }
  482. static void ssb_pcie_mdio_set_phy(struct ssb_pcicore *pc, u8 phy)
  483. {
  484. const u16 mdio_control = 0x128;
  485. const u16 mdio_data = 0x12C;
  486. u32 v;
  487. int i;
  488. v = (1 << 30); /* Start of Transaction */
  489. v |= (1 << 28); /* Write Transaction */
  490. v |= (1 << 17); /* Turnaround */
  491. v |= (0x1F << 18);
  492. v |= (phy << 4);
  493. pcicore_write32(pc, mdio_data, v);
  494. udelay(10);
  495. for (i = 0; i < 200; i++) {
  496. v = pcicore_read32(pc, mdio_control);
  497. if (v & 0x100 /* Trans complete */)
  498. break;
  499. msleep(1);
  500. }
  501. }
  502. static u16 ssb_pcie_mdio_read(struct ssb_pcicore *pc, u8 device, u8 address)
  503. {
  504. const u16 mdio_control = 0x128;
  505. const u16 mdio_data = 0x12C;
  506. int max_retries = 10;
  507. u16 ret = 0;
  508. u32 v;
  509. int i;
  510. v = 0x80; /* Enable Preamble Sequence */
  511. v |= 0x2; /* MDIO Clock Divisor */
  512. pcicore_write32(pc, mdio_control, v);
  513. if (pc->dev->id.revision >= 10) {
  514. max_retries = 200;
  515. ssb_pcie_mdio_set_phy(pc, device);
  516. }
  517. v = (1 << 30); /* Start of Transaction */
  518. v |= (1 << 29); /* Read Transaction */
  519. v |= (1 << 17); /* Turnaround */
  520. if (pc->dev->id.revision < 10)
  521. v |= (u32)device << 22;
  522. v |= (u32)address << 18;
  523. pcicore_write32(pc, mdio_data, v);
  524. /* Wait for the device to complete the transaction */
  525. udelay(10);
  526. for (i = 0; i < max_retries; i++) {
  527. v = pcicore_read32(pc, mdio_control);
  528. if (v & 0x100 /* Trans complete */) {
  529. udelay(10);
  530. ret = pcicore_read32(pc, mdio_data);
  531. break;
  532. }
  533. msleep(1);
  534. }
  535. pcicore_write32(pc, mdio_control, 0);
  536. return ret;
  537. }
  538. static void ssb_pcie_mdio_write(struct ssb_pcicore *pc, u8 device,
  539. u8 address, u16 data)
  540. {
  541. const u16 mdio_control = 0x128;
  542. const u16 mdio_data = 0x12C;
  543. int max_retries = 10;
  544. u32 v;
  545. int i;
  546. v = 0x80; /* Enable Preamble Sequence */
  547. v |= 0x2; /* MDIO Clock Divisor */
  548. pcicore_write32(pc, mdio_control, v);
  549. if (pc->dev->id.revision >= 10) {
  550. max_retries = 200;
  551. ssb_pcie_mdio_set_phy(pc, device);
  552. }
  553. v = (1 << 30); /* Start of Transaction */
  554. v |= (1 << 28); /* Write Transaction */
  555. v |= (1 << 17); /* Turnaround */
  556. if (pc->dev->id.revision < 10)
  557. v |= (u32)device << 22;
  558. v |= (u32)address << 18;
  559. v |= data;
  560. pcicore_write32(pc, mdio_data, v);
  561. /* Wait for the device to complete the transaction */
  562. udelay(10);
  563. for (i = 0; i < max_retries; i++) {
  564. v = pcicore_read32(pc, mdio_control);
  565. if (v & 0x100 /* Trans complete */)
  566. break;
  567. msleep(1);
  568. }
  569. pcicore_write32(pc, mdio_control, 0);
  570. }
  571. int ssb_pcicore_dev_irqvecs_enable(struct ssb_pcicore *pc,
  572. struct ssb_device *dev)
  573. {
  574. struct ssb_device *pdev = pc->dev;
  575. struct ssb_bus *bus;
  576. int err = 0;
  577. u32 tmp;
  578. if (dev->bus->bustype != SSB_BUSTYPE_PCI) {
  579. /* This SSB device is not on a PCI host-bus. So the IRQs are
  580. * not routed through the PCI core.
  581. * So we must not enable routing through the PCI core. */
  582. goto out;
  583. }
  584. if (!pdev)
  585. goto out;
  586. bus = pdev->bus;
  587. might_sleep_if(pdev->id.coreid != SSB_DEV_PCI);
  588. /* Enable interrupts for this device. */
  589. if ((pdev->id.revision >= 6) || (pdev->id.coreid == SSB_DEV_PCIE)) {
  590. u32 coremask;
  591. /* Calculate the "coremask" for the device. */
  592. coremask = (1 << dev->core_index);
  593. SSB_WARN_ON(bus->bustype != SSB_BUSTYPE_PCI);
  594. err = pci_read_config_dword(bus->host_pci, SSB_PCI_IRQMASK, &tmp);
  595. if (err)
  596. goto out;
  597. tmp |= coremask << 8;
  598. err = pci_write_config_dword(bus->host_pci, SSB_PCI_IRQMASK, tmp);
  599. if (err)
  600. goto out;
  601. } else {
  602. u32 intvec;
  603. intvec = ssb_read32(pdev, SSB_INTVEC);
  604. tmp = ssb_read32(dev, SSB_TPSFLAG);
  605. tmp &= SSB_TPSFLAG_BPFLAG;
  606. intvec |= (1 << tmp);
  607. ssb_write32(pdev, SSB_INTVEC, intvec);
  608. }
  609. /* Setup PCIcore operation. */
  610. if (pc->setup_done)
  611. goto out;
  612. if (pdev->id.coreid == SSB_DEV_PCI) {
  613. ssb_pcicore_pci_setup_workarounds(pc);
  614. } else {
  615. WARN_ON(pdev->id.coreid != SSB_DEV_PCIE);
  616. ssb_pcicore_pcie_setup_workarounds(pc);
  617. }
  618. pc->setup_done = 1;
  619. out:
  620. return err;
  621. }
  622. EXPORT_SYMBOL(ssb_pcicore_dev_irqvecs_enable);