fsl_rio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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, 2010, 2011 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/extable.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/of_address.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/delay.h>
  34. #include <linux/slab.h>
  35. #include <linux/io.h>
  36. #include <linux/uaccess.h>
  37. #include <asm/machdep.h>
  38. #include "fsl_rio.h"
  39. #undef DEBUG_PW /* Port-Write debugging */
  40. #define RIO_PORT1_EDCSR 0x0640
  41. #define RIO_PORT2_EDCSR 0x0680
  42. #define RIO_PORT1_IECSR 0x10130
  43. #define RIO_PORT2_IECSR 0x101B0
  44. #define RIO_GCCSR 0x13c
  45. #define RIO_ESCSR 0x158
  46. #define ESCSR_CLEAR 0x07120204
  47. #define RIO_PORT2_ESCSR 0x178
  48. #define RIO_CCSR 0x15c
  49. #define RIO_LTLEDCSR_IER 0x80000000
  50. #define RIO_LTLEDCSR_PRT 0x01000000
  51. #define IECSR_CLEAR 0x80000000
  52. #define RIO_ISR_AACR 0x10120
  53. #define RIO_ISR_AACR_AA 0x1 /* Accept All ID */
  54. #define RIWTAR_TRAD_VAL_SHIFT 12
  55. #define RIWTAR_TRAD_MASK 0x00FFFFFF
  56. #define RIWBAR_BADD_VAL_SHIFT 12
  57. #define RIWBAR_BADD_MASK 0x003FFFFF
  58. #define RIWAR_ENABLE 0x80000000
  59. #define RIWAR_TGINT_LOCAL 0x00F00000
  60. #define RIWAR_RDTYP_NO_SNOOP 0x00040000
  61. #define RIWAR_RDTYP_SNOOP 0x00050000
  62. #define RIWAR_WRTYP_NO_SNOOP 0x00004000
  63. #define RIWAR_WRTYP_SNOOP 0x00005000
  64. #define RIWAR_WRTYP_ALLOC 0x00006000
  65. #define RIWAR_SIZE_MASK 0x0000003F
  66. static DEFINE_SPINLOCK(fsl_rio_config_lock);
  67. #define __fsl_read_rio_config(x, addr, err, op) \
  68. __asm__ __volatile__( \
  69. "1: "op" %1,0(%2)\n" \
  70. " eieio\n" \
  71. "2:\n" \
  72. ".section .fixup,\"ax\"\n" \
  73. "3: li %1,-1\n" \
  74. " li %0,%3\n" \
  75. " b 2b\n" \
  76. ".previous\n" \
  77. EX_TABLE(1b, 3b) \
  78. : "=r" (err), "=r" (x) \
  79. : "b" (addr), "i" (-EFAULT), "0" (err))
  80. void __iomem *rio_regs_win;
  81. void __iomem *rmu_regs_win;
  82. resource_size_t rio_law_start;
  83. struct fsl_rio_dbell *dbell;
  84. struct fsl_rio_pw *pw;
  85. #ifdef CONFIG_E500
  86. int fsl_rio_mcheck_exception(struct pt_regs *regs)
  87. {
  88. const struct exception_table_entry *entry;
  89. unsigned long reason;
  90. if (!rio_regs_win)
  91. return 0;
  92. reason = in_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR));
  93. if (reason & (RIO_LTLEDCSR_IER | RIO_LTLEDCSR_PRT)) {
  94. /* Check if we are prepared to handle this fault */
  95. entry = search_exception_tables(regs->nip);
  96. if (entry) {
  97. pr_debug("RIO: %s - MC Exception handled\n",
  98. __func__);
  99. out_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR),
  100. 0);
  101. regs->msr |= MSR_RI;
  102. regs->nip = extable_fixup(entry);
  103. return 1;
  104. }
  105. }
  106. return 0;
  107. }
  108. EXPORT_SYMBOL_GPL(fsl_rio_mcheck_exception);
  109. #endif
  110. /**
  111. * fsl_local_config_read - Generate a MPC85xx local config space read
  112. * @mport: RapidIO master port info
  113. * @index: ID of RapdiIO interface
  114. * @offset: Offset into configuration space
  115. * @len: Length (in bytes) of the maintenance transaction
  116. * @data: Value to be read into
  117. *
  118. * Generates a MPC85xx local configuration space read. Returns %0 on
  119. * success or %-EINVAL on failure.
  120. */
  121. static int fsl_local_config_read(struct rio_mport *mport,
  122. int index, u32 offset, int len, u32 *data)
  123. {
  124. struct rio_priv *priv = mport->priv;
  125. pr_debug("fsl_local_config_read: index %d offset %8.8x\n", index,
  126. offset);
  127. *data = in_be32(priv->regs_win + offset);
  128. return 0;
  129. }
  130. /**
  131. * fsl_local_config_write - Generate a MPC85xx local config space write
  132. * @mport: RapidIO master port info
  133. * @index: ID of RapdiIO interface
  134. * @offset: Offset into configuration space
  135. * @len: Length (in bytes) of the maintenance transaction
  136. * @data: Value to be written
  137. *
  138. * Generates a MPC85xx local configuration space write. Returns %0 on
  139. * success or %-EINVAL on failure.
  140. */
  141. static int fsl_local_config_write(struct rio_mport *mport,
  142. int index, u32 offset, int len, u32 data)
  143. {
  144. struct rio_priv *priv = mport->priv;
  145. pr_debug
  146. ("fsl_local_config_write: index %d offset %8.8x data %8.8x\n",
  147. index, offset, data);
  148. out_be32(priv->regs_win + offset, data);
  149. return 0;
  150. }
  151. /**
  152. * fsl_rio_config_read - Generate a MPC85xx read maintenance transaction
  153. * @mport: RapidIO master port info
  154. * @index: ID of RapdiIO interface
  155. * @destid: Destination ID of transaction
  156. * @hopcount: Number of hops to target device
  157. * @offset: Offset into configuration space
  158. * @len: Length (in bytes) of the maintenance transaction
  159. * @val: Location to be read into
  160. *
  161. * Generates a MPC85xx read maintenance transaction. Returns %0 on
  162. * success or %-EINVAL on failure.
  163. */
  164. static int
  165. fsl_rio_config_read(struct rio_mport *mport, int index, u16 destid,
  166. u8 hopcount, u32 offset, int len, u32 *val)
  167. {
  168. struct rio_priv *priv = mport->priv;
  169. unsigned long flags;
  170. u8 *data;
  171. u32 rval, err = 0;
  172. pr_debug
  173. ("fsl_rio_config_read:"
  174. " index %d destid %d hopcount %d offset %8.8x len %d\n",
  175. index, destid, hopcount, offset, len);
  176. /* 16MB maintenance window possible */
  177. /* allow only aligned access to maintenance registers */
  178. if (offset > (0x1000000 - len) || !IS_ALIGNED(offset, len))
  179. return -EINVAL;
  180. spin_lock_irqsave(&fsl_rio_config_lock, flags);
  181. out_be32(&priv->maint_atmu_regs->rowtar,
  182. (destid << 22) | (hopcount << 12) | (offset >> 12));
  183. out_be32(&priv->maint_atmu_regs->rowtear, (destid >> 10));
  184. data = (u8 *) priv->maint_win + (offset & (RIO_MAINT_WIN_SIZE - 1));
  185. switch (len) {
  186. case 1:
  187. __fsl_read_rio_config(rval, data, err, "lbz");
  188. break;
  189. case 2:
  190. __fsl_read_rio_config(rval, data, err, "lhz");
  191. break;
  192. case 4:
  193. __fsl_read_rio_config(rval, data, err, "lwz");
  194. break;
  195. default:
  196. spin_unlock_irqrestore(&fsl_rio_config_lock, flags);
  197. return -EINVAL;
  198. }
  199. if (err) {
  200. pr_debug("RIO: cfg_read error %d for %x:%x:%x\n",
  201. err, destid, hopcount, offset);
  202. }
  203. spin_unlock_irqrestore(&fsl_rio_config_lock, flags);
  204. *val = rval;
  205. return err;
  206. }
  207. /**
  208. * fsl_rio_config_write - Generate a MPC85xx write maintenance transaction
  209. * @mport: RapidIO master port info
  210. * @index: ID of RapdiIO interface
  211. * @destid: Destination ID of transaction
  212. * @hopcount: Number of hops to target device
  213. * @offset: Offset into configuration space
  214. * @len: Length (in bytes) of the maintenance transaction
  215. * @val: Value to be written
  216. *
  217. * Generates an MPC85xx write maintenance transaction. Returns %0 on
  218. * success or %-EINVAL on failure.
  219. */
  220. static int
  221. fsl_rio_config_write(struct rio_mport *mport, int index, u16 destid,
  222. u8 hopcount, u32 offset, int len, u32 val)
  223. {
  224. struct rio_priv *priv = mport->priv;
  225. unsigned long flags;
  226. u8 *data;
  227. int ret = 0;
  228. pr_debug
  229. ("fsl_rio_config_write:"
  230. " index %d destid %d hopcount %d offset %8.8x len %d val %8.8x\n",
  231. index, destid, hopcount, offset, len, val);
  232. /* 16MB maintenance windows possible */
  233. /* allow only aligned access to maintenance registers */
  234. if (offset > (0x1000000 - len) || !IS_ALIGNED(offset, len))
  235. return -EINVAL;
  236. spin_lock_irqsave(&fsl_rio_config_lock, flags);
  237. out_be32(&priv->maint_atmu_regs->rowtar,
  238. (destid << 22) | (hopcount << 12) | (offset >> 12));
  239. out_be32(&priv->maint_atmu_regs->rowtear, (destid >> 10));
  240. data = (u8 *) priv->maint_win + (offset & (RIO_MAINT_WIN_SIZE - 1));
  241. switch (len) {
  242. case 1:
  243. out_8((u8 *) data, val);
  244. break;
  245. case 2:
  246. out_be16((u16 *) data, val);
  247. break;
  248. case 4:
  249. out_be32((u32 *) data, val);
  250. break;
  251. default:
  252. ret = -EINVAL;
  253. }
  254. spin_unlock_irqrestore(&fsl_rio_config_lock, flags);
  255. return ret;
  256. }
  257. static void fsl_rio_inbound_mem_init(struct rio_priv *priv)
  258. {
  259. int i;
  260. /* close inbound windows */
  261. for (i = 0; i < RIO_INB_ATMU_COUNT; i++)
  262. out_be32(&priv->inb_atmu_regs[i].riwar, 0);
  263. }
  264. int fsl_map_inb_mem(struct rio_mport *mport, dma_addr_t lstart,
  265. u64 rstart, u64 size, u32 flags)
  266. {
  267. struct rio_priv *priv = mport->priv;
  268. u32 base_size;
  269. unsigned int base_size_log;
  270. u64 win_start, win_end;
  271. u32 riwar;
  272. int i;
  273. if ((size & (size - 1)) != 0 || size > 0x400000000ULL)
  274. return -EINVAL;
  275. base_size_log = ilog2(size);
  276. base_size = 1 << base_size_log;
  277. /* check if addresses are aligned with the window size */
  278. if (lstart & (base_size - 1))
  279. return -EINVAL;
  280. if (rstart & (base_size - 1))
  281. return -EINVAL;
  282. /* check for conflicting ranges */
  283. for (i = 0; i < RIO_INB_ATMU_COUNT; i++) {
  284. riwar = in_be32(&priv->inb_atmu_regs[i].riwar);
  285. if ((riwar & RIWAR_ENABLE) == 0)
  286. continue;
  287. win_start = ((u64)(in_be32(&priv->inb_atmu_regs[i].riwbar) & RIWBAR_BADD_MASK))
  288. << RIWBAR_BADD_VAL_SHIFT;
  289. win_end = win_start + ((1 << ((riwar & RIWAR_SIZE_MASK) + 1)) - 1);
  290. if (rstart < win_end && (rstart + size) > win_start)
  291. return -EINVAL;
  292. }
  293. /* find unused atmu */
  294. for (i = 0; i < RIO_INB_ATMU_COUNT; i++) {
  295. riwar = in_be32(&priv->inb_atmu_regs[i].riwar);
  296. if ((riwar & RIWAR_ENABLE) == 0)
  297. break;
  298. }
  299. if (i >= RIO_INB_ATMU_COUNT)
  300. return -ENOMEM;
  301. out_be32(&priv->inb_atmu_regs[i].riwtar, lstart >> RIWTAR_TRAD_VAL_SHIFT);
  302. out_be32(&priv->inb_atmu_regs[i].riwbar, rstart >> RIWBAR_BADD_VAL_SHIFT);
  303. out_be32(&priv->inb_atmu_regs[i].riwar, RIWAR_ENABLE | RIWAR_TGINT_LOCAL |
  304. RIWAR_RDTYP_SNOOP | RIWAR_WRTYP_SNOOP | (base_size_log - 1));
  305. return 0;
  306. }
  307. void fsl_unmap_inb_mem(struct rio_mport *mport, dma_addr_t lstart)
  308. {
  309. u32 win_start_shift, base_start_shift;
  310. struct rio_priv *priv = mport->priv;
  311. u32 riwar, riwtar;
  312. int i;
  313. /* skip default window */
  314. base_start_shift = lstart >> RIWTAR_TRAD_VAL_SHIFT;
  315. for (i = 0; i < RIO_INB_ATMU_COUNT; i++) {
  316. riwar = in_be32(&priv->inb_atmu_regs[i].riwar);
  317. if ((riwar & RIWAR_ENABLE) == 0)
  318. continue;
  319. riwtar = in_be32(&priv->inb_atmu_regs[i].riwtar);
  320. win_start_shift = riwtar & RIWTAR_TRAD_MASK;
  321. if (win_start_shift == base_start_shift) {
  322. out_be32(&priv->inb_atmu_regs[i].riwar, riwar & ~RIWAR_ENABLE);
  323. return;
  324. }
  325. }
  326. }
  327. void fsl_rio_port_error_handler(int offset)
  328. {
  329. /*XXX: Error recovery is not implemented, we just clear errors */
  330. out_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR), 0);
  331. if (offset == 0) {
  332. out_be32((u32 *)(rio_regs_win + RIO_PORT1_EDCSR), 0);
  333. out_be32((u32 *)(rio_regs_win + RIO_PORT1_IECSR), IECSR_CLEAR);
  334. out_be32((u32 *)(rio_regs_win + RIO_ESCSR), ESCSR_CLEAR);
  335. } else {
  336. out_be32((u32 *)(rio_regs_win + RIO_PORT2_EDCSR), 0);
  337. out_be32((u32 *)(rio_regs_win + RIO_PORT2_IECSR), IECSR_CLEAR);
  338. out_be32((u32 *)(rio_regs_win + RIO_PORT2_ESCSR), ESCSR_CLEAR);
  339. }
  340. }
  341. static inline void fsl_rio_info(struct device *dev, u32 ccsr)
  342. {
  343. const char *str;
  344. if (ccsr & 1) {
  345. /* Serial phy */
  346. switch (ccsr >> 30) {
  347. case 0:
  348. str = "1";
  349. break;
  350. case 1:
  351. str = "4";
  352. break;
  353. default:
  354. str = "Unknown";
  355. break;
  356. }
  357. dev_info(dev, "Hardware port width: %s\n", str);
  358. switch ((ccsr >> 27) & 7) {
  359. case 0:
  360. str = "Single-lane 0";
  361. break;
  362. case 1:
  363. str = "Single-lane 2";
  364. break;
  365. case 2:
  366. str = "Four-lane";
  367. break;
  368. default:
  369. str = "Unknown";
  370. break;
  371. }
  372. dev_info(dev, "Training connection status: %s\n", str);
  373. } else {
  374. /* Parallel phy */
  375. if (!(ccsr & 0x80000000))
  376. dev_info(dev, "Output port operating in 8-bit mode\n");
  377. if (!(ccsr & 0x08000000))
  378. dev_info(dev, "Input port operating in 8-bit mode\n");
  379. }
  380. }
  381. /**
  382. * fsl_rio_setup - Setup Freescale PowerPC RapidIO interface
  383. * @dev: platform_device pointer
  384. *
  385. * Initializes MPC85xx RapidIO hardware interface, configures
  386. * master port with system-specific info, and registers the
  387. * master port with the RapidIO subsystem.
  388. */
  389. int fsl_rio_setup(struct platform_device *dev)
  390. {
  391. struct rio_ops *ops;
  392. struct rio_mport *port;
  393. struct rio_priv *priv;
  394. int rc = 0;
  395. const u32 *dt_range, *cell, *port_index;
  396. u32 active_ports = 0;
  397. struct resource regs, rmu_regs;
  398. struct device_node *np, *rmu_node;
  399. int rlen;
  400. u32 ccsr;
  401. u64 range_start, range_size;
  402. int paw, aw, sw;
  403. u32 i;
  404. static int tmp;
  405. struct device_node *rmu_np[MAX_MSG_UNIT_NUM] = {NULL};
  406. if (!dev->dev.of_node) {
  407. dev_err(&dev->dev, "Device OF-Node is NULL");
  408. return -ENODEV;
  409. }
  410. rc = of_address_to_resource(dev->dev.of_node, 0, &regs);
  411. if (rc) {
  412. dev_err(&dev->dev, "Can't get %pOF property 'reg'\n",
  413. dev->dev.of_node);
  414. return -EFAULT;
  415. }
  416. dev_info(&dev->dev, "Of-device full name %pOF\n",
  417. dev->dev.of_node);
  418. dev_info(&dev->dev, "Regs: %pR\n", &regs);
  419. rio_regs_win = ioremap(regs.start, resource_size(&regs));
  420. if (!rio_regs_win) {
  421. dev_err(&dev->dev, "Unable to map rio register window\n");
  422. rc = -ENOMEM;
  423. goto err_rio_regs;
  424. }
  425. ops = kzalloc(sizeof(struct rio_ops), GFP_KERNEL);
  426. if (!ops) {
  427. rc = -ENOMEM;
  428. goto err_ops;
  429. }
  430. ops->lcread = fsl_local_config_read;
  431. ops->lcwrite = fsl_local_config_write;
  432. ops->cread = fsl_rio_config_read;
  433. ops->cwrite = fsl_rio_config_write;
  434. ops->dsend = fsl_rio_doorbell_send;
  435. ops->pwenable = fsl_rio_pw_enable;
  436. ops->open_outb_mbox = fsl_open_outb_mbox;
  437. ops->open_inb_mbox = fsl_open_inb_mbox;
  438. ops->close_outb_mbox = fsl_close_outb_mbox;
  439. ops->close_inb_mbox = fsl_close_inb_mbox;
  440. ops->add_outb_message = fsl_add_outb_message;
  441. ops->add_inb_buffer = fsl_add_inb_buffer;
  442. ops->get_inb_message = fsl_get_inb_message;
  443. ops->map_inb = fsl_map_inb_mem;
  444. ops->unmap_inb = fsl_unmap_inb_mem;
  445. rmu_node = of_parse_phandle(dev->dev.of_node, "fsl,srio-rmu-handle", 0);
  446. if (!rmu_node) {
  447. dev_err(&dev->dev, "No valid fsl,srio-rmu-handle property\n");
  448. rc = -ENOENT;
  449. goto err_rmu;
  450. }
  451. rc = of_address_to_resource(rmu_node, 0, &rmu_regs);
  452. if (rc) {
  453. dev_err(&dev->dev, "Can't get %pOF property 'reg'\n",
  454. rmu_node);
  455. goto err_rmu;
  456. }
  457. rmu_regs_win = ioremap(rmu_regs.start, resource_size(&rmu_regs));
  458. if (!rmu_regs_win) {
  459. dev_err(&dev->dev, "Unable to map rmu register window\n");
  460. rc = -ENOMEM;
  461. goto err_rmu;
  462. }
  463. for_each_compatible_node(np, NULL, "fsl,srio-msg-unit") {
  464. rmu_np[tmp] = np;
  465. tmp++;
  466. }
  467. /*set up doobell node*/
  468. np = of_find_compatible_node(NULL, NULL, "fsl,srio-dbell-unit");
  469. if (!np) {
  470. dev_err(&dev->dev, "No fsl,srio-dbell-unit node\n");
  471. rc = -ENODEV;
  472. goto err_dbell;
  473. }
  474. dbell = kzalloc(sizeof(struct fsl_rio_dbell), GFP_KERNEL);
  475. if (!(dbell)) {
  476. dev_err(&dev->dev, "Can't alloc memory for 'fsl_rio_dbell'\n");
  477. rc = -ENOMEM;
  478. goto err_dbell;
  479. }
  480. dbell->dev = &dev->dev;
  481. dbell->bellirq = irq_of_parse_and_map(np, 1);
  482. dev_info(&dev->dev, "bellirq: %d\n", dbell->bellirq);
  483. aw = of_n_addr_cells(np);
  484. dt_range = of_get_property(np, "reg", &rlen);
  485. if (!dt_range) {
  486. pr_err("%pOF: unable to find 'reg' property\n",
  487. np);
  488. rc = -ENOMEM;
  489. goto err_pw;
  490. }
  491. range_start = of_read_number(dt_range, aw);
  492. dbell->dbell_regs = (struct rio_dbell_regs *)(rmu_regs_win +
  493. (u32)range_start);
  494. /*set up port write node*/
  495. np = of_find_compatible_node(NULL, NULL, "fsl,srio-port-write-unit");
  496. if (!np) {
  497. dev_err(&dev->dev, "No fsl,srio-port-write-unit node\n");
  498. rc = -ENODEV;
  499. goto err_pw;
  500. }
  501. pw = kzalloc(sizeof(struct fsl_rio_pw), GFP_KERNEL);
  502. if (!(pw)) {
  503. dev_err(&dev->dev, "Can't alloc memory for 'fsl_rio_pw'\n");
  504. rc = -ENOMEM;
  505. goto err_pw;
  506. }
  507. pw->dev = &dev->dev;
  508. pw->pwirq = irq_of_parse_and_map(np, 0);
  509. dev_info(&dev->dev, "pwirq: %d\n", pw->pwirq);
  510. aw = of_n_addr_cells(np);
  511. dt_range = of_get_property(np, "reg", &rlen);
  512. if (!dt_range) {
  513. pr_err("%pOF: unable to find 'reg' property\n",
  514. np);
  515. rc = -ENOMEM;
  516. goto err;
  517. }
  518. range_start = of_read_number(dt_range, aw);
  519. pw->pw_regs = (struct rio_pw_regs *)(rmu_regs_win + (u32)range_start);
  520. /*set up ports node*/
  521. for_each_child_of_node(dev->dev.of_node, np) {
  522. port_index = of_get_property(np, "cell-index", NULL);
  523. if (!port_index) {
  524. dev_err(&dev->dev, "Can't get %pOF property 'cell-index'\n",
  525. np);
  526. continue;
  527. }
  528. dt_range = of_get_property(np, "ranges", &rlen);
  529. if (!dt_range) {
  530. dev_err(&dev->dev, "Can't get %pOF property 'ranges'\n",
  531. np);
  532. continue;
  533. }
  534. /* Get node address wide */
  535. cell = of_get_property(np, "#address-cells", NULL);
  536. if (cell)
  537. aw = *cell;
  538. else
  539. aw = of_n_addr_cells(np);
  540. /* Get node size wide */
  541. cell = of_get_property(np, "#size-cells", NULL);
  542. if (cell)
  543. sw = *cell;
  544. else
  545. sw = of_n_size_cells(np);
  546. /* Get parent address wide wide */
  547. paw = of_n_addr_cells(np);
  548. range_start = of_read_number(dt_range + aw, paw);
  549. range_size = of_read_number(dt_range + aw + paw, sw);
  550. dev_info(&dev->dev, "%pOF: LAW start 0x%016llx, size 0x%016llx.\n",
  551. np, range_start, range_size);
  552. port = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
  553. if (!port)
  554. continue;
  555. rc = rio_mport_initialize(port);
  556. if (rc) {
  557. kfree(port);
  558. continue;
  559. }
  560. i = *port_index - 1;
  561. port->index = (unsigned char)i;
  562. priv = kzalloc(sizeof(struct rio_priv), GFP_KERNEL);
  563. if (!priv) {
  564. dev_err(&dev->dev, "Can't alloc memory for 'priv'\n");
  565. kfree(port);
  566. continue;
  567. }
  568. INIT_LIST_HEAD(&port->dbells);
  569. port->iores.start = range_start;
  570. port->iores.end = port->iores.start + range_size - 1;
  571. port->iores.flags = IORESOURCE_MEM;
  572. port->iores.name = "rio_io_win";
  573. if (request_resource(&iomem_resource, &port->iores) < 0) {
  574. dev_err(&dev->dev, "RIO: Error requesting master port region"
  575. " 0x%016llx-0x%016llx\n",
  576. (u64)port->iores.start, (u64)port->iores.end);
  577. kfree(priv);
  578. kfree(port);
  579. continue;
  580. }
  581. sprintf(port->name, "RIO mport %d", i);
  582. priv->dev = &dev->dev;
  583. port->dev.parent = &dev->dev;
  584. port->ops = ops;
  585. port->priv = priv;
  586. port->phys_efptr = 0x100;
  587. port->phys_rmap = 1;
  588. priv->regs_win = rio_regs_win;
  589. ccsr = in_be32(priv->regs_win + RIO_CCSR + i*0x20);
  590. /* Checking the port training status */
  591. if (in_be32((priv->regs_win + RIO_ESCSR + i*0x20)) & 1) {
  592. dev_err(&dev->dev, "Port %d is not ready. "
  593. "Try to restart connection...\n", i);
  594. /* Disable ports */
  595. out_be32(priv->regs_win
  596. + RIO_CCSR + i*0x20, 0);
  597. /* Set 1x lane */
  598. setbits32(priv->regs_win
  599. + RIO_CCSR + i*0x20, 0x02000000);
  600. /* Enable ports */
  601. setbits32(priv->regs_win
  602. + RIO_CCSR + i*0x20, 0x00600000);
  603. msleep(100);
  604. if (in_be32((priv->regs_win
  605. + RIO_ESCSR + i*0x20)) & 1) {
  606. dev_err(&dev->dev,
  607. "Port %d restart failed.\n", i);
  608. release_resource(&port->iores);
  609. kfree(priv);
  610. kfree(port);
  611. continue;
  612. }
  613. dev_info(&dev->dev, "Port %d restart success!\n", i);
  614. }
  615. fsl_rio_info(&dev->dev, ccsr);
  616. port->sys_size = (in_be32((priv->regs_win + RIO_PEF_CAR))
  617. & RIO_PEF_CTLS) >> 4;
  618. dev_info(&dev->dev, "RapidIO Common Transport System size: %d\n",
  619. port->sys_size ? 65536 : 256);
  620. if (port->host_deviceid >= 0)
  621. out_be32(priv->regs_win + RIO_GCCSR, RIO_PORT_GEN_HOST |
  622. RIO_PORT_GEN_MASTER | RIO_PORT_GEN_DISCOVERED);
  623. else
  624. out_be32(priv->regs_win + RIO_GCCSR,
  625. RIO_PORT_GEN_MASTER);
  626. priv->atmu_regs = (struct rio_atmu_regs *)(priv->regs_win
  627. + ((i == 0) ? RIO_ATMU_REGS_PORT1_OFFSET :
  628. RIO_ATMU_REGS_PORT2_OFFSET));
  629. priv->maint_atmu_regs = priv->atmu_regs + 1;
  630. priv->inb_atmu_regs = (struct rio_inb_atmu_regs __iomem *)
  631. (priv->regs_win +
  632. ((i == 0) ? RIO_INB_ATMU_REGS_PORT1_OFFSET :
  633. RIO_INB_ATMU_REGS_PORT2_OFFSET));
  634. /* Set to receive packets with any dest ID */
  635. out_be32((priv->regs_win + RIO_ISR_AACR + i*0x80),
  636. RIO_ISR_AACR_AA);
  637. /* Configure maintenance transaction window */
  638. out_be32(&priv->maint_atmu_regs->rowbar,
  639. port->iores.start >> 12);
  640. out_be32(&priv->maint_atmu_regs->rowar,
  641. 0x80077000 | (ilog2(RIO_MAINT_WIN_SIZE) - 1));
  642. priv->maint_win = ioremap(port->iores.start,
  643. RIO_MAINT_WIN_SIZE);
  644. rio_law_start = range_start;
  645. fsl_rio_setup_rmu(port, rmu_np[i]);
  646. fsl_rio_inbound_mem_init(priv);
  647. dbell->mport[i] = port;
  648. pw->mport[i] = port;
  649. if (rio_register_mport(port)) {
  650. release_resource(&port->iores);
  651. kfree(priv);
  652. kfree(port);
  653. continue;
  654. }
  655. active_ports++;
  656. }
  657. if (!active_ports) {
  658. rc = -ENOLINK;
  659. goto err;
  660. }
  661. fsl_rio_doorbell_init(dbell);
  662. fsl_rio_port_write_init(pw);
  663. return 0;
  664. err:
  665. kfree(pw);
  666. pw = NULL;
  667. err_pw:
  668. kfree(dbell);
  669. dbell = NULL;
  670. err_dbell:
  671. iounmap(rmu_regs_win);
  672. rmu_regs_win = NULL;
  673. err_rmu:
  674. kfree(ops);
  675. err_ops:
  676. iounmap(rio_regs_win);
  677. rio_regs_win = NULL;
  678. err_rio_regs:
  679. return rc;
  680. }
  681. /* The probe function for RapidIO peer-to-peer network.
  682. */
  683. static int fsl_of_rio_rpn_probe(struct platform_device *dev)
  684. {
  685. printk(KERN_INFO "Setting up RapidIO peer-to-peer network %pOF\n",
  686. dev->dev.of_node);
  687. return fsl_rio_setup(dev);
  688. };
  689. static const struct of_device_id fsl_of_rio_rpn_ids[] = {
  690. {
  691. .compatible = "fsl,srio",
  692. },
  693. {},
  694. };
  695. static struct platform_driver fsl_of_rio_rpn_driver = {
  696. .driver = {
  697. .name = "fsl-of-rio",
  698. .of_match_table = fsl_of_rio_rpn_ids,
  699. },
  700. .probe = fsl_of_rio_rpn_probe,
  701. };
  702. static __init int fsl_of_rio_rpn_init(void)
  703. {
  704. return platform_driver_register(&fsl_of_rio_rpn_driver);
  705. }
  706. subsys_initcall(fsl_of_rio_rpn_init);