driver_pcicore.c 19 KB

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