fsl_rio.c 42 KB

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