bios32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * bios32.c - PCI BIOS functions for m68k systems.
  3. *
  4. * Written by Wout Klaren.
  5. *
  6. * Based on the DEC Alpha bios32.c by Dave Rusling and David Mosberger.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #if 0
  11. # define DBG_DEVS(args) printk args
  12. #else
  13. # define DBG_DEVS(args)
  14. #endif
  15. #ifdef CONFIG_PCI
  16. /*
  17. * PCI support for Linux/m68k. Currently only the Hades is supported.
  18. *
  19. * The support for PCI bridges in the DEC Alpha version has
  20. * been removed in this version.
  21. */
  22. #include <linux/pci.h>
  23. #include <linux/slab.h>
  24. #include <linux/mm.h>
  25. #include <asm/io.h>
  26. #include <asm/pci.h>
  27. #include <asm/uaccess.h>
  28. #define KB 1024
  29. #define MB (1024*KB)
  30. #define GB (1024*MB)
  31. #define MAJOR_REV 0
  32. #define MINOR_REV 5
  33. /*
  34. * Align VAL to ALIGN, which must be a power of two.
  35. */
  36. #define ALIGN(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
  37. /*
  38. * Offsets relative to the I/O and memory base addresses from where resources
  39. * are allocated.
  40. */
  41. #define IO_ALLOC_OFFSET 0x00004000
  42. #define MEM_ALLOC_OFFSET 0x04000000
  43. /*
  44. * Declarations of hardware specific initialisation functions.
  45. */
  46. extern struct pci_bus_info *init_hades_pci(void);
  47. /*
  48. * Bus info structure of the PCI bus. A pointer to this structure is
  49. * put in the sysdata member of the pci_bus structure.
  50. */
  51. static struct pci_bus_info *bus_info;
  52. static int pci_modify = 1; /* If set, layout the PCI bus ourself. */
  53. static int skip_vga; /* If set do not modify base addresses
  54. of vga cards.*/
  55. static int disable_pci_burst; /* If set do not allow PCI bursts. */
  56. static unsigned int io_base;
  57. static unsigned int mem_base;
  58. /*
  59. * static void disable_dev(struct pci_dev *dev)
  60. *
  61. * Disable PCI device DEV so that it does not respond to I/O or memory
  62. * accesses.
  63. *
  64. * Parameters:
  65. *
  66. * dev - device to disable.
  67. */
  68. static void __init disable_dev(struct pci_dev *dev)
  69. {
  70. unsigned short cmd;
  71. if (((dev->class >> 8 == PCI_CLASS_NOT_DEFINED_VGA) ||
  72. (dev->class >> 8 == PCI_CLASS_DISPLAY_VGA) ||
  73. (dev->class >> 8 == PCI_CLASS_DISPLAY_XGA)) && skip_vga)
  74. return;
  75. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  76. cmd &= (~PCI_COMMAND_IO & ~PCI_COMMAND_MEMORY & ~PCI_COMMAND_MASTER);
  77. pci_write_config_word(dev, PCI_COMMAND, cmd);
  78. }
  79. /*
  80. * static void layout_dev(struct pci_dev *dev)
  81. *
  82. * Layout memory and I/O for a device.
  83. *
  84. * Parameters:
  85. *
  86. * device - device to layout memory and I/O for.
  87. */
  88. static void __init layout_dev(struct pci_dev *dev)
  89. {
  90. unsigned short cmd;
  91. unsigned int base, mask, size, reg;
  92. unsigned int alignto;
  93. int i;
  94. /*
  95. * Skip video cards if requested.
  96. */
  97. if (((dev->class >> 8 == PCI_CLASS_NOT_DEFINED_VGA) ||
  98. (dev->class >> 8 == PCI_CLASS_DISPLAY_VGA) ||
  99. (dev->class >> 8 == PCI_CLASS_DISPLAY_XGA)) && skip_vga)
  100. return;
  101. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  102. for (reg = PCI_BASE_ADDRESS_0, i = 0; reg <= PCI_BASE_ADDRESS_5; reg += 4, i++)
  103. {
  104. /*
  105. * Figure out how much space and of what type this
  106. * device wants.
  107. */
  108. pci_write_config_dword(dev, reg, 0xffffffff);
  109. pci_read_config_dword(dev, reg, &base);
  110. if (!base)
  111. {
  112. /* this base-address register is unused */
  113. dev->resource[i].start = 0;
  114. dev->resource[i].end = 0;
  115. dev->resource[i].flags = 0;
  116. continue;
  117. }
  118. /*
  119. * We've read the base address register back after
  120. * writing all ones and so now we must decode it.
  121. */
  122. if (base & PCI_BASE_ADDRESS_SPACE_IO)
  123. {
  124. /*
  125. * I/O space base address register.
  126. */
  127. cmd |= PCI_COMMAND_IO;
  128. base &= PCI_BASE_ADDRESS_IO_MASK;
  129. mask = (~base << 1) | 0x1;
  130. size = (mask & base) & 0xffffffff;
  131. /*
  132. * Align to multiple of size of minimum base.
  133. */
  134. alignto = max_t(unsigned int, 0x040, size);
  135. base = ALIGN(io_base, alignto);
  136. io_base = base + size;
  137. pci_write_config_dword(dev, reg, base | PCI_BASE_ADDRESS_SPACE_IO);
  138. dev->resource[i].start = base;
  139. dev->resource[i].end = dev->resource[i].start + size - 1;
  140. dev->resource[i].flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  141. DBG_DEVS(("layout_dev: IO address: %lX\n", base));
  142. }
  143. else
  144. {
  145. unsigned int type;
  146. /*
  147. * Memory space base address register.
  148. */
  149. cmd |= PCI_COMMAND_MEMORY;
  150. type = base & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  151. base &= PCI_BASE_ADDRESS_MEM_MASK;
  152. mask = (~base << 1) | 0x1;
  153. size = (mask & base) & 0xffffffff;
  154. switch (type)
  155. {
  156. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  157. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  158. break;
  159. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  160. printk("bios32 WARNING: slot %d, function %d "
  161. "requests memory below 1MB---don't "
  162. "know how to do that.\n",
  163. PCI_SLOT(dev->devfn),
  164. PCI_FUNC(dev->devfn));
  165. continue;
  166. }
  167. /*
  168. * Align to multiple of size of minimum base.
  169. */
  170. alignto = max_t(unsigned int, 0x1000, size);
  171. base = ALIGN(mem_base, alignto);
  172. mem_base = base + size;
  173. pci_write_config_dword(dev, reg, base);
  174. dev->resource[i].start = base;
  175. dev->resource[i].end = dev->resource[i].start + size - 1;
  176. dev->resource[i].flags = IORESOURCE_MEM;
  177. if (type == PCI_BASE_ADDRESS_MEM_TYPE_64)
  178. {
  179. /*
  180. * 64-bit address, set the highest 32 bits
  181. * to zero.
  182. */
  183. reg += 4;
  184. pci_write_config_dword(dev, reg, 0);
  185. i++;
  186. dev->resource[i].start = 0;
  187. dev->resource[i].end = 0;
  188. dev->resource[i].flags = 0;
  189. }
  190. }
  191. }
  192. /*
  193. * Enable device:
  194. */
  195. if (dev->class >> 8 == PCI_CLASS_NOT_DEFINED ||
  196. dev->class >> 8 == PCI_CLASS_NOT_DEFINED_VGA ||
  197. dev->class >> 8 == PCI_CLASS_DISPLAY_VGA ||
  198. dev->class >> 8 == PCI_CLASS_DISPLAY_XGA)
  199. {
  200. /*
  201. * All of these (may) have I/O scattered all around
  202. * and may not use i/o-base address registers at all.
  203. * So we just have to always enable I/O to these
  204. * devices.
  205. */
  206. cmd |= PCI_COMMAND_IO;
  207. }
  208. pci_write_config_word(dev, PCI_COMMAND, cmd | PCI_COMMAND_MASTER);
  209. pci_write_config_byte(dev, PCI_LATENCY_TIMER, (disable_pci_burst) ? 0 : 32);
  210. if (bus_info != NULL)
  211. bus_info->conf_device(dev); /* Machine dependent configuration. */
  212. DBG_DEVS(("layout_dev: bus %d slot 0x%x VID 0x%x DID 0x%x class 0x%x\n",
  213. dev->bus->number, PCI_SLOT(dev->devfn), dev->vendor, dev->device, dev->class));
  214. }
  215. /*
  216. * static void layout_bus(struct pci_bus *bus)
  217. *
  218. * Layout memory and I/O for all devices on the given bus.
  219. *
  220. * Parameters:
  221. *
  222. * bus - bus.
  223. */
  224. static void __init layout_bus(struct pci_bus *bus)
  225. {
  226. unsigned int bio, bmem;
  227. struct pci_dev *dev;
  228. DBG_DEVS(("layout_bus: starting bus %d\n", bus->number));
  229. if (!bus->devices && !bus->children)
  230. return;
  231. /*
  232. * Align the current bases on appropriate boundaries (4K for
  233. * IO and 1MB for memory).
  234. */
  235. bio = io_base = ALIGN(io_base, 4*KB);
  236. bmem = mem_base = ALIGN(mem_base, 1*MB);
  237. /*
  238. * PCI devices might have been setup by a PCI BIOS emulation
  239. * running under TOS. In these cases there is a
  240. * window during which two devices may have an overlapping
  241. * address range. To avoid this causing trouble, we first
  242. * turn off the I/O and memory address decoders for all PCI
  243. * devices. They'll be re-enabled only once all address
  244. * decoders are programmed consistently.
  245. */
  246. DBG_DEVS(("layout_bus: disable_dev for bus %d\n", bus->number));
  247. for (dev = bus->devices; dev; dev = dev->sibling)
  248. {
  249. if ((dev->class >> 16 != PCI_BASE_CLASS_BRIDGE) ||
  250. (dev->class >> 8 == PCI_CLASS_BRIDGE_PCMCIA))
  251. disable_dev(dev);
  252. }
  253. /*
  254. * Allocate space to each device:
  255. */
  256. DBG_DEVS(("layout_bus: starting bus %d devices\n", bus->number));
  257. for (dev = bus->devices; dev; dev = dev->sibling)
  258. {
  259. if ((dev->class >> 16 != PCI_BASE_CLASS_BRIDGE) ||
  260. (dev->class >> 8 == PCI_CLASS_BRIDGE_PCMCIA))
  261. layout_dev(dev);
  262. }
  263. DBG_DEVS(("layout_bus: bus %d finished\n", bus->number));
  264. }
  265. /*
  266. * static void pcibios_fixup(void)
  267. *
  268. * Layout memory and I/O of all devices on the PCI bus if 'pci_modify' is
  269. * true. This might be necessary because not every m68k machine with a PCI
  270. * bus has a PCI BIOS. This function should be called right after
  271. * pci_scan_bus() in pcibios_init().
  272. */
  273. static void __init pcibios_fixup(void)
  274. {
  275. if (pci_modify)
  276. {
  277. /*
  278. * Set base addresses for allocation of I/O and memory space.
  279. */
  280. io_base = bus_info->io_space.start + IO_ALLOC_OFFSET;
  281. mem_base = bus_info->mem_space.start + MEM_ALLOC_OFFSET;
  282. /*
  283. * Scan the tree, allocating PCI memory and I/O space.
  284. */
  285. layout_bus(pci_bus_b(pci_root.next));
  286. }
  287. /*
  288. * Fix interrupt assignments, etc.
  289. */
  290. bus_info->fixup(pci_modify);
  291. }
  292. /*
  293. * static void pcibios_claim_resources(struct pci_bus *bus)
  294. *
  295. * Claim all resources that are assigned to devices on the given bus.
  296. *
  297. * Parameters:
  298. *
  299. * bus - bus.
  300. */
  301. static void __init pcibios_claim_resources(struct pci_bus *bus)
  302. {
  303. struct pci_dev *dev;
  304. int i;
  305. while (bus)
  306. {
  307. for (dev = bus->devices; (dev != NULL); dev = dev->sibling)
  308. {
  309. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  310. {
  311. struct resource *r = &dev->resource[i];
  312. struct resource *pr;
  313. struct pci_bus_info *bus_info = (struct pci_bus_info *) dev->sysdata;
  314. if ((r->start == 0) || (r->parent != NULL))
  315. continue;
  316. #if 1
  317. if (r->flags & IORESOURCE_IO)
  318. pr = &bus_info->io_space;
  319. else
  320. pr = &bus_info->mem_space;
  321. #else
  322. if (r->flags & IORESOURCE_IO)
  323. pr = &ioport_resource;
  324. else
  325. pr = &iomem_resource;
  326. #endif
  327. if (request_resource(pr, r) < 0)
  328. {
  329. printk(KERN_ERR "PCI: Address space collision on region %d of device %s\n", i, dev->name);
  330. }
  331. }
  332. }
  333. if (bus->children)
  334. pcibios_claim_resources(bus->children);
  335. bus = bus->next;
  336. }
  337. }
  338. /*
  339. * int pcibios_assign_resource(struct pci_dev *dev, int i)
  340. *
  341. * Assign a new address to a PCI resource.
  342. *
  343. * Parameters:
  344. *
  345. * dev - device.
  346. * i - resource.
  347. *
  348. * Result: 0 if successful.
  349. */
  350. int __init pcibios_assign_resource(struct pci_dev *dev, int i)
  351. {
  352. struct resource *r = &dev->resource[i];
  353. struct resource *pr = pci_find_parent_resource(dev, r);
  354. unsigned long size = r->end + 1;
  355. if (!pr)
  356. return -EINVAL;
  357. if (r->flags & IORESOURCE_IO)
  358. {
  359. if (size > 0x100)
  360. return -EFBIG;
  361. if (allocate_resource(pr, r, size, bus_info->io_space.start +
  362. IO_ALLOC_OFFSET, bus_info->io_space.end, 1024))
  363. return -EBUSY;
  364. }
  365. else
  366. {
  367. if (allocate_resource(pr, r, size, bus_info->mem_space.start +
  368. MEM_ALLOC_OFFSET, bus_info->mem_space.end, size))
  369. return -EBUSY;
  370. }
  371. if (i < 6)
  372. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, r->start);
  373. return 0;
  374. }
  375. void __init pcibios_fixup_bus(struct pci_bus *bus)
  376. {
  377. struct pci_dev *dev;
  378. void *sysdata;
  379. sysdata = (bus->parent) ? bus->parent->sysdata : bus->sysdata;
  380. for (dev = bus->devices; (dev != NULL); dev = dev->sibling)
  381. dev->sysdata = sysdata;
  382. }
  383. void __init pcibios_init(void)
  384. {
  385. printk("Linux/m68k PCI BIOS32 revision %x.%02x\n", MAJOR_REV, MINOR_REV);
  386. bus_info = NULL;
  387. #ifdef CONFIG_HADES
  388. if (MACH_IS_HADES)
  389. bus_info = init_hades_pci();
  390. #endif
  391. if (bus_info != NULL)
  392. {
  393. printk("PCI: Probing PCI hardware\n");
  394. pci_scan_bus(0, bus_info->m68k_pci_ops, bus_info);
  395. pcibios_fixup();
  396. pcibios_claim_resources(pci_root);
  397. }
  398. else
  399. printk("PCI: No PCI bus detected\n");
  400. }
  401. char * __init pcibios_setup(char *str)
  402. {
  403. if (!strcmp(str, "nomodify"))
  404. {
  405. pci_modify = 0;
  406. return NULL;
  407. }
  408. else if (!strcmp(str, "skipvga"))
  409. {
  410. skip_vga = 1;
  411. return NULL;
  412. }
  413. else if (!strcmp(str, "noburst"))
  414. {
  415. disable_pci_burst = 1;
  416. return NULL;
  417. }
  418. return str;
  419. }
  420. #endif /* CONFIG_PCI */