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/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_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
73 static void tiocx_bus_release(struct device
*dev
)
75 kfree(to_cx_dev(dev
));
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
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
)
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
);
117 if (!cx_dev
->driver
&& cx_drv
->probe
) {
118 id
= cx_device_match(cx_drv
->id_table
, cx_dev
);
120 if ((error
= cx_drv
->probe(cx_dev
, id
)) < 0)
123 cx_dev
->driver
= cx_drv
;
131 * cx_driver_remove - Remove driver from device struct.
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
;
139 cx_drv
->remove(cx_dev
);
140 cx_dev
->driver
= NULL
;
144 struct bus_type tiocx_bus_type
= {
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
);
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
;
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 dev_set_name(&cx_dev
->dev
, "%d", cx_dev
->cx_id
.nasid
);
210 r
= device_register(&cx_dev
->dev
);
215 get_device(&cx_dev
->dev
);
217 device_create_file(&cx_dev
->dev
, &dev_attr_cxdev_control
);
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
);
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
,
250 static inline u64
tiocx_intr_alloc(nasid_t nasid
, int widget
,
252 int req_irq
, nasid_t req_nasid
,
255 struct ia64_sal_retval rv
;
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
);
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
;
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
;
284 int sn_irq_size
= sizeof(struct sn_irq_info
);
286 if ((nasid
& 1) == 0)
289 sn_irq_info
= kzalloc(sn_irq_size
, GFP_KERNEL
);
290 if (sn_irq_info
== NULL
)
293 status
= tiocx_intr_alloc(nasid
, widget
, __pa(sn_irq_info
), irq
,
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
);
310 widget
= TIO_SWIN_WIDGETNUM(bridge
);
311 tiocx_intr_free(nasid
, widget
, 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
)
339 u64 disable_cb
= (1ull << 61);
344 ice_frz
= REMOTE_HUB_L(nasid
, TIO_ICE_FRZ_CFG
);
346 if (!(ice_frz
& disable_cb
)) /* already enabled */
348 ice_frz
&= ~disable_cb
;
350 if (ice_frz
& disable_cb
) /* already disabled */
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
)
366 REMOTE_HUB_S(nasid
, TIO_ICE_PMI_TX_CFG
, 1 << corelet
);
368 REMOTE_HUB_S(nasid
, TIO_ICE_PMI_TX_CFG
, 0);
372 static int is_fpga_tio(int nasid
, int *bt
)
374 u16
uninitialized_var(ioboard_type
); /* GCC be quiet */
377 rc
= ia64_sn_sysctl_ioboard_get(nasid
, &ioboard_type
);
379 printk(KERN_WARNING
"ia64_sn_sysctl_ioboard_get failed: %ld\n",
384 switch (ioboard_type
) {
385 case L1_BRICKTYPE_SA
:
386 case L1_BRICKTYPE_ATHENA
:
387 case L1_BOARDTYPE_DAYTONA
:
395 static int bitstream_loaded(nasid_t nasid
)
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
)) {
416 rv
= ia64_sn_sysctl_tio_clock_reset(nasid
);
418 printk(KERN_ALERT
"CX port JTAG reset failed.\n");
420 cx_id
= *(volatile u64
*)
421 (TIO_SWIN_BASE(nasid
, TIOCX_CORELET
) +
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
)
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",
451 cx_dev
->cx_id
.part_num
, cx_dev
->cx_id
.mfg_num
,
455 static ssize_t
store_cxdev_control(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
459 struct cx_dev
*cx_dev
= to_cx_dev(dev
);
461 if (!capable(CAP_SYS_ADMIN
))
467 n
= simple_strtoul(buf
, NULL
, 0);
471 tio_corelet_reset(cx_dev
->cx_id
.nasid
, TIOCX_CORELET
);
472 tiocx_reload(cx_dev
);
475 tiocx_reload(cx_dev
);
478 tio_corelet_reset(cx_dev
->cx_id
.nasid
, TIOCX_CORELET
);
487 DEVICE_ATTR(cxdev_control
, 0644, show_cxdev_control
, store_cxdev_control
);
489 static int __init
tiocx_init(void)
492 int found_tiocx_device
= 0;
494 if (!ia64_platform_is("sn2"))
497 bus_register(&tiocx_bus_type
);
499 for (cnodeid
= 0; cnodeid
< num_cnodes
; cnodeid
++) {
503 nasid
= cnodeid_to_nasid(cnodeid
);
505 if ((nasid
& 0x1) && is_fpga_tio(nasid
, &bt
)) {
506 struct hubdev_info
*hubdev
;
507 struct xwidget_info
*widgetp
;
509 DBG("Found TIO at nasid 0x%x\n", nasid
);
512 (struct hubdev_info
*)(NODEPDA(cnodeid
)->pdinfo
);
514 widgetp
= &hubdev
->hdi_xwidget_info
[TIOCX_CORELET
];
516 /* The CE hangs off of the CX port but is not an FPGA */
517 if (widgetp
->xwi_hwid
.part_num
== TIO_CE_ASIC_PARTNUM
)
520 tio_corelet_reset(nasid
, TIOCX_CORELET
);
521 tio_conveyor_enable(nasid
);
523 if (cx_device_register
524 (nasid
, widgetp
->xwi_hwid
.part_num
,
525 widgetp
->xwi_hwid
.mfg_num
, hubdev
, bt
) < 0)
528 found_tiocx_device
++;
532 /* It's ok if we find zero devices. */
533 DBG("found_tiocx_device= %d\n", found_tiocx_device
);
538 static int cx_remove_device(struct device
* dev
, void * data
)
540 struct cx_dev
*cx_dev
= to_cx_dev(dev
);
541 device_remove_file(dev
, &dev_attr_cxdev_control
);
542 cx_device_unregister(cx_dev
);
546 static void __exit
tiocx_exit(void)
551 * Unregister devices.
553 bus_for_each_dev(&tiocx_bus_type
, NULL
, NULL
, cx_remove_device
);
554 bus_unregister(&tiocx_bus_type
);
557 fs_initcall(tiocx_init
);
558 module_exit(tiocx_exit
);
560 /************************************************************************
561 * Module licensing and description
562 ************************************************************************/
563 MODULE_LICENSE("GPL");
564 MODULE_AUTHOR("Bruce Losure <blosure@sgi.com>");
565 MODULE_DESCRIPTION("TIOCX module");
566 MODULE_SUPPORTED_DEVICE(DEVICE_NAME
);