Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / arch / ia64 / sn / kernel / tiocx.c
blobe35f6485c1fd1964cfb4f379dd89497e52dc4de8
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.
6 * Copyright (c) 2005 Silicon Graphics, Inc. All rights reserved.
7 */
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/capability.h>
15 #include <linux/device.h>
16 #include <linux/delay.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"
30 #define CX_DEV_NONE 0
31 #define DEVICE_NAME "tiocx"
32 #define WIDGET_ID 0
33 #define TIOCX_DEBUG 0
35 #if TIOCX_DEBUG
36 #define DBG(fmt...) printk(KERN_ALERT fmt)
37 #else
38 #define DBG(fmt...)
39 #endif
41 struct device_attribute dev_attr_cxdev_control;
43 /**
44 * tiocx_match - Try to match driver id list with device.
45 * @dev: device pointer
46 * @drv: driver pointer
48 * Returns 1 if match, 0 otherwise.
50 static int tiocx_match(struct device *dev, struct device_driver *drv)
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;
56 if (!ids)
57 return 0;
59 while (ids->part_num) {
60 if (ids->part_num == cx_dev->cx_id.part_num)
61 return 1;
62 ids++;
64 return 0;
68 static int tiocx_uevent(struct device *dev, struct kobj_uevent_env *env)
70 return -ENODEV;
73 static void tiocx_bus_release(struct device *dev)
75 kfree(to_cx_dev(dev));
78 /**
79 * cx_device_match - Find cx_device in the id table.
80 * @ids: id table from driver
81 * @cx_device: part/mfg id for the device
84 static const struct cx_device_id *cx_device_match(const struct cx_device_id
85 *ids,
86 struct cx_dev *cx_device)
89 * NOTES: We may want to check for CX_ANY_ID too.
90 * Do we want to match against nasid too?
91 * CX_DEV_NONE == 0, if the driver tries to register for
92 * part/mfg == 0 we should return no-match (NULL) here.
94 while (ids->part_num && ids->mfg_num) {
95 if (ids->part_num == cx_device->cx_id.part_num &&
96 ids->mfg_num == cx_device->cx_id.mfg_num)
97 return ids;
98 ids++;
101 return NULL;
105 * cx_device_probe - Look for matching device.
106 * Call driver probe routine if found.
107 * @cx_driver: driver table (cx_drv struct) from driver
108 * @cx_device: part/mfg id for the device
110 static int cx_device_probe(struct device *dev)
112 const struct cx_device_id *id;
113 struct cx_drv *cx_drv = to_cx_driver(dev->driver);
114 struct cx_dev *cx_dev = to_cx_dev(dev);
115 int error = 0;
117 if (!cx_dev->driver && cx_drv->probe) {
118 id = cx_device_match(cx_drv->id_table, cx_dev);
119 if (id) {
120 if ((error = cx_drv->probe(cx_dev, id)) < 0)
121 return error;
122 else
123 cx_dev->driver = cx_drv;
127 return error;
131 * cx_driver_remove - Remove driver from device struct.
132 * @dev: device
134 static int cx_driver_remove(struct device *dev)
136 struct cx_dev *cx_dev = to_cx_dev(dev);
137 struct cx_drv *cx_drv = cx_dev->driver;
138 if (cx_drv->remove)
139 cx_drv->remove(cx_dev);
140 cx_dev->driver = NULL;
141 return 0;
144 struct bus_type tiocx_bus_type = {
145 .name = "tiocx",
146 .match = tiocx_match,
147 .uevent = tiocx_uevent,
148 .probe = cx_device_probe,
149 .remove = cx_driver_remove,
153 * cx_driver_register - Register the driver.
154 * @cx_driver: driver table (cx_drv struct) from driver
156 * Called from the driver init routine to register a driver.
157 * The cx_drv struct contains the driver name, a pointer to
158 * a table of part/mfg numbers and a pointer to the driver's
159 * probe/attach routine.
161 int cx_driver_register(struct cx_drv *cx_driver)
163 cx_driver->driver.name = cx_driver->name;
164 cx_driver->driver.bus = &tiocx_bus_type;
166 return driver_register(&cx_driver->driver);
170 * cx_driver_unregister - Unregister the driver.
171 * @cx_driver: driver table (cx_drv struct) from driver
173 int cx_driver_unregister(struct cx_drv *cx_driver)
175 driver_unregister(&cx_driver->driver);
176 return 0;
180 * cx_device_register - Register a device.
181 * @nasid: device's nasid
182 * @part_num: device's part number
183 * @mfg_num: device's manufacturer number
184 * @hubdev: hub info associated with this device
185 * @bt: board type of the device
189 cx_device_register(nasid_t nasid, int part_num, int mfg_num,
190 struct hubdev_info *hubdev, int bt)
192 struct cx_dev *cx_dev;
193 int r;
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;
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;
206 cx_dev->dev.parent = NULL;
207 cx_dev->dev.bus = &tiocx_bus_type;
208 cx_dev->dev.release = tiocx_bus_release;
209 dev_set_name(&cx_dev->dev, "%d", cx_dev->cx_id.nasid);
210 r = device_register(&cx_dev->dev);
211 if (r) {
212 kfree(cx_dev);
213 return r;
215 get_device(&cx_dev->dev);
217 device_create_file(&cx_dev->dev, &dev_attr_cxdev_control);
219 return 0;
223 * cx_device_unregister - Unregister a device.
224 * @cx_dev: part/mfg id for the device
226 int cx_device_unregister(struct cx_dev *cx_dev)
228 put_device(&cx_dev->dev);
229 device_unregister(&cx_dev->dev);
230 return 0;
234 * cx_device_reload - Reload the device.
235 * @nasid: device's nasid
236 * @part_num: device's part number
237 * @mfg_num: device's manufacturer number
239 * Remove the device associated with 'nasid' from device list and then
240 * call device-register with the given part/mfg numbers.
242 static int cx_device_reload(struct cx_dev *cx_dev)
244 cx_device_unregister(cx_dev);
245 return cx_device_register(cx_dev->cx_id.nasid, cx_dev->cx_id.part_num,
246 cx_dev->cx_id.mfg_num, cx_dev->hubdev,
247 cx_dev->bt);
250 static inline u64 tiocx_intr_alloc(nasid_t nasid, int widget,
251 u64 sn_irq_info,
252 int req_irq, nasid_t req_nasid,
253 int req_slice)
255 struct ia64_sal_retval rv;
256 rv.status = 0;
257 rv.v0 = 0;
259 ia64_sal_oemcall_nolock(&rv, SN_SAL_IOIF_INTERRUPT,
260 SAL_INTR_ALLOC, nasid,
261 widget, sn_irq_info, req_irq,
262 req_nasid, req_slice);
263 return rv.status;
266 static inline void tiocx_intr_free(nasid_t nasid, int widget,
267 struct sn_irq_info *sn_irq_info)
269 struct ia64_sal_retval rv;
270 rv.status = 0;
271 rv.v0 = 0;
273 ia64_sal_oemcall_nolock(&rv, SN_SAL_IOIF_INTERRUPT,
274 SAL_INTR_FREE, nasid,
275 widget, sn_irq_info->irq_irq,
276 sn_irq_info->irq_cookie, 0, 0);
279 struct sn_irq_info *tiocx_irq_alloc(nasid_t nasid, int widget, int irq,
280 nasid_t req_nasid, int slice)
282 struct sn_irq_info *sn_irq_info;
283 int status;
284 int sn_irq_size = sizeof(struct sn_irq_info);
286 if ((nasid & 1) == 0)
287 return NULL;
289 sn_irq_info = kzalloc(sn_irq_size, GFP_KERNEL);
290 if (sn_irq_info == NULL)
291 return NULL;
293 status = tiocx_intr_alloc(nasid, widget, __pa(sn_irq_info), irq,
294 req_nasid, slice);
295 if (status) {
296 kfree(sn_irq_info);
297 return NULL;
298 } else {
299 return sn_irq_info;
303 void tiocx_irq_free(struct sn_irq_info *sn_irq_info)
305 u64 bridge = (u64) sn_irq_info->irq_bridge;
306 nasid_t nasid = NASID_GET(bridge);
307 int widget;
309 if (nasid & 1) {
310 widget = TIO_SWIN_WIDGETNUM(bridge);
311 tiocx_intr_free(nasid, widget, sn_irq_info);
312 kfree(sn_irq_info);
316 u64 tiocx_dma_addr(u64 addr)
318 return PHYS_TO_TIODMA(addr);
321 u64 tiocx_swin_base(int nasid)
323 return TIO_SWIN_BASE(nasid, TIOCX_CORELET);
326 EXPORT_SYMBOL(cx_driver_register);
327 EXPORT_SYMBOL(cx_driver_unregister);
328 EXPORT_SYMBOL(cx_device_register);
329 EXPORT_SYMBOL(cx_device_unregister);
330 EXPORT_SYMBOL(tiocx_irq_alloc);
331 EXPORT_SYMBOL(tiocx_irq_free);
332 EXPORT_SYMBOL(tiocx_bus_type);
333 EXPORT_SYMBOL(tiocx_dma_addr);
334 EXPORT_SYMBOL(tiocx_swin_base);
336 static void tio_conveyor_set(nasid_t nasid, int enable_flag)
338 u64 ice_frz;
339 u64 disable_cb = (1ull << 61);
341 if (!(nasid & 1))
342 return;
344 ice_frz = REMOTE_HUB_L(nasid, TIO_ICE_FRZ_CFG);
345 if (enable_flag) {
346 if (!(ice_frz & disable_cb)) /* already enabled */
347 return;
348 ice_frz &= ~disable_cb;
349 } else {
350 if (ice_frz & disable_cb) /* already disabled */
351 return;
352 ice_frz |= disable_cb;
354 DBG(KERN_ALERT "TIO_ICE_FRZ_CFG= 0x%lx\n", ice_frz);
355 REMOTE_HUB_S(nasid, TIO_ICE_FRZ_CFG, ice_frz);
358 #define tio_conveyor_enable(nasid) tio_conveyor_set(nasid, 1)
359 #define tio_conveyor_disable(nasid) tio_conveyor_set(nasid, 0)
361 static void tio_corelet_reset(nasid_t nasid, int corelet)
363 if (!(nasid & 1))
364 return;
366 REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 1 << corelet);
367 udelay(2000);
368 REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 0);
369 udelay(2000);
372 static int is_fpga_tio(int nasid, int *bt)
374 u16 uninitialized_var(ioboard_type); /* GCC be quiet */
375 long rc;
377 rc = ia64_sn_sysctl_ioboard_get(nasid, &ioboard_type);
378 if (rc) {
379 printk(KERN_WARNING "ia64_sn_sysctl_ioboard_get failed: %ld\n",
380 rc);
381 return 0;
384 switch (ioboard_type) {
385 case L1_BRICKTYPE_SA:
386 case L1_BRICKTYPE_ATHENA:
387 case L1_BOARDTYPE_DAYTONA:
388 *bt = ioboard_type;
389 return 1;
392 return 0;
395 static int bitstream_loaded(nasid_t nasid)
397 u64 cx_credits;
399 cx_credits = REMOTE_HUB_L(nasid, TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3);
400 cx_credits &= TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3_CREDIT_CNT_MASK;
401 DBG("cx_credits= 0x%lx\n", cx_credits);
403 return (cx_credits == 0xf) ? 1 : 0;
406 static int tiocx_reload(struct cx_dev *cx_dev)
408 int part_num = CX_DEV_NONE;
409 int mfg_num = CX_DEV_NONE;
410 nasid_t nasid = cx_dev->cx_id.nasid;
412 if (bitstream_loaded(nasid)) {
413 u64 cx_id;
414 int rv;
416 rv = ia64_sn_sysctl_tio_clock_reset(nasid);
417 if (rv) {
418 printk(KERN_ALERT "CX port JTAG reset failed.\n");
419 } else {
420 cx_id = *(volatile u64 *)
421 (TIO_SWIN_BASE(nasid, TIOCX_CORELET) +
422 WIDGET_ID);
423 part_num = XWIDGET_PART_NUM(cx_id);
424 mfg_num = XWIDGET_MFG_NUM(cx_id);
425 DBG("part= 0x%x, mfg= 0x%x\n", part_num, mfg_num);
426 /* just ignore it if it's a CE */
427 if (part_num == TIO_CE_ASIC_PARTNUM)
428 return 0;
432 cx_dev->cx_id.part_num = part_num;
433 cx_dev->cx_id.mfg_num = mfg_num;
436 * Delete old device and register the new one. It's ok if
437 * part_num/mfg_num == CX_DEV_NONE. We want to register
438 * devices in the table even if a bitstream isn't loaded.
439 * That allows use to see that a bitstream isn't loaded via
440 * TIOCX_IOCTL_DEV_LIST.
442 return cx_device_reload(cx_dev);
445 static ssize_t show_cxdev_control(struct device *dev, struct device_attribute *attr, char *buf)
447 struct cx_dev *cx_dev = to_cx_dev(dev);
449 return sprintf(buf, "0x%x 0x%x 0x%x 0x%x\n",
450 cx_dev->cx_id.nasid,
451 cx_dev->cx_id.part_num, cx_dev->cx_id.mfg_num,
452 cx_dev->bt);
455 static ssize_t store_cxdev_control(struct device *dev, struct device_attribute *attr, const char *buf,
456 size_t count)
458 int n;
459 struct cx_dev *cx_dev = to_cx_dev(dev);
461 if (!capable(CAP_SYS_ADMIN))
462 return -EPERM;
464 if (count <= 0)
465 return 0;
467 n = simple_strtoul(buf, NULL, 0);
469 switch (n) {
470 case 1:
471 tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
472 tiocx_reload(cx_dev);
473 break;
474 case 2:
475 tiocx_reload(cx_dev);
476 break;
477 case 3:
478 tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
479 break;
480 default:
481 break;
484 return count;
487 DEVICE_ATTR(cxdev_control, 0644, show_cxdev_control, store_cxdev_control);
489 static int __init tiocx_init(void)
491 cnodeid_t cnodeid;
492 int found_tiocx_device = 0;
493 int err;
495 if (!ia64_platform_is("sn2"))
496 return 0;
498 err = bus_register(&tiocx_bus_type);
499 if (err)
500 return err;
502 for (cnodeid = 0; cnodeid < num_cnodes; cnodeid++) {
503 nasid_t nasid;
504 int bt;
506 nasid = cnodeid_to_nasid(cnodeid);
508 if ((nasid & 0x1) && is_fpga_tio(nasid, &bt)) {
509 struct hubdev_info *hubdev;
510 struct xwidget_info *widgetp;
512 DBG("Found TIO at nasid 0x%x\n", nasid);
514 hubdev =
515 (struct hubdev_info *)(NODEPDA(cnodeid)->pdinfo);
517 widgetp = &hubdev->hdi_xwidget_info[TIOCX_CORELET];
519 /* The CE hangs off of the CX port but is not an FPGA */
520 if (widgetp->xwi_hwid.part_num == TIO_CE_ASIC_PARTNUM)
521 continue;
523 tio_corelet_reset(nasid, TIOCX_CORELET);
524 tio_conveyor_enable(nasid);
526 if (cx_device_register
527 (nasid, widgetp->xwi_hwid.part_num,
528 widgetp->xwi_hwid.mfg_num, hubdev, bt) < 0)
529 return -ENXIO;
530 else
531 found_tiocx_device++;
535 /* It's ok if we find zero devices. */
536 DBG("found_tiocx_device= %d\n", found_tiocx_device);
538 return 0;
541 static int cx_remove_device(struct device * dev, void * data)
543 struct cx_dev *cx_dev = to_cx_dev(dev);
544 device_remove_file(dev, &dev_attr_cxdev_control);
545 cx_device_unregister(cx_dev);
546 return 0;
549 static void __exit tiocx_exit(void)
551 DBG("tiocx_exit\n");
554 * Unregister devices.
556 bus_for_each_dev(&tiocx_bus_type, NULL, NULL, cx_remove_device);
557 bus_unregister(&tiocx_bus_type);
560 fs_initcall(tiocx_init);
561 module_exit(tiocx_exit);
563 /************************************************************************
564 * Module licensing and description
565 ************************************************************************/
566 MODULE_LICENSE("GPL");
567 MODULE_AUTHOR("Bruce Losure <blosure@sgi.com>");
568 MODULE_DESCRIPTION("TIOCX module");
569 MODULE_SUPPORTED_DEVICE(DEVICE_NAME);