fsl_rio.c 44 KB

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