devpccard.c 39 KB

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