Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v2.6.15-rc2 485 lines 13 kB view raw
1/* 2 * ocp.c 3 * 4 * (c) Benjamin Herrenschmidt (benh@kernel.crashing.org) 5 * Mipsys - France 6 * 7 * Derived from work (c) Armin Kuster akuster@pacbell.net 8 * 9 * Additional support and port to 2.6 LDM/sysfs by 10 * Matt Porter <mporter@kernel.crashing.org> 11 * Copyright 2004 MontaVista Software, Inc. 12 * 13 * This program is free software; you can redistribute it and/or modify it 14 * under the terms of the GNU General Public License as published by the 15 * Free Software Foundation; either version 2 of the License, or (at your 16 * option) any later version. 17 * 18 * OCP (On Chip Peripheral) is a software emulated "bus" with a 19 * pseudo discovery method for dumb peripherals. Usually these type 20 * of peripherals are found on embedded SoC (System On a Chip) 21 * processors or highly integrated system controllers that have 22 * a host bridge and many peripherals. Common examples where 23 * this is already used include the PPC4xx, PPC85xx, MPC52xx, 24 * and MV64xxx parts. 25 * 26 * This subsystem creates a standard OCP bus type within the 27 * device model. The devices on the OCP bus are seeded by an 28 * an initial OCP device array created by the arch-specific 29 * Device entries can be added/removed/modified through OCP 30 * helper functions to accomodate system and board-specific 31 * parameters commonly found in embedded systems. OCP also 32 * provides a standard method for devices to describe extended 33 * attributes about themselves to the system. A standard access 34 * method allows OCP drivers to obtain the information, both 35 * SoC-specific and system/board-specific, needed for operation. 36 */ 37 38#include <linux/module.h> 39#include <linux/config.h> 40#include <linux/list.h> 41#include <linux/miscdevice.h> 42#include <linux/slab.h> 43#include <linux/types.h> 44#include <linux/init.h> 45#include <linux/pm.h> 46#include <linux/bootmem.h> 47#include <linux/device.h> 48 49#include <asm/io.h> 50#include <asm/ocp.h> 51#include <asm/errno.h> 52#include <asm/rwsem.h> 53#include <asm/semaphore.h> 54 55//#define DBG(x) printk x 56#define DBG(x) 57 58extern int mem_init_done; 59 60extern struct ocp_def core_ocp[]; /* Static list of devices, provided by 61 CPU core */ 62 63LIST_HEAD(ocp_devices); /* List of all OCP devices */ 64DECLARE_RWSEM(ocp_devices_sem); /* Global semaphores for those lists */ 65 66static int ocp_inited; 67 68/* Sysfs support */ 69#define OCP_DEF_ATTR(field, format_string) \ 70static ssize_t \ 71show_##field(struct device *dev, struct device_attribute *attr, char *buf) \ 72{ \ 73 struct ocp_device *odev = to_ocp_dev(dev); \ 74 \ 75 return sprintf(buf, format_string, odev->def->field); \ 76} \ 77static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); 78 79OCP_DEF_ATTR(vendor, "0x%04x\n"); 80OCP_DEF_ATTR(function, "0x%04x\n"); 81OCP_DEF_ATTR(index, "0x%04x\n"); 82#ifdef CONFIG_PTE_64BIT 83OCP_DEF_ATTR(paddr, "0x%016Lx\n"); 84#else 85OCP_DEF_ATTR(paddr, "0x%08lx\n"); 86#endif 87OCP_DEF_ATTR(irq, "%d\n"); 88OCP_DEF_ATTR(pm, "%lu\n"); 89 90void ocp_create_sysfs_dev_files(struct ocp_device *odev) 91{ 92 struct device *dev = &odev->dev; 93 94 /* Current OCP device def attributes */ 95 device_create_file(dev, &dev_attr_vendor); 96 device_create_file(dev, &dev_attr_function); 97 device_create_file(dev, &dev_attr_index); 98 device_create_file(dev, &dev_attr_paddr); 99 device_create_file(dev, &dev_attr_irq); 100 device_create_file(dev, &dev_attr_pm); 101 /* Current OCP device additions attributes */ 102 if (odev->def->additions && odev->def->show) 103 odev->def->show(dev); 104} 105 106/** 107 * ocp_device_match - Match one driver to one device 108 * @drv: driver to match 109 * @dev: device to match 110 * 111 * This function returns 0 if the driver and device don't match 112 */ 113static int 114ocp_device_match(struct device *dev, struct device_driver *drv) 115{ 116 struct ocp_device *ocp_dev = to_ocp_dev(dev); 117 struct ocp_driver *ocp_drv = to_ocp_drv(drv); 118 const struct ocp_device_id *ids = ocp_drv->id_table; 119 120 if (!ids) 121 return 0; 122 123 while (ids->vendor || ids->function) { 124 if ((ids->vendor == OCP_ANY_ID 125 || ids->vendor == ocp_dev->def->vendor) 126 && (ids->function == OCP_ANY_ID 127 || ids->function == ocp_dev->def->function)) 128 return 1; 129 ids++; 130 } 131 return 0; 132} 133 134static int 135ocp_device_probe(struct device *dev) 136{ 137 int error = 0; 138 struct ocp_driver *drv; 139 struct ocp_device *ocp_dev; 140 141 drv = to_ocp_drv(dev->driver); 142 ocp_dev = to_ocp_dev(dev); 143 144 if (drv->probe) { 145 error = drv->probe(ocp_dev); 146 if (error >= 0) { 147 ocp_dev->driver = drv; 148 error = 0; 149 } 150 } 151 return error; 152} 153 154static int 155ocp_device_remove(struct device *dev) 156{ 157 struct ocp_device *ocp_dev = to_ocp_dev(dev); 158 159 if (ocp_dev->driver) { 160 if (ocp_dev->driver->remove) 161 ocp_dev->driver->remove(ocp_dev); 162 ocp_dev->driver = NULL; 163 } 164 return 0; 165} 166 167static int 168ocp_device_suspend(struct device *dev, pm_message_t state) 169{ 170 struct ocp_device *ocp_dev = to_ocp_dev(dev); 171 struct ocp_driver *ocp_drv = to_ocp_drv(dev->driver); 172 173 if (dev->driver && ocp_drv->suspend) 174 return ocp_drv->suspend(ocp_dev, state); 175 return 0; 176} 177 178static int 179ocp_device_resume(struct device *dev) 180{ 181 struct ocp_device *ocp_dev = to_ocp_dev(dev); 182 struct ocp_driver *ocp_drv = to_ocp_drv(dev->driver); 183 184 if (dev->driver && ocp_drv->resume) 185 return ocp_drv->resume(ocp_dev); 186 return 0; 187} 188 189struct bus_type ocp_bus_type = { 190 .name = "ocp", 191 .match = ocp_device_match, 192 .suspend = ocp_device_suspend, 193 .resume = ocp_device_resume, 194}; 195 196/** 197 * ocp_register_driver - Register an OCP driver 198 * @drv: pointer to statically defined ocp_driver structure 199 * 200 * The driver's probe() callback is called either recursively 201 * by this function or upon later call of ocp_driver_init 202 * 203 * NOTE: Detection of devices is a 2 pass step on this implementation, 204 * hotswap isn't supported. First, all OCP devices are put in the device 205 * list, _then_ all drivers are probed on each match. 206 */ 207int 208ocp_register_driver(struct ocp_driver *drv) 209{ 210 /* initialize common driver fields */ 211 drv->driver.name = drv->name; 212 drv->driver.bus = &ocp_bus_type; 213 drv->driver.probe = ocp_device_probe; 214 drv->driver.remove = ocp_device_remove; 215 216 /* register with core */ 217 return driver_register(&drv->driver); 218} 219 220/** 221 * ocp_unregister_driver - Unregister an OCP driver 222 * @drv: pointer to statically defined ocp_driver structure 223 * 224 * The driver's remove() callback is called recursively 225 * by this function for any device already registered 226 */ 227void 228ocp_unregister_driver(struct ocp_driver *drv) 229{ 230 DBG(("ocp: ocp_unregister_driver(%s)...\n", drv->name)); 231 232 driver_unregister(&drv->driver); 233 234 DBG(("ocp: ocp_unregister_driver(%s)... done.\n", drv->name)); 235} 236 237/* Core of ocp_find_device(). Caller must hold ocp_devices_sem */ 238static struct ocp_device * 239__ocp_find_device(unsigned int vendor, unsigned int function, int index) 240{ 241 struct list_head *entry; 242 struct ocp_device *dev, *found = NULL; 243 244 DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)...\n", vendor, function, index)); 245 246 list_for_each(entry, &ocp_devices) { 247 dev = list_entry(entry, struct ocp_device, link); 248 if (vendor != OCP_ANY_ID && vendor != dev->def->vendor) 249 continue; 250 if (function != OCP_ANY_ID && function != dev->def->function) 251 continue; 252 if (index != OCP_ANY_INDEX && index != dev->def->index) 253 continue; 254 found = dev; 255 break; 256 } 257 258 DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)... done\n", vendor, function, index)); 259 260 return found; 261} 262 263/** 264 * ocp_find_device - Find a device by function & index 265 * @vendor: vendor ID of the device (or OCP_ANY_ID) 266 * @function: function code of the device (or OCP_ANY_ID) 267 * @idx: index of the device (or OCP_ANY_INDEX) 268 * 269 * This function allows a lookup of a given function by it's 270 * index, it's typically used to find the MAL or ZMII associated 271 * with an EMAC or similar horrors. 272 * You can pass vendor, though you usually want OCP_ANY_ID there... 273 */ 274struct ocp_device * 275ocp_find_device(unsigned int vendor, unsigned int function, int index) 276{ 277 struct ocp_device *dev; 278 279 down_read(&ocp_devices_sem); 280 dev = __ocp_find_device(vendor, function, index); 281 up_read(&ocp_devices_sem); 282 283 return dev; 284} 285 286/** 287 * ocp_get_one_device - Find a def by function & index 288 * @vendor: vendor ID of the device (or OCP_ANY_ID) 289 * @function: function code of the device (or OCP_ANY_ID) 290 * @idx: index of the device (or OCP_ANY_INDEX) 291 * 292 * This function allows a lookup of a given ocp_def by it's 293 * vendor, function, and index. The main purpose for is to 294 * allow modification of the def before binding to the driver 295 */ 296struct ocp_def * 297ocp_get_one_device(unsigned int vendor, unsigned int function, int index) 298{ 299 struct ocp_device *dev; 300 struct ocp_def *found = NULL; 301 302 DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)...\n", 303 vendor, function, index)); 304 305 dev = ocp_find_device(vendor, function, index); 306 307 if (dev) 308 found = dev->def; 309 310 DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)... done.\n", 311 vendor, function, index)); 312 313 return found; 314} 315 316/** 317 * ocp_add_one_device - Add a device 318 * @def: static device definition structure 319 * 320 * This function adds a device definition to the 321 * device list. It may only be called before 322 * ocp_driver_init() and will return an error 323 * otherwise. 324 */ 325int 326ocp_add_one_device(struct ocp_def *def) 327{ 328 struct ocp_device *dev; 329 330 DBG(("ocp: ocp_add_one_device()...\n")); 331 332 /* Can't be called after ocp driver init */ 333 if (ocp_inited) 334 return 1; 335 336 if (mem_init_done) 337 dev = kmalloc(sizeof(*dev), GFP_KERNEL); 338 else 339 dev = alloc_bootmem(sizeof(*dev)); 340 341 if (dev == NULL) 342 return 1; 343 memset(dev, 0, sizeof(*dev)); 344 dev->def = def; 345 dev->current_state = 4; 346 sprintf(dev->name, "OCP device %04x:%04x:%04x", 347 dev->def->vendor, dev->def->function, dev->def->index); 348 down_write(&ocp_devices_sem); 349 list_add_tail(&dev->link, &ocp_devices); 350 up_write(&ocp_devices_sem); 351 352 DBG(("ocp: ocp_add_one_device()...done\n")); 353 354 return 0; 355} 356 357/** 358 * ocp_remove_one_device - Remove a device by function & index 359 * @vendor: vendor ID of the device (or OCP_ANY_ID) 360 * @function: function code of the device (or OCP_ANY_ID) 361 * @idx: index of the device (or OCP_ANY_INDEX) 362 * 363 * This function allows removal of a given function by its 364 * index. It may only be called before ocp_driver_init() 365 * and will return an error otherwise. 366 */ 367int 368ocp_remove_one_device(unsigned int vendor, unsigned int function, int index) 369{ 370 struct ocp_device *dev; 371 372 DBG(("ocp: ocp_remove_one_device(vendor: %x, function: %x, index: %d)...\n", vendor, function, index)); 373 374 /* Can't be called after ocp driver init */ 375 if (ocp_inited) 376 return 1; 377 378 down_write(&ocp_devices_sem); 379 dev = __ocp_find_device(vendor, function, index); 380 list_del((struct list_head *)dev); 381 up_write(&ocp_devices_sem); 382 383 DBG(("ocp: ocp_remove_one_device(vendor: %x, function: %x, index: %d)... done.\n", vendor, function, index)); 384 385 return 0; 386} 387 388/** 389 * ocp_for_each_device - Iterate over OCP devices 390 * @callback: routine to execute for each ocp device. 391 * @arg: user data to be passed to callback routine. 392 * 393 * This routine holds the ocp_device semaphore, so the 394 * callback routine cannot modify the ocp_device list. 395 */ 396void 397ocp_for_each_device(void(*callback)(struct ocp_device *, void *arg), void *arg) 398{ 399 struct list_head *entry; 400 401 if (callback) { 402 down_read(&ocp_devices_sem); 403 list_for_each(entry, &ocp_devices) 404 callback(list_entry(entry, struct ocp_device, link), 405 arg); 406 up_read(&ocp_devices_sem); 407 } 408} 409 410/** 411 * ocp_early_init - Init OCP device management 412 * 413 * This function builds the list of devices before setup_arch. 414 * This allows platform code to modify the device lists before 415 * they are bound to drivers (changes to paddr, removing devices 416 * etc) 417 */ 418int __init 419ocp_early_init(void) 420{ 421 struct ocp_def *def; 422 423 DBG(("ocp: ocp_early_init()...\n")); 424 425 /* Fill the devices list */ 426 for (def = core_ocp; def->vendor != OCP_VENDOR_INVALID; def++) 427 ocp_add_one_device(def); 428 429 DBG(("ocp: ocp_early_init()... done.\n")); 430 431 return 0; 432} 433 434/** 435 * ocp_driver_init - Init OCP device management 436 * 437 * This function is meant to be called via OCP bus registration. 438 */ 439static int __init 440ocp_driver_init(void) 441{ 442 int ret = 0, index = 0; 443 struct device *ocp_bus; 444 struct list_head *entry; 445 struct ocp_device *dev; 446 447 if (ocp_inited) 448 return ret; 449 ocp_inited = 1; 450 451 DBG(("ocp: ocp_driver_init()...\n")); 452 453 /* Allocate/register primary OCP bus */ 454 ocp_bus = kmalloc(sizeof(struct device), GFP_KERNEL); 455 if (ocp_bus == NULL) 456 return 1; 457 memset(ocp_bus, 0, sizeof(struct device)); 458 strcpy(ocp_bus->bus_id, "ocp"); 459 460 bus_register(&ocp_bus_type); 461 462 device_register(ocp_bus); 463 464 /* Put each OCP device into global device list */ 465 list_for_each(entry, &ocp_devices) { 466 dev = list_entry(entry, struct ocp_device, link); 467 sprintf(dev->dev.bus_id, "%2.2x", index); 468 dev->dev.parent = ocp_bus; 469 dev->dev.bus = &ocp_bus_type; 470 device_register(&dev->dev); 471 ocp_create_sysfs_dev_files(dev); 472 index++; 473 } 474 475 DBG(("ocp: ocp_driver_init()... done.\n")); 476 477 return 0; 478} 479 480postcore_initcall(ocp_driver_init); 481 482EXPORT_SYMBOL(ocp_bus_type); 483EXPORT_SYMBOL(ocp_find_device); 484EXPORT_SYMBOL(ocp_register_driver); 485EXPORT_SYMBOL(ocp_unregister_driver);