cardbus.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:11k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*======================================================================
  2.   
  3.     Cardbus device configuration
  4.     
  5.     cardbus.c 1.63 1999/11/08 20:47:02
  6.     The contents of this file are subject to the Mozilla Public
  7.     License Version 1.1 (the "License"); you may not use this file
  8.     except in compliance with the License. You may obtain a copy of
  9.     the License at http://www.mozilla.org/MPL/
  10.     Software distributed under the License is distributed on an "AS
  11.     IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  12.     implied. See the License for the specific language governing
  13.     rights and limitations under the License.
  14.     The initial developer of the original code is David A. Hinds
  15.     <dhinds@pcmcia.sourceforge.org>.  Portions created by David A. Hinds
  16.     are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
  17.     Alternatively, the contents of this file may be used under the
  18.     terms of the GNU General Public License version 2 (the "GPL"), in which
  19.     case the provisions of the GPL are applicable instead of the
  20.     above.  If you wish to allow the use of your version of this file
  21.     only under the terms of the GPL and not to allow others to use
  22.     your version of this file under the MPL, indicate your decision
  23.     by deleting the provisions above and replace them with the notice
  24.     and other provisions required by the GPL.  If you do not delete
  25.     the provisions above, a recipient may use your version of this
  26.     file under either the MPL or the GPL.
  27.     
  28.     These routines handle allocating resources for Cardbus cards, as
  29.     well as setting up and shutting down Cardbus sockets.  They are
  30.     called from cs.c in response to Request/ReleaseConfiguration and
  31.     Request/ReleaseIO calls.
  32. ======================================================================*/
  33. /*
  34.  * This file is going away.  Cardbus handling has been re-written to be
  35.  * more of a PCI bridge thing, and the PCI code basically does all the
  36.  * resource handling. This has wrappers to make the rest of the PCMCIA
  37.  * subsystem not notice that it's not here any more.
  38.  *
  39.  * Linus, Jan 2000
  40.  */
  41. #define __NO_VERSION__
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/mm.h>
  47. #include <linux/pci.h>
  48. #include <linux/ioport.h>
  49. #include <asm/irq.h>
  50. #include <asm/io.h>
  51. #define IN_CARD_SERVICES
  52. #include <pcmcia/version.h>
  53. #include <pcmcia/cs_types.h>
  54. #include <pcmcia/ss.h>
  55. #include <pcmcia/cs.h>
  56. #include <pcmcia/bulkmem.h>
  57. #include <pcmcia/cistpl.h>
  58. #include "cs_internal.h"
  59. #ifdef PCMCIA_DEBUG
  60. static int pc_debug = PCMCIA_DEBUG;
  61. #endif
  62. /*====================================================================*/
  63. #define FIND_FIRST_BIT(n) ((n) - ((n) & ((n)-1)))
  64. #define pci_readb pci_read_config_byte
  65. #define pci_writeb pci_write_config_byte
  66. #define pci_readw pci_read_config_word
  67. #define pci_writew pci_write_config_word
  68. #define pci_readl pci_read_config_dword
  69. #define pci_writel pci_write_config_dword
  70. /* Offsets in the Expansion ROM Image Header */
  71. #define ROM_SIGNATURE 0x0000 /* 2 bytes */
  72. #define ROM_DATA_PTR 0x0018 /* 2 bytes */
  73. /* Offsets in the CardBus PC Card Data Structure */
  74. #define PCDATA_SIGNATURE 0x0000 /* 4 bytes */
  75. #define PCDATA_VPD_PTR 0x0008 /* 2 bytes */
  76. #define PCDATA_LENGTH 0x000a /* 2 bytes */
  77. #define PCDATA_REVISION 0x000c
  78. #define PCDATA_IMAGE_SZ 0x0010 /* 2 bytes */
  79. #define PCDATA_ROM_LEVEL 0x0012 /* 2 bytes */
  80. #define PCDATA_CODE_TYPE 0x0014
  81. #define PCDATA_INDICATOR 0x0015
  82. typedef struct cb_config_t {
  83. struct pci_dev dev;
  84. } cb_config_t;
  85. /*=====================================================================
  86.     Expansion ROM's have a special layout, and pointers specify an
  87.     image number and an offset within that image.  xlate_rom_addr()
  88.     converts an image/offset address to an absolute offset from the
  89.     ROM's base address.
  90.     
  91. =====================================================================*/
  92. static u_int xlate_rom_addr(u_char * b, u_int addr)
  93. {
  94. u_int img = 0, ofs = 0, sz;
  95. u_short data;
  96. while ((readb(b) == 0x55) && (readb(b + 1) == 0xaa)) {
  97. if (img == (addr >> 28))
  98. return (addr & 0x0fffffff) + ofs;
  99. data = readb(b + ROM_DATA_PTR) + (readb(b + ROM_DATA_PTR + 1) << 8);
  100. sz = 512 * (readb(b + data + PCDATA_IMAGE_SZ) +
  101.     (readb(b + data + PCDATA_IMAGE_SZ + 1) << 8));
  102. if ((sz == 0) || (readb(b + data + PCDATA_INDICATOR) & 0x80))
  103. break;
  104. b += sz;
  105. ofs += sz;
  106. img++;
  107. }
  108. return 0;
  109. }
  110. /*=====================================================================
  111.     These are similar to setup_cis_mem and release_cis_mem for 16-bit
  112.     cards.  The "result" that is used externally is the cb_cis_virt
  113.     pointer in the socket_info_t structure.
  114.     
  115. =====================================================================*/
  116. void cb_release_cis_mem(socket_info_t * s)
  117. {
  118. if (s->cb_cis_virt) {
  119. DEBUG(1, "cs: cb_release_cis_mem()n");
  120. iounmap(s->cb_cis_virt);
  121. s->cb_cis_virt = NULL;
  122. s->cb_cis_res = 0;
  123. }
  124. }
  125. static int cb_setup_cis_mem(socket_info_t * s, struct pci_dev *dev, struct resource *res)
  126. {
  127. unsigned int start, size;
  128. if (res == s->cb_cis_res)
  129. return 0;
  130. if (s->cb_cis_res)
  131. cb_release_cis_mem(s);
  132. start = res->start;
  133. size = res->end - start + 1;
  134. s->cb_cis_virt = ioremap(start, size);
  135. if (!s->cb_cis_virt)
  136. return -1;
  137. s->cb_cis_res = res;
  138. return 0;
  139. }
  140. /*=====================================================================
  141.     This is used by the CIS processing code to read CIS information
  142.     from a CardBus device.
  143.     
  144. =====================================================================*/
  145. void read_cb_mem(socket_info_t * s, u_char fn, int space,
  146.  u_int addr, u_int len, void *ptr)
  147. {
  148. struct pci_dev *dev;
  149. struct resource *res;
  150. DEBUG(3, "cs: read_cb_mem(%d, %#x, %u)n", space, addr, len);
  151. if (!s->cb_config)
  152. goto fail;
  153. dev = &s->cb_config[fn].dev;
  154. /* Config space? */
  155. if (space == 0) {
  156. if (addr + len > 0x100)
  157. goto fail;
  158. for (; len; addr++, ptr++, len--)
  159. pci_readb(dev, addr, (u_char *) ptr);
  160. return;
  161. }
  162. res = dev->resource + space - 1;
  163. if (!res->flags)
  164. goto fail;
  165. if (cb_setup_cis_mem(s, dev, res) != 0)
  166. goto fail;
  167. if (space == 7) {
  168. addr = xlate_rom_addr(s->cb_cis_virt, addr);
  169. if (addr == 0)
  170. goto fail;
  171. }
  172. if (addr + len > res->end - res->start)
  173. goto fail;
  174. memcpy_fromio(ptr, s->cb_cis_virt + addr, len);
  175. return;
  176. fail:
  177. memset(ptr, 0xff, len);
  178. return;
  179. }
  180. /*=====================================================================
  181.     cb_alloc() and cb_free() allocate and free the kernel data
  182.     structures for a Cardbus device, and handle the lowest level PCI
  183.     device setup issues.
  184.     
  185. =====================================================================*/
  186. int cb_alloc(socket_info_t * s)
  187. {
  188. struct pci_bus *bus;
  189. struct pci_dev tmp;
  190. u_short vend, v, dev;
  191. u_char i, hdr, fn;
  192. cb_config_t *c;
  193. int irq;
  194. bus = s->cap.cb_dev->subordinate;
  195. memset(&tmp, 0, sizeof(tmp));
  196. tmp.bus = bus;
  197. tmp.sysdata = bus->sysdata;
  198. tmp.devfn = 0;
  199. pci_readw(&tmp, PCI_VENDOR_ID, &vend);
  200. pci_readw(&tmp, PCI_DEVICE_ID, &dev);
  201. printk(KERN_INFO "cs: cb_alloc(bus %d): vendor 0x%04x, "
  202.        "device 0x%04xn", bus->number, vend, dev);
  203. pci_readb(&tmp, PCI_HEADER_TYPE, &hdr);
  204. fn = 1;
  205. if (hdr & 0x80) {
  206. do {
  207. tmp.devfn = fn;
  208. if (pci_readw(&tmp, PCI_VENDOR_ID, &v) || !v || v == 0xffff)
  209. break;
  210. fn++;
  211. } while (fn < 8);
  212. }
  213. s->functions = fn;
  214. c = kmalloc(fn * sizeof(struct cb_config_t), GFP_ATOMIC);
  215. if (!c)
  216. return CS_OUT_OF_RESOURCE;
  217. memset(c, 0, fn * sizeof(struct cb_config_t));
  218. irq = s->cap.pci_irq;
  219. for (i = 0; i < fn; i++) {
  220. struct pci_dev *dev = &c[i].dev;
  221. u8 irq_pin;
  222. int r;
  223. dev->bus = bus;
  224. dev->sysdata = bus->sysdata;
  225. dev->devfn = i;
  226. dev->vendor = vend;
  227. pci_readw(dev, PCI_DEVICE_ID, &dev->device);
  228. dev->hdr_type = hdr & 0x7f;
  229. pci_setup_device(dev);
  230. /* FIXME: Do we need to enable the expansion ROM? */
  231. for (r = 0; r < 7; r++) {
  232. struct resource *res = dev->resource + r;
  233. if (res->flags)
  234. pci_assign_resource(dev, r);
  235. }
  236. /* Does this function have an interrupt at all? */
  237. pci_readb(dev, PCI_INTERRUPT_PIN, &irq_pin);
  238. if (irq_pin) {
  239. dev->irq = irq;
  240. pci_writeb(dev, PCI_INTERRUPT_LINE, irq);
  241. }
  242. pci_enable_device(dev); /* XXX check return */
  243. pci_insert_device(dev, bus);
  244. }
  245. s->cb_config = c;
  246. s->irq.AssignedIRQ = irq;
  247. return CS_SUCCESS;
  248. }
  249. void cb_free(socket_info_t * s)
  250. {
  251. cb_config_t *c = s->cb_config;
  252. if (c) {
  253. int i;
  254. s->cb_config = NULL;
  255. for (i = 0 ; i < s->functions ; i++)
  256. pci_remove_device(&c[i].dev);
  257. kfree(c);
  258. printk(KERN_INFO "cs: cb_free(bus %d)n", s->cap.cb_dev->subordinate->number);
  259. }
  260. }
  261. /*=====================================================================
  262.     cb_config() has the job of allocating all system resources that
  263.     a Cardbus card requires.  Rather than using the CIS (which seems
  264.     to not always be present), it treats the card as an ordinary PCI
  265.     device, and probes the base address registers to determine each
  266.     function's IO and memory space needs.
  267.     It is called from the RequestIO card service.
  268.     
  269. ======================================================================*/
  270. int cb_config(socket_info_t * s)
  271. {
  272. return CS_SUCCESS;
  273. }
  274. /*======================================================================
  275.     cb_release() releases all the system resources (IO and memory
  276.     space, and interrupt) committed for a Cardbus card by a prior call
  277.     to cb_config().
  278.     It is called from the ReleaseIO() service.
  279.     
  280. ======================================================================*/
  281. void cb_release(socket_info_t * s)
  282. {
  283. DEBUG(0, "cs: cb_release(bus %d)n", s->cap.cb_dev->subordinate->number);
  284. }
  285. /*=====================================================================
  286.     cb_enable() has the job of configuring a socket for a Cardbus
  287.     card, and initializing the card's PCI configuration registers.
  288.     It first sets up the Cardbus bridge windows, for IO and memory
  289.     accesses.  Then, it initializes each card function's base address
  290.     registers, interrupt line register, and command register.
  291.     It is called as part of the RequestConfiguration card service.
  292.     It should be called after a previous call to cb_config() (via the
  293.     RequestIO service).
  294.     
  295. ======================================================================*/
  296. void cb_enable(socket_info_t * s)
  297. {
  298. struct pci_dev *dev;
  299. u_char i;
  300. DEBUG(0, "cs: cb_enable(bus %d)n", s->cap.cb_dev->subordinate->number);
  301. /* Configure bridge */
  302. cb_release_cis_mem(s);
  303. /* Set up PCI interrupt and command registers */
  304. for (i = 0; i < s->functions; i++) {
  305. dev = &s->cb_config[i].dev;
  306. pci_writeb(dev, PCI_COMMAND, PCI_COMMAND_MASTER |
  307.    PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  308. pci_writeb(dev, PCI_CACHE_LINE_SIZE, L1_CACHE_BYTES / 4);
  309. }
  310. if (s->irq.AssignedIRQ) {
  311. for (i = 0; i < s->functions; i++) {
  312. dev = &s->cb_config[i].dev;
  313. pci_writeb(dev, PCI_INTERRUPT_LINE, s->irq.AssignedIRQ);
  314. }
  315. s->socket.io_irq = s->irq.AssignedIRQ;
  316. s->ss_entry->set_socket(s->sock, &s->socket);
  317. }
  318. }
  319. /*======================================================================
  320.     cb_disable() unconfigures a Cardbus card previously set up by
  321.     cb_enable().
  322.     It is called from the ReleaseConfiguration service.
  323.     
  324. ======================================================================*/
  325. void cb_disable(socket_info_t * s)
  326. {
  327. DEBUG(0, "cs: cb_disable(bus %d)n", s->cap.cb_dev->subordinate->number);
  328. /* Turn off bridge windows */
  329. cb_release_cis_mem(s);
  330. }