tsi721.c 66 KB

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