fsl_rio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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_platform.h>
  31. #include <linux/delay.h>
  32. #include <linux/slab.h>
  33. #include <linux/io.h>
  34. #include <linux/uaccess.h>
  35. #include <asm/machdep.h>
  36. #include "fsl_rio.h"
  37. #undef DEBUG_PW /* Port-Write debugging */
  38. #define RIO_PORT1_EDCSR 0x0640
  39. #define RIO_PORT2_EDCSR 0x0680
  40. #define RIO_PORT1_IECSR 0x10130
  41. #define RIO_PORT2_IECSR 0x101B0
  42. #define RIO_ATMU_REGS_OFFSET 0x10c00
  43. #define RIO_GCCSR 0x13c
  44. #define RIO_ESCSR 0x158
  45. #define ESCSR_CLEAR 0x07120204
  46. #define RIO_PORT2_ESCSR 0x178
  47. #define RIO_CCSR 0x15c
  48. #define RIO_LTLEDCSR_IER 0x80000000
  49. #define RIO_LTLEDCSR_PRT 0x01000000
  50. #define IECSR_CLEAR 0x80000000
  51. #define RIO_ISR_AACR 0x10120
  52. #define RIO_ISR_AACR_AA 0x1 /* Accept All ID */
  53. #define __fsl_read_rio_config(x, addr, err, op) \
  54. __asm__ __volatile__( \
  55. "1: "op" %1,0(%2)\n" \
  56. " eieio\n" \
  57. "2:\n" \
  58. ".section .fixup,\"ax\"\n" \
  59. "3: li %1,-1\n" \
  60. " li %0,%3\n" \
  61. " b 2b\n" \
  62. ".section __ex_table,\"a\"\n" \
  63. " .align 2\n" \
  64. " .long 1b,3b\n" \
  65. ".text" \
  66. : "=r" (err), "=r" (x) \
  67. : "b" (addr), "i" (-EFAULT), "0" (err))
  68. void __iomem *rio_regs_win;
  69. #ifdef CONFIG_E500
  70. int fsl_rio_mcheck_exception(struct pt_regs *regs)
  71. {
  72. const struct exception_table_entry *entry;
  73. unsigned long reason;
  74. if (!rio_regs_win)
  75. return 0;
  76. reason = in_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR));
  77. if (reason & (RIO_LTLEDCSR_IER | RIO_LTLEDCSR_PRT)) {
  78. /* Check if we are prepared to handle this fault */
  79. entry = search_exception_tables(regs->nip);
  80. if (entry) {
  81. pr_debug("RIO: %s - MC Exception handled\n",
  82. __func__);
  83. out_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR),
  84. 0);
  85. regs->msr |= MSR_RI;
  86. regs->nip = entry->fixup;
  87. return 1;
  88. }
  89. }
  90. return 0;
  91. }
  92. EXPORT_SYMBOL_GPL(fsl_rio_mcheck_exception);
  93. #endif
  94. /**
  95. * fsl_local_config_read - Generate a MPC85xx local config space read
  96. * @mport: RapidIO master port info
  97. * @index: ID of RapdiIO interface
  98. * @offset: Offset into configuration space
  99. * @len: Length (in bytes) of the maintenance transaction
  100. * @data: Value to be read into
  101. *
  102. * Generates a MPC85xx local configuration space read. Returns %0 on
  103. * success or %-EINVAL on failure.
  104. */
  105. static int fsl_local_config_read(struct rio_mport *mport,
  106. int index, u32 offset, int len, u32 *data)
  107. {
  108. struct rio_priv *priv = mport->priv;
  109. pr_debug("fsl_local_config_read: index %d offset %8.8x\n", index,
  110. offset);
  111. *data = in_be32(priv->regs_win + offset);
  112. return 0;
  113. }
  114. /**
  115. * fsl_local_config_write - Generate a MPC85xx local config space write
  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 written
  121. *
  122. * Generates a MPC85xx local configuration space write. Returns %0 on
  123. * success or %-EINVAL on failure.
  124. */
  125. static int fsl_local_config_write(struct rio_mport *mport,
  126. int index, u32 offset, int len, u32 data)
  127. {
  128. struct rio_priv *priv = mport->priv;
  129. pr_debug
  130. ("fsl_local_config_write: index %d offset %8.8x data %8.8x\n",
  131. index, offset, data);
  132. out_be32(priv->regs_win + offset, data);
  133. return 0;
  134. }
  135. /**
  136. * fsl_rio_config_read - Generate a MPC85xx read maintenance transaction
  137. * @mport: RapidIO master port info
  138. * @index: ID of RapdiIO interface
  139. * @destid: Destination ID of transaction
  140. * @hopcount: Number of hops to target device
  141. * @offset: Offset into configuration space
  142. * @len: Length (in bytes) of the maintenance transaction
  143. * @val: Location to be read into
  144. *
  145. * Generates a MPC85xx read maintenance transaction. Returns %0 on
  146. * success or %-EINVAL on failure.
  147. */
  148. static int
  149. fsl_rio_config_read(struct rio_mport *mport, int index, u16 destid,
  150. u8 hopcount, u32 offset, int len, u32 *val)
  151. {
  152. struct rio_priv *priv = mport->priv;
  153. u8 *data;
  154. u32 rval, err = 0;
  155. pr_debug
  156. ("fsl_rio_config_read:"
  157. " index %d destid %d hopcount %d offset %8.8x len %d\n",
  158. index, destid, hopcount, offset, len);
  159. /* 16MB maintenance window possible */
  160. /* allow only aligned access to maintenance registers */
  161. if (offset > (0x1000000 - len) || !IS_ALIGNED(offset, len))
  162. return -EINVAL;
  163. out_be32(&priv->maint_atmu_regs->rowtar,
  164. (destid << 22) | (hopcount << 12) | (offset >> 12));
  165. out_be32(&priv->maint_atmu_regs->rowtear, (destid >> 10));
  166. data = (u8 *) priv->maint_win + (offset & (RIO_MAINT_WIN_SIZE - 1));
  167. switch (len) {
  168. case 1:
  169. __fsl_read_rio_config(rval, data, err, "lbz");
  170. break;
  171. case 2:
  172. __fsl_read_rio_config(rval, data, err, "lhz");
  173. break;
  174. case 4:
  175. __fsl_read_rio_config(rval, data, err, "lwz");
  176. break;
  177. default:
  178. return -EINVAL;
  179. }
  180. if (err) {
  181. pr_debug("RIO: cfg_read error %d for %x:%x:%x\n",
  182. err, destid, hopcount, offset);
  183. }
  184. *val = rval;
  185. return err;
  186. }
  187. /**
  188. * fsl_rio_config_write - Generate a MPC85xx write maintenance transaction
  189. * @mport: RapidIO master port info
  190. * @index: ID of RapdiIO interface
  191. * @destid: Destination ID of transaction
  192. * @hopcount: Number of hops to target device
  193. * @offset: Offset into configuration space
  194. * @len: Length (in bytes) of the maintenance transaction
  195. * @val: Value to be written
  196. *
  197. * Generates an MPC85xx write maintenance transaction. Returns %0 on
  198. * success or %-EINVAL on failure.
  199. */
  200. static int
  201. fsl_rio_config_write(struct rio_mport *mport, int index, u16 destid,
  202. u8 hopcount, u32 offset, int len, u32 val)
  203. {
  204. struct rio_priv *priv = mport->priv;
  205. u8 *data;
  206. pr_debug
  207. ("fsl_rio_config_write:"
  208. "index %d destid %d hopcount %d offset %8.8x len %d val %8.8x\n",
  209. index, destid, hopcount, offset, len, val);
  210. /* 16MB maintenance windows possible */
  211. /* allow only aligned access to maintenance registers */
  212. if (offset > (0x1000000 - len) || !IS_ALIGNED(offset, len))
  213. return -EINVAL;
  214. out_be32(&priv->maint_atmu_regs->rowtar,
  215. (destid << 22) | (hopcount << 12) | (offset >> 12));
  216. out_be32(&priv->maint_atmu_regs->rowtear, (destid >> 10));
  217. data = (u8 *) priv->maint_win + (offset & (RIO_MAINT_WIN_SIZE - 1));
  218. switch (len) {
  219. case 1:
  220. out_8((u8 *) data, val);
  221. break;
  222. case 2:
  223. out_be16((u16 *) data, val);
  224. break;
  225. case 4:
  226. out_be32((u32 *) data, val);
  227. break;
  228. default:
  229. return -EINVAL;
  230. }
  231. return 0;
  232. }
  233. void fsl_rio_port_error_handler(struct rio_mport *port, int offset)
  234. {
  235. /*XXX: Error recovery is not implemented, we just clear errors */
  236. out_be32((u32 *)(rio_regs_win + RIO_LTLEDCSR), 0);
  237. if (offset == 0) {
  238. out_be32((u32 *)(rio_regs_win + RIO_PORT1_EDCSR), 0);
  239. out_be32((u32 *)(rio_regs_win + RIO_PORT1_IECSR), IECSR_CLEAR);
  240. out_be32((u32 *)(rio_regs_win + RIO_ESCSR), ESCSR_CLEAR);
  241. } else {
  242. out_be32((u32 *)(rio_regs_win + RIO_PORT2_EDCSR), 0);
  243. out_be32((u32 *)(rio_regs_win + RIO_PORT2_IECSR), IECSR_CLEAR);
  244. out_be32((u32 *)(rio_regs_win + RIO_PORT2_ESCSR), ESCSR_CLEAR);
  245. }
  246. }
  247. static inline void fsl_rio_info(struct device *dev, u32 ccsr)
  248. {
  249. const char *str;
  250. if (ccsr & 1) {
  251. /* Serial phy */
  252. switch (ccsr >> 30) {
  253. case 0:
  254. str = "1";
  255. break;
  256. case 1:
  257. str = "4";
  258. break;
  259. default:
  260. str = "Unknown";
  261. break;
  262. }
  263. dev_info(dev, "Hardware port width: %s\n", str);
  264. switch ((ccsr >> 27) & 7) {
  265. case 0:
  266. str = "Single-lane 0";
  267. break;
  268. case 1:
  269. str = "Single-lane 2";
  270. break;
  271. case 2:
  272. str = "Four-lane";
  273. break;
  274. default:
  275. str = "Unknown";
  276. break;
  277. }
  278. dev_info(dev, "Training connection status: %s\n", str);
  279. } else {
  280. /* Parallel phy */
  281. if (!(ccsr & 0x80000000))
  282. dev_info(dev, "Output port operating in 8-bit mode\n");
  283. if (!(ccsr & 0x08000000))
  284. dev_info(dev, "Input port operating in 8-bit mode\n");
  285. }
  286. }
  287. /**
  288. * fsl_rio_setup - Setup Freescale PowerPC RapidIO interface
  289. * @dev: platform_device pointer
  290. *
  291. * Initializes MPC85xx RapidIO hardware interface, configures
  292. * master port with system-specific info, and registers the
  293. * master port with the RapidIO subsystem.
  294. */
  295. int fsl_rio_setup(struct platform_device *dev)
  296. {
  297. struct rio_ops *ops;
  298. struct rio_mport *port;
  299. struct rio_priv *priv;
  300. int rc = 0;
  301. const u32 *dt_range, *cell;
  302. struct resource regs;
  303. int rlen;
  304. u32 ccsr;
  305. u64 law_start, law_size;
  306. int paw, aw, sw;
  307. if (!dev->dev.of_node) {
  308. dev_err(&dev->dev, "Device OF-Node is NULL");
  309. return -EFAULT;
  310. }
  311. rc = of_address_to_resource(dev->dev.of_node, 0, &regs);
  312. if (rc) {
  313. dev_err(&dev->dev, "Can't get %s property 'reg'\n",
  314. dev->dev.of_node->full_name);
  315. return -EFAULT;
  316. }
  317. dev_info(&dev->dev, "Of-device full name %s\n",
  318. dev->dev.of_node->full_name);
  319. dev_info(&dev->dev, "Regs: %pR\n", &regs);
  320. dt_range = of_get_property(dev->dev.of_node, "ranges", &rlen);
  321. if (!dt_range) {
  322. dev_err(&dev->dev, "Can't get %s property 'ranges'\n",
  323. dev->dev.of_node->full_name);
  324. return -EFAULT;
  325. }
  326. /* Get node address wide */
  327. cell = of_get_property(dev->dev.of_node, "#address-cells", NULL);
  328. if (cell)
  329. aw = *cell;
  330. else
  331. aw = of_n_addr_cells(dev->dev.of_node);
  332. /* Get node size wide */
  333. cell = of_get_property(dev->dev.of_node, "#size-cells", NULL);
  334. if (cell)
  335. sw = *cell;
  336. else
  337. sw = of_n_size_cells(dev->dev.of_node);
  338. /* Get parent address wide wide */
  339. paw = of_n_addr_cells(dev->dev.of_node);
  340. law_start = of_read_number(dt_range + aw, paw);
  341. law_size = of_read_number(dt_range + aw + paw, sw);
  342. dev_info(&dev->dev, "LAW start 0x%016llx, size 0x%016llx.\n",
  343. law_start, law_size);
  344. ops = kzalloc(sizeof(struct rio_ops), GFP_KERNEL);
  345. if (!ops) {
  346. rc = -ENOMEM;
  347. goto err_ops;
  348. }
  349. ops->lcread = fsl_local_config_read;
  350. ops->lcwrite = fsl_local_config_write;
  351. ops->cread = fsl_rio_config_read;
  352. ops->cwrite = fsl_rio_config_write;
  353. ops->pwenable = fsl_rio_pw_enable;
  354. port = kzalloc(sizeof(struct rio_mport), GFP_KERNEL);
  355. if (!port) {
  356. rc = -ENOMEM;
  357. goto err_port;
  358. }
  359. port->index = 0;
  360. priv = kzalloc(sizeof(struct rio_priv), GFP_KERNEL);
  361. if (!priv) {
  362. printk(KERN_ERR "Can't alloc memory for 'priv'\n");
  363. rc = -ENOMEM;
  364. goto err_priv;
  365. }
  366. INIT_LIST_HEAD(&port->dbells);
  367. port->iores.start = law_start;
  368. port->iores.end = law_start + law_size - 1;
  369. port->iores.flags = IORESOURCE_MEM;
  370. port->iores.name = "rio_io_win";
  371. if (request_resource(&iomem_resource, &port->iores) < 0) {
  372. dev_err(&dev->dev, "RIO: Error requesting master port region"
  373. " 0x%016llx-0x%016llx\n",
  374. (u64)port->iores.start, (u64)port->iores.end);
  375. rc = -ENOMEM;
  376. goto err_res;
  377. }
  378. priv->pwirq = irq_of_parse_and_map(dev->dev.of_node, 0);
  379. dev_info(&dev->dev, "pwirq: %d\n", priv->pwirq);
  380. strcpy(port->name, "RIO0 mport");
  381. priv->dev = &dev->dev;
  382. port->ops = ops;
  383. port->priv = priv;
  384. port->phys_efptr = 0x100;
  385. priv->regs_win = ioremap(regs.start, resource_size(&regs));
  386. rio_regs_win = priv->regs_win;
  387. /* Probe the master port phy type */
  388. ccsr = in_be32(priv->regs_win + RIO_CCSR);
  389. port->phy_type = (ccsr & 1) ? RIO_PHY_SERIAL : RIO_PHY_PARALLEL;
  390. dev_info(&dev->dev, "RapidIO PHY type: %s\n",
  391. (port->phy_type == RIO_PHY_PARALLEL) ? "parallel" :
  392. ((port->phy_type == RIO_PHY_SERIAL) ? "serial" :
  393. "unknown"));
  394. /* Checking the port training status */
  395. if (in_be32((priv->regs_win + RIO_ESCSR)) & 1) {
  396. dev_err(&dev->dev, "Port is not ready. "
  397. "Try to restart connection...\n");
  398. switch (port->phy_type) {
  399. case RIO_PHY_SERIAL:
  400. /* Disable ports */
  401. out_be32(priv->regs_win + RIO_CCSR, 0);
  402. /* Set 1x lane */
  403. setbits32(priv->regs_win + RIO_CCSR, 0x02000000);
  404. /* Enable ports */
  405. setbits32(priv->regs_win + RIO_CCSR, 0x00600000);
  406. break;
  407. case RIO_PHY_PARALLEL:
  408. /* Disable ports */
  409. out_be32(priv->regs_win + RIO_CCSR, 0x22000000);
  410. /* Enable ports */
  411. out_be32(priv->regs_win + RIO_CCSR, 0x44000000);
  412. break;
  413. }
  414. msleep(100);
  415. if (in_be32((priv->regs_win + RIO_ESCSR)) & 1) {
  416. dev_err(&dev->dev, "Port restart failed.\n");
  417. rc = -ENOLINK;
  418. goto err;
  419. }
  420. dev_info(&dev->dev, "Port restart success!\n");
  421. }
  422. fsl_rio_info(&dev->dev, ccsr);
  423. port->sys_size = (in_be32((priv->regs_win + RIO_PEF_CAR))
  424. & RIO_PEF_CTLS) >> 4;
  425. dev_info(&dev->dev, "RapidIO Common Transport System size: %d\n",
  426. port->sys_size ? 65536 : 256);
  427. if (rio_register_mport(port))
  428. goto err;
  429. if (port->host_deviceid >= 0)
  430. out_be32(priv->regs_win + RIO_GCCSR, RIO_PORT_GEN_HOST |
  431. RIO_PORT_GEN_MASTER | RIO_PORT_GEN_DISCOVERED);
  432. else
  433. out_be32(priv->regs_win + RIO_GCCSR, 0x00000000);
  434. priv->atmu_regs = (struct rio_atmu_regs *)(priv->regs_win
  435. + RIO_ATMU_REGS_OFFSET);
  436. priv->maint_atmu_regs = priv->atmu_regs + 1;
  437. /* Set to receive any dist ID for serial RapidIO controller. */
  438. if (port->phy_type == RIO_PHY_SERIAL)
  439. out_be32((priv->regs_win + RIO_ISR_AACR), RIO_ISR_AACR_AA);
  440. /* Configure maintenance transaction window */
  441. out_be32(&priv->maint_atmu_regs->rowbar, law_start >> 12);
  442. out_be32(&priv->maint_atmu_regs->rowar,
  443. 0x80077000 | (ilog2(RIO_MAINT_WIN_SIZE) - 1));
  444. priv->maint_win = ioremap(law_start, RIO_MAINT_WIN_SIZE);
  445. fsl_rio_setup_rmu(port, dev->dev.of_node);
  446. fsl_rio_port_write_init(port);
  447. return 0;
  448. err:
  449. iounmap(priv->regs_win);
  450. release_resource(&port->iores);
  451. err_res:
  452. kfree(priv);
  453. err_priv:
  454. kfree(port);
  455. err_port:
  456. kfree(ops);
  457. err_ops:
  458. return rc;
  459. }
  460. /* The probe function for RapidIO peer-to-peer network.
  461. */
  462. static int __devinit fsl_of_rio_rpn_probe(struct platform_device *dev)
  463. {
  464. printk(KERN_INFO "Setting up RapidIO peer-to-peer network %s\n",
  465. dev->dev.of_node->full_name);
  466. return fsl_rio_setup(dev);
  467. };
  468. static const struct of_device_id fsl_of_rio_rpn_ids[] = {
  469. {
  470. .compatible = "fsl,rapidio-delta",
  471. },
  472. {},
  473. };
  474. static struct platform_driver fsl_of_rio_rpn_driver = {
  475. .driver = {
  476. .name = "fsl-of-rio",
  477. .owner = THIS_MODULE,
  478. .of_match_table = fsl_of_rio_rpn_ids,
  479. },
  480. .probe = fsl_of_rio_rpn_probe,
  481. };
  482. static __init int fsl_of_rio_rpn_init(void)
  483. {
  484. return platform_driver_register(&fsl_of_rio_rpn_driver);
  485. }
  486. subsys_initcall(fsl_of_rio_rpn_init);