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