vme_tsi148.c 73 KB

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