vme_tsi148.c 73 KB

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