hades-pci.c 11 KB

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