of_device_32.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include <linux/string.h>
  2. #include <linux/kernel.h>
  3. #include <linux/of.h>
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/mod_devicetable.h>
  7. #include <linux/slab.h>
  8. #include <linux/errno.h>
  9. #include <linux/irq.h>
  10. #include <linux/of_device.h>
  11. #include <linux/of_platform.h>
  12. #include "of_device_common.h"
  13. /*
  14. * PCI bus specific translator
  15. */
  16. static int of_bus_pci_match(struct device_node *np)
  17. {
  18. if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
  19. /* Do not do PCI specific frobbing if the
  20. * PCI bridge lacks a ranges property. We
  21. * want to pass it through up to the next
  22. * parent as-is, not with the PCI translate
  23. * method which chops off the top address cell.
  24. */
  25. if (!of_find_property(np, "ranges", NULL))
  26. return 0;
  27. return 1;
  28. }
  29. return 0;
  30. }
  31. static void of_bus_pci_count_cells(struct device_node *np,
  32. int *addrc, int *sizec)
  33. {
  34. if (addrc)
  35. *addrc = 3;
  36. if (sizec)
  37. *sizec = 2;
  38. }
  39. static int of_bus_pci_map(u32 *addr, const u32 *range,
  40. int na, int ns, int pna)
  41. {
  42. u32 result[OF_MAX_ADDR_CELLS];
  43. int i;
  44. /* Check address type match */
  45. if ((addr[0] ^ range[0]) & 0x03000000)
  46. return -EINVAL;
  47. if (of_out_of_range(addr + 1, range + 1, range + na + pna,
  48. na - 1, ns))
  49. return -EINVAL;
  50. /* Start with the parent range base. */
  51. memcpy(result, range + na, pna * 4);
  52. /* Add in the child address offset, skipping high cell. */
  53. for (i = 0; i < na - 1; i++)
  54. result[pna - 1 - i] +=
  55. (addr[na - 1 - i] -
  56. range[na - 1 - i]);
  57. memcpy(addr, result, pna * 4);
  58. return 0;
  59. }
  60. static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
  61. {
  62. u32 w = addr[0];
  63. /* For PCI, we override whatever child busses may have used. */
  64. flags = 0;
  65. switch((w >> 24) & 0x03) {
  66. case 0x01:
  67. flags |= IORESOURCE_IO;
  68. break;
  69. case 0x02: /* 32 bits */
  70. case 0x03: /* 64 bits */
  71. flags |= IORESOURCE_MEM;
  72. break;
  73. }
  74. if (w & 0x40000000)
  75. flags |= IORESOURCE_PREFETCH;
  76. return flags;
  77. }
  78. static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags)
  79. {
  80. return IORESOURCE_MEM;
  81. }
  82. /*
  83. * Array of bus specific translators
  84. */
  85. static struct of_bus of_busses[] = {
  86. /* PCI */
  87. {
  88. .name = "pci",
  89. .addr_prop_name = "assigned-addresses",
  90. .match = of_bus_pci_match,
  91. .count_cells = of_bus_pci_count_cells,
  92. .map = of_bus_pci_map,
  93. .get_flags = of_bus_pci_get_flags,
  94. },
  95. /* SBUS */
  96. {
  97. .name = "sbus",
  98. .addr_prop_name = "reg",
  99. .match = of_bus_sbus_match,
  100. .count_cells = of_bus_sbus_count_cells,
  101. .map = of_bus_default_map,
  102. .get_flags = of_bus_sbus_get_flags,
  103. },
  104. /* Default */
  105. {
  106. .name = "default",
  107. .addr_prop_name = "reg",
  108. .match = NULL,
  109. .count_cells = of_bus_default_count_cells,
  110. .map = of_bus_default_map,
  111. .get_flags = of_bus_default_get_flags,
  112. },
  113. };
  114. static struct of_bus *of_match_bus(struct device_node *np)
  115. {
  116. int i;
  117. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  118. if (!of_busses[i].match || of_busses[i].match(np))
  119. return &of_busses[i];
  120. BUG();
  121. return NULL;
  122. }
  123. static int __init build_one_resource(struct device_node *parent,
  124. struct of_bus *bus,
  125. struct of_bus *pbus,
  126. u32 *addr,
  127. int na, int ns, int pna)
  128. {
  129. const u32 *ranges;
  130. unsigned int rlen;
  131. int rone;
  132. ranges = of_get_property(parent, "ranges", &rlen);
  133. if (ranges == NULL || rlen == 0) {
  134. u32 result[OF_MAX_ADDR_CELLS];
  135. int i;
  136. memset(result, 0, pna * 4);
  137. for (i = 0; i < na; i++)
  138. result[pna - 1 - i] =
  139. addr[na - 1 - i];
  140. memcpy(addr, result, pna * 4);
  141. return 0;
  142. }
  143. /* Now walk through the ranges */
  144. rlen /= 4;
  145. rone = na + pna + ns;
  146. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  147. if (!bus->map(addr, ranges, na, ns, pna))
  148. return 0;
  149. }
  150. return 1;
  151. }
  152. static int __init use_1to1_mapping(struct device_node *pp)
  153. {
  154. /* If we have a ranges property in the parent, use it. */
  155. if (of_find_property(pp, "ranges", NULL) != NULL)
  156. return 0;
  157. /* Some SBUS devices use intermediate nodes to express
  158. * hierarchy within the device itself. These aren't
  159. * real bus nodes, and don't have a 'ranges' property.
  160. * But, we should still pass the translation work up
  161. * to the SBUS itself.
  162. */
  163. if (!strcmp(pp->name, "dma") ||
  164. !strcmp(pp->name, "espdma") ||
  165. !strcmp(pp->name, "ledma") ||
  166. !strcmp(pp->name, "lebuffer"))
  167. return 0;
  168. return 1;
  169. }
  170. static int of_resource_verbose;
  171. static void __init build_device_resources(struct of_device *op,
  172. struct device *parent)
  173. {
  174. struct of_device *p_op;
  175. struct of_bus *bus;
  176. int na, ns;
  177. int index, num_reg;
  178. const void *preg;
  179. if (!parent)
  180. return;
  181. p_op = to_of_device(parent);
  182. bus = of_match_bus(p_op->node);
  183. bus->count_cells(op->node, &na, &ns);
  184. preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
  185. if (!preg || num_reg == 0)
  186. return;
  187. /* Convert to num-cells. */
  188. num_reg /= 4;
  189. /* Conver to num-entries. */
  190. num_reg /= na + ns;
  191. for (index = 0; index < num_reg; index++) {
  192. struct resource *r = &op->resource[index];
  193. u32 addr[OF_MAX_ADDR_CELLS];
  194. const u32 *reg = (preg + (index * ((na + ns) * 4)));
  195. struct device_node *dp = op->node;
  196. struct device_node *pp = p_op->node;
  197. struct of_bus *pbus, *dbus;
  198. u64 size, result = OF_BAD_ADDR;
  199. unsigned long flags;
  200. int dna, dns;
  201. int pna, pns;
  202. size = of_read_addr(reg + na, ns);
  203. memcpy(addr, reg, na * 4);
  204. flags = bus->get_flags(reg, 0);
  205. if (use_1to1_mapping(pp)) {
  206. result = of_read_addr(addr, na);
  207. goto build_res;
  208. }
  209. dna = na;
  210. dns = ns;
  211. dbus = bus;
  212. while (1) {
  213. dp = pp;
  214. pp = dp->parent;
  215. if (!pp) {
  216. result = of_read_addr(addr, dna);
  217. break;
  218. }
  219. pbus = of_match_bus(pp);
  220. pbus->count_cells(dp, &pna, &pns);
  221. if (build_one_resource(dp, dbus, pbus, addr,
  222. dna, dns, pna))
  223. break;
  224. flags = pbus->get_flags(addr, flags);
  225. dna = pna;
  226. dns = pns;
  227. dbus = pbus;
  228. }
  229. build_res:
  230. memset(r, 0, sizeof(*r));
  231. if (of_resource_verbose)
  232. printk("%s reg[%d] -> %llx\n",
  233. op->node->full_name, index,
  234. result);
  235. if (result != OF_BAD_ADDR) {
  236. r->start = result & 0xffffffff;
  237. r->end = result + size - 1;
  238. r->flags = flags | ((result >> 32ULL) & 0xffUL);
  239. }
  240. r->name = op->node->name;
  241. }
  242. }
  243. static struct of_device * __init scan_one_device(struct device_node *dp,
  244. struct device *parent)
  245. {
  246. struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
  247. const struct linux_prom_irqs *intr;
  248. struct dev_archdata *sd;
  249. int len, i;
  250. if (!op)
  251. return NULL;
  252. sd = &op->dev.archdata;
  253. sd->prom_node = dp;
  254. sd->op = op;
  255. op->node = dp;
  256. op->clock_freq = of_getintprop_default(dp, "clock-frequency",
  257. (25*1000*1000));
  258. op->portid = of_getintprop_default(dp, "upa-portid", -1);
  259. if (op->portid == -1)
  260. op->portid = of_getintprop_default(dp, "portid", -1);
  261. intr = of_get_property(dp, "intr", &len);
  262. if (intr) {
  263. op->num_irqs = len / sizeof(struct linux_prom_irqs);
  264. for (i = 0; i < op->num_irqs; i++)
  265. op->irqs[i] = intr[i].pri;
  266. } else {
  267. const unsigned int *irq =
  268. of_get_property(dp, "interrupts", &len);
  269. if (irq) {
  270. op->num_irqs = len / sizeof(unsigned int);
  271. for (i = 0; i < op->num_irqs; i++)
  272. op->irqs[i] = irq[i];
  273. } else {
  274. op->num_irqs = 0;
  275. }
  276. }
  277. if (sparc_cpu_model == sun4d) {
  278. static int pil_to_sbus[] = {
  279. 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
  280. };
  281. struct device_node *io_unit, *sbi = dp->parent;
  282. const struct linux_prom_registers *regs;
  283. int board, slot;
  284. while (sbi) {
  285. if (!strcmp(sbi->name, "sbi"))
  286. break;
  287. sbi = sbi->parent;
  288. }
  289. if (!sbi)
  290. goto build_resources;
  291. regs = of_get_property(dp, "reg", NULL);
  292. if (!regs)
  293. goto build_resources;
  294. slot = regs->which_io;
  295. /* If SBI's parent is not io-unit or the io-unit lacks
  296. * a "board#" property, something is very wrong.
  297. */
  298. if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
  299. printk("%s: Error, parent is not io-unit.\n",
  300. sbi->full_name);
  301. goto build_resources;
  302. }
  303. io_unit = sbi->parent;
  304. board = of_getintprop_default(io_unit, "board#", -1);
  305. if (board == -1) {
  306. printk("%s: Error, lacks board# property.\n",
  307. io_unit->full_name);
  308. goto build_resources;
  309. }
  310. for (i = 0; i < op->num_irqs; i++) {
  311. int this_irq = op->irqs[i];
  312. int sbusl = pil_to_sbus[this_irq];
  313. if (sbusl)
  314. this_irq = (((board + 1) << 5) +
  315. (sbusl << 2) +
  316. slot);
  317. op->irqs[i] = this_irq;
  318. }
  319. }
  320. build_resources:
  321. build_device_resources(op, parent);
  322. op->dev.parent = parent;
  323. op->dev.bus = &of_platform_bus_type;
  324. if (!parent)
  325. dev_set_name(&op->dev, "root");
  326. else
  327. dev_set_name(&op->dev, "%08x", dp->node);
  328. if (of_device_register(op)) {
  329. printk("%s: Could not register of device.\n",
  330. dp->full_name);
  331. kfree(op);
  332. op = NULL;
  333. }
  334. return op;
  335. }
  336. static void __init scan_tree(struct device_node *dp, struct device *parent)
  337. {
  338. while (dp) {
  339. struct of_device *op = scan_one_device(dp, parent);
  340. if (op)
  341. scan_tree(dp->child, &op->dev);
  342. dp = dp->sibling;
  343. }
  344. }
  345. static void __init scan_of_devices(void)
  346. {
  347. struct device_node *root = of_find_node_by_path("/");
  348. struct of_device *parent;
  349. parent = scan_one_device(root, NULL);
  350. if (!parent)
  351. return;
  352. scan_tree(root->child, &parent->dev);
  353. }
  354. static int __init of_bus_driver_init(void)
  355. {
  356. int err;
  357. err = of_bus_type_init(&of_platform_bus_type, "of");
  358. if (!err)
  359. scan_of_devices();
  360. return err;
  361. }
  362. postcore_initcall(of_bus_driver_init);
  363. static int __init of_debug(char *str)
  364. {
  365. int val = 0;
  366. get_option(&str, &val);
  367. if (val & 1)
  368. of_resource_verbose = 1;
  369. return 1;
  370. }
  371. __setup("of_debug=", of_debug);