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