vme_tsi148.c 69 KB

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