sdladrv.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318
  1. /*****************************************************************************
  2. * sdladrv.c SDLA Support Module. Main module.
  3. *
  4. * This module is a library of common hardware-specific functions
  5. * used by all Sangoma drivers.
  6. *
  7. * Author: Gideon Hack
  8. *
  9. * Copyright: (c) 1995-2000 Sangoma Technologies Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. * ============================================================================
  16. * Mar 20, 2001 Nenad Corbic Added the auto_pci_cfg filed, to support
  17. * the PCISLOT #0.
  18. * Apr 04, 2000 Nenad Corbic Fixed the auto memory detection code.
  19. * The memory test at address 0xC8000.
  20. * Mar 09, 2000 Nenad Corbic Added Gideon's Bug Fix: clear pci
  21. * interrupt flags on initial load.
  22. * Jun 02, 1999 Gideon Hack Added support for the S514 adapter.
  23. * Updates for Linux 2.2.X kernels.
  24. * Sep 17, 1998 Jaspreet Singh Updates for linux 2.2.X kernels
  25. * Dec 20, 1996 Gene Kozin Version 3.0.0. Complete overhaul.
  26. * Jul 12, 1996 Gene Kozin Changes for Linux 2.0 compatibility.
  27. * Jun 12, 1996 Gene Kozin Added support for S503 card.
  28. * Apr 30, 1996 Gene Kozin SDLA hardware interrupt is acknowledged before
  29. * calling protocolspecific ISR.
  30. * Register I/O ports with Linux kernel.
  31. * Miscellaneous bug fixes.
  32. * Dec 20, 1995 Gene Kozin Fixed a bug in interrupt routine.
  33. * Oct 14, 1995 Gene Kozin Initial version.
  34. *****************************************************************************/
  35. /*****************************************************************************
  36. * Notes:
  37. * ------
  38. * 1. This code is ment to be system-independent (as much as possible). To
  39. * achive this, various macros are used to hide system-specific interfaces.
  40. * To compile this code, one of the following constants must be defined:
  41. *
  42. * Platform Define
  43. * -------- ------
  44. * Linux _LINUX_
  45. * SCO Unix _SCO_UNIX_
  46. *
  47. * 2. Supported adapter types:
  48. *
  49. * S502A
  50. * ES502A (S502E)
  51. * S503
  52. * S507
  53. * S508 (S509)
  54. *
  55. * 3. S502A Notes:
  56. *
  57. * There is no separate DPM window enable/disable control in S502A. It
  58. * opens immediately after a window number it written to the HMCR
  59. * register. To close the window, HMCR has to be written a value
  60. * ????1111b (e.g. 0x0F or 0xFF).
  61. *
  62. * S502A DPM window cannot be located at offset E000 (e.g. 0xAE000).
  63. *
  64. * There should be a delay of ??? before reading back S502A status
  65. * register.
  66. *
  67. * 4. S502E Notes:
  68. *
  69. * S502E has a h/w bug: although default IRQ line state is HIGH, enabling
  70. * interrupts by setting bit 1 of the control register (BASE) to '1'
  71. * causes it to go LOW! Therefore, disabling interrupts by setting that
  72. * bit to '0' causes low-to-high transition on IRQ line (ghosty
  73. * interrupt). The same occurs when disabling CPU by resetting bit 0 of
  74. * CPU control register (BASE+3) - see the next note.
  75. *
  76. * S502E CPU and DPM control is limited:
  77. *
  78. * o CPU cannot be stopped independently. Resetting bit 0 of the CPUi
  79. * control register (BASE+3) shuts the board down entirely, including
  80. * DPM;
  81. *
  82. * o DPM access cannot be controlled dynamically. Ones CPU is started,
  83. * bit 1 of the control register (BASE) is used to enable/disable IRQ,
  84. * so that access to shared memory cannot be disabled while CPU is
  85. * running.
  86. ****************************************************************************/
  87. #define _LINUX_
  88. #if defined(_LINUX_) /****** Linux *******************************/
  89. #include <linux/config.h>
  90. #include <linux/kernel.h> /* printk(), and other useful stuff */
  91. #include <linux/stddef.h> /* offsetof(), etc. */
  92. #include <linux/errno.h> /* return codes */
  93. #include <linux/string.h> /* inline memset(), etc. */
  94. #include <linux/module.h> /* support for loadable modules */
  95. #include <linux/jiffies.h> /* for jiffies, HZ, etc. */
  96. #include <linux/sdladrv.h> /* API definitions */
  97. #include <linux/sdlasfm.h> /* SDLA firmware module definitions */
  98. #include <linux/sdlapci.h> /* SDLA PCI hardware definitions */
  99. #include <linux/pci.h> /* PCI defines and function prototypes */
  100. #include <asm/io.h> /* for inb(), outb(), etc. */
  101. #define _INB(port) (inb(port))
  102. #define _OUTB(port, byte) (outb((byte),(port)))
  103. #define SYSTEM_TICK jiffies
  104. #include <linux/init.h>
  105. #elif defined(_SCO_UNIX_) /****** SCO Unix ****************************/
  106. #if !defined(INKERNEL)
  107. #error This code MUST be compiled in kernel mode!
  108. #endif
  109. #include <sys/sdladrv.h> /* API definitions */
  110. #include <sys/sdlasfm.h> /* SDLA firmware module definitions */
  111. #include <sys/inline.h> /* for inb(), outb(), etc. */
  112. #define _INB(port) (inb(port))
  113. #define _OUTB(port, byte) (outb((port),(byte)))
  114. #define SYSTEM_TICK lbolt
  115. #else
  116. #error Unknown system type!
  117. #endif
  118. #define MOD_VERSION 3
  119. #define MOD_RELEASE 0
  120. #define SDLA_IODELAY 100 /* I/O Rd/Wr delay, 10 works for 486DX2-66 */
  121. #define EXEC_DELAY 20 /* shared memory access delay, mks */
  122. #define EXEC_TIMEOUT (HZ*2) /* command timeout, in ticks */
  123. /* I/O port address range */
  124. #define S502A_IORANGE 3
  125. #define S502E_IORANGE 4
  126. #define S503_IORANGE 3
  127. #define S507_IORANGE 4
  128. #define S508_IORANGE 4
  129. /* Maximum amount of memory */
  130. #define S502_MAXMEM 0x10000L
  131. #define S503_MAXMEM 0x10000L
  132. #define S507_MAXMEM 0x40000L
  133. #define S508_MAXMEM 0x40000L
  134. /* Minimum amount of memory */
  135. #define S502_MINMEM 0x8000L
  136. #define S503_MINMEM 0x8000L
  137. #define S507_MINMEM 0x20000L
  138. #define S508_MINMEM 0x20000L
  139. #define NO_PORT -1
  140. /****** Function Prototypes *************************************************/
  141. /* Hardware-specific functions */
  142. static int sdla_detect (sdlahw_t* hw);
  143. static int sdla_autodpm (sdlahw_t* hw);
  144. static int sdla_setdpm (sdlahw_t* hw);
  145. static int sdla_load (sdlahw_t* hw, sfm_t* sfm, unsigned len);
  146. static int sdla_init (sdlahw_t* hw);
  147. static unsigned long sdla_memtest (sdlahw_t* hw);
  148. static int sdla_bootcfg (sdlahw_t* hw, sfm_info_t* sfminfo);
  149. static unsigned char make_config_byte (sdlahw_t* hw);
  150. static int sdla_start (sdlahw_t* hw, unsigned addr);
  151. static int init_s502a (sdlahw_t* hw);
  152. static int init_s502e (sdlahw_t* hw);
  153. static int init_s503 (sdlahw_t* hw);
  154. static int init_s507 (sdlahw_t* hw);
  155. static int init_s508 (sdlahw_t* hw);
  156. static int detect_s502a (int port);
  157. static int detect_s502e (int port);
  158. static int detect_s503 (int port);
  159. static int detect_s507 (int port);
  160. static int detect_s508 (int port);
  161. static int detect_s514 (sdlahw_t* hw);
  162. static int find_s514_adapter(sdlahw_t* hw, char find_first_S514_card);
  163. /* Miscellaneous functions */
  164. static void peek_by_4 (unsigned long src, void* buf, unsigned len);
  165. static void poke_by_4 (unsigned long dest, void* buf, unsigned len);
  166. static int calibrate_delay (int mks);
  167. static int get_option_index (unsigned* optlist, unsigned optval);
  168. static unsigned check_memregion (void* ptr, unsigned len);
  169. static unsigned test_memregion (void* ptr, unsigned len);
  170. static unsigned short checksum (unsigned char* buf, unsigned len);
  171. static int init_pci_slot(sdlahw_t *);
  172. static int pci_probe(sdlahw_t *hw);
  173. /****** Global Data **********************************************************
  174. * Note: All data must be explicitly initialized!!!
  175. */
  176. static struct pci_device_id sdladrv_pci_tbl[] = {
  177. { V3_VENDOR_ID, V3_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, },
  178. { } /* Terminating entry */
  179. };
  180. MODULE_DEVICE_TABLE(pci, sdladrv_pci_tbl);
  181. MODULE_LICENSE("GPL");
  182. /* private data */
  183. static char modname[] = "sdladrv";
  184. static char fullname[] = "SDLA Support Module";
  185. static char copyright[] = "(c) 1995-1999 Sangoma Technologies Inc.";
  186. static unsigned exec_idle;
  187. /* Hardware configuration options.
  188. * These are arrays of configuration options used by verification routines.
  189. * The first element of each array is its size (i.e. number of options).
  190. */
  191. static unsigned s502_port_options[] =
  192. { 4, 0x250, 0x300, 0x350, 0x360 }
  193. ;
  194. static unsigned s503_port_options[] =
  195. { 8, 0x250, 0x254, 0x300, 0x304, 0x350, 0x354, 0x360, 0x364 }
  196. ;
  197. static unsigned s508_port_options[] =
  198. { 8, 0x250, 0x270, 0x280, 0x300, 0x350, 0x360, 0x380, 0x390 }
  199. ;
  200. static unsigned s502a_irq_options[] = { 0 };
  201. static unsigned s502e_irq_options[] = { 4, 2, 3, 5, 7 };
  202. static unsigned s503_irq_options[] = { 5, 2, 3, 4, 5, 7 };
  203. static unsigned s508_irq_options[] = { 8, 3, 4, 5, 7, 10, 11, 12, 15 };
  204. static unsigned s502a_dpmbase_options[] =
  205. {
  206. 28,
  207. 0xA0000, 0xA2000, 0xA4000, 0xA6000, 0xA8000, 0xAA000, 0xAC000,
  208. 0xC0000, 0xC2000, 0xC4000, 0xC6000, 0xC8000, 0xCA000, 0xCC000,
  209. 0xD0000, 0xD2000, 0xD4000, 0xD6000, 0xD8000, 0xDA000, 0xDC000,
  210. 0xE0000, 0xE2000, 0xE4000, 0xE6000, 0xE8000, 0xEA000, 0xEC000,
  211. };
  212. static unsigned s507_dpmbase_options[] =
  213. {
  214. 32,
  215. 0xA0000, 0xA2000, 0xA4000, 0xA6000, 0xA8000, 0xAA000, 0xAC000, 0xAE000,
  216. 0xB0000, 0xB2000, 0xB4000, 0xB6000, 0xB8000, 0xBA000, 0xBC000, 0xBE000,
  217. 0xC0000, 0xC2000, 0xC4000, 0xC6000, 0xC8000, 0xCA000, 0xCC000, 0xCE000,
  218. 0xE0000, 0xE2000, 0xE4000, 0xE6000, 0xE8000, 0xEA000, 0xEC000, 0xEE000,
  219. };
  220. static unsigned s508_dpmbase_options[] = /* incl. S502E and S503 */
  221. {
  222. 32,
  223. 0xA0000, 0xA2000, 0xA4000, 0xA6000, 0xA8000, 0xAA000, 0xAC000, 0xAE000,
  224. 0xC0000, 0xC2000, 0xC4000, 0xC6000, 0xC8000, 0xCA000, 0xCC000, 0xCE000,
  225. 0xD0000, 0xD2000, 0xD4000, 0xD6000, 0xD8000, 0xDA000, 0xDC000, 0xDE000,
  226. 0xE0000, 0xE2000, 0xE4000, 0xE6000, 0xE8000, 0xEA000, 0xEC000, 0xEE000,
  227. };
  228. /*
  229. static unsigned s502_dpmsize_options[] = { 2, 0x2000, 0x10000 };
  230. static unsigned s507_dpmsize_options[] = { 2, 0x2000, 0x4000 };
  231. static unsigned s508_dpmsize_options[] = { 1, 0x2000 };
  232. */
  233. static unsigned s502a_pclk_options[] = { 2, 3600, 7200 };
  234. static unsigned s502e_pclk_options[] = { 5, 3600, 5000, 7200, 8000, 10000 };
  235. static unsigned s503_pclk_options[] = { 3, 7200, 8000, 10000 };
  236. static unsigned s507_pclk_options[] = { 1, 12288 };
  237. static unsigned s508_pclk_options[] = { 1, 16000 };
  238. /* Host memory control register masks */
  239. static unsigned char s502a_hmcr[] =
  240. {
  241. 0x10, 0x12, 0x14, 0x16, 0x18, 0x1A, 0x1C, /* A0000 - AC000 */
  242. 0x20, 0x22, 0x24, 0x26, 0x28, 0x2A, 0x2C, /* C0000 - CC000 */
  243. 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, /* D0000 - DC000 */
  244. 0x30, 0x32, 0x34, 0x36, 0x38, 0x3A, 0x3C, /* E0000 - EC000 */
  245. };
  246. static unsigned char s502e_hmcr[] =
  247. {
  248. 0x10, 0x12, 0x14, 0x16, 0x18, 0x1A, 0x1C, 0x1E, /* A0000 - AE000 */
  249. 0x20, 0x22, 0x24, 0x26, 0x28, 0x2A, 0x2C, 0x2E, /* C0000 - CE000 */
  250. 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, /* D0000 - DE000 */
  251. 0x30, 0x32, 0x34, 0x36, 0x38, 0x3A, 0x3C, 0x3E, /* E0000 - EE000 */
  252. };
  253. static unsigned char s507_hmcr[] =
  254. {
  255. 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, /* A0000 - AE000 */
  256. 0x40, 0x42, 0x44, 0x46, 0x48, 0x4A, 0x4C, 0x4E, /* B0000 - BE000 */
  257. 0x80, 0x82, 0x84, 0x86, 0x88, 0x8A, 0x8C, 0x8E, /* C0000 - CE000 */
  258. 0xC0, 0xC2, 0xC4, 0xC6, 0xC8, 0xCA, 0xCC, 0xCE, /* E0000 - EE000 */
  259. };
  260. static unsigned char s508_hmcr[] =
  261. {
  262. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* A0000 - AE000 */
  263. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* C0000 - CE000 */
  264. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, /* D0000 - DE000 */
  265. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, /* E0000 - EE000 */
  266. };
  267. static unsigned char s507_irqmask[] =
  268. {
  269. 0x00, 0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xE0
  270. };
  271. static int pci_slot_ar[MAX_S514_CARDS];
  272. /******* Kernel Loadable Module Entry Points ********************************/
  273. /*============================================================================
  274. * Module 'insert' entry point.
  275. * o print announcement
  276. * o initialize static data
  277. * o calibrate SDLA shared memory access delay.
  278. *
  279. * Return: 0 Ok
  280. * < 0 error.
  281. * Context: process
  282. */
  283. static int __init sdladrv_init(void)
  284. {
  285. int i=0;
  286. printk(KERN_INFO "%s v%u.%u %s\n",
  287. fullname, MOD_VERSION, MOD_RELEASE, copyright);
  288. exec_idle = calibrate_delay(EXEC_DELAY);
  289. #ifdef WANDEBUG
  290. printk(KERN_DEBUG "%s: exec_idle = %d\n", modname, exec_idle);
  291. #endif
  292. /* Initialize the PCI Card array, which
  293. * will store flags, used to mark
  294. * card initialization state */
  295. for (i=0; i<MAX_S514_CARDS; i++)
  296. pci_slot_ar[i] = 0xFF;
  297. return 0;
  298. }
  299. /*============================================================================
  300. * Module 'remove' entry point.
  301. * o release all remaining system resources
  302. */
  303. static void __exit sdladrv_cleanup(void)
  304. {
  305. }
  306. module_init(sdladrv_init);
  307. module_exit(sdladrv_cleanup);
  308. /******* Kernel APIs ********************************************************/
  309. /*============================================================================
  310. * Set up adapter.
  311. * o detect adapter type
  312. * o verify hardware configuration options
  313. * o check for hardware conflicts
  314. * o set up adapter shared memory
  315. * o test adapter memory
  316. * o load firmware
  317. * Return: 0 ok.
  318. * < 0 error
  319. */
  320. EXPORT_SYMBOL(sdla_setup);
  321. int sdla_setup (sdlahw_t* hw, void* sfm, unsigned len)
  322. {
  323. unsigned* irq_opt = NULL; /* IRQ options */
  324. unsigned* dpmbase_opt = NULL; /* DPM window base options */
  325. unsigned* pclk_opt = NULL; /* CPU clock rate options */
  326. int err=0;
  327. if (sdla_detect(hw)) {
  328. if(hw->type != SDLA_S514)
  329. printk(KERN_INFO "%s: no SDLA card found at port 0x%X\n",
  330. modname, hw->port);
  331. return -EINVAL;
  332. }
  333. if(hw->type != SDLA_S514) {
  334. printk(KERN_INFO "%s: found S%04u card at port 0x%X.\n",
  335. modname, hw->type, hw->port);
  336. hw->dpmsize = SDLA_WINDOWSIZE;
  337. switch (hw->type) {
  338. case SDLA_S502A:
  339. hw->io_range = S502A_IORANGE;
  340. irq_opt = s502a_irq_options;
  341. dpmbase_opt = s502a_dpmbase_options;
  342. pclk_opt = s502a_pclk_options;
  343. break;
  344. case SDLA_S502E:
  345. hw->io_range = S502E_IORANGE;
  346. irq_opt = s502e_irq_options;
  347. dpmbase_opt = s508_dpmbase_options;
  348. pclk_opt = s502e_pclk_options;
  349. break;
  350. case SDLA_S503:
  351. hw->io_range = S503_IORANGE;
  352. irq_opt = s503_irq_options;
  353. dpmbase_opt = s508_dpmbase_options;
  354. pclk_opt = s503_pclk_options;
  355. break;
  356. case SDLA_S507:
  357. hw->io_range = S507_IORANGE;
  358. irq_opt = s508_irq_options;
  359. dpmbase_opt = s507_dpmbase_options;
  360. pclk_opt = s507_pclk_options;
  361. break;
  362. case SDLA_S508:
  363. hw->io_range = S508_IORANGE;
  364. irq_opt = s508_irq_options;
  365. dpmbase_opt = s508_dpmbase_options;
  366. pclk_opt = s508_pclk_options;
  367. break;
  368. }
  369. /* Verify IRQ configuration options */
  370. if (!get_option_index(irq_opt, hw->irq)) {
  371. printk(KERN_INFO "%s: IRQ %d is invalid!\n",
  372. modname, hw->irq);
  373. return -EINVAL;
  374. }
  375. /* Verify CPU clock rate configuration options */
  376. if (hw->pclk == 0)
  377. hw->pclk = pclk_opt[1]; /* use default */
  378. else if (!get_option_index(pclk_opt, hw->pclk)) {
  379. printk(KERN_INFO "%s: CPU clock %u is invalid!\n",
  380. modname, hw->pclk);
  381. return -EINVAL;
  382. }
  383. printk(KERN_INFO "%s: assuming CPU clock rate of %u kHz.\n",
  384. modname, hw->pclk);
  385. /* Setup adapter dual-port memory window and test memory */
  386. if (hw->dpmbase == 0) {
  387. err = sdla_autodpm(hw);
  388. if (err) {
  389. printk(KERN_INFO
  390. "%s: can't find available memory region!\n",
  391. modname);
  392. return err;
  393. }
  394. }
  395. else if (!get_option_index(dpmbase_opt,
  396. virt_to_phys(hw->dpmbase))) {
  397. printk(KERN_INFO
  398. "%s: memory address 0x%lX is invalid!\n",
  399. modname, virt_to_phys(hw->dpmbase));
  400. return -EINVAL;
  401. }
  402. else if (sdla_setdpm(hw)) {
  403. printk(KERN_INFO
  404. "%s: 8K memory region at 0x%lX is not available!\n",
  405. modname, virt_to_phys(hw->dpmbase));
  406. return -EINVAL;
  407. }
  408. printk(KERN_INFO
  409. "%s: dual-port memory window is set at 0x%lX.\n",
  410. modname, virt_to_phys(hw->dpmbase));
  411. /* If we find memory in 0xE**** Memory region,
  412. * warn the user to disable the SHADOW RAM.
  413. * Since memory corruption can occur if SHADOW is
  414. * enabled. This can causes random crashes ! */
  415. if (virt_to_phys(hw->dpmbase) >= 0xE0000){
  416. printk(KERN_WARNING "\n%s: !!!!!!!! WARNING !!!!!!!!\n",modname);
  417. printk(KERN_WARNING "%s: WANPIPE is using 0x%lX memory region !!!\n",
  418. modname, virt_to_phys(hw->dpmbase));
  419. printk(KERN_WARNING " Please disable the SHADOW RAM, otherwise\n");
  420. printk(KERN_WARNING " your system might crash randomly from time to time !\n");
  421. printk(KERN_WARNING "%s: !!!!!!!! WARNING !!!!!!!!\n\n",modname);
  422. }
  423. }
  424. else {
  425. hw->memory = test_memregion((void*)hw->dpmbase,
  426. MAX_SIZEOF_S514_MEMORY);
  427. if(hw->memory < (256 * 1024)) {
  428. printk(KERN_INFO
  429. "%s: error in testing S514 memory (0x%lX)\n",
  430. modname, hw->memory);
  431. sdla_down(hw);
  432. return -EINVAL;
  433. }
  434. }
  435. printk(KERN_INFO "%s: found %luK bytes of on-board memory\n",
  436. modname, hw->memory / 1024);
  437. /* Load firmware. If loader fails then shut down adapter */
  438. err = sdla_load(hw, sfm, len);
  439. if (err) sdla_down(hw); /* shutdown adapter */
  440. return err;
  441. }
  442. /*============================================================================
  443. * Shut down SDLA: disable shared memory access and interrupts, stop CPU, etc.
  444. */
  445. EXPORT_SYMBOL(sdla_down);
  446. int sdla_down (sdlahw_t* hw)
  447. {
  448. unsigned port = hw->port;
  449. int i;
  450. unsigned char CPU_no;
  451. u32 int_config, int_status;
  452. if(!port && (hw->type != SDLA_S514))
  453. return -EFAULT;
  454. switch (hw->type) {
  455. case SDLA_S502A:
  456. _OUTB(port, 0x08); /* halt CPU */
  457. _OUTB(port, 0x08);
  458. _OUTB(port, 0x08);
  459. hw->regs[0] = 0x08;
  460. _OUTB(port + 1, 0xFF); /* close memory window */
  461. hw->regs[1] = 0xFF;
  462. break;
  463. case SDLA_S502E:
  464. _OUTB(port + 3, 0); /* stop CPU */
  465. _OUTB(port, 0); /* reset board */
  466. for (i = 0; i < S502E_IORANGE; ++i)
  467. hw->regs[i] = 0
  468. ;
  469. break;
  470. case SDLA_S503:
  471. case SDLA_S507:
  472. case SDLA_S508:
  473. _OUTB(port, 0); /* reset board logic */
  474. hw->regs[0] = 0;
  475. break;
  476. case SDLA_S514:
  477. /* halt the adapter */
  478. *(char *)hw->vector = S514_CPU_HALT;
  479. CPU_no = hw->S514_cpu_no[0];
  480. /* disable the PCI IRQ and disable memory access */
  481. pci_read_config_dword(hw->pci_dev, PCI_INT_CONFIG, &int_config);
  482. int_config &= (CPU_no == S514_CPU_A) ? ~PCI_DISABLE_IRQ_CPU_A : ~PCI_DISABLE_IRQ_CPU_B;
  483. pci_write_config_dword(hw->pci_dev, PCI_INT_CONFIG, int_config);
  484. read_S514_int_stat(hw, &int_status);
  485. S514_intack(hw, int_status);
  486. if(CPU_no == S514_CPU_A)
  487. pci_write_config_dword(hw->pci_dev, PCI_MAP0_DWORD,
  488. PCI_CPU_A_MEM_DISABLE);
  489. else
  490. pci_write_config_dword(hw->pci_dev, PCI_MAP1_DWORD,
  491. PCI_CPU_B_MEM_DISABLE);
  492. /* free up the allocated virtual memory */
  493. iounmap((void *)hw->dpmbase);
  494. iounmap((void *)hw->vector);
  495. break;
  496. default:
  497. return -EINVAL;
  498. }
  499. return 0;
  500. }
  501. /*============================================================================
  502. * Map shared memory window into SDLA address space.
  503. */
  504. EXPORT_SYMBOL(sdla_mapmem);
  505. int sdla_mapmem (sdlahw_t* hw, unsigned long addr)
  506. {
  507. unsigned port = hw->port;
  508. register int tmp;
  509. switch (hw->type) {
  510. case SDLA_S502A:
  511. case SDLA_S502E:
  512. if (addr < S502_MAXMEM) { /* verify parameter */
  513. tmp = addr >> 13; /* convert to register mask */
  514. _OUTB(port + 2, tmp);
  515. hw->regs[2] = tmp;
  516. }
  517. else return -EINVAL;
  518. break;
  519. case SDLA_S503:
  520. if (addr < S503_MAXMEM) { /* verify parameter */
  521. tmp = (hw->regs[0] & 0x8F) | ((addr >> 9) & 0x70);
  522. _OUTB(port, tmp);
  523. hw->regs[0] = tmp;
  524. }
  525. else return -EINVAL;
  526. break;
  527. case SDLA_S507:
  528. if (addr < S507_MAXMEM) {
  529. if (!(_INB(port) & 0x02))
  530. return -EIO;
  531. tmp = addr >> 13; /* convert to register mask */
  532. _OUTB(port + 2, tmp);
  533. hw->regs[2] = tmp;
  534. }
  535. else return -EINVAL;
  536. break;
  537. case SDLA_S508:
  538. if (addr < S508_MAXMEM) {
  539. tmp = addr >> 13; /* convert to register mask */
  540. _OUTB(port + 2, tmp);
  541. hw->regs[2] = tmp;
  542. }
  543. else return -EINVAL;
  544. break;
  545. case SDLA_S514:
  546. return 0;
  547. default:
  548. return -EINVAL;
  549. }
  550. hw->vector = addr & 0xFFFFE000L;
  551. return 0;
  552. }
  553. /*============================================================================
  554. * Enable interrupt generation.
  555. */
  556. EXPORT_SYMBOL(sdla_inten);
  557. int sdla_inten (sdlahw_t* hw)
  558. {
  559. unsigned port = hw->port;
  560. int tmp, i;
  561. switch (hw->type) {
  562. case SDLA_S502E:
  563. /* Note thar interrupt control operations on S502E are allowed
  564. * only if CPU is enabled (bit 0 of status register is set).
  565. */
  566. if (_INB(port) & 0x01) {
  567. _OUTB(port, 0x02); /* bit1 = 1, bit2 = 0 */
  568. _OUTB(port, 0x06); /* bit1 = 1, bit2 = 1 */
  569. hw->regs[0] = 0x06;
  570. }
  571. else return -EIO;
  572. break;
  573. case SDLA_S503:
  574. tmp = hw->regs[0] | 0x04;
  575. _OUTB(port, tmp);
  576. hw->regs[0] = tmp; /* update mirror */
  577. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  578. if (!(_INB(port) & 0x02)) /* verify */
  579. return -EIO;
  580. break;
  581. case SDLA_S508:
  582. tmp = hw->regs[0] | 0x10;
  583. _OUTB(port, tmp);
  584. hw->regs[0] = tmp; /* update mirror */
  585. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  586. if (!(_INB(port + 1) & 0x10)) /* verify */
  587. return -EIO;
  588. break;
  589. case SDLA_S502A:
  590. case SDLA_S507:
  591. break;
  592. case SDLA_S514:
  593. break;
  594. default:
  595. return -EINVAL;
  596. }
  597. return 0;
  598. }
  599. /*============================================================================
  600. * Disable interrupt generation.
  601. */
  602. EXPORT_SYMBOL(sdla_intde);
  603. int sdla_intde (sdlahw_t* hw)
  604. {
  605. unsigned port = hw->port;
  606. int tmp, i;
  607. switch (hw->type) {
  608. case SDLA_S502E:
  609. /* Notes:
  610. * 1) interrupt control operations are allowed only if CPU is
  611. * enabled (bit 0 of status register is set).
  612. * 2) disabling interrupts using bit 1 of control register
  613. * causes IRQ line go high, therefore we are going to use
  614. * 0x04 instead: lower it to inhibit interrupts to PC.
  615. */
  616. if (_INB(port) & 0x01) {
  617. _OUTB(port, hw->regs[0] & ~0x04);
  618. hw->regs[0] &= ~0x04;
  619. }
  620. else return -EIO;
  621. break;
  622. case SDLA_S503:
  623. tmp = hw->regs[0] & ~0x04;
  624. _OUTB(port, tmp);
  625. hw->regs[0] = tmp; /* update mirror */
  626. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  627. if (_INB(port) & 0x02) /* verify */
  628. return -EIO;
  629. break;
  630. case SDLA_S508:
  631. tmp = hw->regs[0] & ~0x10;
  632. _OUTB(port, tmp);
  633. hw->regs[0] = tmp; /* update mirror */
  634. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  635. if (_INB(port) & 0x10) /* verify */
  636. return -EIO;
  637. break;
  638. case SDLA_S502A:
  639. case SDLA_S507:
  640. break;
  641. default:
  642. return -EINVAL;
  643. }
  644. return 0;
  645. }
  646. /*============================================================================
  647. * Acknowledge SDLA hardware interrupt.
  648. */
  649. EXPORT_SYMBOL(sdla_intack);
  650. int sdla_intack (sdlahw_t* hw)
  651. {
  652. unsigned port = hw->port;
  653. int tmp;
  654. switch (hw->type) {
  655. case SDLA_S502E:
  656. /* To acknoledge hardware interrupt we have to toggle bit 3 of
  657. * control register: \_/
  658. * Note that interrupt control operations on S502E are allowed
  659. * only if CPU is enabled (bit 1 of status register is set).
  660. */
  661. if (_INB(port) & 0x01) {
  662. tmp = hw->regs[0] & ~0x04;
  663. _OUTB(port, tmp);
  664. tmp |= 0x04;
  665. _OUTB(port, tmp);
  666. hw->regs[0] = tmp;
  667. }
  668. else return -EIO;
  669. break;
  670. case SDLA_S503:
  671. if (_INB(port) & 0x04) {
  672. tmp = hw->regs[0] & ~0x08;
  673. _OUTB(port, tmp);
  674. tmp |= 0x08;
  675. _OUTB(port, tmp);
  676. hw->regs[0] = tmp;
  677. }
  678. break;
  679. case SDLA_S502A:
  680. case SDLA_S507:
  681. case SDLA_S508:
  682. break;
  683. default:
  684. return -EINVAL;
  685. }
  686. return 0;
  687. }
  688. /*============================================================================
  689. * Acknowledge S514 hardware interrupt.
  690. */
  691. EXPORT_SYMBOL(S514_intack);
  692. void S514_intack (sdlahw_t* hw, u32 int_status)
  693. {
  694. pci_write_config_dword(hw->pci_dev, PCI_INT_STATUS, int_status);
  695. }
  696. /*============================================================================
  697. * Read the S514 hardware interrupt status.
  698. */
  699. EXPORT_SYMBOL(read_S514_int_stat);
  700. void read_S514_int_stat (sdlahw_t* hw, u32* int_status)
  701. {
  702. pci_read_config_dword(hw->pci_dev, PCI_INT_STATUS, int_status);
  703. }
  704. /*============================================================================
  705. * Generate an interrupt to adapter's CPU.
  706. */
  707. EXPORT_SYMBOL(sdla_intr);
  708. int sdla_intr (sdlahw_t* hw)
  709. {
  710. unsigned port = hw->port;
  711. switch (hw->type) {
  712. case SDLA_S502A:
  713. if (!(_INB(port) & 0x40)) {
  714. _OUTB(port, 0x10); /* issue NMI to CPU */
  715. hw->regs[0] = 0x10;
  716. }
  717. else return -EIO;
  718. break;
  719. case SDLA_S507:
  720. if ((_INB(port) & 0x06) == 0x06) {
  721. _OUTB(port + 3, 0);
  722. }
  723. else return -EIO;
  724. break;
  725. case SDLA_S508:
  726. if (_INB(port + 1) & 0x02) {
  727. _OUTB(port, 0x08);
  728. }
  729. else return -EIO;
  730. break;
  731. case SDLA_S502E:
  732. case SDLA_S503:
  733. default:
  734. return -EINVAL;
  735. }
  736. return 0;
  737. }
  738. /*============================================================================
  739. * Execute Adapter Command.
  740. * o Set exec flag.
  741. * o Busy-wait until flag is reset.
  742. * o Return number of loops made, or 0 if command timed out.
  743. */
  744. EXPORT_SYMBOL(sdla_exec);
  745. int sdla_exec (void* opflag)
  746. {
  747. volatile unsigned char* flag = opflag;
  748. unsigned long tstop;
  749. int nloops;
  750. if(readb(flag) != 0x00) {
  751. printk(KERN_INFO
  752. "WANPIPE: opp flag set on entry to sdla_exec\n");
  753. return 0;
  754. }
  755. writeb(0x01, flag);
  756. tstop = SYSTEM_TICK + EXEC_TIMEOUT;
  757. for (nloops = 1; (readb(flag) == 0x01); ++ nloops) {
  758. unsigned delay = exec_idle;
  759. while (-- delay); /* delay */
  760. if (SYSTEM_TICK > tstop) return 0; /* time is up! */
  761. }
  762. return nloops;
  763. }
  764. /*============================================================================
  765. * Read absolute adapter memory.
  766. * Transfer data from adapter's memory to data buffer.
  767. *
  768. * Note:
  769. * Care should be taken when crossing dual-port memory window boundary.
  770. * This function is not atomic, so caller must disable interrupt if
  771. * interrupt routines are accessing adapter shared memory.
  772. */
  773. EXPORT_SYMBOL(sdla_peek);
  774. int sdla_peek (sdlahw_t* hw, unsigned long addr, void* buf, unsigned len)
  775. {
  776. if (addr + len > hw->memory) /* verify arguments */
  777. return -EINVAL;
  778. if(hw->type == SDLA_S514) { /* copy data for the S514 adapter */
  779. peek_by_4 ((unsigned long)hw->dpmbase + addr, buf, len);
  780. return 0;
  781. }
  782. else { /* copy data for the S508 adapter */
  783. unsigned long oldvec = hw->vector;
  784. unsigned winsize = hw->dpmsize;
  785. unsigned curpos, curlen; /* current offset and block size */
  786. unsigned long curvec; /* current DPM window vector */
  787. int err = 0;
  788. while (len && !err) {
  789. curpos = addr % winsize; /* current window offset */
  790. curvec = addr - curpos; /* current window vector */
  791. curlen = (len > (winsize - curpos)) ?
  792. (winsize - curpos) : len;
  793. /* Relocate window and copy block of data */
  794. err = sdla_mapmem(hw, curvec);
  795. peek_by_4 ((unsigned long)hw->dpmbase + curpos, buf,
  796. curlen);
  797. addr += curlen;
  798. buf = (char*)buf + curlen;
  799. len -= curlen;
  800. }
  801. /* Restore DPM window position */
  802. sdla_mapmem(hw, oldvec);
  803. return err;
  804. }
  805. }
  806. /*============================================================================
  807. * Read data from adapter's memory to a data buffer in 4-byte chunks.
  808. * Note that we ensure that the SDLA memory address is on a 4-byte boundary
  809. * before we begin moving the data in 4-byte chunks.
  810. */
  811. static void peek_by_4 (unsigned long src, void* buf, unsigned len)
  812. {
  813. /* byte copy data until we get to a 4-byte boundary */
  814. while (len && (src & 0x03)) {
  815. *(char *)buf ++ = readb(src ++);
  816. len --;
  817. }
  818. /* copy data in 4-byte chunks */
  819. while (len >= 4) {
  820. *(unsigned long *)buf = readl(src);
  821. buf += 4;
  822. src += 4;
  823. len -= 4;
  824. }
  825. /* byte copy any remaining data */
  826. while (len) {
  827. *(char *)buf ++ = readb(src ++);
  828. len --;
  829. }
  830. }
  831. /*============================================================================
  832. * Write Absolute Adapter Memory.
  833. * Transfer data from data buffer to adapter's memory.
  834. *
  835. * Note:
  836. * Care should be taken when crossing dual-port memory window boundary.
  837. * This function is not atomic, so caller must disable interrupt if
  838. * interrupt routines are accessing adapter shared memory.
  839. */
  840. EXPORT_SYMBOL(sdla_poke);
  841. int sdla_poke (sdlahw_t* hw, unsigned long addr, void* buf, unsigned len)
  842. {
  843. if (addr + len > hw->memory) /* verify arguments */
  844. return -EINVAL;
  845. if(hw->type == SDLA_S514) { /* copy data for the S514 adapter */
  846. poke_by_4 ((unsigned long)hw->dpmbase + addr, buf, len);
  847. return 0;
  848. }
  849. else { /* copy data for the S508 adapter */
  850. unsigned long oldvec = hw->vector;
  851. unsigned winsize = hw->dpmsize;
  852. unsigned curpos, curlen; /* current offset and block size */
  853. unsigned long curvec; /* current DPM window vector */
  854. int err = 0;
  855. while (len && !err) {
  856. curpos = addr % winsize; /* current window offset */
  857. curvec = addr - curpos; /* current window vector */
  858. curlen = (len > (winsize - curpos)) ?
  859. (winsize - curpos) : len;
  860. /* Relocate window and copy block of data */
  861. sdla_mapmem(hw, curvec);
  862. poke_by_4 ((unsigned long)hw->dpmbase + curpos, buf,
  863. curlen);
  864. addr += curlen;
  865. buf = (char*)buf + curlen;
  866. len -= curlen;
  867. }
  868. /* Restore DPM window position */
  869. sdla_mapmem(hw, oldvec);
  870. return err;
  871. }
  872. }
  873. /*============================================================================
  874. * Write from a data buffer to adapter's memory in 4-byte chunks.
  875. * Note that we ensure that the SDLA memory address is on a 4-byte boundary
  876. * before we begin moving the data in 4-byte chunks.
  877. */
  878. static void poke_by_4 (unsigned long dest, void* buf, unsigned len)
  879. {
  880. /* byte copy data until we get to a 4-byte boundary */
  881. while (len && (dest & 0x03)) {
  882. writeb (*(char *)buf ++, dest ++);
  883. len --;
  884. }
  885. /* copy data in 4-byte chunks */
  886. while (len >= 4) {
  887. writel (*(unsigned long *)buf, dest);
  888. dest += 4;
  889. buf += 4;
  890. len -= 4;
  891. }
  892. /* byte copy any remaining data */
  893. while (len) {
  894. writeb (*(char *)buf ++ , dest ++);
  895. len --;
  896. }
  897. }
  898. #ifdef DONT_COMPIPLE_THIS
  899. #endif /* DONT_COMPIPLE_THIS */
  900. /****** Hardware-Specific Functions *****************************************/
  901. /*============================================================================
  902. * Detect adapter type.
  903. * o if adapter type is specified then call detection routine for that adapter
  904. * type. Otherwise call detection routines for every adapter types until
  905. * adapter is detected.
  906. *
  907. * Notes:
  908. * 1) Detection tests are destructive! Adapter will be left in shutdown state
  909. * after the test.
  910. */
  911. static int sdla_detect (sdlahw_t* hw)
  912. {
  913. unsigned port = hw->port;
  914. int err = 0;
  915. if (!port && (hw->type != SDLA_S514))
  916. return -EFAULT;
  917. switch (hw->type) {
  918. case SDLA_S502A:
  919. if (!detect_s502a(port)) err = -ENODEV;
  920. break;
  921. case SDLA_S502E:
  922. if (!detect_s502e(port)) err = -ENODEV;
  923. break;
  924. case SDLA_S503:
  925. if (!detect_s503(port)) err = -ENODEV;
  926. break;
  927. case SDLA_S507:
  928. if (!detect_s507(port)) err = -ENODEV;
  929. break;
  930. case SDLA_S508:
  931. if (!detect_s508(port)) err = -ENODEV;
  932. break;
  933. case SDLA_S514:
  934. if (!detect_s514(hw)) err = -ENODEV;
  935. break;
  936. default:
  937. if (detect_s502a(port))
  938. hw->type = SDLA_S502A;
  939. else if (detect_s502e(port))
  940. hw->type = SDLA_S502E;
  941. else if (detect_s503(port))
  942. hw->type = SDLA_S503;
  943. else if (detect_s507(port))
  944. hw->type = SDLA_S507;
  945. else if (detect_s508(port))
  946. hw->type = SDLA_S508;
  947. else err = -ENODEV;
  948. }
  949. return err;
  950. }
  951. /*============================================================================
  952. * Autoselect memory region.
  953. * o try all available DMP address options from the top down until success.
  954. */
  955. static int sdla_autodpm (sdlahw_t* hw)
  956. {
  957. int i, err = -EINVAL;
  958. unsigned* opt;
  959. switch (hw->type) {
  960. case SDLA_S502A:
  961. opt = s502a_dpmbase_options;
  962. break;
  963. case SDLA_S502E:
  964. case SDLA_S503:
  965. case SDLA_S508:
  966. opt = s508_dpmbase_options;
  967. break;
  968. case SDLA_S507:
  969. opt = s507_dpmbase_options;
  970. break;
  971. default:
  972. return -EINVAL;
  973. }
  974. /* Start testing from 8th position, address
  975. * 0xC8000 from the 508 address table.
  976. * We don't want to test A**** addresses, since
  977. * they are usually used for Video */
  978. for (i = 8; i <= opt[0] && err; i++) {
  979. hw->dpmbase = phys_to_virt(opt[i]);
  980. err = sdla_setdpm(hw);
  981. }
  982. return err;
  983. }
  984. /*============================================================================
  985. * Set up adapter dual-port memory window.
  986. * o shut down adapter
  987. * o make sure that no physical memory exists in this region, i.e entire
  988. * region reads 0xFF and is not writable when adapter is shut down.
  989. * o initialize adapter hardware
  990. * o make sure that region is usable with SDLA card, i.e. we can write to it
  991. * when adapter is configured.
  992. */
  993. static int sdla_setdpm (sdlahw_t* hw)
  994. {
  995. int err;
  996. /* Shut down card and verify memory region */
  997. sdla_down(hw);
  998. if (check_memregion(hw->dpmbase, hw->dpmsize))
  999. return -EINVAL;
  1000. /* Initialize adapter and test on-board memory segment by segment.
  1001. * If memory size appears to be less than shared memory window size,
  1002. * assume that memory region is unusable.
  1003. */
  1004. err = sdla_init(hw);
  1005. if (err) return err;
  1006. if (sdla_memtest(hw) < hw->dpmsize) { /* less than window size */
  1007. sdla_down(hw);
  1008. return -EIO;
  1009. }
  1010. sdla_mapmem(hw, 0L); /* set window vector at bottom */
  1011. return 0;
  1012. }
  1013. /*============================================================================
  1014. * Load adapter from the memory image of the SDLA firmware module.
  1015. * o verify firmware integrity and compatibility
  1016. * o start adapter up
  1017. */
  1018. static int sdla_load (sdlahw_t* hw, sfm_t* sfm, unsigned len)
  1019. {
  1020. int i;
  1021. /* Verify firmware signature */
  1022. if (strcmp(sfm->signature, SFM_SIGNATURE)) {
  1023. printk(KERN_INFO "%s: not SDLA firmware!\n",
  1024. modname);
  1025. return -EINVAL;
  1026. }
  1027. /* Verify firmware module format version */
  1028. if (sfm->version != SFM_VERSION) {
  1029. printk(KERN_INFO
  1030. "%s: firmware format %u rejected! Expecting %u.\n",
  1031. modname, sfm->version, SFM_VERSION);
  1032. return -EINVAL;
  1033. }
  1034. /* Verify firmware module length and checksum */
  1035. if ((len - offsetof(sfm_t, image) != sfm->info.codesize) ||
  1036. (checksum((void*)&sfm->info,
  1037. sizeof(sfm_info_t) + sfm->info.codesize) != sfm->checksum)) {
  1038. printk(KERN_INFO "%s: firmware corrupted!\n", modname);
  1039. return -EINVAL;
  1040. }
  1041. /* Announce */
  1042. printk(KERN_INFO "%s: loading %s (ID=%u)...\n", modname,
  1043. (sfm->descr[0] != '\0') ? sfm->descr : "unknown firmware",
  1044. sfm->info.codeid);
  1045. if(hw->type == SDLA_S514)
  1046. printk(KERN_INFO "%s: loading S514 adapter, CPU %c\n",
  1047. modname, hw->S514_cpu_no[0]);
  1048. /* Scan through the list of compatible adapters and make sure our
  1049. * adapter type is listed.
  1050. */
  1051. for (i = 0;
  1052. (i < SFM_MAX_SDLA) && (sfm->info.adapter[i] != hw->type);
  1053. ++i);
  1054. if (i == SFM_MAX_SDLA) {
  1055. printk(KERN_INFO "%s: firmware is not compatible with S%u!\n",
  1056. modname, hw->type);
  1057. return -EINVAL;
  1058. }
  1059. /* Make sure there is enough on-board memory */
  1060. if (hw->memory < sfm->info.memsize) {
  1061. printk(KERN_INFO
  1062. "%s: firmware needs %lu bytes of on-board memory!\n",
  1063. modname, sfm->info.memsize);
  1064. return -EINVAL;
  1065. }
  1066. /* Move code onto adapter */
  1067. if (sdla_poke(hw, sfm->info.codeoffs, sfm->image, sfm->info.codesize)) {
  1068. printk(KERN_INFO "%s: failed to load code segment!\n",
  1069. modname);
  1070. return -EIO;
  1071. }
  1072. /* Prepare boot-time configuration data and kick-off CPU */
  1073. sdla_bootcfg(hw, &sfm->info);
  1074. if (sdla_start(hw, sfm->info.startoffs)) {
  1075. printk(KERN_INFO "%s: Damn... Adapter won't start!\n",
  1076. modname);
  1077. return -EIO;
  1078. }
  1079. /* position DPM window over the mailbox and enable interrupts */
  1080. if (sdla_mapmem(hw, sfm->info.winoffs) || sdla_inten(hw)) {
  1081. printk(KERN_INFO "%s: adapter hardware failure!\n",
  1082. modname);
  1083. return -EIO;
  1084. }
  1085. hw->fwid = sfm->info.codeid; /* set firmware ID */
  1086. return 0;
  1087. }
  1088. /*============================================================================
  1089. * Initialize SDLA hardware: setup memory window, IRQ, etc.
  1090. */
  1091. static int sdla_init (sdlahw_t* hw)
  1092. {
  1093. int i;
  1094. for (i = 0; i < SDLA_MAXIORANGE; ++i)
  1095. hw->regs[i] = 0;
  1096. switch (hw->type) {
  1097. case SDLA_S502A: return init_s502a(hw);
  1098. case SDLA_S502E: return init_s502e(hw);
  1099. case SDLA_S503: return init_s503(hw);
  1100. case SDLA_S507: return init_s507(hw);
  1101. case SDLA_S508: return init_s508(hw);
  1102. }
  1103. return -EINVAL;
  1104. }
  1105. /*============================================================================
  1106. * Test adapter on-board memory.
  1107. * o slide DPM window from the bottom up and test adapter memory segment by
  1108. * segment.
  1109. * Return adapter memory size.
  1110. */
  1111. static unsigned long sdla_memtest (sdlahw_t* hw)
  1112. {
  1113. unsigned long memsize;
  1114. unsigned winsize;
  1115. for (memsize = 0, winsize = hw->dpmsize;
  1116. !sdla_mapmem(hw, memsize) &&
  1117. (test_memregion(hw->dpmbase, winsize) == winsize)
  1118. ;
  1119. memsize += winsize)
  1120. ;
  1121. hw->memory = memsize;
  1122. return memsize;
  1123. }
  1124. /*============================================================================
  1125. * Prepare boot-time firmware configuration data.
  1126. * o position DPM window
  1127. * o initialize configuration data area
  1128. */
  1129. static int sdla_bootcfg (sdlahw_t* hw, sfm_info_t* sfminfo)
  1130. {
  1131. unsigned char* data;
  1132. if (!sfminfo->datasize) return 0; /* nothing to do */
  1133. if (sdla_mapmem(hw, sfminfo->dataoffs) != 0)
  1134. return -EIO;
  1135. if(hw->type == SDLA_S514)
  1136. data = (void*)(hw->dpmbase + sfminfo->dataoffs);
  1137. else
  1138. data = (void*)((u8 *)hw->dpmbase +
  1139. (sfminfo->dataoffs - hw->vector));
  1140. memset_io (data, 0, sfminfo->datasize);
  1141. writeb (make_config_byte(hw), &data[0x00]);
  1142. switch (sfminfo->codeid) {
  1143. case SFID_X25_502:
  1144. case SFID_X25_508:
  1145. writeb (3, &data[0x01]); /* T1 timer */
  1146. writeb (10, &data[0x03]); /* N2 */
  1147. writeb (7, &data[0x06]); /* HDLC window size */
  1148. writeb (1, &data[0x0B]); /* DTE */
  1149. writeb (2, &data[0x0C]); /* X.25 packet window size */
  1150. writew (128, &data[0x0D]); /* default X.25 data size */
  1151. writew (128, &data[0x0F]); /* maximum X.25 data size */
  1152. break;
  1153. }
  1154. return 0;
  1155. }
  1156. /*============================================================================
  1157. * Prepare configuration byte identifying adapter type and CPU clock rate.
  1158. */
  1159. static unsigned char make_config_byte (sdlahw_t* hw)
  1160. {
  1161. unsigned char byte = 0;
  1162. switch (hw->pclk) {
  1163. case 5000: byte = 0x01; break;
  1164. case 7200: byte = 0x02; break;
  1165. case 8000: byte = 0x03; break;
  1166. case 10000: byte = 0x04; break;
  1167. case 16000: byte = 0x05; break;
  1168. }
  1169. switch (hw->type) {
  1170. case SDLA_S502E: byte |= 0x80; break;
  1171. case SDLA_S503: byte |= 0x40; break;
  1172. }
  1173. return byte;
  1174. }
  1175. /*============================================================================
  1176. * Start adapter's CPU.
  1177. * o calculate a pointer to adapter's cold boot entry point
  1178. * o position DPM window
  1179. * o place boot instruction (jp addr) at cold boot entry point
  1180. * o start CPU
  1181. */
  1182. static int sdla_start (sdlahw_t* hw, unsigned addr)
  1183. {
  1184. unsigned port = hw->port;
  1185. unsigned char *bootp;
  1186. int err, tmp, i;
  1187. if (!port && (hw->type != SDLA_S514)) return -EFAULT;
  1188. switch (hw->type) {
  1189. case SDLA_S502A:
  1190. bootp = hw->dpmbase;
  1191. bootp += 0x66;
  1192. break;
  1193. case SDLA_S502E:
  1194. case SDLA_S503:
  1195. case SDLA_S507:
  1196. case SDLA_S508:
  1197. case SDLA_S514:
  1198. bootp = hw->dpmbase;
  1199. break;
  1200. default:
  1201. return -EINVAL;
  1202. }
  1203. err = sdla_mapmem(hw, 0);
  1204. if (err) return err;
  1205. writeb (0xC3, bootp); /* Z80: 'jp' opcode */
  1206. bootp ++;
  1207. writew (addr, bootp);
  1208. switch (hw->type) {
  1209. case SDLA_S502A:
  1210. _OUTB(port, 0x10); /* issue NMI to CPU */
  1211. hw->regs[0] = 0x10;
  1212. break;
  1213. case SDLA_S502E:
  1214. _OUTB(port + 3, 0x01); /* start CPU */
  1215. hw->regs[3] = 0x01;
  1216. for (i = 0; i < SDLA_IODELAY; ++i);
  1217. if (_INB(port) & 0x01) { /* verify */
  1218. /*
  1219. * Enabling CPU changes functionality of the
  1220. * control register, so we have to reset its
  1221. * mirror.
  1222. */
  1223. _OUTB(port, 0); /* disable interrupts */
  1224. hw->regs[0] = 0;
  1225. }
  1226. else return -EIO;
  1227. break;
  1228. case SDLA_S503:
  1229. tmp = hw->regs[0] | 0x09; /* set bits 0 and 3 */
  1230. _OUTB(port, tmp);
  1231. hw->regs[0] = tmp; /* update mirror */
  1232. for (i = 0; i < SDLA_IODELAY; ++i);
  1233. if (!(_INB(port) & 0x01)) /* verify */
  1234. return -EIO;
  1235. break;
  1236. case SDLA_S507:
  1237. tmp = hw->regs[0] | 0x02;
  1238. _OUTB(port, tmp);
  1239. hw->regs[0] = tmp; /* update mirror */
  1240. for (i = 0; i < SDLA_IODELAY; ++i);
  1241. if (!(_INB(port) & 0x04)) /* verify */
  1242. return -EIO;
  1243. break;
  1244. case SDLA_S508:
  1245. tmp = hw->regs[0] | 0x02;
  1246. _OUTB(port, tmp);
  1247. hw->regs[0] = tmp; /* update mirror */
  1248. for (i = 0; i < SDLA_IODELAY; ++i);
  1249. if (!(_INB(port + 1) & 0x02)) /* verify */
  1250. return -EIO;
  1251. break;
  1252. case SDLA_S514:
  1253. writeb (S514_CPU_START, hw->vector);
  1254. break;
  1255. default:
  1256. return -EINVAL;
  1257. }
  1258. return 0;
  1259. }
  1260. /*============================================================================
  1261. * Initialize S502A adapter.
  1262. */
  1263. static int init_s502a (sdlahw_t* hw)
  1264. {
  1265. unsigned port = hw->port;
  1266. int tmp, i;
  1267. if (!detect_s502a(port))
  1268. return -ENODEV;
  1269. hw->regs[0] = 0x08;
  1270. hw->regs[1] = 0xFF;
  1271. /* Verify configuration options */
  1272. i = get_option_index(s502a_dpmbase_options, virt_to_phys(hw->dpmbase));
  1273. if (i == 0)
  1274. return -EINVAL;
  1275. tmp = s502a_hmcr[i - 1];
  1276. switch (hw->dpmsize) {
  1277. case 0x2000:
  1278. tmp |= 0x01;
  1279. break;
  1280. case 0x10000L:
  1281. break;
  1282. default:
  1283. return -EINVAL;
  1284. }
  1285. /* Setup dual-port memory window (this also enables memory access) */
  1286. _OUTB(port + 1, tmp);
  1287. hw->regs[1] = tmp;
  1288. hw->regs[0] = 0x08;
  1289. return 0;
  1290. }
  1291. /*============================================================================
  1292. * Initialize S502E adapter.
  1293. */
  1294. static int init_s502e (sdlahw_t* hw)
  1295. {
  1296. unsigned port = hw->port;
  1297. int tmp, i;
  1298. if (!detect_s502e(port))
  1299. return -ENODEV;
  1300. /* Verify configuration options */
  1301. i = get_option_index(s508_dpmbase_options, virt_to_phys(hw->dpmbase));
  1302. if (i == 0)
  1303. return -EINVAL;
  1304. tmp = s502e_hmcr[i - 1];
  1305. switch (hw->dpmsize) {
  1306. case 0x2000:
  1307. tmp |= 0x01;
  1308. break;
  1309. case 0x10000L:
  1310. break;
  1311. default:
  1312. return -EINVAL;
  1313. }
  1314. /* Setup dual-port memory window */
  1315. _OUTB(port + 1, tmp);
  1316. hw->regs[1] = tmp;
  1317. /* Enable memory access */
  1318. _OUTB(port, 0x02);
  1319. hw->regs[0] = 0x02;
  1320. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1321. return (_INB(port) & 0x02) ? 0 : -EIO;
  1322. }
  1323. /*============================================================================
  1324. * Initialize S503 adapter.
  1325. * ---------------------------------------------------------------------------
  1326. */
  1327. static int init_s503 (sdlahw_t* hw)
  1328. {
  1329. unsigned port = hw->port;
  1330. int tmp, i;
  1331. if (!detect_s503(port))
  1332. return -ENODEV;
  1333. /* Verify configuration options */
  1334. i = get_option_index(s508_dpmbase_options, virt_to_phys(hw->dpmbase));
  1335. if (i == 0)
  1336. return -EINVAL;
  1337. tmp = s502e_hmcr[i - 1];
  1338. switch (hw->dpmsize) {
  1339. case 0x2000:
  1340. tmp |= 0x01;
  1341. break;
  1342. case 0x10000L:
  1343. break;
  1344. default:
  1345. return -EINVAL;
  1346. }
  1347. /* Setup dual-port memory window */
  1348. _OUTB(port + 1, tmp);
  1349. hw->regs[1] = tmp;
  1350. /* Enable memory access */
  1351. _OUTB(port, 0x02);
  1352. hw->regs[0] = 0x02; /* update mirror */
  1353. return 0;
  1354. }
  1355. /*============================================================================
  1356. * Initialize S507 adapter.
  1357. */
  1358. static int init_s507 (sdlahw_t* hw)
  1359. {
  1360. unsigned port = hw->port;
  1361. int tmp, i;
  1362. if (!detect_s507(port))
  1363. return -ENODEV;
  1364. /* Verify configuration options */
  1365. i = get_option_index(s507_dpmbase_options, virt_to_phys(hw->dpmbase));
  1366. if (i == 0)
  1367. return -EINVAL;
  1368. tmp = s507_hmcr[i - 1];
  1369. switch (hw->dpmsize) {
  1370. case 0x2000:
  1371. tmp |= 0x01;
  1372. break;
  1373. case 0x10000L:
  1374. break;
  1375. default:
  1376. return -EINVAL;
  1377. }
  1378. /* Enable adapter's logic */
  1379. _OUTB(port, 0x01);
  1380. hw->regs[0] = 0x01;
  1381. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1382. if (!(_INB(port) & 0x20))
  1383. return -EIO;
  1384. /* Setup dual-port memory window */
  1385. _OUTB(port + 1, tmp);
  1386. hw->regs[1] = tmp;
  1387. /* Enable memory access */
  1388. tmp = hw->regs[0] | 0x04;
  1389. if (hw->irq) {
  1390. i = get_option_index(s508_irq_options, hw->irq);
  1391. if (i) tmp |= s507_irqmask[i - 1];
  1392. }
  1393. _OUTB(port, tmp);
  1394. hw->regs[0] = tmp; /* update mirror */
  1395. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1396. return (_INB(port) & 0x08) ? 0 : -EIO;
  1397. }
  1398. /*============================================================================
  1399. * Initialize S508 adapter.
  1400. */
  1401. static int init_s508 (sdlahw_t* hw)
  1402. {
  1403. unsigned port = hw->port;
  1404. int tmp, i;
  1405. if (!detect_s508(port))
  1406. return -ENODEV;
  1407. /* Verify configuration options */
  1408. i = get_option_index(s508_dpmbase_options, virt_to_phys(hw->dpmbase));
  1409. if (i == 0)
  1410. return -EINVAL;
  1411. /* Setup memory configuration */
  1412. tmp = s508_hmcr[i - 1];
  1413. _OUTB(port + 1, tmp);
  1414. hw->regs[1] = tmp;
  1415. /* Enable memory access */
  1416. _OUTB(port, 0x04);
  1417. hw->regs[0] = 0x04; /* update mirror */
  1418. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1419. return (_INB(port + 1) & 0x04) ? 0 : -EIO;
  1420. }
  1421. /*============================================================================
  1422. * Detect S502A adapter.
  1423. * Following tests are used to detect S502A adapter:
  1424. * 1. All registers other than status (BASE) should read 0xFF
  1425. * 2. After writing 00001000b to control register, status register should
  1426. * read 01000000b.
  1427. * 3. After writing 0 to control register, status register should still
  1428. * read 01000000b.
  1429. * 4. After writing 00000100b to control register, status register should
  1430. * read 01000100b.
  1431. * Return 1 if detected o.k. or 0 if failed.
  1432. * Note: This test is destructive! Adapter will be left in shutdown
  1433. * state after the test.
  1434. */
  1435. static int detect_s502a (int port)
  1436. {
  1437. int i, j;
  1438. if (!get_option_index(s502_port_options, port))
  1439. return 0;
  1440. for (j = 1; j < SDLA_MAXIORANGE; ++j) {
  1441. if (_INB(port + j) != 0xFF)
  1442. return 0;
  1443. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1444. }
  1445. _OUTB(port, 0x08); /* halt CPU */
  1446. _OUTB(port, 0x08);
  1447. _OUTB(port, 0x08);
  1448. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1449. if (_INB(port) != 0x40)
  1450. return 0;
  1451. _OUTB(port, 0x00);
  1452. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1453. if (_INB(port) != 0x40)
  1454. return 0;
  1455. _OUTB(port, 0x04);
  1456. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1457. if (_INB(port) != 0x44)
  1458. return 0;
  1459. /* Reset adapter */
  1460. _OUTB(port, 0x08);
  1461. _OUTB(port, 0x08);
  1462. _OUTB(port, 0x08);
  1463. _OUTB(port + 1, 0xFF);
  1464. return 1;
  1465. }
  1466. /*============================================================================
  1467. * Detect S502E adapter.
  1468. * Following tests are used to verify adapter presence:
  1469. * 1. All registers other than status (BASE) should read 0xFF.
  1470. * 2. After writing 0 to CPU control register (BASE+3), status register
  1471. * (BASE) should read 11111000b.
  1472. * 3. After writing 00000100b to port BASE (set bit 2), status register
  1473. * (BASE) should read 11111100b.
  1474. * Return 1 if detected o.k. or 0 if failed.
  1475. * Note: This test is destructive! Adapter will be left in shutdown
  1476. * state after the test.
  1477. */
  1478. static int detect_s502e (int port)
  1479. {
  1480. int i, j;
  1481. if (!get_option_index(s502_port_options, port))
  1482. return 0;
  1483. for (j = 1; j < SDLA_MAXIORANGE; ++j) {
  1484. if (_INB(port + j) != 0xFF)
  1485. return 0;
  1486. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1487. }
  1488. _OUTB(port + 3, 0); /* CPU control reg. */
  1489. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1490. if (_INB(port) != 0xF8) /* read status */
  1491. return 0;
  1492. _OUTB(port, 0x04); /* set bit 2 */
  1493. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1494. if (_INB(port) != 0xFC) /* verify */
  1495. return 0;
  1496. /* Reset adapter */
  1497. _OUTB(port, 0);
  1498. return 1;
  1499. }
  1500. /*============================================================================
  1501. * Detect s503 adapter.
  1502. * Following tests are used to verify adapter presence:
  1503. * 1. All registers other than status (BASE) should read 0xFF.
  1504. * 2. After writing 0 to control register (BASE), status register (BASE)
  1505. * should read 11110000b.
  1506. * 3. After writing 00000100b (set bit 2) to control register (BASE),
  1507. * status register should read 11110010b.
  1508. * Return 1 if detected o.k. or 0 if failed.
  1509. * Note: This test is destructive! Adapter will be left in shutdown
  1510. * state after the test.
  1511. */
  1512. static int detect_s503 (int port)
  1513. {
  1514. int i, j;
  1515. if (!get_option_index(s503_port_options, port))
  1516. return 0;
  1517. for (j = 1; j < SDLA_MAXIORANGE; ++j) {
  1518. if (_INB(port + j) != 0xFF)
  1519. return 0;
  1520. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1521. }
  1522. _OUTB(port, 0); /* reset control reg.*/
  1523. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1524. if (_INB(port) != 0xF0) /* read status */
  1525. return 0;
  1526. _OUTB(port, 0x04); /* set bit 2 */
  1527. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1528. if (_INB(port) != 0xF2) /* verify */
  1529. return 0;
  1530. /* Reset adapter */
  1531. _OUTB(port, 0);
  1532. return 1;
  1533. }
  1534. /*============================================================================
  1535. * Detect s507 adapter.
  1536. * Following tests are used to detect s507 adapter:
  1537. * 1. All ports should read the same value.
  1538. * 2. After writing 0x00 to control register, status register should read
  1539. * ?011000?b.
  1540. * 3. After writing 0x01 to control register, status register should read
  1541. * ?011001?b.
  1542. * Return 1 if detected o.k. or 0 if failed.
  1543. * Note: This test is destructive! Adapter will be left in shutdown
  1544. * state after the test.
  1545. */
  1546. static int detect_s507 (int port)
  1547. {
  1548. int tmp, i, j;
  1549. if (!get_option_index(s508_port_options, port))
  1550. return 0;
  1551. tmp = _INB(port);
  1552. for (j = 1; j < S507_IORANGE; ++j) {
  1553. if (_INB(port + j) != tmp)
  1554. return 0;
  1555. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1556. }
  1557. _OUTB(port, 0x00);
  1558. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1559. if ((_INB(port) & 0x7E) != 0x30)
  1560. return 0;
  1561. _OUTB(port, 0x01);
  1562. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1563. if ((_INB(port) & 0x7E) != 0x32)
  1564. return 0;
  1565. /* Reset adapter */
  1566. _OUTB(port, 0x00);
  1567. return 1;
  1568. }
  1569. /*============================================================================
  1570. * Detect s508 adapter.
  1571. * Following tests are used to detect s508 adapter:
  1572. * 1. After writing 0x00 to control register, status register should read
  1573. * ??000000b.
  1574. * 2. After writing 0x10 to control register, status register should read
  1575. * ??010000b
  1576. * Return 1 if detected o.k. or 0 if failed.
  1577. * Note: This test is destructive! Adapter will be left in shutdown
  1578. * state after the test.
  1579. */
  1580. static int detect_s508 (int port)
  1581. {
  1582. int i;
  1583. if (!get_option_index(s508_port_options, port))
  1584. return 0;
  1585. _OUTB(port, 0x00);
  1586. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1587. if ((_INB(port + 1) & 0x3F) != 0x00)
  1588. return 0;
  1589. _OUTB(port, 0x10);
  1590. for (i = 0; i < SDLA_IODELAY; ++i); /* delay */
  1591. if ((_INB(port + 1) & 0x3F) != 0x10)
  1592. return 0;
  1593. /* Reset adapter */
  1594. _OUTB(port, 0x00);
  1595. return 1;
  1596. }
  1597. /*============================================================================
  1598. * Detect s514 PCI adapter.
  1599. * Return 1 if detected o.k. or 0 if failed.
  1600. * Note: This test is destructive! Adapter will be left in shutdown
  1601. * state after the test.
  1602. */
  1603. static int detect_s514 (sdlahw_t* hw)
  1604. {
  1605. unsigned char CPU_no, slot_no, auto_slot_cfg;
  1606. int number_S514_cards = 0;
  1607. u32 S514_mem_base_addr = 0;
  1608. u32 ut_u32;
  1609. struct pci_dev *pci_dev;
  1610. #ifndef CONFIG_PCI
  1611. printk(KERN_INFO "%s: Linux not compiled for PCI usage!\n", modname);
  1612. return 0;
  1613. #endif
  1614. /*
  1615. The 'setup()' procedure in 'sdlamain.c' passes the CPU number and the
  1616. slot number defined in 'router.conf' via the 'port' definition.
  1617. */
  1618. CPU_no = hw->S514_cpu_no[0];
  1619. slot_no = hw->S514_slot_no;
  1620. auto_slot_cfg = hw->auto_pci_cfg;
  1621. if (auto_slot_cfg){
  1622. printk(KERN_INFO "%s: srch... S514 card, CPU %c, Slot=Auto\n",
  1623. modname, CPU_no);
  1624. }else{
  1625. printk(KERN_INFO "%s: srch... S514 card, CPU %c, Slot #%d\n",
  1626. modname, CPU_no, slot_no);
  1627. }
  1628. /* check to see that CPU A or B has been selected in 'router.conf' */
  1629. switch(CPU_no) {
  1630. case S514_CPU_A:
  1631. case S514_CPU_B:
  1632. break;
  1633. default:
  1634. printk(KERN_INFO "%s: S514 CPU definition invalid.\n",
  1635. modname);
  1636. printk(KERN_INFO "Must be 'A' or 'B'\n");
  1637. return 0;
  1638. }
  1639. number_S514_cards = find_s514_adapter(hw, 0);
  1640. if(!number_S514_cards)
  1641. return 0;
  1642. /* we are using a single S514 adapter with a slot of 0 so re-read the */
  1643. /* location of this adapter */
  1644. if((number_S514_cards == 1) && auto_slot_cfg) {
  1645. number_S514_cards = find_s514_adapter(hw, 1);
  1646. if(!number_S514_cards) {
  1647. printk(KERN_INFO "%s: Error finding PCI card\n",
  1648. modname);
  1649. return 0;
  1650. }
  1651. }
  1652. pci_dev = hw->pci_dev;
  1653. /* read the physical memory base address */
  1654. S514_mem_base_addr = (CPU_no == S514_CPU_A) ?
  1655. (pci_dev->resource[1].start) :
  1656. (pci_dev->resource[2].start);
  1657. printk(KERN_INFO "%s: S514 PCI memory at 0x%X\n",
  1658. modname, S514_mem_base_addr);
  1659. if(!S514_mem_base_addr) {
  1660. if(CPU_no == S514_CPU_B)
  1661. printk(KERN_INFO "%s: CPU #B not present on the card\n", modname);
  1662. else
  1663. printk(KERN_INFO "%s: No PCI memory allocated to card\n", modname);
  1664. return 0;
  1665. }
  1666. /* enable the PCI memory */
  1667. pci_read_config_dword(pci_dev,
  1668. (CPU_no == S514_CPU_A) ? PCI_MAP0_DWORD : PCI_MAP1_DWORD,
  1669. &ut_u32);
  1670. pci_write_config_dword(pci_dev,
  1671. (CPU_no == S514_CPU_A) ? PCI_MAP0_DWORD : PCI_MAP1_DWORD,
  1672. (ut_u32 | PCI_MEMORY_ENABLE));
  1673. /* check the IRQ allocated and enable IRQ usage */
  1674. if(!(hw->irq = pci_dev->irq)) {
  1675. printk(KERN_INFO "%s: IRQ not allocated to S514 adapter\n",
  1676. modname);
  1677. return 0;
  1678. }
  1679. /* BUG FIX : Mar 6 2000
  1680. * On a initial loading of the card, we must check
  1681. * and clear PCI interrupt bits, due to a reset
  1682. * problem on some other boards. i.e. An interrupt
  1683. * might be pending, even after system bootup,
  1684. * in which case, when starting wanrouter the machine
  1685. * would crash.
  1686. */
  1687. if (init_pci_slot(hw))
  1688. return 0;
  1689. pci_read_config_dword(pci_dev, PCI_INT_CONFIG, &ut_u32);
  1690. ut_u32 |= (CPU_no == S514_CPU_A) ?
  1691. PCI_ENABLE_IRQ_CPU_A : PCI_ENABLE_IRQ_CPU_B;
  1692. pci_write_config_dword(pci_dev, PCI_INT_CONFIG, ut_u32);
  1693. printk(KERN_INFO "%s: IRQ %d allocated to the S514 card\n",
  1694. modname, hw->irq);
  1695. /* map the physical PCI memory to virtual memory */
  1696. (void *)hw->dpmbase = ioremap((unsigned long)S514_mem_base_addr,
  1697. (unsigned long)MAX_SIZEOF_S514_MEMORY);
  1698. /* map the physical control register memory to virtual memory */
  1699. hw->vector = (unsigned long)ioremap(
  1700. (unsigned long)(S514_mem_base_addr + S514_CTRL_REG_BYTE),
  1701. (unsigned long)16);
  1702. if(!hw->dpmbase || !hw->vector) {
  1703. printk(KERN_INFO "%s: PCI virtual memory allocation failed\n",
  1704. modname);
  1705. return 0;
  1706. }
  1707. /* halt the adapter */
  1708. writeb (S514_CPU_HALT, hw->vector);
  1709. return 1;
  1710. }
  1711. /*============================================================================
  1712. * Find the S514 PCI adapter in the PCI bus.
  1713. * Return the number of S514 adapters found (0 if no adapter found).
  1714. */
  1715. static int find_s514_adapter(sdlahw_t* hw, char find_first_S514_card)
  1716. {
  1717. unsigned char slot_no;
  1718. int number_S514_cards = 0;
  1719. char S514_found_in_slot = 0;
  1720. u16 PCI_subsys_vendor;
  1721. struct pci_dev *pci_dev = NULL;
  1722. slot_no = hw->S514_slot_no;
  1723. while ((pci_dev = pci_find_device(V3_VENDOR_ID, V3_DEVICE_ID, pci_dev))
  1724. != NULL) {
  1725. pci_read_config_word(pci_dev, PCI_SUBSYS_VENDOR_WORD,
  1726. &PCI_subsys_vendor);
  1727. if(PCI_subsys_vendor != SANGOMA_SUBSYS_VENDOR)
  1728. continue;
  1729. hw->pci_dev = pci_dev;
  1730. if(find_first_S514_card)
  1731. return(1);
  1732. number_S514_cards ++;
  1733. printk(KERN_INFO
  1734. "%s: S514 card found, slot #%d (devfn 0x%X)\n",
  1735. modname, ((pci_dev->devfn >> 3) & PCI_DEV_SLOT_MASK),
  1736. pci_dev->devfn);
  1737. if (hw->auto_pci_cfg){
  1738. hw->S514_slot_no = ((pci_dev->devfn >> 3) & PCI_DEV_SLOT_MASK);
  1739. slot_no = hw->S514_slot_no;
  1740. }else if (((pci_dev->devfn >> 3) & PCI_DEV_SLOT_MASK) == slot_no){
  1741. S514_found_in_slot = 1;
  1742. break;
  1743. }
  1744. }
  1745. /* if no S514 adapter has been found, then exit */
  1746. if (!number_S514_cards) {
  1747. printk(KERN_INFO "%s: Error, no S514 adapters found\n", modname);
  1748. return 0;
  1749. }
  1750. /* if more than one S514 card has been found, then the user must have */ /* defined a slot number so that the correct adapter is used */
  1751. else if ((number_S514_cards > 1) && hw->auto_pci_cfg) {
  1752. printk(KERN_INFO "%s: Error, PCI Slot autodetect Failed! \n"
  1753. "%s: More than one S514 adapter found.\n"
  1754. "%s: Disable the Autodetect feature and supply\n"
  1755. "%s: the PCISLOT numbers for each card.\n",
  1756. modname,modname,modname,modname);
  1757. return 0;
  1758. }
  1759. /* if the user has specified a slot number and the S514 adapter has */
  1760. /* not been found in that slot, then exit */
  1761. else if (!hw->auto_pci_cfg && !S514_found_in_slot) {
  1762. printk(KERN_INFO
  1763. "%s: Error, S514 card not found in specified slot #%d\n",
  1764. modname, slot_no);
  1765. return 0;
  1766. }
  1767. return (number_S514_cards);
  1768. }
  1769. /******* Miscellaneous ******************************************************/
  1770. /*============================================================================
  1771. * Calibrate SDLA memory access delay.
  1772. * Count number of idle loops made within 1 second and then calculate the
  1773. * number of loops that should be made to achive desired delay.
  1774. */
  1775. static int calibrate_delay (int mks)
  1776. {
  1777. unsigned int delay;
  1778. unsigned long stop;
  1779. for (delay = 0, stop = SYSTEM_TICK + HZ; SYSTEM_TICK < stop; ++delay);
  1780. return (delay/(1000000L/mks) + 1);
  1781. }
  1782. /*============================================================================
  1783. * Get option's index into the options list.
  1784. * Return option's index (1 .. N) or zero if option is invalid.
  1785. */
  1786. static int get_option_index (unsigned* optlist, unsigned optval)
  1787. {
  1788. int i;
  1789. for (i = 1; i <= optlist[0]; ++i)
  1790. if ( optlist[i] == optval)
  1791. return i;
  1792. return 0;
  1793. }
  1794. /*============================================================================
  1795. * Check memory region to see if it's available.
  1796. * Return: 0 ok.
  1797. */
  1798. static unsigned check_memregion (void* ptr, unsigned len)
  1799. {
  1800. volatile unsigned char* p = ptr;
  1801. for (; len && (readb (p) == 0xFF); --len, ++p) {
  1802. writeb (0, p); /* attempt to write 0 */
  1803. if (readb(p) != 0xFF) { /* still has to read 0xFF */
  1804. writeb (0xFF, p);/* restore original value */
  1805. break; /* not good */
  1806. }
  1807. }
  1808. return len;
  1809. }
  1810. /*============================================================================
  1811. * Test memory region.
  1812. * Return: size of the region that passed the test.
  1813. * Note: Region size must be multiple of 2 !
  1814. */
  1815. static unsigned test_memregion (void* ptr, unsigned len)
  1816. {
  1817. volatile unsigned short* w_ptr;
  1818. unsigned len_w = len >> 1; /* region len in words */
  1819. unsigned i;
  1820. for (i = 0, w_ptr = ptr; i < len_w; ++i, ++w_ptr)
  1821. writew (0xAA55, w_ptr);
  1822. for (i = 0, w_ptr = ptr; i < len_w; ++i, ++w_ptr)
  1823. if (readw (w_ptr) != 0xAA55) {
  1824. len_w = i;
  1825. break;
  1826. }
  1827. for (i = 0, w_ptr = ptr; i < len_w; ++i, ++w_ptr)
  1828. writew (0x55AA, w_ptr);
  1829. for (i = 0, w_ptr = ptr; i < len_w; ++i, ++w_ptr)
  1830. if (readw(w_ptr) != 0x55AA) {
  1831. len_w = i;
  1832. break;
  1833. }
  1834. for (i = 0, w_ptr = ptr; i < len_w; ++i, ++w_ptr)
  1835. writew (0, w_ptr);
  1836. return len_w << 1;
  1837. }
  1838. /*============================================================================
  1839. * Calculate 16-bit CRC using CCITT polynomial.
  1840. */
  1841. static unsigned short checksum (unsigned char* buf, unsigned len)
  1842. {
  1843. unsigned short crc = 0;
  1844. unsigned mask, flag;
  1845. for (; len; --len, ++buf) {
  1846. for (mask = 0x80; mask; mask >>= 1) {
  1847. flag = (crc & 0x8000);
  1848. crc <<= 1;
  1849. crc |= ((*buf & mask) ? 1 : 0);
  1850. if (flag) crc ^= 0x1021;
  1851. }
  1852. }
  1853. return crc;
  1854. }
  1855. static int init_pci_slot(sdlahw_t *hw)
  1856. {
  1857. u32 int_status;
  1858. int volatile found=0;
  1859. int i=0;
  1860. /* Check if this is a very first load for a specific
  1861. * pci card. If it is, clear the interrput bits, and
  1862. * set the flag indicating that this card was initialized.
  1863. */
  1864. for (i=0; (i<MAX_S514_CARDS) && !found; i++){
  1865. if (pci_slot_ar[i] == hw->S514_slot_no){
  1866. found=1;
  1867. break;
  1868. }
  1869. if (pci_slot_ar[i] == 0xFF){
  1870. break;
  1871. }
  1872. }
  1873. if (!found){
  1874. read_S514_int_stat(hw,&int_status);
  1875. S514_intack(hw,int_status);
  1876. if (i == MAX_S514_CARDS){
  1877. printk(KERN_INFO "%s: Critical Error !!!\n",modname);
  1878. printk(KERN_INFO
  1879. "%s: Number of Sangoma PCI cards exceeded maximum limit.\n",
  1880. modname);
  1881. printk(KERN_INFO "Please contact Sangoma Technologies\n");
  1882. return 1;
  1883. }
  1884. pci_slot_ar[i] = hw->S514_slot_no;
  1885. }
  1886. return 0;
  1887. }
  1888. static int pci_probe(sdlahw_t *hw)
  1889. {
  1890. unsigned char slot_no;
  1891. int number_S514_cards = 0;
  1892. u16 PCI_subsys_vendor;
  1893. u16 PCI_card_type;
  1894. struct pci_dev *pci_dev = NULL;
  1895. struct pci_bus *bus = NULL;
  1896. slot_no = 0;
  1897. while ((pci_dev = pci_find_device(V3_VENDOR_ID, V3_DEVICE_ID, pci_dev))
  1898. != NULL) {
  1899. pci_read_config_word(pci_dev, PCI_SUBSYS_VENDOR_WORD,
  1900. &PCI_subsys_vendor);
  1901. if(PCI_subsys_vendor != SANGOMA_SUBSYS_VENDOR)
  1902. continue;
  1903. pci_read_config_word(pci_dev, PCI_CARD_TYPE,
  1904. &PCI_card_type);
  1905. bus = pci_dev->bus;
  1906. /* A dual cpu card can support up to 4 physical connections,
  1907. * where a single cpu card can support up to 2 physical
  1908. * connections. The FT1 card can only support a single
  1909. * connection, however we cannot distinguish between a Single
  1910. * CPU card and an FT1 card. */
  1911. if (PCI_card_type == S514_DUAL_CPU){
  1912. number_S514_cards += 4;
  1913. printk(KERN_INFO
  1914. "wanpipe: S514-PCI card found, cpu(s) 2, bus #%d, slot #%d, irq #%d\n",
  1915. bus->number,((pci_dev->devfn >> 3) & PCI_DEV_SLOT_MASK),
  1916. pci_dev->irq);
  1917. }else{
  1918. number_S514_cards += 2;
  1919. printk(KERN_INFO
  1920. "wanpipe: S514-PCI card found, cpu(s) 1, bus #%d, slot #%d, irq #%d\n",
  1921. bus->number,((pci_dev->devfn >> 3) & PCI_DEV_SLOT_MASK),
  1922. pci_dev->irq);
  1923. }
  1924. }
  1925. return number_S514_cards;
  1926. }
  1927. EXPORT_SYMBOL(wanpipe_hw_probe);
  1928. unsigned wanpipe_hw_probe(void)
  1929. {
  1930. sdlahw_t hw;
  1931. unsigned* opt = s508_port_options;
  1932. unsigned cardno=0;
  1933. int i;
  1934. memset(&hw, 0, sizeof(hw));
  1935. for (i = 1; i <= opt[0]; i++) {
  1936. if (detect_s508(opt[i])){
  1937. /* S508 card can support up to two physical links */
  1938. cardno+=2;
  1939. printk(KERN_INFO "wanpipe: S508-ISA card found, port 0x%x\n",opt[i]);
  1940. }
  1941. }
  1942. #ifdef CONFIG_PCI
  1943. hw.S514_slot_no = 0;
  1944. cardno += pci_probe(&hw);
  1945. #else
  1946. printk(KERN_INFO "wanpipe: Warning, Kernel not compiled for PCI support!\n");
  1947. printk(KERN_INFO "wanpipe: PCI Hardware Probe Failed!\n");
  1948. #endif
  1949. return cardno;
  1950. }
  1951. /****** End *****************************************************************/