devpccard.c 39 KB

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