cardbus.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <linux/io.h>
  28. #include <asm/irq.h>
  29. #include <pcmcia/cs_types.h>
  30. #include <pcmcia/ss.h>
  31. #include <pcmcia/cs.h>
  32. #include <pcmcia/cistpl.h>
  33. #include "cs_internal.h"
  34. /*====================================================================*/
  35. /* Offsets in the Expansion ROM Image Header */
  36. #define ROM_SIGNATURE 0x0000 /* 2 bytes */
  37. #define ROM_DATA_PTR 0x0018 /* 2 bytes */
  38. /* Offsets in the CardBus PC Card Data Structure */
  39. #define PCDATA_SIGNATURE 0x0000 /* 4 bytes */
  40. #define PCDATA_VPD_PTR 0x0008 /* 2 bytes */
  41. #define PCDATA_LENGTH 0x000a /* 2 bytes */
  42. #define PCDATA_REVISION 0x000c
  43. #define PCDATA_IMAGE_SZ 0x0010 /* 2 bytes */
  44. #define PCDATA_ROM_LEVEL 0x0012 /* 2 bytes */
  45. #define PCDATA_CODE_TYPE 0x0014
  46. #define PCDATA_INDICATOR 0x0015
  47. /*=====================================================================
  48. Expansion ROM's have a special layout, and pointers specify an
  49. image number and an offset within that image. xlate_rom_addr()
  50. converts an image/offset address to an absolute offset from the
  51. ROM's base address.
  52. =====================================================================*/
  53. static u_int xlate_rom_addr(void __iomem *b, u_int addr)
  54. {
  55. u_int img = 0, ofs = 0, sz;
  56. u_short data;
  57. while ((readb(b) == 0x55) && (readb(b + 1) == 0xaa)) {
  58. if (img == (addr >> 28))
  59. return (addr & 0x0fffffff) + ofs;
  60. data = readb(b + ROM_DATA_PTR) + (readb(b + ROM_DATA_PTR + 1) << 8);
  61. sz = 512 * (readb(b + data + PCDATA_IMAGE_SZ) +
  62. (readb(b + data + PCDATA_IMAGE_SZ + 1) << 8));
  63. if ((sz == 0) || (readb(b + data + PCDATA_INDICATOR) & 0x80))
  64. break;
  65. b += sz;
  66. ofs += sz;
  67. img++;
  68. }
  69. return 0;
  70. }
  71. /*=====================================================================
  72. These are similar to setup_cis_mem and release_cis_mem for 16-bit
  73. cards. The "result" that is used externally is the cb_cis_virt
  74. pointer in the struct pcmcia_socket structure.
  75. =====================================================================*/
  76. static void cb_release_cis_mem(struct pcmcia_socket *s)
  77. {
  78. if (s->cb_cis_virt) {
  79. dev_dbg(&s->dev, "cb_release_cis_mem()\n");
  80. iounmap(s->cb_cis_virt);
  81. s->cb_cis_virt = NULL;
  82. s->cb_cis_res = NULL;
  83. }
  84. }
  85. static int cb_setup_cis_mem(struct pcmcia_socket *s, struct resource *res)
  86. {
  87. unsigned int start, size;
  88. if (res == s->cb_cis_res)
  89. return 0;
  90. if (s->cb_cis_res)
  91. cb_release_cis_mem(s);
  92. start = res->start;
  93. size = res->end - start + 1;
  94. s->cb_cis_virt = ioremap(start, size);
  95. if (!s->cb_cis_virt)
  96. return -1;
  97. s->cb_cis_res = res;
  98. return 0;
  99. }
  100. /*=====================================================================
  101. This is used by the CIS processing code to read CIS information
  102. from a CardBus device.
  103. =====================================================================*/
  104. int read_cb_mem(struct pcmcia_socket *s, int space, u_int addr, u_int len,
  105. void *ptr)
  106. {
  107. struct pci_dev *dev;
  108. struct resource *res;
  109. dev_dbg(&s->dev, "read_cb_mem(%d, %#x, %u)\n", space, addr, len);
  110. dev = pci_get_slot(s->cb_dev->subordinate, 0);
  111. if (!dev)
  112. goto fail;
  113. /* Config space? */
  114. if (space == 0) {
  115. if (addr + len > 0x100)
  116. goto failput;
  117. for (; len; addr++, ptr++, len--)
  118. pci_read_config_byte(dev, addr, ptr);
  119. return 0;
  120. }
  121. res = dev->resource + space - 1;
  122. pci_dev_put(dev);
  123. if (!res->flags)
  124. goto fail;
  125. if (cb_setup_cis_mem(s, res) != 0)
  126. goto fail;
  127. if (space == 7) {
  128. addr = xlate_rom_addr(s->cb_cis_virt, addr);
  129. if (addr == 0)
  130. goto fail;
  131. }
  132. if (addr + len > res->end - res->start)
  133. goto fail;
  134. memcpy_fromio(ptr, s->cb_cis_virt + addr, len);
  135. return 0;
  136. failput:
  137. pci_dev_put(dev);
  138. fail:
  139. memset(ptr, 0xff, len);
  140. return -1;
  141. }
  142. /*=====================================================================
  143. cb_alloc() and cb_free() allocate and free the kernel data
  144. structures for a Cardbus device, and handle the lowest level PCI
  145. device setup issues.
  146. =====================================================================*/
  147. static void cardbus_config_irq_and_cls(struct pci_bus *bus, int irq)
  148. {
  149. struct pci_dev *dev;
  150. list_for_each_entry(dev, &bus->devices, bus_list) {
  151. u8 irq_pin;
  152. /*
  153. * Since there is only one interrupt available to
  154. * CardBus devices, all devices downstream of this
  155. * device must be using this IRQ.
  156. */
  157. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq_pin);
  158. if (irq_pin) {
  159. dev->irq = irq;
  160. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
  161. }
  162. /*
  163. * Some controllers transfer very slowly with 0 CLS.
  164. * Configure it. This may fail as CLS configuration
  165. * is mandatory only for MWI.
  166. */
  167. pci_set_cacheline_size(dev);
  168. if (dev->subordinate)
  169. cardbus_config_irq_and_cls(dev->subordinate, irq);
  170. }
  171. }
  172. int __ref cb_alloc(struct pcmcia_socket *s)
  173. {
  174. struct pci_bus *bus = s->cb_dev->subordinate;
  175. struct pci_dev *dev;
  176. unsigned int max, pass;
  177. s->functions = pci_scan_slot(bus, PCI_DEVFN(0, 0));
  178. pci_fixup_cardbus(bus);
  179. max = bus->secondary;
  180. for (pass = 0; pass < 2; pass++)
  181. list_for_each_entry(dev, &bus->devices, bus_list)
  182. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  183. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  184. max = pci_scan_bridge(bus, dev, max, pass);
  185. /*
  186. * Size all resources below the CardBus controller.
  187. */
  188. pci_bus_size_bridges(bus);
  189. pci_bus_assign_resources(bus);
  190. cardbus_config_irq_and_cls(bus, s->pci_irq);
  191. /* socket specific tune function */
  192. if (s->tune_bridge)
  193. s->tune_bridge(s, bus);
  194. pci_enable_bridges(bus);
  195. pci_bus_add_devices(bus);
  196. s->irq.AssignedIRQ = s->pci_irq;
  197. return 0;
  198. }
  199. void cb_free(struct pcmcia_socket *s)
  200. {
  201. struct pci_dev *bridge = s->cb_dev;
  202. cb_release_cis_mem(s);
  203. if (bridge)
  204. pci_remove_behind_bridge(bridge);
  205. }