ppc_sys.c 7.7 KB

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