vme_tsi148.c 72 KB

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