vme_tsi148.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690
  1. /*
  2. * Support for the Tundra TSI148 VME-PCI Bridge Chip
  3. *
  4. * Author: Martyn Welch <martyn.welch@ge.com>
  5. * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
  6. *
  7. * Based on work by Tom Armistead and Ajit Prem
  8. * Copyright 2004 Motorola Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/mm.h>
  18. #include <linux/types.h>
  19. #include <linux/errno.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/pci.h>
  22. #include <linux/poll.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <linux/time.h>
  29. #include <linux/io.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/byteorder/generic.h>
  32. #include <linux/vme.h>
  33. #include "../vme_bridge.h"
  34. #include "vme_tsi148.h"
  35. static int tsi148_probe(struct pci_dev *, const struct pci_device_id *);
  36. static void tsi148_remove(struct pci_dev *);
  37. /* Module parameter */
  38. static bool err_chk;
  39. static int geoid;
  40. static const char driver_name[] = "vme_tsi148";
  41. static const struct pci_device_id tsi148_ids[] = {
  42. { PCI_DEVICE(PCI_VENDOR_ID_TUNDRA, PCI_DEVICE_ID_TUNDRA_TSI148) },
  43. { },
  44. };
  45. MODULE_DEVICE_TABLE(pci, tsi148_ids);
  46. static struct pci_driver tsi148_driver = {
  47. .name = driver_name,
  48. .id_table = tsi148_ids,
  49. .probe = tsi148_probe,
  50. .remove = tsi148_remove,
  51. };
  52. static void reg_join(unsigned int high, unsigned int low,
  53. unsigned long long *variable)
  54. {
  55. *variable = (unsigned long long)high << 32;
  56. *variable |= (unsigned long long)low;
  57. }
  58. static void reg_split(unsigned long long variable, unsigned int *high,
  59. unsigned int *low)
  60. {
  61. *low = (unsigned int)variable & 0xFFFFFFFF;
  62. *high = (unsigned int)(variable >> 32);
  63. }
  64. /*
  65. * Wakes up DMA queue.
  66. */
  67. static u32 tsi148_DMA_irqhandler(struct tsi148_driver *bridge,
  68. int channel_mask)
  69. {
  70. u32 serviced = 0;
  71. if (channel_mask & TSI148_LCSR_INTS_DMA0S) {
  72. wake_up(&bridge->dma_queue[0]);
  73. serviced |= TSI148_LCSR_INTC_DMA0C;
  74. }
  75. if (channel_mask & TSI148_LCSR_INTS_DMA1S) {
  76. wake_up(&bridge->dma_queue[1]);
  77. serviced |= TSI148_LCSR_INTC_DMA1C;
  78. }
  79. return serviced;
  80. }
  81. /*
  82. * Wake up location monitor queue
  83. */
  84. static u32 tsi148_LM_irqhandler(struct tsi148_driver *bridge, u32 stat)
  85. {
  86. int i;
  87. u32 serviced = 0;
  88. for (i = 0; i < 4; i++) {
  89. if (stat & TSI148_LCSR_INTS_LMS[i]) {
  90. /* We only enable interrupts if the callback is set */
  91. bridge->lm_callback[i](bridge->lm_data[i]);
  92. serviced |= TSI148_LCSR_INTC_LMC[i];
  93. }
  94. }
  95. return serviced;
  96. }
  97. /*
  98. * Wake up mail box queue.
  99. *
  100. * XXX This functionality is not exposed up though API.
  101. */
  102. static u32 tsi148_MB_irqhandler(struct vme_bridge *tsi148_bridge, u32 stat)
  103. {
  104. int i;
  105. u32 val;
  106. u32 serviced = 0;
  107. struct tsi148_driver *bridge;
  108. bridge = tsi148_bridge->driver_priv;
  109. for (i = 0; i < 4; i++) {
  110. if (stat & TSI148_LCSR_INTS_MBS[i]) {
  111. val = ioread32be(bridge->base + TSI148_GCSR_MBOX[i]);
  112. dev_err(tsi148_bridge->parent, "VME Mailbox %d received"
  113. ": 0x%x\n", i, val);
  114. serviced |= TSI148_LCSR_INTC_MBC[i];
  115. }
  116. }
  117. return serviced;
  118. }
  119. /*
  120. * Display error & status message when PERR (PCI) exception interrupt occurs.
  121. */
  122. static u32 tsi148_PERR_irqhandler(struct vme_bridge *tsi148_bridge)
  123. {
  124. struct tsi148_driver *bridge;
  125. bridge = tsi148_bridge->driver_priv;
  126. dev_err(tsi148_bridge->parent, "PCI Exception at address: 0x%08x:%08x, "
  127. "attributes: %08x\n",
  128. ioread32be(bridge->base + TSI148_LCSR_EDPAU),
  129. ioread32be(bridge->base + TSI148_LCSR_EDPAL),
  130. ioread32be(bridge->base + TSI148_LCSR_EDPAT));
  131. dev_err(tsi148_bridge->parent, "PCI-X attribute reg: %08x, PCI-X split "
  132. "completion reg: %08x\n",
  133. ioread32be(bridge->base + TSI148_LCSR_EDPXA),
  134. ioread32be(bridge->base + TSI148_LCSR_EDPXS));
  135. iowrite32be(TSI148_LCSR_EDPAT_EDPCL, bridge->base + TSI148_LCSR_EDPAT);
  136. return TSI148_LCSR_INTC_PERRC;
  137. }
  138. /*
  139. * Save address and status when VME error interrupt occurs.
  140. */
  141. static u32 tsi148_VERR_irqhandler(struct vme_bridge *tsi148_bridge)
  142. {
  143. unsigned int error_addr_high, error_addr_low;
  144. unsigned long long error_addr;
  145. u32 error_attrib;
  146. int error_am;
  147. struct tsi148_driver *bridge;
  148. bridge = tsi148_bridge->driver_priv;
  149. error_addr_high = ioread32be(bridge->base + TSI148_LCSR_VEAU);
  150. error_addr_low = ioread32be(bridge->base + TSI148_LCSR_VEAL);
  151. error_attrib = ioread32be(bridge->base + TSI148_LCSR_VEAT);
  152. error_am = (error_attrib & TSI148_LCSR_VEAT_AM_M) >> 8;
  153. reg_join(error_addr_high, error_addr_low, &error_addr);
  154. /* Check for exception register overflow (we have lost error data) */
  155. if (error_attrib & TSI148_LCSR_VEAT_VEOF) {
  156. dev_err(tsi148_bridge->parent, "VME Bus Exception Overflow "
  157. "Occurred\n");
  158. }
  159. if (err_chk)
  160. vme_bus_error_handler(tsi148_bridge, error_addr, error_am);
  161. else
  162. dev_err(tsi148_bridge->parent,
  163. "VME Bus Error at address: 0x%llx, attributes: %08x\n",
  164. error_addr, error_attrib);
  165. /* Clear Status */
  166. iowrite32be(TSI148_LCSR_VEAT_VESCL, bridge->base + TSI148_LCSR_VEAT);
  167. return TSI148_LCSR_INTC_VERRC;
  168. }
  169. /*
  170. * Wake up IACK queue.
  171. */
  172. static u32 tsi148_IACK_irqhandler(struct tsi148_driver *bridge)
  173. {
  174. wake_up(&bridge->iack_queue);
  175. return TSI148_LCSR_INTC_IACKC;
  176. }
  177. /*
  178. * Calling VME bus interrupt callback if provided.
  179. */
  180. static u32 tsi148_VIRQ_irqhandler(struct vme_bridge *tsi148_bridge,
  181. u32 stat)
  182. {
  183. int vec, i, serviced = 0;
  184. struct tsi148_driver *bridge;
  185. bridge = tsi148_bridge->driver_priv;
  186. for (i = 7; i > 0; i--) {
  187. if (stat & (1 << i)) {
  188. /*
  189. * Note: Even though the registers are defined as
  190. * 32-bits in the spec, we only want to issue 8-bit
  191. * IACK cycles on the bus, read from offset 3.
  192. */
  193. vec = ioread8(bridge->base + TSI148_LCSR_VIACK[i] + 3);
  194. vme_irq_handler(tsi148_bridge, i, vec);
  195. serviced |= (1 << i);
  196. }
  197. }
  198. return serviced;
  199. }
  200. /*
  201. * Top level interrupt handler. Clears appropriate interrupt status bits and
  202. * then calls appropriate sub handler(s).
  203. */
  204. static irqreturn_t tsi148_irqhandler(int irq, void *ptr)
  205. {
  206. u32 stat, enable, serviced = 0;
  207. struct vme_bridge *tsi148_bridge;
  208. struct tsi148_driver *bridge;
  209. tsi148_bridge = ptr;
  210. bridge = tsi148_bridge->driver_priv;
  211. /* Determine which interrupts are unmasked and set */
  212. enable = ioread32be(bridge->base + TSI148_LCSR_INTEO);
  213. stat = ioread32be(bridge->base + TSI148_LCSR_INTS);
  214. /* Only look at unmasked interrupts */
  215. stat &= enable;
  216. if (unlikely(!stat))
  217. return IRQ_NONE;
  218. /* Call subhandlers as appropriate */
  219. /* DMA irqs */
  220. if (stat & (TSI148_LCSR_INTS_DMA1S | TSI148_LCSR_INTS_DMA0S))
  221. serviced |= tsi148_DMA_irqhandler(bridge, stat);
  222. /* Location monitor irqs */
  223. if (stat & (TSI148_LCSR_INTS_LM3S | TSI148_LCSR_INTS_LM2S |
  224. TSI148_LCSR_INTS_LM1S | TSI148_LCSR_INTS_LM0S))
  225. serviced |= tsi148_LM_irqhandler(bridge, stat);
  226. /* Mail box irqs */
  227. if (stat & (TSI148_LCSR_INTS_MB3S | TSI148_LCSR_INTS_MB2S |
  228. TSI148_LCSR_INTS_MB1S | TSI148_LCSR_INTS_MB0S))
  229. serviced |= tsi148_MB_irqhandler(tsi148_bridge, stat);
  230. /* PCI bus error */
  231. if (stat & TSI148_LCSR_INTS_PERRS)
  232. serviced |= tsi148_PERR_irqhandler(tsi148_bridge);
  233. /* VME bus error */
  234. if (stat & TSI148_LCSR_INTS_VERRS)
  235. serviced |= tsi148_VERR_irqhandler(tsi148_bridge);
  236. /* IACK irq */
  237. if (stat & TSI148_LCSR_INTS_IACKS)
  238. serviced |= tsi148_IACK_irqhandler(bridge);
  239. /* VME bus irqs */
  240. if (stat & (TSI148_LCSR_INTS_IRQ7S | TSI148_LCSR_INTS_IRQ6S |
  241. TSI148_LCSR_INTS_IRQ5S | TSI148_LCSR_INTS_IRQ4S |
  242. TSI148_LCSR_INTS_IRQ3S | TSI148_LCSR_INTS_IRQ2S |
  243. TSI148_LCSR_INTS_IRQ1S))
  244. serviced |= tsi148_VIRQ_irqhandler(tsi148_bridge, stat);
  245. /* Clear serviced interrupts */
  246. iowrite32be(serviced, bridge->base + TSI148_LCSR_INTC);
  247. return IRQ_HANDLED;
  248. }
  249. static int tsi148_irq_init(struct vme_bridge *tsi148_bridge)
  250. {
  251. int result;
  252. unsigned int tmp;
  253. struct pci_dev *pdev;
  254. struct tsi148_driver *bridge;
  255. pdev = to_pci_dev(tsi148_bridge->parent);
  256. bridge = tsi148_bridge->driver_priv;
  257. result = request_irq(pdev->irq,
  258. tsi148_irqhandler,
  259. IRQF_SHARED,
  260. driver_name, tsi148_bridge);
  261. if (result) {
  262. dev_err(tsi148_bridge->parent, "Can't get assigned pci irq "
  263. "vector %02X\n", pdev->irq);
  264. return result;
  265. }
  266. /* Enable and unmask interrupts */
  267. tmp = TSI148_LCSR_INTEO_DMA1EO | TSI148_LCSR_INTEO_DMA0EO |
  268. TSI148_LCSR_INTEO_MB3EO | TSI148_LCSR_INTEO_MB2EO |
  269. TSI148_LCSR_INTEO_MB1EO | TSI148_LCSR_INTEO_MB0EO |
  270. TSI148_LCSR_INTEO_PERREO | TSI148_LCSR_INTEO_VERREO |
  271. TSI148_LCSR_INTEO_IACKEO;
  272. /* This leaves the following interrupts masked.
  273. * TSI148_LCSR_INTEO_VIEEO
  274. * TSI148_LCSR_INTEO_SYSFLEO
  275. * TSI148_LCSR_INTEO_ACFLEO
  276. */
  277. /* Don't enable Location Monitor interrupts here - they will be
  278. * enabled when the location monitors are properly configured and
  279. * a callback has been attached.
  280. * TSI148_LCSR_INTEO_LM0EO
  281. * TSI148_LCSR_INTEO_LM1EO
  282. * TSI148_LCSR_INTEO_LM2EO
  283. * TSI148_LCSR_INTEO_LM3EO
  284. */
  285. /* Don't enable VME interrupts until we add a handler, else the board
  286. * will respond to it and we don't want that unless it knows how to
  287. * properly deal with it.
  288. * TSI148_LCSR_INTEO_IRQ7EO
  289. * TSI148_LCSR_INTEO_IRQ6EO
  290. * TSI148_LCSR_INTEO_IRQ5EO
  291. * TSI148_LCSR_INTEO_IRQ4EO
  292. * TSI148_LCSR_INTEO_IRQ3EO
  293. * TSI148_LCSR_INTEO_IRQ2EO
  294. * TSI148_LCSR_INTEO_IRQ1EO
  295. */
  296. iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
  297. iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEN);
  298. return 0;
  299. }
  300. static void tsi148_irq_exit(struct vme_bridge *tsi148_bridge,
  301. struct pci_dev *pdev)
  302. {
  303. struct tsi148_driver *bridge = tsi148_bridge->driver_priv;
  304. /* Turn off interrupts */
  305. iowrite32be(0x0, bridge->base + TSI148_LCSR_INTEO);
  306. iowrite32be(0x0, bridge->base + TSI148_LCSR_INTEN);
  307. /* Clear all interrupts */
  308. iowrite32be(0xFFFFFFFF, bridge->base + TSI148_LCSR_INTC);
  309. /* Detach interrupt handler */
  310. free_irq(pdev->irq, tsi148_bridge);
  311. }
  312. /*
  313. * Check to see if an IACk has been received, return true (1) or false (0).
  314. */
  315. static int tsi148_iack_received(struct tsi148_driver *bridge)
  316. {
  317. u32 tmp;
  318. tmp = ioread32be(bridge->base + TSI148_LCSR_VICR);
  319. if (tmp & TSI148_LCSR_VICR_IRQS)
  320. return 0;
  321. else
  322. return 1;
  323. }
  324. /*
  325. * Configure VME interrupt
  326. */
  327. static void tsi148_irq_set(struct vme_bridge *tsi148_bridge, int level,
  328. int state, int sync)
  329. {
  330. struct pci_dev *pdev;
  331. u32 tmp;
  332. struct tsi148_driver *bridge;
  333. bridge = tsi148_bridge->driver_priv;
  334. /* We need to do the ordering differently for enabling and disabling */
  335. if (state == 0) {
  336. tmp = ioread32be(bridge->base + TSI148_LCSR_INTEN);
  337. tmp &= ~TSI148_LCSR_INTEN_IRQEN[level - 1];
  338. iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEN);
  339. tmp = ioread32be(bridge->base + TSI148_LCSR_INTEO);
  340. tmp &= ~TSI148_LCSR_INTEO_IRQEO[level - 1];
  341. iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
  342. if (sync != 0) {
  343. pdev = to_pci_dev(tsi148_bridge->parent);
  344. synchronize_irq(pdev->irq);
  345. }
  346. } else {
  347. tmp = ioread32be(bridge->base + TSI148_LCSR_INTEO);
  348. tmp |= TSI148_LCSR_INTEO_IRQEO[level - 1];
  349. iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
  350. tmp = ioread32be(bridge->base + TSI148_LCSR_INTEN);
  351. tmp |= TSI148_LCSR_INTEN_IRQEN[level - 1];
  352. iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEN);
  353. }
  354. }
  355. /*
  356. * Generate a VME bus interrupt at the requested level & vector. Wait for
  357. * interrupt to be acked.
  358. */
  359. static int tsi148_irq_generate(struct vme_bridge *tsi148_bridge, int level,
  360. int statid)
  361. {
  362. u32 tmp;
  363. struct tsi148_driver *bridge;
  364. bridge = tsi148_bridge->driver_priv;
  365. mutex_lock(&bridge->vme_int);
  366. /* Read VICR register */
  367. tmp = ioread32be(bridge->base + TSI148_LCSR_VICR);
  368. /* Set Status/ID */
  369. tmp = (tmp & ~TSI148_LCSR_VICR_STID_M) |
  370. (statid & TSI148_LCSR_VICR_STID_M);
  371. iowrite32be(tmp, bridge->base + TSI148_LCSR_VICR);
  372. /* Assert VMEbus IRQ */
  373. tmp = tmp | TSI148_LCSR_VICR_IRQL[level];
  374. iowrite32be(tmp, bridge->base + TSI148_LCSR_VICR);
  375. /* XXX Consider implementing a timeout? */
  376. wait_event_interruptible(bridge->iack_queue,
  377. tsi148_iack_received(bridge));
  378. mutex_unlock(&bridge->vme_int);
  379. return 0;
  380. }
  381. /*
  382. * Initialize a slave window with the requested attributes.
  383. */
  384. static int tsi148_slave_set(struct vme_slave_resource *image, int enabled,
  385. unsigned long long vme_base, unsigned long long size,
  386. dma_addr_t pci_base, u32 aspace, u32 cycle)
  387. {
  388. unsigned int i, addr = 0, granularity = 0;
  389. unsigned int temp_ctl = 0;
  390. unsigned int vme_base_low, vme_base_high;
  391. unsigned int vme_bound_low, vme_bound_high;
  392. unsigned int pci_offset_low, pci_offset_high;
  393. unsigned long long vme_bound, pci_offset;
  394. struct vme_bridge *tsi148_bridge;
  395. struct tsi148_driver *bridge;
  396. tsi148_bridge = image->parent;
  397. bridge = tsi148_bridge->driver_priv;
  398. i = image->number;
  399. switch (aspace) {
  400. case VME_A16:
  401. granularity = 0x10;
  402. addr |= TSI148_LCSR_ITAT_AS_A16;
  403. break;
  404. case VME_A24:
  405. granularity = 0x1000;
  406. addr |= TSI148_LCSR_ITAT_AS_A24;
  407. break;
  408. case VME_A32:
  409. granularity = 0x10000;
  410. addr |= TSI148_LCSR_ITAT_AS_A32;
  411. break;
  412. case VME_A64:
  413. granularity = 0x10000;
  414. addr |= TSI148_LCSR_ITAT_AS_A64;
  415. break;
  416. default:
  417. dev_err(tsi148_bridge->parent, "Invalid address space\n");
  418. return -EINVAL;
  419. break;
  420. }
  421. /* Convert 64-bit variables to 2x 32-bit variables */
  422. reg_split(vme_base, &vme_base_high, &vme_base_low);
  423. /*
  424. * Bound address is a valid address for the window, adjust
  425. * accordingly
  426. */
  427. vme_bound = vme_base + size - granularity;
  428. reg_split(vme_bound, &vme_bound_high, &vme_bound_low);
  429. pci_offset = (unsigned long long)pci_base - vme_base;
  430. reg_split(pci_offset, &pci_offset_high, &pci_offset_low);
  431. if (vme_base_low & (granularity - 1)) {
  432. dev_err(tsi148_bridge->parent, "Invalid VME base alignment\n");
  433. return -EINVAL;
  434. }
  435. if (vme_bound_low & (granularity - 1)) {
  436. dev_err(tsi148_bridge->parent, "Invalid VME bound alignment\n");
  437. return -EINVAL;
  438. }
  439. if (pci_offset_low & (granularity - 1)) {
  440. dev_err(tsi148_bridge->parent, "Invalid PCI Offset "
  441. "alignment\n");
  442. return -EINVAL;
  443. }
  444. /* Disable while we are mucking around */
  445. temp_ctl = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
  446. TSI148_LCSR_OFFSET_ITAT);
  447. temp_ctl &= ~TSI148_LCSR_ITAT_EN;
  448. iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_IT[i] +
  449. TSI148_LCSR_OFFSET_ITAT);
  450. /* Setup mapping */
  451. iowrite32be(vme_base_high, bridge->base + TSI148_LCSR_IT[i] +
  452. TSI148_LCSR_OFFSET_ITSAU);
  453. iowrite32be(vme_base_low, bridge->base + TSI148_LCSR_IT[i] +
  454. TSI148_LCSR_OFFSET_ITSAL);
  455. iowrite32be(vme_bound_high, bridge->base + TSI148_LCSR_IT[i] +
  456. TSI148_LCSR_OFFSET_ITEAU);
  457. iowrite32be(vme_bound_low, bridge->base + TSI148_LCSR_IT[i] +
  458. TSI148_LCSR_OFFSET_ITEAL);
  459. iowrite32be(pci_offset_high, bridge->base + TSI148_LCSR_IT[i] +
  460. TSI148_LCSR_OFFSET_ITOFU);
  461. iowrite32be(pci_offset_low, bridge->base + TSI148_LCSR_IT[i] +
  462. TSI148_LCSR_OFFSET_ITOFL);
  463. /* Setup 2eSST speeds */
  464. temp_ctl &= ~TSI148_LCSR_ITAT_2eSSTM_M;
  465. switch (cycle & (VME_2eSST160 | VME_2eSST267 | VME_2eSST320)) {
  466. case VME_2eSST160:
  467. temp_ctl |= TSI148_LCSR_ITAT_2eSSTM_160;
  468. break;
  469. case VME_2eSST267:
  470. temp_ctl |= TSI148_LCSR_ITAT_2eSSTM_267;
  471. break;
  472. case VME_2eSST320:
  473. temp_ctl |= TSI148_LCSR_ITAT_2eSSTM_320;
  474. break;
  475. }
  476. /* Setup cycle types */
  477. temp_ctl &= ~(0x1F << 7);
  478. if (cycle & VME_BLT)
  479. temp_ctl |= TSI148_LCSR_ITAT_BLT;
  480. if (cycle & VME_MBLT)
  481. temp_ctl |= TSI148_LCSR_ITAT_MBLT;
  482. if (cycle & VME_2eVME)
  483. temp_ctl |= TSI148_LCSR_ITAT_2eVME;
  484. if (cycle & VME_2eSST)
  485. temp_ctl |= TSI148_LCSR_ITAT_2eSST;
  486. if (cycle & VME_2eSSTB)
  487. temp_ctl |= TSI148_LCSR_ITAT_2eSSTB;
  488. /* Setup address space */
  489. temp_ctl &= ~TSI148_LCSR_ITAT_AS_M;
  490. temp_ctl |= addr;
  491. temp_ctl &= ~0xF;
  492. if (cycle & VME_SUPER)
  493. temp_ctl |= TSI148_LCSR_ITAT_SUPR ;
  494. if (cycle & VME_USER)
  495. temp_ctl |= TSI148_LCSR_ITAT_NPRIV;
  496. if (cycle & VME_PROG)
  497. temp_ctl |= TSI148_LCSR_ITAT_PGM;
  498. if (cycle & VME_DATA)
  499. temp_ctl |= TSI148_LCSR_ITAT_DATA;
  500. /* Write ctl reg without enable */
  501. iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_IT[i] +
  502. TSI148_LCSR_OFFSET_ITAT);
  503. if (enabled)
  504. temp_ctl |= TSI148_LCSR_ITAT_EN;
  505. iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_IT[i] +
  506. TSI148_LCSR_OFFSET_ITAT);
  507. return 0;
  508. }
  509. /*
  510. * Get slave window configuration.
  511. */
  512. static int tsi148_slave_get(struct vme_slave_resource *image, int *enabled,
  513. unsigned long long *vme_base, unsigned long long *size,
  514. dma_addr_t *pci_base, u32 *aspace, u32 *cycle)
  515. {
  516. unsigned int i, granularity = 0, ctl = 0;
  517. unsigned int vme_base_low, vme_base_high;
  518. unsigned int vme_bound_low, vme_bound_high;
  519. unsigned int pci_offset_low, pci_offset_high;
  520. unsigned long long vme_bound, pci_offset;
  521. struct tsi148_driver *bridge;
  522. bridge = image->parent->driver_priv;
  523. i = image->number;
  524. /* Read registers */
  525. ctl = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
  526. TSI148_LCSR_OFFSET_ITAT);
  527. vme_base_high = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
  528. TSI148_LCSR_OFFSET_ITSAU);
  529. vme_base_low = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
  530. TSI148_LCSR_OFFSET_ITSAL);
  531. vme_bound_high = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
  532. TSI148_LCSR_OFFSET_ITEAU);
  533. vme_bound_low = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
  534. TSI148_LCSR_OFFSET_ITEAL);
  535. pci_offset_high = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
  536. TSI148_LCSR_OFFSET_ITOFU);
  537. pci_offset_low = ioread32be(bridge->base + TSI148_LCSR_IT[i] +
  538. TSI148_LCSR_OFFSET_ITOFL);
  539. /* Convert 64-bit variables to 2x 32-bit variables */
  540. reg_join(vme_base_high, vme_base_low, vme_base);
  541. reg_join(vme_bound_high, vme_bound_low, &vme_bound);
  542. reg_join(pci_offset_high, pci_offset_low, &pci_offset);
  543. *pci_base = (dma_addr_t)(*vme_base + pci_offset);
  544. *enabled = 0;
  545. *aspace = 0;
  546. *cycle = 0;
  547. if (ctl & TSI148_LCSR_ITAT_EN)
  548. *enabled = 1;
  549. if ((ctl & TSI148_LCSR_ITAT_AS_M) == TSI148_LCSR_ITAT_AS_A16) {
  550. granularity = 0x10;
  551. *aspace |= VME_A16;
  552. }
  553. if ((ctl & TSI148_LCSR_ITAT_AS_M) == TSI148_LCSR_ITAT_AS_A24) {
  554. granularity = 0x1000;
  555. *aspace |= VME_A24;
  556. }
  557. if ((ctl & TSI148_LCSR_ITAT_AS_M) == TSI148_LCSR_ITAT_AS_A32) {
  558. granularity = 0x10000;
  559. *aspace |= VME_A32;
  560. }
  561. if ((ctl & TSI148_LCSR_ITAT_AS_M) == TSI148_LCSR_ITAT_AS_A64) {
  562. granularity = 0x10000;
  563. *aspace |= VME_A64;
  564. }
  565. /* Need granularity before we set the size */
  566. *size = (unsigned long long)((vme_bound - *vme_base) + granularity);
  567. if ((ctl & TSI148_LCSR_ITAT_2eSSTM_M) == TSI148_LCSR_ITAT_2eSSTM_160)
  568. *cycle |= VME_2eSST160;
  569. if ((ctl & TSI148_LCSR_ITAT_2eSSTM_M) == TSI148_LCSR_ITAT_2eSSTM_267)
  570. *cycle |= VME_2eSST267;
  571. if ((ctl & TSI148_LCSR_ITAT_2eSSTM_M) == TSI148_LCSR_ITAT_2eSSTM_320)
  572. *cycle |= VME_2eSST320;
  573. if (ctl & TSI148_LCSR_ITAT_BLT)
  574. *cycle |= VME_BLT;
  575. if (ctl & TSI148_LCSR_ITAT_MBLT)
  576. *cycle |= VME_MBLT;
  577. if (ctl & TSI148_LCSR_ITAT_2eVME)
  578. *cycle |= VME_2eVME;
  579. if (ctl & TSI148_LCSR_ITAT_2eSST)
  580. *cycle |= VME_2eSST;
  581. if (ctl & TSI148_LCSR_ITAT_2eSSTB)
  582. *cycle |= VME_2eSSTB;
  583. if (ctl & TSI148_LCSR_ITAT_SUPR)
  584. *cycle |= VME_SUPER;
  585. if (ctl & TSI148_LCSR_ITAT_NPRIV)
  586. *cycle |= VME_USER;
  587. if (ctl & TSI148_LCSR_ITAT_PGM)
  588. *cycle |= VME_PROG;
  589. if (ctl & TSI148_LCSR_ITAT_DATA)
  590. *cycle |= VME_DATA;
  591. return 0;
  592. }
  593. /*
  594. * Allocate and map PCI Resource
  595. */
  596. static int tsi148_alloc_resource(struct vme_master_resource *image,
  597. unsigned long long size)
  598. {
  599. unsigned long long existing_size;
  600. int retval = 0;
  601. struct pci_dev *pdev;
  602. struct vme_bridge *tsi148_bridge;
  603. tsi148_bridge = image->parent;
  604. pdev = to_pci_dev(tsi148_bridge->parent);
  605. existing_size = (unsigned long long)(image->bus_resource.end -
  606. image->bus_resource.start);
  607. /* If the existing size is OK, return */
  608. if ((size != 0) && (existing_size == (size - 1)))
  609. return 0;
  610. if (existing_size != 0) {
  611. iounmap(image->kern_base);
  612. image->kern_base = NULL;
  613. kfree(image->bus_resource.name);
  614. release_resource(&image->bus_resource);
  615. memset(&image->bus_resource, 0, sizeof(struct resource));
  616. }
  617. /* Exit here if size is zero */
  618. if (size == 0)
  619. return 0;
  620. if (image->bus_resource.name == NULL) {
  621. image->bus_resource.name = kmalloc(VMENAMSIZ+3, GFP_ATOMIC);
  622. if (image->bus_resource.name == NULL) {
  623. dev_err(tsi148_bridge->parent, "Unable to allocate "
  624. "memory for resource name\n");
  625. retval = -ENOMEM;
  626. goto err_name;
  627. }
  628. }
  629. sprintf((char *)image->bus_resource.name, "%s.%d", tsi148_bridge->name,
  630. image->number);
  631. image->bus_resource.start = 0;
  632. image->bus_resource.end = (unsigned long)size;
  633. image->bus_resource.flags = IORESOURCE_MEM;
  634. retval = pci_bus_alloc_resource(pdev->bus,
  635. &image->bus_resource, size, 0x10000, PCIBIOS_MIN_MEM,
  636. 0, NULL, NULL);
  637. if (retval) {
  638. dev_err(tsi148_bridge->parent, "Failed to allocate mem "
  639. "resource for window %d size 0x%lx start 0x%lx\n",
  640. image->number, (unsigned long)size,
  641. (unsigned long)image->bus_resource.start);
  642. goto err_resource;
  643. }
  644. image->kern_base = ioremap_nocache(
  645. image->bus_resource.start, size);
  646. if (image->kern_base == NULL) {
  647. dev_err(tsi148_bridge->parent, "Failed to remap resource\n");
  648. retval = -ENOMEM;
  649. goto err_remap;
  650. }
  651. return 0;
  652. err_remap:
  653. release_resource(&image->bus_resource);
  654. err_resource:
  655. kfree(image->bus_resource.name);
  656. memset(&image->bus_resource, 0, sizeof(struct resource));
  657. err_name:
  658. return retval;
  659. }
  660. /*
  661. * Free and unmap PCI Resource
  662. */
  663. static void tsi148_free_resource(struct vme_master_resource *image)
  664. {
  665. iounmap(image->kern_base);
  666. image->kern_base = NULL;
  667. release_resource(&image->bus_resource);
  668. kfree(image->bus_resource.name);
  669. memset(&image->bus_resource, 0, sizeof(struct resource));
  670. }
  671. /*
  672. * Set the attributes of an outbound window.
  673. */
  674. static int tsi148_master_set(struct vme_master_resource *image, int enabled,
  675. unsigned long long vme_base, unsigned long long size, u32 aspace,
  676. u32 cycle, u32 dwidth)
  677. {
  678. int retval = 0;
  679. unsigned int i;
  680. unsigned int temp_ctl = 0;
  681. unsigned int pci_base_low, pci_base_high;
  682. unsigned int pci_bound_low, pci_bound_high;
  683. unsigned int vme_offset_low, vme_offset_high;
  684. unsigned long long pci_bound, vme_offset, pci_base;
  685. struct vme_bridge *tsi148_bridge;
  686. struct tsi148_driver *bridge;
  687. struct pci_bus_region region;
  688. struct pci_dev *pdev;
  689. tsi148_bridge = image->parent;
  690. bridge = tsi148_bridge->driver_priv;
  691. pdev = to_pci_dev(tsi148_bridge->parent);
  692. /* Verify input data */
  693. if (vme_base & 0xFFFF) {
  694. dev_err(tsi148_bridge->parent, "Invalid VME Window "
  695. "alignment\n");
  696. retval = -EINVAL;
  697. goto err_window;
  698. }
  699. if ((size == 0) && (enabled != 0)) {
  700. dev_err(tsi148_bridge->parent, "Size must be non-zero for "
  701. "enabled windows\n");
  702. retval = -EINVAL;
  703. goto err_window;
  704. }
  705. spin_lock(&image->lock);
  706. /* Let's allocate the resource here rather than further up the stack as
  707. * it avoids pushing loads of bus dependent stuff up the stack. If size
  708. * is zero, any existing resource will be freed.
  709. */
  710. retval = tsi148_alloc_resource(image, size);
  711. if (retval) {
  712. spin_unlock(&image->lock);
  713. dev_err(tsi148_bridge->parent, "Unable to allocate memory for "
  714. "resource\n");
  715. goto err_res;
  716. }
  717. if (size == 0) {
  718. pci_base = 0;
  719. pci_bound = 0;
  720. vme_offset = 0;
  721. } else {
  722. pcibios_resource_to_bus(pdev->bus, &region,
  723. &image->bus_resource);
  724. pci_base = region.start;
  725. /*
  726. * Bound address is a valid address for the window, adjust
  727. * according to window granularity.
  728. */
  729. pci_bound = pci_base + (size - 0x10000);
  730. vme_offset = vme_base - pci_base;
  731. }
  732. /* Convert 64-bit variables to 2x 32-bit variables */
  733. reg_split(pci_base, &pci_base_high, &pci_base_low);
  734. reg_split(pci_bound, &pci_bound_high, &pci_bound_low);
  735. reg_split(vme_offset, &vme_offset_high, &vme_offset_low);
  736. if (pci_base_low & 0xFFFF) {
  737. spin_unlock(&image->lock);
  738. dev_err(tsi148_bridge->parent, "Invalid PCI base alignment\n");
  739. retval = -EINVAL;
  740. goto err_gran;
  741. }
  742. if (pci_bound_low & 0xFFFF) {
  743. spin_unlock(&image->lock);
  744. dev_err(tsi148_bridge->parent, "Invalid PCI bound alignment\n");
  745. retval = -EINVAL;
  746. goto err_gran;
  747. }
  748. if (vme_offset_low & 0xFFFF) {
  749. spin_unlock(&image->lock);
  750. dev_err(tsi148_bridge->parent, "Invalid VME Offset "
  751. "alignment\n");
  752. retval = -EINVAL;
  753. goto err_gran;
  754. }
  755. i = image->number;
  756. /* Disable while we are mucking around */
  757. temp_ctl = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
  758. TSI148_LCSR_OFFSET_OTAT);
  759. temp_ctl &= ~TSI148_LCSR_OTAT_EN;
  760. iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_OT[i] +
  761. TSI148_LCSR_OFFSET_OTAT);
  762. /* Setup 2eSST speeds */
  763. temp_ctl &= ~TSI148_LCSR_OTAT_2eSSTM_M;
  764. switch (cycle & (VME_2eSST160 | VME_2eSST267 | VME_2eSST320)) {
  765. case VME_2eSST160:
  766. temp_ctl |= TSI148_LCSR_OTAT_2eSSTM_160;
  767. break;
  768. case VME_2eSST267:
  769. temp_ctl |= TSI148_LCSR_OTAT_2eSSTM_267;
  770. break;
  771. case VME_2eSST320:
  772. temp_ctl |= TSI148_LCSR_OTAT_2eSSTM_320;
  773. break;
  774. }
  775. /* Setup cycle types */
  776. if (cycle & VME_BLT) {
  777. temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
  778. temp_ctl |= TSI148_LCSR_OTAT_TM_BLT;
  779. }
  780. if (cycle & VME_MBLT) {
  781. temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
  782. temp_ctl |= TSI148_LCSR_OTAT_TM_MBLT;
  783. }
  784. if (cycle & VME_2eVME) {
  785. temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
  786. temp_ctl |= TSI148_LCSR_OTAT_TM_2eVME;
  787. }
  788. if (cycle & VME_2eSST) {
  789. temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
  790. temp_ctl |= TSI148_LCSR_OTAT_TM_2eSST;
  791. }
  792. if (cycle & VME_2eSSTB) {
  793. dev_warn(tsi148_bridge->parent, "Currently not setting "
  794. "Broadcast Select Registers\n");
  795. temp_ctl &= ~TSI148_LCSR_OTAT_TM_M;
  796. temp_ctl |= TSI148_LCSR_OTAT_TM_2eSSTB;
  797. }
  798. /* Setup data width */
  799. temp_ctl &= ~TSI148_LCSR_OTAT_DBW_M;
  800. switch (dwidth) {
  801. case VME_D16:
  802. temp_ctl |= TSI148_LCSR_OTAT_DBW_16;
  803. break;
  804. case VME_D32:
  805. temp_ctl |= TSI148_LCSR_OTAT_DBW_32;
  806. break;
  807. default:
  808. spin_unlock(&image->lock);
  809. dev_err(tsi148_bridge->parent, "Invalid data width\n");
  810. retval = -EINVAL;
  811. goto err_dwidth;
  812. }
  813. /* Setup address space */
  814. temp_ctl &= ~TSI148_LCSR_OTAT_AMODE_M;
  815. switch (aspace) {
  816. case VME_A16:
  817. temp_ctl |= TSI148_LCSR_OTAT_AMODE_A16;
  818. break;
  819. case VME_A24:
  820. temp_ctl |= TSI148_LCSR_OTAT_AMODE_A24;
  821. break;
  822. case VME_A32:
  823. temp_ctl |= TSI148_LCSR_OTAT_AMODE_A32;
  824. break;
  825. case VME_A64:
  826. temp_ctl |= TSI148_LCSR_OTAT_AMODE_A64;
  827. break;
  828. case VME_CRCSR:
  829. temp_ctl |= TSI148_LCSR_OTAT_AMODE_CRCSR;
  830. break;
  831. case VME_USER1:
  832. temp_ctl |= TSI148_LCSR_OTAT_AMODE_USER1;
  833. break;
  834. case VME_USER2:
  835. temp_ctl |= TSI148_LCSR_OTAT_AMODE_USER2;
  836. break;
  837. case VME_USER3:
  838. temp_ctl |= TSI148_LCSR_OTAT_AMODE_USER3;
  839. break;
  840. case VME_USER4:
  841. temp_ctl |= TSI148_LCSR_OTAT_AMODE_USER4;
  842. break;
  843. default:
  844. spin_unlock(&image->lock);
  845. dev_err(tsi148_bridge->parent, "Invalid address space\n");
  846. retval = -EINVAL;
  847. goto err_aspace;
  848. break;
  849. }
  850. temp_ctl &= ~(3<<4);
  851. if (cycle & VME_SUPER)
  852. temp_ctl |= TSI148_LCSR_OTAT_SUP;
  853. if (cycle & VME_PROG)
  854. temp_ctl |= TSI148_LCSR_OTAT_PGM;
  855. /* Setup mapping */
  856. iowrite32be(pci_base_high, bridge->base + TSI148_LCSR_OT[i] +
  857. TSI148_LCSR_OFFSET_OTSAU);
  858. iowrite32be(pci_base_low, bridge->base + TSI148_LCSR_OT[i] +
  859. TSI148_LCSR_OFFSET_OTSAL);
  860. iowrite32be(pci_bound_high, bridge->base + TSI148_LCSR_OT[i] +
  861. TSI148_LCSR_OFFSET_OTEAU);
  862. iowrite32be(pci_bound_low, bridge->base + TSI148_LCSR_OT[i] +
  863. TSI148_LCSR_OFFSET_OTEAL);
  864. iowrite32be(vme_offset_high, bridge->base + TSI148_LCSR_OT[i] +
  865. TSI148_LCSR_OFFSET_OTOFU);
  866. iowrite32be(vme_offset_low, bridge->base + TSI148_LCSR_OT[i] +
  867. TSI148_LCSR_OFFSET_OTOFL);
  868. /* Write ctl reg without enable */
  869. iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_OT[i] +
  870. TSI148_LCSR_OFFSET_OTAT);
  871. if (enabled)
  872. temp_ctl |= TSI148_LCSR_OTAT_EN;
  873. iowrite32be(temp_ctl, bridge->base + TSI148_LCSR_OT[i] +
  874. TSI148_LCSR_OFFSET_OTAT);
  875. spin_unlock(&image->lock);
  876. return 0;
  877. err_aspace:
  878. err_dwidth:
  879. err_gran:
  880. tsi148_free_resource(image);
  881. err_res:
  882. err_window:
  883. return retval;
  884. }
  885. /*
  886. * Set the attributes of an outbound window.
  887. *
  888. * XXX Not parsing prefetch information.
  889. */
  890. static int __tsi148_master_get(struct vme_master_resource *image, int *enabled,
  891. unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
  892. u32 *cycle, u32 *dwidth)
  893. {
  894. unsigned int i, ctl;
  895. unsigned int pci_base_low, pci_base_high;
  896. unsigned int pci_bound_low, pci_bound_high;
  897. unsigned int vme_offset_low, vme_offset_high;
  898. unsigned long long pci_base, pci_bound, vme_offset;
  899. struct tsi148_driver *bridge;
  900. bridge = image->parent->driver_priv;
  901. i = image->number;
  902. ctl = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
  903. TSI148_LCSR_OFFSET_OTAT);
  904. pci_base_high = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
  905. TSI148_LCSR_OFFSET_OTSAU);
  906. pci_base_low = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
  907. TSI148_LCSR_OFFSET_OTSAL);
  908. pci_bound_high = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
  909. TSI148_LCSR_OFFSET_OTEAU);
  910. pci_bound_low = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
  911. TSI148_LCSR_OFFSET_OTEAL);
  912. vme_offset_high = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
  913. TSI148_LCSR_OFFSET_OTOFU);
  914. vme_offset_low = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
  915. TSI148_LCSR_OFFSET_OTOFL);
  916. /* Convert 64-bit variables to 2x 32-bit variables */
  917. reg_join(pci_base_high, pci_base_low, &pci_base);
  918. reg_join(pci_bound_high, pci_bound_low, &pci_bound);
  919. reg_join(vme_offset_high, vme_offset_low, &vme_offset);
  920. *vme_base = pci_base + vme_offset;
  921. *size = (unsigned long long)(pci_bound - pci_base) + 0x10000;
  922. *enabled = 0;
  923. *aspace = 0;
  924. *cycle = 0;
  925. *dwidth = 0;
  926. if (ctl & TSI148_LCSR_OTAT_EN)
  927. *enabled = 1;
  928. /* Setup address space */
  929. if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_A16)
  930. *aspace |= VME_A16;
  931. if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_A24)
  932. *aspace |= VME_A24;
  933. if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_A32)
  934. *aspace |= VME_A32;
  935. if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_A64)
  936. *aspace |= VME_A64;
  937. if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_CRCSR)
  938. *aspace |= VME_CRCSR;
  939. if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_USER1)
  940. *aspace |= VME_USER1;
  941. if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_USER2)
  942. *aspace |= VME_USER2;
  943. if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_USER3)
  944. *aspace |= VME_USER3;
  945. if ((ctl & TSI148_LCSR_OTAT_AMODE_M) == TSI148_LCSR_OTAT_AMODE_USER4)
  946. *aspace |= VME_USER4;
  947. /* Setup 2eSST speeds */
  948. if ((ctl & TSI148_LCSR_OTAT_2eSSTM_M) == TSI148_LCSR_OTAT_2eSSTM_160)
  949. *cycle |= VME_2eSST160;
  950. if ((ctl & TSI148_LCSR_OTAT_2eSSTM_M) == TSI148_LCSR_OTAT_2eSSTM_267)
  951. *cycle |= VME_2eSST267;
  952. if ((ctl & TSI148_LCSR_OTAT_2eSSTM_M) == TSI148_LCSR_OTAT_2eSSTM_320)
  953. *cycle |= VME_2eSST320;
  954. /* Setup cycle types */
  955. if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_SCT)
  956. *cycle |= VME_SCT;
  957. if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_BLT)
  958. *cycle |= VME_BLT;
  959. if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_MBLT)
  960. *cycle |= VME_MBLT;
  961. if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_2eVME)
  962. *cycle |= VME_2eVME;
  963. if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_2eSST)
  964. *cycle |= VME_2eSST;
  965. if ((ctl & TSI148_LCSR_OTAT_TM_M) == TSI148_LCSR_OTAT_TM_2eSSTB)
  966. *cycle |= VME_2eSSTB;
  967. if (ctl & TSI148_LCSR_OTAT_SUP)
  968. *cycle |= VME_SUPER;
  969. else
  970. *cycle |= VME_USER;
  971. if (ctl & TSI148_LCSR_OTAT_PGM)
  972. *cycle |= VME_PROG;
  973. else
  974. *cycle |= VME_DATA;
  975. /* Setup data width */
  976. if ((ctl & TSI148_LCSR_OTAT_DBW_M) == TSI148_LCSR_OTAT_DBW_16)
  977. *dwidth = VME_D16;
  978. if ((ctl & TSI148_LCSR_OTAT_DBW_M) == TSI148_LCSR_OTAT_DBW_32)
  979. *dwidth = VME_D32;
  980. return 0;
  981. }
  982. static int tsi148_master_get(struct vme_master_resource *image, int *enabled,
  983. unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
  984. u32 *cycle, u32 *dwidth)
  985. {
  986. int retval;
  987. spin_lock(&image->lock);
  988. retval = __tsi148_master_get(image, enabled, vme_base, size, aspace,
  989. cycle, dwidth);
  990. spin_unlock(&image->lock);
  991. return retval;
  992. }
  993. static ssize_t tsi148_master_read(struct vme_master_resource *image, void *buf,
  994. size_t count, loff_t offset)
  995. {
  996. int retval, enabled;
  997. unsigned long long vme_base, size;
  998. u32 aspace, cycle, dwidth;
  999. struct vme_error_handler *handler = NULL;
  1000. struct vme_bridge *tsi148_bridge;
  1001. void __iomem *addr = image->kern_base + offset;
  1002. unsigned int done = 0;
  1003. unsigned int count32;
  1004. tsi148_bridge = image->parent;
  1005. spin_lock(&image->lock);
  1006. if (err_chk) {
  1007. __tsi148_master_get(image, &enabled, &vme_base, &size, &aspace,
  1008. &cycle, &dwidth);
  1009. handler = vme_register_error_handler(tsi148_bridge, aspace,
  1010. vme_base + offset, count);
  1011. if (!handler) {
  1012. spin_unlock(&image->lock);
  1013. return -ENOMEM;
  1014. }
  1015. }
  1016. /* The following code handles VME address alignment. We cannot use
  1017. * memcpy_xxx here because it may cut data transfers in to 8-bit
  1018. * cycles when D16 or D32 cycles are required on the VME bus.
  1019. * On the other hand, the bridge itself assures that the maximum data
  1020. * cycle configured for the transfer is used and splits it
  1021. * automatically for non-aligned addresses, so we don't want the
  1022. * overhead of needlessly forcing small transfers for the entire cycle.
  1023. */
  1024. if ((uintptr_t)addr & 0x1) {
  1025. *(u8 *)buf = ioread8(addr);
  1026. done += 1;
  1027. if (done == count)
  1028. goto out;
  1029. }
  1030. if ((uintptr_t)(addr + done) & 0x2) {
  1031. if ((count - done) < 2) {
  1032. *(u8 *)(buf + done) = ioread8(addr + done);
  1033. done += 1;
  1034. goto out;
  1035. } else {
  1036. *(u16 *)(buf + done) = ioread16(addr + done);
  1037. done += 2;
  1038. }
  1039. }
  1040. count32 = (count - done) & ~0x3;
  1041. while (done < count32) {
  1042. *(u32 *)(buf + done) = ioread32(addr + done);
  1043. done += 4;
  1044. }
  1045. if ((count - done) & 0x2) {
  1046. *(u16 *)(buf + done) = ioread16(addr + done);
  1047. done += 2;
  1048. }
  1049. if ((count - done) & 0x1) {
  1050. *(u8 *)(buf + done) = ioread8(addr + done);
  1051. done += 1;
  1052. }
  1053. out:
  1054. retval = count;
  1055. if (err_chk) {
  1056. if (handler->num_errors) {
  1057. dev_err(image->parent->parent,
  1058. "First VME read error detected an at address 0x%llx\n",
  1059. handler->first_error);
  1060. retval = handler->first_error - (vme_base + offset);
  1061. }
  1062. vme_unregister_error_handler(handler);
  1063. }
  1064. spin_unlock(&image->lock);
  1065. return retval;
  1066. }
  1067. static ssize_t tsi148_master_write(struct vme_master_resource *image, void *buf,
  1068. size_t count, loff_t offset)
  1069. {
  1070. int retval = 0, enabled;
  1071. unsigned long long vme_base, size;
  1072. u32 aspace, cycle, dwidth;
  1073. void __iomem *addr = image->kern_base + offset;
  1074. unsigned int done = 0;
  1075. unsigned int count32;
  1076. struct vme_error_handler *handler = NULL;
  1077. struct vme_bridge *tsi148_bridge;
  1078. struct tsi148_driver *bridge;
  1079. tsi148_bridge = image->parent;
  1080. bridge = tsi148_bridge->driver_priv;
  1081. spin_lock(&image->lock);
  1082. if (err_chk) {
  1083. __tsi148_master_get(image, &enabled, &vme_base, &size, &aspace,
  1084. &cycle, &dwidth);
  1085. handler = vme_register_error_handler(tsi148_bridge, aspace,
  1086. vme_base + offset, count);
  1087. if (!handler) {
  1088. spin_unlock(&image->lock);
  1089. return -ENOMEM;
  1090. }
  1091. }
  1092. /* Here we apply for the same strategy we do in master_read
  1093. * function in order to assure the correct cycles.
  1094. */
  1095. if ((uintptr_t)addr & 0x1) {
  1096. iowrite8(*(u8 *)buf, addr);
  1097. done += 1;
  1098. if (done == count)
  1099. goto out;
  1100. }
  1101. if ((uintptr_t)(addr + done) & 0x2) {
  1102. if ((count - done) < 2) {
  1103. iowrite8(*(u8 *)(buf + done), addr + done);
  1104. done += 1;
  1105. goto out;
  1106. } else {
  1107. iowrite16(*(u16 *)(buf + done), addr + done);
  1108. done += 2;
  1109. }
  1110. }
  1111. count32 = (count - done) & ~0x3;
  1112. while (done < count32) {
  1113. iowrite32(*(u32 *)(buf + done), addr + done);
  1114. done += 4;
  1115. }
  1116. if ((count - done) & 0x2) {
  1117. iowrite16(*(u16 *)(buf + done), addr + done);
  1118. done += 2;
  1119. }
  1120. if ((count - done) & 0x1) {
  1121. iowrite8(*(u8 *)(buf + done), addr + done);
  1122. done += 1;
  1123. }
  1124. out:
  1125. retval = count;
  1126. /*
  1127. * Writes are posted. We need to do a read on the VME bus to flush out
  1128. * all of the writes before we check for errors. We can't guarantee
  1129. * that reading the data we have just written is safe. It is believed
  1130. * that there isn't any read, write re-ordering, so we can read any
  1131. * location in VME space, so lets read the Device ID from the tsi148's
  1132. * own registers as mapped into CR/CSR space.
  1133. *
  1134. * We check for saved errors in the written address range/space.
  1135. */
  1136. if (err_chk) {
  1137. ioread16(bridge->flush_image->kern_base + 0x7F000);
  1138. if (handler->num_errors) {
  1139. dev_warn(tsi148_bridge->parent,
  1140. "First VME write error detected an at address 0x%llx\n",
  1141. handler->first_error);
  1142. retval = handler->first_error - (vme_base + offset);
  1143. }
  1144. vme_unregister_error_handler(handler);
  1145. }
  1146. spin_unlock(&image->lock);
  1147. return retval;
  1148. }
  1149. /*
  1150. * Perform an RMW cycle on the VME bus.
  1151. *
  1152. * Requires a previously configured master window, returns final value.
  1153. */
  1154. static unsigned int tsi148_master_rmw(struct vme_master_resource *image,
  1155. unsigned int mask, unsigned int compare, unsigned int swap,
  1156. loff_t offset)
  1157. {
  1158. unsigned long long pci_addr;
  1159. unsigned int pci_addr_high, pci_addr_low;
  1160. u32 tmp, result;
  1161. int i;
  1162. struct tsi148_driver *bridge;
  1163. bridge = image->parent->driver_priv;
  1164. /* Find the PCI address that maps to the desired VME address */
  1165. i = image->number;
  1166. /* Locking as we can only do one of these at a time */
  1167. mutex_lock(&bridge->vme_rmw);
  1168. /* Lock image */
  1169. spin_lock(&image->lock);
  1170. pci_addr_high = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
  1171. TSI148_LCSR_OFFSET_OTSAU);
  1172. pci_addr_low = ioread32be(bridge->base + TSI148_LCSR_OT[i] +
  1173. TSI148_LCSR_OFFSET_OTSAL);
  1174. reg_join(pci_addr_high, pci_addr_low, &pci_addr);
  1175. reg_split(pci_addr + offset, &pci_addr_high, &pci_addr_low);
  1176. /* Configure registers */
  1177. iowrite32be(mask, bridge->base + TSI148_LCSR_RMWEN);
  1178. iowrite32be(compare, bridge->base + TSI148_LCSR_RMWC);
  1179. iowrite32be(swap, bridge->base + TSI148_LCSR_RMWS);
  1180. iowrite32be(pci_addr_high, bridge->base + TSI148_LCSR_RMWAU);
  1181. iowrite32be(pci_addr_low, bridge->base + TSI148_LCSR_RMWAL);
  1182. /* Enable RMW */
  1183. tmp = ioread32be(bridge->base + TSI148_LCSR_VMCTRL);
  1184. tmp |= TSI148_LCSR_VMCTRL_RMWEN;
  1185. iowrite32be(tmp, bridge->base + TSI148_LCSR_VMCTRL);
  1186. /* Kick process off with a read to the required address. */
  1187. result = ioread32be(image->kern_base + offset);
  1188. /* Disable RMW */
  1189. tmp = ioread32be(bridge->base + TSI148_LCSR_VMCTRL);
  1190. tmp &= ~TSI148_LCSR_VMCTRL_RMWEN;
  1191. iowrite32be(tmp, bridge->base + TSI148_LCSR_VMCTRL);
  1192. spin_unlock(&image->lock);
  1193. mutex_unlock(&bridge->vme_rmw);
  1194. return result;
  1195. }
  1196. static int tsi148_dma_set_vme_src_attributes(struct device *dev, __be32 *attr,
  1197. u32 aspace, u32 cycle, u32 dwidth)
  1198. {
  1199. u32 val;
  1200. val = be32_to_cpu(*attr);
  1201. /* Setup 2eSST speeds */
  1202. switch (cycle & (VME_2eSST160 | VME_2eSST267 | VME_2eSST320)) {
  1203. case VME_2eSST160:
  1204. val |= TSI148_LCSR_DSAT_2eSSTM_160;
  1205. break;
  1206. case VME_2eSST267:
  1207. val |= TSI148_LCSR_DSAT_2eSSTM_267;
  1208. break;
  1209. case VME_2eSST320:
  1210. val |= TSI148_LCSR_DSAT_2eSSTM_320;
  1211. break;
  1212. }
  1213. /* Setup cycle types */
  1214. if (cycle & VME_SCT)
  1215. val |= TSI148_LCSR_DSAT_TM_SCT;
  1216. if (cycle & VME_BLT)
  1217. val |= TSI148_LCSR_DSAT_TM_BLT;
  1218. if (cycle & VME_MBLT)
  1219. val |= TSI148_LCSR_DSAT_TM_MBLT;
  1220. if (cycle & VME_2eVME)
  1221. val |= TSI148_LCSR_DSAT_TM_2eVME;
  1222. if (cycle & VME_2eSST)
  1223. val |= TSI148_LCSR_DSAT_TM_2eSST;
  1224. if (cycle & VME_2eSSTB) {
  1225. dev_err(dev, "Currently not setting Broadcast Select "
  1226. "Registers\n");
  1227. val |= TSI148_LCSR_DSAT_TM_2eSSTB;
  1228. }
  1229. /* Setup data width */
  1230. switch (dwidth) {
  1231. case VME_D16:
  1232. val |= TSI148_LCSR_DSAT_DBW_16;
  1233. break;
  1234. case VME_D32:
  1235. val |= TSI148_LCSR_DSAT_DBW_32;
  1236. break;
  1237. default:
  1238. dev_err(dev, "Invalid data width\n");
  1239. return -EINVAL;
  1240. }
  1241. /* Setup address space */
  1242. switch (aspace) {
  1243. case VME_A16:
  1244. val |= TSI148_LCSR_DSAT_AMODE_A16;
  1245. break;
  1246. case VME_A24:
  1247. val |= TSI148_LCSR_DSAT_AMODE_A24;
  1248. break;
  1249. case VME_A32:
  1250. val |= TSI148_LCSR_DSAT_AMODE_A32;
  1251. break;
  1252. case VME_A64:
  1253. val |= TSI148_LCSR_DSAT_AMODE_A64;
  1254. break;
  1255. case VME_CRCSR:
  1256. val |= TSI148_LCSR_DSAT_AMODE_CRCSR;
  1257. break;
  1258. case VME_USER1:
  1259. val |= TSI148_LCSR_DSAT_AMODE_USER1;
  1260. break;
  1261. case VME_USER2:
  1262. val |= TSI148_LCSR_DSAT_AMODE_USER2;
  1263. break;
  1264. case VME_USER3:
  1265. val |= TSI148_LCSR_DSAT_AMODE_USER3;
  1266. break;
  1267. case VME_USER4:
  1268. val |= TSI148_LCSR_DSAT_AMODE_USER4;
  1269. break;
  1270. default:
  1271. dev_err(dev, "Invalid address space\n");
  1272. return -EINVAL;
  1273. break;
  1274. }
  1275. if (cycle & VME_SUPER)
  1276. val |= TSI148_LCSR_DSAT_SUP;
  1277. if (cycle & VME_PROG)
  1278. val |= TSI148_LCSR_DSAT_PGM;
  1279. *attr = cpu_to_be32(val);
  1280. return 0;
  1281. }
  1282. static int tsi148_dma_set_vme_dest_attributes(struct device *dev, __be32 *attr,
  1283. u32 aspace, u32 cycle, u32 dwidth)
  1284. {
  1285. u32 val;
  1286. val = be32_to_cpu(*attr);
  1287. /* Setup 2eSST speeds */
  1288. switch (cycle & (VME_2eSST160 | VME_2eSST267 | VME_2eSST320)) {
  1289. case VME_2eSST160:
  1290. val |= TSI148_LCSR_DDAT_2eSSTM_160;
  1291. break;
  1292. case VME_2eSST267:
  1293. val |= TSI148_LCSR_DDAT_2eSSTM_267;
  1294. break;
  1295. case VME_2eSST320:
  1296. val |= TSI148_LCSR_DDAT_2eSSTM_320;
  1297. break;
  1298. }
  1299. /* Setup cycle types */
  1300. if (cycle & VME_SCT)
  1301. val |= TSI148_LCSR_DDAT_TM_SCT;
  1302. if (cycle & VME_BLT)
  1303. val |= TSI148_LCSR_DDAT_TM_BLT;
  1304. if (cycle & VME_MBLT)
  1305. val |= TSI148_LCSR_DDAT_TM_MBLT;
  1306. if (cycle & VME_2eVME)
  1307. val |= TSI148_LCSR_DDAT_TM_2eVME;
  1308. if (cycle & VME_2eSST)
  1309. val |= TSI148_LCSR_DDAT_TM_2eSST;
  1310. if (cycle & VME_2eSSTB) {
  1311. dev_err(dev, "Currently not setting Broadcast Select "
  1312. "Registers\n");
  1313. val |= TSI148_LCSR_DDAT_TM_2eSSTB;
  1314. }
  1315. /* Setup data width */
  1316. switch (dwidth) {
  1317. case VME_D16:
  1318. val |= TSI148_LCSR_DDAT_DBW_16;
  1319. break;
  1320. case VME_D32:
  1321. val |= TSI148_LCSR_DDAT_DBW_32;
  1322. break;
  1323. default:
  1324. dev_err(dev, "Invalid data width\n");
  1325. return -EINVAL;
  1326. }
  1327. /* Setup address space */
  1328. switch (aspace) {
  1329. case VME_A16:
  1330. val |= TSI148_LCSR_DDAT_AMODE_A16;
  1331. break;
  1332. case VME_A24:
  1333. val |= TSI148_LCSR_DDAT_AMODE_A24;
  1334. break;
  1335. case VME_A32:
  1336. val |= TSI148_LCSR_DDAT_AMODE_A32;
  1337. break;
  1338. case VME_A64:
  1339. val |= TSI148_LCSR_DDAT_AMODE_A64;
  1340. break;
  1341. case VME_CRCSR:
  1342. val |= TSI148_LCSR_DDAT_AMODE_CRCSR;
  1343. break;
  1344. case VME_USER1:
  1345. val |= TSI148_LCSR_DDAT_AMODE_USER1;
  1346. break;
  1347. case VME_USER2:
  1348. val |= TSI148_LCSR_DDAT_AMODE_USER2;
  1349. break;
  1350. case VME_USER3:
  1351. val |= TSI148_LCSR_DDAT_AMODE_USER3;
  1352. break;
  1353. case VME_USER4:
  1354. val |= TSI148_LCSR_DDAT_AMODE_USER4;
  1355. break;
  1356. default:
  1357. dev_err(dev, "Invalid address space\n");
  1358. return -EINVAL;
  1359. break;
  1360. }
  1361. if (cycle & VME_SUPER)
  1362. val |= TSI148_LCSR_DDAT_SUP;
  1363. if (cycle & VME_PROG)
  1364. val |= TSI148_LCSR_DDAT_PGM;
  1365. *attr = cpu_to_be32(val);
  1366. return 0;
  1367. }
  1368. /*
  1369. * Add a link list descriptor to the list
  1370. *
  1371. * Note: DMA engine expects the DMA descriptor to be big endian.
  1372. */
  1373. static int tsi148_dma_list_add(struct vme_dma_list *list,
  1374. struct vme_dma_attr *src, struct vme_dma_attr *dest, size_t count)
  1375. {
  1376. struct tsi148_dma_entry *entry, *prev;
  1377. u32 address_high, address_low, val;
  1378. struct vme_dma_pattern *pattern_attr;
  1379. struct vme_dma_pci *pci_attr;
  1380. struct vme_dma_vme *vme_attr;
  1381. int retval = 0;
  1382. struct vme_bridge *tsi148_bridge;
  1383. tsi148_bridge = list->parent->parent;
  1384. /* Descriptor must be aligned on 64-bit boundaries */
  1385. entry = kmalloc(sizeof(struct tsi148_dma_entry), GFP_KERNEL);
  1386. if (entry == NULL) {
  1387. dev_err(tsi148_bridge->parent, "Failed to allocate memory for "
  1388. "dma resource structure\n");
  1389. retval = -ENOMEM;
  1390. goto err_mem;
  1391. }
  1392. /* Test descriptor alignment */
  1393. if ((unsigned long)&entry->descriptor & 0x7) {
  1394. dev_err(tsi148_bridge->parent, "Descriptor not aligned to 8 "
  1395. "byte boundary as required: %p\n",
  1396. &entry->descriptor);
  1397. retval = -EINVAL;
  1398. goto err_align;
  1399. }
  1400. /* Given we are going to fill out the structure, we probably don't
  1401. * need to zero it, but better safe than sorry for now.
  1402. */
  1403. memset(&entry->descriptor, 0, sizeof(struct tsi148_dma_descriptor));
  1404. /* Fill out source part */
  1405. switch (src->type) {
  1406. case VME_DMA_PATTERN:
  1407. pattern_attr = src->private;
  1408. entry->descriptor.dsal = cpu_to_be32(pattern_attr->pattern);
  1409. val = TSI148_LCSR_DSAT_TYP_PAT;
  1410. /* Default behaviour is 32 bit pattern */
  1411. if (pattern_attr->type & VME_DMA_PATTERN_BYTE)
  1412. val |= TSI148_LCSR_DSAT_PSZ;
  1413. /* It seems that the default behaviour is to increment */
  1414. if ((pattern_attr->type & VME_DMA_PATTERN_INCREMENT) == 0)
  1415. val |= TSI148_LCSR_DSAT_NIN;
  1416. entry->descriptor.dsat = cpu_to_be32(val);
  1417. break;
  1418. case VME_DMA_PCI:
  1419. pci_attr = src->private;
  1420. reg_split((unsigned long long)pci_attr->address, &address_high,
  1421. &address_low);
  1422. entry->descriptor.dsau = cpu_to_be32(address_high);
  1423. entry->descriptor.dsal = cpu_to_be32(address_low);
  1424. entry->descriptor.dsat = cpu_to_be32(TSI148_LCSR_DSAT_TYP_PCI);
  1425. break;
  1426. case VME_DMA_VME:
  1427. vme_attr = src->private;
  1428. reg_split((unsigned long long)vme_attr->address, &address_high,
  1429. &address_low);
  1430. entry->descriptor.dsau = cpu_to_be32(address_high);
  1431. entry->descriptor.dsal = cpu_to_be32(address_low);
  1432. entry->descriptor.dsat = cpu_to_be32(TSI148_LCSR_DSAT_TYP_VME);
  1433. retval = tsi148_dma_set_vme_src_attributes(
  1434. tsi148_bridge->parent, &entry->descriptor.dsat,
  1435. vme_attr->aspace, vme_attr->cycle, vme_attr->dwidth);
  1436. if (retval < 0)
  1437. goto err_source;
  1438. break;
  1439. default:
  1440. dev_err(tsi148_bridge->parent, "Invalid source type\n");
  1441. retval = -EINVAL;
  1442. goto err_source;
  1443. break;
  1444. }
  1445. /* Assume last link - this will be over-written by adding another */
  1446. entry->descriptor.dnlau = cpu_to_be32(0);
  1447. entry->descriptor.dnlal = cpu_to_be32(TSI148_LCSR_DNLAL_LLA);
  1448. /* Fill out destination part */
  1449. switch (dest->type) {
  1450. case VME_DMA_PCI:
  1451. pci_attr = dest->private;
  1452. reg_split((unsigned long long)pci_attr->address, &address_high,
  1453. &address_low);
  1454. entry->descriptor.ddau = cpu_to_be32(address_high);
  1455. entry->descriptor.ddal = cpu_to_be32(address_low);
  1456. entry->descriptor.ddat = cpu_to_be32(TSI148_LCSR_DDAT_TYP_PCI);
  1457. break;
  1458. case VME_DMA_VME:
  1459. vme_attr = dest->private;
  1460. reg_split((unsigned long long)vme_attr->address, &address_high,
  1461. &address_low);
  1462. entry->descriptor.ddau = cpu_to_be32(address_high);
  1463. entry->descriptor.ddal = cpu_to_be32(address_low);
  1464. entry->descriptor.ddat = cpu_to_be32(TSI148_LCSR_DDAT_TYP_VME);
  1465. retval = tsi148_dma_set_vme_dest_attributes(
  1466. tsi148_bridge->parent, &entry->descriptor.ddat,
  1467. vme_attr->aspace, vme_attr->cycle, vme_attr->dwidth);
  1468. if (retval < 0)
  1469. goto err_dest;
  1470. break;
  1471. default:
  1472. dev_err(tsi148_bridge->parent, "Invalid destination type\n");
  1473. retval = -EINVAL;
  1474. goto err_dest;
  1475. break;
  1476. }
  1477. /* Fill out count */
  1478. entry->descriptor.dcnt = cpu_to_be32((u32)count);
  1479. /* Add to list */
  1480. list_add_tail(&entry->list, &list->entries);
  1481. entry->dma_handle = dma_map_single(tsi148_bridge->parent,
  1482. &entry->descriptor,
  1483. sizeof(struct tsi148_dma_descriptor), DMA_TO_DEVICE);
  1484. if (dma_mapping_error(tsi148_bridge->parent, entry->dma_handle)) {
  1485. dev_err(tsi148_bridge->parent, "DMA mapping error\n");
  1486. retval = -EINVAL;
  1487. goto err_dma;
  1488. }
  1489. /* Fill out previous descriptors "Next Address" */
  1490. if (entry->list.prev != &list->entries) {
  1491. reg_split((unsigned long long)entry->dma_handle, &address_high,
  1492. &address_low);
  1493. prev = list_entry(entry->list.prev, struct tsi148_dma_entry,
  1494. list);
  1495. prev->descriptor.dnlau = cpu_to_be32(address_high);
  1496. prev->descriptor.dnlal = cpu_to_be32(address_low);
  1497. }
  1498. return 0;
  1499. err_dma:
  1500. err_dest:
  1501. err_source:
  1502. err_align:
  1503. kfree(entry);
  1504. err_mem:
  1505. return retval;
  1506. }
  1507. /*
  1508. * Check to see if the provided DMA channel is busy.
  1509. */
  1510. static int tsi148_dma_busy(struct vme_bridge *tsi148_bridge, int channel)
  1511. {
  1512. u32 tmp;
  1513. struct tsi148_driver *bridge;
  1514. bridge = tsi148_bridge->driver_priv;
  1515. tmp = ioread32be(bridge->base + TSI148_LCSR_DMA[channel] +
  1516. TSI148_LCSR_OFFSET_DSTA);
  1517. if (tmp & TSI148_LCSR_DSTA_BSY)
  1518. return 0;
  1519. else
  1520. return 1;
  1521. }
  1522. /*
  1523. * Execute a previously generated link list
  1524. *
  1525. * XXX Need to provide control register configuration.
  1526. */
  1527. static int tsi148_dma_list_exec(struct vme_dma_list *list)
  1528. {
  1529. struct vme_dma_resource *ctrlr;
  1530. int channel, retval;
  1531. struct tsi148_dma_entry *entry;
  1532. u32 bus_addr_high, bus_addr_low;
  1533. u32 val, dctlreg = 0;
  1534. struct vme_bridge *tsi148_bridge;
  1535. struct tsi148_driver *bridge;
  1536. ctrlr = list->parent;
  1537. tsi148_bridge = ctrlr->parent;
  1538. bridge = tsi148_bridge->driver_priv;
  1539. mutex_lock(&ctrlr->mtx);
  1540. channel = ctrlr->number;
  1541. if (!list_empty(&ctrlr->running)) {
  1542. /*
  1543. * XXX We have an active DMA transfer and currently haven't
  1544. * sorted out the mechanism for "pending" DMA transfers.
  1545. * Return busy.
  1546. */
  1547. /* Need to add to pending here */
  1548. mutex_unlock(&ctrlr->mtx);
  1549. return -EBUSY;
  1550. } else {
  1551. list_add(&list->list, &ctrlr->running);
  1552. }
  1553. /* Get first bus address and write into registers */
  1554. entry = list_first_entry(&list->entries, struct tsi148_dma_entry,
  1555. list);
  1556. mutex_unlock(&ctrlr->mtx);
  1557. reg_split(entry->dma_handle, &bus_addr_high, &bus_addr_low);
  1558. iowrite32be(bus_addr_high, bridge->base +
  1559. TSI148_LCSR_DMA[channel] + TSI148_LCSR_OFFSET_DNLAU);
  1560. iowrite32be(bus_addr_low, bridge->base +
  1561. TSI148_LCSR_DMA[channel] + TSI148_LCSR_OFFSET_DNLAL);
  1562. dctlreg = ioread32be(bridge->base + TSI148_LCSR_DMA[channel] +
  1563. TSI148_LCSR_OFFSET_DCTL);
  1564. /* Start the operation */
  1565. iowrite32be(dctlreg | TSI148_LCSR_DCTL_DGO, bridge->base +
  1566. TSI148_LCSR_DMA[channel] + TSI148_LCSR_OFFSET_DCTL);
  1567. retval = wait_event_interruptible(bridge->dma_queue[channel],
  1568. tsi148_dma_busy(ctrlr->parent, channel));
  1569. if (retval) {
  1570. iowrite32be(dctlreg | TSI148_LCSR_DCTL_ABT, bridge->base +
  1571. TSI148_LCSR_DMA[channel] + TSI148_LCSR_OFFSET_DCTL);
  1572. /* Wait for the operation to abort */
  1573. wait_event(bridge->dma_queue[channel],
  1574. tsi148_dma_busy(ctrlr->parent, channel));
  1575. retval = -EINTR;
  1576. goto exit;
  1577. }
  1578. /*
  1579. * Read status register, this register is valid until we kick off a
  1580. * new transfer.
  1581. */
  1582. val = ioread32be(bridge->base + TSI148_LCSR_DMA[channel] +
  1583. TSI148_LCSR_OFFSET_DSTA);
  1584. if (val & TSI148_LCSR_DSTA_VBE) {
  1585. dev_err(tsi148_bridge->parent, "DMA Error. DSTA=%08X\n", val);
  1586. retval = -EIO;
  1587. }
  1588. exit:
  1589. /* Remove list from running list */
  1590. mutex_lock(&ctrlr->mtx);
  1591. list_del(&list->list);
  1592. mutex_unlock(&ctrlr->mtx);
  1593. return retval;
  1594. }
  1595. /*
  1596. * Clean up a previously generated link list
  1597. *
  1598. * We have a separate function, don't assume that the chain can't be reused.
  1599. */
  1600. static int tsi148_dma_list_empty(struct vme_dma_list *list)
  1601. {
  1602. struct list_head *pos, *temp;
  1603. struct tsi148_dma_entry *entry;
  1604. struct vme_bridge *tsi148_bridge = list->parent->parent;
  1605. /* detach and free each entry */
  1606. list_for_each_safe(pos, temp, &list->entries) {
  1607. list_del(pos);
  1608. entry = list_entry(pos, struct tsi148_dma_entry, list);
  1609. dma_unmap_single(tsi148_bridge->parent, entry->dma_handle,
  1610. sizeof(struct tsi148_dma_descriptor), DMA_TO_DEVICE);
  1611. kfree(entry);
  1612. }
  1613. return 0;
  1614. }
  1615. /*
  1616. * All 4 location monitors reside at the same base - this is therefore a
  1617. * system wide configuration.
  1618. *
  1619. * This does not enable the LM monitor - that should be done when the first
  1620. * callback is attached and disabled when the last callback is removed.
  1621. */
  1622. static int tsi148_lm_set(struct vme_lm_resource *lm, unsigned long long lm_base,
  1623. u32 aspace, u32 cycle)
  1624. {
  1625. u32 lm_base_high, lm_base_low, lm_ctl = 0;
  1626. int i;
  1627. struct vme_bridge *tsi148_bridge;
  1628. struct tsi148_driver *bridge;
  1629. tsi148_bridge = lm->parent;
  1630. bridge = tsi148_bridge->driver_priv;
  1631. mutex_lock(&lm->mtx);
  1632. /* If we already have a callback attached, we can't move it! */
  1633. for (i = 0; i < lm->monitors; i++) {
  1634. if (bridge->lm_callback[i] != NULL) {
  1635. mutex_unlock(&lm->mtx);
  1636. dev_err(tsi148_bridge->parent, "Location monitor "
  1637. "callback attached, can't reset\n");
  1638. return -EBUSY;
  1639. }
  1640. }
  1641. switch (aspace) {
  1642. case VME_A16:
  1643. lm_ctl |= TSI148_LCSR_LMAT_AS_A16;
  1644. break;
  1645. case VME_A24:
  1646. lm_ctl |= TSI148_LCSR_LMAT_AS_A24;
  1647. break;
  1648. case VME_A32:
  1649. lm_ctl |= TSI148_LCSR_LMAT_AS_A32;
  1650. break;
  1651. case VME_A64:
  1652. lm_ctl |= TSI148_LCSR_LMAT_AS_A64;
  1653. break;
  1654. default:
  1655. mutex_unlock(&lm->mtx);
  1656. dev_err(tsi148_bridge->parent, "Invalid address space\n");
  1657. return -EINVAL;
  1658. break;
  1659. }
  1660. if (cycle & VME_SUPER)
  1661. lm_ctl |= TSI148_LCSR_LMAT_SUPR ;
  1662. if (cycle & VME_USER)
  1663. lm_ctl |= TSI148_LCSR_LMAT_NPRIV;
  1664. if (cycle & VME_PROG)
  1665. lm_ctl |= TSI148_LCSR_LMAT_PGM;
  1666. if (cycle & VME_DATA)
  1667. lm_ctl |= TSI148_LCSR_LMAT_DATA;
  1668. reg_split(lm_base, &lm_base_high, &lm_base_low);
  1669. iowrite32be(lm_base_high, bridge->base + TSI148_LCSR_LMBAU);
  1670. iowrite32be(lm_base_low, bridge->base + TSI148_LCSR_LMBAL);
  1671. iowrite32be(lm_ctl, bridge->base + TSI148_LCSR_LMAT);
  1672. mutex_unlock(&lm->mtx);
  1673. return 0;
  1674. }
  1675. /* Get configuration of the callback monitor and return whether it is enabled
  1676. * or disabled.
  1677. */
  1678. static int tsi148_lm_get(struct vme_lm_resource *lm,
  1679. unsigned long long *lm_base, u32 *aspace, u32 *cycle)
  1680. {
  1681. u32 lm_base_high, lm_base_low, lm_ctl, enabled = 0;
  1682. struct tsi148_driver *bridge;
  1683. bridge = lm->parent->driver_priv;
  1684. mutex_lock(&lm->mtx);
  1685. lm_base_high = ioread32be(bridge->base + TSI148_LCSR_LMBAU);
  1686. lm_base_low = ioread32be(bridge->base + TSI148_LCSR_LMBAL);
  1687. lm_ctl = ioread32be(bridge->base + TSI148_LCSR_LMAT);
  1688. reg_join(lm_base_high, lm_base_low, lm_base);
  1689. if (lm_ctl & TSI148_LCSR_LMAT_EN)
  1690. enabled = 1;
  1691. if ((lm_ctl & TSI148_LCSR_LMAT_AS_M) == TSI148_LCSR_LMAT_AS_A16)
  1692. *aspace |= VME_A16;
  1693. if ((lm_ctl & TSI148_LCSR_LMAT_AS_M) == TSI148_LCSR_LMAT_AS_A24)
  1694. *aspace |= VME_A24;
  1695. if ((lm_ctl & TSI148_LCSR_LMAT_AS_M) == TSI148_LCSR_LMAT_AS_A32)
  1696. *aspace |= VME_A32;
  1697. if ((lm_ctl & TSI148_LCSR_LMAT_AS_M) == TSI148_LCSR_LMAT_AS_A64)
  1698. *aspace |= VME_A64;
  1699. if (lm_ctl & TSI148_LCSR_LMAT_SUPR)
  1700. *cycle |= VME_SUPER;
  1701. if (lm_ctl & TSI148_LCSR_LMAT_NPRIV)
  1702. *cycle |= VME_USER;
  1703. if (lm_ctl & TSI148_LCSR_LMAT_PGM)
  1704. *cycle |= VME_PROG;
  1705. if (lm_ctl & TSI148_LCSR_LMAT_DATA)
  1706. *cycle |= VME_DATA;
  1707. mutex_unlock(&lm->mtx);
  1708. return enabled;
  1709. }
  1710. /*
  1711. * Attach a callback to a specific location monitor.
  1712. *
  1713. * Callback will be passed the monitor triggered.
  1714. */
  1715. static int tsi148_lm_attach(struct vme_lm_resource *lm, int monitor,
  1716. void (*callback)(void *), void *data)
  1717. {
  1718. u32 lm_ctl, tmp;
  1719. struct vme_bridge *tsi148_bridge;
  1720. struct tsi148_driver *bridge;
  1721. tsi148_bridge = lm->parent;
  1722. bridge = tsi148_bridge->driver_priv;
  1723. mutex_lock(&lm->mtx);
  1724. /* Ensure that the location monitor is configured - need PGM or DATA */
  1725. lm_ctl = ioread32be(bridge->base + TSI148_LCSR_LMAT);
  1726. if ((lm_ctl & (TSI148_LCSR_LMAT_PGM | TSI148_LCSR_LMAT_DATA)) == 0) {
  1727. mutex_unlock(&lm->mtx);
  1728. dev_err(tsi148_bridge->parent, "Location monitor not properly "
  1729. "configured\n");
  1730. return -EINVAL;
  1731. }
  1732. /* Check that a callback isn't already attached */
  1733. if (bridge->lm_callback[monitor] != NULL) {
  1734. mutex_unlock(&lm->mtx);
  1735. dev_err(tsi148_bridge->parent, "Existing callback attached\n");
  1736. return -EBUSY;
  1737. }
  1738. /* Attach callback */
  1739. bridge->lm_callback[monitor] = callback;
  1740. bridge->lm_data[monitor] = data;
  1741. /* Enable Location Monitor interrupt */
  1742. tmp = ioread32be(bridge->base + TSI148_LCSR_INTEN);
  1743. tmp |= TSI148_LCSR_INTEN_LMEN[monitor];
  1744. iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEN);
  1745. tmp = ioread32be(bridge->base + TSI148_LCSR_INTEO);
  1746. tmp |= TSI148_LCSR_INTEO_LMEO[monitor];
  1747. iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
  1748. /* Ensure that global Location Monitor Enable set */
  1749. if ((lm_ctl & TSI148_LCSR_LMAT_EN) == 0) {
  1750. lm_ctl |= TSI148_LCSR_LMAT_EN;
  1751. iowrite32be(lm_ctl, bridge->base + TSI148_LCSR_LMAT);
  1752. }
  1753. mutex_unlock(&lm->mtx);
  1754. return 0;
  1755. }
  1756. /*
  1757. * Detach a callback function forn a specific location monitor.
  1758. */
  1759. static int tsi148_lm_detach(struct vme_lm_resource *lm, int monitor)
  1760. {
  1761. u32 lm_en, tmp;
  1762. struct tsi148_driver *bridge;
  1763. bridge = lm->parent->driver_priv;
  1764. mutex_lock(&lm->mtx);
  1765. /* Disable Location Monitor and ensure previous interrupts are clear */
  1766. lm_en = ioread32be(bridge->base + TSI148_LCSR_INTEN);
  1767. lm_en &= ~TSI148_LCSR_INTEN_LMEN[monitor];
  1768. iowrite32be(lm_en, bridge->base + TSI148_LCSR_INTEN);
  1769. tmp = ioread32be(bridge->base + TSI148_LCSR_INTEO);
  1770. tmp &= ~TSI148_LCSR_INTEO_LMEO[monitor];
  1771. iowrite32be(tmp, bridge->base + TSI148_LCSR_INTEO);
  1772. iowrite32be(TSI148_LCSR_INTC_LMC[monitor],
  1773. bridge->base + TSI148_LCSR_INTC);
  1774. /* Detach callback */
  1775. bridge->lm_callback[monitor] = NULL;
  1776. bridge->lm_data[monitor] = NULL;
  1777. /* If all location monitors disabled, disable global Location Monitor */
  1778. if ((lm_en & (TSI148_LCSR_INTS_LM0S | TSI148_LCSR_INTS_LM1S |
  1779. TSI148_LCSR_INTS_LM2S | TSI148_LCSR_INTS_LM3S)) == 0) {
  1780. tmp = ioread32be(bridge->base + TSI148_LCSR_LMAT);
  1781. tmp &= ~TSI148_LCSR_LMAT_EN;
  1782. iowrite32be(tmp, bridge->base + TSI148_LCSR_LMAT);
  1783. }
  1784. mutex_unlock(&lm->mtx);
  1785. return 0;
  1786. }
  1787. /*
  1788. * Determine Geographical Addressing
  1789. */
  1790. static int tsi148_slot_get(struct vme_bridge *tsi148_bridge)
  1791. {
  1792. u32 slot = 0;
  1793. struct tsi148_driver *bridge;
  1794. bridge = tsi148_bridge->driver_priv;
  1795. if (!geoid) {
  1796. slot = ioread32be(bridge->base + TSI148_LCSR_VSTAT);
  1797. slot = slot & TSI148_LCSR_VSTAT_GA_M;
  1798. } else
  1799. slot = geoid;
  1800. return (int)slot;
  1801. }
  1802. static void *tsi148_alloc_consistent(struct device *parent, size_t size,
  1803. dma_addr_t *dma)
  1804. {
  1805. struct pci_dev *pdev;
  1806. /* Find pci_dev container of dev */
  1807. pdev = to_pci_dev(parent);
  1808. return pci_alloc_consistent(pdev, size, dma);
  1809. }
  1810. static void tsi148_free_consistent(struct device *parent, size_t size,
  1811. void *vaddr, dma_addr_t dma)
  1812. {
  1813. struct pci_dev *pdev;
  1814. /* Find pci_dev container of dev */
  1815. pdev = to_pci_dev(parent);
  1816. pci_free_consistent(pdev, size, vaddr, dma);
  1817. }
  1818. /*
  1819. * Configure CR/CSR space
  1820. *
  1821. * Access to the CR/CSR can be configured at power-up. The location of the
  1822. * CR/CSR registers in the CR/CSR address space is determined by the boards
  1823. * Auto-ID or Geographic address. This function ensures that the window is
  1824. * enabled at an offset consistent with the boards geopgraphic address.
  1825. *
  1826. * Each board has a 512kB window, with the highest 4kB being used for the
  1827. * boards registers, this means there is a fix length 508kB window which must
  1828. * be mapped onto PCI memory.
  1829. */
  1830. static int tsi148_crcsr_init(struct vme_bridge *tsi148_bridge,
  1831. struct pci_dev *pdev)
  1832. {
  1833. u32 cbar, crat, vstat;
  1834. u32 crcsr_bus_high, crcsr_bus_low;
  1835. int retval;
  1836. struct tsi148_driver *bridge;
  1837. bridge = tsi148_bridge->driver_priv;
  1838. /* Allocate mem for CR/CSR image */
  1839. bridge->crcsr_kernel = pci_zalloc_consistent(pdev, VME_CRCSR_BUF_SIZE,
  1840. &bridge->crcsr_bus);
  1841. if (bridge->crcsr_kernel == NULL) {
  1842. dev_err(tsi148_bridge->parent, "Failed to allocate memory for "
  1843. "CR/CSR image\n");
  1844. return -ENOMEM;
  1845. }
  1846. reg_split(bridge->crcsr_bus, &crcsr_bus_high, &crcsr_bus_low);
  1847. iowrite32be(crcsr_bus_high, bridge->base + TSI148_LCSR_CROU);
  1848. iowrite32be(crcsr_bus_low, bridge->base + TSI148_LCSR_CROL);
  1849. /* Ensure that the CR/CSR is configured at the correct offset */
  1850. cbar = ioread32be(bridge->base + TSI148_CBAR);
  1851. cbar = (cbar & TSI148_CRCSR_CBAR_M)>>3;
  1852. vstat = tsi148_slot_get(tsi148_bridge);
  1853. if (cbar != vstat) {
  1854. cbar = vstat;
  1855. dev_info(tsi148_bridge->parent, "Setting CR/CSR offset\n");
  1856. iowrite32be(cbar<<3, bridge->base + TSI148_CBAR);
  1857. }
  1858. dev_info(tsi148_bridge->parent, "CR/CSR Offset: %d\n", cbar);
  1859. crat = ioread32be(bridge->base + TSI148_LCSR_CRAT);
  1860. if (crat & TSI148_LCSR_CRAT_EN)
  1861. dev_info(tsi148_bridge->parent, "CR/CSR already enabled\n");
  1862. else {
  1863. dev_info(tsi148_bridge->parent, "Enabling CR/CSR space\n");
  1864. iowrite32be(crat | TSI148_LCSR_CRAT_EN,
  1865. bridge->base + TSI148_LCSR_CRAT);
  1866. }
  1867. /* If we want flushed, error-checked writes, set up a window
  1868. * over the CR/CSR registers. We read from here to safely flush
  1869. * through VME writes.
  1870. */
  1871. if (err_chk) {
  1872. retval = tsi148_master_set(bridge->flush_image, 1,
  1873. (vstat * 0x80000), 0x80000, VME_CRCSR, VME_SCT,
  1874. VME_D16);
  1875. if (retval)
  1876. dev_err(tsi148_bridge->parent, "Configuring flush image"
  1877. " failed\n");
  1878. }
  1879. return 0;
  1880. }
  1881. static void tsi148_crcsr_exit(struct vme_bridge *tsi148_bridge,
  1882. struct pci_dev *pdev)
  1883. {
  1884. u32 crat;
  1885. struct tsi148_driver *bridge;
  1886. bridge = tsi148_bridge->driver_priv;
  1887. /* Turn off CR/CSR space */
  1888. crat = ioread32be(bridge->base + TSI148_LCSR_CRAT);
  1889. iowrite32be(crat & ~TSI148_LCSR_CRAT_EN,
  1890. bridge->base + TSI148_LCSR_CRAT);
  1891. /* Free image */
  1892. iowrite32be(0, bridge->base + TSI148_LCSR_CROU);
  1893. iowrite32be(0, bridge->base + TSI148_LCSR_CROL);
  1894. pci_free_consistent(pdev, VME_CRCSR_BUF_SIZE, bridge->crcsr_kernel,
  1895. bridge->crcsr_bus);
  1896. }
  1897. static int tsi148_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1898. {
  1899. int retval, i, master_num;
  1900. u32 data;
  1901. struct list_head *pos = NULL, *n;
  1902. struct vme_bridge *tsi148_bridge;
  1903. struct tsi148_driver *tsi148_device;
  1904. struct vme_master_resource *master_image;
  1905. struct vme_slave_resource *slave_image;
  1906. struct vme_dma_resource *dma_ctrlr;
  1907. struct vme_lm_resource *lm;
  1908. /* If we want to support more than one of each bridge, we need to
  1909. * dynamically generate this so we get one per device
  1910. */
  1911. tsi148_bridge = kzalloc(sizeof(struct vme_bridge), GFP_KERNEL);
  1912. if (tsi148_bridge == NULL) {
  1913. dev_err(&pdev->dev, "Failed to allocate memory for device "
  1914. "structure\n");
  1915. retval = -ENOMEM;
  1916. goto err_struct;
  1917. }
  1918. vme_init_bridge(tsi148_bridge);
  1919. tsi148_device = kzalloc(sizeof(struct tsi148_driver), GFP_KERNEL);
  1920. if (tsi148_device == NULL) {
  1921. dev_err(&pdev->dev, "Failed to allocate memory for device "
  1922. "structure\n");
  1923. retval = -ENOMEM;
  1924. goto err_driver;
  1925. }
  1926. tsi148_bridge->driver_priv = tsi148_device;
  1927. /* Enable the device */
  1928. retval = pci_enable_device(pdev);
  1929. if (retval) {
  1930. dev_err(&pdev->dev, "Unable to enable device\n");
  1931. goto err_enable;
  1932. }
  1933. /* Map Registers */
  1934. retval = pci_request_regions(pdev, driver_name);
  1935. if (retval) {
  1936. dev_err(&pdev->dev, "Unable to reserve resources\n");
  1937. goto err_resource;
  1938. }
  1939. /* map registers in BAR 0 */
  1940. tsi148_device->base = ioremap_nocache(pci_resource_start(pdev, 0),
  1941. 4096);
  1942. if (!tsi148_device->base) {
  1943. dev_err(&pdev->dev, "Unable to remap CRG region\n");
  1944. retval = -EIO;
  1945. goto err_remap;
  1946. }
  1947. /* Check to see if the mapping worked out */
  1948. data = ioread32(tsi148_device->base + TSI148_PCFS_ID) & 0x0000FFFF;
  1949. if (data != PCI_VENDOR_ID_TUNDRA) {
  1950. dev_err(&pdev->dev, "CRG region check failed\n");
  1951. retval = -EIO;
  1952. goto err_test;
  1953. }
  1954. /* Initialize wait queues & mutual exclusion flags */
  1955. init_waitqueue_head(&tsi148_device->dma_queue[0]);
  1956. init_waitqueue_head(&tsi148_device->dma_queue[1]);
  1957. init_waitqueue_head(&tsi148_device->iack_queue);
  1958. mutex_init(&tsi148_device->vme_int);
  1959. mutex_init(&tsi148_device->vme_rmw);
  1960. tsi148_bridge->parent = &pdev->dev;
  1961. strcpy(tsi148_bridge->name, driver_name);
  1962. /* Setup IRQ */
  1963. retval = tsi148_irq_init(tsi148_bridge);
  1964. if (retval != 0) {
  1965. dev_err(&pdev->dev, "Chip Initialization failed.\n");
  1966. goto err_irq;
  1967. }
  1968. /* If we are going to flush writes, we need to read from the VME bus.
  1969. * We need to do this safely, thus we read the devices own CR/CSR
  1970. * register. To do this we must set up a window in CR/CSR space and
  1971. * hence have one less master window resource available.
  1972. */
  1973. master_num = TSI148_MAX_MASTER;
  1974. if (err_chk) {
  1975. master_num--;
  1976. tsi148_device->flush_image =
  1977. kmalloc(sizeof(struct vme_master_resource), GFP_KERNEL);
  1978. if (tsi148_device->flush_image == NULL) {
  1979. dev_err(&pdev->dev, "Failed to allocate memory for "
  1980. "flush resource structure\n");
  1981. retval = -ENOMEM;
  1982. goto err_master;
  1983. }
  1984. tsi148_device->flush_image->parent = tsi148_bridge;
  1985. spin_lock_init(&tsi148_device->flush_image->lock);
  1986. tsi148_device->flush_image->locked = 1;
  1987. tsi148_device->flush_image->number = master_num;
  1988. memset(&tsi148_device->flush_image->bus_resource, 0,
  1989. sizeof(struct resource));
  1990. tsi148_device->flush_image->kern_base = NULL;
  1991. }
  1992. /* Add master windows to list */
  1993. for (i = 0; i < master_num; i++) {
  1994. master_image = kmalloc(sizeof(struct vme_master_resource),
  1995. GFP_KERNEL);
  1996. if (master_image == NULL) {
  1997. dev_err(&pdev->dev, "Failed to allocate memory for "
  1998. "master resource structure\n");
  1999. retval = -ENOMEM;
  2000. goto err_master;
  2001. }
  2002. master_image->parent = tsi148_bridge;
  2003. spin_lock_init(&master_image->lock);
  2004. master_image->locked = 0;
  2005. master_image->number = i;
  2006. master_image->address_attr = VME_A16 | VME_A24 | VME_A32 |
  2007. VME_A64 | VME_CRCSR | VME_USER1 | VME_USER2 |
  2008. VME_USER3 | VME_USER4;
  2009. master_image->cycle_attr = VME_SCT | VME_BLT | VME_MBLT |
  2010. VME_2eVME | VME_2eSST | VME_2eSSTB | VME_2eSST160 |
  2011. VME_2eSST267 | VME_2eSST320 | VME_SUPER | VME_USER |
  2012. VME_PROG | VME_DATA;
  2013. master_image->width_attr = VME_D16 | VME_D32;
  2014. memset(&master_image->bus_resource, 0,
  2015. sizeof(struct resource));
  2016. master_image->kern_base = NULL;
  2017. list_add_tail(&master_image->list,
  2018. &tsi148_bridge->master_resources);
  2019. }
  2020. /* Add slave windows to list */
  2021. for (i = 0; i < TSI148_MAX_SLAVE; i++) {
  2022. slave_image = kmalloc(sizeof(struct vme_slave_resource),
  2023. GFP_KERNEL);
  2024. if (slave_image == NULL) {
  2025. dev_err(&pdev->dev, "Failed to allocate memory for "
  2026. "slave resource structure\n");
  2027. retval = -ENOMEM;
  2028. goto err_slave;
  2029. }
  2030. slave_image->parent = tsi148_bridge;
  2031. mutex_init(&slave_image->mtx);
  2032. slave_image->locked = 0;
  2033. slave_image->number = i;
  2034. slave_image->address_attr = VME_A16 | VME_A24 | VME_A32 |
  2035. VME_A64;
  2036. slave_image->cycle_attr = VME_SCT | VME_BLT | VME_MBLT |
  2037. VME_2eVME | VME_2eSST | VME_2eSSTB | VME_2eSST160 |
  2038. VME_2eSST267 | VME_2eSST320 | VME_SUPER | VME_USER |
  2039. VME_PROG | VME_DATA;
  2040. list_add_tail(&slave_image->list,
  2041. &tsi148_bridge->slave_resources);
  2042. }
  2043. /* Add dma engines to list */
  2044. for (i = 0; i < TSI148_MAX_DMA; i++) {
  2045. dma_ctrlr = kmalloc(sizeof(struct vme_dma_resource),
  2046. GFP_KERNEL);
  2047. if (dma_ctrlr == NULL) {
  2048. dev_err(&pdev->dev, "Failed to allocate memory for "
  2049. "dma resource structure\n");
  2050. retval = -ENOMEM;
  2051. goto err_dma;
  2052. }
  2053. dma_ctrlr->parent = tsi148_bridge;
  2054. mutex_init(&dma_ctrlr->mtx);
  2055. dma_ctrlr->locked = 0;
  2056. dma_ctrlr->number = i;
  2057. dma_ctrlr->route_attr = VME_DMA_VME_TO_MEM |
  2058. VME_DMA_MEM_TO_VME | VME_DMA_VME_TO_VME |
  2059. VME_DMA_MEM_TO_MEM | VME_DMA_PATTERN_TO_VME |
  2060. VME_DMA_PATTERN_TO_MEM;
  2061. INIT_LIST_HEAD(&dma_ctrlr->pending);
  2062. INIT_LIST_HEAD(&dma_ctrlr->running);
  2063. list_add_tail(&dma_ctrlr->list,
  2064. &tsi148_bridge->dma_resources);
  2065. }
  2066. /* Add location monitor to list */
  2067. lm = kmalloc(sizeof(struct vme_lm_resource), GFP_KERNEL);
  2068. if (lm == NULL) {
  2069. dev_err(&pdev->dev, "Failed to allocate memory for "
  2070. "location monitor resource structure\n");
  2071. retval = -ENOMEM;
  2072. goto err_lm;
  2073. }
  2074. lm->parent = tsi148_bridge;
  2075. mutex_init(&lm->mtx);
  2076. lm->locked = 0;
  2077. lm->number = 1;
  2078. lm->monitors = 4;
  2079. list_add_tail(&lm->list, &tsi148_bridge->lm_resources);
  2080. tsi148_bridge->slave_get = tsi148_slave_get;
  2081. tsi148_bridge->slave_set = tsi148_slave_set;
  2082. tsi148_bridge->master_get = tsi148_master_get;
  2083. tsi148_bridge->master_set = tsi148_master_set;
  2084. tsi148_bridge->master_read = tsi148_master_read;
  2085. tsi148_bridge->master_write = tsi148_master_write;
  2086. tsi148_bridge->master_rmw = tsi148_master_rmw;
  2087. tsi148_bridge->dma_list_add = tsi148_dma_list_add;
  2088. tsi148_bridge->dma_list_exec = tsi148_dma_list_exec;
  2089. tsi148_bridge->dma_list_empty = tsi148_dma_list_empty;
  2090. tsi148_bridge->irq_set = tsi148_irq_set;
  2091. tsi148_bridge->irq_generate = tsi148_irq_generate;
  2092. tsi148_bridge->lm_set = tsi148_lm_set;
  2093. tsi148_bridge->lm_get = tsi148_lm_get;
  2094. tsi148_bridge->lm_attach = tsi148_lm_attach;
  2095. tsi148_bridge->lm_detach = tsi148_lm_detach;
  2096. tsi148_bridge->slot_get = tsi148_slot_get;
  2097. tsi148_bridge->alloc_consistent = tsi148_alloc_consistent;
  2098. tsi148_bridge->free_consistent = tsi148_free_consistent;
  2099. data = ioread32be(tsi148_device->base + TSI148_LCSR_VSTAT);
  2100. dev_info(&pdev->dev, "Board is%s the VME system controller\n",
  2101. (data & TSI148_LCSR_VSTAT_SCONS) ? "" : " not");
  2102. if (!geoid)
  2103. dev_info(&pdev->dev, "VME geographical address is %d\n",
  2104. data & TSI148_LCSR_VSTAT_GA_M);
  2105. else
  2106. dev_info(&pdev->dev, "VME geographical address is set to %d\n",
  2107. geoid);
  2108. dev_info(&pdev->dev, "VME Write and flush and error check is %s\n",
  2109. err_chk ? "enabled" : "disabled");
  2110. retval = tsi148_crcsr_init(tsi148_bridge, pdev);
  2111. if (retval) {
  2112. dev_err(&pdev->dev, "CR/CSR configuration failed.\n");
  2113. goto err_crcsr;
  2114. }
  2115. retval = vme_register_bridge(tsi148_bridge);
  2116. if (retval != 0) {
  2117. dev_err(&pdev->dev, "Chip Registration failed.\n");
  2118. goto err_reg;
  2119. }
  2120. pci_set_drvdata(pdev, tsi148_bridge);
  2121. /* Clear VME bus "board fail", and "power-up reset" lines */
  2122. data = ioread32be(tsi148_device->base + TSI148_LCSR_VSTAT);
  2123. data &= ~TSI148_LCSR_VSTAT_BRDFL;
  2124. data |= TSI148_LCSR_VSTAT_CPURST;
  2125. iowrite32be(data, tsi148_device->base + TSI148_LCSR_VSTAT);
  2126. return 0;
  2127. err_reg:
  2128. tsi148_crcsr_exit(tsi148_bridge, pdev);
  2129. err_crcsr:
  2130. err_lm:
  2131. /* resources are stored in link list */
  2132. list_for_each_safe(pos, n, &tsi148_bridge->lm_resources) {
  2133. lm = list_entry(pos, struct vme_lm_resource, list);
  2134. list_del(pos);
  2135. kfree(lm);
  2136. }
  2137. err_dma:
  2138. /* resources are stored in link list */
  2139. list_for_each_safe(pos, n, &tsi148_bridge->dma_resources) {
  2140. dma_ctrlr = list_entry(pos, struct vme_dma_resource, list);
  2141. list_del(pos);
  2142. kfree(dma_ctrlr);
  2143. }
  2144. err_slave:
  2145. /* resources are stored in link list */
  2146. list_for_each_safe(pos, n, &tsi148_bridge->slave_resources) {
  2147. slave_image = list_entry(pos, struct vme_slave_resource, list);
  2148. list_del(pos);
  2149. kfree(slave_image);
  2150. }
  2151. err_master:
  2152. /* resources are stored in link list */
  2153. list_for_each_safe(pos, n, &tsi148_bridge->master_resources) {
  2154. master_image = list_entry(pos, struct vme_master_resource,
  2155. list);
  2156. list_del(pos);
  2157. kfree(master_image);
  2158. }
  2159. tsi148_irq_exit(tsi148_bridge, pdev);
  2160. err_irq:
  2161. err_test:
  2162. iounmap(tsi148_device->base);
  2163. err_remap:
  2164. pci_release_regions(pdev);
  2165. err_resource:
  2166. pci_disable_device(pdev);
  2167. err_enable:
  2168. kfree(tsi148_device);
  2169. err_driver:
  2170. kfree(tsi148_bridge);
  2171. err_struct:
  2172. return retval;
  2173. }
  2174. static void tsi148_remove(struct pci_dev *pdev)
  2175. {
  2176. struct list_head *pos = NULL;
  2177. struct list_head *tmplist;
  2178. struct vme_master_resource *master_image;
  2179. struct vme_slave_resource *slave_image;
  2180. struct vme_dma_resource *dma_ctrlr;
  2181. int i;
  2182. struct tsi148_driver *bridge;
  2183. struct vme_bridge *tsi148_bridge = pci_get_drvdata(pdev);
  2184. bridge = tsi148_bridge->driver_priv;
  2185. dev_dbg(&pdev->dev, "Driver is being unloaded.\n");
  2186. /*
  2187. * Shutdown all inbound and outbound windows.
  2188. */
  2189. for (i = 0; i < 8; i++) {
  2190. iowrite32be(0, bridge->base + TSI148_LCSR_IT[i] +
  2191. TSI148_LCSR_OFFSET_ITAT);
  2192. iowrite32be(0, bridge->base + TSI148_LCSR_OT[i] +
  2193. TSI148_LCSR_OFFSET_OTAT);
  2194. }
  2195. /*
  2196. * Shutdown Location monitor.
  2197. */
  2198. iowrite32be(0, bridge->base + TSI148_LCSR_LMAT);
  2199. /*
  2200. * Shutdown CRG map.
  2201. */
  2202. iowrite32be(0, bridge->base + TSI148_LCSR_CSRAT);
  2203. /*
  2204. * Clear error status.
  2205. */
  2206. iowrite32be(0xFFFFFFFF, bridge->base + TSI148_LCSR_EDPAT);
  2207. iowrite32be(0xFFFFFFFF, bridge->base + TSI148_LCSR_VEAT);
  2208. iowrite32be(0x07000700, bridge->base + TSI148_LCSR_PSTAT);
  2209. /*
  2210. * Remove VIRQ interrupt (if any)
  2211. */
  2212. if (ioread32be(bridge->base + TSI148_LCSR_VICR) & 0x800)
  2213. iowrite32be(0x8000, bridge->base + TSI148_LCSR_VICR);
  2214. /*
  2215. * Map all Interrupts to PCI INTA
  2216. */
  2217. iowrite32be(0x0, bridge->base + TSI148_LCSR_INTM1);
  2218. iowrite32be(0x0, bridge->base + TSI148_LCSR_INTM2);
  2219. tsi148_irq_exit(tsi148_bridge, pdev);
  2220. vme_unregister_bridge(tsi148_bridge);
  2221. tsi148_crcsr_exit(tsi148_bridge, pdev);
  2222. /* resources are stored in link list */
  2223. list_for_each_safe(pos, tmplist, &tsi148_bridge->dma_resources) {
  2224. dma_ctrlr = list_entry(pos, struct vme_dma_resource, list);
  2225. list_del(pos);
  2226. kfree(dma_ctrlr);
  2227. }
  2228. /* resources are stored in link list */
  2229. list_for_each_safe(pos, tmplist, &tsi148_bridge->slave_resources) {
  2230. slave_image = list_entry(pos, struct vme_slave_resource, list);
  2231. list_del(pos);
  2232. kfree(slave_image);
  2233. }
  2234. /* resources are stored in link list */
  2235. list_for_each_safe(pos, tmplist, &tsi148_bridge->master_resources) {
  2236. master_image = list_entry(pos, struct vme_master_resource,
  2237. list);
  2238. list_del(pos);
  2239. kfree(master_image);
  2240. }
  2241. iounmap(bridge->base);
  2242. pci_release_regions(pdev);
  2243. pci_disable_device(pdev);
  2244. kfree(tsi148_bridge->driver_priv);
  2245. kfree(tsi148_bridge);
  2246. }
  2247. module_pci_driver(tsi148_driver);
  2248. MODULE_PARM_DESC(err_chk, "Check for VME errors on reads and writes");
  2249. module_param(err_chk, bool, 0);
  2250. MODULE_PARM_DESC(geoid, "Override geographical addressing");
  2251. module_param(geoid, int, 0);
  2252. MODULE_DESCRIPTION("VME driver for the Tundra Tempe VME bridge");
  2253. MODULE_LICENSE("GPL");