fsl_rmu.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /*
  2. * Freescale MPC85xx/MPC86xx RapidIO RMU support
  3. *
  4. * Copyright 2009 Sysgo AG
  5. * Thomas Moll <thomas.moll@sysgo.com>
  6. * - fixed maintenance access routines, check for aligned access
  7. *
  8. * Copyright 2009 Integrated Device Technology, Inc.
  9. * Alex Bounine <alexandre.bounine@idt.com>
  10. * - Added Port-Write message handling
  11. * - Added Machine Check exception handling
  12. *
  13. * Copyright (C) 2007, 2008, 2010, 2011 Freescale Semiconductor, Inc.
  14. * Zhang Wei <wei.zhang@freescale.com>
  15. * Lian Minghuan-B31939 <Minghuan.Lian@freescale.com>
  16. * Liu Gang <Gang.Liu@freescale.com>
  17. *
  18. * Copyright 2005 MontaVista Software, Inc.
  19. * Matt Porter <mporter@kernel.crashing.org>
  20. *
  21. * This program is free software; you can redistribute it and/or modify it
  22. * under the terms of the GNU General Public License as published by the
  23. * Free Software Foundation; either version 2 of the License, or (at your
  24. * option) any later version.
  25. */
  26. #include <linux/types.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/slab.h>
  32. #include "fsl_rio.h"
  33. #define GET_RMM_HANDLE(mport) \
  34. (((struct rio_priv *)(mport->priv))->rmm_handle)
  35. /* RapidIO definition irq, which read from OF-tree */
  36. #define IRQ_RIO_PW(m) (((struct fsl_rio_pw *)(m))->pwirq)
  37. #define IRQ_RIO_BELL(m) (((struct fsl_rio_dbell *)(m))->bellirq)
  38. #define IRQ_RIO_TX(m) (((struct fsl_rmu *)(GET_RMM_HANDLE(m)))->txirq)
  39. #define IRQ_RIO_RX(m) (((struct fsl_rmu *)(GET_RMM_HANDLE(m)))->rxirq)
  40. #define RIO_MIN_TX_RING_SIZE 2
  41. #define RIO_MAX_TX_RING_SIZE 2048
  42. #define RIO_MIN_RX_RING_SIZE 2
  43. #define RIO_MAX_RX_RING_SIZE 2048
  44. #define RIO_IPWMR_SEN 0x00100000
  45. #define RIO_IPWMR_QFIE 0x00000100
  46. #define RIO_IPWMR_EIE 0x00000020
  47. #define RIO_IPWMR_CQ 0x00000002
  48. #define RIO_IPWMR_PWE 0x00000001
  49. #define RIO_IPWSR_QF 0x00100000
  50. #define RIO_IPWSR_TE 0x00000080
  51. #define RIO_IPWSR_QFI 0x00000010
  52. #define RIO_IPWSR_PWD 0x00000008
  53. #define RIO_IPWSR_PWB 0x00000004
  54. #define RIO_EPWISR 0x10010
  55. /* EPWISR Error match value */
  56. #define RIO_EPWISR_PINT1 0x80000000
  57. #define RIO_EPWISR_PINT2 0x40000000
  58. #define RIO_EPWISR_MU 0x00000002
  59. #define RIO_EPWISR_PW 0x00000001
  60. #define IPWSR_CLEAR 0x98
  61. #define OMSR_CLEAR 0x1cb3
  62. #define IMSR_CLEAR 0x491
  63. #define IDSR_CLEAR 0x91
  64. #define ODSR_CLEAR 0x1c00
  65. #define LTLEECSR_ENABLE_ALL 0xFFC000FC
  66. #define RIO_LTLEECSR 0x060c
  67. #define RIO_IM0SR 0x64
  68. #define RIO_IM1SR 0x164
  69. #define RIO_OM0SR 0x4
  70. #define RIO_OM1SR 0x104
  71. #define RIO_DBELL_WIN_SIZE 0x1000
  72. #define RIO_MSG_OMR_MUI 0x00000002
  73. #define RIO_MSG_OSR_TE 0x00000080
  74. #define RIO_MSG_OSR_QOI 0x00000020
  75. #define RIO_MSG_OSR_QFI 0x00000010
  76. #define RIO_MSG_OSR_MUB 0x00000004
  77. #define RIO_MSG_OSR_EOMI 0x00000002
  78. #define RIO_MSG_OSR_QEI 0x00000001
  79. #define RIO_MSG_IMR_MI 0x00000002
  80. #define RIO_MSG_ISR_TE 0x00000080
  81. #define RIO_MSG_ISR_QFI 0x00000010
  82. #define RIO_MSG_ISR_DIQI 0x00000001
  83. #define RIO_MSG_DESC_SIZE 32
  84. #define RIO_MSG_BUFFER_SIZE 4096
  85. #define DOORBELL_DMR_DI 0x00000002
  86. #define DOORBELL_DSR_TE 0x00000080
  87. #define DOORBELL_DSR_QFI 0x00000010
  88. #define DOORBELL_DSR_DIQI 0x00000001
  89. #define DOORBELL_MESSAGE_SIZE 0x08
  90. struct rio_msg_regs {
  91. u32 omr;
  92. u32 osr;
  93. u32 pad1;
  94. u32 odqdpar;
  95. u32 pad2;
  96. u32 osar;
  97. u32 odpr;
  98. u32 odatr;
  99. u32 odcr;
  100. u32 pad3;
  101. u32 odqepar;
  102. u32 pad4[13];
  103. u32 imr;
  104. u32 isr;
  105. u32 pad5;
  106. u32 ifqdpar;
  107. u32 pad6;
  108. u32 ifqepar;
  109. };
  110. struct rio_dbell_regs {
  111. u32 odmr;
  112. u32 odsr;
  113. u32 pad1[4];
  114. u32 oddpr;
  115. u32 oddatr;
  116. u32 pad2[3];
  117. u32 odretcr;
  118. u32 pad3[12];
  119. u32 dmr;
  120. u32 dsr;
  121. u32 pad4;
  122. u32 dqdpar;
  123. u32 pad5;
  124. u32 dqepar;
  125. };
  126. struct rio_pw_regs {
  127. u32 pwmr;
  128. u32 pwsr;
  129. u32 epwqbar;
  130. u32 pwqbar;
  131. };
  132. struct rio_tx_desc {
  133. u32 pad1;
  134. u32 saddr;
  135. u32 dport;
  136. u32 dattr;
  137. u32 pad2;
  138. u32 pad3;
  139. u32 dwcnt;
  140. u32 pad4;
  141. };
  142. struct rio_msg_tx_ring {
  143. void *virt;
  144. dma_addr_t phys;
  145. void *virt_buffer[RIO_MAX_TX_RING_SIZE];
  146. dma_addr_t phys_buffer[RIO_MAX_TX_RING_SIZE];
  147. int tx_slot;
  148. int size;
  149. void *dev_id;
  150. };
  151. struct rio_msg_rx_ring {
  152. void *virt;
  153. dma_addr_t phys;
  154. void *virt_buffer[RIO_MAX_RX_RING_SIZE];
  155. int rx_slot;
  156. int size;
  157. void *dev_id;
  158. };
  159. struct fsl_rmu {
  160. struct rio_msg_regs __iomem *msg_regs;
  161. struct rio_msg_tx_ring msg_tx_ring;
  162. struct rio_msg_rx_ring msg_rx_ring;
  163. int txirq;
  164. int rxirq;
  165. };
  166. struct rio_dbell_msg {
  167. u16 pad1;
  168. u16 tid;
  169. u16 sid;
  170. u16 info;
  171. };
  172. /**
  173. * fsl_rio_tx_handler - MPC85xx outbound message interrupt handler
  174. * @irq: Linux interrupt number
  175. * @dev_instance: Pointer to interrupt-specific data
  176. *
  177. * Handles outbound message interrupts. Executes a register outbound
  178. * mailbox event handler and acks the interrupt occurrence.
  179. */
  180. static irqreturn_t
  181. fsl_rio_tx_handler(int irq, void *dev_instance)
  182. {
  183. int osr;
  184. struct rio_mport *port = (struct rio_mport *)dev_instance;
  185. struct fsl_rmu *rmu = GET_RMM_HANDLE(port);
  186. osr = in_be32(&rmu->msg_regs->osr);
  187. if (osr & RIO_MSG_OSR_TE) {
  188. pr_info("RIO: outbound message transmission error\n");
  189. out_be32(&rmu->msg_regs->osr, RIO_MSG_OSR_TE);
  190. goto out;
  191. }
  192. if (osr & RIO_MSG_OSR_QOI) {
  193. pr_info("RIO: outbound message queue overflow\n");
  194. out_be32(&rmu->msg_regs->osr, RIO_MSG_OSR_QOI);
  195. goto out;
  196. }
  197. if (osr & RIO_MSG_OSR_EOMI) {
  198. u32 dqp = in_be32(&rmu->msg_regs->odqdpar);
  199. int slot = (dqp - rmu->msg_tx_ring.phys) >> 5;
  200. if (port->outb_msg[0].mcback != NULL) {
  201. port->outb_msg[0].mcback(port, rmu->msg_tx_ring.dev_id,
  202. -1,
  203. slot);
  204. }
  205. /* Ack the end-of-message interrupt */
  206. out_be32(&rmu->msg_regs->osr, RIO_MSG_OSR_EOMI);
  207. }
  208. out:
  209. return IRQ_HANDLED;
  210. }
  211. /**
  212. * fsl_rio_rx_handler - MPC85xx inbound message interrupt handler
  213. * @irq: Linux interrupt number
  214. * @dev_instance: Pointer to interrupt-specific data
  215. *
  216. * Handles inbound message interrupts. Executes a registered inbound
  217. * mailbox event handler and acks the interrupt occurrence.
  218. */
  219. static irqreturn_t
  220. fsl_rio_rx_handler(int irq, void *dev_instance)
  221. {
  222. int isr;
  223. struct rio_mport *port = (struct rio_mport *)dev_instance;
  224. struct fsl_rmu *rmu = GET_RMM_HANDLE(port);
  225. isr = in_be32(&rmu->msg_regs->isr);
  226. if (isr & RIO_MSG_ISR_TE) {
  227. pr_info("RIO: inbound message reception error\n");
  228. out_be32((void *)&rmu->msg_regs->isr, RIO_MSG_ISR_TE);
  229. goto out;
  230. }
  231. /* XXX Need to check/dispatch until queue empty */
  232. if (isr & RIO_MSG_ISR_DIQI) {
  233. /*
  234. * Can receive messages for any mailbox/letter to that
  235. * mailbox destination. So, make the callback with an
  236. * unknown/invalid mailbox number argument.
  237. */
  238. if (port->inb_msg[0].mcback != NULL)
  239. port->inb_msg[0].mcback(port, rmu->msg_rx_ring.dev_id,
  240. -1,
  241. -1);
  242. /* Ack the queueing interrupt */
  243. out_be32(&rmu->msg_regs->isr, RIO_MSG_ISR_DIQI);
  244. }
  245. out:
  246. return IRQ_HANDLED;
  247. }
  248. /**
  249. * fsl_rio_dbell_handler - MPC85xx doorbell interrupt handler
  250. * @irq: Linux interrupt number
  251. * @dev_instance: Pointer to interrupt-specific data
  252. *
  253. * Handles doorbell interrupts. Parses a list of registered
  254. * doorbell event handlers and executes a matching event handler.
  255. */
  256. static irqreturn_t
  257. fsl_rio_dbell_handler(int irq, void *dev_instance)
  258. {
  259. int dsr;
  260. struct fsl_rio_dbell *fsl_dbell = (struct fsl_rio_dbell *)dev_instance;
  261. int i;
  262. dsr = in_be32(&fsl_dbell->dbell_regs->dsr);
  263. if (dsr & DOORBELL_DSR_TE) {
  264. pr_info("RIO: doorbell reception error\n");
  265. out_be32(&fsl_dbell->dbell_regs->dsr, DOORBELL_DSR_TE);
  266. goto out;
  267. }
  268. if (dsr & DOORBELL_DSR_QFI) {
  269. pr_info("RIO: doorbell queue full\n");
  270. out_be32(&fsl_dbell->dbell_regs->dsr, DOORBELL_DSR_QFI);
  271. }
  272. /* XXX Need to check/dispatch until queue empty */
  273. if (dsr & DOORBELL_DSR_DIQI) {
  274. struct rio_dbell_msg *dmsg =
  275. fsl_dbell->dbell_ring.virt +
  276. (in_be32(&fsl_dbell->dbell_regs->dqdpar) & 0xfff);
  277. struct rio_dbell *dbell;
  278. int found = 0;
  279. pr_debug
  280. ("RIO: processing doorbell,"
  281. " sid %2.2x tid %2.2x info %4.4x\n",
  282. dmsg->sid, dmsg->tid, dmsg->info);
  283. for (i = 0; i < MAX_PORT_NUM; i++) {
  284. if (fsl_dbell->mport[i]) {
  285. list_for_each_entry(dbell,
  286. &fsl_dbell->mport[i]->dbells, node) {
  287. if ((dbell->res->start
  288. <= dmsg->info)
  289. && (dbell->res->end
  290. >= dmsg->info)) {
  291. found = 1;
  292. break;
  293. }
  294. }
  295. if (found && dbell->dinb) {
  296. dbell->dinb(fsl_dbell->mport[i],
  297. dbell->dev_id, dmsg->sid,
  298. dmsg->tid,
  299. dmsg->info);
  300. break;
  301. }
  302. }
  303. }
  304. if (!found) {
  305. pr_debug
  306. ("RIO: spurious doorbell,"
  307. " sid %2.2x tid %2.2x info %4.4x\n",
  308. dmsg->sid, dmsg->tid,
  309. dmsg->info);
  310. }
  311. setbits32(&fsl_dbell->dbell_regs->dmr, DOORBELL_DMR_DI);
  312. out_be32(&fsl_dbell->dbell_regs->dsr, DOORBELL_DSR_DIQI);
  313. }
  314. out:
  315. return IRQ_HANDLED;
  316. }
  317. void msg_unit_error_handler(void)
  318. {
  319. /*XXX: Error recovery is not implemented, we just clear errors */
  320. out_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR), 0);
  321. out_be32((u32 *)(rmu_regs_win + RIO_IM0SR), IMSR_CLEAR);
  322. out_be32((u32 *)(rmu_regs_win + RIO_IM1SR), IMSR_CLEAR);
  323. out_be32((u32 *)(rmu_regs_win + RIO_OM0SR), OMSR_CLEAR);
  324. out_be32((u32 *)(rmu_regs_win + RIO_OM1SR), OMSR_CLEAR);
  325. out_be32(&dbell->dbell_regs->odsr, ODSR_CLEAR);
  326. out_be32(&dbell->dbell_regs->dsr, IDSR_CLEAR);
  327. out_be32(&pw->pw_regs->pwsr, IPWSR_CLEAR);
  328. }
  329. /**
  330. * fsl_rio_port_write_handler - MPC85xx port write interrupt handler
  331. * @irq: Linux interrupt number
  332. * @dev_instance: Pointer to interrupt-specific data
  333. *
  334. * Handles port write interrupts. Parses a list of registered
  335. * port write event handlers and executes a matching event handler.
  336. */
  337. static irqreturn_t
  338. fsl_rio_port_write_handler(int irq, void *dev_instance)
  339. {
  340. u32 ipwmr, ipwsr;
  341. struct fsl_rio_pw *pw = (struct fsl_rio_pw *)dev_instance;
  342. u32 epwisr, tmp;
  343. epwisr = in_be32(rio_regs_win + RIO_EPWISR);
  344. if (!(epwisr & RIO_EPWISR_PW))
  345. goto pw_done;
  346. ipwmr = in_be32(&pw->pw_regs->pwmr);
  347. ipwsr = in_be32(&pw->pw_regs->pwsr);
  348. #ifdef DEBUG_PW
  349. pr_debug("PW Int->IPWMR: 0x%08x IPWSR: 0x%08x (", ipwmr, ipwsr);
  350. if (ipwsr & RIO_IPWSR_QF)
  351. pr_debug(" QF");
  352. if (ipwsr & RIO_IPWSR_TE)
  353. pr_debug(" TE");
  354. if (ipwsr & RIO_IPWSR_QFI)
  355. pr_debug(" QFI");
  356. if (ipwsr & RIO_IPWSR_PWD)
  357. pr_debug(" PWD");
  358. if (ipwsr & RIO_IPWSR_PWB)
  359. pr_debug(" PWB");
  360. pr_debug(" )\n");
  361. #endif
  362. /* Schedule deferred processing if PW was received */
  363. if (ipwsr & RIO_IPWSR_QFI) {
  364. /* Save PW message (if there is room in FIFO),
  365. * otherwise discard it.
  366. */
  367. if (kfifo_avail(&pw->pw_fifo) >= RIO_PW_MSG_SIZE) {
  368. pw->port_write_msg.msg_count++;
  369. kfifo_in(&pw->pw_fifo, pw->port_write_msg.virt,
  370. RIO_PW_MSG_SIZE);
  371. } else {
  372. pw->port_write_msg.discard_count++;
  373. pr_debug("RIO: ISR Discarded Port-Write Msg(s) (%d)\n",
  374. pw->port_write_msg.discard_count);
  375. }
  376. /* Clear interrupt and issue Clear Queue command. This allows
  377. * another port-write to be received.
  378. */
  379. out_be32(&pw->pw_regs->pwsr, RIO_IPWSR_QFI);
  380. out_be32(&pw->pw_regs->pwmr, ipwmr | RIO_IPWMR_CQ);
  381. schedule_work(&pw->pw_work);
  382. }
  383. if ((ipwmr & RIO_IPWMR_EIE) && (ipwsr & RIO_IPWSR_TE)) {
  384. pw->port_write_msg.err_count++;
  385. pr_debug("RIO: Port-Write Transaction Err (%d)\n",
  386. pw->port_write_msg.err_count);
  387. /* Clear Transaction Error: port-write controller should be
  388. * disabled when clearing this error
  389. */
  390. out_be32(&pw->pw_regs->pwmr, ipwmr & ~RIO_IPWMR_PWE);
  391. out_be32(&pw->pw_regs->pwsr, RIO_IPWSR_TE);
  392. out_be32(&pw->pw_regs->pwmr, ipwmr);
  393. }
  394. if (ipwsr & RIO_IPWSR_PWD) {
  395. pw->port_write_msg.discard_count++;
  396. pr_debug("RIO: Port Discarded Port-Write Msg(s) (%d)\n",
  397. pw->port_write_msg.discard_count);
  398. out_be32(&pw->pw_regs->pwsr, RIO_IPWSR_PWD);
  399. }
  400. pw_done:
  401. if (epwisr & RIO_EPWISR_PINT1) {
  402. tmp = in_be32(rio_regs_win + RIO_LTLEDCSR);
  403. pr_debug("RIO_LTLEDCSR = 0x%x\n", tmp);
  404. fsl_rio_port_error_handler(0);
  405. }
  406. if (epwisr & RIO_EPWISR_PINT2) {
  407. tmp = in_be32(rio_regs_win + RIO_LTLEDCSR);
  408. pr_debug("RIO_LTLEDCSR = 0x%x\n", tmp);
  409. fsl_rio_port_error_handler(1);
  410. }
  411. if (epwisr & RIO_EPWISR_MU) {
  412. tmp = in_be32(rio_regs_win + RIO_LTLEDCSR);
  413. pr_debug("RIO_LTLEDCSR = 0x%x\n", tmp);
  414. msg_unit_error_handler();
  415. }
  416. return IRQ_HANDLED;
  417. }
  418. static void fsl_pw_dpc(struct work_struct *work)
  419. {
  420. struct fsl_rio_pw *pw = container_of(work, struct fsl_rio_pw, pw_work);
  421. u32 msg_buffer[RIO_PW_MSG_SIZE/sizeof(u32)];
  422. /*
  423. * Process port-write messages
  424. */
  425. while (kfifo_out_spinlocked(&pw->pw_fifo, (unsigned char *)msg_buffer,
  426. RIO_PW_MSG_SIZE, &pw->pw_fifo_lock)) {
  427. /* Process one message */
  428. #ifdef DEBUG_PW
  429. {
  430. u32 i;
  431. pr_debug("%s : Port-Write Message:", __func__);
  432. for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32); i++) {
  433. if ((i%4) == 0)
  434. pr_debug("\n0x%02x: 0x%08x", i*4,
  435. msg_buffer[i]);
  436. else
  437. pr_debug(" 0x%08x", msg_buffer[i]);
  438. }
  439. pr_debug("\n");
  440. }
  441. #endif
  442. /* Pass the port-write message to RIO core for processing */
  443. rio_inb_pwrite_handler((union rio_pw_msg *)msg_buffer);
  444. }
  445. }
  446. /**
  447. * fsl_rio_pw_enable - enable/disable port-write interface init
  448. * @mport: Master port implementing the port write unit
  449. * @enable: 1=enable; 0=disable port-write message handling
  450. */
  451. int fsl_rio_pw_enable(struct rio_mport *mport, int enable)
  452. {
  453. u32 rval;
  454. rval = in_be32(&pw->pw_regs->pwmr);
  455. if (enable)
  456. rval |= RIO_IPWMR_PWE;
  457. else
  458. rval &= ~RIO_IPWMR_PWE;
  459. out_be32(&pw->pw_regs->pwmr, rval);
  460. return 0;
  461. }
  462. /**
  463. * fsl_rio_port_write_init - MPC85xx port write interface init
  464. * @mport: Master port implementing the port write unit
  465. *
  466. * Initializes port write unit hardware and DMA buffer
  467. * ring. Called from fsl_rio_setup(). Returns %0 on success
  468. * or %-ENOMEM on failure.
  469. */
  470. int fsl_rio_port_write_init(struct fsl_rio_pw *pw)
  471. {
  472. int rc = 0;
  473. /* Following configurations require a disabled port write controller */
  474. out_be32(&pw->pw_regs->pwmr,
  475. in_be32(&pw->pw_regs->pwmr) & ~RIO_IPWMR_PWE);
  476. /* Initialize port write */
  477. pw->port_write_msg.virt = dma_alloc_coherent(pw->dev,
  478. RIO_PW_MSG_SIZE,
  479. &pw->port_write_msg.phys, GFP_KERNEL);
  480. if (!pw->port_write_msg.virt) {
  481. pr_err("RIO: unable allocate port write queue\n");
  482. return -ENOMEM;
  483. }
  484. pw->port_write_msg.err_count = 0;
  485. pw->port_write_msg.discard_count = 0;
  486. /* Point dequeue/enqueue pointers at first entry */
  487. out_be32(&pw->pw_regs->epwqbar, 0);
  488. out_be32(&pw->pw_regs->pwqbar, (u32) pw->port_write_msg.phys);
  489. pr_debug("EIPWQBAR: 0x%08x IPWQBAR: 0x%08x\n",
  490. in_be32(&pw->pw_regs->epwqbar),
  491. in_be32(&pw->pw_regs->pwqbar));
  492. /* Clear interrupt status IPWSR */
  493. out_be32(&pw->pw_regs->pwsr,
  494. (RIO_IPWSR_TE | RIO_IPWSR_QFI | RIO_IPWSR_PWD));
  495. /* Configure port write contoller for snooping enable all reporting,
  496. clear queue full */
  497. out_be32(&pw->pw_regs->pwmr,
  498. RIO_IPWMR_SEN | RIO_IPWMR_QFIE | RIO_IPWMR_EIE | RIO_IPWMR_CQ);
  499. /* Hook up port-write handler */
  500. rc = request_irq(IRQ_RIO_PW(pw), fsl_rio_port_write_handler,
  501. IRQF_SHARED, "port-write", (void *)pw);
  502. if (rc < 0) {
  503. pr_err("MPC85xx RIO: unable to request inbound doorbell irq");
  504. goto err_out;
  505. }
  506. /* Enable Error Interrupt */
  507. out_be32((u32 *)(rio_regs_win + RIO_LTLEECSR), LTLEECSR_ENABLE_ALL);
  508. INIT_WORK(&pw->pw_work, fsl_pw_dpc);
  509. spin_lock_init(&pw->pw_fifo_lock);
  510. if (kfifo_alloc(&pw->pw_fifo, RIO_PW_MSG_SIZE * 32, GFP_KERNEL)) {
  511. pr_err("FIFO allocation failed\n");
  512. rc = -ENOMEM;
  513. goto err_out_irq;
  514. }
  515. pr_debug("IPWMR: 0x%08x IPWSR: 0x%08x\n",
  516. in_be32(&pw->pw_regs->pwmr),
  517. in_be32(&pw->pw_regs->pwsr));
  518. return rc;
  519. err_out_irq:
  520. free_irq(IRQ_RIO_PW(pw), (void *)pw);
  521. err_out:
  522. dma_free_coherent(pw->dev, RIO_PW_MSG_SIZE,
  523. pw->port_write_msg.virt,
  524. pw->port_write_msg.phys);
  525. return rc;
  526. }
  527. /**
  528. * fsl_rio_doorbell_send - Send a MPC85xx doorbell message
  529. * @mport: RapidIO master port info
  530. * @index: ID of RapidIO interface
  531. * @destid: Destination ID of target device
  532. * @data: 16-bit info field of RapidIO doorbell message
  533. *
  534. * Sends a MPC85xx doorbell message. Returns %0 on success or
  535. * %-EINVAL on failure.
  536. */
  537. int fsl_rio_doorbell_send(struct rio_mport *mport,
  538. int index, u16 destid, u16 data)
  539. {
  540. pr_debug("fsl_doorbell_send: index %d destid %4.4x data %4.4x\n",
  541. index, destid, data);
  542. /* In the serial version silicons, such as MPC8548, MPC8641,
  543. * below operations is must be.
  544. */
  545. out_be32(&dbell->dbell_regs->odmr, 0x00000000);
  546. out_be32(&dbell->dbell_regs->odretcr, 0x00000004);
  547. out_be32(&dbell->dbell_regs->oddpr, destid << 16);
  548. out_be32(&dbell->dbell_regs->oddatr, (index << 20) | data);
  549. out_be32(&dbell->dbell_regs->odmr, 0x00000001);
  550. return 0;
  551. }
  552. /**
  553. * fsl_add_outb_message - Add message to the MPC85xx outbound message queue
  554. * @mport: Master port with outbound message queue
  555. * @rdev: Target of outbound message
  556. * @mbox: Outbound mailbox
  557. * @buffer: Message to add to outbound queue
  558. * @len: Length of message
  559. *
  560. * Adds the @buffer message to the MPC85xx outbound message queue. Returns
  561. * %0 on success or %-EINVAL on failure.
  562. */
  563. int
  564. fsl_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
  565. void *buffer, size_t len)
  566. {
  567. struct fsl_rmu *rmu = GET_RMM_HANDLE(mport);
  568. u32 omr;
  569. struct rio_tx_desc *desc = (struct rio_tx_desc *)rmu->msg_tx_ring.virt
  570. + rmu->msg_tx_ring.tx_slot;
  571. int ret = 0;
  572. pr_debug("RIO: fsl_add_outb_message(): destid %4.4x mbox %d buffer " \
  573. "%p len %8.8zx\n", rdev->destid, mbox, buffer, len);
  574. if ((len < 8) || (len > RIO_MAX_MSG_SIZE)) {
  575. ret = -EINVAL;
  576. goto out;
  577. }
  578. /* Copy and clear rest of buffer */
  579. memcpy(rmu->msg_tx_ring.virt_buffer[rmu->msg_tx_ring.tx_slot], buffer,
  580. len);
  581. if (len < (RIO_MAX_MSG_SIZE - 4))
  582. memset(rmu->msg_tx_ring.virt_buffer[rmu->msg_tx_ring.tx_slot]
  583. + len, 0, RIO_MAX_MSG_SIZE - len);
  584. /* Set mbox field for message, and set destid */
  585. desc->dport = (rdev->destid << 16) | (mbox & 0x3);
  586. /* Enable EOMI interrupt and priority */
  587. desc->dattr = 0x28000000 | ((mport->index) << 20);
  588. /* Set transfer size aligned to next power of 2 (in double words) */
  589. desc->dwcnt = is_power_of_2(len) ? len : 1 << get_bitmask_order(len);
  590. /* Set snooping and source buffer address */
  591. desc->saddr = 0x00000004
  592. | rmu->msg_tx_ring.phys_buffer[rmu->msg_tx_ring.tx_slot];
  593. /* Increment enqueue pointer */
  594. omr = in_be32(&rmu->msg_regs->omr);
  595. out_be32(&rmu->msg_regs->omr, omr | RIO_MSG_OMR_MUI);
  596. /* Go to next descriptor */
  597. if (++rmu->msg_tx_ring.tx_slot == rmu->msg_tx_ring.size)
  598. rmu->msg_tx_ring.tx_slot = 0;
  599. out:
  600. return ret;
  601. }
  602. /**
  603. * fsl_open_outb_mbox - Initialize MPC85xx outbound mailbox
  604. * @mport: Master port implementing the outbound message unit
  605. * @dev_id: Device specific pointer to pass on event
  606. * @mbox: Mailbox to open
  607. * @entries: Number of entries in the outbound mailbox ring
  608. *
  609. * Initializes buffer ring, request the outbound message interrupt,
  610. * and enables the outbound message unit. Returns %0 on success and
  611. * %-EINVAL or %-ENOMEM on failure.
  612. */
  613. int
  614. fsl_open_outb_mbox(struct rio_mport *mport, void *dev_id, int mbox, int entries)
  615. {
  616. int i, j, rc = 0;
  617. struct rio_priv *priv = mport->priv;
  618. struct fsl_rmu *rmu = GET_RMM_HANDLE(mport);
  619. if ((entries < RIO_MIN_TX_RING_SIZE) ||
  620. (entries > RIO_MAX_TX_RING_SIZE) || (!is_power_of_2(entries))) {
  621. rc = -EINVAL;
  622. goto out;
  623. }
  624. /* Initialize shadow copy ring */
  625. rmu->msg_tx_ring.dev_id = dev_id;
  626. rmu->msg_tx_ring.size = entries;
  627. for (i = 0; i < rmu->msg_tx_ring.size; i++) {
  628. rmu->msg_tx_ring.virt_buffer[i] =
  629. dma_alloc_coherent(priv->dev, RIO_MSG_BUFFER_SIZE,
  630. &rmu->msg_tx_ring.phys_buffer[i], GFP_KERNEL);
  631. if (!rmu->msg_tx_ring.virt_buffer[i]) {
  632. rc = -ENOMEM;
  633. for (j = 0; j < rmu->msg_tx_ring.size; j++)
  634. if (rmu->msg_tx_ring.virt_buffer[j])
  635. dma_free_coherent(priv->dev,
  636. RIO_MSG_BUFFER_SIZE,
  637. rmu->msg_tx_ring.
  638. virt_buffer[j],
  639. rmu->msg_tx_ring.
  640. phys_buffer[j]);
  641. goto out;
  642. }
  643. }
  644. /* Initialize outbound message descriptor ring */
  645. rmu->msg_tx_ring.virt = dma_alloc_coherent(priv->dev,
  646. rmu->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
  647. &rmu->msg_tx_ring.phys, GFP_KERNEL);
  648. if (!rmu->msg_tx_ring.virt) {
  649. rc = -ENOMEM;
  650. goto out_dma;
  651. }
  652. memset(rmu->msg_tx_ring.virt, 0,
  653. rmu->msg_tx_ring.size * RIO_MSG_DESC_SIZE);
  654. rmu->msg_tx_ring.tx_slot = 0;
  655. /* Point dequeue/enqueue pointers at first entry in ring */
  656. out_be32(&rmu->msg_regs->odqdpar, rmu->msg_tx_ring.phys);
  657. out_be32(&rmu->msg_regs->odqepar, rmu->msg_tx_ring.phys);
  658. /* Configure for snooping */
  659. out_be32(&rmu->msg_regs->osar, 0x00000004);
  660. /* Clear interrupt status */
  661. out_be32(&rmu->msg_regs->osr, 0x000000b3);
  662. /* Hook up outbound message handler */
  663. rc = request_irq(IRQ_RIO_TX(mport), fsl_rio_tx_handler, 0,
  664. "msg_tx", (void *)mport);
  665. if (rc < 0)
  666. goto out_irq;
  667. /*
  668. * Configure outbound message unit
  669. * Snooping
  670. * Interrupts (all enabled, except QEIE)
  671. * Chaining mode
  672. * Disable
  673. */
  674. out_be32(&rmu->msg_regs->omr, 0x00100220);
  675. /* Set number of entries */
  676. out_be32(&rmu->msg_regs->omr,
  677. in_be32(&rmu->msg_regs->omr) |
  678. ((get_bitmask_order(entries) - 2) << 12));
  679. /* Now enable the unit */
  680. out_be32(&rmu->msg_regs->omr, in_be32(&rmu->msg_regs->omr) | 0x1);
  681. out:
  682. return rc;
  683. out_irq:
  684. dma_free_coherent(priv->dev,
  685. rmu->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
  686. rmu->msg_tx_ring.virt, rmu->msg_tx_ring.phys);
  687. out_dma:
  688. for (i = 0; i < rmu->msg_tx_ring.size; i++)
  689. dma_free_coherent(priv->dev, RIO_MSG_BUFFER_SIZE,
  690. rmu->msg_tx_ring.virt_buffer[i],
  691. rmu->msg_tx_ring.phys_buffer[i]);
  692. return rc;
  693. }
  694. /**
  695. * fsl_close_outb_mbox - Shut down MPC85xx outbound mailbox
  696. * @mport: Master port implementing the outbound message unit
  697. * @mbox: Mailbox to close
  698. *
  699. * Disables the outbound message unit, free all buffers, and
  700. * frees the outbound message interrupt.
  701. */
  702. void fsl_close_outb_mbox(struct rio_mport *mport, int mbox)
  703. {
  704. struct rio_priv *priv = mport->priv;
  705. struct fsl_rmu *rmu = GET_RMM_HANDLE(mport);
  706. /* Disable inbound message unit */
  707. out_be32(&rmu->msg_regs->omr, 0);
  708. /* Free ring */
  709. dma_free_coherent(priv->dev,
  710. rmu->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
  711. rmu->msg_tx_ring.virt, rmu->msg_tx_ring.phys);
  712. /* Free interrupt */
  713. free_irq(IRQ_RIO_TX(mport), (void *)mport);
  714. }
  715. /**
  716. * fsl_open_inb_mbox - Initialize MPC85xx inbound mailbox
  717. * @mport: Master port implementing the inbound message unit
  718. * @dev_id: Device specific pointer to pass on event
  719. * @mbox: Mailbox to open
  720. * @entries: Number of entries in the inbound mailbox ring
  721. *
  722. * Initializes buffer ring, request the inbound message interrupt,
  723. * and enables the inbound message unit. Returns %0 on success
  724. * and %-EINVAL or %-ENOMEM on failure.
  725. */
  726. int
  727. fsl_open_inb_mbox(struct rio_mport *mport, void *dev_id, int mbox, int entries)
  728. {
  729. int i, rc = 0;
  730. struct rio_priv *priv = mport->priv;
  731. struct fsl_rmu *rmu = GET_RMM_HANDLE(mport);
  732. if ((entries < RIO_MIN_RX_RING_SIZE) ||
  733. (entries > RIO_MAX_RX_RING_SIZE) || (!is_power_of_2(entries))) {
  734. rc = -EINVAL;
  735. goto out;
  736. }
  737. /* Initialize client buffer ring */
  738. rmu->msg_rx_ring.dev_id = dev_id;
  739. rmu->msg_rx_ring.size = entries;
  740. rmu->msg_rx_ring.rx_slot = 0;
  741. for (i = 0; i < rmu->msg_rx_ring.size; i++)
  742. rmu->msg_rx_ring.virt_buffer[i] = NULL;
  743. /* Initialize inbound message ring */
  744. rmu->msg_rx_ring.virt = dma_alloc_coherent(priv->dev,
  745. rmu->msg_rx_ring.size * RIO_MAX_MSG_SIZE,
  746. &rmu->msg_rx_ring.phys, GFP_KERNEL);
  747. if (!rmu->msg_rx_ring.virt) {
  748. rc = -ENOMEM;
  749. goto out;
  750. }
  751. /* Point dequeue/enqueue pointers at first entry in ring */
  752. out_be32(&rmu->msg_regs->ifqdpar, (u32) rmu->msg_rx_ring.phys);
  753. out_be32(&rmu->msg_regs->ifqepar, (u32) rmu->msg_rx_ring.phys);
  754. /* Clear interrupt status */
  755. out_be32(&rmu->msg_regs->isr, 0x00000091);
  756. /* Hook up inbound message handler */
  757. rc = request_irq(IRQ_RIO_RX(mport), fsl_rio_rx_handler, 0,
  758. "msg_rx", (void *)mport);
  759. if (rc < 0) {
  760. dma_free_coherent(priv->dev,
  761. rmu->msg_rx_ring.size * RIO_MAX_MSG_SIZE,
  762. rmu->msg_rx_ring.virt, rmu->msg_rx_ring.phys);
  763. goto out;
  764. }
  765. /*
  766. * Configure inbound message unit:
  767. * Snooping
  768. * 4KB max message size
  769. * Unmask all interrupt sources
  770. * Disable
  771. */
  772. out_be32(&rmu->msg_regs->imr, 0x001b0060);
  773. /* Set number of queue entries */
  774. setbits32(&rmu->msg_regs->imr, (get_bitmask_order(entries) - 2) << 12);
  775. /* Now enable the unit */
  776. setbits32(&rmu->msg_regs->imr, 0x1);
  777. out:
  778. return rc;
  779. }
  780. /**
  781. * fsl_close_inb_mbox - Shut down MPC85xx inbound mailbox
  782. * @mport: Master port implementing the inbound message unit
  783. * @mbox: Mailbox to close
  784. *
  785. * Disables the inbound message unit, free all buffers, and
  786. * frees the inbound message interrupt.
  787. */
  788. void fsl_close_inb_mbox(struct rio_mport *mport, int mbox)
  789. {
  790. struct rio_priv *priv = mport->priv;
  791. struct fsl_rmu *rmu = GET_RMM_HANDLE(mport);
  792. /* Disable inbound message unit */
  793. out_be32(&rmu->msg_regs->imr, 0);
  794. /* Free ring */
  795. dma_free_coherent(priv->dev, rmu->msg_rx_ring.size * RIO_MAX_MSG_SIZE,
  796. rmu->msg_rx_ring.virt, rmu->msg_rx_ring.phys);
  797. /* Free interrupt */
  798. free_irq(IRQ_RIO_RX(mport), (void *)mport);
  799. }
  800. /**
  801. * fsl_add_inb_buffer - Add buffer to the MPC85xx inbound message queue
  802. * @mport: Master port implementing the inbound message unit
  803. * @mbox: Inbound mailbox number
  804. * @buf: Buffer to add to inbound queue
  805. *
  806. * Adds the @buf buffer to the MPC85xx inbound message queue. Returns
  807. * %0 on success or %-EINVAL on failure.
  808. */
  809. int fsl_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
  810. {
  811. int rc = 0;
  812. struct fsl_rmu *rmu = GET_RMM_HANDLE(mport);
  813. pr_debug("RIO: fsl_add_inb_buffer(), msg_rx_ring.rx_slot %d\n",
  814. rmu->msg_rx_ring.rx_slot);
  815. if (rmu->msg_rx_ring.virt_buffer[rmu->msg_rx_ring.rx_slot]) {
  816. printk(KERN_ERR
  817. "RIO: error adding inbound buffer %d, buffer exists\n",
  818. rmu->msg_rx_ring.rx_slot);
  819. rc = -EINVAL;
  820. goto out;
  821. }
  822. rmu->msg_rx_ring.virt_buffer[rmu->msg_rx_ring.rx_slot] = buf;
  823. if (++rmu->msg_rx_ring.rx_slot == rmu->msg_rx_ring.size)
  824. rmu->msg_rx_ring.rx_slot = 0;
  825. out:
  826. return rc;
  827. }
  828. /**
  829. * fsl_get_inb_message - Fetch inbound message from the MPC85xx message unit
  830. * @mport: Master port implementing the inbound message unit
  831. * @mbox: Inbound mailbox number
  832. *
  833. * Gets the next available inbound message from the inbound message queue.
  834. * A pointer to the message is returned on success or NULL on failure.
  835. */
  836. void *fsl_get_inb_message(struct rio_mport *mport, int mbox)
  837. {
  838. struct fsl_rmu *rmu = GET_RMM_HANDLE(mport);
  839. u32 phys_buf;
  840. void *virt_buf;
  841. void *buf = NULL;
  842. int buf_idx;
  843. phys_buf = in_be32(&rmu->msg_regs->ifqdpar);
  844. /* If no more messages, then bail out */
  845. if (phys_buf == in_be32(&rmu->msg_regs->ifqepar))
  846. goto out2;
  847. virt_buf = rmu->msg_rx_ring.virt + (phys_buf
  848. - rmu->msg_rx_ring.phys);
  849. buf_idx = (phys_buf - rmu->msg_rx_ring.phys) / RIO_MAX_MSG_SIZE;
  850. buf = rmu->msg_rx_ring.virt_buffer[buf_idx];
  851. if (!buf) {
  852. printk(KERN_ERR
  853. "RIO: inbound message copy failed, no buffers\n");
  854. goto out1;
  855. }
  856. /* Copy max message size, caller is expected to allocate that big */
  857. memcpy(buf, virt_buf, RIO_MAX_MSG_SIZE);
  858. /* Clear the available buffer */
  859. rmu->msg_rx_ring.virt_buffer[buf_idx] = NULL;
  860. out1:
  861. setbits32(&rmu->msg_regs->imr, RIO_MSG_IMR_MI);
  862. out2:
  863. return buf;
  864. }
  865. /**
  866. * fsl_rio_doorbell_init - MPC85xx doorbell interface init
  867. * @mport: Master port implementing the inbound doorbell unit
  868. *
  869. * Initializes doorbell unit hardware and inbound DMA buffer
  870. * ring. Called from fsl_rio_setup(). Returns %0 on success
  871. * or %-ENOMEM on failure.
  872. */
  873. int fsl_rio_doorbell_init(struct fsl_rio_dbell *dbell)
  874. {
  875. int rc = 0;
  876. /* Initialize inbound doorbells */
  877. dbell->dbell_ring.virt = dma_alloc_coherent(dbell->dev, 512 *
  878. DOORBELL_MESSAGE_SIZE, &dbell->dbell_ring.phys, GFP_KERNEL);
  879. if (!dbell->dbell_ring.virt) {
  880. printk(KERN_ERR "RIO: unable allocate inbound doorbell ring\n");
  881. rc = -ENOMEM;
  882. goto out;
  883. }
  884. /* Point dequeue/enqueue pointers at first entry in ring */
  885. out_be32(&dbell->dbell_regs->dqdpar, (u32) dbell->dbell_ring.phys);
  886. out_be32(&dbell->dbell_regs->dqepar, (u32) dbell->dbell_ring.phys);
  887. /* Clear interrupt status */
  888. out_be32(&dbell->dbell_regs->dsr, 0x00000091);
  889. /* Hook up doorbell handler */
  890. rc = request_irq(IRQ_RIO_BELL(dbell), fsl_rio_dbell_handler, 0,
  891. "dbell_rx", (void *)dbell);
  892. if (rc < 0) {
  893. dma_free_coherent(dbell->dev, 512 * DOORBELL_MESSAGE_SIZE,
  894. dbell->dbell_ring.virt, dbell->dbell_ring.phys);
  895. printk(KERN_ERR
  896. "MPC85xx RIO: unable to request inbound doorbell irq");
  897. goto out;
  898. }
  899. /* Configure doorbells for snooping, 512 entries, and enable */
  900. out_be32(&dbell->dbell_regs->dmr, 0x00108161);
  901. out:
  902. return rc;
  903. }
  904. int fsl_rio_setup_rmu(struct rio_mport *mport, struct device_node *node)
  905. {
  906. struct rio_priv *priv;
  907. struct fsl_rmu *rmu;
  908. u64 msg_start;
  909. const u32 *msg_addr;
  910. int mlen;
  911. int aw;
  912. if (!mport || !mport->priv)
  913. return -EINVAL;
  914. priv = mport->priv;
  915. if (!node) {
  916. dev_warn(priv->dev, "Can't get %s property 'fsl,rmu'\n",
  917. priv->dev->of_node->full_name);
  918. return -EINVAL;
  919. }
  920. rmu = kzalloc(sizeof(struct fsl_rmu), GFP_KERNEL);
  921. if (!rmu)
  922. return -ENOMEM;
  923. aw = of_n_addr_cells(node);
  924. msg_addr = of_get_property(node, "reg", &mlen);
  925. if (!msg_addr) {
  926. pr_err("%s: unable to find 'reg' property of message-unit\n",
  927. node->full_name);
  928. kfree(rmu);
  929. return -ENOMEM;
  930. }
  931. msg_start = of_read_number(msg_addr, aw);
  932. rmu->msg_regs = (struct rio_msg_regs *)
  933. (rmu_regs_win + (u32)msg_start);
  934. rmu->txirq = irq_of_parse_and_map(node, 0);
  935. rmu->rxirq = irq_of_parse_and_map(node, 1);
  936. printk(KERN_INFO "%s: txirq: %d, rxirq %d\n",
  937. node->full_name, rmu->txirq, rmu->rxirq);
  938. priv->rmm_handle = rmu;
  939. rio_init_dbell_res(&mport->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
  940. rio_init_mbox_res(&mport->riores[RIO_INB_MBOX_RESOURCE], 0, 0);
  941. rio_init_mbox_res(&mport->riores[RIO_OUTB_MBOX_RESOURCE], 0, 0);
  942. return 0;
  943. }