tsi721.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511
  1. /*
  2. * RapidIO mport driver for Tsi721 PCIExpress-to-SRIO bridge
  3. *
  4. * Copyright 2011 Integrated Device Technology, Inc.
  5. * Alexandre Bounine <alexandre.bounine@idt.com>
  6. * Chul Kim <chul.kim@idt.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 59
  20. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <linux/io.h>
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/ioport.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/rio.h>
  30. #include <linux/rio_drv.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/kfifo.h>
  34. #include <linux/delay.h>
  35. #include "tsi721.h"
  36. #define DEBUG_PW /* Inbound Port-Write debugging */
  37. static void tsi721_omsg_handler(struct tsi721_device *priv, int ch);
  38. static void tsi721_imsg_handler(struct tsi721_device *priv, int ch);
  39. /**
  40. * tsi721_lcread - read from local SREP config space
  41. * @mport: RapidIO master port info
  42. * @index: ID of RapdiIO interface
  43. * @offset: Offset into configuration space
  44. * @len: Length (in bytes) of the maintenance transaction
  45. * @data: Value to be read into
  46. *
  47. * Generates a local SREP space read. Returns %0 on
  48. * success or %-EINVAL on failure.
  49. */
  50. static int tsi721_lcread(struct rio_mport *mport, int index, u32 offset,
  51. int len, u32 *data)
  52. {
  53. struct tsi721_device *priv = mport->priv;
  54. if (len != sizeof(u32))
  55. return -EINVAL; /* only 32-bit access is supported */
  56. *data = ioread32(priv->regs + offset);
  57. return 0;
  58. }
  59. /**
  60. * tsi721_lcwrite - write into local SREP config space
  61. * @mport: RapidIO master port info
  62. * @index: ID of RapdiIO interface
  63. * @offset: Offset into configuration space
  64. * @len: Length (in bytes) of the maintenance transaction
  65. * @data: Value to be written
  66. *
  67. * Generates a local write into SREP configuration space. Returns %0 on
  68. * success or %-EINVAL on failure.
  69. */
  70. static int tsi721_lcwrite(struct rio_mport *mport, int index, u32 offset,
  71. int len, u32 data)
  72. {
  73. struct tsi721_device *priv = mport->priv;
  74. if (len != sizeof(u32))
  75. return -EINVAL; /* only 32-bit access is supported */
  76. iowrite32(data, priv->regs + offset);
  77. return 0;
  78. }
  79. /**
  80. * tsi721_maint_dma - Helper function to generate RapidIO maintenance
  81. * transactions using designated Tsi721 DMA channel.
  82. * @priv: pointer to tsi721 private data
  83. * @sys_size: RapdiIO transport system size
  84. * @destid: Destination ID of transaction
  85. * @hopcount: Number of hops to target device
  86. * @offset: Offset into configuration space
  87. * @len: Length (in bytes) of the maintenance transaction
  88. * @data: Location to be read from or write into
  89. * @do_wr: Operation flag (1 == MAINT_WR)
  90. *
  91. * Generates a RapidIO maintenance transaction (Read or Write).
  92. * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
  93. */
  94. static int tsi721_maint_dma(struct tsi721_device *priv, u32 sys_size,
  95. u16 destid, u8 hopcount, u32 offset, int len,
  96. u32 *data, int do_wr)
  97. {
  98. void __iomem *regs = priv->regs + TSI721_DMAC_BASE(priv->mdma.ch_id);
  99. struct tsi721_dma_desc *bd_ptr;
  100. u32 rd_count, swr_ptr, ch_stat;
  101. int i, err = 0;
  102. u32 op = do_wr ? MAINT_WR : MAINT_RD;
  103. if (offset > (RIO_MAINT_SPACE_SZ - len) || (len != sizeof(u32)))
  104. return -EINVAL;
  105. bd_ptr = priv->mdma.bd_base;
  106. rd_count = ioread32(regs + TSI721_DMAC_DRDCNT);
  107. /* Initialize DMA descriptor */
  108. bd_ptr[0].type_id = cpu_to_le32((DTYPE2 << 29) | (op << 19) | destid);
  109. bd_ptr[0].bcount = cpu_to_le32((sys_size << 26) | 0x04);
  110. bd_ptr[0].raddr_lo = cpu_to_le32((hopcount << 24) | offset);
  111. bd_ptr[0].raddr_hi = 0;
  112. if (do_wr)
  113. bd_ptr[0].data[0] = cpu_to_be32p(data);
  114. else
  115. bd_ptr[0].data[0] = 0xffffffff;
  116. mb();
  117. /* Start DMA operation */
  118. iowrite32(rd_count + 2, regs + TSI721_DMAC_DWRCNT);
  119. ioread32(regs + TSI721_DMAC_DWRCNT);
  120. i = 0;
  121. /* Wait until DMA transfer is finished */
  122. while ((ch_stat = ioread32(regs + TSI721_DMAC_STS))
  123. & TSI721_DMAC_STS_RUN) {
  124. udelay(1);
  125. if (++i >= 5000000) {
  126. dev_dbg(&priv->pdev->dev,
  127. "%s : DMA[%d] read timeout ch_status=%x\n",
  128. __func__, priv->mdma.ch_id, ch_stat);
  129. if (!do_wr)
  130. *data = 0xffffffff;
  131. err = -EIO;
  132. goto err_out;
  133. }
  134. }
  135. if (ch_stat & TSI721_DMAC_STS_ABORT) {
  136. /* If DMA operation aborted due to error,
  137. * reinitialize DMA channel
  138. */
  139. dev_dbg(&priv->pdev->dev, "%s : DMA ABORT ch_stat=%x\n",
  140. __func__, ch_stat);
  141. dev_dbg(&priv->pdev->dev, "OP=%d : destid=%x hc=%x off=%x\n",
  142. do_wr ? MAINT_WR : MAINT_RD, destid, hopcount, offset);
  143. iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
  144. iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
  145. udelay(10);
  146. iowrite32(0, regs + TSI721_DMAC_DWRCNT);
  147. udelay(1);
  148. if (!do_wr)
  149. *data = 0xffffffff;
  150. err = -EIO;
  151. goto err_out;
  152. }
  153. if (!do_wr)
  154. *data = be32_to_cpu(bd_ptr[0].data[0]);
  155. /*
  156. * Update descriptor status FIFO RD pointer.
  157. * NOTE: Skipping check and clear FIFO entries because we are waiting
  158. * for transfer to be completed.
  159. */
  160. swr_ptr = ioread32(regs + TSI721_DMAC_DSWP);
  161. iowrite32(swr_ptr, regs + TSI721_DMAC_DSRP);
  162. err_out:
  163. return err;
  164. }
  165. /**
  166. * tsi721_cread_dma - Generate a RapidIO maintenance read transaction
  167. * using Tsi721 BDMA engine.
  168. * @mport: RapidIO master port control structure
  169. * @index: ID of RapdiIO interface
  170. * @destid: Destination ID of transaction
  171. * @hopcount: Number of hops to target device
  172. * @offset: Offset into configuration space
  173. * @len: Length (in bytes) of the maintenance transaction
  174. * @val: Location to be read into
  175. *
  176. * Generates a RapidIO maintenance read transaction.
  177. * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
  178. */
  179. static int tsi721_cread_dma(struct rio_mport *mport, int index, u16 destid,
  180. u8 hopcount, u32 offset, int len, u32 *data)
  181. {
  182. struct tsi721_device *priv = mport->priv;
  183. return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
  184. offset, len, data, 0);
  185. }
  186. /**
  187. * tsi721_cwrite_dma - Generate a RapidIO maintenance write transaction
  188. * using Tsi721 BDMA engine
  189. * @mport: RapidIO master port control structure
  190. * @index: ID of RapdiIO interface
  191. * @destid: Destination ID of transaction
  192. * @hopcount: Number of hops to target device
  193. * @offset: Offset into configuration space
  194. * @len: Length (in bytes) of the maintenance transaction
  195. * @val: Value to be written
  196. *
  197. * Generates a RapidIO maintenance write transaction.
  198. * Returns %0 on success and %-EINVAL or %-EFAULT on failure.
  199. */
  200. static int tsi721_cwrite_dma(struct rio_mport *mport, int index, u16 destid,
  201. u8 hopcount, u32 offset, int len, u32 data)
  202. {
  203. struct tsi721_device *priv = mport->priv;
  204. u32 temp = data;
  205. return tsi721_maint_dma(priv, mport->sys_size, destid, hopcount,
  206. offset, len, &temp, 1);
  207. }
  208. /**
  209. * tsi721_pw_handler - Tsi721 inbound port-write interrupt handler
  210. * @mport: RapidIO master port structure
  211. *
  212. * Handles inbound port-write interrupts. Copies PW message from an internal
  213. * buffer into PW message FIFO and schedules deferred routine to process
  214. * queued messages.
  215. */
  216. static int
  217. tsi721_pw_handler(struct rio_mport *mport)
  218. {
  219. struct tsi721_device *priv = mport->priv;
  220. u32 pw_stat;
  221. u32 pw_buf[TSI721_RIO_PW_MSG_SIZE/sizeof(u32)];
  222. pw_stat = ioread32(priv->regs + TSI721_RIO_PW_RX_STAT);
  223. if (pw_stat & TSI721_RIO_PW_RX_STAT_PW_VAL) {
  224. pw_buf[0] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(0));
  225. pw_buf[1] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(1));
  226. pw_buf[2] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(2));
  227. pw_buf[3] = ioread32(priv->regs + TSI721_RIO_PW_RX_CAPT(3));
  228. /* Queue PW message (if there is room in FIFO),
  229. * otherwise discard it.
  230. */
  231. spin_lock(&priv->pw_fifo_lock);
  232. if (kfifo_avail(&priv->pw_fifo) >= TSI721_RIO_PW_MSG_SIZE)
  233. kfifo_in(&priv->pw_fifo, pw_buf,
  234. TSI721_RIO_PW_MSG_SIZE);
  235. else
  236. priv->pw_discard_count++;
  237. spin_unlock(&priv->pw_fifo_lock);
  238. }
  239. /* Clear pending PW interrupts */
  240. iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
  241. priv->regs + TSI721_RIO_PW_RX_STAT);
  242. schedule_work(&priv->pw_work);
  243. return 0;
  244. }
  245. static void tsi721_pw_dpc(struct work_struct *work)
  246. {
  247. struct tsi721_device *priv = container_of(work, struct tsi721_device,
  248. pw_work);
  249. u32 msg_buffer[RIO_PW_MSG_SIZE/sizeof(u32)]; /* Use full size PW message
  250. buffer for RIO layer */
  251. /*
  252. * Process port-write messages
  253. */
  254. while (kfifo_out_spinlocked(&priv->pw_fifo, (unsigned char *)msg_buffer,
  255. TSI721_RIO_PW_MSG_SIZE, &priv->pw_fifo_lock)) {
  256. /* Process one message */
  257. #ifdef DEBUG_PW
  258. {
  259. u32 i;
  260. pr_debug("%s : Port-Write Message:", __func__);
  261. for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32); ) {
  262. pr_debug("0x%02x: %08x %08x %08x %08x", i*4,
  263. msg_buffer[i], msg_buffer[i + 1],
  264. msg_buffer[i + 2], msg_buffer[i + 3]);
  265. i += 4;
  266. }
  267. pr_debug("\n");
  268. }
  269. #endif
  270. /* Pass the port-write message to RIO core for processing */
  271. rio_inb_pwrite_handler((union rio_pw_msg *)msg_buffer);
  272. }
  273. }
  274. /**
  275. * tsi721_pw_enable - enable/disable port-write interface init
  276. * @mport: Master port implementing the port write unit
  277. * @enable: 1=enable; 0=disable port-write message handling
  278. */
  279. static int tsi721_pw_enable(struct rio_mport *mport, int enable)
  280. {
  281. struct tsi721_device *priv = mport->priv;
  282. u32 rval;
  283. rval = ioread32(priv->regs + TSI721_RIO_EM_INT_ENABLE);
  284. if (enable)
  285. rval |= TSI721_RIO_EM_INT_ENABLE_PW_RX;
  286. else
  287. rval &= ~TSI721_RIO_EM_INT_ENABLE_PW_RX;
  288. /* Clear pending PW interrupts */
  289. iowrite32(TSI721_RIO_PW_RX_STAT_PW_DISC | TSI721_RIO_PW_RX_STAT_PW_VAL,
  290. priv->regs + TSI721_RIO_PW_RX_STAT);
  291. /* Update enable bits */
  292. iowrite32(rval, priv->regs + TSI721_RIO_EM_INT_ENABLE);
  293. return 0;
  294. }
  295. /**
  296. * tsi721_dsend - Send a RapidIO doorbell
  297. * @mport: RapidIO master port info
  298. * @index: ID of RapidIO interface
  299. * @destid: Destination ID of target device
  300. * @data: 16-bit info field of RapidIO doorbell
  301. *
  302. * Sends a RapidIO doorbell message. Always returns %0.
  303. */
  304. static int tsi721_dsend(struct rio_mport *mport, int index,
  305. u16 destid, u16 data)
  306. {
  307. struct tsi721_device *priv = mport->priv;
  308. u32 offset;
  309. offset = (((mport->sys_size) ? RIO_TT_CODE_16 : RIO_TT_CODE_8) << 18) |
  310. (destid << 2);
  311. dev_dbg(&priv->pdev->dev,
  312. "Send Doorbell 0x%04x to destID 0x%x\n", data, destid);
  313. iowrite16be(data, priv->odb_base + offset);
  314. return 0;
  315. }
  316. /**
  317. * tsi721_dbell_handler - Tsi721 doorbell interrupt handler
  318. * @mport: RapidIO master port structure
  319. *
  320. * Handles inbound doorbell interrupts. Copies doorbell entry from an internal
  321. * buffer into DB message FIFO and schedules deferred routine to process
  322. * queued DBs.
  323. */
  324. static int
  325. tsi721_dbell_handler(struct rio_mport *mport)
  326. {
  327. struct tsi721_device *priv = mport->priv;
  328. u32 regval;
  329. /* Disable IDB interrupts */
  330. regval = ioread32(priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
  331. regval &= ~TSI721_SR_CHINT_IDBQRCV;
  332. iowrite32(regval,
  333. priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
  334. schedule_work(&priv->idb_work);
  335. return 0;
  336. }
  337. static void tsi721_db_dpc(struct work_struct *work)
  338. {
  339. struct tsi721_device *priv = container_of(work, struct tsi721_device,
  340. idb_work);
  341. struct rio_mport *mport;
  342. struct rio_dbell *dbell;
  343. int found = 0;
  344. u32 wr_ptr, rd_ptr;
  345. u64 *idb_entry;
  346. u32 regval;
  347. union {
  348. u64 msg;
  349. u8 bytes[8];
  350. } idb;
  351. /*
  352. * Process queued inbound doorbells
  353. */
  354. mport = priv->mport;
  355. wr_ptr = ioread32(priv->regs + TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
  356. rd_ptr = ioread32(priv->regs + TSI721_IDQ_RP(IDB_QUEUE)) % IDB_QSIZE;
  357. while (wr_ptr != rd_ptr) {
  358. idb_entry = (u64 *)(priv->idb_base +
  359. (TSI721_IDB_ENTRY_SIZE * rd_ptr));
  360. rd_ptr++;
  361. rd_ptr %= IDB_QSIZE;
  362. idb.msg = *idb_entry;
  363. *idb_entry = 0;
  364. /* Process one doorbell */
  365. list_for_each_entry(dbell, &mport->dbells, node) {
  366. if ((dbell->res->start <= DBELL_INF(idb.bytes)) &&
  367. (dbell->res->end >= DBELL_INF(idb.bytes))) {
  368. found = 1;
  369. break;
  370. }
  371. }
  372. if (found) {
  373. dbell->dinb(mport, dbell->dev_id, DBELL_SID(idb.bytes),
  374. DBELL_TID(idb.bytes), DBELL_INF(idb.bytes));
  375. } else {
  376. dev_dbg(&priv->pdev->dev,
  377. "spurious inb doorbell, sid %2.2x tid %2.2x"
  378. " info %4.4x\n", DBELL_SID(idb.bytes),
  379. DBELL_TID(idb.bytes), DBELL_INF(idb.bytes));
  380. }
  381. wr_ptr = ioread32(priv->regs +
  382. TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
  383. }
  384. iowrite32(rd_ptr & (IDB_QSIZE - 1),
  385. priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
  386. /* Re-enable IDB interrupts */
  387. regval = ioread32(priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
  388. regval |= TSI721_SR_CHINT_IDBQRCV;
  389. iowrite32(regval,
  390. priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
  391. wr_ptr = ioread32(priv->regs + TSI721_IDQ_WP(IDB_QUEUE)) % IDB_QSIZE;
  392. if (wr_ptr != rd_ptr)
  393. schedule_work(&priv->idb_work);
  394. }
  395. /**
  396. * tsi721_irqhandler - Tsi721 interrupt handler
  397. * @irq: Linux interrupt number
  398. * @ptr: Pointer to interrupt-specific data (mport structure)
  399. *
  400. * Handles Tsi721 interrupts signaled using MSI and INTA. Checks reported
  401. * interrupt events and calls an event-specific handler(s).
  402. */
  403. static irqreturn_t tsi721_irqhandler(int irq, void *ptr)
  404. {
  405. struct rio_mport *mport = (struct rio_mport *)ptr;
  406. struct tsi721_device *priv = mport->priv;
  407. u32 dev_int;
  408. u32 dev_ch_int;
  409. u32 intval;
  410. u32 ch_inte;
  411. dev_int = ioread32(priv->regs + TSI721_DEV_INT);
  412. if (!dev_int)
  413. return IRQ_NONE;
  414. dev_ch_int = ioread32(priv->regs + TSI721_DEV_CHAN_INT);
  415. if (dev_int & TSI721_DEV_INT_SR2PC_CH) {
  416. /* Service SR2PC Channel interrupts */
  417. if (dev_ch_int & TSI721_INT_SR2PC_CHAN(IDB_QUEUE)) {
  418. /* Service Inbound Doorbell interrupt */
  419. intval = ioread32(priv->regs +
  420. TSI721_SR_CHINT(IDB_QUEUE));
  421. if (intval & TSI721_SR_CHINT_IDBQRCV)
  422. tsi721_dbell_handler(mport);
  423. else
  424. dev_info(&priv->pdev->dev,
  425. "Unsupported SR_CH_INT %x\n", intval);
  426. /* Clear interrupts */
  427. iowrite32(intval,
  428. priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
  429. ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
  430. }
  431. }
  432. if (dev_int & TSI721_DEV_INT_SMSG_CH) {
  433. int ch;
  434. /*
  435. * Service channel interrupts from Messaging Engine
  436. */
  437. if (dev_ch_int & TSI721_INT_IMSG_CHAN_M) { /* Inbound Msg */
  438. /* Disable signaled OB MSG Channel interrupts */
  439. ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  440. ch_inte &= ~(dev_ch_int & TSI721_INT_IMSG_CHAN_M);
  441. iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
  442. /*
  443. * Process Inbound Message interrupt for each MBOX
  444. */
  445. for (ch = 4; ch < RIO_MAX_MBOX + 4; ch++) {
  446. if (!(dev_ch_int & TSI721_INT_IMSG_CHAN(ch)))
  447. continue;
  448. tsi721_imsg_handler(priv, ch);
  449. }
  450. }
  451. if (dev_ch_int & TSI721_INT_OMSG_CHAN_M) { /* Outbound Msg */
  452. /* Disable signaled OB MSG Channel interrupts */
  453. ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  454. ch_inte &= ~(dev_ch_int & TSI721_INT_OMSG_CHAN_M);
  455. iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
  456. /*
  457. * Process Outbound Message interrupts for each MBOX
  458. */
  459. for (ch = 0; ch < RIO_MAX_MBOX; ch++) {
  460. if (!(dev_ch_int & TSI721_INT_OMSG_CHAN(ch)))
  461. continue;
  462. tsi721_omsg_handler(priv, ch);
  463. }
  464. }
  465. }
  466. if (dev_int & TSI721_DEV_INT_SRIO) {
  467. /* Service SRIO MAC interrupts */
  468. intval = ioread32(priv->regs + TSI721_RIO_EM_INT_STAT);
  469. if (intval & TSI721_RIO_EM_INT_STAT_PW_RX)
  470. tsi721_pw_handler(mport);
  471. }
  472. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  473. if (dev_int & TSI721_DEV_INT_BDMA_CH) {
  474. int ch;
  475. if (dev_ch_int & TSI721_INT_BDMA_CHAN_M) {
  476. dev_dbg(&priv->pdev->dev,
  477. "IRQ from DMA channel 0x%08x\n", dev_ch_int);
  478. for (ch = 0; ch < TSI721_DMA_MAXCH; ch++) {
  479. if (!(dev_ch_int & TSI721_INT_BDMA_CHAN(ch)))
  480. continue;
  481. tsi721_bdma_handler(&priv->bdma[ch]);
  482. }
  483. }
  484. }
  485. #endif
  486. return IRQ_HANDLED;
  487. }
  488. static void tsi721_interrupts_init(struct tsi721_device *priv)
  489. {
  490. u32 intr;
  491. /* Enable IDB interrupts */
  492. iowrite32(TSI721_SR_CHINT_ALL,
  493. priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
  494. iowrite32(TSI721_SR_CHINT_IDBQRCV,
  495. priv->regs + TSI721_SR_CHINTE(IDB_QUEUE));
  496. /* Enable SRIO MAC interrupts */
  497. iowrite32(TSI721_RIO_EM_DEV_INT_EN_INT,
  498. priv->regs + TSI721_RIO_EM_DEV_INT_EN);
  499. /* Enable interrupts from channels in use */
  500. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  501. intr = TSI721_INT_SR2PC_CHAN(IDB_QUEUE) |
  502. (TSI721_INT_BDMA_CHAN_M &
  503. ~TSI721_INT_BDMA_CHAN(TSI721_DMACH_MAINT));
  504. #else
  505. intr = TSI721_INT_SR2PC_CHAN(IDB_QUEUE);
  506. #endif
  507. iowrite32(intr, priv->regs + TSI721_DEV_CHAN_INTE);
  508. if (priv->flags & TSI721_USING_MSIX)
  509. intr = TSI721_DEV_INT_SRIO;
  510. else
  511. intr = TSI721_DEV_INT_SR2PC_CH | TSI721_DEV_INT_SRIO |
  512. TSI721_DEV_INT_SMSG_CH | TSI721_DEV_INT_BDMA_CH;
  513. iowrite32(intr, priv->regs + TSI721_DEV_INTE);
  514. ioread32(priv->regs + TSI721_DEV_INTE);
  515. }
  516. #ifdef CONFIG_PCI_MSI
  517. /**
  518. * tsi721_omsg_msix - MSI-X interrupt handler for outbound messaging
  519. * @irq: Linux interrupt number
  520. * @ptr: Pointer to interrupt-specific data (mport structure)
  521. *
  522. * Handles outbound messaging interrupts signaled using MSI-X.
  523. */
  524. static irqreturn_t tsi721_omsg_msix(int irq, void *ptr)
  525. {
  526. struct tsi721_device *priv = ((struct rio_mport *)ptr)->priv;
  527. int mbox;
  528. mbox = (irq - priv->msix[TSI721_VECT_OMB0_DONE].vector) % RIO_MAX_MBOX;
  529. tsi721_omsg_handler(priv, mbox);
  530. return IRQ_HANDLED;
  531. }
  532. /**
  533. * tsi721_imsg_msix - MSI-X interrupt handler for inbound messaging
  534. * @irq: Linux interrupt number
  535. * @ptr: Pointer to interrupt-specific data (mport structure)
  536. *
  537. * Handles inbound messaging interrupts signaled using MSI-X.
  538. */
  539. static irqreturn_t tsi721_imsg_msix(int irq, void *ptr)
  540. {
  541. struct tsi721_device *priv = ((struct rio_mport *)ptr)->priv;
  542. int mbox;
  543. mbox = (irq - priv->msix[TSI721_VECT_IMB0_RCV].vector) % RIO_MAX_MBOX;
  544. tsi721_imsg_handler(priv, mbox + 4);
  545. return IRQ_HANDLED;
  546. }
  547. /**
  548. * tsi721_srio_msix - Tsi721 MSI-X SRIO MAC interrupt handler
  549. * @irq: Linux interrupt number
  550. * @ptr: Pointer to interrupt-specific data (mport structure)
  551. *
  552. * Handles Tsi721 interrupts from SRIO MAC.
  553. */
  554. static irqreturn_t tsi721_srio_msix(int irq, void *ptr)
  555. {
  556. struct tsi721_device *priv = ((struct rio_mport *)ptr)->priv;
  557. u32 srio_int;
  558. /* Service SRIO MAC interrupts */
  559. srio_int = ioread32(priv->regs + TSI721_RIO_EM_INT_STAT);
  560. if (srio_int & TSI721_RIO_EM_INT_STAT_PW_RX)
  561. tsi721_pw_handler((struct rio_mport *)ptr);
  562. return IRQ_HANDLED;
  563. }
  564. /**
  565. * tsi721_sr2pc_ch_msix - Tsi721 MSI-X SR2PC Channel interrupt handler
  566. * @irq: Linux interrupt number
  567. * @ptr: Pointer to interrupt-specific data (mport structure)
  568. *
  569. * Handles Tsi721 interrupts from SR2PC Channel.
  570. * NOTE: At this moment services only one SR2PC channel associated with inbound
  571. * doorbells.
  572. */
  573. static irqreturn_t tsi721_sr2pc_ch_msix(int irq, void *ptr)
  574. {
  575. struct tsi721_device *priv = ((struct rio_mport *)ptr)->priv;
  576. u32 sr_ch_int;
  577. /* Service Inbound DB interrupt from SR2PC channel */
  578. sr_ch_int = ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
  579. if (sr_ch_int & TSI721_SR_CHINT_IDBQRCV)
  580. tsi721_dbell_handler((struct rio_mport *)ptr);
  581. /* Clear interrupts */
  582. iowrite32(sr_ch_int, priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
  583. /* Read back to ensure that interrupt was cleared */
  584. sr_ch_int = ioread32(priv->regs + TSI721_SR_CHINT(IDB_QUEUE));
  585. return IRQ_HANDLED;
  586. }
  587. /**
  588. * tsi721_request_msix - register interrupt service for MSI-X mode.
  589. * @mport: RapidIO master port structure
  590. *
  591. * Registers MSI-X interrupt service routines for interrupts that are active
  592. * immediately after mport initialization. Messaging interrupt service routines
  593. * should be registered during corresponding open requests.
  594. */
  595. static int tsi721_request_msix(struct rio_mport *mport)
  596. {
  597. struct tsi721_device *priv = mport->priv;
  598. int err = 0;
  599. err = request_irq(priv->msix[TSI721_VECT_IDB].vector,
  600. tsi721_sr2pc_ch_msix, 0,
  601. priv->msix[TSI721_VECT_IDB].irq_name, (void *)mport);
  602. if (err)
  603. goto out;
  604. err = request_irq(priv->msix[TSI721_VECT_PWRX].vector,
  605. tsi721_srio_msix, 0,
  606. priv->msix[TSI721_VECT_PWRX].irq_name, (void *)mport);
  607. if (err)
  608. free_irq(
  609. priv->msix[TSI721_VECT_IDB].vector,
  610. (void *)mport);
  611. out:
  612. return err;
  613. }
  614. /**
  615. * tsi721_enable_msix - Attempts to enable MSI-X support for Tsi721.
  616. * @priv: pointer to tsi721 private data
  617. *
  618. * Configures MSI-X support for Tsi721. Supports only an exact number
  619. * of requested vectors.
  620. */
  621. static int tsi721_enable_msix(struct tsi721_device *priv)
  622. {
  623. struct msix_entry entries[TSI721_VECT_MAX];
  624. int err;
  625. int i;
  626. entries[TSI721_VECT_IDB].entry = TSI721_MSIX_SR2PC_IDBQ_RCV(IDB_QUEUE);
  627. entries[TSI721_VECT_PWRX].entry = TSI721_MSIX_SRIO_MAC_INT;
  628. /*
  629. * Initialize MSI-X entries for Messaging Engine:
  630. * this driver supports four RIO mailboxes (inbound and outbound)
  631. * NOTE: Inbound message MBOX 0...4 use IB channels 4...7. Therefore
  632. * offset +4 is added to IB MBOX number.
  633. */
  634. for (i = 0; i < RIO_MAX_MBOX; i++) {
  635. entries[TSI721_VECT_IMB0_RCV + i].entry =
  636. TSI721_MSIX_IMSG_DQ_RCV(i + 4);
  637. entries[TSI721_VECT_IMB0_INT + i].entry =
  638. TSI721_MSIX_IMSG_INT(i + 4);
  639. entries[TSI721_VECT_OMB0_DONE + i].entry =
  640. TSI721_MSIX_OMSG_DONE(i);
  641. entries[TSI721_VECT_OMB0_INT + i].entry =
  642. TSI721_MSIX_OMSG_INT(i);
  643. }
  644. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  645. /*
  646. * Initialize MSI-X entries for Block DMA Engine:
  647. * this driver supports XXX DMA channels
  648. * (one is reserved for SRIO maintenance transactions)
  649. */
  650. for (i = 0; i < TSI721_DMA_CHNUM; i++) {
  651. entries[TSI721_VECT_DMA0_DONE + i].entry =
  652. TSI721_MSIX_DMACH_DONE(i);
  653. entries[TSI721_VECT_DMA0_INT + i].entry =
  654. TSI721_MSIX_DMACH_INT(i);
  655. }
  656. #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
  657. err = pci_enable_msix(priv->pdev, entries, ARRAY_SIZE(entries));
  658. if (err) {
  659. if (err > 0)
  660. dev_info(&priv->pdev->dev,
  661. "Only %d MSI-X vectors available, "
  662. "not using MSI-X\n", err);
  663. else
  664. dev_err(&priv->pdev->dev,
  665. "Failed to enable MSI-X (err=%d)\n", err);
  666. return err;
  667. }
  668. /*
  669. * Copy MSI-X vector information into tsi721 private structure
  670. */
  671. priv->msix[TSI721_VECT_IDB].vector = entries[TSI721_VECT_IDB].vector;
  672. snprintf(priv->msix[TSI721_VECT_IDB].irq_name, IRQ_DEVICE_NAME_MAX,
  673. DRV_NAME "-idb@pci:%s", pci_name(priv->pdev));
  674. priv->msix[TSI721_VECT_PWRX].vector = entries[TSI721_VECT_PWRX].vector;
  675. snprintf(priv->msix[TSI721_VECT_PWRX].irq_name, IRQ_DEVICE_NAME_MAX,
  676. DRV_NAME "-pwrx@pci:%s", pci_name(priv->pdev));
  677. for (i = 0; i < RIO_MAX_MBOX; i++) {
  678. priv->msix[TSI721_VECT_IMB0_RCV + i].vector =
  679. entries[TSI721_VECT_IMB0_RCV + i].vector;
  680. snprintf(priv->msix[TSI721_VECT_IMB0_RCV + i].irq_name,
  681. IRQ_DEVICE_NAME_MAX, DRV_NAME "-imbr%d@pci:%s",
  682. i, pci_name(priv->pdev));
  683. priv->msix[TSI721_VECT_IMB0_INT + i].vector =
  684. entries[TSI721_VECT_IMB0_INT + i].vector;
  685. snprintf(priv->msix[TSI721_VECT_IMB0_INT + i].irq_name,
  686. IRQ_DEVICE_NAME_MAX, DRV_NAME "-imbi%d@pci:%s",
  687. i, pci_name(priv->pdev));
  688. priv->msix[TSI721_VECT_OMB0_DONE + i].vector =
  689. entries[TSI721_VECT_OMB0_DONE + i].vector;
  690. snprintf(priv->msix[TSI721_VECT_OMB0_DONE + i].irq_name,
  691. IRQ_DEVICE_NAME_MAX, DRV_NAME "-ombd%d@pci:%s",
  692. i, pci_name(priv->pdev));
  693. priv->msix[TSI721_VECT_OMB0_INT + i].vector =
  694. entries[TSI721_VECT_OMB0_INT + i].vector;
  695. snprintf(priv->msix[TSI721_VECT_OMB0_INT + i].irq_name,
  696. IRQ_DEVICE_NAME_MAX, DRV_NAME "-ombi%d@pci:%s",
  697. i, pci_name(priv->pdev));
  698. }
  699. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  700. for (i = 0; i < TSI721_DMA_CHNUM; i++) {
  701. priv->msix[TSI721_VECT_DMA0_DONE + i].vector =
  702. entries[TSI721_VECT_DMA0_DONE + i].vector;
  703. snprintf(priv->msix[TSI721_VECT_DMA0_DONE + i].irq_name,
  704. IRQ_DEVICE_NAME_MAX, DRV_NAME "-dmad%d@pci:%s",
  705. i, pci_name(priv->pdev));
  706. priv->msix[TSI721_VECT_DMA0_INT + i].vector =
  707. entries[TSI721_VECT_DMA0_INT + i].vector;
  708. snprintf(priv->msix[TSI721_VECT_DMA0_INT + i].irq_name,
  709. IRQ_DEVICE_NAME_MAX, DRV_NAME "-dmai%d@pci:%s",
  710. i, pci_name(priv->pdev));
  711. }
  712. #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
  713. return 0;
  714. }
  715. #endif /* CONFIG_PCI_MSI */
  716. static int tsi721_request_irq(struct rio_mport *mport)
  717. {
  718. struct tsi721_device *priv = mport->priv;
  719. int err;
  720. #ifdef CONFIG_PCI_MSI
  721. if (priv->flags & TSI721_USING_MSIX)
  722. err = tsi721_request_msix(mport);
  723. else
  724. #endif
  725. err = request_irq(priv->pdev->irq, tsi721_irqhandler,
  726. (priv->flags & TSI721_USING_MSI) ? 0 : IRQF_SHARED,
  727. DRV_NAME, (void *)mport);
  728. if (err)
  729. dev_err(&priv->pdev->dev,
  730. "Unable to allocate interrupt, Error: %d\n", err);
  731. return err;
  732. }
  733. /**
  734. * tsi721_init_pc2sr_mapping - initializes outbound (PCIe->SRIO)
  735. * translation regions.
  736. * @priv: pointer to tsi721 private data
  737. *
  738. * Disables SREP translation regions.
  739. */
  740. static void tsi721_init_pc2sr_mapping(struct tsi721_device *priv)
  741. {
  742. int i;
  743. /* Disable all PC2SR translation windows */
  744. for (i = 0; i < TSI721_OBWIN_NUM; i++)
  745. iowrite32(0, priv->regs + TSI721_OBWINLB(i));
  746. }
  747. /**
  748. * tsi721_rio_map_inb_mem -- Mapping inbound memory region.
  749. * @mport: RapidIO master port
  750. * @lstart: Local memory space start address.
  751. * @rstart: RapidIO space start address.
  752. * @size: The mapping region size.
  753. * @flags: Flags for mapping. 0 for using default flags.
  754. *
  755. * Return: 0 -- Success.
  756. *
  757. * This function will create the inbound mapping
  758. * from rstart to lstart.
  759. */
  760. static int tsi721_rio_map_inb_mem(struct rio_mport *mport, dma_addr_t lstart,
  761. u64 rstart, u32 size, u32 flags)
  762. {
  763. struct tsi721_device *priv = mport->priv;
  764. int i;
  765. u32 regval;
  766. if (!is_power_of_2(size) || size < 0x1000 ||
  767. ((u64)lstart & (size - 1)) || (rstart & (size - 1)))
  768. return -EINVAL;
  769. /* Search for free inbound translation window */
  770. for (i = 0; i < TSI721_IBWIN_NUM; i++) {
  771. regval = ioread32(priv->regs + TSI721_IBWIN_LB(i));
  772. if (!(regval & TSI721_IBWIN_LB_WEN))
  773. break;
  774. }
  775. if (i >= TSI721_IBWIN_NUM) {
  776. dev_err(&priv->pdev->dev,
  777. "Unable to find free inbound window\n");
  778. return -EBUSY;
  779. }
  780. iowrite32(TSI721_IBWIN_SIZE(size) << 8,
  781. priv->regs + TSI721_IBWIN_SZ(i));
  782. iowrite32(((u64)lstart >> 32), priv->regs + TSI721_IBWIN_TUA(i));
  783. iowrite32(((u64)lstart & TSI721_IBWIN_TLA_ADD),
  784. priv->regs + TSI721_IBWIN_TLA(i));
  785. iowrite32(rstart >> 32, priv->regs + TSI721_IBWIN_UB(i));
  786. iowrite32((rstart & TSI721_IBWIN_LB_BA) | TSI721_IBWIN_LB_WEN,
  787. priv->regs + TSI721_IBWIN_LB(i));
  788. dev_dbg(&priv->pdev->dev,
  789. "Configured IBWIN%d mapping (RIO_0x%llx -> PCIe_0x%llx)\n",
  790. i, rstart, (unsigned long long)lstart);
  791. return 0;
  792. }
  793. /**
  794. * fsl_rio_unmap_inb_mem -- Unmapping inbound memory region.
  795. * @mport: RapidIO master port
  796. * @lstart: Local memory space start address.
  797. */
  798. static void tsi721_rio_unmap_inb_mem(struct rio_mport *mport,
  799. dma_addr_t lstart)
  800. {
  801. struct tsi721_device *priv = mport->priv;
  802. int i;
  803. u64 addr;
  804. u32 regval;
  805. /* Search for matching active inbound translation window */
  806. for (i = 0; i < TSI721_IBWIN_NUM; i++) {
  807. regval = ioread32(priv->regs + TSI721_IBWIN_LB(i));
  808. if (regval & TSI721_IBWIN_LB_WEN) {
  809. regval = ioread32(priv->regs + TSI721_IBWIN_TUA(i));
  810. addr = (u64)regval << 32;
  811. regval = ioread32(priv->regs + TSI721_IBWIN_TLA(i));
  812. addr |= regval & TSI721_IBWIN_TLA_ADD;
  813. if (addr == (u64)lstart) {
  814. iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
  815. break;
  816. }
  817. }
  818. }
  819. }
  820. /**
  821. * tsi721_init_sr2pc_mapping - initializes inbound (SRIO->PCIe)
  822. * translation regions.
  823. * @priv: pointer to tsi721 private data
  824. *
  825. * Disables inbound windows.
  826. */
  827. static void tsi721_init_sr2pc_mapping(struct tsi721_device *priv)
  828. {
  829. int i;
  830. /* Disable all SR2PC inbound windows */
  831. for (i = 0; i < TSI721_IBWIN_NUM; i++)
  832. iowrite32(0, priv->regs + TSI721_IBWIN_LB(i));
  833. }
  834. /**
  835. * tsi721_port_write_init - Inbound port write interface init
  836. * @priv: pointer to tsi721 private data
  837. *
  838. * Initializes inbound port write handler.
  839. * Returns %0 on success or %-ENOMEM on failure.
  840. */
  841. static int tsi721_port_write_init(struct tsi721_device *priv)
  842. {
  843. priv->pw_discard_count = 0;
  844. INIT_WORK(&priv->pw_work, tsi721_pw_dpc);
  845. spin_lock_init(&priv->pw_fifo_lock);
  846. if (kfifo_alloc(&priv->pw_fifo,
  847. TSI721_RIO_PW_MSG_SIZE * 32, GFP_KERNEL)) {
  848. dev_err(&priv->pdev->dev, "PW FIFO allocation failed\n");
  849. return -ENOMEM;
  850. }
  851. /* Use reliable port-write capture mode */
  852. iowrite32(TSI721_RIO_PW_CTL_PWC_REL, priv->regs + TSI721_RIO_PW_CTL);
  853. return 0;
  854. }
  855. static int tsi721_doorbell_init(struct tsi721_device *priv)
  856. {
  857. /* Outbound Doorbells do not require any setup.
  858. * Tsi721 uses dedicated PCI BAR1 to generate doorbells.
  859. * That BAR1 was mapped during the probe routine.
  860. */
  861. /* Initialize Inbound Doorbell processing DPC and queue */
  862. priv->db_discard_count = 0;
  863. INIT_WORK(&priv->idb_work, tsi721_db_dpc);
  864. /* Allocate buffer for inbound doorbells queue */
  865. priv->idb_base = dma_zalloc_coherent(&priv->pdev->dev,
  866. IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
  867. &priv->idb_dma, GFP_KERNEL);
  868. if (!priv->idb_base)
  869. return -ENOMEM;
  870. dev_dbg(&priv->pdev->dev, "Allocated IDB buffer @ %p (phys = %llx)\n",
  871. priv->idb_base, (unsigned long long)priv->idb_dma);
  872. iowrite32(TSI721_IDQ_SIZE_VAL(IDB_QSIZE),
  873. priv->regs + TSI721_IDQ_SIZE(IDB_QUEUE));
  874. iowrite32(((u64)priv->idb_dma >> 32),
  875. priv->regs + TSI721_IDQ_BASEU(IDB_QUEUE));
  876. iowrite32(((u64)priv->idb_dma & TSI721_IDQ_BASEL_ADDR),
  877. priv->regs + TSI721_IDQ_BASEL(IDB_QUEUE));
  878. /* Enable accepting all inbound doorbells */
  879. iowrite32(0, priv->regs + TSI721_IDQ_MASK(IDB_QUEUE));
  880. iowrite32(TSI721_IDQ_INIT, priv->regs + TSI721_IDQ_CTL(IDB_QUEUE));
  881. iowrite32(0, priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
  882. return 0;
  883. }
  884. static void tsi721_doorbell_free(struct tsi721_device *priv)
  885. {
  886. if (priv->idb_base == NULL)
  887. return;
  888. /* Free buffer allocated for inbound doorbell queue */
  889. dma_free_coherent(&priv->pdev->dev, IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
  890. priv->idb_base, priv->idb_dma);
  891. priv->idb_base = NULL;
  892. }
  893. /**
  894. * tsi721_bdma_maint_init - Initialize maintenance request BDMA channel.
  895. * @priv: pointer to tsi721 private data
  896. *
  897. * Initialize BDMA channel allocated for RapidIO maintenance read/write
  898. * request generation
  899. * Returns %0 on success or %-ENOMEM on failure.
  900. */
  901. static int tsi721_bdma_maint_init(struct tsi721_device *priv)
  902. {
  903. struct tsi721_dma_desc *bd_ptr;
  904. u64 *sts_ptr;
  905. dma_addr_t bd_phys, sts_phys;
  906. int sts_size;
  907. int bd_num = 2;
  908. void __iomem *regs;
  909. dev_dbg(&priv->pdev->dev,
  910. "Init Block DMA Engine for Maintenance requests, CH%d\n",
  911. TSI721_DMACH_MAINT);
  912. /*
  913. * Initialize DMA channel for maintenance requests
  914. */
  915. priv->mdma.ch_id = TSI721_DMACH_MAINT;
  916. regs = priv->regs + TSI721_DMAC_BASE(TSI721_DMACH_MAINT);
  917. /* Allocate space for DMA descriptors */
  918. bd_ptr = dma_zalloc_coherent(&priv->pdev->dev,
  919. bd_num * sizeof(struct tsi721_dma_desc),
  920. &bd_phys, GFP_KERNEL);
  921. if (!bd_ptr)
  922. return -ENOMEM;
  923. priv->mdma.bd_num = bd_num;
  924. priv->mdma.bd_phys = bd_phys;
  925. priv->mdma.bd_base = bd_ptr;
  926. dev_dbg(&priv->pdev->dev, "DMA descriptors @ %p (phys = %llx)\n",
  927. bd_ptr, (unsigned long long)bd_phys);
  928. /* Allocate space for descriptor status FIFO */
  929. sts_size = (bd_num >= TSI721_DMA_MINSTSSZ) ?
  930. bd_num : TSI721_DMA_MINSTSSZ;
  931. sts_size = roundup_pow_of_two(sts_size);
  932. sts_ptr = dma_zalloc_coherent(&priv->pdev->dev,
  933. sts_size * sizeof(struct tsi721_dma_sts),
  934. &sts_phys, GFP_KERNEL);
  935. if (!sts_ptr) {
  936. /* Free space allocated for DMA descriptors */
  937. dma_free_coherent(&priv->pdev->dev,
  938. bd_num * sizeof(struct tsi721_dma_desc),
  939. bd_ptr, bd_phys);
  940. priv->mdma.bd_base = NULL;
  941. return -ENOMEM;
  942. }
  943. priv->mdma.sts_phys = sts_phys;
  944. priv->mdma.sts_base = sts_ptr;
  945. priv->mdma.sts_size = sts_size;
  946. dev_dbg(&priv->pdev->dev,
  947. "desc status FIFO @ %p (phys = %llx) size=0x%x\n",
  948. sts_ptr, (unsigned long long)sts_phys, sts_size);
  949. /* Initialize DMA descriptors ring */
  950. bd_ptr[bd_num - 1].type_id = cpu_to_le32(DTYPE3 << 29);
  951. bd_ptr[bd_num - 1].next_lo = cpu_to_le32((u64)bd_phys &
  952. TSI721_DMAC_DPTRL_MASK);
  953. bd_ptr[bd_num - 1].next_hi = cpu_to_le32((u64)bd_phys >> 32);
  954. /* Setup DMA descriptor pointers */
  955. iowrite32(((u64)bd_phys >> 32), regs + TSI721_DMAC_DPTRH);
  956. iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
  957. regs + TSI721_DMAC_DPTRL);
  958. /* Setup descriptor status FIFO */
  959. iowrite32(((u64)sts_phys >> 32), regs + TSI721_DMAC_DSBH);
  960. iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
  961. regs + TSI721_DMAC_DSBL);
  962. iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
  963. regs + TSI721_DMAC_DSSZ);
  964. /* Clear interrupt bits */
  965. iowrite32(TSI721_DMAC_INT_ALL, regs + TSI721_DMAC_INT);
  966. ioread32(regs + TSI721_DMAC_INT);
  967. /* Toggle DMA channel initialization */
  968. iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
  969. ioread32(regs + TSI721_DMAC_CTL);
  970. udelay(10);
  971. return 0;
  972. }
  973. static int tsi721_bdma_maint_free(struct tsi721_device *priv)
  974. {
  975. u32 ch_stat;
  976. struct tsi721_bdma_maint *mdma = &priv->mdma;
  977. void __iomem *regs = priv->regs + TSI721_DMAC_BASE(mdma->ch_id);
  978. if (mdma->bd_base == NULL)
  979. return 0;
  980. /* Check if DMA channel still running */
  981. ch_stat = ioread32(regs + TSI721_DMAC_STS);
  982. if (ch_stat & TSI721_DMAC_STS_RUN)
  983. return -EFAULT;
  984. /* Put DMA channel into init state */
  985. iowrite32(TSI721_DMAC_CTL_INIT, regs + TSI721_DMAC_CTL);
  986. /* Free space allocated for DMA descriptors */
  987. dma_free_coherent(&priv->pdev->dev,
  988. mdma->bd_num * sizeof(struct tsi721_dma_desc),
  989. mdma->bd_base, mdma->bd_phys);
  990. mdma->bd_base = NULL;
  991. /* Free space allocated for status FIFO */
  992. dma_free_coherent(&priv->pdev->dev,
  993. mdma->sts_size * sizeof(struct tsi721_dma_sts),
  994. mdma->sts_base, mdma->sts_phys);
  995. mdma->sts_base = NULL;
  996. return 0;
  997. }
  998. /* Enable Inbound Messaging Interrupts */
  999. static void
  1000. tsi721_imsg_interrupt_enable(struct tsi721_device *priv, int ch,
  1001. u32 inte_mask)
  1002. {
  1003. u32 rval;
  1004. if (!inte_mask)
  1005. return;
  1006. /* Clear pending Inbound Messaging interrupts */
  1007. iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
  1008. /* Enable Inbound Messaging interrupts */
  1009. rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
  1010. iowrite32(rval | inte_mask, priv->regs + TSI721_IBDMAC_INTE(ch));
  1011. if (priv->flags & TSI721_USING_MSIX)
  1012. return; /* Finished if we are in MSI-X mode */
  1013. /*
  1014. * For MSI and INTA interrupt signalling we need to enable next levels
  1015. */
  1016. /* Enable Device Channel Interrupt */
  1017. rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1018. iowrite32(rval | TSI721_INT_IMSG_CHAN(ch),
  1019. priv->regs + TSI721_DEV_CHAN_INTE);
  1020. }
  1021. /* Disable Inbound Messaging Interrupts */
  1022. static void
  1023. tsi721_imsg_interrupt_disable(struct tsi721_device *priv, int ch,
  1024. u32 inte_mask)
  1025. {
  1026. u32 rval;
  1027. if (!inte_mask)
  1028. return;
  1029. /* Clear pending Inbound Messaging interrupts */
  1030. iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
  1031. /* Disable Inbound Messaging interrupts */
  1032. rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
  1033. rval &= ~inte_mask;
  1034. iowrite32(rval, priv->regs + TSI721_IBDMAC_INTE(ch));
  1035. if (priv->flags & TSI721_USING_MSIX)
  1036. return; /* Finished if we are in MSI-X mode */
  1037. /*
  1038. * For MSI and INTA interrupt signalling we need to disable next levels
  1039. */
  1040. /* Disable Device Channel Interrupt */
  1041. rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1042. rval &= ~TSI721_INT_IMSG_CHAN(ch);
  1043. iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
  1044. }
  1045. /* Enable Outbound Messaging interrupts */
  1046. static void
  1047. tsi721_omsg_interrupt_enable(struct tsi721_device *priv, int ch,
  1048. u32 inte_mask)
  1049. {
  1050. u32 rval;
  1051. if (!inte_mask)
  1052. return;
  1053. /* Clear pending Outbound Messaging interrupts */
  1054. iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
  1055. /* Enable Outbound Messaging channel interrupts */
  1056. rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
  1057. iowrite32(rval | inte_mask, priv->regs + TSI721_OBDMAC_INTE(ch));
  1058. if (priv->flags & TSI721_USING_MSIX)
  1059. return; /* Finished if we are in MSI-X mode */
  1060. /*
  1061. * For MSI and INTA interrupt signalling we need to enable next levels
  1062. */
  1063. /* Enable Device Channel Interrupt */
  1064. rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1065. iowrite32(rval | TSI721_INT_OMSG_CHAN(ch),
  1066. priv->regs + TSI721_DEV_CHAN_INTE);
  1067. }
  1068. /* Disable Outbound Messaging interrupts */
  1069. static void
  1070. tsi721_omsg_interrupt_disable(struct tsi721_device *priv, int ch,
  1071. u32 inte_mask)
  1072. {
  1073. u32 rval;
  1074. if (!inte_mask)
  1075. return;
  1076. /* Clear pending Outbound Messaging interrupts */
  1077. iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
  1078. /* Disable Outbound Messaging interrupts */
  1079. rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
  1080. rval &= ~inte_mask;
  1081. iowrite32(rval, priv->regs + TSI721_OBDMAC_INTE(ch));
  1082. if (priv->flags & TSI721_USING_MSIX)
  1083. return; /* Finished if we are in MSI-X mode */
  1084. /*
  1085. * For MSI and INTA interrupt signalling we need to disable next levels
  1086. */
  1087. /* Disable Device Channel Interrupt */
  1088. rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1089. rval &= ~TSI721_INT_OMSG_CHAN(ch);
  1090. iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
  1091. }
  1092. /**
  1093. * tsi721_add_outb_message - Add message to the Tsi721 outbound message queue
  1094. * @mport: Master port with outbound message queue
  1095. * @rdev: Target of outbound message
  1096. * @mbox: Outbound mailbox
  1097. * @buffer: Message to add to outbound queue
  1098. * @len: Length of message
  1099. */
  1100. static int
  1101. tsi721_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
  1102. void *buffer, size_t len)
  1103. {
  1104. struct tsi721_device *priv = mport->priv;
  1105. struct tsi721_omsg_desc *desc;
  1106. u32 tx_slot;
  1107. if (!priv->omsg_init[mbox] ||
  1108. len > TSI721_MSG_MAX_SIZE || len < 8)
  1109. return -EINVAL;
  1110. tx_slot = priv->omsg_ring[mbox].tx_slot;
  1111. /* Copy copy message into transfer buffer */
  1112. memcpy(priv->omsg_ring[mbox].omq_base[tx_slot], buffer, len);
  1113. if (len & 0x7)
  1114. len += 8;
  1115. /* Build descriptor associated with buffer */
  1116. desc = priv->omsg_ring[mbox].omd_base;
  1117. desc[tx_slot].type_id = cpu_to_le32((DTYPE4 << 29) | rdev->destid);
  1118. if (tx_slot % 4 == 0)
  1119. desc[tx_slot].type_id |= cpu_to_le32(TSI721_OMD_IOF);
  1120. desc[tx_slot].msg_info =
  1121. cpu_to_le32((mport->sys_size << 26) | (mbox << 22) |
  1122. (0xe << 12) | (len & 0xff8));
  1123. desc[tx_slot].bufptr_lo =
  1124. cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] &
  1125. 0xffffffff);
  1126. desc[tx_slot].bufptr_hi =
  1127. cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] >> 32);
  1128. priv->omsg_ring[mbox].wr_count++;
  1129. /* Go to next descriptor */
  1130. if (++priv->omsg_ring[mbox].tx_slot == priv->omsg_ring[mbox].size) {
  1131. priv->omsg_ring[mbox].tx_slot = 0;
  1132. /* Move through the ring link descriptor at the end */
  1133. priv->omsg_ring[mbox].wr_count++;
  1134. }
  1135. mb();
  1136. /* Set new write count value */
  1137. iowrite32(priv->omsg_ring[mbox].wr_count,
  1138. priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
  1139. ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
  1140. return 0;
  1141. }
  1142. /**
  1143. * tsi721_omsg_handler - Outbound Message Interrupt Handler
  1144. * @priv: pointer to tsi721 private data
  1145. * @ch: number of OB MSG channel to service
  1146. *
  1147. * Services channel interrupts from outbound messaging engine.
  1148. */
  1149. static void tsi721_omsg_handler(struct tsi721_device *priv, int ch)
  1150. {
  1151. u32 omsg_int;
  1152. spin_lock(&priv->omsg_ring[ch].lock);
  1153. omsg_int = ioread32(priv->regs + TSI721_OBDMAC_INT(ch));
  1154. if (omsg_int & TSI721_OBDMAC_INT_ST_FULL)
  1155. dev_info(&priv->pdev->dev,
  1156. "OB MBOX%d: Status FIFO is full\n", ch);
  1157. if (omsg_int & (TSI721_OBDMAC_INT_DONE | TSI721_OBDMAC_INT_IOF_DONE)) {
  1158. u32 srd_ptr;
  1159. u64 *sts_ptr, last_ptr = 0, prev_ptr = 0;
  1160. int i, j;
  1161. u32 tx_slot;
  1162. /*
  1163. * Find last successfully processed descriptor
  1164. */
  1165. /* Check and clear descriptor status FIFO entries */
  1166. srd_ptr = priv->omsg_ring[ch].sts_rdptr;
  1167. sts_ptr = priv->omsg_ring[ch].sts_base;
  1168. j = srd_ptr * 8;
  1169. while (sts_ptr[j]) {
  1170. for (i = 0; i < 8 && sts_ptr[j]; i++, j++) {
  1171. prev_ptr = last_ptr;
  1172. last_ptr = le64_to_cpu(sts_ptr[j]);
  1173. sts_ptr[j] = 0;
  1174. }
  1175. ++srd_ptr;
  1176. srd_ptr %= priv->omsg_ring[ch].sts_size;
  1177. j = srd_ptr * 8;
  1178. }
  1179. if (last_ptr == 0)
  1180. goto no_sts_update;
  1181. priv->omsg_ring[ch].sts_rdptr = srd_ptr;
  1182. iowrite32(srd_ptr, priv->regs + TSI721_OBDMAC_DSRP(ch));
  1183. if (!priv->mport->outb_msg[ch].mcback)
  1184. goto no_sts_update;
  1185. /* Inform upper layer about transfer completion */
  1186. tx_slot = (last_ptr - (u64)priv->omsg_ring[ch].omd_phys)/
  1187. sizeof(struct tsi721_omsg_desc);
  1188. /*
  1189. * Check if this is a Link Descriptor (LD).
  1190. * If yes, ignore LD and use descriptor processed
  1191. * before LD.
  1192. */
  1193. if (tx_slot == priv->omsg_ring[ch].size) {
  1194. if (prev_ptr)
  1195. tx_slot = (prev_ptr -
  1196. (u64)priv->omsg_ring[ch].omd_phys)/
  1197. sizeof(struct tsi721_omsg_desc);
  1198. else
  1199. goto no_sts_update;
  1200. }
  1201. /* Move slot index to the next message to be sent */
  1202. ++tx_slot;
  1203. if (tx_slot == priv->omsg_ring[ch].size)
  1204. tx_slot = 0;
  1205. BUG_ON(tx_slot >= priv->omsg_ring[ch].size);
  1206. priv->mport->outb_msg[ch].mcback(priv->mport,
  1207. priv->omsg_ring[ch].dev_id, ch,
  1208. tx_slot);
  1209. }
  1210. no_sts_update:
  1211. if (omsg_int & TSI721_OBDMAC_INT_ERROR) {
  1212. /*
  1213. * Outbound message operation aborted due to error,
  1214. * reinitialize OB MSG channel
  1215. */
  1216. dev_dbg(&priv->pdev->dev, "OB MSG ABORT ch_stat=%x\n",
  1217. ioread32(priv->regs + TSI721_OBDMAC_STS(ch)));
  1218. iowrite32(TSI721_OBDMAC_INT_ERROR,
  1219. priv->regs + TSI721_OBDMAC_INT(ch));
  1220. iowrite32(TSI721_OBDMAC_CTL_INIT,
  1221. priv->regs + TSI721_OBDMAC_CTL(ch));
  1222. ioread32(priv->regs + TSI721_OBDMAC_CTL(ch));
  1223. /* Inform upper level to clear all pending tx slots */
  1224. if (priv->mport->outb_msg[ch].mcback)
  1225. priv->mport->outb_msg[ch].mcback(priv->mport,
  1226. priv->omsg_ring[ch].dev_id, ch,
  1227. priv->omsg_ring[ch].tx_slot);
  1228. /* Synch tx_slot tracking */
  1229. iowrite32(priv->omsg_ring[ch].tx_slot,
  1230. priv->regs + TSI721_OBDMAC_DRDCNT(ch));
  1231. ioread32(priv->regs + TSI721_OBDMAC_DRDCNT(ch));
  1232. priv->omsg_ring[ch].wr_count = priv->omsg_ring[ch].tx_slot;
  1233. priv->omsg_ring[ch].sts_rdptr = 0;
  1234. }
  1235. /* Clear channel interrupts */
  1236. iowrite32(omsg_int, priv->regs + TSI721_OBDMAC_INT(ch));
  1237. if (!(priv->flags & TSI721_USING_MSIX)) {
  1238. u32 ch_inte;
  1239. /* Re-enable channel interrupts */
  1240. ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1241. ch_inte |= TSI721_INT_OMSG_CHAN(ch);
  1242. iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
  1243. }
  1244. spin_unlock(&priv->omsg_ring[ch].lock);
  1245. }
  1246. /**
  1247. * tsi721_open_outb_mbox - Initialize Tsi721 outbound mailbox
  1248. * @mport: Master port implementing Outbound Messaging Engine
  1249. * @dev_id: Device specific pointer to pass on event
  1250. * @mbox: Mailbox to open
  1251. * @entries: Number of entries in the outbound mailbox ring
  1252. */
  1253. static int tsi721_open_outb_mbox(struct rio_mport *mport, void *dev_id,
  1254. int mbox, int entries)
  1255. {
  1256. struct tsi721_device *priv = mport->priv;
  1257. struct tsi721_omsg_desc *bd_ptr;
  1258. int i, rc = 0;
  1259. if ((entries < TSI721_OMSGD_MIN_RING_SIZE) ||
  1260. (entries > (TSI721_OMSGD_RING_SIZE)) ||
  1261. (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
  1262. rc = -EINVAL;
  1263. goto out;
  1264. }
  1265. priv->omsg_ring[mbox].dev_id = dev_id;
  1266. priv->omsg_ring[mbox].size = entries;
  1267. priv->omsg_ring[mbox].sts_rdptr = 0;
  1268. spin_lock_init(&priv->omsg_ring[mbox].lock);
  1269. /* Outbound Msg Buffer allocation based on
  1270. the number of maximum descriptor entries */
  1271. for (i = 0; i < entries; i++) {
  1272. priv->omsg_ring[mbox].omq_base[i] =
  1273. dma_alloc_coherent(
  1274. &priv->pdev->dev, TSI721_MSG_BUFFER_SIZE,
  1275. &priv->omsg_ring[mbox].omq_phys[i],
  1276. GFP_KERNEL);
  1277. if (priv->omsg_ring[mbox].omq_base[i] == NULL) {
  1278. dev_dbg(&priv->pdev->dev,
  1279. "Unable to allocate OB MSG data buffer for"
  1280. " MBOX%d\n", mbox);
  1281. rc = -ENOMEM;
  1282. goto out_buf;
  1283. }
  1284. }
  1285. /* Outbound message descriptor allocation */
  1286. priv->omsg_ring[mbox].omd_base = dma_alloc_coherent(
  1287. &priv->pdev->dev,
  1288. (entries + 1) * sizeof(struct tsi721_omsg_desc),
  1289. &priv->omsg_ring[mbox].omd_phys, GFP_KERNEL);
  1290. if (priv->omsg_ring[mbox].omd_base == NULL) {
  1291. dev_dbg(&priv->pdev->dev,
  1292. "Unable to allocate OB MSG descriptor memory "
  1293. "for MBOX%d\n", mbox);
  1294. rc = -ENOMEM;
  1295. goto out_buf;
  1296. }
  1297. priv->omsg_ring[mbox].tx_slot = 0;
  1298. /* Outbound message descriptor status FIFO allocation */
  1299. priv->omsg_ring[mbox].sts_size = roundup_pow_of_two(entries + 1);
  1300. priv->omsg_ring[mbox].sts_base = dma_zalloc_coherent(&priv->pdev->dev,
  1301. priv->omsg_ring[mbox].sts_size *
  1302. sizeof(struct tsi721_dma_sts),
  1303. &priv->omsg_ring[mbox].sts_phys, GFP_KERNEL);
  1304. if (priv->omsg_ring[mbox].sts_base == NULL) {
  1305. dev_dbg(&priv->pdev->dev,
  1306. "Unable to allocate OB MSG descriptor status FIFO "
  1307. "for MBOX%d\n", mbox);
  1308. rc = -ENOMEM;
  1309. goto out_desc;
  1310. }
  1311. /*
  1312. * Configure Outbound Messaging Engine
  1313. */
  1314. /* Setup Outbound Message descriptor pointer */
  1315. iowrite32(((u64)priv->omsg_ring[mbox].omd_phys >> 32),
  1316. priv->regs + TSI721_OBDMAC_DPTRH(mbox));
  1317. iowrite32(((u64)priv->omsg_ring[mbox].omd_phys &
  1318. TSI721_OBDMAC_DPTRL_MASK),
  1319. priv->regs + TSI721_OBDMAC_DPTRL(mbox));
  1320. /* Setup Outbound Message descriptor status FIFO */
  1321. iowrite32(((u64)priv->omsg_ring[mbox].sts_phys >> 32),
  1322. priv->regs + TSI721_OBDMAC_DSBH(mbox));
  1323. iowrite32(((u64)priv->omsg_ring[mbox].sts_phys &
  1324. TSI721_OBDMAC_DSBL_MASK),
  1325. priv->regs + TSI721_OBDMAC_DSBL(mbox));
  1326. iowrite32(TSI721_DMAC_DSSZ_SIZE(priv->omsg_ring[mbox].sts_size),
  1327. priv->regs + (u32)TSI721_OBDMAC_DSSZ(mbox));
  1328. /* Enable interrupts */
  1329. #ifdef CONFIG_PCI_MSI
  1330. if (priv->flags & TSI721_USING_MSIX) {
  1331. /* Request interrupt service if we are in MSI-X mode */
  1332. rc = request_irq(
  1333. priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
  1334. tsi721_omsg_msix, 0,
  1335. priv->msix[TSI721_VECT_OMB0_DONE + mbox].irq_name,
  1336. (void *)mport);
  1337. if (rc) {
  1338. dev_dbg(&priv->pdev->dev,
  1339. "Unable to allocate MSI-X interrupt for "
  1340. "OBOX%d-DONE\n", mbox);
  1341. goto out_stat;
  1342. }
  1343. rc = request_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
  1344. tsi721_omsg_msix, 0,
  1345. priv->msix[TSI721_VECT_OMB0_INT + mbox].irq_name,
  1346. (void *)mport);
  1347. if (rc) {
  1348. dev_dbg(&priv->pdev->dev,
  1349. "Unable to allocate MSI-X interrupt for "
  1350. "MBOX%d-INT\n", mbox);
  1351. free_irq(
  1352. priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
  1353. (void *)mport);
  1354. goto out_stat;
  1355. }
  1356. }
  1357. #endif /* CONFIG_PCI_MSI */
  1358. tsi721_omsg_interrupt_enable(priv, mbox, TSI721_OBDMAC_INT_ALL);
  1359. /* Initialize Outbound Message descriptors ring */
  1360. bd_ptr = priv->omsg_ring[mbox].omd_base;
  1361. bd_ptr[entries].type_id = cpu_to_le32(DTYPE5 << 29);
  1362. bd_ptr[entries].msg_info = 0;
  1363. bd_ptr[entries].next_lo =
  1364. cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys &
  1365. TSI721_OBDMAC_DPTRL_MASK);
  1366. bd_ptr[entries].next_hi =
  1367. cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys >> 32);
  1368. priv->omsg_ring[mbox].wr_count = 0;
  1369. mb();
  1370. /* Initialize Outbound Message engine */
  1371. iowrite32(TSI721_OBDMAC_CTL_INIT, priv->regs + TSI721_OBDMAC_CTL(mbox));
  1372. ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
  1373. udelay(10);
  1374. priv->omsg_init[mbox] = 1;
  1375. return 0;
  1376. #ifdef CONFIG_PCI_MSI
  1377. out_stat:
  1378. dma_free_coherent(&priv->pdev->dev,
  1379. priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
  1380. priv->omsg_ring[mbox].sts_base,
  1381. priv->omsg_ring[mbox].sts_phys);
  1382. priv->omsg_ring[mbox].sts_base = NULL;
  1383. #endif /* CONFIG_PCI_MSI */
  1384. out_desc:
  1385. dma_free_coherent(&priv->pdev->dev,
  1386. (entries + 1) * sizeof(struct tsi721_omsg_desc),
  1387. priv->omsg_ring[mbox].omd_base,
  1388. priv->omsg_ring[mbox].omd_phys);
  1389. priv->omsg_ring[mbox].omd_base = NULL;
  1390. out_buf:
  1391. for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
  1392. if (priv->omsg_ring[mbox].omq_base[i]) {
  1393. dma_free_coherent(&priv->pdev->dev,
  1394. TSI721_MSG_BUFFER_SIZE,
  1395. priv->omsg_ring[mbox].omq_base[i],
  1396. priv->omsg_ring[mbox].omq_phys[i]);
  1397. priv->omsg_ring[mbox].omq_base[i] = NULL;
  1398. }
  1399. }
  1400. out:
  1401. return rc;
  1402. }
  1403. /**
  1404. * tsi721_close_outb_mbox - Close Tsi721 outbound mailbox
  1405. * @mport: Master port implementing the outbound message unit
  1406. * @mbox: Mailbox to close
  1407. */
  1408. static void tsi721_close_outb_mbox(struct rio_mport *mport, int mbox)
  1409. {
  1410. struct tsi721_device *priv = mport->priv;
  1411. u32 i;
  1412. if (!priv->omsg_init[mbox])
  1413. return;
  1414. priv->omsg_init[mbox] = 0;
  1415. /* Disable Interrupts */
  1416. tsi721_omsg_interrupt_disable(priv, mbox, TSI721_OBDMAC_INT_ALL);
  1417. #ifdef CONFIG_PCI_MSI
  1418. if (priv->flags & TSI721_USING_MSIX) {
  1419. free_irq(priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
  1420. (void *)mport);
  1421. free_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
  1422. (void *)mport);
  1423. }
  1424. #endif /* CONFIG_PCI_MSI */
  1425. /* Free OMSG Descriptor Status FIFO */
  1426. dma_free_coherent(&priv->pdev->dev,
  1427. priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
  1428. priv->omsg_ring[mbox].sts_base,
  1429. priv->omsg_ring[mbox].sts_phys);
  1430. priv->omsg_ring[mbox].sts_base = NULL;
  1431. /* Free OMSG descriptors */
  1432. dma_free_coherent(&priv->pdev->dev,
  1433. (priv->omsg_ring[mbox].size + 1) *
  1434. sizeof(struct tsi721_omsg_desc),
  1435. priv->omsg_ring[mbox].omd_base,
  1436. priv->omsg_ring[mbox].omd_phys);
  1437. priv->omsg_ring[mbox].omd_base = NULL;
  1438. /* Free message buffers */
  1439. for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
  1440. if (priv->omsg_ring[mbox].omq_base[i]) {
  1441. dma_free_coherent(&priv->pdev->dev,
  1442. TSI721_MSG_BUFFER_SIZE,
  1443. priv->omsg_ring[mbox].omq_base[i],
  1444. priv->omsg_ring[mbox].omq_phys[i]);
  1445. priv->omsg_ring[mbox].omq_base[i] = NULL;
  1446. }
  1447. }
  1448. }
  1449. /**
  1450. * tsi721_imsg_handler - Inbound Message Interrupt Handler
  1451. * @priv: pointer to tsi721 private data
  1452. * @ch: inbound message channel number to service
  1453. *
  1454. * Services channel interrupts from inbound messaging engine.
  1455. */
  1456. static void tsi721_imsg_handler(struct tsi721_device *priv, int ch)
  1457. {
  1458. u32 mbox = ch - 4;
  1459. u32 imsg_int;
  1460. spin_lock(&priv->imsg_ring[mbox].lock);
  1461. imsg_int = ioread32(priv->regs + TSI721_IBDMAC_INT(ch));
  1462. if (imsg_int & TSI721_IBDMAC_INT_SRTO)
  1463. dev_info(&priv->pdev->dev, "IB MBOX%d SRIO timeout\n",
  1464. mbox);
  1465. if (imsg_int & TSI721_IBDMAC_INT_PC_ERROR)
  1466. dev_info(&priv->pdev->dev, "IB MBOX%d PCIe error\n",
  1467. mbox);
  1468. if (imsg_int & TSI721_IBDMAC_INT_FQ_LOW)
  1469. dev_info(&priv->pdev->dev,
  1470. "IB MBOX%d IB free queue low\n", mbox);
  1471. /* Clear IB channel interrupts */
  1472. iowrite32(imsg_int, priv->regs + TSI721_IBDMAC_INT(ch));
  1473. /* If an IB Msg is received notify the upper layer */
  1474. if (imsg_int & TSI721_IBDMAC_INT_DQ_RCV &&
  1475. priv->mport->inb_msg[mbox].mcback)
  1476. priv->mport->inb_msg[mbox].mcback(priv->mport,
  1477. priv->imsg_ring[mbox].dev_id, mbox, -1);
  1478. if (!(priv->flags & TSI721_USING_MSIX)) {
  1479. u32 ch_inte;
  1480. /* Re-enable channel interrupts */
  1481. ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1482. ch_inte |= TSI721_INT_IMSG_CHAN(ch);
  1483. iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
  1484. }
  1485. spin_unlock(&priv->imsg_ring[mbox].lock);
  1486. }
  1487. /**
  1488. * tsi721_open_inb_mbox - Initialize Tsi721 inbound mailbox
  1489. * @mport: Master port implementing the Inbound Messaging Engine
  1490. * @dev_id: Device specific pointer to pass on event
  1491. * @mbox: Mailbox to open
  1492. * @entries: Number of entries in the inbound mailbox ring
  1493. */
  1494. static int tsi721_open_inb_mbox(struct rio_mport *mport, void *dev_id,
  1495. int mbox, int entries)
  1496. {
  1497. struct tsi721_device *priv = mport->priv;
  1498. int ch = mbox + 4;
  1499. int i;
  1500. u64 *free_ptr;
  1501. int rc = 0;
  1502. if ((entries < TSI721_IMSGD_MIN_RING_SIZE) ||
  1503. (entries > TSI721_IMSGD_RING_SIZE) ||
  1504. (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
  1505. rc = -EINVAL;
  1506. goto out;
  1507. }
  1508. /* Initialize IB Messaging Ring */
  1509. priv->imsg_ring[mbox].dev_id = dev_id;
  1510. priv->imsg_ring[mbox].size = entries;
  1511. priv->imsg_ring[mbox].rx_slot = 0;
  1512. priv->imsg_ring[mbox].desc_rdptr = 0;
  1513. priv->imsg_ring[mbox].fq_wrptr = 0;
  1514. for (i = 0; i < priv->imsg_ring[mbox].size; i++)
  1515. priv->imsg_ring[mbox].imq_base[i] = NULL;
  1516. spin_lock_init(&priv->imsg_ring[mbox].lock);
  1517. /* Allocate buffers for incoming messages */
  1518. priv->imsg_ring[mbox].buf_base =
  1519. dma_alloc_coherent(&priv->pdev->dev,
  1520. entries * TSI721_MSG_BUFFER_SIZE,
  1521. &priv->imsg_ring[mbox].buf_phys,
  1522. GFP_KERNEL);
  1523. if (priv->imsg_ring[mbox].buf_base == NULL) {
  1524. dev_err(&priv->pdev->dev,
  1525. "Failed to allocate buffers for IB MBOX%d\n", mbox);
  1526. rc = -ENOMEM;
  1527. goto out;
  1528. }
  1529. /* Allocate memory for circular free list */
  1530. priv->imsg_ring[mbox].imfq_base =
  1531. dma_alloc_coherent(&priv->pdev->dev,
  1532. entries * 8,
  1533. &priv->imsg_ring[mbox].imfq_phys,
  1534. GFP_KERNEL);
  1535. if (priv->imsg_ring[mbox].imfq_base == NULL) {
  1536. dev_err(&priv->pdev->dev,
  1537. "Failed to allocate free queue for IB MBOX%d\n", mbox);
  1538. rc = -ENOMEM;
  1539. goto out_buf;
  1540. }
  1541. /* Allocate memory for Inbound message descriptors */
  1542. priv->imsg_ring[mbox].imd_base =
  1543. dma_alloc_coherent(&priv->pdev->dev,
  1544. entries * sizeof(struct tsi721_imsg_desc),
  1545. &priv->imsg_ring[mbox].imd_phys, GFP_KERNEL);
  1546. if (priv->imsg_ring[mbox].imd_base == NULL) {
  1547. dev_err(&priv->pdev->dev,
  1548. "Failed to allocate descriptor memory for IB MBOX%d\n",
  1549. mbox);
  1550. rc = -ENOMEM;
  1551. goto out_dma;
  1552. }
  1553. /* Fill free buffer pointer list */
  1554. free_ptr = priv->imsg_ring[mbox].imfq_base;
  1555. for (i = 0; i < entries; i++)
  1556. free_ptr[i] = cpu_to_le64(
  1557. (u64)(priv->imsg_ring[mbox].buf_phys) +
  1558. i * 0x1000);
  1559. mb();
  1560. /*
  1561. * For mapping of inbound SRIO Messages into appropriate queues we need
  1562. * to set Inbound Device ID register in the messaging engine. We do it
  1563. * once when first inbound mailbox is requested.
  1564. */
  1565. if (!(priv->flags & TSI721_IMSGID_SET)) {
  1566. iowrite32((u32)priv->mport->host_deviceid,
  1567. priv->regs + TSI721_IB_DEVID);
  1568. priv->flags |= TSI721_IMSGID_SET;
  1569. }
  1570. /*
  1571. * Configure Inbound Messaging channel (ch = mbox + 4)
  1572. */
  1573. /* Setup Inbound Message free queue */
  1574. iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys >> 32),
  1575. priv->regs + TSI721_IBDMAC_FQBH(ch));
  1576. iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys &
  1577. TSI721_IBDMAC_FQBL_MASK),
  1578. priv->regs+TSI721_IBDMAC_FQBL(ch));
  1579. iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
  1580. priv->regs + TSI721_IBDMAC_FQSZ(ch));
  1581. /* Setup Inbound Message descriptor queue */
  1582. iowrite32(((u64)priv->imsg_ring[mbox].imd_phys >> 32),
  1583. priv->regs + TSI721_IBDMAC_DQBH(ch));
  1584. iowrite32(((u32)priv->imsg_ring[mbox].imd_phys &
  1585. (u32)TSI721_IBDMAC_DQBL_MASK),
  1586. priv->regs+TSI721_IBDMAC_DQBL(ch));
  1587. iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
  1588. priv->regs + TSI721_IBDMAC_DQSZ(ch));
  1589. /* Enable interrupts */
  1590. #ifdef CONFIG_PCI_MSI
  1591. if (priv->flags & TSI721_USING_MSIX) {
  1592. /* Request interrupt service if we are in MSI-X mode */
  1593. rc = request_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
  1594. tsi721_imsg_msix, 0,
  1595. priv->msix[TSI721_VECT_IMB0_RCV + mbox].irq_name,
  1596. (void *)mport);
  1597. if (rc) {
  1598. dev_dbg(&priv->pdev->dev,
  1599. "Unable to allocate MSI-X interrupt for "
  1600. "IBOX%d-DONE\n", mbox);
  1601. goto out_desc;
  1602. }
  1603. rc = request_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
  1604. tsi721_imsg_msix, 0,
  1605. priv->msix[TSI721_VECT_IMB0_INT + mbox].irq_name,
  1606. (void *)mport);
  1607. if (rc) {
  1608. dev_dbg(&priv->pdev->dev,
  1609. "Unable to allocate MSI-X interrupt for "
  1610. "IBOX%d-INT\n", mbox);
  1611. free_irq(
  1612. priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
  1613. (void *)mport);
  1614. goto out_desc;
  1615. }
  1616. }
  1617. #endif /* CONFIG_PCI_MSI */
  1618. tsi721_imsg_interrupt_enable(priv, ch, TSI721_IBDMAC_INT_ALL);
  1619. /* Initialize Inbound Message Engine */
  1620. iowrite32(TSI721_IBDMAC_CTL_INIT, priv->regs + TSI721_IBDMAC_CTL(ch));
  1621. ioread32(priv->regs + TSI721_IBDMAC_CTL(ch));
  1622. udelay(10);
  1623. priv->imsg_ring[mbox].fq_wrptr = entries - 1;
  1624. iowrite32(entries - 1, priv->regs + TSI721_IBDMAC_FQWP(ch));
  1625. priv->imsg_init[mbox] = 1;
  1626. return 0;
  1627. #ifdef CONFIG_PCI_MSI
  1628. out_desc:
  1629. dma_free_coherent(&priv->pdev->dev,
  1630. priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
  1631. priv->imsg_ring[mbox].imd_base,
  1632. priv->imsg_ring[mbox].imd_phys);
  1633. priv->imsg_ring[mbox].imd_base = NULL;
  1634. #endif /* CONFIG_PCI_MSI */
  1635. out_dma:
  1636. dma_free_coherent(&priv->pdev->dev,
  1637. priv->imsg_ring[mbox].size * 8,
  1638. priv->imsg_ring[mbox].imfq_base,
  1639. priv->imsg_ring[mbox].imfq_phys);
  1640. priv->imsg_ring[mbox].imfq_base = NULL;
  1641. out_buf:
  1642. dma_free_coherent(&priv->pdev->dev,
  1643. priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
  1644. priv->imsg_ring[mbox].buf_base,
  1645. priv->imsg_ring[mbox].buf_phys);
  1646. priv->imsg_ring[mbox].buf_base = NULL;
  1647. out:
  1648. return rc;
  1649. }
  1650. /**
  1651. * tsi721_close_inb_mbox - Shut down Tsi721 inbound mailbox
  1652. * @mport: Master port implementing the Inbound Messaging Engine
  1653. * @mbox: Mailbox to close
  1654. */
  1655. static void tsi721_close_inb_mbox(struct rio_mport *mport, int mbox)
  1656. {
  1657. struct tsi721_device *priv = mport->priv;
  1658. u32 rx_slot;
  1659. int ch = mbox + 4;
  1660. if (!priv->imsg_init[mbox]) /* mbox isn't initialized yet */
  1661. return;
  1662. priv->imsg_init[mbox] = 0;
  1663. /* Disable Inbound Messaging Engine */
  1664. /* Disable Interrupts */
  1665. tsi721_imsg_interrupt_disable(priv, ch, TSI721_OBDMAC_INT_MASK);
  1666. #ifdef CONFIG_PCI_MSI
  1667. if (priv->flags & TSI721_USING_MSIX) {
  1668. free_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
  1669. (void *)mport);
  1670. free_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
  1671. (void *)mport);
  1672. }
  1673. #endif /* CONFIG_PCI_MSI */
  1674. /* Clear Inbound Buffer Queue */
  1675. for (rx_slot = 0; rx_slot < priv->imsg_ring[mbox].size; rx_slot++)
  1676. priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
  1677. /* Free memory allocated for message buffers */
  1678. dma_free_coherent(&priv->pdev->dev,
  1679. priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
  1680. priv->imsg_ring[mbox].buf_base,
  1681. priv->imsg_ring[mbox].buf_phys);
  1682. priv->imsg_ring[mbox].buf_base = NULL;
  1683. /* Free memory allocated for free pointr list */
  1684. dma_free_coherent(&priv->pdev->dev,
  1685. priv->imsg_ring[mbox].size * 8,
  1686. priv->imsg_ring[mbox].imfq_base,
  1687. priv->imsg_ring[mbox].imfq_phys);
  1688. priv->imsg_ring[mbox].imfq_base = NULL;
  1689. /* Free memory allocated for RX descriptors */
  1690. dma_free_coherent(&priv->pdev->dev,
  1691. priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
  1692. priv->imsg_ring[mbox].imd_base,
  1693. priv->imsg_ring[mbox].imd_phys);
  1694. priv->imsg_ring[mbox].imd_base = NULL;
  1695. }
  1696. /**
  1697. * tsi721_add_inb_buffer - Add buffer to the Tsi721 inbound message queue
  1698. * @mport: Master port implementing the Inbound Messaging Engine
  1699. * @mbox: Inbound mailbox number
  1700. * @buf: Buffer to add to inbound queue
  1701. */
  1702. static int tsi721_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
  1703. {
  1704. struct tsi721_device *priv = mport->priv;
  1705. u32 rx_slot;
  1706. int rc = 0;
  1707. rx_slot = priv->imsg_ring[mbox].rx_slot;
  1708. if (priv->imsg_ring[mbox].imq_base[rx_slot]) {
  1709. dev_err(&priv->pdev->dev,
  1710. "Error adding inbound buffer %d, buffer exists\n",
  1711. rx_slot);
  1712. rc = -EINVAL;
  1713. goto out;
  1714. }
  1715. priv->imsg_ring[mbox].imq_base[rx_slot] = buf;
  1716. if (++priv->imsg_ring[mbox].rx_slot == priv->imsg_ring[mbox].size)
  1717. priv->imsg_ring[mbox].rx_slot = 0;
  1718. out:
  1719. return rc;
  1720. }
  1721. /**
  1722. * tsi721_get_inb_message - Fetch inbound message from the Tsi721 MSG Queue
  1723. * @mport: Master port implementing the Inbound Messaging Engine
  1724. * @mbox: Inbound mailbox number
  1725. *
  1726. * Returns pointer to the message on success or NULL on failure.
  1727. */
  1728. static void *tsi721_get_inb_message(struct rio_mport *mport, int mbox)
  1729. {
  1730. struct tsi721_device *priv = mport->priv;
  1731. struct tsi721_imsg_desc *desc;
  1732. u32 rx_slot;
  1733. void *rx_virt = NULL;
  1734. u64 rx_phys;
  1735. void *buf = NULL;
  1736. u64 *free_ptr;
  1737. int ch = mbox + 4;
  1738. int msg_size;
  1739. if (!priv->imsg_init[mbox])
  1740. return NULL;
  1741. desc = priv->imsg_ring[mbox].imd_base;
  1742. desc += priv->imsg_ring[mbox].desc_rdptr;
  1743. if (!(le32_to_cpu(desc->msg_info) & TSI721_IMD_HO))
  1744. goto out;
  1745. rx_slot = priv->imsg_ring[mbox].rx_slot;
  1746. while (priv->imsg_ring[mbox].imq_base[rx_slot] == NULL) {
  1747. if (++rx_slot == priv->imsg_ring[mbox].size)
  1748. rx_slot = 0;
  1749. }
  1750. rx_phys = ((u64)le32_to_cpu(desc->bufptr_hi) << 32) |
  1751. le32_to_cpu(desc->bufptr_lo);
  1752. rx_virt = priv->imsg_ring[mbox].buf_base +
  1753. (rx_phys - (u64)priv->imsg_ring[mbox].buf_phys);
  1754. buf = priv->imsg_ring[mbox].imq_base[rx_slot];
  1755. msg_size = le32_to_cpu(desc->msg_info) & TSI721_IMD_BCOUNT;
  1756. if (msg_size == 0)
  1757. msg_size = RIO_MAX_MSG_SIZE;
  1758. memcpy(buf, rx_virt, msg_size);
  1759. priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
  1760. desc->msg_info &= cpu_to_le32(~TSI721_IMD_HO);
  1761. if (++priv->imsg_ring[mbox].desc_rdptr == priv->imsg_ring[mbox].size)
  1762. priv->imsg_ring[mbox].desc_rdptr = 0;
  1763. iowrite32(priv->imsg_ring[mbox].desc_rdptr,
  1764. priv->regs + TSI721_IBDMAC_DQRP(ch));
  1765. /* Return free buffer into the pointer list */
  1766. free_ptr = priv->imsg_ring[mbox].imfq_base;
  1767. free_ptr[priv->imsg_ring[mbox].fq_wrptr] = cpu_to_le64(rx_phys);
  1768. if (++priv->imsg_ring[mbox].fq_wrptr == priv->imsg_ring[mbox].size)
  1769. priv->imsg_ring[mbox].fq_wrptr = 0;
  1770. iowrite32(priv->imsg_ring[mbox].fq_wrptr,
  1771. priv->regs + TSI721_IBDMAC_FQWP(ch));
  1772. out:
  1773. return buf;
  1774. }
  1775. /**
  1776. * tsi721_messages_init - Initialization of Messaging Engine
  1777. * @priv: pointer to tsi721 private data
  1778. *
  1779. * Configures Tsi721 messaging engine.
  1780. */
  1781. static int tsi721_messages_init(struct tsi721_device *priv)
  1782. {
  1783. int ch;
  1784. iowrite32(0, priv->regs + TSI721_SMSG_ECC_LOG);
  1785. iowrite32(0, priv->regs + TSI721_RETRY_GEN_CNT);
  1786. iowrite32(0, priv->regs + TSI721_RETRY_RX_CNT);
  1787. /* Set SRIO Message Request/Response Timeout */
  1788. iowrite32(TSI721_RQRPTO_VAL, priv->regs + TSI721_RQRPTO);
  1789. /* Initialize Inbound Messaging Engine Registers */
  1790. for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++) {
  1791. /* Clear interrupt bits */
  1792. iowrite32(TSI721_IBDMAC_INT_MASK,
  1793. priv->regs + TSI721_IBDMAC_INT(ch));
  1794. /* Clear Status */
  1795. iowrite32(0, priv->regs + TSI721_IBDMAC_STS(ch));
  1796. iowrite32(TSI721_SMSG_ECC_COR_LOG_MASK,
  1797. priv->regs + TSI721_SMSG_ECC_COR_LOG(ch));
  1798. iowrite32(TSI721_SMSG_ECC_NCOR_MASK,
  1799. priv->regs + TSI721_SMSG_ECC_NCOR(ch));
  1800. }
  1801. return 0;
  1802. }
  1803. /**
  1804. * tsi721_disable_ints - disables all device interrupts
  1805. * @priv: pointer to tsi721 private data
  1806. */
  1807. static void tsi721_disable_ints(struct tsi721_device *priv)
  1808. {
  1809. int ch;
  1810. /* Disable all device level interrupts */
  1811. iowrite32(0, priv->regs + TSI721_DEV_INTE);
  1812. /* Disable all Device Channel interrupts */
  1813. iowrite32(0, priv->regs + TSI721_DEV_CHAN_INTE);
  1814. /* Disable all Inbound Msg Channel interrupts */
  1815. for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++)
  1816. iowrite32(0, priv->regs + TSI721_IBDMAC_INTE(ch));
  1817. /* Disable all Outbound Msg Channel interrupts */
  1818. for (ch = 0; ch < TSI721_OMSG_CHNUM; ch++)
  1819. iowrite32(0, priv->regs + TSI721_OBDMAC_INTE(ch));
  1820. /* Disable all general messaging interrupts */
  1821. iowrite32(0, priv->regs + TSI721_SMSG_INTE);
  1822. /* Disable all BDMA Channel interrupts */
  1823. for (ch = 0; ch < TSI721_DMA_MAXCH; ch++)
  1824. iowrite32(0,
  1825. priv->regs + TSI721_DMAC_BASE(ch) + TSI721_DMAC_INTE);
  1826. /* Disable all general BDMA interrupts */
  1827. iowrite32(0, priv->regs + TSI721_BDMA_INTE);
  1828. /* Disable all SRIO Channel interrupts */
  1829. for (ch = 0; ch < TSI721_SRIO_MAXCH; ch++)
  1830. iowrite32(0, priv->regs + TSI721_SR_CHINTE(ch));
  1831. /* Disable all general SR2PC interrupts */
  1832. iowrite32(0, priv->regs + TSI721_SR2PC_GEN_INTE);
  1833. /* Disable all PC2SR interrupts */
  1834. iowrite32(0, priv->regs + TSI721_PC2SR_INTE);
  1835. /* Disable all I2C interrupts */
  1836. iowrite32(0, priv->regs + TSI721_I2C_INT_ENABLE);
  1837. /* Disable SRIO MAC interrupts */
  1838. iowrite32(0, priv->regs + TSI721_RIO_EM_INT_ENABLE);
  1839. iowrite32(0, priv->regs + TSI721_RIO_EM_DEV_INT_EN);
  1840. }
  1841. /**
  1842. * tsi721_setup_mport - Setup Tsi721 as RapidIO subsystem master port
  1843. * @priv: pointer to tsi721 private data
  1844. *
  1845. * Configures Tsi721 as RapidIO master port.
  1846. */
  1847. static int tsi721_setup_mport(struct tsi721_device *priv)
  1848. {
  1849. struct pci_dev *pdev = priv->pdev;
  1850. int err = 0;
  1851. struct rio_ops *ops;
  1852. struct rio_mport *mport;
  1853. ops = kzalloc(sizeof(struct rio_ops), GFP_KERNEL);
  1854. if (!ops) {
  1855. dev_dbg(&pdev->dev, "Unable to allocate memory for rio_ops\n");
  1856. return -ENOMEM;
  1857. }
  1858. ops->lcread = tsi721_lcread;
  1859. ops->lcwrite = tsi721_lcwrite;
  1860. ops->cread = tsi721_cread_dma;
  1861. ops->cwrite = tsi721_cwrite_dma;
  1862. ops->dsend = tsi721_dsend;
  1863. ops->open_inb_mbox = tsi721_open_inb_mbox;
  1864. ops->close_inb_mbox = tsi721_close_inb_mbox;
  1865. ops->open_outb_mbox = tsi721_open_outb_mbox;
  1866. ops->close_outb_mbox = tsi721_close_outb_mbox;
  1867. ops->add_outb_message = tsi721_add_outb_message;
  1868. ops->add_inb_buffer = tsi721_add_inb_buffer;
  1869. ops->get_inb_message = tsi721_get_inb_message;
  1870. ops->map_inb = tsi721_rio_map_inb_mem;
  1871. ops->unmap_inb = tsi721_rio_unmap_inb_mem;
  1872. mport = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
  1873. if (!mport) {
  1874. kfree(ops);
  1875. dev_dbg(&pdev->dev, "Unable to allocate memory for mport\n");
  1876. return -ENOMEM;
  1877. }
  1878. mport->ops = ops;
  1879. mport->index = 0;
  1880. mport->sys_size = 0; /* small system */
  1881. mport->phy_type = RIO_PHY_SERIAL;
  1882. mport->priv = (void *)priv;
  1883. mport->phys_efptr = 0x100;
  1884. priv->mport = mport;
  1885. INIT_LIST_HEAD(&mport->dbells);
  1886. rio_init_dbell_res(&mport->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
  1887. rio_init_mbox_res(&mport->riores[RIO_INB_MBOX_RESOURCE], 0, 3);
  1888. rio_init_mbox_res(&mport->riores[RIO_OUTB_MBOX_RESOURCE], 0, 3);
  1889. snprintf(mport->name, RIO_MAX_MPORT_NAME, "%s(%s)",
  1890. dev_driver_string(&pdev->dev), dev_name(&pdev->dev));
  1891. /* Hook up interrupt handler */
  1892. #ifdef CONFIG_PCI_MSI
  1893. if (!tsi721_enable_msix(priv))
  1894. priv->flags |= TSI721_USING_MSIX;
  1895. else if (!pci_enable_msi(pdev))
  1896. priv->flags |= TSI721_USING_MSI;
  1897. else
  1898. dev_info(&pdev->dev,
  1899. "MSI/MSI-X is not available. Using legacy INTx.\n");
  1900. #endif /* CONFIG_PCI_MSI */
  1901. err = tsi721_request_irq(mport);
  1902. if (!err) {
  1903. tsi721_interrupts_init(priv);
  1904. ops->pwenable = tsi721_pw_enable;
  1905. } else {
  1906. dev_err(&pdev->dev, "Unable to get assigned PCI IRQ "
  1907. "vector %02X err=0x%x\n", pdev->irq, err);
  1908. goto err_exit;
  1909. }
  1910. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  1911. tsi721_register_dma(priv);
  1912. #endif
  1913. /* Enable SRIO link */
  1914. iowrite32(ioread32(priv->regs + TSI721_DEVCTL) |
  1915. TSI721_DEVCTL_SRBOOT_CMPL,
  1916. priv->regs + TSI721_DEVCTL);
  1917. rio_register_mport(mport);
  1918. if (mport->host_deviceid >= 0)
  1919. iowrite32(RIO_PORT_GEN_HOST | RIO_PORT_GEN_MASTER |
  1920. RIO_PORT_GEN_DISCOVERED,
  1921. priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
  1922. else
  1923. iowrite32(0, priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
  1924. return 0;
  1925. err_exit:
  1926. kfree(mport);
  1927. kfree(ops);
  1928. return err;
  1929. }
  1930. static int tsi721_probe(struct pci_dev *pdev,
  1931. const struct pci_device_id *id)
  1932. {
  1933. struct tsi721_device *priv;
  1934. int err;
  1935. priv = kzalloc(sizeof(struct tsi721_device), GFP_KERNEL);
  1936. if (priv == NULL) {
  1937. dev_err(&pdev->dev, "Failed to allocate memory for device\n");
  1938. err = -ENOMEM;
  1939. goto err_exit;
  1940. }
  1941. err = pci_enable_device(pdev);
  1942. if (err) {
  1943. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  1944. goto err_clean;
  1945. }
  1946. priv->pdev = pdev;
  1947. #ifdef DEBUG
  1948. {
  1949. int i;
  1950. for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
  1951. dev_dbg(&pdev->dev, "res[%d] @ 0x%llx (0x%lx, 0x%lx)\n",
  1952. i, (unsigned long long)pci_resource_start(pdev, i),
  1953. (unsigned long)pci_resource_len(pdev, i),
  1954. pci_resource_flags(pdev, i));
  1955. }
  1956. }
  1957. #endif
  1958. /*
  1959. * Verify BAR configuration
  1960. */
  1961. /* BAR_0 (registers) must be 512KB+ in 32-bit address space */
  1962. if (!(pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM) ||
  1963. pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM_64 ||
  1964. pci_resource_len(pdev, BAR_0) < TSI721_REG_SPACE_SIZE) {
  1965. dev_err(&pdev->dev,
  1966. "Missing or misconfigured CSR BAR0, aborting.\n");
  1967. err = -ENODEV;
  1968. goto err_disable_pdev;
  1969. }
  1970. /* BAR_1 (outbound doorbells) must be 16MB+ in 32-bit address space */
  1971. if (!(pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM) ||
  1972. pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM_64 ||
  1973. pci_resource_len(pdev, BAR_1) < TSI721_DB_WIN_SIZE) {
  1974. dev_err(&pdev->dev,
  1975. "Missing or misconfigured Doorbell BAR1, aborting.\n");
  1976. err = -ENODEV;
  1977. goto err_disable_pdev;
  1978. }
  1979. /*
  1980. * BAR_2 and BAR_4 (outbound translation) must be in 64-bit PCIe address
  1981. * space.
  1982. * NOTE: BAR_2 and BAR_4 are not used by this version of driver.
  1983. * It may be a good idea to keep them disabled using HW configuration
  1984. * to save PCI memory space.
  1985. */
  1986. if ((pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM) &&
  1987. (pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM_64)) {
  1988. dev_info(&pdev->dev, "Outbound BAR2 is not used but enabled.\n");
  1989. }
  1990. if ((pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM) &&
  1991. (pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM_64)) {
  1992. dev_info(&pdev->dev, "Outbound BAR4 is not used but enabled.\n");
  1993. }
  1994. err = pci_request_regions(pdev, DRV_NAME);
  1995. if (err) {
  1996. dev_err(&pdev->dev, "Cannot obtain PCI resources, "
  1997. "aborting.\n");
  1998. goto err_disable_pdev;
  1999. }
  2000. pci_set_master(pdev);
  2001. priv->regs = pci_ioremap_bar(pdev, BAR_0);
  2002. if (!priv->regs) {
  2003. dev_err(&pdev->dev,
  2004. "Unable to map device registers space, aborting\n");
  2005. err = -ENOMEM;
  2006. goto err_free_res;
  2007. }
  2008. priv->odb_base = pci_ioremap_bar(pdev, BAR_1);
  2009. if (!priv->odb_base) {
  2010. dev_err(&pdev->dev,
  2011. "Unable to map outbound doorbells space, aborting\n");
  2012. err = -ENOMEM;
  2013. goto err_unmap_bars;
  2014. }
  2015. /* Configure DMA attributes. */
  2016. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  2017. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2018. if (err) {
  2019. dev_info(&pdev->dev, "Unable to set DMA mask\n");
  2020. goto err_unmap_bars;
  2021. }
  2022. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
  2023. dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
  2024. } else {
  2025. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  2026. if (err)
  2027. dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
  2028. }
  2029. BUG_ON(!pci_is_pcie(pdev));
  2030. /* Clear "no snoop" and "relaxed ordering" bits, use default MRRS. */
  2031. pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
  2032. PCI_EXP_DEVCTL_READRQ | PCI_EXP_DEVCTL_RELAX_EN |
  2033. PCI_EXP_DEVCTL_NOSNOOP_EN,
  2034. 0x2 << MAX_READ_REQUEST_SZ_SHIFT);
  2035. /* Adjust PCIe completion timeout. */
  2036. pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL2, 0xf, 0x2);
  2037. /*
  2038. * FIXUP: correct offsets of MSI-X tables in the MSI-X Capability Block
  2039. */
  2040. pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0x01);
  2041. pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXTBL,
  2042. TSI721_MSIXTBL_OFFSET);
  2043. pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXPBA,
  2044. TSI721_MSIXPBA_OFFSET);
  2045. pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0);
  2046. /* End of FIXUP */
  2047. tsi721_disable_ints(priv);
  2048. tsi721_init_pc2sr_mapping(priv);
  2049. tsi721_init_sr2pc_mapping(priv);
  2050. if (tsi721_bdma_maint_init(priv)) {
  2051. dev_err(&pdev->dev, "BDMA initialization failed, aborting\n");
  2052. err = -ENOMEM;
  2053. goto err_unmap_bars;
  2054. }
  2055. err = tsi721_doorbell_init(priv);
  2056. if (err)
  2057. goto err_free_bdma;
  2058. tsi721_port_write_init(priv);
  2059. err = tsi721_messages_init(priv);
  2060. if (err)
  2061. goto err_free_consistent;
  2062. err = tsi721_setup_mport(priv);
  2063. if (err)
  2064. goto err_free_consistent;
  2065. return 0;
  2066. err_free_consistent:
  2067. tsi721_doorbell_free(priv);
  2068. err_free_bdma:
  2069. tsi721_bdma_maint_free(priv);
  2070. err_unmap_bars:
  2071. if (priv->regs)
  2072. iounmap(priv->regs);
  2073. if (priv->odb_base)
  2074. iounmap(priv->odb_base);
  2075. err_free_res:
  2076. pci_release_regions(pdev);
  2077. pci_clear_master(pdev);
  2078. err_disable_pdev:
  2079. pci_disable_device(pdev);
  2080. err_clean:
  2081. kfree(priv);
  2082. err_exit:
  2083. return err;
  2084. }
  2085. static DEFINE_PCI_DEVICE_TABLE(tsi721_pci_tbl) = {
  2086. { PCI_DEVICE(PCI_VENDOR_ID_IDT, PCI_DEVICE_ID_TSI721) },
  2087. { 0, } /* terminate list */
  2088. };
  2089. MODULE_DEVICE_TABLE(pci, tsi721_pci_tbl);
  2090. static struct pci_driver tsi721_driver = {
  2091. .name = "tsi721",
  2092. .id_table = tsi721_pci_tbl,
  2093. .probe = tsi721_probe,
  2094. };
  2095. static int __init tsi721_init(void)
  2096. {
  2097. return pci_register_driver(&tsi721_driver);
  2098. }
  2099. static void __exit tsi721_exit(void)
  2100. {
  2101. pci_unregister_driver(&tsi721_driver);
  2102. }
  2103. device_initcall(tsi721_init);