tsi721.c 66 KB

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