tsi721.c 66 KB

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