cardbus.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * cardbus.c -- 16-bit PCMCIA core support
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * The initial developer of the original code is David A. Hinds
  9. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  10. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  11. *
  12. * (C) 1999 David A. Hinds
  13. */
  14. /*
  15. * Cardbus handling has been re-written to be more of a PCI bridge thing,
  16. * and the PCI code basically does all the resource handling.
  17. *
  18. * Linus, Jan 2000
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/string.h>
  23. #include <linux/slab.h>
  24. #include <linux/mm.h>
  25. #include <linux/pci.h>
  26. #include <linux/ioport.h>
  27. #include <asm/irq.h>
  28. #include <asm/io.h>
  29. #define IN_CARD_SERVICES
  30. #include <pcmcia/cs_types.h>
  31. #include <pcmcia/ss.h>
  32. #include <pcmcia/cs.h>
  33. #include <pcmcia/bulkmem.h>
  34. #include <pcmcia/cistpl.h>
  35. #include "cs_internal.h"
  36. /*====================================================================*/
  37. #define FIND_FIRST_BIT(n) ((n) - ((n) & ((n)-1)))
  38. /* Offsets in the Expansion ROM Image Header */
  39. #define ROM_SIGNATURE 0x0000 /* 2 bytes */
  40. #define ROM_DATA_PTR 0x0018 /* 2 bytes */
  41. /* Offsets in the CardBus PC Card Data Structure */
  42. #define PCDATA_SIGNATURE 0x0000 /* 4 bytes */
  43. #define PCDATA_VPD_PTR 0x0008 /* 2 bytes */
  44. #define PCDATA_LENGTH 0x000a /* 2 bytes */
  45. #define PCDATA_REVISION 0x000c
  46. #define PCDATA_IMAGE_SZ 0x0010 /* 2 bytes */
  47. #define PCDATA_ROM_LEVEL 0x0012 /* 2 bytes */
  48. #define PCDATA_CODE_TYPE 0x0014
  49. #define PCDATA_INDICATOR 0x0015
  50. /*=====================================================================
  51. Expansion ROM's have a special layout, and pointers specify an
  52. image number and an offset within that image. xlate_rom_addr()
  53. converts an image/offset address to an absolute offset from the
  54. ROM's base address.
  55. =====================================================================*/
  56. static u_int xlate_rom_addr(void __iomem *b, u_int addr)
  57. {
  58. u_int img = 0, ofs = 0, sz;
  59. u_short data;
  60. while ((readb(b) == 0x55) && (readb(b + 1) == 0xaa)) {
  61. if (img == (addr >> 28))
  62. return (addr & 0x0fffffff) + ofs;
  63. data = readb(b + ROM_DATA_PTR) + (readb(b + ROM_DATA_PTR + 1) << 8);
  64. sz = 512 * (readb(b + data + PCDATA_IMAGE_SZ) +
  65. (readb(b + data + PCDATA_IMAGE_SZ + 1) << 8));
  66. if ((sz == 0) || (readb(b + data + PCDATA_INDICATOR) & 0x80))
  67. break;
  68. b += sz;
  69. ofs += sz;
  70. img++;
  71. }
  72. return 0;
  73. }
  74. /*=====================================================================
  75. These are similar to setup_cis_mem and release_cis_mem for 16-bit
  76. cards. The "result" that is used externally is the cb_cis_virt
  77. pointer in the struct pcmcia_socket structure.
  78. =====================================================================*/
  79. static void cb_release_cis_mem(struct pcmcia_socket * s)
  80. {
  81. if (s->cb_cis_virt) {
  82. cs_dbg(s, 1, "cb_release_cis_mem()\n");
  83. iounmap(s->cb_cis_virt);
  84. s->cb_cis_virt = NULL;
  85. s->cb_cis_res = NULL;
  86. }
  87. }
  88. static int cb_setup_cis_mem(struct pcmcia_socket * s, struct resource *res)
  89. {
  90. unsigned int start, size;
  91. if (res == s->cb_cis_res)
  92. return 0;
  93. if (s->cb_cis_res)
  94. cb_release_cis_mem(s);
  95. start = res->start;
  96. size = res->end - start + 1;
  97. s->cb_cis_virt = ioremap(start, size);
  98. if (!s->cb_cis_virt)
  99. return -1;
  100. s->cb_cis_res = res;
  101. return 0;
  102. }
  103. /*=====================================================================
  104. This is used by the CIS processing code to read CIS information
  105. from a CardBus device.
  106. =====================================================================*/
  107. int read_cb_mem(struct pcmcia_socket * s, int space, u_int addr, u_int len, void *ptr)
  108. {
  109. struct pci_dev *dev;
  110. struct resource *res;
  111. cs_dbg(s, 3, "read_cb_mem(%d, %#x, %u)\n", space, addr, len);
  112. dev = pci_find_slot(s->cb_dev->subordinate->number, 0);
  113. if (!dev)
  114. goto fail;
  115. /* Config space? */
  116. if (space == 0) {
  117. if (addr + len > 0x100)
  118. goto fail;
  119. for (; len; addr++, ptr++, len--)
  120. pci_read_config_byte(dev, addr, ptr);
  121. return 0;
  122. }
  123. res = dev->resource + space - 1;
  124. if (!res->flags)
  125. goto fail;
  126. if (cb_setup_cis_mem(s, res) != 0)
  127. goto fail;
  128. if (space == 7) {
  129. addr = xlate_rom_addr(s->cb_cis_virt, addr);
  130. if (addr == 0)
  131. goto fail;
  132. }
  133. if (addr + len > res->end - res->start)
  134. goto fail;
  135. memcpy_fromio(ptr, s->cb_cis_virt + addr, len);
  136. return 0;
  137. fail:
  138. memset(ptr, 0xff, len);
  139. return -1;
  140. }
  141. /*=====================================================================
  142. cb_alloc() and cb_free() allocate and free the kernel data
  143. structures for a Cardbus device, and handle the lowest level PCI
  144. device setup issues.
  145. =====================================================================*/
  146. /*
  147. * Since there is only one interrupt available to CardBus
  148. * devices, all devices downstream of this device must
  149. * be using this IRQ.
  150. */
  151. static void cardbus_assign_irqs(struct pci_bus *bus, int irq)
  152. {
  153. struct pci_dev *dev;
  154. list_for_each_entry(dev, &bus->devices, bus_list) {
  155. u8 irq_pin;
  156. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq_pin);
  157. if (irq_pin) {
  158. dev->irq = irq;
  159. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
  160. }
  161. if (dev->subordinate)
  162. cardbus_assign_irqs(dev->subordinate, irq);
  163. }
  164. }
  165. int cb_alloc(struct pcmcia_socket * s)
  166. {
  167. struct pci_bus *bus = s->cb_dev->subordinate;
  168. struct pci_dev *dev;
  169. unsigned int max, pass;
  170. s->functions = pci_scan_slot(bus, PCI_DEVFN(0, 0));
  171. // pcibios_fixup_bus(bus);
  172. max = bus->secondary;
  173. for (pass = 0; pass < 2; pass++)
  174. list_for_each_entry(dev, &bus->devices, bus_list)
  175. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  176. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  177. max = pci_scan_bridge(bus, dev, max, pass);
  178. /*
  179. * Size all resources below the CardBus controller.
  180. */
  181. pci_bus_size_bridges(bus);
  182. pci_bus_assign_resources(bus);
  183. cardbus_assign_irqs(bus, s->pci_irq);
  184. /* socket specific tune function */
  185. if (s->tune_bridge)
  186. s->tune_bridge(s, bus);
  187. pci_enable_bridges(bus);
  188. pci_bus_add_devices(bus);
  189. s->irq.AssignedIRQ = s->pci_irq;
  190. return CS_SUCCESS;
  191. }
  192. void cb_free(struct pcmcia_socket * s)
  193. {
  194. struct pci_dev *bridge = s->cb_dev;
  195. cb_release_cis_mem(s);
  196. if (bridge)
  197. pci_remove_behind_bridge(bridge);
  198. }