ocp.c 13 KB

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