pci-alchemy.c 14 KB

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