fsl_rio.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. /*
  2. * Freescale MPC85xx/MPC86xx RapidIO support
  3. *
  4. * Copyright 2009 Integrated Device Technology, Inc.
  5. * Alex Bounine <alexandre.bounine@idt.com>
  6. * - Added Port-Write message handling
  7. * - Added Machine Check exception handling
  8. *
  9. * Copyright (C) 2007, 2008 Freescale Semiconductor, Inc.
  10. * Zhang Wei <wei.zhang@freescale.com>
  11. *
  12. * Copyright 2005 MontaVista Software, Inc.
  13. * Matt Porter <mporter@kernel.crashing.org>
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/device.h>
  26. #include <linux/rio.h>
  27. #include <linux/rio_drv.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/delay.h>
  30. #include <linux/slab.h>
  31. #include <linux/kfifo.h>
  32. #include <asm/io.h>
  33. #include <asm/machdep.h>
  34. #include <asm/uaccess.h>
  35. #undef DEBUG_PW /* Port-Write debugging */
  36. /* RapidIO definition irq, which read from OF-tree */
  37. #define IRQ_RIO_BELL(m) (((struct rio_priv *)(m->priv))->bellirq)
  38. #define IRQ_RIO_TX(m) (((struct rio_priv *)(m->priv))->txirq)
  39. #define IRQ_RIO_RX(m) (((struct rio_priv *)(m->priv))->rxirq)
  40. #define IRQ_RIO_PW(m) (((struct rio_priv *)(m->priv))->pwirq)
  41. #define RIO_ATMU_REGS_OFFSET 0x10c00
  42. #define RIO_P_MSG_REGS_OFFSET 0x11000
  43. #define RIO_S_MSG_REGS_OFFSET 0x13000
  44. #define RIO_ESCSR 0x158
  45. #define RIO_CCSR 0x15c
  46. #define RIO_LTLEDCSR 0x0608
  47. #define RIO_LTLEDCSR_IER 0x80000000
  48. #define RIO_LTLEDCSR_PRT 0x01000000
  49. #define RIO_LTLEECSR 0x060c
  50. #define RIO_EPWISR 0x10010
  51. #define RIO_ISR_AACR 0x10120
  52. #define RIO_ISR_AACR_AA 0x1 /* Accept All ID */
  53. #define RIO_MAINT_WIN_SIZE 0x400000
  54. #define RIO_DBELL_WIN_SIZE 0x1000
  55. #define RIO_MSG_OMR_MUI 0x00000002
  56. #define RIO_MSG_OSR_TE 0x00000080
  57. #define RIO_MSG_OSR_QOI 0x00000020
  58. #define RIO_MSG_OSR_QFI 0x00000010
  59. #define RIO_MSG_OSR_MUB 0x00000004
  60. #define RIO_MSG_OSR_EOMI 0x00000002
  61. #define RIO_MSG_OSR_QEI 0x00000001
  62. #define RIO_MSG_IMR_MI 0x00000002
  63. #define RIO_MSG_ISR_TE 0x00000080
  64. #define RIO_MSG_ISR_QFI 0x00000010
  65. #define RIO_MSG_ISR_DIQI 0x00000001
  66. #define RIO_IPWMR_SEN 0x00100000
  67. #define RIO_IPWMR_QFIE 0x00000100
  68. #define RIO_IPWMR_EIE 0x00000020
  69. #define RIO_IPWMR_CQ 0x00000002
  70. #define RIO_IPWMR_PWE 0x00000001
  71. #define RIO_IPWSR_QF 0x00100000
  72. #define RIO_IPWSR_TE 0x00000080
  73. #define RIO_IPWSR_QFI 0x00000010
  74. #define RIO_IPWSR_PWD 0x00000008
  75. #define RIO_IPWSR_PWB 0x00000004
  76. #define RIO_MSG_DESC_SIZE 32
  77. #define RIO_MSG_BUFFER_SIZE 4096
  78. #define RIO_MIN_TX_RING_SIZE 2
  79. #define RIO_MAX_TX_RING_SIZE 2048
  80. #define RIO_MIN_RX_RING_SIZE 2
  81. #define RIO_MAX_RX_RING_SIZE 2048
  82. #define DOORBELL_DMR_DI 0x00000002
  83. #define DOORBELL_DSR_TE 0x00000080
  84. #define DOORBELL_DSR_QFI 0x00000010
  85. #define DOORBELL_DSR_DIQI 0x00000001
  86. #define DOORBELL_TID_OFFSET 0x02
  87. #define DOORBELL_SID_OFFSET 0x04
  88. #define DOORBELL_INFO_OFFSET 0x06
  89. #define DOORBELL_MESSAGE_SIZE 0x08
  90. #define DBELL_SID(x) (*(u16 *)(x + DOORBELL_SID_OFFSET))
  91. #define DBELL_TID(x) (*(u16 *)(x + DOORBELL_TID_OFFSET))
  92. #define DBELL_INF(x) (*(u16 *)(x + DOORBELL_INFO_OFFSET))
  93. struct rio_atmu_regs {
  94. u32 rowtar;
  95. u32 rowtear;
  96. u32 rowbar;
  97. u32 pad2;
  98. u32 rowar;
  99. u32 pad3[3];
  100. };
  101. struct rio_msg_regs {
  102. u32 omr;
  103. u32 osr;
  104. u32 pad1;
  105. u32 odqdpar;
  106. u32 pad2;
  107. u32 osar;
  108. u32 odpr;
  109. u32 odatr;
  110. u32 odcr;
  111. u32 pad3;
  112. u32 odqepar;
  113. u32 pad4[13];
  114. u32 imr;
  115. u32 isr;
  116. u32 pad5;
  117. u32 ifqdpar;
  118. u32 pad6;
  119. u32 ifqepar;
  120. u32 pad7[226];
  121. u32 odmr;
  122. u32 odsr;
  123. u32 res0[4];
  124. u32 oddpr;
  125. u32 oddatr;
  126. u32 res1[3];
  127. u32 odretcr;
  128. u32 res2[12];
  129. u32 dmr;
  130. u32 dsr;
  131. u32 pad8;
  132. u32 dqdpar;
  133. u32 pad9;
  134. u32 dqepar;
  135. u32 pad10[26];
  136. u32 pwmr;
  137. u32 pwsr;
  138. u32 epwqbar;
  139. u32 pwqbar;
  140. };
  141. struct rio_tx_desc {
  142. u32 res1;
  143. u32 saddr;
  144. u32 dport;
  145. u32 dattr;
  146. u32 res2;
  147. u32 res3;
  148. u32 dwcnt;
  149. u32 res4;
  150. };
  151. struct rio_dbell_ring {
  152. void *virt;
  153. dma_addr_t phys;
  154. };
  155. struct rio_msg_tx_ring {
  156. void *virt;
  157. dma_addr_t phys;
  158. void *virt_buffer[RIO_MAX_TX_RING_SIZE];
  159. dma_addr_t phys_buffer[RIO_MAX_TX_RING_SIZE];
  160. int tx_slot;
  161. int size;
  162. void *dev_id;
  163. };
  164. struct rio_msg_rx_ring {
  165. void *virt;
  166. dma_addr_t phys;
  167. void *virt_buffer[RIO_MAX_RX_RING_SIZE];
  168. int rx_slot;
  169. int size;
  170. void *dev_id;
  171. };
  172. struct rio_port_write_msg {
  173. void *virt;
  174. dma_addr_t phys;
  175. u32 msg_count;
  176. u32 err_count;
  177. u32 discard_count;
  178. };
  179. struct rio_priv {
  180. struct device *dev;
  181. void __iomem *regs_win;
  182. struct rio_atmu_regs __iomem *atmu_regs;
  183. struct rio_atmu_regs __iomem *maint_atmu_regs;
  184. struct rio_atmu_regs __iomem *dbell_atmu_regs;
  185. void __iomem *dbell_win;
  186. void __iomem *maint_win;
  187. struct rio_msg_regs __iomem *msg_regs;
  188. struct rio_dbell_ring dbell_ring;
  189. struct rio_msg_tx_ring msg_tx_ring;
  190. struct rio_msg_rx_ring msg_rx_ring;
  191. struct rio_port_write_msg port_write_msg;
  192. int bellirq;
  193. int txirq;
  194. int rxirq;
  195. int pwirq;
  196. struct work_struct pw_work;
  197. struct kfifo pw_fifo;
  198. spinlock_t pw_fifo_lock;
  199. };
  200. #define __fsl_read_rio_config(x, addr, err, op) \
  201. __asm__ __volatile__( \
  202. "1: "op" %1,0(%2)\n" \
  203. " eieio\n" \
  204. "2:\n" \
  205. ".section .fixup,\"ax\"\n" \
  206. "3: li %1,-1\n" \
  207. " li %0,%3\n" \
  208. " b 2b\n" \
  209. ".section __ex_table,\"a\"\n" \
  210. " .align 2\n" \
  211. " .long 1b,3b\n" \
  212. ".text" \
  213. : "=r" (err), "=r" (x) \
  214. : "b" (addr), "i" (-EFAULT), "0" (err))
  215. static void __iomem *rio_regs_win;
  216. static int (*saved_mcheck_exception)(struct pt_regs *regs);
  217. static int fsl_rio_mcheck_exception(struct pt_regs *regs)
  218. {
  219. const struct exception_table_entry *entry = NULL;
  220. unsigned long reason = (mfspr(SPRN_MCSR) & MCSR_MASK);
  221. if (reason & MCSR_BUS_RBERR) {
  222. reason = in_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR));
  223. if (reason & (RIO_LTLEDCSR_IER | RIO_LTLEDCSR_PRT)) {
  224. /* Check if we are prepared to handle this fault */
  225. entry = search_exception_tables(regs->nip);
  226. if (entry) {
  227. pr_debug("RIO: %s - MC Exception handled\n",
  228. __func__);
  229. out_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR),
  230. 0);
  231. regs->msr |= MSR_RI;
  232. regs->nip = entry->fixup;
  233. return 1;
  234. }
  235. }
  236. }
  237. if (saved_mcheck_exception)
  238. return saved_mcheck_exception(regs);
  239. else
  240. return cur_cpu_spec->machine_check(regs);
  241. }
  242. /**
  243. * fsl_rio_doorbell_send - Send a MPC85xx doorbell message
  244. * @mport: RapidIO master port info
  245. * @index: ID of RapidIO interface
  246. * @destid: Destination ID of target device
  247. * @data: 16-bit info field of RapidIO doorbell message
  248. *
  249. * Sends a MPC85xx doorbell message. Returns %0 on success or
  250. * %-EINVAL on failure.
  251. */
  252. static int fsl_rio_doorbell_send(struct rio_mport *mport,
  253. int index, u16 destid, u16 data)
  254. {
  255. struct rio_priv *priv = mport->priv;
  256. pr_debug("fsl_doorbell_send: index %d destid %4.4x data %4.4x\n",
  257. index, destid, data);
  258. switch (mport->phy_type) {
  259. case RIO_PHY_PARALLEL:
  260. out_be32(&priv->dbell_atmu_regs->rowtar, destid << 22);
  261. out_be16(priv->dbell_win, data);
  262. break;
  263. case RIO_PHY_SERIAL:
  264. /* In the serial version silicons, such as MPC8548, MPC8641,
  265. * below operations is must be.
  266. */
  267. out_be32(&priv->msg_regs->odmr, 0x00000000);
  268. out_be32(&priv->msg_regs->odretcr, 0x00000004);
  269. out_be32(&priv->msg_regs->oddpr, destid << 16);
  270. out_be32(&priv->msg_regs->oddatr, data);
  271. out_be32(&priv->msg_regs->odmr, 0x00000001);
  272. break;
  273. }
  274. return 0;
  275. }
  276. /**
  277. * fsl_local_config_read - Generate a MPC85xx local config space read
  278. * @mport: RapidIO master port info
  279. * @index: ID of RapdiIO interface
  280. * @offset: Offset into configuration space
  281. * @len: Length (in bytes) of the maintenance transaction
  282. * @data: Value to be read into
  283. *
  284. * Generates a MPC85xx local configuration space read. Returns %0 on
  285. * success or %-EINVAL on failure.
  286. */
  287. static int fsl_local_config_read(struct rio_mport *mport,
  288. int index, u32 offset, int len, u32 *data)
  289. {
  290. struct rio_priv *priv = mport->priv;
  291. pr_debug("fsl_local_config_read: index %d offset %8.8x\n", index,
  292. offset);
  293. *data = in_be32(priv->regs_win + offset);
  294. return 0;
  295. }
  296. /**
  297. * fsl_local_config_write - Generate a MPC85xx local config space write
  298. * @mport: RapidIO master port info
  299. * @index: ID of RapdiIO interface
  300. * @offset: Offset into configuration space
  301. * @len: Length (in bytes) of the maintenance transaction
  302. * @data: Value to be written
  303. *
  304. * Generates a MPC85xx local configuration space write. Returns %0 on
  305. * success or %-EINVAL on failure.
  306. */
  307. static int fsl_local_config_write(struct rio_mport *mport,
  308. int index, u32 offset, int len, u32 data)
  309. {
  310. struct rio_priv *priv = mport->priv;
  311. pr_debug
  312. ("fsl_local_config_write: index %d offset %8.8x data %8.8x\n",
  313. index, offset, data);
  314. out_be32(priv->regs_win + offset, data);
  315. return 0;
  316. }
  317. /**
  318. * fsl_rio_config_read - Generate a MPC85xx read maintenance transaction
  319. * @mport: RapidIO master port info
  320. * @index: ID of RapdiIO interface
  321. * @destid: Destination ID of transaction
  322. * @hopcount: Number of hops to target device
  323. * @offset: Offset into configuration space
  324. * @len: Length (in bytes) of the maintenance transaction
  325. * @val: Location to be read into
  326. *
  327. * Generates a MPC85xx read maintenance transaction. Returns %0 on
  328. * success or %-EINVAL on failure.
  329. */
  330. static int
  331. fsl_rio_config_read(struct rio_mport *mport, int index, u16 destid,
  332. u8 hopcount, u32 offset, int len, u32 *val)
  333. {
  334. struct rio_priv *priv = mport->priv;
  335. u8 *data;
  336. u32 rval, err = 0;
  337. pr_debug
  338. ("fsl_rio_config_read: index %d destid %d hopcount %d offset %8.8x len %d\n",
  339. index, destid, hopcount, offset, len);
  340. out_be32(&priv->maint_atmu_regs->rowtar,
  341. (destid << 22) | (hopcount << 12) | ((offset & ~0x3) >> 9));
  342. data = (u8 *) priv->maint_win + offset;
  343. switch (len) {
  344. case 1:
  345. __fsl_read_rio_config(rval, data, err, "lbz");
  346. break;
  347. case 2:
  348. __fsl_read_rio_config(rval, data, err, "lhz");
  349. break;
  350. default:
  351. __fsl_read_rio_config(rval, data, err, "lwz");
  352. break;
  353. }
  354. if (err) {
  355. pr_debug("RIO: cfg_read error %d for %x:%x:%x\n",
  356. err, destid, hopcount, offset);
  357. }
  358. *val = rval;
  359. return err;
  360. }
  361. /**
  362. * fsl_rio_config_write - Generate a MPC85xx write maintenance transaction
  363. * @mport: RapidIO master port info
  364. * @index: ID of RapdiIO interface
  365. * @destid: Destination ID of transaction
  366. * @hopcount: Number of hops to target device
  367. * @offset: Offset into configuration space
  368. * @len: Length (in bytes) of the maintenance transaction
  369. * @val: Value to be written
  370. *
  371. * Generates an MPC85xx write maintenance transaction. Returns %0 on
  372. * success or %-EINVAL on failure.
  373. */
  374. static int
  375. fsl_rio_config_write(struct rio_mport *mport, int index, u16 destid,
  376. u8 hopcount, u32 offset, int len, u32 val)
  377. {
  378. struct rio_priv *priv = mport->priv;
  379. u8 *data;
  380. pr_debug
  381. ("fsl_rio_config_write: index %d destid %d hopcount %d offset %8.8x len %d val %8.8x\n",
  382. index, destid, hopcount, offset, len, val);
  383. out_be32(&priv->maint_atmu_regs->rowtar,
  384. (destid << 22) | (hopcount << 12) | ((offset & ~0x3) >> 9));
  385. data = (u8 *) priv->maint_win + offset;
  386. switch (len) {
  387. case 1:
  388. out_8((u8 *) data, val);
  389. break;
  390. case 2:
  391. out_be16((u16 *) data, val);
  392. break;
  393. default:
  394. out_be32((u32 *) data, val);
  395. break;
  396. }
  397. return 0;
  398. }
  399. /**
  400. * rio_hw_add_outb_message - Add message to the MPC85xx outbound message queue
  401. * @mport: Master port with outbound message queue
  402. * @rdev: Target of outbound message
  403. * @mbox: Outbound mailbox
  404. * @buffer: Message to add to outbound queue
  405. * @len: Length of message
  406. *
  407. * Adds the @buffer message to the MPC85xx outbound message queue. Returns
  408. * %0 on success or %-EINVAL on failure.
  409. */
  410. int
  411. rio_hw_add_outb_message(struct rio_mport *mport, struct rio_dev *rdev, int mbox,
  412. void *buffer, size_t len)
  413. {
  414. struct rio_priv *priv = mport->priv;
  415. u32 omr;
  416. struct rio_tx_desc *desc = (struct rio_tx_desc *)priv->msg_tx_ring.virt
  417. + priv->msg_tx_ring.tx_slot;
  418. int ret = 0;
  419. pr_debug
  420. ("RIO: rio_hw_add_outb_message(): destid %4.4x mbox %d buffer %8.8x len %8.8x\n",
  421. rdev->destid, mbox, (int)buffer, len);
  422. if ((len < 8) || (len > RIO_MAX_MSG_SIZE)) {
  423. ret = -EINVAL;
  424. goto out;
  425. }
  426. /* Copy and clear rest of buffer */
  427. memcpy(priv->msg_tx_ring.virt_buffer[priv->msg_tx_ring.tx_slot], buffer,
  428. len);
  429. if (len < (RIO_MAX_MSG_SIZE - 4))
  430. memset(priv->msg_tx_ring.virt_buffer[priv->msg_tx_ring.tx_slot]
  431. + len, 0, RIO_MAX_MSG_SIZE - len);
  432. switch (mport->phy_type) {
  433. case RIO_PHY_PARALLEL:
  434. /* Set mbox field for message */
  435. desc->dport = mbox & 0x3;
  436. /* Enable EOMI interrupt, set priority, and set destid */
  437. desc->dattr = 0x28000000 | (rdev->destid << 2);
  438. break;
  439. case RIO_PHY_SERIAL:
  440. /* Set mbox field for message, and set destid */
  441. desc->dport = (rdev->destid << 16) | (mbox & 0x3);
  442. /* Enable EOMI interrupt and priority */
  443. desc->dattr = 0x28000000;
  444. break;
  445. }
  446. /* Set transfer size aligned to next power of 2 (in double words) */
  447. desc->dwcnt = is_power_of_2(len) ? len : 1 << get_bitmask_order(len);
  448. /* Set snooping and source buffer address */
  449. desc->saddr = 0x00000004
  450. | priv->msg_tx_ring.phys_buffer[priv->msg_tx_ring.tx_slot];
  451. /* Increment enqueue pointer */
  452. omr = in_be32(&priv->msg_regs->omr);
  453. out_be32(&priv->msg_regs->omr, omr | RIO_MSG_OMR_MUI);
  454. /* Go to next descriptor */
  455. if (++priv->msg_tx_ring.tx_slot == priv->msg_tx_ring.size)
  456. priv->msg_tx_ring.tx_slot = 0;
  457. out:
  458. return ret;
  459. }
  460. EXPORT_SYMBOL_GPL(rio_hw_add_outb_message);
  461. /**
  462. * fsl_rio_tx_handler - MPC85xx outbound message interrupt handler
  463. * @irq: Linux interrupt number
  464. * @dev_instance: Pointer to interrupt-specific data
  465. *
  466. * Handles outbound message interrupts. Executes a register outbound
  467. * mailbox event handler and acks the interrupt occurrence.
  468. */
  469. static irqreturn_t
  470. fsl_rio_tx_handler(int irq, void *dev_instance)
  471. {
  472. int osr;
  473. struct rio_mport *port = (struct rio_mport *)dev_instance;
  474. struct rio_priv *priv = port->priv;
  475. osr = in_be32(&priv->msg_regs->osr);
  476. if (osr & RIO_MSG_OSR_TE) {
  477. pr_info("RIO: outbound message transmission error\n");
  478. out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_TE);
  479. goto out;
  480. }
  481. if (osr & RIO_MSG_OSR_QOI) {
  482. pr_info("RIO: outbound message queue overflow\n");
  483. out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_QOI);
  484. goto out;
  485. }
  486. if (osr & RIO_MSG_OSR_EOMI) {
  487. u32 dqp = in_be32(&priv->msg_regs->odqdpar);
  488. int slot = (dqp - priv->msg_tx_ring.phys) >> 5;
  489. port->outb_msg[0].mcback(port, priv->msg_tx_ring.dev_id, -1,
  490. slot);
  491. /* Ack the end-of-message interrupt */
  492. out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_EOMI);
  493. }
  494. out:
  495. return IRQ_HANDLED;
  496. }
  497. /**
  498. * rio_open_outb_mbox - Initialize MPC85xx outbound mailbox
  499. * @mport: Master port implementing the outbound message unit
  500. * @dev_id: Device specific pointer to pass on event
  501. * @mbox: Mailbox to open
  502. * @entries: Number of entries in the outbound mailbox ring
  503. *
  504. * Initializes buffer ring, request the outbound message interrupt,
  505. * and enables the outbound message unit. Returns %0 on success and
  506. * %-EINVAL or %-ENOMEM on failure.
  507. */
  508. int rio_open_outb_mbox(struct rio_mport *mport, void *dev_id, int mbox, int entries)
  509. {
  510. int i, j, rc = 0;
  511. struct rio_priv *priv = mport->priv;
  512. if ((entries < RIO_MIN_TX_RING_SIZE) ||
  513. (entries > RIO_MAX_TX_RING_SIZE) || (!is_power_of_2(entries))) {
  514. rc = -EINVAL;
  515. goto out;
  516. }
  517. /* Initialize shadow copy ring */
  518. priv->msg_tx_ring.dev_id = dev_id;
  519. priv->msg_tx_ring.size = entries;
  520. for (i = 0; i < priv->msg_tx_ring.size; i++) {
  521. priv->msg_tx_ring.virt_buffer[i] =
  522. dma_alloc_coherent(priv->dev, RIO_MSG_BUFFER_SIZE,
  523. &priv->msg_tx_ring.phys_buffer[i], GFP_KERNEL);
  524. if (!priv->msg_tx_ring.virt_buffer[i]) {
  525. rc = -ENOMEM;
  526. for (j = 0; j < priv->msg_tx_ring.size; j++)
  527. if (priv->msg_tx_ring.virt_buffer[j])
  528. dma_free_coherent(priv->dev,
  529. RIO_MSG_BUFFER_SIZE,
  530. priv->msg_tx_ring.
  531. virt_buffer[j],
  532. priv->msg_tx_ring.
  533. phys_buffer[j]);
  534. goto out;
  535. }
  536. }
  537. /* Initialize outbound message descriptor ring */
  538. priv->msg_tx_ring.virt = dma_alloc_coherent(priv->dev,
  539. priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
  540. &priv->msg_tx_ring.phys, GFP_KERNEL);
  541. if (!priv->msg_tx_ring.virt) {
  542. rc = -ENOMEM;
  543. goto out_dma;
  544. }
  545. memset(priv->msg_tx_ring.virt, 0,
  546. priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE);
  547. priv->msg_tx_ring.tx_slot = 0;
  548. /* Point dequeue/enqueue pointers at first entry in ring */
  549. out_be32(&priv->msg_regs->odqdpar, priv->msg_tx_ring.phys);
  550. out_be32(&priv->msg_regs->odqepar, priv->msg_tx_ring.phys);
  551. /* Configure for snooping */
  552. out_be32(&priv->msg_regs->osar, 0x00000004);
  553. /* Clear interrupt status */
  554. out_be32(&priv->msg_regs->osr, 0x000000b3);
  555. /* Hook up outbound message handler */
  556. rc = request_irq(IRQ_RIO_TX(mport), fsl_rio_tx_handler, 0,
  557. "msg_tx", (void *)mport);
  558. if (rc < 0)
  559. goto out_irq;
  560. /*
  561. * Configure outbound message unit
  562. * Snooping
  563. * Interrupts (all enabled, except QEIE)
  564. * Chaining mode
  565. * Disable
  566. */
  567. out_be32(&priv->msg_regs->omr, 0x00100220);
  568. /* Set number of entries */
  569. out_be32(&priv->msg_regs->omr,
  570. in_be32(&priv->msg_regs->omr) |
  571. ((get_bitmask_order(entries) - 2) << 12));
  572. /* Now enable the unit */
  573. out_be32(&priv->msg_regs->omr, in_be32(&priv->msg_regs->omr) | 0x1);
  574. out:
  575. return rc;
  576. out_irq:
  577. dma_free_coherent(priv->dev,
  578. priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
  579. priv->msg_tx_ring.virt, priv->msg_tx_ring.phys);
  580. out_dma:
  581. for (i = 0; i < priv->msg_tx_ring.size; i++)
  582. dma_free_coherent(priv->dev, RIO_MSG_BUFFER_SIZE,
  583. priv->msg_tx_ring.virt_buffer[i],
  584. priv->msg_tx_ring.phys_buffer[i]);
  585. return rc;
  586. }
  587. /**
  588. * rio_close_outb_mbox - Shut down MPC85xx outbound mailbox
  589. * @mport: Master port implementing the outbound message unit
  590. * @mbox: Mailbox to close
  591. *
  592. * Disables the outbound message unit, free all buffers, and
  593. * frees the outbound message interrupt.
  594. */
  595. void rio_close_outb_mbox(struct rio_mport *mport, int mbox)
  596. {
  597. struct rio_priv *priv = mport->priv;
  598. /* Disable inbound message unit */
  599. out_be32(&priv->msg_regs->omr, 0);
  600. /* Free ring */
  601. dma_free_coherent(priv->dev,
  602. priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
  603. priv->msg_tx_ring.virt, priv->msg_tx_ring.phys);
  604. /* Free interrupt */
  605. free_irq(IRQ_RIO_TX(mport), (void *)mport);
  606. }
  607. /**
  608. * fsl_rio_rx_handler - MPC85xx inbound message interrupt handler
  609. * @irq: Linux interrupt number
  610. * @dev_instance: Pointer to interrupt-specific data
  611. *
  612. * Handles inbound message interrupts. Executes a registered inbound
  613. * mailbox event handler and acks the interrupt occurrence.
  614. */
  615. static irqreturn_t
  616. fsl_rio_rx_handler(int irq, void *dev_instance)
  617. {
  618. int isr;
  619. struct rio_mport *port = (struct rio_mport *)dev_instance;
  620. struct rio_priv *priv = port->priv;
  621. isr = in_be32(&priv->msg_regs->isr);
  622. if (isr & RIO_MSG_ISR_TE) {
  623. pr_info("RIO: inbound message reception error\n");
  624. out_be32((void *)&priv->msg_regs->isr, RIO_MSG_ISR_TE);
  625. goto out;
  626. }
  627. /* XXX Need to check/dispatch until queue empty */
  628. if (isr & RIO_MSG_ISR_DIQI) {
  629. /*
  630. * We implement *only* mailbox 0, but can receive messages
  631. * for any mailbox/letter to that mailbox destination. So,
  632. * make the callback with an unknown/invalid mailbox number
  633. * argument.
  634. */
  635. port->inb_msg[0].mcback(port, priv->msg_rx_ring.dev_id, -1, -1);
  636. /* Ack the queueing interrupt */
  637. out_be32(&priv->msg_regs->isr, RIO_MSG_ISR_DIQI);
  638. }
  639. out:
  640. return IRQ_HANDLED;
  641. }
  642. /**
  643. * rio_open_inb_mbox - Initialize MPC85xx inbound mailbox
  644. * @mport: Master port implementing the inbound message unit
  645. * @dev_id: Device specific pointer to pass on event
  646. * @mbox: Mailbox to open
  647. * @entries: Number of entries in the inbound mailbox ring
  648. *
  649. * Initializes buffer ring, request the inbound message interrupt,
  650. * and enables the inbound message unit. Returns %0 on success
  651. * and %-EINVAL or %-ENOMEM on failure.
  652. */
  653. int rio_open_inb_mbox(struct rio_mport *mport, void *dev_id, int mbox, int entries)
  654. {
  655. int i, rc = 0;
  656. struct rio_priv *priv = mport->priv;
  657. if ((entries < RIO_MIN_RX_RING_SIZE) ||
  658. (entries > RIO_MAX_RX_RING_SIZE) || (!is_power_of_2(entries))) {
  659. rc = -EINVAL;
  660. goto out;
  661. }
  662. /* Initialize client buffer ring */
  663. priv->msg_rx_ring.dev_id = dev_id;
  664. priv->msg_rx_ring.size = entries;
  665. priv->msg_rx_ring.rx_slot = 0;
  666. for (i = 0; i < priv->msg_rx_ring.size; i++)
  667. priv->msg_rx_ring.virt_buffer[i] = NULL;
  668. /* Initialize inbound message ring */
  669. priv->msg_rx_ring.virt = dma_alloc_coherent(priv->dev,
  670. priv->msg_rx_ring.size * RIO_MAX_MSG_SIZE,
  671. &priv->msg_rx_ring.phys, GFP_KERNEL);
  672. if (!priv->msg_rx_ring.virt) {
  673. rc = -ENOMEM;
  674. goto out;
  675. }
  676. /* Point dequeue/enqueue pointers at first entry in ring */
  677. out_be32(&priv->msg_regs->ifqdpar, (u32) priv->msg_rx_ring.phys);
  678. out_be32(&priv->msg_regs->ifqepar, (u32) priv->msg_rx_ring.phys);
  679. /* Clear interrupt status */
  680. out_be32(&priv->msg_regs->isr, 0x00000091);
  681. /* Hook up inbound message handler */
  682. rc = request_irq(IRQ_RIO_RX(mport), fsl_rio_rx_handler, 0,
  683. "msg_rx", (void *)mport);
  684. if (rc < 0) {
  685. dma_free_coherent(priv->dev, RIO_MSG_BUFFER_SIZE,
  686. priv->msg_tx_ring.virt_buffer[i],
  687. priv->msg_tx_ring.phys_buffer[i]);
  688. goto out;
  689. }
  690. /*
  691. * Configure inbound message unit:
  692. * Snooping
  693. * 4KB max message size
  694. * Unmask all interrupt sources
  695. * Disable
  696. */
  697. out_be32(&priv->msg_regs->imr, 0x001b0060);
  698. /* Set number of queue entries */
  699. setbits32(&priv->msg_regs->imr, (get_bitmask_order(entries) - 2) << 12);
  700. /* Now enable the unit */
  701. setbits32(&priv->msg_regs->imr, 0x1);
  702. out:
  703. return rc;
  704. }
  705. /**
  706. * rio_close_inb_mbox - Shut down MPC85xx inbound mailbox
  707. * @mport: Master port implementing the inbound message unit
  708. * @mbox: Mailbox to close
  709. *
  710. * Disables the inbound message unit, free all buffers, and
  711. * frees the inbound message interrupt.
  712. */
  713. void rio_close_inb_mbox(struct rio_mport *mport, int mbox)
  714. {
  715. struct rio_priv *priv = mport->priv;
  716. /* Disable inbound message unit */
  717. out_be32(&priv->msg_regs->imr, 0);
  718. /* Free ring */
  719. dma_free_coherent(priv->dev, priv->msg_rx_ring.size * RIO_MAX_MSG_SIZE,
  720. priv->msg_rx_ring.virt, priv->msg_rx_ring.phys);
  721. /* Free interrupt */
  722. free_irq(IRQ_RIO_RX(mport), (void *)mport);
  723. }
  724. /**
  725. * rio_hw_add_inb_buffer - Add buffer to the MPC85xx inbound message queue
  726. * @mport: Master port implementing the inbound message unit
  727. * @mbox: Inbound mailbox number
  728. * @buf: Buffer to add to inbound queue
  729. *
  730. * Adds the @buf buffer to the MPC85xx inbound message queue. Returns
  731. * %0 on success or %-EINVAL on failure.
  732. */
  733. int rio_hw_add_inb_buffer(struct rio_mport *mport, int mbox, void *buf)
  734. {
  735. int rc = 0;
  736. struct rio_priv *priv = mport->priv;
  737. pr_debug("RIO: rio_hw_add_inb_buffer(), msg_rx_ring.rx_slot %d\n",
  738. priv->msg_rx_ring.rx_slot);
  739. if (priv->msg_rx_ring.virt_buffer[priv->msg_rx_ring.rx_slot]) {
  740. printk(KERN_ERR
  741. "RIO: error adding inbound buffer %d, buffer exists\n",
  742. priv->msg_rx_ring.rx_slot);
  743. rc = -EINVAL;
  744. goto out;
  745. }
  746. priv->msg_rx_ring.virt_buffer[priv->msg_rx_ring.rx_slot] = buf;
  747. if (++priv->msg_rx_ring.rx_slot == priv->msg_rx_ring.size)
  748. priv->msg_rx_ring.rx_slot = 0;
  749. out:
  750. return rc;
  751. }
  752. EXPORT_SYMBOL_GPL(rio_hw_add_inb_buffer);
  753. /**
  754. * rio_hw_get_inb_message - Fetch inbound message from the MPC85xx message unit
  755. * @mport: Master port implementing the inbound message unit
  756. * @mbox: Inbound mailbox number
  757. *
  758. * Gets the next available inbound message from the inbound message queue.
  759. * A pointer to the message is returned on success or NULL on failure.
  760. */
  761. void *rio_hw_get_inb_message(struct rio_mport *mport, int mbox)
  762. {
  763. struct rio_priv *priv = mport->priv;
  764. u32 phys_buf, virt_buf;
  765. void *buf = NULL;
  766. int buf_idx;
  767. phys_buf = in_be32(&priv->msg_regs->ifqdpar);
  768. /* If no more messages, then bail out */
  769. if (phys_buf == in_be32(&priv->msg_regs->ifqepar))
  770. goto out2;
  771. virt_buf = (u32) priv->msg_rx_ring.virt + (phys_buf
  772. - priv->msg_rx_ring.phys);
  773. buf_idx = (phys_buf - priv->msg_rx_ring.phys) / RIO_MAX_MSG_SIZE;
  774. buf = priv->msg_rx_ring.virt_buffer[buf_idx];
  775. if (!buf) {
  776. printk(KERN_ERR
  777. "RIO: inbound message copy failed, no buffers\n");
  778. goto out1;
  779. }
  780. /* Copy max message size, caller is expected to allocate that big */
  781. memcpy(buf, (void *)virt_buf, RIO_MAX_MSG_SIZE);
  782. /* Clear the available buffer */
  783. priv->msg_rx_ring.virt_buffer[buf_idx] = NULL;
  784. out1:
  785. setbits32(&priv->msg_regs->imr, RIO_MSG_IMR_MI);
  786. out2:
  787. return buf;
  788. }
  789. EXPORT_SYMBOL_GPL(rio_hw_get_inb_message);
  790. /**
  791. * fsl_rio_dbell_handler - MPC85xx doorbell interrupt handler
  792. * @irq: Linux interrupt number
  793. * @dev_instance: Pointer to interrupt-specific data
  794. *
  795. * Handles doorbell interrupts. Parses a list of registered
  796. * doorbell event handlers and executes a matching event handler.
  797. */
  798. static irqreturn_t
  799. fsl_rio_dbell_handler(int irq, void *dev_instance)
  800. {
  801. int dsr;
  802. struct rio_mport *port = (struct rio_mport *)dev_instance;
  803. struct rio_priv *priv = port->priv;
  804. dsr = in_be32(&priv->msg_regs->dsr);
  805. if (dsr & DOORBELL_DSR_TE) {
  806. pr_info("RIO: doorbell reception error\n");
  807. out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_TE);
  808. goto out;
  809. }
  810. if (dsr & DOORBELL_DSR_QFI) {
  811. pr_info("RIO: doorbell queue full\n");
  812. out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_QFI);
  813. goto out;
  814. }
  815. /* XXX Need to check/dispatch until queue empty */
  816. if (dsr & DOORBELL_DSR_DIQI) {
  817. u32 dmsg =
  818. (u32) priv->dbell_ring.virt +
  819. (in_be32(&priv->msg_regs->dqdpar) & 0xfff);
  820. struct rio_dbell *dbell;
  821. int found = 0;
  822. pr_debug
  823. ("RIO: processing doorbell, sid %2.2x tid %2.2x info %4.4x\n",
  824. DBELL_SID(dmsg), DBELL_TID(dmsg), DBELL_INF(dmsg));
  825. list_for_each_entry(dbell, &port->dbells, node) {
  826. if ((dbell->res->start <= DBELL_INF(dmsg)) &&
  827. (dbell->res->end >= DBELL_INF(dmsg))) {
  828. found = 1;
  829. break;
  830. }
  831. }
  832. if (found) {
  833. dbell->dinb(port, dbell->dev_id, DBELL_SID(dmsg), DBELL_TID(dmsg),
  834. DBELL_INF(dmsg));
  835. } else {
  836. pr_debug
  837. ("RIO: spurious doorbell, sid %2.2x tid %2.2x info %4.4x\n",
  838. DBELL_SID(dmsg), DBELL_TID(dmsg), DBELL_INF(dmsg));
  839. }
  840. setbits32(&priv->msg_regs->dmr, DOORBELL_DMR_DI);
  841. out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_DIQI);
  842. }
  843. out:
  844. return IRQ_HANDLED;
  845. }
  846. /**
  847. * fsl_rio_doorbell_init - MPC85xx doorbell interface init
  848. * @mport: Master port implementing the inbound doorbell unit
  849. *
  850. * Initializes doorbell unit hardware and inbound DMA buffer
  851. * ring. Called from fsl_rio_setup(). Returns %0 on success
  852. * or %-ENOMEM on failure.
  853. */
  854. static int fsl_rio_doorbell_init(struct rio_mport *mport)
  855. {
  856. struct rio_priv *priv = mport->priv;
  857. int rc = 0;
  858. /* Map outbound doorbell window immediately after maintenance window */
  859. priv->dbell_win = ioremap(mport->iores.start + RIO_MAINT_WIN_SIZE,
  860. RIO_DBELL_WIN_SIZE);
  861. if (!priv->dbell_win) {
  862. printk(KERN_ERR
  863. "RIO: unable to map outbound doorbell window\n");
  864. rc = -ENOMEM;
  865. goto out;
  866. }
  867. /* Initialize inbound doorbells */
  868. priv->dbell_ring.virt = dma_alloc_coherent(priv->dev, 512 *
  869. DOORBELL_MESSAGE_SIZE, &priv->dbell_ring.phys, GFP_KERNEL);
  870. if (!priv->dbell_ring.virt) {
  871. printk(KERN_ERR "RIO: unable allocate inbound doorbell ring\n");
  872. rc = -ENOMEM;
  873. iounmap(priv->dbell_win);
  874. goto out;
  875. }
  876. /* Point dequeue/enqueue pointers at first entry in ring */
  877. out_be32(&priv->msg_regs->dqdpar, (u32) priv->dbell_ring.phys);
  878. out_be32(&priv->msg_regs->dqepar, (u32) priv->dbell_ring.phys);
  879. /* Clear interrupt status */
  880. out_be32(&priv->msg_regs->dsr, 0x00000091);
  881. /* Hook up doorbell handler */
  882. rc = request_irq(IRQ_RIO_BELL(mport), fsl_rio_dbell_handler, 0,
  883. "dbell_rx", (void *)mport);
  884. if (rc < 0) {
  885. iounmap(priv->dbell_win);
  886. dma_free_coherent(priv->dev, 512 * DOORBELL_MESSAGE_SIZE,
  887. priv->dbell_ring.virt, priv->dbell_ring.phys);
  888. printk(KERN_ERR
  889. "MPC85xx RIO: unable to request inbound doorbell irq");
  890. goto out;
  891. }
  892. /* Configure doorbells for snooping, 512 entries, and enable */
  893. out_be32(&priv->msg_regs->dmr, 0x00108161);
  894. out:
  895. return rc;
  896. }
  897. /**
  898. * fsl_rio_port_write_handler - MPC85xx port write interrupt handler
  899. * @irq: Linux interrupt number
  900. * @dev_instance: Pointer to interrupt-specific data
  901. *
  902. * Handles port write interrupts. Parses a list of registered
  903. * port write event handlers and executes a matching event handler.
  904. */
  905. static irqreturn_t
  906. fsl_rio_port_write_handler(int irq, void *dev_instance)
  907. {
  908. u32 ipwmr, ipwsr;
  909. struct rio_mport *port = (struct rio_mport *)dev_instance;
  910. struct rio_priv *priv = port->priv;
  911. u32 epwisr, tmp;
  912. ipwmr = in_be32(&priv->msg_regs->pwmr);
  913. ipwsr = in_be32(&priv->msg_regs->pwsr);
  914. epwisr = in_be32(priv->regs_win + RIO_EPWISR);
  915. if (epwisr & 0x80000000) {
  916. tmp = in_be32(priv->regs_win + RIO_LTLEDCSR);
  917. pr_info("RIO_LTLEDCSR = 0x%x\n", tmp);
  918. out_be32(priv->regs_win + RIO_LTLEDCSR, 0);
  919. }
  920. if (!(epwisr & 0x00000001))
  921. return IRQ_HANDLED;
  922. #ifdef DEBUG_PW
  923. pr_debug("PW Int->IPWMR: 0x%08x IPWSR: 0x%08x (", ipwmr, ipwsr);
  924. if (ipwsr & RIO_IPWSR_QF)
  925. pr_debug(" QF");
  926. if (ipwsr & RIO_IPWSR_TE)
  927. pr_debug(" TE");
  928. if (ipwsr & RIO_IPWSR_QFI)
  929. pr_debug(" QFI");
  930. if (ipwsr & RIO_IPWSR_PWD)
  931. pr_debug(" PWD");
  932. if (ipwsr & RIO_IPWSR_PWB)
  933. pr_debug(" PWB");
  934. pr_debug(" )\n");
  935. #endif
  936. out_be32(&priv->msg_regs->pwsr,
  937. ipwsr & (RIO_IPWSR_TE | RIO_IPWSR_QFI | RIO_IPWSR_PWD));
  938. if ((ipwmr & RIO_IPWMR_EIE) && (ipwsr & RIO_IPWSR_TE)) {
  939. priv->port_write_msg.err_count++;
  940. pr_info("RIO: Port-Write Transaction Err (%d)\n",
  941. priv->port_write_msg.err_count);
  942. }
  943. if (ipwsr & RIO_IPWSR_PWD) {
  944. priv->port_write_msg.discard_count++;
  945. pr_info("RIO: Port Discarded Port-Write Msg(s) (%d)\n",
  946. priv->port_write_msg.discard_count);
  947. }
  948. /* Schedule deferred processing if PW was received */
  949. if (ipwsr & RIO_IPWSR_QFI) {
  950. /* Save PW message (if there is room in FIFO),
  951. * otherwise discard it.
  952. */
  953. if (kfifo_avail(&priv->pw_fifo) >= RIO_PW_MSG_SIZE) {
  954. priv->port_write_msg.msg_count++;
  955. kfifo_in(&priv->pw_fifo, priv->port_write_msg.virt,
  956. RIO_PW_MSG_SIZE);
  957. } else {
  958. priv->port_write_msg.discard_count++;
  959. pr_info("RIO: ISR Discarded Port-Write Msg(s) (%d)\n",
  960. priv->port_write_msg.discard_count);
  961. }
  962. schedule_work(&priv->pw_work);
  963. }
  964. /* Issue Clear Queue command. This allows another
  965. * port-write to be received.
  966. */
  967. out_be32(&priv->msg_regs->pwmr, ipwmr | RIO_IPWMR_CQ);
  968. return IRQ_HANDLED;
  969. }
  970. static void fsl_pw_dpc(struct work_struct *work)
  971. {
  972. struct rio_priv *priv = container_of(work, struct rio_priv, pw_work);
  973. unsigned long flags;
  974. u32 msg_buffer[RIO_PW_MSG_SIZE/sizeof(u32)];
  975. /*
  976. * Process port-write messages
  977. */
  978. spin_lock_irqsave(&priv->pw_fifo_lock, flags);
  979. while (kfifo_out(&priv->pw_fifo, (unsigned char *)msg_buffer,
  980. RIO_PW_MSG_SIZE)) {
  981. /* Process one message */
  982. spin_unlock_irqrestore(&priv->pw_fifo_lock, flags);
  983. #ifdef DEBUG_PW
  984. {
  985. u32 i;
  986. pr_debug("%s : Port-Write Message:", __func__);
  987. for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32); i++) {
  988. if ((i%4) == 0)
  989. pr_debug("\n0x%02x: 0x%08x", i*4,
  990. msg_buffer[i]);
  991. else
  992. pr_debug(" 0x%08x", msg_buffer[i]);
  993. }
  994. pr_debug("\n");
  995. }
  996. #endif
  997. /* Pass the port-write message to RIO core for processing */
  998. rio_inb_pwrite_handler((union rio_pw_msg *)msg_buffer);
  999. spin_lock_irqsave(&priv->pw_fifo_lock, flags);
  1000. }
  1001. spin_unlock_irqrestore(&priv->pw_fifo_lock, flags);
  1002. }
  1003. /**
  1004. * fsl_rio_pw_enable - enable/disable port-write interface init
  1005. * @mport: Master port implementing the port write unit
  1006. * @enable: 1=enable; 0=disable port-write message handling
  1007. */
  1008. static int fsl_rio_pw_enable(struct rio_mport *mport, int enable)
  1009. {
  1010. struct rio_priv *priv = mport->priv;
  1011. u32 rval;
  1012. rval = in_be32(&priv->msg_regs->pwmr);
  1013. if (enable)
  1014. rval |= RIO_IPWMR_PWE;
  1015. else
  1016. rval &= ~RIO_IPWMR_PWE;
  1017. out_be32(&priv->msg_regs->pwmr, rval);
  1018. return 0;
  1019. }
  1020. /**
  1021. * fsl_rio_port_write_init - MPC85xx port write interface init
  1022. * @mport: Master port implementing the port write unit
  1023. *
  1024. * Initializes port write unit hardware and DMA buffer
  1025. * ring. Called from fsl_rio_setup(). Returns %0 on success
  1026. * or %-ENOMEM on failure.
  1027. */
  1028. static int fsl_rio_port_write_init(struct rio_mport *mport)
  1029. {
  1030. struct rio_priv *priv = mport->priv;
  1031. int rc = 0;
  1032. /* Following configurations require a disabled port write controller */
  1033. out_be32(&priv->msg_regs->pwmr,
  1034. in_be32(&priv->msg_regs->pwmr) & ~RIO_IPWMR_PWE);
  1035. /* Initialize port write */
  1036. priv->port_write_msg.virt = dma_alloc_coherent(priv->dev,
  1037. RIO_PW_MSG_SIZE,
  1038. &priv->port_write_msg.phys, GFP_KERNEL);
  1039. if (!priv->port_write_msg.virt) {
  1040. pr_err("RIO: unable allocate port write queue\n");
  1041. return -ENOMEM;
  1042. }
  1043. priv->port_write_msg.err_count = 0;
  1044. priv->port_write_msg.discard_count = 0;
  1045. /* Point dequeue/enqueue pointers at first entry */
  1046. out_be32(&priv->msg_regs->epwqbar, 0);
  1047. out_be32(&priv->msg_regs->pwqbar, (u32) priv->port_write_msg.phys);
  1048. pr_debug("EIPWQBAR: 0x%08x IPWQBAR: 0x%08x\n",
  1049. in_be32(&priv->msg_regs->epwqbar),
  1050. in_be32(&priv->msg_regs->pwqbar));
  1051. /* Clear interrupt status IPWSR */
  1052. out_be32(&priv->msg_regs->pwsr,
  1053. (RIO_IPWSR_TE | RIO_IPWSR_QFI | RIO_IPWSR_PWD));
  1054. /* Configure port write contoller for snooping enable all reporting,
  1055. clear queue full */
  1056. out_be32(&priv->msg_regs->pwmr,
  1057. RIO_IPWMR_SEN | RIO_IPWMR_QFIE | RIO_IPWMR_EIE | RIO_IPWMR_CQ);
  1058. /* Hook up port-write handler */
  1059. rc = request_irq(IRQ_RIO_PW(mport), fsl_rio_port_write_handler, 0,
  1060. "port-write", (void *)mport);
  1061. if (rc < 0) {
  1062. pr_err("MPC85xx RIO: unable to request inbound doorbell irq");
  1063. goto err_out;
  1064. }
  1065. INIT_WORK(&priv->pw_work, fsl_pw_dpc);
  1066. spin_lock_init(&priv->pw_fifo_lock);
  1067. if (kfifo_alloc(&priv->pw_fifo, RIO_PW_MSG_SIZE * 32, GFP_KERNEL)) {
  1068. pr_err("FIFO allocation failed\n");
  1069. rc = -ENOMEM;
  1070. goto err_out_irq;
  1071. }
  1072. pr_debug("IPWMR: 0x%08x IPWSR: 0x%08x\n",
  1073. in_be32(&priv->msg_regs->pwmr),
  1074. in_be32(&priv->msg_regs->pwsr));
  1075. return rc;
  1076. err_out_irq:
  1077. free_irq(IRQ_RIO_PW(mport), (void *)mport);
  1078. err_out:
  1079. dma_free_coherent(priv->dev, RIO_PW_MSG_SIZE,
  1080. priv->port_write_msg.virt,
  1081. priv->port_write_msg.phys);
  1082. return rc;
  1083. }
  1084. static char *cmdline = NULL;
  1085. static int fsl_rio_get_hdid(int index)
  1086. {
  1087. /* XXX Need to parse multiple entries in some format */
  1088. if (!cmdline)
  1089. return -1;
  1090. return simple_strtol(cmdline, NULL, 0);
  1091. }
  1092. static int fsl_rio_get_cmdline(char *s)
  1093. {
  1094. if (!s)
  1095. return 0;
  1096. cmdline = s;
  1097. return 1;
  1098. }
  1099. __setup("riohdid=", fsl_rio_get_cmdline);
  1100. static inline void fsl_rio_info(struct device *dev, u32 ccsr)
  1101. {
  1102. const char *str;
  1103. if (ccsr & 1) {
  1104. /* Serial phy */
  1105. switch (ccsr >> 30) {
  1106. case 0:
  1107. str = "1";
  1108. break;
  1109. case 1:
  1110. str = "4";
  1111. break;
  1112. default:
  1113. str = "Unknown";
  1114. break;
  1115. }
  1116. dev_info(dev, "Hardware port width: %s\n", str);
  1117. switch ((ccsr >> 27) & 7) {
  1118. case 0:
  1119. str = "Single-lane 0";
  1120. break;
  1121. case 1:
  1122. str = "Single-lane 2";
  1123. break;
  1124. case 2:
  1125. str = "Four-lane";
  1126. break;
  1127. default:
  1128. str = "Unknown";
  1129. break;
  1130. }
  1131. dev_info(dev, "Training connection status: %s\n", str);
  1132. } else {
  1133. /* Parallel phy */
  1134. if (!(ccsr & 0x80000000))
  1135. dev_info(dev, "Output port operating in 8-bit mode\n");
  1136. if (!(ccsr & 0x08000000))
  1137. dev_info(dev, "Input port operating in 8-bit mode\n");
  1138. }
  1139. }
  1140. /**
  1141. * fsl_rio_setup - Setup Freescale PowerPC RapidIO interface
  1142. * @dev: of_device pointer
  1143. *
  1144. * Initializes MPC85xx RapidIO hardware interface, configures
  1145. * master port with system-specific info, and registers the
  1146. * master port with the RapidIO subsystem.
  1147. */
  1148. int fsl_rio_setup(struct of_device *dev)
  1149. {
  1150. struct rio_ops *ops;
  1151. struct rio_mport *port;
  1152. struct rio_priv *priv;
  1153. int rc = 0;
  1154. const u32 *dt_range, *cell;
  1155. struct resource regs;
  1156. int rlen;
  1157. u32 ccsr;
  1158. u64 law_start, law_size;
  1159. int paw, aw, sw;
  1160. if (!dev->dev.of_node) {
  1161. dev_err(&dev->dev, "Device OF-Node is NULL");
  1162. return -EFAULT;
  1163. }
  1164. rc = of_address_to_resource(dev->dev.of_node, 0, &regs);
  1165. if (rc) {
  1166. dev_err(&dev->dev, "Can't get %s property 'reg'\n",
  1167. dev->dev.of_node->full_name);
  1168. return -EFAULT;
  1169. }
  1170. dev_info(&dev->dev, "Of-device full name %s\n", dev->dev.of_node->full_name);
  1171. dev_info(&dev->dev, "Regs: %pR\n", &regs);
  1172. dt_range = of_get_property(dev->dev.of_node, "ranges", &rlen);
  1173. if (!dt_range) {
  1174. dev_err(&dev->dev, "Can't get %s property 'ranges'\n",
  1175. dev->dev.of_node->full_name);
  1176. return -EFAULT;
  1177. }
  1178. /* Get node address wide */
  1179. cell = of_get_property(dev->dev.of_node, "#address-cells", NULL);
  1180. if (cell)
  1181. aw = *cell;
  1182. else
  1183. aw = of_n_addr_cells(dev->dev.of_node);
  1184. /* Get node size wide */
  1185. cell = of_get_property(dev->dev.of_node, "#size-cells", NULL);
  1186. if (cell)
  1187. sw = *cell;
  1188. else
  1189. sw = of_n_size_cells(dev->dev.of_node);
  1190. /* Get parent address wide wide */
  1191. paw = of_n_addr_cells(dev->dev.of_node);
  1192. law_start = of_read_number(dt_range + aw, paw);
  1193. law_size = of_read_number(dt_range + aw + paw, sw);
  1194. dev_info(&dev->dev, "LAW start 0x%016llx, size 0x%016llx.\n",
  1195. law_start, law_size);
  1196. ops = kzalloc(sizeof(struct rio_ops), GFP_KERNEL);
  1197. if (!ops) {
  1198. rc = -ENOMEM;
  1199. goto err_ops;
  1200. }
  1201. ops->lcread = fsl_local_config_read;
  1202. ops->lcwrite = fsl_local_config_write;
  1203. ops->cread = fsl_rio_config_read;
  1204. ops->cwrite = fsl_rio_config_write;
  1205. ops->dsend = fsl_rio_doorbell_send;
  1206. ops->pwenable = fsl_rio_pw_enable;
  1207. port = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
  1208. if (!port) {
  1209. rc = -ENOMEM;
  1210. goto err_port;
  1211. }
  1212. port->id = 0;
  1213. port->index = 0;
  1214. priv = kzalloc(sizeof(struct rio_priv), GFP_KERNEL);
  1215. if (!priv) {
  1216. printk(KERN_ERR "Can't alloc memory for 'priv'\n");
  1217. rc = -ENOMEM;
  1218. goto err_priv;
  1219. }
  1220. INIT_LIST_HEAD(&port->dbells);
  1221. port->iores.start = law_start;
  1222. port->iores.end = law_start + law_size - 1;
  1223. port->iores.flags = IORESOURCE_MEM;
  1224. port->iores.name = "rio_io_win";
  1225. priv->pwirq = irq_of_parse_and_map(dev->node, 0);
  1226. priv->bellirq = irq_of_parse_and_map(dev->dev.of_node, 2);
  1227. priv->txirq = irq_of_parse_and_map(dev->dev.of_node, 3);
  1228. priv->rxirq = irq_of_parse_and_map(dev->dev.of_node, 4);
  1229. dev_info(&dev->dev, "pwirq: %d, bellirq: %d, txirq: %d, rxirq %d\n",
  1230. priv->pwirq, priv->bellirq, priv->txirq, priv->rxirq);
  1231. rio_init_dbell_res(&port->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
  1232. rio_init_mbox_res(&port->riores[RIO_INB_MBOX_RESOURCE], 0, 0);
  1233. rio_init_mbox_res(&port->riores[RIO_OUTB_MBOX_RESOURCE], 0, 0);
  1234. strcpy(port->name, "RIO0 mport");
  1235. priv->dev = &dev->dev;
  1236. port->ops = ops;
  1237. port->host_deviceid = fsl_rio_get_hdid(port->id);
  1238. port->priv = priv;
  1239. rio_register_mport(port);
  1240. priv->regs_win = ioremap(regs.start, regs.end - regs.start + 1);
  1241. rio_regs_win = priv->regs_win;
  1242. /* Probe the master port phy type */
  1243. ccsr = in_be32(priv->regs_win + RIO_CCSR);
  1244. port->phy_type = (ccsr & 1) ? RIO_PHY_SERIAL : RIO_PHY_PARALLEL;
  1245. dev_info(&dev->dev, "RapidIO PHY type: %s\n",
  1246. (port->phy_type == RIO_PHY_PARALLEL) ? "parallel" :
  1247. ((port->phy_type == RIO_PHY_SERIAL) ? "serial" :
  1248. "unknown"));
  1249. /* Checking the port training status */
  1250. if (in_be32((priv->regs_win + RIO_ESCSR)) & 1) {
  1251. dev_err(&dev->dev, "Port is not ready. "
  1252. "Try to restart connection...\n");
  1253. switch (port->phy_type) {
  1254. case RIO_PHY_SERIAL:
  1255. /* Disable ports */
  1256. out_be32(priv->regs_win + RIO_CCSR, 0);
  1257. /* Set 1x lane */
  1258. setbits32(priv->regs_win + RIO_CCSR, 0x02000000);
  1259. /* Enable ports */
  1260. setbits32(priv->regs_win + RIO_CCSR, 0x00600000);
  1261. break;
  1262. case RIO_PHY_PARALLEL:
  1263. /* Disable ports */
  1264. out_be32(priv->regs_win + RIO_CCSR, 0x22000000);
  1265. /* Enable ports */
  1266. out_be32(priv->regs_win + RIO_CCSR, 0x44000000);
  1267. break;
  1268. }
  1269. msleep(100);
  1270. if (in_be32((priv->regs_win + RIO_ESCSR)) & 1) {
  1271. dev_err(&dev->dev, "Port restart failed.\n");
  1272. rc = -ENOLINK;
  1273. goto err;
  1274. }
  1275. dev_info(&dev->dev, "Port restart success!\n");
  1276. }
  1277. fsl_rio_info(&dev->dev, ccsr);
  1278. port->sys_size = (in_be32((priv->regs_win + RIO_PEF_CAR))
  1279. & RIO_PEF_CTLS) >> 4;
  1280. dev_info(&dev->dev, "RapidIO Common Transport System size: %d\n",
  1281. port->sys_size ? 65536 : 256);
  1282. priv->atmu_regs = (struct rio_atmu_regs *)(priv->regs_win
  1283. + RIO_ATMU_REGS_OFFSET);
  1284. priv->maint_atmu_regs = priv->atmu_regs + 1;
  1285. priv->dbell_atmu_regs = priv->atmu_regs + 2;
  1286. priv->msg_regs = (struct rio_msg_regs *)(priv->regs_win +
  1287. ((port->phy_type == RIO_PHY_SERIAL) ?
  1288. RIO_S_MSG_REGS_OFFSET : RIO_P_MSG_REGS_OFFSET));
  1289. /* Set to receive any dist ID for serial RapidIO controller. */
  1290. if (port->phy_type == RIO_PHY_SERIAL)
  1291. out_be32((priv->regs_win + RIO_ISR_AACR), RIO_ISR_AACR_AA);
  1292. /* Configure maintenance transaction window */
  1293. out_be32(&priv->maint_atmu_regs->rowbar, law_start >> 12);
  1294. out_be32(&priv->maint_atmu_regs->rowar, 0x80077015); /* 4M */
  1295. priv->maint_win = ioremap(law_start, RIO_MAINT_WIN_SIZE);
  1296. /* Configure outbound doorbell window */
  1297. out_be32(&priv->dbell_atmu_regs->rowbar,
  1298. (law_start + RIO_MAINT_WIN_SIZE) >> 12);
  1299. out_be32(&priv->dbell_atmu_regs->rowar, 0x8004200b); /* 4k */
  1300. fsl_rio_doorbell_init(port);
  1301. fsl_rio_port_write_init(port);
  1302. saved_mcheck_exception = ppc_md.machine_check_exception;
  1303. ppc_md.machine_check_exception = fsl_rio_mcheck_exception;
  1304. /* Ensure that RFXE is set */
  1305. mtspr(SPRN_HID1, (mfspr(SPRN_HID1) | 0x20000));
  1306. return 0;
  1307. err:
  1308. iounmap(priv->regs_win);
  1309. kfree(priv);
  1310. err_priv:
  1311. kfree(port);
  1312. err_port:
  1313. kfree(ops);
  1314. err_ops:
  1315. return rc;
  1316. }
  1317. /* The probe function for RapidIO peer-to-peer network.
  1318. */
  1319. static int __devinit fsl_of_rio_rpn_probe(struct of_device *dev,
  1320. const struct of_device_id *match)
  1321. {
  1322. int rc;
  1323. printk(KERN_INFO "Setting up RapidIO peer-to-peer network %s\n",
  1324. dev->dev.of_node->full_name);
  1325. rc = fsl_rio_setup(dev);
  1326. if (rc)
  1327. goto out;
  1328. /* Enumerate all registered ports */
  1329. rc = rio_init_mports();
  1330. out:
  1331. return rc;
  1332. };
  1333. static const struct of_device_id fsl_of_rio_rpn_ids[] = {
  1334. {
  1335. .compatible = "fsl,rapidio-delta",
  1336. },
  1337. {},
  1338. };
  1339. static struct of_platform_driver fsl_of_rio_rpn_driver = {
  1340. .driver = {
  1341. .name = "fsl-of-rio",
  1342. .owner = THIS_MODULE,
  1343. .of_match_table = fsl_of_rio_rpn_ids,
  1344. },
  1345. .probe = fsl_of_rio_rpn_probe,
  1346. };
  1347. static __init int fsl_of_rio_rpn_init(void)
  1348. {
  1349. return of_register_platform_driver(&fsl_of_rio_rpn_driver);
  1350. }
  1351. subsys_initcall(fsl_of_rio_rpn_init);