tsi721.c 70 KB

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