fsl_rio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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/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/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, u32 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)
  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. goto err_rmu;
  442. }
  443. rc = of_address_to_resource(rmu_node, 0, &rmu_regs);
  444. if (rc) {
  445. dev_err(&dev->dev, "Can't get %s property 'reg'\n",
  446. rmu_node->full_name);
  447. goto err_rmu;
  448. }
  449. rmu_regs_win = ioremap(rmu_regs.start, resource_size(&rmu_regs));
  450. if (!rmu_regs_win) {
  451. dev_err(&dev->dev, "Unable to map rmu register window\n");
  452. rc = -ENOMEM;
  453. goto err_rmu;
  454. }
  455. for_each_compatible_node(np, NULL, "fsl,srio-msg-unit") {
  456. rmu_np[tmp] = np;
  457. tmp++;
  458. }
  459. /*set up doobell node*/
  460. np = of_find_compatible_node(NULL, NULL, "fsl,srio-dbell-unit");
  461. if (!np) {
  462. dev_err(&dev->dev, "No fsl,srio-dbell-unit node\n");
  463. rc = -ENODEV;
  464. goto err_dbell;
  465. }
  466. dbell = kzalloc(sizeof(struct fsl_rio_dbell), GFP_KERNEL);
  467. if (!(dbell)) {
  468. dev_err(&dev->dev, "Can't alloc memory for 'fsl_rio_dbell'\n");
  469. rc = -ENOMEM;
  470. goto err_dbell;
  471. }
  472. dbell->dev = &dev->dev;
  473. dbell->bellirq = irq_of_parse_and_map(np, 1);
  474. dev_info(&dev->dev, "bellirq: %d\n", dbell->bellirq);
  475. aw = of_n_addr_cells(np);
  476. dt_range = of_get_property(np, "reg", &rlen);
  477. if (!dt_range) {
  478. pr_err("%s: unable to find 'reg' property\n",
  479. np->full_name);
  480. rc = -ENOMEM;
  481. goto err_pw;
  482. }
  483. range_start = of_read_number(dt_range, aw);
  484. dbell->dbell_regs = (struct rio_dbell_regs *)(rmu_regs_win +
  485. (u32)range_start);
  486. /*set up port write node*/
  487. np = of_find_compatible_node(NULL, NULL, "fsl,srio-port-write-unit");
  488. if (!np) {
  489. dev_err(&dev->dev, "No fsl,srio-port-write-unit node\n");
  490. rc = -ENODEV;
  491. goto err_pw;
  492. }
  493. pw = kzalloc(sizeof(struct fsl_rio_pw), GFP_KERNEL);
  494. if (!(pw)) {
  495. dev_err(&dev->dev, "Can't alloc memory for 'fsl_rio_pw'\n");
  496. rc = -ENOMEM;
  497. goto err_pw;
  498. }
  499. pw->dev = &dev->dev;
  500. pw->pwirq = irq_of_parse_and_map(np, 0);
  501. dev_info(&dev->dev, "pwirq: %d\n", pw->pwirq);
  502. aw = of_n_addr_cells(np);
  503. dt_range = of_get_property(np, "reg", &rlen);
  504. if (!dt_range) {
  505. pr_err("%s: unable to find 'reg' property\n",
  506. np->full_name);
  507. rc = -ENOMEM;
  508. goto err;
  509. }
  510. range_start = of_read_number(dt_range, aw);
  511. pw->pw_regs = (struct rio_pw_regs *)(rmu_regs_win + (u32)range_start);
  512. /*set up ports node*/
  513. for_each_child_of_node(dev->dev.of_node, np) {
  514. port_index = of_get_property(np, "cell-index", NULL);
  515. if (!port_index) {
  516. dev_err(&dev->dev, "Can't get %s property 'cell-index'\n",
  517. np->full_name);
  518. continue;
  519. }
  520. dt_range = of_get_property(np, "ranges", &rlen);
  521. if (!dt_range) {
  522. dev_err(&dev->dev, "Can't get %s property 'ranges'\n",
  523. np->full_name);
  524. continue;
  525. }
  526. /* Get node address wide */
  527. cell = of_get_property(np, "#address-cells", NULL);
  528. if (cell)
  529. aw = *cell;
  530. else
  531. aw = of_n_addr_cells(np);
  532. /* Get node size wide */
  533. cell = of_get_property(np, "#size-cells", NULL);
  534. if (cell)
  535. sw = *cell;
  536. else
  537. sw = of_n_size_cells(np);
  538. /* Get parent address wide wide */
  539. paw = of_n_addr_cells(np);
  540. range_start = of_read_number(dt_range + aw, paw);
  541. range_size = of_read_number(dt_range + aw + paw, sw);
  542. dev_info(&dev->dev, "%s: LAW start 0x%016llx, size 0x%016llx.\n",
  543. np->full_name, range_start, range_size);
  544. port = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
  545. if (!port)
  546. continue;
  547. i = *port_index - 1;
  548. port->index = (unsigned char)i;
  549. priv = kzalloc(sizeof(struct rio_priv), GFP_KERNEL);
  550. if (!priv) {
  551. dev_err(&dev->dev, "Can't alloc memory for 'priv'\n");
  552. kfree(port);
  553. continue;
  554. }
  555. INIT_LIST_HEAD(&port->dbells);
  556. port->iores.start = range_start;
  557. port->iores.end = port->iores.start + range_size - 1;
  558. port->iores.flags = IORESOURCE_MEM;
  559. port->iores.name = "rio_io_win";
  560. if (request_resource(&iomem_resource, &port->iores) < 0) {
  561. dev_err(&dev->dev, "RIO: Error requesting master port region"
  562. " 0x%016llx-0x%016llx\n",
  563. (u64)port->iores.start, (u64)port->iores.end);
  564. kfree(priv);
  565. kfree(port);
  566. continue;
  567. }
  568. sprintf(port->name, "RIO mport %d", i);
  569. priv->dev = &dev->dev;
  570. port->dev.parent = &dev->dev;
  571. port->ops = ops;
  572. port->priv = priv;
  573. port->phys_efptr = 0x100;
  574. priv->regs_win = rio_regs_win;
  575. /* Probe the master port phy type */
  576. ccsr = in_be32(priv->regs_win + RIO_CCSR + i*0x20);
  577. port->phy_type = (ccsr & 1) ? RIO_PHY_SERIAL : RIO_PHY_PARALLEL;
  578. if (port->phy_type == RIO_PHY_PARALLEL) {
  579. dev_err(&dev->dev, "RIO: Parallel PHY type, unsupported port type!\n");
  580. release_resource(&port->iores);
  581. kfree(priv);
  582. kfree(port);
  583. continue;
  584. }
  585. dev_info(&dev->dev, "RapidIO PHY type: Serial\n");
  586. /* Checking the port training status */
  587. if (in_be32((priv->regs_win + RIO_ESCSR + i*0x20)) & 1) {
  588. dev_err(&dev->dev, "Port %d is not ready. "
  589. "Try to restart connection...\n", i);
  590. /* Disable ports */
  591. out_be32(priv->regs_win
  592. + RIO_CCSR + i*0x20, 0);
  593. /* Set 1x lane */
  594. setbits32(priv->regs_win
  595. + RIO_CCSR + i*0x20, 0x02000000);
  596. /* Enable ports */
  597. setbits32(priv->regs_win
  598. + RIO_CCSR + i*0x20, 0x00600000);
  599. msleep(100);
  600. if (in_be32((priv->regs_win
  601. + RIO_ESCSR + i*0x20)) & 1) {
  602. dev_err(&dev->dev,
  603. "Port %d restart failed.\n", i);
  604. release_resource(&port->iores);
  605. kfree(priv);
  606. kfree(port);
  607. continue;
  608. }
  609. dev_info(&dev->dev, "Port %d restart success!\n", i);
  610. }
  611. fsl_rio_info(&dev->dev, ccsr);
  612. port->sys_size = (in_be32((priv->regs_win + RIO_PEF_CAR))
  613. & RIO_PEF_CTLS) >> 4;
  614. dev_info(&dev->dev, "RapidIO Common Transport System size: %d\n",
  615. port->sys_size ? 65536 : 256);
  616. if (rio_register_mport(port)) {
  617. release_resource(&port->iores);
  618. kfree(priv);
  619. kfree(port);
  620. continue;
  621. }
  622. if (port->host_deviceid >= 0)
  623. out_be32(priv->regs_win + RIO_GCCSR, RIO_PORT_GEN_HOST |
  624. RIO_PORT_GEN_MASTER | RIO_PORT_GEN_DISCOVERED);
  625. else
  626. out_be32(priv->regs_win + RIO_GCCSR,
  627. RIO_PORT_GEN_MASTER);
  628. priv->atmu_regs = (struct rio_atmu_regs *)(priv->regs_win
  629. + ((i == 0) ? RIO_ATMU_REGS_PORT1_OFFSET :
  630. RIO_ATMU_REGS_PORT2_OFFSET));
  631. priv->maint_atmu_regs = priv->atmu_regs + 1;
  632. priv->inb_atmu_regs = (struct rio_inb_atmu_regs __iomem *)
  633. (priv->regs_win +
  634. ((i == 0) ? RIO_INB_ATMU_REGS_PORT1_OFFSET :
  635. RIO_INB_ATMU_REGS_PORT2_OFFSET));
  636. /* Set to receive any dist ID for serial RapidIO controller. */
  637. if (port->phy_type == RIO_PHY_SERIAL)
  638. out_be32((priv->regs_win
  639. + RIO_ISR_AACR + i*0x80), RIO_ISR_AACR_AA);
  640. /* Configure maintenance transaction window */
  641. out_be32(&priv->maint_atmu_regs->rowbar,
  642. port->iores.start >> 12);
  643. out_be32(&priv->maint_atmu_regs->rowar,
  644. 0x80077000 | (ilog2(RIO_MAINT_WIN_SIZE) - 1));
  645. priv->maint_win = ioremap(port->iores.start,
  646. RIO_MAINT_WIN_SIZE);
  647. rio_law_start = range_start;
  648. fsl_rio_setup_rmu(port, rmu_np[i]);
  649. fsl_rio_inbound_mem_init(priv);
  650. dbell->mport[i] = port;
  651. active_ports++;
  652. }
  653. if (!active_ports) {
  654. rc = -ENOLINK;
  655. goto err;
  656. }
  657. fsl_rio_doorbell_init(dbell);
  658. fsl_rio_port_write_init(pw);
  659. return 0;
  660. err:
  661. kfree(pw);
  662. pw = NULL;
  663. err_pw:
  664. kfree(dbell);
  665. dbell = NULL;
  666. err_dbell:
  667. iounmap(rmu_regs_win);
  668. rmu_regs_win = NULL;
  669. err_rmu:
  670. kfree(ops);
  671. err_ops:
  672. iounmap(rio_regs_win);
  673. rio_regs_win = NULL;
  674. err_rio_regs:
  675. return rc;
  676. }
  677. /* The probe function for RapidIO peer-to-peer network.
  678. */
  679. static int fsl_of_rio_rpn_probe(struct platform_device *dev)
  680. {
  681. printk(KERN_INFO "Setting up RapidIO peer-to-peer network %s\n",
  682. dev->dev.of_node->full_name);
  683. return fsl_rio_setup(dev);
  684. };
  685. static const struct of_device_id fsl_of_rio_rpn_ids[] = {
  686. {
  687. .compatible = "fsl,srio",
  688. },
  689. {},
  690. };
  691. static struct platform_driver fsl_of_rio_rpn_driver = {
  692. .driver = {
  693. .name = "fsl-of-rio",
  694. .of_match_table = fsl_of_rio_rpn_ids,
  695. },
  696. .probe = fsl_of_rio_rpn_probe,
  697. };
  698. static __init int fsl_of_rio_rpn_init(void)
  699. {
  700. return platform_driver_register(&fsl_of_rio_rpn_driver);
  701. }
  702. subsys_initcall(fsl_of_rio_rpn_init);