pci-alchemy.c 14 KB

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