fsl_rio.c 44 KB

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