ocp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. #include <linux/module.h>
  38. #include <linux/list.h>
  39. #include <linux/miscdevice.h>
  40. #include <linux/slab.h>
  41. #include <linux/types.h>
  42. #include <linux/init.h>
  43. #include <linux/pm.h>
  44. #include <linux/bootmem.h>
  45. #include <linux/device.h>
  46. #include <asm/io.h>
  47. #include <asm/ocp.h>
  48. #include <asm/errno.h>
  49. #include <asm/rwsem.h>
  50. #include <asm/semaphore.h>
  51. //#define DBG(x) printk x
  52. #define DBG(x)
  53. extern int mem_init_done;
  54. extern struct ocp_def core_ocp[]; /* Static list of devices, provided by
  55. CPU core */
  56. LIST_HEAD(ocp_devices); /* List of all OCP devices */
  57. DECLARE_RWSEM(ocp_devices_sem); /* Global semaphores for those lists */
  58. static int ocp_inited;
  59. /* Sysfs support */
  60. #define OCP_DEF_ATTR(field, format_string) \
  61. static ssize_t \
  62. show_##field(struct device *dev, struct device_attribute *attr, char *buf) \
  63. { \
  64. struct ocp_device *odev = to_ocp_dev(dev); \
  65. \
  66. return sprintf(buf, format_string, odev->def->field); \
  67. } \
  68. static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
  69. OCP_DEF_ATTR(vendor, "0x%04x\n");
  70. OCP_DEF_ATTR(function, "0x%04x\n");
  71. OCP_DEF_ATTR(index, "0x%04x\n");
  72. #ifdef CONFIG_PTE_64BIT
  73. OCP_DEF_ATTR(paddr, "0x%016Lx\n");
  74. #else
  75. OCP_DEF_ATTR(paddr, "0x%08lx\n");
  76. #endif
  77. OCP_DEF_ATTR(irq, "%d\n");
  78. OCP_DEF_ATTR(pm, "%lu\n");
  79. void ocp_create_sysfs_dev_files(struct ocp_device *odev)
  80. {
  81. struct device *dev = &odev->dev;
  82. /* Current OCP device def attributes */
  83. device_create_file(dev, &dev_attr_vendor);
  84. device_create_file(dev, &dev_attr_function);
  85. device_create_file(dev, &dev_attr_index);
  86. device_create_file(dev, &dev_attr_paddr);
  87. device_create_file(dev, &dev_attr_irq);
  88. device_create_file(dev, &dev_attr_pm);
  89. /* Current OCP device additions attributes */
  90. if (odev->def->additions && odev->def->show)
  91. odev->def->show(dev);
  92. }
  93. /**
  94. * ocp_device_match - Match one driver to one device
  95. * @drv: driver to match
  96. * @dev: device to match
  97. *
  98. * This function returns 0 if the driver and device don't match
  99. */
  100. static int
  101. ocp_device_match(struct device *dev, struct device_driver *drv)
  102. {
  103. struct ocp_device *ocp_dev = to_ocp_dev(dev);
  104. struct ocp_driver *ocp_drv = to_ocp_drv(drv);
  105. const struct ocp_device_id *ids = ocp_drv->id_table;
  106. if (!ids)
  107. return 0;
  108. while (ids->vendor || ids->function) {
  109. if ((ids->vendor == OCP_ANY_ID
  110. || ids->vendor == ocp_dev->def->vendor)
  111. && (ids->function == OCP_ANY_ID
  112. || ids->function == ocp_dev->def->function))
  113. return 1;
  114. ids++;
  115. }
  116. return 0;
  117. }
  118. static int
  119. ocp_device_probe(struct device *dev)
  120. {
  121. int error = 0;
  122. struct ocp_driver *drv;
  123. struct ocp_device *ocp_dev;
  124. drv = to_ocp_drv(dev->driver);
  125. ocp_dev = to_ocp_dev(dev);
  126. if (drv->probe) {
  127. error = drv->probe(ocp_dev);
  128. if (error >= 0) {
  129. ocp_dev->driver = drv;
  130. error = 0;
  131. }
  132. }
  133. return error;
  134. }
  135. static int
  136. ocp_device_remove(struct device *dev)
  137. {
  138. struct ocp_device *ocp_dev = to_ocp_dev(dev);
  139. if (ocp_dev->driver) {
  140. if (ocp_dev->driver->remove)
  141. ocp_dev->driver->remove(ocp_dev);
  142. ocp_dev->driver = NULL;
  143. }
  144. return 0;
  145. }
  146. static int
  147. ocp_device_suspend(struct device *dev, pm_message_t state)
  148. {
  149. struct ocp_device *ocp_dev = to_ocp_dev(dev);
  150. struct ocp_driver *ocp_drv = to_ocp_drv(dev->driver);
  151. if (dev->driver && ocp_drv->suspend)
  152. return ocp_drv->suspend(ocp_dev, state);
  153. return 0;
  154. }
  155. static int
  156. ocp_device_resume(struct device *dev)
  157. {
  158. struct ocp_device *ocp_dev = to_ocp_dev(dev);
  159. struct ocp_driver *ocp_drv = to_ocp_drv(dev->driver);
  160. if (dev->driver && ocp_drv->resume)
  161. return ocp_drv->resume(ocp_dev);
  162. return 0;
  163. }
  164. struct bus_type ocp_bus_type = {
  165. .name = "ocp",
  166. .match = ocp_device_match,
  167. .probe = ocp_device_probe,
  168. .remove = ocp_device_remove,
  169. .suspend = ocp_device_suspend,
  170. .resume = ocp_device_resume,
  171. };
  172. /**
  173. * ocp_register_driver - Register an OCP driver
  174. * @drv: pointer to statically defined ocp_driver structure
  175. *
  176. * The driver's probe() callback is called either recursively
  177. * by this function or upon later call of ocp_driver_init
  178. *
  179. * NOTE: Detection of devices is a 2 pass step on this implementation,
  180. * hotswap isn't supported. First, all OCP devices are put in the device
  181. * list, _then_ all drivers are probed on each match.
  182. */
  183. int
  184. ocp_register_driver(struct ocp_driver *drv)
  185. {
  186. /* initialize common driver fields */
  187. drv->driver.name = drv->name;
  188. drv->driver.bus = &ocp_bus_type;
  189. /* register with core */
  190. return driver_register(&drv->driver);
  191. }
  192. /**
  193. * ocp_unregister_driver - Unregister an OCP driver
  194. * @drv: pointer to statically defined ocp_driver structure
  195. *
  196. * The driver's remove() callback is called recursively
  197. * by this function for any device already registered
  198. */
  199. void
  200. ocp_unregister_driver(struct ocp_driver *drv)
  201. {
  202. DBG(("ocp: ocp_unregister_driver(%s)...\n", drv->name));
  203. driver_unregister(&drv->driver);
  204. DBG(("ocp: ocp_unregister_driver(%s)... done.\n", drv->name));
  205. }
  206. /* Core of ocp_find_device(). Caller must hold ocp_devices_sem */
  207. static struct ocp_device *
  208. __ocp_find_device(unsigned int vendor, unsigned int function, int index)
  209. {
  210. struct list_head *entry;
  211. struct ocp_device *dev, *found = NULL;
  212. DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)...\n", vendor, function, index));
  213. list_for_each(entry, &ocp_devices) {
  214. dev = list_entry(entry, struct ocp_device, link);
  215. if (vendor != OCP_ANY_ID && vendor != dev->def->vendor)
  216. continue;
  217. if (function != OCP_ANY_ID && function != dev->def->function)
  218. continue;
  219. if (index != OCP_ANY_INDEX && index != dev->def->index)
  220. continue;
  221. found = dev;
  222. break;
  223. }
  224. DBG(("ocp: __ocp_find_device(vendor: %x, function: %x, index: %d)... done\n", vendor, function, index));
  225. return found;
  226. }
  227. /**
  228. * ocp_find_device - Find a device by function & index
  229. * @vendor: vendor ID of the device (or OCP_ANY_ID)
  230. * @function: function code of the device (or OCP_ANY_ID)
  231. * @idx: index of the device (or OCP_ANY_INDEX)
  232. *
  233. * This function allows a lookup of a given function by it's
  234. * index, it's typically used to find the MAL or ZMII associated
  235. * with an EMAC or similar horrors.
  236. * You can pass vendor, though you usually want OCP_ANY_ID there...
  237. */
  238. struct ocp_device *
  239. ocp_find_device(unsigned int vendor, unsigned int function, int index)
  240. {
  241. struct ocp_device *dev;
  242. down_read(&ocp_devices_sem);
  243. dev = __ocp_find_device(vendor, function, index);
  244. up_read(&ocp_devices_sem);
  245. return dev;
  246. }
  247. /**
  248. * ocp_get_one_device - Find a def by function & index
  249. * @vendor: vendor ID of the device (or OCP_ANY_ID)
  250. * @function: function code of the device (or OCP_ANY_ID)
  251. * @idx: index of the device (or OCP_ANY_INDEX)
  252. *
  253. * This function allows a lookup of a given ocp_def by it's
  254. * vendor, function, and index. The main purpose for is to
  255. * allow modification of the def before binding to the driver
  256. */
  257. struct ocp_def *
  258. ocp_get_one_device(unsigned int vendor, unsigned int function, int index)
  259. {
  260. struct ocp_device *dev;
  261. struct ocp_def *found = NULL;
  262. DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)...\n",
  263. vendor, function, index));
  264. dev = ocp_find_device(vendor, function, index);
  265. if (dev)
  266. found = dev->def;
  267. DBG(("ocp: ocp_get_one_device(vendor: %x, function: %x, index: %d)... done.\n",
  268. vendor, function, index));
  269. return found;
  270. }
  271. /**
  272. * ocp_add_one_device - Add a device
  273. * @def: static device definition structure
  274. *
  275. * This function adds a device definition to the
  276. * device list. It may only be called before
  277. * ocp_driver_init() and will return an error
  278. * otherwise.
  279. */
  280. int
  281. ocp_add_one_device(struct ocp_def *def)
  282. {
  283. struct ocp_device *dev;
  284. DBG(("ocp: ocp_add_one_device()...\n"));
  285. /* Can't be called after ocp driver init */
  286. if (ocp_inited)
  287. return 1;
  288. if (mem_init_done)
  289. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  290. else
  291. dev = alloc_bootmem(sizeof(*dev));
  292. if (dev == NULL)
  293. return 1;
  294. memset(dev, 0, sizeof(*dev));
  295. dev->def = def;
  296. dev->current_state = 4;
  297. sprintf(dev->name, "OCP device %04x:%04x:%04x",
  298. dev->def->vendor, dev->def->function, dev->def->index);
  299. down_write(&ocp_devices_sem);
  300. list_add_tail(&dev->link, &ocp_devices);
  301. up_write(&ocp_devices_sem);
  302. DBG(("ocp: ocp_add_one_device()...done\n"));
  303. return 0;
  304. }
  305. /**
  306. * ocp_remove_one_device - Remove a device by function & index
  307. * @vendor: vendor ID of the device (or OCP_ANY_ID)
  308. * @function: function code of the device (or OCP_ANY_ID)
  309. * @idx: index of the device (or OCP_ANY_INDEX)
  310. *
  311. * This function allows removal of a given function by its
  312. * index. It may only be called before ocp_driver_init()
  313. * and will return an error otherwise.
  314. */
  315. int
  316. ocp_remove_one_device(unsigned int vendor, unsigned int function, int index)
  317. {
  318. struct ocp_device *dev;
  319. DBG(("ocp: ocp_remove_one_device(vendor: %x, function: %x, index: %d)...\n", vendor, function, index));
  320. /* Can't be called after ocp driver init */
  321. if (ocp_inited)
  322. return 1;
  323. down_write(&ocp_devices_sem);
  324. dev = __ocp_find_device(vendor, function, index);
  325. list_del((struct list_head *)dev);
  326. up_write(&ocp_devices_sem);
  327. DBG(("ocp: ocp_remove_one_device(vendor: %x, function: %x, index: %d)... done.\n", vendor, function, index));
  328. return 0;
  329. }
  330. /**
  331. * ocp_for_each_device - Iterate over OCP devices
  332. * @callback: routine to execute for each ocp device.
  333. * @arg: user data to be passed to callback routine.
  334. *
  335. * This routine holds the ocp_device semaphore, so the
  336. * callback routine cannot modify the ocp_device list.
  337. */
  338. void
  339. ocp_for_each_device(void(*callback)(struct ocp_device *, void *arg), void *arg)
  340. {
  341. struct list_head *entry;
  342. if (callback) {
  343. down_read(&ocp_devices_sem);
  344. list_for_each(entry, &ocp_devices)
  345. callback(list_entry(entry, struct ocp_device, link),
  346. arg);
  347. up_read(&ocp_devices_sem);
  348. }
  349. }
  350. /**
  351. * ocp_early_init - Init OCP device management
  352. *
  353. * This function builds the list of devices before setup_arch.
  354. * This allows platform code to modify the device lists before
  355. * they are bound to drivers (changes to paddr, removing devices
  356. * etc)
  357. */
  358. int __init
  359. ocp_early_init(void)
  360. {
  361. struct ocp_def *def;
  362. DBG(("ocp: ocp_early_init()...\n"));
  363. /* Fill the devices list */
  364. for (def = core_ocp; def->vendor != OCP_VENDOR_INVALID; def++)
  365. ocp_add_one_device(def);
  366. DBG(("ocp: ocp_early_init()... done.\n"));
  367. return 0;
  368. }
  369. /**
  370. * ocp_driver_init - Init OCP device management
  371. *
  372. * This function is meant to be called via OCP bus registration.
  373. */
  374. static int __init
  375. ocp_driver_init(void)
  376. {
  377. int ret = 0, index = 0;
  378. struct device *ocp_bus;
  379. struct list_head *entry;
  380. struct ocp_device *dev;
  381. if (ocp_inited)
  382. return ret;
  383. ocp_inited = 1;
  384. DBG(("ocp: ocp_driver_init()...\n"));
  385. /* Allocate/register primary OCP bus */
  386. ocp_bus = kzalloc(sizeof(struct device), GFP_KERNEL);
  387. if (ocp_bus == NULL)
  388. return 1;
  389. strcpy(ocp_bus->bus_id, "ocp");
  390. bus_register(&ocp_bus_type);
  391. device_register(ocp_bus);
  392. /* Put each OCP device into global device list */
  393. list_for_each(entry, &ocp_devices) {
  394. dev = list_entry(entry, struct ocp_device, link);
  395. sprintf(dev->dev.bus_id, "%2.2x", index);
  396. dev->dev.parent = ocp_bus;
  397. dev->dev.bus = &ocp_bus_type;
  398. device_register(&dev->dev);
  399. ocp_create_sysfs_dev_files(dev);
  400. index++;
  401. }
  402. DBG(("ocp: ocp_driver_init()... done.\n"));
  403. return 0;
  404. }
  405. postcore_initcall(ocp_driver_init);
  406. EXPORT_SYMBOL(ocp_bus_type);
  407. EXPORT_SYMBOL(ocp_find_device);
  408. EXPORT_SYMBOL(ocp_register_driver);
  409. EXPORT_SYMBOL(ocp_unregister_driver);