fsl_rio.c 20 KB

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