Merge branch 'master' of /usr/src/ntfs-2.6/
[linux-2.6] / arch / ia64 / sn / kernel / tiocx.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (c) 2005 Silicon Graphics, Inc.  All rights reserved.
7  */
8
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/proc_fs.h>
14 #include <linux/device.h>
15 #include <linux/delay.h>
16 #include <asm/system.h>
17 #include <asm/uaccess.h>
18 #include <asm/sn/sn_sal.h>
19 #include <asm/sn/addrs.h>
20 #include <asm/sn/io.h>
21 #include <asm/sn/types.h>
22 #include <asm/sn/shubio.h>
23 #include <asm/sn/tiocx.h>
24 #include <asm/sn/l1.h>
25 #include <asm/sn/module.h>
26 #include "tio.h"
27 #include "xtalk/xwidgetdev.h"
28 #include "xtalk/hubdev.h"
29
30 #define CX_DEV_NONE 0
31 #define DEVICE_NAME "tiocx"
32 #define WIDGET_ID 0
33 #define TIOCX_DEBUG 0
34
35 #if TIOCX_DEBUG
36 #define DBG(fmt...)    printk(KERN_ALERT fmt)
37 #else
38 #define DBG(fmt...)
39 #endif
40
41 struct device_attribute dev_attr_cxdev_control;
42
43 /**
44  * tiocx_match - Try to match driver id list with device.
45  * @dev: device pointer
46  * @drv: driver pointer
47  *
48  * Returns 1 if match, 0 otherwise.
49  */
50 static int tiocx_match(struct device *dev, struct device_driver *drv)
51 {
52         struct cx_dev *cx_dev = to_cx_dev(dev);
53         struct cx_drv *cx_drv = to_cx_driver(drv);
54         const struct cx_device_id *ids = cx_drv->id_table;
55
56         if (!ids)
57                 return 0;
58
59         while (ids->part_num) {
60                 if (ids->part_num == cx_dev->cx_id.part_num)
61                         return 1;
62                 ids++;
63         }
64         return 0;
65
66 }
67
68 static int tiocx_hotplug(struct device *dev, char **envp, int num_envp,
69                          char *buffer, int buffer_size)
70 {
71         return -ENODEV;
72 }
73
74 static void tiocx_bus_release(struct device *dev)
75 {
76         kfree(to_cx_dev(dev));
77 }
78
79 struct bus_type tiocx_bus_type = {
80         .name = "tiocx",
81         .match = tiocx_match,
82         .hotplug = tiocx_hotplug,
83 };
84
85 /**
86  * cx_device_match - Find cx_device in the id table.
87  * @ids: id table from driver
88  * @cx_device: part/mfg id for the device
89  *
90  */
91 static const struct cx_device_id *cx_device_match(const struct cx_device_id
92                                                   *ids,
93                                                   struct cx_dev *cx_device)
94 {
95         /*
96          * NOTES: We may want to check for CX_ANY_ID too.
97          *        Do we want to match against nasid too?
98          *        CX_DEV_NONE == 0, if the driver tries to register for
99          *        part/mfg == 0 we should return no-match (NULL) here.
100          */
101         while (ids->part_num && ids->mfg_num) {
102                 if (ids->part_num == cx_device->cx_id.part_num &&
103                     ids->mfg_num == cx_device->cx_id.mfg_num)
104                         return ids;
105                 ids++;
106         }
107
108         return NULL;
109 }
110
111 /**
112  * cx_device_probe - Look for matching device.
113  *                      Call driver probe routine if found.
114  * @cx_driver: driver table (cx_drv struct) from driver
115  * @cx_device: part/mfg id for the device
116  */
117 static int cx_device_probe(struct device *dev)
118 {
119         const struct cx_device_id *id;
120         struct cx_drv *cx_drv = to_cx_driver(dev->driver);
121         struct cx_dev *cx_dev = to_cx_dev(dev);
122         int error = 0;
123
124         if (!cx_dev->driver && cx_drv->probe) {
125                 id = cx_device_match(cx_drv->id_table, cx_dev);
126                 if (id) {
127                         if ((error = cx_drv->probe(cx_dev, id)) < 0)
128                                 return error;
129                         else
130                                 cx_dev->driver = cx_drv;
131                 }
132         }
133
134         return error;
135 }
136
137 /**
138  * cx_driver_remove - Remove driver from device struct.
139  * @dev: device
140  */
141 static int cx_driver_remove(struct device *dev)
142 {
143         struct cx_dev *cx_dev = to_cx_dev(dev);
144         struct cx_drv *cx_drv = cx_dev->driver;
145         if (cx_drv->remove)
146                 cx_drv->remove(cx_dev);
147         cx_dev->driver = NULL;
148         return 0;
149 }
150
151 /**
152  * cx_driver_register - Register the driver.
153  * @cx_driver: driver table (cx_drv struct) from driver
154  * 
155  * Called from the driver init routine to register a driver.
156  * The cx_drv struct contains the driver name, a pointer to
157  * a table of part/mfg numbers and a pointer to the driver's
158  * probe/attach routine.
159  */
160 int cx_driver_register(struct cx_drv *cx_driver)
161 {
162         cx_driver->driver.name = cx_driver->name;
163         cx_driver->driver.bus = &tiocx_bus_type;
164         cx_driver->driver.probe = cx_device_probe;
165         cx_driver->driver.remove = cx_driver_remove;
166
167         return driver_register(&cx_driver->driver);
168 }
169
170 /**
171  * cx_driver_unregister - Unregister the driver.
172  * @cx_driver: driver table (cx_drv struct) from driver
173  */
174 int cx_driver_unregister(struct cx_drv *cx_driver)
175 {
176         driver_unregister(&cx_driver->driver);
177         return 0;
178 }
179
180 /**
181  * cx_device_register - Register a device.
182  * @nasid: device's nasid
183  * @part_num: device's part number
184  * @mfg_num: device's manufacturer number
185  * @hubdev: hub info associated with this device
186  * @bt: board type of the device
187  *
188  */
189 int
190 cx_device_register(nasid_t nasid, int part_num, int mfg_num,
191                    struct hubdev_info *hubdev, int bt)
192 {
193         struct cx_dev *cx_dev;
194
195         cx_dev = kzalloc(sizeof(struct cx_dev), GFP_KERNEL);
196         DBG("cx_dev= 0x%p\n", cx_dev);
197         if (cx_dev == NULL)
198                 return -ENOMEM;
199
200         cx_dev->cx_id.part_num = part_num;
201         cx_dev->cx_id.mfg_num = mfg_num;
202         cx_dev->cx_id.nasid = nasid;
203         cx_dev->hubdev = hubdev;
204         cx_dev->bt = bt;
205
206         cx_dev->dev.parent = NULL;
207         cx_dev->dev.bus = &tiocx_bus_type;
208         cx_dev->dev.release = tiocx_bus_release;
209         snprintf(cx_dev->dev.bus_id, BUS_ID_SIZE, "%d",
210                  cx_dev->cx_id.nasid);
211         device_register(&cx_dev->dev);
212         get_device(&cx_dev->dev);
213
214         device_create_file(&cx_dev->dev, &dev_attr_cxdev_control);
215
216         return 0;
217 }
218
219 /**
220  * cx_device_unregister - Unregister a device.
221  * @cx_dev: part/mfg id for the device
222  */
223 int cx_device_unregister(struct cx_dev *cx_dev)
224 {
225         put_device(&cx_dev->dev);
226         device_unregister(&cx_dev->dev);
227         return 0;
228 }
229
230 /**
231  * cx_device_reload - Reload the device.
232  * @nasid: device's nasid
233  * @part_num: device's part number
234  * @mfg_num: device's manufacturer number
235  *
236  * Remove the device associated with 'nasid' from device list and then
237  * call device-register with the given part/mfg numbers.
238  */
239 static int cx_device_reload(struct cx_dev *cx_dev)
240 {
241         cx_device_unregister(cx_dev);
242         return cx_device_register(cx_dev->cx_id.nasid, cx_dev->cx_id.part_num,
243                                   cx_dev->cx_id.mfg_num, cx_dev->hubdev,
244                                   cx_dev->bt);
245 }
246
247 static inline uint64_t tiocx_intr_alloc(nasid_t nasid, int widget,
248                                         u64 sn_irq_info,
249                                         int req_irq, nasid_t req_nasid,
250                                         int req_slice)
251 {
252         struct ia64_sal_retval rv;
253         rv.status = 0;
254         rv.v0 = 0;
255
256         ia64_sal_oemcall_nolock(&rv, SN_SAL_IOIF_INTERRUPT,
257                                 SAL_INTR_ALLOC, nasid,
258                                 widget, sn_irq_info, req_irq,
259                                 req_nasid, req_slice);
260         return rv.status;
261 }
262
263 static inline void tiocx_intr_free(nasid_t nasid, int widget,
264                                    struct sn_irq_info *sn_irq_info)
265 {
266         struct ia64_sal_retval rv;
267         rv.status = 0;
268         rv.v0 = 0;
269
270         ia64_sal_oemcall_nolock(&rv, SN_SAL_IOIF_INTERRUPT,
271                                 SAL_INTR_FREE, nasid,
272                                 widget, sn_irq_info->irq_irq,
273                                 sn_irq_info->irq_cookie, 0, 0);
274 }
275
276 struct sn_irq_info *tiocx_irq_alloc(nasid_t nasid, int widget, int irq,
277                                     nasid_t req_nasid, int slice)
278 {
279         struct sn_irq_info *sn_irq_info;
280         int status;
281         int sn_irq_size = sizeof(struct sn_irq_info);
282
283         if ((nasid & 1) == 0)
284                 return NULL;
285
286         sn_irq_info = kmalloc(sn_irq_size, GFP_KERNEL);
287         if (sn_irq_info == NULL)
288                 return NULL;
289
290         memset(sn_irq_info, 0x0, sn_irq_size);
291
292         status = tiocx_intr_alloc(nasid, widget, __pa(sn_irq_info), irq,
293                                   req_nasid, slice);
294         if (status) {
295                 kfree(sn_irq_info);
296                 return NULL;
297         } else {
298                 return sn_irq_info;
299         }
300 }
301
302 void tiocx_irq_free(struct sn_irq_info *sn_irq_info)
303 {
304         uint64_t bridge = (uint64_t) sn_irq_info->irq_bridge;
305         nasid_t nasid = NASID_GET(bridge);
306         int widget;
307
308         if (nasid & 1) {
309                 widget = TIO_SWIN_WIDGETNUM(bridge);
310                 tiocx_intr_free(nasid, widget, sn_irq_info);
311                 kfree(sn_irq_info);
312         }
313 }
314
315 uint64_t tiocx_dma_addr(uint64_t addr)
316 {
317         return PHYS_TO_TIODMA(addr);
318 }
319
320 uint64_t tiocx_swin_base(int nasid)
321 {
322         return TIO_SWIN_BASE(nasid, TIOCX_CORELET);
323 }
324
325 EXPORT_SYMBOL(cx_driver_register);
326 EXPORT_SYMBOL(cx_driver_unregister);
327 EXPORT_SYMBOL(cx_device_register);
328 EXPORT_SYMBOL(cx_device_unregister);
329 EXPORT_SYMBOL(tiocx_irq_alloc);
330 EXPORT_SYMBOL(tiocx_irq_free);
331 EXPORT_SYMBOL(tiocx_bus_type);
332 EXPORT_SYMBOL(tiocx_dma_addr);
333 EXPORT_SYMBOL(tiocx_swin_base);
334
335 static void tio_conveyor_set(nasid_t nasid, int enable_flag)
336 {
337         uint64_t ice_frz;
338         uint64_t disable_cb = (1ull << 61);
339
340         if (!(nasid & 1))
341                 return;
342
343         ice_frz = REMOTE_HUB_L(nasid, TIO_ICE_FRZ_CFG);
344         if (enable_flag) {
345                 if (!(ice_frz & disable_cb))    /* already enabled */
346                         return;
347                 ice_frz &= ~disable_cb;
348         } else {
349                 if (ice_frz & disable_cb)       /* already disabled */
350                         return;
351                 ice_frz |= disable_cb;
352         }
353         DBG(KERN_ALERT "TIO_ICE_FRZ_CFG= 0x%lx\n", ice_frz);
354         REMOTE_HUB_S(nasid, TIO_ICE_FRZ_CFG, ice_frz);
355 }
356
357 #define tio_conveyor_enable(nasid) tio_conveyor_set(nasid, 1)
358 #define tio_conveyor_disable(nasid) tio_conveyor_set(nasid, 0)
359
360 static void tio_corelet_reset(nasid_t nasid, int corelet)
361 {
362         if (!(nasid & 1))
363                 return;
364
365         REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 1 << corelet);
366         udelay(2000);
367         REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 0);
368         udelay(2000);
369 }
370
371 static int is_fpga_tio(int nasid, int *bt)
372 {
373         int ioboard_type;
374
375         ioboard_type = ia64_sn_sysctl_ioboard_get(nasid);
376
377         switch (ioboard_type) {
378         case L1_BRICKTYPE_SA:
379         case L1_BRICKTYPE_ATHENA:
380         case L1_BOARDTYPE_DAYTONA:
381                 *bt = ioboard_type;
382                 return 1;
383         }
384
385         return 0;
386 }
387
388 static int bitstream_loaded(nasid_t nasid)
389 {
390         uint64_t cx_credits;
391
392         cx_credits = REMOTE_HUB_L(nasid, TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3);
393         cx_credits &= TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3_CREDIT_CNT_MASK;
394         DBG("cx_credits= 0x%lx\n", cx_credits);
395
396         return (cx_credits == 0xf) ? 1 : 0;
397 }
398
399 static int tiocx_reload(struct cx_dev *cx_dev)
400 {
401         int part_num = CX_DEV_NONE;
402         int mfg_num = CX_DEV_NONE;
403         nasid_t nasid = cx_dev->cx_id.nasid;
404
405         if (bitstream_loaded(nasid)) {
406                 uint64_t cx_id;
407                 int rv;
408
409                 rv = ia64_sn_sysctl_tio_clock_reset(nasid);
410                 if (rv) {
411                         printk(KERN_ALERT "CX port JTAG reset failed.\n");
412                 } else {
413                         cx_id = *(volatile uint64_t *)
414                                 (TIO_SWIN_BASE(nasid, TIOCX_CORELET) +
415                                           WIDGET_ID);
416                         part_num = XWIDGET_PART_NUM(cx_id);
417                         mfg_num = XWIDGET_MFG_NUM(cx_id);
418                         DBG("part= 0x%x, mfg= 0x%x\n", part_num, mfg_num);
419                         /* just ignore it if it's a CE */
420                         if (part_num == TIO_CE_ASIC_PARTNUM)
421                                 return 0;
422                 }
423         }
424
425         cx_dev->cx_id.part_num = part_num;
426         cx_dev->cx_id.mfg_num = mfg_num;
427
428         /*
429          * Delete old device and register the new one.  It's ok if
430          * part_num/mfg_num == CX_DEV_NONE.  We want to register
431          * devices in the table even if a bitstream isn't loaded.
432          * That allows use to see that a bitstream isn't loaded via
433          * TIOCX_IOCTL_DEV_LIST.
434          */
435         return cx_device_reload(cx_dev);
436 }
437
438 static ssize_t show_cxdev_control(struct device *dev, struct device_attribute *attr, char *buf)
439 {
440         struct cx_dev *cx_dev = to_cx_dev(dev);
441
442         return sprintf(buf, "0x%x 0x%x 0x%x 0x%x\n",
443                        cx_dev->cx_id.nasid,
444                        cx_dev->cx_id.part_num, cx_dev->cx_id.mfg_num,
445                        cx_dev->bt);
446 }
447
448 static ssize_t store_cxdev_control(struct device *dev, struct device_attribute *attr, const char *buf,
449                                    size_t count)
450 {
451         int n;
452         struct cx_dev *cx_dev = to_cx_dev(dev);
453
454         if (!capable(CAP_SYS_ADMIN))
455                 return -EPERM;
456
457         if (count <= 0)
458                 return 0;
459
460         n = simple_strtoul(buf, NULL, 0);
461
462         switch (n) {
463         case 1:
464                 tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
465                 tiocx_reload(cx_dev);
466                 break;
467         case 2:
468                 tiocx_reload(cx_dev);
469                 break;
470         case 3:
471                 tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
472                 break;
473         default:
474                 break;
475         }
476
477         return count;
478 }
479
480 DEVICE_ATTR(cxdev_control, 0644, show_cxdev_control, store_cxdev_control);
481
482 static int __init tiocx_init(void)
483 {
484         cnodeid_t cnodeid;
485         int found_tiocx_device = 0;
486
487         if (!ia64_platform_is("sn2"))
488                 return -ENODEV;
489
490         bus_register(&tiocx_bus_type);
491
492         for (cnodeid = 0; cnodeid < num_cnodes; cnodeid++) {
493                 nasid_t nasid;
494                 int bt;
495
496                 nasid = cnodeid_to_nasid(cnodeid);
497
498                 if ((nasid & 0x1) && is_fpga_tio(nasid, &bt)) {
499                         struct hubdev_info *hubdev;
500                         struct xwidget_info *widgetp;
501
502                         DBG("Found TIO at nasid 0x%x\n", nasid);
503
504                         hubdev =
505                             (struct hubdev_info *)(NODEPDA(cnodeid)->pdinfo);
506
507                         widgetp = &hubdev->hdi_xwidget_info[TIOCX_CORELET];
508
509                         /* The CE hangs off of the CX port but is not an FPGA */
510                         if (widgetp->xwi_hwid.part_num == TIO_CE_ASIC_PARTNUM)
511                                 continue;
512
513                         tio_corelet_reset(nasid, TIOCX_CORELET);
514                         tio_conveyor_enable(nasid);
515
516                         if (cx_device_register
517                             (nasid, widgetp->xwi_hwid.part_num,
518                              widgetp->xwi_hwid.mfg_num, hubdev, bt) < 0)
519                                 return -ENXIO;
520                         else
521                                 found_tiocx_device++;
522                 }
523         }
524
525         /* It's ok if we find zero devices. */
526         DBG("found_tiocx_device= %d\n", found_tiocx_device);
527
528         return 0;
529 }
530
531 static int cx_remove_device(struct device * dev, void * data)
532 {
533         struct cx_dev *cx_dev = to_cx_dev(dev);
534         device_remove_file(dev, &dev_attr_cxdev_control);
535         cx_device_unregister(cx_dev);
536         return 0;
537 }
538
539 static void __exit tiocx_exit(void)
540 {
541         DBG("tiocx_exit\n");
542
543         /*
544          * Unregister devices.
545          */
546         bus_for_each_dev(&tiocx_bus_type, NULL, NULL, cx_remove_device);
547         bus_unregister(&tiocx_bus_type);
548 }
549
550 subsys_initcall(tiocx_init);
551 module_exit(tiocx_exit);
552
553 /************************************************************************
554  * Module licensing and description
555  ************************************************************************/
556 MODULE_LICENSE("GPL");
557 MODULE_AUTHOR("Bruce Losure <blosure@sgi.com>");
558 MODULE_DESCRIPTION("TIOCX module");
559 MODULE_SUPPORTED_DEVICE(DEVICE_NAME);