ppc_sys.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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->flags & PPC_SYS_IORESOURCE_FIXUPPED) != PPC_SYS_IORESOURCE_FIXUPPED)) {
  97. r->start += paddr;
  98. r->end += paddr;
  99. r->flags |= PPC_SYS_IORESOURCE_FIXUPPED;
  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. len = s - dev->bus_id;
  140. } else {
  141. s = dev->bus_id;
  142. len = strlen(dev->bus_id);
  143. }
  144. if (!strncmp(dev->bus_id, map->bus_id, len)) {
  145. pdev = container_of(dev, struct platform_device, dev);
  146. map->rtn(pdev, idx);
  147. }
  148. map++;
  149. }
  150. }
  151. /*
  152. Function assignment stuff.
  153. Intended to work as follows:
  154. the device name defined in foo_devices.c will be concatenated with :"func",
  155. where func is string map of respective function from platfom_device_func enum
  156. The PPC_SYS_FUNC_DUMMY function is intended to remove all assignments, making the device to appear
  157. in platform bus with unmodified name.
  158. */
  159. /*
  160. Here we'll replace .name pointers with fixed-lenght strings
  161. Hereby, this should be called *before* any func stuff triggeded.
  162. */
  163. void ppc_sys_device_initfunc(void)
  164. {
  165. int i;
  166. const char *name;
  167. static char new_names[NUM_PPC_SYS_DEVS][BUS_ID_SIZE];
  168. enum ppc_sys_devices cur_dev;
  169. /* If inited yet, do nothing */
  170. if (ppc_sys_func_inited)
  171. return;
  172. for (i = 0; i < cur_ppc_sys_spec->num_devices; i++) {
  173. if ((cur_dev = cur_ppc_sys_spec->device_list[i]) < 0)
  174. continue;
  175. if (ppc_sys_platform_devices[cur_dev].name) {
  176. /*backup name */
  177. name = ppc_sys_platform_devices[cur_dev].name;
  178. strlcpy(new_names[i], name, BUS_ID_SIZE);
  179. ppc_sys_platform_devices[cur_dev].name = new_names[i];
  180. }
  181. }
  182. ppc_sys_func_inited = 1;
  183. }
  184. /*The "engine" of the func stuff. Here we either concat specified function string description
  185. to the name, or remove it if PPC_SYS_FUNC_DUMMY parameter is passed here*/
  186. void ppc_sys_device_setfunc(enum ppc_sys_devices dev,
  187. enum platform_device_func func)
  188. {
  189. char *s;
  190. char *name = (char *)ppc_sys_platform_devices[dev].name;
  191. char tmp[BUS_ID_SIZE];
  192. if (!ppc_sys_func_inited) {
  193. printk(KERN_ERR "Unable to alter function - not inited!\n");
  194. return;
  195. }
  196. if (ppc_sys_inited) {
  197. platform_device_unregister(&ppc_sys_platform_devices[dev]);
  198. }
  199. if ((s = (char *)strchr(name, ':')) != NULL) { /* reassign */
  200. /* Either change the name after ':' or remove func modifications */
  201. if (func != PPC_SYS_FUNC_DUMMY)
  202. strlcpy(s + 1, ppc_sys_func_names[func], BUS_ID_SIZE);
  203. else
  204. *s = 0;
  205. } else if (func != PPC_SYS_FUNC_DUMMY) {
  206. /* do assignment if it is not just "clear" request */
  207. sprintf(tmp, "%s:%s", name, ppc_sys_func_names[func]);
  208. strlcpy(name, tmp, BUS_ID_SIZE);
  209. }
  210. if (ppc_sys_inited) {
  211. platform_device_register(&ppc_sys_platform_devices[dev]);
  212. }
  213. }
  214. void ppc_sys_device_disable(enum ppc_sys_devices dev)
  215. {
  216. BUG_ON(cur_ppc_sys_spec == NULL);
  217. /*Check if it is enabled*/
  218. if(!(cur_ppc_sys_spec->config[dev] & PPC_SYS_CONFIG_DISABLED)) {
  219. if (ppc_sys_inited) {
  220. platform_device_unregister(&ppc_sys_platform_devices[dev]);
  221. }
  222. cur_ppc_sys_spec->config[dev] |= PPC_SYS_CONFIG_DISABLED;
  223. }
  224. }
  225. void ppc_sys_device_enable(enum ppc_sys_devices dev)
  226. {
  227. BUG_ON(cur_ppc_sys_spec == NULL);
  228. /*Check if it is disabled*/
  229. if(cur_ppc_sys_spec->config[dev] & PPC_SYS_CONFIG_DISABLED) {
  230. if (ppc_sys_inited) {
  231. platform_device_register(&ppc_sys_platform_devices[dev]);
  232. }
  233. cur_ppc_sys_spec->config[dev] &= ~PPC_SYS_CONFIG_DISABLED;
  234. }
  235. }
  236. void ppc_sys_device_enable_all(void)
  237. {
  238. enum ppc_sys_devices cur_dev;
  239. int i;
  240. for (i = 0; i < cur_ppc_sys_spec->num_devices; i++) {
  241. cur_dev = cur_ppc_sys_spec->device_list[i];
  242. ppc_sys_device_enable(cur_dev);
  243. }
  244. }
  245. void ppc_sys_device_disable_all(void)
  246. {
  247. enum ppc_sys_devices cur_dev;
  248. int i;
  249. for (i = 0; i < cur_ppc_sys_spec->num_devices; i++) {
  250. cur_dev = cur_ppc_sys_spec->device_list[i];
  251. ppc_sys_device_disable(cur_dev);
  252. }
  253. }
  254. static int __init ppc_sys_init(void)
  255. {
  256. unsigned int i, dev_id, ret = 0;
  257. BUG_ON(cur_ppc_sys_spec == NULL);
  258. for (i = 0; i < cur_ppc_sys_spec->num_devices; i++) {
  259. dev_id = cur_ppc_sys_spec->device_list[i];
  260. if ((dev_id != -1) &&
  261. !(cur_ppc_sys_spec->config[dev_id] & PPC_SYS_CONFIG_DISABLED)) {
  262. if (ppc_sys_device_fixup != NULL)
  263. ppc_sys_device_fixup(&ppc_sys_platform_devices
  264. [dev_id]);
  265. if (platform_device_register
  266. (&ppc_sys_platform_devices[dev_id])) {
  267. ret = 1;
  268. printk(KERN_ERR
  269. "unable to register device %d\n",
  270. dev_id);
  271. }
  272. }
  273. }
  274. ppc_sys_inited = 1;
  275. return ret;
  276. }
  277. subsys_initcall(ppc_sys_init);