pci-alchemy.c 14 KB

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