tsi721.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351
  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_zalloc_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. dev_dbg(&priv->pdev->dev, "Allocated IDB buffer @ %p (phys = %llx)\n",
  745. priv->idb_base, (unsigned long long)priv->idb_dma);
  746. iowrite32(TSI721_IDQ_SIZE_VAL(IDB_QSIZE),
  747. priv->regs + TSI721_IDQ_SIZE(IDB_QUEUE));
  748. iowrite32(((u64)priv->idb_dma >> 32),
  749. priv->regs + TSI721_IDQ_BASEU(IDB_QUEUE));
  750. iowrite32(((u64)priv->idb_dma & TSI721_IDQ_BASEL_ADDR),
  751. priv->regs + TSI721_IDQ_BASEL(IDB_QUEUE));
  752. /* Enable accepting all inbound doorbells */
  753. iowrite32(0, priv->regs + TSI721_IDQ_MASK(IDB_QUEUE));
  754. iowrite32(TSI721_IDQ_INIT, priv->regs + TSI721_IDQ_CTL(IDB_QUEUE));
  755. iowrite32(0, priv->regs + TSI721_IDQ_RP(IDB_QUEUE));
  756. return 0;
  757. }
  758. static void tsi721_doorbell_free(struct tsi721_device *priv)
  759. {
  760. if (priv->idb_base == NULL)
  761. return;
  762. /* Free buffer allocated for inbound doorbell queue */
  763. dma_free_coherent(&priv->pdev->dev, IDB_QSIZE * TSI721_IDB_ENTRY_SIZE,
  764. priv->idb_base, priv->idb_dma);
  765. priv->idb_base = NULL;
  766. }
  767. static int tsi721_bdma_ch_init(struct tsi721_device *priv, int chnum)
  768. {
  769. struct tsi721_dma_desc *bd_ptr;
  770. u64 *sts_ptr;
  771. dma_addr_t bd_phys, sts_phys;
  772. int sts_size;
  773. int bd_num = priv->bdma[chnum].bd_num;
  774. dev_dbg(&priv->pdev->dev, "Init Block DMA Engine, CH%d\n", chnum);
  775. /*
  776. * Initialize DMA channel for maintenance requests
  777. */
  778. /* Allocate space for DMA descriptors */
  779. bd_ptr = dma_zalloc_coherent(&priv->pdev->dev,
  780. bd_num * sizeof(struct tsi721_dma_desc),
  781. &bd_phys, GFP_KERNEL);
  782. if (!bd_ptr)
  783. return -ENOMEM;
  784. priv->bdma[chnum].bd_phys = bd_phys;
  785. priv->bdma[chnum].bd_base = bd_ptr;
  786. dev_dbg(&priv->pdev->dev, "DMA descriptors @ %p (phys = %llx)\n",
  787. bd_ptr, (unsigned long long)bd_phys);
  788. /* Allocate space for descriptor status FIFO */
  789. sts_size = (bd_num >= TSI721_DMA_MINSTSSZ) ?
  790. bd_num : TSI721_DMA_MINSTSSZ;
  791. sts_size = roundup_pow_of_two(sts_size);
  792. sts_ptr = dma_zalloc_coherent(&priv->pdev->dev,
  793. sts_size * sizeof(struct tsi721_dma_sts),
  794. &sts_phys, GFP_KERNEL);
  795. if (!sts_ptr) {
  796. /* Free space allocated for DMA descriptors */
  797. dma_free_coherent(&priv->pdev->dev,
  798. bd_num * sizeof(struct tsi721_dma_desc),
  799. bd_ptr, bd_phys);
  800. priv->bdma[chnum].bd_base = NULL;
  801. return -ENOMEM;
  802. }
  803. priv->bdma[chnum].sts_phys = sts_phys;
  804. priv->bdma[chnum].sts_base = sts_ptr;
  805. priv->bdma[chnum].sts_size = sts_size;
  806. dev_dbg(&priv->pdev->dev,
  807. "desc status FIFO @ %p (phys = %llx) size=0x%x\n",
  808. sts_ptr, (unsigned long long)sts_phys, sts_size);
  809. /* Initialize DMA descriptors ring */
  810. bd_ptr[bd_num - 1].type_id = cpu_to_le32(DTYPE3 << 29);
  811. bd_ptr[bd_num - 1].next_lo = cpu_to_le32((u64)bd_phys &
  812. TSI721_DMAC_DPTRL_MASK);
  813. bd_ptr[bd_num - 1].next_hi = cpu_to_le32((u64)bd_phys >> 32);
  814. /* Setup DMA descriptor pointers */
  815. iowrite32(((u64)bd_phys >> 32),
  816. priv->regs + TSI721_DMAC_DPTRH(chnum));
  817. iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
  818. priv->regs + TSI721_DMAC_DPTRL(chnum));
  819. /* Setup descriptor status FIFO */
  820. iowrite32(((u64)sts_phys >> 32),
  821. priv->regs + TSI721_DMAC_DSBH(chnum));
  822. iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
  823. priv->regs + TSI721_DMAC_DSBL(chnum));
  824. iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
  825. priv->regs + TSI721_DMAC_DSSZ(chnum));
  826. /* Clear interrupt bits */
  827. iowrite32(TSI721_DMAC_INT_ALL,
  828. priv->regs + TSI721_DMAC_INT(chnum));
  829. ioread32(priv->regs + TSI721_DMAC_INT(chnum));
  830. /* Toggle DMA channel initialization */
  831. iowrite32(TSI721_DMAC_CTL_INIT, priv->regs + TSI721_DMAC_CTL(chnum));
  832. ioread32(priv->regs + TSI721_DMAC_CTL(chnum));
  833. udelay(10);
  834. return 0;
  835. }
  836. static int tsi721_bdma_ch_free(struct tsi721_device *priv, int chnum)
  837. {
  838. u32 ch_stat;
  839. if (priv->bdma[chnum].bd_base == NULL)
  840. return 0;
  841. /* Check if DMA channel still running */
  842. ch_stat = ioread32(priv->regs + TSI721_DMAC_STS(chnum));
  843. if (ch_stat & TSI721_DMAC_STS_RUN)
  844. return -EFAULT;
  845. /* Put DMA channel into init state */
  846. iowrite32(TSI721_DMAC_CTL_INIT,
  847. priv->regs + TSI721_DMAC_CTL(chnum));
  848. /* Free space allocated for DMA descriptors */
  849. dma_free_coherent(&priv->pdev->dev,
  850. priv->bdma[chnum].bd_num * sizeof(struct tsi721_dma_desc),
  851. priv->bdma[chnum].bd_base, priv->bdma[chnum].bd_phys);
  852. priv->bdma[chnum].bd_base = NULL;
  853. /* Free space allocated for status FIFO */
  854. dma_free_coherent(&priv->pdev->dev,
  855. priv->bdma[chnum].sts_size * sizeof(struct tsi721_dma_sts),
  856. priv->bdma[chnum].sts_base, priv->bdma[chnum].sts_phys);
  857. priv->bdma[chnum].sts_base = NULL;
  858. return 0;
  859. }
  860. static int tsi721_bdma_init(struct tsi721_device *priv)
  861. {
  862. /* Initialize BDMA channel allocated for RapidIO maintenance read/write
  863. * request generation
  864. */
  865. priv->bdma[TSI721_DMACH_MAINT].bd_num = 2;
  866. if (tsi721_bdma_ch_init(priv, TSI721_DMACH_MAINT)) {
  867. dev_err(&priv->pdev->dev, "Unable to initialize maintenance DMA"
  868. " channel %d, aborting\n", TSI721_DMACH_MAINT);
  869. return -ENOMEM;
  870. }
  871. return 0;
  872. }
  873. static void tsi721_bdma_free(struct tsi721_device *priv)
  874. {
  875. tsi721_bdma_ch_free(priv, TSI721_DMACH_MAINT);
  876. }
  877. /* Enable Inbound Messaging Interrupts */
  878. static void
  879. tsi721_imsg_interrupt_enable(struct tsi721_device *priv, int ch,
  880. u32 inte_mask)
  881. {
  882. u32 rval;
  883. if (!inte_mask)
  884. return;
  885. /* Clear pending Inbound Messaging interrupts */
  886. iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
  887. /* Enable Inbound Messaging interrupts */
  888. rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
  889. iowrite32(rval | inte_mask, priv->regs + TSI721_IBDMAC_INTE(ch));
  890. if (priv->flags & TSI721_USING_MSIX)
  891. return; /* Finished if we are in MSI-X mode */
  892. /*
  893. * For MSI and INTA interrupt signalling we need to enable next levels
  894. */
  895. /* Enable Device Channel Interrupt */
  896. rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  897. iowrite32(rval | TSI721_INT_IMSG_CHAN(ch),
  898. priv->regs + TSI721_DEV_CHAN_INTE);
  899. }
  900. /* Disable Inbound Messaging Interrupts */
  901. static void
  902. tsi721_imsg_interrupt_disable(struct tsi721_device *priv, int ch,
  903. u32 inte_mask)
  904. {
  905. u32 rval;
  906. if (!inte_mask)
  907. return;
  908. /* Clear pending Inbound Messaging interrupts */
  909. iowrite32(inte_mask, priv->regs + TSI721_IBDMAC_INT(ch));
  910. /* Disable Inbound Messaging interrupts */
  911. rval = ioread32(priv->regs + TSI721_IBDMAC_INTE(ch));
  912. rval &= ~inte_mask;
  913. iowrite32(rval, priv->regs + TSI721_IBDMAC_INTE(ch));
  914. if (priv->flags & TSI721_USING_MSIX)
  915. return; /* Finished if we are in MSI-X mode */
  916. /*
  917. * For MSI and INTA interrupt signalling we need to disable next levels
  918. */
  919. /* Disable Device Channel Interrupt */
  920. rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  921. rval &= ~TSI721_INT_IMSG_CHAN(ch);
  922. iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
  923. }
  924. /* Enable Outbound Messaging interrupts */
  925. static void
  926. tsi721_omsg_interrupt_enable(struct tsi721_device *priv, int ch,
  927. u32 inte_mask)
  928. {
  929. u32 rval;
  930. if (!inte_mask)
  931. return;
  932. /* Clear pending Outbound Messaging interrupts */
  933. iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
  934. /* Enable Outbound Messaging channel interrupts */
  935. rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
  936. iowrite32(rval | inte_mask, priv->regs + TSI721_OBDMAC_INTE(ch));
  937. if (priv->flags & TSI721_USING_MSIX)
  938. return; /* Finished if we are in MSI-X mode */
  939. /*
  940. * For MSI and INTA interrupt signalling we need to enable next levels
  941. */
  942. /* Enable Device Channel Interrupt */
  943. rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  944. iowrite32(rval | TSI721_INT_OMSG_CHAN(ch),
  945. priv->regs + TSI721_DEV_CHAN_INTE);
  946. }
  947. /* Disable Outbound Messaging interrupts */
  948. static void
  949. tsi721_omsg_interrupt_disable(struct tsi721_device *priv, int ch,
  950. u32 inte_mask)
  951. {
  952. u32 rval;
  953. if (!inte_mask)
  954. return;
  955. /* Clear pending Outbound Messaging interrupts */
  956. iowrite32(inte_mask, priv->regs + TSI721_OBDMAC_INT(ch));
  957. /* Disable Outbound Messaging interrupts */
  958. rval = ioread32(priv->regs + TSI721_OBDMAC_INTE(ch));
  959. rval &= ~inte_mask;
  960. iowrite32(rval, priv->regs + TSI721_OBDMAC_INTE(ch));
  961. if (priv->flags & TSI721_USING_MSIX)
  962. return; /* Finished if we are in MSI-X mode */
  963. /*
  964. * For MSI and INTA interrupt signalling we need to disable next levels
  965. */
  966. /* Disable Device Channel Interrupt */
  967. rval = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  968. rval &= ~TSI721_INT_OMSG_CHAN(ch);
  969. iowrite32(rval, priv->regs + TSI721_DEV_CHAN_INTE);
  970. }
  971. /**
  972. * tsi721_add_outb_message - Add message to the Tsi721 outbound message queue
  973. * @mport: Master port with outbound message queue
  974. * @rdev: Target of outbound message
  975. * @mbox: Outbound mailbox
  976. * @buffer: Message to add to outbound queue
  977. * @len: Length of message
  978. */
  979. static int
  980. tsi721_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
  981. void *buffer, size_t len)
  982. {
  983. struct tsi721_device *priv = mport->priv;
  984. struct tsi721_omsg_desc *desc;
  985. u32 tx_slot;
  986. if (!priv->omsg_init[mbox] ||
  987. len > TSI721_MSG_MAX_SIZE || len < 8)
  988. return -EINVAL;
  989. tx_slot = priv->omsg_ring[mbox].tx_slot;
  990. /* Copy copy message into transfer buffer */
  991. memcpy(priv->omsg_ring[mbox].omq_base[tx_slot], buffer, len);
  992. if (len & 0x7)
  993. len += 8;
  994. /* Build descriptor associated with buffer */
  995. desc = priv->omsg_ring[mbox].omd_base;
  996. desc[tx_slot].type_id = cpu_to_le32((DTYPE4 << 29) | rdev->destid);
  997. if (tx_slot % 4 == 0)
  998. desc[tx_slot].type_id |= cpu_to_le32(TSI721_OMD_IOF);
  999. desc[tx_slot].msg_info =
  1000. cpu_to_le32((mport->sys_size << 26) | (mbox << 22) |
  1001. (0xe << 12) | (len & 0xff8));
  1002. desc[tx_slot].bufptr_lo =
  1003. cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] &
  1004. 0xffffffff);
  1005. desc[tx_slot].bufptr_hi =
  1006. cpu_to_le32((u64)priv->omsg_ring[mbox].omq_phys[tx_slot] >> 32);
  1007. priv->omsg_ring[mbox].wr_count++;
  1008. /* Go to next descriptor */
  1009. if (++priv->omsg_ring[mbox].tx_slot == priv->omsg_ring[mbox].size) {
  1010. priv->omsg_ring[mbox].tx_slot = 0;
  1011. /* Move through the ring link descriptor at the end */
  1012. priv->omsg_ring[mbox].wr_count++;
  1013. }
  1014. mb();
  1015. /* Set new write count value */
  1016. iowrite32(priv->omsg_ring[mbox].wr_count,
  1017. priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
  1018. ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
  1019. return 0;
  1020. }
  1021. /**
  1022. * tsi721_omsg_handler - Outbound Message Interrupt Handler
  1023. * @priv: pointer to tsi721 private data
  1024. * @ch: number of OB MSG channel to service
  1025. *
  1026. * Services channel interrupts from outbound messaging engine.
  1027. */
  1028. static void tsi721_omsg_handler(struct tsi721_device *priv, int ch)
  1029. {
  1030. u32 omsg_int;
  1031. spin_lock(&priv->omsg_ring[ch].lock);
  1032. omsg_int = ioread32(priv->regs + TSI721_OBDMAC_INT(ch));
  1033. if (omsg_int & TSI721_OBDMAC_INT_ST_FULL)
  1034. dev_info(&priv->pdev->dev,
  1035. "OB MBOX%d: Status FIFO is full\n", ch);
  1036. if (omsg_int & (TSI721_OBDMAC_INT_DONE | TSI721_OBDMAC_INT_IOF_DONE)) {
  1037. u32 srd_ptr;
  1038. u64 *sts_ptr, last_ptr = 0, prev_ptr = 0;
  1039. int i, j;
  1040. u32 tx_slot;
  1041. /*
  1042. * Find last successfully processed descriptor
  1043. */
  1044. /* Check and clear descriptor status FIFO entries */
  1045. srd_ptr = priv->omsg_ring[ch].sts_rdptr;
  1046. sts_ptr = priv->omsg_ring[ch].sts_base;
  1047. j = srd_ptr * 8;
  1048. while (sts_ptr[j]) {
  1049. for (i = 0; i < 8 && sts_ptr[j]; i++, j++) {
  1050. prev_ptr = last_ptr;
  1051. last_ptr = le64_to_cpu(sts_ptr[j]);
  1052. sts_ptr[j] = 0;
  1053. }
  1054. ++srd_ptr;
  1055. srd_ptr %= priv->omsg_ring[ch].sts_size;
  1056. j = srd_ptr * 8;
  1057. }
  1058. if (last_ptr == 0)
  1059. goto no_sts_update;
  1060. priv->omsg_ring[ch].sts_rdptr = srd_ptr;
  1061. iowrite32(srd_ptr, priv->regs + TSI721_OBDMAC_DSRP(ch));
  1062. if (!priv->mport->outb_msg[ch].mcback)
  1063. goto no_sts_update;
  1064. /* Inform upper layer about transfer completion */
  1065. tx_slot = (last_ptr - (u64)priv->omsg_ring[ch].omd_phys)/
  1066. sizeof(struct tsi721_omsg_desc);
  1067. /*
  1068. * Check if this is a Link Descriptor (LD).
  1069. * If yes, ignore LD and use descriptor processed
  1070. * before LD.
  1071. */
  1072. if (tx_slot == priv->omsg_ring[ch].size) {
  1073. if (prev_ptr)
  1074. tx_slot = (prev_ptr -
  1075. (u64)priv->omsg_ring[ch].omd_phys)/
  1076. sizeof(struct tsi721_omsg_desc);
  1077. else
  1078. goto no_sts_update;
  1079. }
  1080. /* Move slot index to the next message to be sent */
  1081. ++tx_slot;
  1082. if (tx_slot == priv->omsg_ring[ch].size)
  1083. tx_slot = 0;
  1084. BUG_ON(tx_slot >= priv->omsg_ring[ch].size);
  1085. priv->mport->outb_msg[ch].mcback(priv->mport,
  1086. priv->omsg_ring[ch].dev_id, ch,
  1087. tx_slot);
  1088. }
  1089. no_sts_update:
  1090. if (omsg_int & TSI721_OBDMAC_INT_ERROR) {
  1091. /*
  1092. * Outbound message operation aborted due to error,
  1093. * reinitialize OB MSG channel
  1094. */
  1095. dev_dbg(&priv->pdev->dev, "OB MSG ABORT ch_stat=%x\n",
  1096. ioread32(priv->regs + TSI721_OBDMAC_STS(ch)));
  1097. iowrite32(TSI721_OBDMAC_INT_ERROR,
  1098. priv->regs + TSI721_OBDMAC_INT(ch));
  1099. iowrite32(TSI721_OBDMAC_CTL_INIT,
  1100. priv->regs + TSI721_OBDMAC_CTL(ch));
  1101. ioread32(priv->regs + TSI721_OBDMAC_CTL(ch));
  1102. /* Inform upper level to clear all pending tx slots */
  1103. if (priv->mport->outb_msg[ch].mcback)
  1104. priv->mport->outb_msg[ch].mcback(priv->mport,
  1105. priv->omsg_ring[ch].dev_id, ch,
  1106. priv->omsg_ring[ch].tx_slot);
  1107. /* Synch tx_slot tracking */
  1108. iowrite32(priv->omsg_ring[ch].tx_slot,
  1109. priv->regs + TSI721_OBDMAC_DRDCNT(ch));
  1110. ioread32(priv->regs + TSI721_OBDMAC_DRDCNT(ch));
  1111. priv->omsg_ring[ch].wr_count = priv->omsg_ring[ch].tx_slot;
  1112. priv->omsg_ring[ch].sts_rdptr = 0;
  1113. }
  1114. /* Clear channel interrupts */
  1115. iowrite32(omsg_int, priv->regs + TSI721_OBDMAC_INT(ch));
  1116. if (!(priv->flags & TSI721_USING_MSIX)) {
  1117. u32 ch_inte;
  1118. /* Re-enable channel interrupts */
  1119. ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1120. ch_inte |= TSI721_INT_OMSG_CHAN(ch);
  1121. iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
  1122. }
  1123. spin_unlock(&priv->omsg_ring[ch].lock);
  1124. }
  1125. /**
  1126. * tsi721_open_outb_mbox - Initialize Tsi721 outbound mailbox
  1127. * @mport: Master port implementing Outbound Messaging Engine
  1128. * @dev_id: Device specific pointer to pass on event
  1129. * @mbox: Mailbox to open
  1130. * @entries: Number of entries in the outbound mailbox ring
  1131. */
  1132. static int tsi721_open_outb_mbox(struct rio_mport *mport, void *dev_id,
  1133. int mbox, int entries)
  1134. {
  1135. struct tsi721_device *priv = mport->priv;
  1136. struct tsi721_omsg_desc *bd_ptr;
  1137. int i, rc = 0;
  1138. if ((entries < TSI721_OMSGD_MIN_RING_SIZE) ||
  1139. (entries > (TSI721_OMSGD_RING_SIZE)) ||
  1140. (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
  1141. rc = -EINVAL;
  1142. goto out;
  1143. }
  1144. priv->omsg_ring[mbox].dev_id = dev_id;
  1145. priv->omsg_ring[mbox].size = entries;
  1146. priv->omsg_ring[mbox].sts_rdptr = 0;
  1147. spin_lock_init(&priv->omsg_ring[mbox].lock);
  1148. /* Outbound Msg Buffer allocation based on
  1149. the number of maximum descriptor entries */
  1150. for (i = 0; i < entries; i++) {
  1151. priv->omsg_ring[mbox].omq_base[i] =
  1152. dma_alloc_coherent(
  1153. &priv->pdev->dev, TSI721_MSG_BUFFER_SIZE,
  1154. &priv->omsg_ring[mbox].omq_phys[i],
  1155. GFP_KERNEL);
  1156. if (priv->omsg_ring[mbox].omq_base[i] == NULL) {
  1157. dev_dbg(&priv->pdev->dev,
  1158. "Unable to allocate OB MSG data buffer for"
  1159. " MBOX%d\n", mbox);
  1160. rc = -ENOMEM;
  1161. goto out_buf;
  1162. }
  1163. }
  1164. /* Outbound message descriptor allocation */
  1165. priv->omsg_ring[mbox].omd_base = dma_alloc_coherent(
  1166. &priv->pdev->dev,
  1167. (entries + 1) * sizeof(struct tsi721_omsg_desc),
  1168. &priv->omsg_ring[mbox].omd_phys, GFP_KERNEL);
  1169. if (priv->omsg_ring[mbox].omd_base == NULL) {
  1170. dev_dbg(&priv->pdev->dev,
  1171. "Unable to allocate OB MSG descriptor memory "
  1172. "for MBOX%d\n", mbox);
  1173. rc = -ENOMEM;
  1174. goto out_buf;
  1175. }
  1176. priv->omsg_ring[mbox].tx_slot = 0;
  1177. /* Outbound message descriptor status FIFO allocation */
  1178. priv->omsg_ring[mbox].sts_size = roundup_pow_of_two(entries + 1);
  1179. priv->omsg_ring[mbox].sts_base = dma_zalloc_coherent(&priv->pdev->dev,
  1180. priv->omsg_ring[mbox].sts_size *
  1181. sizeof(struct tsi721_dma_sts),
  1182. &priv->omsg_ring[mbox].sts_phys, GFP_KERNEL);
  1183. if (priv->omsg_ring[mbox].sts_base == NULL) {
  1184. dev_dbg(&priv->pdev->dev,
  1185. "Unable to allocate OB MSG descriptor status FIFO "
  1186. "for MBOX%d\n", mbox);
  1187. rc = -ENOMEM;
  1188. goto out_desc;
  1189. }
  1190. /*
  1191. * Configure Outbound Messaging Engine
  1192. */
  1193. /* Setup Outbound Message descriptor pointer */
  1194. iowrite32(((u64)priv->omsg_ring[mbox].omd_phys >> 32),
  1195. priv->regs + TSI721_OBDMAC_DPTRH(mbox));
  1196. iowrite32(((u64)priv->omsg_ring[mbox].omd_phys &
  1197. TSI721_OBDMAC_DPTRL_MASK),
  1198. priv->regs + TSI721_OBDMAC_DPTRL(mbox));
  1199. /* Setup Outbound Message descriptor status FIFO */
  1200. iowrite32(((u64)priv->omsg_ring[mbox].sts_phys >> 32),
  1201. priv->regs + TSI721_OBDMAC_DSBH(mbox));
  1202. iowrite32(((u64)priv->omsg_ring[mbox].sts_phys &
  1203. TSI721_OBDMAC_DSBL_MASK),
  1204. priv->regs + TSI721_OBDMAC_DSBL(mbox));
  1205. iowrite32(TSI721_DMAC_DSSZ_SIZE(priv->omsg_ring[mbox].sts_size),
  1206. priv->regs + (u32)TSI721_OBDMAC_DSSZ(mbox));
  1207. /* Enable interrupts */
  1208. #ifdef CONFIG_PCI_MSI
  1209. if (priv->flags & TSI721_USING_MSIX) {
  1210. /* Request interrupt service if we are in MSI-X mode */
  1211. rc = request_irq(
  1212. priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
  1213. tsi721_omsg_msix, 0,
  1214. priv->msix[TSI721_VECT_OMB0_DONE + mbox].irq_name,
  1215. (void *)mport);
  1216. if (rc) {
  1217. dev_dbg(&priv->pdev->dev,
  1218. "Unable to allocate MSI-X interrupt for "
  1219. "OBOX%d-DONE\n", mbox);
  1220. goto out_stat;
  1221. }
  1222. rc = request_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
  1223. tsi721_omsg_msix, 0,
  1224. priv->msix[TSI721_VECT_OMB0_INT + mbox].irq_name,
  1225. (void *)mport);
  1226. if (rc) {
  1227. dev_dbg(&priv->pdev->dev,
  1228. "Unable to allocate MSI-X interrupt for "
  1229. "MBOX%d-INT\n", mbox);
  1230. free_irq(
  1231. priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
  1232. (void *)mport);
  1233. goto out_stat;
  1234. }
  1235. }
  1236. #endif /* CONFIG_PCI_MSI */
  1237. tsi721_omsg_interrupt_enable(priv, mbox, TSI721_OBDMAC_INT_ALL);
  1238. /* Initialize Outbound Message descriptors ring */
  1239. bd_ptr = priv->omsg_ring[mbox].omd_base;
  1240. bd_ptr[entries].type_id = cpu_to_le32(DTYPE5 << 29);
  1241. bd_ptr[entries].msg_info = 0;
  1242. bd_ptr[entries].next_lo =
  1243. cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys &
  1244. TSI721_OBDMAC_DPTRL_MASK);
  1245. bd_ptr[entries].next_hi =
  1246. cpu_to_le32((u64)priv->omsg_ring[mbox].omd_phys >> 32);
  1247. priv->omsg_ring[mbox].wr_count = 0;
  1248. mb();
  1249. /* Initialize Outbound Message engine */
  1250. iowrite32(TSI721_OBDMAC_CTL_INIT, priv->regs + TSI721_OBDMAC_CTL(mbox));
  1251. ioread32(priv->regs + TSI721_OBDMAC_DWRCNT(mbox));
  1252. udelay(10);
  1253. priv->omsg_init[mbox] = 1;
  1254. return 0;
  1255. #ifdef CONFIG_PCI_MSI
  1256. out_stat:
  1257. dma_free_coherent(&priv->pdev->dev,
  1258. priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
  1259. priv->omsg_ring[mbox].sts_base,
  1260. priv->omsg_ring[mbox].sts_phys);
  1261. priv->omsg_ring[mbox].sts_base = NULL;
  1262. #endif /* CONFIG_PCI_MSI */
  1263. out_desc:
  1264. dma_free_coherent(&priv->pdev->dev,
  1265. (entries + 1) * sizeof(struct tsi721_omsg_desc),
  1266. priv->omsg_ring[mbox].omd_base,
  1267. priv->omsg_ring[mbox].omd_phys);
  1268. priv->omsg_ring[mbox].omd_base = NULL;
  1269. out_buf:
  1270. for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
  1271. if (priv->omsg_ring[mbox].omq_base[i]) {
  1272. dma_free_coherent(&priv->pdev->dev,
  1273. TSI721_MSG_BUFFER_SIZE,
  1274. priv->omsg_ring[mbox].omq_base[i],
  1275. priv->omsg_ring[mbox].omq_phys[i]);
  1276. priv->omsg_ring[mbox].omq_base[i] = NULL;
  1277. }
  1278. }
  1279. out:
  1280. return rc;
  1281. }
  1282. /**
  1283. * tsi721_close_outb_mbox - Close Tsi721 outbound mailbox
  1284. * @mport: Master port implementing the outbound message unit
  1285. * @mbox: Mailbox to close
  1286. */
  1287. static void tsi721_close_outb_mbox(struct rio_mport *mport, int mbox)
  1288. {
  1289. struct tsi721_device *priv = mport->priv;
  1290. u32 i;
  1291. if (!priv->omsg_init[mbox])
  1292. return;
  1293. priv->omsg_init[mbox] = 0;
  1294. /* Disable Interrupts */
  1295. tsi721_omsg_interrupt_disable(priv, mbox, TSI721_OBDMAC_INT_ALL);
  1296. #ifdef CONFIG_PCI_MSI
  1297. if (priv->flags & TSI721_USING_MSIX) {
  1298. free_irq(priv->msix[TSI721_VECT_OMB0_DONE + mbox].vector,
  1299. (void *)mport);
  1300. free_irq(priv->msix[TSI721_VECT_OMB0_INT + mbox].vector,
  1301. (void *)mport);
  1302. }
  1303. #endif /* CONFIG_PCI_MSI */
  1304. /* Free OMSG Descriptor Status FIFO */
  1305. dma_free_coherent(&priv->pdev->dev,
  1306. priv->omsg_ring[mbox].sts_size * sizeof(struct tsi721_dma_sts),
  1307. priv->omsg_ring[mbox].sts_base,
  1308. priv->omsg_ring[mbox].sts_phys);
  1309. priv->omsg_ring[mbox].sts_base = NULL;
  1310. /* Free OMSG descriptors */
  1311. dma_free_coherent(&priv->pdev->dev,
  1312. (priv->omsg_ring[mbox].size + 1) *
  1313. sizeof(struct tsi721_omsg_desc),
  1314. priv->omsg_ring[mbox].omd_base,
  1315. priv->omsg_ring[mbox].omd_phys);
  1316. priv->omsg_ring[mbox].omd_base = NULL;
  1317. /* Free message buffers */
  1318. for (i = 0; i < priv->omsg_ring[mbox].size; i++) {
  1319. if (priv->omsg_ring[mbox].omq_base[i]) {
  1320. dma_free_coherent(&priv->pdev->dev,
  1321. TSI721_MSG_BUFFER_SIZE,
  1322. priv->omsg_ring[mbox].omq_base[i],
  1323. priv->omsg_ring[mbox].omq_phys[i]);
  1324. priv->omsg_ring[mbox].omq_base[i] = NULL;
  1325. }
  1326. }
  1327. }
  1328. /**
  1329. * tsi721_imsg_handler - Inbound Message Interrupt Handler
  1330. * @priv: pointer to tsi721 private data
  1331. * @ch: inbound message channel number to service
  1332. *
  1333. * Services channel interrupts from inbound messaging engine.
  1334. */
  1335. static void tsi721_imsg_handler(struct tsi721_device *priv, int ch)
  1336. {
  1337. u32 mbox = ch - 4;
  1338. u32 imsg_int;
  1339. spin_lock(&priv->imsg_ring[mbox].lock);
  1340. imsg_int = ioread32(priv->regs + TSI721_IBDMAC_INT(ch));
  1341. if (imsg_int & TSI721_IBDMAC_INT_SRTO)
  1342. dev_info(&priv->pdev->dev, "IB MBOX%d SRIO timeout\n",
  1343. mbox);
  1344. if (imsg_int & TSI721_IBDMAC_INT_PC_ERROR)
  1345. dev_info(&priv->pdev->dev, "IB MBOX%d PCIe error\n",
  1346. mbox);
  1347. if (imsg_int & TSI721_IBDMAC_INT_FQ_LOW)
  1348. dev_info(&priv->pdev->dev,
  1349. "IB MBOX%d IB free queue low\n", mbox);
  1350. /* Clear IB channel interrupts */
  1351. iowrite32(imsg_int, priv->regs + TSI721_IBDMAC_INT(ch));
  1352. /* If an IB Msg is received notify the upper layer */
  1353. if (imsg_int & TSI721_IBDMAC_INT_DQ_RCV &&
  1354. priv->mport->inb_msg[mbox].mcback)
  1355. priv->mport->inb_msg[mbox].mcback(priv->mport,
  1356. priv->imsg_ring[mbox].dev_id, mbox, -1);
  1357. if (!(priv->flags & TSI721_USING_MSIX)) {
  1358. u32 ch_inte;
  1359. /* Re-enable channel interrupts */
  1360. ch_inte = ioread32(priv->regs + TSI721_DEV_CHAN_INTE);
  1361. ch_inte |= TSI721_INT_IMSG_CHAN(ch);
  1362. iowrite32(ch_inte, priv->regs + TSI721_DEV_CHAN_INTE);
  1363. }
  1364. spin_unlock(&priv->imsg_ring[mbox].lock);
  1365. }
  1366. /**
  1367. * tsi721_open_inb_mbox - Initialize Tsi721 inbound mailbox
  1368. * @mport: Master port implementing the Inbound Messaging Engine
  1369. * @dev_id: Device specific pointer to pass on event
  1370. * @mbox: Mailbox to open
  1371. * @entries: Number of entries in the inbound mailbox ring
  1372. */
  1373. static int tsi721_open_inb_mbox(struct rio_mport *mport, void *dev_id,
  1374. int mbox, int entries)
  1375. {
  1376. struct tsi721_device *priv = mport->priv;
  1377. int ch = mbox + 4;
  1378. int i;
  1379. u64 *free_ptr;
  1380. int rc = 0;
  1381. if ((entries < TSI721_IMSGD_MIN_RING_SIZE) ||
  1382. (entries > TSI721_IMSGD_RING_SIZE) ||
  1383. (!is_power_of_2(entries)) || mbox >= RIO_MAX_MBOX) {
  1384. rc = -EINVAL;
  1385. goto out;
  1386. }
  1387. /* Initialize IB Messaging Ring */
  1388. priv->imsg_ring[mbox].dev_id = dev_id;
  1389. priv->imsg_ring[mbox].size = entries;
  1390. priv->imsg_ring[mbox].rx_slot = 0;
  1391. priv->imsg_ring[mbox].desc_rdptr = 0;
  1392. priv->imsg_ring[mbox].fq_wrptr = 0;
  1393. for (i = 0; i < priv->imsg_ring[mbox].size; i++)
  1394. priv->imsg_ring[mbox].imq_base[i] = NULL;
  1395. spin_lock_init(&priv->imsg_ring[mbox].lock);
  1396. /* Allocate buffers for incoming messages */
  1397. priv->imsg_ring[mbox].buf_base =
  1398. dma_alloc_coherent(&priv->pdev->dev,
  1399. entries * TSI721_MSG_BUFFER_SIZE,
  1400. &priv->imsg_ring[mbox].buf_phys,
  1401. GFP_KERNEL);
  1402. if (priv->imsg_ring[mbox].buf_base == NULL) {
  1403. dev_err(&priv->pdev->dev,
  1404. "Failed to allocate buffers for IB MBOX%d\n", mbox);
  1405. rc = -ENOMEM;
  1406. goto out;
  1407. }
  1408. /* Allocate memory for circular free list */
  1409. priv->imsg_ring[mbox].imfq_base =
  1410. dma_alloc_coherent(&priv->pdev->dev,
  1411. entries * 8,
  1412. &priv->imsg_ring[mbox].imfq_phys,
  1413. GFP_KERNEL);
  1414. if (priv->imsg_ring[mbox].imfq_base == NULL) {
  1415. dev_err(&priv->pdev->dev,
  1416. "Failed to allocate free queue for IB MBOX%d\n", mbox);
  1417. rc = -ENOMEM;
  1418. goto out_buf;
  1419. }
  1420. /* Allocate memory for Inbound message descriptors */
  1421. priv->imsg_ring[mbox].imd_base =
  1422. dma_alloc_coherent(&priv->pdev->dev,
  1423. entries * sizeof(struct tsi721_imsg_desc),
  1424. &priv->imsg_ring[mbox].imd_phys, GFP_KERNEL);
  1425. if (priv->imsg_ring[mbox].imd_base == NULL) {
  1426. dev_err(&priv->pdev->dev,
  1427. "Failed to allocate descriptor memory for IB MBOX%d\n",
  1428. mbox);
  1429. rc = -ENOMEM;
  1430. goto out_dma;
  1431. }
  1432. /* Fill free buffer pointer list */
  1433. free_ptr = priv->imsg_ring[mbox].imfq_base;
  1434. for (i = 0; i < entries; i++)
  1435. free_ptr[i] = cpu_to_le64(
  1436. (u64)(priv->imsg_ring[mbox].buf_phys) +
  1437. i * 0x1000);
  1438. mb();
  1439. /*
  1440. * For mapping of inbound SRIO Messages into appropriate queues we need
  1441. * to set Inbound Device ID register in the messaging engine. We do it
  1442. * once when first inbound mailbox is requested.
  1443. */
  1444. if (!(priv->flags & TSI721_IMSGID_SET)) {
  1445. iowrite32((u32)priv->mport->host_deviceid,
  1446. priv->regs + TSI721_IB_DEVID);
  1447. priv->flags |= TSI721_IMSGID_SET;
  1448. }
  1449. /*
  1450. * Configure Inbound Messaging channel (ch = mbox + 4)
  1451. */
  1452. /* Setup Inbound Message free queue */
  1453. iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys >> 32),
  1454. priv->regs + TSI721_IBDMAC_FQBH(ch));
  1455. iowrite32(((u64)priv->imsg_ring[mbox].imfq_phys &
  1456. TSI721_IBDMAC_FQBL_MASK),
  1457. priv->regs+TSI721_IBDMAC_FQBL(ch));
  1458. iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
  1459. priv->regs + TSI721_IBDMAC_FQSZ(ch));
  1460. /* Setup Inbound Message descriptor queue */
  1461. iowrite32(((u64)priv->imsg_ring[mbox].imd_phys >> 32),
  1462. priv->regs + TSI721_IBDMAC_DQBH(ch));
  1463. iowrite32(((u32)priv->imsg_ring[mbox].imd_phys &
  1464. (u32)TSI721_IBDMAC_DQBL_MASK),
  1465. priv->regs+TSI721_IBDMAC_DQBL(ch));
  1466. iowrite32(TSI721_DMAC_DSSZ_SIZE(entries),
  1467. priv->regs + TSI721_IBDMAC_DQSZ(ch));
  1468. /* Enable interrupts */
  1469. #ifdef CONFIG_PCI_MSI
  1470. if (priv->flags & TSI721_USING_MSIX) {
  1471. /* Request interrupt service if we are in MSI-X mode */
  1472. rc = request_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
  1473. tsi721_imsg_msix, 0,
  1474. priv->msix[TSI721_VECT_IMB0_RCV + mbox].irq_name,
  1475. (void *)mport);
  1476. if (rc) {
  1477. dev_dbg(&priv->pdev->dev,
  1478. "Unable to allocate MSI-X interrupt for "
  1479. "IBOX%d-DONE\n", mbox);
  1480. goto out_desc;
  1481. }
  1482. rc = request_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
  1483. tsi721_imsg_msix, 0,
  1484. priv->msix[TSI721_VECT_IMB0_INT + mbox].irq_name,
  1485. (void *)mport);
  1486. if (rc) {
  1487. dev_dbg(&priv->pdev->dev,
  1488. "Unable to allocate MSI-X interrupt for "
  1489. "IBOX%d-INT\n", mbox);
  1490. free_irq(
  1491. priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
  1492. (void *)mport);
  1493. goto out_desc;
  1494. }
  1495. }
  1496. #endif /* CONFIG_PCI_MSI */
  1497. tsi721_imsg_interrupt_enable(priv, ch, TSI721_IBDMAC_INT_ALL);
  1498. /* Initialize Inbound Message Engine */
  1499. iowrite32(TSI721_IBDMAC_CTL_INIT, priv->regs + TSI721_IBDMAC_CTL(ch));
  1500. ioread32(priv->regs + TSI721_IBDMAC_CTL(ch));
  1501. udelay(10);
  1502. priv->imsg_ring[mbox].fq_wrptr = entries - 1;
  1503. iowrite32(entries - 1, priv->regs + TSI721_IBDMAC_FQWP(ch));
  1504. priv->imsg_init[mbox] = 1;
  1505. return 0;
  1506. #ifdef CONFIG_PCI_MSI
  1507. out_desc:
  1508. dma_free_coherent(&priv->pdev->dev,
  1509. priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
  1510. priv->imsg_ring[mbox].imd_base,
  1511. priv->imsg_ring[mbox].imd_phys);
  1512. priv->imsg_ring[mbox].imd_base = NULL;
  1513. #endif /* CONFIG_PCI_MSI */
  1514. out_dma:
  1515. dma_free_coherent(&priv->pdev->dev,
  1516. priv->imsg_ring[mbox].size * 8,
  1517. priv->imsg_ring[mbox].imfq_base,
  1518. priv->imsg_ring[mbox].imfq_phys);
  1519. priv->imsg_ring[mbox].imfq_base = NULL;
  1520. out_buf:
  1521. dma_free_coherent(&priv->pdev->dev,
  1522. priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
  1523. priv->imsg_ring[mbox].buf_base,
  1524. priv->imsg_ring[mbox].buf_phys);
  1525. priv->imsg_ring[mbox].buf_base = NULL;
  1526. out:
  1527. return rc;
  1528. }
  1529. /**
  1530. * tsi721_close_inb_mbox - Shut down Tsi721 inbound mailbox
  1531. * @mport: Master port implementing the Inbound Messaging Engine
  1532. * @mbox: Mailbox to close
  1533. */
  1534. static void tsi721_close_inb_mbox(struct rio_mport *mport, int mbox)
  1535. {
  1536. struct tsi721_device *priv = mport->priv;
  1537. u32 rx_slot;
  1538. int ch = mbox + 4;
  1539. if (!priv->imsg_init[mbox]) /* mbox isn't initialized yet */
  1540. return;
  1541. priv->imsg_init[mbox] = 0;
  1542. /* Disable Inbound Messaging Engine */
  1543. /* Disable Interrupts */
  1544. tsi721_imsg_interrupt_disable(priv, ch, TSI721_OBDMAC_INT_MASK);
  1545. #ifdef CONFIG_PCI_MSI
  1546. if (priv->flags & TSI721_USING_MSIX) {
  1547. free_irq(priv->msix[TSI721_VECT_IMB0_RCV + mbox].vector,
  1548. (void *)mport);
  1549. free_irq(priv->msix[TSI721_VECT_IMB0_INT + mbox].vector,
  1550. (void *)mport);
  1551. }
  1552. #endif /* CONFIG_PCI_MSI */
  1553. /* Clear Inbound Buffer Queue */
  1554. for (rx_slot = 0; rx_slot < priv->imsg_ring[mbox].size; rx_slot++)
  1555. priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
  1556. /* Free memory allocated for message buffers */
  1557. dma_free_coherent(&priv->pdev->dev,
  1558. priv->imsg_ring[mbox].size * TSI721_MSG_BUFFER_SIZE,
  1559. priv->imsg_ring[mbox].buf_base,
  1560. priv->imsg_ring[mbox].buf_phys);
  1561. priv->imsg_ring[mbox].buf_base = NULL;
  1562. /* Free memory allocated for free pointr list */
  1563. dma_free_coherent(&priv->pdev->dev,
  1564. priv->imsg_ring[mbox].size * 8,
  1565. priv->imsg_ring[mbox].imfq_base,
  1566. priv->imsg_ring[mbox].imfq_phys);
  1567. priv->imsg_ring[mbox].imfq_base = NULL;
  1568. /* Free memory allocated for RX descriptors */
  1569. dma_free_coherent(&priv->pdev->dev,
  1570. priv->imsg_ring[mbox].size * sizeof(struct tsi721_imsg_desc),
  1571. priv->imsg_ring[mbox].imd_base,
  1572. priv->imsg_ring[mbox].imd_phys);
  1573. priv->imsg_ring[mbox].imd_base = NULL;
  1574. }
  1575. /**
  1576. * tsi721_add_inb_buffer - Add buffer to the Tsi721 inbound message queue
  1577. * @mport: Master port implementing the Inbound Messaging Engine
  1578. * @mbox: Inbound mailbox number
  1579. * @buf: Buffer to add to inbound queue
  1580. */
  1581. static int tsi721_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
  1582. {
  1583. struct tsi721_device *priv = mport->priv;
  1584. u32 rx_slot;
  1585. int rc = 0;
  1586. rx_slot = priv->imsg_ring[mbox].rx_slot;
  1587. if (priv->imsg_ring[mbox].imq_base[rx_slot]) {
  1588. dev_err(&priv->pdev->dev,
  1589. "Error adding inbound buffer %d, buffer exists\n",
  1590. rx_slot);
  1591. rc = -EINVAL;
  1592. goto out;
  1593. }
  1594. priv->imsg_ring[mbox].imq_base[rx_slot] = buf;
  1595. if (++priv->imsg_ring[mbox].rx_slot == priv->imsg_ring[mbox].size)
  1596. priv->imsg_ring[mbox].rx_slot = 0;
  1597. out:
  1598. return rc;
  1599. }
  1600. /**
  1601. * tsi721_get_inb_message - Fetch inbound message from the Tsi721 MSG Queue
  1602. * @mport: Master port implementing the Inbound Messaging Engine
  1603. * @mbox: Inbound mailbox number
  1604. *
  1605. * Returns pointer to the message on success or NULL on failure.
  1606. */
  1607. static void *tsi721_get_inb_message(struct rio_mport *mport, int mbox)
  1608. {
  1609. struct tsi721_device *priv = mport->priv;
  1610. struct tsi721_imsg_desc *desc;
  1611. u32 rx_slot;
  1612. void *rx_virt = NULL;
  1613. u64 rx_phys;
  1614. void *buf = NULL;
  1615. u64 *free_ptr;
  1616. int ch = mbox + 4;
  1617. int msg_size;
  1618. if (!priv->imsg_init[mbox])
  1619. return NULL;
  1620. desc = priv->imsg_ring[mbox].imd_base;
  1621. desc += priv->imsg_ring[mbox].desc_rdptr;
  1622. if (!(le32_to_cpu(desc->msg_info) & TSI721_IMD_HO))
  1623. goto out;
  1624. rx_slot = priv->imsg_ring[mbox].rx_slot;
  1625. while (priv->imsg_ring[mbox].imq_base[rx_slot] == NULL) {
  1626. if (++rx_slot == priv->imsg_ring[mbox].size)
  1627. rx_slot = 0;
  1628. }
  1629. rx_phys = ((u64)le32_to_cpu(desc->bufptr_hi) << 32) |
  1630. le32_to_cpu(desc->bufptr_lo);
  1631. rx_virt = priv->imsg_ring[mbox].buf_base +
  1632. (rx_phys - (u64)priv->imsg_ring[mbox].buf_phys);
  1633. buf = priv->imsg_ring[mbox].imq_base[rx_slot];
  1634. msg_size = le32_to_cpu(desc->msg_info) & TSI721_IMD_BCOUNT;
  1635. if (msg_size == 0)
  1636. msg_size = RIO_MAX_MSG_SIZE;
  1637. memcpy(buf, rx_virt, msg_size);
  1638. priv->imsg_ring[mbox].imq_base[rx_slot] = NULL;
  1639. desc->msg_info &= cpu_to_le32(~TSI721_IMD_HO);
  1640. if (++priv->imsg_ring[mbox].desc_rdptr == priv->imsg_ring[mbox].size)
  1641. priv->imsg_ring[mbox].desc_rdptr = 0;
  1642. iowrite32(priv->imsg_ring[mbox].desc_rdptr,
  1643. priv->regs + TSI721_IBDMAC_DQRP(ch));
  1644. /* Return free buffer into the pointer list */
  1645. free_ptr = priv->imsg_ring[mbox].imfq_base;
  1646. free_ptr[priv->imsg_ring[mbox].fq_wrptr] = cpu_to_le64(rx_phys);
  1647. if (++priv->imsg_ring[mbox].fq_wrptr == priv->imsg_ring[mbox].size)
  1648. priv->imsg_ring[mbox].fq_wrptr = 0;
  1649. iowrite32(priv->imsg_ring[mbox].fq_wrptr,
  1650. priv->regs + TSI721_IBDMAC_FQWP(ch));
  1651. out:
  1652. return buf;
  1653. }
  1654. /**
  1655. * tsi721_messages_init - Initialization of Messaging Engine
  1656. * @priv: pointer to tsi721 private data
  1657. *
  1658. * Configures Tsi721 messaging engine.
  1659. */
  1660. static int tsi721_messages_init(struct tsi721_device *priv)
  1661. {
  1662. int ch;
  1663. iowrite32(0, priv->regs + TSI721_SMSG_ECC_LOG);
  1664. iowrite32(0, priv->regs + TSI721_RETRY_GEN_CNT);
  1665. iowrite32(0, priv->regs + TSI721_RETRY_RX_CNT);
  1666. /* Set SRIO Message Request/Response Timeout */
  1667. iowrite32(TSI721_RQRPTO_VAL, priv->regs + TSI721_RQRPTO);
  1668. /* Initialize Inbound Messaging Engine Registers */
  1669. for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++) {
  1670. /* Clear interrupt bits */
  1671. iowrite32(TSI721_IBDMAC_INT_MASK,
  1672. priv->regs + TSI721_IBDMAC_INT(ch));
  1673. /* Clear Status */
  1674. iowrite32(0, priv->regs + TSI721_IBDMAC_STS(ch));
  1675. iowrite32(TSI721_SMSG_ECC_COR_LOG_MASK,
  1676. priv->regs + TSI721_SMSG_ECC_COR_LOG(ch));
  1677. iowrite32(TSI721_SMSG_ECC_NCOR_MASK,
  1678. priv->regs + TSI721_SMSG_ECC_NCOR(ch));
  1679. }
  1680. return 0;
  1681. }
  1682. /**
  1683. * tsi721_disable_ints - disables all device interrupts
  1684. * @priv: pointer to tsi721 private data
  1685. */
  1686. static void tsi721_disable_ints(struct tsi721_device *priv)
  1687. {
  1688. int ch;
  1689. /* Disable all device level interrupts */
  1690. iowrite32(0, priv->regs + TSI721_DEV_INTE);
  1691. /* Disable all Device Channel interrupts */
  1692. iowrite32(0, priv->regs + TSI721_DEV_CHAN_INTE);
  1693. /* Disable all Inbound Msg Channel interrupts */
  1694. for (ch = 0; ch < TSI721_IMSG_CHNUM; ch++)
  1695. iowrite32(0, priv->regs + TSI721_IBDMAC_INTE(ch));
  1696. /* Disable all Outbound Msg Channel interrupts */
  1697. for (ch = 0; ch < TSI721_OMSG_CHNUM; ch++)
  1698. iowrite32(0, priv->regs + TSI721_OBDMAC_INTE(ch));
  1699. /* Disable all general messaging interrupts */
  1700. iowrite32(0, priv->regs + TSI721_SMSG_INTE);
  1701. /* Disable all BDMA Channel interrupts */
  1702. for (ch = 0; ch < TSI721_DMA_MAXCH; ch++)
  1703. iowrite32(0, priv->regs + TSI721_DMAC_INTE(ch));
  1704. /* Disable all general BDMA interrupts */
  1705. iowrite32(0, priv->regs + TSI721_BDMA_INTE);
  1706. /* Disable all SRIO Channel interrupts */
  1707. for (ch = 0; ch < TSI721_SRIO_MAXCH; ch++)
  1708. iowrite32(0, priv->regs + TSI721_SR_CHINTE(ch));
  1709. /* Disable all general SR2PC interrupts */
  1710. iowrite32(0, priv->regs + TSI721_SR2PC_GEN_INTE);
  1711. /* Disable all PC2SR interrupts */
  1712. iowrite32(0, priv->regs + TSI721_PC2SR_INTE);
  1713. /* Disable all I2C interrupts */
  1714. iowrite32(0, priv->regs + TSI721_I2C_INT_ENABLE);
  1715. /* Disable SRIO MAC interrupts */
  1716. iowrite32(0, priv->regs + TSI721_RIO_EM_INT_ENABLE);
  1717. iowrite32(0, priv->regs + TSI721_RIO_EM_DEV_INT_EN);
  1718. }
  1719. /**
  1720. * tsi721_setup_mport - Setup Tsi721 as RapidIO subsystem master port
  1721. * @priv: pointer to tsi721 private data
  1722. *
  1723. * Configures Tsi721 as RapidIO master port.
  1724. */
  1725. static int __devinit tsi721_setup_mport(struct tsi721_device *priv)
  1726. {
  1727. struct pci_dev *pdev = priv->pdev;
  1728. int err = 0;
  1729. struct rio_ops *ops;
  1730. struct rio_mport *mport;
  1731. ops = kzalloc(sizeof(struct rio_ops), GFP_KERNEL);
  1732. if (!ops) {
  1733. dev_dbg(&pdev->dev, "Unable to allocate memory for rio_ops\n");
  1734. return -ENOMEM;
  1735. }
  1736. ops->lcread = tsi721_lcread;
  1737. ops->lcwrite = tsi721_lcwrite;
  1738. ops->cread = tsi721_cread_dma;
  1739. ops->cwrite = tsi721_cwrite_dma;
  1740. ops->dsend = tsi721_dsend;
  1741. ops->open_inb_mbox = tsi721_open_inb_mbox;
  1742. ops->close_inb_mbox = tsi721_close_inb_mbox;
  1743. ops->open_outb_mbox = tsi721_open_outb_mbox;
  1744. ops->close_outb_mbox = tsi721_close_outb_mbox;
  1745. ops->add_outb_message = tsi721_add_outb_message;
  1746. ops->add_inb_buffer = tsi721_add_inb_buffer;
  1747. ops->get_inb_message = tsi721_get_inb_message;
  1748. mport = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
  1749. if (!mport) {
  1750. kfree(ops);
  1751. dev_dbg(&pdev->dev, "Unable to allocate memory for mport\n");
  1752. return -ENOMEM;
  1753. }
  1754. mport->ops = ops;
  1755. mport->index = 0;
  1756. mport->sys_size = 0; /* small system */
  1757. mport->phy_type = RIO_PHY_SERIAL;
  1758. mport->priv = (void *)priv;
  1759. mport->phys_efptr = 0x100;
  1760. INIT_LIST_HEAD(&mport->dbells);
  1761. rio_init_dbell_res(&mport->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
  1762. rio_init_mbox_res(&mport->riores[RIO_INB_MBOX_RESOURCE], 0, 0);
  1763. rio_init_mbox_res(&mport->riores[RIO_OUTB_MBOX_RESOURCE], 0, 0);
  1764. strcpy(mport->name, "Tsi721 mport");
  1765. /* Hook up interrupt handler */
  1766. #ifdef CONFIG_PCI_MSI
  1767. if (!tsi721_enable_msix(priv))
  1768. priv->flags |= TSI721_USING_MSIX;
  1769. else if (!pci_enable_msi(pdev))
  1770. priv->flags |= TSI721_USING_MSI;
  1771. else
  1772. dev_info(&pdev->dev,
  1773. "MSI/MSI-X is not available. Using legacy INTx.\n");
  1774. #endif /* CONFIG_PCI_MSI */
  1775. err = tsi721_request_irq(mport);
  1776. if (!err) {
  1777. tsi721_interrupts_init(priv);
  1778. ops->pwenable = tsi721_pw_enable;
  1779. } else
  1780. dev_err(&pdev->dev, "Unable to get assigned PCI IRQ "
  1781. "vector %02X err=0x%x\n", pdev->irq, err);
  1782. /* Enable SRIO link */
  1783. iowrite32(ioread32(priv->regs + TSI721_DEVCTL) |
  1784. TSI721_DEVCTL_SRBOOT_CMPL,
  1785. priv->regs + TSI721_DEVCTL);
  1786. rio_register_mport(mport);
  1787. priv->mport = mport;
  1788. if (mport->host_deviceid >= 0)
  1789. iowrite32(RIO_PORT_GEN_HOST | RIO_PORT_GEN_MASTER |
  1790. RIO_PORT_GEN_DISCOVERED,
  1791. priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
  1792. else
  1793. iowrite32(0, priv->regs + (0x100 + RIO_PORT_GEN_CTL_CSR));
  1794. return 0;
  1795. }
  1796. static int __devinit tsi721_probe(struct pci_dev *pdev,
  1797. const struct pci_device_id *id)
  1798. {
  1799. struct tsi721_device *priv;
  1800. int i;
  1801. int err;
  1802. u32 regval;
  1803. priv = kzalloc(sizeof(struct tsi721_device), GFP_KERNEL);
  1804. if (priv == NULL) {
  1805. dev_err(&pdev->dev, "Failed to allocate memory for device\n");
  1806. err = -ENOMEM;
  1807. goto err_exit;
  1808. }
  1809. err = pci_enable_device(pdev);
  1810. if (err) {
  1811. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  1812. goto err_clean;
  1813. }
  1814. priv->pdev = pdev;
  1815. #ifdef DEBUG
  1816. for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
  1817. dev_dbg(&pdev->dev, "res[%d] @ 0x%llx (0x%lx, 0x%lx)\n",
  1818. i, (unsigned long long)pci_resource_start(pdev, i),
  1819. (unsigned long)pci_resource_len(pdev, i),
  1820. pci_resource_flags(pdev, i));
  1821. }
  1822. #endif
  1823. /*
  1824. * Verify BAR configuration
  1825. */
  1826. /* BAR_0 (registers) must be 512KB+ in 32-bit address space */
  1827. if (!(pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM) ||
  1828. pci_resource_flags(pdev, BAR_0) & IORESOURCE_MEM_64 ||
  1829. pci_resource_len(pdev, BAR_0) < TSI721_REG_SPACE_SIZE) {
  1830. dev_err(&pdev->dev,
  1831. "Missing or misconfigured CSR BAR0, aborting.\n");
  1832. err = -ENODEV;
  1833. goto err_disable_pdev;
  1834. }
  1835. /* BAR_1 (outbound doorbells) must be 16MB+ in 32-bit address space */
  1836. if (!(pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM) ||
  1837. pci_resource_flags(pdev, BAR_1) & IORESOURCE_MEM_64 ||
  1838. pci_resource_len(pdev, BAR_1) < TSI721_DB_WIN_SIZE) {
  1839. dev_err(&pdev->dev,
  1840. "Missing or misconfigured Doorbell BAR1, aborting.\n");
  1841. err = -ENODEV;
  1842. goto err_disable_pdev;
  1843. }
  1844. /*
  1845. * BAR_2 and BAR_4 (outbound translation) must be in 64-bit PCIe address
  1846. * space.
  1847. * NOTE: BAR_2 and BAR_4 are not used by this version of driver.
  1848. * It may be a good idea to keep them disabled using HW configuration
  1849. * to save PCI memory space.
  1850. */
  1851. if ((pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM) &&
  1852. (pci_resource_flags(pdev, BAR_2) & IORESOURCE_MEM_64)) {
  1853. dev_info(&pdev->dev, "Outbound BAR2 is not used but enabled.\n");
  1854. }
  1855. if ((pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM) &&
  1856. (pci_resource_flags(pdev, BAR_4) & IORESOURCE_MEM_64)) {
  1857. dev_info(&pdev->dev, "Outbound BAR4 is not used but enabled.\n");
  1858. }
  1859. err = pci_request_regions(pdev, DRV_NAME);
  1860. if (err) {
  1861. dev_err(&pdev->dev, "Cannot obtain PCI resources, "
  1862. "aborting.\n");
  1863. goto err_disable_pdev;
  1864. }
  1865. pci_set_master(pdev);
  1866. priv->regs = pci_ioremap_bar(pdev, BAR_0);
  1867. if (!priv->regs) {
  1868. dev_err(&pdev->dev,
  1869. "Unable to map device registers space, aborting\n");
  1870. err = -ENOMEM;
  1871. goto err_free_res;
  1872. }
  1873. priv->odb_base = pci_ioremap_bar(pdev, BAR_1);
  1874. if (!priv->odb_base) {
  1875. dev_err(&pdev->dev,
  1876. "Unable to map outbound doorbells space, aborting\n");
  1877. err = -ENOMEM;
  1878. goto err_unmap_bars;
  1879. }
  1880. /* Configure DMA attributes. */
  1881. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  1882. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1883. dev_info(&pdev->dev, "Unable to set DMA mask\n");
  1884. goto err_unmap_bars;
  1885. }
  1886. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
  1887. dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
  1888. } else {
  1889. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1890. if (err)
  1891. dev_info(&pdev->dev, "Unable to set consistent DMA mask\n");
  1892. }
  1893. /* Clear "no snoop" and "relaxed ordering" bits. */
  1894. pci_read_config_dword(pdev, 0x40 + PCI_EXP_DEVCTL, &regval);
  1895. regval &= ~(PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN);
  1896. pci_write_config_dword(pdev, 0x40 + PCI_EXP_DEVCTL, regval);
  1897. /*
  1898. * FIXUP: correct offsets of MSI-X tables in the MSI-X Capability Block
  1899. */
  1900. pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0x01);
  1901. pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXTBL,
  1902. TSI721_MSIXTBL_OFFSET);
  1903. pci_write_config_dword(pdev, TSI721_PCIECFG_MSIXPBA,
  1904. TSI721_MSIXPBA_OFFSET);
  1905. pci_write_config_dword(pdev, TSI721_PCIECFG_EPCTL, 0);
  1906. /* End of FIXUP */
  1907. tsi721_disable_ints(priv);
  1908. tsi721_init_pc2sr_mapping(priv);
  1909. tsi721_init_sr2pc_mapping(priv);
  1910. if (tsi721_bdma_init(priv)) {
  1911. dev_err(&pdev->dev, "BDMA initialization failed, aborting\n");
  1912. err = -ENOMEM;
  1913. goto err_unmap_bars;
  1914. }
  1915. err = tsi721_doorbell_init(priv);
  1916. if (err)
  1917. goto err_free_bdma;
  1918. tsi721_port_write_init(priv);
  1919. err = tsi721_messages_init(priv);
  1920. if (err)
  1921. goto err_free_consistent;
  1922. err = tsi721_setup_mport(priv);
  1923. if (err)
  1924. goto err_free_consistent;
  1925. return 0;
  1926. err_free_consistent:
  1927. tsi721_doorbell_free(priv);
  1928. err_free_bdma:
  1929. tsi721_bdma_free(priv);
  1930. err_unmap_bars:
  1931. if (priv->regs)
  1932. iounmap(priv->regs);
  1933. if (priv->odb_base)
  1934. iounmap(priv->odb_base);
  1935. err_free_res:
  1936. pci_release_regions(pdev);
  1937. pci_clear_master(pdev);
  1938. err_disable_pdev:
  1939. pci_disable_device(pdev);
  1940. err_clean:
  1941. kfree(priv);
  1942. err_exit:
  1943. return err;
  1944. }
  1945. static DEFINE_PCI_DEVICE_TABLE(tsi721_pci_tbl) = {
  1946. { PCI_DEVICE(PCI_VENDOR_ID_IDT, PCI_DEVICE_ID_TSI721) },
  1947. { 0, } /* terminate list */
  1948. };
  1949. MODULE_DEVICE_TABLE(pci, tsi721_pci_tbl);
  1950. static struct pci_driver tsi721_driver = {
  1951. .name = "tsi721",
  1952. .id_table = tsi721_pci_tbl,
  1953. .probe = tsi721_probe,
  1954. };
  1955. static int __init tsi721_init(void)
  1956. {
  1957. return pci_register_driver(&tsi721_driver);
  1958. }
  1959. static void __exit tsi721_exit(void)
  1960. {
  1961. pci_unregister_driver(&tsi721_driver);
  1962. }
  1963. device_initcall(tsi721_init);