hades-pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * hades-pci.c - Hardware specific PCI BIOS functions the Hades Atari clone.
  3. *
  4. * Written by Wout Klaren.
  5. */
  6. #include <linux/config.h>
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <asm/io.h>
  10. #if 0
  11. # define DBG_DEVS(args) printk args
  12. #else
  13. # define DBG_DEVS(args)
  14. #endif
  15. #if defined(CONFIG_PCI) && defined(CONFIG_HADES)
  16. #include <linux/slab.h>
  17. #include <linux/mm.h>
  18. #include <linux/pci.h>
  19. #include <asm/atarihw.h>
  20. #include <asm/atariints.h>
  21. #include <asm/byteorder.h>
  22. #include <asm/pci.h>
  23. #define HADES_MEM_BASE 0x80000000
  24. #define HADES_MEM_SIZE 0x20000000
  25. #define HADES_CONFIG_BASE 0xA0000000
  26. #define HADES_CONFIG_SIZE 0x10000000
  27. #define HADES_IO_BASE 0xB0000000
  28. #define HADES_IO_SIZE 0x10000000
  29. #define HADES_VIRT_IO_SIZE 0x00010000 /* Only 64k is remapped and actually used. */
  30. #define N_SLOTS 4 /* Number of PCI slots. */
  31. static const char pci_mem_name[] = "PCI memory space";
  32. static const char pci_io_name[] = "PCI I/O space";
  33. static const char pci_config_name[] = "PCI config space";
  34. static struct resource config_space = {
  35. .name = pci_config_name,
  36. .start = HADES_CONFIG_BASE,
  37. .end = HADES_CONFIG_BASE + HADES_CONFIG_SIZE - 1
  38. };
  39. static struct resource io_space = {
  40. .name = pci_io_name,
  41. .start = HADES_IO_BASE,
  42. .end = HADES_IO_BASE + HADES_IO_SIZE - 1
  43. };
  44. static const unsigned long pci_conf_base_phys[] = {
  45. 0xA0080000, 0xA0040000, 0xA0020000, 0xA0010000
  46. };
  47. static unsigned long pci_conf_base_virt[N_SLOTS];
  48. static unsigned long pci_io_base_virt;
  49. /*
  50. * static void *mk_conf_addr(unsigned char bus, unsigned char device_fn,
  51. * unsigned char where)
  52. *
  53. * Calculate the address of the PCI configuration area of the given
  54. * device.
  55. *
  56. * BUG: boards with multiple functions are probably not correctly
  57. * supported.
  58. */
  59. static void *mk_conf_addr(struct pci_dev *dev, int where)
  60. {
  61. int device = dev->devfn >> 3, function = dev->devfn & 7;
  62. void *result;
  63. DBG_DEVS(("mk_conf_addr(bus=%d ,device_fn=0x%x, where=0x%x, pci_addr=0x%p)\n",
  64. dev->bus->number, dev->devfn, where, pci_addr));
  65. if (device > 3)
  66. {
  67. DBG_DEVS(("mk_conf_addr: device (%d) > 3, returning NULL\n", device));
  68. return NULL;
  69. }
  70. if (dev->bus->number != 0)
  71. {
  72. DBG_DEVS(("mk_conf_addr: bus (%d) > 0, returning NULL\n", device));
  73. return NULL;
  74. }
  75. result = (void *) (pci_conf_base_virt[device] | (function << 8) | (where));
  76. DBG_DEVS(("mk_conf_addr: returning pci_addr 0x%lx\n", (unsigned long) result));
  77. return result;
  78. }
  79. static int hades_read_config_byte(struct pci_dev *dev, int where, u8 *value)
  80. {
  81. volatile unsigned char *pci_addr;
  82. *value = 0xff;
  83. if ((pci_addr = (unsigned char *) mk_conf_addr(dev, where)) == NULL)
  84. return PCIBIOS_DEVICE_NOT_FOUND;
  85. *value = *pci_addr;
  86. return PCIBIOS_SUCCESSFUL;
  87. }
  88. static int hades_read_config_word(struct pci_dev *dev, int where, u16 *value)
  89. {
  90. volatile unsigned short *pci_addr;
  91. *value = 0xffff;
  92. if (where & 0x1)
  93. return PCIBIOS_BAD_REGISTER_NUMBER;
  94. if ((pci_addr = (unsigned short *) mk_conf_addr(dev, where)) == NULL)
  95. return PCIBIOS_DEVICE_NOT_FOUND;
  96. *value = le16_to_cpu(*pci_addr);
  97. return PCIBIOS_SUCCESSFUL;
  98. }
  99. static int hades_read_config_dword(struct pci_dev *dev, int where, u32 *value)
  100. {
  101. volatile unsigned int *pci_addr;
  102. unsigned char header_type;
  103. int result;
  104. *value = 0xffffffff;
  105. if (where & 0x3)
  106. return PCIBIOS_BAD_REGISTER_NUMBER;
  107. if ((pci_addr = (unsigned int *) mk_conf_addr(dev, where)) == NULL)
  108. return PCIBIOS_DEVICE_NOT_FOUND;
  109. *value = le32_to_cpu(*pci_addr);
  110. /*
  111. * Check if the value is an address on the bus. If true, add the
  112. * base address of the PCI memory or PCI I/O area on the Hades.
  113. */
  114. if ((result = hades_read_config_byte(dev, PCI_HEADER_TYPE,
  115. &header_type)) != PCIBIOS_SUCCESSFUL)
  116. return result;
  117. if (((where >= PCI_BASE_ADDRESS_0) && (where <= PCI_BASE_ADDRESS_1)) ||
  118. ((header_type != PCI_HEADER_TYPE_BRIDGE) && ((where >= PCI_BASE_ADDRESS_2) &&
  119. (where <= PCI_BASE_ADDRESS_5))))
  120. {
  121. if ((*value & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
  122. {
  123. /*
  124. * Base address register that contains an I/O address. If the
  125. * address is valid on the Hades (0 <= *value < HADES_VIRT_IO_SIZE),
  126. * add 'pci_io_base_virt' to the value.
  127. */
  128. if (*value < HADES_VIRT_IO_SIZE)
  129. *value += pci_io_base_virt;
  130. }
  131. else
  132. {
  133. /*
  134. * Base address register that contains an memory address. If the
  135. * address is valid on the Hades (0 <= *value < HADES_MEM_SIZE),
  136. * add HADES_MEM_BASE to the value.
  137. */
  138. if (*value == 0)
  139. {
  140. /*
  141. * Base address is 0. Test if this base
  142. * address register is used.
  143. */
  144. *pci_addr = 0xffffffff;
  145. if (*pci_addr != 0)
  146. {
  147. *pci_addr = *value;
  148. if (*value < HADES_MEM_SIZE)
  149. *value += HADES_MEM_BASE;
  150. }
  151. }
  152. else
  153. {
  154. if (*value < HADES_MEM_SIZE)
  155. *value += HADES_MEM_BASE;
  156. }
  157. }
  158. }
  159. return PCIBIOS_SUCCESSFUL;
  160. }
  161. static int hades_write_config_byte(struct pci_dev *dev, int where, u8 value)
  162. {
  163. volatile unsigned char *pci_addr;
  164. if ((pci_addr = (unsigned char *) mk_conf_addr(dev, where)) == NULL)
  165. return PCIBIOS_DEVICE_NOT_FOUND;
  166. *pci_addr = value;
  167. return PCIBIOS_SUCCESSFUL;
  168. }
  169. static int hades_write_config_word(struct pci_dev *dev, int where, u16 value)
  170. {
  171. volatile unsigned short *pci_addr;
  172. if ((pci_addr = (unsigned short *) mk_conf_addr(dev, where)) == NULL)
  173. return PCIBIOS_DEVICE_NOT_FOUND;
  174. *pci_addr = cpu_to_le16(value);
  175. return PCIBIOS_SUCCESSFUL;
  176. }
  177. static int hades_write_config_dword(struct pci_dev *dev, int where, u32 value)
  178. {
  179. volatile unsigned int *pci_addr;
  180. unsigned char header_type;
  181. int result;
  182. if ((pci_addr = (unsigned int *) mk_conf_addr(dev, where)) == NULL)
  183. return PCIBIOS_DEVICE_NOT_FOUND;
  184. /*
  185. * Check if the value is an address on the bus. If true, subtract the
  186. * base address of the PCI memory or PCI I/O area on the Hades.
  187. */
  188. if ((result = hades_read_config_byte(dev, PCI_HEADER_TYPE,
  189. &header_type)) != PCIBIOS_SUCCESSFUL)
  190. return result;
  191. if (((where >= PCI_BASE_ADDRESS_0) && (where <= PCI_BASE_ADDRESS_1)) ||
  192. ((header_type != PCI_HEADER_TYPE_BRIDGE) && ((where >= PCI_BASE_ADDRESS_2) &&
  193. (where <= PCI_BASE_ADDRESS_5))))
  194. {
  195. if ((value & PCI_BASE_ADDRESS_SPACE) ==
  196. PCI_BASE_ADDRESS_SPACE_IO)
  197. {
  198. /*
  199. * I/O address. Check if the address is valid address on
  200. * the Hades (pci_io_base_virt <= value < pci_io_base_virt +
  201. * HADES_VIRT_IO_SIZE) or if the value is 0xffffffff. If not
  202. * true do not write the base address register. If it is a
  203. * valid base address subtract 'pci_io_base_virt' from the value.
  204. */
  205. if ((value >= pci_io_base_virt) && (value < (pci_io_base_virt +
  206. HADES_VIRT_IO_SIZE)))
  207. value -= pci_io_base_virt;
  208. else
  209. {
  210. if (value != 0xffffffff)
  211. return PCIBIOS_SET_FAILED;
  212. }
  213. }
  214. else
  215. {
  216. /*
  217. * Memory address. Check if the address is valid address on
  218. * the Hades (HADES_MEM_BASE <= value < HADES_MEM_BASE + HADES_MEM_SIZE) or
  219. * if the value is 0xffffffff. If not true do not write
  220. * the base address register. If it is a valid base address
  221. * subtract HADES_MEM_BASE from the value.
  222. */
  223. if ((value >= HADES_MEM_BASE) && (value < (HADES_MEM_BASE + HADES_MEM_SIZE)))
  224. value -= HADES_MEM_BASE;
  225. else
  226. {
  227. if (value != 0xffffffff)
  228. return PCIBIOS_SET_FAILED;
  229. }
  230. }
  231. }
  232. *pci_addr = cpu_to_le32(value);
  233. return PCIBIOS_SUCCESSFUL;
  234. }
  235. /*
  236. * static inline void hades_fixup(void)
  237. *
  238. * Assign IRQ numbers as used by Linux to the interrupt pins
  239. * of the PCI cards.
  240. */
  241. static void __init hades_fixup(int pci_modify)
  242. {
  243. char irq_tab[4] = {
  244. [0] = IRQ_TT_MFP_IO0, /* Slot 0. */
  245. [1] = IRQ_TT_MFP_IO1, /* Slot 1. */
  246. [2] = IRQ_TT_MFP_SCC, /* Slot 2. */
  247. [3] = IRQ_TT_MFP_SCSIDMA /* Slot 3. */
  248. };
  249. struct pci_dev *dev = NULL;
  250. unsigned char slot;
  251. /*
  252. * Go through all devices, fixing up irqs as we see fit:
  253. */
  254. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL)
  255. {
  256. if (dev->class >> 16 != PCI_BASE_CLASS_BRIDGE)
  257. {
  258. slot = PCI_SLOT(dev->devfn); /* Determine slot number. */
  259. dev->irq = irq_tab[slot];
  260. if (pci_modify)
  261. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
  262. }
  263. }
  264. }
  265. /*
  266. * static void hades_conf_device(struct pci_dev *dev)
  267. *
  268. * Machine dependent Configure the given device.
  269. *
  270. * Parameters:
  271. *
  272. * dev - the pci device.
  273. */
  274. static void __init hades_conf_device(struct pci_dev *dev)
  275. {
  276. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 0);
  277. }
  278. static struct pci_ops hades_pci_ops = {
  279. .read_byte = hades_read_config_byte,
  280. .read_word = hades_read_config_word,
  281. .read_dword = hades_read_config_dword,
  282. .write_byte = hades_write_config_byte,
  283. .write_word = hades_write_config_word,
  284. .write_dword = hades_write_config_dword
  285. };
  286. /*
  287. * struct pci_bus_info *init_hades_pci(void)
  288. *
  289. * Machine specific initialisation:
  290. *
  291. * - Allocate and initialise a 'pci_bus_info' structure
  292. * - Initialise hardware
  293. *
  294. * Result: pointer to 'pci_bus_info' structure.
  295. */
  296. struct pci_bus_info * __init init_hades_pci(void)
  297. {
  298. struct pci_bus_info *bus;
  299. int i;
  300. /*
  301. * Remap I/O and configuration space.
  302. */
  303. pci_io_base_virt = (unsigned long) ioremap(HADES_IO_BASE, HADES_VIRT_IO_SIZE);
  304. for (i = 0; i < N_SLOTS; i++)
  305. pci_conf_base_virt[i] = (unsigned long) ioremap(pci_conf_base_phys[i], 0x10000);
  306. /*
  307. * Allocate memory for bus info structure.
  308. */
  309. bus = kmalloc(sizeof(struct pci_bus_info), GFP_KERNEL);
  310. if (!bus)
  311. return NULL;
  312. memset(bus, 0, sizeof(struct pci_bus_info));
  313. /*
  314. * Claim resources. The m68k has no separate I/O space, both
  315. * PCI memory space and PCI I/O space are in memory space. Therefore
  316. * the I/O resources are requested in memory space as well.
  317. */
  318. if (request_resource(&iomem_resource, &config_space) != 0)
  319. {
  320. kfree(bus);
  321. return NULL;
  322. }
  323. if (request_resource(&iomem_resource, &io_space) != 0)
  324. {
  325. release_resource(&config_space);
  326. kfree(bus);
  327. return NULL;
  328. }
  329. bus->mem_space.start = HADES_MEM_BASE;
  330. bus->mem_space.end = HADES_MEM_BASE + HADES_MEM_SIZE - 1;
  331. bus->mem_space.name = pci_mem_name;
  332. #if 1
  333. if (request_resource(&iomem_resource, &bus->mem_space) != 0)
  334. {
  335. release_resource(&io_space);
  336. release_resource(&config_space);
  337. kfree(bus);
  338. return NULL;
  339. }
  340. #endif
  341. bus->io_space.start = pci_io_base_virt;
  342. bus->io_space.end = pci_io_base_virt + HADES_VIRT_IO_SIZE - 1;
  343. bus->io_space.name = pci_io_name;
  344. #if 1
  345. if (request_resource(&ioport_resource, &bus->io_space) != 0)
  346. {
  347. release_resource(&bus->mem_space);
  348. release_resource(&io_space);
  349. release_resource(&config_space);
  350. kfree(bus);
  351. return NULL;
  352. }
  353. #endif
  354. /*
  355. * Set hardware dependent functions.
  356. */
  357. bus->m68k_pci_ops = &hades_pci_ops;
  358. bus->fixup = hades_fixup;
  359. bus->conf_device = hades_conf_device;
  360. /*
  361. * Select high to low edge for PCI interrupts.
  362. */
  363. tt_mfp.active_edge &= ~0x27;
  364. return bus;
  365. }
  366. #endif