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