vme_tsi148.c 72 KB

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