fsl_rio.c 44 KB

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