pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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_device.h>
  33. #include <asm/io.h>
  34. #include <asm/irq.h>
  35. #include <asm/prom.h>
  36. #include <asm/machdep.h>
  37. #include <asm/pci-bridge.h>
  38. #include <asm/ppc-pci.h>
  39. #include "pci.h"
  40. #include "interrupt.h"
  41. #define MAX_PCI_DEVICES 32
  42. #define MAX_PCI_FUNCTIONS 8
  43. #define MAX_PCI_BASE_ADDRS 3 /* use 64 bit address */
  44. /* definition for fake pci configuration area for GbE, .... ,and etc. */
  45. struct celleb_pci_resource {
  46. struct resource r[MAX_PCI_BASE_ADDRS];
  47. };
  48. struct celleb_pci_private {
  49. unsigned char *fake_config[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];
  50. struct celleb_pci_resource *res[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];
  51. };
  52. static inline u8 celleb_fake_config_readb(void *addr)
  53. {
  54. u8 *p = addr;
  55. return *p;
  56. }
  57. static inline u16 celleb_fake_config_readw(void *addr)
  58. {
  59. __le16 *p = addr;
  60. return le16_to_cpu(*p);
  61. }
  62. static inline u32 celleb_fake_config_readl(void *addr)
  63. {
  64. __le32 *p = addr;
  65. return le32_to_cpu(*p);
  66. }
  67. static inline void celleb_fake_config_writeb(u32 val, void *addr)
  68. {
  69. u8 *p = addr;
  70. *p = val;
  71. }
  72. static inline void celleb_fake_config_writew(u32 val, void *addr)
  73. {
  74. __le16 val16;
  75. __le16 *p = addr;
  76. val16 = cpu_to_le16(val);
  77. *p = val16;
  78. }
  79. static inline void celleb_fake_config_writel(u32 val, void *addr)
  80. {
  81. __le32 val32;
  82. __le32 *p = addr;
  83. val32 = cpu_to_le32(val);
  84. *p = val32;
  85. }
  86. static unsigned char *get_fake_config_start(struct pci_controller *hose,
  87. int devno, int fn)
  88. {
  89. struct celleb_pci_private *private = hose->private_data;
  90. if (private == NULL)
  91. return NULL;
  92. return private->fake_config[devno][fn];
  93. }
  94. static struct celleb_pci_resource *get_resource_start(
  95. struct pci_controller *hose,
  96. int devno, int fn)
  97. {
  98. struct celleb_pci_private *private = hose->private_data;
  99. if (private == NULL)
  100. return NULL;
  101. return private->res[devno][fn];
  102. }
  103. static void celleb_config_read_fake(unsigned char *config, int where,
  104. int size, u32 *val)
  105. {
  106. char *p = config + where;
  107. switch (size) {
  108. case 1:
  109. *val = celleb_fake_config_readb(p);
  110. break;
  111. case 2:
  112. *val = celleb_fake_config_readw(p);
  113. break;
  114. case 4:
  115. *val = celleb_fake_config_readl(p);
  116. break;
  117. }
  118. return;
  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. return;
  136. }
  137. static int celleb_fake_pci_read_config(struct pci_bus *bus,
  138. unsigned int devfn, int where, int size, u32 *val)
  139. {
  140. char *config;
  141. struct device_node *node;
  142. struct pci_controller *hose;
  143. unsigned int devno = devfn >> 3;
  144. unsigned int fn = devfn & 0x7;
  145. /* allignment check */
  146. BUG_ON(where % size);
  147. pr_debug(" fake read: bus=0x%x, ", bus->number);
  148. node = (struct device_node *)bus->sysdata;
  149. hose = pci_find_hose_for_OF_device(node);
  150. config = get_fake_config_start(hose, devno, fn);
  151. pr_debug("devno=0x%x, where=0x%x, size=0x%x, ", devno, where, size);
  152. if (!config) {
  153. pr_debug("failed\n");
  154. return PCIBIOS_DEVICE_NOT_FOUND;
  155. }
  156. celleb_config_read_fake(config, where, size, val);
  157. pr_debug("val=0x%x\n", *val);
  158. return PCIBIOS_SUCCESSFUL;
  159. }
  160. static int celleb_fake_pci_write_config(struct pci_bus *bus,
  161. unsigned int devfn, int where, int size, u32 val)
  162. {
  163. char *config;
  164. struct device_node *node;
  165. struct pci_controller *hose;
  166. struct celleb_pci_resource *res;
  167. unsigned int devno = devfn >> 3;
  168. unsigned int fn = devfn & 0x7;
  169. /* allignment check */
  170. BUG_ON(where % size);
  171. node = (struct device_node *)bus->sysdata;
  172. hose = pci_find_hose_for_OF_device(node);
  173. config = get_fake_config_start(hose, devno, fn);
  174. if (!config)
  175. return PCIBIOS_DEVICE_NOT_FOUND;
  176. if (val == ~0) {
  177. int i = (where - PCI_BASE_ADDRESS_0) >> 3;
  178. switch (where) {
  179. case PCI_BASE_ADDRESS_0:
  180. case PCI_BASE_ADDRESS_2:
  181. if (size != 4)
  182. return PCIBIOS_DEVICE_NOT_FOUND;
  183. res = get_resource_start(hose, devno, fn);
  184. if (!res)
  185. return PCIBIOS_DEVICE_NOT_FOUND;
  186. celleb_config_write_fake(config, where, size,
  187. (res->r[i].end - res->r[i].start));
  188. return PCIBIOS_SUCCESSFUL;
  189. case PCI_BASE_ADDRESS_1:
  190. case PCI_BASE_ADDRESS_3:
  191. case PCI_BASE_ADDRESS_4:
  192. case PCI_BASE_ADDRESS_5:
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. celleb_config_write_fake(config, where, size, val);
  199. pr_debug(" fake write: where=%x, size=%d, val=%x\n",
  200. where, size, val);
  201. return PCIBIOS_SUCCESSFUL;
  202. }
  203. static struct pci_ops celleb_fake_pci_ops = {
  204. .read = celleb_fake_pci_read_config,
  205. .write = celleb_fake_pci_write_config,
  206. };
  207. static inline void celleb_setup_pci_base_addrs(struct pci_controller *hose,
  208. unsigned int devno, unsigned int fn,
  209. unsigned int num_base_addr)
  210. {
  211. u32 val;
  212. unsigned char *config;
  213. struct celleb_pci_resource *res;
  214. config = get_fake_config_start(hose, devno, fn);
  215. res = get_resource_start(hose, devno, fn);
  216. if (!config || !res)
  217. return;
  218. switch (num_base_addr) {
  219. case 3:
  220. val = (res->r[2].start & 0xfffffff0)
  221. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  222. celleb_config_write_fake(config, PCI_BASE_ADDRESS_4, 4, val);
  223. val = res->r[2].start >> 32;
  224. celleb_config_write_fake(config, PCI_BASE_ADDRESS_5, 4, val);
  225. /* FALLTHROUGH */
  226. case 2:
  227. val = (res->r[1].start & 0xfffffff0)
  228. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  229. celleb_config_write_fake(config, PCI_BASE_ADDRESS_2, 4, val);
  230. val = res->r[1].start >> 32;
  231. celleb_config_write_fake(config, PCI_BASE_ADDRESS_3, 4, val);
  232. /* FALLTHROUGH */
  233. case 1:
  234. val = (res->r[0].start & 0xfffffff0)
  235. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  236. celleb_config_write_fake(config, PCI_BASE_ADDRESS_0, 4, val);
  237. val = res->r[0].start >> 32;
  238. celleb_config_write_fake(config, PCI_BASE_ADDRESS_1, 4, val);
  239. break;
  240. }
  241. val = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  242. celleb_config_write_fake(config, PCI_COMMAND, 2, val);
  243. }
  244. static int __init celleb_setup_fake_pci_device(struct device_node *node,
  245. struct pci_controller *hose)
  246. {
  247. unsigned int rlen;
  248. int num_base_addr = 0;
  249. u32 val;
  250. const u32 *wi0, *wi1, *wi2, *wi3, *wi4;
  251. unsigned int devno, fn;
  252. struct celleb_pci_private *private = hose->private_data;
  253. unsigned char **config = NULL;
  254. struct celleb_pci_resource **res = NULL;
  255. const char *name;
  256. const unsigned long *li;
  257. int size, result;
  258. if (private == NULL) {
  259. printk(KERN_ERR "PCI: "
  260. "memory space for pci controller is not assigned\n");
  261. goto error;
  262. }
  263. name = of_get_property(node, "model", &rlen);
  264. if (!name) {
  265. printk(KERN_ERR "PCI: model property not found.\n");
  266. goto error;
  267. }
  268. wi4 = of_get_property(node, "reg", &rlen);
  269. if (wi4 == NULL)
  270. goto error;
  271. devno = ((wi4[0] >> 8) & 0xff) >> 3;
  272. fn = (wi4[0] >> 8) & 0x7;
  273. pr_debug("PCI: celleb_setup_fake_pci() %s devno=%x fn=%x\n", name,
  274. devno, fn);
  275. size = 256;
  276. config = &private->fake_config[devno][fn];
  277. *config = alloc_maybe_bootmem(size, GFP_KERNEL);
  278. if (*config == NULL) {
  279. printk(KERN_ERR "PCI: "
  280. "not enough memory for fake configuration space\n");
  281. goto error;
  282. }
  283. pr_debug("PCI: fake config area assigned 0x%016lx\n",
  284. (unsigned long)*config);
  285. size = sizeof(struct celleb_pci_resource);
  286. res = &private->res[devno][fn];
  287. *res = alloc_maybe_bootmem(size, GFP_KERNEL);
  288. if (*res == NULL) {
  289. printk(KERN_ERR
  290. "PCI: not enough memory for resource data space\n");
  291. goto error;
  292. }
  293. pr_debug("PCI: res assigned 0x%016lx\n", (unsigned long)*res);
  294. wi0 = of_get_property(node, "device-id", NULL);
  295. wi1 = of_get_property(node, "vendor-id", NULL);
  296. wi2 = of_get_property(node, "class-code", NULL);
  297. wi3 = of_get_property(node, "revision-id", NULL);
  298. celleb_config_write_fake(*config, PCI_DEVICE_ID, 2, wi0[0] & 0xffff);
  299. celleb_config_write_fake(*config, PCI_VENDOR_ID, 2, wi1[0] & 0xffff);
  300. pr_debug("class-code = 0x%08x\n", wi2[0]);
  301. celleb_config_write_fake(*config, PCI_CLASS_PROG, 1, wi2[0] & 0xff);
  302. celleb_config_write_fake(*config, PCI_CLASS_DEVICE, 2,
  303. (wi2[0] >> 8) & 0xffff);
  304. celleb_config_write_fake(*config, PCI_REVISION_ID, 1, wi3[0]);
  305. while (num_base_addr < MAX_PCI_BASE_ADDRS) {
  306. result = of_address_to_resource(node,
  307. num_base_addr, &(*res)->r[num_base_addr]);
  308. if (result)
  309. break;
  310. num_base_addr++;
  311. }
  312. celleb_setup_pci_base_addrs(hose, devno, fn, num_base_addr);
  313. li = of_get_property(node, "interrupts", &rlen);
  314. val = li[0];
  315. celleb_config_write_fake(*config, PCI_INTERRUPT_PIN, 1, 1);
  316. celleb_config_write_fake(*config, PCI_INTERRUPT_LINE, 1, val);
  317. #ifdef DEBUG
  318. pr_debug("PCI: %s irq=%ld\n", name, li[0]);
  319. for (i = 0; i < 6; i++) {
  320. celleb_config_read_fake(*config,
  321. PCI_BASE_ADDRESS_0 + 0x4 * i, 4,
  322. &val);
  323. pr_debug("PCI: %s fn=%d base_address_%d=0x%x\n",
  324. name, fn, i, val);
  325. }
  326. #endif
  327. celleb_config_write_fake(*config, PCI_HEADER_TYPE, 1,
  328. PCI_HEADER_TYPE_NORMAL);
  329. return 0;
  330. error:
  331. if (mem_init_done) {
  332. if (config && *config)
  333. kfree(*config);
  334. if (res && *res)
  335. kfree(*res);
  336. } else {
  337. if (config && *config) {
  338. size = 256;
  339. free_bootmem((unsigned long)(*config), size);
  340. }
  341. if (res && *res) {
  342. size = sizeof(struct celleb_pci_resource);
  343. free_bootmem((unsigned long)(*res), size);
  344. }
  345. }
  346. return 1;
  347. }
  348. static int __init phb_set_bus_ranges(struct device_node *dev,
  349. struct pci_controller *phb)
  350. {
  351. const int *bus_range;
  352. unsigned int len;
  353. bus_range = of_get_property(dev, "bus-range", &len);
  354. if (bus_range == NULL || len < 2 * sizeof(int))
  355. return 1;
  356. phb->first_busno = bus_range[0];
  357. phb->last_busno = bus_range[1];
  358. return 0;
  359. }
  360. static void __init celleb_alloc_private_mem(struct pci_controller *hose)
  361. {
  362. hose->private_data =
  363. alloc_maybe_bootmem(sizeof(struct celleb_pci_private),
  364. GFP_KERNEL);
  365. }
  366. static int __init celleb_setup_fake_pci(struct device_node *dev,
  367. struct pci_controller *phb)
  368. {
  369. struct device_node *node;
  370. phb->ops = &celleb_fake_pci_ops;
  371. celleb_alloc_private_mem(phb);
  372. for (node = of_get_next_child(dev, NULL);
  373. node != NULL; node = of_get_next_child(dev, node))
  374. celleb_setup_fake_pci_device(node, phb);
  375. return 0;
  376. }
  377. void __init fake_pci_workaround_init(struct pci_controller *phb)
  378. {
  379. /**
  380. * We will add fake pci bus to scc_pci_bus for the purpose to improve
  381. * I/O Macro performance. But device-tree and device drivers
  382. * are not ready to use address with a token.
  383. */
  384. /* celleb_pci_add_one(phb, NULL); */
  385. }
  386. static struct of_device_id celleb_phb_match[] __initdata = {
  387. {
  388. .name = "pci-pseudo",
  389. .data = celleb_setup_fake_pci,
  390. }, {
  391. .name = "epci",
  392. .data = celleb_setup_epci,
  393. }, {
  394. },
  395. };
  396. int __init celleb_setup_phb(struct pci_controller *phb)
  397. {
  398. struct device_node *dev = phb->arch_data;
  399. const struct of_device_id *match;
  400. int (*setup_func)(struct device_node *, struct pci_controller *);
  401. match = of_match_node(celleb_phb_match, dev);
  402. if (!match)
  403. return 1;
  404. phb_set_bus_ranges(dev, phb);
  405. phb->buid = 1;
  406. setup_func = match->data;
  407. return (*setup_func)(dev, phb);
  408. }
  409. int celleb_pci_probe_mode(struct pci_bus *bus)
  410. {
  411. return PCI_PROBE_DEVTREE;
  412. }