devpccard.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  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. pci->pcr = pcicfgr16(pci, PciPCR) | PciPCR_IO|PciPCR_MEM|PciPCR_Master;
  719. pci->cls = 8;
  720. pci->ltr = 64;
  721. pcicfgw16(pci, PciPCR, pci->pcr);
  722. pcicfgw8(pci, PciCLS, pci->cls);
  723. pcicfgw8(pci, PciLTR, pci->ltr);
  724. if (pcicfgr8(pci, PciINTP)) {
  725. pci->intl = pcicfgr8(cb->pci, PciINTL);
  726. pcicfgw8(pci, PciINTL, pci->intl);
  727. /* Route interrupts to INTA#/B# */
  728. pcicfgw16(cb->pci, PciBCR,
  729. pcicfgr16(cb->pci, PciBCR) & ~(1 << 7));
  730. }
  731. pci = pci->list;
  732. }
  733. }
  734. static void
  735. unconfigure(Cardbus *cb)
  736. {
  737. Pcidev *pci;
  738. int i, ioindex, memindex;
  739. if (cb->type == PC16) {
  740. print("#Y%d: Don't know how to unconfigure a PC16 card\n",
  741. (int)(cb - cbslots));
  742. memset(&cb->linfo, 0, sizeof(Pcminfo));
  743. return;
  744. }
  745. pci = cb->pci->bridge;
  746. if (pci == nil)
  747. return; /* Not configured */
  748. cb->pci->bridge = nil;
  749. memindex = ioindex = 0;
  750. while (pci) {
  751. Pcidev *_pci;
  752. for (i = 0; i != Nbars; i++) {
  753. if (pci->mem[i].size == 0) continue;
  754. if (pci->mem[i].bar & 1) {
  755. iofree(pci->mem[i].bar & ~1);
  756. pcicfgw16(cb->pci, PciCBIBR0 + ioindex * 8,
  757. (ushort)-1);
  758. pcicfgw16(cb->pci, PciCBILR0 + ioindex * 8, 0);
  759. ioindex++;
  760. continue;
  761. }
  762. upafree(pci->mem[i].bar & ~0xF, pci->mem[i].size);
  763. pcicfgw32(cb->pci, PciCBMBR0 + memindex * 8,
  764. (ulong)-1);
  765. pcicfgw32(cb->pci, PciCBMLR0 + memindex * 8, 0);
  766. pcicfgw16(cb->pci, PciBCR,
  767. pcicfgr16(cb->pci, PciBCR) &
  768. ~(1 << (8 + memindex)));
  769. memindex++;
  770. }
  771. if (pci->rom.bar && memindex < 2) {
  772. upafree(pci->rom.bar & ~0xF, pci->rom.size);
  773. pcicfgw32(cb->pci, PciCBMBR0 + memindex * 8,
  774. (ulong)-1);
  775. pcicfgw32(cb->pci, PciCBMLR0 + memindex * 8, 0);
  776. memindex++;
  777. }
  778. _pci = pci->list;
  779. free(_pci);
  780. pci = _pci;
  781. }
  782. }
  783. static void
  784. i82365configure(Cardbus *cb)
  785. {
  786. int this;
  787. Cisdat cis;
  788. PCMmap *m;
  789. uchar type, link;
  790. /*
  791. * Read all tuples in attribute space.
  792. */
  793. m = isamap(cb, 0, 0, 1);
  794. if(m == 0)
  795. return;
  796. cis.cisbase = KADDR(m->isa);
  797. cis.cispos = 0;
  798. cis.cisskip = 2;
  799. cis.cislen = m->len;
  800. /* loop through all the tuples */
  801. for(;;){
  802. this = cis.cispos;
  803. if(readc(&cis, &type) != 1)
  804. break;
  805. if(type == 0xFF)
  806. break;
  807. if(readc(&cis, &link) != 1)
  808. break;
  809. switch(type){
  810. default:
  811. break;
  812. case 0x15:
  813. tvers1(cb, &cis, type);
  814. break;
  815. case 0x1A:
  816. tcfig(cb, &cis, type);
  817. break;
  818. case 0x1B:
  819. tentry(cb, &cis, type);
  820. break;
  821. }
  822. if(link == 0xFF)
  823. break;
  824. cis.cispos = this + (2+link);
  825. }
  826. isaunmap(m);
  827. }
  828. /*
  829. * look for a card whose version contains 'idstr'
  830. */
  831. static int
  832. pccard_pcmspecial(char *idstr, ISAConf *isa)
  833. {
  834. int i, irq;
  835. PCMconftab *ct, *et;
  836. Pcminfo *pi;
  837. Cardbus *cb;
  838. uchar x, we, *p;
  839. cb = nil;
  840. for (i = 0; i != nslots; i++) {
  841. cb = &cbslots[i];
  842. lock(cb);
  843. if (cb->state == SlotConfigured &&
  844. cb->type == PC16 &&
  845. !cb->special &&
  846. strstr(cb->linfo.verstr, idstr))
  847. break;
  848. unlock(cb);
  849. }
  850. if (i == nslots) {
  851. // print("#Y: %s not found\n", idstr);
  852. return -1;
  853. }
  854. pi = &cb->linfo;
  855. /*
  856. * configure the PCMslot for IO. We assume very heavily that we can read
  857. * configuration info from the CIS. If not, we won't set up correctly.
  858. */
  859. irq = isa->irq;
  860. if(irq == 2)
  861. irq = 9;
  862. et = &pi->ctab[pi->nctab];
  863. ct = nil;
  864. for(i = 0; i < isa->nopt; i++){
  865. int index;
  866. char *cp;
  867. if(strncmp(isa->opt[i], "index=", 6))
  868. continue;
  869. index = strtol(&isa->opt[i][6], &cp, 0);
  870. if(cp == &isa->opt[i][6] || index >= pi->nctab) {
  871. unlock(cb);
  872. print("#Y%d: Cannot find index %d in conf table\n",
  873. (int)(cb - cbslots), index);
  874. return -1;
  875. }
  876. ct = &pi->ctab[index];
  877. }
  878. if(ct == nil){
  879. PCMconftab *t;
  880. /* assume default is right */
  881. if(pi->defctab)
  882. ct = pi->defctab;
  883. else
  884. ct = pi->ctab;
  885. /* try for best match */
  886. if(ct->nio == 0
  887. || ct->io[0].start != isa->port || ((1<<irq) & ct->irqs) == 0){
  888. for(t = pi->ctab; t < et; t++)
  889. if(t->nio
  890. && t->io[0].start == isa->port
  891. && ((1<<irq) & t->irqs)){
  892. ct = t;
  893. break;
  894. }
  895. }
  896. if(ct->nio == 0 || ((1<<irq) & ct->irqs) == 0){
  897. for(t = pi->ctab; t < et; t++)
  898. if(t->nio && ((1<<irq) & t->irqs)){
  899. ct = t;
  900. break;
  901. }
  902. }
  903. if(ct->nio == 0){
  904. for(t = pi->ctab; t < et; t++)
  905. if(t->nio){
  906. ct = t;
  907. break;
  908. }
  909. }
  910. }
  911. if(ct == et || ct->nio == 0) {
  912. unlock(cb);
  913. print("#Y%d: No configuration?\n", (int)(cb - cbslots));
  914. return -1;
  915. }
  916. if(isa->port == 0 && ct->io[0].start == 0) {
  917. unlock(cb);
  918. print("#Y%d: No part or start address\n", (int)(cb - cbslots));
  919. return -1;
  920. }
  921. cb->special = 1; /* taken */
  922. /* route interrupts */
  923. isa->irq = irq;
  924. wrreg(cb, Rigc, irq | Fnotreset | Fiocard);
  925. /* set power and enable device */
  926. x = vcode(ct->vpp1);
  927. wrreg(cb, Rpc, x|Fautopower|Foutena|Fcardena);
  928. /* 16-bit data path */
  929. if(ct->bit16)
  930. x = Ftiming|Fiocs16|Fwidth16;
  931. else
  932. x = Ftiming;
  933. if(ct->nio == 2 && ct->io[1].start)
  934. x |= x<<4;
  935. wrreg(cb, Rio, x);
  936. /*
  937. * enable io port map 0
  938. * the 'top' register value includes the last valid address
  939. */
  940. if(isa->port == 0)
  941. isa->port = ct->io[0].start;
  942. we = rdreg(cb, Rwe);
  943. wrreg(cb, Riobtm0lo, isa->port);
  944. wrreg(cb, Riobtm0hi, isa->port>>8);
  945. i = isa->port+ct->io[0].len-1;
  946. wrreg(cb, Riotop0lo, i);
  947. wrreg(cb, Riotop0hi, i>>8);
  948. we |= 1<<6;
  949. if(ct->nio == 2 && ct->io[1].start){
  950. wrreg(cb, Riobtm1lo, ct->io[1].start);
  951. wrreg(cb, Riobtm1hi, ct->io[1].start>>8);
  952. i = ct->io[1].start+ct->io[1].len-1;
  953. wrreg(cb, Riotop1lo, i);
  954. wrreg(cb, Riotop1hi, i>>8);
  955. we |= 1<<7;
  956. }
  957. wrreg(cb, Rwe, we);
  958. /* only touch Rconfig if it is present */
  959. if(pi->conf_present & (1<<Rconfig)){
  960. PCMmap *m;
  961. /* Reset adapter */
  962. m = isamap(cb, pi->conf_addr + Rconfig, 1, 1);
  963. p = KADDR(m->isa + pi->conf_addr + Rconfig - m->ca);
  964. /* set configuration and interrupt type */
  965. x = ct->index;
  966. if(ct->irqtype & 0x20)
  967. x |= Clevel;
  968. *p = x;
  969. delay(5);
  970. isaunmap(m);
  971. }
  972. pi->port = isa->port;
  973. pi->irq = isa->irq;
  974. unlock(cb);
  975. print("#Y%d: %s irq %d, port %lX\n", (int)(cb - cbslots), pi->verstr, isa->irq, isa->port);
  976. return (int)(cb - cbslots);
  977. }
  978. static void
  979. pccard_pcmspecialclose(int slotno)
  980. {
  981. Cardbus *cb = &cbslots[slotno];
  982. wrreg(cb, Rwe, 0); /* no windows */
  983. cb->special = 0;
  984. }
  985. static int
  986. xcistuple(int slotno, int tuple, int subtuple, void *v, int nv, int attr)
  987. {
  988. PCMmap *m;
  989. Cisdat cis;
  990. int i, l;
  991. uchar *p;
  992. uchar type, link, n, c;
  993. int this, subtype;
  994. Cardbus *cb = &cbslots[slotno];
  995. m = isamap(cb, 0, 0, attr);
  996. if(m == 0)
  997. return -1;
  998. cis.cisbase = KADDR(m->isa);
  999. cis.cispos = 0;
  1000. cis.cisskip = attr ? 2 : 1;
  1001. cis.cislen = m->len;
  1002. /* loop through all the tuples */
  1003. for(i = 0; i < 1000; i++){
  1004. this = cis.cispos;
  1005. if(readc(&cis, &type) != 1)
  1006. break;
  1007. if(type == 0xFF)
  1008. break;
  1009. if(readc(&cis, &link) != 1)
  1010. break;
  1011. if(link == 0xFF)
  1012. break;
  1013. n = link;
  1014. if (link > 1 && subtuple != -1) {
  1015. if (readc(&cis, &c) != 1)
  1016. break;
  1017. subtype = c;
  1018. n--;
  1019. } else
  1020. subtype = -1;
  1021. if(type == tuple && subtype == subtuple) {
  1022. p = v;
  1023. for(l=0; l<nv && l<n; l++)
  1024. if(readc(&cis, p++) != 1)
  1025. break;
  1026. isaunmap(m);
  1027. return nv;
  1028. }
  1029. cis.cispos = this + (2+link);
  1030. }
  1031. isaunmap(m);
  1032. return -1;
  1033. }
  1034. static Chan*
  1035. pccardattach(char *spec)
  1036. {
  1037. if (!managerstarted) {
  1038. managerstarted = 1;
  1039. kproc("cardbus", processevents, nil);
  1040. }
  1041. return devattach('Y', spec);
  1042. }
  1043. enum
  1044. {
  1045. Qdir,
  1046. Qctl,
  1047. Nents = 1,
  1048. };
  1049. #define SLOTNO(c) ((ulong)((c->qid.path>>8)&0xff))
  1050. #define TYPE(c) ((ulong)(c->qid.path&0xff))
  1051. #define QID(s,t) (((s)<<8)|(t))
  1052. static int
  1053. pccardgen(Chan *c, char*, Dirtab *, int , int i, Dir *dp)
  1054. {
  1055. int slotno;
  1056. Qid qid;
  1057. long len;
  1058. int entry;
  1059. if(i == DEVDOTDOT){
  1060. mkqid(&qid, Qdir, 0, QTDIR);
  1061. devdir(c, qid, "#Y", 0, eve, 0555, dp);
  1062. return 1;
  1063. }
  1064. len = 0;
  1065. if(i >= Nents * nslots) return -1;
  1066. slotno = i / Nents;
  1067. entry = i % Nents;
  1068. if (entry == 0) {
  1069. qid.path = QID(slotno, Qctl);
  1070. snprint(up->genbuf, sizeof up->genbuf, "cb%dctl", slotno);
  1071. }
  1072. else {
  1073. /* Entries for memory regions. I'll implement them when
  1074. needed. (pb) */
  1075. }
  1076. qid.vers = 0;
  1077. qid.type = QTFILE;
  1078. devdir(c, qid, up->genbuf, len, eve, 0660, dp);
  1079. return 1;
  1080. }
  1081. static Walkqid*
  1082. pccardwalk(Chan *c, Chan *nc, char **name, int nname)
  1083. {
  1084. return devwalk(c, nc, name, nname, 0, 0, pccardgen);
  1085. }
  1086. static int
  1087. pccardstat(Chan *c, uchar *db, int n)
  1088. {
  1089. return devstat(c, db, n, 0, 0, pccardgen);
  1090. }
  1091. static void
  1092. increfp(Cardbus *cb)
  1093. {
  1094. lock(&cb->refslock);
  1095. cb->refs++;
  1096. unlock(&cb->refslock);
  1097. }
  1098. static void
  1099. decrefp(Cardbus *cb)
  1100. {
  1101. lock(&cb->refslock);
  1102. cb->refs--;
  1103. unlock(&cb->refslock);
  1104. }
  1105. static Chan*
  1106. pccardopen(Chan *c, int omode)
  1107. {
  1108. if (c->qid.type & QTDIR){
  1109. if(omode != OREAD)
  1110. error(Eperm);
  1111. } else
  1112. increfp(&cbslots[SLOTNO(c)]);
  1113. c->mode = openmode(omode);
  1114. c->flag |= COPEN;
  1115. c->offset = 0;
  1116. return c;
  1117. }
  1118. static void
  1119. pccardclose(Chan *c)
  1120. {
  1121. if(c->flag & COPEN)
  1122. if((c->qid.type & QTDIR) == 0)
  1123. decrefp(&cbslots[SLOTNO(c)]);
  1124. }
  1125. static long
  1126. pccardread(Chan *c, void *a, long n, vlong offset)
  1127. {
  1128. Cardbus *cb;
  1129. char *buf, *p, *e;
  1130. int i;
  1131. switch(TYPE(c)){
  1132. case Qdir:
  1133. return devdirread(c, a, n, 0, 0, pccardgen);
  1134. case Qctl:
  1135. buf = p = malloc(READSTR);
  1136. buf[0] = 0;
  1137. e = p + READSTR;
  1138. cb = &cbslots[SLOTNO(c)];
  1139. lock(cb);
  1140. p = seprint(p, e, "slot %ld: %s; ", cb - cbslots, states[cb->state]);
  1141. switch (cb->type) {
  1142. case -1:
  1143. seprint(p, e, "\n");
  1144. break;
  1145. case PC32:
  1146. if (cb->pci->bridge) {
  1147. Pcidev *pci = cb->pci->bridge;
  1148. int i;
  1149. while (pci) {
  1150. p = seprint(p, e, "%.4uX %.4uX; irq %d\n",
  1151. pci->vid, pci->did, pci->intl);
  1152. for (i = 0; i != Nbars; i++)
  1153. if (pci->mem[i].size)
  1154. p = seprint(p, e,
  1155. "\tmem[%d] %.8ulX (%.8uX)\n",
  1156. i, pci->mem[i].bar,
  1157. pci->mem[i].size);
  1158. if (pci->rom.size)
  1159. p = seprint(p, e, "\tROM %.8ulX (%.8uX)\n",
  1160. pci->rom.bar, pci->rom.size);
  1161. pci = pci->list;
  1162. }
  1163. }
  1164. break;
  1165. case PC16:
  1166. if (cb->state == SlotConfigured) {
  1167. Pcminfo *pi = &cb->linfo;
  1168. p = seprint(p, e, "%s port %X; irq %d;\n",
  1169. pi->verstr, pi->port,
  1170. pi->irq);
  1171. for (i = 0; i != pi->nctab; i++) {
  1172. PCMconftab *ct;
  1173. int j;
  1174. ct = &pi->ctab[i];
  1175. p = seprint(p, e,
  1176. "\tconfiguration[%d] irqs %.4uX; vpp %d, %d; %s\n",
  1177. i, ct->irqs, ct->vpp1, ct->vpp2,
  1178. (ct == pi->defctab)? "(default);": "");
  1179. for (j = 0; j != ct->nio; j++)
  1180. if (ct->io[j].len > 0)
  1181. p = seprint(p, e, "\t\tio[%d] %.8ulX %uld\n",
  1182. j, ct->io[j].start, ct->io[j].len);
  1183. }
  1184. }
  1185. break;
  1186. }
  1187. unlock(cb);
  1188. n = readstr(offset, a, n, buf);
  1189. free(buf);
  1190. return n;
  1191. }
  1192. return 0;
  1193. }
  1194. static long
  1195. pccardwrite(Chan *c, void *v, long n, vlong)
  1196. {
  1197. Rune r;
  1198. ulong n0;
  1199. char *device;
  1200. Cmdbuf *cbf;
  1201. Cmdtab *ct;
  1202. Cardbus *cb;
  1203. n0 = n;
  1204. switch(TYPE(c)){
  1205. case Qctl:
  1206. cb = &cbslots[SLOTNO(c)];
  1207. cbf = parsecmd(v, n);
  1208. if(waserror()){
  1209. free(cbf);
  1210. nexterror();
  1211. }
  1212. ct = lookupcmd(cbf, pccardctlmsg, nelem(pccardctlmsg));
  1213. switch(ct->index){
  1214. case CMdown:
  1215. device = cbf->f[1];
  1216. device += chartorune(&r, device);
  1217. if ((n = devno(r, 1)) >= 0 && devtab[n]->config)
  1218. devtab[n]->config(0, device, nil);
  1219. qengine(cb, CardEjected);
  1220. break;
  1221. case CMpower:
  1222. if ((cb->regs[SocketState] & SS_CCD) == 0)
  1223. qengine(cb, CardDetected);
  1224. break;
  1225. }
  1226. poperror();
  1227. free(cbf);
  1228. break;
  1229. }
  1230. return n0 - n;
  1231. }
  1232. Dev pccarddevtab = {
  1233. 'Y',
  1234. "cardbus",
  1235. devreset,
  1236. devinit,
  1237. devshutdown,
  1238. pccardattach,
  1239. pccardwalk,
  1240. pccardstat,
  1241. pccardopen,
  1242. devcreate,
  1243. pccardclose,
  1244. pccardread,
  1245. devbread,
  1246. pccardwrite,
  1247. devbwrite,
  1248. devremove,
  1249. devwstat,
  1250. };
  1251. static PCMmap *
  1252. isamap(Cardbus *cb, ulong offset, int len, int attr)
  1253. {
  1254. uchar we, bit;
  1255. PCMmap *m, *nm;
  1256. Pcminfo *pi;
  1257. int i;
  1258. ulong e;
  1259. pi = &cb->linfo;
  1260. /* convert offset to granularity */
  1261. if(len <= 0)
  1262. len = 1;
  1263. e = ROUND(offset+len, Mgran);
  1264. offset &= Mmask;
  1265. len = e - offset;
  1266. /* look for a map that covers the right area */
  1267. we = rdreg(cb, Rwe);
  1268. bit = 1;
  1269. nm = 0;
  1270. for(m = pi->mmap; m < &pi->mmap[nelem(pi->mmap)]; m++){
  1271. if((we & bit))
  1272. if(m->attr == attr)
  1273. if(offset >= m->ca && e <= m->cea){
  1274. m->ref++;
  1275. return m;
  1276. }
  1277. bit <<= 1;
  1278. if(nm == 0 && m->ref == 0)
  1279. nm = m;
  1280. }
  1281. m = nm;
  1282. if(m == 0)
  1283. return 0;
  1284. /* if isa space isn't big enough, free it and get more */
  1285. if(m->len < len){
  1286. if(m->isa){
  1287. umbfree(m->isa, m->len);
  1288. m->len = 0;
  1289. }
  1290. m->isa = PADDR(umbmalloc(0, len, Mgran));
  1291. if(m->isa == 0){
  1292. print("isamap: out of isa space\n");
  1293. return 0;
  1294. }
  1295. m->len = len;
  1296. }
  1297. /* set up new map */
  1298. m->ca = offset;
  1299. m->cea = m->ca + m->len;
  1300. m->attr = attr;
  1301. i = m - pi->mmap;
  1302. bit = 1<<i;
  1303. wrreg(cb, Rwe, we & ~bit); /* disable map before changing it */
  1304. wrreg(cb, MAP(i, Mbtmlo), m->isa>>Mshift);
  1305. wrreg(cb, MAP(i, Mbtmhi), (m->isa>>(Mshift+8)) | F16bit);
  1306. wrreg(cb, MAP(i, Mtoplo), (m->isa+m->len-1)>>Mshift);
  1307. wrreg(cb, MAP(i, Mtophi), ((m->isa+m->len-1)>>(Mshift+8)));
  1308. offset -= m->isa;
  1309. offset &= (1<<25)-1;
  1310. offset >>= Mshift;
  1311. wrreg(cb, MAP(i, Mofflo), offset);
  1312. wrreg(cb, MAP(i, Moffhi), (offset>>8) | (attr ? Fregactive : 0));
  1313. wrreg(cb, Rwe, we | bit); /* enable map */
  1314. m->ref = 1;
  1315. return m;
  1316. }
  1317. static void
  1318. isaunmap(PCMmap* m)
  1319. {
  1320. m->ref--;
  1321. }
  1322. /*
  1323. * reading and writing card registers
  1324. */
  1325. static uchar
  1326. rdreg(Cardbus *cb, int index)
  1327. {
  1328. outb(cb->lindex, cb->lbase + index);
  1329. return inb(cb->ldata);
  1330. }
  1331. static void
  1332. wrreg(Cardbus *cb, int index, uchar val)
  1333. {
  1334. outb(cb->lindex, cb->lbase + index);
  1335. outb(cb->ldata, val);
  1336. }
  1337. static int
  1338. readc(Cisdat *cis, uchar *x)
  1339. {
  1340. if(cis->cispos >= cis->cislen)
  1341. return 0;
  1342. *x = cis->cisbase[cis->cisskip*cis->cispos];
  1343. cis->cispos++;
  1344. return 1;
  1345. }
  1346. static ulong
  1347. getlong(Cisdat *cis, int size)
  1348. {
  1349. uchar c;
  1350. int i;
  1351. ulong x;
  1352. x = 0;
  1353. for(i = 0; i < size; i++){
  1354. if(readc(cis, &c) != 1)
  1355. break;
  1356. x |= c<<(i*8);
  1357. }
  1358. return x;
  1359. }
  1360. static void
  1361. tcfig(Cardbus *cb, Cisdat *cis, int )
  1362. {
  1363. uchar size, rasize, rmsize;
  1364. uchar last;
  1365. Pcminfo *pi;
  1366. if(readc(cis, &size) != 1)
  1367. return;
  1368. rasize = (size&0x3) + 1;
  1369. rmsize = ((size>>2)&0xf) + 1;
  1370. if(readc(cis, &last) != 1)
  1371. return;
  1372. pi = &cb->linfo;
  1373. pi->conf_addr = getlong(cis, rasize);
  1374. pi->conf_present = getlong(cis, rmsize);
  1375. }
  1376. static void
  1377. tvers1(Cardbus *cb, Cisdat *cis, int )
  1378. {
  1379. uchar c, major, minor, last;
  1380. int i;
  1381. Pcminfo *pi;
  1382. pi = &cb->linfo;
  1383. if(readc(cis, &major) != 1)
  1384. return;
  1385. if(readc(cis, &minor) != 1)
  1386. return;
  1387. last = 0;
  1388. for(i = 0; i < sizeof(pi->verstr) - 1; i++){
  1389. if(readc(cis, &c) != 1)
  1390. return;
  1391. if(c == 0)
  1392. c = ';';
  1393. if(c == '\n')
  1394. c = ';';
  1395. if(c == 0xff)
  1396. break;
  1397. if(c == ';' && last == ';')
  1398. continue;
  1399. pi->verstr[i] = c;
  1400. last = c;
  1401. }
  1402. pi->verstr[i] = 0;
  1403. }
  1404. static ulong
  1405. microvolt(Cisdat *cis)
  1406. {
  1407. uchar c;
  1408. ulong microvolts;
  1409. ulong exp;
  1410. if(readc(cis, &c) != 1)
  1411. return 0;
  1412. exp = exponent[c&0x7];
  1413. microvolts = vmant[(c>>3)&0xf]*exp;
  1414. while(c & 0x80){
  1415. if(readc(cis, &c) != 1)
  1416. return 0;
  1417. switch(c){
  1418. case 0x7d:
  1419. break; /* high impedence when sleeping */
  1420. case 0x7e:
  1421. case 0x7f:
  1422. microvolts = 0; /* no connection */
  1423. break;
  1424. default:
  1425. exp /= 10;
  1426. microvolts += exp*(c&0x7f);
  1427. }
  1428. }
  1429. return microvolts;
  1430. }
  1431. static ulong
  1432. nanoamps(Cisdat *cis)
  1433. {
  1434. uchar c;
  1435. ulong nanoamps;
  1436. if(readc(cis, &c) != 1)
  1437. return 0;
  1438. nanoamps = exponent[c&0x7]*vmant[(c>>3)&0xf];
  1439. while(c & 0x80){
  1440. if(readc(cis, &c) != 1)
  1441. return 0;
  1442. if(c == 0x7d || c == 0x7e || c == 0x7f)
  1443. nanoamps = 0;
  1444. }
  1445. return nanoamps;
  1446. }
  1447. /*
  1448. * only nominal voltage (feature 1) is important for config,
  1449. * other features must read card to stay in sync.
  1450. */
  1451. static ulong
  1452. power(Cisdat *cis)
  1453. {
  1454. uchar feature;
  1455. ulong mv;
  1456. mv = 0;
  1457. if(readc(cis, &feature) != 1)
  1458. return 0;
  1459. if(feature & 1)
  1460. mv = microvolt(cis);
  1461. if(feature & 2)
  1462. microvolt(cis);
  1463. if(feature & 4)
  1464. microvolt(cis);
  1465. if(feature & 8)
  1466. nanoamps(cis);
  1467. if(feature & 0x10)
  1468. nanoamps(cis);
  1469. if(feature & 0x20)
  1470. nanoamps(cis);
  1471. if(feature & 0x40)
  1472. nanoamps(cis);
  1473. return mv/1000000;
  1474. }
  1475. static ulong
  1476. ttiming(Cisdat *cis, int scale)
  1477. {
  1478. uchar unscaled;
  1479. ulong nanosecs;
  1480. if(readc(cis, &unscaled) != 1)
  1481. return 0;
  1482. nanosecs = (mantissa[(unscaled>>3)&0xf]*exponent[unscaled&7])/10;
  1483. nanosecs = nanosecs * exponent[scale];
  1484. return nanosecs;
  1485. }
  1486. static void
  1487. timing(Cisdat *cis, PCMconftab *ct)
  1488. {
  1489. uchar c, i;
  1490. if(readc(cis, &c) != 1)
  1491. return;
  1492. i = c&0x3;
  1493. if(i != 3)
  1494. ct->maxwait = ttiming(cis, i); /* max wait */
  1495. i = (c>>2)&0x7;
  1496. if(i != 7)
  1497. ct->readywait = ttiming(cis, i); /* max ready/busy wait */
  1498. i = (c>>5)&0x7;
  1499. if(i != 7)
  1500. ct->otherwait = ttiming(cis, i); /* reserved wait */
  1501. }
  1502. static void
  1503. iospaces(Cisdat *cis, PCMconftab *ct)
  1504. {
  1505. uchar c;
  1506. int i, nio;
  1507. ct->nio = 0;
  1508. if(readc(cis, &c) != 1)
  1509. return;
  1510. ct->bit16 = ((c>>5)&3) >= 2;
  1511. if(!(c & 0x80)){
  1512. ct->io[0].start = 0;
  1513. ct->io[0].len = 1<<(c&0x1f);
  1514. ct->nio = 1;
  1515. return;
  1516. }
  1517. if(readc(cis, &c) != 1)
  1518. return;
  1519. /*
  1520. * For each of the range descriptions read the
  1521. * start address and the length (value is length-1).
  1522. */
  1523. nio = (c&0xf)+1;
  1524. for(i = 0; i < nio; i++){
  1525. ct->io[i].start = getlong(cis, (c>>4)&0x3);
  1526. ct->io[i].len = getlong(cis, (c>>6)&0x3)+1;
  1527. }
  1528. ct->nio = nio;
  1529. }
  1530. static void
  1531. irq(Cisdat *cis, PCMconftab *ct)
  1532. {
  1533. uchar c;
  1534. if(readc(cis, &c) != 1)
  1535. return;
  1536. ct->irqtype = c & 0xe0;
  1537. if(c & 0x10)
  1538. ct->irqs = getlong(cis, 2);
  1539. else
  1540. ct->irqs = 1<<(c&0xf);
  1541. ct->irqs &= 0xDEB8; /* levels available to card */
  1542. }
  1543. static void
  1544. memspace(Cisdat *cis, int asize, int lsize, int host)
  1545. {
  1546. ulong haddress, address, len;
  1547. len = getlong(cis, lsize)*256;
  1548. address = getlong(cis, asize)*256;
  1549. USED(len, address);
  1550. if(host){
  1551. haddress = getlong(cis, asize)*256;
  1552. USED(haddress);
  1553. }
  1554. }
  1555. static void
  1556. tentry(Cardbus *cb, Cisdat *cis, int )
  1557. {
  1558. uchar c, i, feature;
  1559. PCMconftab *ct;
  1560. Pcminfo *pi;
  1561. pi = &cb->linfo;
  1562. if(pi->nctab >= nelem(pi->ctab))
  1563. return;
  1564. if(readc(cis, &c) != 1)
  1565. return;
  1566. ct = &pi->ctab[pi->nctab++];
  1567. /* copy from last default config */
  1568. if(pi->defctab)
  1569. *ct = *pi->defctab;
  1570. ct->index = c & 0x3f;
  1571. /* is this the new default? */
  1572. if(c & 0x40)
  1573. pi->defctab = ct;
  1574. /* memory wait specified? */
  1575. if(c & 0x80){
  1576. if(readc(cis, &i) != 1)
  1577. return;
  1578. if(i&0x80)
  1579. ct->memwait = 1;
  1580. }
  1581. if(readc(cis, &feature) != 1)
  1582. return;
  1583. switch(feature&0x3){
  1584. case 1:
  1585. ct->vpp1 = ct->vpp2 = power(cis);
  1586. break;
  1587. case 2:
  1588. power(cis);
  1589. ct->vpp1 = ct->vpp2 = power(cis);
  1590. break;
  1591. case 3:
  1592. power(cis);
  1593. ct->vpp1 = power(cis);
  1594. ct->vpp2 = power(cis);
  1595. break;
  1596. default:
  1597. break;
  1598. }
  1599. if(feature&0x4)
  1600. timing(cis, ct);
  1601. if(feature&0x8)
  1602. iospaces(cis, ct);
  1603. if(feature&0x10)
  1604. irq(cis, ct);
  1605. switch((feature>>5)&0x3){
  1606. case 1:
  1607. memspace(cis, 0, 2, 0);
  1608. break;
  1609. case 2:
  1610. memspace(cis, 2, 2, 0);
  1611. break;
  1612. case 3:
  1613. if(readc(cis, &c) != 1)
  1614. return;
  1615. for(i = 0; i <= (c&0x7); i++)
  1616. memspace(cis, (c>>5)&0x3, (c>>3)&0x3, c&0x80);
  1617. break;
  1618. }
  1619. }
  1620. static void
  1621. i82365probe(Cardbus *cb, int lindex, int ldata)
  1622. {
  1623. uchar c, id;
  1624. int dev = 0; /* According to the Ricoh spec 00->3F _and_ 80->BF seem
  1625. to be the same socket A (ditto for B). */
  1626. outb(lindex, Rid + (dev<<7));
  1627. id = inb(ldata);
  1628. if((id & 0xf0) != 0x80)
  1629. return; /* not a memory & I/O card */
  1630. if((id & 0x0f) == 0x00)
  1631. return; /* no revision number, not possible */
  1632. cb->lindex = lindex;
  1633. cb->ldata = ldata;
  1634. cb->ltype = Ti82365;
  1635. cb->lbase = (int)(cb - cbslots) * 0x40;
  1636. switch(id){
  1637. case 0x82:
  1638. case 0x83:
  1639. case 0x84:
  1640. /* could be a cirrus */
  1641. outb(cb->lindex, Rchipinfo + (dev<<7));
  1642. outb(cb->ldata, 0);
  1643. c = inb(cb->ldata);
  1644. if((c & 0xc0) != 0xc0)
  1645. break;
  1646. c = inb(cb->ldata);
  1647. if((c & 0xc0) != 0x00)
  1648. break;
  1649. if(c & 0x20){
  1650. cb->ltype = Tpd6720;
  1651. } else {
  1652. cb->ltype = Tpd6710;
  1653. }
  1654. break;
  1655. }
  1656. /* if it's not a Cirrus, it could be a Vadem... */
  1657. if(cb->ltype == Ti82365){
  1658. /* unlock the Vadem extended regs */
  1659. outb(cb->lindex, 0x0E + (dev<<7));
  1660. outb(cb->lindex, 0x37 + (dev<<7));
  1661. /* make the id register show the Vadem id */
  1662. outb(cb->lindex, 0x3A + (dev<<7));
  1663. c = inb(cb->ldata);
  1664. outb(cb->ldata, c|0xC0);
  1665. outb(cb->lindex, Rid + (dev<<7));
  1666. c = inb(cb->ldata);
  1667. if(c & 0x08)
  1668. cb->ltype = Tvg46x;
  1669. /* go back to Intel compatible id */
  1670. outb(cb->lindex, 0x3A + (dev<<7));
  1671. c = inb(cb->ldata);
  1672. outb(cb->ldata, c & ~0xC0);
  1673. }
  1674. }
  1675. static int
  1676. vcode(int volt)
  1677. {
  1678. switch(volt){
  1679. case 5:
  1680. return 1;
  1681. case 12:
  1682. return 2;
  1683. default:
  1684. return 0;
  1685. }
  1686. }