fsl_rio.c 21 KB

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