tsi721.c 67 KB

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