fsl_rio.c 44 KB

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