driver_pcicore.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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 void ssb_commit_settings(struct ssb_bus *bus);
  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_printk(KERN_INFO "PCI: Fixing up device %s\n",
  229. pci_name(d));
  230. /* Fix up interrupt lines */
  231. d->irq = ssb_mips_irq(extpci_core->dev) + 2;
  232. pci_write_config_byte(d, PCI_INTERRUPT_LINE, d->irq);
  233. return 0;
  234. }
  235. /* Early PCI fixup for a device on the PCI-core bridge. */
  236. static void ssb_pcicore_fixup_pcibridge(struct pci_dev *dev)
  237. {
  238. u8 lat;
  239. if (dev->bus->ops != &ssb_pcicore_pciops) {
  240. /* This is not a device on the PCI-core bridge. */
  241. return;
  242. }
  243. if (dev->bus->number != 0 || PCI_SLOT(dev->devfn) != 0)
  244. return;
  245. ssb_printk(KERN_INFO "PCI: Fixing up bridge %s\n", pci_name(dev));
  246. /* Enable PCI bridge bus mastering and memory space */
  247. pci_set_master(dev);
  248. if (pcibios_enable_device(dev, ~0) < 0) {
  249. ssb_printk(KERN_ERR "PCI: SSB bridge enable failed\n");
  250. return;
  251. }
  252. /* Enable PCI bridge BAR1 prefetch and burst */
  253. pci_write_config_dword(dev, SSB_BAR1_CONTROL, 3);
  254. /* Make sure our latency is high enough to handle the devices behind us */
  255. lat = 168;
  256. ssb_printk(KERN_INFO "PCI: Fixing latency timer of device %s to %u\n",
  257. pci_name(dev), lat);
  258. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  259. }
  260. DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, ssb_pcicore_fixup_pcibridge);
  261. /* PCI device IRQ mapping. */
  262. int ssb_pcicore_pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  263. {
  264. if (dev->bus->ops != &ssb_pcicore_pciops) {
  265. /* This is not a device on the PCI-core bridge. */
  266. return -ENODEV;
  267. }
  268. return ssb_mips_irq(extpci_core->dev) + 2;
  269. }
  270. static void ssb_pcicore_init_hostmode(struct ssb_pcicore *pc)
  271. {
  272. u32 val;
  273. if (WARN_ON(extpci_core))
  274. return;
  275. extpci_core = pc;
  276. ssb_dprintk(KERN_INFO PFX "PCIcore in host mode found\n");
  277. /* Reset devices on the external PCI bus */
  278. val = SSB_PCICORE_CTL_RST_OE;
  279. val |= SSB_PCICORE_CTL_CLK_OE;
  280. pcicore_write32(pc, SSB_PCICORE_CTL, val);
  281. val |= SSB_PCICORE_CTL_CLK; /* Clock on */
  282. pcicore_write32(pc, SSB_PCICORE_CTL, val);
  283. udelay(150); /* Assertion time demanded by the PCI standard */
  284. val |= SSB_PCICORE_CTL_RST; /* Deassert RST# */
  285. pcicore_write32(pc, SSB_PCICORE_CTL, val);
  286. val = SSB_PCICORE_ARBCTL_INTERN;
  287. pcicore_write32(pc, SSB_PCICORE_ARBCTL, val);
  288. udelay(1); /* Assertion time demanded by the PCI standard */
  289. if (pc->dev->bus->has_cardbus_slot) {
  290. ssb_dprintk(KERN_INFO PFX "CardBus slot detected\n");
  291. pc->cardbusmode = 1;
  292. /* GPIO 1 resets the bridge */
  293. ssb_gpio_out(pc->dev->bus, 1, 1);
  294. ssb_gpio_outen(pc->dev->bus, 1, 1);
  295. pcicore_write16(pc, SSB_PCICORE_SPROM(0),
  296. pcicore_read16(pc, SSB_PCICORE_SPROM(0))
  297. | 0x0400);
  298. }
  299. /* 64MB I/O window */
  300. pcicore_write32(pc, SSB_PCICORE_SBTOPCI0,
  301. SSB_PCICORE_SBTOPCI_IO);
  302. /* 64MB config space */
  303. pcicore_write32(pc, SSB_PCICORE_SBTOPCI1,
  304. SSB_PCICORE_SBTOPCI_CFG0);
  305. /* 1GB memory window */
  306. pcicore_write32(pc, SSB_PCICORE_SBTOPCI2,
  307. SSB_PCICORE_SBTOPCI_MEM | SSB_PCI_DMA);
  308. /* Enable PCI bridge BAR0 prefetch and burst */
  309. val = PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
  310. ssb_extpci_write_config(pc, 0, 0, 0, PCI_COMMAND, &val, 2);
  311. /* Clear error conditions */
  312. val = 0;
  313. ssb_extpci_write_config(pc, 0, 0, 0, PCI_STATUS, &val, 2);
  314. /* Enable PCI interrupts */
  315. pcicore_write32(pc, SSB_PCICORE_IMASK,
  316. SSB_PCICORE_IMASK_INTA);
  317. /* Ok, ready to run, register it to the system.
  318. * The following needs change, if we want to port hostmode
  319. * to non-MIPS platform. */
  320. ssb_pcicore_controller.io_map_base = (unsigned long)ioremap_nocache(SSB_PCI_MEM, 0x04000000);
  321. set_io_port_base(ssb_pcicore_controller.io_map_base);
  322. /* Give some time to the PCI controller to configure itself with the new
  323. * values. Not waiting at this point causes crashes of the machine. */
  324. mdelay(10);
  325. register_pci_controller(&ssb_pcicore_controller);
  326. }
  327. static int pcicore_is_in_hostmode(struct ssb_pcicore *pc)
  328. {
  329. struct ssb_bus *bus = pc->dev->bus;
  330. u16 chipid_top;
  331. u32 tmp;
  332. chipid_top = (bus->chip_id & 0xFF00);
  333. if (chipid_top != 0x4700 &&
  334. chipid_top != 0x5300)
  335. return 0;
  336. if (bus->sprom.boardflags_lo & SSB_PCICORE_BFL_NOPCI)
  337. return 0;
  338. /* The 200-pin BCM4712 package does not bond out PCI. Even when
  339. * PCI is bonded out, some boards may leave the pins floating. */
  340. if (bus->chip_id == 0x4712) {
  341. if (bus->chip_package == SSB_CHIPPACK_BCM4712S)
  342. return 0;
  343. if (bus->chip_package == SSB_CHIPPACK_BCM4712M)
  344. return 0;
  345. }
  346. if (bus->chip_id == 0x5350)
  347. return 0;
  348. return !mips_busprobe32(tmp, (bus->mmio + (pc->dev->core_index * SSB_CORE_SIZE)));
  349. }
  350. #endif /* CONFIG_SSB_PCICORE_HOSTMODE */
  351. /**************************************************
  352. * Workarounds.
  353. **************************************************/
  354. static void ssb_pcicore_fix_sprom_core_index(struct ssb_pcicore *pc)
  355. {
  356. u16 tmp = pcicore_read16(pc, SSB_PCICORE_SPROM(0));
  357. if (((tmp & 0xF000) >> 12) != pc->dev->core_index) {
  358. tmp &= ~0xF000;
  359. tmp |= (pc->dev->core_index << 12);
  360. pcicore_write16(pc, SSB_PCICORE_SPROM(0), tmp);
  361. }
  362. }
  363. static u8 ssb_pcicore_polarity_workaround(struct ssb_pcicore *pc)
  364. {
  365. return (ssb_pcie_read(pc, 0x204) & 0x10) ? 0xC0 : 0x80;
  366. }
  367. static void ssb_pcicore_serdes_workaround(struct ssb_pcicore *pc)
  368. {
  369. const u8 serdes_pll_device = 0x1D;
  370. const u8 serdes_rx_device = 0x1F;
  371. u16 tmp;
  372. ssb_pcie_mdio_write(pc, serdes_rx_device, 1 /* Control */,
  373. ssb_pcicore_polarity_workaround(pc));
  374. tmp = ssb_pcie_mdio_read(pc, serdes_pll_device, 1 /* Control */);
  375. if (tmp & 0x4000)
  376. ssb_pcie_mdio_write(pc, serdes_pll_device, 1, tmp & ~0x4000);
  377. }
  378. static void ssb_pcicore_pci_setup_workarounds(struct ssb_pcicore *pc)
  379. {
  380. struct ssb_device *pdev = pc->dev;
  381. struct ssb_bus *bus = pdev->bus;
  382. u32 tmp;
  383. tmp = pcicore_read32(pc, SSB_PCICORE_SBTOPCI2);
  384. tmp |= SSB_PCICORE_SBTOPCI_PREF;
  385. tmp |= SSB_PCICORE_SBTOPCI_BURST;
  386. pcicore_write32(pc, SSB_PCICORE_SBTOPCI2, tmp);
  387. if (pdev->id.revision < 5) {
  388. tmp = ssb_read32(pdev, SSB_IMCFGLO);
  389. tmp &= ~SSB_IMCFGLO_SERTO;
  390. tmp |= 2;
  391. tmp &= ~SSB_IMCFGLO_REQTO;
  392. tmp |= 3 << SSB_IMCFGLO_REQTO_SHIFT;
  393. ssb_write32(pdev, SSB_IMCFGLO, tmp);
  394. ssb_commit_settings(bus);
  395. } else if (pdev->id.revision >= 11) {
  396. tmp = pcicore_read32(pc, SSB_PCICORE_SBTOPCI2);
  397. tmp |= SSB_PCICORE_SBTOPCI_MRM;
  398. pcicore_write32(pc, SSB_PCICORE_SBTOPCI2, tmp);
  399. }
  400. }
  401. static void ssb_pcicore_pcie_setup_workarounds(struct ssb_pcicore *pc)
  402. {
  403. u32 tmp;
  404. u8 rev = pc->dev->id.revision;
  405. if (rev == 0 || rev == 1) {
  406. /* TLP Workaround register. */
  407. tmp = ssb_pcie_read(pc, 0x4);
  408. tmp |= 0x8;
  409. ssb_pcie_write(pc, 0x4, tmp);
  410. }
  411. if (rev == 1) {
  412. /* DLLP Link Control register. */
  413. tmp = ssb_pcie_read(pc, 0x100);
  414. tmp |= 0x40;
  415. ssb_pcie_write(pc, 0x100, tmp);
  416. }
  417. if (rev == 0) {
  418. const u8 serdes_rx_device = 0x1F;
  419. ssb_pcie_mdio_write(pc, serdes_rx_device,
  420. 2 /* Timer */, 0x8128);
  421. ssb_pcie_mdio_write(pc, serdes_rx_device,
  422. 6 /* CDR */, 0x0100);
  423. ssb_pcie_mdio_write(pc, serdes_rx_device,
  424. 7 /* CDR BW */, 0x1466);
  425. } else if (rev == 3 || rev == 4 || rev == 5) {
  426. /* TODO: DLLP Power Management Threshold */
  427. ssb_pcicore_serdes_workaround(pc);
  428. /* TODO: ASPM */
  429. } else if (rev == 7) {
  430. /* TODO: No PLL down */
  431. }
  432. if (rev >= 6) {
  433. /* Miscellaneous Configuration Fixup */
  434. tmp = pcicore_read16(pc, SSB_PCICORE_SPROM(5));
  435. if (!(tmp & 0x8000))
  436. pcicore_write16(pc, SSB_PCICORE_SPROM(5),
  437. tmp | 0x8000);
  438. }
  439. }
  440. /**************************************************
  441. * Generic and Clientmode operation code.
  442. **************************************************/
  443. static void ssb_pcicore_init_clientmode(struct ssb_pcicore *pc)
  444. {
  445. /* Disable PCI interrupts. */
  446. ssb_write32(pc->dev, SSB_INTVEC, 0);
  447. }
  448. void ssb_pcicore_init(struct ssb_pcicore *pc)
  449. {
  450. struct ssb_device *dev = pc->dev;
  451. if (!dev)
  452. return;
  453. if (!ssb_device_is_enabled(dev))
  454. ssb_device_enable(dev, 0);
  455. ssb_pcicore_fix_sprom_core_index(pc);
  456. #ifdef CONFIG_SSB_PCICORE_HOSTMODE
  457. pc->hostmode = pcicore_is_in_hostmode(pc);
  458. if (pc->hostmode)
  459. ssb_pcicore_init_hostmode(pc);
  460. #endif /* CONFIG_SSB_PCICORE_HOSTMODE */
  461. if (!pc->hostmode)
  462. ssb_pcicore_init_clientmode(pc);
  463. /* Additional always once-executed workarounds */
  464. ssb_pcicore_serdes_workaround(pc);
  465. /* TODO: ASPM */
  466. /* TODO: Clock Request Update */
  467. }
  468. static u32 ssb_pcie_read(struct ssb_pcicore *pc, u32 address)
  469. {
  470. pcicore_write32(pc, 0x130, address);
  471. return pcicore_read32(pc, 0x134);
  472. }
  473. static void ssb_pcie_write(struct ssb_pcicore *pc, u32 address, u32 data)
  474. {
  475. pcicore_write32(pc, 0x130, address);
  476. pcicore_write32(pc, 0x134, data);
  477. }
  478. static void ssb_pcie_mdio_set_phy(struct ssb_pcicore *pc, u8 phy)
  479. {
  480. const u16 mdio_control = 0x128;
  481. const u16 mdio_data = 0x12C;
  482. u32 v;
  483. int i;
  484. v = (1 << 30); /* Start of Transaction */
  485. v |= (1 << 28); /* Write Transaction */
  486. v |= (1 << 17); /* Turnaround */
  487. v |= (0x1F << 18);
  488. v |= (phy << 4);
  489. pcicore_write32(pc, mdio_data, v);
  490. udelay(10);
  491. for (i = 0; i < 200; i++) {
  492. v = pcicore_read32(pc, mdio_control);
  493. if (v & 0x100 /* Trans complete */)
  494. break;
  495. msleep(1);
  496. }
  497. }
  498. static u16 ssb_pcie_mdio_read(struct ssb_pcicore *pc, u8 device, u8 address)
  499. {
  500. const u16 mdio_control = 0x128;
  501. const u16 mdio_data = 0x12C;
  502. int max_retries = 10;
  503. u16 ret = 0;
  504. u32 v;
  505. int i;
  506. v = 0x80; /* Enable Preamble Sequence */
  507. v |= 0x2; /* MDIO Clock Divisor */
  508. pcicore_write32(pc, mdio_control, v);
  509. if (pc->dev->id.revision >= 10) {
  510. max_retries = 200;
  511. ssb_pcie_mdio_set_phy(pc, device);
  512. }
  513. v = (1 << 30); /* Start of Transaction */
  514. v |= (1 << 29); /* Read Transaction */
  515. v |= (1 << 17); /* Turnaround */
  516. if (pc->dev->id.revision < 10)
  517. v |= (u32)device << 22;
  518. v |= (u32)address << 18;
  519. pcicore_write32(pc, mdio_data, v);
  520. /* Wait for the device to complete the transaction */
  521. udelay(10);
  522. for (i = 0; i < max_retries; i++) {
  523. v = pcicore_read32(pc, mdio_control);
  524. if (v & 0x100 /* Trans complete */) {
  525. udelay(10);
  526. ret = pcicore_read32(pc, mdio_data);
  527. break;
  528. }
  529. msleep(1);
  530. }
  531. pcicore_write32(pc, mdio_control, 0);
  532. return ret;
  533. }
  534. static void ssb_pcie_mdio_write(struct ssb_pcicore *pc, u8 device,
  535. u8 address, u16 data)
  536. {
  537. const u16 mdio_control = 0x128;
  538. const u16 mdio_data = 0x12C;
  539. int max_retries = 10;
  540. u32 v;
  541. int i;
  542. v = 0x80; /* Enable Preamble Sequence */
  543. v |= 0x2; /* MDIO Clock Divisor */
  544. pcicore_write32(pc, mdio_control, v);
  545. if (pc->dev->id.revision >= 10) {
  546. max_retries = 200;
  547. ssb_pcie_mdio_set_phy(pc, device);
  548. }
  549. v = (1 << 30); /* Start of Transaction */
  550. v |= (1 << 28); /* Write Transaction */
  551. v |= (1 << 17); /* Turnaround */
  552. if (pc->dev->id.revision < 10)
  553. v |= (u32)device << 22;
  554. v |= (u32)address << 18;
  555. v |= data;
  556. pcicore_write32(pc, mdio_data, v);
  557. /* Wait for the device to complete the transaction */
  558. udelay(10);
  559. for (i = 0; i < max_retries; i++) {
  560. v = pcicore_read32(pc, mdio_control);
  561. if (v & 0x100 /* Trans complete */)
  562. break;
  563. msleep(1);
  564. }
  565. pcicore_write32(pc, mdio_control, 0);
  566. }
  567. static void ssb_broadcast_value(struct ssb_device *dev,
  568. u32 address, u32 data)
  569. {
  570. /* This is used for both, PCI and ChipCommon core, so be careful. */
  571. BUILD_BUG_ON(SSB_PCICORE_BCAST_ADDR != SSB_CHIPCO_BCAST_ADDR);
  572. BUILD_BUG_ON(SSB_PCICORE_BCAST_DATA != SSB_CHIPCO_BCAST_DATA);
  573. ssb_write32(dev, SSB_PCICORE_BCAST_ADDR, address);
  574. ssb_read32(dev, SSB_PCICORE_BCAST_ADDR); /* flush */
  575. ssb_write32(dev, SSB_PCICORE_BCAST_DATA, data);
  576. ssb_read32(dev, SSB_PCICORE_BCAST_DATA); /* flush */
  577. }
  578. static void ssb_commit_settings(struct ssb_bus *bus)
  579. {
  580. struct ssb_device *dev;
  581. dev = bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev;
  582. if (WARN_ON(!dev))
  583. return;
  584. /* This forces an update of the cached registers. */
  585. ssb_broadcast_value(dev, 0xFD8, 0);
  586. }
  587. int ssb_pcicore_dev_irqvecs_enable(struct ssb_pcicore *pc,
  588. struct ssb_device *dev)
  589. {
  590. struct ssb_device *pdev = pc->dev;
  591. struct ssb_bus *bus;
  592. int err = 0;
  593. u32 tmp;
  594. if (dev->bus->bustype != SSB_BUSTYPE_PCI) {
  595. /* This SSB device is not on a PCI host-bus. So the IRQs are
  596. * not routed through the PCI core.
  597. * So we must not enable routing through the PCI core. */
  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. SSB_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);