fsl_rio.c 21 KB

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