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
6 * Copyright (c) 2005 Silicon Graphics, Inc. All rights reserved.
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/system.h>
18 #include <asm/uaccess.h>
19 #include <asm/sn/sn_sal.h>
20 #include <asm/sn/addrs.h>
21 #include <asm/sn/io.h>
22 #include <asm/sn/types.h>
23 #include <asm/sn/shubio.h>
24 #include <asm/sn/tiocx.h>
25 #include <asm/sn/l1.h>
26 #include <asm/sn/module.h>
28 #include "xtalk/xwidgetdev.h"
29 #include "xtalk/hubdev.h"
32 #define DEVICE_NAME "tiocx"
37 #define DBG(fmt...) printk(KERN_ALERT fmt)
42 struct device_attribute dev_attr_cxdev_control;
45 * tiocx_match - Try to match driver id list with device.
46 * @dev: device pointer
47 * @drv: driver pointer
49 * Returns 1 if match, 0 otherwise.
51 static int tiocx_match(struct device *dev, struct device_driver *drv)
53 struct cx_dev *cx_dev = to_cx_dev(dev);
54 struct cx_drv *cx_drv = to_cx_driver(drv);
55 const struct cx_device_id *ids = cx_drv->id_table;
60 while (ids->part_num) {
61 if (ids->part_num == cx_dev->cx_id.part_num)
69 static int tiocx_uevent(struct device *dev, char **envp, int num_envp,
70 char *buffer, int buffer_size)
75 static void tiocx_bus_release(struct device *dev)
77 kfree(to_cx_dev(dev));
81 * cx_device_match - Find cx_device in the id table.
82 * @ids: id table from driver
83 * @cx_device: part/mfg id for the device
86 static const struct cx_device_id *cx_device_match(const struct cx_device_id
88 struct cx_dev *cx_device)
91 * NOTES: We may want to check for CX_ANY_ID too.
92 * Do we want to match against nasid too?
93 * CX_DEV_NONE == 0, if the driver tries to register for
94 * part/mfg == 0 we should return no-match (NULL) here.
96 while (ids->part_num && ids->mfg_num) {
97 if (ids->part_num == cx_device->cx_id.part_num &&
98 ids->mfg_num == cx_device->cx_id.mfg_num)
107 * cx_device_probe - Look for matching device.
108 * Call driver probe routine if found.
109 * @cx_driver: driver table (cx_drv struct) from driver
110 * @cx_device: part/mfg id for the device
112 static int cx_device_probe(struct device *dev)
114 const struct cx_device_id *id;
115 struct cx_drv *cx_drv = to_cx_driver(dev->driver);
116 struct cx_dev *cx_dev = to_cx_dev(dev);
119 if (!cx_dev->driver && cx_drv->probe) {
120 id = cx_device_match(cx_drv->id_table, cx_dev);
122 if ((error = cx_drv->probe(cx_dev, id)) < 0)
125 cx_dev->driver = cx_drv;
133 * cx_driver_remove - Remove driver from device struct.
136 static int cx_driver_remove(struct device *dev)
138 struct cx_dev *cx_dev = to_cx_dev(dev);
139 struct cx_drv *cx_drv = cx_dev->driver;
141 cx_drv->remove(cx_dev);
142 cx_dev->driver = NULL;
146 struct bus_type tiocx_bus_type = {
148 .match = tiocx_match,
149 .uevent = tiocx_uevent,
150 .probe = cx_device_probe,
151 .remove = cx_driver_remove,
155 * cx_driver_register - Register the driver.
156 * @cx_driver: driver table (cx_drv struct) from driver
158 * Called from the driver init routine to register a driver.
159 * The cx_drv struct contains the driver name, a pointer to
160 * a table of part/mfg numbers and a pointer to the driver's
161 * probe/attach routine.
163 int cx_driver_register(struct cx_drv *cx_driver)
165 cx_driver->driver.name = cx_driver->name;
166 cx_driver->driver.bus = &tiocx_bus_type;
168 return driver_register(&cx_driver->driver);
172 * cx_driver_unregister - Unregister the driver.
173 * @cx_driver: driver table (cx_drv struct) from driver
175 int cx_driver_unregister(struct cx_drv *cx_driver)
177 driver_unregister(&cx_driver->driver);
182 * cx_device_register - Register a device.
183 * @nasid: device's nasid
184 * @part_num: device's part number
185 * @mfg_num: device's manufacturer number
186 * @hubdev: hub info associated with this device
187 * @bt: board type of the device
191 cx_device_register(nasid_t nasid, int part_num, int mfg_num,
192 struct hubdev_info *hubdev, int bt)
194 struct cx_dev *cx_dev;
196 cx_dev = kzalloc(sizeof(struct cx_dev), GFP_KERNEL);
197 DBG("cx_dev= 0x%p\n", cx_dev);
201 cx_dev->cx_id.part_num = part_num;
202 cx_dev->cx_id.mfg_num = mfg_num;
203 cx_dev->cx_id.nasid = nasid;
204 cx_dev->hubdev = hubdev;
207 cx_dev->dev.parent = NULL;
208 cx_dev->dev.bus = &tiocx_bus_type;
209 cx_dev->dev.release = tiocx_bus_release;
210 snprintf(cx_dev->dev.bus_id, BUS_ID_SIZE, "%d",
211 cx_dev->cx_id.nasid);
212 device_register(&cx_dev->dev);
213 get_device(&cx_dev->dev);
215 device_create_file(&cx_dev->dev, &dev_attr_cxdev_control);
221 * cx_device_unregister - Unregister a device.
222 * @cx_dev: part/mfg id for the device
224 int cx_device_unregister(struct cx_dev *cx_dev)
226 put_device(&cx_dev->dev);
227 device_unregister(&cx_dev->dev);
232 * cx_device_reload - Reload the device.
233 * @nasid: device's nasid
234 * @part_num: device's part number
235 * @mfg_num: device's manufacturer number
237 * Remove the device associated with 'nasid' from device list and then
238 * call device-register with the given part/mfg numbers.
240 static int cx_device_reload(struct cx_dev *cx_dev)
242 cx_device_unregister(cx_dev);
243 return cx_device_register(cx_dev->cx_id.nasid, cx_dev->cx_id.part_num,
244 cx_dev->cx_id.mfg_num, cx_dev->hubdev,
248 static inline u64 tiocx_intr_alloc(nasid_t nasid, int widget,
250 int req_irq, nasid_t req_nasid,
253 struct ia64_sal_retval rv;
257 ia64_sal_oemcall_nolock(&rv, SN_SAL_IOIF_INTERRUPT,
258 SAL_INTR_ALLOC, nasid,
259 widget, sn_irq_info, req_irq,
260 req_nasid, req_slice);
264 static inline void tiocx_intr_free(nasid_t nasid, int widget,
265 struct sn_irq_info *sn_irq_info)
267 struct ia64_sal_retval rv;
271 ia64_sal_oemcall_nolock(&rv, SN_SAL_IOIF_INTERRUPT,
272 SAL_INTR_FREE, nasid,
273 widget, sn_irq_info->irq_irq,
274 sn_irq_info->irq_cookie, 0, 0);
277 struct sn_irq_info *tiocx_irq_alloc(nasid_t nasid, int widget, int irq,
278 nasid_t req_nasid, int slice)
280 struct sn_irq_info *sn_irq_info;
282 int sn_irq_size = sizeof(struct sn_irq_info);
284 if ((nasid & 1) == 0)
287 sn_irq_info = kzalloc(sn_irq_size, GFP_KERNEL);
288 if (sn_irq_info == NULL)
291 status = tiocx_intr_alloc(nasid, widget, __pa(sn_irq_info), irq,
301 void tiocx_irq_free(struct sn_irq_info *sn_irq_info)
303 u64 bridge = (u64) sn_irq_info->irq_bridge;
304 nasid_t nasid = NASID_GET(bridge);
308 widget = TIO_SWIN_WIDGETNUM(bridge);
309 tiocx_intr_free(nasid, widget, sn_irq_info);
314 u64 tiocx_dma_addr(u64 addr)
316 return PHYS_TO_TIODMA(addr);
319 u64 tiocx_swin_base(int nasid)
321 return TIO_SWIN_BASE(nasid, TIOCX_CORELET);
324 EXPORT_SYMBOL(cx_driver_register);
325 EXPORT_SYMBOL(cx_driver_unregister);
326 EXPORT_SYMBOL(cx_device_register);
327 EXPORT_SYMBOL(cx_device_unregister);
328 EXPORT_SYMBOL(tiocx_irq_alloc);
329 EXPORT_SYMBOL(tiocx_irq_free);
330 EXPORT_SYMBOL(tiocx_bus_type);
331 EXPORT_SYMBOL(tiocx_dma_addr);
332 EXPORT_SYMBOL(tiocx_swin_base);
334 static void tio_conveyor_set(nasid_t nasid, int enable_flag)
337 u64 disable_cb = (1ull << 61);
342 ice_frz = REMOTE_HUB_L(nasid, TIO_ICE_FRZ_CFG);
344 if (!(ice_frz & disable_cb)) /* already enabled */
346 ice_frz &= ~disable_cb;
348 if (ice_frz & disable_cb) /* already disabled */
350 ice_frz |= disable_cb;
352 DBG(KERN_ALERT "TIO_ICE_FRZ_CFG= 0x%lx\n", ice_frz);
353 REMOTE_HUB_S(nasid, TIO_ICE_FRZ_CFG, ice_frz);
356 #define tio_conveyor_enable(nasid) tio_conveyor_set(nasid, 1)
357 #define tio_conveyor_disable(nasid) tio_conveyor_set(nasid, 0)
359 static void tio_corelet_reset(nasid_t nasid, int corelet)
364 REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 1 << corelet);
366 REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 0);
370 static int is_fpga_tio(int nasid, int *bt)
372 u16 uninitialized_var(ioboard_type); /* GCC be quiet */
375 rc = ia64_sn_sysctl_ioboard_get(nasid, &ioboard_type);
377 printk(KERN_WARNING "ia64_sn_sysctl_ioboard_get failed: %ld\n",
382 switch (ioboard_type) {
383 case L1_BRICKTYPE_SA:
384 case L1_BRICKTYPE_ATHENA:
385 case L1_BOARDTYPE_DAYTONA:
393 static int bitstream_loaded(nasid_t nasid)
397 cx_credits = REMOTE_HUB_L(nasid, TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3);
398 cx_credits &= TIO_ICE_PMI_TX_DYN_CREDIT_STAT_CB3_CREDIT_CNT_MASK;
399 DBG("cx_credits= 0x%lx\n", cx_credits);
401 return (cx_credits == 0xf) ? 1 : 0;
404 static int tiocx_reload(struct cx_dev *cx_dev)
406 int part_num = CX_DEV_NONE;
407 int mfg_num = CX_DEV_NONE;
408 nasid_t nasid = cx_dev->cx_id.nasid;
410 if (bitstream_loaded(nasid)) {
414 rv = ia64_sn_sysctl_tio_clock_reset(nasid);
416 printk(KERN_ALERT "CX port JTAG reset failed.\n");
418 cx_id = *(volatile u64 *)
419 (TIO_SWIN_BASE(nasid, TIOCX_CORELET) +
421 part_num = XWIDGET_PART_NUM(cx_id);
422 mfg_num = XWIDGET_MFG_NUM(cx_id);
423 DBG("part= 0x%x, mfg= 0x%x\n", part_num, mfg_num);
424 /* just ignore it if it's a CE */
425 if (part_num == TIO_CE_ASIC_PARTNUM)
430 cx_dev->cx_id.part_num = part_num;
431 cx_dev->cx_id.mfg_num = mfg_num;
434 * Delete old device and register the new one. It's ok if
435 * part_num/mfg_num == CX_DEV_NONE. We want to register
436 * devices in the table even if a bitstream isn't loaded.
437 * That allows use to see that a bitstream isn't loaded via
438 * TIOCX_IOCTL_DEV_LIST.
440 return cx_device_reload(cx_dev);
443 static ssize_t show_cxdev_control(struct device *dev, struct device_attribute *attr, char *buf)
445 struct cx_dev *cx_dev = to_cx_dev(dev);
447 return sprintf(buf, "0x%x 0x%x 0x%x 0x%x\n",
449 cx_dev->cx_id.part_num, cx_dev->cx_id.mfg_num,
453 static ssize_t store_cxdev_control(struct device *dev, struct device_attribute *attr, const char *buf,
457 struct cx_dev *cx_dev = to_cx_dev(dev);
459 if (!capable(CAP_SYS_ADMIN))
465 n = simple_strtoul(buf, NULL, 0);
469 tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
470 tiocx_reload(cx_dev);
473 tiocx_reload(cx_dev);
476 tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
485 DEVICE_ATTR(cxdev_control, 0644, show_cxdev_control, store_cxdev_control);
487 static int __init tiocx_init(void)
490 int found_tiocx_device = 0;
492 if (!ia64_platform_is("sn2"))
495 bus_register(&tiocx_bus_type);
497 for (cnodeid = 0; cnodeid < num_cnodes; cnodeid++) {
501 nasid = cnodeid_to_nasid(cnodeid);
503 if ((nasid & 0x1) && is_fpga_tio(nasid, &bt)) {
504 struct hubdev_info *hubdev;
505 struct xwidget_info *widgetp;
507 DBG("Found TIO at nasid 0x%x\n", nasid);
510 (struct hubdev_info *)(NODEPDA(cnodeid)->pdinfo);
512 widgetp = &hubdev->hdi_xwidget_info[TIOCX_CORELET];
514 /* The CE hangs off of the CX port but is not an FPGA */
515 if (widgetp->xwi_hwid.part_num == TIO_CE_ASIC_PARTNUM)
518 tio_corelet_reset(nasid, TIOCX_CORELET);
519 tio_conveyor_enable(nasid);
521 if (cx_device_register
522 (nasid, widgetp->xwi_hwid.part_num,
523 widgetp->xwi_hwid.mfg_num, hubdev, bt) < 0)
526 found_tiocx_device++;
530 /* It's ok if we find zero devices. */
531 DBG("found_tiocx_device= %d\n", found_tiocx_device);
536 static int cx_remove_device(struct device * dev, void * data)
538 struct cx_dev *cx_dev = to_cx_dev(dev);
539 device_remove_file(dev, &dev_attr_cxdev_control);
540 cx_device_unregister(cx_dev);
544 static void __exit tiocx_exit(void)
549 * Unregister devices.
551 bus_for_each_dev(&tiocx_bus_type, NULL, NULL, cx_remove_device);
552 bus_unregister(&tiocx_bus_type);
555 fs_initcall(tiocx_init);
556 module_exit(tiocx_exit);
558 /************************************************************************
559 * Module licensing and description
560 ************************************************************************/
561 MODULE_LICENSE("GPL");
562 MODULE_AUTHOR("Bruce Losure <blosure@sgi.com>");
563 MODULE_DESCRIPTION("TIOCX module");
564 MODULE_SUPPORTED_DEVICE(DEVICE_NAME);