fsl_rio.c 44 KB

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