devpccard.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. /*
  2. cardbus and pcmcia (grmph) support.
  3. */
  4. #include "u.h"
  5. #include "lib.h"
  6. #include "mem.h"
  7. #include "dat.h"
  8. #include "fns.h"
  9. #include "error.h"
  10. #include "io.h"
  11. extern int pciscan(int, Pcidev **);
  12. int (*_pcmspecial)(char *, ISAConf *);
  13. void (*_pcmspecialclose)(int);
  14. int
  15. pcmspecial(char *idstr, ISAConf *isa)
  16. {
  17. return (_pcmspecial != nil)? _pcmspecial(idstr, isa): -1;
  18. }
  19. void
  20. pcmspecialclose(int a)
  21. {
  22. if (_pcmspecialclose != nil)
  23. _pcmspecialclose(a);
  24. }
  25. static ulong
  26. ioreserve(ulong, int size, int align, char *)
  27. {
  28. static ulong isaend = 0x400; /*0xfd00*/
  29. ulong ioaddr;
  30. if (align)
  31. isaend = ((isaend + align - 1) / align) * align;
  32. ioaddr = isaend;
  33. isaend += size;
  34. return ioaddr;
  35. }
  36. #define MAP(x,o) (Rmap + (x)*0x8 + o)
  37. enum {
  38. TI_vid = 0x104c,
  39. TI_1131_did = 0xAC15,
  40. TI_1250_did = 0xAC16,
  41. TI_1450_did = 0xAC1B,
  42. TI_1251A_did = 0xAC1D,
  43. TI_1420_did = 0xAC51,
  44. Ricoh_vid = 0x1180,
  45. Ricoh_475_did = 0x0475,
  46. Ricoh_476_did = 0x0476,
  47. Ricoh_478_did = 0x0478,
  48. O2_vid = 0x1217,
  49. O2_OZ711M3_did = 0x7134,
  50. Nslots = 4, /* Maximum number of CardBus slots to use */
  51. K = 1024,
  52. M = K * K,
  53. LegacyAddr = 0x3e0,
  54. NUMEVENTS = 10,
  55. TI1131xSC = 0x80, // system control
  56. TI122X_SC_INTRTIE = 1 << 29,
  57. TI12xxIM = 0x8c, //
  58. TI1131xCC = 0x91, // card control
  59. TI113X_CC_RIENB = 1 << 7,
  60. TI113X_CC_ZVENABLE = 1 << 6,
  61. TI113X_CC_PCI_IRQ_ENA = 1 << 5,
  62. TI113X_CC_PCI_IREQ = 1 << 4,
  63. TI113X_CC_PCI_CSC = 1 << 3,
  64. TI113X_CC_SPKROUTEN = 1 << 1,
  65. TI113X_CC_IFG = 1 << 0,
  66. TI1131xDC = 0x92, // device control
  67. };
  68. typedef struct Variant Variant;
  69. struct Variant {
  70. ushort vid;
  71. ushort did;
  72. char *name;
  73. };
  74. static Variant variant[] = {
  75. { Ricoh_vid, Ricoh_475_did, "Ricoh 475 PCI/Cardbus bridge", },
  76. { Ricoh_vid, Ricoh_476_did, "Ricoh 476 PCI/Cardbus bridge", },
  77. { Ricoh_vid, Ricoh_478_did, "Ricoh 478 PCI/Cardbus bridge", },
  78. { TI_vid, TI_1131_did, "TI PCI-1131 Cardbus Controller", },
  79. { TI_vid, TI_1250_did, "TI PCI-1250 Cardbus Controller", },
  80. { TI_vid, TI_1450_did, "TI PCI-1450 Cardbus Controller", },
  81. { TI_vid, TI_1251A_did, "TI PCI-1251A Cardbus Controller", },
  82. { TI_vid, TI_1420_did, "TI PCI-1420 Cardbus Controller", },
  83. { O2_vid, O2_OZ711M3_did, "O2Micro OZ711M3 MemoryCardBus", },
  84. };
  85. /* Cardbus registers */
  86. enum {
  87. SocketEvent = 0,
  88. SE_CCD = 3 << 1,
  89. SE_POWER = 1 << 3,
  90. SocketMask = 1,
  91. SocketState = 2,
  92. SS_CCD = 3 << 1,
  93. SS_POWER = 1 << 3,
  94. SS_PC16 = 1 << 4,
  95. SS_CBC = 1 << 5,
  96. SS_NOTCARD = 1 << 7,
  97. SS_BADVCC = 1 << 9,
  98. SS_5V = 1 << 10,
  99. SS_3V = 1 << 11,
  100. SocketForce = 3,
  101. SocketControl = 4,
  102. SC_5V = 0x22,
  103. SC_3V = 0x33,
  104. };
  105. enum {
  106. PciPCR_IO = 1 << 0,
  107. PciPCR_MEM = 1 << 1,
  108. PciPCR_Master = 1 << 2,
  109. PciPMC = 0xa4,
  110. Nbars = 6,
  111. Ncmd = 10,
  112. CBIRQ = 9,
  113. PC16,
  114. PC32,
  115. };
  116. enum {
  117. Ti82365,
  118. Tpd6710,
  119. Tpd6720,
  120. Tvg46x,
  121. };
  122. /*
  123. * Intel 82365SL PCIC controller for the PCMCIA or
  124. * Cirrus Logic PD6710/PD6720 which is mostly register compatible
  125. */
  126. enum
  127. {
  128. /*
  129. * registers indices
  130. */
  131. Rid= 0x0, /* identification and revision */
  132. Ris= 0x1, /* interface status */
  133. Rpc= 0x2, /* power control */
  134. Foutena= (1<<7), /* output enable */
  135. Fautopower= (1<<5), /* automatic power switching */
  136. Fcardena= (1<<4), /* PC card enable */
  137. Rigc= 0x3, /* interrupt and general control */
  138. Fiocard= (1<<5), /* I/O card (vs memory) */
  139. Fnotreset= (1<<6), /* reset if not set */
  140. FSMIena= (1<<4), /* enable change interrupt on SMI */
  141. Rcsc= 0x4, /* card status change */
  142. Rcscic= 0x5, /* card status change interrupt config */
  143. Fchangeena= (1<<3), /* card changed */
  144. Fbwarnena= (1<<1), /* card battery warning */
  145. Fbdeadena= (1<<0), /* card battery dead */
  146. Rwe= 0x6, /* address window enable */
  147. Fmem16= (1<<5), /* use A23-A12 to decode address */
  148. Rio= 0x7, /* I/O control */
  149. Fwidth16= (1<<0), /* 16 bit data width */
  150. Fiocs16= (1<<1), /* IOCS16 determines data width */
  151. Fzerows= (1<<2), /* zero wait state */
  152. Ftiming= (1<<3), /* timing register to use */
  153. Riobtm0lo= 0x8, /* I/O address 0 start low byte */
  154. Riobtm0hi= 0x9, /* I/O address 0 start high byte */
  155. Riotop0lo= 0xa, /* I/O address 0 stop low byte */
  156. Riotop0hi= 0xb, /* I/O address 0 stop high byte */
  157. Riobtm1lo= 0xc, /* I/O address 1 start low byte */
  158. Riobtm1hi= 0xd, /* I/O address 1 start high byte */
  159. Riotop1lo= 0xe, /* I/O address 1 stop low byte */
  160. Riotop1hi= 0xf, /* I/O address 1 stop high byte */
  161. Rmap= 0x10, /* map 0 */
  162. /*
  163. * CL-PD67xx extension registers
  164. */
  165. Rmisc1= 0x16, /* misc control 1 */
  166. F5Vdetect= (1<<0),
  167. Fvcc3V= (1<<1),
  168. Fpmint= (1<<2),
  169. Fpsirq= (1<<3),
  170. Fspeaker= (1<<4),
  171. Finpack= (1<<7),
  172. Rfifo= 0x17, /* fifo control */
  173. Fflush= (1<<7), /* flush fifo */
  174. Rmisc2= 0x1E, /* misc control 2 */
  175. Flowpow= (1<<1), /* low power mode */
  176. Rchipinfo= 0x1F, /* chip information */
  177. Ratactl= 0x26, /* ATA control */
  178. /*
  179. * offsets into the system memory address maps
  180. */
  181. Mbtmlo= 0x0, /* System mem addr mapping start low byte */
  182. Mbtmhi= 0x1, /* System mem addr mapping start high byte */
  183. F16bit= (1<<7), /* 16-bit wide data path */
  184. Mtoplo= 0x2, /* System mem addr mapping stop low byte */
  185. Mtophi= 0x3, /* System mem addr mapping stop high byte */
  186. Ftimer1= (1<<6), /* timer set 1 */
  187. Mofflo= 0x4, /* Card memory offset address low byte */
  188. Moffhi= 0x5, /* Card memory offset address high byte */
  189. Fregactive= (1<<6), /* attribute memory */
  190. /*
  191. * configuration registers - they start at an offset in attribute
  192. * memory found in the CIS.
  193. */
  194. Rconfig= 0,
  195. Creset= (1<<7), /* reset device */
  196. Clevel= (1<<6), /* level sensitive interrupt line */
  197. };
  198. /*
  199. * read and crack the card information structure enough to set
  200. * important parameters like power
  201. */
  202. /* cis memory walking */
  203. typedef struct Cisdat Cisdat;
  204. struct Cisdat {
  205. uchar *cisbase;
  206. int cispos;
  207. int cisskip;
  208. int cislen;
  209. };
  210. typedef struct Pcminfo Pcminfo;
  211. struct Pcminfo {
  212. char verstr[512]; /* Version string */
  213. PCMmap mmap[4]; /* maps, last is always for the kernel */
  214. ulong conf_addr; /* Config address */
  215. uchar conf_present; /* Config register present */
  216. int nctab; /* In use configuration tables */
  217. PCMconftab ctab[8]; /* Configuration tables */
  218. PCMconftab *defctab; /* Default conftab */
  219. int port; /* Actual port usage */
  220. int irq; /* Actual IRQ usage */
  221. };
  222. typedef struct Cardbus Cardbus;
  223. struct Cardbus {
  224. Lock;
  225. Variant *variant; /* Which CardBus chipset */
  226. Pcidev *pci; /* The bridge itself */
  227. ulong *regs; /* Cardbus registers */
  228. int ltype; /* Legacy type */
  229. int lindex; /* Legacy port index address */
  230. int ldata; /* Legacy port data address */
  231. int lbase; /* Base register for this socket */
  232. int state; /* Current state of card */
  233. int type; /* Type of card */
  234. Pcminfo linfo; /* PCMCIA slot info */
  235. int special; /* card is allocated to a driver */
  236. int refs; /* Number of refs to slot */
  237. Lock refslock; /* inc/dev ref lock */
  238. };
  239. enum {
  240. Mshift= 12,
  241. Mgran= (1<<Mshift), /* granularity of maps */
  242. Mmask= ~(Mgran-1), /* mask for address bits important to the chip */
  243. };
  244. static Cardbus cbslots[Nslots];
  245. static int nslots;
  246. static ulong exponent[8] = {
  247. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  248. };
  249. static ulong vmant[16] = {
  250. 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80, 90,
  251. };
  252. static ulong mantissa[16] = {
  253. 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80,
  254. };
  255. static void cbint(Ureg *, void *);
  256. static int powerup(Cardbus *);
  257. static void configure(Cardbus *);
  258. static void managecard(Cardbus *);
  259. static void cardmanager(void *);
  260. static void eject(Cardbus *);
  261. static void interrupt(Ureg *, void *);
  262. static void powerdown(Cardbus *cb);
  263. static void unconfigure(Cardbus *cb);
  264. static void i82365probe(Cardbus *cb, int lindex, int ldata);
  265. static void i82365configure(Cardbus *cb);
  266. static PCMmap *isamap(Cardbus *cb, ulong offset, int len, int attr);
  267. static void isaunmap(PCMmap* m);
  268. static uchar rdreg(Cardbus *cb, int index);
  269. static void wrreg(Cardbus *cb, int index, uchar val);
  270. static int readc(Cisdat *cis, uchar *x);
  271. static void tvers1(Cardbus *cb, Cisdat *cis, int );
  272. static void tcfig(Cardbus *cb, Cisdat *cis, int );
  273. static void tentry(Cardbus *cb, Cisdat *cis, int );
  274. static int vcode(int volt);
  275. static int pccard_pcmspecial(char *idstr, ISAConf *isa);
  276. static void pccard_pcmspecialclose(int slotno);
  277. enum {
  278. CardDetected,
  279. CardPowered,
  280. CardEjected,
  281. CardConfigured,
  282. };
  283. static char *messages[] = {
  284. [CardDetected] "CardDetected",
  285. [CardPowered] "CardPowered",
  286. [CardEjected] "CardEjected",
  287. [CardConfigured] "CardConfigured",
  288. };
  289. enum {
  290. SlotEmpty,
  291. SlotFull,
  292. SlotPowered,
  293. SlotConfigured,
  294. };
  295. static char *states[] = {
  296. [SlotEmpty] "SlotEmpty",
  297. [SlotFull] "SlotFull",
  298. [SlotPowered] "SlotPowered",
  299. [SlotConfigured] "SlotConfigured",
  300. };
  301. static void
  302. engine(Cardbus *cb, int message)
  303. {
  304. // print("engine(%d): %s(%s)\n",
  305. // (int)(cb - cbslots), states[cb->state], messages[message]);
  306. switch (cb->state) {
  307. case SlotEmpty:
  308. switch (message) {
  309. case CardDetected:
  310. cb->state = SlotFull;
  311. powerup(cb);
  312. break;
  313. case CardEjected:
  314. break;
  315. default:
  316. //print("#Y%d: Invalid message %s in SlotEmpty state\n",
  317. // (int)(cb - cbslots), messages[message]);
  318. break;
  319. }
  320. break;
  321. case SlotFull:
  322. switch (message) {
  323. case CardPowered:
  324. cb->state = SlotPowered;
  325. configure(cb);
  326. break;
  327. case CardEjected:
  328. cb->state = SlotEmpty;
  329. powerdown(cb);
  330. break;
  331. default:
  332. //print("#Y%d: Invalid message %s in SlotFull state\n",
  333. // (int)(cb - cbslots), messages[message]);
  334. break;
  335. }
  336. break;
  337. case SlotPowered:
  338. switch (message) {
  339. case CardConfigured:
  340. cb->state = SlotConfigured;
  341. break;
  342. case CardEjected:
  343. cb->state = SlotEmpty;
  344. unconfigure(cb);
  345. powerdown(cb);
  346. break;
  347. default:
  348. print("#Y%d: Invalid message %s in SlotPowered state\n",
  349. (int)(cb - cbslots), messages[message]);
  350. break;
  351. }
  352. break;
  353. case SlotConfigured:
  354. switch (message) {
  355. case CardEjected:
  356. cb->state = SlotEmpty;
  357. unconfigure(cb);
  358. powerdown(cb);
  359. break;
  360. default:
  361. //print("#Y%d: Invalid message %s in SlotConfigured state\n",
  362. // (int)(cb - cbslots), messages[message]);
  363. break;
  364. }
  365. break;
  366. }
  367. }
  368. void
  369. devpccardlink(void)
  370. {
  371. static int initialized;
  372. Pcidev *pci;
  373. int i;
  374. char *p;
  375. if (initialized)
  376. return;
  377. initialized = 1;
  378. if((p=getconf("pccard0")) && strncmp(p, "disabled", 8)==0)
  379. return;
  380. if(_pcmspecial)
  381. return;
  382. /* Allocate legacy space */
  383. if (ioalloc(LegacyAddr, 2, 0, "i82365.0") < 0)
  384. print("#Y: WARNING: Cannot allocate legacy ports\n");
  385. /* Find all CardBus controllers */
  386. pci = nil;
  387. while ((pci = pcimatch(pci, 0, 0)) != nil) {
  388. ulong baddr;
  389. uchar pin;
  390. Cardbus *cb;
  391. int slot;
  392. if(pci->ccrb != 6 || pci->ccru != 7)
  393. continue;
  394. for (i = 0; i != nelem(variant); i++)
  395. if (pci->vid == variant[i].vid && pci->did == variant[i].did)
  396. break;
  397. if (i == nelem(variant))
  398. continue;
  399. /* initialize this slot */
  400. slot = nslots++;
  401. cb = &cbslots[slot];
  402. cb->pci = pci;
  403. cb->variant = &variant[i];
  404. // Set up PCI bus numbers if needed.
  405. if (pcicfgr8(pci, PciSBN) == 0) {
  406. static int busbase = 0x20;
  407. pcicfgw8(pci, PciSBN, busbase);
  408. pcicfgw8(pci, PciUBN, busbase + 2);
  409. busbase += 3;
  410. }
  411. // Patch up intl if needed.
  412. if ((pin = pcicfgr8(pci, PciINTP)) != 0 &&
  413. (pci->intl == 0xff || pci->intl == 0)) {
  414. pci->intl = pciipin(nil, pin);
  415. pcicfgw8(pci, PciINTL, pci->intl);
  416. if (pci->intl == 0xff || pci->intl == 0)
  417. print("#Y%d: No interrupt?\n", (int)(cb - cbslots));
  418. }
  419. // Don't you love standards!
  420. if (pci->vid == TI_vid) {
  421. if (pci->did <= TI_1131_did) {
  422. uchar cc;
  423. cc = pcicfgr8(pci, TI1131xCC);
  424. cc &= ~(TI113X_CC_PCI_IRQ_ENA |
  425. TI113X_CC_PCI_IREQ |
  426. TI113X_CC_PCI_CSC |
  427. TI113X_CC_ZVENABLE);
  428. cc |= TI113X_CC_PCI_IRQ_ENA |
  429. TI113X_CC_PCI_IREQ |
  430. TI113X_CC_SPKROUTEN;
  431. pcicfgw8(pci, TI1131xCC, cc);
  432. // PCI interrupts only
  433. pcicfgw8(pci, TI1131xDC,
  434. pcicfgr8(pci, TI1131xDC) & ~6);
  435. // CSC ints to PCI bus.
  436. wrreg(cb, Rigc, rdreg(cb, Rigc) | 0x10);
  437. }
  438. else if (pci->did == TI_1250_did) {
  439. print("No support yet for the TI_1250_did, prod pb\n");
  440. }
  441. else if (pci->did == TI_1420_did) {
  442. // Disable Vcc protection
  443. pcicfgw32(cb->pci, 0x80,
  444. pcicfgr32(cb->pci, 0x80) | (1 << 21));
  445. }
  446. pcicfgw16(cb->pci, PciPMC, pcicfgr16(cb->pci, PciPMC) & ~3);
  447. }
  448. if ((baddr = pcicfgr32(cb->pci, PciBAR0)) == 0) {
  449. int size = (pci->did == Ricoh_478_did)? 0x10000: 0x1000;
  450. baddr = upamalloc(baddr, size, size);
  451. pcicfgw32(cb->pci, PciBAR0, baddr);
  452. cb->regs = (ulong *)KADDR(baddr);
  453. }
  454. else
  455. cb->regs = (ulong *)KADDR(upamalloc(baddr, 4096, 0));
  456. cb->state = SlotEmpty;
  457. /* Don't really know what to do with this... */
  458. i82365probe(cb, LegacyAddr, LegacyAddr + 1);
  459. print("#Y%ld: %s, %.8ulX intl %d\n", cb - cbslots,
  460. variant[i].name, baddr, pci->intl);
  461. }
  462. if (nslots == 0)
  463. return;
  464. _pcmspecial = pccard_pcmspecial;
  465. _pcmspecialclose = pccard_pcmspecialclose;
  466. for (i = 0; i != nslots; i++) {
  467. Cardbus *cb = &cbslots[i];
  468. if ((cb->regs[SocketState] & SE_CCD) == 0)
  469. engine(cb, CardDetected);
  470. }
  471. delay(500); /* Allow time for power up */
  472. for (i = 0; i != nslots; i++) {
  473. Cardbus *cb = &cbslots[i];
  474. if (cb->regs[SocketState] & SE_POWER)
  475. engine(cb, CardPowered);
  476. /* Ack and enable interrupts on all events */
  477. //cb->regs[SocketEvent] = cb->regs[SocketEvent];
  478. //cb->regs[SocketMask] |= 0xF;
  479. //wrreg(cb, Rcscic, 0xC);
  480. }
  481. }
  482. static int
  483. powerup(Cardbus *cb)
  484. {
  485. ulong state;
  486. ushort bcr;
  487. state = cb->regs[SocketState];
  488. if (state & SS_PC16) {
  489. // print("#Y%ld: Probed a PC16 card, powering up card\n", cb - cbslots);
  490. cb->type = PC16;
  491. memset(&cb->linfo, 0, sizeof(Pcminfo));
  492. /* power up and unreset, wait's are empirical (???) */
  493. wrreg(cb, Rpc, Fautopower|Foutena|Fcardena);
  494. delay(300);
  495. wrreg(cb, Rigc, 0);
  496. delay(100);
  497. wrreg(cb, Rigc, Fnotreset);
  498. delay(500);
  499. return 1;
  500. }
  501. if (state & SS_CCD)
  502. return 0;
  503. if (state & SS_NOTCARD) {
  504. print("#Y%ld: No card inserted\n", cb - cbslots);
  505. return 0;
  506. }
  507. if (state & SS_BADVCC) {
  508. print("#Y%ld: Bad VCC request to card, powering down card!\n",
  509. cb - cbslots);
  510. cb->regs[SocketControl] = 0;
  511. return 0;
  512. }
  513. if ((state & SS_3V) == 0 && (state & SS_5V) == 0) {
  514. print("#Y%ld: Unsupported voltage, powering down card!\n",
  515. cb - cbslots);
  516. cb->regs[SocketControl] = 0;
  517. return 0;
  518. }
  519. //print("#Y%ld: card %spowered at %d volt\n", cb - cbslots,
  520. // (state & SS_POWER)? "": "not ",
  521. // (state & SS_3V)? 3: (state & SS_5V)? 5: -1);
  522. /* Power up the card
  523. * and make sure the secondary bus is not in reset.
  524. */
  525. cb->regs[SocketControl] = (state & SS_5V)? SC_5V: SC_3V;
  526. delay(50);
  527. bcr = pcicfgr16(cb->pci, PciBCR);
  528. bcr &= ~0x40;
  529. pcicfgw16(cb->pci, PciBCR, bcr);
  530. delay(100);
  531. cb->type = PC32;
  532. return 1;
  533. }
  534. static void
  535. powerdown(Cardbus *cb)
  536. {
  537. ushort bcr;
  538. if (cb->type == PC16) {
  539. wrreg(cb, Rpc, 0); /* turn off card power */
  540. wrreg(cb, Rwe, 0); /* no windows */
  541. cb->type = -1;
  542. return;
  543. }
  544. bcr = pcicfgr16(cb->pci, PciBCR);
  545. bcr |= 0x40;
  546. pcicfgw16(cb->pci, PciBCR, bcr);
  547. cb->regs[SocketControl] = 0;
  548. cb->type = -1;
  549. }
  550. static void
  551. configure(Cardbus *cb)
  552. {
  553. Pcidev *pci;
  554. ulong size, bar;
  555. int i, ioindex, memindex, r;
  556. //print("configuring slot %d (%s)\n", (int)(cb - cbslots), states[cb->state]);
  557. if (cb->state == SlotConfigured)
  558. return;
  559. engine(cb, CardConfigured);
  560. delay(50); /* Emperically established */
  561. if (cb->type == PC16) {
  562. i82365configure(cb);
  563. return;
  564. }
  565. /* Scan the CardBus for new PCI devices */
  566. pciscan(pcicfgr8(cb->pci, PciSBN), &cb->pci->bridge);
  567. pci = cb->pci->bridge;
  568. while (pci) {
  569. r = pcicfgr16(pci, PciPCR);
  570. r &= ~(PciPCR_IO|PciPCR_MEM);
  571. pcicfgw16(pci, PciPCR, r);
  572. /*
  573. * Treat the found device as an ordinary PCI card.
  574. * It seems that the CIS is not always present in
  575. * CardBus cards.
  576. * XXX, need to support multifunction cards
  577. */
  578. memindex = ioindex = 0;
  579. for (i = 0; i != Nbars; i++) {
  580. if (pci->mem[i].size == 0)
  581. continue;
  582. if (pci->mem[i].bar & 1) {
  583. // Allocate I/O space
  584. if (ioindex > 1) {
  585. print("#Y%ld: WARNING: Can only configure 2 I/O slots\n", cb - cbslots);
  586. continue;
  587. }
  588. bar = ioreserve(-1, pci->mem[i].size, 0, "cardbus");
  589. pci->mem[i].bar = bar | 1;
  590. pcicfgw32(pci, PciBAR0 + i * sizeof(ulong),
  591. pci->mem[i].bar);
  592. pcicfgw16(cb->pci, PciCBIBR0 + ioindex * 8, bar);
  593. pcicfgw16(cb->pci, PciCBILR0 + ioindex * 8,
  594. bar + pci->mem[i].size - 1);
  595. //print("ioindex[%d] %.8uX (%d)\n",
  596. // ioindex, bar, pci->mem[i].size);
  597. ioindex++;
  598. continue;
  599. }
  600. // Allocating memory space
  601. if (memindex > 1) {
  602. print("#Y%ld: WARNING: Can only configure 2 memory slots\n", cb - cbslots);
  603. continue;
  604. }
  605. bar = upamalloc(0, pci->mem[i].size, BY2PG);
  606. pci->mem[i].bar = bar | (pci->mem[i].bar & 0x80);
  607. pcicfgw32(pci, PciBAR0 + i * sizeof(ulong), pci->mem[i].bar);
  608. pcicfgw32(cb->pci, PciCBMBR0 + memindex * 8, bar);
  609. pcicfgw32(cb->pci, PciCBMLR0 + memindex * 8,
  610. bar + pci->mem[i].size - 1);
  611. if (pci->mem[i].bar & 0x80) {
  612. /* Enable prefetch */
  613. r = pcicfgr16(cb->pci, PciBCR);
  614. r |= 1 << (8 + memindex);
  615. pcicfgw16(cb->pci, PciBCR, r);
  616. }
  617. //print("memindex[%d] %.8uX (%d)\n",
  618. // memindex, bar, pci->mem[i].size);
  619. memindex++;
  620. }
  621. if ((size = pcibarsize(pci, PciEBAR0)) > 0) {
  622. if (memindex > 1)
  623. print("#Y%ld: WARNING: Too many memory spaces, not mapping ROM space\n",
  624. cb - cbslots);
  625. else {
  626. pci->rom.bar = upamalloc(0, size, BY2PG);
  627. pci->rom.size = size;
  628. pcicfgw32(pci, PciEBAR0, pci->rom.bar);
  629. pcicfgw32(cb->pci, PciCBMBR0 + memindex * 8,
  630. pci->rom.bar);
  631. pcicfgw32(cb->pci, PciCBMLR0 + memindex * 8,
  632. pci->rom.bar + pci->rom.size - 1);
  633. }
  634. }
  635. /* Set the basic PCI registers for the device */
  636. pci->pcr = pcicfgr16(pci, PciPCR);
  637. pci->pcr |= PciPCR_IO|PciPCR_MEM|PciPCR_Master;
  638. pci->cls = 8;
  639. pci->ltr = 64;
  640. pcicfgw16(pci, PciPCR, pci->pcr);
  641. pcicfgw8(pci, PciCLS, pci->cls);
  642. pcicfgw8(pci, PciLTR, pci->ltr);
  643. if (pcicfgr8(pci, PciINTP)) {
  644. pci->intl = pcicfgr8(cb->pci, PciINTL);
  645. pcicfgw8(pci, PciINTL, pci->intl);
  646. /* Route interrupts to INTA#/B# */
  647. pcicfgw16(cb->pci, PciBCR,
  648. pcicfgr16(cb->pci, PciBCR) & ~(1 << 7));
  649. }
  650. pci = pci->list;
  651. }
  652. }
  653. static void
  654. unconfigure(Cardbus *cb)
  655. {
  656. Pcidev *pci;
  657. int i, ioindex, memindex, r;
  658. if (cb->type == PC16) {
  659. print("#Y%d: Don't know how to unconfigure a PC16 card\n",
  660. (int)(cb - cbslots));
  661. memset(&cb->linfo, 0, sizeof(Pcminfo));
  662. return;
  663. }
  664. pci = cb->pci->bridge;
  665. if (pci == nil)
  666. return; /* Not configured */
  667. cb->pci->bridge = nil;
  668. memindex = ioindex = 0;
  669. while (pci) {
  670. Pcidev *_pci;
  671. for (i = 0; i != Nbars; i++) {
  672. if (pci->mem[i].size == 0)
  673. continue;
  674. if (pci->mem[i].bar & 1) {
  675. iofree(pci->mem[i].bar & ~1);
  676. pcicfgw16(cb->pci, PciCBIBR0 + ioindex * 8,
  677. (ushort)-1);
  678. pcicfgw16(cb->pci, PciCBILR0 + ioindex * 8, 0);
  679. ioindex++;
  680. continue;
  681. }
  682. upafree(pci->mem[i].bar & ~0xF, pci->mem[i].size);
  683. pcicfgw32(cb->pci, PciCBMBR0 + memindex * 8, (ulong)-1);
  684. pcicfgw32(cb->pci, PciCBMLR0 + memindex * 8, 0);
  685. r = pcicfgr16(cb->pci, PciBCR);
  686. r &= ~(1 << (8 + memindex));
  687. pcicfgw16(cb->pci, PciBCR, r);
  688. memindex++;
  689. }
  690. if (pci->rom.bar && memindex < 2) {
  691. upafree(pci->rom.bar & ~0xF, pci->rom.size);
  692. pcicfgw32(cb->pci, PciCBMBR0 + memindex * 8, (ulong)-1);
  693. pcicfgw32(cb->pci, PciCBMLR0 + memindex * 8, 0);
  694. memindex++;
  695. }
  696. _pci = pci->list;
  697. free(_pci);
  698. pci = _pci;
  699. }
  700. }
  701. static void
  702. i82365configure(Cardbus *cb)
  703. {
  704. int this;
  705. Cisdat cis;
  706. PCMmap *m;
  707. uchar type, link;
  708. /*
  709. * Read all tuples in attribute space.
  710. */
  711. m = isamap(cb, 0, 0, 1);
  712. if(m == 0)
  713. return;
  714. cis.cisbase = KADDR(m->isa);
  715. cis.cispos = 0;
  716. cis.cisskip = 2;
  717. cis.cislen = m->len;
  718. /* loop through all the tuples */
  719. for(;;){
  720. this = cis.cispos;
  721. if(readc(&cis, &type) != 1)
  722. break;
  723. if(type == 0xFF)
  724. break;
  725. if(readc(&cis, &link) != 1)
  726. break;
  727. switch(type){
  728. default:
  729. break;
  730. case 0x15:
  731. tvers1(cb, &cis, type);
  732. break;
  733. case 0x1A:
  734. tcfig(cb, &cis, type);
  735. break;
  736. case 0x1B:
  737. tentry(cb, &cis, type);
  738. break;
  739. }
  740. if(link == 0xFF)
  741. break;
  742. cis.cispos = this + (2+link);
  743. }
  744. isaunmap(m);
  745. }
  746. /*
  747. * look for a card whose version contains 'idstr'
  748. */
  749. static int
  750. pccard_pcmspecial(char *idstr, ISAConf *isa)
  751. {
  752. int i, irq;
  753. PCMconftab *ct, *et;
  754. Pcminfo *pi;
  755. Cardbus *cb;
  756. uchar x, we, *p;
  757. cb = nil;
  758. for (i = 0; i != nslots; i++) {
  759. cb = &cbslots[i];
  760. lock(cb);
  761. if (cb->state == SlotConfigured &&
  762. cb->type == PC16 &&
  763. !cb->special &&
  764. strstr(cb->linfo.verstr, idstr))
  765. break;
  766. unlock(cb);
  767. }
  768. if (i == nslots) {
  769. // print("#Y: %s not found\n", idstr);
  770. return -1;
  771. }
  772. pi = &cb->linfo;
  773. /*
  774. * configure the PCMslot for IO. We assume very heavily that we can read
  775. * configuration info from the CIS. If not, we won't set up correctly.
  776. */
  777. irq = isa->irq;
  778. if(irq == 2)
  779. irq = 9;
  780. et = &pi->ctab[pi->nctab];
  781. ct = nil;
  782. for(i = 0; i < isa->nopt; i++){
  783. int index;
  784. char *cp;
  785. if(strncmp(isa->opt[i], "index=", 6))
  786. continue;
  787. index = strtol(&isa->opt[i][6], &cp, 0);
  788. if(cp == &isa->opt[i][6] || index >= pi->nctab) {
  789. unlock(cb);
  790. print("#Y%d: Cannot find index %d in conf table\n",
  791. (int)(cb - cbslots), index);
  792. return -1;
  793. }
  794. ct = &pi->ctab[index];
  795. }
  796. if(ct == nil){
  797. PCMconftab *t;
  798. /* assume default is right */
  799. if(pi->defctab)
  800. ct = pi->defctab;
  801. else
  802. ct = pi->ctab;
  803. /* try for best match */
  804. if(ct->nio == 0
  805. || ct->io[0].start != isa->port || ((1<<irq) & ct->irqs) == 0){
  806. for(t = pi->ctab; t < et; t++)
  807. if(t->nio
  808. && t->io[0].start == isa->port
  809. && ((1<<irq) & t->irqs)){
  810. ct = t;
  811. break;
  812. }
  813. }
  814. if(ct->nio == 0 || ((1<<irq) & ct->irqs) == 0){
  815. for(t = pi->ctab; t < et; t++)
  816. if(t->nio && ((1<<irq) & t->irqs)){
  817. ct = t;
  818. break;
  819. }
  820. }
  821. if(ct->nio == 0){
  822. for(t = pi->ctab; t < et; t++)
  823. if(t->nio){
  824. ct = t;
  825. break;
  826. }
  827. }
  828. }
  829. if(ct == et || ct->nio == 0) {
  830. unlock(cb);
  831. print("#Y%d: No configuration?\n", (int)(cb - cbslots));
  832. return -1;
  833. }
  834. if(isa->port == 0 && ct->io[0].start == 0) {
  835. unlock(cb);
  836. print("#Y%d: No part or start address\n", (int)(cb - cbslots));
  837. return -1;
  838. }
  839. cb->special = 1; /* taken */
  840. /* route interrupts */
  841. isa->irq = irq;
  842. wrreg(cb, Rigc, irq | Fnotreset | Fiocard);
  843. /* set power and enable device */
  844. x = vcode(ct->vpp1);
  845. wrreg(cb, Rpc, x|Fautopower|Foutena|Fcardena);
  846. /* 16-bit data path */
  847. if(ct->bit16)
  848. x = Ftiming|Fiocs16|Fwidth16;
  849. else
  850. x = Ftiming;
  851. if(ct->nio == 2 && ct->io[1].start)
  852. x |= x<<4;
  853. wrreg(cb, Rio, x);
  854. /*
  855. * enable io port map 0
  856. * the 'top' register value includes the last valid address
  857. */
  858. if(isa->port == 0)
  859. isa->port = ct->io[0].start;
  860. we = rdreg(cb, Rwe);
  861. wrreg(cb, Riobtm0lo, isa->port);
  862. wrreg(cb, Riobtm0hi, isa->port>>8);
  863. i = isa->port+ct->io[0].len-1;
  864. wrreg(cb, Riotop0lo, i);
  865. wrreg(cb, Riotop0hi, i>>8);
  866. we |= 1<<6;
  867. if(ct->nio == 2 && ct->io[1].start){
  868. wrreg(cb, Riobtm1lo, ct->io[1].start);
  869. wrreg(cb, Riobtm1hi, ct->io[1].start>>8);
  870. i = ct->io[1].start+ct->io[1].len-1;
  871. wrreg(cb, Riotop1lo, i);
  872. wrreg(cb, Riotop1hi, i>>8);
  873. we |= 1<<7;
  874. }
  875. wrreg(cb, Rwe, we);
  876. /* only touch Rconfig if it is present */
  877. if(pi->conf_present & (1<<Rconfig)){
  878. PCMmap *m;
  879. /* Reset adapter */
  880. m = isamap(cb, pi->conf_addr + Rconfig, 1, 1);
  881. p = KADDR(m->isa + pi->conf_addr + Rconfig - m->ca);
  882. /* set configuration and interrupt type */
  883. x = ct->index;
  884. if((ct->irqtype & 0x20)/* && ((ct->irqtype & 0x40)==0 || isa->irq>7)*/)
  885. x |= Clevel;
  886. *p = x;
  887. delay(5);
  888. isaunmap(m);
  889. }
  890. pi->port = isa->port;
  891. pi->irq = isa->irq;
  892. unlock(cb);
  893. print("#Y%d: %s irq %ld, port %lX\n", (int)(cb - cbslots), pi->verstr, isa->irq, isa->port);
  894. return (int)(cb - cbslots);
  895. }
  896. static void
  897. pccard_pcmspecialclose(int slotno)
  898. {
  899. Cardbus *cb = &cbslots[slotno];
  900. wrreg(cb, Rwe, 0); /* no windows */
  901. cb->special = 0;
  902. }
  903. static int
  904. xcistuple(int slotno, int tuple, int subtuple, void *v, int nv, int attr)
  905. {
  906. PCMmap *m;
  907. Cisdat cis;
  908. int i, l;
  909. uchar *p;
  910. uchar type, link, n, c;
  911. int this, subtype;
  912. Cardbus *cb = &cbslots[slotno];
  913. m = isamap(cb, 0, 0, attr);
  914. if(m == 0)
  915. return -1;
  916. cis.cisbase = KADDR(m->isa);
  917. cis.cispos = 0;
  918. cis.cisskip = attr ? 2 : 1;
  919. cis.cislen = m->len;
  920. /* loop through all the tuples */
  921. for(i = 0; i < 1000; i++){
  922. this = cis.cispos;
  923. if(readc(&cis, &type) != 1)
  924. break;
  925. if(type == 0xFF)
  926. break;
  927. if(readc(&cis, &link) != 1)
  928. break;
  929. if(link == 0xFF)
  930. break;
  931. n = link;
  932. if (link > 1 && subtuple != -1) {
  933. if (readc(&cis, &c) != 1)
  934. break;
  935. subtype = c;
  936. n--;
  937. } else
  938. subtype = -1;
  939. if(type == tuple && subtype == subtuple) {
  940. p = v;
  941. for(l=0; l<nv && l<n; l++)
  942. if(readc(&cis, p++) != 1)
  943. break;
  944. isaunmap(m);
  945. return nv;
  946. }
  947. cis.cispos = this + (2+link);
  948. }
  949. isaunmap(m);
  950. return -1;
  951. }
  952. static PCMmap *
  953. isamap(Cardbus *cb, ulong offset, int len, int attr)
  954. {
  955. uchar we, bit;
  956. PCMmap *m, *nm;
  957. Pcminfo *pi;
  958. int i;
  959. ulong e;
  960. pi = &cb->linfo;
  961. /* convert offset to granularity */
  962. if(len <= 0)
  963. len = 1;
  964. e = ROUND(offset+len, Mgran);
  965. offset &= Mmask;
  966. len = e - offset;
  967. /* look for a map that covers the right area */
  968. we = rdreg(cb, Rwe);
  969. bit = 1;
  970. nm = 0;
  971. for(m = pi->mmap; m < &pi->mmap[nelem(pi->mmap)]; m++){
  972. if((we & bit))
  973. if(m->attr == attr)
  974. if(offset >= m->ca && e <= m->cea){
  975. m->ref++;
  976. return m;
  977. }
  978. bit <<= 1;
  979. if(nm == 0 && m->ref == 0)
  980. nm = m;
  981. }
  982. m = nm;
  983. if(m == 0)
  984. return 0;
  985. /* if isa space isn't big enough, free it and get more */
  986. if(m->len < len){
  987. if(m->isa){
  988. umbfree(m->isa, m->len);
  989. m->len = 0;
  990. }
  991. m->isa = PADDR(umbmalloc(0, len, Mgran));
  992. if(m->isa == 0){
  993. print("isamap: out of isa space\n");
  994. return 0;
  995. }
  996. m->len = len;
  997. }
  998. /* set up new map */
  999. m->ca = offset;
  1000. m->cea = m->ca + m->len;
  1001. m->attr = attr;
  1002. i = m - pi->mmap;
  1003. bit = 1<<i;
  1004. wrreg(cb, Rwe, we & ~bit); /* disable map before changing it */
  1005. wrreg(cb, MAP(i, Mbtmlo), m->isa>>Mshift);
  1006. wrreg(cb, MAP(i, Mbtmhi), (m->isa>>(Mshift+8)) | F16bit);
  1007. wrreg(cb, MAP(i, Mtoplo), (m->isa+m->len-1)>>Mshift);
  1008. wrreg(cb, MAP(i, Mtophi), ((m->isa+m->len-1)>>(Mshift+8)));
  1009. offset -= m->isa;
  1010. offset &= (1<<25)-1;
  1011. offset >>= Mshift;
  1012. wrreg(cb, MAP(i, Mofflo), offset);
  1013. wrreg(cb, MAP(i, Moffhi), (offset>>8) | (attr ? Fregactive : 0));
  1014. wrreg(cb, Rwe, we | bit); /* enable map */
  1015. m->ref = 1;
  1016. return m;
  1017. }
  1018. static void
  1019. isaunmap(PCMmap* m)
  1020. {
  1021. m->ref--;
  1022. }
  1023. /*
  1024. * reading and writing card registers
  1025. */
  1026. static uchar
  1027. rdreg(Cardbus *cb, int index)
  1028. {
  1029. outb(cb->lindex, cb->lbase + index);
  1030. return inb(cb->ldata);
  1031. }
  1032. static void
  1033. wrreg(Cardbus *cb, int index, uchar val)
  1034. {
  1035. outb(cb->lindex, cb->lbase + index);
  1036. outb(cb->ldata, val);
  1037. }
  1038. static int
  1039. readc(Cisdat *cis, uchar *x)
  1040. {
  1041. if(cis->cispos >= cis->cislen)
  1042. return 0;
  1043. *x = cis->cisbase[cis->cisskip*cis->cispos];
  1044. cis->cispos++;
  1045. return 1;
  1046. }
  1047. static ulong
  1048. getlong(Cisdat *cis, int size)
  1049. {
  1050. uchar c;
  1051. int i;
  1052. ulong x;
  1053. x = 0;
  1054. for(i = 0; i < size; i++){
  1055. if(readc(cis, &c) != 1)
  1056. break;
  1057. x |= c<<(i*8);
  1058. }
  1059. return x;
  1060. }
  1061. static void
  1062. tcfig(Cardbus *cb, Cisdat *cis, int )
  1063. {
  1064. uchar size, rasize, rmsize;
  1065. uchar last;
  1066. Pcminfo *pi;
  1067. if(readc(cis, &size) != 1)
  1068. return;
  1069. rasize = (size&0x3) + 1;
  1070. rmsize = ((size>>2)&0xf) + 1;
  1071. if(readc(cis, &last) != 1)
  1072. return;
  1073. pi = &cb->linfo;
  1074. pi->conf_addr = getlong(cis, rasize);
  1075. pi->conf_present = getlong(cis, rmsize);
  1076. }
  1077. static void
  1078. tvers1(Cardbus *cb, Cisdat *cis, int )
  1079. {
  1080. uchar c, major, minor, last;
  1081. int i;
  1082. Pcminfo *pi;
  1083. pi = &cb->linfo;
  1084. if(readc(cis, &major) != 1)
  1085. return;
  1086. if(readc(cis, &minor) != 1)
  1087. return;
  1088. last = 0;
  1089. for(i = 0; i < sizeof(pi->verstr) - 1; i++){
  1090. if(readc(cis, &c) != 1)
  1091. return;
  1092. if(c == 0)
  1093. c = ';';
  1094. if(c == '\n')
  1095. c = ';';
  1096. if(c == 0xff)
  1097. break;
  1098. if(c == ';' && last == ';')
  1099. continue;
  1100. pi->verstr[i] = c;
  1101. last = c;
  1102. }
  1103. pi->verstr[i] = 0;
  1104. }
  1105. static ulong
  1106. microvolt(Cisdat *cis)
  1107. {
  1108. uchar c;
  1109. ulong microvolts;
  1110. ulong exp;
  1111. if(readc(cis, &c) != 1)
  1112. return 0;
  1113. exp = exponent[c&0x7];
  1114. microvolts = vmant[(c>>3)&0xf]*exp;
  1115. while(c & 0x80){
  1116. if(readc(cis, &c) != 1)
  1117. return 0;
  1118. switch(c){
  1119. case 0x7d:
  1120. break; /* high impedence when sleeping */
  1121. case 0x7e:
  1122. case 0x7f:
  1123. microvolts = 0; /* no connection */
  1124. break;
  1125. default:
  1126. exp /= 10;
  1127. microvolts += exp*(c&0x7f);
  1128. }
  1129. }
  1130. return microvolts;
  1131. }
  1132. static ulong
  1133. nanoamps(Cisdat *cis)
  1134. {
  1135. uchar c;
  1136. ulong nanoamps;
  1137. if(readc(cis, &c) != 1)
  1138. return 0;
  1139. nanoamps = exponent[c&0x7]*vmant[(c>>3)&0xf];
  1140. while(c & 0x80){
  1141. if(readc(cis, &c) != 1)
  1142. return 0;
  1143. if(c == 0x7d || c == 0x7e || c == 0x7f)
  1144. nanoamps = 0;
  1145. }
  1146. return nanoamps;
  1147. }
  1148. /*
  1149. * only nominal voltage (feature 1) is important for config,
  1150. * other features must read card to stay in sync.
  1151. */
  1152. static ulong
  1153. power(Cisdat *cis)
  1154. {
  1155. uchar feature;
  1156. ulong mv;
  1157. mv = 0;
  1158. if(readc(cis, &feature) != 1)
  1159. return 0;
  1160. if(feature & 1)
  1161. mv = microvolt(cis);
  1162. if(feature & 2)
  1163. microvolt(cis);
  1164. if(feature & 4)
  1165. microvolt(cis);
  1166. if(feature & 8)
  1167. nanoamps(cis);
  1168. if(feature & 0x10)
  1169. nanoamps(cis);
  1170. if(feature & 0x20)
  1171. nanoamps(cis);
  1172. if(feature & 0x40)
  1173. nanoamps(cis);
  1174. return mv/1000000;
  1175. }
  1176. static ulong
  1177. ttiming(Cisdat *cis, int scale)
  1178. {
  1179. uchar unscaled;
  1180. ulong nanosecs;
  1181. if(readc(cis, &unscaled) != 1)
  1182. return 0;
  1183. nanosecs = (mantissa[(unscaled>>3)&0xf]*exponent[unscaled&7])/10;
  1184. nanosecs = nanosecs * exponent[scale];
  1185. return nanosecs;
  1186. }
  1187. static void
  1188. timing(Cisdat *cis, PCMconftab *ct)
  1189. {
  1190. uchar c, i;
  1191. if(readc(cis, &c) != 1)
  1192. return;
  1193. i = c&0x3;
  1194. if(i != 3)
  1195. ct->maxwait = ttiming(cis, i); /* max wait */
  1196. i = (c>>2)&0x7;
  1197. if(i != 7)
  1198. ct->readywait = ttiming(cis, i); /* max ready/busy wait */
  1199. i = (c>>5)&0x7;
  1200. if(i != 7)
  1201. ct->otherwait = ttiming(cis, i); /* reserved wait */
  1202. }
  1203. static void
  1204. iospaces(Cisdat *cis, PCMconftab *ct)
  1205. {
  1206. uchar c;
  1207. int i, nio;
  1208. ct->nio = 0;
  1209. if(readc(cis, &c) != 1)
  1210. return;
  1211. ct->bit16 = ((c>>5)&3) >= 2;
  1212. if(!(c & 0x80)){
  1213. ct->io[0].start = 0;
  1214. ct->io[0].len = 1<<(c&0x1f);
  1215. ct->nio = 1;
  1216. return;
  1217. }
  1218. if(readc(cis, &c) != 1)
  1219. return;
  1220. /*
  1221. * For each of the range descriptions read the
  1222. * start address and the length (value is length-1).
  1223. */
  1224. nio = (c&0xf)+1;
  1225. for(i = 0; i < nio; i++){
  1226. ct->io[i].start = getlong(cis, (c>>4)&0x3);
  1227. ct->io[i].len = getlong(cis, (c>>6)&0x3)+1;
  1228. }
  1229. ct->nio = nio;
  1230. }
  1231. static void
  1232. irq(Cisdat *cis, PCMconftab *ct)
  1233. {
  1234. uchar c;
  1235. if(readc(cis, &c) != 1)
  1236. return;
  1237. ct->irqtype = c & 0xe0;
  1238. if(c & 0x10)
  1239. ct->irqs = getlong(cis, 2);
  1240. else
  1241. ct->irqs = 1<<(c&0xf);
  1242. ct->irqs &= 0xDEB8; /* levels available to card */
  1243. }
  1244. static void
  1245. memspace(Cisdat *cis, int asize, int lsize, int host)
  1246. {
  1247. ulong haddress, address, len;
  1248. len = getlong(cis, lsize)*256;
  1249. address = getlong(cis, asize)*256;
  1250. USED(len, address);
  1251. if(host){
  1252. haddress = getlong(cis, asize)*256;
  1253. USED(haddress);
  1254. }
  1255. }
  1256. static void
  1257. tentry(Cardbus *cb, Cisdat *cis, int )
  1258. {
  1259. uchar c, i, feature;
  1260. PCMconftab *ct;
  1261. Pcminfo *pi;
  1262. pi = &cb->linfo;
  1263. if(pi->nctab >= nelem(pi->ctab))
  1264. return;
  1265. if(readc(cis, &c) != 1)
  1266. return;
  1267. ct = &pi->ctab[pi->nctab++];
  1268. /* copy from last default config */
  1269. if(pi->defctab)
  1270. *ct = *pi->defctab;
  1271. ct->index = c & 0x3f;
  1272. /* is this the new default? */
  1273. if(c & 0x40)
  1274. pi->defctab = ct;
  1275. /* memory wait specified? */
  1276. if(c & 0x80){
  1277. if(readc(cis, &i) != 1)
  1278. return;
  1279. if(i&0x80)
  1280. ct->memwait = 1;
  1281. }
  1282. if(readc(cis, &feature) != 1)
  1283. return;
  1284. switch(feature&0x3){
  1285. case 1:
  1286. ct->vpp1 = ct->vpp2 = power(cis);
  1287. break;
  1288. case 2:
  1289. power(cis);
  1290. ct->vpp1 = ct->vpp2 = power(cis);
  1291. break;
  1292. case 3:
  1293. power(cis);
  1294. ct->vpp1 = power(cis);
  1295. ct->vpp2 = power(cis);
  1296. break;
  1297. default:
  1298. break;
  1299. }
  1300. if(feature&0x4)
  1301. timing(cis, ct);
  1302. if(feature&0x8)
  1303. iospaces(cis, ct);
  1304. if(feature&0x10)
  1305. irq(cis, ct);
  1306. switch((feature>>5)&0x3){
  1307. case 1:
  1308. memspace(cis, 0, 2, 0);
  1309. break;
  1310. case 2:
  1311. memspace(cis, 2, 2, 0);
  1312. break;
  1313. case 3:
  1314. if(readc(cis, &c) != 1)
  1315. return;
  1316. for(i = 0; i <= (c&0x7); i++)
  1317. memspace(cis, (c>>5)&0x3, (c>>3)&0x3, c&0x80);
  1318. break;
  1319. }
  1320. }
  1321. static void
  1322. i82365probe(Cardbus *cb, int lindex, int ldata)
  1323. {
  1324. uchar c, id;
  1325. int dev = 0; /* According to the Ricoh spec 00->3F _and_ 80->BF seem
  1326. to be the same socket A (ditto for B). */
  1327. outb(lindex, Rid + (dev<<7));
  1328. id = inb(ldata);
  1329. if((id & 0xf0) != 0x80)
  1330. return; /* not a memory & I/O card */
  1331. if((id & 0x0f) == 0x00)
  1332. return; /* no revision number, not possible */
  1333. cb->lindex = lindex;
  1334. cb->ldata = ldata;
  1335. cb->ltype = Ti82365;
  1336. cb->lbase = (int)(cb - cbslots) * 0x40;
  1337. switch(id){
  1338. case 0x82:
  1339. case 0x83:
  1340. case 0x84:
  1341. /* could be a cirrus */
  1342. outb(cb->lindex, Rchipinfo + (dev<<7));
  1343. outb(cb->ldata, 0);
  1344. c = inb(cb->ldata);
  1345. if((c & 0xc0) != 0xc0)
  1346. break;
  1347. c = inb(cb->ldata);
  1348. if((c & 0xc0) != 0x00)
  1349. break;
  1350. if(c & 0x20){
  1351. cb->ltype = Tpd6720;
  1352. } else {
  1353. cb->ltype = Tpd6710;
  1354. }
  1355. break;
  1356. }
  1357. /* if it's not a Cirrus, it could be a Vadem... */
  1358. if(cb->ltype == Ti82365){
  1359. /* unlock the Vadem extended regs */
  1360. outb(cb->lindex, 0x0E + (dev<<7));
  1361. outb(cb->lindex, 0x37 + (dev<<7));
  1362. /* make the id register show the Vadem id */
  1363. outb(cb->lindex, 0x3A + (dev<<7));
  1364. c = inb(cb->ldata);
  1365. outb(cb->ldata, c|0xC0);
  1366. outb(cb->lindex, Rid + (dev<<7));
  1367. c = inb(cb->ldata);
  1368. if(c & 0x08)
  1369. cb->ltype = Tvg46x;
  1370. /* go back to Intel compatible id */
  1371. outb(cb->lindex, 0x3A + (dev<<7));
  1372. c = inb(cb->ldata);
  1373. outb(cb->ldata, c & ~0xC0);
  1374. }
  1375. }
  1376. static int
  1377. vcode(int volt)
  1378. {
  1379. switch(volt){
  1380. case 5:
  1381. return 1;
  1382. case 12:
  1383. return 2;
  1384. default:
  1385. return 0;
  1386. }
  1387. }