devpccard.c 40 KB

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