i2ellis.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. /*******************************************************************************
  2. *
  3. * (c) 1998 by Computone Corporation
  4. *
  5. ********************************************************************************
  6. *
  7. *
  8. * PACKAGE: Linux tty Device Driver for IntelliPort family of multiport
  9. * serial I/O controllers.
  10. *
  11. * DESCRIPTION: Low-level interface code for the device driver
  12. * (This is included source code, not a separate compilation
  13. * module.)
  14. *
  15. *******************************************************************************/
  16. //---------------------------------------------
  17. // Function declarations private to this module
  18. //---------------------------------------------
  19. // Functions called only indirectly through i2eBordStr entries.
  20. static int iiWriteBuf16(i2eBordStrPtr, unsigned char *, int);
  21. static int iiWriteBuf8(i2eBordStrPtr, unsigned char *, int);
  22. static int iiReadBuf16(i2eBordStrPtr, unsigned char *, int);
  23. static int iiReadBuf8(i2eBordStrPtr, unsigned char *, int);
  24. static unsigned short iiReadWord16(i2eBordStrPtr);
  25. static unsigned short iiReadWord8(i2eBordStrPtr);
  26. static void iiWriteWord16(i2eBordStrPtr, unsigned short);
  27. static void iiWriteWord8(i2eBordStrPtr, unsigned short);
  28. static int iiWaitForTxEmptyII(i2eBordStrPtr, int);
  29. static int iiWaitForTxEmptyIIEX(i2eBordStrPtr, int);
  30. static int iiTxMailEmptyII(i2eBordStrPtr);
  31. static int iiTxMailEmptyIIEX(i2eBordStrPtr);
  32. static int iiTrySendMailII(i2eBordStrPtr, unsigned char);
  33. static int iiTrySendMailIIEX(i2eBordStrPtr, unsigned char);
  34. static unsigned short iiGetMailII(i2eBordStrPtr);
  35. static unsigned short iiGetMailIIEX(i2eBordStrPtr);
  36. static void iiEnableMailIrqII(i2eBordStrPtr);
  37. static void iiEnableMailIrqIIEX(i2eBordStrPtr);
  38. static void iiWriteMaskII(i2eBordStrPtr, unsigned char);
  39. static void iiWriteMaskIIEX(i2eBordStrPtr, unsigned char);
  40. static void ii2Nop(void);
  41. //***************
  42. //* Static Data *
  43. //***************
  44. static int ii2Safe; // Safe I/O address for delay routine
  45. static int iiDelayed; // Set when the iiResetDelay function is
  46. // called. Cleared when ANY board is reset.
  47. static DEFINE_RWLOCK(Dl_spinlock);
  48. //********
  49. //* Code *
  50. //********
  51. //=======================================================
  52. // Initialization Routines
  53. //
  54. // iiSetAddress
  55. // iiReset
  56. // iiResetDelay
  57. // iiInitialize
  58. //=======================================================
  59. //******************************************************************************
  60. // Function: iiSetAddress(pB, address, delay)
  61. // Parameters: pB - pointer to the board structure
  62. // address - the purported I/O address of the board
  63. // delay - pointer to the 1-ms delay function to use
  64. // in this and any future operations to this board
  65. //
  66. // Returns: True if everything appears copacetic.
  67. // False if there is any error: the pB->i2eError field has the error
  68. //
  69. // Description:
  70. //
  71. // This routine (roughly) checks for address validity, sets the i2eValid OK and
  72. // sets the state to II_STATE_COLD which means that we haven't even sent a reset
  73. // yet.
  74. //
  75. //******************************************************************************
  76. static int
  77. iiSetAddress( i2eBordStrPtr pB, int address, delayFunc_t delay )
  78. {
  79. // Should any failure occur before init is finished...
  80. pB->i2eValid = I2E_INCOMPLETE;
  81. // Cannot check upper limit except extremely: Might be microchannel
  82. // Address must be on an 8-byte boundary
  83. if ((unsigned int)address <= 0x100
  84. || (unsigned int)address >= 0xfff8
  85. || (address & 0x7)
  86. )
  87. {
  88. I2_COMPLETE(pB, I2EE_BADADDR);
  89. }
  90. // Initialize accelerators
  91. pB->i2eBase = address;
  92. pB->i2eData = address + FIFO_DATA;
  93. pB->i2eStatus = address + FIFO_STATUS;
  94. pB->i2ePointer = address + FIFO_PTR;
  95. pB->i2eXMail = address + FIFO_MAIL;
  96. pB->i2eXMask = address + FIFO_MASK;
  97. // Initialize i/o address for ii2DelayIO
  98. ii2Safe = address + FIFO_NOP;
  99. // Initialize the delay routine
  100. pB->i2eDelay = ((delay != (delayFunc_t)NULL) ? delay : (delayFunc_t)ii2Nop);
  101. pB->i2eValid = I2E_MAGIC;
  102. pB->i2eState = II_STATE_COLD;
  103. I2_COMPLETE(pB, I2EE_GOOD);
  104. }
  105. //******************************************************************************
  106. // Function: iiReset(pB)
  107. // Parameters: pB - pointer to the board structure
  108. //
  109. // Returns: True if everything appears copacetic.
  110. // False if there is any error: the pB->i2eError field has the error
  111. //
  112. // Description:
  113. //
  114. // Attempts to reset the board (see also i2hw.h). Normally, we would use this to
  115. // reset a board immediately after iiSetAddress(), but it is valid to reset a
  116. // board from any state, say, in order to change or re-load loadware. (Under
  117. // such circumstances, no reason to re-run iiSetAddress(), which is why it is a
  118. // separate routine and not included in this routine.
  119. //
  120. //******************************************************************************
  121. static int
  122. iiReset(i2eBordStrPtr pB)
  123. {
  124. // Magic number should be set, else even the address is suspect
  125. if (pB->i2eValid != I2E_MAGIC)
  126. {
  127. I2_COMPLETE(pB, I2EE_BADMAGIC);
  128. }
  129. outb(0, pB->i2eBase + FIFO_RESET); /* Any data will do */
  130. iiDelay(pB, 50); // Pause between resets
  131. outb(0, pB->i2eBase + FIFO_RESET); /* Second reset */
  132. // We must wait before even attempting to read anything from the FIFO: the
  133. // board's P.O.S.T may actually attempt to read and write its end of the
  134. // FIFO in order to check flags, loop back (where supported), etc. On
  135. // completion of this testing it would reset the FIFO, and on completion
  136. // of all // P.O.S.T., write the message. We must not mistake data which
  137. // might have been sent for testing as part of the reset message. To
  138. // better utilize time, say, when resetting several boards, we allow the
  139. // delay to be performed externally; in this way the caller can reset
  140. // several boards, delay a single time, then call the initialization
  141. // routine for all.
  142. pB->i2eState = II_STATE_RESET;
  143. iiDelayed = 0; // i.e., the delay routine hasn't been called since the most
  144. // recent reset.
  145. // Ensure anything which would have been of use to standard loadware is
  146. // blanked out, since board has now forgotten everything!.
  147. pB->i2eUsingIrq = I2_IRQ_UNDEFINED; /* to not use an interrupt so far */
  148. pB->i2eWaitingForEmptyFifo = 0;
  149. pB->i2eOutMailWaiting = 0;
  150. pB->i2eChannelPtr = NULL;
  151. pB->i2eChannelCnt = 0;
  152. pB->i2eLeadoffWord[0] = 0;
  153. pB->i2eFifoInInts = 0;
  154. pB->i2eFifoOutInts = 0;
  155. pB->i2eFatalTrap = NULL;
  156. pB->i2eFatal = 0;
  157. I2_COMPLETE(pB, I2EE_GOOD);
  158. }
  159. //******************************************************************************
  160. // Function: iiResetDelay(pB)
  161. // Parameters: pB - pointer to the board structure
  162. //
  163. // Returns: True if everything appears copacetic.
  164. // False if there is any error: the pB->i2eError field has the error
  165. //
  166. // Description:
  167. //
  168. // Using the delay defined in board structure, waits two seconds (for board to
  169. // reset).
  170. //
  171. //******************************************************************************
  172. static int
  173. iiResetDelay(i2eBordStrPtr pB)
  174. {
  175. if (pB->i2eValid != I2E_MAGIC) {
  176. I2_COMPLETE(pB, I2EE_BADMAGIC);
  177. }
  178. if (pB->i2eState != II_STATE_RESET) {
  179. I2_COMPLETE(pB, I2EE_BADSTATE);
  180. }
  181. iiDelay(pB,2000); /* Now we wait for two seconds. */
  182. iiDelayed = 1; /* Delay has been called: ok to initialize */
  183. I2_COMPLETE(pB, I2EE_GOOD);
  184. }
  185. //******************************************************************************
  186. // Function: iiInitialize(pB)
  187. // Parameters: pB - pointer to the board structure
  188. //
  189. // Returns: True if everything appears copacetic.
  190. // False if there is any error: the pB->i2eError field has the error
  191. //
  192. // Description:
  193. //
  194. // Attempts to read the Power-on reset message. Initializes any remaining fields
  195. // in the pB structure.
  196. //
  197. // This should be called as the third step of a process beginning with
  198. // iiReset(), then iiResetDelay(). This routine checks to see that the structure
  199. // is "valid" and in the reset state, also confirms that the delay routine has
  200. // been called since the latest reset (to any board! overly strong!).
  201. //
  202. //******************************************************************************
  203. static int
  204. iiInitialize(i2eBordStrPtr pB)
  205. {
  206. int itemp;
  207. unsigned char c;
  208. unsigned short utemp;
  209. unsigned int ilimit;
  210. if (pB->i2eValid != I2E_MAGIC)
  211. {
  212. I2_COMPLETE(pB, I2EE_BADMAGIC);
  213. }
  214. if (pB->i2eState != II_STATE_RESET || !iiDelayed)
  215. {
  216. I2_COMPLETE(pB, I2EE_BADSTATE);
  217. }
  218. // In case there is a failure short of our completely reading the power-up
  219. // message.
  220. pB->i2eValid = I2E_INCOMPLETE;
  221. // Now attempt to read the message.
  222. for (itemp = 0; itemp < sizeof(porStr); itemp++)
  223. {
  224. // We expect the entire message is ready.
  225. if (!I2_HAS_INPUT(pB)) {
  226. pB->i2ePomSize = itemp;
  227. I2_COMPLETE(pB, I2EE_PORM_SHORT);
  228. }
  229. pB->i2ePom.c[itemp] = c = inb(pB->i2eData);
  230. // We check the magic numbers as soon as they are supposed to be read
  231. // (rather than after) to minimize effect of reading something we
  232. // already suspect can't be "us".
  233. if ( (itemp == POR_1_INDEX && c != POR_MAGIC_1) ||
  234. (itemp == POR_2_INDEX && c != POR_MAGIC_2))
  235. {
  236. pB->i2ePomSize = itemp+1;
  237. I2_COMPLETE(pB, I2EE_BADMAGIC);
  238. }
  239. }
  240. pB->i2ePomSize = itemp;
  241. // Ensure that this was all the data...
  242. if (I2_HAS_INPUT(pB))
  243. I2_COMPLETE(pB, I2EE_PORM_LONG);
  244. // For now, we'll fail to initialize if P.O.S.T reports bad chip mapper:
  245. // Implying we will not be able to download any code either: That's ok: the
  246. // condition is pretty explicit.
  247. if (pB->i2ePom.e.porDiag1 & POR_BAD_MAPPER)
  248. {
  249. I2_COMPLETE(pB, I2EE_POSTERR);
  250. }
  251. // Determine anything which must be done differently depending on the family
  252. // of boards!
  253. switch (pB->i2ePom.e.porID & POR_ID_FAMILY)
  254. {
  255. case POR_ID_FII: // IntelliPort-II
  256. pB->i2eFifoStyle = FIFO_II;
  257. pB->i2eFifoSize = 512; // 512 bytes, always
  258. pB->i2eDataWidth16 = false;
  259. pB->i2eMaxIrq = 15; // Because board cannot tell us it is in an 8-bit
  260. // slot, we do allow it to be done (documentation!)
  261. pB->i2eGoodMap[1] =
  262. pB->i2eGoodMap[2] =
  263. pB->i2eGoodMap[3] =
  264. pB->i2eChannelMap[1] =
  265. pB->i2eChannelMap[2] =
  266. pB->i2eChannelMap[3] = 0;
  267. switch (pB->i2ePom.e.porID & POR_ID_SIZE)
  268. {
  269. case POR_ID_II_4:
  270. pB->i2eGoodMap[0] =
  271. pB->i2eChannelMap[0] = 0x0f; // four-port
  272. // Since porPorts1 is based on the Hardware ID register, the numbers
  273. // should always be consistent for IntelliPort-II. Ditto below...
  274. if (pB->i2ePom.e.porPorts1 != 4)
  275. {
  276. I2_COMPLETE(pB, I2EE_INCONSIST);
  277. }
  278. break;
  279. case POR_ID_II_8:
  280. case POR_ID_II_8R:
  281. pB->i2eGoodMap[0] =
  282. pB->i2eChannelMap[0] = 0xff; // Eight port
  283. if (pB->i2ePom.e.porPorts1 != 8)
  284. {
  285. I2_COMPLETE(pB, I2EE_INCONSIST);
  286. }
  287. break;
  288. case POR_ID_II_6:
  289. pB->i2eGoodMap[0] =
  290. pB->i2eChannelMap[0] = 0x3f; // Six Port
  291. if (pB->i2ePom.e.porPorts1 != 6)
  292. {
  293. I2_COMPLETE(pB, I2EE_INCONSIST);
  294. }
  295. break;
  296. }
  297. // Fix up the "good channel list based on any errors reported.
  298. if (pB->i2ePom.e.porDiag1 & POR_BAD_UART1)
  299. {
  300. pB->i2eGoodMap[0] &= ~0x0f;
  301. }
  302. if (pB->i2ePom.e.porDiag1 & POR_BAD_UART2)
  303. {
  304. pB->i2eGoodMap[0] &= ~0xf0;
  305. }
  306. break; // POR_ID_FII case
  307. case POR_ID_FIIEX: // IntelliPort-IIEX
  308. pB->i2eFifoStyle = FIFO_IIEX;
  309. itemp = pB->i2ePom.e.porFifoSize;
  310. // Implicit assumption that fifo would not grow beyond 32k,
  311. // nor would ever be less than 256.
  312. if (itemp < 8 || itemp > 15)
  313. {
  314. I2_COMPLETE(pB, I2EE_INCONSIST);
  315. }
  316. pB->i2eFifoSize = (1 << itemp);
  317. // These are based on what P.O.S.T thinks should be there, based on
  318. // box ID registers
  319. ilimit = pB->i2ePom.e.porNumBoxes;
  320. if (ilimit > ABS_MAX_BOXES)
  321. {
  322. ilimit = ABS_MAX_BOXES;
  323. }
  324. // For as many boxes as EXIST, gives the type of box.
  325. // Added 8/6/93: check for the ISA-4 (asic) which looks like an
  326. // expandable but for whom "8 or 16?" is not the right question.
  327. utemp = pB->i2ePom.e.porFlags;
  328. if (utemp & POR_CEX4)
  329. {
  330. pB->i2eChannelMap[0] = 0x000f;
  331. } else {
  332. utemp &= POR_BOXES;
  333. for (itemp = 0; itemp < ilimit; itemp++)
  334. {
  335. pB->i2eChannelMap[itemp] =
  336. ((utemp & POR_BOX_16) ? 0xffff : 0x00ff);
  337. utemp >>= 1;
  338. }
  339. }
  340. // These are based on what P.O.S.T actually found.
  341. utemp = (pB->i2ePom.e.porPorts2 << 8) + pB->i2ePom.e.porPorts1;
  342. for (itemp = 0; itemp < ilimit; itemp++)
  343. {
  344. pB->i2eGoodMap[itemp] = 0;
  345. if (utemp & 1) pB->i2eGoodMap[itemp] |= 0x000f;
  346. if (utemp & 2) pB->i2eGoodMap[itemp] |= 0x00f0;
  347. if (utemp & 4) pB->i2eGoodMap[itemp] |= 0x0f00;
  348. if (utemp & 8) pB->i2eGoodMap[itemp] |= 0xf000;
  349. utemp >>= 4;
  350. }
  351. // Now determine whether we should transfer in 8 or 16-bit mode.
  352. switch (pB->i2ePom.e.porBus & (POR_BUS_SLOT16 | POR_BUS_DIP16) )
  353. {
  354. case POR_BUS_SLOT16 | POR_BUS_DIP16:
  355. pB->i2eDataWidth16 = true;
  356. pB->i2eMaxIrq = 15;
  357. break;
  358. case POR_BUS_SLOT16:
  359. pB->i2eDataWidth16 = false;
  360. pB->i2eMaxIrq = 15;
  361. break;
  362. case 0:
  363. case POR_BUS_DIP16: // In an 8-bit slot, DIP switch don't care.
  364. default:
  365. pB->i2eDataWidth16 = false;
  366. pB->i2eMaxIrq = 7;
  367. break;
  368. }
  369. break; // POR_ID_FIIEX case
  370. default: // Unknown type of board
  371. I2_COMPLETE(pB, I2EE_BAD_FAMILY);
  372. break;
  373. } // End the switch based on family
  374. // Temporarily, claim there is no room in the outbound fifo.
  375. // We will maintain this whenever we check for an empty outbound FIFO.
  376. pB->i2eFifoRemains = 0;
  377. // Now, based on the bus type, should we expect to be able to re-configure
  378. // interrupts (say, for testing purposes).
  379. switch (pB->i2ePom.e.porBus & POR_BUS_TYPE)
  380. {
  381. case POR_BUS_T_ISA:
  382. case POR_BUS_T_UNK: // If the type of bus is undeclared, assume ok.
  383. case POR_BUS_T_MCA:
  384. case POR_BUS_T_EISA:
  385. break;
  386. default:
  387. I2_COMPLETE(pB, I2EE_BADBUS);
  388. }
  389. if (pB->i2eDataWidth16)
  390. {
  391. pB->i2eWriteBuf = iiWriteBuf16;
  392. pB->i2eReadBuf = iiReadBuf16;
  393. pB->i2eWriteWord = iiWriteWord16;
  394. pB->i2eReadWord = iiReadWord16;
  395. } else {
  396. pB->i2eWriteBuf = iiWriteBuf8;
  397. pB->i2eReadBuf = iiReadBuf8;
  398. pB->i2eWriteWord = iiWriteWord8;
  399. pB->i2eReadWord = iiReadWord8;
  400. }
  401. switch(pB->i2eFifoStyle)
  402. {
  403. case FIFO_II:
  404. pB->i2eWaitForTxEmpty = iiWaitForTxEmptyII;
  405. pB->i2eTxMailEmpty = iiTxMailEmptyII;
  406. pB->i2eTrySendMail = iiTrySendMailII;
  407. pB->i2eGetMail = iiGetMailII;
  408. pB->i2eEnableMailIrq = iiEnableMailIrqII;
  409. pB->i2eWriteMask = iiWriteMaskII;
  410. break;
  411. case FIFO_IIEX:
  412. pB->i2eWaitForTxEmpty = iiWaitForTxEmptyIIEX;
  413. pB->i2eTxMailEmpty = iiTxMailEmptyIIEX;
  414. pB->i2eTrySendMail = iiTrySendMailIIEX;
  415. pB->i2eGetMail = iiGetMailIIEX;
  416. pB->i2eEnableMailIrq = iiEnableMailIrqIIEX;
  417. pB->i2eWriteMask = iiWriteMaskIIEX;
  418. break;
  419. default:
  420. I2_COMPLETE(pB, I2EE_INCONSIST);
  421. }
  422. // Initialize state information.
  423. pB->i2eState = II_STATE_READY; // Ready to load loadware.
  424. // Some Final cleanup:
  425. // For some boards, the bootstrap firmware may perform some sort of test
  426. // resulting in a stray character pending in the incoming mailbox. If one is
  427. // there, it should be read and discarded, especially since for the standard
  428. // firmware, it's the mailbox that interrupts the host.
  429. pB->i2eStartMail = iiGetMail(pB);
  430. // Throw it away and clear the mailbox structure element
  431. pB->i2eStartMail = NO_MAIL_HERE;
  432. // Everything is ok now, return with good status/
  433. pB->i2eValid = I2E_MAGIC;
  434. I2_COMPLETE(pB, I2EE_GOOD);
  435. }
  436. //******************************************************************************
  437. // Function: ii2DelayTimer(mseconds)
  438. // Parameters: mseconds - number of milliseconds to delay
  439. //
  440. // Returns: Nothing
  441. //
  442. // Description:
  443. //
  444. // This routine delays for approximately mseconds milliseconds and is intended
  445. // to be called indirectly through i2Delay field in i2eBordStr. It uses the
  446. // Linux timer_list mechanism.
  447. //
  448. // The Linux timers use a unit called "jiffies" which are 10mS in the Intel
  449. // architecture. This function rounds the delay period up to the next "jiffy".
  450. // In the Alpha architecture the "jiffy" is 1mS, but this driver is not intended
  451. // for Alpha platforms at this time.
  452. //
  453. //******************************************************************************
  454. static void
  455. ii2DelayTimer(unsigned int mseconds)
  456. {
  457. msleep_interruptible(mseconds);
  458. }
  459. #if 0
  460. //static void ii2DelayIO(unsigned int);
  461. //******************************************************************************
  462. // !!! Not Used, this is DOS crap, some of you young folks may be interested in
  463. // in how things were done in the stone age of caculating machines !!!
  464. // Function: ii2DelayIO(mseconds)
  465. // Parameters: mseconds - number of milliseconds to delay
  466. //
  467. // Returns: Nothing
  468. //
  469. // Description:
  470. //
  471. // This routine delays for approximately mseconds milliseconds and is intended
  472. // to be called indirectly through i2Delay field in i2eBordStr. It is intended
  473. // for use where a clock-based function is impossible: for example, DOS drivers.
  474. //
  475. // This function uses the IN instruction to place bounds on the timing and
  476. // assumes that ii2Safe has been set. This is because I/O instructions are not
  477. // subject to caching and will therefore take a certain minimum time. To ensure
  478. // the delay is at least long enough on fast machines, it is based on some
  479. // fastest-case calculations. On slower machines this may cause VERY long
  480. // delays. (3 x fastest case). In the fastest case, everything is cached except
  481. // the I/O instruction itself.
  482. //
  483. // Timing calculations:
  484. // The fastest bus speed for I/O operations is likely to be 10 MHz. The I/O
  485. // operation in question is a byte operation to an odd address. For 8-bit
  486. // operations, the architecture generally enforces two wait states. At 10 MHz, a
  487. // single cycle time is 100nS. A read operation at two wait states takes 6
  488. // cycles for a total time of 600nS. Therefore approximately 1666 iterations
  489. // would be required to generate a single millisecond delay. The worst
  490. // (reasonable) case would be an 8MHz system with no cacheing. In this case, the
  491. // I/O instruction would take 125nS x 6 cyles = 750 nS. More importantly, code
  492. // fetch of other instructions in the loop would take time (zero wait states,
  493. // however) and would be hard to estimate. This is minimized by using in-line
  494. // assembler for the in inner loop of IN instructions. This consists of just a
  495. // few bytes. So we'll guess about four code fetches per loop. Each code fetch
  496. // should take four cycles, so we have 125nS * 8 = 1000nS. Worst case then is
  497. // that what should have taken 1 mS takes instead 1666 * (1750) = 2.9 mS.
  498. //
  499. // So much for theoretical timings: results using 1666 value on some actual
  500. // machines:
  501. // IBM 286 6MHz 3.15 mS
  502. // Zenith 386 33MHz 2.45 mS
  503. // (brandX) 386 33MHz 1.90 mS (has cache)
  504. // (brandY) 486 33MHz 2.35 mS
  505. // NCR 486 ?? 1.65 mS (microchannel)
  506. //
  507. // For most machines, it is probably safe to scale this number back (remember,
  508. // for robust operation use an actual timed delay if possible), so we are using
  509. // a value of 1190. This yields 1.17 mS for the fastest machine in our sample,
  510. // 1.75 mS for typical 386 machines, and 2.25 mS the absolute slowest machine.
  511. //
  512. // 1/29/93:
  513. // The above timings are too slow. Actual cycle times might be faster. ISA cycle
  514. // times could approach 500 nS, and ...
  515. // The IBM model 77 being microchannel has no wait states for 8-bit reads and
  516. // seems to be accessing the I/O at 440 nS per access (from start of one to
  517. // start of next). This would imply we need 1000/.440 = 2272 iterations to
  518. // guarantee we are fast enough. In actual testing, we see that 2 * 1190 are in
  519. // fact enough. For diagnostics, we keep the level at 1190, but developers note
  520. // this needs tuning.
  521. //
  522. // Safe assumption: 2270 i/o reads = 1 millisecond
  523. //
  524. //******************************************************************************
  525. static int ii2DelValue = 1190; // See timing calculations below
  526. // 1666 for fastest theoretical machine
  527. // 1190 safe for most fast 386 machines
  528. // 1000 for fastest machine tested here
  529. // 540 (sic) for AT286/6Mhz
  530. static void
  531. ii2DelayIO(unsigned int mseconds)
  532. {
  533. if (!ii2Safe)
  534. return; /* Do nothing if this variable uninitialized */
  535. while(mseconds--) {
  536. int i = ii2DelValue;
  537. while ( i-- ) {
  538. inb(ii2Safe);
  539. }
  540. }
  541. }
  542. #endif
  543. //******************************************************************************
  544. // Function: ii2Nop()
  545. // Parameters: None
  546. //
  547. // Returns: Nothing
  548. //
  549. // Description:
  550. //
  551. // iiInitialize will set i2eDelay to this if the delay parameter is NULL. This
  552. // saves checking for a NULL pointer at every call.
  553. //******************************************************************************
  554. static void
  555. ii2Nop(void)
  556. {
  557. return; // no mystery here
  558. }
  559. //=======================================================
  560. // Routines which are available in 8/16-bit versions, or
  561. // in different fifo styles. These are ALL called
  562. // indirectly through the board structure.
  563. //=======================================================
  564. //******************************************************************************
  565. // Function: iiWriteBuf16(pB, address, count)
  566. // Parameters: pB - pointer to board structure
  567. // address - address of data to write
  568. // count - number of data bytes to write
  569. //
  570. // Returns: True if everything appears copacetic.
  571. // False if there is any error: the pB->i2eError field has the error
  572. //
  573. // Description:
  574. //
  575. // Writes 'count' bytes from 'address' to the data fifo specified by the board
  576. // structure pointer pB. Should count happen to be odd, an extra pad byte is
  577. // sent (identity unknown...). Uses 16-bit (word) operations. Is called
  578. // indirectly through pB->i2eWriteBuf.
  579. //
  580. //******************************************************************************
  581. static int
  582. iiWriteBuf16(i2eBordStrPtr pB, unsigned char *address, int count)
  583. {
  584. // Rudimentary sanity checking here.
  585. if (pB->i2eValid != I2E_MAGIC)
  586. I2_COMPLETE(pB, I2EE_INVALID);
  587. I2_OUTSW(pB->i2eData, address, count);
  588. I2_COMPLETE(pB, I2EE_GOOD);
  589. }
  590. //******************************************************************************
  591. // Function: iiWriteBuf8(pB, address, count)
  592. // Parameters: pB - pointer to board structure
  593. // address - address of data to write
  594. // count - number of data bytes to write
  595. //
  596. // Returns: True if everything appears copacetic.
  597. // False if there is any error: the pB->i2eError field has the error
  598. //
  599. // Description:
  600. //
  601. // Writes 'count' bytes from 'address' to the data fifo specified by the board
  602. // structure pointer pB. Should count happen to be odd, an extra pad byte is
  603. // sent (identity unknown...). This is to be consistent with the 16-bit version.
  604. // Uses 8-bit (byte) operations. Is called indirectly through pB->i2eWriteBuf.
  605. //
  606. //******************************************************************************
  607. static int
  608. iiWriteBuf8(i2eBordStrPtr pB, unsigned char *address, int count)
  609. {
  610. /* Rudimentary sanity checking here */
  611. if (pB->i2eValid != I2E_MAGIC)
  612. I2_COMPLETE(pB, I2EE_INVALID);
  613. I2_OUTSB(pB->i2eData, address, count);
  614. I2_COMPLETE(pB, I2EE_GOOD);
  615. }
  616. //******************************************************************************
  617. // Function: iiReadBuf16(pB, address, count)
  618. // Parameters: pB - pointer to board structure
  619. // address - address to put data read
  620. // count - number of data bytes to read
  621. //
  622. // Returns: True if everything appears copacetic.
  623. // False if there is any error: the pB->i2eError field has the error
  624. //
  625. // Description:
  626. //
  627. // Reads 'count' bytes into 'address' from the data fifo specified by the board
  628. // structure pointer pB. Should count happen to be odd, an extra pad byte is
  629. // received (identity unknown...). Uses 16-bit (word) operations. Is called
  630. // indirectly through pB->i2eReadBuf.
  631. //
  632. //******************************************************************************
  633. static int
  634. iiReadBuf16(i2eBordStrPtr pB, unsigned char *address, int count)
  635. {
  636. // Rudimentary sanity checking here.
  637. if (pB->i2eValid != I2E_MAGIC)
  638. I2_COMPLETE(pB, I2EE_INVALID);
  639. I2_INSW(pB->i2eData, address, count);
  640. I2_COMPLETE(pB, I2EE_GOOD);
  641. }
  642. //******************************************************************************
  643. // Function: iiReadBuf8(pB, address, count)
  644. // Parameters: pB - pointer to board structure
  645. // address - address to put data read
  646. // count - number of data bytes to read
  647. //
  648. // Returns: True if everything appears copacetic.
  649. // False if there is any error: the pB->i2eError field has the error
  650. //
  651. // Description:
  652. //
  653. // Reads 'count' bytes into 'address' from the data fifo specified by the board
  654. // structure pointer pB. Should count happen to be odd, an extra pad byte is
  655. // received (identity unknown...). This to match the 16-bit behaviour. Uses
  656. // 8-bit (byte) operations. Is called indirectly through pB->i2eReadBuf.
  657. //
  658. //******************************************************************************
  659. static int
  660. iiReadBuf8(i2eBordStrPtr pB, unsigned char *address, int count)
  661. {
  662. // Rudimentary sanity checking here.
  663. if (pB->i2eValid != I2E_MAGIC)
  664. I2_COMPLETE(pB, I2EE_INVALID);
  665. I2_INSB(pB->i2eData, address, count);
  666. I2_COMPLETE(pB, I2EE_GOOD);
  667. }
  668. //******************************************************************************
  669. // Function: iiReadWord16(pB)
  670. // Parameters: pB - pointer to board structure
  671. //
  672. // Returns: True if everything appears copacetic.
  673. // False if there is any error: the pB->i2eError field has the error
  674. //
  675. // Description:
  676. //
  677. // Returns the word read from the data fifo specified by the board-structure
  678. // pointer pB. Uses a 16-bit operation. Is called indirectly through
  679. // pB->i2eReadWord.
  680. //
  681. //******************************************************************************
  682. static unsigned short
  683. iiReadWord16(i2eBordStrPtr pB)
  684. {
  685. return inw(pB->i2eData);
  686. }
  687. //******************************************************************************
  688. // Function: iiReadWord8(pB)
  689. // Parameters: pB - pointer to board structure
  690. //
  691. // Returns: True if everything appears copacetic.
  692. // False if there is any error: the pB->i2eError field has the error
  693. //
  694. // Description:
  695. //
  696. // Returns the word read from the data fifo specified by the board-structure
  697. // pointer pB. Uses two 8-bit operations. Bytes are assumed to be LSB first. Is
  698. // called indirectly through pB->i2eReadWord.
  699. //
  700. //******************************************************************************
  701. static unsigned short
  702. iiReadWord8(i2eBordStrPtr pB)
  703. {
  704. unsigned short urs;
  705. urs = inb(pB->i2eData);
  706. return (inb(pB->i2eData) << 8) | urs;
  707. }
  708. //******************************************************************************
  709. // Function: iiWriteWord16(pB, value)
  710. // Parameters: pB - pointer to board structure
  711. // value - data to write
  712. //
  713. // Returns: True if everything appears copacetic.
  714. // False if there is any error: the pB->i2eError field has the error
  715. //
  716. // Description:
  717. //
  718. // Writes the word 'value' to the data fifo specified by the board-structure
  719. // pointer pB. Uses 16-bit operation. Is called indirectly through
  720. // pB->i2eWriteWord.
  721. //
  722. //******************************************************************************
  723. static void
  724. iiWriteWord16(i2eBordStrPtr pB, unsigned short value)
  725. {
  726. outw((int)value, pB->i2eData);
  727. }
  728. //******************************************************************************
  729. // Function: iiWriteWord8(pB, value)
  730. // Parameters: pB - pointer to board structure
  731. // value - data to write
  732. //
  733. // Returns: True if everything appears copacetic.
  734. // False if there is any error: the pB->i2eError field has the error
  735. //
  736. // Description:
  737. //
  738. // Writes the word 'value' to the data fifo specified by the board-structure
  739. // pointer pB. Uses two 8-bit operations (writes LSB first). Is called
  740. // indirectly through pB->i2eWriteWord.
  741. //
  742. //******************************************************************************
  743. static void
  744. iiWriteWord8(i2eBordStrPtr pB, unsigned short value)
  745. {
  746. outb((char)value, pB->i2eData);
  747. outb((char)(value >> 8), pB->i2eData);
  748. }
  749. //******************************************************************************
  750. // Function: iiWaitForTxEmptyII(pB, mSdelay)
  751. // Parameters: pB - pointer to board structure
  752. // mSdelay - period to wait before returning
  753. //
  754. // Returns: True if the FIFO is empty.
  755. // False if it not empty in the required time: the pB->i2eError
  756. // field has the error.
  757. //
  758. // Description:
  759. //
  760. // Waits up to "mSdelay" milliseconds for the outgoing FIFO to become empty; if
  761. // not empty by the required time, returns false and error in pB->i2eError,
  762. // otherwise returns true.
  763. //
  764. // mSdelay == 0 is taken to mean must be empty on the first test.
  765. //
  766. // This version operates on IntelliPort-II - style FIFO's
  767. //
  768. // Note this routine is organized so that if status is ok there is no delay at
  769. // all called either before or after the test. Is called indirectly through
  770. // pB->i2eWaitForTxEmpty.
  771. //
  772. //******************************************************************************
  773. static int
  774. iiWaitForTxEmptyII(i2eBordStrPtr pB, int mSdelay)
  775. {
  776. unsigned long flags;
  777. int itemp;
  778. for (;;)
  779. {
  780. // This routine hinges on being able to see the "other" status register
  781. // (as seen by the local processor). His incoming fifo is our outgoing
  782. // FIFO.
  783. //
  784. // By the nature of this routine, you would be using this as part of a
  785. // larger atomic context: i.e., you would use this routine to ensure the
  786. // fifo empty, then act on this information. Between these two halves,
  787. // you will generally not want to service interrupts or in any way
  788. // disrupt the assumptions implicit in the larger context.
  789. //
  790. // Even worse, however, this routine "shifts" the status register to
  791. // point to the local status register which is not the usual situation.
  792. // Therefore for extra safety, we force the critical section to be
  793. // completely atomic, and pick up after ourselves before allowing any
  794. // interrupts of any kind.
  795. write_lock_irqsave(&Dl_spinlock, flags);
  796. outb(SEL_COMMAND, pB->i2ePointer);
  797. outb(SEL_CMD_SH, pB->i2ePointer);
  798. itemp = inb(pB->i2eStatus);
  799. outb(SEL_COMMAND, pB->i2ePointer);
  800. outb(SEL_CMD_UNSH, pB->i2ePointer);
  801. if (itemp & ST_IN_EMPTY)
  802. {
  803. I2_UPDATE_FIFO_ROOM(pB);
  804. write_unlock_irqrestore(&Dl_spinlock, flags);
  805. I2_COMPLETE(pB, I2EE_GOOD);
  806. }
  807. write_unlock_irqrestore(&Dl_spinlock, flags);
  808. if (mSdelay-- == 0)
  809. break;
  810. iiDelay(pB, 1); /* 1 mS granularity on checking condition */
  811. }
  812. I2_COMPLETE(pB, I2EE_TXE_TIME);
  813. }
  814. //******************************************************************************
  815. // Function: iiWaitForTxEmptyIIEX(pB, mSdelay)
  816. // Parameters: pB - pointer to board structure
  817. // mSdelay - period to wait before returning
  818. //
  819. // Returns: True if the FIFO is empty.
  820. // False if it not empty in the required time: the pB->i2eError
  821. // field has the error.
  822. //
  823. // Description:
  824. //
  825. // Waits up to "mSdelay" milliseconds for the outgoing FIFO to become empty; if
  826. // not empty by the required time, returns false and error in pB->i2eError,
  827. // otherwise returns true.
  828. //
  829. // mSdelay == 0 is taken to mean must be empty on the first test.
  830. //
  831. // This version operates on IntelliPort-IIEX - style FIFO's
  832. //
  833. // Note this routine is organized so that if status is ok there is no delay at
  834. // all called either before or after the test. Is called indirectly through
  835. // pB->i2eWaitForTxEmpty.
  836. //
  837. //******************************************************************************
  838. static int
  839. iiWaitForTxEmptyIIEX(i2eBordStrPtr pB, int mSdelay)
  840. {
  841. unsigned long flags;
  842. for (;;)
  843. {
  844. // By the nature of this routine, you would be using this as part of a
  845. // larger atomic context: i.e., you would use this routine to ensure the
  846. // fifo empty, then act on this information. Between these two halves,
  847. // you will generally not want to service interrupts or in any way
  848. // disrupt the assumptions implicit in the larger context.
  849. write_lock_irqsave(&Dl_spinlock, flags);
  850. if (inb(pB->i2eStatus) & STE_OUT_MT) {
  851. I2_UPDATE_FIFO_ROOM(pB);
  852. write_unlock_irqrestore(&Dl_spinlock, flags);
  853. I2_COMPLETE(pB, I2EE_GOOD);
  854. }
  855. write_unlock_irqrestore(&Dl_spinlock, flags);
  856. if (mSdelay-- == 0)
  857. break;
  858. iiDelay(pB, 1); // 1 mS granularity on checking condition
  859. }
  860. I2_COMPLETE(pB, I2EE_TXE_TIME);
  861. }
  862. //******************************************************************************
  863. // Function: iiTxMailEmptyII(pB)
  864. // Parameters: pB - pointer to board structure
  865. //
  866. // Returns: True if the transmit mailbox is empty.
  867. // False if it not empty.
  868. //
  869. // Description:
  870. //
  871. // Returns true or false according to whether the transmit mailbox is empty (and
  872. // therefore able to accept more mail)
  873. //
  874. // This version operates on IntelliPort-II - style FIFO's
  875. //
  876. //******************************************************************************
  877. static int
  878. iiTxMailEmptyII(i2eBordStrPtr pB)
  879. {
  880. int port = pB->i2ePointer;
  881. outb(SEL_OUTMAIL, port);
  882. return inb(port) == 0;
  883. }
  884. //******************************************************************************
  885. // Function: iiTxMailEmptyIIEX(pB)
  886. // Parameters: pB - pointer to board structure
  887. //
  888. // Returns: True if the transmit mailbox is empty.
  889. // False if it not empty.
  890. //
  891. // Description:
  892. //
  893. // Returns true or false according to whether the transmit mailbox is empty (and
  894. // therefore able to accept more mail)
  895. //
  896. // This version operates on IntelliPort-IIEX - style FIFO's
  897. //
  898. //******************************************************************************
  899. static int
  900. iiTxMailEmptyIIEX(i2eBordStrPtr pB)
  901. {
  902. return !(inb(pB->i2eStatus) & STE_OUT_MAIL);
  903. }
  904. //******************************************************************************
  905. // Function: iiTrySendMailII(pB,mail)
  906. // Parameters: pB - pointer to board structure
  907. // mail - value to write to mailbox
  908. //
  909. // Returns: True if the transmit mailbox is empty, and mail is sent.
  910. // False if it not empty.
  911. //
  912. // Description:
  913. //
  914. // If outgoing mailbox is empty, sends mail and returns true. If outgoing
  915. // mailbox is not empty, returns false.
  916. //
  917. // This version operates on IntelliPort-II - style FIFO's
  918. //
  919. //******************************************************************************
  920. static int
  921. iiTrySendMailII(i2eBordStrPtr pB, unsigned char mail)
  922. {
  923. int port = pB->i2ePointer;
  924. outb(SEL_OUTMAIL, port);
  925. if (inb(port) == 0) {
  926. outb(SEL_OUTMAIL, port);
  927. outb(mail, port);
  928. return 1;
  929. }
  930. return 0;
  931. }
  932. //******************************************************************************
  933. // Function: iiTrySendMailIIEX(pB,mail)
  934. // Parameters: pB - pointer to board structure
  935. // mail - value to write to mailbox
  936. //
  937. // Returns: True if the transmit mailbox is empty, and mail is sent.
  938. // False if it not empty.
  939. //
  940. // Description:
  941. //
  942. // If outgoing mailbox is empty, sends mail and returns true. If outgoing
  943. // mailbox is not empty, returns false.
  944. //
  945. // This version operates on IntelliPort-IIEX - style FIFO's
  946. //
  947. //******************************************************************************
  948. static int
  949. iiTrySendMailIIEX(i2eBordStrPtr pB, unsigned char mail)
  950. {
  951. if (inb(pB->i2eStatus) & STE_OUT_MAIL)
  952. return 0;
  953. outb(mail, pB->i2eXMail);
  954. return 1;
  955. }
  956. //******************************************************************************
  957. // Function: iiGetMailII(pB,mail)
  958. // Parameters: pB - pointer to board structure
  959. //
  960. // Returns: Mailbox data or NO_MAIL_HERE.
  961. //
  962. // Description:
  963. //
  964. // If no mail available, returns NO_MAIL_HERE otherwise returns the data from
  965. // the mailbox, which is guaranteed != NO_MAIL_HERE.
  966. //
  967. // This version operates on IntelliPort-II - style FIFO's
  968. //
  969. //******************************************************************************
  970. static unsigned short
  971. iiGetMailII(i2eBordStrPtr pB)
  972. {
  973. if (I2_HAS_MAIL(pB)) {
  974. outb(SEL_INMAIL, pB->i2ePointer);
  975. return inb(pB->i2ePointer);
  976. } else {
  977. return NO_MAIL_HERE;
  978. }
  979. }
  980. //******************************************************************************
  981. // Function: iiGetMailIIEX(pB,mail)
  982. // Parameters: pB - pointer to board structure
  983. //
  984. // Returns: Mailbox data or NO_MAIL_HERE.
  985. //
  986. // Description:
  987. //
  988. // If no mail available, returns NO_MAIL_HERE otherwise returns the data from
  989. // the mailbox, which is guaranteed != NO_MAIL_HERE.
  990. //
  991. // This version operates on IntelliPort-IIEX - style FIFO's
  992. //
  993. //******************************************************************************
  994. static unsigned short
  995. iiGetMailIIEX(i2eBordStrPtr pB)
  996. {
  997. if (I2_HAS_MAIL(pB))
  998. return inb(pB->i2eXMail);
  999. else
  1000. return NO_MAIL_HERE;
  1001. }
  1002. //******************************************************************************
  1003. // Function: iiEnableMailIrqII(pB)
  1004. // Parameters: pB - pointer to board structure
  1005. //
  1006. // Returns: Nothing
  1007. //
  1008. // Description:
  1009. //
  1010. // Enables board to interrupt host (only) by writing to host's in-bound mailbox.
  1011. //
  1012. // This version operates on IntelliPort-II - style FIFO's
  1013. //
  1014. //******************************************************************************
  1015. static void
  1016. iiEnableMailIrqII(i2eBordStrPtr pB)
  1017. {
  1018. outb(SEL_MASK, pB->i2ePointer);
  1019. outb(ST_IN_MAIL, pB->i2ePointer);
  1020. }
  1021. //******************************************************************************
  1022. // Function: iiEnableMailIrqIIEX(pB)
  1023. // Parameters: pB - pointer to board structure
  1024. //
  1025. // Returns: Nothing
  1026. //
  1027. // Description:
  1028. //
  1029. // Enables board to interrupt host (only) by writing to host's in-bound mailbox.
  1030. //
  1031. // This version operates on IntelliPort-IIEX - style FIFO's
  1032. //
  1033. //******************************************************************************
  1034. static void
  1035. iiEnableMailIrqIIEX(i2eBordStrPtr pB)
  1036. {
  1037. outb(MX_IN_MAIL, pB->i2eXMask);
  1038. }
  1039. //******************************************************************************
  1040. // Function: iiWriteMaskII(pB)
  1041. // Parameters: pB - pointer to board structure
  1042. //
  1043. // Returns: Nothing
  1044. //
  1045. // Description:
  1046. //
  1047. // Writes arbitrary value to the mask register.
  1048. //
  1049. // This version operates on IntelliPort-II - style FIFO's
  1050. //
  1051. //******************************************************************************
  1052. static void
  1053. iiWriteMaskII(i2eBordStrPtr pB, unsigned char value)
  1054. {
  1055. outb(SEL_MASK, pB->i2ePointer);
  1056. outb(value, pB->i2ePointer);
  1057. }
  1058. //******************************************************************************
  1059. // Function: iiWriteMaskIIEX(pB)
  1060. // Parameters: pB - pointer to board structure
  1061. //
  1062. // Returns: Nothing
  1063. //
  1064. // Description:
  1065. //
  1066. // Writes arbitrary value to the mask register.
  1067. //
  1068. // This version operates on IntelliPort-IIEX - style FIFO's
  1069. //
  1070. //******************************************************************************
  1071. static void
  1072. iiWriteMaskIIEX(i2eBordStrPtr pB, unsigned char value)
  1073. {
  1074. outb(value, pB->i2eXMask);
  1075. }
  1076. //******************************************************************************
  1077. // Function: iiDownloadBlock(pB, pSource, isStandard)
  1078. // Parameters: pB - pointer to board structure
  1079. // pSource - loadware block to download
  1080. // isStandard - True if "standard" loadware, else false.
  1081. //
  1082. // Returns: Success or Failure
  1083. //
  1084. // Description:
  1085. //
  1086. // Downloads a single block (at pSource)to the board referenced by pB. Caller
  1087. // sets isStandard to true/false according to whether the "standard" loadware is
  1088. // what's being loaded. The normal process, then, is to perform an iiInitialize
  1089. // to the board, then perform some number of iiDownloadBlocks using the returned
  1090. // state to determine when download is complete.
  1091. //
  1092. // Possible return values: (see I2ELLIS.H)
  1093. // II_DOWN_BADVALID
  1094. // II_DOWN_BADFILE
  1095. // II_DOWN_CONTINUING
  1096. // II_DOWN_GOOD
  1097. // II_DOWN_BAD
  1098. // II_DOWN_BADSTATE
  1099. // II_DOWN_TIMEOUT
  1100. //
  1101. // Uses the i2eState and i2eToLoad fields (initialized at iiInitialize) to
  1102. // determine whether this is the first block, whether to check for magic
  1103. // numbers, how many blocks there are to go...
  1104. //
  1105. //******************************************************************************
  1106. static int
  1107. iiDownloadBlock ( i2eBordStrPtr pB, loadHdrStrPtr pSource, int isStandard)
  1108. {
  1109. int itemp;
  1110. int loadedFirst;
  1111. if (pB->i2eValid != I2E_MAGIC) return II_DOWN_BADVALID;
  1112. switch(pB->i2eState)
  1113. {
  1114. case II_STATE_READY:
  1115. // Loading the first block after reset. Must check the magic number of the
  1116. // loadfile, store the number of blocks we expect to load.
  1117. if (pSource->e.loadMagic != MAGIC_LOADFILE)
  1118. {
  1119. return II_DOWN_BADFILE;
  1120. }
  1121. // Next we store the total number of blocks to load, including this one.
  1122. pB->i2eToLoad = 1 + pSource->e.loadBlocksMore;
  1123. // Set the state, store the version numbers. ('Cause this may have come
  1124. // from a file - we might want to report these versions and revisions in
  1125. // case of an error!
  1126. pB->i2eState = II_STATE_LOADING;
  1127. pB->i2eLVersion = pSource->e.loadVersion;
  1128. pB->i2eLRevision = pSource->e.loadRevision;
  1129. pB->i2eLSub = pSource->e.loadSubRevision;
  1130. // The time and date of compilation is also available but don't bother
  1131. // storing it for normal purposes.
  1132. loadedFirst = 1;
  1133. break;
  1134. case II_STATE_LOADING:
  1135. loadedFirst = 0;
  1136. break;
  1137. default:
  1138. return II_DOWN_BADSTATE;
  1139. }
  1140. // Now we must be in the II_STATE_LOADING state, and we assume i2eToLoad
  1141. // must be positive still, because otherwise we would have cleaned up last
  1142. // time and set the state to II_STATE_LOADED.
  1143. if (!iiWaitForTxEmpty(pB, MAX_DLOAD_READ_TIME)) {
  1144. return II_DOWN_TIMEOUT;
  1145. }
  1146. if (!iiWriteBuf(pB, pSource->c, LOADWARE_BLOCK_SIZE)) {
  1147. return II_DOWN_BADVALID;
  1148. }
  1149. // If we just loaded the first block, wait for the fifo to empty an extra
  1150. // long time to allow for any special startup code in the firmware, like
  1151. // sending status messages to the LCD's.
  1152. if (loadedFirst) {
  1153. if (!iiWaitForTxEmpty(pB, MAX_DLOAD_START_TIME)) {
  1154. return II_DOWN_TIMEOUT;
  1155. }
  1156. }
  1157. // Determine whether this was our last block!
  1158. if (--(pB->i2eToLoad)) {
  1159. return II_DOWN_CONTINUING; // more to come...
  1160. }
  1161. // It WAS our last block: Clean up operations...
  1162. // ...Wait for last buffer to drain from the board...
  1163. if (!iiWaitForTxEmpty(pB, MAX_DLOAD_READ_TIME)) {
  1164. return II_DOWN_TIMEOUT;
  1165. }
  1166. // If there were only a single block written, this would come back
  1167. // immediately and be harmless, though not strictly necessary.
  1168. itemp = MAX_DLOAD_ACK_TIME/10;
  1169. while (--itemp) {
  1170. if (I2_HAS_INPUT(pB)) {
  1171. switch (inb(pB->i2eData)) {
  1172. case LOADWARE_OK:
  1173. pB->i2eState =
  1174. isStandard ? II_STATE_STDLOADED :II_STATE_LOADED;
  1175. // Some revisions of the bootstrap firmware (e.g. ISA-8 1.0.2)
  1176. // will, // if there is a debug port attached, require some
  1177. // time to send information to the debug port now. It will do
  1178. // this before // executing any of the code we just downloaded.
  1179. // It may take up to 700 milliseconds.
  1180. if (pB->i2ePom.e.porDiag2 & POR_DEBUG_PORT) {
  1181. iiDelay(pB, 700);
  1182. }
  1183. return II_DOWN_GOOD;
  1184. case LOADWARE_BAD:
  1185. default:
  1186. return II_DOWN_BAD;
  1187. }
  1188. }
  1189. iiDelay(pB, 10); // 10 mS granularity on checking condition
  1190. }
  1191. // Drop-through --> timed out waiting for firmware confirmation
  1192. pB->i2eState = II_STATE_BADLOAD;
  1193. return II_DOWN_TIMEOUT;
  1194. }
  1195. //******************************************************************************
  1196. // Function: iiDownloadAll(pB, pSource, isStandard, size)
  1197. // Parameters: pB - pointer to board structure
  1198. // pSource - loadware block to download
  1199. // isStandard - True if "standard" loadware, else false.
  1200. // size - size of data to download (in bytes)
  1201. //
  1202. // Returns: Success or Failure
  1203. //
  1204. // Description:
  1205. //
  1206. // Given a pointer to a board structure, a pointer to the beginning of some
  1207. // loadware, whether it is considered the "standard loadware", and the size of
  1208. // the array in bytes loads the entire array to the board as loadware.
  1209. //
  1210. // Assumes the board has been freshly reset and the power-up reset message read.
  1211. // (i.e., in II_STATE_READY). Complains if state is bad, or if there seems to be
  1212. // too much or too little data to load, or if iiDownloadBlock complains.
  1213. //******************************************************************************
  1214. static int
  1215. iiDownloadAll(i2eBordStrPtr pB, loadHdrStrPtr pSource, int isStandard, int size)
  1216. {
  1217. int status;
  1218. // We know (from context) board should be ready for the first block of
  1219. // download. Complain if not.
  1220. if (pB->i2eState != II_STATE_READY) return II_DOWN_BADSTATE;
  1221. while (size > 0) {
  1222. size -= LOADWARE_BLOCK_SIZE; // How much data should there be left to
  1223. // load after the following operation ?
  1224. // Note we just bump pSource by "one", because its size is actually that
  1225. // of an entire block, same as LOADWARE_BLOCK_SIZE.
  1226. status = iiDownloadBlock(pB, pSource++, isStandard);
  1227. switch(status)
  1228. {
  1229. case II_DOWN_GOOD:
  1230. return ( (size > 0) ? II_DOWN_OVER : II_DOWN_GOOD);
  1231. case II_DOWN_CONTINUING:
  1232. break;
  1233. default:
  1234. return status;
  1235. }
  1236. }
  1237. // We shouldn't drop out: it means "while" caught us with nothing left to
  1238. // download, yet the previous DownloadBlock did not return complete. Ergo,
  1239. // not enough data to match the size byte in the header.
  1240. return II_DOWN_UNDER;
  1241. }