pci-alchemy.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * Alchemy PCI host mode support.
  3. *
  4. * Copyright 2001-2003, 2007-2008 MontaVista Software Inc.
  5. * Author: MontaVista Software, Inc. <source@mvista.com>
  6. *
  7. * Support for all devices (greater than 16) added by David Gathright.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/pci.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/vmalloc.h>
  15. #include <asm/mach-au1x00/au1000.h>
  16. #ifdef CONFIG_DEBUG_PCI
  17. #define DBG(x...) printk(KERN_DEBUG x)
  18. #else
  19. #define DBG(x...) do {} while (0)
  20. #endif
  21. #define PCI_ACCESS_READ 0
  22. #define PCI_ACCESS_WRITE 1
  23. struct alchemy_pci_context {
  24. struct pci_controller alchemy_pci_ctrl; /* leave as first member! */
  25. void __iomem *regs; /* ctrl base */
  26. /* tools for wired entry for config space access */
  27. unsigned long last_elo0;
  28. unsigned long last_elo1;
  29. int wired_entry;
  30. struct vm_struct *pci_cfg_vm;
  31. unsigned long pm[12];
  32. int (*board_map_irq)(const struct pci_dev *d, u8 slot, u8 pin);
  33. int (*board_pci_idsel)(unsigned int devsel, int assert);
  34. };
  35. /* IO/MEM resources for PCI. Keep the memres in sync with __fixup_bigphys_addr
  36. * in arch/mips/alchemy/common/setup.c
  37. */
  38. static struct resource alchemy_pci_def_memres = {
  39. .start = ALCHEMY_PCI_MEMWIN_START,
  40. .end = ALCHEMY_PCI_MEMWIN_END,
  41. .name = "PCI memory space",
  42. .flags = IORESOURCE_MEM
  43. };
  44. static struct resource alchemy_pci_def_iores = {
  45. .start = ALCHEMY_PCI_IOWIN_START,
  46. .end = ALCHEMY_PCI_IOWIN_END,
  47. .name = "PCI IO space",
  48. .flags = IORESOURCE_IO
  49. };
  50. static void mod_wired_entry(int entry, unsigned long entrylo0,
  51. unsigned long entrylo1, unsigned long entryhi,
  52. unsigned long pagemask)
  53. {
  54. unsigned long old_pagemask;
  55. unsigned long old_ctx;
  56. /* Save old context and create impossible VPN2 value */
  57. old_ctx = read_c0_entryhi() & 0xff;
  58. old_pagemask = read_c0_pagemask();
  59. write_c0_index(entry);
  60. write_c0_pagemask(pagemask);
  61. write_c0_entryhi(entryhi);
  62. write_c0_entrylo0(entrylo0);
  63. write_c0_entrylo1(entrylo1);
  64. tlb_write_indexed();
  65. write_c0_entryhi(old_ctx);
  66. write_c0_pagemask(old_pagemask);
  67. }
  68. static void alchemy_pci_wired_entry(struct alchemy_pci_context *ctx)
  69. {
  70. ctx->wired_entry = read_c0_wired();
  71. add_wired_entry(0, 0, (unsigned long)ctx->pci_cfg_vm->addr, PM_4K);
  72. ctx->last_elo0 = ctx->last_elo1 = ~0;
  73. }
  74. static int config_access(unsigned char access_type, struct pci_bus *bus,
  75. unsigned int dev_fn, unsigned char where, u32 *data)
  76. {
  77. struct alchemy_pci_context *ctx = bus->sysdata;
  78. unsigned int device = PCI_SLOT(dev_fn);
  79. unsigned int function = PCI_FUNC(dev_fn);
  80. unsigned long offset, status, cfg_base, flags, entryLo0, entryLo1, r;
  81. int error = PCIBIOS_SUCCESSFUL;
  82. if (device > 19) {
  83. *data = 0xffffffff;
  84. return -1;
  85. }
  86. /* YAMON on all db1xxx boards wipes the TLB and writes zero to C0_wired
  87. * on resume, clearing our wired entry. Unfortunately the ->resume()
  88. * callback is called way way way too late (and ->suspend() too early)
  89. * to have them destroy and recreate it. Instead just test if c0_wired
  90. * is now lower than the index we retrieved before suspending and then
  91. * recreate the entry if necessary. Of course this is totally bonkers
  92. * and breaks as soon as someone else adds another wired entry somewhere
  93. * else. Anyone have any ideas how to handle this better?
  94. */
  95. if (unlikely(read_c0_wired() < ctx->wired_entry))
  96. alchemy_pci_wired_entry(ctx);
  97. local_irq_save(flags);
  98. r = __raw_readl(ctx->regs + PCI_REG_STATCMD) & 0x0000ffff;
  99. r |= PCI_STATCMD_STATUS(0x2000);
  100. __raw_writel(r, ctx->regs + PCI_REG_STATCMD);
  101. wmb();
  102. /* Allow board vendors to implement their own off-chip IDSEL.
  103. * If it doesn't succeed, may as well bail out at this point.
  104. */
  105. if (ctx->board_pci_idsel(device, 1) == 0) {
  106. *data = 0xffffffff;
  107. local_irq_restore(flags);
  108. return -1;
  109. }
  110. /* Setup the config window */
  111. if (bus->number == 0)
  112. cfg_base = (1 << device) << 11;
  113. else
  114. cfg_base = 0x80000000 | (bus->number << 16) | (device << 11);
  115. /* Setup the lower bits of the 36-bit address */
  116. offset = (function << 8) | (where & ~0x3);
  117. /* Pick up any address that falls below the page mask */
  118. offset |= cfg_base & ~PAGE_MASK;
  119. /* Page boundary */
  120. cfg_base = cfg_base & PAGE_MASK;
  121. /* To improve performance, if the current device is the same as
  122. * the last device accessed, we don't touch the TLB.
  123. */
  124. entryLo0 = (6 << 26) | (cfg_base >> 6) | (2 << 3) | 7;
  125. entryLo1 = (6 << 26) | (cfg_base >> 6) | (0x1000 >> 6) | (2 << 3) | 7;
  126. if ((entryLo0 != ctx->last_elo0) || (entryLo1 != ctx->last_elo1)) {
  127. mod_wired_entry(ctx->wired_entry, entryLo0, entryLo1,
  128. (unsigned long)ctx->pci_cfg_vm->addr, PM_4K);
  129. ctx->last_elo0 = entryLo0;
  130. ctx->last_elo1 = entryLo1;
  131. }
  132. if (access_type == PCI_ACCESS_WRITE)
  133. __raw_writel(*data, ctx->pci_cfg_vm->addr + offset);
  134. else
  135. *data = __raw_readl(ctx->pci_cfg_vm->addr + offset);
  136. wmb();
  137. DBG("alchemy-pci: cfg access %d bus %u dev %u at %x dat %x conf %lx\n",
  138. access_type, bus->number, device, where, *data, offset);
  139. /* check for errors, master abort */
  140. status = __raw_readl(ctx->regs + PCI_REG_STATCMD);
  141. if (status & (1 << 29)) {
  142. *data = 0xffffffff;
  143. error = -1;
  144. DBG("alchemy-pci: master abort on cfg access %d bus %d dev %d",
  145. access_type, bus->number, device);
  146. } else if ((status >> 28) & 0xf) {
  147. DBG("alchemy-pci: PCI ERR detected: dev %d, status %lx\n",
  148. device, (status >> 28) & 0xf);
  149. /* clear errors */
  150. __raw_writel(status & 0xf000ffff, ctx->regs + PCI_REG_STATCMD);
  151. *data = 0xffffffff;
  152. error = -1;
  153. }
  154. /* Take away the IDSEL. */
  155. (void)ctx->board_pci_idsel(device, 0);
  156. local_irq_restore(flags);
  157. return error;
  158. }
  159. static int read_config_byte(struct pci_bus *bus, unsigned int devfn,
  160. int where, u8 *val)
  161. {
  162. u32 data;
  163. int ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
  164. if (where & 1)
  165. data >>= 8;
  166. if (where & 2)
  167. data >>= 16;
  168. *val = data & 0xff;
  169. return ret;
  170. }
  171. static int read_config_word(struct pci_bus *bus, unsigned int devfn,
  172. int where, u16 *val)
  173. {
  174. u32 data;
  175. int ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
  176. if (where & 2)
  177. data >>= 16;
  178. *val = data & 0xffff;
  179. return ret;
  180. }
  181. static int read_config_dword(struct pci_bus *bus, unsigned int devfn,
  182. int where, u32 *val)
  183. {
  184. return config_access(PCI_ACCESS_READ, bus, devfn, where, val);
  185. }
  186. static int write_config_byte(struct pci_bus *bus, unsigned int devfn,
  187. int where, u8 val)
  188. {
  189. u32 data = 0;
  190. if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
  191. return -1;
  192. data = (data & ~(0xff << ((where & 3) << 3))) |
  193. (val << ((where & 3) << 3));
  194. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
  195. return -1;
  196. return PCIBIOS_SUCCESSFUL;
  197. }
  198. static int write_config_word(struct pci_bus *bus, unsigned int devfn,
  199. int where, u16 val)
  200. {
  201. u32 data = 0;
  202. if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
  203. return -1;
  204. data = (data & ~(0xffff << ((where & 3) << 3))) |
  205. (val << ((where & 3) << 3));
  206. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
  207. return -1;
  208. return PCIBIOS_SUCCESSFUL;
  209. }
  210. static int write_config_dword(struct pci_bus *bus, unsigned int devfn,
  211. int where, u32 val)
  212. {
  213. return config_access(PCI_ACCESS_WRITE, bus, devfn, where, &val);
  214. }
  215. static int alchemy_pci_read(struct pci_bus *bus, unsigned int devfn,
  216. int where, int size, u32 *val)
  217. {
  218. switch (size) {
  219. case 1: {
  220. u8 _val;
  221. int rc = read_config_byte(bus, devfn, where, &_val);
  222. *val = _val;
  223. return rc;
  224. }
  225. case 2: {
  226. u16 _val;
  227. int rc = read_config_word(bus, devfn, where, &_val);
  228. *val = _val;
  229. return rc;
  230. }
  231. default:
  232. return read_config_dword(bus, devfn, where, val);
  233. }
  234. }
  235. static int alchemy_pci_write(struct pci_bus *bus, unsigned int devfn,
  236. int where, int size, u32 val)
  237. {
  238. switch (size) {
  239. case 1:
  240. return write_config_byte(bus, devfn, where, (u8) val);
  241. case 2:
  242. return write_config_word(bus, devfn, where, (u16) val);
  243. default:
  244. return write_config_dword(bus, devfn, where, val);
  245. }
  246. }
  247. static struct pci_ops alchemy_pci_ops = {
  248. .read = alchemy_pci_read,
  249. .write = alchemy_pci_write,
  250. };
  251. static int alchemy_pci_def_idsel(unsigned int devsel, int assert)
  252. {
  253. return 1; /* success */
  254. }
  255. static int __devinit alchemy_pci_probe(struct platform_device *pdev)
  256. {
  257. struct alchemy_pci_platdata *pd = pdev->dev.platform_data;
  258. struct alchemy_pci_context *ctx;
  259. void __iomem *virt_io;
  260. unsigned long val;
  261. struct resource *r;
  262. int ret;
  263. /* need at least PCI IRQ mapping table */
  264. if (!pd) {
  265. dev_err(&pdev->dev, "need platform data for PCI setup\n");
  266. ret = -ENODEV;
  267. goto out;
  268. }
  269. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  270. if (!ctx) {
  271. dev_err(&pdev->dev, "no memory for pcictl context\n");
  272. ret = -ENOMEM;
  273. goto out;
  274. }
  275. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  276. if (!r) {
  277. dev_err(&pdev->dev, "no pcictl ctrl regs resource\n");
  278. ret = -ENODEV;
  279. goto out1;
  280. }
  281. if (!request_mem_region(r->start, resource_size(r), pdev->name)) {
  282. dev_err(&pdev->dev, "cannot claim pci regs\n");
  283. ret = -ENODEV;
  284. goto out1;
  285. }
  286. ctx->regs = ioremap_nocache(r->start, resource_size(r));
  287. if (!ctx->regs) {
  288. dev_err(&pdev->dev, "cannot map pci regs\n");
  289. ret = -ENODEV;
  290. goto out2;
  291. }
  292. /* map parts of the PCI IO area */
  293. /* REVISIT: if this changes with a newer variant (doubt it) make this
  294. * a platform resource.
  295. */
  296. virt_io = ioremap(AU1500_PCI_IO_PHYS_ADDR, 0x00100000);
  297. if (!virt_io) {
  298. dev_err(&pdev->dev, "cannot remap pci io space\n");
  299. ret = -ENODEV;
  300. goto out3;
  301. }
  302. ctx->alchemy_pci_ctrl.io_map_base = (unsigned long)virt_io;
  303. #ifdef CONFIG_DMA_NONCOHERENT
  304. /* Au1500 revisions older than AD have borked coherent PCI */
  305. if ((alchemy_get_cputype() == ALCHEMY_CPU_AU1500) &&
  306. (read_c0_prid() < 0x01030202)) {
  307. val = __raw_readl(ctx->regs + PCI_REG_CONFIG);
  308. val |= PCI_CONFIG_NC;
  309. __raw_writel(val, ctx->regs + PCI_REG_CONFIG);
  310. wmb();
  311. dev_info(&pdev->dev, "non-coherent PCI on Au1500 AA/AB/AC\n");
  312. }
  313. #endif
  314. if (pd->board_map_irq)
  315. ctx->board_map_irq = pd->board_map_irq;
  316. if (pd->board_pci_idsel)
  317. ctx->board_pci_idsel = pd->board_pci_idsel;
  318. else
  319. ctx->board_pci_idsel = alchemy_pci_def_idsel;
  320. /* fill in relevant pci_controller members */
  321. ctx->alchemy_pci_ctrl.pci_ops = &alchemy_pci_ops;
  322. ctx->alchemy_pci_ctrl.mem_resource = &alchemy_pci_def_memres;
  323. ctx->alchemy_pci_ctrl.io_resource = &alchemy_pci_def_iores;
  324. /* we can't ioremap the entire pci config space because it's too large,
  325. * nor can we dynamically ioremap it because some drivers use the
  326. * PCI config routines from within atomic contex and that becomes a
  327. * problem in get_vm_area(). Instead we use one wired TLB entry to
  328. * handle all config accesses for all busses.
  329. */
  330. ctx->pci_cfg_vm = get_vm_area(0x2000, VM_IOREMAP);
  331. if (!ctx->pci_cfg_vm) {
  332. dev_err(&pdev->dev, "unable to get vm area\n");
  333. ret = -ENOMEM;
  334. goto out4;
  335. }
  336. ctx->wired_entry = 8192; /* impossibly high value */
  337. set_io_port_base((unsigned long)ctx->alchemy_pci_ctrl.io_map_base);
  338. /* board may want to modify bits in the config register, do it now */
  339. val = __raw_readl(ctx->regs + PCI_REG_CONFIG);
  340. val &= ~pd->pci_cfg_clr;
  341. val |= pd->pci_cfg_set;
  342. val &= ~PCI_CONFIG_PD; /* clear disable bit */
  343. __raw_writel(val, ctx->regs + PCI_REG_CONFIG);
  344. wmb();
  345. platform_set_drvdata(pdev, ctx);
  346. register_pci_controller(&ctx->alchemy_pci_ctrl);
  347. return 0;
  348. out4:
  349. iounmap(virt_io);
  350. out3:
  351. iounmap(ctx->regs);
  352. out2:
  353. release_mem_region(r->start, resource_size(r));
  354. out1:
  355. kfree(ctx);
  356. out:
  357. return ret;
  358. }
  359. #ifdef CONFIG_PM
  360. /* save PCI controller register contents. */
  361. static int alchemy_pci_suspend(struct device *dev)
  362. {
  363. struct alchemy_pci_context *ctx = dev_get_drvdata(dev);
  364. ctx->pm[0] = __raw_readl(ctx->regs + PCI_REG_CMEM);
  365. ctx->pm[1] = __raw_readl(ctx->regs + PCI_REG_CONFIG) & 0x0009ffff;
  366. ctx->pm[2] = __raw_readl(ctx->regs + PCI_REG_B2BMASK_CCH);
  367. ctx->pm[3] = __raw_readl(ctx->regs + PCI_REG_B2BBASE0_VID);
  368. ctx->pm[4] = __raw_readl(ctx->regs + PCI_REG_B2BBASE1_SID);
  369. ctx->pm[5] = __raw_readl(ctx->regs + PCI_REG_MWMASK_DEV);
  370. ctx->pm[6] = __raw_readl(ctx->regs + PCI_REG_MWBASE_REV_CCL);
  371. ctx->pm[7] = __raw_readl(ctx->regs + PCI_REG_ID);
  372. ctx->pm[8] = __raw_readl(ctx->regs + PCI_REG_CLASSREV);
  373. ctx->pm[9] = __raw_readl(ctx->regs + PCI_REG_PARAM);
  374. ctx->pm[10] = __raw_readl(ctx->regs + PCI_REG_MBAR);
  375. ctx->pm[11] = __raw_readl(ctx->regs + PCI_REG_TIMEOUT);
  376. return 0;
  377. }
  378. static int alchemy_pci_resume(struct device *dev)
  379. {
  380. struct alchemy_pci_context *ctx = dev_get_drvdata(dev);
  381. __raw_writel(ctx->pm[0], ctx->regs + PCI_REG_CMEM);
  382. __raw_writel(ctx->pm[2], ctx->regs + PCI_REG_B2BMASK_CCH);
  383. __raw_writel(ctx->pm[3], ctx->regs + PCI_REG_B2BBASE0_VID);
  384. __raw_writel(ctx->pm[4], ctx->regs + PCI_REG_B2BBASE1_SID);
  385. __raw_writel(ctx->pm[5], ctx->regs + PCI_REG_MWMASK_DEV);
  386. __raw_writel(ctx->pm[6], ctx->regs + PCI_REG_MWBASE_REV_CCL);
  387. __raw_writel(ctx->pm[7], ctx->regs + PCI_REG_ID);
  388. __raw_writel(ctx->pm[8], ctx->regs + PCI_REG_CLASSREV);
  389. __raw_writel(ctx->pm[9], ctx->regs + PCI_REG_PARAM);
  390. __raw_writel(ctx->pm[10], ctx->regs + PCI_REG_MBAR);
  391. __raw_writel(ctx->pm[11], ctx->regs + PCI_REG_TIMEOUT);
  392. wmb();
  393. __raw_writel(ctx->pm[1], ctx->regs + PCI_REG_CONFIG);
  394. wmb();
  395. return 0;
  396. }
  397. static const struct dev_pm_ops alchemy_pci_pmops = {
  398. .suspend = alchemy_pci_suspend,
  399. .resume = alchemy_pci_resume,
  400. };
  401. #define ALCHEMY_PCICTL_PM (&alchemy_pci_pmops)
  402. #else
  403. #define ALCHEMY_PCICTL_PM NULL
  404. #endif
  405. static struct platform_driver alchemy_pcictl_driver = {
  406. .probe = alchemy_pci_probe,
  407. .driver = {
  408. .name = "alchemy-pci",
  409. .owner = THIS_MODULE,
  410. .pm = ALCHEMY_PCICTL_PM,
  411. },
  412. };
  413. static int __init alchemy_pci_init(void)
  414. {
  415. /* Au1500/Au1550 have PCI */
  416. switch (alchemy_get_cputype()) {
  417. case ALCHEMY_CPU_AU1500:
  418. case ALCHEMY_CPU_AU1550:
  419. return platform_driver_register(&alchemy_pcictl_driver);
  420. }
  421. return 0;
  422. }
  423. arch_initcall(alchemy_pci_init);
  424. int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  425. {
  426. struct alchemy_pci_context *ctx = dev->sysdata;
  427. if (ctx && ctx->board_map_irq)
  428. return ctx->board_map_irq(dev, slot, pin);
  429. return -1;
  430. }
  431. int pcibios_plat_dev_init(struct pci_dev *dev)
  432. {
  433. return 0;
  434. }