fsl_rio.c 20 KB

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