prom.c 44 KB

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