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/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>
27 #include "xtalk/xwidgetdev.h"
28 #include "xtalk/hubdev.h"
31 #define DEVICE_NAME "tiocx"
36 #define DBG(fmt...) printk(KERN_ALERT fmt)
41 struct device_attribute dev_attr_cxdev_control;
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;
59 while (ids->part_num) {
60 if (ids->part_num == cx_dev->cx_id.part_num)
68 static int tiocx_hotplug(struct device *dev, char **envp, int num_envp,
69 char *buffer, int buffer_size)
74 static void tiocx_bus_release(struct device *dev)
76 kfree(to_cx_dev(dev));
79 struct bus_type tiocx_bus_type = {
82 .hotplug = tiocx_hotplug,
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
91 static const struct cx_device_id *cx_device_match(const struct cx_device_id
93 struct cx_dev *cx_device)
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.
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)
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
117 static int cx_device_probe(struct device *dev)
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);
124 if (!cx_dev->driver && cx_drv->probe) {
125 id = cx_device_match(cx_drv->id_table, cx_dev);
127 if ((error = cx_drv->probe(cx_dev, id)) < 0)
130 cx_dev->driver = cx_drv;
138 * cx_driver_remove - Remove driver from device struct.
141 static int cx_driver_remove(struct device *dev)
143 struct cx_dev *cx_dev = to_cx_dev(dev);
144 struct cx_drv *cx_drv = cx_dev->driver;
146 cx_drv->remove(cx_dev);
147 cx_dev->driver = NULL;
152 * cx_driver_register - Register the driver.
153 * @cx_driver: driver table (cx_drv struct) from driver
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.
160 int cx_driver_register(struct cx_drv *cx_driver)
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;
167 return driver_register(&cx_driver->driver);
171 * cx_driver_unregister - Unregister the driver.
172 * @cx_driver: driver table (cx_drv struct) from driver
174 int cx_driver_unregister(struct cx_drv *cx_driver)
176 driver_unregister(&cx_driver->driver);
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
190 cx_device_register(nasid_t nasid, int part_num, int mfg_num,
191 struct hubdev_info *hubdev, int bt)
193 struct cx_dev *cx_dev;
195 cx_dev = kzalloc(sizeof(struct cx_dev), GFP_KERNEL);
196 DBG("cx_dev= 0x%p\n", cx_dev);
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;
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);
214 device_create_file(&cx_dev->dev, &dev_attr_cxdev_control);
220 * cx_device_unregister - Unregister a device.
221 * @cx_dev: part/mfg id for the device
223 int cx_device_unregister(struct cx_dev *cx_dev)
225 put_device(&cx_dev->dev);
226 device_unregister(&cx_dev->dev);
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
236 * Remove the device associated with 'nasid' from device list and then
237 * call device-register with the given part/mfg numbers.
239 static int cx_device_reload(struct cx_dev *cx_dev)
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,
247 static inline uint64_t tiocx_intr_alloc(nasid_t nasid, int widget,
249 int req_irq, nasid_t req_nasid,
252 struct ia64_sal_retval rv;
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);
263 static inline void tiocx_intr_free(nasid_t nasid, int widget,
264 struct sn_irq_info *sn_irq_info)
266 struct ia64_sal_retval rv;
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);
276 struct sn_irq_info *tiocx_irq_alloc(nasid_t nasid, int widget, int irq,
277 nasid_t req_nasid, int slice)
279 struct sn_irq_info *sn_irq_info;
281 int sn_irq_size = sizeof(struct sn_irq_info);
283 if ((nasid & 1) == 0)
286 sn_irq_info = kmalloc(sn_irq_size, GFP_KERNEL);
287 if (sn_irq_info == NULL)
290 memset(sn_irq_info, 0x0, sn_irq_size);
292 status = tiocx_intr_alloc(nasid, widget, __pa(sn_irq_info), irq,
302 void tiocx_irq_free(struct sn_irq_info *sn_irq_info)
304 uint64_t bridge = (uint64_t) sn_irq_info->irq_bridge;
305 nasid_t nasid = NASID_GET(bridge);
309 widget = TIO_SWIN_WIDGETNUM(bridge);
310 tiocx_intr_free(nasid, widget, sn_irq_info);
315 uint64_t tiocx_dma_addr(uint64_t addr)
317 return PHYS_TO_TIODMA(addr);
320 uint64_t tiocx_swin_base(int nasid)
322 return TIO_SWIN_BASE(nasid, TIOCX_CORELET);
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);
335 static void tio_conveyor_set(nasid_t nasid, int enable_flag)
338 uint64_t disable_cb = (1ull << 61);
343 ice_frz = REMOTE_HUB_L(nasid, TIO_ICE_FRZ_CFG);
345 if (!(ice_frz & disable_cb)) /* already enabled */
347 ice_frz &= ~disable_cb;
349 if (ice_frz & disable_cb) /* already disabled */
351 ice_frz |= disable_cb;
353 DBG(KERN_ALERT "TIO_ICE_FRZ_CFG= 0x%lx\n", ice_frz);
354 REMOTE_HUB_S(nasid, TIO_ICE_FRZ_CFG, ice_frz);
357 #define tio_conveyor_enable(nasid) tio_conveyor_set(nasid, 1)
358 #define tio_conveyor_disable(nasid) tio_conveyor_set(nasid, 0)
360 static void tio_corelet_reset(nasid_t nasid, int corelet)
365 REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 1 << corelet);
367 REMOTE_HUB_S(nasid, TIO_ICE_PMI_TX_CFG, 0);
371 static int is_fpga_tio(int nasid, int *bt)
375 ioboard_type = ia64_sn_sysctl_ioboard_get(nasid);
377 switch (ioboard_type) {
378 case L1_BRICKTYPE_SA:
379 case L1_BRICKTYPE_ATHENA:
380 case L1_BOARDTYPE_DAYTONA:
388 static int bitstream_loaded(nasid_t nasid)
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);
396 return (cx_credits == 0xf) ? 1 : 0;
399 static int tiocx_reload(struct cx_dev *cx_dev)
401 int part_num = CX_DEV_NONE;
402 int mfg_num = CX_DEV_NONE;
403 nasid_t nasid = cx_dev->cx_id.nasid;
405 if (bitstream_loaded(nasid)) {
409 rv = ia64_sn_sysctl_tio_clock_reset(nasid);
411 printk(KERN_ALERT "CX port JTAG reset failed.\n");
413 cx_id = *(volatile uint64_t *)
414 (TIO_SWIN_BASE(nasid, TIOCX_CORELET) +
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)
425 cx_dev->cx_id.part_num = part_num;
426 cx_dev->cx_id.mfg_num = mfg_num;
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.
435 return cx_device_reload(cx_dev);
438 static ssize_t show_cxdev_control(struct device *dev, struct device_attribute *attr, char *buf)
440 struct cx_dev *cx_dev = to_cx_dev(dev);
442 return sprintf(buf, "0x%x 0x%x 0x%x 0x%x\n",
444 cx_dev->cx_id.part_num, cx_dev->cx_id.mfg_num,
448 static ssize_t store_cxdev_control(struct device *dev, struct device_attribute *attr, const char *buf,
452 struct cx_dev *cx_dev = to_cx_dev(dev);
454 if (!capable(CAP_SYS_ADMIN))
460 n = simple_strtoul(buf, NULL, 0);
464 tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
465 tiocx_reload(cx_dev);
468 tiocx_reload(cx_dev);
471 tio_corelet_reset(cx_dev->cx_id.nasid, TIOCX_CORELET);
480 DEVICE_ATTR(cxdev_control, 0644, show_cxdev_control, store_cxdev_control);
482 static int __init tiocx_init(void)
485 int found_tiocx_device = 0;
487 if (!ia64_platform_is("sn2"))
490 bus_register(&tiocx_bus_type);
492 for (cnodeid = 0; cnodeid < MAX_COMPACT_NODES; cnodeid++) {
496 if ((nasid = cnodeid_to_nasid(cnodeid)) < 0)
497 break; /* No more nasids .. bail out of loop */
499 if ((nasid & 0x1) && is_fpga_tio(nasid, &bt)) {
500 struct hubdev_info *hubdev;
501 struct xwidget_info *widgetp;
503 DBG("Found TIO at nasid 0x%x\n", nasid);
506 (struct hubdev_info *)(NODEPDA(cnodeid)->pdinfo);
508 widgetp = &hubdev->hdi_xwidget_info[TIOCX_CORELET];
510 /* The CE hangs off of the CX port but is not an FPGA */
511 if (widgetp->xwi_hwid.part_num == TIO_CE_ASIC_PARTNUM)
514 tio_corelet_reset(nasid, TIOCX_CORELET);
515 tio_conveyor_enable(nasid);
517 if (cx_device_register
518 (nasid, widgetp->xwi_hwid.part_num,
519 widgetp->xwi_hwid.mfg_num, hubdev, bt) < 0)
522 found_tiocx_device++;
526 /* It's ok if we find zero devices. */
527 DBG("found_tiocx_device= %d\n", found_tiocx_device);
532 static int cx_remove_device(struct device * dev, void * data)
534 struct cx_dev *cx_dev = to_cx_dev(dev);
535 device_remove_file(dev, &dev_attr_cxdev_control);
536 cx_device_unregister(cx_dev);
540 static void __exit tiocx_exit(void)
545 * Unregister devices.
547 bus_for_each_dev(&tiocx_bus_type, NULL, NULL, cx_remove_device);
548 bus_unregister(&tiocx_bus_type);
551 subsys_initcall(tiocx_init);
552 module_exit(tiocx_exit);
554 /************************************************************************
555 * Module licensing and description
556 ************************************************************************/
557 MODULE_LICENSE("GPL");
558 MODULE_AUTHOR("Bruce Losure <blosure@sgi.com>");
559 MODULE_DESCRIPTION("TIOCX module");
560 MODULE_SUPPORTED_DEVICE(DEVICE_NAME);