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. Nslots = 4, /* Maximum number of CardBus slots to use */
  24. K = 1024,
  25. M = K * K,
  26. LegacyAddr = 0x3e0,
  27. NUMEVENTS = 10,
  28. TI1131xSC = 0x80, // system control
  29. TI122X_SC_INTRTIE = 1 << 29,
  30. TI12xxIM = 0x8c, //
  31. TI1131xCC = 0x91, // card control
  32. TI113X_CC_RIENB = 1 << 7,
  33. TI113X_CC_ZVENABLE = 1 << 6,
  34. TI113X_CC_PCI_IRQ_ENA = 1 << 5,
  35. TI113X_CC_PCI_IREQ = 1 << 4,
  36. TI113X_CC_PCI_CSC = 1 << 3,
  37. TI113X_CC_SPKROUTEN = 1 << 1,
  38. TI113X_CC_IFG = 1 << 0,
  39. TI1131xDC = 0x92, // device control
  40. };
  41. typedef struct Variant Variant;
  42. struct Variant {
  43. ushort vid;
  44. ushort did;
  45. char *name;
  46. };
  47. static Variant variant[] = {
  48. { Ricoh_vid, Ricoh_475_did, "Ricoh 475 PCI/Cardbus bridge", },
  49. { Ricoh_vid, Ricoh_476_did, "Ricoh 476 PCI/Cardbus bridge", },
  50. { Ricoh_vid, Ricoh_478_did, "Ricoh 478 PCI/Cardbus bridge", },
  51. { TI_vid, TI_1131_did, "TI PCI-1131 Cardbus Controller", },
  52. { TI_vid, TI_1250_did, "TI PCI-1250 Cardbus Controller", },
  53. { TI_vid, TI_1450_did, "TI PCI-1450 Cardbus Controller", },
  54. { TI_vid, TI_1251A_did, "TI PCI-1251A Cardbus Controller", },
  55. { TI_vid, TI_1420_did, "TI PCI-1420 Cardbus Controller", },
  56. };
  57. /* Cardbus registers */
  58. enum {
  59. SocketEvent = 0,
  60. SE_CCD = 3 << 1,
  61. SE_POWER = 1 << 3,
  62. SocketMask = 1,
  63. SocketState = 2,
  64. SS_CCD = 3 << 1,
  65. SS_POWER = 1 << 3,
  66. SS_PC16 = 1 << 4,
  67. SS_CBC = 1 << 5,
  68. SS_NOTCARD = 1 << 7,
  69. SS_BADVCC = 1 << 9,
  70. SS_5V = 1 << 10,
  71. SS_3V = 1 << 11,
  72. SocketForce = 3,
  73. SocketControl = 4,
  74. SC_5V = 0x22,
  75. SC_3V = 0x33,
  76. };
  77. enum {
  78. PciPCR_IO = 1 << 0,
  79. PciPCR_MEM = 1 << 1,
  80. PciPCR_Master = 1 << 2,
  81. PciPMC = 0xa4,
  82. Nbars = 6,
  83. Ncmd = 10,
  84. CBIRQ = 9,
  85. PC16,
  86. PC32,
  87. };
  88. enum {
  89. Ti82365,
  90. Tpd6710,
  91. Tpd6720,
  92. Tvg46x,
  93. };
  94. static char *chipname[] = {
  95. [Ti82365] "Intel 82365SL",
  96. [Tpd6710] "Cirrus Logic PD6710",
  97. [Tpd6720] "Cirrus Logic PD6720",
  98. [Tvg46x] "Vadem VG-46x",
  99. };
  100. /*
  101. * Intel 82365SL PCIC controller for the PCMCIA or
  102. * Cirrus Logic PD6710/PD6720 which is mostly register compatible
  103. */
  104. enum
  105. {
  106. /*
  107. * registers indices
  108. */
  109. Rid= 0x0, /* identification and revision */
  110. Ris= 0x1, /* interface status */
  111. Rpc= 0x2, /* power control */
  112. Foutena= (1<<7), /* output enable */
  113. Fautopower= (1<<5), /* automatic power switching */
  114. Fcardena= (1<<4), /* PC card enable */
  115. Rigc= 0x3, /* interrupt and general control */
  116. Fiocard= (1<<5), /* I/O card (vs memory) */
  117. Fnotreset= (1<<6), /* reset if not set */
  118. FSMIena= (1<<4), /* enable change interrupt on SMI */
  119. Rcsc= 0x4, /* card status change */
  120. Rcscic= 0x5, /* card status change interrupt config */
  121. Fchangeena= (1<<3), /* card changed */
  122. Fbwarnena= (1<<1), /* card battery warning */
  123. Fbdeadena= (1<<0), /* card battery dead */
  124. Rwe= 0x6, /* address window enable */
  125. Fmem16= (1<<5), /* use A23-A12 to decode address */
  126. Rio= 0x7, /* I/O control */
  127. Fwidth16= (1<<0), /* 16 bit data width */
  128. Fiocs16= (1<<1), /* IOCS16 determines data width */
  129. Fzerows= (1<<2), /* zero wait state */
  130. Ftiming= (1<<3), /* timing register to use */
  131. Riobtm0lo= 0x8, /* I/O address 0 start low byte */
  132. Riobtm0hi= 0x9, /* I/O address 0 start high byte */
  133. Riotop0lo= 0xa, /* I/O address 0 stop low byte */
  134. Riotop0hi= 0xb, /* I/O address 0 stop high byte */
  135. Riobtm1lo= 0xc, /* I/O address 1 start low byte */
  136. Riobtm1hi= 0xd, /* I/O address 1 start high byte */
  137. Riotop1lo= 0xe, /* I/O address 1 stop low byte */
  138. Riotop1hi= 0xf, /* I/O address 1 stop high byte */
  139. Rmap= 0x10, /* map 0 */
  140. /*
  141. * CL-PD67xx extension registers
  142. */
  143. Rmisc1= 0x16, /* misc control 1 */
  144. F5Vdetect= (1<<0),
  145. Fvcc3V= (1<<1),
  146. Fpmint= (1<<2),
  147. Fpsirq= (1<<3),
  148. Fspeaker= (1<<4),
  149. Finpack= (1<<7),
  150. Rfifo= 0x17, /* fifo control */
  151. Fflush= (1<<7), /* flush fifo */
  152. Rmisc2= 0x1E, /* misc control 2 */
  153. Flowpow= (1<<1), /* low power mode */
  154. Rchipinfo= 0x1F, /* chip information */
  155. Ratactl= 0x26, /* ATA control */
  156. /*
  157. * offsets into the system memory address maps
  158. */
  159. Mbtmlo= 0x0, /* System mem addr mapping start low byte */
  160. Mbtmhi= 0x1, /* System mem addr mapping start high byte */
  161. F16bit= (1<<7), /* 16-bit wide data path */
  162. Mtoplo= 0x2, /* System mem addr mapping stop low byte */
  163. Mtophi= 0x3, /* System mem addr mapping stop high byte */
  164. Ftimer1= (1<<6), /* timer set 1 */
  165. Mofflo= 0x4, /* Card memory offset address low byte */
  166. Moffhi= 0x5, /* Card memory offset address high byte */
  167. Fregactive= (1<<6), /* attribute memory */
  168. /*
  169. * configuration registers - they start at an offset in attribute
  170. * memory found in the CIS.
  171. */
  172. Rconfig= 0,
  173. Creset= (1<<7), /* reset device */
  174. Clevel= (1<<6), /* level sensitive interrupt line */
  175. };
  176. /*
  177. * read and crack the card information structure enough to set
  178. * important parameters like power
  179. */
  180. /* cis memory walking */
  181. typedef struct Cisdat Cisdat;
  182. struct Cisdat {
  183. uchar *cisbase;
  184. int cispos;
  185. int cisskip;
  186. int cislen;
  187. };
  188. typedef struct Pcminfo Pcminfo;
  189. struct Pcminfo {
  190. char verstr[512]; /* Version string */
  191. PCMmap mmap[4]; /* maps, last is always for the kernel */
  192. ulong conf_addr; /* Config address */
  193. uchar conf_present; /* Config register present */
  194. int nctab; /* In use configuration tables */
  195. PCMconftab ctab[8]; /* Configuration tables */
  196. PCMconftab *defctab; /* Default conftab */
  197. int port; /* Actual port usage */
  198. int irq; /* Actual IRQ usage */
  199. };
  200. typedef struct Cardbus Cardbus;
  201. struct Cardbus {
  202. Lock;
  203. Variant *variant; /* Which CardBus chipset */
  204. Pcidev *pci; /* The bridge itself */
  205. ulong *regs; /* Cardbus registers */
  206. int ltype; /* Legacy type */
  207. int lindex; /* Legacy port index address */
  208. int ldata; /* Legacy port data address */
  209. int lbase; /* Base register for this socket */
  210. int state; /* Current state of card */
  211. int type; /* Type of card */
  212. Pcminfo linfo; /* PCMCIA slot info */
  213. int special; /* card is allocated to a driver */
  214. int refs; /* Number of refs to slot */
  215. Lock refslock; /* inc/dev ref lock */
  216. };
  217. static int managerstarted;
  218. enum {
  219. Mshift= 12,
  220. Mgran= (1<<Mshift), /* granularity of maps */
  221. Mmask= ~(Mgran-1), /* mask for address bits important to the chip */
  222. };
  223. static Cardbus cbslots[Nslots];
  224. static int nslots;
  225. static ulong exponent[8] = {
  226. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  227. };
  228. static ulong vmant[16] = {
  229. 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80, 90,
  230. };
  231. static ulong mantissa[16] = {
  232. 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80,
  233. };
  234. static char Enocard[] = "No card in slot";
  235. enum
  236. {
  237. CMdown,
  238. CMpower,
  239. };
  240. static Cmdtab pccardctlmsg[] =
  241. {
  242. CMdown, "down", 2,
  243. CMpower, "power", 1,
  244. };
  245. static void cbint(Ureg *, void *);
  246. static int powerup(Cardbus *);
  247. static void configure(Cardbus *);
  248. static void managecard(Cardbus *);
  249. static void cardmanager(void *);
  250. static void eject(Cardbus *);
  251. static void interrupt(Ureg *, void *);
  252. static void powerdown(Cardbus *cb);
  253. static void unconfigure(Cardbus *cb);
  254. static void i82365probe(Cardbus *cb, int lindex, int ldata);
  255. static void i82365configure(Cardbus *cb);
  256. static PCMmap *isamap(Cardbus *cb, ulong offset, int len, int attr);
  257. static void isaunmap(PCMmap* m);
  258. static uchar rdreg(Cardbus *cb, int index);
  259. static void wrreg(Cardbus *cb, int index, uchar val);
  260. static int readc(Cisdat *cis, uchar *x);
  261. static void tvers1(Cardbus *cb, Cisdat *cis, int );
  262. static void tcfig(Cardbus *cb, Cisdat *cis, int );
  263. static void tentry(Cardbus *cb, Cisdat *cis, int );
  264. static int vcode(int volt);
  265. static int pccard_pcmspecial(char *idstr, ISAConf *isa);
  266. static void pccard_pcmspecialclose(int slotno);
  267. enum {
  268. CardDetected,
  269. CardPowered,
  270. CardEjected,
  271. CardConfigured,
  272. };
  273. static char *messages[] = {
  274. [CardDetected] "CardDetected",
  275. [CardPowered] "CardPowered",
  276. [CardEjected] "CardEjected",
  277. [CardConfigured] "CardConfigured",
  278. };
  279. enum {
  280. SlotEmpty,
  281. SlotFull,
  282. SlotPowered,
  283. SlotConfigured,
  284. };
  285. static char *states[] = {
  286. [SlotEmpty] "SlotEmpty",
  287. [SlotFull] "SlotFull",
  288. [SlotPowered] "SlotPowered",
  289. [SlotConfigured] "SlotConfigured",
  290. };
  291. static void
  292. engine(Cardbus *cb, int message)
  293. {
  294. //print("engine(%d): %s(%s)\n",
  295. // (int)(cb - cbslots), states[cb->state], messages[message]);
  296. switch (cb->state) {
  297. case SlotEmpty:
  298. switch (message) {
  299. case CardDetected:
  300. cb->state = SlotFull;
  301. powerup(cb);
  302. break;
  303. case CardEjected:
  304. break;
  305. default:
  306. //print("#Y%d: Invalid message %s in SlotEmpty state\n",
  307. // (int)(cb - cbslots), messages[message]);
  308. break;
  309. }
  310. break;
  311. case SlotFull:
  312. switch (message) {
  313. case CardPowered:
  314. cb->state = SlotPowered;
  315. configure(cb);
  316. break;
  317. case CardEjected:
  318. cb->state = SlotEmpty;
  319. powerdown(cb);
  320. break;
  321. default:
  322. //print("#Y%d: Invalid message %s in SlotFull state\n",
  323. // (int)(cb - cbslots), messages[message]);
  324. break;
  325. }
  326. break;
  327. case SlotPowered:
  328. switch (message) {
  329. case CardConfigured:
  330. cb->state = SlotConfigured;
  331. break;
  332. case CardEjected:
  333. cb->state = SlotEmpty;
  334. unconfigure(cb);
  335. powerdown(cb);
  336. break;
  337. default:
  338. //print("#Y%d: Invalid message %s in SlotPowered state\n",
  339. // (int)(cb - cbslots), messages[message]);
  340. break;
  341. }
  342. break;
  343. case SlotConfigured:
  344. switch (message) {
  345. case CardEjected:
  346. cb->state = SlotEmpty;
  347. unconfigure(cb);
  348. powerdown(cb);
  349. break;
  350. default:
  351. //print("#Y%d: Invalid message %s in SlotConfigured state\n",
  352. // (int)(cb - cbslots), messages[message]);
  353. break;
  354. }
  355. break;
  356. }
  357. }
  358. static void
  359. qengine(Cardbus *cb, int message)
  360. {
  361. lock(cb);
  362. engine(cb, message);
  363. unlock(cb);
  364. }
  365. typedef struct Events Events;
  366. struct Events {
  367. Cardbus *cb;
  368. int message;
  369. };
  370. static Lock levents;
  371. static Events events[NUMEVENTS];
  372. static Rendez revents;
  373. static int nevents;
  374. static void
  375. iengine(Cardbus *cb, int message)
  376. {
  377. if (nevents >= NUMEVENTS) {
  378. print("#Y: Too many events queued, discarding request\n");
  379. return;
  380. }
  381. ilock(&levents);
  382. events[nevents].cb = cb;
  383. events[nevents].message = message;
  384. nevents++;
  385. iunlock(&levents);
  386. wakeup(&revents);
  387. }
  388. static int
  389. eventoccured(void)
  390. {
  391. return nevents > 0;
  392. }
  393. static void
  394. processevents(void *)
  395. {
  396. while (1) {
  397. int message;
  398. Cardbus *cb;
  399. sleep(&revents, (int (*)(void *))eventoccured, nil);
  400. cb = nil;
  401. message = 0;
  402. ilock(&levents);
  403. if (nevents > 0) {
  404. cb = events[0].cb;
  405. message = events[0].message;
  406. nevents--;
  407. if (nevents > 0)
  408. memmove(events, &events[1], nevents * sizeof(Events));
  409. }
  410. iunlock(&levents);
  411. if (cb)
  412. qengine(cb, message);
  413. }
  414. }
  415. static void
  416. cbinterrupt(Ureg *, void *)
  417. {
  418. int i;
  419. for (i = 0; i != nslots; i++) {
  420. Cardbus *cb = &cbslots[i];
  421. ulong event, state;
  422. event= cb->regs[SocketEvent];
  423. state = cb->regs[SocketState];
  424. rdreg(cb, Rcsc); /* Ack the interrupt */
  425. //print("interrupt: slot %d, event %.8lX, state %.8lX, (%s)\n",
  426. // (int)(cb - cbslots), event, state, states[cb->state]);
  427. if (event & SE_CCD) {
  428. cb->regs[SocketEvent] |= SE_CCD; /* Ack interrupt */
  429. if (state & SE_CCD) {
  430. if (cb->state != SlotEmpty) {
  431. print("#Y: take cardejected interrupt\n");
  432. iengine(cb, CardEjected);
  433. }
  434. }
  435. else
  436. iengine(cb, CardDetected);
  437. }
  438. if (event & SE_POWER) {
  439. cb->regs[SocketEvent] |= SE_POWER; /* Ack interrupt */
  440. iengine(cb, CardPowered);
  441. }
  442. }
  443. }
  444. void
  445. devpccardlink(void)
  446. {
  447. static int initialized;
  448. Pcidev *pci;
  449. int i;
  450. uchar intl;
  451. char *p;
  452. if (initialized)
  453. return;
  454. initialized = 1;
  455. if((p=getconf("pccard0")) && strncmp(p, "disabled", 8)==0)
  456. return;
  457. if(_pcmspecial)
  458. return;
  459. /* Allocate legacy space */
  460. if (ioalloc(LegacyAddr, 2, 0, "i82365.0") < 0)
  461. print("#Y: WARNING: Cannot allocate legacy ports\n");
  462. /* Find all CardBus controllers */
  463. pci = nil;
  464. intl = (uchar)-1;
  465. while ((pci = pcimatch(pci, 0, 0)) != nil) {
  466. ulong baddr;
  467. Cardbus *cb;
  468. int slot;
  469. uchar pin;
  470. for (i = 0; i != nelem(variant); i++)
  471. if (pci->vid == variant[i].vid && pci->did == variant[i].did)
  472. break;
  473. if (i == nelem(variant))
  474. continue;
  475. /* initialize this slot */
  476. slot = nslots++;
  477. cb = &cbslots[slot];
  478. cb->pci = pci;
  479. cb->variant = &variant[i];
  480. if (pci->vid != TI_vid) {
  481. // Gross hack, needs a fix. Inherit the mappings from 9load
  482. // for the TIs (pb)
  483. pcicfgw32(pci, PciCBMBR0, 0xffffffff);
  484. pcicfgw32(pci, PciCBMLR0, 0);
  485. pcicfgw32(pci, PciCBMBR1, 0xffffffff);
  486. pcicfgw32(pci, PciCBMLR1, 0);
  487. pcicfgw32(pci, PciCBIBR0, 0xffffffff);
  488. pcicfgw32(pci, PciCBILR0, 0);
  489. pcicfgw32(pci, PciCBIBR1, 0xffffffff);
  490. pcicfgw32(pci, PciCBILR1, 0);
  491. }
  492. // Set up PCI bus numbers if needed.
  493. if (pcicfgr8(pci, PciSBN) == 0) {
  494. static int busbase = 0x20;
  495. pcicfgw8(pci, PciSBN, busbase);
  496. pcicfgw8(pci, PciUBN, busbase + 2);
  497. busbase += 3;
  498. }
  499. // Patch up intl if needed.
  500. if ((pin = pcicfgr8(pci, PciINTP)) != 0 &&
  501. (pci->intl == 0xff || pci->intl == 0)) {
  502. pci->intl = pciipin(nil, pin);
  503. pcicfgw8(pci, PciINTL, pci->intl);
  504. if (pci->intl == 0xff || pci->intl == 0)
  505. print("#Y%d: No interrupt?\n", (int)(cb - cbslots));
  506. }
  507. // Don't you love standards!
  508. if (pci->vid == TI_vid) {
  509. if (pci->did <= TI_1131_did) {
  510. uchar cc;
  511. cc = pcicfgr8(pci, TI1131xCC);
  512. cc &= ~(TI113X_CC_PCI_IRQ_ENA |
  513. TI113X_CC_PCI_IREQ |
  514. TI113X_CC_PCI_CSC |
  515. TI113X_CC_ZVENABLE);
  516. cc |= TI113X_CC_PCI_IRQ_ENA |
  517. TI113X_CC_PCI_IREQ |
  518. TI113X_CC_SPKROUTEN;
  519. pcicfgw8(pci, TI1131xCC, cc);
  520. // PCI interrupts only
  521. pcicfgw8(pci, TI1131xDC,
  522. pcicfgr8(pci, TI1131xDC) & ~6);
  523. // CSC ints to PCI bus.
  524. wrreg(cb, Rigc, rdreg(cb, Rigc) | 0x10);
  525. }
  526. else if (pci->did == TI_1250_did) {
  527. print("No support yet for the TI_1250_did, prod pb\n");
  528. }
  529. else if (pci->did == TI_1420_did) {
  530. // Disable Vcc protection
  531. pcicfgw32(cb->pci, 0x80,
  532. pcicfgr32(cb->pci, 0x80) | (1 << 21));
  533. }
  534. pcicfgw16(cb->pci, PciPMC, pcicfgr16(cb->pci, PciPMC) & ~3);
  535. }
  536. if (intl != -1 && intl != pci->intl)
  537. intrenable(pci->intl, cbinterrupt, cb, pci->tbdf, "cardbus");
  538. intl = pci->intl;
  539. if ((baddr = pcicfgr32(cb->pci, PciBAR0)) == 0) {
  540. int align = (pci->did == Ricoh_478_did)? 0x10000: 0x1000;
  541. baddr = upamalloc(baddr, align, align);
  542. pcicfgw32(cb->pci, PciBAR0, baddr);
  543. cb->regs = (ulong *)KADDR(baddr);
  544. }
  545. else
  546. cb->regs = (ulong *)KADDR(upamalloc(baddr, 4096, 0));
  547. cb->state = SlotEmpty;
  548. /* Don't really know what to do with this... */
  549. i82365probe(cb, LegacyAddr, LegacyAddr + 1);
  550. print("#Y%ld: %s, %.8ulX intl %d\n", cb - cbslots,
  551. variant[i].name, baddr, pci->intl);
  552. }
  553. if (nslots == 0){
  554. iofree(LegacyAddr);
  555. return;
  556. }
  557. _pcmspecial = pccard_pcmspecial;
  558. _pcmspecialclose = pccard_pcmspecialclose;
  559. for (i = 0; i != nslots; i++) {
  560. Cardbus *cb = &cbslots[i];
  561. if ((cb->regs[SocketState] & SE_CCD) == 0)
  562. engine(cb, CardDetected);
  563. }
  564. delay(500); /* Allow time for power up */
  565. for (i = 0; i != nslots; i++) {
  566. Cardbus *cb = &cbslots[i];
  567. if (cb->regs[SocketState] & SE_POWER)
  568. engine(cb, CardPowered);
  569. /* Ack and enable interrupts on all events */
  570. // cb->regs[SocketEvent] = cb->regs[SocketEvent];
  571. cb->regs[SocketMask] |= 0xF;
  572. wrreg(cb, Rcscic, 0xC);
  573. }
  574. }
  575. static int
  576. powerup(Cardbus *cb)
  577. {
  578. ulong state;
  579. ushort bcr;
  580. state = cb->regs[SocketState];
  581. if (state & SS_PC16) {
  582. // print("#Y%ld: Probed a PC16 card, powering up card\n", cb - cbslots);
  583. cb->type = PC16;
  584. memset(&cb->linfo, 0, sizeof(Pcminfo));
  585. /* power up and unreset, wait's are empirical (???) */
  586. wrreg(cb, Rpc, Fautopower|Foutena|Fcardena);
  587. delay(300);
  588. wrreg(cb, Rigc, 0);
  589. delay(100);
  590. wrreg(cb, Rigc, Fnotreset);
  591. delay(500);
  592. return 1;
  593. }
  594. if (state & SS_CCD)
  595. return 0;
  596. if (state & SS_NOTCARD) {
  597. print("#Y%ld: Not a card inserted\n", cb - cbslots);
  598. return 0;
  599. }
  600. if ((state & SS_3V) == 0 && (state & SS_5V) == 0) {
  601. print("#Y%ld: Unsupported voltage, powering down card!\n",
  602. cb - cbslots);
  603. cb->regs[SocketControl] = 0;
  604. return 0;
  605. }
  606. //print("#Y%ld: card %spowered at %d volt\n", cb - cbslots,
  607. // (state & SS_POWER)? "": "not ",
  608. // (state & SS_3V)? 3: (state & SS_5V)? 5: -1);
  609. /* Power up the card
  610. * and make sure the secondary bus is not in reset.
  611. */
  612. cb->regs[SocketControl] = (state & SS_5V)? SC_5V: SC_3V;
  613. delay(50);
  614. bcr = pcicfgr16(cb->pci, PciBCR);
  615. bcr &= ~0x40;
  616. pcicfgw16(cb->pci, PciBCR, bcr);
  617. delay(100);
  618. cb->type = (state & SS_PC16)? PC16: PC32;
  619. return 1;
  620. }
  621. static void
  622. powerdown(Cardbus *cb)
  623. {
  624. ushort bcr;
  625. if (cb->type == PC16) {
  626. wrreg(cb, Rpc, 0); /* turn off card power */
  627. wrreg(cb, Rwe, 0); /* no windows */
  628. cb->type = -1;
  629. return;
  630. }
  631. bcr = pcicfgr16(cb->pci, PciBCR);
  632. bcr |= 0x40;
  633. pcicfgw16(cb->pci, PciBCR, bcr);
  634. cb->regs[SocketControl] = 0;
  635. cb->type = -1;
  636. }
  637. static void
  638. configure(Cardbus *cb)
  639. {
  640. int i;
  641. Pcidev *pci;
  642. //print("configuring slot %d (%s)\n", (int)(cb - cbslots), states[cb->state]);
  643. if (cb->state == SlotConfigured)
  644. return;
  645. engine(cb, CardConfigured);
  646. delay(50); /* Emperically established */
  647. if (cb->type == PC16) {
  648. i82365configure(cb);
  649. return;
  650. }
  651. /* Scan the CardBus for new PCI devices */
  652. pciscan(pcicfgr8(cb->pci, PciSBN), &cb->pci->bridge);
  653. pci = cb->pci->bridge;
  654. while (pci) {
  655. ulong size, bar;
  656. int memindex, ioindex;
  657. pcicfgw16(pci, PciPCR,
  658. pcicfgr16(pci, PciPCR) & ~(PciPCR_IO|PciPCR_MEM));
  659. /* Treat the found device as an ordinary PCI card. It seems that the
  660. CIS is not always present in CardBus cards. XXX, need to support
  661. multifunction cards */
  662. memindex = ioindex = 0;
  663. for (i = 0; i != Nbars; i++) {
  664. if (pci->mem[i].size == 0) continue;
  665. if (pci->mem[i].bar & 1) {
  666. // Allocate I/O space
  667. if (ioindex > 1) {
  668. print("#Y%ld: WARNING: Can only configure 2 I/O slots\n", cb - cbslots);
  669. continue;
  670. }
  671. bar = ioreserve(-1, pci->mem[i].size, 0, "cardbus");
  672. pci->mem[i].bar = bar | 1;
  673. pcicfgw32(pci, PciBAR0 + i * sizeof(ulong),
  674. pci->mem[i].bar);
  675. pcicfgw16(cb->pci, PciCBIBR0 + ioindex * 8, bar);
  676. pcicfgw16(cb->pci, PciCBILR0 + ioindex * 8,
  677. bar + pci->mem[i].size - 1);
  678. //print("ioindex[%d] %.8uX (%d)\n",
  679. // ioindex, bar, pci->mem[i].size);
  680. ioindex++;
  681. continue;
  682. }
  683. // Allocating memory space
  684. if (memindex > 1) {
  685. print("#Y%ld: WARNING: Can only configure 2 memory slots\n", cb - cbslots);
  686. continue;
  687. }
  688. bar = upamalloc(0, pci->mem[i].size, BY2PG);
  689. pci->mem[i].bar = bar | (pci->mem[i].bar & 0x80);
  690. pcicfgw32(pci, PciBAR0 + i * sizeof(ulong), pci->mem[i].bar);
  691. pcicfgw32(cb->pci, PciCBMBR0 + memindex * 8, bar);
  692. pcicfgw32(cb->pci, PciCBMLR0 + memindex * 8,
  693. bar + pci->mem[i].size - 1);
  694. if (pci->mem[i].bar & 0x80)
  695. /* Enable prefetch */
  696. pcicfgw16(cb->pci, PciBCR,
  697. pcicfgr16(cb->pci, PciBCR) |
  698. (1 << (8 + memindex)));
  699. //print("memindex[%d] %.8uX (%d)\n",
  700. // memindex, bar, pci->mem[i].size);
  701. memindex++;
  702. }
  703. if ((size = pcibarsize(pci, PciEBAR0)) > 0) {
  704. if (memindex > 1)
  705. print("#Y%ld: WARNING: Too many memory spaces, not mapping ROM space\n",
  706. cb - cbslots);
  707. else {
  708. pci->rom.bar = upamalloc(0, size, BY2PG);
  709. pci->rom.size = size;
  710. pcicfgw32(pci, PciEBAR0, pci->rom.bar);
  711. pcicfgw32(cb->pci, PciCBMBR0 + memindex * 8,
  712. pci->rom.bar);
  713. pcicfgw32(cb->pci, PciCBMLR0 + memindex * 8,
  714. pci->rom.bar + pci->rom.size - 1);
  715. }
  716. }
  717. /* Set the basic PCI registers for the device */
  718. pcicfgw16(pci, PciPCR,
  719. pcicfgr16(pci, PciPCR) |
  720. PciPCR_IO|PciPCR_MEM|PciPCR_Master);
  721. pcicfgw8(pci, PciCLS, 8);
  722. pcicfgw8(pci, PciLTR, 64);
  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. }