prom.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc64 by David S. Miller davem@davemloft.net
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/module.h>
  23. #include <asm/prom.h>
  24. #include <asm/of_device.h>
  25. #include <asm/oplib.h>
  26. #include <asm/irq.h>
  27. #include <asm/asi.h>
  28. #include <asm/upa.h>
  29. static struct device_node *allnodes;
  30. /* use when traversing tree through the allnext, child, sibling,
  31. * or parent members of struct device_node.
  32. */
  33. static DEFINE_RWLOCK(devtree_lock);
  34. int of_device_is_compatible(const struct device_node *device,
  35. const char *compat)
  36. {
  37. const char* cp;
  38. int cplen, l;
  39. cp = of_get_property(device, "compatible", &cplen);
  40. if (cp == NULL)
  41. return 0;
  42. while (cplen > 0) {
  43. if (strncmp(cp, compat, strlen(compat)) == 0)
  44. return 1;
  45. l = strlen(cp) + 1;
  46. cp += l;
  47. cplen -= l;
  48. }
  49. return 0;
  50. }
  51. EXPORT_SYMBOL(of_device_is_compatible);
  52. struct device_node *of_get_parent(const struct device_node *node)
  53. {
  54. struct device_node *np;
  55. if (!node)
  56. return NULL;
  57. np = node->parent;
  58. return np;
  59. }
  60. EXPORT_SYMBOL(of_get_parent);
  61. struct device_node *of_get_next_child(const struct device_node *node,
  62. struct device_node *prev)
  63. {
  64. struct device_node *next;
  65. next = prev ? prev->sibling : node->child;
  66. for (; next != 0; next = next->sibling) {
  67. break;
  68. }
  69. return next;
  70. }
  71. EXPORT_SYMBOL(of_get_next_child);
  72. struct device_node *of_find_node_by_path(const char *path)
  73. {
  74. struct device_node *np = allnodes;
  75. for (; np != 0; np = np->allnext) {
  76. if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
  77. break;
  78. }
  79. return np;
  80. }
  81. EXPORT_SYMBOL(of_find_node_by_path);
  82. struct device_node *of_find_node_by_phandle(phandle handle)
  83. {
  84. struct device_node *np;
  85. for (np = allnodes; np != 0; np = np->allnext)
  86. if (np->node == handle)
  87. break;
  88. return np;
  89. }
  90. EXPORT_SYMBOL(of_find_node_by_phandle);
  91. struct device_node *of_find_node_by_name(struct device_node *from,
  92. const char *name)
  93. {
  94. struct device_node *np;
  95. np = from ? from->allnext : allnodes;
  96. for (; np != NULL; np = np->allnext)
  97. if (np->name != NULL && strcmp(np->name, name) == 0)
  98. break;
  99. return np;
  100. }
  101. EXPORT_SYMBOL(of_find_node_by_name);
  102. struct device_node *of_find_node_by_type(struct device_node *from,
  103. const char *type)
  104. {
  105. struct device_node *np;
  106. np = from ? from->allnext : allnodes;
  107. for (; np != 0; np = np->allnext)
  108. if (np->type != 0 && strcmp(np->type, type) == 0)
  109. break;
  110. return np;
  111. }
  112. EXPORT_SYMBOL(of_find_node_by_type);
  113. struct device_node *of_find_compatible_node(struct device_node *from,
  114. const char *type, const char *compatible)
  115. {
  116. struct device_node *np;
  117. np = from ? from->allnext : allnodes;
  118. for (; np != 0; np = np->allnext) {
  119. if (type != NULL
  120. && !(np->type != 0 && strcmp(np->type, type) == 0))
  121. continue;
  122. if (of_device_is_compatible(np, compatible))
  123. break;
  124. }
  125. return np;
  126. }
  127. EXPORT_SYMBOL(of_find_compatible_node);
  128. struct property *of_find_property(const struct device_node *np,
  129. const char *name,
  130. int *lenp)
  131. {
  132. struct property *pp;
  133. for (pp = np->properties; pp != 0; pp = pp->next) {
  134. if (strcasecmp(pp->name, name) == 0) {
  135. if (lenp != 0)
  136. *lenp = pp->length;
  137. break;
  138. }
  139. }
  140. return pp;
  141. }
  142. EXPORT_SYMBOL(of_find_property);
  143. /*
  144. * Find a property with a given name for a given node
  145. * and return the value.
  146. */
  147. const void *of_get_property(const struct device_node *np, const char *name,
  148. int *lenp)
  149. {
  150. struct property *pp = of_find_property(np,name,lenp);
  151. return pp ? pp->value : NULL;
  152. }
  153. EXPORT_SYMBOL(of_get_property);
  154. int of_getintprop_default(struct device_node *np, const char *name, int def)
  155. {
  156. struct property *prop;
  157. int len;
  158. prop = of_find_property(np, name, &len);
  159. if (!prop || len != 4)
  160. return def;
  161. return *(int *) prop->value;
  162. }
  163. EXPORT_SYMBOL(of_getintprop_default);
  164. int of_n_addr_cells(struct device_node *np)
  165. {
  166. const int* ip;
  167. do {
  168. if (np->parent)
  169. np = np->parent;
  170. ip = of_get_property(np, "#address-cells", NULL);
  171. if (ip != NULL)
  172. return *ip;
  173. } while (np->parent);
  174. /* No #address-cells property for the root node, default to 2 */
  175. return 2;
  176. }
  177. EXPORT_SYMBOL(of_n_addr_cells);
  178. int of_n_size_cells(struct device_node *np)
  179. {
  180. const int* ip;
  181. do {
  182. if (np->parent)
  183. np = np->parent;
  184. ip = of_get_property(np, "#size-cells", NULL);
  185. if (ip != NULL)
  186. return *ip;
  187. } while (np->parent);
  188. /* No #size-cells property for the root node, default to 1 */
  189. return 1;
  190. }
  191. EXPORT_SYMBOL(of_n_size_cells);
  192. int of_set_property(struct device_node *dp, const char *name, void *val, int len)
  193. {
  194. struct property **prevp;
  195. void *new_val;
  196. int err;
  197. new_val = kmalloc(len, GFP_KERNEL);
  198. if (!new_val)
  199. return -ENOMEM;
  200. memcpy(new_val, val, len);
  201. err = -ENODEV;
  202. write_lock(&devtree_lock);
  203. prevp = &dp->properties;
  204. while (*prevp) {
  205. struct property *prop = *prevp;
  206. if (!strcasecmp(prop->name, name)) {
  207. void *old_val = prop->value;
  208. int ret;
  209. ret = prom_setprop(dp->node, name, val, len);
  210. err = -EINVAL;
  211. if (ret >= 0) {
  212. prop->value = new_val;
  213. prop->length = len;
  214. if (OF_IS_DYNAMIC(prop))
  215. kfree(old_val);
  216. OF_MARK_DYNAMIC(prop);
  217. err = 0;
  218. }
  219. break;
  220. }
  221. prevp = &(*prevp)->next;
  222. }
  223. write_unlock(&devtree_lock);
  224. /* XXX Upate procfs if necessary... */
  225. return err;
  226. }
  227. EXPORT_SYMBOL(of_set_property);
  228. static unsigned int prom_early_allocated;
  229. static void * __init prom_early_alloc(unsigned long size)
  230. {
  231. void *ret;
  232. ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
  233. if (ret != NULL)
  234. memset(ret, 0, size);
  235. prom_early_allocated += size;
  236. return ret;
  237. }
  238. #ifdef CONFIG_PCI
  239. /* PSYCHO interrupt mapping support. */
  240. #define PSYCHO_IMAP_A_SLOT0 0x0c00UL
  241. #define PSYCHO_IMAP_B_SLOT0 0x0c20UL
  242. static unsigned long psycho_pcislot_imap_offset(unsigned long ino)
  243. {
  244. unsigned int bus = (ino & 0x10) >> 4;
  245. unsigned int slot = (ino & 0x0c) >> 2;
  246. if (bus == 0)
  247. return PSYCHO_IMAP_A_SLOT0 + (slot * 8);
  248. else
  249. return PSYCHO_IMAP_B_SLOT0 + (slot * 8);
  250. }
  251. #define PSYCHO_IMAP_SCSI 0x1000UL
  252. #define PSYCHO_IMAP_ETH 0x1008UL
  253. #define PSYCHO_IMAP_BPP 0x1010UL
  254. #define PSYCHO_IMAP_AU_REC 0x1018UL
  255. #define PSYCHO_IMAP_AU_PLAY 0x1020UL
  256. #define PSYCHO_IMAP_PFAIL 0x1028UL
  257. #define PSYCHO_IMAP_KMS 0x1030UL
  258. #define PSYCHO_IMAP_FLPY 0x1038UL
  259. #define PSYCHO_IMAP_SHW 0x1040UL
  260. #define PSYCHO_IMAP_KBD 0x1048UL
  261. #define PSYCHO_IMAP_MS 0x1050UL
  262. #define PSYCHO_IMAP_SER 0x1058UL
  263. #define PSYCHO_IMAP_TIM0 0x1060UL
  264. #define PSYCHO_IMAP_TIM1 0x1068UL
  265. #define PSYCHO_IMAP_UE 0x1070UL
  266. #define PSYCHO_IMAP_CE 0x1078UL
  267. #define PSYCHO_IMAP_A_ERR 0x1080UL
  268. #define PSYCHO_IMAP_B_ERR 0x1088UL
  269. #define PSYCHO_IMAP_PMGMT 0x1090UL
  270. #define PSYCHO_IMAP_GFX 0x1098UL
  271. #define PSYCHO_IMAP_EUPA 0x10a0UL
  272. static unsigned long __psycho_onboard_imap_off[] = {
  273. /*0x20*/ PSYCHO_IMAP_SCSI,
  274. /*0x21*/ PSYCHO_IMAP_ETH,
  275. /*0x22*/ PSYCHO_IMAP_BPP,
  276. /*0x23*/ PSYCHO_IMAP_AU_REC,
  277. /*0x24*/ PSYCHO_IMAP_AU_PLAY,
  278. /*0x25*/ PSYCHO_IMAP_PFAIL,
  279. /*0x26*/ PSYCHO_IMAP_KMS,
  280. /*0x27*/ PSYCHO_IMAP_FLPY,
  281. /*0x28*/ PSYCHO_IMAP_SHW,
  282. /*0x29*/ PSYCHO_IMAP_KBD,
  283. /*0x2a*/ PSYCHO_IMAP_MS,
  284. /*0x2b*/ PSYCHO_IMAP_SER,
  285. /*0x2c*/ PSYCHO_IMAP_TIM0,
  286. /*0x2d*/ PSYCHO_IMAP_TIM1,
  287. /*0x2e*/ PSYCHO_IMAP_UE,
  288. /*0x2f*/ PSYCHO_IMAP_CE,
  289. /*0x30*/ PSYCHO_IMAP_A_ERR,
  290. /*0x31*/ PSYCHO_IMAP_B_ERR,
  291. /*0x32*/ PSYCHO_IMAP_PMGMT,
  292. /*0x33*/ PSYCHO_IMAP_GFX,
  293. /*0x34*/ PSYCHO_IMAP_EUPA,
  294. };
  295. #define PSYCHO_ONBOARD_IRQ_BASE 0x20
  296. #define PSYCHO_ONBOARD_IRQ_LAST 0x34
  297. #define psycho_onboard_imap_offset(__ino) \
  298. __psycho_onboard_imap_off[(__ino) - PSYCHO_ONBOARD_IRQ_BASE]
  299. #define PSYCHO_ICLR_A_SLOT0 0x1400UL
  300. #define PSYCHO_ICLR_SCSI 0x1800UL
  301. #define psycho_iclr_offset(ino) \
  302. ((ino & 0x20) ? (PSYCHO_ICLR_SCSI + (((ino) & 0x1f) << 3)) : \
  303. (PSYCHO_ICLR_A_SLOT0 + (((ino) & 0x1f)<<3)))
  304. static unsigned int psycho_irq_build(struct device_node *dp,
  305. unsigned int ino,
  306. void *_data)
  307. {
  308. unsigned long controller_regs = (unsigned long) _data;
  309. unsigned long imap, iclr;
  310. unsigned long imap_off, iclr_off;
  311. int inofixup = 0;
  312. ino &= 0x3f;
  313. if (ino < PSYCHO_ONBOARD_IRQ_BASE) {
  314. /* PCI slot */
  315. imap_off = psycho_pcislot_imap_offset(ino);
  316. } else {
  317. /* Onboard device */
  318. if (ino > PSYCHO_ONBOARD_IRQ_LAST) {
  319. prom_printf("psycho_irq_build: Wacky INO [%x]\n", ino);
  320. prom_halt();
  321. }
  322. imap_off = psycho_onboard_imap_offset(ino);
  323. }
  324. /* Now build the IRQ bucket. */
  325. imap = controller_regs + imap_off;
  326. iclr_off = psycho_iclr_offset(ino);
  327. iclr = controller_regs + iclr_off;
  328. if ((ino & 0x20) == 0)
  329. inofixup = ino & 0x03;
  330. return build_irq(inofixup, iclr, imap);
  331. }
  332. static void psycho_irq_trans_init(struct device_node *dp)
  333. {
  334. const struct linux_prom64_registers *regs;
  335. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  336. dp->irq_trans->irq_build = psycho_irq_build;
  337. regs = of_get_property(dp, "reg", NULL);
  338. dp->irq_trans->data = (void *) regs[2].phys_addr;
  339. }
  340. #define sabre_read(__reg) \
  341. ({ u64 __ret; \
  342. __asm__ __volatile__("ldxa [%1] %2, %0" \
  343. : "=r" (__ret) \
  344. : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
  345. : "memory"); \
  346. __ret; \
  347. })
  348. struct sabre_irq_data {
  349. unsigned long controller_regs;
  350. unsigned int pci_first_busno;
  351. };
  352. #define SABRE_CONFIGSPACE 0x001000000UL
  353. #define SABRE_WRSYNC 0x1c20UL
  354. #define SABRE_CONFIG_BASE(CONFIG_SPACE) \
  355. (CONFIG_SPACE | (1UL << 24))
  356. #define SABRE_CONFIG_ENCODE(BUS, DEVFN, REG) \
  357. (((unsigned long)(BUS) << 16) | \
  358. ((unsigned long)(DEVFN) << 8) | \
  359. ((unsigned long)(REG)))
  360. /* When a device lives behind a bridge deeper in the PCI bus topology
  361. * than APB, a special sequence must run to make sure all pending DMA
  362. * transfers at the time of IRQ delivery are visible in the coherency
  363. * domain by the cpu. This sequence is to perform a read on the far
  364. * side of the non-APB bridge, then perform a read of Sabre's DMA
  365. * write-sync register.
  366. */
  367. static void sabre_wsync_handler(unsigned int ino, void *_arg1, void *_arg2)
  368. {
  369. unsigned int phys_hi = (unsigned int) (unsigned long) _arg1;
  370. struct sabre_irq_data *irq_data = _arg2;
  371. unsigned long controller_regs = irq_data->controller_regs;
  372. unsigned long sync_reg = controller_regs + SABRE_WRSYNC;
  373. unsigned long config_space = controller_regs + SABRE_CONFIGSPACE;
  374. unsigned int bus, devfn;
  375. u16 _unused;
  376. config_space = SABRE_CONFIG_BASE(config_space);
  377. bus = (phys_hi >> 16) & 0xff;
  378. devfn = (phys_hi >> 8) & 0xff;
  379. config_space |= SABRE_CONFIG_ENCODE(bus, devfn, 0x00);
  380. __asm__ __volatile__("membar #Sync\n\t"
  381. "lduha [%1] %2, %0\n\t"
  382. "membar #Sync"
  383. : "=r" (_unused)
  384. : "r" ((u16 *) config_space),
  385. "i" (ASI_PHYS_BYPASS_EC_E_L)
  386. : "memory");
  387. sabre_read(sync_reg);
  388. }
  389. #define SABRE_IMAP_A_SLOT0 0x0c00UL
  390. #define SABRE_IMAP_B_SLOT0 0x0c20UL
  391. #define SABRE_IMAP_SCSI 0x1000UL
  392. #define SABRE_IMAP_ETH 0x1008UL
  393. #define SABRE_IMAP_BPP 0x1010UL
  394. #define SABRE_IMAP_AU_REC 0x1018UL
  395. #define SABRE_IMAP_AU_PLAY 0x1020UL
  396. #define SABRE_IMAP_PFAIL 0x1028UL
  397. #define SABRE_IMAP_KMS 0x1030UL
  398. #define SABRE_IMAP_FLPY 0x1038UL
  399. #define SABRE_IMAP_SHW 0x1040UL
  400. #define SABRE_IMAP_KBD 0x1048UL
  401. #define SABRE_IMAP_MS 0x1050UL
  402. #define SABRE_IMAP_SER 0x1058UL
  403. #define SABRE_IMAP_UE 0x1070UL
  404. #define SABRE_IMAP_CE 0x1078UL
  405. #define SABRE_IMAP_PCIERR 0x1080UL
  406. #define SABRE_IMAP_GFX 0x1098UL
  407. #define SABRE_IMAP_EUPA 0x10a0UL
  408. #define SABRE_ICLR_A_SLOT0 0x1400UL
  409. #define SABRE_ICLR_B_SLOT0 0x1480UL
  410. #define SABRE_ICLR_SCSI 0x1800UL
  411. #define SABRE_ICLR_ETH 0x1808UL
  412. #define SABRE_ICLR_BPP 0x1810UL
  413. #define SABRE_ICLR_AU_REC 0x1818UL
  414. #define SABRE_ICLR_AU_PLAY 0x1820UL
  415. #define SABRE_ICLR_PFAIL 0x1828UL
  416. #define SABRE_ICLR_KMS 0x1830UL
  417. #define SABRE_ICLR_FLPY 0x1838UL
  418. #define SABRE_ICLR_SHW 0x1840UL
  419. #define SABRE_ICLR_KBD 0x1848UL
  420. #define SABRE_ICLR_MS 0x1850UL
  421. #define SABRE_ICLR_SER 0x1858UL
  422. #define SABRE_ICLR_UE 0x1870UL
  423. #define SABRE_ICLR_CE 0x1878UL
  424. #define SABRE_ICLR_PCIERR 0x1880UL
  425. static unsigned long sabre_pcislot_imap_offset(unsigned long ino)
  426. {
  427. unsigned int bus = (ino & 0x10) >> 4;
  428. unsigned int slot = (ino & 0x0c) >> 2;
  429. if (bus == 0)
  430. return SABRE_IMAP_A_SLOT0 + (slot * 8);
  431. else
  432. return SABRE_IMAP_B_SLOT0 + (slot * 8);
  433. }
  434. static unsigned long __sabre_onboard_imap_off[] = {
  435. /*0x20*/ SABRE_IMAP_SCSI,
  436. /*0x21*/ SABRE_IMAP_ETH,
  437. /*0x22*/ SABRE_IMAP_BPP,
  438. /*0x23*/ SABRE_IMAP_AU_REC,
  439. /*0x24*/ SABRE_IMAP_AU_PLAY,
  440. /*0x25*/ SABRE_IMAP_PFAIL,
  441. /*0x26*/ SABRE_IMAP_KMS,
  442. /*0x27*/ SABRE_IMAP_FLPY,
  443. /*0x28*/ SABRE_IMAP_SHW,
  444. /*0x29*/ SABRE_IMAP_KBD,
  445. /*0x2a*/ SABRE_IMAP_MS,
  446. /*0x2b*/ SABRE_IMAP_SER,
  447. /*0x2c*/ 0 /* reserved */,
  448. /*0x2d*/ 0 /* reserved */,
  449. /*0x2e*/ SABRE_IMAP_UE,
  450. /*0x2f*/ SABRE_IMAP_CE,
  451. /*0x30*/ SABRE_IMAP_PCIERR,
  452. /*0x31*/ 0 /* reserved */,
  453. /*0x32*/ 0 /* reserved */,
  454. /*0x33*/ SABRE_IMAP_GFX,
  455. /*0x34*/ SABRE_IMAP_EUPA,
  456. };
  457. #define SABRE_ONBOARD_IRQ_BASE 0x20
  458. #define SABRE_ONBOARD_IRQ_LAST 0x30
  459. #define sabre_onboard_imap_offset(__ino) \
  460. __sabre_onboard_imap_off[(__ino) - SABRE_ONBOARD_IRQ_BASE]
  461. #define sabre_iclr_offset(ino) \
  462. ((ino & 0x20) ? (SABRE_ICLR_SCSI + (((ino) & 0x1f) << 3)) : \
  463. (SABRE_ICLR_A_SLOT0 + (((ino) & 0x1f)<<3)))
  464. static int sabre_device_needs_wsync(struct device_node *dp)
  465. {
  466. struct device_node *parent = dp->parent;
  467. const char *parent_model, *parent_compat;
  468. /* This traversal up towards the root is meant to
  469. * handle two cases:
  470. *
  471. * 1) non-PCI bus sitting under PCI, such as 'ebus'
  472. * 2) the PCI controller interrupts themselves, which
  473. * will use the sabre_irq_build but do not need
  474. * the DMA synchronization handling
  475. */
  476. while (parent) {
  477. if (!strcmp(parent->type, "pci"))
  478. break;
  479. parent = parent->parent;
  480. }
  481. if (!parent)
  482. return 0;
  483. parent_model = of_get_property(parent,
  484. "model", NULL);
  485. if (parent_model &&
  486. (!strcmp(parent_model, "SUNW,sabre") ||
  487. !strcmp(parent_model, "SUNW,simba")))
  488. return 0;
  489. parent_compat = of_get_property(parent,
  490. "compatible", NULL);
  491. if (parent_compat &&
  492. (!strcmp(parent_compat, "pci108e,a000") ||
  493. !strcmp(parent_compat, "pci108e,a001")))
  494. return 0;
  495. return 1;
  496. }
  497. static unsigned int sabre_irq_build(struct device_node *dp,
  498. unsigned int ino,
  499. void *_data)
  500. {
  501. struct sabre_irq_data *irq_data = _data;
  502. unsigned long controller_regs = irq_data->controller_regs;
  503. const struct linux_prom_pci_registers *regs;
  504. unsigned long imap, iclr;
  505. unsigned long imap_off, iclr_off;
  506. int inofixup = 0;
  507. int virt_irq;
  508. ino &= 0x3f;
  509. if (ino < SABRE_ONBOARD_IRQ_BASE) {
  510. /* PCI slot */
  511. imap_off = sabre_pcislot_imap_offset(ino);
  512. } else {
  513. /* onboard device */
  514. if (ino > SABRE_ONBOARD_IRQ_LAST) {
  515. prom_printf("sabre_irq_build: Wacky INO [%x]\n", ino);
  516. prom_halt();
  517. }
  518. imap_off = sabre_onboard_imap_offset(ino);
  519. }
  520. /* Now build the IRQ bucket. */
  521. imap = controller_regs + imap_off;
  522. iclr_off = sabre_iclr_offset(ino);
  523. iclr = controller_regs + iclr_off;
  524. if ((ino & 0x20) == 0)
  525. inofixup = ino & 0x03;
  526. virt_irq = build_irq(inofixup, iclr, imap);
  527. /* If the parent device is a PCI<->PCI bridge other than
  528. * APB, we have to install a pre-handler to ensure that
  529. * all pending DMA is drained before the interrupt handler
  530. * is run.
  531. */
  532. regs = of_get_property(dp, "reg", NULL);
  533. if (regs && sabre_device_needs_wsync(dp)) {
  534. irq_install_pre_handler(virt_irq,
  535. sabre_wsync_handler,
  536. (void *) (long) regs->phys_hi,
  537. (void *) irq_data);
  538. }
  539. return virt_irq;
  540. }
  541. static void sabre_irq_trans_init(struct device_node *dp)
  542. {
  543. const struct linux_prom64_registers *regs;
  544. struct sabre_irq_data *irq_data;
  545. const u32 *busrange;
  546. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  547. dp->irq_trans->irq_build = sabre_irq_build;
  548. irq_data = prom_early_alloc(sizeof(struct sabre_irq_data));
  549. regs = of_get_property(dp, "reg", NULL);
  550. irq_data->controller_regs = regs[0].phys_addr;
  551. busrange = of_get_property(dp, "bus-range", NULL);
  552. irq_data->pci_first_busno = busrange[0];
  553. dp->irq_trans->data = irq_data;
  554. }
  555. /* SCHIZO interrupt mapping support. Unlike Psycho, for this controller the
  556. * imap/iclr registers are per-PBM.
  557. */
  558. #define SCHIZO_IMAP_BASE 0x1000UL
  559. #define SCHIZO_ICLR_BASE 0x1400UL
  560. static unsigned long schizo_imap_offset(unsigned long ino)
  561. {
  562. return SCHIZO_IMAP_BASE + (ino * 8UL);
  563. }
  564. static unsigned long schizo_iclr_offset(unsigned long ino)
  565. {
  566. return SCHIZO_ICLR_BASE + (ino * 8UL);
  567. }
  568. static unsigned long schizo_ino_to_iclr(unsigned long pbm_regs,
  569. unsigned int ino)
  570. {
  571. return pbm_regs + schizo_iclr_offset(ino);
  572. }
  573. static unsigned long schizo_ino_to_imap(unsigned long pbm_regs,
  574. unsigned int ino)
  575. {
  576. return pbm_regs + schizo_imap_offset(ino);
  577. }
  578. #define schizo_read(__reg) \
  579. ({ u64 __ret; \
  580. __asm__ __volatile__("ldxa [%1] %2, %0" \
  581. : "=r" (__ret) \
  582. : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
  583. : "memory"); \
  584. __ret; \
  585. })
  586. #define schizo_write(__reg, __val) \
  587. __asm__ __volatile__("stxa %0, [%1] %2" \
  588. : /* no outputs */ \
  589. : "r" (__val), "r" (__reg), \
  590. "i" (ASI_PHYS_BYPASS_EC_E) \
  591. : "memory")
  592. static void tomatillo_wsync_handler(unsigned int ino, void *_arg1, void *_arg2)
  593. {
  594. unsigned long sync_reg = (unsigned long) _arg2;
  595. u64 mask = 1UL << (ino & IMAP_INO);
  596. u64 val;
  597. int limit;
  598. schizo_write(sync_reg, mask);
  599. limit = 100000;
  600. val = 0;
  601. while (--limit) {
  602. val = schizo_read(sync_reg);
  603. if (!(val & mask))
  604. break;
  605. }
  606. if (limit <= 0) {
  607. printk("tomatillo_wsync_handler: DMA won't sync [%lx:%lx]\n",
  608. val, mask);
  609. }
  610. if (_arg1) {
  611. static unsigned char cacheline[64]
  612. __attribute__ ((aligned (64)));
  613. __asm__ __volatile__("rd %%fprs, %0\n\t"
  614. "or %0, %4, %1\n\t"
  615. "wr %1, 0x0, %%fprs\n\t"
  616. "stda %%f0, [%5] %6\n\t"
  617. "wr %0, 0x0, %%fprs\n\t"
  618. "membar #Sync"
  619. : "=&r" (mask), "=&r" (val)
  620. : "0" (mask), "1" (val),
  621. "i" (FPRS_FEF), "r" (&cacheline[0]),
  622. "i" (ASI_BLK_COMMIT_P));
  623. }
  624. }
  625. struct schizo_irq_data {
  626. unsigned long pbm_regs;
  627. unsigned long sync_reg;
  628. u32 portid;
  629. int chip_version;
  630. };
  631. static unsigned int schizo_irq_build(struct device_node *dp,
  632. unsigned int ino,
  633. void *_data)
  634. {
  635. struct schizo_irq_data *irq_data = _data;
  636. unsigned long pbm_regs = irq_data->pbm_regs;
  637. unsigned long imap, iclr;
  638. int ign_fixup;
  639. int virt_irq;
  640. int is_tomatillo;
  641. ino &= 0x3f;
  642. /* Now build the IRQ bucket. */
  643. imap = schizo_ino_to_imap(pbm_regs, ino);
  644. iclr = schizo_ino_to_iclr(pbm_regs, ino);
  645. /* On Schizo, no inofixup occurs. This is because each
  646. * INO has it's own IMAP register. On Psycho and Sabre
  647. * there is only one IMAP register for each PCI slot even
  648. * though four different INOs can be generated by each
  649. * PCI slot.
  650. *
  651. * But, for JBUS variants (essentially, Tomatillo), we have
  652. * to fixup the lowest bit of the interrupt group number.
  653. */
  654. ign_fixup = 0;
  655. is_tomatillo = (irq_data->sync_reg != 0UL);
  656. if (is_tomatillo) {
  657. if (irq_data->portid & 1)
  658. ign_fixup = (1 << 6);
  659. }
  660. virt_irq = build_irq(ign_fixup, iclr, imap);
  661. if (is_tomatillo) {
  662. irq_install_pre_handler(virt_irq,
  663. tomatillo_wsync_handler,
  664. ((irq_data->chip_version <= 4) ?
  665. (void *) 1 : (void *) 0),
  666. (void *) irq_data->sync_reg);
  667. }
  668. return virt_irq;
  669. }
  670. static void __schizo_irq_trans_init(struct device_node *dp, int is_tomatillo)
  671. {
  672. const struct linux_prom64_registers *regs;
  673. struct schizo_irq_data *irq_data;
  674. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  675. dp->irq_trans->irq_build = schizo_irq_build;
  676. irq_data = prom_early_alloc(sizeof(struct schizo_irq_data));
  677. regs = of_get_property(dp, "reg", NULL);
  678. dp->irq_trans->data = irq_data;
  679. irq_data->pbm_regs = regs[0].phys_addr;
  680. if (is_tomatillo)
  681. irq_data->sync_reg = regs[3].phys_addr + 0x1a18UL;
  682. else
  683. irq_data->sync_reg = 0UL;
  684. irq_data->portid = of_getintprop_default(dp, "portid", 0);
  685. irq_data->chip_version = of_getintprop_default(dp, "version#", 0);
  686. }
  687. static void schizo_irq_trans_init(struct device_node *dp)
  688. {
  689. __schizo_irq_trans_init(dp, 0);
  690. }
  691. static void tomatillo_irq_trans_init(struct device_node *dp)
  692. {
  693. __schizo_irq_trans_init(dp, 1);
  694. }
  695. static unsigned int pci_sun4v_irq_build(struct device_node *dp,
  696. unsigned int devino,
  697. void *_data)
  698. {
  699. u32 devhandle = (u32) (unsigned long) _data;
  700. return sun4v_build_irq(devhandle, devino);
  701. }
  702. static void pci_sun4v_irq_trans_init(struct device_node *dp)
  703. {
  704. const struct linux_prom64_registers *regs;
  705. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  706. dp->irq_trans->irq_build = pci_sun4v_irq_build;
  707. regs = of_get_property(dp, "reg", NULL);
  708. dp->irq_trans->data = (void *) (unsigned long)
  709. ((regs->phys_addr >> 32UL) & 0x0fffffff);
  710. }
  711. struct fire_irq_data {
  712. unsigned long pbm_regs;
  713. u32 portid;
  714. };
  715. #define FIRE_IMAP_BASE 0x001000
  716. #define FIRE_ICLR_BASE 0x001400
  717. static unsigned long fire_imap_offset(unsigned long ino)
  718. {
  719. return FIRE_IMAP_BASE + (ino * 8UL);
  720. }
  721. static unsigned long fire_iclr_offset(unsigned long ino)
  722. {
  723. return FIRE_ICLR_BASE + (ino * 8UL);
  724. }
  725. static unsigned long fire_ino_to_iclr(unsigned long pbm_regs,
  726. unsigned int ino)
  727. {
  728. return pbm_regs + fire_iclr_offset(ino);
  729. }
  730. static unsigned long fire_ino_to_imap(unsigned long pbm_regs,
  731. unsigned int ino)
  732. {
  733. return pbm_regs + fire_imap_offset(ino);
  734. }
  735. static unsigned int fire_irq_build(struct device_node *dp,
  736. unsigned int ino,
  737. void *_data)
  738. {
  739. struct fire_irq_data *irq_data = _data;
  740. unsigned long pbm_regs = irq_data->pbm_regs;
  741. unsigned long imap, iclr;
  742. unsigned long int_ctrlr;
  743. ino &= 0x3f;
  744. /* Now build the IRQ bucket. */
  745. imap = fire_ino_to_imap(pbm_regs, ino);
  746. iclr = fire_ino_to_iclr(pbm_regs, ino);
  747. /* Set the interrupt controller number. */
  748. int_ctrlr = 1 << 6;
  749. upa_writeq(int_ctrlr, imap);
  750. /* The interrupt map registers do not have an INO field
  751. * like other chips do. They return zero in the INO
  752. * field, and the interrupt controller number is controlled
  753. * in bits 6 thru 9. So in order for build_irq() to get
  754. * the INO right we pass it in as part of the fixup
  755. * which will get added to the map register zero value
  756. * read by build_irq().
  757. */
  758. ino |= (irq_data->portid << 6);
  759. ino -= int_ctrlr;
  760. return build_irq(ino, iclr, imap);
  761. }
  762. static void fire_irq_trans_init(struct device_node *dp)
  763. {
  764. const struct linux_prom64_registers *regs;
  765. struct fire_irq_data *irq_data;
  766. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  767. dp->irq_trans->irq_build = fire_irq_build;
  768. irq_data = prom_early_alloc(sizeof(struct fire_irq_data));
  769. regs = of_get_property(dp, "reg", NULL);
  770. dp->irq_trans->data = irq_data;
  771. irq_data->pbm_regs = regs[0].phys_addr;
  772. irq_data->portid = of_getintprop_default(dp, "portid", 0);
  773. }
  774. #endif /* CONFIG_PCI */
  775. #ifdef CONFIG_SBUS
  776. /* INO number to IMAP register offset for SYSIO external IRQ's.
  777. * This should conform to both Sunfire/Wildfire server and Fusion
  778. * desktop designs.
  779. */
  780. #define SYSIO_IMAP_SLOT0 0x2c04UL
  781. #define SYSIO_IMAP_SLOT1 0x2c0cUL
  782. #define SYSIO_IMAP_SLOT2 0x2c14UL
  783. #define SYSIO_IMAP_SLOT3 0x2c1cUL
  784. #define SYSIO_IMAP_SCSI 0x3004UL
  785. #define SYSIO_IMAP_ETH 0x300cUL
  786. #define SYSIO_IMAP_BPP 0x3014UL
  787. #define SYSIO_IMAP_AUDIO 0x301cUL
  788. #define SYSIO_IMAP_PFAIL 0x3024UL
  789. #define SYSIO_IMAP_KMS 0x302cUL
  790. #define SYSIO_IMAP_FLPY 0x3034UL
  791. #define SYSIO_IMAP_SHW 0x303cUL
  792. #define SYSIO_IMAP_KBD 0x3044UL
  793. #define SYSIO_IMAP_MS 0x304cUL
  794. #define SYSIO_IMAP_SER 0x3054UL
  795. #define SYSIO_IMAP_TIM0 0x3064UL
  796. #define SYSIO_IMAP_TIM1 0x306cUL
  797. #define SYSIO_IMAP_UE 0x3074UL
  798. #define SYSIO_IMAP_CE 0x307cUL
  799. #define SYSIO_IMAP_SBERR 0x3084UL
  800. #define SYSIO_IMAP_PMGMT 0x308cUL
  801. #define SYSIO_IMAP_GFX 0x3094UL
  802. #define SYSIO_IMAP_EUPA 0x309cUL
  803. #define bogon ((unsigned long) -1)
  804. static unsigned long sysio_irq_offsets[] = {
  805. /* SBUS Slot 0 --> 3, level 1 --> 7 */
  806. SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
  807. SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
  808. SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
  809. SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
  810. SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
  811. SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
  812. SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
  813. SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
  814. /* Onboard devices (not relevant/used on SunFire). */
  815. SYSIO_IMAP_SCSI,
  816. SYSIO_IMAP_ETH,
  817. SYSIO_IMAP_BPP,
  818. bogon,
  819. SYSIO_IMAP_AUDIO,
  820. SYSIO_IMAP_PFAIL,
  821. bogon,
  822. bogon,
  823. SYSIO_IMAP_KMS,
  824. SYSIO_IMAP_FLPY,
  825. SYSIO_IMAP_SHW,
  826. SYSIO_IMAP_KBD,
  827. SYSIO_IMAP_MS,
  828. SYSIO_IMAP_SER,
  829. bogon,
  830. bogon,
  831. SYSIO_IMAP_TIM0,
  832. SYSIO_IMAP_TIM1,
  833. bogon,
  834. bogon,
  835. SYSIO_IMAP_UE,
  836. SYSIO_IMAP_CE,
  837. SYSIO_IMAP_SBERR,
  838. SYSIO_IMAP_PMGMT,
  839. SYSIO_IMAP_GFX,
  840. SYSIO_IMAP_EUPA,
  841. };
  842. #undef bogon
  843. #define NUM_SYSIO_OFFSETS ARRAY_SIZE(sysio_irq_offsets)
  844. /* Convert Interrupt Mapping register pointer to associated
  845. * Interrupt Clear register pointer, SYSIO specific version.
  846. */
  847. #define SYSIO_ICLR_UNUSED0 0x3400UL
  848. #define SYSIO_ICLR_SLOT0 0x340cUL
  849. #define SYSIO_ICLR_SLOT1 0x344cUL
  850. #define SYSIO_ICLR_SLOT2 0x348cUL
  851. #define SYSIO_ICLR_SLOT3 0x34ccUL
  852. static unsigned long sysio_imap_to_iclr(unsigned long imap)
  853. {
  854. unsigned long diff = SYSIO_ICLR_UNUSED0 - SYSIO_IMAP_SLOT0;
  855. return imap + diff;
  856. }
  857. static unsigned int sbus_of_build_irq(struct device_node *dp,
  858. unsigned int ino,
  859. void *_data)
  860. {
  861. unsigned long reg_base = (unsigned long) _data;
  862. const struct linux_prom_registers *regs;
  863. unsigned long imap, iclr;
  864. int sbus_slot = 0;
  865. int sbus_level = 0;
  866. ino &= 0x3f;
  867. regs = of_get_property(dp, "reg", NULL);
  868. if (regs)
  869. sbus_slot = regs->which_io;
  870. if (ino < 0x20)
  871. ino += (sbus_slot * 8);
  872. imap = sysio_irq_offsets[ino];
  873. if (imap == ((unsigned long)-1)) {
  874. prom_printf("get_irq_translations: Bad SYSIO INO[%x]\n",
  875. ino);
  876. prom_halt();
  877. }
  878. imap += reg_base;
  879. /* SYSIO inconsistency. For external SLOTS, we have to select
  880. * the right ICLR register based upon the lower SBUS irq level
  881. * bits.
  882. */
  883. if (ino >= 0x20) {
  884. iclr = sysio_imap_to_iclr(imap);
  885. } else {
  886. sbus_level = ino & 0x7;
  887. switch(sbus_slot) {
  888. case 0:
  889. iclr = reg_base + SYSIO_ICLR_SLOT0;
  890. break;
  891. case 1:
  892. iclr = reg_base + SYSIO_ICLR_SLOT1;
  893. break;
  894. case 2:
  895. iclr = reg_base + SYSIO_ICLR_SLOT2;
  896. break;
  897. default:
  898. case 3:
  899. iclr = reg_base + SYSIO_ICLR_SLOT3;
  900. break;
  901. };
  902. iclr += ((unsigned long)sbus_level - 1UL) * 8UL;
  903. }
  904. return build_irq(sbus_level, iclr, imap);
  905. }
  906. static void sbus_irq_trans_init(struct device_node *dp)
  907. {
  908. const struct linux_prom64_registers *regs;
  909. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  910. dp->irq_trans->irq_build = sbus_of_build_irq;
  911. regs = of_get_property(dp, "reg", NULL);
  912. dp->irq_trans->data = (void *) (unsigned long) regs->phys_addr;
  913. }
  914. #endif /* CONFIG_SBUS */
  915. static unsigned int central_build_irq(struct device_node *dp,
  916. unsigned int ino,
  917. void *_data)
  918. {
  919. struct device_node *central_dp = _data;
  920. struct of_device *central_op = of_find_device_by_node(central_dp);
  921. struct resource *res;
  922. unsigned long imap, iclr;
  923. u32 tmp;
  924. if (!strcmp(dp->name, "eeprom")) {
  925. res = &central_op->resource[5];
  926. } else if (!strcmp(dp->name, "zs")) {
  927. res = &central_op->resource[4];
  928. } else if (!strcmp(dp->name, "clock-board")) {
  929. res = &central_op->resource[3];
  930. } else {
  931. return ino;
  932. }
  933. imap = res->start + 0x00UL;
  934. iclr = res->start + 0x10UL;
  935. /* Set the INO state to idle, and disable. */
  936. upa_writel(0, iclr);
  937. upa_readl(iclr);
  938. tmp = upa_readl(imap);
  939. tmp &= ~0x80000000;
  940. upa_writel(tmp, imap);
  941. return build_irq(0, iclr, imap);
  942. }
  943. static void central_irq_trans_init(struct device_node *dp)
  944. {
  945. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  946. dp->irq_trans->irq_build = central_build_irq;
  947. dp->irq_trans->data = dp;
  948. }
  949. struct irq_trans {
  950. const char *name;
  951. void (*init)(struct device_node *);
  952. };
  953. #ifdef CONFIG_PCI
  954. static struct irq_trans pci_irq_trans_table[] = {
  955. { "SUNW,sabre", sabre_irq_trans_init },
  956. { "pci108e,a000", sabre_irq_trans_init },
  957. { "pci108e,a001", sabre_irq_trans_init },
  958. { "SUNW,psycho", psycho_irq_trans_init },
  959. { "pci108e,8000", psycho_irq_trans_init },
  960. { "SUNW,schizo", schizo_irq_trans_init },
  961. { "pci108e,8001", schizo_irq_trans_init },
  962. { "SUNW,schizo+", schizo_irq_trans_init },
  963. { "pci108e,8002", schizo_irq_trans_init },
  964. { "SUNW,tomatillo", tomatillo_irq_trans_init },
  965. { "pci108e,a801", tomatillo_irq_trans_init },
  966. { "SUNW,sun4v-pci", pci_sun4v_irq_trans_init },
  967. { "pciex108e,80f0", fire_irq_trans_init },
  968. };
  969. #endif
  970. static unsigned int sun4v_vdev_irq_build(struct device_node *dp,
  971. unsigned int devino,
  972. void *_data)
  973. {
  974. u32 devhandle = (u32) (unsigned long) _data;
  975. return sun4v_build_irq(devhandle, devino);
  976. }
  977. static void sun4v_vdev_irq_trans_init(struct device_node *dp)
  978. {
  979. const struct linux_prom64_registers *regs;
  980. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  981. dp->irq_trans->irq_build = sun4v_vdev_irq_build;
  982. regs = of_get_property(dp, "reg", NULL);
  983. dp->irq_trans->data = (void *) (unsigned long)
  984. ((regs->phys_addr >> 32UL) & 0x0fffffff);
  985. }
  986. static void irq_trans_init(struct device_node *dp)
  987. {
  988. #ifdef CONFIG_PCI
  989. const char *model;
  990. int i;
  991. #endif
  992. #ifdef CONFIG_PCI
  993. model = of_get_property(dp, "model", NULL);
  994. if (!model)
  995. model = of_get_property(dp, "compatible", NULL);
  996. if (model) {
  997. for (i = 0; i < ARRAY_SIZE(pci_irq_trans_table); i++) {
  998. struct irq_trans *t = &pci_irq_trans_table[i];
  999. if (!strcmp(model, t->name))
  1000. return t->init(dp);
  1001. }
  1002. }
  1003. #endif
  1004. #ifdef CONFIG_SBUS
  1005. if (!strcmp(dp->name, "sbus") ||
  1006. !strcmp(dp->name, "sbi"))
  1007. return sbus_irq_trans_init(dp);
  1008. #endif
  1009. if (!strcmp(dp->name, "fhc") &&
  1010. !strcmp(dp->parent->name, "central"))
  1011. return central_irq_trans_init(dp);
  1012. if (!strcmp(dp->name, "virtual-devices"))
  1013. return sun4v_vdev_irq_trans_init(dp);
  1014. }
  1015. static int is_root_node(const struct device_node *dp)
  1016. {
  1017. if (!dp)
  1018. return 0;
  1019. return (dp->parent == NULL);
  1020. }
  1021. /* The following routines deal with the black magic of fully naming a
  1022. * node.
  1023. *
  1024. * Certain well known named nodes are just the simple name string.
  1025. *
  1026. * Actual devices have an address specifier appended to the base name
  1027. * string, like this "foo@addr". The "addr" can be in any number of
  1028. * formats, and the platform plus the type of the node determine the
  1029. * format and how it is constructed.
  1030. *
  1031. * For children of the ROOT node, the naming convention is fixed and
  1032. * determined by whether this is a sun4u or sun4v system.
  1033. *
  1034. * For children of other nodes, it is bus type specific. So
  1035. * we walk up the tree until we discover a "device_type" property
  1036. * we recognize and we go from there.
  1037. *
  1038. * As an example, the boot device on my workstation has a full path:
  1039. *
  1040. * /pci@1e,600000/ide@d/disk@0,0:c
  1041. */
  1042. static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
  1043. {
  1044. struct linux_prom64_registers *regs;
  1045. struct property *rprop;
  1046. u32 high_bits, low_bits, type;
  1047. rprop = of_find_property(dp, "reg", NULL);
  1048. if (!rprop)
  1049. return;
  1050. regs = rprop->value;
  1051. if (!is_root_node(dp->parent)) {
  1052. sprintf(tmp_buf, "%s@%x,%x",
  1053. dp->name,
  1054. (unsigned int) (regs->phys_addr >> 32UL),
  1055. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  1056. return;
  1057. }
  1058. type = regs->phys_addr >> 60UL;
  1059. high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
  1060. low_bits = (regs->phys_addr & 0xffffffffUL);
  1061. if (type == 0 || type == 8) {
  1062. const char *prefix = (type == 0) ? "m" : "i";
  1063. if (low_bits)
  1064. sprintf(tmp_buf, "%s@%s%x,%x",
  1065. dp->name, prefix,
  1066. high_bits, low_bits);
  1067. else
  1068. sprintf(tmp_buf, "%s@%s%x",
  1069. dp->name,
  1070. prefix,
  1071. high_bits);
  1072. } else if (type == 12) {
  1073. sprintf(tmp_buf, "%s@%x",
  1074. dp->name, high_bits);
  1075. }
  1076. }
  1077. static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
  1078. {
  1079. struct linux_prom64_registers *regs;
  1080. struct property *prop;
  1081. prop = of_find_property(dp, "reg", NULL);
  1082. if (!prop)
  1083. return;
  1084. regs = prop->value;
  1085. if (!is_root_node(dp->parent)) {
  1086. sprintf(tmp_buf, "%s@%x,%x",
  1087. dp->name,
  1088. (unsigned int) (regs->phys_addr >> 32UL),
  1089. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  1090. return;
  1091. }
  1092. prop = of_find_property(dp, "upa-portid", NULL);
  1093. if (!prop)
  1094. prop = of_find_property(dp, "portid", NULL);
  1095. if (prop) {
  1096. unsigned long mask = 0xffffffffUL;
  1097. if (tlb_type >= cheetah)
  1098. mask = 0x7fffff;
  1099. sprintf(tmp_buf, "%s@%x,%x",
  1100. dp->name,
  1101. *(u32 *)prop->value,
  1102. (unsigned int) (regs->phys_addr & mask));
  1103. }
  1104. }
  1105. /* "name@slot,offset" */
  1106. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  1107. {
  1108. struct linux_prom_registers *regs;
  1109. struct property *prop;
  1110. prop = of_find_property(dp, "reg", NULL);
  1111. if (!prop)
  1112. return;
  1113. regs = prop->value;
  1114. sprintf(tmp_buf, "%s@%x,%x",
  1115. dp->name,
  1116. regs->which_io,
  1117. regs->phys_addr);
  1118. }
  1119. /* "name@devnum[,func]" */
  1120. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  1121. {
  1122. struct linux_prom_pci_registers *regs;
  1123. struct property *prop;
  1124. unsigned int devfn;
  1125. prop = of_find_property(dp, "reg", NULL);
  1126. if (!prop)
  1127. return;
  1128. regs = prop->value;
  1129. devfn = (regs->phys_hi >> 8) & 0xff;
  1130. if (devfn & 0x07) {
  1131. sprintf(tmp_buf, "%s@%x,%x",
  1132. dp->name,
  1133. devfn >> 3,
  1134. devfn & 0x07);
  1135. } else {
  1136. sprintf(tmp_buf, "%s@%x",
  1137. dp->name,
  1138. devfn >> 3);
  1139. }
  1140. }
  1141. /* "name@UPA_PORTID,offset" */
  1142. static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
  1143. {
  1144. struct linux_prom64_registers *regs;
  1145. struct property *prop;
  1146. prop = of_find_property(dp, "reg", NULL);
  1147. if (!prop)
  1148. return;
  1149. regs = prop->value;
  1150. prop = of_find_property(dp, "upa-portid", NULL);
  1151. if (!prop)
  1152. return;
  1153. sprintf(tmp_buf, "%s@%x,%x",
  1154. dp->name,
  1155. *(u32 *) prop->value,
  1156. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  1157. }
  1158. /* "name@reg" */
  1159. static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
  1160. {
  1161. struct property *prop;
  1162. u32 *regs;
  1163. prop = of_find_property(dp, "reg", NULL);
  1164. if (!prop)
  1165. return;
  1166. regs = prop->value;
  1167. sprintf(tmp_buf, "%s@%x", dp->name, *regs);
  1168. }
  1169. /* "name@addrhi,addrlo" */
  1170. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  1171. {
  1172. struct linux_prom64_registers *regs;
  1173. struct property *prop;
  1174. prop = of_find_property(dp, "reg", NULL);
  1175. if (!prop)
  1176. return;
  1177. regs = prop->value;
  1178. sprintf(tmp_buf, "%s@%x,%x",
  1179. dp->name,
  1180. (unsigned int) (regs->phys_addr >> 32UL),
  1181. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  1182. }
  1183. /* "name@bus,addr" */
  1184. static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
  1185. {
  1186. struct property *prop;
  1187. u32 *regs;
  1188. prop = of_find_property(dp, "reg", NULL);
  1189. if (!prop)
  1190. return;
  1191. regs = prop->value;
  1192. /* This actually isn't right... should look at the #address-cells
  1193. * property of the i2c bus node etc. etc.
  1194. */
  1195. sprintf(tmp_buf, "%s@%x,%x",
  1196. dp->name, regs[0], regs[1]);
  1197. }
  1198. /* "name@reg0[,reg1]" */
  1199. static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
  1200. {
  1201. struct property *prop;
  1202. u32 *regs;
  1203. prop = of_find_property(dp, "reg", NULL);
  1204. if (!prop)
  1205. return;
  1206. regs = prop->value;
  1207. if (prop->length == sizeof(u32) || regs[1] == 1) {
  1208. sprintf(tmp_buf, "%s@%x",
  1209. dp->name, regs[0]);
  1210. } else {
  1211. sprintf(tmp_buf, "%s@%x,%x",
  1212. dp->name, regs[0], regs[1]);
  1213. }
  1214. }
  1215. /* "name@reg0reg1[,reg2reg3]" */
  1216. static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
  1217. {
  1218. struct property *prop;
  1219. u32 *regs;
  1220. prop = of_find_property(dp, "reg", NULL);
  1221. if (!prop)
  1222. return;
  1223. regs = prop->value;
  1224. if (regs[2] || regs[3]) {
  1225. sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
  1226. dp->name, regs[0], regs[1], regs[2], regs[3]);
  1227. } else {
  1228. sprintf(tmp_buf, "%s@%08x%08x",
  1229. dp->name, regs[0], regs[1]);
  1230. }
  1231. }
  1232. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  1233. {
  1234. struct device_node *parent = dp->parent;
  1235. if (parent != NULL) {
  1236. if (!strcmp(parent->type, "pci") ||
  1237. !strcmp(parent->type, "pciex"))
  1238. return pci_path_component(dp, tmp_buf);
  1239. if (!strcmp(parent->type, "sbus"))
  1240. return sbus_path_component(dp, tmp_buf);
  1241. if (!strcmp(parent->type, "upa"))
  1242. return upa_path_component(dp, tmp_buf);
  1243. if (!strcmp(parent->type, "ebus"))
  1244. return ebus_path_component(dp, tmp_buf);
  1245. if (!strcmp(parent->name, "usb") ||
  1246. !strcmp(parent->name, "hub"))
  1247. return usb_path_component(dp, tmp_buf);
  1248. if (!strcmp(parent->type, "i2c"))
  1249. return i2c_path_component(dp, tmp_buf);
  1250. if (!strcmp(parent->type, "firewire"))
  1251. return ieee1394_path_component(dp, tmp_buf);
  1252. if (!strcmp(parent->type, "virtual-devices"))
  1253. return vdev_path_component(dp, tmp_buf);
  1254. /* "isa" is handled with platform naming */
  1255. }
  1256. /* Use platform naming convention. */
  1257. if (tlb_type == hypervisor)
  1258. return sun4v_path_component(dp, tmp_buf);
  1259. else
  1260. return sun4u_path_component(dp, tmp_buf);
  1261. }
  1262. static char * __init build_path_component(struct device_node *dp)
  1263. {
  1264. char tmp_buf[64], *n;
  1265. tmp_buf[0] = '\0';
  1266. __build_path_component(dp, tmp_buf);
  1267. if (tmp_buf[0] == '\0')
  1268. strcpy(tmp_buf, dp->name);
  1269. n = prom_early_alloc(strlen(tmp_buf) + 1);
  1270. strcpy(n, tmp_buf);
  1271. return n;
  1272. }
  1273. static char * __init build_full_name(struct device_node *dp)
  1274. {
  1275. int len, ourlen, plen;
  1276. char *n;
  1277. plen = strlen(dp->parent->full_name);
  1278. ourlen = strlen(dp->path_component_name);
  1279. len = ourlen + plen + 2;
  1280. n = prom_early_alloc(len);
  1281. strcpy(n, dp->parent->full_name);
  1282. if (!is_root_node(dp->parent)) {
  1283. strcpy(n + plen, "/");
  1284. plen++;
  1285. }
  1286. strcpy(n + plen, dp->path_component_name);
  1287. return n;
  1288. }
  1289. static unsigned int unique_id;
  1290. static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
  1291. {
  1292. static struct property *tmp = NULL;
  1293. struct property *p;
  1294. if (tmp) {
  1295. p = tmp;
  1296. memset(p, 0, sizeof(*p) + 32);
  1297. tmp = NULL;
  1298. } else {
  1299. p = prom_early_alloc(sizeof(struct property) + 32);
  1300. p->unique_id = unique_id++;
  1301. }
  1302. p->name = (char *) (p + 1);
  1303. if (special_name) {
  1304. strcpy(p->name, special_name);
  1305. p->length = special_len;
  1306. p->value = prom_early_alloc(special_len);
  1307. memcpy(p->value, special_val, special_len);
  1308. } else {
  1309. if (prev == NULL) {
  1310. prom_firstprop(node, p->name);
  1311. } else {
  1312. prom_nextprop(node, prev, p->name);
  1313. }
  1314. if (strlen(p->name) == 0) {
  1315. tmp = p;
  1316. return NULL;
  1317. }
  1318. p->length = prom_getproplen(node, p->name);
  1319. if (p->length <= 0) {
  1320. p->length = 0;
  1321. } else {
  1322. p->value = prom_early_alloc(p->length + 1);
  1323. prom_getproperty(node, p->name, p->value, p->length);
  1324. ((unsigned char *)p->value)[p->length] = '\0';
  1325. }
  1326. }
  1327. return p;
  1328. }
  1329. static struct property * __init build_prop_list(phandle node)
  1330. {
  1331. struct property *head, *tail;
  1332. head = tail = build_one_prop(node, NULL,
  1333. ".node", &node, sizeof(node));
  1334. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  1335. tail = tail->next;
  1336. while(tail) {
  1337. tail->next = build_one_prop(node, tail->name,
  1338. NULL, NULL, 0);
  1339. tail = tail->next;
  1340. }
  1341. return head;
  1342. }
  1343. static char * __init get_one_property(phandle node, const char *name)
  1344. {
  1345. char *buf = "<NULL>";
  1346. int len;
  1347. len = prom_getproplen(node, name);
  1348. if (len > 0) {
  1349. buf = prom_early_alloc(len);
  1350. prom_getproperty(node, name, buf, len);
  1351. }
  1352. return buf;
  1353. }
  1354. static struct device_node * __init create_node(phandle node, struct device_node *parent)
  1355. {
  1356. struct device_node *dp;
  1357. if (!node)
  1358. return NULL;
  1359. dp = prom_early_alloc(sizeof(*dp));
  1360. dp->unique_id = unique_id++;
  1361. dp->parent = parent;
  1362. kref_init(&dp->kref);
  1363. dp->name = get_one_property(node, "name");
  1364. dp->type = get_one_property(node, "device_type");
  1365. dp->node = node;
  1366. dp->properties = build_prop_list(node);
  1367. irq_trans_init(dp);
  1368. return dp;
  1369. }
  1370. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  1371. {
  1372. struct device_node *dp;
  1373. dp = create_node(node, parent);
  1374. if (dp) {
  1375. *(*nextp) = dp;
  1376. *nextp = &dp->allnext;
  1377. dp->path_component_name = build_path_component(dp);
  1378. dp->full_name = build_full_name(dp);
  1379. dp->child = build_tree(dp, prom_getchild(node), nextp);
  1380. dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
  1381. }
  1382. return dp;
  1383. }
  1384. void __init prom_build_devicetree(void)
  1385. {
  1386. struct device_node **nextp;
  1387. allnodes = create_node(prom_root_node, NULL);
  1388. allnodes->path_component_name = "";
  1389. allnodes->full_name = "/";
  1390. nextp = &allnodes->allnext;
  1391. allnodes->child = build_tree(allnodes,
  1392. prom_getchild(allnodes->node),
  1393. &nextp);
  1394. printk("PROM: Built device tree with %u bytes of memory.\n",
  1395. prom_early_allocated);
  1396. }