devpccard.c 40 KB

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