celleb_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Support for PCI on Celleb platform.
  3. *
  4. * (C) Copyright 2006-2007 TOSHIBA CORPORATION
  5. *
  6. * This code is based on arch/powerpc/kernel/rtas_pci.c:
  7. * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
  8. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. #undef DEBUG
  25. #include <linux/kernel.h>
  26. #include <linux/threads.h>
  27. #include <linux/pci.h>
  28. #include <linux/string.h>
  29. #include <linux/init.h>
  30. #include <linux/bootmem.h>
  31. #include <linux/pci_regs.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/slab.h>
  35. #include <asm/io.h>
  36. #include <asm/irq.h>
  37. #include <asm/prom.h>
  38. #include <asm/pci-bridge.h>
  39. #include <asm/ppc-pci.h>
  40. #include "io-workarounds.h"
  41. #include "celleb_pci.h"
  42. #define MAX_PCI_DEVICES 32
  43. #define MAX_PCI_FUNCTIONS 8
  44. #define MAX_PCI_BASE_ADDRS 3 /* use 64 bit address */
  45. /* definition for fake pci configuration area for GbE, .... ,and etc. */
  46. struct celleb_pci_resource {
  47. struct resource r[MAX_PCI_BASE_ADDRS];
  48. };
  49. struct celleb_pci_private {
  50. unsigned char *fake_config[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];
  51. struct celleb_pci_resource *res[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];
  52. };
  53. static inline u8 celleb_fake_config_readb(void *addr)
  54. {
  55. u8 *p = addr;
  56. return *p;
  57. }
  58. static inline u16 celleb_fake_config_readw(void *addr)
  59. {
  60. __le16 *p = addr;
  61. return le16_to_cpu(*p);
  62. }
  63. static inline u32 celleb_fake_config_readl(void *addr)
  64. {
  65. __le32 *p = addr;
  66. return le32_to_cpu(*p);
  67. }
  68. static inline void celleb_fake_config_writeb(u32 val, void *addr)
  69. {
  70. u8 *p = addr;
  71. *p = val;
  72. }
  73. static inline void celleb_fake_config_writew(u32 val, void *addr)
  74. {
  75. __le16 val16;
  76. __le16 *p = addr;
  77. val16 = cpu_to_le16(val);
  78. *p = val16;
  79. }
  80. static inline void celleb_fake_config_writel(u32 val, void *addr)
  81. {
  82. __le32 val32;
  83. __le32 *p = addr;
  84. val32 = cpu_to_le32(val);
  85. *p = val32;
  86. }
  87. static unsigned char *get_fake_config_start(struct pci_controller *hose,
  88. int devno, int fn)
  89. {
  90. struct celleb_pci_private *private = hose->private_data;
  91. if (private == NULL)
  92. return NULL;
  93. return private->fake_config[devno][fn];
  94. }
  95. static struct celleb_pci_resource *get_resource_start(
  96. struct pci_controller *hose,
  97. int devno, int fn)
  98. {
  99. struct celleb_pci_private *private = hose->private_data;
  100. if (private == NULL)
  101. return NULL;
  102. return private->res[devno][fn];
  103. }
  104. static void celleb_config_read_fake(unsigned char *config, int where,
  105. int size, u32 *val)
  106. {
  107. char *p = config + where;
  108. switch (size) {
  109. case 1:
  110. *val = celleb_fake_config_readb(p);
  111. break;
  112. case 2:
  113. *val = celleb_fake_config_readw(p);
  114. break;
  115. case 4:
  116. *val = celleb_fake_config_readl(p);
  117. break;
  118. }
  119. }
  120. static void celleb_config_write_fake(unsigned char *config, int where,
  121. int size, u32 val)
  122. {
  123. char *p = config + where;
  124. switch (size) {
  125. case 1:
  126. celleb_fake_config_writeb(val, p);
  127. break;
  128. case 2:
  129. celleb_fake_config_writew(val, p);
  130. break;
  131. case 4:
  132. celleb_fake_config_writel(val, p);
  133. break;
  134. }
  135. }
  136. static int celleb_fake_pci_read_config(struct pci_bus *bus,
  137. unsigned int devfn, int where, int size, u32 *val)
  138. {
  139. char *config;
  140. struct pci_controller *hose = pci_bus_to_host(bus);
  141. unsigned int devno = devfn >> 3;
  142. unsigned int fn = devfn & 0x7;
  143. /* allignment check */
  144. BUG_ON(where % size);
  145. pr_debug(" fake read: bus=0x%x, ", bus->number);
  146. config = get_fake_config_start(hose, devno, fn);
  147. pr_debug("devno=0x%x, where=0x%x, size=0x%x, ", devno, where, size);
  148. if (!config) {
  149. pr_debug("failed\n");
  150. return PCIBIOS_DEVICE_NOT_FOUND;
  151. }
  152. celleb_config_read_fake(config, where, size, val);
  153. pr_debug("val=0x%x\n", *val);
  154. return PCIBIOS_SUCCESSFUL;
  155. }
  156. static int celleb_fake_pci_write_config(struct pci_bus *bus,
  157. unsigned int devfn, int where, int size, u32 val)
  158. {
  159. char *config;
  160. struct pci_controller *hose = pci_bus_to_host(bus);
  161. struct celleb_pci_resource *res;
  162. unsigned int devno = devfn >> 3;
  163. unsigned int fn = devfn & 0x7;
  164. /* allignment check */
  165. BUG_ON(where % size);
  166. config = get_fake_config_start(hose, devno, fn);
  167. if (!config)
  168. return PCIBIOS_DEVICE_NOT_FOUND;
  169. if (val == ~0) {
  170. int i = (where - PCI_BASE_ADDRESS_0) >> 3;
  171. switch (where) {
  172. case PCI_BASE_ADDRESS_0:
  173. case PCI_BASE_ADDRESS_2:
  174. if (size != 4)
  175. return PCIBIOS_DEVICE_NOT_FOUND;
  176. res = get_resource_start(hose, devno, fn);
  177. if (!res)
  178. return PCIBIOS_DEVICE_NOT_FOUND;
  179. celleb_config_write_fake(config, where, size,
  180. (res->r[i].end - res->r[i].start));
  181. return PCIBIOS_SUCCESSFUL;
  182. case PCI_BASE_ADDRESS_1:
  183. case PCI_BASE_ADDRESS_3:
  184. case PCI_BASE_ADDRESS_4:
  185. case PCI_BASE_ADDRESS_5:
  186. break;
  187. default:
  188. break;
  189. }
  190. }
  191. celleb_config_write_fake(config, where, size, val);
  192. pr_debug(" fake write: where=%x, size=%d, val=%x\n",
  193. where, size, val);
  194. return PCIBIOS_SUCCESSFUL;
  195. }
  196. static struct pci_ops celleb_fake_pci_ops = {
  197. .read = celleb_fake_pci_read_config,
  198. .write = celleb_fake_pci_write_config,
  199. };
  200. static inline void celleb_setup_pci_base_addrs(struct pci_controller *hose,
  201. unsigned int devno, unsigned int fn,
  202. unsigned int num_base_addr)
  203. {
  204. u32 val;
  205. unsigned char *config;
  206. struct celleb_pci_resource *res;
  207. config = get_fake_config_start(hose, devno, fn);
  208. res = get_resource_start(hose, devno, fn);
  209. if (!config || !res)
  210. return;
  211. switch (num_base_addr) {
  212. case 3:
  213. val = (res->r[2].start & 0xfffffff0)
  214. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  215. celleb_config_write_fake(config, PCI_BASE_ADDRESS_4, 4, val);
  216. val = res->r[2].start >> 32;
  217. celleb_config_write_fake(config, PCI_BASE_ADDRESS_5, 4, val);
  218. /* FALLTHROUGH */
  219. case 2:
  220. val = (res->r[1].start & 0xfffffff0)
  221. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  222. celleb_config_write_fake(config, PCI_BASE_ADDRESS_2, 4, val);
  223. val = res->r[1].start >> 32;
  224. celleb_config_write_fake(config, PCI_BASE_ADDRESS_3, 4, val);
  225. /* FALLTHROUGH */
  226. case 1:
  227. val = (res->r[0].start & 0xfffffff0)
  228. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  229. celleb_config_write_fake(config, PCI_BASE_ADDRESS_0, 4, val);
  230. val = res->r[0].start >> 32;
  231. celleb_config_write_fake(config, PCI_BASE_ADDRESS_1, 4, val);
  232. break;
  233. }
  234. val = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  235. celleb_config_write_fake(config, PCI_COMMAND, 2, val);
  236. }
  237. static int __init celleb_setup_fake_pci_device(struct device_node *node,
  238. struct pci_controller *hose)
  239. {
  240. unsigned int rlen;
  241. int num_base_addr = 0;
  242. u32 val;
  243. const u32 *wi0, *wi1, *wi2, *wi3, *wi4;
  244. unsigned int devno, fn;
  245. struct celleb_pci_private *private = hose->private_data;
  246. unsigned char **config = NULL;
  247. struct celleb_pci_resource **res = NULL;
  248. const char *name;
  249. const unsigned long *li;
  250. int size, result;
  251. if (private == NULL) {
  252. printk(KERN_ERR "PCI: "
  253. "memory space for pci controller is not assigned\n");
  254. goto error;
  255. }
  256. name = of_get_property(node, "model", &rlen);
  257. if (!name) {
  258. printk(KERN_ERR "PCI: model property not found.\n");
  259. goto error;
  260. }
  261. wi4 = of_get_property(node, "reg", &rlen);
  262. if (wi4 == NULL)
  263. goto error;
  264. devno = ((wi4[0] >> 8) & 0xff) >> 3;
  265. fn = (wi4[0] >> 8) & 0x7;
  266. pr_debug("PCI: celleb_setup_fake_pci() %s devno=%x fn=%x\n", name,
  267. devno, fn);
  268. size = 256;
  269. config = &private->fake_config[devno][fn];
  270. *config = alloc_maybe_bootmem(size, GFP_KERNEL);
  271. if (*config == NULL) {
  272. printk(KERN_ERR "PCI: "
  273. "not enough memory for fake configuration space\n");
  274. goto error;
  275. }
  276. pr_debug("PCI: fake config area assigned 0x%016lx\n",
  277. (unsigned long)*config);
  278. size = sizeof(struct celleb_pci_resource);
  279. res = &private->res[devno][fn];
  280. *res = alloc_maybe_bootmem(size, GFP_KERNEL);
  281. if (*res == NULL) {
  282. printk(KERN_ERR
  283. "PCI: not enough memory for resource data space\n");
  284. goto error;
  285. }
  286. pr_debug("PCI: res assigned 0x%016lx\n", (unsigned long)*res);
  287. wi0 = of_get_property(node, "device-id", NULL);
  288. wi1 = of_get_property(node, "vendor-id", NULL);
  289. wi2 = of_get_property(node, "class-code", NULL);
  290. wi3 = of_get_property(node, "revision-id", NULL);
  291. if (!wi0 || !wi1 || !wi2 || !wi3) {
  292. printk(KERN_ERR "PCI: Missing device tree properties.\n");
  293. goto error;
  294. }
  295. celleb_config_write_fake(*config, PCI_DEVICE_ID, 2, wi0[0] & 0xffff);
  296. celleb_config_write_fake(*config, PCI_VENDOR_ID, 2, wi1[0] & 0xffff);
  297. pr_debug("class-code = 0x%08x\n", wi2[0]);
  298. celleb_config_write_fake(*config, PCI_CLASS_PROG, 1, wi2[0] & 0xff);
  299. celleb_config_write_fake(*config, PCI_CLASS_DEVICE, 2,
  300. (wi2[0] >> 8) & 0xffff);
  301. celleb_config_write_fake(*config, PCI_REVISION_ID, 1, wi3[0]);
  302. while (num_base_addr < MAX_PCI_BASE_ADDRS) {
  303. result = of_address_to_resource(node,
  304. num_base_addr, &(*res)->r[num_base_addr]);
  305. if (result)
  306. break;
  307. num_base_addr++;
  308. }
  309. celleb_setup_pci_base_addrs(hose, devno, fn, num_base_addr);
  310. li = of_get_property(node, "interrupts", &rlen);
  311. if (!li) {
  312. printk(KERN_ERR "PCI: interrupts not found.\n");
  313. goto error;
  314. }
  315. val = li[0];
  316. celleb_config_write_fake(*config, PCI_INTERRUPT_PIN, 1, 1);
  317. celleb_config_write_fake(*config, PCI_INTERRUPT_LINE, 1, val);
  318. #ifdef DEBUG
  319. pr_debug("PCI: %s irq=%ld\n", name, li[0]);
  320. for (i = 0; i < 6; i++) {
  321. celleb_config_read_fake(*config,
  322. PCI_BASE_ADDRESS_0 + 0x4 * i, 4,
  323. &val);
  324. pr_debug("PCI: %s fn=%d base_address_%d=0x%x\n",
  325. name, fn, i, val);
  326. }
  327. #endif
  328. celleb_config_write_fake(*config, PCI_HEADER_TYPE, 1,
  329. PCI_HEADER_TYPE_NORMAL);
  330. return 0;
  331. error:
  332. if (mem_init_done) {
  333. if (config && *config)
  334. kfree(*config);
  335. if (res && *res)
  336. kfree(*res);
  337. } else {
  338. if (config && *config) {
  339. size = 256;
  340. free_bootmem((unsigned long)(*config), size);
  341. }
  342. if (res && *res) {
  343. size = sizeof(struct celleb_pci_resource);
  344. free_bootmem((unsigned long)(*res), size);
  345. }
  346. }
  347. return 1;
  348. }
  349. static int __init phb_set_bus_ranges(struct device_node *dev,
  350. struct pci_controller *phb)
  351. {
  352. const int *bus_range;
  353. unsigned int len;
  354. bus_range = of_get_property(dev, "bus-range", &len);
  355. if (bus_range == NULL || len < 2 * sizeof(int))
  356. return 1;
  357. phb->first_busno = bus_range[0];
  358. phb->last_busno = bus_range[1];
  359. return 0;
  360. }
  361. static void __init celleb_alloc_private_mem(struct pci_controller *hose)
  362. {
  363. hose->private_data =
  364. alloc_maybe_bootmem(sizeof(struct celleb_pci_private),
  365. GFP_KERNEL);
  366. }
  367. static int __init celleb_setup_fake_pci(struct device_node *dev,
  368. struct pci_controller *phb)
  369. {
  370. struct device_node *node;
  371. phb->ops = &celleb_fake_pci_ops;
  372. celleb_alloc_private_mem(phb);
  373. for (node = of_get_next_child(dev, NULL);
  374. node != NULL; node = of_get_next_child(dev, node))
  375. celleb_setup_fake_pci_device(node, phb);
  376. return 0;
  377. }
  378. static struct celleb_phb_spec celleb_fake_pci_spec __initdata = {
  379. .setup = celleb_setup_fake_pci,
  380. };
  381. static struct of_device_id celleb_phb_match[] __initdata = {
  382. {
  383. .name = "pci-pseudo",
  384. .data = &celleb_fake_pci_spec,
  385. }, {
  386. .name = "epci",
  387. .data = &celleb_epci_spec,
  388. }, {
  389. .name = "pcie",
  390. .data = &celleb_pciex_spec,
  391. }, {
  392. },
  393. };
  394. static int __init celleb_io_workaround_init(struct pci_controller *phb,
  395. struct celleb_phb_spec *phb_spec)
  396. {
  397. if (phb_spec->ops) {
  398. iowa_register_bus(phb, phb_spec->ops, phb_spec->iowa_init,
  399. phb_spec->iowa_data);
  400. io_workaround_init();
  401. }
  402. return 0;
  403. }
  404. int __init celleb_setup_phb(struct pci_controller *phb)
  405. {
  406. struct device_node *dev = phb->dn;
  407. const struct of_device_id *match;
  408. struct celleb_phb_spec *phb_spec;
  409. int rc;
  410. match = of_match_node(celleb_phb_match, dev);
  411. if (!match)
  412. return 1;
  413. phb_set_bus_ranges(dev, phb);
  414. phb->buid = 1;
  415. phb_spec = match->data;
  416. rc = (*phb_spec->setup)(dev, phb);
  417. if (rc)
  418. return 1;
  419. return celleb_io_workaround_init(phb, phb_spec);
  420. }
  421. int celleb_pci_probe_mode(struct pci_bus *bus)
  422. {
  423. return PCI_PROBE_DEVTREE;
  424. }