fsl_rio.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  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. * rio_hw_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. int
  449. rio_hw_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
  458. ("RIO: rio_hw_add_outb_message(): destid %4.4x mbox %d buffer %8.8x len %8.8x\n",
  459. rdev->destid, mbox, (int)buffer, len);
  460. if ((len < 8) || (len > RIO_MAX_MSG_SIZE)) {
  461. ret = -EINVAL;
  462. goto out;
  463. }
  464. /* Copy and clear rest of buffer */
  465. memcpy(priv->msg_tx_ring.virt_buffer[priv->msg_tx_ring.tx_slot], buffer,
  466. len);
  467. if (len < (RIO_MAX_MSG_SIZE - 4))
  468. memset(priv->msg_tx_ring.virt_buffer[priv->msg_tx_ring.tx_slot]
  469. + len, 0, RIO_MAX_MSG_SIZE - len);
  470. switch (mport->phy_type) {
  471. case RIO_PHY_PARALLEL:
  472. /* Set mbox field for message */
  473. desc->dport = mbox & 0x3;
  474. /* Enable EOMI interrupt, set priority, and set destid */
  475. desc->dattr = 0x28000000 | (rdev->destid << 2);
  476. break;
  477. case RIO_PHY_SERIAL:
  478. /* Set mbox field for message, and set destid */
  479. desc->dport = (rdev->destid << 16) | (mbox & 0x3);
  480. /* Enable EOMI interrupt and priority */
  481. desc->dattr = 0x28000000;
  482. break;
  483. }
  484. /* Set transfer size aligned to next power of 2 (in double words) */
  485. desc->dwcnt = is_power_of_2(len) ? len : 1 << get_bitmask_order(len);
  486. /* Set snooping and source buffer address */
  487. desc->saddr = 0x00000004
  488. | priv->msg_tx_ring.phys_buffer[priv->msg_tx_ring.tx_slot];
  489. /* Increment enqueue pointer */
  490. omr = in_be32(&priv->msg_regs->omr);
  491. out_be32(&priv->msg_regs->omr, omr | RIO_MSG_OMR_MUI);
  492. /* Go to next descriptor */
  493. if (++priv->msg_tx_ring.tx_slot == priv->msg_tx_ring.size)
  494. priv->msg_tx_ring.tx_slot = 0;
  495. out:
  496. return ret;
  497. }
  498. EXPORT_SYMBOL_GPL(rio_hw_add_outb_message);
  499. /**
  500. * fsl_rio_tx_handler - MPC85xx outbound message interrupt handler
  501. * @irq: Linux interrupt number
  502. * @dev_instance: Pointer to interrupt-specific data
  503. *
  504. * Handles outbound message interrupts. Executes a register outbound
  505. * mailbox event handler and acks the interrupt occurrence.
  506. */
  507. static irqreturn_t
  508. fsl_rio_tx_handler(int irq, void *dev_instance)
  509. {
  510. int osr;
  511. struct rio_mport *port = (struct rio_mport *)dev_instance;
  512. struct rio_priv *priv = port->priv;
  513. osr = in_be32(&priv->msg_regs->osr);
  514. if (osr & RIO_MSG_OSR_TE) {
  515. pr_info("RIO: outbound message transmission error\n");
  516. out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_TE);
  517. goto out;
  518. }
  519. if (osr & RIO_MSG_OSR_QOI) {
  520. pr_info("RIO: outbound message queue overflow\n");
  521. out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_QOI);
  522. goto out;
  523. }
  524. if (osr & RIO_MSG_OSR_EOMI) {
  525. u32 dqp = in_be32(&priv->msg_regs->odqdpar);
  526. int slot = (dqp - priv->msg_tx_ring.phys) >> 5;
  527. port->outb_msg[0].mcback(port, priv->msg_tx_ring.dev_id, -1,
  528. slot);
  529. /* Ack the end-of-message interrupt */
  530. out_be32(&priv->msg_regs->osr, RIO_MSG_OSR_EOMI);
  531. }
  532. out:
  533. return IRQ_HANDLED;
  534. }
  535. /**
  536. * rio_open_outb_mbox - Initialize MPC85xx outbound mailbox
  537. * @mport: Master port implementing the outbound message unit
  538. * @dev_id: Device specific pointer to pass on event
  539. * @mbox: Mailbox to open
  540. * @entries: Number of entries in the outbound mailbox ring
  541. *
  542. * Initializes buffer ring, request the outbound message interrupt,
  543. * and enables the outbound message unit. Returns %0 on success and
  544. * %-EINVAL or %-ENOMEM on failure.
  545. */
  546. int rio_open_outb_mbox(struct rio_mport *mport, void *dev_id, int mbox, int entries)
  547. {
  548. int i, j, rc = 0;
  549. struct rio_priv *priv = mport->priv;
  550. if ((entries < RIO_MIN_TX_RING_SIZE) ||
  551. (entries > RIO_MAX_TX_RING_SIZE) || (!is_power_of_2(entries))) {
  552. rc = -EINVAL;
  553. goto out;
  554. }
  555. /* Initialize shadow copy ring */
  556. priv->msg_tx_ring.dev_id = dev_id;
  557. priv->msg_tx_ring.size = entries;
  558. for (i = 0; i < priv->msg_tx_ring.size; i++) {
  559. priv->msg_tx_ring.virt_buffer[i] =
  560. dma_alloc_coherent(priv->dev, RIO_MSG_BUFFER_SIZE,
  561. &priv->msg_tx_ring.phys_buffer[i], GFP_KERNEL);
  562. if (!priv->msg_tx_ring.virt_buffer[i]) {
  563. rc = -ENOMEM;
  564. for (j = 0; j < priv->msg_tx_ring.size; j++)
  565. if (priv->msg_tx_ring.virt_buffer[j])
  566. dma_free_coherent(priv->dev,
  567. RIO_MSG_BUFFER_SIZE,
  568. priv->msg_tx_ring.
  569. virt_buffer[j],
  570. priv->msg_tx_ring.
  571. phys_buffer[j]);
  572. goto out;
  573. }
  574. }
  575. /* Initialize outbound message descriptor ring */
  576. priv->msg_tx_ring.virt = dma_alloc_coherent(priv->dev,
  577. priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
  578. &priv->msg_tx_ring.phys, GFP_KERNEL);
  579. if (!priv->msg_tx_ring.virt) {
  580. rc = -ENOMEM;
  581. goto out_dma;
  582. }
  583. memset(priv->msg_tx_ring.virt, 0,
  584. priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE);
  585. priv->msg_tx_ring.tx_slot = 0;
  586. /* Point dequeue/enqueue pointers at first entry in ring */
  587. out_be32(&priv->msg_regs->odqdpar, priv->msg_tx_ring.phys);
  588. out_be32(&priv->msg_regs->odqepar, priv->msg_tx_ring.phys);
  589. /* Configure for snooping */
  590. out_be32(&priv->msg_regs->osar, 0x00000004);
  591. /* Clear interrupt status */
  592. out_be32(&priv->msg_regs->osr, 0x000000b3);
  593. /* Hook up outbound message handler */
  594. rc = request_irq(IRQ_RIO_TX(mport), fsl_rio_tx_handler, 0,
  595. "msg_tx", (void *)mport);
  596. if (rc < 0)
  597. goto out_irq;
  598. /*
  599. * Configure outbound message unit
  600. * Snooping
  601. * Interrupts (all enabled, except QEIE)
  602. * Chaining mode
  603. * Disable
  604. */
  605. out_be32(&priv->msg_regs->omr, 0x00100220);
  606. /* Set number of entries */
  607. out_be32(&priv->msg_regs->omr,
  608. in_be32(&priv->msg_regs->omr) |
  609. ((get_bitmask_order(entries) - 2) << 12));
  610. /* Now enable the unit */
  611. out_be32(&priv->msg_regs->omr, in_be32(&priv->msg_regs->omr) | 0x1);
  612. out:
  613. return rc;
  614. out_irq:
  615. dma_free_coherent(priv->dev,
  616. priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
  617. priv->msg_tx_ring.virt, priv->msg_tx_ring.phys);
  618. out_dma:
  619. for (i = 0; i < priv->msg_tx_ring.size; i++)
  620. dma_free_coherent(priv->dev, RIO_MSG_BUFFER_SIZE,
  621. priv->msg_tx_ring.virt_buffer[i],
  622. priv->msg_tx_ring.phys_buffer[i]);
  623. return rc;
  624. }
  625. /**
  626. * rio_close_outb_mbox - Shut down MPC85xx outbound mailbox
  627. * @mport: Master port implementing the outbound message unit
  628. * @mbox: Mailbox to close
  629. *
  630. * Disables the outbound message unit, free all buffers, and
  631. * frees the outbound message interrupt.
  632. */
  633. void rio_close_outb_mbox(struct rio_mport *mport, int mbox)
  634. {
  635. struct rio_priv *priv = mport->priv;
  636. /* Disable inbound message unit */
  637. out_be32(&priv->msg_regs->omr, 0);
  638. /* Free ring */
  639. dma_free_coherent(priv->dev,
  640. priv->msg_tx_ring.size * RIO_MSG_DESC_SIZE,
  641. priv->msg_tx_ring.virt, priv->msg_tx_ring.phys);
  642. /* Free interrupt */
  643. free_irq(IRQ_RIO_TX(mport), (void *)mport);
  644. }
  645. /**
  646. * fsl_rio_rx_handler - MPC85xx inbound message interrupt handler
  647. * @irq: Linux interrupt number
  648. * @dev_instance: Pointer to interrupt-specific data
  649. *
  650. * Handles inbound message interrupts. Executes a registered inbound
  651. * mailbox event handler and acks the interrupt occurrence.
  652. */
  653. static irqreturn_t
  654. fsl_rio_rx_handler(int irq, void *dev_instance)
  655. {
  656. int isr;
  657. struct rio_mport *port = (struct rio_mport *)dev_instance;
  658. struct rio_priv *priv = port->priv;
  659. isr = in_be32(&priv->msg_regs->isr);
  660. if (isr & RIO_MSG_ISR_TE) {
  661. pr_info("RIO: inbound message reception error\n");
  662. out_be32((void *)&priv->msg_regs->isr, RIO_MSG_ISR_TE);
  663. goto out;
  664. }
  665. /* XXX Need to check/dispatch until queue empty */
  666. if (isr & RIO_MSG_ISR_DIQI) {
  667. /*
  668. * We implement *only* mailbox 0, but can receive messages
  669. * for any mailbox/letter to that mailbox destination. So,
  670. * make the callback with an unknown/invalid mailbox number
  671. * argument.
  672. */
  673. port->inb_msg[0].mcback(port, priv->msg_rx_ring.dev_id, -1, -1);
  674. /* Ack the queueing interrupt */
  675. out_be32(&priv->msg_regs->isr, RIO_MSG_ISR_DIQI);
  676. }
  677. out:
  678. return IRQ_HANDLED;
  679. }
  680. /**
  681. * rio_open_inb_mbox - Initialize MPC85xx inbound mailbox
  682. * @mport: Master port implementing the inbound message unit
  683. * @dev_id: Device specific pointer to pass on event
  684. * @mbox: Mailbox to open
  685. * @entries: Number of entries in the inbound mailbox ring
  686. *
  687. * Initializes buffer ring, request the inbound message interrupt,
  688. * and enables the inbound message unit. Returns %0 on success
  689. * and %-EINVAL or %-ENOMEM on failure.
  690. */
  691. int rio_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. * rio_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. void rio_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. * rio_hw_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. int rio_hw_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: rio_hw_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. EXPORT_SYMBOL_GPL(rio_hw_add_inb_buffer);
  791. /**
  792. * rio_hw_get_inb_message - Fetch inbound message from the MPC85xx message unit
  793. * @mport: Master port implementing the inbound message unit
  794. * @mbox: Inbound mailbox number
  795. *
  796. * Gets the next available inbound message from the inbound message queue.
  797. * A pointer to the message is returned on success or NULL on failure.
  798. */
  799. void *rio_hw_get_inb_message(struct rio_mport *mport, int mbox)
  800. {
  801. struct rio_priv *priv = mport->priv;
  802. u32 phys_buf, virt_buf;
  803. void *buf = NULL;
  804. int buf_idx;
  805. phys_buf = in_be32(&priv->msg_regs->ifqdpar);
  806. /* If no more messages, then bail out */
  807. if (phys_buf == in_be32(&priv->msg_regs->ifqepar))
  808. goto out2;
  809. virt_buf = (u32) priv->msg_rx_ring.virt + (phys_buf
  810. - priv->msg_rx_ring.phys);
  811. buf_idx = (phys_buf - priv->msg_rx_ring.phys) / RIO_MAX_MSG_SIZE;
  812. buf = priv->msg_rx_ring.virt_buffer[buf_idx];
  813. if (!buf) {
  814. printk(KERN_ERR
  815. "RIO: inbound message copy failed, no buffers\n");
  816. goto out1;
  817. }
  818. /* Copy max message size, caller is expected to allocate that big */
  819. memcpy(buf, (void *)virt_buf, RIO_MAX_MSG_SIZE);
  820. /* Clear the available buffer */
  821. priv->msg_rx_ring.virt_buffer[buf_idx] = NULL;
  822. out1:
  823. setbits32(&priv->msg_regs->imr, RIO_MSG_IMR_MI);
  824. out2:
  825. return buf;
  826. }
  827. EXPORT_SYMBOL_GPL(rio_hw_get_inb_message);
  828. /**
  829. * fsl_rio_dbell_handler - MPC85xx doorbell interrupt handler
  830. * @irq: Linux interrupt number
  831. * @dev_instance: Pointer to interrupt-specific data
  832. *
  833. * Handles doorbell interrupts. Parses a list of registered
  834. * doorbell event handlers and executes a matching event handler.
  835. */
  836. static irqreturn_t
  837. fsl_rio_dbell_handler(int irq, void *dev_instance)
  838. {
  839. int dsr;
  840. struct rio_mport *port = (struct rio_mport *)dev_instance;
  841. struct rio_priv *priv = port->priv;
  842. dsr = in_be32(&priv->msg_regs->dsr);
  843. if (dsr & DOORBELL_DSR_TE) {
  844. pr_info("RIO: doorbell reception error\n");
  845. out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_TE);
  846. goto out;
  847. }
  848. if (dsr & DOORBELL_DSR_QFI) {
  849. pr_info("RIO: doorbell queue full\n");
  850. out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_QFI);
  851. goto out;
  852. }
  853. /* XXX Need to check/dispatch until queue empty */
  854. if (dsr & DOORBELL_DSR_DIQI) {
  855. u32 dmsg =
  856. (u32) priv->dbell_ring.virt +
  857. (in_be32(&priv->msg_regs->dqdpar) & 0xfff);
  858. struct rio_dbell *dbell;
  859. int found = 0;
  860. pr_debug
  861. ("RIO: processing doorbell, sid %2.2x tid %2.2x info %4.4x\n",
  862. DBELL_SID(dmsg), DBELL_TID(dmsg), DBELL_INF(dmsg));
  863. list_for_each_entry(dbell, &port->dbells, node) {
  864. if ((dbell->res->start <= DBELL_INF(dmsg)) &&
  865. (dbell->res->end >= DBELL_INF(dmsg))) {
  866. found = 1;
  867. break;
  868. }
  869. }
  870. if (found) {
  871. dbell->dinb(port, dbell->dev_id, DBELL_SID(dmsg), DBELL_TID(dmsg),
  872. DBELL_INF(dmsg));
  873. } else {
  874. pr_debug
  875. ("RIO: spurious doorbell, sid %2.2x tid %2.2x info %4.4x\n",
  876. DBELL_SID(dmsg), DBELL_TID(dmsg), DBELL_INF(dmsg));
  877. }
  878. setbits32(&priv->msg_regs->dmr, DOORBELL_DMR_DI);
  879. out_be32(&priv->msg_regs->dsr, DOORBELL_DSR_DIQI);
  880. }
  881. out:
  882. return IRQ_HANDLED;
  883. }
  884. /**
  885. * fsl_rio_doorbell_init - MPC85xx doorbell interface init
  886. * @mport: Master port implementing the inbound doorbell unit
  887. *
  888. * Initializes doorbell unit hardware and inbound DMA buffer
  889. * ring. Called from fsl_rio_setup(). Returns %0 on success
  890. * or %-ENOMEM on failure.
  891. */
  892. static int fsl_rio_doorbell_init(struct rio_mport *mport)
  893. {
  894. struct rio_priv *priv = mport->priv;
  895. int rc = 0;
  896. /* Map outbound doorbell window immediately after maintenance window */
  897. priv->dbell_win = ioremap(mport->iores.start + RIO_MAINT_WIN_SIZE,
  898. RIO_DBELL_WIN_SIZE);
  899. if (!priv->dbell_win) {
  900. printk(KERN_ERR
  901. "RIO: unable to map outbound doorbell window\n");
  902. rc = -ENOMEM;
  903. goto out;
  904. }
  905. /* Initialize inbound doorbells */
  906. priv->dbell_ring.virt = dma_alloc_coherent(priv->dev, 512 *
  907. DOORBELL_MESSAGE_SIZE, &priv->dbell_ring.phys, GFP_KERNEL);
  908. if (!priv->dbell_ring.virt) {
  909. printk(KERN_ERR "RIO: unable allocate inbound doorbell ring\n");
  910. rc = -ENOMEM;
  911. iounmap(priv->dbell_win);
  912. goto out;
  913. }
  914. /* Point dequeue/enqueue pointers at first entry in ring */
  915. out_be32(&priv->msg_regs->dqdpar, (u32) priv->dbell_ring.phys);
  916. out_be32(&priv->msg_regs->dqepar, (u32) priv->dbell_ring.phys);
  917. /* Clear interrupt status */
  918. out_be32(&priv->msg_regs->dsr, 0x00000091);
  919. /* Hook up doorbell handler */
  920. rc = request_irq(IRQ_RIO_BELL(mport), fsl_rio_dbell_handler, 0,
  921. "dbell_rx", (void *)mport);
  922. if (rc < 0) {
  923. iounmap(priv->dbell_win);
  924. dma_free_coherent(priv->dev, 512 * DOORBELL_MESSAGE_SIZE,
  925. priv->dbell_ring.virt, priv->dbell_ring.phys);
  926. printk(KERN_ERR
  927. "MPC85xx RIO: unable to request inbound doorbell irq");
  928. goto out;
  929. }
  930. /* Configure doorbells for snooping, 512 entries, and enable */
  931. out_be32(&priv->msg_regs->dmr, 0x00108161);
  932. out:
  933. return rc;
  934. }
  935. /**
  936. * fsl_rio_port_write_handler - MPC85xx port write interrupt handler
  937. * @irq: Linux interrupt number
  938. * @dev_instance: Pointer to interrupt-specific data
  939. *
  940. * Handles port write interrupts. Parses a list of registered
  941. * port write event handlers and executes a matching event handler.
  942. */
  943. static irqreturn_t
  944. fsl_rio_port_write_handler(int irq, void *dev_instance)
  945. {
  946. u32 ipwmr, ipwsr;
  947. struct rio_mport *port = (struct rio_mport *)dev_instance;
  948. struct rio_priv *priv = port->priv;
  949. u32 epwisr, tmp;
  950. epwisr = in_be32(priv->regs_win + RIO_EPWISR);
  951. if (!(epwisr & RIO_EPWISR_PW))
  952. goto pw_done;
  953. ipwmr = in_be32(&priv->msg_regs->pwmr);
  954. ipwsr = in_be32(&priv->msg_regs->pwsr);
  955. #ifdef DEBUG_PW
  956. pr_debug("PW Int->IPWMR: 0x%08x IPWSR: 0x%08x (", ipwmr, ipwsr);
  957. if (ipwsr & RIO_IPWSR_QF)
  958. pr_debug(" QF");
  959. if (ipwsr & RIO_IPWSR_TE)
  960. pr_debug(" TE");
  961. if (ipwsr & RIO_IPWSR_QFI)
  962. pr_debug(" QFI");
  963. if (ipwsr & RIO_IPWSR_PWD)
  964. pr_debug(" PWD");
  965. if (ipwsr & RIO_IPWSR_PWB)
  966. pr_debug(" PWB");
  967. pr_debug(" )\n");
  968. #endif
  969. /* Schedule deferred processing if PW was received */
  970. if (ipwsr & RIO_IPWSR_QFI) {
  971. /* Save PW message (if there is room in FIFO),
  972. * otherwise discard it.
  973. */
  974. if (kfifo_avail(&priv->pw_fifo) >= RIO_PW_MSG_SIZE) {
  975. priv->port_write_msg.msg_count++;
  976. kfifo_in(&priv->pw_fifo, priv->port_write_msg.virt,
  977. RIO_PW_MSG_SIZE);
  978. } else {
  979. priv->port_write_msg.discard_count++;
  980. pr_debug("RIO: ISR Discarded Port-Write Msg(s) (%d)\n",
  981. priv->port_write_msg.discard_count);
  982. }
  983. /* Clear interrupt and issue Clear Queue command. This allows
  984. * another port-write to be received.
  985. */
  986. out_be32(&priv->msg_regs->pwsr, RIO_IPWSR_QFI);
  987. out_be32(&priv->msg_regs->pwmr, ipwmr | RIO_IPWMR_CQ);
  988. schedule_work(&priv->pw_work);
  989. }
  990. if ((ipwmr & RIO_IPWMR_EIE) && (ipwsr & RIO_IPWSR_TE)) {
  991. priv->port_write_msg.err_count++;
  992. pr_debug("RIO: Port-Write Transaction Err (%d)\n",
  993. priv->port_write_msg.err_count);
  994. /* Clear Transaction Error: port-write controller should be
  995. * disabled when clearing this error
  996. */
  997. out_be32(&priv->msg_regs->pwmr, ipwmr & ~RIO_IPWMR_PWE);
  998. out_be32(&priv->msg_regs->pwsr, RIO_IPWSR_TE);
  999. out_be32(&priv->msg_regs->pwmr, ipwmr);
  1000. }
  1001. if (ipwsr & RIO_IPWSR_PWD) {
  1002. priv->port_write_msg.discard_count++;
  1003. pr_debug("RIO: Port Discarded Port-Write Msg(s) (%d)\n",
  1004. priv->port_write_msg.discard_count);
  1005. out_be32(&priv->msg_regs->pwsr, RIO_IPWSR_PWD);
  1006. }
  1007. pw_done:
  1008. if (epwisr & RIO_EPWISR_PINT) {
  1009. tmp = in_be32(priv->regs_win + RIO_LTLEDCSR);
  1010. pr_debug("RIO_LTLEDCSR = 0x%x\n", tmp);
  1011. out_be32(priv->regs_win + RIO_LTLEDCSR, 0);
  1012. }
  1013. return IRQ_HANDLED;
  1014. }
  1015. static void fsl_pw_dpc(struct work_struct *work)
  1016. {
  1017. struct rio_priv *priv = container_of(work, struct rio_priv, pw_work);
  1018. unsigned long flags;
  1019. u32 msg_buffer[RIO_PW_MSG_SIZE/sizeof(u32)];
  1020. /*
  1021. * Process port-write messages
  1022. */
  1023. spin_lock_irqsave(&priv->pw_fifo_lock, flags);
  1024. while (kfifo_out(&priv->pw_fifo, (unsigned char *)msg_buffer,
  1025. RIO_PW_MSG_SIZE)) {
  1026. /* Process one message */
  1027. spin_unlock_irqrestore(&priv->pw_fifo_lock, flags);
  1028. #ifdef DEBUG_PW
  1029. {
  1030. u32 i;
  1031. pr_debug("%s : Port-Write Message:", __func__);
  1032. for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32); i++) {
  1033. if ((i%4) == 0)
  1034. pr_debug("\n0x%02x: 0x%08x", i*4,
  1035. msg_buffer[i]);
  1036. else
  1037. pr_debug(" 0x%08x", msg_buffer[i]);
  1038. }
  1039. pr_debug("\n");
  1040. }
  1041. #endif
  1042. /* Pass the port-write message to RIO core for processing */
  1043. rio_inb_pwrite_handler((union rio_pw_msg *)msg_buffer);
  1044. spin_lock_irqsave(&priv->pw_fifo_lock, flags);
  1045. }
  1046. spin_unlock_irqrestore(&priv->pw_fifo_lock, flags);
  1047. }
  1048. /**
  1049. * fsl_rio_pw_enable - enable/disable port-write interface init
  1050. * @mport: Master port implementing the port write unit
  1051. * @enable: 1=enable; 0=disable port-write message handling
  1052. */
  1053. static int fsl_rio_pw_enable(struct rio_mport *mport, int enable)
  1054. {
  1055. struct rio_priv *priv = mport->priv;
  1056. u32 rval;
  1057. rval = in_be32(&priv->msg_regs->pwmr);
  1058. if (enable)
  1059. rval |= RIO_IPWMR_PWE;
  1060. else
  1061. rval &= ~RIO_IPWMR_PWE;
  1062. out_be32(&priv->msg_regs->pwmr, rval);
  1063. return 0;
  1064. }
  1065. /**
  1066. * fsl_rio_port_write_init - MPC85xx port write interface init
  1067. * @mport: Master port implementing the port write unit
  1068. *
  1069. * Initializes port write unit hardware and DMA buffer
  1070. * ring. Called from fsl_rio_setup(). Returns %0 on success
  1071. * or %-ENOMEM on failure.
  1072. */
  1073. static int fsl_rio_port_write_init(struct rio_mport *mport)
  1074. {
  1075. struct rio_priv *priv = mport->priv;
  1076. int rc = 0;
  1077. /* Following configurations require a disabled port write controller */
  1078. out_be32(&priv->msg_regs->pwmr,
  1079. in_be32(&priv->msg_regs->pwmr) & ~RIO_IPWMR_PWE);
  1080. /* Initialize port write */
  1081. priv->port_write_msg.virt = dma_alloc_coherent(priv->dev,
  1082. RIO_PW_MSG_SIZE,
  1083. &priv->port_write_msg.phys, GFP_KERNEL);
  1084. if (!priv->port_write_msg.virt) {
  1085. pr_err("RIO: unable allocate port write queue\n");
  1086. return -ENOMEM;
  1087. }
  1088. priv->port_write_msg.err_count = 0;
  1089. priv->port_write_msg.discard_count = 0;
  1090. /* Point dequeue/enqueue pointers at first entry */
  1091. out_be32(&priv->msg_regs->epwqbar, 0);
  1092. out_be32(&priv->msg_regs->pwqbar, (u32) priv->port_write_msg.phys);
  1093. pr_debug("EIPWQBAR: 0x%08x IPWQBAR: 0x%08x\n",
  1094. in_be32(&priv->msg_regs->epwqbar),
  1095. in_be32(&priv->msg_regs->pwqbar));
  1096. /* Clear interrupt status IPWSR */
  1097. out_be32(&priv->msg_regs->pwsr,
  1098. (RIO_IPWSR_TE | RIO_IPWSR_QFI | RIO_IPWSR_PWD));
  1099. /* Configure port write contoller for snooping enable all reporting,
  1100. clear queue full */
  1101. out_be32(&priv->msg_regs->pwmr,
  1102. RIO_IPWMR_SEN | RIO_IPWMR_QFIE | RIO_IPWMR_EIE | RIO_IPWMR_CQ);
  1103. /* Hook up port-write handler */
  1104. rc = request_irq(IRQ_RIO_PW(mport), fsl_rio_port_write_handler, 0,
  1105. "port-write", (void *)mport);
  1106. if (rc < 0) {
  1107. pr_err("MPC85xx RIO: unable to request inbound doorbell irq");
  1108. goto err_out;
  1109. }
  1110. INIT_WORK(&priv->pw_work, fsl_pw_dpc);
  1111. spin_lock_init(&priv->pw_fifo_lock);
  1112. if (kfifo_alloc(&priv->pw_fifo, RIO_PW_MSG_SIZE * 32, GFP_KERNEL)) {
  1113. pr_err("FIFO allocation failed\n");
  1114. rc = -ENOMEM;
  1115. goto err_out_irq;
  1116. }
  1117. pr_debug("IPWMR: 0x%08x IPWSR: 0x%08x\n",
  1118. in_be32(&priv->msg_regs->pwmr),
  1119. in_be32(&priv->msg_regs->pwsr));
  1120. return rc;
  1121. err_out_irq:
  1122. free_irq(IRQ_RIO_PW(mport), (void *)mport);
  1123. err_out:
  1124. dma_free_coherent(priv->dev, RIO_PW_MSG_SIZE,
  1125. priv->port_write_msg.virt,
  1126. priv->port_write_msg.phys);
  1127. return rc;
  1128. }
  1129. static char *cmdline = NULL;
  1130. static int fsl_rio_get_hdid(int index)
  1131. {
  1132. /* XXX Need to parse multiple entries in some format */
  1133. if (!cmdline)
  1134. return -1;
  1135. return simple_strtol(cmdline, NULL, 0);
  1136. }
  1137. static int fsl_rio_get_cmdline(char *s)
  1138. {
  1139. if (!s)
  1140. return 0;
  1141. cmdline = s;
  1142. return 1;
  1143. }
  1144. __setup("riohdid=", fsl_rio_get_cmdline);
  1145. static inline void fsl_rio_info(struct device *dev, u32 ccsr)
  1146. {
  1147. const char *str;
  1148. if (ccsr & 1) {
  1149. /* Serial phy */
  1150. switch (ccsr >> 30) {
  1151. case 0:
  1152. str = "1";
  1153. break;
  1154. case 1:
  1155. str = "4";
  1156. break;
  1157. default:
  1158. str = "Unknown";
  1159. break;
  1160. }
  1161. dev_info(dev, "Hardware port width: %s\n", str);
  1162. switch ((ccsr >> 27) & 7) {
  1163. case 0:
  1164. str = "Single-lane 0";
  1165. break;
  1166. case 1:
  1167. str = "Single-lane 2";
  1168. break;
  1169. case 2:
  1170. str = "Four-lane";
  1171. break;
  1172. default:
  1173. str = "Unknown";
  1174. break;
  1175. }
  1176. dev_info(dev, "Training connection status: %s\n", str);
  1177. } else {
  1178. /* Parallel phy */
  1179. if (!(ccsr & 0x80000000))
  1180. dev_info(dev, "Output port operating in 8-bit mode\n");
  1181. if (!(ccsr & 0x08000000))
  1182. dev_info(dev, "Input port operating in 8-bit mode\n");
  1183. }
  1184. }
  1185. /**
  1186. * fsl_rio_setup - Setup Freescale PowerPC RapidIO interface
  1187. * @dev: platform_device pointer
  1188. *
  1189. * Initializes MPC85xx RapidIO hardware interface, configures
  1190. * master port with system-specific info, and registers the
  1191. * master port with the RapidIO subsystem.
  1192. */
  1193. int fsl_rio_setup(struct platform_device *dev)
  1194. {
  1195. struct rio_ops *ops;
  1196. struct rio_mport *port;
  1197. struct rio_priv *priv;
  1198. int rc = 0;
  1199. const u32 *dt_range, *cell;
  1200. struct resource regs;
  1201. int rlen;
  1202. u32 ccsr;
  1203. u64 law_start, law_size;
  1204. int paw, aw, sw;
  1205. if (!dev->dev.of_node) {
  1206. dev_err(&dev->dev, "Device OF-Node is NULL");
  1207. return -EFAULT;
  1208. }
  1209. rc = of_address_to_resource(dev->dev.of_node, 0, &regs);
  1210. if (rc) {
  1211. dev_err(&dev->dev, "Can't get %s property 'reg'\n",
  1212. dev->dev.of_node->full_name);
  1213. return -EFAULT;
  1214. }
  1215. dev_info(&dev->dev, "Of-device full name %s\n", dev->dev.of_node->full_name);
  1216. dev_info(&dev->dev, "Regs: %pR\n", &regs);
  1217. dt_range = of_get_property(dev->dev.of_node, "ranges", &rlen);
  1218. if (!dt_range) {
  1219. dev_err(&dev->dev, "Can't get %s property 'ranges'\n",
  1220. dev->dev.of_node->full_name);
  1221. return -EFAULT;
  1222. }
  1223. /* Get node address wide */
  1224. cell = of_get_property(dev->dev.of_node, "#address-cells", NULL);
  1225. if (cell)
  1226. aw = *cell;
  1227. else
  1228. aw = of_n_addr_cells(dev->dev.of_node);
  1229. /* Get node size wide */
  1230. cell = of_get_property(dev->dev.of_node, "#size-cells", NULL);
  1231. if (cell)
  1232. sw = *cell;
  1233. else
  1234. sw = of_n_size_cells(dev->dev.of_node);
  1235. /* Get parent address wide wide */
  1236. paw = of_n_addr_cells(dev->dev.of_node);
  1237. law_start = of_read_number(dt_range + aw, paw);
  1238. law_size = of_read_number(dt_range + aw + paw, sw);
  1239. dev_info(&dev->dev, "LAW start 0x%016llx, size 0x%016llx.\n",
  1240. law_start, law_size);
  1241. ops = kzalloc(sizeof(struct rio_ops), GFP_KERNEL);
  1242. if (!ops) {
  1243. rc = -ENOMEM;
  1244. goto err_ops;
  1245. }
  1246. ops->lcread = fsl_local_config_read;
  1247. ops->lcwrite = fsl_local_config_write;
  1248. ops->cread = fsl_rio_config_read;
  1249. ops->cwrite = fsl_rio_config_write;
  1250. ops->dsend = fsl_rio_doorbell_send;
  1251. ops->pwenable = fsl_rio_pw_enable;
  1252. port = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
  1253. if (!port) {
  1254. rc = -ENOMEM;
  1255. goto err_port;
  1256. }
  1257. port->id = 0;
  1258. port->index = 0;
  1259. priv = kzalloc(sizeof(struct rio_priv), GFP_KERNEL);
  1260. if (!priv) {
  1261. printk(KERN_ERR "Can't alloc memory for 'priv'\n");
  1262. rc = -ENOMEM;
  1263. goto err_priv;
  1264. }
  1265. INIT_LIST_HEAD(&port->dbells);
  1266. port->iores.start = law_start;
  1267. port->iores.end = law_start + law_size - 1;
  1268. port->iores.flags = IORESOURCE_MEM;
  1269. port->iores.name = "rio_io_win";
  1270. priv->pwirq = irq_of_parse_and_map(dev->dev.of_node, 0);
  1271. priv->bellirq = irq_of_parse_and_map(dev->dev.of_node, 2);
  1272. priv->txirq = irq_of_parse_and_map(dev->dev.of_node, 3);
  1273. priv->rxirq = irq_of_parse_and_map(dev->dev.of_node, 4);
  1274. dev_info(&dev->dev, "pwirq: %d, bellirq: %d, txirq: %d, rxirq %d\n",
  1275. priv->pwirq, priv->bellirq, priv->txirq, priv->rxirq);
  1276. rio_init_dbell_res(&port->riores[RIO_DOORBELL_RESOURCE], 0, 0xffff);
  1277. rio_init_mbox_res(&port->riores[RIO_INB_MBOX_RESOURCE], 0, 0);
  1278. rio_init_mbox_res(&port->riores[RIO_OUTB_MBOX_RESOURCE], 0, 0);
  1279. strcpy(port->name, "RIO0 mport");
  1280. priv->dev = &dev->dev;
  1281. port->ops = ops;
  1282. port->host_deviceid = fsl_rio_get_hdid(port->id);
  1283. port->priv = priv;
  1284. port->phys_efptr = 0x100;
  1285. rio_register_mport(port);
  1286. priv->regs_win = ioremap(regs.start, regs.end - regs.start + 1);
  1287. rio_regs_win = priv->regs_win;
  1288. /* Probe the master port phy type */
  1289. ccsr = in_be32(priv->regs_win + RIO_CCSR);
  1290. port->phy_type = (ccsr & 1) ? RIO_PHY_SERIAL : RIO_PHY_PARALLEL;
  1291. dev_info(&dev->dev, "RapidIO PHY type: %s\n",
  1292. (port->phy_type == RIO_PHY_PARALLEL) ? "parallel" :
  1293. ((port->phy_type == RIO_PHY_SERIAL) ? "serial" :
  1294. "unknown"));
  1295. /* Checking the port training status */
  1296. if (in_be32((priv->regs_win + RIO_ESCSR)) & 1) {
  1297. dev_err(&dev->dev, "Port is not ready. "
  1298. "Try to restart connection...\n");
  1299. switch (port->phy_type) {
  1300. case RIO_PHY_SERIAL:
  1301. /* Disable ports */
  1302. out_be32(priv->regs_win + RIO_CCSR, 0);
  1303. /* Set 1x lane */
  1304. setbits32(priv->regs_win + RIO_CCSR, 0x02000000);
  1305. /* Enable ports */
  1306. setbits32(priv->regs_win + RIO_CCSR, 0x00600000);
  1307. break;
  1308. case RIO_PHY_PARALLEL:
  1309. /* Disable ports */
  1310. out_be32(priv->regs_win + RIO_CCSR, 0x22000000);
  1311. /* Enable ports */
  1312. out_be32(priv->regs_win + RIO_CCSR, 0x44000000);
  1313. break;
  1314. }
  1315. msleep(100);
  1316. if (in_be32((priv->regs_win + RIO_ESCSR)) & 1) {
  1317. dev_err(&dev->dev, "Port restart failed.\n");
  1318. rc = -ENOLINK;
  1319. goto err;
  1320. }
  1321. dev_info(&dev->dev, "Port restart success!\n");
  1322. }
  1323. fsl_rio_info(&dev->dev, ccsr);
  1324. port->sys_size = (in_be32((priv->regs_win + RIO_PEF_CAR))
  1325. & RIO_PEF_CTLS) >> 4;
  1326. dev_info(&dev->dev, "RapidIO Common Transport System size: %d\n",
  1327. port->sys_size ? 65536 : 256);
  1328. if (port->host_deviceid >= 0)
  1329. out_be32(priv->regs_win + RIO_GCCSR, RIO_PORT_GEN_HOST |
  1330. RIO_PORT_GEN_MASTER | RIO_PORT_GEN_DISCOVERED);
  1331. else
  1332. out_be32(priv->regs_win + RIO_GCCSR, 0x00000000);
  1333. priv->atmu_regs = (struct rio_atmu_regs *)(priv->regs_win
  1334. + RIO_ATMU_REGS_OFFSET);
  1335. priv->maint_atmu_regs = priv->atmu_regs + 1;
  1336. priv->dbell_atmu_regs = priv->atmu_regs + 2;
  1337. priv->msg_regs = (struct rio_msg_regs *)(priv->regs_win +
  1338. ((port->phy_type == RIO_PHY_SERIAL) ?
  1339. RIO_S_MSG_REGS_OFFSET : RIO_P_MSG_REGS_OFFSET));
  1340. /* Set to receive any dist ID for serial RapidIO controller. */
  1341. if (port->phy_type == RIO_PHY_SERIAL)
  1342. out_be32((priv->regs_win + RIO_ISR_AACR), RIO_ISR_AACR_AA);
  1343. /* Configure maintenance transaction window */
  1344. out_be32(&priv->maint_atmu_regs->rowbar, law_start >> 12);
  1345. out_be32(&priv->maint_atmu_regs->rowar,
  1346. 0x80077000 | (ilog2(RIO_MAINT_WIN_SIZE) - 1));
  1347. priv->maint_win = ioremap(law_start, RIO_MAINT_WIN_SIZE);
  1348. /* Configure outbound doorbell window */
  1349. out_be32(&priv->dbell_atmu_regs->rowbar,
  1350. (law_start + RIO_MAINT_WIN_SIZE) >> 12);
  1351. out_be32(&priv->dbell_atmu_regs->rowar, 0x8004200b); /* 4k */
  1352. fsl_rio_doorbell_init(port);
  1353. fsl_rio_port_write_init(port);
  1354. #ifdef CONFIG_E500
  1355. saved_mcheck_exception = ppc_md.machine_check_exception;
  1356. ppc_md.machine_check_exception = fsl_rio_mcheck_exception;
  1357. #endif
  1358. return 0;
  1359. err:
  1360. iounmap(priv->regs_win);
  1361. kfree(priv);
  1362. err_priv:
  1363. kfree(port);
  1364. err_port:
  1365. kfree(ops);
  1366. err_ops:
  1367. return rc;
  1368. }
  1369. /* The probe function for RapidIO peer-to-peer network.
  1370. */
  1371. static int __devinit fsl_of_rio_rpn_probe(struct platform_device *dev,
  1372. const struct of_device_id *match)
  1373. {
  1374. int rc;
  1375. printk(KERN_INFO "Setting up RapidIO peer-to-peer network %s\n",
  1376. dev->dev.of_node->full_name);
  1377. rc = fsl_rio_setup(dev);
  1378. if (rc)
  1379. goto out;
  1380. /* Enumerate all registered ports */
  1381. rc = rio_init_mports();
  1382. out:
  1383. return rc;
  1384. };
  1385. static const struct of_device_id fsl_of_rio_rpn_ids[] = {
  1386. {
  1387. .compatible = "fsl,rapidio-delta",
  1388. },
  1389. {},
  1390. };
  1391. static struct of_platform_driver fsl_of_rio_rpn_driver = {
  1392. .driver = {
  1393. .name = "fsl-of-rio",
  1394. .owner = THIS_MODULE,
  1395. .of_match_table = fsl_of_rio_rpn_ids,
  1396. },
  1397. .probe = fsl_of_rio_rpn_probe,
  1398. };
  1399. static __init int fsl_of_rio_rpn_init(void)
  1400. {
  1401. return of_register_platform_driver(&fsl_of_rio_rpn_driver);
  1402. }
  1403. subsys_initcall(fsl_of_rio_rpn_init);