devpccard.c 39 KB

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