driver_pcicore.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 "ssb_private.h"
  14. static inline
  15. u32 pcicore_read32(struct ssb_pcicore *pc, u16 offset)
  16. {
  17. return ssb_read32(pc->dev, offset);
  18. }
  19. static inline
  20. void pcicore_write32(struct ssb_pcicore *pc, u16 offset, u32 value)
  21. {
  22. ssb_write32(pc->dev, offset, value);
  23. }
  24. /**************************************************
  25. * Code for hostmode operation.
  26. **************************************************/
  27. #ifdef CONFIG_SSB_PCICORE_HOSTMODE
  28. #include <asm/paccess.h>
  29. /* Probe a 32bit value on the bus and catch bus exceptions.
  30. * Returns nonzero on a bus exception.
  31. * This is MIPS specific */
  32. #define mips_busprobe32(val, addr) get_dbe((val), ((u32 *)(addr)))
  33. /* Assume one-hot slot wiring */
  34. #define SSB_PCI_SLOT_MAX 16
  35. /* Global lock is OK, as we won't have more than one extpci anyway. */
  36. static DEFINE_SPINLOCK(cfgspace_lock);
  37. /* Core to access the external PCI config space. Can only have one. */
  38. static struct ssb_pcicore *extpci_core;
  39. static u32 ssb_pcicore_pcibus_iobase = 0x100;
  40. static u32 ssb_pcicore_pcibus_membase = SSB_PCI_DMA;
  41. int pcibios_plat_dev_init(struct pci_dev *d)
  42. {
  43. struct resource *res;
  44. int pos, size;
  45. u32 *base;
  46. ssb_printk(KERN_INFO "PCI: Fixing up device %s\n",
  47. pci_name(d));
  48. /* Fix up resource bases */
  49. for (pos = 0; pos < 6; pos++) {
  50. res = &d->resource[pos];
  51. if (res->flags & IORESOURCE_IO)
  52. base = &ssb_pcicore_pcibus_iobase;
  53. else
  54. base = &ssb_pcicore_pcibus_membase;
  55. if (res->end) {
  56. size = res->end - res->start + 1;
  57. if (*base & (size - 1))
  58. *base = (*base + size) & ~(size - 1);
  59. res->start = *base;
  60. res->end = res->start + size - 1;
  61. *base += size;
  62. pci_write_config_dword(d, PCI_BASE_ADDRESS_0 + (pos << 2), res->start);
  63. }
  64. /* Fix up PCI bridge BAR0 only */
  65. if (d->bus->number == 0 && PCI_SLOT(d->devfn) == 0)
  66. break;
  67. }
  68. /* Fix up interrupt lines */
  69. d->irq = ssb_mips_irq(extpci_core->dev) + 2;
  70. pci_write_config_byte(d, PCI_INTERRUPT_LINE, d->irq);
  71. return 0;
  72. }
  73. static void __init ssb_fixup_pcibridge(struct pci_dev *dev)
  74. {
  75. if (dev->bus->number != 0 || PCI_SLOT(dev->devfn) != 0)
  76. return;
  77. ssb_printk(KERN_INFO "PCI: fixing up bridge\n");
  78. /* Enable PCI bridge bus mastering and memory space */
  79. pci_set_master(dev);
  80. pcibios_enable_device(dev, ~0);
  81. /* Enable PCI bridge BAR1 prefetch and burst */
  82. pci_write_config_dword(dev, SSB_BAR1_CONTROL, 3);
  83. /* Make sure our latency is high enough to handle the devices behind us */
  84. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xa8);
  85. }
  86. DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, ssb_fixup_pcibridge);
  87. int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  88. {
  89. return ssb_mips_irq(extpci_core->dev) + 2;
  90. }
  91. static u32 get_cfgspace_addr(struct ssb_pcicore *pc,
  92. unsigned int bus, unsigned int dev,
  93. unsigned int func, unsigned int off)
  94. {
  95. u32 addr = 0;
  96. u32 tmp;
  97. if (unlikely(pc->cardbusmode && dev > 1))
  98. goto out;
  99. if (bus == 0) {
  100. /* Type 0 transaction */
  101. if (unlikely(dev >= SSB_PCI_SLOT_MAX))
  102. goto out;
  103. /* Slide the window */
  104. tmp = SSB_PCICORE_SBTOPCI_CFG0;
  105. tmp |= ((1 << (dev + 16)) & SSB_PCICORE_SBTOPCI1_MASK);
  106. pcicore_write32(pc, SSB_PCICORE_SBTOPCI1, tmp);
  107. /* Calculate the address */
  108. addr = SSB_PCI_CFG;
  109. addr |= ((1 << (dev + 16)) & ~SSB_PCICORE_SBTOPCI1_MASK);
  110. addr |= (func << 8);
  111. addr |= (off & ~3);
  112. } else {
  113. /* Type 1 transaction */
  114. pcicore_write32(pc, SSB_PCICORE_SBTOPCI1,
  115. SSB_PCICORE_SBTOPCI_CFG1);
  116. /* Calculate the address */
  117. addr = SSB_PCI_CFG;
  118. addr |= (bus << 16);
  119. addr |= (dev << 11);
  120. addr |= (func << 8);
  121. addr |= (off & ~3);
  122. }
  123. out:
  124. return addr;
  125. }
  126. static int ssb_extpci_read_config(struct ssb_pcicore *pc,
  127. unsigned int bus, unsigned int dev,
  128. unsigned int func, unsigned int off,
  129. void *buf, int len)
  130. {
  131. int err = -EINVAL;
  132. u32 addr, val;
  133. void __iomem *mmio;
  134. SSB_WARN_ON(!pc->hostmode);
  135. if (unlikely(len != 1 && len != 2 && len != 4))
  136. goto out;
  137. addr = get_cfgspace_addr(pc, bus, dev, func, off);
  138. if (unlikely(!addr))
  139. goto out;
  140. err = -ENOMEM;
  141. mmio = ioremap_nocache(addr, len);
  142. if (!mmio)
  143. goto out;
  144. if (mips_busprobe32(val, mmio)) {
  145. val = 0xffffffff;
  146. goto unmap;
  147. }
  148. val = readl(mmio);
  149. val >>= (8 * (off & 3));
  150. switch (len) {
  151. case 1:
  152. *((u8 *)buf) = (u8)val;
  153. break;
  154. case 2:
  155. *((u16 *)buf) = (u16)val;
  156. break;
  157. case 4:
  158. *((u32 *)buf) = (u32)val;
  159. break;
  160. }
  161. err = 0;
  162. unmap:
  163. iounmap(mmio);
  164. out:
  165. return err;
  166. }
  167. static int ssb_extpci_write_config(struct ssb_pcicore *pc,
  168. unsigned int bus, unsigned int dev,
  169. unsigned int func, unsigned int off,
  170. const void *buf, int len)
  171. {
  172. int err = -EINVAL;
  173. u32 addr, val = 0;
  174. void __iomem *mmio;
  175. SSB_WARN_ON(!pc->hostmode);
  176. if (unlikely(len != 1 && len != 2 && len != 4))
  177. goto out;
  178. addr = get_cfgspace_addr(pc, bus, dev, func, off);
  179. if (unlikely(!addr))
  180. goto out;
  181. err = -ENOMEM;
  182. mmio = ioremap_nocache(addr, len);
  183. if (!mmio)
  184. goto out;
  185. if (mips_busprobe32(val, mmio)) {
  186. val = 0xffffffff;
  187. goto unmap;
  188. }
  189. switch (len) {
  190. case 1:
  191. val = readl(mmio);
  192. val &= ~(0xFF << (8 * (off & 3)));
  193. val |= *((const u8 *)buf) << (8 * (off & 3));
  194. break;
  195. case 2:
  196. val = readl(mmio);
  197. val &= ~(0xFFFF << (8 * (off & 3)));
  198. val |= *((const u16 *)buf) << (8 * (off & 3));
  199. break;
  200. case 4:
  201. val = *((const u32 *)buf);
  202. break;
  203. }
  204. writel(val, mmio);
  205. err = 0;
  206. unmap:
  207. iounmap(mmio);
  208. out:
  209. return err;
  210. }
  211. static int ssb_pcicore_read_config(struct pci_bus *bus, unsigned int devfn,
  212. int reg, int size, u32 *val)
  213. {
  214. unsigned long flags;
  215. int err;
  216. spin_lock_irqsave(&cfgspace_lock, flags);
  217. err = ssb_extpci_read_config(extpci_core, bus->number, PCI_SLOT(devfn),
  218. PCI_FUNC(devfn), reg, val, size);
  219. spin_unlock_irqrestore(&cfgspace_lock, flags);
  220. return err ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
  221. }
  222. static int ssb_pcicore_write_config(struct pci_bus *bus, unsigned int devfn,
  223. int reg, int size, u32 val)
  224. {
  225. unsigned long flags;
  226. int err;
  227. spin_lock_irqsave(&cfgspace_lock, flags);
  228. err = ssb_extpci_write_config(extpci_core, bus->number, PCI_SLOT(devfn),
  229. PCI_FUNC(devfn), reg, &val, size);
  230. spin_unlock_irqrestore(&cfgspace_lock, flags);
  231. return err ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
  232. }
  233. static struct pci_ops ssb_pcicore_pciops = {
  234. .read = ssb_pcicore_read_config,
  235. .write = ssb_pcicore_write_config,
  236. };
  237. static struct resource ssb_pcicore_mem_resource = {
  238. .name = "SSB PCIcore external memory",
  239. .start = SSB_PCI_DMA,
  240. .end = SSB_PCI_DMA + SSB_PCI_DMA_SZ - 1,
  241. .flags = IORESOURCE_MEM,
  242. };
  243. static struct resource ssb_pcicore_io_resource = {
  244. .name = "SSB PCIcore external I/O",
  245. .start = 0x100,
  246. .end = 0x7FF,
  247. .flags = IORESOURCE_IO,
  248. };
  249. static struct pci_controller ssb_pcicore_controller = {
  250. .pci_ops = &ssb_pcicore_pciops,
  251. .io_resource = &ssb_pcicore_io_resource,
  252. .mem_resource = &ssb_pcicore_mem_resource,
  253. .mem_offset = 0x24000000,
  254. };
  255. static void ssb_pcicore_init_hostmode(struct ssb_pcicore *pc)
  256. {
  257. u32 val;
  258. if (WARN_ON(extpci_core))
  259. return;
  260. extpci_core = pc;
  261. ssb_dprintk(KERN_INFO PFX "PCIcore in host mode found\n");
  262. /* Reset devices on the external PCI bus */
  263. val = SSB_PCICORE_CTL_RST_OE;
  264. val |= SSB_PCICORE_CTL_CLK_OE;
  265. pcicore_write32(pc, SSB_PCICORE_CTL, val);
  266. val |= SSB_PCICORE_CTL_CLK; /* Clock on */
  267. pcicore_write32(pc, SSB_PCICORE_CTL, val);
  268. udelay(150); /* Assertion time demanded by the PCI standard */
  269. val |= SSB_PCICORE_CTL_RST; /* Deassert RST# */
  270. pcicore_write32(pc, SSB_PCICORE_CTL, val);
  271. val = SSB_PCICORE_ARBCTL_INTERN;
  272. pcicore_write32(pc, SSB_PCICORE_ARBCTL, val);
  273. udelay(1); /* Assertion time demanded by the PCI standard */
  274. /*TODO cardbus mode */
  275. /* 64MB I/O window */
  276. pcicore_write32(pc, SSB_PCICORE_SBTOPCI0,
  277. SSB_PCICORE_SBTOPCI_IO);
  278. /* 64MB config space */
  279. pcicore_write32(pc, SSB_PCICORE_SBTOPCI1,
  280. SSB_PCICORE_SBTOPCI_CFG0);
  281. /* 1GB memory window */
  282. pcicore_write32(pc, SSB_PCICORE_SBTOPCI2,
  283. SSB_PCICORE_SBTOPCI_MEM | SSB_PCI_DMA);
  284. /* Enable PCI bridge BAR0 prefetch and burst */
  285. val = PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
  286. ssb_extpci_write_config(pc, 0, 0, 0, PCI_COMMAND, &val, 2);
  287. /* Clear error conditions */
  288. val = 0;
  289. ssb_extpci_write_config(pc, 0, 0, 0, PCI_STATUS, &val, 2);
  290. /* Enable PCI interrupts */
  291. pcicore_write32(pc, SSB_PCICORE_IMASK,
  292. SSB_PCICORE_IMASK_INTA);
  293. /* Ok, ready to run, register it to the system.
  294. * The following needs change, if we want to port hostmode
  295. * to non-MIPS platform. */
  296. set_io_port_base((unsigned long)ioremap_nocache(SSB_PCI_MEM, 0x04000000));
  297. /* Give some time to the PCI controller to configure itself with the new
  298. * values. Not waiting at this point causes crashes of the machine. */
  299. mdelay(10);
  300. register_pci_controller(&ssb_pcicore_controller);
  301. }
  302. static int pcicore_is_in_hostmode(struct ssb_pcicore *pc)
  303. {
  304. struct ssb_bus *bus = pc->dev->bus;
  305. u16 chipid_top;
  306. u32 tmp;
  307. chipid_top = (bus->chip_id & 0xFF00);
  308. if (chipid_top != 0x4700 &&
  309. chipid_top != 0x5300)
  310. return 0;
  311. if (bus->sprom.r1.boardflags_lo & SSB_PCICORE_BFL_NOPCI)
  312. return 0;
  313. /* The 200-pin BCM4712 package does not bond out PCI. Even when
  314. * PCI is bonded out, some boards may leave the pins floating. */
  315. if (bus->chip_id == 0x4712) {
  316. if (bus->chip_package == SSB_CHIPPACK_BCM4712S)
  317. return 0;
  318. if (bus->chip_package == SSB_CHIPPACK_BCM4712M)
  319. return 0;
  320. }
  321. if (bus->chip_id == 0x5350)
  322. return 0;
  323. return !mips_busprobe32(tmp, (bus->mmio + (pc->dev->core_index * SSB_CORE_SIZE)));
  324. }
  325. #endif /* CONFIG_SSB_PCICORE_HOSTMODE */
  326. /**************************************************
  327. * Generic and Clientmode operation code.
  328. **************************************************/
  329. static void ssb_pcicore_init_clientmode(struct ssb_pcicore *pc)
  330. {
  331. /* Disable PCI interrupts. */
  332. ssb_write32(pc->dev, SSB_INTVEC, 0);
  333. }
  334. void ssb_pcicore_init(struct ssb_pcicore *pc)
  335. {
  336. struct ssb_device *dev = pc->dev;
  337. struct ssb_bus *bus;
  338. if (!dev)
  339. return;
  340. bus = dev->bus;
  341. if (!ssb_device_is_enabled(dev))
  342. ssb_device_enable(dev, 0);
  343. #ifdef CONFIG_SSB_PCICORE_HOSTMODE
  344. pc->hostmode = pcicore_is_in_hostmode(pc);
  345. if (pc->hostmode)
  346. ssb_pcicore_init_hostmode(pc);
  347. #endif /* CONFIG_SSB_PCICORE_HOSTMODE */
  348. if (!pc->hostmode)
  349. ssb_pcicore_init_clientmode(pc);
  350. }
  351. static u32 ssb_pcie_read(struct ssb_pcicore *pc, u32 address)
  352. {
  353. pcicore_write32(pc, 0x130, address);
  354. return pcicore_read32(pc, 0x134);
  355. }
  356. static void ssb_pcie_write(struct ssb_pcicore *pc, u32 address, u32 data)
  357. {
  358. pcicore_write32(pc, 0x130, address);
  359. pcicore_write32(pc, 0x134, data);
  360. }
  361. static void ssb_pcie_mdio_write(struct ssb_pcicore *pc, u8 device,
  362. u8 address, u16 data)
  363. {
  364. const u16 mdio_control = 0x128;
  365. const u16 mdio_data = 0x12C;
  366. u32 v;
  367. int i;
  368. v = 0x80; /* Enable Preamble Sequence */
  369. v |= 0x2; /* MDIO Clock Divisor */
  370. pcicore_write32(pc, mdio_control, v);
  371. v = (1 << 30); /* Start of Transaction */
  372. v |= (1 << 28); /* Write Transaction */
  373. v |= (1 << 17); /* Turnaround */
  374. v |= (u32)device << 22;
  375. v |= (u32)address << 18;
  376. v |= data;
  377. pcicore_write32(pc, mdio_data, v);
  378. /* Wait for the device to complete the transaction */
  379. udelay(10);
  380. for (i = 0; i < 10; i++) {
  381. v = pcicore_read32(pc, mdio_control);
  382. if (v & 0x100 /* Trans complete */)
  383. break;
  384. msleep(1);
  385. }
  386. pcicore_write32(pc, mdio_control, 0);
  387. }
  388. static void ssb_broadcast_value(struct ssb_device *dev,
  389. u32 address, u32 data)
  390. {
  391. /* This is used for both, PCI and ChipCommon core, so be careful. */
  392. BUILD_BUG_ON(SSB_PCICORE_BCAST_ADDR != SSB_CHIPCO_BCAST_ADDR);
  393. BUILD_BUG_ON(SSB_PCICORE_BCAST_DATA != SSB_CHIPCO_BCAST_DATA);
  394. ssb_write32(dev, SSB_PCICORE_BCAST_ADDR, address);
  395. ssb_read32(dev, SSB_PCICORE_BCAST_ADDR); /* flush */
  396. ssb_write32(dev, SSB_PCICORE_BCAST_DATA, data);
  397. ssb_read32(dev, SSB_PCICORE_BCAST_DATA); /* flush */
  398. }
  399. static void ssb_commit_settings(struct ssb_bus *bus)
  400. {
  401. struct ssb_device *dev;
  402. dev = bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev;
  403. if (WARN_ON(!dev))
  404. return;
  405. /* This forces an update of the cached registers. */
  406. ssb_broadcast_value(dev, 0xFD8, 0);
  407. }
  408. int ssb_pcicore_dev_irqvecs_enable(struct ssb_pcicore *pc,
  409. struct ssb_device *dev)
  410. {
  411. struct ssb_device *pdev = pc->dev;
  412. struct ssb_bus *bus;
  413. int err = 0;
  414. u32 tmp;
  415. might_sleep();
  416. if (!pdev)
  417. goto out;
  418. bus = pdev->bus;
  419. /* Enable interrupts for this device. */
  420. if (bus->host_pci &&
  421. ((pdev->id.revision >= 6) || (pdev->id.coreid == SSB_DEV_PCIE))) {
  422. u32 coremask;
  423. /* Calculate the "coremask" for the device. */
  424. coremask = (1 << dev->core_index);
  425. err = pci_read_config_dword(bus->host_pci, SSB_PCI_IRQMASK, &tmp);
  426. if (err)
  427. goto out;
  428. tmp |= coremask << 8;
  429. err = pci_write_config_dword(bus->host_pci, SSB_PCI_IRQMASK, tmp);
  430. if (err)
  431. goto out;
  432. } else {
  433. u32 intvec;
  434. intvec = ssb_read32(pdev, SSB_INTVEC);
  435. if ((bus->chip_id & 0xFF00) == 0x4400) {
  436. /* Workaround: On the BCM44XX the BPFLAG routing
  437. * bit is wrong. Use a hardcoded constant. */
  438. intvec |= 0x00000002;
  439. } else {
  440. tmp = ssb_read32(dev, SSB_TPSFLAG);
  441. tmp &= SSB_TPSFLAG_BPFLAG;
  442. intvec |= tmp;
  443. }
  444. ssb_write32(pdev, SSB_INTVEC, intvec);
  445. }
  446. /* Setup PCIcore operation. */
  447. if (pc->setup_done)
  448. goto out;
  449. if (pdev->id.coreid == SSB_DEV_PCI) {
  450. tmp = pcicore_read32(pc, SSB_PCICORE_SBTOPCI2);
  451. tmp |= SSB_PCICORE_SBTOPCI_PREF;
  452. tmp |= SSB_PCICORE_SBTOPCI_BURST;
  453. pcicore_write32(pc, SSB_PCICORE_SBTOPCI2, tmp);
  454. if (pdev->id.revision < 5) {
  455. tmp = ssb_read32(pdev, SSB_IMCFGLO);
  456. tmp &= ~SSB_IMCFGLO_SERTO;
  457. tmp |= 2;
  458. tmp &= ~SSB_IMCFGLO_REQTO;
  459. tmp |= 3 << SSB_IMCFGLO_REQTO_SHIFT;
  460. ssb_write32(pdev, SSB_IMCFGLO, tmp);
  461. ssb_commit_settings(bus);
  462. } else if (pdev->id.revision >= 11) {
  463. tmp = pcicore_read32(pc, SSB_PCICORE_SBTOPCI2);
  464. tmp |= SSB_PCICORE_SBTOPCI_MRM;
  465. pcicore_write32(pc, SSB_PCICORE_SBTOPCI2, tmp);
  466. }
  467. } else {
  468. WARN_ON(pdev->id.coreid != SSB_DEV_PCIE);
  469. //TODO: Better make defines for all these magic PCIE values.
  470. if ((pdev->id.revision == 0) || (pdev->id.revision == 1)) {
  471. /* TLP Workaround register. */
  472. tmp = ssb_pcie_read(pc, 0x4);
  473. tmp |= 0x8;
  474. ssb_pcie_write(pc, 0x4, tmp);
  475. }
  476. if (pdev->id.revision == 0) {
  477. const u8 serdes_rx_device = 0x1F;
  478. ssb_pcie_mdio_write(pc, serdes_rx_device,
  479. 2 /* Timer */, 0x8128);
  480. ssb_pcie_mdio_write(pc, serdes_rx_device,
  481. 6 /* CDR */, 0x0100);
  482. ssb_pcie_mdio_write(pc, serdes_rx_device,
  483. 7 /* CDR BW */, 0x1466);
  484. } else if (pdev->id.revision == 1) {
  485. /* DLLP Link Control register. */
  486. tmp = ssb_pcie_read(pc, 0x100);
  487. tmp |= 0x40;
  488. ssb_pcie_write(pc, 0x100, tmp);
  489. }
  490. }
  491. pc->setup_done = 1;
  492. out:
  493. return err;
  494. }
  495. EXPORT_SYMBOL(ssb_pcicore_dev_irqvecs_enable);