cardbus.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /* $OpenBSD: cardbus.c,v 1.50 2015/03/14 03:38:47 jsg Exp $ */
  2. /* $NetBSD: cardbus.c,v 1.24 2000/04/02 19:11:37 mycroft Exp $ */
  3. /*
  4. * Copyright (c) 1997, 1998, 1999 and 2000
  5. * HAYAKAWA Koichi. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  21. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <sys/param.h>
  30. #include <sys/systm.h>
  31. #include <sys/device.h>
  32. #include <sys/malloc.h>
  33. #include <sys/kernel.h>
  34. #include <machine/bus.h>
  35. #include <dev/cardbus/cardbusvar.h>
  36. #include <dev/pci/pcidevs.h>
  37. #include <dev/cardbus/cardbus_exrom.h>
  38. #include <dev/pci/pcivar.h> /* XXX */
  39. #include <dev/pci/pcireg.h> /* XXX */
  40. #include <dev/pcmcia/pcmciareg.h>
  41. #ifdef CARDBUS_DEBUG
  42. #define STATIC
  43. #define DPRINTF(a) printf a
  44. #else
  45. #ifdef DDB
  46. #define STATIC
  47. #else
  48. #define STATIC static
  49. #endif
  50. #define DPRINTF(a)
  51. #endif
  52. STATIC void cardbusattach(struct device *, struct device *, void *);
  53. /* STATIC int cardbusprint(void *, const char *); */
  54. STATIC int cardbusmatch(struct device *, void *, void *);
  55. STATIC int cardbussubmatch(struct device *, void *, void *);
  56. STATIC int cardbusprint(void *, const char *);
  57. typedef void (*tuple_decode_func)(u_int8_t *, int, void *);
  58. STATIC int decode_tuples(u_int8_t *, int, tuple_decode_func, void *);
  59. STATIC void parse_tuple(u_int8_t *, int, void *);
  60. #ifdef CARDBUS_DEBUG
  61. static void print_tuple(u_int8_t *, int, void *);
  62. #endif
  63. STATIC int cardbus_read_tuples(struct cardbus_attach_args *,
  64. pcireg_t, u_int8_t *, size_t);
  65. STATIC void enable_function(struct cardbus_softc *, int, int);
  66. STATIC void disable_function(struct cardbus_softc *, int);
  67. struct cfattach cardbus_ca = {
  68. sizeof(struct cardbus_softc), cardbusmatch, cardbusattach
  69. };
  70. struct cfdriver cardbus_cd = {
  71. NULL, "cardbus", DV_DULL
  72. };
  73. STATIC int
  74. cardbusmatch(struct device *parent, void *match, void *aux)
  75. {
  76. struct cfdata *cf = match;
  77. struct cbslot_attach_args *cba = aux;
  78. if (strcmp(cba->cba_busname, cf->cf_driver->cd_name)) {
  79. DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
  80. cba->cba_busname, cf->cf_driver->cd_name));
  81. return (0);
  82. }
  83. return (1);
  84. }
  85. STATIC void
  86. cardbusattach(struct device *parent, struct device *self, void *aux)
  87. {
  88. struct cardbus_softc *sc = (void *)self;
  89. struct cbslot_attach_args *cba = aux;
  90. int cdstatus;
  91. sc->sc_bus = cba->cba_bus;
  92. sc->sc_device = 0;
  93. sc->sc_intrline = cba->cba_intrline;
  94. sc->sc_cacheline = cba->cba_cacheline;
  95. sc->sc_lattimer = cba->cba_lattimer;
  96. printf(": bus %d device %d", sc->sc_bus, sc->sc_device);
  97. printf(" cacheline 0x%x, lattimer 0x%x\n",
  98. sc->sc_cacheline,sc->sc_lattimer);
  99. sc->sc_iot = cba->cba_iot; /* CardBus I/O space tag */
  100. sc->sc_memt = cba->cba_memt; /* CardBus MEM space tag */
  101. sc->sc_dmat = cba->cba_dmat; /* DMA tag */
  102. sc->sc_cc = cba->cba_cc;
  103. sc->sc_pc = cba->cba_pc;
  104. sc->sc_cf = cba->cba_cf;
  105. sc->sc_rbus_iot = cba->cba_rbus_iot;
  106. sc->sc_rbus_memt = cba->cba_rbus_memt;
  107. cdstatus = 0;
  108. }
  109. STATIC int
  110. cardbus_read_tuples(struct cardbus_attach_args *ca, pcireg_t cis_ptr,
  111. u_int8_t *tuples, size_t len)
  112. {
  113. struct cardbus_softc *sc = ca->ca_ct->ct_sc;
  114. pci_chipset_tag_t pc = ca->ca_pc;
  115. pcitag_t tag = ca->ca_tag;
  116. pcireg_t command;
  117. int found = 0;
  118. int i, j;
  119. int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK;
  120. bus_space_tag_t bar_tag;
  121. bus_space_handle_t bar_memh;
  122. bus_size_t bar_size;
  123. bus_addr_t bar_addr;
  124. int reg;
  125. memset(tuples, 0, len);
  126. cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK;
  127. switch (cardbus_space) {
  128. case CARDBUS_CIS_ASI_TUPLE:
  129. DPRINTF(("%s: reading CIS data from configuration space\n",
  130. sc->sc_dev.dv_xname));
  131. for (i = cis_ptr, j = 0; i < 0xff; i += 4) {
  132. u_int32_t e = pci_conf_read(pc, tag, i);
  133. tuples[j] = 0xff & e;
  134. e >>= 8;
  135. tuples[j + 1] = 0xff & e;
  136. e >>= 8;
  137. tuples[j + 2] = 0xff & e;
  138. e >>= 8;
  139. tuples[j + 3] = 0xff & e;
  140. j += 4;
  141. }
  142. found++;
  143. break;
  144. case CARDBUS_CIS_ASI_BAR0:
  145. case CARDBUS_CIS_ASI_BAR1:
  146. case CARDBUS_CIS_ASI_BAR2:
  147. case CARDBUS_CIS_ASI_BAR3:
  148. case CARDBUS_CIS_ASI_BAR4:
  149. case CARDBUS_CIS_ASI_BAR5:
  150. case CARDBUS_CIS_ASI_ROM:
  151. if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
  152. reg = CARDBUS_ROM_REG;
  153. DPRINTF(("%s: reading CIS data from ROM\n",
  154. sc->sc_dev.dv_xname));
  155. } else {
  156. reg = CARDBUS_BASE0_REG + (cardbus_space - 1) * 4;
  157. DPRINTF(("%s: reading CIS data from BAR%d\n",
  158. sc->sc_dev.dv_xname, cardbus_space - 1));
  159. }
  160. /* XXX zero register so mapreg_map doesn't get confused by old
  161. contents */
  162. pci_conf_write(pc, tag, reg, 0);
  163. if (Cardbus_mapreg_map(ca->ca_ct, reg,
  164. PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
  165. &bar_tag, &bar_memh, &bar_addr, &bar_size)) {
  166. printf("%s: can't map memory\n",
  167. sc->sc_dev.dv_xname);
  168. return (1);
  169. }
  170. if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
  171. pcireg_t exrom;
  172. int save;
  173. struct cardbus_rom_image_head rom_image;
  174. struct cardbus_rom_image *p;
  175. save = splhigh();
  176. /* enable rom address decoder */
  177. exrom = pci_conf_read(pc, tag, reg);
  178. pci_conf_write(pc, tag, reg, exrom | 1);
  179. command = pci_conf_read(pc, tag,
  180. PCI_COMMAND_STATUS_REG);
  181. pci_conf_write(pc, tag,
  182. PCI_COMMAND_STATUS_REG,
  183. command | PCI_COMMAND_MEM_ENABLE);
  184. if (cardbus_read_exrom(ca->ca_memt, bar_memh,
  185. &rom_image))
  186. goto out;
  187. for (p = SIMPLEQ_FIRST(&rom_image); p;
  188. p = SIMPLEQ_NEXT(p, next)) {
  189. if (p->rom_image ==
  190. CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
  191. bus_space_read_region_1(p->romt,
  192. p->romh, CARDBUS_CIS_ADDR(cis_ptr),
  193. tuples, MIN(p->image_size, len));
  194. found++;
  195. break;
  196. }
  197. }
  198. out:
  199. while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
  200. SIMPLEQ_REMOVE_HEAD(&rom_image, next);
  201. free(p, M_DEVBUF, 0);
  202. }
  203. exrom = pci_conf_read(pc, tag, reg);
  204. pci_conf_write(pc, tag, reg, exrom & ~1);
  205. splx(save);
  206. } else {
  207. command = pci_conf_read(pc, tag,
  208. PCI_COMMAND_STATUS_REG);
  209. pci_conf_write(pc, tag,
  210. PCI_COMMAND_STATUS_REG,
  211. command | PCI_COMMAND_MEM_ENABLE);
  212. /* XXX byte order? */
  213. bus_space_read_region_1(ca->ca_memt, bar_memh,
  214. cis_ptr, tuples, 256);
  215. found++;
  216. }
  217. command = pci_conf_read(pc, tag,
  218. PCI_COMMAND_STATUS_REG);
  219. pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
  220. command & ~PCI_COMMAND_MEM_ENABLE);
  221. pci_conf_write(pc, tag, reg, 0);
  222. Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh,
  223. bar_size);
  224. break;
  225. #ifdef DIAGNOSTIC
  226. default:
  227. panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname,
  228. cardbus_space);
  229. #endif
  230. }
  231. return (!found);
  232. }
  233. STATIC void
  234. parse_tuple(u_int8_t *tuple, int len, void *data)
  235. {
  236. struct cardbus_cis_info *cis = data;
  237. int bar_index;
  238. int i;
  239. char *p;
  240. switch (tuple[0]) {
  241. case PCMCIA_CISTPL_MANFID:
  242. if (tuple[1] < 4) {
  243. DPRINTF(("%s: wrong length manufacturer id (%d)\n",
  244. __func__, tuple[1]));
  245. break;
  246. }
  247. cis->manufacturer = tuple[2] | (tuple[3] << 8);
  248. cis->product = tuple[4] | (tuple[5] << 8);
  249. break;
  250. case PCMCIA_CISTPL_VERS_1:
  251. bcopy(tuple + 2, cis->cis1_info_buf, tuple[1]);
  252. i = 0;
  253. p = cis->cis1_info_buf + 2;
  254. while (i <
  255. sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
  256. if (p >= cis->cis1_info_buf + tuple[1] || *p == '\xff')
  257. break;
  258. cis->cis1_info[i++] = p;
  259. while (*p != '\0' && *p != '\xff')
  260. p++;
  261. if (*p == '\0')
  262. p++;
  263. }
  264. break;
  265. case PCMCIA_CISTPL_BAR:
  266. if (tuple[1] != 6) {
  267. DPRINTF(("%s: BAR with short length (%d)\n",
  268. __func__, tuple[1]));
  269. break;
  270. }
  271. bar_index = tuple[2] & 7;
  272. if (bar_index == 0) {
  273. DPRINTF(("%s: invalid ASI in BAR tuple\n",
  274. __func__));
  275. break;
  276. }
  277. bar_index--;
  278. cis->bar[bar_index].flags = tuple[2];
  279. cis->bar[bar_index].size = (tuple[4] << 0) |
  280. (tuple[5] << 8) | (tuple[6] << 16) | (tuple[7] << 24);
  281. break;
  282. case PCMCIA_CISTPL_FUNCID:
  283. cis->funcid = tuple[2];
  284. break;
  285. case PCMCIA_CISTPL_FUNCE:
  286. switch (cis->funcid) {
  287. case PCMCIA_FUNCTION_SERIAL:
  288. if (tuple[1] >= 2 &&
  289. tuple[2] == 0
  290. /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */) {
  291. cis->funce.serial.uart_type = tuple[3] & 0x1f;
  292. cis->funce.serial.uart_present = 1;
  293. }
  294. break;
  295. case PCMCIA_FUNCTION_NETWORK:
  296. if (tuple[1] >= 8 && tuple[2] ==
  297. PCMCIA_TPLFE_TYPE_LAN_NID) {
  298. if (tuple[3] >
  299. sizeof(cis->funce.network.netid)) {
  300. DPRINTF(("%s: unknown network id type"
  301. " (len = %d)\n", __func__,
  302. tuple[3]));
  303. } else {
  304. cis->funce.network.netid_present = 1;
  305. bcopy(tuple + 4,
  306. cis->funce.network.netid, tuple[3]);
  307. }
  308. }
  309. }
  310. break;
  311. }
  312. }
  313. /*
  314. * int cardbus_attach_card(struct cardbus_softc *sc)
  315. *
  316. * This function attaches the card on the slot: turns on power,
  317. * reads and analyses tuple, sets configuration index.
  318. *
  319. * This function returns the number of recognised device functions.
  320. * If no functions are recognised, return 0.
  321. */
  322. int
  323. cardbus_attach_card(struct cardbus_softc *sc)
  324. {
  325. cardbus_chipset_tag_t cc;
  326. cardbus_function_tag_t cf;
  327. int cdstatus;
  328. pcitag_t tag;
  329. pcireg_t id, class, cis_ptr;
  330. pcireg_t bhlc;
  331. u_int8_t *tuple;
  332. int function, nfunction;
  333. struct device *csc;
  334. int no_work_funcs = 0;
  335. cardbus_devfunc_t ct;
  336. pci_chipset_tag_t pc = sc->sc_pc;
  337. int i;
  338. cc = sc->sc_cc;
  339. cf = sc->sc_cf;
  340. DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit));
  341. /* inspect initial voltage */
  342. if (0 == (cdstatus = (cf->cardbus_ctrl)(cc, CARDBUS_CD))) {
  343. DPRINTF(("cardbusattach: no CardBus card on cb%d\n",
  344. sc->sc_dev.dv_unit));
  345. return (0);
  346. }
  347. /* XXX use fake function 8 to keep power on during whole configuration */
  348. enable_function(sc, cdstatus, 8);
  349. function = 0;
  350. tag = pci_make_tag(pc, sc->sc_bus, sc->sc_device, function);
  351. /* Wait until power comes up. Maximum 500 ms. */
  352. for (i = 0; i < 5; ++i) {
  353. id = pci_conf_read(pc, tag, PCI_ID_REG);
  354. if (id != 0xffffffff && id != 0)
  355. break;
  356. if (cold) { /* before kernel thread invoked */
  357. delay(100*1000);
  358. } else { /* thread context */
  359. if (tsleep((void *)sc, PCATCH, "cardbus",
  360. hz/10) != EWOULDBLOCK) {
  361. break;
  362. }
  363. }
  364. }
  365. if (i == 5)
  366. return (0);
  367. bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG);
  368. DPRINTF(("%s bhlc 0x%08x -> ", sc->sc_dev.dv_xname, bhlc));
  369. nfunction = PCI_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
  370. tuple = malloc(2048, M_TEMP, M_NOWAIT);
  371. if (tuple == NULL)
  372. panic("no room for cardbus tuples");
  373. for (function = 0; function < nfunction; function++) {
  374. struct cardbus_attach_args ca;
  375. tag = pci_make_tag(pc, sc->sc_bus, sc->sc_device,
  376. function);
  377. id = pci_conf_read(pc, tag, PCI_ID_REG);
  378. class = pci_conf_read(pc, tag, PCI_CLASS_REG);
  379. cis_ptr = pci_conf_read(pc, tag, CARDBUS_CIS_REG);
  380. /* Invalid vendor ID value? */
  381. if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
  382. continue;
  383. DPRINTF(("cardbus_attach_card: Vendor 0x%x, Product 0x%x, "
  384. "CIS 0x%x\n", PCI_VENDOR(id), PCI_PRODUCT(id),
  385. cis_ptr));
  386. enable_function(sc, cdstatus, function);
  387. /* clean up every BAR */
  388. pci_conf_write(pc, tag, CARDBUS_BASE0_REG, 0);
  389. pci_conf_write(pc, tag, CARDBUS_BASE1_REG, 0);
  390. pci_conf_write(pc, tag, CARDBUS_BASE2_REG, 0);
  391. pci_conf_write(pc, tag, CARDBUS_BASE3_REG, 0);
  392. pci_conf_write(pc, tag, CARDBUS_BASE4_REG, 0);
  393. pci_conf_write(pc, tag, CARDBUS_BASE5_REG, 0);
  394. pci_conf_write(pc, tag, CARDBUS_ROM_REG, 0);
  395. /* set initial latency and cacheline size */
  396. bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG);
  397. DPRINTF(("%s func%d bhlc 0x%08x -> ", sc->sc_dev.dv_xname,
  398. function, bhlc));
  399. bhlc &= ~((PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT) |
  400. (PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT));
  401. bhlc |= ((sc->sc_cacheline & PCI_CACHELINE_MASK) <<
  402. PCI_CACHELINE_SHIFT);
  403. bhlc |= ((sc->sc_lattimer & PCI_LATTIMER_MASK) <<
  404. PCI_LATTIMER_SHIFT);
  405. pci_conf_write(pc, tag, PCI_BHLC_REG, bhlc);
  406. bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG);
  407. DPRINTF(("0x%08x\n", bhlc));
  408. if (PCI_LATTIMER(bhlc) < 0x10) {
  409. bhlc &= ~(PCI_LATTIMER_MASK <<
  410. PCI_LATTIMER_SHIFT);
  411. bhlc |= (0x10 << PCI_LATTIMER_SHIFT);
  412. pci_conf_write(pc, tag, PCI_BHLC_REG,
  413. bhlc);
  414. }
  415. /*
  416. * We need to allocate the ct here, since we might
  417. * need it when reading the CIS
  418. */
  419. if ((ct =
  420. (cardbus_devfunc_t)malloc(sizeof(struct cardbus_devfunc),
  421. M_DEVBUF, M_NOWAIT)) == NULL)
  422. panic("no room for cardbus_tag");
  423. ct->ct_cc = sc->sc_cc;
  424. ct->ct_cf = sc->sc_cf;
  425. ct->ct_bus = sc->sc_bus;
  426. ct->ct_dev = sc->sc_device;
  427. ct->ct_func = function;
  428. ct->ct_sc = sc;
  429. sc->sc_funcs[function] = ct;
  430. memset(&ca, 0, sizeof(ca));
  431. ca.ca_unit = sc->sc_dev.dv_unit;
  432. ca.ca_ct = ct;
  433. ca.ca_iot = sc->sc_iot;
  434. ca.ca_memt = sc->sc_memt;
  435. ca.ca_dmat = sc->sc_dmat;
  436. ca.ca_rbus_iot = sc->sc_rbus_iot;
  437. ca.ca_rbus_memt = sc->sc_rbus_memt;
  438. ca.ca_tag = tag;
  439. ca.ca_bus = sc->sc_bus;
  440. ca.ca_device = sc->sc_device;
  441. ca.ca_function = function;
  442. ca.ca_id = id;
  443. ca.ca_class = class;
  444. ca.ca_pc = sc->sc_pc;
  445. ca.ca_intrline = sc->sc_intrline;
  446. if (cis_ptr != 0) {
  447. if (cardbus_read_tuples(&ca, cis_ptr, tuple, 2048)) {
  448. printf("cardbus_attach_card: failed to "
  449. "read CIS\n");
  450. } else {
  451. #ifdef CARDBUS_DEBUG
  452. decode_tuples(tuple, 2048, print_tuple, NULL);
  453. #endif
  454. decode_tuples(tuple, 2048, parse_tuple,
  455. &ca.ca_cis);
  456. }
  457. }
  458. if ((csc = config_found_sm((void *)sc, &ca, cardbusprint,
  459. cardbussubmatch)) == NULL) {
  460. /* do not match */
  461. disable_function(sc, function);
  462. sc->sc_funcs[function] = NULL;
  463. free(ct, M_DEVBUF, 0);
  464. } else {
  465. /* found */
  466. ct->ct_device = csc;
  467. ++no_work_funcs;
  468. }
  469. }
  470. /*
  471. * XXX power down pseudo function 8 (this will power down the card
  472. * if no functions were attached).
  473. */
  474. disable_function(sc, 8);
  475. free(tuple, M_TEMP, 0);
  476. return (no_work_funcs);
  477. }
  478. STATIC int
  479. cardbussubmatch(struct device *parent, void *match, void *aux)
  480. {
  481. struct cfdata *cf = match;
  482. struct cardbus_attach_args *ca = aux;
  483. if (cf->cardbuscf_dev != CARDBUS_UNK_DEV &&
  484. cf->cardbuscf_dev != ca->ca_unit) {
  485. return (0);
  486. }
  487. if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION &&
  488. cf->cardbuscf_function != ca->ca_function) {
  489. return (0);
  490. }
  491. return ((*cf->cf_attach->ca_match)(parent, cf, aux));
  492. }
  493. STATIC int
  494. cardbusprint(void *aux, const char *pnp)
  495. {
  496. struct cardbus_attach_args *ca = aux;
  497. char devinfo[256];
  498. if (pnp) {
  499. pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo,
  500. sizeof(devinfo));
  501. printf("%s at %s", devinfo, pnp);
  502. }
  503. printf(" dev %d function %d", ca->ca_device, ca->ca_function);
  504. if (!pnp) {
  505. pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo,
  506. sizeof(devinfo));
  507. printf(" %s", devinfo);
  508. }
  509. return (UNCONF);
  510. }
  511. /*
  512. * void cardbus_detach_card(struct cardbus_softc *sc)
  513. *
  514. * This function detaches the card on the slot: detach device data
  515. * structure and turns off the power.
  516. *
  517. * This function must not be called under interrupt context.
  518. */
  519. void
  520. cardbus_detach_card(struct cardbus_softc *sc)
  521. {
  522. struct cardbus_devfunc *ct;
  523. int f;
  524. for (f = 0; f < 8; f++) {
  525. ct = sc->sc_funcs[f];
  526. if (ct == NULL)
  527. continue;
  528. DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname,
  529. ct->ct_device->dv_xname));
  530. if (config_detach(ct->ct_device, 0) != 0) {
  531. printf("%s: cannot detach dev %s, function %d\n",
  532. sc->sc_dev.dv_xname, ct->ct_device->dv_xname,
  533. ct->ct_func);
  534. } else {
  535. sc->sc_poweron_func &= ~(1 << ct->ct_func);
  536. sc->sc_funcs[ct->ct_func] = NULL;
  537. free(ct, M_DEVBUF, 0);
  538. }
  539. }
  540. sc->sc_poweron_func = 0;
  541. sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V | CARDBUS_VPP_0V);
  542. }
  543. /*
  544. * void *cardbus_intr_establish(cc, cf, irq, level, func, arg, name)
  545. * Interrupt handler of pccard.
  546. * args:
  547. * cardbus_chipset_tag_t *cc
  548. * int irq:
  549. */
  550. void *
  551. cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
  552. cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg,
  553. const char *name)
  554. {
  555. DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
  556. return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg, name);
  557. }
  558. /*
  559. * void cardbus_intr_disestablish(cc, cf, handler)
  560. * Interrupt handler of pccard.
  561. * args:
  562. * cardbus_chipset_tag_t *cc
  563. */
  564. void
  565. cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
  566. void *handler)
  567. {
  568. DPRINTF(("- pccard_intr_disestablish\n"));
  569. (*cf->cardbus_intr_disestablish)(cc, handler);
  570. }
  571. /* XXX this should be merged with cardbus_function_{enable,disable},
  572. but we don't have a ct when these functions are called */
  573. STATIC void
  574. enable_function(struct cardbus_softc *sc, int cdstatus, int function)
  575. {
  576. if (sc->sc_poweron_func == 0) {
  577. /* switch to 3V and/or wait for power to stabilize */
  578. if (cdstatus & CARDBUS_3V_CARD) {
  579. sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V);
  580. } else {
  581. /* No cards other than 3.3V cards. */
  582. return;
  583. }
  584. (sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
  585. }
  586. sc->sc_poweron_func |= (1 << function);
  587. }
  588. STATIC void
  589. disable_function(struct cardbus_softc *sc, int function)
  590. {
  591. sc->sc_poweron_func &= ~(1 << function);
  592. if (sc->sc_poweron_func == 0) {
  593. /* power-off because no functions are enabled */
  594. sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V);
  595. }
  596. }
  597. /*
  598. * int cardbus_function_enable(struct cardbus_softc *sc, int func)
  599. *
  600. * This function enables a function on a card. When no power is
  601. * applied on the card, power will be applied on it.
  602. */
  603. int
  604. cardbus_function_enable(struct cardbus_softc *sc, int func)
  605. {
  606. pci_chipset_tag_t pc = sc->sc_pc;
  607. pcireg_t command;
  608. pcitag_t tag;
  609. DPRINTF(("entering cardbus_function_enable... "));
  610. /* entering critical area */
  611. /* XXX: sc_vold should be used */
  612. enable_function(sc, CARDBUS_3V_CARD, func);
  613. /* exiting critical area */
  614. tag = pci_make_tag(pc, sc->sc_bus, sc->sc_device, func);
  615. command = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
  616. command |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_IO_ENABLE |
  617. PCI_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
  618. pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, command);
  619. DPRINTF(("%x\n", sc->sc_poweron_func));
  620. return (0);
  621. }
  622. /*
  623. * int cardbus_function_disable(struct cardbus_softc *, int func)
  624. *
  625. * This function disable a function on a card. When no functions are
  626. * enabled, it turns off the power.
  627. */
  628. int
  629. cardbus_function_disable(struct cardbus_softc *sc, int func)
  630. {
  631. DPRINTF(("entering cardbus_function_disable... "));
  632. disable_function(sc, func);
  633. return (0);
  634. }
  635. int
  636. cardbus_matchbyid(struct cardbus_attach_args *ca,
  637. const struct pci_matchid *ids, int nent)
  638. {
  639. const struct pci_matchid *pm;
  640. int i;
  641. for (i = 0, pm = ids; i < nent; i++, pm++)
  642. if (PCI_VENDOR(ca->ca_id) == pm->pm_vid &&
  643. PCI_PRODUCT(ca->ca_id) == pm->pm_pid)
  644. return (1);
  645. return (0);
  646. }
  647. /*
  648. * below this line, there are some functions for decoding tuples.
  649. * They should go out from this file.
  650. */
  651. STATIC u_int8_t *
  652. decode_tuple(u_int8_t *, u_int8_t *, tuple_decode_func, void *);
  653. STATIC int
  654. decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
  655. {
  656. u_int8_t *tp = tuple;
  657. if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
  658. DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
  659. return (0);
  660. }
  661. while ((tp = decode_tuple(tp, tuple + buflen, func, data)) != NULL)
  662. ;
  663. return (1);
  664. }
  665. STATIC u_int8_t *
  666. decode_tuple(u_int8_t *tuple, u_int8_t *end, tuple_decode_func func,
  667. void *data)
  668. {
  669. u_int8_t type;
  670. u_int8_t len;
  671. type = tuple[0];
  672. switch (type) {
  673. case PCMCIA_CISTPL_NULL:
  674. case PCMCIA_CISTPL_END:
  675. len = 1;
  676. break;
  677. default:
  678. if (tuple + 2 > end)
  679. return (NULL);
  680. len = tuple[1] + 2;
  681. break;
  682. }
  683. if (tuple + len > end)
  684. return (NULL);
  685. (*func)(tuple, len, data);
  686. if (PCMCIA_CISTPL_END == type || tuple + len == end)
  687. return (NULL);
  688. return (tuple + len);
  689. }
  690. #ifdef CARDBUS_DEBUG
  691. static char *tuple_name(int type);
  692. static char *
  693. tuple_name(int type)
  694. {
  695. static char *tuple_name_s [] = {
  696. "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
  697. "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
  698. "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
  699. "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
  700. "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
  701. "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A", /* 14-17 */
  702. "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY", /* 18-1B */
  703. "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", /* 1C-1E */
  704. "DEVICE_GEO_A", "MANFID", "FUNCID", "FUNCE", "SWIL", /* 1F-23 */
  705. "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
  706. "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
  707. "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
  708. "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
  709. "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
  710. "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
  711. "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
  712. "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER", /* 40-43 */
  713. "DATE", "BATTERY", "ORG", "FORMAT_A" /* 44-47 */
  714. };
  715. if (type > 0 && type < nitems(tuple_name_s))
  716. return (tuple_name_s[type]);
  717. else if (0xff == type)
  718. return ("END");
  719. else
  720. return ("Reserved");
  721. }
  722. static void
  723. print_tuple(u_int8_t *tuple, int len, void *data)
  724. {
  725. int i;
  726. printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
  727. for (i = 0; i < len; ++i) {
  728. if (i % 16 == 0)
  729. printf(" 0x%02x:", i);
  730. printf(" %x",tuple[i]);
  731. if (i % 16 == 15)
  732. printf("\n");
  733. }
  734. if (i % 16 != 0)
  735. printf("\n");
  736. }
  737. #endif