tsi721.c 67 KB

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