tsi721.c 66 KB

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