driver_pcicore.c 19 KB

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