ppc_sys.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * PPC System library functions
  3. *
  4. * Maintainer: Kumar Gala <galak@kernel.crashing.org>
  5. *
  6. * Copyright 2005 Freescale Semiconductor Inc.
  7. * Copyright 2005 MontaVista, Inc. by Vitaly Bordug <vbordug@ru.mvista.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/string.h>
  15. #include <linux/bootmem.h>
  16. #include <asm/ppc_sys.h>
  17. int (*ppc_sys_device_fixup) (struct platform_device * pdev);
  18. static int ppc_sys_inited;
  19. static int ppc_sys_func_inited;
  20. static const char *ppc_sys_func_names[] = {
  21. [PPC_SYS_FUNC_DUMMY] = "dummy",
  22. [PPC_SYS_FUNC_ETH] = "eth",
  23. [PPC_SYS_FUNC_UART] = "uart",
  24. [PPC_SYS_FUNC_HLDC] = "hldc",
  25. [PPC_SYS_FUNC_USB] = "usb",
  26. [PPC_SYS_FUNC_IRDA] = "irda",
  27. };
  28. void __init identify_ppc_sys_by_id(u32 id)
  29. {
  30. unsigned int i = 0;
  31. while (1) {
  32. if ((ppc_sys_specs[i].mask & id) == ppc_sys_specs[i].value)
  33. break;
  34. i++;
  35. }
  36. cur_ppc_sys_spec = &ppc_sys_specs[i];
  37. return;
  38. }
  39. void __init identify_ppc_sys_by_name(char *name)
  40. {
  41. unsigned int i = 0;
  42. while (ppc_sys_specs[i].ppc_sys_name[0]) {
  43. if (!strcmp(ppc_sys_specs[i].ppc_sys_name, name))
  44. break;
  45. i++;
  46. }
  47. cur_ppc_sys_spec = &ppc_sys_specs[i];
  48. return;
  49. }
  50. static int __init count_sys_specs(void)
  51. {
  52. int i = 0;
  53. while (ppc_sys_specs[i].ppc_sys_name[0])
  54. i++;
  55. return i;
  56. }
  57. static int __init find_chip_by_name_and_id(char *name, u32 id)
  58. {
  59. int ret = -1;
  60. unsigned int i = 0;
  61. unsigned int j = 0;
  62. unsigned int dups = 0;
  63. unsigned char matched[count_sys_specs()];
  64. while (ppc_sys_specs[i].ppc_sys_name[0]) {
  65. if (!strcmp(ppc_sys_specs[i].ppc_sys_name, name))
  66. matched[j++] = i;
  67. i++;
  68. }
  69. ret = i;
  70. if (j != 0) {
  71. for (i = 0; i < j; i++) {
  72. if ((ppc_sys_specs[matched[i]].mask & id) ==
  73. ppc_sys_specs[matched[i]].value) {
  74. ret = matched[i];
  75. dups++;
  76. }
  77. }
  78. ret = (dups == 1) ? ret : (-1 * dups);
  79. }
  80. return ret;
  81. }
  82. void __init identify_ppc_sys_by_name_and_id(char *name, u32 id)
  83. {
  84. int i = find_chip_by_name_and_id(name, id);
  85. BUG_ON(i < 0);
  86. cur_ppc_sys_spec = &ppc_sys_specs[i];
  87. }
  88. /* Update all memory resources by paddr, call before platform_device_register */
  89. void __init
  90. ppc_sys_fixup_mem_resource(struct platform_device *pdev, phys_addr_t paddr)
  91. {
  92. int i;
  93. for (i = 0; i < pdev->num_resources; i++) {
  94. struct resource *r = &pdev->resource[i];
  95. if ((r->flags & IORESOURCE_MEM) == IORESOURCE_MEM) {
  96. r->start += paddr;
  97. r->end += paddr;
  98. }
  99. }
  100. }
  101. /* Get platform_data pointer out of platform device, call before platform_device_register */
  102. void *__init ppc_sys_get_pdata(enum ppc_sys_devices dev)
  103. {
  104. return ppc_sys_platform_devices[dev].dev.platform_data;
  105. }
  106. void ppc_sys_device_remove(enum ppc_sys_devices dev)
  107. {
  108. unsigned int i;
  109. if (ppc_sys_inited) {
  110. platform_device_unregister(&ppc_sys_platform_devices[dev]);
  111. } else {
  112. if (cur_ppc_sys_spec == NULL)
  113. return;
  114. for (i = 0; i < cur_ppc_sys_spec->num_devices; i++)
  115. if (cur_ppc_sys_spec->device_list[i] == dev)
  116. cur_ppc_sys_spec->device_list[i] = -1;
  117. }
  118. }
  119. /* Platform-notify mapping
  120. * Helper function for BSP code to assign board-specific platfom-divice bits
  121. */
  122. void platform_notify_map(const struct platform_notify_dev_map *map,
  123. struct device *dev)
  124. {
  125. struct platform_device *pdev;
  126. int len, idx;
  127. const char *s;
  128. /* do nothing if no device or no bus_id */
  129. if (!dev || !dev->bus_id)
  130. return;
  131. /* call per device map */
  132. while (map->bus_id != NULL) {
  133. idx = -1;
  134. s = strrchr(dev->bus_id, '.');
  135. if (s != NULL)
  136. idx = (int)simple_strtol(s + 1, NULL, 10);
  137. else
  138. s = dev->bus_id;
  139. len = s - dev->bus_id;
  140. if (!strncmp(dev->bus_id, map->bus_id, len)) {
  141. pdev = container_of(dev, struct platform_device, dev);
  142. map->rtn(pdev, idx);
  143. }
  144. map++;
  145. }
  146. }
  147. /*
  148. Function assignment stuff.
  149. Intended to work as follows:
  150. the device name defined in foo_devices.c will be concatenated with :"func",
  151. where func is string map of respective function from platfom_device_func enum
  152. The PPC_SYS_FUNC_DUMMY function is intended to remove all assignments, making the device to appear
  153. in platform bus with unmodified name.
  154. */
  155. /*
  156. Here we'll replace .name pointers with fixed-lenght strings
  157. Hereby, this should be called *before* any func stuff triggeded.
  158. */
  159. void ppc_sys_device_initfunc(void)
  160. {
  161. int i;
  162. const char *name;
  163. static char new_names[NUM_PPC_SYS_DEVS][BUS_ID_SIZE];
  164. enum ppc_sys_devices cur_dev;
  165. /* If inited yet, do nothing */
  166. if (ppc_sys_func_inited)
  167. return;
  168. for (i = 0; i < cur_ppc_sys_spec->num_devices; i++) {
  169. if ((cur_dev = cur_ppc_sys_spec->device_list[i]) < 0)
  170. continue;
  171. if (ppc_sys_platform_devices[cur_dev].name) {
  172. /*backup name */
  173. name = ppc_sys_platform_devices[cur_dev].name;
  174. strlcpy(new_names[i], name, BUS_ID_SIZE);
  175. ppc_sys_platform_devices[cur_dev].name = new_names[i];
  176. }
  177. }
  178. ppc_sys_func_inited = 1;
  179. }
  180. /*The "engine" of the func stuff. Here we either concat specified function string description
  181. to the name, or remove it if PPC_SYS_FUNC_DUMMY parameter is passed here*/
  182. void ppc_sys_device_setfunc(enum ppc_sys_devices dev,
  183. enum platform_device_func func)
  184. {
  185. char *s;
  186. char *name = (char *)ppc_sys_platform_devices[dev].name;
  187. char tmp[BUS_ID_SIZE];
  188. if (!ppc_sys_func_inited) {
  189. printk(KERN_ERR "Unable to alter function - not inited!\n");
  190. return;
  191. }
  192. if (ppc_sys_inited) {
  193. platform_device_unregister(&ppc_sys_platform_devices[dev]);
  194. }
  195. if ((s = (char *)strchr(name, ':')) != NULL) { /* reassign */
  196. /* Either change the name after ':' or remove func modifications */
  197. if (func != PPC_SYS_FUNC_DUMMY)
  198. strlcpy(s + 1, ppc_sys_func_names[func], BUS_ID_SIZE);
  199. else
  200. *s = 0;
  201. } else if (func != PPC_SYS_FUNC_DUMMY) {
  202. /* do assignment if it is not just "clear" request */
  203. sprintf(tmp, "%s:%s", name, ppc_sys_func_names[func]);
  204. strlcpy(name, tmp, BUS_ID_SIZE);
  205. }
  206. if (ppc_sys_inited) {
  207. platform_device_register(&ppc_sys_platform_devices[dev]);
  208. }
  209. }
  210. void ppc_sys_device_disable(enum ppc_sys_devices dev)
  211. {
  212. BUG_ON(cur_ppc_sys_spec == NULL);
  213. /*Check if it is enabled*/
  214. if(!(cur_ppc_sys_spec->config[dev] & PPC_SYS_CONFIG_DISABLED)) {
  215. if (ppc_sys_inited) {
  216. platform_device_unregister(&ppc_sys_platform_devices[dev]);
  217. }
  218. cur_ppc_sys_spec->config[dev] |= PPC_SYS_CONFIG_DISABLED;
  219. }
  220. }
  221. void ppc_sys_device_enable(enum ppc_sys_devices dev)
  222. {
  223. BUG_ON(cur_ppc_sys_spec == NULL);
  224. /*Check if it is disabled*/
  225. if(cur_ppc_sys_spec->config[dev] & PPC_SYS_CONFIG_DISABLED) {
  226. if (ppc_sys_inited) {
  227. platform_device_register(&ppc_sys_platform_devices[dev]);
  228. }
  229. cur_ppc_sys_spec->config[dev] &= ~PPC_SYS_CONFIG_DISABLED;
  230. }
  231. }
  232. void ppc_sys_device_enable_all(void)
  233. {
  234. enum ppc_sys_devices cur_dev;
  235. int i;
  236. for (i = 0; i < cur_ppc_sys_spec->num_devices; i++) {
  237. cur_dev = cur_ppc_sys_spec->device_list[i];
  238. ppc_sys_device_enable(cur_dev);
  239. }
  240. }
  241. void ppc_sys_device_disable_all(void)
  242. {
  243. enum ppc_sys_devices cur_dev;
  244. int i;
  245. for (i = 0; i < cur_ppc_sys_spec->num_devices; i++) {
  246. cur_dev = cur_ppc_sys_spec->device_list[i];
  247. ppc_sys_device_disable(cur_dev);
  248. }
  249. }
  250. static int __init ppc_sys_init(void)
  251. {
  252. unsigned int i, dev_id, ret = 0;
  253. BUG_ON(cur_ppc_sys_spec == NULL);
  254. for (i = 0; i < cur_ppc_sys_spec->num_devices; i++) {
  255. dev_id = cur_ppc_sys_spec->device_list[i];
  256. if ((dev_id != -1) &&
  257. !(cur_ppc_sys_spec->config[dev_id] & PPC_SYS_CONFIG_DISABLED)) {
  258. if (ppc_sys_device_fixup != NULL)
  259. ppc_sys_device_fixup(&ppc_sys_platform_devices
  260. [dev_id]);
  261. if (platform_device_register
  262. (&ppc_sys_platform_devices[dev_id])) {
  263. ret = 1;
  264. printk(KERN_ERR
  265. "unable to register device %d\n",
  266. dev_id);
  267. }
  268. }
  269. }
  270. ppc_sys_inited = 1;
  271. return ret;
  272. }
  273. subsys_initcall(ppc_sys_init);