devpccard.c 39 KB

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